2 Unix SMB/CIFS implementation.
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/>.
24 #include "rpcclient.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_lsa.h"
27 #include "../librpc/gen_ndr/ndr_lsa_c.h"
28 #include "rpc_client/cli_lsarpc.h"
29 #include "rpc_client/init_lsa.h"
30 #include "../libcli/security/security.h"
32 /* useful function to allow entering a name instead of a SID and
33 * looking it up automatically */
34 static NTSTATUS name_to_sid(struct rpc_pipe_client *cli,
36 struct dom_sid *sid, const char *name)
38 struct policy_handle pol;
39 enum lsa_SidType *sid_types;
40 NTSTATUS status, result;
42 struct dcerpc_binding_handle *b = cli->binding_handle;
44 /* maybe its a raw SID */
45 if (strncmp(name, "S-", 2) == 0 &&
46 string_to_sid(sid, name)) {
50 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
51 SEC_FLAG_MAXIMUM_ALLOWED,
53 if (!NT_STATUS_IS_OK(status))
56 status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, NULL, 1, &sids, &sid_types);
57 if (!NT_STATUS_IS_OK(status))
60 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
68 static void display_query_info_1(struct lsa_AuditLogInfo *r)
70 d_printf("percent_full:\t%d\n", r->percent_full);
71 d_printf("maximum_log_size:\t%d\n", r->maximum_log_size);
72 d_printf("retention_time:\t%lld\n", (long long)r->retention_time);
73 d_printf("shutdown_in_progress:\t%d\n", r->shutdown_in_progress);
74 d_printf("time_to_shutdown:\t%lld\n", (long long)r->time_to_shutdown);
75 d_printf("next_audit_record:\t%d\n", r->next_audit_record);
78 static void display_query_info_2(struct lsa_AuditEventsInfo *r)
81 d_printf("Auditing enabled:\t%d\n", r->auditing_mode);
82 d_printf("Auditing categories:\t%d\n", r->count);
83 d_printf("Auditsettings:\n");
84 for (i=0; i<r->count; i++) {
85 const char *val = audit_policy_str(talloc_tos(), r->settings[i]);
86 const char *policy = audit_description_str(i);
87 d_printf("%s:\t%s\n", policy, val);
91 static void display_query_info_3(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_5(struct lsa_DomainInfo *r)
99 d_printf("Domain Name: %s\n", r->name.string);
100 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
103 static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
105 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
108 static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
110 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
111 d_printf("Log is full: %d\n", r->log_is_full);
114 static void display_query_info_12(struct lsa_DnsDomainInfo *r)
116 d_printf("Domain NetBios Name: %s\n", r->name.string);
117 d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
118 d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
119 d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
120 d_printf("Domain GUID: %s\n", GUID_string(talloc_tos(),
124 static void display_lsa_query_info(union lsa_PolicyInformation *info,
125 enum lsa_PolicyInfo level)
129 display_query_info_1(&info->audit_log);
132 display_query_info_2(&info->audit_events);
135 display_query_info_3(&info->domain);
138 display_query_info_5(&info->account_domain);
141 display_query_info_10(&info->auditfullset);
144 display_query_info_11(&info->auditfullquery);
147 display_query_info_12(&info->dns);
150 printf("can't display info level: %d\n", level);
155 static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli,
156 TALLOC_CTX *mem_ctx, int argc,
159 struct policy_handle pol;
160 NTSTATUS status, result;
161 union lsa_PolicyInformation *info = NULL;
162 struct dcerpc_binding_handle *b = cli->binding_handle;
164 uint32_t info_class = 3;
167 printf("Usage: %s [info_class]\n", argv[0]);
172 info_class = atoi(argv[1]);
174 switch (info_class) {
176 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
177 SEC_FLAG_MAXIMUM_ALLOWED,
180 if (!NT_STATUS_IS_OK(status))
183 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
190 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
191 SEC_FLAG_MAXIMUM_ALLOWED,
194 if (!NT_STATUS_IS_OK(status))
197 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
204 if (!NT_STATUS_IS_OK(status)) {
208 if (NT_STATUS_IS_OK(result)) {
209 display_lsa_query_info(info, info_class);
212 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
218 /* Resolve a list of names to a list of sids */
220 static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli,
221 TALLOC_CTX *mem_ctx, int argc,
224 struct policy_handle pol;
225 NTSTATUS status, result;
226 struct dom_sid *sids;
227 enum lsa_SidType *types;
229 struct dcerpc_binding_handle *b = cli->binding_handle;
232 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
236 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
237 SEC_FLAG_MAXIMUM_ALLOWED,
240 if (!NT_STATUS_IS_OK(status))
243 status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
244 (const char**)(argv + 1), NULL, 1, &sids, &types);
246 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
247 NT_STATUS_V(STATUS_SOME_UNMAPPED))
250 status = NT_STATUS_OK;
254 for (i = 0; i < (argc - 1); i++) {
256 sid_to_fstring(sid_str, &sids[i]);
257 printf("%s %s (%s: %d)\n", argv[i + 1], sid_str,
258 sid_type_lookup(types[i]), types[i]);
261 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
267 /* Resolve a list of names to a list of sids */
269 static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli,
270 TALLOC_CTX *mem_ctx, int argc,
273 struct policy_handle pol;
274 NTSTATUS status, result;
275 struct dom_sid *sids = NULL;
276 enum lsa_SidType *types = NULL;
278 struct dcerpc_binding_handle *b = cli->binding_handle;
281 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
285 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
286 SEC_FLAG_MAXIMUM_ALLOWED,
288 if (!NT_STATUS_IS_OK(status)) {
292 level = atoi(argv[1]);
294 status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2,
295 (const char**)(argv + 2), NULL, level, &sids, &types);
297 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
298 NT_STATUS_V(STATUS_SOME_UNMAPPED))
303 status = NT_STATUS_OK;
307 for (i = 0; i < (argc - 2); i++) {
309 sid_to_fstring(sid_str, &sids[i]);
310 printf("%s %s (%s: %d)\n", argv[i + 2], sid_str,
311 sid_type_lookup(types[i]), types[i]);
314 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
320 static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
321 TALLOC_CTX *mem_ctx, int argc,
324 NTSTATUS status, result;
327 struct lsa_String *names;
328 struct lsa_RefDomainList *domains = NULL;
329 struct lsa_TransSidArray3 sids;
332 struct dcerpc_binding_handle *b = cli->binding_handle;
335 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
342 names = talloc_array(mem_ctx, struct lsa_String, num_names);
343 NT_STATUS_HAVE_NO_MEMORY(names);
345 for (i=0; i < num_names; i++) {
346 init_lsa_String(&names[i], argv[i+1]);
349 status = dcerpc_lsa_LookupNames4(b, mem_ctx,
359 if (!NT_STATUS_IS_OK(status)) {
362 if (!NT_STATUS_IS_OK(result)) {
366 if (sids.count != num_names) {
367 return NT_STATUS_INVALID_NETWORK_RESPONSE;
370 for (i = 0; i < sids.count; i++) {
372 sid_to_fstring(sid_str, sids.sids[i].sid);
373 printf("%s %s (%s: %d)\n", argv[i+1], sid_str,
374 sid_type_lookup(sids.sids[i].sid_type),
375 sids.sids[i].sid_type);
381 /* Resolve a list of SIDs to a list of names */
383 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
384 int argc, const char **argv)
386 struct policy_handle pol;
387 NTSTATUS status, result;
388 struct dom_sid *sids;
391 enum lsa_SidType *types;
393 struct dcerpc_binding_handle *b = cli->binding_handle;
396 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
400 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
401 SEC_FLAG_MAXIMUM_ALLOWED,
404 if (!NT_STATUS_IS_OK(status))
407 /* Convert arguments to sids */
409 sids = talloc_array(mem_ctx, struct dom_sid, argc - 1);
412 printf("could not allocate memory for %d sids\n", argc - 1);
416 for (i = 0; i < argc - 1; i++)
417 if (!string_to_sid(&sids[i], argv[i + 1])) {
418 status = NT_STATUS_INVALID_SID;
422 /* Lookup the SIDs */
424 status = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
425 &domains, &names, &types);
427 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
428 NT_STATUS_V(STATUS_SOME_UNMAPPED))
431 status = NT_STATUS_OK;
435 for (i = 0; i < (argc - 1); i++) {
438 sid_to_fstring(sid_str, &sids[i]);
439 if (types[i] == SID_NAME_DOMAIN) {
440 printf("%s %s (%d)\n", sid_str,
441 domains[i] ? domains[i] : "*unknown*",
444 printf("%s %s\\%s (%d)\n", sid_str,
445 domains[i] ? domains[i] : "*unknown*",
446 names[i] ? names[i] : "*unknown*",
451 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
457 /* Resolve a list of SIDs to a list of names */
459 static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
461 int argc, const char **argv)
463 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
465 struct lsa_SidArray sids;
466 struct lsa_RefDomainList *domains = NULL;
467 struct lsa_TransNameArray2 names;
469 struct dcerpc_binding_handle *b = cli->binding_handle;
472 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
478 /* Convert arguments to sids */
480 sids.num_sids = argc-1;
481 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
483 printf("could not allocate memory for %d sids\n", sids.num_sids);
487 for (i = 0; i < sids.num_sids; i++) {
488 sids.sids[i].sid = talloc(sids.sids, struct dom_sid);
489 if (sids.sids[i].sid == NULL) {
490 status = NT_STATUS_NO_MEMORY;
493 if (!string_to_sid(sids.sids[i].sid, argv[i+1])) {
494 status = NT_STATUS_INVALID_SID;
499 /* Lookup the SIDs */
500 status = dcerpc_lsa_LookupSids3(b, mem_ctx,
509 if (!NT_STATUS_IS_OK(status)) {
512 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
513 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
518 status = NT_STATUS_OK;
522 for (i = 0; i < names.count; i++) {
525 if (i >= sids.num_sids) {
528 sid_to_fstring(sid_str, sids.sids[i].sid);
529 printf("%s %s (%d)\n", sid_str,
530 names.names[i].name.string,
531 names.names[i].sid_type);
539 /* Enumerate list of trusted domains */
541 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
542 TALLOC_CTX *mem_ctx, int argc,
545 struct policy_handle pol;
546 NTSTATUS status, result;
547 struct lsa_DomainList domain_list;
548 struct dcerpc_binding_handle *b = cli->binding_handle;
550 /* defaults, but may be changed using params */
551 uint32_t enum_ctx = 0;
553 uint32_t max_size = (uint32_t)-1;
556 printf("Usage: %s [enum context (0)]\n", argv[0]);
560 if (argc == 2 && argv[1]) {
561 enum_ctx = atoi(argv[2]);
564 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
565 LSA_POLICY_VIEW_LOCAL_INFORMATION,
568 if (!NT_STATUS_IS_OK(status))
571 status = STATUS_MORE_ENTRIES;
573 while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
575 /* Lookup list of trusted domains */
577 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
583 if (!NT_STATUS_IS_OK(status)) {
586 if (!NT_STATUS_IS_OK(result) &&
587 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
588 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
593 /* Print results: list of names and sids returned in this
595 for (i = 0; i < domain_list.count; i++) {
598 sid_to_fstring(sid_str, domain_list.domains[i].sid);
600 domain_list.domains[i].name.string ?
601 domain_list.domains[i].name.string : "*unknown*",
606 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
611 /* Enumerates privileges */
613 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli,
614 TALLOC_CTX *mem_ctx, int argc,
617 struct policy_handle pol;
618 NTSTATUS status, result;
619 struct lsa_PrivArray priv_array;
620 struct dcerpc_binding_handle *b = cli->binding_handle;
622 uint32_t enum_context=0;
623 uint32_t pref_max_length=0x1000;
627 printf("Usage: %s [enum context] [max length]\n", argv[0]);
632 enum_context=atoi(argv[1]);
635 pref_max_length=atoi(argv[2]);
637 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
638 SEC_FLAG_MAXIMUM_ALLOWED,
641 if (!NT_STATUS_IS_OK(status))
644 status = dcerpc_lsa_EnumPrivs(b, mem_ctx,
650 if (!NT_STATUS_IS_OK(status))
652 if (!NT_STATUS_IS_OK(result)) {
658 printf("found %d privileges\n\n", priv_array.count);
660 for (i = 0; i < priv_array.count; i++) {
661 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
662 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
663 priv_array.privs[i].luid.high,
664 priv_array.privs[i].luid.low,
665 priv_array.privs[i].luid.high,
666 priv_array.privs[i].luid.low);
669 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
674 /* Get privilege name */
676 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli,
677 TALLOC_CTX *mem_ctx, int argc,
680 struct policy_handle pol;
681 NTSTATUS status, result;
682 struct dcerpc_binding_handle *b = cli->binding_handle;
685 uint16_t lang_id_sys=0;
686 uint16_t lang_id_desc;
687 struct lsa_String lsa_name;
688 struct lsa_StringLarge *description = NULL;
691 printf("Usage: %s privilege name\n", argv[0]);
695 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
696 SEC_FLAG_MAXIMUM_ALLOWED,
699 if (!NT_STATUS_IS_OK(status))
702 init_lsa_String(&lsa_name, argv[1]);
704 status = dcerpc_lsa_LookupPrivDisplayName(b, mem_ctx,
712 if (!NT_STATUS_IS_OK(status))
714 if (!NT_STATUS_IS_OK(result)) {
720 printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
722 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
727 /* Enumerate the LSA SIDS */
729 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli,
730 TALLOC_CTX *mem_ctx, int argc,
733 struct policy_handle pol;
734 NTSTATUS status, result;
735 struct dcerpc_binding_handle *b = cli->binding_handle;
737 uint32_t enum_context=0;
738 uint32_t pref_max_length=0x1000;
739 struct lsa_SidArray sid_array;
743 printf("Usage: %s [enum context] [max length]\n", argv[0]);
748 enum_context=atoi(argv[1]);
751 pref_max_length=atoi(argv[2]);
753 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
754 SEC_FLAG_MAXIMUM_ALLOWED,
757 if (!NT_STATUS_IS_OK(status))
760 status = dcerpc_lsa_EnumAccounts(b, mem_ctx,
766 if (!NT_STATUS_IS_OK(status))
768 if (!NT_STATUS_IS_OK(result)) {
774 printf("found %d SIDs\n\n", sid_array.num_sids);
776 for (i = 0; i < sid_array.num_sids; i++) {
779 sid_to_fstring(sid_str, sid_array.sids[i].sid);
780 printf("%s\n", sid_str);
783 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
788 /* Create a new account */
790 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli,
791 TALLOC_CTX *mem_ctx, int argc,
794 struct policy_handle dom_pol;
795 struct policy_handle user_pol;
796 NTSTATUS status, result;
797 uint32_t des_access = 0x000f000f;
798 struct dcerpc_binding_handle *b = cli->binding_handle;
803 printf("Usage: %s SID\n", argv[0]);
807 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
808 if (!NT_STATUS_IS_OK(status))
811 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
812 SEC_FLAG_MAXIMUM_ALLOWED,
815 if (!NT_STATUS_IS_OK(status))
818 status = dcerpc_lsa_CreateAccount(b, mem_ctx,
824 if (!NT_STATUS_IS_OK(status))
826 if (!NT_STATUS_IS_OK(result)) {
831 printf("Account for SID %s successfully created\n\n", argv[1]);
832 status = NT_STATUS_OK;
834 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
840 /* Enumerate the privileges of an SID */
842 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli,
843 TALLOC_CTX *mem_ctx, int argc,
846 struct policy_handle dom_pol;
847 struct policy_handle user_pol;
848 NTSTATUS status, result;
849 uint32_t access_desired = 0x000f000f;
851 struct lsa_PrivilegeSet *privs = NULL;
853 struct dcerpc_binding_handle *b = cli->binding_handle;
856 printf("Usage: %s SID\n", argv[0]);
860 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
861 if (!NT_STATUS_IS_OK(status))
864 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
865 SEC_FLAG_MAXIMUM_ALLOWED,
868 if (!NT_STATUS_IS_OK(status))
871 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
877 if (!NT_STATUS_IS_OK(status))
879 if (!NT_STATUS_IS_OK(result)) {
884 status = dcerpc_lsa_EnumPrivsAccount(b, mem_ctx,
888 if (!NT_STATUS_IS_OK(status))
890 if (!NT_STATUS_IS_OK(result)) {
896 printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
897 printf("high\tlow\tattribute\n");
899 for (i = 0; i < privs->count; i++) {
900 printf("%u\t%u\t%u\n",
901 privs->set[i].luid.high,
902 privs->set[i].luid.low,
903 privs->set[i].attribute);
906 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
912 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
914 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli,
915 TALLOC_CTX *mem_ctx, int argc,
918 struct policy_handle dom_pol;
919 NTSTATUS status, result;
921 struct lsa_RightSet rights;
922 struct dcerpc_binding_handle *b = cli->binding_handle;
927 printf("Usage: %s SID\n", argv[0]);
931 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
932 if (!NT_STATUS_IS_OK(status))
935 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
936 SEC_FLAG_MAXIMUM_ALLOWED,
939 if (!NT_STATUS_IS_OK(status))
942 status = dcerpc_lsa_EnumAccountRights(b, mem_ctx,
947 if (!NT_STATUS_IS_OK(status))
949 if (!NT_STATUS_IS_OK(result)) {
954 printf("found %d privileges for SID %s\n", rights.count,
955 sid_string_tos(&sid));
957 for (i = 0; i < rights.count; i++) {
958 printf("\t%s\n", rights.names[i].string);
961 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
967 /* add some privileges to a SID via LsaAddAccountRights */
969 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli,
970 TALLOC_CTX *mem_ctx, int argc,
973 struct policy_handle dom_pol;
974 NTSTATUS status, result;
975 struct lsa_RightSet rights;
978 struct dcerpc_binding_handle *b = cli->binding_handle;
981 printf("Usage: %s SID [rights...]\n", argv[0]);
985 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
986 if (!NT_STATUS_IS_OK(status))
989 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
990 SEC_FLAG_MAXIMUM_ALLOWED,
993 if (!NT_STATUS_IS_OK(status))
996 rights.count = argc-2;
997 rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
1000 return NT_STATUS_NO_MEMORY;
1003 for (i=0; i<argc-2; i++) {
1004 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
1007 status = dcerpc_lsa_AddAccountRights(b, mem_ctx,
1012 if (!NT_STATUS_IS_OK(status))
1014 if (!NT_STATUS_IS_OK(result)) {
1019 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1025 /* remove some privileges to a SID via LsaRemoveAccountRights */
1027 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli,
1028 TALLOC_CTX *mem_ctx, int argc,
1031 struct policy_handle dom_pol;
1032 NTSTATUS status, result;
1033 struct lsa_RightSet rights;
1036 struct dcerpc_binding_handle *b = cli->binding_handle;
1039 printf("Usage: %s SID [rights...]\n", argv[0]);
1040 return NT_STATUS_OK;
1043 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1044 if (!NT_STATUS_IS_OK(status))
1047 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1048 SEC_FLAG_MAXIMUM_ALLOWED,
1051 if (!NT_STATUS_IS_OK(status))
1054 rights.count = argc-2;
1055 rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
1057 if (!rights.names) {
1058 return NT_STATUS_NO_MEMORY;
1061 for (i=0; i<argc-2; i++) {
1062 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
1065 status = dcerpc_lsa_RemoveAccountRights(b, mem_ctx,
1071 if (!NT_STATUS_IS_OK(status))
1073 if (!NT_STATUS_IS_OK(result)) {
1078 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1085 /* Get a privilege value given its name */
1087 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli,
1088 TALLOC_CTX *mem_ctx, int argc,
1091 struct policy_handle pol;
1092 NTSTATUS status, result;
1093 struct lsa_LUID luid;
1094 struct lsa_String name;
1095 struct dcerpc_binding_handle *b = cli->binding_handle;
1098 printf("Usage: %s name\n", argv[0]);
1099 return NT_STATUS_OK;
1102 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1103 SEC_FLAG_MAXIMUM_ALLOWED,
1106 if (!NT_STATUS_IS_OK(status))
1109 init_lsa_String(&name, argv[1]);
1111 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1116 if (!NT_STATUS_IS_OK(status))
1118 if (!NT_STATUS_IS_OK(result)) {
1125 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
1127 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1132 /* Query LSA security object */
1134 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli,
1135 TALLOC_CTX *mem_ctx, int argc,
1138 struct policy_handle pol;
1139 NTSTATUS status, result;
1140 struct sec_desc_buf *sdb;
1141 uint32_t sec_info = SECINFO_DACL;
1142 struct dcerpc_binding_handle *b = cli->binding_handle;
1144 if (argc < 1 || argc > 2) {
1145 printf("Usage: %s [sec_info]\n", argv[0]);
1146 return NT_STATUS_OK;
1149 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1150 SEC_FLAG_MAXIMUM_ALLOWED,
1154 sscanf(argv[1], "%x", &sec_info);
1156 if (!NT_STATUS_IS_OK(status))
1159 status = dcerpc_lsa_QuerySecurity(b, mem_ctx,
1164 if (!NT_STATUS_IS_OK(status))
1166 if (!NT_STATUS_IS_OK(result)) {
1173 display_sec_desc(sdb->sd);
1175 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1180 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
1181 DATA_BLOB session_key)
1183 char *pwd, *pwd_old;
1185 DATA_BLOB data = data_blob_const(p->password->data, p->password->length);
1186 DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
1188 pwd = sess_decrypt_string(talloc_tos(), &data, &session_key);
1189 pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key);
1191 d_printf("Password:\t%s\n", pwd);
1192 d_printf("Old Password:\t%s\n", pwd_old);
1195 talloc_free(pwd_old);
1198 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
1199 union lsa_TrustedDomainInfo *info,
1200 enum lsa_TrustDomInfoEnum info_class,
1201 DATA_BLOB session_key)
1203 switch (info_class) {
1204 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
1205 display_trust_dom_info_4(&info->password, session_key);
1208 const char *str = NULL;
1209 str = NDR_PRINT_UNION_STRING(mem_ctx,
1210 lsa_TrustedDomainInfo,
1213 d_printf("%s\n", str);
1220 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
1221 TALLOC_CTX *mem_ctx, int argc,
1224 struct policy_handle pol;
1225 NTSTATUS status, result;
1226 struct dom_sid dom_sid;
1227 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1228 union lsa_TrustedDomainInfo *info = NULL;
1229 enum lsa_TrustDomInfoEnum info_class = 1;
1230 DATA_BLOB session_key;
1231 struct dcerpc_binding_handle *b = cli->binding_handle;
1233 if (argc > 3 || argc < 2) {
1234 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1235 return NT_STATUS_OK;
1238 if (!string_to_sid(&dom_sid, argv[1]))
1239 return NT_STATUS_NO_MEMORY;
1242 info_class = atoi(argv[2]);
1244 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1246 if (!NT_STATUS_IS_OK(status))
1249 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
1255 if (!NT_STATUS_IS_OK(status))
1257 if (!NT_STATUS_IS_OK(result)) {
1262 status = cli_get_session_key(mem_ctx, cli, &session_key);
1263 if (!NT_STATUS_IS_OK(status)) {
1264 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1268 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1271 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1276 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1277 TALLOC_CTX *mem_ctx, int argc,
1280 struct policy_handle pol;
1281 NTSTATUS status, result;
1282 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1283 union lsa_TrustedDomainInfo *info = NULL;
1284 enum lsa_TrustDomInfoEnum info_class = 1;
1285 struct lsa_String trusted_domain;
1286 struct dcerpc_binding_handle *b = cli->binding_handle;
1287 DATA_BLOB session_key;
1289 if (argc > 3 || argc < 2) {
1290 printf("Usage: %s [name] [info_class]\n", argv[0]);
1291 return NT_STATUS_OK;
1295 info_class = atoi(argv[2]);
1297 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1299 if (!NT_STATUS_IS_OK(status))
1302 init_lsa_String(&trusted_domain, argv[1]);
1304 status = dcerpc_lsa_QueryTrustedDomainInfoByName(b, mem_ctx,
1310 if (!NT_STATUS_IS_OK(status))
1312 if (!NT_STATUS_IS_OK(result)) {
1317 status = cli_get_session_key(mem_ctx, cli, &session_key);
1318 if (!NT_STATUS_IS_OK(status)) {
1319 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1323 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1326 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1331 static NTSTATUS cmd_lsa_set_trustdominfo(struct rpc_pipe_client *cli,
1332 TALLOC_CTX *mem_ctx, int argc,
1335 struct policy_handle pol, trustdom_pol;
1336 NTSTATUS status, result;
1337 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1338 union lsa_TrustedDomainInfo info;
1339 struct dom_sid dom_sid;
1340 enum lsa_TrustDomInfoEnum info_class = 1;
1341 struct dcerpc_binding_handle *b = cli->binding_handle;
1343 if (argc > 4 || argc < 3) {
1344 printf("Usage: %s [sid] [info_class] [value]\n", argv[0]);
1345 return NT_STATUS_OK;
1348 if (!string_to_sid(&dom_sid, argv[1])) {
1349 return NT_STATUS_NO_MEMORY;
1353 info_class = atoi(argv[2]);
1355 switch (info_class) {
1356 case 13: /* LSA_TRUSTED_DOMAIN_SUPPORTED_ENCRYPTION_TYPES */
1357 info.enc_types.enc_types = atoi(argv[3]);
1360 return NT_STATUS_INVALID_PARAMETER;
1363 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1364 if (!NT_STATUS_IS_OK(status)) {
1368 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
1374 if (!NT_STATUS_IS_OK(status)) {
1377 if (!NT_STATUS_IS_OK(result)) {
1382 status = dcerpc_lsa_SetInformationTrustedDomain(b, mem_ctx,
1387 if (!NT_STATUS_IS_OK(status)) {
1390 if (!NT_STATUS_IS_OK(result)) {
1395 dcerpc_lsa_Close(b, mem_ctx, &trustdom_pol, &result);
1396 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1401 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1402 TALLOC_CTX *mem_ctx, int argc,
1405 struct policy_handle pol, trustdom_pol;
1406 NTSTATUS status, result;
1407 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1408 union lsa_TrustedDomainInfo *info = NULL;
1409 struct dom_sid dom_sid;
1410 enum lsa_TrustDomInfoEnum info_class = 1;
1411 DATA_BLOB session_key;
1412 struct dcerpc_binding_handle *b = cli->binding_handle;
1414 if (argc > 3 || argc < 2) {
1415 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1416 return NT_STATUS_OK;
1419 if (!string_to_sid(&dom_sid, argv[1]))
1420 return NT_STATUS_NO_MEMORY;
1424 info_class = atoi(argv[2]);
1426 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1428 if (!NT_STATUS_IS_OK(status))
1431 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
1437 if (!NT_STATUS_IS_OK(status))
1439 if (!NT_STATUS_IS_OK(result)) {
1444 status = dcerpc_lsa_QueryTrustedDomainInfo(b, mem_ctx,
1449 if (!NT_STATUS_IS_OK(status))
1451 if (!NT_STATUS_IS_OK(result)) {
1456 status = cli_get_session_key(mem_ctx, cli, &session_key);
1457 if (!NT_STATUS_IS_OK(status)) {
1458 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1462 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1465 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1470 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1471 TALLOC_CTX *mem_ctx, int argc,
1474 NTSTATUS status, result;
1475 const char *servername = cli->desthost;
1476 struct lsa_String *account_name = NULL;
1477 struct lsa_String *authority_name = NULL;
1478 struct dcerpc_binding_handle *b = cli->binding_handle;
1481 printf("Usage: %s servername\n", argv[0]);
1482 return NT_STATUS_OK;
1485 status = dcerpc_lsa_GetUserName(b, mem_ctx,
1490 if (!NT_STATUS_IS_OK(status)) {
1493 if (!NT_STATUS_IS_OK(result)) {
1500 printf("Account Name: %s, Authority Name: %s\n",
1501 account_name->string, authority_name ? authority_name->string :
1508 static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
1509 TALLOC_CTX *mem_ctx, int argc,
1512 struct policy_handle dom_pol, user_pol;
1513 NTSTATUS status, result;
1514 struct lsa_PrivilegeSet privs;
1515 struct lsa_LUIDAttribute *set = NULL;
1518 struct dcerpc_binding_handle *b = cli->binding_handle;
1523 printf("Usage: %s SID [rights...]\n", argv[0]);
1524 return NT_STATUS_OK;
1527 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1528 if (!NT_STATUS_IS_OK(status)) {
1532 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1533 SEC_FLAG_MAXIMUM_ALLOWED,
1536 if (!NT_STATUS_IS_OK(status)) {
1540 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1543 SEC_FLAG_MAXIMUM_ALLOWED,
1546 if (!NT_STATUS_IS_OK(status)) {
1549 if (!NT_STATUS_IS_OK(result)) {
1554 for (i=2; i<argc; i++) {
1556 struct lsa_String priv_name;
1557 struct lsa_LUID luid;
1559 init_lsa_String(&priv_name, argv[i]);
1561 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1566 if (!NT_STATUS_IS_OK(status)) {
1569 if (!NT_STATUS_IS_OK(result)) {
1575 set = talloc_realloc(mem_ctx, set,
1576 struct lsa_LUIDAttribute,
1579 return NT_STATUS_NO_MEMORY;
1582 set[privs.count-1].luid = luid;
1583 set[privs.count-1].attribute = 0;
1588 status = dcerpc_lsa_AddPrivilegesToAccount(b, mem_ctx,
1592 if (!NT_STATUS_IS_OK(status)) {
1595 if (!NT_STATUS_IS_OK(result)) {
1600 dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
1601 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1606 static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
1607 TALLOC_CTX *mem_ctx, int argc,
1610 struct policy_handle dom_pol, user_pol;
1611 NTSTATUS status, result;
1612 struct lsa_PrivilegeSet privs;
1613 struct lsa_LUIDAttribute *set = NULL;
1616 struct dcerpc_binding_handle *b = cli->binding_handle;
1621 printf("Usage: %s SID [rights...]\n", argv[0]);
1622 return NT_STATUS_OK;
1625 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1626 if (!NT_STATUS_IS_OK(status)) {
1630 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1631 SEC_FLAG_MAXIMUM_ALLOWED,
1634 if (!NT_STATUS_IS_OK(status)) {
1638 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1641 SEC_FLAG_MAXIMUM_ALLOWED,
1644 if (!NT_STATUS_IS_OK(status)) {
1647 if (!NT_STATUS_IS_OK(result)) {
1652 for (i=2; i<argc; i++) {
1654 struct lsa_String priv_name;
1655 struct lsa_LUID luid;
1657 init_lsa_String(&priv_name, argv[i]);
1659 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1664 if (!NT_STATUS_IS_OK(status)) {
1667 if (!NT_STATUS_IS_OK(result)) {
1673 set = talloc_realloc(mem_ctx, set,
1674 struct lsa_LUIDAttribute,
1677 return NT_STATUS_NO_MEMORY;
1680 set[privs.count-1].luid = luid;
1681 set[privs.count-1].attribute = 0;
1687 status = dcerpc_lsa_RemovePrivilegesFromAccount(b, mem_ctx,
1692 if (!NT_STATUS_IS_OK(status)) {
1695 if (!NT_STATUS_IS_OK(result)) {
1700 dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
1701 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1706 static NTSTATUS cmd_lsa_create_secret(struct rpc_pipe_client *cli,
1707 TALLOC_CTX *mem_ctx, int argc,
1710 NTSTATUS status, result;
1711 struct policy_handle handle, sec_handle;
1712 struct lsa_String name;
1713 struct dcerpc_binding_handle *b = cli->binding_handle;
1716 printf("Usage: %s name\n", argv[0]);
1717 return NT_STATUS_OK;
1720 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1722 SEC_FLAG_MAXIMUM_ALLOWED,
1724 if (!NT_STATUS_IS_OK(status)) {
1728 init_lsa_String(&name, argv[1]);
1730 status = dcerpc_lsa_CreateSecret(b, mem_ctx,
1733 SEC_FLAG_MAXIMUM_ALLOWED,
1736 if (!NT_STATUS_IS_OK(status)) {
1739 if (!NT_STATUS_IS_OK(result)) {
1745 if (is_valid_policy_hnd(&sec_handle)) {
1746 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1748 if (is_valid_policy_hnd(&handle)) {
1749 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1755 static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
1756 TALLOC_CTX *mem_ctx, int argc,
1759 NTSTATUS status, result;
1760 struct policy_handle handle, sec_handle;
1761 struct lsa_String name;
1762 struct dcerpc_binding_handle *b = cli->binding_handle;
1765 printf("Usage: %s name\n", argv[0]);
1766 return NT_STATUS_OK;
1769 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1771 SEC_FLAG_MAXIMUM_ALLOWED,
1773 if (!NT_STATUS_IS_OK(status)) {
1777 init_lsa_String(&name, argv[1]);
1779 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
1782 SEC_FLAG_MAXIMUM_ALLOWED,
1785 if (!NT_STATUS_IS_OK(status)) {
1788 if (!NT_STATUS_IS_OK(result)) {
1793 status = dcerpc_lsa_DeleteObject(b, mem_ctx,
1796 if (!NT_STATUS_IS_OK(status)) {
1799 if (!NT_STATUS_IS_OK(result)) {
1805 if (is_valid_policy_hnd(&sec_handle)) {
1806 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1808 if (is_valid_policy_hnd(&handle)) {
1809 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1815 static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
1816 TALLOC_CTX *mem_ctx, int argc,
1819 NTSTATUS status, result;
1820 struct policy_handle handle, sec_handle;
1821 struct lsa_String name;
1822 struct lsa_DATA_BUF_PTR new_val;
1823 NTTIME new_mtime = 0;
1824 struct lsa_DATA_BUF_PTR old_val;
1825 NTTIME old_mtime = 0;
1826 DATA_BLOB session_key;
1827 DATA_BLOB new_blob = data_blob_null;
1828 DATA_BLOB old_blob = data_blob_null;
1829 char *new_secret, *old_secret;
1830 struct dcerpc_binding_handle *b = cli->binding_handle;
1833 printf("Usage: %s name\n", argv[0]);
1834 return NT_STATUS_OK;
1837 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1839 SEC_FLAG_MAXIMUM_ALLOWED,
1841 if (!NT_STATUS_IS_OK(status)) {
1845 init_lsa_String(&name, argv[1]);
1847 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
1850 SEC_FLAG_MAXIMUM_ALLOWED,
1853 if (!NT_STATUS_IS_OK(status)) {
1856 if (!NT_STATUS_IS_OK(result)) {
1861 ZERO_STRUCT(new_val);
1862 ZERO_STRUCT(old_val);
1864 status = dcerpc_lsa_QuerySecret(b, mem_ctx,
1871 if (!NT_STATUS_IS_OK(status)) {
1874 if (!NT_STATUS_IS_OK(result)) {
1879 status = cli_get_session_key(mem_ctx, cli, &session_key);
1880 if (!NT_STATUS_IS_OK(status)) {
1885 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
1888 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
1891 new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
1892 old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
1894 d_printf("new secret: %s\n", new_secret);
1897 d_printf("old secret: %s\n", old_secret);
1901 if (is_valid_policy_hnd(&sec_handle)) {
1902 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1904 if (is_valid_policy_hnd(&handle)) {
1905 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1911 static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
1912 TALLOC_CTX *mem_ctx, int argc,
1915 NTSTATUS status, result;
1916 struct policy_handle handle, sec_handle;
1917 struct lsa_String name;
1918 struct lsa_DATA_BUF new_val;
1919 struct lsa_DATA_BUF old_val;
1921 DATA_BLOB session_key;
1922 struct dcerpc_binding_handle *b = cli->binding_handle;
1925 printf("Usage: %s name secret\n", argv[0]);
1926 return NT_STATUS_OK;
1929 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1931 SEC_FLAG_MAXIMUM_ALLOWED,
1933 if (!NT_STATUS_IS_OK(status)) {
1937 init_lsa_String(&name, argv[1]);
1939 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
1942 SEC_FLAG_MAXIMUM_ALLOWED,
1945 if (!NT_STATUS_IS_OK(status)) {
1948 if (!NT_STATUS_IS_OK(result)) {
1953 ZERO_STRUCT(new_val);
1954 ZERO_STRUCT(old_val);
1956 status = cli_get_session_key(mem_ctx, cli, &session_key);
1957 if (!NT_STATUS_IS_OK(status)) {
1961 enc_key = sess_encrypt_string(argv[2], &session_key);
1963 new_val.length = enc_key.length;
1964 new_val.size = enc_key.length;
1965 new_val.data = enc_key.data;
1967 status = dcerpc_lsa_SetSecret(b, mem_ctx,
1972 if (!NT_STATUS_IS_OK(status)) {
1975 if (!NT_STATUS_IS_OK(result)) {
1981 if (is_valid_policy_hnd(&sec_handle)) {
1982 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1984 if (is_valid_policy_hnd(&handle)) {
1985 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1991 static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
1992 TALLOC_CTX *mem_ctx, int argc,
1995 NTSTATUS status, result;
1996 struct policy_handle handle;
1997 struct lsa_String name;
1998 struct lsa_DATA_BUF *val;
1999 DATA_BLOB session_key;
2000 DATA_BLOB blob = data_blob_null;
2002 struct dcerpc_binding_handle *b = cli->binding_handle;
2005 printf("Usage: %s name\n", argv[0]);
2006 return NT_STATUS_OK;
2009 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2011 SEC_FLAG_MAXIMUM_ALLOWED,
2013 if (!NT_STATUS_IS_OK(status)) {
2017 init_lsa_String(&name, argv[1]);
2021 status = dcerpc_lsa_RetrievePrivateData(b, mem_ctx,
2026 if (!NT_STATUS_IS_OK(status)) {
2029 if (!NT_STATUS_IS_OK(result)) {
2034 status = cli_get_session_key(mem_ctx, cli, &session_key);
2035 if (!NT_STATUS_IS_OK(status)) {
2040 blob = data_blob_const(val->data, val->length);
2043 secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
2045 d_printf("secret: %s\n", secret);
2049 if (is_valid_policy_hnd(&handle)) {
2050 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2056 static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
2057 TALLOC_CTX *mem_ctx, int argc,
2060 NTSTATUS status, result;
2061 struct policy_handle handle;
2062 struct lsa_String name;
2063 struct lsa_DATA_BUF val;
2064 DATA_BLOB session_key;
2066 struct dcerpc_binding_handle *b = cli->binding_handle;
2069 printf("Usage: %s name secret\n", argv[0]);
2070 return NT_STATUS_OK;
2073 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2075 SEC_FLAG_MAXIMUM_ALLOWED,
2077 if (!NT_STATUS_IS_OK(status)) {
2081 init_lsa_String(&name, argv[1]);
2085 status = cli_get_session_key(mem_ctx, cli, &session_key);
2086 if (!NT_STATUS_IS_OK(status)) {
2090 enc_key = sess_encrypt_string(argv[2], &session_key);
2092 val.length = enc_key.length;
2093 val.size = enc_key.length;
2094 val.data = enc_key.data;
2096 status = dcerpc_lsa_StorePrivateData(b, mem_ctx,
2101 if (!NT_STATUS_IS_OK(status)) {
2104 if (!NT_STATUS_IS_OK(result)) {
2110 if (is_valid_policy_hnd(&handle)) {
2111 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2117 static NTSTATUS cmd_lsa_create_trusted_domain(struct rpc_pipe_client *cli,
2118 TALLOC_CTX *mem_ctx, int argc,
2121 NTSTATUS status, result;
2122 struct policy_handle handle, trustdom_handle;
2124 struct lsa_DomainInfo info;
2125 struct dcerpc_binding_handle *b = cli->binding_handle;
2128 printf("Usage: %s name sid\n", argv[0]);
2129 return NT_STATUS_OK;
2132 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2134 SEC_FLAG_MAXIMUM_ALLOWED,
2136 if (!NT_STATUS_IS_OK(status)) {
2140 init_lsa_StringLarge(&info.name, argv[1]);
2142 string_to_sid(&sid, argv[2]);
2144 status = dcerpc_lsa_CreateTrustedDomain(b, mem_ctx,
2147 SEC_FLAG_MAXIMUM_ALLOWED,
2150 if (!NT_STATUS_IS_OK(status)) {
2153 if (!NT_STATUS_IS_OK(result)) {
2159 if (is_valid_policy_hnd(&trustdom_handle)) {
2160 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
2163 if (is_valid_policy_hnd(&handle)) {
2164 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2170 static NTSTATUS cmd_lsa_delete_trusted_domain(struct rpc_pipe_client *cli,
2171 TALLOC_CTX *mem_ctx, int argc,
2174 NTSTATUS status, result;
2175 struct policy_handle handle, trustdom_handle;
2176 struct lsa_String name;
2177 struct dom_sid *sid = NULL;
2178 struct dcerpc_binding_handle *b = cli->binding_handle;
2181 printf("Usage: %s name\n", argv[0]);
2182 return NT_STATUS_OK;
2185 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2187 SEC_FLAG_MAXIMUM_ALLOWED,
2189 if (!NT_STATUS_IS_OK(status)) {
2193 init_lsa_String(&name, argv[1]);
2195 status = dcerpc_lsa_OpenTrustedDomainByName(b, mem_ctx,
2198 SEC_FLAG_MAXIMUM_ALLOWED,
2201 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2206 uint32_t resume_handle = 0;
2207 struct lsa_DomainList domains;
2210 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
2216 if (!NT_STATUS_IS_OK(status)) {
2219 if (!NT_STATUS_IS_OK(result)) {
2224 for (i=0; i < domains.count; i++) {
2225 if (strequal(domains.domains[i].name.string, argv[1])) {
2226 sid = domains.domains[i].sid;
2232 return NT_STATUS_INVALID_SID;
2236 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
2239 SEC_FLAG_MAXIMUM_ALLOWED,
2242 if (!NT_STATUS_IS_OK(status)) {
2245 if (!NT_STATUS_IS_OK(result)) {
2251 status = dcerpc_lsa_DeleteObject(b, mem_ctx,
2254 if (!NT_STATUS_IS_OK(status)) {
2257 if (!NT_STATUS_IS_OK(result)) {
2263 if (is_valid_policy_hnd(&trustdom_handle)) {
2264 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
2267 if (is_valid_policy_hnd(&handle)) {
2268 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2275 /* List of commands exported by this module */
2277 struct cmd_set lsarpc_commands[] = {
2281 { "lsaquery", RPC_RTYPE_NTSTATUS, cmd_lsa_query_info_policy, NULL, &ndr_table_lsarpc, NULL, "Query info policy", "" },
2282 { "lookupsids", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids, NULL, &ndr_table_lsarpc, NULL, "Convert SIDs to names", "" },
2283 { "lookupsids3", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids3, NULL, &ndr_table_lsarpc, NULL, "Convert SIDs to names", "" },
2284 { "lookupnames", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names, NULL, &ndr_table_lsarpc, NULL, "Convert names to SIDs", "" },
2285 { "lookupnames4", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names4, NULL, &ndr_table_lsarpc, NULL, "Convert names to SIDs", "" },
2286 { "lookupnames_level", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names_level, NULL, &ndr_table_lsarpc, NULL, "Convert names to SIDs", "" },
2287 { "enumtrust", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_trust_dom, NULL, &ndr_table_lsarpc, NULL, "Enumerate trusted domains", "Usage: [preferred max number] [enum context (0)]" },
2288 { "enumprivs", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privilege, NULL, &ndr_table_lsarpc, NULL, "Enumerate privileges", "" },
2289 { "getdispname", RPC_RTYPE_NTSTATUS, cmd_lsa_get_dispname, NULL, &ndr_table_lsarpc, NULL, "Get the privilege name", "" },
2290 { "lsaenumsid", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_sids, NULL, &ndr_table_lsarpc, NULL, "Enumerate the LSA SIDS", "" },
2291 { "lsacreateaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_create_account, NULL, &ndr_table_lsarpc, NULL, "Create a new lsa account", "" },
2292 { "lsaenumprivsaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privsaccounts, NULL, &ndr_table_lsarpc, NULL, "Enumerate the privileges of an SID", "" },
2293 { "lsaenumacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_acct_rights, NULL, &ndr_table_lsarpc, NULL, "Enumerate the rights of an SID", "" },
2294 { "lsaaddpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_add_priv, NULL, &ndr_table_lsarpc, NULL, "Assign a privilege to a SID", "" },
2295 { "lsadelpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_del_priv, NULL, &ndr_table_lsarpc, NULL, "Revoke a privilege from a SID", "" },
2296 { "lsaaddacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_add_acct_rights, NULL, &ndr_table_lsarpc, NULL, "Add rights to an account", "" },
2297 { "lsaremoveacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_remove_acct_rights, NULL, &ndr_table_lsarpc, NULL, "Remove rights from an account", "" },
2298 { "lsalookupprivvalue", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_priv_value, NULL, &ndr_table_lsarpc, NULL, "Get a privilege value given its name", "" },
2299 { "lsaquerysecobj", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secobj, NULL, &ndr_table_lsarpc, NULL, "Query LSA security object", "" },
2300 { "lsaquerytrustdominfo",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfo, NULL, &ndr_table_lsarpc, NULL, "Query LSA trusted domains info (given a SID)", "" },
2301 { "lsaquerytrustdominfobyname",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobyname, NULL, &ndr_table_lsarpc, NULL, "Query LSA trusted domains info (given a name), only works for Windows > 2k", "" },
2302 { "lsaquerytrustdominfobysid",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobysid, NULL, &ndr_table_lsarpc, NULL, "Query LSA trusted domains info (given a SID)", "" },
2303 { "lsasettrustdominfo", RPC_RTYPE_NTSTATUS, cmd_lsa_set_trustdominfo, NULL, &ndr_table_lsarpc, NULL, "Set LSA trusted domain info", "" },
2304 { "getusername", RPC_RTYPE_NTSTATUS, cmd_lsa_get_username, NULL, &ndr_table_lsarpc, NULL, "Get username", "" },
2305 { "createsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_create_secret, NULL, &ndr_table_lsarpc, NULL, "Create Secret", "" },
2306 { "deletesecret", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_secret, NULL, &ndr_table_lsarpc, NULL, "Delete Secret", "" },
2307 { "querysecret", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secret, NULL, &ndr_table_lsarpc, NULL, "Query Secret", "" },
2308 { "setsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_set_secret, NULL, &ndr_table_lsarpc, NULL, "Set Secret", "" },
2309 { "retrieveprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_retrieve_private_data, NULL, &ndr_table_lsarpc, NULL, "Retrieve Private Data", "" },
2310 { "storeprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_store_private_data, NULL, &ndr_table_lsarpc, NULL, "Store Private Data", "" },
2311 { "createtrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_create_trusted_domain, NULL, &ndr_table_lsarpc, NULL, "Create Trusted Domain", "" },
2312 { "deletetrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_trusted_domain, NULL, &ndr_table_lsarpc, NULL, "Delete Trusted Domain", "" },