PY3: relative import fixes
[Samba.git] / source3 / rpcclient / cmd_samr.c
blob19ae0aa30ea9fb13be52936cec1fb62ecf36ad4b
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_t info_level = 21;
320 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
321 union samr_UserInfo *info = NULL;
322 uint32_t user_rid = 0;
323 struct dcerpc_binding_handle *b = cli->binding_handle;
325 if ((argc < 2) || (argc > 4)) {
326 printf("Usage: %s rid [info level] [access mask] \n", argv[0]);
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;
389 if (NT_STATUS_IS_OK(result)) {
390 if (rids.count != 1) {
391 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
392 goto done;
394 if (types.count != 1) {
395 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
396 goto done;
399 status = dcerpc_samr_OpenUser(b, mem_ctx,
400 &domain_pol,
401 access_mask,
402 rids.ids[0],
403 &user_pol,
404 &result);
405 if (!NT_STATUS_IS_OK(status)) {
406 goto done;
412 if (!NT_STATUS_IS_OK(result)) {
413 status = result;
414 goto done;
417 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
418 &user_pol,
419 info_level,
420 &info,
421 &result);
422 if (!NT_STATUS_IS_OK(status)) {
423 goto done;
425 if (!NT_STATUS_IS_OK(result)) {
426 status = result;
427 goto done;
430 switch (info_level) {
431 case 7:
432 display_samr_user_info_7(&info->info7);
433 break;
434 case 9:
435 display_samr_user_info_9(&info->info9);
436 break;
437 case 16:
438 display_samr_user_info_16(&info->info16);
439 break;
440 case 20:
441 display_samr_user_info_20(&info->info20);
442 break;
443 case 21:
444 display_samr_user_info_21(&info->info21);
445 break;
446 default:
447 printf("Unsupported infolevel: %d\n", info_level);
448 break;
451 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
452 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
453 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
455 done:
456 return status;
459 /****************************************************************************
460 display group info
461 ****************************************************************************/
462 static void display_group_info1(struct samr_GroupInfoAll *info1)
464 printf("\tGroup Name:\t%s\n", info1->name.string);
465 printf("\tDescription:\t%s\n", info1->description.string);
466 printf("\tGroup Attribute:%d\n", info1->attributes);
467 printf("\tNum Members:%d\n", info1->num_members);
470 /****************************************************************************
471 display group info
472 ****************************************************************************/
473 static void display_group_info2(struct lsa_String *info2)
475 printf("\tGroup Description:%s\n", info2->string);
479 /****************************************************************************
480 display group info
481 ****************************************************************************/
482 static void display_group_info3(struct samr_GroupInfoAttributes *info3)
484 printf("\tGroup Attribute:%d\n", info3->attributes);
488 /****************************************************************************
489 display group info
490 ****************************************************************************/
491 static void display_group_info4(struct lsa_String *info4)
493 printf("\tGroup Description:%s\n", info4->string);
496 /****************************************************************************
497 display group info
498 ****************************************************************************/
499 static void display_group_info5(struct samr_GroupInfoAll *info5)
501 printf("\tGroup Name:\t%s\n", info5->name.string);
502 printf("\tDescription:\t%s\n", info5->description.string);
503 printf("\tGroup Attribute:%d\n", info5->attributes);
504 printf("\tNum Members:%d\n", info5->num_members);
507 /****************************************************************************
508 display sam sync structure
509 ****************************************************************************/
510 static void display_group_info(union samr_GroupInfo *info,
511 enum samr_GroupInfoEnum level)
513 switch (level) {
514 case 1:
515 display_group_info1(&info->all);
516 break;
517 case 2:
518 display_group_info2(&info->name);
519 break;
520 case 3:
521 display_group_info3(&info->attributes);
522 break;
523 case 4:
524 display_group_info4(&info->description);
525 break;
526 case 5:
527 display_group_info5(&info->all2);
528 break;
532 /***********************************************************************
533 * Query group information
535 static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
536 TALLOC_CTX *mem_ctx,
537 int argc, const char **argv)
539 struct policy_handle connect_pol, domain_pol, group_pol;
540 NTSTATUS status, result;
541 enum samr_GroupInfoEnum info_level = GROUPINFOALL;
542 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
543 union samr_GroupInfo *group_info = NULL;
544 uint32_t group_rid;
545 struct dcerpc_binding_handle *b = cli->binding_handle;
547 if ((argc < 2) || (argc > 4)) {
548 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
549 return NT_STATUS_OK;
552 sscanf(argv[1], "%i", &group_rid);
554 if (argc > 2)
555 info_level = atoi(argv[2]);
557 if (argc > 3)
558 sscanf(argv[3], "%x", &access_mask);
560 status = rpccli_try_samr_connects(cli, mem_ctx,
561 MAXIMUM_ALLOWED_ACCESS,
562 &connect_pol);
563 if (!NT_STATUS_IS_OK(status)) {
564 goto done;
567 status = dcerpc_samr_OpenDomain(b, mem_ctx,
568 &connect_pol,
569 MAXIMUM_ALLOWED_ACCESS,
570 &domain_sid,
571 &domain_pol,
572 &result);
573 if (!NT_STATUS_IS_OK(status)) {
574 goto done;
576 if (!NT_STATUS_IS_OK(result)) {
577 status = result;
578 goto done;
581 status = dcerpc_samr_OpenGroup(b, mem_ctx,
582 &domain_pol,
583 access_mask,
584 group_rid,
585 &group_pol,
586 &result);
587 if (!NT_STATUS_IS_OK(status)) {
588 goto done;
590 if (!NT_STATUS_IS_OK(result)) {
591 status = result;
592 goto done;
595 status = dcerpc_samr_QueryGroupInfo(b, mem_ctx,
596 &group_pol,
597 info_level,
598 &group_info,
599 &result);
600 if (!NT_STATUS_IS_OK(status)) {
601 goto done;
603 if (!NT_STATUS_IS_OK(result)) {
604 status = result;
605 goto done;
608 display_group_info(group_info, info_level);
610 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
611 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
612 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
613 done:
614 return status;
617 /* Query groups a user is a member of */
619 static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
620 TALLOC_CTX *mem_ctx,
621 int argc, const char **argv)
623 struct policy_handle connect_pol,
624 domain_pol,
625 user_pol;
626 NTSTATUS status, result;
627 uint32_t user_rid;
628 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
629 int i;
630 struct samr_RidWithAttributeArray *rid_array = NULL;
631 struct dcerpc_binding_handle *b = cli->binding_handle;
633 if ((argc < 2) || (argc > 3)) {
634 printf("Usage: %s rid [access mask]\n", argv[0]);
635 return NT_STATUS_OK;
638 sscanf(argv[1], "%i", &user_rid);
640 if (argc > 2)
641 sscanf(argv[2], "%x", &access_mask);
643 status = rpccli_try_samr_connects(cli, mem_ctx,
644 MAXIMUM_ALLOWED_ACCESS,
645 &connect_pol);
646 if (!NT_STATUS_IS_OK(status)) {
647 goto done;
650 status = dcerpc_samr_OpenDomain(b, mem_ctx,
651 &connect_pol,
652 MAXIMUM_ALLOWED_ACCESS,
653 &domain_sid,
654 &domain_pol,
655 &result);
656 if (!NT_STATUS_IS_OK(status)) {
657 goto done;
659 if (!NT_STATUS_IS_OK(result)) {
660 status = result;
661 goto done;
664 status = dcerpc_samr_OpenUser(b, mem_ctx,
665 &domain_pol,
666 access_mask,
667 user_rid,
668 &user_pol,
669 &result);
671 if (!NT_STATUS_IS_OK(status)) {
672 goto done;
674 if (!NT_STATUS_IS_OK(result)) {
675 status = result;
676 goto done;
679 status = dcerpc_samr_GetGroupsForUser(b, mem_ctx,
680 &user_pol,
681 &rid_array,
682 &result);
683 if (!NT_STATUS_IS_OK(status)) {
684 goto done;
686 if (!NT_STATUS_IS_OK(result)) {
687 status = result;
688 goto done;
691 for (i = 0; i < rid_array->count; i++) {
692 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
693 rid_array->rids[i].rid,
694 rid_array->rids[i].attributes);
697 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
698 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
699 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
700 done:
701 return status;
704 /* Query aliases a user is a member of */
706 static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
707 TALLOC_CTX *mem_ctx,
708 int argc, const char **argv)
710 struct policy_handle connect_pol, domain_pol;
711 NTSTATUS status, result;
712 struct dom_sid *sids;
713 uint32_t num_sids;
714 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
715 int i;
716 struct lsa_SidArray sid_array;
717 struct samr_Ids alias_rids;
718 struct dcerpc_binding_handle *b = cli->binding_handle;
720 if (argc < 3) {
721 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
722 return NT_STATUS_INVALID_PARAMETER;
725 sids = NULL;
726 num_sids = 0;
728 for (i=2; i<argc; i++) {
729 struct dom_sid tmp_sid;
730 if (!string_to_sid(&tmp_sid, argv[i])) {
731 printf("%s is not a legal SID\n", argv[i]);
732 return NT_STATUS_INVALID_PARAMETER;
734 result = add_sid_to_array(mem_ctx, &tmp_sid, &sids, &num_sids);
735 if (!NT_STATUS_IS_OK(result)) {
736 return result;
740 if (num_sids) {
741 sid_array.sids = talloc_zero_array(mem_ctx, struct lsa_SidPtr, num_sids);
742 if (sid_array.sids == NULL)
743 return NT_STATUS_NO_MEMORY;
744 } else {
745 sid_array.sids = NULL;
748 for (i=0; i<num_sids; i++) {
749 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
750 if (!sid_array.sids[i].sid) {
751 return NT_STATUS_NO_MEMORY;
755 sid_array.num_sids = num_sids;
757 status = rpccli_try_samr_connects(cli, mem_ctx,
758 MAXIMUM_ALLOWED_ACCESS,
759 &connect_pol);
760 if (!NT_STATUS_IS_OK(status)) {
761 goto done;
764 status = get_domain_handle(cli, mem_ctx, argv[1],
765 &connect_pol,
766 access_mask,
767 &domain_sid,
768 &domain_pol);
769 if (!NT_STATUS_IS_OK(status)) {
770 goto done;
773 status = dcerpc_samr_GetAliasMembership(b, mem_ctx,
774 &domain_pol,
775 &sid_array,
776 &alias_rids,
777 &result);
778 if (!NT_STATUS_IS_OK(status)) {
779 goto done;
781 if (!NT_STATUS_IS_OK(result)) {
782 status = result;
783 goto done;
786 for (i = 0; i < alias_rids.count; i++) {
787 printf("\tgroup rid:[0x%x]\n", alias_rids.ids[i]);
790 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
791 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
792 done:
793 return status;
796 /* Query members of a group */
798 static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
799 TALLOC_CTX *mem_ctx,
800 int argc, const char **argv)
802 struct policy_handle connect_pol, domain_pol, group_pol;
803 NTSTATUS status, result;
804 uint32_t group_rid;
805 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
806 int i;
807 unsigned int old_timeout;
808 struct samr_RidAttrArray *rids = NULL;
809 struct dcerpc_binding_handle *b = cli->binding_handle;
811 if ((argc < 2) || (argc > 3)) {
812 printf("Usage: %s rid [access mask]\n", argv[0]);
813 return NT_STATUS_OK;
816 sscanf(argv[1], "%i", &group_rid);
818 if (argc > 2)
819 sscanf(argv[2], "%x", &access_mask);
821 status = rpccli_try_samr_connects(cli, mem_ctx,
822 MAXIMUM_ALLOWED_ACCESS,
823 &connect_pol);
824 if (!NT_STATUS_IS_OK(status)) {
825 goto done;
828 status = dcerpc_samr_OpenDomain(b, mem_ctx,
829 &connect_pol,
830 MAXIMUM_ALLOWED_ACCESS,
831 &domain_sid,
832 &domain_pol,
833 &result);
834 if (!NT_STATUS_IS_OK(status)) {
835 goto done;
837 if (!NT_STATUS_IS_OK(result)) {
838 status = result;
839 goto done;
842 status = dcerpc_samr_OpenGroup(b, mem_ctx,
843 &domain_pol,
844 access_mask,
845 group_rid,
846 &group_pol,
847 &result);
848 if (!NT_STATUS_IS_OK(status)) {
849 goto done;
851 if (!NT_STATUS_IS_OK(result)) {
852 status = result;
853 goto done;
856 /* Make sure to wait for our DC's reply */
857 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
858 rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
860 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
861 &group_pol,
862 &rids,
863 &result);
865 rpccli_set_timeout(cli, old_timeout);
867 if (!NT_STATUS_IS_OK(status)) {
868 goto done;
870 if (!NT_STATUS_IS_OK(result)) {
871 status = result;
872 goto done;
875 for (i = 0; i < rids->count; i++) {
876 printf("\trid:[0x%x] attr:[0x%x]\n", rids->rids[i],
877 rids->attributes[i]);
880 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
881 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
882 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
883 done:
884 return status;
887 /* Enumerate domain users */
889 static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
890 TALLOC_CTX *mem_ctx,
891 int argc, const char **argv)
893 struct policy_handle connect_pol;
894 struct policy_handle domain_pol = { 0, };
895 NTSTATUS status, result;
896 uint32_t start_idx, num_dom_users, i;
897 struct samr_SamArray *dom_users = NULL;
898 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
899 uint32_t acb_mask = ACB_NORMAL;
900 uint32_t size = 0xffff;
901 struct dcerpc_binding_handle *b = cli->binding_handle;
903 if ((argc < 1) || (argc > 4)) {
904 printf("Usage: %s [access_mask] [acb_mask] [size]\n", argv[0]);
905 return NT_STATUS_OK;
908 if (argc > 1) {
909 sscanf(argv[1], "%x", &access_mask);
912 if (argc > 2) {
913 sscanf(argv[2], "%x", &acb_mask);
916 if (argc > 3) {
917 sscanf(argv[3], "%x", &size);
920 /* Get sam policy handle */
922 status = rpccli_try_samr_connects(cli, mem_ctx,
923 MAXIMUM_ALLOWED_ACCESS,
924 &connect_pol);
925 if (!NT_STATUS_IS_OK(status)) {
926 goto done;
929 /* Get domain policy handle */
931 status = get_domain_handle(cli, mem_ctx, "domain",
932 &connect_pol,
933 access_mask,
934 &domain_sid,
935 &domain_pol);
936 if (!NT_STATUS_IS_OK(status)) {
937 goto done;
940 /* Enumerate domain users */
942 start_idx = 0;
944 do {
945 status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
946 &domain_pol,
947 &start_idx,
948 acb_mask,
949 &dom_users,
950 size,
951 &num_dom_users,
952 &result);
953 if (!NT_STATUS_IS_OK(status)) {
954 goto done;
956 if (NT_STATUS_IS_OK(result) ||
957 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
959 for (i = 0; i < num_dom_users; i++)
960 printf("user:[%s] rid:[0x%x]\n",
961 dom_users->entries[i].name.string,
962 dom_users->entries[i].idx);
965 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
967 done:
968 if (is_valid_policy_hnd(&domain_pol))
969 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
971 if (is_valid_policy_hnd(&connect_pol))
972 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
974 return status;
977 /* Enumerate domain groups */
979 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
980 TALLOC_CTX *mem_ctx,
981 int argc, const char **argv)
983 struct policy_handle connect_pol;
984 struct policy_handle domain_pol = { 0, };
985 NTSTATUS status, result;
986 uint32_t start_idx, num_dom_groups, i;
987 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
988 struct samr_SamArray *dom_groups = NULL;
989 uint32_t size = 0xffff;
990 struct dcerpc_binding_handle *b = cli->binding_handle;
992 if ((argc < 1) || (argc > 3)) {
993 printf("Usage: %s [access_mask] [max_size]\n", argv[0]);
994 return NT_STATUS_OK;
997 if (argc > 1) {
998 sscanf(argv[1], "%x", &access_mask);
1001 if (argc > 2) {
1002 sscanf(argv[2], "%x", &size);
1005 /* Get sam policy handle */
1007 status = rpccli_try_samr_connects(cli, mem_ctx,
1008 MAXIMUM_ALLOWED_ACCESS,
1009 &connect_pol);
1010 if (!NT_STATUS_IS_OK(status)) {
1011 goto done;
1014 /* Get domain policy handle */
1016 status = get_domain_handle(cli, mem_ctx, "domain",
1017 &connect_pol,
1018 access_mask,
1019 &domain_sid,
1020 &domain_pol);
1021 if (!NT_STATUS_IS_OK(status)) {
1022 goto done;
1025 /* Enumerate domain groups */
1027 start_idx = 0;
1029 do {
1030 status = dcerpc_samr_EnumDomainGroups(b, mem_ctx,
1031 &domain_pol,
1032 &start_idx,
1033 &dom_groups,
1034 size,
1035 &num_dom_groups,
1036 &result);
1037 if (!NT_STATUS_IS_OK(status)) {
1038 goto done;
1040 if (NT_STATUS_IS_OK(result) ||
1041 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1043 for (i = 0; i < num_dom_groups; i++)
1044 printf("group:[%s] rid:[0x%x]\n",
1045 dom_groups->entries[i].name.string,
1046 dom_groups->entries[i].idx);
1049 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1051 done:
1052 if (is_valid_policy_hnd(&domain_pol))
1053 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1055 if (is_valid_policy_hnd(&connect_pol))
1056 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1058 return status;
1061 /* Enumerate alias groups */
1063 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
1064 TALLOC_CTX *mem_ctx,
1065 int argc, const char **argv)
1067 struct policy_handle connect_pol;
1068 struct policy_handle domain_pol = { 0, };
1069 NTSTATUS status, result;
1070 uint32_t start_idx, num_als_groups, i;
1071 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1072 struct samr_SamArray *als_groups = NULL;
1073 uint32_t size = 0xffff;
1074 struct dcerpc_binding_handle *b = cli->binding_handle;
1076 if ((argc < 2) || (argc > 4)) {
1077 printf("Usage: %s builtin|domain [access mask] [max_size]\n", argv[0]);
1078 return NT_STATUS_OK;
1081 if (argc > 2) {
1082 sscanf(argv[2], "%x", &access_mask);
1085 if (argc > 3) {
1086 sscanf(argv[3], "%x", &size);
1089 /* Get sam policy handle */
1091 status = rpccli_try_samr_connects(cli, mem_ctx,
1092 MAXIMUM_ALLOWED_ACCESS,
1093 &connect_pol);
1094 if (!NT_STATUS_IS_OK(status)) {
1095 goto done;
1098 /* Get domain policy handle */
1100 status = get_domain_handle(cli, mem_ctx, argv[1],
1101 &connect_pol,
1102 access_mask,
1103 &domain_sid,
1104 &domain_pol);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 goto done;
1109 /* Enumerate alias groups */
1111 start_idx = 0;
1113 do {
1114 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
1115 &domain_pol,
1116 &start_idx,
1117 &als_groups,
1118 size,
1119 &num_als_groups,
1120 &result);
1121 if (!NT_STATUS_IS_OK(status)) {
1122 goto done;
1124 if (NT_STATUS_IS_OK(result) ||
1125 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1127 for (i = 0; i < num_als_groups; i++)
1128 printf("group:[%s] rid:[0x%x]\n",
1129 als_groups->entries[i].name.string,
1130 als_groups->entries[i].idx);
1132 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1134 done:
1135 if (is_valid_policy_hnd(&domain_pol))
1136 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1138 if (is_valid_policy_hnd(&connect_pol))
1139 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1141 return status;
1144 /* Enumerate domains */
1146 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
1147 TALLOC_CTX *mem_ctx,
1148 int argc, const char **argv)
1150 struct policy_handle connect_pol;
1151 NTSTATUS status, result;
1152 uint32_t start_idx, size, num_entries, i;
1153 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1154 struct samr_SamArray *sam = NULL;
1155 struct dcerpc_binding_handle *b = cli->binding_handle;
1157 if ((argc < 1) || (argc > 2)) {
1158 printf("Usage: %s [access mask]\n", argv[0]);
1159 return NT_STATUS_OK;
1162 if (argc > 1) {
1163 sscanf(argv[1], "%x", &access_mask);
1166 /* Get sam policy handle */
1168 status = rpccli_try_samr_connects(cli, mem_ctx,
1169 access_mask,
1170 &connect_pol);
1171 if (!NT_STATUS_IS_OK(status)) {
1172 goto done;
1175 /* Enumerate alias groups */
1177 start_idx = 0;
1178 size = 0xffff;
1180 do {
1181 status = dcerpc_samr_EnumDomains(b, mem_ctx,
1182 &connect_pol,
1183 &start_idx,
1184 &sam,
1185 size,
1186 &num_entries,
1187 &result);
1188 if (!NT_STATUS_IS_OK(status)) {
1189 goto done;
1191 if (NT_STATUS_IS_OK(result) ||
1192 NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1194 for (i = 0; i < num_entries; i++)
1195 printf("name:[%s] idx:[0x%x]\n",
1196 sam->entries[i].name.string,
1197 sam->entries[i].idx);
1199 } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1201 done:
1202 if (is_valid_policy_hnd(&connect_pol)) {
1203 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1206 return status;
1210 /* Query alias membership */
1212 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1213 TALLOC_CTX *mem_ctx,
1214 int argc, const char **argv)
1216 struct policy_handle connect_pol, domain_pol, alias_pol;
1217 NTSTATUS status, result;
1218 uint32_t alias_rid, i;
1219 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1220 struct lsa_SidArray sid_array;
1221 struct dcerpc_binding_handle *b = cli->binding_handle;
1223 if ((argc < 3) || (argc > 4)) {
1224 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1225 return NT_STATUS_OK;
1228 sscanf(argv[2], "%i", &alias_rid);
1230 if (argc > 3)
1231 sscanf(argv[3], "%x", &access_mask);
1233 /* Open SAMR handle */
1235 status = rpccli_try_samr_connects(cli, mem_ctx,
1236 MAXIMUM_ALLOWED_ACCESS,
1237 &connect_pol);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 goto done;
1242 /* Open handle on domain */
1244 status = get_domain_handle(cli, mem_ctx, argv[1],
1245 &connect_pol,
1246 MAXIMUM_ALLOWED_ACCESS,
1247 &domain_sid,
1248 &domain_pol);
1249 if (!NT_STATUS_IS_OK(status)) {
1250 goto done;
1253 /* Open handle on alias */
1255 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1256 &domain_pol,
1257 access_mask,
1258 alias_rid,
1259 &alias_pol,
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 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
1270 &alias_pol,
1271 &sid_array,
1272 &result);
1273 if (!NT_STATUS_IS_OK(status)) {
1274 goto done;
1276 if (!NT_STATUS_IS_OK(result)) {
1277 status = result;
1278 goto done;
1281 for (i = 0; i < sid_array.num_sids; i++) {
1282 fstring sid_str;
1284 sid_to_fstring(sid_str, sid_array.sids[i].sid);
1285 printf("\tsid:[%s]\n", sid_str);
1288 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1289 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1290 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1291 done:
1292 return status;
1295 /* Query alias info */
1297 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
1298 TALLOC_CTX *mem_ctx,
1299 int argc, const char **argv)
1301 struct policy_handle connect_pol, domain_pol, alias_pol;
1302 NTSTATUS status, result;
1303 uint32_t alias_rid;
1304 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1305 union samr_AliasInfo *info = NULL;
1306 enum samr_AliasInfoEnum level = ALIASINFOALL;
1307 struct dcerpc_binding_handle *b = cli->binding_handle;
1309 if ((argc < 3) || (argc > 4)) {
1310 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1311 argv[0]);
1312 return NT_STATUS_OK;
1315 sscanf(argv[2], "%i", &alias_rid);
1317 if (argc > 2) {
1318 level = atoi(argv[3]);
1321 if (argc > 3) {
1322 sscanf(argv[4], "%x", &access_mask);
1325 /* Open SAMR handle */
1327 status = rpccli_try_samr_connects(cli, mem_ctx,
1328 SEC_FLAG_MAXIMUM_ALLOWED,
1329 &connect_pol);
1330 if (!NT_STATUS_IS_OK(status)) {
1331 goto done;
1334 /* Open handle on domain */
1336 status = get_domain_handle(cli, mem_ctx, argv[1],
1337 &connect_pol,
1338 SEC_FLAG_MAXIMUM_ALLOWED,
1339 &domain_sid,
1340 &domain_pol);
1341 if (!NT_STATUS_IS_OK(status)) {
1342 goto done;
1345 /* Open handle on alias */
1347 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1348 &domain_pol,
1349 access_mask,
1350 alias_rid,
1351 &alias_pol,
1352 &result);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 goto done;
1356 if (!NT_STATUS_IS_OK(result)) {
1357 status = result;
1358 goto done;
1361 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
1362 &alias_pol,
1363 level,
1364 &info,
1365 &result);
1366 if (!NT_STATUS_IS_OK(status)) {
1367 goto done;
1369 if (!NT_STATUS_IS_OK(result)) {
1370 status = result;
1371 goto done;
1374 switch (level) {
1375 case ALIASINFOALL:
1376 printf("Name: %s\n", info->all.name.string);
1377 printf("Description: %s\n", info->all.description.string);
1378 printf("Num Members: %d\n", info->all.num_members);
1379 break;
1380 case ALIASINFONAME:
1381 printf("Name: %s\n", info->name.string);
1382 break;
1383 case ALIASINFODESCRIPTION:
1384 printf("Description: %s\n", info->description.string);
1385 break;
1386 default:
1387 break;
1390 dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1391 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1392 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1393 done:
1394 return status;
1398 /* Query delete an alias membership */
1400 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1401 TALLOC_CTX *mem_ctx,
1402 int argc, const char **argv)
1404 struct policy_handle connect_pol, domain_pol, alias_pol;
1405 NTSTATUS status, result;
1406 uint32_t alias_rid;
1407 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1408 struct dcerpc_binding_handle *b = cli->binding_handle;
1410 if (argc != 3) {
1411 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1412 return NT_STATUS_OK;
1415 alias_rid = strtoul(argv[2], NULL, 10);
1417 /* Open SAMR handle */
1419 status = rpccli_try_samr_connects(cli, mem_ctx,
1420 MAXIMUM_ALLOWED_ACCESS,
1421 &connect_pol);
1422 if (!NT_STATUS_IS_OK(status)) {
1423 goto done;
1426 /* Open handle on domain */
1428 status = get_domain_handle(cli, mem_ctx, argv[1],
1429 &connect_pol,
1430 MAXIMUM_ALLOWED_ACCESS,
1431 &domain_sid,
1432 &domain_pol);
1433 if (!NT_STATUS_IS_OK(status)) {
1434 goto done;
1437 /* Open handle on alias */
1439 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1440 &domain_pol,
1441 access_mask,
1442 alias_rid,
1443 &alias_pol,
1444 &result);
1445 if (!NT_STATUS_IS_OK(status)) {
1446 goto done;
1448 if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1449 /* Probably this was a user name, try lookupnames */
1450 struct samr_Ids rids, types;
1451 struct lsa_String lsa_acct_name;
1453 init_lsa_String(&lsa_acct_name, argv[2]);
1455 status = dcerpc_samr_LookupNames(b, mem_ctx,
1456 &domain_pol,
1458 &lsa_acct_name,
1459 &rids,
1460 &types,
1461 &result);
1462 if (!NT_STATUS_IS_OK(status)) {
1463 goto done;
1465 if (NT_STATUS_IS_OK(result)) {
1466 if (rids.count != 1) {
1467 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1468 goto done;
1470 if (types.count != 1) {
1471 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1472 goto done;
1475 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1476 &domain_pol,
1477 access_mask,
1478 rids.ids[0],
1479 &alias_pol,
1480 &result);
1481 if (!NT_STATUS_IS_OK(status)) {
1482 goto done;
1487 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1488 &alias_pol,
1489 &result);
1490 if (!NT_STATUS_IS_OK(status)) {
1491 goto done;
1493 if (!NT_STATUS_IS_OK(result)) {
1494 status = result;
1495 goto done;
1498 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1499 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1500 done:
1501 return status;
1504 /* Query display info */
1506 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1507 TALLOC_CTX *mem_ctx,
1508 int argc, const char **argv,
1509 uint32_t opcode)
1511 struct policy_handle connect_pol, domain_pol;
1512 NTSTATUS status, result;
1513 uint32_t start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1514 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1515 uint32_t info_level = 1;
1516 union samr_DispInfo info;
1517 int loop_count = 0;
1518 bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1519 uint32_t total_size, returned_size;
1520 struct dcerpc_binding_handle *b = cli->binding_handle;
1522 if (argc > 6) {
1523 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1524 return NT_STATUS_OK;
1527 if (argc >= 2)
1528 sscanf(argv[1], "%i", &info_level);
1530 if (argc >= 3)
1531 sscanf(argv[2], "%i", &start_idx);
1533 if (argc >= 4) {
1534 sscanf(argv[3], "%i", &max_entries);
1535 got_params = True;
1538 if (argc >= 5) {
1539 sscanf(argv[4], "%i", &max_size);
1540 got_params = True;
1543 if (argc >= 6)
1544 sscanf(argv[5], "%x", &access_mask);
1546 /* Get sam policy handle */
1548 status = rpccli_try_samr_connects(cli, mem_ctx,
1549 MAXIMUM_ALLOWED_ACCESS,
1550 &connect_pol);
1551 if (!NT_STATUS_IS_OK(status)) {
1552 goto done;
1555 /* Get domain policy handle */
1557 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1558 &connect_pol,
1559 access_mask,
1560 &domain_sid,
1561 &domain_pol,
1562 &result);
1563 if (!NT_STATUS_IS_OK(status)) {
1564 goto done;
1566 if (!NT_STATUS_IS_OK(result)) {
1567 status = result;
1568 goto done;
1571 /* Query display info */
1573 do {
1575 if (!got_params)
1576 dcerpc_get_query_dispinfo_params(
1577 loop_count, &max_entries, &max_size);
1579 switch (opcode) {
1580 case NDR_SAMR_QUERYDISPLAYINFO:
1581 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1582 &domain_pol,
1583 info_level,
1584 start_idx,
1585 max_entries,
1586 max_size,
1587 &total_size,
1588 &returned_size,
1589 &info,
1590 &result);
1591 break;
1592 case NDR_SAMR_QUERYDISPLAYINFO2:
1593 status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1594 &domain_pol,
1595 info_level,
1596 start_idx,
1597 max_entries,
1598 max_size,
1599 &total_size,
1600 &returned_size,
1601 &info,
1602 &result);
1604 break;
1605 case NDR_SAMR_QUERYDISPLAYINFO3:
1606 status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1607 &domain_pol,
1608 info_level,
1609 start_idx,
1610 max_entries,
1611 max_size,
1612 &total_size,
1613 &returned_size,
1614 &info,
1615 &result);
1617 break;
1618 default:
1619 return NT_STATUS_INVALID_PARAMETER;
1622 if (!NT_STATUS_IS_OK(status)) {
1623 break;
1625 status = result;
1626 if (!NT_STATUS_IS_OK(result) &&
1627 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1628 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1629 break;
1632 loop_count++;
1634 switch (info_level) {
1635 case 1:
1636 num_entries = info.info1.count;
1637 break;
1638 case 2:
1639 num_entries = info.info2.count;
1640 break;
1641 case 3:
1642 num_entries = info.info3.count;
1643 break;
1644 case 4:
1645 num_entries = info.info4.count;
1646 break;
1647 case 5:
1648 num_entries = info.info5.count;
1649 break;
1650 default:
1651 break;
1654 start_idx += num_entries;
1656 if (num_entries == 0)
1657 break;
1659 for (i = 0; i < num_entries; i++) {
1660 switch (info_level) {
1661 case 1:
1662 display_sam_info_1(&info.info1.entries[i]);
1663 break;
1664 case 2:
1665 display_sam_info_2(&info.info2.entries[i]);
1666 break;
1667 case 3:
1668 display_sam_info_3(&info.info3.entries[i]);
1669 break;
1670 case 4:
1671 display_sam_info_4(&info.info4.entries[i]);
1672 break;
1673 case 5:
1674 display_sam_info_5(&info.info5.entries[i]);
1675 break;
1678 } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1680 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1681 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1682 done:
1683 return status;
1686 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1687 TALLOC_CTX *mem_ctx,
1688 int argc, const char **argv)
1690 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1691 NDR_SAMR_QUERYDISPLAYINFO);
1694 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1695 TALLOC_CTX *mem_ctx,
1696 int argc, const char **argv)
1698 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1699 NDR_SAMR_QUERYDISPLAYINFO2);
1702 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1703 TALLOC_CTX *mem_ctx,
1704 int argc, const char **argv)
1706 return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1707 NDR_SAMR_QUERYDISPLAYINFO3);
1710 /* Query domain info */
1712 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1713 TALLOC_CTX *mem_ctx,
1714 int argc, const char **argv)
1716 struct policy_handle connect_pol, domain_pol;
1717 NTSTATUS status, result;
1718 uint32_t switch_level = 2;
1719 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1720 union samr_DomainInfo *info = NULL;
1721 struct dcerpc_binding_handle *b = cli->binding_handle;
1723 if (argc > 3) {
1724 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1725 return NT_STATUS_OK;
1728 if (argc > 1)
1729 sscanf(argv[1], "%i", &switch_level);
1731 if (argc > 2)
1732 sscanf(argv[2], "%x", &access_mask);
1734 /* Get sam policy handle */
1736 status = rpccli_try_samr_connects(cli, mem_ctx,
1737 MAXIMUM_ALLOWED_ACCESS,
1738 &connect_pol);
1739 if (!NT_STATUS_IS_OK(status)) {
1740 goto done;
1743 /* Get domain policy handle */
1745 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1746 &connect_pol,
1747 access_mask,
1748 &domain_sid,
1749 &domain_pol,
1750 &result);
1751 if (!NT_STATUS_IS_OK(status)) {
1752 goto done;
1754 if (!NT_STATUS_IS_OK(result)) {
1755 status = result;
1756 goto done;
1759 /* Query domain info */
1761 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1762 &domain_pol,
1763 switch_level,
1764 &info,
1765 &result);
1766 if (!NT_STATUS_IS_OK(status)) {
1767 goto done;
1769 if (!NT_STATUS_IS_OK(result)) {
1770 status = result;
1771 goto done;
1774 /* Display domain info */
1776 switch (switch_level) {
1777 case 1:
1778 display_sam_dom_info_1(&info->info1);
1779 break;
1780 case 2:
1781 display_sam_dom_info_2(&info->general);
1782 break;
1783 case 3:
1784 display_sam_dom_info_3(&info->info3);
1785 break;
1786 case 4:
1787 display_sam_dom_info_4(&info->oem);
1788 break;
1789 case 5:
1790 display_sam_dom_info_5(&info->info5);
1791 break;
1792 case 6:
1793 display_sam_dom_info_6(&info->info6);
1794 break;
1795 case 7:
1796 display_sam_dom_info_7(&info->info7);
1797 break;
1798 case 8:
1799 display_sam_dom_info_8(&info->info8);
1800 break;
1801 case 9:
1802 display_sam_dom_info_9(&info->info9);
1803 break;
1804 case 12:
1805 display_sam_dom_info_12(&info->info12);
1806 break;
1807 case 13:
1808 display_sam_dom_info_13(&info->info13);
1809 break;
1811 default:
1812 printf("cannot display domain info for switch value %d\n",
1813 switch_level);
1814 break;
1817 done:
1819 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1820 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1821 return status;
1824 /* Create domain user */
1826 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1827 TALLOC_CTX *mem_ctx,
1828 int argc, const char **argv)
1830 struct policy_handle connect_pol, domain_pol, user_pol;
1831 NTSTATUS status, result;
1832 struct lsa_String acct_name;
1833 uint32_t acb_info;
1834 uint32_t acct_flags, user_rid;
1835 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1836 uint32_t access_granted = 0;
1837 struct dcerpc_binding_handle *b = cli->binding_handle;
1839 if ((argc < 2) || (argc > 3)) {
1840 printf("Usage: %s username [access mask]\n", argv[0]);
1841 return NT_STATUS_OK;
1844 init_lsa_String(&acct_name, argv[1]);
1846 if (argc > 2)
1847 sscanf(argv[2], "%x", &access_mask);
1849 /* Get sam policy handle */
1851 status = rpccli_try_samr_connects(cli, mem_ctx,
1852 MAXIMUM_ALLOWED_ACCESS,
1853 &connect_pol);
1854 if (!NT_STATUS_IS_OK(status)) {
1855 goto done;
1858 /* Get domain policy handle */
1860 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1861 &connect_pol,
1862 access_mask,
1863 &domain_sid,
1864 &domain_pol,
1865 &result);
1866 if (!NT_STATUS_IS_OK(status)) {
1867 goto done;
1869 if (!NT_STATUS_IS_OK(result)) {
1870 status = result;
1871 goto done;
1874 /* Create domain user */
1876 acb_info = ACB_NORMAL;
1877 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1878 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1879 SAMR_USER_ACCESS_SET_PASSWORD |
1880 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1881 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1883 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1884 &domain_pol,
1885 &acct_name,
1886 acb_info,
1887 acct_flags,
1888 &user_pol,
1889 &access_granted,
1890 &user_rid,
1891 &result);
1892 if (!NT_STATUS_IS_OK(status)) {
1893 goto done;
1895 if (!NT_STATUS_IS_OK(result)) {
1896 status = result;
1897 goto done;
1900 status = dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1901 if (!NT_STATUS_IS_OK(status)) goto done;
1903 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1904 if (!NT_STATUS_IS_OK(status)) goto done;
1906 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1907 if (!NT_STATUS_IS_OK(status)) goto done;
1909 done:
1910 return status;
1913 /* Create domain group */
1915 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1916 TALLOC_CTX *mem_ctx,
1917 int argc, const char **argv)
1919 struct policy_handle connect_pol, domain_pol, group_pol;
1920 NTSTATUS status, result;
1921 struct lsa_String grp_name;
1922 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1923 uint32_t rid = 0;
1924 struct dcerpc_binding_handle *b = cli->binding_handle;
1926 if ((argc < 2) || (argc > 3)) {
1927 printf("Usage: %s groupname [access mask]\n", argv[0]);
1928 return NT_STATUS_OK;
1931 init_lsa_String(&grp_name, argv[1]);
1933 if (argc > 2)
1934 sscanf(argv[2], "%x", &access_mask);
1936 /* Get sam policy handle */
1938 status = rpccli_try_samr_connects(cli, mem_ctx,
1939 MAXIMUM_ALLOWED_ACCESS,
1940 &connect_pol);
1941 if (!NT_STATUS_IS_OK(status)) {
1942 goto done;
1945 /* Get domain policy handle */
1947 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1948 &connect_pol,
1949 access_mask,
1950 &domain_sid,
1951 &domain_pol,
1952 &result);
1953 if (!NT_STATUS_IS_OK(status)) {
1954 goto done;
1956 if (!NT_STATUS_IS_OK(result)) {
1957 status = result;
1958 goto done;
1961 /* Create domain user */
1962 status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
1963 &domain_pol,
1964 &grp_name,
1965 MAXIMUM_ALLOWED_ACCESS,
1966 &group_pol,
1967 &rid,
1968 &result);
1969 if (!NT_STATUS_IS_OK(status)) {
1970 goto done;
1972 if (!NT_STATUS_IS_OK(result)) {
1973 status = result;
1974 goto done;
1977 status = dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
1978 if (!NT_STATUS_IS_OK(status)) goto done;
1980 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1981 if (!NT_STATUS_IS_OK(status)) goto done;
1983 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1984 if (!NT_STATUS_IS_OK(status)) goto done;
1986 done:
1987 return status;
1990 /* Create domain alias */
1992 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1993 TALLOC_CTX *mem_ctx,
1994 int argc, const char **argv)
1996 struct policy_handle connect_pol, domain_pol, alias_pol;
1997 NTSTATUS status, result;
1998 struct lsa_String alias_name;
1999 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2000 uint32_t rid = 0;
2001 struct dcerpc_binding_handle *b = cli->binding_handle;
2003 if ((argc < 2) || (argc > 3)) {
2004 printf("Usage: %s aliasname [access mask]\n", argv[0]);
2005 return NT_STATUS_OK;
2008 init_lsa_String(&alias_name, argv[1]);
2010 if (argc > 2)
2011 sscanf(argv[2], "%x", &access_mask);
2013 /* Get sam policy handle */
2015 status = rpccli_try_samr_connects(cli, mem_ctx,
2016 MAXIMUM_ALLOWED_ACCESS,
2017 &connect_pol);
2018 if (!NT_STATUS_IS_OK(status)) {
2019 goto done;
2022 /* Get domain policy handle */
2024 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2025 &connect_pol,
2026 access_mask,
2027 &domain_sid,
2028 &domain_pol,
2029 &result);
2030 if (!NT_STATUS_IS_OK(status)) {
2031 goto done;
2033 if (!NT_STATUS_IS_OK(result)) {
2034 status = result;
2035 goto done;
2038 /* Create domain user */
2040 status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2041 &domain_pol,
2042 &alias_name,
2043 MAXIMUM_ALLOWED_ACCESS,
2044 &alias_pol,
2045 &rid,
2046 &result);
2047 if (!NT_STATUS_IS_OK(status)) {
2048 goto done;
2050 if (!NT_STATUS_IS_OK(result)) {
2051 status = result;
2052 goto done;
2056 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
2057 if (!NT_STATUS_IS_OK(status)) goto done;
2059 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2060 if (!NT_STATUS_IS_OK(status)) goto done;
2062 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2063 if (!NT_STATUS_IS_OK(status)) goto done;
2065 done:
2066 return status;
2069 /* Lookup sam names */
2071 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
2072 TALLOC_CTX *mem_ctx,
2073 int argc, const char **argv)
2075 NTSTATUS status, result;
2076 struct policy_handle connect_pol, domain_pol;
2077 uint32_t num_names;
2078 struct samr_Ids rids, name_types;
2079 int i;
2080 struct lsa_String *names = NULL;
2081 struct dcerpc_binding_handle *b = cli->binding_handle;
2083 if (argc < 3) {
2084 printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
2085 printf("check on the domain SID: S-1-5-21-x-y-z\n");
2086 printf("or check on the builtin SID: S-1-5-32\n");
2087 return NT_STATUS_OK;
2090 /* Get sam policy and domain handles */
2092 status = rpccli_try_samr_connects(cli, mem_ctx,
2093 MAXIMUM_ALLOWED_ACCESS,
2094 &connect_pol);
2095 if (!NT_STATUS_IS_OK(status)) {
2096 goto done;
2099 status = get_domain_handle(cli, mem_ctx, argv[1],
2100 &connect_pol,
2101 MAXIMUM_ALLOWED_ACCESS,
2102 &domain_sid,
2103 &domain_pol);
2104 if (!NT_STATUS_IS_OK(status)) {
2105 goto done;
2108 /* Look up names */
2110 num_names = argc - 2;
2112 if ((names = talloc_array(mem_ctx, struct lsa_String, num_names)) == NULL) {
2113 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2114 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2115 status = NT_STATUS_NO_MEMORY;
2116 goto done;
2119 for (i = 0; i < num_names; i++) {
2120 init_lsa_String(&names[i], argv[i + 2]);
2123 status = dcerpc_samr_LookupNames(b, mem_ctx,
2124 &domain_pol,
2125 num_names,
2126 names,
2127 &rids,
2128 &name_types,
2129 &result);
2130 if (!NT_STATUS_IS_OK(status)) {
2131 goto done;
2133 if (!NT_STATUS_IS_OK(result)) {
2134 status = result;
2135 goto done;
2137 if (rids.count != num_names) {
2138 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2139 goto done;
2141 if (name_types.count != num_names) {
2142 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2143 goto done;
2146 /* Display results */
2148 for (i = 0; i < num_names; i++)
2149 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
2150 name_types.ids[i]);
2152 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2153 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2154 done:
2155 return status;
2158 /* Lookup sam rids */
2160 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
2161 TALLOC_CTX *mem_ctx,
2162 int argc, const char **argv)
2164 NTSTATUS status, result;
2165 struct policy_handle connect_pol, domain_pol;
2166 uint32_t num_rids, *rids;
2167 struct lsa_Strings names;
2168 struct samr_Ids types;
2169 struct dcerpc_binding_handle *b = cli->binding_handle;
2171 int i;
2173 if (argc < 3) {
2174 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
2175 return NT_STATUS_OK;
2178 /* Get sam policy and domain handles */
2180 status = rpccli_try_samr_connects(cli, mem_ctx,
2181 MAXIMUM_ALLOWED_ACCESS,
2182 &connect_pol);
2183 if (!NT_STATUS_IS_OK(status)) {
2184 goto done;
2187 status = get_domain_handle(cli, mem_ctx, argv[1],
2188 &connect_pol,
2189 MAXIMUM_ALLOWED_ACCESS,
2190 &domain_sid,
2191 &domain_pol);
2192 if (!NT_STATUS_IS_OK(status)) {
2193 goto done;
2196 /* Look up rids */
2198 num_rids = argc - 2;
2200 if ((rids = talloc_array(mem_ctx, uint32_t, num_rids)) == NULL) {
2201 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2202 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2203 status = NT_STATUS_NO_MEMORY;
2204 goto done;
2207 for (i = 0; i < argc - 2; i++)
2208 sscanf(argv[i + 2], "%i", &rids[i]);
2210 status = dcerpc_samr_LookupRids(b, mem_ctx,
2211 &domain_pol,
2212 num_rids,
2213 rids,
2214 &names,
2215 &types,
2216 &result);
2217 if (!NT_STATUS_IS_OK(status)) {
2218 goto done;
2220 status = result;
2221 if (!NT_STATUS_IS_OK(result) &&
2222 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2223 goto done;
2225 /* Display results */
2226 if (num_rids != names.count) {
2227 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2228 goto done;
2230 if (num_rids != types.count) {
2231 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2232 goto done;
2235 for (i = 0; i < num_rids; i++) {
2236 printf("rid 0x%x: %s (%d)\n",
2237 rids[i], names.names[i].string, types.ids[i]);
2240 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2241 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2242 done:
2243 return status;
2246 /* Delete domain group */
2248 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
2249 TALLOC_CTX *mem_ctx,
2250 int argc, const char **argv)
2252 NTSTATUS status, result;
2253 struct policy_handle connect_pol, domain_pol, group_pol;
2254 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2255 struct dcerpc_binding_handle *b = cli->binding_handle;
2257 if ((argc < 2) || (argc > 3)) {
2258 printf("Usage: %s groupname\n", argv[0]);
2259 return NT_STATUS_OK;
2262 if (argc > 2)
2263 sscanf(argv[2], "%x", &access_mask);
2265 /* Get sam policy and domain handles */
2267 status = rpccli_try_samr_connects(cli, mem_ctx,
2268 MAXIMUM_ALLOWED_ACCESS,
2269 &connect_pol);
2270 if (!NT_STATUS_IS_OK(status)) {
2271 goto done;
2274 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2275 &connect_pol,
2276 MAXIMUM_ALLOWED_ACCESS,
2277 &domain_sid,
2278 &domain_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;
2288 /* Get handle on group */
2291 struct samr_Ids group_rids, name_types;
2292 struct lsa_String lsa_acct_name;
2294 init_lsa_String(&lsa_acct_name, argv[1]);
2296 status = dcerpc_samr_LookupNames(b, mem_ctx,
2297 &domain_pol,
2299 &lsa_acct_name,
2300 &group_rids,
2301 &name_types,
2302 &result);
2303 if (!NT_STATUS_IS_OK(status)) {
2304 goto done;
2306 if (!NT_STATUS_IS_OK(result)) {
2307 status = result;
2308 goto done;
2310 if (group_rids.count != 1) {
2311 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2312 goto done;
2314 if (name_types.count != 1) {
2315 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2316 goto done;
2319 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2320 &domain_pol,
2321 access_mask,
2322 group_rids.ids[0],
2323 &group_pol,
2324 &result);
2325 if (!NT_STATUS_IS_OK(status)) {
2326 goto done;
2328 if (!NT_STATUS_IS_OK(result)) {
2329 status = result;
2330 goto done;
2334 /* Delete group */
2336 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2337 &group_pol,
2338 &result);
2339 if (!NT_STATUS_IS_OK(status)) {
2340 goto done;
2342 if (!NT_STATUS_IS_OK(result)) {
2343 status = result;
2344 goto done;
2347 /* Display results */
2349 dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2350 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2351 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2353 done:
2354 return status;
2357 /* Delete domain user */
2359 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2360 TALLOC_CTX *mem_ctx,
2361 int argc, const char **argv)
2363 NTSTATUS status, result;
2364 struct policy_handle connect_pol, domain_pol, user_pol;
2365 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2366 struct dcerpc_binding_handle *b = cli->binding_handle;
2368 if ((argc < 2) || (argc > 3)) {
2369 printf("Usage: %s username\n", argv[0]);
2370 return NT_STATUS_OK;
2373 if (argc > 2)
2374 sscanf(argv[2], "%x", &access_mask);
2376 /* Get sam policy and domain handles */
2378 status = rpccli_try_samr_connects(cli, mem_ctx,
2379 MAXIMUM_ALLOWED_ACCESS,
2380 &connect_pol);
2381 if (!NT_STATUS_IS_OK(status)) {
2382 goto done;
2385 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2386 &connect_pol,
2387 MAXIMUM_ALLOWED_ACCESS,
2388 &domain_sid,
2389 &domain_pol,
2390 &result);
2391 if (!NT_STATUS_IS_OK(status)) {
2392 goto done;
2394 if (!NT_STATUS_IS_OK(result)) {
2395 status = result;
2396 goto done;
2399 /* Get handle on user */
2402 struct samr_Ids user_rids, name_types;
2403 struct lsa_String lsa_acct_name;
2405 init_lsa_String(&lsa_acct_name, argv[1]);
2407 status = dcerpc_samr_LookupNames(b, mem_ctx,
2408 &domain_pol,
2410 &lsa_acct_name,
2411 &user_rids,
2412 &name_types,
2413 &result);
2414 if (!NT_STATUS_IS_OK(status)) {
2415 goto done;
2417 if (!NT_STATUS_IS_OK(result)) {
2418 status = result;
2419 goto done;
2421 if (user_rids.count != 1) {
2422 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2423 goto done;
2425 if (name_types.count != 1) {
2426 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2427 goto done;
2430 status = dcerpc_samr_OpenUser(b, mem_ctx,
2431 &domain_pol,
2432 access_mask,
2433 user_rids.ids[0],
2434 &user_pol,
2435 &result);
2436 if (!NT_STATUS_IS_OK(status)) {
2437 goto done;
2439 if (!NT_STATUS_IS_OK(result)) {
2440 status = result;
2441 goto done;
2445 /* Delete user */
2447 status = dcerpc_samr_DeleteUser(b, mem_ctx,
2448 &user_pol,
2449 &result);
2450 if (!NT_STATUS_IS_OK(status)) {
2451 goto done;
2453 if (!NT_STATUS_IS_OK(result)) {
2454 status = result;
2455 goto done;
2458 /* Display results */
2460 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2461 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2462 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2464 done:
2465 return status;
2468 /**********************************************************************
2469 * Query user security object
2471 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2472 TALLOC_CTX *mem_ctx,
2473 int argc, const char **argv)
2475 struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2476 NTSTATUS status, result;
2477 uint32_t sec_info = SECINFO_DACL;
2478 uint32_t user_rid = 0;
2479 TALLOC_CTX *ctx = NULL;
2480 struct sec_desc_buf *sec_desc_buf=NULL;
2481 bool domain = False;
2482 struct dcerpc_binding_handle *b = cli->binding_handle;
2484 ctx=talloc_init("cmd_samr_query_sec_obj");
2486 if ((argc < 1) || (argc > 3)) {
2487 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2488 printf("\tSpecify rid for security on user, -d for security on domain\n");
2489 talloc_destroy(ctx);
2490 return NT_STATUS_OK;
2493 if (argc > 1) {
2494 if (strcmp(argv[1], "-d") == 0)
2495 domain = True;
2496 else
2497 sscanf(argv[1], "%i", &user_rid);
2500 if (argc == 3) {
2501 sec_info = atoi(argv[2]);
2504 status = rpccli_try_samr_connects(cli, mem_ctx,
2505 MAXIMUM_ALLOWED_ACCESS,
2506 &connect_pol);
2507 if (!NT_STATUS_IS_OK(status)) {
2508 goto done;
2511 if (domain || user_rid) {
2512 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2513 &connect_pol,
2514 MAXIMUM_ALLOWED_ACCESS,
2515 &domain_sid,
2516 &domain_pol,
2517 &result);
2518 if (!NT_STATUS_IS_OK(status)) {
2519 goto done;
2521 if (!NT_STATUS_IS_OK(result)) {
2522 status = result;
2523 goto done;
2527 if (user_rid) {
2528 status = dcerpc_samr_OpenUser(b, mem_ctx,
2529 &domain_pol,
2530 MAXIMUM_ALLOWED_ACCESS,
2531 user_rid,
2532 &user_pol,
2533 &result);
2534 if (!NT_STATUS_IS_OK(status)) {
2535 goto done;
2537 if (!NT_STATUS_IS_OK(result)) {
2538 status = result;
2539 goto done;
2543 /* Pick which query pol to use */
2545 pol = &connect_pol;
2547 if (domain)
2548 pol = &domain_pol;
2550 if (user_rid)
2551 pol = &user_pol;
2553 /* Query SAM security object */
2555 status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2556 pol,
2557 sec_info,
2558 &sec_desc_buf,
2559 &result);
2560 if (!NT_STATUS_IS_OK(status)) {
2561 goto done;
2563 if (!NT_STATUS_IS_OK(result)) {
2564 status = result;
2565 goto done;
2568 display_sec_desc(sec_desc_buf->sd);
2570 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2571 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2572 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2573 done:
2574 talloc_destroy(ctx);
2575 return status;
2578 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2579 TALLOC_CTX *mem_ctx,
2580 int argc, const char **argv)
2582 NTSTATUS status, result;
2583 struct policy_handle connect_pol, domain_pol, user_pol;
2584 struct samr_PwInfo info;
2585 uint32_t rid;
2586 struct dcerpc_binding_handle *b = cli->binding_handle;
2588 if (argc != 2) {
2589 printf("Usage: %s rid\n", argv[0]);
2590 return NT_STATUS_OK;
2593 sscanf(argv[1], "%i", &rid);
2595 status = rpccli_try_samr_connects(cli, mem_ctx,
2596 MAXIMUM_ALLOWED_ACCESS,
2597 &connect_pol);
2598 if (!NT_STATUS_IS_OK(status)) {
2599 goto done;
2602 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2603 &connect_pol,
2604 MAXIMUM_ALLOWED_ACCESS,
2605 &domain_sid,
2606 &domain_pol,
2607 &result);
2608 if (!NT_STATUS_IS_OK(status)) {
2609 goto done;
2611 if (!NT_STATUS_IS_OK(result)) {
2612 status = result;
2613 goto done;
2616 status = dcerpc_samr_OpenUser(b, mem_ctx,
2617 &domain_pol,
2618 MAXIMUM_ALLOWED_ACCESS,
2619 rid,
2620 &user_pol,
2621 &result);
2622 if (!NT_STATUS_IS_OK(status)) {
2623 goto done;
2625 if (!NT_STATUS_IS_OK(result)) {
2626 status = result;
2627 goto done;
2630 status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2631 &user_pol,
2632 &info,
2633 &result);
2634 if (!NT_STATUS_IS_OK(status)) {
2635 goto done;
2637 status = result;
2638 if (NT_STATUS_IS_OK(result)) {
2639 printf("min_password_length: %d\n", info.min_password_length);
2640 printf("%s\n",
2641 NDR_PRINT_STRUCT_STRING(mem_ctx,
2642 samr_PasswordProperties, &info.password_properties));
2645 done:
2646 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2647 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2648 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2650 return status;
2653 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2654 TALLOC_CTX *mem_ctx,
2655 int argc, const char **argv)
2657 NTSTATUS status, result;
2658 struct lsa_String domain_name;
2659 struct samr_PwInfo info;
2660 struct dcerpc_binding_handle *b = cli->binding_handle;
2662 if (argc < 1 || argc > 3) {
2663 printf("Usage: %s <domain>\n", argv[0]);
2664 return NT_STATUS_OK;
2667 init_lsa_String(&domain_name, argv[1]);
2669 status = dcerpc_samr_GetDomPwInfo(b, mem_ctx,
2670 &domain_name,
2671 &info,
2672 &result);
2673 if (!NT_STATUS_IS_OK(status)) {
2674 return status;
2676 if (NT_STATUS_IS_OK(result)) {
2677 printf("min_password_length: %d\n", info.min_password_length);
2678 display_password_properties(info.password_properties);
2681 return result;
2684 /* Look up domain name */
2686 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2687 TALLOC_CTX *mem_ctx,
2688 int argc, const char **argv)
2690 struct policy_handle connect_pol, domain_pol;
2691 NTSTATUS status, result;
2692 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2693 fstring sid_string;
2694 struct lsa_String domain_name;
2695 struct dom_sid *sid = NULL;
2696 struct dcerpc_binding_handle *b = cli->binding_handle;
2698 if (argc != 2) {
2699 printf("Usage: %s domain_name\n", argv[0]);
2700 return NT_STATUS_OK;
2703 init_lsa_String(&domain_name, argv[1]);
2705 status = rpccli_try_samr_connects(cli, mem_ctx,
2706 access_mask,
2707 &connect_pol);
2708 if (!NT_STATUS_IS_OK(status)) {
2709 goto done;
2712 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2713 &connect_pol,
2714 access_mask,
2715 &domain_sid,
2716 &domain_pol,
2717 &result);
2718 if (!NT_STATUS_IS_OK(status)) {
2719 goto done;
2721 if (!NT_STATUS_IS_OK(result)) {
2722 status = result;
2723 goto done;
2726 status = dcerpc_samr_LookupDomain(b, mem_ctx,
2727 &connect_pol,
2728 &domain_name,
2729 &sid,
2730 &result);
2731 if (!NT_STATUS_IS_OK(status)) {
2732 goto done;
2734 if (!NT_STATUS_IS_OK(result)) {
2735 status = result;
2736 goto done;
2739 if (NT_STATUS_IS_OK(result)) {
2740 sid_to_fstring(sid_string, sid);
2741 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2742 argv[1], sid_string);
2745 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2746 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2747 done:
2748 return status;
2751 /* Change user password */
2753 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2754 TALLOC_CTX *mem_ctx,
2755 int argc, const char **argv)
2757 struct policy_handle connect_pol;
2758 struct policy_handle domain_pol = { 0, };
2759 struct policy_handle user_pol = { 0, };
2760 NTSTATUS status, result;
2761 const char *user, *oldpass, *newpass;
2762 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2763 struct samr_Ids rids, types;
2764 struct lsa_String lsa_acct_name;
2765 struct dcerpc_binding_handle *b = cli->binding_handle;
2767 if (argc < 3) {
2768 printf("Usage: %s username oldpass newpass\n", argv[0]);
2769 return NT_STATUS_INVALID_PARAMETER;
2772 user = argv[1];
2773 oldpass = argv[2];
2774 newpass = argv[3];
2776 /* Get sam policy handle */
2778 status = rpccli_try_samr_connects(cli, mem_ctx,
2779 MAXIMUM_ALLOWED_ACCESS,
2780 &connect_pol);
2781 if (!NT_STATUS_IS_OK(status)) {
2782 goto done;
2785 /* Get domain policy handle */
2787 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2788 &connect_pol,
2789 access_mask,
2790 &domain_sid,
2791 &domain_pol,
2792 &result);
2793 if (!NT_STATUS_IS_OK(status)) {
2794 goto done;
2796 if (!NT_STATUS_IS_OK(result)) {
2797 status = result;
2798 goto done;
2801 init_lsa_String(&lsa_acct_name, user);
2803 status = dcerpc_samr_LookupNames(b, mem_ctx,
2804 &domain_pol,
2806 &lsa_acct_name,
2807 &rids,
2808 &types,
2809 &result);
2810 if (!NT_STATUS_IS_OK(status)) {
2811 goto done;
2813 if (!NT_STATUS_IS_OK(result)) {
2814 status = result;
2815 goto done;
2817 if (rids.count != 1) {
2818 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2819 goto done;
2821 if (types.count != 1) {
2822 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2823 goto done;
2826 status = dcerpc_samr_OpenUser(b, mem_ctx,
2827 &domain_pol,
2828 access_mask,
2829 rids.ids[0],
2830 &user_pol,
2831 &result);
2832 if (!NT_STATUS_IS_OK(status)) {
2833 goto done;
2835 if (!NT_STATUS_IS_OK(result)) {
2836 status = result;
2837 goto done;
2840 /* Change user password */
2841 status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2842 &user_pol,
2843 newpass,
2844 oldpass);
2845 if (!NT_STATUS_IS_OK(status)) {
2846 goto done;
2849 done:
2850 if (is_valid_policy_hnd(&user_pol)) {
2851 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2853 if (is_valid_policy_hnd(&domain_pol)) {
2854 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2856 if (is_valid_policy_hnd(&connect_pol)) {
2857 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2860 return status;
2864 /* Change user password */
2866 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2867 TALLOC_CTX *mem_ctx,
2868 int argc, const char **argv)
2870 struct policy_handle connect_pol, domain_pol;
2871 NTSTATUS status, result;
2872 const char *user, *oldpass, *newpass;
2873 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2874 struct dcerpc_binding_handle *b = cli->binding_handle;
2876 if (argc < 3) {
2877 printf("Usage: %s username oldpass newpass\n", argv[0]);
2878 return NT_STATUS_INVALID_PARAMETER;
2881 user = argv[1];
2882 oldpass = argv[2];
2883 newpass = argv[3];
2885 /* Get sam policy handle */
2887 status = rpccli_try_samr_connects(cli, mem_ctx,
2888 MAXIMUM_ALLOWED_ACCESS,
2889 &connect_pol);
2890 if (!NT_STATUS_IS_OK(status)) {
2891 goto done;
2894 /* Get domain policy handle */
2896 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2897 &connect_pol,
2898 access_mask,
2899 &domain_sid,
2900 &domain_pol,
2901 &result);
2902 if (!NT_STATUS_IS_OK(status)) {
2903 goto done;
2905 if (!NT_STATUS_IS_OK(result)) {
2906 status = result;
2907 goto done;
2910 /* Change user password */
2911 status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2913 if (!NT_STATUS_IS_OK(status)) {
2914 goto done;
2917 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2918 if (!NT_STATUS_IS_OK(status)) goto done;
2920 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2921 if (!NT_STATUS_IS_OK(status)) goto done;
2923 done:
2924 return status;
2928 /* Change user password */
2930 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2931 TALLOC_CTX *mem_ctx,
2932 int argc, const char **argv)
2934 struct policy_handle connect_pol, domain_pol;
2935 NTSTATUS status, result;
2936 const char *user, *oldpass, *newpass;
2937 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2938 struct samr_DomInfo1 *info = NULL;
2939 struct userPwdChangeFailureInformation *reject = NULL;
2940 struct dcerpc_binding_handle *b = cli->binding_handle;
2942 if (argc < 3) {
2943 printf("Usage: %s username oldpass newpass\n", argv[0]);
2944 return NT_STATUS_INVALID_PARAMETER;
2947 user = argv[1];
2948 oldpass = argv[2];
2949 newpass = argv[3];
2951 /* Get sam policy handle */
2953 status = rpccli_try_samr_connects(cli, mem_ctx,
2954 MAXIMUM_ALLOWED_ACCESS,
2955 &connect_pol);
2956 if (!NT_STATUS_IS_OK(status)) {
2957 goto done;
2960 /* Get domain policy handle */
2962 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2963 &connect_pol,
2964 access_mask,
2965 &domain_sid,
2966 &domain_pol,
2967 &result);
2968 if (!NT_STATUS_IS_OK(status)) {
2969 goto done;
2971 if (!NT_STATUS_IS_OK(result)) {
2972 status = result;
2973 goto done;
2976 /* Change user password */
2977 status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2978 user,
2979 newpass,
2980 oldpass,
2981 &info,
2982 &reject);
2983 if (!NT_STATUS_IS_OK(status)) {
2984 goto done;
2987 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2989 display_sam_dom_info_1(info);
2991 switch (reject->extendedFailureReason) {
2992 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
2993 d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
2994 break;
2995 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
2996 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
2997 break;
2998 case SAM_PWD_CHANGE_NOT_COMPLEX:
2999 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
3000 break;
3001 default:
3002 d_printf("unknown reject reason: %d\n",
3003 reject->extendedFailureReason);
3004 break;
3008 if (!NT_STATUS_IS_OK(result)) {
3009 status = result;
3010 goto done;
3013 status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3014 if (!NT_STATUS_IS_OK(status)) goto done;
3016 status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
3017 if (!NT_STATUS_IS_OK(status)) goto done;
3019 done:
3020 return status;
3023 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
3024 TALLOC_CTX *mem_ctx,
3025 int argc, const char **argv,
3026 int opcode)
3028 struct policy_handle connect_pol, domain_pol, user_pol;
3029 NTSTATUS status, result;
3030 const char *user, *param;
3031 uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
3032 uint32_t level;
3033 uint32_t user_rid;
3034 union samr_UserInfo info;
3035 struct samr_CryptPassword pwd_buf;
3036 struct samr_CryptPasswordEx pwd_buf_ex;
3037 uint8_t nt_hash[16];
3038 uint8_t lm_hash[16];
3039 DATA_BLOB session_key;
3040 uint8_t password_expired = 0;
3041 struct dcerpc_binding_handle *b = cli->binding_handle;
3043 if (argc < 4) {
3044 printf("Usage: %s username level password [password_expired]\n",
3045 argv[0]);
3046 return NT_STATUS_INVALID_PARAMETER;
3049 user = argv[1];
3050 level = atoi(argv[2]);
3051 param = argv[3];
3053 if (argc >= 5) {
3054 password_expired = atoi(argv[4]);
3057 status = cli_get_session_key(mem_ctx, cli, &session_key);
3058 if (!NT_STATUS_IS_OK(status)) {
3059 return status;
3062 init_samr_CryptPassword(param, &session_key, &pwd_buf);
3063 init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
3064 nt_lm_owf_gen(param, nt_hash, lm_hash);
3066 switch (level) {
3067 case 18:
3069 DATA_BLOB in,out;
3070 in = data_blob_const(nt_hash, 16);
3071 out = data_blob_talloc_zero(mem_ctx, 16);
3072 sess_crypt_blob(&out, &in, &session_key, true);
3073 memcpy(nt_hash, out.data, out.length);
3076 DATA_BLOB in,out;
3077 in = data_blob_const(lm_hash, 16);
3078 out = data_blob_talloc_zero(mem_ctx, 16);
3079 sess_crypt_blob(&out, &in, &session_key, true);
3080 memcpy(lm_hash, out.data, out.length);
3083 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
3084 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
3085 info.info18.nt_pwd_active = true;
3086 info.info18.lm_pwd_active = true;
3087 info.info18.password_expired = password_expired;
3089 break;
3090 case 21:
3091 ZERO_STRUCT(info.info21);
3093 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3094 SAMR_FIELD_LM_PASSWORD_PRESENT;
3095 if (argc >= 5) {
3096 info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3097 info.info21.password_expired = password_expired;
3100 info.info21.lm_password_set = true;
3101 info.info21.lm_owf_password.length = 16;
3102 info.info21.lm_owf_password.size = 16;
3104 info.info21.nt_password_set = true;
3105 info.info21.nt_owf_password.length = 16;
3106 info.info21.nt_owf_password.size = 16;
3109 DATA_BLOB in,out;
3110 in = data_blob_const(nt_hash, 16);
3111 out = data_blob_talloc_zero(mem_ctx, 16);
3112 sess_crypt_blob(&out, &in, &session_key, true);
3113 info.info21.nt_owf_password.array =
3114 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3117 DATA_BLOB in,out;
3118 in = data_blob_const(lm_hash, 16);
3119 out = data_blob_talloc_zero(mem_ctx, 16);
3120 sess_crypt_blob(&out, &in, &session_key, true);
3121 info.info21.lm_owf_password.array =
3122 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3125 break;
3126 case 23:
3127 ZERO_STRUCT(info.info23);
3129 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3130 SAMR_FIELD_LM_PASSWORD_PRESENT;
3131 if (argc >= 5) {
3132 info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3133 info.info23.info.password_expired = password_expired;
3136 info.info23.password = pwd_buf;
3138 break;
3139 case 24:
3140 info.info24.password = pwd_buf;
3141 info.info24.password_expired = password_expired;
3143 break;
3144 case 25:
3145 ZERO_STRUCT(info.info25);
3147 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3148 SAMR_FIELD_LM_PASSWORD_PRESENT;
3149 if (argc >= 5) {
3150 info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3151 info.info25.info.password_expired = password_expired;
3154 info.info25.password = pwd_buf_ex;
3156 break;
3157 case 26:
3158 info.info26.password = pwd_buf_ex;
3159 info.info26.password_expired = password_expired;
3161 break;
3162 default:
3163 return NT_STATUS_INVALID_INFO_CLASS;
3166 /* Get sam policy handle */
3168 status = rpccli_try_samr_connects(cli, mem_ctx,
3169 MAXIMUM_ALLOWED_ACCESS,
3170 &connect_pol);
3171 if (!NT_STATUS_IS_OK(status)) {
3172 goto done;
3175 /* Get domain policy handle */
3177 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3178 &connect_pol,
3179 access_mask,
3180 &domain_sid,
3181 &domain_pol,
3182 &result);
3184 if (!NT_STATUS_IS_OK(status))
3185 goto done;
3186 if (!NT_STATUS_IS_OK(result)) {
3187 status = result;
3188 goto done;
3191 user_rid = strtol(user, NULL, 0);
3192 if (user_rid) {
3193 status = dcerpc_samr_OpenUser(b, mem_ctx,
3194 &domain_pol,
3195 access_mask,
3196 user_rid,
3197 &user_pol,
3198 &result);
3199 if (!NT_STATUS_IS_OK(status)) {
3200 goto done;
3203 status = result;
3206 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3207 (user_rid == 0)) {
3209 /* Probably this was a user name, try lookupnames */
3210 struct samr_Ids rids, types;
3211 struct lsa_String lsa_acct_name;
3213 init_lsa_String(&lsa_acct_name, user);
3215 status = dcerpc_samr_LookupNames(b, mem_ctx,
3216 &domain_pol,
3218 &lsa_acct_name,
3219 &rids,
3220 &types,
3221 &result);
3222 if (!NT_STATUS_IS_OK(status)) {
3223 return status;
3225 if (!NT_STATUS_IS_OK(result)) {
3226 return result;
3228 if (rids.count != 1) {
3229 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3231 if (types.count != 1) {
3232 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3235 status = dcerpc_samr_OpenUser(b, mem_ctx,
3236 &domain_pol,
3237 access_mask,
3238 rids.ids[0],
3239 &user_pol,
3240 &result);
3241 if (!NT_STATUS_IS_OK(status)) {
3242 return status;
3244 if (!NT_STATUS_IS_OK(result)) {
3245 return result;
3249 switch (opcode) {
3250 case NDR_SAMR_SETUSERINFO:
3251 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
3252 &user_pol,
3253 level,
3254 &info,
3255 &result);
3256 break;
3257 case NDR_SAMR_SETUSERINFO2:
3258 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
3259 &user_pol,
3260 level,
3261 &info,
3262 &result);
3263 break;
3264 default:
3265 return NT_STATUS_INVALID_PARAMETER;
3267 if (!NT_STATUS_IS_OK(status)) {
3268 DEBUG(0,("status: %s\n", nt_errstr(status)));
3269 goto done;
3271 if (!NT_STATUS_IS_OK(result)) {
3272 status = result;
3273 DEBUG(0,("result: %s\n", nt_errstr(status)));
3274 goto done;
3276 done:
3277 return status;
3280 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
3281 TALLOC_CTX *mem_ctx,
3282 int argc, const char **argv)
3284 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3285 NDR_SAMR_SETUSERINFO);
3288 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
3289 TALLOC_CTX *mem_ctx,
3290 int argc, const char **argv)
3292 return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3293 NDR_SAMR_SETUSERINFO2);
3296 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
3297 TALLOC_CTX *mem_ctx,
3298 int argc, const char **argv)
3300 NTSTATUS status, result;
3301 struct policy_handle connect_handle;
3302 struct policy_handle domain_handle = { 0, };
3303 uint16_t level = 1;
3304 struct lsa_String name;
3305 uint32_t idx = 0;
3306 struct dcerpc_binding_handle *b = cli->binding_handle;
3308 if (argc < 2 || argc > 3) {
3309 printf("Usage: %s name level\n", argv[0]);
3310 return NT_STATUS_INVALID_PARAMETER;
3313 init_lsa_String(&name, argv[1]);
3315 if (argc == 3) {
3316 level = atoi(argv[2]);
3319 status = rpccli_try_samr_connects(cli, mem_ctx,
3320 SEC_FLAG_MAXIMUM_ALLOWED,
3321 &connect_handle);
3322 if (!NT_STATUS_IS_OK(status)) {
3323 goto done;
3326 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3327 &connect_handle,
3328 SEC_FLAG_MAXIMUM_ALLOWED,
3329 &domain_sid,
3330 &domain_handle,
3331 &result);
3332 if (!NT_STATUS_IS_OK(status)) {
3333 goto done;
3335 if (!NT_STATUS_IS_OK(result)) {
3336 status = result;
3337 goto done;
3340 status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3341 &domain_handle,
3342 level,
3343 &name,
3344 &idx,
3345 &result);
3346 if (!NT_STATUS_IS_OK(status)) {
3347 goto done;
3350 status = result;
3352 if (NT_STATUS_IS_OK(status) ||
3353 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
3354 printf("idx: %d (0x%08x)\n", idx, idx);
3356 done:
3358 if (is_valid_policy_hnd(&domain_handle)) {
3359 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
3361 if (is_valid_policy_hnd(&connect_handle)) {
3362 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
3365 return status;
3368 /* List of commands exported by this module */
3370 struct cmd_set samr_commands[] = {
3372 { "SAMR" },
3374 { "queryuser", RPC_RTYPE_NTSTATUS, cmd_samr_query_user, NULL, &ndr_table_samr, NULL, "Query user info", "" },
3375 { "querygroup", RPC_RTYPE_NTSTATUS, cmd_samr_query_group, NULL, &ndr_table_samr, NULL, "Query group info", "" },
3376 { "queryusergroups", RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups, NULL, &ndr_table_samr, NULL, "Query user groups", "" },
3377 { "queryuseraliases", RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases, NULL, &ndr_table_samr, NULL, "Query user aliases", "" },
3378 { "querygroupmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem, NULL, &ndr_table_samr, NULL, "Query group membership", "" },
3379 { "queryaliasmem", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem, NULL, &ndr_table_samr, NULL, "Query alias membership", "" },
3380 { "queryaliasinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo, NULL, &ndr_table_samr, NULL, "Query alias info", "" },
3381 { "deletealias", RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias, NULL, &ndr_table_samr, NULL, "Delete an alias", "" },
3382 { "querydispinfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3383 { "querydispinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3384 { "querydispinfo3", RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3, NULL, &ndr_table_samr, NULL, "Query display info", "" },
3385 { "querydominfo", RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo, NULL, &ndr_table_samr, NULL, "Query domain info", "" },
3386 { "enumdomusers", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users, NULL, &ndr_table_samr, NULL, "Enumerate domain users", "" },
3387 { "enumdomgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups, NULL, &ndr_table_samr, NULL, "Enumerate domain groups", "" },
3388 { "enumalsgroups", RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups, NULL, &ndr_table_samr, NULL, "Enumerate alias groups", "" },
3389 { "enumdomains", RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains, NULL, &ndr_table_samr, NULL, "Enumerate domains", "" },
3391 { "createdomuser", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user, NULL, &ndr_table_samr, NULL, "Create domain user", "" },
3392 { "createdomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group, NULL, &ndr_table_samr, NULL, "Create domain group", "" },
3393 { "createdomalias", RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias, NULL, &ndr_table_samr, NULL, "Create domain alias", "" },
3394 { "samlookupnames", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names, NULL, &ndr_table_samr, NULL, "Look up names", "" },
3395 { "samlookuprids", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids, NULL, &ndr_table_samr, NULL, "Look up names", "" },
3396 { "deletedomgroup", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group, NULL, &ndr_table_samr, NULL, "Delete domain group", "" },
3397 { "deletedomuser", RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user, NULL, &ndr_table_samr, NULL, "Delete domain user", "" },
3398 { "samquerysecobj", RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj, NULL, &ndr_table_samr, NULL, "Query SAMR security object", "" },
3399 { "getdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo, NULL, &ndr_table_samr, NULL, "Retrieve domain password info", "" },
3400 { "getusrdompwinfo", RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo, NULL, &ndr_table_samr, NULL, "Retrieve user domain password info", "" },
3402 { "lookupdomain", RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain, NULL, &ndr_table_samr, NULL, "Lookup Domain Name", "" },
3403 { "chgpasswd", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3404 { "chgpasswd2", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3405 { "chgpasswd3", RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3, NULL, &ndr_table_samr, NULL, "Change user password", "" },
3406 { "getdispinfoidx", RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx, NULL, &ndr_table_samr, NULL, "Get Display Information Index", "" },
3407 { "setuserinfo", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo, NULL, &ndr_table_samr, NULL, "Set user info", "" },
3408 { "setuserinfo2", RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2, NULL, &ndr_table_samr, NULL, "Set user info2", "" },
3409 { NULL }