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.

399 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. rasmon.c
  5. Abstract:
  6. Main rasmon file.
  7. Revision History:
  8. pmay
  9. --*/
  10. #include "precomp.h"
  11. GUID g_RasmontrGuid = RASMONTR_GUID;
  12. GUID g_NetshGuid = NETSH_ROOT_GUID;
  13. #define RAS_HELPER_VERSION 1
  14. CMD_ENTRY g_RasAddCmdTable[] =
  15. {
  16. CREATE_CMD_ENTRY(RASFLAG_AUTHTYPE_ADD, HandleRasflagAuthtypeAdd),
  17. CREATE_CMD_ENTRY(RASFLAG_LINK_ADD, HandleRasflagLinkAdd),
  18. CREATE_CMD_ENTRY(RASFLAG_MLINK_ADD, HandleRasflagMlinkAdd),
  19. CREATE_CMD_ENTRY(DOMAIN_REGISTER, HandleDomainRegister),
  20. };
  21. CMD_ENTRY g_RasDelCmdTable[] =
  22. {
  23. CREATE_CMD_ENTRY(RASFLAG_AUTHTYPE_DEL, HandleRasflagAuthtypeDel),
  24. CREATE_CMD_ENTRY(RASFLAG_LINK_DEL, HandleRasflagLinkDel),
  25. CREATE_CMD_ENTRY(RASFLAG_MLINK_DEL, HandleRasflagMlinkDel),
  26. CREATE_CMD_ENTRY(DOMAIN_UNREGISTER, HandleDomainUnregister),
  27. };
  28. CMD_ENTRY g_RasSetCmdTable[] =
  29. {
  30. CREATE_CMD_ENTRY(TRACE_SET, HandleTraceSet),
  31. CREATE_CMD_ENTRY(RASUSER_SET, HandleUserSet),
  32. CREATE_CMD_ENTRY(RASFLAG_AUTHMODE_SET, HandleRasflagAuthmodeSet),
  33. };
  34. CMD_ENTRY g_RasShowCmdTable[] =
  35. {
  36. CREATE_CMD_ENTRY(TRACE_SHOW, HandleTraceShow),
  37. CREATE_CMD_ENTRY(RASUSER_SHOW, HandleUserShow),
  38. CREATE_CMD_ENTRY(RASFLAG_AUTHMODE_SHOW, HandleRasflagAuthmodeShow),
  39. CREATE_CMD_ENTRY(RASFLAG_AUTHTYPE_SHOW, HandleRasflagAuthtypeShow),
  40. CREATE_CMD_ENTRY(RASFLAG_LINK_SHOW, HandleRasflagLinkShow),
  41. CREATE_CMD_ENTRY(RASFLAG_MLINK_SHOW, HandleRasflagMlinkShow),
  42. CREATE_CMD_ENTRY(DOMAIN_SHOWREG, HandleDomainShowRegistration),
  43. CREATE_CMD_ENTRY(SHOW_SERVERS, HandleRasShowServers),
  44. CREATE_CMD_ENTRY(SHOW_CLIENT, HandleClientShow),
  45. };
  46. CMD_GROUP_ENTRY g_RasCmdGroups[] =
  47. {
  48. CREATE_CMD_GROUP_ENTRY(GROUP_ADD, g_RasAddCmdTable),
  49. CREATE_CMD_GROUP_ENTRY(GROUP_DEL, g_RasDelCmdTable),
  50. CREATE_CMD_GROUP_ENTRY(GROUP_SET, g_RasSetCmdTable),
  51. CREATE_CMD_GROUP_ENTRY(GROUP_SHOW, g_RasShowCmdTable),
  52. };
  53. ULONG g_ulNumGroups = sizeof(g_RasCmdGroups)/sizeof(CMD_GROUP_ENTRY);
  54. HANDLE g_hModule;
  55. BOOL g_bCommit;
  56. DWORD g_dwNumTableEntries;
  57. RASMON_SERVERINFO g_ServerInfo, *g_pServerInfo = NULL;
  58. ULONG g_ulInitCount;
  59. NS_CONTEXT_CONNECT_FN RasConnect;
  60. DWORD ParentVersion;
  61. BOOL g_bRasDirty = FALSE;
  62. DWORD
  63. Connect(
  64. IN LPCWSTR pwszServer
  65. );
  66. DWORD
  67. WINAPI
  68. RasCommit(
  69. IN DWORD dwAction
  70. )
  71. {
  72. BOOL bCommit, bFlush = FALSE;
  73. switch(dwAction)
  74. {
  75. case NETSH_COMMIT:
  76. {
  77. if(g_bCommit)
  78. {
  79. return NO_ERROR;
  80. }
  81. g_bCommit = TRUE;
  82. break;
  83. }
  84. case NETSH_UNCOMMIT:
  85. {
  86. g_bCommit = FALSE;
  87. return NO_ERROR;
  88. }
  89. case NETSH_SAVE:
  90. {
  91. if(g_bCommit)
  92. {
  93. return NO_ERROR;
  94. }
  95. break;
  96. }
  97. case NETSH_FLUSH:
  98. {
  99. //
  100. // Action is a flush. If current state is commit, then
  101. // nothing to be done.
  102. //
  103. if(g_bCommit)
  104. {
  105. return NO_ERROR;
  106. }
  107. bFlush = TRUE;
  108. break;
  109. }
  110. default:
  111. {
  112. return NO_ERROR;
  113. }
  114. }
  115. //
  116. // Switched to commit mode. So set all valid info in the
  117. // strutures. Free memory and invalidate the info.
  118. //
  119. return NO_ERROR;
  120. }
  121. DWORD
  122. WINAPI
  123. RasStartHelper(
  124. IN CONST GUID *pguidParent,
  125. IN DWORD dwVersion
  126. )
  127. {
  128. DWORD dwErr;
  129. NS_CONTEXT_ATTRIBUTES attMyAttributes;
  130. ParentVersion = dwVersion;
  131. ZeroMemory(&attMyAttributes, sizeof(attMyAttributes));
  132. attMyAttributes.pwszContext = L"ras";
  133. attMyAttributes.guidHelper = g_RasmontrGuid;
  134. attMyAttributes.dwVersion = 1;
  135. attMyAttributes.dwFlags = 0;
  136. attMyAttributes.ulNumTopCmds = 0;
  137. attMyAttributes.pTopCmds = NULL;
  138. attMyAttributes.ulNumGroups = g_ulNumGroups;
  139. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_RasCmdGroups;
  140. attMyAttributes.pfnCommitFn = RasCommit;
  141. attMyAttributes.pfnDumpFn = RasDump;
  142. attMyAttributes.pfnConnectFn = RasConnect;
  143. dwErr = RegisterContext( &attMyAttributes );
  144. return dwErr;
  145. }
  146. VOID
  147. Disconnect()
  148. {
  149. if (g_pServerInfo->hkMachine)
  150. {
  151. RegCloseKey( g_pServerInfo->hkMachine );
  152. }
  153. // Clear out any server handles
  154. //
  155. UserServerInfoUninit(g_pServerInfo);
  156. // Free up the server name if needed
  157. if (g_pServerInfo->pszServer)
  158. {
  159. RutlFree(g_pServerInfo->pszServer);
  160. g_pServerInfo->pszServer = NULL;
  161. }
  162. }
  163. DWORD
  164. WINAPI
  165. RasUnInit(
  166. IN DWORD dwReserved
  167. )
  168. {
  169. if(InterlockedDecrement(&g_ulInitCount) isnot 0)
  170. {
  171. return NO_ERROR;
  172. }
  173. Disconnect();
  174. return NO_ERROR;
  175. }
  176. BOOL
  177. WINAPI
  178. RasDllEntry(
  179. HINSTANCE hInstDll,
  180. DWORD fdwReason,
  181. LPVOID pReserved
  182. )
  183. {
  184. switch (fdwReason)
  185. {
  186. case DLL_PROCESS_ATTACH:
  187. {
  188. g_hModule = hInstDll;
  189. DisableThreadLibraryCalls(hInstDll);
  190. break;
  191. }
  192. case DLL_PROCESS_DETACH:
  193. {
  194. break;
  195. }
  196. default:
  197. {
  198. break;
  199. }
  200. }
  201. return TRUE;
  202. }
  203. DWORD WINAPI
  204. InitHelperDll(
  205. IN DWORD dwNetshVersion,
  206. OUT PVOID pReserved
  207. )
  208. {
  209. DWORD dwSize = 0, dwErr;
  210. NS_HELPER_ATTRIBUTES attMyAttributes;
  211. //
  212. // See if this is the first time we are being called
  213. //
  214. if(InterlockedIncrement(&g_ulInitCount) != 1)
  215. {
  216. return NO_ERROR;
  217. }
  218. g_bCommit = TRUE;
  219. // Initialize the global server info
  220. g_pServerInfo = &g_ServerInfo;
  221. ZeroMemory(g_pServerInfo, sizeof(RASMON_SERVERINFO));
  222. Connect(NULL);
  223. // Register this module as a helper to the netsh root
  224. // context.
  225. //
  226. ZeroMemory( &attMyAttributes, sizeof(attMyAttributes) );
  227. attMyAttributes.dwVersion = RAS_HELPER_VERSION;
  228. attMyAttributes.guidHelper = g_RasmontrGuid;
  229. attMyAttributes.pfnStart = RasStartHelper;
  230. attMyAttributes.pfnStop = NULL;
  231. RegisterHelper( &g_NetshGuid, &attMyAttributes );
  232. // Register any sub contexts implemented in this dll
  233. //
  234. dwErr = RasContextInstallSubContexts();
  235. if (dwErr isnot NO_ERROR)
  236. {
  237. RasUnInit(0);
  238. return dwErr;
  239. }
  240. return NO_ERROR;
  241. }
  242. DWORD
  243. Connect(
  244. IN LPCWSTR pwszServer
  245. )
  246. {
  247. DWORD dwErr, dwSize;
  248. do {
  249. // Try to connect to the new router
  250. ZeroMemory(g_pServerInfo, sizeof(RASMON_SERVERINFO));
  251. if (pwszServer)
  252. {
  253. // Calculate the size to initialize the server name
  254. dwSize = (wcslen(pwszServer) + 1) * sizeof(WCHAR);
  255. if (*pwszServer != L'\\')
  256. {
  257. dwSize += 2 * sizeof(WCHAR);
  258. }
  259. // Allocate the server name
  260. g_pServerInfo->pszServer = RutlAlloc(dwSize, FALSE);
  261. if(g_pServerInfo->pszServer is NULL)
  262. {
  263. dwErr = GetLastError();
  264. break;
  265. }
  266. if (*pwszServer != L'\\')
  267. {
  268. wcscpy(g_pServerInfo->pszServer, L"\\\\");
  269. wcscpy(g_pServerInfo->pszServer + 2, pwszServer);
  270. }
  271. else
  272. {
  273. wcscpy(g_pServerInfo->pszServer, pwszServer);
  274. }
  275. }
  276. // Initialize the build number for the server
  277. dwErr = RutlGetOsVersion( g_pServerInfo );
  278. if (dwErr)
  279. {
  280. break;
  281. }
  282. // As soon as this doesn't cause a hang (bug in netcfg), readd it here.
  283. //
  284. // dwErr = UserServerInfoInit( g_pServerInfo );
  285. } while (FALSE);
  286. return dwErr;
  287. }
  288. DWORD
  289. RasConnectToServer(
  290. IN LPCWSTR pwszServer
  291. )
  292. {
  293. DWORD dwErr = NO_ERROR, dwSize;
  294. do {
  295. if ((g_pServerInfo->pszServer != pwszServer) &&
  296. (!pwszServer || !g_pServerInfo->pszServer ||
  297. wcscmp(pwszServer, g_pServerInfo->pszServer)))
  298. {
  299. // Disconnect from the old router
  300. Disconnect();
  301. dwErr = Connect(pwszServer);
  302. }
  303. } while (FALSE);
  304. return dwErr;
  305. }
  306. DWORD WINAPI
  307. RasConnect(
  308. IN LPCWSTR pwszMachineName
  309. )
  310. {
  311. // If context info is dirty, reregister it
  312. if (g_bRasDirty)
  313. {
  314. RasStartHelper(NULL, ParentVersion);
  315. }
  316. return RasConnectToServer(pwszMachineName);
  317. }