s3-dcerpc: Pull packet in the caller, before validation
[Samba.git] / source3 / utils / net_rpc_rights.c
blob3dc3f85ffb143d91ec72dc3f210c820f24d2c0ff
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Gerald (Jerry) Carter 2004
5 Copyright (C) Guenther Deschner 2008
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "utils/net.h"
22 #include "../librpc/gen_ndr/cli_lsa.h"
23 #include "rpc_client/cli_lsarpc.h"
25 /********************************************************************
26 ********************************************************************/
28 static NTSTATUS sid_to_name(struct rpc_pipe_client *pipe_hnd,
29 TALLOC_CTX *mem_ctx,
30 struct dom_sid *sid,
31 fstring name)
33 struct policy_handle pol;
34 enum lsa_SidType *sid_types = NULL;
35 NTSTATUS result;
36 char **domains = NULL, **names = NULL;
38 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
39 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
41 if ( !NT_STATUS_IS_OK(result) )
42 return result;
44 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &pol, 1, sid, &domains, &names, &sid_types);
46 if ( NT_STATUS_IS_OK(result) ) {
47 if ( *domains[0] )
48 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
49 else
50 fstrcpy( name, names[0] );
53 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
54 return result;
57 /********************************************************************
58 ********************************************************************/
60 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
61 TALLOC_CTX *mem_ctx,
62 struct dom_sid *sid, const char *name)
64 struct policy_handle pol;
65 enum lsa_SidType *sid_types;
66 NTSTATUS result;
67 struct dom_sid *sids;
69 /* maybe its a raw SID */
70 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
71 return NT_STATUS_OK;
74 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
75 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
77 if ( !NT_STATUS_IS_OK(result) )
78 return result;
80 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &pol, 1, &name,
81 NULL, 1, &sids, &sid_types);
83 if ( NT_STATUS_IS_OK(result) )
84 sid_copy( sid, &sids[0] );
86 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
87 return result;
90 /********************************************************************
91 ********************************************************************/
93 static NTSTATUS enum_privileges(struct rpc_pipe_client *pipe_hnd,
94 TALLOC_CTX *ctx,
95 struct policy_handle *pol )
97 NTSTATUS result;
98 uint32 enum_context = 0;
99 uint32 pref_max_length=0x1000;
100 int i;
101 uint16 lang_id=0;
102 uint16 lang_id_sys=0;
103 uint16 lang_id_desc;
104 struct lsa_StringLarge *description = NULL;
105 struct lsa_PrivArray priv_array;
107 result = rpccli_lsa_EnumPrivs(pipe_hnd, ctx,
108 pol,
109 &enum_context,
110 &priv_array,
111 pref_max_length);
113 if ( !NT_STATUS_IS_OK(result) )
114 return result;
116 /* Print results */
118 for (i = 0; i < priv_array.count; i++) {
120 struct lsa_String lsa_name;
122 d_printf("%30s ",
123 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*" );
125 /* try to get the description */
127 init_lsa_String(&lsa_name, priv_array.privs[i].name.string);
129 result = rpccli_lsa_LookupPrivDisplayName(pipe_hnd, ctx,
130 pol,
131 &lsa_name,
132 lang_id,
133 lang_id_sys,
134 &description,
135 &lang_id_desc);
137 if (!NT_STATUS_IS_OK(result)) {
138 d_printf("??????\n");
139 continue;
142 d_printf("%s\n", description->string);
145 return NT_STATUS_OK;
148 /********************************************************************
149 ********************************************************************/
151 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
152 TALLOC_CTX *ctx,
153 struct policy_handle *pol,
154 struct dom_sid *sid,
155 const char *right)
157 NTSTATUS result;
158 struct lsa_RightSet rights;
159 int i;
161 result = rpccli_lsa_EnumAccountRights(pipe_hnd, ctx,
162 pol,
163 sid,
164 &rights);
166 if (!NT_STATUS_IS_OK(result)) {
167 return result;
170 if (rights.count == 0) {
171 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
174 for (i = 0; i < rights.count; i++) {
175 if (StrCaseCmp(rights.names[i].string, right) == 0) {
176 return NT_STATUS_OK;
180 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
183 /********************************************************************
184 ********************************************************************/
186 static NTSTATUS enum_privileges_for_user(struct rpc_pipe_client *pipe_hnd,
187 TALLOC_CTX *ctx,
188 struct policy_handle *pol,
189 struct dom_sid *sid )
191 NTSTATUS result;
192 struct lsa_RightSet rights;
193 int i;
195 result = rpccli_lsa_EnumAccountRights(pipe_hnd, ctx,
196 pol,
197 sid,
198 &rights);
200 if (!NT_STATUS_IS_OK(result))
201 return result;
203 if (rights.count == 0) {
204 d_printf(_("No privileges assigned\n"));
207 for (i = 0; i < rights.count; i++) {
208 printf("%s\n", rights.names[i].string);
211 return NT_STATUS_OK;
214 /********************************************************************
215 ********************************************************************/
217 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
218 TALLOC_CTX *ctx,
219 struct policy_handle *pol,
220 const char *privilege)
222 NTSTATUS result;
223 uint32 enum_context=0;
224 uint32 pref_max_length=0x1000;
225 struct lsa_SidArray sid_array;
226 int i;
227 fstring name;
229 result = rpccli_lsa_EnumAccounts(pipe_hnd, ctx,
230 pol,
231 &enum_context,
232 &sid_array,
233 pref_max_length);
235 if (!NT_STATUS_IS_OK(result))
236 return result;
238 d_printf("%s:\n", privilege);
240 for ( i=0; i<sid_array.num_sids; i++ ) {
242 result = check_privilege_for_user(pipe_hnd, ctx, pol,
243 sid_array.sids[i].sid,
244 privilege);
246 if ( ! NT_STATUS_IS_OK(result)) {
247 if ( ! NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
248 return result;
250 continue;
253 /* try to convert the SID to a name. Fall back to
254 printing the raw SID if necessary */
255 result = sid_to_name( pipe_hnd, ctx, sid_array.sids[i].sid, name );
256 if ( !NT_STATUS_IS_OK (result) )
257 sid_to_fstring(name, sid_array.sids[i].sid);
259 d_printf(" %s\n", name);
262 return NT_STATUS_OK;
265 /********************************************************************
266 ********************************************************************/
268 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
269 TALLOC_CTX *ctx,
270 struct policy_handle *pol)
272 NTSTATUS result;
273 uint32 enum_context=0;
274 uint32 pref_max_length=0x1000;
275 struct lsa_SidArray sid_array;
276 int i;
277 fstring name;
279 result = rpccli_lsa_EnumAccounts(pipe_hnd, ctx,
280 pol,
281 &enum_context,
282 &sid_array,
283 pref_max_length);
285 if (!NT_STATUS_IS_OK(result))
286 return result;
288 for ( i=0; i<sid_array.num_sids; i++ ) {
290 /* try to convert the SID to a name. Fall back to
291 printing the raw SID if necessary */
293 result = sid_to_name(pipe_hnd, ctx, sid_array.sids[i].sid, name);
294 if ( !NT_STATUS_IS_OK (result) )
295 sid_to_fstring(name, sid_array.sids[i].sid);
297 d_printf("%s\n", name);
299 result = enum_privileges_for_user(pipe_hnd, ctx, pol,
300 sid_array.sids[i].sid);
301 if ( !NT_STATUS_IS_OK(result) )
302 return result;
304 d_printf("\n");
307 return NT_STATUS_OK;
310 /********************************************************************
311 ********************************************************************/
313 static NTSTATUS rpc_rights_list_internal(struct net_context *c,
314 const struct dom_sid *domain_sid,
315 const char *domain_name,
316 struct cli_state *cli,
317 struct rpc_pipe_client *pipe_hnd,
318 TALLOC_CTX *mem_ctx,
319 int argc,
320 const char **argv )
322 struct policy_handle pol;
323 NTSTATUS result;
324 struct dom_sid sid;
325 fstring privname;
326 struct lsa_String lsa_name;
327 struct lsa_StringLarge *description = NULL;
328 uint16 lang_id = 0;
329 uint16 lang_id_sys = 0;
330 uint16 lang_id_desc;
332 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
333 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
335 if ( !NT_STATUS_IS_OK(result) )
336 return result;
338 /* backwards compatibility; just list available privileges if no arguement */
340 if (argc == 0) {
341 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
342 goto done;
345 if (strequal(argv[0], "privileges")) {
346 int i = 1;
348 if (argv[1] == NULL) {
349 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
350 goto done;
353 while ( argv[i] != NULL ) {
354 fstrcpy(privname, argv[i]);
355 init_lsa_String(&lsa_name, argv[i]);
356 i++;
358 /* verify that this is a valid privilege for error reporting */
359 result = rpccli_lsa_LookupPrivDisplayName(pipe_hnd, mem_ctx,
360 &pol,
361 &lsa_name,
362 lang_id,
363 lang_id_sys,
364 &description,
365 &lang_id_desc);
367 if ( !NT_STATUS_IS_OK(result) ) {
368 if ( NT_STATUS_EQUAL( result, NT_STATUS_NO_SUCH_PRIVILEGE ) )
369 d_fprintf(stderr, _("No such privilege "
370 "exists: %s.\n"), privname);
371 else
372 d_fprintf(stderr, _("Error resolving "
373 "privilege display name "
374 "[%s].\n"),
375 nt_errstr(result));
376 continue;
379 result = enum_accounts_for_privilege(pipe_hnd, mem_ctx, &pol, privname);
380 if (!NT_STATUS_IS_OK(result)) {
381 d_fprintf(stderr, _("Error enumerating "
382 "accounts for privilege %s [%s].\n"),
383 privname, nt_errstr(result));
384 continue;
387 goto done;
390 /* special case to enumerate all privileged SIDs with associated rights */
392 if (strequal( argv[0], "accounts")) {
393 int i = 1;
395 if (argv[1] == NULL) {
396 result = enum_privileges_for_accounts(pipe_hnd, mem_ctx, &pol);
397 goto done;
400 while (argv[i] != NULL) {
401 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[i]);
402 if (!NT_STATUS_IS_OK(result)) {
403 goto done;
405 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid);
406 if (!NT_STATUS_IS_OK(result)) {
407 goto done;
409 i++;
411 goto done;
414 /* backward comaptibility: if no keyword provided, treat the key
415 as an account name */
416 if (argc > 1) {
417 d_printf("%s net rpc rights list [[accounts|privileges] "
418 "[name|SID]]\n", _("Usage:"));
419 result = NT_STATUS_OK;
420 goto done;
423 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
424 if (!NT_STATUS_IS_OK(result)) {
425 goto done;
427 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid );
429 done:
430 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
432 return result;
435 /********************************************************************
436 ********************************************************************/
438 static NTSTATUS rpc_rights_grant_internal(struct net_context *c,
439 const struct dom_sid *domain_sid,
440 const char *domain_name,
441 struct cli_state *cli,
442 struct rpc_pipe_client *pipe_hnd,
443 TALLOC_CTX *mem_ctx,
444 int argc,
445 const char **argv )
447 struct policy_handle dom_pol;
448 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
449 struct lsa_RightSet rights;
450 int i;
452 struct dom_sid sid;
454 if (argc < 2 ) {
455 d_printf("%s\n%s",
456 _("Usage:"),
457 _(" net rpc rights grant <name|SID> <rights...>\n"));
458 return NT_STATUS_OK;
461 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
462 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED))
463 result = NT_STATUS_NO_SUCH_USER;
465 if (!NT_STATUS_IS_OK(result))
466 goto done;
468 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
469 SEC_FLAG_MAXIMUM_ALLOWED,
470 &dom_pol);
472 if (!NT_STATUS_IS_OK(result))
473 return result;
475 rights.count = argc-1;
476 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
477 rights.count);
478 if (!rights.names) {
479 return NT_STATUS_NO_MEMORY;
482 for (i=0; i<argc-1; i++) {
483 init_lsa_StringLarge(&rights.names[i], argv[i+1]);
486 result = rpccli_lsa_AddAccountRights(pipe_hnd, mem_ctx,
487 &dom_pol,
488 &sid,
489 &rights);
491 if (!NT_STATUS_IS_OK(result))
492 goto done;
494 d_printf(_("Successfully granted rights.\n"));
496 done:
497 if ( !NT_STATUS_IS_OK(result) ) {
498 d_fprintf(stderr, _("Failed to grant privileges for %s (%s)\n"),
499 argv[0], nt_errstr(result));
502 rpccli_lsa_Close(pipe_hnd, mem_ctx, &dom_pol);
504 return result;
507 /********************************************************************
508 ********************************************************************/
510 static NTSTATUS rpc_rights_revoke_internal(struct net_context *c,
511 const struct dom_sid *domain_sid,
512 const char *domain_name,
513 struct cli_state *cli,
514 struct rpc_pipe_client *pipe_hnd,
515 TALLOC_CTX *mem_ctx,
516 int argc,
517 const char **argv )
519 struct policy_handle dom_pol;
520 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
521 struct lsa_RightSet rights;
522 struct dom_sid sid;
523 int i;
525 if (argc < 2 ) {
526 d_printf("%s\n%s",
527 _("Usage:"),
528 _(" net rpc rights revoke <name|SID> <rights...>\n"));
529 return NT_STATUS_OK;
532 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
533 if (!NT_STATUS_IS_OK(result))
534 return result;
536 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
537 SEC_FLAG_MAXIMUM_ALLOWED,
538 &dom_pol);
540 if (!NT_STATUS_IS_OK(result))
541 return result;
543 rights.count = argc-1;
544 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
545 rights.count);
546 if (!rights.names) {
547 return NT_STATUS_NO_MEMORY;
550 for (i=0; i<argc-1; i++) {
551 init_lsa_StringLarge(&rights.names[i], argv[i+1]);
554 result = rpccli_lsa_RemoveAccountRights(pipe_hnd, mem_ctx,
555 &dom_pol,
556 &sid,
557 false,
558 &rights);
560 if (!NT_STATUS_IS_OK(result))
561 goto done;
563 d_printf(_("Successfully revoked rights.\n"));
565 done:
566 if ( !NT_STATUS_IS_OK(result) ) {
567 d_fprintf(stderr,_("Failed to revoke privileges for %s (%s)\n"),
568 argv[0], nt_errstr(result));
571 rpccli_lsa_Close(pipe_hnd, mem_ctx, &dom_pol);
573 return result;
577 /********************************************************************
578 ********************************************************************/
580 static int rpc_rights_list(struct net_context *c, int argc, const char **argv )
582 if (c->display_usage) {
583 d_printf("%s\n%s",
584 _("Usage:"),
585 _("net rpc rights list [{accounts|privileges} "
586 "[name|SID]]\n"
587 " View available/assigned privileges\n"));
588 return 0;
591 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
592 rpc_rights_list_internal, argc, argv );
595 /********************************************************************
596 ********************************************************************/
598 static int rpc_rights_grant(struct net_context *c, int argc, const char **argv )
600 if (c->display_usage) {
601 d_printf("%s\n%s",
602 _("Usage:"),
603 _("net rpc rights grant <name|SID> <right>\n"
604 " Assign privilege[s]\n"));
605 d_printf(_("For example:\n"
606 " net rpc rights grant 'VALE\\biddle' "
607 "SePrintOperatorPrivilege SeDiskOperatorPrivilege\n"
608 " would grant the printer admin and disk manager "
609 "rights to the user 'VALE\\biddle'\n"));
610 return 0;
613 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
614 rpc_rights_grant_internal, argc, argv );
617 /********************************************************************
618 ********************************************************************/
620 static int rpc_rights_revoke(struct net_context *c, int argc, const char **argv)
622 if (c->display_usage) {
623 d_printf("%s\n%s",
624 _("Usage:"),
625 _("net rpc rights revoke <name|SID> <right>\n"
626 " Revoke privilege[s]\n"));
627 d_printf(_("For example:\n"
628 " net rpc rights revoke 'VALE\\biddle' "
629 "SePrintOperatorPrivilege SeDiskOperatorPrivilege\n"
630 " would revoke the printer admin and disk manager"
631 " rights from the user 'VALE\\biddle'\n"));
632 return 0;
635 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
636 rpc_rights_revoke_internal, argc, argv );
639 /********************************************************************
640 ********************************************************************/
642 int net_rpc_rights(struct net_context *c, int argc, const char **argv)
644 struct functable func[] = {
646 "list",
647 rpc_rights_list,
648 NET_TRANSPORT_RPC,
649 N_("View available/assigned privileges"),
650 N_("net rpc rights list\n"
651 " View available/assigned privileges")
654 "grant",
655 rpc_rights_grant,
656 NET_TRANSPORT_RPC,
657 N_("Assign privilege[s]"),
658 N_("net rpc rights grant\n"
659 " Assign privilege[s]")
662 "revoke",
663 rpc_rights_revoke,
664 NET_TRANSPORT_RPC,
665 N_("Revoke privilege[s]"),
666 N_("net rpc rights revoke\n"
667 " Revoke privilege[s]")
669 {NULL, NULL, 0, NULL, NULL}
672 return net_run_function(c, argc, argv, "net rpc rights", func);
675 static NTSTATUS rpc_sh_rights_list(struct net_context *c,
676 TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
677 struct rpc_pipe_client *pipe_hnd,
678 int argc, const char **argv)
680 return rpc_rights_list_internal(c, ctx->domain_sid, ctx->domain_name,
681 ctx->cli, pipe_hnd, mem_ctx,
682 argc, argv);
685 static NTSTATUS rpc_sh_rights_grant(struct net_context *c,
686 TALLOC_CTX *mem_ctx,
687 struct rpc_sh_ctx *ctx,
688 struct rpc_pipe_client *pipe_hnd,
689 int argc, const char **argv)
691 return rpc_rights_grant_internal(c, ctx->domain_sid, ctx->domain_name,
692 ctx->cli, pipe_hnd, mem_ctx,
693 argc, argv);
696 static NTSTATUS rpc_sh_rights_revoke(struct net_context *c,
697 TALLOC_CTX *mem_ctx,
698 struct rpc_sh_ctx *ctx,
699 struct rpc_pipe_client *pipe_hnd,
700 int argc, const char **argv)
702 return rpc_rights_revoke_internal(c, ctx->domain_sid, ctx->domain_name,
703 ctx->cli, pipe_hnd, mem_ctx,
704 argc, argv);
707 struct rpc_sh_cmd *net_rpc_rights_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
708 struct rpc_sh_ctx *ctx)
710 static struct rpc_sh_cmd cmds[] = {
712 { "list", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_list,
713 N_("View available or assigned privileges") },
715 { "grant", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_grant,
716 N_("Assign privilege[s]") },
718 { "revoke", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_revoke,
719 N_("Revoke privilege[s]") },
721 { NULL, NULL, 0, NULL, NULL }
724 return cmds;