s4:rootdse LDB module - remove unused variable
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_lsarpc.c
blobf55400d134e4101dcc982ddc3b97cb6d32818fbc
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/cli_lsa.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, const char *name)
38 struct policy_handle pol;
39 enum lsa_SidType *sid_types;
40 NTSTATUS result;
41 struct dom_sid *sids;
43 /* maybe its a raw SID */
44 if (strncmp(name, "S-", 2) == 0 &&
45 string_to_sid(sid, name)) {
46 return NT_STATUS_OK;
49 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
50 SEC_FLAG_MAXIMUM_ALLOWED,
51 &pol);
52 if (!NT_STATUS_IS_OK(result))
53 goto done;
55 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, NULL, 1, &sids, &sid_types);
56 if (!NT_STATUS_IS_OK(result))
57 goto done;
59 rpccli_lsa_Close(cli, mem_ctx, &pol);
61 *sid = sids[0];
63 done:
64 return result;
67 static void display_query_info_1(struct lsa_AuditLogInfo *r)
69 d_printf("percent_full:\t%d\n", r->percent_full);
70 d_printf("maximum_log_size:\t%d\n", r->maximum_log_size);
71 d_printf("retention_time:\t%lld\n", (long long)r->retention_time);
72 d_printf("shutdown_in_progress:\t%d\n", r->shutdown_in_progress);
73 d_printf("time_to_shutdown:\t%lld\n", (long long)r->time_to_shutdown);
74 d_printf("next_audit_record:\t%d\n", r->next_audit_record);
77 static void display_query_info_2(struct lsa_AuditEventsInfo *r)
79 int i;
80 d_printf("Auditing enabled:\t%d\n", r->auditing_mode);
81 d_printf("Auditing categories:\t%d\n", r->count);
82 d_printf("Auditsettings:\n");
83 for (i=0; i<r->count; i++) {
84 const char *val = audit_policy_str(talloc_tos(), r->settings[i]);
85 const char *policy = audit_description_str(i);
86 d_printf("%s:\t%s\n", policy, val);
90 static void display_query_info_3(struct lsa_DomainInfo *r)
92 d_printf("Domain Name: %s\n", r->name.string);
93 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
96 static void display_query_info_5(struct lsa_DomainInfo *r)
98 d_printf("Domain Name: %s\n", r->name.string);
99 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
102 static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
104 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
107 static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
109 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
110 d_printf("Log is full: %d\n", r->log_is_full);
113 static void display_query_info_12(struct lsa_DnsDomainInfo *r)
115 d_printf("Domain NetBios Name: %s\n", r->name.string);
116 d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
117 d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
118 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
119 d_printf("Domain GUID: %s\n", GUID_string(talloc_tos(),
120 &r->domain_guid));
123 static void display_lsa_query_info(union lsa_PolicyInformation *info,
124 enum lsa_PolicyInfo level)
126 switch (level) {
127 case 1:
128 display_query_info_1(&info->audit_log);
129 break;
130 case 2:
131 display_query_info_2(&info->audit_events);
132 break;
133 case 3:
134 display_query_info_3(&info->domain);
135 break;
136 case 5:
137 display_query_info_5(&info->account_domain);
138 break;
139 case 10:
140 display_query_info_10(&info->auditfullset);
141 break;
142 case 11:
143 display_query_info_11(&info->auditfullquery);
144 break;
145 case 12:
146 display_query_info_12(&info->dns);
147 break;
148 default:
149 printf("can't display info level: %d\n", level);
150 break;
154 static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli,
155 TALLOC_CTX *mem_ctx, int argc,
156 const char **argv)
158 struct policy_handle pol;
159 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
160 union lsa_PolicyInformation *info = NULL;
162 uint32 info_class = 3;
164 if (argc > 2) {
165 printf("Usage: %s [info_class]\n", argv[0]);
166 return NT_STATUS_OK;
169 if (argc == 2)
170 info_class = atoi(argv[1]);
172 switch (info_class) {
173 case 12:
174 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
175 SEC_FLAG_MAXIMUM_ALLOWED,
176 &pol);
178 if (!NT_STATUS_IS_OK(result))
179 goto done;
181 result = rpccli_lsa_QueryInfoPolicy2(cli, mem_ctx,
182 &pol,
183 info_class,
184 &info);
185 break;
186 default:
187 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
188 SEC_FLAG_MAXIMUM_ALLOWED,
189 &pol);
191 if (!NT_STATUS_IS_OK(result))
192 goto done;
194 result = rpccli_lsa_QueryInfoPolicy(cli, mem_ctx,
195 &pol,
196 info_class,
197 &info);
200 if (NT_STATUS_IS_OK(result)) {
201 display_lsa_query_info(info, info_class);
204 rpccli_lsa_Close(cli, mem_ctx, &pol);
206 done:
207 return result;
210 /* Resolve a list of names to a list of sids */
212 static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli,
213 TALLOC_CTX *mem_ctx, int argc,
214 const char **argv)
216 struct policy_handle pol;
217 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
218 struct dom_sid *sids;
219 enum lsa_SidType *types;
220 int i;
222 if (argc == 1) {
223 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
224 return NT_STATUS_OK;
227 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
228 SEC_FLAG_MAXIMUM_ALLOWED,
229 &pol);
231 if (!NT_STATUS_IS_OK(result))
232 goto done;
234 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
235 (const char**)(argv + 1), NULL, 1, &sids, &types);
237 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
238 NT_STATUS_V(STATUS_SOME_UNMAPPED))
239 goto done;
241 result = NT_STATUS_OK;
243 /* Print results */
245 for (i = 0; i < (argc - 1); i++) {
246 fstring sid_str;
247 sid_to_fstring(sid_str, &sids[i]);
248 printf("%s %s (%s: %d)\n", argv[i + 1], sid_str,
249 sid_type_lookup(types[i]), types[i]);
252 rpccli_lsa_Close(cli, mem_ctx, &pol);
254 done:
255 return result;
258 /* Resolve a list of names to a list of sids */
260 static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli,
261 TALLOC_CTX *mem_ctx, int argc,
262 const char **argv)
264 struct policy_handle pol;
265 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
266 struct dom_sid *sids;
267 enum lsa_SidType *types;
268 int i, level;
270 if (argc < 3) {
271 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
272 return NT_STATUS_OK;
275 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
276 SEC_FLAG_MAXIMUM_ALLOWED,
277 &pol);
279 if (!NT_STATUS_IS_OK(result))
280 goto done;
282 level = atoi(argv[1]);
284 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2,
285 (const char**)(argv + 2), NULL, level, &sids, &types);
287 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
288 NT_STATUS_V(STATUS_SOME_UNMAPPED))
289 goto done;
291 result = NT_STATUS_OK;
293 /* Print results */
295 for (i = 0; i < (argc - 2); i++) {
296 fstring sid_str;
297 sid_to_fstring(sid_str, &sids[i]);
298 printf("%s %s (%s: %d)\n", argv[i + 2], sid_str,
299 sid_type_lookup(types[i]), types[i]);
302 rpccli_lsa_Close(cli, mem_ctx, &pol);
304 done:
305 return result;
308 static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
309 TALLOC_CTX *mem_ctx, int argc,
310 const char **argv)
312 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
314 uint32_t num_names;
315 struct lsa_String *names;
316 struct lsa_RefDomainList *domains;
317 struct lsa_TransSidArray3 sids;
318 uint32_t count = 0;
319 int i;
321 if (argc == 1) {
322 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
323 return NT_STATUS_OK;
326 ZERO_STRUCT(sids);
328 num_names = argc-1;
329 names = talloc_array(mem_ctx, struct lsa_String, num_names);
330 NT_STATUS_HAVE_NO_MEMORY(names);
332 for (i=0; i < num_names; i++) {
333 init_lsa_String(&names[i], argv[i+1]);
336 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
337 num_names,
338 names,
339 &domains,
340 &sids,
342 &count,
345 if (!NT_STATUS_IS_OK(result)) {
346 return result;
349 for (i = 0; i < sids.count; i++) {
350 fstring sid_str;
351 sid_to_fstring(sid_str, sids.sids[i].sid);
352 printf("%s %s (%s: %d)\n", argv[i+1], sid_str,
353 sid_type_lookup(sids.sids[i].sid_type),
354 sids.sids[i].sid_type);
357 return result;
360 /* Resolve a list of SIDs to a list of names */
362 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
363 int argc, const char **argv)
365 struct policy_handle pol;
366 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
367 struct dom_sid *sids;
368 char **domains;
369 char **names;
370 enum lsa_SidType *types;
371 int i;
373 if (argc == 1) {
374 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
375 return NT_STATUS_OK;
378 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
379 SEC_FLAG_MAXIMUM_ALLOWED,
380 &pol);
382 if (!NT_STATUS_IS_OK(result))
383 goto done;
385 /* Convert arguments to sids */
387 sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, argc - 1);
389 if (!sids) {
390 printf("could not allocate memory for %d sids\n", argc - 1);
391 goto done;
394 for (i = 0; i < argc - 1; i++)
395 if (!string_to_sid(&sids[i], argv[i + 1])) {
396 result = NT_STATUS_INVALID_SID;
397 goto done;
400 /* Lookup the SIDs */
402 result = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
403 &domains, &names, &types);
405 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
406 NT_STATUS_V(STATUS_SOME_UNMAPPED))
407 goto done;
409 result = NT_STATUS_OK;
411 /* Print results */
413 for (i = 0; i < (argc - 1); i++) {
414 fstring sid_str;
416 sid_to_fstring(sid_str, &sids[i]);
417 printf("%s %s\\%s (%d)\n", sid_str,
418 domains[i] ? domains[i] : "*unknown*",
419 names[i] ? names[i] : "*unknown*", types[i]);
422 rpccli_lsa_Close(cli, mem_ctx, &pol);
424 done:
425 return result;
428 /* Resolve a list of SIDs to a list of names */
430 static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
431 TALLOC_CTX *mem_ctx,
432 int argc, const char **argv)
434 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
435 int i;
436 struct lsa_SidArray sids;
437 struct lsa_RefDomainList *domains;
438 struct lsa_TransNameArray2 names;
439 uint32_t count = 0;
441 if (argc == 1) {
442 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
443 return NT_STATUS_OK;
446 ZERO_STRUCT(names);
448 /* Convert arguments to sids */
450 sids.num_sids = argc-1;
451 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
452 if (!sids.sids) {
453 printf("could not allocate memory for %d sids\n", sids.num_sids);
454 goto done;
457 for (i = 0; i < sids.num_sids; i++) {
458 sids.sids[i].sid = talloc(sids.sids, struct dom_sid);
459 if (sids.sids[i].sid == NULL) {
460 result = NT_STATUS_NO_MEMORY;
461 goto done;
463 if (!string_to_sid(sids.sids[i].sid, argv[i+1])) {
464 result = NT_STATUS_INVALID_SID;
465 goto done;
469 /* Lookup the SIDs */
470 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
471 &sids,
472 &domains,
473 &names,
475 &count,
479 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
480 NT_STATUS_V(STATUS_SOME_UNMAPPED))
481 goto done;
483 result = NT_STATUS_OK;
485 /* Print results */
487 for (i = 0; i < count; i++) {
488 fstring sid_str;
490 sid_to_fstring(sid_str, sids.sids[i].sid);
491 printf("%s %s (%d)\n", sid_str,
492 names.names[i].name.string,
493 names.names[i].sid_type);
496 done:
497 return result;
501 /* Enumerate list of trusted domains */
503 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
504 TALLOC_CTX *mem_ctx, int argc,
505 const char **argv)
507 struct policy_handle pol;
508 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
509 struct lsa_DomainList domain_list;
511 /* defaults, but may be changed using params */
512 uint32 enum_ctx = 0;
513 int i;
514 uint32_t max_size = (uint32_t)-1;
516 if (argc > 2) {
517 printf("Usage: %s [enum context (0)]\n", argv[0]);
518 return NT_STATUS_OK;
521 if (argc == 2 && argv[1]) {
522 enum_ctx = atoi(argv[2]);
525 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
526 LSA_POLICY_VIEW_LOCAL_INFORMATION,
527 &pol);
529 if (!NT_STATUS_IS_OK(result))
530 goto done;
532 result = STATUS_MORE_ENTRIES;
534 while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
536 /* Lookup list of trusted domains */
538 result = rpccli_lsa_EnumTrustDom(cli, mem_ctx,
539 &pol,
540 &enum_ctx,
541 &domain_list,
542 max_size);
543 if (!NT_STATUS_IS_OK(result) &&
544 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
545 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
546 goto done;
548 /* Print results: list of names and sids returned in this
549 * response. */
550 for (i = 0; i < domain_list.count; i++) {
551 fstring sid_str;
553 sid_to_fstring(sid_str, domain_list.domains[i].sid);
554 printf("%s %s\n",
555 domain_list.domains[i].name.string ?
556 domain_list.domains[i].name.string : "*unknown*",
557 sid_str);
561 rpccli_lsa_Close(cli, mem_ctx, &pol);
562 done:
563 return result;
566 /* Enumerates privileges */
568 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli,
569 TALLOC_CTX *mem_ctx, int argc,
570 const char **argv)
572 struct policy_handle pol;
573 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
574 struct lsa_PrivArray priv_array;
576 uint32 enum_context=0;
577 uint32 pref_max_length=0x1000;
578 int i;
580 if (argc > 3) {
581 printf("Usage: %s [enum context] [max length]\n", argv[0]);
582 return NT_STATUS_OK;
585 if (argc>=2)
586 enum_context=atoi(argv[1]);
588 if (argc==3)
589 pref_max_length=atoi(argv[2]);
591 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
592 SEC_FLAG_MAXIMUM_ALLOWED,
593 &pol);
595 if (!NT_STATUS_IS_OK(result))
596 goto done;
598 result = rpccli_lsa_EnumPrivs(cli, mem_ctx,
599 &pol,
600 &enum_context,
601 &priv_array,
602 pref_max_length);
603 if (!NT_STATUS_IS_OK(result))
604 goto done;
606 /* Print results */
607 printf("found %d privileges\n\n", priv_array.count);
609 for (i = 0; i < priv_array.count; i++) {
610 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
611 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
612 priv_array.privs[i].luid.high,
613 priv_array.privs[i].luid.low,
614 priv_array.privs[i].luid.high,
615 priv_array.privs[i].luid.low);
618 rpccli_lsa_Close(cli, mem_ctx, &pol);
619 done:
620 return result;
623 /* Get privilege name */
625 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli,
626 TALLOC_CTX *mem_ctx, int argc,
627 const char **argv)
629 struct policy_handle pol;
630 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
632 uint16 lang_id=0;
633 uint16 lang_id_sys=0;
634 uint16 lang_id_desc;
635 struct lsa_String lsa_name;
636 struct lsa_StringLarge *description = NULL;
638 if (argc != 2) {
639 printf("Usage: %s privilege name\n", argv[0]);
640 return NT_STATUS_OK;
643 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
644 SEC_FLAG_MAXIMUM_ALLOWED,
645 &pol);
647 if (!NT_STATUS_IS_OK(result))
648 goto done;
650 init_lsa_String(&lsa_name, argv[1]);
652 result = rpccli_lsa_LookupPrivDisplayName(cli, mem_ctx,
653 &pol,
654 &lsa_name,
655 lang_id,
656 lang_id_sys,
657 &description,
658 &lang_id_desc);
660 if (!NT_STATUS_IS_OK(result))
661 goto done;
663 /* Print results */
664 printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
666 rpccli_lsa_Close(cli, mem_ctx, &pol);
667 done:
668 return result;
671 /* Enumerate the LSA SIDS */
673 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli,
674 TALLOC_CTX *mem_ctx, int argc,
675 const char **argv)
677 struct policy_handle pol;
678 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
680 uint32 enum_context=0;
681 uint32 pref_max_length=0x1000;
682 struct lsa_SidArray sid_array;
683 int i;
685 if (argc > 3) {
686 printf("Usage: %s [enum context] [max length]\n", argv[0]);
687 return NT_STATUS_OK;
690 if (argc>=2)
691 enum_context=atoi(argv[1]);
693 if (argc==3)
694 pref_max_length=atoi(argv[2]);
696 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
697 SEC_FLAG_MAXIMUM_ALLOWED,
698 &pol);
700 if (!NT_STATUS_IS_OK(result))
701 goto done;
703 result = rpccli_lsa_EnumAccounts(cli, mem_ctx,
704 &pol,
705 &enum_context,
706 &sid_array,
707 pref_max_length);
709 if (!NT_STATUS_IS_OK(result))
710 goto done;
712 /* Print results */
713 printf("found %d SIDs\n\n", sid_array.num_sids);
715 for (i = 0; i < sid_array.num_sids; i++) {
716 fstring sid_str;
718 sid_to_fstring(sid_str, sid_array.sids[i].sid);
719 printf("%s\n", sid_str);
722 rpccli_lsa_Close(cli, mem_ctx, &pol);
723 done:
724 return result;
727 /* Create a new account */
729 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli,
730 TALLOC_CTX *mem_ctx, int argc,
731 const char **argv)
733 struct policy_handle dom_pol;
734 struct policy_handle user_pol;
735 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
736 uint32 des_access = 0x000f000f;
738 struct dom_sid sid;
740 if (argc != 2 ) {
741 printf("Usage: %s SID\n", argv[0]);
742 return NT_STATUS_OK;
745 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
746 if (!NT_STATUS_IS_OK(result))
747 goto done;
749 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
750 SEC_FLAG_MAXIMUM_ALLOWED,
751 &dom_pol);
753 if (!NT_STATUS_IS_OK(result))
754 goto done;
756 result = rpccli_lsa_CreateAccount(cli, mem_ctx,
757 &dom_pol,
758 &sid,
759 des_access,
760 &user_pol);
762 if (!NT_STATUS_IS_OK(result))
763 goto done;
765 printf("Account for SID %s successfully created\n\n", argv[1]);
766 result = NT_STATUS_OK;
768 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
769 done:
770 return result;
774 /* Enumerate the privileges of an SID */
776 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli,
777 TALLOC_CTX *mem_ctx, int argc,
778 const char **argv)
780 struct policy_handle dom_pol;
781 struct policy_handle user_pol;
782 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
783 uint32 access_desired = 0x000f000f;
784 struct dom_sid sid;
785 struct lsa_PrivilegeSet *privs = NULL;
786 int i;
788 if (argc != 2 ) {
789 printf("Usage: %s SID\n", argv[0]);
790 return NT_STATUS_OK;
793 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
794 if (!NT_STATUS_IS_OK(result))
795 goto done;
797 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
798 SEC_FLAG_MAXIMUM_ALLOWED,
799 &dom_pol);
801 if (!NT_STATUS_IS_OK(result))
802 goto done;
804 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
805 &dom_pol,
806 &sid,
807 access_desired,
808 &user_pol);
810 if (!NT_STATUS_IS_OK(result))
811 goto done;
813 result = rpccli_lsa_EnumPrivsAccount(cli, mem_ctx,
814 &user_pol,
815 &privs);
817 if (!NT_STATUS_IS_OK(result))
818 goto done;
820 /* Print results */
821 printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
822 printf("high\tlow\tattribute\n");
824 for (i = 0; i < privs->count; i++) {
825 printf("%u\t%u\t%u\n",
826 privs->set[i].luid.high,
827 privs->set[i].luid.low,
828 privs->set[i].attribute);
831 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
832 done:
833 return result;
837 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
839 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli,
840 TALLOC_CTX *mem_ctx, int argc,
841 const char **argv)
843 struct policy_handle dom_pol;
844 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
845 struct dom_sid sid;
846 struct lsa_RightSet rights;
848 int i;
850 if (argc != 2 ) {
851 printf("Usage: %s SID\n", argv[0]);
852 return NT_STATUS_OK;
855 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
856 if (!NT_STATUS_IS_OK(result))
857 goto done;
859 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
860 SEC_FLAG_MAXIMUM_ALLOWED,
861 &dom_pol);
863 if (!NT_STATUS_IS_OK(result))
864 goto done;
866 result = rpccli_lsa_EnumAccountRights(cli, mem_ctx,
867 &dom_pol,
868 &sid,
869 &rights);
871 if (!NT_STATUS_IS_OK(result))
872 goto done;
874 printf("found %d privileges for SID %s\n", rights.count,
875 sid_string_tos(&sid));
877 for (i = 0; i < rights.count; i++) {
878 printf("\t%s\n", rights.names[i].string);
881 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
882 done:
883 return result;
887 /* add some privileges to a SID via LsaAddAccountRights */
889 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli,
890 TALLOC_CTX *mem_ctx, int argc,
891 const char **argv)
893 struct policy_handle dom_pol;
894 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
895 struct lsa_RightSet rights;
896 struct dom_sid sid;
897 int i;
899 if (argc < 3 ) {
900 printf("Usage: %s SID [rights...]\n", argv[0]);
901 return NT_STATUS_OK;
904 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
905 if (!NT_STATUS_IS_OK(result))
906 goto done;
908 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
909 SEC_FLAG_MAXIMUM_ALLOWED,
910 &dom_pol);
912 if (!NT_STATUS_IS_OK(result))
913 goto done;
915 rights.count = argc-2;
916 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
917 rights.count);
918 if (!rights.names) {
919 return NT_STATUS_NO_MEMORY;
922 for (i=0; i<argc-2; i++) {
923 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
926 result = rpccli_lsa_AddAccountRights(cli, mem_ctx,
927 &dom_pol,
928 &sid,
929 &rights);
931 if (!NT_STATUS_IS_OK(result))
932 goto done;
934 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
935 done:
936 return result;
940 /* remove some privileges to a SID via LsaRemoveAccountRights */
942 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli,
943 TALLOC_CTX *mem_ctx, int argc,
944 const char **argv)
946 struct policy_handle dom_pol;
947 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
948 struct lsa_RightSet rights;
949 struct dom_sid sid;
950 int i;
952 if (argc < 3 ) {
953 printf("Usage: %s SID [rights...]\n", argv[0]);
954 return NT_STATUS_OK;
957 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
958 if (!NT_STATUS_IS_OK(result))
959 goto done;
961 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
962 SEC_FLAG_MAXIMUM_ALLOWED,
963 &dom_pol);
965 if (!NT_STATUS_IS_OK(result))
966 goto done;
968 rights.count = argc-2;
969 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
970 rights.count);
971 if (!rights.names) {
972 return NT_STATUS_NO_MEMORY;
975 for (i=0; i<argc-2; i++) {
976 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
979 result = rpccli_lsa_RemoveAccountRights(cli, mem_ctx,
980 &dom_pol,
981 &sid,
982 false,
983 &rights);
985 if (!NT_STATUS_IS_OK(result))
986 goto done;
988 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
990 done:
991 return result;
995 /* Get a privilege value given its name */
997 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli,
998 TALLOC_CTX *mem_ctx, int argc,
999 const char **argv)
1001 struct policy_handle pol;
1002 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1003 struct lsa_LUID luid;
1004 struct lsa_String name;
1006 if (argc != 2 ) {
1007 printf("Usage: %s name\n", argv[0]);
1008 return NT_STATUS_OK;
1011 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1012 SEC_FLAG_MAXIMUM_ALLOWED,
1013 &pol);
1015 if (!NT_STATUS_IS_OK(result))
1016 goto done;
1018 init_lsa_String(&name, argv[1]);
1020 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1021 &pol,
1022 &name,
1023 &luid);
1025 if (!NT_STATUS_IS_OK(result))
1026 goto done;
1028 /* Print results */
1030 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
1032 rpccli_lsa_Close(cli, mem_ctx, &pol);
1033 done:
1034 return result;
1037 /* Query LSA security object */
1039 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli,
1040 TALLOC_CTX *mem_ctx, int argc,
1041 const char **argv)
1043 struct policy_handle pol;
1044 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1045 struct sec_desc_buf *sdb;
1046 uint32 sec_info = SECINFO_DACL;
1048 if (argc < 1 || argc > 2) {
1049 printf("Usage: %s [sec_info]\n", argv[0]);
1050 return NT_STATUS_OK;
1053 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1054 SEC_FLAG_MAXIMUM_ALLOWED,
1055 &pol);
1057 if (argc == 2)
1058 sscanf(argv[1], "%x", &sec_info);
1060 if (!NT_STATUS_IS_OK(result))
1061 goto done;
1063 result = rpccli_lsa_QuerySecurity(cli, mem_ctx,
1064 &pol,
1065 sec_info,
1066 &sdb);
1067 if (!NT_STATUS_IS_OK(result))
1068 goto done;
1070 /* Print results */
1072 display_sec_desc(sdb->sd);
1074 rpccli_lsa_Close(cli, mem_ctx, &pol);
1075 done:
1076 return result;
1079 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
1080 uint8_t session_key[16])
1082 char *pwd, *pwd_old;
1084 DATA_BLOB data = data_blob_const(p->password->data, p->password->length);
1085 DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
1086 DATA_BLOB session_key_blob = data_blob_const(session_key, sizeof(session_key));
1088 pwd = sess_decrypt_string(talloc_tos(), &data, &session_key_blob);
1089 pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key_blob);
1091 d_printf("Password:\t%s\n", pwd);
1092 d_printf("Old Password:\t%s\n", pwd_old);
1094 talloc_free(pwd);
1095 talloc_free(pwd_old);
1098 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
1099 union lsa_TrustedDomainInfo *info,
1100 enum lsa_TrustDomInfoEnum info_class,
1101 uint8_t nt_hash[16])
1103 switch (info_class) {
1104 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
1105 display_trust_dom_info_4(&info->password, nt_hash);
1106 break;
1107 default: {
1108 const char *str = NULL;
1109 str = NDR_PRINT_UNION_STRING(mem_ctx,
1110 lsa_TrustedDomainInfo,
1111 info_class, info);
1112 if (str) {
1113 d_printf("%s\n", str);
1115 break;
1120 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
1121 TALLOC_CTX *mem_ctx, int argc,
1122 const char **argv)
1124 struct policy_handle pol;
1125 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1126 struct dom_sid dom_sid;
1127 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1128 union lsa_TrustedDomainInfo *info = NULL;
1129 enum lsa_TrustDomInfoEnum info_class = 1;
1130 uint8_t nt_hash[16];
1132 if (argc > 3 || argc < 2) {
1133 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1134 return NT_STATUS_OK;
1137 if (!string_to_sid(&dom_sid, argv[1]))
1138 return NT_STATUS_NO_MEMORY;
1140 if (argc == 3)
1141 info_class = atoi(argv[2]);
1143 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1145 if (!NT_STATUS_IS_OK(result))
1146 goto done;
1148 result = rpccli_lsa_QueryTrustedDomainInfoBySid(cli, mem_ctx,
1149 &pol,
1150 &dom_sid,
1151 info_class,
1152 &info);
1153 if (!NT_STATUS_IS_OK(result))
1154 goto done;
1156 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1157 d_fprintf(stderr, "Could not get pwd hash\n");
1158 goto done;
1161 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1163 done:
1164 rpccli_lsa_Close(cli, mem_ctx, &pol);
1166 return result;
1169 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1170 TALLOC_CTX *mem_ctx, int argc,
1171 const char **argv)
1173 struct policy_handle pol;
1174 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1175 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1176 union lsa_TrustedDomainInfo *info = NULL;
1177 enum lsa_TrustDomInfoEnum info_class = 1;
1178 struct lsa_String trusted_domain;
1179 uint8_t nt_hash[16];
1181 if (argc > 3 || argc < 2) {
1182 printf("Usage: %s [name] [info_class]\n", argv[0]);
1183 return NT_STATUS_OK;
1186 if (argc == 3)
1187 info_class = atoi(argv[2]);
1189 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1191 if (!NT_STATUS_IS_OK(result))
1192 goto done;
1194 init_lsa_String(&trusted_domain, argv[1]);
1196 result = rpccli_lsa_QueryTrustedDomainInfoByName(cli, mem_ctx,
1197 &pol,
1198 &trusted_domain,
1199 info_class,
1200 &info);
1201 if (!NT_STATUS_IS_OK(result))
1202 goto done;
1204 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1205 d_fprintf(stderr, "Could not get pwd hash\n");
1206 goto done;
1209 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1211 done:
1212 rpccli_lsa_Close(cli, mem_ctx, &pol);
1214 return result;
1217 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1218 TALLOC_CTX *mem_ctx, int argc,
1219 const char **argv)
1221 struct policy_handle pol, trustdom_pol;
1222 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1223 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1224 union lsa_TrustedDomainInfo *info = NULL;
1225 struct dom_sid dom_sid;
1226 enum lsa_TrustDomInfoEnum info_class = 1;
1227 uint8_t nt_hash[16];
1229 if (argc > 3 || argc < 2) {
1230 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1231 return NT_STATUS_OK;
1234 if (!string_to_sid(&dom_sid, argv[1]))
1235 return NT_STATUS_NO_MEMORY;
1238 if (argc == 3)
1239 info_class = atoi(argv[2]);
1241 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1243 if (!NT_STATUS_IS_OK(result))
1244 goto done;
1246 result = rpccli_lsa_OpenTrustedDomain(cli, mem_ctx,
1247 &pol,
1248 &dom_sid,
1249 access_mask,
1250 &trustdom_pol);
1252 if (!NT_STATUS_IS_OK(result))
1253 goto done;
1255 result = rpccli_lsa_QueryTrustedDomainInfo(cli, mem_ctx,
1256 &trustdom_pol,
1257 info_class,
1258 &info);
1260 if (!NT_STATUS_IS_OK(result))
1261 goto done;
1263 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1264 d_fprintf(stderr, "Could not get pwd hash\n");
1265 goto done;
1268 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1270 done:
1271 rpccli_lsa_Close(cli, mem_ctx, &pol);
1273 return result;
1276 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1277 TALLOC_CTX *mem_ctx, int argc,
1278 const char **argv)
1280 struct policy_handle pol;
1281 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1282 const char *servername = cli->desthost;
1283 struct lsa_String *account_name = NULL;
1284 struct lsa_String *authority_name = NULL;
1286 if (argc > 2) {
1287 printf("Usage: %s servername\n", argv[0]);
1288 return NT_STATUS_OK;
1291 result = rpccli_lsa_open_policy(cli, mem_ctx, true,
1292 SEC_FLAG_MAXIMUM_ALLOWED,
1293 &pol);
1295 if (!NT_STATUS_IS_OK(result)) {
1296 goto done;
1299 result = rpccli_lsa_GetUserName(cli, mem_ctx,
1300 servername,
1301 &account_name,
1302 &authority_name);
1303 if (!NT_STATUS_IS_OK(result)) {
1304 goto done;
1307 /* Print results */
1309 printf("Account Name: %s, Authority Name: %s\n",
1310 account_name->string, authority_name ? authority_name->string :
1311 "");
1313 rpccli_lsa_Close(cli, mem_ctx, &pol);
1314 done:
1315 return result;
1318 static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
1319 TALLOC_CTX *mem_ctx, int argc,
1320 const char **argv)
1322 struct policy_handle dom_pol, user_pol;
1323 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1324 struct lsa_PrivilegeSet privs;
1325 struct lsa_LUIDAttribute *set = NULL;
1326 struct dom_sid sid;
1327 int i;
1329 ZERO_STRUCT(privs);
1331 if (argc < 3 ) {
1332 printf("Usage: %s SID [rights...]\n", argv[0]);
1333 return NT_STATUS_OK;
1336 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1337 if (!NT_STATUS_IS_OK(result)) {
1338 goto done;
1341 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1342 SEC_FLAG_MAXIMUM_ALLOWED,
1343 &dom_pol);
1345 if (!NT_STATUS_IS_OK(result)) {
1346 goto done;
1349 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
1350 &dom_pol,
1351 &sid,
1352 SEC_FLAG_MAXIMUM_ALLOWED,
1353 &user_pol);
1355 if (!NT_STATUS_IS_OK(result)) {
1356 goto done;
1359 for (i=2; i<argc; i++) {
1361 struct lsa_String priv_name;
1362 struct lsa_LUID luid;
1364 init_lsa_String(&priv_name, argv[i]);
1366 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1367 &dom_pol,
1368 &priv_name,
1369 &luid);
1370 if (!NT_STATUS_IS_OK(result)) {
1371 continue;
1374 privs.count++;
1375 set = TALLOC_REALLOC_ARRAY(mem_ctx, set,
1376 struct lsa_LUIDAttribute,
1377 privs.count);
1378 if (!set) {
1379 return NT_STATUS_NO_MEMORY;
1382 set[privs.count-1].luid = luid;
1383 set[privs.count-1].attribute = 0;
1386 privs.set = set;
1388 result = rpccli_lsa_AddPrivilegesToAccount(cli, mem_ctx,
1389 &user_pol,
1390 &privs);
1392 if (!NT_STATUS_IS_OK(result)) {
1393 goto done;
1396 rpccli_lsa_Close(cli, mem_ctx, &user_pol);
1397 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
1398 done:
1399 return result;
1402 static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
1403 TALLOC_CTX *mem_ctx, int argc,
1404 const char **argv)
1406 struct policy_handle dom_pol, user_pol;
1407 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1408 struct lsa_PrivilegeSet privs;
1409 struct lsa_LUIDAttribute *set = NULL;
1410 struct dom_sid sid;
1411 int i;
1413 ZERO_STRUCT(privs);
1415 if (argc < 3 ) {
1416 printf("Usage: %s SID [rights...]\n", argv[0]);
1417 return NT_STATUS_OK;
1420 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1421 if (!NT_STATUS_IS_OK(result)) {
1422 goto done;
1425 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1426 SEC_FLAG_MAXIMUM_ALLOWED,
1427 &dom_pol);
1429 if (!NT_STATUS_IS_OK(result)) {
1430 goto done;
1433 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
1434 &dom_pol,
1435 &sid,
1436 SEC_FLAG_MAXIMUM_ALLOWED,
1437 &user_pol);
1439 if (!NT_STATUS_IS_OK(result)) {
1440 goto done;
1443 for (i=2; i<argc; i++) {
1445 struct lsa_String priv_name;
1446 struct lsa_LUID luid;
1448 init_lsa_String(&priv_name, argv[i]);
1450 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1451 &dom_pol,
1452 &priv_name,
1453 &luid);
1454 if (!NT_STATUS_IS_OK(result)) {
1455 continue;
1458 privs.count++;
1459 set = TALLOC_REALLOC_ARRAY(mem_ctx, set,
1460 struct lsa_LUIDAttribute,
1461 privs.count);
1462 if (!set) {
1463 return NT_STATUS_NO_MEMORY;
1466 set[privs.count-1].luid = luid;
1467 set[privs.count-1].attribute = 0;
1470 privs.set = set;
1473 result = rpccli_lsa_RemovePrivilegesFromAccount(cli, mem_ctx,
1474 &user_pol,
1475 false,
1476 &privs);
1478 if (!NT_STATUS_IS_OK(result)) {
1479 goto done;
1482 rpccli_lsa_Close(cli, mem_ctx, &user_pol);
1483 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
1484 done:
1485 return result;
1488 static NTSTATUS cmd_lsa_create_secret(struct rpc_pipe_client *cli,
1489 TALLOC_CTX *mem_ctx, int argc,
1490 const char **argv)
1492 NTSTATUS status;
1493 struct policy_handle handle, sec_handle;
1494 struct lsa_String name;
1496 if (argc < 2) {
1497 printf("Usage: %s name\n", argv[0]);
1498 return NT_STATUS_OK;
1501 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1502 true,
1503 SEC_FLAG_MAXIMUM_ALLOWED,
1504 &handle);
1505 if (!NT_STATUS_IS_OK(status)) {
1506 return status;
1509 init_lsa_String(&name, argv[1]);
1511 status = rpccli_lsa_CreateSecret(cli, mem_ctx,
1512 &handle,
1513 name,
1514 SEC_FLAG_MAXIMUM_ALLOWED,
1515 &sec_handle);
1516 if (!NT_STATUS_IS_OK(status)) {
1517 goto done;
1520 done:
1521 if (is_valid_policy_hnd(&sec_handle)) {
1522 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1524 if (is_valid_policy_hnd(&handle)) {
1525 rpccli_lsa_Close(cli, mem_ctx, &handle);
1528 return status;
1531 static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
1532 TALLOC_CTX *mem_ctx, int argc,
1533 const char **argv)
1535 NTSTATUS status;
1536 struct policy_handle handle, sec_handle;
1537 struct lsa_String name;
1539 if (argc < 2) {
1540 printf("Usage: %s name\n", argv[0]);
1541 return NT_STATUS_OK;
1544 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1545 true,
1546 SEC_FLAG_MAXIMUM_ALLOWED,
1547 &handle);
1548 if (!NT_STATUS_IS_OK(status)) {
1549 return status;
1552 init_lsa_String(&name, argv[1]);
1554 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1555 &handle,
1556 name,
1557 SEC_FLAG_MAXIMUM_ALLOWED,
1558 &sec_handle);
1559 if (!NT_STATUS_IS_OK(status)) {
1560 goto done;
1563 status = rpccli_lsa_DeleteObject(cli, mem_ctx,
1564 &sec_handle);
1565 if (!NT_STATUS_IS_OK(status)) {
1566 goto done;
1569 done:
1570 if (is_valid_policy_hnd(&sec_handle)) {
1571 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1573 if (is_valid_policy_hnd(&handle)) {
1574 rpccli_lsa_Close(cli, mem_ctx, &handle);
1577 return status;
1580 static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
1581 TALLOC_CTX *mem_ctx, int argc,
1582 const char **argv)
1584 NTSTATUS status;
1585 struct policy_handle handle, sec_handle;
1586 struct lsa_String name;
1587 struct lsa_DATA_BUF_PTR new_val;
1588 NTTIME new_mtime = 0;
1589 struct lsa_DATA_BUF_PTR old_val;
1590 NTTIME old_mtime = 0;
1591 DATA_BLOB session_key;
1592 DATA_BLOB new_blob = data_blob_null;
1593 DATA_BLOB old_blob = data_blob_null;
1594 char *new_secret, *old_secret;
1596 if (argc < 2) {
1597 printf("Usage: %s name\n", argv[0]);
1598 return NT_STATUS_OK;
1601 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1602 true,
1603 SEC_FLAG_MAXIMUM_ALLOWED,
1604 &handle);
1605 if (!NT_STATUS_IS_OK(status)) {
1606 return status;
1609 init_lsa_String(&name, argv[1]);
1611 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1612 &handle,
1613 name,
1614 SEC_FLAG_MAXIMUM_ALLOWED,
1615 &sec_handle);
1616 if (!NT_STATUS_IS_OK(status)) {
1617 goto done;
1620 ZERO_STRUCT(new_val);
1621 ZERO_STRUCT(old_val);
1623 status = rpccli_lsa_QuerySecret(cli, mem_ctx,
1624 &sec_handle,
1625 &new_val,
1626 &new_mtime,
1627 &old_val,
1628 &old_mtime);
1629 if (!NT_STATUS_IS_OK(status)) {
1630 goto done;
1633 status = cli_get_session_key(mem_ctx, cli, &session_key);
1634 if (!NT_STATUS_IS_OK(status)) {
1635 goto done;
1638 if (new_val.buf) {
1639 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
1641 if (old_val.buf) {
1642 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
1645 new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
1646 old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
1647 if (new_secret) {
1648 d_printf("new secret: %s\n", new_secret);
1650 if (old_secret) {
1651 d_printf("old secret: %s\n", old_secret);
1654 done:
1655 if (is_valid_policy_hnd(&sec_handle)) {
1656 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1658 if (is_valid_policy_hnd(&handle)) {
1659 rpccli_lsa_Close(cli, mem_ctx, &handle);
1662 return status;
1665 static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
1666 TALLOC_CTX *mem_ctx, int argc,
1667 const char **argv)
1669 NTSTATUS status;
1670 struct policy_handle handle, sec_handle;
1671 struct lsa_String name;
1672 struct lsa_DATA_BUF new_val;
1673 struct lsa_DATA_BUF old_val;
1674 DATA_BLOB enc_key;
1675 DATA_BLOB session_key;
1677 if (argc < 3) {
1678 printf("Usage: %s name secret\n", argv[0]);
1679 return NT_STATUS_OK;
1682 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1683 true,
1684 SEC_FLAG_MAXIMUM_ALLOWED,
1685 &handle);
1686 if (!NT_STATUS_IS_OK(status)) {
1687 return status;
1690 init_lsa_String(&name, argv[1]);
1692 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1693 &handle,
1694 name,
1695 SEC_FLAG_MAXIMUM_ALLOWED,
1696 &sec_handle);
1697 if (!NT_STATUS_IS_OK(status)) {
1698 goto done;
1701 ZERO_STRUCT(new_val);
1702 ZERO_STRUCT(old_val);
1704 status = cli_get_session_key(mem_ctx, cli, &session_key);
1705 if (!NT_STATUS_IS_OK(status)) {
1706 goto done;
1709 enc_key = sess_encrypt_string(argv[2], &session_key);
1711 new_val.length = enc_key.length;
1712 new_val.size = enc_key.length;
1713 new_val.data = enc_key.data;
1715 status = rpccli_lsa_SetSecret(cli, mem_ctx,
1716 &sec_handle,
1717 &new_val,
1718 NULL);
1719 if (!NT_STATUS_IS_OK(status)) {
1720 goto done;
1723 done:
1724 if (is_valid_policy_hnd(&sec_handle)) {
1725 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1727 if (is_valid_policy_hnd(&handle)) {
1728 rpccli_lsa_Close(cli, mem_ctx, &handle);
1731 return status;
1734 static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
1735 TALLOC_CTX *mem_ctx, int argc,
1736 const char **argv)
1738 NTSTATUS status;
1739 struct policy_handle handle;
1740 struct lsa_String name;
1741 struct lsa_DATA_BUF *val;
1742 DATA_BLOB session_key;
1743 DATA_BLOB blob = data_blob_null;
1744 char *secret;
1746 if (argc < 2) {
1747 printf("Usage: %s name\n", argv[0]);
1748 return NT_STATUS_OK;
1751 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1752 true,
1753 SEC_FLAG_MAXIMUM_ALLOWED,
1754 &handle);
1755 if (!NT_STATUS_IS_OK(status)) {
1756 return status;
1759 init_lsa_String(&name, argv[1]);
1761 ZERO_STRUCT(val);
1763 status = rpccli_lsa_RetrievePrivateData(cli, mem_ctx,
1764 &handle,
1765 &name,
1766 &val);
1767 if (!NT_STATUS_IS_OK(status)) {
1768 goto done;
1771 status = cli_get_session_key(mem_ctx, cli, &session_key);
1772 if (!NT_STATUS_IS_OK(status)) {
1773 goto done;
1776 if (val) {
1777 blob = data_blob_const(val->data, val->length);
1780 secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
1781 if (secret) {
1782 d_printf("secret: %s\n", secret);
1785 done:
1786 if (is_valid_policy_hnd(&handle)) {
1787 rpccli_lsa_Close(cli, mem_ctx, &handle);
1790 return status;
1793 static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
1794 TALLOC_CTX *mem_ctx, int argc,
1795 const char **argv)
1797 NTSTATUS status;
1798 struct policy_handle handle;
1799 struct lsa_String name;
1800 struct lsa_DATA_BUF val;
1801 DATA_BLOB session_key;
1802 DATA_BLOB enc_key;
1804 if (argc < 3) {
1805 printf("Usage: %s name secret\n", argv[0]);
1806 return NT_STATUS_OK;
1809 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1810 true,
1811 SEC_FLAG_MAXIMUM_ALLOWED,
1812 &handle);
1813 if (!NT_STATUS_IS_OK(status)) {
1814 return status;
1817 init_lsa_String(&name, argv[1]);
1819 ZERO_STRUCT(val);
1821 status = cli_get_session_key(mem_ctx, cli, &session_key);
1822 if (!NT_STATUS_IS_OK(status)) {
1823 goto done;
1826 enc_key = sess_encrypt_string(argv[2], &session_key);
1828 val.length = enc_key.length;
1829 val.size = enc_key.length;
1830 val.data = enc_key.data;
1832 status = rpccli_lsa_StorePrivateData(cli, mem_ctx,
1833 &handle,
1834 &name,
1835 &val);
1836 if (!NT_STATUS_IS_OK(status)) {
1837 goto done;
1840 done:
1841 if (is_valid_policy_hnd(&handle)) {
1842 rpccli_lsa_Close(cli, mem_ctx, &handle);
1845 return status;
1848 static NTSTATUS cmd_lsa_create_trusted_domain(struct rpc_pipe_client *cli,
1849 TALLOC_CTX *mem_ctx, int argc,
1850 const char **argv)
1852 NTSTATUS status;
1853 struct policy_handle handle, trustdom_handle;
1854 struct dom_sid sid;
1855 struct lsa_DomainInfo info;
1857 if (argc < 3) {
1858 printf("Usage: %s name sid\n", argv[0]);
1859 return NT_STATUS_OK;
1862 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1863 true,
1864 SEC_FLAG_MAXIMUM_ALLOWED,
1865 &handle);
1866 if (!NT_STATUS_IS_OK(status)) {
1867 return status;
1870 init_lsa_StringLarge(&info.name, argv[1]);
1871 info.sid = &sid;
1872 string_to_sid(&sid, argv[2]);
1874 status = rpccli_lsa_CreateTrustedDomain(cli, mem_ctx,
1875 &handle,
1876 &info,
1877 SEC_FLAG_MAXIMUM_ALLOWED,
1878 &trustdom_handle);
1879 if (!NT_STATUS_IS_OK(status)) {
1880 goto done;
1883 done:
1884 if (is_valid_policy_hnd(&trustdom_handle)) {
1885 rpccli_lsa_Close(cli, mem_ctx, &trustdom_handle);
1888 if (is_valid_policy_hnd(&handle)) {
1889 rpccli_lsa_Close(cli, mem_ctx, &handle);
1892 return status;
1895 static NTSTATUS cmd_lsa_delete_trusted_domain(struct rpc_pipe_client *cli,
1896 TALLOC_CTX *mem_ctx, int argc,
1897 const char **argv)
1899 NTSTATUS status;
1900 struct policy_handle handle, trustdom_handle;
1901 struct lsa_String name;
1902 struct dom_sid *sid = NULL;
1904 if (argc < 2) {
1905 printf("Usage: %s name\n", argv[0]);
1906 return NT_STATUS_OK;
1909 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1910 true,
1911 SEC_FLAG_MAXIMUM_ALLOWED,
1912 &handle);
1913 if (!NT_STATUS_IS_OK(status)) {
1914 return status;
1917 init_lsa_String(&name, argv[1]);
1919 status = rpccli_lsa_OpenTrustedDomainByName(cli, mem_ctx,
1920 &handle,
1921 name,
1922 SEC_FLAG_MAXIMUM_ALLOWED,
1923 &trustdom_handle);
1924 if (NT_STATUS_IS_OK(status)) {
1925 goto delete_object;
1929 uint32_t resume_handle = 0;
1930 struct lsa_DomainList domains;
1931 int i;
1933 status = rpccli_lsa_EnumTrustDom(cli, mem_ctx,
1934 &handle,
1935 &resume_handle,
1936 &domains,
1937 0xffff);
1938 if (!NT_STATUS_IS_OK(status)) {
1939 goto done;
1942 for (i=0; i < domains.count; i++) {
1943 if (strequal(domains.domains[i].name.string, argv[1])) {
1944 sid = domains.domains[i].sid;
1945 break;
1949 if (!sid) {
1950 return NT_STATUS_INVALID_SID;
1954 status = rpccli_lsa_OpenTrustedDomain(cli, mem_ctx,
1955 &handle,
1956 sid,
1957 SEC_FLAG_MAXIMUM_ALLOWED,
1958 &trustdom_handle);
1959 if (!NT_STATUS_IS_OK(status)) {
1960 goto done;
1963 delete_object:
1964 status = rpccli_lsa_DeleteObject(cli, mem_ctx,
1965 &trustdom_handle);
1966 if (!NT_STATUS_IS_OK(status)) {
1967 goto done;
1970 done:
1971 if (is_valid_policy_hnd(&trustdom_handle)) {
1972 rpccli_lsa_Close(cli, mem_ctx, &trustdom_handle);
1975 if (is_valid_policy_hnd(&handle)) {
1976 rpccli_lsa_Close(cli, mem_ctx, &handle);
1979 return status;
1983 /* List of commands exported by this module */
1985 struct cmd_set lsarpc_commands[] = {
1987 { "LSARPC" },
1989 { "lsaquery", RPC_RTYPE_NTSTATUS, cmd_lsa_query_info_policy, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query info policy", "" },
1990 { "lookupsids", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert SIDs to names", "" },
1991 { "lookupsids3", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids3, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert SIDs to names", "" },
1992 { "lookupnames", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1993 { "lookupnames4", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names4, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1994 { "lookupnames_level", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names_level, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1995 { "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)]" },
1996 { "enumprivs", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privilege, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate privileges", "" },
1997 { "getdispname", RPC_RTYPE_NTSTATUS, cmd_lsa_get_dispname, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get the privilege name", "" },
1998 { "lsaenumsid", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_sids, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the LSA SIDS", "" },
1999 { "lsacreateaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_create_account, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create a new lsa account", "" },
2000 { "lsaenumprivsaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privsaccounts, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the privileges of an SID", "" },
2001 { "lsaenumacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the rights of an SID", "" },
2002 { "lsaaddpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_add_priv, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Assign a privilege to a SID", "" },
2003 { "lsadelpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_del_priv, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Revoke a privilege from a SID", "" },
2004 { "lsaaddacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_add_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Add rights to an account", "" },
2005 { "lsaremoveacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_remove_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Remove rights from an account", "" },
2006 { "lsalookupprivvalue", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_priv_value, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get a privilege value given its name", "" },
2007 { "lsaquerysecobj", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secobj, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA security object", "" },
2008 { "lsaquerytrustdominfo",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfo, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a SID)", "" },
2009 { "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", "" },
2010 { "lsaquerytrustdominfobysid",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobysid, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a SID)", "" },
2011 { "getusername", RPC_RTYPE_NTSTATUS, cmd_lsa_get_username, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get username", "" },
2012 { "createsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_create_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create Secret", "" },
2013 { "deletesecret", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Delete Secret", "" },
2014 { "querysecret", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query Secret", "" },
2015 { "setsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_set_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Set Secret", "" },
2016 { "retrieveprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_retrieve_private_data, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Retrieve Private Data", "" },
2017 { "storeprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_store_private_data, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Store Private Data", "" },
2018 { "createtrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_create_trusted_domain, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create Trusted Domain", "" },
2019 { "deletetrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_trusted_domain, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Delete Trusted Domain", "" },
2021 { NULL }