void HttpRequest::FetchGravatarForEmail() { unsigned char ehash[MD5LEN]; { char email[1024]; DWORD d; size_t len; if (WideCharToMultiByte(CP_UTF8, 0, m_target.c_str(), -1, email, sizeof(email), NULL, NULL) == 0) { ReportStatus(KHERR_ERROR, L"Can't convert email address to UTF-8", L"%s", GetLastErrorString().c_str()); return; } _strlwr_s(email, sizeof(email)); if (FAILED(StringCbLengthA(email, sizeof(email), &len))) { ReportStatus(KHERR_ERROR, L"UTF-8 email address too long", L"The email address can't be longer than 1024 characters"); return; } d = sizeof(ehash); if (KHM_FAILED(hash_data((BYTE *) email, (DWORD)len, CALG_MD5, (BYTE *) ehash, &d))) { ReportStatus(KHERR_ERROR, L"Failed to hash email address", NULL); return; } } { wchar_t resource[60]; wchar_t * tail; size_t len; int i; static const wchar_t hexdigits[] = L"0123456789abcdef"; StringCbCopyEx(resource, sizeof(resource), L"/avatar/", &tail, &len, STRSAFE_NO_TRUNCATION); for (i = 0; i < sizeof(ehash); i++) { *tail++ = hexdigits[ehash[i] >> 4]; *tail++ = hexdigits[ehash[i] & 0xf]; len -= sizeof(wchar_t) * 2; } *tail++ = L'\0'; StringCbCat(resource, sizeof(resource), L".jpg?d=404&s=128"); FetchResource(L"www.gravatar.com", resource, jpg_mimetypes); } }
VOID LogEvent(WORD wEventType, DWORD dwEventID, ...) { va_list listArgs; HANDLE hEventSource; HANDLE hMutex = NULL; LPTSTR lpArgs[MAXARGS]; CHAR lpStrings[MAXARGS][STRLEN]; static CHAR lpLastStrings[MAXARGS][STRLEN]; WORD wNumArgs = 0; static WORD wLastNumArgs = MAXARGS; static time_t lastMessageTime = 0; static WORD wLastEventType = 0; static DWORD dwLastEventID = 0; time_t now; DWORD code; BOOL bLogMessage = TRUE; WORD i = 0, j; // Ensure that our event source is properly initialized. if (!AddEventSource()) return; // Get a handle to the event log. hEventSource = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME); if (hEventSource == NULL) return; // Construct the array of substitution strings. va_start(listArgs, dwEventID); switch ( dwEventID ) { case MSG_FLUSH_NO_SHARE_NAME: case MSG_FLUSH_NO_MEMORY: case MSG_FLUSH_IMPERSONATE_ERROR: case MSG_FLUSH_UNEXPECTED_EVENT: case MSG_UNHANDLED_EXCEPTION: case MSG_SMB_ZERO_TRANSACTION_COUNT: case MSG_SERVICE_INCORRECT_VERSIONS: case MSG_SERVICE_STOPPING: case MSG_SERVICE_STOPPED: case MSG_SERVICE_ERROR_STOP: case MSG_CRYPT_OFF: case MSG_CRYPT_ON: break; case MSG_SERVICE_START_PENDING: wNumArgs = 1; lpArgs[0] = AFSVersion; break; case MSG_SERVICE_RUNNING: wNumArgs = 1; if (smb_Enabled && RDR_Initialized) lpArgs[0] = "SMB and RDR interfaces"; else if (smb_Enabled) lpArgs[0] = "SMB interface"; else if (RDR_Initialized) lpArgs[0] = "RDR interface"; else lpArgs[0] = "No active interface"; break; case MSG_FLUSH_BAD_SHARE_NAME: case MSG_FLUSH_OPEN_ENUM_ERROR: case MSG_FLUSH_ENUM_ERROR: case MSG_FLUSH_FAILED: case MSG_RX_HARD_DEAD_TIME_EXCEEDED: case MSG_SERVICE_ERROR_STOP_WITH_MSG: case MSG_SMB_SEND_PACKET_FAILURE: case MSG_UNEXPECTED_SMB_SESSION_CLOSE: case MSG_RX_MSGSIZE_EXCEEDED: case MSG_RX_BUSY_CALL_CHANNEL: wNumArgs = 1; lpArgs[0] = va_arg(listArgs, LPTSTR); break; case MSG_TIME_FLUSH_PER_VOLUME: case MSG_TIME_FLUSH_TOTAL: case MSG_SMB_MAX_MPX_COUNT: case MSG_SMB_MAX_BUFFER_SIZE: wNumArgs = 2; lpArgs[0] = va_arg(listArgs, LPTSTR); lpArgs[1] = va_arg(listArgs, LPTSTR); break; case MSG_SERVER_REPORTS_VNOVOL: case MSG_SERVER_REPORTS_VMOVED: case MSG_SERVER_REPORTS_VOFFLINE: case MSG_SERVER_REPORTS_VSALVAGE: case MSG_SERVER_REPORTS_VNOSERVICE: case MSG_SERVER_REPORTS_VIO: case MSG_SERVER_REPORTS_VBUSY: case MSG_SERVER_REPORTS_VRESTARTING: wNumArgs = 3; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,afs_int32)); lpArgs[1] = lpStrings[1]; lpArgs[2] = va_arg(listArgs, LPTSTR); break; case MSG_ALL_SERVERS_BUSY: case MSG_ALL_SERVERS_OFFLINE: case MSG_ALL_SERVERS_DOWN: wNumArgs = 2; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,afs_int32)); lpArgs[1] = lpStrings[1]; break; case MSG_BAD_SMB_PARAM: wNumArgs = 5; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[4],STRLEN,"%d",va_arg(listArgs,WORD)); lpArgs[1] = lpStrings[1]; lpArgs[2] = lpStrings[2]; lpArgs[3] = lpStrings[3]; lpArgs[4] = lpStrings[4]; break; case MSG_BAD_SMB_PARAM_WITH_OFFSET: wNumArgs = 6; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[4],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[5],STRLEN,"%d",va_arg(listArgs,WORD)); lpArgs[1] = lpStrings[1]; lpArgs[2] = lpStrings[2]; lpArgs[3] = lpStrings[3]; lpArgs[4] = lpStrings[4]; lpArgs[5] = lpStrings[5]; break; case MSG_BAD_SMB_TOO_SHORT: case MSG_BAD_SMB_INVALID: case MSG_BAD_SMB_INCOMPLETE: wNumArgs = 1; StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,WORD)); lpArgs[0] = lpStrings[0]; break; case MSG_SMB_SESSION_START: wNumArgs = 1; StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,long)); lpArgs[0] = lpStrings[0]; break; case MSG_BAD_SMB_WRONG_SESSION: wNumArgs = 2; StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,DWORD)); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,WORD)); lpArgs[0] = lpStrings[0]; lpArgs[1] = lpStrings[1]; break; case MSG_BAD_VCP: wNumArgs = 4; StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,UCHAR)); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,UCHAR)); StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,UCHAR)); StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,UCHAR)); lpArgs[0] = lpStrings[0]; lpArgs[1] = lpStrings[1]; lpArgs[2] = lpStrings[2]; lpArgs[3] = lpStrings[3]; break; case MSG_SERVICE_ERROR_STOP_WITH_MSG_AND_LOCATION: wNumArgs = 3; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int)); lpArgs[1] = lpStrings[1]; lpArgs[2] = va_arg(listArgs,LPTSTR); break; case MSG_DIRTY_BUFFER_AT_SHUTDOWN: wNumArgs = 6; lpArgs[0] = va_arg(listArgs, LPTSTR); lpArgs[1] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[2],STRLEN,"%u",va_arg(listArgs,int)); StringCbPrintf(lpStrings[3],STRLEN,"%u",va_arg(listArgs,int)); StringCbPrintf(lpStrings[4],STRLEN,"%I64u",va_arg(listArgs,afs_int64)); StringCbPrintf(lpStrings[5],STRLEN,"%I64u",va_arg(listArgs,afs_int64)); lpArgs[2] = lpStrings[2]; lpArgs[3] = lpStrings[3]; lpArgs[4] = lpStrings[4]; lpArgs[5] = lpStrings[5]; break; } va_end(listArgs); // Make sure we were not given too many args. if (wNumArgs >= MAXARGS) goto done; hMutex = CreateMutex( NULL, TRUE, "AFSD Event Log Mutex"); if (hMutex == NULL) goto done; if (GetLastError() == ERROR_ALREADY_EXISTS) { code = WaitForSingleObject( hMutex, 500); if (code != WAIT_OBJECT_0) goto done; } /* * We rate limit consecutive duplicate messages to one every * five seconds. */ now = time(NULL); if (now < lastMessageTime + 5 && wEventType == wLastEventType && dwEventID == dwLastEventID && wNumArgs == wLastNumArgs) { for (i=0; i<wNumArgs; i++) { if ( strncmp(lpArgs[i], lpLastStrings[i], STRLEN)) break; } if (i == wNumArgs) bLogMessage = FALSE; } if ( bLogMessage) { wLastNumArgs = wNumArgs; wLastEventType = wEventType; dwLastEventID = dwEventID; lastMessageTime = now; for ( j = (i == wNumArgs ? 0 : i) ; i < wNumArgs; i++) { StringCbCopyEx( lpLastStrings[i], STRLEN, lpArgs[i], NULL, NULL, STRSAFE_NULL_ON_FAILURE); } } ReleaseMutex(hMutex); // Log the event. if ( bLogMessage) code = ReportEvent(hEventSource, // handle of event source wEventType, // event type 0, // event category dwEventID, // event ID NULL, // current user's SID wNumArgs, // strings in lpszArgs 0, // no bytes of raw data wNumArgs ? lpArgs : NULL,// array of error strings NULL); // no raw data done: if (hMutex) CloseHandle(hMutex); DeregisterEventSource(hEventSource); }
HRESULT Uninstall( ) { HRESULT hr = S_OK; TCHAR szCurrentDllPath[MAX_PATH << 1]; TCHAR szTemp[MAX_PATH << 1]; LPTSTR lpszFileToDelete = szCurrentDllPath; LPTSTR lpszTempAppend = szTemp + GetModuleFileName(g_hModThisDll, szTemp, countof(szTemp)); StringCbCopy(szCurrentDllPath, sizeof(szCurrentDllPath), szTemp); #ifdef _WIN64 // If this 64-bit dll was installed to the default location, // uninstall the 32-bit dll if it exists in its default location TCHAR lpszDefInstallPath[MAX_PATH + 0x20]; UINT uSize = GetSystemDirectory(lpszDefInstallPath, MAX_PATH); if (uSize && uSize < MAX_PATH) { LPTSTR lpszPathAppend = lpszDefInstallPath + uSize; if (*(lpszPathAppend - 1) != TEXT('\\')) *lpszPathAppend++ = TEXT('\\'); static const TCHAR szFolderAndFilename[] = TEXT("ShellExt") TEXT("\\") TEXT(HASHCHECK_FILENAME_STR); SSStaticCpy(lpszPathAppend, szFolderAndFilename); // If this 64-bit dll was installed to the default location if (StrCmpI(szCurrentDllPath, lpszDefInstallPath) == 0) { TCHAR lpszSystemWow64[MAX_PATH + 0x20]; uSize = GetSystemWow64Directory(lpszSystemWow64, MAX_PATH); if (uSize && uSize < MAX_PATH) { LPTSTR lpszSystemWow64Append = lpszSystemWow64 + uSize; if (*(lpszSystemWow64Append - 1) != TEXT('\\')) SSCpy2Ch(lpszSystemWow64Append++, TEXT('\\'), 0); StringCbCopyEx(lpszDefInstallPath, sizeof(lpszDefInstallPath), lpszSystemWow64, &lpszPathAppend, NULL, 0); SSStaticCpy(lpszPathAppend, szFolderAndFilename); // If the 32-bit dll exists in its default location if (PathFileExists(lpszDefInstallPath)) { static const TCHAR szRegsvr32[] = TEXT("regsvr32.exe"); SSStaticCpy(lpszSystemWow64Append, szRegsvr32); // the lpszSystemWow64 buffer now contains the full regsvr32.exe path TCHAR lpszCommandLine[MAX_PATH + 0x20]; LPTSTR lpszCommandLineAppend; static const TCHAR szCommandOpts[] = TEXT("regsvr32.exe /u /i /n /s "); lpszCommandLineAppend = SSStaticCpy(lpszCommandLine, szCommandOpts) - 1; StringCbCopy(lpszCommandLineAppend, sizeof(lpszCommandLine)-sizeof(szCommandOpts), lpszDefInstallPath); STARTUPINFO si; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); PROCESS_INFORMATION pi; memset(&pi, 0, sizeof(pi)); if (!CreateProcess(lpszSystemWow64, lpszCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return E_FAIL; DWORD dwExit; WaitForSingleObject(pi.hProcess, INFINITE); GetExitCodeProcess(pi.hProcess, &dwExit); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); if (dwExit != 0) return E_FAIL; } } } } #endif // Rename the DLL prior to scheduling it for deletion *lpszTempAppend++ = TEXT('.'); SSCpy2Ch(lpszTempAppend, 0, 0); for (TCHAR ch = TEXT('0'); ch <= TEXT('9'); ++ch) { *lpszTempAppend = ch; if (MoveFileEx(szCurrentDllPath, szTemp, MOVEFILE_REPLACE_EXISTING)) { lpszFileToDelete = szTemp; break; } } // Schedule the DLL to be deleted at shutdown/reboot if (!MoveFileEx(lpszFileToDelete, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)) hr = E_FAIL; // Unregister if (DllUnregisterServer() != S_OK) hr = E_FAIL; // Disassociate file extensions; see the comment in DllUnregisterServer for // why this step is skipped for Wow64 processes if (!Wow64CheckProcess()) { for (UINT i = 0; i < countof(g_szHashExtsTab); ++i) { HKEY hKey; if (hKey = RegOpen(HKEY_CLASSES_ROOT, g_szHashExtsTab[i], NULL, FALSE)) { RegGetSZ(hKey, NULL, szTemp, sizeof(szTemp)); if (_tcscmp(szTemp, PROGID_STR_HashCheck) == 0) RegDeleteValue(hKey, NULL); RegCloseKey(hKey); } } } // We don't need the uninstall strings any more... RegDelete(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s"), CLSNAME_STR_HashCheck); return(hr); }