LCOV - code coverage report
Current view: top level - source3/rpcclient - cmd_lsarpc.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 129 1092 11.8 %
Date: 2023-11-21 12:31:41 Functions: 6 44 13.6 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    RPC pipe client
       4             : 
       5             :    Copyright (C) Tim Potter              2000
       6             :    Copyright (C) Rafal Szczesniak        2002
       7             :    Copyright (C) Guenther Deschner       2008
       8             : 
       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.
      13             : 
      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.
      18             : 
      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/>.
      21             : */
      22             : 
      23             : #include "includes.h"
      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"
      31             : 
      32             : /* useful function to allow entering a name instead of a SID and
      33             :  * looking it up automatically */
      34           0 : static NTSTATUS name_to_sid(struct rpc_pipe_client *cli, 
      35             :                             TALLOC_CTX *mem_ctx,
      36             :                             struct dom_sid *sid, const char *name)
      37             : {
      38             :         struct policy_handle pol;
      39             :         enum lsa_SidType *sid_types;
      40             :         NTSTATUS status, result;
      41             :         struct dom_sid *sids;
      42           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
      43             : 
      44             :         /* maybe its a raw SID */
      45           0 :         if (strncmp(name, "S-", 2) == 0 &&
      46           0 :             string_to_sid(sid, name)) {
      47           0 :                 return NT_STATUS_OK;
      48             :         }
      49             : 
      50           0 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
      51             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
      52             :                                      &pol);
      53           0 :         if (!NT_STATUS_IS_OK(status))
      54           0 :                 goto done;
      55             : 
      56           0 :         status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, NULL, 1, &sids, &sid_types);
      57           0 :         if (!NT_STATUS_IS_OK(status))
      58           0 :                 goto done;
      59             : 
      60           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
      61             : 
      62           0 :         *sid = sids[0];
      63             : 
      64           0 : done:
      65           0 :         return status;
      66             : }
      67             : 
      68           0 : static void display_query_info_1(struct lsa_AuditLogInfo *r)
      69             : {
      70           0 :         d_printf("percent_full:\t%d\n", r->percent_full);
      71           0 :         d_printf("maximum_log_size:\t%d\n", r->maximum_log_size);
      72           0 :         d_printf("retention_time:\t%lld\n", (long long)r->retention_time);
      73           0 :         d_printf("shutdown_in_progress:\t%d\n", r->shutdown_in_progress);
      74           0 :         d_printf("time_to_shutdown:\t%lld\n", (long long)r->time_to_shutdown);
      75           0 :         d_printf("next_audit_record:\t%d\n", r->next_audit_record);
      76           0 : }
      77             : 
      78           0 : static void display_query_info_2(struct lsa_AuditEventsInfo *r)
      79             : {
      80             :         int i;
      81           0 :         d_printf("Auditing enabled:\t%d\n", r->auditing_mode);
      82           0 :         d_printf("Auditing categories:\t%d\n", r->count);
      83           0 :         d_printf("Auditsettings:\n");
      84           0 :         for (i=0; i<r->count; i++) {
      85           0 :                 const char *val = audit_policy_str(talloc_tos(), r->settings[i]);
      86           0 :                 const char *policy = audit_description_str(i);
      87           0 :                 d_printf("%s:\t%s\n", policy, val);
      88             :         }
      89           0 : }
      90             : 
      91           0 : static void display_query_info_3(struct lsa_DomainInfo *r)
      92             : {
      93             :         struct dom_sid_buf buf;
      94           0 :         d_printf("Domain Name: %s\n", r->name.string);
      95           0 :         d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
      96           0 : }
      97             : 
      98           0 : static void display_query_info_5(struct lsa_DomainInfo *r)
      99             : {
     100             :         struct dom_sid_buf buf;
     101           0 :         d_printf("Domain Name: %s\n", r->name.string);
     102           0 :         d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
     103           0 : }
     104             : 
     105           0 : static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
     106             : {
     107           0 :         d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
     108           0 : }
     109             : 
     110           0 : static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
     111             : {
     112           0 :         d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
     113           0 :         d_printf("Log is full: %d\n", r->log_is_full);
     114           0 : }
     115             : 
     116           0 : static void display_query_info_12(struct lsa_DnsDomainInfo *r)
     117             : {
     118             :         struct dom_sid_buf buf;
     119           0 :         d_printf("Domain NetBios Name: %s\n", r->name.string);
     120           0 :         d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
     121           0 :         d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
     122           0 :         d_printf("Domain Sid: %s\n", dom_sid_str_buf(r->sid, &buf));
     123           0 :         d_printf("Domain GUID: %s\n", GUID_string(talloc_tos(),
     124           0 :                                                       &r->domain_guid));
     125           0 : }
     126             : 
     127           0 : static void display_lsa_query_info(union lsa_PolicyInformation *info,
     128             :                                    enum lsa_PolicyInfo level)
     129             : {
     130           0 :         switch (level) {
     131           0 :                 case 1:
     132           0 :                         display_query_info_1(&info->audit_log);
     133           0 :                         break;
     134           0 :                 case 2:
     135           0 :                         display_query_info_2(&info->audit_events);
     136           0 :                         break;
     137           0 :                 case 3:
     138           0 :                         display_query_info_3(&info->domain);
     139           0 :                         break;
     140           0 :                 case 5:
     141           0 :                         display_query_info_5(&info->account_domain);
     142           0 :                         break;
     143           0 :                 case 10:
     144           0 :                         display_query_info_10(&info->auditfullset);
     145           0 :                         break;
     146           0 :                 case 11:
     147           0 :                         display_query_info_11(&info->auditfullquery);
     148           0 :                         break;
     149           0 :                 case 12:
     150           0 :                         display_query_info_12(&info->dns);
     151           0 :                         break;
     152           0 :                 default:
     153           0 :                         printf("can't display info level: %d\n", level);
     154           0 :                         break;
     155             :         }
     156           0 : }
     157             : 
     158           0 : static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli, 
     159             :                                           TALLOC_CTX *mem_ctx, int argc, 
     160             :                                           const char **argv) 
     161             : {
     162             :         struct policy_handle pol;
     163             :         NTSTATUS status, result;
     164           0 :         union lsa_PolicyInformation *info = NULL;
     165           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     166             : 
     167           0 :         uint32_t info_class = 3;
     168             : 
     169           0 :         if (argc > 2) {
     170           0 :                 printf("Usage: %s [info_class]\n", argv[0]);
     171           0 :                 return NT_STATUS_OK;
     172             :         }
     173             : 
     174           0 :         if (argc == 2)
     175           0 :                 info_class = atoi(argv[1]);
     176             : 
     177           0 :         switch (info_class) {
     178           0 :         case 12:
     179           0 :                 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
     180             :                                                  SEC_FLAG_MAXIMUM_ALLOWED,
     181             :                                                  &pol);
     182             : 
     183           0 :                 if (!NT_STATUS_IS_OK(status))
     184           0 :                         goto done;
     185             : 
     186           0 :                 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
     187             :                                                      &pol,
     188             :                                                      info_class,
     189             :                                                      &info,
     190             :                                                      &result);
     191           0 :                 break;
     192           0 :         default:
     193           0 :                 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     194             :                                                 SEC_FLAG_MAXIMUM_ALLOWED,
     195             :                                                 &pol);
     196             : 
     197           0 :                 if (!NT_STATUS_IS_OK(status))
     198           0 :                         goto done;
     199             : 
     200           0 :                 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
     201             :                                                     &pol,
     202             :                                                     info_class,
     203             :                                                     &info,
     204             :                                                     &result);
     205             :         }
     206             : 
     207           0 :         if (!NT_STATUS_IS_OK(status)) {
     208           0 :                 goto done;
     209             :         }
     210           0 :         status = result;
     211           0 :         if (NT_STATUS_IS_OK(result)) {
     212           0 :                 display_lsa_query_info(info, info_class);
     213             :         }
     214             : 
     215           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     216             : 
     217           0 :  done:
     218           0 :         return status;
     219             : }
     220             : 
     221             : /* Resolve a list of names to a list of sids */
     222             : 
     223           2 : static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli, 
     224             :                                      TALLOC_CTX *mem_ctx, int argc, 
     225             :                                      const char **argv)
     226             : {
     227             :         struct policy_handle pol;
     228             :         NTSTATUS status, result;
     229             :         struct dom_sid *sids;
     230             :         enum lsa_SidType *types;
     231             :         int i;
     232           2 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     233             : 
     234           2 :         if (argc == 1) {
     235           0 :                 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
     236           0 :                 return NT_STATUS_OK;
     237             :         }
     238             : 
     239           2 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     240             :                                         LSA_POLICY_LOOKUP_NAMES,
     241             :                                         &pol);
     242             : 
     243           2 :         if (!NT_STATUS_IS_OK(status))
     244           0 :                 goto done;
     245             : 
     246           2 :         status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
     247             :                                       (const char**)(argv + 1), NULL, 1, &sids, &types);
     248             : 
     249           2 :         if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
     250           0 :             NT_STATUS_V(STATUS_SOME_UNMAPPED))
     251           0 :                 goto done;
     252             : 
     253           2 :         status = NT_STATUS_OK;
     254             : 
     255             :         /* Print results */
     256             : 
     257           4 :         for (i = 0; i < (argc - 1); i++) {
     258             :                 struct dom_sid_buf sid_str;
     259           6 :                 printf("%s %s (%s: %d)\n",
     260           2 :                        argv[i + 1],
     261           2 :                        dom_sid_str_buf(&sids[i], &sid_str),
     262           2 :                        sid_type_lookup(types[i]),
     263           2 :                        types[i]);
     264             :         }
     265             : 
     266           2 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     267             : 
     268           2 :  done:
     269           2 :         return status;
     270             : }
     271             : 
     272             : /* Resolve a list of names to a list of sids */
     273             : 
     274         102 : static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli, 
     275             :                                            TALLOC_CTX *mem_ctx, int argc, 
     276             :                                            const char **argv)
     277             : {
     278             :         struct policy_handle pol;
     279             :         NTSTATUS status, result;
     280         102 :         struct dom_sid *sids = NULL;
     281         102 :         enum lsa_SidType *types = NULL;
     282             :         int i, level;
     283         102 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     284             : 
     285         102 :         if (argc < 3) {
     286           0 :                 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
     287           0 :                 return NT_STATUS_OK;
     288             :         }
     289             : 
     290         102 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     291             :                                         LSA_POLICY_LOOKUP_NAMES,
     292             :                                         &pol);
     293         102 :         if (!NT_STATUS_IS_OK(status)) {
     294           0 :                 goto done;
     295             :         }
     296             : 
     297         102 :         level = atoi(argv[1]);
     298             : 
     299         102 :         status = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2,
     300             :                                       (const char**)(argv + 2), NULL, level, &sids, &types);
     301             : 
     302         102 :         if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
     303          22 :             NT_STATUS_V(STATUS_SOME_UNMAPPED))
     304             :         {
     305          22 :                 goto done;
     306             :         }
     307             : 
     308          80 :         status = NT_STATUS_OK;
     309             : 
     310             :         /* Print results */
     311             : 
     312         160 :         for (i = 0; i < (argc - 2); i++) {
     313             :                 struct dom_sid_buf sid_str;
     314         240 :                 printf("%s %s (%s: %d)\n",
     315          80 :                        argv[i + 2],
     316          80 :                        dom_sid_str_buf(&sids[i], &sid_str),
     317          80 :                        sid_type_lookup(types[i]),
     318          80 :                        types[i]);
     319             :         }
     320             : 
     321          80 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     322             : 
     323         102 :  done:
     324         102 :         return status;
     325             : }
     326             : 
     327           0 : static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
     328             :                                       TALLOC_CTX *mem_ctx, int argc,
     329             :                                       const char **argv)
     330             : {
     331             :         NTSTATUS status, result;
     332             : 
     333             :         uint32_t num_names;
     334             :         struct lsa_String *names;
     335           0 :         struct lsa_RefDomainList *domains = NULL;
     336             :         struct lsa_TransSidArray3 sids;
     337           0 :         uint32_t count = 0;
     338             :         int i;
     339           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     340             : 
     341           0 :         if (argc == 1) {
     342           0 :                 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
     343           0 :                 return NT_STATUS_OK;
     344             :         }
     345             : 
     346           0 :         ZERO_STRUCT(sids);
     347             : 
     348           0 :         num_names = argc-1;
     349           0 :         names = talloc_array(mem_ctx, struct lsa_String, num_names);
     350           0 :         NT_STATUS_HAVE_NO_MEMORY(names);
     351             : 
     352           0 :         for (i=0; i < num_names; i++) {
     353           0 :                 init_lsa_String(&names[i], argv[i+1]);
     354             :         }
     355             : 
     356           0 :         status = dcerpc_lsa_LookupNames4(b, mem_ctx,
     357             :                                          num_names,
     358             :                                          names,
     359             :                                          &domains,
     360             :                                          &sids,
     361             :                                          1,
     362             :                                          &count,
     363             :                                          0,
     364             :                                          0,
     365             :                                          &result);
     366           0 :         if (!NT_STATUS_IS_OK(status)) {
     367           0 :                 return status;
     368             :         }
     369           0 :         if (!NT_STATUS_IS_OK(result)) {
     370           0 :                 return result;
     371             :         }
     372             : 
     373           0 :         if (sids.count != num_names) {
     374           0 :                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
     375             :         }
     376             : 
     377           0 :         for (i = 0; i < sids.count; i++) {
     378             :                 struct dom_sid_buf sid_str;
     379           0 :                 printf("%s %s (%s: %d)\n",
     380           0 :                        argv[i+1],
     381           0 :                        dom_sid_str_buf(sids.sids[i].sid, &sid_str),
     382           0 :                        sid_type_lookup(sids.sids[i].sid_type),
     383           0 :                        sids.sids[i].sid_type);
     384             :         }
     385             : 
     386           0 :         return status;
     387             : }
     388             : 
     389             : /* Resolve a list of SIDs to a list of names */
     390             : 
     391           2 : static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
     392             :                                     int argc, const char **argv)
     393             : {
     394             :         struct policy_handle pol;
     395             :         NTSTATUS status, result;
     396             :         struct dom_sid *sids;
     397             :         char **domains;
     398             :         char **names;
     399             :         enum lsa_SidType *types;
     400             :         int i;
     401           2 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     402             : 
     403           2 :         if (argc == 1) {
     404           0 :                 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
     405           0 :                 return NT_STATUS_OK;
     406             :         }
     407             : 
     408           2 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     409             :                                         LSA_POLICY_LOOKUP_NAMES,
     410             :                                         &pol);
     411             : 
     412           2 :         if (!NT_STATUS_IS_OK(status))
     413           0 :                 goto done;
     414             : 
     415             :         /* Convert arguments to sids */
     416             : 
     417           2 :         sids = talloc_array(mem_ctx, struct dom_sid, argc - 1);
     418             : 
     419           2 :         if (!sids) {
     420           0 :                 printf("could not allocate memory for %d sids\n", argc - 1);
     421           0 :                 goto done;
     422             :         }
     423             : 
     424           4 :         for (i = 0; i < argc - 1; i++) 
     425           2 :                 if (!string_to_sid(&sids[i], argv[i + 1])) {
     426           0 :                         status = NT_STATUS_INVALID_SID;
     427           0 :                         goto done;
     428             :                 }
     429             : 
     430             :         /* Lookup the SIDs */
     431             : 
     432           2 :         status = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
     433             :                                      &domains, &names, &types);
     434             : 
     435           2 :         if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
     436           0 :             NT_STATUS_V(STATUS_SOME_UNMAPPED))
     437           0 :                 goto done;
     438             : 
     439           2 :         status = NT_STATUS_OK;
     440             : 
     441             :         /* Print results */
     442             : 
     443           4 :         for (i = 0; i < (argc - 1); i++) {
     444             :                 struct dom_sid_buf sid_str;
     445             : 
     446           2 :                 dom_sid_str_buf(&sids[i], &sid_str);
     447           2 :                 if (types[i] == SID_NAME_DOMAIN) {
     448           0 :                         printf("%s %s (%d)\n", sid_str.buf,
     449           0 :                                domains[i] ? domains[i] : "*unknown*",
     450           0 :                                types[i]);
     451             :                 } else {
     452           2 :                         printf("%s %s\\%s (%d)\n", sid_str.buf,
     453           2 :                                domains[i] ? domains[i] : "*unknown*",
     454           2 :                                names[i] ? names[i] : "*unknown*",
     455           2 :                                types[i]);
     456             :                 }
     457             :         }
     458             : 
     459           2 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     460             : 
     461           2 :  done:
     462           2 :         return status;
     463             : }
     464             : 
     465           2 : static NTSTATUS cmd_lsa_lookup_sids_level(struct rpc_pipe_client *cli,
     466             :                                           TALLOC_CTX *mem_ctx, int argc,
     467             :                                           const char **argv)
     468             : {
     469             :         struct policy_handle pol;
     470             :         NTSTATUS status, result;
     471           2 :         struct dom_sid *sids = NULL;
     472           2 :         char **domains = NULL;
     473           2 :         char **names = NULL;
     474           2 :         enum lsa_SidType *types = NULL;
     475             :         int i, level;
     476           2 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     477             : 
     478           2 :         if (argc < 3) {
     479           0 :                 printf("Usage: %s [level] [sid1 [sid2 [...]]]\n", argv[0]);
     480           0 :                 return NT_STATUS_OK;
     481             :         }
     482             : 
     483           2 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     484             :                                         LSA_POLICY_LOOKUP_NAMES,
     485             :                                         &pol);
     486           2 :         if (!NT_STATUS_IS_OK(status)) {
     487           0 :                 goto done;
     488             :         }
     489             : 
     490           2 :         level = atoi(argv[1]);
     491             : 
     492             :         /* Convert arguments to sids */
     493             : 
     494           2 :         sids = talloc_array(mem_ctx, struct dom_sid, argc - 2);
     495           2 :         if (sids == NULL) {
     496           0 :                 printf("could not allocate memory for %d sids\n", argc - 2);
     497           0 :                 goto done;
     498             :         }
     499             : 
     500           4 :         for (i = 0; i < argc - 2; i++) {
     501           2 :                 if (!string_to_sid(&sids[i], argv[i + 2])) {
     502           0 :                         status = NT_STATUS_INVALID_SID;
     503           0 :                         goto done;
     504             :                 }
     505             :         }
     506             : 
     507             :         /* Lookup the SIDs */
     508             : 
     509           2 :         status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
     510             :                                                 mem_ctx,
     511             :                                                 &pol,
     512             :                                                 argc - 2,
     513             :                                                 sids,
     514             :                                                 level,
     515             :                                                 &domains,
     516             :                                                 &names,
     517             :                                                 &types,
     518             :                                                 false,
     519             :                                                 &result);
     520           2 :         if (!NT_STATUS_IS_OK(status)) {
     521           0 :                 goto done;
     522             :         }
     523           2 :         status = result;
     524             : 
     525           2 :         if (!NT_STATUS_IS_OK(status) && NT_STATUS_V(status) !=
     526           0 :             NT_STATUS_V(STATUS_SOME_UNMAPPED))
     527             :         {
     528           0 :                 goto done;
     529             :         }
     530             : 
     531           2 :         status = NT_STATUS_OK;
     532             : 
     533             :         /* Print results */
     534             : 
     535           4 :         for (i = 0; i < (argc - 2); i++) {
     536             :                 struct dom_sid_buf sid_str;
     537             : 
     538           2 :                 dom_sid_str_buf(&sids[i], &sid_str);
     539           2 :                 if (types[i] == SID_NAME_DOMAIN) {
     540           0 :                         printf("%s %s (%d)\n", sid_str.buf,
     541           0 :                                domains[i] ? domains[i] : "*unknown*",
     542           0 :                                types[i]);
     543             :                 } else {
     544           2 :                         printf("%s %s\\%s (%d)\n", sid_str.buf,
     545           2 :                                domains[i] ? domains[i] : "*unknown*",
     546           2 :                                names[i] ? names[i] : "*unknown*",
     547           2 :                                types[i]);
     548             :                 }
     549             :         }
     550             : 
     551           2 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     552             : 
     553           2 :  done:
     554           2 :         return status;
     555             : }
     556             : 
     557             : /* Resolve a list of SIDs to a list of names */
     558             : 
     559           2 : static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
     560             :                                      TALLOC_CTX *mem_ctx,
     561             :                                      int argc, const char **argv)
     562             : {
     563           2 :         NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
     564             :         int i;
     565             :         struct lsa_SidArray sids;
     566           2 :         struct lsa_RefDomainList *domains = NULL;
     567             :         struct lsa_TransNameArray2 names;
     568           2 :         uint32_t count = 0;
     569           2 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     570             : 
     571           2 :         if (argc == 1) {
     572           0 :                 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
     573           0 :                 return NT_STATUS_OK;
     574             :         }
     575             : 
     576           2 :         ZERO_STRUCT(names);
     577             : 
     578             :         /* Convert arguments to sids */
     579             : 
     580           2 :         sids.num_sids = argc-1;
     581           2 :         sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
     582           2 :         if (!sids.sids) {
     583           0 :                 printf("could not allocate memory for %d sids\n", sids.num_sids);
     584           0 :                 goto done;
     585             :         }
     586             : 
     587           4 :         for (i = 0; i < sids.num_sids; i++) {
     588           2 :                 sids.sids[i].sid = talloc(sids.sids, struct dom_sid);
     589           2 :                 if (sids.sids[i].sid == NULL) {
     590           0 :                         status = NT_STATUS_NO_MEMORY;
     591           0 :                         goto done;
     592             :                 }
     593           2 :                 if (!string_to_sid(sids.sids[i].sid, argv[i+1])) {
     594           0 :                         status = NT_STATUS_INVALID_SID;
     595           0 :                         goto done;
     596             :                 }
     597             :         }
     598             : 
     599             :         /* Lookup the SIDs */
     600           2 :         status = dcerpc_lsa_LookupSids3(b, mem_ctx,
     601             :                                         &sids,
     602             :                                         &domains,
     603             :                                         &names,
     604             :                                         1,
     605             :                                         &count,
     606             :                                         0,
     607             :                                         0,
     608             :                                         &result);
     609           2 :         if (!NT_STATUS_IS_OK(status)) {
     610           0 :                 goto done;
     611             :         }
     612           2 :         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
     613           0 :             NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
     614           0 :                 status = result;
     615           0 :                 goto done;
     616             :         }
     617             : 
     618           2 :         status = NT_STATUS_OK;
     619             : 
     620             :         /* Print results */
     621             : 
     622           4 :         for (i = 0; i < names.count; i++) {
     623             :                 struct dom_sid_buf sid_str;
     624             : 
     625           2 :                 if (i >= sids.num_sids) {
     626           0 :                         break;
     627             :                 }
     628           2 :                 printf("%s %s (%d)\n",
     629           2 :                        dom_sid_str_buf(sids.sids[i].sid, &sid_str),
     630           2 :                        names.names[i].name.string,
     631           2 :                        names.names[i].sid_type);
     632             :         }
     633             : 
     634           2 :  done:
     635           2 :         return status;
     636             : }
     637             : 
     638             : 
     639             : /* Enumerate list of trusted domains */
     640             : 
     641           0 : static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli, 
     642             :                                        TALLOC_CTX *mem_ctx, int argc, 
     643             :                                        const char **argv)
     644             : {
     645             :         struct policy_handle pol;
     646             :         NTSTATUS status, result;
     647             :         struct lsa_DomainList domain_list;
     648           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     649             : 
     650             :         /* defaults, but may be changed using params */
     651           0 :         uint32_t enum_ctx = 0;
     652             :         int i;
     653           0 :         uint32_t max_size = (uint32_t)-1;
     654             : 
     655           0 :         if (argc > 2) {
     656           0 :                 printf("Usage: %s [enum context (0)]\n", argv[0]);
     657           0 :                 return NT_STATUS_OK;
     658             :         }
     659             : 
     660           0 :         if (argc == 2 && argv[1]) {
     661           0 :                 enum_ctx = atoi(argv[2]);
     662             :         }       
     663             : 
     664           0 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     665             :                                      LSA_POLICY_VIEW_LOCAL_INFORMATION,
     666             :                                      &pol);
     667             : 
     668           0 :         if (!NT_STATUS_IS_OK(status))
     669           0 :                 goto done;
     670             : 
     671           0 :         status = STATUS_MORE_ENTRIES;
     672             : 
     673           0 :         while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
     674             : 
     675             :                 /* Lookup list of trusted domains */
     676             : 
     677           0 :                 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
     678             :                                                  &pol,
     679             :                                                  &enum_ctx,
     680             :                                                  &domain_list,
     681             :                                                  max_size,
     682             :                                                  &result);
     683           0 :                 if (!NT_STATUS_IS_OK(status)) {
     684           0 :                         goto done;
     685             :                 }
     686           0 :                 if (!NT_STATUS_IS_OK(result) &&
     687           0 :                     !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
     688           0 :                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
     689           0 :                         status = result;
     690           0 :                         goto done;
     691             :                 }
     692             : 
     693             :                 /* Print results: list of names and sids returned in this
     694             :                  * response. */  
     695           0 :                 for (i = 0; i < domain_list.count; i++) {
     696             :                         struct dom_sid_buf sid_str;
     697             : 
     698           0 :                         printf("%s %s\n",
     699           0 :                                 domain_list.domains[i].name.string ?
     700           0 :                                 domain_list.domains[i].name.string : "*unknown*",
     701           0 :                                 dom_sid_str_buf(domain_list.domains[i].sid,
     702             :                                                 &sid_str));
     703             :                 }
     704             :         }
     705             : 
     706           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     707           0 :  done:
     708           0 :         return status;
     709             : }
     710             : 
     711             : /* Enumerates privileges */
     712             : 
     713           0 : static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli, 
     714             :                                        TALLOC_CTX *mem_ctx, int argc, 
     715             :                                        const char **argv) 
     716             : {
     717             :         struct policy_handle pol;
     718             :         NTSTATUS status, result;
     719             :         struct lsa_PrivArray priv_array;
     720           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     721             : 
     722           0 :         uint32_t enum_context=0;
     723           0 :         uint32_t pref_max_length=0x1000;
     724             :         int i;
     725             : 
     726           0 :         if (argc > 3) {
     727           0 :                 printf("Usage: %s [enum context] [max length]\n", argv[0]);
     728           0 :                 return NT_STATUS_OK;
     729             :         }
     730             : 
     731           0 :         if (argc>=2)
     732           0 :                 enum_context=atoi(argv[1]);
     733             : 
     734           0 :         if (argc==3)
     735           0 :                 pref_max_length=atoi(argv[2]);
     736             : 
     737           0 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     738             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
     739             :                                      &pol);
     740             : 
     741           0 :         if (!NT_STATUS_IS_OK(status))
     742           0 :                 goto done;
     743             : 
     744           0 :         status = dcerpc_lsa_EnumPrivs(b, mem_ctx,
     745             :                                       &pol,
     746             :                                       &enum_context,
     747             :                                       &priv_array,
     748             :                                       pref_max_length,
     749             :                                       &result);
     750           0 :         if (!NT_STATUS_IS_OK(status))
     751           0 :                 goto done;
     752           0 :         if (!NT_STATUS_IS_OK(result)) {
     753           0 :                 status = result;
     754           0 :                 goto done;
     755             :         }
     756             : 
     757             :         /* Print results */
     758           0 :         printf("found %d privileges\n\n", priv_array.count);
     759             : 
     760           0 :         for (i = 0; i < priv_array.count; i++) {
     761           0 :                 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
     762           0 :                        priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
     763           0 :                        priv_array.privs[i].luid.high,
     764           0 :                        priv_array.privs[i].luid.low,
     765           0 :                        priv_array.privs[i].luid.high,
     766           0 :                        priv_array.privs[i].luid.low);
     767             :         }
     768             : 
     769           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     770           0 :  done:
     771           0 :         return status;
     772             : }
     773             : 
     774             : /* Get privilege name */
     775             : 
     776           0 : static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli, 
     777             :                                      TALLOC_CTX *mem_ctx, int argc, 
     778             :                                      const char **argv) 
     779             : {
     780             :         struct policy_handle pol;
     781             :         NTSTATUS status, result;
     782           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     783             : 
     784           0 :         uint16_t lang_id=0;
     785           0 :         uint16_t lang_id_sys=0;
     786             :         uint16_t lang_id_desc;
     787             :         struct lsa_String lsa_name;
     788           0 :         struct lsa_StringLarge *description = NULL;
     789             : 
     790           0 :         if (argc != 2) {
     791           0 :                 printf("Usage: %s privilege name\n", argv[0]);
     792           0 :                 return NT_STATUS_OK;
     793             :         }
     794             : 
     795           0 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     796             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
     797             :                                      &pol);
     798             : 
     799           0 :         if (!NT_STATUS_IS_OK(status))
     800           0 :                 goto done;
     801             : 
     802           0 :         init_lsa_String(&lsa_name, argv[1]);
     803             : 
     804           0 :         status = dcerpc_lsa_LookupPrivDisplayName(b, mem_ctx,
     805             :                                                   &pol,
     806             :                                                   &lsa_name,
     807             :                                                   lang_id,
     808             :                                                   lang_id_sys,
     809             :                                                   &description,
     810             :                                                   &lang_id_desc,
     811             :                                                   &result);
     812           0 :         if (!NT_STATUS_IS_OK(status))
     813           0 :                 goto done;
     814           0 :         if (!NT_STATUS_IS_OK(result)) {
     815           0 :                 status = result;
     816           0 :                 goto done;
     817             :         }
     818             : 
     819             :         /* Print results */
     820           0 :         printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
     821             : 
     822           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     823           0 :  done:
     824           0 :         return status;
     825             : }
     826             : 
     827             : /* Enumerate the LSA SIDS */
     828             : 
     829           0 : static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli, 
     830             :                                   TALLOC_CTX *mem_ctx, int argc, 
     831             :                                   const char **argv) 
     832             : {
     833             :         struct policy_handle pol;
     834             :         NTSTATUS status, result;
     835           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     836             : 
     837           0 :         uint32_t enum_context=0;
     838           0 :         uint32_t pref_max_length=0x1000;
     839             :         struct lsa_SidArray sid_array;
     840             :         int i;
     841             : 
     842           0 :         if (argc > 3) {
     843           0 :                 printf("Usage: %s [enum context] [max length]\n", argv[0]);
     844           0 :                 return NT_STATUS_OK;
     845             :         }
     846             : 
     847           0 :         if (argc>=2)
     848           0 :                 enum_context=atoi(argv[1]);
     849             : 
     850           0 :         if (argc==3)
     851           0 :                 pref_max_length=atoi(argv[2]);
     852             : 
     853           0 :         status = rpccli_lsa_open_policy(cli, mem_ctx, True,
     854             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
     855             :                                      &pol);
     856             : 
     857           0 :         if (!NT_STATUS_IS_OK(status))
     858           0 :                 goto done;
     859             : 
     860           0 :         status = dcerpc_lsa_EnumAccounts(b, mem_ctx,
     861             :                                          &pol,
     862             :                                          &enum_context,
     863             :                                          &sid_array,
     864             :                                          pref_max_length,
     865             :                                          &result);
     866           0 :         if (!NT_STATUS_IS_OK(status))
     867           0 :                 goto done;
     868           0 :         if (!NT_STATUS_IS_OK(result)) {
     869           0 :                 status = result;
     870           0 :                 goto done;
     871             :         }
     872             : 
     873             :         /* Print results */
     874           0 :         printf("found %d SIDs\n\n", sid_array.num_sids);
     875             : 
     876           0 :         for (i = 0; i < sid_array.num_sids; i++) {
     877             :                 struct dom_sid_buf sid_str;
     878             : 
     879           0 :                 printf("%s\n",
     880           0 :                        dom_sid_str_buf(sid_array.sids[i].sid, &sid_str));
     881             :         }
     882             : 
     883           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
     884           0 :  done:
     885           0 :         return status;
     886             : }
     887             : 
     888             : /* Create a new account */
     889             : 
     890           0 : static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli, 
     891             :                                            TALLOC_CTX *mem_ctx, int argc, 
     892             :                                            const char **argv) 
     893             : {
     894             :         struct policy_handle dom_pol;
     895             :         struct policy_handle user_pol;
     896             :         NTSTATUS status, result;
     897           0 :         uint32_t des_access = 0x000f000f;
     898           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     899             : 
     900             :         struct dom_sid sid;
     901             : 
     902           0 :         if (argc != 2 ) {
     903           0 :                 printf("Usage: %s SID\n", argv[0]);
     904           0 :                 return NT_STATUS_OK;
     905             :         }
     906             : 
     907           0 :         status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
     908           0 :         if (!NT_STATUS_IS_OK(status))
     909           0 :                 goto done;      
     910             : 
     911           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
     912             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
     913             :                                      &dom_pol);
     914             : 
     915           0 :         if (!NT_STATUS_IS_OK(status))
     916           0 :                 goto done;
     917             : 
     918           0 :         status = dcerpc_lsa_CreateAccount(b, mem_ctx,
     919             :                                           &dom_pol,
     920             :                                           &sid,
     921             :                                           des_access,
     922             :                                           &user_pol,
     923             :                                           &result);
     924           0 :         if (!NT_STATUS_IS_OK(status))
     925           0 :                 goto done;
     926           0 :         if (!NT_STATUS_IS_OK(result)) {
     927           0 :                 status = result;
     928           0 :                 goto done;
     929             :         }
     930             : 
     931           0 :         printf("Account for SID %s successfully created\n\n", argv[1]);
     932           0 :         status = NT_STATUS_OK;
     933             : 
     934           0 :         dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
     935           0 :  done:
     936           0 :         return status;
     937             : }
     938             : 
     939             : 
     940             : /* Enumerate the privileges of an SID */
     941             : 
     942           0 : static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli, 
     943             :                                            TALLOC_CTX *mem_ctx, int argc, 
     944             :                                            const char **argv) 
     945             : {
     946             :         struct policy_handle dom_pol;
     947             :         struct policy_handle user_pol;
     948             :         NTSTATUS status, result;
     949           0 :         uint32_t access_desired = 0x000f000f;
     950             :         struct dom_sid sid;
     951           0 :         struct lsa_PrivilegeSet *privs = NULL;
     952             :         int i;
     953           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
     954             : 
     955           0 :         if (argc != 2 ) {
     956           0 :                 printf("Usage: %s SID\n", argv[0]);
     957           0 :                 return NT_STATUS_OK;
     958             :         }
     959             : 
     960           0 :         status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
     961           0 :         if (!NT_STATUS_IS_OK(status))
     962           0 :                 goto done;      
     963             : 
     964           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
     965             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
     966             :                                      &dom_pol);
     967             : 
     968           0 :         if (!NT_STATUS_IS_OK(status))
     969           0 :                 goto done;
     970             : 
     971           0 :         status = dcerpc_lsa_OpenAccount(b, mem_ctx,
     972             :                                         &dom_pol,
     973             :                                         &sid,
     974             :                                         access_desired,
     975             :                                         &user_pol,
     976             :                                         &result);
     977           0 :         if (!NT_STATUS_IS_OK(status))
     978           0 :                 goto done;
     979           0 :         if (!NT_STATUS_IS_OK(result)) {
     980           0 :                 status = result;
     981           0 :                 goto done;
     982             :         }
     983             : 
     984           0 :         status = dcerpc_lsa_EnumPrivsAccount(b, mem_ctx,
     985             :                                              &user_pol,
     986             :                                              &privs,
     987             :                                              &result);
     988           0 :         if (!NT_STATUS_IS_OK(status))
     989           0 :                 goto done;
     990           0 :         if (!NT_STATUS_IS_OK(result)) {
     991           0 :                 status = result;
     992           0 :                 goto done;
     993             :         }
     994             : 
     995             :         /* Print results */
     996           0 :         printf("found %d privileges for SID %s\n\n", privs->count, argv[1]);
     997           0 :         printf("high\tlow\tattribute\n");
     998             : 
     999           0 :         for (i = 0; i < privs->count; i++) {
    1000           0 :                 printf("%u\t%u\t%u\n",
    1001           0 :                         privs->set[i].luid.high,
    1002           0 :                         privs->set[i].luid.low,
    1003           0 :                         privs->set[i].attribute);
    1004             :         }
    1005             : 
    1006           0 :         dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
    1007           0 :  done:
    1008           0 :         return status;
    1009             : }
    1010             : 
    1011             : 
    1012             : /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
    1013             : 
    1014           0 : static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli, 
    1015             :                                          TALLOC_CTX *mem_ctx, int argc, 
    1016             :                                          const char **argv) 
    1017             : {
    1018             :         struct policy_handle dom_pol;
    1019             :         NTSTATUS status, result;
    1020             :         struct dom_sid sid;
    1021             :         struct dom_sid_buf buf;
    1022             :         struct lsa_RightSet rights;
    1023           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1024             : 
    1025             :         int i;
    1026             : 
    1027           0 :         if (argc != 2 ) {
    1028           0 :                 printf("Usage: %s SID\n", argv[0]);
    1029           0 :                 return NT_STATUS_OK;
    1030             :         }
    1031             : 
    1032           0 :         status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
    1033           0 :         if (!NT_STATUS_IS_OK(status))
    1034           0 :                 goto done;      
    1035             : 
    1036           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
    1037             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
    1038             :                                      &dom_pol);
    1039             : 
    1040           0 :         if (!NT_STATUS_IS_OK(status))
    1041           0 :                 goto done;
    1042             : 
    1043           0 :         status = dcerpc_lsa_EnumAccountRights(b, mem_ctx,
    1044             :                                               &dom_pol,
    1045             :                                               &sid,
    1046             :                                               &rights,
    1047             :                                               &result);
    1048           0 :         if (!NT_STATUS_IS_OK(status))
    1049           0 :                 goto done;
    1050           0 :         if (!NT_STATUS_IS_OK(result)) {
    1051           0 :                 status = result;
    1052           0 :                 goto done;
    1053             :         }
    1054             : 
    1055           0 :         printf("found %d privileges for SID %s\n", rights.count,
    1056             :                dom_sid_str_buf(&sid, &buf));
    1057             : 
    1058           0 :         for (i = 0; i < rights.count; i++) {
    1059           0 :                 printf("\t%s\n", rights.names[i].string);
    1060             :         }
    1061             : 
    1062           0 :         dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
    1063           0 :  done:
    1064           0 :         return status;
    1065             : }
    1066             : 
    1067             : 
    1068             : /* add some privileges to a SID via LsaAddAccountRights */
    1069             : 
    1070           0 : static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli, 
    1071             :                                         TALLOC_CTX *mem_ctx, int argc, 
    1072             :                                         const char **argv) 
    1073             : {
    1074             :         struct policy_handle dom_pol;
    1075             :         NTSTATUS status, result;
    1076             :         struct lsa_RightSet rights;
    1077             :         struct dom_sid sid;
    1078             :         int i;
    1079           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1080             : 
    1081           0 :         if (argc < 3 ) {
    1082           0 :                 printf("Usage: %s SID [rights...]\n", argv[0]);
    1083           0 :                 return NT_STATUS_OK;
    1084             :         }
    1085             : 
    1086           0 :         status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
    1087           0 :         if (!NT_STATUS_IS_OK(status))
    1088           0 :                 goto done;      
    1089             : 
    1090           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
    1091             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
    1092             :                                      &dom_pol);
    1093             : 
    1094           0 :         if (!NT_STATUS_IS_OK(status))
    1095           0 :                 goto done;
    1096             : 
    1097           0 :         rights.count = argc-2;
    1098           0 :         rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
    1099             :                                     rights.count);
    1100           0 :         if (!rights.names) {
    1101           0 :                 return NT_STATUS_NO_MEMORY;
    1102             :         }
    1103             : 
    1104           0 :         for (i=0; i<argc-2; i++) {
    1105           0 :                 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
    1106             :         }
    1107             : 
    1108           0 :         status = dcerpc_lsa_AddAccountRights(b, mem_ctx,
    1109             :                                              &dom_pol,
    1110             :                                              &sid,
    1111             :                                              &rights,
    1112             :                                              &result);
    1113           0 :         if (!NT_STATUS_IS_OK(status))
    1114           0 :                 goto done;
    1115           0 :         if (!NT_STATUS_IS_OK(result)) {
    1116           0 :                 status = result;
    1117           0 :                 goto done;
    1118             :         }
    1119             : 
    1120           0 :         dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
    1121           0 :  done:
    1122           0 :         return status;
    1123             : }
    1124             : 
    1125             : 
    1126             : /* remove some privileges to a SID via LsaRemoveAccountRights */
    1127             : 
    1128           0 : static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli, 
    1129             :                                         TALLOC_CTX *mem_ctx, int argc, 
    1130             :                                         const char **argv) 
    1131             : {
    1132             :         struct policy_handle dom_pol;
    1133             :         NTSTATUS status, result;
    1134             :         struct lsa_RightSet rights;
    1135             :         struct dom_sid sid;
    1136             :         int i;
    1137           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1138             : 
    1139           0 :         if (argc < 3 ) {
    1140           0 :                 printf("Usage: %s SID [rights...]\n", argv[0]);
    1141           0 :                 return NT_STATUS_OK;
    1142             :         }
    1143             : 
    1144           0 :         status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
    1145           0 :         if (!NT_STATUS_IS_OK(status))
    1146           0 :                 goto done;      
    1147             : 
    1148           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
    1149             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
    1150             :                                      &dom_pol);
    1151             : 
    1152           0 :         if (!NT_STATUS_IS_OK(status))
    1153           0 :                 goto done;
    1154             : 
    1155           0 :         rights.count = argc-2;
    1156           0 :         rights.names = talloc_array(mem_ctx, struct lsa_StringLarge,
    1157             :                                     rights.count);
    1158           0 :         if (!rights.names) {
    1159           0 :                 return NT_STATUS_NO_MEMORY;
    1160             :         }
    1161             : 
    1162           0 :         for (i=0; i<argc-2; i++) {
    1163           0 :                 init_lsa_StringLarge(&rights.names[i], argv[i+2]);
    1164             :         }
    1165             : 
    1166           0 :         status = dcerpc_lsa_RemoveAccountRights(b, mem_ctx,
    1167             :                                                 &dom_pol,
    1168             :                                                 &sid,
    1169             :                                                 false,
    1170             :                                                 &rights,
    1171             :                                                 &result);
    1172           0 :         if (!NT_STATUS_IS_OK(status))
    1173           0 :                 goto done;
    1174           0 :         if (!NT_STATUS_IS_OK(result)) {
    1175           0 :                 status = result;
    1176           0 :                 goto done;
    1177             :         }
    1178             : 
    1179           0 :         dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
    1180             : 
    1181           0 :  done:
    1182           0 :         return status;
    1183             : }
    1184             : 
    1185             : 
    1186             : /* Get a privilege value given its name */
    1187             : 
    1188           0 : static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli, 
    1189             :                                         TALLOC_CTX *mem_ctx, int argc, 
    1190             :                                         const char **argv) 
    1191             : {
    1192             :         struct policy_handle pol;
    1193             :         NTSTATUS status, result;
    1194             :         struct lsa_LUID luid;
    1195             :         struct lsa_String name;
    1196           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1197             : 
    1198           0 :         if (argc != 2 ) {
    1199           0 :                 printf("Usage: %s name\n", argv[0]);
    1200           0 :                 return NT_STATUS_OK;
    1201             :         }
    1202             : 
    1203           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
    1204             :                                      SEC_FLAG_MAXIMUM_ALLOWED,
    1205             :                                      &pol);
    1206             : 
    1207           0 :         if (!NT_STATUS_IS_OK(status))
    1208           0 :                 goto done;
    1209             : 
    1210           0 :         init_lsa_String(&name, argv[1]);
    1211             : 
    1212           0 :         status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
    1213             :                                             &pol,
    1214             :                                             &name,
    1215             :                                             &luid,
    1216             :                                             &result);
    1217           0 :         if (!NT_STATUS_IS_OK(status))
    1218           0 :                 goto done;
    1219           0 :         if (!NT_STATUS_IS_OK(result)) {
    1220           0 :                 status = result;
    1221           0 :                 goto done;
    1222             :         }
    1223             : 
    1224             :         /* Print results */
    1225             : 
    1226           0 :         printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
    1227             : 
    1228           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
    1229           0 :  done:
    1230           0 :         return status;
    1231             : }
    1232             : 
    1233             : /* Query LSA security object */
    1234             : 
    1235           0 : static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli, 
    1236             :                                      TALLOC_CTX *mem_ctx, int argc, 
    1237             :                                      const char **argv) 
    1238             : {
    1239             :         struct policy_handle pol;
    1240             :         NTSTATUS status, result;
    1241             :         struct sec_desc_buf *sdb;
    1242           0 :         uint32_t sec_info = SECINFO_DACL;
    1243           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1244             : 
    1245           0 :         if (argc < 1 || argc > 2) {
    1246           0 :                 printf("Usage: %s [sec_info]\n", argv[0]);
    1247           0 :                 return NT_STATUS_OK;
    1248             :         }
    1249             : 
    1250           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
    1251             :                                       SEC_FLAG_MAXIMUM_ALLOWED,
    1252             :                                       &pol);
    1253             : 
    1254           0 :         if (argc == 2) 
    1255           0 :                 sscanf(argv[1], "%x", &sec_info);
    1256             : 
    1257           0 :         if (!NT_STATUS_IS_OK(status))
    1258           0 :                 goto done;
    1259             : 
    1260           0 :         status = dcerpc_lsa_QuerySecurity(b, mem_ctx,
    1261             :                                           &pol,
    1262             :                                           sec_info,
    1263             :                                           &sdb,
    1264             :                                           &result);
    1265           0 :         if (!NT_STATUS_IS_OK(status))
    1266           0 :                 goto done;
    1267           0 :         if (!NT_STATUS_IS_OK(result)) {
    1268           0 :                 status = result;
    1269           0 :                 goto done;
    1270             :         }
    1271             : 
    1272             :         /* Print results */
    1273             : 
    1274           0 :         display_sec_desc(sdb->sd);
    1275             : 
    1276           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
    1277           0 :  done:
    1278           0 :         return status;
    1279             : }
    1280             : 
    1281           0 : static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p,
    1282             :                                      DATA_BLOB session_key)
    1283             : {
    1284             :         char *pwd, *pwd_old;
    1285             : 
    1286           0 :         DATA_BLOB data     = data_blob_const(p->password->data, p->password->length);
    1287           0 :         DATA_BLOB data_old = data_blob_const(p->old_password->data, p->old_password->length);
    1288             : 
    1289           0 :         pwd     = sess_decrypt_string(talloc_tos(), &data, &session_key);
    1290           0 :         pwd_old = sess_decrypt_string(talloc_tos(), &data_old, &session_key);
    1291             : 
    1292           0 :         d_printf("Password:\t%s\n", pwd);
    1293           0 :         d_printf("Old Password:\t%s\n", pwd_old);
    1294             : 
    1295           0 :         talloc_free(pwd);
    1296           0 :         talloc_free(pwd_old);
    1297           0 : }
    1298             : 
    1299           0 : 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)
    1303             : {
    1304           0 :         switch (info_class) {
    1305           0 :                 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
    1306           0 :                         display_trust_dom_info_4(&info->password, session_key);
    1307           0 :                         break;
    1308           0 :                 default: {
    1309           0 :                         const char *str = NULL;
    1310           0 :                         str = NDR_PRINT_UNION_STRING(mem_ctx,
    1311             :                                                      lsa_TrustedDomainInfo,
    1312             :                                                      info_class, info);
    1313           0 :                         if (str) {
    1314           0 :                                 d_printf("%s\n", str);
    1315             :                         }
    1316           0 :                         break;
    1317             :                 }
    1318             :         }
    1319           0 : }
    1320             : 
    1321           0 : static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
    1322             :                                                 TALLOC_CTX *mem_ctx, int argc, 
    1323             :                                                 const char **argv) 
    1324             : {
    1325             :         struct policy_handle pol;
    1326             :         NTSTATUS status, result;
    1327             :         struct dom_sid dom_sid;
    1328           0 :         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
    1329           0 :         union lsa_TrustedDomainInfo *info = NULL;
    1330           0 :         enum lsa_TrustDomInfoEnum info_class = 1;
    1331             :         DATA_BLOB session_key;
    1332           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1333             : 
    1334           0 :         if (argc > 3 || argc < 2) {
    1335           0 :                 printf("Usage: %s [sid] [info_class]\n", argv[0]);
    1336           0 :                 return NT_STATUS_OK;
    1337             :         }
    1338             : 
    1339           0 :         if (!string_to_sid(&dom_sid, argv[1]))
    1340           0 :                 return NT_STATUS_NO_MEMORY;
    1341             : 
    1342           0 :         if (argc == 3)
    1343           0 :                 info_class = atoi(argv[2]);
    1344             : 
    1345           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
    1346             : 
    1347           0 :         if (!NT_STATUS_IS_OK(status))
    1348           0 :                 goto done;
    1349             : 
    1350           0 :         status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
    1351             :                                                         &pol,
    1352             :                                                         &dom_sid,
    1353             :                                                         info_class,
    1354             :                                                         &info,
    1355             :                                                         &result);
    1356           0 :         if (!NT_STATUS_IS_OK(status))
    1357           0 :                 goto done;
    1358           0 :         if (!NT_STATUS_IS_OK(result)) {
    1359           0 :                 status = result;
    1360           0 :                 goto done;
    1361             :         }
    1362             : 
    1363           0 :         status = cli_get_session_key(mem_ctx, cli, &session_key);
    1364           0 :         if (!NT_STATUS_IS_OK(status)) {
    1365           0 :                 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
    1366           0 :                 goto done;
    1367             :         }
    1368             : 
    1369           0 :         display_trust_dom_info(mem_ctx, info, info_class, session_key);
    1370             : 
    1371           0 :  done:
    1372           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
    1373             : 
    1374           0 :         return status;
    1375             : }
    1376             : 
    1377           0 : static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
    1378             :                                                  TALLOC_CTX *mem_ctx, int argc,
    1379             :                                                  const char **argv) 
    1380             : {
    1381             :         struct policy_handle pol;
    1382             :         NTSTATUS status, result;
    1383           0 :         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
    1384           0 :         union lsa_TrustedDomainInfo *info = NULL;
    1385           0 :         enum lsa_TrustDomInfoEnum info_class = 1;
    1386             :         struct lsa_String trusted_domain;
    1387           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1388             :         DATA_BLOB session_key;
    1389             : 
    1390           0 :         if (argc > 3 || argc < 2) {
    1391           0 :                 printf("Usage: %s [name] [info_class]\n", argv[0]);
    1392           0 :                 return NT_STATUS_OK;
    1393             :         }
    1394             : 
    1395           0 :         if (argc == 3)
    1396           0 :                 info_class = atoi(argv[2]);
    1397             : 
    1398           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
    1399             : 
    1400           0 :         if (!NT_STATUS_IS_OK(status))
    1401           0 :                 goto done;
    1402             : 
    1403           0 :         init_lsa_String(&trusted_domain, argv[1]);
    1404             : 
    1405           0 :         status = dcerpc_lsa_QueryTrustedDomainInfoByName(b, mem_ctx,
    1406             :                                                          &pol,
    1407             :                                                          &trusted_domain,
    1408             :                                                          info_class,
    1409             :                                                          &info,
    1410             :                                                          &result);
    1411           0 :         if (!NT_STATUS_IS_OK(status))
    1412           0 :                 goto done;
    1413           0 :         if (!NT_STATUS_IS_OK(result)) {
    1414           0 :                 status = result;
    1415           0 :                 goto done;
    1416             :         }
    1417             : 
    1418           0 :         status = cli_get_session_key(mem_ctx, cli, &session_key);
    1419           0 :         if (!NT_STATUS_IS_OK(status)) {
    1420           0 :                 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
    1421           0 :                 goto done;
    1422             :         }
    1423             : 
    1424           0 :         display_trust_dom_info(mem_ctx, info, info_class, session_key);
    1425             : 
    1426           0 :  done:
    1427           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
    1428             : 
    1429           0 :         return status;
    1430             : }
    1431             : 
    1432           0 : static NTSTATUS cmd_lsa_set_trustdominfo(struct rpc_pipe_client *cli,
    1433             :                                          TALLOC_CTX *mem_ctx, int argc,
    1434             :                                          const char **argv)
    1435             : {
    1436             :         struct policy_handle pol, trustdom_pol;
    1437             :         NTSTATUS status, result;
    1438           0 :         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
    1439             :         union lsa_TrustedDomainInfo info;
    1440             :         struct dom_sid dom_sid;
    1441           0 :         enum lsa_TrustDomInfoEnum info_class = 1;
    1442           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1443             : 
    1444           0 :         if (argc > 4 || argc < 3) {
    1445           0 :                 printf("Usage: %s [sid] [info_class] [value]\n", argv[0]);
    1446           0 :                 return NT_STATUS_OK;
    1447             :         }
    1448             : 
    1449           0 :         if (!string_to_sid(&dom_sid, argv[1])) {
    1450           0 :                 return NT_STATUS_NO_MEMORY;
    1451             :         }
    1452             : 
    1453             : 
    1454           0 :         info_class = atoi(argv[2]);
    1455             : 
    1456           0 :         switch (info_class) {
    1457           0 :         case 13: /* LSA_TRUSTED_DOMAIN_SUPPORTED_ENCRYPTION_TYPES */
    1458           0 :                 info.enc_types.enc_types = atoi(argv[3]);
    1459           0 :                 break;
    1460           0 :         default:
    1461           0 :                 return NT_STATUS_INVALID_PARAMETER;
    1462             :         }
    1463             : 
    1464           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
    1465           0 :         if (!NT_STATUS_IS_OK(status)) {
    1466           0 :                 goto done;
    1467             :         }
    1468             : 
    1469           0 :         status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
    1470             :                                               &pol,
    1471             :                                               &dom_sid,
    1472             :                                               access_mask,
    1473             :                                               &trustdom_pol,
    1474             :                                               &result);
    1475           0 :         if (!NT_STATUS_IS_OK(status)) {
    1476           0 :                 goto done;
    1477             :         }
    1478           0 :         if (!NT_STATUS_IS_OK(result)) {
    1479           0 :                 status = result;
    1480           0 :                 goto done;
    1481             :         }
    1482             : 
    1483           0 :         status = dcerpc_lsa_SetInformationTrustedDomain(b, mem_ctx,
    1484             :                                                         &trustdom_pol,
    1485             :                                                         info_class,
    1486             :                                                         &info,
    1487             :                                                         &result);
    1488           0 :         if (!NT_STATUS_IS_OK(status)) {
    1489           0 :                 goto done;
    1490             :         }
    1491           0 :         if (!NT_STATUS_IS_OK(result)) {
    1492           0 :                 status = result;
    1493           0 :                 goto done;
    1494             :         }
    1495           0 :  done:
    1496           0 :         dcerpc_lsa_Close(b, mem_ctx, &trustdom_pol, &result);
    1497           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
    1498             : 
    1499           0 :         return status;
    1500             : }
    1501             : 
    1502           0 : static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
    1503             :                                            TALLOC_CTX *mem_ctx, int argc,
    1504             :                                            const char **argv) 
    1505             : {
    1506             :         struct policy_handle pol, trustdom_pol;
    1507             :         NTSTATUS status, result;
    1508           0 :         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
    1509           0 :         union lsa_TrustedDomainInfo *info = NULL;
    1510             :         struct dom_sid dom_sid;
    1511           0 :         enum lsa_TrustDomInfoEnum info_class = 1;
    1512             :         DATA_BLOB session_key;
    1513           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1514             : 
    1515           0 :         if (argc > 3 || argc < 2) {
    1516           0 :                 printf("Usage: %s [sid] [info_class]\n", argv[0]);
    1517           0 :                 return NT_STATUS_OK;
    1518             :         }
    1519             : 
    1520           0 :         if (!string_to_sid(&dom_sid, argv[1]))
    1521           0 :                 return NT_STATUS_NO_MEMORY;
    1522             : 
    1523             : 
    1524           0 :         if (argc == 3)
    1525           0 :                 info_class = atoi(argv[2]);
    1526             : 
    1527           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
    1528             : 
    1529           0 :         if (!NT_STATUS_IS_OK(status))
    1530           0 :                 goto done;
    1531             : 
    1532           0 :         status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
    1533             :                                               &pol,
    1534             :                                               &dom_sid,
    1535             :                                               access_mask,
    1536             :                                               &trustdom_pol,
    1537             :                                               &result);
    1538           0 :         if (!NT_STATUS_IS_OK(status))
    1539           0 :                 goto done;
    1540           0 :         if (!NT_STATUS_IS_OK(result)) {
    1541           0 :                 status = result;
    1542           0 :                 goto done;
    1543             :         }
    1544             : 
    1545           0 :         status = dcerpc_lsa_QueryTrustedDomainInfo(b, mem_ctx,
    1546             :                                                    &trustdom_pol,
    1547             :                                                    info_class,
    1548             :                                                    &info,
    1549             :                                                    &result);
    1550           0 :         if (!NT_STATUS_IS_OK(status))
    1551           0 :                 goto done;
    1552           0 :         if (!NT_STATUS_IS_OK(result)) {
    1553           0 :                 status = result;
    1554           0 :                 goto done;
    1555             :         }
    1556             : 
    1557           0 :         status = cli_get_session_key(mem_ctx, cli, &session_key);
    1558           0 :         if (!NT_STATUS_IS_OK(status)) {
    1559           0 :                 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(status)));
    1560           0 :                 goto done;
    1561             :         }
    1562             : 
    1563           0 :         display_trust_dom_info(mem_ctx, info, info_class, session_key);
    1564             : 
    1565           0 :  done:
    1566           0 :         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
    1567             : 
    1568           0 :         return status;
    1569             : }
    1570             : 
    1571         223 : static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
    1572             :                                      TALLOC_CTX *mem_ctx, int argc,
    1573             :                                      const char **argv)
    1574             : {
    1575             :         NTSTATUS status, result;
    1576         223 :         const char *servername = cli->desthost;
    1577         223 :         struct lsa_String *account_name = NULL;
    1578         223 :         struct lsa_String *authority_name = NULL;
    1579         223 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1580             : 
    1581         223 :         if (argc > 2) {
    1582           0 :                 printf("Usage: %s servername\n", argv[0]);
    1583           0 :                 return NT_STATUS_OK;
    1584             :         }
    1585             : 
    1586         223 :         status = dcerpc_lsa_GetUserName(b, mem_ctx,
    1587             :                                         servername,
    1588             :                                         &account_name,
    1589             :                                         &authority_name,
    1590             :                                         &result);
    1591         223 :         if (!NT_STATUS_IS_OK(status)) {
    1592          30 :                 goto done;
    1593             :         }
    1594         193 :         if (!NT_STATUS_IS_OK(result)) {
    1595           0 :                 status = result;
    1596           0 :                 goto done;
    1597             :         }
    1598             : 
    1599             :         /* Print results */
    1600             : 
    1601           0 :         printf("Account Name: %s, Authority Name: %s\n",
    1602         193 :                 account_name->string, authority_name ? authority_name->string :
    1603             :                 "");
    1604             : 
    1605         223 :  done:
    1606         223 :         return status;
    1607             : }
    1608             : 
    1609           0 : static NTSTATUS cmd_lsa_add_priv(struct rpc_pipe_client *cli,
    1610             :                                  TALLOC_CTX *mem_ctx, int argc,
    1611             :                                  const char **argv)
    1612             : {
    1613             :         struct policy_handle dom_pol, user_pol;
    1614             :         NTSTATUS status, result;
    1615             :         struct lsa_PrivilegeSet privs;
    1616           0 :         struct lsa_LUIDAttribute *set = NULL;
    1617             :         struct dom_sid sid;
    1618             :         int i;
    1619           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1620             : 
    1621           0 :         ZERO_STRUCT(privs);
    1622             : 
    1623           0 :         if (argc < 3 ) {
    1624           0 :                 printf("Usage: %s SID [rights...]\n", argv[0]);
    1625           0 :                 return NT_STATUS_OK;
    1626             :         }
    1627             : 
    1628           0 :         status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
    1629           0 :         if (!NT_STATUS_IS_OK(status)) {
    1630           0 :                 goto done;
    1631             :         }
    1632             : 
    1633           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
    1634             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    1635             :                                          &dom_pol);
    1636             : 
    1637           0 :         if (!NT_STATUS_IS_OK(status)) {
    1638           0 :                 goto done;
    1639             :         }
    1640             : 
    1641           0 :         status = dcerpc_lsa_OpenAccount(b, mem_ctx,
    1642             :                                         &dom_pol,
    1643             :                                         &sid,
    1644             :                                         SEC_FLAG_MAXIMUM_ALLOWED,
    1645             :                                         &user_pol,
    1646             :                                         &result);
    1647           0 :         if (!NT_STATUS_IS_OK(status)) {
    1648           0 :                 goto done;
    1649             :         }
    1650           0 :         if (!NT_STATUS_IS_OK(result)) {
    1651           0 :                 status = result;
    1652           0 :                 goto done;
    1653             :         }
    1654             : 
    1655           0 :         for (i=2; i<argc; i++) {
    1656             : 
    1657             :                 struct lsa_String priv_name;
    1658             :                 struct lsa_LUID luid;
    1659             : 
    1660           0 :                 init_lsa_String(&priv_name, argv[i]);
    1661             : 
    1662           0 :                 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
    1663             :                                                     &dom_pol,
    1664             :                                                     &priv_name,
    1665             :                                                     &luid,
    1666             :                                                     &result);
    1667           0 :                 if (!NT_STATUS_IS_OK(status)) {
    1668           0 :                         continue;
    1669             :                 }
    1670           0 :                 if (!NT_STATUS_IS_OK(result)) {
    1671           0 :                         status = result;
    1672           0 :                         continue;
    1673             :                 }
    1674             : 
    1675           0 :                 privs.count++;
    1676           0 :                 set = talloc_realloc(mem_ctx, set,
    1677             :                                            struct lsa_LUIDAttribute,
    1678             :                                            privs.count);
    1679           0 :                 if (!set) {
    1680           0 :                         return NT_STATUS_NO_MEMORY;
    1681             :                 }
    1682             : 
    1683           0 :                 set[privs.count-1].luid = luid;
    1684           0 :                 set[privs.count-1].attribute = 0;
    1685             :         }
    1686             : 
    1687           0 :         privs.set = set;
    1688             : 
    1689           0 :         status = dcerpc_lsa_AddPrivilegesToAccount(b, mem_ctx,
    1690             :                                                    &user_pol,
    1691             :                                                    &privs,
    1692             :                                                    &result);
    1693           0 :         if (!NT_STATUS_IS_OK(status)) {
    1694           0 :                 goto done;
    1695             :         }
    1696           0 :         if (!NT_STATUS_IS_OK(result)) {
    1697           0 :                 status = result;
    1698           0 :                 goto done;
    1699             :         }
    1700             : 
    1701           0 :         dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
    1702           0 :         dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
    1703           0 :  done:
    1704           0 :         return status;
    1705             : }
    1706             : 
    1707           0 : static NTSTATUS cmd_lsa_del_priv(struct rpc_pipe_client *cli,
    1708             :                                  TALLOC_CTX *mem_ctx, int argc,
    1709             :                                  const char **argv)
    1710             : {
    1711             :         struct policy_handle dom_pol, user_pol;
    1712             :         NTSTATUS status, result;
    1713             :         struct lsa_PrivilegeSet privs;
    1714           0 :         struct lsa_LUIDAttribute *set = NULL;
    1715             :         struct dom_sid sid;
    1716             :         int i;
    1717           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1718             : 
    1719           0 :         ZERO_STRUCT(privs);
    1720             : 
    1721           0 :         if (argc < 3 ) {
    1722           0 :                 printf("Usage: %s SID [rights...]\n", argv[0]);
    1723           0 :                 return NT_STATUS_OK;
    1724             :         }
    1725             : 
    1726           0 :         status = name_to_sid(cli, mem_ctx, &sid, argv[1]);
    1727           0 :         if (!NT_STATUS_IS_OK(status)) {
    1728           0 :                 goto done;
    1729             :         }
    1730             : 
    1731           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
    1732             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    1733             :                                          &dom_pol);
    1734             : 
    1735           0 :         if (!NT_STATUS_IS_OK(status)) {
    1736           0 :                 goto done;
    1737             :         }
    1738             : 
    1739           0 :         status = dcerpc_lsa_OpenAccount(b, mem_ctx,
    1740             :                                         &dom_pol,
    1741             :                                         &sid,
    1742             :                                         SEC_FLAG_MAXIMUM_ALLOWED,
    1743             :                                         &user_pol,
    1744             :                                         &result);
    1745           0 :         if (!NT_STATUS_IS_OK(status)) {
    1746           0 :                 goto done;
    1747             :         }
    1748           0 :         if (!NT_STATUS_IS_OK(result)) {
    1749           0 :                 status = result;
    1750           0 :                 goto done;
    1751             :         }
    1752             : 
    1753           0 :         for (i=2; i<argc; i++) {
    1754             : 
    1755             :                 struct lsa_String priv_name;
    1756             :                 struct lsa_LUID luid;
    1757             : 
    1758           0 :                 init_lsa_String(&priv_name, argv[i]);
    1759             : 
    1760           0 :                 status = dcerpc_lsa_LookupPrivValue(b, mem_ctx,
    1761             :                                                     &dom_pol,
    1762             :                                                     &priv_name,
    1763             :                                                     &luid,
    1764             :                                                     &result);
    1765           0 :                 if (!NT_STATUS_IS_OK(status)) {
    1766           0 :                         continue;
    1767             :                 }
    1768           0 :                 if (!NT_STATUS_IS_OK(result)) {
    1769           0 :                         status = result;
    1770           0 :                         continue;
    1771             :                 }
    1772             : 
    1773           0 :                 privs.count++;
    1774           0 :                 set = talloc_realloc(mem_ctx, set,
    1775             :                                            struct lsa_LUIDAttribute,
    1776             :                                            privs.count);
    1777           0 :                 if (!set) {
    1778           0 :                         return NT_STATUS_NO_MEMORY;
    1779             :                 }
    1780             : 
    1781           0 :                 set[privs.count-1].luid = luid;
    1782           0 :                 set[privs.count-1].attribute = 0;
    1783             :         }
    1784             : 
    1785           0 :         privs.set = set;
    1786             : 
    1787             : 
    1788           0 :         status = dcerpc_lsa_RemovePrivilegesFromAccount(b, mem_ctx,
    1789             :                                                         &user_pol,
    1790             :                                                         false,
    1791             :                                                         &privs,
    1792             :                                                         &result);
    1793           0 :         if (!NT_STATUS_IS_OK(status)) {
    1794           0 :                 goto done;
    1795             :         }
    1796           0 :         if (!NT_STATUS_IS_OK(result)) {
    1797           0 :                 status = result;
    1798           0 :                 goto done;
    1799             :         }
    1800             : 
    1801           0 :         dcerpc_lsa_Close(b, mem_ctx, &user_pol, &result);
    1802           0 :         dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
    1803           0 :  done:
    1804           0 :         return status;
    1805             : }
    1806             : 
    1807           0 : static NTSTATUS cmd_lsa_create_secret(struct rpc_pipe_client *cli,
    1808             :                                       TALLOC_CTX *mem_ctx, int argc,
    1809             :                                       const char **argv)
    1810             : {
    1811             :         NTSTATUS status, result;
    1812             :         struct policy_handle handle, sec_handle;
    1813             :         struct lsa_String name;
    1814           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1815             : 
    1816           0 :         if (argc < 2) {
    1817           0 :                 printf("Usage: %s name\n", argv[0]);
    1818           0 :                 return NT_STATUS_OK;
    1819             :         }
    1820             : 
    1821           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx,
    1822             :                                          true,
    1823             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    1824             :                                          &handle);
    1825           0 :         if (!NT_STATUS_IS_OK(status)) {
    1826           0 :                 return status;
    1827             :         }
    1828             : 
    1829           0 :         init_lsa_String(&name, argv[1]);
    1830             : 
    1831           0 :         status = dcerpc_lsa_CreateSecret(b, mem_ctx,
    1832             :                                          &handle,
    1833             :                                          name,
    1834             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    1835             :                                          &sec_handle,
    1836             :                                          &result);
    1837           0 :         if (!NT_STATUS_IS_OK(status)) {
    1838           0 :                 goto done;
    1839             :         }
    1840           0 :         if (!NT_STATUS_IS_OK(result)) {
    1841           0 :                 status = result;
    1842           0 :                 goto done;
    1843             :         }
    1844             : 
    1845           0 :  done:
    1846           0 :         if (is_valid_policy_hnd(&sec_handle)) {
    1847           0 :                 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
    1848             :         }
    1849           0 :         if (is_valid_policy_hnd(&handle)) {
    1850           0 :                 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
    1851             :         }
    1852             : 
    1853           0 :         return status;
    1854             : }
    1855             : 
    1856           0 : static NTSTATUS cmd_lsa_delete_secret(struct rpc_pipe_client *cli,
    1857             :                                       TALLOC_CTX *mem_ctx, int argc,
    1858             :                                       const char **argv)
    1859             : {
    1860             :         NTSTATUS status, result;
    1861             :         struct policy_handle handle, sec_handle;
    1862             :         struct lsa_String name;
    1863           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1864             : 
    1865           0 :         if (argc < 2) {
    1866           0 :                 printf("Usage: %s name\n", argv[0]);
    1867           0 :                 return NT_STATUS_OK;
    1868             :         }
    1869             : 
    1870           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx,
    1871             :                                          true,
    1872             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    1873             :                                          &handle);
    1874           0 :         if (!NT_STATUS_IS_OK(status)) {
    1875           0 :                 return status;
    1876             :         }
    1877             : 
    1878           0 :         init_lsa_String(&name, argv[1]);
    1879             : 
    1880           0 :         status = dcerpc_lsa_OpenSecret(b, mem_ctx,
    1881             :                                        &handle,
    1882             :                                        name,
    1883             :                                        SEC_FLAG_MAXIMUM_ALLOWED,
    1884             :                                        &sec_handle,
    1885             :                                        &result);
    1886           0 :         if (!NT_STATUS_IS_OK(status)) {
    1887           0 :                 goto done;
    1888             :         }
    1889           0 :         if (!NT_STATUS_IS_OK(result)) {
    1890           0 :                 status = result;
    1891           0 :                 goto done;
    1892             :         }
    1893             : 
    1894           0 :         status = dcerpc_lsa_DeleteObject(b, mem_ctx,
    1895             :                                          &sec_handle,
    1896             :                                          &result);
    1897           0 :         if (!NT_STATUS_IS_OK(status)) {
    1898           0 :                 goto done;
    1899             :         }
    1900           0 :         if (!NT_STATUS_IS_OK(result)) {
    1901           0 :                 status = result;
    1902           0 :                 goto done;
    1903             :         }
    1904             : 
    1905           0 :  done:
    1906           0 :         if (is_valid_policy_hnd(&sec_handle)) {
    1907           0 :                 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
    1908             :         }
    1909           0 :         if (is_valid_policy_hnd(&handle)) {
    1910           0 :                 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
    1911             :         }
    1912             : 
    1913           0 :         return status;
    1914             : }
    1915             : 
    1916           0 : static NTSTATUS cmd_lsa_query_secret(struct rpc_pipe_client *cli,
    1917             :                                      TALLOC_CTX *mem_ctx, int argc,
    1918             :                                      const char **argv)
    1919             : {
    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           0 :         NTTIME new_mtime = 0;
    1925             :         struct lsa_DATA_BUF_PTR old_val;
    1926           0 :         NTTIME old_mtime = 0;
    1927             :         DATA_BLOB session_key;
    1928           0 :         DATA_BLOB new_blob = data_blob_null;
    1929           0 :         DATA_BLOB old_blob = data_blob_null;
    1930             :         char *new_secret, *old_secret;
    1931           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    1932             : 
    1933           0 :         if (argc < 2) {
    1934           0 :                 printf("Usage: %s name\n", argv[0]);
    1935           0 :                 return NT_STATUS_OK;
    1936             :         }
    1937             : 
    1938           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx,
    1939             :                                          true,
    1940             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    1941             :                                          &handle);
    1942           0 :         if (!NT_STATUS_IS_OK(status)) {
    1943           0 :                 return status;
    1944             :         }
    1945             : 
    1946           0 :         init_lsa_String(&name, argv[1]);
    1947             : 
    1948           0 :         status = dcerpc_lsa_OpenSecret(b, mem_ctx,
    1949             :                                        &handle,
    1950             :                                        name,
    1951             :                                        SEC_FLAG_MAXIMUM_ALLOWED,
    1952             :                                        &sec_handle,
    1953             :                                        &result);
    1954           0 :         if (!NT_STATUS_IS_OK(status)) {
    1955           0 :                 goto done;
    1956             :         }
    1957           0 :         if (!NT_STATUS_IS_OK(result)) {
    1958           0 :                 status = result;
    1959           0 :                 goto done;
    1960             :         }
    1961             : 
    1962           0 :         ZERO_STRUCT(new_val);
    1963           0 :         ZERO_STRUCT(old_val);
    1964             : 
    1965           0 :         status = dcerpc_lsa_QuerySecret(b, mem_ctx,
    1966             :                                         &sec_handle,
    1967             :                                         &new_val,
    1968             :                                         &new_mtime,
    1969             :                                         &old_val,
    1970             :                                         &old_mtime,
    1971             :                                         &result);
    1972           0 :         if (!NT_STATUS_IS_OK(status)) {
    1973           0 :                 goto done;
    1974             :         }
    1975           0 :         if (!NT_STATUS_IS_OK(result)) {
    1976           0 :                 status = result;
    1977           0 :                 goto done;
    1978             :         }
    1979             : 
    1980           0 :         status = cli_get_session_key(mem_ctx, cli, &session_key);
    1981           0 :         if (!NT_STATUS_IS_OK(status)) {
    1982           0 :                 goto done;
    1983             :         }
    1984             : 
    1985           0 :         if (new_val.buf) {
    1986           0 :                 new_blob = data_blob_const(new_val.buf->data, new_val.buf->length);
    1987             :         }
    1988           0 :         if (old_val.buf) {
    1989           0 :                 old_blob = data_blob_const(old_val.buf->data, old_val.buf->length);
    1990             :         }
    1991             : 
    1992           0 :         new_secret = sess_decrypt_string(mem_ctx, &new_blob, &session_key);
    1993           0 :         old_secret = sess_decrypt_string(mem_ctx, &old_blob, &session_key);
    1994           0 :         if (new_secret) {
    1995           0 :                 d_printf("new secret: %s\n", new_secret);
    1996             :         }
    1997           0 :         if (old_secret) {
    1998           0 :                 d_printf("old secret: %s\n", old_secret);
    1999             :         }
    2000             : 
    2001           0 :  done:
    2002           0 :         if (is_valid_policy_hnd(&sec_handle)) {
    2003           0 :                 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
    2004             :         }
    2005           0 :         if (is_valid_policy_hnd(&handle)) {
    2006           0 :                 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
    2007             :         }
    2008             : 
    2009           0 :         return status;
    2010             : }
    2011             : 
    2012           0 : static NTSTATUS cmd_lsa_set_secret(struct rpc_pipe_client *cli,
    2013             :                                    TALLOC_CTX *mem_ctx, int argc,
    2014             :                                    const char **argv)
    2015             : {
    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;
    2021             :         DATA_BLOB enc_key;
    2022             :         DATA_BLOB session_key;
    2023           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    2024             : 
    2025           0 :         if (argc < 3) {
    2026           0 :                 printf("Usage: %s name secret\n", argv[0]);
    2027           0 :                 return NT_STATUS_OK;
    2028             :         }
    2029             : 
    2030           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx,
    2031             :                                          true,
    2032             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    2033             :                                          &handle);
    2034           0 :         if (!NT_STATUS_IS_OK(status)) {
    2035           0 :                 return status;
    2036             :         }
    2037             : 
    2038           0 :         init_lsa_String(&name, argv[1]);
    2039             : 
    2040           0 :         status = dcerpc_lsa_OpenSecret(b, mem_ctx,
    2041             :                                        &handle,
    2042             :                                        name,
    2043             :                                        SEC_FLAG_MAXIMUM_ALLOWED,
    2044             :                                        &sec_handle,
    2045             :                                        &result);
    2046           0 :         if (!NT_STATUS_IS_OK(status)) {
    2047           0 :                 goto done;
    2048             :         }
    2049           0 :         if (!NT_STATUS_IS_OK(result)) {
    2050           0 :                 status = result;
    2051           0 :                 goto done;
    2052             :         }
    2053             : 
    2054           0 :         ZERO_STRUCT(new_val);
    2055           0 :         ZERO_STRUCT(old_val);
    2056             : 
    2057           0 :         status = cli_get_session_key(mem_ctx, cli, &session_key);
    2058           0 :         if (!NT_STATUS_IS_OK(status)) {
    2059           0 :                 goto done;
    2060             :         }
    2061             : 
    2062           0 :         enc_key = sess_encrypt_string(argv[2], &session_key);
    2063             : 
    2064           0 :         new_val.length = enc_key.length;
    2065           0 :         new_val.size = enc_key.length;
    2066           0 :         new_val.data = enc_key.data;
    2067             : 
    2068           0 :         status = dcerpc_lsa_SetSecret(b, mem_ctx,
    2069             :                                       &sec_handle,
    2070             :                                       &new_val,
    2071             :                                       NULL,
    2072             :                                       &result);
    2073           0 :         if (!NT_STATUS_IS_OK(status)) {
    2074           0 :                 goto done;
    2075             :         }
    2076           0 :         if (!NT_STATUS_IS_OK(result)) {
    2077           0 :                 status = result;
    2078           0 :                 goto done;
    2079             :         }
    2080             : 
    2081           0 :  done:
    2082           0 :         if (is_valid_policy_hnd(&sec_handle)) {
    2083           0 :                 dcerpc_lsa_Close(b, mem_ctx, &sec_handle, &result);
    2084             :         }
    2085           0 :         if (is_valid_policy_hnd(&handle)) {
    2086           0 :                 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
    2087             :         }
    2088             : 
    2089           0 :         return status;
    2090             : }
    2091             : 
    2092           0 : static NTSTATUS cmd_lsa_retrieve_private_data(struct rpc_pipe_client *cli,
    2093             :                                               TALLOC_CTX *mem_ctx, int argc,
    2094             :                                               const char **argv)
    2095             : {
    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           0 :         DATA_BLOB blob = data_blob_null;
    2102             :         char *secret;
    2103           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    2104             : 
    2105           0 :         if (argc < 2) {
    2106           0 :                 printf("Usage: %s name\n", argv[0]);
    2107           0 :                 return NT_STATUS_OK;
    2108             :         }
    2109             : 
    2110           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx,
    2111             :                                          true,
    2112             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    2113             :                                          &handle);
    2114           0 :         if (!NT_STATUS_IS_OK(status)) {
    2115           0 :                 return status;
    2116             :         }
    2117             : 
    2118           0 :         init_lsa_String(&name, argv[1]);
    2119             : 
    2120           0 :         ZERO_STRUCT(val);
    2121             : 
    2122           0 :         status = dcerpc_lsa_RetrievePrivateData(b, mem_ctx,
    2123             :                                                 &handle,
    2124             :                                                 &name,
    2125             :                                                 &val,
    2126             :                                                 &result);
    2127           0 :         if (!NT_STATUS_IS_OK(status)) {
    2128           0 :                 goto done;
    2129             :         }
    2130           0 :         if (!NT_STATUS_IS_OK(result)) {
    2131           0 :                 status = result;
    2132           0 :                 goto done;
    2133             :         }
    2134             : 
    2135           0 :         status = cli_get_session_key(mem_ctx, cli, &session_key);
    2136           0 :         if (!NT_STATUS_IS_OK(status)) {
    2137           0 :                 goto done;
    2138             :         }
    2139             : 
    2140           0 :         if (val) {
    2141           0 :                 blob = data_blob_const(val->data, val->length);
    2142             :         }
    2143             : 
    2144           0 :         secret = sess_decrypt_string(mem_ctx, &blob, &session_key);
    2145           0 :         if (secret) {
    2146           0 :                 d_printf("secret: %s\n", secret);
    2147             :         }
    2148             : 
    2149           0 :  done:
    2150           0 :         if (is_valid_policy_hnd(&handle)) {
    2151           0 :                 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
    2152             :         }
    2153             : 
    2154           0 :         return status;
    2155             : }
    2156             : 
    2157           0 : static NTSTATUS cmd_lsa_store_private_data(struct rpc_pipe_client *cli,
    2158             :                                            TALLOC_CTX *mem_ctx, int argc,
    2159             :                                            const char **argv)
    2160             : {
    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;
    2166             :         DATA_BLOB enc_key;
    2167           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    2168             : 
    2169           0 :         if (argc < 3) {
    2170           0 :                 printf("Usage: %s name secret\n", argv[0]);
    2171           0 :                 return NT_STATUS_OK;
    2172             :         }
    2173             : 
    2174           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx,
    2175             :                                          true,
    2176             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    2177             :                                          &handle);
    2178           0 :         if (!NT_STATUS_IS_OK(status)) {
    2179           0 :                 return status;
    2180             :         }
    2181             : 
    2182           0 :         init_lsa_String(&name, argv[1]);
    2183             : 
    2184           0 :         ZERO_STRUCT(val);
    2185             : 
    2186           0 :         status = cli_get_session_key(mem_ctx, cli, &session_key);
    2187           0 :         if (!NT_STATUS_IS_OK(status)) {
    2188           0 :                 goto done;
    2189             :         }
    2190             : 
    2191           0 :         enc_key = sess_encrypt_string(argv[2], &session_key);
    2192             : 
    2193           0 :         val.length = enc_key.length;
    2194           0 :         val.size = enc_key.length;
    2195           0 :         val.data = enc_key.data;
    2196             : 
    2197           0 :         status = dcerpc_lsa_StorePrivateData(b, mem_ctx,
    2198             :                                              &handle,
    2199             :                                              &name,
    2200             :                                              &val,
    2201             :                                              &result);
    2202           0 :         if (!NT_STATUS_IS_OK(status)) {
    2203           0 :                 goto done;
    2204             :         }
    2205           0 :         if (!NT_STATUS_IS_OK(result)) {
    2206           0 :                 status = result;
    2207           0 :                 goto done;
    2208             :         }
    2209             : 
    2210           0 :  done:
    2211           0 :         if (is_valid_policy_hnd(&handle)) {
    2212           0 :                 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
    2213             :         }
    2214             : 
    2215           0 :         return status;
    2216             : }
    2217             : 
    2218           0 : static NTSTATUS cmd_lsa_create_trusted_domain(struct rpc_pipe_client *cli,
    2219             :                                               TALLOC_CTX *mem_ctx, int argc,
    2220             :                                               const char **argv)
    2221             : {
    2222             :         NTSTATUS status, result;
    2223             :         struct policy_handle handle, trustdom_handle;
    2224             :         struct dom_sid sid;
    2225             :         struct lsa_DomainInfo info;
    2226           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    2227             : 
    2228           0 :         if (argc < 3) {
    2229           0 :                 printf("Usage: %s name sid\n", argv[0]);
    2230           0 :                 return NT_STATUS_OK;
    2231             :         }
    2232             : 
    2233           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx,
    2234             :                                          true,
    2235             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    2236             :                                          &handle);
    2237           0 :         if (!NT_STATUS_IS_OK(status)) {
    2238           0 :                 return status;
    2239             :         }
    2240             : 
    2241           0 :         init_lsa_StringLarge(&info.name, argv[1]);
    2242           0 :         info.sid = &sid;
    2243           0 :         string_to_sid(&sid, argv[2]);
    2244             : 
    2245           0 :         status = dcerpc_lsa_CreateTrustedDomain(b, mem_ctx,
    2246             :                                                 &handle,
    2247             :                                                 &info,
    2248             :                                                 SEC_FLAG_MAXIMUM_ALLOWED,
    2249             :                                                 &trustdom_handle,
    2250             :                                                 &result);
    2251           0 :         if (!NT_STATUS_IS_OK(status)) {
    2252           0 :                 goto done;
    2253             :         }
    2254           0 :         if (!NT_STATUS_IS_OK(result)) {
    2255           0 :                 status = result;
    2256           0 :                 goto done;
    2257             :         }
    2258             : 
    2259           0 :  done:
    2260           0 :         if (is_valid_policy_hnd(&trustdom_handle)) {
    2261           0 :                 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
    2262             :         }
    2263             : 
    2264           0 :         if (is_valid_policy_hnd(&handle)) {
    2265           0 :                 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
    2266             :         }
    2267             : 
    2268           0 :         return status;
    2269             : }
    2270             : 
    2271           0 : static NTSTATUS cmd_lsa_delete_trusted_domain(struct rpc_pipe_client *cli,
    2272             :                                               TALLOC_CTX *mem_ctx, int argc,
    2273             :                                               const char **argv)
    2274             : {
    2275             :         NTSTATUS status, result;
    2276             :         struct policy_handle handle, trustdom_handle;
    2277             :         struct lsa_String name;
    2278           0 :         struct dom_sid *sid = NULL;
    2279           0 :         struct dcerpc_binding_handle *b = cli->binding_handle;
    2280             : 
    2281           0 :         if (argc < 2) {
    2282           0 :                 printf("Usage: %s name\n", argv[0]);
    2283           0 :                 return NT_STATUS_OK;
    2284             :         }
    2285             : 
    2286           0 :         status = rpccli_lsa_open_policy2(cli, mem_ctx,
    2287             :                                          true,
    2288             :                                          SEC_FLAG_MAXIMUM_ALLOWED,
    2289             :                                          &handle);
    2290           0 :         if (!NT_STATUS_IS_OK(status)) {
    2291           0 :                 return status;
    2292             :         }
    2293             : 
    2294           0 :         init_lsa_String(&name, argv[1]);
    2295             : 
    2296           0 :         status = dcerpc_lsa_OpenTrustedDomainByName(b, mem_ctx,
    2297             :                                                     &handle,
    2298             :                                                     name,
    2299             :                                                     SEC_FLAG_MAXIMUM_ALLOWED,
    2300             :                                                     &trustdom_handle,
    2301             :                                                     &result);
    2302           0 :         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
    2303           0 :                 goto delete_object;
    2304             :         }
    2305             : 
    2306             :         {
    2307           0 :                 uint32_t resume_handle = 0;
    2308             :                 struct lsa_DomainList domains;
    2309             :                 int i;
    2310             : 
    2311           0 :                 status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
    2312             :                                                  &handle,
    2313             :                                                  &resume_handle,
    2314             :                                                  &domains,
    2315             :                                                  0xffff,
    2316             :                                                  &result);
    2317           0 :                 if (!NT_STATUS_IS_OK(status)) {
    2318           0 :                         goto done;
    2319             :                 }
    2320           0 :                 if (!NT_STATUS_IS_OK(result)) {
    2321           0 :                         status = result;
    2322           0 :                         goto done;
    2323             :                 }
    2324             : 
    2325           0 :                 for (i=0; i < domains.count; i++) {
    2326           0 :                         if (strequal(domains.domains[i].name.string, argv[1])) {
    2327           0 :                                 sid = domains.domains[i].sid;
    2328           0 :                                 break;
    2329             :                         }
    2330             :                 }
    2331             : 
    2332           0 :                 if (!sid) {
    2333           0 :                         return NT_STATUS_INVALID_SID;
    2334             :                 }
    2335             :         }
    2336             : 
    2337           0 :         status = dcerpc_lsa_OpenTrustedDomain(b, mem_ctx,
    2338             :                                               &handle,
    2339             :                                               sid,
    2340             :                                               SEC_FLAG_MAXIMUM_ALLOWED,
    2341             :                                               &trustdom_handle,
    2342             :                                               &result);
    2343           0 :         if (!NT_STATUS_IS_OK(status)) {
    2344           0 :                 goto done;
    2345             :         }
    2346           0 :         if (!NT_STATUS_IS_OK(result)) {
    2347           0 :                 status = result;
    2348           0 :                 goto done;
    2349             :         }
    2350             : 
    2351           0 :  delete_object:
    2352           0 :         status = dcerpc_lsa_DeleteObject(b, mem_ctx,
    2353             :                                          &trustdom_handle,
    2354             :                                          &result);
    2355           0 :         if (!NT_STATUS_IS_OK(status)) {
    2356           0 :                 goto done;
    2357             :         }
    2358           0 :         if (!NT_STATUS_IS_OK(result)) {
    2359           0 :                 status = result;
    2360           0 :                 goto done;
    2361             :         }
    2362             : 
    2363           0 :  done:
    2364           0 :         if (is_valid_policy_hnd(&trustdom_handle)) {
    2365           0 :                 dcerpc_lsa_Close(b, mem_ctx, &trustdom_handle, &result);
    2366             :         }
    2367             : 
    2368           0 :         if (is_valid_policy_hnd(&handle)) {
    2369           0 :                 dcerpc_lsa_Close(b, mem_ctx, &handle, &result);
    2370             :         }
    2371             : 
    2372           0 :         return status;
    2373             : }
    2374             : 
    2375             : 
    2376             : /* List of commands exported by this module */
    2377             : 
    2378             : struct cmd_set lsarpc_commands[] = {
    2379             : 
    2380             :         {
    2381             :                 .name = "LSARPC",
    2382             :         },
    2383             : 
    2384             :         {
    2385             :                 .name               = "lsaquery",
    2386             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2387             :                 .ntfn               = cmd_lsa_query_info_policy,
    2388             :                 .wfn                =  NULL,
    2389             :                 .table              = &ndr_table_lsarpc,
    2390             :                 .rpc_pipe           = NULL,
    2391             :                 .description        = "Query info policy",
    2392             :                 .usage              = "",
    2393             :         },
    2394             :         {
    2395             :                 .name               = "lookupsids",
    2396             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2397             :                 .ntfn               = cmd_lsa_lookup_sids,
    2398             :                 .wfn                = NULL,
    2399             :                 .table              = &ndr_table_lsarpc,
    2400             :                 .rpc_pipe           = NULL,
    2401             :                 .description        = "Convert SIDs to names",
    2402             :                 .usage              = "",
    2403             :         },
    2404             :         {
    2405             :                 .name               = "lookupsids3",
    2406             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2407             :                 .ntfn               = cmd_lsa_lookup_sids3,
    2408             :                 .wfn                = NULL,
    2409             :                 .table              = &ndr_table_lsarpc,
    2410             :                 .rpc_pipe           = NULL,
    2411             :                 .description        = "Convert SIDs to names",
    2412             :                 .usage              = "",
    2413             :         },
    2414             :         {
    2415             :                 .name               = "lookupsids_level",
    2416             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2417             :                 .ntfn               = cmd_lsa_lookup_sids_level,
    2418             :                 .wfn                = NULL,
    2419             :                 .table              = &ndr_table_lsarpc,
    2420             :                 .rpc_pipe           = NULL,
    2421             :                 .description        = "Convert SIDs to names",
    2422             :                 .usage              = "",
    2423             :         },
    2424             :         {
    2425             :                 .name               = "lookupnames",
    2426             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2427             :                 .ntfn               = cmd_lsa_lookup_names,
    2428             :                 .wfn                = NULL,
    2429             :                 .table              = &ndr_table_lsarpc,
    2430             :                 .rpc_pipe           = NULL,
    2431             :                 .description        = "Convert names to SIDs",
    2432             :                 .usage              = "",
    2433             :         },
    2434             :         {
    2435             :                 .name               = "lookupnames4",
    2436             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2437             :                 .ntfn               = cmd_lsa_lookup_names4,
    2438             :                 .wfn                = NULL,
    2439             :                 .table              = &ndr_table_lsarpc,
    2440             :                 .rpc_pipe           = NULL,
    2441             :                 .description        = "Convert names to SIDs",
    2442             :                 .usage              = "",
    2443             :         },
    2444             :         {
    2445             :                 .name               = "lookupnames_level",
    2446             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2447             :                 .ntfn               = cmd_lsa_lookup_names_level,
    2448             :                 .wfn                = NULL,
    2449             :                 .table              = &ndr_table_lsarpc,
    2450             :                 .rpc_pipe           = NULL,
    2451             :                 .description        = "Convert names to SIDs",
    2452             :                 .usage              = "",
    2453             :         },
    2454             :         {
    2455             :                 .name               = "enumtrust",
    2456             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2457             :                 .ntfn               = cmd_lsa_enum_trust_dom,
    2458             :                 .wfn                = NULL,
    2459             :                 .table              = &ndr_table_lsarpc,
    2460             :                 .rpc_pipe           = NULL,
    2461             :                 .description        = "Enumerate trusted domains",
    2462             :                 .usage              = "Usage: [preferred max number] [enum context (0)]",
    2463             :         },
    2464             :         {
    2465             :                 .name               = "enumprivs",
    2466             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2467             :                 .ntfn               = cmd_lsa_enum_privilege,
    2468             :                 .wfn                = NULL,
    2469             :                 .table              = &ndr_table_lsarpc,
    2470             :                 .rpc_pipe           = NULL,
    2471             :                 .description        = "Enumerate privileges",
    2472             :                 .usage              = "",
    2473             :         },
    2474             :         {
    2475             :                 .name               = "getdispname",
    2476             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2477             :                 .ntfn               = cmd_lsa_get_dispname,
    2478             :                 .wfn                = NULL,
    2479             :                 .table              = &ndr_table_lsarpc,
    2480             :                 .rpc_pipe           = NULL,
    2481             :                 .description        = "Get the privilege name",
    2482             :                 .usage              = "",
    2483             :         },
    2484             :         {
    2485             :                 .name               = "lsaenumsid",
    2486             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2487             :                 .ntfn               = cmd_lsa_enum_sids,
    2488             :                 .wfn                = NULL,
    2489             :                 .table              = &ndr_table_lsarpc,
    2490             :                 .rpc_pipe           = NULL,
    2491             :                 .description        = "Enumerate the LSA SIDS",
    2492             :                 .usage              = "",
    2493             :         },
    2494             :         {
    2495             :                 .name               = "lsacreateaccount",
    2496             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2497             :                 .ntfn               = cmd_lsa_create_account,
    2498             :                 .wfn                = NULL,
    2499             :                 .table              = &ndr_table_lsarpc,
    2500             :                 .rpc_pipe           = NULL,
    2501             :                 .description        = "Create a new lsa account",
    2502             :                 .usage              = "",
    2503             :         },
    2504             :         {
    2505             :                 .name               = "lsaenumprivsaccount",
    2506             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2507             :                 .ntfn               = cmd_lsa_enum_privsaccounts,
    2508             :                 .wfn                = NULL,
    2509             :                 .table              = &ndr_table_lsarpc,
    2510             :                 .rpc_pipe           = NULL,
    2511             :                 .description        = "Enumerate the privileges of an SID",
    2512             :                 .usage              = "",
    2513             :         },
    2514             :         {
    2515             :                 .name               = "lsaenumacctrights",
    2516             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2517             :                 .ntfn               = cmd_lsa_enum_acct_rights,
    2518             :                 .wfn                = NULL,
    2519             :                 .table              = &ndr_table_lsarpc,
    2520             :                 .rpc_pipe           = NULL,
    2521             :                 .description        = "Enumerate the rights of an SID",
    2522             :                 .usage              = "",
    2523             :         },
    2524             :         {
    2525             :                 .name               = "lsaaddpriv",
    2526             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2527             :                 .ntfn               = cmd_lsa_add_priv,
    2528             :                 .wfn                = NULL,
    2529             :                 .table              = &ndr_table_lsarpc,
    2530             :                 .rpc_pipe           = NULL,
    2531             :                 .description        = "Assign a privilege to a SID",
    2532             :                 .usage              = "",
    2533             :         },
    2534             :         {
    2535             :                 .name               = "lsadelpriv",
    2536             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2537             :                 .ntfn               = cmd_lsa_del_priv,
    2538             :                 .wfn                = NULL,
    2539             :                 .table              = &ndr_table_lsarpc,
    2540             :                 .rpc_pipe           = NULL,
    2541             :                 .description        = "Revoke a privilege from a SID",
    2542             :                 .usage              = "",
    2543             :         },
    2544             :         {
    2545             :                 .name               = "lsaaddacctrights",
    2546             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2547             :                 .ntfn               = cmd_lsa_add_acct_rights,
    2548             :                 .wfn                = NULL,
    2549             :                 .table              = &ndr_table_lsarpc,
    2550             :                 .rpc_pipe           = NULL,
    2551             :                 .description        = "Add rights to an account",
    2552             :                 .usage              = "",
    2553             :         },
    2554             :         {
    2555             :                 .name               = "lsaremoveacctrights",
    2556             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2557             :                 .ntfn               = cmd_lsa_remove_acct_rights,
    2558             :                 .wfn                = NULL,
    2559             :                 .table              = &ndr_table_lsarpc,
    2560             :                 .rpc_pipe           = NULL,
    2561             :                 .description        = "Remove rights from an account",
    2562             :                 .usage              = "",
    2563             :         },
    2564             :         {
    2565             :                 .name               = "lsalookupprivvalue",
    2566             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2567             :                 .ntfn               = cmd_lsa_lookup_priv_value,
    2568             :                 .wfn                = NULL,
    2569             :                 .table              = &ndr_table_lsarpc,
    2570             :                 .rpc_pipe           = NULL,
    2571             :                 .description        = "Get a privilege value given its name",
    2572             :                 .usage              = "",
    2573             :         },
    2574             :         {
    2575             :                 .name               = "lsaquerysecobj",
    2576             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2577             :                 .ntfn               = cmd_lsa_query_secobj,
    2578             :                 .wfn                = NULL,
    2579             :                 .table              = &ndr_table_lsarpc,
    2580             :                 .rpc_pipe           = NULL,
    2581             :                 .description        = "Query LSA security object",
    2582             :                 .usage              = "",
    2583             :         },
    2584             :         {
    2585             :                 .name               = "lsaquerytrustdominfo",
    2586             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2587             :                 .ntfn               = cmd_lsa_query_trustdominfo,
    2588             :                 .wfn                = NULL,
    2589             :                 .table              = &ndr_table_lsarpc,
    2590             :                 .rpc_pipe           = NULL,
    2591             :                 .description        = "Query LSA trusted domains info (given a SID)",
    2592             :                 .usage              = "",
    2593             :         },
    2594             :         {
    2595             :                 .name               = "lsaquerytrustdominfobyname",
    2596             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2597             :                 .ntfn               = cmd_lsa_query_trustdominfobyname,
    2598             :                 .wfn                = NULL,
    2599             :                 .table              = &ndr_table_lsarpc,
    2600             :                 .rpc_pipe           = NULL,
    2601             :                 .description        = "Query LSA trusted domains info (given a name), only works for Windows > 2k",
    2602             :                 .usage              = "",
    2603             :         },
    2604             :         {
    2605             :                 .name               = "lsaquerytrustdominfobysid",
    2606             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2607             :                 .ntfn               = cmd_lsa_query_trustdominfobysid,
    2608             :                 .wfn                = NULL,
    2609             :                 .table              = &ndr_table_lsarpc,
    2610             :                 .rpc_pipe           = NULL,
    2611             :                 .description        = "Query LSA trusted domains info (given a SID)",
    2612             :                 .usage              = "",
    2613             :         },
    2614             :         {
    2615             :                 .name               = "lsasettrustdominfo",
    2616             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2617             :                 .ntfn               = cmd_lsa_set_trustdominfo,
    2618             :                 .wfn                = NULL,
    2619             :                 .table              = &ndr_table_lsarpc,
    2620             :                 .rpc_pipe           = NULL,
    2621             :                 .description        = "Set LSA trusted domain info",
    2622             :                 .usage              = "",
    2623             :         },
    2624             :         {
    2625             :                 .name               = "getusername",
    2626             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2627             :                 .ntfn               = cmd_lsa_get_username,
    2628             :                 .wfn                = NULL,
    2629             :                 .table              = &ndr_table_lsarpc,
    2630             :                 .rpc_pipe           = NULL,
    2631             :                 .description        = "Get username",
    2632             :                 .usage              = "",
    2633             :         },
    2634             :         {
    2635             :                 .name               = "createsecret",
    2636             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2637             :                 .ntfn               = cmd_lsa_create_secret,
    2638             :                 .wfn                = NULL,
    2639             :                 .table              = &ndr_table_lsarpc,
    2640             :                 .rpc_pipe           = NULL,
    2641             :                 .description        = "Create Secret",
    2642             :                 .usage              = "",
    2643             :         },
    2644             :         {
    2645             :                 .name               = "deletesecret",
    2646             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2647             :                 .ntfn               = cmd_lsa_delete_secret,
    2648             :                 .wfn                = NULL,
    2649             :                 .table              = &ndr_table_lsarpc,
    2650             :                 .rpc_pipe           = NULL,
    2651             :                 .description        = "Delete Secret",
    2652             :                 .usage              = "",
    2653             :         },
    2654             :         {
    2655             :                 .name               = "querysecret",
    2656             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2657             :                 .ntfn               = cmd_lsa_query_secret,
    2658             :                 .wfn                = NULL,
    2659             :                 .table              = &ndr_table_lsarpc,
    2660             :                 .rpc_pipe           = NULL,
    2661             :                 .description        = "Query Secret",
    2662             :                 .usage              = "",
    2663             :         },
    2664             :         {
    2665             :                 .name               = "setsecret",
    2666             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2667             :                 .ntfn               = cmd_lsa_set_secret,
    2668             :                 .wfn                = NULL,
    2669             :                 .table              = &ndr_table_lsarpc,
    2670             :                 .rpc_pipe           = NULL,
    2671             :                 .description        = "Set Secret",
    2672             :                 .usage              = "",
    2673             :         },
    2674             :         {
    2675             :                 .name               = "retrieveprivatedata",
    2676             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2677             :                 .ntfn               = cmd_lsa_retrieve_private_data,
    2678             :                 .wfn                = NULL,
    2679             :                 .table              = &ndr_table_lsarpc,
    2680             :                 .rpc_pipe           = NULL,
    2681             :                 .description        = "Retrieve Private Data",
    2682             :                 .usage              = "",
    2683             :         },
    2684             :         {
    2685             :                 .name               = "storeprivatedata",
    2686             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2687             :                 .ntfn               = cmd_lsa_store_private_data,
    2688             :                 .wfn                = NULL,
    2689             :                 .table              = &ndr_table_lsarpc,
    2690             :                 .rpc_pipe           = NULL,
    2691             :                 .description        = "Store Private Data",
    2692             :                 .usage              = "",
    2693             :         },
    2694             :         {
    2695             :                 .name               = "createtrustdom",
    2696             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2697             :                 .ntfn               = cmd_lsa_create_trusted_domain,
    2698             :                 .wfn                = NULL,
    2699             :                 .table              = &ndr_table_lsarpc,
    2700             :                 .rpc_pipe           = NULL,
    2701             :                 .description        = "Create Trusted Domain",
    2702             :                 .usage              = "",
    2703             :         },
    2704             :         {
    2705             :                 .name               = "deletetrustdom",
    2706             :                 .returntype         = RPC_RTYPE_NTSTATUS,
    2707             :                 .ntfn               = cmd_lsa_delete_trusted_domain,
    2708             :                 .wfn                = NULL,
    2709             :                 .table              = &ndr_table_lsarpc,
    2710             :                 .rpc_pipe           = NULL,
    2711             :                 .description        = "Delete Trusted Domain",
    2712             :                 .usage              = "",
    2713             :         },
    2714             :         {
    2715             :                 .name = NULL,
    2716             :         },
    2717             : };

Generated by: LCOV version 1.14