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.

370 lines
10 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: util.cpp
  4. //
  5. // Module: CMPROXY.DLL (TOOL)
  6. //
  7. // Synopsis: Utility functions 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. //
  17. // Function: GetBrowserVersion
  18. //
  19. // Synopsis: This function returns the version of IE currently installed by
  20. // using the DllGetVersion function of shdocvw.dll. This is the
  21. // IE team recommended way of determining the current version of
  22. // Internet Explorer.
  23. //
  24. // Arguments: DLLVERSIONINFO* pDllVersionInfo - structure for determining the
  25. // version of shdocvw.dll.
  26. //
  27. // Returns: HRESULT - Standard COM error codes
  28. //
  29. // History: quintinb Created 10/27/99
  30. //
  31. //+----------------------------------------------------------------------------
  32. HRESULT GetBrowserVersion(DLLVERSIONINFO* pDllVersionInfo)
  33. {
  34. HINSTANCE hBrowser;
  35. HRESULT hr = E_FAIL;
  36. //
  37. // Load the DLL
  38. //
  39. hBrowser = LoadLibrary("shdocvw.dll");
  40. if (hBrowser)
  41. {
  42. DLLGETVERSIONPROC pDllGetVersion;
  43. //
  44. // Load the version proc
  45. //
  46. pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hBrowser, "DllGetVersion");
  47. if (pDllGetVersion)
  48. {
  49. ZeroMemory(pDllVersionInfo, sizeof(DLLVERSIONINFO));
  50. pDllVersionInfo->cbSize = sizeof(DLLVERSIONINFO);
  51. hr = (*pDllGetVersion)(pDllVersionInfo);
  52. }
  53. FreeLibrary(hBrowser);
  54. }
  55. return hr;
  56. }
  57. //
  58. // Borrowed from cmdl32.exe
  59. //
  60. #define MAX_CMD_ARGS 15
  61. typedef enum _CMDLN_STATE
  62. {
  63. CS_END_SPACE, // done handling a space
  64. CS_BEGIN_QUOTE, // we've encountered a begin quote
  65. CS_END_QUOTE, // we've encountered a end quote
  66. CS_CHAR, // we're scanning chars
  67. CS_DONE
  68. } CMDLN_STATE;
  69. //+----------------------------------------------------------------------------
  70. //
  71. // Function: GetCmArgV
  72. //
  73. // Synopsis: Simulates ArgV using GetCommandLine
  74. //
  75. // Arguments: LPTSTR pszCmdLine - Ptr to a copy of the command line to be processed
  76. //
  77. // Returns: LPTSTR * - Ptr to a ptr array containing the arguments. Caller is
  78. // responsible for releasing memory.
  79. //
  80. // History: nickball Created 4/9/98
  81. //
  82. //+----------------------------------------------------------------------------
  83. LPTSTR *GetCmArgV(LPTSTR pszCmdLine)
  84. {
  85. MYDBGASSERT(pszCmdLine);
  86. if (NULL == pszCmdLine || NULL == pszCmdLine[0])
  87. {
  88. return NULL;
  89. }
  90. //
  91. // Allocate Ptr array, up to MAX_CMD_ARGS ptrs
  92. //
  93. LPTSTR *ppCmArgV = (LPTSTR *) CmMalloc(sizeof(LPTSTR) * MAX_CMD_ARGS);
  94. if (NULL == ppCmArgV)
  95. {
  96. return NULL;
  97. }
  98. //
  99. // Declare locals
  100. //
  101. LPTSTR pszCurr;
  102. LPTSTR pszNext;
  103. LPTSTR pszToken;
  104. CMDLN_STATE state;
  105. state = CS_CHAR;
  106. int ndx = 0;
  107. //
  108. // Parse out pszCmdLine and store pointers in ppCmArgV
  109. //
  110. pszCurr = pszToken = pszCmdLine;
  111. do
  112. {
  113. switch (*pszCurr)
  114. {
  115. case TEXT(' '):
  116. if (state == CS_CHAR)
  117. {
  118. //
  119. // We found a token
  120. //
  121. pszNext = CharNext(pszCurr);
  122. *pszCurr = TEXT('\0');
  123. ppCmArgV[ndx] = pszToken;
  124. ndx++;
  125. pszCurr = pszToken = pszNext;
  126. state = CS_END_SPACE;
  127. continue;
  128. }
  129. else
  130. {
  131. if (state == CS_END_SPACE || state == CS_END_QUOTE)
  132. {
  133. pszToken = CharNext(pszToken);
  134. }
  135. }
  136. break;
  137. case TEXT('\"'):
  138. if (state == CS_BEGIN_QUOTE)
  139. {
  140. //
  141. // We found a token
  142. //
  143. pszNext = CharNext(pszCurr);
  144. *pszCurr = TEXT('\0');
  145. //
  146. // skip the opening quote
  147. //
  148. pszToken = CharNext(pszToken);
  149. ppCmArgV[ndx] = pszToken;
  150. ndx++;
  151. pszCurr = pszToken = pszNext;
  152. state = CS_END_QUOTE;
  153. continue;
  154. }
  155. else
  156. {
  157. state = CS_BEGIN_QUOTE;
  158. }
  159. break;
  160. case TEXT('\0'):
  161. if (state != CS_END_QUOTE)
  162. {
  163. //
  164. // End of the line, set last token
  165. //
  166. ppCmArgV[ndx] = pszToken;
  167. }
  168. state = CS_DONE;
  169. break;
  170. default:
  171. if (state == CS_END_SPACE || state == CS_END_QUOTE)
  172. {
  173. state = CS_CHAR;
  174. }
  175. break;
  176. }
  177. pszCurr = CharNext(pszCurr);
  178. } while (state != CS_DONE);
  179. return ppCmArgV;
  180. }
  181. //+----------------------------------------------------------------------------
  182. //
  183. // Function: UseVpnName
  184. //
  185. // Synopsis: This function loads rasapi32.dll and enumerates the active
  186. // RAS connections using RasEnumConnections to see if the given
  187. // connectoid name is found. If it is then it returns TRUE, implying
  188. // that the alternate name passed in should be used instead of the
  189. // regular connectoid name (ie. the tunnel connectoid name exists,
  190. // therefore you are tunneling).
  191. //
  192. // Arguments: LPSTR pszAltName -
  193. //
  194. // Returns: BOOL - return TRUE if the VPN connectoid should be used instead
  195. // of the regular dialup connectoid.
  196. //
  197. // History: quintinb Created 10/28/99
  198. //
  199. //+----------------------------------------------------------------------------
  200. BOOL UseVpnName(LPSTR pszAltName)
  201. {
  202. BOOL bReturn = FALSE;
  203. //
  204. // Load RAS
  205. //
  206. HINSTANCE hRas = LoadLibrary("rasapi32.dll");
  207. if (hRas)
  208. {
  209. //
  210. // Load RasEnumConnections
  211. //
  212. typedef DWORD (WINAPI* pfnRasEnumConnectionsSpec)(LPRASCONNA, LPDWORD, LPDWORD);
  213. pfnRasEnumConnectionsSpec pfnRasEnumConnections = NULL;
  214. pfnRasEnumConnections = (pfnRasEnumConnectionsSpec)GetProcAddress(hRas, "RasEnumConnectionsA");
  215. if (pfnRasEnumConnections)
  216. {
  217. LPRASCONN pRasConn = NULL;
  218. DWORD dwSize = 2*sizeof(RASCONN);
  219. DWORD dwNum = 0;
  220. DWORD dwResult = 0;
  221. //
  222. // Get a list of Active Connections
  223. //
  224. do
  225. {
  226. CmFree(pRasConn);
  227. pRasConn = (LPRASCONN)CmMalloc(dwSize);
  228. if (pRasConn)
  229. {
  230. pRasConn[0].dwSize = sizeof(RASCONN);
  231. dwResult = (pfnRasEnumConnections)(pRasConn, &dwSize, &dwNum);
  232. }
  233. } while (ERROR_INSUFFICIENT_BUFFER == dwResult);
  234. //
  235. // Search for the name passed in
  236. //
  237. if (ERROR_SUCCESS == dwResult)
  238. {
  239. for (DWORD dwIndex = 0; dwIndex < dwNum; dwIndex++)
  240. {
  241. if (0 == lstrcmpi(pszAltName, pRasConn[dwIndex].szEntryName))
  242. {
  243. //
  244. // Then the Tunnel Name is active and that should be used for
  245. // the proxy
  246. //
  247. bReturn = TRUE;
  248. break;
  249. }
  250. }
  251. }
  252. CmFree(pRasConn);
  253. }
  254. FreeLibrary (hRas);
  255. }
  256. return bReturn;
  257. }
  258. //+----------------------------------------------------------------------------
  259. //
  260. // Function: GetString
  261. //
  262. // Synopsis: Wrapper for GetPrivateProfileString that takes care of allocating
  263. // memory (using CmMalloc) correctly. GetString will max sure to
  264. // allocate enough memory for the string (1MB is used as a sanity
  265. // check, no string should be that large and GetString will stop
  266. // trying to allocate memory at that point). Please note that it is
  267. // the callers responsibility to free the allocated memory.
  268. //
  269. // Arguments: LPCSTR pszSection - Section name
  270. // LPCSTR pszKey - Key name
  271. // LPSTR* ppString - string pointer to fill with the memory
  272. // containing the requested string
  273. // LPCSTR pszFile - File to retrieve info from
  274. //
  275. // Returns: Nothing
  276. //
  277. // History: quintinb Created 10/28/99
  278. //
  279. //+----------------------------------------------------------------------------
  280. void GetString(LPCSTR pszSection, LPCSTR pszKey, LPSTR* ppString, LPCSTR pszFile)
  281. {
  282. DWORD dwTemp;
  283. DWORD dwSize = MAX_PATH;
  284. BOOL bExit = FALSE;
  285. do
  286. {
  287. CmFree(*ppString);
  288. *ppString = (CHAR*)CmMalloc(dwSize);
  289. if (*ppString)
  290. {
  291. dwTemp = GetPrivateProfileString(pszSection, pszKey, "",
  292. *ppString, dwSize, pszFile);
  293. if (((dwSize - 1) == dwTemp) && (1024*1024 > dwSize))
  294. {
  295. //
  296. // Buffer too small, lets try again.
  297. //
  298. dwSize = 2*dwSize;
  299. }
  300. else
  301. {
  302. bExit = TRUE;
  303. }
  304. }
  305. else
  306. {
  307. bExit = TRUE;
  308. }
  309. } while (!bExit);
  310. }