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.

370 lines
11 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 2001
  5. //
  6. // File: CompName.h
  7. //
  8. // Contents: Definitions for the computer name management code.
  9. //
  10. // History: 20-April-2001 EricB Created
  11. //
  12. //-----------------------------------------------------------------------------
  13. #include "pch.h"
  14. #pragma hdrstop
  15. #include "netdom.h"
  16. #include "CompName.h"
  17. //+----------------------------------------------------------------------------
  18. //
  19. // Function: NetDomComputerNames
  20. //
  21. // Synopsis: Entry point for the computer name command.
  22. //
  23. // Arguments: [rgNetDomArgs] - The command line argument array.
  24. //
  25. //-----------------------------------------------------------------------------
  26. DWORD
  27. NetDomComputerNames(ARG_RECORD * rgNetDomArgs)
  28. {
  29. DWORD Win32Err = ERROR_SUCCESS;
  30. Win32Err = NetDompValidateSecondaryArguments(rgNetDomArgs,
  31. eObject,
  32. eCommUserNameO,
  33. eCommPasswordO,
  34. eCommUserNameD,
  35. eCommPasswordD,
  36. eCommAdd,
  37. eCommRemove,
  38. eCompNameMakePri,
  39. eCompNameEnum,
  40. eCommVerbose,
  41. eArgEnd);
  42. if (ERROR_SUCCESS != Win32Err)
  43. {
  44. DisplayHelp(ePriCompName);
  45. return Win32Err;
  46. }
  47. PWSTR pwzMachine = rgNetDomArgs[eObject].strValue;
  48. if (!pwzMachine)
  49. {
  50. DisplayHelp(ePriCompName);
  51. return ERROR_INVALID_PARAMETER;
  52. }
  53. //
  54. // Get the users and passwords if they were entered.
  55. //
  56. ND5_AUTH_INFO MachineUser = {0}, DomainUser = {0};
  57. if (CmdFlagOn(rgNetDomArgs, eCommUserNameO))
  58. {
  59. Win32Err = NetDompGetUserAndPasswordForOperation(rgNetDomArgs,
  60. eCommUserNameO,
  61. pwzMachine,
  62. &MachineUser);
  63. if (ERROR_SUCCESS != Win32Err)
  64. {
  65. DisplayHelp(ePriCompName);
  66. return Win32Err;
  67. }
  68. }
  69. if (CmdFlagOn(rgNetDomArgs, eCommUserNameD))
  70. {
  71. Win32Err = NetDompGetUserAndPasswordForOperation(rgNetDomArgs,
  72. eCommUserNameD,
  73. pwzMachine,
  74. &DomainUser);
  75. if (ERROR_SUCCESS != Win32Err)
  76. {
  77. DisplayHelp(ePriCompName);
  78. goto CompNameExit;
  79. }
  80. }
  81. //
  82. // See which name operation is specified.
  83. //
  84. bool fHaveOp = false;
  85. PWSTR pwzOp = NULL;
  86. NETDOM_ARG_ENUM eOp = eArgNull, eBadOp = eArgNull;
  87. if (CmdFlagOn(rgNetDomArgs, eCommAdd))
  88. {
  89. Win32Err = NetDompGetArgumentString(rgNetDomArgs,
  90. eCommAdd,
  91. &pwzOp);
  92. if (NO_ERROR == Win32Err)
  93. {
  94. eOp = eCommAdd;
  95. }
  96. }
  97. if (CmdFlagOn(rgNetDomArgs, eCommRemove))
  98. {
  99. Win32Err = NetDompGetArgumentString(rgNetDomArgs,
  100. eCommRemove,
  101. &pwzOp);
  102. if (NO_ERROR == Win32Err)
  103. {
  104. if (eArgNull == eOp)
  105. {
  106. eOp = eCommRemove;
  107. }
  108. else
  109. {
  110. eBadOp = eCommRemove;
  111. }
  112. }
  113. }
  114. if (CmdFlagOn(rgNetDomArgs, eCompNameMakePri))
  115. {
  116. Win32Err = NetDompGetArgumentString(rgNetDomArgs,
  117. eCompNameMakePri,
  118. &pwzOp);
  119. if (NO_ERROR == Win32Err)
  120. {
  121. if (eArgNull == eOp)
  122. {
  123. eOp = eCompNameMakePri;
  124. }
  125. else
  126. {
  127. eBadOp = eCompNameMakePri;
  128. }
  129. }
  130. }
  131. if (CmdFlagOn(rgNetDomArgs, eCompNameEnum))
  132. {
  133. Win32Err = NetDompGetArgumentString(rgNetDomArgs,
  134. eCompNameEnum,
  135. &pwzOp);
  136. if (NO_ERROR == Win32Err)
  137. {
  138. if (eArgNull == eOp)
  139. {
  140. eOp = eCompNameEnum;
  141. }
  142. else
  143. {
  144. eBadOp = eCompNameEnum;
  145. }
  146. }
  147. }
  148. if (eArgNull != eBadOp)
  149. {
  150. ASSERT(rgNetDomArgs[eBadOp].strArg1);
  151. NetDompDisplayUnexpectedParameter(rgNetDomArgs[eBadOp].strArg1);
  152. DisplayHelp(ePriCompName);
  153. Win32Err = ERROR_INVALID_PARAMETER;
  154. goto CompNameExit;
  155. }
  156. if (eArgNull == eOp)
  157. {
  158. DisplayHelp(ePriCompName);
  159. Win32Err = ERROR_INVALID_PARAMETER;
  160. goto CompNameExit;
  161. }
  162. if (CmdFlagOn(rgNetDomArgs, eCommUserNameO))
  163. {
  164. LOG_VERBOSE((MSG_VERBOSE_ESTABLISH_SESSION, pwzMachine));
  165. Win32Err = NetpManageIPCConnect(pwzMachine,
  166. MachineUser.User,
  167. MachineUser.Password,
  168. NETSETUPP_CONNECT_IPC);
  169. }
  170. if (NO_ERROR != Win32Err)
  171. {
  172. goto CompNameExit;
  173. }
  174. //
  175. // Do the operation.
  176. //
  177. switch (eOp)
  178. {
  179. case eCommAdd:
  180. if (!rgNetDomArgs[eCommAdd].strValue ||
  181. !wcslen(rgNetDomArgs[eCommAdd].strValue))
  182. {
  183. DisplayHelp(ePriCompName);
  184. Win32Err = ERROR_INVALID_PARAMETER;
  185. goto CompNameExit;
  186. }
  187. Win32Err = NetAddAlternateComputerName(pwzMachine,
  188. rgNetDomArgs[eCommAdd].strValue,
  189. DomainUser.User,
  190. DomainUser.Password,
  191. 0);
  192. if (NO_ERROR == Win32Err)
  193. {
  194. NetDompDisplayMessage(MSG_COMPNAME_ADD, rgNetDomArgs[eCommAdd].strValue);
  195. }
  196. else
  197. {
  198. NetDompDisplayMessage(MSG_COMPNAME_ADD_FAIL, rgNetDomArgs[eCommAdd].strValue);
  199. NetDompDisplayErrorMessage(Win32Err);
  200. }
  201. break;
  202. case eCommRemove:
  203. if (!rgNetDomArgs[eCommRemove].strValue ||
  204. !wcslen(rgNetDomArgs[eCommRemove].strValue))
  205. {
  206. DisplayHelp(ePriCompName);
  207. Win32Err = ERROR_INVALID_PARAMETER;
  208. goto CompNameExit;
  209. }
  210. Win32Err = NetRemoveAlternateComputerName(pwzMachine,
  211. rgNetDomArgs[eCommRemove].strValue,
  212. DomainUser.User,
  213. DomainUser.Password,
  214. 0);
  215. if (NO_ERROR == Win32Err)
  216. {
  217. NetDompDisplayMessage(MSG_COMPNAME_REM, rgNetDomArgs[eCommRemove].strValue);
  218. }
  219. else
  220. {
  221. NetDompDisplayMessage(MSG_COMPNAME_REM_FAIL, rgNetDomArgs[eCommRemove].strValue);
  222. NetDompDisplayErrorMessage(Win32Err);
  223. }
  224. break;
  225. case eCompNameMakePri:
  226. if (!rgNetDomArgs[eCompNameMakePri].strValue ||
  227. !wcslen(rgNetDomArgs[eCompNameMakePri].strValue))
  228. {
  229. DisplayHelp(ePriCompName);
  230. Win32Err = ERROR_INVALID_PARAMETER;
  231. goto CompNameExit;
  232. }
  233. Win32Err = NetSetPrimaryComputerName(pwzMachine,
  234. rgNetDomArgs[eCompNameMakePri].strValue,
  235. DomainUser.User,
  236. DomainUser.Password,
  237. 0);
  238. if (NO_ERROR == Win32Err)
  239. {
  240. NetDompDisplayMessage(MSG_COMPNAME_MAKEPRI, rgNetDomArgs[eCompNameMakePri].strValue);
  241. }
  242. else
  243. {
  244. NetDompDisplayMessage(MSG_COMPNAME_MAKEPRI_FAIL, rgNetDomArgs[eCompNameMakePri].strValue);
  245. NetDompDisplayErrorMessage(Win32Err);
  246. }
  247. break;
  248. case eCompNameEnum:
  249. {
  250. NET_COMPUTER_NAME_TYPE NameType = NetAllComputerNames;
  251. WCHAR wzBuf[MAX_PATH+1];
  252. DWORD dwMsgID = MSG_COMPNAME_ENUMALL;
  253. if (rgNetDomArgs[eCompNameEnum].strValue &&
  254. wcslen(rgNetDomArgs[eCompNameEnum].strValue))
  255. {
  256. if (!LoadString(g_hInstance, IDS_ENUM_ALT, wzBuf, MAX_PATH))
  257. {
  258. Win32Err = GetLastError();
  259. goto CompNameExit;
  260. }
  261. if (_wcsicmp(wzBuf, rgNetDomArgs[eCompNameEnum].strValue) == 0)
  262. {
  263. NameType = NetAlternateComputerNames;
  264. dwMsgID = MSG_COMPNAME_ENUMALT;
  265. }
  266. else
  267. {
  268. if (!LoadString(g_hInstance, IDS_ENUM_PRI, wzBuf, MAX_PATH))
  269. {
  270. Win32Err = GetLastError();
  271. goto CompNameExit;
  272. }
  273. if (_wcsicmp(wzBuf, rgNetDomArgs[eCompNameEnum].strValue) == 0)
  274. {
  275. NameType = NetPrimaryComputerName;
  276. dwMsgID = MSG_COMPNAME_ENUMPRI;
  277. }
  278. else
  279. {
  280. if (!LoadString(g_hInstance, IDS_ENUM_ALL, wzBuf, MAX_PATH))
  281. {
  282. Win32Err = GetLastError();
  283. goto CompNameExit;
  284. }
  285. if (_wcsicmp(wzBuf, rgNetDomArgs[eCompNameEnum].strValue) == 0)
  286. {
  287. NameType = NetAllComputerNames;
  288. dwMsgID = MSG_COMPNAME_ENUMALL;
  289. }
  290. else
  291. {
  292. NetDompDisplayUnexpectedParameter(rgNetDomArgs[eCompNameEnum].strValue);
  293. DisplayHelp(ePriCompName);
  294. Win32Err = ERROR_INVALID_PARAMETER;
  295. goto CompNameExit;
  296. }
  297. }
  298. }
  299. }
  300. DWORD dwCount = 0;
  301. PWSTR * rgpwzNames = NULL;
  302. DBG_VERBOSE(("NetEnumerateComputerNames(%ws, %d, 0, etc)\n", pwzMachine, NameType));
  303. Win32Err = NetEnumerateComputerNames(pwzMachine,
  304. NameType,
  305. 0,
  306. &dwCount,
  307. &rgpwzNames);
  308. if (NO_ERROR != Win32Err)
  309. {
  310. NetDompDisplayErrorMessage(Win32Err);
  311. goto CompNameExit;
  312. }
  313. NetDompDisplayMessage(dwMsgID);
  314. for (DWORD i = 0; i < dwCount; i++)
  315. {
  316. ASSERT(rgpwzNames[i]);
  317. printf("%ws\n", rgpwzNames[i]);
  318. }
  319. if (rgpwzNames)
  320. {
  321. NetApiBufferFree(rgpwzNames);
  322. }
  323. }
  324. break;
  325. default:
  326. ASSERT(FALSE);
  327. Win32Err = ERROR_INVALID_PARAMETER;
  328. goto CompNameExit;
  329. }
  330. CompNameExit:
  331. if (CmdFlagOn(rgNetDomArgs, eCommUserNameO))
  332. {
  333. LOG_VERBOSE((MSG_VERBOSE_DELETE_SESSION, pwzMachine));
  334. NetpManageIPCConnect(pwzMachine,
  335. MachineUser.User,
  336. MachineUser.Password,
  337. NETSETUPP_DISCONNECT_IPC);
  338. }
  339. NetDompFreeAuthIdent(&MachineUser);
  340. NetDompFreeAuthIdent(&DomainUser);
  341. return Win32Err;
  342. }