Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

963 lines
37 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: cmproxy.cpp
  4. //
  5. // Module: CMPROXY.DLL (TOOL)
  6. //
  7. // Synopsis: Main source for IE proxy setting connect action
  8. //
  9. // Copyright (c) 1999 Microsoft Corporation
  10. //
  11. // Author: quintinb Created 10/27/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "pch.h"
  15. //
  16. // Include the locale-safe replacement for lstrcmpi
  17. //
  18. #include "CompareString.cpp"
  19. const CHAR* const c_pszInternetSettingsPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
  20. const CHAR* const c_pszProxyEnable = "ProxyEnable";
  21. const CHAR* const c_pszProxyServer = "ProxyServer";
  22. const CHAR* const c_pszProxyOverride = "ProxyOverride";
  23. const CHAR* const c_pszManualProxySection = "Manual Proxy";
  24. const CHAR* const c_pszAutoProxySection = "Automatic Proxy";
  25. const CHAR* const c_pszAutoConfigScript = "AutoConfigScript";
  26. const CHAR* const c_pszAutoProxyEnable = "AutoProxyEnable";
  27. const CHAR* const c_pszAutoConfigScriptEnable = "AutoConfigScriptEnable";
  28. const CHAR* const c_pszUseVpnName = "UseVpnName";
  29. const CHAR* const c_pszEmpty = "";
  30. //
  31. // Typedefs and Function Pointers for the Wininet APIs that we use.
  32. //
  33. typedef BOOL (WINAPI* pfnInternetQueryOptionSpec)(HINTERNET, DWORD, LPVOID, LPDWORD);
  34. typedef BOOL (WINAPI* pfnInternetSetOptionSpec)(HINTERNET, DWORD, LPVOID, DWORD);
  35. pfnInternetQueryOptionSpec g_pfnInternetQueryOption = NULL;
  36. pfnInternetSetOptionSpec g_pfnInternetSetOption = NULL;
  37. //+----------------------------------------------------------------------------
  38. //
  39. // Function: SetIE5ProxySettings
  40. //
  41. // Synopsis: This function sets the IE5, per connection proxy settings using
  42. // the given connection, enabled value, proxy server, and override
  43. // settings.
  44. //
  45. // Arguments: LPSTR pszConnection - Connection name to set the proxy settings for
  46. // BOOL bManualProxy - whether the manual proxy is enabled or not
  47. // BOOL bAutomaticProxy - whether the auto proxy detection is enabled
  48. // BOOL bAutoConfigScript - whether an auto config script should be used
  49. // LPSTR pszProxyServer - proxy server name in the proxyserver:port format
  50. // LPSTR pszProxyOverride - a semi-colon seperated list of
  51. // realms to bypass the proxy for
  52. // LPSTR pszAutoConfigScript - auto config URL
  53. //
  54. // Returns: HRESULT - Standard COM return codes
  55. //
  56. // History: quintinb Created 10/27/99
  57. //
  58. //+----------------------------------------------------------------------------
  59. HRESULT SetIE5ProxySettings(LPSTR pszConnection, BOOL bManualProxy, BOOL bAutomaticProxy, BOOL bAutoConfigScript,
  60. LPSTR pszProxyServer, LPSTR pszProxyOverride, LPSTR pszAutoConfigScript)
  61. {
  62. //
  63. // Check Inputs, note allow pszConnection to be NULL (to set the LAN connection)
  64. //
  65. if ((NULL == g_pfnInternetSetOption) || (NULL == pszProxyServer) ||
  66. (NULL == pszProxyOverride) || (NULL == pszAutoConfigScript))
  67. {
  68. return E_INVALIDARG;
  69. }
  70. HRESULT hr = S_OK;
  71. INTERNET_PER_CONN_OPTION_LIST PerConnOptionList;
  72. DWORD dwSize = sizeof(PerConnOptionList);
  73. PerConnOptionList.dwSize = sizeof(PerConnOptionList);
  74. PerConnOptionList.pszConnection = pszConnection;
  75. PerConnOptionList.dwOptionCount = 4;
  76. PerConnOptionList.dwOptionError = 0;
  77. PerConnOptionList.pOptions = (INTERNET_PER_CONN_OPTION*)CmMalloc(4*sizeof(INTERNET_PER_CONN_OPTION));
  78. if(NULL == PerConnOptionList.pOptions)
  79. {
  80. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  81. }
  82. //
  83. // set flags
  84. //
  85. PerConnOptionList.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
  86. PerConnOptionList.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT;
  87. if (bManualProxy)
  88. {
  89. PerConnOptionList.pOptions[0].Value.dwValue |= PROXY_TYPE_PROXY;
  90. }
  91. if (bAutomaticProxy)
  92. {
  93. PerConnOptionList.pOptions[0].Value.dwValue |= PROXY_TYPE_AUTO_DETECT;
  94. }
  95. if (bAutoConfigScript)
  96. {
  97. PerConnOptionList.pOptions[0].Value.dwValue |= PROXY_TYPE_AUTO_PROXY_URL;
  98. }
  99. //
  100. // set proxy name
  101. //
  102. PerConnOptionList.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
  103. PerConnOptionList.pOptions[1].Value.pszValue = pszProxyServer;
  104. //
  105. // set proxy override
  106. //
  107. PerConnOptionList.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
  108. PerConnOptionList.pOptions[2].Value.pszValue = pszProxyOverride;
  109. //
  110. // set auto config URL
  111. //
  112. PerConnOptionList.pOptions[3].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL;
  113. PerConnOptionList.pOptions[3].Value.pszValue = pszAutoConfigScript;
  114. //
  115. // tell wininet
  116. //
  117. if (!g_pfnInternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &PerConnOptionList, dwSize))
  118. {
  119. hr = HRESULT_FROM_WIN32(GetLastError());
  120. }
  121. CmFree(PerConnOptionList.pOptions);
  122. return hr;
  123. }
  124. //+----------------------------------------------------------------------------
  125. //
  126. // Function: SetIE4ProxySettings
  127. //
  128. // Synopsis: This function sets the IE4 proxy settings (global to a machine) using
  129. // the given connection, enabled value, proxy server, and override
  130. // settings.
  131. //
  132. // Arguments: LPSTR pszConnection - ignored (exists to have same prototype as IE5 version)
  133. // BOOL bManualProxy - whether the manual proxy is enabled or not
  134. // BOOL bAutomaticProxy - ignored (exists to have same prototype as IE5 version)
  135. // BOOL bAutoConfigScript - ignored (exists to have same prototype as IE5 version)
  136. // LPSTR pszProxyServer - proxy server name in the proxyserver:port format
  137. // LPSTR pszProxyOverride - a semi-colon seperated list of
  138. // realms to bypass the proxy for
  139. // LPSTR pszAutoConfigScript - ignored (exists to have same prototype as IE5 version)
  140. //
  141. // Returns: HRESULT - Standard COM return codes
  142. //
  143. // History: quintinb Created 10/27/99
  144. //
  145. //+----------------------------------------------------------------------------
  146. HRESULT SetIE4ProxySettings(LPSTR pszConnection, BOOL bManualProxy, BOOL bAutomaticProxy, BOOL bAutoConfigScript,
  147. LPSTR pszProxyServer, LPSTR pszProxyOverride, LPSTR pszAutoConfigScript)
  148. {
  149. //
  150. // Check Inputs, note that we don't allow the strings to be NULL but they could
  151. // be empty. Also note that pszConnection is ignored because IE4 proxy settings are global.
  152. //
  153. if ((NULL == pszProxyServer) || (NULL == pszProxyOverride))
  154. {
  155. return E_INVALIDARG;
  156. }
  157. DWORD dwTemp;
  158. HKEY hKey = NULL;
  159. HRESULT hr = S_OK;
  160. //
  161. // Now Create/Open the Internet Settings key
  162. //
  163. LONG lResult = RegCreateKeyEx(HKEY_CURRENT_USER, c_pszInternetSettingsPath, 0, NULL,
  164. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwTemp);
  165. hr = HRESULT_FROM_WIN32(lResult);
  166. if (SUCCEEDED(hr))
  167. {
  168. //
  169. // Set the proxy values
  170. //
  171. dwTemp = bManualProxy ? 1 : 0; // use a true 1 or 0 value.
  172. lResult = RegSetValueEx(hKey, c_pszProxyEnable, 0, REG_DWORD, (CONST BYTE*)&dwTemp, sizeof(DWORD));
  173. hr = HRESULT_FROM_WIN32(lResult);
  174. if (SUCCEEDED(hr))
  175. {
  176. lResult = RegSetValueEx(hKey, c_pszProxyServer, 0, REG_SZ, (CONST BYTE*)pszProxyServer, lstrlen(pszProxyServer)+1);
  177. hr = HRESULT_FROM_WIN32(lResult);
  178. if (SUCCEEDED(hr))
  179. {
  180. lResult = RegSetValueEx(hKey, c_pszProxyOverride, 0, REG_SZ, (CONST BYTE*)pszProxyOverride, lstrlen(pszProxyOverride)+1);
  181. hr = HRESULT_FROM_WIN32(lResult);
  182. }
  183. }
  184. (VOID)RegCloseKey(hKey);
  185. }
  186. return hr;
  187. }
  188. //+----------------------------------------------------------------------------
  189. //
  190. // Function: GetIE5ProxySettings
  191. //
  192. // Synopsis: Gets the IE5, per connection, proxy settings for the given connection.
  193. // Please note that the strings allocated for the proxy server and the
  194. // proxy override values must be freed by the caller.
  195. //
  196. // Arguments: LPSTR pszConnection - connection name to get the proxy settings for
  197. // LPBOOL pbManualProxy - bool pointer to hold whether the manual
  198. // proxy is enabled or not
  199. // LPBOOL pbAutomaticProxy - bool pointer to hold whether automatic
  200. // proxy detection is enabled or not
  201. // LPBOOL pbAutoConfigScript - bool pointer to hold whether an auto
  202. // config script should be used or not
  203. // LPSTR* ppszProxyServer - string pointer to hold the retrieved
  204. // proxy server string
  205. // LPSTR* ppszProxyOverride - string pointer to hold the retrieved
  206. // proxy server string
  207. // LPSTR* ppszAutoConfigScript - string pointer to hold the retrieved
  208. // auto config script
  209. //
  210. // Returns: HRESULT - Standard COM return codes
  211. //
  212. // History: quintinb Created 10/27/99
  213. //
  214. //+----------------------------------------------------------------------------
  215. HRESULT GetIE5ProxySettings(LPSTR pszConnection, LPBOOL pbManualProxy, LPBOOL pbAutomaticProxy, LPBOOL pbAutoConfigScript,
  216. LPSTR* ppszProxyServer, LPSTR* ppszProxyOverride, LPSTR* ppszAutoConfigScript)
  217. {
  218. //
  219. // Check Inputs, note that pszConnection can be NULL. It will set the LAN connection in that case.
  220. //
  221. if ((NULL == pbManualProxy) || (NULL == pbAutomaticProxy) || (NULL == pbAutoConfigScript) ||
  222. (NULL == ppszProxyServer) || (NULL == ppszProxyOverride) || (NULL == ppszAutoConfigScript) ||
  223. (NULL == g_pfnInternetQueryOption))
  224. {
  225. return E_INVALIDARG;
  226. }
  227. //
  228. // Zero the output params
  229. //
  230. *pbManualProxy = FALSE;
  231. *pbAutomaticProxy = FALSE;
  232. *pbAutoConfigScript = FALSE;
  233. *ppszProxyServer = CmStrCpyAlloc(c_pszEmpty);
  234. *ppszProxyOverride = CmStrCpyAlloc(c_pszEmpty);
  235. *ppszAutoConfigScript = CmStrCpyAlloc(c_pszEmpty);
  236. //
  237. // Setup the Option List Struct
  238. //
  239. HRESULT hr = S_OK;
  240. INTERNET_PER_CONN_OPTION_LIST PerConnOptionList;
  241. PerConnOptionList.dwSize = sizeof(PerConnOptionList);
  242. PerConnOptionList.pszConnection = pszConnection;
  243. PerConnOptionList.dwOptionError = 0;
  244. PerConnOptionList.dwOptionCount = 4;
  245. PerConnOptionList.pOptions = (INTERNET_PER_CONN_OPTION*)CmMalloc(4*sizeof(INTERNET_PER_CONN_OPTION));
  246. if(NULL == PerConnOptionList.pOptions)
  247. {
  248. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  249. }
  250. //
  251. // set flags we want info about
  252. //
  253. PerConnOptionList.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
  254. PerConnOptionList.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
  255. PerConnOptionList.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
  256. PerConnOptionList.pOptions[3].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL;
  257. DWORD dwSize = sizeof(PerConnOptionList);
  258. //
  259. // Get the Options
  260. //
  261. if (!g_pfnInternetQueryOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &PerConnOptionList, &dwSize))
  262. {
  263. hr = HRESULT_FROM_WIN32(GetLastError());
  264. }
  265. if (SUCCEEDED(hr))
  266. {
  267. //
  268. // Parse the returned options to find the options we are interested in
  269. //
  270. for (DWORD dwIndex=0; dwIndex < PerConnOptionList.dwOptionCount; dwIndex++)
  271. {
  272. switch(PerConnOptionList.pOptions[dwIndex].dwOption)
  273. {
  274. case INTERNET_PER_CONN_FLAGS:
  275. if (PROXY_TYPE_PROXY & PerConnOptionList.pOptions[dwIndex].Value.dwValue)
  276. {
  277. *pbManualProxy = TRUE;
  278. }
  279. if (PROXY_TYPE_AUTO_PROXY_URL & PerConnOptionList.pOptions[dwIndex].Value.dwValue)
  280. {
  281. *pbAutoConfigScript = TRUE;
  282. }
  283. if (PROXY_TYPE_AUTO_DETECT & PerConnOptionList.pOptions[dwIndex].Value.dwValue)
  284. {
  285. *pbAutomaticProxy = TRUE;
  286. }
  287. break;
  288. case INTERNET_PER_CONN_PROXY_SERVER:
  289. if (NULL != PerConnOptionList.pOptions[dwIndex].Value.pszValue)
  290. {
  291. CmFree(*ppszProxyServer);
  292. *ppszProxyServer = CmStrCpyAlloc(PerConnOptionList.pOptions[dwIndex].Value.pszValue);
  293. LocalFree(PerConnOptionList.pOptions[dwIndex].Value.pszValue);
  294. }
  295. break;
  296. case INTERNET_PER_CONN_PROXY_BYPASS:
  297. if (NULL != PerConnOptionList.pOptions[dwIndex].Value.pszValue)
  298. {
  299. CmFree(*ppszProxyOverride);
  300. *ppszProxyOverride = CmStrCpyAlloc(PerConnOptionList.pOptions[dwIndex].Value.pszValue);
  301. LocalFree(PerConnOptionList.pOptions[dwIndex].Value.pszValue);
  302. }
  303. break;
  304. case INTERNET_PER_CONN_AUTOCONFIG_URL:
  305. if (NULL != PerConnOptionList.pOptions[dwIndex].Value.pszValue)
  306. {
  307. CmFree(*ppszAutoConfigScript);
  308. *ppszAutoConfigScript = CmStrCpyAlloc(PerConnOptionList.pOptions[dwIndex].Value.pszValue);
  309. LocalFree(PerConnOptionList.pOptions[dwIndex].Value.pszValue);
  310. }
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. CmFree(PerConnOptionList.pOptions);
  317. }
  318. return hr;
  319. }
  320. //+----------------------------------------------------------------------------
  321. //
  322. // Function: GetIE4ProxySettings
  323. //
  324. // Synopsis: Gets the IE4, per machine, proxy settings.
  325. // Please note that the strings allocated for the proxy server and the
  326. // proxy override values must be freed by the caller.
  327. //
  328. // Arguments: LPSTR pszConnection - ignored (exists for prototype consistency with the IE5 version)
  329. // LPBOOL pbManualProxy - bool pointer to hold whether the manual
  330. // proxy is enabled or not
  331. // LPBOOL pbAutomaticProxy - ignored (not supported by IE4)
  332. // LPBOOL pbAutoConfigScript - ignored (not supported by IE4)
  333. // LPSTR* ppszProxyServer - string pointer to hold the retrieved
  334. // proxy server string
  335. // LPSTR* ppszProxyOverride - string pointer to hold the retrieved
  336. // proxy server string
  337. // LPSTR* ppszAutoConfigScript - ignored (not supported by IE4)
  338. //
  339. // Returns: HRESULT - Standard COM return codes
  340. //
  341. // History: quintinb Created 10/27/99
  342. //
  343. //+----------------------------------------------------------------------------
  344. HRESULT GetIE4ProxySettings(LPSTR pszConnection, LPBOOL pbManualProxy, LPBOOL pbAutomaticProxy, LPBOOL pbAutoConfigScript,
  345. LPSTR* ppszProxyServer, LPSTR* ppszProxyOverride, LPSTR* ppszAutoConfigScript)
  346. {
  347. //
  348. // Check Inputs, note that we don't allow the pointers to be NULL but they could
  349. // be empty. Also note that pszConnection is ignored because IE4 proxy settings are global.
  350. //
  351. if ((NULL == pbManualProxy) || (NULL == ppszProxyServer) || (NULL == ppszProxyOverride))
  352. {
  353. return E_INVALIDARG;
  354. }
  355. DWORD dwTemp;
  356. DWORD dwSize;
  357. DWORD dwType;
  358. HKEY hKey = NULL;
  359. HRESULT hr = S_OK;
  360. //
  361. // Zero the output params
  362. //
  363. *pbManualProxy = FALSE;
  364. *pbAutomaticProxy = FALSE;
  365. *pbAutoConfigScript = FALSE;
  366. *ppszProxyServer = CmStrCpyAlloc(c_pszEmpty);
  367. *ppszProxyOverride = CmStrCpyAlloc(c_pszEmpty);
  368. *ppszAutoConfigScript = CmStrCpyAlloc(c_pszEmpty);
  369. //
  370. // Now Create/Open the Internet Settings key
  371. //
  372. LONG lResult = RegOpenKeyEx(HKEY_CURRENT_USER, c_pszInternetSettingsPath, 0, KEY_READ, &hKey);
  373. hr = HRESULT_FROM_WIN32(lResult);
  374. if (SUCCEEDED(hr))
  375. {
  376. //
  377. // get whether the proxy is enabled or not
  378. //
  379. dwSize = sizeof(DWORD);
  380. lResult = RegQueryValueEx(hKey, c_pszProxyEnable, 0, &dwType, (LPBYTE)pbManualProxy, &dwSize);
  381. hr = HRESULT_FROM_WIN32(lResult);
  382. if (SUCCEEDED(hr))
  383. {
  384. //
  385. // get the proxy server value
  386. //
  387. lResult = ERROR_INSUFFICIENT_BUFFER;
  388. dwSize = MAX_PATH;
  389. do
  390. {
  391. //
  392. // Alloc a Buffer
  393. //
  394. CmFree(*ppszProxyServer);
  395. *ppszProxyServer = (CHAR*)CmMalloc(dwSize);
  396. if (*ppszProxyServer)
  397. {
  398. lResult = RegQueryValueEx(hKey, c_pszProxyServer, 0, &dwType,
  399. (LPBYTE)*ppszProxyServer, &dwSize);
  400. }
  401. else
  402. {
  403. lResult = ERROR_NOT_ENOUGH_MEMORY;
  404. }
  405. hr = HRESULT_FROM_WIN32(lResult);
  406. dwSize = 2*dwSize;
  407. } while ((ERROR_INSUFFICIENT_BUFFER == lResult) && (dwSize < 1024*1024));
  408. if (SUCCEEDED(hr))
  409. {
  410. //
  411. // get the proxy override value
  412. //
  413. lResult = ERROR_INSUFFICIENT_BUFFER;
  414. dwSize = MAX_PATH;
  415. do
  416. {
  417. //
  418. // Alloc a Buffer
  419. //
  420. CmFree(*ppszProxyOverride);
  421. *ppszProxyOverride = (CHAR*)CmMalloc(dwSize);
  422. if (*ppszProxyOverride)
  423. {
  424. lResult = RegQueryValueEx(hKey, c_pszProxyOverride, 0, &dwType,
  425. (LPBYTE)*ppszProxyOverride, &dwSize);
  426. }
  427. else
  428. {
  429. lResult = ERROR_NOT_ENOUGH_MEMORY;
  430. }
  431. hr = HRESULT_FROM_WIN32(lResult);
  432. dwSize = 2*dwSize;
  433. } while ((ERROR_INSUFFICIENT_BUFFER == lResult) && (dwSize < 1024*1024));
  434. }
  435. }
  436. }
  437. else
  438. {
  439. if (ERROR_FILE_NOT_FOUND == lResult)
  440. {
  441. //
  442. // No Proxy settings to get.
  443. //
  444. hr = S_FALSE;
  445. }
  446. }
  447. if (hKey)
  448. {
  449. (VOID)RegCloseKey(hKey);
  450. }
  451. return hr;
  452. }
  453. //+----------------------------------------------------------------------------
  454. //
  455. // Function: ReadProxySettingsFromFile
  456. //
  457. // Synopsis: Reads the proxy settings from the given proxy file and stores them
  458. // in the provided pointers. Please note that the buffers allocated
  459. // by GetString and stored in ppszProxyOverride, ppszProxyServer, and
  460. // ppszAutoConfigScript must be freed by the caller. Please see the above
  461. // format guide for specifics.
  462. //
  463. // Arguments: LPCSTR pszSourceFile - file to read the proxy settings from.
  464. // LPBOOL pbManualProxy - determines if the manual proxy is enabled or not
  465. // LPBOOL pbAutomaticProxy - determines if automatic proxy detection is enabled or not
  466. // LPBOOL pbAutoConfigScript - determines if an automatic config script should be used
  467. // LPSTR* ppszProxyServer - string pointer to hold the Proxy server value
  468. // (in server:port format)
  469. // LPSTR* ppszProxyOverride - string pointer to hold the Proxy override values
  470. // (a semi-colon seperated list)
  471. // LPSTR* ppszAutoConfigScript - URL for an automatic config script
  472. // LPBOOL pbUseVpnName - whether the alternate connectoid name should
  473. // be used (the VPN connectoid name)
  474. //
  475. // Returns: BOOL - TRUE if the settings were successfully read
  476. //
  477. // History: quintinb Created 10/27/99
  478. //
  479. //+----------------------------------------------------------------------------
  480. BOOL ReadProxySettingsFromFile(LPCSTR pszSourceFile, LPBOOL pbManualProxy, LPBOOL pbAutomaticProxy, LPBOOL pbAutoConfigScript,
  481. LPSTR* ppszProxyServer, LPSTR* ppszProxyOverride, LPSTR* ppszAutoConfigScript, LPBOOL pbUseVpnName)
  482. {
  483. //
  484. // Check input params
  485. //
  486. if ((NULL == ppszProxyOverride) || (NULL == ppszProxyServer) || (NULL == ppszAutoConfigScript) ||
  487. (NULL == pbAutomaticProxy) || (NULL == pbAutoConfigScript) || (NULL == pbManualProxy) ||
  488. (NULL == pbUseVpnName) || (NULL == pszSourceFile) || ('\0' == pszSourceFile[0]))
  489. {
  490. return FALSE;
  491. }
  492. //
  493. // Get the Manual proxy settings
  494. //
  495. *pbManualProxy = GetPrivateProfileInt(c_pszManualProxySection, c_pszProxyEnable, 0, pszSourceFile);
  496. GetString(c_pszManualProxySection, c_pszProxyServer, ppszProxyServer, pszSourceFile);
  497. if (NULL == *ppszProxyServer)
  498. {
  499. return FALSE;
  500. }
  501. GetString(c_pszManualProxySection, c_pszProxyOverride, ppszProxyOverride, pszSourceFile);
  502. if (NULL == *ppszProxyOverride)
  503. {
  504. return FALSE;
  505. }
  506. //
  507. // If this is a backup file, we will have the UseVpnName flag to tell us which connectoid name
  508. // is appropriate. Lets look it up. Note that we default to using the standard connectoid.
  509. //
  510. *pbUseVpnName = GetPrivateProfileInt(c_pszManualProxySection, c_pszUseVpnName, 0, pszSourceFile);
  511. //
  512. // Get the Auto proxy settings
  513. //
  514. *pbAutomaticProxy = GetPrivateProfileInt(c_pszAutoProxySection, c_pszAutoProxyEnable, 0, pszSourceFile); //"Automatically detect settings" checkbox
  515. *pbAutoConfigScript = GetPrivateProfileInt(c_pszAutoProxySection, c_pszAutoConfigScriptEnable, 0, pszSourceFile);//"Use automatic configuration script" checkbox
  516. GetString(c_pszAutoProxySection, c_pszAutoConfigScript, ppszAutoConfigScript, pszSourceFile);
  517. return TRUE;
  518. }
  519. //+----------------------------------------------------------------------------
  520. //
  521. // Function: WriteProxySettingsToFile
  522. //
  523. // Synopsis: Writes the specified settings to the given backup proxy filename.
  524. // Please see the above format guide for specifics.
  525. //
  526. // Arguments: LPCSTR pszBackupFile - backup file to write the current settings to
  527. // BOOL bManualProxy -- bool to tell if the manual proxy is enabled.
  528. // BOOL bAutomaticProxy -- bool to tell if auto proxy detection is enabled.
  529. // BOOL bAutoConfigScript -- bool to tell if an auto config
  530. // script should be used.
  531. // LPSTR pszProxyServer - proxy server string in server:port format
  532. // LPSTR pszProxyOverride - semi-colon seperated list of realms for
  533. // which the proxy server should be bypassed.
  534. // BOOL bUseVpnName - value to write to the UseVpnName file, see format doc above.
  535. //
  536. // Returns: BOOL - TRUE if the values were written successfully
  537. //
  538. // History: quintinb Created 10/27/99
  539. //
  540. //+----------------------------------------------------------------------------
  541. BOOL WriteProxySettingsToFile(LPCSTR pszBackupFile, BOOL bManualProxy, BOOL bAutomaticProxy, BOOL bAutoConfigScript,
  542. LPSTR pszProxyServer, LPSTR pszProxyOverride, LPSTR pszAutoConfigScript, BOOL bUseVpnName)
  543. {
  544. //
  545. // Check inputs
  546. //
  547. if ((NULL == pszBackupFile) || ('\0' == pszBackupFile[0]) || (NULL == pszProxyServer) ||
  548. (NULL == pszProxyOverride) || (NULL == pszAutoConfigScript))
  549. {
  550. return FALSE;
  551. }
  552. BOOL bReturn = TRUE;
  553. CHAR szTemp[MAX_PATH];
  554. //
  555. // Write the Manual Proxy Settings
  556. //
  557. wsprintf(szTemp, "%d", bManualProxy);
  558. if (!WritePrivateProfileString(c_pszManualProxySection, c_pszProxyEnable, szTemp, pszBackupFile))
  559. {
  560. CMTRACE1("CmProxy -- WriteProxySettingsToFile failed, GLE %d", GetLastError());
  561. bReturn = FALSE;
  562. }
  563. if (!WritePrivateProfileString(c_pszManualProxySection, c_pszProxyServer, pszProxyServer, pszBackupFile))
  564. {
  565. CMTRACE1("CmProxy -- WriteProxySettingsToFile failed, GLE %d", GetLastError());
  566. bReturn = FALSE;
  567. }
  568. if (!WritePrivateProfileString(c_pszManualProxySection, c_pszProxyOverride, pszProxyOverride, pszBackupFile))
  569. {
  570. CMTRACE1("CmProxy -- WriteProxySettingsToFile failed, GLE %d", GetLastError());
  571. bReturn = FALSE;
  572. }
  573. wsprintf(szTemp, "%d", bUseVpnName);
  574. if (!WritePrivateProfileString(c_pszManualProxySection, c_pszUseVpnName, szTemp, pszBackupFile))
  575. {
  576. CMTRACE1("CmProxy -- WriteProxySettingsToFile failed, GLE %d", GetLastError());
  577. bReturn = FALSE;
  578. }
  579. //
  580. // Write the Automatic Proxy Settings
  581. //
  582. wsprintf(szTemp, "%d", bAutomaticProxy);
  583. if (!WritePrivateProfileString(c_pszAutoProxySection, c_pszAutoProxyEnable, szTemp, pszBackupFile))
  584. {
  585. CMTRACE1("CmProxy -- WriteProxySettingsToFile failed, GLE %d", GetLastError());
  586. bReturn = FALSE;
  587. }
  588. wsprintf(szTemp, "%d", bAutoConfigScript);
  589. if (!WritePrivateProfileString(c_pszAutoProxySection, c_pszAutoConfigScriptEnable, szTemp, pszBackupFile))
  590. {
  591. CMTRACE1("CmProxy -- WriteProxySettingsToFile failed, GLE %d", GetLastError());
  592. bReturn = FALSE;
  593. }
  594. if (!WritePrivateProfileString(c_pszAutoProxySection, c_pszAutoConfigScript, pszAutoConfigScript, pszBackupFile))
  595. {
  596. CMTRACE1("CmProxy -- WriteProxySettingsToFile failed, GLE %d", GetLastError());
  597. bReturn = FALSE;
  598. }
  599. return bReturn;
  600. }
  601. //+----------------------------------------------------------------------------
  602. //
  603. // Function: SetProxy
  604. //
  605. // Synopsis: Proxy entry point for setting the IE4 and IE5 style proxies. Since
  606. // this is a Connection Manager connect action it uses the CM connect
  607. // action format (see CMAK docs for more info). Thus the parameters
  608. // to the dll are passed via a string which contains parameters (see the
  609. // cmproxy spec for a list of the parameter values).
  610. //
  611. // Arguments: HWND hWnd - Window handle of caller
  612. // HINSTANCE hInst - Instance handle of caller
  613. // LPSTR pszArgs - Argument string
  614. // int nShow - Unused
  615. //
  616. // Returns: DWORD WINAPI - Error code
  617. //
  618. // History: quintinb Created 10/27/99
  619. //
  620. //+----------------------------------------------------------------------------
  621. HRESULT WINAPI SetProxy(HWND hWnd, HINSTANCE hInst, LPSTR pszArgs, int nShow)
  622. {
  623. //
  624. // First figure out if we have IE4 or IE5 available.
  625. //
  626. typedef HRESULT (WINAPI *pfnSetProxySettings)(LPSTR, BOOL, BOOL, BOOL, LPSTR, LPSTR, LPSTR);
  627. typedef HRESULT (WINAPI *pfnGetProxySettings)(LPSTR, LPBOOL, LPBOOL, LPBOOL, LPSTR*, LPSTR*, LPSTR*);
  628. pfnSetProxySettings SetProxySettings = NULL;
  629. pfnGetProxySettings GetProxySettings = NULL;
  630. BOOL bIE5 = FALSE;
  631. BOOL bManualProxy;
  632. BOOL bAutomaticProxy;
  633. BOOL bAutoConfigScript;
  634. BOOL bUseVpnName = FALSE;
  635. DLLVERSIONINFO VersionInfo;
  636. HINSTANCE hWinInet = NULL;
  637. LPSTR pszProxyServer = NULL;
  638. LPSTR pszProxyOverride = NULL;
  639. LPSTR pszAutoConfigScript = NULL;
  640. LPSTR pszSourceFile = NULL;
  641. LPSTR pszBackupFile = NULL;
  642. LPSTR pszConnectionName = NULL;
  643. LPSTR pszProfileDirPath = NULL;
  644. LPSTR pszAltName = NULL;
  645. LPSTR* CmArgV = NULL;
  646. if (SUCCEEDED(GetBrowserVersion(&VersionInfo)))
  647. {
  648. if (5 <= VersionInfo.dwMajorVersion)
  649. {
  650. //
  651. // Set the function pointers to use the IE5 versions of the functions
  652. //
  653. SetProxySettings = SetIE5ProxySettings;
  654. GetProxySettings = GetIE5ProxySettings;
  655. bIE5 = TRUE;
  656. }
  657. else if ((4 == VersionInfo.dwMajorVersion) &&
  658. ((71 == VersionInfo.dwMinorVersion) || (72 == VersionInfo.dwMinorVersion)))
  659. {
  660. //
  661. // Use the IE4 version of the proxy functions
  662. //
  663. SetProxySettings = SetIE4ProxySettings;
  664. GetProxySettings = GetIE4ProxySettings;
  665. }
  666. else
  667. {
  668. //
  669. // We don't work with IE versions less than 4 so lets return right here
  670. // without setting anything.
  671. //
  672. CMTRACE("CMPROXY--Unable to set the proxy settings because of insufficient IE version.");
  673. return TRUE;
  674. }
  675. //
  676. // If we have IE5, then we need to load wininet.dll.
  677. //
  678. if (bIE5)
  679. {
  680. hWinInet = LoadLibrary("wininet.dll");
  681. if (hWinInet)
  682. {
  683. //
  684. // Okay, lets get the procedure addresses for InternetSetOption and InternetQueryOption
  685. //
  686. g_pfnInternetQueryOption = (pfnInternetQueryOptionSpec)GetProcAddress(hWinInet, "InternetQueryOptionA");
  687. g_pfnInternetSetOption = (pfnInternetSetOptionSpec)GetProcAddress(hWinInet, "InternetSetOptionA");
  688. if ((NULL == g_pfnInternetQueryOption) || (NULL == g_pfnInternetSetOption))
  689. {
  690. FreeLibrary(hWinInet);
  691. return FALSE;
  692. }
  693. }
  694. }
  695. //
  696. // Parse out the command line parameters
  697. //
  698. // command line is of the form: /profile %PROFILE% /DialRasEntry %DIALRASENTRY% /TunnelRasEntry %TUNNELRASENTRYNAME% /source_filename Proxy.txt /backup_filename backup.txt
  699. CmArgV = GetCmArgV(pszArgs);
  700. int i = 0;
  701. if (!CmArgV)
  702. {
  703. goto exit;
  704. }
  705. while (CmArgV[i])
  706. {
  707. if (0 == SafeCompareString(CmArgV[i], "/source_filename") && CmArgV[i+1])
  708. {
  709. pszSourceFile = CmStrCpyAlloc(CmArgV[i+1]);
  710. i = i+2;
  711. }
  712. else if (0 == SafeCompareString(CmArgV[i], "/backup_filename") && CmArgV[i+1])
  713. {
  714. pszBackupFile = CmStrCpyAlloc(CmArgV[i+1]);
  715. i = i+2;
  716. }
  717. else if (0 == SafeCompareString(CmArgV[i], "/DialRasEntry") && CmArgV[i+1])
  718. {
  719. pszConnectionName = (CmArgV[i+1]);
  720. i = i+2;
  721. }
  722. else if (0 == SafeCompareString(CmArgV[i], "/TunnelRasEntry") && CmArgV[i+1])
  723. {
  724. pszAltName = (CmArgV[i+1]);
  725. i = i+2;
  726. }
  727. else if (0 == SafeCompareString(CmArgV[i], "/profile") && CmArgV[i+1])
  728. {
  729. pszProfileDirPath = (CmArgV[i+1]);
  730. i = i+2;
  731. }
  732. else
  733. {
  734. //
  735. // Unknown option. Lets trace it out and try to continue. We will do param
  736. // verification next so if we don't have enough info to operate correctly we will work there.
  737. //
  738. CMTRACE1("Unknown option: %s", CmArgV[i]);
  739. i++;
  740. }
  741. }
  742. //
  743. // Verify that we have at least a source file and a name.
  744. //
  745. if ((pszSourceFile) && (pszConnectionName))
  746. {
  747. //
  748. // Lets parse the cmp path into the profile dir and append it to the filename.
  749. //
  750. if (pszProfileDirPath)
  751. {
  752. LPSTR pszTemp = CmStrrchr(pszProfileDirPath, '.');
  753. if (pszTemp)
  754. {
  755. *pszTemp = '\\';
  756. *(pszTemp+1) = '\0';
  757. pszTemp = CmStrCpyAlloc(pszProfileDirPath);
  758. CmStrCatAlloc(&pszTemp, pszSourceFile);
  759. CmFree(pszSourceFile);
  760. pszSourceFile = pszTemp;
  761. if (pszBackupFile)
  762. {
  763. pszTemp = CmStrCpyAlloc(pszProfileDirPath);
  764. CmStrCatAlloc(&pszTemp, pszBackupFile);
  765. CmFree(pszBackupFile);
  766. pszBackupFile = pszTemp;
  767. }
  768. }
  769. }
  770. //
  771. // If we have a direct connection or if this is a double dial connection on Win9x then we
  772. // will want to use pszAltName instead of pszConnectionName. This is because in the Win9x case,
  773. // the tunnel connectoid has "Tunnel" appended to it since all of the connectoids are stored in
  774. // the registry and we cannot have two connectoids with the same name. If this is a direct
  775. // connection this is also important because pszConnectoid will come through as "NULL" and the
  776. // Tunnel connectoid name will be the important one. Also, in these cases we need to set
  777. // bWriteOutUseVpnName to TRUE in order to write this flag out for the disconnect action.
  778. //
  779. BOOL bWriteOutUseVpnName = FALSE;
  780. if (pszConnectionName && pszAltName && bIE5)
  781. {
  782. if ((0 == SafeCompareString(pszConnectionName, TEXT("NULL"))) || OS_W9X)
  783. {
  784. //
  785. // Then we have a direct connection or a double dial connection on 9x
  786. //
  787. if (UseVpnName(pszAltName))
  788. {
  789. pszConnectionName = pszAltName;
  790. pszAltName = NULL;
  791. bWriteOutUseVpnName = TRUE;
  792. }
  793. }
  794. }
  795. //
  796. // If we have a backup filename specified then we need to read the current Proxy settings
  797. // and save them out to the given filename.
  798. //
  799. if (NULL != pszBackupFile)
  800. {
  801. if (SUCCEEDED(GetProxySettings(pszConnectionName, &bManualProxy, &bAutomaticProxy, &bAutoConfigScript,
  802. &pszProxyServer, &pszProxyOverride, &pszAutoConfigScript)))
  803. {
  804. BOOL bSuccess = WriteProxySettingsToFile(pszBackupFile, bManualProxy, bAutomaticProxy, bAutoConfigScript,
  805. pszProxyServer, pszProxyOverride, pszAutoConfigScript,
  806. bWriteOutUseVpnName);
  807. if (!bSuccess)
  808. {
  809. CMTRACE("Unable to save settings to backup file, exiting!");
  810. goto exit;
  811. }
  812. CmFree(pszProxyServer); pszProxyServer = NULL;
  813. CmFree(pszProxyOverride); pszProxyOverride = NULL;
  814. CmFree(pszAutoConfigScript); pszAutoConfigScript = NULL;
  815. }
  816. }
  817. //
  818. // Now we need to read the proxy settings to apply out of the given file
  819. //
  820. if (ReadProxySettingsFromFile(pszSourceFile, &bManualProxy, &bAutomaticProxy, &bAutoConfigScript, &pszProxyServer,
  821. &pszProxyOverride, &pszAutoConfigScript, &bUseVpnName))
  822. {
  823. //
  824. // Finally write the proxy settings.
  825. //
  826. if (SUCCEEDED(SetProxySettings(pszConnectionName, bManualProxy, bAutomaticProxy, bAutoConfigScript,
  827. pszProxyServer, pszProxyOverride, pszAutoConfigScript)))
  828. {
  829. CMTRACE1("CmProxy -- Set proxy settings successfully for %s.", pszConnectionName);
  830. }
  831. }
  832. }
  833. }
  834. exit:
  835. CmFree(pszProxyServer);
  836. CmFree(pszProxyOverride);
  837. CmFree(pszAutoConfigScript);
  838. CmFree(pszSourceFile);
  839. CmFree(pszBackupFile);
  840. CmFree(CmArgV);
  841. if (hWinInet)
  842. {
  843. FreeLibrary(hWinInet);
  844. }
  845. //
  846. // Always return S_OK because failing to set the proxy shouldn't stop the connection
  847. // process.
  848. //
  849. return S_OK;
  850. }