r4904: sync up with 3.0 for 3.0.11pre2
[Samba.git] / source / rpcclient / cmd_samr.c
bloba69a0cb73a7fb18f35f3d21136f04eda191163a8
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 void display_sam_unk_info_1(SAM_UNK_INFO_1 *info1)
147 printf("Minimum password length: %d\n", info1->min_length_password);
148 printf("Password uniqueness (remember x passwords): %d\n", info1->password_history);
149 printf("flag: ");
150 if(info1->flag&&2==2) printf("users must open a session to change password ");
151 printf("\n");
153 printf("password expire in: %s\n", display_time(info1->expire));
154 printf("Min password age (allow changing in x days): %s\n", display_time(info1->min_passwordage));
157 static void display_sam_unk_info_2(SAM_UNK_INFO_2 *info2)
159 fstring name;
161 unistr2_to_ascii(name, &info2->uni_domain, sizeof(name) - 1);
162 printf("Domain:\t%s\n", name);
164 unistr2_to_ascii(name, &info2->uni_server, sizeof(name) - 1);
165 printf("Server:\t%s\n", name);
167 unistr2_to_ascii(name, &info2->uni_comment, sizeof(name) - 1);
168 printf("Comment:\t%s\n", name);
170 printf("Total Users:\t%d\n", info2->num_domain_usrs);
171 printf("Total Groups:\t%d\n", info2->num_domain_grps);
172 printf("Total Aliases:\t%d\n", info2->num_local_grps);
174 printf("Sequence No:\t%d\n", info2->seq_num.low);
176 printf("Force Logoff:\t%d\n", (int)nt_time_to_unix_abs(&info2->logout));
178 printf("Unknown 4:\t0x%x\n", info2->unknown_4);
179 printf("Unknown 5:\t0x%x\n", info2->unknown_5);
180 printf("Unknown 6:\t0x%x\n", info2->unknown_6);
183 static void display_sam_unk_info_8(SAM_UNK_INFO_8 *info8)
185 printf("Sequence No:\t%d\n", info8->seq_num.low);
186 printf("Domain Create Time:\t%s\n",
187 http_timestring(nt_time_to_unix(&info8->domain_create_time)));
191 static void display_sam_unk_info_12(SAM_UNK_INFO_12 *info12)
193 printf("Bad password lockout duration: %s\n", display_time(info12->duration));
194 printf("Reset Lockout after: %s\n", display_time(info12->reset_count));
195 printf("Lockout after bad attempts: %d\n", info12->bad_attempt_lockout);
198 static void display_sam_info_1(SAM_ENTRY1 *e1, SAM_STR1 *s1)
200 fstring tmp;
202 printf("index: 0x%x ", e1->user_idx);
203 printf("RID: 0x%x ", e1->rid_user);
204 printf("acb: 0x%x ", e1->acb_info);
206 unistr2_to_ascii(tmp, &s1->uni_acct_name, sizeof(tmp)-1);
207 printf("Account: %s\t", tmp);
209 unistr2_to_ascii(tmp, &s1->uni_full_name, sizeof(tmp)-1);
210 printf("Name: %s\t", tmp);
212 unistr2_to_ascii(tmp, &s1->uni_acct_desc, sizeof(tmp)-1);
213 printf("Desc: %s\n", tmp);
216 static void display_sam_info_2(SAM_ENTRY2 *e2, SAM_STR2 *s2)
218 fstring tmp;
220 printf("index: 0x%x ", e2->user_idx);
221 printf("RID: 0x%x ", e2->rid_user);
222 printf("acb: 0x%x ", e2->acb_info);
224 unistr2_to_ascii(tmp, &s2->uni_srv_name, sizeof(tmp)-1);
225 printf("Account: %s\t", tmp);
227 unistr2_to_ascii(tmp, &s2->uni_srv_desc, sizeof(tmp)-1);
228 printf("Name: %s\n", tmp);
232 static void display_sam_info_3(SAM_ENTRY3 *e3, SAM_STR3 *s3)
234 fstring tmp;
236 printf("index: 0x%x ", e3->grp_idx);
237 printf("RID: 0x%x ", e3->rid_grp);
238 printf("attr: 0x%x ", e3->attr);
240 unistr2_to_ascii(tmp, &s3->uni_grp_name, sizeof(tmp)-1);
241 printf("Account: %s\t", tmp);
243 unistr2_to_ascii(tmp, &s3->uni_grp_desc, sizeof(tmp)-1);
244 printf("Name: %s\n", tmp);
248 static void display_sam_info_4(SAM_ENTRY4 *e4, SAM_STR4 *s4)
250 int i;
252 printf("index: %d ", e4->user_idx);
254 printf("Account: ");
255 for (i=0; i<s4->acct_name.str_str_len; i++)
256 printf("%c", s4->acct_name.buffer[i]);
257 printf("\n");
261 static void display_sam_info_5(SAM_ENTRY5 *e5, SAM_STR5 *s5)
263 int i;
265 printf("index: 0x%x ", e5->grp_idx);
267 printf("Account: ");
268 for (i=0; i<s5->grp_name.str_str_len; i++)
269 printf("%c", s5->grp_name.buffer[i]);
270 printf("\n");
274 /****************************************************************************
275 Try samr_connect4 first, then samr_conenct if it fails
276 ****************************************************************************/
277 static NTSTATUS try_samr_connects(struct cli_state *cli, TALLOC_CTX *mem_ctx,
278 uint32 access_mask, POLICY_HND *connect_pol)
280 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
282 result = cli_samr_connect4(cli, mem_ctx, access_mask, connect_pol);
283 if (!NT_STATUS_IS_OK(result)) {
284 result = cli_samr_connect(cli, mem_ctx, access_mask,
285 connect_pol);
287 return result;
290 /**********************************************************************
291 * Query user information
293 static NTSTATUS cmd_samr_query_user(struct cli_state *cli,
294 TALLOC_CTX *mem_ctx,
295 int argc, const char **argv)
297 POLICY_HND connect_pol, domain_pol, user_pol;
298 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
299 uint32 info_level = 21;
300 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
301 SAM_USERINFO_CTR *user_ctr;
302 fstring server;
303 uint32 user_rid;
305 if ((argc < 2) || (argc > 4)) {
306 printf("Usage: %s rid [info level] [access mask] \n", argv[0]);
307 return NT_STATUS_OK;
310 sscanf(argv[1], "%i", &user_rid);
312 if (argc > 2)
313 sscanf(argv[2], "%i", &info_level);
315 if (argc > 3)
316 sscanf(argv[3], "%x", &access_mask);
319 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
320 strupper_m(server);
322 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
323 &connect_pol);
325 if (!NT_STATUS_IS_OK(result))
326 goto done;
328 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
329 MAXIMUM_ALLOWED_ACCESS,
330 &domain_sid, &domain_pol);
332 if (!NT_STATUS_IS_OK(result))
333 goto done;
335 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
336 access_mask,
337 user_rid, &user_pol);
339 if (!NT_STATUS_IS_OK(result))
340 goto done;
342 ZERO_STRUCT(user_ctr);
344 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
345 info_level, &user_ctr);
347 if (!NT_STATUS_IS_OK(result))
348 goto done;
350 switch (user_ctr->switch_value) {
351 case 21:
352 display_sam_user_info_21(user_ctr->info.id21);
353 break;
354 case 7:
355 display_sam_user_info_7(user_ctr->info.id7);
356 break;
357 default:
358 printf("Unsupported infolevel: %d\n", info_level);
359 break;
362 done:
363 return result;
366 /****************************************************************************
367 display group info
368 ****************************************************************************/
369 static void display_group_info1(GROUP_INFO1 *info1)
371 fstring temp;
373 unistr2_to_ascii(temp, &info1->uni_acct_name, sizeof(temp)-1);
374 printf("\tGroup Name:\t%s\n", temp);
375 unistr2_to_ascii(temp, &info1->uni_acct_desc, sizeof(temp)-1);
376 printf("\tDescription:\t%s\n", temp);
377 printf("\tunk1:%d\n", info1->unknown_1);
378 printf("\tNum Members:%d\n", info1->num_members);
381 /****************************************************************************
382 display group info
383 ****************************************************************************/
384 static void display_group_info4(GROUP_INFO4 *info4)
386 fstring desc;
388 unistr2_to_ascii(desc, &info4->uni_acct_desc, sizeof(desc)-1);
389 printf("\tGroup Description:%s\n", desc);
392 /****************************************************************************
393 display sam sync structure
394 ****************************************************************************/
395 static void display_group_info_ctr(GROUP_INFO_CTR *ctr)
397 switch (ctr->switch_value1) {
398 case 1: {
399 display_group_info1(&ctr->group.info1);
400 break;
402 case 4: {
403 display_group_info4(&ctr->group.info4);
404 break;
409 /***********************************************************************
410 * Query group information
412 static NTSTATUS cmd_samr_query_group(struct cli_state *cli,
413 TALLOC_CTX *mem_ctx,
414 int argc, const char **argv)
416 POLICY_HND connect_pol, domain_pol, group_pol;
417 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
418 uint32 info_level = 1;
419 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
420 GROUP_INFO_CTR *group_ctr;
421 fstring server;
422 uint32 group_rid;
424 if ((argc < 2) || (argc > 4)) {
425 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
426 return NT_STATUS_OK;
429 sscanf(argv[1], "%i", &group_rid);
431 if (argc > 2)
432 sscanf(argv[2], "%i", &info_level);
434 if (argc > 3)
435 sscanf(argv[3], "%x", &access_mask);
437 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
438 strupper_m(server);
440 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
441 &connect_pol);
443 if (!NT_STATUS_IS_OK(result))
444 goto done;
446 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
447 MAXIMUM_ALLOWED_ACCESS,
448 &domain_sid, &domain_pol);
450 if (!NT_STATUS_IS_OK(result))
451 goto done;
453 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
454 access_mask,
455 group_rid, &group_pol);
457 if (!NT_STATUS_IS_OK(result))
458 goto done;
460 result = cli_samr_query_groupinfo(cli, mem_ctx, &group_pol,
461 info_level, &group_ctr);
462 if (!NT_STATUS_IS_OK(result)) {
463 goto done;
466 display_group_info_ctr(group_ctr);
468 done:
469 return result;
472 /* Query groups a user is a member of */
474 static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli,
475 TALLOC_CTX *mem_ctx,
476 int argc, const char **argv)
478 POLICY_HND connect_pol,
479 domain_pol,
480 user_pol;
481 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
482 uint32 num_groups,
483 user_rid;
484 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
485 DOM_GID *user_gids;
486 int i;
487 fstring server;
489 if ((argc < 2) || (argc > 3)) {
490 printf("Usage: %s rid [access mask]\n", argv[0]);
491 return NT_STATUS_OK;
494 sscanf(argv[1], "%i", &user_rid);
496 if (argc > 2)
497 sscanf(argv[2], "%x", &access_mask);
499 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
500 strupper_m(server);
502 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
503 &connect_pol);
505 if (!NT_STATUS_IS_OK(result))
506 goto done;
508 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
509 MAXIMUM_ALLOWED_ACCESS,
510 &domain_sid, &domain_pol);
512 if (!NT_STATUS_IS_OK(result))
513 goto done;
515 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
516 access_mask,
517 user_rid, &user_pol);
519 if (!NT_STATUS_IS_OK(result))
520 goto done;
522 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
523 &num_groups, &user_gids);
525 if (!NT_STATUS_IS_OK(result))
526 goto done;
528 for (i = 0; i < num_groups; i++) {
529 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
530 user_gids[i].g_rid, user_gids[i].attr);
533 done:
534 return result;
537 /* Query aliases a user is a member of */
539 static NTSTATUS cmd_samr_query_useraliases(struct cli_state *cli,
540 TALLOC_CTX *mem_ctx,
541 int argc, const char **argv)
543 POLICY_HND connect_pol, domain_pol;
544 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
545 uint32 user_rid, num_aliases, *alias_rids;
546 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
547 int i;
548 fstring server;
549 DOM_SID tmp_sid;
550 DOM_SID2 sid;
551 DOM_SID global_sid_Builtin;
553 string_to_sid(&global_sid_Builtin, "S-1-5-32");
555 if ((argc < 3) || (argc > 4)) {
556 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
557 return NT_STATUS_OK;
560 sscanf(argv[2], "%i", &user_rid);
562 if (argc > 3)
563 sscanf(argv[3], "%x", &access_mask);
565 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
566 strupper_m(server);
568 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
569 &connect_pol);
571 if (!NT_STATUS_IS_OK(result))
572 goto done;
574 if (StrCaseCmp(argv[1], "domain")==0)
575 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
576 access_mask,
577 &domain_sid, &domain_pol);
578 else if (StrCaseCmp(argv[1], "builtin")==0)
579 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
580 access_mask,
581 &global_sid_Builtin, &domain_pol);
582 else
583 return NT_STATUS_OK;
585 if (!NT_STATUS_IS_OK(result))
586 goto done;
588 sid_copy(&tmp_sid, &domain_sid);
589 sid_append_rid(&tmp_sid, user_rid);
590 init_dom_sid2(&sid, &tmp_sid);
592 result = cli_samr_query_useraliases(cli, mem_ctx, &domain_pol, 1, &sid, &num_aliases, &alias_rids);
594 if (!NT_STATUS_IS_OK(result))
595 goto done;
597 for (i = 0; i < num_aliases; i++) {
598 printf("\tgroup rid:[0x%x]\n", alias_rids[i]);
601 done:
602 return result;
605 /* Query members of a group */
607 static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli,
608 TALLOC_CTX *mem_ctx,
609 int argc, const char **argv)
611 POLICY_HND connect_pol, domain_pol, group_pol;
612 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
613 uint32 num_members, *group_rids, *group_attrs, group_rid;
614 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
615 int i;
616 fstring server;
618 if ((argc < 2) || (argc > 3)) {
619 printf("Usage: %s rid [access mask]\n", argv[0]);
620 return NT_STATUS_OK;
623 sscanf(argv[1], "%i", &group_rid);
625 if (argc > 2)
626 sscanf(argv[2], "%x", &access_mask);
628 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
629 strupper_m(server);
631 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
632 &connect_pol);
634 if (!NT_STATUS_IS_OK(result))
635 goto done;
637 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
638 MAXIMUM_ALLOWED_ACCESS,
639 &domain_sid, &domain_pol);
641 if (!NT_STATUS_IS_OK(result))
642 goto done;
644 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
645 access_mask,
646 group_rid, &group_pol);
648 if (!NT_STATUS_IS_OK(result))
649 goto done;
651 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
652 &num_members, &group_rids,
653 &group_attrs);
655 if (!NT_STATUS_IS_OK(result))
656 goto done;
658 for (i = 0; i < num_members; i++) {
659 printf("\trid:[0x%x] attr:[0x%x]\n", group_rids[i],
660 group_attrs[i]);
663 done:
664 return result;
667 /* Enumerate domain users */
669 static NTSTATUS cmd_samr_enum_dom_users(struct cli_state *cli,
670 TALLOC_CTX *mem_ctx,
671 int argc, const char **argv)
673 POLICY_HND connect_pol, domain_pol;
674 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
675 uint32 start_idx, size, num_dom_users, i;
676 char **dom_users;
677 uint32 *dom_rids;
678 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
679 uint16 acb_mask = ACB_NORMAL;
680 BOOL got_connect_pol = False, got_domain_pol = False;
682 if ((argc < 1) || (argc > 3)) {
683 printf("Usage: %s [access_mask] [acb_mask]\n", argv[0]);
684 return NT_STATUS_OK;
687 if (argc > 1)
688 sscanf(argv[1], "%x", &access_mask);
690 if (argc > 2)
691 sscanf(argv[2], "%x", &acb_mask);
693 /* Get sam policy handle */
695 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
696 &connect_pol);
698 if (!NT_STATUS_IS_OK(result))
699 goto done;
701 got_connect_pol = True;
703 /* Get domain policy handle */
705 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
706 access_mask,
707 &domain_sid, &domain_pol);
709 if (!NT_STATUS_IS_OK(result))
710 goto done;
712 got_domain_pol = True;
714 /* Enumerate domain users */
716 start_idx = 0;
717 size = 0xffff;
719 do {
720 result = cli_samr_enum_dom_users(
721 cli, mem_ctx, &domain_pol, &start_idx, acb_mask,
722 size, &dom_users, &dom_rids, &num_dom_users);
724 if (NT_STATUS_IS_OK(result) ||
725 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
727 for (i = 0; i < num_dom_users; i++)
728 printf("user:[%s] rid:[0x%x]\n",
729 dom_users[i], dom_rids[i]);
732 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
734 done:
735 if (got_domain_pol)
736 cli_samr_close(cli, mem_ctx, &domain_pol);
738 if (got_connect_pol)
739 cli_samr_close(cli, mem_ctx, &connect_pol);
741 return result;
744 /* Enumerate domain groups */
746 static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli,
747 TALLOC_CTX *mem_ctx,
748 int argc, const char **argv)
750 POLICY_HND connect_pol, domain_pol;
751 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
752 uint32 start_idx, size, num_dom_groups, i;
753 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
754 struct acct_info *dom_groups;
755 BOOL got_connect_pol = False, got_domain_pol = False;
757 if ((argc < 1) || (argc > 2)) {
758 printf("Usage: %s [access_mask]\n", argv[0]);
759 return NT_STATUS_OK;
762 if (argc > 1)
763 sscanf(argv[1], "%x", &access_mask);
765 /* Get sam policy handle */
767 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
768 &connect_pol);
770 if (!NT_STATUS_IS_OK(result))
771 goto done;
773 got_connect_pol = True;
775 /* Get domain policy handle */
777 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
778 access_mask,
779 &domain_sid, &domain_pol);
781 if (!NT_STATUS_IS_OK(result))
782 goto done;
784 got_domain_pol = True;
786 /* Enumerate domain groups */
788 start_idx = 0;
789 size = 0xffff;
791 do {
792 result = cli_samr_enum_dom_groups(
793 cli, mem_ctx, &domain_pol, &start_idx, size,
794 &dom_groups, &num_dom_groups);
796 if (NT_STATUS_IS_OK(result) ||
797 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
799 for (i = 0; i < num_dom_groups; i++)
800 printf("group:[%s] rid:[0x%x]\n",
801 dom_groups[i].acct_name,
802 dom_groups[i].rid);
805 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
807 done:
808 if (got_domain_pol)
809 cli_samr_close(cli, mem_ctx, &domain_pol);
811 if (got_connect_pol)
812 cli_samr_close(cli, mem_ctx, &connect_pol);
814 return result;
817 /* Enumerate alias groups */
819 static NTSTATUS cmd_samr_enum_als_groups(struct cli_state *cli,
820 TALLOC_CTX *mem_ctx,
821 int argc, const char **argv)
823 POLICY_HND connect_pol, domain_pol;
824 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
825 uint32 start_idx, size, num_als_groups, i;
826 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
827 struct acct_info *als_groups;
828 DOM_SID global_sid_Builtin;
829 BOOL got_connect_pol = False, got_domain_pol = False;
831 string_to_sid(&global_sid_Builtin, "S-1-5-32");
833 if ((argc < 2) || (argc > 3)) {
834 printf("Usage: %s builtin|domain [access mask]\n", argv[0]);
835 return NT_STATUS_OK;
838 if (argc > 2)
839 sscanf(argv[2], "%x", &access_mask);
841 /* Get sam policy handle */
843 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
844 &connect_pol);
846 if (!NT_STATUS_IS_OK(result))
847 goto done;
849 got_connect_pol = True;
851 /* Get domain policy handle */
853 if (StrCaseCmp(argv[1], "domain")==0)
854 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
855 access_mask,
856 &domain_sid, &domain_pol);
857 else if (StrCaseCmp(argv[1], "builtin")==0)
858 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
859 access_mask,
860 &global_sid_Builtin, &domain_pol);
861 else
862 return NT_STATUS_OK;
864 if (!NT_STATUS_IS_OK(result))
865 goto done;
867 got_domain_pol = True;
869 /* Enumerate alias groups */
871 start_idx = 0;
872 size = 0xffff; /* Number of groups to retrieve */
874 do {
875 result = cli_samr_enum_als_groups(
876 cli, mem_ctx, &domain_pol, &start_idx, size,
877 &als_groups, &num_als_groups);
879 if (NT_STATUS_IS_OK(result) ||
880 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
882 for (i = 0; i < num_als_groups; i++)
883 printf("group:[%s] rid:[0x%x]\n",
884 als_groups[i].acct_name,
885 als_groups[i].rid);
887 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
889 done:
890 if (got_domain_pol)
891 cli_samr_close(cli, mem_ctx, &domain_pol);
893 if (got_connect_pol)
894 cli_samr_close(cli, mem_ctx, &connect_pol);
896 return result;
899 /* Query alias membership */
901 static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli,
902 TALLOC_CTX *mem_ctx,
903 int argc, const char **argv)
905 POLICY_HND connect_pol, domain_pol, alias_pol;
906 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
907 uint32 alias_rid, num_members, i;
908 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
909 DOM_SID *alias_sids;
910 DOM_SID global_sid_Builtin;
912 string_to_sid(&global_sid_Builtin, "S-1-5-32");
914 if ((argc < 3) || (argc > 4)) {
915 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
916 return NT_STATUS_OK;
919 sscanf(argv[2], "%i", &alias_rid);
921 if (argc > 3)
922 sscanf(argv[3], "%x", &access_mask);
924 /* Open SAMR handle */
926 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
927 &connect_pol);
929 if (!NT_STATUS_IS_OK(result))
930 goto done;
932 /* Open handle on domain */
934 if (StrCaseCmp(argv[1], "domain")==0)
935 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
936 MAXIMUM_ALLOWED_ACCESS,
937 &domain_sid, &domain_pol);
938 else if (StrCaseCmp(argv[1], "builtin")==0)
939 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
940 MAXIMUM_ALLOWED_ACCESS,
941 &global_sid_Builtin, &domain_pol);
942 else
943 return NT_STATUS_OK;
945 if (!NT_STATUS_IS_OK(result))
946 goto done;
948 /* Open handle on alias */
950 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
951 access_mask,
952 alias_rid, &alias_pol);
953 if (!NT_STATUS_IS_OK(result))
954 goto done;
956 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
957 &num_members, &alias_sids);
959 if (!NT_STATUS_IS_OK(result))
960 goto done;
962 for (i = 0; i < num_members; i++) {
963 fstring sid_str;
965 sid_to_string(sid_str, &alias_sids[i]);
966 printf("\tsid:[%s]\n", sid_str);
969 done:
970 return result;
973 /* Query display info */
975 static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli,
976 TALLOC_CTX *mem_ctx,
977 int argc, const char **argv)
979 POLICY_HND connect_pol, domain_pol;
980 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
981 uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries, i;
982 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
983 uint32 info_level = 1;
984 SAM_DISPINFO_CTR ctr;
985 SAM_DISPINFO_1 info1;
986 SAM_DISPINFO_2 info2;
987 SAM_DISPINFO_3 info3;
988 SAM_DISPINFO_4 info4;
989 SAM_DISPINFO_5 info5;
990 int loop_count = 0;
991 BOOL got_params = False; /* Use get_query_dispinfo_params() or not? */
993 if (argc > 5) {
994 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
995 return NT_STATUS_OK;
998 if (argc >= 2)
999 sscanf(argv[1], "%i", &info_level);
1001 if (argc >= 3)
1002 sscanf(argv[2], "%i", &start_idx);
1004 if (argc >= 4) {
1005 sscanf(argv[3], "%i", &max_entries);
1006 got_params = True;
1009 if (argc >= 5) {
1010 sscanf(argv[4], "%i", &max_size);
1011 got_params = True;
1014 if (argc >= 6)
1015 sscanf(argv[5], "%x", &access_mask);
1017 /* Get sam policy handle */
1019 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1020 &connect_pol);
1022 if (!NT_STATUS_IS_OK(result))
1023 goto done;
1025 /* Get domain policy handle */
1027 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1028 access_mask,
1029 &domain_sid, &domain_pol);
1031 if (!NT_STATUS_IS_OK(result))
1032 goto done;
1034 /* Query display info */
1036 ZERO_STRUCT(ctr);
1037 ZERO_STRUCT(info1);
1039 switch (info_level) {
1040 case 1:
1041 ZERO_STRUCT(info1);
1042 ctr.sam.info1 = &info1;
1043 break;
1044 case 2:
1045 ZERO_STRUCT(info2);
1046 ctr.sam.info2 = &info2;
1047 break;
1048 case 3:
1049 ZERO_STRUCT(info3);
1050 ctr.sam.info3 = &info3;
1051 break;
1052 case 4:
1053 ZERO_STRUCT(info4);
1054 ctr.sam.info4 = &info4;
1055 break;
1056 case 5:
1057 ZERO_STRUCT(info5);
1058 ctr.sam.info5 = &info5;
1059 break;
1063 while(1) {
1065 if (!got_params)
1066 get_query_dispinfo_params(
1067 loop_count, &max_entries, &max_size);
1069 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1070 &start_idx, info_level,
1071 &num_entries, max_entries,
1072 max_size, &ctr);
1074 loop_count++;
1076 if (!NT_STATUS_IS_OK(result) && !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1077 break;
1079 if (num_entries == 0)
1080 break;
1082 for (i = 0; i < num_entries; i++) {
1083 switch (info_level) {
1084 case 1:
1085 display_sam_info_1(&ctr.sam.info1->sam[i], &ctr.sam.info1->str[i]);
1086 break;
1087 case 2:
1088 display_sam_info_2(&ctr.sam.info2->sam[i], &ctr.sam.info2->str[i]);
1089 break;
1090 case 3:
1091 display_sam_info_3(&ctr.sam.info3->sam[i], &ctr.sam.info3->str[i]);
1092 break;
1093 case 4:
1094 display_sam_info_4(&ctr.sam.info4->sam[i], &ctr.sam.info4->str[i]);
1095 break;
1096 case 5:
1097 display_sam_info_5(&ctr.sam.info5->sam[i], &ctr.sam.info5->str[i]);
1098 break;
1103 done:
1104 return result;
1107 /* Query domain info */
1109 static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli,
1110 TALLOC_CTX *mem_ctx,
1111 int argc, const char **argv)
1113 POLICY_HND connect_pol, domain_pol;
1114 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1115 uint32 switch_level = 2;
1116 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1117 SAM_UNK_CTR ctr;
1119 if (argc > 2) {
1120 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1121 return NT_STATUS_OK;
1124 if (argc > 1)
1125 sscanf(argv[1], "%i", &switch_level);
1127 if (argc > 2)
1128 sscanf(argv[2], "%x", &access_mask);
1130 /* Get sam policy handle */
1132 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1133 &connect_pol);
1135 if (!NT_STATUS_IS_OK(result))
1136 goto done;
1138 /* Get domain policy handle */
1140 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1141 access_mask,
1142 &domain_sid, &domain_pol);
1144 if (!NT_STATUS_IS_OK(result))
1145 goto done;
1147 /* Query domain info */
1149 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
1150 switch_level, &ctr);
1152 if (!NT_STATUS_IS_OK(result))
1153 goto done;
1155 /* Display domain info */
1157 switch (switch_level) {
1158 case 1:
1159 display_sam_unk_info_1(&ctr.info.inf1);
1160 break;
1161 case 2:
1162 display_sam_unk_info_2(&ctr.info.inf2);
1163 break;
1164 case 8:
1165 display_sam_unk_info_8(&ctr.info.inf8);
1166 break;
1167 case 12:
1168 display_sam_unk_info_12(&ctr.info.inf12);
1169 break;
1170 default:
1171 printf("cannot display domain info for switch value %d\n",
1172 switch_level);
1173 break;
1176 done:
1178 cli_samr_close(cli, mem_ctx, &domain_pol);
1179 cli_samr_close(cli, mem_ctx, &connect_pol);
1180 return result;
1183 /* Create domain user */
1185 static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli,
1186 TALLOC_CTX *mem_ctx,
1187 int argc, const char **argv)
1189 POLICY_HND connect_pol, domain_pol, user_pol;
1190 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1191 const char *acct_name;
1192 uint16 acb_info;
1193 uint32 unknown, user_rid;
1194 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1196 if ((argc < 2) || (argc > 3)) {
1197 printf("Usage: %s username [access mask]\n", argv[0]);
1198 return NT_STATUS_OK;
1201 acct_name = argv[1];
1203 if (argc > 2)
1204 sscanf(argv[2], "%x", &access_mask);
1206 /* Get sam policy handle */
1208 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1209 &connect_pol);
1211 if (!NT_STATUS_IS_OK(result))
1212 goto done;
1214 /* Get domain policy handle */
1216 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1217 access_mask,
1218 &domain_sid, &domain_pol);
1220 if (!NT_STATUS_IS_OK(result))
1221 goto done;
1223 /* Create domain user */
1225 acb_info = ACB_NORMAL;
1226 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
1228 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1229 acct_name, acb_info, unknown,
1230 &user_pol, &user_rid);
1232 if (!NT_STATUS_IS_OK(result))
1233 goto done;
1235 done:
1236 return result;
1239 /* Create domain group */
1241 static NTSTATUS cmd_samr_create_dom_group(struct cli_state *cli,
1242 TALLOC_CTX *mem_ctx,
1243 int argc, const char **argv)
1245 POLICY_HND connect_pol, domain_pol, group_pol;
1246 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1247 const char *grp_name;
1248 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1250 if ((argc < 2) || (argc > 3)) {
1251 printf("Usage: %s groupname [access mask]\n", argv[0]);
1252 return NT_STATUS_OK;
1255 grp_name = argv[1];
1257 if (argc > 2)
1258 sscanf(argv[2], "%x", &access_mask);
1260 /* Get sam policy handle */
1262 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1263 &connect_pol);
1265 if (!NT_STATUS_IS_OK(result))
1266 goto done;
1268 /* Get domain policy handle */
1270 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1271 access_mask,
1272 &domain_sid, &domain_pol);
1274 if (!NT_STATUS_IS_OK(result))
1275 goto done;
1277 /* Create domain user */
1279 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1280 grp_name, MAXIMUM_ALLOWED_ACCESS,
1281 &group_pol);
1283 if (!NT_STATUS_IS_OK(result))
1284 goto done;
1286 done:
1287 return result;
1290 /* Lookup sam names */
1292 static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli,
1293 TALLOC_CTX *mem_ctx,
1294 int argc, const char **argv)
1296 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1297 POLICY_HND connect_pol, domain_pol;
1298 uint32 flags = 0x000003e8; /* Unknown */
1299 uint32 num_rids, num_names, *name_types, *rids;
1300 const char **names;
1301 int i;
1302 DOM_SID global_sid_Builtin;
1304 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1306 if (argc < 3) {
1307 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
1308 printf("check on the domain SID: S-1-5-21-x-y-z\n");
1309 printf("or check on the builtin SID: S-1-5-32\n");
1310 return NT_STATUS_OK;
1313 /* Get sam policy and domain handles */
1315 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1316 &connect_pol);
1318 if (!NT_STATUS_IS_OK(result))
1319 goto done;
1321 if (StrCaseCmp(argv[1], "domain")==0)
1322 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1323 MAXIMUM_ALLOWED_ACCESS,
1324 &domain_sid, &domain_pol);
1325 else if (StrCaseCmp(argv[1], "builtin")==0)
1326 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1327 MAXIMUM_ALLOWED_ACCESS,
1328 &global_sid_Builtin, &domain_pol);
1329 else
1330 return NT_STATUS_OK;
1332 if (!NT_STATUS_IS_OK(result))
1333 goto done;
1335 /* Look up names */
1337 num_names = argc - 2;
1338 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
1340 for (i = 0; i < argc - 2; i++)
1341 names[i] = argv[i + 2];
1343 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1344 flags, num_names, names,
1345 &num_rids, &rids, &name_types);
1347 if (!NT_STATUS_IS_OK(result))
1348 goto done;
1350 /* Display results */
1352 for (i = 0; i < num_names; i++)
1353 printf("name %s: 0x%x (%d)\n", names[i], rids[i],
1354 name_types[i]);
1356 done:
1357 return result;
1360 /* Lookup sam rids */
1362 static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli,
1363 TALLOC_CTX *mem_ctx,
1364 int argc, const char **argv)
1366 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1367 POLICY_HND connect_pol, domain_pol;
1368 uint32 flags = 0x000003e8; /* Unknown */
1369 uint32 num_rids, num_names, *rids, *name_types;
1370 char **names;
1371 int i;
1373 if (argc < 2) {
1374 printf("Usage: %s rid1 [rid2 [rid3] [...]]\n", argv[0]);
1375 return NT_STATUS_OK;
1378 /* Get sam policy and domain handles */
1380 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1381 &connect_pol);
1383 if (!NT_STATUS_IS_OK(result))
1384 goto done;
1386 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1387 MAXIMUM_ALLOWED_ACCESS,
1388 &domain_sid, &domain_pol);
1390 if (!NT_STATUS_IS_OK(result))
1391 goto done;
1393 /* Look up rids */
1395 num_rids = argc - 1;
1396 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1398 for (i = 0; i < argc - 1; i++)
1399 sscanf(argv[i + 1], "%i", &rids[i]);
1401 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
1402 flags, num_rids, rids,
1403 &num_names, &names, &name_types);
1405 if (!NT_STATUS_IS_OK(result))
1406 goto done;
1408 /* Display results */
1410 for (i = 0; i < num_names; i++)
1411 printf("rid 0x%x: %s (%d)\n", rids[i], names[i], name_types[i]);
1413 done:
1414 return result;
1417 /* Delete domain user */
1419 static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli,
1420 TALLOC_CTX *mem_ctx,
1421 int argc, const char **argv)
1423 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1424 POLICY_HND connect_pol, domain_pol, user_pol;
1425 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1427 if ((argc < 2) || (argc > 3)) {
1428 printf("Usage: %s username\n", argv[0]);
1429 return NT_STATUS_OK;
1432 if (argc > 2)
1433 sscanf(argv[2], "%x", &access_mask);
1435 /* Get sam policy and domain handles */
1437 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1438 &connect_pol);
1440 if (!NT_STATUS_IS_OK(result))
1441 goto done;
1443 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1444 MAXIMUM_ALLOWED_ACCESS,
1445 &domain_sid, &domain_pol);
1447 if (!NT_STATUS_IS_OK(result))
1448 goto done;
1450 /* Get handle on user */
1453 uint32 *user_rids, num_rids, *name_types;
1454 uint32 flags = 0x000003e8; /* Unknown */
1456 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1457 flags, 1, (const char **)&argv[1],
1458 &num_rids, &user_rids,
1459 &name_types);
1461 if (!NT_STATUS_IS_OK(result))
1462 goto done;
1464 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1465 access_mask,
1466 user_rids[0], &user_pol);
1468 if (!NT_STATUS_IS_OK(result))
1469 goto done;
1472 /* Delete user */
1474 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
1476 if (!NT_STATUS_IS_OK(result))
1477 goto done;
1479 /* Display results */
1481 done:
1482 return result;
1485 /**********************************************************************
1486 * Query user security object
1488 static NTSTATUS cmd_samr_query_sec_obj(struct cli_state *cli,
1489 TALLOC_CTX *mem_ctx,
1490 int argc, const char **argv)
1492 POLICY_HND connect_pol, domain_pol, user_pol, *pol;
1493 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1494 uint32 info_level = 4;
1495 fstring server;
1496 uint32 user_rid = 0;
1497 TALLOC_CTX *ctx = NULL;
1498 SEC_DESC_BUF *sec_desc_buf=NULL;
1499 BOOL domain = False;
1501 ctx=talloc_init("cmd_samr_query_sec_obj");
1503 if ((argc < 1) || (argc > 2)) {
1504 printf("Usage: %s [rid|-d]\n", argv[0]);
1505 printf("\tSpecify rid for security on user, -d for security on domain\n");
1506 return NT_STATUS_OK;
1509 if (argc > 1) {
1510 if (strcmp(argv[1], "-d") == 0)
1511 domain = True;
1512 else
1513 sscanf(argv[1], "%i", &user_rid);
1516 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1517 strupper_m(server);
1518 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1519 &connect_pol);
1521 if (!NT_STATUS_IS_OK(result))
1522 goto done;
1524 if (domain || user_rid)
1525 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1526 MAXIMUM_ALLOWED_ACCESS,
1527 &domain_sid, &domain_pol);
1529 if (!NT_STATUS_IS_OK(result))
1530 goto done;
1532 if (user_rid)
1533 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1534 MAXIMUM_ALLOWED_ACCESS,
1535 user_rid, &user_pol);
1537 if (!NT_STATUS_IS_OK(result))
1538 goto done;
1540 /* Pick which query pol to use */
1542 pol = &connect_pol;
1544 if (domain)
1545 pol = &domain_pol;
1547 if (user_rid)
1548 pol = &user_pol;
1550 /* Query SAM security object */
1552 result = cli_samr_query_sec_obj(cli, mem_ctx, pol, info_level, ctx,
1553 &sec_desc_buf);
1555 if (!NT_STATUS_IS_OK(result))
1556 goto done;
1558 display_sec_desc(sec_desc_buf->sec);
1560 done:
1561 talloc_destroy(ctx);
1562 return result;
1565 static NTSTATUS cmd_samr_get_dom_pwinfo(struct cli_state *cli,
1566 TALLOC_CTX *mem_ctx,
1567 int argc, const char **argv)
1569 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1570 uint16 unk_0, unk_1;
1572 if (argc != 1) {
1573 printf("Usage: %s\n", argv[0]);
1574 return NT_STATUS_OK;
1577 result = cli_samr_get_dom_pwinfo(cli, mem_ctx, &unk_0, &unk_1) ;
1579 if (NT_STATUS_IS_OK(result)) {
1580 printf("unk_0 = 0x%08x\n", unk_0);
1581 printf("unk_1 = 0x%08x\n", unk_1);
1584 return result;
1587 /* Look up domain name */
1589 static NTSTATUS cmd_samr_lookup_domain(struct cli_state *cli,
1590 TALLOC_CTX *mem_ctx,
1591 int argc, const char **argv)
1593 POLICY_HND connect_pol, domain_pol;
1594 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1595 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1596 fstring domain_name,sid_string;
1597 DOM_SID sid;
1599 if (argc != 2) {
1600 printf("Usage: %s domain_name\n", argv[0]);
1601 return NT_STATUS_OK;
1604 sscanf(argv[1], "%s", domain_name);
1606 result = try_samr_connects(cli, mem_ctx, access_mask, &connect_pol);
1608 if (!NT_STATUS_IS_OK(result))
1609 goto done;
1611 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1612 access_mask, &domain_sid, &domain_pol);
1614 if (!NT_STATUS_IS_OK(result))
1615 goto done;
1617 result = cli_samr_lookup_domain(
1618 cli, mem_ctx, &connect_pol, domain_name, &sid);
1620 sid_to_string(sid_string,&sid);
1622 if (NT_STATUS_IS_OK(result))
1623 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
1624 domain_name,sid_string);
1626 done:
1627 return result;
1631 /* List of commands exported by this module */
1633 struct cmd_set samr_commands[] = {
1635 { "SAMR" },
1637 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, PI_SAMR, "Query user info", "" },
1638 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, PI_SAMR, "Query group info", "" },
1639 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, PI_SAMR, "Query user groups", "" },
1640 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, PI_SAMR, "Query user aliases", "" },
1641 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, PI_SAMR, "Query group membership", "" },
1642 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, PI_SAMR, "Query alias membership", "" },
1643 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, PI_SAMR, "Query display info", "" },
1644 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, PI_SAMR, "Query domain info", "" },
1645 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, PI_SAMR, "Enumerate domain users", "" },
1646 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, PI_SAMR, "Enumerate domain groups", "" },
1647 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, PI_SAMR, "Enumerate alias groups", "" },
1649 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, PI_SAMR, "Create domain user", "" },
1650 { "createdomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group, NULL, PI_SAMR, "Create domain group", "" },
1651 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, PI_SAMR, "Look up names", "" },
1652 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, PI_SAMR, "Look up names", "" },
1653 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, PI_SAMR, "Delete domain user", "" },
1654 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, PI_SAMR, "Query SAMR security object", "" },
1655 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, PI_SAMR, "Retrieve domain password info", "" },
1657 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, PI_SAMR, "Lookup Domain Name", "" },
1658 { NULL }