s3/net: split up some printable stings to ease i18n
[Samba.git] / source3 / utils / net_rpc_rights.c
blob355a226198b1a232f1b9f606ff69c8cba53f2cb8
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"
24 /********************************************************************
25 ********************************************************************/
27 static NTSTATUS sid_to_name(struct rpc_pipe_client *pipe_hnd,
28 TALLOC_CTX *mem_ctx,
29 DOM_SID *sid,
30 fstring name)
32 struct policy_handle pol;
33 enum lsa_SidType *sid_types = NULL;
34 NTSTATUS result;
35 char **domains = NULL, **names = NULL;
37 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
38 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
40 if ( !NT_STATUS_IS_OK(result) )
41 return result;
43 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &pol, 1, sid, &domains, &names, &sid_types);
45 if ( NT_STATUS_IS_OK(result) ) {
46 if ( *domains[0] )
47 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
48 else
49 fstrcpy( name, names[0] );
52 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
53 return result;
56 /********************************************************************
57 ********************************************************************/
59 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
60 TALLOC_CTX *mem_ctx,
61 DOM_SID *sid, const char *name)
63 struct policy_handle pol;
64 enum lsa_SidType *sid_types;
65 NTSTATUS result;
66 DOM_SID *sids;
68 /* maybe its a raw SID */
69 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
70 return NT_STATUS_OK;
73 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
74 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
76 if ( !NT_STATUS_IS_OK(result) )
77 return result;
79 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &pol, 1, &name,
80 NULL, 1, &sids, &sid_types);
82 if ( NT_STATUS_IS_OK(result) )
83 sid_copy( sid, &sids[0] );
85 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
86 return result;
89 /********************************************************************
90 ********************************************************************/
92 static NTSTATUS enum_privileges(struct rpc_pipe_client *pipe_hnd,
93 TALLOC_CTX *ctx,
94 struct policy_handle *pol )
96 NTSTATUS result;
97 uint32 enum_context = 0;
98 uint32 pref_max_length=0x1000;
99 int i;
100 uint16 lang_id=0;
101 uint16 lang_id_sys=0;
102 uint16 lang_id_desc;
103 struct lsa_StringLarge *description = NULL;
104 struct lsa_PrivArray priv_array;
106 result = rpccli_lsa_EnumPrivs(pipe_hnd, ctx,
107 pol,
108 &enum_context,
109 &priv_array,
110 pref_max_length);
112 if ( !NT_STATUS_IS_OK(result) )
113 return result;
115 /* Print results */
117 for (i = 0; i < priv_array.count; i++) {
119 struct lsa_String lsa_name;
121 d_printf("%30s ",
122 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*" );
124 /* try to get the description */
126 init_lsa_String(&lsa_name, priv_array.privs[i].name.string);
128 result = rpccli_lsa_LookupPrivDisplayName(pipe_hnd, ctx,
129 pol,
130 &lsa_name,
131 lang_id,
132 lang_id_sys,
133 &description,
134 &lang_id_desc);
136 if (!NT_STATUS_IS_OK(result)) {
137 d_printf("??????\n");
138 continue;
141 d_printf("%s\n", description->string);
144 return NT_STATUS_OK;
147 /********************************************************************
148 ********************************************************************/
150 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
151 TALLOC_CTX *ctx,
152 struct policy_handle *pol,
153 DOM_SID *sid,
154 const char *right)
156 NTSTATUS result;
157 struct lsa_RightSet rights;
158 int i;
160 result = rpccli_lsa_EnumAccountRights(pipe_hnd, ctx,
161 pol,
162 sid,
163 &rights);
165 if (!NT_STATUS_IS_OK(result)) {
166 return result;
169 if (rights.count == 0) {
170 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
173 for (i = 0; i < rights.count; i++) {
174 if (StrCaseCmp(rights.names[i].string, right) == 0) {
175 return NT_STATUS_OK;
179 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
182 /********************************************************************
183 ********************************************************************/
185 static NTSTATUS enum_privileges_for_user(struct rpc_pipe_client *pipe_hnd,
186 TALLOC_CTX *ctx,
187 struct policy_handle *pol,
188 DOM_SID *sid )
190 NTSTATUS result;
191 struct lsa_RightSet rights;
192 int i;
194 result = rpccli_lsa_EnumAccountRights(pipe_hnd, ctx,
195 pol,
196 sid,
197 &rights);
199 if (!NT_STATUS_IS_OK(result))
200 return result;
202 if (rights.count == 0) {
203 d_printf(_("No privileges assigned\n"));
206 for (i = 0; i < rights.count; i++) {
207 printf("%s\n", rights.names[i].string);
210 return NT_STATUS_OK;
213 /********************************************************************
214 ********************************************************************/
216 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
217 TALLOC_CTX *ctx,
218 struct policy_handle *pol,
219 const char *privilege)
221 NTSTATUS result;
222 uint32 enum_context=0;
223 uint32 pref_max_length=0x1000;
224 struct lsa_SidArray sid_array;
225 int i;
226 fstring name;
228 result = rpccli_lsa_EnumAccounts(pipe_hnd, ctx,
229 pol,
230 &enum_context,
231 &sid_array,
232 pref_max_length);
234 if (!NT_STATUS_IS_OK(result))
235 return result;
237 d_printf("%s:\n", privilege);
239 for ( i=0; i<sid_array.num_sids; i++ ) {
241 result = check_privilege_for_user(pipe_hnd, ctx, pol,
242 sid_array.sids[i].sid,
243 privilege);
245 if ( ! NT_STATUS_IS_OK(result)) {
246 if ( ! NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
247 return result;
249 continue;
252 /* try to convert the SID to a name. Fall back to
253 printing the raw SID if necessary */
254 result = sid_to_name( pipe_hnd, ctx, sid_array.sids[i].sid, name );
255 if ( !NT_STATUS_IS_OK (result) )
256 sid_to_fstring(name, sid_array.sids[i].sid);
258 d_printf(" %s\n", name);
261 return NT_STATUS_OK;
264 /********************************************************************
265 ********************************************************************/
267 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
268 TALLOC_CTX *ctx,
269 struct policy_handle *pol)
271 NTSTATUS result;
272 uint32 enum_context=0;
273 uint32 pref_max_length=0x1000;
274 struct lsa_SidArray sid_array;
275 int i;
276 fstring name;
278 result = rpccli_lsa_EnumAccounts(pipe_hnd, ctx,
279 pol,
280 &enum_context,
281 &sid_array,
282 pref_max_length);
284 if (!NT_STATUS_IS_OK(result))
285 return result;
287 for ( i=0; i<sid_array.num_sids; i++ ) {
289 /* try to convert the SID to a name. Fall back to
290 printing the raw SID if necessary */
292 result = sid_to_name(pipe_hnd, ctx, sid_array.sids[i].sid, name);
293 if ( !NT_STATUS_IS_OK (result) )
294 sid_to_fstring(name, sid_array.sids[i].sid);
296 d_printf("%s\n", name);
298 result = enum_privileges_for_user(pipe_hnd, ctx, pol,
299 sid_array.sids[i].sid);
300 if ( !NT_STATUS_IS_OK(result) )
301 return result;
303 d_printf("\n");
306 return NT_STATUS_OK;
309 /********************************************************************
310 ********************************************************************/
312 static NTSTATUS rpc_rights_list_internal(struct net_context *c,
313 const DOM_SID *domain_sid,
314 const char *domain_name,
315 struct cli_state *cli,
316 struct rpc_pipe_client *pipe_hnd,
317 TALLOC_CTX *mem_ctx,
318 int argc,
319 const char **argv )
321 struct policy_handle pol;
322 NTSTATUS result;
323 DOM_SID sid;
324 fstring privname;
325 struct lsa_String lsa_name;
326 struct lsa_StringLarge *description = NULL;
327 uint16 lang_id = 0;
328 uint16 lang_id_sys = 0;
329 uint16 lang_id_desc;
331 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
332 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
334 if ( !NT_STATUS_IS_OK(result) )
335 return result;
337 /* backwards compatibility; just list available privileges if no arguement */
339 if (argc == 0) {
340 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
341 goto done;
344 if (strequal(argv[0], "privileges")) {
345 int i = 1;
347 if (argv[1] == NULL) {
348 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
349 goto done;
352 while ( argv[i] != NULL ) {
353 fstrcpy(privname, argv[i]);
354 init_lsa_String(&lsa_name, argv[i]);
355 i++;
357 /* verify that this is a valid privilege for error reporting */
358 result = rpccli_lsa_LookupPrivDisplayName(pipe_hnd, mem_ctx,
359 &pol,
360 &lsa_name,
361 lang_id,
362 lang_id_sys,
363 &description,
364 &lang_id_desc);
366 if ( !NT_STATUS_IS_OK(result) ) {
367 if ( NT_STATUS_EQUAL( result, NT_STATUS_NO_SUCH_PRIVILEGE ) )
368 d_fprintf(stderr, _("No such privilege "
369 "exists: %s.\n"), privname);
370 else
371 d_fprintf(stderr, _("Error resolving "
372 "privilege display name "
373 "[%s].\n"),
374 nt_errstr(result));
375 continue;
378 result = enum_accounts_for_privilege(pipe_hnd, mem_ctx, &pol, privname);
379 if (!NT_STATUS_IS_OK(result)) {
380 d_fprintf(stderr, _("Error enumerating "
381 "accounts for privilege %s [%s].\n"),
382 privname, nt_errstr(result));
383 continue;
386 goto done;
389 /* special case to enumerate all privileged SIDs with associated rights */
391 if (strequal( argv[0], "accounts")) {
392 int i = 1;
394 if (argv[1] == NULL) {
395 result = enum_privileges_for_accounts(pipe_hnd, mem_ctx, &pol);
396 goto done;
399 while (argv[i] != NULL) {
400 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[i]);
401 if (!NT_STATUS_IS_OK(result)) {
402 goto done;
404 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid);
405 if (!NT_STATUS_IS_OK(result)) {
406 goto done;
408 i++;
410 goto done;
413 /* backward comaptibility: if no keyword provided, treat the key
414 as an account name */
415 if (argc > 1) {
416 d_printf(_("Usage:")," net rpc rights list [[accounts|privileges] "
417 "[name|SID]]\n");
418 result = NT_STATUS_OK;
419 goto done;
422 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
423 if (!NT_STATUS_IS_OK(result)) {
424 goto done;
426 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid );
428 done:
429 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
431 return result;
434 /********************************************************************
435 ********************************************************************/
437 static NTSTATUS rpc_rights_grant_internal(struct net_context *c,
438 const DOM_SID *domain_sid,
439 const char *domain_name,
440 struct cli_state *cli,
441 struct rpc_pipe_client *pipe_hnd,
442 TALLOC_CTX *mem_ctx,
443 int argc,
444 const char **argv )
446 struct policy_handle dom_pol;
447 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
448 struct lsa_RightSet rights;
449 int i;
451 DOM_SID sid;
453 if (argc < 2 ) {
454 d_printf(_("Usage:"),_(" net rpc rights grant <name|SID> "
455 "<rights...>\n"));
456 return NT_STATUS_OK;
459 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
460 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED))
461 result = NT_STATUS_NO_SUCH_USER;
463 if (!NT_STATUS_IS_OK(result))
464 goto done;
466 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
467 SEC_FLAG_MAXIMUM_ALLOWED,
468 &dom_pol);
470 if (!NT_STATUS_IS_OK(result))
471 return result;
473 rights.count = argc-1;
474 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
475 rights.count);
476 if (!rights.names) {
477 return NT_STATUS_NO_MEMORY;
480 for (i=0; i<argc-1; i++) {
481 init_lsa_StringLarge(&rights.names[i], argv[i+1]);
484 result = rpccli_lsa_AddAccountRights(pipe_hnd, mem_ctx,
485 &dom_pol,
486 &sid,
487 &rights);
489 if (!NT_STATUS_IS_OK(result))
490 goto done;
492 d_printf(_("Successfully granted rights.\n"));
494 done:
495 if ( !NT_STATUS_IS_OK(result) ) {
496 d_fprintf(stderr, _("Failed to grant privileges for %s (%s)\n"),
497 argv[0], nt_errstr(result));
500 rpccli_lsa_Close(pipe_hnd, mem_ctx, &dom_pol);
502 return result;
505 /********************************************************************
506 ********************************************************************/
508 static NTSTATUS rpc_rights_revoke_internal(struct net_context *c,
509 const DOM_SID *domain_sid,
510 const char *domain_name,
511 struct cli_state *cli,
512 struct rpc_pipe_client *pipe_hnd,
513 TALLOC_CTX *mem_ctx,
514 int argc,
515 const char **argv )
517 struct policy_handle dom_pol;
518 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
519 struct lsa_RightSet rights;
520 DOM_SID sid;
521 int i;
523 if (argc < 2 ) {
524 d_printf(_("Usage:"),_(" net rpc rights revoke <name|SID> "
525 "<rights...>\n"));
526 return NT_STATUS_OK;
529 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
530 if (!NT_STATUS_IS_OK(result))
531 return result;
533 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
534 SEC_FLAG_MAXIMUM_ALLOWED,
535 &dom_pol);
537 if (!NT_STATUS_IS_OK(result))
538 return result;
540 rights.count = argc-1;
541 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
542 rights.count);
543 if (!rights.names) {
544 return NT_STATUS_NO_MEMORY;
547 for (i=0; i<argc-1; i++) {
548 init_lsa_StringLarge(&rights.names[i], argv[i+1]);
551 result = rpccli_lsa_RemoveAccountRights(pipe_hnd, mem_ctx,
552 &dom_pol,
553 &sid,
554 false,
555 &rights);
557 if (!NT_STATUS_IS_OK(result))
558 goto done;
560 d_printf(_("Successfully revoked rights.\n"));
562 done:
563 if ( !NT_STATUS_IS_OK(result) ) {
564 d_fprintf(stderr,_("Failed to revoke privileges for %s (%s)\n"),
565 argv[0], nt_errstr(result));
568 rpccli_lsa_Close(pipe_hnd, mem_ctx, &dom_pol);
570 return result;
574 /********************************************************************
575 ********************************************************************/
577 static int rpc_rights_list(struct net_context *c, int argc, const char **argv )
579 if (c->display_usage) {
580 d_printf(_("Usage:\n"),
581 _("net rpc rights list [{accounts|privileges} "
582 "[name|SID]]\n"
583 " View available/assigned privileges\n"));
584 return 0;
587 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
588 rpc_rights_list_internal, argc, argv );
591 /********************************************************************
592 ********************************************************************/
594 static int rpc_rights_grant(struct net_context *c, int argc, const char **argv )
596 if (c->display_usage) {
597 d_printf(_("Usage:\n"),
598 _("net rpc rights grant <name|SID> <right>\n"
599 " Assign privilege[s]\n"));
600 d_printf(_("For example:\n"
601 " net rpc rights grant 'VALE\\biddle' "
602 "SePrintOperatorPrivilege SeDiskOperatorPrivilege\n"
603 " would grant the printer admin and disk manager "
604 "rights to the user 'VALE\\biddle'\n"));
605 return 0;
608 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
609 rpc_rights_grant_internal, argc, argv );
612 /********************************************************************
613 ********************************************************************/
615 static int rpc_rights_revoke(struct net_context *c, int argc, const char **argv)
617 if (c->display_usage) {
618 d_printf(_("Usage:\n"),
619 _("net rpc rights revoke <name|SID> <right>\n"
620 " Revoke privilege[s]\n"));
621 d_printf(_("For example:\n"
622 " net rpc rights revoke 'VALE\\biddle' "
623 "SePrintOperatorPrivilege SeDiskOperatorPrivilege\n"
624 " would revoke the printer admin and disk manager"
625 " rights from the user 'VALE\\biddle'\n"));
626 return 0;
629 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
630 rpc_rights_revoke_internal, argc, argv );
633 /********************************************************************
634 ********************************************************************/
636 int net_rpc_rights(struct net_context *c, int argc, const char **argv)
638 struct functable func[] = {
640 "list",
641 rpc_rights_list,
642 NET_TRANSPORT_RPC,
643 N_("View available/assigned privileges"),
644 N_("net rpc rights list\n"
645 " View available/assigned privileges")
648 "grant",
649 rpc_rights_grant,
650 NET_TRANSPORT_RPC,
651 N_("Assign privilege[s]"),
652 N_("net rpc rights grant\n"
653 " Assign privilege[s]")
656 "revoke",
657 rpc_rights_revoke,
658 NET_TRANSPORT_RPC,
659 N_("Revoke privilege[s]"),
660 N_("net rpc rights revoke\n"
661 " Revoke privilege[s]")
663 {NULL, NULL, 0, NULL, NULL}
666 return net_run_function(c, argc, argv, "net rpc rights", func);
669 static NTSTATUS rpc_sh_rights_list(struct net_context *c,
670 TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
671 struct rpc_pipe_client *pipe_hnd,
672 int argc, const char **argv)
674 return rpc_rights_list_internal(c, ctx->domain_sid, ctx->domain_name,
675 ctx->cli, pipe_hnd, mem_ctx,
676 argc, argv);
679 static NTSTATUS rpc_sh_rights_grant(struct net_context *c,
680 TALLOC_CTX *mem_ctx,
681 struct rpc_sh_ctx *ctx,
682 struct rpc_pipe_client *pipe_hnd,
683 int argc, const char **argv)
685 return rpc_rights_grant_internal(c, ctx->domain_sid, ctx->domain_name,
686 ctx->cli, pipe_hnd, mem_ctx,
687 argc, argv);
690 static NTSTATUS rpc_sh_rights_revoke(struct net_context *c,
691 TALLOC_CTX *mem_ctx,
692 struct rpc_sh_ctx *ctx,
693 struct rpc_pipe_client *pipe_hnd,
694 int argc, const char **argv)
696 return rpc_rights_revoke_internal(c, ctx->domain_sid, ctx->domain_name,
697 ctx->cli, pipe_hnd, mem_ctx,
698 argc, argv);
701 struct rpc_sh_cmd *net_rpc_rights_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
702 struct rpc_sh_ctx *ctx)
704 static struct rpc_sh_cmd cmds[] = {
706 { "list", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_list,
707 N_("View available or assigned privileges") },
709 { "grant", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_grant,
710 N_("Assign privilege[s]") },
712 { "revoke", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_revoke,
713 N_("Revoke privilege[s]") },
715 { NULL, NULL, 0, NULL, NULL }
718 return cmds;