s3:libsmb/cliconnect: make use of ntlmssp_is_anonymous()
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_samr.c
blobab263e765f86b2ccfef51ec0c5678c8dc2dde554
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Andrew Tridgell 1992-2000,
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
7 Copyright (C) Elrond 2000,
8 Copyright (C) Tim Potter 2000
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/>.
25 #include "includes.h"
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,
277 TALLOC_CTX *mem_ctx,
278 const char *sam,
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,
289 connect_pol,
290 access_mask,
291 _domain_sid,
292 domain_pol,
293 &result);
294 } else if (strcasecmp_m(sam, "builtin") == 0) {
295 status = dcerpc_samr_OpenDomain(b, mem_ctx,
296 connect_pol,
297 access_mask,
298 discard_const_p(struct dom_sid2, &global_sid_Builtin),
299 domain_pol,
300 &result);
303 if (!NT_STATUS_IS_OK(status)) {
304 return status;
307 return result;
310 /**********************************************************************
311 * Query user information
313 static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
314 TALLOC_CTX *mem_ctx,
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;
322 uint32 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]);
327 return NT_STATUS_OK;
330 sscanf(argv[1], "%i", &user_rid);
332 if (argc > 2)
333 sscanf(argv[2], "%i", &info_level);
335 if (argc > 3)
336 sscanf(argv[3], "%x", &access_mask);
339 status = rpccli_try_samr_connects(cli, mem_ctx,
340 MAXIMUM_ALLOWED_ACCESS,
341 &connect_pol);
342 if (!NT_STATUS_IS_OK(status)) {
343 goto done;
346 status = dcerpc_samr_OpenDomain(b, mem_ctx,
347 &connect_pol,
348 MAXIMUM_ALLOWED_ACCESS,
349 &domain_sid,
350 &domain_pol,
351 &result);
352 if (!NT_STATUS_IS_OK(status)) {
353 goto done;
355 if (!NT_STATUS_IS_OK(result)) {
356 status = result;
357 goto done;
360 status = dcerpc_samr_OpenUser(b, mem_ctx,
361 &domain_pol,
362 access_mask,
363 user_rid,
364 &user_pol,
365 &result);
366 if (!NT_STATUS_IS_OK(status)) {
367 goto done;
369 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) &&
370 (user_rid == 0)) {
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,
379 &domain_pol,
381 &lsa_acct_name,
382 &rids,
383 &types,
384 &result);
385 if (!NT_STATUS_IS_OK(status)) {
386 goto done;
388 if (NT_STATUS_IS_OK(result)) {
389 status = dcerpc_samr_OpenUser(b, mem_ctx,
390 &domain_pol,
391 access_mask,
392 rids.ids[0],
393 &user_pol,
394 &result);
395 if (!NT_STATUS_IS_OK(status)) {
396 goto done;
402 if (!NT_STATUS_IS_OK(result)) {
403 status = result;
404 goto done;
407 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
408 &user_pol,
409 info_level,
410 &info,
411 &result);
412 if (!NT_STATUS_IS_OK(status)) {
413 goto done;
415 if (!NT_STATUS_IS_OK(result)) {
416 status = result;
417 goto done;
420 switch (info_level) {
421 case 7:
422 display_samr_user_info_7(&info->info7);
423 break;
424 case 9:
425 display_samr_user_info_9(&info->info9);
426 break;
427 case 16:
428 display_samr_user_info_16(&info->info16);
429 break;
430 case 20:
431 display_samr_user_info_20(&info->info20);
432 break;
433 case 21:
434 display_samr_user_info_21(&info->info21);
435 break;
436 default:
437 printf("Unsupported infolevel: %d\n", info_level);
438 break;
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);
445 done:
446 return status;
449 /****************************************************************************
450 display group info
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 /****************************************************************************
461 display group info
462 ****************************************************************************/
463 static void display_group_info2(struct lsa_String *info2)
465 printf("\tGroup Description:%s\n", info2->string);
469 /****************************************************************************
470 display group info
471 ****************************************************************************/
472 static void display_group_info3(struct samr_GroupInfoAttributes *info3)
474 printf("\tGroup Attribute:%d\n", info3->attributes);
478 /****************************************************************************
479 display group info
480 ****************************************************************************/
481 static void display_group_info4(struct lsa_String *info4)
483 printf("\tGroup Description:%s\n", info4->string);
486 /****************************************************************************
487 display group info
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)
503 switch (level) {
504 case 1:
505 display_group_info1(&info->all);
506 break;
507 case 2:
508 display_group_info2(&info->name);
509 break;
510 case 3:
511 display_group_info3(&info->attributes);
512 break;
513 case 4:
514 display_group_info4(&info->description);
515 break;
516 case 5:
517 display_group_info5(&info->all2);
518 break;
522 /***********************************************************************
523 * Query group information
525 static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
526 TALLOC_CTX *mem_ctx,
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;
534 uint32 group_rid;
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]);
539 return NT_STATUS_OK;
542 sscanf(argv[1], "%i", &group_rid);
544 if (argc > 2)
545 info_level = atoi(argv[2]);
547 if (argc > 3)
548 sscanf(argv[3], "%x", &access_mask);
550 status = rpccli_try_samr_connects(cli, mem_ctx,
551 MAXIMUM_ALLOWED_ACCESS,
552 &connect_pol);
553 if (!NT_STATUS_IS_OK(status)) {
554 goto done;
557 status = dcerpc_samr_OpenDomain(b, mem_ctx,
558 &connect_pol,
559 MAXIMUM_ALLOWED_ACCESS,
560 &domain_sid,
561 &domain_pol,
562 &result);
563 if (!NT_STATUS_IS_OK(status)) {
564 goto done;
566 if (!NT_STATUS_IS_OK(result)) {
567 status = result;
568 goto done;
571 status = dcerpc_samr_OpenGroup(b, mem_ctx,
572 &domain_pol,
573 access_mask,
574 group_rid,
575 &group_pol,
576 &result);
577 if (!NT_STATUS_IS_OK(status)) {
578 goto done;
580 if (!NT_STATUS_IS_OK(result)) {
581 status = result;
582 goto done;
585 status = dcerpc_samr_QueryGroupInfo(b, mem_ctx,
586 &group_pol,
587 info_level,
588 &group_info,
589 &result);
590 if (!NT_STATUS_IS_OK(status)) {
591 goto done;
593 if (!NT_STATUS_IS_OK(result)) {
594 status = result;
595 goto done;
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);
603 done:
604 return status;
607 /* Query groups a user is a member of */
609 static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
610 TALLOC_CTX *mem_ctx,
611 int argc, const char **argv)
613 struct policy_handle connect_pol,
614 domain_pol,
615 user_pol;
616 NTSTATUS status, result;
617 uint32 user_rid;
618 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
619 int i;
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]);
625 return NT_STATUS_OK;
628 sscanf(argv[1], "%i", &user_rid);
630 if (argc > 2)
631 sscanf(argv[2], "%x", &access_mask);
633 status = rpccli_try_samr_connects(cli, mem_ctx,
634 MAXIMUM_ALLOWED_ACCESS,
635 &connect_pol);
636 if (!NT_STATUS_IS_OK(status)) {
637 goto done;
640 status = dcerpc_samr_OpenDomain(b, mem_ctx,
641 &connect_pol,
642 MAXIMUM_ALLOWED_ACCESS,
643 &domain_sid,
644 &domain_pol,
645 &result);
646 if (!NT_STATUS_IS_OK(status)) {
647 goto done;
649 if (!NT_STATUS_IS_OK(result)) {
650 status = result;
651 goto done;
654 status = dcerpc_samr_OpenUser(b, mem_ctx,
655 &domain_pol,
656 access_mask,
657 user_rid,
658 &user_pol,
659 &result);
661 if (!NT_STATUS_IS_OK(status)) {
662 goto done;
664 if (!NT_STATUS_IS_OK(result)) {
665 status = result;
666 goto done;
669 status = dcerpc_samr_GetGroupsForUser(b, mem_ctx,
670 &user_pol,
671 &rid_array,
672 &result);
673 if (!NT_STATUS_IS_OK(status)) {
674 goto done;
676 if (!NT_STATUS_IS_OK(result)) {
677 status = result;
678 goto done;
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);
690 done:
691 return status;
694 /* Query aliases a user is a member of */
696 static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
697 TALLOC_CTX *mem_ctx,
698 int argc, const char **argv)
700 struct policy_handle connect_pol, domain_pol;
701 NTSTATUS status, result;
702 struct dom_sid *sids;
703 uint32_t num_sids;
704 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
705 int i;
706 struct lsa_SidArray sid_array;
707 struct samr_Ids alias_rids;
708 struct dcerpc_binding_handle *b = cli->binding_handle;
710 if (argc < 3) {
711 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
712 return NT_STATUS_INVALID_PARAMETER;
715 sids = NULL;
716 num_sids = 0;
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)) {
726 return result;
730 if (num_sids) {
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;
734 } else {
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,
749 &connect_pol);
750 if (!NT_STATUS_IS_OK(status)) {
751 goto done;
754 status = get_domain_handle(cli, mem_ctx, argv[1],
755 &connect_pol,
756 access_mask,
757 &domain_sid,
758 &domain_pol);
759 if (!NT_STATUS_IS_OK(status)) {
760 goto done;
763 status = dcerpc_samr_GetAliasMembership(b, mem_ctx,
764 &domain_pol,
765 &sid_array,
766 &alias_rids,
767 &result);
768 if (!NT_STATUS_IS_OK(status)) {
769 goto done;
771 if (!NT_STATUS_IS_OK(result)) {
772 status = result;
773 goto done;
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);
782 done:
783 return status;
786 /* Query members of a group */
788 static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
789 TALLOC_CTX *mem_ctx,
790 int argc, const char **argv)
792 struct policy_handle connect_pol, domain_pol, group_pol;
793 NTSTATUS status, result;
794 uint32 group_rid;
795 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
796 int i;
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]);
803 return NT_STATUS_OK;
806 sscanf(argv[1], "%i", &group_rid);
808 if (argc > 2)
809 sscanf(argv[2], "%x", &access_mask);
811 status = rpccli_try_samr_connects(cli, mem_ctx,
812 MAXIMUM_ALLOWED_ACCESS,
813 &connect_pol);
814 if (!NT_STATUS_IS_OK(status)) {
815 goto done;
818 status = dcerpc_samr_OpenDomain(b, mem_ctx,
819 &connect_pol,
820 MAXIMUM_ALLOWED_ACCESS,
821 &domain_sid,
822 &domain_pol,
823 &result);
824 if (!NT_STATUS_IS_OK(status)) {
825 goto done;
827 if (!NT_STATUS_IS_OK(result)) {
828 status = result;
829 goto done;
832 status = dcerpc_samr_OpenGroup(b, mem_ctx,
833 &domain_pol,
834 access_mask,
835 group_rid,
836 &group_pol,
837 &result);
838 if (!NT_STATUS_IS_OK(status)) {
839 goto done;
841 if (!NT_STATUS_IS_OK(result)) {
842 status = result;
843 goto done;
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,
851 &group_pol,
852 &rids,
853 &result);
855 rpccli_set_timeout(cli, old_timeout);
857 if (!NT_STATUS_IS_OK(status)) {
858 goto done;
860 if (!NT_STATUS_IS_OK(result)) {
861 status = result;
862 goto done;
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);
873 done:
874 return status;
877 /* Enumerate domain users */
879 static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
880 TALLOC_CTX *mem_ctx,
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]);
894 return NT_STATUS_OK;
897 if (argc > 1) {
898 sscanf(argv[1], "%x", &access_mask);
901 if (argc > 2) {
902 sscanf(argv[2], "%x", &acb_mask);
905 if (argc > 3) {
906 sscanf(argv[3], "%x", &size);
909 /* Get sam policy handle */
911 status = rpccli_try_samr_connects(cli, mem_ctx,
912 MAXIMUM_ALLOWED_ACCESS,
913 &connect_pol);
914 if (!NT_STATUS_IS_OK(status)) {
915 goto done;
918 /* Get domain policy handle */
920 status = get_domain_handle(cli, mem_ctx, "domain",
921 &connect_pol,
922 access_mask,
923 &domain_sid,
924 &domain_pol);
925 if (!NT_STATUS_IS_OK(status)) {
926 goto done;
929 /* Enumerate domain users */
931 start_idx = 0;
933 do {
934 status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
935 &domain_pol,
936 &start_idx,
937 acb_mask,
938 &dom_users,
939 size,
940 &num_dom_users,
941 &result);
942 if (!NT_STATUS_IS_OK(status)) {
943 goto done;
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));
956 done:
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);
963 return status;
966 /* Enumerate domain groups */
968 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
969 TALLOC_CTX *mem_ctx,
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]);
982 return NT_STATUS_OK;
985 if (argc > 1) {
986 sscanf(argv[1], "%x", &access_mask);
989 if (argc > 2) {
990 sscanf(argv[2], "%x", &size);
993 /* Get sam policy handle */
995 status = rpccli_try_samr_connects(cli, mem_ctx,
996 MAXIMUM_ALLOWED_ACCESS,
997 &connect_pol);
998 if (!NT_STATUS_IS_OK(status)) {
999 goto done;
1002 /* Get domain policy handle */
1004 status = get_domain_handle(cli, mem_ctx, "domain",
1005 &connect_pol,
1006 access_mask,
1007 &domain_sid,
1008 &domain_pol);
1009 if (!NT_STATUS_IS_OK(status)) {
1010 goto done;
1013 /* Enumerate domain groups */
1015 start_idx = 0;
1017 do {
1018 status = dcerpc_samr_EnumDomainGroups(b, mem_ctx,
1019 &domain_pol,
1020 &start_idx,
1021 &dom_groups,
1022 size,
1023 &num_dom_groups,
1024 &result);
1025 if (!NT_STATUS_IS_OK(status)) {
1026 goto done;
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));
1039 done:
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);
1046 return status;
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;
1068 if (argc > 2) {
1069 sscanf(argv[2], "%x", &access_mask);
1072 if (argc > 3) {
1073 sscanf(argv[3], "%x", &size);
1076 /* Get sam policy handle */
1078 status = rpccli_try_samr_connects(cli, mem_ctx,
1079 MAXIMUM_ALLOWED_ACCESS,
1080 &connect_pol);
1081 if (!NT_STATUS_IS_OK(status)) {
1082 goto done;
1085 /* Get domain policy handle */
1087 status = get_domain_handle(cli, mem_ctx, argv[1],
1088 &connect_pol,
1089 access_mask,
1090 &domain_sid,
1091 &domain_pol);
1092 if (!NT_STATUS_IS_OK(status)) {
1093 goto done;
1096 /* Enumerate alias groups */
1098 start_idx = 0;
1100 do {
1101 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
1102 &domain_pol,
1103 &start_idx,
1104 &als_groups,
1105 size,
1106 &num_als_groups,
1107 &result);
1108 if (!NT_STATUS_IS_OK(status)) {
1109 goto done;
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));
1121 done:
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);
1128 return status;
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;
1149 if (argc > 1) {
1150 sscanf(argv[1], "%x", &access_mask);
1153 /* Get sam policy handle */
1155 status = rpccli_try_samr_connects(cli, mem_ctx,
1156 access_mask,
1157 &connect_pol);
1158 if (!NT_STATUS_IS_OK(status)) {
1159 goto done;
1162 /* Enumerate alias groups */
1164 start_idx = 0;
1165 size = 0xffff;
1167 do {
1168 status = dcerpc_samr_EnumDomains(b, mem_ctx,
1169 &connect_pol,
1170 &start_idx,
1171 &sam,
1172 size,
1173 &num_entries,
1174 &result);
1175 if (!NT_STATUS_IS_OK(status)) {
1176 goto done;
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));
1188 done:
1189 if (is_valid_policy_hnd(&connect_pol)) {
1190 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1193 return status;
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);
1217 if (argc > 3)
1218 sscanf(argv[3], "%x", &access_mask);
1220 /* Open SAMR handle */
1222 status = rpccli_try_samr_connects(cli, mem_ctx,
1223 MAXIMUM_ALLOWED_ACCESS,
1224 &connect_pol);
1225 if (!NT_STATUS_IS_OK(status)) {
1226 goto done;
1229 /* Open handle on domain */
1231 status = get_domain_handle(cli, mem_ctx, argv[1],
1232 &connect_pol,
1233 MAXIMUM_ALLOWED_ACCESS,
1234 &domain_sid,
1235 &domain_pol);
1236 if (!NT_STATUS_IS_OK(status)) {
1237 goto done;
1240 /* Open handle on alias */
1242 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1243 &domain_pol,
1244 access_mask,
1245 alias_rid,
1246 &alias_pol,
1247 &result);
1248 if (!NT_STATUS_IS_OK(status)) {
1249 goto done;
1251 if (!NT_STATUS_IS_OK(result)) {
1252 status = result;
1253 goto done;
1256 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
1257 &alias_pol,
1258 &sid_array,
1259 &result);
1260 if (!NT_STATUS_IS_OK(status)) {
1261 goto done;
1263 if (!NT_STATUS_IS_OK(result)) {
1264 status = result;
1265 goto done;
1268 for (i = 0; i < sid_array.num_sids; i++) {
1269 fstring sid_str;
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);
1278 done:
1279 return status;
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;
1290 uint32_t alias_rid;
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",
1298 argv[0]);
1299 return NT_STATUS_OK;
1302 sscanf(argv[2], "%i", &alias_rid);
1304 if (argc > 2) {
1305 level = atoi(argv[3]);
1308 if (argc > 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,
1316 &connect_pol);
1317 if (!NT_STATUS_IS_OK(status)) {
1318 goto done;
1321 /* Open handle on domain */
1323 status = get_domain_handle(cli, mem_ctx, argv[1],
1324 &connect_pol,
1325 SEC_FLAG_MAXIMUM_ALLOWED,
1326 &domain_sid,
1327 &domain_pol);
1328 if (!NT_STATUS_IS_OK(status)) {
1329 goto done;
1332 /* Open handle on alias */
1334 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1335 &domain_pol,
1336 access_mask,
1337 alias_rid,
1338 &alias_pol,
1339 &result);
1340 if (!NT_STATUS_IS_OK(status)) {
1341 goto done;
1343 if (!NT_STATUS_IS_OK(result)) {
1344 status = result;
1345 goto done;
1348 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
1349 &alias_pol,
1350 level,
1351 &info,
1352 &result);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 goto done;
1356 if (!NT_STATUS_IS_OK(result)) {
1357 status = result;
1358 goto done;
1361 switch (level) {
1362 case ALIASINFOALL:
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);
1366 break;
1367 case ALIASINFONAME:
1368 printf("Name: %s\n", info->name.string);
1369 break;
1370 case ALIASINFODESCRIPTION:
1371 printf("Description: %s\n", info->description.string);
1372 break;
1373 default:
1374 break;
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);
1380 done:
1381 return status;
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;
1393 uint32 alias_rid;
1394 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1395 struct dcerpc_binding_handle *b = cli->binding_handle;
1397 if (argc != 3) {
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,
1408 &connect_pol);
1409 if (!NT_STATUS_IS_OK(status)) {
1410 goto done;
1413 /* Open handle on domain */
1415 status = get_domain_handle(cli, mem_ctx, argv[1],
1416 &connect_pol,
1417 MAXIMUM_ALLOWED_ACCESS,
1418 &domain_sid,
1419 &domain_pol);
1420 if (!NT_STATUS_IS_OK(status)) {
1421 goto done;
1424 /* Open handle on alias */
1426 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1427 &domain_pol,
1428 access_mask,
1429 alias_rid,
1430 &alias_pol,
1431 &result);
1432 if (!NT_STATUS_IS_OK(status)) {
1433 goto done;
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,
1443 &domain_pol,
1445 &lsa_acct_name,
1446 &rids,
1447 &types,
1448 &result);
1449 if (!NT_STATUS_IS_OK(status)) {
1450 goto done;
1452 if (NT_STATUS_IS_OK(result)) {
1453 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1454 &domain_pol,
1455 access_mask,
1456 rids.ids[0],
1457 &alias_pol,
1458 &result);
1459 if (!NT_STATUS_IS_OK(status)) {
1460 goto done;
1465 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1466 &alias_pol,
1467 &result);
1468 if (!NT_STATUS_IS_OK(status)) {
1469 goto done;
1471 if (!NT_STATUS_IS_OK(result)) {
1472 status = result;
1473 goto done;
1476 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1477 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1478 done:
1479 return status;
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,
1487 uint32_t opcode)
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;
1495 int loop_count = 0;
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;
1500 if (argc > 6) {
1501 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1502 return NT_STATUS_OK;
1505 if (argc >= 2)
1506 sscanf(argv[1], "%i", &info_level);
1508 if (argc >= 3)
1509 sscanf(argv[2], "%i", &start_idx);
1511 if (argc >= 4) {
1512 sscanf(argv[3], "%i", &max_entries);
1513 got_params = True;
1516 if (argc >= 5) {
1517 sscanf(argv[4], "%i", &max_size);
1518 got_params = True;
1521 if (argc >= 6)
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,
1528 &connect_pol);
1529 if (!NT_STATUS_IS_OK(status)) {
1530 goto done;
1533 /* Get domain policy handle */
1535 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1536 &connect_pol,
1537 access_mask,
1538 &domain_sid,
1539 &domain_pol,
1540 &result);
1541 if (!NT_STATUS_IS_OK(status)) {
1542 goto done;
1544 if (!NT_STATUS_IS_OK(result)) {
1545 status = result;
1546 goto done;
1549 /* Query display info */
1551 do {
1553 if (!got_params)
1554 dcerpc_get_query_dispinfo_params(
1555 loop_count, &max_entries, &max_size);
1557 switch (opcode) {
1558 case NDR_SAMR_QUERYDISPLAYINFO:
1559 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1560 &domain_pol,
1561 info_level,
1562 start_idx,
1563 max_entries,
1564 max_size,
1565 &total_size,
1566 &returned_size,
1567 &info,
1568 &result);
1569 break;
1570 case NDR_SAMR_QUERYDISPLAYINFO2:
1571 status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1572 &domain_pol,
1573 info_level,
1574 start_idx,
1575 max_entries,
1576 max_size,
1577 &total_size,
1578 &returned_size,
1579 &info,
1580 &result);
1582 break;
1583 case NDR_SAMR_QUERYDISPLAYINFO3:
1584 status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1585 &domain_pol,
1586 info_level,
1587 start_idx,
1588 max_entries,
1589 max_size,
1590 &total_size,
1591 &returned_size,
1592 &info,
1593 &result);
1595 break;
1596 default:
1597 return NT_STATUS_INVALID_PARAMETER;
1600 if (!NT_STATUS_IS_OK(status)) {
1601 break;
1603 status = result;
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)) {
1607 break;
1610 loop_count++;
1612 switch (info_level) {
1613 case 1:
1614 num_entries = info.info1.count;
1615 break;
1616 case 2:
1617 num_entries = info.info2.count;
1618 break;
1619 case 3:
1620 num_entries = info.info3.count;
1621 break;
1622 case 4:
1623 num_entries = info.info4.count;
1624 break;
1625 case 5:
1626 num_entries = info.info5.count;
1627 break;
1628 default:
1629 break;
1632 start_idx += num_entries;
1634 if (num_entries == 0)
1635 break;
1637 for (i = 0; i < num_entries; i++) {
1638 switch (info_level) {
1639 case 1:
1640 display_sam_info_1(&info.info1.entries[i]);
1641 break;
1642 case 2:
1643 display_sam_info_2(&info.info2.entries[i]);
1644 break;
1645 case 3:
1646 display_sam_info_3(&info.info3.entries[i]);
1647 break;
1648 case 4:
1649 display_sam_info_4(&info.info4.entries[i]);
1650 break;
1651 case 5:
1652 display_sam_info_5(&info.info5.entries[i]);
1653 break;
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);
1660 done:
1661 return status;
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;
1701 if (argc > 3) {
1702 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1703 return NT_STATUS_OK;
1706 if (argc > 1)
1707 sscanf(argv[1], "%i", &switch_level);
1709 if (argc > 2)
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,
1716 &connect_pol);
1717 if (!NT_STATUS_IS_OK(status)) {
1718 goto done;
1721 /* Get domain policy handle */
1723 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1724 &connect_pol,
1725 access_mask,
1726 &domain_sid,
1727 &domain_pol,
1728 &result);
1729 if (!NT_STATUS_IS_OK(status)) {
1730 goto done;
1732 if (!NT_STATUS_IS_OK(result)) {
1733 status = result;
1734 goto done;
1737 /* Query domain info */
1739 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1740 &domain_pol,
1741 switch_level,
1742 &info,
1743 &result);
1744 if (!NT_STATUS_IS_OK(status)) {
1745 goto done;
1747 if (!NT_STATUS_IS_OK(result)) {
1748 status = result;
1749 goto done;
1752 /* Display domain info */
1754 switch (switch_level) {
1755 case 1:
1756 display_sam_dom_info_1(&info->info1);
1757 break;
1758 case 2:
1759 display_sam_dom_info_2(&info->general);
1760 break;
1761 case 3:
1762 display_sam_dom_info_3(&info->info3);
1763 break;
1764 case 4:
1765 display_sam_dom_info_4(&info->oem);
1766 break;
1767 case 5:
1768 display_sam_dom_info_5(&info->info5);
1769 break;
1770 case 6:
1771 display_sam_dom_info_6(&info->info6);
1772 break;
1773 case 7:
1774 display_sam_dom_info_7(&info->info7);
1775 break;
1776 case 8:
1777 display_sam_dom_info_8(&info->info8);
1778 break;
1779 case 9:
1780 display_sam_dom_info_9(&info->info9);
1781 break;
1782 case 12:
1783 display_sam_dom_info_12(&info->info12);
1784 break;
1785 case 13:
1786 display_sam_dom_info_13(&info->info13);
1787 break;
1789 default:
1790 printf("cannot display domain info for switch value %d\n",
1791 switch_level);
1792 break;
1795 done:
1797 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1798 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1799 return status;
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;
1811 uint32 acb_info;
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]);
1824 if (argc > 2)
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,
1831 &connect_pol);
1832 if (!NT_STATUS_IS_OK(status)) {
1833 goto done;
1836 /* Get domain policy handle */
1838 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1839 &connect_pol,
1840 access_mask,
1841 &domain_sid,
1842 &domain_pol,
1843 &result);
1844 if (!NT_STATUS_IS_OK(status)) {
1845 goto done;
1847 if (!NT_STATUS_IS_OK(result)) {
1848 status = result;
1849 goto done;
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,
1862 &domain_pol,
1863 &acct_name,
1864 acb_info,
1865 acct_flags,
1866 &user_pol,
1867 &access_granted,
1868 &user_rid,
1869 &result);
1870 if (!NT_STATUS_IS_OK(status)) {
1871 goto done;
1873 if (!NT_STATUS_IS_OK(result)) {
1874 status = result;
1875 goto done;
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;
1887 done:
1888 return status;
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;
1901 uint32_t rid = 0;
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]);
1911 if (argc > 2)
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,
1918 &connect_pol);
1919 if (!NT_STATUS_IS_OK(status)) {
1920 goto done;
1923 /* Get domain policy handle */
1925 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1926 &connect_pol,
1927 access_mask,
1928 &domain_sid,
1929 &domain_pol,
1930 &result);
1931 if (!NT_STATUS_IS_OK(status)) {
1932 goto done;
1934 if (!NT_STATUS_IS_OK(result)) {
1935 status = result;
1936 goto done;
1939 /* Create domain user */
1940 status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
1941 &domain_pol,
1942 &grp_name,
1943 MAXIMUM_ALLOWED_ACCESS,
1944 &group_pol,
1945 &rid,
1946 &result);
1947 if (!NT_STATUS_IS_OK(status)) {
1948 goto done;
1950 if (!NT_STATUS_IS_OK(result)) {
1951 status = result;
1952 goto done;
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;
1964 done:
1965 return status;
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;
1978 uint32_t rid = 0;
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]);
1988 if (argc > 2)
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,
1995 &connect_pol);
1996 if (!NT_STATUS_IS_OK(status)) {
1997 goto done;
2000 /* Get domain policy handle */
2002 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2003 &connect_pol,
2004 access_mask,
2005 &domain_sid,
2006 &domain_pol,
2007 &result);
2008 if (!NT_STATUS_IS_OK(status)) {
2009 goto done;
2011 if (!NT_STATUS_IS_OK(result)) {
2012 status = result;
2013 goto done;
2016 /* Create domain user */
2018 status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2019 &domain_pol,
2020 &alias_name,
2021 MAXIMUM_ALLOWED_ACCESS,
2022 &alias_pol,
2023 &rid,
2024 &result);
2025 if (!NT_STATUS_IS_OK(status)) {
2026 goto done;
2028 if (!NT_STATUS_IS_OK(result)) {
2029 status = result;
2030 goto done;
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;
2043 done:
2044 return status;
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;
2055 uint32 num_names;
2056 struct samr_Ids rids, name_types;
2057 int i;
2058 struct lsa_String *names = NULL;
2059 struct dcerpc_binding_handle *b = cli->binding_handle;
2061 if (argc < 3) {
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,
2072 &connect_pol);
2073 if (!NT_STATUS_IS_OK(status)) {
2074 goto done;
2077 status = get_domain_handle(cli, mem_ctx, argv[1],
2078 &connect_pol,
2079 MAXIMUM_ALLOWED_ACCESS,
2080 &domain_sid,
2081 &domain_pol);
2082 if (!NT_STATUS_IS_OK(status)) {
2083 goto done;
2086 /* Look up names */
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;
2094 goto done;
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,
2102 &domain_pol,
2103 num_names,
2104 names,
2105 &rids,
2106 &name_types,
2107 &result);
2108 if (!NT_STATUS_IS_OK(status)) {
2109 goto done;
2111 if (!NT_STATUS_IS_OK(result)) {
2112 status = result;
2113 goto done;
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],
2120 name_types.ids[i]);
2122 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2123 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2124 done:
2125 return status;
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;
2141 int i;
2143 if (argc < 3) {
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,
2152 &connect_pol);
2153 if (!NT_STATUS_IS_OK(status)) {
2154 goto done;
2157 status = get_domain_handle(cli, mem_ctx, argv[1],
2158 &connect_pol,
2159 MAXIMUM_ALLOWED_ACCESS,
2160 &domain_sid,
2161 &domain_pol);
2162 if (!NT_STATUS_IS_OK(status)) {
2163 goto done;
2166 /* Look up rids */
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;
2174 goto done;
2177 for (i = 0; i < argc - 2; i++)
2178 sscanf(argv[i + 2], "%i", &rids[i]);
2180 status = dcerpc_samr_LookupRids(b, mem_ctx,
2181 &domain_pol,
2182 num_rids,
2183 rids,
2184 &names,
2185 &types,
2186 &result);
2187 if (!NT_STATUS_IS_OK(status)) {
2188 goto done;
2190 status = result;
2191 if (!NT_STATUS_IS_OK(result) &&
2192 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2193 goto done;
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);
2204 done:
2205 return status;
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;
2224 if (argc > 2)
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,
2231 &connect_pol);
2232 if (!NT_STATUS_IS_OK(status)) {
2233 goto done;
2236 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2237 &connect_pol,
2238 MAXIMUM_ALLOWED_ACCESS,
2239 &domain_sid,
2240 &domain_pol,
2241 &result);
2242 if (!NT_STATUS_IS_OK(status)) {
2243 goto done;
2245 if (!NT_STATUS_IS_OK(result)) {
2246 status = result;
2247 goto done;
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,
2259 &domain_pol,
2261 &lsa_acct_name,
2262 &group_rids,
2263 &name_types,
2264 &result);
2265 if (!NT_STATUS_IS_OK(status)) {
2266 goto done;
2268 if (!NT_STATUS_IS_OK(result)) {
2269 status = result;
2270 goto done;
2273 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2274 &domain_pol,
2275 access_mask,
2276 group_rids.ids[0],
2277 &group_pol,
2278 &result);
2279 if (!NT_STATUS_IS_OK(status)) {
2280 goto done;
2282 if (!NT_STATUS_IS_OK(result)) {
2283 status = result;
2284 goto done;
2288 /* Delete group */
2290 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2291 &group_pol,
2292 &result);
2293 if (!NT_STATUS_IS_OK(status)) {
2294 goto done;
2296 if (!NT_STATUS_IS_OK(result)) {
2297 status = result;
2298 goto done;
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);
2307 done:
2308 return status;
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;
2327 if (argc > 2)
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,
2334 &connect_pol);
2335 if (!NT_STATUS_IS_OK(status)) {
2336 goto done;
2339 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2340 &connect_pol,
2341 MAXIMUM_ALLOWED_ACCESS,
2342 &domain_sid,
2343 &domain_pol,
2344 &result);
2345 if (!NT_STATUS_IS_OK(status)) {
2346 goto done;
2348 if (!NT_STATUS_IS_OK(result)) {
2349 status = result;
2350 goto done;
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,
2362 &domain_pol,
2364 &lsa_acct_name,
2365 &user_rids,
2366 &name_types,
2367 &result);
2368 if (!NT_STATUS_IS_OK(status)) {
2369 goto done;
2371 if (!NT_STATUS_IS_OK(result)) {
2372 status = result;
2373 goto done;
2376 status = dcerpc_samr_OpenUser(b, mem_ctx,
2377 &domain_pol,
2378 access_mask,
2379 user_rids.ids[0],
2380 &user_pol,
2381 &result);
2382 if (!NT_STATUS_IS_OK(status)) {
2383 goto done;
2385 if (!NT_STATUS_IS_OK(result)) {
2386 status = result;
2387 goto done;
2391 /* Delete user */
2393 status = dcerpc_samr_DeleteUser(b, mem_ctx,
2394 &user_pol,
2395 &result);
2396 if (!NT_STATUS_IS_OK(status)) {
2397 goto done;
2399 if (!NT_STATUS_IS_OK(result)) {
2400 status = result;
2401 goto done;
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);
2410 done:
2411 return status;
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;
2439 if (argc > 1) {
2440 if (strcmp(argv[1], "-d") == 0)
2441 domain = True;
2442 else
2443 sscanf(argv[1], "%i", &user_rid);
2446 if (argc == 3) {
2447 sec_info = atoi(argv[2]);
2450 status = rpccli_try_samr_connects(cli, mem_ctx,
2451 MAXIMUM_ALLOWED_ACCESS,
2452 &connect_pol);
2453 if (!NT_STATUS_IS_OK(status)) {
2454 goto done;
2457 if (domain || user_rid) {
2458 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2459 &connect_pol,
2460 MAXIMUM_ALLOWED_ACCESS,
2461 &domain_sid,
2462 &domain_pol,
2463 &result);
2464 if (!NT_STATUS_IS_OK(status)) {
2465 goto done;
2467 if (!NT_STATUS_IS_OK(result)) {
2468 status = result;
2469 goto done;
2473 if (user_rid) {
2474 status = dcerpc_samr_OpenUser(b, mem_ctx,
2475 &domain_pol,
2476 MAXIMUM_ALLOWED_ACCESS,
2477 user_rid,
2478 &user_pol,
2479 &result);
2480 if (!NT_STATUS_IS_OK(status)) {
2481 goto done;
2483 if (!NT_STATUS_IS_OK(result)) {
2484 status = result;
2485 goto done;
2489 /* Pick which query pol to use */
2491 pol = &connect_pol;
2493 if (domain)
2494 pol = &domain_pol;
2496 if (user_rid)
2497 pol = &user_pol;
2499 /* Query SAM security object */
2501 status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2502 pol,
2503 sec_info,
2504 &sec_desc_buf,
2505 &result);
2506 if (!NT_STATUS_IS_OK(status)) {
2507 goto done;
2509 if (!NT_STATUS_IS_OK(result)) {
2510 status = result;
2511 goto done;
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);
2519 done:
2520 talloc_destroy(ctx);
2521 return status;
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;
2531 uint32_t rid;
2532 struct dcerpc_binding_handle *b = cli->binding_handle;
2534 if (argc != 2) {
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,
2543 &connect_pol);
2544 if (!NT_STATUS_IS_OK(status)) {
2545 goto done;
2548 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2549 &connect_pol,
2550 MAXIMUM_ALLOWED_ACCESS,
2551 &domain_sid,
2552 &domain_pol,
2553 &result);
2554 if (!NT_STATUS_IS_OK(status)) {
2555 goto done;
2557 if (!NT_STATUS_IS_OK(result)) {
2558 status = result;
2559 goto done;
2562 status = dcerpc_samr_OpenUser(b, mem_ctx,
2563 &domain_pol,
2564 MAXIMUM_ALLOWED_ACCESS,
2565 rid,
2566 &user_pol,
2567 &result);
2568 if (!NT_STATUS_IS_OK(status)) {
2569 goto done;
2571 if (!NT_STATUS_IS_OK(result)) {
2572 status = result;
2573 goto done;
2576 status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2577 &user_pol,
2578 &info,
2579 &result);
2580 if (!NT_STATUS_IS_OK(status)) {
2581 goto done;
2583 status = result;
2584 if (NT_STATUS_IS_OK(result)) {
2585 printf("min_password_length: %d\n", info.min_password_length);
2586 printf("%s\n",
2587 NDR_PRINT_STRUCT_STRING(mem_ctx,
2588 samr_PasswordProperties, &info.password_properties));
2591 done:
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);
2596 return status;
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,
2616 &domain_name,
2617 &info,
2618 &result);
2619 if (!NT_STATUS_IS_OK(status)) {
2620 return 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);
2627 return result;
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;
2639 fstring sid_string;
2640 struct lsa_String domain_name;
2641 struct dom_sid *sid = NULL;
2642 struct dcerpc_binding_handle *b = cli->binding_handle;
2644 if (argc != 2) {
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,
2652 access_mask,
2653 &connect_pol);
2654 if (!NT_STATUS_IS_OK(status)) {
2655 goto done;
2658 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2659 &connect_pol,
2660 access_mask,
2661 &domain_sid,
2662 &domain_pol,
2663 &result);
2664 if (!NT_STATUS_IS_OK(status)) {
2665 goto done;
2667 if (!NT_STATUS_IS_OK(result)) {
2668 status = result;
2669 goto done;
2672 status = dcerpc_samr_LookupDomain(b, mem_ctx,
2673 &connect_pol,
2674 &domain_name,
2675 &sid,
2676 &result);
2677 if (!NT_STATUS_IS_OK(status)) {
2678 goto done;
2680 if (!NT_STATUS_IS_OK(result)) {
2681 status = result;
2682 goto done;
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);
2693 done:
2694 return status;
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;
2711 if (argc < 3) {
2712 printf("Usage: %s username oldpass newpass\n", argv[0]);
2713 return NT_STATUS_INVALID_PARAMETER;
2716 user = argv[1];
2717 oldpass = argv[2];
2718 newpass = argv[3];
2720 /* Get sam policy handle */
2722 status = rpccli_try_samr_connects(cli, mem_ctx,
2723 MAXIMUM_ALLOWED_ACCESS,
2724 &connect_pol);
2725 if (!NT_STATUS_IS_OK(status)) {
2726 goto done;
2729 /* Get domain policy handle */
2731 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2732 &connect_pol,
2733 access_mask,
2734 &domain_sid,
2735 &domain_pol,
2736 &result);
2737 if (!NT_STATUS_IS_OK(status)) {
2738 goto done;
2740 if (!NT_STATUS_IS_OK(result)) {
2741 status = result;
2742 goto done;
2745 init_lsa_String(&lsa_acct_name, user);
2747 status = dcerpc_samr_LookupNames(b, mem_ctx,
2748 &domain_pol,
2750 &lsa_acct_name,
2751 &rids,
2752 &types,
2753 &result);
2754 if (!NT_STATUS_IS_OK(status)) {
2755 goto done;
2757 if (!NT_STATUS_IS_OK(result)) {
2758 status = result;
2759 goto done;
2762 status = dcerpc_samr_OpenUser(b, mem_ctx,
2763 &domain_pol,
2764 access_mask,
2765 rids.ids[0],
2766 &user_pol,
2767 &result);
2768 if (!NT_STATUS_IS_OK(status)) {
2769 goto done;
2771 if (!NT_STATUS_IS_OK(result)) {
2772 status = result;
2773 goto done;
2776 /* Change user password */
2777 status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2778 &user_pol,
2779 newpass,
2780 oldpass);
2781 if (!NT_STATUS_IS_OK(status)) {
2782 goto done;
2785 done:
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);
2796 return status;
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;
2812 if (argc < 3) {
2813 printf("Usage: %s username oldpass newpass\n", argv[0]);
2814 return NT_STATUS_INVALID_PARAMETER;
2817 user = argv[1];
2818 oldpass = argv[2];
2819 newpass = argv[3];
2821 /* Get sam policy handle */
2823 status = rpccli_try_samr_connects(cli, mem_ctx,
2824 MAXIMUM_ALLOWED_ACCESS,
2825 &connect_pol);
2826 if (!NT_STATUS_IS_OK(status)) {
2827 goto done;
2830 /* Get domain policy handle */
2832 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2833 &connect_pol,
2834 access_mask,
2835 &domain_sid,
2836 &domain_pol,
2837 &result);
2838 if (!NT_STATUS_IS_OK(status)) {
2839 goto done;
2841 if (!NT_STATUS_IS_OK(result)) {
2842 status = result;
2843 goto done;
2846 /* Change user password */
2847 status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2849 if (!NT_STATUS_IS_OK(status)) {
2850 goto done;
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;
2859 done:
2860 return status;
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;
2878 if (argc < 3) {
2879 printf("Usage: %s username oldpass newpass\n", argv[0]);
2880 return NT_STATUS_INVALID_PARAMETER;
2883 user = argv[1];
2884 oldpass = argv[2];
2885 newpass = argv[3];
2887 /* Get sam policy handle */
2889 status = rpccli_try_samr_connects(cli, mem_ctx,
2890 MAXIMUM_ALLOWED_ACCESS,
2891 &connect_pol);
2892 if (!NT_STATUS_IS_OK(status)) {
2893 goto done;
2896 /* Get domain policy handle */
2898 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2899 &connect_pol,
2900 access_mask,
2901 &domain_sid,
2902 &domain_pol,
2903 &result);
2904 if (!NT_STATUS_IS_OK(status)) {
2905 goto done;
2907 if (!NT_STATUS_IS_OK(result)) {
2908 status = result;
2909 goto done;
2912 /* Change user password */
2913 status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2914 user,
2915 newpass,
2916 oldpass,
2917 &info,
2918 &reject);
2919 if (!NT_STATUS_IS_OK(status)) {
2920 goto done;
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");
2930 break;
2931 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
2932 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
2933 break;
2934 case SAM_PWD_CHANGE_NOT_COMPLEX:
2935 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
2936 break;
2937 default:
2938 d_printf("unknown reject reason: %d\n",
2939 reject->extendedFailureReason);
2940 break;
2944 if (!NT_STATUS_IS_OK(result)) {
2945 status = result;
2946 goto done;
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;
2955 done:
2956 return status;
2959 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
2960 TALLOC_CTX *mem_ctx,
2961 int argc, const char **argv,
2962 int opcode)
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;
2968 uint32_t level;
2969 uint32_t user_rid;
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;
2979 if (argc < 4) {
2980 printf("Usage: %s username level password [password_expired]\n",
2981 argv[0]);
2982 return NT_STATUS_INVALID_PARAMETER;
2985 user = argv[1];
2986 level = atoi(argv[2]);
2987 param = argv[3];
2989 if (argc >= 5) {
2990 password_expired = atoi(argv[4]);
2993 status = cli_get_session_key(mem_ctx, cli, &session_key);
2994 if (!NT_STATUS_IS_OK(status)) {
2995 return 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);
3002 switch (level) {
3003 case 18:
3005 DATA_BLOB in,out;
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);
3012 DATA_BLOB in,out;
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;
3025 break;
3026 case 21:
3027 ZERO_STRUCT(info.info21);
3029 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3030 SAMR_FIELD_LM_PASSWORD_PRESENT;
3031 if (argc >= 5) {
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;
3045 DATA_BLOB in,out;
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);
3053 DATA_BLOB in,out;
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);
3061 break;
3062 case 23:
3063 ZERO_STRUCT(info.info23);
3065 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3066 SAMR_FIELD_LM_PASSWORD_PRESENT;
3067 if (argc >= 5) {
3068 info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3069 info.info23.info.password_expired = password_expired;
3072 info.info23.password = pwd_buf;
3074 break;
3075 case 24:
3076 info.info24.password = pwd_buf;
3077 info.info24.password_expired = password_expired;
3079 break;
3080 case 25:
3081 ZERO_STRUCT(info.info25);
3083 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3084 SAMR_FIELD_LM_PASSWORD_PRESENT;
3085 if (argc >= 5) {
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;
3092 break;
3093 case 26:
3094 info.info26.password = pwd_buf_ex;
3095 info.info26.password_expired = password_expired;
3097 break;
3098 default:
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,
3106 &connect_pol);
3107 if (!NT_STATUS_IS_OK(status)) {
3108 goto done;
3111 /* Get domain policy handle */
3113 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3114 &connect_pol,
3115 access_mask,
3116 &domain_sid,
3117 &domain_pol,
3118 &result);
3120 if (!NT_STATUS_IS_OK(status))
3121 goto done;
3122 if (!NT_STATUS_IS_OK(result)) {
3123 status = result;
3124 goto done;
3127 user_rid = strtol(user, NULL, 0);
3128 if (user_rid) {
3129 status = dcerpc_samr_OpenUser(b, mem_ctx,
3130 &domain_pol,
3131 access_mask,
3132 user_rid,
3133 &user_pol,
3134 &result);
3135 if (!NT_STATUS_IS_OK(status)) {
3136 goto done;
3139 status = result;
3142 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3143 (user_rid == 0)) {
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,
3152 &domain_pol,
3154 &lsa_acct_name,
3155 &rids,
3156 &types,
3157 &result);
3158 if (!NT_STATUS_IS_OK(status)) {
3159 return status;
3161 if (!NT_STATUS_IS_OK(result)) {
3162 return result;
3166 status = dcerpc_samr_OpenUser(b, mem_ctx,
3167 &domain_pol,
3168 access_mask,
3169 rids.ids[0],
3170 &user_pol,
3171 &result);
3172 if (!NT_STATUS_IS_OK(status)) {
3173 return status;
3175 if (!NT_STATUS_IS_OK(result)) {
3176 return result;
3180 switch (opcode) {
3181 case NDR_SAMR_SETUSERINFO:
3182 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
3183 &user_pol,
3184 level,
3185 &info,
3186 &result);
3187 break;
3188 case NDR_SAMR_SETUSERINFO2:
3189 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
3190 &user_pol,
3191 level,
3192 &info,
3193 &result);
3194 break;
3195 default:
3196 return NT_STATUS_INVALID_PARAMETER;
3198 if (!NT_STATUS_IS_OK(status)) {
3199 DEBUG(0,("status: %s\n", nt_errstr(status)));
3200 goto done;
3202 if (!NT_STATUS_IS_OK(result)) {
3203 status = result;
3204 DEBUG(0,("result: %s\n", nt_errstr(status)));
3205 goto done;
3207 done:
3208 return 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;
3234 uint16_t level = 1;
3235 struct lsa_String name;
3236 uint32_t idx = 0;
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]);
3246 if (argc == 3) {
3247 level = atoi(argv[2]);
3250 status = rpccli_try_samr_connects(cli, mem_ctx,
3251 SEC_FLAG_MAXIMUM_ALLOWED,
3252 &connect_handle);
3253 if (!NT_STATUS_IS_OK(status)) {
3254 goto done;
3257 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3258 &connect_handle,
3259 SEC_FLAG_MAXIMUM_ALLOWED,
3260 &domain_sid,
3261 &domain_handle,
3262 &result);
3263 if (!NT_STATUS_IS_OK(status)) {
3264 goto done;
3266 if (!NT_STATUS_IS_OK(result)) {
3267 status = result;
3268 goto done;
3271 status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3272 &domain_handle,
3273 level,
3274 &name,
3275 &idx,
3276 &result);
3277 if (!NT_STATUS_IS_OK(status)) {
3278 goto done;
3281 status = result;
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);
3287 done:
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);
3296 return status;
3299 /* List of commands exported by this module */
3301 struct cmd_set samr_commands[] = {
3303 { "SAMR" },
3305 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, &ndr_table_samr, NULL, "Query user info", "" },
3306 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, &ndr_table_samr, NULL, "Query group info", "" },
3307 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, &ndr_table_samr, NULL, "Query user groups", "" },
3308 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, &ndr_table_samr, NULL, "Query user aliases", "" },
3309 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, &ndr_table_samr, NULL, "Query group membership", "" },
3310 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, &ndr_table_samr, NULL, "Query alias membership", "" },
3311 { "queryaliasinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo, NULL, &ndr_table_samr, NULL, "Query alias info", "" },
3312 { "deletealias", RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias, NULL, &ndr_table_samr, NULL, "Delete an alias", "" },
3313 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3314 { "querydispinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3315 { "querydispinfo3", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3316 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, &ndr_table_samr, NULL, "Query domain info", "" },
3317 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, &ndr_table_samr, NULL, "Enumerate domain users", "" },
3318 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, &ndr_table_samr, NULL, "Enumerate domain groups", "" },
3319 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, &ndr_table_samr, NULL, "Enumerate alias groups", "" },
3320 { "enumdomains", RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains, NULL, &ndr_table_samr, NULL, "Enumerate domains", "" },
3322 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, &ndr_table_samr, NULL, "Create domain user", "" },
3323 { "createdomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group, NULL, &ndr_table_samr, NULL, "Create domain group", "" },
3324 { "createdomalias", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias, NULL, &ndr_table_samr, NULL, "Create domain alias", "" },
3325 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, &ndr_table_samr, NULL, "Look up names", "" },
3326 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, &ndr_table_samr, NULL, "Look up names", "" },
3327 { "deletedomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group, NULL, &ndr_table_samr, NULL, "Delete domain group", "" },
3328 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, &ndr_table_samr, NULL, "Delete domain user", "" },
3329 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, &ndr_table_samr, NULL, "Query SAMR security object", "" },
3330 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, &ndr_table_samr, NULL, "Retrieve domain password info", "" },
3331 { "getusrdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo, NULL, &ndr_table_samr, NULL, "Retrieve user domain password info", "" },
3333 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, &ndr_table_samr, NULL, "Lookup Domain Name", "" },
3334 { "chgpasswd", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3335 { "chgpasswd2", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3336 { "chgpasswd3", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3337 { "getdispinfoidx", RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx, NULL, &ndr_table_samr, NULL, "Get Display Information Index", "" },
3338 { "setuserinfo", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo, NULL, &ndr_table_samr, NULL, "Set user info", "" },
3339 { "setuserinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2, NULL, &ndr_table_samr, NULL, "Set user info2", "" },
3340 { NULL }