Apply the changes that Derrell Lipman supplied ...
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_lsarpc.c
blob217e6b1d68a8f25cab7fc15789ab84972d4fa552
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 const char **argv)
69 POLICY_HND pol;
70 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
71 DOM_SID dom_sid;
72 struct uuid 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 %s\n",
127 smb_uuid_string_static(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 const 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, const 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 if (!string_to_sid(&sids[i], argv[i + 1])) {
215 result = NT_STATUS_INVALID_SID;
216 goto done;
219 /* Lookup the SIDs */
221 result = cli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids,
222 &domains, &names, &types);
224 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
225 NT_STATUS_V(STATUS_SOME_UNMAPPED))
226 goto done;
228 result = NT_STATUS_OK;
230 /* Print results */
232 for (i = 0; i < (argc - 1); i++) {
233 fstring sid_str;
235 sid_to_string(sid_str, &sids[i]);
236 printf("%s %s\\%s (%d)\n", sid_str,
237 domains[i] ? domains[i] : "*unknown*",
238 names[i] ? names[i] : "*unknown*", types[i]);
241 done:
242 return result;
245 /* Enumerate list of trusted domains */
247 static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli,
248 TALLOC_CTX *mem_ctx, int argc,
249 const char **argv)
251 POLICY_HND pol;
252 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
253 DOM_SID *domain_sids;
254 char **domain_names;
256 /* defaults, but may be changed using params */
257 uint32 enum_ctx = 0;
258 uint32 num_domains = 0;
259 int i;
261 if (argc > 2) {
262 printf("Usage: %s [enum context (0)]\n", argv[0]);
263 return NT_STATUS_OK;
266 if (argc == 2 && argv[1]) {
267 enum_ctx = atoi(argv[2]);
270 result = cli_lsa_open_policy(cli, mem_ctx, True,
271 POLICY_VIEW_LOCAL_INFORMATION,
272 &pol);
274 if (!NT_STATUS_IS_OK(result))
275 goto done;
277 /* Lookup list of trusted domains */
279 result = cli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx,
280 &num_domains,
281 &domain_names, &domain_sids);
282 if (!NT_STATUS_IS_OK(result) &&
283 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
284 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
285 goto done;
287 /* Print results: list of names and sids returned in this response. */
288 for (i = 0; i < num_domains; i++) {
289 fstring sid_str;
291 sid_to_string(sid_str, &domain_sids[i]);
292 printf("%s %s\n", domain_names[i] ? domain_names[i] :
293 "*unknown*", sid_str);
296 done:
297 return result;
300 /* Enumerates privileges */
302 static NTSTATUS cmd_lsa_enum_privilege(struct cli_state *cli,
303 TALLOC_CTX *mem_ctx, int argc,
304 const char **argv)
306 POLICY_HND pol;
307 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
309 uint32 enum_context=0;
310 uint32 pref_max_length=0x1000;
311 uint32 count=0;
312 char **privs_name;
313 uint32 *privs_high;
314 uint32 *privs_low;
315 int i;
317 if (argc > 3) {
318 printf("Usage: %s [enum context] [max length]\n", argv[0]);
319 return NT_STATUS_OK;
322 if (argc>=2)
323 enum_context=atoi(argv[1]);
325 if (argc==3)
326 pref_max_length=atoi(argv[2]);
328 result = cli_lsa_open_policy(cli, mem_ctx, True,
329 SEC_RIGHTS_MAXIMUM_ALLOWED,
330 &pol);
332 if (!NT_STATUS_IS_OK(result))
333 goto done;
335 result = cli_lsa_enum_privilege(cli, mem_ctx, &pol, &enum_context, pref_max_length,
336 &count, &privs_name, &privs_high, &privs_low);
338 if (!NT_STATUS_IS_OK(result))
339 goto done;
341 /* Print results */
342 printf("found %d privileges\n\n", count);
344 for (i = 0; i < count; i++) {
345 printf("%s \t\t%d:%d (0x%x:0x%x)\n", privs_name[i] ? privs_name[i] : "*unknown*",
346 privs_high[i], privs_low[i], privs_high[i], privs_low[i]);
349 done:
350 return result;
353 /* Get privilege name */
355 static NTSTATUS cmd_lsa_get_dispname(struct cli_state *cli,
356 TALLOC_CTX *mem_ctx, int argc,
357 const char **argv)
359 POLICY_HND pol;
360 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
362 uint16 lang_id=0;
363 uint16 lang_id_sys=0;
364 uint16 lang_id_desc;
365 fstring description;
367 if (argc != 2) {
368 printf("Usage: %s privilege name\n", argv[0]);
369 return NT_STATUS_OK;
372 result = cli_lsa_open_policy(cli, mem_ctx, True,
373 SEC_RIGHTS_MAXIMUM_ALLOWED,
374 &pol);
376 if (!NT_STATUS_IS_OK(result))
377 goto done;
379 result = cli_lsa_get_dispname(cli, mem_ctx, &pol, argv[1], lang_id, lang_id_sys, description, &lang_id_desc);
381 if (!NT_STATUS_IS_OK(result))
382 goto done;
384 /* Print results */
385 printf("%s -> %s (language: 0x%x)\n", argv[1], description, lang_id_desc);
387 done:
388 return result;
391 /* Enumerate the LSA SIDS */
393 static NTSTATUS cmd_lsa_enum_sids(struct cli_state *cli,
394 TALLOC_CTX *mem_ctx, int argc,
395 const char **argv)
397 POLICY_HND pol;
398 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
400 uint32 enum_context=0;
401 uint32 pref_max_length=0x1000;
402 DOM_SID *sids;
403 uint32 count=0;
404 int i;
406 if (argc > 3) {
407 printf("Usage: %s [enum context] [max length]\n", argv[0]);
408 return NT_STATUS_OK;
411 if (argc>=2)
412 enum_context=atoi(argv[1]);
414 if (argc==3)
415 pref_max_length=atoi(argv[2]);
417 result = cli_lsa_open_policy(cli, mem_ctx, True,
418 SEC_RIGHTS_MAXIMUM_ALLOWED,
419 &pol);
421 if (!NT_STATUS_IS_OK(result))
422 goto done;
424 result = cli_lsa_enum_sids(cli, mem_ctx, &pol, &enum_context, pref_max_length,
425 &count, &sids);
427 if (!NT_STATUS_IS_OK(result))
428 goto done;
430 /* Print results */
431 printf("found %d SIDs\n\n", count);
433 for (i = 0; i < count; i++) {
434 fstring sid_str;
436 sid_to_string(sid_str, &sids[i]);
437 printf("%s\n", sid_str);
440 done:
441 return result;
444 /* Enumerate the privileges of an SID */
446 static NTSTATUS cmd_lsa_enum_privsaccounts(struct cli_state *cli,
447 TALLOC_CTX *mem_ctx, int argc,
448 const char **argv)
450 POLICY_HND dom_pol;
451 POLICY_HND user_pol;
452 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
453 uint32 access_desired = 0x000f000f;
455 DOM_SID sid;
456 uint32 count=0;
457 LUID_ATTR *set;
458 int i;
460 if (argc != 2 ) {
461 printf("Usage: %s SID\n", argv[0]);
462 return NT_STATUS_OK;
465 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
466 if (!NT_STATUS_IS_OK(result))
467 goto done;
469 result = cli_lsa_open_policy2(cli, mem_ctx, True,
470 SEC_RIGHTS_MAXIMUM_ALLOWED,
471 &dom_pol);
473 if (!NT_STATUS_IS_OK(result))
474 goto done;
476 result = cli_lsa_open_account(cli, mem_ctx, &dom_pol, &sid, access_desired, &user_pol);
478 if (!NT_STATUS_IS_OK(result))
479 goto done;
481 result = cli_lsa_enum_privsaccount(cli, mem_ctx, &user_pol, &count, &set);
483 if (!NT_STATUS_IS_OK(result))
484 goto done;
486 /* Print results */
487 printf("found %d privileges for SID %s\n\n", count, argv[1]);
488 printf("high\tlow\tattribute\n");
490 for (i = 0; i < count; i++) {
491 printf("%u\t%u\t%u\n", set[i].luid.high, set[i].luid.low, set[i].attr);
494 done:
495 return result;
499 /* Enumerate the privileges of an SID via LsaEnumerateAccountRights */
501 static NTSTATUS cmd_lsa_enum_acct_rights(struct cli_state *cli,
502 TALLOC_CTX *mem_ctx, int argc,
503 const char **argv)
505 POLICY_HND dom_pol;
506 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
508 DOM_SID sid;
509 uint32 count;
510 char **rights;
512 int i;
514 if (argc != 2 ) {
515 printf("Usage: %s SID\n", argv[0]);
516 return NT_STATUS_OK;
519 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
520 if (!NT_STATUS_IS_OK(result))
521 goto done;
523 result = cli_lsa_open_policy2(cli, mem_ctx, True,
524 SEC_RIGHTS_MAXIMUM_ALLOWED,
525 &dom_pol);
527 if (!NT_STATUS_IS_OK(result))
528 goto done;
530 result = cli_lsa_enum_account_rights(cli, mem_ctx, &dom_pol, sid, &count, &rights);
532 if (!NT_STATUS_IS_OK(result))
533 goto done;
535 printf("found %d privileges for SID %s\n", count, sid_string_static(&sid));
537 for (i = 0; i < count; i++) {
538 printf("\t%s\n", rights[i]);
541 done:
542 return result;
546 /* add some privileges to a SID via LsaAddAccountRights */
548 static NTSTATUS cmd_lsa_add_acct_rights(struct cli_state *cli,
549 TALLOC_CTX *mem_ctx, int argc,
550 const char **argv)
552 POLICY_HND dom_pol;
553 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
555 DOM_SID sid;
557 if (argc < 3 ) {
558 printf("Usage: %s SID [rights...]\n", argv[0]);
559 return NT_STATUS_OK;
562 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
563 if (!NT_STATUS_IS_OK(result))
564 goto done;
566 result = cli_lsa_open_policy2(cli, mem_ctx, True,
567 SEC_RIGHTS_MAXIMUM_ALLOWED,
568 &dom_pol);
570 if (!NT_STATUS_IS_OK(result))
571 goto done;
573 result = cli_lsa_add_account_rights(cli, mem_ctx, &dom_pol, sid,
574 argc-2, argv+2);
576 if (!NT_STATUS_IS_OK(result))
577 goto done;
579 done:
580 return result;
584 /* remove some privileges to a SID via LsaRemoveAccountRights */
586 static NTSTATUS cmd_lsa_remove_acct_rights(struct cli_state *cli,
587 TALLOC_CTX *mem_ctx, int argc,
588 const char **argv)
590 POLICY_HND dom_pol;
591 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
593 DOM_SID sid;
595 if (argc < 3 ) {
596 printf("Usage: %s SID [rights...]\n", argv[0]);
597 return NT_STATUS_OK;
600 result = name_to_sid(cli, mem_ctx, &sid, argv[1]);
601 if (!NT_STATUS_IS_OK(result))
602 goto done;
604 result = cli_lsa_open_policy2(cli, mem_ctx, True,
605 SEC_RIGHTS_MAXIMUM_ALLOWED,
606 &dom_pol);
608 if (!NT_STATUS_IS_OK(result))
609 goto done;
611 result = cli_lsa_remove_account_rights(cli, mem_ctx, &dom_pol, sid,
612 False, argc-2, argv+2);
614 if (!NT_STATUS_IS_OK(result))
615 goto done;
617 done:
618 return result;
622 /* Get a privilege value given its name */
624 static NTSTATUS cmd_lsa_lookupprivvalue(struct cli_state *cli,
625 TALLOC_CTX *mem_ctx, int argc,
626 const char **argv)
628 POLICY_HND pol;
629 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
630 LUID luid;
632 if (argc != 2 ) {
633 printf("Usage: %s name\n", argv[0]);
634 return NT_STATUS_OK;
637 result = cli_lsa_open_policy2(cli, mem_ctx, True,
638 SEC_RIGHTS_MAXIMUM_ALLOWED,
639 &pol);
641 if (!NT_STATUS_IS_OK(result))
642 goto done;
644 result = cli_lsa_lookupprivvalue(cli, mem_ctx, &pol, argv[1], &luid);
646 if (!NT_STATUS_IS_OK(result))
647 goto done;
649 /* Print results */
651 printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low);
653 done:
654 return result;
657 /* Query LSA security object */
659 static NTSTATUS cmd_lsa_query_secobj(struct cli_state *cli,
660 TALLOC_CTX *mem_ctx, int argc,
661 const char **argv)
663 POLICY_HND pol;
664 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
665 SEC_DESC_BUF *sdb;
666 uint32 sec_info = 0x00000004; /* ??? */
668 if (argc != 1 ) {
669 printf("Usage: %s\n", argv[0]);
670 return NT_STATUS_OK;
673 result = cli_lsa_open_policy2(cli, mem_ctx, True,
674 SEC_RIGHTS_MAXIMUM_ALLOWED,
675 &pol);
677 if (!NT_STATUS_IS_OK(result))
678 goto done;
680 result = cli_lsa_query_secobj(cli, mem_ctx, &pol, sec_info, &sdb);
682 if (!NT_STATUS_IS_OK(result))
683 goto done;
685 /* Print results */
687 display_sec_desc(sdb->sec);
689 done:
690 return result;
694 /* List of commands exported by this module */
696 struct cmd_set lsarpc_commands[] = {
698 { "LSARPC" },
700 { "lsaquery", RPC_RTYPE_NTSTATUS, cmd_lsa_query_info_policy, NULL, PI_LSARPC, "Query info policy", "" },
701 { "lookupsids", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_sids, NULL, PI_LSARPC, "Convert SIDs to names", "" },
702 { "lookupnames", RPC_RTYPE_NTSTATUS, cmd_lsa_lookup_names, NULL, PI_LSARPC, "Convert names to SIDs", "" },
703 { "enumtrust", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_trust_dom, NULL, PI_LSARPC, "Enumerate trusted domains", "Usage: [preferred max number] [enum context (0)]" },
704 { "enumprivs", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privilege, NULL, PI_LSARPC, "Enumerate privileges", "" },
705 { "getdispname", RPC_RTYPE_NTSTATUS, cmd_lsa_get_dispname, NULL, PI_LSARPC, "Get the privilege name", "" },
706 { "lsaenumsid", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_sids, NULL, PI_LSARPC, "Enumerate the LSA SIDS", "" },
707 { "lsaenumprivsaccount", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_privsaccounts, NULL, PI_LSARPC, "Enumerate the privileges of an SID", "" },
708 { "lsaenumacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_enum_acct_rights, NULL, PI_LSARPC, "Enumerate the rights of an SID", "" },
709 { "lsaaddacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_add_acct_rights, NULL, PI_LSARPC, "Add rights to an account", "" },
710 { "lsaremoveacctrights", RPC_RTYPE_NTSTATUS, cmd_lsa_remove_acct_rights, NULL, PI_LSARPC, "Remove rights from an account", "" },
711 { "lsalookupprivvalue", RPC_RTYPE_NTSTATUS, cmd_lsa_lookupprivvalue, NULL, PI_LSARPC, "Get a privilege value given its name", "" },
712 { "lsaquerysecobj", RPC_RTYPE_NTSTATUS, cmd_lsa_query_secobj, NULL, PI_LSARPC, "Query LSA security object", "" },
714 { NULL }