more rpcclient syncs with HEAD. Should not affect server functionality
[Samba.git] / source / rpcclient / cmd_samr.c
blobed152b9f5c88b25549803e5e35b8ec94a89e778d
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_21 structure
32 ****************************************************************************/
33 static void display_sam_user_info_21(SAM_USER_INFO_21 *usr)
35 fstring temp;
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 :\t%x\n" , usr->user_rid ); /* User ID */
83 printf("\tgroup_rid:\t%x\n" , usr->group_rid); /* Group ID */
84 printf("\tacb_info :\t%04x\n", usr->acb_info ); /* Account Control Info */
86 printf("\tunknown_3:\t%08x\n", usr->unknown_3); /* 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("\tunknown_5:\t%08x\n", usr->unknown_5); /* 0x0002 0000 */
90 printf("\tpadding1[0..7]...\n");
92 if (usr->ptr_logon_hrs) {
93 printf("\tlogon_hrs[0..%d]...\n", usr->logon_hrs.len);
97 static char *display_time(NTTIME nttime)
99 static fstring string;
101 float high;
102 float low;
103 int sec;
104 int days, hours, mins, secs;
106 if (nttime.high==0 && nttime.low==0)
107 return "Now";
109 if (nttime.high==0x80000000 && nttime.low==0)
110 return "Never";
112 high = 65536;
113 high = high/10000;
114 high = high*65536;
115 high = high/1000;
116 high = high * (~nttime.high);
118 low = ~nttime.low;
119 low = low/(1000*1000*10);
121 sec=high+low;
123 days=sec/(60*60*24);
124 hours=(sec - (days*60*60*24)) / (60*60);
125 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
126 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
128 snprintf(string, sizeof(string)-1, "%u days, %u hours, %u minutes, %u seconds", days, hours, mins, secs);
129 return (string);
132 static void display_sam_unk_info_1(SAM_UNK_INFO_1 *info1)
135 printf("Minimum password length: %d\n", info1->min_length_password);
136 printf("Password uniqueness (remember x passwords): %d\n", info1->password_history);
137 printf("flag: ");
138 if(info1->flag&&2==2) printf("users must open a session to change password ");
139 printf("\n");
141 printf("password expire in: %s\n", display_time(info1->expire));
142 printf("Min password age (allow changing in x days): %s\n", display_time(info1->min_passwordage));
145 static void display_sam_unk_info_2(SAM_UNK_INFO_2 *info2)
147 fstring name;
149 unistr2_to_ascii(name, &info2->uni_domain, sizeof(name) - 1);
150 printf("Domain:\t%s\n", name);
152 unistr2_to_ascii(name, &info2->uni_server, sizeof(name) - 1);
153 printf("Server:\t%s\n", name);
155 printf("Total Users:\t%d\n", info2->num_domain_usrs);
156 printf("Total Groups:\t%d\n", info2->num_domain_grps);
157 printf("Total Aliases:\t%d\n", info2->num_local_grps);
159 printf("Sequence No:\t%d\n", info2->seq_num);
161 printf("Unknown 0:\t0x%x\n", info2->unknown_0);
162 printf("Unknown 1:\t0x%x\n", info2->unknown_1);
163 printf("Unknown 2:\t0x%x\n", info2->unknown_2);
164 printf("Unknown 3:\t0x%x\n", info2->unknown_3);
165 printf("Unknown 4:\t0x%x\n", info2->unknown_4);
166 printf("Unknown 5:\t0x%x\n", info2->unknown_5);
167 printf("Unknown 6:\t0x%x\n", info2->unknown_6);
170 static void display_sam_info_1(SAM_ENTRY1 *e1, SAM_STR1 *s1)
172 fstring tmp;
174 printf("index: 0x%x ", e1->user_idx);
175 printf("RID: 0x%x ", e1->rid_user);
176 printf("acb: 0x%x ", e1->acb_info);
178 unistr2_to_ascii(tmp, &s1->uni_acct_name, sizeof(tmp)-1);
179 printf("Account: %s\t", tmp);
181 unistr2_to_ascii(tmp, &s1->uni_full_name, sizeof(tmp)-1);
182 printf("Name: %s\t", tmp);
184 unistr2_to_ascii(tmp, &s1->uni_acct_desc, sizeof(tmp)-1);
185 printf("Desc: %s\n", tmp);
188 static void display_sam_info_2(SAM_ENTRY2 *e2, SAM_STR2 *s2)
190 fstring tmp;
192 printf("index: 0x%x ", e2->user_idx);
193 printf("RID: 0x%x ", e2->rid_user);
194 printf("acb: 0x%x ", e2->acb_info);
196 unistr2_to_ascii(tmp, &s2->uni_srv_name, sizeof(tmp)-1);
197 printf("Account: %s\t", tmp);
199 unistr2_to_ascii(tmp, &s2->uni_srv_desc, sizeof(tmp)-1);
200 printf("Name: %s\n", tmp);
204 static void display_sam_info_3(SAM_ENTRY3 *e3, SAM_STR3 *s3)
206 fstring tmp;
208 printf("index: 0x%x ", e3->grp_idx);
209 printf("RID: 0x%x ", e3->rid_grp);
210 printf("attr: 0x%x ", e3->attr);
212 unistr2_to_ascii(tmp, &s3->uni_grp_name, sizeof(tmp)-1);
213 printf("Account: %s\t", tmp);
215 unistr2_to_ascii(tmp, &s3->uni_grp_desc, sizeof(tmp)-1);
216 printf("Name: %s\n", tmp);
220 static void display_sam_info_4(SAM_ENTRY4 *e4, SAM_STR4 *s4)
222 int i;
224 printf("index: %d ", e4->user_idx);
226 printf("Account: ");
227 for (i=0; i<s4->acct_name.str_str_len; i++)
228 printf("%c", s4->acct_name.buffer[i]);
229 printf("\n");
233 static void display_sam_info_5(SAM_ENTRY5 *e5, SAM_STR5 *s5)
235 int i;
237 printf("index: 0x%x ", e5->grp_idx);
239 printf("Account: ");
240 for (i=0; i<s5->grp_name.str_str_len; i++)
241 printf("%c", s5->grp_name.buffer[i]);
242 printf("\n");
246 /**********************************************************************
247 * Query user information
249 static NTSTATUS cmd_samr_query_user(struct cli_state *cli,
250 TALLOC_CTX *mem_ctx,
251 int argc, char **argv)
253 POLICY_HND connect_pol, domain_pol, user_pol;
254 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
255 uint32 info_level = 21;
256 SAM_USERINFO_CTR *user_ctr;
257 fstring server;
258 uint32 user_rid;
260 if (argc != 2) {
261 printf("Usage: %s rid\n", argv[0]);
262 return NT_STATUS_OK;
265 sscanf(argv[1], "%i", &user_rid);
267 slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
268 strupper (server);
270 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
271 &connect_pol);
273 if (!NT_STATUS_IS_OK(result))
274 goto done;
276 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
277 MAXIMUM_ALLOWED_ACCESS,
278 &domain_sid, &domain_pol);
280 if (!NT_STATUS_IS_OK(result))
281 goto done;
283 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
284 MAXIMUM_ALLOWED_ACCESS,
285 user_rid, &user_pol);
287 if (!NT_STATUS_IS_OK(result))
288 goto done;
290 ZERO_STRUCT(user_ctr);
292 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
293 info_level, &user_ctr);
295 if (!NT_STATUS_IS_OK(result))
296 goto done;
298 display_sam_user_info_21(user_ctr->info.id21);
300 done:
301 return result;
304 /****************************************************************************
305 display group info
306 ****************************************************************************/
307 static void display_group_info1(GROUP_INFO1 *info1)
309 fstring temp;
311 unistr2_to_ascii(temp, &info1->uni_acct_name, sizeof(temp)-1);
312 printf("\tGroup Name:\t%s\n", temp);
313 unistr2_to_ascii(temp, &info1->uni_acct_desc, sizeof(temp)-1);
314 printf("\tDescription:\t%s\n", temp);
315 printf("\tunk1:%d\n", info1->unknown_1);
316 printf("\tNum Members:%d\n", info1->num_members);
319 /****************************************************************************
320 display group info
321 ****************************************************************************/
322 static void display_group_info4(GROUP_INFO4 *info4)
324 fstring desc;
326 unistr2_to_ascii(desc, &info4->uni_acct_desc, sizeof(desc)-1);
327 printf("\tGroup Description:%s\n", desc);
330 /****************************************************************************
331 display sam sync structure
332 ****************************************************************************/
333 static void display_group_info_ctr(GROUP_INFO_CTR *ctr)
335 switch (ctr->switch_value1) {
336 case 1: {
337 display_group_info1(&ctr->group.info1);
338 break;
340 case 4: {
341 display_group_info4(&ctr->group.info4);
342 break;
347 /***********************************************************************
348 * Query group information
350 static NTSTATUS cmd_samr_query_group(struct cli_state *cli,
351 TALLOC_CTX *mem_ctx,
352 int argc, char **argv)
354 POLICY_HND connect_pol, domain_pol, group_pol;
355 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
356 uint32 info_level = 1;
357 GROUP_INFO_CTR group_ctr;
358 fstring server;
359 uint32 group_rid;
361 if (argc != 2) {
362 printf("Usage: %s rid\n", argv[0]);
363 return NT_STATUS_OK;
366 sscanf(argv[1], "%i", &group_rid);
368 slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
369 strupper (server);
371 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
372 &connect_pol);
374 if (!NT_STATUS_IS_OK(result))
375 goto done;
377 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
378 MAXIMUM_ALLOWED_ACCESS,
379 &domain_sid, &domain_pol);
381 if (!NT_STATUS_IS_OK(result))
382 goto done;
384 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
385 MAXIMUM_ALLOWED_ACCESS,
386 group_rid, &group_pol);
388 if (!NT_STATUS_IS_OK(result))
389 goto done;
391 ZERO_STRUCT(group_ctr);
393 result = cli_samr_query_groupinfo(cli, mem_ctx, &group_pol,
394 info_level, &group_ctr);
395 if (!NT_STATUS_IS_OK(result)) {
396 goto done;
399 display_group_info_ctr(&group_ctr);
401 done:
402 return result;
405 /* Query groups a user is a member of */
407 static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli,
408 TALLOC_CTX *mem_ctx,
409 int argc, char **argv)
411 POLICY_HND connect_pol,
412 domain_pol,
413 user_pol;
414 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
415 uint32 num_groups,
416 user_rid;
417 DOM_GID *user_gids;
418 int i;
419 fstring server;
421 if (argc != 2) {
422 printf("Usage: %s rid\n", argv[0]);
423 return NT_STATUS_OK;
426 sscanf(argv[1], "%i", &user_rid);
428 slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
429 strupper (server);
431 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
432 &connect_pol);
433 if (!NT_STATUS_IS_OK(result)) {
434 goto done;
437 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
438 MAXIMUM_ALLOWED_ACCESS,
439 &domain_sid, &domain_pol);
440 if (!NT_STATUS_IS_OK(result)) {
441 goto done;
444 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
445 MAXIMUM_ALLOWED_ACCESS,
446 user_rid, &user_pol);
447 if (!NT_STATUS_IS_OK(result)) {
448 goto done;
451 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
452 &num_groups, &user_gids);
453 if (!NT_STATUS_IS_OK(result)) {
454 goto done;
457 for (i = 0; i < num_groups; i++) {
458 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
459 user_gids[i].g_rid, user_gids[i].attr);
462 done:
463 return result;
466 /* Query aliases a user is a member of */
468 static NTSTATUS cmd_samr_query_useraliases(struct cli_state *cli,
469 TALLOC_CTX *mem_ctx,
470 int argc, char **argv)
472 POLICY_HND connect_pol, domain_pol;
473 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
474 uint32 user_rid, num_aliases, *alias_rids;
475 int i;
476 fstring server;
477 DOM_SID tmp_sid;
478 DOM_SID2 sid;
479 DOM_SID global_sid_Builtin;
481 string_to_sid(&global_sid_Builtin, "S-1-5-32");
483 if (argc != 3) {
484 printf("Usage: %s builtin|domain rid\n", argv[0]);
485 return NT_STATUS_OK;
488 sscanf(argv[2], "%i", &user_rid);
490 slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
491 strupper (server);
493 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
494 &connect_pol);
495 if (!NT_STATUS_IS_OK(result)) {
496 goto done;
499 if (StrCaseCmp(argv[1], "domain")==0)
500 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
501 MAXIMUM_ALLOWED_ACCESS,
502 &domain_sid, &domain_pol);
503 else if (StrCaseCmp(argv[1], "builtin")==0)
504 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
505 MAXIMUM_ALLOWED_ACCESS,
506 &global_sid_Builtin, &domain_pol);
507 else
508 return NT_STATUS_OK;
510 if (!NT_STATUS_IS_OK(result)) {
511 goto done;
514 sid_copy(&tmp_sid, &domain_sid);
515 sid_append_rid(&tmp_sid, user_rid);
516 init_dom_sid2(&sid, &tmp_sid);
518 result = cli_samr_query_useraliases(cli, mem_ctx, &domain_pol, 1, &sid, &num_aliases, &alias_rids);
519 if (!NT_STATUS_IS_OK(result)) {
520 goto done;
523 for (i = 0; i < num_aliases; i++) {
524 printf("\tgroup rid:[0x%x]\n", alias_rids[i]);
527 done:
528 return result;
531 /* Query members of a group */
533 static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli,
534 TALLOC_CTX *mem_ctx,
535 int argc, char **argv)
537 POLICY_HND connect_pol, domain_pol, group_pol;
538 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
539 uint32 num_members, *group_rids, *group_attrs, group_rid;
540 int i;
541 fstring server;
543 if (argc != 2) {
544 printf("Usage: %s rid\n", argv[0]);
545 return NT_STATUS_OK;
548 sscanf(argv[1], "%i", &group_rid);
550 slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
551 strupper (server);
553 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
554 &connect_pol);
555 if (!NT_STATUS_IS_OK(result)) {
556 goto done;
559 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
560 MAXIMUM_ALLOWED_ACCESS,
561 &domain_sid, &domain_pol);
562 if (!NT_STATUS_IS_OK(result)) {
563 goto done;
566 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
567 MAXIMUM_ALLOWED_ACCESS,
568 group_rid, &group_pol);
569 if (!NT_STATUS_IS_OK(result)) {
570 goto done;
573 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
574 &num_members, &group_rids,
575 &group_attrs);
576 if (!NT_STATUS_IS_OK(result)) {
577 goto done;
580 for (i = 0; i < num_members; i++) {
581 printf("\trid:[0x%x] attr:[0x%x]\n", group_rids[i],
582 group_attrs[i]);
585 done:
586 return result;
589 /* Enumerate domain groups */
591 static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli,
592 TALLOC_CTX *mem_ctx,
593 int argc, char **argv)
595 POLICY_HND connect_pol, domain_pol;
596 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
597 uint32 start_idx, size, num_dom_groups, i;
598 struct acct_info *dom_groups;
600 if (argc != 1) {
601 printf("Usage: %s\n", argv[0]);
602 return NT_STATUS_OK;
605 /* Get sam policy handle */
607 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
608 &connect_pol);
609 if (!NT_STATUS_IS_OK(result)) {
610 goto done;
613 /* Get domain policy handle */
615 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
616 MAXIMUM_ALLOWED_ACCESS,
617 &domain_sid, &domain_pol);
618 if (!NT_STATUS_IS_OK(result)) {
619 goto done;
622 /* Enumerate domain groups */
624 start_idx = 0;
625 size = 0xffff;
627 result = cli_samr_enum_dom_groups(cli, mem_ctx, &domain_pol,
628 &start_idx, size,
629 &dom_groups, &num_dom_groups);
631 for (i = 0; i < num_dom_groups; i++)
632 printf("group:[%s] rid:[0x%x]\n", dom_groups[i].acct_name,
633 dom_groups[i].rid);
635 done:
636 return result;
639 /* Enumerate domain groups */
641 static NTSTATUS cmd_samr_enum_als_groups(struct cli_state *cli,
642 TALLOC_CTX *mem_ctx,
643 int argc, char **argv)
645 POLICY_HND connect_pol, domain_pol;
646 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
647 uint32 start_idx, size, num_dom_groups, i;
648 struct acct_info *dom_groups;
649 DOM_SID global_sid_Builtin;
651 string_to_sid(&global_sid_Builtin, "S-1-5-32");
653 if (argc != 2) {
654 printf("Usage: %s builtin|domain\n", argv[0]);
655 return NT_STATUS_OK;
658 /* Get sam policy handle */
660 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
661 &connect_pol);
662 if (!NT_STATUS_IS_OK(result)) {
663 goto done;
666 /* Get domain policy handle */
668 if (StrCaseCmp(argv[1], "domain")==0)
669 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
670 MAXIMUM_ALLOWED_ACCESS,
671 &domain_sid, &domain_pol);
672 else if (StrCaseCmp(argv[1], "builtin")==0)
673 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
674 MAXIMUM_ALLOWED_ACCESS,
675 &global_sid_Builtin, &domain_pol);
676 else
677 return NT_STATUS_OK;
678 if (!NT_STATUS_IS_OK(result)) {
679 goto done;
682 /* Enumerate domain groups */
684 start_idx = 0;
685 size = 0xffff;
687 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
688 &start_idx, size,
689 &dom_groups, &num_dom_groups);
691 for (i = 0; i < num_dom_groups; i++)
692 printf("group:[%s] rid:[0x%x]\n", dom_groups[i].acct_name,
693 dom_groups[i].rid);
695 done:
696 return result;
699 /* Query alias membership */
701 static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli,
702 TALLOC_CTX *mem_ctx,
703 int argc, char **argv)
705 POLICY_HND connect_pol, domain_pol, alias_pol;
706 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
707 uint32 alias_rid, num_members, i;
708 DOM_SID *alias_sids;
710 if (argc != 2) {
711 printf("Usage: %s rid\n", argv[0]);
712 return NT_STATUS_OK;
715 sscanf(argv[1], "%i", &alias_rid);
717 /* Open SAMR handle */
719 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
720 &connect_pol);
721 if (!NT_STATUS_IS_OK(result)) {
722 goto done;
725 /* Open handle on domain */
727 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
728 MAXIMUM_ALLOWED_ACCESS,
729 &domain_sid, &domain_pol);
730 if (!NT_STATUS_IS_OK(result)) {
731 goto done;
734 /* Open handle on alias */
736 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
737 MAXIMUM_ALLOWED_ACCESS,
738 alias_rid, &alias_pol);
739 if (!NT_STATUS_IS_OK(result)) {
740 goto done;
743 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
744 &num_members, &alias_sids);
745 if (!NT_STATUS_IS_OK(result)) {
746 goto done;
749 for (i = 0; i < num_members; i++) {
750 fstring sid_str;
752 sid_to_string(sid_str, &alias_sids[i]);
753 printf("\tsid:[%s]\n", sid_str);
756 done:
757 return result;
760 /* Query display info */
762 static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli,
763 TALLOC_CTX *mem_ctx,
764 int argc, char **argv)
766 POLICY_HND connect_pol, domain_pol;
767 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
768 uint32 start_idx=0, max_entries=250, num_entries, i;
769 int info_level = 1;
770 SAM_DISPINFO_CTR ctr;
771 SAM_DISPINFO_1 info1;
772 SAM_DISPINFO_2 info2;
773 SAM_DISPINFO_3 info3;
774 SAM_DISPINFO_4 info4;
775 SAM_DISPINFO_5 info5;
777 if (argc > 4) {
778 printf("Usage: %s [info level] [start index] [max entries]\n", argv[0]);
779 return NT_STATUS_OK;
782 if (argc >= 2)
783 sscanf(argv[1], "%i", &info_level);
785 if (argc >= 3)
786 sscanf(argv[2], "%i", &start_idx);
788 if (argc >= 4)
789 sscanf(argv[3], "%i", &max_entries);
791 /* Get sam policy handle */
793 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, &connect_pol);
794 if (!NT_STATUS_IS_OK(result)) {
795 goto done;
798 /* Get domain policy handle */
800 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
801 MAXIMUM_ALLOWED_ACCESS,
802 &domain_sid, &domain_pol);
803 if (!NT_STATUS_IS_OK(result)) {
804 goto done;
807 /* Query display info */
809 ZERO_STRUCT(ctr);
810 ZERO_STRUCT(info1);
812 switch (info_level) {
813 case 1:
814 ZERO_STRUCT(info1);
815 ctr.sam.info1 = &info1;
816 break;
817 case 2:
818 ZERO_STRUCT(info2);
819 ctr.sam.info2 = &info2;
820 break;
821 case 3:
822 ZERO_STRUCT(info3);
823 ctr.sam.info3 = &info3;
824 break;
825 case 4:
826 ZERO_STRUCT(info4);
827 ctr.sam.info4 = &info4;
828 break;
829 case 5:
830 ZERO_STRUCT(info5);
831 ctr.sam.info5 = &info5;
832 break;
836 do {
837 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
838 &start_idx, info_level,
839 &num_entries, max_entries, &ctr);
841 for (i = 0; i < num_entries; i++) {
842 switch (info_level) {
843 case 1:
844 display_sam_info_1(&ctr.sam.info1->sam[i], &ctr.sam.info1->str[i]);
845 break;
846 case 2:
847 display_sam_info_2(&ctr.sam.info2->sam[i], &ctr.sam.info2->str[i]);
848 break;
849 case 3:
850 display_sam_info_3(&ctr.sam.info3->sam[i], &ctr.sam.info3->str[i]);
851 break;
852 case 4:
853 display_sam_info_4(&ctr.sam.info4->sam[i], &ctr.sam.info4->str[i]);
854 break;
855 case 5:
856 display_sam_info_5(&ctr.sam.info5->sam[i], &ctr.sam.info5->str[i]);
857 break;
860 } while (!NT_STATUS_IS_OK(result));
861 done:
862 return result;
865 /* Query domain info */
867 static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli,
868 TALLOC_CTX *mem_ctx,
869 int argc, char **argv)
871 POLICY_HND connect_pol, domain_pol;
872 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
873 int switch_value = 2;
874 SAM_UNK_CTR ctr;
876 if (argc > 2) {
877 printf("Usage: %s [infolevel]\n", argv[0]);
878 return NT_STATUS_OK;
881 if (argc == 2)
882 sscanf(argv[1], "%i", &switch_value);
884 /* Get sam policy handle */
886 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
887 &connect_pol);
888 if (!NT_STATUS_IS_OK(result)) {
889 goto done;
892 /* Get domain policy handle */
894 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
895 MAXIMUM_ALLOWED_ACCESS,
896 &domain_sid, &domain_pol);
897 if (!NT_STATUS_IS_OK(result)) {
898 goto done;
901 /* Query domain info */
903 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
904 switch_value, &ctr);
905 if (!NT_STATUS_IS_OK(result)) {
906 goto done;
909 /* Display domain info */
911 switch (switch_value) {
912 case 1:
913 display_sam_unk_info_1(&ctr.info.inf1);
914 break;
915 case 2:
916 display_sam_unk_info_2(&ctr.info.inf2);
917 break;
918 default:
919 printf("cannot display domain info for switch value %d\n",
920 switch_value);
921 break;
924 done:
926 cli_samr_close(cli, mem_ctx, &domain_pol);
927 cli_samr_close(cli, mem_ctx, &connect_pol);
928 return result;
931 /* Create domain user */
933 static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli,
934 TALLOC_CTX *mem_ctx,
935 int argc, char **argv)
937 POLICY_HND connect_pol, domain_pol, user_pol;
938 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
939 char *acct_name;
940 uint16 acb_info;
941 uint32 unknown, user_rid;
943 if (argc != 2) {
944 printf("Usage: %s username\n", argv[0]);
945 return NT_STATUS_OK;
948 acct_name = argv[1];
950 /* Get sam policy handle */
952 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
953 &connect_pol);
954 if (!NT_STATUS_IS_OK(result)) {
955 goto done;
958 /* Get domain policy handle */
960 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
961 MAXIMUM_ALLOWED_ACCESS,
962 &domain_sid, &domain_pol);
963 if (!NT_STATUS_IS_OK(result)) {
964 goto done;
967 /* Create domain user */
969 acb_info = ACB_NORMAL;
970 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
972 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
973 acct_name, acb_info, unknown,
974 &user_pol, &user_rid);
975 if (!NT_STATUS_IS_OK(result)) {
976 goto done;
979 done:
980 return result;
983 /* Lookup sam names */
985 static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli,
986 TALLOC_CTX *mem_ctx,
987 int argc, char **argv)
989 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
990 POLICY_HND connect_pol, domain_pol;
991 uint32 flags = 0x000003e8; /* Unknown */
992 uint32 num_rids, num_names, *name_types, *rids;
993 char **names;
994 int i;
995 DOM_SID global_sid_Builtin;
997 string_to_sid(&global_sid_Builtin, "S-1-5-32");
999 if (argc < 3) {
1000 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
1001 printf("check on the domain SID: S-1-5-21-x-y-z\n");
1002 printf("or check on the builtin SID: S-1-5-32\n");
1003 return NT_STATUS_OK;
1006 /* Get sam policy and domain handles */
1008 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1009 &connect_pol);
1011 if (!NT_STATUS_IS_OK(result)) {
1012 goto done;
1015 if (StrCaseCmp(argv[1], "domain")==0)
1016 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1017 MAXIMUM_ALLOWED_ACCESS,
1018 &domain_sid, &domain_pol);
1019 else if (StrCaseCmp(argv[1], "builtin")==0)
1020 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1021 MAXIMUM_ALLOWED_ACCESS,
1022 &global_sid_Builtin, &domain_pol);
1023 else
1024 return NT_STATUS_OK;
1026 if (!NT_STATUS_IS_OK(result)) {
1027 goto done;
1030 /* Look up names */
1032 num_names = argc - 2;
1033 names = (char **)talloc(mem_ctx, sizeof(char *) * num_names);
1035 for (i = 0; i < argc - 2; i++)
1036 names[i] = argv[i + 2];
1038 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1039 flags, num_names, names,
1040 &num_rids, &rids, &name_types);
1042 if (!NT_STATUS_IS_OK(result)) {
1043 goto done;
1046 /* Display results */
1048 for (i = 0; i < num_names; i++)
1049 printf("name %s: 0x%x (%d)\n", names[i], rids[i],
1050 name_types[i]);
1052 done:
1053 return result;
1056 /* Lookup sam rids */
1058 static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli,
1059 TALLOC_CTX *mem_ctx,
1060 int argc, char **argv)
1062 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1063 POLICY_HND connect_pol, domain_pol;
1064 uint32 flags = 0x000003e8; /* Unknown */
1065 uint32 num_rids, num_names, *rids, *name_types;
1066 char **names;
1067 int i;
1069 if (argc < 2) {
1070 printf("Usage: %s rid1 [rid2 [rid3] [...]]\n", argv[0]);
1071 return NT_STATUS_OK;
1074 /* Get sam policy and domain handles */
1076 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1077 &connect_pol);
1079 if (!NT_STATUS_IS_OK(result)) {
1080 goto done;
1083 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1084 MAXIMUM_ALLOWED_ACCESS,
1085 &domain_sid, &domain_pol);
1087 if (!NT_STATUS_IS_OK(result)) {
1088 goto done;
1091 /* Look up rids */
1093 num_rids = argc - 1;
1094 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
1096 for (i = 0; i < argc - 1; i++)
1097 sscanf(argv[i + 1], "%i", &rids[i]);
1099 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
1100 flags, num_rids, rids,
1101 &num_names, &names, &name_types);
1103 if (!NT_STATUS_IS_OK(result)) {
1104 goto done;
1107 /* Display results */
1109 for (i = 0; i < num_names; i++)
1110 printf("rid 0x%x: %s (%d)\n", rids[i], names[i], name_types[i]);
1112 done:
1113 return result;
1116 /* Delete domain user */
1118 static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli,
1119 TALLOC_CTX *mem_ctx,
1120 int argc, char **argv)
1122 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1123 POLICY_HND connect_pol, domain_pol, user_pol;
1125 if (argc != 2) {
1126 printf("Usage: %s username\n", argv[0]);
1127 return NT_STATUS_OK;
1130 /* Get sam policy and domain handles */
1132 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1133 &connect_pol);
1135 if (!NT_STATUS_IS_OK(result)) {
1136 goto done;
1139 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1140 MAXIMUM_ALLOWED_ACCESS,
1141 &domain_sid, &domain_pol);
1143 if (!NT_STATUS_IS_OK(result)) {
1144 goto done;
1147 /* Get handle on user */
1150 uint32 *user_rids, num_rids, *name_types;
1151 uint32 flags = 0x000003e8; /* Unknown */
1153 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1154 flags, 1, &argv[1],
1155 &num_rids, &user_rids,
1156 &name_types);
1158 if (!NT_STATUS_IS_OK(result)) {
1159 goto done;
1162 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1163 MAXIMUM_ALLOWED_ACCESS,
1164 user_rids[0], &user_pol);
1166 if (!NT_STATUS_IS_OK(result)) {
1167 goto done;
1171 /* Delete user */
1173 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
1175 if (!NT_STATUS_IS_OK(result)) {
1176 goto done;
1179 /* Display results */
1181 done:
1182 return result;
1185 /**********************************************************************
1186 * Query user security object
1188 static NTSTATUS cmd_samr_query_sec_obj(struct cli_state *cli,
1189 TALLOC_CTX *mem_ctx,
1190 int argc, char **argv)
1192 POLICY_HND connect_pol, domain_pol, user_pol, *pol;
1193 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1194 uint32 info_level = 4;
1195 fstring server;
1196 uint32 user_rid = 0;
1197 TALLOC_CTX *ctx = NULL;
1198 SEC_DESC_BUF *sec_desc_buf=NULL;
1199 BOOL domain = False;
1201 ctx=talloc_init();
1203 if (argc > 2) {
1204 printf("Usage: %s [rid|-d]\n", argv[0]);
1205 printf("\tSpecify rid for security on user, -d for security on domain\n");
1206 return NT_STATUS_OK;
1209 if (argc == 2) {
1210 if (strcmp(argv[1], "-d") == 0)
1211 domain = True;
1212 else
1213 sscanf(argv[1], "%i", &user_rid);
1216 slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1217 strupper (server);
1218 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1219 &connect_pol);
1221 if (!NT_STATUS_IS_OK(result))
1222 goto done;
1224 if (domain || user_rid)
1225 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1226 MAXIMUM_ALLOWED_ACCESS,
1227 &domain_sid, &domain_pol);
1229 if (!NT_STATUS_IS_OK(result))
1230 goto done;
1232 if (user_rid)
1233 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1234 MAXIMUM_ALLOWED_ACCESS,
1235 user_rid, &user_pol);
1237 if (!NT_STATUS_IS_OK(result))
1238 goto done;
1240 /* Pick which query pol to use */
1242 pol = &connect_pol;
1244 if (domain)
1245 pol = &domain_pol;
1247 if (user_rid)
1248 pol = &user_pol;
1250 /* Query SAM security object */
1252 result = cli_samr_query_sec_obj(cli, mem_ctx, pol, info_level, ctx,
1253 &sec_desc_buf);
1255 if (!NT_STATUS_IS_OK(result))
1256 goto done;
1258 display_sec_desc(sec_desc_buf->sec);
1260 done:
1261 talloc_destroy(ctx);
1262 return result;
1265 /* List of commands exported by this module */
1267 struct cmd_set samr_commands[] = {
1269 { "SAMR" },
1271 { "queryuser", cmd_samr_query_user, PIPE_SAMR, "Query user info", "" },
1272 { "querygroup", cmd_samr_query_group, PIPE_SAMR, "Query group info", "" },
1273 { "queryusergroups", cmd_samr_query_usergroups, PIPE_SAMR, "Query user groups", "" },
1274 { "queryuseraliases", cmd_samr_query_useraliases, PIPE_SAMR, "Query user aliases", "" },
1275 { "querygroupmem", cmd_samr_query_groupmem, PIPE_SAMR, "Query group membership", "" },
1276 { "queryaliasmem", cmd_samr_query_aliasmem, PIPE_SAMR, "Query alias membership", "" },
1277 { "querydispinfo", cmd_samr_query_dispinfo, PIPE_SAMR, "Query display info", "" },
1278 { "querydominfo", cmd_samr_query_dominfo, PIPE_SAMR, "Query domain info", "" },
1279 { "enumdomgroups", cmd_samr_enum_dom_groups, PIPE_SAMR, "Enumerate domain groups", "" },
1280 { "enumalsgroups", cmd_samr_enum_als_groups, PIPE_SAMR, "Enumerate alias groups", "" },
1282 { "createdomuser", cmd_samr_create_dom_user, PIPE_SAMR, "Create domain user", "" },
1283 { "samlookupnames", cmd_samr_lookup_names, PIPE_SAMR, "Look up names", "" },
1284 { "samlookuprids", cmd_samr_lookup_rids, PIPE_SAMR, "Look up names", "" },
1285 { "deletedomuser", cmd_samr_delete_dom_user, PIPE_SAMR, "Delete domain user", "" },
1286 { "samquerysecobj", cmd_samr_query_sec_obj, PIPE_SAMR, "Query SAMR security object", "" },
1288 { NULL }