cleaned up the lsa_enum_acct_rights function and added a
[Samba.git] / source / rpcclient / cmd_lsarpc.c
blob991e56fece4f57e390c4c339b9a08a04d1dd1746
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Tim Potter 2000
6 Copyright (C) Rafal Szczesniak 2002
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.
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.
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.
23 #include "includes.h"
24 #include "rpcclient.h"
27 /* useful function to allow entering a name instead of a SID and
28 * looking it up automatically */
29 static NTSTATUS name_to_sid(struct cli_state *cli,
30 TALLOC_CTX *mem_ctx,
31 DOM_SID *sid, const char *name)
33 POLICY_HND pol;
34 uint32 *sid_types;
35 NTSTATUS result;
36 DOM_SID *sids;
38 /* maybe its a raw SID */
39 if (strncmp(name, "S-", 2) == 0 &&
40 string_to_sid(sid, name)) {
41 return NT_STATUS_OK;
44 result = cli_lsa_open_policy(cli, mem_ctx, True,
45 SEC_RIGHTS_MAXIMUM_ALLOWED,
46 &pol);
47 if (!NT_STATUS_IS_OK(result))
48 goto done;
50 result = cli_lsa_lookup_names(cli, mem_ctx, &pol, 1, &name, &sids, &sid_types);
51 if (!NT_STATUS_IS_OK(result))
52 goto done;
54 cli_lsa_close(cli, mem_ctx, &pol);
56 *sid = sids[0];
58 done:
59 return result;
63 /* Look up domain related information on a remote host */
65 static NTSTATUS cmd_lsa_query_info_policy(struct cli_state *cli,
66 TALLOC_CTX *mem_ctx, int argc,
67 char **argv)
69 POLICY_HND pol;
70 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
71 DOM_SID dom_sid;
72 GUID dom_guid;
73 fstring sid_str, domain_name="", dns_name="", forest_name="";
74 uint32 info_class = 3;
76 if (argc > 2) {
77 printf("Usage: %s [info_class]\n", argv[0]);
78 return NT_STATUS_OK;
81 if (argc == 2)
82 info_class = atoi(argv[1]);
84 /* Lookup info policy */
85 switch (info_class) {
86 case 12:
87 result = cli_lsa_open_policy2(cli, mem_ctx, True,
88 SEC_RIGHTS_MAXIMUM_ALLOWED,
89 &pol);
91 if (!NT_STATUS_IS_OK(result))
92 goto done;
93 result = cli_lsa_query_info_policy2(cli, mem_ctx, &pol,
94 info_class, domain_name,
95 dns_name, forest_name,
96 &dom_guid, &dom_sid);
97 break;
98 default:
99 result = cli_lsa_open_policy(cli, mem_ctx, True,
100 SEC_RIGHTS_MAXIMUM_ALLOWED,
101 &pol);
103 if (!NT_STATUS_IS_OK(result))
104 goto done;
105 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol,
106 info_class, domain_name,
107 &dom_sid);
110 if (!NT_STATUS_IS_OK(result))
111 goto done;
113 sid_to_string(sid_str, &dom_sid);
115 if (domain_name[0])
116 printf("domain %s has sid %s\n", domain_name, sid_str);
117 else
118 printf("could not query info for level %d\n", info_class);
120 if (dns_name[0])
121 printf("domain dns name is %s\n", dns_name);
122 if (forest_name[0])
123 printf("forest name is %s\n", forest_name);
125 if (info_class == 12) {
126 printf("domain GUID is ");
127 print_guid(&dom_guid);
129 done:
130 return result;
133 /* Resolve a list of names to a list of sids */
135 static NTSTATUS cmd_lsa_lookup_names(struct cli_state *cli,
136 TALLOC_CTX *mem_ctx, int argc,
137 char **argv)
139 POLICY_HND pol;
140 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
141 DOM_SID *sids;
142 uint32 *types;
143 int i;
145 if (argc == 1) {
146 printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
147 return NT_STATUS_OK;
150 result = cli_lsa_open_policy(cli, mem_ctx, True,
151 SEC_RIGHTS_MAXIMUM_ALLOWED,
152 &pol);
154 if (!NT_STATUS_IS_OK(result))
155 goto done;
157 result = cli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1,
158 (const char**)(argv + 1), &sids, &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;
170 sid_to_string(sid_str, &sids[i]);
171 printf("%s %s (%s: %d)\n", argv[i + 1], sid_str,
172 sid_type_lookup(types[i]), types[i]);
175 done:
176 return result;
179 /* Resolve a list of SIDs to a list of names */
181 static NTSTATUS cmd_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
182 int argc, char **argv)
184 POLICY_HND pol;
185 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
186 DOM_SID *sids;
187 char **domains;
188 char **names;
189 uint32 *types;
190 int i;
192 if (argc == 1) {
193 printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
194 return NT_STATUS_OK;
197 result = cli_lsa_open_policy(cli, mem_ctx, True,
198 SEC_RIGHTS_MAXIMUM_ALLOWED,
199 &pol);
201 if (!NT_STATUS_IS_OK(result))
202 goto done;
204 /* Convert arguments to sids */
206 sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * (argc - 1));
208 if (!sids) {
209 printf("could not allocate memory for %d sids\n", argc - 1);
210 goto done;
213 for (i = 0; i < argc - 1; i++)
214 string_to_sid(&sids[i], argv[i + 1]);
216 /* Lookup the SIDs */
218 result = cli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
219 &domains, &names, &types);
221 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
222 NT_STATUS_V(STATUS_SOME_UNMAPPED))
223 goto done;
225 result = NT_STATUS_OK;
227 /* Print results */
229 for (i = 0; i < (argc - 1); i++) {
230 fstring sid_str;
232 sid_to_string(sid_str, &sids[i]);
233 printf("%s %s\\%s (%d)\n", sid_str,
234 domains[i] ? domains[i] : "*unknown*",
235 names[i] ? names[i] : "*unknown*", types[i]);
238 done:
239 return result;
242 /* Enumerate list of trusted domains */
244 static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli,
245 TALLOC_CTX *mem_ctx, int argc,
246 char **argv)
248 POLICY_HND pol;
249 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
250 DOM_SID *domain_sids;
251 char **domain_names;
253 /* defaults, but may be changed using params */
254 uint32 enum_ctx = 0;
255 uint32 num_domains = 0;
256 int i;
258 if (argc > 2) {
259 printf("Usage: %s [enum context (0)]\n", argv[0]);
260 return NT_STATUS_OK;
263 if (argc == 2 && argv[1]) {
264 enum_ctx = atoi(argv[2]);
267 result = cli_lsa_open_policy(cli, mem_ctx, True,
268 POLICY_VIEW_LOCAL_INFORMATION,
269 &pol);
271 if (!NT_STATUS_IS_OK(result))
272 goto done;
274 /* Lookup list of trusted domains */
276 result = cli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx,
277 &num_domains,
278 &domain_names, &domain_sids);
279 if (!NT_STATUS_IS_OK(result) &&
280 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
281 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
282 goto done;
284 /* Print results: list of names and sids returned in this response. */
285 for (i = 0; i < num_domains; i++) {
286 fstring sid_str;
288 sid_to_string(sid_str, &domain_sids[i]);
289 printf("%s %s\n", domain_names[i] ? domain_names[i] :
290 "*unknown*", sid_str);
293 done:
294 return result;
297 /* Enumerates privileges */
299 static NTSTATUS cmd_lsa_enum_privilege(struct cli_state *cli,
300 TALLOC_CTX *mem_ctx, int argc,
301 char **argv)
303 POLICY_HND pol;
304 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
306 uint32 enum_context=0;
307 uint32 pref_max_length=0x1000;
308 uint32 count=0;
309 char **privs_name;
310 uint32 *privs_high;
311 uint32 *privs_low;
312 int i;
314 if (argc > 3) {
315 printf("Usage: %s [enum context] [max length]\n", argv[0]);
316 return NT_STATUS_OK;
319 if (argc>=2)
320 enum_context=atoi(argv[1]);
322 if (argc==3)
323 pref_max_length=atoi(argv[2]);
325 result = cli_lsa_open_policy(cli, mem_ctx, True,
326 SEC_RIGHTS_MAXIMUM_ALLOWED,
327 &pol);
329 if (!NT_STATUS_IS_OK(result))
330 goto done;
332 result = cli_lsa_enum_privilege(cli, mem_ctx, &pol, &enum_context, pref_max_length,
333 &count, &privs_name, &privs_high, &privs_low);
335 if (!NT_STATUS_IS_OK(result))
336 goto done;
338 /* Print results */
339 printf("found %d privileges\n\n", count);
341 for (i = 0; i < count; i++) {
342 printf("%s \t\t%d:%d (0x%x:0x%x)\n", privs_name[i] ? privs_name[i] : "*unknown*",
343 privs_high[i], privs_low[i], privs_high[i], privs_low[i]);
346 done:
347 return result;
350 /* Get privilege name */
352 static NTSTATUS cmd_lsa_get_dispname(struct cli_state *cli,
353 TALLOC_CTX *mem_ctx, int argc,
354 char **argv)
356 POLICY_HND pol;
357 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
359 uint16 lang_id=0;
360 uint16 lang_id_sys=0;
361 uint16 lang_id_desc;
362 fstring description;
364 if (argc != 2) {
365 printf("Usage: %s privilege name\n", argv[0]);
366 return NT_STATUS_OK;
369 result = cli_lsa_open_policy(cli, mem_ctx, True,
370 SEC_RIGHTS_MAXIMUM_ALLOWED,
371 &pol);
373 if (!NT_STATUS_IS_OK(result))
374 goto done;
376 result = cli_lsa_get_dispname(cli, mem_ctx, &pol, argv[1], lang_id, lang_id_sys, description, &lang_id_desc);
378 if (!NT_STATUS_IS_OK(result))
379 goto done;
381 /* Print results */
382 printf("%s -> %s (language: 0x%x)\n", argv[1], description, lang_id_desc);
384 done:
385 return result;
388 /* Enumerate the LSA SIDS */
390 static NTSTATUS cmd_lsa_enum_sids(struct cli_state *cli,
391 TALLOC_CTX *mem_ctx, int argc,
392 char **argv)
394 POLICY_HND pol;
395 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
397 uint32 enum_context=0;
398 uint32 pref_max_length=0x1000;
399 DOM_SID *sids;
400 uint32 count=0;
401 int i;
403 if (argc > 3) {
404 printf("Usage: %s [enum context] [max length]\n", argv[0]);
405 return NT_STATUS_OK;
408 if (argc>=2)
409 enum_context=atoi(argv[1]);
411 if (argc==3)
412 pref_max_length=atoi(argv[2]);
414 result = cli_lsa_open_policy(cli, mem_ctx, True,
415 SEC_RIGHTS_MAXIMUM_ALLOWED,
416 &pol);
418 if (!NT_STATUS_IS_OK(result))
419 goto done;
421 result = cli_lsa_enum_sids(cli, mem_ctx, &pol, &enum_context, pref_max_length,
422 &count, &sids);
424 if (!NT_STATUS_IS_OK(result))
425 goto done;
427 /* Print results */
428 printf("found %d SIDs\n\n", count);
430 for (i = 0; i < count; i++) {
431 fstring sid_str;
433 sid_to_string(sid_str, &sids[i]);
434 printf("%s\n", sid_str);
437 done:
438 return result;
441 /* Enumerate the privileges of an SID */
443 static NTSTATUS cmd_lsa_enum_privsaccounts(struct cli_state *cli,
444 TALLOC_CTX *mem_ctx, int argc,
445 char **argv)
447 POLICY_HND dom_pol;
448 POLICY_HND user_pol;
449 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
450 uint32 access_desired = 0x000f000f;
452 DOM_SID sid;
453 uint32 count=0;
454 LUID_ATTR *set;
455 int i;
457 if (argc != 2 ) {
458 printf("Usage: %s SID\n", argv[0]);
459 return NT_STATUS_OK;
462 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
463 if (!NT_STATUS_IS_OK(result))
464 goto done;
466 result = cli_lsa_open_policy2(cli, mem_ctx, True,
467 SEC_RIGHTS_MAXIMUM_ALLOWED,
468 &dom_pol);
470 if (!NT_STATUS_IS_OK(result))
471 goto done;
473 result = cli_lsa_open_account(cli, mem_ctx, &dom_pol, &sid, access_desired, &user_pol);
475 if (!NT_STATUS_IS_OK(result))
476 goto done;
478 result = cli_lsa_enum_privsaccount(cli, mem_ctx, &user_pol, &count, &set);
480 if (!NT_STATUS_IS_OK(result))
481 goto done;
483 /* Print results */
484 printf("found %d privileges for SID %s\n\n", count, argv[1]);
485 printf("high\tlow\tattribute\n");
487 for (i = 0; i < count; i++) {
488 printf("%u\t%u\t%u\n", set[i].luid.high, set[i].luid.low, set[i].attr);
491 done:
492 return result;
496 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
498 static NTSTATUS cmd_lsa_enum_acct_rights(struct cli_state *cli,
499 TALLOC_CTX *mem_ctx, int argc,
500 char **argv)
502 POLICY_HND dom_pol;
503 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
505 DOM_SID sid;
506 uint32 count;
507 char **rights;
509 int i;
511 if (argc != 2 ) {
512 printf("Usage: %s SID\n", argv[0]);
513 return NT_STATUS_OK;
516 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
517 if (!NT_STATUS_IS_OK(result))
518 goto done;
520 result = cli_lsa_open_policy2(cli, mem_ctx, True,
521 SEC_RIGHTS_MAXIMUM_ALLOWED,
522 &dom_pol);
524 if (!NT_STATUS_IS_OK(result))
525 goto done;
527 result = cli_lsa_enum_account_rights(cli, mem_ctx, &dom_pol, sid, &count, &rights);
529 if (!NT_STATUS_IS_OK(result))
530 goto done;
532 printf("found %d privileges for SID %s\n", count, sid_string_static(&sid));
534 for (i = 0; i < count; i++) {
535 printf("\t%s\n", rights[i]);
538 done:
539 return result;
543 /* add some privileges to a SID via LsaAddAccountRights */
545 static NTSTATUS cmd_lsa_add_acct_rights(struct cli_state *cli,
546 TALLOC_CTX *mem_ctx, int argc,
547 const char **argv)
549 POLICY_HND dom_pol;
550 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
552 DOM_SID sid;
554 if (argc < 3 ) {
555 printf("Usage: %s SID [rights...]\n", argv[0]);
556 return NT_STATUS_OK;
559 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
560 if (!NT_STATUS_IS_OK(result))
561 goto done;
563 result = cli_lsa_open_policy2(cli, mem_ctx, True,
564 SEC_RIGHTS_MAXIMUM_ALLOWED,
565 &dom_pol);
567 if (!NT_STATUS_IS_OK(result))
568 goto done;
570 result = cli_lsa_add_account_rights(cli, mem_ctx, &dom_pol, sid,
571 argc-2, argv+2);
573 if (!NT_STATUS_IS_OK(result))
574 goto done;
576 done:
577 return result;
581 /* Get a privilege value given its name */
583 static NTSTATUS cmd_lsa_lookupprivvalue(struct cli_state *cli,
584 TALLOC_CTX *mem_ctx, int argc,
585 char **argv)
587 POLICY_HND pol;
588 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
589 LUID luid;
591 if (argc != 2 ) {
592 printf("Usage: %s name\n", argv[0]);
593 return NT_STATUS_OK;
596 result = cli_lsa_open_policy2(cli, mem_ctx, True,
597 SEC_RIGHTS_MAXIMUM_ALLOWED,
598 &pol);
600 if (!NT_STATUS_IS_OK(result))
601 goto done;
603 result = cli_lsa_lookupprivvalue(cli, mem_ctx, &pol, argv[1], &luid);
605 if (!NT_STATUS_IS_OK(result))
606 goto done;
608 /* Print results */
610 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
612 done:
613 return result;
616 /* Query LSA security object */
618 static NTSTATUS cmd_lsa_query_secobj(struct cli_state *cli,
619 TALLOC_CTX *mem_ctx, int argc,
620 char **argv)
622 POLICY_HND pol;
623 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
624 SEC_DESC_BUF *sdb;
625 uint32 sec_info = 0x00000004; /* ??? */
627 if (argc != 1 ) {
628 printf("Usage: %s\n", argv[0]);
629 return NT_STATUS_OK;
632 result = cli_lsa_open_policy2(cli, mem_ctx, True,
633 SEC_RIGHTS_MAXIMUM_ALLOWED,
634 &pol);
636 if (!NT_STATUS_IS_OK(result))
637 goto done;
639 result = cli_lsa_query_secobj(cli, mem_ctx, &pol, sec_info, &sdb);
641 if (!NT_STATUS_IS_OK(result))
642 goto done;
644 /* Print results */
646 display_sec_desc(sdb->sec);
648 done:
649 return result;
653 /* List of commands exported by this module */
655 struct cmd_set lsarpc_commands[] = {
657 { "LSARPC" },
659 { "lsaquery", cmd_lsa_query_info_policy, PI_LSARPC, "Query info policy", "" },
660 { "lookupsids", cmd_lsa_lookup_sids, PI_LSARPC, "Convert SIDs to names", "" },
661 { "lookupnames", cmd_lsa_lookup_names, PI_LSARPC, "Convert names to SIDs", "" },
662 { "enumtrust", cmd_lsa_enum_trust_dom, PI_LSARPC, "Enumerate trusted domains", "Usage: [preferred max number] [enum context (0)]" },
663 { "enumprivs", cmd_lsa_enum_privilege, PI_LSARPC, "Enumerate privileges", "" },
664 { "getdispname", cmd_lsa_get_dispname, PI_LSARPC, "Get the privilege name", "" },
665 { "lsaenumsid", cmd_lsa_enum_sids, PI_LSARPC, "Enumerate the LSA SIDS", "" },
666 { "lsaenumprivsaccount", cmd_lsa_enum_privsaccounts, PI_LSARPC, "Enumerate the privileges of an SID", "" },
667 { "lsaenumacctrights", cmd_lsa_enum_acct_rights, PI_LSARPC, "Enumerate the rights of an SID", "" },
668 { "lsaaddacctrights", cmd_lsa_add_acct_rights, PI_LSARPC, "Add rights to an account", "" },
669 { "lsalookupprivvalue", cmd_lsa_lookupprivvalue, PI_LSARPC, "Get a privilege value given its name", "" },
670 { "lsaquerysecobj", cmd_lsa_query_secobj, PI_LSARPC, "Query LSA security object", "" },
672 { NULL }