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