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.

488 lines
10 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. #define RAS_HELPER_VERSION 1
  12. CMD_ENTRY g_RasAddCmdTable[] =
  13. {
  14. CREATE_CMD_ENTRY(RASFLAG_AUTHTYPE_ADD, HandleRasflagAuthtypeAdd),
  15. CREATE_CMD_ENTRY(RASFLAG_LINK_ADD, HandleRasflagLinkAdd),
  16. CREATE_CMD_ENTRY(RASFLAG_MLINK_ADD, HandleRasflagMlinkAdd),
  17. CREATE_CMD_ENTRY(DOMAIN_REGISTER, HandleDomainRegister),
  18. };
  19. CMD_ENTRY g_RasDelCmdTable[] =
  20. {
  21. CREATE_CMD_ENTRY(RASFLAG_AUTHTYPE_DEL, HandleRasflagAuthtypeDel),
  22. CREATE_CMD_ENTRY(RASFLAG_LINK_DEL, HandleRasflagLinkDel),
  23. CREATE_CMD_ENTRY(RASFLAG_MLINK_DEL, HandleRasflagMlinkDel),
  24. CREATE_CMD_ENTRY(DOMAIN_UNREGISTER, HandleDomainUnregister),
  25. };
  26. CMD_ENTRY g_RasSetCmdTable[] =
  27. {
  28. CREATE_CMD_ENTRY_EX(TRACE_SET, HandleTraceSet, CMD_FLAG_HIDDEN),
  29. CREATE_CMD_ENTRY(RASUSER_SET, HandleUserSet),
  30. CREATE_CMD_ENTRY(RASFLAG_AUTHMODE_SET, HandleRasflagAuthmodeSet),
  31. };
  32. CMD_ENTRY g_RasShowCmdTable[] =
  33. {
  34. CREATE_CMD_ENTRY_EX(TRACE_SHOW, HandleTraceShow, CMD_FLAG_HIDDEN),
  35. CREATE_CMD_ENTRY(RASUSER_SHOW, HandleUserShow),
  36. CREATE_CMD_ENTRY(RASFLAG_AUTHMODE_SHOW, HandleRasflagAuthmodeShow),
  37. CREATE_CMD_ENTRY(RASFLAG_AUTHTYPE_SHOW, HandleRasflagAuthtypeShow),
  38. CREATE_CMD_ENTRY(RASFLAG_LINK_SHOW, HandleRasflagLinkShow),
  39. CREATE_CMD_ENTRY(RASFLAG_MLINK_SHOW, HandleRasflagMlinkShow),
  40. CREATE_CMD_ENTRY(DOMAIN_SHOWREG, HandleDomainShowRegistration),
  41. CREATE_CMD_ENTRY(SHOW_SERVERS, HandleRasShowServers),
  42. CREATE_CMD_ENTRY(SHOW_CLIENT, HandleClientShow),
  43. };
  44. CMD_GROUP_ENTRY g_RasCmdGroups[] =
  45. {
  46. CREATE_CMD_GROUP_ENTRY(GROUP_ADD, g_RasAddCmdTable),
  47. CREATE_CMD_GROUP_ENTRY(GROUP_DEL, g_RasDelCmdTable),
  48. CREATE_CMD_GROUP_ENTRY(GROUP_SET, g_RasSetCmdTable),
  49. CREATE_CMD_GROUP_ENTRY(GROUP_SHOW, g_RasShowCmdTable),
  50. };
  51. ULONG g_ulNumGroups = sizeof(g_RasCmdGroups) / sizeof(CMD_GROUP_ENTRY);
  52. BOOL g_bCommit, g_bRasDirty = FALSE;
  53. GUID g_RasmontrGuid = RASMONTR_GUID, g_NetshGuid = NETSH_ROOT_GUID;
  54. DWORD g_dwNumTableEntries, g_dwParentVersion;
  55. ULONG g_ulInitCount;
  56. HANDLE g_hModule;
  57. RASMON_SERVERINFO g_ServerInfo, *g_pServerInfo = NULL;
  58. NS_CONTEXT_CONNECT_FN RasConnect;
  59. DWORD
  60. Connect(
  61. IN LPCWSTR pwszServer);
  62. DWORD
  63. WINAPI
  64. RasCommit(
  65. IN DWORD dwAction)
  66. {
  67. BOOL bCommit, bFlush = FALSE;
  68. switch(dwAction)
  69. {
  70. case NETSH_COMMIT:
  71. {
  72. if(g_bCommit)
  73. {
  74. return NO_ERROR;
  75. }
  76. g_bCommit = TRUE;
  77. break;
  78. }
  79. case NETSH_UNCOMMIT:
  80. {
  81. g_bCommit = FALSE;
  82. return NO_ERROR;
  83. }
  84. case NETSH_SAVE:
  85. {
  86. if(g_bCommit)
  87. {
  88. return NO_ERROR;
  89. }
  90. break;
  91. }
  92. case NETSH_FLUSH:
  93. {
  94. //
  95. // Action is a flush. If current state is commit, then
  96. // nothing to be done.
  97. //
  98. if(g_bCommit)
  99. {
  100. return NO_ERROR;
  101. }
  102. bFlush = TRUE;
  103. break;
  104. }
  105. default:
  106. {
  107. return NO_ERROR;
  108. }
  109. }
  110. //
  111. // Switched to commit mode. So set all valid info in the
  112. // strutures. Free memory and invalidate the info.
  113. //
  114. return NO_ERROR;
  115. }
  116. DWORD
  117. WINAPI
  118. RasStartHelper(
  119. IN CONST GUID* pguidParent,
  120. IN DWORD dwVersion)
  121. {
  122. DWORD dwErr;
  123. NS_CONTEXT_ATTRIBUTES attMyAttributes;
  124. g_dwParentVersion = dwVersion;
  125. ZeroMemory(&attMyAttributes, sizeof(attMyAttributes));
  126. attMyAttributes.pwszContext = L"ras";
  127. attMyAttributes.guidHelper = g_RasmontrGuid;
  128. attMyAttributes.dwVersion = 1;
  129. attMyAttributes.dwFlags = 0;
  130. attMyAttributes.ulNumTopCmds = 0;
  131. attMyAttributes.pTopCmds = NULL;
  132. attMyAttributes.ulNumGroups = g_ulNumGroups;
  133. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_RasCmdGroups;
  134. attMyAttributes.pfnCommitFn = RasCommit;
  135. attMyAttributes.pfnDumpFn = RasDump;
  136. attMyAttributes.pfnConnectFn = RasConnect;
  137. dwErr = RegisterContext(&attMyAttributes);
  138. return dwErr;
  139. }
  140. VOID
  141. Disconnect()
  142. {
  143. if (g_pServerInfo->hkMachine)
  144. {
  145. RegCloseKey(g_pServerInfo->hkMachine);
  146. }
  147. //
  148. // Clear out any server handles
  149. //
  150. UserServerInfoUninit(g_pServerInfo);
  151. //
  152. // Free up the server name if needed
  153. //
  154. if (g_pServerInfo->pszServer)
  155. {
  156. RutlFree(g_pServerInfo->pszServer);
  157. g_pServerInfo->pszServer = NULL;
  158. }
  159. }
  160. DWORD
  161. WINAPI
  162. RasUnInit(
  163. IN DWORD dwReserved)
  164. {
  165. if(InterlockedDecrement(&g_ulInitCount) != 0)
  166. {
  167. return NO_ERROR;
  168. }
  169. Disconnect();
  170. return NO_ERROR;
  171. }
  172. BOOL
  173. WINAPI
  174. DllMain(
  175. HINSTANCE hInstDll,
  176. DWORD fdwReason,
  177. LPVOID pReserved)
  178. {
  179. switch (fdwReason)
  180. {
  181. case DLL_PROCESS_ATTACH:
  182. {
  183. g_hModule = hInstDll;
  184. DisableThreadLibraryCalls(hInstDll);
  185. break;
  186. }
  187. case DLL_PROCESS_DETACH:
  188. {
  189. break;
  190. }
  191. default:
  192. {
  193. break;
  194. }
  195. }
  196. return TRUE;
  197. }
  198. DWORD
  199. WINAPI
  200. InitHelperDll(
  201. IN DWORD dwNetshVersion,
  202. OUT PVOID pReserved)
  203. {
  204. DWORD dwSize = 0, dwErr;
  205. NS_HELPER_ATTRIBUTES attMyAttributes;
  206. //
  207. // See if this is the first time we are being called
  208. //
  209. if(InterlockedIncrement(&g_ulInitCount) != 1)
  210. {
  211. return NO_ERROR;
  212. }
  213. g_bCommit = TRUE;
  214. //
  215. // Initialize the global server info
  216. //
  217. g_pServerInfo = &g_ServerInfo;
  218. ZeroMemory(g_pServerInfo, sizeof(RASMON_SERVERINFO));
  219. Connect(NULL);
  220. //
  221. // Register this module as a helper to the netsh root
  222. // context.
  223. //
  224. ZeroMemory( &attMyAttributes, sizeof(attMyAttributes) );
  225. attMyAttributes.dwVersion = RAS_HELPER_VERSION;
  226. attMyAttributes.guidHelper = g_RasmontrGuid;
  227. attMyAttributes.pfnStart = RasStartHelper;
  228. attMyAttributes.pfnStop = NULL;
  229. RegisterHelper( &g_NetshGuid, &attMyAttributes );
  230. //
  231. // Register any sub contexts implemented in this dll
  232. //
  233. dwErr = RasContextInstallSubContexts();
  234. if (dwErr != NO_ERROR)
  235. {
  236. RasUnInit(0);
  237. return dwErr;
  238. }
  239. return NO_ERROR;
  240. }
  241. DWORD
  242. Connect(
  243. IN LPCWSTR pwszServer)
  244. {
  245. DWORD dwErr, dwSize;
  246. do
  247. {
  248. //
  249. // Try to connect to the new router
  250. //
  251. ZeroMemory(g_pServerInfo, sizeof(RASMON_SERVERINFO));
  252. if (pwszServer)
  253. {
  254. //
  255. // Calculate the size to initialize the server name
  256. //
  257. dwSize = (wcslen(pwszServer) + 1) * sizeof(WCHAR);
  258. if (*pwszServer != g_pwszBackSlash)
  259. {
  260. dwSize += 2 * sizeof(WCHAR);
  261. }
  262. //
  263. // Allocate the server name
  264. //
  265. g_pServerInfo->pszServer = RutlAlloc(dwSize, FALSE);
  266. if(g_pServerInfo->pszServer == NULL)
  267. {
  268. dwErr = GetLastError();
  269. break;
  270. }
  271. if (*pwszServer != g_pwszBackSlash)
  272. {
  273. wcscpy(g_pServerInfo->pszServer, L"\\\\");
  274. wcscpy(g_pServerInfo->pszServer + 2, pwszServer);
  275. }
  276. else
  277. {
  278. wcscpy(g_pServerInfo->pszServer, pwszServer);
  279. }
  280. }
  281. //
  282. // Initialize the build number for the server
  283. //
  284. dwErr = RutlGetOsVersion(g_pServerInfo);
  285. if (dwErr)
  286. {
  287. break;
  288. }
  289. //
  290. // As soon as this doesn't cause a hang (bug in netcfg), readd it here.
  291. //
  292. // dwErr = UserServerInfoInit( g_pServerInfo );
  293. //
  294. } while (FALSE);
  295. return dwErr;
  296. }
  297. DWORD
  298. RasConnectToServer(
  299. IN LPCWSTR pwszServer)
  300. {
  301. DWORD dwErr = NO_ERROR, dwSize;
  302. do
  303. {
  304. if ((g_pServerInfo->pszServer != pwszServer) &&
  305. (!pwszServer || !g_pServerInfo->pszServer ||
  306. wcscmp(pwszServer, g_pServerInfo->pszServer))
  307. )
  308. {
  309. //
  310. // Disconnect from the old router
  311. //
  312. Disconnect();
  313. dwErr = Connect(pwszServer);
  314. }
  315. } while (FALSE);
  316. return dwErr;
  317. }
  318. DWORD
  319. WINAPI
  320. RasConnect(
  321. IN LPCWSTR pwszMachineName)
  322. {
  323. //
  324. // If context info is dirty, reregister it
  325. //
  326. if (g_bRasDirty)
  327. {
  328. RasStartHelper(NULL, g_dwParentVersion);
  329. }
  330. return RasConnectToServer(pwszMachineName);
  331. }
  332. DWORD
  333. Init()
  334. {
  335. //
  336. // Initialize the global server info
  337. //
  338. if (!g_pServerInfo)
  339. {
  340. g_pServerInfo = &g_ServerInfo;
  341. Connect(NULL);
  342. }
  343. return NO_ERROR;
  344. }
  345. DWORD
  346. UnInit()
  347. {
  348. if (g_pServerInfo)
  349. {
  350. Disconnect();
  351. g_pServerInfo = NULL;
  352. }
  353. return NO_ERROR;
  354. }
  355. DWORD
  356. ClearAll()
  357. {
  358. return DiagClearAll(FALSE);
  359. }
  360. DWORD
  361. GetReport(
  362. IN DWORD dwFlags,
  363. IN OUT LPCWSTR pwszString,
  364. IN OPTIONAL DiagGetReportCb pCallback,
  365. IN OPTIONAL PVOID pContext)
  366. {
  367. return DiagGetReport(dwFlags, pwszString, pCallback, pContext);
  368. }
  369. BOOL
  370. GetState()
  371. {
  372. return DiagGetState();
  373. }
  374. DWORD
  375. SetAll(
  376. IN BOOL fEnable)
  377. {
  378. return DiagSetAll(fEnable, FALSE);
  379. }
  380. DWORD
  381. SetAllRas(
  382. IN BOOL fEnable)
  383. {
  384. return DiagSetAllRas(fEnable);
  385. }
  386. DWORD
  387. WppTrace()
  388. {
  389. DiagInitWppTracing();
  390. return NO_ERROR;
  391. }
  392. DWORD
  393. GetDiagnosticFunctions(
  394. OUT RAS_DIAGNOSTIC_FUNCTIONS* pFunctions)
  395. {
  396. if (!pFunctions)
  397. {
  398. return ERROR_INVALID_PARAMETER;
  399. }
  400. pFunctions->Init = Init;
  401. pFunctions->UnInit = UnInit;
  402. pFunctions->ClearAll = ClearAll;
  403. pFunctions->GetReport = GetReport;
  404. pFunctions->GetState = GetState;
  405. pFunctions->SetAll = SetAll;
  406. pFunctions->SetAllRas = SetAllRas;
  407. pFunctions->WppTrace = WppTrace;
  408. return NO_ERROR;
  409. }