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 static void init_lsa_String(struct lsa_String *name, const char *s)
27 /********************************************************************
28 ********************************************************************/
30 static NTSTATUS sid_to_name(struct rpc_pipe_client *pipe_hnd,
36 enum lsa_SidType *sid_types = NULL;
38 char **domains = NULL, **names = NULL;
40 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
41 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
43 if ( !NT_STATUS_IS_OK(result) )
46 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &pol, 1, sid, &domains, &names, &sid_types);
48 if ( NT_STATUS_IS_OK(result) ) {
50 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
52 fstrcpy( name, names[0] );
55 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
59 /********************************************************************
60 ********************************************************************/
62 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
64 DOM_SID *sid, const char *name)
67 enum lsa_SidType *sid_types;
71 /* maybe its a raw SID */
72 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
76 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
77 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
79 if ( !NT_STATUS_IS_OK(result) )
82 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &pol, 1, &name,
83 NULL, 1, &sids, &sid_types);
85 if ( NT_STATUS_IS_OK(result) )
86 sid_copy( sid, &sids[0] );
88 rpccli_lsa_Close(pipe_hnd, mem_ctx, &pol);
92 /********************************************************************
93 ********************************************************************/
95 static NTSTATUS enum_privileges(struct rpc_pipe_client *pipe_hnd,
100 uint32 enum_context = 0;
101 uint32 pref_max_length=0x1000;
104 uint16 lang_id_sys=0;
106 struct lsa_StringLarge *description = NULL;
107 struct lsa_PrivArray priv_array;
109 result = rpccli_lsa_EnumPrivs(pipe_hnd, ctx,
115 if ( !NT_STATUS_IS_OK(result) )
120 for (i = 0; i < priv_array.count; i++) {
122 struct lsa_String lsa_name;
125 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*" );
127 /* try to get the description */
129 init_lsa_String(&lsa_name, priv_array.privs[i].name.string);
131 result = rpccli_lsa_LookupPrivDisplayName(pipe_hnd, ctx,
139 if (!NT_STATUS_IS_OK(result)) {
140 d_printf("??????\n");
144 d_printf("%s\n", description->string);
150 /********************************************************************
151 ********************************************************************/
153 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
164 result = rpccli_lsa_enum_account_rights(pipe_hnd, ctx, pol, sid, &count, &rights);
166 if (!NT_STATUS_IS_OK(result)) {
171 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
174 for (i = 0; i < count; i++) {
175 if (StrCaseCmp(rights[i], 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,
196 result = rpccli_lsa_enum_account_rights(pipe_hnd, ctx, pol, sid, &count, &rights);
198 if (!NT_STATUS_IS_OK(result))
202 d_printf("No privileges assigned\n");
204 for (i = 0; i < count; i++) {
205 printf("%s\n", rights[i]);
211 /********************************************************************
212 ********************************************************************/
214 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
217 const char *privilege)
220 uint32 enum_context=0;
221 uint32 pref_max_length=0x1000;
222 DOM_SID *sids = NULL;
227 result = rpccli_lsa_enum_sids(pipe_hnd, ctx, pol, &enum_context,
228 pref_max_length, &count, &sids);
230 if (!NT_STATUS_IS_OK(result))
233 d_printf("%s:\n", privilege);
235 for ( i=0; i<count; i++ ) {
238 result = check_privilege_for_user( pipe_hnd, ctx, pol, &sids[i], privilege);
240 if ( ! NT_STATUS_IS_OK(result)) {
241 if ( ! NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
247 /* try to convert the SID to a name. Fall back to
248 printing the raw SID if necessary */
249 result = sid_to_name( pipe_hnd, ctx, &sids[i], name );
250 if ( !NT_STATUS_IS_OK (result) )
251 sid_to_fstring(name, &sids[i]);
253 d_printf(" %s\n", name);
259 /********************************************************************
260 ********************************************************************/
262 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
267 uint32 enum_context=0;
268 uint32 pref_max_length=0x1000;
274 result = rpccli_lsa_enum_sids(pipe_hnd, ctx, pol, &enum_context,
275 pref_max_length, &count, &sids);
277 if (!NT_STATUS_IS_OK(result))
280 for ( i=0; i<count; 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, &sids[i], name );
286 if ( !NT_STATUS_IS_OK (result) )
287 sid_to_fstring(name, &sids[i]);
289 d_printf("%s\n", name);
291 result = enum_privileges_for_user(pipe_hnd, ctx, pol, &sids[i] );
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 }