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