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.

475 lines
11 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998.
  5. //
  6. // File: RasProc.cpp
  7. //
  8. // Contents: Exports used by Ras for doing Pending Disconnect
  9. //
  10. // Classes:
  11. //
  12. // Notes:
  13. //
  14. // History: 09-Jan-98 rogerg Created.
  15. //
  16. //--------------------------------------------------------------------------
  17. // Windows Header Files:
  18. // !!! define this file winver to be at least 5.0 so we can get
  19. // NT 5.0 specific ras structures and still be a single binary
  20. // Ras calls made on < 50 will fail because structure is wrong size.
  21. // Do not use preocompiled headers since they would have already defined
  22. // the Winver to be 40
  23. #ifdef WINVER
  24. #undef WINVER
  25. #endif
  26. #define WINVER 0x500
  27. #include <windows.h>
  28. #include <commctrl.h>
  29. #include <objbase.h>
  30. #include "mobsync.h"
  31. #include "mobsyncp.h"
  32. #include "debug.h"
  33. #include "alloc.h"
  34. #include "critsect.h"
  35. #include "netapi.h"
  36. #include "syncmgrr.h"
  37. #include "rasui.h"
  38. #include "dllreg.h"
  39. #include "cnetapi.h"
  40. #include "rasproc.h"
  41. extern OSVERSIONINFOA g_OSVersionInfo; // osVersionInfo.
  42. //+-------------------------------------------------------------------
  43. //
  44. // Function: SyncMgrRasProc
  45. //
  46. // Synopsis: Main entry point for RAS to call to perform
  47. // a pending disconnect.
  48. //
  49. // Arguments:
  50. //
  51. //
  52. // Notes:
  53. //
  54. //--------------------------------------------------------------------
  55. LRESULT CALLBACK SyncMgrRasProc(UINT uMsg,WPARAM wParam, LPARAM lParam)
  56. {
  57. switch(uMsg)
  58. {
  59. case SYNCMGRRASPROC_QUERYSHOWSYNCUI:
  60. return SyncMgrRasQueryShowSyncUI(wParam,lParam);
  61. break;
  62. case SYNCMGRRASPROC_SYNCDISCONNECT:
  63. return SyncMgrRasDisconnect(wParam,lParam);
  64. break;
  65. default:
  66. AssertSz(0,"Unknown RasProc Message");
  67. break;
  68. };
  69. return -1;
  70. }
  71. //+-------------------------------------------------------------------
  72. //
  73. // Function: SyncMgrRasQueryShowSyncUI
  74. //
  75. // Synopsis: Called by RAS to determine if Ras Should show
  76. // the Disconnect checkbox and what state it should be.
  77. //
  78. // Arguments:
  79. // wParam = 0
  80. // lParam = Pointer to SYNCMGRQUERYSHOWSYNCUI structure
  81. //
  82. // Notes:
  83. //
  84. //--------------------------------------------------------------------
  85. LRESULT SyncMgrRasQueryShowSyncUI(WPARAM wParam,LPARAM lParam)
  86. {
  87. CNetApi *pNetApi;
  88. SYNCMGRQUERYSHOWSYNCUI *pQueryStruct = (SYNCMGRQUERYSHOWSYNCUI *) lParam;
  89. // RASENTRY rasEntry;
  90. // TCHAR lpszEntry[RAS_MaxEntryName + 1];
  91. LRESULT lResult = -1;
  92. if (pQueryStruct->cbSize != sizeof(SYNCMGRQUERYSHOWSYNCUI))
  93. {
  94. Assert(pQueryStruct->cbSize == sizeof(SYNCMGRQUERYSHOWSYNCUI));
  95. return -1;
  96. }
  97. pQueryStruct->fShowCheckBox = FALSE;
  98. pQueryStruct->nCheckState = BST_UNCHECKED;
  99. pNetApi = new CNetApi();
  100. if (!pNetApi)
  101. {
  102. AssertSz(0,"Failed to Load Ras");
  103. return -1;
  104. }
  105. RegSetUserDefaults(); // Make Sure the UserDefaults are up to date
  106. // don't use guid lookup, instead just use the passed in name.
  107. if (1 /* NOERROR == pNetApi->RasConvertGuidToEntry(&(pQueryStruct->GuidConnection),
  108. (LPTSTR) &lpszEntry,
  109. &rasEntry) */)
  110. {
  111. CONNECTIONSETTINGS connectSettings;
  112. lstrcpy(connectSettings.pszConnectionName,pQueryStruct->pszConnectionName); // Review, should just pass this to Function
  113. // look up preferences for this entry and see if disconnect has been chosen.
  114. lResult = 0; // return NOERROR even if no entry is found
  115. if (RegGetAutoSyncSettings(&connectSettings))
  116. {
  117. if (connectSettings.dwLogoff)
  118. {
  119. pQueryStruct->fShowCheckBox = TRUE;
  120. pQueryStruct->nCheckState = BST_CHECKED;
  121. }
  122. }
  123. }
  124. pNetApi->Release();
  125. return lResult;
  126. }
  127. //+-------------------------------------------------------------------
  128. //
  129. // Function: SyncMgrRasDisconnect
  130. //
  131. // Synopsis: Main entry point for RAS to call to perform
  132. // a pending disconnect.
  133. //
  134. // Arguments:
  135. // wParam = 0
  136. // lParam = Pointer to SYNCMGRSYNCDISCONNECT structure
  137. //
  138. // Notes:
  139. //
  140. //--------------------------------------------------------------------
  141. LRESULT SyncMgrRasDisconnect(WPARAM wParam,LPARAM lParam)
  142. {
  143. CNetApi *pNetApi;
  144. SYNCMGRSYNCDISCONNECT *pDisconnectStruct = (SYNCMGRSYNCDISCONNECT *) lParam;
  145. TCHAR szEntry[RAS_MaxEntryName + 1];
  146. // RASENTRY rasEntry;
  147. if (pDisconnectStruct->cbSize != sizeof(SYNCMGRSYNCDISCONNECT))
  148. {
  149. Assert(pDisconnectStruct->cbSize == sizeof(SYNCMGRSYNCDISCONNECT));
  150. return -1;
  151. }
  152. pNetApi = new CNetApi();
  153. if (!pNetApi)
  154. {
  155. AssertSz(0,"Failed to Load Ras");
  156. return -1;
  157. }
  158. if (1 /* NOERROR == pNetApi->RasConvertGuidToEntry(&(pDisconnectStruct->GuidConnection),
  159. (LPTSTR) &szEntry,
  160. &rasEntry) */)
  161. {
  162. HRESULT hr;
  163. LPUNKNOWN lpUnk;
  164. // invoke SyncMgr.exe informing it is a Logoff and then wait in
  165. // a message loop until the event we pass in gets signalled.
  166. lstrcpy(szEntry,pDisconnectStruct->pszConnectionName);
  167. hr = CoInitialize(NULL);
  168. if (SUCCEEDED(hr))
  169. {
  170. hr = CoCreateInstance(CLSID_SyncMgrp,NULL,CLSCTX_ALL,IID_IUnknown,(void **) &lpUnk);
  171. if (NOERROR == hr)
  172. {
  173. LPPRIVSYNCMGRSYNCHRONIZEINVOKE pSynchInvoke = NULL;
  174. hr = lpUnk->QueryInterface(IID_IPrivSyncMgrSynchronizeInvoke,
  175. (void **) &pSynchInvoke);
  176. if (NOERROR == hr)
  177. {
  178. // should have everything we need
  179. hr = pSynchInvoke->RasPendingDisconnect(
  180. (RAS_MaxEntryName + 1)*sizeof(TCHAR),
  181. (BYTE *) szEntry);
  182. pSynchInvoke->Release();
  183. }
  184. lpUnk->Release();
  185. }
  186. CoUninitialize();
  187. }
  188. }
  189. pNetApi->Release();
  190. return 0;
  191. }
  192. // !!!!These method are in this file so we can locally define WINVER to be 5.0 so we
  193. // get the proper rasentry size.
  194. #define RASENTRYNAMEW struct tagRASENTRYNAMEW
  195. typedef struct _tagRASENTRYNAME40W
  196. {
  197. DWORD dwSize;
  198. WCHAR szEntryName[ RAS_MaxEntryName + 1 ];
  199. } RASENTRYNAME40W;
  200. //+-------------------------------------------------------------------
  201. //
  202. // Member: CNetApi::RasEnumEntriesNT50
  203. //
  204. // Synopsis: enums the RasEntries thunking to the NT50 structure.
  205. //
  206. //
  207. // Notes: Only Available on NT 5.0
  208. //
  209. //--------------------------------------------------------------------
  210. DWORD CNetApi::RasEnumEntriesNT50(LPWSTR reserved,LPWSTR lpszPhoneBook,
  211. LPRASENTRYNAME lprasEntryName,LPDWORD lpcb,LPDWORD lpcEntries)
  212. {
  213. BOOL fIsNT = (g_OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
  214. LPRASENTRYNAME lpRasEntryNT50 = NULL;
  215. DWORD cbSizeNT50 = 0;
  216. DWORD dwReturn;
  217. // if ras isn't loaded just return false
  218. // call ras exports directly fro here to ensure
  219. // we stick with 5.0 sizes.
  220. if (!fIsNT || g_OSVersionInfo.dwMajorVersion < 5)
  221. {
  222. Assert(fIsNT);
  223. Assert(g_OSVersionInfo.dwMajorVersion >= 5);
  224. return -1;
  225. }
  226. if (NOERROR != LoadRasApiDll())
  227. {
  228. return -1;
  229. }
  230. // if there is an entry name set up the NT 5.0 size.
  231. if (lprasEntryName)
  232. {
  233. DWORD cbNumRasEntries;
  234. Assert(lprasEntryName->dwSize == sizeof(RASENTRYNAME40W));
  235. cbNumRasEntries = (*lpcb)/sizeof(RASENTRYNAME40W);
  236. Assert(cbNumRasEntries > 0);
  237. if (cbNumRasEntries)
  238. {
  239. cbSizeNT50 = sizeof(RASENTRYNAME)*cbNumRasEntries;
  240. lpRasEntryNT50 = (RASENTRYNAME *) ALLOC(cbSizeNT50);
  241. if (NULL == lpRasEntryNT50)
  242. {
  243. // if out of memory, return an error.
  244. return -1;
  245. }
  246. else
  247. {
  248. lpRasEntryNT50->dwSize = sizeof(RASENTRYNAME);
  249. }
  250. }
  251. }
  252. dwReturn = (*m_pRasEnumEntriesW)(reserved,lpszPhoneBook,
  253. lpRasEntryNT50,&cbSizeNT50,lpcEntries);
  254. // set out buffer to size return dbSizeNT50. Bigger than
  255. // we need but then don't have to worry.
  256. *lpcb = cbSizeNT50;
  257. // on success thunk back to original structure.
  258. if (0 == dwReturn && lpRasEntryNT50)
  259. {
  260. DWORD dwItems = *lpcEntries;
  261. RASENTRYNAME40W *pCurRasEntry = (RASENTRYNAME40W *) lprasEntryName;
  262. LPRASENTRYNAME pCurRasEntryNT50 = lpRasEntryNT50;
  263. while(dwItems--)
  264. {
  265. pCurRasEntry->dwSize = sizeof(RASENTRYNAME40W);
  266. lstrcpy(pCurRasEntry->szEntryName,pCurRasEntryNT50->szEntryName);
  267. ++pCurRasEntry;
  268. ++pCurRasEntryNT50;
  269. }
  270. }
  271. if (lpRasEntryNT50)
  272. {
  273. FREE(lpRasEntryNT50);
  274. }
  275. return dwReturn;
  276. }
  277. //+-------------------------------------------------------------------
  278. //
  279. // Member: CNetApi::RasConvertGuidToEntry
  280. //
  281. // Synopsis: Given a Guid, convert, finds the corresponding
  282. // Ras Entry.
  283. //
  284. // Arguments:
  285. // pGuid - Guid of Connection to Convert.
  286. // lpszEntry - PhoneBook Entry if a match is found.
  287. // pRasEntry - Points to valid RasEntry structure if a match is found.
  288. //
  289. // Notes: Only Available on NT 5.0
  290. //
  291. //--------------------------------------------------------------------
  292. STDMETHODIMP CNetApi::RasConvertGuidToEntry(GUID *pGuid,LPTSTR lpszEntry,RASENTRY *pRasEntry)
  293. {
  294. DWORD dwSize;
  295. DWORD cEntries;
  296. DWORD dwError = -1;
  297. LPRASENTRYNAME lprasentry;
  298. BOOL fFoundMatch = FALSE;
  299. // if ras isn't loaded just return false
  300. // call ras exports directly fro here to ensure
  301. // we stick with 5.0 sizes.
  302. Assert(m_fIsUnicode);
  303. if (NOERROR != LoadRasApiDll())
  304. {
  305. return S_FALSE;
  306. }
  307. if (!m_hInstRasApiDll || (NULL == m_pRasEnumEntriesW)
  308. || (NULL == m_pRasGetEntryPropertiesW) )
  309. {
  310. return S_FALSE;
  311. }
  312. dwSize = sizeof(RASENTRYNAME);
  313. lprasentry = (LPRASENTRYNAME) ALLOC(dwSize);
  314. if(!lprasentry)
  315. {
  316. goto error;
  317. }
  318. lprasentry->dwSize = sizeof(RASENTRYNAME);
  319. cEntries = 0;
  320. dwError = (*m_pRasEnumEntriesW)(NULL, NULL,lprasentry, &dwSize, &cEntries);
  321. if (dwError == ERROR_BUFFER_TOO_SMALL)
  322. {
  323. FREE(lprasentry);
  324. lprasentry = (LPRASENTRYNAME) ALLOC(dwSize);
  325. if(!lprasentry)
  326. goto error;
  327. lprasentry->dwSize = sizeof(RASENTRYNAME);
  328. cEntries = 0;
  329. dwError = (*m_pRasEnumEntriesW)(NULL, NULL,lprasentry, &dwSize, &cEntries);
  330. }
  331. if (0 != dwError)
  332. {
  333. goto error;
  334. }
  335. while(cEntries && !fFoundMatch)
  336. {
  337. TCHAR *pszEntryName = lprasentry[cEntries-1].szEntryName;
  338. TCHAR *pszPhoneBookName = lprasentry[cEntries-1].szPhonebookPath;
  339. DWORD dwSizeRasEntry;
  340. LPRASENTRY pRasEntry;
  341. DWORD dwErr;
  342. dwSizeRasEntry = 0;
  343. // get the required size
  344. if (ERROR_BUFFER_TOO_SMALL ==
  345. (*m_pRasGetEntryPropertiesW)(pszPhoneBookName,pszEntryName,NULL, &dwSizeRasEntry,NULL,NULL))
  346. {
  347. // allocate the required size.
  348. pRasEntry = (LPRASENTRY) ALLOC(dwSizeRasEntry);
  349. if (pRasEntry)
  350. {
  351. pRasEntry->dwSize = sizeof(RASENTRY);
  352. if (0 == (dwErr = (*m_pRasGetEntryPropertiesW)(pszPhoneBookName,pszEntryName, (LPBYTE) pRasEntry, &dwSizeRasEntry,NULL,NULL)))
  353. {
  354. if (IsEqualGUID(pRasEntry->guidId,*pGuid))
  355. {
  356. lstrcpy(lpszEntry,pszEntryName);
  357. fFoundMatch = TRUE;
  358. }
  359. }
  360. FREE(pRasEntry);
  361. }
  362. }
  363. cEntries--;
  364. }
  365. if (FALSE == fFoundMatch)
  366. dwError = -1;
  367. error:
  368. return (dwError == 0) ? NOERROR : S_FALSE;
  369. }