CASA/c_adlib/ad_ff/native/FirefoxPasswordManager.cpp
Manohar 0bc1f679e4
2006-03-06 09:54:36 +00:00

593 lines
15 KiB
C++

#include "FirefoxPasswordManager.h"
#include "Common.h"
#include "ProfileManager.h"
ProfileManager profileManager[MAX_PROFILE_COUNT];
int profileCount = 0;
/*
* Gets the list of profile names...
*
* @param[in/out] profiles pointer to array of profile names
* @param[in/out] profileFlag Indicates if default profile or not.
* @return count count of profiles
* 0 no profiles found
* < 0 on error
*
* If one or more profiles found then profiles array is filled with
* the profile names and count of profiles is returned. ProfileFlag[]
* array is filled with 1 or 0 to indicate if the respective profile
* is default profile or not.If no profiles found then value 0 is
* returned and negative values is returned if there is an error.
*
*/
extern "C" APIEXPORT int FPM_GetProfileList(char **profileList[], int *profileFlag[])
{
#ifdef WIN32
char profileDir[MAX_PATH] = "";
char partialPath[] = "Application Data\\Mozilla\\Firefox";
char profilePath[MAX_PATH];
char line[1024];
DWORD pathSize = MAX_PATH;
char *finalProfilePath = NULL;
int profileCount = 0;
unsigned int i;
HANDLE token;
// Get current user's profile directory
if( OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) == FALSE )
{
PrintMessage(MESG_ERROR, "\n GetProfileList : Failed to get current process token ");
return FPM_FALSE;
}
if( GetUserProfileDirectory(token, profileDir, &pathSize) == FALSE )
{
PrintMessage(MESG_ERROR, "\n GetProfileList : Failed to get user profile directory");
return FPM_FALSE;
}
PrintMessage(MESG_DEBUG, "\n GetProfileList : User Profile directory = %s", profileDir);
// Get firefox profile directory
strcpy(profilePath, profileDir);
strcat(profilePath,"\\");
strcat(profilePath,partialPath);
strcat(profilePath,"\\profiles.ini");
PrintMessage(MESG_DEBUG, "\n GetProfileList : Firefox profile dir path = %s ", profilePath);
#else // Linux platform....
char profileDir[] ="/.mozilla/firefox";
char profileFile[] ="/.mozilla/firefox/profiles.ini";
char line[1024];
char *profilePath = NULL;
char *homeDir = NULL;
char *finalProfilePath = NULL;
int profileCount = 0;
unsigned int i;
// Get home directory
homeDir = getenv("HOME");
if(homeDir == NULL )
{
PrintMessage(MESG_ERROR, "\n GetProfileList : Unable to get home directory ");
return FPM_FALSE;
}
profilePath = (char*) malloc( strlen(homeDir) + strlen(profileFile) + 3 );
if( profilePath == NULL )
{
PrintMessage(MESG_ERROR, "\n GetProfileList : Insufficient memory ");
return FPM_FALSE;
}
strcpy(profilePath,homeDir);
strcat(profilePath,profileFile);
PrintMessage(MESG_DEBUG, "\n GetProfileList : Firefox profile dir path = %s ", profilePath);
#endif
// Open the firefox profile setting file
FILE *profile = fopen(profilePath, "r");
if( profile == NULL )
{
PrintMessage(MESG_ERROR, "\n GetProfileList : Unable to find firefox profile file : %s ", profilePath);
return FPM_FALSE;
}
// First find out the count of profiles....
profileCount = 0;
while(fgets(line, 1024, profile))
{
// Remove trailing end of line character
line[strlen(line)-1]= 0;
// Convert to smaller case until "=" found....
for(i=0; i<strlen(line); i++)
{
if( line[i] == '=' )
break;
if( line[i] >=65 && line[i]<=90 )
line[i]+=32;
}
if( strstr(line, "name=") != NULL )
profileCount++;
}
PrintMessage(MESG_DEBUG, "\n GetProfileList : Total profiles found = %d ", profileCount);
if( profileCount == 0 )
{
fclose(profile);
return FPM_FALSE;
}
*profileList = ( char**) malloc(profileCount * sizeof (char *));
*profileFlag = ( int * ) malloc(profileCount * sizeof(int));
if( *profileList == NULL || *profileFlag == NULL )
{
PrintMessage(MESG_ERROR, "\n GetProfileList : Insufficient memory ");
fclose(profile);
return FPM_FALSE;
}
char **profList = *profileList;
int *profFlag = *profileFlag;
// Now read the profile names and store it..
fseek(profile, 0, SEEK_SET);
profileCount = 0;
while(fgets(line, 1024, profile))
{
// Remove trailing end of line character
line[strlen(line)-1]= 0;
// Convert to smaller case until "=" found....
for(i=0; i<strlen(line); i++)
{
if( line[i] == '=' )
break;
if( line[i] >=65 && line[i]<=90 )
line[i]+=32;
}
if( strstr(line, "name=") != NULL )
{
char *temp = strchr(line,'=') + 1;
profList[profileCount] = (char*) malloc(strlen(temp)+1);
if( profList[profileCount] == NULL )
{
PrintMessage(MESG_ERROR, "\n GetProfileList : Insufficient memory ");
fclose(profile);
return 0;
}
strcpy(profList[profileCount],temp);
profFlag[profileCount] = 0;
PrintMessage(MESG_DEBUG, "\n GetProfileList : Found profile = [%s]", profList[profileCount]);
profileCount++;
continue;
}
// check if the current profile is default
if( strstr(line, "default=1") != NULL )
{
profFlag[profileCount-1] = 1;
}
}
fclose(profile);
// if there is only one profile then set it default profile
if( profileCount == 1 )
{
**profileFlag = 1;
}
return profileCount;
}
extern "C" APIEXPORT int FPM_FirefoxProfileInit(char *profileName)
{
int retValue;
int profileIndex = -1;
// Check if the object for specified profile already present...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
PrintMessage(MESG_ERROR, "\n FirefoxProfileInit : Object for specified profile %s exist ", profileName);
profileIndex = i;
break;
}
}
}
// This is new profile...
if( profileIndex == -1)
{
if( (profileCount + 1) >= MAX_PROFILE_COUNT)
{
PrintMessage(MESG_ERROR, "\n FirefoxProfileInit : Max profile count exceeded.");
return FPM_PROFILE_LIMIT_EXCEEDED;
}
profileIndex = profileCount;
profileCount++;
}
// check if the profile is already initialized...
if( profileManager[profileIndex].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_ERROR, "\n FirefoxProfileInit : Specified profile %s is already initialized", profileName);
return FPM_TRUE;
}
if( (retValue = profileManager[profileIndex].ProfileInit(profileName)) != FPM_TRUE )
{
PrintMessage(MESG_ERROR, "\n FirefoxProfileInit : Failed to initialize the profile %s ", profileName);
return retValue;
}
PrintMessage(MESG_DEBUG, "\n FirefoxProfileInit : Firefox profile %s initialized successfully ", profileName);
return FPM_TRUE;
}
extern "C" APIEXPORT int FPM_FirefoxProfileExit(char *profileName)
{
// Find the profile...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
// check if its initialized
if( profileManager[i].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_DEBUG, "\n FirefoxProfileExit : Exiting the firefox profile %s ", profileName);
profileManager[i].ProfileExit();
return FPM_TRUE;
}
else
{
PrintMessage(MESG_ERROR, "\n FirefoxProfileExit : Specified profile %s is not initialized , cannot exit the profile", profileName);
return FPM_PROFILE_NOT_INITIALIZED;
}
}
}
}
PrintMessage(MESG_ERROR, "\n FirefoxProfileExit : Specified profile %s is not found", profileName);
return FPM_PROFILE_NOT_PRESENT;
}
extern "C" APIEXPORT int FPM_IsMasterPasswordSet(char *profileName)
{
// Find the profile...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
// check if its initialized
if( profileManager[i].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_DEBUG, "\n IsMasterPasswordSet : invoking IsMasterPasswordSet for profile %s", profileName);
return profileManager[i].IsMasterPasswordSet();
}
else
{
PrintMessage(MESG_ERROR, "\n IsMasterPasswordSet : Specified profile %s is not initialized ", profileName);
return FPM_PROFILE_NOT_INITIALIZED;
}
}
}
}
PrintMessage(MESG_ERROR, "\n IsMasterPasswordSet : Specified profile %s is not found", profileName);
return FPM_PROFILE_NOT_PRESENT;
}
extern "C" APIEXPORT int FPM_CheckMasterPassword(char *profileName, char *masterPassword)
{
// Find the profile...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
// check if its initialized
if( profileManager[i].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_DEBUG, "\n CheckMasterPassword : invoking CheckMasterPassword for profile %s", profileName);
return profileManager[i].CheckMasterPassword(masterPassword, 1);
}
else
{
PrintMessage(MESG_ERROR, "\n CheckMasterPassword : Specified profile %s is not initialized ", profileName);
return FPM_PROFILE_NOT_INITIALIZED;
}
}
}
}
PrintMessage(MESG_ERROR, "\n CheckMasterPassword : Specified profile %s is not found", profileName);
return FPM_PROFILE_NOT_PRESENT;
}
extern "C" APIEXPORT int FPM_GetSignonData(char *profileName,struct Host **host, int doRefresh)
{
// Find the profile...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
// check if its initialized
if( profileManager[i].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_DEBUG, "\n GetSignonData : invoking GetSignonData for profile %s", profileName);
return profileManager[i].GetSignonData(host, doRefresh);
}
else
{
PrintMessage(MESG_ERROR, "\n GetSignonData : Specified profile %s is not initialized", profileName);
return FPM_PROFILE_NOT_INITIALIZED;
}
}
}
}
PrintMessage(MESG_ERROR, "\n GetSignonData : Specified profile %s is not found", profileName);
return FPM_PROFILE_NOT_PRESENT;
}
extern "C" APIEXPORT int FPM_WriteSignonData(char *profileName)
{
// Find the profile...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
// check if its initialized
if( profileManager[i].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_DEBUG, "\n WriteSignonData : invoking WriteSignonData for profile %s", profileName);
return profileManager[i].WriteSignonData();
}
else
{
PrintMessage(MESG_ERROR, "\n WriteSignonData : Specified profile %s is not initialized", profileName);
return FPM_PROFILE_NOT_INITIALIZED;
}
}
}
}
PrintMessage(MESG_ERROR, "\n WriteSignonData : Specified profile %s is not found", profileName);
return FPM_PROFILE_NOT_PRESENT;
}
extern "C" APIEXPORT int FPM_AddHost(char *profileName, struct Host *host, int doUpdate)
{
// Find the profile...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
// check if its initialized
if( profileManager[i].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_DEBUG, "\n AddHost : invoking AddHost for profile %s", profileName);
return profileManager[i].AddHost(host, doUpdate);
}
else
{
PrintMessage(MESG_ERROR, "\n AddHost : Specified profile %s is not initialized", profileName);
return FPM_PROFILE_NOT_INITIALIZED;
}
}
}
}
PrintMessage(MESG_ERROR, "\n AddHost : Specified profile %s is not found", profileName);
return FPM_PROFILE_NOT_PRESENT;
}
extern "C" APIEXPORT int FPM_ModifyHost(char *profileName, struct Host *host, int doUpdate)
{
// Find the profile...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
// check if its initialized
if( profileManager[i].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_DEBUG, "\n ModifyHost : invoking ModifyHost for profile %s", profileName);
return profileManager[i].ModifyHost(host, doUpdate);
}
else
{
PrintMessage(MESG_ERROR, "\n ModifyHost : Specified profile %s is not initialized", profileName);
return FPM_PROFILE_NOT_INITIALIZED;
}
}
}
}
PrintMessage(MESG_ERROR, "\n ModifyHost : Specified profile %s is not found", profileName);
return FPM_PROFILE_NOT_PRESENT;
}
extern "C" APIEXPORT int FPM_RemoveHost(char *profileName, char *hostName, int doUpdate)
{
// Find the profile...
for(int i=0; i< profileCount; i++)
{
if( profileManager[i].profileName != NULL )
{
if( STRCMPI(profileManager[i].profileName, profileName) == 0 )
{
// check if its initialized
if( profileManager[i].isInitialized == FPM_TRUE )
{
PrintMessage(MESG_DEBUG, "\n RemoveHost : invoking RemoveHost for profile %s", profileName);
return profileManager[i].RemoveHost(hostName, doUpdate);
}
else
{
PrintMessage(MESG_ERROR, "\n RemoveHost : Specified profile %s is not initialized", profileName);
return FPM_PROFILE_NOT_INITIALIZED;
}
}
}
}
PrintMessage(MESG_ERROR, "\n RemoveHost : Specified profile %s is not found", profileName);
return FPM_PROFILE_NOT_PRESENT;
}
/*
int main(int argc, char* argv[])
{
char **profileList;
int *profileFlag;
int profCount;
profCount = FPM_GetProfileList(&profileList, &profileFlag);
if( profCount > 0)
{
printf("\n Profile names are as follows...");
for(int i=0; i< profCount; i++)
{
printf("\n %d => [%s] [%d]", i+1, profileList[i], profileFlag[i]);
// Load the default profile...
if( profileFlag[i] == 1 )
{
ProfileManager *pm = new ProfileManager();
printf("\n ***************************************");
if( pm->ProfileInit(profileList[i]) == 1)
{
pm->CheckMasterPassword("test123", 1);
struct Host *hostInfo;
pm->GetSignonData(&hostInfo, 1);
// Print all the data
PrintMessage(MESG_PRINT, "\n\n List of hosts ");
for(Host *t=hostInfo; t ; t=t->next)
{
PrintMessage(MESG_PRINT, "\n\n %s", t->hostName);
for(HostElement *h=t->child; h ; h= h->next)
{
PrintMessage(MESG_PRINT, "\n %s : %s ", h->name, h->value);
}
}
pm->WriteSignonData();
pm->ProfileExit();
}
}
printf("\n ***************************************");
}
}
return 0;
}
*/