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.

553 lines
17 KiB

  1. /*++
  2. Copyright (c) 1997 - 1997 Microsoft Corporation
  3. Module Name:
  4. joindom.c
  5. Abstract:
  6. Unit test for NetJoinDomain and NetUnjoinDomain APIs
  7. Author:
  8. Mac McLain (MacM) 19-Feb-1997
  9. Environment:
  10. User mode only.
  11. Revision History:
  12. --*/
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <windows.h>
  17. #include <lmcons.h>
  18. #include <lmerr.h>
  19. #include <lmjoin.h>
  20. #include <lmapibuf.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #define JOIN_TAG "join"
  25. #define UNJOIN_TAG "unjoin"
  26. #define NAME_TAG "name"
  27. #define RENAME_TAG "rename"
  28. #define INFO_TAG "info"
  29. #define OU_TAG "ous"
  30. #define ADD_ALT_NAME "addaltname"
  31. #define DEL_ALT_NAME "delaltname"
  32. #define SET_PRI_NAME "setpriname"
  33. #define GET_PRI_NAME "getpriname"
  34. #define GET_ALT_NAMES "getaltnames"
  35. #define GET_ALL_NAMES "getallnames"
  36. NET_API_STATUS
  37. NET_API_FUNCTION
  38. NetpGetListOfJoinableOUs(
  39. IN LPWSTR Domain,
  40. IN LPWSTR Account,
  41. IN LPWSTR Password,
  42. OUT PULONG Count,
  43. OUT PWSTR **OUs
  44. );
  45. NET_API_STATUS
  46. NET_API_FUNCTION
  47. NetpCreateComputerObjectInOU(
  48. IN LPWSTR DC,
  49. IN LPWSTR OU,
  50. IN LPWSTR ComputerName,
  51. IN LPWSTR Account,
  52. IN LPWSTR Password,
  53. IN LPWSTR MachinePassword
  54. );
  55. void
  56. Usage(
  57. VOID
  58. )
  59. /*++
  60. Routine Description:
  61. Displays the expected usage
  62. Arguments:
  63. None
  64. Return Value:
  65. VOID
  66. --*/
  67. {
  68. printf("joindom -operation options domain/server [account] [password] [new/alt name] [OU]\n");
  69. printf(" where:\n");
  70. printf(" operation indicates what joindom should do:\n");
  71. printf(" -%s Joins a workstation to a domain/workgroup\n", JOIN_TAG);
  72. printf(" -%s Unjoins a workstation from a domain\n", UNJOIN_TAG);
  73. printf(" -%s Renames the machine to a new name\n", RENAME_TAG);
  74. printf(" -%s Validates the give name for validity\n", NAME_TAG);
  75. printf(" -%s Returns information about the state of the join\n", INFO_TAG );
  76. printf(" -%s Returns information about the joinable ous\n", OU_TAG );
  77. printf(" -%s Add alternative computer name\n", ADD_ALT_NAME );
  78. printf(" -%s Delete alternative computer name\n", DEL_ALT_NAME );
  79. printf(" -%s Set primary computer name\n", SET_PRI_NAME );
  80. printf(" -%s Get primary computer name\n", GET_PRI_NAME );
  81. printf(" -%s Get altername computer names\n", GET_ALT_NAMES );
  82. printf(" -%s Get all computer names\n", GET_ALL_NAMES );
  83. printf(" options specifies the flags for the command\n");
  84. printf(" 0x000000001 Joins a domain instead of workgroup\n");
  85. printf(" 0x000000002 Creates the account on the domain\n");
  86. printf(" 0x000000004 Deletes the account when leaving a domain\n");
  87. printf(" domain/server specifies the domain (or server for alt name APIs) path to operate on\n" );
  88. printf(" [account] is the name of the account to use for accessing domain objects\n");
  89. printf(" [password] is the password of the account to use while "
  90. "accessing domain objects\n");
  91. printf(" [newname] is the new computer name \n");
  92. printf(" [OU] is the name of the ou to create the machine account in\n");
  93. }
  94. int __cdecl
  95. main(
  96. IN int argc,
  97. IN char ** argv
  98. )
  99. /*++
  100. Routine Description:
  101. See main comment.
  102. Arguments:
  103. argc - the number of command-line arguments.
  104. argv - an array of pointers to the arguments.
  105. Return Value:
  106. Exit status
  107. --*/
  108. {
  109. NET_API_STATUS NetStatus = NERR_Success;
  110. WCHAR wszDomain[MAX_PATH + 1];
  111. WCHAR rgwszOpts[3][MAX_PATH + 1];
  112. PWSTR ppwszOpts[3] = { NULL, NULL, NULL };
  113. PWSTR ppwszJoinTypes[ ] = { L"NetSetupUnknownStatus",
  114. L"NetSetupUnjoined",
  115. L"NetSetupWorkgroupName",
  116. L"NetSetupDomainName" };
  117. WCHAR wszMachine[MAX_COMPUTERNAME_LENGTH + 1];
  118. PWSTR pwszJoinInfo;
  119. WCHAR wszInfo[256];
  120. ULONG i, Options = 0, cLen, Count;
  121. PWSTR *OUs;
  122. PSTR pszNext;
  123. NETSETUP_JOIN_STATUS JoinType;
  124. BOOLEAN EnumerateComputerName = FALSE;
  125. NET_COMPUTER_NAME_TYPE ComputerNameType;
  126. LPSTR ComputerNameTypeString = NULL;
  127. BOOLEAN PrintUsage = FALSE;
  128. if (argc < 4) {
  129. PrintUsage = TRUE;
  130. goto Cleanup;
  131. } else {
  132. cLen = MAX_COMPUTERNAME_LENGTH + 1;
  133. if ( GetComputerNameW( wszMachine, &cLen ) == FALSE ) {
  134. NetStatus = GetLastError();
  135. printf("Failed to get the local computer name: %lu\n", NetStatus );
  136. }
  137. }
  138. if ( NetStatus == NERR_Success) {
  139. //
  140. // Process the command line
  141. //
  142. Options = strtoul( argv[2], &pszNext, 0 );
  143. mbstowcs( wszDomain, argv[3], strlen(argv[3]) + 1 );
  144. for ( i = 4 ; i < (ULONG)argc && i < 7 ; i++ ) {
  145. mbstowcs(rgwszOpts[i - 4], argv[i], strlen( argv[i] ) + 1 );
  146. ppwszOpts[i - 4] = rgwszOpts[i - 4];
  147. }
  148. }
  149. //
  150. // Now, do the operations
  151. //
  152. if ( NetStatus == NERR_Success && (*argv[1] == '/' || *argv[1] == '-' ) ) {
  153. if ( _stricmp( argv[1] + 1, JOIN_TAG ) == 0 ) {
  154. //
  155. // Do the call
  156. //
  157. NetStatus = NetJoinDomain( wszMachine, wszDomain, ppwszOpts[2],
  158. ppwszOpts[0], ppwszOpts[1],
  159. Options );
  160. if ( NetStatus != NERR_Success ) {
  161. printf( "NetJoinDomain [%ws] [%ws] [%ws] [0x%lx] failed with %lu\n",
  162. wszDomain,
  163. ppwszOpts[0] == NULL ? L"NULL" : ppwszOpts[0],
  164. ppwszOpts[1] == NULL ? L"NULL" : ppwszOpts[1],
  165. Options,
  166. NetStatus );
  167. }
  168. } else if ( _stricmp( argv[1] + 1, UNJOIN_TAG ) == 0 ) {
  169. NetStatus = NetUnjoinDomain( NULL, ppwszOpts[0], ppwszOpts[1], Options );
  170. if ( NetStatus != NERR_Success ) {
  171. printf( "NetUnjoinDomain [%ws] [%ws] [0x%lx] failed with %lu\n",
  172. ppwszOpts[0] == NULL ? L"NULL" : ppwszOpts[0],
  173. ppwszOpts[1] == NULL ? L"NULL" : ppwszOpts[1],
  174. Options,
  175. NetStatus );
  176. }
  177. } else if ( _stricmp( argv[1] + 1, RENAME_TAG ) == 0 ) {
  178. //
  179. // First, set the computer name
  180. //
  181. if ( ppwszOpts[2] == NULL ) {
  182. PrintUsage = TRUE;
  183. goto Cleanup;
  184. } else {
  185. if( SetComputerName( ppwszOpts[2] ) == FALSE ) {
  186. NetStatus = GetLastError();
  187. } else {
  188. NetStatus = NetRenameMachineInDomain( NULL, ppwszOpts[ 2 ],
  189. ppwszOpts[0], ppwszOpts[1],
  190. Options );
  191. if ( NetStatus != NERR_Success ) {
  192. printf( "NetRenameMachineInDomain [%ws] [%ws] [%ws] [%ws] [0x%lx] failed with %lu\n",
  193. ppwszOpts[2],
  194. wszDomain,
  195. ppwszOpts[0] == NULL ? L"NULL" : ppwszOpts[0],
  196. ppwszOpts[1] == NULL ? L"NULL" : ppwszOpts[1],
  197. Options,
  198. NetStatus );
  199. }
  200. }
  201. }
  202. } else if ( _stricmp( argv[1] + 1, NAME_TAG ) == 0 ) {
  203. //
  204. // Validate name as all four types
  205. //
  206. if ( NetStatus == NERR_Success ) {
  207. NetStatus = NetValidateName( wszMachine, wszDomain, ppwszOpts[0], ppwszOpts[1],
  208. NetSetupMachine );
  209. if( NetStatus == NERR_Success ) {
  210. printf( "Name %ws valid as machine name\n", wszDomain );
  211. } else {
  212. printf( "Name %ws invalid as machine name: %lu\n",
  213. wszDomain, NetStatus );
  214. }
  215. //
  216. // workgroup name
  217. //
  218. NetStatus = NetValidateName( wszMachine, wszDomain, ppwszOpts[0], ppwszOpts[1],
  219. NetSetupWorkgroup );
  220. if( NetStatus == NERR_Success ) {
  221. printf( "Name %ws valid as workgroup name\n", wszDomain );
  222. } else {
  223. printf( "Name %ws invalid as workgroup name: %lu\n",
  224. wszDomain, NetStatus );
  225. }
  226. //
  227. // Domain name
  228. //
  229. NetStatus = NetValidateName( wszMachine, wszDomain, ppwszOpts[0], ppwszOpts[1],
  230. NetSetupDomain );
  231. if( NetStatus == NERR_Success ) {
  232. printf( "Name %ws valid as domain name\n", wszDomain );
  233. } else {
  234. printf( "Name %ws invalid as domain name: %lu\n",
  235. wszDomain, NetStatus );
  236. }
  237. //
  238. // Domain name
  239. //
  240. NetStatus = NetValidateName( wszMachine, wszDomain, ppwszOpts[0], ppwszOpts[1],
  241. NetSetupNonExistentDomain );
  242. if( NetStatus == NERR_Success ) {
  243. printf( "Name %ws valid as non existent domain name\n", wszDomain );
  244. } else {
  245. printf( "Name %ws invalid as nonexistent domain name: %lu\n",
  246. wszDomain, NetStatus );
  247. }
  248. }
  249. } else if ( _stricmp( argv[1] + 1, INFO_TAG ) == 0 ) {
  250. //
  251. // Call it 3 times, 1: NULL buffer, 2: valid ptr, 3: remotely
  252. //
  253. #if 0
  254. NetStatus = NetGetJoinInformation( NULL, NULL, &JoinType );
  255. if ( NetStatus == ERROR_INVALID_PARAMETER ) {
  256. NetStatus = NERR_Success;
  257. printf( "JoinInfo with NULL buffer returned correctly\n" );
  258. } else {
  259. printf( "JoinInfo with NULL buffer incorrectly returned %lu\n", NetStatus );
  260. NetStatus = ERROR_INVALID_DATA;
  261. }
  262. #endif
  263. NetStatus = NetGetJoinInformation( NULL, &pwszJoinInfo, &JoinType );
  264. if ( NetStatus == NERR_Success ) {
  265. printf( "Join type: %ws\n", ppwszJoinTypes[ JoinType ] );
  266. printf( "Joined to %ws\n", pwszJoinInfo );
  267. NetApiBufferFree( pwszJoinInfo );
  268. } else {
  269. printf(" NetGetJoinInformation, failed with %lu\n", NetStatus );
  270. }
  271. #if 0
  272. NetStatus = NetGetJoinInformation( wszMachine, &pwszJoinInfo, &JoinType );
  273. if ( NetStatus == NERR_Success ) {
  274. printf( "Joined to %ws\n", pwszJoinInfo );
  275. NetApiBufferFree( pwszJoinInfo );
  276. } else {
  277. printf(" NetGetJoinInformation, remotely, failed with %lu\n", NetStatus );
  278. }
  279. #endif
  280. } else if ( _stricmp( argv[1] + 1, OU_TAG ) == 0 ) {
  281. NetStatus = NetGetJoinableOUs( NULL, ( LPCWSTR )wszDomain, ( LPCWSTR )ppwszOpts[0],
  282. ( LPCWSTR )ppwszOpts[1], &Count, &OUs );
  283. if ( NetStatus != NERR_Success ) {
  284. printf( "NetGetJoinableOUs returned %lu\n", NetStatus );
  285. } else {
  286. printf( "NetGetJoinableOUs returned %lu ous\n", Count );
  287. for ( i = 0; i < Count; i++ ) {
  288. printf(" %ws\n", OUs[ i ] );
  289. }
  290. NetApiBufferFree( OUs );
  291. }
  292. } else if ( _stricmp( argv[1] + 1, ADD_ALT_NAME ) == 0 ) {
  293. printf( "\nAdding alternate computer name with following parameters:\n" );
  294. printf( " Server: %ws\n", wszDomain );
  295. printf( " Alternate name: %ws\n", ppwszOpts[2] );
  296. printf( " Options: 0x%lx\n", Options );
  297. printf( " Account: %ws\n", ppwszOpts[0] );
  298. printf( " Password: %ws\n\n", ppwszOpts[1] );
  299. NetStatus = NetAddAlternateComputerName(
  300. wszDomain, // treated as server name
  301. ppwszOpts[2], // alternate name
  302. ppwszOpts[0], // domain account
  303. ppwszOpts[1], // domain account pwd
  304. Options );
  305. if ( NetStatus != NERR_Success ) {
  306. printf( "NetAddAlternateComputerName failed: 0x%lx\n", NetStatus );
  307. }
  308. } else if ( _stricmp( argv[1] + 1, DEL_ALT_NAME ) == 0 ) {
  309. printf( "\nDeleting alternate computer name with following parameters:\n" );
  310. printf( " Server: %ws\n", wszDomain );
  311. printf( " Alternate name: %ws\n", ppwszOpts[2] );
  312. printf( " Options: 0x%lx\n", Options );
  313. printf( " Account: %ws\n", ppwszOpts[0] );
  314. printf( " Password: %ws\n\n", ppwszOpts[1] );
  315. NetStatus = NetRemoveAlternateComputerName(
  316. wszDomain, // treated as server name
  317. ppwszOpts[2], // alternate name
  318. ppwszOpts[0], // domain account
  319. ppwszOpts[1], // domain account pwd
  320. Options );
  321. if ( NetStatus != NERR_Success ) {
  322. printf( "NetRemoveAlternateComputerName failed: 0x%lx\n", NetStatus );
  323. }
  324. } else if ( _stricmp( argv[1] + 1, SET_PRI_NAME ) == 0 ) {
  325. printf( "\nSetting primary computer name with following parameters:\n" );
  326. printf( " Server: %ws\n", wszDomain );
  327. printf( " Primary name: %ws\n", ppwszOpts[2] );
  328. printf( " Options: 0x%lx\n", Options );
  329. printf( " Account: %ws\n", ppwszOpts[0] );
  330. printf( " Password: %ws\n\n", ppwszOpts[1] );
  331. NetStatus = NetSetPrimaryComputerName(
  332. wszDomain, // treated as server name
  333. ppwszOpts[2], // alternate name
  334. ppwszOpts[0], // domain account
  335. ppwszOpts[1], // domain account pwd
  336. Options );
  337. if ( NetStatus != NERR_Success ) {
  338. printf( "NetSetPrimaryComputerName failed: 0x%lx\n", NetStatus );
  339. }
  340. } else if ( _stricmp( argv[1] + 1, GET_PRI_NAME ) == 0 ) {
  341. EnumerateComputerName = TRUE;
  342. ComputerNameType = NetPrimaryComputerName;
  343. ComputerNameTypeString = "primary";
  344. } else if ( _stricmp( argv[1] + 1, GET_ALT_NAMES ) == 0 ) {
  345. EnumerateComputerName = TRUE;
  346. ComputerNameType = NetAlternateComputerNames;
  347. ComputerNameTypeString = "alternate";
  348. } else if ( _stricmp( argv[1] + 1, GET_ALL_NAMES ) == 0 ) {
  349. EnumerateComputerName = TRUE;
  350. ComputerNameType = NetAllComputerNames;
  351. ComputerNameTypeString = "all";
  352. } else {
  353. PrintUsage = TRUE;
  354. goto Cleanup;
  355. }
  356. } else {
  357. PrintUsage = TRUE;
  358. goto Cleanup;
  359. }
  360. if ( EnumerateComputerName ) {
  361. ULONG EntryCount = 0;
  362. LPWSTR *ComputerNames = NULL;
  363. printf( "\nGetting %s computer name(s) for server %ws with options 0x%lx:\n",
  364. ComputerNameTypeString,
  365. wszDomain, // treated as server name
  366. Options );
  367. NetStatus = NetEnumerateComputerNames(
  368. wszDomain, // treated as server name
  369. ComputerNameType, // name type
  370. Options,
  371. &EntryCount,
  372. &ComputerNames );
  373. if ( NetStatus != NERR_Success ) {
  374. printf( "NetEnumerateComputerNames failed: 0x%lx\n", NetStatus );
  375. } else {
  376. ULONG i;
  377. if ( EntryCount > 0 ) {
  378. for ( i = 0; i < EntryCount; i++ ) {
  379. printf( "%ws\n", ComputerNames[i] );
  380. }
  381. } else {
  382. printf( "No names returned.\n" );
  383. }
  384. NetApiBufferFree( ComputerNames );
  385. }
  386. }
  387. Cleanup:
  388. if ( PrintUsage ) {
  389. Usage();
  390. }
  391. if ( NetStatus == NERR_Success ) {
  392. printf( "The command completed successfully\n" );
  393. } else {
  394. printf( "The command failed with error %lu\n", NetStatus );
  395. }
  396. return( NetStatus == NERR_Success );
  397. }