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_t info_level = 21;
320 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
321 union samr_UserInfo *info = NULL;
322 uint32_t user_rid = 0;
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)) {
389 if (NT_STATUS_IS_OK(result)) {
390 if (rids.count != 1) {
391 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
394 if (types.count != 1) {
395 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
399 status = dcerpc_samr_OpenUser(b, mem_ctx,
405 if (!NT_STATUS_IS_OK(status)) {
412 if (!NT_STATUS_IS_OK(result)) {
417 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
422 if (!NT_STATUS_IS_OK(status)) {
425 if (!NT_STATUS_IS_OK(result)) {
430 switch (info_level) {
432 display_samr_user_info_7(&info->info7);
435 display_samr_user_info_9(&info->info9);
438 display_samr_user_info_16(&info->info16);
441 display_samr_user_info_20(&info->info20);
444 display_samr_user_info_21(&info->info21);
447 printf("Unsupported infolevel: %d\n", info_level);
451 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
452 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
453 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
459 /****************************************************************************
461 ****************************************************************************/
462 static void display_group_info1(struct samr_GroupInfoAll *info1)
464 printf("\tGroup Name:\t%s\n", info1->name.string);
465 printf("\tDescription:\t%s\n", info1->description.string);
466 printf("\tGroup Attribute:%d\n", info1->attributes);
467 printf("\tNum Members:%d\n", info1->num_members);
470 /****************************************************************************
472 ****************************************************************************/
473 static void display_group_info2(struct lsa_String *info2)
475 printf("\tGroup Description:%s\n", info2->string);
479 /****************************************************************************
481 ****************************************************************************/
482 static void display_group_info3(struct samr_GroupInfoAttributes *info3)
484 printf("\tGroup Attribute:%d\n", info3->attributes);
488 /****************************************************************************
490 ****************************************************************************/
491 static void display_group_info4(struct lsa_String *info4)
493 printf("\tGroup Description:%s\n", info4->string);
496 /****************************************************************************
498 ****************************************************************************/
499 static void display_group_info5(struct samr_GroupInfoAll *info5)
501 printf("\tGroup Name:\t%s\n", info5->name.string);
502 printf("\tDescription:\t%s\n", info5->description.string);
503 printf("\tGroup Attribute:%d\n", info5->attributes);
504 printf("\tNum Members:%d\n", info5->num_members);
507 /****************************************************************************
508 display sam sync structure
509 ****************************************************************************/
510 static void display_group_info(union samr_GroupInfo *info,
511 enum samr_GroupInfoEnum level)
515 display_group_info1(&info->all);
518 display_group_info2(&info->name);
521 display_group_info3(&info->attributes);
524 display_group_info4(&info->description);
527 display_group_info5(&info->all2);
532 /***********************************************************************
533 * Query group information
535 static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
537 int argc, const char **argv)
539 struct policy_handle connect_pol, domain_pol, group_pol;
540 NTSTATUS status, result;
541 enum samr_GroupInfoEnum info_level = GROUPINFOALL;
542 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
543 union samr_GroupInfo *group_info = NULL;
545 struct dcerpc_binding_handle *b = cli->binding_handle;
547 if ((argc < 2) || (argc > 4)) {
548 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
552 sscanf(argv[1], "%i", &group_rid);
555 info_level = atoi(argv[2]);
558 sscanf(argv[3], "%x", &access_mask);
560 status = rpccli_try_samr_connects(cli, mem_ctx,
561 MAXIMUM_ALLOWED_ACCESS,
563 if (!NT_STATUS_IS_OK(status)) {
567 status = dcerpc_samr_OpenDomain(b, mem_ctx,
569 MAXIMUM_ALLOWED_ACCESS,
573 if (!NT_STATUS_IS_OK(status)) {
576 if (!NT_STATUS_IS_OK(result)) {
581 status = dcerpc_samr_OpenGroup(b, mem_ctx,
587 if (!NT_STATUS_IS_OK(status)) {
590 if (!NT_STATUS_IS_OK(result)) {
595 status = dcerpc_samr_QueryGroupInfo(b, mem_ctx,
600 if (!NT_STATUS_IS_OK(status)) {
603 if (!NT_STATUS_IS_OK(result)) {
608 display_group_info(group_info, info_level);
610 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
611 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
612 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
617 /* Query groups a user is a member of */
619 static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
621 int argc, const char **argv)
623 struct policy_handle connect_pol,
626 NTSTATUS status, result;
628 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
630 struct samr_RidWithAttributeArray *rid_array = NULL;
631 struct dcerpc_binding_handle *b = cli->binding_handle;
633 if ((argc < 2) || (argc > 3)) {
634 printf("Usage: %s rid [access mask]\n", argv[0]);
638 sscanf(argv[1], "%i", &user_rid);
641 sscanf(argv[2], "%x", &access_mask);
643 status = rpccli_try_samr_connects(cli, mem_ctx,
644 MAXIMUM_ALLOWED_ACCESS,
646 if (!NT_STATUS_IS_OK(status)) {
650 status = dcerpc_samr_OpenDomain(b, mem_ctx,
652 MAXIMUM_ALLOWED_ACCESS,
656 if (!NT_STATUS_IS_OK(status)) {
659 if (!NT_STATUS_IS_OK(result)) {
664 status = dcerpc_samr_OpenUser(b, mem_ctx,
671 if (!NT_STATUS_IS_OK(status)) {
674 if (!NT_STATUS_IS_OK(result)) {
679 status = dcerpc_samr_GetGroupsForUser(b, mem_ctx,
683 if (!NT_STATUS_IS_OK(status)) {
686 if (!NT_STATUS_IS_OK(result)) {
691 for (i = 0; i < rid_array->count; i++) {
692 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
693 rid_array->rids[i].rid,
694 rid_array->rids[i].attributes);
697 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
698 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
699 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
704 /* Query aliases a user is a member of */
706 static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
708 int argc, const char **argv)
710 struct policy_handle connect_pol, domain_pol;
711 NTSTATUS status, result;
712 struct dom_sid *sids;
714 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
716 struct lsa_SidArray sid_array;
717 struct samr_Ids alias_rids;
718 struct dcerpc_binding_handle *b = cli->binding_handle;
721 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
722 return NT_STATUS_INVALID_PARAMETER;
728 for (i=2; i<argc; i++) {
729 struct dom_sid tmp_sid;
730 if (!string_to_sid(&tmp_sid, argv[i])) {
731 printf("%s is not a legal SID\n", argv[i]);
732 return NT_STATUS_INVALID_PARAMETER;
734 result = add_sid_to_array(mem_ctx, &tmp_sid, &sids, &num_sids);
735 if (!NT_STATUS_IS_OK(result)) {
741 sid_array.sids = talloc_zero_array(mem_ctx, struct lsa_SidPtr, num_sids);
742 if (sid_array.sids == NULL)
743 return NT_STATUS_NO_MEMORY;
745 sid_array.sids = NULL;
748 for (i=0; i<num_sids; i++) {
749 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
750 if (!sid_array.sids[i].sid) {
751 return NT_STATUS_NO_MEMORY;
755 sid_array.num_sids = num_sids;
757 status = rpccli_try_samr_connects(cli, mem_ctx,
758 MAXIMUM_ALLOWED_ACCESS,
760 if (!NT_STATUS_IS_OK(status)) {
764 status = get_domain_handle(cli, mem_ctx, argv[1],
769 if (!NT_STATUS_IS_OK(status)) {
773 status = dcerpc_samr_GetAliasMembership(b, mem_ctx,
778 if (!NT_STATUS_IS_OK(status)) {
781 if (!NT_STATUS_IS_OK(result)) {
786 for (i = 0; i < alias_rids.count; i++) {
787 printf("\tgroup rid:[0x%x]\n", alias_rids.ids[i]);
790 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
791 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
796 /* Query members of a group */
798 static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
800 int argc, const char **argv)
802 struct policy_handle connect_pol, domain_pol, group_pol;
803 NTSTATUS status, result;
805 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
807 unsigned int old_timeout;
808 struct samr_RidAttrArray *rids = NULL;
809 struct dcerpc_binding_handle *b = cli->binding_handle;
811 if ((argc < 2) || (argc > 3)) {
812 printf("Usage: %s rid [access mask]\n", argv[0]);
816 sscanf(argv[1], "%i", &group_rid);
819 sscanf(argv[2], "%x", &access_mask);
821 status = rpccli_try_samr_connects(cli, mem_ctx,
822 MAXIMUM_ALLOWED_ACCESS,
824 if (!NT_STATUS_IS_OK(status)) {
828 status = dcerpc_samr_OpenDomain(b, mem_ctx,
830 MAXIMUM_ALLOWED_ACCESS,
834 if (!NT_STATUS_IS_OK(status)) {
837 if (!NT_STATUS_IS_OK(result)) {
842 status = dcerpc_samr_OpenGroup(b, mem_ctx,
848 if (!NT_STATUS_IS_OK(status)) {
851 if (!NT_STATUS_IS_OK(result)) {
856 /* Make sure to wait for our DC's reply */
857 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
858 rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
860 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
865 rpccli_set_timeout(cli, old_timeout);
867 if (!NT_STATUS_IS_OK(status)) {
870 if (!NT_STATUS_IS_OK(result)) {
875 for (i = 0; i < rids->count; i++) {
876 printf("\trid:[0x%x] attr:[0x%x]\n", rids->rids[i],
877 rids->attributes[i]);
880 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
881 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
882 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
887 /* Enumerate domain users */
889 static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
891 int argc, const char **argv)
893 struct policy_handle connect_pol;
894 struct policy_handle domain_pol = { 0, };
895 NTSTATUS status, result;
896 uint32_t start_idx, num_dom_users, i;
897 struct samr_SamArray *dom_users = NULL;
898 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
899 uint32_t acb_mask = ACB_NORMAL;
900 uint32_t size = 0xffff;
901 struct dcerpc_binding_handle *b = cli->binding_handle;
903 if ((argc < 1) || (argc > 4)) {
904 printf("Usage: %s [access_mask] [acb_mask] [size]\n", argv[0]);
909 sscanf(argv[1], "%x", &access_mask);
913 sscanf(argv[2], "%x", &acb_mask);
917 sscanf(argv[3], "%x", &size);
920 /* Get sam policy handle */
922 status = rpccli_try_samr_connects(cli, mem_ctx,
923 MAXIMUM_ALLOWED_ACCESS,
925 if (!NT_STATUS_IS_OK(status)) {
929 /* Get domain policy handle */
931 status = get_domain_handle(cli, mem_ctx, "domain",
936 if (!NT_STATUS_IS_OK(status)) {
940 /* Enumerate domain users */
945 status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
953 if (!NT_STATUS_IS_OK(status)) {
956 if (NT_STATUS_IS_OK(result) ||
957 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
959 for (i = 0; i < num_dom_users; i++)
960 printf("user:[%s] rid:[0x%x]\n",
961 dom_users->entries[i].name.string,
962 dom_users->entries[i].idx);
965 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
968 if (is_valid_policy_hnd(&domain_pol))
969 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
971 if (is_valid_policy_hnd(&connect_pol))
972 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
977 /* Enumerate domain groups */
979 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
981 int argc, const char **argv)
983 struct policy_handle connect_pol;
984 struct policy_handle domain_pol = { 0, };
985 NTSTATUS status, result;
986 uint32_t start_idx, num_dom_groups, i;
987 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
988 struct samr_SamArray *dom_groups = NULL;
989 uint32_t size = 0xffff;
990 struct dcerpc_binding_handle *b = cli->binding_handle;
992 if ((argc < 1) || (argc > 3)) {
993 printf("Usage: %s [access_mask] [max_size]\n", argv[0]);
998 sscanf(argv[1], "%x", &access_mask);
1002 sscanf(argv[2], "%x", &size);
1005 /* Get sam policy handle */
1007 status = rpccli_try_samr_connects(cli, mem_ctx,
1008 MAXIMUM_ALLOWED_ACCESS,
1010 if (!NT_STATUS_IS_OK(status)) {
1014 /* Get domain policy handle */
1016 status = get_domain_handle(cli, mem_ctx, "domain",
1021 if (!NT_STATUS_IS_OK(status)) {
1025 /* Enumerate domain groups */
1030 status = dcerpc_samr_EnumDomainGroups(b, mem_ctx,
1037 if (!NT_STATUS_IS_OK(status)) {
1040 if (NT_STATUS_IS_OK(result) ||
1041 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1043 for (i = 0; i < num_dom_groups; i++)
1044 printf("group:[%s] rid:[0x%x]\n",
1045 dom_groups->entries[i].name.string,
1046 dom_groups->entries[i].idx);
1049 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1052 if (is_valid_policy_hnd(&domain_pol))
1053 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1055 if (is_valid_policy_hnd(&connect_pol))
1056 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1061 /* Enumerate alias groups */
1063 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
1064 TALLOC_CTX *mem_ctx,
1065 int argc, const char **argv)
1067 struct policy_handle connect_pol;
1068 struct policy_handle domain_pol = { 0, };
1069 NTSTATUS status, result;
1070 uint32_t start_idx, num_als_groups, i;
1071 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1072 struct samr_SamArray *als_groups = NULL;
1073 uint32_t size = 0xffff;
1074 struct dcerpc_binding_handle *b = cli->binding_handle;
1076 if ((argc < 2) || (argc > 4)) {
1077 printf("Usage: %s builtin|domain [access mask] [max_size]\n", argv[0]);
1078 return NT_STATUS_OK;
1082 sscanf(argv[2], "%x", &access_mask);
1086 sscanf(argv[3], "%x", &size);
1089 /* Get sam policy handle */
1091 status = rpccli_try_samr_connects(cli, mem_ctx,
1092 MAXIMUM_ALLOWED_ACCESS,
1094 if (!NT_STATUS_IS_OK(status)) {
1098 /* Get domain policy handle */
1100 status = get_domain_handle(cli, mem_ctx, argv[1],
1105 if (!NT_STATUS_IS_OK(status)) {
1109 /* Enumerate alias groups */
1114 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
1121 if (!NT_STATUS_IS_OK(status)) {
1124 if (NT_STATUS_IS_OK(result) ||
1125 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1127 for (i = 0; i < num_als_groups; i++)
1128 printf("group:[%s] rid:[0x%x]\n",
1129 als_groups->entries[i].name.string,
1130 als_groups->entries[i].idx);
1132 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1135 if (is_valid_policy_hnd(&domain_pol))
1136 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1138 if (is_valid_policy_hnd(&connect_pol))
1139 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1144 /* Enumerate domains */
1146 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
1147 TALLOC_CTX *mem_ctx,
1148 int argc, const char **argv)
1150 struct policy_handle connect_pol;
1151 NTSTATUS status, result;
1152 uint32_t start_idx, size, num_entries, i;
1153 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1154 struct samr_SamArray *sam = NULL;
1155 struct dcerpc_binding_handle *b = cli->binding_handle;
1157 if ((argc < 1) || (argc > 2)) {
1158 printf("Usage: %s [access mask]\n", argv[0]);
1159 return NT_STATUS_OK;
1163 sscanf(argv[1], "%x", &access_mask);
1166 /* Get sam policy handle */
1168 status = rpccli_try_samr_connects(cli, mem_ctx,
1171 if (!NT_STATUS_IS_OK(status)) {
1175 /* Enumerate alias groups */
1181 status = dcerpc_samr_EnumDomains(b, mem_ctx,
1188 if (!NT_STATUS_IS_OK(status)) {
1191 if (NT_STATUS_IS_OK(result) ||
1192 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1194 for (i = 0; i < num_entries; i++)
1195 printf("name:[%s] idx:[0x%x]\n",
1196 sam->entries[i].name.string,
1197 sam->entries[i].idx);
1199 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1202 if (is_valid_policy_hnd(&connect_pol)) {
1203 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1210 /* Query alias membership */
1212 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1213 TALLOC_CTX *mem_ctx,
1214 int argc, const char **argv)
1216 struct policy_handle connect_pol, domain_pol, alias_pol;
1217 NTSTATUS status, result;
1218 uint32_t alias_rid, i;
1219 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1220 struct lsa_SidArray sid_array;
1221 struct dcerpc_binding_handle *b = cli->binding_handle;
1223 if ((argc < 3) || (argc > 4)) {
1224 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1225 return NT_STATUS_OK;
1228 sscanf(argv[2], "%i", &alias_rid);
1231 sscanf(argv[3], "%x", &access_mask);
1233 /* Open SAMR handle */
1235 status = rpccli_try_samr_connects(cli, mem_ctx,
1236 MAXIMUM_ALLOWED_ACCESS,
1238 if (!NT_STATUS_IS_OK(status)) {
1242 /* Open handle on domain */
1244 status = get_domain_handle(cli, mem_ctx, argv[1],
1246 MAXIMUM_ALLOWED_ACCESS,
1249 if (!NT_STATUS_IS_OK(status)) {
1253 /* Open handle on alias */
1255 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1261 if (!NT_STATUS_IS_OK(status)) {
1264 if (!NT_STATUS_IS_OK(result)) {
1269 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
1273 if (!NT_STATUS_IS_OK(status)) {
1276 if (!NT_STATUS_IS_OK(result)) {
1281 for (i = 0; i < sid_array.num_sids; i++) {
1282 struct dom_sid_buf sid_str;
1284 printf("\tsid:[%s]\n",
1285 dom_sid_str_buf(sid_array.sids[i].sid, &sid_str));
1288 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1289 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1290 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1295 /* Query alias info */
1297 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
1298 TALLOC_CTX *mem_ctx,
1299 int argc, const char **argv)
1301 struct policy_handle connect_pol, domain_pol, alias_pol;
1302 NTSTATUS status, result;
1304 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1305 union samr_AliasInfo *info = NULL;
1306 enum samr_AliasInfoEnum level = ALIASINFOALL;
1307 struct dcerpc_binding_handle *b = cli->binding_handle;
1309 if ((argc < 3) || (argc > 4)) {
1310 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1312 return NT_STATUS_OK;
1315 sscanf(argv[2], "%i", &alias_rid);
1318 level = atoi(argv[3]);
1322 sscanf(argv[4], "%x", &access_mask);
1325 /* Open SAMR handle */
1327 status = rpccli_try_samr_connects(cli, mem_ctx,
1328 SEC_FLAG_MAXIMUM_ALLOWED,
1330 if (!NT_STATUS_IS_OK(status)) {
1334 /* Open handle on domain */
1336 status = get_domain_handle(cli, mem_ctx, argv[1],
1338 SEC_FLAG_MAXIMUM_ALLOWED,
1341 if (!NT_STATUS_IS_OK(status)) {
1345 /* Open handle on alias */
1347 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1353 if (!NT_STATUS_IS_OK(status)) {
1356 if (!NT_STATUS_IS_OK(result)) {
1361 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
1366 if (!NT_STATUS_IS_OK(status)) {
1369 if (!NT_STATUS_IS_OK(result)) {
1376 printf("Name: %s\n", info->all.name.string);
1377 printf("Description: %s\n", info->all.description.string);
1378 printf("Num Members: %d\n", info->all.num_members);
1381 printf("Name: %s\n", info->name.string);
1383 case ALIASINFODESCRIPTION:
1384 printf("Description: %s\n", info->description.string);
1390 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1391 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1392 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1398 /* Query delete an alias membership */
1400 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1401 TALLOC_CTX *mem_ctx,
1402 int argc, const char **argv)
1404 struct policy_handle connect_pol, domain_pol, alias_pol;
1405 NTSTATUS status, result;
1407 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1408 struct dcerpc_binding_handle *b = cli->binding_handle;
1412 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1413 return NT_STATUS_OK;
1416 alias_rid = strtoul_err(argv[2], NULL, 10, &error);
1418 return NT_STATUS_INVALID_PARAMETER;
1422 /* Open SAMR handle */
1424 status = rpccli_try_samr_connects(cli, mem_ctx,
1425 MAXIMUM_ALLOWED_ACCESS,
1427 if (!NT_STATUS_IS_OK(status)) {
1431 /* Open handle on domain */
1433 status = get_domain_handle(cli, mem_ctx, argv[1],
1435 MAXIMUM_ALLOWED_ACCESS,
1438 if (!NT_STATUS_IS_OK(status)) {
1442 /* Open handle on alias */
1444 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1450 if (!NT_STATUS_IS_OK(status)) {
1453 if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1454 /* Probably this was a user name, try lookupnames */
1455 struct samr_Ids rids, types;
1456 struct lsa_String lsa_acct_name;
1458 init_lsa_String(&lsa_acct_name, argv[2]);
1460 status = dcerpc_samr_LookupNames(b, mem_ctx,
1467 if (!NT_STATUS_IS_OK(status)) {
1470 if (NT_STATUS_IS_OK(result)) {
1471 if (rids.count != 1) {
1472 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1475 if (types.count != 1) {
1476 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1480 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1486 if (!NT_STATUS_IS_OK(status)) {
1492 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1495 if (!NT_STATUS_IS_OK(status)) {
1498 if (!NT_STATUS_IS_OK(result)) {
1503 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1504 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1509 /* Query display info */
1511 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1512 TALLOC_CTX *mem_ctx,
1513 int argc, const char **argv,
1516 struct policy_handle connect_pol, domain_pol;
1517 NTSTATUS status, result;
1518 uint32_t start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1519 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1520 uint32_t info_level = 1;
1521 union samr_DispInfo info;
1523 bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1524 uint32_t total_size, returned_size;
1525 struct dcerpc_binding_handle *b = cli->binding_handle;
1528 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1529 return NT_STATUS_OK;
1533 sscanf(argv[1], "%i", &info_level);
1536 sscanf(argv[2], "%i", &start_idx);
1539 sscanf(argv[3], "%i", &max_entries);
1544 sscanf(argv[4], "%i", &max_size);
1549 sscanf(argv[5], "%x", &access_mask);
1551 /* Get sam policy handle */
1553 status = rpccli_try_samr_connects(cli, mem_ctx,
1554 MAXIMUM_ALLOWED_ACCESS,
1556 if (!NT_STATUS_IS_OK(status)) {
1560 /* Get domain policy handle */
1562 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1568 if (!NT_STATUS_IS_OK(status)) {
1571 if (!NT_STATUS_IS_OK(result)) {
1576 /* Query display info */
1581 dcerpc_get_query_dispinfo_params(
1582 loop_count, &max_entries, &max_size);
1585 case NDR_SAMR_QUERYDISPLAYINFO:
1586 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1597 case NDR_SAMR_QUERYDISPLAYINFO2:
1598 status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1610 case NDR_SAMR_QUERYDISPLAYINFO3:
1611 status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1624 return NT_STATUS_INVALID_PARAMETER;
1627 if (!NT_STATUS_IS_OK(status)) {
1631 if (!NT_STATUS_IS_OK(result) &&
1632 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1633 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1639 switch (info_level) {
1641 num_entries = info.info1.count;
1644 num_entries = info.info2.count;
1647 num_entries = info.info3.count;
1650 num_entries = info.info4.count;
1653 num_entries = info.info5.count;
1659 start_idx += num_entries;
1661 if (num_entries == 0)
1664 for (i = 0; i < num_entries; i++) {
1665 switch (info_level) {
1667 display_sam_info_1(&info.info1.entries[i]);
1670 display_sam_info_2(&info.info2.entries[i]);
1673 display_sam_info_3(&info.info3.entries[i]);
1676 display_sam_info_4(&info.info4.entries[i]);
1679 display_sam_info_5(&info.info5.entries[i]);
1683 } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1685 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1686 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1691 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1692 TALLOC_CTX *mem_ctx,
1693 int argc, const char **argv)
1695 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1696 NDR_SAMR_QUERYDISPLAYINFO);
1699 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1700 TALLOC_CTX *mem_ctx,
1701 int argc, const char **argv)
1703 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1704 NDR_SAMR_QUERYDISPLAYINFO2);
1707 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1708 TALLOC_CTX *mem_ctx,
1709 int argc, const char **argv)
1711 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1712 NDR_SAMR_QUERYDISPLAYINFO3);
1715 /* Query domain info */
1717 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1718 TALLOC_CTX *mem_ctx,
1719 int argc, const char **argv)
1721 struct policy_handle connect_pol, domain_pol;
1722 NTSTATUS status, result;
1723 uint32_t switch_level = 2;
1724 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1725 union samr_DomainInfo *info = NULL;
1726 struct dcerpc_binding_handle *b = cli->binding_handle;
1729 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1730 return NT_STATUS_OK;
1734 sscanf(argv[1], "%i", &switch_level);
1737 sscanf(argv[2], "%x", &access_mask);
1739 /* Get sam policy handle */
1741 status = rpccli_try_samr_connects(cli, mem_ctx,
1742 MAXIMUM_ALLOWED_ACCESS,
1744 if (!NT_STATUS_IS_OK(status)) {
1748 /* Get domain policy handle */
1750 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1756 if (!NT_STATUS_IS_OK(status)) {
1759 if (!NT_STATUS_IS_OK(result)) {
1764 /* Query domain info */
1766 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1771 if (!NT_STATUS_IS_OK(status)) {
1774 if (!NT_STATUS_IS_OK(result)) {
1779 /* Display domain info */
1781 switch (switch_level) {
1783 display_sam_dom_info_1(&info->info1);
1786 display_sam_dom_info_2(&info->general);
1789 display_sam_dom_info_3(&info->info3);
1792 display_sam_dom_info_4(&info->oem);
1795 display_sam_dom_info_5(&info->info5);
1798 display_sam_dom_info_6(&info->info6);
1801 display_sam_dom_info_7(&info->info7);
1804 display_sam_dom_info_8(&info->info8);
1807 display_sam_dom_info_9(&info->info9);
1810 display_sam_dom_info_12(&info->info12);
1813 display_sam_dom_info_13(&info->info13);
1817 printf("cannot display domain info for switch value %d\n",
1824 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1825 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1829 /* Create domain user */
1831 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1832 TALLOC_CTX *mem_ctx,
1833 int argc, const char **argv)
1835 struct policy_handle connect_pol, domain_pol, user_pol;
1836 NTSTATUS status, result;
1837 struct lsa_String acct_name;
1839 uint32_t acct_flags, user_rid;
1840 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1841 uint32_t access_granted = 0;
1842 struct dcerpc_binding_handle *b = cli->binding_handle;
1844 if ((argc < 2) || (argc > 3)) {
1845 printf("Usage: %s username [access mask]\n", argv[0]);
1846 return NT_STATUS_OK;
1849 init_lsa_String(&acct_name, argv[1]);
1852 sscanf(argv[2], "%x", &access_mask);
1854 /* Get sam policy handle */
1856 status = rpccli_try_samr_connects(cli, mem_ctx,
1857 MAXIMUM_ALLOWED_ACCESS,
1859 if (!NT_STATUS_IS_OK(status)) {
1863 /* Get domain policy handle */
1865 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1871 if (!NT_STATUS_IS_OK(status)) {
1874 if (!NT_STATUS_IS_OK(result)) {
1879 /* Create domain user */
1881 acb_info = ACB_NORMAL;
1882 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1883 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1884 SAMR_USER_ACCESS_SET_PASSWORD |
1885 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1886 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1888 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1897 if (!NT_STATUS_IS_OK(status)) {
1900 if (!NT_STATUS_IS_OK(result)) {
1905 status = dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1906 if (!NT_STATUS_IS_OK(status)) goto done;
1908 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1909 if (!NT_STATUS_IS_OK(status)) goto done;
1911 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1912 if (!NT_STATUS_IS_OK(status)) goto done;
1918 /* Create domain group */
1920 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1921 TALLOC_CTX *mem_ctx,
1922 int argc, const char **argv)
1924 struct policy_handle connect_pol, domain_pol, group_pol;
1925 NTSTATUS status, result;
1926 struct lsa_String grp_name;
1927 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1929 struct dcerpc_binding_handle *b = cli->binding_handle;
1931 if ((argc < 2) || (argc > 3)) {
1932 printf("Usage: %s groupname [access mask]\n", argv[0]);
1933 return NT_STATUS_OK;
1936 init_lsa_String(&grp_name, argv[1]);
1939 sscanf(argv[2], "%x", &access_mask);
1941 /* Get sam policy handle */
1943 status = rpccli_try_samr_connects(cli, mem_ctx,
1944 MAXIMUM_ALLOWED_ACCESS,
1946 if (!NT_STATUS_IS_OK(status)) {
1950 /* Get domain policy handle */
1952 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1958 if (!NT_STATUS_IS_OK(status)) {
1961 if (!NT_STATUS_IS_OK(result)) {
1966 /* Create domain user */
1967 status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
1970 MAXIMUM_ALLOWED_ACCESS,
1974 if (!NT_STATUS_IS_OK(status)) {
1977 if (!NT_STATUS_IS_OK(result)) {
1982 status = dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
1983 if (!NT_STATUS_IS_OK(status)) goto done;
1985 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1986 if (!NT_STATUS_IS_OK(status)) goto done;
1988 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1989 if (!NT_STATUS_IS_OK(status)) goto done;
1995 /* Create domain alias */
1997 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1998 TALLOC_CTX *mem_ctx,
1999 int argc, const char **argv)
2001 struct policy_handle connect_pol, domain_pol, alias_pol;
2002 NTSTATUS status, result;
2003 struct lsa_String alias_name;
2004 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2006 struct dcerpc_binding_handle *b = cli->binding_handle;
2008 if ((argc < 2) || (argc > 3)) {
2009 printf("Usage: %s aliasname [access mask]\n", argv[0]);
2010 return NT_STATUS_OK;
2013 init_lsa_String(&alias_name, argv[1]);
2016 sscanf(argv[2], "%x", &access_mask);
2018 /* Get sam policy handle */
2020 status = rpccli_try_samr_connects(cli, mem_ctx,
2021 MAXIMUM_ALLOWED_ACCESS,
2023 if (!NT_STATUS_IS_OK(status)) {
2027 /* Get domain policy handle */
2029 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2035 if (!NT_STATUS_IS_OK(status)) {
2038 if (!NT_STATUS_IS_OK(result)) {
2043 /* Create domain user */
2045 status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2048 MAXIMUM_ALLOWED_ACCESS,
2052 if (!NT_STATUS_IS_OK(status)) {
2055 if (!NT_STATUS_IS_OK(result)) {
2061 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
2062 if (!NT_STATUS_IS_OK(status)) goto done;
2064 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2065 if (!NT_STATUS_IS_OK(status)) goto done;
2067 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2068 if (!NT_STATUS_IS_OK(status)) goto done;
2074 /* Lookup sam names */
2076 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
2077 TALLOC_CTX *mem_ctx,
2078 int argc, const char **argv)
2080 NTSTATUS status, result;
2081 struct policy_handle connect_pol, domain_pol;
2083 struct samr_Ids rids, name_types;
2085 struct lsa_String *names = NULL;
2086 struct dcerpc_binding_handle *b = cli->binding_handle;
2089 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
2090 printf("check on the domain SID: S-1-5-21-x-y-z\n");
2091 printf("or check on the builtin SID: S-1-5-32\n");
2092 return NT_STATUS_OK;
2095 /* Get sam policy and domain handles */
2097 status = rpccli_try_samr_connects(cli, mem_ctx,
2098 MAXIMUM_ALLOWED_ACCESS,
2100 if (!NT_STATUS_IS_OK(status)) {
2104 status = get_domain_handle(cli, mem_ctx, argv[1],
2106 MAXIMUM_ALLOWED_ACCESS,
2109 if (!NT_STATUS_IS_OK(status)) {
2115 num_names = argc - 2;
2117 if ((names = talloc_array(mem_ctx, struct lsa_String, num_names)) == NULL) {
2118 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2119 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2120 status = NT_STATUS_NO_MEMORY;
2124 for (i = 0; i < num_names; i++) {
2125 init_lsa_String(&names[i], argv[i + 2]);
2128 status = dcerpc_samr_LookupNames(b, mem_ctx,
2135 if (!NT_STATUS_IS_OK(status)) {
2138 if (!NT_STATUS_IS_OK(result)) {
2142 if (rids.count != num_names) {
2143 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2146 if (name_types.count != num_names) {
2147 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2151 /* Display results */
2153 for (i = 0; i < num_names; i++)
2154 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
2157 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2158 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2163 /* Lookup sam rids */
2165 static NTSTATUS cmd_samr_lookup_rids(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;
2171 uint32_t num_rids, *rids;
2172 struct lsa_Strings names;
2173 struct samr_Ids types;
2174 struct dcerpc_binding_handle *b = cli->binding_handle;
2179 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
2180 return NT_STATUS_OK;
2183 /* Get sam policy and domain handles */
2185 status = rpccli_try_samr_connects(cli, mem_ctx,
2186 MAXIMUM_ALLOWED_ACCESS,
2188 if (!NT_STATUS_IS_OK(status)) {
2192 status = get_domain_handle(cli, mem_ctx, argv[1],
2194 MAXIMUM_ALLOWED_ACCESS,
2197 if (!NT_STATUS_IS_OK(status)) {
2203 num_rids = argc - 2;
2205 if ((rids = talloc_array(mem_ctx, uint32_t, num_rids)) == NULL) {
2206 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2207 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2208 status = NT_STATUS_NO_MEMORY;
2212 for (i = 0; i < argc - 2; i++)
2213 sscanf(argv[i + 2], "%i", &rids[i]);
2215 status = dcerpc_samr_LookupRids(b, mem_ctx,
2222 if (!NT_STATUS_IS_OK(status)) {
2226 if (!NT_STATUS_IS_OK(result) &&
2227 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2230 /* Display results */
2231 if (num_rids != names.count) {
2232 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2235 if (num_rids != types.count) {
2236 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2240 for (i = 0; i < num_rids; i++) {
2241 printf("rid 0x%x: %s (%d)\n",
2242 rids[i], names.names[i].string, types.ids[i]);
2245 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2246 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2251 /* Delete domain group */
2253 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
2254 TALLOC_CTX *mem_ctx,
2255 int argc, const char **argv)
2257 NTSTATUS status, result;
2258 struct policy_handle connect_pol, domain_pol, group_pol;
2259 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2260 struct dcerpc_binding_handle *b = cli->binding_handle;
2262 if ((argc < 2) || (argc > 3)) {
2263 printf("Usage: %s groupname\n", argv[0]);
2264 return NT_STATUS_OK;
2268 sscanf(argv[2], "%x", &access_mask);
2270 /* Get sam policy and domain handles */
2272 status = rpccli_try_samr_connects(cli, mem_ctx,
2273 MAXIMUM_ALLOWED_ACCESS,
2275 if (!NT_STATUS_IS_OK(status)) {
2279 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2281 MAXIMUM_ALLOWED_ACCESS,
2285 if (!NT_STATUS_IS_OK(status)) {
2288 if (!NT_STATUS_IS_OK(result)) {
2293 /* Get handle on group */
2296 struct samr_Ids group_rids, name_types;
2297 struct lsa_String lsa_acct_name;
2299 init_lsa_String(&lsa_acct_name, argv[1]);
2301 status = dcerpc_samr_LookupNames(b, mem_ctx,
2308 if (!NT_STATUS_IS_OK(status)) {
2311 if (!NT_STATUS_IS_OK(result)) {
2315 if (group_rids.count != 1) {
2316 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2319 if (name_types.count != 1) {
2320 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2324 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2330 if (!NT_STATUS_IS_OK(status)) {
2333 if (!NT_STATUS_IS_OK(result)) {
2341 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2344 if (!NT_STATUS_IS_OK(status)) {
2347 if (!NT_STATUS_IS_OK(result)) {
2352 /* Display results */
2354 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2355 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2356 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2362 /* Delete domain user */
2364 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2365 TALLOC_CTX *mem_ctx,
2366 int argc, const char **argv)
2368 NTSTATUS status, result;
2369 struct policy_handle connect_pol, domain_pol, user_pol;
2370 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2371 struct dcerpc_binding_handle *b = cli->binding_handle;
2373 if ((argc < 2) || (argc > 3)) {
2374 printf("Usage: %s username\n", argv[0]);
2375 return NT_STATUS_OK;
2379 sscanf(argv[2], "%x", &access_mask);
2381 /* Get sam policy and domain handles */
2383 status = rpccli_try_samr_connects(cli, mem_ctx,
2384 MAXIMUM_ALLOWED_ACCESS,
2386 if (!NT_STATUS_IS_OK(status)) {
2390 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2392 MAXIMUM_ALLOWED_ACCESS,
2396 if (!NT_STATUS_IS_OK(status)) {
2399 if (!NT_STATUS_IS_OK(result)) {
2404 /* Get handle on user */
2407 struct samr_Ids user_rids, name_types;
2408 struct lsa_String lsa_acct_name;
2410 init_lsa_String(&lsa_acct_name, argv[1]);
2412 status = dcerpc_samr_LookupNames(b, mem_ctx,
2419 if (!NT_STATUS_IS_OK(status)) {
2422 if (!NT_STATUS_IS_OK(result)) {
2426 if (user_rids.count != 1) {
2427 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2430 if (name_types.count != 1) {
2431 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2435 status = dcerpc_samr_OpenUser(b, mem_ctx,
2441 if (!NT_STATUS_IS_OK(status)) {
2444 if (!NT_STATUS_IS_OK(result)) {
2452 status = dcerpc_samr_DeleteUser(b, mem_ctx,
2455 if (!NT_STATUS_IS_OK(status)) {
2458 if (!NT_STATUS_IS_OK(result)) {
2463 /* Display results */
2465 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2466 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2467 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2473 /**********************************************************************
2474 * Query user security object
2476 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2477 TALLOC_CTX *mem_ctx,
2478 int argc, const char **argv)
2480 struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2481 NTSTATUS status, result;
2482 uint32_t sec_info = SECINFO_DACL;
2483 uint32_t user_rid = 0;
2484 TALLOC_CTX *ctx = NULL;
2485 struct sec_desc_buf *sec_desc_buf=NULL;
2486 bool domain = False;
2487 struct dcerpc_binding_handle *b = cli->binding_handle;
2489 ctx=talloc_init("cmd_samr_query_sec_obj");
2491 if ((argc < 1) || (argc > 3)) {
2492 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2493 printf("\tSpecify rid for security on user, -d for security on domain\n");
2494 talloc_destroy(ctx);
2495 return NT_STATUS_OK;
2499 if (strcmp(argv[1], "-d") == 0)
2502 sscanf(argv[1], "%i", &user_rid);
2506 sec_info = atoi(argv[2]);
2509 status = rpccli_try_samr_connects(cli, mem_ctx,
2510 MAXIMUM_ALLOWED_ACCESS,
2512 if (!NT_STATUS_IS_OK(status)) {
2516 if (domain || user_rid) {
2517 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2519 MAXIMUM_ALLOWED_ACCESS,
2523 if (!NT_STATUS_IS_OK(status)) {
2526 if (!NT_STATUS_IS_OK(result)) {
2533 status = dcerpc_samr_OpenUser(b, mem_ctx,
2535 MAXIMUM_ALLOWED_ACCESS,
2539 if (!NT_STATUS_IS_OK(status)) {
2542 if (!NT_STATUS_IS_OK(result)) {
2548 /* Pick which query pol to use */
2558 /* Query SAM security object */
2560 status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2565 if (!NT_STATUS_IS_OK(status)) {
2568 if (!NT_STATUS_IS_OK(result)) {
2573 display_sec_desc(sec_desc_buf->sd);
2575 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2576 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2577 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2579 talloc_destroy(ctx);
2583 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2584 TALLOC_CTX *mem_ctx,
2585 int argc, const char **argv)
2587 NTSTATUS status, result;
2588 struct policy_handle connect_pol, domain_pol, user_pol;
2589 struct samr_PwInfo info;
2591 struct dcerpc_binding_handle *b = cli->binding_handle;
2594 printf("Usage: %s rid\n", argv[0]);
2595 return NT_STATUS_OK;
2598 sscanf(argv[1], "%i", &rid);
2600 status = rpccli_try_samr_connects(cli, mem_ctx,
2601 MAXIMUM_ALLOWED_ACCESS,
2603 if (!NT_STATUS_IS_OK(status)) {
2607 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2609 MAXIMUM_ALLOWED_ACCESS,
2613 if (!NT_STATUS_IS_OK(status)) {
2616 if (!NT_STATUS_IS_OK(result)) {
2621 status = dcerpc_samr_OpenUser(b, mem_ctx,
2623 MAXIMUM_ALLOWED_ACCESS,
2627 if (!NT_STATUS_IS_OK(status)) {
2630 if (!NT_STATUS_IS_OK(result)) {
2635 status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2639 if (!NT_STATUS_IS_OK(status)) {
2643 if (NT_STATUS_IS_OK(result)) {
2644 printf("min_password_length: %d\n", info.min_password_length);
2646 NDR_PRINT_STRUCT_STRING(mem_ctx,
2647 samr_PasswordProperties, &info.password_properties));
2651 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2652 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2653 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2658 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2659 TALLOC_CTX *mem_ctx,
2660 int argc, const char **argv)
2662 NTSTATUS status, result;
2663 struct lsa_String domain_name;
2664 struct samr_PwInfo info;
2665 struct dcerpc_binding_handle *b = cli->binding_handle;
2667 if (argc < 1 || argc > 3) {
2668 printf("Usage: %s <domain>\n", argv[0]);
2669 return NT_STATUS_OK;
2672 init_lsa_String(&domain_name, argv[1]);
2674 status = dcerpc_samr_GetDomPwInfo(b, mem_ctx,
2678 if (!NT_STATUS_IS_OK(status)) {
2681 if (NT_STATUS_IS_OK(result)) {
2682 printf("min_password_length: %d\n", info.min_password_length);
2683 display_password_properties(info.password_properties);
2689 /* Look up domain name */
2691 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2692 TALLOC_CTX *mem_ctx,
2693 int argc, const char **argv)
2695 struct policy_handle connect_pol, domain_pol;
2696 NTSTATUS status, result;
2697 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2698 struct lsa_String domain_name;
2699 struct dom_sid *sid = NULL;
2700 struct dcerpc_binding_handle *b = cli->binding_handle;
2703 printf("Usage: %s domain_name\n", argv[0]);
2704 return NT_STATUS_OK;
2707 init_lsa_String(&domain_name, argv[1]);
2709 status = rpccli_try_samr_connects(cli, mem_ctx,
2712 if (!NT_STATUS_IS_OK(status)) {
2716 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2722 if (!NT_STATUS_IS_OK(status)) {
2725 if (!NT_STATUS_IS_OK(result)) {
2730 status = dcerpc_samr_LookupDomain(b, mem_ctx,
2735 if (!NT_STATUS_IS_OK(status)) {
2738 if (!NT_STATUS_IS_OK(result)) {
2743 if (NT_STATUS_IS_OK(result)) {
2744 struct dom_sid_buf sid_str;
2745 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2747 dom_sid_str_buf(sid, &sid_str));
2750 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2751 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2756 /* Change user password */
2758 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2759 TALLOC_CTX *mem_ctx,
2760 int argc, const char **argv)
2762 struct policy_handle connect_pol;
2763 struct policy_handle domain_pol = { 0, };
2764 struct policy_handle user_pol = { 0, };
2765 NTSTATUS status, result;
2766 const char *user, *oldpass, *newpass;
2767 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2768 struct samr_Ids rids, types;
2769 struct lsa_String lsa_acct_name;
2770 struct dcerpc_binding_handle *b = cli->binding_handle;
2773 printf("Usage: %s username oldpass newpass\n", argv[0]);
2774 return NT_STATUS_INVALID_PARAMETER;
2781 /* Get sam policy handle */
2783 status = rpccli_try_samr_connects(cli, mem_ctx,
2784 MAXIMUM_ALLOWED_ACCESS,
2786 if (!NT_STATUS_IS_OK(status)) {
2790 /* Get domain policy handle */
2792 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2798 if (!NT_STATUS_IS_OK(status)) {
2801 if (!NT_STATUS_IS_OK(result)) {
2806 init_lsa_String(&lsa_acct_name, user);
2808 status = dcerpc_samr_LookupNames(b, mem_ctx,
2815 if (!NT_STATUS_IS_OK(status)) {
2818 if (!NT_STATUS_IS_OK(result)) {
2822 if (rids.count != 1) {
2823 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2826 if (types.count != 1) {
2827 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2831 status = dcerpc_samr_OpenUser(b, mem_ctx,
2837 if (!NT_STATUS_IS_OK(status)) {
2840 if (!NT_STATUS_IS_OK(result)) {
2845 /* Change user password */
2846 status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2850 if (!NT_STATUS_IS_OK(status)) {
2855 if (is_valid_policy_hnd(&user_pol)) {
2856 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2858 if (is_valid_policy_hnd(&domain_pol)) {
2859 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2861 if (is_valid_policy_hnd(&connect_pol)) {
2862 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2869 /* Change user password */
2871 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2872 TALLOC_CTX *mem_ctx,
2873 int argc, const char **argv)
2875 struct policy_handle connect_pol, domain_pol;
2876 NTSTATUS status, result;
2877 const char *user, *oldpass, *newpass;
2878 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2879 struct dcerpc_binding_handle *b = cli->binding_handle;
2882 printf("Usage: %s username oldpass newpass\n", argv[0]);
2883 return NT_STATUS_INVALID_PARAMETER;
2890 /* Get sam policy handle */
2892 status = rpccli_try_samr_connects(cli, mem_ctx,
2893 MAXIMUM_ALLOWED_ACCESS,
2895 if (!NT_STATUS_IS_OK(status)) {
2899 /* Get domain policy handle */
2901 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2907 if (!NT_STATUS_IS_OK(status)) {
2910 if (!NT_STATUS_IS_OK(result)) {
2915 /* Change user password */
2916 status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2918 if (!NT_STATUS_IS_OK(status)) {
2922 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2923 if (!NT_STATUS_IS_OK(status)) goto done;
2925 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2926 if (!NT_STATUS_IS_OK(status)) goto done;
2933 /* Change user password */
2935 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2936 TALLOC_CTX *mem_ctx,
2937 int argc, const char **argv)
2939 struct policy_handle connect_pol, domain_pol;
2940 NTSTATUS status, result;
2941 const char *user, *oldpass, *newpass;
2942 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2943 struct samr_DomInfo1 *info = NULL;
2944 struct userPwdChangeFailureInformation *reject = NULL;
2945 struct dcerpc_binding_handle *b = cli->binding_handle;
2948 printf("Usage: %s username oldpass newpass\n", argv[0]);
2949 return NT_STATUS_INVALID_PARAMETER;
2956 /* Get sam policy handle */
2958 status = rpccli_try_samr_connects(cli, mem_ctx,
2959 MAXIMUM_ALLOWED_ACCESS,
2961 if (!NT_STATUS_IS_OK(status)) {
2965 /* Get domain policy handle */
2967 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2973 if (!NT_STATUS_IS_OK(status)) {
2976 if (!NT_STATUS_IS_OK(result)) {
2981 /* Change user password */
2982 status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2988 if (!NT_STATUS_IS_OK(status)) {
2992 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2994 display_sam_dom_info_1(info);
2996 switch (reject->extendedFailureReason) {
2997 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
2998 d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
3000 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
3001 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
3003 case SAM_PWD_CHANGE_NOT_COMPLEX:
3004 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
3007 d_printf("unknown reject reason: %d\n",
3008 reject->extendedFailureReason);
3013 if (!NT_STATUS_IS_OK(result)) {
3018 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3019 if (!NT_STATUS_IS_OK(status)) goto done;
3021 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
3022 if (!NT_STATUS_IS_OK(status)) goto done;
3028 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
3029 TALLOC_CTX *mem_ctx,
3030 int argc, const char **argv,
3033 struct policy_handle connect_pol, domain_pol, user_pol;
3034 NTSTATUS status, result;
3035 const char *user, *param;
3036 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
3039 union samr_UserInfo info;
3040 struct samr_CryptPassword pwd_buf;
3041 struct samr_CryptPasswordEx pwd_buf_ex;
3042 uint8_t nt_hash[16];
3043 uint8_t lm_hash[16];
3044 DATA_BLOB session_key;
3045 uint8_t password_expired = 0;
3046 struct dcerpc_binding_handle *b = cli->binding_handle;
3049 printf("Usage: %s username level password [password_expired]\n",
3051 return NT_STATUS_INVALID_PARAMETER;
3055 level = atoi(argv[2]);
3059 password_expired = atoi(argv[4]);
3062 status = cli_get_session_key(mem_ctx, cli, &session_key);
3063 if (!NT_STATUS_IS_OK(status)) {
3067 init_samr_CryptPassword(param, &session_key, &pwd_buf);
3068 init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
3069 nt_lm_owf_gen(param, nt_hash, lm_hash);
3075 in = data_blob_const(nt_hash, 16);
3076 out = data_blob_talloc_zero(mem_ctx, 16);
3077 sess_crypt_blob(&out, &in, &session_key, true);
3078 memcpy(nt_hash, out.data, out.length);
3082 in = data_blob_const(lm_hash, 16);
3083 out = data_blob_talloc_zero(mem_ctx, 16);
3084 sess_crypt_blob(&out, &in, &session_key, true);
3085 memcpy(lm_hash, out.data, out.length);
3088 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
3089 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
3090 info.info18.nt_pwd_active = true;
3091 info.info18.lm_pwd_active = true;
3092 info.info18.password_expired = password_expired;
3096 ZERO_STRUCT(info.info21);
3098 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3099 SAMR_FIELD_LM_PASSWORD_PRESENT;
3101 info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3102 info.info21.password_expired = password_expired;
3105 info.info21.lm_password_set = true;
3106 info.info21.lm_owf_password.length = 16;
3107 info.info21.lm_owf_password.size = 16;
3109 info.info21.nt_password_set = true;
3110 info.info21.nt_owf_password.length = 16;
3111 info.info21.nt_owf_password.size = 16;
3115 in = data_blob_const(nt_hash, 16);
3116 out = data_blob_talloc_zero(mem_ctx, 16);
3117 sess_crypt_blob(&out, &in, &session_key, true);
3118 info.info21.nt_owf_password.array =
3119 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3123 in = data_blob_const(lm_hash, 16);
3124 out = data_blob_talloc_zero(mem_ctx, 16);
3125 sess_crypt_blob(&out, &in, &session_key, true);
3126 info.info21.lm_owf_password.array =
3127 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3132 ZERO_STRUCT(info.info23);
3134 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3135 SAMR_FIELD_LM_PASSWORD_PRESENT;
3137 info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3138 info.info23.info.password_expired = password_expired;
3141 info.info23.password = pwd_buf;
3145 info.info24.password = pwd_buf;
3146 info.info24.password_expired = password_expired;
3150 ZERO_STRUCT(info.info25);
3152 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3153 SAMR_FIELD_LM_PASSWORD_PRESENT;
3155 info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3156 info.info25.info.password_expired = password_expired;
3159 info.info25.password = pwd_buf_ex;
3163 info.info26.password = pwd_buf_ex;
3164 info.info26.password_expired = password_expired;
3168 return NT_STATUS_INVALID_INFO_CLASS;
3171 /* Get sam policy handle */
3173 status = rpccli_try_samr_connects(cli, mem_ctx,
3174 MAXIMUM_ALLOWED_ACCESS,
3176 if (!NT_STATUS_IS_OK(status)) {
3180 /* Get domain policy handle */
3182 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3189 if (!NT_STATUS_IS_OK(status))
3191 if (!NT_STATUS_IS_OK(result)) {
3196 user_rid = strtol(user, NULL, 0);
3198 status = dcerpc_samr_OpenUser(b, mem_ctx,
3204 if (!NT_STATUS_IS_OK(status)) {
3211 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3214 /* Probably this was a user name, try lookupnames */
3215 struct samr_Ids rids, types;
3216 struct lsa_String lsa_acct_name;
3218 init_lsa_String(&lsa_acct_name, user);
3220 status = dcerpc_samr_LookupNames(b, mem_ctx,
3227 if (!NT_STATUS_IS_OK(status)) {
3230 if (!NT_STATUS_IS_OK(result)) {
3233 if (rids.count != 1) {
3234 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3236 if (types.count != 1) {
3237 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3240 status = dcerpc_samr_OpenUser(b, mem_ctx,
3246 if (!NT_STATUS_IS_OK(status)) {
3249 if (!NT_STATUS_IS_OK(result)) {
3255 case NDR_SAMR_SETUSERINFO:
3256 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
3262 case NDR_SAMR_SETUSERINFO2:
3263 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
3270 return NT_STATUS_INVALID_PARAMETER;
3272 if (!NT_STATUS_IS_OK(status)) {
3273 DEBUG(0,("status: %s\n", nt_errstr(status)));
3276 if (!NT_STATUS_IS_OK(result)) {
3278 DEBUG(0,("result: %s\n", nt_errstr(status)));
3285 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
3286 TALLOC_CTX *mem_ctx,
3287 int argc, const char **argv)
3289 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3290 NDR_SAMR_SETUSERINFO);
3293 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
3294 TALLOC_CTX *mem_ctx,
3295 int argc, const char **argv)
3297 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3298 NDR_SAMR_SETUSERINFO2);
3301 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
3302 TALLOC_CTX *mem_ctx,
3303 int argc, const char **argv)
3305 NTSTATUS status, result;
3306 struct policy_handle connect_handle;
3307 struct policy_handle domain_handle = { 0, };
3309 struct lsa_String name;
3311 struct dcerpc_binding_handle *b = cli->binding_handle;
3313 if (argc < 2 || argc > 3) {
3314 printf("Usage: %s name level\n", argv[0]);
3315 return NT_STATUS_INVALID_PARAMETER;
3318 init_lsa_String(&name, argv[1]);
3321 level = atoi(argv[2]);
3324 status = rpccli_try_samr_connects(cli, mem_ctx,
3325 SEC_FLAG_MAXIMUM_ALLOWED,
3327 if (!NT_STATUS_IS_OK(status)) {
3331 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3333 SEC_FLAG_MAXIMUM_ALLOWED,
3337 if (!NT_STATUS_IS_OK(status)) {
3340 if (!NT_STATUS_IS_OK(result)) {
3345 status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3351 if (!NT_STATUS_IS_OK(status)) {
3357 if (NT_STATUS_IS_OK(status) ||
3358 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
3359 printf("idx: %d (0x%08x)\n", idx, idx);
3363 if (is_valid_policy_hnd(&domain_handle)) {
3364 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
3366 if (is_valid_policy_hnd(&connect_handle)) {
3367 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
3373 /* List of commands exported by this module */
3375 struct cmd_set samr_commands[] = {
3382 .name = "queryuser",
3383 .returntype = RPC_RTYPE_NTSTATUS,
3384 .ntfn = cmd_samr_query_user,
3386 .table = &ndr_table_samr,
3388 .description = "Query user info",
3392 .name = "querygroup",
3393 .returntype = RPC_RTYPE_NTSTATUS,
3394 .ntfn = cmd_samr_query_group,
3396 .table = &ndr_table_samr,
3398 .description = "Query group info",
3402 .name = "queryusergroups",
3403 .returntype = RPC_RTYPE_NTSTATUS,
3404 .ntfn = cmd_samr_query_usergroups,
3406 .table = &ndr_table_samr,
3408 .description = "Query user groups",
3412 .name = "queryuseraliases",
3413 .returntype = RPC_RTYPE_NTSTATUS,
3414 .ntfn = cmd_samr_query_useraliases,
3416 .table = &ndr_table_samr,
3418 .description = "Query user aliases",
3422 .name = "querygroupmem",
3423 .returntype = RPC_RTYPE_NTSTATUS,
3424 .ntfn = cmd_samr_query_groupmem,
3426 .table = &ndr_table_samr,
3428 .description = "Query group membership",
3432 .name = "queryaliasmem",
3433 .returntype = RPC_RTYPE_NTSTATUS,
3434 .ntfn = cmd_samr_query_aliasmem,
3436 .table = &ndr_table_samr,
3438 .description = "Query alias membership",
3442 .name = "queryaliasinfo",
3443 .returntype = RPC_RTYPE_NTSTATUS,
3444 .ntfn = cmd_samr_query_aliasinfo,
3446 .table = &ndr_table_samr,
3448 .description = "Query alias info",
3452 .name = "deletealias",
3453 .returntype = RPC_RTYPE_NTSTATUS,
3454 .ntfn = cmd_samr_delete_alias,
3456 .table = &ndr_table_samr,
3458 .description = "Delete an alias",
3462 .name = "querydispinfo",
3463 .returntype = RPC_RTYPE_NTSTATUS,
3464 .ntfn = cmd_samr_query_dispinfo,
3466 .table = &ndr_table_samr,
3468 .description = "Query display info",
3472 .name = "querydispinfo2",
3473 .returntype = RPC_RTYPE_NTSTATUS,
3474 .ntfn = cmd_samr_query_dispinfo2,
3476 .table = &ndr_table_samr,
3478 .description = "Query display info",
3482 .name = "querydispinfo3",
3483 .returntype = RPC_RTYPE_NTSTATUS,
3484 .ntfn = cmd_samr_query_dispinfo3,
3486 .table = &ndr_table_samr,
3488 .description = "Query display info",
3492 .name = "querydominfo",
3493 .returntype = RPC_RTYPE_NTSTATUS,
3494 .ntfn = cmd_samr_query_dominfo,
3496 .table = &ndr_table_samr,
3498 .description = "Query domain info",
3502 .name = "enumdomusers",
3503 .returntype = RPC_RTYPE_NTSTATUS,
3504 .ntfn = cmd_samr_enum_dom_users,
3506 .table = &ndr_table_samr,
3508 .description = "Enumerate domain users",
3512 .name = "enumdomgroups",
3513 .returntype = RPC_RTYPE_NTSTATUS,
3514 .ntfn = cmd_samr_enum_dom_groups,
3516 .table = &ndr_table_samr,
3518 .description = "Enumerate domain groups",
3522 .name = "enumalsgroups",
3523 .returntype = RPC_RTYPE_NTSTATUS,
3524 .ntfn = cmd_samr_enum_als_groups,
3526 .table = &ndr_table_samr,
3528 .description = "Enumerate alias groups",
3532 .name = "enumdomains",
3533 .returntype = RPC_RTYPE_NTSTATUS,
3534 .ntfn = cmd_samr_enum_domains,
3536 .table = &ndr_table_samr,
3538 .description = "Enumerate domains",
3543 .name = "createdomuser",
3544 .returntype = RPC_RTYPE_NTSTATUS,
3545 .ntfn = cmd_samr_create_dom_user,
3547 .table = &ndr_table_samr,
3549 .description = "Create domain user",
3553 .name = "createdomgroup",
3554 .returntype = RPC_RTYPE_NTSTATUS,
3555 .ntfn = cmd_samr_create_dom_group,
3557 .table = &ndr_table_samr,
3559 .description = "Create domain group",
3563 .name = "createdomalias",
3564 .returntype = RPC_RTYPE_NTSTATUS,
3565 .ntfn = cmd_samr_create_dom_alias,
3567 .table = &ndr_table_samr,
3569 .description = "Create domain alias",
3573 .name = "samlookupnames",
3574 .returntype = RPC_RTYPE_NTSTATUS,
3575 .ntfn = cmd_samr_lookup_names,
3577 .table = &ndr_table_samr,
3579 .description = "Look up names",
3583 .name = "samlookuprids",
3584 .returntype = RPC_RTYPE_NTSTATUS,
3585 .ntfn = cmd_samr_lookup_rids,
3587 .table = &ndr_table_samr,
3589 .description = "Look up names",
3593 .name = "deletedomgroup",
3594 .returntype = RPC_RTYPE_NTSTATUS,
3595 .ntfn = cmd_samr_delete_dom_group,
3597 .table = &ndr_table_samr,
3599 .description = "Delete domain group",
3603 .name = "deletedomuser",
3604 .returntype = RPC_RTYPE_NTSTATUS,
3605 .ntfn = cmd_samr_delete_dom_user,
3607 .table = &ndr_table_samr,
3609 .description = "Delete domain user",
3613 .name = "samquerysecobj",
3614 .returntype = RPC_RTYPE_NTSTATUS,
3615 .ntfn = cmd_samr_query_sec_obj,
3617 .table = &ndr_table_samr,
3619 .description = "Query SAMR security object",
3623 .name = "getdompwinfo",
3624 .returntype = RPC_RTYPE_NTSTATUS,
3625 .ntfn = cmd_samr_get_dom_pwinfo,
3627 .table = &ndr_table_samr,
3629 .description = "Retrieve domain password info",
3633 .name = "getusrdompwinfo",
3634 .returntype = RPC_RTYPE_NTSTATUS,
3635 .ntfn = cmd_samr_get_usrdom_pwinfo,
3637 .table = &ndr_table_samr,
3639 .description = "Retrieve user domain password info",
3644 .name = "lookupdomain",
3645 .returntype = RPC_RTYPE_NTSTATUS,
3646 .ntfn = cmd_samr_lookup_domain,
3648 .table = &ndr_table_samr,
3650 .description = "Lookup Domain Name",
3654 .name = "chgpasswd",
3655 .returntype = RPC_RTYPE_NTSTATUS,
3656 .ntfn = cmd_samr_chgpasswd,
3658 .table = &ndr_table_samr,
3660 .description = "Change user password",
3664 .name = "chgpasswd2",
3665 .returntype = RPC_RTYPE_NTSTATUS,
3666 .ntfn = cmd_samr_chgpasswd2,
3668 .table = &ndr_table_samr,
3670 .description = "Change user password",
3674 .name = "chgpasswd3",
3675 .returntype = RPC_RTYPE_NTSTATUS,
3676 .ntfn = cmd_samr_chgpasswd3,
3678 .table = &ndr_table_samr,
3680 .description = "Change user password",
3684 .name = "getdispinfoidx",
3685 .returntype = RPC_RTYPE_NTSTATUS,
3686 .ntfn = cmd_samr_get_dispinfo_idx,
3688 .table = &ndr_table_samr,
3690 .description = "Get Display Information Index",
3694 .name = "setuserinfo",
3695 .returntype = RPC_RTYPE_NTSTATUS,
3696 .ntfn = cmd_samr_setuserinfo,
3698 .table = &ndr_table_samr,
3700 .description = "Set user info",
3704 .name = "setuserinfo2",
3705 .returntype = RPC_RTYPE_NTSTATUS,
3706 .ntfn = cmd_samr_setuserinfo2,
3708 .table = &ndr_table_samr,
3710 .description = "Set user info2",