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