s3-waf: Add check for dirent.d_off member
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_lsarpc.c
blobda01e6ec94788363bcecd0001cb7c4bd06462e0b
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"
27 #include "rpc_client/cli_lsarpc.h"
29 /* useful function to allow entering a name instead of a SID and
30 * looking it up automatically */
31 static NTSTATUS name_to_sid(struct rpc_pipe_client *cli,
32 TALLOC_CTX *mem_ctx,
33 struct dom_sid *sid, const char *name)
35 struct policy_handle pol;
36 enum lsa_SidType *sid_types;
37 NTSTATUS result;
38 struct dom_sid *sids;
40 /* maybe its a raw SID */
41 if (strncmp(name, "S-", 2) == 0 &&
42 string_to_sid(sid, name)) {
43 return NT_STATUS_OK;
46 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
47 SEC_FLAG_MAXIMUM_ALLOWED,
48 &pol);
49 if (!NT_STATUS_IS_OK(result))
50 goto done;
52 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, NULL, 1, &sids, &sid_types);
53 if (!NT_STATUS_IS_OK(result))
54 goto done;
56 rpccli_lsa_Close(cli, mem_ctx, &pol);
58 *sid = sids[0];
60 done:
61 return result;
64 static void display_query_info_1(struct lsa_AuditLogInfo *r)
66 d_printf("percent_full:\t%d\n", r->percent_full);
67 d_printf("maximum_log_size:\t%d\n", r->maximum_log_size);
68 d_printf("retention_time:\t%lld\n", (long long)r->retention_time);
69 d_printf("shutdown_in_progress:\t%d\n", r->shutdown_in_progress);
70 d_printf("time_to_shutdown:\t%lld\n", (long long)r->time_to_shutdown);
71 d_printf("next_audit_record:\t%d\n", r->next_audit_record);
74 static void display_query_info_2(struct lsa_AuditEventsInfo *r)
76 int i;
77 d_printf("Auditing enabled:\t%d\n", r->auditing_mode);
78 d_printf("Auditing categories:\t%d\n", r->count);
79 d_printf("Auditsettings:\n");
80 for (i=0; i<r->count; i++) {
81 const char *val = audit_policy_str(talloc_tos(), r->settings[i]);
82 const char *policy = audit_description_str(i);
83 d_printf("%s:\t%s\n", policy, val);
87 static void display_query_info_3(struct lsa_DomainInfo *r)
89 d_printf("Domain Name: %s\n", r->name.string);
90 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
93 static void display_query_info_5(struct lsa_DomainInfo *r)
95 d_printf("Domain Name: %s\n", r->name.string);
96 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
99 static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
101 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
104 static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
106 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
107 d_printf("Log is full: %d\n", r->log_is_full);
110 static void display_query_info_12(struct lsa_DnsDomainInfo *r)
112 d_printf("Domain NetBios Name: %s\n", r->name.string);
113 d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
114 d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
115 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
116 d_printf("Domain GUID: %s\n", GUID_string(talloc_tos(),
117 &r->domain_guid));
120 static void display_lsa_query_info(union lsa_PolicyInformation *info,
121 enum lsa_PolicyInfo level)
123 switch (level) {
124 case 1:
125 display_query_info_1(&info->audit_log);
126 break;
127 case 2:
128 display_query_info_2(&info->audit_events);
129 break;
130 case 3:
131 display_query_info_3(&info->domain);
132 break;
133 case 5:
134 display_query_info_5(&info->account_domain);
135 break;
136 case 10:
137 display_query_info_10(&info->auditfullset);
138 break;
139 case 11:
140 display_query_info_11(&info->auditfullquery);
141 break;
142 case 12:
143 display_query_info_12(&info->dns);
144 break;
145 default:
146 printf("can't display info level: %d\n", level);
147 break;
151 static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli,
152 TALLOC_CTX *mem_ctx, int argc,
153 const char **argv)
155 struct policy_handle pol;
156 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
157 union lsa_PolicyInformation *info = NULL;
159 uint32 info_class = 3;
161 if (argc > 2) {
162 printf("Usage: %s [info_class]\n", argv[0]);
163 return NT_STATUS_OK;
166 if (argc == 2)
167 info_class = atoi(argv[1]);
169 switch (info_class) {
170 case 12:
171 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
172 SEC_FLAG_MAXIMUM_ALLOWED,
173 &pol);
175 if (!NT_STATUS_IS_OK(result))
176 goto done;
178 result = rpccli_lsa_QueryInfoPolicy2(cli, mem_ctx,
179 &pol,
180 info_class,
181 &info);
182 break;
183 default:
184 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
185 SEC_FLAG_MAXIMUM_ALLOWED,
186 &pol);
188 if (!NT_STATUS_IS_OK(result))
189 goto done;
191 result = rpccli_lsa_QueryInfoPolicy(cli, mem_ctx,
192 &pol,
193 info_class,
194 &info);
197 if (NT_STATUS_IS_OK(result)) {
198 display_lsa_query_info(info, info_class);
201 rpccli_lsa_Close(cli, mem_ctx, &pol);
203 done:
204 return result;
207 /* Resolve a list of names to a list of sids */
209 static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli,
210 TALLOC_CTX *mem_ctx, int argc,
211 const char **argv)
213 struct policy_handle pol;
214 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
215 struct dom_sid *sids;
216 enum lsa_SidType *types;
217 int i;
219 if (argc == 1) {
220 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
221 return NT_STATUS_OK;
224 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
225 SEC_FLAG_MAXIMUM_ALLOWED,
226 &pol);
228 if (!NT_STATUS_IS_OK(result))
229 goto done;
231 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
232 (const char**)(argv + 1), NULL, 1, &sids, &types);
234 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
235 NT_STATUS_V(STATUS_SOME_UNMAPPED))
236 goto done;
238 result = NT_STATUS_OK;
240 /* Print results */
242 for (i = 0; i < (argc - 1); i++) {
243 fstring sid_str;
244 sid_to_fstring(sid_str, &sids[i]);
245 printf("%s %s (%s: %d)\n", argv[i + 1], sid_str,
246 sid_type_lookup(types[i]), types[i]);
249 rpccli_lsa_Close(cli, mem_ctx, &pol);
251 done:
252 return result;
255 /* Resolve a list of names to a list of sids */
257 static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli,
258 TALLOC_CTX *mem_ctx, int argc,
259 const char **argv)
261 struct policy_handle pol;
262 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
263 struct dom_sid *sids;
264 enum lsa_SidType *types;
265 int i, level;
267 if (argc < 3) {
268 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
269 return NT_STATUS_OK;
272 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
273 SEC_FLAG_MAXIMUM_ALLOWED,
274 &pol);
276 if (!NT_STATUS_IS_OK(result))
277 goto done;
279 level = atoi(argv[1]);
281 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2,
282 (const char**)(argv + 2), NULL, level, &sids, &types);
284 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
285 NT_STATUS_V(STATUS_SOME_UNMAPPED))
286 goto done;
288 result = NT_STATUS_OK;
290 /* Print results */
292 for (i = 0; i < (argc - 2); i++) {
293 fstring sid_str;
294 sid_to_fstring(sid_str, &sids[i]);
295 printf("%s %s (%s: %d)\n", argv[i + 2], sid_str,
296 sid_type_lookup(types[i]), types[i]);
299 rpccli_lsa_Close(cli, mem_ctx, &pol);
301 done:
302 return result;
305 static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
306 TALLOC_CTX *mem_ctx, int argc,
307 const char **argv)
309 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
311 uint32_t num_names;
312 struct lsa_String *names;
313 struct lsa_RefDomainList *domains;
314 struct lsa_TransSidArray3 sids;
315 uint32_t count = 0;
316 int i;
318 if (argc == 1) {
319 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
320 return NT_STATUS_OK;
323 ZERO_STRUCT(sids);
325 num_names = argc-1;
326 names = talloc_array(mem_ctx, struct lsa_String, num_names);
327 NT_STATUS_HAVE_NO_MEMORY(names);
329 for (i=0; i < num_names; i++) {
330 init_lsa_String(&names[i], argv[i+1]);
333 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
334 num_names,
335 names,
336 &domains,
337 &sids,
339 &count,
342 if (!NT_STATUS_IS_OK(result)) {
343 return result;
346 for (i = 0; i < sids.count; i++) {
347 fstring sid_str;
348 sid_to_fstring(sid_str, sids.sids[i].sid);
349 printf("%s %s (%s: %d)\n", argv[i+1], sid_str,
350 sid_type_lookup(sids.sids[i].sid_type),
351 sids.sids[i].sid_type);
354 return result;
357 /* Resolve a list of SIDs to a list of names */
359 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
360 int argc, const char **argv)
362 struct policy_handle pol;
363 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
364 struct dom_sid *sids;
365 char **domains;
366 char **names;
367 enum lsa_SidType *types;
368 int i;
370 if (argc == 1) {
371 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
372 return NT_STATUS_OK;
375 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
376 SEC_FLAG_MAXIMUM_ALLOWED,
377 &pol);
379 if (!NT_STATUS_IS_OK(result))
380 goto done;
382 /* Convert arguments to sids */
384 sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, argc - 1);
386 if (!sids) {
387 printf("could not allocate memory for %d sids\n", argc - 1);
388 goto done;
391 for (i = 0; i < argc - 1; i++)
392 if (!string_to_sid(&sids[i], argv[i + 1])) {
393 result = NT_STATUS_INVALID_SID;
394 goto done;
397 /* Lookup the SIDs */
399 result = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
400 &domains, &names, &types);
402 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
403 NT_STATUS_V(STATUS_SOME_UNMAPPED))
404 goto done;
406 result = NT_STATUS_OK;
408 /* Print results */
410 for (i = 0; i < (argc - 1); i++) {
411 fstring sid_str;
413 sid_to_fstring(sid_str, &sids[i]);
414 printf("%s %s\\%s (%d)\n", sid_str,
415 domains[i] ? domains[i] : "*unknown*",
416 names[i] ? names[i] : "*unknown*", types[i]);
419 rpccli_lsa_Close(cli, mem_ctx, &pol);
421 done:
422 return result;
425 /* Resolve a list of SIDs to a list of names */
427 static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
428 TALLOC_CTX *mem_ctx,
429 int argc, const char **argv)
431 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
432 int i;
433 struct lsa_SidArray sids;
434 struct lsa_RefDomainList *domains;
435 struct lsa_TransNameArray2 names;
436 uint32_t count = 0;
438 if (argc == 1) {
439 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
440 return NT_STATUS_OK;
443 ZERO_STRUCT(names);
445 /* Convert arguments to sids */
447 sids.num_sids = argc-1;
448 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
449 if (!sids.sids) {
450 printf("could not allocate memory for %d sids\n", sids.num_sids);
451 goto done;
454 for (i = 0; i < sids.num_sids; i++) {
455 sids.sids[i].sid = talloc(sids.sids, struct dom_sid);
456 if (sids.sids[i].sid == NULL) {
457 result = NT_STATUS_NO_MEMORY;
458 goto done;
460 if (!string_to_sid(sids.sids[i].sid, argv[i+1])) {
461 result = NT_STATUS_INVALID_SID;
462 goto done;
466 /* Lookup the SIDs */
467 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
468 &sids,
469 &domains,
470 &names,
472 &count,
476 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
477 NT_STATUS_V(STATUS_SOME_UNMAPPED))
478 goto done;
480 result = NT_STATUS_OK;
482 /* Print results */
484 for (i = 0; i < count; i++) {
485 fstring sid_str;
487 sid_to_fstring(sid_str, sids.sids[i].sid);
488 printf("%s %s (%d)\n", sid_str,
489 names.names[i].name.string,
490 names.names[i].sid_type);
493 done:
494 return result;
498 /* Enumerate list of trusted domains */
500 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
501 TALLOC_CTX *mem_ctx, int argc,
502 const char **argv)
504 struct policy_handle pol;
505 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
506 struct lsa_DomainList domain_list;
508 /* defaults, but may be changed using params */
509 uint32 enum_ctx = 0;
510 int i;
511 uint32_t max_size = (uint32_t)-1;
513 if (argc > 2) {
514 printf("Usage: %s [enum context (0)]\n", argv[0]);
515 return NT_STATUS_OK;
518 if (argc == 2 && argv[1]) {
519 enum_ctx = atoi(argv[2]);
522 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
523 LSA_POLICY_VIEW_LOCAL_INFORMATION,
524 &pol);
526 if (!NT_STATUS_IS_OK(result))
527 goto done;
529 result = STATUS_MORE_ENTRIES;
531 while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
533 /* Lookup list of trusted domains */
535 result = rpccli_lsa_EnumTrustDom(cli, mem_ctx,
536 &pol,
537 &enum_ctx,
538 &domain_list,
539 max_size);
540 if (!NT_STATUS_IS_OK(result) &&
541 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
542 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
543 goto done;
545 /* Print results: list of names and sids returned in this
546 * response. */
547 for (i = 0; i < domain_list.count; i++) {
548 fstring sid_str;
550 sid_to_fstring(sid_str, domain_list.domains[i].sid);
551 printf("%s %s\n",
552 domain_list.domains[i].name.string ?
553 domain_list.domains[i].name.string : "*unknown*",
554 sid_str);
558 rpccli_lsa_Close(cli, mem_ctx, &pol);
559 done:
560 return result;
563 /* Enumerates privileges */
565 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli,
566 TALLOC_CTX *mem_ctx, int argc,
567 const char **argv)
569 struct policy_handle pol;
570 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
571 struct lsa_PrivArray priv_array;
573 uint32 enum_context=0;
574 uint32 pref_max_length=0x1000;
575 int i;
577 if (argc > 3) {
578 printf("Usage: %s [enum context] [max length]\n", argv[0]);
579 return NT_STATUS_OK;
582 if (argc>=2)
583 enum_context=atoi(argv[1]);
585 if (argc==3)
586 pref_max_length=atoi(argv[2]);
588 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
589 SEC_FLAG_MAXIMUM_ALLOWED,
590 &pol);
592 if (!NT_STATUS_IS_OK(result))
593 goto done;
595 result = rpccli_lsa_EnumPrivs(cli, mem_ctx,
596 &pol,
597 &enum_context,
598 &priv_array,
599 pref_max_length);
600 if (!NT_STATUS_IS_OK(result))
601 goto done;
603 /* Print results */
604 printf("found %d privileges\n\n", priv_array.count);
606 for (i = 0; i < priv_array.count; i++) {
607 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
608 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
609 priv_array.privs[i].luid.high,
610 priv_array.privs[i].luid.low,
611 priv_array.privs[i].luid.high,
612 priv_array.privs[i].luid.low);
615 rpccli_lsa_Close(cli, mem_ctx, &pol);
616 done:
617 return result;
620 /* Get privilege name */
622 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli,
623 TALLOC_CTX *mem_ctx, int argc,
624 const char **argv)
626 struct policy_handle pol;
627 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
629 uint16 lang_id=0;
630 uint16 lang_id_sys=0;
631 uint16 lang_id_desc;
632 struct lsa_String lsa_name;
633 struct lsa_StringLarge *description = NULL;
635 if (argc != 2) {
636 printf("Usage: %s privilege name\n", argv[0]);
637 return NT_STATUS_OK;
640 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
641 SEC_FLAG_MAXIMUM_ALLOWED,
642 &pol);
644 if (!NT_STATUS_IS_OK(result))
645 goto done;
647 init_lsa_String(&lsa_name, argv[1]);
649 result = rpccli_lsa_LookupPrivDisplayName(cli, mem_ctx,
650 &pol,
651 &lsa_name,
652 lang_id,
653 lang_id_sys,
654 &description,
655 &lang_id_desc);
657 if (!NT_STATUS_IS_OK(result))
658 goto done;
660 /* Print results */
661 printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
663 rpccli_lsa_Close(cli, mem_ctx, &pol);
664 done:
665 return result;
668 /* Enumerate the LSA SIDS */
670 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli,
671 TALLOC_CTX *mem_ctx, int argc,
672 const char **argv)
674 struct policy_handle pol;
675 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
677 uint32 enum_context=0;
678 uint32 pref_max_length=0x1000;
679 struct lsa_SidArray sid_array;
680 int i;
682 if (argc > 3) {
683 printf("Usage: %s [enum context] [max length]\n", argv[0]);
684 return NT_STATUS_OK;
687 if (argc>=2)
688 enum_context=atoi(argv[1]);
690 if (argc==3)
691 pref_max_length=atoi(argv[2]);
693 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
694 SEC_FLAG_MAXIMUM_ALLOWED,
695 &pol);
697 if (!NT_STATUS_IS_OK(result))
698 goto done;
700 result = rpccli_lsa_EnumAccounts(cli, mem_ctx,
701 &pol,
702 &enum_context,
703 &sid_array,
704 pref_max_length);
706 if (!NT_STATUS_IS_OK(result))
707 goto done;
709 /* Print results */
710 printf("found %d SIDs\n\n", sid_array.num_sids);
712 for (i = 0; i < sid_array.num_sids; i++) {
713 fstring sid_str;
715 sid_to_fstring(sid_str, sid_array.sids[i].sid);
716 printf("%s\n", sid_str);
719 rpccli_lsa_Close(cli, mem_ctx, &pol);
720 done:
721 return result;
724 /* Create a new account */
726 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli,
727 TALLOC_CTX *mem_ctx, int argc,
728 const char **argv)
730 struct policy_handle dom_pol;
731 struct policy_handle user_pol;
732 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
733 uint32 des_access = 0x000f000f;
735 struct dom_sid sid;
737 if (argc != 2 ) {
738 printf("Usage: %s SID\n", argv[0]);
739 return NT_STATUS_OK;
742 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
743 if (!NT_STATUS_IS_OK(result))
744 goto done;
746 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
747 SEC_FLAG_MAXIMUM_ALLOWED,
748 &dom_pol);
750 if (!NT_STATUS_IS_OK(result))
751 goto done;
753 result = rpccli_lsa_CreateAccount(cli, mem_ctx,
754 &dom_pol,
755 &sid,
756 des_access,
757 &user_pol);
759 if (!NT_STATUS_IS_OK(result))
760 goto done;
762 printf("Account for SID %s successfully created\n\n", argv[1]);
763 result = NT_STATUS_OK;
765 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
766 done:
767 return result;
771 /* Enumerate the privileges of an SID */
773 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli,
774 TALLOC_CTX *mem_ctx, int argc,
775 const char **argv)
777 struct policy_handle dom_pol;
778 struct policy_handle user_pol;
779 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
780 uint32 access_desired = 0x000f000f;
781 struct dom_sid sid;
782 struct lsa_PrivilegeSet *privs = NULL;
783 int i;
785 if (argc != 2 ) {
786 printf("Usage: %s SID\n", argv[0]);
787 return NT_STATUS_OK;
790 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
791 if (!NT_STATUS_IS_OK(result))
792 goto done;
794 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
795 SEC_FLAG_MAXIMUM_ALLOWED,
796 &dom_pol);
798 if (!NT_STATUS_IS_OK(result))
799 goto done;
801 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
802 &dom_pol,
803 &sid,
804 access_desired,
805 &user_pol);
807 if (!NT_STATUS_IS_OK(result))
808 goto done;
810 result = rpccli_lsa_EnumPrivsAccount(cli, mem_ctx,
811 &user_pol,
812 &privs);
814 if (!NT_STATUS_IS_OK(result))
815 goto done;
817 /* Print results */
818 printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
819 printf("high\tlow\tattribute\n");
821 for (i = 0; i < privs->count; i++) {
822 printf("%u\t%u\t%u\n",
823 privs->set[i].luid.high,
824 privs->set[i].luid.low,
825 privs->set[i].attribute);
828 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
829 done:
830 return result;
834 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
836 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli,
837 TALLOC_CTX *mem_ctx, int argc,
838 const char **argv)
840 struct policy_handle dom_pol;
841 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
842 struct dom_sid sid;
843 struct lsa_RightSet rights;
845 int i;
847 if (argc != 2 ) {
848 printf("Usage: %s SID\n", argv[0]);
849 return NT_STATUS_OK;
852 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
853 if (!NT_STATUS_IS_OK(result))
854 goto done;
856 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
857 SEC_FLAG_MAXIMUM_ALLOWED,
858 &dom_pol);
860 if (!NT_STATUS_IS_OK(result))
861 goto done;
863 result = rpccli_lsa_EnumAccountRights(cli, mem_ctx,
864 &dom_pol,
865 &sid,
866 &rights);
868 if (!NT_STATUS_IS_OK(result))
869 goto done;
871 printf("found %d privileges for SID %s\n", rights.count,
872 sid_string_tos(&sid));
874 for (i = 0; i < rights.count; i++) {
875 printf("\t%s\n", rights.names[i].string);
878 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
879 done:
880 return result;
884 /* add some privileges to a SID via LsaAddAccountRights */
886 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli,
887 TALLOC_CTX *mem_ctx, int argc,
888 const char **argv)
890 struct policy_handle dom_pol;
891 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
892 struct lsa_RightSet rights;
893 struct dom_sid sid;
894 int i;
896 if (argc < 3 ) {
897 printf("Usage: %s SID [rights...]\n", argv[0]);
898 return NT_STATUS_OK;
901 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
902 if (!NT_STATUS_IS_OK(result))
903 goto done;
905 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
906 SEC_FLAG_MAXIMUM_ALLOWED,
907 &dom_pol);
909 if (!NT_STATUS_IS_OK(result))
910 goto done;
912 rights.count = argc-2;
913 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
914 rights.count);
915 if (!rights.names) {
916 return NT_STATUS_NO_MEMORY;
919 for (i=0; i<argc-2; i++) {
920 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
923 result = rpccli_lsa_AddAccountRights(cli, mem_ctx,
924 &dom_pol,
925 &sid,
926 &rights);
928 if (!NT_STATUS_IS_OK(result))
929 goto done;
931 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
932 done:
933 return result;
937 /* remove some privileges to a SID via LsaRemoveAccountRights */
939 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli,
940 TALLOC_CTX *mem_ctx, int argc,
941 const char **argv)
943 struct policy_handle dom_pol;
944 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
945 struct lsa_RightSet rights;
946 struct dom_sid sid;
947 int i;
949 if (argc < 3 ) {
950 printf("Usage: %s SID [rights...]\n", argv[0]);
951 return NT_STATUS_OK;
954 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
955 if (!NT_STATUS_IS_OK(result))
956 goto done;
958 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
959 SEC_FLAG_MAXIMUM_ALLOWED,
960 &dom_pol);
962 if (!NT_STATUS_IS_OK(result))
963 goto done;
965 rights.count = argc-2;
966 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
967 rights.count);
968 if (!rights.names) {
969 return NT_STATUS_NO_MEMORY;
972 for (i=0; i<argc-2; i++) {
973 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
976 result = rpccli_lsa_RemoveAccountRights(cli, mem_ctx,
977 &dom_pol,
978 &sid,
979 false,
980 &rights);
982 if (!NT_STATUS_IS_OK(result))
983 goto done;
985 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
987 done:
988 return result;
992 /* Get a privilege value given its name */
994 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli,
995 TALLOC_CTX *mem_ctx, int argc,
996 const char **argv)
998 struct policy_handle pol;
999 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1000 struct lsa_LUID luid;
1001 struct lsa_String name;
1003 if (argc != 2 ) {
1004 printf("Usage: %s name\n", argv[0]);
1005 return NT_STATUS_OK;
1008 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1009 SEC_FLAG_MAXIMUM_ALLOWED,
1010 &pol);
1012 if (!NT_STATUS_IS_OK(result))
1013 goto done;
1015 init_lsa_String(&name, argv[1]);
1017 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1018 &pol,
1019 &name,
1020 &luid);
1022 if (!NT_STATUS_IS_OK(result))
1023 goto done;
1025 /* Print results */
1027 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
1029 rpccli_lsa_Close(cli, mem_ctx, &pol);
1030 done:
1031 return result;
1034 /* Query LSA security object */
1036 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli,
1037 TALLOC_CTX *mem_ctx, int argc,
1038 const char **argv)
1040 struct policy_handle pol;
1041 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1042 struct sec_desc_buf *sdb;
1043 uint32 sec_info = DACL_SECURITY_INFORMATION;
1045 if (argc < 1 || argc > 2) {
1046 printf("Usage: %s [sec_info]\n", argv[0]);
1047 return NT_STATUS_OK;
1050 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1051 SEC_FLAG_MAXIMUM_ALLOWED,
1052 &pol);
1054 if (argc == 2)
1055 sscanf(argv[1], "%x", &sec_info);
1057 if (!NT_STATUS_IS_OK(result))
1058 goto done;
1060 result = rpccli_lsa_QuerySecurity(cli, mem_ctx,
1061 &pol,
1062 sec_info,
1063 &sdb);
1064 if (!NT_STATUS_IS_OK(result))
1065 goto done;
1067 /* Print results */
1069 display_sec_desc(sdb->sd);
1071 rpccli_lsa_Close(cli, mem_ctx, &pol);
1072 done:
1073 return result;
1076 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
1077 uint8_t session_key[16])
1079 char *pwd, *pwd_old;
1081 DATA_BLOB data = data_blob_const(p->password->data, p->password->length);
1082 DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
1083 DATA_BLOB session_key_blob = data_blob_const(session_key, sizeof(session_key));
1085 pwd = sess_decrypt_string(talloc_tos(), &data, &session_key_blob);
1086 pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key_blob);
1088 d_printf("Password:\t%s\n", pwd);
1089 d_printf("Old Password:\t%s\n", pwd_old);
1091 talloc_free(pwd);
1092 talloc_free(pwd_old);
1095 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
1096 union lsa_TrustedDomainInfo *info,
1097 enum lsa_TrustDomInfoEnum info_class,
1098 uint8_t nt_hash[16])
1100 switch (info_class) {
1101 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
1102 display_trust_dom_info_4(&info->password, nt_hash);
1103 break;
1104 default: {
1105 const char *str = NULL;
1106 str = NDR_PRINT_UNION_STRING(mem_ctx,
1107 lsa_TrustedDomainInfo,
1108 info_class, info);
1109 if (str) {
1110 d_printf("%s\n", str);
1112 break;
1117 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
1118 TALLOC_CTX *mem_ctx, int argc,
1119 const char **argv)
1121 struct policy_handle pol;
1122 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1123 struct dom_sid dom_sid;
1124 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1125 union lsa_TrustedDomainInfo *info = NULL;
1126 enum lsa_TrustDomInfoEnum info_class = 1;
1127 uint8_t nt_hash[16];
1129 if (argc > 3 || argc < 2) {
1130 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1131 return NT_STATUS_OK;
1134 if (!string_to_sid(&dom_sid, argv[1]))
1135 return NT_STATUS_NO_MEMORY;
1137 if (argc == 3)
1138 info_class = atoi(argv[2]);
1140 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1142 if (!NT_STATUS_IS_OK(result))
1143 goto done;
1145 result = rpccli_lsa_QueryTrustedDomainInfoBySid(cli, mem_ctx,
1146 &pol,
1147 &dom_sid,
1148 info_class,
1149 &info);
1150 if (!NT_STATUS_IS_OK(result))
1151 goto done;
1153 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1154 d_fprintf(stderr, "Could not get pwd hash\n");
1155 goto done;
1158 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1160 done:
1161 rpccli_lsa_Close(cli, mem_ctx, &pol);
1163 return result;
1166 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1167 TALLOC_CTX *mem_ctx, int argc,
1168 const char **argv)
1170 struct policy_handle pol;
1171 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1172 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1173 union lsa_TrustedDomainInfo *info = NULL;
1174 enum lsa_TrustDomInfoEnum info_class = 1;
1175 struct lsa_String trusted_domain;
1176 uint8_t nt_hash[16];
1178 if (argc > 3 || argc < 2) {
1179 printf("Usage: %s [name] [info_class]\n", argv[0]);
1180 return NT_STATUS_OK;
1183 if (argc == 3)
1184 info_class = atoi(argv[2]);
1186 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1188 if (!NT_STATUS_IS_OK(result))
1189 goto done;
1191 init_lsa_String(&trusted_domain, argv[1]);
1193 result = rpccli_lsa_QueryTrustedDomainInfoByName(cli, mem_ctx,
1194 &pol,
1195 &trusted_domain,
1196 info_class,
1197 &info);
1198 if (!NT_STATUS_IS_OK(result))
1199 goto done;
1201 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1202 d_fprintf(stderr, "Could not get pwd hash\n");
1203 goto done;
1206 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1208 done:
1209 rpccli_lsa_Close(cli, mem_ctx, &pol);
1211 return result;
1214 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1215 TALLOC_CTX *mem_ctx, int argc,
1216 const char **argv)
1218 struct policy_handle pol, trustdom_pol;
1219 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1220 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1221 union lsa_TrustedDomainInfo *info = NULL;
1222 struct dom_sid dom_sid;
1223 enum lsa_TrustDomInfoEnum info_class = 1;
1224 uint8_t nt_hash[16];
1226 if (argc > 3 || argc < 2) {
1227 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1228 return NT_STATUS_OK;
1231 if (!string_to_sid(&dom_sid, argv[1]))
1232 return NT_STATUS_NO_MEMORY;
1235 if (argc == 3)
1236 info_class = atoi(argv[2]);
1238 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1240 if (!NT_STATUS_IS_OK(result))
1241 goto done;
1243 result = rpccli_lsa_OpenTrustedDomain(cli, mem_ctx,
1244 &pol,
1245 &dom_sid,
1246 access_mask,
1247 &trustdom_pol);
1249 if (!NT_STATUS_IS_OK(result))
1250 goto done;
1252 result = rpccli_lsa_QueryTrustedDomainInfo(cli, mem_ctx,
1253 &trustdom_pol,
1254 info_class,
1255 &info);
1257 if (!NT_STATUS_IS_OK(result))
1258 goto done;
1260 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1261 d_fprintf(stderr, "Could not get pwd hash\n");
1262 goto done;
1265 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1267 done:
1268 rpccli_lsa_Close(cli, mem_ctx, &pol);
1270 return result;
1273 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1274 TALLOC_CTX *mem_ctx, int argc,
1275 const char **argv)
1277 struct policy_handle pol;
1278 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1279 const char *servername = cli->desthost;
1280 struct lsa_String *account_name = NULL;
1281 struct lsa_String *authority_name = NULL;
1283 if (argc > 2) {
1284 printf("Usage: %s servername\n", argv[0]);
1285 return NT_STATUS_OK;
1288 result = rpccli_lsa_open_policy(cli, mem_ctx, true,
1289 SEC_FLAG_MAXIMUM_ALLOWED,
1290 &pol);
1292 if (!NT_STATUS_IS_OK(result)) {
1293 goto done;
1296 result = rpccli_lsa_GetUserName(cli, mem_ctx,
1297 servername,
1298 &account_name,
1299 &authority_name);
1300 if (!NT_STATUS_IS_OK(result)) {
1301 goto done;
1304 /* Print results */
1306 printf("Account Name: %s, Authority Name: %s\n",
1307 account_name->string, authority_name ? authority_name->string :
1308 "");
1310 rpccli_lsa_Close(cli, mem_ctx, &pol);
1311 done:
1312 return result;
1315 static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
1316 TALLOC_CTX *mem_ctx, int argc,
1317 const char **argv)
1319 struct policy_handle dom_pol, user_pol;
1320 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1321 struct lsa_PrivilegeSet privs;
1322 struct lsa_LUIDAttribute *set = NULL;
1323 struct dom_sid sid;
1324 int i;
1326 ZERO_STRUCT(privs);
1328 if (argc < 3 ) {
1329 printf("Usage: %s SID [rights...]\n", argv[0]);
1330 return NT_STATUS_OK;
1333 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1334 if (!NT_STATUS_IS_OK(result)) {
1335 goto done;
1338 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1339 SEC_FLAG_MAXIMUM_ALLOWED,
1340 &dom_pol);
1342 if (!NT_STATUS_IS_OK(result)) {
1343 goto done;
1346 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
1347 &dom_pol,
1348 &sid,
1349 SEC_FLAG_MAXIMUM_ALLOWED,
1350 &user_pol);
1352 if (!NT_STATUS_IS_OK(result)) {
1353 goto done;
1356 for (i=2; i<argc; i++) {
1358 struct lsa_String priv_name;
1359 struct lsa_LUID luid;
1361 init_lsa_String(&priv_name, argv[i]);
1363 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1364 &dom_pol,
1365 &priv_name,
1366 &luid);
1367 if (!NT_STATUS_IS_OK(result)) {
1368 continue;
1371 privs.count++;
1372 set = TALLOC_REALLOC_ARRAY(mem_ctx, set,
1373 struct lsa_LUIDAttribute,
1374 privs.count);
1375 if (!set) {
1376 return NT_STATUS_NO_MEMORY;
1379 set[privs.count-1].luid = luid;
1380 set[privs.count-1].attribute = 0;
1383 privs.set = set;
1385 result = rpccli_lsa_AddPrivilegesToAccount(cli, mem_ctx,
1386 &user_pol,
1387 &privs);
1389 if (!NT_STATUS_IS_OK(result)) {
1390 goto done;
1393 rpccli_lsa_Close(cli, mem_ctx, &user_pol);
1394 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
1395 done:
1396 return result;
1399 static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
1400 TALLOC_CTX *mem_ctx, int argc,
1401 const char **argv)
1403 struct policy_handle dom_pol, user_pol;
1404 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1405 struct lsa_PrivilegeSet privs;
1406 struct lsa_LUIDAttribute *set = NULL;
1407 struct dom_sid sid;
1408 int i;
1410 ZERO_STRUCT(privs);
1412 if (argc < 3 ) {
1413 printf("Usage: %s SID [rights...]\n", argv[0]);
1414 return NT_STATUS_OK;
1417 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1418 if (!NT_STATUS_IS_OK(result)) {
1419 goto done;
1422 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1423 SEC_FLAG_MAXIMUM_ALLOWED,
1424 &dom_pol);
1426 if (!NT_STATUS_IS_OK(result)) {
1427 goto done;
1430 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
1431 &dom_pol,
1432 &sid,
1433 SEC_FLAG_MAXIMUM_ALLOWED,
1434 &user_pol);
1436 if (!NT_STATUS_IS_OK(result)) {
1437 goto done;
1440 for (i=2; i<argc; i++) {
1442 struct lsa_String priv_name;
1443 struct lsa_LUID luid;
1445 init_lsa_String(&priv_name, argv[i]);
1447 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1448 &dom_pol,
1449 &priv_name,
1450 &luid);
1451 if (!NT_STATUS_IS_OK(result)) {
1452 continue;
1455 privs.count++;
1456 set = TALLOC_REALLOC_ARRAY(mem_ctx, set,
1457 struct lsa_LUIDAttribute,
1458 privs.count);
1459 if (!set) {
1460 return NT_STATUS_NO_MEMORY;
1463 set[privs.count-1].luid = luid;
1464 set[privs.count-1].attribute = 0;
1467 privs.set = set;
1470 result = rpccli_lsa_RemovePrivilegesFromAccount(cli, mem_ctx,
1471 &user_pol,
1472 false,
1473 &privs);
1475 if (!NT_STATUS_IS_OK(result)) {
1476 goto done;
1479 rpccli_lsa_Close(cli, mem_ctx, &user_pol);
1480 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
1481 done:
1482 return result;
1485 static NTSTATUS cmd_lsa_create_secret(struct rpc_pipe_client *cli,
1486 TALLOC_CTX *mem_ctx, int argc,
1487 const char **argv)
1489 NTSTATUS status;
1490 struct policy_handle handle, sec_handle;
1491 struct lsa_String name;
1493 if (argc < 2) {
1494 printf("Usage: %s name\n", argv[0]);
1495 return NT_STATUS_OK;
1498 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1499 true,
1500 SEC_FLAG_MAXIMUM_ALLOWED,
1501 &handle);
1502 if (!NT_STATUS_IS_OK(status)) {
1503 return status;
1506 init_lsa_String(&name, argv[1]);
1508 status = rpccli_lsa_CreateSecret(cli, mem_ctx,
1509 &handle,
1510 name,
1511 SEC_FLAG_MAXIMUM_ALLOWED,
1512 &sec_handle);
1513 if (!NT_STATUS_IS_OK(status)) {
1514 goto done;
1517 done:
1518 if (is_valid_policy_hnd(&sec_handle)) {
1519 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1521 if (is_valid_policy_hnd(&handle)) {
1522 rpccli_lsa_Close(cli, mem_ctx, &handle);
1525 return status;
1528 static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
1529 TALLOC_CTX *mem_ctx, int argc,
1530 const char **argv)
1532 NTSTATUS status;
1533 struct policy_handle handle, sec_handle;
1534 struct lsa_String name;
1536 if (argc < 2) {
1537 printf("Usage: %s name\n", argv[0]);
1538 return NT_STATUS_OK;
1541 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1542 true,
1543 SEC_FLAG_MAXIMUM_ALLOWED,
1544 &handle);
1545 if (!NT_STATUS_IS_OK(status)) {
1546 return status;
1549 init_lsa_String(&name, argv[1]);
1551 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1552 &handle,
1553 name,
1554 SEC_FLAG_MAXIMUM_ALLOWED,
1555 &sec_handle);
1556 if (!NT_STATUS_IS_OK(status)) {
1557 goto done;
1560 status = rpccli_lsa_DeleteObject(cli, mem_ctx,
1561 &sec_handle);
1562 if (!NT_STATUS_IS_OK(status)) {
1563 goto done;
1566 done:
1567 if (is_valid_policy_hnd(&sec_handle)) {
1568 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1570 if (is_valid_policy_hnd(&handle)) {
1571 rpccli_lsa_Close(cli, mem_ctx, &handle);
1574 return status;
1577 static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
1578 TALLOC_CTX *mem_ctx, int argc,
1579 const char **argv)
1581 NTSTATUS status;
1582 struct policy_handle handle, sec_handle;
1583 struct lsa_String name;
1584 struct lsa_DATA_BUF_PTR new_val;
1585 NTTIME new_mtime = 0;
1586 struct lsa_DATA_BUF_PTR old_val;
1587 NTTIME old_mtime = 0;
1588 DATA_BLOB session_key;
1589 DATA_BLOB new_blob = data_blob_null;
1590 DATA_BLOB old_blob = data_blob_null;
1591 char *new_secret, *old_secret;
1593 if (argc < 2) {
1594 printf("Usage: %s name\n", argv[0]);
1595 return NT_STATUS_OK;
1598 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1599 true,
1600 SEC_FLAG_MAXIMUM_ALLOWED,
1601 &handle);
1602 if (!NT_STATUS_IS_OK(status)) {
1603 return status;
1606 init_lsa_String(&name, argv[1]);
1608 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1609 &handle,
1610 name,
1611 SEC_FLAG_MAXIMUM_ALLOWED,
1612 &sec_handle);
1613 if (!NT_STATUS_IS_OK(status)) {
1614 goto done;
1617 ZERO_STRUCT(new_val);
1618 ZERO_STRUCT(old_val);
1620 status = rpccli_lsa_QuerySecret(cli, mem_ctx,
1621 &sec_handle,
1622 &new_val,
1623 &new_mtime,
1624 &old_val,
1625 &old_mtime);
1626 if (!NT_STATUS_IS_OK(status)) {
1627 goto done;
1630 status = cli_get_session_key(mem_ctx, cli, &session_key);
1631 if (!NT_STATUS_IS_OK(status)) {
1632 goto done;
1635 if (new_val.buf) {
1636 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
1638 if (old_val.buf) {
1639 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
1642 new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
1643 old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
1644 if (new_secret) {
1645 d_printf("new secret: %s\n", new_secret);
1647 if (old_secret) {
1648 d_printf("old secret: %s\n", old_secret);
1651 done:
1652 if (is_valid_policy_hnd(&sec_handle)) {
1653 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1655 if (is_valid_policy_hnd(&handle)) {
1656 rpccli_lsa_Close(cli, mem_ctx, &handle);
1659 return status;
1662 static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
1663 TALLOC_CTX *mem_ctx, int argc,
1664 const char **argv)
1666 NTSTATUS status;
1667 struct policy_handle handle, sec_handle;
1668 struct lsa_String name;
1669 struct lsa_DATA_BUF new_val;
1670 struct lsa_DATA_BUF old_val;
1671 DATA_BLOB enc_key;
1672 DATA_BLOB session_key;
1674 if (argc < 3) {
1675 printf("Usage: %s name secret\n", argv[0]);
1676 return NT_STATUS_OK;
1679 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1680 true,
1681 SEC_FLAG_MAXIMUM_ALLOWED,
1682 &handle);
1683 if (!NT_STATUS_IS_OK(status)) {
1684 return status;
1687 init_lsa_String(&name, argv[1]);
1689 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1690 &handle,
1691 name,
1692 SEC_FLAG_MAXIMUM_ALLOWED,
1693 &sec_handle);
1694 if (!NT_STATUS_IS_OK(status)) {
1695 goto done;
1698 ZERO_STRUCT(new_val);
1699 ZERO_STRUCT(old_val);
1701 status = cli_get_session_key(mem_ctx, cli, &session_key);
1702 if (!NT_STATUS_IS_OK(status)) {
1703 goto done;
1706 enc_key = sess_encrypt_string(argv[2], &session_key);
1708 new_val.length = enc_key.length;
1709 new_val.size = enc_key.length;
1710 new_val.data = enc_key.data;
1712 status = rpccli_lsa_SetSecret(cli, mem_ctx,
1713 &sec_handle,
1714 &new_val,
1715 NULL);
1716 if (!NT_STATUS_IS_OK(status)) {
1717 goto done;
1720 done:
1721 if (is_valid_policy_hnd(&sec_handle)) {
1722 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1724 if (is_valid_policy_hnd(&handle)) {
1725 rpccli_lsa_Close(cli, mem_ctx, &handle);
1728 return status;
1731 static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
1732 TALLOC_CTX *mem_ctx, int argc,
1733 const char **argv)
1735 NTSTATUS status;
1736 struct policy_handle handle;
1737 struct lsa_String name;
1738 struct lsa_DATA_BUF *val;
1739 DATA_BLOB session_key;
1740 DATA_BLOB blob = data_blob_null;
1741 char *secret;
1743 if (argc < 2) {
1744 printf("Usage: %s name\n", argv[0]);
1745 return NT_STATUS_OK;
1748 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1749 true,
1750 SEC_FLAG_MAXIMUM_ALLOWED,
1751 &handle);
1752 if (!NT_STATUS_IS_OK(status)) {
1753 return status;
1756 init_lsa_String(&name, argv[1]);
1758 ZERO_STRUCT(val);
1760 status = rpccli_lsa_RetrievePrivateData(cli, mem_ctx,
1761 &handle,
1762 &name,
1763 &val);
1764 if (!NT_STATUS_IS_OK(status)) {
1765 goto done;
1768 status = cli_get_session_key(mem_ctx, cli, &session_key);
1769 if (!NT_STATUS_IS_OK(status)) {
1770 goto done;
1773 if (val) {
1774 blob = data_blob_const(val->data, val->length);
1777 secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
1778 if (secret) {
1779 d_printf("secret: %s\n", secret);
1782 done:
1783 if (is_valid_policy_hnd(&handle)) {
1784 rpccli_lsa_Close(cli, mem_ctx, &handle);
1787 return status;
1790 static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
1791 TALLOC_CTX *mem_ctx, int argc,
1792 const char **argv)
1794 NTSTATUS status;
1795 struct policy_handle handle;
1796 struct lsa_String name;
1797 struct lsa_DATA_BUF val;
1798 DATA_BLOB session_key;
1799 DATA_BLOB enc_key;
1801 if (argc < 3) {
1802 printf("Usage: %s name secret\n", argv[0]);
1803 return NT_STATUS_OK;
1806 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1807 true,
1808 SEC_FLAG_MAXIMUM_ALLOWED,
1809 &handle);
1810 if (!NT_STATUS_IS_OK(status)) {
1811 return status;
1814 init_lsa_String(&name, argv[1]);
1816 ZERO_STRUCT(val);
1818 status = cli_get_session_key(mem_ctx, cli, &session_key);
1819 if (!NT_STATUS_IS_OK(status)) {
1820 goto done;
1823 enc_key = sess_encrypt_string(argv[2], &session_key);
1825 val.length = enc_key.length;
1826 val.size = enc_key.length;
1827 val.data = enc_key.data;
1829 status = rpccli_lsa_StorePrivateData(cli, mem_ctx,
1830 &handle,
1831 &name,
1832 &val);
1833 if (!NT_STATUS_IS_OK(status)) {
1834 goto done;
1837 done:
1838 if (is_valid_policy_hnd(&handle)) {
1839 rpccli_lsa_Close(cli, mem_ctx, &handle);
1842 return status;
1845 static NTSTATUS cmd_lsa_create_trusted_domain(struct rpc_pipe_client *cli,
1846 TALLOC_CTX *mem_ctx, int argc,
1847 const char **argv)
1849 NTSTATUS status;
1850 struct policy_handle handle, trustdom_handle;
1851 struct dom_sid sid;
1852 struct lsa_DomainInfo info;
1854 if (argc < 3) {
1855 printf("Usage: %s name sid\n", argv[0]);
1856 return NT_STATUS_OK;
1859 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1860 true,
1861 SEC_FLAG_MAXIMUM_ALLOWED,
1862 &handle);
1863 if (!NT_STATUS_IS_OK(status)) {
1864 return status;
1867 init_lsa_StringLarge(&info.name, argv[1]);
1868 info.sid = &sid;
1869 string_to_sid(&sid, argv[2]);
1871 status = rpccli_lsa_CreateTrustedDomain(cli, mem_ctx,
1872 &handle,
1873 &info,
1874 SEC_FLAG_MAXIMUM_ALLOWED,
1875 &trustdom_handle);
1876 if (!NT_STATUS_IS_OK(status)) {
1877 goto done;
1880 done:
1881 if (is_valid_policy_hnd(&trustdom_handle)) {
1882 rpccli_lsa_Close(cli, mem_ctx, &trustdom_handle);
1885 if (is_valid_policy_hnd(&handle)) {
1886 rpccli_lsa_Close(cli, mem_ctx, &handle);
1889 return status;
1892 static NTSTATUS cmd_lsa_delete_trusted_domain(struct rpc_pipe_client *cli,
1893 TALLOC_CTX *mem_ctx, int argc,
1894 const char **argv)
1896 NTSTATUS status;
1897 struct policy_handle handle, trustdom_handle;
1898 struct lsa_String name;
1899 struct dom_sid *sid = NULL;
1901 if (argc < 2) {
1902 printf("Usage: %s name\n", argv[0]);
1903 return NT_STATUS_OK;
1906 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1907 true,
1908 SEC_FLAG_MAXIMUM_ALLOWED,
1909 &handle);
1910 if (!NT_STATUS_IS_OK(status)) {
1911 return status;
1914 init_lsa_String(&name, argv[1]);
1916 status = rpccli_lsa_OpenTrustedDomainByName(cli, mem_ctx,
1917 &handle,
1918 name,
1919 SEC_FLAG_MAXIMUM_ALLOWED,
1920 &trustdom_handle);
1921 if (NT_STATUS_IS_OK(status)) {
1922 goto delete_object;
1926 uint32_t resume_handle = 0;
1927 struct lsa_DomainList domains;
1928 int i;
1930 status = rpccli_lsa_EnumTrustDom(cli, mem_ctx,
1931 &handle,
1932 &resume_handle,
1933 &domains,
1934 0xffff);
1935 if (!NT_STATUS_IS_OK(status)) {
1936 goto done;
1939 for (i=0; i < domains.count; i++) {
1940 if (strequal(domains.domains[i].name.string, argv[1])) {
1941 sid = domains.domains[i].sid;
1942 break;
1946 if (!sid) {
1947 return NT_STATUS_INVALID_SID;
1951 status = rpccli_lsa_OpenTrustedDomain(cli, mem_ctx,
1952 &handle,
1953 sid,
1954 SEC_FLAG_MAXIMUM_ALLOWED,
1955 &trustdom_handle);
1956 if (!NT_STATUS_IS_OK(status)) {
1957 goto done;
1960 delete_object:
1961 status = rpccli_lsa_DeleteObject(cli, mem_ctx,
1962 &trustdom_handle);
1963 if (!NT_STATUS_IS_OK(status)) {
1964 goto done;
1967 done:
1968 if (is_valid_policy_hnd(&trustdom_handle)) {
1969 rpccli_lsa_Close(cli, mem_ctx, &trustdom_handle);
1972 if (is_valid_policy_hnd(&handle)) {
1973 rpccli_lsa_Close(cli, mem_ctx, &handle);
1976 return status;
1980 /* List of commands exported by this module */
1982 struct cmd_set lsarpc_commands[] = {
1984 { "LSARPC" },
1986 { "lsaquery", RPC_RTYPE_NTSTATUS, cmd_lsa_query_info_policy, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query info policy", "" },
1987 { "lookupsids", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert SIDs to names", "" },
1988 { "lookupsids3", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids3, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert SIDs to names", "" },
1989 { "lookupnames", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1990 { "lookupnames4", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names4, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1991 { "lookupnames_level", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names_level, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1992 { "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)]" },
1993 { "enumprivs", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privilege, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate privileges", "" },
1994 { "getdispname", RPC_RTYPE_NTSTATUS, cmd_lsa_get_dispname, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get the privilege name", "" },
1995 { "lsaenumsid", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_sids, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the LSA SIDS", "" },
1996 { "lsacreateaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_create_account, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create a new lsa account", "" },
1997 { "lsaenumprivsaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privsaccounts, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the privileges of an SID", "" },
1998 { "lsaenumacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the rights of an SID", "" },
1999 { "lsaaddpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_add_priv, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Assign a privilege to a SID", "" },
2000 { "lsadelpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_del_priv, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Revoke a privilege from a SID", "" },
2001 { "lsaaddacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_add_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Add rights to an account", "" },
2002 { "lsaremoveacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_remove_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Remove rights from an account", "" },
2003 { "lsalookupprivvalue", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_priv_value, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get a privilege value given its name", "" },
2004 { "lsaquerysecobj", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secobj, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA security object", "" },
2005 { "lsaquerytrustdominfo",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfo, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a SID)", "" },
2006 { "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", "" },
2007 { "lsaquerytrustdominfobysid",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobysid, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a SID)", "" },
2008 { "getusername", RPC_RTYPE_NTSTATUS, cmd_lsa_get_username, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get username", "" },
2009 { "createsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_create_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create Secret", "" },
2010 { "deletesecret", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Delete Secret", "" },
2011 { "querysecret", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query Secret", "" },
2012 { "setsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_set_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Set Secret", "" },
2013 { "retrieveprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_retrieve_private_data, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Retrieve Private Data", "" },
2014 { "storeprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_store_private_data, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Store Private Data", "" },
2015 { "createtrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_create_trusted_domain, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create Trusted Domain", "" },
2016 { "deletetrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_trusted_domain, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Delete Trusted Domain", "" },
2018 { NULL }