ensure that 'available = no' works for [homes]; reported by Walter Haidinger
[Samba.git] / source / rpcclient / cmd_lsarpc.c
blob17180e237f73230288b3eb068a2afdfb394e3177
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Tim Potter 2000
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.
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.
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.
22 #include "includes.h"
23 #include "rpcclient.h"
25 /* Look up domain related information on a remote host */
27 static NTSTATUS cmd_lsa_query_info_policy(struct cli_state *cli,
28 TALLOC_CTX *mem_ctx, int argc,
29 char **argv)
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;
37 if (argc > 2) {
38 printf("Usage: %s [info_class]\n", argv[0]);
39 return NT_STATUS_OK;
42 if (argc == 2)
43 info_class = atoi(argv[1]);
45 result = cli_lsa_open_policy(cli, mem_ctx, True,
46 SEC_RIGHTS_MAXIMUM_ALLOWED,
47 &pol);
49 if (!NT_STATUS_IS_OK(result))
50 goto done;
52 /* Lookup info policy */
54 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
55 domain_name, &dom_sid);
57 if (!NT_STATUS_IS_OK(result))
58 goto done;
60 sid_to_string(sid_str, &dom_sid);
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);
67 done:
68 return result;
71 /* Resolve a list of names to a list of sids */
73 static NTSTATUS cmd_lsa_lookup_names(struct cli_state *cli,
74 TALLOC_CTX *mem_ctx, int argc,
75 char **argv)
77 POLICY_HND pol;
78 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
79 DOM_SID *sids;
80 uint32 *types;
81 int i;
83 if (argc == 1) {
84 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
85 return NT_STATUS_OK;
88 result = cli_lsa_open_policy(cli, mem_ctx, True,
89 SEC_RIGHTS_MAXIMUM_ALLOWED,
90 &pol);
92 if (!NT_STATUS_IS_OK(result))
93 goto done;
95 result = cli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
96 (const char**)(argv + 1), &sids, &types);
98 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
99 NT_STATUS_V(STATUS_SOME_UNMAPPED))
100 goto done;
102 result = NT_STATUS_OK;
104 /* Print results */
106 for (i = 0; i < (argc - 1); i++) {
107 fstring sid_str;
109 sid_to_string(sid_str, &sids[i]);
110 printf("%s %s (%d)\n", argv[i + 1], sid_str,
111 types[i]);
114 done:
115 return result;
118 /* Resolve a list of SIDs to a list of names */
120 static NTSTATUS cmd_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
121 int argc, char **argv)
123 POLICY_HND pol;
124 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
125 DOM_SID *sids;
126 char **domains;
127 char **names;
128 uint32 *types;
129 int i;
131 if (argc == 1) {
132 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
133 return NT_STATUS_OK;
136 result = cli_lsa_open_policy(cli, mem_ctx, True,
137 SEC_RIGHTS_MAXIMUM_ALLOWED,
138 &pol);
140 if (!NT_STATUS_IS_OK(result))
141 goto done;
143 /* Convert arguments to sids */
145 sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * (argc - 1));
147 if (!sids) {
148 printf("could not allocate memory for %d sids\n", argc - 1);
149 goto done;
152 for (i = 0; i < argc - 1; i++)
153 string_to_sid(&sids[i], argv[i + 1]);
155 /* Lookup the SIDs */
157 result = cli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
158 &domains, &names, &types);
160 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
161 NT_STATUS_V(STATUS_SOME_UNMAPPED))
162 goto done;
164 result = NT_STATUS_OK;
166 /* Print results */
168 for (i = 0; i < (argc - 1); i++) {
169 fstring sid_str;
171 sid_to_string(sid_str, &sids[i]);
172 printf("%s %s\\%s (%d)\n", sid_str,
173 domains[i] ? domains[i] : "*unknown*",
174 names[i] ? names[i] : "*unknown*", types[i]);
177 done:
178 return result;
181 /* Enumerate list of trusted domains */
183 static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli,
184 TALLOC_CTX *mem_ctx, int argc,
185 char **argv)
187 POLICY_HND pol;
188 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
189 DOM_SID *domain_sids;
190 char **domain_names;
191 uint32 enum_ctx = 0;
192 uint32 num_domains;
193 int i;
195 if (argc != 1) {
196 printf("Usage: %s\n", argv[0]);
197 return NT_STATUS_OK;
200 result = cli_lsa_open_policy(cli, mem_ctx, True,
201 SEC_RIGHTS_MAXIMUM_ALLOWED,
202 &pol);
204 if (!NT_STATUS_IS_OK(result))
205 goto done;
207 /* Lookup list of trusted domains */
209 result = cli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx,
210 &num_domains, &domain_names,
211 &domain_sids);
213 if (!NT_STATUS_IS_OK(result))
214 goto done;
216 /* Print results */
218 for (i = 0; i < num_domains; i++) {
219 fstring sid_str;
221 sid_to_string(sid_str, &domain_sids[i]);
222 printf("%s %s\n", domain_names[i] ? domain_names[i] :
223 "*unknown*", sid_str);
226 done:
227 return result;
230 /* Enumerates privileges */
232 static NTSTATUS cmd_lsa_enum_privilege(struct cli_state *cli,
233 TALLOC_CTX *mem_ctx, int argc,
234 char **argv)
236 POLICY_HND pol;
237 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
239 uint32 enum_context=0;
240 uint32 pref_max_length=0x1000;
241 uint32 count=0;
242 char **privs_name;
243 uint32 *privs_high;
244 uint32 *privs_low;
245 int i;
247 if (argc > 3) {
248 printf("Usage: %s [enum context] [max length]\n", argv[0]);
249 return NT_STATUS_OK;
252 if (argc>=2)
253 enum_context=atoi(argv[1]);
255 if (argc==3)
256 pref_max_length=atoi(argv[2]);
258 result = cli_lsa_open_policy(cli, mem_ctx, True,
259 SEC_RIGHTS_MAXIMUM_ALLOWED,
260 &pol);
262 if (!NT_STATUS_IS_OK(result))
263 goto done;
265 result = cli_lsa_enum_privilege(cli, mem_ctx, &pol, &enum_context, pref_max_length,
266 &count, &privs_name, &privs_high, &privs_low);
268 if (!NT_STATUS_IS_OK(result))
269 goto done;
271 /* Print results */
272 printf("found %d privileges\n\n", count);
274 for (i = 0; i < count; i++) {
275 printf("%s \t\t%d:%d (0x%x:0x%x)\n", privs_name[i] ? privs_name[i] : "*unknown*",
276 privs_high[i], privs_low[i], privs_high[i], privs_low[i]);
279 done:
280 return result;
283 /* Get privilege name */
285 static NTSTATUS cmd_lsa_get_dispname(struct cli_state *cli,
286 TALLOC_CTX *mem_ctx, int argc,
287 char **argv)
289 POLICY_HND pol;
290 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
292 uint16 lang_id=0;
293 uint16 lang_id_sys=0;
294 uint16 lang_id_desc;
295 fstring description;
297 if (argc != 2) {
298 printf("Usage: %s privilege name\n", argv[0]);
299 return NT_STATUS_OK;
302 result = cli_lsa_open_policy(cli, mem_ctx, True,
303 SEC_RIGHTS_MAXIMUM_ALLOWED,
304 &pol);
306 if (!NT_STATUS_IS_OK(result))
307 goto done;
309 result = cli_lsa_get_dispname(cli, mem_ctx, &pol, argv[1], lang_id, lang_id_sys, description, &lang_id_desc);
311 if (!NT_STATUS_IS_OK(result))
312 goto done;
314 /* Print results */
315 printf("%s -> %s (language: 0x%x)\n", argv[1], description, lang_id_desc);
317 done:
318 return result;
321 /* Enumerate the LSA SIDS */
323 static NTSTATUS cmd_lsa_enum_sids(struct cli_state *cli,
324 TALLOC_CTX *mem_ctx, int argc,
325 char **argv)
327 POLICY_HND pol;
328 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
330 uint32 enum_context=0;
331 uint32 pref_max_length=0x1000;
332 DOM_SID *sids;
333 uint32 count=0;
334 int i;
336 if (argc > 3) {
337 printf("Usage: %s [enum context] [max length]\n", argv[0]);
338 return NT_STATUS_OK;
341 if (argc>=2)
342 enum_context=atoi(argv[1]);
344 if (argc==3)
345 pref_max_length=atoi(argv[2]);
347 result = cli_lsa_open_policy(cli, mem_ctx, True,
348 SEC_RIGHTS_MAXIMUM_ALLOWED,
349 &pol);
351 if (!NT_STATUS_IS_OK(result))
352 goto done;
354 result = cli_lsa_enum_sids(cli, mem_ctx, &pol, &enum_context, pref_max_length,
355 &count, &sids);
357 if (!NT_STATUS_IS_OK(result))
358 goto done;
360 /* Print results */
361 printf("found %d SIDs\n\n", count);
363 for (i = 0; i < count; i++) {
364 fstring sid_str;
366 sid_to_string(sid_str, &sids[i]);
367 printf("%s\n", sid_str);
370 done:
371 return result;
374 /* Enumerate the privileges of an SID */
376 static NTSTATUS cmd_lsa_enum_privsaccounts(struct cli_state *cli,
377 TALLOC_CTX *mem_ctx, int argc,
378 char **argv)
380 POLICY_HND dom_pol;
381 POLICY_HND user_pol;
382 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
383 uint32 access_desired = 0x000f000f;
385 DOM_SID sid;
386 uint32 count=0;
387 LUID_ATTR *set;
388 int i;
390 if (argc != 2 ) {
391 printf("Usage: %s SID\n", argv[0]);
392 return NT_STATUS_OK;
395 string_to_sid(&sid, argv[1]);
397 result = cli_lsa_open_policy2(cli, mem_ctx, True,
398 SEC_RIGHTS_MAXIMUM_ALLOWED,
399 &dom_pol);
401 if (!NT_STATUS_IS_OK(result))
402 goto done;
404 result = cli_lsa_open_account(cli, mem_ctx, &dom_pol, &sid, access_desired, &user_pol);
406 if (!NT_STATUS_IS_OK(result))
407 goto done;
409 result = cli_lsa_enum_privsaccount(cli, mem_ctx, &user_pol, &count, &set);
411 if (!NT_STATUS_IS_OK(result))
412 goto done;
414 /* Print results */
415 printf("found %d privileges for SID %s\n\n", count, argv[1]);
416 printf("high\tlow\tattribute\n");
418 for (i = 0; i < count; i++) {
419 printf("%u\t%u\t%u\n", set[i].luid.high, set[i].luid.low, set[i].attr);
422 done:
423 return result;
426 /* Get a privilege value given its name */
428 static NTSTATUS cmd_lsa_lookupprivvalue(struct cli_state *cli,
429 TALLOC_CTX *mem_ctx, int argc,
430 char **argv)
432 POLICY_HND pol;
433 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
434 LUID luid;
436 if (argc != 2 ) {
437 printf("Usage: %s name\n", argv[0]);
438 return NT_STATUS_OK;
441 result = cli_lsa_open_policy2(cli, mem_ctx, True,
442 SEC_RIGHTS_MAXIMUM_ALLOWED,
443 &pol);
445 if (!NT_STATUS_IS_OK(result))
446 goto done;
448 result = cli_lsa_lookupprivvalue(cli, mem_ctx, &pol, argv[1], &luid);
450 if (!NT_STATUS_IS_OK(result))
451 goto done;
453 /* Print results */
455 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
457 done:
458 return result;
461 /* Query LSA security object */
463 static NTSTATUS cmd_lsa_query_secobj(struct cli_state *cli,
464 TALLOC_CTX *mem_ctx, int argc,
465 char **argv)
467 POLICY_HND pol;
468 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
469 SEC_DESC_BUF *sdb;
470 uint32 sec_info = 0x00000004; /* ??? */
472 if (argc != 1 ) {
473 printf("Usage: %s\n", argv[0]);
474 return NT_STATUS_OK;
477 result = cli_lsa_open_policy2(cli, mem_ctx, True,
478 SEC_RIGHTS_MAXIMUM_ALLOWED,
479 &pol);
481 if (!NT_STATUS_IS_OK(result))
482 goto done;
484 result = cli_lsa_query_secobj(cli, mem_ctx, &pol, sec_info, &sdb);
486 if (!NT_STATUS_IS_OK(result))
487 goto done;
489 /* Print results */
491 display_sec_desc(sdb->sec);
493 done:
494 return result;
497 /* List of commands exported by this module */
499 struct cmd_set lsarpc_commands[] = {
501 { "LSARPC" },
503 { "lsaquery", cmd_lsa_query_info_policy, PIPE_LSARPC, "Query info policy", "" },
504 { "lookupsids", cmd_lsa_lookup_sids, PIPE_LSARPC, "Convert SIDs to names", "" },
505 { "lookupnames", cmd_lsa_lookup_names, PIPE_LSARPC, "Convert names to SIDs", "" },
506 { "enumtrust", cmd_lsa_enum_trust_dom, PIPE_LSARPC, "Enumerate trusted domains", "" },
507 { "enumprivs", cmd_lsa_enum_privilege, PIPE_LSARPC, "Enumerate privileges", "" },
508 { "getdispname", cmd_lsa_get_dispname, PIPE_LSARPC, "Get the privilege name", "" },
509 { "lsaenumsid", cmd_lsa_enum_sids, PIPE_LSARPC, "Enumerate the LSA SIDS", "" },
510 { "lsaenumprivsaccount", cmd_lsa_enum_privsaccounts, PIPE_LSARPC, "Enumerate the privileges of an SID", "" },
511 { "lsalookupprivvalue", cmd_lsa_lookupprivvalue, PIPE_LSARPC, "Get a privilege value given its name", "" },
512 { "lsaquerysecobj", cmd_lsa_query_secobj, PIPE_LSARPC, "Query LSA security object", "" },
514 { NULL }