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
9 Copyright (C) Guenther Deschner 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "rpcclient.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "../librpc/gen_ndr/ndr_samr.h"
29 #include "../librpc/gen_ndr/ndr_samr_c.h"
30 #include "rpc_client/cli_samr.h"
31 #include "rpc_client/init_samr.h"
32 #include "rpc_client/init_lsa.h"
33 #include "../libcli/security/security.h"
35 static struct dom_sid domain_sid;
37 /****************************************************************************
38 display samr_user_info_7 structure
39 ****************************************************************************/
40 static void display_samr_user_info_7(struct samr_UserInfo7 *r)
42 printf("\tUser Name :\t%s\n", r->account_name.string);
45 /****************************************************************************
46 display samr_user_info_9 structure
47 ****************************************************************************/
48 static void display_samr_user_info_9(struct samr_UserInfo9 *r)
50 printf("\tPrimary group RID :\tox%x\n", r->primary_gid);
53 /****************************************************************************
54 display samr_user_info_16 structure
55 ****************************************************************************/
56 static void display_samr_user_info_16(struct samr_UserInfo16 *r)
58 printf("\tAcct Flags :\tox%x\n", r->acct_flags);
61 /****************************************************************************
62 display samr_user_info_20 structure
63 ****************************************************************************/
64 static void display_samr_user_info_20(struct samr_UserInfo20 *r)
66 printf("\tRemote Dial :\n");
67 dump_data(0, (uint8_t *)r->parameters.array, r->parameters.length*2);
71 /****************************************************************************
72 display samr_user_info_21 structure
73 ****************************************************************************/
74 static void display_samr_user_info_21(struct samr_UserInfo21 *r)
76 printf("\tUser Name :\t%s\n", r->account_name.string);
77 printf("\tFull Name :\t%s\n", r->full_name.string);
78 printf("\tHome Drive :\t%s\n", r->home_directory.string);
79 printf("\tDir Drive :\t%s\n", r->home_drive.string);
80 printf("\tProfile Path:\t%s\n", r->profile_path.string);
81 printf("\tLogon Script:\t%s\n", r->logon_script.string);
82 printf("\tDescription :\t%s\n", r->description.string);
83 printf("\tWorkstations:\t%s\n", r->workstations.string);
84 printf("\tComment :\t%s\n", r->comment.string);
85 printf("\tRemote Dial :\n");
86 dump_data(0, (uint8_t *)r->parameters.array, r->parameters.length*2);
88 printf("\tLogon Time :\t%s\n",
89 http_timestring(talloc_tos(), nt_time_to_unix(r->last_logon)));
90 printf("\tLogoff Time :\t%s\n",
91 http_timestring(talloc_tos(), nt_time_to_unix(r->last_logoff)));
92 printf("\tKickoff Time :\t%s\n",
93 http_timestring(talloc_tos(), nt_time_to_unix(r->acct_expiry)));
94 printf("\tPassword last set Time :\t%s\n",
95 http_timestring(talloc_tos(), nt_time_to_unix(r->last_password_change)));
96 printf("\tPassword can change Time :\t%s\n",
97 http_timestring(talloc_tos(), nt_time_to_unix(r->allow_password_change)));
98 printf("\tPassword must change Time:\t%s\n",
99 http_timestring(talloc_tos(), nt_time_to_unix(r->force_password_change)));
101 printf("\tunknown_2[0..31]...\n"); /* user passwords? */
103 printf("\tuser_rid :\t0x%x\n" , r->rid); /* User ID */
104 printf("\tgroup_rid:\t0x%x\n" , r->primary_gid); /* Group ID */
105 printf("\tacb_info :\t0x%08x\n", r->acct_flags); /* Account Control Info */
107 printf("\tfields_present:\t0x%08x\n", r->fields_present); /* 0x00ff ffff */
108 printf("\tlogon_divs:\t%d\n", r->logon_hours.units_per_week); /* 0x0000 00a8 which is 168 which is num hrs in a week */
109 printf("\tbad_password_count:\t0x%08x\n", r->bad_password_count);
110 printf("\tlogon_count:\t0x%08x\n", r->logon_count);
112 printf("\tpadding1[0..7]...\n");
114 if (r->logon_hours.bits) {
115 printf("\tlogon_hrs[0..%d]...\n", r->logon_hours.units_per_week/8);
120 static void display_password_properties(uint32_t password_properties)
122 printf("password_properties: 0x%08x\n", password_properties);
124 if (password_properties & DOMAIN_PASSWORD_COMPLEX)
125 printf("\tDOMAIN_PASSWORD_COMPLEX\n");
127 if (password_properties & DOMAIN_PASSWORD_NO_ANON_CHANGE)
128 printf("\tDOMAIN_PASSWORD_NO_ANON_CHANGE\n");
130 if (password_properties & DOMAIN_PASSWORD_NO_CLEAR_CHANGE)
131 printf("\tDOMAIN_PASSWORD_NO_CLEAR_CHANGE\n");
133 if (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)
134 printf("\tDOMAIN_PASSWORD_LOCKOUT_ADMINS\n");
136 if (password_properties & DOMAIN_PASSWORD_STORE_CLEARTEXT)
137 printf("\tDOMAIN_PASSWORD_STORE_CLEARTEXT\n");
139 if (password_properties & DOMAIN_REFUSE_PASSWORD_CHANGE)
140 printf("\tDOMAIN_REFUSE_PASSWORD_CHANGE\n");
143 static void display_sam_dom_info_1(struct samr_DomInfo1 *info1)
145 printf("Minimum password length:\t\t\t%d\n",
146 info1->min_password_length);
147 printf("Password uniqueness (remember x passwords):\t%d\n",
148 info1->password_history_length);
149 display_password_properties(info1->password_properties);
150 printf("password expire in:\t\t\t\t%s\n",
151 display_time(info1->max_password_age));
152 printf("Min password age (allow changing in x days):\t%s\n",
153 display_time(info1->min_password_age));
156 static void display_sam_dom_info_2(struct samr_DomGeneralInformation *general)
158 printf("Domain:\t\t%s\n", general->domain_name.string);
159 printf("Server:\t\t%s\n", general->primary.string);
160 printf("Comment:\t%s\n", general->oem_information.string);
162 printf("Total Users:\t%d\n", general->num_users);
163 printf("Total Groups:\t%d\n", general->num_groups);
164 printf("Total Aliases:\t%d\n", general->num_aliases);
166 printf("Sequence No:\t%llu\n", (unsigned long long)general->sequence_num);
168 printf("Force Logoff:\t%d\n",
169 (int)nt_time_to_unix_abs(&general->force_logoff_time));
171 printf("Domain Server State:\t0x%x\n", general->domain_server_state);
172 printf("Server Role:\t%s\n", server_role_str(general->role));
173 printf("Unknown 3:\t0x%x\n", general->unknown3);
176 static void display_sam_dom_info_3(struct samr_DomInfo3 *info3)
178 printf("Force Logoff:\t%d\n",
179 (int)nt_time_to_unix_abs(&info3->force_logoff_time));
182 static void display_sam_dom_info_4(struct samr_DomOEMInformation *oem)
184 printf("Comment:\t%s\n", oem->oem_information.string);
187 static void display_sam_dom_info_5(struct samr_DomInfo5 *info5)
189 printf("Domain:\t\t%s\n", info5->domain_name.string);
192 static void display_sam_dom_info_6(struct samr_DomInfo6 *info6)
194 printf("Server:\t\t%s\n", info6->primary.string);
197 static void display_sam_dom_info_7(struct samr_DomInfo7 *info7)
199 printf("Server Role:\t%s\n", server_role_str(info7->role));
202 static void display_sam_dom_info_8(struct samr_DomInfo8 *info8)
204 printf("Sequence No:\t%llu\n", (unsigned long long)info8->sequence_num);
205 printf("Domain Create Time:\t%s\n",
206 http_timestring(talloc_tos(), nt_time_to_unix(info8->domain_create_time)));
209 static void display_sam_dom_info_9(struct samr_DomInfo9 *info9)
211 printf("Domain Server State:\t0x%x\n", info9->domain_server_state);
214 static void display_sam_dom_info_12(struct samr_DomInfo12 *info12)
216 printf("Bad password lockout duration: %s\n",
217 display_time(info12->lockout_duration));
218 printf("Reset Lockout after: %s\n",
219 display_time(info12->lockout_window));
220 printf("Lockout after bad attempts: %d\n",
221 info12->lockout_threshold);
224 static void display_sam_dom_info_13(struct samr_DomInfo13 *info13)
226 printf("Sequence No:\t%llu\n", (unsigned long long)info13->sequence_num);
227 printf("Domain Create Time:\t%s\n",
228 http_timestring(talloc_tos(), nt_time_to_unix(info13->domain_create_time)));
229 printf("Sequence No at last promotion:\t%llu\n",
230 (unsigned long long)info13->modified_count_at_last_promotion);
233 static void display_sam_info_1(struct samr_DispEntryGeneral *r)
235 printf("index: 0x%x ", r->idx);
236 printf("RID: 0x%x ", r->rid);
237 printf("acb: 0x%08x ", r->acct_flags);
238 printf("Account: %s\t", r->account_name.string);
239 printf("Name: %s\t", r->full_name.string);
240 printf("Desc: %s\n", r->description.string);
243 static void display_sam_info_2(struct samr_DispEntryFull *r)
245 printf("index: 0x%x ", r->idx);
246 printf("RID: 0x%x ", r->rid);
247 printf("acb: 0x%08x ", r->acct_flags);
248 printf("Account: %s\t", r->account_name.string);
249 printf("Desc: %s\n", r->description.string);
252 static void display_sam_info_3(struct samr_DispEntryFullGroup *r)
254 printf("index: 0x%x ", r->idx);
255 printf("RID: 0x%x ", r->rid);
256 printf("acb: 0x%08x ", r->acct_flags);
257 printf("Account: %s\t", r->account_name.string);
258 printf("Desc: %s\n", r->description.string);
261 static void display_sam_info_4(struct samr_DispEntryAscii *r)
263 printf("index: 0x%x ", r->idx);
264 printf("Account: %s\n", r->account_name.string);
267 static void display_sam_info_5(struct samr_DispEntryAscii *r)
269 printf("index: 0x%x ", r->idx);
270 printf("Account: %s\n", r->account_name.string);
273 static NTSTATUS rpccli_try_samr_connects(
274 struct rpc_pipe_client *cli,
276 uint32_t access_mask,
277 struct policy_handle *connect_pol)
279 struct dcerpc_binding_handle *b = cli->binding_handle;
281 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
282 uint32_t start_idx = 0;
283 uint32_t i, num_entries;
284 struct samr_SamArray *sam = NULL;
285 struct dom_sid *domsid = NULL;
287 status = dcerpc_try_samr_connects(
294 if (!NT_STATUS_IS_OK(status)) {
297 if (!NT_STATUS_IS_OK(result)) {
301 if (!is_null_sid(&domain_sid)) {
306 * Look up the servers domain SID. Just pick the first
307 * non-builtin domain from samr_EnumDomains.
310 status = dcerpc_samr_EnumDomains(
319 if (!NT_STATUS_IS_OK(status)) {
322 if (!NT_STATUS_IS_OK(result)) {
327 for (i=0; i<num_entries; i++) {
328 if (!strequal(sam->entries[i].name.string, "builtin")) {
332 if (i == num_entries) {
333 status = NT_STATUS_NOT_FOUND;
337 status = dcerpc_samr_LookupDomain(
341 &sam->entries[i].name,
344 if (!NT_STATUS_IS_OK(status)) {
347 if (!NT_STATUS_IS_OK(result)) {
352 sid_copy(&domain_sid, domsid);
358 dcerpc_samr_Close(b, mem_ctx, connect_pol, &result);
362 /****************************************************************************
363 ****************************************************************************/
365 static NTSTATUS get_domain_handle(struct rpc_pipe_client *cli,
368 struct policy_handle *connect_pol,
369 uint32_t access_mask,
370 struct dom_sid *_domain_sid,
371 struct policy_handle *domain_pol)
373 struct dcerpc_binding_handle *b = cli->binding_handle;
374 NTSTATUS status = NT_STATUS_INVALID_PARAMETER, result;
376 if (strcasecmp_m(sam, "domain") == 0) {
377 status = dcerpc_samr_OpenDomain(b, mem_ctx,
383 } else if (strcasecmp_m(sam, "builtin") == 0) {
384 status = dcerpc_samr_OpenDomain(b, mem_ctx,
387 discard_const_p(struct dom_sid2, &global_sid_Builtin),
392 if (!NT_STATUS_IS_OK(status)) {
399 /**********************************************************************
400 * Query user information
402 static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
404 int argc, const char **argv)
406 struct policy_handle connect_pol, domain_pol, user_pol;
407 NTSTATUS status, result;
408 uint32_t info_level = 21;
409 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
410 union samr_UserInfo *info = NULL;
411 uint32_t user_rid = 0;
412 struct dcerpc_binding_handle *b = cli->binding_handle;
414 if ((argc < 2) || (argc > 4)) {
415 printf("Usage: %s rid [info level] [access mask] \n", argv[0]);
419 sscanf(argv[1], "%i", &user_rid);
422 sscanf(argv[2], "%i", &info_level);
425 sscanf(argv[3], "%x", &access_mask);
428 status = rpccli_try_samr_connects(cli, mem_ctx,
429 MAXIMUM_ALLOWED_ACCESS,
431 if (!NT_STATUS_IS_OK(status)) {
435 status = dcerpc_samr_OpenDomain(b, mem_ctx,
437 MAXIMUM_ALLOWED_ACCESS,
441 if (!NT_STATUS_IS_OK(status)) {
444 if (!NT_STATUS_IS_OK(result)) {
449 status = dcerpc_samr_OpenUser(b, mem_ctx,
455 if (!NT_STATUS_IS_OK(status)) {
458 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) &&
461 /* Probably this was a user name, try lookupnames */
462 struct samr_Ids rids, types;
463 struct lsa_String lsa_acct_name;
465 init_lsa_String(&lsa_acct_name, argv[1]);
467 status = dcerpc_samr_LookupNames(b, mem_ctx,
474 if (!NT_STATUS_IS_OK(status)) {
478 if (NT_STATUS_IS_OK(result)) {
479 if (rids.count != 1) {
480 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
483 if (types.count != 1) {
484 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
488 status = dcerpc_samr_OpenUser(b, mem_ctx,
494 if (!NT_STATUS_IS_OK(status)) {
501 if (!NT_STATUS_IS_OK(result)) {
506 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
511 if (!NT_STATUS_IS_OK(status)) {
514 if (!NT_STATUS_IS_OK(result)) {
519 switch (info_level) {
521 display_samr_user_info_7(&info->info7);
524 display_samr_user_info_9(&info->info9);
527 display_samr_user_info_16(&info->info16);
530 display_samr_user_info_20(&info->info20);
533 display_samr_user_info_21(&info->info21);
536 printf("Unsupported infolevel: %d\n", info_level);
540 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
541 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
542 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
548 /****************************************************************************
550 ****************************************************************************/
551 static void display_group_info1(struct samr_GroupInfoAll *info1)
553 printf("\tGroup Name:\t%s\n", info1->name.string);
554 printf("\tDescription:\t%s\n", info1->description.string);
555 printf("\tGroup Attribute:%d\n", info1->attributes);
556 printf("\tNum Members:%d\n", info1->num_members);
559 /****************************************************************************
561 ****************************************************************************/
562 static void display_group_info2(struct lsa_String *info2)
564 printf("\tGroup Description:%s\n", info2->string);
568 /****************************************************************************
570 ****************************************************************************/
571 static void display_group_info3(struct samr_GroupInfoAttributes *info3)
573 printf("\tGroup Attribute:%d\n", info3->attributes);
577 /****************************************************************************
579 ****************************************************************************/
580 static void display_group_info4(struct lsa_String *info4)
582 printf("\tGroup Description:%s\n", info4->string);
585 /****************************************************************************
587 ****************************************************************************/
588 static void display_group_info5(struct samr_GroupInfoAll *info5)
590 printf("\tGroup Name:\t%s\n", info5->name.string);
591 printf("\tDescription:\t%s\n", info5->description.string);
592 printf("\tGroup Attribute:%d\n", info5->attributes);
593 printf("\tNum Members:%d\n", info5->num_members);
596 /****************************************************************************
597 display sam sync structure
598 ****************************************************************************/
599 static void display_group_info(union samr_GroupInfo *info,
600 enum samr_GroupInfoEnum level)
604 display_group_info1(&info->all);
607 display_group_info2(&info->name);
610 display_group_info3(&info->attributes);
613 display_group_info4(&info->description);
616 display_group_info5(&info->all2);
621 /***********************************************************************
622 * Query group information
624 static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
626 int argc, const char **argv)
628 struct policy_handle connect_pol, domain_pol, group_pol;
629 NTSTATUS status, result;
630 enum samr_GroupInfoEnum info_level = GROUPINFOALL;
631 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
632 union samr_GroupInfo *group_info = NULL;
634 struct dcerpc_binding_handle *b = cli->binding_handle;
636 if ((argc < 2) || (argc > 4)) {
637 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
641 sscanf(argv[1], "%i", &group_rid);
644 info_level = atoi(argv[2]);
647 sscanf(argv[3], "%x", &access_mask);
649 status = rpccli_try_samr_connects(cli, mem_ctx,
650 MAXIMUM_ALLOWED_ACCESS,
652 if (!NT_STATUS_IS_OK(status)) {
656 status = dcerpc_samr_OpenDomain(b, mem_ctx,
658 MAXIMUM_ALLOWED_ACCESS,
662 if (!NT_STATUS_IS_OK(status)) {
665 if (!NT_STATUS_IS_OK(result)) {
670 status = dcerpc_samr_OpenGroup(b, mem_ctx,
676 if (!NT_STATUS_IS_OK(status)) {
679 if (!NT_STATUS_IS_OK(result)) {
684 status = dcerpc_samr_QueryGroupInfo(b, mem_ctx,
689 if (!NT_STATUS_IS_OK(status)) {
692 if (!NT_STATUS_IS_OK(result)) {
697 display_group_info(group_info, info_level);
699 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
700 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
701 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
706 /* Query groups a user is a member of */
708 static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
710 int argc, const char **argv)
712 struct policy_handle connect_pol,
715 NTSTATUS status, result;
717 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
719 struct samr_RidWithAttributeArray *rid_array = NULL;
720 struct dcerpc_binding_handle *b = cli->binding_handle;
722 if ((argc < 2) || (argc > 3)) {
723 printf("Usage: %s rid [access mask]\n", argv[0]);
727 sscanf(argv[1], "%i", &user_rid);
730 sscanf(argv[2], "%x", &access_mask);
732 status = rpccli_try_samr_connects(cli, mem_ctx,
733 MAXIMUM_ALLOWED_ACCESS,
735 if (!NT_STATUS_IS_OK(status)) {
739 status = dcerpc_samr_OpenDomain(b, mem_ctx,
741 MAXIMUM_ALLOWED_ACCESS,
745 if (!NT_STATUS_IS_OK(status)) {
748 if (!NT_STATUS_IS_OK(result)) {
753 status = dcerpc_samr_OpenUser(b, mem_ctx,
760 if (!NT_STATUS_IS_OK(status)) {
763 if (!NT_STATUS_IS_OK(result)) {
768 status = dcerpc_samr_GetGroupsForUser(b, mem_ctx,
772 if (!NT_STATUS_IS_OK(status)) {
775 if (!NT_STATUS_IS_OK(result)) {
780 for (i = 0; i < rid_array->count; i++) {
781 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
782 rid_array->rids[i].rid,
783 rid_array->rids[i].attributes);
786 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
787 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
788 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
793 /* Query aliases a user is a member of */
795 static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
797 int argc, const char **argv)
799 struct policy_handle connect_pol, domain_pol;
800 NTSTATUS status, result;
801 struct dom_sid *sids;
803 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
805 struct lsa_SidArray sid_array;
806 struct samr_Ids alias_rids;
807 struct dcerpc_binding_handle *b = cli->binding_handle;
810 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
811 return NT_STATUS_INVALID_PARAMETER;
817 for (i=2; i<argc; i++) {
818 struct dom_sid tmp_sid;
819 if (!string_to_sid(&tmp_sid, argv[i])) {
820 printf("%s is not a legal SID\n", argv[i]);
821 return NT_STATUS_INVALID_PARAMETER;
823 result = add_sid_to_array(mem_ctx, &tmp_sid, &sids, &num_sids);
824 if (!NT_STATUS_IS_OK(result)) {
830 sid_array.sids = talloc_zero_array(mem_ctx, struct lsa_SidPtr, num_sids);
831 if (sid_array.sids == NULL)
832 return NT_STATUS_NO_MEMORY;
834 sid_array.sids = NULL;
837 for (i=0; i<num_sids; i++) {
838 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
839 if (!sid_array.sids[i].sid) {
840 return NT_STATUS_NO_MEMORY;
844 sid_array.num_sids = num_sids;
846 status = rpccli_try_samr_connects(cli, mem_ctx,
847 MAXIMUM_ALLOWED_ACCESS,
849 if (!NT_STATUS_IS_OK(status)) {
853 status = get_domain_handle(cli, mem_ctx, argv[1],
858 if (!NT_STATUS_IS_OK(status)) {
862 status = dcerpc_samr_GetAliasMembership(b, mem_ctx,
867 if (!NT_STATUS_IS_OK(status)) {
870 if (!NT_STATUS_IS_OK(result)) {
875 for (i = 0; i < alias_rids.count; i++) {
876 printf("\tgroup rid:[0x%x]\n", alias_rids.ids[i]);
879 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
880 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
885 /* Query members of a group */
887 static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
889 int argc, const char **argv)
891 struct policy_handle connect_pol, domain_pol, group_pol;
892 NTSTATUS status, result;
894 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
896 unsigned int old_timeout;
897 struct samr_RidAttrArray *rids = NULL;
898 struct dcerpc_binding_handle *b = cli->binding_handle;
900 if ((argc < 2) || (argc > 3)) {
901 printf("Usage: %s rid [access mask]\n", argv[0]);
905 sscanf(argv[1], "%i", &group_rid);
908 sscanf(argv[2], "%x", &access_mask);
910 status = rpccli_try_samr_connects(cli, mem_ctx,
911 MAXIMUM_ALLOWED_ACCESS,
913 if (!NT_STATUS_IS_OK(status)) {
917 status = dcerpc_samr_OpenDomain(b, mem_ctx,
919 MAXIMUM_ALLOWED_ACCESS,
923 if (!NT_STATUS_IS_OK(status)) {
926 if (!NT_STATUS_IS_OK(result)) {
931 status = dcerpc_samr_OpenGroup(b, mem_ctx,
937 if (!NT_STATUS_IS_OK(status)) {
940 if (!NT_STATUS_IS_OK(result)) {
945 /* Make sure to wait for our DC's reply */
946 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
947 rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
949 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
954 rpccli_set_timeout(cli, old_timeout);
956 if (!NT_STATUS_IS_OK(status)) {
959 if (!NT_STATUS_IS_OK(result)) {
964 for (i = 0; i < rids->count; i++) {
965 printf("\trid:[0x%x] attr:[0x%x]\n", rids->rids[i],
966 rids->attributes[i]);
969 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
970 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
971 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
976 /* Enumerate domain users */
978 static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
980 int argc, const char **argv)
982 struct policy_handle connect_pol;
983 struct policy_handle domain_pol = { 0, };
984 NTSTATUS status, result;
985 uint32_t start_idx, num_dom_users, i;
986 struct samr_SamArray *dom_users = NULL;
987 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
988 uint32_t acb_mask = ACB_NORMAL;
989 uint32_t size = 0xffff;
990 struct dcerpc_binding_handle *b = cli->binding_handle;
992 if ((argc < 1) || (argc > 4)) {
993 printf("Usage: %s [access_mask] [acb_mask] [size]\n", argv[0]);
998 sscanf(argv[1], "%x", &access_mask);
1002 sscanf(argv[2], "%x", &acb_mask);
1006 sscanf(argv[3], "%x", &size);
1009 /* Get sam policy handle */
1011 status = rpccli_try_samr_connects(cli, mem_ctx,
1012 MAXIMUM_ALLOWED_ACCESS,
1014 if (!NT_STATUS_IS_OK(status)) {
1018 /* Get domain policy handle */
1020 status = get_domain_handle(cli, mem_ctx, "domain",
1025 if (!NT_STATUS_IS_OK(status)) {
1029 /* Enumerate domain users */
1034 status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
1042 if (!NT_STATUS_IS_OK(status)) {
1045 if (NT_STATUS_IS_OK(result) ||
1046 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1048 for (i = 0; i < num_dom_users; i++)
1049 printf("user:[%s] rid:[0x%x]\n",
1050 dom_users->entries[i].name.string,
1051 dom_users->entries[i].idx);
1054 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1057 if (is_valid_policy_hnd(&domain_pol))
1058 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1060 if (is_valid_policy_hnd(&connect_pol))
1061 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1066 /* Enumerate domain groups */
1068 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
1069 TALLOC_CTX *mem_ctx,
1070 int argc, const char **argv)
1072 struct policy_handle connect_pol;
1073 struct policy_handle domain_pol = { 0, };
1074 NTSTATUS status, result;
1075 uint32_t start_idx, num_dom_groups, i;
1076 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1077 struct samr_SamArray *dom_groups = NULL;
1078 uint32_t size = 0xffff;
1079 struct dcerpc_binding_handle *b = cli->binding_handle;
1081 if ((argc < 1) || (argc > 3)) {
1082 printf("Usage: %s [access_mask] [max_size]\n", argv[0]);
1083 return NT_STATUS_OK;
1087 sscanf(argv[1], "%x", &access_mask);
1091 sscanf(argv[2], "%x", &size);
1094 /* Get sam policy handle */
1096 status = rpccli_try_samr_connects(cli, mem_ctx,
1097 MAXIMUM_ALLOWED_ACCESS,
1099 if (!NT_STATUS_IS_OK(status)) {
1103 /* Get domain policy handle */
1105 status = get_domain_handle(cli, mem_ctx, "domain",
1110 if (!NT_STATUS_IS_OK(status)) {
1114 /* Enumerate domain groups */
1119 status = dcerpc_samr_EnumDomainGroups(b, mem_ctx,
1126 if (!NT_STATUS_IS_OK(status)) {
1129 if (NT_STATUS_IS_OK(result) ||
1130 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1132 for (i = 0; i < num_dom_groups; i++)
1133 printf("group:[%s] rid:[0x%x]\n",
1134 dom_groups->entries[i].name.string,
1135 dom_groups->entries[i].idx);
1138 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1141 if (is_valid_policy_hnd(&domain_pol))
1142 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1144 if (is_valid_policy_hnd(&connect_pol))
1145 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1150 /* Enumerate alias groups */
1152 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
1153 TALLOC_CTX *mem_ctx,
1154 int argc, const char **argv)
1156 struct policy_handle connect_pol;
1157 struct policy_handle domain_pol = { 0, };
1158 NTSTATUS status, result;
1159 uint32_t start_idx, num_als_groups, i;
1160 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1161 struct samr_SamArray *als_groups = NULL;
1162 uint32_t size = 0xffff;
1163 struct dcerpc_binding_handle *b = cli->binding_handle;
1165 if ((argc < 2) || (argc > 4)) {
1166 printf("Usage: %s builtin|domain [access mask] [max_size]\n", argv[0]);
1167 return NT_STATUS_OK;
1171 sscanf(argv[2], "%x", &access_mask);
1175 sscanf(argv[3], "%x", &size);
1178 /* Get sam policy handle */
1180 status = rpccli_try_samr_connects(cli, mem_ctx,
1181 MAXIMUM_ALLOWED_ACCESS,
1183 if (!NT_STATUS_IS_OK(status)) {
1187 /* Get domain policy handle */
1189 status = get_domain_handle(cli, mem_ctx, argv[1],
1194 if (!NT_STATUS_IS_OK(status)) {
1198 /* Enumerate alias groups */
1203 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
1210 if (!NT_STATUS_IS_OK(status)) {
1213 if (NT_STATUS_IS_OK(result) ||
1214 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1216 for (i = 0; i < num_als_groups; i++)
1217 printf("group:[%s] rid:[0x%x]\n",
1218 als_groups->entries[i].name.string,
1219 als_groups->entries[i].idx);
1221 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1224 if (is_valid_policy_hnd(&domain_pol))
1225 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1227 if (is_valid_policy_hnd(&connect_pol))
1228 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1233 /* Enumerate domains */
1235 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
1236 TALLOC_CTX *mem_ctx,
1237 int argc, const char **argv)
1239 struct policy_handle connect_pol;
1240 NTSTATUS status, result;
1241 uint32_t start_idx, size, num_entries, i;
1242 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1243 struct samr_SamArray *sam = NULL;
1244 struct dcerpc_binding_handle *b = cli->binding_handle;
1246 if ((argc < 1) || (argc > 2)) {
1247 printf("Usage: %s [access mask]\n", argv[0]);
1248 return NT_STATUS_OK;
1252 sscanf(argv[1], "%x", &access_mask);
1255 /* Get sam policy handle */
1257 status = rpccli_try_samr_connects(cli, mem_ctx,
1260 if (!NT_STATUS_IS_OK(status)) {
1264 /* Enumerate domains */
1270 status = dcerpc_samr_EnumDomains(b, mem_ctx,
1277 if (!NT_STATUS_IS_OK(status)) {
1280 if (NT_STATUS_IS_OK(result) ||
1281 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1283 for (i = 0; i < num_entries; i++)
1284 printf("name:[%s] idx:[0x%x]\n",
1285 sam->entries[i].name.string,
1286 sam->entries[i].idx);
1288 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1291 if (is_valid_policy_hnd(&connect_pol)) {
1292 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1299 /* Query alias membership */
1301 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1302 TALLOC_CTX *mem_ctx,
1303 int argc, const char **argv)
1305 struct policy_handle connect_pol, domain_pol, alias_pol;
1306 NTSTATUS status, result;
1307 uint32_t alias_rid, i;
1308 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1309 struct lsa_SidArray sid_array;
1310 struct dcerpc_binding_handle *b = cli->binding_handle;
1312 if ((argc < 3) || (argc > 4)) {
1313 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1314 return NT_STATUS_OK;
1317 sscanf(argv[2], "%i", &alias_rid);
1320 sscanf(argv[3], "%x", &access_mask);
1322 /* Open SAMR handle */
1324 status = rpccli_try_samr_connects(cli, mem_ctx,
1325 MAXIMUM_ALLOWED_ACCESS,
1327 if (!NT_STATUS_IS_OK(status)) {
1331 /* Open handle on domain */
1333 status = get_domain_handle(cli, mem_ctx, argv[1],
1335 MAXIMUM_ALLOWED_ACCESS,
1338 if (!NT_STATUS_IS_OK(status)) {
1342 /* Open handle on alias */
1344 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1350 if (!NT_STATUS_IS_OK(status)) {
1353 if (!NT_STATUS_IS_OK(result)) {
1358 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
1362 if (!NT_STATUS_IS_OK(status)) {
1365 if (!NT_STATUS_IS_OK(result)) {
1370 for (i = 0; i < sid_array.num_sids; i++) {
1371 struct dom_sid_buf sid_str;
1373 printf("\tsid:[%s]\n",
1374 dom_sid_str_buf(sid_array.sids[i].sid, &sid_str));
1377 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1378 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1379 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1384 /* Query alias info */
1386 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
1387 TALLOC_CTX *mem_ctx,
1388 int argc, const char **argv)
1390 struct policy_handle connect_pol, domain_pol, alias_pol;
1391 NTSTATUS status, result;
1393 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1394 union samr_AliasInfo *info = NULL;
1395 enum samr_AliasInfoEnum level = ALIASINFOALL;
1396 struct dcerpc_binding_handle *b = cli->binding_handle;
1398 if ((argc < 3) || (argc > 4)) {
1399 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1401 return NT_STATUS_OK;
1404 sscanf(argv[2], "%i", &alias_rid);
1407 level = atoi(argv[3]);
1411 sscanf(argv[4], "%x", &access_mask);
1414 /* Open SAMR handle */
1416 status = rpccli_try_samr_connects(cli, mem_ctx,
1417 SEC_FLAG_MAXIMUM_ALLOWED,
1419 if (!NT_STATUS_IS_OK(status)) {
1423 /* Open handle on domain */
1425 status = get_domain_handle(cli, mem_ctx, argv[1],
1427 SEC_FLAG_MAXIMUM_ALLOWED,
1430 if (!NT_STATUS_IS_OK(status)) {
1434 /* Open handle on alias */
1436 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1442 if (!NT_STATUS_IS_OK(status)) {
1445 if (!NT_STATUS_IS_OK(result)) {
1450 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
1455 if (!NT_STATUS_IS_OK(status)) {
1458 if (!NT_STATUS_IS_OK(result)) {
1465 printf("Name: %s\n", info->all.name.string);
1466 printf("Description: %s\n", info->all.description.string);
1467 printf("Num Members: %d\n", info->all.num_members);
1470 printf("Name: %s\n", info->name.string);
1472 case ALIASINFODESCRIPTION:
1473 printf("Description: %s\n", info->description.string);
1479 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1480 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1481 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1487 /* Query delete an alias membership */
1489 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1490 TALLOC_CTX *mem_ctx,
1491 int argc, const char **argv)
1493 struct policy_handle connect_pol, domain_pol, alias_pol;
1494 NTSTATUS status, result;
1496 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1497 struct dcerpc_binding_handle *b = cli->binding_handle;
1501 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1502 return NT_STATUS_OK;
1505 alias_rid = smb_strtoul(argv[2], NULL, 10, &error, SMB_STR_STANDARD);
1507 return NT_STATUS_INVALID_PARAMETER;
1511 /* Open SAMR handle */
1513 status = rpccli_try_samr_connects(cli, mem_ctx,
1514 MAXIMUM_ALLOWED_ACCESS,
1516 if (!NT_STATUS_IS_OK(status)) {
1520 /* Open handle on domain */
1522 status = get_domain_handle(cli, mem_ctx, argv[1],
1524 MAXIMUM_ALLOWED_ACCESS,
1527 if (!NT_STATUS_IS_OK(status)) {
1531 /* Open handle on alias */
1533 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1539 if (!NT_STATUS_IS_OK(status)) {
1542 if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1543 /* Probably this was a user name, try lookupnames */
1544 struct samr_Ids rids, types;
1545 struct lsa_String lsa_acct_name;
1547 init_lsa_String(&lsa_acct_name, argv[2]);
1549 status = dcerpc_samr_LookupNames(b, mem_ctx,
1556 if (!NT_STATUS_IS_OK(status)) {
1559 if (NT_STATUS_IS_OK(result)) {
1560 if (rids.count != 1) {
1561 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1564 if (types.count != 1) {
1565 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1569 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1575 if (!NT_STATUS_IS_OK(status)) {
1581 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1584 if (!NT_STATUS_IS_OK(status)) {
1587 if (!NT_STATUS_IS_OK(result)) {
1592 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1593 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1598 /* Query display info */
1600 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1601 TALLOC_CTX *mem_ctx,
1602 int argc, const char **argv,
1605 struct policy_handle connect_pol, domain_pol;
1606 NTSTATUS status, result;
1607 uint32_t start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1608 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1609 uint32_t info_level = 1;
1610 union samr_DispInfo info;
1612 bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1613 uint32_t total_size, returned_size;
1614 struct dcerpc_binding_handle *b = cli->binding_handle;
1617 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1618 return NT_STATUS_OK;
1622 sscanf(argv[1], "%i", &info_level);
1625 sscanf(argv[2], "%i", &start_idx);
1628 sscanf(argv[3], "%i", &max_entries);
1633 sscanf(argv[4], "%i", &max_size);
1638 sscanf(argv[5], "%x", &access_mask);
1640 /* Get sam policy handle */
1642 status = rpccli_try_samr_connects(cli, mem_ctx,
1643 MAXIMUM_ALLOWED_ACCESS,
1645 if (!NT_STATUS_IS_OK(status)) {
1649 /* Get domain policy handle */
1651 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1657 if (!NT_STATUS_IS_OK(status)) {
1660 if (!NT_STATUS_IS_OK(result)) {
1665 /* Query display info */
1670 dcerpc_get_query_dispinfo_params(
1671 loop_count, &max_entries, &max_size);
1674 case NDR_SAMR_QUERYDISPLAYINFO:
1675 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1686 case NDR_SAMR_QUERYDISPLAYINFO2:
1687 status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1699 case NDR_SAMR_QUERYDISPLAYINFO3:
1700 status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1713 return NT_STATUS_INVALID_PARAMETER;
1716 if (!NT_STATUS_IS_OK(status)) {
1720 if (!NT_STATUS_IS_OK(result) &&
1721 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1722 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1728 switch (info_level) {
1730 num_entries = info.info1.count;
1733 num_entries = info.info2.count;
1736 num_entries = info.info3.count;
1739 num_entries = info.info4.count;
1742 num_entries = info.info5.count;
1748 start_idx += num_entries;
1750 if (num_entries == 0)
1753 for (i = 0; i < num_entries; i++) {
1754 switch (info_level) {
1756 display_sam_info_1(&info.info1.entries[i]);
1759 display_sam_info_2(&info.info2.entries[i]);
1762 display_sam_info_3(&info.info3.entries[i]);
1765 display_sam_info_4(&info.info4.entries[i]);
1768 display_sam_info_5(&info.info5.entries[i]);
1772 } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1774 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1775 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1780 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1781 TALLOC_CTX *mem_ctx,
1782 int argc, const char **argv)
1784 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1785 NDR_SAMR_QUERYDISPLAYINFO);
1788 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1789 TALLOC_CTX *mem_ctx,
1790 int argc, const char **argv)
1792 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1793 NDR_SAMR_QUERYDISPLAYINFO2);
1796 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1797 TALLOC_CTX *mem_ctx,
1798 int argc, const char **argv)
1800 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1801 NDR_SAMR_QUERYDISPLAYINFO3);
1804 /* Query domain info */
1806 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1807 TALLOC_CTX *mem_ctx,
1808 int argc, const char **argv)
1810 struct policy_handle connect_pol, domain_pol;
1811 NTSTATUS status, result;
1812 uint32_t switch_level = 2;
1813 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1814 union samr_DomainInfo *info = NULL;
1815 struct dcerpc_binding_handle *b = cli->binding_handle;
1818 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1819 return NT_STATUS_OK;
1823 sscanf(argv[1], "%i", &switch_level);
1826 sscanf(argv[2], "%x", &access_mask);
1828 /* Get sam policy handle */
1830 status = rpccli_try_samr_connects(cli, mem_ctx,
1831 MAXIMUM_ALLOWED_ACCESS,
1833 if (!NT_STATUS_IS_OK(status)) {
1837 /* Get domain policy handle */
1839 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1845 if (!NT_STATUS_IS_OK(status)) {
1848 if (!NT_STATUS_IS_OK(result)) {
1853 /* Query domain info */
1855 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1860 if (!NT_STATUS_IS_OK(status)) {
1863 if (!NT_STATUS_IS_OK(result)) {
1868 /* Display domain info */
1870 switch (switch_level) {
1872 display_sam_dom_info_1(&info->info1);
1875 display_sam_dom_info_2(&info->general);
1878 display_sam_dom_info_3(&info->info3);
1881 display_sam_dom_info_4(&info->oem);
1884 display_sam_dom_info_5(&info->info5);
1887 display_sam_dom_info_6(&info->info6);
1890 display_sam_dom_info_7(&info->info7);
1893 display_sam_dom_info_8(&info->info8);
1896 display_sam_dom_info_9(&info->info9);
1899 display_sam_dom_info_12(&info->info12);
1902 display_sam_dom_info_13(&info->info13);
1906 printf("cannot display domain info for switch value %d\n",
1913 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1914 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1918 /* Create domain user */
1920 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1921 TALLOC_CTX *mem_ctx,
1922 int argc, const char **argv)
1924 struct policy_handle connect_pol, domain_pol, user_pol;
1925 NTSTATUS status, result;
1926 struct lsa_String acct_name;
1928 uint32_t acct_flags, user_rid;
1929 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1930 uint32_t access_granted = 0;
1931 struct dcerpc_binding_handle *b = cli->binding_handle;
1933 if ((argc < 2) || (argc > 3)) {
1934 printf("Usage: %s username [access mask]\n", argv[0]);
1935 return NT_STATUS_OK;
1938 init_lsa_String(&acct_name, argv[1]);
1941 sscanf(argv[2], "%x", &access_mask);
1943 /* Get sam policy handle */
1945 status = rpccli_try_samr_connects(cli, mem_ctx,
1946 MAXIMUM_ALLOWED_ACCESS,
1948 if (!NT_STATUS_IS_OK(status)) {
1952 /* Get domain policy handle */
1954 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1960 if (!NT_STATUS_IS_OK(status)) {
1963 if (!NT_STATUS_IS_OK(result)) {
1968 /* Create domain user */
1970 acb_info = ACB_NORMAL;
1971 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1972 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1973 SAMR_USER_ACCESS_SET_PASSWORD |
1974 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1975 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1977 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1986 if (!NT_STATUS_IS_OK(status)) {
1989 if (!NT_STATUS_IS_OK(result)) {
1994 status = dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1995 if (!NT_STATUS_IS_OK(status)) goto done;
1997 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1998 if (!NT_STATUS_IS_OK(status)) goto done;
2000 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2001 if (!NT_STATUS_IS_OK(status)) goto done;
2007 /* Create domain group */
2009 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
2010 TALLOC_CTX *mem_ctx,
2011 int argc, const char **argv)
2013 struct policy_handle connect_pol, domain_pol, group_pol;
2014 NTSTATUS status, result;
2015 struct lsa_String grp_name;
2016 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2018 struct dcerpc_binding_handle *b = cli->binding_handle;
2020 if ((argc < 2) || (argc > 3)) {
2021 printf("Usage: %s groupname [access mask]\n", argv[0]);
2022 return NT_STATUS_OK;
2025 init_lsa_String(&grp_name, argv[1]);
2028 sscanf(argv[2], "%x", &access_mask);
2030 /* Get sam policy handle */
2032 status = rpccli_try_samr_connects(cli, mem_ctx,
2033 MAXIMUM_ALLOWED_ACCESS,
2035 if (!NT_STATUS_IS_OK(status)) {
2039 /* Get domain policy handle */
2041 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2047 if (!NT_STATUS_IS_OK(status)) {
2050 if (!NT_STATUS_IS_OK(result)) {
2055 /* Create domain user */
2056 status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
2059 MAXIMUM_ALLOWED_ACCESS,
2063 if (!NT_STATUS_IS_OK(status)) {
2066 if (!NT_STATUS_IS_OK(result)) {
2071 status = dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2072 if (!NT_STATUS_IS_OK(status)) goto done;
2074 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2075 if (!NT_STATUS_IS_OK(status)) goto done;
2077 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2078 if (!NT_STATUS_IS_OK(status)) goto done;
2084 /* Create domain alias */
2086 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
2087 TALLOC_CTX *mem_ctx,
2088 int argc, const char **argv)
2090 struct policy_handle connect_pol, domain_pol, alias_pol;
2091 NTSTATUS status, result;
2092 struct lsa_String alias_name;
2093 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2095 struct dcerpc_binding_handle *b = cli->binding_handle;
2097 if ((argc < 2) || (argc > 3)) {
2098 printf("Usage: %s aliasname [access mask]\n", argv[0]);
2099 return NT_STATUS_OK;
2102 init_lsa_String(&alias_name, argv[1]);
2105 sscanf(argv[2], "%x", &access_mask);
2107 /* Get sam policy handle */
2109 status = rpccli_try_samr_connects(cli, mem_ctx,
2110 MAXIMUM_ALLOWED_ACCESS,
2112 if (!NT_STATUS_IS_OK(status)) {
2116 /* Get domain policy handle */
2118 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2124 if (!NT_STATUS_IS_OK(status)) {
2127 if (!NT_STATUS_IS_OK(result)) {
2132 /* Create domain user */
2134 status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2137 MAXIMUM_ALLOWED_ACCESS,
2141 if (!NT_STATUS_IS_OK(status)) {
2144 if (!NT_STATUS_IS_OK(result)) {
2150 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
2151 if (!NT_STATUS_IS_OK(status)) goto done;
2153 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2154 if (!NT_STATUS_IS_OK(status)) goto done;
2156 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2157 if (!NT_STATUS_IS_OK(status)) goto done;
2163 /* Lookup sam names */
2165 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
2166 TALLOC_CTX *mem_ctx,
2167 int argc, const char **argv)
2169 NTSTATUS status, result;
2170 struct policy_handle connect_pol, domain_pol;
2172 struct samr_Ids rids, name_types;
2174 struct lsa_String *names = NULL;
2175 struct dcerpc_binding_handle *b = cli->binding_handle;
2178 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
2179 printf("check on the domain SID: S-1-5-21-x-y-z\n");
2180 printf("or check on the builtin SID: S-1-5-32\n");
2181 return NT_STATUS_OK;
2184 /* Get sam policy and domain handles */
2186 status = rpccli_try_samr_connects(cli, mem_ctx,
2187 MAXIMUM_ALLOWED_ACCESS,
2189 if (!NT_STATUS_IS_OK(status)) {
2193 status = get_domain_handle(cli, mem_ctx, argv[1],
2195 MAXIMUM_ALLOWED_ACCESS,
2198 if (!NT_STATUS_IS_OK(status)) {
2204 num_names = argc - 2;
2206 if ((names = talloc_array(mem_ctx, struct lsa_String, num_names)) == NULL) {
2207 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2208 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2209 status = NT_STATUS_NO_MEMORY;
2213 for (i = 0; i < num_names; i++) {
2214 init_lsa_String(&names[i], argv[i + 2]);
2217 status = dcerpc_samr_LookupNames(b, mem_ctx,
2224 if (!NT_STATUS_IS_OK(status)) {
2227 if (!NT_STATUS_IS_OK(result)) {
2231 if (rids.count != num_names) {
2232 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2235 if (name_types.count != num_names) {
2236 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2240 /* Display results */
2242 for (i = 0; i < num_names; i++)
2243 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
2246 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2247 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2252 /* Lookup sam rids */
2254 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
2255 TALLOC_CTX *mem_ctx,
2256 int argc, const char **argv)
2258 NTSTATUS status, result;
2259 struct policy_handle connect_pol, domain_pol;
2260 uint32_t num_rids, *rids;
2261 struct lsa_Strings names;
2262 struct samr_Ids types;
2263 struct dcerpc_binding_handle *b = cli->binding_handle;
2268 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
2269 return NT_STATUS_OK;
2272 /* Get sam policy and domain handles */
2274 status = rpccli_try_samr_connects(cli, mem_ctx,
2275 MAXIMUM_ALLOWED_ACCESS,
2277 if (!NT_STATUS_IS_OK(status)) {
2281 status = get_domain_handle(cli, mem_ctx, argv[1],
2283 MAXIMUM_ALLOWED_ACCESS,
2286 if (!NT_STATUS_IS_OK(status)) {
2292 num_rids = argc - 2;
2294 if ((rids = talloc_array(mem_ctx, uint32_t, num_rids)) == NULL) {
2295 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2296 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2297 status = NT_STATUS_NO_MEMORY;
2301 for (i = 0; i < argc - 2; i++)
2302 sscanf(argv[i + 2], "%i", &rids[i]);
2304 status = dcerpc_samr_LookupRids(b, mem_ctx,
2311 if (!NT_STATUS_IS_OK(status)) {
2315 if (!NT_STATUS_IS_OK(result) &&
2316 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2319 /* Display results */
2320 if (num_rids != names.count) {
2321 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2324 if (num_rids != types.count) {
2325 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2329 for (i = 0; i < num_rids; i++) {
2330 printf("rid 0x%x: %s (%d)\n",
2331 rids[i], names.names[i].string, types.ids[i]);
2334 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2335 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2340 /* Delete domain group */
2342 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
2343 TALLOC_CTX *mem_ctx,
2344 int argc, const char **argv)
2346 NTSTATUS status, result;
2347 struct policy_handle connect_pol, domain_pol, group_pol;
2348 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2349 struct dcerpc_binding_handle *b = cli->binding_handle;
2351 if ((argc < 2) || (argc > 3)) {
2352 printf("Usage: %s groupname\n", argv[0]);
2353 return NT_STATUS_OK;
2357 sscanf(argv[2], "%x", &access_mask);
2359 /* Get sam policy and domain handles */
2361 status = rpccli_try_samr_connects(cli, mem_ctx,
2362 MAXIMUM_ALLOWED_ACCESS,
2364 if (!NT_STATUS_IS_OK(status)) {
2368 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2370 MAXIMUM_ALLOWED_ACCESS,
2374 if (!NT_STATUS_IS_OK(status)) {
2377 if (!NT_STATUS_IS_OK(result)) {
2382 /* Get handle on group */
2385 struct samr_Ids group_rids, name_types;
2386 struct lsa_String lsa_acct_name;
2388 init_lsa_String(&lsa_acct_name, argv[1]);
2390 status = dcerpc_samr_LookupNames(b, mem_ctx,
2397 if (!NT_STATUS_IS_OK(status)) {
2400 if (!NT_STATUS_IS_OK(result)) {
2404 if (group_rids.count != 1) {
2405 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2408 if (name_types.count != 1) {
2409 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2413 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2419 if (!NT_STATUS_IS_OK(status)) {
2422 if (!NT_STATUS_IS_OK(result)) {
2430 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2433 if (!NT_STATUS_IS_OK(status)) {
2436 if (!NT_STATUS_IS_OK(result)) {
2441 /* Display results */
2443 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2444 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2445 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2451 /* Delete domain user */
2453 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2454 TALLOC_CTX *mem_ctx,
2455 int argc, const char **argv)
2457 NTSTATUS status, result;
2458 struct policy_handle connect_pol, domain_pol, user_pol;
2459 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2460 struct dcerpc_binding_handle *b = cli->binding_handle;
2462 if ((argc < 2) || (argc > 3)) {
2463 printf("Usage: %s username\n", argv[0]);
2464 return NT_STATUS_OK;
2468 sscanf(argv[2], "%x", &access_mask);
2470 /* Get sam policy and domain handles */
2472 status = rpccli_try_samr_connects(cli, mem_ctx,
2473 MAXIMUM_ALLOWED_ACCESS,
2475 if (!NT_STATUS_IS_OK(status)) {
2479 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2481 MAXIMUM_ALLOWED_ACCESS,
2485 if (!NT_STATUS_IS_OK(status)) {
2488 if (!NT_STATUS_IS_OK(result)) {
2493 /* Get handle on user */
2496 struct samr_Ids user_rids, name_types;
2497 struct lsa_String lsa_acct_name;
2499 init_lsa_String(&lsa_acct_name, argv[1]);
2501 status = dcerpc_samr_LookupNames(b, mem_ctx,
2508 if (!NT_STATUS_IS_OK(status)) {
2511 if (!NT_STATUS_IS_OK(result)) {
2515 if (user_rids.count != 1) {
2516 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2519 if (name_types.count != 1) {
2520 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2524 status = dcerpc_samr_OpenUser(b, mem_ctx,
2530 if (!NT_STATUS_IS_OK(status)) {
2533 if (!NT_STATUS_IS_OK(result)) {
2541 status = dcerpc_samr_DeleteUser(b, mem_ctx,
2544 if (!NT_STATUS_IS_OK(status)) {
2547 if (!NT_STATUS_IS_OK(result)) {
2552 /* Display results */
2554 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2555 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2556 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2562 /**********************************************************************
2563 * Query user security object
2565 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2566 TALLOC_CTX *mem_ctx,
2567 int argc, const char **argv)
2569 struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2570 NTSTATUS status, result;
2571 uint32_t sec_info = SECINFO_DACL;
2572 uint32_t user_rid = 0;
2573 TALLOC_CTX *ctx = NULL;
2574 struct sec_desc_buf *sec_desc_buf=NULL;
2575 bool domain = False;
2576 struct dcerpc_binding_handle *b = cli->binding_handle;
2578 ctx=talloc_init("cmd_samr_query_sec_obj");
2580 if ((argc < 1) || (argc > 3)) {
2581 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2582 printf("\tSpecify rid for security on user, -d for security on domain\n");
2583 talloc_destroy(ctx);
2584 return NT_STATUS_OK;
2588 if (strcmp(argv[1], "-d") == 0)
2591 sscanf(argv[1], "%i", &user_rid);
2595 sec_info = atoi(argv[2]);
2598 status = rpccli_try_samr_connects(cli, mem_ctx,
2599 MAXIMUM_ALLOWED_ACCESS,
2601 if (!NT_STATUS_IS_OK(status)) {
2605 if (domain || user_rid) {
2606 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2608 MAXIMUM_ALLOWED_ACCESS,
2612 if (!NT_STATUS_IS_OK(status)) {
2615 if (!NT_STATUS_IS_OK(result)) {
2622 status = dcerpc_samr_OpenUser(b, mem_ctx,
2624 MAXIMUM_ALLOWED_ACCESS,
2628 if (!NT_STATUS_IS_OK(status)) {
2631 if (!NT_STATUS_IS_OK(result)) {
2637 /* Pick which query pol to use */
2647 /* Query SAM security object */
2649 status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2654 if (!NT_STATUS_IS_OK(status)) {
2657 if (!NT_STATUS_IS_OK(result)) {
2662 display_sec_desc(sec_desc_buf->sd);
2664 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2665 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2666 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2668 talloc_destroy(ctx);
2672 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2673 TALLOC_CTX *mem_ctx,
2674 int argc, const char **argv)
2676 NTSTATUS status, result;
2677 struct policy_handle connect_pol, domain_pol, user_pol;
2678 struct samr_PwInfo info;
2680 struct dcerpc_binding_handle *b = cli->binding_handle;
2683 printf("Usage: %s rid\n", argv[0]);
2684 return NT_STATUS_OK;
2687 sscanf(argv[1], "%i", &rid);
2689 status = rpccli_try_samr_connects(cli, mem_ctx,
2690 MAXIMUM_ALLOWED_ACCESS,
2692 if (!NT_STATUS_IS_OK(status)) {
2696 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2698 MAXIMUM_ALLOWED_ACCESS,
2702 if (!NT_STATUS_IS_OK(status)) {
2705 if (!NT_STATUS_IS_OK(result)) {
2710 status = dcerpc_samr_OpenUser(b, mem_ctx,
2712 MAXIMUM_ALLOWED_ACCESS,
2716 if (!NT_STATUS_IS_OK(status)) {
2719 if (!NT_STATUS_IS_OK(result)) {
2724 status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2728 if (!NT_STATUS_IS_OK(status)) {
2732 if (NT_STATUS_IS_OK(result)) {
2734 NDR_PRINT_STRUCT_STRING(mem_ctx,
2735 samr_PwInfo, &info));
2739 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2740 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2741 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2746 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2747 TALLOC_CTX *mem_ctx,
2748 int argc, const char **argv)
2750 NTSTATUS status, result;
2751 struct lsa_String domain_name;
2752 struct samr_PwInfo info;
2753 struct dcerpc_binding_handle *b = cli->binding_handle;
2755 if (argc < 1 || argc > 3) {
2756 printf("Usage: %s <domain>\n", argv[0]);
2757 return NT_STATUS_OK;
2760 init_lsa_String(&domain_name, argv[1]);
2762 status = dcerpc_samr_GetDomPwInfo(b, mem_ctx,
2766 if (!NT_STATUS_IS_OK(status)) {
2769 if (NT_STATUS_IS_OK(result)) {
2770 printf("min_password_length: %d\n", info.min_password_length);
2771 display_password_properties(info.password_properties);
2777 /* Look up domain name */
2779 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2780 TALLOC_CTX *mem_ctx,
2781 int argc, const char **argv)
2783 struct policy_handle connect_pol, domain_pol;
2784 NTSTATUS status, result;
2785 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2786 struct lsa_String domain_name;
2787 struct dom_sid *sid = NULL;
2788 struct dcerpc_binding_handle *b = cli->binding_handle;
2791 printf("Usage: %s domain_name\n", argv[0]);
2792 return NT_STATUS_OK;
2795 init_lsa_String(&domain_name, argv[1]);
2797 status = rpccli_try_samr_connects(cli, mem_ctx,
2800 if (!NT_STATUS_IS_OK(status)) {
2804 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2810 if (!NT_STATUS_IS_OK(status)) {
2813 if (!NT_STATUS_IS_OK(result)) {
2818 status = dcerpc_samr_LookupDomain(b, mem_ctx,
2823 if (!NT_STATUS_IS_OK(status)) {
2826 if (!NT_STATUS_IS_OK(result)) {
2831 if (NT_STATUS_IS_OK(result)) {
2832 struct dom_sid_buf sid_str;
2833 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2835 dom_sid_str_buf(sid, &sid_str));
2838 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2839 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2844 /* Change user password */
2846 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2847 TALLOC_CTX *mem_ctx,
2848 int argc, const char **argv)
2850 struct policy_handle connect_pol;
2851 struct policy_handle domain_pol = { 0, };
2852 struct policy_handle user_pol = { 0, };
2853 NTSTATUS status, result;
2854 const char *user, *oldpass, *newpass;
2855 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2856 struct samr_Ids rids, types;
2857 struct lsa_String lsa_acct_name;
2858 struct dcerpc_binding_handle *b = cli->binding_handle;
2861 printf("Usage: %s username oldpass newpass\n", argv[0]);
2862 return NT_STATUS_INVALID_PARAMETER;
2869 /* Get sam policy handle */
2871 status = rpccli_try_samr_connects(cli, mem_ctx,
2872 MAXIMUM_ALLOWED_ACCESS,
2874 if (!NT_STATUS_IS_OK(status)) {
2878 /* Get domain policy handle */
2880 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2886 if (!NT_STATUS_IS_OK(status)) {
2889 if (!NT_STATUS_IS_OK(result)) {
2894 init_lsa_String(&lsa_acct_name, user);
2896 status = dcerpc_samr_LookupNames(b, mem_ctx,
2903 if (!NT_STATUS_IS_OK(status)) {
2906 if (!NT_STATUS_IS_OK(result)) {
2910 if (rids.count != 1) {
2911 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2914 if (types.count != 1) {
2915 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2919 status = dcerpc_samr_OpenUser(b, mem_ctx,
2925 if (!NT_STATUS_IS_OK(status)) {
2928 if (!NT_STATUS_IS_OK(result)) {
2933 /* Change user password */
2934 status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2938 if (!NT_STATUS_IS_OK(status)) {
2943 if (is_valid_policy_hnd(&user_pol)) {
2944 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2946 if (is_valid_policy_hnd(&domain_pol)) {
2947 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2949 if (is_valid_policy_hnd(&connect_pol)) {
2950 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2957 /* Change user password */
2959 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2960 TALLOC_CTX *mem_ctx,
2961 int argc, const char **argv)
2963 struct policy_handle connect_pol, domain_pol;
2964 NTSTATUS status, result;
2965 const char *user, *oldpass, *newpass;
2966 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2967 struct dcerpc_binding_handle *b = cli->binding_handle;
2970 printf("Usage: %s username oldpass newpass\n", argv[0]);
2971 return NT_STATUS_INVALID_PARAMETER;
2978 /* Get sam policy handle */
2980 status = rpccli_try_samr_connects(cli, mem_ctx,
2981 MAXIMUM_ALLOWED_ACCESS,
2983 if (!NT_STATUS_IS_OK(status)) {
2987 /* Get domain policy handle */
2989 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2995 if (!NT_STATUS_IS_OK(status)) {
2998 if (!NT_STATUS_IS_OK(result)) {
3003 /* Change user password */
3004 status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
3006 if (!NT_STATUS_IS_OK(status)) {
3010 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3011 if (!NT_STATUS_IS_OK(status)) goto done;
3013 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
3014 if (!NT_STATUS_IS_OK(status)) goto done;
3021 /* Change user password */
3023 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
3024 TALLOC_CTX *mem_ctx,
3025 int argc, const char **argv)
3027 struct policy_handle connect_pol, domain_pol;
3028 NTSTATUS status, result;
3029 const char *user, *oldpass, *newpass;
3030 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
3031 struct samr_DomInfo1 *info = NULL;
3032 struct userPwdChangeFailureInformation *reject = NULL;
3033 struct dcerpc_binding_handle *b = cli->binding_handle;
3036 printf("Usage: %s username oldpass newpass\n", argv[0]);
3037 return NT_STATUS_INVALID_PARAMETER;
3044 /* Get sam policy handle */
3046 status = rpccli_try_samr_connects(cli, mem_ctx,
3047 MAXIMUM_ALLOWED_ACCESS,
3049 if (!NT_STATUS_IS_OK(status)) {
3053 /* Get domain policy handle */
3055 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3061 if (!NT_STATUS_IS_OK(status)) {
3064 if (!NT_STATUS_IS_OK(result)) {
3069 /* Change user password */
3070 status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
3076 if (!NT_STATUS_IS_OK(status)) {
3080 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
3082 display_sam_dom_info_1(info);
3084 switch (reject->extendedFailureReason) {
3085 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
3086 d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
3088 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
3089 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
3091 case SAM_PWD_CHANGE_NOT_COMPLEX:
3092 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
3095 d_printf("unknown reject reason: %d\n",
3096 reject->extendedFailureReason);
3101 if (!NT_STATUS_IS_OK(result)) {
3106 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3107 if (!NT_STATUS_IS_OK(status)) goto done;
3109 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
3110 if (!NT_STATUS_IS_OK(status)) goto done;
3116 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
3117 TALLOC_CTX *mem_ctx,
3118 int argc, const char **argv,
3121 struct policy_handle connect_pol, domain_pol, user_pol;
3122 NTSTATUS status, result;
3123 const char *user, *param;
3124 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
3127 union samr_UserInfo info;
3128 struct samr_CryptPassword pwd_buf;
3129 struct samr_CryptPasswordEx pwd_buf_ex;
3130 uint8_t nt_hash[16];
3131 uint8_t lm_hash[16];
3132 DATA_BLOB session_key;
3133 uint8_t password_expired = 0;
3134 struct dcerpc_binding_handle *b = cli->binding_handle;
3135 TALLOC_CTX *frame = NULL;
3138 printf("Usage: %s username level password [password_expired]\n",
3140 return NT_STATUS_INVALID_PARAMETER;
3143 frame = talloc_stackframe();
3146 level = atoi(argv[2]);
3150 password_expired = atoi(argv[4]);
3153 status = cli_get_session_key(frame, cli, &session_key);
3154 if (!NT_STATUS_IS_OK(status)) {
3158 status = init_samr_CryptPassword(param, &session_key, &pwd_buf);
3159 if (!NT_STATUS_IS_OK(status)) {
3162 status = init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
3163 if (!NT_STATUS_IS_OK(status)) {
3166 nt_lm_owf_gen(param, nt_hash, lm_hash);
3172 in = data_blob_const(nt_hash, 16);
3173 out = data_blob_talloc_zero(frame, 16);
3174 if (out.data == NULL) {
3175 status = NT_STATUS_NO_MEMORY;
3178 sess_crypt_blob(&out, &in, &session_key, true);
3179 memcpy(nt_hash, out.data, out.length);
3183 in = data_blob_const(lm_hash, 16);
3184 out = data_blob_talloc_zero(frame, 15);
3185 if (out.data == NULL) {
3186 status = NT_STATUS_NO_MEMORY;
3189 sess_crypt_blob(&out, &in, &session_key, true);
3190 memcpy(lm_hash, out.data, out.length);
3193 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
3194 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
3195 info.info18.nt_pwd_active = true;
3196 info.info18.lm_pwd_active = true;
3197 info.info18.password_expired = password_expired;
3201 ZERO_STRUCT(info.info21);
3203 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3204 SAMR_FIELD_LM_PASSWORD_PRESENT;
3206 info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3207 info.info21.password_expired = password_expired;
3210 info.info21.lm_password_set = true;
3211 info.info21.lm_owf_password.length = 16;
3212 info.info21.lm_owf_password.size = 16;
3214 info.info21.nt_password_set = true;
3215 info.info21.nt_owf_password.length = 16;
3216 info.info21.nt_owf_password.size = 16;
3220 in = data_blob_const(nt_hash, 16);
3221 out = data_blob_talloc_zero(frame, 16);
3222 if (out.data == NULL) {
3223 status = NT_STATUS_NO_MEMORY;
3226 sess_crypt_blob(&out, &in, &session_key, true);
3227 info.info21.nt_owf_password.array =
3228 (uint16_t *)talloc_memdup(frame, out.data, 16);
3232 in = data_blob_const(lm_hash, 16);
3233 out = data_blob_talloc_zero(frame, 16);
3234 sess_crypt_blob(&out, &in, &session_key, true);
3235 info.info21.lm_owf_password.array =
3236 (uint16_t *)talloc_memdup(frame, out.data, 16);
3237 if (out.data == NULL) {
3238 status = NT_STATUS_NO_MEMORY;
3245 ZERO_STRUCT(info.info23);
3247 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3248 SAMR_FIELD_LM_PASSWORD_PRESENT;
3250 info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3251 info.info23.info.password_expired = password_expired;
3254 info.info23.password = pwd_buf;
3258 info.info24.password = pwd_buf;
3259 info.info24.password_expired = password_expired;
3263 ZERO_STRUCT(info.info25);
3265 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3266 SAMR_FIELD_LM_PASSWORD_PRESENT;
3268 info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3269 info.info25.info.password_expired = password_expired;
3272 info.info25.password = pwd_buf_ex;
3276 info.info26.password = pwd_buf_ex;
3277 info.info26.password_expired = password_expired;
3281 return NT_STATUS_INVALID_INFO_CLASS;
3284 /* Get sam policy handle */
3286 status = rpccli_try_samr_connects(cli, frame,
3287 MAXIMUM_ALLOWED_ACCESS,
3289 if (!NT_STATUS_IS_OK(status)) {
3293 /* Get domain policy handle */
3295 status = dcerpc_samr_OpenDomain(b, frame,
3302 if (!NT_STATUS_IS_OK(status))
3304 if (!NT_STATUS_IS_OK(result)) {
3309 user_rid = strtol(user, NULL, 0);
3311 status = dcerpc_samr_OpenUser(b, frame,
3317 if (!NT_STATUS_IS_OK(status)) {
3324 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3327 /* Probably this was a user name, try lookupnames */
3328 struct samr_Ids rids, types;
3329 struct lsa_String lsa_acct_name;
3331 init_lsa_String(&lsa_acct_name, user);
3333 status = dcerpc_samr_LookupNames(b, frame,
3340 if (!NT_STATUS_IS_OK(status)) {
3343 if (!NT_STATUS_IS_OK(result)) {
3346 if (rids.count != 1) {
3347 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3349 if (types.count != 1) {
3350 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3353 status = dcerpc_samr_OpenUser(b, frame,
3359 if (!NT_STATUS_IS_OK(status)) {
3362 if (!NT_STATUS_IS_OK(result)) {
3368 case NDR_SAMR_SETUSERINFO:
3369 status = dcerpc_samr_SetUserInfo(b, frame,
3375 case NDR_SAMR_SETUSERINFO2:
3376 status = dcerpc_samr_SetUserInfo2(b, frame,
3383 return NT_STATUS_INVALID_PARAMETER;
3385 if (!NT_STATUS_IS_OK(status)) {
3386 DEBUG(0,("status: %s\n", nt_errstr(status)));
3389 if (!NT_STATUS_IS_OK(result)) {
3391 DEBUG(0,("result: %s\n", nt_errstr(status)));
3395 status = NT_STATUS_OK;
3401 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
3402 TALLOC_CTX *mem_ctx,
3403 int argc, const char **argv)
3405 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3406 NDR_SAMR_SETUSERINFO);
3409 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
3410 TALLOC_CTX *mem_ctx,
3411 int argc, const char **argv)
3413 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3414 NDR_SAMR_SETUSERINFO2);
3417 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
3418 TALLOC_CTX *mem_ctx,
3419 int argc, const char **argv)
3421 NTSTATUS status, result;
3422 struct policy_handle connect_handle;
3423 struct policy_handle domain_handle = { 0, };
3425 struct lsa_String name;
3427 struct dcerpc_binding_handle *b = cli->binding_handle;
3429 if (argc < 2 || argc > 3) {
3430 printf("Usage: %s name level\n", argv[0]);
3431 return NT_STATUS_INVALID_PARAMETER;
3434 init_lsa_String(&name, argv[1]);
3437 level = atoi(argv[2]);
3440 status = rpccli_try_samr_connects(cli, mem_ctx,
3441 SEC_FLAG_MAXIMUM_ALLOWED,
3443 if (!NT_STATUS_IS_OK(status)) {
3447 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3449 SEC_FLAG_MAXIMUM_ALLOWED,
3453 if (!NT_STATUS_IS_OK(status)) {
3456 if (!NT_STATUS_IS_OK(result)) {
3461 status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3467 if (!NT_STATUS_IS_OK(status)) {
3473 if (NT_STATUS_IS_OK(status) ||
3474 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
3475 printf("idx: %d (0x%08x)\n", idx, idx);
3479 if (is_valid_policy_hnd(&domain_handle)) {
3480 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
3482 if (is_valid_policy_hnd(&connect_handle)) {
3483 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
3489 /* List of commands exported by this module */
3491 struct cmd_set samr_commands[] = {
3498 .name = "queryuser",
3499 .returntype = RPC_RTYPE_NTSTATUS,
3500 .ntfn = cmd_samr_query_user,
3502 .table = &ndr_table_samr,
3504 .description = "Query user info",
3508 .name = "querygroup",
3509 .returntype = RPC_RTYPE_NTSTATUS,
3510 .ntfn = cmd_samr_query_group,
3512 .table = &ndr_table_samr,
3514 .description = "Query group info",
3518 .name = "queryusergroups",
3519 .returntype = RPC_RTYPE_NTSTATUS,
3520 .ntfn = cmd_samr_query_usergroups,
3522 .table = &ndr_table_samr,
3524 .description = "Query user groups",
3528 .name = "queryuseraliases",
3529 .returntype = RPC_RTYPE_NTSTATUS,
3530 .ntfn = cmd_samr_query_useraliases,
3532 .table = &ndr_table_samr,
3534 .description = "Query user aliases",
3538 .name = "querygroupmem",
3539 .returntype = RPC_RTYPE_NTSTATUS,
3540 .ntfn = cmd_samr_query_groupmem,
3542 .table = &ndr_table_samr,
3544 .description = "Query group membership",
3548 .name = "queryaliasmem",
3549 .returntype = RPC_RTYPE_NTSTATUS,
3550 .ntfn = cmd_samr_query_aliasmem,
3552 .table = &ndr_table_samr,
3554 .description = "Query alias membership",
3558 .name = "queryaliasinfo",
3559 .returntype = RPC_RTYPE_NTSTATUS,
3560 .ntfn = cmd_samr_query_aliasinfo,
3562 .table = &ndr_table_samr,
3564 .description = "Query alias info",
3568 .name = "deletealias",
3569 .returntype = RPC_RTYPE_NTSTATUS,
3570 .ntfn = cmd_samr_delete_alias,
3572 .table = &ndr_table_samr,
3574 .description = "Delete an alias",
3578 .name = "querydispinfo",
3579 .returntype = RPC_RTYPE_NTSTATUS,
3580 .ntfn = cmd_samr_query_dispinfo,
3582 .table = &ndr_table_samr,
3584 .description = "Query display info",
3588 .name = "querydispinfo2",
3589 .returntype = RPC_RTYPE_NTSTATUS,
3590 .ntfn = cmd_samr_query_dispinfo2,
3592 .table = &ndr_table_samr,
3594 .description = "Query display info",
3598 .name = "querydispinfo3",
3599 .returntype = RPC_RTYPE_NTSTATUS,
3600 .ntfn = cmd_samr_query_dispinfo3,
3602 .table = &ndr_table_samr,
3604 .description = "Query display info",
3608 .name = "querydominfo",
3609 .returntype = RPC_RTYPE_NTSTATUS,
3610 .ntfn = cmd_samr_query_dominfo,
3612 .table = &ndr_table_samr,
3614 .description = "Query domain info",
3618 .name = "enumdomusers",
3619 .returntype = RPC_RTYPE_NTSTATUS,
3620 .ntfn = cmd_samr_enum_dom_users,
3622 .table = &ndr_table_samr,
3624 .description = "Enumerate domain users",
3628 .name = "enumdomgroups",
3629 .returntype = RPC_RTYPE_NTSTATUS,
3630 .ntfn = cmd_samr_enum_dom_groups,
3632 .table = &ndr_table_samr,
3634 .description = "Enumerate domain groups",
3638 .name = "enumalsgroups",
3639 .returntype = RPC_RTYPE_NTSTATUS,
3640 .ntfn = cmd_samr_enum_als_groups,
3642 .table = &ndr_table_samr,
3644 .description = "Enumerate alias groups",
3648 .name = "enumdomains",
3649 .returntype = RPC_RTYPE_NTSTATUS,
3650 .ntfn = cmd_samr_enum_domains,
3652 .table = &ndr_table_samr,
3654 .description = "Enumerate domains",
3659 .name = "createdomuser",
3660 .returntype = RPC_RTYPE_NTSTATUS,
3661 .ntfn = cmd_samr_create_dom_user,
3663 .table = &ndr_table_samr,
3665 .description = "Create domain user",
3669 .name = "createdomgroup",
3670 .returntype = RPC_RTYPE_NTSTATUS,
3671 .ntfn = cmd_samr_create_dom_group,
3673 .table = &ndr_table_samr,
3675 .description = "Create domain group",
3679 .name = "createdomalias",
3680 .returntype = RPC_RTYPE_NTSTATUS,
3681 .ntfn = cmd_samr_create_dom_alias,
3683 .table = &ndr_table_samr,
3685 .description = "Create domain alias",
3689 .name = "samlookupnames",
3690 .returntype = RPC_RTYPE_NTSTATUS,
3691 .ntfn = cmd_samr_lookup_names,
3693 .table = &ndr_table_samr,
3695 .description = "Look up names",
3699 .name = "samlookuprids",
3700 .returntype = RPC_RTYPE_NTSTATUS,
3701 .ntfn = cmd_samr_lookup_rids,
3703 .table = &ndr_table_samr,
3705 .description = "Look up names",
3709 .name = "deletedomgroup",
3710 .returntype = RPC_RTYPE_NTSTATUS,
3711 .ntfn = cmd_samr_delete_dom_group,
3713 .table = &ndr_table_samr,
3715 .description = "Delete domain group",
3719 .name = "deletedomuser",
3720 .returntype = RPC_RTYPE_NTSTATUS,
3721 .ntfn = cmd_samr_delete_dom_user,
3723 .table = &ndr_table_samr,
3725 .description = "Delete domain user",
3729 .name = "samquerysecobj",
3730 .returntype = RPC_RTYPE_NTSTATUS,
3731 .ntfn = cmd_samr_query_sec_obj,
3733 .table = &ndr_table_samr,
3735 .description = "Query SAMR security object",
3739 .name = "getdompwinfo",
3740 .returntype = RPC_RTYPE_NTSTATUS,
3741 .ntfn = cmd_samr_get_dom_pwinfo,
3743 .table = &ndr_table_samr,
3745 .description = "Retrieve domain password info",
3749 .name = "getusrdompwinfo",
3750 .returntype = RPC_RTYPE_NTSTATUS,
3751 .ntfn = cmd_samr_get_usrdom_pwinfo,
3753 .table = &ndr_table_samr,
3755 .description = "Retrieve user domain password info",
3760 .name = "lookupdomain",
3761 .returntype = RPC_RTYPE_NTSTATUS,
3762 .ntfn = cmd_samr_lookup_domain,
3764 .table = &ndr_table_samr,
3766 .description = "Lookup Domain Name",
3770 .name = "chgpasswd",
3771 .returntype = RPC_RTYPE_NTSTATUS,
3772 .ntfn = cmd_samr_chgpasswd,
3774 .table = &ndr_table_samr,
3776 .description = "Change user password",
3780 .name = "chgpasswd2",
3781 .returntype = RPC_RTYPE_NTSTATUS,
3782 .ntfn = cmd_samr_chgpasswd2,
3784 .table = &ndr_table_samr,
3786 .description = "Change user password",
3790 .name = "chgpasswd3",
3791 .returntype = RPC_RTYPE_NTSTATUS,
3792 .ntfn = cmd_samr_chgpasswd3,
3794 .table = &ndr_table_samr,
3796 .description = "Change user password",
3800 .name = "getdispinfoidx",
3801 .returntype = RPC_RTYPE_NTSTATUS,
3802 .ntfn = cmd_samr_get_dispinfo_idx,
3804 .table = &ndr_table_samr,
3806 .description = "Get Display Information Index",
3810 .name = "setuserinfo",
3811 .returntype = RPC_RTYPE_NTSTATUS,
3812 .ntfn = cmd_samr_setuserinfo,
3814 .table = &ndr_table_samr,
3816 .description = "Set user info",
3820 .name = "setuserinfo2",
3821 .returntype = RPC_RTYPE_NTSTATUS,
3822 .ntfn = cmd_samr_setuserinfo2,
3824 .table = &ndr_table_samr,
3826 .description = "Set user info2",