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 struct dom_sid_buf buf;
94 d_printf("Domain Name: %s\n", r->name.string);
95 d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
98 static void display_query_info_5(struct lsa_DomainInfo *r)
100 struct dom_sid_buf buf;
101 d_printf("Domain Name: %s\n", r->name.string);
102 d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
105 static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
107 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
110 static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
112 d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
113 d_printf("Log is full: %d\n", r->log_is_full);
116 static void display_query_info_12(struct lsa_DnsDomainInfo *r)
118 struct dom_sid_buf buf;
119 d_printf("Domain NetBios Name: %s\n", r->name.string);
120 d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
121 d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
122 d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
123 d_printf("Domain GUID: %s\n", GUID_string(talloc_tos(),
127 static void display_lsa_query_info(union lsa_PolicyInformation *info,
128 enum lsa_PolicyInfo level)
132 display_query_info_1(&info->audit_log);
135 display_query_info_2(&info->audit_events);
138 display_query_info_3(&info->domain);
141 display_query_info_5(&info->account_domain);
144 display_query_info_10(&info->auditfullset);
147 display_query_info_11(&info->auditfullquery);
150 display_query_info_12(&info->dns);
153 printf("can't display info level: %d\n", level);
158 static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli,
159 TALLOC_CTX *mem_ctx, int argc,
162 struct policy_handle pol;
163 NTSTATUS status, result;
164 union lsa_PolicyInformation *info = NULL;
165 struct dcerpc_binding_handle *b = cli->binding_handle;
167 uint32_t info_class = 3;
170 printf("Usage: %s [info_class]\n", argv[0]);
175 info_class = atoi(argv[1]);
177 switch (info_class) {
179 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
180 SEC_FLAG_MAXIMUM_ALLOWED,
183 if (!NT_STATUS_IS_OK(status))
186 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
193 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
194 SEC_FLAG_MAXIMUM_ALLOWED,
197 if (!NT_STATUS_IS_OK(status))
200 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
207 if (!NT_STATUS_IS_OK(status)) {
211 if (NT_STATUS_IS_OK(result)) {
212 display_lsa_query_info(info, info_class);
215 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
221 /* Resolve a list of names to a list of sids */
223 static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli,
224 TALLOC_CTX *mem_ctx, int argc,
227 struct policy_handle pol;
228 NTSTATUS status, result;
229 struct dom_sid *sids;
230 enum lsa_SidType *types;
232 struct dcerpc_binding_handle *b = cli->binding_handle;
235 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
239 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
240 SEC_FLAG_MAXIMUM_ALLOWED,
243 if (!NT_STATUS_IS_OK(status))
246 status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
247 (const char**)(argv + 1), NULL, 1, &sids, &types);
249 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
250 NT_STATUS_V(STATUS_SOME_UNMAPPED))
253 status = NT_STATUS_OK;
257 for (i = 0; i < (argc - 1); i++) {
258 struct dom_sid_buf sid_str;
259 printf("%s %s (%s: %d)\n",
261 dom_sid_str_buf(&sids[i], &sid_str),
262 sid_type_lookup(types[i]),
266 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
272 /* Resolve a list of names to a list of sids */
274 static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli,
275 TALLOC_CTX *mem_ctx, int argc,
278 struct policy_handle pol;
279 NTSTATUS status, result;
280 struct dom_sid *sids = NULL;
281 enum lsa_SidType *types = NULL;
283 struct dcerpc_binding_handle *b = cli->binding_handle;
286 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
290 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
291 SEC_FLAG_MAXIMUM_ALLOWED,
293 if (!NT_STATUS_IS_OK(status)) {
297 level = atoi(argv[1]);
299 status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2,
300 (const char**)(argv + 2), NULL, level, &sids, &types);
302 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
303 NT_STATUS_V(STATUS_SOME_UNMAPPED))
308 status = NT_STATUS_OK;
312 for (i = 0; i < (argc - 2); i++) {
313 struct dom_sid_buf sid_str;
314 printf("%s %s (%s: %d)\n",
316 dom_sid_str_buf(&sids[i], &sid_str),
317 sid_type_lookup(types[i]),
321 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
327 static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
328 TALLOC_CTX *mem_ctx, int argc,
331 NTSTATUS status, result;
334 struct lsa_String *names;
335 struct lsa_RefDomainList *domains = NULL;
336 struct lsa_TransSidArray3 sids;
339 struct dcerpc_binding_handle *b = cli->binding_handle;
342 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
349 names = talloc_array(mem_ctx, struct lsa_String, num_names);
350 NT_STATUS_HAVE_NO_MEMORY(names);
352 for (i=0; i < num_names; i++) {
353 init_lsa_String(&names[i], argv[i+1]);
356 status = dcerpc_lsa_LookupNames4(b, mem_ctx,
366 if (!NT_STATUS_IS_OK(status)) {
369 if (!NT_STATUS_IS_OK(result)) {
373 if (sids.count != num_names) {
374 return NT_STATUS_INVALID_NETWORK_RESPONSE;
377 for (i = 0; i < sids.count; i++) {
378 struct dom_sid_buf sid_str;
379 printf("%s %s (%s: %d)\n",
381 dom_sid_str_buf(sids.sids[i].sid, &sid_str),
382 sid_type_lookup(sids.sids[i].sid_type),
383 sids.sids[i].sid_type);
389 /* Resolve a list of SIDs to a list of names */
391 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
392 int argc, const char **argv)
394 struct policy_handle pol;
395 NTSTATUS status, result;
396 struct dom_sid *sids;
399 enum lsa_SidType *types;
401 struct dcerpc_binding_handle *b = cli->binding_handle;
404 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
408 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
409 SEC_FLAG_MAXIMUM_ALLOWED,
412 if (!NT_STATUS_IS_OK(status))
415 /* Convert arguments to sids */
417 sids = talloc_array(mem_ctx, struct dom_sid, argc - 1);
420 printf("could not allocate memory for %d sids\n", argc - 1);
424 for (i = 0; i < argc - 1; i++)
425 if (!string_to_sid(&sids[i], argv[i + 1])) {
426 status = NT_STATUS_INVALID_SID;
430 /* Lookup the SIDs */
432 status = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
433 &domains, &names, &types);
435 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
436 NT_STATUS_V(STATUS_SOME_UNMAPPED))
439 status = NT_STATUS_OK;
443 for (i = 0; i < (argc - 1); i++) {
444 struct dom_sid_buf sid_str;
446 dom_sid_str_buf(&sids[i], &sid_str);
447 if (types[i] == SID_NAME_DOMAIN) {
448 printf("%s %s (%d)\n", sid_str.buf,
449 domains[i] ? domains[i] : "*unknown*",
452 printf("%s %s\\%s (%d)\n", sid_str.buf,
453 domains[i] ? domains[i] : "*unknown*",
454 names[i] ? names[i] : "*unknown*",
459 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
465 static NTSTATUS cmd_lsa_lookup_sids_level(struct rpc_pipe_client *cli,
466 TALLOC_CTX *mem_ctx, int argc,
469 struct policy_handle pol;
470 NTSTATUS status, result;
471 struct dom_sid *sids = NULL;
472 char **domains = NULL;
474 enum lsa_SidType *types = NULL;
476 struct dcerpc_binding_handle *b = cli->binding_handle;
479 printf("Usage: %s [level] [sid1 [sid2 [...]]]\n", argv[0]);
483 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
484 SEC_FLAG_MAXIMUM_ALLOWED,
486 if (!NT_STATUS_IS_OK(status)) {
490 level = atoi(argv[1]);
492 /* Convert arguments to sids */
494 sids = talloc_array(mem_ctx, struct dom_sid, argc - 2);
496 printf("could not allocate memory for %d sids\n", argc - 2);
500 for (i = 0; i < argc - 2; i++) {
501 if (!string_to_sid(&sids[i], argv[i + 2])) {
502 status = NT_STATUS_INVALID_SID;
507 /* Lookup the SIDs */
509 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
520 if (!NT_STATUS_IS_OK(status)) {
525 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
526 NT_STATUS_V(STATUS_SOME_UNMAPPED))
531 status = NT_STATUS_OK;
535 for (i = 0; i < (argc - 2); i++) {
536 struct dom_sid_buf sid_str;
538 dom_sid_str_buf(&sids[i], &sid_str);
539 if (types[i] == SID_NAME_DOMAIN) {
540 printf("%s %s (%d)\n", sid_str.buf,
541 domains[i] ? domains[i] : "*unknown*",
544 printf("%s %s\\%s (%d)\n", sid_str.buf,
545 domains[i] ? domains[i] : "*unknown*",
546 names[i] ? names[i] : "*unknown*",
551 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
557 /* Resolve a list of SIDs to a list of names */
559 static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
561 int argc, const char **argv)
563 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
565 struct lsa_SidArray sids;
566 struct lsa_RefDomainList *domains = NULL;
567 struct lsa_TransNameArray2 names;
569 struct dcerpc_binding_handle *b = cli->binding_handle;
572 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
578 /* Convert arguments to sids */
580 sids.num_sids = argc-1;
581 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
583 printf("could not allocate memory for %d sids\n", sids.num_sids);
587 for (i = 0; i < sids.num_sids; i++) {
588 sids.sids[i].sid = talloc(sids.sids, struct dom_sid);
589 if (sids.sids[i].sid == NULL) {
590 status = NT_STATUS_NO_MEMORY;
593 if (!string_to_sid(sids.sids[i].sid, argv[i+1])) {
594 status = NT_STATUS_INVALID_SID;
599 /* Lookup the SIDs */
600 status = dcerpc_lsa_LookupSids3(b, mem_ctx,
609 if (!NT_STATUS_IS_OK(status)) {
612 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
613 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
618 status = NT_STATUS_OK;
622 for (i = 0; i < names.count; i++) {
623 struct dom_sid_buf sid_str;
625 if (i >= sids.num_sids) {
628 printf("%s %s (%d)\n",
629 dom_sid_str_buf(sids.sids[i].sid, &sid_str),
630 names.names[i].name.string,
631 names.names[i].sid_type);
639 /* Enumerate list of trusted domains */
641 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
642 TALLOC_CTX *mem_ctx, int argc,
645 struct policy_handle pol;
646 NTSTATUS status, result;
647 struct lsa_DomainList domain_list;
648 struct dcerpc_binding_handle *b = cli->binding_handle;
650 /* defaults, but may be changed using params */
651 uint32_t enum_ctx = 0;
653 uint32_t max_size = (uint32_t)-1;
656 printf("Usage: %s [enum context (0)]\n", argv[0]);
660 if (argc == 2 && argv[1]) {
661 enum_ctx = atoi(argv[2]);
664 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
665 LSA_POLICY_VIEW_LOCAL_INFORMATION,
668 if (!NT_STATUS_IS_OK(status))
671 status = STATUS_MORE_ENTRIES;
673 while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
675 /* Lookup list of trusted domains */
677 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
683 if (!NT_STATUS_IS_OK(status)) {
686 if (!NT_STATUS_IS_OK(result) &&
687 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
688 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
693 /* Print results: list of names and sids returned in this
695 for (i = 0; i < domain_list.count; i++) {
696 struct dom_sid_buf sid_str;
699 domain_list.domains[i].name.string ?
700 domain_list.domains[i].name.string : "*unknown*",
701 dom_sid_str_buf(domain_list.domains[i].sid,
706 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
711 /* Enumerates privileges */
713 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli,
714 TALLOC_CTX *mem_ctx, int argc,
717 struct policy_handle pol;
718 NTSTATUS status, result;
719 struct lsa_PrivArray priv_array;
720 struct dcerpc_binding_handle *b = cli->binding_handle;
722 uint32_t enum_context=0;
723 uint32_t pref_max_length=0x1000;
727 printf("Usage: %s [enum context] [max length]\n", argv[0]);
732 enum_context=atoi(argv[1]);
735 pref_max_length=atoi(argv[2]);
737 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
738 SEC_FLAG_MAXIMUM_ALLOWED,
741 if (!NT_STATUS_IS_OK(status))
744 status = dcerpc_lsa_EnumPrivs(b, mem_ctx,
750 if (!NT_STATUS_IS_OK(status))
752 if (!NT_STATUS_IS_OK(result)) {
758 printf("found %d privileges\n\n", priv_array.count);
760 for (i = 0; i < priv_array.count; i++) {
761 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
762 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
763 priv_array.privs[i].luid.high,
764 priv_array.privs[i].luid.low,
765 priv_array.privs[i].luid.high,
766 priv_array.privs[i].luid.low);
769 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
774 /* Get privilege name */
776 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli,
777 TALLOC_CTX *mem_ctx, int argc,
780 struct policy_handle pol;
781 NTSTATUS status, result;
782 struct dcerpc_binding_handle *b = cli->binding_handle;
785 uint16_t lang_id_sys=0;
786 uint16_t lang_id_desc;
787 struct lsa_String lsa_name;
788 struct lsa_StringLarge *description = NULL;
791 printf("Usage: %s privilege name\n", argv[0]);
795 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
796 SEC_FLAG_MAXIMUM_ALLOWED,
799 if (!NT_STATUS_IS_OK(status))
802 init_lsa_String(&lsa_name, argv[1]);
804 status = dcerpc_lsa_LookupPrivDisplayName(b, mem_ctx,
812 if (!NT_STATUS_IS_OK(status))
814 if (!NT_STATUS_IS_OK(result)) {
820 printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
822 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
827 /* Enumerate the LSA SIDS */
829 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli,
830 TALLOC_CTX *mem_ctx, int argc,
833 struct policy_handle pol;
834 NTSTATUS status, result;
835 struct dcerpc_binding_handle *b = cli->binding_handle;
837 uint32_t enum_context=0;
838 uint32_t pref_max_length=0x1000;
839 struct lsa_SidArray sid_array;
843 printf("Usage: %s [enum context] [max length]\n", argv[0]);
848 enum_context=atoi(argv[1]);
851 pref_max_length=atoi(argv[2]);
853 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
854 SEC_FLAG_MAXIMUM_ALLOWED,
857 if (!NT_STATUS_IS_OK(status))
860 status = dcerpc_lsa_EnumAccounts(b, mem_ctx,
866 if (!NT_STATUS_IS_OK(status))
868 if (!NT_STATUS_IS_OK(result)) {
874 printf("found %d SIDs\n\n", sid_array.num_sids);
876 for (i = 0; i < sid_array.num_sids; i++) {
877 struct dom_sid_buf sid_str;
880 dom_sid_str_buf(sid_array.sids[i].sid, &sid_str));
883 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
888 /* Create a new account */
890 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli,
891 TALLOC_CTX *mem_ctx, int argc,
894 struct policy_handle dom_pol;
895 struct policy_handle user_pol;
896 NTSTATUS status, result;
897 uint32_t des_access = 0x000f000f;
898 struct dcerpc_binding_handle *b = cli->binding_handle;
903 printf("Usage: %s SID\n", argv[0]);
907 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
908 if (!NT_STATUS_IS_OK(status))
911 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
912 SEC_FLAG_MAXIMUM_ALLOWED,
915 if (!NT_STATUS_IS_OK(status))
918 status = dcerpc_lsa_CreateAccount(b, mem_ctx,
924 if (!NT_STATUS_IS_OK(status))
926 if (!NT_STATUS_IS_OK(result)) {
931 printf("Account for SID %s successfully created\n\n", argv[1]);
932 status = NT_STATUS_OK;
934 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
940 /* Enumerate the privileges of an SID */
942 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli,
943 TALLOC_CTX *mem_ctx, int argc,
946 struct policy_handle dom_pol;
947 struct policy_handle user_pol;
948 NTSTATUS status, result;
949 uint32_t access_desired = 0x000f000f;
951 struct lsa_PrivilegeSet *privs = NULL;
953 struct dcerpc_binding_handle *b = cli->binding_handle;
956 printf("Usage: %s SID\n", argv[0]);
960 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
961 if (!NT_STATUS_IS_OK(status))
964 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
965 SEC_FLAG_MAXIMUM_ALLOWED,
968 if (!NT_STATUS_IS_OK(status))
971 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
977 if (!NT_STATUS_IS_OK(status))
979 if (!NT_STATUS_IS_OK(result)) {
984 status = dcerpc_lsa_EnumPrivsAccount(b, mem_ctx,
988 if (!NT_STATUS_IS_OK(status))
990 if (!NT_STATUS_IS_OK(result)) {
996 printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
997 printf("high\tlow\tattribute\n");
999 for (i = 0; i < privs->count; i++) {
1000 printf("%u\t%u\t%u\n",
1001 privs->set[i].luid.high,
1002 privs->set[i].luid.low,
1003 privs->set[i].attribute);
1006 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1012 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
1014 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli,
1015 TALLOC_CTX *mem_ctx, int argc,
1018 struct policy_handle dom_pol;
1019 NTSTATUS status, result;
1021 struct dom_sid_buf buf;
1022 struct lsa_RightSet rights;
1023 struct dcerpc_binding_handle *b = cli->binding_handle;
1028 printf("Usage: %s SID\n", argv[0]);
1029 return NT_STATUS_OK;
1032 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1033 if (!NT_STATUS_IS_OK(status))
1036 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1037 SEC_FLAG_MAXIMUM_ALLOWED,
1040 if (!NT_STATUS_IS_OK(status))
1043 status = dcerpc_lsa_EnumAccountRights(b, mem_ctx,
1048 if (!NT_STATUS_IS_OK(status))
1050 if (!NT_STATUS_IS_OK(result)) {
1055 printf("found %d privileges for SID %s\n", rights.count,
1056 dom_sid_str_buf(&sid, &buf));
1058 for (i = 0; i < rights.count; i++) {
1059 printf("\t%s\n", rights.names[i].string);
1062 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1068 /* add some privileges to a SID via LsaAddAccountRights */
1070 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli,
1071 TALLOC_CTX *mem_ctx, int argc,
1074 struct policy_handle dom_pol;
1075 NTSTATUS status, result;
1076 struct lsa_RightSet rights;
1079 struct dcerpc_binding_handle *b = cli->binding_handle;
1082 printf("Usage: %s SID [rights...]\n", argv[0]);
1083 return NT_STATUS_OK;
1086 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1087 if (!NT_STATUS_IS_OK(status))
1090 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1091 SEC_FLAG_MAXIMUM_ALLOWED,
1094 if (!NT_STATUS_IS_OK(status))
1097 rights.count = argc-2;
1098 rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
1100 if (!rights.names) {
1101 return NT_STATUS_NO_MEMORY;
1104 for (i=0; i<argc-2; i++) {
1105 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
1108 status = dcerpc_lsa_AddAccountRights(b, mem_ctx,
1113 if (!NT_STATUS_IS_OK(status))
1115 if (!NT_STATUS_IS_OK(result)) {
1120 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1126 /* remove some privileges to a SID via LsaRemoveAccountRights */
1128 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli,
1129 TALLOC_CTX *mem_ctx, int argc,
1132 struct policy_handle dom_pol;
1133 NTSTATUS status, result;
1134 struct lsa_RightSet rights;
1137 struct dcerpc_binding_handle *b = cli->binding_handle;
1140 printf("Usage: %s SID [rights...]\n", argv[0]);
1141 return NT_STATUS_OK;
1144 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1145 if (!NT_STATUS_IS_OK(status))
1148 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1149 SEC_FLAG_MAXIMUM_ALLOWED,
1152 if (!NT_STATUS_IS_OK(status))
1155 rights.count = argc-2;
1156 rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
1158 if (!rights.names) {
1159 return NT_STATUS_NO_MEMORY;
1162 for (i=0; i<argc-2; i++) {
1163 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
1166 status = dcerpc_lsa_RemoveAccountRights(b, mem_ctx,
1172 if (!NT_STATUS_IS_OK(status))
1174 if (!NT_STATUS_IS_OK(result)) {
1179 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1186 /* Get a privilege value given its name */
1188 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli,
1189 TALLOC_CTX *mem_ctx, int argc,
1192 struct policy_handle pol;
1193 NTSTATUS status, result;
1194 struct lsa_LUID luid;
1195 struct lsa_String name;
1196 struct dcerpc_binding_handle *b = cli->binding_handle;
1199 printf("Usage: %s name\n", argv[0]);
1200 return NT_STATUS_OK;
1203 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1204 SEC_FLAG_MAXIMUM_ALLOWED,
1207 if (!NT_STATUS_IS_OK(status))
1210 init_lsa_String(&name, argv[1]);
1212 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1217 if (!NT_STATUS_IS_OK(status))
1219 if (!NT_STATUS_IS_OK(result)) {
1226 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
1228 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1233 /* Query LSA security object */
1235 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli,
1236 TALLOC_CTX *mem_ctx, int argc,
1239 struct policy_handle pol;
1240 NTSTATUS status, result;
1241 struct sec_desc_buf *sdb;
1242 uint32_t sec_info = SECINFO_DACL;
1243 struct dcerpc_binding_handle *b = cli->binding_handle;
1245 if (argc < 1 || argc > 2) {
1246 printf("Usage: %s [sec_info]\n", argv[0]);
1247 return NT_STATUS_OK;
1250 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1251 SEC_FLAG_MAXIMUM_ALLOWED,
1255 sscanf(argv[1], "%x", &sec_info);
1257 if (!NT_STATUS_IS_OK(status))
1260 status = dcerpc_lsa_QuerySecurity(b, mem_ctx,
1265 if (!NT_STATUS_IS_OK(status))
1267 if (!NT_STATUS_IS_OK(result)) {
1274 display_sec_desc(sdb->sd);
1276 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1281 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
1282 DATA_BLOB session_key)
1284 char *pwd, *pwd_old;
1286 DATA_BLOB data = data_blob_const(p->password->data, p->password->length);
1287 DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
1289 pwd = sess_decrypt_string(talloc_tos(), &data, &session_key);
1290 pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key);
1292 d_printf("Password:\t%s\n", pwd);
1293 d_printf("Old Password:\t%s\n", pwd_old);
1296 talloc_free(pwd_old);
1299 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
1300 union lsa_TrustedDomainInfo *info,
1301 enum lsa_TrustDomInfoEnum info_class,
1302 DATA_BLOB session_key)
1304 switch (info_class) {
1305 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
1306 display_trust_dom_info_4(&info->password, session_key);
1309 const char *str = NULL;
1310 str = NDR_PRINT_UNION_STRING(mem_ctx,
1311 lsa_TrustedDomainInfo,
1314 d_printf("%s\n", str);
1321 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
1322 TALLOC_CTX *mem_ctx, int argc,
1325 struct policy_handle pol;
1326 NTSTATUS status, result;
1327 struct dom_sid dom_sid;
1328 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1329 union lsa_TrustedDomainInfo *info = NULL;
1330 enum lsa_TrustDomInfoEnum info_class = 1;
1331 DATA_BLOB session_key;
1332 struct dcerpc_binding_handle *b = cli->binding_handle;
1334 if (argc > 3 || argc < 2) {
1335 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1336 return NT_STATUS_OK;
1339 if (!string_to_sid(&dom_sid, argv[1]))
1340 return NT_STATUS_NO_MEMORY;
1343 info_class = atoi(argv[2]);
1345 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1347 if (!NT_STATUS_IS_OK(status))
1350 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
1356 if (!NT_STATUS_IS_OK(status))
1358 if (!NT_STATUS_IS_OK(result)) {
1363 status = cli_get_session_key(mem_ctx, cli, &session_key);
1364 if (!NT_STATUS_IS_OK(status)) {
1365 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1369 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1372 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1377 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1378 TALLOC_CTX *mem_ctx, int argc,
1381 struct policy_handle pol;
1382 NTSTATUS status, result;
1383 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1384 union lsa_TrustedDomainInfo *info = NULL;
1385 enum lsa_TrustDomInfoEnum info_class = 1;
1386 struct lsa_String trusted_domain;
1387 struct dcerpc_binding_handle *b = cli->binding_handle;
1388 DATA_BLOB session_key;
1390 if (argc > 3 || argc < 2) {
1391 printf("Usage: %s [name] [info_class]\n", argv[0]);
1392 return NT_STATUS_OK;
1396 info_class = atoi(argv[2]);
1398 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1400 if (!NT_STATUS_IS_OK(status))
1403 init_lsa_String(&trusted_domain, argv[1]);
1405 status = dcerpc_lsa_QueryTrustedDomainInfoByName(b, mem_ctx,
1411 if (!NT_STATUS_IS_OK(status))
1413 if (!NT_STATUS_IS_OK(result)) {
1418 status = cli_get_session_key(mem_ctx, cli, &session_key);
1419 if (!NT_STATUS_IS_OK(status)) {
1420 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1424 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1427 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1432 static NTSTATUS cmd_lsa_set_trustdominfo(struct rpc_pipe_client *cli,
1433 TALLOC_CTX *mem_ctx, int argc,
1436 struct policy_handle pol, trustdom_pol;
1437 NTSTATUS status, result;
1438 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1439 union lsa_TrustedDomainInfo info;
1440 struct dom_sid dom_sid;
1441 enum lsa_TrustDomInfoEnum info_class = 1;
1442 struct dcerpc_binding_handle *b = cli->binding_handle;
1444 if (argc > 4 || argc < 3) {
1445 printf("Usage: %s [sid] [info_class] [value]\n", argv[0]);
1446 return NT_STATUS_OK;
1449 if (!string_to_sid(&dom_sid, argv[1])) {
1450 return NT_STATUS_NO_MEMORY;
1454 info_class = atoi(argv[2]);
1456 switch (info_class) {
1457 case 13: /* LSA_TRUSTED_DOMAIN_SUPPORTED_ENCRYPTION_TYPES */
1458 info.enc_types.enc_types = atoi(argv[3]);
1461 return NT_STATUS_INVALID_PARAMETER;
1464 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1465 if (!NT_STATUS_IS_OK(status)) {
1469 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
1475 if (!NT_STATUS_IS_OK(status)) {
1478 if (!NT_STATUS_IS_OK(result)) {
1483 status = dcerpc_lsa_SetInformationTrustedDomain(b, mem_ctx,
1488 if (!NT_STATUS_IS_OK(status)) {
1491 if (!NT_STATUS_IS_OK(result)) {
1496 dcerpc_lsa_Close(b, mem_ctx, &trustdom_pol, &result);
1497 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1502 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1503 TALLOC_CTX *mem_ctx, int argc,
1506 struct policy_handle pol, trustdom_pol;
1507 NTSTATUS status, result;
1508 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1509 union lsa_TrustedDomainInfo *info = NULL;
1510 struct dom_sid dom_sid;
1511 enum lsa_TrustDomInfoEnum info_class = 1;
1512 DATA_BLOB session_key;
1513 struct dcerpc_binding_handle *b = cli->binding_handle;
1515 if (argc > 3 || argc < 2) {
1516 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1517 return NT_STATUS_OK;
1520 if (!string_to_sid(&dom_sid, argv[1]))
1521 return NT_STATUS_NO_MEMORY;
1525 info_class = atoi(argv[2]);
1527 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1529 if (!NT_STATUS_IS_OK(status))
1532 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
1538 if (!NT_STATUS_IS_OK(status))
1540 if (!NT_STATUS_IS_OK(result)) {
1545 status = dcerpc_lsa_QueryTrustedDomainInfo(b, mem_ctx,
1550 if (!NT_STATUS_IS_OK(status))
1552 if (!NT_STATUS_IS_OK(result)) {
1557 status = cli_get_session_key(mem_ctx, cli, &session_key);
1558 if (!NT_STATUS_IS_OK(status)) {
1559 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1563 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1566 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1571 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1572 TALLOC_CTX *mem_ctx, int argc,
1575 NTSTATUS status, result;
1576 const char *servername = cli->desthost;
1577 struct lsa_String *account_name = NULL;
1578 struct lsa_String *authority_name = NULL;
1579 struct dcerpc_binding_handle *b = cli->binding_handle;
1582 printf("Usage: %s servername\n", argv[0]);
1583 return NT_STATUS_OK;
1586 status = dcerpc_lsa_GetUserName(b, mem_ctx,
1591 if (!NT_STATUS_IS_OK(status)) {
1594 if (!NT_STATUS_IS_OK(result)) {
1601 printf("Account Name: %s, Authority Name: %s\n",
1602 account_name->string, authority_name ? authority_name->string :
1609 static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
1610 TALLOC_CTX *mem_ctx, int argc,
1613 struct policy_handle dom_pol, user_pol;
1614 NTSTATUS status, result;
1615 struct lsa_PrivilegeSet privs;
1616 struct lsa_LUIDAttribute *set = NULL;
1619 struct dcerpc_binding_handle *b = cli->binding_handle;
1624 printf("Usage: %s SID [rights...]\n", argv[0]);
1625 return NT_STATUS_OK;
1628 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1629 if (!NT_STATUS_IS_OK(status)) {
1633 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1634 SEC_FLAG_MAXIMUM_ALLOWED,
1637 if (!NT_STATUS_IS_OK(status)) {
1641 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1644 SEC_FLAG_MAXIMUM_ALLOWED,
1647 if (!NT_STATUS_IS_OK(status)) {
1650 if (!NT_STATUS_IS_OK(result)) {
1655 for (i=2; i<argc; i++) {
1657 struct lsa_String priv_name;
1658 struct lsa_LUID luid;
1660 init_lsa_String(&priv_name, argv[i]);
1662 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1667 if (!NT_STATUS_IS_OK(status)) {
1670 if (!NT_STATUS_IS_OK(result)) {
1676 set = talloc_realloc(mem_ctx, set,
1677 struct lsa_LUIDAttribute,
1680 return NT_STATUS_NO_MEMORY;
1683 set[privs.count-1].luid = luid;
1684 set[privs.count-1].attribute = 0;
1689 status = dcerpc_lsa_AddPrivilegesToAccount(b, mem_ctx,
1693 if (!NT_STATUS_IS_OK(status)) {
1696 if (!NT_STATUS_IS_OK(result)) {
1701 dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
1702 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1707 static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
1708 TALLOC_CTX *mem_ctx, int argc,
1711 struct policy_handle dom_pol, user_pol;
1712 NTSTATUS status, result;
1713 struct lsa_PrivilegeSet privs;
1714 struct lsa_LUIDAttribute *set = NULL;
1717 struct dcerpc_binding_handle *b = cli->binding_handle;
1722 printf("Usage: %s SID [rights...]\n", argv[0]);
1723 return NT_STATUS_OK;
1726 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1727 if (!NT_STATUS_IS_OK(status)) {
1731 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1732 SEC_FLAG_MAXIMUM_ALLOWED,
1735 if (!NT_STATUS_IS_OK(status)) {
1739 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1742 SEC_FLAG_MAXIMUM_ALLOWED,
1745 if (!NT_STATUS_IS_OK(status)) {
1748 if (!NT_STATUS_IS_OK(result)) {
1753 for (i=2; i<argc; i++) {
1755 struct lsa_String priv_name;
1756 struct lsa_LUID luid;
1758 init_lsa_String(&priv_name, argv[i]);
1760 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1765 if (!NT_STATUS_IS_OK(status)) {
1768 if (!NT_STATUS_IS_OK(result)) {
1774 set = talloc_realloc(mem_ctx, set,
1775 struct lsa_LUIDAttribute,
1778 return NT_STATUS_NO_MEMORY;
1781 set[privs.count-1].luid = luid;
1782 set[privs.count-1].attribute = 0;
1788 status = dcerpc_lsa_RemovePrivilegesFromAccount(b, mem_ctx,
1793 if (!NT_STATUS_IS_OK(status)) {
1796 if (!NT_STATUS_IS_OK(result)) {
1801 dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
1802 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1807 static NTSTATUS cmd_lsa_create_secret(struct rpc_pipe_client *cli,
1808 TALLOC_CTX *mem_ctx, int argc,
1811 NTSTATUS status, result;
1812 struct policy_handle handle, sec_handle;
1813 struct lsa_String name;
1814 struct dcerpc_binding_handle *b = cli->binding_handle;
1817 printf("Usage: %s name\n", argv[0]);
1818 return NT_STATUS_OK;
1821 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1823 SEC_FLAG_MAXIMUM_ALLOWED,
1825 if (!NT_STATUS_IS_OK(status)) {
1829 init_lsa_String(&name, argv[1]);
1831 status = dcerpc_lsa_CreateSecret(b, mem_ctx,
1834 SEC_FLAG_MAXIMUM_ALLOWED,
1837 if (!NT_STATUS_IS_OK(status)) {
1840 if (!NT_STATUS_IS_OK(result)) {
1846 if (is_valid_policy_hnd(&sec_handle)) {
1847 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1849 if (is_valid_policy_hnd(&handle)) {
1850 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1856 static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
1857 TALLOC_CTX *mem_ctx, int argc,
1860 NTSTATUS status, result;
1861 struct policy_handle handle, sec_handle;
1862 struct lsa_String name;
1863 struct dcerpc_binding_handle *b = cli->binding_handle;
1866 printf("Usage: %s name\n", argv[0]);
1867 return NT_STATUS_OK;
1870 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1872 SEC_FLAG_MAXIMUM_ALLOWED,
1874 if (!NT_STATUS_IS_OK(status)) {
1878 init_lsa_String(&name, argv[1]);
1880 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
1883 SEC_FLAG_MAXIMUM_ALLOWED,
1886 if (!NT_STATUS_IS_OK(status)) {
1889 if (!NT_STATUS_IS_OK(result)) {
1894 status = dcerpc_lsa_DeleteObject(b, mem_ctx,
1897 if (!NT_STATUS_IS_OK(status)) {
1900 if (!NT_STATUS_IS_OK(result)) {
1906 if (is_valid_policy_hnd(&sec_handle)) {
1907 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1909 if (is_valid_policy_hnd(&handle)) {
1910 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1916 static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
1917 TALLOC_CTX *mem_ctx, int argc,
1920 NTSTATUS status, result;
1921 struct policy_handle handle, sec_handle;
1922 struct lsa_String name;
1923 struct lsa_DATA_BUF_PTR new_val;
1924 NTTIME new_mtime = 0;
1925 struct lsa_DATA_BUF_PTR old_val;
1926 NTTIME old_mtime = 0;
1927 DATA_BLOB session_key;
1928 DATA_BLOB new_blob = data_blob_null;
1929 DATA_BLOB old_blob = data_blob_null;
1930 char *new_secret, *old_secret;
1931 struct dcerpc_binding_handle *b = cli->binding_handle;
1934 printf("Usage: %s name\n", argv[0]);
1935 return NT_STATUS_OK;
1938 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1940 SEC_FLAG_MAXIMUM_ALLOWED,
1942 if (!NT_STATUS_IS_OK(status)) {
1946 init_lsa_String(&name, argv[1]);
1948 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
1951 SEC_FLAG_MAXIMUM_ALLOWED,
1954 if (!NT_STATUS_IS_OK(status)) {
1957 if (!NT_STATUS_IS_OK(result)) {
1962 ZERO_STRUCT(new_val);
1963 ZERO_STRUCT(old_val);
1965 status = dcerpc_lsa_QuerySecret(b, mem_ctx,
1972 if (!NT_STATUS_IS_OK(status)) {
1975 if (!NT_STATUS_IS_OK(result)) {
1980 status = cli_get_session_key(mem_ctx, cli, &session_key);
1981 if (!NT_STATUS_IS_OK(status)) {
1986 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
1989 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
1992 new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
1993 old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
1995 d_printf("new secret: %s\n", new_secret);
1998 d_printf("old secret: %s\n", old_secret);
2002 if (is_valid_policy_hnd(&sec_handle)) {
2003 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
2005 if (is_valid_policy_hnd(&handle)) {
2006 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2012 static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
2013 TALLOC_CTX *mem_ctx, int argc,
2016 NTSTATUS status, result;
2017 struct policy_handle handle, sec_handle;
2018 struct lsa_String name;
2019 struct lsa_DATA_BUF new_val;
2020 struct lsa_DATA_BUF old_val;
2022 DATA_BLOB session_key;
2023 struct dcerpc_binding_handle *b = cli->binding_handle;
2026 printf("Usage: %s name secret\n", argv[0]);
2027 return NT_STATUS_OK;
2030 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2032 SEC_FLAG_MAXIMUM_ALLOWED,
2034 if (!NT_STATUS_IS_OK(status)) {
2038 init_lsa_String(&name, argv[1]);
2040 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
2043 SEC_FLAG_MAXIMUM_ALLOWED,
2046 if (!NT_STATUS_IS_OK(status)) {
2049 if (!NT_STATUS_IS_OK(result)) {
2054 ZERO_STRUCT(new_val);
2055 ZERO_STRUCT(old_val);
2057 status = cli_get_session_key(mem_ctx, cli, &session_key);
2058 if (!NT_STATUS_IS_OK(status)) {
2062 enc_key = sess_encrypt_string(argv[2], &session_key);
2064 new_val.length = enc_key.length;
2065 new_val.size = enc_key.length;
2066 new_val.data = enc_key.data;
2068 status = dcerpc_lsa_SetSecret(b, mem_ctx,
2073 if (!NT_STATUS_IS_OK(status)) {
2076 if (!NT_STATUS_IS_OK(result)) {
2082 if (is_valid_policy_hnd(&sec_handle)) {
2083 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
2085 if (is_valid_policy_hnd(&handle)) {
2086 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2092 static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
2093 TALLOC_CTX *mem_ctx, int argc,
2096 NTSTATUS status, result;
2097 struct policy_handle handle;
2098 struct lsa_String name;
2099 struct lsa_DATA_BUF *val;
2100 DATA_BLOB session_key;
2101 DATA_BLOB blob = data_blob_null;
2103 struct dcerpc_binding_handle *b = cli->binding_handle;
2106 printf("Usage: %s name\n", argv[0]);
2107 return NT_STATUS_OK;
2110 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2112 SEC_FLAG_MAXIMUM_ALLOWED,
2114 if (!NT_STATUS_IS_OK(status)) {
2118 init_lsa_String(&name, argv[1]);
2122 status = dcerpc_lsa_RetrievePrivateData(b, mem_ctx,
2127 if (!NT_STATUS_IS_OK(status)) {
2130 if (!NT_STATUS_IS_OK(result)) {
2135 status = cli_get_session_key(mem_ctx, cli, &session_key);
2136 if (!NT_STATUS_IS_OK(status)) {
2141 blob = data_blob_const(val->data, val->length);
2144 secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
2146 d_printf("secret: %s\n", secret);
2150 if (is_valid_policy_hnd(&handle)) {
2151 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2157 static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
2158 TALLOC_CTX *mem_ctx, int argc,
2161 NTSTATUS status, result;
2162 struct policy_handle handle;
2163 struct lsa_String name;
2164 struct lsa_DATA_BUF val;
2165 DATA_BLOB session_key;
2167 struct dcerpc_binding_handle *b = cli->binding_handle;
2170 printf("Usage: %s name secret\n", argv[0]);
2171 return NT_STATUS_OK;
2174 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2176 SEC_FLAG_MAXIMUM_ALLOWED,
2178 if (!NT_STATUS_IS_OK(status)) {
2182 init_lsa_String(&name, argv[1]);
2186 status = cli_get_session_key(mem_ctx, cli, &session_key);
2187 if (!NT_STATUS_IS_OK(status)) {
2191 enc_key = sess_encrypt_string(argv[2], &session_key);
2193 val.length = enc_key.length;
2194 val.size = enc_key.length;
2195 val.data = enc_key.data;
2197 status = dcerpc_lsa_StorePrivateData(b, mem_ctx,
2202 if (!NT_STATUS_IS_OK(status)) {
2205 if (!NT_STATUS_IS_OK(result)) {
2211 if (is_valid_policy_hnd(&handle)) {
2212 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2218 static NTSTATUS cmd_lsa_create_trusted_domain(struct rpc_pipe_client *cli,
2219 TALLOC_CTX *mem_ctx, int argc,
2222 NTSTATUS status, result;
2223 struct policy_handle handle, trustdom_handle;
2225 struct lsa_DomainInfo info;
2226 struct dcerpc_binding_handle *b = cli->binding_handle;
2229 printf("Usage: %s name sid\n", argv[0]);
2230 return NT_STATUS_OK;
2233 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2235 SEC_FLAG_MAXIMUM_ALLOWED,
2237 if (!NT_STATUS_IS_OK(status)) {
2241 init_lsa_StringLarge(&info.name, argv[1]);
2243 string_to_sid(&sid, argv[2]);
2245 status = dcerpc_lsa_CreateTrustedDomain(b, mem_ctx,
2248 SEC_FLAG_MAXIMUM_ALLOWED,
2251 if (!NT_STATUS_IS_OK(status)) {
2254 if (!NT_STATUS_IS_OK(result)) {
2260 if (is_valid_policy_hnd(&trustdom_handle)) {
2261 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
2264 if (is_valid_policy_hnd(&handle)) {
2265 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2271 static NTSTATUS cmd_lsa_delete_trusted_domain(struct rpc_pipe_client *cli,
2272 TALLOC_CTX *mem_ctx, int argc,
2275 NTSTATUS status, result;
2276 struct policy_handle handle, trustdom_handle;
2277 struct lsa_String name;
2278 struct dom_sid *sid = NULL;
2279 struct dcerpc_binding_handle *b = cli->binding_handle;
2282 printf("Usage: %s name\n", argv[0]);
2283 return NT_STATUS_OK;
2286 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2288 SEC_FLAG_MAXIMUM_ALLOWED,
2290 if (!NT_STATUS_IS_OK(status)) {
2294 init_lsa_String(&name, argv[1]);
2296 status = dcerpc_lsa_OpenTrustedDomainByName(b, mem_ctx,
2299 SEC_FLAG_MAXIMUM_ALLOWED,
2302 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2307 uint32_t resume_handle = 0;
2308 struct lsa_DomainList domains;
2311 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
2317 if (!NT_STATUS_IS_OK(status)) {
2320 if (!NT_STATUS_IS_OK(result)) {
2325 for (i=0; i < domains.count; i++) {
2326 if (strequal(domains.domains[i].name.string, argv[1])) {
2327 sid = domains.domains[i].sid;
2333 return NT_STATUS_INVALID_SID;
2337 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
2340 SEC_FLAG_MAXIMUM_ALLOWED,
2343 if (!NT_STATUS_IS_OK(status)) {
2346 if (!NT_STATUS_IS_OK(result)) {
2352 status = dcerpc_lsa_DeleteObject(b, mem_ctx,
2355 if (!NT_STATUS_IS_OK(status)) {
2358 if (!NT_STATUS_IS_OK(result)) {
2364 if (is_valid_policy_hnd(&trustdom_handle)) {
2365 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
2368 if (is_valid_policy_hnd(&handle)) {
2369 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2376 /* List of commands exported by this module */
2378 struct cmd_set lsarpc_commands[] = {
2386 .returntype = RPC_RTYPE_NTSTATUS,
2387 .ntfn = cmd_lsa_query_info_policy,
2389 .table = &ndr_table_lsarpc,
2391 .description = "Query info policy",
2395 .name = "lookupsids",
2396 .returntype = RPC_RTYPE_NTSTATUS,
2397 .ntfn = cmd_lsa_lookup_sids,
2399 .table = &ndr_table_lsarpc,
2401 .description = "Convert SIDs to names",
2405 .name = "lookupsids3",
2406 .returntype = RPC_RTYPE_NTSTATUS,
2407 .ntfn = cmd_lsa_lookup_sids3,
2409 .table = &ndr_table_lsarpc,
2411 .description = "Convert SIDs to names",
2415 .name = "lookupsids_level",
2416 .returntype = RPC_RTYPE_NTSTATUS,
2417 .ntfn = cmd_lsa_lookup_sids_level,
2419 .table = &ndr_table_lsarpc,
2421 .description = "Convert SIDs to names",
2425 .name = "lookupnames",
2426 .returntype = RPC_RTYPE_NTSTATUS,
2427 .ntfn = cmd_lsa_lookup_names,
2429 .table = &ndr_table_lsarpc,
2431 .description = "Convert names to SIDs",
2435 .name = "lookupnames4",
2436 .returntype = RPC_RTYPE_NTSTATUS,
2437 .ntfn = cmd_lsa_lookup_names4,
2439 .table = &ndr_table_lsarpc,
2441 .description = "Convert names to SIDs",
2445 .name = "lookupnames_level",
2446 .returntype = RPC_RTYPE_NTSTATUS,
2447 .ntfn = cmd_lsa_lookup_names_level,
2449 .table = &ndr_table_lsarpc,
2451 .description = "Convert names to SIDs",
2455 .name = "enumtrust",
2456 .returntype = RPC_RTYPE_NTSTATUS,
2457 .ntfn = cmd_lsa_enum_trust_dom,
2459 .table = &ndr_table_lsarpc,
2461 .description = "Enumerate trusted domains",
2462 .usage = "Usage: [preferred max number] [enum context (0)]",
2465 .name = "enumprivs",
2466 .returntype = RPC_RTYPE_NTSTATUS,
2467 .ntfn = cmd_lsa_enum_privilege,
2469 .table = &ndr_table_lsarpc,
2471 .description = "Enumerate privileges",
2475 .name = "getdispname",
2476 .returntype = RPC_RTYPE_NTSTATUS,
2477 .ntfn = cmd_lsa_get_dispname,
2479 .table = &ndr_table_lsarpc,
2481 .description = "Get the privilege name",
2485 .name = "lsaenumsid",
2486 .returntype = RPC_RTYPE_NTSTATUS,
2487 .ntfn = cmd_lsa_enum_sids,
2489 .table = &ndr_table_lsarpc,
2491 .description = "Enumerate the LSA SIDS",
2495 .name = "lsacreateaccount",
2496 .returntype = RPC_RTYPE_NTSTATUS,
2497 .ntfn = cmd_lsa_create_account,
2499 .table = &ndr_table_lsarpc,
2501 .description = "Create a new lsa account",
2505 .name = "lsaenumprivsaccount",
2506 .returntype = RPC_RTYPE_NTSTATUS,
2507 .ntfn = cmd_lsa_enum_privsaccounts,
2509 .table = &ndr_table_lsarpc,
2511 .description = "Enumerate the privileges of an SID",
2515 .name = "lsaenumacctrights",
2516 .returntype = RPC_RTYPE_NTSTATUS,
2517 .ntfn = cmd_lsa_enum_acct_rights,
2519 .table = &ndr_table_lsarpc,
2521 .description = "Enumerate the rights of an SID",
2525 .name = "lsaaddpriv",
2526 .returntype = RPC_RTYPE_NTSTATUS,
2527 .ntfn = cmd_lsa_add_priv,
2529 .table = &ndr_table_lsarpc,
2531 .description = "Assign a privilege to a SID",
2535 .name = "lsadelpriv",
2536 .returntype = RPC_RTYPE_NTSTATUS,
2537 .ntfn = cmd_lsa_del_priv,
2539 .table = &ndr_table_lsarpc,
2541 .description = "Revoke a privilege from a SID",
2545 .name = "lsaaddacctrights",
2546 .returntype = RPC_RTYPE_NTSTATUS,
2547 .ntfn = cmd_lsa_add_acct_rights,
2549 .table = &ndr_table_lsarpc,
2551 .description = "Add rights to an account",
2555 .name = "lsaremoveacctrights",
2556 .returntype = RPC_RTYPE_NTSTATUS,
2557 .ntfn = cmd_lsa_remove_acct_rights,
2559 .table = &ndr_table_lsarpc,
2561 .description = "Remove rights from an account",
2565 .name = "lsalookupprivvalue",
2566 .returntype = RPC_RTYPE_NTSTATUS,
2567 .ntfn = cmd_lsa_lookup_priv_value,
2569 .table = &ndr_table_lsarpc,
2571 .description = "Get a privilege value given its name",
2575 .name = "lsaquerysecobj",
2576 .returntype = RPC_RTYPE_NTSTATUS,
2577 .ntfn = cmd_lsa_query_secobj,
2579 .table = &ndr_table_lsarpc,
2581 .description = "Query LSA security object",
2585 .name = "lsaquerytrustdominfo",
2586 .returntype = RPC_RTYPE_NTSTATUS,
2587 .ntfn = cmd_lsa_query_trustdominfo,
2589 .table = &ndr_table_lsarpc,
2591 .description = "Query LSA trusted domains info (given a SID)",
2595 .name = "lsaquerytrustdominfobyname",
2596 .returntype = RPC_RTYPE_NTSTATUS,
2597 .ntfn = cmd_lsa_query_trustdominfobyname,
2599 .table = &ndr_table_lsarpc,
2601 .description = "Query LSA trusted domains info (given a name), only works for Windows > 2k",
2605 .name = "lsaquerytrustdominfobysid",
2606 .returntype = RPC_RTYPE_NTSTATUS,
2607 .ntfn = cmd_lsa_query_trustdominfobysid,
2609 .table = &ndr_table_lsarpc,
2611 .description = "Query LSA trusted domains info (given a SID)",
2615 .name = "lsasettrustdominfo",
2616 .returntype = RPC_RTYPE_NTSTATUS,
2617 .ntfn = cmd_lsa_set_trustdominfo,
2619 .table = &ndr_table_lsarpc,
2621 .description = "Set LSA trusted domain info",
2625 .name = "getusername",
2626 .returntype = RPC_RTYPE_NTSTATUS,
2627 .ntfn = cmd_lsa_get_username,
2629 .table = &ndr_table_lsarpc,
2631 .description = "Get username",
2635 .name = "createsecret",
2636 .returntype = RPC_RTYPE_NTSTATUS,
2637 .ntfn = cmd_lsa_create_secret,
2639 .table = &ndr_table_lsarpc,
2641 .description = "Create Secret",
2645 .name = "deletesecret",
2646 .returntype = RPC_RTYPE_NTSTATUS,
2647 .ntfn = cmd_lsa_delete_secret,
2649 .table = &ndr_table_lsarpc,
2651 .description = "Delete Secret",
2655 .name = "querysecret",
2656 .returntype = RPC_RTYPE_NTSTATUS,
2657 .ntfn = cmd_lsa_query_secret,
2659 .table = &ndr_table_lsarpc,
2661 .description = "Query Secret",
2665 .name = "setsecret",
2666 .returntype = RPC_RTYPE_NTSTATUS,
2667 .ntfn = cmd_lsa_set_secret,
2669 .table = &ndr_table_lsarpc,
2671 .description = "Set Secret",
2675 .name = "retrieveprivatedata",
2676 .returntype = RPC_RTYPE_NTSTATUS,
2677 .ntfn = cmd_lsa_retrieve_private_data,
2679 .table = &ndr_table_lsarpc,
2681 .description = "Retrieve Private Data",
2685 .name = "storeprivatedata",
2686 .returntype = RPC_RTYPE_NTSTATUS,
2687 .ntfn = cmd_lsa_store_private_data,
2689 .table = &ndr_table_lsarpc,
2691 .description = "Store Private Data",
2695 .name = "createtrustdom",
2696 .returntype = RPC_RTYPE_NTSTATUS,
2697 .ntfn = cmd_lsa_create_trusted_domain,
2699 .table = &ndr_table_lsarpc,
2701 .description = "Create Trusted Domain",
2705 .name = "deletetrustdom",
2706 .returntype = RPC_RTYPE_NTSTATUS,
2707 .ntfn = cmd_lsa_delete_trusted_domain,
2709 .table = &ndr_table_lsarpc,
2711 .description = "Delete Trusted Domain",