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.

449 lines
9.6 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. domhndl.c
  5. Abstract:
  6. Handlers for ras commands
  7. Revision History:
  8. pmay
  9. --*/
  10. #include "precomp.h"
  11. #pragma hdrstop
  12. //
  13. // Common data to all domain api's
  14. //
  15. typedef struct _DOMAIN_API_DATA
  16. {
  17. PWCHAR pszDomain;
  18. PWCHAR pszServer;
  19. DWORD dwLevel;
  20. } DOMAIN_API_DATA;
  21. VOID
  22. DomainFreeApiData(
  23. IN DOMAIN_API_DATA* pData)
  24. {
  25. if (pData)
  26. {
  27. if (pData->pszDomain)
  28. {
  29. RutlFree(pData->pszDomain);
  30. }
  31. if (pData->pszServer)
  32. {
  33. RutlFree(pData->pszServer);
  34. }
  35. RutlFree(pData);
  36. }
  37. }
  38. //
  39. // Generates an equivalent set of domain api data suitable for
  40. // display
  41. //
  42. DWORD
  43. DomainGeneratePrintableData(
  44. IN DOMAIN_API_DATA* pSrc,
  45. OUT DOMAIN_API_DATA** ppDst)
  46. {
  47. DOMAIN_API_DATA* pDst = NULL;
  48. DOMAIN_CONTROLLER_INFO* pDomInfo = NULL;
  49. DWORD dwErr = NO_ERROR;
  50. do
  51. {
  52. *ppDst = NULL;
  53. pDst = (DOMAIN_API_DATA*) RutlAlloc(sizeof(DOMAIN_API_DATA), TRUE);
  54. if (pDst is NULL)
  55. {
  56. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  57. break;
  58. }
  59. if (pSrc->pszDomain is NULL)
  60. {
  61. // Get the default domain
  62. //
  63. dwErr = DsGetDcName(NULL, NULL, NULL, NULL, 0, &pDomInfo);
  64. if (dwErr isnot NO_ERROR)
  65. {
  66. break;
  67. }
  68. pDst->pszDomain = RutlStrDup(pDomInfo->DomainName);
  69. }
  70. else
  71. {
  72. pDst->pszDomain = RutlStrDup(pSrc->pszDomain);
  73. }
  74. if (pSrc->pszServer is NULL)
  75. {
  76. DWORD dwSize = 0;
  77. // Find out the computer name length
  78. //
  79. GetComputerName(NULL, &dwSize);
  80. dwErr = GetLastError();
  81. dwSize = (dwSize + 1) * sizeof(WCHAR);
  82. if ( (dwErr isnot NO_ERROR) && (dwErr isnot ERROR_BUFFER_OVERFLOW) )
  83. {
  84. break;
  85. }
  86. dwErr = NO_ERROR;
  87. pDst->pszServer = (PWCHAR) RutlAlloc(dwSize, TRUE);
  88. if (pDst->pszServer is NULL)
  89. {
  90. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  91. break;
  92. }
  93. GetComputerName(pDst->pszServer, &dwSize);
  94. }
  95. else
  96. {
  97. pDst->pszServer = RutlStrDup(pSrc->pszServer);
  98. }
  99. if (pDst->pszDomain is NULL)
  100. {
  101. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  102. break;
  103. }
  104. if (pDst->pszServer is NULL)
  105. {
  106. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  107. break;
  108. }
  109. *ppDst = pDst;
  110. } while (FALSE);
  111. // Cleanup
  112. {
  113. if (dwErr isnot NO_ERROR)
  114. {
  115. DomainFreeApiData(pDst);
  116. }
  117. if (pDomInfo)
  118. {
  119. NetApiBufferFree(pDomInfo);
  120. }
  121. }
  122. return dwErr;
  123. }
  124. //
  125. // Dumps domain related configuration
  126. //
  127. DWORD
  128. DomainDumpConfig(
  129. IN HANDLE hFile
  130. )
  131. {
  132. DWORD dwErr = NO_ERROR;
  133. BOOL bRegistered = FALSE;
  134. //
  135. // Record the registration of the server
  136. //
  137. dwErr = MprDomainQueryRasServer (NULL, NULL, &bRegistered);
  138. if (dwErr is NO_ERROR)
  139. {
  140. DisplayMessageT(
  141. (bRegistered) ? DMP_DOMAIN_REGISTER
  142. : DMP_DOMAIN_UNREGISTER);
  143. DisplayMessageT(MSG_NEWLINE);
  144. }
  145. return dwErr;
  146. }
  147. DWORD
  148. DomainRegister(
  149. IN DOMAIN_API_DATA* pApiData,
  150. IN DOMAIN_API_DATA* pPrintData)
  151. {
  152. DWORD dwErr = NO_ERROR;
  153. dwErr = MprDomainRegisterRasServer (
  154. pApiData->pszDomain,
  155. pApiData->pszServer,
  156. TRUE);
  157. if (dwErr is NO_ERROR)
  158. {
  159. DisplayMessage(
  160. g_hModule,
  161. MSG_DOMAIN_REGISTER_SUCCESS,
  162. pPrintData->pszServer,
  163. pPrintData->pszDomain);
  164. }
  165. else
  166. {
  167. DisplayMessage(
  168. g_hModule,
  169. MSG_DOMAIN_REGISTER_FAIL,
  170. pPrintData->pszServer,
  171. pPrintData->pszDomain);
  172. }
  173. return dwErr;
  174. }
  175. DWORD
  176. DomainUnregister(
  177. IN DOMAIN_API_DATA* pApiData,
  178. IN DOMAIN_API_DATA* pPrintData)
  179. {
  180. DWORD dwErr = NO_ERROR;
  181. dwErr = MprDomainRegisterRasServer (
  182. pApiData->pszDomain,
  183. pApiData->pszServer,
  184. FALSE);
  185. if (dwErr is NO_ERROR)
  186. {
  187. DisplayMessage(
  188. g_hModule,
  189. MSG_DOMAIN_UNREGISTER_SUCCESS,
  190. pPrintData->pszServer,
  191. pPrintData->pszDomain);
  192. }
  193. else
  194. {
  195. DisplayMessage(
  196. g_hModule,
  197. MSG_DOMAIN_UNREGISTER_FAIL,
  198. pPrintData->pszServer,
  199. pPrintData->pszDomain);
  200. }
  201. return dwErr;
  202. }
  203. DWORD
  204. DomainShowRegistration(
  205. IN DOMAIN_API_DATA* pApiData,
  206. IN DOMAIN_API_DATA* pPrintData)
  207. {
  208. DWORD dwErr = NO_ERROR;
  209. BOOL bYes = FALSE;
  210. dwErr = MprDomainQueryRasServer (
  211. pApiData->pszDomain,
  212. pApiData->pszServer,
  213. &bYes);
  214. if (dwErr is NO_ERROR)
  215. {
  216. DisplayMessage(
  217. g_hModule,
  218. (bYes) ? MSG_DOMAIN_SHOW_REGISTERED
  219. : MSG_DOMAIN_SHOW_UNREGISTERED,
  220. pPrintData->pszServer,
  221. pPrintData->pszDomain);
  222. }
  223. else
  224. {
  225. DisplayMessage(
  226. g_hModule,
  227. MSG_DOMAIN_SHOW_REGISTER_FAIL,
  228. pPrintData->pszServer,
  229. pPrintData->pszDomain);
  230. }
  231. return dwErr;
  232. }
  233. //
  234. // Registers/unregisters an W2K machine as a ras server in the
  235. // active directory of the given domain.
  236. //
  237. DWORD
  238. HandleDomainRegistration(
  239. IN OUT LPWSTR *ppwcArguments,
  240. IN DWORD dwCurrentIndex,
  241. IN DWORD dwArgCount,
  242. IN BOOL *pbDone,
  243. IN DWORD dwMode // 0 = register, 1 = unregister, 2 = show
  244. )
  245. {
  246. DWORD dwErr = NO_ERROR;
  247. DOMAIN_API_DATA *pData = NULL, *pPrint = NULL;
  248. RASMON_CMD_ARG pArgs[] =
  249. {
  250. {
  251. RASMONTR_CMD_TYPE_STRING,
  252. {TOKEN_DOMAIN, FALSE, FALSE},
  253. NULL,
  254. 0,
  255. NULL
  256. },
  257. {
  258. RASMONTR_CMD_TYPE_STRING,
  259. {TOKEN_SERVER, FALSE, FALSE},
  260. NULL,
  261. 0,
  262. NULL
  263. }
  264. };
  265. do
  266. {
  267. // Allocate data structure
  268. //
  269. pData = (DOMAIN_API_DATA*) RutlAlloc(sizeof(DOMAIN_API_DATA), TRUE);
  270. if (pData == NULL)
  271. {
  272. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  273. break;
  274. }
  275. // Parse
  276. //
  277. dwErr = RutlParse(
  278. ppwcArguments,
  279. dwCurrentIndex,
  280. dwArgCount,
  281. NULL,
  282. pArgs,
  283. sizeof(pArgs)/sizeof(*pArgs));
  284. BREAK_ON_DWERR(dwErr);
  285. // Get the arguments
  286. //
  287. pData->pszDomain = RASMON_CMD_ARG_GetPsz(&pArgs[0]);
  288. pData->pszServer = RASMON_CMD_ARG_GetPsz(&pArgs[1]);
  289. //
  290. // Generate printable data
  291. //
  292. dwErr = DomainGeneratePrintableData(
  293. pData,
  294. &pPrint);
  295. BREAK_ON_DWERR(dwErr);
  296. // Register
  297. //
  298. if (dwMode == 0)
  299. {
  300. dwErr = DomainRegister(
  301. pData,
  302. pPrint);
  303. }
  304. else if (dwMode == 1)
  305. {
  306. dwErr = DomainUnregister(
  307. pData,
  308. pPrint);
  309. }
  310. else
  311. {
  312. dwErr = DomainShowRegistration(
  313. pData,
  314. pPrint);
  315. }
  316. BREAK_ON_DWERR(dwErr);
  317. } while (FALSE);
  318. // Cleanup
  319. {
  320. DomainFreeApiData(pData);
  321. DomainFreeApiData(pPrint);
  322. }
  323. return dwErr;
  324. }
  325. //
  326. // Registers a W2K server as ras server in active directory
  327. // of the given domain.
  328. //
  329. DWORD
  330. HandleDomainRegister(
  331. IN LPCWSTR pwszMachine,
  332. IN OUT LPWSTR *ppwcArguments,
  333. IN DWORD dwCurrentIndex,
  334. IN DWORD dwArgCount,
  335. IN DWORD dwFlags,
  336. IN LPCVOID pvData,
  337. OUT BOOL *pbDone
  338. )
  339. {
  340. return HandleDomainRegistration(
  341. ppwcArguments,
  342. dwCurrentIndex,
  343. dwArgCount,
  344. pbDone,
  345. 0);
  346. }
  347. //
  348. // Unregisters a W2K server as ras server in active directory
  349. // of the given domain.
  350. //
  351. DWORD
  352. HandleDomainUnregister(
  353. IN LPCWSTR pwszMachine,
  354. IN OUT LPWSTR *ppwcArguments,
  355. IN DWORD dwCurrentIndex,
  356. IN DWORD dwArgCount,
  357. IN DWORD dwFlags,
  358. IN LPCVOID pvData,
  359. OUT BOOL *pbDone
  360. )
  361. {
  362. return HandleDomainRegistration(
  363. ppwcArguments,
  364. dwCurrentIndex,
  365. dwArgCount,
  366. pbDone,
  367. 1);
  368. }
  369. //
  370. // Shows whether the given computer is registered
  371. // in the given domain
  372. //
  373. DWORD
  374. HandleDomainShowRegistration(
  375. IN LPCWSTR pwszMachine,
  376. IN OUT LPWSTR *ppwcArguments,
  377. IN DWORD dwCurrentIndex,
  378. IN DWORD dwArgCount,
  379. IN DWORD dwFlags,
  380. IN LPCVOID pvData,
  381. OUT BOOL *pbDone
  382. )
  383. {
  384. return HandleDomainRegistration(
  385. ppwcArguments,
  386. dwCurrentIndex,
  387. dwArgCount,
  388. pbDone,
  389. 2);
  390. }