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/>.
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,
33 struct policy_handle pol;
34 enum lsa_SidType *sid_types = NULL;
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) )
44 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &pol, 1, sid, &domains, &names, &sid_types);
46 if ( NT_STATUS_IS_OK(result) ) {
48 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
50 fstrcpy( name, names[0] );
53 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
57 /********************************************************************
58 ********************************************************************/
60 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
62 DOM_SID *sid, const char *name)
64 struct policy_handle pol;
65 enum lsa_SidType *sid_types;
69 /* maybe its a raw SID */
70 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
74 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
75 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
77 if ( !NT_STATUS_IS_OK(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);
90 /********************************************************************
91 ********************************************************************/
93 static NTSTATUS enum_privileges(struct rpc_pipe_client *pipe_hnd,
95 struct policy_handle *pol )
98 uint32 enum_context = 0;
99 uint32 pref_max_length=0x1000;
102 uint16 lang_id_sys=0;
104 struct lsa_StringLarge *description = NULL;
105 struct lsa_PrivArray priv_array;
107 result = rpccli_lsa_EnumPrivs(pipe_hnd, ctx,
113 if ( !NT_STATUS_IS_OK(result) )
118 for (i = 0; i < priv_array.count; i++) {
120 struct lsa_String lsa_name;
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,
137 if (!NT_STATUS_IS_OK(result)) {
138 d_printf("??????\n");
142 d_printf("%s\n", description->string);
148 /********************************************************************
149 ********************************************************************/
151 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
153 struct policy_handle *pol,
158 struct lsa_RightSet rights;
161 result = rpccli_lsa_EnumAccountRights(pipe_hnd, ctx,
166 if (!NT_STATUS_IS_OK(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) {
180 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
183 /********************************************************************
184 ********************************************************************/
186 static NTSTATUS enum_privileges_for_user(struct rpc_pipe_client *pipe_hnd,
188 struct policy_handle *pol,
192 struct lsa_RightSet rights;
195 result = rpccli_lsa_EnumAccountRights(pipe_hnd, ctx,
200 if (!NT_STATUS_IS_OK(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);
214 /********************************************************************
215 ********************************************************************/
217 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
219 struct policy_handle *pol,
220 const char *privilege)
223 uint32 enum_context=0;
224 uint32 pref_max_length=0x1000;
225 struct lsa_SidArray sid_array;
229 result = rpccli_lsa_EnumAccounts(pipe_hnd, ctx,
235 if (!NT_STATUS_IS_OK(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,
246 if ( ! NT_STATUS_IS_OK(result)) {
247 if ( ! NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
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);
265 /********************************************************************
266 ********************************************************************/
268 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
270 struct policy_handle *pol)
273 uint32 enum_context=0;
274 uint32 pref_max_length=0x1000;
275 struct lsa_SidArray sid_array;
279 result = rpccli_lsa_EnumAccounts(pipe_hnd, ctx,
285 if (!NT_STATUS_IS_OK(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) )
310 /********************************************************************
311 ********************************************************************/
313 static NTSTATUS rpc_rights_list_internal(struct net_context *c,
314 const DOM_SID *domain_sid,
315 const char *domain_name,
316 struct cli_state *cli,
317 struct rpc_pipe_client *pipe_hnd,
322 struct policy_handle pol;
326 struct lsa_String lsa_name;
327 struct lsa_StringLarge *description = NULL;
329 uint16 lang_id_sys = 0;
332 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
333 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
335 if ( !NT_STATUS_IS_OK(result) )
338 /* backwards compatibility; just list available privileges if no arguement */
341 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
345 if (strequal(argv[0], "privileges")) {
348 if (argv[1] == NULL) {
349 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
353 while ( argv[i] != NULL ) {
354 fstrcpy(privname, argv[i]);
355 init_lsa_String(&lsa_name, argv[i]);
358 /* verify that this is a valid privilege for error reporting */
359 result = rpccli_lsa_LookupPrivDisplayName(pipe_hnd, mem_ctx,
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);
372 d_fprintf(stderr, _("Error resolving "
373 "privilege display name "
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));
390 /* special case to enumerate all privileged SIDs with associated rights */
392 if (strequal( argv[0], "accounts")) {
395 if (argv[1] == NULL) {
396 result = enum_privileges_for_accounts(pipe_hnd, mem_ctx, &pol);
400 while (argv[i] != NULL) {
401 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[i]);
402 if (!NT_STATUS_IS_OK(result)) {
405 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid);
406 if (!NT_STATUS_IS_OK(result)) {
414 /* backward comaptibility: if no keyword provided, treat the key
415 as an account name */
417 d_printf("%s net rpc rights list [[accounts|privileges] "
418 "[name|SID]]\n", _("Usage:"));
419 result = NT_STATUS_OK;
423 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
424 if (!NT_STATUS_IS_OK(result)) {
427 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid );
430 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
435 /********************************************************************
436 ********************************************************************/
438 static NTSTATUS rpc_rights_grant_internal(struct net_context *c,
439 const DOM_SID *domain_sid,
440 const char *domain_name,
441 struct cli_state *cli,
442 struct rpc_pipe_client *pipe_hnd,
447 struct policy_handle dom_pol;
448 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
449 struct lsa_RightSet rights;
457 _(" net rpc rights grant <name|SID> <rights...>\n"));
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))
468 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
469 SEC_FLAG_MAXIMUM_ALLOWED,
472 if (!NT_STATUS_IS_OK(result))
475 rights.count = argc-1;
476 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
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,
491 if (!NT_STATUS_IS_OK(result))
494 d_printf(_("Successfully granted rights.\n"));
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);
507 /********************************************************************
508 ********************************************************************/
510 static NTSTATUS rpc_rights_revoke_internal(struct net_context *c,
511 const DOM_SID *domain_sid,
512 const char *domain_name,
513 struct cli_state *cli,
514 struct rpc_pipe_client *pipe_hnd,
519 struct policy_handle dom_pol;
520 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
521 struct lsa_RightSet rights;
528 _(" net rpc rights revoke <name|SID> <rights...>\n"));
532 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
533 if (!NT_STATUS_IS_OK(result))
536 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
537 SEC_FLAG_MAXIMUM_ALLOWED,
540 if (!NT_STATUS_IS_OK(result))
543 rights.count = argc-1;
544 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
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,
560 if (!NT_STATUS_IS_OK(result))
563 d_printf(_("Successfully revoked rights.\n"));
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);
577 /********************************************************************
578 ********************************************************************/
580 static int rpc_rights_list(struct net_context *c, int argc, const char **argv )
582 if (c->display_usage) {
585 _("net rpc rights list [{accounts|privileges} "
587 " View available/assigned privileges\n"));
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) {
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"));
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) {
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"));
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[] = {
649 N_("View available/assigned privileges"),
650 N_("net rpc rights list\n"
651 " View available/assigned privileges")
657 N_("Assign privilege[s]"),
658 N_("net rpc rights grant\n"
659 " Assign privilege[s]")
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,
685 static NTSTATUS rpc_sh_rights_grant(struct net_context *c,
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,
696 static NTSTATUS rpc_sh_rights_revoke(struct net_context *c,
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,
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 }