Add cmd_lsa_get_username command to rpcclient.
[kai/samba.git] / source3 / rpcclient / cmd_lsarpc.c
1 /*
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Tim Potter              2000
6    Copyright (C) Rafal Szczesniak        2002
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "rpcclient.h"
24
25 static void init_lsa_String(struct lsa_String *name, const char *s)
26 {
27         name->string = s;
28 }
29
30 /* useful function to allow entering a name instead of a SID and
31  * looking it up automatically */
32 static NTSTATUS name_to_sid(struct rpc_pipe_client *cli, 
33                             TALLOC_CTX *mem_ctx,
34                             DOM_SID *sid, const char *name)
35 {
36         POLICY_HND pol;
37         enum lsa_SidType *sid_types;
38         NTSTATUS result;
39         DOM_SID *sids;
40
41         /* maybe its a raw SID */
42         if (strncmp(name, "S-", 2) == 0 &&
43             string_to_sid(sid, name)) {
44                 return NT_STATUS_OK;
45         }
46
47         result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
48                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
49                                      &pol);
50         if (!NT_STATUS_IS_OK(result))
51                 goto done;
52
53         result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, NULL, 1, &sids, &sid_types);
54         if (!NT_STATUS_IS_OK(result))
55                 goto done;
56
57         rpccli_lsa_Close(cli, mem_ctx, &pol);
58
59         *sid = sids[0];
60
61 done:
62         return result;
63 }
64
65 static void display_query_info_1(struct lsa_AuditLogInfo *r)
66 {
67         d_printf("percent_full:\t%d\n", r->percent_full);
68         d_printf("log_size:\t%d\n", r->log_size);
69         d_printf("retention_time:\t%lld\n", (long long)r->retention_time);
70         d_printf("shutdown_in_progress:\t%d\n", r->shutdown_in_progress);
71         d_printf("time_to_shutdown:\t%lld\n", (long long)r->time_to_shutdown);
72         d_printf("next_audit_record:\t%d\n", r->next_audit_record);
73         d_printf("unknown:\t%d\n", r->unknown);
74 }
75
76 static void display_query_info_2(struct lsa_AuditEventsInfo *r)
77 {
78         int i;
79         d_printf("Auditing enabled:\t%d\n", r->auditing_mode);
80         d_printf("Auditing categories:\t%d\n", r->count);
81         d_printf("Auditsettings:\n");
82         for (i=0; i<r->count; i++) {
83                 const char *val = audit_policy_str(talloc_tos(), r->settings[i]);
84                 const char *policy = audit_description_str(i);
85                 d_printf("%s:\t%s\n", policy, val);
86         }
87 }
88
89 static void display_query_info_3(struct lsa_DomainInfo *r)
90 {
91         d_printf("Domain Name: %s\n", r->name.string);
92         d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
93 }
94
95 static void display_query_info_5(struct lsa_DomainInfo *r)
96 {
97         d_printf("Domain Name: %s\n", r->name.string);
98         d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
99 }
100
101 static void display_query_info_10(struct lsa_AuditFullSetInfo *r)
102 {
103         d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
104 }
105
106 static void display_query_info_11(struct lsa_AuditFullQueryInfo *r)
107 {
108         d_printf("Shutdown on full: %d\n", r->shutdown_on_full);
109         d_printf("Log is full: %d\n", r->log_is_full);
110         d_printf("Unknown: %d\n", r->unknown);
111 }
112
113 static void display_query_info_12(struct lsa_DnsDomainInfo *r)
114 {
115         d_printf("Domain NetBios Name: %s\n", r->name.string);
116         d_printf("Domain DNS Name: %s\n", r->dns_domain.string);
117         d_printf("Domain Forest Name: %s\n", r->dns_forest.string);
118         d_printf("Domain Sid: %s\n", sid_string_tos(r->sid));
119         d_printf("Domain GUID: %s\n", smb_uuid_string(talloc_tos(),
120                                                       r->domain_guid));
121 }
122
123 static void display_lsa_query_info(union lsa_PolicyInformation *info,
124                                    enum lsa_PolicyInfo level)
125 {
126         switch (level) {
127                 case 1:
128                         display_query_info_1(&info->audit_log);
129                         break;
130                 case 2:
131                         display_query_info_2(&info->audit_events);
132                         break;
133                 case 3:
134                         display_query_info_3(&info->domain);
135                         break;
136                 case 5:
137                         display_query_info_5(&info->account_domain);
138                         break;
139                 case 10:
140                         display_query_info_10(&info->auditfullset);
141                         break;
142                 case 11:
143                         display_query_info_11(&info->auditfullquery);
144                         break;
145                 case 12:
146                         display_query_info_12(&info->dns);
147                         break;
148                 default:
149                         printf("can't display info level: %d\n", level);
150                         break;
151         }
152 }
153
154 static NTSTATUS cmd_lsa_query_info_policy(struct rpc_pipe_client *cli, 
155                                           TALLOC_CTX *mem_ctx, int argc, 
156                                           const char **argv) 
157 {
158         POLICY_HND pol;
159         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
160         union lsa_PolicyInformation *info = NULL;
161
162         uint32 info_class = 3;
163
164         if (argc > 2) {
165                 printf("Usage: %s [info_class]\n", argv[0]);
166                 return NT_STATUS_OK;
167         }
168
169         if (argc == 2)
170                 info_class = atoi(argv[1]);
171
172         switch (info_class) {
173         case 12:
174                 result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
175                                                  SEC_RIGHTS_MAXIMUM_ALLOWED,
176                                                  &pol);
177
178                 if (!NT_STATUS_IS_OK(result))
179                         goto done;
180                         
181                 result = rpccli_lsa_QueryInfoPolicy2(cli, mem_ctx,
182                                                      &pol,
183                                                      info_class,
184                                                      &info);
185                 break;
186         default:
187                 result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
188                                                 SEC_RIGHTS_MAXIMUM_ALLOWED,
189                                                 &pol);
190
191                 if (!NT_STATUS_IS_OK(result))
192                         goto done;
193                 
194                 result = rpccli_lsa_QueryInfoPolicy(cli, mem_ctx,
195                                                     &pol,
196                                                     info_class,
197                                                     &info);
198         }
199
200         if (NT_STATUS_IS_OK(result)) {
201                 display_lsa_query_info(info, info_class);
202         }
203
204         rpccli_lsa_Close(cli, mem_ctx, &pol);
205
206  done:
207         return result;
208 }
209
210 /* Resolve a list of names to a list of sids */
211
212 static NTSTATUS cmd_lsa_lookup_names(struct rpc_pipe_client *cli, 
213                                      TALLOC_CTX *mem_ctx, int argc, 
214                                      const char **argv)
215 {
216         POLICY_HND pol;
217         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
218         DOM_SID *sids;
219         enum lsa_SidType *types;
220         int i;
221
222         if (argc == 1) {
223                 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
224                 return NT_STATUS_OK;
225         }
226
227         result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
228                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
229                                      &pol);
230
231         if (!NT_STATUS_IS_OK(result))
232                 goto done;
233
234         result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1, 
235                                       (const char**)(argv + 1), NULL, 1, &sids, &types);
236
237         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) != 
238             NT_STATUS_V(STATUS_SOME_UNMAPPED))
239                 goto done;
240
241         result = NT_STATUS_OK;
242
243         /* Print results */
244
245         for (i = 0; i < (argc - 1); i++) {
246                 fstring sid_str;
247                 sid_to_fstring(sid_str, &sids[i]);
248                 printf("%s %s (%s: %d)\n", argv[i + 1], sid_str,
249                        sid_type_lookup(types[i]), types[i]);
250         }
251
252         rpccli_lsa_Close(cli, mem_ctx, &pol);
253
254  done:
255         return result;
256 }
257
258 /* Resolve a list of names to a list of sids */
259
260 static NTSTATUS cmd_lsa_lookup_names_level(struct rpc_pipe_client *cli, 
261                                            TALLOC_CTX *mem_ctx, int argc, 
262                                            const char **argv)
263 {
264         POLICY_HND pol;
265         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
266         DOM_SID *sids;
267         enum lsa_SidType *types;
268         int i, level;
269
270         if (argc < 3) {
271                 printf("Usage: %s [level] [name1 [name2 [...]]]\n", argv[0]);
272                 return NT_STATUS_OK;
273         }
274
275         result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
276                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
277                                      &pol);
278
279         if (!NT_STATUS_IS_OK(result))
280                 goto done;
281
282         level = atoi(argv[1]);
283
284         result = rpccli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 2, 
285                                       (const char**)(argv + 2), NULL, level, &sids, &types);
286
287         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) != 
288             NT_STATUS_V(STATUS_SOME_UNMAPPED))
289                 goto done;
290
291         result = NT_STATUS_OK;
292
293         /* Print results */
294
295         for (i = 0; i < (argc - 2); i++) {
296                 fstring sid_str;
297                 sid_to_fstring(sid_str, &sids[i]);
298                 printf("%s %s (%s: %d)\n", argv[i + 2], sid_str,
299                        sid_type_lookup(types[i]), types[i]);
300         }
301
302         rpccli_lsa_Close(cli, mem_ctx, &pol);
303
304  done:
305         return result;
306 }
307
308
309 /* Resolve a list of SIDs to a list of names */
310
311 static NTSTATUS cmd_lsa_lookup_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
312                                     int argc, const char **argv)
313 {
314         POLICY_HND pol;
315         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
316         DOM_SID *sids;
317         char **domains;
318         char **names;
319         enum lsa_SidType *types;
320         int i;
321
322         if (argc == 1) {
323                 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
324                 return NT_STATUS_OK;
325         }
326
327         result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
328                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
329                                      &pol);
330
331         if (!NT_STATUS_IS_OK(result))
332                 goto done;
333
334         /* Convert arguments to sids */
335
336         sids = TALLOC_ARRAY(mem_ctx, DOM_SID, argc - 1);
337
338         if (!sids) {
339                 printf("could not allocate memory for %d sids\n", argc - 1);
340                 goto done;
341         }
342
343         for (i = 0; i < argc - 1; i++) 
344                 if (!string_to_sid(&sids[i], argv[i + 1])) {
345                         result = NT_STATUS_INVALID_SID;
346                         goto done;
347                 }
348
349         /* Lookup the SIDs */
350
351         result = rpccli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids, 
352                                      &domains, &names, &types);
353
354         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) != 
355             NT_STATUS_V(STATUS_SOME_UNMAPPED))
356                 goto done;
357
358         result = NT_STATUS_OK;
359
360         /* Print results */
361
362         for (i = 0; i < (argc - 1); i++) {
363                 fstring sid_str;
364
365                 sid_to_fstring(sid_str, &sids[i]);
366                 printf("%s %s\\%s (%d)\n", sid_str, 
367                        domains[i] ? domains[i] : "*unknown*", 
368                        names[i] ? names[i] : "*unknown*", types[i]);
369         }
370
371         rpccli_lsa_Close(cli, mem_ctx, &pol);
372
373  done:
374         return result;
375 }
376
377 /* Enumerate list of trusted domains */
378
379 static NTSTATUS cmd_lsa_enum_trust_dom(struct rpc_pipe_client *cli, 
380                                        TALLOC_CTX *mem_ctx, int argc, 
381                                        const char **argv)
382 {
383         POLICY_HND pol;
384         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
385         DOM_SID *domain_sids;
386         char **domain_names;
387
388         /* defaults, but may be changed using params */
389         uint32 enum_ctx = 0;
390         uint32 num_domains = 0;
391         int i;
392
393         if (argc > 2) {
394                 printf("Usage: %s [enum context (0)]\n", argv[0]);
395                 return NT_STATUS_OK;
396         }
397
398         if (argc == 2 && argv[1]) {
399                 enum_ctx = atoi(argv[2]);
400         }       
401
402         result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
403                                      POLICY_VIEW_LOCAL_INFORMATION,
404                                      &pol);
405
406         if (!NT_STATUS_IS_OK(result))
407                 goto done;
408
409         result = STATUS_MORE_ENTRIES;
410
411         while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
412
413                 /* Lookup list of trusted domains */
414
415                 result = rpccli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx,
416                                                 &num_domains,
417                                                 &domain_names, &domain_sids);
418                 if (!NT_STATUS_IS_OK(result) &&
419                     !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
420                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
421                         goto done;
422
423                 /* Print results: list of names and sids returned in this
424                  * response. */  
425                 for (i = 0; i < num_domains; i++) {
426                         fstring sid_str;
427
428                         sid_to_fstring(sid_str, &domain_sids[i]);
429                         printf("%s %s\n", domain_names[i] ? domain_names[i] : 
430                                "*unknown*", sid_str);
431                 }
432         }
433
434         rpccli_lsa_Close(cli, mem_ctx, &pol);
435  done:
436         return result;
437 }
438
439 /* Enumerates privileges */
440
441 static NTSTATUS cmd_lsa_enum_privilege(struct rpc_pipe_client *cli, 
442                                        TALLOC_CTX *mem_ctx, int argc, 
443                                        const char **argv) 
444 {
445         POLICY_HND pol;
446         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
447         struct lsa_PrivArray priv_array;
448
449         uint32 enum_context=0;
450         uint32 pref_max_length=0x1000;
451         int i;
452
453         if (argc > 3) {
454                 printf("Usage: %s [enum context] [max length]\n", argv[0]);
455                 return NT_STATUS_OK;
456         }
457
458         if (argc>=2)
459                 enum_context=atoi(argv[1]);
460
461         if (argc==3)
462                 pref_max_length=atoi(argv[2]);
463
464         result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
465                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
466                                      &pol);
467
468         if (!NT_STATUS_IS_OK(result))
469                 goto done;
470
471         result = rpccli_lsa_EnumPrivs(cli, mem_ctx,
472                                       &pol,
473                                       &enum_context,
474                                       &priv_array,
475                                       pref_max_length);
476         if (!NT_STATUS_IS_OK(result))
477                 goto done;
478
479         /* Print results */
480         printf("found %d privileges\n\n", priv_array.count);
481
482         for (i = 0; i < priv_array.count; i++) {
483                 printf("%s \t\t%d:%d (0x%x:0x%x)\n",
484                        priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*",
485                        priv_array.privs[i].luid.high,
486                        priv_array.privs[i].luid.low,
487                        priv_array.privs[i].luid.high,
488                        priv_array.privs[i].luid.low);
489         }
490
491         rpccli_lsa_Close(cli, mem_ctx, &pol);
492  done:
493         return result;
494 }
495
496 /* Get privilege name */
497
498 static NTSTATUS cmd_lsa_get_dispname(struct rpc_pipe_client *cli, 
499                                      TALLOC_CTX *mem_ctx, int argc, 
500                                      const char **argv) 
501 {
502         POLICY_HND pol;
503         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
504
505         uint16 lang_id=0;
506         uint16 lang_id_sys=0;
507         uint16 lang_id_desc;
508         struct lsa_String lsa_name;
509         struct lsa_StringLarge *description = NULL;
510
511         if (argc != 2) {
512                 printf("Usage: %s privilege name\n", argv[0]);
513                 return NT_STATUS_OK;
514         }
515
516         result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
517                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
518                                      &pol);
519
520         if (!NT_STATUS_IS_OK(result))
521                 goto done;
522
523         init_lsa_String(&lsa_name, argv[1]);
524
525         result = rpccli_lsa_LookupPrivDisplayName(cli, mem_ctx,
526                                                   &pol,
527                                                   &lsa_name,
528                                                   lang_id,
529                                                   lang_id_sys,
530                                                   &description,
531                                                   &lang_id_desc);
532
533         if (!NT_STATUS_IS_OK(result))
534                 goto done;
535
536         /* Print results */
537         printf("%s -> %s (language: 0x%x)\n", argv[1], description->string, lang_id_desc);
538
539         rpccli_lsa_Close(cli, mem_ctx, &pol);
540  done:
541         return result;
542 }
543
544 /* Enumerate the LSA SIDS */
545
546 static NTSTATUS cmd_lsa_enum_sids(struct rpc_pipe_client *cli, 
547                                   TALLOC_CTX *mem_ctx, int argc, 
548                                   const char **argv) 
549 {
550         POLICY_HND pol;
551         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
552
553         uint32 enum_context=0;
554         uint32 pref_max_length=0x1000;
555         DOM_SID *sids;
556         uint32 count=0;
557         int i;
558
559         if (argc > 3) {
560                 printf("Usage: %s [enum context] [max length]\n", argv[0]);
561                 return NT_STATUS_OK;
562         }
563
564         if (argc>=2)
565                 enum_context=atoi(argv[1]);
566
567         if (argc==3)
568                 pref_max_length=atoi(argv[2]);
569
570         result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
571                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
572                                      &pol);
573
574         if (!NT_STATUS_IS_OK(result))
575                 goto done;
576
577         result = rpccli_lsa_enum_sids(cli, mem_ctx, &pol, &enum_context, pref_max_length,
578                                         &count, &sids);
579
580         if (!NT_STATUS_IS_OK(result))
581                 goto done;
582
583         /* Print results */
584         printf("found %d SIDs\n\n", count);
585
586         for (i = 0; i < count; i++) {
587                 fstring sid_str;
588
589                 sid_to_fstring(sid_str, &sids[i]);
590                 printf("%s\n", sid_str);
591         }
592
593         rpccli_lsa_Close(cli, mem_ctx, &pol);
594  done:
595         return result;
596 }
597
598 /* Create a new account */
599
600 static NTSTATUS cmd_lsa_create_account(struct rpc_pipe_client *cli, 
601                                            TALLOC_CTX *mem_ctx, int argc, 
602                                            const char **argv) 
603 {
604         POLICY_HND dom_pol;
605         POLICY_HND user_pol;
606         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
607         uint32 des_access = 0x000f000f;
608         
609         DOM_SID sid;
610
611         if (argc != 2 ) {
612                 printf("Usage: %s SID\n", argv[0]);
613                 return NT_STATUS_OK;
614         }
615
616         result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
617         if (!NT_STATUS_IS_OK(result))
618                 goto done;      
619
620         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
621                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
622                                      &dom_pol);
623
624         if (!NT_STATUS_IS_OK(result))
625                 goto done;
626
627         result = rpccli_lsa_CreateAccount(cli, mem_ctx,
628                                           &dom_pol,
629                                           &sid,
630                                           des_access,
631                                           &user_pol);
632
633         if (!NT_STATUS_IS_OK(result))
634                 goto done;
635
636         printf("Account for SID %s successfully created\n\n", argv[1]);
637         result = NT_STATUS_OK;
638
639         rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
640  done:
641         return result;
642 }
643
644
645 /* Enumerate the privileges of an SID */
646
647 static NTSTATUS cmd_lsa_enum_privsaccounts(struct rpc_pipe_client *cli, 
648                                            TALLOC_CTX *mem_ctx, int argc, 
649                                            const char **argv) 
650 {
651         POLICY_HND dom_pol;
652         POLICY_HND user_pol;
653         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
654         uint32 access_desired = 0x000f000f;
655         
656         DOM_SID sid;
657         uint32 count=0;
658         LUID_ATTR *set;
659         int i;
660
661         if (argc != 2 ) {
662                 printf("Usage: %s SID\n", argv[0]);
663                 return NT_STATUS_OK;
664         }
665
666         result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
667         if (!NT_STATUS_IS_OK(result))
668                 goto done;      
669
670         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
671                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
672                                      &dom_pol);
673
674         if (!NT_STATUS_IS_OK(result))
675                 goto done;
676
677         result = rpccli_lsa_OpenAccount(cli, mem_ctx,
678                                         &dom_pol,
679                                         &sid,
680                                         access_desired,
681                                         &user_pol);
682
683         if (!NT_STATUS_IS_OK(result))
684                 goto done;
685
686         result = rpccli_lsa_enum_privsaccount(cli, mem_ctx, &user_pol, &count, &set);
687
688         if (!NT_STATUS_IS_OK(result))
689                 goto done;
690
691         /* Print results */
692         printf("found %d privileges for SID %s\n\n", count, argv[1]);
693         printf("high\tlow\tattribute\n");
694
695         for (i = 0; i < count; i++) {
696                 printf("%u\t%u\t%u\n", set[i].luid.high, set[i].luid.low, set[i].attr);
697         }
698
699         rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
700  done:
701         return result;
702 }
703
704
705 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
706
707 static NTSTATUS cmd_lsa_enum_acct_rights(struct rpc_pipe_client *cli, 
708                                          TALLOC_CTX *mem_ctx, int argc, 
709                                          const char **argv) 
710 {
711         POLICY_HND dom_pol;
712         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
713
714         DOM_SID sid;
715         uint32 count;
716         char **rights;
717
718         int i;
719
720         if (argc != 2 ) {
721                 printf("Usage: %s SID\n", argv[0]);
722                 return NT_STATUS_OK;
723         }
724
725         result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
726         if (!NT_STATUS_IS_OK(result))
727                 goto done;      
728
729         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
730                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
731                                      &dom_pol);
732
733         if (!NT_STATUS_IS_OK(result))
734                 goto done;
735
736         result = rpccli_lsa_enum_account_rights(cli, mem_ctx, &dom_pol, &sid, &count, &rights);
737
738         if (!NT_STATUS_IS_OK(result))
739                 goto done;
740
741         printf("found %d privileges for SID %s\n", count,
742                sid_string_tos(&sid));
743
744         for (i = 0; i < count; i++) {
745                 printf("\t%s\n", rights[i]);
746         }
747
748         rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
749  done:
750         return result;
751 }
752
753
754 /* add some privileges to a SID via LsaAddAccountRights */
755
756 static NTSTATUS cmd_lsa_add_acct_rights(struct rpc_pipe_client *cli, 
757                                         TALLOC_CTX *mem_ctx, int argc, 
758                                         const char **argv) 
759 {
760         POLICY_HND dom_pol;
761         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
762
763         DOM_SID sid;
764
765         if (argc < 3 ) {
766                 printf("Usage: %s SID [rights...]\n", argv[0]);
767                 return NT_STATUS_OK;
768         }
769
770         result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
771         if (!NT_STATUS_IS_OK(result))
772                 goto done;      
773
774         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
775                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
776                                      &dom_pol);
777
778         if (!NT_STATUS_IS_OK(result))
779                 goto done;
780
781         result = rpccli_lsa_add_account_rights(cli, mem_ctx, &dom_pol, sid, 
782                                             argc-2, argv+2);
783
784         if (!NT_STATUS_IS_OK(result))
785                 goto done;
786
787         rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
788  done:
789         return result;
790 }
791
792
793 /* remove some privileges to a SID via LsaRemoveAccountRights */
794
795 static NTSTATUS cmd_lsa_remove_acct_rights(struct rpc_pipe_client *cli, 
796                                         TALLOC_CTX *mem_ctx, int argc, 
797                                         const char **argv) 
798 {
799         POLICY_HND dom_pol;
800         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
801
802         DOM_SID sid;
803
804         if (argc < 3 ) {
805                 printf("Usage: %s SID [rights...]\n", argv[0]);
806                 return NT_STATUS_OK;
807         }
808
809         result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
810         if (!NT_STATUS_IS_OK(result))
811                 goto done;      
812
813         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
814                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
815                                      &dom_pol);
816
817         if (!NT_STATUS_IS_OK(result))
818                 goto done;
819
820         result = rpccli_lsa_remove_account_rights(cli, mem_ctx, &dom_pol, sid, 
821                                                False, argc-2, argv+2);
822
823         if (!NT_STATUS_IS_OK(result))
824                 goto done;
825
826         rpccli_lsa_Close(cli, mem_ctx, &dom_pol);
827
828  done:
829         return result;
830 }
831
832
833 /* Get a privilege value given its name */
834
835 static NTSTATUS cmd_lsa_lookup_priv_value(struct rpc_pipe_client *cli, 
836                                         TALLOC_CTX *mem_ctx, int argc, 
837                                         const char **argv) 
838 {
839         POLICY_HND pol;
840         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
841         LUID luid;
842
843         if (argc != 2 ) {
844                 printf("Usage: %s name\n", argv[0]);
845                 return NT_STATUS_OK;
846         }
847
848         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
849                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
850                                      &pol);
851
852         if (!NT_STATUS_IS_OK(result))
853                 goto done;
854
855         result = rpccli_lsa_lookup_priv_value(cli, mem_ctx, &pol, argv[1], &luid);
856
857         if (!NT_STATUS_IS_OK(result))
858                 goto done;
859
860         /* Print results */
861
862         printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
863
864         rpccli_lsa_Close(cli, mem_ctx, &pol);
865  done:
866         return result;
867 }
868
869 /* Query LSA security object */
870
871 static NTSTATUS cmd_lsa_query_secobj(struct rpc_pipe_client *cli, 
872                                      TALLOC_CTX *mem_ctx, int argc, 
873                                      const char **argv) 
874 {
875         POLICY_HND pol;
876         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
877         SEC_DESC_BUF *sdb;
878         uint32 sec_info = DACL_SECURITY_INFORMATION;
879
880         if (argc < 1 || argc > 2) {
881                 printf("Usage: %s [sec_info]\n", argv[0]);
882                 return NT_STATUS_OK;
883         }
884
885         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
886                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
887                                       &pol);
888
889         if (argc == 2) 
890                 sscanf(argv[1], "%x", &sec_info);
891
892         if (!NT_STATUS_IS_OK(result))
893                 goto done;
894
895         result = rpccli_lsa_QuerySecurity(cli, mem_ctx,
896                                           &pol,
897                                           sec_info,
898                                           &sdb);
899         if (!NT_STATUS_IS_OK(result))
900                 goto done;
901
902         /* Print results */
903
904         display_sec_desc(sdb->sd);
905
906         rpccli_lsa_Close(cli, mem_ctx, &pol);
907  done:
908         return result;
909 }
910
911 static void display_trust_dom_info_4(struct lsa_TrustDomainInfoPassword *p, const char *password)
912 {
913         char *pwd, *pwd_old;
914         
915         DATA_BLOB data     = data_blob(NULL, p->password->length);
916         DATA_BLOB data_old = data_blob(NULL, p->old_password->length);
917
918         memcpy(data.data, p->password->data, p->password->length);
919         memcpy(data_old.data, p->old_password->data, p->old_password->length);
920         
921         pwd     = decrypt_trustdom_secret(password, &data);
922         pwd_old = decrypt_trustdom_secret(password, &data_old);
923         
924         d_printf("Password:\t%s\n", pwd);
925         d_printf("Old Password:\t%s\n", pwd_old);
926
927         SAFE_FREE(pwd);
928         SAFE_FREE(pwd_old);
929         
930         data_blob_free(&data);
931         data_blob_free(&data_old);
932 }
933
934 static void display_trust_dom_info(TALLOC_CTX *mem_ctx,
935                                    union lsa_TrustedDomainInfo *info,
936                                    enum lsa_TrustDomInfoEnum info_class,
937                                    const char *pass)
938 {
939         switch (info_class) {
940                 case LSA_TRUSTED_DOMAIN_INFO_PASSWORD:
941                         display_trust_dom_info_4(&info->password, pass);
942                         break;
943                 default: {
944                         const char *str = NULL;
945                         str = NDR_PRINT_UNION_STRING(mem_ctx,
946                                                      lsa_TrustedDomainInfo,
947                                                      info_class, info);
948                         if (str) {
949                                 d_printf("%s\n", str);
950                         }
951                         break;
952                 }
953         }
954 }
955
956 static NTSTATUS cmd_lsa_query_trustdominfobysid(struct rpc_pipe_client *cli,
957                                                 TALLOC_CTX *mem_ctx, int argc, 
958                                                 const char **argv) 
959 {
960         POLICY_HND pol;
961         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
962         DOM_SID dom_sid;
963         uint32 access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
964         union lsa_TrustedDomainInfo info;
965         enum lsa_TrustDomInfoEnum info_class = 1;
966
967         if (argc > 3 || argc < 2) {
968                 printf("Usage: %s [sid] [info_class]\n", argv[0]);
969                 return NT_STATUS_OK;
970         }
971
972         if (!string_to_sid(&dom_sid, argv[1]))
973                 return NT_STATUS_NO_MEMORY;
974
975         if (argc == 3)
976                 info_class = atoi(argv[2]);
977
978         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
979
980         if (!NT_STATUS_IS_OK(result))
981                 goto done;
982
983         result = rpccli_lsa_QueryTrustedDomainInfoBySid(cli, mem_ctx,
984                                                         &pol,
985                                                         &dom_sid,
986                                                         info_class,
987                                                         &info);
988         if (!NT_STATUS_IS_OK(result))
989                 goto done;
990
991         display_trust_dom_info(mem_ctx, &info, info_class, cli->pwd.password);
992
993  done:
994         rpccli_lsa_Close(cli, mem_ctx, &pol);
995
996         return result;
997 }
998
999 static NTSTATUS cmd_lsa_query_trustdominfobyname(struct rpc_pipe_client *cli,
1000                                                  TALLOC_CTX *mem_ctx, int argc,
1001                                                  const char **argv) 
1002 {
1003         POLICY_HND pol;
1004         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1005         uint32 access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
1006         union lsa_TrustedDomainInfo info;
1007         enum lsa_TrustDomInfoEnum info_class = 1;
1008         struct lsa_String trusted_domain;
1009
1010         if (argc > 3 || argc < 2) {
1011                 printf("Usage: %s [name] [info_class]\n", argv[0]);
1012                 return NT_STATUS_OK;
1013         }
1014
1015         if (argc == 3)
1016                 info_class = atoi(argv[2]);
1017
1018         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1019
1020         if (!NT_STATUS_IS_OK(result))
1021                 goto done;
1022
1023         init_lsa_String(&trusted_domain, argv[1]);
1024
1025         result = rpccli_lsa_QueryTrustedDomainInfoByName(cli, mem_ctx,
1026                                                          &pol,
1027                                                          &trusted_domain,
1028                                                          info_class,
1029                                                          &info);
1030         if (!NT_STATUS_IS_OK(result))
1031                 goto done;
1032
1033         display_trust_dom_info(mem_ctx, &info, info_class, cli->pwd.password);
1034
1035  done:
1036         rpccli_lsa_Close(cli, mem_ctx, &pol);
1037
1038         return result;
1039 }
1040
1041 static NTSTATUS cmd_lsa_query_trustdominfo(struct rpc_pipe_client *cli,
1042                                            TALLOC_CTX *mem_ctx, int argc,
1043                                            const char **argv) 
1044 {
1045         POLICY_HND pol, trustdom_pol;
1046         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1047         uint32 access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
1048         union lsa_TrustedDomainInfo info;
1049         DOM_SID dom_sid;
1050         enum lsa_TrustDomInfoEnum info_class = 1;
1051
1052         if (argc > 3 || argc < 2) {
1053                 printf("Usage: %s [sid] [info_class]\n", argv[0]);
1054                 return NT_STATUS_OK;
1055         }
1056
1057         if (!string_to_sid(&dom_sid, argv[1]))
1058                 return NT_STATUS_NO_MEMORY;
1059
1060
1061         if (argc == 3)
1062                 info_class = atoi(argv[2]);
1063
1064         result = rpccli_lsa_open_policy2(cli, mem_ctx, True, access_mask, &pol);
1065
1066         if (!NT_STATUS_IS_OK(result))
1067                 goto done;
1068
1069         result = rpccli_lsa_OpenTrustedDomain(cli, mem_ctx,
1070                                               &pol,
1071                                               &dom_sid,
1072                                               access_mask,
1073                                               &trustdom_pol);
1074
1075         if (!NT_STATUS_IS_OK(result))
1076                 goto done;
1077
1078         result = rpccli_lsa_QueryTrustedDomainInfo(cli, mem_ctx,
1079                                                    &trustdom_pol,
1080                                                    info_class,
1081                                                    &info);
1082
1083         if (!NT_STATUS_IS_OK(result))
1084                 goto done;
1085
1086         display_trust_dom_info(mem_ctx, &info, info_class, cli->pwd.password);
1087
1088  done:
1089         rpccli_lsa_Close(cli, mem_ctx, &pol);
1090
1091         return result;
1092 }
1093
1094 static NTSTATUS cmd_lsa_get_username(struct rpc_pipe_client *cli,
1095                                      TALLOC_CTX *mem_ctx, int argc,
1096                                      const char **argv)
1097 {
1098         POLICY_HND pol;
1099         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1100         const char *servername = cli->cli->desthost;
1101         struct lsa_String *account_name = NULL;
1102         struct lsa_String *authority_name = NULL;
1103
1104         if (argc > 2) {
1105                 printf("Usage: %s servername\n", argv[0]);
1106                 return NT_STATUS_OK;
1107         }
1108
1109         result = rpccli_lsa_open_policy(cli, mem_ctx, true,
1110                                         SEC_RIGHTS_MAXIMUM_ALLOWED,
1111                                         &pol);
1112
1113         if (!NT_STATUS_IS_OK(result)) {
1114                 goto done;
1115         }
1116
1117         result = rpccli_lsa_GetUserName(cli, mem_ctx,
1118                                         servername,
1119                                         &account_name,
1120                                         &authority_name);
1121         if (!NT_STATUS_IS_OK(result)) {
1122                 goto done;
1123         }
1124
1125         /* Print results */
1126
1127         printf("Account Name: %s, Authority Name: %s\n",
1128                 account_name->string, authority_name->string);
1129
1130         rpccli_lsa_Close(cli, mem_ctx, &pol);
1131  done:
1132         return result;
1133 }
1134
1135
1136 /* List of commands exported by this module */
1137
1138 struct cmd_set lsarpc_commands[] = {
1139
1140         { "LSARPC" },
1141
1142         { "lsaquery",            RPC_RTYPE_NTSTATUS, cmd_lsa_query_info_policy,  NULL, PI_LSARPC, NULL, "Query info policy",                    "" },
1143         { "lookupsids",          RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids,        NULL, PI_LSARPC, NULL, "Convert SIDs to names",                "" },
1144         { "lookupnames",         RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names,       NULL, PI_LSARPC, NULL, "Convert names to SIDs",                "" },
1145         { "lookupnames_level",   RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names_level, NULL, PI_LSARPC, NULL, "Convert names to SIDs",                "" },
1146         { "enumtrust",           RPC_RTYPE_NTSTATUS, cmd_lsa_enum_trust_dom,     NULL, PI_LSARPC, NULL, "Enumerate trusted domains",            "Usage: [preferred max number] [enum context (0)]" },
1147         { "enumprivs",           RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privilege,     NULL, PI_LSARPC, NULL, "Enumerate privileges",                 "" },
1148         { "getdispname",         RPC_RTYPE_NTSTATUS, cmd_lsa_get_dispname,       NULL, PI_LSARPC, NULL, "Get the privilege name",               "" },
1149         { "lsaenumsid",          RPC_RTYPE_NTSTATUS, cmd_lsa_enum_sids,          NULL, PI_LSARPC, NULL, "Enumerate the LSA SIDS",               "" },
1150         { "lsacreateaccount",    RPC_RTYPE_NTSTATUS, cmd_lsa_create_account,     NULL, PI_LSARPC, NULL, "Create a new lsa account",   "" },
1151         { "lsaenumprivsaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privsaccounts, NULL, PI_LSARPC, NULL, "Enumerate the privileges of an SID",   "" },
1152         { "lsaenumacctrights",   RPC_RTYPE_NTSTATUS, cmd_lsa_enum_acct_rights,   NULL, PI_LSARPC, NULL, "Enumerate the rights of an SID",   "" },
1153 #if 0
1154         { "lsaaddpriv",          RPC_RTYPE_NTSTATUS, cmd_lsa_add_priv,           NULL, PI_LSARPC, "Assign a privilege to a SID", "" },
1155         { "lsadelpriv",          RPC_RTYPE_NTSTATUS, cmd_lsa_del_priv,           NULL, PI_LSARPC, "Revoke a privilege from a SID", "" },
1156 #endif
1157         { "lsaaddacctrights",    RPC_RTYPE_NTSTATUS, cmd_lsa_add_acct_rights,    NULL, PI_LSARPC, NULL, "Add rights to an account",   "" },
1158         { "lsaremoveacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_remove_acct_rights, NULL, PI_LSARPC, NULL, "Remove rights from an account",   "" },
1159         { "lsalookupprivvalue",  RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_priv_value,  NULL, PI_LSARPC, NULL, "Get a privilege value given its name", "" },
1160         { "lsaquerysecobj",      RPC_RTYPE_NTSTATUS, cmd_lsa_query_secobj,       NULL, PI_LSARPC, NULL, "Query LSA security object", "" },
1161         { "lsaquerytrustdominfo",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfo, NULL, PI_LSARPC, NULL, "Query LSA trusted domains info (given a SID)", "" },
1162         { "lsaquerytrustdominfobyname",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobyname, NULL, PI_LSARPC, NULL, "Query LSA trusted domains info (given a name), only works for Windows > 2k", "" },
1163         { "lsaquerytrustdominfobysid",RPC_RTYPE_NTSTATUS, cmd_lsa_query_trustdominfobysid, NULL, PI_LSARPC, NULL, "Query LSA trusted domains info (given a SID)", "" },
1164         { "getusername",          RPC_RTYPE_NTSTATUS, cmd_lsa_get_username, NULL, PI_LSARPC, NULL, "Get username", "" },
1165
1166         { NULL }
1167 };
1168