2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Gerald (Jerry) Carter 2004
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "utils/net.h"
22 /********************************************************************
23 ********************************************************************/
25 static NTSTATUS sid_to_name(struct rpc_pipe_client *pipe_hnd,
31 enum lsa_SidType *sid_types = NULL;
33 char **domains = NULL, **names = NULL;
35 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
36 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
38 if ( !NT_STATUS_IS_OK(result) )
41 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &pol, 1, sid, &domains, &names, &sid_types);
43 if ( NT_STATUS_IS_OK(result) ) {
45 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
47 fstrcpy( name, names[0] );
50 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
54 /********************************************************************
55 ********************************************************************/
57 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
59 DOM_SID *sid, const char *name)
62 enum lsa_SidType *sid_types;
66 /* maybe its a raw SID */
67 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
71 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
72 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
74 if ( !NT_STATUS_IS_OK(result) )
77 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &pol, 1, &name,
78 NULL, 1, &sids, &sid_types);
80 if ( NT_STATUS_IS_OK(result) )
81 sid_copy( sid, &sids[0] );
83 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
87 /********************************************************************
88 ********************************************************************/
90 static NTSTATUS enum_privileges(struct rpc_pipe_client *pipe_hnd,
95 uint32 enum_context = 0;
96 uint32 pref_max_length=0x1000;
101 struct lsa_StringLarge *description = NULL;
102 struct lsa_PrivArray priv_array;
104 result = rpccli_lsa_EnumPrivs(pipe_hnd, ctx,
110 if ( !NT_STATUS_IS_OK(result) )
115 for (i = 0; i < priv_array.count; i++) {
117 struct lsa_String lsa_name;
120 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*" );
122 /* try to get the description */
124 init_lsa_String(&lsa_name, priv_array.privs[i].name.string);
126 result = rpccli_lsa_LookupPrivDisplayName(pipe_hnd, ctx,
134 if (!NT_STATUS_IS_OK(result)) {
135 d_printf("??????\n");
139 d_printf("%s\n", description->string);
145 /********************************************************************
146 ********************************************************************/
148 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
159 result = rpccli_lsa_enum_account_rights(pipe_hnd, ctx, pol, sid, &count, &rights);
161 if (!NT_STATUS_IS_OK(result)) {
166 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
169 for (i = 0; i < count; i++) {
170 if (StrCaseCmp(rights[i], right) == 0) {
175 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
178 /********************************************************************
179 ********************************************************************/
181 static NTSTATUS enum_privileges_for_user(struct rpc_pipe_client *pipe_hnd,
191 result = rpccli_lsa_enum_account_rights(pipe_hnd, ctx, pol, sid, &count, &rights);
193 if (!NT_STATUS_IS_OK(result))
197 d_printf("No privileges assigned\n");
199 for (i = 0; i < count; i++) {
200 printf("%s\n", rights[i]);
206 /********************************************************************
207 ********************************************************************/
209 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
212 const char *privilege)
215 uint32 enum_context=0;
216 uint32 pref_max_length=0x1000;
217 struct lsa_SidArray sid_array;
221 result = rpccli_lsa_EnumAccounts(pipe_hnd, ctx,
227 if (!NT_STATUS_IS_OK(result))
230 d_printf("%s:\n", privilege);
232 for ( i=0; i<sid_array.num_sids; i++ ) {
234 result = check_privilege_for_user(pipe_hnd, ctx, pol,
235 sid_array.sids[i].sid,
238 if ( ! NT_STATUS_IS_OK(result)) {
239 if ( ! NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
245 /* try to convert the SID to a name. Fall back to
246 printing the raw SID if necessary */
247 result = sid_to_name( pipe_hnd, ctx, sid_array.sids[i].sid, name );
248 if ( !NT_STATUS_IS_OK (result) )
249 sid_to_fstring(name, sid_array.sids[i].sid);
251 d_printf(" %s\n", name);
257 /********************************************************************
258 ********************************************************************/
260 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
265 uint32 enum_context=0;
266 uint32 pref_max_length=0x1000;
267 struct lsa_SidArray sid_array;
271 result = rpccli_lsa_EnumAccounts(pipe_hnd, ctx,
277 if (!NT_STATUS_IS_OK(result))
280 for ( i=0; i<sid_array.num_sids; i++ ) {
282 /* try to convert the SID to a name. Fall back to
283 printing the raw SID if necessary */
285 result = sid_to_name(pipe_hnd, ctx, sid_array.sids[i].sid, name);
286 if ( !NT_STATUS_IS_OK (result) )
287 sid_to_fstring(name, sid_array.sids[i].sid);
289 d_printf("%s\n", name);
291 result = enum_privileges_for_user(pipe_hnd, ctx, pol,
292 sid_array.sids[i].sid);
293 if ( !NT_STATUS_IS_OK(result) )
302 /********************************************************************
303 ********************************************************************/
305 static NTSTATUS rpc_rights_list_internal(const DOM_SID *domain_sid,
306 const char *domain_name,
307 struct cli_state *cli,
308 struct rpc_pipe_client *pipe_hnd,
317 struct lsa_String lsa_name;
318 struct lsa_StringLarge *description = NULL;
320 uint16 lang_id_sys = 0;
324 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
325 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
327 if ( !NT_STATUS_IS_OK(result) )
330 /* backwards compatibility; just list available privileges if no arguement */
333 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
337 if (strequal(argv[0], "privileges")) {
340 if (argv[1] == NULL) {
341 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
345 while ( argv[i] != NULL ) {
346 fstrcpy(privname, argv[i]);
347 init_lsa_String(&lsa_name, argv[i]);
350 /* verify that this is a valid privilege for error reporting */
351 result = rpccli_lsa_LookupPrivDisplayName(pipe_hnd, mem_ctx,
359 if ( !NT_STATUS_IS_OK(result) ) {
360 if ( NT_STATUS_EQUAL( result, NT_STATUS_NO_SUCH_PRIVILEGE ) )
361 d_fprintf(stderr, "No such privilege exists: %s.\n", privname);
363 d_fprintf(stderr, "Error resolving privilege display name [%s].\n", nt_errstr(result));
367 result = enum_accounts_for_privilege(pipe_hnd, mem_ctx, &pol, privname);
368 if (!NT_STATUS_IS_OK(result)) {
369 d_fprintf(stderr, "Error enumerating accounts for privilege %s [%s].\n",
370 privname, nt_errstr(result));
377 /* special case to enumerate all privileged SIDs with associated rights */
379 if (strequal( argv[0], "accounts")) {
382 if (argv[1] == NULL) {
383 result = enum_privileges_for_accounts(pipe_hnd, mem_ctx, &pol);
387 while (argv[i] != NULL) {
388 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[i]);
389 if (!NT_STATUS_IS_OK(result)) {
392 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid);
393 if (!NT_STATUS_IS_OK(result)) {
401 /* backward comaptibility: if no keyword provided, treat the key
402 as an account name */
404 d_printf("Usage: net rpc rights list [[accounts|privileges] [name|SID]]\n");
405 result = NT_STATUS_OK;
409 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
410 if (!NT_STATUS_IS_OK(result)) {
413 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid );
416 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
421 /********************************************************************
422 ********************************************************************/
424 static NTSTATUS rpc_rights_grant_internal(const DOM_SID *domain_sid,
425 const char *domain_name,
426 struct cli_state *cli,
427 struct rpc_pipe_client *pipe_hnd,
433 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
438 d_printf("Usage: net rpc rights grant <name|SID> <rights...>\n");
442 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
443 if (!NT_STATUS_IS_OK(result))
446 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True,
447 SEC_RIGHTS_MAXIMUM_ALLOWED,
450 if (!NT_STATUS_IS_OK(result))
453 result = rpccli_lsa_add_account_rights(pipe_hnd, mem_ctx, &dom_pol, sid,
456 if (!NT_STATUS_IS_OK(result))
459 d_printf("Successfully granted rights.\n");
462 if ( !NT_STATUS_IS_OK(result) ) {
463 d_fprintf(stderr, "Failed to grant privileges for %s (%s)\n",
464 argv[0], nt_errstr(result));
467 rpccli_lsa_Close(pipe_hnd, mem_ctx, &dom_pol);
472 /********************************************************************
473 ********************************************************************/
475 static NTSTATUS rpc_rights_revoke_internal(const DOM_SID *domain_sid,
476 const char *domain_name,
477 struct cli_state *cli,
478 struct rpc_pipe_client *pipe_hnd,
484 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
489 d_printf("Usage: net rpc rights revoke <name|SID> <rights...>\n");
493 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
494 if (!NT_STATUS_IS_OK(result))
497 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True,
498 SEC_RIGHTS_MAXIMUM_ALLOWED,
501 if (!NT_STATUS_IS_OK(result))
504 result = rpccli_lsa_remove_account_rights(pipe_hnd, mem_ctx, &dom_pol, sid,
505 False, argc-1, argv+1);
507 if (!NT_STATUS_IS_OK(result))
510 d_printf("Successfully revoked rights.\n");
513 if ( !NT_STATUS_IS_OK(result) ) {
514 d_fprintf(stderr, "Failed to revoke privileges for %s (%s)\n",
515 argv[0], nt_errstr(result));
518 rpccli_lsa_Close(pipe_hnd, mem_ctx, &dom_pol);
524 /********************************************************************
525 ********************************************************************/
527 static int rpc_rights_list( int argc, const char **argv )
529 return run_rpc_command( NULL, PI_LSARPC, 0,
530 rpc_rights_list_internal, argc, argv );
533 /********************************************************************
534 ********************************************************************/
536 static int rpc_rights_grant( int argc, const char **argv )
538 return run_rpc_command( NULL, PI_LSARPC, 0,
539 rpc_rights_grant_internal, argc, argv );
542 /********************************************************************
543 ********************************************************************/
545 static int rpc_rights_revoke( int argc, const char **argv )
547 return run_rpc_command( NULL, PI_LSARPC, 0,
548 rpc_rights_revoke_internal, argc, argv );
551 /********************************************************************
552 ********************************************************************/
554 static int net_help_rights( int argc, const char **argv )
556 d_printf("net rpc rights list [{accounts|privileges} [name|SID]] View available or assigned privileges\n");
557 d_printf("net rpc rights grant <name|SID> <right> Assign privilege[s]\n");
558 d_printf("net rpc rights revoke <name|SID> <right> Revoke privilege[s]\n");
560 d_printf("\nBoth 'grant' and 'revoke' require a SID and a list of privilege names.\n");
561 d_printf("For example\n");
562 d_printf("\n net rpc rights grant 'VALE\\biddle' SePrintOperatorPrivilege SeDiskOperatorPrivilege\n");
563 d_printf("\nwould grant the printer admin and disk manager rights to the user 'VALE\\biddle'\n\n");
569 /********************************************************************
570 ********************************************************************/
572 int net_rpc_rights(int argc, const char **argv)
574 struct functable func[] = {
575 {"list", rpc_rights_list},
576 {"grant", rpc_rights_grant},
577 {"revoke", rpc_rights_revoke},
582 return net_run_function( argc, argv, func, net_help_rights );
584 return net_help_rights( argc, argv );
587 static NTSTATUS rpc_sh_rights_list(TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
588 struct rpc_pipe_client *pipe_hnd,
589 int argc, const char **argv)
591 return rpc_rights_list_internal(ctx->domain_sid, ctx->domain_name,
592 ctx->cli, pipe_hnd, mem_ctx,
596 static NTSTATUS rpc_sh_rights_grant(TALLOC_CTX *mem_ctx,
597 struct rpc_sh_ctx *ctx,
598 struct rpc_pipe_client *pipe_hnd,
599 int argc, const char **argv)
601 return rpc_rights_grant_internal(ctx->domain_sid, ctx->domain_name,
602 ctx->cli, pipe_hnd, mem_ctx,
606 static NTSTATUS rpc_sh_rights_revoke(TALLOC_CTX *mem_ctx,
607 struct rpc_sh_ctx *ctx,
608 struct rpc_pipe_client *pipe_hnd,
609 int argc, const char **argv)
611 return rpc_rights_revoke_internal(ctx->domain_sid, ctx->domain_name,
612 ctx->cli, pipe_hnd, mem_ctx,
616 struct rpc_sh_cmd *net_rpc_rights_cmds(TALLOC_CTX *mem_ctx,
617 struct rpc_sh_ctx *ctx)
619 static struct rpc_sh_cmd cmds[] = {
621 { "list", NULL, PI_LSARPC, rpc_sh_rights_list,
622 "View available or assigned privileges" },
624 { "grant", NULL, PI_LSARPC, rpc_sh_rights_grant,
625 "Assign privilege[s]" },
627 { "revoke", NULL, PI_LSARPC, rpc_sh_rights_revoke,
628 "Revoke privilege[s]" },
630 { NULL, NULL, 0, NULL, NULL }