r18702: re-enable non-decimal specification of rid
[Samba/nascimento.git] / source3 / rpcclient / cmd_samr.c
blob6f4d027cca924bb0b400b569ab36e9ef96ef011b
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Andrew Tridgell 1992-2000,
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
7 Copyright (C) Elrond 2000,
8 Copyright (C) Tim Potter 2000
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
26 #include "rpcclient.h"
28 extern DOM_SID domain_sid;
30 /****************************************************************************
31 display sam_user_info_7 structure
32 ****************************************************************************/
33 static void display_sam_user_info_7(SAM_USER_INFO_7 *usr)
35 fstring temp;
37 unistr2_to_ascii(temp, &usr->uni_name, sizeof(temp)-1);
38 printf("\tUser Name :\t%s\n", temp);
41 /****************************************************************************
42 display sam_user_info_9 structure
43 ****************************************************************************/
44 static void display_sam_user_info_9(SAM_USER_INFO_9 *usr)
46 printf("\tPrimary group RID :\tox%x\n", usr->rid_group);
49 /****************************************************************************
50 display sam_user_info_21 structure
51 ****************************************************************************/
52 static void display_sam_user_info_21(SAM_USER_INFO_21 *usr)
54 fstring temp;
56 unistr2_to_ascii(temp, &usr->uni_user_name, sizeof(temp)-1);
57 printf("\tUser Name :\t%s\n", temp);
59 unistr2_to_ascii(temp, &usr->uni_full_name, sizeof(temp)-1);
60 printf("\tFull Name :\t%s\n", temp);
62 unistr2_to_ascii(temp, &usr->uni_home_dir, sizeof(temp)-1);
63 printf("\tHome Drive :\t%s\n", temp);
65 unistr2_to_ascii(temp, &usr->uni_dir_drive, sizeof(temp)-1);
66 printf("\tDir Drive :\t%s\n", temp);
68 unistr2_to_ascii(temp, &usr->uni_profile_path, sizeof(temp)-1);
69 printf("\tProfile Path:\t%s\n", temp);
71 unistr2_to_ascii(temp, &usr->uni_logon_script, sizeof(temp)-1);
72 printf("\tLogon Script:\t%s\n", temp);
74 unistr2_to_ascii(temp, &usr->uni_acct_desc, sizeof(temp)-1);
75 printf("\tDescription :\t%s\n", temp);
77 unistr2_to_ascii(temp, &usr->uni_workstations, sizeof(temp)-1);
78 printf("\tWorkstations:\t%s\n", temp);
80 unistr2_to_ascii(temp, &usr->uni_comment, sizeof(temp)-1);
81 printf("\tUnknown Str :\t%s\n", temp);
83 unistr2_to_ascii(temp, &usr->uni_munged_dial, sizeof(temp)-1);
84 printf("\tRemote Dial :\t%s\n", temp);
86 printf("\tLogon Time :\t%s\n",
87 http_timestring(nt_time_to_unix(usr->logon_time)));
88 printf("\tLogoff Time :\t%s\n",
89 http_timestring(nt_time_to_unix(usr->logoff_time)));
90 printf("\tKickoff Time :\t%s\n",
91 http_timestring(nt_time_to_unix(usr->kickoff_time)));
92 printf("\tPassword last set Time :\t%s\n",
93 http_timestring(nt_time_to_unix(usr->pass_last_set_time)));
94 printf("\tPassword can change Time :\t%s\n",
95 http_timestring(nt_time_to_unix(usr->pass_can_change_time)));
96 printf("\tPassword must change Time:\t%s\n",
97 http_timestring(nt_time_to_unix(usr->pass_must_change_time)));
99 printf("\tunknown_2[0..31]...\n"); /* user passwords? */
101 printf("\tuser_rid :\t0x%x\n" , usr->user_rid ); /* User ID */
102 printf("\tgroup_rid:\t0x%x\n" , usr->group_rid); /* Group ID */
103 printf("\tacb_info :\t0x%08x\n", usr->acb_info ); /* Account Control Info */
105 printf("\tfields_present:\t0x%08x\n", usr->fields_present); /* 0x00ff ffff */
106 printf("\tlogon_divs:\t%d\n", usr->logon_divs); /* 0x0000 00a8 which is 168 which is num hrs in a week */
107 printf("\tbad_password_count:\t0x%08x\n", usr->bad_password_count);
108 printf("\tlogon_count:\t0x%08x\n", usr->logon_count);
110 printf("\tpadding1[0..7]...\n");
112 if (usr->ptr_logon_hrs) {
113 printf("\tlogon_hrs[0..%d]...\n", usr->logon_hrs.len);
118 static void display_password_properties(uint32 password_properties)
120 printf("password_properties: 0x%08x\n", password_properties);
122 if (password_properties & DOMAIN_PASSWORD_COMPLEX)
123 printf("\tDOMAIN_PASSWORD_COMPLEX\n");
125 if (password_properties & DOMAIN_PASSWORD_NO_ANON_CHANGE)
126 printf("\tDOMAIN_PASSWORD_NO_ANON_CHANGE\n");
128 if (password_properties & DOMAIN_PASSWORD_NO_CLEAR_CHANGE)
129 printf("\tDOMAIN_PASSWORD_NO_CLEAR_CHANGE\n");
131 if (password_properties & DOMAIN_LOCKOUT_ADMINS)
132 printf("\tDOMAIN_LOCKOUT_ADMINS\n");
134 if (password_properties & DOMAIN_PASSWORD_STORE_CLEARTEXT)
135 printf("\tDOMAIN_PASSWORD_STORE_CLEARTEXT\n");
137 if (password_properties & DOMAIN_REFUSE_PASSWORD_CHANGE)
138 printf("\tDOMAIN_REFUSE_PASSWORD_CHANGE\n");
141 static void display_sam_unk_info_1(SAM_UNK_INFO_1 *info1)
144 printf("Minimum password length:\t\t\t%d\n", info1->min_length_password);
145 printf("Password uniqueness (remember x passwords):\t%d\n", info1->password_history);
146 display_password_properties(info1->password_properties);
147 printf("password expire in:\t\t\t\t%s\n", display_time(info1->expire));
148 printf("Min password age (allow changing in x days):\t%s\n", display_time(info1->min_passwordage));
151 static void display_sam_unk_info_2(SAM_UNK_INFO_2 *info2)
153 fstring name;
155 unistr2_to_ascii(name, &info2->uni_domain, sizeof(name) - 1);
156 printf("Domain:\t\t%s\n", name);
158 unistr2_to_ascii(name, &info2->uni_server, sizeof(name) - 1);
159 printf("Server:\t\t%s\n", name);
161 unistr2_to_ascii(name, &info2->uni_comment, sizeof(name) - 1);
162 printf("Comment:\t%s\n", name);
164 printf("Total Users:\t%d\n", info2->num_domain_usrs);
165 printf("Total Groups:\t%d\n", info2->num_domain_grps);
166 printf("Total Aliases:\t%d\n", info2->num_local_grps);
168 printf("Sequence No:\t%lld\n", info2->seq_num);
170 printf("Force Logoff:\t%d\n", (int)nt_time_to_unix_abs(&info2->logout));
172 printf("Unknown 4:\t0x%x\n", info2->unknown_4);
173 printf("Server Role:\t%s\n", server_role_str(info2->server_role));
174 printf("Unknown 6:\t0x%x\n", info2->unknown_6);
177 static void display_sam_unk_info_3(SAM_UNK_INFO_3 *info3)
179 printf("Force Logoff:\t%d\n", (int)nt_time_to_unix_abs(&info3->logout));
182 static void display_sam_unk_info_4(SAM_UNK_INFO_4 *info4)
184 fstring name;
186 unistr2_to_ascii(name, &info4->uni_comment, sizeof(name) - 1);
187 printf("Comment:\t%s\n", name);
190 static void display_sam_unk_info_5(SAM_UNK_INFO_5 *info5)
192 fstring name;
194 unistr2_to_ascii(name, &info5->uni_domain, sizeof(name) - 1);
195 printf("Domain:\t\t%s\n", name);
198 static void display_sam_unk_info_6(SAM_UNK_INFO_6 *info6)
200 fstring name;
202 unistr2_to_ascii(name, &info6->uni_server, sizeof(name) - 1);
203 printf("Server:\t\t%s\n", name);
206 static void display_sam_unk_info_7(SAM_UNK_INFO_7 *info7)
208 printf("Server Role:\t%s\n", server_role_str(info7->server_role));
211 static void display_sam_unk_info_8(SAM_UNK_INFO_8 *info8)
213 printf("Sequence No:\t%lld\n", info8->seq_num);
214 printf("Domain Create Time:\t%s\n",
215 http_timestring(nt_time_to_unix(info8->domain_create_time)));
218 static void display_sam_unk_info_9(SAM_UNK_INFO_9 *info9)
220 printf("unknown:\t%d (0x%08x)\n", info9->unknown, info9->unknown);
223 static void display_sam_unk_info_12(SAM_UNK_INFO_12 *info12)
225 printf("Bad password lockout duration: %s\n", display_time(info12->duration));
226 printf("Reset Lockout after: %s\n", display_time(info12->reset_count));
227 printf("Lockout after bad attempts: %d\n", info12->bad_attempt_lockout);
230 static void display_sam_unk_info_13(SAM_UNK_INFO_13 *info13)
232 printf("Sequence No:\t%lld\n", info13->seq_num);
233 printf("Domain Create Time:\t%s\n",
234 http_timestring(nt_time_to_unix(info13->domain_create_time)));
235 printf("Unknown1:\t%d\n", info13->unknown1);
236 printf("Unknown2:\t%d\n", info13->unknown2);
240 static void display_sam_info_1(SAM_ENTRY1 *e1, SAM_STR1 *s1)
242 fstring tmp;
244 printf("index: 0x%x ", e1->user_idx);
245 printf("RID: 0x%x ", e1->rid_user);
246 printf("acb: 0x%x ", e1->acb_info);
248 unistr2_to_ascii(tmp, &s1->uni_acct_name, sizeof(tmp)-1);
249 printf("Account: %s\t", tmp);
251 unistr2_to_ascii(tmp, &s1->uni_full_name, sizeof(tmp)-1);
252 printf("Name: %s\t", tmp);
254 unistr2_to_ascii(tmp, &s1->uni_acct_desc, sizeof(tmp)-1);
255 printf("Desc: %s\n", tmp);
258 static void display_sam_info_2(SAM_ENTRY2 *e2, SAM_STR2 *s2)
260 fstring tmp;
262 printf("index: 0x%x ", e2->user_idx);
263 printf("RID: 0x%x ", e2->rid_user);
264 printf("acb: 0x%x ", e2->acb_info);
266 unistr2_to_ascii(tmp, &s2->uni_srv_name, sizeof(tmp)-1);
267 printf("Account: %s\t", tmp);
269 unistr2_to_ascii(tmp, &s2->uni_srv_desc, sizeof(tmp)-1);
270 printf("Name: %s\n", tmp);
274 static void display_sam_info_3(SAM_ENTRY3 *e3, SAM_STR3 *s3)
276 fstring tmp;
278 printf("index: 0x%x ", e3->grp_idx);
279 printf("RID: 0x%x ", e3->rid_grp);
280 printf("attr: 0x%x ", e3->attr);
282 unistr2_to_ascii(tmp, &s3->uni_grp_name, sizeof(tmp)-1);
283 printf("Account: %s\t", tmp);
285 unistr2_to_ascii(tmp, &s3->uni_grp_desc, sizeof(tmp)-1);
286 printf("Name: %s\n", tmp);
290 static void display_sam_info_4(SAM_ENTRY4 *e4, SAM_STR4 *s4)
292 int i;
294 printf("index: %d ", e4->user_idx);
296 printf("Account: ");
297 for (i=0; i<s4->acct_name.str_str_len; i++)
298 printf("%c", s4->acct_name.buffer[i]);
299 printf("\n");
303 static void display_sam_info_5(SAM_ENTRY5 *e5, SAM_STR5 *s5)
305 int i;
307 printf("index: 0x%x ", e5->grp_idx);
309 printf("Account: ");
310 for (i=0; i<s5->grp_name.str_str_len; i++)
311 printf("%c", s5->grp_name.buffer[i]);
312 printf("\n");
316 /****************************************************************************
317 Try samr_connect4 first, then samr_conenct if it fails
318 ****************************************************************************/
319 static NTSTATUS try_samr_connects(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
320 uint32 access_mask, POLICY_HND *connect_pol)
322 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
324 result = rpccli_samr_connect4(cli, mem_ctx, access_mask, connect_pol);
325 if (!NT_STATUS_IS_OK(result)) {
326 result = rpccli_samr_connect(cli, mem_ctx, access_mask,
327 connect_pol);
329 return result;
332 /**********************************************************************
333 * Query user information
335 static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
336 TALLOC_CTX *mem_ctx,
337 int argc, const char **argv)
339 POLICY_HND connect_pol, domain_pol, user_pol;
340 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
341 uint32 info_level = 21;
342 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
343 SAM_USERINFO_CTR *user_ctr;
344 fstring server;
345 uint32 user_rid = 0;
347 if ((argc < 2) || (argc > 4)) {
348 printf("Usage: %s rid [info level] [access mask] \n", argv[0]);
349 return NT_STATUS_OK;
352 sscanf(argv[1], "%i", &user_rid);
354 if (argc > 2)
355 sscanf(argv[2], "%i", &info_level);
357 if (argc > 3)
358 sscanf(argv[3], "%x", &access_mask);
361 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
362 strupper_m(server);
364 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
365 &connect_pol);
367 if (!NT_STATUS_IS_OK(result))
368 goto done;
370 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
371 MAXIMUM_ALLOWED_ACCESS,
372 &domain_sid, &domain_pol);
374 if (!NT_STATUS_IS_OK(result))
375 goto done;
377 result = rpccli_samr_open_user(cli, mem_ctx, &domain_pol,
378 access_mask,
379 user_rid, &user_pol);
381 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) &&
382 (user_rid == 0)) {
384 /* Probably this was a user name, try lookupnames */
385 uint32 num_rids;
386 uint32 *rids, *types;
388 result = rpccli_samr_lookup_names(cli, mem_ctx, &domain_pol,
389 1000, 1, &argv[1],
390 &num_rids, &rids,
391 &types);
393 if (NT_STATUS_IS_OK(result)) {
394 result = rpccli_samr_open_user(cli, mem_ctx,
395 &domain_pol,
396 access_mask,
397 rids[0], &user_pol);
402 if (!NT_STATUS_IS_OK(result))
403 goto done;
405 ZERO_STRUCT(user_ctr);
407 result = rpccli_samr_query_userinfo(cli, mem_ctx, &user_pol,
408 info_level, &user_ctr);
410 if (!NT_STATUS_IS_OK(result))
411 goto done;
413 switch (user_ctr->switch_value) {
414 case 21:
415 display_sam_user_info_21(user_ctr->info.id21);
416 break;
417 case 7:
418 display_sam_user_info_7(user_ctr->info.id7);
419 break;
420 case 9:
421 display_sam_user_info_9(user_ctr->info.id9);
422 break;
423 default:
424 printf("Unsupported infolevel: %d\n", info_level);
425 break;
428 rpccli_samr_close(cli, mem_ctx, &user_pol);
429 rpccli_samr_close(cli, mem_ctx, &domain_pol);
430 rpccli_samr_close(cli, mem_ctx, &connect_pol);
432 done:
433 return result;
436 /****************************************************************************
437 display group info
438 ****************************************************************************/
439 static void display_group_info1(GROUP_INFO1 *info1)
441 fstring temp;
443 unistr2_to_ascii(temp, &info1->uni_acct_name, sizeof(temp)-1);
444 printf("\tGroup Name:\t%s\n", temp);
445 unistr2_to_ascii(temp, &info1->uni_acct_desc, sizeof(temp)-1);
446 printf("\tDescription:\t%s\n", temp);
447 printf("\tGroup Attribute:%d\n", info1->group_attr);
448 printf("\tNum Members:%d\n", info1->num_members);
451 /****************************************************************************
452 display group info
453 ****************************************************************************/
454 static void display_group_info2(GROUP_INFO2 *info2)
456 fstring name;
458 unistr2_to_ascii(name, &info2->uni_acct_name, sizeof(name)-1);
459 printf("\tGroup Description:%s\n", name);
463 /****************************************************************************
464 display group info
465 ****************************************************************************/
466 static void display_group_info3(GROUP_INFO3 *info3)
468 printf("\tGroup Attribute:%d\n", info3->group_attr);
472 /****************************************************************************
473 display group info
474 ****************************************************************************/
475 static void display_group_info4(GROUP_INFO4 *info4)
477 fstring desc;
479 unistr2_to_ascii(desc, &info4->uni_acct_desc, sizeof(desc)-1);
480 printf("\tGroup Description:%s\n", desc);
483 /****************************************************************************
484 display group info
485 ****************************************************************************/
486 static void display_group_info5(GROUP_INFO5 *info5)
488 fstring temp;
490 unistr2_to_ascii(temp, &info5->uni_acct_name, sizeof(temp)-1);
491 printf("\tGroup Name:\t%s\n", temp);
492 unistr2_to_ascii(temp, &info5->uni_acct_desc, sizeof(temp)-1);
493 printf("\tDescription:\t%s\n", temp);
494 printf("\tGroup Attribute:%d\n", info5->group_attr);
495 printf("\tNum Members:%d\n", info5->num_members);
498 /****************************************************************************
499 display sam sync structure
500 ****************************************************************************/
501 static void display_group_info_ctr(GROUP_INFO_CTR *ctr)
503 switch (ctr->switch_value1) {
504 case 1:
505 display_group_info1(&ctr->group.info1);
506 break;
507 case 2:
508 display_group_info2(&ctr->group.info2);
509 break;
510 case 3:
511 display_group_info3(&ctr->group.info3);
512 break;
513 case 4:
514 display_group_info4(&ctr->group.info4);
515 break;
516 case 5:
517 display_group_info5(&ctr->group.info5);
518 break;
523 /***********************************************************************
524 * Query group information
526 static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
527 TALLOC_CTX *mem_ctx,
528 int argc, const char **argv)
530 POLICY_HND connect_pol, domain_pol, group_pol;
531 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
532 uint32 info_level = 1;
533 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
534 GROUP_INFO_CTR *group_ctr;
535 fstring server;
536 uint32 group_rid;
538 if ((argc < 2) || (argc > 4)) {
539 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
540 return NT_STATUS_OK;
543 sscanf(argv[1], "%i", &group_rid);
545 if (argc > 2)
546 sscanf(argv[2], "%i", &info_level);
548 if (argc > 3)
549 sscanf(argv[3], "%x", &access_mask);
551 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
552 strupper_m(server);
554 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
555 &connect_pol);
557 if (!NT_STATUS_IS_OK(result))
558 goto done;
560 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
561 MAXIMUM_ALLOWED_ACCESS,
562 &domain_sid, &domain_pol);
564 if (!NT_STATUS_IS_OK(result))
565 goto done;
567 result = rpccli_samr_open_group(cli, mem_ctx, &domain_pol,
568 access_mask,
569 group_rid, &group_pol);
571 if (!NT_STATUS_IS_OK(result))
572 goto done;
574 result = rpccli_samr_query_groupinfo(cli, mem_ctx, &group_pol,
575 info_level, &group_ctr);
576 if (!NT_STATUS_IS_OK(result)) {
577 goto done;
580 display_group_info_ctr(group_ctr);
582 rpccli_samr_close(cli, mem_ctx, &group_pol);
583 rpccli_samr_close(cli, mem_ctx, &domain_pol);
584 rpccli_samr_close(cli, mem_ctx, &connect_pol);
585 done:
586 return result;
589 /* Query groups a user is a member of */
591 static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
592 TALLOC_CTX *mem_ctx,
593 int argc, const char **argv)
595 POLICY_HND connect_pol,
596 domain_pol,
597 user_pol;
598 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
599 uint32 num_groups,
600 user_rid;
601 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
602 DOM_GID *user_gids;
603 int i;
604 fstring server;
606 if ((argc < 2) || (argc > 3)) {
607 printf("Usage: %s rid [access mask]\n", argv[0]);
608 return NT_STATUS_OK;
611 sscanf(argv[1], "%i", &user_rid);
613 if (argc > 2)
614 sscanf(argv[2], "%x", &access_mask);
616 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
617 strupper_m(server);
619 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
620 &connect_pol);
622 if (!NT_STATUS_IS_OK(result))
623 goto done;
625 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
626 MAXIMUM_ALLOWED_ACCESS,
627 &domain_sid, &domain_pol);
629 if (!NT_STATUS_IS_OK(result))
630 goto done;
632 result = rpccli_samr_open_user(cli, mem_ctx, &domain_pol,
633 access_mask,
634 user_rid, &user_pol);
636 if (!NT_STATUS_IS_OK(result))
637 goto done;
639 result = rpccli_samr_query_usergroups(cli, mem_ctx, &user_pol,
640 &num_groups, &user_gids);
642 if (!NT_STATUS_IS_OK(result))
643 goto done;
645 for (i = 0; i < num_groups; i++) {
646 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
647 user_gids[i].g_rid, user_gids[i].attr);
650 rpccli_samr_close(cli, mem_ctx, &user_pol);
651 rpccli_samr_close(cli, mem_ctx, &domain_pol);
652 rpccli_samr_close(cli, mem_ctx, &connect_pol);
653 done:
654 return result;
657 /* Query aliases a user is a member of */
659 static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
660 TALLOC_CTX *mem_ctx,
661 int argc, const char **argv)
663 POLICY_HND connect_pol, domain_pol;
664 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
665 DOM_SID *sids;
666 size_t num_sids;
667 uint32 num_aliases, *alias_rids;
668 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
669 int i;
670 fstring server;
671 DOM_SID2 *sid2;
673 if (argc < 3) {
674 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
675 return NT_STATUS_INVALID_PARAMETER;
678 sids = NULL;
679 num_sids = 0;
681 for (i=2; i<argc; i++) {
682 DOM_SID tmp_sid;
683 if (!string_to_sid(&tmp_sid, argv[i])) {
684 printf("%s is not a legal SID\n", argv[i]);
685 return NT_STATUS_INVALID_PARAMETER;
687 add_sid_to_array(mem_ctx, &tmp_sid, &sids, &num_sids);
690 sid2 = TALLOC_ARRAY(mem_ctx, DOM_SID2, num_sids);
691 if (sid2 == NULL)
692 return NT_STATUS_NO_MEMORY;
694 for (i=0; i<num_sids; i++) {
695 sid_copy(&sid2[i].sid, &sids[i]);
696 sid2[i].num_auths = sid2[i].sid.num_auths;
699 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
700 strupper_m(server);
702 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
703 &connect_pol);
705 if (!NT_STATUS_IS_OK(result))
706 goto done;
708 if (StrCaseCmp(argv[1], "domain")==0)
709 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
710 access_mask,
711 &domain_sid, &domain_pol);
712 else if (StrCaseCmp(argv[1], "builtin")==0)
713 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
714 access_mask,
715 &global_sid_Builtin,
716 &domain_pol);
717 else {
718 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
719 return NT_STATUS_INVALID_PARAMETER;
722 if (!NT_STATUS_IS_OK(result))
723 goto done;
725 result = rpccli_samr_query_useraliases(cli, mem_ctx, &domain_pol,
726 num_sids, sid2,
727 &num_aliases, &alias_rids);
729 if (!NT_STATUS_IS_OK(result))
730 goto done;
732 for (i = 0; i < num_aliases; i++) {
733 printf("\tgroup rid:[0x%x]\n", alias_rids[i]);
736 rpccli_samr_close(cli, mem_ctx, &domain_pol);
737 rpccli_samr_close(cli, mem_ctx, &connect_pol);
738 done:
739 return result;
742 /* Query members of a group */
744 static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
745 TALLOC_CTX *mem_ctx,
746 int argc, const char **argv)
748 POLICY_HND connect_pol, domain_pol, group_pol;
749 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
750 uint32 num_members, *group_rids, *group_attrs, group_rid;
751 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
752 int i;
753 fstring server;
755 if ((argc < 2) || (argc > 3)) {
756 printf("Usage: %s rid [access mask]\n", argv[0]);
757 return NT_STATUS_OK;
760 sscanf(argv[1], "%i", &group_rid);
762 if (argc > 2)
763 sscanf(argv[2], "%x", &access_mask);
765 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
766 strupper_m(server);
768 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
769 &connect_pol);
771 if (!NT_STATUS_IS_OK(result))
772 goto done;
774 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
775 MAXIMUM_ALLOWED_ACCESS,
776 &domain_sid, &domain_pol);
778 if (!NT_STATUS_IS_OK(result))
779 goto done;
781 result = rpccli_samr_open_group(cli, mem_ctx, &domain_pol,
782 access_mask,
783 group_rid, &group_pol);
785 if (!NT_STATUS_IS_OK(result))
786 goto done;
788 result = rpccli_samr_query_groupmem(cli, mem_ctx, &group_pol,
789 &num_members, &group_rids,
790 &group_attrs);
792 if (!NT_STATUS_IS_OK(result))
793 goto done;
795 for (i = 0; i < num_members; i++) {
796 printf("\trid:[0x%x] attr:[0x%x]\n", group_rids[i],
797 group_attrs[i]);
800 rpccli_samr_close(cli, mem_ctx, &group_pol);
801 rpccli_samr_close(cli, mem_ctx, &domain_pol);
802 rpccli_samr_close(cli, mem_ctx, &connect_pol);
803 done:
804 return result;
807 /* Enumerate domain users */
809 static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
810 TALLOC_CTX *mem_ctx,
811 int argc, const char **argv)
813 POLICY_HND connect_pol, domain_pol;
814 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
815 uint32 start_idx, size, num_dom_users, i;
816 char **dom_users;
817 uint32 *dom_rids;
818 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
819 uint32 acb_mask = ACB_NORMAL;
820 BOOL got_connect_pol = False, got_domain_pol = False;
822 if ((argc < 1) || (argc > 3)) {
823 printf("Usage: %s [access_mask] [acb_mask]\n", argv[0]);
824 return NT_STATUS_OK;
827 if (argc > 1)
828 sscanf(argv[1], "%x", &access_mask);
830 if (argc > 2)
831 sscanf(argv[2], "%x", &acb_mask);
833 /* Get sam policy handle */
835 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
836 &connect_pol);
838 if (!NT_STATUS_IS_OK(result))
839 goto done;
841 got_connect_pol = True;
843 /* Get domain policy handle */
845 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
846 access_mask,
847 &domain_sid, &domain_pol);
849 if (!NT_STATUS_IS_OK(result))
850 goto done;
852 got_domain_pol = True;
854 /* Enumerate domain users */
856 start_idx = 0;
857 size = 0xffff;
859 do {
860 result = rpccli_samr_enum_dom_users(
861 cli, mem_ctx, &domain_pol, &start_idx, acb_mask,
862 size, &dom_users, &dom_rids, &num_dom_users);
864 if (NT_STATUS_IS_OK(result) ||
865 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
867 for (i = 0; i < num_dom_users; i++)
868 printf("user:[%s] rid:[0x%x]\n",
869 dom_users[i], dom_rids[i]);
872 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
874 done:
875 if (got_domain_pol)
876 rpccli_samr_close(cli, mem_ctx, &domain_pol);
878 if (got_connect_pol)
879 rpccli_samr_close(cli, mem_ctx, &connect_pol);
881 return result;
884 /* Enumerate domain groups */
886 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
887 TALLOC_CTX *mem_ctx,
888 int argc, const char **argv)
890 POLICY_HND connect_pol, domain_pol;
891 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
892 uint32 start_idx, size, num_dom_groups, i;
893 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
894 struct acct_info *dom_groups;
895 BOOL got_connect_pol = False, got_domain_pol = False;
897 if ((argc < 1) || (argc > 2)) {
898 printf("Usage: %s [access_mask]\n", argv[0]);
899 return NT_STATUS_OK;
902 if (argc > 1)
903 sscanf(argv[1], "%x", &access_mask);
905 /* Get sam policy handle */
907 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
908 &connect_pol);
910 if (!NT_STATUS_IS_OK(result))
911 goto done;
913 got_connect_pol = True;
915 /* Get domain policy handle */
917 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
918 access_mask,
919 &domain_sid, &domain_pol);
921 if (!NT_STATUS_IS_OK(result))
922 goto done;
924 got_domain_pol = True;
926 /* Enumerate domain groups */
928 start_idx = 0;
929 size = 0xffff;
931 do {
932 result = rpccli_samr_enum_dom_groups(
933 cli, mem_ctx, &domain_pol, &start_idx, size,
934 &dom_groups, &num_dom_groups);
936 if (NT_STATUS_IS_OK(result) ||
937 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
939 for (i = 0; i < num_dom_groups; i++)
940 printf("group:[%s] rid:[0x%x]\n",
941 dom_groups[i].acct_name,
942 dom_groups[i].rid);
945 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
947 done:
948 if (got_domain_pol)
949 rpccli_samr_close(cli, mem_ctx, &domain_pol);
951 if (got_connect_pol)
952 rpccli_samr_close(cli, mem_ctx, &connect_pol);
954 return result;
957 /* Enumerate alias groups */
959 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
960 TALLOC_CTX *mem_ctx,
961 int argc, const char **argv)
963 POLICY_HND connect_pol, domain_pol;
964 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
965 uint32 start_idx, size, num_als_groups, i;
966 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
967 struct acct_info *als_groups;
968 BOOL got_connect_pol = False, got_domain_pol = False;
970 if ((argc < 2) || (argc > 3)) {
971 printf("Usage: %s builtin|domain [access mask]\n", argv[0]);
972 return NT_STATUS_OK;
975 if (argc > 2)
976 sscanf(argv[2], "%x", &access_mask);
978 /* Get sam policy handle */
980 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
981 &connect_pol);
983 if (!NT_STATUS_IS_OK(result))
984 goto done;
986 got_connect_pol = True;
988 /* Get domain policy handle */
990 if (StrCaseCmp(argv[1], "domain")==0)
991 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
992 access_mask,
993 &domain_sid, &domain_pol);
994 else if (StrCaseCmp(argv[1], "builtin")==0)
995 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
996 access_mask,
997 &global_sid_Builtin, &domain_pol);
998 else
999 return NT_STATUS_OK;
1001 if (!NT_STATUS_IS_OK(result))
1002 goto done;
1004 got_domain_pol = True;
1006 /* Enumerate alias groups */
1008 start_idx = 0;
1009 size = 0xffff; /* Number of groups to retrieve */
1011 do {
1012 result = rpccli_samr_enum_als_groups(
1013 cli, mem_ctx, &domain_pol, &start_idx, size,
1014 &als_groups, &num_als_groups);
1016 if (NT_STATUS_IS_OK(result) ||
1017 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1019 for (i = 0; i < num_als_groups; i++)
1020 printf("group:[%s] rid:[0x%x]\n",
1021 als_groups[i].acct_name,
1022 als_groups[i].rid);
1024 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1026 done:
1027 if (got_domain_pol)
1028 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1030 if (got_connect_pol)
1031 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1033 return result;
1036 /* Query alias membership */
1038 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1039 TALLOC_CTX *mem_ctx,
1040 int argc, const char **argv)
1042 POLICY_HND connect_pol, domain_pol, alias_pol;
1043 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1044 uint32 alias_rid, num_members, i;
1045 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1046 DOM_SID *alias_sids;
1048 if ((argc < 3) || (argc > 4)) {
1049 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1050 return NT_STATUS_OK;
1053 sscanf(argv[2], "%i", &alias_rid);
1055 if (argc > 3)
1056 sscanf(argv[3], "%x", &access_mask);
1058 /* Open SAMR handle */
1060 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1061 &connect_pol);
1063 if (!NT_STATUS_IS_OK(result))
1064 goto done;
1066 /* Open handle on domain */
1068 if (StrCaseCmp(argv[1], "domain")==0)
1069 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1070 MAXIMUM_ALLOWED_ACCESS,
1071 &domain_sid, &domain_pol);
1072 else if (StrCaseCmp(argv[1], "builtin")==0)
1073 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1074 MAXIMUM_ALLOWED_ACCESS,
1075 &global_sid_Builtin, &domain_pol);
1076 else
1077 return NT_STATUS_OK;
1079 if (!NT_STATUS_IS_OK(result))
1080 goto done;
1082 /* Open handle on alias */
1084 result = rpccli_samr_open_alias(cli, mem_ctx, &domain_pol,
1085 access_mask,
1086 alias_rid, &alias_pol);
1087 if (!NT_STATUS_IS_OK(result))
1088 goto done;
1090 result = rpccli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
1091 &num_members, &alias_sids);
1093 if (!NT_STATUS_IS_OK(result))
1094 goto done;
1096 for (i = 0; i < num_members; i++) {
1097 fstring sid_str;
1099 sid_to_string(sid_str, &alias_sids[i]);
1100 printf("\tsid:[%s]\n", sid_str);
1103 rpccli_samr_close(cli, mem_ctx, &alias_pol);
1104 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1105 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1106 done:
1107 return result;
1110 /* Query delete an alias membership */
1112 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1113 TALLOC_CTX *mem_ctx,
1114 int argc, const char **argv)
1116 POLICY_HND connect_pol, domain_pol, alias_pol;
1117 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1118 uint32 alias_rid;
1119 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1121 if (argc != 3) {
1122 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1123 return NT_STATUS_OK;
1126 alias_rid = strtoul(argv[2], NULL, 10);
1128 /* Open SAMR handle */
1130 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1131 &connect_pol);
1133 if (!NT_STATUS_IS_OK(result))
1134 goto done;
1136 /* Open handle on domain */
1138 if (StrCaseCmp(argv[1], "domain")==0)
1139 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1140 MAXIMUM_ALLOWED_ACCESS,
1141 &domain_sid, &domain_pol);
1142 else if (StrCaseCmp(argv[1], "builtin")==0)
1143 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1144 MAXIMUM_ALLOWED_ACCESS,
1145 &global_sid_Builtin, &domain_pol);
1146 else
1147 return NT_STATUS_INVALID_PARAMETER;
1149 if (!NT_STATUS_IS_OK(result))
1150 goto done;
1152 /* Open handle on alias */
1154 result = rpccli_samr_open_alias(cli, mem_ctx, &domain_pol,
1155 access_mask,
1156 alias_rid, &alias_pol);
1157 if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1158 /* Probably this was a user name, try lookupnames */
1159 uint32 num_rids;
1160 uint32 *rids, *types;
1162 result = rpccli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1163 1000, 1, &argv[2],
1164 &num_rids, &rids,
1165 &types);
1167 if (NT_STATUS_IS_OK(result)) {
1168 result = rpccli_samr_open_alias(cli, mem_ctx,
1169 &domain_pol,
1170 access_mask,
1171 rids[0], &alias_pol);
1175 result = rpccli_samr_delete_dom_alias(cli, mem_ctx, &alias_pol);
1177 if (!NT_STATUS_IS_OK(result))
1178 goto done;
1180 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1181 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1182 done:
1183 return result;
1186 /* Query display info */
1188 static NTSTATUS cmd_samr_query_dispinfo_int(struct rpc_pipe_client *cli,
1189 TALLOC_CTX *mem_ctx,
1190 int argc, const char **argv,
1191 int opcode)
1193 POLICY_HND connect_pol, domain_pol;
1194 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1195 uint32 start_idx=0, max_entries=250, max_size = (uint32) -1, num_entries, i;
1196 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1197 uint32 info_level = 1;
1198 SAM_DISPINFO_CTR ctr;
1199 SAM_DISPINFO_1 info1;
1200 SAM_DISPINFO_2 info2;
1201 SAM_DISPINFO_3 info3;
1202 SAM_DISPINFO_4 info4;
1203 SAM_DISPINFO_5 info5;
1204 int loop_count = 0;
1205 BOOL got_params = False; /* Use get_query_dispinfo_params() or not? */
1207 if (argc > 6) {
1208 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1209 return NT_STATUS_OK;
1212 if (argc >= 2)
1213 sscanf(argv[1], "%i", &info_level);
1215 if (argc >= 3)
1216 sscanf(argv[2], "%i", &start_idx);
1218 if (argc >= 4) {
1219 sscanf(argv[3], "%i", &max_entries);
1220 got_params = True;
1223 if (argc >= 5) {
1224 sscanf(argv[4], "%i", &max_size);
1225 got_params = True;
1228 if (argc >= 6)
1229 sscanf(argv[5], "%x", &access_mask);
1232 /* Get sam policy handle */
1234 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1235 &connect_pol);
1237 if (!NT_STATUS_IS_OK(result))
1238 goto done;
1240 /* Get domain policy handle */
1242 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1243 access_mask,
1244 &domain_sid, &domain_pol);
1246 if (!NT_STATUS_IS_OK(result))
1247 goto done;
1249 /* Query display info */
1251 ZERO_STRUCT(ctr);
1252 ZERO_STRUCT(info1);
1254 switch (info_level) {
1255 case 1:
1256 ZERO_STRUCT(info1);
1257 ctr.sam.info1 = &info1;
1258 break;
1259 case 2:
1260 ZERO_STRUCT(info2);
1261 ctr.sam.info2 = &info2;
1262 break;
1263 case 3:
1264 ZERO_STRUCT(info3);
1265 ctr.sam.info3 = &info3;
1266 break;
1267 case 4:
1268 ZERO_STRUCT(info4);
1269 ctr.sam.info4 = &info4;
1270 break;
1271 case 5:
1272 ZERO_STRUCT(info5);
1273 ctr.sam.info5 = &info5;
1274 break;
1278 do {
1280 if (!got_params)
1281 get_query_dispinfo_params(
1282 loop_count, &max_entries, &max_size);
1284 switch (opcode) {
1285 case SAMR_QUERY_DISPINFO:
1286 result = rpccli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1287 &start_idx, info_level,
1288 &num_entries, max_entries,
1289 max_size, &ctr);
1290 break;
1291 case SAMR_QUERY_DISPINFO2:
1292 result = rpccli_samr_query_dispinfo2(cli, mem_ctx, &domain_pol,
1293 &start_idx, info_level,
1294 &num_entries, max_entries,
1295 max_size, &ctr);
1296 break;
1297 case SAMR_QUERY_DISPINFO3:
1298 result = rpccli_samr_query_dispinfo3(cli, mem_ctx, &domain_pol,
1299 &start_idx, info_level,
1300 &num_entries, max_entries,
1301 max_size, &ctr);
1302 break;
1303 default:
1304 printf("unknown opcode: %d\n", opcode);
1305 return NT_STATUS_INVALID_PARAMETER;
1306 break;
1309 loop_count++;
1311 if (NT_STATUS_IS_ERR(result))
1312 break;
1314 if (num_entries == 0)
1315 break;
1317 for (i = 0; i < num_entries; i++) {
1318 switch (info_level) {
1319 case 1:
1320 display_sam_info_1(&ctr.sam.info1->sam[i], &ctr.sam.info1->str[i]);
1321 break;
1322 case 2:
1323 display_sam_info_2(&ctr.sam.info2->sam[i], &ctr.sam.info2->str[i]);
1324 break;
1325 case 3:
1326 display_sam_info_3(&ctr.sam.info3->sam[i], &ctr.sam.info3->str[i]);
1327 break;
1328 case 4:
1329 display_sam_info_4(&ctr.sam.info4->sam[i], &ctr.sam.info4->str[i]);
1330 break;
1331 case 5:
1332 display_sam_info_5(&ctr.sam.info5->sam[i], &ctr.sam.info5->str[i]);
1333 break;
1336 } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1338 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1339 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1340 done:
1341 return result;
1344 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1345 TALLOC_CTX *mem_ctx,
1346 int argc, const char **argv)
1348 return cmd_samr_query_dispinfo_int(cli, mem_ctx, argc, argv, SAMR_QUERY_DISPINFO);
1351 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1352 TALLOC_CTX *mem_ctx,
1353 int argc, const char **argv)
1355 return cmd_samr_query_dispinfo_int(cli, mem_ctx, argc, argv, SAMR_QUERY_DISPINFO2);
1358 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1359 TALLOC_CTX *mem_ctx,
1360 int argc, const char **argv)
1362 return cmd_samr_query_dispinfo_int(cli, mem_ctx, argc, argv, SAMR_QUERY_DISPINFO3);
1365 /* Query display info index */
1367 static NTSTATUS cmd_samr_get_dispenum_index(struct rpc_pipe_client *cli,
1368 TALLOC_CTX *mem_ctx,
1369 int argc, const char **argv)
1371 POLICY_HND connect_pol, domain_pol;
1372 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1373 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1374 uint16 info_level = 1;
1375 uint32 idx;
1376 const char *name;
1378 if (argc > 6) {
1379 printf("Usage: %s mame [info level]\n", argv[0]);
1380 return NT_STATUS_OK;
1383 if (argc >= 2)
1384 name = argv[1];
1386 if (argc >= 3)
1387 sscanf(argv[2], "%hd", &info_level);
1390 /* Get sam policy handle */
1392 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1393 &connect_pol);
1395 if (!NT_STATUS_IS_OK(result))
1396 goto done;
1398 /* Get domain policy handle */
1400 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1401 access_mask,
1402 &domain_sid, &domain_pol);
1404 if (!NT_STATUS_IS_OK(result))
1405 goto done;
1407 /* Query display info index */
1409 result = rpccli_samr_get_dispenum_index(cli, mem_ctx, &domain_pol,
1410 info_level, name, &idx);
1411 if (!NT_STATUS_IS_OK(result)) {
1412 goto done;
1415 printf("idx is: %d\n", idx);
1417 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1418 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1419 done:
1420 return result;
1425 /* Query domain info */
1427 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1428 TALLOC_CTX *mem_ctx,
1429 int argc, const char **argv)
1431 POLICY_HND connect_pol, domain_pol;
1432 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1433 uint32 switch_level = 2;
1434 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1435 SAM_UNK_CTR ctr;
1437 if (argc > 3) {
1438 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1439 return NT_STATUS_OK;
1442 if (argc > 1)
1443 sscanf(argv[1], "%i", &switch_level);
1445 if (argc > 2)
1446 sscanf(argv[2], "%x", &access_mask);
1448 /* Get sam policy handle */
1450 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1451 &connect_pol);
1453 if (!NT_STATUS_IS_OK(result))
1454 goto done;
1456 /* Get domain policy handle */
1458 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1459 access_mask,
1460 &domain_sid, &domain_pol);
1462 if (!NT_STATUS_IS_OK(result))
1463 goto done;
1465 /* Query domain info */
1467 result = rpccli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
1468 switch_level, &ctr);
1470 if (!NT_STATUS_IS_OK(result))
1471 goto done;
1473 /* Display domain info */
1475 switch (switch_level) {
1476 case 1:
1477 display_sam_unk_info_1(&ctr.info.inf1);
1478 break;
1479 case 2:
1480 display_sam_unk_info_2(&ctr.info.inf2);
1481 break;
1482 case 3:
1483 display_sam_unk_info_3(&ctr.info.inf3);
1484 break;
1485 case 4:
1486 display_sam_unk_info_4(&ctr.info.inf4);
1487 break;
1488 case 5:
1489 display_sam_unk_info_5(&ctr.info.inf5);
1490 break;
1491 case 6:
1492 display_sam_unk_info_6(&ctr.info.inf6);
1493 break;
1494 case 7:
1495 display_sam_unk_info_7(&ctr.info.inf7);
1496 break;
1497 case 8:
1498 display_sam_unk_info_8(&ctr.info.inf8);
1499 break;
1500 case 9:
1501 display_sam_unk_info_9(&ctr.info.inf9);
1502 break;
1503 case 12:
1504 display_sam_unk_info_12(&ctr.info.inf12);
1505 break;
1506 case 13:
1507 display_sam_unk_info_13(&ctr.info.inf13);
1508 break;
1510 default:
1511 printf("cannot display domain info for switch value %d\n",
1512 switch_level);
1513 break;
1516 done:
1518 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1519 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1520 return result;
1523 /* Create domain user */
1525 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1526 TALLOC_CTX *mem_ctx,
1527 int argc, const char **argv)
1529 POLICY_HND connect_pol, domain_pol, user_pol;
1530 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1531 const char *acct_name;
1532 uint32 acb_info;
1533 uint32 unknown, user_rid;
1534 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1536 if ((argc < 2) || (argc > 3)) {
1537 printf("Usage: %s username [access mask]\n", argv[0]);
1538 return NT_STATUS_OK;
1541 acct_name = argv[1];
1543 if (argc > 2)
1544 sscanf(argv[2], "%x", &access_mask);
1546 /* Get sam policy handle */
1548 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1549 &connect_pol);
1551 if (!NT_STATUS_IS_OK(result))
1552 goto done;
1554 /* Get domain policy handle */
1556 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1557 access_mask,
1558 &domain_sid, &domain_pol);
1560 if (!NT_STATUS_IS_OK(result))
1561 goto done;
1563 /* Create domain user */
1565 acb_info = ACB_NORMAL;
1566 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
1568 result = rpccli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1569 acct_name, acb_info, unknown,
1570 &user_pol, &user_rid);
1572 if (!NT_STATUS_IS_OK(result))
1573 goto done;
1575 result = rpccli_samr_close(cli, mem_ctx, &user_pol);
1576 if (!NT_STATUS_IS_OK(result)) goto done;
1578 result = rpccli_samr_close(cli, mem_ctx, &domain_pol);
1579 if (!NT_STATUS_IS_OK(result)) goto done;
1581 result = rpccli_samr_close(cli, mem_ctx, &connect_pol);
1582 if (!NT_STATUS_IS_OK(result)) goto done;
1584 done:
1585 return result;
1588 /* Create domain group */
1590 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1591 TALLOC_CTX *mem_ctx,
1592 int argc, const char **argv)
1594 POLICY_HND connect_pol, domain_pol, group_pol;
1595 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1596 const char *grp_name;
1597 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1599 if ((argc < 2) || (argc > 3)) {
1600 printf("Usage: %s groupname [access mask]\n", argv[0]);
1601 return NT_STATUS_OK;
1604 grp_name = argv[1];
1606 if (argc > 2)
1607 sscanf(argv[2], "%x", &access_mask);
1609 /* Get sam policy handle */
1611 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1612 &connect_pol);
1614 if (!NT_STATUS_IS_OK(result))
1615 goto done;
1617 /* Get domain policy handle */
1619 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1620 access_mask,
1621 &domain_sid, &domain_pol);
1623 if (!NT_STATUS_IS_OK(result))
1624 goto done;
1626 /* Create domain user */
1628 result = rpccli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1629 grp_name, MAXIMUM_ALLOWED_ACCESS,
1630 &group_pol);
1632 if (!NT_STATUS_IS_OK(result))
1633 goto done;
1635 result = rpccli_samr_close(cli, mem_ctx, &group_pol);
1636 if (!NT_STATUS_IS_OK(result)) goto done;
1638 result = rpccli_samr_close(cli, mem_ctx, &domain_pol);
1639 if (!NT_STATUS_IS_OK(result)) goto done;
1641 result = rpccli_samr_close(cli, mem_ctx, &connect_pol);
1642 if (!NT_STATUS_IS_OK(result)) goto done;
1644 done:
1645 return result;
1648 /* Create domain alias */
1650 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1651 TALLOC_CTX *mem_ctx,
1652 int argc, const char **argv)
1654 POLICY_HND connect_pol, domain_pol, alias_pol;
1655 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1656 const char *alias_name;
1657 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1659 if ((argc < 2) || (argc > 3)) {
1660 printf("Usage: %s aliasname [access mask]\n", argv[0]);
1661 return NT_STATUS_OK;
1664 alias_name = argv[1];
1666 if (argc > 2)
1667 sscanf(argv[2], "%x", &access_mask);
1669 /* Get sam policy handle */
1671 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1672 &connect_pol);
1674 if (!NT_STATUS_IS_OK(result))
1675 goto done;
1677 /* Get domain policy handle */
1679 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1680 access_mask,
1681 &domain_sid, &domain_pol);
1683 if (!NT_STATUS_IS_OK(result))
1684 goto done;
1686 /* Create domain user */
1688 result = rpccli_samr_create_dom_alias(cli, mem_ctx, &domain_pol,
1689 alias_name, &alias_pol);
1691 if (!NT_STATUS_IS_OK(result))
1692 goto done;
1694 result = rpccli_samr_close(cli, mem_ctx, &alias_pol);
1695 if (!NT_STATUS_IS_OK(result)) goto done;
1697 result = rpccli_samr_close(cli, mem_ctx, &domain_pol);
1698 if (!NT_STATUS_IS_OK(result)) goto done;
1700 result = rpccli_samr_close(cli, mem_ctx, &connect_pol);
1701 if (!NT_STATUS_IS_OK(result)) goto done;
1703 done:
1704 return result;
1707 /* Lookup sam names */
1709 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
1710 TALLOC_CTX *mem_ctx,
1711 int argc, const char **argv)
1713 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1714 POLICY_HND connect_pol, domain_pol;
1715 uint32 flags = 0x000003e8; /* Unknown */
1716 uint32 num_rids, num_names, *name_types, *rids;
1717 const char **names;
1718 int i;
1720 if (argc < 3) {
1721 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
1722 printf("check on the domain SID: S-1-5-21-x-y-z\n");
1723 printf("or check on the builtin SID: S-1-5-32\n");
1724 return NT_STATUS_OK;
1727 /* Get sam policy and domain handles */
1729 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1730 &connect_pol);
1732 if (!NT_STATUS_IS_OK(result))
1733 goto done;
1735 if (StrCaseCmp(argv[1], "domain")==0)
1736 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1737 MAXIMUM_ALLOWED_ACCESS,
1738 &domain_sid, &domain_pol);
1739 else if (StrCaseCmp(argv[1], "builtin")==0)
1740 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1741 MAXIMUM_ALLOWED_ACCESS,
1742 &global_sid_Builtin, &domain_pol);
1743 else
1744 return NT_STATUS_OK;
1746 if (!NT_STATUS_IS_OK(result))
1747 goto done;
1749 /* Look up names */
1751 num_names = argc - 2;
1752 if ((names = TALLOC_ARRAY(mem_ctx, const char *, num_names)) == NULL) {
1753 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1754 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1755 result = NT_STATUS_NO_MEMORY;
1756 goto done;
1759 for (i = 0; i < argc - 2; i++)
1760 names[i] = argv[i + 2];
1762 result = rpccli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1763 flags, num_names, names,
1764 &num_rids, &rids, &name_types);
1766 if (!NT_STATUS_IS_OK(result))
1767 goto done;
1769 /* Display results */
1771 for (i = 0; i < num_names; i++)
1772 printf("name %s: 0x%x (%d)\n", names[i], rids[i],
1773 name_types[i]);
1775 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1776 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1777 done:
1778 return result;
1781 /* Lookup sam rids */
1783 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
1784 TALLOC_CTX *mem_ctx,
1785 int argc, const char **argv)
1787 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1788 POLICY_HND connect_pol, domain_pol;
1789 uint32 num_rids, num_names, *rids, *name_types;
1790 char **names;
1791 int i;
1793 if (argc < 3) {
1794 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
1795 return NT_STATUS_OK;
1798 /* Get sam policy and domain handles */
1800 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1801 &connect_pol);
1803 if (!NT_STATUS_IS_OK(result))
1804 goto done;
1806 if (StrCaseCmp(argv[1], "domain")==0)
1807 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1808 MAXIMUM_ALLOWED_ACCESS,
1809 &domain_sid, &domain_pol);
1810 else if (StrCaseCmp(argv[1], "builtin")==0)
1811 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1812 MAXIMUM_ALLOWED_ACCESS,
1813 &global_sid_Builtin, &domain_pol);
1814 else
1815 return NT_STATUS_OK;
1817 if (!NT_STATUS_IS_OK(result))
1818 goto done;
1820 /* Look up rids */
1822 num_rids = argc - 2;
1823 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1824 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1825 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1826 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1827 result = NT_STATUS_NO_MEMORY;
1828 goto done;
1831 for (i = 0; i < argc - 2; i++)
1832 sscanf(argv[i + 2], "%i", &rids[i]);
1834 result = rpccli_samr_lookup_rids(cli, mem_ctx, &domain_pol, num_rids, rids,
1835 &num_names, &names, &name_types);
1837 if (!NT_STATUS_IS_OK(result) &&
1838 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
1839 goto done;
1841 /* Display results */
1843 for (i = 0; i < num_names; i++)
1844 printf("rid 0x%x: %s (%d)\n", rids[i], names[i], name_types[i]);
1846 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1847 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1848 done:
1849 return result;
1852 /* Delete domain user */
1854 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
1855 TALLOC_CTX *mem_ctx,
1856 int argc, const char **argv)
1858 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1859 POLICY_HND connect_pol, domain_pol, user_pol;
1860 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1862 if ((argc < 2) || (argc > 3)) {
1863 printf("Usage: %s username\n", argv[0]);
1864 return NT_STATUS_OK;
1867 if (argc > 2)
1868 sscanf(argv[2], "%x", &access_mask);
1870 /* Get sam policy and domain handles */
1872 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1873 &connect_pol);
1875 if (!NT_STATUS_IS_OK(result))
1876 goto done;
1878 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1879 MAXIMUM_ALLOWED_ACCESS,
1880 &domain_sid, &domain_pol);
1882 if (!NT_STATUS_IS_OK(result))
1883 goto done;
1885 /* Get handle on user */
1888 uint32 *user_rids, num_rids, *name_types;
1889 uint32 flags = 0x000003e8; /* Unknown */
1891 result = rpccli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1892 flags, 1, (const char **)&argv[1],
1893 &num_rids, &user_rids,
1894 &name_types);
1896 if (!NT_STATUS_IS_OK(result))
1897 goto done;
1899 result = rpccli_samr_open_user(cli, mem_ctx, &domain_pol,
1900 access_mask,
1901 user_rids[0], &user_pol);
1903 if (!NT_STATUS_IS_OK(result))
1904 goto done;
1907 /* Delete user */
1909 result = rpccli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
1911 if (!NT_STATUS_IS_OK(result))
1912 goto done;
1914 /* Display results */
1916 rpccli_samr_close(cli, mem_ctx, &user_pol);
1917 rpccli_samr_close(cli, mem_ctx, &domain_pol);
1918 rpccli_samr_close(cli, mem_ctx, &connect_pol);
1920 done:
1921 return result;
1924 /**********************************************************************
1925 * Query user security object
1927 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
1928 TALLOC_CTX *mem_ctx,
1929 int argc, const char **argv)
1931 POLICY_HND connect_pol, domain_pol, user_pol, *pol;
1932 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1933 uint32 sec_info = DACL_SECURITY_INFORMATION;
1934 fstring server;
1935 uint32 user_rid = 0;
1936 TALLOC_CTX *ctx = NULL;
1937 SEC_DESC_BUF *sec_desc_buf=NULL;
1938 BOOL domain = False;
1940 ctx=talloc_init("cmd_samr_query_sec_obj");
1942 if ((argc < 1) || (argc > 3)) {
1943 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
1944 printf("\tSpecify rid for security on user, -d for security on domain\n");
1945 return NT_STATUS_OK;
1948 if (argc > 1) {
1949 if (strcmp(argv[1], "-d") == 0)
1950 domain = True;
1951 else
1952 sscanf(argv[1], "%i", &user_rid);
1955 if (argc == 3) {
1956 sec_info = atoi(argv[2]);
1959 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
1960 strupper_m(server);
1961 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1962 &connect_pol);
1964 if (!NT_STATUS_IS_OK(result))
1965 goto done;
1967 if (domain || user_rid)
1968 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
1969 MAXIMUM_ALLOWED_ACCESS,
1970 &domain_sid, &domain_pol);
1972 if (!NT_STATUS_IS_OK(result))
1973 goto done;
1975 if (user_rid)
1976 result = rpccli_samr_open_user(cli, mem_ctx, &domain_pol,
1977 MAXIMUM_ALLOWED_ACCESS,
1978 user_rid, &user_pol);
1980 if (!NT_STATUS_IS_OK(result))
1981 goto done;
1983 /* Pick which query pol to use */
1985 pol = &connect_pol;
1987 if (domain)
1988 pol = &domain_pol;
1990 if (user_rid)
1991 pol = &user_pol;
1993 /* Query SAM security object */
1995 result = rpccli_samr_query_sec_obj(cli, mem_ctx, pol, sec_info, ctx,
1996 &sec_desc_buf);
1998 if (!NT_STATUS_IS_OK(result))
1999 goto done;
2001 display_sec_desc(sec_desc_buf->sec);
2003 rpccli_samr_close(cli, mem_ctx, &user_pol);
2004 rpccli_samr_close(cli, mem_ctx, &domain_pol);
2005 rpccli_samr_close(cli, mem_ctx, &connect_pol);
2006 done:
2007 talloc_destroy(ctx);
2008 return result;
2011 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2012 TALLOC_CTX *mem_ctx,
2013 int argc, const char **argv)
2015 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2016 POLICY_HND connect_pol, domain_pol, user_pol;
2017 uint16 min_pwd_length;
2018 uint32 password_properties, unknown1, rid;
2020 if (argc != 2) {
2021 printf("Usage: %s rid\n", argv[0]);
2022 return NT_STATUS_OK;
2025 sscanf(argv[1], "%i", &rid);
2027 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2028 &connect_pol);
2030 if (!NT_STATUS_IS_OK(result)) {
2031 goto done;
2034 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
2035 MAXIMUM_ALLOWED_ACCESS, &domain_sid, &domain_pol);
2037 if (!NT_STATUS_IS_OK(result)) {
2038 goto done;
2041 result = rpccli_samr_open_user(cli, mem_ctx, &domain_pol,
2042 MAXIMUM_ALLOWED_ACCESS,
2043 rid, &user_pol);
2045 if (!NT_STATUS_IS_OK(result)) {
2046 goto done;
2049 result = rpccli_samr_get_usrdom_pwinfo(cli, mem_ctx, &user_pol,
2050 &min_pwd_length, &password_properties,
2051 &unknown1) ;
2053 if (NT_STATUS_IS_OK(result)) {
2054 printf("min_pwd_length: %d\n", min_pwd_length);
2055 printf("unknown1: %d\n", unknown1);
2056 display_password_properties(password_properties);
2059 done:
2060 rpccli_samr_close(cli, mem_ctx, &user_pol);
2061 rpccli_samr_close(cli, mem_ctx, &domain_pol);
2062 rpccli_samr_close(cli, mem_ctx, &connect_pol);
2064 return result;
2068 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2069 TALLOC_CTX *mem_ctx,
2070 int argc, const char **argv)
2072 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2073 uint16 min_pwd_length;
2074 uint32 password_properties;
2076 if (argc != 1) {
2077 printf("Usage: %s\n", argv[0]);
2078 return NT_STATUS_OK;
2081 result = rpccli_samr_get_dom_pwinfo(cli, mem_ctx, &min_pwd_length, &password_properties) ;
2083 if (NT_STATUS_IS_OK(result)) {
2084 printf("min_pwd_length: %d\n", min_pwd_length);
2085 display_password_properties(password_properties);
2088 return result;
2091 /* Look up domain name */
2093 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2094 TALLOC_CTX *mem_ctx,
2095 int argc, const char **argv)
2097 POLICY_HND connect_pol, domain_pol;
2098 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2099 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2100 fstring domain_name,sid_string;
2101 DOM_SID sid;
2103 if (argc != 2) {
2104 printf("Usage: %s domain_name\n", argv[0]);
2105 return NT_STATUS_OK;
2108 sscanf(argv[1], "%s", domain_name);
2110 result = try_samr_connects(cli, mem_ctx, access_mask, &connect_pol);
2112 if (!NT_STATUS_IS_OK(result))
2113 goto done;
2115 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
2116 access_mask, &domain_sid, &domain_pol);
2118 if (!NT_STATUS_IS_OK(result))
2119 goto done;
2121 result = rpccli_samr_lookup_domain(
2122 cli, mem_ctx, &connect_pol, domain_name, &sid);
2124 sid_to_string(sid_string,&sid);
2126 if (NT_STATUS_IS_OK(result))
2127 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2128 domain_name,sid_string);
2130 rpccli_samr_close(cli, mem_ctx, &domain_pol);
2131 rpccli_samr_close(cli, mem_ctx, &connect_pol);
2132 done:
2133 return result;
2136 /* Change user password */
2138 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2139 TALLOC_CTX *mem_ctx,
2140 int argc, const char **argv)
2142 POLICY_HND connect_pol, domain_pol;
2143 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2144 const char *user, *oldpass, *newpass;
2145 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2146 SAM_UNK_INFO_1 info;
2147 SAMR_CHANGE_REJECT reject;
2149 if (argc < 3) {
2150 printf("Usage: %s username oldpass newpass\n", argv[0]);
2151 return NT_STATUS_INVALID_PARAMETER;
2154 user = argv[1];
2155 oldpass = argv[2];
2156 newpass = argv[3];
2158 /* Get sam policy handle */
2160 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2161 &connect_pol);
2163 if (!NT_STATUS_IS_OK(result))
2164 goto done;
2166 /* Get domain policy handle */
2168 result = rpccli_samr_open_domain(cli, mem_ctx, &connect_pol,
2169 access_mask,
2170 &domain_sid, &domain_pol);
2172 if (!NT_STATUS_IS_OK(result))
2173 goto done;
2175 /* Change user password */
2176 result = rpccli_samr_chgpasswd3(cli, mem_ctx, user, newpass, oldpass, &info, &reject);
2178 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2180 display_sam_unk_info_1(&info);
2182 switch (reject.reject_reason) {
2183 case REJECT_REASON_TOO_SHORT:
2184 d_printf("REJECT_REASON_TOO_SHORT\n");
2185 break;
2186 case REJECT_REASON_IN_HISTORY:
2187 d_printf("REJECT_REASON_IN_HISTORY\n");
2188 break;
2189 case REJECT_REASON_NOT_COMPLEX:
2190 d_printf("REJECT_REASON_NOT_COMPLEX\n");
2191 break;
2192 case REJECT_REASON_OTHER:
2193 d_printf("REJECT_REASON_OTHER\n");
2194 break;
2195 default:
2196 d_printf("unknown reject reason: %d\n", reject.reject_reason);
2197 break;
2201 if (!NT_STATUS_IS_OK(result))
2202 goto done;
2204 result = rpccli_samr_close(cli, mem_ctx, &domain_pol);
2205 if (!NT_STATUS_IS_OK(result)) goto done;
2207 result = rpccli_samr_close(cli, mem_ctx, &connect_pol);
2208 if (!NT_STATUS_IS_OK(result)) goto done;
2210 done:
2211 return result;
2214 /* List of commands exported by this module */
2216 struct cmd_set samr_commands[] = {
2218 { "SAMR" },
2220 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, PI_SAMR, NULL, "Query user info", "" },
2221 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, PI_SAMR, NULL, "Query group info", "" },
2222 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, PI_SAMR, NULL, "Query user groups", "" },
2223 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, PI_SAMR, NULL, "Query user aliases", "" },
2224 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, PI_SAMR, NULL, "Query group membership", "" },
2225 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, PI_SAMR, NULL, "Query alias membership", "" },
2226 { "deletealias", RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias, NULL, PI_SAMR, NULL, "Delete an alias", "" },
2227 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, PI_SAMR, NULL, "Query display info", "" },
2228 { "querydispinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2, NULL, PI_SAMR, NULL, "Query display info 2", "" },
2229 { "querydispinfo3", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3, NULL, PI_SAMR, NULL, "Query display info 3", "" },
2230 { "getdispenumindex", RPC_RTYPE_NTSTATUS, cmd_samr_get_dispenum_index, NULL, PI_SAMR, NULL, "Query display info index", "" },
2231 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, PI_SAMR, NULL, "Query domain info", "" },
2232 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, PI_SAMR, NULL, "Enumerate domain users", "" },
2233 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, PI_SAMR, NULL, "Enumerate domain groups", "" },
2234 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, PI_SAMR, NULL, "Enumerate alias groups", "" },
2236 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, PI_SAMR, NULL, "Create domain user", "" },
2237 { "createdomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group, NULL, PI_SAMR, NULL, "Create domain group", "" },
2238 { "createdomalias", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias, NULL, PI_SAMR, NULL, "Create domain alias", "" },
2239 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, PI_SAMR, NULL, "Look up names", "" },
2240 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, PI_SAMR, NULL, "Look up names", "" },
2241 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, PI_SAMR, NULL, "Delete domain user", "" },
2242 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, PI_SAMR, NULL, "Query SAMR security object", "" },
2243 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, PI_SAMR, NULL, "Retrieve domain password info", "" },
2244 { "getusrdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo, NULL, PI_SAMR, NULL, "Retrieve user domain password info", "" },
2246 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, PI_SAMR, NULL, "Lookup Domain Name", "" },
2247 { "chgpasswd3", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3, NULL, PI_SAMR, NULL, "Change user password", "" },
2248 { NULL }