A few more trusted domains updates from mimir.
[tprouty/samba.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "rpcclient.h"
25
26 /* Look up domain related information on a remote host */
27
28 static NTSTATUS cmd_lsa_query_info_policy(struct cli_state *cli, 
29                                           TALLOC_CTX *mem_ctx, int argc, 
30                                           char **argv) 
31 {
32         POLICY_HND pol;
33         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
34         DOM_SID dom_sid;
35         fstring sid_str, domain_name;
36         uint32 info_class = 3;
37
38         if (argc > 2) {
39                 printf("Usage: %s [info_class]\n", argv[0]);
40                 return NT_STATUS_OK;
41         }
42
43         if (argc == 2)
44                 info_class = atoi(argv[1]);
45         
46         result = cli_lsa_open_policy(cli, mem_ctx, True, 
47                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
48                                      &pol);
49
50         if (!NT_STATUS_IS_OK(result))
51                 goto done;
52
53         /* Lookup info policy */
54
55         result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class, 
56                                            domain_name, &dom_sid);
57
58         if (!NT_STATUS_IS_OK(result))
59                 goto done;
60
61         sid_to_string(sid_str, &dom_sid);
62
63         if (domain_name[0])
64                 printf("domain %s has sid %s\n", domain_name, sid_str);
65         else
66                 printf("could not query info for level %d\n", info_class);
67
68  done:
69         return result;
70 }
71
72 /* Resolve a list of names to a list of sids */
73
74 static NTSTATUS cmd_lsa_lookup_names(struct cli_state *cli, 
75                                      TALLOC_CTX *mem_ctx, int argc, 
76                                      char **argv)
77 {
78         POLICY_HND pol;
79         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
80         DOM_SID *sids;
81         uint32 *types;
82         int i;
83
84         if (argc == 1) {
85                 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
86                 return NT_STATUS_OK;
87         }
88
89         result = cli_lsa_open_policy(cli, mem_ctx, True, 
90                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
91                                      &pol);
92
93         if (!NT_STATUS_IS_OK(result))
94                 goto done;
95
96         result = cli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1, 
97                                       (const char**)(argv + 1), &sids, &types);
98
99         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) != 
100             NT_STATUS_V(STATUS_SOME_UNMAPPED))
101                 goto done;
102
103         result = NT_STATUS_OK;
104
105         /* Print results */
106
107         for (i = 0; i < (argc - 1); i++) {
108                 fstring sid_str;
109
110                 sid_to_string(sid_str, &sids[i]);
111                 printf("%s %s (%d)\n", argv[i + 1], sid_str,
112                        types[i]);
113         }
114
115  done:
116         return result;
117 }
118
119 /* Resolve a list of SIDs to a list of names */
120
121 static NTSTATUS cmd_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
122                                     int argc, char **argv)
123 {
124         POLICY_HND pol;
125         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
126         DOM_SID *sids;
127         char **domains;
128         char **names;
129         uint32 *types;
130         int i;
131
132         if (argc == 1) {
133                 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
134                 return NT_STATUS_OK;
135         }
136
137         result = cli_lsa_open_policy(cli, mem_ctx, True, 
138                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
139                                      &pol);
140
141         if (!NT_STATUS_IS_OK(result))
142                 goto done;
143
144         /* Convert arguments to sids */
145
146         sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * (argc - 1));
147
148         if (!sids) {
149                 printf("could not allocate memory for %d sids\n", argc - 1);
150                 goto done;
151         }
152
153         for (i = 0; i < argc - 1; i++)
154                 string_to_sid(&sids[i], argv[i + 1]);
155
156         /* Lookup the SIDs */
157
158         result = cli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids, 
159                                      &domains, &names, &types);
160
161         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) != 
162             NT_STATUS_V(STATUS_SOME_UNMAPPED))
163                 goto done;
164
165         result = NT_STATUS_OK;
166
167         /* Print results */
168
169         for (i = 0; i < (argc - 1); i++) {
170                 fstring sid_str;
171
172                 sid_to_string(sid_str, &sids[i]);
173                 printf("%s %s\\%s (%d)\n", sid_str, 
174                        domains[i] ? domains[i] : "*unknown*", 
175                        names[i] ? names[i] : "*unknown*", types[i]);
176         }
177
178  done:
179         return result;
180 }
181
182 /* Enumerate list of trusted domains */
183
184 static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli, 
185                                        TALLOC_CTX *mem_ctx, int argc, 
186                                        char **argv)
187 {
188         POLICY_HND pol;
189         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
190         DOM_SID *domain_sids;
191         char **domain_names;
192
193         /* defaults, but may be changed using params */
194         uint32 enum_ctx = 0;
195         uint32 preferred_maxnum = 5;
196         uint32 num_domains = 0;
197         int i;
198
199         if (argc > 3) {
200                 printf("Usage: %s [preferred max number (%d)] [enum context (0)]\n",
201                         argv[0], preferred_maxnum);
202                 return NT_STATUS_OK;
203         }
204
205         /* enumeration context */
206         if (argc >= 2 && argv[1]) {
207                 preferred_maxnum = atoi(argv[1]);
208         }       
209
210         /* preferred maximum number */
211         if (argc == 3 && argv[2]) {
212                 enum_ctx = atoi(argv[2]);
213         }       
214
215         result = cli_lsa_open_policy(cli, mem_ctx, True, 
216                                      POLICY_VIEW_LOCAL_INFORMATION,
217                                      &pol);
218
219         if (!NT_STATUS_IS_OK(result))
220                 goto done;
221
222         /* Lookup list of trusted domains */
223
224         result = cli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx,
225                                                 &preferred_maxnum, &num_domains,
226                                                 &domain_names, &domain_sids);
227         if (!NT_STATUS_IS_OK(result) &&
228             !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
229             !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
230             goto done;
231
232         /* Print results: list of names and sids returned in this response. */   
233         for (i = 0; i < num_domains; i++) {
234                 fstring sid_str;
235
236                 sid_to_string(sid_str, &domain_sids[i]);
237                 printf("%s %s\n", domain_names[i] ? domain_names[i] : 
238                        "*unknown*", sid_str);
239         }
240
241  done:
242         return result;
243 }
244
245 /* Enumerates privileges */
246
247 static NTSTATUS cmd_lsa_enum_privilege(struct cli_state *cli, 
248                                           TALLOC_CTX *mem_ctx, int argc, 
249                                           char **argv) 
250 {
251         POLICY_HND pol;
252         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
253
254         uint32 enum_context=0;
255         uint32 pref_max_length=0x1000;
256         uint32 count=0;
257         char   **privs_name;
258         uint32 *privs_high;
259         uint32 *privs_low;
260         int i;
261
262         if (argc > 3) {
263                 printf("Usage: %s [enum context] [max length]\n", argv[0]);
264                 return NT_STATUS_OK;
265         }
266
267         if (argc>=2)
268                 enum_context=atoi(argv[1]);
269
270         if (argc==3)
271                 pref_max_length=atoi(argv[2]);
272
273         result = cli_lsa_open_policy(cli, mem_ctx, True, 
274                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
275                                      &pol);
276
277         if (!NT_STATUS_IS_OK(result))
278                 goto done;
279
280         result = cli_lsa_enum_privilege(cli, mem_ctx, &pol, &enum_context, pref_max_length,
281                                         &count, &privs_name, &privs_high, &privs_low);
282
283         if (!NT_STATUS_IS_OK(result))
284                 goto done;
285
286         /* Print results */
287         printf("found %d privileges\n\n", count);
288
289         for (i = 0; i < count; i++) {
290                 printf("%s \t\t%d:%d (0x%x:0x%x)\n", privs_name[i] ? privs_name[i] : "*unknown*",
291                        privs_high[i], privs_low[i], privs_high[i], privs_low[i]);
292         }
293
294  done:
295         return result;
296 }
297
298 /* Get privilege name */
299
300 static NTSTATUS cmd_lsa_get_dispname(struct cli_state *cli, 
301                                      TALLOC_CTX *mem_ctx, int argc, 
302                                      char **argv) 
303 {
304         POLICY_HND pol;
305         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
306
307         uint16 lang_id=0;
308         uint16 lang_id_sys=0;
309         uint16 lang_id_desc;
310         fstring description;
311
312         if (argc != 2) {
313                 printf("Usage: %s privilege name\n", argv[0]);
314                 return NT_STATUS_OK;
315         }
316
317         result = cli_lsa_open_policy(cli, mem_ctx, True, 
318                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
319                                      &pol);
320
321         if (!NT_STATUS_IS_OK(result))
322                 goto done;
323
324         result = cli_lsa_get_dispname(cli, mem_ctx, &pol, argv[1], lang_id, lang_id_sys, description, &lang_id_desc);
325
326         if (!NT_STATUS_IS_OK(result))
327                 goto done;
328
329         /* Print results */
330         printf("%s -> %s (language: 0x%x)\n", argv[1], description, lang_id_desc);
331
332  done:
333         return result;
334 }
335
336 /* Enumerate the LSA SIDS */
337
338 static NTSTATUS cmd_lsa_enum_sids(struct cli_state *cli, 
339                                      TALLOC_CTX *mem_ctx, int argc, 
340                                      char **argv) 
341 {
342         POLICY_HND pol;
343         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
344
345         uint32 enum_context=0;
346         uint32 pref_max_length=0x1000;
347         DOM_SID *sids;
348         uint32 count=0;
349         int i;
350
351         if (argc > 3) {
352                 printf("Usage: %s [enum context] [max length]\n", argv[0]);
353                 return NT_STATUS_OK;
354         }
355
356         if (argc>=2)
357                 enum_context=atoi(argv[1]);
358
359         if (argc==3)
360                 pref_max_length=atoi(argv[2]);
361
362         result = cli_lsa_open_policy(cli, mem_ctx, True, 
363                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
364                                      &pol);
365
366         if (!NT_STATUS_IS_OK(result))
367                 goto done;
368
369         result = cli_lsa_enum_sids(cli, mem_ctx, &pol, &enum_context, pref_max_length,
370                                         &count, &sids);
371
372         if (!NT_STATUS_IS_OK(result))
373                 goto done;
374
375         /* Print results */
376         printf("found %d SIDs\n\n", count);
377
378         for (i = 0; i < count; i++) {
379                 fstring sid_str;
380
381                 sid_to_string(sid_str, &sids[i]);
382                 printf("%s\n", sid_str);
383         }
384
385  done:
386         return result;
387 }
388
389 /* Enumerate the privileges of an SID */
390
391 static NTSTATUS cmd_lsa_enum_privsaccounts(struct cli_state *cli, 
392                                            TALLOC_CTX *mem_ctx, int argc, 
393                                            char **argv) 
394 {
395         POLICY_HND dom_pol;
396         POLICY_HND user_pol;
397         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
398         uint32 access_desired = 0x000f000f;
399         
400         DOM_SID sid;
401         uint32 count=0;
402         LUID_ATTR *set;
403         int i;
404
405         if (argc != 2 ) {
406                 printf("Usage: %s SID\n", argv[0]);
407                 return NT_STATUS_OK;
408         }
409
410         string_to_sid(&sid, argv[1]);
411
412         result = cli_lsa_open_policy2(cli, mem_ctx, True, 
413                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
414                                      &dom_pol);
415
416         if (!NT_STATUS_IS_OK(result))
417                 goto done;
418
419         result = cli_lsa_open_account(cli, mem_ctx, &dom_pol, &sid, access_desired, &user_pol);
420
421         if (!NT_STATUS_IS_OK(result))
422                 goto done;
423
424         result = cli_lsa_enum_privsaccount(cli, mem_ctx, &user_pol, &count, &set);
425
426         if (!NT_STATUS_IS_OK(result))
427                 goto done;
428
429         /* Print results */
430         printf("found %d privileges for SID %s\n\n", count, argv[1]);
431         printf("high\tlow\tattribute\n");
432
433         for (i = 0; i < count; i++) {
434                 printf("%u\t%u\t%u\n", set[i].luid.high, set[i].luid.low, set[i].attr);
435         }
436
437  done:
438         return result;
439 }
440
441 /* Get a privilege value given its name */
442
443 static NTSTATUS cmd_lsa_lookupprivvalue(struct cli_state *cli, 
444                                            TALLOC_CTX *mem_ctx, int argc, 
445                                            char **argv) 
446 {
447         POLICY_HND pol;
448         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
449         LUID luid;
450
451         if (argc != 2 ) {
452                 printf("Usage: %s name\n", argv[0]);
453                 return NT_STATUS_OK;
454         }
455
456         result = cli_lsa_open_policy2(cli, mem_ctx, True, 
457                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
458                                      &pol);
459
460         if (!NT_STATUS_IS_OK(result))
461                 goto done;
462
463         result = cli_lsa_lookupprivvalue(cli, mem_ctx, &pol, argv[1], &luid);
464
465         if (!NT_STATUS_IS_OK(result))
466                 goto done;
467
468         /* Print results */
469
470         printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
471
472  done:
473         return result;
474 }
475
476 /* Query LSA security object */
477
478 static NTSTATUS cmd_lsa_query_secobj(struct cli_state *cli, 
479                                      TALLOC_CTX *mem_ctx, int argc, 
480                                      char **argv) 
481 {
482         POLICY_HND pol;
483         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
484         SEC_DESC_BUF *sdb;
485         uint32 sec_info = 0x00000004; /* ??? */
486
487         if (argc != 1 ) {
488                 printf("Usage: %s\n", argv[0]);
489                 return NT_STATUS_OK;
490         }
491
492         result = cli_lsa_open_policy2(cli, mem_ctx, True, 
493                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
494                                       &pol);
495
496         if (!NT_STATUS_IS_OK(result))
497                 goto done;
498
499         result = cli_lsa_query_secobj(cli, mem_ctx, &pol, sec_info, &sdb);
500
501         if (!NT_STATUS_IS_OK(result))
502                 goto done;
503
504         /* Print results */
505
506         display_sec_desc(sdb->sec);
507
508  done:
509         return result;
510 }
511
512 /* List of commands exported by this module */
513
514 struct cmd_set lsarpc_commands[] = {
515
516         { "LSARPC" },
517
518         { "lsaquery",            cmd_lsa_query_info_policy,  PIPE_LSARPC, "Query info policy",                    "" },
519         { "lookupsids",          cmd_lsa_lookup_sids,        PIPE_LSARPC, "Convert SIDs to names",                "" },
520         { "lookupnames",         cmd_lsa_lookup_names,       PIPE_LSARPC, "Convert names to SIDs",                "" },
521         { "enumtrust",           cmd_lsa_enum_trust_dom,     PIPE_LSARPC, "Enumerate trusted domains",            "Usage: [preferred max number] [enum context (0)]" },
522         { "enumprivs",           cmd_lsa_enum_privilege,     PIPE_LSARPC, "Enumerate privileges",                 "" },
523         { "getdispname",         cmd_lsa_get_dispname,       PIPE_LSARPC, "Get the privilege name",               "" },
524         { "lsaenumsid",          cmd_lsa_enum_sids,          PIPE_LSARPC, "Enumerate the LSA SIDS",               "" },
525         { "lsaenumprivsaccount", cmd_lsa_enum_privsaccounts, PIPE_LSARPC, "Enumerate the privileges of an SID",   "" },
526         { "lsalookupprivvalue",  cmd_lsa_lookupprivvalue,    PIPE_LSARPC, "Get a privilege value given its name", "" },
527         { "lsaquerysecobj",      cmd_lsa_query_secobj,       PIPE_LSARPC, "Query LSA security object", "" },
528
529         { NULL }
530 };