1 /* 2 * Copyright 2018 Piotr Caban for CodeWeavers 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 17 * 18 */ 19 20 #include <stdarg.h> 21 #include <windef.h> 22 #include <winbase.h> 23 #include <winnls.h> 24 #include <wininet.h> 25 #include <winuser.h> 26 #include <winreg.h> 27 28 #include "inetcpl.h" 29 #include "wine/debug.h" 30 #include "wine/heap.h" 31 32 WINE_DEFAULT_DEBUG_CHANNEL(inetcpl); 33 34 static const WCHAR auto_config_url[] = {'A','u','t','o','C','o','n','f','i','g','U','R','L',0}; 35 static const WCHAR internet_settings[] = {'S','o','f','t','w','a','r','e','\\', 36 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\', 37 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\', 38 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0}; 39 static const WCHAR proxy_enable[] = {'P','r','o','x','y','E','n','a','b','l','e',0}; 40 static const WCHAR proxy_server[] = {'P','r','o','x','y','S','e','r','v','e','r',0}; 41 static const WCHAR connections[] = {'C','o','n','n','e','c','t','i','o','n','s',0}; 42 static const WCHAR default_connection_settings[] = {'D','e','f','a','u','l','t', 43 'C','o','n','n','e','c','t','i','o','n','S','e','t','t','i','n','g','s',0}; 44 45 static BOOL initdialog_done; 46 47 #define CONNECTION_SETTINGS_VERSION 0x46 48 #define CONNECTION_SETTINGS_MANUAL_PROXY 0x2 49 #define CONNECTION_SETTINGS_PAC_SCRIPT 0x4 50 #define CONNECTION_SETTINGS_WPAD 0x8 51 52 typedef struct { 53 DWORD version; 54 DWORD id; 55 DWORD flags; 56 BYTE data[1]; 57 /* DWORD proxy_server_len; */ 58 /* UTF8 proxy_server[proxy_server_len]; */ 59 /* DWORD bypass_list_len; */ 60 /* UTF8 bypass_list[bypass_list_len]; */ 61 /* DWORD configuration_script_len; */ 62 /* UTF8 configuration_script[configuration_script_len]; */ 63 /* DWORD unk[8]; set to 0 */ 64 } connection_settings; 65 66 static DWORD create_connection_settings(BOOL manual_proxy, const WCHAR *proxy_server, 67 BOOL use_wpad, BOOL use_pac_script, const WCHAR *pac_url, connection_settings **ret) 68 { 69 DWORD size = FIELD_OFFSET(connection_settings, data), pos; 70 DWORD proxy_server_len; 71 DWORD pac_url_len; 72 73 size += sizeof(DWORD); 74 if(proxy_server) 75 { 76 proxy_server_len = WideCharToMultiByte(CP_UTF8, 0, proxy_server, -1, 77 NULL, 0, NULL, NULL); 78 if(!proxy_server_len) return 0; 79 proxy_server_len--; 80 } 81 else 82 proxy_server_len = 0; 83 size += proxy_server_len; 84 if(pac_url) 85 { 86 pac_url_len = WideCharToMultiByte(CP_UTF8, 0, pac_url, -1, 87 NULL, 0, NULL, NULL); 88 if(!pac_url_len) return 0; 89 pac_url_len--; 90 } 91 else 92 pac_url_len = 0; 93 size += sizeof(DWORD)*10; 94 95 *ret = heap_alloc_zero(size); 96 if(!*ret) return 0; 97 98 (*ret)->version = CONNECTION_SETTINGS_VERSION; 99 (*ret)->flags = 1; 100 if(manual_proxy) (*ret)->flags |= CONNECTION_SETTINGS_MANUAL_PROXY; 101 if(use_pac_script) (*ret)->flags |= CONNECTION_SETTINGS_PAC_SCRIPT; 102 if(use_wpad) (*ret)->flags |= CONNECTION_SETTINGS_WPAD; 103 ((DWORD*)(*ret)->data)[0] = proxy_server_len; 104 pos = sizeof(DWORD); 105 if(proxy_server_len) 106 { 107 WideCharToMultiByte(CP_UTF8, 0, proxy_server, -1, 108 (char*)(*ret)->data+pos, proxy_server_len, NULL, NULL); 109 pos += proxy_server_len; 110 } 111 pos += sizeof(DWORD); /* skip proxy bypass list */ 112 ((DWORD*)((*ret)->data+pos))[0] = pac_url_len; 113 pos += sizeof(DWORD); 114 if(pac_url_len) 115 { 116 WideCharToMultiByte(CP_UTF8, 0, pac_url, -1, 117 (char*)(*ret)->data+pos, pac_url_len, NULL, NULL); 118 pos += pac_url_len; 119 } 120 return size; 121 } 122 123 static void connections_on_initdialog(HWND hwnd) 124 { 125 DWORD type, size, enabled; 126 WCHAR address[INTERNET_MAX_URL_LENGTH], *port; 127 WCHAR pac_url[INTERNET_MAX_URL_LENGTH]; 128 HKEY hkey, con; 129 LONG res; 130 131 SendMessageW(GetDlgItem(hwnd, IDC_EDIT_PAC_SCRIPT), 132 EM_LIMITTEXT, INTERNET_MAX_URL_LENGTH, 0); 133 SendMessageW(GetDlgItem(hwnd, IDC_EDIT_PROXY_SERVER), 134 EM_LIMITTEXT, INTERNET_MAX_URL_LENGTH-10, 0); 135 SendMessageW(GetDlgItem(hwnd, IDC_EDIT_PROXY_PORT), EM_LIMITTEXT, 8, 0); 136 137 res = RegOpenKeyW(HKEY_CURRENT_USER, internet_settings, &hkey); 138 if(res) 139 return; 140 141 size = sizeof(enabled); 142 res = RegQueryValueExW(hkey, proxy_enable, NULL, &type, (BYTE*)&enabled, &size); 143 if(res || type != REG_DWORD) 144 enabled = 0; 145 size = sizeof(address); 146 res = RegQueryValueExW(hkey, proxy_server, NULL, &type, (BYTE*)address, &size); 147 if(res || type != REG_SZ) 148 address[0] = 0; 149 size = sizeof(pac_url); 150 res = RegQueryValueExW(hkey, auto_config_url, NULL, &type, (BYTE*)pac_url, &size); 151 if(res || type != REG_SZ) 152 pac_url[0] = 0; 153 154 res = RegOpenKeyW(hkey, connections, &con); 155 RegCloseKey(hkey); 156 if(!res) 157 { 158 connection_settings *settings = NULL; 159 size = 0; 160 161 while((res = RegQueryValueExW(con, default_connection_settings, NULL, &type, 162 (BYTE*)settings, &size)) == ERROR_MORE_DATA || !settings) 163 { 164 connection_settings *new_settings = heap_realloc(settings, size); 165 if(!new_settings) 166 { 167 RegCloseKey(con); 168 heap_free(settings); 169 return; 170 } 171 settings = new_settings; 172 } 173 RegCloseKey(con); 174 175 if(!res && type == REG_BINARY) 176 { 177 if(settings->version != CONNECTION_SETTINGS_VERSION) 178 FIXME("unexpected structure version (%x)\n", settings->version); 179 else if(settings->flags & CONNECTION_SETTINGS_WPAD) 180 CheckDlgButton(hwnd, IDC_USE_WPAD, BST_CHECKED); 181 } 182 heap_free(settings); 183 } 184 185 TRACE("ProxyEnable = %x\n", enabled); 186 TRACE("ProxyServer = %s\n", wine_dbgstr_w(address)); 187 TRACE("AutoConfigURL = %s\n", wine_dbgstr_w(auto_config_url)); 188 189 if(enabled) 190 { 191 CheckDlgButton(hwnd, IDC_USE_PROXY_SERVER, BST_CHECKED); 192 EnableWindow(GetDlgItem(hwnd, IDC_EDIT_PROXY_SERVER), TRUE); 193 EnableWindow(GetDlgItem(hwnd, IDC_EDIT_PROXY_PORT), TRUE); 194 } 195 196 port = wcschr(address, ':'); 197 if(port) 198 { 199 *port = 0; 200 port++; 201 } 202 SetDlgItemTextW(hwnd, IDC_EDIT_PROXY_SERVER, address); 203 if(port) 204 SetDlgItemTextW(hwnd, IDC_EDIT_PROXY_PORT, port); 205 206 if(pac_url[0]) 207 { 208 CheckDlgButton(hwnd, IDC_USE_PAC_SCRIPT, BST_CHECKED); 209 EnableWindow(GetDlgItem(hwnd, IDC_EDIT_PAC_SCRIPT), TRUE); 210 SetDlgItemTextW(hwnd, IDC_EDIT_PAC_SCRIPT, pac_url); 211 } 212 213 return; 214 } 215 216 static INT_PTR connections_on_command(HWND hwnd, WPARAM wparam) 217 { 218 BOOL checked; 219 220 switch (wparam) 221 { 222 case IDC_USE_PAC_SCRIPT: 223 checked = IsDlgButtonChecked(hwnd, IDC_USE_PAC_SCRIPT); 224 EnableWindow(GetDlgItem(hwnd, IDC_EDIT_PAC_SCRIPT), checked); 225 break; 226 case IDC_USE_PROXY_SERVER: 227 checked = IsDlgButtonChecked(hwnd, IDC_USE_PROXY_SERVER); 228 EnableWindow(GetDlgItem(hwnd, IDC_EDIT_PROXY_SERVER), checked); 229 EnableWindow(GetDlgItem(hwnd, IDC_EDIT_PROXY_PORT), checked); 230 } 231 232 switch (wparam) 233 { 234 case IDC_USE_WPAD: 235 case IDC_USE_PAC_SCRIPT: 236 case IDC_USE_PROXY_SERVER: 237 case MAKEWPARAM(IDC_EDIT_PAC_SCRIPT, EN_CHANGE): 238 case MAKEWPARAM(IDC_EDIT_PROXY_SERVER, EN_CHANGE): 239 case MAKEWPARAM(IDC_EDIT_PROXY_PORT, EN_CHANGE): 240 if(initdialog_done) 241 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0); 242 return TRUE; 243 } 244 245 return FALSE; 246 } 247 248 static INT_PTR connections_on_notify(HWND hwnd, WPARAM wparam, LPARAM lparam) 249 { 250 connection_settings *default_connection; 251 WCHAR proxy[INTERNET_MAX_URL_LENGTH]; 252 WCHAR pac_script[INTERNET_MAX_URL_LENGTH]; 253 PSHNOTIFY *psn = (PSHNOTIFY*)lparam; 254 DWORD proxy_len, port_len, pac_script_len; 255 DWORD use_proxy, use_pac_script, use_wpad, size; 256 LRESULT res; 257 HKEY hkey, con; 258 259 if(psn->hdr.code != PSN_APPLY) 260 return FALSE; 261 262 res = RegOpenKeyW(HKEY_CURRENT_USER, internet_settings, &hkey); 263 if(res) 264 return FALSE; 265 266 use_proxy = IsDlgButtonChecked(hwnd, IDC_USE_PROXY_SERVER); 267 res = RegSetValueExW(hkey, proxy_enable, 0, REG_DWORD, 268 (BYTE*)&use_proxy, sizeof(use_proxy)); 269 if(res) 270 { 271 RegCloseKey(hkey); 272 return FALSE; 273 } 274 TRACE("ProxyEnable set to %x\n", use_proxy); 275 276 proxy_len = GetDlgItemTextW(hwnd, IDC_EDIT_PROXY_SERVER, proxy, ARRAY_SIZE(proxy)); 277 if(proxy_len) 278 { 279 proxy[proxy_len++] = ':'; 280 port_len = GetDlgItemTextW(hwnd, IDC_EDIT_PROXY_PORT, proxy+proxy_len, 281 ARRAY_SIZE(proxy)-proxy_len); 282 if(!port_len) 283 { 284 proxy[proxy_len++] = '8'; 285 proxy[proxy_len++] = '0'; 286 proxy[proxy_len] = 0; 287 } 288 289 res = RegSetValueExW(hkey, proxy_server, 0, REG_SZ, 290 (BYTE*)proxy, (proxy_len+port_len)*sizeof(WCHAR)); 291 } 292 else 293 { 294 res = RegDeleteValueW(hkey, proxy_server); 295 if(res == ERROR_FILE_NOT_FOUND) 296 res = ERROR_SUCCESS; 297 } 298 if(res) 299 { 300 RegCloseKey(hkey); 301 return FALSE; 302 } 303 TRACE("ProxtServer set to %s\n", wine_dbgstr_w(proxy)); 304 305 use_pac_script = IsDlgButtonChecked(hwnd, IDC_USE_PAC_SCRIPT); 306 pac_script_len = GetDlgItemTextW(hwnd, IDC_EDIT_PAC_SCRIPT, 307 pac_script, ARRAY_SIZE(pac_script)); 308 if(!pac_script_len) use_pac_script = FALSE; 309 if(use_pac_script) 310 { 311 res = RegSetValueExW(hkey, auto_config_url, 0, REG_SZ, 312 (BYTE*)pac_script, pac_script_len*sizeof(WCHAR)); 313 } 314 else 315 { 316 res = RegDeleteValueW(hkey, auto_config_url); 317 if(res == ERROR_FILE_NOT_FOUND) 318 res = ERROR_SUCCESS; 319 } 320 if(res) 321 { 322 RegCloseKey(hkey); 323 return FALSE; 324 } 325 TRACE("AutoConfigURL set to %s\n", wine_dbgstr_w(use_pac_script ? pac_script : NULL)); 326 327 use_wpad = IsDlgButtonChecked(hwnd, IDC_USE_WPAD); 328 329 res = RegCreateKeyExW(hkey, connections, 0, NULL, 0, KEY_WRITE, NULL, &con, NULL); 330 RegCloseKey(hkey); 331 if(res) 332 return FALSE; 333 334 size = create_connection_settings(use_proxy, proxy, use_wpad, 335 use_pac_script, pac_script, &default_connection); 336 if(!size) 337 { 338 RegCloseKey(con); 339 return FALSE; 340 } 341 342 res = RegSetValueExW(con, default_connection_settings, 0, REG_BINARY, 343 (BYTE*)default_connection, size); 344 heap_free(default_connection); 345 RegCloseKey(con); 346 return !res; 347 } 348 349 INT_PTR CALLBACK connections_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) 350 { 351 switch (msg) 352 { 353 case WM_INITDIALOG: 354 connections_on_initdialog(hwnd); 355 initdialog_done = TRUE; 356 break; 357 case WM_COMMAND: 358 return connections_on_command(hwnd, wparam); 359 case WM_NOTIFY: 360 return connections_on_notify(hwnd, wparam, lparam); 361 } 362 return FALSE; 363 } 364