s3:rpcclient: Use dcerpc_lsa_open_policy_fallback() in cmd_lsarpc.c
[samba.git] / source3 / rpcclient / cmd_lsarpc.c
blob5374c9a90612a51594f4ff4eea3f29e285ed8c01
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Tim Potter 2000
6 Copyright (C) Rafal Szczesniak 2002
7 Copyright (C) Guenther Deschner 2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "rpcclient.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_lsa.h"
27 #include "../librpc/gen_ndr/ndr_lsa_c.h"
28 #include "rpc_client/cli_lsarpc.h"
29 #include "rpc_client/init_lsa.h"
30 #include "../libcli/security/security.h"
32 /* useful function to allow entering a name instead of a SID and
33 * looking it up automatically */
34 static NTSTATUS name_to_sid(struct rpc_pipe_client *cli,
35 TALLOC_CTX *mem_ctx,
36 struct dom_sid *sid,
37 const char *name)
39 struct policy_handle pol;
40 enum lsa_SidType *sid_types;
41 NTSTATUS status, result;
42 struct dom_sid *sids;
43 struct dcerpc_binding_handle *b = cli->binding_handle;
45 /* maybe its a raw SID */
46 if (strncmp(name, "S-", 2) == 0 &&
47 string_to_sid(sid, name)) {
48 return NT_STATUS_OK;
51 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
52 SEC_FLAG_MAXIMUM_ALLOWED,
53 &pol);
54 if (!NT_STATUS_IS_OK(status))
55 goto done;
57 status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, NULL, 1, &sids, &sid_types);
58 if (!NT_STATUS_IS_OK(status))
59 goto done;
61 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
63 *sid = sids[0];
65 done:
66 return status;
69 static void display_query_info_1(struct lsa_AuditLogInfo *r)
71 d_printf("percent_full:\t%d\n", r->percent_full);
72 d_printf("maximum_log_size:\t%d\n", r->maximum_log_size);
73 d_printf("retention_time:\t%lld\n", (long long)r->retention_time);
74 d_printf("shutdown_in_progress:\t%d\n", r->shutdown_in_progress);
75 d_printf("time_to_shutdown:\t%lld\n", (long long)r->time_to_shutdown);
76 d_printf("next_audit_record:\t%d\n", r->next_audit_record);
79 static void display_query_info_2(struct lsa_AuditEventsInfo *r)
81 int i;
82 d_printf("Auditing enabled:\t%d\n", r->auditing_mode);
83 d_printf("Auditing categories:\t%d\n", r->count);
84 d_printf("Auditsettings:\n");
85 for (i=0; i<r->count; i++) {
86 const char *val = audit_policy_str(talloc_tos(), r->settings[i]);
87 const char *policy = audit_description_str(i);
88 d_printf("%s:\t%s\n", policy, val);
92 static void display_query_info_3(struct lsa_DomainInfo *r)
94 struct dom_sid_buf buf;
95 d_printf("Domain Name: %s\n", r->name.string);
96 d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
99 static void display_query_info_5(struct lsa_DomainInfo *r)
101 struct dom_sid_buf buf;
102 d_printf("Domain Name: %s\n", r->name.string);
103 d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
106 static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
108 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
111 static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
113 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
114 d_printf("Log is full: %d\n", r->log_is_full);
117 static void display_query_info_12(struct lsa_DnsDomainInfo *r)
119 struct dom_sid_buf buf;
120 d_printf("Domain NetBios Name: %s\n", r->name.string);
121 d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
122 d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
123 d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
124 d_printf("Domain GUID: %s\n", GUID_string(talloc_tos(),
125 &r->domain_guid));
128 static void display_lsa_query_info(union lsa_PolicyInformation *info,
129 enum lsa_PolicyInfo level)
131 switch (level) {
132 case 1:
133 display_query_info_1(&info->audit_log);
134 break;
135 case 2:
136 display_query_info_2(&info->audit_events);
137 break;
138 case 3:
139 display_query_info_3(&info->domain);
140 break;
141 case 5:
142 display_query_info_5(&info->account_domain);
143 break;
144 case 10:
145 display_query_info_10(&info->auditfullset);
146 break;
147 case 11:
148 display_query_info_11(&info->auditfullquery);
149 break;
150 case 12:
151 display_query_info_12(&info->dns);
152 break;
153 default:
154 printf("can't display info level: %d\n", level);
155 break;
159 static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli,
160 TALLOC_CTX *mem_ctx,
161 int argc,
162 const char **argv)
164 struct policy_handle pol;
165 NTSTATUS status, result;
166 union lsa_PolicyInformation *info = NULL;
167 struct dcerpc_binding_handle *b = cli->binding_handle;
169 uint32_t info_class = 3;
171 if (argc > 2) {
172 printf("Usage: %s [info_class]\n", argv[0]);
173 return NT_STATUS_OK;
176 if (argc == 2)
177 info_class = atoi(argv[1]);
179 switch (info_class) {
180 case 12: {
181 union lsa_revision_info out_revision_info = {
182 .info1 = {
183 .revision = 0,
186 uint32_t out_version = 0;
188 status = dcerpc_lsa_open_policy_fallback(
190 mem_ctx,
191 cli->srv_name_slash,
192 true,
193 SEC_FLAG_MAXIMUM_ALLOWED,
194 &out_version,
195 &out_revision_info,
196 &pol,
197 &result);
198 if (any_nt_status_not_ok(status, result, &status)) {
199 goto done;
202 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
203 &pol,
204 info_class,
205 &info,
206 &result);
207 break;
209 default:
210 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
211 SEC_FLAG_MAXIMUM_ALLOWED,
212 &pol);
214 if (!NT_STATUS_IS_OK(status))
215 goto done;
217 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
218 &pol,
219 info_class,
220 &info,
221 &result);
224 if (!NT_STATUS_IS_OK(status)) {
225 goto done;
227 status = result;
228 if (NT_STATUS_IS_OK(result)) {
229 display_lsa_query_info(info, info_class);
232 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
234 done:
235 return status;
238 /* Resolve a list of names to a list of sids */
240 static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli,
241 TALLOC_CTX *mem_ctx,
242 int argc,
243 const char **argv)
245 struct policy_handle pol;
246 NTSTATUS status, result;
247 struct dom_sid *sids;
248 enum lsa_SidType *types;
249 int i;
250 struct dcerpc_binding_handle *b = cli->binding_handle;
252 if (argc == 1) {
253 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
254 return NT_STATUS_OK;
257 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
258 LSA_POLICY_LOOKUP_NAMES,
259 &pol);
261 if (!NT_STATUS_IS_OK(status))
262 goto done;
264 status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
265 (const char**)(argv + 1), NULL, 1, &sids, &types);
267 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
268 NT_STATUS_V(STATUS_SOME_UNMAPPED))
269 goto done;
271 status = NT_STATUS_OK;
273 /* Print results */
275 for (i = 0; i < (argc - 1); i++) {
276 struct dom_sid_buf sid_str;
277 printf("%s %s (%s: %d)\n",
278 argv[i + 1],
279 dom_sid_str_buf(&sids[i], &sid_str),
280 sid_type_lookup(types[i]),
281 types[i]);
284 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
286 done:
287 return status;
290 /* Resolve a list of names to a list of sids */
292 static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli,
293 TALLOC_CTX *mem_ctx,
294 int argc,
295 const char **argv)
297 struct policy_handle pol;
298 NTSTATUS status, result;
299 struct dom_sid *sids = NULL;
300 enum lsa_SidType *types = NULL;
301 int i, level;
302 struct dcerpc_binding_handle *b = cli->binding_handle;
304 if (argc < 3) {
305 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
306 return NT_STATUS_OK;
309 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
310 LSA_POLICY_LOOKUP_NAMES,
311 &pol);
312 if (!NT_STATUS_IS_OK(status)) {
313 goto done;
316 level = atoi(argv[1]);
318 status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2,
319 (const char**)(argv + 2), NULL, level, &sids, &types);
321 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
322 NT_STATUS_V(STATUS_SOME_UNMAPPED))
324 goto done;
327 status = NT_STATUS_OK;
329 /* Print results */
331 for (i = 0; i < (argc - 2); i++) {
332 struct dom_sid_buf sid_str;
333 printf("%s %s (%s: %d)\n",
334 argv[i + 2],
335 dom_sid_str_buf(&sids[i], &sid_str),
336 sid_type_lookup(types[i]),
337 types[i]);
340 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
342 done:
343 return status;
346 static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
347 TALLOC_CTX *mem_ctx, int argc,
348 const char **argv)
350 NTSTATUS status, result;
352 uint32_t num_names;
353 struct lsa_String *names;
354 struct lsa_RefDomainList *domains = NULL;
355 struct lsa_TransSidArray3 sids;
356 uint32_t count = 0;
357 int i;
358 struct dcerpc_binding_handle *b = cli->binding_handle;
360 if (argc == 1) {
361 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
362 return NT_STATUS_OK;
365 ZERO_STRUCT(sids);
367 num_names = argc-1;
368 names = talloc_array(mem_ctx, struct lsa_String, num_names);
369 NT_STATUS_HAVE_NO_MEMORY(names);
371 for (i=0; i < num_names; i++) {
372 init_lsa_String(&names[i], argv[i+1]);
375 status = dcerpc_lsa_LookupNames4(b, mem_ctx,
376 num_names,
377 names,
378 &domains,
379 &sids,
381 &count,
384 &result);
385 if (!NT_STATUS_IS_OK(status)) {
386 return status;
388 if (!NT_STATUS_IS_OK(result)) {
389 return result;
392 if (sids.count != num_names) {
393 return NT_STATUS_INVALID_NETWORK_RESPONSE;
396 for (i = 0; i < sids.count; i++) {
397 struct dom_sid_buf sid_str;
398 printf("%s %s (%s: %d)\n",
399 argv[i+1],
400 dom_sid_str_buf(sids.sids[i].sid, &sid_str),
401 sid_type_lookup(sids.sids[i].sid_type),
402 sids.sids[i].sid_type);
405 return status;
408 /* Resolve a list of SIDs to a list of names */
410 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
411 int argc, const char **argv)
413 struct policy_handle pol;
414 NTSTATUS status, result;
415 struct dom_sid *sids;
416 char **domains;
417 char **names;
418 enum lsa_SidType *types;
419 int i;
420 struct dcerpc_binding_handle *b = cli->binding_handle;
422 if (argc == 1) {
423 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
424 return NT_STATUS_OK;
427 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
428 LSA_POLICY_LOOKUP_NAMES,
429 &pol);
431 if (!NT_STATUS_IS_OK(status))
432 goto done;
434 /* Convert arguments to sids */
436 sids = talloc_array(mem_ctx, struct dom_sid, argc - 1);
438 if (!sids) {
439 printf("could not allocate memory for %d sids\n", argc - 1);
440 goto done;
443 for (i = 0; i < argc - 1; i++)
444 if (!string_to_sid(&sids[i], argv[i + 1])) {
445 status = NT_STATUS_INVALID_SID;
446 goto done;
449 /* Lookup the SIDs */
451 status = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
452 &domains, &names, &types);
454 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
455 NT_STATUS_V(STATUS_SOME_UNMAPPED))
456 goto done;
458 status = NT_STATUS_OK;
460 /* Print results */
462 for (i = 0; i < (argc - 1); i++) {
463 struct dom_sid_buf sid_str;
465 dom_sid_str_buf(&sids[i], &sid_str);
466 if (types[i] == SID_NAME_DOMAIN) {
467 printf("%s %s (%d)\n", sid_str.buf,
468 domains[i] ? domains[i] : "*unknown*",
469 types[i]);
470 } else {
471 printf("%s %s\\%s (%d)\n", sid_str.buf,
472 domains[i] ? domains[i] : "*unknown*",
473 names[i] ? names[i] : "*unknown*",
474 types[i]);
478 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
480 done:
481 return status;
484 static NTSTATUS cmd_lsa_lookup_sids_level(struct rpc_pipe_client *cli,
485 TALLOC_CTX *mem_ctx, int argc,
486 const char **argv)
488 struct policy_handle pol;
489 NTSTATUS status, result;
490 struct dom_sid *sids = NULL;
491 char **domains = NULL;
492 char **names = NULL;
493 enum lsa_SidType *types = NULL;
494 int i, level;
495 struct dcerpc_binding_handle *b = cli->binding_handle;
497 if (argc < 3) {
498 printf("Usage: %s [level] [sid1 [sid2 [...]]]\n", argv[0]);
499 return NT_STATUS_OK;
502 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
503 LSA_POLICY_LOOKUP_NAMES,
504 &pol);
505 if (!NT_STATUS_IS_OK(status)) {
506 goto done;
509 level = atoi(argv[1]);
511 /* Convert arguments to sids */
513 sids = talloc_array(mem_ctx, struct dom_sid, argc - 2);
514 if (sids == NULL) {
515 printf("could not allocate memory for %d sids\n", argc - 2);
516 goto done;
519 for (i = 0; i < argc - 2; i++) {
520 if (!string_to_sid(&sids[i], argv[i + 2])) {
521 status = NT_STATUS_INVALID_SID;
522 goto done;
526 /* Lookup the SIDs */
528 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
529 mem_ctx,
530 &pol,
531 argc - 2,
532 sids,
533 level,
534 &domains,
535 &names,
536 &types,
537 false,
538 &result);
539 if (!NT_STATUS_IS_OK(status)) {
540 goto done;
542 status = result;
544 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
545 NT_STATUS_V(STATUS_SOME_UNMAPPED))
547 goto done;
550 status = NT_STATUS_OK;
552 /* Print results */
554 for (i = 0; i < (argc - 2); i++) {
555 struct dom_sid_buf sid_str;
557 dom_sid_str_buf(&sids[i], &sid_str);
558 if (types[i] == SID_NAME_DOMAIN) {
559 printf("%s %s (%d)\n", sid_str.buf,
560 domains[i] ? domains[i] : "*unknown*",
561 types[i]);
562 } else {
563 printf("%s %s\\%s (%d)\n", sid_str.buf,
564 domains[i] ? domains[i] : "*unknown*",
565 names[i] ? names[i] : "*unknown*",
566 types[i]);
570 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
572 done:
573 return status;
576 /* Resolve a list of SIDs to a list of names */
578 static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
579 TALLOC_CTX *mem_ctx,
580 int argc, const char **argv)
582 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
583 int i;
584 struct lsa_SidArray sids;
585 struct lsa_RefDomainList *domains = NULL;
586 struct lsa_TransNameArray2 names;
587 uint32_t count = 0;
588 struct dcerpc_binding_handle *b = cli->binding_handle;
590 if (argc == 1) {
591 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
592 return NT_STATUS_OK;
595 ZERO_STRUCT(names);
597 /* Convert arguments to sids */
599 sids.num_sids = argc-1;
600 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
601 if (!sids.sids) {
602 printf("could not allocate memory for %d sids\n", sids.num_sids);
603 goto done;
606 for (i = 0; i < sids.num_sids; i++) {
607 sids.sids[i].sid = talloc(sids.sids, struct dom_sid);
608 if (sids.sids[i].sid == NULL) {
609 status = NT_STATUS_NO_MEMORY;
610 goto done;
612 if (!string_to_sid(sids.sids[i].sid, argv[i+1])) {
613 status = NT_STATUS_INVALID_SID;
614 goto done;
618 /* Lookup the SIDs */
619 status = dcerpc_lsa_LookupSids3(b, mem_ctx,
620 &sids,
621 &domains,
622 &names,
624 &count,
627 &result);
628 if (!NT_STATUS_IS_OK(status)) {
629 goto done;
631 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
632 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
633 status = result;
634 goto done;
637 status = NT_STATUS_OK;
639 /* Print results */
641 for (i = 0; i < names.count; i++) {
642 struct dom_sid_buf sid_str;
644 if (i >= sids.num_sids) {
645 break;
647 printf("%s %s (%d)\n",
648 dom_sid_str_buf(sids.sids[i].sid, &sid_str),
649 names.names[i].name.string,
650 names.names[i].sid_type);
653 done:
654 return status;
658 /* Enumerate list of trusted domains */
660 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
661 TALLOC_CTX *mem_ctx,
662 int argc,
663 const char **argv)
665 struct policy_handle pol;
666 NTSTATUS status, result;
667 struct lsa_DomainList domain_list;
668 struct dcerpc_binding_handle *b = cli->binding_handle;
670 /* defaults, but may be changed using params */
671 uint32_t enum_ctx = 0;
672 int i;
673 uint32_t max_size = (uint32_t)-1;
675 if (argc > 2) {
676 printf("Usage: %s [enum context (0)]\n", argv[0]);
677 return NT_STATUS_OK;
680 if (argc == 2 && argv[1]) {
681 enum_ctx = atoi(argv[2]);
684 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
685 LSA_POLICY_VIEW_LOCAL_INFORMATION,
686 &pol);
688 if (!NT_STATUS_IS_OK(status))
689 goto done;
691 status = STATUS_MORE_ENTRIES;
693 while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
695 /* Lookup list of trusted domains */
697 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
698 &pol,
699 &enum_ctx,
700 &domain_list,
701 max_size,
702 &result);
703 if (!NT_STATUS_IS_OK(status)) {
704 goto done;
706 if (!NT_STATUS_IS_OK(result) &&
707 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
708 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
709 status = result;
710 goto done;
713 /* Print results: list of names and sids returned in this
714 * response. */
715 for (i = 0; i < domain_list.count; i++) {
716 struct dom_sid_buf sid_str;
718 printf("%s %s\n",
719 domain_list.domains[i].name.string ?
720 domain_list.domains[i].name.string : "*unknown*",
721 dom_sid_str_buf(domain_list.domains[i].sid,
722 &sid_str));
726 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
727 done:
728 return status;
731 /* Enumerates privileges */
733 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli,
734 TALLOC_CTX *mem_ctx,
735 int argc,
736 const char **argv)
738 struct policy_handle pol;
739 NTSTATUS status, result;
740 struct lsa_PrivArray priv_array;
741 struct dcerpc_binding_handle *b = cli->binding_handle;
743 uint32_t enum_context=0;
744 uint32_t pref_max_length=0x1000;
745 int i;
747 if (argc > 3) {
748 printf("Usage: %s [enum context] [max length]\n", argv[0]);
749 return NT_STATUS_OK;
752 if (argc>=2)
753 enum_context=atoi(argv[1]);
755 if (argc==3)
756 pref_max_length=atoi(argv[2]);
758 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
759 SEC_FLAG_MAXIMUM_ALLOWED,
760 &pol);
762 if (!NT_STATUS_IS_OK(status))
763 goto done;
765 status = dcerpc_lsa_EnumPrivs(b, mem_ctx,
766 &pol,
767 &enum_context,
768 &priv_array,
769 pref_max_length,
770 &result);
771 if (!NT_STATUS_IS_OK(status))
772 goto done;
773 if (!NT_STATUS_IS_OK(result)) {
774 status = result;
775 goto done;
778 /* Print results */
779 printf("found %d privileges\n\n", priv_array.count);
781 for (i = 0; i < priv_array.count; i++) {
782 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
783 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
784 priv_array.privs[i].luid.high,
785 priv_array.privs[i].luid.low,
786 priv_array.privs[i].luid.high,
787 priv_array.privs[i].luid.low);
790 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
791 done:
792 return status;
795 /* Get privilege name */
797 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli,
798 TALLOC_CTX *mem_ctx,
799 int argc,
800 const char **argv)
802 struct policy_handle pol;
803 NTSTATUS status, result;
804 struct dcerpc_binding_handle *b = cli->binding_handle;
806 uint16_t lang_id=0;
807 uint16_t lang_id_sys=0;
808 uint16_t lang_id_desc;
809 struct lsa_String lsa_name;
810 struct lsa_StringLarge *description = NULL;
812 if (argc != 2) {
813 printf("Usage: %s privilege name\n", argv[0]);
814 return NT_STATUS_OK;
817 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
818 SEC_FLAG_MAXIMUM_ALLOWED,
819 &pol);
821 if (!NT_STATUS_IS_OK(status))
822 goto done;
824 init_lsa_String(&lsa_name, argv[1]);
826 status = dcerpc_lsa_LookupPrivDisplayName(b, mem_ctx,
827 &pol,
828 &lsa_name,
829 lang_id,
830 lang_id_sys,
831 &description,
832 &lang_id_desc,
833 &result);
834 if (!NT_STATUS_IS_OK(status))
835 goto done;
836 if (!NT_STATUS_IS_OK(result)) {
837 status = result;
838 goto done;
841 /* Print results */
842 printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
844 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
845 done:
846 return status;
849 /* Enumerate the LSA SIDS */
851 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli,
852 TALLOC_CTX *mem_ctx,
853 int argc,
854 const char **argv)
856 struct policy_handle pol;
857 NTSTATUS status, result;
858 struct dcerpc_binding_handle *b = cli->binding_handle;
860 uint32_t enum_context=0;
861 uint32_t pref_max_length=0x1000;
862 struct lsa_SidArray sid_array;
863 int i;
865 if (argc > 3) {
866 printf("Usage: %s [enum context] [max length]\n", argv[0]);
867 return NT_STATUS_OK;
870 if (argc>=2)
871 enum_context=atoi(argv[1]);
873 if (argc==3)
874 pref_max_length=atoi(argv[2]);
876 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
877 SEC_FLAG_MAXIMUM_ALLOWED,
878 &pol);
880 if (!NT_STATUS_IS_OK(status))
881 goto done;
883 status = dcerpc_lsa_EnumAccounts(b, mem_ctx,
884 &pol,
885 &enum_context,
886 &sid_array,
887 pref_max_length,
888 &result);
889 if (!NT_STATUS_IS_OK(status))
890 goto done;
891 if (!NT_STATUS_IS_OK(result)) {
892 status = result;
893 goto done;
896 /* Print results */
897 printf("found %d SIDs\n\n", sid_array.num_sids);
899 for (i = 0; i < sid_array.num_sids; i++) {
900 struct dom_sid_buf sid_str;
902 printf("%s\n",
903 dom_sid_str_buf(sid_array.sids[i].sid, &sid_str));
906 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
907 done:
908 return status;
911 /* Create a new account */
913 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli,
914 TALLOC_CTX *mem_ctx,
915 int argc,
916 const char **argv)
918 struct policy_handle dom_pol;
919 struct policy_handle user_pol;
920 NTSTATUS status, result;
921 uint32_t des_access = 0x000f000f;
922 struct dcerpc_binding_handle *b = cli->binding_handle;
923 union lsa_revision_info out_revision_info = {
924 .info1 = {
925 .revision = 0,
928 uint32_t out_version = 0;
930 struct dom_sid sid;
932 if (argc != 2 ) {
933 printf("Usage: %s SID\n", argv[0]);
934 return NT_STATUS_OK;
937 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
938 if (!NT_STATUS_IS_OK(status))
939 goto done;
941 status = dcerpc_lsa_open_policy_fallback(b,
942 mem_ctx,
943 cli->srv_name_slash,
944 true,
945 SEC_FLAG_MAXIMUM_ALLOWED,
946 &out_version,
947 &out_revision_info,
948 &dom_pol,
949 &result);
950 if (any_nt_status_not_ok(status, result, &status)) {
951 goto done;
954 status = dcerpc_lsa_CreateAccount(b, mem_ctx,
955 &dom_pol,
956 &sid,
957 des_access,
958 &user_pol,
959 &result);
960 if (!NT_STATUS_IS_OK(status))
961 goto done;
962 if (!NT_STATUS_IS_OK(result)) {
963 status = result;
964 goto done;
967 printf("Account for SID %s successfully created\n\n", argv[1]);
968 status = NT_STATUS_OK;
970 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
971 done:
972 return status;
976 /* Enumerate the privileges of an SID */
978 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli,
979 TALLOC_CTX *mem_ctx,
980 int argc,
981 const char **argv)
983 struct policy_handle dom_pol;
984 struct policy_handle user_pol;
985 NTSTATUS status, result;
986 uint32_t access_desired = 0x000f000f;
987 struct dom_sid sid;
988 struct lsa_PrivilegeSet *privs = NULL;
989 int i;
990 struct dcerpc_binding_handle *b = cli->binding_handle;
991 union lsa_revision_info out_revision_info = {
992 .info1 = {
993 .revision = 0,
996 uint32_t out_version = 0;
998 if (argc != 2 ) {
999 printf("Usage: %s SID\n", argv[0]);
1000 return NT_STATUS_OK;
1003 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1004 if (!NT_STATUS_IS_OK(status))
1005 goto done;
1007 status = dcerpc_lsa_open_policy_fallback(b,
1008 mem_ctx,
1009 cli->srv_name_slash,
1010 true,
1011 SEC_FLAG_MAXIMUM_ALLOWED,
1012 &out_version,
1013 &out_revision_info,
1014 &dom_pol,
1015 &result);
1016 if (any_nt_status_not_ok(status, result, &status)) {
1017 goto done;
1020 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1021 &dom_pol,
1022 &sid,
1023 access_desired,
1024 &user_pol,
1025 &result);
1026 if (!NT_STATUS_IS_OK(status))
1027 goto done;
1028 if (!NT_STATUS_IS_OK(result)) {
1029 status = result;
1030 goto done;
1033 status = dcerpc_lsa_EnumPrivsAccount(b, mem_ctx,
1034 &user_pol,
1035 &privs,
1036 &result);
1037 if (!NT_STATUS_IS_OK(status))
1038 goto done;
1039 if (!NT_STATUS_IS_OK(result)) {
1040 status = result;
1041 goto done;
1044 /* Print results */
1045 printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
1046 printf("high\tlow\tattribute\n");
1048 for (i = 0; i < privs->count; i++) {
1049 printf("%u\t%u\t%u\n",
1050 privs->set[i].luid.high,
1051 privs->set[i].luid.low,
1052 privs->set[i].attribute);
1055 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1056 done:
1057 return status;
1061 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
1063 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli,
1064 TALLOC_CTX *mem_ctx,
1065 int argc,
1066 const char **argv)
1068 struct policy_handle dom_pol;
1069 NTSTATUS status, result;
1070 struct dom_sid sid;
1071 struct dom_sid_buf buf;
1072 struct lsa_RightSet rights;
1073 struct dcerpc_binding_handle *b = cli->binding_handle;
1074 union lsa_revision_info out_revision_info = {
1075 .info1 = {
1076 .revision = 0,
1079 uint32_t out_version = 0;
1081 int i;
1083 if (argc != 2 ) {
1084 printf("Usage: %s SID\n", argv[0]);
1085 return NT_STATUS_OK;
1088 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1089 if (!NT_STATUS_IS_OK(status))
1090 goto done;
1092 status = dcerpc_lsa_open_policy_fallback(b,
1093 mem_ctx,
1094 cli->srv_name_slash,
1095 true,
1096 SEC_FLAG_MAXIMUM_ALLOWED,
1097 &out_version,
1098 &out_revision_info,
1099 &dom_pol,
1100 &result);
1101 if (any_nt_status_not_ok(status, result, &status)) {
1102 goto done;
1105 status = dcerpc_lsa_EnumAccountRights(b, mem_ctx,
1106 &dom_pol,
1107 &sid,
1108 &rights,
1109 &result);
1110 if (!NT_STATUS_IS_OK(status))
1111 goto done;
1112 if (!NT_STATUS_IS_OK(result)) {
1113 status = result;
1114 goto done;
1117 printf("found %d privileges for SID %s\n", rights.count,
1118 dom_sid_str_buf(&sid, &buf));
1120 for (i = 0; i < rights.count; i++) {
1121 printf("\t%s\n", rights.names[i].string);
1124 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1125 done:
1126 return status;
1130 /* add some privileges to a SID via LsaAddAccountRights */
1132 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli,
1133 TALLOC_CTX *mem_ctx,
1134 int argc,
1135 const char **argv)
1137 struct policy_handle dom_pol;
1138 NTSTATUS status, result;
1139 struct lsa_RightSet rights;
1140 struct dom_sid sid;
1141 int i;
1142 struct dcerpc_binding_handle *b = cli->binding_handle;
1143 union lsa_revision_info out_revision_info = {
1144 .info1 = {
1145 .revision = 0,
1148 uint32_t out_version = 0;
1150 if (argc < 3 ) {
1151 printf("Usage: %s SID [rights...]\n", argv[0]);
1152 return NT_STATUS_OK;
1155 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1156 if (!NT_STATUS_IS_OK(status))
1157 goto done;
1159 status = dcerpc_lsa_open_policy_fallback(b,
1160 mem_ctx,
1161 cli->srv_name_slash,
1162 true,
1163 SEC_FLAG_MAXIMUM_ALLOWED,
1164 &out_version,
1165 &out_revision_info,
1166 &dom_pol,
1167 &result);
1168 if (any_nt_status_not_ok(status, result, &status)) {
1169 goto done;
1172 rights.count = argc-2;
1173 rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
1174 rights.count);
1175 if (!rights.names) {
1176 return NT_STATUS_NO_MEMORY;
1179 for (i=0; i<argc-2; i++) {
1180 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
1183 status = dcerpc_lsa_AddAccountRights(b, mem_ctx,
1184 &dom_pol,
1185 &sid,
1186 &rights,
1187 &result);
1188 if (!NT_STATUS_IS_OK(status))
1189 goto done;
1190 if (!NT_STATUS_IS_OK(result)) {
1191 status = result;
1192 goto done;
1195 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1196 done:
1197 return status;
1201 /* remove some privileges to a SID via LsaRemoveAccountRights */
1203 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli,
1204 TALLOC_CTX *mem_ctx,
1205 int argc,
1206 const char **argv)
1208 struct policy_handle dom_pol;
1209 NTSTATUS status, result;
1210 struct lsa_RightSet rights;
1211 struct dom_sid sid;
1212 int i;
1213 struct dcerpc_binding_handle *b = cli->binding_handle;
1214 union lsa_revision_info out_revision_info = {
1215 .info1 = {
1216 .revision = 0,
1219 uint32_t out_version = 0;
1221 if (argc < 3 ) {
1222 printf("Usage: %s SID [rights...]\n", argv[0]);
1223 return NT_STATUS_OK;
1226 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1227 if (!NT_STATUS_IS_OK(status))
1228 goto done;
1230 status = dcerpc_lsa_open_policy_fallback(b,
1231 mem_ctx,
1232 cli->srv_name_slash,
1233 true,
1234 SEC_FLAG_MAXIMUM_ALLOWED,
1235 &out_version,
1236 &out_revision_info,
1237 &dom_pol,
1238 &result);
1239 if (any_nt_status_not_ok(status, result, &status)) {
1240 goto done;
1243 rights.count = argc-2;
1244 rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
1245 rights.count);
1246 if (!rights.names) {
1247 return NT_STATUS_NO_MEMORY;
1250 for (i=0; i<argc-2; i++) {
1251 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
1254 status = dcerpc_lsa_RemoveAccountRights(b, mem_ctx,
1255 &dom_pol,
1256 &sid,
1257 false,
1258 &rights,
1259 &result);
1260 if (!NT_STATUS_IS_OK(status))
1261 goto done;
1262 if (!NT_STATUS_IS_OK(result)) {
1263 status = result;
1264 goto done;
1267 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1269 done:
1270 return status;
1274 /* Get a privilege value given its name */
1276 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli,
1277 TALLOC_CTX *mem_ctx,
1278 int argc,
1279 const char **argv)
1281 struct policy_handle pol;
1282 NTSTATUS status, result;
1283 struct lsa_LUID luid;
1284 struct lsa_String name;
1285 struct dcerpc_binding_handle *b = cli->binding_handle;
1286 union lsa_revision_info out_revision_info = {
1287 .info1 = {
1288 .revision = 0,
1291 uint32_t out_version = 0;
1293 if (argc != 2 ) {
1294 printf("Usage: %s name\n", argv[0]);
1295 return NT_STATUS_OK;
1298 status = dcerpc_lsa_open_policy_fallback(b,
1299 mem_ctx,
1300 cli->srv_name_slash,
1301 true,
1302 SEC_FLAG_MAXIMUM_ALLOWED,
1303 &out_version,
1304 &out_revision_info,
1305 &pol,
1306 &result);
1307 if (any_nt_status_not_ok(status, result, &status)) {
1308 goto done;
1311 init_lsa_String(&name, argv[1]);
1313 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1314 &pol,
1315 &name,
1316 &luid,
1317 &result);
1318 if (!NT_STATUS_IS_OK(status))
1319 goto done;
1320 if (!NT_STATUS_IS_OK(result)) {
1321 status = result;
1322 goto done;
1325 /* Print results */
1327 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
1329 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1330 done:
1331 return status;
1334 /* Query LSA security object */
1336 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli,
1337 TALLOC_CTX *mem_ctx,
1338 int argc,
1339 const char **argv)
1341 struct policy_handle pol;
1342 NTSTATUS status, result;
1343 struct sec_desc_buf *sdb;
1344 uint32_t sec_info = SECINFO_DACL;
1345 struct dcerpc_binding_handle *b = cli->binding_handle;
1346 union lsa_revision_info out_revision_info = {
1347 .info1 = {
1348 .revision = 0,
1351 uint32_t out_version = 0;
1353 if (argc < 1 || argc > 2) {
1354 printf("Usage: %s [sec_info]\n", argv[0]);
1355 return NT_STATUS_OK;
1358 if (argc == 2)
1359 sscanf(argv[1], "%x", &sec_info);
1361 status = dcerpc_lsa_open_policy_fallback(b,
1362 mem_ctx,
1363 cli->srv_name_slash,
1364 true,
1365 SEC_FLAG_MAXIMUM_ALLOWED,
1366 &out_version,
1367 &out_revision_info,
1368 &pol,
1369 &result);
1370 if (any_nt_status_not_ok(status, result, &status)) {
1371 goto done;
1374 status = dcerpc_lsa_QuerySecurity(b, mem_ctx,
1375 &pol,
1376 sec_info,
1377 &sdb,
1378 &result);
1379 if (!NT_STATUS_IS_OK(status))
1380 goto done;
1381 if (!NT_STATUS_IS_OK(result)) {
1382 status = result;
1383 goto done;
1386 /* Print results */
1388 display_sec_desc(sdb->sd);
1390 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1391 done:
1392 return status;
1395 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
1396 DATA_BLOB session_key)
1398 char *pwd, *pwd_old;
1400 DATA_BLOB data = data_blob_const(p->password->data, p->password->length);
1401 DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
1403 pwd = sess_decrypt_string(talloc_tos(), &data, &session_key);
1404 pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key);
1406 d_printf("Password:\t%s\n", pwd);
1407 d_printf("Old Password:\t%s\n", pwd_old);
1409 talloc_free(pwd);
1410 talloc_free(pwd_old);
1413 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
1414 union lsa_TrustedDomainInfo *info,
1415 enum lsa_TrustDomInfoEnum info_class,
1416 DATA_BLOB session_key)
1418 switch (info_class) {
1419 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
1420 display_trust_dom_info_4(&info->password, session_key);
1421 break;
1422 default: {
1423 const char *str = NULL;
1424 str = NDR_PRINT_UNION_STRING(mem_ctx,
1425 lsa_TrustedDomainInfo,
1426 info_class, info);
1427 if (str) {
1428 d_printf("%s\n", str);
1430 break;
1435 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
1436 TALLOC_CTX *mem_ctx,
1437 int argc,
1438 const char **argv)
1440 struct policy_handle pol;
1441 NTSTATUS status, result;
1442 struct dom_sid dom_sid;
1443 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1444 union lsa_TrustedDomainInfo *info = NULL;
1445 enum lsa_TrustDomInfoEnum info_class = 1;
1446 DATA_BLOB session_key;
1447 struct dcerpc_binding_handle *b = cli->binding_handle;
1448 union lsa_revision_info out_revision_info = {
1449 .info1 = {
1450 .revision = 0,
1453 uint32_t out_version = 0;
1455 if (argc > 3 || argc < 2) {
1456 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1457 return NT_STATUS_OK;
1460 if (!string_to_sid(&dom_sid, argv[1]))
1461 return NT_STATUS_NO_MEMORY;
1463 if (argc == 3)
1464 info_class = atoi(argv[2]);
1466 status = dcerpc_lsa_open_policy_fallback(b,
1467 mem_ctx,
1468 cli->srv_name_slash,
1469 true,
1470 access_mask,
1471 &out_version,
1472 &out_revision_info,
1473 &pol,
1474 &result);
1475 if (any_nt_status_not_ok(status, result, &status)) {
1476 goto done;
1479 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
1480 &pol,
1481 &dom_sid,
1482 info_class,
1483 &info,
1484 &result);
1485 if (!NT_STATUS_IS_OK(status))
1486 goto done;
1487 if (!NT_STATUS_IS_OK(result)) {
1488 status = result;
1489 goto done;
1492 status = cli_get_session_key(mem_ctx, cli, &session_key);
1493 if (!NT_STATUS_IS_OK(status)) {
1494 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1495 goto done;
1498 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1500 done:
1501 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1503 return status;
1506 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1507 TALLOC_CTX *mem_ctx,
1508 int argc,
1509 const char **argv)
1511 struct policy_handle pol;
1512 NTSTATUS status, result;
1513 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1514 union lsa_TrustedDomainInfo *info = NULL;
1515 enum lsa_TrustDomInfoEnum info_class = 1;
1516 struct lsa_String trusted_domain;
1517 struct dcerpc_binding_handle *b = cli->binding_handle;
1518 DATA_BLOB session_key;
1519 union lsa_revision_info out_revision_info = {
1520 .info1 = {
1521 .revision = 0,
1524 uint32_t out_version = 0;
1526 if (argc > 3 || argc < 2) {
1527 printf("Usage: %s [name] [info_class]\n", argv[0]);
1528 return NT_STATUS_OK;
1531 if (argc == 3)
1532 info_class = atoi(argv[2]);
1534 status = dcerpc_lsa_open_policy_fallback(b,
1535 mem_ctx,
1536 cli->srv_name_slash,
1537 true,
1538 access_mask,
1539 &out_version,
1540 &out_revision_info,
1541 &pol,
1542 &result);
1543 if (any_nt_status_not_ok(status, result, &status)) {
1544 goto done;
1547 init_lsa_String(&trusted_domain, argv[1]);
1549 status = dcerpc_lsa_QueryTrustedDomainInfoByName(b, mem_ctx,
1550 &pol,
1551 &trusted_domain,
1552 info_class,
1553 &info,
1554 &result);
1555 if (!NT_STATUS_IS_OK(status))
1556 goto done;
1557 if (!NT_STATUS_IS_OK(result)) {
1558 status = result;
1559 goto done;
1562 status = cli_get_session_key(mem_ctx, cli, &session_key);
1563 if (!NT_STATUS_IS_OK(status)) {
1564 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1565 goto done;
1568 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1570 done:
1571 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1573 return status;
1576 static NTSTATUS cmd_lsa_set_trustdominfo(struct rpc_pipe_client *cli,
1577 TALLOC_CTX *mem_ctx, int argc,
1578 const char **argv)
1580 struct policy_handle pol, trustdom_pol;
1581 NTSTATUS status, result;
1582 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1583 union lsa_TrustedDomainInfo info;
1584 struct dom_sid dom_sid;
1585 enum lsa_TrustDomInfoEnum info_class = 1;
1586 struct dcerpc_binding_handle *b = cli->binding_handle;
1587 union lsa_revision_info out_revision_info = {
1588 .info1 = {
1589 .revision = 0,
1592 uint32_t out_version = 0;
1594 if (argc > 4 || argc < 3) {
1595 printf("Usage: %s [sid] [info_class] [value]\n", argv[0]);
1596 return NT_STATUS_OK;
1599 if (!string_to_sid(&dom_sid, argv[1])) {
1600 return NT_STATUS_NO_MEMORY;
1604 info_class = atoi(argv[2]);
1606 switch (info_class) {
1607 case 13: /* LSA_TRUSTED_DOMAIN_SUPPORTED_ENCRYPTION_TYPES */
1608 info.enc_types.enc_types = atoi(argv[3]);
1609 break;
1610 default:
1611 return NT_STATUS_INVALID_PARAMETER;
1614 status = dcerpc_lsa_open_policy_fallback(b,
1615 mem_ctx,
1616 cli->srv_name_slash,
1617 true,
1618 access_mask,
1619 &out_version,
1620 &out_revision_info,
1621 &pol,
1622 &result);
1623 if (any_nt_status_not_ok(status, result, &status)) {
1624 goto done;
1627 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
1628 &pol,
1629 &dom_sid,
1630 access_mask,
1631 &trustdom_pol,
1632 &result);
1633 if (!NT_STATUS_IS_OK(status)) {
1634 goto done;
1636 if (!NT_STATUS_IS_OK(result)) {
1637 status = result;
1638 goto done;
1641 status = dcerpc_lsa_SetInformationTrustedDomain(b, mem_ctx,
1642 &trustdom_pol,
1643 info_class,
1644 &info,
1645 &result);
1646 if (!NT_STATUS_IS_OK(status)) {
1647 goto done;
1649 if (!NT_STATUS_IS_OK(result)) {
1650 status = result;
1651 goto done;
1653 done:
1654 dcerpc_lsa_Close(b, mem_ctx, &trustdom_pol, &result);
1655 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1657 return status;
1660 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1661 TALLOC_CTX *mem_ctx,
1662 int argc,
1663 const char **argv)
1665 struct policy_handle pol, trustdom_pol;
1666 NTSTATUS status, result;
1667 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1668 union lsa_TrustedDomainInfo *info = NULL;
1669 struct dom_sid dom_sid;
1670 enum lsa_TrustDomInfoEnum info_class = 1;
1671 DATA_BLOB session_key;
1672 struct dcerpc_binding_handle *b = cli->binding_handle;
1673 union lsa_revision_info out_revision_info = {
1674 .info1 = {
1675 .revision = 0,
1678 uint32_t out_version = 0;
1680 if (argc > 3 || argc < 2) {
1681 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1682 return NT_STATUS_OK;
1685 if (!string_to_sid(&dom_sid, argv[1]))
1686 return NT_STATUS_NO_MEMORY;
1689 if (argc == 3)
1690 info_class = atoi(argv[2]);
1692 status = dcerpc_lsa_open_policy_fallback(b,
1693 mem_ctx,
1694 cli->srv_name_slash,
1695 true,
1696 access_mask,
1697 &out_version,
1698 &out_revision_info,
1699 &pol,
1700 &result);
1701 if (any_nt_status_not_ok(status, result, &status)) {
1702 goto done;
1705 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
1706 &pol,
1707 &dom_sid,
1708 access_mask,
1709 &trustdom_pol,
1710 &result);
1711 if (!NT_STATUS_IS_OK(status))
1712 goto done;
1713 if (!NT_STATUS_IS_OK(result)) {
1714 status = result;
1715 goto done;
1718 status = dcerpc_lsa_QueryTrustedDomainInfo(b, mem_ctx,
1719 &trustdom_pol,
1720 info_class,
1721 &info,
1722 &result);
1723 if (!NT_STATUS_IS_OK(status))
1724 goto done;
1725 if (!NT_STATUS_IS_OK(result)) {
1726 status = result;
1727 goto done;
1730 status = cli_get_session_key(mem_ctx, cli, &session_key);
1731 if (!NT_STATUS_IS_OK(status)) {
1732 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1733 goto done;
1736 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1738 done:
1739 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1741 return status;
1744 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1745 TALLOC_CTX *mem_ctx, int argc,
1746 const char **argv)
1748 NTSTATUS status, result;
1749 const char *servername = cli->desthost;
1750 struct lsa_String *account_name = NULL;
1751 struct lsa_String *authority_name = NULL;
1752 struct dcerpc_binding_handle *b = cli->binding_handle;
1754 if (argc > 2) {
1755 printf("Usage: %s servername\n", argv[0]);
1756 return NT_STATUS_OK;
1759 status = dcerpc_lsa_GetUserName(b, mem_ctx,
1760 servername,
1761 &account_name,
1762 &authority_name,
1763 &result);
1764 if (!NT_STATUS_IS_OK(status)) {
1765 goto done;
1767 if (!NT_STATUS_IS_OK(result)) {
1768 status = result;
1769 goto done;
1772 /* Print results */
1774 printf("Account Name: %s, Authority Name: %s\n",
1775 account_name->string, authority_name ? authority_name->string :
1776 "");
1778 done:
1779 return status;
1782 static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
1783 TALLOC_CTX *mem_ctx, int argc,
1784 const char **argv)
1786 struct policy_handle dom_pol, user_pol;
1787 NTSTATUS status, result;
1788 struct lsa_PrivilegeSet privs;
1789 struct lsa_LUIDAttribute *set = NULL;
1790 struct dom_sid sid;
1791 int i;
1792 struct dcerpc_binding_handle *b = cli->binding_handle;
1793 union lsa_revision_info out_revision_info = {
1794 .info1 = {
1795 .revision = 0,
1798 uint32_t out_version = 0;
1800 ZERO_STRUCT(privs);
1802 if (argc < 3 ) {
1803 printf("Usage: %s SID [rights...]\n", argv[0]);
1804 return NT_STATUS_OK;
1807 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1808 if (!NT_STATUS_IS_OK(status)) {
1809 goto done;
1812 status = dcerpc_lsa_open_policy_fallback(b,
1813 mem_ctx,
1814 cli->srv_name_slash,
1815 true,
1816 SEC_FLAG_MAXIMUM_ALLOWED,
1817 &out_version,
1818 &out_revision_info,
1819 &dom_pol,
1820 &result);
1821 if (any_nt_status_not_ok(status, result, &status)) {
1822 goto done;
1825 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1826 &dom_pol,
1827 &sid,
1828 SEC_FLAG_MAXIMUM_ALLOWED,
1829 &user_pol,
1830 &result);
1831 if (!NT_STATUS_IS_OK(status)) {
1832 goto done;
1834 if (!NT_STATUS_IS_OK(result)) {
1835 status = result;
1836 goto done;
1839 for (i=2; i<argc; i++) {
1841 struct lsa_String priv_name;
1842 struct lsa_LUID luid;
1844 init_lsa_String(&priv_name, argv[i]);
1846 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1847 &dom_pol,
1848 &priv_name,
1849 &luid,
1850 &result);
1851 if (!NT_STATUS_IS_OK(status)) {
1852 continue;
1854 if (!NT_STATUS_IS_OK(result)) {
1855 status = result;
1856 continue;
1859 privs.count++;
1860 set = talloc_realloc(mem_ctx, set,
1861 struct lsa_LUIDAttribute,
1862 privs.count);
1863 if (!set) {
1864 return NT_STATUS_NO_MEMORY;
1867 set[privs.count-1].luid = luid;
1868 set[privs.count-1].attribute = 0;
1871 privs.set = set;
1873 status = dcerpc_lsa_AddPrivilegesToAccount(b, mem_ctx,
1874 &user_pol,
1875 &privs,
1876 &result);
1877 if (!NT_STATUS_IS_OK(status)) {
1878 goto done;
1880 if (!NT_STATUS_IS_OK(result)) {
1881 status = result;
1882 goto done;
1885 dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
1886 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1887 done:
1888 return status;
1891 static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
1892 TALLOC_CTX *mem_ctx, int argc,
1893 const char **argv)
1895 struct policy_handle dom_pol, user_pol;
1896 NTSTATUS status, result;
1897 struct lsa_PrivilegeSet privs;
1898 struct lsa_LUIDAttribute *set = NULL;
1899 struct dom_sid sid;
1900 int i;
1901 struct dcerpc_binding_handle *b = cli->binding_handle;
1902 union lsa_revision_info out_revision_info = {
1903 .info1 = {
1904 .revision = 0,
1907 uint32_t out_version = 0;
1909 ZERO_STRUCT(privs);
1911 if (argc < 3 ) {
1912 printf("Usage: %s SID [rights...]\n", argv[0]);
1913 return NT_STATUS_OK;
1916 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1917 if (!NT_STATUS_IS_OK(status)) {
1918 goto done;
1921 status = dcerpc_lsa_open_policy_fallback(b,
1922 mem_ctx,
1923 cli->srv_name_slash,
1924 true,
1925 SEC_FLAG_MAXIMUM_ALLOWED,
1926 &out_version,
1927 &out_revision_info,
1928 &dom_pol,
1929 &result);
1930 if (any_nt_status_not_ok(status, result, &status)) {
1931 goto done;
1934 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1935 &dom_pol,
1936 &sid,
1937 SEC_FLAG_MAXIMUM_ALLOWED,
1938 &user_pol,
1939 &result);
1940 if (!NT_STATUS_IS_OK(status)) {
1941 goto done;
1943 if (!NT_STATUS_IS_OK(result)) {
1944 status = result;
1945 goto done;
1948 for (i=2; i<argc; i++) {
1950 struct lsa_String priv_name;
1951 struct lsa_LUID luid;
1953 init_lsa_String(&priv_name, argv[i]);
1955 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1956 &dom_pol,
1957 &priv_name,
1958 &luid,
1959 &result);
1960 if (!NT_STATUS_IS_OK(status)) {
1961 continue;
1963 if (!NT_STATUS_IS_OK(result)) {
1964 status = result;
1965 continue;
1968 privs.count++;
1969 set = talloc_realloc(mem_ctx, set,
1970 struct lsa_LUIDAttribute,
1971 privs.count);
1972 if (!set) {
1973 return NT_STATUS_NO_MEMORY;
1976 set[privs.count-1].luid = luid;
1977 set[privs.count-1].attribute = 0;
1980 privs.set = set;
1983 status = dcerpc_lsa_RemovePrivilegesFromAccount(b, mem_ctx,
1984 &user_pol,
1985 false,
1986 &privs,
1987 &result);
1988 if (!NT_STATUS_IS_OK(status)) {
1989 goto done;
1991 if (!NT_STATUS_IS_OK(result)) {
1992 status = result;
1993 goto done;
1996 dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
1997 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1998 done:
1999 return status;
2002 static NTSTATUS cmd_lsa_create_secret(struct rpc_pipe_client *cli,
2003 TALLOC_CTX *mem_ctx, int argc,
2004 const char **argv)
2006 NTSTATUS status, result;
2007 struct policy_handle handle, sec_handle;
2008 struct lsa_String name;
2009 struct dcerpc_binding_handle *b = cli->binding_handle;
2010 union lsa_revision_info out_revision_info = {
2011 .info1 = {
2012 .revision = 0,
2015 uint32_t out_version = 0;
2017 if (argc < 2) {
2018 printf("Usage: %s name\n", argv[0]);
2019 return NT_STATUS_OK;
2022 status = dcerpc_lsa_open_policy_fallback(b,
2023 mem_ctx,
2024 cli->srv_name_slash,
2025 true,
2026 SEC_FLAG_MAXIMUM_ALLOWED,
2027 &out_version,
2028 &out_revision_info,
2029 &sec_handle,
2030 &result);
2031 if (any_nt_status_not_ok(status, result, &status)) {
2032 return status;
2035 init_lsa_String(&name, argv[1]);
2037 status = dcerpc_lsa_CreateSecret(b, mem_ctx,
2038 &handle,
2039 name,
2040 SEC_FLAG_MAXIMUM_ALLOWED,
2041 &sec_handle,
2042 &result);
2043 if (!NT_STATUS_IS_OK(status)) {
2044 goto done;
2046 if (!NT_STATUS_IS_OK(result)) {
2047 status = result;
2048 goto done;
2051 done:
2052 if (is_valid_policy_hnd(&sec_handle)) {
2053 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
2055 if (is_valid_policy_hnd(&handle)) {
2056 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2059 return status;
2062 static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
2063 TALLOC_CTX *mem_ctx, int argc,
2064 const char **argv)
2066 NTSTATUS status, result;
2067 struct policy_handle handle, sec_handle;
2068 struct lsa_String name;
2069 struct dcerpc_binding_handle *b = cli->binding_handle;
2070 union lsa_revision_info out_revision_info = {
2071 .info1 = {
2072 .revision = 0,
2075 uint32_t out_version = 0;
2077 if (argc < 2) {
2078 printf("Usage: %s name\n", argv[0]);
2079 return NT_STATUS_OK;
2082 status = dcerpc_lsa_open_policy_fallback(b,
2083 mem_ctx,
2084 cli->srv_name_slash,
2085 true,
2086 SEC_FLAG_MAXIMUM_ALLOWED,
2087 &out_version,
2088 &out_revision_info,
2089 &handle,
2090 &result);
2091 if (any_nt_status_not_ok(status, result, &status)) {
2092 return status;
2095 init_lsa_String(&name, argv[1]);
2097 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
2098 &handle,
2099 name,
2100 SEC_FLAG_MAXIMUM_ALLOWED,
2101 &sec_handle,
2102 &result);
2103 if (!NT_STATUS_IS_OK(status)) {
2104 goto done;
2106 if (!NT_STATUS_IS_OK(result)) {
2107 status = result;
2108 goto done;
2111 status = dcerpc_lsa_DeleteObject(b, mem_ctx,
2112 &sec_handle,
2113 &result);
2114 if (!NT_STATUS_IS_OK(status)) {
2115 goto done;
2117 if (!NT_STATUS_IS_OK(result)) {
2118 status = result;
2119 goto done;
2122 done:
2123 if (is_valid_policy_hnd(&sec_handle)) {
2124 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
2126 if (is_valid_policy_hnd(&handle)) {
2127 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2130 return status;
2133 static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
2134 TALLOC_CTX *mem_ctx, int argc,
2135 const char **argv)
2137 NTSTATUS status, result;
2138 struct policy_handle handle, sec_handle;
2139 struct lsa_String name;
2140 struct lsa_DATA_BUF_PTR new_val;
2141 NTTIME new_mtime = 0;
2142 struct lsa_DATA_BUF_PTR old_val;
2143 NTTIME old_mtime = 0;
2144 DATA_BLOB session_key;
2145 DATA_BLOB new_blob = data_blob_null;
2146 DATA_BLOB old_blob = data_blob_null;
2147 char *new_secret, *old_secret;
2148 struct dcerpc_binding_handle *b = cli->binding_handle;
2149 union lsa_revision_info out_revision_info = {
2150 .info1 = {
2151 .revision = 0,
2154 uint32_t out_version = 0;
2156 if (argc < 2) {
2157 printf("Usage: %s name\n", argv[0]);
2158 return NT_STATUS_OK;
2161 status = dcerpc_lsa_open_policy_fallback(b,
2162 mem_ctx,
2163 cli->srv_name_slash,
2164 true,
2165 SEC_FLAG_MAXIMUM_ALLOWED,
2166 &out_version,
2167 &out_revision_info,
2168 &handle,
2169 &result);
2170 if (any_nt_status_not_ok(status, result, &status)) {
2171 return status;
2174 init_lsa_String(&name, argv[1]);
2176 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
2177 &handle,
2178 name,
2179 SEC_FLAG_MAXIMUM_ALLOWED,
2180 &sec_handle,
2181 &result);
2182 if (!NT_STATUS_IS_OK(status)) {
2183 goto done;
2185 if (!NT_STATUS_IS_OK(result)) {
2186 status = result;
2187 goto done;
2190 ZERO_STRUCT(new_val);
2191 ZERO_STRUCT(old_val);
2193 status = dcerpc_lsa_QuerySecret(b, mem_ctx,
2194 &sec_handle,
2195 &new_val,
2196 &new_mtime,
2197 &old_val,
2198 &old_mtime,
2199 &result);
2200 if (!NT_STATUS_IS_OK(status)) {
2201 goto done;
2203 if (!NT_STATUS_IS_OK(result)) {
2204 status = result;
2205 goto done;
2208 status = cli_get_session_key(mem_ctx, cli, &session_key);
2209 if (!NT_STATUS_IS_OK(status)) {
2210 goto done;
2213 if (new_val.buf) {
2214 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
2216 if (old_val.buf) {
2217 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
2220 new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
2221 old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
2222 if (new_secret) {
2223 d_printf("new secret: %s\n", new_secret);
2225 if (old_secret) {
2226 d_printf("old secret: %s\n", old_secret);
2229 done:
2230 if (is_valid_policy_hnd(&sec_handle)) {
2231 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
2233 if (is_valid_policy_hnd(&handle)) {
2234 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2237 return status;
2240 static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
2241 TALLOC_CTX *mem_ctx, int argc,
2242 const char **argv)
2244 NTSTATUS status, result;
2245 struct policy_handle handle, sec_handle;
2246 struct lsa_String name;
2247 struct lsa_DATA_BUF new_val;
2248 struct lsa_DATA_BUF old_val;
2249 DATA_BLOB enc_key;
2250 DATA_BLOB session_key;
2251 struct dcerpc_binding_handle *b = cli->binding_handle;
2252 union lsa_revision_info out_revision_info = {
2253 .info1 = {
2254 .revision = 0,
2257 uint32_t out_version = 0;
2259 if (argc < 3) {
2260 printf("Usage: %s name secret\n", argv[0]);
2261 return NT_STATUS_OK;
2264 status = dcerpc_lsa_open_policy_fallback(b,
2265 mem_ctx,
2266 cli->srv_name_slash,
2267 true,
2268 SEC_FLAG_MAXIMUM_ALLOWED,
2269 &out_version,
2270 &out_revision_info,
2271 &handle,
2272 &result);
2273 if (any_nt_status_not_ok(status, result, &status)) {
2274 return status;
2277 init_lsa_String(&name, argv[1]);
2279 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
2280 &handle,
2281 name,
2282 SEC_FLAG_MAXIMUM_ALLOWED,
2283 &sec_handle,
2284 &result);
2285 if (!NT_STATUS_IS_OK(status)) {
2286 goto done;
2288 if (!NT_STATUS_IS_OK(result)) {
2289 status = result;
2290 goto done;
2293 ZERO_STRUCT(new_val);
2294 ZERO_STRUCT(old_val);
2296 status = cli_get_session_key(mem_ctx, cli, &session_key);
2297 if (!NT_STATUS_IS_OK(status)) {
2298 goto done;
2301 enc_key = sess_encrypt_string(argv[2], &session_key);
2303 new_val.length = enc_key.length;
2304 new_val.size = enc_key.length;
2305 new_val.data = enc_key.data;
2307 status = dcerpc_lsa_SetSecret(b, mem_ctx,
2308 &sec_handle,
2309 &new_val,
2310 NULL,
2311 &result);
2312 if (!NT_STATUS_IS_OK(status)) {
2313 goto done;
2315 if (!NT_STATUS_IS_OK(result)) {
2316 status = result;
2317 goto done;
2320 done:
2321 if (is_valid_policy_hnd(&sec_handle)) {
2322 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
2324 if (is_valid_policy_hnd(&handle)) {
2325 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2328 return status;
2331 static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
2332 TALLOC_CTX *mem_ctx, int argc,
2333 const char **argv)
2335 NTSTATUS status, result;
2336 struct policy_handle handle;
2337 struct lsa_String name;
2338 struct lsa_DATA_BUF *val;
2339 DATA_BLOB session_key;
2340 DATA_BLOB blob = data_blob_null;
2341 char *secret;
2342 struct dcerpc_binding_handle *b = cli->binding_handle;
2343 union lsa_revision_info out_revision_info = {
2344 .info1 = {
2345 .revision = 0,
2348 uint32_t out_version = 0;
2350 if (argc < 2) {
2351 printf("Usage: %s name\n", argv[0]);
2352 return NT_STATUS_OK;
2355 status = dcerpc_lsa_open_policy_fallback(b,
2356 mem_ctx,
2357 cli->srv_name_slash,
2358 true,
2359 SEC_FLAG_MAXIMUM_ALLOWED,
2360 &out_version,
2361 &out_revision_info,
2362 &handle,
2363 &result);
2364 if (any_nt_status_not_ok(status, result, &status)) {
2365 return status;
2368 init_lsa_String(&name, argv[1]);
2370 ZERO_STRUCT(val);
2372 status = dcerpc_lsa_RetrievePrivateData(b, mem_ctx,
2373 &handle,
2374 &name,
2375 &val,
2376 &result);
2377 if (!NT_STATUS_IS_OK(status)) {
2378 goto done;
2380 if (!NT_STATUS_IS_OK(result)) {
2381 status = result;
2382 goto done;
2385 status = cli_get_session_key(mem_ctx, cli, &session_key);
2386 if (!NT_STATUS_IS_OK(status)) {
2387 goto done;
2390 if (val) {
2391 blob = data_blob_const(val->data, val->length);
2394 secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
2395 if (secret) {
2396 d_printf("secret: %s\n", secret);
2399 done:
2400 if (is_valid_policy_hnd(&handle)) {
2401 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2404 return status;
2407 static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
2408 TALLOC_CTX *mem_ctx, int argc,
2409 const char **argv)
2411 NTSTATUS status, result;
2412 struct policy_handle handle;
2413 struct lsa_String name;
2414 struct lsa_DATA_BUF val;
2415 DATA_BLOB session_key;
2416 DATA_BLOB enc_key;
2417 struct dcerpc_binding_handle *b = cli->binding_handle;
2418 union lsa_revision_info out_revision_info = {
2419 .info1 = {
2420 .revision = 0,
2423 uint32_t out_version = 0;
2425 if (argc < 3) {
2426 printf("Usage: %s name secret\n", argv[0]);
2427 return NT_STATUS_OK;
2430 status = dcerpc_lsa_open_policy_fallback(b,
2431 mem_ctx,
2432 cli->srv_name_slash,
2433 true,
2434 SEC_FLAG_MAXIMUM_ALLOWED,
2435 &out_version,
2436 &out_revision_info,
2437 &handle,
2438 &result);
2439 if (any_nt_status_not_ok(status, result, &status)) {
2440 return status;
2443 init_lsa_String(&name, argv[1]);
2445 ZERO_STRUCT(val);
2447 status = cli_get_session_key(mem_ctx, cli, &session_key);
2448 if (!NT_STATUS_IS_OK(status)) {
2449 goto done;
2452 enc_key = sess_encrypt_string(argv[2], &session_key);
2454 val.length = enc_key.length;
2455 val.size = enc_key.length;
2456 val.data = enc_key.data;
2458 status = dcerpc_lsa_StorePrivateData(b, mem_ctx,
2459 &handle,
2460 &name,
2461 &val,
2462 &result);
2463 if (!NT_STATUS_IS_OK(status)) {
2464 goto done;
2466 if (!NT_STATUS_IS_OK(result)) {
2467 status = result;
2468 goto done;
2471 done:
2472 if (is_valid_policy_hnd(&handle)) {
2473 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2476 return status;
2479 static NTSTATUS cmd_lsa_create_trusted_domain(struct rpc_pipe_client *cli,
2480 TALLOC_CTX *mem_ctx, int argc,
2481 const char **argv)
2483 NTSTATUS status, result;
2484 struct policy_handle handle, trustdom_handle;
2485 struct dom_sid sid;
2486 struct lsa_DomainInfo info;
2487 struct dcerpc_binding_handle *b = cli->binding_handle;
2488 union lsa_revision_info out_revision_info = {
2489 .info1 = {
2490 .revision = 0,
2493 uint32_t out_version = 0;
2495 if (argc < 3) {
2496 printf("Usage: %s name sid\n", argv[0]);
2497 return NT_STATUS_OK;
2500 status = dcerpc_lsa_open_policy_fallback(b,
2501 mem_ctx,
2502 cli->srv_name_slash,
2503 true,
2504 SEC_FLAG_MAXIMUM_ALLOWED,
2505 &out_version,
2506 &out_revision_info,
2507 &handle,
2508 &result);
2509 if (any_nt_status_not_ok(status, result, &status)) {
2510 return status;
2513 init_lsa_StringLarge(&info.name, argv[1]);
2514 info.sid = &sid;
2515 string_to_sid(&sid, argv[2]);
2517 status = dcerpc_lsa_CreateTrustedDomain(b, mem_ctx,
2518 &handle,
2519 &info,
2520 SEC_FLAG_MAXIMUM_ALLOWED,
2521 &trustdom_handle,
2522 &result);
2523 if (!NT_STATUS_IS_OK(status)) {
2524 goto done;
2526 if (!NT_STATUS_IS_OK(result)) {
2527 status = result;
2528 goto done;
2531 done:
2532 if (is_valid_policy_hnd(&trustdom_handle)) {
2533 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
2536 if (is_valid_policy_hnd(&handle)) {
2537 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2540 return status;
2543 static NTSTATUS cmd_lsa_delete_trusted_domain(struct rpc_pipe_client *cli,
2544 TALLOC_CTX *mem_ctx, int argc,
2545 const char **argv)
2547 NTSTATUS status, result;
2548 struct policy_handle handle, trustdom_handle;
2549 struct lsa_String name;
2550 struct dom_sid *sid = NULL;
2551 struct dcerpc_binding_handle *b = cli->binding_handle;
2552 union lsa_revision_info out_revision_info = {
2553 .info1 = {
2554 .revision = 0,
2557 uint32_t out_version = 0;
2559 if (argc < 2) {
2560 printf("Usage: %s name\n", argv[0]);
2561 return NT_STATUS_OK;
2564 status = dcerpc_lsa_open_policy_fallback(b,
2565 mem_ctx,
2566 cli->srv_name_slash,
2567 true,
2568 SEC_FLAG_MAXIMUM_ALLOWED,
2569 &out_version,
2570 &out_revision_info,
2571 &handle,
2572 &result);
2573 if (any_nt_status_not_ok(status, result, &status)) {
2574 return status;
2577 init_lsa_String(&name, argv[1]);
2579 status = dcerpc_lsa_OpenTrustedDomainByName(b, mem_ctx,
2580 &handle,
2581 name,
2582 SEC_FLAG_MAXIMUM_ALLOWED,
2583 &trustdom_handle,
2584 &result);
2585 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2586 goto delete_object;
2590 uint32_t resume_handle = 0;
2591 struct lsa_DomainList domains;
2592 int i;
2594 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
2595 &handle,
2596 &resume_handle,
2597 &domains,
2598 0xffff,
2599 &result);
2600 if (!NT_STATUS_IS_OK(status)) {
2601 goto done;
2603 if (!NT_STATUS_IS_OK(result)) {
2604 status = result;
2605 goto done;
2608 for (i=0; i < domains.count; i++) {
2609 if (strequal(domains.domains[i].name.string, argv[1])) {
2610 sid = domains.domains[i].sid;
2611 break;
2615 if (!sid) {
2616 return NT_STATUS_INVALID_SID;
2620 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
2621 &handle,
2622 sid,
2623 SEC_FLAG_MAXIMUM_ALLOWED,
2624 &trustdom_handle,
2625 &result);
2626 if (!NT_STATUS_IS_OK(status)) {
2627 goto done;
2629 if (!NT_STATUS_IS_OK(result)) {
2630 status = result;
2631 goto done;
2634 delete_object:
2635 status = dcerpc_lsa_DeleteObject(b, mem_ctx,
2636 &trustdom_handle,
2637 &result);
2638 if (!NT_STATUS_IS_OK(status)) {
2639 goto done;
2641 if (!NT_STATUS_IS_OK(result)) {
2642 status = result;
2643 goto done;
2646 done:
2647 if (is_valid_policy_hnd(&trustdom_handle)) {
2648 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
2651 if (is_valid_policy_hnd(&handle)) {
2652 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2655 return status;
2659 /* List of commands exported by this module */
2661 struct cmd_set lsarpc_commands[] = {
2664 .name = "LSARPC",
2668 .name = "lsaquery",
2669 .returntype = RPC_RTYPE_NTSTATUS,
2670 .ntfn = cmd_lsa_query_info_policy,
2671 .wfn = NULL,
2672 .table = &ndr_table_lsarpc,
2673 .rpc_pipe = NULL,
2674 .description = "Query info policy",
2675 .usage = "",
2678 .name = "lookupsids",
2679 .returntype = RPC_RTYPE_NTSTATUS,
2680 .ntfn = cmd_lsa_lookup_sids,
2681 .wfn = NULL,
2682 .table = &ndr_table_lsarpc,
2683 .rpc_pipe = NULL,
2684 .description = "Convert SIDs to names",
2685 .usage = "",
2688 .name = "lookupsids3",
2689 .returntype = RPC_RTYPE_NTSTATUS,
2690 .ntfn = cmd_lsa_lookup_sids3,
2691 .wfn = NULL,
2692 .table = &ndr_table_lsarpc,
2693 .rpc_pipe = NULL,
2694 .description = "Convert SIDs to names",
2695 .usage = "",
2698 .name = "lookupsids_level",
2699 .returntype = RPC_RTYPE_NTSTATUS,
2700 .ntfn = cmd_lsa_lookup_sids_level,
2701 .wfn = NULL,
2702 .table = &ndr_table_lsarpc,
2703 .rpc_pipe = NULL,
2704 .description = "Convert SIDs to names",
2705 .usage = "",
2708 .name = "lookupnames",
2709 .returntype = RPC_RTYPE_NTSTATUS,
2710 .ntfn = cmd_lsa_lookup_names,
2711 .wfn = NULL,
2712 .table = &ndr_table_lsarpc,
2713 .rpc_pipe = NULL,
2714 .description = "Convert names to SIDs",
2715 .usage = "",
2718 .name = "lookupnames4",
2719 .returntype = RPC_RTYPE_NTSTATUS,
2720 .ntfn = cmd_lsa_lookup_names4,
2721 .wfn = NULL,
2722 .table = &ndr_table_lsarpc,
2723 .rpc_pipe = NULL,
2724 .description = "Convert names to SIDs",
2725 .usage = "",
2728 .name = "lookupnames_level",
2729 .returntype = RPC_RTYPE_NTSTATUS,
2730 .ntfn = cmd_lsa_lookup_names_level,
2731 .wfn = NULL,
2732 .table = &ndr_table_lsarpc,
2733 .rpc_pipe = NULL,
2734 .description = "Convert names to SIDs",
2735 .usage = "",
2738 .name = "enumtrust",
2739 .returntype = RPC_RTYPE_NTSTATUS,
2740 .ntfn = cmd_lsa_enum_trust_dom,
2741 .wfn = NULL,
2742 .table = &ndr_table_lsarpc,
2743 .rpc_pipe = NULL,
2744 .description = "Enumerate trusted domains",
2745 .usage = "Usage: [preferred max number] [enum context (0)]",
2748 .name = "enumprivs",
2749 .returntype = RPC_RTYPE_NTSTATUS,
2750 .ntfn = cmd_lsa_enum_privilege,
2751 .wfn = NULL,
2752 .table = &ndr_table_lsarpc,
2753 .rpc_pipe = NULL,
2754 .description = "Enumerate privileges",
2755 .usage = "",
2758 .name = "getdispname",
2759 .returntype = RPC_RTYPE_NTSTATUS,
2760 .ntfn = cmd_lsa_get_dispname,
2761 .wfn = NULL,
2762 .table = &ndr_table_lsarpc,
2763 .rpc_pipe = NULL,
2764 .description = "Get the privilege name",
2765 .usage = "",
2768 .name = "lsaenumsid",
2769 .returntype = RPC_RTYPE_NTSTATUS,
2770 .ntfn = cmd_lsa_enum_sids,
2771 .wfn = NULL,
2772 .table = &ndr_table_lsarpc,
2773 .rpc_pipe = NULL,
2774 .description = "Enumerate the LSA SIDS",
2775 .usage = "",
2778 .name = "lsacreateaccount",
2779 .returntype = RPC_RTYPE_NTSTATUS,
2780 .ntfn = cmd_lsa_create_account,
2781 .wfn = NULL,
2782 .table = &ndr_table_lsarpc,
2783 .rpc_pipe = NULL,
2784 .description = "Create a new lsa account",
2785 .usage = "",
2788 .name = "lsaenumprivsaccount",
2789 .returntype = RPC_RTYPE_NTSTATUS,
2790 .ntfn = cmd_lsa_enum_privsaccounts,
2791 .wfn = NULL,
2792 .table = &ndr_table_lsarpc,
2793 .rpc_pipe = NULL,
2794 .description = "Enumerate the privileges of an SID",
2795 .usage = "",
2798 .name = "lsaenumacctrights",
2799 .returntype = RPC_RTYPE_NTSTATUS,
2800 .ntfn = cmd_lsa_enum_acct_rights,
2801 .wfn = NULL,
2802 .table = &ndr_table_lsarpc,
2803 .rpc_pipe = NULL,
2804 .description = "Enumerate the rights of an SID",
2805 .usage = "",
2808 .name = "lsaaddpriv",
2809 .returntype = RPC_RTYPE_NTSTATUS,
2810 .ntfn = cmd_lsa_add_priv,
2811 .wfn = NULL,
2812 .table = &ndr_table_lsarpc,
2813 .rpc_pipe = NULL,
2814 .description = "Assign a privilege to a SID",
2815 .usage = "",
2818 .name = "lsadelpriv",
2819 .returntype = RPC_RTYPE_NTSTATUS,
2820 .ntfn = cmd_lsa_del_priv,
2821 .wfn = NULL,
2822 .table = &ndr_table_lsarpc,
2823 .rpc_pipe = NULL,
2824 .description = "Revoke a privilege from a SID",
2825 .usage = "",
2828 .name = "lsaaddacctrights",
2829 .returntype = RPC_RTYPE_NTSTATUS,
2830 .ntfn = cmd_lsa_add_acct_rights,
2831 .wfn = NULL,
2832 .table = &ndr_table_lsarpc,
2833 .rpc_pipe = NULL,
2834 .description = "Add rights to an account",
2835 .usage = "",
2838 .name = "lsaremoveacctrights",
2839 .returntype = RPC_RTYPE_NTSTATUS,
2840 .ntfn = cmd_lsa_remove_acct_rights,
2841 .wfn = NULL,
2842 .table = &ndr_table_lsarpc,
2843 .rpc_pipe = NULL,
2844 .description = "Remove rights from an account",
2845 .usage = "",
2848 .name = "lsalookupprivvalue",
2849 .returntype = RPC_RTYPE_NTSTATUS,
2850 .ntfn = cmd_lsa_lookup_priv_value,
2851 .wfn = NULL,
2852 .table = &ndr_table_lsarpc,
2853 .rpc_pipe = NULL,
2854 .description = "Get a privilege value given its name",
2855 .usage = "",
2858 .name = "lsaquerysecobj",
2859 .returntype = RPC_RTYPE_NTSTATUS,
2860 .ntfn = cmd_lsa_query_secobj,
2861 .wfn = NULL,
2862 .table = &ndr_table_lsarpc,
2863 .rpc_pipe = NULL,
2864 .description = "Query LSA security object",
2865 .usage = "",
2868 .name = "lsaquerytrustdominfo",
2869 .returntype = RPC_RTYPE_NTSTATUS,
2870 .ntfn = cmd_lsa_query_trustdominfo,
2871 .wfn = NULL,
2872 .table = &ndr_table_lsarpc,
2873 .rpc_pipe = NULL,
2874 .description = "Query LSA trusted domains info (given a SID)",
2875 .usage = "",
2878 .name = "lsaquerytrustdominfobyname",
2879 .returntype = RPC_RTYPE_NTSTATUS,
2880 .ntfn = cmd_lsa_query_trustdominfobyname,
2881 .wfn = NULL,
2882 .table = &ndr_table_lsarpc,
2883 .rpc_pipe = NULL,
2884 .description = "Query LSA trusted domains info (given a name), only works for Windows > 2k",
2885 .usage = "",
2888 .name = "lsaquerytrustdominfobysid",
2889 .returntype = RPC_RTYPE_NTSTATUS,
2890 .ntfn = cmd_lsa_query_trustdominfobysid,
2891 .wfn = NULL,
2892 .table = &ndr_table_lsarpc,
2893 .rpc_pipe = NULL,
2894 .description = "Query LSA trusted domains info (given a SID)",
2895 .usage = "",
2898 .name = "lsasettrustdominfo",
2899 .returntype = RPC_RTYPE_NTSTATUS,
2900 .ntfn = cmd_lsa_set_trustdominfo,
2901 .wfn = NULL,
2902 .table = &ndr_table_lsarpc,
2903 .rpc_pipe = NULL,
2904 .description = "Set LSA trusted domain info",
2905 .usage = "",
2908 .name = "getusername",
2909 .returntype = RPC_RTYPE_NTSTATUS,
2910 .ntfn = cmd_lsa_get_username,
2911 .wfn = NULL,
2912 .table = &ndr_table_lsarpc,
2913 .rpc_pipe = NULL,
2914 .description = "Get username",
2915 .usage = "",
2918 .name = "createsecret",
2919 .returntype = RPC_RTYPE_NTSTATUS,
2920 .ntfn = cmd_lsa_create_secret,
2921 .wfn = NULL,
2922 .table = &ndr_table_lsarpc,
2923 .rpc_pipe = NULL,
2924 .description = "Create Secret",
2925 .usage = "",
2928 .name = "deletesecret",
2929 .returntype = RPC_RTYPE_NTSTATUS,
2930 .ntfn = cmd_lsa_delete_secret,
2931 .wfn = NULL,
2932 .table = &ndr_table_lsarpc,
2933 .rpc_pipe = NULL,
2934 .description = "Delete Secret",
2935 .usage = "",
2938 .name = "querysecret",
2939 .returntype = RPC_RTYPE_NTSTATUS,
2940 .ntfn = cmd_lsa_query_secret,
2941 .wfn = NULL,
2942 .table = &ndr_table_lsarpc,
2943 .rpc_pipe = NULL,
2944 .description = "Query Secret",
2945 .usage = "",
2948 .name = "setsecret",
2949 .returntype = RPC_RTYPE_NTSTATUS,
2950 .ntfn = cmd_lsa_set_secret,
2951 .wfn = NULL,
2952 .table = &ndr_table_lsarpc,
2953 .rpc_pipe = NULL,
2954 .description = "Set Secret",
2955 .usage = "",
2958 .name = "retrieveprivatedata",
2959 .returntype = RPC_RTYPE_NTSTATUS,
2960 .ntfn = cmd_lsa_retrieve_private_data,
2961 .wfn = NULL,
2962 .table = &ndr_table_lsarpc,
2963 .rpc_pipe = NULL,
2964 .description = "Retrieve Private Data",
2965 .usage = "",
2968 .name = "storeprivatedata",
2969 .returntype = RPC_RTYPE_NTSTATUS,
2970 .ntfn = cmd_lsa_store_private_data,
2971 .wfn = NULL,
2972 .table = &ndr_table_lsarpc,
2973 .rpc_pipe = NULL,
2974 .description = "Store Private Data",
2975 .usage = "",
2978 .name = "createtrustdom",
2979 .returntype = RPC_RTYPE_NTSTATUS,
2980 .ntfn = cmd_lsa_create_trusted_domain,
2981 .wfn = NULL,
2982 .table = &ndr_table_lsarpc,
2983 .rpc_pipe = NULL,
2984 .description = "Create Trusted Domain",
2985 .usage = "",
2988 .name = "deletetrustdom",
2989 .returntype = RPC_RTYPE_NTSTATUS,
2990 .ntfn = cmd_lsa_delete_trusted_domain,
2991 .wfn = NULL,
2992 .table = &ndr_table_lsarpc,
2993 .rpc_pipe = NULL,
2994 .description = "Delete Trusted Domain",
2995 .usage = "",
2998 .name = NULL,