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.

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