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 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;
276 enum lsa_SidType *types;
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,
289 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))
301 status = NT_STATUS_OK;
305 for (i = 0; i < (argc - 2); i++) {
307 sid_to_fstring(sid_str, &sids[i]);
308 printf("%s %s (%s: %d)\n", argv[i + 2], sid_str,
309 sid_type_lookup(types[i]), types[i]);
312 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
318 static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
319 TALLOC_CTX *mem_ctx, int argc,
322 NTSTATUS status, result;
325 struct lsa_String *names;
326 struct lsa_RefDomainList *domains;
327 struct lsa_TransSidArray3 sids;
330 struct dcerpc_binding_handle *b = cli->binding_handle;
333 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
340 names = talloc_array(mem_ctx, struct lsa_String, num_names);
341 NT_STATUS_HAVE_NO_MEMORY(names);
343 for (i=0; i < num_names; i++) {
344 init_lsa_String(&names[i], argv[i+1]);
347 status = dcerpc_lsa_LookupNames4(b, mem_ctx,
357 if (!NT_STATUS_IS_OK(status)) {
360 if (!NT_STATUS_IS_OK(result)) {
364 for (i = 0; i < sids.count; i++) {
366 sid_to_fstring(sid_str, sids.sids[i].sid);
367 printf("%s %s (%s: %d)\n", argv[i+1], sid_str,
368 sid_type_lookup(sids.sids[i].sid_type),
369 sids.sids[i].sid_type);
375 /* Resolve a list of SIDs to a list of names */
377 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
378 int argc, const char **argv)
380 struct policy_handle pol;
381 NTSTATUS status, result;
382 struct dom_sid *sids;
385 enum lsa_SidType *types;
387 struct dcerpc_binding_handle *b = cli->binding_handle;
390 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
394 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
395 SEC_FLAG_MAXIMUM_ALLOWED,
398 if (!NT_STATUS_IS_OK(status))
401 /* Convert arguments to sids */
403 sids = talloc_array(mem_ctx, struct dom_sid, argc - 1);
406 printf("could not allocate memory for %d sids\n", argc - 1);
410 for (i = 0; i < argc - 1; i++)
411 if (!string_to_sid(&sids[i], argv[i + 1])) {
412 status = NT_STATUS_INVALID_SID;
416 /* Lookup the SIDs */
418 status = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
419 &domains, &names, &types);
421 if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
422 NT_STATUS_V(STATUS_SOME_UNMAPPED))
425 status = NT_STATUS_OK;
429 for (i = 0; i < (argc - 1); i++) {
432 sid_to_fstring(sid_str, &sids[i]);
433 if (types[i] == SID_NAME_DOMAIN) {
434 printf("%s %s (%d)\n", sid_str,
435 domains[i] ? domains[i] : "*unknown*",
438 printf("%s %s\\%s (%d)\n", sid_str,
439 domains[i] ? domains[i] : "*unknown*",
440 names[i] ? names[i] : "*unknown*",
445 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
451 /* Resolve a list of SIDs to a list of names */
453 static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
455 int argc, const char **argv)
457 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
459 struct lsa_SidArray sids;
460 struct lsa_RefDomainList *domains;
461 struct lsa_TransNameArray2 names;
463 struct dcerpc_binding_handle *b = cli->binding_handle;
466 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
472 /* Convert arguments to sids */
474 sids.num_sids = argc-1;
475 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
477 printf("could not allocate memory for %d sids\n", sids.num_sids);
481 for (i = 0; i < sids.num_sids; i++) {
482 sids.sids[i].sid = talloc(sids.sids, struct dom_sid);
483 if (sids.sids[i].sid == NULL) {
484 status = NT_STATUS_NO_MEMORY;
487 if (!string_to_sid(sids.sids[i].sid, argv[i+1])) {
488 status = NT_STATUS_INVALID_SID;
493 /* Lookup the SIDs */
494 status = dcerpc_lsa_LookupSids3(b, mem_ctx,
503 if (!NT_STATUS_IS_OK(status)) {
506 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
507 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
512 status = NT_STATUS_OK;
516 for (i = 0; i < count; i++) {
519 sid_to_fstring(sid_str, sids.sids[i].sid);
520 printf("%s %s (%d)\n", sid_str,
521 names.names[i].name.string,
522 names.names[i].sid_type);
530 /* Enumerate list of trusted domains */
532 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
533 TALLOC_CTX *mem_ctx, int argc,
536 struct policy_handle pol;
537 NTSTATUS status, result;
538 struct lsa_DomainList domain_list;
539 struct dcerpc_binding_handle *b = cli->binding_handle;
541 /* defaults, but may be changed using params */
544 uint32_t max_size = (uint32_t)-1;
547 printf("Usage: %s [enum context (0)]\n", argv[0]);
551 if (argc == 2 && argv[1]) {
552 enum_ctx = atoi(argv[2]);
555 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
556 LSA_POLICY_VIEW_LOCAL_INFORMATION,
559 if (!NT_STATUS_IS_OK(status))
562 status = STATUS_MORE_ENTRIES;
564 while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
566 /* Lookup list of trusted domains */
568 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
574 if (!NT_STATUS_IS_OK(status)) {
577 if (!NT_STATUS_IS_OK(result) &&
578 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
579 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
584 /* Print results: list of names and sids returned in this
586 for (i = 0; i < domain_list.count; i++) {
589 sid_to_fstring(sid_str, domain_list.domains[i].sid);
591 domain_list.domains[i].name.string ?
592 domain_list.domains[i].name.string : "*unknown*",
597 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
602 /* Enumerates privileges */
604 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli,
605 TALLOC_CTX *mem_ctx, int argc,
608 struct policy_handle pol;
609 NTSTATUS status, result;
610 struct lsa_PrivArray priv_array;
611 struct dcerpc_binding_handle *b = cli->binding_handle;
613 uint32 enum_context=0;
614 uint32 pref_max_length=0x1000;
618 printf("Usage: %s [enum context] [max length]\n", argv[0]);
623 enum_context=atoi(argv[1]);
626 pref_max_length=atoi(argv[2]);
628 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
629 SEC_FLAG_MAXIMUM_ALLOWED,
632 if (!NT_STATUS_IS_OK(status))
635 status = dcerpc_lsa_EnumPrivs(b, mem_ctx,
641 if (!NT_STATUS_IS_OK(status))
643 if (!NT_STATUS_IS_OK(result)) {
649 printf("found %d privileges\n\n", priv_array.count);
651 for (i = 0; i < priv_array.count; i++) {
652 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
653 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
654 priv_array.privs[i].luid.high,
655 priv_array.privs[i].luid.low,
656 priv_array.privs[i].luid.high,
657 priv_array.privs[i].luid.low);
660 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
665 /* Get privilege name */
667 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli,
668 TALLOC_CTX *mem_ctx, int argc,
671 struct policy_handle pol;
672 NTSTATUS status, result;
673 struct dcerpc_binding_handle *b = cli->binding_handle;
676 uint16 lang_id_sys=0;
678 struct lsa_String lsa_name;
679 struct lsa_StringLarge *description = NULL;
682 printf("Usage: %s privilege name\n", argv[0]);
686 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
687 SEC_FLAG_MAXIMUM_ALLOWED,
690 if (!NT_STATUS_IS_OK(status))
693 init_lsa_String(&lsa_name, argv[1]);
695 status = dcerpc_lsa_LookupPrivDisplayName(b, mem_ctx,
703 if (!NT_STATUS_IS_OK(status))
705 if (!NT_STATUS_IS_OK(result)) {
711 printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
713 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
718 /* Enumerate the LSA SIDS */
720 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli,
721 TALLOC_CTX *mem_ctx, int argc,
724 struct policy_handle pol;
725 NTSTATUS status, result;
726 struct dcerpc_binding_handle *b = cli->binding_handle;
728 uint32 enum_context=0;
729 uint32 pref_max_length=0x1000;
730 struct lsa_SidArray sid_array;
734 printf("Usage: %s [enum context] [max length]\n", argv[0]);
739 enum_context=atoi(argv[1]);
742 pref_max_length=atoi(argv[2]);
744 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
745 SEC_FLAG_MAXIMUM_ALLOWED,
748 if (!NT_STATUS_IS_OK(status))
751 status = dcerpc_lsa_EnumAccounts(b, mem_ctx,
757 if (!NT_STATUS_IS_OK(status))
759 if (!NT_STATUS_IS_OK(result)) {
765 printf("found %d SIDs\n\n", sid_array.num_sids);
767 for (i = 0; i < sid_array.num_sids; i++) {
770 sid_to_fstring(sid_str, sid_array.sids[i].sid);
771 printf("%s\n", sid_str);
774 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
779 /* Create a new account */
781 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli,
782 TALLOC_CTX *mem_ctx, int argc,
785 struct policy_handle dom_pol;
786 struct policy_handle user_pol;
787 NTSTATUS status, result;
788 uint32 des_access = 0x000f000f;
789 struct dcerpc_binding_handle *b = cli->binding_handle;
794 printf("Usage: %s SID\n", argv[0]);
798 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
799 if (!NT_STATUS_IS_OK(status))
802 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
803 SEC_FLAG_MAXIMUM_ALLOWED,
806 if (!NT_STATUS_IS_OK(status))
809 status = dcerpc_lsa_CreateAccount(b, mem_ctx,
815 if (!NT_STATUS_IS_OK(status))
817 if (!NT_STATUS_IS_OK(result)) {
822 printf("Account for SID %s successfully created\n\n", argv[1]);
823 status = NT_STATUS_OK;
825 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
831 /* Enumerate the privileges of an SID */
833 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli,
834 TALLOC_CTX *mem_ctx, int argc,
837 struct policy_handle dom_pol;
838 struct policy_handle user_pol;
839 NTSTATUS status, result;
840 uint32 access_desired = 0x000f000f;
842 struct lsa_PrivilegeSet *privs = NULL;
844 struct dcerpc_binding_handle *b = cli->binding_handle;
847 printf("Usage: %s SID\n", argv[0]);
851 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
852 if (!NT_STATUS_IS_OK(status))
855 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
856 SEC_FLAG_MAXIMUM_ALLOWED,
859 if (!NT_STATUS_IS_OK(status))
862 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
868 if (!NT_STATUS_IS_OK(status))
870 if (!NT_STATUS_IS_OK(result)) {
875 status = dcerpc_lsa_EnumPrivsAccount(b, mem_ctx,
879 if (!NT_STATUS_IS_OK(status))
881 if (!NT_STATUS_IS_OK(result)) {
887 printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
888 printf("high\tlow\tattribute\n");
890 for (i = 0; i < privs->count; i++) {
891 printf("%u\t%u\t%u\n",
892 privs->set[i].luid.high,
893 privs->set[i].luid.low,
894 privs->set[i].attribute);
897 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
903 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
905 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli,
906 TALLOC_CTX *mem_ctx, int argc,
909 struct policy_handle dom_pol;
910 NTSTATUS status, result;
912 struct lsa_RightSet rights;
913 struct dcerpc_binding_handle *b = cli->binding_handle;
918 printf("Usage: %s SID\n", argv[0]);
922 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
923 if (!NT_STATUS_IS_OK(status))
926 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
927 SEC_FLAG_MAXIMUM_ALLOWED,
930 if (!NT_STATUS_IS_OK(status))
933 status = dcerpc_lsa_EnumAccountRights(b, mem_ctx,
938 if (!NT_STATUS_IS_OK(status))
940 if (!NT_STATUS_IS_OK(result)) {
945 printf("found %d privileges for SID %s\n", rights.count,
946 sid_string_tos(&sid));
948 for (i = 0; i < rights.count; i++) {
949 printf("\t%s\n", rights.names[i].string);
952 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
958 /* add some privileges to a SID via LsaAddAccountRights */
960 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli,
961 TALLOC_CTX *mem_ctx, int argc,
964 struct policy_handle dom_pol;
965 NTSTATUS status, result;
966 struct lsa_RightSet rights;
969 struct dcerpc_binding_handle *b = cli->binding_handle;
972 printf("Usage: %s SID [rights...]\n", argv[0]);
976 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
977 if (!NT_STATUS_IS_OK(status))
980 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
981 SEC_FLAG_MAXIMUM_ALLOWED,
984 if (!NT_STATUS_IS_OK(status))
987 rights.count = argc-2;
988 rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
991 return NT_STATUS_NO_MEMORY;
994 for (i=0; i<argc-2; i++) {
995 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
998 status = dcerpc_lsa_AddAccountRights(b, mem_ctx,
1003 if (!NT_STATUS_IS_OK(status))
1005 if (!NT_STATUS_IS_OK(result)) {
1010 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1016 /* remove some privileges to a SID via LsaRemoveAccountRights */
1018 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli,
1019 TALLOC_CTX *mem_ctx, int argc,
1022 struct policy_handle dom_pol;
1023 NTSTATUS status, result;
1024 struct lsa_RightSet rights;
1027 struct dcerpc_binding_handle *b = cli->binding_handle;
1030 printf("Usage: %s SID [rights...]\n", argv[0]);
1031 return NT_STATUS_OK;
1034 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1035 if (!NT_STATUS_IS_OK(status))
1038 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1039 SEC_FLAG_MAXIMUM_ALLOWED,
1042 if (!NT_STATUS_IS_OK(status))
1045 rights.count = argc-2;
1046 rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
1048 if (!rights.names) {
1049 return NT_STATUS_NO_MEMORY;
1052 for (i=0; i<argc-2; i++) {
1053 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
1056 status = dcerpc_lsa_RemoveAccountRights(b, mem_ctx,
1062 if (!NT_STATUS_IS_OK(status))
1064 if (!NT_STATUS_IS_OK(result)) {
1069 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1076 /* Get a privilege value given its name */
1078 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli,
1079 TALLOC_CTX *mem_ctx, int argc,
1082 struct policy_handle pol;
1083 NTSTATUS status, result;
1084 struct lsa_LUID luid;
1085 struct lsa_String name;
1086 struct dcerpc_binding_handle *b = cli->binding_handle;
1089 printf("Usage: %s name\n", argv[0]);
1090 return NT_STATUS_OK;
1093 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1094 SEC_FLAG_MAXIMUM_ALLOWED,
1097 if (!NT_STATUS_IS_OK(status))
1100 init_lsa_String(&name, argv[1]);
1102 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1107 if (!NT_STATUS_IS_OK(status))
1109 if (!NT_STATUS_IS_OK(result)) {
1116 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
1118 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1123 /* Query LSA security object */
1125 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli,
1126 TALLOC_CTX *mem_ctx, int argc,
1129 struct policy_handle pol;
1130 NTSTATUS status, result;
1131 struct sec_desc_buf *sdb;
1132 uint32 sec_info = SECINFO_DACL;
1133 struct dcerpc_binding_handle *b = cli->binding_handle;
1135 if (argc < 1 || argc > 2) {
1136 printf("Usage: %s [sec_info]\n", argv[0]);
1137 return NT_STATUS_OK;
1140 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1141 SEC_FLAG_MAXIMUM_ALLOWED,
1145 sscanf(argv[1], "%x", &sec_info);
1147 if (!NT_STATUS_IS_OK(status))
1150 status = dcerpc_lsa_QuerySecurity(b, mem_ctx,
1155 if (!NT_STATUS_IS_OK(status))
1157 if (!NT_STATUS_IS_OK(result)) {
1164 display_sec_desc(sdb->sd);
1166 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1171 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
1172 DATA_BLOB session_key)
1174 char *pwd, *pwd_old;
1176 DATA_BLOB data = data_blob_const(p->password->data, p->password->length);
1177 DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
1179 pwd = sess_decrypt_string(talloc_tos(), &data, &session_key);
1180 pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key);
1182 d_printf("Password:\t%s\n", pwd);
1183 d_printf("Old Password:\t%s\n", pwd_old);
1186 talloc_free(pwd_old);
1189 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
1190 union lsa_TrustedDomainInfo *info,
1191 enum lsa_TrustDomInfoEnum info_class,
1192 DATA_BLOB session_key)
1194 switch (info_class) {
1195 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
1196 display_trust_dom_info_4(&info->password, session_key);
1199 const char *str = NULL;
1200 str = NDR_PRINT_UNION_STRING(mem_ctx,
1201 lsa_TrustedDomainInfo,
1204 d_printf("%s\n", str);
1211 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
1212 TALLOC_CTX *mem_ctx, int argc,
1215 struct policy_handle pol;
1216 NTSTATUS status, result;
1217 struct dom_sid dom_sid;
1218 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1219 union lsa_TrustedDomainInfo *info = NULL;
1220 enum lsa_TrustDomInfoEnum info_class = 1;
1221 DATA_BLOB session_key;
1222 struct dcerpc_binding_handle *b = cli->binding_handle;
1224 if (argc > 3 || argc < 2) {
1225 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1226 return NT_STATUS_OK;
1229 if (!string_to_sid(&dom_sid, argv[1]))
1230 return NT_STATUS_NO_MEMORY;
1233 info_class = atoi(argv[2]);
1235 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1237 if (!NT_STATUS_IS_OK(status))
1240 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
1246 if (!NT_STATUS_IS_OK(status))
1248 if (!NT_STATUS_IS_OK(result)) {
1253 status = cli_get_session_key(mem_ctx, cli, &session_key);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1259 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1262 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1267 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1268 TALLOC_CTX *mem_ctx, int argc,
1271 struct policy_handle pol;
1272 NTSTATUS status, result;
1273 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1274 union lsa_TrustedDomainInfo *info = NULL;
1275 enum lsa_TrustDomInfoEnum info_class = 1;
1276 struct lsa_String trusted_domain;
1277 struct dcerpc_binding_handle *b = cli->binding_handle;
1278 DATA_BLOB session_key;
1280 if (argc > 3 || argc < 2) {
1281 printf("Usage: %s [name] [info_class]\n", argv[0]);
1282 return NT_STATUS_OK;
1286 info_class = atoi(argv[2]);
1288 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1290 if (!NT_STATUS_IS_OK(status))
1293 init_lsa_String(&trusted_domain, argv[1]);
1295 status = dcerpc_lsa_QueryTrustedDomainInfoByName(b, mem_ctx,
1301 if (!NT_STATUS_IS_OK(status))
1303 if (!NT_STATUS_IS_OK(result)) {
1308 status = cli_get_session_key(mem_ctx, cli, &session_key);
1309 if (!NT_STATUS_IS_OK(status)) {
1310 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1314 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1317 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1322 static NTSTATUS cmd_lsa_set_trustdominfo(struct rpc_pipe_client *cli,
1323 TALLOC_CTX *mem_ctx, int argc,
1326 struct policy_handle pol, trustdom_pol;
1327 NTSTATUS status, result;
1328 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1329 union lsa_TrustedDomainInfo info;
1330 struct dom_sid dom_sid;
1331 enum lsa_TrustDomInfoEnum info_class = 1;
1332 struct dcerpc_binding_handle *b = cli->binding_handle;
1334 if (argc > 4 || argc < 3) {
1335 printf("Usage: %s [sid] [info_class] [value]\n", argv[0]);
1336 return NT_STATUS_OK;
1339 if (!string_to_sid(&dom_sid, argv[1])) {
1340 return NT_STATUS_NO_MEMORY;
1344 info_class = atoi(argv[2]);
1346 switch (info_class) {
1347 case 13: /* LSA_TRUSTED_DOMAIN_SUPPORTED_ENCRYPTION_TYPES */
1348 info.enc_types.enc_types = atoi(argv[3]);
1351 return NT_STATUS_INVALID_PARAMETER;
1354 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1355 if (!NT_STATUS_IS_OK(status)) {
1359 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
1365 if (!NT_STATUS_IS_OK(status)) {
1368 if (!NT_STATUS_IS_OK(result)) {
1373 status = dcerpc_lsa_SetInformationTrustedDomain(b, mem_ctx,
1378 if (!NT_STATUS_IS_OK(status)) {
1381 if (!NT_STATUS_IS_OK(result)) {
1386 dcerpc_lsa_Close(b, mem_ctx, &trustdom_pol, &result);
1387 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1392 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1393 TALLOC_CTX *mem_ctx, int argc,
1396 struct policy_handle pol, trustdom_pol;
1397 NTSTATUS status, result;
1398 uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1399 union lsa_TrustedDomainInfo *info = NULL;
1400 struct dom_sid dom_sid;
1401 enum lsa_TrustDomInfoEnum info_class = 1;
1402 DATA_BLOB session_key;
1403 struct dcerpc_binding_handle *b = cli->binding_handle;
1405 if (argc > 3 || argc < 2) {
1406 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1407 return NT_STATUS_OK;
1410 if (!string_to_sid(&dom_sid, argv[1]))
1411 return NT_STATUS_NO_MEMORY;
1415 info_class = atoi(argv[2]);
1417 status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1419 if (!NT_STATUS_IS_OK(status))
1422 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
1428 if (!NT_STATUS_IS_OK(status))
1430 if (!NT_STATUS_IS_OK(result)) {
1435 status = dcerpc_lsa_QueryTrustedDomainInfo(b, mem_ctx,
1440 if (!NT_STATUS_IS_OK(status))
1442 if (!NT_STATUS_IS_OK(result)) {
1447 status = cli_get_session_key(mem_ctx, cli, &session_key);
1448 if (!NT_STATUS_IS_OK(status)) {
1449 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
1453 display_trust_dom_info(mem_ctx, info, info_class, session_key);
1456 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1461 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1462 TALLOC_CTX *mem_ctx, int argc,
1465 struct policy_handle pol;
1466 NTSTATUS status, result;
1467 const char *servername = cli->desthost;
1468 struct lsa_String *account_name = NULL;
1469 struct lsa_String *authority_name = NULL;
1470 struct dcerpc_binding_handle *b = cli->binding_handle;
1473 printf("Usage: %s servername\n", argv[0]);
1474 return NT_STATUS_OK;
1477 status = rpccli_lsa_open_policy(cli, mem_ctx, true,
1478 SEC_FLAG_MAXIMUM_ALLOWED,
1481 if (!NT_STATUS_IS_OK(status)) {
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 :
1504 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
1509 static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
1510 TALLOC_CTX *mem_ctx, int argc,
1513 struct policy_handle dom_pol, user_pol;
1514 NTSTATUS status, result;
1515 struct lsa_PrivilegeSet privs;
1516 struct lsa_LUIDAttribute *set = NULL;
1519 struct dcerpc_binding_handle *b = cli->binding_handle;
1524 printf("Usage: %s SID [rights...]\n", argv[0]);
1525 return NT_STATUS_OK;
1528 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1529 if (!NT_STATUS_IS_OK(status)) {
1533 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1534 SEC_FLAG_MAXIMUM_ALLOWED,
1537 if (!NT_STATUS_IS_OK(status)) {
1541 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1544 SEC_FLAG_MAXIMUM_ALLOWED,
1547 if (!NT_STATUS_IS_OK(status)) {
1550 if (!NT_STATUS_IS_OK(result)) {
1555 for (i=2; i<argc; i++) {
1557 struct lsa_String priv_name;
1558 struct lsa_LUID luid;
1560 init_lsa_String(&priv_name, argv[i]);
1562 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1567 if (!NT_STATUS_IS_OK(status)) {
1570 if (!NT_STATUS_IS_OK(result)) {
1576 set = talloc_realloc(mem_ctx, set,
1577 struct lsa_LUIDAttribute,
1580 return NT_STATUS_NO_MEMORY;
1583 set[privs.count-1].luid = luid;
1584 set[privs.count-1].attribute = 0;
1589 status = dcerpc_lsa_AddPrivilegesToAccount(b, mem_ctx,
1593 if (!NT_STATUS_IS_OK(status)) {
1596 if (!NT_STATUS_IS_OK(result)) {
1601 dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
1602 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
1607 static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
1608 TALLOC_CTX *mem_ctx, int argc,
1611 struct policy_handle dom_pol, user_pol;
1612 NTSTATUS status, result;
1613 struct lsa_PrivilegeSet privs;
1614 struct lsa_LUIDAttribute *set = NULL;
1617 struct dcerpc_binding_handle *b = cli->binding_handle;
1622 printf("Usage: %s SID [rights...]\n", argv[0]);
1623 return NT_STATUS_OK;
1626 status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
1627 if (!NT_STATUS_IS_OK(status)) {
1631 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1632 SEC_FLAG_MAXIMUM_ALLOWED,
1635 if (!NT_STATUS_IS_OK(status)) {
1639 status = dcerpc_lsa_OpenAccount(b, mem_ctx,
1642 SEC_FLAG_MAXIMUM_ALLOWED,
1645 if (!NT_STATUS_IS_OK(status)) {
1648 if (!NT_STATUS_IS_OK(result)) {
1653 for (i=2; i<argc; i++) {
1655 struct lsa_String priv_name;
1656 struct lsa_LUID luid;
1658 init_lsa_String(&priv_name, argv[i]);
1660 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
1665 if (!NT_STATUS_IS_OK(status)) {
1668 if (!NT_STATUS_IS_OK(result)) {
1674 set = talloc_realloc(mem_ctx, set,
1675 struct lsa_LUIDAttribute,
1678 return NT_STATUS_NO_MEMORY;
1681 set[privs.count-1].luid = luid;
1682 set[privs.count-1].attribute = 0;
1688 status = dcerpc_lsa_RemovePrivilegesFromAccount(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_create_secret(struct rpc_pipe_client *cli,
1708 TALLOC_CTX *mem_ctx, int argc,
1711 NTSTATUS status, result;
1712 struct policy_handle handle, sec_handle;
1713 struct lsa_String name;
1714 struct dcerpc_binding_handle *b = cli->binding_handle;
1717 printf("Usage: %s name\n", argv[0]);
1718 return NT_STATUS_OK;
1721 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1723 SEC_FLAG_MAXIMUM_ALLOWED,
1725 if (!NT_STATUS_IS_OK(status)) {
1729 init_lsa_String(&name, argv[1]);
1731 status = dcerpc_lsa_CreateSecret(b, mem_ctx,
1734 SEC_FLAG_MAXIMUM_ALLOWED,
1737 if (!NT_STATUS_IS_OK(status)) {
1740 if (!NT_STATUS_IS_OK(result)) {
1746 if (is_valid_policy_hnd(&sec_handle)) {
1747 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1749 if (is_valid_policy_hnd(&handle)) {
1750 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1756 static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
1757 TALLOC_CTX *mem_ctx, int argc,
1760 NTSTATUS status, result;
1761 struct policy_handle handle, sec_handle;
1762 struct lsa_String name;
1763 struct dcerpc_binding_handle *b = cli->binding_handle;
1766 printf("Usage: %s name\n", argv[0]);
1767 return NT_STATUS_OK;
1770 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1772 SEC_FLAG_MAXIMUM_ALLOWED,
1774 if (!NT_STATUS_IS_OK(status)) {
1778 init_lsa_String(&name, argv[1]);
1780 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
1783 SEC_FLAG_MAXIMUM_ALLOWED,
1786 if (!NT_STATUS_IS_OK(status)) {
1789 if (!NT_STATUS_IS_OK(result)) {
1794 status = dcerpc_lsa_DeleteObject(b, mem_ctx,
1797 if (!NT_STATUS_IS_OK(status)) {
1800 if (!NT_STATUS_IS_OK(result)) {
1806 if (is_valid_policy_hnd(&sec_handle)) {
1807 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1809 if (is_valid_policy_hnd(&handle)) {
1810 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1816 static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
1817 TALLOC_CTX *mem_ctx, int argc,
1820 NTSTATUS status, result;
1821 struct policy_handle handle, sec_handle;
1822 struct lsa_String name;
1823 struct lsa_DATA_BUF_PTR new_val;
1824 NTTIME new_mtime = 0;
1825 struct lsa_DATA_BUF_PTR old_val;
1826 NTTIME old_mtime = 0;
1827 DATA_BLOB session_key;
1828 DATA_BLOB new_blob = data_blob_null;
1829 DATA_BLOB old_blob = data_blob_null;
1830 char *new_secret, *old_secret;
1831 struct dcerpc_binding_handle *b = cli->binding_handle;
1834 printf("Usage: %s name\n", argv[0]);
1835 return NT_STATUS_OK;
1838 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1840 SEC_FLAG_MAXIMUM_ALLOWED,
1842 if (!NT_STATUS_IS_OK(status)) {
1846 init_lsa_String(&name, argv[1]);
1848 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
1851 SEC_FLAG_MAXIMUM_ALLOWED,
1854 if (!NT_STATUS_IS_OK(status)) {
1857 if (!NT_STATUS_IS_OK(result)) {
1862 ZERO_STRUCT(new_val);
1863 ZERO_STRUCT(old_val);
1865 status = dcerpc_lsa_QuerySecret(b, mem_ctx,
1872 if (!NT_STATUS_IS_OK(status)) {
1875 if (!NT_STATUS_IS_OK(result)) {
1880 status = cli_get_session_key(mem_ctx, cli, &session_key);
1881 if (!NT_STATUS_IS_OK(status)) {
1886 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
1889 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
1892 new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
1893 old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
1895 d_printf("new secret: %s\n", new_secret);
1898 d_printf("old secret: %s\n", old_secret);
1902 if (is_valid_policy_hnd(&sec_handle)) {
1903 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1905 if (is_valid_policy_hnd(&handle)) {
1906 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1912 static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
1913 TALLOC_CTX *mem_ctx, int argc,
1916 NTSTATUS status, result;
1917 struct policy_handle handle, sec_handle;
1918 struct lsa_String name;
1919 struct lsa_DATA_BUF new_val;
1920 struct lsa_DATA_BUF old_val;
1922 DATA_BLOB session_key;
1923 struct dcerpc_binding_handle *b = cli->binding_handle;
1926 printf("Usage: %s name secret\n", argv[0]);
1927 return NT_STATUS_OK;
1930 status = rpccli_lsa_open_policy2(cli, mem_ctx,
1932 SEC_FLAG_MAXIMUM_ALLOWED,
1934 if (!NT_STATUS_IS_OK(status)) {
1938 init_lsa_String(&name, argv[1]);
1940 status = dcerpc_lsa_OpenSecret(b, mem_ctx,
1943 SEC_FLAG_MAXIMUM_ALLOWED,
1946 if (!NT_STATUS_IS_OK(status)) {
1949 if (!NT_STATUS_IS_OK(result)) {
1954 ZERO_STRUCT(new_val);
1955 ZERO_STRUCT(old_val);
1957 status = cli_get_session_key(mem_ctx, cli, &session_key);
1958 if (!NT_STATUS_IS_OK(status)) {
1962 enc_key = sess_encrypt_string(argv[2], &session_key);
1964 new_val.length = enc_key.length;
1965 new_val.size = enc_key.length;
1966 new_val.data = enc_key.data;
1968 status = dcerpc_lsa_SetSecret(b, mem_ctx,
1973 if (!NT_STATUS_IS_OK(status)) {
1976 if (!NT_STATUS_IS_OK(result)) {
1982 if (is_valid_policy_hnd(&sec_handle)) {
1983 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
1985 if (is_valid_policy_hnd(&handle)) {
1986 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
1992 static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
1993 TALLOC_CTX *mem_ctx, int argc,
1996 NTSTATUS status, result;
1997 struct policy_handle handle;
1998 struct lsa_String name;
1999 struct lsa_DATA_BUF *val;
2000 DATA_BLOB session_key;
2001 DATA_BLOB blob = data_blob_null;
2003 struct dcerpc_binding_handle *b = cli->binding_handle;
2006 printf("Usage: %s name\n", argv[0]);
2007 return NT_STATUS_OK;
2010 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2012 SEC_FLAG_MAXIMUM_ALLOWED,
2014 if (!NT_STATUS_IS_OK(status)) {
2018 init_lsa_String(&name, argv[1]);
2022 status = dcerpc_lsa_RetrievePrivateData(b, mem_ctx,
2027 if (!NT_STATUS_IS_OK(status)) {
2030 if (!NT_STATUS_IS_OK(result)) {
2035 status = cli_get_session_key(mem_ctx, cli, &session_key);
2036 if (!NT_STATUS_IS_OK(status)) {
2041 blob = data_blob_const(val->data, val->length);
2044 secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
2046 d_printf("secret: %s\n", secret);
2050 if (is_valid_policy_hnd(&handle)) {
2051 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2057 static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
2058 TALLOC_CTX *mem_ctx, int argc,
2061 NTSTATUS status, result;
2062 struct policy_handle handle;
2063 struct lsa_String name;
2064 struct lsa_DATA_BUF val;
2065 DATA_BLOB session_key;
2067 struct dcerpc_binding_handle *b = cli->binding_handle;
2070 printf("Usage: %s name secret\n", argv[0]);
2071 return NT_STATUS_OK;
2074 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2076 SEC_FLAG_MAXIMUM_ALLOWED,
2078 if (!NT_STATUS_IS_OK(status)) {
2082 init_lsa_String(&name, argv[1]);
2086 status = cli_get_session_key(mem_ctx, cli, &session_key);
2087 if (!NT_STATUS_IS_OK(status)) {
2091 enc_key = sess_encrypt_string(argv[2], &session_key);
2093 val.length = enc_key.length;
2094 val.size = enc_key.length;
2095 val.data = enc_key.data;
2097 status = dcerpc_lsa_StorePrivateData(b, mem_ctx,
2102 if (!NT_STATUS_IS_OK(status)) {
2105 if (!NT_STATUS_IS_OK(result)) {
2111 if (is_valid_policy_hnd(&handle)) {
2112 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2118 static NTSTATUS cmd_lsa_create_trusted_domain(struct rpc_pipe_client *cli,
2119 TALLOC_CTX *mem_ctx, int argc,
2122 NTSTATUS status, result;
2123 struct policy_handle handle, trustdom_handle;
2125 struct lsa_DomainInfo info;
2126 struct dcerpc_binding_handle *b = cli->binding_handle;
2129 printf("Usage: %s name sid\n", argv[0]);
2130 return NT_STATUS_OK;
2133 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2135 SEC_FLAG_MAXIMUM_ALLOWED,
2137 if (!NT_STATUS_IS_OK(status)) {
2141 init_lsa_StringLarge(&info.name, argv[1]);
2143 string_to_sid(&sid, argv[2]);
2145 status = dcerpc_lsa_CreateTrustedDomain(b, mem_ctx,
2148 SEC_FLAG_MAXIMUM_ALLOWED,
2151 if (!NT_STATUS_IS_OK(status)) {
2154 if (!NT_STATUS_IS_OK(result)) {
2160 if (is_valid_policy_hnd(&trustdom_handle)) {
2161 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
2164 if (is_valid_policy_hnd(&handle)) {
2165 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2171 static NTSTATUS cmd_lsa_delete_trusted_domain(struct rpc_pipe_client *cli,
2172 TALLOC_CTX *mem_ctx, int argc,
2175 NTSTATUS status, result;
2176 struct policy_handle handle, trustdom_handle;
2177 struct lsa_String name;
2178 struct dom_sid *sid = NULL;
2179 struct dcerpc_binding_handle *b = cli->binding_handle;
2182 printf("Usage: %s name\n", argv[0]);
2183 return NT_STATUS_OK;
2186 status = rpccli_lsa_open_policy2(cli, mem_ctx,
2188 SEC_FLAG_MAXIMUM_ALLOWED,
2190 if (!NT_STATUS_IS_OK(status)) {
2194 init_lsa_String(&name, argv[1]);
2196 status = dcerpc_lsa_OpenTrustedDomainByName(b, mem_ctx,
2199 SEC_FLAG_MAXIMUM_ALLOWED,
2202 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2207 uint32_t resume_handle = 0;
2208 struct lsa_DomainList domains;
2211 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
2217 if (!NT_STATUS_IS_OK(status)) {
2220 if (!NT_STATUS_IS_OK(result)) {
2225 for (i=0; i < domains.count; i++) {
2226 if (strequal(domains.domains[i].name.string, argv[1])) {
2227 sid = domains.domains[i].sid;
2233 return NT_STATUS_INVALID_SID;
2237 status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
2240 SEC_FLAG_MAXIMUM_ALLOWED,
2243 if (!NT_STATUS_IS_OK(status)) {
2246 if (!NT_STATUS_IS_OK(result)) {
2252 status = dcerpc_lsa_DeleteObject(b, mem_ctx,
2255 if (!NT_STATUS_IS_OK(status)) {
2258 if (!NT_STATUS_IS_OK(result)) {
2264 if (is_valid_policy_hnd(&trustdom_handle)) {
2265 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
2268 if (is_valid_policy_hnd(&handle)) {
2269 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
2276 /* List of commands exported by this module */
2278 struct cmd_set lsarpc_commands[] = {
2282 { "lsaquery", RPC_RTYPE_NTSTATUS, cmd_lsa_query_info_policy, NULL, &ndr_table_lsarpc, NULL, "Query info policy", "" },
2283 { "lookupsids", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids, NULL, &ndr_table_lsarpc, NULL, "Convert SIDs to names", "" },
2284 { "lookupsids3", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids3, NULL, &ndr_table_lsarpc, NULL, "Convert SIDs to names", "" },
2285 { "lookupnames", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names, NULL, &ndr_table_lsarpc, NULL, "Convert names to SIDs", "" },
2286 { "lookupnames4", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names4, NULL, &ndr_table_lsarpc, NULL, "Convert names to SIDs", "" },
2287 { "lookupnames_level", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names_level, NULL, &ndr_table_lsarpc, NULL, "Convert names to SIDs", "" },
2288 { "enumtrust", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_trust_dom, NULL, &ndr_table_lsarpc, NULL, "Enumerate trusted domains", "Usage: [preferred max number] [enum context (0)]" },
2289 { "enumprivs", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privilege, NULL, &ndr_table_lsarpc, NULL, "Enumerate privileges", "" },
2290 { "getdispname", RPC_RTYPE_NTSTATUS, cmd_lsa_get_dispname, NULL, &ndr_table_lsarpc, NULL, "Get the privilege name", "" },
2291 { "lsaenumsid", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_sids, NULL, &ndr_table_lsarpc, NULL, "Enumerate the LSA SIDS", "" },
2292 { "lsacreateaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_create_account, NULL, &ndr_table_lsarpc, NULL, "Create a new lsa account", "" },
2293 { "lsaenumprivsaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privsaccounts, NULL, &ndr_table_lsarpc, NULL, "Enumerate the privileges of an SID", "" },
2294 { "lsaenumacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_acct_rights, NULL, &ndr_table_lsarpc, NULL, "Enumerate the rights of an SID", "" },
2295 { "lsaaddpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_add_priv, NULL, &ndr_table_lsarpc, NULL, "Assign a privilege to a SID", "" },
2296 { "lsadelpriv", RPC_RTYPE_NTSTATUS, cmd_lsa_del_priv, NULL, &ndr_table_lsarpc, NULL, "Revoke a privilege from a SID", "" },
2297 { "lsaaddacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_add_acct_rights, NULL, &ndr_table_lsarpc, NULL, "Add rights to an account", "" },
2298 { "lsaremoveacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_remove_acct_rights, NULL, &ndr_table_lsarpc, NULL, "Remove rights from an account", "" },
2299 { "lsalookupprivvalue", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_priv_value, NULL, &ndr_table_lsarpc, NULL, "Get a privilege value given its name", "" },
2300 { "lsaquerysecobj", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secobj, NULL, &ndr_table_lsarpc, NULL, "Query LSA security object", "" },
2301 { "lsaquerytrustdominfo",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfo, NULL, &ndr_table_lsarpc, NULL, "Query LSA trusted domains info (given a SID)", "" },
2302 { "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", "" },
2303 { "lsaquerytrustdominfobysid",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobysid, NULL, &ndr_table_lsarpc, NULL, "Query LSA trusted domains info (given a SID)", "" },
2304 { "lsasettrustdominfo", RPC_RTYPE_NTSTATUS, cmd_lsa_set_trustdominfo, NULL, &ndr_table_lsarpc, NULL, "Set LSA trusted domain info", "" },
2305 { "getusername", RPC_RTYPE_NTSTATUS, cmd_lsa_get_username, NULL, &ndr_table_lsarpc, NULL, "Get username", "" },
2306 { "createsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_create_secret, NULL, &ndr_table_lsarpc, NULL, "Create Secret", "" },
2307 { "deletesecret", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_secret, NULL, &ndr_table_lsarpc, NULL, "Delete Secret", "" },
2308 { "querysecret", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secret, NULL, &ndr_table_lsarpc, NULL, "Query Secret", "" },
2309 { "setsecret", RPC_RTYPE_NTSTATUS, cmd_lsa_set_secret, NULL, &ndr_table_lsarpc, NULL, "Set Secret", "" },
2310 { "retrieveprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_retrieve_private_data, NULL, &ndr_table_lsarpc, NULL, "Retrieve Private Data", "" },
2311 { "storeprivatedata", RPC_RTYPE_NTSTATUS, cmd_lsa_store_private_data, NULL, &ndr_table_lsarpc, NULL, "Store Private Data", "" },
2312 { "createtrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_create_trusted_domain, NULL, &ndr_table_lsarpc, NULL, "Create Trusted Domain", "" },
2313 { "deletetrustdom", RPC_RTYPE_NTSTATUS, cmd_lsa_delete_trusted_domain, NULL, &ndr_table_lsarpc, NULL, "Delete Trusted Domain", "" },