Ejemplo n.º 1
0
// Serialize memory config
// Returns the number of bytes written to the destination on success or 0 for failure.
long GProfile::WriteCurrentConfigHelper(const char *pzPathAndFileName, GString *pDest, const char *pzSection/*=0*/, bool bWriteXML/*=0*/)
{
	GString strLocalDest;
	GString *strConfigData = (pDest) ? pDest : &strLocalDest;

	try
	{
		if (bWriteXML)
		{
		    *strConfigData << "<configuration>";
		}

		GListIterator itSections(&m_lstSections);
		while ( itSections() )
		{
			GProfileSection *pSection = (GProfileSection *)itSections++;
			if (pzSection)
			{
				if( pSection->m_strName.CompareNoCase(pzSection) != 0)
				{
					continue;
				}
			}
			if (bWriteXML)
			{
				pSection->ToXML(strConfigData, 1);
			}
			else
			{
				(*strConfigData) << "[" << pSection->m_strName << "]\r\n";

				GListIterator itNVP(&pSection->m_lstNVP);
				while (itNVP())
				{
					GProfileEntry *pNVP = (GProfileEntry *)itNVP++;
					(*strConfigData) << pNVP->m_strName << "=" << pNVP->m_strValue << "\r\n";
				}
				if (itSections())
					(*strConfigData) << "\r\n\r\n";
			}
		}
		if (bWriteXML)
		{
		    *strConfigData << "\r\n</configuration>";
		}

		if (pzPathAndFileName)
			strConfigData->ToFile(pzPathAndFileName);

		return (long)strConfigData->Length();
	}
	catch(GException &)
	{
		// most likely due to invalid path or file name.
		return 0;	
	}
	return 0; // can't get here
}
Ejemplo n.º 2
0
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstExePrev, 
   LPSTR pszCmdLine, int nCmdShow) {


	// ***** To debug this application  ****
	// set fDebug to 1, then recompile (in debug mode)
	// and run the program in a debugger like any windows .exe
	// The exact same code executes as if the user clicked the 
	// 'start' button in the NT services manager.
	int fDebug = 0;


   int nArgc = __argc;
#ifdef UNICODE
   LPCTSTR *ppArgv = (LPCTSTR*) CommandLineToArgvW(GetCommandLine(), &nArgc);
#else
   LPCTSTR *ppArgv = (LPCTSTR*) __argv;
#endif

   BOOL fStartService = (nArgc < 2);
   int i;

   int bInstall = 0;
   int bRemove  = 0;
   int bChange  = 0;
   GString  strPort("10888");

   GString strBoot;
   GString strRoot("NotUsed");
   for (i = 1; i < nArgc; i++) 
   {
      if ((ppArgv[i][0] == __TEXT('-')) || (ppArgv[i][0] == __TEXT('/'))) 
	  {
         // Command line switch
         if (lstrcmpi(&ppArgv[i][1], __TEXT("install")) == 0) 
            bInstall = 1;

         if (lstrcmpi(&ppArgv[i][1], __TEXT("remove"))  == 0)
            bRemove = 1;

         if (lstrcmpi(&ppArgv[i][1], __TEXT("change"))  == 0)
            bChange = 1;
		 

         GString strTemp(&ppArgv[i][1],strlen("desc:"));
		 if (strTemp.CompareNoCase("desc:") == 0)
		 {
            strServerDescription = &ppArgv[i][1+strlen("desc:")];
		 }

         GString strTemp2(&ppArgv[i][1],strlen("name:"));
		 if (strTemp2.CompareNoCase("name:") == 0)
		 {
            strServerName = &ppArgv[i][1+strlen("name:")];
		 }

         GString strTemp3(&ppArgv[i][1],strlen("pass:"******"pass:"******"pass:"******"boot:"));
		 if (strTemp4.CompareNoCase("boot:") == 0)
		 {
            strBoot = &ppArgv[i][1+strlen("boot:")];
		 }

         GString strTemp5(&ppArgv[i][1],strlen("port:"));
		 if (strTemp5.CompareNoCase("port:") == 0)
		 {
            strPort = &ppArgv[i][1+strlen("port:")];
		 }

         GString strTemp6(&ppArgv[i][1],strlen("root:"));
		 if (strTemp6.CompareNoCase("root:") == 0)
		 {
            strRoot = &ppArgv[i][1+strlen("root:")];
		 }
      }
   }

	if (bChange)
	{
		ModifyStartupFile(strBoot,strServerPassword,strRoot,strPort);
	}

	if (bInstall)
	{
		GString strThisEXEName(GetThisEXEName());


		// uuencode strBoot 
		BUFFER b;
		BufferInit(&b);
		uuencode((unsigned char *)(const char *)strBoot, (int)strBoot.Length(), &b);
		GString strEncodedBoot((char *)b.pBuf, b.cLen);
		BufferTerminate(&b);

		// open the registry, save the coded boot key, close the registry
		HKEY hk;
		if (RegCreateKey(HKEY_CLASSES_ROOT,(const char *)strThisEXEName,&hk) == ERROR_SUCCESS)
		{
			RegSetValue(hk,NULL,REG_SZ,(const char *)strEncodedBoot,0);
			RegCloseKey(hk);
		}

		// use root of file system to store the startup file 
		// unless specified otherwise by environment setting
		GString strOutFile("c:\\");
		strOutFile += strThisEXEName;
		if (getenv(strThisEXEName))
		{
			strOutFile += getenv(strThisEXEName);
		}
		
		
		// create the startup file
		GString strTempFile;
		strTempFile << strServerPassword << "&&" << strRoot << "&&" << strPort;
		strTempFile.ToFile("tempfile");
		GString strErrorOut;
		FileEncrypt(strBoot, "tempfile", strOutFile, strErrorOut);
		unlink("tempfile");


		InstallService();
	}
	if (bRemove)
	{
		RemoveService();

		GString strThisEXEName(GetThisEXEName());

		// remove the registry entry
		RegDeleteKey(HKEY_CLASSES_ROOT,(const char *)strThisEXEName);

		// remove the startup file
		GString strOutFile("c:\\");
		strOutFile += strThisEXEName;
		if (getenv(strThisEXEName))
		{
			strOutFile += getenv(strThisEXEName);
		}
		unlink(strOutFile);

	}

   strcpy(pzServer,strServerName);


   if (fDebug) 
   {
      // Running as EXE not as service, just run the service for debugging
	  TimeServiceMain(0, NULL);
   }

   if (fStartService) {
      SERVICE_TABLE_ENTRY ServiceTable[] = {
         { pzServer, TimeServiceMain },
         { NULL,        NULL }   // End of list
      };
      StartServiceCtrlDispatcher(ServiceTable);
   }

   return(0);
}
Ejemplo n.º 3
0
void ModifyStartupFile(GString &strStartupKey,GString &strServerPassword,GString &strRoot,GString &strPort)
{
	///////////////////////////////////////////////////////////////////
	// load the following values fron the startup file:
	//////////////////////////////////////////////////////////////////
	GString strCurrentPassword;
	GString strCurrentRoot;
	GString strCurrentPort;
	
	GString strThisEXEName(GetThisEXEName());
	GString strStartupData;
	if (strStartupKey.Length())
	{
		// look for a file in the root of the file system (c:\)
		// with the same name as this .exe
		GString strFile("c:\\");
		strFile += strThisEXEName;


		// load the crypted disk file into clear text memory
		char *pDest;
		int nDestLen;
		GString strErrorOut;
		if (FileDecryptToMemory(strStartupKey, strFile, &pDest, &nDestLen, strErrorOut))
		{
			// parse into the profile data structures
			pDest[7 + nDestLen] = 0; // null terminate it
			strStartupData.write(&pDest[7], nDestLen + 1); // and cap the GString 
		}
		else
		{
			// if the file was not in the root of the file system
			// see if there is an environment setting directing
			// this server to look for the file in another location
			// The variable name is dynamic, matching this .exe name
			// and the environment variable value must be a fully
			// qualified path and file name to the startup file.
			if (getenv(strThisEXEName))
			{
				if (FileDecryptToMemory(strStartupKey, getenv(strThisEXEName), &pDest, &nDestLen, strErrorOut))
				{
					// parse into the profile data structures
					strStartupData.write(&pDest[7], nDestLen-7);
				}
			}
		}

		// parse stored settings in startup file to startup variables
		if (strStartupData.Length())
		{
			GStringList lstOptions("&&",strStartupData);
			GStringIterator it(&lstOptions);
			
			if (it()) strCurrentPassword = it++;
			if (it()) strCurrentRoot = it++;
			if (it()) strCurrentPort = it++;
		}
	}
	//////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////


	// new values over write the old ones
	if (strServerPassword.Length())
		strCurrentPassword = strServerPassword;

	if (strPort.Length())
		strCurrentPort = strPort;

	if (strRoot.Length() && strRoot.CompareNoCase("none") != 0)
		strCurrentRoot = strRoot;
	else if (strRoot.CompareNoCase("none") == 0)
	{
		strCurrentRoot = "";
	}
	


	// use root of file system to store the startup file 
	// unless specified otherwise by environment setting
	GString strOutFile("c:\\");
	strOutFile += strThisEXEName;
	if (getenv(strThisEXEName))
	{
		strOutFile += getenv(strThisEXEName);
	}
	
	
	// create the new startup file
	GString strTempFile;
	strTempFile << strServerPassword << "&&" << strRoot << "&&" << strPort;
	strTempFile.ToFile("tempfile");
	GString strErrorOut;
	unlink(strOutFile);
	FileEncrypt(strStartupKey, "tempfile", strOutFile, strErrorOut);
	unlink("tempfile");
}