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.

975 lines
38 KiB

  1. #include <windows.h>
  2. #include "regstr.h"
  3. #include "sdsutils.h"
  4. //----------------------------------------------------------------
  5. // defines used to form the ProxyServer values
  6. //----------------------------------------------------------------
  7. #define TYPE_HTTP 1
  8. #define TYPE_FTP 2
  9. #define TYPE_GOPHER 3
  10. #define TYPE_HTTPS 4
  11. #define TYPE_SOCKS 5
  12. #define MANUAL_PROXY 1
  13. #define AUTO_PROXY 2
  14. #define NO_PROXY 3
  15. #define MAX_STRING 1024
  16. // define keynames
  17. const char NS_HTTP_KeyName[] = "HTTP_Proxy";
  18. const char NS_HTTP_PortKeyName[] = "Http_ProxyPort";
  19. const char NS_FTP_KeyName[] = "FTP_Proxy";
  20. const char NS_FTP_PortKeyName[] = "Ftp_ProxyPort";
  21. const char NS_Gopher_KeyName[] = "Gopher_Proxy";
  22. const char NS_Gopher_PortKeyName[] = "Gopher_ProxyPort";
  23. const char NS_HTTPS_KeyName[] = "HTTPS_Proxy";
  24. const char NS_HTTPS_PortKeyName[] = "HTTPS_ProxyPort";
  25. const char NS_SOCKS_KeyName[] = "SOCKS_Server";
  26. const char NS_SOCKS_PortKeyName[] = "SOCKS_ServerPort";
  27. // the string below have to match the strings in the prefs.js file netscape is using for
  28. // it's settings. The parsing code needs them.
  29. const char c_gszNetworkProxyType[] = "network.proxy.type";
  30. const char c_gszNetworkProxyHttp[] = "network.proxy.http";
  31. const char c_gszNetworkProxyHttpPort[] = "network.proxy.http_port";
  32. const char c_gszNetworkProxyFtp[] = "network.proxy.ftp";
  33. const char c_gszNetworkProxyFtpPort[] = "network.proxy.ftp_port";
  34. const char c_gszNetworkProxyGopher[] = "network.proxy.gopher";
  35. const char c_gszNetworkProxyGopherPort[] = "network.proxy.gopher_port";
  36. const char c_gszNetworkProxySsl[] = "network.proxy.ssl";
  37. const char c_gszNetworkProxySslPort[] = "network.proxy.ssl_port";
  38. const char c_gszNetworkProxyNoProxyOn[] = "network.proxy.no_proxies_on";
  39. const char c_gszNetworkAutoProxy[] = "network.proxy.autoconfig_url";
  40. const char c_gszNSAutoConfigUrl[] = "Auto Config URL";
  41. // This are the string we append the proxy settings to for IE
  42. const char c_gszHTTP[] = "http=";
  43. const char c_gszFTP[] = "ftp=";
  44. const char c_gszGopher[] = "gopher=";
  45. const char c_gszHTTPS[] = "https=";
  46. const char c_gszSOCKS[] = "socks=";
  47. // This are the registry key/valuenames for IE
  48. const char c_gszIERegPath[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
  49. const char c_gszIEProxyKeyName[] = "ProxyServer";
  50. const char c_gszIEOverrideKeyName[] = "ProxyOverride";
  51. const char c_gszIEWEnableKeyName[] = "ProxyEnable";
  52. const char c_gszIEAutoConfigUrl[] = "AutoConfigURL";
  53. const char c_gszIE4Setup[] = "Software\\Microsoft\\IE Setup\\Setup";
  54. const char c_gszPre_DEFAULTBROWSER[] = "PreDefaultBrowser";
  55. const char c_gszNavigator3[] = "Navigator30";
  56. const char c_gszNavigator4[] = "Navigator40";
  57. const char c_gszInternetExplorer[] = "Internet Explorer";
  58. //-------------------------------------------------------------------------
  59. // function prototype
  60. //-------------------------------------------------------------------------
  61. BOOL GetNSProxyValue(char * szProxyValue, DWORD * pdwSize);
  62. BOOL RegStrValueEmpty(HKEY hTheKey, char * szPath, char * szKey);
  63. BOOL IsIEDefaultBrowser();
  64. void MyGetVersionFromFile(LPSTR lpszFilename, LPDWORD pdwMSVer, LPDWORD pdwLSVer, BOOL bVersion);
  65. BOOL ImportNetscapeProxy(void);
  66. void ImportNetscape4Proxy();
  67. BOOL GetNav4UserDir(LPSTR lpszDir);
  68. void ImportNav4Settings(LPSTR lpData, DWORD dwBytes);
  69. void AppendOneNav4Setting(LPSTR lpData, DWORD dwBytes, LPSTR lpProxyName, LPSTR lpProxyPort, LPSTR lpProxyType, LPSTR lpProxyValue);
  70. void AppendOneNav4Setting(LPSTR lpData, DWORD dwBytes, LPSTR lpProxyName, LPSTR lpProxyPort, LPSTR lpProxyType, LPSTR lpProxyValue);
  71. BOOL GetValueFormNav4(LPSTR lpData, DWORD dwBytes, LPSTR lpName, DWORD dwSize, LPSTR lpValue);
  72. void CheckPreDefBrowser( DWORD *pdwVerMS );
  73. LPSTR ConvertNetscapeProxyList(LPSTR pszBypassList);
  74. //-------------------------------------------------------------------------
  75. // functions
  76. //-------------------------------------------------------------------------
  77. void ImportNetscapeProxySettings( DWORD dwFlags )
  78. {
  79. DWORD dwVerMS = 0;
  80. if ( dwFlags & IMPTPROXY_CALLAFTIE4 )
  81. {
  82. CheckPreDefBrowser( &dwVerMS );
  83. }
  84. else if (!IsIEDefaultBrowser())
  85. {
  86. dwVerMS = GetNetScapeVersion();
  87. }
  88. // Only if we go a version number see what netscape we should migrate.
  89. // It could still be that neither netscape nor IE is the default browser
  90. if (dwVerMS != 0)
  91. {
  92. // If Netscape 4 is install over netscape 3 and then uninstalled
  93. // the apppath for netscape is empty, but netscape 3 is still working.
  94. if (dwVerMS < NS_NAVI4)
  95. ImportNetscapeProxy();
  96. else
  97. ImportNetscape4Proxy();
  98. }
  99. }
  100. void CheckPreDefBrowser( DWORD *pdwVerMS )
  101. {
  102. HKEY hKey;
  103. DWORD dwSize;
  104. char szBuf[MAX_PATH];
  105. if ( !pdwVerMS )
  106. return;
  107. *pdwVerMS = 0;
  108. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, c_gszIE4Setup, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  109. {
  110. dwSize = sizeof(szBuf);
  111. if ( RegQueryValueEx(hKey, c_gszPre_DEFAULTBROWSER, 0, NULL, (LPBYTE)szBuf, &dwSize) == ERROR_SUCCESS )
  112. {
  113. if ( !lstrcmpi( szBuf, c_gszNavigator4 ) )
  114. *pdwVerMS = NS_NAVI4;
  115. else if ( !lstrcmpi( szBuf, c_gszNavigator3 ) )
  116. *pdwVerMS = NS_NAVI3ORLESS;
  117. }
  118. RegCloseKey( hKey );
  119. }
  120. }
  121. DWORD GetNetScapeVersion()
  122. {
  123. // Determine which of the import code we should call
  124. char c_gszRegstrPathNetscape[] = REGSTR_PATH_APPPATHS "\\netscape.exe";
  125. HKEY hKey;
  126. DWORD dwSize;
  127. DWORD dwType;
  128. DWORD dwVerMS = 0;
  129. DWORD dwVerLS = 0;
  130. char szTmp[MAX_PATH];
  131. char *pTmp;
  132. char *pBrowser;
  133. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, c_gszRegstrPathNetscape, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  134. {
  135. dwSize = sizeof(szTmp);
  136. if ((RegQueryValueEx(hKey, NULL, 0, &dwType, (LPBYTE)szTmp, &dwSize) == ERROR_SUCCESS) &&
  137. (dwType == REG_SZ))
  138. {
  139. if (GetFileAttributes(szTmp) != 0xFFFFFFFF)
  140. {
  141. // File exists
  142. // Check the version
  143. MyGetVersionFromFile(szTmp, &dwVerMS, &dwVerLS, TRUE);
  144. }
  145. }
  146. RegCloseKey(hKey);
  147. }
  148. if (dwVerMS == 0)
  149. {
  150. // Assume the registry entry above does not exist or the file it pointed to does not exist.
  151. // GetVersionFromFile will retrun 0 if the file does not exist.
  152. if (RegOpenKeyEx(HKEY_CLASSES_ROOT, ".htm", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  153. {
  154. dwSize = sizeof(szTmp);
  155. if (RegQueryValueEx(hKey, NULL, 0, &dwType, (LPBYTE)szTmp, &dwSize) != ERROR_SUCCESS)
  156. *szTmp = '\0';
  157. RegCloseKey(hKey);
  158. if (*szTmp != '\0')
  159. {
  160. AddPath(szTmp,"shell\\open\\command");
  161. if (RegOpenKeyEx(HKEY_CLASSES_ROOT, szTmp, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  162. {
  163. dwSize = sizeof(szTmp);
  164. if (RegQueryValueEx(hKey, NULL, 0, &dwType, (LPBYTE)szTmp, &dwSize) == ERROR_SUCCESS)
  165. {
  166. // We have now the command line for the browser.
  167. pTmp = szTmp;
  168. if (*pTmp == '\"')
  169. {
  170. pTmp = CharNext(pTmp);
  171. pBrowser = pTmp;
  172. while ((*pTmp) && (*pTmp != '\"'))
  173. pTmp = CharNext(pTmp);
  174. }
  175. else
  176. {
  177. pBrowser = pTmp;
  178. while ((*pTmp) && (*pTmp != ' '))
  179. pTmp = CharNext(pTmp);
  180. }
  181. *pTmp = '\0';
  182. MyGetVersionFromFile(pBrowser, &dwVerMS, &dwVerLS, TRUE);
  183. }
  184. RegCloseKey(hKey);
  185. }
  186. }
  187. }
  188. }
  189. return dwVerMS;
  190. }
  191. /****************************************************\
  192. FUNCTION: ImportNetscapeProxy
  193. PARAMETERS:
  194. BOOL return - Error result. FALSE == ERROR
  195. DESCRIPTION:
  196. This function will check to see if IE's proxy
  197. value is set. If it is not set, and Netscape's
  198. proxy value is set, we will copy their value over
  199. to ours. We will also do this for the Proxy Override.
  200. \***************************************************/
  201. BOOL ImportNetscapeProxy(void)
  202. {
  203. DWORD dwRegType = 0;
  204. HKEY hIEKey = NULL;
  205. HKEY hNSKey = NULL;
  206. // NS keys
  207. //
  208. char *szNSRegPath = "Software\\Netscape\\Netscape Navigator\\Proxy Information";
  209. char *szNSRegPath2 = "Software\\Netscape\\Netscape Navigator\\Services";
  210. char *szNSOverrideKeyName = "No_Proxy";
  211. char *szNSEnableKeyName = "Proxy Type";
  212. char *pszNewOverride = NULL;
  213. char szCurrentProxy[1024];
  214. char szCurrentProxyOverride[1024];
  215. DWORD dwDataSize = sizeof(szCurrentProxy);
  216. DWORD dwProxyEnabled = 0;
  217. szCurrentProxy[0] = '\0';
  218. szCurrentProxyOverride[0] = '\0';
  219. // If Netscape does not have their proxy set to "Manual", then we will not
  220. // bother picking up their settings.
  221. if ((ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, szNSRegPath, 0, KEY_QUERY_VALUE, &hNSKey)) &&
  222. (NULL != hNSKey))
  223. {
  224. dwDataSize = sizeof(dwProxyEnabled);
  225. RegQueryValueEx(hNSKey, szNSEnableKeyName, NULL, &dwRegType, (LPBYTE)&dwProxyEnabled, &dwDataSize);
  226. RegCloseKey(hNSKey);
  227. hNSKey = NULL;
  228. }
  229. switch (dwProxyEnabled)
  230. {
  231. case MANUAL_PROXY:
  232. // We only want to create an IE proxy server value if an IE value does not exist and
  233. // a Netscape value does.
  234. if (//(TRUE == RegStrValueEmpty(HKEY_CURRENT_USER, (LPSTR)c_gszIERegPath, (LPSTR)c_gszIEProxyKeyName)) &&
  235. ((FALSE == RegStrValueEmpty(HKEY_CURRENT_USER, szNSRegPath, (LPSTR)NS_HTTP_KeyName)) ||
  236. (FALSE == RegStrValueEmpty(HKEY_CURRENT_USER, szNSRegPath, (LPSTR)NS_FTP_KeyName)) ||
  237. (FALSE == RegStrValueEmpty(HKEY_CURRENT_USER, szNSRegPath, (LPSTR)NS_Gopher_KeyName)) ||
  238. (FALSE == RegStrValueEmpty(HKEY_CURRENT_USER, szNSRegPath, (LPSTR)NS_HTTPS_KeyName)) ||
  239. (FALSE == RegStrValueEmpty(HKEY_CURRENT_USER, szNSRegPath2,(LPSTR)NS_SOCKS_KeyName)) ) )
  240. {
  241. dwDataSize = sizeof(szCurrentProxy);
  242. if (TRUE == GetNSProxyValue(szCurrentProxy, &dwDataSize))
  243. {
  244. // ASSERTSZ(NULL != szCurrentProxy, "GetNSProxyValue() was called and failed.");
  245. if ((NULL != szCurrentProxy) &&
  246. (ERROR_SUCCESS == RegCreateKeyEx(HKEY_CURRENT_USER, c_gszIERegPath, 0, NULL, REG_OPTION_NON_VOLATILE,
  247. KEY_SET_VALUE, NULL, &hIEKey, NULL)) &&
  248. (NULL != hIEKey))
  249. {
  250. // At this point, the NS value exists, the IE value does not, so we will import the NS value.
  251. RegSetValueEx(hIEKey, c_gszIEProxyKeyName, 0, REG_SZ, (unsigned char*)szCurrentProxy, lstrlen(szCurrentProxy)+1);
  252. // We also need to turn on the Proxy
  253. dwProxyEnabled = 1;
  254. RegSetValueEx(hIEKey, c_gszIEWEnableKeyName, 0, REG_BINARY, (unsigned char*)&dwProxyEnabled, sizeof(dwProxyEnabled));
  255. RegCloseKey(hIEKey);
  256. hIEKey = NULL;
  257. }
  258. }
  259. }
  260. // At this point, we want to import the Proxy Override value if it does
  261. // not exist for IE but does for NS.
  262. if (//(TRUE == RegStrValueEmpty(HKEY_CURRENT_USER, (LPSTR)c_gszIERegPath, (LPSTR)c_gszIEOverrideKeyName)) &&
  263. (FALSE == RegStrValueEmpty(HKEY_CURRENT_USER, szNSRegPath, szNSOverrideKeyName)))
  264. {
  265. if ((ERROR_SUCCESS == RegCreateKeyEx(HKEY_CURRENT_USER, c_gszIERegPath, 0, NULL, REG_OPTION_NON_VOLATILE,
  266. KEY_SET_VALUE, NULL, &hIEKey, NULL)) &&
  267. (NULL != hIEKey))
  268. {
  269. if ((ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, szNSRegPath, 0, KEY_QUERY_VALUE, &hNSKey)) &&
  270. (NULL != hNSKey))
  271. {
  272. dwDataSize = sizeof(szCurrentProxyOverride);
  273. if (ERROR_SUCCESS == RegQueryValueEx(hNSKey, szNSOverrideKeyName, NULL, &dwRegType, (LPBYTE)szCurrentProxyOverride, &dwDataSize))
  274. {
  275. if ((NULL != szCurrentProxyOverride) &&
  276. (REG_SZ == dwRegType) &&
  277. (0 != szCurrentProxyOverride[0]))
  278. {
  279. // At this point, the value exists, and it's invalid, so we need to change it.
  280. pszNewOverride = ConvertNetscapeProxyList(szCurrentProxyOverride);
  281. // Use the override list iff we successfully inserted '*' characters as appropriate.
  282. if (pszNewOverride)
  283. {
  284. RegSetValueEx(hIEKey, c_gszIEOverrideKeyName, 0, REG_SZ, (unsigned char*)pszNewOverride, lstrlen(pszNewOverride)+1);
  285. LocalFree(pszNewOverride); // This is the caller's responsibility.
  286. }
  287. else
  288. {
  289. RegSetValueEx(hIEKey, c_gszIEOverrideKeyName, 0, REG_SZ, (unsigned char*)szCurrentProxyOverride, lstrlen(szCurrentProxyOverride)+1);
  290. }
  291. }
  292. }
  293. RegCloseKey(hNSKey);
  294. hNSKey = NULL;
  295. }
  296. RegCloseKey(hIEKey);
  297. hIEKey = NULL;
  298. }
  299. }
  300. break;
  301. case AUTO_PROXY:
  302. if (RegOpenKeyEx(HKEY_CURRENT_USER, szNSRegPath, 0, KEY_QUERY_VALUE, &hNSKey) == ERROR_SUCCESS)
  303. {
  304. dwDataSize = sizeof(szCurrentProxy);
  305. if (RegQueryValueEx(hNSKey, c_gszNSAutoConfigUrl, NULL, NULL, (LPBYTE)szCurrentProxy, &dwDataSize) == ERROR_SUCCESS)
  306. {
  307. if ((*szCurrentProxy != '\0') &&
  308. (RegCreateKeyEx(HKEY_CURRENT_USER, c_gszIERegPath, 0, NULL, REG_OPTION_NON_VOLATILE,
  309. KEY_SET_VALUE, NULL, &hIEKey, NULL) == ERROR_SUCCESS))
  310. {
  311. // At this point, the value exists, and it's invalid, so we need to change it.
  312. RegSetValueEx(hIEKey, c_gszIEAutoConfigUrl, 0, REG_SZ, (LPBYTE)szCurrentProxy, lstrlen(szCurrentProxy)+1);
  313. RegCloseKey(hIEKey);
  314. }
  315. }
  316. RegCloseKey(hNSKey);
  317. }
  318. break;
  319. case NO_PROXY:
  320. // Nothing to do!?
  321. break;
  322. }
  323. return(TRUE);
  324. }
  325. // form proxy value for all protocals
  326. //
  327. BOOL GetOneProxyValue( char *szProxyValue, HKEY hKey, UINT type )
  328. {
  329. BOOL fExistPortNum = FALSE;
  330. DWORD dwRegType = 0;
  331. long lPortNum = 0;
  332. DWORD dwDataSize = sizeof(lPortNum);
  333. char szValue[MAX_PATH] = {0};
  334. DWORD dwSize = sizeof( szValue );
  335. LPSTR pszValueName;
  336. LPSTR pszPortName;
  337. LPSTR pszType;
  338. BOOL fValid = FALSE;
  339. if ( !szProxyValue || !hKey )
  340. {
  341. return FALSE;
  342. }
  343. switch( type )
  344. {
  345. case TYPE_HTTP:
  346. pszValueName = (LPSTR)NS_HTTP_KeyName;
  347. pszPortName = (LPSTR)NS_HTTP_PortKeyName;
  348. pszType = (LPSTR)c_gszHTTP;
  349. break;
  350. case TYPE_FTP:
  351. pszValueName = (LPSTR)NS_FTP_KeyName;
  352. pszPortName = (LPSTR)NS_FTP_PortKeyName;
  353. pszType = (LPSTR)c_gszFTP;
  354. break;
  355. case TYPE_GOPHER:
  356. pszValueName = (LPSTR)NS_Gopher_KeyName;
  357. pszPortName = (LPSTR)NS_Gopher_PortKeyName;
  358. pszType = (LPSTR)c_gszGopher;
  359. break;
  360. case TYPE_HTTPS:
  361. pszValueName = (LPSTR)NS_HTTPS_KeyName;
  362. pszPortName = (LPSTR)NS_HTTPS_PortKeyName;
  363. pszType = (LPSTR)c_gszHTTPS;
  364. break;
  365. case TYPE_SOCKS:
  366. pszValueName = (LPSTR)NS_SOCKS_KeyName;
  367. pszPortName = (LPSTR)NS_SOCKS_PortKeyName;
  368. pszType = (LPSTR)c_gszSOCKS;
  369. break;
  370. default:
  371. return FALSE;
  372. }
  373. if (ERROR_SUCCESS == RegQueryValueEx(hKey, pszPortName, NULL, &dwRegType, (LPBYTE)&lPortNum, &dwDataSize))
  374. {
  375. if (REG_DWORD == dwRegType)
  376. {
  377. fExistPortNum = TRUE;
  378. }
  379. }
  380. if (ERROR_SUCCESS == RegQueryValueEx(hKey, pszValueName, NULL, &dwRegType, (LPBYTE)szValue, &dwSize))
  381. {
  382. if ((0 != szValue[0] ) && (REG_SZ == dwRegType))
  383. {
  384. // Append the Port number if it was found above.
  385. if (TRUE == fExistPortNum)
  386. {
  387. char szPortNumStr[10];
  388. lstrcat(szValue, ":");
  389. wsprintf(szPortNumStr, "%lu", lPortNum);
  390. lstrcat(szValue, szPortNumStr);
  391. }
  392. fValid = TRUE;
  393. }
  394. }
  395. if ( fValid )
  396. {
  397. if ( lstrlen(szProxyValue) > 0)
  398. lstrcat( szProxyValue, ";" );
  399. lstrcat( szProxyValue, pszType );
  400. lstrcat( szProxyValue, szValue );
  401. }
  402. return fValid;
  403. }
  404. /****************************************************\
  405. FUNCTION: GetNSProxyValue
  406. PARAMETERS:
  407. BOOL return - Error result. FALSE == ERROR
  408. DESCRIPTION:
  409. This function will create the Server Proxy
  410. value used in an IE format. Netscape stores the
  411. name of the server and the port number separately.
  412. IE contains a string that has the server name,
  413. a ":" and followed by a port number. This
  414. function will convert the NS version to the IE
  415. version. Note that the port number is optional.
  416. \***************************************************/
  417. BOOL GetNSProxyValue(char * szProxyValue, DWORD * pdwSize)
  418. {
  419. HKEY hkey = NULL;
  420. char *szNSRegPath = "Software\\Netscape\\Netscape Navigator\\Proxy Information";
  421. char *szNSRegPath2 = "Software\\Netscape\\Netscape Navigator\\Services";
  422. //ASSERTSZ(NULL != szProxyValue, "Don't pass NULL");
  423. //ASSERTSZ(NULL != pdwSize, "Don't pass NULL (pdwSize)");
  424. // Get the port number if it exists...
  425. if ((NULL != szProxyValue) &&
  426. (NULL != pdwSize) &&
  427. (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, szNSRegPath, 0, KEY_QUERY_VALUE, &hkey)) &&
  428. (NULL != hkey))
  429. {
  430. GetOneProxyValue( szProxyValue, hkey, TYPE_HTTP );
  431. GetOneProxyValue( szProxyValue, hkey, TYPE_FTP );
  432. GetOneProxyValue( szProxyValue, hkey, TYPE_GOPHER );
  433. GetOneProxyValue( szProxyValue, hkey, TYPE_HTTPS );
  434. RegCloseKey(hkey);
  435. hkey = NULL;
  436. if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, szNSRegPath2, 0, KEY_QUERY_VALUE, &hkey) )
  437. {
  438. GetOneProxyValue( szProxyValue, hkey, TYPE_SOCKS );
  439. RegCloseKey(hkey);
  440. hkey = NULL;
  441. }
  442. }
  443. return(TRUE);
  444. }
  445. /****************************************************\
  446. FUNCTION: RegStrValueEmpty
  447. PARAMETERS:
  448. BOOL return - Error result. FALSE == ERROR
  449. DESCRIPTION:
  450. This function will check to see if the reg
  451. key passed in as a parameter is an empty str and return
  452. TRUE if it is empty and FALSE if it's not empty.
  453. If the reg key does not exist, we return TRUE.
  454. \***************************************************/
  455. BOOL RegStrValueEmpty(HKEY hTheKey, char * szPath, char * szKey)
  456. {
  457. char szCurrentValue[1024];
  458. HKEY hkey = NULL;
  459. DWORD dwRegType = 0;
  460. DWORD dwDataSize = sizeof(szCurrentValue);
  461. BOOL fSuccess = TRUE;
  462. szCurrentValue[0] = '\0';
  463. //ASSERTSZ(NULL != szPath,"Don't pass me NULL. (szPath)");
  464. //ASSERTSZ(NULL != szKey,"Don't pass me NULL. (szKey)");
  465. if ((NULL != szPath) &&
  466. (NULL != szKey) &&
  467. (ERROR_SUCCESS == RegOpenKeyEx(hTheKey, szPath, 0, KEY_QUERY_VALUE, &hkey)) &&
  468. (NULL != hkey))
  469. {
  470. if (ERROR_SUCCESS == RegQueryValueEx(hkey, szKey, NULL, &dwRegType, (LPBYTE)szCurrentValue, &dwDataSize))
  471. {
  472. if (REG_SZ == dwRegType)
  473. {
  474. if (0 != szCurrentValue[0])
  475. {
  476. // The value exists, but it's not equal to "".
  477. fSuccess = FALSE;
  478. }
  479. }
  480. }
  481. RegCloseKey(hkey);
  482. hkey = NULL;
  483. }
  484. return(fSuccess);
  485. }
  486. ///////////////////////////////////////////////////////////////////////////////////////
  487. // Netscape 4.0 proxy migration code
  488. ///////////////////////////////////////////////////////////////////////////////////////
  489. void ImportNetscape4Proxy()
  490. {
  491. char szProxyFile[MAX_PATH];
  492. DWORD dwFileSize;
  493. DWORD dwBytesRead;
  494. WIN32_FIND_DATA FindData;
  495. HANDLE hf;
  496. LPSTR lpData;
  497. // Only if we don't have proxy settings for IE
  498. // if (RegStrValueEmpty(HKEY_CURRENT_USER, (LPSTR)c_gszIERegPath, (LPSTR)c_gszIEProxyKeyName))
  499. {
  500. if (GetNav4UserDir(szProxyFile))
  501. {
  502. AddPath(szProxyFile, "prefs.js"); // Add the preferences file name
  503. // Get the data from the file
  504. hf = FindFirstFile(szProxyFile, &FindData);
  505. if (hf != INVALID_HANDLE_VALUE)
  506. {
  507. FindClose(hf);
  508. dwFileSize = FindData.nFileSizeLow;
  509. lpData = (LPSTR)LocalAlloc(LPTR, dwFileSize);
  510. if (lpData)
  511. {
  512. hf = CreateFile(szProxyFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  513. if (hf != INVALID_HANDLE_VALUE)
  514. {
  515. if (ReadFile(hf, lpData, dwFileSize, &dwBytesRead, NULL))
  516. {
  517. // Parse the data.
  518. ImportNav4Settings(lpData, dwBytesRead);
  519. }
  520. CloseHandle(hf);
  521. }
  522. LocalFree(lpData);
  523. }
  524. }
  525. }
  526. }
  527. }
  528. BOOL GetNav4UserDir(LPSTR lpszDir)
  529. {
  530. char szDir[MAX_PATH];
  531. HKEY hKey;
  532. HKEY hKeyUser;
  533. char szUser[MAX_PATH];
  534. DWORD dwSize;
  535. BOOL bDirFound = FALSE;
  536. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Netscape\\Netscape Navigator\\Users", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  537. {
  538. dwSize = sizeof(szUser);
  539. if (RegQueryValueEx(hKey, "CurrentUser", NULL, NULL, (LPBYTE)szUser, &dwSize) == ERROR_SUCCESS)
  540. {
  541. if (RegOpenKeyEx(hKey, szUser, 0, KEY_QUERY_VALUE, &hKeyUser) == ERROR_SUCCESS)
  542. {
  543. dwSize = sizeof(szDir);
  544. if (RegQueryValueEx(hKeyUser, "DirRoot", NULL, NULL, (LPBYTE)szDir, &dwSize) == ERROR_SUCCESS)
  545. {
  546. // Found the directory for the current user.
  547. lstrcpy(lpszDir, szDir);
  548. bDirFound = TRUE;
  549. }
  550. RegCloseKey(hKeyUser);
  551. }
  552. }
  553. RegCloseKey(hKey);
  554. }
  555. if (!bDirFound)
  556. {
  557. *szUser = '\0';
  558. // NAV 4.5 is not writing the above keys. there is a different way of finding the user dir.
  559. if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Netscape\\Netscape Navigator\\biff", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  560. {
  561. dwSize = sizeof(szUser);
  562. if (RegQueryValueEx(hKey, "CurrentUser", NULL, NULL, (LPBYTE)szUser, &dwSize) == ERROR_SUCCESS)
  563. {
  564. // Have the current user name. Now get the root folder where the user folder are.
  565. if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Netscape\\Netscape Navigator\\Main", 0, KEY_QUERY_VALUE, &hKeyUser) == ERROR_SUCCESS)
  566. {
  567. dwSize = sizeof(szDir);
  568. if (RegQueryValueEx(hKeyUser, "Install Directory", NULL, NULL, (LPBYTE)szDir, &dwSize) == ERROR_SUCCESS)
  569. {
  570. // Got the install folder.
  571. // Need to the the parent folder and then append users\%s , %s gets replaced with
  572. // the CurrentUser name.
  573. if (GetParentDir(szDir))
  574. {
  575. AddPath(szDir, "Users");
  576. AddPath(szDir, szUser);
  577. bDirFound = TRUE;
  578. lstrcpy(lpszDir, szDir);
  579. }
  580. }
  581. RegCloseKey(hKeyUser);
  582. }
  583. }
  584. RegCloseKey(hKey);
  585. }
  586. }
  587. return bDirFound;
  588. }
  589. void ImportNav4Settings(LPSTR lpData, DWORD dwBytes)
  590. {
  591. char szValue[MAX_PATH];
  592. char szProxyValue[MAX_STRING];
  593. UINT uiType;
  594. HKEY hIEKey;
  595. DWORD dwProxyEnabled = 0;
  596. char *pszNewOverride = NULL;
  597. if (GetValueFormNav4(lpData, dwBytes, (LPSTR)c_gszNetworkProxyType, lstrlen(c_gszNetworkProxyType), szValue))
  598. {
  599. uiType = (INT) AtoL(szValue);
  600. switch (uiType)
  601. {
  602. case MANUAL_PROXY:
  603. *szProxyValue = '\0';
  604. AppendOneNav4Setting(lpData, dwBytes, (LPSTR)c_gszNetworkProxyHttp, (LPSTR)c_gszNetworkProxyHttpPort, (LPSTR)c_gszHTTP, szProxyValue);
  605. AppendOneNav4Setting(lpData, dwBytes, (LPSTR)c_gszNetworkProxyFtp, (LPSTR)c_gszNetworkProxyFtpPort, (LPSTR)c_gszFTP, szProxyValue);
  606. AppendOneNav4Setting(lpData, dwBytes, (LPSTR)c_gszNetworkProxyGopher, (LPSTR)c_gszNetworkProxyGopherPort, (LPSTR)c_gszGopher, szProxyValue);
  607. AppendOneNav4Setting(lpData, dwBytes, (LPSTR)c_gszNetworkProxySsl, (LPSTR)c_gszNetworkProxySslPort, (LPSTR)c_gszHTTPS, szProxyValue);
  608. // Need to set the IE4 value(s)
  609. if (RegCreateKeyEx(HKEY_CURRENT_USER, c_gszIERegPath, 0, NULL, REG_OPTION_NON_VOLATILE,
  610. KEY_SET_VALUE, NULL, &hIEKey, NULL) == ERROR_SUCCESS)
  611. {
  612. // At this point, the NS value exists, so we will import the NS value.
  613. RegSetValueEx(hIEKey, c_gszIEProxyKeyName, 0, REG_SZ, (LPBYTE)szProxyValue, lstrlen(szProxyValue)+1);
  614. // We also need to turn on the Proxy
  615. dwProxyEnabled = 1;
  616. RegSetValueEx(hIEKey, c_gszIEWEnableKeyName, 0, REG_BINARY, (LPBYTE)&dwProxyEnabled, sizeof(dwProxyEnabled));
  617. RegCloseKey(hIEKey);
  618. }
  619. // need to do the proxyoverride.
  620. // if (RegStrValueEmpty(HKEY_CURRENT_USER, (LPSTR)c_gszIERegPath, (LPSTR)c_gszIEOverrideKeyName))
  621. {
  622. if (GetValueFormNav4(lpData, dwBytes, (LPSTR)c_gszNetworkProxyNoProxyOn, lstrlen(c_gszNetworkProxyNoProxyOn), szValue))
  623. {
  624. if ((*szValue != '\0') &&
  625. (RegCreateKeyEx(HKEY_CURRENT_USER, c_gszIERegPath, 0, NULL, REG_OPTION_NON_VOLATILE,
  626. KEY_SET_VALUE, NULL, &hIEKey, NULL) == ERROR_SUCCESS))
  627. {
  628. // At this point, the value exists, and it's invalid, so we need to change it.
  629. pszNewOverride = ConvertNetscapeProxyList(szValue);
  630. // Use the override list iff we successfully inserted '*' characters as appropriate.
  631. if (pszNewOverride)
  632. {
  633. RegSetValueEx(hIEKey, c_gszIEOverrideKeyName, 0, REG_SZ, (unsigned char*)pszNewOverride, lstrlen(pszNewOverride)+1);
  634. LocalFree(pszNewOverride); // This is the caller's responsibility.
  635. }
  636. else
  637. {
  638. RegSetValueEx(hIEKey, c_gszIEOverrideKeyName, 0, REG_SZ, (LPBYTE)szValue, lstrlen(szValue)+1);
  639. }
  640. RegCloseKey(hIEKey);
  641. }
  642. }
  643. }
  644. break;
  645. case AUTO_PROXY:
  646. if (GetValueFormNav4(lpData, dwBytes, (LPSTR)c_gszNetworkAutoProxy, lstrlen(c_gszNetworkAutoProxy), szValue))
  647. {
  648. if ((*szValue != '\0') &&
  649. (RegCreateKeyEx(HKEY_CURRENT_USER, c_gszIERegPath, 0, NULL, REG_OPTION_NON_VOLATILE,
  650. KEY_SET_VALUE, NULL, &hIEKey, NULL) == ERROR_SUCCESS))
  651. {
  652. // At this point, the value exists, and it's invalid, so we need to change it.
  653. RegSetValueEx(hIEKey, c_gszIEAutoConfigUrl, 0, REG_SZ, (LPBYTE)szValue, lstrlen(szValue)+1);
  654. RegCloseKey(hIEKey);
  655. }
  656. }
  657. break;
  658. case NO_PROXY:
  659. // Nothing to do!?
  660. break;
  661. }
  662. }
  663. }
  664. // QFE 3046: This is a low risk helper function when importing Netscape
  665. // proxy bypass lists. They don't prefix non-server specific
  666. // addresses with a '*'.
  667. // For example, "*.netscape.com" would be ".netscape.com" in Nav.
  668. // This function allocates a new string and inserts '*' when
  669. // an address is found and it contains more than just whitespace
  670. // (and isn't already prefixed with a '*').
  671. // For example, "netscape.com, *.microsoft.com , ,domain.com"
  672. // would become "*netscape.com, *.microsoft.com , ,*domain.com".
  673. LPSTR ConvertNetscapeProxyList(LPSTR pszBypassList)
  674. {
  675. LPSTR pszNewBypassList = NULL;
  676. LPSTR pszOffset1 = NULL;
  677. LPSTR pszOffset2 = NULL;
  678. if (pszBypassList)
  679. {
  680. // No one should ever have a list that's very big,
  681. // so we'll make life easy here and alloc a string
  682. // guaranteed to be >= the converted string.
  683. pszNewBypassList = (LPSTR) LocalAlloc(LPTR, 2*lstrlen(pszBypassList) + 1);
  684. if (pszNewBypassList)
  685. {
  686. *pszNewBypassList = '\0';
  687. pszOffset1 = pszBypassList;
  688. while (*pszOffset1)
  689. {
  690. pszOffset2 = pszOffset1;
  691. // Copy and ignore any leading whitespace
  692. while (*pszOffset2 && IsSpace(*pszOffset2))
  693. {
  694. pszOffset2 = CharNext(pszOffset2);
  695. }
  696. if (pszOffset1 != pszOffset2)
  697. {
  698. lstrcpyn(pszNewBypassList + lstrlen(pszNewBypassList), pszOffset1, (size_t)(pszOffset2-pszOffset1+1));
  699. pszOffset1 = pszOffset2;
  700. }
  701. // while (*pszOffset2 && IsSpace(*pszOffset2) && *pszOffset2 != ',')
  702. // {
  703. // pszOffset2 = CharNext(pszOffset2);
  704. // }
  705. // Only insert a '*' if the item contains more than just whitespace.
  706. // Inserting a '*' gets the IE setting equivalent to the behavior in Nav.
  707. if (*pszOffset2 && *pszOffset2 != ',' && *pszOffset2 != '*')
  708. {
  709. lstrcat(pszNewBypassList, "*");
  710. }
  711. // Look for the next server/domain item
  712. // by finding the separator.
  713. pszOffset2 = ANSIStrChr(pszOffset2, ',');
  714. if (pszOffset2)
  715. {
  716. // Found a separator. Append everything up to the
  717. // next character after the separator.
  718. lstrcpyn(pszNewBypassList + lstrlen(pszNewBypassList), pszOffset1, (size_t)(pszOffset2-pszOffset1+2));
  719. pszOffset1 = pszOffset2+1;
  720. }
  721. else
  722. {
  723. // This is the last item. Append it and get ready to exit.
  724. lstrcat(pszNewBypassList, pszOffset1);
  725. pszOffset1 += lstrlen(pszOffset1);
  726. }
  727. }
  728. }
  729. }
  730. return pszNewBypassList;
  731. }
  732. BOOL GetValueFormNav4(LPSTR lpData, DWORD dwBytes, LPSTR lpName, DWORD dwSize, LPSTR lpValue)
  733. {
  734. LPSTR lp;
  735. LPSTR lpEnd;
  736. BYTE c;
  737. BOOL bFound = FALSE;
  738. // determine the tpe of proxy settings
  739. lp = ANSIStrStrI(lpData, lpName);
  740. if (lp)
  741. {
  742. lp += dwSize; // lp should point now to the closing "
  743. if (*lp == '\"')
  744. {
  745. lp = CharNext((LPCSTR)lp);
  746. while ( *lp && (*lp == ' '))
  747. lp = CharNext((LPCSTR)lp);
  748. if (*lp == ',')
  749. {
  750. lp = CharNext((LPCSTR)lp);
  751. while ( *lp && (*lp == ' '))
  752. lp = CharNext((LPCSTR)lp);
  753. // lp is now the start of the value.
  754. if (*lp == '\"')
  755. {
  756. lp = CharNext((LPCSTR)lp);
  757. lpEnd = lp;
  758. while ((*lpEnd) && (*lpEnd != '\"'))
  759. lpEnd = CharNext((LPCSTR)lpEnd);
  760. }
  761. else
  762. {
  763. lpEnd = lp;
  764. while ( *lpEnd && (*lpEnd != ')') && !IsSpace( (int) *lpEnd ))
  765. lpEnd = CharNext((LPCSTR)lpEnd);
  766. }
  767. c = *lpEnd;
  768. *lpEnd = '\0';
  769. lstrcpy(lpValue, (LPCSTR)lp);
  770. *lpEnd = c;
  771. bFound = TRUE;
  772. }
  773. }
  774. }
  775. return bFound;
  776. }
  777. void AppendOneNav4Setting(LPSTR lpData, DWORD dwBytes, LPSTR lpProxyName, LPSTR lpProxyPort, LPSTR lpProxyType, LPSTR lpProxyValue)
  778. {
  779. char szValue[MAX_PATH];
  780. if (GetValueFormNav4(lpData, dwBytes, lpProxyName, lstrlen(lpProxyName), szValue))
  781. {
  782. // Append the proxy name
  783. if ( lstrlen(lpProxyValue) > 0)
  784. lstrcat( lpProxyValue, ";" );
  785. lstrcat(lpProxyValue, lpProxyType);
  786. lstrcat(lpProxyValue, szValue );
  787. // If the proxy has a port value, append it.
  788. if (GetValueFormNav4(lpData, dwBytes, lpProxyPort, lstrlen(lpProxyPort), szValue))
  789. {
  790. lstrcat(lpProxyValue, ":");
  791. lstrcat(lpProxyValue, szValue );
  792. }
  793. }
  794. }
  795. void MyGetVersionFromFile(LPSTR lpszFilename, LPDWORD pdwMSVer, LPDWORD pdwLSVer, BOOL bVersion)
  796. {
  797. unsigned uiSize;
  798. DWORD dwVerInfoSize;
  799. DWORD dwHandle;
  800. VS_FIXEDFILEINFO * lpVSFixedFileInfo;
  801. void FAR *lpBuffer;
  802. LPVOID lpVerBuffer;
  803. *pdwMSVer = *pdwLSVer = 0L;
  804. dwVerInfoSize = GetFileVersionInfoSize(lpszFilename, &dwHandle);
  805. if (dwVerInfoSize)
  806. {
  807. // Alloc the memory for the version stamping
  808. lpBuffer = LocalAlloc(LPTR, dwVerInfoSize);
  809. if (lpBuffer)
  810. {
  811. // Read version stamping info
  812. if (GetFileVersionInfo(lpszFilename, dwHandle, dwVerInfoSize, lpBuffer))
  813. {
  814. if (bVersion)
  815. {
  816. // Get the value for Translation
  817. if (VerQueryValue(lpBuffer, "\\", (LPVOID*)&lpVSFixedFileInfo, &uiSize) &&
  818. (uiSize))
  819. {
  820. *pdwMSVer = lpVSFixedFileInfo->dwFileVersionMS;
  821. *pdwLSVer = lpVSFixedFileInfo->dwFileVersionLS;
  822. }
  823. }
  824. else
  825. {
  826. if (VerQueryValue(lpBuffer, "\\VarFileInfo\\Translation", &lpVerBuffer, &uiSize) &&
  827. (uiSize))
  828. {
  829. *pdwMSVer = LOWORD(*((DWORD *) lpVerBuffer)); // Language ID
  830. *pdwLSVer = HIWORD(*((DWORD *) lpVerBuffer)); // Codepage ID
  831. }
  832. }
  833. }
  834. LocalFree(lpBuffer);
  835. }
  836. }
  837. return ;
  838. }
  839. BOOL IsIEDefaultBrowser()
  840. {
  841. HKEY hKey;
  842. DWORD dwSize;
  843. DWORD dwType;
  844. char szTmp[MAX_PATH];
  845. BOOL bIEDefaultBrowser = FALSE;
  846. // Check where the default value for this is pointing
  847. if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "http\\shell\\open\\command", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  848. {
  849. dwSize = sizeof(szTmp);
  850. if ( RegQueryValueEx(hKey, NULL, 0, &dwType, (LPBYTE)szTmp, &dwSize) == ERROR_SUCCESS )
  851. {
  852. CharLower(szTmp); // lowercase the string for the strstr call.
  853. bIEDefaultBrowser = (ANSIStrStrI(szTmp, "iexplore.exe") != NULL);
  854. }
  855. RegCloseKey(hKey);
  856. }
  857. return bIEDefaultBrowser;
  858. }