s3: Fix Coverity ID 242718 Uninitialized scalar variable
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_samr.c
blobebc13c3334734686dc844992636f061008a059f8
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;
884 struct policy_handle domain_pol = { 0, };
885 NTSTATUS status, result;
886 uint32 start_idx, num_dom_users, i;
887 struct samr_SamArray *dom_users = NULL;
888 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
889 uint32 acb_mask = ACB_NORMAL;
890 uint32_t size = 0xffff;
891 struct dcerpc_binding_handle *b = cli->binding_handle;
893 if ((argc < 1) || (argc > 4)) {
894 printf("Usage: %s [access_mask] [acb_mask] [size]\n", argv[0]);
895 return NT_STATUS_OK;
898 if (argc > 1) {
899 sscanf(argv[1], "%x", &access_mask);
902 if (argc > 2) {
903 sscanf(argv[2], "%x", &acb_mask);
906 if (argc > 3) {
907 sscanf(argv[3], "%x", &size);
910 /* Get sam policy handle */
912 status = rpccli_try_samr_connects(cli, mem_ctx,
913 MAXIMUM_ALLOWED_ACCESS,
914 &connect_pol);
915 if (!NT_STATUS_IS_OK(status)) {
916 goto done;
919 /* Get domain policy handle */
921 status = get_domain_handle(cli, mem_ctx, "domain",
922 &connect_pol,
923 access_mask,
924 &domain_sid,
925 &domain_pol);
926 if (!NT_STATUS_IS_OK(status)) {
927 goto done;
930 /* Enumerate domain users */
932 start_idx = 0;
934 do {
935 status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
936 &domain_pol,
937 &start_idx,
938 acb_mask,
939 &dom_users,
940 size,
941 &num_dom_users,
942 &result);
943 if (!NT_STATUS_IS_OK(status)) {
944 goto done;
946 if (NT_STATUS_IS_OK(result) ||
947 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
949 for (i = 0; i < num_dom_users; i++)
950 printf("user:[%s] rid:[0x%x]\n",
951 dom_users->entries[i].name.string,
952 dom_users->entries[i].idx);
955 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
957 done:
958 if (is_valid_policy_hnd(&domain_pol))
959 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
961 if (is_valid_policy_hnd(&connect_pol))
962 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
964 return status;
967 /* Enumerate domain groups */
969 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
970 TALLOC_CTX *mem_ctx,
971 int argc, const char **argv)
973 struct policy_handle connect_pol;
974 struct policy_handle domain_pol = { 0, };
975 NTSTATUS status, result;
976 uint32 start_idx, num_dom_groups, i;
977 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
978 struct samr_SamArray *dom_groups = NULL;
979 uint32_t size = 0xffff;
980 struct dcerpc_binding_handle *b = cli->binding_handle;
982 if ((argc < 1) || (argc > 3)) {
983 printf("Usage: %s [access_mask] [max_size]\n", argv[0]);
984 return NT_STATUS_OK;
987 if (argc > 1) {
988 sscanf(argv[1], "%x", &access_mask);
991 if (argc > 2) {
992 sscanf(argv[2], "%x", &size);
995 /* Get sam policy handle */
997 status = rpccli_try_samr_connects(cli, mem_ctx,
998 MAXIMUM_ALLOWED_ACCESS,
999 &connect_pol);
1000 if (!NT_STATUS_IS_OK(status)) {
1001 goto done;
1004 /* Get domain policy handle */
1006 status = get_domain_handle(cli, mem_ctx, "domain",
1007 &connect_pol,
1008 access_mask,
1009 &domain_sid,
1010 &domain_pol);
1011 if (!NT_STATUS_IS_OK(status)) {
1012 goto done;
1015 /* Enumerate domain groups */
1017 start_idx = 0;
1019 do {
1020 status = dcerpc_samr_EnumDomainGroups(b, mem_ctx,
1021 &domain_pol,
1022 &start_idx,
1023 &dom_groups,
1024 size,
1025 &num_dom_groups,
1026 &result);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 goto done;
1030 if (NT_STATUS_IS_OK(result) ||
1031 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1033 for (i = 0; i < num_dom_groups; i++)
1034 printf("group:[%s] rid:[0x%x]\n",
1035 dom_groups->entries[i].name.string,
1036 dom_groups->entries[i].idx);
1039 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1041 done:
1042 if (is_valid_policy_hnd(&domain_pol))
1043 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1045 if (is_valid_policy_hnd(&connect_pol))
1046 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1048 return status;
1051 /* Enumerate alias groups */
1053 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
1054 TALLOC_CTX *mem_ctx,
1055 int argc, const char **argv)
1057 struct policy_handle connect_pol;
1058 struct policy_handle domain_pol = { 0, };
1059 NTSTATUS status, result;
1060 uint32 start_idx, num_als_groups, i;
1061 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1062 struct samr_SamArray *als_groups = NULL;
1063 uint32_t size = 0xffff;
1064 struct dcerpc_binding_handle *b = cli->binding_handle;
1066 if ((argc < 2) || (argc > 4)) {
1067 printf("Usage: %s builtin|domain [access mask] [max_size]\n", argv[0]);
1068 return NT_STATUS_OK;
1071 if (argc > 2) {
1072 sscanf(argv[2], "%x", &access_mask);
1075 if (argc > 3) {
1076 sscanf(argv[3], "%x", &size);
1079 /* Get sam policy handle */
1081 status = rpccli_try_samr_connects(cli, mem_ctx,
1082 MAXIMUM_ALLOWED_ACCESS,
1083 &connect_pol);
1084 if (!NT_STATUS_IS_OK(status)) {
1085 goto done;
1088 /* Get domain policy handle */
1090 status = get_domain_handle(cli, mem_ctx, argv[1],
1091 &connect_pol,
1092 access_mask,
1093 &domain_sid,
1094 &domain_pol);
1095 if (!NT_STATUS_IS_OK(status)) {
1096 goto done;
1099 /* Enumerate alias groups */
1101 start_idx = 0;
1103 do {
1104 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
1105 &domain_pol,
1106 &start_idx,
1107 &als_groups,
1108 size,
1109 &num_als_groups,
1110 &result);
1111 if (!NT_STATUS_IS_OK(status)) {
1112 goto done;
1114 if (NT_STATUS_IS_OK(result) ||
1115 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1117 for (i = 0; i < num_als_groups; i++)
1118 printf("group:[%s] rid:[0x%x]\n",
1119 als_groups->entries[i].name.string,
1120 als_groups->entries[i].idx);
1122 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1124 done:
1125 if (is_valid_policy_hnd(&domain_pol))
1126 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1128 if (is_valid_policy_hnd(&connect_pol))
1129 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1131 return status;
1134 /* Enumerate domains */
1136 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
1137 TALLOC_CTX *mem_ctx,
1138 int argc, const char **argv)
1140 struct policy_handle connect_pol;
1141 NTSTATUS status, result;
1142 uint32 start_idx, size, num_entries, i;
1143 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1144 struct samr_SamArray *sam = NULL;
1145 struct dcerpc_binding_handle *b = cli->binding_handle;
1147 if ((argc < 1) || (argc > 2)) {
1148 printf("Usage: %s [access mask]\n", argv[0]);
1149 return NT_STATUS_OK;
1152 if (argc > 1) {
1153 sscanf(argv[1], "%x", &access_mask);
1156 /* Get sam policy handle */
1158 status = rpccli_try_samr_connects(cli, mem_ctx,
1159 access_mask,
1160 &connect_pol);
1161 if (!NT_STATUS_IS_OK(status)) {
1162 goto done;
1165 /* Enumerate alias groups */
1167 start_idx = 0;
1168 size = 0xffff;
1170 do {
1171 status = dcerpc_samr_EnumDomains(b, mem_ctx,
1172 &connect_pol,
1173 &start_idx,
1174 &sam,
1175 size,
1176 &num_entries,
1177 &result);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 goto done;
1181 if (NT_STATUS_IS_OK(result) ||
1182 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1184 for (i = 0; i < num_entries; i++)
1185 printf("name:[%s] idx:[0x%x]\n",
1186 sam->entries[i].name.string,
1187 sam->entries[i].idx);
1189 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1191 done:
1192 if (is_valid_policy_hnd(&connect_pol)) {
1193 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1196 return status;
1200 /* Query alias membership */
1202 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1203 TALLOC_CTX *mem_ctx,
1204 int argc, const char **argv)
1206 struct policy_handle connect_pol, domain_pol, alias_pol;
1207 NTSTATUS status, result;
1208 uint32 alias_rid, i;
1209 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1210 struct lsa_SidArray sid_array;
1211 struct dcerpc_binding_handle *b = cli->binding_handle;
1213 if ((argc < 3) || (argc > 4)) {
1214 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1215 return NT_STATUS_OK;
1218 sscanf(argv[2], "%i", &alias_rid);
1220 if (argc > 3)
1221 sscanf(argv[3], "%x", &access_mask);
1223 /* Open SAMR handle */
1225 status = rpccli_try_samr_connects(cli, mem_ctx,
1226 MAXIMUM_ALLOWED_ACCESS,
1227 &connect_pol);
1228 if (!NT_STATUS_IS_OK(status)) {
1229 goto done;
1232 /* Open handle on domain */
1234 status = get_domain_handle(cli, mem_ctx, argv[1],
1235 &connect_pol,
1236 MAXIMUM_ALLOWED_ACCESS,
1237 &domain_sid,
1238 &domain_pol);
1239 if (!NT_STATUS_IS_OK(status)) {
1240 goto done;
1243 /* Open handle on alias */
1245 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1246 &domain_pol,
1247 access_mask,
1248 alias_rid,
1249 &alias_pol,
1250 &result);
1251 if (!NT_STATUS_IS_OK(status)) {
1252 goto done;
1254 if (!NT_STATUS_IS_OK(result)) {
1255 status = result;
1256 goto done;
1259 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
1260 &alias_pol,
1261 &sid_array,
1262 &result);
1263 if (!NT_STATUS_IS_OK(status)) {
1264 goto done;
1266 if (!NT_STATUS_IS_OK(result)) {
1267 status = result;
1268 goto done;
1271 for (i = 0; i < sid_array.num_sids; i++) {
1272 fstring sid_str;
1274 sid_to_fstring(sid_str, sid_array.sids[i].sid);
1275 printf("\tsid:[%s]\n", sid_str);
1278 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1279 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1280 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1281 done:
1282 return status;
1285 /* Query alias info */
1287 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
1288 TALLOC_CTX *mem_ctx,
1289 int argc, const char **argv)
1291 struct policy_handle connect_pol, domain_pol, alias_pol;
1292 NTSTATUS status, result;
1293 uint32_t alias_rid;
1294 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1295 union samr_AliasInfo *info = NULL;
1296 enum samr_AliasInfoEnum level = ALIASINFOALL;
1297 struct dcerpc_binding_handle *b = cli->binding_handle;
1299 if ((argc < 3) || (argc > 4)) {
1300 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1301 argv[0]);
1302 return NT_STATUS_OK;
1305 sscanf(argv[2], "%i", &alias_rid);
1307 if (argc > 2) {
1308 level = atoi(argv[3]);
1311 if (argc > 3) {
1312 sscanf(argv[4], "%x", &access_mask);
1315 /* Open SAMR handle */
1317 status = rpccli_try_samr_connects(cli, mem_ctx,
1318 SEC_FLAG_MAXIMUM_ALLOWED,
1319 &connect_pol);
1320 if (!NT_STATUS_IS_OK(status)) {
1321 goto done;
1324 /* Open handle on domain */
1326 status = get_domain_handle(cli, mem_ctx, argv[1],
1327 &connect_pol,
1328 SEC_FLAG_MAXIMUM_ALLOWED,
1329 &domain_sid,
1330 &domain_pol);
1331 if (!NT_STATUS_IS_OK(status)) {
1332 goto done;
1335 /* Open handle on alias */
1337 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1338 &domain_pol,
1339 access_mask,
1340 alias_rid,
1341 &alias_pol,
1342 &result);
1343 if (!NT_STATUS_IS_OK(status)) {
1344 goto done;
1346 if (!NT_STATUS_IS_OK(result)) {
1347 status = result;
1348 goto done;
1351 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
1352 &alias_pol,
1353 level,
1354 &info,
1355 &result);
1356 if (!NT_STATUS_IS_OK(status)) {
1357 goto done;
1359 if (!NT_STATUS_IS_OK(result)) {
1360 status = result;
1361 goto done;
1364 switch (level) {
1365 case ALIASINFOALL:
1366 printf("Name: %s\n", info->all.name.string);
1367 printf("Description: %s\n", info->all.description.string);
1368 printf("Num Members: %d\n", info->all.num_members);
1369 break;
1370 case ALIASINFONAME:
1371 printf("Name: %s\n", info->name.string);
1372 break;
1373 case ALIASINFODESCRIPTION:
1374 printf("Description: %s\n", info->description.string);
1375 break;
1376 default:
1377 break;
1380 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1381 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1382 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1383 done:
1384 return status;
1388 /* Query delete an alias membership */
1390 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1391 TALLOC_CTX *mem_ctx,
1392 int argc, const char **argv)
1394 struct policy_handle connect_pol, domain_pol, alias_pol;
1395 NTSTATUS status, result;
1396 uint32 alias_rid;
1397 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1398 struct dcerpc_binding_handle *b = cli->binding_handle;
1400 if (argc != 3) {
1401 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1402 return NT_STATUS_OK;
1405 alias_rid = strtoul(argv[2], NULL, 10);
1407 /* Open SAMR handle */
1409 status = rpccli_try_samr_connects(cli, mem_ctx,
1410 MAXIMUM_ALLOWED_ACCESS,
1411 &connect_pol);
1412 if (!NT_STATUS_IS_OK(status)) {
1413 goto done;
1416 /* Open handle on domain */
1418 status = get_domain_handle(cli, mem_ctx, argv[1],
1419 &connect_pol,
1420 MAXIMUM_ALLOWED_ACCESS,
1421 &domain_sid,
1422 &domain_pol);
1423 if (!NT_STATUS_IS_OK(status)) {
1424 goto done;
1427 /* Open handle on alias */
1429 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1430 &domain_pol,
1431 access_mask,
1432 alias_rid,
1433 &alias_pol,
1434 &result);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 goto done;
1438 if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1439 /* Probably this was a user name, try lookupnames */
1440 struct samr_Ids rids, types;
1441 struct lsa_String lsa_acct_name;
1443 init_lsa_String(&lsa_acct_name, argv[2]);
1445 status = dcerpc_samr_LookupNames(b, mem_ctx,
1446 &domain_pol,
1448 &lsa_acct_name,
1449 &rids,
1450 &types,
1451 &result);
1452 if (!NT_STATUS_IS_OK(status)) {
1453 goto done;
1455 if (NT_STATUS_IS_OK(result)) {
1456 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1457 &domain_pol,
1458 access_mask,
1459 rids.ids[0],
1460 &alias_pol,
1461 &result);
1462 if (!NT_STATUS_IS_OK(status)) {
1463 goto done;
1468 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1469 &alias_pol,
1470 &result);
1471 if (!NT_STATUS_IS_OK(status)) {
1472 goto done;
1474 if (!NT_STATUS_IS_OK(result)) {
1475 status = result;
1476 goto done;
1479 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1480 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1481 done:
1482 return status;
1485 /* Query display info */
1487 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1488 TALLOC_CTX *mem_ctx,
1489 int argc, const char **argv,
1490 uint32_t opcode)
1492 struct policy_handle connect_pol, domain_pol;
1493 NTSTATUS status, result;
1494 uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1495 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1496 uint32 info_level = 1;
1497 union samr_DispInfo info;
1498 int loop_count = 0;
1499 bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1500 uint32_t total_size, returned_size;
1501 struct dcerpc_binding_handle *b = cli->binding_handle;
1503 if (argc > 6) {
1504 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1505 return NT_STATUS_OK;
1508 if (argc >= 2)
1509 sscanf(argv[1], "%i", &info_level);
1511 if (argc >= 3)
1512 sscanf(argv[2], "%i", &start_idx);
1514 if (argc >= 4) {
1515 sscanf(argv[3], "%i", &max_entries);
1516 got_params = True;
1519 if (argc >= 5) {
1520 sscanf(argv[4], "%i", &max_size);
1521 got_params = True;
1524 if (argc >= 6)
1525 sscanf(argv[5], "%x", &access_mask);
1527 /* Get sam policy handle */
1529 status = rpccli_try_samr_connects(cli, mem_ctx,
1530 MAXIMUM_ALLOWED_ACCESS,
1531 &connect_pol);
1532 if (!NT_STATUS_IS_OK(status)) {
1533 goto done;
1536 /* Get domain policy handle */
1538 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1539 &connect_pol,
1540 access_mask,
1541 &domain_sid,
1542 &domain_pol,
1543 &result);
1544 if (!NT_STATUS_IS_OK(status)) {
1545 goto done;
1547 if (!NT_STATUS_IS_OK(result)) {
1548 status = result;
1549 goto done;
1552 /* Query display info */
1554 do {
1556 if (!got_params)
1557 dcerpc_get_query_dispinfo_params(
1558 loop_count, &max_entries, &max_size);
1560 switch (opcode) {
1561 case NDR_SAMR_QUERYDISPLAYINFO:
1562 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1563 &domain_pol,
1564 info_level,
1565 start_idx,
1566 max_entries,
1567 max_size,
1568 &total_size,
1569 &returned_size,
1570 &info,
1571 &result);
1572 break;
1573 case NDR_SAMR_QUERYDISPLAYINFO2:
1574 status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1575 &domain_pol,
1576 info_level,
1577 start_idx,
1578 max_entries,
1579 max_size,
1580 &total_size,
1581 &returned_size,
1582 &info,
1583 &result);
1585 break;
1586 case NDR_SAMR_QUERYDISPLAYINFO3:
1587 status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1588 &domain_pol,
1589 info_level,
1590 start_idx,
1591 max_entries,
1592 max_size,
1593 &total_size,
1594 &returned_size,
1595 &info,
1596 &result);
1598 break;
1599 default:
1600 return NT_STATUS_INVALID_PARAMETER;
1603 if (!NT_STATUS_IS_OK(status)) {
1604 break;
1606 status = result;
1607 if (!NT_STATUS_IS_OK(result) &&
1608 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1609 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1610 break;
1613 loop_count++;
1615 switch (info_level) {
1616 case 1:
1617 num_entries = info.info1.count;
1618 break;
1619 case 2:
1620 num_entries = info.info2.count;
1621 break;
1622 case 3:
1623 num_entries = info.info3.count;
1624 break;
1625 case 4:
1626 num_entries = info.info4.count;
1627 break;
1628 case 5:
1629 num_entries = info.info5.count;
1630 break;
1631 default:
1632 break;
1635 start_idx += num_entries;
1637 if (num_entries == 0)
1638 break;
1640 for (i = 0; i < num_entries; i++) {
1641 switch (info_level) {
1642 case 1:
1643 display_sam_info_1(&info.info1.entries[i]);
1644 break;
1645 case 2:
1646 display_sam_info_2(&info.info2.entries[i]);
1647 break;
1648 case 3:
1649 display_sam_info_3(&info.info3.entries[i]);
1650 break;
1651 case 4:
1652 display_sam_info_4(&info.info4.entries[i]);
1653 break;
1654 case 5:
1655 display_sam_info_5(&info.info5.entries[i]);
1656 break;
1659 } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1661 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1662 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1663 done:
1664 return status;
1667 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1668 TALLOC_CTX *mem_ctx,
1669 int argc, const char **argv)
1671 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1672 NDR_SAMR_QUERYDISPLAYINFO);
1675 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1676 TALLOC_CTX *mem_ctx,
1677 int argc, const char **argv)
1679 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1680 NDR_SAMR_QUERYDISPLAYINFO2);
1683 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1684 TALLOC_CTX *mem_ctx,
1685 int argc, const char **argv)
1687 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1688 NDR_SAMR_QUERYDISPLAYINFO3);
1691 /* Query domain info */
1693 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1694 TALLOC_CTX *mem_ctx,
1695 int argc, const char **argv)
1697 struct policy_handle connect_pol, domain_pol;
1698 NTSTATUS status, result;
1699 uint32 switch_level = 2;
1700 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1701 union samr_DomainInfo *info = NULL;
1702 struct dcerpc_binding_handle *b = cli->binding_handle;
1704 if (argc > 3) {
1705 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1706 return NT_STATUS_OK;
1709 if (argc > 1)
1710 sscanf(argv[1], "%i", &switch_level);
1712 if (argc > 2)
1713 sscanf(argv[2], "%x", &access_mask);
1715 /* Get sam policy handle */
1717 status = rpccli_try_samr_connects(cli, mem_ctx,
1718 MAXIMUM_ALLOWED_ACCESS,
1719 &connect_pol);
1720 if (!NT_STATUS_IS_OK(status)) {
1721 goto done;
1724 /* Get domain policy handle */
1726 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1727 &connect_pol,
1728 access_mask,
1729 &domain_sid,
1730 &domain_pol,
1731 &result);
1732 if (!NT_STATUS_IS_OK(status)) {
1733 goto done;
1735 if (!NT_STATUS_IS_OK(result)) {
1736 status = result;
1737 goto done;
1740 /* Query domain info */
1742 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1743 &domain_pol,
1744 switch_level,
1745 &info,
1746 &result);
1747 if (!NT_STATUS_IS_OK(status)) {
1748 goto done;
1750 if (!NT_STATUS_IS_OK(result)) {
1751 status = result;
1752 goto done;
1755 /* Display domain info */
1757 switch (switch_level) {
1758 case 1:
1759 display_sam_dom_info_1(&info->info1);
1760 break;
1761 case 2:
1762 display_sam_dom_info_2(&info->general);
1763 break;
1764 case 3:
1765 display_sam_dom_info_3(&info->info3);
1766 break;
1767 case 4:
1768 display_sam_dom_info_4(&info->oem);
1769 break;
1770 case 5:
1771 display_sam_dom_info_5(&info->info5);
1772 break;
1773 case 6:
1774 display_sam_dom_info_6(&info->info6);
1775 break;
1776 case 7:
1777 display_sam_dom_info_7(&info->info7);
1778 break;
1779 case 8:
1780 display_sam_dom_info_8(&info->info8);
1781 break;
1782 case 9:
1783 display_sam_dom_info_9(&info->info9);
1784 break;
1785 case 12:
1786 display_sam_dom_info_12(&info->info12);
1787 break;
1788 case 13:
1789 display_sam_dom_info_13(&info->info13);
1790 break;
1792 default:
1793 printf("cannot display domain info for switch value %d\n",
1794 switch_level);
1795 break;
1798 done:
1800 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1801 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1802 return status;
1805 /* Create domain user */
1807 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1808 TALLOC_CTX *mem_ctx,
1809 int argc, const char **argv)
1811 struct policy_handle connect_pol, domain_pol, user_pol;
1812 NTSTATUS status, result;
1813 struct lsa_String acct_name;
1814 uint32 acb_info;
1815 uint32 acct_flags, user_rid;
1816 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1817 uint32_t access_granted = 0;
1818 struct dcerpc_binding_handle *b = cli->binding_handle;
1820 if ((argc < 2) || (argc > 3)) {
1821 printf("Usage: %s username [access mask]\n", argv[0]);
1822 return NT_STATUS_OK;
1825 init_lsa_String(&acct_name, argv[1]);
1827 if (argc > 2)
1828 sscanf(argv[2], "%x", &access_mask);
1830 /* Get sam policy handle */
1832 status = rpccli_try_samr_connects(cli, mem_ctx,
1833 MAXIMUM_ALLOWED_ACCESS,
1834 &connect_pol);
1835 if (!NT_STATUS_IS_OK(status)) {
1836 goto done;
1839 /* Get domain policy handle */
1841 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1842 &connect_pol,
1843 access_mask,
1844 &domain_sid,
1845 &domain_pol,
1846 &result);
1847 if (!NT_STATUS_IS_OK(status)) {
1848 goto done;
1850 if (!NT_STATUS_IS_OK(result)) {
1851 status = result;
1852 goto done;
1855 /* Create domain user */
1857 acb_info = ACB_NORMAL;
1858 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1859 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1860 SAMR_USER_ACCESS_SET_PASSWORD |
1861 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1862 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1864 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1865 &domain_pol,
1866 &acct_name,
1867 acb_info,
1868 acct_flags,
1869 &user_pol,
1870 &access_granted,
1871 &user_rid,
1872 &result);
1873 if (!NT_STATUS_IS_OK(status)) {
1874 goto done;
1876 if (!NT_STATUS_IS_OK(result)) {
1877 status = result;
1878 goto done;
1881 status = dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1882 if (!NT_STATUS_IS_OK(status)) goto done;
1884 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1885 if (!NT_STATUS_IS_OK(status)) goto done;
1887 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1888 if (!NT_STATUS_IS_OK(status)) goto done;
1890 done:
1891 return status;
1894 /* Create domain group */
1896 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1897 TALLOC_CTX *mem_ctx,
1898 int argc, const char **argv)
1900 struct policy_handle connect_pol, domain_pol, group_pol;
1901 NTSTATUS status, result;
1902 struct lsa_String grp_name;
1903 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1904 uint32_t rid = 0;
1905 struct dcerpc_binding_handle *b = cli->binding_handle;
1907 if ((argc < 2) || (argc > 3)) {
1908 printf("Usage: %s groupname [access mask]\n", argv[0]);
1909 return NT_STATUS_OK;
1912 init_lsa_String(&grp_name, argv[1]);
1914 if (argc > 2)
1915 sscanf(argv[2], "%x", &access_mask);
1917 /* Get sam policy handle */
1919 status = rpccli_try_samr_connects(cli, mem_ctx,
1920 MAXIMUM_ALLOWED_ACCESS,
1921 &connect_pol);
1922 if (!NT_STATUS_IS_OK(status)) {
1923 goto done;
1926 /* Get domain policy handle */
1928 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1929 &connect_pol,
1930 access_mask,
1931 &domain_sid,
1932 &domain_pol,
1933 &result);
1934 if (!NT_STATUS_IS_OK(status)) {
1935 goto done;
1937 if (!NT_STATUS_IS_OK(result)) {
1938 status = result;
1939 goto done;
1942 /* Create domain user */
1943 status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
1944 &domain_pol,
1945 &grp_name,
1946 MAXIMUM_ALLOWED_ACCESS,
1947 &group_pol,
1948 &rid,
1949 &result);
1950 if (!NT_STATUS_IS_OK(status)) {
1951 goto done;
1953 if (!NT_STATUS_IS_OK(result)) {
1954 status = result;
1955 goto done;
1958 status = dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
1959 if (!NT_STATUS_IS_OK(status)) goto done;
1961 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1962 if (!NT_STATUS_IS_OK(status)) goto done;
1964 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1965 if (!NT_STATUS_IS_OK(status)) goto done;
1967 done:
1968 return status;
1971 /* Create domain alias */
1973 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1974 TALLOC_CTX *mem_ctx,
1975 int argc, const char **argv)
1977 struct policy_handle connect_pol, domain_pol, alias_pol;
1978 NTSTATUS status, result;
1979 struct lsa_String alias_name;
1980 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1981 uint32_t rid = 0;
1982 struct dcerpc_binding_handle *b = cli->binding_handle;
1984 if ((argc < 2) || (argc > 3)) {
1985 printf("Usage: %s aliasname [access mask]\n", argv[0]);
1986 return NT_STATUS_OK;
1989 init_lsa_String(&alias_name, argv[1]);
1991 if (argc > 2)
1992 sscanf(argv[2], "%x", &access_mask);
1994 /* Get sam policy handle */
1996 status = rpccli_try_samr_connects(cli, mem_ctx,
1997 MAXIMUM_ALLOWED_ACCESS,
1998 &connect_pol);
1999 if (!NT_STATUS_IS_OK(status)) {
2000 goto done;
2003 /* Get domain policy handle */
2005 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2006 &connect_pol,
2007 access_mask,
2008 &domain_sid,
2009 &domain_pol,
2010 &result);
2011 if (!NT_STATUS_IS_OK(status)) {
2012 goto done;
2014 if (!NT_STATUS_IS_OK(result)) {
2015 status = result;
2016 goto done;
2019 /* Create domain user */
2021 status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2022 &domain_pol,
2023 &alias_name,
2024 MAXIMUM_ALLOWED_ACCESS,
2025 &alias_pol,
2026 &rid,
2027 &result);
2028 if (!NT_STATUS_IS_OK(status)) {
2029 goto done;
2031 if (!NT_STATUS_IS_OK(result)) {
2032 status = result;
2033 goto done;
2037 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
2038 if (!NT_STATUS_IS_OK(status)) goto done;
2040 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2041 if (!NT_STATUS_IS_OK(status)) goto done;
2043 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2044 if (!NT_STATUS_IS_OK(status)) goto done;
2046 done:
2047 return status;
2050 /* Lookup sam names */
2052 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
2053 TALLOC_CTX *mem_ctx,
2054 int argc, const char **argv)
2056 NTSTATUS status, result;
2057 struct policy_handle connect_pol, domain_pol;
2058 uint32 num_names;
2059 struct samr_Ids rids, name_types;
2060 int i;
2061 struct lsa_String *names = NULL;
2062 struct dcerpc_binding_handle *b = cli->binding_handle;
2064 if (argc < 3) {
2065 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
2066 printf("check on the domain SID: S-1-5-21-x-y-z\n");
2067 printf("or check on the builtin SID: S-1-5-32\n");
2068 return NT_STATUS_OK;
2071 /* Get sam policy and domain handles */
2073 status = rpccli_try_samr_connects(cli, mem_ctx,
2074 MAXIMUM_ALLOWED_ACCESS,
2075 &connect_pol);
2076 if (!NT_STATUS_IS_OK(status)) {
2077 goto done;
2080 status = get_domain_handle(cli, mem_ctx, argv[1],
2081 &connect_pol,
2082 MAXIMUM_ALLOWED_ACCESS,
2083 &domain_sid,
2084 &domain_pol);
2085 if (!NT_STATUS_IS_OK(status)) {
2086 goto done;
2089 /* Look up names */
2091 num_names = argc - 2;
2093 if ((names = talloc_array(mem_ctx, struct lsa_String, num_names)) == NULL) {
2094 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2095 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2096 status = NT_STATUS_NO_MEMORY;
2097 goto done;
2100 for (i = 0; i < num_names; i++) {
2101 init_lsa_String(&names[i], argv[i + 2]);
2104 status = dcerpc_samr_LookupNames(b, mem_ctx,
2105 &domain_pol,
2106 num_names,
2107 names,
2108 &rids,
2109 &name_types,
2110 &result);
2111 if (!NT_STATUS_IS_OK(status)) {
2112 goto done;
2114 if (!NT_STATUS_IS_OK(result)) {
2115 status = result;
2116 goto done;
2119 /* Display results */
2121 for (i = 0; i < num_names; i++)
2122 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
2123 name_types.ids[i]);
2125 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2126 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2127 done:
2128 return status;
2131 /* Lookup sam rids */
2133 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
2134 TALLOC_CTX *mem_ctx,
2135 int argc, const char **argv)
2137 NTSTATUS status, result;
2138 struct policy_handle connect_pol, domain_pol;
2139 uint32_t num_rids, *rids;
2140 struct lsa_Strings names;
2141 struct samr_Ids types;
2142 struct dcerpc_binding_handle *b = cli->binding_handle;
2144 int i;
2146 if (argc < 3) {
2147 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
2148 return NT_STATUS_OK;
2151 /* Get sam policy and domain handles */
2153 status = rpccli_try_samr_connects(cli, mem_ctx,
2154 MAXIMUM_ALLOWED_ACCESS,
2155 &connect_pol);
2156 if (!NT_STATUS_IS_OK(status)) {
2157 goto done;
2160 status = get_domain_handle(cli, mem_ctx, argv[1],
2161 &connect_pol,
2162 MAXIMUM_ALLOWED_ACCESS,
2163 &domain_sid,
2164 &domain_pol);
2165 if (!NT_STATUS_IS_OK(status)) {
2166 goto done;
2169 /* Look up rids */
2171 num_rids = argc - 2;
2173 if ((rids = talloc_array(mem_ctx, uint32, num_rids)) == NULL) {
2174 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2175 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2176 status = NT_STATUS_NO_MEMORY;
2177 goto done;
2180 for (i = 0; i < argc - 2; i++)
2181 sscanf(argv[i + 2], "%i", &rids[i]);
2183 status = dcerpc_samr_LookupRids(b, mem_ctx,
2184 &domain_pol,
2185 num_rids,
2186 rids,
2187 &names,
2188 &types,
2189 &result);
2190 if (!NT_STATUS_IS_OK(status)) {
2191 goto done;
2193 status = result;
2194 if (!NT_STATUS_IS_OK(result) &&
2195 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2196 goto done;
2198 /* Display results */
2200 for (i = 0; i < num_rids; i++) {
2201 printf("rid 0x%x: %s (%d)\n",
2202 rids[i], names.names[i].string, types.ids[i]);
2205 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2206 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2207 done:
2208 return status;
2211 /* Delete domain group */
2213 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
2214 TALLOC_CTX *mem_ctx,
2215 int argc, const char **argv)
2217 NTSTATUS status, result;
2218 struct policy_handle connect_pol, domain_pol, group_pol;
2219 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2220 struct dcerpc_binding_handle *b = cli->binding_handle;
2222 if ((argc < 2) || (argc > 3)) {
2223 printf("Usage: %s groupname\n", argv[0]);
2224 return NT_STATUS_OK;
2227 if (argc > 2)
2228 sscanf(argv[2], "%x", &access_mask);
2230 /* Get sam policy and domain handles */
2232 status = rpccli_try_samr_connects(cli, mem_ctx,
2233 MAXIMUM_ALLOWED_ACCESS,
2234 &connect_pol);
2235 if (!NT_STATUS_IS_OK(status)) {
2236 goto done;
2239 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2240 &connect_pol,
2241 MAXIMUM_ALLOWED_ACCESS,
2242 &domain_sid,
2243 &domain_pol,
2244 &result);
2245 if (!NT_STATUS_IS_OK(status)) {
2246 goto done;
2248 if (!NT_STATUS_IS_OK(result)) {
2249 status = result;
2250 goto done;
2253 /* Get handle on group */
2256 struct samr_Ids group_rids, name_types;
2257 struct lsa_String lsa_acct_name;
2259 init_lsa_String(&lsa_acct_name, argv[1]);
2261 status = dcerpc_samr_LookupNames(b, mem_ctx,
2262 &domain_pol,
2264 &lsa_acct_name,
2265 &group_rids,
2266 &name_types,
2267 &result);
2268 if (!NT_STATUS_IS_OK(status)) {
2269 goto done;
2271 if (!NT_STATUS_IS_OK(result)) {
2272 status = result;
2273 goto done;
2276 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2277 &domain_pol,
2278 access_mask,
2279 group_rids.ids[0],
2280 &group_pol,
2281 &result);
2282 if (!NT_STATUS_IS_OK(status)) {
2283 goto done;
2285 if (!NT_STATUS_IS_OK(result)) {
2286 status = result;
2287 goto done;
2291 /* Delete group */
2293 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2294 &group_pol,
2295 &result);
2296 if (!NT_STATUS_IS_OK(status)) {
2297 goto done;
2299 if (!NT_STATUS_IS_OK(result)) {
2300 status = result;
2301 goto done;
2304 /* Display results */
2306 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2307 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2308 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2310 done:
2311 return status;
2314 /* Delete domain user */
2316 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2317 TALLOC_CTX *mem_ctx,
2318 int argc, const char **argv)
2320 NTSTATUS status, result;
2321 struct policy_handle connect_pol, domain_pol, user_pol;
2322 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2323 struct dcerpc_binding_handle *b = cli->binding_handle;
2325 if ((argc < 2) || (argc > 3)) {
2326 printf("Usage: %s username\n", argv[0]);
2327 return NT_STATUS_OK;
2330 if (argc > 2)
2331 sscanf(argv[2], "%x", &access_mask);
2333 /* Get sam policy and domain handles */
2335 status = rpccli_try_samr_connects(cli, mem_ctx,
2336 MAXIMUM_ALLOWED_ACCESS,
2337 &connect_pol);
2338 if (!NT_STATUS_IS_OK(status)) {
2339 goto done;
2342 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2343 &connect_pol,
2344 MAXIMUM_ALLOWED_ACCESS,
2345 &domain_sid,
2346 &domain_pol,
2347 &result);
2348 if (!NT_STATUS_IS_OK(status)) {
2349 goto done;
2351 if (!NT_STATUS_IS_OK(result)) {
2352 status = result;
2353 goto done;
2356 /* Get handle on user */
2359 struct samr_Ids user_rids, name_types;
2360 struct lsa_String lsa_acct_name;
2362 init_lsa_String(&lsa_acct_name, argv[1]);
2364 status = dcerpc_samr_LookupNames(b, mem_ctx,
2365 &domain_pol,
2367 &lsa_acct_name,
2368 &user_rids,
2369 &name_types,
2370 &result);
2371 if (!NT_STATUS_IS_OK(status)) {
2372 goto done;
2374 if (!NT_STATUS_IS_OK(result)) {
2375 status = result;
2376 goto done;
2379 status = dcerpc_samr_OpenUser(b, mem_ctx,
2380 &domain_pol,
2381 access_mask,
2382 user_rids.ids[0],
2383 &user_pol,
2384 &result);
2385 if (!NT_STATUS_IS_OK(status)) {
2386 goto done;
2388 if (!NT_STATUS_IS_OK(result)) {
2389 status = result;
2390 goto done;
2394 /* Delete user */
2396 status = dcerpc_samr_DeleteUser(b, mem_ctx,
2397 &user_pol,
2398 &result);
2399 if (!NT_STATUS_IS_OK(status)) {
2400 goto done;
2402 if (!NT_STATUS_IS_OK(result)) {
2403 status = result;
2404 goto done;
2407 /* Display results */
2409 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2410 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2411 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2413 done:
2414 return status;
2417 /**********************************************************************
2418 * Query user security object
2420 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2421 TALLOC_CTX *mem_ctx,
2422 int argc, const char **argv)
2424 struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2425 NTSTATUS status, result;
2426 uint32 sec_info = SECINFO_DACL;
2427 uint32 user_rid = 0;
2428 TALLOC_CTX *ctx = NULL;
2429 struct sec_desc_buf *sec_desc_buf=NULL;
2430 bool domain = False;
2431 struct dcerpc_binding_handle *b = cli->binding_handle;
2433 ctx=talloc_init("cmd_samr_query_sec_obj");
2435 if ((argc < 1) || (argc > 3)) {
2436 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2437 printf("\tSpecify rid for security on user, -d for security on domain\n");
2438 talloc_destroy(ctx);
2439 return NT_STATUS_OK;
2442 if (argc > 1) {
2443 if (strcmp(argv[1], "-d") == 0)
2444 domain = True;
2445 else
2446 sscanf(argv[1], "%i", &user_rid);
2449 if (argc == 3) {
2450 sec_info = atoi(argv[2]);
2453 status = rpccli_try_samr_connects(cli, mem_ctx,
2454 MAXIMUM_ALLOWED_ACCESS,
2455 &connect_pol);
2456 if (!NT_STATUS_IS_OK(status)) {
2457 goto done;
2460 if (domain || user_rid) {
2461 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2462 &connect_pol,
2463 MAXIMUM_ALLOWED_ACCESS,
2464 &domain_sid,
2465 &domain_pol,
2466 &result);
2467 if (!NT_STATUS_IS_OK(status)) {
2468 goto done;
2470 if (!NT_STATUS_IS_OK(result)) {
2471 status = result;
2472 goto done;
2476 if (user_rid) {
2477 status = dcerpc_samr_OpenUser(b, mem_ctx,
2478 &domain_pol,
2479 MAXIMUM_ALLOWED_ACCESS,
2480 user_rid,
2481 &user_pol,
2482 &result);
2483 if (!NT_STATUS_IS_OK(status)) {
2484 goto done;
2486 if (!NT_STATUS_IS_OK(result)) {
2487 status = result;
2488 goto done;
2492 /* Pick which query pol to use */
2494 pol = &connect_pol;
2496 if (domain)
2497 pol = &domain_pol;
2499 if (user_rid)
2500 pol = &user_pol;
2502 /* Query SAM security object */
2504 status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2505 pol,
2506 sec_info,
2507 &sec_desc_buf,
2508 &result);
2509 if (!NT_STATUS_IS_OK(status)) {
2510 goto done;
2512 if (!NT_STATUS_IS_OK(result)) {
2513 status = result;
2514 goto done;
2517 display_sec_desc(sec_desc_buf->sd);
2519 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2520 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2521 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2522 done:
2523 talloc_destroy(ctx);
2524 return status;
2527 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2528 TALLOC_CTX *mem_ctx,
2529 int argc, const char **argv)
2531 NTSTATUS status, result;
2532 struct policy_handle connect_pol, domain_pol, user_pol;
2533 struct samr_PwInfo info;
2534 uint32_t rid;
2535 struct dcerpc_binding_handle *b = cli->binding_handle;
2537 if (argc != 2) {
2538 printf("Usage: %s rid\n", argv[0]);
2539 return NT_STATUS_OK;
2542 sscanf(argv[1], "%i", &rid);
2544 status = rpccli_try_samr_connects(cli, mem_ctx,
2545 MAXIMUM_ALLOWED_ACCESS,
2546 &connect_pol);
2547 if (!NT_STATUS_IS_OK(status)) {
2548 goto done;
2551 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2552 &connect_pol,
2553 MAXIMUM_ALLOWED_ACCESS,
2554 &domain_sid,
2555 &domain_pol,
2556 &result);
2557 if (!NT_STATUS_IS_OK(status)) {
2558 goto done;
2560 if (!NT_STATUS_IS_OK(result)) {
2561 status = result;
2562 goto done;
2565 status = dcerpc_samr_OpenUser(b, mem_ctx,
2566 &domain_pol,
2567 MAXIMUM_ALLOWED_ACCESS,
2568 rid,
2569 &user_pol,
2570 &result);
2571 if (!NT_STATUS_IS_OK(status)) {
2572 goto done;
2574 if (!NT_STATUS_IS_OK(result)) {
2575 status = result;
2576 goto done;
2579 status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2580 &user_pol,
2581 &info,
2582 &result);
2583 if (!NT_STATUS_IS_OK(status)) {
2584 goto done;
2586 status = result;
2587 if (NT_STATUS_IS_OK(result)) {
2588 printf("min_password_length: %d\n", info.min_password_length);
2589 printf("%s\n",
2590 NDR_PRINT_STRUCT_STRING(mem_ctx,
2591 samr_PasswordProperties, &info.password_properties));
2594 done:
2595 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2596 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2597 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2599 return status;
2602 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2603 TALLOC_CTX *mem_ctx,
2604 int argc, const char **argv)
2606 NTSTATUS status, result;
2607 struct lsa_String domain_name;
2608 struct samr_PwInfo info;
2609 struct dcerpc_binding_handle *b = cli->binding_handle;
2611 if (argc < 1 || argc > 3) {
2612 printf("Usage: %s <domain>\n", argv[0]);
2613 return NT_STATUS_OK;
2616 init_lsa_String(&domain_name, argv[1]);
2618 status = dcerpc_samr_GetDomPwInfo(b, mem_ctx,
2619 &domain_name,
2620 &info,
2621 &result);
2622 if (!NT_STATUS_IS_OK(status)) {
2623 return status;
2625 if (NT_STATUS_IS_OK(result)) {
2626 printf("min_password_length: %d\n", info.min_password_length);
2627 display_password_properties(info.password_properties);
2630 return result;
2633 /* Look up domain name */
2635 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2636 TALLOC_CTX *mem_ctx,
2637 int argc, const char **argv)
2639 struct policy_handle connect_pol, domain_pol;
2640 NTSTATUS status, result;
2641 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2642 fstring sid_string;
2643 struct lsa_String domain_name;
2644 struct dom_sid *sid = NULL;
2645 struct dcerpc_binding_handle *b = cli->binding_handle;
2647 if (argc != 2) {
2648 printf("Usage: %s domain_name\n", argv[0]);
2649 return NT_STATUS_OK;
2652 init_lsa_String(&domain_name, argv[1]);
2654 status = rpccli_try_samr_connects(cli, mem_ctx,
2655 access_mask,
2656 &connect_pol);
2657 if (!NT_STATUS_IS_OK(status)) {
2658 goto done;
2661 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2662 &connect_pol,
2663 access_mask,
2664 &domain_sid,
2665 &domain_pol,
2666 &result);
2667 if (!NT_STATUS_IS_OK(status)) {
2668 goto done;
2670 if (!NT_STATUS_IS_OK(result)) {
2671 status = result;
2672 goto done;
2675 status = dcerpc_samr_LookupDomain(b, mem_ctx,
2676 &connect_pol,
2677 &domain_name,
2678 &sid,
2679 &result);
2680 if (!NT_STATUS_IS_OK(status)) {
2681 goto done;
2683 if (!NT_STATUS_IS_OK(result)) {
2684 status = result;
2685 goto done;
2688 if (NT_STATUS_IS_OK(result)) {
2689 sid_to_fstring(sid_string, sid);
2690 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2691 argv[1], sid_string);
2694 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2695 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2696 done:
2697 return status;
2700 /* Change user password */
2702 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2703 TALLOC_CTX *mem_ctx,
2704 int argc, const char **argv)
2706 struct policy_handle connect_pol, domain_pol;
2707 struct policy_handle user_pol = { 0, };
2708 NTSTATUS status, result;
2709 const char *user, *oldpass, *newpass;
2710 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2711 struct samr_Ids rids, types;
2712 struct lsa_String lsa_acct_name;
2713 struct dcerpc_binding_handle *b = cli->binding_handle;
2715 if (argc < 3) {
2716 printf("Usage: %s username oldpass newpass\n", argv[0]);
2717 return NT_STATUS_INVALID_PARAMETER;
2720 user = argv[1];
2721 oldpass = argv[2];
2722 newpass = argv[3];
2724 /* Get sam policy handle */
2726 status = rpccli_try_samr_connects(cli, mem_ctx,
2727 MAXIMUM_ALLOWED_ACCESS,
2728 &connect_pol);
2729 if (!NT_STATUS_IS_OK(status)) {
2730 goto done;
2733 /* Get domain policy handle */
2735 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2736 &connect_pol,
2737 access_mask,
2738 &domain_sid,
2739 &domain_pol,
2740 &result);
2741 if (!NT_STATUS_IS_OK(status)) {
2742 goto done;
2744 if (!NT_STATUS_IS_OK(result)) {
2745 status = result;
2746 goto done;
2749 init_lsa_String(&lsa_acct_name, user);
2751 status = dcerpc_samr_LookupNames(b, mem_ctx,
2752 &domain_pol,
2754 &lsa_acct_name,
2755 &rids,
2756 &types,
2757 &result);
2758 if (!NT_STATUS_IS_OK(status)) {
2759 goto done;
2761 if (!NT_STATUS_IS_OK(result)) {
2762 status = result;
2763 goto done;
2766 status = dcerpc_samr_OpenUser(b, mem_ctx,
2767 &domain_pol,
2768 access_mask,
2769 rids.ids[0],
2770 &user_pol,
2771 &result);
2772 if (!NT_STATUS_IS_OK(status)) {
2773 goto done;
2775 if (!NT_STATUS_IS_OK(result)) {
2776 status = result;
2777 goto done;
2780 /* Change user password */
2781 status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2782 &user_pol,
2783 newpass,
2784 oldpass);
2785 if (!NT_STATUS_IS_OK(status)) {
2786 goto done;
2789 done:
2790 if (is_valid_policy_hnd(&user_pol)) {
2791 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2793 if (is_valid_policy_hnd(&domain_pol)) {
2794 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2796 if (is_valid_policy_hnd(&connect_pol)) {
2797 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2800 return status;
2804 /* Change user password */
2806 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2807 TALLOC_CTX *mem_ctx,
2808 int argc, const char **argv)
2810 struct policy_handle connect_pol, domain_pol;
2811 NTSTATUS status, result;
2812 const char *user, *oldpass, *newpass;
2813 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2814 struct dcerpc_binding_handle *b = cli->binding_handle;
2816 if (argc < 3) {
2817 printf("Usage: %s username oldpass newpass\n", argv[0]);
2818 return NT_STATUS_INVALID_PARAMETER;
2821 user = argv[1];
2822 oldpass = argv[2];
2823 newpass = argv[3];
2825 /* Get sam policy handle */
2827 status = rpccli_try_samr_connects(cli, mem_ctx,
2828 MAXIMUM_ALLOWED_ACCESS,
2829 &connect_pol);
2830 if (!NT_STATUS_IS_OK(status)) {
2831 goto done;
2834 /* Get domain policy handle */
2836 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2837 &connect_pol,
2838 access_mask,
2839 &domain_sid,
2840 &domain_pol,
2841 &result);
2842 if (!NT_STATUS_IS_OK(status)) {
2843 goto done;
2845 if (!NT_STATUS_IS_OK(result)) {
2846 status = result;
2847 goto done;
2850 /* Change user password */
2851 status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2853 if (!NT_STATUS_IS_OK(status)) {
2854 goto done;
2857 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2858 if (!NT_STATUS_IS_OK(status)) goto done;
2860 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2861 if (!NT_STATUS_IS_OK(status)) goto done;
2863 done:
2864 return status;
2868 /* Change user password */
2870 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2871 TALLOC_CTX *mem_ctx,
2872 int argc, const char **argv)
2874 struct policy_handle connect_pol, domain_pol;
2875 NTSTATUS status, result;
2876 const char *user, *oldpass, *newpass;
2877 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2878 struct samr_DomInfo1 *info = NULL;
2879 struct userPwdChangeFailureInformation *reject = NULL;
2880 struct dcerpc_binding_handle *b = cli->binding_handle;
2882 if (argc < 3) {
2883 printf("Usage: %s username oldpass newpass\n", argv[0]);
2884 return NT_STATUS_INVALID_PARAMETER;
2887 user = argv[1];
2888 oldpass = argv[2];
2889 newpass = argv[3];
2891 /* Get sam policy handle */
2893 status = rpccli_try_samr_connects(cli, mem_ctx,
2894 MAXIMUM_ALLOWED_ACCESS,
2895 &connect_pol);
2896 if (!NT_STATUS_IS_OK(status)) {
2897 goto done;
2900 /* Get domain policy handle */
2902 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2903 &connect_pol,
2904 access_mask,
2905 &domain_sid,
2906 &domain_pol,
2907 &result);
2908 if (!NT_STATUS_IS_OK(status)) {
2909 goto done;
2911 if (!NT_STATUS_IS_OK(result)) {
2912 status = result;
2913 goto done;
2916 /* Change user password */
2917 status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2918 user,
2919 newpass,
2920 oldpass,
2921 &info,
2922 &reject);
2923 if (!NT_STATUS_IS_OK(status)) {
2924 goto done;
2927 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2929 display_sam_dom_info_1(info);
2931 switch (reject->extendedFailureReason) {
2932 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
2933 d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
2934 break;
2935 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
2936 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
2937 break;
2938 case SAM_PWD_CHANGE_NOT_COMPLEX:
2939 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
2940 break;
2941 default:
2942 d_printf("unknown reject reason: %d\n",
2943 reject->extendedFailureReason);
2944 break;
2948 if (!NT_STATUS_IS_OK(result)) {
2949 status = result;
2950 goto done;
2953 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2954 if (!NT_STATUS_IS_OK(status)) goto done;
2956 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2957 if (!NT_STATUS_IS_OK(status)) goto done;
2959 done:
2960 return status;
2963 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
2964 TALLOC_CTX *mem_ctx,
2965 int argc, const char **argv,
2966 int opcode)
2968 struct policy_handle connect_pol, domain_pol, user_pol;
2969 NTSTATUS status, result;
2970 const char *user, *param;
2971 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2972 uint32_t level;
2973 uint32_t user_rid;
2974 union samr_UserInfo info;
2975 struct samr_CryptPassword pwd_buf;
2976 struct samr_CryptPasswordEx pwd_buf_ex;
2977 uint8_t nt_hash[16];
2978 uint8_t lm_hash[16];
2979 DATA_BLOB session_key;
2980 uint8_t password_expired = 0;
2981 struct dcerpc_binding_handle *b = cli->binding_handle;
2983 if (argc < 4) {
2984 printf("Usage: %s username level password [password_expired]\n",
2985 argv[0]);
2986 return NT_STATUS_INVALID_PARAMETER;
2989 user = argv[1];
2990 level = atoi(argv[2]);
2991 param = argv[3];
2993 if (argc >= 5) {
2994 password_expired = atoi(argv[4]);
2997 status = cli_get_session_key(mem_ctx, cli, &session_key);
2998 if (!NT_STATUS_IS_OK(status)) {
2999 return status;
3002 init_samr_CryptPassword(param, &session_key, &pwd_buf);
3003 init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
3004 nt_lm_owf_gen(param, nt_hash, lm_hash);
3006 switch (level) {
3007 case 18:
3009 DATA_BLOB in,out;
3010 in = data_blob_const(nt_hash, 16);
3011 out = data_blob_talloc_zero(mem_ctx, 16);
3012 sess_crypt_blob(&out, &in, &session_key, true);
3013 memcpy(nt_hash, out.data, out.length);
3016 DATA_BLOB in,out;
3017 in = data_blob_const(lm_hash, 16);
3018 out = data_blob_talloc_zero(mem_ctx, 16);
3019 sess_crypt_blob(&out, &in, &session_key, true);
3020 memcpy(lm_hash, out.data, out.length);
3023 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
3024 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
3025 info.info18.nt_pwd_active = true;
3026 info.info18.lm_pwd_active = true;
3027 info.info18.password_expired = password_expired;
3029 break;
3030 case 21:
3031 ZERO_STRUCT(info.info21);
3033 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3034 SAMR_FIELD_LM_PASSWORD_PRESENT;
3035 if (argc >= 5) {
3036 info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3037 info.info21.password_expired = password_expired;
3040 info.info21.lm_password_set = true;
3041 info.info21.lm_owf_password.length = 16;
3042 info.info21.lm_owf_password.size = 16;
3044 info.info21.nt_password_set = true;
3045 info.info21.nt_owf_password.length = 16;
3046 info.info21.nt_owf_password.size = 16;
3049 DATA_BLOB in,out;
3050 in = data_blob_const(nt_hash, 16);
3051 out = data_blob_talloc_zero(mem_ctx, 16);
3052 sess_crypt_blob(&out, &in, &session_key, true);
3053 info.info21.nt_owf_password.array =
3054 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3057 DATA_BLOB in,out;
3058 in = data_blob_const(lm_hash, 16);
3059 out = data_blob_talloc_zero(mem_ctx, 16);
3060 sess_crypt_blob(&out, &in, &session_key, true);
3061 info.info21.lm_owf_password.array =
3062 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3065 break;
3066 case 23:
3067 ZERO_STRUCT(info.info23);
3069 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3070 SAMR_FIELD_LM_PASSWORD_PRESENT;
3071 if (argc >= 5) {
3072 info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3073 info.info23.info.password_expired = password_expired;
3076 info.info23.password = pwd_buf;
3078 break;
3079 case 24:
3080 info.info24.password = pwd_buf;
3081 info.info24.password_expired = password_expired;
3083 break;
3084 case 25:
3085 ZERO_STRUCT(info.info25);
3087 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3088 SAMR_FIELD_LM_PASSWORD_PRESENT;
3089 if (argc >= 5) {
3090 info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3091 info.info25.info.password_expired = password_expired;
3094 info.info25.password = pwd_buf_ex;
3096 break;
3097 case 26:
3098 info.info26.password = pwd_buf_ex;
3099 info.info26.password_expired = password_expired;
3101 break;
3102 default:
3103 return NT_STATUS_INVALID_INFO_CLASS;
3106 /* Get sam policy handle */
3108 status = rpccli_try_samr_connects(cli, mem_ctx,
3109 MAXIMUM_ALLOWED_ACCESS,
3110 &connect_pol);
3111 if (!NT_STATUS_IS_OK(status)) {
3112 goto done;
3115 /* Get domain policy handle */
3117 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3118 &connect_pol,
3119 access_mask,
3120 &domain_sid,
3121 &domain_pol,
3122 &result);
3124 if (!NT_STATUS_IS_OK(status))
3125 goto done;
3126 if (!NT_STATUS_IS_OK(result)) {
3127 status = result;
3128 goto done;
3131 user_rid = strtol(user, NULL, 0);
3132 if (user_rid) {
3133 status = dcerpc_samr_OpenUser(b, mem_ctx,
3134 &domain_pol,
3135 access_mask,
3136 user_rid,
3137 &user_pol,
3138 &result);
3139 if (!NT_STATUS_IS_OK(status)) {
3140 goto done;
3143 status = result;
3146 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3147 (user_rid == 0)) {
3149 /* Probably this was a user name, try lookupnames */
3150 struct samr_Ids rids, types;
3151 struct lsa_String lsa_acct_name;
3153 init_lsa_String(&lsa_acct_name, user);
3155 status = dcerpc_samr_LookupNames(b, mem_ctx,
3156 &domain_pol,
3158 &lsa_acct_name,
3159 &rids,
3160 &types,
3161 &result);
3162 if (!NT_STATUS_IS_OK(status)) {
3163 return status;
3165 if (!NT_STATUS_IS_OK(result)) {
3166 return result;
3170 status = dcerpc_samr_OpenUser(b, mem_ctx,
3171 &domain_pol,
3172 access_mask,
3173 rids.ids[0],
3174 &user_pol,
3175 &result);
3176 if (!NT_STATUS_IS_OK(status)) {
3177 return status;
3179 if (!NT_STATUS_IS_OK(result)) {
3180 return result;
3184 switch (opcode) {
3185 case NDR_SAMR_SETUSERINFO:
3186 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
3187 &user_pol,
3188 level,
3189 &info,
3190 &result);
3191 break;
3192 case NDR_SAMR_SETUSERINFO2:
3193 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
3194 &user_pol,
3195 level,
3196 &info,
3197 &result);
3198 break;
3199 default:
3200 return NT_STATUS_INVALID_PARAMETER;
3202 if (!NT_STATUS_IS_OK(status)) {
3203 DEBUG(0,("status: %s\n", nt_errstr(status)));
3204 goto done;
3206 if (!NT_STATUS_IS_OK(result)) {
3207 status = result;
3208 DEBUG(0,("result: %s\n", nt_errstr(status)));
3209 goto done;
3211 done:
3212 return status;
3215 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
3216 TALLOC_CTX *mem_ctx,
3217 int argc, const char **argv)
3219 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3220 NDR_SAMR_SETUSERINFO);
3223 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
3224 TALLOC_CTX *mem_ctx,
3225 int argc, const char **argv)
3227 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3228 NDR_SAMR_SETUSERINFO2);
3231 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
3232 TALLOC_CTX *mem_ctx,
3233 int argc, const char **argv)
3235 NTSTATUS status, result;
3236 struct policy_handle connect_handle;
3237 struct policy_handle domain_handle = { 0, };
3238 uint16_t level = 1;
3239 struct lsa_String name;
3240 uint32_t idx = 0;
3241 struct dcerpc_binding_handle *b = cli->binding_handle;
3243 if (argc < 2 || argc > 3) {
3244 printf("Usage: %s name level\n", argv[0]);
3245 return NT_STATUS_INVALID_PARAMETER;
3248 init_lsa_String(&name, argv[1]);
3250 if (argc == 3) {
3251 level = atoi(argv[2]);
3254 status = rpccli_try_samr_connects(cli, mem_ctx,
3255 SEC_FLAG_MAXIMUM_ALLOWED,
3256 &connect_handle);
3257 if (!NT_STATUS_IS_OK(status)) {
3258 goto done;
3261 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3262 &connect_handle,
3263 SEC_FLAG_MAXIMUM_ALLOWED,
3264 &domain_sid,
3265 &domain_handle,
3266 &result);
3267 if (!NT_STATUS_IS_OK(status)) {
3268 goto done;
3270 if (!NT_STATUS_IS_OK(result)) {
3271 status = result;
3272 goto done;
3275 status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3276 &domain_handle,
3277 level,
3278 &name,
3279 &idx,
3280 &result);
3281 if (!NT_STATUS_IS_OK(status)) {
3282 goto done;
3285 status = result;
3287 if (NT_STATUS_IS_OK(status) ||
3288 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
3289 printf("idx: %d (0x%08x)\n", idx, idx);
3291 done:
3293 if (is_valid_policy_hnd(&domain_handle)) {
3294 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
3296 if (is_valid_policy_hnd(&connect_handle)) {
3297 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
3300 return status;
3303 /* List of commands exported by this module */
3305 struct cmd_set samr_commands[] = {
3307 { "SAMR" },
3309 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, &ndr_table_samr, NULL, "Query user info", "" },
3310 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, &ndr_table_samr, NULL, "Query group info", "" },
3311 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, &ndr_table_samr, NULL, "Query user groups", "" },
3312 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, &ndr_table_samr, NULL, "Query user aliases", "" },
3313 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, &ndr_table_samr, NULL, "Query group membership", "" },
3314 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, &ndr_table_samr, NULL, "Query alias membership", "" },
3315 { "queryaliasinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo, NULL, &ndr_table_samr, NULL, "Query alias info", "" },
3316 { "deletealias", RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias, NULL, &ndr_table_samr, NULL, "Delete an alias", "" },
3317 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3318 { "querydispinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3319 { "querydispinfo3", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3320 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, &ndr_table_samr, NULL, "Query domain info", "" },
3321 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, &ndr_table_samr, NULL, "Enumerate domain users", "" },
3322 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, &ndr_table_samr, NULL, "Enumerate domain groups", "" },
3323 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, &ndr_table_samr, NULL, "Enumerate alias groups", "" },
3324 { "enumdomains", RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains, NULL, &ndr_table_samr, NULL, "Enumerate domains", "" },
3326 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, &ndr_table_samr, NULL, "Create domain user", "" },
3327 { "createdomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group, NULL, &ndr_table_samr, NULL, "Create domain group", "" },
3328 { "createdomalias", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias, NULL, &ndr_table_samr, NULL, "Create domain alias", "" },
3329 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, &ndr_table_samr, NULL, "Look up names", "" },
3330 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, &ndr_table_samr, NULL, "Look up names", "" },
3331 { "deletedomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group, NULL, &ndr_table_samr, NULL, "Delete domain group", "" },
3332 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, &ndr_table_samr, NULL, "Delete domain user", "" },
3333 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, &ndr_table_samr, NULL, "Query SAMR security object", "" },
3334 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, &ndr_table_samr, NULL, "Retrieve domain password info", "" },
3335 { "getusrdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo, NULL, &ndr_table_samr, NULL, "Retrieve user domain password info", "" },
3337 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, &ndr_table_samr, NULL, "Lookup Domain Name", "" },
3338 { "chgpasswd", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3339 { "chgpasswd2", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3340 { "chgpasswd3", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3341 { "getdispinfoidx", RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx, NULL, &ndr_table_samr, NULL, "Get Display Information Index", "" },
3342 { "setuserinfo", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo, NULL, &ndr_table_samr, NULL, "Set user info", "" },
3343 { "setuserinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2, NULL, &ndr_table_samr, NULL, "Set user info2", "" },
3344 { NULL }