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 extern 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 /****************************************************************************
274 ****************************************************************************/
276 static NTSTATUS get_domain_handle(struct rpc_pipe_client *cli,
279 struct policy_handle *connect_pol,
280 uint32_t access_mask,
281 struct dom_sid *_domain_sid,
282 struct policy_handle *domain_pol)
284 struct dcerpc_binding_handle *b = cli->binding_handle;
285 NTSTATUS status = NT_STATUS_INVALID_PARAMETER, result;
287 if (strcasecmp_m(sam, "domain") == 0) {
288 status = dcerpc_samr_OpenDomain(b, mem_ctx,
294 } else if (strcasecmp_m(sam, "builtin") == 0) {
295 status = dcerpc_samr_OpenDomain(b, mem_ctx,
298 discard_const_p(struct dom_sid2, &global_sid_Builtin),
303 if (!NT_STATUS_IS_OK(status)) {
310 /**********************************************************************
311 * Query user information
313 static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
315 int argc, const char **argv)
317 struct policy_handle connect_pol, domain_pol, user_pol;
318 NTSTATUS status, result;
319 uint32 info_level = 21;
320 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
321 union samr_UserInfo *info = NULL;
323 struct dcerpc_binding_handle *b = cli->binding_handle;
325 if ((argc < 2) || (argc > 4)) {
326 printf("Usage: %s rid [info level] [access mask] \n", argv[0]);
330 sscanf(argv[1], "%i", &user_rid);
333 sscanf(argv[2], "%i", &info_level);
336 sscanf(argv[3], "%x", &access_mask);
339 status = rpccli_try_samr_connects(cli, mem_ctx,
340 MAXIMUM_ALLOWED_ACCESS,
342 if (!NT_STATUS_IS_OK(status)) {
346 status = dcerpc_samr_OpenDomain(b, mem_ctx,
348 MAXIMUM_ALLOWED_ACCESS,
352 if (!NT_STATUS_IS_OK(status)) {
355 if (!NT_STATUS_IS_OK(result)) {
360 status = dcerpc_samr_OpenUser(b, mem_ctx,
366 if (!NT_STATUS_IS_OK(status)) {
369 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) &&
372 /* Probably this was a user name, try lookupnames */
373 struct samr_Ids rids, types;
374 struct lsa_String lsa_acct_name;
376 init_lsa_String(&lsa_acct_name, argv[1]);
378 status = dcerpc_samr_LookupNames(b, mem_ctx,
385 if (!NT_STATUS_IS_OK(status)) {
388 if (NT_STATUS_IS_OK(result)) {
389 status = dcerpc_samr_OpenUser(b, mem_ctx,
395 if (!NT_STATUS_IS_OK(status)) {
402 if (!NT_STATUS_IS_OK(result)) {
407 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
412 if (!NT_STATUS_IS_OK(status)) {
415 if (!NT_STATUS_IS_OK(result)) {
420 switch (info_level) {
422 display_samr_user_info_7(&info->info7);
425 display_samr_user_info_9(&info->info9);
428 display_samr_user_info_16(&info->info16);
431 display_samr_user_info_20(&info->info20);
434 display_samr_user_info_21(&info->info21);
437 printf("Unsupported infolevel: %d\n", info_level);
441 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
442 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
443 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
449 /****************************************************************************
451 ****************************************************************************/
452 static void display_group_info1(struct samr_GroupInfoAll *info1)
454 printf("\tGroup Name:\t%s\n", info1->name.string);
455 printf("\tDescription:\t%s\n", info1->description.string);
456 printf("\tGroup Attribute:%d\n", info1->attributes);
457 printf("\tNum Members:%d\n", info1->num_members);
460 /****************************************************************************
462 ****************************************************************************/
463 static void display_group_info2(struct lsa_String *info2)
465 printf("\tGroup Description:%s\n", info2->string);
469 /****************************************************************************
471 ****************************************************************************/
472 static void display_group_info3(struct samr_GroupInfoAttributes *info3)
474 printf("\tGroup Attribute:%d\n", info3->attributes);
478 /****************************************************************************
480 ****************************************************************************/
481 static void display_group_info4(struct lsa_String *info4)
483 printf("\tGroup Description:%s\n", info4->string);
486 /****************************************************************************
488 ****************************************************************************/
489 static void display_group_info5(struct samr_GroupInfoAll *info5)
491 printf("\tGroup Name:\t%s\n", info5->name.string);
492 printf("\tDescription:\t%s\n", info5->description.string);
493 printf("\tGroup Attribute:%d\n", info5->attributes);
494 printf("\tNum Members:%d\n", info5->num_members);
497 /****************************************************************************
498 display sam sync structure
499 ****************************************************************************/
500 static void display_group_info(union samr_GroupInfo *info,
501 enum samr_GroupInfoEnum level)
505 display_group_info1(&info->all);
508 display_group_info2(&info->name);
511 display_group_info3(&info->attributes);
514 display_group_info4(&info->description);
517 display_group_info5(&info->all2);
522 /***********************************************************************
523 * Query group information
525 static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
527 int argc, const char **argv)
529 struct policy_handle connect_pol, domain_pol, group_pol;
530 NTSTATUS status, result;
531 enum samr_GroupInfoEnum info_level = GROUPINFOALL;
532 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
533 union samr_GroupInfo *group_info = NULL;
535 struct dcerpc_binding_handle *b = cli->binding_handle;
537 if ((argc < 2) || (argc > 4)) {
538 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
542 sscanf(argv[1], "%i", &group_rid);
545 info_level = atoi(argv[2]);
548 sscanf(argv[3], "%x", &access_mask);
550 status = rpccli_try_samr_connects(cli, mem_ctx,
551 MAXIMUM_ALLOWED_ACCESS,
553 if (!NT_STATUS_IS_OK(status)) {
557 status = dcerpc_samr_OpenDomain(b, mem_ctx,
559 MAXIMUM_ALLOWED_ACCESS,
563 if (!NT_STATUS_IS_OK(status)) {
566 if (!NT_STATUS_IS_OK(result)) {
571 status = dcerpc_samr_OpenGroup(b, mem_ctx,
577 if (!NT_STATUS_IS_OK(status)) {
580 if (!NT_STATUS_IS_OK(result)) {
585 status = dcerpc_samr_QueryGroupInfo(b, mem_ctx,
590 if (!NT_STATUS_IS_OK(status)) {
593 if (!NT_STATUS_IS_OK(result)) {
598 display_group_info(group_info, info_level);
600 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
601 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
602 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
607 /* Query groups a user is a member of */
609 static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
611 int argc, const char **argv)
613 struct policy_handle connect_pol,
616 NTSTATUS status, result;
618 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
620 struct samr_RidWithAttributeArray *rid_array = NULL;
621 struct dcerpc_binding_handle *b = cli->binding_handle;
623 if ((argc < 2) || (argc > 3)) {
624 printf("Usage: %s rid [access mask]\n", argv[0]);
628 sscanf(argv[1], "%i", &user_rid);
631 sscanf(argv[2], "%x", &access_mask);
633 status = rpccli_try_samr_connects(cli, mem_ctx,
634 MAXIMUM_ALLOWED_ACCESS,
636 if (!NT_STATUS_IS_OK(status)) {
640 status = dcerpc_samr_OpenDomain(b, mem_ctx,
642 MAXIMUM_ALLOWED_ACCESS,
646 if (!NT_STATUS_IS_OK(status)) {
649 if (!NT_STATUS_IS_OK(result)) {
654 status = dcerpc_samr_OpenUser(b, mem_ctx,
661 if (!NT_STATUS_IS_OK(status)) {
664 if (!NT_STATUS_IS_OK(result)) {
669 status = dcerpc_samr_GetGroupsForUser(b, mem_ctx,
673 if (!NT_STATUS_IS_OK(status)) {
676 if (!NT_STATUS_IS_OK(result)) {
681 for (i = 0; i < rid_array->count; i++) {
682 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
683 rid_array->rids[i].rid,
684 rid_array->rids[i].attributes);
687 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
688 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
689 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
694 /* Query aliases a user is a member of */
696 static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
698 int argc, const char **argv)
700 struct policy_handle connect_pol, domain_pol;
701 NTSTATUS status, result;
702 struct dom_sid *sids;
704 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
706 struct lsa_SidArray sid_array;
707 struct samr_Ids alias_rids;
708 struct dcerpc_binding_handle *b = cli->binding_handle;
711 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
712 return NT_STATUS_INVALID_PARAMETER;
718 for (i=2; i<argc; i++) {
719 struct dom_sid tmp_sid;
720 if (!string_to_sid(&tmp_sid, argv[i])) {
721 printf("%s is not a legal SID\n", argv[i]);
722 return NT_STATUS_INVALID_PARAMETER;
724 result = add_sid_to_array(mem_ctx, &tmp_sid, &sids, &num_sids);
725 if (!NT_STATUS_IS_OK(result)) {
731 sid_array.sids = TALLOC_ZERO_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
732 if (sid_array.sids == NULL)
733 return NT_STATUS_NO_MEMORY;
735 sid_array.sids = NULL;
738 for (i=0; i<num_sids; i++) {
739 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
740 if (!sid_array.sids[i].sid) {
741 return NT_STATUS_NO_MEMORY;
745 sid_array.num_sids = num_sids;
747 status = rpccli_try_samr_connects(cli, mem_ctx,
748 MAXIMUM_ALLOWED_ACCESS,
750 if (!NT_STATUS_IS_OK(status)) {
754 status = get_domain_handle(cli, mem_ctx, argv[1],
759 if (!NT_STATUS_IS_OK(status)) {
763 status = dcerpc_samr_GetAliasMembership(b, mem_ctx,
768 if (!NT_STATUS_IS_OK(status)) {
771 if (!NT_STATUS_IS_OK(result)) {
776 for (i = 0; i < alias_rids.count; i++) {
777 printf("\tgroup rid:[0x%x]\n", alias_rids.ids[i]);
780 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
781 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
786 /* Query members of a group */
788 static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
790 int argc, const char **argv)
792 struct policy_handle connect_pol, domain_pol, group_pol;
793 NTSTATUS status, result;
795 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
797 unsigned int old_timeout;
798 struct samr_RidAttrArray *rids = NULL;
799 struct dcerpc_binding_handle *b = cli->binding_handle;
801 if ((argc < 2) || (argc > 3)) {
802 printf("Usage: %s rid [access mask]\n", argv[0]);
806 sscanf(argv[1], "%i", &group_rid);
809 sscanf(argv[2], "%x", &access_mask);
811 status = rpccli_try_samr_connects(cli, mem_ctx,
812 MAXIMUM_ALLOWED_ACCESS,
814 if (!NT_STATUS_IS_OK(status)) {
818 status = dcerpc_samr_OpenDomain(b, mem_ctx,
820 MAXIMUM_ALLOWED_ACCESS,
824 if (!NT_STATUS_IS_OK(status)) {
827 if (!NT_STATUS_IS_OK(result)) {
832 status = dcerpc_samr_OpenGroup(b, mem_ctx,
838 if (!NT_STATUS_IS_OK(status)) {
841 if (!NT_STATUS_IS_OK(result)) {
846 /* Make sure to wait for our DC's reply */
847 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
848 rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
850 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
855 rpccli_set_timeout(cli, old_timeout);
857 if (!NT_STATUS_IS_OK(status)) {
860 if (!NT_STATUS_IS_OK(result)) {
865 for (i = 0; i < rids->count; i++) {
866 printf("\trid:[0x%x] attr:[0x%x]\n", rids->rids[i],
867 rids->attributes[i]);
870 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
871 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
872 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
877 /* Enumerate domain users */
879 static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
881 int argc, const char **argv)
883 struct policy_handle connect_pol, domain_pol;
884 NTSTATUS status, result;
885 uint32 start_idx, num_dom_users, i;
886 struct samr_SamArray *dom_users = NULL;
887 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
888 uint32 acb_mask = ACB_NORMAL;
889 uint32_t size = 0xffff;
890 struct dcerpc_binding_handle *b = cli->binding_handle;
892 if ((argc < 1) || (argc > 4)) {
893 printf("Usage: %s [access_mask] [acb_mask] [size]\n", argv[0]);
898 sscanf(argv[1], "%x", &access_mask);
902 sscanf(argv[2], "%x", &acb_mask);
906 sscanf(argv[3], "%x", &size);
909 /* Get sam policy handle */
911 status = rpccli_try_samr_connects(cli, mem_ctx,
912 MAXIMUM_ALLOWED_ACCESS,
914 if (!NT_STATUS_IS_OK(status)) {
918 /* Get domain policy handle */
920 status = get_domain_handle(cli, mem_ctx, "domain",
925 if (!NT_STATUS_IS_OK(status)) {
929 /* Enumerate domain users */
934 status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
942 if (!NT_STATUS_IS_OK(status)) {
945 if (NT_STATUS_IS_OK(result) ||
946 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
948 for (i = 0; i < num_dom_users; i++)
949 printf("user:[%s] rid:[0x%x]\n",
950 dom_users->entries[i].name.string,
951 dom_users->entries[i].idx);
954 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
957 if (is_valid_policy_hnd(&domain_pol))
958 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
960 if (is_valid_policy_hnd(&connect_pol))
961 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
966 /* Enumerate domain groups */
968 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
970 int argc, const char **argv)
972 struct policy_handle connect_pol, domain_pol;
973 NTSTATUS status, result;
974 uint32 start_idx, num_dom_groups, i;
975 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
976 struct samr_SamArray *dom_groups = NULL;
977 uint32_t size = 0xffff;
978 struct dcerpc_binding_handle *b = cli->binding_handle;
980 if ((argc < 1) || (argc > 3)) {
981 printf("Usage: %s [access_mask] [max_size]\n", argv[0]);
986 sscanf(argv[1], "%x", &access_mask);
990 sscanf(argv[2], "%x", &size);
993 /* Get sam policy handle */
995 status = rpccli_try_samr_connects(cli, mem_ctx,
996 MAXIMUM_ALLOWED_ACCESS,
998 if (!NT_STATUS_IS_OK(status)) {
1002 /* Get domain policy handle */
1004 status = get_domain_handle(cli, mem_ctx, "domain",
1009 if (!NT_STATUS_IS_OK(status)) {
1013 /* Enumerate domain groups */
1018 status = dcerpc_samr_EnumDomainGroups(b, mem_ctx,
1025 if (!NT_STATUS_IS_OK(status)) {
1028 if (NT_STATUS_IS_OK(result) ||
1029 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1031 for (i = 0; i < num_dom_groups; i++)
1032 printf("group:[%s] rid:[0x%x]\n",
1033 dom_groups->entries[i].name.string,
1034 dom_groups->entries[i].idx);
1037 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1040 if (is_valid_policy_hnd(&domain_pol))
1041 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1043 if (is_valid_policy_hnd(&connect_pol))
1044 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1049 /* Enumerate alias groups */
1051 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
1052 TALLOC_CTX *mem_ctx,
1053 int argc, const char **argv)
1055 struct policy_handle connect_pol, domain_pol;
1056 NTSTATUS status, result;
1057 uint32 start_idx, num_als_groups, i;
1058 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1059 struct samr_SamArray *als_groups = NULL;
1060 uint32_t size = 0xffff;
1061 struct dcerpc_binding_handle *b = cli->binding_handle;
1063 if ((argc < 2) || (argc > 4)) {
1064 printf("Usage: %s builtin|domain [access mask] [max_size]\n", argv[0]);
1065 return NT_STATUS_OK;
1069 sscanf(argv[2], "%x", &access_mask);
1073 sscanf(argv[3], "%x", &size);
1076 /* Get sam policy handle */
1078 status = rpccli_try_samr_connects(cli, mem_ctx,
1079 MAXIMUM_ALLOWED_ACCESS,
1081 if (!NT_STATUS_IS_OK(status)) {
1085 /* Get domain policy handle */
1087 status = get_domain_handle(cli, mem_ctx, argv[1],
1092 if (!NT_STATUS_IS_OK(status)) {
1096 /* Enumerate alias groups */
1101 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
1108 if (!NT_STATUS_IS_OK(status)) {
1111 if (NT_STATUS_IS_OK(result) ||
1112 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1114 for (i = 0; i < num_als_groups; i++)
1115 printf("group:[%s] rid:[0x%x]\n",
1116 als_groups->entries[i].name.string,
1117 als_groups->entries[i].idx);
1119 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1122 if (is_valid_policy_hnd(&domain_pol))
1123 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1125 if (is_valid_policy_hnd(&connect_pol))
1126 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1131 /* Enumerate domains */
1133 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
1134 TALLOC_CTX *mem_ctx,
1135 int argc, const char **argv)
1137 struct policy_handle connect_pol;
1138 NTSTATUS status, result;
1139 uint32 start_idx, size, num_entries, i;
1140 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1141 struct samr_SamArray *sam = NULL;
1142 struct dcerpc_binding_handle *b = cli->binding_handle;
1144 if ((argc < 1) || (argc > 2)) {
1145 printf("Usage: %s [access mask]\n", argv[0]);
1146 return NT_STATUS_OK;
1150 sscanf(argv[1], "%x", &access_mask);
1153 /* Get sam policy handle */
1155 status = rpccli_try_samr_connects(cli, mem_ctx,
1158 if (!NT_STATUS_IS_OK(status)) {
1162 /* Enumerate alias groups */
1168 status = dcerpc_samr_EnumDomains(b, mem_ctx,
1175 if (!NT_STATUS_IS_OK(status)) {
1178 if (NT_STATUS_IS_OK(result) ||
1179 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1181 for (i = 0; i < num_entries; i++)
1182 printf("name:[%s] idx:[0x%x]\n",
1183 sam->entries[i].name.string,
1184 sam->entries[i].idx);
1186 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1189 if (is_valid_policy_hnd(&connect_pol)) {
1190 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1197 /* Query alias membership */
1199 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1200 TALLOC_CTX *mem_ctx,
1201 int argc, const char **argv)
1203 struct policy_handle connect_pol, domain_pol, alias_pol;
1204 NTSTATUS status, result;
1205 uint32 alias_rid, i;
1206 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1207 struct lsa_SidArray sid_array;
1208 struct dcerpc_binding_handle *b = cli->binding_handle;
1210 if ((argc < 3) || (argc > 4)) {
1211 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1212 return NT_STATUS_OK;
1215 sscanf(argv[2], "%i", &alias_rid);
1218 sscanf(argv[3], "%x", &access_mask);
1220 /* Open SAMR handle */
1222 status = rpccli_try_samr_connects(cli, mem_ctx,
1223 MAXIMUM_ALLOWED_ACCESS,
1225 if (!NT_STATUS_IS_OK(status)) {
1229 /* Open handle on domain */
1231 status = get_domain_handle(cli, mem_ctx, argv[1],
1233 MAXIMUM_ALLOWED_ACCESS,
1236 if (!NT_STATUS_IS_OK(status)) {
1240 /* Open handle on alias */
1242 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1248 if (!NT_STATUS_IS_OK(status)) {
1251 if (!NT_STATUS_IS_OK(result)) {
1256 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
1260 if (!NT_STATUS_IS_OK(status)) {
1263 if (!NT_STATUS_IS_OK(result)) {
1268 for (i = 0; i < sid_array.num_sids; i++) {
1271 sid_to_fstring(sid_str, sid_array.sids[i].sid);
1272 printf("\tsid:[%s]\n", sid_str);
1275 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1276 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1277 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1282 /* Query alias info */
1284 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
1285 TALLOC_CTX *mem_ctx,
1286 int argc, const char **argv)
1288 struct policy_handle connect_pol, domain_pol, alias_pol;
1289 NTSTATUS status, result;
1291 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1292 union samr_AliasInfo *info = NULL;
1293 enum samr_AliasInfoEnum level = ALIASINFOALL;
1294 struct dcerpc_binding_handle *b = cli->binding_handle;
1296 if ((argc < 3) || (argc > 4)) {
1297 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1299 return NT_STATUS_OK;
1302 sscanf(argv[2], "%i", &alias_rid);
1305 level = atoi(argv[3]);
1309 sscanf(argv[4], "%x", &access_mask);
1312 /* Open SAMR handle */
1314 status = rpccli_try_samr_connects(cli, mem_ctx,
1315 SEC_FLAG_MAXIMUM_ALLOWED,
1317 if (!NT_STATUS_IS_OK(status)) {
1321 /* Open handle on domain */
1323 status = get_domain_handle(cli, mem_ctx, argv[1],
1325 SEC_FLAG_MAXIMUM_ALLOWED,
1328 if (!NT_STATUS_IS_OK(status)) {
1332 /* Open handle on alias */
1334 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1340 if (!NT_STATUS_IS_OK(status)) {
1343 if (!NT_STATUS_IS_OK(result)) {
1348 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
1353 if (!NT_STATUS_IS_OK(status)) {
1356 if (!NT_STATUS_IS_OK(result)) {
1363 printf("Name: %s\n", info->all.name.string);
1364 printf("Description: %s\n", info->all.description.string);
1365 printf("Num Members: %d\n", info->all.num_members);
1368 printf("Name: %s\n", info->name.string);
1370 case ALIASINFODESCRIPTION:
1371 printf("Description: %s\n", info->description.string);
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);
1385 /* Query delete an alias membership */
1387 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1388 TALLOC_CTX *mem_ctx,
1389 int argc, const char **argv)
1391 struct policy_handle connect_pol, domain_pol, alias_pol;
1392 NTSTATUS status, result;
1394 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1395 struct dcerpc_binding_handle *b = cli->binding_handle;
1398 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1399 return NT_STATUS_OK;
1402 alias_rid = strtoul(argv[2], NULL, 10);
1404 /* Open SAMR handle */
1406 status = rpccli_try_samr_connects(cli, mem_ctx,
1407 MAXIMUM_ALLOWED_ACCESS,
1409 if (!NT_STATUS_IS_OK(status)) {
1413 /* Open handle on domain */
1415 status = get_domain_handle(cli, mem_ctx, argv[1],
1417 MAXIMUM_ALLOWED_ACCESS,
1420 if (!NT_STATUS_IS_OK(status)) {
1424 /* Open handle on alias */
1426 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1432 if (!NT_STATUS_IS_OK(status)) {
1435 if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1436 /* Probably this was a user name, try lookupnames */
1437 struct samr_Ids rids, types;
1438 struct lsa_String lsa_acct_name;
1440 init_lsa_String(&lsa_acct_name, argv[2]);
1442 status = dcerpc_samr_LookupNames(b, mem_ctx,
1449 if (!NT_STATUS_IS_OK(status)) {
1452 if (NT_STATUS_IS_OK(result)) {
1453 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1459 if (!NT_STATUS_IS_OK(status)) {
1465 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1468 if (!NT_STATUS_IS_OK(status)) {
1471 if (!NT_STATUS_IS_OK(result)) {
1476 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1477 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1482 /* Query display info */
1484 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1485 TALLOC_CTX *mem_ctx,
1486 int argc, const char **argv,
1489 struct policy_handle connect_pol, domain_pol;
1490 NTSTATUS status, result;
1491 uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1492 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1493 uint32 info_level = 1;
1494 union samr_DispInfo info;
1496 bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1497 uint32_t total_size, returned_size;
1498 struct dcerpc_binding_handle *b = cli->binding_handle;
1501 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1502 return NT_STATUS_OK;
1506 sscanf(argv[1], "%i", &info_level);
1509 sscanf(argv[2], "%i", &start_idx);
1512 sscanf(argv[3], "%i", &max_entries);
1517 sscanf(argv[4], "%i", &max_size);
1522 sscanf(argv[5], "%x", &access_mask);
1524 /* Get sam policy handle */
1526 status = rpccli_try_samr_connects(cli, mem_ctx,
1527 MAXIMUM_ALLOWED_ACCESS,
1529 if (!NT_STATUS_IS_OK(status)) {
1533 /* Get domain policy handle */
1535 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1541 if (!NT_STATUS_IS_OK(status)) {
1544 if (!NT_STATUS_IS_OK(result)) {
1549 /* Query display info */
1554 dcerpc_get_query_dispinfo_params(
1555 loop_count, &max_entries, &max_size);
1558 case NDR_SAMR_QUERYDISPLAYINFO:
1559 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1570 case NDR_SAMR_QUERYDISPLAYINFO2:
1571 status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1583 case NDR_SAMR_QUERYDISPLAYINFO3:
1584 status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1597 return NT_STATUS_INVALID_PARAMETER;
1600 if (!NT_STATUS_IS_OK(status)) {
1604 if (!NT_STATUS_IS_OK(result) &&
1605 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1606 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1612 switch (info_level) {
1614 num_entries = info.info1.count;
1617 num_entries = info.info2.count;
1620 num_entries = info.info3.count;
1623 num_entries = info.info4.count;
1626 num_entries = info.info5.count;
1632 start_idx += num_entries;
1634 if (num_entries == 0)
1637 for (i = 0; i < num_entries; i++) {
1638 switch (info_level) {
1640 display_sam_info_1(&info.info1.entries[i]);
1643 display_sam_info_2(&info.info2.entries[i]);
1646 display_sam_info_3(&info.info3.entries[i]);
1649 display_sam_info_4(&info.info4.entries[i]);
1652 display_sam_info_5(&info.info5.entries[i]);
1656 } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1658 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1659 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1664 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1665 TALLOC_CTX *mem_ctx,
1666 int argc, const char **argv)
1668 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1669 NDR_SAMR_QUERYDISPLAYINFO);
1672 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1673 TALLOC_CTX *mem_ctx,
1674 int argc, const char **argv)
1676 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1677 NDR_SAMR_QUERYDISPLAYINFO2);
1680 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1681 TALLOC_CTX *mem_ctx,
1682 int argc, const char **argv)
1684 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1685 NDR_SAMR_QUERYDISPLAYINFO3);
1688 /* Query domain info */
1690 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1691 TALLOC_CTX *mem_ctx,
1692 int argc, const char **argv)
1694 struct policy_handle connect_pol, domain_pol;
1695 NTSTATUS status, result;
1696 uint32 switch_level = 2;
1697 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1698 union samr_DomainInfo *info = NULL;
1699 struct dcerpc_binding_handle *b = cli->binding_handle;
1702 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1703 return NT_STATUS_OK;
1707 sscanf(argv[1], "%i", &switch_level);
1710 sscanf(argv[2], "%x", &access_mask);
1712 /* Get sam policy handle */
1714 status = rpccli_try_samr_connects(cli, mem_ctx,
1715 MAXIMUM_ALLOWED_ACCESS,
1717 if (!NT_STATUS_IS_OK(status)) {
1721 /* Get domain policy handle */
1723 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1729 if (!NT_STATUS_IS_OK(status)) {
1732 if (!NT_STATUS_IS_OK(result)) {
1737 /* Query domain info */
1739 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1744 if (!NT_STATUS_IS_OK(status)) {
1747 if (!NT_STATUS_IS_OK(result)) {
1752 /* Display domain info */
1754 switch (switch_level) {
1756 display_sam_dom_info_1(&info->info1);
1759 display_sam_dom_info_2(&info->general);
1762 display_sam_dom_info_3(&info->info3);
1765 display_sam_dom_info_4(&info->oem);
1768 display_sam_dom_info_5(&info->info5);
1771 display_sam_dom_info_6(&info->info6);
1774 display_sam_dom_info_7(&info->info7);
1777 display_sam_dom_info_8(&info->info8);
1780 display_sam_dom_info_9(&info->info9);
1783 display_sam_dom_info_12(&info->info12);
1786 display_sam_dom_info_13(&info->info13);
1790 printf("cannot display domain info for switch value %d\n",
1797 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1798 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1802 /* Create domain user */
1804 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1805 TALLOC_CTX *mem_ctx,
1806 int argc, const char **argv)
1808 struct policy_handle connect_pol, domain_pol, user_pol;
1809 NTSTATUS status, result;
1810 struct lsa_String acct_name;
1812 uint32 acct_flags, user_rid;
1813 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1814 uint32_t access_granted = 0;
1815 struct dcerpc_binding_handle *b = cli->binding_handle;
1817 if ((argc < 2) || (argc > 3)) {
1818 printf("Usage: %s username [access mask]\n", argv[0]);
1819 return NT_STATUS_OK;
1822 init_lsa_String(&acct_name, argv[1]);
1825 sscanf(argv[2], "%x", &access_mask);
1827 /* Get sam policy handle */
1829 status = rpccli_try_samr_connects(cli, mem_ctx,
1830 MAXIMUM_ALLOWED_ACCESS,
1832 if (!NT_STATUS_IS_OK(status)) {
1836 /* Get domain policy handle */
1838 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1844 if (!NT_STATUS_IS_OK(status)) {
1847 if (!NT_STATUS_IS_OK(result)) {
1852 /* Create domain user */
1854 acb_info = ACB_NORMAL;
1855 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1856 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1857 SAMR_USER_ACCESS_SET_PASSWORD |
1858 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1859 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1861 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1870 if (!NT_STATUS_IS_OK(status)) {
1873 if (!NT_STATUS_IS_OK(result)) {
1878 status = dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1879 if (!NT_STATUS_IS_OK(status)) goto done;
1881 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1882 if (!NT_STATUS_IS_OK(status)) goto done;
1884 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1885 if (!NT_STATUS_IS_OK(status)) goto done;
1891 /* Create domain group */
1893 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1894 TALLOC_CTX *mem_ctx,
1895 int argc, const char **argv)
1897 struct policy_handle connect_pol, domain_pol, group_pol;
1898 NTSTATUS status, result;
1899 struct lsa_String grp_name;
1900 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1902 struct dcerpc_binding_handle *b = cli->binding_handle;
1904 if ((argc < 2) || (argc > 3)) {
1905 printf("Usage: %s groupname [access mask]\n", argv[0]);
1906 return NT_STATUS_OK;
1909 init_lsa_String(&grp_name, argv[1]);
1912 sscanf(argv[2], "%x", &access_mask);
1914 /* Get sam policy handle */
1916 status = rpccli_try_samr_connects(cli, mem_ctx,
1917 MAXIMUM_ALLOWED_ACCESS,
1919 if (!NT_STATUS_IS_OK(status)) {
1923 /* Get domain policy handle */
1925 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1931 if (!NT_STATUS_IS_OK(status)) {
1934 if (!NT_STATUS_IS_OK(result)) {
1939 /* Create domain user */
1940 status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
1943 MAXIMUM_ALLOWED_ACCESS,
1947 if (!NT_STATUS_IS_OK(status)) {
1950 if (!NT_STATUS_IS_OK(result)) {
1955 status = dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
1956 if (!NT_STATUS_IS_OK(status)) goto done;
1958 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1959 if (!NT_STATUS_IS_OK(status)) goto done;
1961 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1962 if (!NT_STATUS_IS_OK(status)) goto done;
1968 /* Create domain alias */
1970 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1971 TALLOC_CTX *mem_ctx,
1972 int argc, const char **argv)
1974 struct policy_handle connect_pol, domain_pol, alias_pol;
1975 NTSTATUS status, result;
1976 struct lsa_String alias_name;
1977 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1979 struct dcerpc_binding_handle *b = cli->binding_handle;
1981 if ((argc < 2) || (argc > 3)) {
1982 printf("Usage: %s aliasname [access mask]\n", argv[0]);
1983 return NT_STATUS_OK;
1986 init_lsa_String(&alias_name, argv[1]);
1989 sscanf(argv[2], "%x", &access_mask);
1991 /* Get sam policy handle */
1993 status = rpccli_try_samr_connects(cli, mem_ctx,
1994 MAXIMUM_ALLOWED_ACCESS,
1996 if (!NT_STATUS_IS_OK(status)) {
2000 /* Get domain policy handle */
2002 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2008 if (!NT_STATUS_IS_OK(status)) {
2011 if (!NT_STATUS_IS_OK(result)) {
2016 /* Create domain user */
2018 status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2021 MAXIMUM_ALLOWED_ACCESS,
2025 if (!NT_STATUS_IS_OK(status)) {
2028 if (!NT_STATUS_IS_OK(result)) {
2034 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
2035 if (!NT_STATUS_IS_OK(status)) goto done;
2037 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2038 if (!NT_STATUS_IS_OK(status)) goto done;
2040 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2041 if (!NT_STATUS_IS_OK(status)) goto done;
2047 /* Lookup sam names */
2049 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
2050 TALLOC_CTX *mem_ctx,
2051 int argc, const char **argv)
2053 NTSTATUS status, result;
2054 struct policy_handle connect_pol, domain_pol;
2056 struct samr_Ids rids, name_types;
2058 struct lsa_String *names = NULL;
2059 struct dcerpc_binding_handle *b = cli->binding_handle;
2062 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
2063 printf("check on the domain SID: S-1-5-21-x-y-z\n");
2064 printf("or check on the builtin SID: S-1-5-32\n");
2065 return NT_STATUS_OK;
2068 /* Get sam policy and domain handles */
2070 status = rpccli_try_samr_connects(cli, mem_ctx,
2071 MAXIMUM_ALLOWED_ACCESS,
2073 if (!NT_STATUS_IS_OK(status)) {
2077 status = get_domain_handle(cli, mem_ctx, argv[1],
2079 MAXIMUM_ALLOWED_ACCESS,
2082 if (!NT_STATUS_IS_OK(status)) {
2088 num_names = argc - 2;
2090 if ((names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names)) == NULL) {
2091 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2092 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2093 status = NT_STATUS_NO_MEMORY;
2097 for (i = 0; i < num_names; i++) {
2098 init_lsa_String(&names[i], argv[i + 2]);
2101 status = dcerpc_samr_LookupNames(b, mem_ctx,
2108 if (!NT_STATUS_IS_OK(status)) {
2111 if (!NT_STATUS_IS_OK(result)) {
2116 /* Display results */
2118 for (i = 0; i < num_names; i++)
2119 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
2122 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2123 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2128 /* Lookup sam rids */
2130 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
2131 TALLOC_CTX *mem_ctx,
2132 int argc, const char **argv)
2134 NTSTATUS status, result;
2135 struct policy_handle connect_pol, domain_pol;
2136 uint32_t num_rids, *rids;
2137 struct lsa_Strings names;
2138 struct samr_Ids types;
2139 struct dcerpc_binding_handle *b = cli->binding_handle;
2144 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
2145 return NT_STATUS_OK;
2148 /* Get sam policy and domain handles */
2150 status = rpccli_try_samr_connects(cli, mem_ctx,
2151 MAXIMUM_ALLOWED_ACCESS,
2153 if (!NT_STATUS_IS_OK(status)) {
2157 status = get_domain_handle(cli, mem_ctx, argv[1],
2159 MAXIMUM_ALLOWED_ACCESS,
2162 if (!NT_STATUS_IS_OK(status)) {
2168 num_rids = argc - 2;
2170 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
2171 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2172 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2173 status = NT_STATUS_NO_MEMORY;
2177 for (i = 0; i < argc - 2; i++)
2178 sscanf(argv[i + 2], "%i", &rids[i]);
2180 status = dcerpc_samr_LookupRids(b, mem_ctx,
2187 if (!NT_STATUS_IS_OK(status)) {
2191 if (!NT_STATUS_IS_OK(result) &&
2192 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2195 /* Display results */
2197 for (i = 0; i < num_rids; i++) {
2198 printf("rid 0x%x: %s (%d)\n",
2199 rids[i], names.names[i].string, types.ids[i]);
2202 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2203 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2208 /* Delete domain group */
2210 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
2211 TALLOC_CTX *mem_ctx,
2212 int argc, const char **argv)
2214 NTSTATUS status, result;
2215 struct policy_handle connect_pol, domain_pol, group_pol;
2216 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2217 struct dcerpc_binding_handle *b = cli->binding_handle;
2219 if ((argc < 2) || (argc > 3)) {
2220 printf("Usage: %s groupname\n", argv[0]);
2221 return NT_STATUS_OK;
2225 sscanf(argv[2], "%x", &access_mask);
2227 /* Get sam policy and domain handles */
2229 status = rpccli_try_samr_connects(cli, mem_ctx,
2230 MAXIMUM_ALLOWED_ACCESS,
2232 if (!NT_STATUS_IS_OK(status)) {
2236 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2238 MAXIMUM_ALLOWED_ACCESS,
2242 if (!NT_STATUS_IS_OK(status)) {
2245 if (!NT_STATUS_IS_OK(result)) {
2250 /* Get handle on group */
2253 struct samr_Ids group_rids, name_types;
2254 struct lsa_String lsa_acct_name;
2256 init_lsa_String(&lsa_acct_name, argv[1]);
2258 status = dcerpc_samr_LookupNames(b, mem_ctx,
2265 if (!NT_STATUS_IS_OK(status)) {
2268 if (!NT_STATUS_IS_OK(result)) {
2273 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2279 if (!NT_STATUS_IS_OK(status)) {
2282 if (!NT_STATUS_IS_OK(result)) {
2290 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2293 if (!NT_STATUS_IS_OK(status)) {
2296 if (!NT_STATUS_IS_OK(result)) {
2301 /* Display results */
2303 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2304 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2305 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2311 /* Delete domain user */
2313 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2314 TALLOC_CTX *mem_ctx,
2315 int argc, const char **argv)
2317 NTSTATUS status, result;
2318 struct policy_handle connect_pol, domain_pol, user_pol;
2319 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2320 struct dcerpc_binding_handle *b = cli->binding_handle;
2322 if ((argc < 2) || (argc > 3)) {
2323 printf("Usage: %s username\n", argv[0]);
2324 return NT_STATUS_OK;
2328 sscanf(argv[2], "%x", &access_mask);
2330 /* Get sam policy and domain handles */
2332 status = rpccli_try_samr_connects(cli, mem_ctx,
2333 MAXIMUM_ALLOWED_ACCESS,
2335 if (!NT_STATUS_IS_OK(status)) {
2339 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2341 MAXIMUM_ALLOWED_ACCESS,
2345 if (!NT_STATUS_IS_OK(status)) {
2348 if (!NT_STATUS_IS_OK(result)) {
2353 /* Get handle on user */
2356 struct samr_Ids user_rids, name_types;
2357 struct lsa_String lsa_acct_name;
2359 init_lsa_String(&lsa_acct_name, argv[1]);
2361 status = dcerpc_samr_LookupNames(b, mem_ctx,
2368 if (!NT_STATUS_IS_OK(status)) {
2371 if (!NT_STATUS_IS_OK(result)) {
2376 status = dcerpc_samr_OpenUser(b, mem_ctx,
2382 if (!NT_STATUS_IS_OK(status)) {
2385 if (!NT_STATUS_IS_OK(result)) {
2393 status = dcerpc_samr_DeleteUser(b, mem_ctx,
2396 if (!NT_STATUS_IS_OK(status)) {
2399 if (!NT_STATUS_IS_OK(result)) {
2404 /* Display results */
2406 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2407 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2408 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2414 /**********************************************************************
2415 * Query user security object
2417 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2418 TALLOC_CTX *mem_ctx,
2419 int argc, const char **argv)
2421 struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2422 NTSTATUS status, result;
2423 uint32 sec_info = SECINFO_DACL;
2424 uint32 user_rid = 0;
2425 TALLOC_CTX *ctx = NULL;
2426 struct sec_desc_buf *sec_desc_buf=NULL;
2427 bool domain = False;
2428 struct dcerpc_binding_handle *b = cli->binding_handle;
2430 ctx=talloc_init("cmd_samr_query_sec_obj");
2432 if ((argc < 1) || (argc > 3)) {
2433 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2434 printf("\tSpecify rid for security on user, -d for security on domain\n");
2435 talloc_destroy(ctx);
2436 return NT_STATUS_OK;
2440 if (strcmp(argv[1], "-d") == 0)
2443 sscanf(argv[1], "%i", &user_rid);
2447 sec_info = atoi(argv[2]);
2450 status = rpccli_try_samr_connects(cli, mem_ctx,
2451 MAXIMUM_ALLOWED_ACCESS,
2453 if (!NT_STATUS_IS_OK(status)) {
2457 if (domain || user_rid) {
2458 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2460 MAXIMUM_ALLOWED_ACCESS,
2464 if (!NT_STATUS_IS_OK(status)) {
2467 if (!NT_STATUS_IS_OK(result)) {
2474 status = dcerpc_samr_OpenUser(b, mem_ctx,
2476 MAXIMUM_ALLOWED_ACCESS,
2480 if (!NT_STATUS_IS_OK(status)) {
2483 if (!NT_STATUS_IS_OK(result)) {
2489 /* Pick which query pol to use */
2499 /* Query SAM security object */
2501 status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2506 if (!NT_STATUS_IS_OK(status)) {
2509 if (!NT_STATUS_IS_OK(result)) {
2514 display_sec_desc(sec_desc_buf->sd);
2516 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2517 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2518 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2520 talloc_destroy(ctx);
2524 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2525 TALLOC_CTX *mem_ctx,
2526 int argc, const char **argv)
2528 NTSTATUS status, result;
2529 struct policy_handle connect_pol, domain_pol, user_pol;
2530 struct samr_PwInfo info;
2532 struct dcerpc_binding_handle *b = cli->binding_handle;
2535 printf("Usage: %s rid\n", argv[0]);
2536 return NT_STATUS_OK;
2539 sscanf(argv[1], "%i", &rid);
2541 status = rpccli_try_samr_connects(cli, mem_ctx,
2542 MAXIMUM_ALLOWED_ACCESS,
2544 if (!NT_STATUS_IS_OK(status)) {
2548 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2550 MAXIMUM_ALLOWED_ACCESS,
2554 if (!NT_STATUS_IS_OK(status)) {
2557 if (!NT_STATUS_IS_OK(result)) {
2562 status = dcerpc_samr_OpenUser(b, mem_ctx,
2564 MAXIMUM_ALLOWED_ACCESS,
2568 if (!NT_STATUS_IS_OK(status)) {
2571 if (!NT_STATUS_IS_OK(result)) {
2576 status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2580 if (!NT_STATUS_IS_OK(status)) {
2584 if (NT_STATUS_IS_OK(result)) {
2585 printf("min_password_length: %d\n", info.min_password_length);
2587 NDR_PRINT_STRUCT_STRING(mem_ctx,
2588 samr_PasswordProperties, &info.password_properties));
2592 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2593 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2594 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2599 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2600 TALLOC_CTX *mem_ctx,
2601 int argc, const char **argv)
2603 NTSTATUS status, result;
2604 struct lsa_String domain_name;
2605 struct samr_PwInfo info;
2606 struct dcerpc_binding_handle *b = cli->binding_handle;
2608 if (argc < 1 || argc > 3) {
2609 printf("Usage: %s <domain>\n", argv[0]);
2610 return NT_STATUS_OK;
2613 init_lsa_String(&domain_name, argv[1]);
2615 status = dcerpc_samr_GetDomPwInfo(b, mem_ctx,
2619 if (!NT_STATUS_IS_OK(status)) {
2622 if (NT_STATUS_IS_OK(result)) {
2623 printf("min_password_length: %d\n", info.min_password_length);
2624 display_password_properties(info.password_properties);
2630 /* Look up domain name */
2632 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2633 TALLOC_CTX *mem_ctx,
2634 int argc, const char **argv)
2636 struct policy_handle connect_pol, domain_pol;
2637 NTSTATUS status, result;
2638 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2640 struct lsa_String domain_name;
2641 struct dom_sid *sid = NULL;
2642 struct dcerpc_binding_handle *b = cli->binding_handle;
2645 printf("Usage: %s domain_name\n", argv[0]);
2646 return NT_STATUS_OK;
2649 init_lsa_String(&domain_name, argv[1]);
2651 status = rpccli_try_samr_connects(cli, mem_ctx,
2654 if (!NT_STATUS_IS_OK(status)) {
2658 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2664 if (!NT_STATUS_IS_OK(status)) {
2667 if (!NT_STATUS_IS_OK(result)) {
2672 status = dcerpc_samr_LookupDomain(b, mem_ctx,
2677 if (!NT_STATUS_IS_OK(status)) {
2680 if (!NT_STATUS_IS_OK(result)) {
2685 if (NT_STATUS_IS_OK(result)) {
2686 sid_to_fstring(sid_string, sid);
2687 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2688 argv[1], sid_string);
2691 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2692 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2697 /* Change user password */
2699 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2700 TALLOC_CTX *mem_ctx,
2701 int argc, const char **argv)
2703 struct policy_handle connect_pol, domain_pol, user_pol;
2704 NTSTATUS status, result;
2705 const char *user, *oldpass, *newpass;
2706 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2707 struct samr_Ids rids, types;
2708 struct lsa_String lsa_acct_name;
2709 struct dcerpc_binding_handle *b = cli->binding_handle;
2712 printf("Usage: %s username oldpass newpass\n", argv[0]);
2713 return NT_STATUS_INVALID_PARAMETER;
2720 /* Get sam policy handle */
2722 status = rpccli_try_samr_connects(cli, mem_ctx,
2723 MAXIMUM_ALLOWED_ACCESS,
2725 if (!NT_STATUS_IS_OK(status)) {
2729 /* Get domain policy handle */
2731 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2737 if (!NT_STATUS_IS_OK(status)) {
2740 if (!NT_STATUS_IS_OK(result)) {
2745 init_lsa_String(&lsa_acct_name, user);
2747 status = dcerpc_samr_LookupNames(b, mem_ctx,
2754 if (!NT_STATUS_IS_OK(status)) {
2757 if (!NT_STATUS_IS_OK(result)) {
2762 status = dcerpc_samr_OpenUser(b, mem_ctx,
2768 if (!NT_STATUS_IS_OK(status)) {
2771 if (!NT_STATUS_IS_OK(result)) {
2776 /* Change user password */
2777 status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2781 if (!NT_STATUS_IS_OK(status)) {
2786 if (is_valid_policy_hnd(&user_pol)) {
2787 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2789 if (is_valid_policy_hnd(&domain_pol)) {
2790 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2792 if (is_valid_policy_hnd(&connect_pol)) {
2793 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2800 /* Change user password */
2802 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2803 TALLOC_CTX *mem_ctx,
2804 int argc, const char **argv)
2806 struct policy_handle connect_pol, domain_pol;
2807 NTSTATUS status, result;
2808 const char *user, *oldpass, *newpass;
2809 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2810 struct dcerpc_binding_handle *b = cli->binding_handle;
2813 printf("Usage: %s username oldpass newpass\n", argv[0]);
2814 return NT_STATUS_INVALID_PARAMETER;
2821 /* Get sam policy handle */
2823 status = rpccli_try_samr_connects(cli, mem_ctx,
2824 MAXIMUM_ALLOWED_ACCESS,
2826 if (!NT_STATUS_IS_OK(status)) {
2830 /* Get domain policy handle */
2832 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2838 if (!NT_STATUS_IS_OK(status)) {
2841 if (!NT_STATUS_IS_OK(result)) {
2846 /* Change user password */
2847 status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2849 if (!NT_STATUS_IS_OK(status)) {
2853 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2854 if (!NT_STATUS_IS_OK(status)) goto done;
2856 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2857 if (!NT_STATUS_IS_OK(status)) goto done;
2864 /* Change user password */
2866 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2867 TALLOC_CTX *mem_ctx,
2868 int argc, const char **argv)
2870 struct policy_handle connect_pol, domain_pol;
2871 NTSTATUS status, result;
2872 const char *user, *oldpass, *newpass;
2873 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2874 struct samr_DomInfo1 *info = NULL;
2875 struct userPwdChangeFailureInformation *reject = NULL;
2876 struct dcerpc_binding_handle *b = cli->binding_handle;
2879 printf("Usage: %s username oldpass newpass\n", argv[0]);
2880 return NT_STATUS_INVALID_PARAMETER;
2887 /* Get sam policy handle */
2889 status = rpccli_try_samr_connects(cli, mem_ctx,
2890 MAXIMUM_ALLOWED_ACCESS,
2892 if (!NT_STATUS_IS_OK(status)) {
2896 /* Get domain policy handle */
2898 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2904 if (!NT_STATUS_IS_OK(status)) {
2907 if (!NT_STATUS_IS_OK(result)) {
2912 /* Change user password */
2913 status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2919 if (!NT_STATUS_IS_OK(status)) {
2923 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2925 display_sam_dom_info_1(info);
2927 switch (reject->extendedFailureReason) {
2928 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
2929 d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
2931 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
2932 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
2934 case SAM_PWD_CHANGE_NOT_COMPLEX:
2935 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
2938 d_printf("unknown reject reason: %d\n",
2939 reject->extendedFailureReason);
2944 if (!NT_STATUS_IS_OK(result)) {
2949 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2950 if (!NT_STATUS_IS_OK(status)) goto done;
2952 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2953 if (!NT_STATUS_IS_OK(status)) goto done;
2959 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
2960 TALLOC_CTX *mem_ctx,
2961 int argc, const char **argv,
2964 struct policy_handle connect_pol, domain_pol, user_pol;
2965 NTSTATUS status, result;
2966 const char *user, *param;
2967 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2970 union samr_UserInfo info;
2971 struct samr_CryptPassword pwd_buf;
2972 struct samr_CryptPasswordEx pwd_buf_ex;
2973 uint8_t nt_hash[16];
2974 uint8_t lm_hash[16];
2975 DATA_BLOB session_key;
2976 uint8_t password_expired = 0;
2977 struct dcerpc_binding_handle *b = cli->binding_handle;
2980 printf("Usage: %s username level password [password_expired]\n",
2982 return NT_STATUS_INVALID_PARAMETER;
2986 level = atoi(argv[2]);
2990 password_expired = atoi(argv[4]);
2993 status = cli_get_session_key(mem_ctx, cli, &session_key);
2994 if (!NT_STATUS_IS_OK(status)) {
2998 init_samr_CryptPassword(param, &session_key, &pwd_buf);
2999 init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
3000 nt_lm_owf_gen(param, nt_hash, lm_hash);
3006 in = data_blob_const(nt_hash, 16);
3007 out = data_blob_talloc_zero(mem_ctx, 16);
3008 sess_crypt_blob(&out, &in, &session_key, true);
3009 memcpy(nt_hash, out.data, out.length);
3013 in = data_blob_const(lm_hash, 16);
3014 out = data_blob_talloc_zero(mem_ctx, 16);
3015 sess_crypt_blob(&out, &in, &session_key, true);
3016 memcpy(lm_hash, out.data, out.length);
3019 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
3020 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
3021 info.info18.nt_pwd_active = true;
3022 info.info18.lm_pwd_active = true;
3023 info.info18.password_expired = password_expired;
3027 ZERO_STRUCT(info.info21);
3029 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3030 SAMR_FIELD_LM_PASSWORD_PRESENT;
3032 info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3033 info.info21.password_expired = password_expired;
3036 info.info21.lm_password_set = true;
3037 info.info21.lm_owf_password.length = 16;
3038 info.info21.lm_owf_password.size = 16;
3040 info.info21.nt_password_set = true;
3041 info.info21.nt_owf_password.length = 16;
3042 info.info21.nt_owf_password.size = 16;
3046 in = data_blob_const(nt_hash, 16);
3047 out = data_blob_talloc_zero(mem_ctx, 16);
3048 sess_crypt_blob(&out, &in, &session_key, true);
3049 info.info21.nt_owf_password.array =
3050 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3054 in = data_blob_const(lm_hash, 16);
3055 out = data_blob_talloc_zero(mem_ctx, 16);
3056 sess_crypt_blob(&out, &in, &session_key, true);
3057 info.info21.lm_owf_password.array =
3058 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3063 ZERO_STRUCT(info.info23);
3065 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3066 SAMR_FIELD_LM_PASSWORD_PRESENT;
3068 info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3069 info.info23.info.password_expired = password_expired;
3072 info.info23.password = pwd_buf;
3076 info.info24.password = pwd_buf;
3077 info.info24.password_expired = password_expired;
3081 ZERO_STRUCT(info.info25);
3083 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3084 SAMR_FIELD_LM_PASSWORD_PRESENT;
3086 info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3087 info.info25.info.password_expired = password_expired;
3090 info.info25.password = pwd_buf_ex;
3094 info.info26.password = pwd_buf_ex;
3095 info.info26.password_expired = password_expired;
3099 return NT_STATUS_INVALID_INFO_CLASS;
3102 /* Get sam policy handle */
3104 status = rpccli_try_samr_connects(cli, mem_ctx,
3105 MAXIMUM_ALLOWED_ACCESS,
3107 if (!NT_STATUS_IS_OK(status)) {
3111 /* Get domain policy handle */
3113 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3120 if (!NT_STATUS_IS_OK(status))
3122 if (!NT_STATUS_IS_OK(result)) {
3127 user_rid = strtol(user, NULL, 0);
3129 status = dcerpc_samr_OpenUser(b, mem_ctx,
3135 if (!NT_STATUS_IS_OK(status)) {
3142 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3145 /* Probably this was a user name, try lookupnames */
3146 struct samr_Ids rids, types;
3147 struct lsa_String lsa_acct_name;
3149 init_lsa_String(&lsa_acct_name, user);
3151 status = dcerpc_samr_LookupNames(b, mem_ctx,
3158 if (!NT_STATUS_IS_OK(status)) {
3161 if (!NT_STATUS_IS_OK(result)) {
3166 status = dcerpc_samr_OpenUser(b, mem_ctx,
3172 if (!NT_STATUS_IS_OK(status)) {
3175 if (!NT_STATUS_IS_OK(result)) {
3181 case NDR_SAMR_SETUSERINFO:
3182 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
3188 case NDR_SAMR_SETUSERINFO2:
3189 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
3196 return NT_STATUS_INVALID_PARAMETER;
3198 if (!NT_STATUS_IS_OK(status)) {
3199 DEBUG(0,("status: %s\n", nt_errstr(status)));
3202 if (!NT_STATUS_IS_OK(result)) {
3204 DEBUG(0,("result: %s\n", nt_errstr(status)));
3211 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
3212 TALLOC_CTX *mem_ctx,
3213 int argc, const char **argv)
3215 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3216 NDR_SAMR_SETUSERINFO);
3219 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
3220 TALLOC_CTX *mem_ctx,
3221 int argc, const char **argv)
3223 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3224 NDR_SAMR_SETUSERINFO2);
3227 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
3228 TALLOC_CTX *mem_ctx,
3229 int argc, const char **argv)
3231 NTSTATUS status, result;
3232 struct policy_handle connect_handle;
3233 struct policy_handle domain_handle;
3235 struct lsa_String name;
3237 struct dcerpc_binding_handle *b = cli->binding_handle;
3239 if (argc < 2 || argc > 3) {
3240 printf("Usage: %s name level\n", argv[0]);
3241 return NT_STATUS_INVALID_PARAMETER;
3244 init_lsa_String(&name, argv[1]);
3247 level = atoi(argv[2]);
3250 status = rpccli_try_samr_connects(cli, mem_ctx,
3251 SEC_FLAG_MAXIMUM_ALLOWED,
3253 if (!NT_STATUS_IS_OK(status)) {
3257 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3259 SEC_FLAG_MAXIMUM_ALLOWED,
3263 if (!NT_STATUS_IS_OK(status)) {
3266 if (!NT_STATUS_IS_OK(result)) {
3271 status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3277 if (!NT_STATUS_IS_OK(status)) {
3283 if (NT_STATUS_IS_OK(status) ||
3284 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
3285 printf("idx: %d (0x%08x)\n", idx, idx);
3289 if (is_valid_policy_hnd(&domain_handle)) {
3290 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
3292 if (is_valid_policy_hnd(&connect_handle)) {
3293 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
3299 /* List of commands exported by this module */
3301 struct cmd_set samr_commands[] = {
3305 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, &ndr_table_samr.syntax_id, NULL, "Query user info", "" },
3306 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, &ndr_table_samr.syntax_id, NULL, "Query group info", "" },
3307 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, &ndr_table_samr.syntax_id, NULL, "Query user groups", "" },
3308 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, &ndr_table_samr.syntax_id, NULL, "Query user aliases", "" },
3309 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, &ndr_table_samr.syntax_id, NULL, "Query group membership", "" },
3310 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, &ndr_table_samr.syntax_id, NULL, "Query alias membership", "" },
3311 { "queryaliasinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo, NULL, &ndr_table_samr.syntax_id, NULL, "Query alias info", "" },
3312 { "deletealias", RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias, NULL, &ndr_table_samr.syntax_id, NULL, "Delete an alias", "" },
3313 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, &ndr_table_samr.syntax_id, NULL, "Query display info", "" },
3314 { "querydispinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2, NULL, &ndr_table_samr.syntax_id, NULL, "Query display info", "" },
3315 { "querydispinfo3", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3, NULL, &ndr_table_samr.syntax_id, NULL, "Query display info", "" },
3316 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, &ndr_table_samr.syntax_id, NULL, "Query domain info", "" },
3317 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, &ndr_table_samr.syntax_id, NULL, "Enumerate domain users", "" },
3318 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, &ndr_table_samr.syntax_id, NULL, "Enumerate domain groups", "" },
3319 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, &ndr_table_samr.syntax_id, NULL, "Enumerate alias groups", "" },
3320 { "enumdomains", RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains, NULL, &ndr_table_samr.syntax_id, NULL, "Enumerate domains", "" },
3322 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, &ndr_table_samr.syntax_id, NULL, "Create domain user", "" },
3323 { "createdomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group, NULL, &ndr_table_samr.syntax_id, NULL, "Create domain group", "" },
3324 { "createdomalias", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias, NULL, &ndr_table_samr.syntax_id, NULL, "Create domain alias", "" },
3325 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, &ndr_table_samr.syntax_id, NULL, "Look up names", "" },
3326 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, &ndr_table_samr.syntax_id, NULL, "Look up names", "" },
3327 { "deletedomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group, NULL, &ndr_table_samr.syntax_id, NULL, "Delete domain group", "" },
3328 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, &ndr_table_samr.syntax_id, NULL, "Delete domain user", "" },
3329 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, &ndr_table_samr.syntax_id, NULL, "Query SAMR security object", "" },
3330 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, &ndr_table_samr.syntax_id, NULL, "Retrieve domain password info", "" },
3331 { "getusrdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo, NULL, &ndr_table_samr.syntax_id, NULL, "Retrieve user domain password info", "" },
3333 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, &ndr_table_samr.syntax_id, NULL, "Lookup Domain Name", "" },
3334 { "chgpasswd", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd, NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
3335 { "chgpasswd2", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2, NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
3336 { "chgpasswd3", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3, NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
3337 { "getdispinfoidx", RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx, NULL, &ndr_table_samr.syntax_id, NULL, "Get Display Information Index", "" },
3338 { "setuserinfo", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo, NULL, &ndr_table_samr.syntax_id, NULL, "Set user info", "" },
3339 { "setuserinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2, NULL, &ndr_table_samr.syntax_id, NULL, "Set user info2", "" },