r6334: revert 3.0.15pre1 changes. roll back to 3.0.14.
[Samba/gbeck.git] / source / rpcclient / cmd_samr.c
blobd3f8954050201430a0e047c5080d34699faf59c1
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_21 structure
43 ****************************************************************************/
44 static void display_sam_user_info_21(SAM_USER_INFO_21 *usr)
46 fstring temp;
48 unistr2_to_ascii(temp, &usr->uni_user_name, sizeof(temp)-1);
49 printf("\tUser Name :\t%s\n", temp);
51 unistr2_to_ascii(temp, &usr->uni_full_name, sizeof(temp)-1);
52 printf("\tFull Name :\t%s\n", temp);
54 unistr2_to_ascii(temp, &usr->uni_home_dir, sizeof(temp)-1);
55 printf("\tHome Drive :\t%s\n", temp);
57 unistr2_to_ascii(temp, &usr->uni_dir_drive, sizeof(temp)-1);
58 printf("\tDir Drive :\t%s\n", temp);
60 unistr2_to_ascii(temp, &usr->uni_profile_path, sizeof(temp)-1);
61 printf("\tProfile Path:\t%s\n", temp);
63 unistr2_to_ascii(temp, &usr->uni_logon_script, sizeof(temp)-1);
64 printf("\tLogon Script:\t%s\n", temp);
66 unistr2_to_ascii(temp, &usr->uni_acct_desc, sizeof(temp)-1);
67 printf("\tDescription :\t%s\n", temp);
69 unistr2_to_ascii(temp, &usr->uni_workstations, sizeof(temp)-1);
70 printf("\tWorkstations:\t%s\n", temp);
72 unistr2_to_ascii(temp, &usr->uni_unknown_str, sizeof(temp)-1);
73 printf("\tUnknown Str :\t%s\n", temp);
75 unistr2_to_ascii(temp, &usr->uni_munged_dial, sizeof(temp)-1);
76 printf("\tRemote Dial :\t%s\n", temp);
78 printf("\tLogon Time :\t%s\n",
79 http_timestring(nt_time_to_unix(&usr->logon_time)));
80 printf("\tLogoff Time :\t%s\n",
81 http_timestring(nt_time_to_unix(&usr->logoff_time)));
82 printf("\tKickoff Time :\t%s\n",
83 http_timestring(nt_time_to_unix(&usr->kickoff_time)));
84 printf("\tPassword last set Time :\t%s\n",
85 http_timestring(nt_time_to_unix(&usr->pass_last_set_time)));
86 printf("\tPassword can change Time :\t%s\n",
87 http_timestring(nt_time_to_unix(&usr->pass_can_change_time)));
88 printf("\tPassword must change Time:\t%s\n",
89 http_timestring(nt_time_to_unix(&usr->pass_must_change_time)));
91 printf("\tunknown_2[0..31]...\n"); /* user passwords? */
93 printf("\tuser_rid :\t0x%x\n" , usr->user_rid ); /* User ID */
94 printf("\tgroup_rid:\t0x%x\n" , usr->group_rid); /* Group ID */
95 printf("\tacb_info :\t0x%04x\n", usr->acb_info ); /* Account Control Info */
97 printf("\tfields_present:\t0x%08x\n", usr->fields_present); /* 0x00ff ffff */
98 printf("\tlogon_divs:\t%d\n", usr->logon_divs); /* 0x0000 00a8 which is 168 which is num hrs in a week */
99 printf("\tbad_password_count:\t0x%08x\n", usr->bad_password_count);
100 printf("\tlogon_count:\t0x%08x\n", usr->logon_count);
102 printf("\tpadding1[0..7]...\n");
104 if (usr->ptr_logon_hrs) {
105 printf("\tlogon_hrs[0..%d]...\n", usr->logon_hrs.len);
109 static const char *display_time(NTTIME nttime)
111 static fstring string;
113 float high;
114 float low;
115 int sec;
116 int days, hours, mins, secs;
118 if (nttime.high==0 && nttime.low==0)
119 return "Now";
121 if (nttime.high==0x80000000 && nttime.low==0)
122 return "Never";
124 high = 65536;
125 high = high/10000;
126 high = high*65536;
127 high = high/1000;
128 high = high * (~nttime.high);
130 low = ~nttime.low;
131 low = low/(1000*1000*10);
133 sec=high+low;
135 days=sec/(60*60*24);
136 hours=(sec - (days*60*60*24)) / (60*60);
137 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
138 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
140 fstr_sprintf(string, "%u days, %u hours, %u minutes, %u seconds", days, hours, mins, secs);
141 return (string);
144 static const char* server_role_str(uint32 server_role)
146 switch(server_role) {
147 case ROLE_STANDALONE:
148 return strdup("ROLE_STANDALONE");
149 break;
150 case ROLE_DOMAIN_MEMBER:
151 return strdup("ROLE_DOMAIN_MEMBER");
152 break;
153 case ROLE_DOMAIN_BDC:
154 return strdup("ROLE_DOMAIN_BDC");
155 break;
156 case ROLE_DOMAIN_PDC:
157 return strdup("ROLE_DOMAIN_PDC");
158 break;
159 default:
160 return strdup("Unknown -- internal error?");
161 break;
165 static void display_sam_unk_info_1(SAM_UNK_INFO_1 *info1)
168 printf("Minimum password length: %d\n", info1->min_length_password);
169 printf("Password uniqueness (remember x passwords): %d\n", info1->password_history);
170 printf("flag: ");
171 if(info1->flag&&2==2) printf("users must open a session to change password ");
172 printf("\n");
174 printf("password expire in: %s\n", display_time(info1->expire));
175 printf("Min password age (allow changing in x days): %s\n", display_time(info1->min_passwordage));
178 static void display_sam_unk_info_2(SAM_UNK_INFO_2 *info2)
180 fstring name;
182 unistr2_to_ascii(name, &info2->uni_domain, sizeof(name) - 1);
183 printf("Domain:\t\t%s\n", name);
185 unistr2_to_ascii(name, &info2->uni_server, sizeof(name) - 1);
186 printf("Server:\t\t%s\n", name);
188 unistr2_to_ascii(name, &info2->uni_comment, sizeof(name) - 1);
189 printf("Comment:\t%s\n", name);
191 printf("Total Users:\t%d\n", info2->num_domain_usrs);
192 printf("Total Groups:\t%d\n", info2->num_domain_grps);
193 printf("Total Aliases:\t%d\n", info2->num_local_grps);
195 printf("Sequence No:\t%d\n", info2->seq_num.low);
197 printf("Force Logoff:\t%d\n", (int)nt_time_to_unix_abs(&info2->logout));
199 printf("Unknown 4:\t0x%x\n", info2->unknown_4);
200 printf("Server Role:\t%s\n", server_role_str(info2->server_role));
201 printf("Unknown 6:\t0x%x\n", info2->unknown_6);
204 static void display_sam_unk_info_7(SAM_UNK_INFO_7 *info7)
206 printf("Server Role:\t%s\n", server_role_str(info7->server_role));
209 static void display_sam_unk_info_8(SAM_UNK_INFO_8 *info8)
211 printf("Sequence No:\t%d\n", info8->seq_num.low);
212 printf("Domain Create Time:\t%s\n",
213 http_timestring(nt_time_to_unix(&info8->domain_create_time)));
217 static void display_sam_unk_info_12(SAM_UNK_INFO_12 *info12)
219 printf("Bad password lockout duration: %s\n", display_time(info12->duration));
220 printf("Reset Lockout after: %s\n", display_time(info12->reset_count));
221 printf("Lockout after bad attempts: %d\n", info12->bad_attempt_lockout);
224 static void display_sam_info_1(SAM_ENTRY1 *e1, SAM_STR1 *s1)
226 fstring tmp;
228 printf("index: 0x%x ", e1->user_idx);
229 printf("RID: 0x%x ", e1->rid_user);
230 printf("acb: 0x%x ", e1->acb_info);
232 unistr2_to_ascii(tmp, &s1->uni_acct_name, sizeof(tmp)-1);
233 printf("Account: %s\t", tmp);
235 unistr2_to_ascii(tmp, &s1->uni_full_name, sizeof(tmp)-1);
236 printf("Name: %s\t", tmp);
238 unistr2_to_ascii(tmp, &s1->uni_acct_desc, sizeof(tmp)-1);
239 printf("Desc: %s\n", tmp);
242 static void display_sam_info_2(SAM_ENTRY2 *e2, SAM_STR2 *s2)
244 fstring tmp;
246 printf("index: 0x%x ", e2->user_idx);
247 printf("RID: 0x%x ", e2->rid_user);
248 printf("acb: 0x%x ", e2->acb_info);
250 unistr2_to_ascii(tmp, &s2->uni_srv_name, sizeof(tmp)-1);
251 printf("Account: %s\t", tmp);
253 unistr2_to_ascii(tmp, &s2->uni_srv_desc, sizeof(tmp)-1);
254 printf("Name: %s\n", tmp);
258 static void display_sam_info_3(SAM_ENTRY3 *e3, SAM_STR3 *s3)
260 fstring tmp;
262 printf("index: 0x%x ", e3->grp_idx);
263 printf("RID: 0x%x ", e3->rid_grp);
264 printf("attr: 0x%x ", e3->attr);
266 unistr2_to_ascii(tmp, &s3->uni_grp_name, sizeof(tmp)-1);
267 printf("Account: %s\t", tmp);
269 unistr2_to_ascii(tmp, &s3->uni_grp_desc, sizeof(tmp)-1);
270 printf("Name: %s\n", tmp);
274 static void display_sam_info_4(SAM_ENTRY4 *e4, SAM_STR4 *s4)
276 int i;
278 printf("index: %d ", e4->user_idx);
280 printf("Account: ");
281 for (i=0; i<s4->acct_name.str_str_len; i++)
282 printf("%c", s4->acct_name.buffer[i]);
283 printf("\n");
287 static void display_sam_info_5(SAM_ENTRY5 *e5, SAM_STR5 *s5)
289 int i;
291 printf("index: 0x%x ", e5->grp_idx);
293 printf("Account: ");
294 for (i=0; i<s5->grp_name.str_str_len; i++)
295 printf("%c", s5->grp_name.buffer[i]);
296 printf("\n");
300 /****************************************************************************
301 Try samr_connect4 first, then samr_conenct if it fails
302 ****************************************************************************/
303 static NTSTATUS try_samr_connects(struct cli_state *cli, TALLOC_CTX *mem_ctx,
304 uint32 access_mask, POLICY_HND *connect_pol)
306 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
308 result = cli_samr_connect4(cli, mem_ctx, access_mask, connect_pol);
309 if (!NT_STATUS_IS_OK(result)) {
310 result = cli_samr_connect(cli, mem_ctx, access_mask,
311 connect_pol);
313 return result;
316 /**********************************************************************
317 * Query user information
319 static NTSTATUS cmd_samr_query_user(struct cli_state *cli,
320 TALLOC_CTX *mem_ctx,
321 int argc, const char **argv)
323 POLICY_HND connect_pol, domain_pol, user_pol;
324 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
325 uint32 info_level = 21;
326 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
327 SAM_USERINFO_CTR *user_ctr;
328 fstring server;
329 uint32 user_rid;
331 if ((argc < 2) || (argc > 4)) {
332 printf("Usage: %s rid [info level] [access mask] \n", argv[0]);
333 return NT_STATUS_OK;
336 sscanf(argv[1], "%i", &user_rid);
338 if (argc > 2)
339 sscanf(argv[2], "%i", &info_level);
341 if (argc > 3)
342 sscanf(argv[3], "%x", &access_mask);
345 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
346 strupper_m(server);
348 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
349 &connect_pol);
351 if (!NT_STATUS_IS_OK(result))
352 goto done;
354 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
355 MAXIMUM_ALLOWED_ACCESS,
356 &domain_sid, &domain_pol);
358 if (!NT_STATUS_IS_OK(result))
359 goto done;
361 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
362 access_mask,
363 user_rid, &user_pol);
365 if (!NT_STATUS_IS_OK(result))
366 goto done;
368 ZERO_STRUCT(user_ctr);
370 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
371 info_level, &user_ctr);
373 if (!NT_STATUS_IS_OK(result))
374 goto done;
376 switch (user_ctr->switch_value) {
377 case 21:
378 display_sam_user_info_21(user_ctr->info.id21);
379 break;
380 case 7:
381 display_sam_user_info_7(user_ctr->info.id7);
382 break;
383 default:
384 printf("Unsupported infolevel: %d\n", info_level);
385 break;
388 done:
389 return result;
392 /****************************************************************************
393 display group info
394 ****************************************************************************/
395 static void display_group_info1(GROUP_INFO1 *info1)
397 fstring temp;
399 unistr2_to_ascii(temp, &info1->uni_acct_name, sizeof(temp)-1);
400 printf("\tGroup Name:\t%s\n", temp);
401 unistr2_to_ascii(temp, &info1->uni_acct_desc, sizeof(temp)-1);
402 printf("\tDescription:\t%s\n", temp);
403 printf("\tunk1:%d\n", info1->unknown_1);
404 printf("\tNum Members:%d\n", info1->num_members);
407 /****************************************************************************
408 display group info
409 ****************************************************************************/
410 static void display_group_info4(GROUP_INFO4 *info4)
412 fstring desc;
414 unistr2_to_ascii(desc, &info4->uni_acct_desc, sizeof(desc)-1);
415 printf("\tGroup Description:%s\n", desc);
418 /****************************************************************************
419 display sam sync structure
420 ****************************************************************************/
421 static void display_group_info_ctr(GROUP_INFO_CTR *ctr)
423 switch (ctr->switch_value1) {
424 case 1: {
425 display_group_info1(&ctr->group.info1);
426 break;
428 case 4: {
429 display_group_info4(&ctr->group.info4);
430 break;
435 /***********************************************************************
436 * Query group information
438 static NTSTATUS cmd_samr_query_group(struct cli_state *cli,
439 TALLOC_CTX *mem_ctx,
440 int argc, const char **argv)
442 POLICY_HND connect_pol, domain_pol, group_pol;
443 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
444 uint32 info_level = 1;
445 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
446 GROUP_INFO_CTR *group_ctr;
447 fstring server;
448 uint32 group_rid;
450 if ((argc < 2) || (argc > 4)) {
451 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
452 return NT_STATUS_OK;
455 sscanf(argv[1], "%i", &group_rid);
457 if (argc > 2)
458 sscanf(argv[2], "%i", &info_level);
460 if (argc > 3)
461 sscanf(argv[3], "%x", &access_mask);
463 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
464 strupper_m(server);
466 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
467 &connect_pol);
469 if (!NT_STATUS_IS_OK(result))
470 goto done;
472 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
473 MAXIMUM_ALLOWED_ACCESS,
474 &domain_sid, &domain_pol);
476 if (!NT_STATUS_IS_OK(result))
477 goto done;
479 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
480 access_mask,
481 group_rid, &group_pol);
483 if (!NT_STATUS_IS_OK(result))
484 goto done;
486 result = cli_samr_query_groupinfo(cli, mem_ctx, &group_pol,
487 info_level, &group_ctr);
488 if (!NT_STATUS_IS_OK(result)) {
489 goto done;
492 display_group_info_ctr(group_ctr);
494 done:
495 return result;
498 /* Query groups a user is a member of */
500 static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli,
501 TALLOC_CTX *mem_ctx,
502 int argc, const char **argv)
504 POLICY_HND connect_pol,
505 domain_pol,
506 user_pol;
507 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
508 uint32 num_groups,
509 user_rid;
510 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
511 DOM_GID *user_gids;
512 int i;
513 fstring server;
515 if ((argc < 2) || (argc > 3)) {
516 printf("Usage: %s rid [access mask]\n", argv[0]);
517 return NT_STATUS_OK;
520 sscanf(argv[1], "%i", &user_rid);
522 if (argc > 2)
523 sscanf(argv[2], "%x", &access_mask);
525 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
526 strupper_m(server);
528 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
529 &connect_pol);
531 if (!NT_STATUS_IS_OK(result))
532 goto done;
534 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
535 MAXIMUM_ALLOWED_ACCESS,
536 &domain_sid, &domain_pol);
538 if (!NT_STATUS_IS_OK(result))
539 goto done;
541 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
542 access_mask,
543 user_rid, &user_pol);
545 if (!NT_STATUS_IS_OK(result))
546 goto done;
548 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
549 &num_groups, &user_gids);
551 if (!NT_STATUS_IS_OK(result))
552 goto done;
554 for (i = 0; i < num_groups; i++) {
555 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
556 user_gids[i].g_rid, user_gids[i].attr);
559 done:
560 return result;
563 /* Query aliases a user is a member of */
565 static NTSTATUS cmd_samr_query_useraliases(struct cli_state *cli,
566 TALLOC_CTX *mem_ctx,
567 int argc, const char **argv)
569 POLICY_HND connect_pol, domain_pol;
570 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
571 uint32 user_rid, num_aliases, *alias_rids;
572 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
573 int i;
574 fstring server;
575 DOM_SID tmp_sid;
576 DOM_SID2 sid;
577 DOM_SID global_sid_Builtin;
579 string_to_sid(&global_sid_Builtin, "S-1-5-32");
581 if ((argc < 3) || (argc > 4)) {
582 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
583 return NT_STATUS_OK;
586 sscanf(argv[2], "%i", &user_rid);
588 if (argc > 3)
589 sscanf(argv[3], "%x", &access_mask);
591 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
592 strupper_m(server);
594 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
595 &connect_pol);
597 if (!NT_STATUS_IS_OK(result))
598 goto done;
600 if (StrCaseCmp(argv[1], "domain")==0)
601 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
602 access_mask,
603 &domain_sid, &domain_pol);
604 else if (StrCaseCmp(argv[1], "builtin")==0)
605 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
606 access_mask,
607 &global_sid_Builtin, &domain_pol);
608 else
609 return NT_STATUS_OK;
611 if (!NT_STATUS_IS_OK(result))
612 goto done;
614 sid_copy(&tmp_sid, &domain_sid);
615 sid_append_rid(&tmp_sid, user_rid);
616 init_dom_sid2(&sid, &tmp_sid);
618 result = cli_samr_query_useraliases(cli, mem_ctx, &domain_pol, 1, &sid, &num_aliases, &alias_rids);
620 if (!NT_STATUS_IS_OK(result))
621 goto done;
623 for (i = 0; i < num_aliases; i++) {
624 printf("\tgroup rid:[0x%x]\n", alias_rids[i]);
627 done:
628 return result;
631 /* Query members of a group */
633 static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli,
634 TALLOC_CTX *mem_ctx,
635 int argc, const char **argv)
637 POLICY_HND connect_pol, domain_pol, group_pol;
638 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
639 uint32 num_members, *group_rids, *group_attrs, group_rid;
640 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
641 int i;
642 fstring server;
644 if ((argc < 2) || (argc > 3)) {
645 printf("Usage: %s rid [access mask]\n", argv[0]);
646 return NT_STATUS_OK;
649 sscanf(argv[1], "%i", &group_rid);
651 if (argc > 2)
652 sscanf(argv[2], "%x", &access_mask);
654 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
655 strupper_m(server);
657 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
658 &connect_pol);
660 if (!NT_STATUS_IS_OK(result))
661 goto done;
663 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
664 MAXIMUM_ALLOWED_ACCESS,
665 &domain_sid, &domain_pol);
667 if (!NT_STATUS_IS_OK(result))
668 goto done;
670 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
671 access_mask,
672 group_rid, &group_pol);
674 if (!NT_STATUS_IS_OK(result))
675 goto done;
677 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
678 &num_members, &group_rids,
679 &group_attrs);
681 if (!NT_STATUS_IS_OK(result))
682 goto done;
684 for (i = 0; i < num_members; i++) {
685 printf("\trid:[0x%x] attr:[0x%x]\n", group_rids[i],
686 group_attrs[i]);
689 done:
690 return result;
693 /* Enumerate domain users */
695 static NTSTATUS cmd_samr_enum_dom_users(struct cli_state *cli,
696 TALLOC_CTX *mem_ctx,
697 int argc, const char **argv)
699 POLICY_HND connect_pol, domain_pol;
700 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
701 uint32 start_idx, size, num_dom_users, i;
702 char **dom_users;
703 uint32 *dom_rids;
704 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
705 uint16 acb_mask = ACB_NORMAL;
706 BOOL got_connect_pol = False, got_domain_pol = False;
708 if ((argc < 1) || (argc > 3)) {
709 printf("Usage: %s [access_mask] [acb_mask]\n", argv[0]);
710 return NT_STATUS_OK;
713 if (argc > 1)
714 sscanf(argv[1], "%x", &access_mask);
716 if (argc > 2)
717 sscanf(argv[2], "%hx", &acb_mask);
719 /* Get sam policy handle */
721 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
722 &connect_pol);
724 if (!NT_STATUS_IS_OK(result))
725 goto done;
727 got_connect_pol = True;
729 /* Get domain policy handle */
731 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
732 access_mask,
733 &domain_sid, &domain_pol);
735 if (!NT_STATUS_IS_OK(result))
736 goto done;
738 got_domain_pol = True;
740 /* Enumerate domain users */
742 start_idx = 0;
743 size = 0xffff;
745 do {
746 result = cli_samr_enum_dom_users(
747 cli, mem_ctx, &domain_pol, &start_idx, acb_mask,
748 size, &dom_users, &dom_rids, &num_dom_users);
750 if (NT_STATUS_IS_OK(result) ||
751 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
753 for (i = 0; i < num_dom_users; i++)
754 printf("user:[%s] rid:[0x%x]\n",
755 dom_users[i], dom_rids[i]);
758 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
760 done:
761 if (got_domain_pol)
762 cli_samr_close(cli, mem_ctx, &domain_pol);
764 if (got_connect_pol)
765 cli_samr_close(cli, mem_ctx, &connect_pol);
767 return result;
770 /* Enumerate domain groups */
772 static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli,
773 TALLOC_CTX *mem_ctx,
774 int argc, const char **argv)
776 POLICY_HND connect_pol, domain_pol;
777 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
778 uint32 start_idx, size, num_dom_groups, i;
779 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
780 struct acct_info *dom_groups;
781 BOOL got_connect_pol = False, got_domain_pol = False;
783 if ((argc < 1) || (argc > 2)) {
784 printf("Usage: %s [access_mask]\n", argv[0]);
785 return NT_STATUS_OK;
788 if (argc > 1)
789 sscanf(argv[1], "%x", &access_mask);
791 /* Get sam policy handle */
793 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
794 &connect_pol);
796 if (!NT_STATUS_IS_OK(result))
797 goto done;
799 got_connect_pol = True;
801 /* Get domain policy handle */
803 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
804 access_mask,
805 &domain_sid, &domain_pol);
807 if (!NT_STATUS_IS_OK(result))
808 goto done;
810 got_domain_pol = True;
812 /* Enumerate domain groups */
814 start_idx = 0;
815 size = 0xffff;
817 do {
818 result = cli_samr_enum_dom_groups(
819 cli, mem_ctx, &domain_pol, &start_idx, size,
820 &dom_groups, &num_dom_groups);
822 if (NT_STATUS_IS_OK(result) ||
823 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
825 for (i = 0; i < num_dom_groups; i++)
826 printf("group:[%s] rid:[0x%x]\n",
827 dom_groups[i].acct_name,
828 dom_groups[i].rid);
831 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
833 done:
834 if (got_domain_pol)
835 cli_samr_close(cli, mem_ctx, &domain_pol);
837 if (got_connect_pol)
838 cli_samr_close(cli, mem_ctx, &connect_pol);
840 return result;
843 /* Enumerate alias groups */
845 static NTSTATUS cmd_samr_enum_als_groups(struct cli_state *cli,
846 TALLOC_CTX *mem_ctx,
847 int argc, const char **argv)
849 POLICY_HND connect_pol, domain_pol;
850 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
851 uint32 start_idx, size, num_als_groups, i;
852 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
853 struct acct_info *als_groups;
854 DOM_SID global_sid_Builtin;
855 BOOL got_connect_pol = False, got_domain_pol = False;
857 string_to_sid(&global_sid_Builtin, "S-1-5-32");
859 if ((argc < 2) || (argc > 3)) {
860 printf("Usage: %s builtin|domain [access mask]\n", argv[0]);
861 return NT_STATUS_OK;
864 if (argc > 2)
865 sscanf(argv[2], "%x", &access_mask);
867 /* Get sam policy handle */
869 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
870 &connect_pol);
872 if (!NT_STATUS_IS_OK(result))
873 goto done;
875 got_connect_pol = True;
877 /* Get domain policy handle */
879 if (StrCaseCmp(argv[1], "domain")==0)
880 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
881 access_mask,
882 &domain_sid, &domain_pol);
883 else if (StrCaseCmp(argv[1], "builtin")==0)
884 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
885 access_mask,
886 &global_sid_Builtin, &domain_pol);
887 else
888 return NT_STATUS_OK;
890 if (!NT_STATUS_IS_OK(result))
891 goto done;
893 got_domain_pol = True;
895 /* Enumerate alias groups */
897 start_idx = 0;
898 size = 0xffff; /* Number of groups to retrieve */
900 do {
901 result = cli_samr_enum_als_groups(
902 cli, mem_ctx, &domain_pol, &start_idx, size,
903 &als_groups, &num_als_groups);
905 if (NT_STATUS_IS_OK(result) ||
906 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
908 for (i = 0; i < num_als_groups; i++)
909 printf("group:[%s] rid:[0x%x]\n",
910 als_groups[i].acct_name,
911 als_groups[i].rid);
913 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
915 done:
916 if (got_domain_pol)
917 cli_samr_close(cli, mem_ctx, &domain_pol);
919 if (got_connect_pol)
920 cli_samr_close(cli, mem_ctx, &connect_pol);
922 return result;
925 /* Query alias membership */
927 static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli,
928 TALLOC_CTX *mem_ctx,
929 int argc, const char **argv)
931 POLICY_HND connect_pol, domain_pol, alias_pol;
932 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
933 uint32 alias_rid, num_members, i;
934 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
935 DOM_SID *alias_sids;
936 DOM_SID global_sid_Builtin;
938 string_to_sid(&global_sid_Builtin, "S-1-5-32");
940 if ((argc < 3) || (argc > 4)) {
941 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
942 return NT_STATUS_OK;
945 sscanf(argv[2], "%i", &alias_rid);
947 if (argc > 3)
948 sscanf(argv[3], "%x", &access_mask);
950 /* Open SAMR handle */
952 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
953 &connect_pol);
955 if (!NT_STATUS_IS_OK(result))
956 goto done;
958 /* Open handle on domain */
960 if (StrCaseCmp(argv[1], "domain")==0)
961 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
962 MAXIMUM_ALLOWED_ACCESS,
963 &domain_sid, &domain_pol);
964 else if (StrCaseCmp(argv[1], "builtin")==0)
965 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
966 MAXIMUM_ALLOWED_ACCESS,
967 &global_sid_Builtin, &domain_pol);
968 else
969 return NT_STATUS_OK;
971 if (!NT_STATUS_IS_OK(result))
972 goto done;
974 /* Open handle on alias */
976 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
977 access_mask,
978 alias_rid, &alias_pol);
979 if (!NT_STATUS_IS_OK(result))
980 goto done;
982 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
983 &num_members, &alias_sids);
985 if (!NT_STATUS_IS_OK(result))
986 goto done;
988 for (i = 0; i < num_members; i++) {
989 fstring sid_str;
991 sid_to_string(sid_str, &alias_sids[i]);
992 printf("\tsid:[%s]\n", sid_str);
995 done:
996 return result;
999 /* Query display info */
1001 static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli,
1002 TALLOC_CTX *mem_ctx,
1003 int argc, const char **argv)
1005 POLICY_HND connect_pol, domain_pol;
1006 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1007 uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries, i;
1008 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1009 uint32 info_level = 1;
1010 SAM_DISPINFO_CTR ctr;
1011 SAM_DISPINFO_1 info1;
1012 SAM_DISPINFO_2 info2;
1013 SAM_DISPINFO_3 info3;
1014 SAM_DISPINFO_4 info4;
1015 SAM_DISPINFO_5 info5;
1016 int loop_count = 0;
1017 BOOL got_params = False; /* Use get_query_dispinfo_params() or not? */
1019 if (argc > 5) {
1020 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1021 return NT_STATUS_OK;
1024 if (argc >= 2)
1025 sscanf(argv[1], "%i", &info_level);
1027 if (argc >= 3)
1028 sscanf(argv[2], "%i", &start_idx);
1030 if (argc >= 4) {
1031 sscanf(argv[3], "%i", &max_entries);
1032 got_params = True;
1035 if (argc >= 5) {
1036 sscanf(argv[4], "%i", &max_size);
1037 got_params = True;
1040 if (argc >= 6)
1041 sscanf(argv[5], "%x", &access_mask);
1043 /* Get sam policy handle */
1045 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1046 &connect_pol);
1048 if (!NT_STATUS_IS_OK(result))
1049 goto done;
1051 /* Get domain policy handle */
1053 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1054 access_mask,
1055 &domain_sid, &domain_pol);
1057 if (!NT_STATUS_IS_OK(result))
1058 goto done;
1060 /* Query display info */
1062 ZERO_STRUCT(ctr);
1063 ZERO_STRUCT(info1);
1065 switch (info_level) {
1066 case 1:
1067 ZERO_STRUCT(info1);
1068 ctr.sam.info1 = &info1;
1069 break;
1070 case 2:
1071 ZERO_STRUCT(info2);
1072 ctr.sam.info2 = &info2;
1073 break;
1074 case 3:
1075 ZERO_STRUCT(info3);
1076 ctr.sam.info3 = &info3;
1077 break;
1078 case 4:
1079 ZERO_STRUCT(info4);
1080 ctr.sam.info4 = &info4;
1081 break;
1082 case 5:
1083 ZERO_STRUCT(info5);
1084 ctr.sam.info5 = &info5;
1085 break;
1089 while(1) {
1091 if (!got_params)
1092 get_query_dispinfo_params(
1093 loop_count, &max_entries, &max_size);
1095 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1096 &start_idx, info_level,
1097 &num_entries, max_entries,
1098 max_size, &ctr);
1100 loop_count++;
1102 if (!NT_STATUS_IS_OK(result) && !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1103 break;
1105 if (num_entries == 0)
1106 break;
1108 for (i = 0; i < num_entries; i++) {
1109 switch (info_level) {
1110 case 1:
1111 display_sam_info_1(&ctr.sam.info1->sam[i], &ctr.sam.info1->str[i]);
1112 break;
1113 case 2:
1114 display_sam_info_2(&ctr.sam.info2->sam[i], &ctr.sam.info2->str[i]);
1115 break;
1116 case 3:
1117 display_sam_info_3(&ctr.sam.info3->sam[i], &ctr.sam.info3->str[i]);
1118 break;
1119 case 4:
1120 display_sam_info_4(&ctr.sam.info4->sam[i], &ctr.sam.info4->str[i]);
1121 break;
1122 case 5:
1123 display_sam_info_5(&ctr.sam.info5->sam[i], &ctr.sam.info5->str[i]);
1124 break;
1129 done:
1130 return result;
1133 /* Query domain info */
1135 static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli,
1136 TALLOC_CTX *mem_ctx,
1137 int argc, const char **argv)
1139 POLICY_HND connect_pol, domain_pol;
1140 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1141 uint32 switch_level = 2;
1142 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1143 SAM_UNK_CTR ctr;
1145 if (argc > 2) {
1146 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1147 return NT_STATUS_OK;
1150 if (argc > 1)
1151 sscanf(argv[1], "%i", &switch_level);
1153 if (argc > 2)
1154 sscanf(argv[2], "%x", &access_mask);
1156 /* Get sam policy handle */
1158 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1159 &connect_pol);
1161 if (!NT_STATUS_IS_OK(result))
1162 goto done;
1164 /* Get domain policy handle */
1166 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1167 access_mask,
1168 &domain_sid, &domain_pol);
1170 if (!NT_STATUS_IS_OK(result))
1171 goto done;
1173 /* Query domain info */
1175 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
1176 switch_level, &ctr);
1178 if (!NT_STATUS_IS_OK(result))
1179 goto done;
1181 /* Display domain info */
1183 switch (switch_level) {
1184 case 1:
1185 display_sam_unk_info_1(&ctr.info.inf1);
1186 break;
1187 case 2:
1188 display_sam_unk_info_2(&ctr.info.inf2);
1189 break;
1190 case 7:
1191 display_sam_unk_info_7(&ctr.info.inf7);
1192 break;
1193 case 8:
1194 display_sam_unk_info_8(&ctr.info.inf8);
1195 break;
1196 case 12:
1197 display_sam_unk_info_12(&ctr.info.inf12);
1198 break;
1199 default:
1200 printf("cannot display domain info for switch value %d\n",
1201 switch_level);
1202 break;
1205 done:
1207 cli_samr_close(cli, mem_ctx, &domain_pol);
1208 cli_samr_close(cli, mem_ctx, &connect_pol);
1209 return result;
1212 /* Create domain user */
1214 static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli,
1215 TALLOC_CTX *mem_ctx,
1216 int argc, const char **argv)
1218 POLICY_HND connect_pol, domain_pol, user_pol;
1219 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1220 const char *acct_name;
1221 uint16 acb_info;
1222 uint32 unknown, user_rid;
1223 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1225 if ((argc < 2) || (argc > 3)) {
1226 printf("Usage: %s username [access mask]\n", argv[0]);
1227 return NT_STATUS_OK;
1230 acct_name = argv[1];
1232 if (argc > 2)
1233 sscanf(argv[2], "%x", &access_mask);
1235 /* Get sam policy handle */
1237 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1238 &connect_pol);
1240 if (!NT_STATUS_IS_OK(result))
1241 goto done;
1243 /* Get domain policy handle */
1245 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1246 access_mask,
1247 &domain_sid, &domain_pol);
1249 if (!NT_STATUS_IS_OK(result))
1250 goto done;
1252 /* Create domain user */
1254 acb_info = ACB_NORMAL;
1255 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
1257 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1258 acct_name, acb_info, unknown,
1259 &user_pol, &user_rid);
1261 if (!NT_STATUS_IS_OK(result))
1262 goto done;
1264 done:
1265 return result;
1268 /* Create domain group */
1270 static NTSTATUS cmd_samr_create_dom_group(struct cli_state *cli,
1271 TALLOC_CTX *mem_ctx,
1272 int argc, const char **argv)
1274 POLICY_HND connect_pol, domain_pol, group_pol;
1275 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1276 const char *grp_name;
1277 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1279 if ((argc < 2) || (argc > 3)) {
1280 printf("Usage: %s groupname [access mask]\n", argv[0]);
1281 return NT_STATUS_OK;
1284 grp_name = argv[1];
1286 if (argc > 2)
1287 sscanf(argv[2], "%x", &access_mask);
1289 /* Get sam policy handle */
1291 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1292 &connect_pol);
1294 if (!NT_STATUS_IS_OK(result))
1295 goto done;
1297 /* Get domain policy handle */
1299 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1300 access_mask,
1301 &domain_sid, &domain_pol);
1303 if (!NT_STATUS_IS_OK(result))
1304 goto done;
1306 /* Create domain user */
1308 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1309 grp_name, MAXIMUM_ALLOWED_ACCESS,
1310 &group_pol);
1312 if (!NT_STATUS_IS_OK(result))
1313 goto done;
1315 done:
1316 return result;
1319 /* Lookup sam names */
1321 static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli,
1322 TALLOC_CTX *mem_ctx,
1323 int argc, const char **argv)
1325 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1326 POLICY_HND connect_pol, domain_pol;
1327 uint32 flags = 0x000003e8; /* Unknown */
1328 uint32 num_rids, num_names, *name_types, *rids;
1329 const char **names;
1330 int i;
1331 DOM_SID global_sid_Builtin;
1333 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1335 if (argc < 3) {
1336 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
1337 printf("check on the domain SID: S-1-5-21-x-y-z\n");
1338 printf("or check on the builtin SID: S-1-5-32\n");
1339 return NT_STATUS_OK;
1342 /* Get sam policy and domain handles */
1344 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1345 &connect_pol);
1347 if (!NT_STATUS_IS_OK(result))
1348 goto done;
1350 if (StrCaseCmp(argv[1], "domain")==0)
1351 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1352 MAXIMUM_ALLOWED_ACCESS,
1353 &domain_sid, &domain_pol);
1354 else if (StrCaseCmp(argv[1], "builtin")==0)
1355 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1356 MAXIMUM_ALLOWED_ACCESS,
1357 &global_sid_Builtin, &domain_pol);
1358 else
1359 return NT_STATUS_OK;
1361 if (!NT_STATUS_IS_OK(result))
1362 goto done;
1364 /* Look up names */
1366 num_names = argc - 2;
1367 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
1369 for (i = 0; i < argc - 2; i++)
1370 names[i] = argv[i + 2];
1372 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1373 flags, num_names, names,
1374 &num_rids, &rids, &name_types);
1376 if (!NT_STATUS_IS_OK(result))
1377 goto done;
1379 /* Display results */
1381 for (i = 0; i < num_names; i++)
1382 printf("name %s: 0x%x (%d)\n", names[i], rids[i],
1383 name_types[i]);
1385 done:
1386 return result;
1389 /* Lookup sam rids */
1391 static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli,
1392 TALLOC_CTX *mem_ctx,
1393 int argc, const char **argv)
1395 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1396 POLICY_HND connect_pol, domain_pol;
1397 uint32 num_rids, num_names, *rids, *name_types;
1398 char **names;
1399 int i;
1401 if (argc < 2) {
1402 printf("Usage: %s rid1 [rid2 [rid3] [...]]\n", argv[0]);
1403 return NT_STATUS_OK;
1406 /* Get sam policy and domain handles */
1408 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1409 &connect_pol);
1411 if (!NT_STATUS_IS_OK(result))
1412 goto done;
1414 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1415 MAXIMUM_ALLOWED_ACCESS,
1416 &domain_sid, &domain_pol);
1418 if (!NT_STATUS_IS_OK(result))
1419 goto done;
1421 /* Look up rids */
1423 num_rids = argc - 1;
1424 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1426 for (i = 0; i < argc - 1; i++)
1427 sscanf(argv[i + 1], "%i", &rids[i]);
1429 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol, num_rids, rids,
1430 &num_names, &names, &name_types);
1432 if (!NT_STATUS_IS_OK(result) &&
1433 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
1434 goto done;
1436 /* Display results */
1438 for (i = 0; i < num_names; i++)
1439 printf("rid 0x%x: %s (%d)\n", rids[i], names[i], name_types[i]);
1441 done:
1442 return result;
1445 /* Delete domain user */
1447 static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli,
1448 TALLOC_CTX *mem_ctx,
1449 int argc, const char **argv)
1451 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1452 POLICY_HND connect_pol, domain_pol, user_pol;
1453 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1455 if ((argc < 2) || (argc > 3)) {
1456 printf("Usage: %s username\n", argv[0]);
1457 return NT_STATUS_OK;
1460 if (argc > 2)
1461 sscanf(argv[2], "%x", &access_mask);
1463 /* Get sam policy and domain handles */
1465 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1466 &connect_pol);
1468 if (!NT_STATUS_IS_OK(result))
1469 goto done;
1471 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1472 MAXIMUM_ALLOWED_ACCESS,
1473 &domain_sid, &domain_pol);
1475 if (!NT_STATUS_IS_OK(result))
1476 goto done;
1478 /* Get handle on user */
1481 uint32 *user_rids, num_rids, *name_types;
1482 uint32 flags = 0x000003e8; /* Unknown */
1484 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1485 flags, 1, (const char **)&argv[1],
1486 &num_rids, &user_rids,
1487 &name_types);
1489 if (!NT_STATUS_IS_OK(result))
1490 goto done;
1492 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1493 access_mask,
1494 user_rids[0], &user_pol);
1496 if (!NT_STATUS_IS_OK(result))
1497 goto done;
1500 /* Delete user */
1502 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
1504 if (!NT_STATUS_IS_OK(result))
1505 goto done;
1507 /* Display results */
1509 done:
1510 return result;
1513 /**********************************************************************
1514 * Query user security object
1516 static NTSTATUS cmd_samr_query_sec_obj(struct cli_state *cli,
1517 TALLOC_CTX *mem_ctx,
1518 int argc, const char **argv)
1520 POLICY_HND connect_pol, domain_pol, user_pol, *pol;
1521 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1522 uint32 info_level = 4;
1523 fstring server;
1524 uint32 user_rid = 0;
1525 TALLOC_CTX *ctx = NULL;
1526 SEC_DESC_BUF *sec_desc_buf=NULL;
1527 BOOL domain = False;
1529 ctx=talloc_init("cmd_samr_query_sec_obj");
1531 if ((argc < 1) || (argc > 2)) {
1532 printf("Usage: %s [rid|-d]\n", argv[0]);
1533 printf("\tSpecify rid for security on user, -d for security on domain\n");
1534 return NT_STATUS_OK;
1537 if (argc > 1) {
1538 if (strcmp(argv[1], "-d") == 0)
1539 domain = True;
1540 else
1541 sscanf(argv[1], "%i", &user_rid);
1544 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1545 strupper_m(server);
1546 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1547 &connect_pol);
1549 if (!NT_STATUS_IS_OK(result))
1550 goto done;
1552 if (domain || user_rid)
1553 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1554 MAXIMUM_ALLOWED_ACCESS,
1555 &domain_sid, &domain_pol);
1557 if (!NT_STATUS_IS_OK(result))
1558 goto done;
1560 if (user_rid)
1561 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1562 MAXIMUM_ALLOWED_ACCESS,
1563 user_rid, &user_pol);
1565 if (!NT_STATUS_IS_OK(result))
1566 goto done;
1568 /* Pick which query pol to use */
1570 pol = &connect_pol;
1572 if (domain)
1573 pol = &domain_pol;
1575 if (user_rid)
1576 pol = &user_pol;
1578 /* Query SAM security object */
1580 result = cli_samr_query_sec_obj(cli, mem_ctx, pol, info_level, ctx,
1581 &sec_desc_buf);
1583 if (!NT_STATUS_IS_OK(result))
1584 goto done;
1586 display_sec_desc(sec_desc_buf->sec);
1588 done:
1589 talloc_destroy(ctx);
1590 return result;
1593 static NTSTATUS cmd_samr_get_dom_pwinfo(struct cli_state *cli,
1594 TALLOC_CTX *mem_ctx,
1595 int argc, const char **argv)
1597 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1598 uint16 unk_0, unk_1;
1600 if (argc != 1) {
1601 printf("Usage: %s\n", argv[0]);
1602 return NT_STATUS_OK;
1605 result = cli_samr_get_dom_pwinfo(cli, mem_ctx, &unk_0, &unk_1) ;
1607 if (NT_STATUS_IS_OK(result)) {
1608 printf("unk_0 = 0x%08x\n", unk_0);
1609 printf("unk_1 = 0x%08x\n", unk_1);
1612 return result;
1615 /* Look up domain name */
1617 static NTSTATUS cmd_samr_lookup_domain(struct cli_state *cli,
1618 TALLOC_CTX *mem_ctx,
1619 int argc, const char **argv)
1621 POLICY_HND connect_pol, domain_pol;
1622 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1623 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1624 fstring domain_name,sid_string;
1625 DOM_SID sid;
1627 if (argc != 2) {
1628 printf("Usage: %s domain_name\n", argv[0]);
1629 return NT_STATUS_OK;
1632 sscanf(argv[1], "%s", domain_name);
1634 result = try_samr_connects(cli, mem_ctx, access_mask, &connect_pol);
1636 if (!NT_STATUS_IS_OK(result))
1637 goto done;
1639 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1640 access_mask, &domain_sid, &domain_pol);
1642 if (!NT_STATUS_IS_OK(result))
1643 goto done;
1645 result = cli_samr_lookup_domain(
1646 cli, mem_ctx, &connect_pol, domain_name, &sid);
1648 sid_to_string(sid_string,&sid);
1650 if (NT_STATUS_IS_OK(result))
1651 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
1652 domain_name,sid_string);
1654 done:
1655 return result;
1659 /* List of commands exported by this module */
1661 struct cmd_set samr_commands[] = {
1663 { "SAMR" },
1665 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, PI_SAMR, "Query user info", "" },
1666 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, PI_SAMR, "Query group info", "" },
1667 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, PI_SAMR, "Query user groups", "" },
1668 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, PI_SAMR, "Query user aliases", "" },
1669 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, PI_SAMR, "Query group membership", "" },
1670 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, PI_SAMR, "Query alias membership", "" },
1671 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, PI_SAMR, "Query display info", "" },
1672 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, PI_SAMR, "Query domain info", "" },
1673 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, PI_SAMR, "Enumerate domain users", "" },
1674 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, PI_SAMR, "Enumerate domain groups", "" },
1675 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, PI_SAMR, "Enumerate alias groups", "" },
1677 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, PI_SAMR, "Create domain user", "" },
1678 { "createdomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group, NULL, PI_SAMR, "Create domain group", "" },
1679 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, PI_SAMR, "Look up names", "" },
1680 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, PI_SAMR, "Look up names", "" },
1681 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, PI_SAMR, "Delete domain user", "" },
1682 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, PI_SAMR, "Query SAMR security object", "" },
1683 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, PI_SAMR, "Retrieve domain password info", "" },
1685 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, PI_SAMR, "Lookup Domain Name", "" },
1686 { NULL }