s3: Fix Coverity ID 242721 Uninitialized scalar variable
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_samr.c
bloba0db1aa5731a29ca34e7c35a3373e83bda199e43
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, domain_pol;
974 NTSTATUS status, result;
975 uint32 start_idx, num_dom_groups, i;
976 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
977 struct samr_SamArray *dom_groups = NULL;
978 uint32_t size = 0xffff;
979 struct dcerpc_binding_handle *b = cli->binding_handle;
981 if ((argc < 1) || (argc > 3)) {
982 printf("Usage: %s [access_mask] [max_size]\n", argv[0]);
983 return NT_STATUS_OK;
986 if (argc > 1) {
987 sscanf(argv[1], "%x", &access_mask);
990 if (argc > 2) {
991 sscanf(argv[2], "%x", &size);
994 /* Get sam policy handle */
996 status = rpccli_try_samr_connects(cli, mem_ctx,
997 MAXIMUM_ALLOWED_ACCESS,
998 &connect_pol);
999 if (!NT_STATUS_IS_OK(status)) {
1000 goto done;
1003 /* Get domain policy handle */
1005 status = get_domain_handle(cli, mem_ctx, "domain",
1006 &connect_pol,
1007 access_mask,
1008 &domain_sid,
1009 &domain_pol);
1010 if (!NT_STATUS_IS_OK(status)) {
1011 goto done;
1014 /* Enumerate domain groups */
1016 start_idx = 0;
1018 do {
1019 status = dcerpc_samr_EnumDomainGroups(b, mem_ctx,
1020 &domain_pol,
1021 &start_idx,
1022 &dom_groups,
1023 size,
1024 &num_dom_groups,
1025 &result);
1026 if (!NT_STATUS_IS_OK(status)) {
1027 goto done;
1029 if (NT_STATUS_IS_OK(result) ||
1030 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1032 for (i = 0; i < num_dom_groups; i++)
1033 printf("group:[%s] rid:[0x%x]\n",
1034 dom_groups->entries[i].name.string,
1035 dom_groups->entries[i].idx);
1038 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1040 done:
1041 if (is_valid_policy_hnd(&domain_pol))
1042 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1044 if (is_valid_policy_hnd(&connect_pol))
1045 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1047 return status;
1050 /* Enumerate alias groups */
1052 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
1053 TALLOC_CTX *mem_ctx,
1054 int argc, const char **argv)
1056 struct policy_handle connect_pol, domain_pol;
1057 NTSTATUS status, result;
1058 uint32 start_idx, num_als_groups, i;
1059 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1060 struct samr_SamArray *als_groups = NULL;
1061 uint32_t size = 0xffff;
1062 struct dcerpc_binding_handle *b = cli->binding_handle;
1064 if ((argc < 2) || (argc > 4)) {
1065 printf("Usage: %s builtin|domain [access mask] [max_size]\n", argv[0]);
1066 return NT_STATUS_OK;
1069 if (argc > 2) {
1070 sscanf(argv[2], "%x", &access_mask);
1073 if (argc > 3) {
1074 sscanf(argv[3], "%x", &size);
1077 /* Get sam policy handle */
1079 status = rpccli_try_samr_connects(cli, mem_ctx,
1080 MAXIMUM_ALLOWED_ACCESS,
1081 &connect_pol);
1082 if (!NT_STATUS_IS_OK(status)) {
1083 goto done;
1086 /* Get domain policy handle */
1088 status = get_domain_handle(cli, mem_ctx, argv[1],
1089 &connect_pol,
1090 access_mask,
1091 &domain_sid,
1092 &domain_pol);
1093 if (!NT_STATUS_IS_OK(status)) {
1094 goto done;
1097 /* Enumerate alias groups */
1099 start_idx = 0;
1101 do {
1102 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
1103 &domain_pol,
1104 &start_idx,
1105 &als_groups,
1106 size,
1107 &num_als_groups,
1108 &result);
1109 if (!NT_STATUS_IS_OK(status)) {
1110 goto done;
1112 if (NT_STATUS_IS_OK(result) ||
1113 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1115 for (i = 0; i < num_als_groups; i++)
1116 printf("group:[%s] rid:[0x%x]\n",
1117 als_groups->entries[i].name.string,
1118 als_groups->entries[i].idx);
1120 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1122 done:
1123 if (is_valid_policy_hnd(&domain_pol))
1124 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1126 if (is_valid_policy_hnd(&connect_pol))
1127 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1129 return status;
1132 /* Enumerate domains */
1134 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
1135 TALLOC_CTX *mem_ctx,
1136 int argc, const char **argv)
1138 struct policy_handle connect_pol;
1139 NTSTATUS status, result;
1140 uint32 start_idx, size, num_entries, i;
1141 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1142 struct samr_SamArray *sam = NULL;
1143 struct dcerpc_binding_handle *b = cli->binding_handle;
1145 if ((argc < 1) || (argc > 2)) {
1146 printf("Usage: %s [access mask]\n", argv[0]);
1147 return NT_STATUS_OK;
1150 if (argc > 1) {
1151 sscanf(argv[1], "%x", &access_mask);
1154 /* Get sam policy handle */
1156 status = rpccli_try_samr_connects(cli, mem_ctx,
1157 access_mask,
1158 &connect_pol);
1159 if (!NT_STATUS_IS_OK(status)) {
1160 goto done;
1163 /* Enumerate alias groups */
1165 start_idx = 0;
1166 size = 0xffff;
1168 do {
1169 status = dcerpc_samr_EnumDomains(b, mem_ctx,
1170 &connect_pol,
1171 &start_idx,
1172 &sam,
1173 size,
1174 &num_entries,
1175 &result);
1176 if (!NT_STATUS_IS_OK(status)) {
1177 goto done;
1179 if (NT_STATUS_IS_OK(result) ||
1180 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1182 for (i = 0; i < num_entries; i++)
1183 printf("name:[%s] idx:[0x%x]\n",
1184 sam->entries[i].name.string,
1185 sam->entries[i].idx);
1187 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1189 done:
1190 if (is_valid_policy_hnd(&connect_pol)) {
1191 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1194 return status;
1198 /* Query alias membership */
1200 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1201 TALLOC_CTX *mem_ctx,
1202 int argc, const char **argv)
1204 struct policy_handle connect_pol, domain_pol, alias_pol;
1205 NTSTATUS status, result;
1206 uint32 alias_rid, i;
1207 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1208 struct lsa_SidArray sid_array;
1209 struct dcerpc_binding_handle *b = cli->binding_handle;
1211 if ((argc < 3) || (argc > 4)) {
1212 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1213 return NT_STATUS_OK;
1216 sscanf(argv[2], "%i", &alias_rid);
1218 if (argc > 3)
1219 sscanf(argv[3], "%x", &access_mask);
1221 /* Open SAMR handle */
1223 status = rpccli_try_samr_connects(cli, mem_ctx,
1224 MAXIMUM_ALLOWED_ACCESS,
1225 &connect_pol);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 goto done;
1230 /* Open handle on domain */
1232 status = get_domain_handle(cli, mem_ctx, argv[1],
1233 &connect_pol,
1234 MAXIMUM_ALLOWED_ACCESS,
1235 &domain_sid,
1236 &domain_pol);
1237 if (!NT_STATUS_IS_OK(status)) {
1238 goto done;
1241 /* Open handle on alias */
1243 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1244 &domain_pol,
1245 access_mask,
1246 alias_rid,
1247 &alias_pol,
1248 &result);
1249 if (!NT_STATUS_IS_OK(status)) {
1250 goto done;
1252 if (!NT_STATUS_IS_OK(result)) {
1253 status = result;
1254 goto done;
1257 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
1258 &alias_pol,
1259 &sid_array,
1260 &result);
1261 if (!NT_STATUS_IS_OK(status)) {
1262 goto done;
1264 if (!NT_STATUS_IS_OK(result)) {
1265 status = result;
1266 goto done;
1269 for (i = 0; i < sid_array.num_sids; i++) {
1270 fstring sid_str;
1272 sid_to_fstring(sid_str, sid_array.sids[i].sid);
1273 printf("\tsid:[%s]\n", sid_str);
1276 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1277 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1278 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1279 done:
1280 return status;
1283 /* Query alias info */
1285 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
1286 TALLOC_CTX *mem_ctx,
1287 int argc, const char **argv)
1289 struct policy_handle connect_pol, domain_pol, alias_pol;
1290 NTSTATUS status, result;
1291 uint32_t alias_rid;
1292 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1293 union samr_AliasInfo *info = NULL;
1294 enum samr_AliasInfoEnum level = ALIASINFOALL;
1295 struct dcerpc_binding_handle *b = cli->binding_handle;
1297 if ((argc < 3) || (argc > 4)) {
1298 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1299 argv[0]);
1300 return NT_STATUS_OK;
1303 sscanf(argv[2], "%i", &alias_rid);
1305 if (argc > 2) {
1306 level = atoi(argv[3]);
1309 if (argc > 3) {
1310 sscanf(argv[4], "%x", &access_mask);
1313 /* Open SAMR handle */
1315 status = rpccli_try_samr_connects(cli, mem_ctx,
1316 SEC_FLAG_MAXIMUM_ALLOWED,
1317 &connect_pol);
1318 if (!NT_STATUS_IS_OK(status)) {
1319 goto done;
1322 /* Open handle on domain */
1324 status = get_domain_handle(cli, mem_ctx, argv[1],
1325 &connect_pol,
1326 SEC_FLAG_MAXIMUM_ALLOWED,
1327 &domain_sid,
1328 &domain_pol);
1329 if (!NT_STATUS_IS_OK(status)) {
1330 goto done;
1333 /* Open handle on alias */
1335 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1336 &domain_pol,
1337 access_mask,
1338 alias_rid,
1339 &alias_pol,
1340 &result);
1341 if (!NT_STATUS_IS_OK(status)) {
1342 goto done;
1344 if (!NT_STATUS_IS_OK(result)) {
1345 status = result;
1346 goto done;
1349 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
1350 &alias_pol,
1351 level,
1352 &info,
1353 &result);
1354 if (!NT_STATUS_IS_OK(status)) {
1355 goto done;
1357 if (!NT_STATUS_IS_OK(result)) {
1358 status = result;
1359 goto done;
1362 switch (level) {
1363 case ALIASINFOALL:
1364 printf("Name: %s\n", info->all.name.string);
1365 printf("Description: %s\n", info->all.description.string);
1366 printf("Num Members: %d\n", info->all.num_members);
1367 break;
1368 case ALIASINFONAME:
1369 printf("Name: %s\n", info->name.string);
1370 break;
1371 case ALIASINFODESCRIPTION:
1372 printf("Description: %s\n", info->description.string);
1373 break;
1374 default:
1375 break;
1378 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1379 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1380 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1381 done:
1382 return status;
1386 /* Query delete an alias membership */
1388 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1389 TALLOC_CTX *mem_ctx,
1390 int argc, const char **argv)
1392 struct policy_handle connect_pol, domain_pol, alias_pol;
1393 NTSTATUS status, result;
1394 uint32 alias_rid;
1395 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1396 struct dcerpc_binding_handle *b = cli->binding_handle;
1398 if (argc != 3) {
1399 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1400 return NT_STATUS_OK;
1403 alias_rid = strtoul(argv[2], NULL, 10);
1405 /* Open SAMR handle */
1407 status = rpccli_try_samr_connects(cli, mem_ctx,
1408 MAXIMUM_ALLOWED_ACCESS,
1409 &connect_pol);
1410 if (!NT_STATUS_IS_OK(status)) {
1411 goto done;
1414 /* Open handle on domain */
1416 status = get_domain_handle(cli, mem_ctx, argv[1],
1417 &connect_pol,
1418 MAXIMUM_ALLOWED_ACCESS,
1419 &domain_sid,
1420 &domain_pol);
1421 if (!NT_STATUS_IS_OK(status)) {
1422 goto done;
1425 /* Open handle on alias */
1427 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1428 &domain_pol,
1429 access_mask,
1430 alias_rid,
1431 &alias_pol,
1432 &result);
1433 if (!NT_STATUS_IS_OK(status)) {
1434 goto done;
1436 if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1437 /* Probably this was a user name, try lookupnames */
1438 struct samr_Ids rids, types;
1439 struct lsa_String lsa_acct_name;
1441 init_lsa_String(&lsa_acct_name, argv[2]);
1443 status = dcerpc_samr_LookupNames(b, mem_ctx,
1444 &domain_pol,
1446 &lsa_acct_name,
1447 &rids,
1448 &types,
1449 &result);
1450 if (!NT_STATUS_IS_OK(status)) {
1451 goto done;
1453 if (NT_STATUS_IS_OK(result)) {
1454 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1455 &domain_pol,
1456 access_mask,
1457 rids.ids[0],
1458 &alias_pol,
1459 &result);
1460 if (!NT_STATUS_IS_OK(status)) {
1461 goto done;
1466 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1467 &alias_pol,
1468 &result);
1469 if (!NT_STATUS_IS_OK(status)) {
1470 goto done;
1472 if (!NT_STATUS_IS_OK(result)) {
1473 status = result;
1474 goto done;
1477 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1478 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1479 done:
1480 return status;
1483 /* Query display info */
1485 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1486 TALLOC_CTX *mem_ctx,
1487 int argc, const char **argv,
1488 uint32_t opcode)
1490 struct policy_handle connect_pol, domain_pol;
1491 NTSTATUS status, result;
1492 uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1493 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1494 uint32 info_level = 1;
1495 union samr_DispInfo info;
1496 int loop_count = 0;
1497 bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1498 uint32_t total_size, returned_size;
1499 struct dcerpc_binding_handle *b = cli->binding_handle;
1501 if (argc > 6) {
1502 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1503 return NT_STATUS_OK;
1506 if (argc >= 2)
1507 sscanf(argv[1], "%i", &info_level);
1509 if (argc >= 3)
1510 sscanf(argv[2], "%i", &start_idx);
1512 if (argc >= 4) {
1513 sscanf(argv[3], "%i", &max_entries);
1514 got_params = True;
1517 if (argc >= 5) {
1518 sscanf(argv[4], "%i", &max_size);
1519 got_params = True;
1522 if (argc >= 6)
1523 sscanf(argv[5], "%x", &access_mask);
1525 /* Get sam policy handle */
1527 status = rpccli_try_samr_connects(cli, mem_ctx,
1528 MAXIMUM_ALLOWED_ACCESS,
1529 &connect_pol);
1530 if (!NT_STATUS_IS_OK(status)) {
1531 goto done;
1534 /* Get domain policy handle */
1536 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1537 &connect_pol,
1538 access_mask,
1539 &domain_sid,
1540 &domain_pol,
1541 &result);
1542 if (!NT_STATUS_IS_OK(status)) {
1543 goto done;
1545 if (!NT_STATUS_IS_OK(result)) {
1546 status = result;
1547 goto done;
1550 /* Query display info */
1552 do {
1554 if (!got_params)
1555 dcerpc_get_query_dispinfo_params(
1556 loop_count, &max_entries, &max_size);
1558 switch (opcode) {
1559 case NDR_SAMR_QUERYDISPLAYINFO:
1560 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1561 &domain_pol,
1562 info_level,
1563 start_idx,
1564 max_entries,
1565 max_size,
1566 &total_size,
1567 &returned_size,
1568 &info,
1569 &result);
1570 break;
1571 case NDR_SAMR_QUERYDISPLAYINFO2:
1572 status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1573 &domain_pol,
1574 info_level,
1575 start_idx,
1576 max_entries,
1577 max_size,
1578 &total_size,
1579 &returned_size,
1580 &info,
1581 &result);
1583 break;
1584 case NDR_SAMR_QUERYDISPLAYINFO3:
1585 status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1586 &domain_pol,
1587 info_level,
1588 start_idx,
1589 max_entries,
1590 max_size,
1591 &total_size,
1592 &returned_size,
1593 &info,
1594 &result);
1596 break;
1597 default:
1598 return NT_STATUS_INVALID_PARAMETER;
1601 if (!NT_STATUS_IS_OK(status)) {
1602 break;
1604 status = result;
1605 if (!NT_STATUS_IS_OK(result) &&
1606 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1607 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1608 break;
1611 loop_count++;
1613 switch (info_level) {
1614 case 1:
1615 num_entries = info.info1.count;
1616 break;
1617 case 2:
1618 num_entries = info.info2.count;
1619 break;
1620 case 3:
1621 num_entries = info.info3.count;
1622 break;
1623 case 4:
1624 num_entries = info.info4.count;
1625 break;
1626 case 5:
1627 num_entries = info.info5.count;
1628 break;
1629 default:
1630 break;
1633 start_idx += num_entries;
1635 if (num_entries == 0)
1636 break;
1638 for (i = 0; i < num_entries; i++) {
1639 switch (info_level) {
1640 case 1:
1641 display_sam_info_1(&info.info1.entries[i]);
1642 break;
1643 case 2:
1644 display_sam_info_2(&info.info2.entries[i]);
1645 break;
1646 case 3:
1647 display_sam_info_3(&info.info3.entries[i]);
1648 break;
1649 case 4:
1650 display_sam_info_4(&info.info4.entries[i]);
1651 break;
1652 case 5:
1653 display_sam_info_5(&info.info5.entries[i]);
1654 break;
1657 } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1659 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1660 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1661 done:
1662 return status;
1665 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1666 TALLOC_CTX *mem_ctx,
1667 int argc, const char **argv)
1669 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1670 NDR_SAMR_QUERYDISPLAYINFO);
1673 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1674 TALLOC_CTX *mem_ctx,
1675 int argc, const char **argv)
1677 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1678 NDR_SAMR_QUERYDISPLAYINFO2);
1681 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1682 TALLOC_CTX *mem_ctx,
1683 int argc, const char **argv)
1685 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1686 NDR_SAMR_QUERYDISPLAYINFO3);
1689 /* Query domain info */
1691 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1692 TALLOC_CTX *mem_ctx,
1693 int argc, const char **argv)
1695 struct policy_handle connect_pol, domain_pol;
1696 NTSTATUS status, result;
1697 uint32 switch_level = 2;
1698 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1699 union samr_DomainInfo *info = NULL;
1700 struct dcerpc_binding_handle *b = cli->binding_handle;
1702 if (argc > 3) {
1703 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1704 return NT_STATUS_OK;
1707 if (argc > 1)
1708 sscanf(argv[1], "%i", &switch_level);
1710 if (argc > 2)
1711 sscanf(argv[2], "%x", &access_mask);
1713 /* Get sam policy handle */
1715 status = rpccli_try_samr_connects(cli, mem_ctx,
1716 MAXIMUM_ALLOWED_ACCESS,
1717 &connect_pol);
1718 if (!NT_STATUS_IS_OK(status)) {
1719 goto done;
1722 /* Get domain policy handle */
1724 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1725 &connect_pol,
1726 access_mask,
1727 &domain_sid,
1728 &domain_pol,
1729 &result);
1730 if (!NT_STATUS_IS_OK(status)) {
1731 goto done;
1733 if (!NT_STATUS_IS_OK(result)) {
1734 status = result;
1735 goto done;
1738 /* Query domain info */
1740 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1741 &domain_pol,
1742 switch_level,
1743 &info,
1744 &result);
1745 if (!NT_STATUS_IS_OK(status)) {
1746 goto done;
1748 if (!NT_STATUS_IS_OK(result)) {
1749 status = result;
1750 goto done;
1753 /* Display domain info */
1755 switch (switch_level) {
1756 case 1:
1757 display_sam_dom_info_1(&info->info1);
1758 break;
1759 case 2:
1760 display_sam_dom_info_2(&info->general);
1761 break;
1762 case 3:
1763 display_sam_dom_info_3(&info->info3);
1764 break;
1765 case 4:
1766 display_sam_dom_info_4(&info->oem);
1767 break;
1768 case 5:
1769 display_sam_dom_info_5(&info->info5);
1770 break;
1771 case 6:
1772 display_sam_dom_info_6(&info->info6);
1773 break;
1774 case 7:
1775 display_sam_dom_info_7(&info->info7);
1776 break;
1777 case 8:
1778 display_sam_dom_info_8(&info->info8);
1779 break;
1780 case 9:
1781 display_sam_dom_info_9(&info->info9);
1782 break;
1783 case 12:
1784 display_sam_dom_info_12(&info->info12);
1785 break;
1786 case 13:
1787 display_sam_dom_info_13(&info->info13);
1788 break;
1790 default:
1791 printf("cannot display domain info for switch value %d\n",
1792 switch_level);
1793 break;
1796 done:
1798 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1799 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1800 return status;
1803 /* Create domain user */
1805 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1806 TALLOC_CTX *mem_ctx,
1807 int argc, const char **argv)
1809 struct policy_handle connect_pol, domain_pol, user_pol;
1810 NTSTATUS status, result;
1811 struct lsa_String acct_name;
1812 uint32 acb_info;
1813 uint32 acct_flags, user_rid;
1814 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1815 uint32_t access_granted = 0;
1816 struct dcerpc_binding_handle *b = cli->binding_handle;
1818 if ((argc < 2) || (argc > 3)) {
1819 printf("Usage: %s username [access mask]\n", argv[0]);
1820 return NT_STATUS_OK;
1823 init_lsa_String(&acct_name, argv[1]);
1825 if (argc > 2)
1826 sscanf(argv[2], "%x", &access_mask);
1828 /* Get sam policy handle */
1830 status = rpccli_try_samr_connects(cli, mem_ctx,
1831 MAXIMUM_ALLOWED_ACCESS,
1832 &connect_pol);
1833 if (!NT_STATUS_IS_OK(status)) {
1834 goto done;
1837 /* Get domain policy handle */
1839 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1840 &connect_pol,
1841 access_mask,
1842 &domain_sid,
1843 &domain_pol,
1844 &result);
1845 if (!NT_STATUS_IS_OK(status)) {
1846 goto done;
1848 if (!NT_STATUS_IS_OK(result)) {
1849 status = result;
1850 goto done;
1853 /* Create domain user */
1855 acb_info = ACB_NORMAL;
1856 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1857 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1858 SAMR_USER_ACCESS_SET_PASSWORD |
1859 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1860 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1862 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1863 &domain_pol,
1864 &acct_name,
1865 acb_info,
1866 acct_flags,
1867 &user_pol,
1868 &access_granted,
1869 &user_rid,
1870 &result);
1871 if (!NT_STATUS_IS_OK(status)) {
1872 goto done;
1874 if (!NT_STATUS_IS_OK(result)) {
1875 status = result;
1876 goto done;
1879 status = dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1880 if (!NT_STATUS_IS_OK(status)) goto done;
1882 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1883 if (!NT_STATUS_IS_OK(status)) goto done;
1885 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1886 if (!NT_STATUS_IS_OK(status)) goto done;
1888 done:
1889 return status;
1892 /* Create domain group */
1894 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1895 TALLOC_CTX *mem_ctx,
1896 int argc, const char **argv)
1898 struct policy_handle connect_pol, domain_pol, group_pol;
1899 NTSTATUS status, result;
1900 struct lsa_String grp_name;
1901 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1902 uint32_t rid = 0;
1903 struct dcerpc_binding_handle *b = cli->binding_handle;
1905 if ((argc < 2) || (argc > 3)) {
1906 printf("Usage: %s groupname [access mask]\n", argv[0]);
1907 return NT_STATUS_OK;
1910 init_lsa_String(&grp_name, argv[1]);
1912 if (argc > 2)
1913 sscanf(argv[2], "%x", &access_mask);
1915 /* Get sam policy handle */
1917 status = rpccli_try_samr_connects(cli, mem_ctx,
1918 MAXIMUM_ALLOWED_ACCESS,
1919 &connect_pol);
1920 if (!NT_STATUS_IS_OK(status)) {
1921 goto done;
1924 /* Get domain policy handle */
1926 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1927 &connect_pol,
1928 access_mask,
1929 &domain_sid,
1930 &domain_pol,
1931 &result);
1932 if (!NT_STATUS_IS_OK(status)) {
1933 goto done;
1935 if (!NT_STATUS_IS_OK(result)) {
1936 status = result;
1937 goto done;
1940 /* Create domain user */
1941 status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
1942 &domain_pol,
1943 &grp_name,
1944 MAXIMUM_ALLOWED_ACCESS,
1945 &group_pol,
1946 &rid,
1947 &result);
1948 if (!NT_STATUS_IS_OK(status)) {
1949 goto done;
1951 if (!NT_STATUS_IS_OK(result)) {
1952 status = result;
1953 goto done;
1956 status = dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
1957 if (!NT_STATUS_IS_OK(status)) goto done;
1959 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1960 if (!NT_STATUS_IS_OK(status)) goto done;
1962 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1963 if (!NT_STATUS_IS_OK(status)) goto done;
1965 done:
1966 return status;
1969 /* Create domain alias */
1971 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1972 TALLOC_CTX *mem_ctx,
1973 int argc, const char **argv)
1975 struct policy_handle connect_pol, domain_pol, alias_pol;
1976 NTSTATUS status, result;
1977 struct lsa_String alias_name;
1978 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1979 uint32_t rid = 0;
1980 struct dcerpc_binding_handle *b = cli->binding_handle;
1982 if ((argc < 2) || (argc > 3)) {
1983 printf("Usage: %s aliasname [access mask]\n", argv[0]);
1984 return NT_STATUS_OK;
1987 init_lsa_String(&alias_name, argv[1]);
1989 if (argc > 2)
1990 sscanf(argv[2], "%x", &access_mask);
1992 /* Get sam policy handle */
1994 status = rpccli_try_samr_connects(cli, mem_ctx,
1995 MAXIMUM_ALLOWED_ACCESS,
1996 &connect_pol);
1997 if (!NT_STATUS_IS_OK(status)) {
1998 goto done;
2001 /* Get domain policy handle */
2003 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2004 &connect_pol,
2005 access_mask,
2006 &domain_sid,
2007 &domain_pol,
2008 &result);
2009 if (!NT_STATUS_IS_OK(status)) {
2010 goto done;
2012 if (!NT_STATUS_IS_OK(result)) {
2013 status = result;
2014 goto done;
2017 /* Create domain user */
2019 status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2020 &domain_pol,
2021 &alias_name,
2022 MAXIMUM_ALLOWED_ACCESS,
2023 &alias_pol,
2024 &rid,
2025 &result);
2026 if (!NT_STATUS_IS_OK(status)) {
2027 goto done;
2029 if (!NT_STATUS_IS_OK(result)) {
2030 status = result;
2031 goto done;
2035 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
2036 if (!NT_STATUS_IS_OK(status)) goto done;
2038 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2039 if (!NT_STATUS_IS_OK(status)) goto done;
2041 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2042 if (!NT_STATUS_IS_OK(status)) goto done;
2044 done:
2045 return status;
2048 /* Lookup sam names */
2050 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
2051 TALLOC_CTX *mem_ctx,
2052 int argc, const char **argv)
2054 NTSTATUS status, result;
2055 struct policy_handle connect_pol, domain_pol;
2056 uint32 num_names;
2057 struct samr_Ids rids, name_types;
2058 int i;
2059 struct lsa_String *names = NULL;
2060 struct dcerpc_binding_handle *b = cli->binding_handle;
2062 if (argc < 3) {
2063 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
2064 printf("check on the domain SID: S-1-5-21-x-y-z\n");
2065 printf("or check on the builtin SID: S-1-5-32\n");
2066 return NT_STATUS_OK;
2069 /* Get sam policy and domain handles */
2071 status = rpccli_try_samr_connects(cli, mem_ctx,
2072 MAXIMUM_ALLOWED_ACCESS,
2073 &connect_pol);
2074 if (!NT_STATUS_IS_OK(status)) {
2075 goto done;
2078 status = get_domain_handle(cli, mem_ctx, argv[1],
2079 &connect_pol,
2080 MAXIMUM_ALLOWED_ACCESS,
2081 &domain_sid,
2082 &domain_pol);
2083 if (!NT_STATUS_IS_OK(status)) {
2084 goto done;
2087 /* Look up names */
2089 num_names = argc - 2;
2091 if ((names = talloc_array(mem_ctx, struct lsa_String, num_names)) == NULL) {
2092 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2093 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2094 status = NT_STATUS_NO_MEMORY;
2095 goto done;
2098 for (i = 0; i < num_names; i++) {
2099 init_lsa_String(&names[i], argv[i + 2]);
2102 status = dcerpc_samr_LookupNames(b, mem_ctx,
2103 &domain_pol,
2104 num_names,
2105 names,
2106 &rids,
2107 &name_types,
2108 &result);
2109 if (!NT_STATUS_IS_OK(status)) {
2110 goto done;
2112 if (!NT_STATUS_IS_OK(result)) {
2113 status = result;
2114 goto done;
2117 /* Display results */
2119 for (i = 0; i < num_names; i++)
2120 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
2121 name_types.ids[i]);
2123 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2124 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2125 done:
2126 return status;
2129 /* Lookup sam rids */
2131 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
2132 TALLOC_CTX *mem_ctx,
2133 int argc, const char **argv)
2135 NTSTATUS status, result;
2136 struct policy_handle connect_pol, domain_pol;
2137 uint32_t num_rids, *rids;
2138 struct lsa_Strings names;
2139 struct samr_Ids types;
2140 struct dcerpc_binding_handle *b = cli->binding_handle;
2142 int i;
2144 if (argc < 3) {
2145 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
2146 return NT_STATUS_OK;
2149 /* Get sam policy and domain handles */
2151 status = rpccli_try_samr_connects(cli, mem_ctx,
2152 MAXIMUM_ALLOWED_ACCESS,
2153 &connect_pol);
2154 if (!NT_STATUS_IS_OK(status)) {
2155 goto done;
2158 status = get_domain_handle(cli, mem_ctx, argv[1],
2159 &connect_pol,
2160 MAXIMUM_ALLOWED_ACCESS,
2161 &domain_sid,
2162 &domain_pol);
2163 if (!NT_STATUS_IS_OK(status)) {
2164 goto done;
2167 /* Look up rids */
2169 num_rids = argc - 2;
2171 if ((rids = talloc_array(mem_ctx, uint32, num_rids)) == NULL) {
2172 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2173 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2174 status = NT_STATUS_NO_MEMORY;
2175 goto done;
2178 for (i = 0; i < argc - 2; i++)
2179 sscanf(argv[i + 2], "%i", &rids[i]);
2181 status = dcerpc_samr_LookupRids(b, mem_ctx,
2182 &domain_pol,
2183 num_rids,
2184 rids,
2185 &names,
2186 &types,
2187 &result);
2188 if (!NT_STATUS_IS_OK(status)) {
2189 goto done;
2191 status = result;
2192 if (!NT_STATUS_IS_OK(result) &&
2193 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2194 goto done;
2196 /* Display results */
2198 for (i = 0; i < num_rids; i++) {
2199 printf("rid 0x%x: %s (%d)\n",
2200 rids[i], names.names[i].string, types.ids[i]);
2203 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2204 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2205 done:
2206 return status;
2209 /* Delete domain group */
2211 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
2212 TALLOC_CTX *mem_ctx,
2213 int argc, const char **argv)
2215 NTSTATUS status, result;
2216 struct policy_handle connect_pol, domain_pol, group_pol;
2217 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2218 struct dcerpc_binding_handle *b = cli->binding_handle;
2220 if ((argc < 2) || (argc > 3)) {
2221 printf("Usage: %s groupname\n", argv[0]);
2222 return NT_STATUS_OK;
2225 if (argc > 2)
2226 sscanf(argv[2], "%x", &access_mask);
2228 /* Get sam policy and domain handles */
2230 status = rpccli_try_samr_connects(cli, mem_ctx,
2231 MAXIMUM_ALLOWED_ACCESS,
2232 &connect_pol);
2233 if (!NT_STATUS_IS_OK(status)) {
2234 goto done;
2237 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2238 &connect_pol,
2239 MAXIMUM_ALLOWED_ACCESS,
2240 &domain_sid,
2241 &domain_pol,
2242 &result);
2243 if (!NT_STATUS_IS_OK(status)) {
2244 goto done;
2246 if (!NT_STATUS_IS_OK(result)) {
2247 status = result;
2248 goto done;
2251 /* Get handle on group */
2254 struct samr_Ids group_rids, name_types;
2255 struct lsa_String lsa_acct_name;
2257 init_lsa_String(&lsa_acct_name, argv[1]);
2259 status = dcerpc_samr_LookupNames(b, mem_ctx,
2260 &domain_pol,
2262 &lsa_acct_name,
2263 &group_rids,
2264 &name_types,
2265 &result);
2266 if (!NT_STATUS_IS_OK(status)) {
2267 goto done;
2269 if (!NT_STATUS_IS_OK(result)) {
2270 status = result;
2271 goto done;
2274 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2275 &domain_pol,
2276 access_mask,
2277 group_rids.ids[0],
2278 &group_pol,
2279 &result);
2280 if (!NT_STATUS_IS_OK(status)) {
2281 goto done;
2283 if (!NT_STATUS_IS_OK(result)) {
2284 status = result;
2285 goto done;
2289 /* Delete group */
2291 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2292 &group_pol,
2293 &result);
2294 if (!NT_STATUS_IS_OK(status)) {
2295 goto done;
2297 if (!NT_STATUS_IS_OK(result)) {
2298 status = result;
2299 goto done;
2302 /* Display results */
2304 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2305 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2306 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2308 done:
2309 return status;
2312 /* Delete domain user */
2314 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2315 TALLOC_CTX *mem_ctx,
2316 int argc, const char **argv)
2318 NTSTATUS status, result;
2319 struct policy_handle connect_pol, domain_pol, user_pol;
2320 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2321 struct dcerpc_binding_handle *b = cli->binding_handle;
2323 if ((argc < 2) || (argc > 3)) {
2324 printf("Usage: %s username\n", argv[0]);
2325 return NT_STATUS_OK;
2328 if (argc > 2)
2329 sscanf(argv[2], "%x", &access_mask);
2331 /* Get sam policy and domain handles */
2333 status = rpccli_try_samr_connects(cli, mem_ctx,
2334 MAXIMUM_ALLOWED_ACCESS,
2335 &connect_pol);
2336 if (!NT_STATUS_IS_OK(status)) {
2337 goto done;
2340 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2341 &connect_pol,
2342 MAXIMUM_ALLOWED_ACCESS,
2343 &domain_sid,
2344 &domain_pol,
2345 &result);
2346 if (!NT_STATUS_IS_OK(status)) {
2347 goto done;
2349 if (!NT_STATUS_IS_OK(result)) {
2350 status = result;
2351 goto done;
2354 /* Get handle on user */
2357 struct samr_Ids user_rids, name_types;
2358 struct lsa_String lsa_acct_name;
2360 init_lsa_String(&lsa_acct_name, argv[1]);
2362 status = dcerpc_samr_LookupNames(b, mem_ctx,
2363 &domain_pol,
2365 &lsa_acct_name,
2366 &user_rids,
2367 &name_types,
2368 &result);
2369 if (!NT_STATUS_IS_OK(status)) {
2370 goto done;
2372 if (!NT_STATUS_IS_OK(result)) {
2373 status = result;
2374 goto done;
2377 status = dcerpc_samr_OpenUser(b, mem_ctx,
2378 &domain_pol,
2379 access_mask,
2380 user_rids.ids[0],
2381 &user_pol,
2382 &result);
2383 if (!NT_STATUS_IS_OK(status)) {
2384 goto done;
2386 if (!NT_STATUS_IS_OK(result)) {
2387 status = result;
2388 goto done;
2392 /* Delete user */
2394 status = dcerpc_samr_DeleteUser(b, mem_ctx,
2395 &user_pol,
2396 &result);
2397 if (!NT_STATUS_IS_OK(status)) {
2398 goto done;
2400 if (!NT_STATUS_IS_OK(result)) {
2401 status = result;
2402 goto done;
2405 /* Display results */
2407 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2408 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2409 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2411 done:
2412 return status;
2415 /**********************************************************************
2416 * Query user security object
2418 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2419 TALLOC_CTX *mem_ctx,
2420 int argc, const char **argv)
2422 struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2423 NTSTATUS status, result;
2424 uint32 sec_info = SECINFO_DACL;
2425 uint32 user_rid = 0;
2426 TALLOC_CTX *ctx = NULL;
2427 struct sec_desc_buf *sec_desc_buf=NULL;
2428 bool domain = False;
2429 struct dcerpc_binding_handle *b = cli->binding_handle;
2431 ctx=talloc_init("cmd_samr_query_sec_obj");
2433 if ((argc < 1) || (argc > 3)) {
2434 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2435 printf("\tSpecify rid for security on user, -d for security on domain\n");
2436 talloc_destroy(ctx);
2437 return NT_STATUS_OK;
2440 if (argc > 1) {
2441 if (strcmp(argv[1], "-d") == 0)
2442 domain = True;
2443 else
2444 sscanf(argv[1], "%i", &user_rid);
2447 if (argc == 3) {
2448 sec_info = atoi(argv[2]);
2451 status = rpccli_try_samr_connects(cli, mem_ctx,
2452 MAXIMUM_ALLOWED_ACCESS,
2453 &connect_pol);
2454 if (!NT_STATUS_IS_OK(status)) {
2455 goto done;
2458 if (domain || user_rid) {
2459 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2460 &connect_pol,
2461 MAXIMUM_ALLOWED_ACCESS,
2462 &domain_sid,
2463 &domain_pol,
2464 &result);
2465 if (!NT_STATUS_IS_OK(status)) {
2466 goto done;
2468 if (!NT_STATUS_IS_OK(result)) {
2469 status = result;
2470 goto done;
2474 if (user_rid) {
2475 status = dcerpc_samr_OpenUser(b, mem_ctx,
2476 &domain_pol,
2477 MAXIMUM_ALLOWED_ACCESS,
2478 user_rid,
2479 &user_pol,
2480 &result);
2481 if (!NT_STATUS_IS_OK(status)) {
2482 goto done;
2484 if (!NT_STATUS_IS_OK(result)) {
2485 status = result;
2486 goto done;
2490 /* Pick which query pol to use */
2492 pol = &connect_pol;
2494 if (domain)
2495 pol = &domain_pol;
2497 if (user_rid)
2498 pol = &user_pol;
2500 /* Query SAM security object */
2502 status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2503 pol,
2504 sec_info,
2505 &sec_desc_buf,
2506 &result);
2507 if (!NT_STATUS_IS_OK(status)) {
2508 goto done;
2510 if (!NT_STATUS_IS_OK(result)) {
2511 status = result;
2512 goto done;
2515 display_sec_desc(sec_desc_buf->sd);
2517 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2518 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2519 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2520 done:
2521 talloc_destroy(ctx);
2522 return status;
2525 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2526 TALLOC_CTX *mem_ctx,
2527 int argc, const char **argv)
2529 NTSTATUS status, result;
2530 struct policy_handle connect_pol, domain_pol, user_pol;
2531 struct samr_PwInfo info;
2532 uint32_t rid;
2533 struct dcerpc_binding_handle *b = cli->binding_handle;
2535 if (argc != 2) {
2536 printf("Usage: %s rid\n", argv[0]);
2537 return NT_STATUS_OK;
2540 sscanf(argv[1], "%i", &rid);
2542 status = rpccli_try_samr_connects(cli, mem_ctx,
2543 MAXIMUM_ALLOWED_ACCESS,
2544 &connect_pol);
2545 if (!NT_STATUS_IS_OK(status)) {
2546 goto done;
2549 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2550 &connect_pol,
2551 MAXIMUM_ALLOWED_ACCESS,
2552 &domain_sid,
2553 &domain_pol,
2554 &result);
2555 if (!NT_STATUS_IS_OK(status)) {
2556 goto done;
2558 if (!NT_STATUS_IS_OK(result)) {
2559 status = result;
2560 goto done;
2563 status = dcerpc_samr_OpenUser(b, mem_ctx,
2564 &domain_pol,
2565 MAXIMUM_ALLOWED_ACCESS,
2566 rid,
2567 &user_pol,
2568 &result);
2569 if (!NT_STATUS_IS_OK(status)) {
2570 goto done;
2572 if (!NT_STATUS_IS_OK(result)) {
2573 status = result;
2574 goto done;
2577 status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2578 &user_pol,
2579 &info,
2580 &result);
2581 if (!NT_STATUS_IS_OK(status)) {
2582 goto done;
2584 status = result;
2585 if (NT_STATUS_IS_OK(result)) {
2586 printf("min_password_length: %d\n", info.min_password_length);
2587 printf("%s\n",
2588 NDR_PRINT_STRUCT_STRING(mem_ctx,
2589 samr_PasswordProperties, &info.password_properties));
2592 done:
2593 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2594 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2595 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2597 return status;
2600 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2601 TALLOC_CTX *mem_ctx,
2602 int argc, const char **argv)
2604 NTSTATUS status, result;
2605 struct lsa_String domain_name;
2606 struct samr_PwInfo info;
2607 struct dcerpc_binding_handle *b = cli->binding_handle;
2609 if (argc < 1 || argc > 3) {
2610 printf("Usage: %s <domain>\n", argv[0]);
2611 return NT_STATUS_OK;
2614 init_lsa_String(&domain_name, argv[1]);
2616 status = dcerpc_samr_GetDomPwInfo(b, mem_ctx,
2617 &domain_name,
2618 &info,
2619 &result);
2620 if (!NT_STATUS_IS_OK(status)) {
2621 return status;
2623 if (NT_STATUS_IS_OK(result)) {
2624 printf("min_password_length: %d\n", info.min_password_length);
2625 display_password_properties(info.password_properties);
2628 return result;
2631 /* Look up domain name */
2633 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2634 TALLOC_CTX *mem_ctx,
2635 int argc, const char **argv)
2637 struct policy_handle connect_pol, domain_pol;
2638 NTSTATUS status, result;
2639 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2640 fstring sid_string;
2641 struct lsa_String domain_name;
2642 struct dom_sid *sid = NULL;
2643 struct dcerpc_binding_handle *b = cli->binding_handle;
2645 if (argc != 2) {
2646 printf("Usage: %s domain_name\n", argv[0]);
2647 return NT_STATUS_OK;
2650 init_lsa_String(&domain_name, argv[1]);
2652 status = rpccli_try_samr_connects(cli, mem_ctx,
2653 access_mask,
2654 &connect_pol);
2655 if (!NT_STATUS_IS_OK(status)) {
2656 goto done;
2659 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2660 &connect_pol,
2661 access_mask,
2662 &domain_sid,
2663 &domain_pol,
2664 &result);
2665 if (!NT_STATUS_IS_OK(status)) {
2666 goto done;
2668 if (!NT_STATUS_IS_OK(result)) {
2669 status = result;
2670 goto done;
2673 status = dcerpc_samr_LookupDomain(b, mem_ctx,
2674 &connect_pol,
2675 &domain_name,
2676 &sid,
2677 &result);
2678 if (!NT_STATUS_IS_OK(status)) {
2679 goto done;
2681 if (!NT_STATUS_IS_OK(result)) {
2682 status = result;
2683 goto done;
2686 if (NT_STATUS_IS_OK(result)) {
2687 sid_to_fstring(sid_string, sid);
2688 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2689 argv[1], sid_string);
2692 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2693 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2694 done:
2695 return status;
2698 /* Change user password */
2700 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2701 TALLOC_CTX *mem_ctx,
2702 int argc, const char **argv)
2704 struct policy_handle connect_pol, domain_pol, user_pol;
2705 NTSTATUS status, result;
2706 const char *user, *oldpass, *newpass;
2707 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2708 struct samr_Ids rids, types;
2709 struct lsa_String lsa_acct_name;
2710 struct dcerpc_binding_handle *b = cli->binding_handle;
2712 if (argc < 3) {
2713 printf("Usage: %s username oldpass newpass\n", argv[0]);
2714 return NT_STATUS_INVALID_PARAMETER;
2717 user = argv[1];
2718 oldpass = argv[2];
2719 newpass = argv[3];
2721 /* Get sam policy handle */
2723 status = rpccli_try_samr_connects(cli, mem_ctx,
2724 MAXIMUM_ALLOWED_ACCESS,
2725 &connect_pol);
2726 if (!NT_STATUS_IS_OK(status)) {
2727 goto done;
2730 /* Get domain policy handle */
2732 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2733 &connect_pol,
2734 access_mask,
2735 &domain_sid,
2736 &domain_pol,
2737 &result);
2738 if (!NT_STATUS_IS_OK(status)) {
2739 goto done;
2741 if (!NT_STATUS_IS_OK(result)) {
2742 status = result;
2743 goto done;
2746 init_lsa_String(&lsa_acct_name, user);
2748 status = dcerpc_samr_LookupNames(b, mem_ctx,
2749 &domain_pol,
2751 &lsa_acct_name,
2752 &rids,
2753 &types,
2754 &result);
2755 if (!NT_STATUS_IS_OK(status)) {
2756 goto done;
2758 if (!NT_STATUS_IS_OK(result)) {
2759 status = result;
2760 goto done;
2763 status = dcerpc_samr_OpenUser(b, mem_ctx,
2764 &domain_pol,
2765 access_mask,
2766 rids.ids[0],
2767 &user_pol,
2768 &result);
2769 if (!NT_STATUS_IS_OK(status)) {
2770 goto done;
2772 if (!NT_STATUS_IS_OK(result)) {
2773 status = result;
2774 goto done;
2777 /* Change user password */
2778 status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2779 &user_pol,
2780 newpass,
2781 oldpass);
2782 if (!NT_STATUS_IS_OK(status)) {
2783 goto done;
2786 done:
2787 if (is_valid_policy_hnd(&user_pol)) {
2788 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2790 if (is_valid_policy_hnd(&domain_pol)) {
2791 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2793 if (is_valid_policy_hnd(&connect_pol)) {
2794 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2797 return status;
2801 /* Change user password */
2803 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2804 TALLOC_CTX *mem_ctx,
2805 int argc, const char **argv)
2807 struct policy_handle connect_pol, domain_pol;
2808 NTSTATUS status, result;
2809 const char *user, *oldpass, *newpass;
2810 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2811 struct dcerpc_binding_handle *b = cli->binding_handle;
2813 if (argc < 3) {
2814 printf("Usage: %s username oldpass newpass\n", argv[0]);
2815 return NT_STATUS_INVALID_PARAMETER;
2818 user = argv[1];
2819 oldpass = argv[2];
2820 newpass = argv[3];
2822 /* Get sam policy handle */
2824 status = rpccli_try_samr_connects(cli, mem_ctx,
2825 MAXIMUM_ALLOWED_ACCESS,
2826 &connect_pol);
2827 if (!NT_STATUS_IS_OK(status)) {
2828 goto done;
2831 /* Get domain policy handle */
2833 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2834 &connect_pol,
2835 access_mask,
2836 &domain_sid,
2837 &domain_pol,
2838 &result);
2839 if (!NT_STATUS_IS_OK(status)) {
2840 goto done;
2842 if (!NT_STATUS_IS_OK(result)) {
2843 status = result;
2844 goto done;
2847 /* Change user password */
2848 status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2850 if (!NT_STATUS_IS_OK(status)) {
2851 goto done;
2854 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2855 if (!NT_STATUS_IS_OK(status)) goto done;
2857 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2858 if (!NT_STATUS_IS_OK(status)) goto done;
2860 done:
2861 return status;
2865 /* Change user password */
2867 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2868 TALLOC_CTX *mem_ctx,
2869 int argc, const char **argv)
2871 struct policy_handle connect_pol, domain_pol;
2872 NTSTATUS status, result;
2873 const char *user, *oldpass, *newpass;
2874 uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2875 struct samr_DomInfo1 *info = NULL;
2876 struct userPwdChangeFailureInformation *reject = NULL;
2877 struct dcerpc_binding_handle *b = cli->binding_handle;
2879 if (argc < 3) {
2880 printf("Usage: %s username oldpass newpass\n", argv[0]);
2881 return NT_STATUS_INVALID_PARAMETER;
2884 user = argv[1];
2885 oldpass = argv[2];
2886 newpass = argv[3];
2888 /* Get sam policy handle */
2890 status = rpccli_try_samr_connects(cli, mem_ctx,
2891 MAXIMUM_ALLOWED_ACCESS,
2892 &connect_pol);
2893 if (!NT_STATUS_IS_OK(status)) {
2894 goto done;
2897 /* Get domain policy handle */
2899 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2900 &connect_pol,
2901 access_mask,
2902 &domain_sid,
2903 &domain_pol,
2904 &result);
2905 if (!NT_STATUS_IS_OK(status)) {
2906 goto done;
2908 if (!NT_STATUS_IS_OK(result)) {
2909 status = result;
2910 goto done;
2913 /* Change user password */
2914 status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2915 user,
2916 newpass,
2917 oldpass,
2918 &info,
2919 &reject);
2920 if (!NT_STATUS_IS_OK(status)) {
2921 goto done;
2924 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2926 display_sam_dom_info_1(info);
2928 switch (reject->extendedFailureReason) {
2929 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
2930 d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
2931 break;
2932 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
2933 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
2934 break;
2935 case SAM_PWD_CHANGE_NOT_COMPLEX:
2936 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
2937 break;
2938 default:
2939 d_printf("unknown reject reason: %d\n",
2940 reject->extendedFailureReason);
2941 break;
2945 if (!NT_STATUS_IS_OK(result)) {
2946 status = result;
2947 goto done;
2950 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2951 if (!NT_STATUS_IS_OK(status)) goto done;
2953 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2954 if (!NT_STATUS_IS_OK(status)) goto done;
2956 done:
2957 return status;
2960 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
2961 TALLOC_CTX *mem_ctx,
2962 int argc, const char **argv,
2963 int opcode)
2965 struct policy_handle connect_pol, domain_pol, user_pol;
2966 NTSTATUS status, result;
2967 const char *user, *param;
2968 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2969 uint32_t level;
2970 uint32_t user_rid;
2971 union samr_UserInfo info;
2972 struct samr_CryptPassword pwd_buf;
2973 struct samr_CryptPasswordEx pwd_buf_ex;
2974 uint8_t nt_hash[16];
2975 uint8_t lm_hash[16];
2976 DATA_BLOB session_key;
2977 uint8_t password_expired = 0;
2978 struct dcerpc_binding_handle *b = cli->binding_handle;
2980 if (argc < 4) {
2981 printf("Usage: %s username level password [password_expired]\n",
2982 argv[0]);
2983 return NT_STATUS_INVALID_PARAMETER;
2986 user = argv[1];
2987 level = atoi(argv[2]);
2988 param = argv[3];
2990 if (argc >= 5) {
2991 password_expired = atoi(argv[4]);
2994 status = cli_get_session_key(mem_ctx, cli, &session_key);
2995 if (!NT_STATUS_IS_OK(status)) {
2996 return status;
2999 init_samr_CryptPassword(param, &session_key, &pwd_buf);
3000 init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
3001 nt_lm_owf_gen(param, nt_hash, lm_hash);
3003 switch (level) {
3004 case 18:
3006 DATA_BLOB in,out;
3007 in = data_blob_const(nt_hash, 16);
3008 out = data_blob_talloc_zero(mem_ctx, 16);
3009 sess_crypt_blob(&out, &in, &session_key, true);
3010 memcpy(nt_hash, out.data, out.length);
3013 DATA_BLOB in,out;
3014 in = data_blob_const(lm_hash, 16);
3015 out = data_blob_talloc_zero(mem_ctx, 16);
3016 sess_crypt_blob(&out, &in, &session_key, true);
3017 memcpy(lm_hash, out.data, out.length);
3020 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
3021 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
3022 info.info18.nt_pwd_active = true;
3023 info.info18.lm_pwd_active = true;
3024 info.info18.password_expired = password_expired;
3026 break;
3027 case 21:
3028 ZERO_STRUCT(info.info21);
3030 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3031 SAMR_FIELD_LM_PASSWORD_PRESENT;
3032 if (argc >= 5) {
3033 info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3034 info.info21.password_expired = password_expired;
3037 info.info21.lm_password_set = true;
3038 info.info21.lm_owf_password.length = 16;
3039 info.info21.lm_owf_password.size = 16;
3041 info.info21.nt_password_set = true;
3042 info.info21.nt_owf_password.length = 16;
3043 info.info21.nt_owf_password.size = 16;
3046 DATA_BLOB in,out;
3047 in = data_blob_const(nt_hash, 16);
3048 out = data_blob_talloc_zero(mem_ctx, 16);
3049 sess_crypt_blob(&out, &in, &session_key, true);
3050 info.info21.nt_owf_password.array =
3051 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3054 DATA_BLOB in,out;
3055 in = data_blob_const(lm_hash, 16);
3056 out = data_blob_talloc_zero(mem_ctx, 16);
3057 sess_crypt_blob(&out, &in, &session_key, true);
3058 info.info21.lm_owf_password.array =
3059 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3062 break;
3063 case 23:
3064 ZERO_STRUCT(info.info23);
3066 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3067 SAMR_FIELD_LM_PASSWORD_PRESENT;
3068 if (argc >= 5) {
3069 info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3070 info.info23.info.password_expired = password_expired;
3073 info.info23.password = pwd_buf;
3075 break;
3076 case 24:
3077 info.info24.password = pwd_buf;
3078 info.info24.password_expired = password_expired;
3080 break;
3081 case 25:
3082 ZERO_STRUCT(info.info25);
3084 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3085 SAMR_FIELD_LM_PASSWORD_PRESENT;
3086 if (argc >= 5) {
3087 info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3088 info.info25.info.password_expired = password_expired;
3091 info.info25.password = pwd_buf_ex;
3093 break;
3094 case 26:
3095 info.info26.password = pwd_buf_ex;
3096 info.info26.password_expired = password_expired;
3098 break;
3099 default:
3100 return NT_STATUS_INVALID_INFO_CLASS;
3103 /* Get sam policy handle */
3105 status = rpccli_try_samr_connects(cli, mem_ctx,
3106 MAXIMUM_ALLOWED_ACCESS,
3107 &connect_pol);
3108 if (!NT_STATUS_IS_OK(status)) {
3109 goto done;
3112 /* Get domain policy handle */
3114 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3115 &connect_pol,
3116 access_mask,
3117 &domain_sid,
3118 &domain_pol,
3119 &result);
3121 if (!NT_STATUS_IS_OK(status))
3122 goto done;
3123 if (!NT_STATUS_IS_OK(result)) {
3124 status = result;
3125 goto done;
3128 user_rid = strtol(user, NULL, 0);
3129 if (user_rid) {
3130 status = dcerpc_samr_OpenUser(b, mem_ctx,
3131 &domain_pol,
3132 access_mask,
3133 user_rid,
3134 &user_pol,
3135 &result);
3136 if (!NT_STATUS_IS_OK(status)) {
3137 goto done;
3140 status = result;
3143 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3144 (user_rid == 0)) {
3146 /* Probably this was a user name, try lookupnames */
3147 struct samr_Ids rids, types;
3148 struct lsa_String lsa_acct_name;
3150 init_lsa_String(&lsa_acct_name, user);
3152 status = dcerpc_samr_LookupNames(b, mem_ctx,
3153 &domain_pol,
3155 &lsa_acct_name,
3156 &rids,
3157 &types,
3158 &result);
3159 if (!NT_STATUS_IS_OK(status)) {
3160 return status;
3162 if (!NT_STATUS_IS_OK(result)) {
3163 return result;
3167 status = dcerpc_samr_OpenUser(b, mem_ctx,
3168 &domain_pol,
3169 access_mask,
3170 rids.ids[0],
3171 &user_pol,
3172 &result);
3173 if (!NT_STATUS_IS_OK(status)) {
3174 return status;
3176 if (!NT_STATUS_IS_OK(result)) {
3177 return result;
3181 switch (opcode) {
3182 case NDR_SAMR_SETUSERINFO:
3183 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
3184 &user_pol,
3185 level,
3186 &info,
3187 &result);
3188 break;
3189 case NDR_SAMR_SETUSERINFO2:
3190 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
3191 &user_pol,
3192 level,
3193 &info,
3194 &result);
3195 break;
3196 default:
3197 return NT_STATUS_INVALID_PARAMETER;
3199 if (!NT_STATUS_IS_OK(status)) {
3200 DEBUG(0,("status: %s\n", nt_errstr(status)));
3201 goto done;
3203 if (!NT_STATUS_IS_OK(result)) {
3204 status = result;
3205 DEBUG(0,("result: %s\n", nt_errstr(status)));
3206 goto done;
3208 done:
3209 return status;
3212 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
3213 TALLOC_CTX *mem_ctx,
3214 int argc, const char **argv)
3216 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3217 NDR_SAMR_SETUSERINFO);
3220 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
3221 TALLOC_CTX *mem_ctx,
3222 int argc, const char **argv)
3224 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3225 NDR_SAMR_SETUSERINFO2);
3228 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
3229 TALLOC_CTX *mem_ctx,
3230 int argc, const char **argv)
3232 NTSTATUS status, result;
3233 struct policy_handle connect_handle;
3234 struct policy_handle domain_handle = { 0, };
3235 uint16_t level = 1;
3236 struct lsa_String name;
3237 uint32_t idx = 0;
3238 struct dcerpc_binding_handle *b = cli->binding_handle;
3240 if (argc < 2 || argc > 3) {
3241 printf("Usage: %s name level\n", argv[0]);
3242 return NT_STATUS_INVALID_PARAMETER;
3245 init_lsa_String(&name, argv[1]);
3247 if (argc == 3) {
3248 level = atoi(argv[2]);
3251 status = rpccli_try_samr_connects(cli, mem_ctx,
3252 SEC_FLAG_MAXIMUM_ALLOWED,
3253 &connect_handle);
3254 if (!NT_STATUS_IS_OK(status)) {
3255 goto done;
3258 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3259 &connect_handle,
3260 SEC_FLAG_MAXIMUM_ALLOWED,
3261 &domain_sid,
3262 &domain_handle,
3263 &result);
3264 if (!NT_STATUS_IS_OK(status)) {
3265 goto done;
3267 if (!NT_STATUS_IS_OK(result)) {
3268 status = result;
3269 goto done;
3272 status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3273 &domain_handle,
3274 level,
3275 &name,
3276 &idx,
3277 &result);
3278 if (!NT_STATUS_IS_OK(status)) {
3279 goto done;
3282 status = result;
3284 if (NT_STATUS_IS_OK(status) ||
3285 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
3286 printf("idx: %d (0x%08x)\n", idx, idx);
3288 done:
3290 if (is_valid_policy_hnd(&domain_handle)) {
3291 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
3293 if (is_valid_policy_hnd(&connect_handle)) {
3294 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
3297 return status;
3300 /* List of commands exported by this module */
3302 struct cmd_set samr_commands[] = {
3304 { "SAMR" },
3306 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, &ndr_table_samr, NULL, "Query user info", "" },
3307 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, &ndr_table_samr, NULL, "Query group info", "" },
3308 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, &ndr_table_samr, NULL, "Query user groups", "" },
3309 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, &ndr_table_samr, NULL, "Query user aliases", "" },
3310 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, &ndr_table_samr, NULL, "Query group membership", "" },
3311 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, &ndr_table_samr, NULL, "Query alias membership", "" },
3312 { "queryaliasinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo, NULL, &ndr_table_samr, NULL, "Query alias info", "" },
3313 { "deletealias", RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias, NULL, &ndr_table_samr, NULL, "Delete an alias", "" },
3314 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3315 { "querydispinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3316 { "querydispinfo3", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3317 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, &ndr_table_samr, NULL, "Query domain info", "" },
3318 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, &ndr_table_samr, NULL, "Enumerate domain users", "" },
3319 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, &ndr_table_samr, NULL, "Enumerate domain groups", "" },
3320 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, &ndr_table_samr, NULL, "Enumerate alias groups", "" },
3321 { "enumdomains", RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains, NULL, &ndr_table_samr, NULL, "Enumerate domains", "" },
3323 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, &ndr_table_samr, NULL, "Create domain user", "" },
3324 { "createdomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group, NULL, &ndr_table_samr, NULL, "Create domain group", "" },
3325 { "createdomalias", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias, NULL, &ndr_table_samr, NULL, "Create domain alias", "" },
3326 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, &ndr_table_samr, NULL, "Look up names", "" },
3327 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, &ndr_table_samr, NULL, "Look up names", "" },
3328 { "deletedomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group, NULL, &ndr_table_samr, NULL, "Delete domain group", "" },
3329 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, &ndr_table_samr, NULL, "Delete domain user", "" },
3330 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, &ndr_table_samr, NULL, "Query SAMR security object", "" },
3331 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, &ndr_table_samr, NULL, "Retrieve domain password info", "" },
3332 { "getusrdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo, NULL, &ndr_table_samr, NULL, "Retrieve user domain password info", "" },
3334 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, &ndr_table_samr, NULL, "Lookup Domain Name", "" },
3335 { "chgpasswd", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3336 { "chgpasswd2", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3337 { "chgpasswd3", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3338 { "getdispinfoidx", RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx, NULL, &ndr_table_samr, NULL, "Get Display Information Index", "" },
3339 { "setuserinfo", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo, NULL, &ndr_table_samr, NULL, "Set user info", "" },
3340 { "setuserinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2, NULL, &ndr_table_samr, NULL, "Set user info2", "" },
3341 { NULL }