Source code of Windows XP (NT5)
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.

288 lines
6.1 KiB

  1. // proxy.cpp
  2. //
  3. #include "stdpch.h"
  4. #pragma hdrstop
  5. #include "host.h" // CHost
  6. #include "output.h" // COutput
  7. #define SECURITY_WIN32
  8. #include "sspi.h"
  9. #include "secext.h"
  10. #include "proxy.h"
  11. #include "tchar.h"
  12. const TCHAR szFileVersion[] = TEXT("FileVersion");
  13. BOOL GetRegEntry(HKEY hKey,
  14. LPCWSTR lpValueName,
  15. LPBYTE *lpData)
  16. {
  17. DWORD dwSize = 256;
  18. DWORD dwRetVal = 0;
  19. DWORD dwType = 0;
  20. if( !lpData )
  21. {
  22. return FALSE;
  23. }
  24. do
  25. {
  26. if( *lpData == NULL )
  27. {
  28. *lpData = (LPBYTE)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwSize);
  29. if( !lpData )
  30. {
  31. return FALSE;
  32. }
  33. }
  34. else
  35. {
  36. LPBYTE pTmp;
  37. pTmp = (LPBYTE)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,*lpData,dwSize);
  38. if( pTmp )
  39. {
  40. *lpData = pTmp;
  41. }
  42. else
  43. {
  44. return FALSE;
  45. }
  46. }
  47. dwRetVal = RegQueryValueEx(hKey,
  48. lpValueName,
  49. NULL,
  50. &dwType,
  51. (LPBYTE)*lpData,
  52. &dwSize);
  53. switch(dwRetVal)
  54. {
  55. case ERROR_SUCCESS:
  56. return TRUE;
  57. case ERROR_MORE_DATA:
  58. break;
  59. default:
  60. return FALSE;
  61. }
  62. }
  63. while(TRUE);
  64. return TRUE;
  65. }
  66. BOOL EXPORT FindProxy(CHost* pProxy, LPDWORD pdwPort, LPDWORD pdwEnabled)
  67. {
  68. auto_reg regIe;
  69. LONG l;
  70. TCHAR szBuffer[256];
  71. DWORD dwType;
  72. DWORD dwEnabled;
  73. DWORD cbBuffer;
  74. UINT cbVer;
  75. LPTSTR pszSearch;
  76. LPTSTR pszStart;
  77. LPVOID lpVerData;
  78. DWORD dwVerInfo;
  79. VS_FIXEDFILEINFO *lpFFI;
  80. HKEY hKeyRoot = NULL;
  81. LPTSTR pszIExplore, pszEnd;
  82. LPTSTR pszSubKey;
  83. auto_reg regApps;
  84. LPTSTR pszBuffer = NULL;
  85. // Find what version of IE we have, so we can figure out where they hide the
  86. // proxy info.
  87. // So where o where do iexplore live, eh?
  88. // Try in the millenium place
  89. l = RegOpenKeyEx(
  90. HKEY_CLASSES_ROOT,
  91. TEXT("Applications\\iexplore.exe\\shell\\open\\command"),
  92. 0,
  93. KEY_READ,
  94. &regApps);
  95. if (l == ERROR_FILE_NOT_FOUND)
  96. {
  97. l = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  98. TEXT("Software\\Classes\\Applications\\iexplore.exe\\shell\\open\\command"),
  99. 0,
  100. KEY_READ,
  101. &regApps);
  102. }
  103. if (S_OK != l)
  104. goto Error;
  105. if( !GetRegEntry(regApps,L"",(LPBYTE*)&pszBuffer) )
  106. {
  107. goto Error;
  108. }
  109. /*
  110. cbBuffer = sizeof(szBuffer);
  111. l = RegQueryValueEx(regApps,
  112. regApps,
  113. TEXT(""),
  114. NULL, // reserved
  115. &dwType,
  116. (LPBYTE)&szBuffer[0],
  117. &cbBuffer);
  118. if (S_OK != l)
  119. goto Error;
  120. // strip the quotes out
  121. pszIExplore = szBuffer;
  122. */
  123. pszIExplore = pszBuffer;
  124. while (*pszIExplore == '\"')
  125. {
  126. ++pszIExplore;
  127. }
  128. pszEnd = pszIExplore;
  129. while (*pszEnd && *pszEnd != '\"')
  130. {
  131. ++pszEnd;
  132. }
  133. *pszEnd = '\0';
  134. DWORD dwNoIdea=0;
  135. if (dwVerInfo = GetFileVersionInfoSize(pszIExplore, &dwNoIdea))
  136. {
  137. lpVerData = _alloca(dwVerInfo);
  138. if (GetFileVersionInfo(pszIExplore, 0, dwVerInfo, lpVerData))
  139. {
  140. VerQueryValue(lpVerData, TEXT("\\"), (LPVOID*)&lpFFI, &cbVer);
  141. // For now we will assume that the proxy settings will not change for for later version of
  142. // IE. It might be good to get the location of the proxy using api calls
  143. if (HIWORD(lpFFI->dwFileVersionMS) >= 5)
  144. {
  145. hKeyRoot = HKEY_CURRENT_USER;
  146. pszSubKey = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
  147. }
  148. }
  149. }
  150. dwVerInfo = GetLastError();
  151. // HKCU is not dynamic, so close the current one
  152. if (hKeyRoot == HKEY_CURRENT_USER)
  153. RegCloseKey(hKeyRoot);
  154. if( !hKeyRoot )
  155. {
  156. // We did not get a key, something went wrong, abort
  157. //
  158. goto Error;
  159. }
  160. l = RegOpenKeyEx(
  161. hKeyRoot,
  162. pszSubKey,
  163. 0,
  164. KEY_READ,
  165. &regIe);
  166. if (S_OK != l)
  167. goto Error;
  168. //cbBuffer = sizeof(szBuffer);
  169. //dwType = REG_SZ;
  170. if( !GetRegEntry(regIe,TEXT("ProxyServer"),(LPBYTE*)&pszBuffer) )
  171. {
  172. goto Error;
  173. }
  174. //pszSearch = _tcsstr(pszBuffer, TEXT("http://"));
  175. pszSearch = _tcsstr(pszBuffer, TEXT("://"));
  176. if (pszSearch)
  177. {
  178. pszSearch += 3; //7;
  179. pszStart = pszSearch;
  180. }
  181. else
  182. {
  183. pszSearch = _tcsstr(pszBuffer, TEXT("="));
  184. if (pszSearch)
  185. {
  186. pszSearch += 1; //7;
  187. pszStart = pszSearch;
  188. }
  189. else
  190. {
  191. pszSearch = pszBuffer;
  192. pszStart = pszBuffer;
  193. }
  194. }
  195. while (*pszSearch && *pszSearch != TEXT(':'))
  196. ++pszSearch;
  197. // Grab the port
  198. if (!*pszSearch)
  199. {
  200. *pdwPort = 80;
  201. }
  202. else
  203. {
  204. *pszSearch = TEXT('\0');
  205. #if UNICODE
  206. *pdwPort = wcstol(pszSearch + 1,NULL,10);
  207. #else
  208. //*pdwPort = atol(pszSearch + 1);
  209. *pdwPort = strtol(pszSearch + 1,NULL,10);
  210. #endif
  211. }
  212. pProxy->SetHost(pszStart);
  213. cbBuffer = sizeof (dwEnabled);
  214. l = RegQueryValueEx(
  215. regIe,
  216. TEXT("ProxyEnable"),
  217. NULL, // reserved
  218. &dwType,
  219. (LPBYTE)&dwEnabled,
  220. &cbBuffer);
  221. if (S_OK != l)
  222. goto Error;
  223. if (pdwEnabled)
  224. *pdwEnabled = dwEnabled;
  225. if (hKeyRoot == HKEY_CURRENT_USER)
  226. RegCloseKey(HKEY_CURRENT_USER);
  227. // CoRevertToSelf();
  228. if( pszBuffer )
  229. {
  230. HeapFree(GetProcessHeap(),0,pszBuffer);
  231. }
  232. return TRUE;
  233. Error:
  234. if (hKeyRoot == HKEY_CURRENT_USER)
  235. RegCloseKey(HKEY_CURRENT_USER);
  236. // CoRevertToSelf();
  237. if( pszBuffer )
  238. {
  239. HeapFree(GetProcessHeap(),0,pszBuffer);
  240. }
  241. return FALSE;
  242. }