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