2 Unix SMB/CIFS implementation.
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.
26 #include "rpcclient.h"
28 extern DOM_SID domain_sid;
30 /****************************************************************************
31 display sam_user_info_21 structure
32 ****************************************************************************/
33 static void display_sam_user_info_21(SAM_USER_INFO_21 *usr)
37 unistr2_to_ascii(temp, &usr->uni_user_name, sizeof(temp)-1);
38 printf("\tUser Name :\t%s\n", temp);
40 unistr2_to_ascii(temp, &usr->uni_full_name, sizeof(temp)-1);
41 printf("\tFull Name :\t%s\n", temp);
43 unistr2_to_ascii(temp, &usr->uni_home_dir, sizeof(temp)-1);
44 printf("\tHome Drive :\t%s\n", temp);
46 unistr2_to_ascii(temp, &usr->uni_dir_drive, sizeof(temp)-1);
47 printf("\tDir Drive :\t%s\n", temp);
49 unistr2_to_ascii(temp, &usr->uni_profile_path, sizeof(temp)-1);
50 printf("\tProfile Path:\t%s\n", temp);
52 unistr2_to_ascii(temp, &usr->uni_logon_script, sizeof(temp)-1);
53 printf("\tLogon Script:\t%s\n", temp);
55 unistr2_to_ascii(temp, &usr->uni_acct_desc, sizeof(temp)-1);
56 printf("\tDescription :\t%s\n", temp);
58 unistr2_to_ascii(temp, &usr->uni_workstations, sizeof(temp)-1);
59 printf("\tWorkstations:\t%s\n", temp);
61 unistr2_to_ascii(temp, &usr->uni_unknown_str, sizeof(temp)-1);
62 printf("\tUnknown Str :\t%s\n", temp);
64 unistr2_to_ascii(temp, &usr->uni_munged_dial, sizeof(temp)-1);
65 printf("\tRemote Dial :\t%s\n", temp);
67 printf("\tLogon Time :\t%s\n",
68 http_timestring(nt_time_to_unix(&usr->logon_time)));
69 printf("\tLogoff Time :\t%s\n",
70 http_timestring(nt_time_to_unix(&usr->logoff_time)));
71 printf("\tKickoff Time :\t%s\n",
72 http_timestring(nt_time_to_unix(&usr->kickoff_time)));
73 printf("\tPassword last set Time :\t%s\n",
74 http_timestring(nt_time_to_unix(&usr->pass_last_set_time)));
75 printf("\tPassword can change Time :\t%s\n",
76 http_timestring(nt_time_to_unix(&usr->pass_can_change_time)));
77 printf("\tPassword must change Time:\t%s\n",
78 http_timestring(nt_time_to_unix(&usr->pass_must_change_time)));
80 printf("\tunknown_2[0..31]...\n"); /* user passwords? */
82 printf("\tuser_rid :\t0x%x\n" , usr->user_rid ); /* User ID */
83 printf("\tgroup_rid:\t0x%x\n" , usr->group_rid); /* Group ID */
84 printf("\tacb_info :\t0x%04x\n", usr->acb_info ); /* Account Control Info */
86 printf("\tfields_present:\t0x%08x\n", usr->fields_present); /* 0x00ff ffff */
87 printf("\tlogon_divs:\t%d\n", usr->logon_divs); /* 0x0000 00a8 which is 168 which is num hrs in a week */
88 printf("\tbad_password_count:\t0x%08x\n", usr->bad_password_count);
89 printf("\tlogon_count:\t0x%08x\n", usr->logon_count);
91 printf("\tpadding1[0..7]...\n");
93 if (usr->ptr_logon_hrs) {
94 printf("\tlogon_hrs[0..%d]...\n", usr->logon_hrs.len);
98 static const char *display_time(NTTIME nttime)
100 static fstring string;
105 int days, hours, mins, secs;
107 if (nttime.high==0 && nttime.low==0)
110 if (nttime.high==0x80000000 && nttime.low==0)
117 high = high * (~nttime.high);
120 low = low/(1000*1000*10);
125 hours=(sec - (days*60*60*24)) / (60*60);
126 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
127 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
129 fstr_sprintf(string, "%u days, %u hours, %u minutes, %u seconds", days, hours, mins, secs);
133 static void display_sam_unk_info_1(SAM_UNK_INFO_1 *info1)
136 printf("Minimum password length: %d\n", info1->min_length_password);
137 printf("Password uniqueness (remember x passwords): %d\n", info1->password_history);
139 if(info1->flag&&2==2) printf("users must open a session to change password ");
142 printf("password expire in: %s\n", display_time(info1->expire));
143 printf("Min password age (allow changing in x days): %s\n", display_time(info1->min_passwordage));
146 static void display_sam_unk_info_2(SAM_UNK_INFO_2 *info2)
150 unistr2_to_ascii(name, &info2->uni_domain, sizeof(name) - 1);
151 printf("Domain:\t%s\n", name);
153 unistr2_to_ascii(name, &info2->uni_server, sizeof(name) - 1);
154 printf("Server:\t%s\n", name);
156 printf("Total Users:\t%d\n", info2->num_domain_usrs);
157 printf("Total Groups:\t%d\n", info2->num_domain_grps);
158 printf("Total Aliases:\t%d\n", info2->num_local_grps);
160 printf("Sequence No:\t%d\n", info2->seq_num);
162 printf("Unknown 0:\t0x%x\n", info2->unknown_0);
163 printf("Unknown 1:\t0x%x\n", info2->unknown_1);
164 printf("Unknown 2:\t0x%x\n", info2->unknown_2);
165 printf("Unknown 3:\t0x%x\n", info2->unknown_3);
166 printf("Unknown 4:\t0x%x\n", info2->unknown_4);
167 printf("Unknown 5:\t0x%x\n", info2->unknown_5);
168 printf("Unknown 6:\t0x%x\n", info2->unknown_6);
171 static void display_sam_info_1(SAM_ENTRY1 *e1, SAM_STR1 *s1)
175 printf("index: 0x%x ", e1->user_idx);
176 printf("RID: 0x%x ", e1->rid_user);
177 printf("acb: 0x%x ", e1->acb_info);
179 unistr2_to_ascii(tmp, &s1->uni_acct_name, sizeof(tmp)-1);
180 printf("Account: %s\t", tmp);
182 unistr2_to_ascii(tmp, &s1->uni_full_name, sizeof(tmp)-1);
183 printf("Name: %s\t", tmp);
185 unistr2_to_ascii(tmp, &s1->uni_acct_desc, sizeof(tmp)-1);
186 printf("Desc: %s\n", tmp);
189 static void display_sam_info_2(SAM_ENTRY2 *e2, SAM_STR2 *s2)
193 printf("index: 0x%x ", e2->user_idx);
194 printf("RID: 0x%x ", e2->rid_user);
195 printf("acb: 0x%x ", e2->acb_info);
197 unistr2_to_ascii(tmp, &s2->uni_srv_name, sizeof(tmp)-1);
198 printf("Account: %s\t", tmp);
200 unistr2_to_ascii(tmp, &s2->uni_srv_desc, sizeof(tmp)-1);
201 printf("Name: %s\n", tmp);
205 static void display_sam_info_3(SAM_ENTRY3 *e3, SAM_STR3 *s3)
209 printf("index: 0x%x ", e3->grp_idx);
210 printf("RID: 0x%x ", e3->rid_grp);
211 printf("attr: 0x%x ", e3->attr);
213 unistr2_to_ascii(tmp, &s3->uni_grp_name, sizeof(tmp)-1);
214 printf("Account: %s\t", tmp);
216 unistr2_to_ascii(tmp, &s3->uni_grp_desc, sizeof(tmp)-1);
217 printf("Name: %s\n", tmp);
221 static void display_sam_info_4(SAM_ENTRY4 *e4, SAM_STR4 *s4)
225 printf("index: %d ", e4->user_idx);
228 for (i=0; i<s4->acct_name.str_str_len; i++)
229 printf("%c", s4->acct_name.buffer[i]);
234 static void display_sam_info_5(SAM_ENTRY5 *e5, SAM_STR5 *s5)
238 printf("index: 0x%x ", e5->grp_idx);
241 for (i=0; i<s5->grp_name.str_str_len; i++)
242 printf("%c", s5->grp_name.buffer[i]);
247 /****************************************************************************
248 Try samr_connect4 first, then samr_conenct if it fails
249 ****************************************************************************/
250 static NTSTATUS try_samr_connects(struct cli_state *cli, TALLOC_CTX *mem_ctx,
251 uint32 access_mask, POLICY_HND *connect_pol)
253 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
255 result = cli_samr_connect4(cli, mem_ctx, access_mask, connect_pol);
256 if (!NT_STATUS_IS_OK(result)) {
257 result = cli_samr_connect(cli, mem_ctx, access_mask,
263 /**********************************************************************
264 * Query user information
266 static NTSTATUS cmd_samr_query_user(struct cli_state *cli,
268 int argc, const char **argv)
270 POLICY_HND connect_pol, domain_pol, user_pol;
271 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
272 uint32 info_level = 21;
273 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
274 SAM_USERINFO_CTR *user_ctr;
278 if ((argc < 2) || (argc > 4)) {
279 printf("Usage: %s rid [info level] [access mask] \n", argv[0]);
283 sscanf(argv[1], "%i", &user_rid);
286 sscanf(argv[2], "%i", &info_level);
289 sscanf(argv[3], "%x", &access_mask);
292 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
295 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
298 if (!NT_STATUS_IS_OK(result))
301 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
302 MAXIMUM_ALLOWED_ACCESS,
303 &domain_sid, &domain_pol);
305 if (!NT_STATUS_IS_OK(result))
308 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
310 user_rid, &user_pol);
312 if (!NT_STATUS_IS_OK(result))
315 ZERO_STRUCT(user_ctr);
317 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
318 info_level, &user_ctr);
320 if (!NT_STATUS_IS_OK(result))
323 display_sam_user_info_21(user_ctr->info.id21);
329 /****************************************************************************
331 ****************************************************************************/
332 static void display_group_info1(GROUP_INFO1 *info1)
336 unistr2_to_ascii(temp, &info1->uni_acct_name, sizeof(temp)-1);
337 printf("\tGroup Name:\t%s\n", temp);
338 unistr2_to_ascii(temp, &info1->uni_acct_desc, sizeof(temp)-1);
339 printf("\tDescription:\t%s\n", temp);
340 printf("\tunk1:%d\n", info1->unknown_1);
341 printf("\tNum Members:%d\n", info1->num_members);
344 /****************************************************************************
346 ****************************************************************************/
347 static void display_group_info4(GROUP_INFO4 *info4)
351 unistr2_to_ascii(desc, &info4->uni_acct_desc, sizeof(desc)-1);
352 printf("\tGroup Description:%s\n", desc);
355 /****************************************************************************
356 display sam sync structure
357 ****************************************************************************/
358 static void display_group_info_ctr(GROUP_INFO_CTR *ctr)
360 switch (ctr->switch_value1) {
362 display_group_info1(&ctr->group.info1);
366 display_group_info4(&ctr->group.info4);
372 /***********************************************************************
373 * Query group information
375 static NTSTATUS cmd_samr_query_group(struct cli_state *cli,
377 int argc, const char **argv)
379 POLICY_HND connect_pol, domain_pol, group_pol;
380 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
381 uint32 info_level = 1;
382 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
383 GROUP_INFO_CTR *group_ctr;
387 if ((argc < 2) || (argc > 4)) {
388 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
392 sscanf(argv[1], "%i", &group_rid);
395 sscanf(argv[2], "%i", &info_level);
398 sscanf(argv[3], "%x", &access_mask);
400 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
403 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
406 if (!NT_STATUS_IS_OK(result))
409 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
410 MAXIMUM_ALLOWED_ACCESS,
411 &domain_sid, &domain_pol);
413 if (!NT_STATUS_IS_OK(result))
416 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
418 group_rid, &group_pol);
420 if (!NT_STATUS_IS_OK(result))
423 result = cli_samr_query_groupinfo(cli, mem_ctx, &group_pol,
424 info_level, &group_ctr);
425 if (!NT_STATUS_IS_OK(result)) {
429 display_group_info_ctr(group_ctr);
435 /* Query groups a user is a member of */
437 static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli,
439 int argc, const char **argv)
441 POLICY_HND connect_pol,
444 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
447 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
452 if ((argc < 2) || (argc > 3)) {
453 printf("Usage: %s rid [access mask]\n", argv[0]);
457 sscanf(argv[1], "%i", &user_rid);
460 sscanf(argv[2], "%x", &access_mask);
462 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
465 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
468 if (!NT_STATUS_IS_OK(result))
471 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
472 MAXIMUM_ALLOWED_ACCESS,
473 &domain_sid, &domain_pol);
475 if (!NT_STATUS_IS_OK(result))
478 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
480 user_rid, &user_pol);
482 if (!NT_STATUS_IS_OK(result))
485 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
486 &num_groups, &user_gids);
488 if (!NT_STATUS_IS_OK(result))
491 for (i = 0; i < num_groups; i++) {
492 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
493 user_gids[i].g_rid, user_gids[i].attr);
500 /* Query aliases a user is a member of */
502 static NTSTATUS cmd_samr_query_useraliases(struct cli_state *cli,
504 int argc, const char **argv)
506 POLICY_HND connect_pol, domain_pol;
507 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
508 uint32 user_rid, num_aliases, *alias_rids;
509 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
514 DOM_SID global_sid_Builtin;
516 string_to_sid(&global_sid_Builtin, "S-1-5-32");
518 if ((argc < 3) || (argc > 4)) {
519 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
523 sscanf(argv[2], "%i", &user_rid);
526 sscanf(argv[3], "%x", &access_mask);
528 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
531 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
534 if (!NT_STATUS_IS_OK(result))
537 if (StrCaseCmp(argv[1], "domain")==0)
538 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
540 &domain_sid, &domain_pol);
541 else if (StrCaseCmp(argv[1], "builtin")==0)
542 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
544 &global_sid_Builtin, &domain_pol);
548 if (!NT_STATUS_IS_OK(result))
551 sid_copy(&tmp_sid, &domain_sid);
552 sid_append_rid(&tmp_sid, user_rid);
553 init_dom_sid2(&sid, &tmp_sid);
555 result = cli_samr_query_useraliases(cli, mem_ctx, &domain_pol, 1, &sid, &num_aliases, &alias_rids);
557 if (!NT_STATUS_IS_OK(result))
560 for (i = 0; i < num_aliases; i++) {
561 printf("\tgroup rid:[0x%x]\n", alias_rids[i]);
568 /* Query members of a group */
570 static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli,
572 int argc, const char **argv)
574 POLICY_HND connect_pol, domain_pol, group_pol;
575 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
576 uint32 num_members, *group_rids, *group_attrs, group_rid;
577 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
581 if ((argc < 2) || (argc > 3)) {
582 printf("Usage: %s rid [access mask]\n", argv[0]);
586 sscanf(argv[1], "%i", &group_rid);
589 sscanf(argv[2], "%x", &access_mask);
591 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
594 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
597 if (!NT_STATUS_IS_OK(result))
600 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
601 MAXIMUM_ALLOWED_ACCESS,
602 &domain_sid, &domain_pol);
604 if (!NT_STATUS_IS_OK(result))
607 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
609 group_rid, &group_pol);
611 if (!NT_STATUS_IS_OK(result))
614 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
615 &num_members, &group_rids,
618 if (!NT_STATUS_IS_OK(result))
621 for (i = 0; i < num_members; i++) {
622 printf("\trid:[0x%x] attr:[0x%x]\n", group_rids[i],
630 /* Enumerate domain users */
632 static NTSTATUS cmd_samr_enum_dom_users(struct cli_state *cli,
634 int argc, const char **argv)
636 POLICY_HND connect_pol, domain_pol;
637 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
638 uint32 start_idx, size, num_dom_users, i;
641 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
642 uint16 acb_mask = ACB_NORMAL;
643 BOOL got_connect_pol = False, got_domain_pol = False;
645 if ((argc < 1) || (argc > 2)) {
646 printf("Usage: %s [access_mask]\n", argv[0]);
651 sscanf(argv[1], "%x", &access_mask);
653 /* Get sam policy handle */
655 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
658 if (!NT_STATUS_IS_OK(result))
661 got_connect_pol = True;
663 /* Get domain policy handle */
665 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
667 &domain_sid, &domain_pol);
669 if (!NT_STATUS_IS_OK(result))
672 got_domain_pol = True;
674 /* Enumerate domain users */
680 result = cli_samr_enum_dom_users(
681 cli, mem_ctx, &domain_pol, &start_idx, acb_mask,
682 size, &dom_users, &dom_rids, &num_dom_users);
684 if (NT_STATUS_IS_OK(result) ||
685 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
687 for (i = 0; i < num_dom_users; i++)
688 printf("user:[%s] rid:[0x%x]\n",
689 dom_users[i], dom_rids[i]);
692 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
696 cli_samr_close(cli, mem_ctx, &domain_pol);
699 cli_samr_close(cli, mem_ctx, &connect_pol);
704 /* Enumerate domain groups */
706 static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli,
708 int argc, const char **argv)
710 POLICY_HND connect_pol, domain_pol;
711 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
712 uint32 start_idx, size, num_dom_groups, i;
713 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
714 struct acct_info *dom_groups;
715 BOOL got_connect_pol = False, got_domain_pol = False;
717 if ((argc < 1) || (argc > 2)) {
718 printf("Usage: %s [access_mask]\n", argv[0]);
723 sscanf(argv[1], "%x", &access_mask);
725 /* Get sam policy handle */
727 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
730 if (!NT_STATUS_IS_OK(result))
733 got_connect_pol = True;
735 /* Get domain policy handle */
737 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
739 &domain_sid, &domain_pol);
741 if (!NT_STATUS_IS_OK(result))
744 got_domain_pol = True;
746 /* Enumerate domain groups */
752 result = cli_samr_enum_dom_groups(
753 cli, mem_ctx, &domain_pol, &start_idx, size,
754 &dom_groups, &num_dom_groups);
756 if (NT_STATUS_IS_OK(result) ||
757 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
759 for (i = 0; i < num_dom_groups; i++)
760 printf("group:[%s] rid:[0x%x]\n",
761 dom_groups[i].acct_name,
765 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
769 cli_samr_close(cli, mem_ctx, &domain_pol);
772 cli_samr_close(cli, mem_ctx, &connect_pol);
777 /* Enumerate alias groups */
779 static NTSTATUS cmd_samr_enum_als_groups(struct cli_state *cli,
781 int argc, const char **argv)
783 POLICY_HND connect_pol, domain_pol;
784 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
785 uint32 start_idx, size, num_als_groups, i;
786 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
787 struct acct_info *als_groups;
788 DOM_SID global_sid_Builtin;
789 BOOL got_connect_pol = False, got_domain_pol = False;
791 string_to_sid(&global_sid_Builtin, "S-1-5-32");
793 if ((argc < 2) || (argc > 3)) {
794 printf("Usage: %s builtin|domain [access mask]\n", argv[0]);
799 sscanf(argv[2], "%x", &access_mask);
801 /* Get sam policy handle */
803 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
806 if (!NT_STATUS_IS_OK(result))
809 got_connect_pol = True;
811 /* Get domain policy handle */
813 if (StrCaseCmp(argv[1], "domain")==0)
814 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
816 &domain_sid, &domain_pol);
817 else if (StrCaseCmp(argv[1], "builtin")==0)
818 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
820 &global_sid_Builtin, &domain_pol);
824 if (!NT_STATUS_IS_OK(result))
827 got_domain_pol = True;
829 /* Enumerate alias groups */
832 size = 0xffff; /* Number of groups to retrieve */
835 result = cli_samr_enum_als_groups(
836 cli, mem_ctx, &domain_pol, &start_idx, size,
837 &als_groups, &num_als_groups);
839 if (NT_STATUS_IS_OK(result) ||
840 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
842 for (i = 0; i < num_als_groups; i++)
843 printf("group:[%s] rid:[0x%x]\n",
844 als_groups[i].acct_name,
847 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
851 cli_samr_close(cli, mem_ctx, &domain_pol);
854 cli_samr_close(cli, mem_ctx, &connect_pol);
859 /* Query alias membership */
861 static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli,
863 int argc, const char **argv)
865 POLICY_HND connect_pol, domain_pol, alias_pol;
866 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
867 uint32 alias_rid, num_members, i;
868 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
870 DOM_SID global_sid_Builtin;
872 string_to_sid(&global_sid_Builtin, "S-1-5-32");
874 if ((argc < 3) || (argc > 4)) {
875 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
879 sscanf(argv[2], "%i", &alias_rid);
882 sscanf(argv[3], "%x", &access_mask);
884 /* Open SAMR handle */
886 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
889 if (!NT_STATUS_IS_OK(result))
892 /* Open handle on domain */
894 if (StrCaseCmp(argv[1], "domain")==0)
895 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
896 MAXIMUM_ALLOWED_ACCESS,
897 &domain_sid, &domain_pol);
898 else if (StrCaseCmp(argv[1], "builtin")==0)
899 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
900 MAXIMUM_ALLOWED_ACCESS,
901 &global_sid_Builtin, &domain_pol);
905 if (!NT_STATUS_IS_OK(result))
908 /* Open handle on alias */
910 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
912 alias_rid, &alias_pol);
913 if (!NT_STATUS_IS_OK(result))
916 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
917 &num_members, &alias_sids);
919 if (!NT_STATUS_IS_OK(result))
922 for (i = 0; i < num_members; i++) {
925 sid_to_string(sid_str, &alias_sids[i]);
926 printf("\tsid:[%s]\n", sid_str);
933 /* Query display info */
935 static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli,
937 int argc, const char **argv)
939 POLICY_HND connect_pol, domain_pol;
940 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
941 uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries, i;
942 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
943 uint32 info_level = 1;
944 SAM_DISPINFO_CTR ctr;
945 SAM_DISPINFO_1 info1;
946 SAM_DISPINFO_2 info2;
947 SAM_DISPINFO_3 info3;
948 SAM_DISPINFO_4 info4;
949 SAM_DISPINFO_5 info5;
951 BOOL got_params = False; /* Use get_query_dispinfo_params() or not? */
954 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
959 sscanf(argv[1], "%i", &info_level);
962 sscanf(argv[2], "%i", &start_idx);
965 sscanf(argv[3], "%i", &max_entries);
970 sscanf(argv[4], "%i", &max_size);
975 sscanf(argv[5], "%x", &access_mask);
977 /* Get sam policy handle */
979 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
982 if (!NT_STATUS_IS_OK(result))
985 /* Get domain policy handle */
987 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
989 &domain_sid, &domain_pol);
991 if (!NT_STATUS_IS_OK(result))
994 /* Query display info */
999 switch (info_level) {
1002 ctr.sam.info1 = &info1;
1006 ctr.sam.info2 = &info2;
1010 ctr.sam.info3 = &info3;
1014 ctr.sam.info4 = &info4;
1018 ctr.sam.info5 = &info5;
1026 get_query_dispinfo_params(
1027 loop_count, &max_entries, &max_size);
1029 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1030 &start_idx, info_level,
1031 &num_entries, max_entries,
1036 if (!NT_STATUS_IS_OK(result) && !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1039 if (num_entries == 0)
1042 for (i = 0; i < num_entries; i++) {
1043 switch (info_level) {
1045 display_sam_info_1(&ctr.sam.info1->sam[i], &ctr.sam.info1->str[i]);
1048 display_sam_info_2(&ctr.sam.info2->sam[i], &ctr.sam.info2->str[i]);
1051 display_sam_info_3(&ctr.sam.info3->sam[i], &ctr.sam.info3->str[i]);
1054 display_sam_info_4(&ctr.sam.info4->sam[i], &ctr.sam.info4->str[i]);
1057 display_sam_info_5(&ctr.sam.info5->sam[i], &ctr.sam.info5->str[i]);
1067 /* Query domain info */
1069 static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli,
1070 TALLOC_CTX *mem_ctx,
1071 int argc, const char **argv)
1073 POLICY_HND connect_pol, domain_pol;
1074 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1075 uint32 switch_level = 2;
1076 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1080 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1081 return NT_STATUS_OK;
1085 sscanf(argv[1], "%i", &switch_level);
1088 sscanf(argv[2], "%x", &access_mask);
1090 /* Get sam policy handle */
1092 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1095 if (!NT_STATUS_IS_OK(result))
1098 /* Get domain policy handle */
1100 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1102 &domain_sid, &domain_pol);
1104 if (!NT_STATUS_IS_OK(result))
1107 /* Query domain info */
1109 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
1110 switch_level, &ctr);
1112 if (!NT_STATUS_IS_OK(result))
1115 /* Display domain info */
1117 switch (switch_level) {
1119 display_sam_unk_info_1(&ctr.info.inf1);
1122 display_sam_unk_info_2(&ctr.info.inf2);
1125 printf("cannot display domain info for switch value %d\n",
1132 cli_samr_close(cli, mem_ctx, &domain_pol);
1133 cli_samr_close(cli, mem_ctx, &connect_pol);
1137 /* Create domain user */
1139 static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli,
1140 TALLOC_CTX *mem_ctx,
1141 int argc, const char **argv)
1143 POLICY_HND connect_pol, domain_pol, user_pol;
1144 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1145 const char *acct_name;
1147 uint32 unknown, user_rid;
1148 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1150 if ((argc < 2) || (argc > 3)) {
1151 printf("Usage: %s username [access mask]\n", argv[0]);
1152 return NT_STATUS_OK;
1155 acct_name = argv[1];
1158 sscanf(argv[2], "%x", &access_mask);
1160 /* Get sam policy handle */
1162 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1165 if (!NT_STATUS_IS_OK(result))
1168 /* Get domain policy handle */
1170 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1172 &domain_sid, &domain_pol);
1174 if (!NT_STATUS_IS_OK(result))
1177 /* Create domain user */
1179 acb_info = ACB_NORMAL;
1180 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
1182 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1183 acct_name, acb_info, unknown,
1184 &user_pol, &user_rid);
1186 if (!NT_STATUS_IS_OK(result))
1193 /* Lookup sam names */
1195 static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli,
1196 TALLOC_CTX *mem_ctx,
1197 int argc, const char **argv)
1199 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1200 POLICY_HND connect_pol, domain_pol;
1201 uint32 flags = 0x000003e8; /* Unknown */
1202 uint32 num_rids, num_names, *name_types, *rids;
1205 DOM_SID global_sid_Builtin;
1207 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1210 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
1211 printf("check on the domain SID: S-1-5-21-x-y-z\n");
1212 printf("or check on the builtin SID: S-1-5-32\n");
1213 return NT_STATUS_OK;
1216 /* Get sam policy and domain handles */
1218 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1221 if (!NT_STATUS_IS_OK(result))
1224 if (StrCaseCmp(argv[1], "domain")==0)
1225 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1226 MAXIMUM_ALLOWED_ACCESS,
1227 &domain_sid, &domain_pol);
1228 else if (StrCaseCmp(argv[1], "builtin")==0)
1229 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1230 MAXIMUM_ALLOWED_ACCESS,
1231 &global_sid_Builtin, &domain_pol);
1233 return NT_STATUS_OK;
1235 if (!NT_STATUS_IS_OK(result))
1240 num_names = argc - 2;
1241 names = (const char **)talloc(mem_ctx, sizeof(char *) * num_names);
1243 for (i = 0; i < argc - 2; i++)
1244 names[i] = argv[i + 2];
1246 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1247 flags, num_names, names,
1248 &num_rids, &rids, &name_types);
1250 if (!NT_STATUS_IS_OK(result))
1253 /* Display results */
1255 for (i = 0; i < num_names; i++)
1256 printf("name %s: 0x%x (%d)\n", names[i], rids[i],
1263 /* Lookup sam rids */
1265 static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli,
1266 TALLOC_CTX *mem_ctx,
1267 int argc, const char **argv)
1269 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1270 POLICY_HND connect_pol, domain_pol;
1271 uint32 flags = 0x000003e8; /* Unknown */
1272 uint32 num_rids, num_names, *rids, *name_types;
1277 printf("Usage: %s rid1 [rid2 [rid3] [...]]\n", argv[0]);
1278 return NT_STATUS_OK;
1281 /* Get sam policy and domain handles */
1283 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1286 if (!NT_STATUS_IS_OK(result))
1289 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1290 MAXIMUM_ALLOWED_ACCESS,
1291 &domain_sid, &domain_pol);
1293 if (!NT_STATUS_IS_OK(result))
1298 num_rids = argc - 1;
1299 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
1301 for (i = 0; i < argc - 1; i++)
1302 sscanf(argv[i + 1], "%i", &rids[i]);
1304 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
1305 flags, num_rids, rids,
1306 &num_names, &names, &name_types);
1308 if (!NT_STATUS_IS_OK(result))
1311 /* Display results */
1313 for (i = 0; i < num_names; i++)
1314 printf("rid 0x%x: %s (%d)\n", rids[i], names[i], name_types[i]);
1320 /* Delete domain user */
1322 static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli,
1323 TALLOC_CTX *mem_ctx,
1324 int argc, const char **argv)
1326 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1327 POLICY_HND connect_pol, domain_pol, user_pol;
1328 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1330 if ((argc < 2) || (argc > 3)) {
1331 printf("Usage: %s username\n", argv[0]);
1332 return NT_STATUS_OK;
1336 sscanf(argv[2], "%x", &access_mask);
1338 /* Get sam policy and domain handles */
1340 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1343 if (!NT_STATUS_IS_OK(result))
1346 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1347 MAXIMUM_ALLOWED_ACCESS,
1348 &domain_sid, &domain_pol);
1350 if (!NT_STATUS_IS_OK(result))
1353 /* Get handle on user */
1356 uint32 *user_rids, num_rids, *name_types;
1357 uint32 flags = 0x000003e8; /* Unknown */
1359 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1360 flags, 1, (const char **)&argv[1],
1361 &num_rids, &user_rids,
1364 if (!NT_STATUS_IS_OK(result))
1367 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1369 user_rids[0], &user_pol);
1371 if (!NT_STATUS_IS_OK(result))
1377 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
1379 if (!NT_STATUS_IS_OK(result))
1382 /* Display results */
1388 /**********************************************************************
1389 * Query user security object
1391 static NTSTATUS cmd_samr_query_sec_obj(struct cli_state *cli,
1392 TALLOC_CTX *mem_ctx,
1393 int argc, const char **argv)
1395 POLICY_HND connect_pol, domain_pol, user_pol, *pol;
1396 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1397 uint32 info_level = 4;
1399 uint32 user_rid = 0;
1400 TALLOC_CTX *ctx = NULL;
1401 SEC_DESC_BUF *sec_desc_buf=NULL;
1402 BOOL domain = False;
1404 ctx=talloc_init("cmd_samr_query_sec_obj");
1406 if ((argc < 1) || (argc > 2)) {
1407 printf("Usage: %s [rid|-d]\n", argv[0]);
1408 printf("\tSpecify rid for security on user, -d for security on domain\n");
1409 return NT_STATUS_OK;
1413 if (strcmp(argv[1], "-d") == 0)
1416 sscanf(argv[1], "%i", &user_rid);
1419 slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1421 result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1424 if (!NT_STATUS_IS_OK(result))
1427 if (domain || user_rid)
1428 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1429 MAXIMUM_ALLOWED_ACCESS,
1430 &domain_sid, &domain_pol);
1432 if (!NT_STATUS_IS_OK(result))
1436 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1437 MAXIMUM_ALLOWED_ACCESS,
1438 user_rid, &user_pol);
1440 if (!NT_STATUS_IS_OK(result))
1443 /* Pick which query pol to use */
1453 /* Query SAM security object */
1455 result = cli_samr_query_sec_obj(cli, mem_ctx, pol, info_level, ctx,
1458 if (!NT_STATUS_IS_OK(result))
1461 display_sec_desc(sec_desc_buf->sec);
1464 talloc_destroy(ctx);
1468 static NTSTATUS cmd_samr_get_dom_pwinfo(struct cli_state *cli,
1469 TALLOC_CTX *mem_ctx,
1470 int argc, const char **argv)
1472 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1473 uint16 unk_0, unk_1, unk_2;
1476 printf("Usage: %s\n", argv[0]);
1477 return NT_STATUS_OK;
1480 result = cli_samr_get_dom_pwinfo(cli, mem_ctx, &unk_0, &unk_1, &unk_2);
1482 if (NT_STATUS_IS_OK(result)) {
1483 printf("unk_0 = 0x%08x\n", unk_0);
1484 printf("unk_1 = 0x%08x\n", unk_1);
1485 printf("unk_2 = 0x%08x\n", unk_2);
1491 /* Look up domain name */
1493 static NTSTATUS cmd_samr_lookup_domain(struct cli_state *cli,
1494 TALLOC_CTX *mem_ctx,
1495 int argc, const char **argv)
1497 POLICY_HND connect_pol, domain_pol;
1498 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1499 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1500 fstring domain_name,sid_string;
1504 printf("Usage: %s domain_name\n", argv[0]);
1505 return NT_STATUS_OK;
1508 sscanf(argv[1], "%s", domain_name);
1510 result = try_samr_connects(cli, mem_ctx, access_mask, &connect_pol);
1512 if (!NT_STATUS_IS_OK(result))
1515 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1516 access_mask, &domain_sid, &domain_pol);
1518 if (!NT_STATUS_IS_OK(result))
1521 result = cli_samr_lookup_domain(
1522 cli, mem_ctx, &connect_pol, domain_name, &sid);
1524 sid_to_string(sid_string,&sid);
1526 if (NT_STATUS_IS_OK(result))
1527 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
1528 domain_name,sid_string);
1535 /* List of commands exported by this module */
1537 struct cmd_set samr_commands[] = {
1541 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, PI_SAMR, "Query user info", "" },
1542 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, PI_SAMR, "Query group info", "" },
1543 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, PI_SAMR, "Query user groups", "" },
1544 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, PI_SAMR, "Query user aliases", "" },
1545 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, PI_SAMR, "Query group membership", "" },
1546 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, PI_SAMR, "Query alias membership", "" },
1547 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, PI_SAMR, "Query display info", "" },
1548 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, PI_SAMR, "Query domain info", "" },
1549 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, PI_SAMR, "Enumerate domain users", "" },
1550 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, PI_SAMR, "Enumerate domain groups", "" },
1551 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, PI_SAMR, "Enumerate alias groups", "" },
1553 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, PI_SAMR, "Create domain user", "" },
1554 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, PI_SAMR, "Look up names", "" },
1555 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, PI_SAMR, "Look up names", "" },
1556 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, PI_SAMR, "Delete domain user", "" },
1557 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, PI_SAMR, "Query SAMR security object", "" },
1558 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, PI_SAMR, "Retrieve domain password info", "" },
1560 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, PI_SAMR, "Lookup Domain Name", "" },