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