diff --git a/plugins/NetworkTools/CHANGELOG.txt b/plugins/NetworkTools/CHANGELOG.txt deleted file mode 100644 index ce778d88a..000000000 --- a/plugins/NetworkTools/CHANGELOG.txt +++ /dev/null @@ -1,33 +0,0 @@ -1.8 - * Added custom tracert and whois support - * Added geoip lookup for country name and image - * Added Tools menu > Network Tools > options - -1.7 - * Added option to specify ping buffer length - * Removed legacy code - -1.6 - * Added PathPing support - -1.5 - * Improved ping graph scaling - -1.4 - * Fixed whois scrolling issue - * Fixed ping threading issues - * Fixed various memory leaks - -1.3 - * Added plugin options for ping/tracert/whois - * Added native ping/tracert/whois support - * Updated UI - -1.2 - * Fixed encoding bug - -1.1 - * Fixed handle leak - -1.0 - * Initial release diff --git a/plugins/NetworkTools/NetworkTools.rc b/plugins/NetworkTools/NetworkTools.rc index 4f1982545..b7a56d6a2 100644 --- a/plugins/NetworkTools/NetworkTools.rc +++ b/plugins/NetworkTools/NetworkTools.rc @@ -57,7 +57,7 @@ BEGIN CONTROL "",IDC_NETOUTPUTEDIT,"RICHEDIT50W",WS_VSCROLL | WS_TABSTOP | 0x4,0,0,389,214 END -IDD_OPTIONS DIALOGEX 0, 0, 253, 129 +IDD_OPTIONS DIALOGEX 0, 0, 303, 122 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Options" FONT 8, "MS Shell Dlg", 400, 0, 0x1 @@ -68,6 +68,11 @@ BEGIN LTEXT "Max Tracert Hops:",IDC_STATIC,105,25,60,8 CONTROL "Enable extended TCP statistics",IDC_ENABLE_EXTENDED_TCP, "Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,7,115,10 + EDITTEXT IDC_KEYTEXT,7,76,184,14,ES_AUTOHSCROLL | ES_READONLY | WS_DISABLED + LTEXT "MaxMind GeoLite license key:",IDC_STATIC,7,66,94,8 + PUSHBUTTON "Download",IDRETRY,246,77,50,14 + PUSHBUTTON "Change",IDC_REFRESH,193,77,50,14 + LTEXT "Note: You can also download GeoLite updates from the Main menu > Tools > Network Tools > Update GeoLite...",IDC_STATIC,7,94,192,19 END IDD_PING DIALOGEX 0, 0, 329, 151 @@ -100,6 +105,19 @@ BEGIN PUSHBUTTON "Refresh",IDC_REFRESH,279,191,50,14 END +IDD_OPTIONSKEY DIALOGEX 0, 0, 309, 68 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Update MaxMind License Key" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + PUSHBUTTON "Save",IDYES,198,47,50,14 + DEFPUSHBUTTON "Cancel",IDCANCEL,252,47,50,14 + EDITTEXT IDC_KEYTEXT,7,24,295,14,ES_AUTOHSCROLL + LTEXT "Paste the license key here: ",IDC_STATIC,7,7,90,8 + CONTROL "Checkout How-to-generate-a-license-key",IDC_HELPLINK, + "SysLink",WS_TABSTOP,7,47,136,14 +END + ///////////////////////////////////////////////////////////////////////////// // @@ -616,9 +634,9 @@ BEGIN IDD_OPTIONS, DIALOG BEGIN LEFTMARGIN, 7 - RIGHTMARGIN, 246 + RIGHTMARGIN, 296 TOPMARGIN, 7 - BOTTOMMARGIN, 122 + BOTTOMMARGIN, 115 END IDD_PING, DIALOG @@ -636,6 +654,14 @@ BEGIN TOPMARGIN, 7 BOTTOMMARGIN, 205 END + + IDD_OPTIONSKEY, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 302 + TOPMARGIN, 7 + BOTTOMMARGIN, 61 + END END #endif // APSTUDIO_INVOKED diff --git a/plugins/NetworkTools/NetworkTools.vcxproj b/plugins/NetworkTools/NetworkTools.vcxproj index 492ce1ecf..9e9fc0882 100644 --- a/plugins/NetworkTools/NetworkTools.vcxproj +++ b/plugins/NetworkTools/NetworkTools.vcxproj @@ -145,9 +145,6 @@ - - - diff --git a/plugins/NetworkTools/NetworkTools.vcxproj.filters b/plugins/NetworkTools/NetworkTools.vcxproj.filters index 171fc252e..6469b0518 100644 --- a/plugins/NetworkTools/NetworkTools.vcxproj.filters +++ b/plugins/NetworkTools/NetworkTools.vcxproj.filters @@ -65,9 +65,6 @@ Resource Files - - - Resource Files\Images diff --git a/plugins/NetworkTools/main.c b/plugins/NetworkTools/main.c index e0749defa..ac08af31d 100644 --- a/plugins/NetworkTools/main.c +++ b/plugins/NetworkTools/main.c @@ -211,28 +211,22 @@ static BOOLEAN ParseNetworkAddress( } VOID NTAPI MenuItemCallback( - _In_opt_ PVOID Parameter, + _In_ PVOID Parameter, _In_opt_ PVOID Context ) { PPH_PLUGIN_MENU_ITEM menuItem = (PPH_PLUGIN_MENU_ITEM)Parameter; - PPH_NETWORK_ITEM networkItem; - - if (!menuItem) - return; - - networkItem = (PPH_NETWORK_ITEM)menuItem->Context; switch (menuItem->Id) { case NETWORK_ACTION_PING: - ShowPingWindow(networkItem); + ShowPingWindow((PPH_NETWORK_ITEM)menuItem->Context); break; case NETWORK_ACTION_TRACEROUTE: - ShowTracertWindow(networkItem); + ShowTracertWindow((PPH_NETWORK_ITEM)menuItem->Context); break; case NETWORK_ACTION_WHOIS: - ShowWhoisWindow(networkItem); + ShowWhoisWindow((PPH_NETWORK_ITEM)menuItem->Context); break; case MAINMENU_ACTION_PING: { @@ -313,26 +307,24 @@ VOID NTAPI MenuItemCallback( } break; case MAINMENU_ACTION_GEOIP_UPDATE: - ShowGeoIPUpdateDialog(); + ShowGeoLiteUpdateDialog(menuItem->OwnerWindow); break; } } VOID NTAPI MainMenuInitializingCallback( - _In_opt_ PVOID Parameter, + _In_ PVOID Parameter, _In_opt_ PVOID Context ) { PPH_PLUGIN_MENU_INFORMATION menuInfo = Parameter; PPH_EMENU_ITEM networkToolsMenu; - if (!menuInfo) - return; if (menuInfo->u.MainMenu.SubMenuIndex != PH_MENU_ITEM_LOCATION_TOOLS) return; networkToolsMenu = PhPluginCreateEMenuItem(PluginInstance, 0, 0, L"&Network Tools", NULL); - PhInsertEMenuItem(networkToolsMenu, PhPluginCreateEMenuItem(PluginInstance, 0, MAINMENU_ACTION_GEOIP_UPDATE, L"&GeoIP database update...", NULL), ULONG_MAX); + PhInsertEMenuItem(networkToolsMenu, PhPluginCreateEMenuItem(PluginInstance, 0, MAINMENU_ACTION_GEOIP_UPDATE, L"&GeoLite database update...", NULL), ULONG_MAX); PhInsertEMenuItem(networkToolsMenu, PhCreateEMenuSeparator(), ULONG_MAX); PhInsertEMenuItem(networkToolsMenu, PhPluginCreateEMenuItem(PluginInstance, 0, MAINMENU_ACTION_PING, L"&Ping address...", NULL), ULONG_MAX); PhInsertEMenuItem(networkToolsMenu, PhPluginCreateEMenuItem(PluginInstance, 0, MAINMENU_ACTION_TRACERT, L"&Traceroute address...", NULL), ULONG_MAX); @@ -341,7 +333,7 @@ VOID NTAPI MainMenuInitializingCallback( } VOID NTAPI NetworkMenuInitializingCallback( - _In_opt_ PVOID Parameter, + _In_ PVOID Parameter, _In_opt_ PVOID Context ) { @@ -351,9 +343,6 @@ VOID NTAPI NetworkMenuInitializingCallback( PPH_EMENU_ITEM traceMenu; PPH_EMENU_ITEM pingMenu; - if (!menuInfo) - return; - if (menuInfo->u.Network.NumberOfNetworkItems == 1) networkItem = menuInfo->u.Network.NetworkItems[0]; else @@ -445,16 +434,13 @@ LONG NTAPI NetworkServiceSortFunction( } VOID NTAPI NetworkTreeNewInitializingCallback( - _In_opt_ PVOID Parameter, + _In_ PVOID Parameter, _In_opt_ PVOID Context ) { PPH_PLUGIN_TREENEW_INFORMATION info = Parameter; PH_TREENEW_COLUMN column; - if (!info) - return; - if (Context) *(HWND*)Context = info->TreeNewHandle; @@ -1003,7 +989,8 @@ LOGICAL DllMain( { IntegerSettingType, SETTING_NAME_TRACERT_MAX_HOPS, L"14" }, { IntegerPairSettingType, SETTING_NAME_WHOIS_WINDOW_POSITION, L"0,0" }, { ScalableIntegerPairSettingType, SETTING_NAME_WHOIS_WINDOW_SIZE, L"@96|600,365" }, - { IntegerSettingType, SETTING_NAME_EXTENDED_TCP_STATS, L"0" } + { IntegerSettingType, SETTING_NAME_EXTENDED_TCP_STATS, L"0" }, + { StringSettingType, SETTING_NAME_GEOLITE_API_KEY, L"" }, }; PluginInstance = PhRegisterPlugin(PLUGIN_NAME, Instance, &info); diff --git a/plugins/NetworkTools/nettools.h b/plugins/NetworkTools/nettools.h index 1d1511704..1caff57d2 100644 --- a/plugins/NetworkTools/nettools.h +++ b/plugins/NetworkTools/nettools.h @@ -38,6 +38,7 @@ #define SETTING_NAME_WHOIS_WINDOW_POSITION (PLUGIN_NAME L".WhoisWindowPosition") #define SETTING_NAME_WHOIS_WINDOW_SIZE (PLUGIN_NAME L".WhoisWindowSize") #define SETTING_NAME_EXTENDED_TCP_STATS (PLUGIN_NAME L".EnableExtendedTcpStats") +#define SETTING_NAME_GEOLITE_API_KEY (PLUGIN_NAME L".MaxMindApiKey") extern PPH_PLUGIN PluginInstance; extern BOOLEAN GeoDbInitialized; @@ -295,20 +296,19 @@ typedef struct _PH_UPDATER_CONTEXT { HWND DialogHandle; WNDPROC DefaultWindowProc; - - PPH_STRING FileDownloadUrl; + ULONG ErrorCode; } PH_UPDATER_CONTEXT, *PPH_UPDATER_CONTEXT; -VOID TaskDialogLinkClicked( +BOOLEAN GeoLiteCheckUpdatePlatformSupported( + VOID + ); + +NTSTATUS GeoLiteUpdateThread( _In_ PPH_UPDATER_CONTEXT Context ); -NTSTATUS GeoIPUpdateThread( - _In_ PVOID Parameter - ); - -VOID ShowGeoIPUpdateDialog( - VOID +VOID ShowGeoLiteUpdateDialog( + _In_opt_ HWND ParentWindowHandle ); // pages.c diff --git a/plugins/NetworkTools/options.c b/plugins/NetworkTools/options.c index eb23d4e5d..ccfff44c2 100644 --- a/plugins/NetworkTools/options.c +++ b/plugins/NetworkTools/options.c @@ -12,6 +12,10 @@ #include "nettools.h" +VOID ShowGeoLiteConfigDialog( + _In_ HWND ParentWindowHandle + ); + INT_PTR CALLBACK OptionsDlgProc( _In_ HWND hwndDlg, _In_ UINT uMsg, @@ -28,6 +32,7 @@ INT_PTR CALLBACK OptionsDlgProc( PhSetDialogItemValue(hwndDlg, IDC_PINGPACKETLENGTH, PhGetIntegerSetting(SETTING_NAME_PING_SIZE), FALSE); PhSetDialogItemValue(hwndDlg, IDC_MAXHOPS, PhGetIntegerSetting(SETTING_NAME_TRACERT_MAX_HOPS), FALSE); Button_SetCheck(GetDlgItem(hwndDlg, IDC_ENABLE_EXTENDED_TCP), PhGetIntegerSetting(SETTING_NAME_EXTENDED_TCP_STATS) ? BST_CHECKED : BST_UNCHECKED); + PhSetDialogItemText(hwndDlg, IDC_KEYTEXT, PhaGetStringSetting(SETTING_NAME_GEOLITE_API_KEY)->Buffer); PhInitializeLayoutManager(&LayoutManager, hwndDlg); PhAddLayoutItem(&LayoutManager, GetDlgItem(hwndDlg, IDC_DATABASE), NULL, PH_ANCHOR_TOP | PH_ANCHOR_LEFT | PH_ANCHOR_RIGHT); @@ -48,6 +53,28 @@ INT_PTR CALLBACK OptionsDlgProc( PhLayoutManagerLayout(&LayoutManager); } break; + case WM_COMMAND: + { + switch (GET_WM_COMMAND_ID(wParam, lParam)) + { + case IDC_REFRESH: + { + if (!GeoLiteCheckUpdatePlatformSupported()) + break; + + ShowGeoLiteConfigDialog(hwndDlg); + + PhSetDialogItemText(hwndDlg, IDC_KEYTEXT, PhaGetStringSetting(SETTING_NAME_GEOLITE_API_KEY)->Buffer); + } + break; + case IDRETRY: + { + ShowGeoLiteUpdateDialog(hwndDlg); + } + break; + } + } + break; case WM_CTLCOLORBTN: return HANDLE_WM_CTLCOLORBTN(hwndDlg, wParam, lParam, PhWindowThemeControlColor); case WM_CTLCOLORDLG: @@ -58,3 +85,86 @@ INT_PTR CALLBACK OptionsDlgProc( return FALSE; } + +INT_PTR CALLBACK OptionsGeoLiteDlgProc( + _In_ HWND hwndDlg, + _In_ UINT uMsg, + _In_ WPARAM wParam, + _In_ LPARAM lParam + ) +{ + switch (uMsg) + { + case WM_INITDIALOG: + { + PhCenterWindow(hwndDlg, GetParent(hwndDlg)); + + PhSetDialogItemText(hwndDlg, IDC_KEYTEXT, PhaGetStringSetting(SETTING_NAME_GEOLITE_API_KEY)->Buffer); + + PhSetDialogFocus(hwndDlg, GetDlgItem(hwndDlg, IDCANCEL)); + } + break; + case WM_COMMAND: + { + switch (GET_WM_COMMAND_ID(wParam, lParam)) + { + case IDCANCEL: + EndDialog(hwndDlg, FALSE); + break; + case IDYES: + { + PPH_STRING string; + + string = PhGetWindowText(GetDlgItem(hwndDlg, IDC_KEYTEXT)); + PhSetStringSetting(SETTING_NAME_GEOLITE_API_KEY, PhGetStringOrEmpty(string)); + PhClearReference(&string); + + EndDialog(hwndDlg, FALSE); + } + break; + } + } + break; + case WM_NOTIFY: + { + LPNMHDR header = (LPNMHDR)lParam; + + switch (header->code) + { + case NM_CLICK: + case NM_RETURN: + { + PNMLINK link = (PNMLINK)lParam; + LITEM item = link->item; + + if (header->idFrom == IDC_HELPLINK && item.iLink == 0) + { + PhShellExecute(hwndDlg, item.szUrl, NULL); + } + } + break; + } + } + break; + case WM_CTLCOLORBTN: + return HANDLE_WM_CTLCOLORBTN(hwndDlg, wParam, lParam, PhWindowThemeControlColor); + case WM_CTLCOLORDLG: + return HANDLE_WM_CTLCOLORDLG(hwndDlg, wParam, lParam, PhWindowThemeControlColor); + case WM_CTLCOLORSTATIC: + return HANDLE_WM_CTLCOLORSTATIC(hwndDlg, wParam, lParam, PhWindowThemeControlColor); + } + + return FALSE; +} + +VOID ShowGeoLiteConfigDialog( + _In_ HWND ParentWindowHandle + ) +{ + DialogBox( + PluginInstance->DllBase, + MAKEINTRESOURCE(IDD_OPTIONSKEY), + ParentWindowHandle, + OptionsGeoLiteDlgProc + ); +} diff --git a/plugins/NetworkTools/pages.c b/plugins/NetworkTools/pages.c index a27c87c07..0cc74b768 100644 --- a/plugins/NetworkTools/pages.c +++ b/plugins/NetworkTools/pages.c @@ -5,7 +5,7 @@ * * Authors: * - * dmex 2016 + * dmex 2016-2022 * */ @@ -47,7 +47,7 @@ HRESULT CALLBACK CheckForUpdatesDbCallbackProc( break; case TDN_HYPERLINK_CLICKED: { - TaskDialogLinkClicked(context); + PhShellExecute(hwndDlg, L"https://www.maxmind.com", NULL); } break; } @@ -73,7 +73,7 @@ HRESULT CALLBACK CheckingForUpdatesDbCallbackProc( SendMessage(hwndDlg, TDM_SET_PROGRESS_BAR_MARQUEE, TRUE, 1); PhReferenceObject(context); - PhQueueItemWorkQueue(PhGetGlobalWorkQueue(), GeoIPUpdateThread, context); + PhQueueItemWorkQueue(PhGetGlobalWorkQueue(), GeoLiteUpdateThread, context); } break; } @@ -167,8 +167,8 @@ VOID ShowDbCheckForUpdatesDialog( config.pfCallback = CheckForUpdatesDbCallbackProc; config.lpCallbackData = (LONG_PTR)Context; - config.pszWindowTitle = L"Network Tools - GeoIP Updater"; - config.pszMainInstruction = L"Download the latest GeoIP database?"; + config.pszWindowTitle = L"Network Tools - GeoLite Updater"; + config.pszMainInstruction = L"Download the latest GeoLite database?"; config.pszContent = L"This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com\r\n\r\nSelect download to continue."; TaskDialogNavigatePage(Context->DialogHandle, &config); @@ -189,7 +189,7 @@ VOID ShowDbCheckingForUpdatesDialog( config.pfCallback = CheckingForUpdatesDbCallbackProc; config.lpCallbackData = (LONG_PTR)Context; - config.pszWindowTitle = L"Network Tools - GeoIP Updater"; + config.pszWindowTitle = L"Network Tools - GeoLite Updater"; config.pszMainInstruction = L"Downloading"; config.pszContent = L"Downloaded: ~ of ~ (~%%)\r\nSpeed: ~/s"; @@ -213,9 +213,9 @@ VOID ShowDbInstallRestartDialog( config.pButtons = RestartButtonArray; config.cButtons = ARRAYSIZE(RestartButtonArray); - config.pszWindowTitle = L"Network Tools - GeoIP Updater"; - config.pszMainInstruction = L"The GeoIP database has been installed"; - config.pszContent = L"You need to restart System Informer for the changes to take effect..."; + config.pszWindowTitle = L"Network Tools - GeoLite Updater"; + config.pszMainInstruction = L"The GeoLite database has been updated."; + config.pszContent = L"Please restart System Informer for the changes to take effect..."; TaskDialogNavigatePage(Context->DialogHandle, &config); } @@ -236,9 +236,32 @@ VOID ShowDbUpdateFailedDialog( config.pfCallback = FinalDbTaskDialogCallbackProc; config.lpCallbackData = (LONG_PTR)Context; - config.pszWindowTitle = L"Network Tools - GeoIP Updater"; - config.pszMainInstruction = L"Error downloading GeoIP database."; - config.pszContent = L"Click Retry to download the database again."; + config.pszWindowTitle = L"Network Tools - GeoLite Updater"; + config.pszMainInstruction = L"Error downloading GeoLite database."; + + if (Context->ErrorCode) + { + PPH_STRING errorMessage; + + if (Context->ErrorCode == ERROR_ACCESS_DENIED) + { + config.pszContent = PhaFormatString(L"[%lu] Access denied (invalid license key)", Context->ErrorCode)->Buffer; + } + else if (errorMessage = PhHttpSocketGetErrorMessage(Context->ErrorCode)) + { + config.pszContent = PhaFormatString(L"[%lu] %s", Context->ErrorCode, errorMessage->Buffer)->Buffer; + PhDereferenceObject(errorMessage); + } + else if (errorMessage = PhGetStatusMessage(0, Context->ErrorCode)) + { + config.pszContent = PhaFormatString(L"[%lu] %s", Context->ErrorCode, errorMessage->Buffer)->Buffer; + PhDereferenceObject(errorMessage); + } + } + else + { + config.pszContent = L"Click Retry to download the update again."; + } TaskDialogNavigatePage(Context->DialogHandle, &config); } diff --git a/plugins/NetworkTools/resource.h b/plugins/NetworkTools/resource.h index cea86c35f..5360b2c7a 100644 --- a/plugins/NetworkTools/resource.h +++ b/plugins/NetworkTools/resource.h @@ -11,6 +11,7 @@ #define AM_PNG 107 #define AN_PNG 108 #define AO_PNG 109 +#define IDD_OPTIONSKEY 109 #define AR_PNG 110 #define AS_PNG 111 #define AT_PNG 112 @@ -274,14 +275,16 @@ #define IDC_LIST_TRACERT 1030 #define IDC_REFRESH 1031 #define IDC_ENABLE_EXTENDED_TCP 1032 +#define IDC_HELPLINK 1036 +#define IDC_KEYTEXT 1037 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 108 +#define _APS_NEXT_RESOURCE_VALUE 111 #define _APS_NEXT_COMMAND_VALUE 40006 -#define _APS_NEXT_CONTROL_VALUE 1033 +#define _APS_NEXT_CONTROL_VALUE 1038 #define _APS_NEXT_SYMED_VALUE 104 #endif #endif diff --git a/plugins/NetworkTools/update.c b/plugins/NetworkTools/update.c index 15115c281..af20e3f61 100644 --- a/plugins/NetworkTools/update.c +++ b/plugins/NetworkTools/update.c @@ -5,14 +5,12 @@ * * Authors: * - * dmex 2016-2021 + * dmex 2016-2022 * */ #include "nettools.h" #include -#include "..\..\tools\thirdparty\gzip\zlib.h" -#include "..\..\tools\thirdparty\gzip\gzguts.h" HWND UpdateDialogHandle = NULL; HANDLE UpdateDialogThreadHandle = NULL; @@ -20,15 +18,36 @@ PH_EVENT InitializedEvent = PH_EVENT_INIT; PPH_OBJECT_TYPE UpdateContextType = NULL; PH_INITONCE UpdateContextTypeInitOnce = PH_INITONCE_INIT; +// Note: We're using the built-in tar.exe on Windows 10/11 for extracting the database +// updates since SI doesn't currently ship with a tar library. (dmex) +BOOLEAN GeoLiteCheckUpdatePlatformSupported( + VOID + ) +{ + BOOLEAN supported = FALSE; + PPH_STRING systemDirectory; + PPH_STRING fileName; + + if (systemDirectory = PhGetSystemDirectory()) + { + if (fileName = PhConcatStringRefZ(&systemDirectory->sr, L"\\tar.exe")) + { + supported = PhDoesFileExistWin32(PhGetString(fileName)); + PhDereferenceObject(fileName); + } + + PhDereferenceObject(systemDirectory); + } + + return supported; +} + VOID UpdateContextDeleteProcedure( _In_ PVOID Object, _In_ ULONG Flags ) { - PPH_UPDATER_CONTEXT context = Object; - - if (context->FileDownloadUrl) - PhDereferenceObject(context->FileDownloadUrl); + //PPH_UPDATER_CONTEXT context = Object; } PPH_UPDATER_CONTEXT CreateUpdateContext( @@ -39,7 +58,7 @@ PPH_UPDATER_CONTEXT CreateUpdateContext( if (PhBeginInitOnce(&UpdateContextTypeInitOnce)) { - UpdateContextType = PhCreateObjectType(L"GeoIpContextObjectType", 0, UpdateContextDeleteProcedure); + UpdateContextType = PhCreateObjectType(L"GeoLiteContextObjectType", 0, UpdateContextDeleteProcedure); PhEndInitOnce(&UpdateContextTypeInitOnce); } @@ -49,310 +68,259 @@ PPH_UPDATER_CONTEXT CreateUpdateContext( return context; } -VOID TaskDialogLinkClicked( - _In_ PPH_UPDATER_CONTEXT Context - ) -{ - PhShellExecute(Context->DialogHandle, L"https://www.maxmind.com", NULL); -} - -PPH_STRING UpdateVersionString( +PPH_STRING CreateUserAgentString( VOID ) { - ULONG majorVersion; - ULONG minorVersion; - ULONG revisionVersion; - PPH_STRING currentVersion; - PPH_STRING versionHeader = NULL; - - PhGetPhVersionNumbers(&majorVersion, &minorVersion, NULL, &revisionVersion); - - if (currentVersion = PhFormatString(L"%lu.%lu.%lu", majorVersion, minorVersion, revisionVersion)) - { - versionHeader = PhConcatStrings2(L"SystemInformer-Build: ", currentVersion->Buffer); - PhDereferenceObject(currentVersion); - } - - return versionHeader; -} - -PPH_STRING UpdateWindowsString( - VOID - ) -{ - PPH_STRING buildString = NULL; - PPH_STRING fileName = NULL; - PPH_STRING fileVersion = NULL; - PVOID versionInfo; - VS_FIXEDFILEINFO* rootBlock; - PH_FORMAT fileVersionFormat[3]; - - fileName = PhGetKernelFileName(); - PhMoveReference(&fileName, PhGetFileName(fileName)); - versionInfo = PhGetFileVersionInfo(fileName->Buffer); - PhDereferenceObject(fileName); - - if (versionInfo) - { - if (rootBlock = PhGetFileVersionFixedInfo(versionInfo)) - { - PhInitFormatU(&fileVersionFormat[0], HIWORD(rootBlock->dwFileVersionLS)); - PhInitFormatC(&fileVersionFormat[1], '.'); - PhInitFormatU(&fileVersionFormat[2], LOWORD(rootBlock->dwFileVersionLS)); - - fileVersion = PhFormat(fileVersionFormat, 3, 30); - } - - PhFree(versionInfo); - } - - if (fileVersion) - { - if (PhIsExecutingInWow64()) - buildString = PhFormatString(L"%s: %s.%s", L"SystemInformer-OsBuild", fileVersion->Buffer, L"x86fre"); - else - buildString = PhFormatString(L"%s: %s.%s", L"SystemInformer-OsBuild", fileVersion->Buffer, L"amd64fre"); - - PhDereferenceObject(fileVersion); - } - - return buildString; - - // - // NOTE: Some security products have started randomizing the build string located in the registry - // breaking the version check that blocks updates that are not compatible with old and unsupported - // versions of Windows. (dmex) - // - //static PH_STRINGREF keyName = PH_STRINGREF_INIT(L"Software\\Microsoft\\Windows NT\\CurrentVersion"); - //HANDLE keyHandle; - //PPH_STRING buildLabHeader = NULL; - // - //if (NT_SUCCESS(PhOpenKey( - // &keyHandle, - // KEY_READ, - // PH_KEY_LOCAL_MACHINE, - // &keyName, - // 0 - // ))) - //{ - // PPH_STRING buildLabString; - // - // if (buildLabString = PhQueryRegistryString(keyHandle, L"BuildLabEx")) - // { - // buildLabHeader = PhConcatStrings2(L"SystemInformer-OsBuild: ", buildLabString->Buffer); - // PhDereferenceObject(buildLabString); - // } - // else if (buildLabString = PhQueryRegistryString(keyHandle, L"BuildLab")) - // { - // buildLabHeader = PhConcatStrings2(L"SystemInformer-OsBuild: ", buildLabString->Buffer); - // PhDereferenceObject(buildLabString); - // } - // - // NtClose(keyHandle); - //} - // - //return buildLabHeader; -} - -PPH_STRING QueryFwLinkUrl( - _In_ PPH_UPDATER_CONTEXT Context - ) -{ - PPH_STRING redirectUrl = NULL; - PPH_HTTP_CONTEXT httpContext = NULL; - PPH_STRING versionString = NULL; - PPH_STRING userAgentString = NULL; - PPH_STRING versionHeader; - PPH_STRING windowsHeader; + PPH_STRING userAgentString; + PPH_STRING versionString; versionString = PhGetPhVersion(); - userAgentString = PhConcatStrings2(L"SystemInformer_", versionString->Buffer); + userAgentString = PhConcatStrings2(L"SystemInformer_", PhGetString(versionString)); + PhDereferenceObject(versionString); - if (!PhHttpSocketCreate(&httpContext, PhGetString(userAgentString))) - goto CleanupExit; - - if (!PhHttpSocketConnect(httpContext, L"wj32.org", PH_HTTP_DEFAULT_HTTPS_PORT)) - goto CleanupExit; - - if (!PhHttpSocketBeginRequest( - httpContext, - NULL, - L"/processhacker/fwlink/maxminddb.php", - PH_HTTP_FLAG_REFRESH | PH_HTTP_FLAG_SECURE - )) - { - goto CleanupExit; - } - - if (versionHeader = UpdateVersionString()) - { - PhHttpSocketAddRequestHeaders(httpContext, versionHeader->Buffer, (ULONG)versionHeader->Length / sizeof(WCHAR)); - PhDereferenceObject(versionHeader); - } - - if (windowsHeader = UpdateWindowsString()) - { - PhHttpSocketAddRequestHeaders(httpContext, windowsHeader->Buffer, (ULONG)windowsHeader->Length / sizeof(WCHAR)); - PhDereferenceObject(windowsHeader); - } - - if (!PhHttpSocketSetFeature(httpContext, PH_HTTP_FEATURE_REDIRECTS, FALSE)) - goto CleanupExit; - - if (!PhHttpSocketSendRequest(httpContext, NULL, 0)) - goto CleanupExit; - - if (!PhHttpSocketEndRequest(httpContext)) - goto CleanupExit; - - //redirectUrl = PhCreateString(L"https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz"); - redirectUrl = PhHttpSocketQueryHeaderString(httpContext, L"Location"); // WINHTTP_QUERY_LOCATION - -CleanupExit: - - if (httpContext) - PhHttpSocketDestroy(httpContext); - - PhClearReference(&versionString); - PhClearReference(&userAgentString); - - return redirectUrl; + return userAgentString; } -NTSTATUS GeoIPUpdateThread( - _In_ PVOID Parameter +NTSTATUS ExtractUpdateToFile( + _In_ PPH_STRING WorkingDirectory, + _In_ PPH_STRING CompressedFileName, + _In_ PCWSTR NewFileName + ) +{ + NTSTATUS status; + PPH_STRING commandLine; + PPH_STRING systemDirectory; + PH_FORMAT format[6]; + + if (!(systemDirectory = PhGetSystemDirectory())) + return STATUS_UNSUCCESSFUL; + + // tar --extract --file="GeoLite2-Country.tar.gz" --directory="%temp%\\guid" --strip-components=1 */GeoLite2-Country.mmdb + + PhInitFormatSR(&format[0], systemDirectory->sr); + PhInitFormatS(&format[1], L"\\tar.exe --extract --file=\""); + PhInitFormatSR(&format[2], CompressedFileName->sr); + PhInitFormatS(&format[3], L"\" --directory=\""); + PhInitFormatSR(&format[4], WorkingDirectory->sr); + PhInitFormatS(&format[5], L"\" --strip-components=1 */GeoLite2-Country.mmdb"); // NewFileName + commandLine = PhFormat(format, RTL_NUMBER_OF(format), 0x100); + + status = PhCreateProcessRedirection( + commandLine, + NULL + ); + + PhDereferenceObject(commandLine); + PhDereferenceObject(systemDirectory); + + return status; +} + +_Success_(return) +BOOLEAN DownloadUpdateToFile( + _In_ PPH_UPDATER_CONTEXT Context, + _Out_ PPH_STRING* UpdateFileName ) { BOOLEAN success = FALSE; + NTSTATUS status = STATUS_UNSUCCESSFUL; HANDLE tempFileHandle = NULL; - PPH_STRING fwLinkUrl; PPH_HTTP_CONTEXT httpContext = NULL; - PPH_STRING zipFilePath = NULL; - PPH_STRING versionString = NULL; - PPH_STRING userAgentString = NULL; - PPH_STRING httpHostName = NULL; - PPH_STRING httpHostPath = NULL; - PPH_STRING dbpath = NULL; - PPH_BYTES mmdbGzPath = NULL; - gzFile gzfile = NULL; - USHORT httpHostPort = 0; - LARGE_INTEGER timeNow; - LARGE_INTEGER timeStart; - ULONG64 timeTicks; - ULONG64 timeBitsPerSecond; - PPH_UPDATER_CONTEXT context = (PPH_UPDATER_CONTEXT)Parameter; + PPH_STRING httpRequestString = NULL; + PPH_STRING httpHeaderFileHash = NULL; + PPH_STRING httpHeaderFileName = NULL; + ULONG httpStatus = 0; + ULONG httpContentLength = 0; + PH_HASH_CONTEXT hashContext; - SendMessage(context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)L"Initializing download request..."); + PhInitializeHash(&hashContext, Md5HashAlgorithm); - if (!(fwLinkUrl = QueryFwLinkUrl(context))) - goto CleanupExit; - - SendMessage(context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)L"Connecting..."); - - if (!PhHttpSocketParseUrl( - fwLinkUrl, - &httpHostName, - &httpHostPath, - &httpHostPort - )) { + PPH_STRING apikeyString = PhGetStringSetting(SETTING_NAME_GEOLITE_API_KEY); + + if (PhIsNullOrEmptyString(apikeyString)) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + httpRequestString = PhFormatString( + L"/app/geoip_download?edition_id=%s&license_key=%s&suffix=tar.gz", + L"GeoLite2-Country", + PhGetString(apikeyString) + ); + + PhClearReference(&apikeyString); + } + + SendMessage(Context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)L"Connecting..."); + + { + PPH_STRING userAgentString = CreateUserAgentString(); + + if (!PhHttpSocketCreate(&httpContext, PhGetString(userAgentString))) + { + PhClearReference(&userAgentString); + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + PhClearReference(&userAgentString); + } + + if (!PhHttpSocketConnect(httpContext, L"download.maxmind.com", PH_HTTP_DEFAULT_HTTPS_PORT)) + { + Context->ErrorCode = GetLastError(); goto CleanupExit; } - versionString = PhGetPhVersion(); - userAgentString = PhConcatStrings2(L"SystemInformer_", versionString->Buffer); - - if (!PhHttpSocketCreate( - &httpContext, - PhGetString(userAgentString) - )) + if (!PhHttpSocketBeginRequest(httpContext, NULL, PhGetString(httpRequestString), PH_HTTP_FLAG_REFRESH | PH_HTTP_FLAG_SECURE)) { + Context->ErrorCode = GetLastError(); goto CleanupExit; } - if (!PhHttpSocketConnect( - httpContext, - PhGetString(httpHostName), - httpHostPort - )) - { - goto CleanupExit; - } - - if (!PhHttpSocketBeginRequest( - httpContext, - NULL, - PhGetString(httpHostPath), - PH_HTTP_FLAG_REFRESH | (httpHostPort == PH_HTTP_DEFAULT_HTTPS_PORT ? PH_HTTP_FLAG_SECURE : 0) - )) - { - goto CleanupExit; - } - - SendMessage(context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)L"Sending download request..."); + SendMessage(Context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)L"Sending download request..."); if (!PhHttpSocketSendRequest(httpContext, NULL, 0)) - goto CleanupExit; - - SendMessage(context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)L"Waiting for response..."); - - if (PhHttpSocketEndRequest(httpContext)) { + Context->ErrorCode = GetLastError(); + goto CleanupExit; + } + + SendMessage(Context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)L"Waiting for response..."); + + if (!PhHttpSocketEndRequest(httpContext)) + { + Context->ErrorCode = GetLastError(); + goto CleanupExit; + } + + // Check http request status. + + if (!PhHttpSocketQueryHeaderUlong(httpContext, PH_HTTP_QUERY_STATUS_CODE, &httpStatus)) + { + Context->ErrorCode = GetLastError(); + goto CleanupExit; + } + + if (httpStatus != PH_HTTP_STATUS_OK) + { + switch (httpStatus) + { + case 401: + Context->ErrorCode = ERROR_ACCESS_DENIED; + break; + default: + Context->ErrorCode = ERROR_UNHANDLED_ERROR; + break; + } + + goto CleanupExit; + } + + if (!PhHttpSocketQueryHeaderUlong(httpContext, PH_HTTP_QUERY_CONTENT_LENGTH, &httpContentLength)) + { + Context->ErrorCode = GetLastError(); + goto CleanupExit; + } + if (httpContentLength == 0) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + // Update status message. + + SendMessage(Context->DialogHandle, TDM_SET_MARQUEE_PROGRESS_BAR, FALSE, 0); + SendMessage(Context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)L"Downloading GeoLite2-Country..."); + + // Extract the content hash from the request headers. + + httpHeaderFileHash = PhHttpSocketQueryHeaderString(httpContext, L"ETag"); + + if (PhIsNullOrEmptyString(httpHeaderFileHash)) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + if (PhStartsWithString2(httpHeaderFileHash, L"\"", TRUE)) + PhSkipStringRef(&httpHeaderFileHash->sr, 1 * sizeof(WCHAR)); + if (PhEndsWithString2(httpHeaderFileHash, L"\"", TRUE)) + httpHeaderFileHash->Length -= 1 * sizeof(WCHAR); + + // Extract the filename from the request headers. + + httpHeaderFileName = PhHttpSocketQueryHeaderString(httpContext, L"content-disposition"); + + if (PhIsNullOrEmptyString(httpHeaderFileName)) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + if (!PhStartsWithString2(httpHeaderFileName, L"attachment; filename=", TRUE)) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + PhSkipStringRef(&httpHeaderFileName->sr, 21 * sizeof(WCHAR)); + + if (!PhStartsWithString2(httpHeaderFileName, L"GeoLite2-Country", TRUE)) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + if (!PhEndsWithString2(httpHeaderFileName, L".tar.gz", TRUE)) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + // Create temporary file in the cache directory. + + PhMoveReference(&httpHeaderFileName, PhCreateCacheFile(httpHeaderFileName)); + + if (PhIsNullOrEmptyString(httpHeaderFileName)) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + status = PhCreateFileWin32Ex( + &tempFileHandle, + PhGetString(httpHeaderFileName), + FILE_GENERIC_READ | FILE_GENERIC_WRITE, + &(LARGE_INTEGER){ .QuadPart = httpContentLength }, + FILE_ATTRIBUTE_NORMAL, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + FILE_OVERWRITE_IF, + FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, + NULL + ); + + if (!NT_SUCCESS(status)) + { + Context->ErrorCode = PhNtStatusToDosError(status); + goto CleanupExit; + } + + // Start downloading the update into the cache. + { + LARGE_INTEGER timeNow; + LARGE_INTEGER timeStart; + ULONG64 timeTicks; + ULONG64 timeBitsPerSecond; ULONG bytesDownloaded = 0; - ULONG downloadedBytes = 0; - ULONG contentLength = 0; - PPH_STRING status; + ULONG bytesTotalDownloaded = 0; IO_STATUS_BLOCK isb; BYTE buffer[PAGE_SIZE]; - memset(buffer, 0, PAGE_SIZE); - - status = PhFormatString(L"Downloading GeoLite2-Country..."); - SendMessage(context->DialogHandle, TDM_SET_MARQUEE_PROGRESS_BAR, FALSE, 0); - SendMessage(context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_MAIN_INSTRUCTION, (LPARAM)status->Buffer); - PhDereferenceObject(status); - - if (!PhHttpSocketQueryHeaderUlong( - httpContext, - PH_HTTP_QUERY_CONTENT_LENGTH, - &contentLength - )) - { - //context->ErrorCode = GetLastError(); - goto CleanupExit; - } - else - { - LARGE_INTEGER allocationSize; - - { - PPH_STRING temp = PhCreateString2(&GeoDbFileName); - zipFilePath = PhCreateCacheFile(temp); - PhClearReference(&temp); - } - - if (PhIsNullOrEmptyString(zipFilePath)) - goto CleanupExit; - - allocationSize.QuadPart = contentLength; - - if (!NT_SUCCESS(PhCreateFileWin32Ex( - &tempFileHandle, - PhGetString(zipFilePath), - FILE_GENERIC_READ | FILE_GENERIC_WRITE, - &allocationSize, - FILE_ATTRIBUTE_NORMAL, - FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, - FILE_OVERWRITE_IF, - FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, - NULL - ))) - { - goto CleanupExit; - } - } - + memset(buffer, 0, sizeof(buffer)); PhQuerySystemTime(&timeStart); while (PhHttpSocketReadData(httpContext, buffer, PAGE_SIZE, &bytesDownloaded)) @@ -361,11 +329,14 @@ NTSTATUS GeoIPUpdateThread( if (bytesDownloaded == 0) break; - // If the dialog was closed; cleanup and exit. - if (!context->DialogHandle) + // If the dialog was closed then cleanup and exit. + if (!Context->DialogHandle) + { + Context->ErrorCode = ERROR_INVALID_DATA; goto CleanupExit; + } - if (!NT_SUCCESS(NtWriteFile( + status = NtWriteFile( tempFileHandle, NULL, NULL, @@ -375,35 +346,43 @@ NTSTATUS GeoIPUpdateThread( bytesDownloaded, NULL, NULL - ))) + ); + + if (!NT_SUCCESS(status)) { + Context->ErrorCode = PhNtStatusToDosError(status); goto CleanupExit; } - downloadedBytes += (ULONG)isb.Information; - - // Check the number of bytes written are the same we downloaded. + // Check the downloaded was copied to the file. if (bytesDownloaded != isb.Information) + { + Context->ErrorCode = ERROR_INVALID_DATA; goto CleanupExit; + } + + // Update the hash. + PhUpdateHash(&hashContext, buffer, bytesDownloaded); // Query the current time PhQuerySystemTime(&timeNow); // Calculate the number of ticks + bytesTotalDownloaded += (ULONG)isb.Information; timeTicks = (timeNow.QuadPart - timeStart.QuadPart) / PH_TICKS_PER_SEC; - timeBitsPerSecond = downloadedBytes / __max(timeTicks, 1); + timeBitsPerSecond = bytesTotalDownloaded / __max(timeTicks, 1); - // TODO: Update on timer callback. + // Update download status (TODO: Update on timer callback) { - FLOAT percent = ((FLOAT)downloadedBytes / contentLength * 100); + FLOAT percent = ((FLOAT)bytesTotalDownloaded / httpContentLength * 100); PH_FORMAT format[9]; WCHAR string[MAX_PATH]; // L"Downloaded: %s of %s (%.0f%%)\r\nSpeed: %s/s" PhInitFormatS(&format[0], L"Downloaded: "); - PhInitFormatSize(&format[1], downloadedBytes); + PhInitFormatSize(&format[1], bytesTotalDownloaded); PhInitFormatS(&format[2], L" of "); - PhInitFormatSize(&format[3], contentLength); + PhInitFormatSize(&format[3], httpContentLength); PhInitFormatS(&format[4], L" ("); PhInitFormatF(&format[5], percent, 1); PhInitFormatS(&format[6], L"%)\r\nSpeed: "); @@ -412,120 +391,179 @@ NTSTATUS GeoIPUpdateThread( if (PhFormatToBuffer(format, RTL_NUMBER_OF(format), string, sizeof(string), NULL)) { - SendMessage(context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_CONTENT, (LPARAM)string); + SendMessage(Context->DialogHandle, TDM_UPDATE_ELEMENT_TEXT, TDE_CONTENT, (LPARAM)string); } - SendMessage(context->DialogHandle, TDM_SET_PROGRESS_BAR_POS, (WPARAM)percent, 0); + SendMessage(Context->DialogHandle, TDM_SET_PROGRESS_BAR_POS, (WPARAM)percent, 0); } } + } + // Check the file hash matches the original ETag hash. + { + PPH_STRING finalHashString; + UCHAR finalHashBuffer[32]; + + if (!PhFinalHash(&hashContext, finalHashBuffer, 16, NULL)) { - dbpath = PhGetApplicationDataFileName(&GeoDbFileName, FALSE); - - if (PhIsNullOrEmptyString(dbpath)) - goto CleanupExit; - - if (PhDoesFileExistWin32(PhGetString(dbpath))) - { - if (!NT_SUCCESS(PhDeleteFileWin32(PhGetString(dbpath)))) - goto CleanupExit; - } - else - { - if (!NT_SUCCESS(PhCreateDirectoryFullPathWin32(&dbpath->sr))) - goto CleanupExit; - } - - mmdbGzPath = PhConvertUtf16ToUtf8(PhGetString(zipFilePath)); - - if (gzfile = gzopen(mmdbGzPath->Buffer, "rb")) - { - HANDLE mmdbFileHandle; - - if (NT_SUCCESS(PhCreateFileWin32( - &mmdbFileHandle, - PhGetStringOrEmpty(dbpath), - FILE_GENERIC_READ | FILE_GENERIC_WRITE, - FILE_ATTRIBUTE_NORMAL, - FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, - FILE_OVERWRITE_IF, - FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT - ))) - { - IO_STATUS_BLOCK isb; - BYTE buffer[PAGE_SIZE]; - - while (!gzeof(gzfile)) - { - INT bytes = gzread(gzfile, buffer, sizeof(buffer)); - - if (bytes == -1) - { - NtClose(mmdbFileHandle); - goto CleanupExit; - } - - if (!NT_SUCCESS(NtWriteFile( - mmdbFileHandle, - NULL, - NULL, - NULL, - &isb, - buffer, - bytes, - NULL, - NULL - ))) - { - NtClose(mmdbFileHandle); - goto CleanupExit; - } - } - - success = TRUE; - - NtClose(mmdbFileHandle); - } - - gzclose(gzfile); - gzfile = NULL; - } + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; } + + finalHashString = PhBufferToHexString(finalHashBuffer, 16); + + if (!PhEqualString(finalHashString, httpHeaderFileHash, TRUE)) + { + Context->ErrorCode = ERROR_SYSTEM_IMAGE_BAD_SIGNATURE; + goto CleanupExit; + } + + success = TRUE; + + PhClearReference(&finalHashString); } CleanupExit: - if (gzfile) - gzclose(gzfile); - if (mmdbGzPath) - PhDereferenceObject(mmdbGzPath); - if (dbpath) - PhDereferenceObject(dbpath); - if (tempFileHandle) NtClose(tempFileHandle); - if (httpContext) PhHttpSocketDestroy(httpContext); + if (httpHeaderFileHash) + PhDereferenceObject(httpHeaderFileHash); + if (httpRequestString) + PhDereferenceObject(httpRequestString); - if (userAgentString) - PhDereferenceObject(userAgentString); - if (versionString) - PhDereferenceObject(versionString); - - if (zipFilePath) + if (success) { - PhDeleteCacheFile(zipFilePath); - PhDereferenceObject(zipFilePath); + *UpdateFileName = httpHeaderFileName; + } + else + { + if (httpHeaderFileName) + { + PhDeleteCacheFile(httpHeaderFileName); + PhDereferenceObject(httpHeaderFileName); + } } - if (context->DialogHandle) + return success; +} + +BOOLEAN MoveUpdateToFile( + _In_ PPH_UPDATER_CONTEXT Context, + _In_ PPH_STRING UpdateFileName + ) +{ + BOOLEAN success = FALSE; + NTSTATUS status = STATUS_UNSUCCESSFUL; + PPH_STRING existingFileName = NULL; + + // Get the current database filename. + + existingFileName = PhGetApplicationDataFileName(&GeoDbFileName, FALSE); + + if (PhIsNullOrEmptyString(existingFileName)) { - PostMessage(context->DialogHandle, - success ? PH_SHOWINSTALL : PH_SHOWERROR, 0, 0); + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; } - PhDereferenceObject(context); + // Backup the current database. + + //backupFileName = PhConcatStringRefZ(&existingFileName->sr, L".backup"); + //if (!NT_SUCCESS(PhMoveFileWin32(PhGetString(existingFileName), PhGetString(backupFileName)))) + // goto CleanupExit; + + // Delete the current database. + + if (PhDoesFileExistWin32(PhGetString(existingFileName))) + { + if (!NT_SUCCESS(status = PhDeleteFileWin32(PhGetString(existingFileName)))) + { + Context->ErrorCode = PhNtStatusToDosError(status); + goto CleanupExit; + } + } + else + { + if (!NT_SUCCESS(status = PhCreateDirectoryFullPathWin32(&existingFileName->sr))) + { + Context->ErrorCode = PhNtStatusToDosError(status); + goto CleanupExit; + } + } + + // Move the update from the cache to the correct location. + + if (!NT_SUCCESS(status = PhMoveFileWin32(PhGetString(UpdateFileName), PhGetString(existingFileName)))) + { + Context->ErrorCode = PhNtStatusToDosError(status); + goto CleanupExit; + } + + success = TRUE; + +CleanupExit: + if (existingFileName) + PhDereferenceObject(existingFileName); + + return success; +} + +NTSTATUS GeoLiteUpdateThread( + _In_ PPH_UPDATER_CONTEXT Context + ) +{ + BOOLEAN success = FALSE; + PPH_STRING compressedFileName = NULL; + PPH_STRING cacheDirectory = NULL; + PPH_STRING updateFileName = NULL; + + // Download the update into the cache. + + if (!DownloadUpdateToFile(Context, &compressedFileName)) + goto CleanupExit; + + // Extract the update into the cache. + + cacheDirectory = PhGetBaseDirectory(compressedFileName); + + if (!NT_SUCCESS(ExtractUpdateToFile(cacheDirectory, compressedFileName, L"\\GeoLite2-Country.mmdb"))) + goto CleanupExit; + + // Check the update file was extracted. + + updateFileName = PhConcatStringRefZ(&cacheDirectory->sr, L"\\GeoLite2-Country.mmdb"); + + if (!PhDoesFileExistWin32(PhGetString(updateFileName))) + { + Context->ErrorCode = ERROR_INVALID_DATA; + goto CleanupExit; + } + + // Update the database to the latest version. + + if (!MoveUpdateToFile(Context, updateFileName)) + goto CleanupExit; + + success = TRUE; + +CleanupExit: + + if (updateFileName) + PhDereferenceObject(updateFileName); + if (cacheDirectory) + PhDereferenceObject(cacheDirectory); + if (compressedFileName) + PhDereferenceObject(compressedFileName); + + if (Context->DialogHandle) + { + PostMessage(Context->DialogHandle, success ? PH_SHOWINSTALL : PH_SHOWERROR, 0, 0); + } + + PhDereferenceObject(Context); return STATUS_SUCCESS; } @@ -616,7 +654,7 @@ HRESULT CALLBACK TaskDialogBootstrapCallback( return S_OK; } -NTSTATUS GeoIPUpdateDialogThread( +NTSTATUS GeoLiteUpdateTaskDialogThread( _In_ PVOID Parameter ) { @@ -693,20 +731,77 @@ NTSTATUS GeoIPUpdateDialogThread( //} } -VOID ShowGeoIPUpdateDialog( - VOID +HRESULT CALLBACK GeoLiteMissingKeyTaskDialogCallbackProc( + _In_ HWND hwndDlg, + _In_ UINT uMsg, + _In_ WPARAM wParam, + _In_ LPARAM lParam, + _In_ LONG_PTR dwRefData ) { - if (!UpdateDialogThreadHandle) + switch (uMsg) { - if (!NT_SUCCESS(PhCreateThreadEx(&UpdateDialogThreadHandle, GeoIPUpdateDialogThread, NULL))) + case TDN_HYPERLINK_CLICKED: { - PhShowError(NULL, L"%s", L"Unable to create the window."); - return; - } + PWSTR hyperlink = (PWSTR)lParam; - PhWaitForEvent(&InitializedEvent, NULL); + PhShellExecute(hwndDlg, hyperlink, NULL); + } + break; } - PostMessage(UpdateDialogHandle, PH_SHOWDIALOG, 0, 0); + return S_OK; +} + +VOID ShowGeoLiteUpdateDialog( + _In_opt_ HWND ParentWindowHandle + ) +{ + if (!GeoLiteCheckUpdatePlatformSupported()) + { + PhShowError(ParentWindowHandle, L"%s", L"The GeoLite updater doesn't support legacy versions of Windows."); + return; + } + + PPH_STRING geolitekey = PhGetStringSetting(SETTING_NAME_GEOLITE_API_KEY); + + if (PhIsNullOrEmptyString(geolitekey)) + { + PhClearReference(&geolitekey); + + TASKDIALOGCONFIG config = { sizeof(TASKDIALOGCONFIG) }; + config.dwFlags = TDF_ENABLE_HYPERLINKS | TDF_ALLOW_DIALOG_CANCELLATION | TDF_POSITION_RELATIVE_TO_WINDOW; + config.dwCommonButtons = TDCBF_OK_BUTTON; + config.pszMainIcon = TD_ERROR_ICON; + config.hwndParent = ParentWindowHandle; + config.pszWindowTitle = L"Network Tools - GeoLite Updater"; + config.pszMainInstruction = L"Unable to download GeoLite database updates."; + config.pszContent = + L"A license key is required to download GeoLite database updates and there are no keys configured.\n\n" + L"The license keys are completely free. If you're unsure how to create keys then please review the documentation here: Generate-a-License-Key\n\n" + L"Once you've created the key you can copy/paste the text into the Options window > NetworkTools settings and System Informer can start downloading GeoLite database updates.\n\n" + L"Special thanks to MaxMind (http://www.maxmind.com) for continuing free GeoLite services <3"; + + config.pfCallback = GeoLiteMissingKeyTaskDialogCallbackProc; + config.cxWidth = 200; + + TaskDialogIndirect(&config, NULL, NULL, NULL); + } + else + { + PhClearReference(&geolitekey); + + if (!UpdateDialogThreadHandle) + { + if (!NT_SUCCESS(PhCreateThreadEx(&UpdateDialogThreadHandle, GeoLiteUpdateTaskDialogThread, NULL))) + { + PhShowError(ParentWindowHandle, L"%s", L"Unable to create the window."); + return; + } + + PhWaitForEvent(&InitializedEvent, NULL); + } + + PostMessage(UpdateDialogHandle, PH_SHOWDIALOG, 0, 0); + } }