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