From 9534ee9695a31dfa4f92f15b3a5ef2d33f524cfc Mon Sep 17 00:00:00 2001 From: Noriko Hosoi Date: Thu, 9 May 2013 17:46:59 -0700 Subject: [PATCH] Ticket #47353 - PassSync fails to open changelog Bug description: The result of opening the changelog file "passhook.dat" was checked by errno (_get_errno(&errno)), which was not updated once an error (ENOENT) was set although the other error returned from GetLastError. Fix description: This patch replaces _get_errno with GetLastError to get the correct error. In addition, debug logging code is added. https://fedorahosted.org/389/ticket/47353 --- passwordsync/passhand.cpp | 53 ++++++++++++++++++++++++++++++-- passwordsync/passsync/ntservice.cpp | 6 ++-- passwordsync/passsync/service.cpp | 61 +++++++++++++++++++++++++++++++++++-- 3 files changed, 111 insertions(+), 9 deletions(-) diff --git a/passwordsync/passhand.cpp b/passwordsync/passhand.cpp index cfab159..f9b39bd 100644 --- a/passwordsync/passhand.cpp +++ b/passwordsync/passhand.cpp @@ -69,6 +69,12 @@ int saveSet(PASS_INFO_LIST* passInfoList, char* filename) int cipherTextLen; int resultTextLen = 0; int pairCount = passInfoList->size(); + fstream outLog; + +#if 0 + // Debug log + outLog.open("passhand.log", ios::out | ios::app); +#endif // Write usernames and passwords to a strstream plainTextStream.write((char*)&pairCount, sizeof(pairCount)); @@ -101,6 +107,11 @@ int saveSet(PASS_INFO_LIST* passInfoList, char* filename) goto exit; } + if(outLog.is_open()) { + timeStamp(&outLog); + outLog << "==> saveSet(" << filename << ")" << endl; + } + // Write cipher text to file outFile.open(filename, ios::out | ios::binary); if(!outFile.is_open()) @@ -110,11 +121,18 @@ int saveSet(PASS_INFO_LIST* passInfoList, char* filename) } outFile.write(cipherTextBuf, resultTextLen); outFile.close(); + if(outLog.is_open()) { + timeStamp(&outLog); + outLog << "<== saveSet(" << filename << ")" << endl; + } exit: // We need to unfreeze plainTextStream so memory gets freed by the destructor plainTextStream.rdbuf()->freeze(false); free(cipherTextBuf); + if(outLog.is_open()) { + outLog.close(); + } return result; } @@ -133,12 +151,26 @@ int loadSet(PASS_INFO_LIST* passInfoList, char* filename) int cipherTextLen = 0; int resultTextLen = 0; int pairCount = 0; - errno_t err = 0; + DWORD err = 0; + fstream outLog; + +#if 0 + // Debug log + outLog.open("passhand.log", ios::out | ios::app); +#endif + if(outLog.is_open()) { + timeStamp(&outLog); + outLog << "==> loadSet(" << filename << ")" << endl; + } // Read in cipher text from file inFile.open(filename, ios::in | ios::binary); - _get_errno(&err); - if (err == ENOENT) { // file does not exist - ok + err = GetLastError(); + if (err == ERROR_FILE_NOT_FOUND) { // file does not exist - ok + if(outLog.is_open()) { + timeStamp(&outLog); + outLog << "loadSet: Failed to open " << filename << ", Error: " << err << endl; + } result = 1; goto exit; } @@ -146,6 +178,10 @@ int loadSet(PASS_INFO_LIST* passInfoList, char* filename) if(!inFile.is_open()) { result = -1; + if(outLog.is_open()) { + timeStamp(&outLog); + outLog << "loadSet: File is not opened " << filename << endl; + } goto exit; } // Determine file size @@ -161,11 +197,19 @@ int loadSet(PASS_INFO_LIST* passInfoList, char* filename) if ((cipherTextBuf == NULL) || (plainTextBuf == NULL)) { result = -1; inFile.close(); + if(outLog.is_open()) { + timeStamp(&outLog); + outLog << "<== loadSet(" << filename << ")" << endl; + } goto exit; } inFile.read(cipherTextBuf, cipherTextLen); inFile.close(); + if(outLog.is_open()) { + timeStamp(&outLog); + outLog << "<== loadSet(" << filename << ")" << endl; + } if(decrypt(cipherTextBuf, cipherTextLen, plainTextBuf, plainTextLen, &resultTextLen) != 0) { @@ -208,6 +252,9 @@ int loadSet(PASS_INFO_LIST* passInfoList, char* filename) delete plainTextStream; exit: + if(outLog.is_open()) { + outLog.close(); + } free(cipherTextBuf); free(plainTextBuf); return result; diff --git a/passwordsync/passsync/ntservice.cpp b/passwordsync/passsync/ntservice.cpp index 05b5acb..bdacfb6 100644 --- a/passwordsync/passsync/ntservice.cpp +++ b/passwordsync/passsync/ntservice.cpp @@ -345,10 +345,10 @@ BOOL CNTService::StartService() }; DebugMsg(_T("Calling StartServiceCtrlDispatcher()")); - // Fails if started from command line, but StartService - // works any way + // Fails if started from command line, but StartService + // works any way BOOL b = ::StartServiceCtrlDispatcher(st); - DWORD err = GetLastError(); + DWORD err = GetLastError(); DebugMsg(_T("Returned from StartServiceCtrlDispatcher()")); return b; } diff --git a/passwordsync/passsync/service.cpp b/passwordsync/passsync/service.cpp index 26fbcee..e5cedbc 100644 --- a/passwordsync/passsync/service.cpp +++ b/passwordsync/passsync/service.cpp @@ -40,6 +40,7 @@ #include #include +#include #include "syncserv.h" #include "dssynchmsg.h" // syncserv.h @@ -113,10 +114,15 @@ static void usage() #define OPT_START_DIRECT 4 ///////////////////////////////////////////////////////////////// -static int checkOptions( PassSyncService *pSynch, int& argc, char *argv[] ) +static int checkOptions( PassSyncService *pSynch, int& argc, char *argv[], fstream *outLog) { int result = OPT_START; // Default is to start the service + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "==> checkOptions(" << pSynch->m_szServiceName << ")" << endl; + } + // Check first for uninstall, since we shouldn't do anything else if set int i; for( i = 1; i < argc; i++ ) @@ -139,6 +145,10 @@ static int checkOptions( PassSyncService *pSynch, int& argc, char *argv[] ) // Terminate after completion result = OPT_TERMINATE; argc = 1; + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "<== checkOption uninstall" << endl; + } return result; } } @@ -166,12 +176,20 @@ static int checkOptions( PassSyncService *pSynch, int& argc, char *argv[] ) { result = OPT_APP; bLocal = TRUE; + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "checkOptions: start as app (-a)" << endl; + } } // Start service else if ( 'x' == opt ) { result = OPT_START_DIRECT; bLocal = TRUE; + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "checkOptions: start service (-x)" << endl; + } } /* // Command port @@ -194,13 +212,25 @@ static int checkOptions( PassSyncService *pSynch, int& argc, char *argv[] ) printf( "%S is already installed\n", pSynch->m_szServiceName ); else { + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "checkOptions: Installing: " << pSynch->m_szServiceName << endl; + } // Try and install the copy that's running if ( pSynch->Install() ) { + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "checkOptions: Installed. " << endl; + } printf( "%S installed\n", pSynch->m_szServiceName ); } else { + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "checkOptions: Failed to install. Error %d" << GetLastError() << endl; + } printf( "%S failed to install. Error %d\n", pSynch->m_szServiceName, GetLastError() ); } @@ -211,18 +241,30 @@ static int checkOptions( PassSyncService *pSynch, int& argc, char *argv[] ) // Terminate after completion else if ( 'n' == opt ) { + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "checkOptions: Synchronize from NT to DS (-n): " << pSynch->m_szServiceName << endl; + } result = OPT_NONE; } // Synchronize from DS to NT // Terminate after completion else if ( 's' == opt ) { + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "checkOptions: Synchronize from DS to NT (-s): " << pSynch->m_szServiceName << endl; + } result = OPT_NONE; } // Synchronize both ways // Terminate after completion else if ( 'r' == opt ) { + if(outLog->is_open()) { + timeStamp(outLog); + *outLog << "checkOptions: Synchronize both ways (-s): " << pSynch->m_szServiceName << endl; + } result = OPT_NONE; } if ( bLocal ) @@ -277,13 +319,24 @@ static int initialize( PassSyncService *pSynch, int argc, char *argv[] ) int main( int argc, char *argv[] ) { + fstream outLog; + +#if 0 + // Debug log + outLog.open("PassSyncService.log", ios::out | ios::app); +#endif + // Global single instance PassSyncService theSynch("Password Synchronization Service"); // Process special install/uninstall switches; this does install/uninstall // It returns non-zero to actually start the service - int nStart = checkOptions( &theSynch, argc, argv ); + int nStart = checkOptions( &theSynch, argc, argv, &outLog ); + if(outLog.is_open()) { + timeStamp(&outLog); + outLog << "checkOptions returned: " << nStart << endl; + } // Set up configuration if ( OPT_TERMINATE != nStart ) initialize( &theSynch, argc, argv ); @@ -323,7 +376,9 @@ main( int argc, char *argv[] ) } exit(theSynch.m_Status.dwWin32ExitCode); - + if(outLog.is_open()) { + outLog.close(); + } ////////// That's it ////// return 0; -- 1.7.11.7