s4:registry - "LDB backend" - don't test for "0" as string termination on binary...
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_lsarpc.c
blob04aec66217bd84cb788fe8aa25b0dd4b94dc75d9
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/cli_lsa.h"
28 /* useful function to allow entering a name instead of a SID and
29 * looking it up automatically */
30 static NTSTATUS name_to_sid(struct rpc_pipe_client *cli,
31 TALLOC_CTX *mem_ctx,
32 DOM_SID *sid, const char *name)
34 struct policy_handle pol;
35 enum lsa_SidType *sid_types;
36 NTSTATUS result;
37 DOM_SID *sids;
39 /* maybe its a raw SID */
40 if (strncmp(name, "S-", 2) == 0 &&
41 string_to_sid(sid, name)) {
42 return NT_STATUS_OK;
45 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
46 SEC_FLAG_MAXIMUM_ALLOWED,
47 &pol);
48 if (!NT_STATUS_IS_OK(result))
49 goto done;
51 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, NULL, 1, &sids, &sid_types);
52 if (!NT_STATUS_IS_OK(result))
53 goto done;
55 rpccli_lsa_Close(cli, mem_ctx, &pol);
57 *sid = sids[0];
59 done:
60 return result;
63 static void display_query_info_1(struct lsa_AuditLogInfo *r)
65 d_printf("percent_full:\t%d\n", r->percent_full);
66 d_printf("maximum_log_size:\t%d\n", r->maximum_log_size);
67 d_printf("retention_time:\t%lld\n", (long long)r->retention_time);
68 d_printf("shutdown_in_progress:\t%d\n", r->shutdown_in_progress);
69 d_printf("time_to_shutdown:\t%lld\n", (long long)r->time_to_shutdown);
70 d_printf("next_audit_record:\t%d\n", r->next_audit_record);
73 static void display_query_info_2(struct lsa_AuditEventsInfo *r)
75 int i;
76 d_printf("Auditing enabled:\t%d\n", r->auditing_mode);
77 d_printf("Auditing categories:\t%d\n", r->count);
78 d_printf("Auditsettings:\n");
79 for (i=0; i<r->count; i++) {
80 const char *val = audit_policy_str(talloc_tos(), r->settings[i]);
81 const char *policy = audit_description_str(i);
82 d_printf("%s:\t%s\n", policy, val);
86 static void display_query_info_3(struct lsa_DomainInfo *r)
88 d_printf("Domain Name: %s\n", r->name.string);
89 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
92 static void display_query_info_5(struct lsa_DomainInfo *r)
94 d_printf("Domain Name: %s\n", r->name.string);
95 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
98 static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
100 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
103 static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
105 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
106 d_printf("Log is full: %d\n", r->log_is_full);
109 static void display_query_info_12(struct lsa_DnsDomainInfo *r)
111 d_printf("Domain NetBios Name: %s\n", r->name.string);
112 d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
113 d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
114 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
115 d_printf("Domain GUID: %s\n", GUID_string(talloc_tos(),
116 &r->domain_guid));
119 static void display_lsa_query_info(union lsa_PolicyInformation *info,
120 enum lsa_PolicyInfo level)
122 switch (level) {
123 case 1:
124 display_query_info_1(&info->audit_log);
125 break;
126 case 2:
127 display_query_info_2(&info->audit_events);
128 break;
129 case 3:
130 display_query_info_3(&info->domain);
131 break;
132 case 5:
133 display_query_info_5(&info->account_domain);
134 break;
135 case 10:
136 display_query_info_10(&info->auditfullset);
137 break;
138 case 11:
139 display_query_info_11(&info->auditfullquery);
140 break;
141 case 12:
142 display_query_info_12(&info->dns);
143 break;
144 default:
145 printf("can't display info level: %d\n", level);
146 break;
150 static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli,
151 TALLOC_CTX *mem_ctx, int argc,
152 const char **argv)
154 struct policy_handle pol;
155 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
156 union lsa_PolicyInformation *info = NULL;
158 uint32 info_class = 3;
160 if (argc > 2) {
161 printf("Usage: %s [info_class]\n", argv[0]);
162 return NT_STATUS_OK;
165 if (argc == 2)
166 info_class = atoi(argv[1]);
168 switch (info_class) {
169 case 12:
170 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
171 SEC_FLAG_MAXIMUM_ALLOWED,
172 &pol);
174 if (!NT_STATUS_IS_OK(result))
175 goto done;
177 result = rpccli_lsa_QueryInfoPolicy2(cli, mem_ctx,
178 &pol,
179 info_class,
180 &info);
181 break;
182 default:
183 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
184 SEC_FLAG_MAXIMUM_ALLOWED,
185 &pol);
187 if (!NT_STATUS_IS_OK(result))
188 goto done;
190 result = rpccli_lsa_QueryInfoPolicy(cli, mem_ctx,
191 &pol,
192 info_class,
193 &info);
196 if (NT_STATUS_IS_OK(result)) {
197 display_lsa_query_info(info, info_class);
200 rpccli_lsa_Close(cli, mem_ctx, &pol);
202 done:
203 return result;
206 /* Resolve a list of names to a list of sids */
208 static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli,
209 TALLOC_CTX *mem_ctx, int argc,
210 const char **argv)
212 struct policy_handle pol;
213 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
214 DOM_SID *sids;
215 enum lsa_SidType *types;
216 int i;
218 if (argc == 1) {
219 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
220 return NT_STATUS_OK;
223 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
224 SEC_FLAG_MAXIMUM_ALLOWED,
225 &pol);
227 if (!NT_STATUS_IS_OK(result))
228 goto done;
230 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
231 (const char**)(argv + 1), NULL, 1, &sids, &types);
233 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
234 NT_STATUS_V(STATUS_SOME_UNMAPPED))
235 goto done;
237 result = NT_STATUS_OK;
239 /* Print results */
241 for (i = 0; i < (argc - 1); i++) {
242 fstring sid_str;
243 sid_to_fstring(sid_str, &sids[i]);
244 printf("%s %s (%s: %d)\n", argv[i + 1], sid_str,
245 sid_type_lookup(types[i]), types[i]);
248 rpccli_lsa_Close(cli, mem_ctx, &pol);
250 done:
251 return result;
254 /* Resolve a list of names to a list of sids */
256 static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli,
257 TALLOC_CTX *mem_ctx, int argc,
258 const char **argv)
260 struct policy_handle pol;
261 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
262 DOM_SID *sids;
263 enum lsa_SidType *types;
264 int i, level;
266 if (argc < 3) {
267 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
268 return NT_STATUS_OK;
271 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
272 SEC_FLAG_MAXIMUM_ALLOWED,
273 &pol);
275 if (!NT_STATUS_IS_OK(result))
276 goto done;
278 level = atoi(argv[1]);
280 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2,
281 (const char**)(argv + 2), NULL, level, &sids, &types);
283 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
284 NT_STATUS_V(STATUS_SOME_UNMAPPED))
285 goto done;
287 result = NT_STATUS_OK;
289 /* Print results */
291 for (i = 0; i < (argc - 2); i++) {
292 fstring sid_str;
293 sid_to_fstring(sid_str, &sids[i]);
294 printf("%s %s (%s: %d)\n", argv[i + 2], sid_str,
295 sid_type_lookup(types[i]), types[i]);
298 rpccli_lsa_Close(cli, mem_ctx, &pol);
300 done:
301 return result;
304 static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
305 TALLOC_CTX *mem_ctx, int argc,
306 const char **argv)
308 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
310 uint32_t num_names;
311 struct lsa_String *names;
312 struct lsa_RefDomainList *domains;
313 struct lsa_TransSidArray3 sids;
314 uint32_t count = 0;
315 int i;
317 if (argc == 1) {
318 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
319 return NT_STATUS_OK;
322 ZERO_STRUCT(sids);
324 num_names = argc-1;
325 names = talloc_array(mem_ctx, struct lsa_String, num_names);
326 NT_STATUS_HAVE_NO_MEMORY(names);
328 for (i=0; i < num_names; i++) {
329 init_lsa_String(&names[i], argv[i+1]);
332 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
333 num_names,
334 names,
335 &domains,
336 &sids,
338 &count,
341 if (!NT_STATUS_IS_OK(result)) {
342 return result;
345 for (i = 0; i < sids.count; i++) {
346 fstring sid_str;
347 sid_to_fstring(sid_str, sids.sids[i].sid);
348 printf("%s %s (%s: %d)\n", argv[i+1], sid_str,
349 sid_type_lookup(sids.sids[i].sid_type),
350 sids.sids[i].sid_type);
353 return result;
356 /* Resolve a list of SIDs to a list of names */
358 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
359 int argc, const char **argv)
361 struct policy_handle pol;
362 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
363 DOM_SID *sids;
364 char **domains;
365 char **names;
366 enum lsa_SidType *types;
367 int i;
369 if (argc == 1) {
370 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
371 return NT_STATUS_OK;
374 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
375 SEC_FLAG_MAXIMUM_ALLOWED,
376 &pol);
378 if (!NT_STATUS_IS_OK(result))
379 goto done;
381 /* Convert arguments to sids */
383 sids = TALLOC_ARRAY(mem_ctx, DOM_SID, argc - 1);
385 if (!sids) {
386 printf("could not allocate memory for %d sids\n", argc - 1);
387 goto done;
390 for (i = 0; i < argc - 1; i++)
391 if (!string_to_sid(&sids[i], argv[i + 1])) {
392 result = NT_STATUS_INVALID_SID;
393 goto done;
396 /* Lookup the SIDs */
398 result = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
399 &domains, &names, &types);
401 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
402 NT_STATUS_V(STATUS_SOME_UNMAPPED))
403 goto done;
405 result = NT_STATUS_OK;
407 /* Print results */
409 for (i = 0; i < (argc - 1); i++) {
410 fstring sid_str;
412 sid_to_fstring(sid_str, &sids[i]);
413 printf("%s %s\\%s (%d)\n", sid_str,
414 domains[i] ? domains[i] : "*unknown*",
415 names[i] ? names[i] : "*unknown*", types[i]);
418 rpccli_lsa_Close(cli, mem_ctx, &pol);
420 done:
421 return result;
424 /* Resolve a list of SIDs to a list of names */
426 static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
427 TALLOC_CTX *mem_ctx,
428 int argc, const char **argv)
430 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
431 int i;
432 struct lsa_SidArray sids;
433 struct lsa_RefDomainList *domains;
434 struct lsa_TransNameArray2 names;
435 uint32_t count = 0;
437 if (argc == 1) {
438 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
439 return NT_STATUS_OK;
442 ZERO_STRUCT(names);
444 /* Convert arguments to sids */
446 sids.num_sids = argc-1;
447 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
448 if (!sids.sids) {
449 printf("could not allocate memory for %d sids\n", sids.num_sids);
450 goto done;
453 for (i = 0; i < sids.num_sids; i++) {
454 sids.sids[i].sid = talloc(sids.sids, struct dom_sid);
455 if (sids.sids[i].sid == NULL) {
456 result = NT_STATUS_NO_MEMORY;
457 goto done;
459 if (!string_to_sid(sids.sids[i].sid, argv[i+1])) {
460 result = NT_STATUS_INVALID_SID;
461 goto done;
465 /* Lookup the SIDs */
466 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
467 &sids,
468 &domains,
469 &names,
471 &count,
475 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
476 NT_STATUS_V(STATUS_SOME_UNMAPPED))
477 goto done;
479 result = NT_STATUS_OK;
481 /* Print results */
483 for (i = 0; i < count; i++) {
484 fstring sid_str;
486 sid_to_fstring(sid_str, sids.sids[i].sid);
487 printf("%s %s (%d)\n", sid_str,
488 names.names[i].name.string,
489 names.names[i].sid_type);
492 done:
493 return result;
497 /* Enumerate list of trusted domains */
499 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
500 TALLOC_CTX *mem_ctx, int argc,
501 const char **argv)
503 struct policy_handle pol;
504 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
505 struct lsa_DomainList domain_list;
507 /* defaults, but may be changed using params */
508 uint32 enum_ctx = 0;
509 int i;
510 uint32_t max_size = (uint32_t)-1;
512 if (argc > 2) {
513 printf("Usage: %s [enum context (0)]\n", argv[0]);
514 return NT_STATUS_OK;
517 if (argc == 2 && argv[1]) {
518 enum_ctx = atoi(argv[2]);
521 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
522 LSA_POLICY_VIEW_LOCAL_INFORMATION,
523 &pol);
525 if (!NT_STATUS_IS_OK(result))
526 goto done;
528 result = STATUS_MORE_ENTRIES;
530 while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
532 /* Lookup list of trusted domains */
534 result = rpccli_lsa_EnumTrustDom(cli, mem_ctx,
535 &pol,
536 &enum_ctx,
537 &domain_list,
538 max_size);
539 if (!NT_STATUS_IS_OK(result) &&
540 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
541 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
542 goto done;
544 /* Print results: list of names and sids returned in this
545 * response. */
546 for (i = 0; i < domain_list.count; i++) {
547 fstring sid_str;
549 sid_to_fstring(sid_str, domain_list.domains[i].sid);
550 printf("%s %s\n",
551 domain_list.domains[i].name.string ?
552 domain_list.domains[i].name.string : "*unknown*",
553 sid_str);
557 rpccli_lsa_Close(cli, mem_ctx, &pol);
558 done:
559 return result;
562 /* Enumerates privileges */
564 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli,
565 TALLOC_CTX *mem_ctx, int argc,
566 const char **argv)
568 struct policy_handle pol;
569 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
570 struct lsa_PrivArray priv_array;
572 uint32 enum_context=0;
573 uint32 pref_max_length=0x1000;
574 int i;
576 if (argc > 3) {
577 printf("Usage: %s [enum context] [max length]\n", argv[0]);
578 return NT_STATUS_OK;
581 if (argc>=2)
582 enum_context=atoi(argv[1]);
584 if (argc==3)
585 pref_max_length=atoi(argv[2]);
587 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
588 SEC_FLAG_MAXIMUM_ALLOWED,
589 &pol);
591 if (!NT_STATUS_IS_OK(result))
592 goto done;
594 result = rpccli_lsa_EnumPrivs(cli, mem_ctx,
595 &pol,
596 &enum_context,
597 &priv_array,
598 pref_max_length);
599 if (!NT_STATUS_IS_OK(result))
600 goto done;
602 /* Print results */
603 printf("found %d privileges\n\n", priv_array.count);
605 for (i = 0; i < priv_array.count; i++) {
606 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
607 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
608 priv_array.privs[i].luid.high,
609 priv_array.privs[i].luid.low,
610 priv_array.privs[i].luid.high,
611 priv_array.privs[i].luid.low);
614 rpccli_lsa_Close(cli, mem_ctx, &pol);
615 done:
616 return result;
619 /* Get privilege name */
621 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli,
622 TALLOC_CTX *mem_ctx, int argc,
623 const char **argv)
625 struct policy_handle pol;
626 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
628 uint16 lang_id=0;
629 uint16 lang_id_sys=0;
630 uint16 lang_id_desc;
631 struct lsa_String lsa_name;
632 struct lsa_StringLarge *description = NULL;
634 if (argc != 2) {
635 printf("Usage: %s privilege name\n", argv[0]);
636 return NT_STATUS_OK;
639 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
640 SEC_FLAG_MAXIMUM_ALLOWED,
641 &pol);
643 if (!NT_STATUS_IS_OK(result))
644 goto done;
646 init_lsa_String(&lsa_name, argv[1]);
648 result = rpccli_lsa_LookupPrivDisplayName(cli, mem_ctx,
649 &pol,
650 &lsa_name,
651 lang_id,
652 lang_id_sys,
653 &description,
654 &lang_id_desc);
656 if (!NT_STATUS_IS_OK(result))
657 goto done;
659 /* Print results */
660 printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
662 rpccli_lsa_Close(cli, mem_ctx, &pol);
663 done:
664 return result;
667 /* Enumerate the LSA SIDS */
669 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli,
670 TALLOC_CTX *mem_ctx, int argc,
671 const char **argv)
673 struct policy_handle pol;
674 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
676 uint32 enum_context=0;
677 uint32 pref_max_length=0x1000;
678 struct lsa_SidArray sid_array;
679 int i;
681 if (argc > 3) {
682 printf("Usage: %s [enum context] [max length]\n", argv[0]);
683 return NT_STATUS_OK;
686 if (argc>=2)
687 enum_context=atoi(argv[1]);
689 if (argc==3)
690 pref_max_length=atoi(argv[2]);
692 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
693 SEC_FLAG_MAXIMUM_ALLOWED,
694 &pol);
696 if (!NT_STATUS_IS_OK(result))
697 goto done;
699 result = rpccli_lsa_EnumAccounts(cli, mem_ctx,
700 &pol,
701 &enum_context,
702 &sid_array,
703 pref_max_length);
705 if (!NT_STATUS_IS_OK(result))
706 goto done;
708 /* Print results */
709 printf("found %d SIDs\n\n", sid_array.num_sids);
711 for (i = 0; i < sid_array.num_sids; i++) {
712 fstring sid_str;
714 sid_to_fstring(sid_str, sid_array.sids[i].sid);
715 printf("%s\n", sid_str);
718 rpccli_lsa_Close(cli, mem_ctx, &pol);
719 done:
720 return result;
723 /* Create a new account */
725 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli,
726 TALLOC_CTX *mem_ctx, int argc,
727 const char **argv)
729 struct policy_handle dom_pol;
730 struct policy_handle user_pol;
731 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
732 uint32 des_access = 0x000f000f;
734 DOM_SID sid;
736 if (argc != 2 ) {
737 printf("Usage: %s SID\n", argv[0]);
738 return NT_STATUS_OK;
741 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
742 if (!NT_STATUS_IS_OK(result))
743 goto done;
745 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
746 SEC_FLAG_MAXIMUM_ALLOWED,
747 &dom_pol);
749 if (!NT_STATUS_IS_OK(result))
750 goto done;
752 result = rpccli_lsa_CreateAccount(cli, mem_ctx,
753 &dom_pol,
754 &sid,
755 des_access,
756 &user_pol);
758 if (!NT_STATUS_IS_OK(result))
759 goto done;
761 printf("Account for SID %s successfully created\n\n", argv[1]);
762 result = NT_STATUS_OK;
764 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
765 done:
766 return result;
770 /* Enumerate the privileges of an SID */
772 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli,
773 TALLOC_CTX *mem_ctx, int argc,
774 const char **argv)
776 struct policy_handle dom_pol;
777 struct policy_handle user_pol;
778 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
779 uint32 access_desired = 0x000f000f;
780 DOM_SID sid;
781 struct lsa_PrivilegeSet *privs = NULL;
782 int i;
784 if (argc != 2 ) {
785 printf("Usage: %s SID\n", argv[0]);
786 return NT_STATUS_OK;
789 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
790 if (!NT_STATUS_IS_OK(result))
791 goto done;
793 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
794 SEC_FLAG_MAXIMUM_ALLOWED,
795 &dom_pol);
797 if (!NT_STATUS_IS_OK(result))
798 goto done;
800 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
801 &dom_pol,
802 &sid,
803 access_desired,
804 &user_pol);
806 if (!NT_STATUS_IS_OK(result))
807 goto done;
809 result = rpccli_lsa_EnumPrivsAccount(cli, mem_ctx,
810 &user_pol,
811 &privs);
813 if (!NT_STATUS_IS_OK(result))
814 goto done;
816 /* Print results */
817 printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
818 printf("high\tlow\tattribute\n");
820 for (i = 0; i < privs->count; i++) {
821 printf("%u\t%u\t%u\n",
822 privs->set[i].luid.high,
823 privs->set[i].luid.low,
824 privs->set[i].attribute);
827 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
828 done:
829 return result;
833 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
835 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli,
836 TALLOC_CTX *mem_ctx, int argc,
837 const char **argv)
839 struct policy_handle dom_pol;
840 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
841 DOM_SID sid;
842 struct lsa_RightSet rights;
844 int i;
846 if (argc != 2 ) {
847 printf("Usage: %s SID\n", argv[0]);
848 return NT_STATUS_OK;
851 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
852 if (!NT_STATUS_IS_OK(result))
853 goto done;
855 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
856 SEC_FLAG_MAXIMUM_ALLOWED,
857 &dom_pol);
859 if (!NT_STATUS_IS_OK(result))
860 goto done;
862 result = rpccli_lsa_EnumAccountRights(cli, mem_ctx,
863 &dom_pol,
864 &sid,
865 &rights);
867 if (!NT_STATUS_IS_OK(result))
868 goto done;
870 printf("found %d privileges for SID %s\n", rights.count,
871 sid_string_tos(&sid));
873 for (i = 0; i < rights.count; i++) {
874 printf("\t%s\n", rights.names[i].string);
877 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
878 done:
879 return result;
883 /* add some privileges to a SID via LsaAddAccountRights */
885 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli,
886 TALLOC_CTX *mem_ctx, int argc,
887 const char **argv)
889 struct policy_handle dom_pol;
890 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
891 struct lsa_RightSet rights;
892 DOM_SID sid;
893 int i;
895 if (argc < 3 ) {
896 printf("Usage: %s SID [rights...]\n", argv[0]);
897 return NT_STATUS_OK;
900 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
901 if (!NT_STATUS_IS_OK(result))
902 goto done;
904 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
905 SEC_FLAG_MAXIMUM_ALLOWED,
906 &dom_pol);
908 if (!NT_STATUS_IS_OK(result))
909 goto done;
911 rights.count = argc-2;
912 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
913 rights.count);
914 if (!rights.names) {
915 return NT_STATUS_NO_MEMORY;
918 for (i=0; i<argc-2; i++) {
919 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
922 result = rpccli_lsa_AddAccountRights(cli, mem_ctx,
923 &dom_pol,
924 &sid,
925 &rights);
927 if (!NT_STATUS_IS_OK(result))
928 goto done;
930 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
931 done:
932 return result;
936 /* remove some privileges to a SID via LsaRemoveAccountRights */
938 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli,
939 TALLOC_CTX *mem_ctx, int argc,
940 const char **argv)
942 struct policy_handle dom_pol;
943 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
944 struct lsa_RightSet rights;
945 DOM_SID sid;
946 int i;
948 if (argc < 3 ) {
949 printf("Usage: %s SID [rights...]\n", argv[0]);
950 return NT_STATUS_OK;
953 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
954 if (!NT_STATUS_IS_OK(result))
955 goto done;
957 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
958 SEC_FLAG_MAXIMUM_ALLOWED,
959 &dom_pol);
961 if (!NT_STATUS_IS_OK(result))
962 goto done;
964 rights.count = argc-2;
965 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
966 rights.count);
967 if (!rights.names) {
968 return NT_STATUS_NO_MEMORY;
971 for (i=0; i<argc-2; i++) {
972 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
975 result = rpccli_lsa_RemoveAccountRights(cli, mem_ctx,
976 &dom_pol,
977 &sid,
978 false,
979 &rights);
981 if (!NT_STATUS_IS_OK(result))
982 goto done;
984 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
986 done:
987 return result;
991 /* Get a privilege value given its name */
993 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli,
994 TALLOC_CTX *mem_ctx, int argc,
995 const char **argv)
997 struct policy_handle pol;
998 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
999 struct lsa_LUID luid;
1000 struct lsa_String name;
1002 if (argc != 2 ) {
1003 printf("Usage: %s name\n", argv[0]);
1004 return NT_STATUS_OK;
1007 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1008 SEC_FLAG_MAXIMUM_ALLOWED,
1009 &pol);
1011 if (!NT_STATUS_IS_OK(result))
1012 goto done;
1014 init_lsa_String(&name, argv[1]);
1016 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1017 &pol,
1018 &name,
1019 &luid);
1021 if (!NT_STATUS_IS_OK(result))
1022 goto done;
1024 /* Print results */
1026 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
1028 rpccli_lsa_Close(cli, mem_ctx, &pol);
1029 done:
1030 return result;
1033 /* Query LSA security object */
1035 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli,
1036 TALLOC_CTX *mem_ctx, int argc,
1037 const char **argv)
1039 struct policy_handle pol;
1040 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1041 SEC_DESC_BUF *sdb;
1042 uint32 sec_info = DACL_SECURITY_INFORMATION;
1044 if (argc < 1 || argc > 2) {
1045 printf("Usage: %s [sec_info]\n", argv[0]);
1046 return NT_STATUS_OK;
1049 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1050 SEC_FLAG_MAXIMUM_ALLOWED,
1051 &pol);
1053 if (argc == 2)
1054 sscanf(argv[1], "%x", &sec_info);
1056 if (!NT_STATUS_IS_OK(result))
1057 goto done;
1059 result = rpccli_lsa_QuerySecurity(cli, mem_ctx,
1060 &pol,
1061 sec_info,
1062 &sdb);
1063 if (!NT_STATUS_IS_OK(result))
1064 goto done;
1066 /* Print results */
1068 display_sec_desc(sdb->sd);
1070 rpccli_lsa_Close(cli, mem_ctx, &pol);
1071 done:
1072 return result;
1075 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
1076 uint8_t session_key[16])
1078 char *pwd, *pwd_old;
1080 DATA_BLOB data = data_blob_const(p->password->data, p->password->length);
1081 DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
1082 DATA_BLOB session_key_blob = data_blob_const(session_key, sizeof(session_key));
1084 pwd = sess_decrypt_string(talloc_tos(), &data, &session_key_blob);
1085 pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key_blob);
1087 d_printf("Password:\t%s\n", pwd);
1088 d_printf("Old Password:\t%s\n", pwd_old);
1090 talloc_free(pwd);
1091 talloc_free(pwd_old);
1094 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
1095 union lsa_TrustedDomainInfo *info,
1096 enum lsa_TrustDomInfoEnum info_class,
1097 uint8_t nt_hash[16])
1099 switch (info_class) {
1100 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
1101 display_trust_dom_info_4(&info->password, nt_hash);
1102 break;
1103 default: {
1104 const char *str = NULL;
1105 str = NDR_PRINT_UNION_STRING(mem_ctx,
1106 lsa_TrustedDomainInfo,
1107 info_class, info);
1108 if (str) {
1109 d_printf("%s\n", str);
1111 break;
1116 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
1117 TALLOC_CTX *mem_ctx, int argc,
1118 const char **argv)
1120 struct policy_handle pol;
1121 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1122 DOM_SID dom_sid;
1123 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1124 union lsa_TrustedDomainInfo *info = NULL;
1125 enum lsa_TrustDomInfoEnum info_class = 1;
1126 uint8_t nt_hash[16];
1128 if (argc > 3 || argc < 2) {
1129 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1130 return NT_STATUS_OK;
1133 if (!string_to_sid(&dom_sid, argv[1]))
1134 return NT_STATUS_NO_MEMORY;
1136 if (argc == 3)
1137 info_class = atoi(argv[2]);
1139 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1141 if (!NT_STATUS_IS_OK(result))
1142 goto done;
1144 result = rpccli_lsa_QueryTrustedDomainInfoBySid(cli, mem_ctx,
1145 &pol,
1146 &dom_sid,
1147 info_class,
1148 &info);
1149 if (!NT_STATUS_IS_OK(result))
1150 goto done;
1152 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1153 d_fprintf(stderr, "Could not get pwd hash\n");
1154 goto done;
1157 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1159 done:
1160 rpccli_lsa_Close(cli, mem_ctx, &pol);
1162 return result;
1165 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1166 TALLOC_CTX *mem_ctx, int argc,
1167 const char **argv)
1169 struct policy_handle pol;
1170 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1171 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1172 union lsa_TrustedDomainInfo *info = NULL;
1173 enum lsa_TrustDomInfoEnum info_class = 1;
1174 struct lsa_String trusted_domain;
1175 uint8_t nt_hash[16];
1177 if (argc > 3 || argc < 2) {
1178 printf("Usage: %s [name] [info_class]\n", argv[0]);
1179 return NT_STATUS_OK;
1182 if (argc == 3)
1183 info_class = atoi(argv[2]);
1185 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1187 if (!NT_STATUS_IS_OK(result))
1188 goto done;
1190 init_lsa_String(&trusted_domain, argv[1]);
1192 result = rpccli_lsa_QueryTrustedDomainInfoByName(cli, mem_ctx,
1193 &pol,
1194 &trusted_domain,
1195 info_class,
1196 &info);
1197 if (!NT_STATUS_IS_OK(result))
1198 goto done;
1200 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1201 d_fprintf(stderr, "Could not get pwd hash\n");
1202 goto done;
1205 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1207 done:
1208 rpccli_lsa_Close(cli, mem_ctx, &pol);
1210 return result;
1213 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1214 TALLOC_CTX *mem_ctx, int argc,
1215 const char **argv)
1217 struct policy_handle pol, trustdom_pol;
1218 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1219 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1220 union lsa_TrustedDomainInfo *info = NULL;
1221 DOM_SID dom_sid;
1222 enum lsa_TrustDomInfoEnum info_class = 1;
1223 uint8_t nt_hash[16];
1225 if (argc > 3 || argc < 2) {
1226 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1227 return NT_STATUS_OK;
1230 if (!string_to_sid(&dom_sid, argv[1]))
1231 return NT_STATUS_NO_MEMORY;
1234 if (argc == 3)
1235 info_class = atoi(argv[2]);
1237 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1239 if (!NT_STATUS_IS_OK(result))
1240 goto done;
1242 result = rpccli_lsa_OpenTrustedDomain(cli, mem_ctx,
1243 &pol,
1244 &dom_sid,
1245 access_mask,
1246 &trustdom_pol);
1248 if (!NT_STATUS_IS_OK(result))
1249 goto done;
1251 result = rpccli_lsa_QueryTrustedDomainInfo(cli, mem_ctx,
1252 &trustdom_pol,
1253 info_class,
1254 &info);
1256 if (!NT_STATUS_IS_OK(result))
1257 goto done;
1259 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1260 d_fprintf(stderr, "Could not get pwd hash\n");
1261 goto done;
1264 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1266 done:
1267 rpccli_lsa_Close(cli, mem_ctx, &pol);
1269 return result;
1272 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1273 TALLOC_CTX *mem_ctx, int argc,
1274 const char **argv)
1276 struct policy_handle pol;
1277 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1278 const char *servername = cli->desthost;
1279 struct lsa_String *account_name = NULL;
1280 struct lsa_String *authority_name = NULL;
1282 if (argc > 2) {
1283 printf("Usage: %s servername\n", argv[0]);
1284 return NT_STATUS_OK;
1287 result = rpccli_lsa_open_policy(cli, mem_ctx, true,
1288 SEC_FLAG_MAXIMUM_ALLOWED,
1289 &pol);
1291 if (!NT_STATUS_IS_OK(result)) {
1292 goto done;
1295 result = rpccli_lsa_GetUserName(cli, mem_ctx,
1296 servername,
1297 &account_name,
1298 &authority_name);
1299 if (!NT_STATUS_IS_OK(result)) {
1300 goto done;
1303 /* Print results */
1305 printf("Account Name: %s, Authority Name: %s\n",
1306 account_name->string, authority_name ? authority_name->string :
1307 "");
1309 rpccli_lsa_Close(cli, mem_ctx, &pol);
1310 done:
1311 return result;
1314 static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
1315 TALLOC_CTX *mem_ctx, int argc,
1316 const char **argv)
1318 struct policy_handle dom_pol, user_pol;
1319 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1320 struct lsa_PrivilegeSet privs;
1321 struct lsa_LUIDAttribute *set = NULL;
1322 DOM_SID sid;
1323 int i;
1325 ZERO_STRUCT(privs);
1327 if (argc < 3 ) {
1328 printf("Usage: %s SID [rights...]\n", argv[0]);
1329 return NT_STATUS_OK;
1332 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1333 if (!NT_STATUS_IS_OK(result)) {
1334 goto done;
1337 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1338 SEC_FLAG_MAXIMUM_ALLOWED,
1339 &dom_pol);
1341 if (!NT_STATUS_IS_OK(result)) {
1342 goto done;
1345 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
1346 &dom_pol,
1347 &sid,
1348 SEC_FLAG_MAXIMUM_ALLOWED,
1349 &user_pol);
1351 if (!NT_STATUS_IS_OK(result)) {
1352 goto done;
1355 for (i=2; i<argc; i++) {
1357 struct lsa_String priv_name;
1358 struct lsa_LUID luid;
1360 init_lsa_String(&priv_name, argv[i]);
1362 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1363 &dom_pol,
1364 &priv_name,
1365 &luid);
1366 if (!NT_STATUS_IS_OK(result)) {
1367 continue;
1370 privs.count++;
1371 set = TALLOC_REALLOC_ARRAY(mem_ctx, set,
1372 struct lsa_LUIDAttribute,
1373 privs.count);
1374 if (!set) {
1375 return NT_STATUS_NO_MEMORY;
1378 set[privs.count-1].luid = luid;
1379 set[privs.count-1].attribute = 0;
1382 privs.set = set;
1384 result = rpccli_lsa_AddPrivilegesToAccount(cli, mem_ctx,
1385 &user_pol,
1386 &privs);
1388 if (!NT_STATUS_IS_OK(result)) {
1389 goto done;
1392 rpccli_lsa_Close(cli, mem_ctx, &user_pol);
1393 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
1394 done:
1395 return result;
1398 static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
1399 TALLOC_CTX *mem_ctx, int argc,
1400 const char **argv)
1402 struct policy_handle dom_pol, user_pol;
1403 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1404 struct lsa_PrivilegeSet privs;
1405 struct lsa_LUIDAttribute *set = NULL;
1406 DOM_SID sid;
1407 int i;
1409 ZERO_STRUCT(privs);
1411 if (argc < 3 ) {
1412 printf("Usage: %s SID [rights...]\n", argv[0]);
1413 return NT_STATUS_OK;
1416 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1417 if (!NT_STATUS_IS_OK(result)) {
1418 goto done;
1421 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1422 SEC_FLAG_MAXIMUM_ALLOWED,
1423 &dom_pol);
1425 if (!NT_STATUS_IS_OK(result)) {
1426 goto done;
1429 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
1430 &dom_pol,
1431 &sid,
1432 SEC_FLAG_MAXIMUM_ALLOWED,
1433 &user_pol);
1435 if (!NT_STATUS_IS_OK(result)) {
1436 goto done;
1439 for (i=2; i<argc; i++) {
1441 struct lsa_String priv_name;
1442 struct lsa_LUID luid;
1444 init_lsa_String(&priv_name, argv[i]);
1446 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1447 &dom_pol,
1448 &priv_name,
1449 &luid);
1450 if (!NT_STATUS_IS_OK(result)) {
1451 continue;
1454 privs.count++;
1455 set = TALLOC_REALLOC_ARRAY(mem_ctx, set,
1456 struct lsa_LUIDAttribute,
1457 privs.count);
1458 if (!set) {
1459 return NT_STATUS_NO_MEMORY;
1462 set[privs.count-1].luid = luid;
1463 set[privs.count-1].attribute = 0;
1466 privs.set = set;
1469 result = rpccli_lsa_RemovePrivilegesFromAccount(cli, mem_ctx,
1470 &user_pol,
1471 false,
1472 &privs);
1474 if (!NT_STATUS_IS_OK(result)) {
1475 goto done;
1478 rpccli_lsa_Close(cli, mem_ctx, &user_pol);
1479 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
1480 done:
1481 return result;
1484 static NTSTATUS cmd_lsa_create_secret(struct rpc_pipe_client *cli,
1485 TALLOC_CTX *mem_ctx, int argc,
1486 const char **argv)
1488 NTSTATUS status;
1489 struct policy_handle handle, sec_handle;
1490 struct lsa_String name;
1492 if (argc < 2) {
1493 printf("Usage: %s name\n", argv[0]);
1494 return NT_STATUS_OK;
1497 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1498 true,
1499 SEC_FLAG_MAXIMUM_ALLOWED,
1500 &handle);
1501 if (!NT_STATUS_IS_OK(status)) {
1502 return status;
1505 init_lsa_String(&name, argv[1]);
1507 status = rpccli_lsa_CreateSecret(cli, mem_ctx,
1508 &handle,
1509 name,
1510 SEC_FLAG_MAXIMUM_ALLOWED,
1511 &sec_handle);
1512 if (!NT_STATUS_IS_OK(status)) {
1513 goto done;
1516 done:
1517 if (is_valid_policy_hnd(&sec_handle)) {
1518 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1520 if (is_valid_policy_hnd(&handle)) {
1521 rpccli_lsa_Close(cli, mem_ctx, &handle);
1524 return status;
1527 static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
1528 TALLOC_CTX *mem_ctx, int argc,
1529 const char **argv)
1531 NTSTATUS status;
1532 struct policy_handle handle, sec_handle;
1533 struct lsa_String name;
1535 if (argc < 2) {
1536 printf("Usage: %s name\n", argv[0]);
1537 return NT_STATUS_OK;
1540 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1541 true,
1542 SEC_FLAG_MAXIMUM_ALLOWED,
1543 &handle);
1544 if (!NT_STATUS_IS_OK(status)) {
1545 return status;
1548 init_lsa_String(&name, argv[1]);
1550 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1551 &handle,
1552 name,
1553 SEC_FLAG_MAXIMUM_ALLOWED,
1554 &sec_handle);
1555 if (!NT_STATUS_IS_OK(status)) {
1556 goto done;
1559 status = rpccli_lsa_DeleteObject(cli, mem_ctx,
1560 &sec_handle);
1561 if (!NT_STATUS_IS_OK(status)) {
1562 goto done;
1565 done:
1566 if (is_valid_policy_hnd(&sec_handle)) {
1567 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1569 if (is_valid_policy_hnd(&handle)) {
1570 rpccli_lsa_Close(cli, mem_ctx, &handle);
1573 return status;
1576 static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
1577 TALLOC_CTX *mem_ctx, int argc,
1578 const char **argv)
1580 NTSTATUS status;
1581 struct policy_handle handle, sec_handle;
1582 struct lsa_String name;
1583 struct lsa_DATA_BUF_PTR new_val;
1584 NTTIME new_mtime = 0;
1585 struct lsa_DATA_BUF_PTR old_val;
1586 NTTIME old_mtime = 0;
1587 DATA_BLOB session_key;
1588 DATA_BLOB new_blob = data_blob_null;
1589 DATA_BLOB old_blob = data_blob_null;
1590 char *new_secret, *old_secret;
1592 if (argc < 2) {
1593 printf("Usage: %s name\n", argv[0]);
1594 return NT_STATUS_OK;
1597 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1598 true,
1599 SEC_FLAG_MAXIMUM_ALLOWED,
1600 &handle);
1601 if (!NT_STATUS_IS_OK(status)) {
1602 return status;
1605 init_lsa_String(&name, argv[1]);
1607 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1608 &handle,
1609 name,
1610 SEC_FLAG_MAXIMUM_ALLOWED,
1611 &sec_handle);
1612 if (!NT_STATUS_IS_OK(status)) {
1613 goto done;
1616 ZERO_STRUCT(new_val);
1617 ZERO_STRUCT(old_val);
1619 status = rpccli_lsa_QuerySecret(cli, mem_ctx,
1620 &sec_handle,
1621 &new_val,
1622 &new_mtime,
1623 &old_val,
1624 &old_mtime);
1625 if (!NT_STATUS_IS_OK(status)) {
1626 goto done;
1629 status = cli_get_session_key(mem_ctx, cli, &session_key);
1630 if (!NT_STATUS_IS_OK(status)) {
1631 goto done;
1634 if (new_val.buf) {
1635 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
1637 if (old_val.buf) {
1638 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
1641 new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
1642 old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
1643 if (new_secret) {
1644 d_printf("new secret: %s\n", new_secret);
1646 if (old_secret) {
1647 d_printf("old secret: %s\n", old_secret);
1650 done:
1651 if (is_valid_policy_hnd(&sec_handle)) {
1652 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1654 if (is_valid_policy_hnd(&handle)) {
1655 rpccli_lsa_Close(cli, mem_ctx, &handle);
1658 return status;
1661 static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
1662 TALLOC_CTX *mem_ctx, int argc,
1663 const char **argv)
1665 NTSTATUS status;
1666 struct policy_handle handle, sec_handle;
1667 struct lsa_String name;
1668 struct lsa_DATA_BUF new_val;
1669 struct lsa_DATA_BUF old_val;
1670 DATA_BLOB enc_key;
1671 DATA_BLOB session_key;
1673 if (argc < 3) {
1674 printf("Usage: %s name secret\n", argv[0]);
1675 return NT_STATUS_OK;
1678 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1679 true,
1680 SEC_FLAG_MAXIMUM_ALLOWED,
1681 &handle);
1682 if (!NT_STATUS_IS_OK(status)) {
1683 return status;
1686 init_lsa_String(&name, argv[1]);
1688 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1689 &handle,
1690 name,
1691 SEC_FLAG_MAXIMUM_ALLOWED,
1692 &sec_handle);
1693 if (!NT_STATUS_IS_OK(status)) {
1694 goto done;
1697 ZERO_STRUCT(new_val);
1698 ZERO_STRUCT(old_val);
1700 status = cli_get_session_key(mem_ctx, cli, &session_key);
1701 if (!NT_STATUS_IS_OK(status)) {
1702 goto done;
1705 enc_key = sess_encrypt_string(argv[2], &session_key);
1707 new_val.length = enc_key.length;
1708 new_val.size = enc_key.length;
1709 new_val.data = enc_key.data;
1711 status = rpccli_lsa_SetSecret(cli, mem_ctx,
1712 &sec_handle,
1713 &new_val,
1714 NULL);
1715 if (!NT_STATUS_IS_OK(status)) {
1716 goto done;
1719 done:
1720 if (is_valid_policy_hnd(&sec_handle)) {
1721 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1723 if (is_valid_policy_hnd(&handle)) {
1724 rpccli_lsa_Close(cli, mem_ctx, &handle);
1727 return status;
1730 static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
1731 TALLOC_CTX *mem_ctx, int argc,
1732 const char **argv)
1734 NTSTATUS status;
1735 struct policy_handle handle;
1736 struct lsa_String name;
1737 struct lsa_DATA_BUF *val;
1738 DATA_BLOB session_key;
1739 DATA_BLOB blob = data_blob_null;
1740 char *secret;
1742 if (argc < 2) {
1743 printf("Usage: %s name\n", argv[0]);
1744 return NT_STATUS_OK;
1747 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1748 true,
1749 SEC_FLAG_MAXIMUM_ALLOWED,
1750 &handle);
1751 if (!NT_STATUS_IS_OK(status)) {
1752 return status;
1755 init_lsa_String(&name, argv[1]);
1757 ZERO_STRUCT(val);
1759 status = rpccli_lsa_RetrievePrivateData(cli, mem_ctx,
1760 &handle,
1761 &name,
1762 &val);
1763 if (!NT_STATUS_IS_OK(status)) {
1764 goto done;
1767 status = cli_get_session_key(mem_ctx, cli, &session_key);
1768 if (!NT_STATUS_IS_OK(status)) {
1769 goto done;
1772 if (val) {
1773 blob = data_blob_const(val->data, val->length);
1776 secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
1777 if (secret) {
1778 d_printf("secret: %s\n", secret);
1781 done:
1782 if (is_valid_policy_hnd(&handle)) {
1783 rpccli_lsa_Close(cli, mem_ctx, &handle);
1786 return status;
1789 static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
1790 TALLOC_CTX *mem_ctx, int argc,
1791 const char **argv)
1793 NTSTATUS status;
1794 struct policy_handle handle;
1795 struct lsa_String name;
1796 struct lsa_DATA_BUF val;
1797 DATA_BLOB session_key;
1798 DATA_BLOB enc_key;
1800 if (argc < 3) {
1801 printf("Usage: %s name secret\n", argv[0]);
1802 return NT_STATUS_OK;
1805 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1806 true,
1807 SEC_FLAG_MAXIMUM_ALLOWED,
1808 &handle);
1809 if (!NT_STATUS_IS_OK(status)) {
1810 return status;
1813 init_lsa_String(&name, argv[1]);
1815 ZERO_STRUCT(val);
1817 status = cli_get_session_key(mem_ctx, cli, &session_key);
1818 if (!NT_STATUS_IS_OK(status)) {
1819 goto done;
1822 enc_key = sess_encrypt_string(argv[2], &session_key);
1824 val.length = enc_key.length;
1825 val.size = enc_key.length;
1826 val.data = enc_key.data;
1828 status = rpccli_lsa_StorePrivateData(cli, mem_ctx,
1829 &handle,
1830 &name,
1831 &val);
1832 if (!NT_STATUS_IS_OK(status)) {
1833 goto done;
1836 done:
1837 if (is_valid_policy_hnd(&handle)) {
1838 rpccli_lsa_Close(cli, mem_ctx, &handle);
1841 return status;
1844 static NTSTATUS cmd_lsa_create_trusted_domain(struct rpc_pipe_client *cli,
1845 TALLOC_CTX *mem_ctx, int argc,
1846 const char **argv)
1848 NTSTATUS status;
1849 struct policy_handle handle, trustdom_handle;
1850 struct dom_sid sid;
1851 struct lsa_DomainInfo info;
1853 if (argc < 3) {
1854 printf("Usage: %s name sid\n", argv[0]);
1855 return NT_STATUS_OK;
1858 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1859 true,
1860 SEC_FLAG_MAXIMUM_ALLOWED,
1861 &handle);
1862 if (!NT_STATUS_IS_OK(status)) {
1863 return status;
1866 init_lsa_StringLarge(&info.name, argv[1]);
1867 info.sid = &sid;
1868 string_to_sid(&sid, argv[2]);
1870 status = rpccli_lsa_CreateTrustedDomain(cli, mem_ctx,
1871 &handle,
1872 &info,
1873 SEC_FLAG_MAXIMUM_ALLOWED,
1874 &trustdom_handle);
1875 if (!NT_STATUS_IS_OK(status)) {
1876 goto done;
1879 done:
1880 if (is_valid_policy_hnd(&trustdom_handle)) {
1881 rpccli_lsa_Close(cli, mem_ctx, &trustdom_handle);
1884 if (is_valid_policy_hnd(&handle)) {
1885 rpccli_lsa_Close(cli, mem_ctx, &handle);
1888 return status;
1891 static NTSTATUS cmd_lsa_delete_trusted_domain(struct rpc_pipe_client *cli,
1892 TALLOC_CTX *mem_ctx, int argc,
1893 const char **argv)
1895 NTSTATUS status;
1896 struct policy_handle handle, trustdom_handle;
1897 struct lsa_String name;
1898 struct dom_sid *sid = NULL;
1900 if (argc < 2) {
1901 printf("Usage: %s name\n", argv[0]);
1902 return NT_STATUS_OK;
1905 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1906 true,
1907 SEC_FLAG_MAXIMUM_ALLOWED,
1908 &handle);
1909 if (!NT_STATUS_IS_OK(status)) {
1910 return status;
1913 init_lsa_String(&name, argv[1]);
1915 status = rpccli_lsa_OpenTrustedDomainByName(cli, mem_ctx,
1916 &handle,
1917 name,
1918 SEC_FLAG_MAXIMUM_ALLOWED,
1919 &trustdom_handle);
1920 if (NT_STATUS_IS_OK(status)) {
1921 goto delete_object;
1925 uint32_t resume_handle = 0;
1926 struct lsa_DomainList domains;
1927 int i;
1929 status = rpccli_lsa_EnumTrustDom(cli, mem_ctx,
1930 &handle,
1931 &resume_handle,
1932 &domains,
1933 0xffff);
1934 if (!NT_STATUS_IS_OK(status)) {
1935 goto done;
1938 for (i=0; i < domains.count; i++) {
1939 if (strequal(domains.domains[i].name.string, argv[1])) {
1940 sid = domains.domains[i].sid;
1941 break;
1945 if (!sid) {
1946 return NT_STATUS_INVALID_SID;
1950 status = rpccli_lsa_OpenTrustedDomain(cli, mem_ctx,
1951 &handle,
1952 sid,
1953 SEC_FLAG_MAXIMUM_ALLOWED,
1954 &trustdom_handle);
1955 if (!NT_STATUS_IS_OK(status)) {
1956 goto done;
1959 delete_object:
1960 status = rpccli_lsa_DeleteObject(cli, mem_ctx,
1961 &trustdom_handle);
1962 if (!NT_STATUS_IS_OK(status)) {
1963 goto done;
1966 done:
1967 if (is_valid_policy_hnd(&trustdom_handle)) {
1968 rpccli_lsa_Close(cli, mem_ctx, &trustdom_handle);
1971 if (is_valid_policy_hnd(&handle)) {
1972 rpccli_lsa_Close(cli, mem_ctx, &handle);
1975 return status;
1979 /* List of commands exported by this module */
1981 struct cmd_set lsarpc_commands[] = {
1983 { "LSARPC" },
1985 { "lsaquery", RPC_RTYPE_NTSTATUS, cmd_lsa_query_info_policy, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query info policy", "" },
1986 { "lookupsids", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert SIDs to names", "" },
1987 { "lookupsids3", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids3, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert SIDs to names", "" },
1988 { "lookupnames", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1989 { "lookupnames4", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names4, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1990 { "lookupnames_level", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names_level, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1991 { "enumtrust", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_trust_dom, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate trusted domains", "Usage: [preferred max number] [enum context (0)]" },
1992 { "enumprivs", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privilege, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate privileges", "" },
1993 { "getdispname", RPC_RTYPE_NTSTATUS, cmd_lsa_get_dispname, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get the privilege name", "" },
1994 { "lsaenumsid", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_sids, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the LSA SIDS", "" },
1995 { "lsacreateaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_create_account, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create a new lsa account", "" },
1996 { "lsaenumprivsaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privsaccounts, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the privileges of an SID", "" },
1997 { "lsaenumacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the rights of an SID", "" },
1998 { "lsaaddpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_add_priv, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Assign a privilege to a SID", "" },
1999 { "lsadelpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_del_priv, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Revoke a privilege from a SID", "" },
2000 { "lsaaddacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_add_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Add rights to an account", "" },
2001 { "lsaremoveacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_remove_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Remove rights from an account", "" },
2002 { "lsalookupprivvalue", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_priv_value, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get a privilege value given its name", "" },
2003 { "lsaquerysecobj", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secobj, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA security object", "" },
2004 { "lsaquerytrustdominfo",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfo, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a SID)", "" },
2005 { "lsaquerytrustdominfobyname",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobyname, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a name), only works for Windows > 2k", "" },
2006 { "lsaquerytrustdominfobysid",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobysid, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a SID)", "" },
2007 { "getusername", RPC_RTYPE_NTSTATUS, cmd_lsa_get_username, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get username", "" },
2008 { "createsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_create_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create Secret", "" },
2009 { "deletesecret", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Delete Secret", "" },
2010 { "querysecret", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query Secret", "" },
2011 { "setsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_set_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Set Secret", "" },
2012 { "retrieveprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_retrieve_private_data, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Retrieve Private Data", "" },
2013 { "storeprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_store_private_data, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Store Private Data", "" },
2014 { "createtrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_create_trusted_domain, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create Trusted Domain", "" },
2015 { "deletetrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_trusted_domain, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Delete Trusted Domain", "" },
2017 { NULL }