Merge branch 'master' of /home/tridge/samba/git/combined
[Samba/aatanasov.git] / source3 / rpcclient / cmd_lsarpc.c
blob752881c2210f5ddca7dbb879270e1e0e69406900
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"
27 /* useful function to allow entering a name instead of a SID and
28 * looking it up automatically */
29 static NTSTATUS name_to_sid(struct rpc_pipe_client *cli,
30 TALLOC_CTX *mem_ctx,
31 DOM_SID *sid, const char *name)
33 struct policy_handle pol;
34 enum lsa_SidType *sid_types;
35 NTSTATUS result;
36 DOM_SID *sids;
38 /* maybe its a raw SID */
39 if (strncmp(name, "S-", 2) == 0 &&
40 string_to_sid(sid, name)) {
41 return NT_STATUS_OK;
44 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
45 SEC_FLAG_MAXIMUM_ALLOWED,
46 &pol);
47 if (!NT_STATUS_IS_OK(result))
48 goto done;
50 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, NULL, 1, &sids, &sid_types);
51 if (!NT_STATUS_IS_OK(result))
52 goto done;
54 rpccli_lsa_Close(cli, mem_ctx, &pol);
56 *sid = sids[0];
58 done:
59 return result;
62 static void display_query_info_1(struct lsa_AuditLogInfo *r)
64 d_printf("percent_full:\t%d\n", r->percent_full);
65 d_printf("maximum_log_size:\t%d\n", r->maximum_log_size);
66 d_printf("retention_time:\t%lld\n", (long long)r->retention_time);
67 d_printf("shutdown_in_progress:\t%d\n", r->shutdown_in_progress);
68 d_printf("time_to_shutdown:\t%lld\n", (long long)r->time_to_shutdown);
69 d_printf("next_audit_record:\t%d\n", r->next_audit_record);
72 static void display_query_info_2(struct lsa_AuditEventsInfo *r)
74 int i;
75 d_printf("Auditing enabled:\t%d\n", r->auditing_mode);
76 d_printf("Auditing categories:\t%d\n", r->count);
77 d_printf("Auditsettings:\n");
78 for (i=0; i<r->count; i++) {
79 const char *val = audit_policy_str(talloc_tos(), r->settings[i]);
80 const char *policy = audit_description_str(i);
81 d_printf("%s:\t%s\n", policy, val);
85 static void display_query_info_3(struct lsa_DomainInfo *r)
87 d_printf("Domain Name: %s\n", r->name.string);
88 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
91 static void display_query_info_5(struct lsa_DomainInfo *r)
93 d_printf("Domain Name: %s\n", r->name.string);
94 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
97 static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
99 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
102 static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
104 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
105 d_printf("Log is full: %d\n", r->log_is_full);
108 static void display_query_info_12(struct lsa_DnsDomainInfo *r)
110 d_printf("Domain NetBios Name: %s\n", r->name.string);
111 d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
112 d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
113 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
114 d_printf("Domain GUID: %s\n", GUID_string(talloc_tos(),
115 &r->domain_guid));
118 static void display_lsa_query_info(union lsa_PolicyInformation *info,
119 enum lsa_PolicyInfo level)
121 switch (level) {
122 case 1:
123 display_query_info_1(&info->audit_log);
124 break;
125 case 2:
126 display_query_info_2(&info->audit_events);
127 break;
128 case 3:
129 display_query_info_3(&info->domain);
130 break;
131 case 5:
132 display_query_info_5(&info->account_domain);
133 break;
134 case 10:
135 display_query_info_10(&info->auditfullset);
136 break;
137 case 11:
138 display_query_info_11(&info->auditfullquery);
139 break;
140 case 12:
141 display_query_info_12(&info->dns);
142 break;
143 default:
144 printf("can't display info level: %d\n", level);
145 break;
149 static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli,
150 TALLOC_CTX *mem_ctx, int argc,
151 const char **argv)
153 struct policy_handle pol;
154 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
155 union lsa_PolicyInformation *info = NULL;
157 uint32 info_class = 3;
159 if (argc > 2) {
160 printf("Usage: %s [info_class]\n", argv[0]);
161 return NT_STATUS_OK;
164 if (argc == 2)
165 info_class = atoi(argv[1]);
167 switch (info_class) {
168 case 12:
169 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
170 SEC_FLAG_MAXIMUM_ALLOWED,
171 &pol);
173 if (!NT_STATUS_IS_OK(result))
174 goto done;
176 result = rpccli_lsa_QueryInfoPolicy2(cli, mem_ctx,
177 &pol,
178 info_class,
179 &info);
180 break;
181 default:
182 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
183 SEC_FLAG_MAXIMUM_ALLOWED,
184 &pol);
186 if (!NT_STATUS_IS_OK(result))
187 goto done;
189 result = rpccli_lsa_QueryInfoPolicy(cli, mem_ctx,
190 &pol,
191 info_class,
192 &info);
195 if (NT_STATUS_IS_OK(result)) {
196 display_lsa_query_info(info, info_class);
199 rpccli_lsa_Close(cli, mem_ctx, &pol);
201 done:
202 return result;
205 /* Resolve a list of names to a list of sids */
207 static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli,
208 TALLOC_CTX *mem_ctx, int argc,
209 const char **argv)
211 struct policy_handle pol;
212 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
213 DOM_SID *sids;
214 enum lsa_SidType *types;
215 int i;
217 if (argc == 1) {
218 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
219 return NT_STATUS_OK;
222 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
223 SEC_FLAG_MAXIMUM_ALLOWED,
224 &pol);
226 if (!NT_STATUS_IS_OK(result))
227 goto done;
229 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
230 (const char**)(argv + 1), NULL, 1, &sids, &types);
232 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
233 NT_STATUS_V(STATUS_SOME_UNMAPPED))
234 goto done;
236 result = NT_STATUS_OK;
238 /* Print results */
240 for (i = 0; i < (argc - 1); i++) {
241 fstring sid_str;
242 sid_to_fstring(sid_str, &sids[i]);
243 printf("%s %s (%s: %d)\n", argv[i + 1], sid_str,
244 sid_type_lookup(types[i]), types[i]);
247 rpccli_lsa_Close(cli, mem_ctx, &pol);
249 done:
250 return result;
253 /* Resolve a list of names to a list of sids */
255 static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli,
256 TALLOC_CTX *mem_ctx, int argc,
257 const char **argv)
259 struct policy_handle pol;
260 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
261 DOM_SID *sids;
262 enum lsa_SidType *types;
263 int i, level;
265 if (argc < 3) {
266 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
267 return NT_STATUS_OK;
270 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
271 SEC_FLAG_MAXIMUM_ALLOWED,
272 &pol);
274 if (!NT_STATUS_IS_OK(result))
275 goto done;
277 level = atoi(argv[1]);
279 result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2,
280 (const char**)(argv + 2), NULL, level, &sids, &types);
282 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
283 NT_STATUS_V(STATUS_SOME_UNMAPPED))
284 goto done;
286 result = NT_STATUS_OK;
288 /* Print results */
290 for (i = 0; i < (argc - 2); i++) {
291 fstring sid_str;
292 sid_to_fstring(sid_str, &sids[i]);
293 printf("%s %s (%s: %d)\n", argv[i + 2], sid_str,
294 sid_type_lookup(types[i]), types[i]);
297 rpccli_lsa_Close(cli, mem_ctx, &pol);
299 done:
300 return result;
303 static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
304 TALLOC_CTX *mem_ctx, int argc,
305 const char **argv)
307 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
309 uint32_t num_names;
310 struct lsa_String *names;
311 struct lsa_RefDomainList *domains;
312 struct lsa_TransSidArray3 sids;
313 uint32_t count = 0;
314 int i;
316 if (argc == 1) {
317 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
318 return NT_STATUS_OK;
321 ZERO_STRUCT(sids);
323 num_names = argc-1;
324 names = talloc_array(mem_ctx, struct lsa_String, num_names);
325 NT_STATUS_HAVE_NO_MEMORY(names);
327 for (i=0; i < num_names; i++) {
328 init_lsa_String(&names[i], argv[i+1]);
331 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
332 num_names,
333 names,
334 &domains,
335 &sids,
337 &count,
340 if (!NT_STATUS_IS_OK(result)) {
341 return result;
344 for (i = 0; i < sids.count; i++) {
345 fstring sid_str;
346 sid_to_fstring(sid_str, sids.sids[i].sid);
347 printf("%s %s (%s: %d)\n", argv[i+1], sid_str,
348 sid_type_lookup(sids.sids[i].sid_type),
349 sids.sids[i].sid_type);
352 return result;
355 /* Resolve a list of SIDs to a list of names */
357 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
358 int argc, const char **argv)
360 struct policy_handle pol;
361 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
362 DOM_SID *sids;
363 char **domains;
364 char **names;
365 enum lsa_SidType *types;
366 int i;
368 if (argc == 1) {
369 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
370 return NT_STATUS_OK;
373 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
374 SEC_FLAG_MAXIMUM_ALLOWED,
375 &pol);
377 if (!NT_STATUS_IS_OK(result))
378 goto done;
380 /* Convert arguments to sids */
382 sids = TALLOC_ARRAY(mem_ctx, DOM_SID, argc - 1);
384 if (!sids) {
385 printf("could not allocate memory for %d sids\n", argc - 1);
386 goto done;
389 for (i = 0; i < argc - 1; i++)
390 if (!string_to_sid(&sids[i], argv[i + 1])) {
391 result = NT_STATUS_INVALID_SID;
392 goto done;
395 /* Lookup the SIDs */
397 result = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
398 &domains, &names, &types);
400 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
401 NT_STATUS_V(STATUS_SOME_UNMAPPED))
402 goto done;
404 result = NT_STATUS_OK;
406 /* Print results */
408 for (i = 0; i < (argc - 1); i++) {
409 fstring sid_str;
411 sid_to_fstring(sid_str, &sids[i]);
412 printf("%s %s\\%s (%d)\n", sid_str,
413 domains[i] ? domains[i] : "*unknown*",
414 names[i] ? names[i] : "*unknown*", types[i]);
417 rpccli_lsa_Close(cli, mem_ctx, &pol);
419 done:
420 return result;
423 /* Resolve a list of SIDs to a list of names */
425 static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
426 TALLOC_CTX *mem_ctx,
427 int argc, const char **argv)
429 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
430 int i;
431 struct lsa_SidArray sids;
432 struct lsa_RefDomainList *domains;
433 struct lsa_TransNameArray2 names;
434 uint32_t count = 0;
436 if (argc == 1) {
437 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
438 return NT_STATUS_OK;
441 ZERO_STRUCT(names);
443 /* Convert arguments to sids */
445 sids.num_sids = argc-1;
446 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
447 if (!sids.sids) {
448 printf("could not allocate memory for %d sids\n", sids.num_sids);
449 goto done;
452 for (i = 0; i < sids.num_sids; i++) {
453 sids.sids[0].sid = string_sid_talloc(sids.sids, argv[i + 1]);
454 if (!sids.sids[0].sid) {
455 result = NT_STATUS_INVALID_SID;
456 goto done;
460 /* Lookup the SIDs */
461 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
462 &sids,
463 &domains,
464 &names,
466 &count,
470 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
471 NT_STATUS_V(STATUS_SOME_UNMAPPED))
472 goto done;
474 result = NT_STATUS_OK;
476 /* Print results */
478 for (i = 0; i < count; i++) {
479 fstring sid_str;
481 sid_to_fstring(sid_str, sids.sids[i].sid);
482 printf("%s %s (%d)\n", sid_str,
483 names.names[i].name.string,
484 names.names[i].sid_type);
487 done:
488 return result;
492 /* Enumerate list of trusted domains */
494 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
495 TALLOC_CTX *mem_ctx, int argc,
496 const char **argv)
498 struct policy_handle pol;
499 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
500 struct lsa_DomainList domain_list;
502 /* defaults, but may be changed using params */
503 uint32 enum_ctx = 0;
504 int i;
505 uint32_t max_size = (uint32_t)-1;
507 if (argc > 2) {
508 printf("Usage: %s [enum context (0)]\n", argv[0]);
509 return NT_STATUS_OK;
512 if (argc == 2 && argv[1]) {
513 enum_ctx = atoi(argv[2]);
516 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
517 LSA_POLICY_VIEW_LOCAL_INFORMATION,
518 &pol);
520 if (!NT_STATUS_IS_OK(result))
521 goto done;
523 result = STATUS_MORE_ENTRIES;
525 while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
527 /* Lookup list of trusted domains */
529 result = rpccli_lsa_EnumTrustDom(cli, mem_ctx,
530 &pol,
531 &enum_ctx,
532 &domain_list,
533 max_size);
534 if (!NT_STATUS_IS_OK(result) &&
535 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
536 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
537 goto done;
539 /* Print results: list of names and sids returned in this
540 * response. */
541 for (i = 0; i < domain_list.count; i++) {
542 fstring sid_str;
544 sid_to_fstring(sid_str, domain_list.domains[i].sid);
545 printf("%s %s\n",
546 domain_list.domains[i].name.string ?
547 domain_list.domains[i].name.string : "*unknown*",
548 sid_str);
552 rpccli_lsa_Close(cli, mem_ctx, &pol);
553 done:
554 return result;
557 /* Enumerates privileges */
559 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli,
560 TALLOC_CTX *mem_ctx, int argc,
561 const char **argv)
563 struct policy_handle pol;
564 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
565 struct lsa_PrivArray priv_array;
567 uint32 enum_context=0;
568 uint32 pref_max_length=0x1000;
569 int i;
571 if (argc > 3) {
572 printf("Usage: %s [enum context] [max length]\n", argv[0]);
573 return NT_STATUS_OK;
576 if (argc>=2)
577 enum_context=atoi(argv[1]);
579 if (argc==3)
580 pref_max_length=atoi(argv[2]);
582 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
583 SEC_FLAG_MAXIMUM_ALLOWED,
584 &pol);
586 if (!NT_STATUS_IS_OK(result))
587 goto done;
589 result = rpccli_lsa_EnumPrivs(cli, mem_ctx,
590 &pol,
591 &enum_context,
592 &priv_array,
593 pref_max_length);
594 if (!NT_STATUS_IS_OK(result))
595 goto done;
597 /* Print results */
598 printf("found %d privileges\n\n", priv_array.count);
600 for (i = 0; i < priv_array.count; i++) {
601 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
602 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
603 priv_array.privs[i].luid.high,
604 priv_array.privs[i].luid.low,
605 priv_array.privs[i].luid.high,
606 priv_array.privs[i].luid.low);
609 rpccli_lsa_Close(cli, mem_ctx, &pol);
610 done:
611 return result;
614 /* Get privilege name */
616 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli,
617 TALLOC_CTX *mem_ctx, int argc,
618 const char **argv)
620 struct policy_handle pol;
621 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
623 uint16 lang_id=0;
624 uint16 lang_id_sys=0;
625 uint16 lang_id_desc;
626 struct lsa_String lsa_name;
627 struct lsa_StringLarge *description = NULL;
629 if (argc != 2) {
630 printf("Usage: %s privilege name\n", argv[0]);
631 return NT_STATUS_OK;
634 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
635 SEC_FLAG_MAXIMUM_ALLOWED,
636 &pol);
638 if (!NT_STATUS_IS_OK(result))
639 goto done;
641 init_lsa_String(&lsa_name, argv[1]);
643 result = rpccli_lsa_LookupPrivDisplayName(cli, mem_ctx,
644 &pol,
645 &lsa_name,
646 lang_id,
647 lang_id_sys,
648 &description,
649 &lang_id_desc);
651 if (!NT_STATUS_IS_OK(result))
652 goto done;
654 /* Print results */
655 printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
657 rpccli_lsa_Close(cli, mem_ctx, &pol);
658 done:
659 return result;
662 /* Enumerate the LSA SIDS */
664 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli,
665 TALLOC_CTX *mem_ctx, int argc,
666 const char **argv)
668 struct policy_handle pol;
669 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
671 uint32 enum_context=0;
672 uint32 pref_max_length=0x1000;
673 struct lsa_SidArray sid_array;
674 int i;
676 if (argc > 3) {
677 printf("Usage: %s [enum context] [max length]\n", argv[0]);
678 return NT_STATUS_OK;
681 if (argc>=2)
682 enum_context=atoi(argv[1]);
684 if (argc==3)
685 pref_max_length=atoi(argv[2]);
687 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
688 SEC_FLAG_MAXIMUM_ALLOWED,
689 &pol);
691 if (!NT_STATUS_IS_OK(result))
692 goto done;
694 result = rpccli_lsa_EnumAccounts(cli, mem_ctx,
695 &pol,
696 &enum_context,
697 &sid_array,
698 pref_max_length);
700 if (!NT_STATUS_IS_OK(result))
701 goto done;
703 /* Print results */
704 printf("found %d SIDs\n\n", sid_array.num_sids);
706 for (i = 0; i < sid_array.num_sids; i++) {
707 fstring sid_str;
709 sid_to_fstring(sid_str, sid_array.sids[i].sid);
710 printf("%s\n", sid_str);
713 rpccli_lsa_Close(cli, mem_ctx, &pol);
714 done:
715 return result;
718 /* Create a new account */
720 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli,
721 TALLOC_CTX *mem_ctx, int argc,
722 const char **argv)
724 struct policy_handle dom_pol;
725 struct policy_handle user_pol;
726 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
727 uint32 des_access = 0x000f000f;
729 DOM_SID sid;
731 if (argc != 2 ) {
732 printf("Usage: %s SID\n", argv[0]);
733 return NT_STATUS_OK;
736 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
737 if (!NT_STATUS_IS_OK(result))
738 goto done;
740 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
741 SEC_FLAG_MAXIMUM_ALLOWED,
742 &dom_pol);
744 if (!NT_STATUS_IS_OK(result))
745 goto done;
747 result = rpccli_lsa_CreateAccount(cli, mem_ctx,
748 &dom_pol,
749 &sid,
750 des_access,
751 &user_pol);
753 if (!NT_STATUS_IS_OK(result))
754 goto done;
756 printf("Account for SID %s successfully created\n\n", argv[1]);
757 result = NT_STATUS_OK;
759 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
760 done:
761 return result;
765 /* Enumerate the privileges of an SID */
767 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli,
768 TALLOC_CTX *mem_ctx, int argc,
769 const char **argv)
771 struct policy_handle dom_pol;
772 struct policy_handle user_pol;
773 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
774 uint32 access_desired = 0x000f000f;
775 DOM_SID sid;
776 struct lsa_PrivilegeSet *privs = NULL;
777 int i;
779 if (argc != 2 ) {
780 printf("Usage: %s SID\n", argv[0]);
781 return NT_STATUS_OK;
784 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
785 if (!NT_STATUS_IS_OK(result))
786 goto done;
788 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
789 SEC_FLAG_MAXIMUM_ALLOWED,
790 &dom_pol);
792 if (!NT_STATUS_IS_OK(result))
793 goto done;
795 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
796 &dom_pol,
797 &sid,
798 access_desired,
799 &user_pol);
801 if (!NT_STATUS_IS_OK(result))
802 goto done;
804 result = rpccli_lsa_EnumPrivsAccount(cli, mem_ctx,
805 &user_pol,
806 &privs);
808 if (!NT_STATUS_IS_OK(result))
809 goto done;
811 /* Print results */
812 printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
813 printf("high\tlow\tattribute\n");
815 for (i = 0; i < privs->count; i++) {
816 printf("%u\t%u\t%u\n",
817 privs->set[i].luid.high,
818 privs->set[i].luid.low,
819 privs->set[i].attribute);
822 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
823 done:
824 return result;
828 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
830 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli,
831 TALLOC_CTX *mem_ctx, int argc,
832 const char **argv)
834 struct policy_handle dom_pol;
835 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
836 DOM_SID sid;
837 struct lsa_RightSet rights;
839 int i;
841 if (argc != 2 ) {
842 printf("Usage: %s SID\n", argv[0]);
843 return NT_STATUS_OK;
846 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
847 if (!NT_STATUS_IS_OK(result))
848 goto done;
850 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
851 SEC_FLAG_MAXIMUM_ALLOWED,
852 &dom_pol);
854 if (!NT_STATUS_IS_OK(result))
855 goto done;
857 result = rpccli_lsa_EnumAccountRights(cli, mem_ctx,
858 &dom_pol,
859 &sid,
860 &rights);
862 if (!NT_STATUS_IS_OK(result))
863 goto done;
865 printf("found %d privileges for SID %s\n", rights.count,
866 sid_string_tos(&sid));
868 for (i = 0; i < rights.count; i++) {
869 printf("\t%s\n", rights.names[i].string);
872 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
873 done:
874 return result;
878 /* add some privileges to a SID via LsaAddAccountRights */
880 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli,
881 TALLOC_CTX *mem_ctx, int argc,
882 const char **argv)
884 struct policy_handle dom_pol;
885 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
886 struct lsa_RightSet rights;
887 DOM_SID sid;
888 int i;
890 if (argc < 3 ) {
891 printf("Usage: %s SID [rights...]\n", argv[0]);
892 return NT_STATUS_OK;
895 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
896 if (!NT_STATUS_IS_OK(result))
897 goto done;
899 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
900 SEC_FLAG_MAXIMUM_ALLOWED,
901 &dom_pol);
903 if (!NT_STATUS_IS_OK(result))
904 goto done;
906 rights.count = argc-2;
907 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
908 rights.count);
909 if (!rights.names) {
910 return NT_STATUS_NO_MEMORY;
913 for (i=0; i<argc-2; i++) {
914 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
917 result = rpccli_lsa_AddAccountRights(cli, mem_ctx,
918 &dom_pol,
919 &sid,
920 &rights);
922 if (!NT_STATUS_IS_OK(result))
923 goto done;
925 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
926 done:
927 return result;
931 /* remove some privileges to a SID via LsaRemoveAccountRights */
933 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli,
934 TALLOC_CTX *mem_ctx, int argc,
935 const char **argv)
937 struct policy_handle dom_pol;
938 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
939 struct lsa_RightSet rights;
940 DOM_SID sid;
941 int i;
943 if (argc < 3 ) {
944 printf("Usage: %s SID [rights...]\n", argv[0]);
945 return NT_STATUS_OK;
948 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
949 if (!NT_STATUS_IS_OK(result))
950 goto done;
952 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
953 SEC_FLAG_MAXIMUM_ALLOWED,
954 &dom_pol);
956 if (!NT_STATUS_IS_OK(result))
957 goto done;
959 rights.count = argc-2;
960 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
961 rights.count);
962 if (!rights.names) {
963 return NT_STATUS_NO_MEMORY;
966 for (i=0; i<argc-2; i++) {
967 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
970 result = rpccli_lsa_RemoveAccountRights(cli, mem_ctx,
971 &dom_pol,
972 &sid,
973 false,
974 &rights);
976 if (!NT_STATUS_IS_OK(result))
977 goto done;
979 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
981 done:
982 return result;
986 /* Get a privilege value given its name */
988 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli,
989 TALLOC_CTX *mem_ctx, int argc,
990 const char **argv)
992 struct policy_handle pol;
993 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
994 struct lsa_LUID luid;
995 struct lsa_String name;
997 if (argc != 2 ) {
998 printf("Usage: %s name\n", argv[0]);
999 return NT_STATUS_OK;
1002 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1003 SEC_FLAG_MAXIMUM_ALLOWED,
1004 &pol);
1006 if (!NT_STATUS_IS_OK(result))
1007 goto done;
1009 init_lsa_String(&name, argv[1]);
1011 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1012 &pol,
1013 &name,
1014 &luid);
1016 if (!NT_STATUS_IS_OK(result))
1017 goto done;
1019 /* Print results */
1021 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
1023 rpccli_lsa_Close(cli, mem_ctx, &pol);
1024 done:
1025 return result;
1028 /* Query LSA security object */
1030 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli,
1031 TALLOC_CTX *mem_ctx, int argc,
1032 const char **argv)
1034 struct policy_handle pol;
1035 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1036 SEC_DESC_BUF *sdb;
1037 uint32 sec_info = DACL_SECURITY_INFORMATION;
1039 if (argc < 1 || argc > 2) {
1040 printf("Usage: %s [sec_info]\n", argv[0]);
1041 return NT_STATUS_OK;
1044 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1045 SEC_FLAG_MAXIMUM_ALLOWED,
1046 &pol);
1048 if (argc == 2)
1049 sscanf(argv[1], "%x", &sec_info);
1051 if (!NT_STATUS_IS_OK(result))
1052 goto done;
1054 result = rpccli_lsa_QuerySecurity(cli, mem_ctx,
1055 &pol,
1056 sec_info,
1057 &sdb);
1058 if (!NT_STATUS_IS_OK(result))
1059 goto done;
1061 /* Print results */
1063 display_sec_desc(sdb->sd);
1065 rpccli_lsa_Close(cli, mem_ctx, &pol);
1066 done:
1067 return result;
1070 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
1071 uint8_t session_key[16])
1073 char *pwd, *pwd_old;
1075 DATA_BLOB data = data_blob_const(p->password->data, p->password->length);
1076 DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
1077 DATA_BLOB session_key_blob = data_blob_const(session_key, sizeof(session_key));
1079 pwd = sess_decrypt_string(talloc_tos(), &data, &session_key_blob);
1080 pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key_blob);
1082 d_printf("Password:\t%s\n", pwd);
1083 d_printf("Old Password:\t%s\n", pwd_old);
1085 talloc_free(pwd);
1086 talloc_free(pwd_old);
1089 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
1090 union lsa_TrustedDomainInfo *info,
1091 enum lsa_TrustDomInfoEnum info_class,
1092 uint8_t nt_hash[16])
1094 switch (info_class) {
1095 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
1096 display_trust_dom_info_4(&info->password, nt_hash);
1097 break;
1098 default: {
1099 const char *str = NULL;
1100 str = NDR_PRINT_UNION_STRING(mem_ctx,
1101 lsa_TrustedDomainInfo,
1102 info_class, info);
1103 if (str) {
1104 d_printf("%s\n", str);
1106 break;
1111 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
1112 TALLOC_CTX *mem_ctx, int argc,
1113 const char **argv)
1115 struct policy_handle pol;
1116 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1117 DOM_SID dom_sid;
1118 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1119 union lsa_TrustedDomainInfo *info = NULL;
1120 enum lsa_TrustDomInfoEnum info_class = 1;
1121 uint8_t nt_hash[16];
1123 if (argc > 3 || argc < 2) {
1124 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1125 return NT_STATUS_OK;
1128 if (!string_to_sid(&dom_sid, argv[1]))
1129 return NT_STATUS_NO_MEMORY;
1131 if (argc == 3)
1132 info_class = atoi(argv[2]);
1134 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1136 if (!NT_STATUS_IS_OK(result))
1137 goto done;
1139 result = rpccli_lsa_QueryTrustedDomainInfoBySid(cli, mem_ctx,
1140 &pol,
1141 &dom_sid,
1142 info_class,
1143 &info);
1144 if (!NT_STATUS_IS_OK(result))
1145 goto done;
1147 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1148 d_fprintf(stderr, "Could not get pwd hash\n");
1149 goto done;
1152 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1154 done:
1155 rpccli_lsa_Close(cli, mem_ctx, &pol);
1157 return result;
1160 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1161 TALLOC_CTX *mem_ctx, int argc,
1162 const char **argv)
1164 struct policy_handle pol;
1165 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1166 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1167 union lsa_TrustedDomainInfo *info = NULL;
1168 enum lsa_TrustDomInfoEnum info_class = 1;
1169 struct lsa_String trusted_domain;
1170 uint8_t nt_hash[16];
1172 if (argc > 3 || argc < 2) {
1173 printf("Usage: %s [name] [info_class]\n", argv[0]);
1174 return NT_STATUS_OK;
1177 if (argc == 3)
1178 info_class = atoi(argv[2]);
1180 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1182 if (!NT_STATUS_IS_OK(result))
1183 goto done;
1185 init_lsa_String(&trusted_domain, argv[1]);
1187 result = rpccli_lsa_QueryTrustedDomainInfoByName(cli, mem_ctx,
1188 &pol,
1189 &trusted_domain,
1190 info_class,
1191 &info);
1192 if (!NT_STATUS_IS_OK(result))
1193 goto done;
1195 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1196 d_fprintf(stderr, "Could not get pwd hash\n");
1197 goto done;
1200 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1202 done:
1203 rpccli_lsa_Close(cli, mem_ctx, &pol);
1205 return result;
1208 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1209 TALLOC_CTX *mem_ctx, int argc,
1210 const char **argv)
1212 struct policy_handle pol, trustdom_pol;
1213 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1214 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1215 union lsa_TrustedDomainInfo *info = NULL;
1216 DOM_SID dom_sid;
1217 enum lsa_TrustDomInfoEnum info_class = 1;
1218 uint8_t nt_hash[16];
1220 if (argc > 3 || argc < 2) {
1221 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1222 return NT_STATUS_OK;
1225 if (!string_to_sid(&dom_sid, argv[1]))
1226 return NT_STATUS_NO_MEMORY;
1229 if (argc == 3)
1230 info_class = atoi(argv[2]);
1232 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1234 if (!NT_STATUS_IS_OK(result))
1235 goto done;
1237 result = rpccli_lsa_OpenTrustedDomain(cli, mem_ctx,
1238 &pol,
1239 &dom_sid,
1240 access_mask,
1241 &trustdom_pol);
1243 if (!NT_STATUS_IS_OK(result))
1244 goto done;
1246 result = rpccli_lsa_QueryTrustedDomainInfo(cli, mem_ctx,
1247 &trustdom_pol,
1248 info_class,
1249 &info);
1251 if (!NT_STATUS_IS_OK(result))
1252 goto done;
1254 if (!rpccli_get_pwd_hash(cli, nt_hash)) {
1255 d_fprintf(stderr, "Could not get pwd hash\n");
1256 goto done;
1259 display_trust_dom_info(mem_ctx, info, info_class, nt_hash);
1261 done:
1262 rpccli_lsa_Close(cli, mem_ctx, &pol);
1264 return result;
1267 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1268 TALLOC_CTX *mem_ctx, int argc,
1269 const char **argv)
1271 struct policy_handle pol;
1272 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1273 const char *servername = cli->desthost;
1274 struct lsa_String *account_name = NULL;
1275 struct lsa_String *authority_name = NULL;
1277 if (argc > 2) {
1278 printf("Usage: %s servername\n", argv[0]);
1279 return NT_STATUS_OK;
1282 result = rpccli_lsa_open_policy(cli, mem_ctx, true,
1283 SEC_FLAG_MAXIMUM_ALLOWED,
1284 &pol);
1286 if (!NT_STATUS_IS_OK(result)) {
1287 goto done;
1290 result = rpccli_lsa_GetUserName(cli, mem_ctx,
1291 servername,
1292 &account_name,
1293 &authority_name);
1294 if (!NT_STATUS_IS_OK(result)) {
1295 goto done;
1298 /* Print results */
1300 printf("Account Name: %s, Authority Name: %s\n",
1301 account_name->string, authority_name ? authority_name->string :
1302 "");
1304 rpccli_lsa_Close(cli, mem_ctx, &pol);
1305 done:
1306 return result;
1309 static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
1310 TALLOC_CTX *mem_ctx, int argc,
1311 const char **argv)
1313 struct policy_handle dom_pol, user_pol;
1314 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1315 struct lsa_PrivilegeSet privs;
1316 struct lsa_LUIDAttribute *set = NULL;
1317 DOM_SID sid;
1318 int i;
1320 ZERO_STRUCT(privs);
1322 if (argc < 3 ) {
1323 printf("Usage: %s SID [rights...]\n", argv[0]);
1324 return NT_STATUS_OK;
1327 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1328 if (!NT_STATUS_IS_OK(result)) {
1329 goto done;
1332 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1333 SEC_FLAG_MAXIMUM_ALLOWED,
1334 &dom_pol);
1336 if (!NT_STATUS_IS_OK(result)) {
1337 goto done;
1340 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
1341 &dom_pol,
1342 &sid,
1343 SEC_FLAG_MAXIMUM_ALLOWED,
1344 &user_pol);
1346 if (!NT_STATUS_IS_OK(result)) {
1347 goto done;
1350 for (i=2; i<argc; i++) {
1352 struct lsa_String priv_name;
1353 struct lsa_LUID luid;
1355 init_lsa_String(&priv_name, argv[i]);
1357 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1358 &dom_pol,
1359 &priv_name,
1360 &luid);
1361 if (!NT_STATUS_IS_OK(result)) {
1362 continue;
1365 privs.count++;
1366 set = TALLOC_REALLOC_ARRAY(mem_ctx, set,
1367 struct lsa_LUIDAttribute,
1368 privs.count);
1369 if (!set) {
1370 return NT_STATUS_NO_MEMORY;
1373 set[privs.count-1].luid = luid;
1374 set[privs.count-1].attribute = 0;
1377 privs.set = set;
1379 result = rpccli_lsa_AddPrivilegesToAccount(cli, mem_ctx,
1380 &user_pol,
1381 &privs);
1383 if (!NT_STATUS_IS_OK(result)) {
1384 goto done;
1387 rpccli_lsa_Close(cli, mem_ctx, &user_pol);
1388 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
1389 done:
1390 return result;
1393 static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
1394 TALLOC_CTX *mem_ctx, int argc,
1395 const char **argv)
1397 struct policy_handle dom_pol, user_pol;
1398 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1399 struct lsa_PrivilegeSet privs;
1400 struct lsa_LUIDAttribute *set = NULL;
1401 DOM_SID sid;
1402 int i;
1404 ZERO_STRUCT(privs);
1406 if (argc < 3 ) {
1407 printf("Usage: %s SID [rights...]\n", argv[0]);
1408 return NT_STATUS_OK;
1411 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1412 if (!NT_STATUS_IS_OK(result)) {
1413 goto done;
1416 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1417 SEC_FLAG_MAXIMUM_ALLOWED,
1418 &dom_pol);
1420 if (!NT_STATUS_IS_OK(result)) {
1421 goto done;
1424 result = rpccli_lsa_OpenAccount(cli, mem_ctx,
1425 &dom_pol,
1426 &sid,
1427 SEC_FLAG_MAXIMUM_ALLOWED,
1428 &user_pol);
1430 if (!NT_STATUS_IS_OK(result)) {
1431 goto done;
1434 for (i=2; i<argc; i++) {
1436 struct lsa_String priv_name;
1437 struct lsa_LUID luid;
1439 init_lsa_String(&priv_name, argv[i]);
1441 result = rpccli_lsa_LookupPrivValue(cli, mem_ctx,
1442 &dom_pol,
1443 &priv_name,
1444 &luid);
1445 if (!NT_STATUS_IS_OK(result)) {
1446 continue;
1449 privs.count++;
1450 set = TALLOC_REALLOC_ARRAY(mem_ctx, set,
1451 struct lsa_LUIDAttribute,
1452 privs.count);
1453 if (!set) {
1454 return NT_STATUS_NO_MEMORY;
1457 set[privs.count-1].luid = luid;
1458 set[privs.count-1].attribute = 0;
1461 privs.set = set;
1464 result = rpccli_lsa_RemovePrivilegesFromAccount(cli, mem_ctx,
1465 &user_pol,
1466 false,
1467 &privs);
1469 if (!NT_STATUS_IS_OK(result)) {
1470 goto done;
1473 rpccli_lsa_Close(cli, mem_ctx, &user_pol);
1474 rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
1475 done:
1476 return result;
1479 static NTSTATUS cmd_lsa_create_secret(struct rpc_pipe_client *cli,
1480 TALLOC_CTX *mem_ctx, int argc,
1481 const char **argv)
1483 NTSTATUS status;
1484 struct policy_handle handle, sec_handle;
1485 struct lsa_String name;
1487 if (argc < 2) {
1488 printf("Usage: %s name\n", argv[0]);
1489 return NT_STATUS_OK;
1492 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1493 true,
1494 SEC_FLAG_MAXIMUM_ALLOWED,
1495 &handle);
1496 if (!NT_STATUS_IS_OK(status)) {
1497 return status;
1500 init_lsa_String(&name, argv[1]);
1502 status = rpccli_lsa_CreateSecret(cli, mem_ctx,
1503 &handle,
1504 name,
1505 SEC_FLAG_MAXIMUM_ALLOWED,
1506 &sec_handle);
1507 if (!NT_STATUS_IS_OK(status)) {
1508 goto done;
1511 done:
1512 if (is_valid_policy_hnd(&sec_handle)) {
1513 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1515 if (is_valid_policy_hnd(&handle)) {
1516 rpccli_lsa_Close(cli, mem_ctx, &handle);
1519 return status;
1522 static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
1523 TALLOC_CTX *mem_ctx, int argc,
1524 const char **argv)
1526 NTSTATUS status;
1527 struct policy_handle handle, sec_handle;
1528 struct lsa_String name;
1530 if (argc < 2) {
1531 printf("Usage: %s name\n", argv[0]);
1532 return NT_STATUS_OK;
1535 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1536 true,
1537 SEC_FLAG_MAXIMUM_ALLOWED,
1538 &handle);
1539 if (!NT_STATUS_IS_OK(status)) {
1540 return status;
1543 init_lsa_String(&name, argv[1]);
1545 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1546 &handle,
1547 name,
1548 SEC_FLAG_MAXIMUM_ALLOWED,
1549 &sec_handle);
1550 if (!NT_STATUS_IS_OK(status)) {
1551 goto done;
1554 status = rpccli_lsa_DeleteObject(cli, mem_ctx,
1555 &sec_handle);
1556 if (!NT_STATUS_IS_OK(status)) {
1557 goto done;
1560 done:
1561 if (is_valid_policy_hnd(&sec_handle)) {
1562 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1564 if (is_valid_policy_hnd(&handle)) {
1565 rpccli_lsa_Close(cli, mem_ctx, &handle);
1568 return status;
1571 static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
1572 TALLOC_CTX *mem_ctx, int argc,
1573 const char **argv)
1575 NTSTATUS status;
1576 struct policy_handle handle, sec_handle;
1577 struct lsa_String name;
1578 struct lsa_DATA_BUF_PTR new_val;
1579 NTTIME new_mtime = 0;
1580 struct lsa_DATA_BUF_PTR old_val;
1581 NTTIME old_mtime = 0;
1582 DATA_BLOB session_key;
1583 DATA_BLOB new_blob = data_blob_null;
1584 DATA_BLOB old_blob = data_blob_null;
1585 char *new_secret, *old_secret;
1587 if (argc < 2) {
1588 printf("Usage: %s name\n", argv[0]);
1589 return NT_STATUS_OK;
1592 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1593 true,
1594 SEC_FLAG_MAXIMUM_ALLOWED,
1595 &handle);
1596 if (!NT_STATUS_IS_OK(status)) {
1597 return status;
1600 init_lsa_String(&name, argv[1]);
1602 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1603 &handle,
1604 name,
1605 SEC_FLAG_MAXIMUM_ALLOWED,
1606 &sec_handle);
1607 if (!NT_STATUS_IS_OK(status)) {
1608 goto done;
1611 ZERO_STRUCT(new_val);
1612 ZERO_STRUCT(old_val);
1614 status = rpccli_lsa_QuerySecret(cli, mem_ctx,
1615 &sec_handle,
1616 &new_val,
1617 &new_mtime,
1618 &old_val,
1619 &old_mtime);
1620 if (!NT_STATUS_IS_OK(status)) {
1621 goto done;
1624 status = cli_get_session_key(mem_ctx, cli, &session_key);
1625 if (!NT_STATUS_IS_OK(status)) {
1626 goto done;
1629 if (new_val.buf) {
1630 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
1632 if (old_val.buf) {
1633 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
1636 new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
1637 old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
1638 if (new_secret) {
1639 d_printf("new secret: %s\n", new_secret);
1641 if (old_secret) {
1642 d_printf("old secret: %s\n", old_secret);
1645 done:
1646 if (is_valid_policy_hnd(&sec_handle)) {
1647 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1649 if (is_valid_policy_hnd(&handle)) {
1650 rpccli_lsa_Close(cli, mem_ctx, &handle);
1653 return status;
1656 static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
1657 TALLOC_CTX *mem_ctx, int argc,
1658 const char **argv)
1660 NTSTATUS status;
1661 struct policy_handle handle, sec_handle;
1662 struct lsa_String name;
1663 struct lsa_DATA_BUF new_val;
1664 struct lsa_DATA_BUF old_val;
1665 DATA_BLOB enc_key;
1666 DATA_BLOB session_key;
1668 if (argc < 3) {
1669 printf("Usage: %s name secret\n", argv[0]);
1670 return NT_STATUS_OK;
1673 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1674 true,
1675 SEC_FLAG_MAXIMUM_ALLOWED,
1676 &handle);
1677 if (!NT_STATUS_IS_OK(status)) {
1678 return status;
1681 init_lsa_String(&name, argv[1]);
1683 status = rpccli_lsa_OpenSecret(cli, mem_ctx,
1684 &handle,
1685 name,
1686 SEC_FLAG_MAXIMUM_ALLOWED,
1687 &sec_handle);
1688 if (!NT_STATUS_IS_OK(status)) {
1689 goto done;
1692 ZERO_STRUCT(new_val);
1693 ZERO_STRUCT(old_val);
1695 status = cli_get_session_key(mem_ctx, cli, &session_key);
1696 if (!NT_STATUS_IS_OK(status)) {
1697 goto done;
1700 enc_key = sess_encrypt_string(argv[2], &session_key);
1702 new_val.length = enc_key.length;
1703 new_val.size = enc_key.length;
1704 new_val.data = enc_key.data;
1706 status = rpccli_lsa_SetSecret(cli, mem_ctx,
1707 &sec_handle,
1708 &new_val,
1709 NULL);
1710 if (!NT_STATUS_IS_OK(status)) {
1711 goto done;
1714 done:
1715 if (is_valid_policy_hnd(&sec_handle)) {
1716 rpccli_lsa_Close(cli, mem_ctx, &sec_handle);
1718 if (is_valid_policy_hnd(&handle)) {
1719 rpccli_lsa_Close(cli, mem_ctx, &handle);
1722 return status;
1725 static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
1726 TALLOC_CTX *mem_ctx, int argc,
1727 const char **argv)
1729 NTSTATUS status;
1730 struct policy_handle handle;
1731 struct lsa_String name;
1732 struct lsa_DATA_BUF *val;
1733 DATA_BLOB session_key;
1734 DATA_BLOB blob = data_blob_null;
1735 char *secret;
1737 if (argc < 2) {
1738 printf("Usage: %s name\n", argv[0]);
1739 return NT_STATUS_OK;
1742 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1743 true,
1744 SEC_FLAG_MAXIMUM_ALLOWED,
1745 &handle);
1746 if (!NT_STATUS_IS_OK(status)) {
1747 return status;
1750 init_lsa_String(&name, argv[1]);
1752 ZERO_STRUCT(val);
1754 status = rpccli_lsa_RetrievePrivateData(cli, mem_ctx,
1755 &handle,
1756 &name,
1757 &val);
1758 if (!NT_STATUS_IS_OK(status)) {
1759 goto done;
1762 status = cli_get_session_key(mem_ctx, cli, &session_key);
1763 if (!NT_STATUS_IS_OK(status)) {
1764 goto done;
1767 if (val) {
1768 blob = data_blob_const(val->data, val->length);
1771 secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
1772 if (secret) {
1773 d_printf("secret: %s\n", secret);
1776 done:
1777 if (is_valid_policy_hnd(&handle)) {
1778 rpccli_lsa_Close(cli, mem_ctx, &handle);
1781 return status;
1784 static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
1785 TALLOC_CTX *mem_ctx, int argc,
1786 const char **argv)
1788 NTSTATUS status;
1789 struct policy_handle handle;
1790 struct lsa_String name;
1791 struct lsa_DATA_BUF val;
1792 DATA_BLOB session_key;
1793 DATA_BLOB enc_key;
1795 if (argc < 3) {
1796 printf("Usage: %s name secret\n", argv[0]);
1797 return NT_STATUS_OK;
1800 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1801 true,
1802 SEC_FLAG_MAXIMUM_ALLOWED,
1803 &handle);
1804 if (!NT_STATUS_IS_OK(status)) {
1805 return status;
1808 init_lsa_String(&name, argv[1]);
1810 ZERO_STRUCT(val);
1812 status = cli_get_session_key(mem_ctx, cli, &session_key);
1813 if (!NT_STATUS_IS_OK(status)) {
1814 goto done;
1817 enc_key = sess_encrypt_string(argv[2], &session_key);
1819 val.length = enc_key.length;
1820 val.size = enc_key.length;
1821 val.data = enc_key.data;
1823 status = rpccli_lsa_StorePrivateData(cli, mem_ctx,
1824 &handle,
1825 &name,
1826 &val);
1827 if (!NT_STATUS_IS_OK(status)) {
1828 goto done;
1831 done:
1832 if (is_valid_policy_hnd(&handle)) {
1833 rpccli_lsa_Close(cli, mem_ctx, &handle);
1836 return status;
1840 /* List of commands exported by this module */
1842 struct cmd_set lsarpc_commands[] = {
1844 { "LSARPC" },
1846 { "lsaquery", RPC_RTYPE_NTSTATUS, cmd_lsa_query_info_policy, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query info policy", "" },
1847 { "lookupsids", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert SIDs to names", "" },
1848 { "lookupsids3", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids3, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert SIDs to names", "" },
1849 { "lookupnames", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1850 { "lookupnames4", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names4, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1851 { "lookupnames_level", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names_level, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Convert names to SIDs", "" },
1852 { "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)]" },
1853 { "enumprivs", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privilege, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate privileges", "" },
1854 { "getdispname", RPC_RTYPE_NTSTATUS, cmd_lsa_get_dispname, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get the privilege name", "" },
1855 { "lsaenumsid", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_sids, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the LSA SIDS", "" },
1856 { "lsacreateaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_create_account, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create a new lsa account", "" },
1857 { "lsaenumprivsaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privsaccounts, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the privileges of an SID", "" },
1858 { "lsaenumacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Enumerate the rights of an SID", "" },
1859 { "lsaaddpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_add_priv, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Assign a privilege to a SID", "" },
1860 { "lsadelpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_del_priv, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Revoke a privilege from a SID", "" },
1861 { "lsaaddacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_add_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Add rights to an account", "" },
1862 { "lsaremoveacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_remove_acct_rights, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Remove rights from an account", "" },
1863 { "lsalookupprivvalue", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_priv_value, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get a privilege value given its name", "" },
1864 { "lsaquerysecobj", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secobj, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA security object", "" },
1865 { "lsaquerytrustdominfo",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfo, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a SID)", "" },
1866 { "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", "" },
1867 { "lsaquerytrustdominfobysid",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobysid, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query LSA trusted domains info (given a SID)", "" },
1868 { "getusername", RPC_RTYPE_NTSTATUS, cmd_lsa_get_username, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Get username", "" },
1869 { "createsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_create_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Create Secret", "" },
1870 { "deletesecret", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Delete Secret", "" },
1871 { "querysecret", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Query Secret", "" },
1872 { "setsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_set_secret, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Set Secret", "" },
1873 { "retrieveprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_retrieve_private_data, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Retrieve Private Data", "" },
1874 { "storeprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_store_private_data, NULL, &ndr_table_lsarpc.syntax_id, NULL, "Store Private Data", "" },
1876 { NULL }