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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "utils/net.h"
23 /********************************************************************
24 ********************************************************************/
26 static NTSTATUS sid_to_name(struct rpc_pipe_client *pipe_hnd,
34 char **domains, **names;
36 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
37 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
39 if ( !NT_STATUS_IS_OK(result) )
42 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &pol, 1, sid, &domains, &names, &sid_types);
44 if ( NT_STATUS_IS_OK(result) ) {
46 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
48 fstrcpy( name, names[0] );
51 rpccli_lsa_close(pipe_hnd, mem_ctx, &pol);
55 /********************************************************************
56 ********************************************************************/
58 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
60 DOM_SID *sid, const char *name)
67 /* maybe its a raw SID */
68 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
72 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
73 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
75 if ( !NT_STATUS_IS_OK(result) )
78 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &pol, 1, &name, &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;
103 uint16 lang_id_sys=0;
107 result = rpccli_lsa_enum_privilege(pipe_hnd, ctx, pol, &enum_context,
108 pref_max_length, &count, &privs_name, &privs_high, &privs_low);
110 if ( !NT_STATUS_IS_OK(result) )
115 for (i = 0; i < count; i++) {
116 d_printf("%30s ", privs_name[i] ? privs_name[i] : "*unknown*" );
118 /* try to get the description */
120 if ( !NT_STATUS_IS_OK(rpccli_lsa_get_dispname(pipe_hnd, ctx, pol,
121 privs_name[i], lang_id, lang_id_sys, description, &lang_id_desc)) )
123 d_printf("??????\n");
127 d_printf("%s\n", description );
133 /********************************************************************
134 ********************************************************************/
136 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
147 result = rpccli_lsa_enum_account_rights(pipe_hnd, ctx, pol, sid, &count, &rights);
149 if (!NT_STATUS_IS_OK(result)) {
154 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
157 for (i = 0; i < count; i++) {
158 if (StrCaseCmp(rights[i], right) == 0) {
163 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
166 /********************************************************************
167 ********************************************************************/
169 static NTSTATUS enum_privileges_for_user(struct rpc_pipe_client *pipe_hnd,
179 result = rpccli_lsa_enum_account_rights(pipe_hnd, ctx, pol, sid, &count, &rights);
181 if (!NT_STATUS_IS_OK(result))
185 d_printf("No privileges assigned\n");
187 for (i = 0; i < count; i++) {
188 printf("%s\n", rights[i]);
194 /********************************************************************
195 ********************************************************************/
197 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
200 const char *privilege)
203 uint32 enum_context=0;
204 uint32 pref_max_length=0x1000;
210 result = rpccli_lsa_enum_sids(pipe_hnd, ctx, pol, &enum_context,
211 pref_max_length, &count, &sids);
213 if (!NT_STATUS_IS_OK(result))
216 d_printf("%s:\n", privilege);
218 for ( i=0; i<count; i++ ) {
221 result = check_privilege_for_user( pipe_hnd, ctx, pol, &sids[i], privilege);
223 if ( ! NT_STATUS_IS_OK(result)) {
224 if ( ! NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
230 /* try to convert the SID to a name. Fall back to
231 printing the raw SID if necessary */
232 result = sid_to_name( pipe_hnd, ctx, &sids[i], name );
233 if ( !NT_STATUS_IS_OK (result) )
234 fstrcpy( name, sid_string_static(&sids[i]) );
236 d_printf(" %s\n", name);
242 /********************************************************************
243 ********************************************************************/
245 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
250 uint32 enum_context=0;
251 uint32 pref_max_length=0x1000;
257 result = rpccli_lsa_enum_sids(pipe_hnd, ctx, pol, &enum_context,
258 pref_max_length, &count, &sids);
260 if (!NT_STATUS_IS_OK(result))
263 for ( i=0; i<count; i++ ) {
265 /* try to convert the SID to a name. Fall back to
266 printing the raw SID if necessary */
268 result = sid_to_name(pipe_hnd, ctx, &sids[i], name );
269 if ( !NT_STATUS_IS_OK (result) )
270 fstrcpy( name, sid_string_static(&sids[i]) );
272 d_printf("%s\n", name);
274 result = enum_privileges_for_user(pipe_hnd, ctx, pol, &sids[i] );
276 if ( !NT_STATUS_IS_OK(result) )
285 /********************************************************************
286 ********************************************************************/
288 static NTSTATUS rpc_rights_list_internal(const DOM_SID *domain_sid,
289 const char *domain_name,
290 struct cli_state *cli,
291 struct rpc_pipe_client *pipe_hnd,
302 uint16 lang_id_sys = 0;
306 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
307 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
309 if ( !NT_STATUS_IS_OK(result) )
312 /* backwards compatibility; just list available privileges if no arguement */
315 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
319 if (strequal(argv[0], "privileges")) {
322 if (argv[1] == NULL) {
323 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
327 while ( argv[i] != NULL ) {
328 fstrcpy( privname, argv[i] );
331 /* verify that this is a valid privilege for error reporting */
333 result = rpccli_lsa_get_dispname(pipe_hnd, mem_ctx, &pol, privname, lang_id,
334 lang_id_sys, description, &lang_id_desc);
336 if ( !NT_STATUS_IS_OK(result) ) {
337 if ( NT_STATUS_EQUAL( result, NT_STATUS_NO_SUCH_PRIVILEGE ) )
338 d_fprintf(stderr, "No such privilege exists: %s.\n", privname);
340 d_fprintf(stderr, "Error resolving privilege display name [%s].\n", nt_errstr(result));
344 result = enum_accounts_for_privilege(pipe_hnd, mem_ctx, &pol, privname);
345 if (!NT_STATUS_IS_OK(result)) {
346 d_fprintf(stderr, "Error enumerating accounts for privilege %s [%s].\n",
347 privname, nt_errstr(result));
354 /* special case to enumerate all privileged SIDs with associated rights */
356 if (strequal( argv[0], "accounts")) {
359 if (argv[1] == NULL) {
360 result = enum_privileges_for_accounts(pipe_hnd, mem_ctx, &pol);
364 while (argv[i] != NULL) {
365 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[i]);
366 if (!NT_STATUS_IS_OK(result)) {
369 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid);
370 if (!NT_STATUS_IS_OK(result)) {
378 /* backward comaptibility: if no keyword provided, treat the key
379 as an account name */
381 d_printf("Usage: net rpc rights list [[accounts|privileges] [name|SID]]\n");
382 result = NT_STATUS_OK;
386 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
387 if (!NT_STATUS_IS_OK(result)) {
390 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid );
393 rpccli_lsa_close(pipe_hnd, mem_ctx, &pol);
398 /********************************************************************
399 ********************************************************************/
401 static NTSTATUS rpc_rights_grant_internal(const DOM_SID *domain_sid,
402 const char *domain_name,
403 struct cli_state *cli,
404 struct rpc_pipe_client *pipe_hnd,
410 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
415 d_printf("Usage: net rpc rights grant <name|SID> <rights...>\n");
419 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
420 if (!NT_STATUS_IS_OK(result))
423 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True,
424 SEC_RIGHTS_MAXIMUM_ALLOWED,
427 if (!NT_STATUS_IS_OK(result))
430 result = rpccli_lsa_add_account_rights(pipe_hnd, mem_ctx, &dom_pol, sid,
433 if (!NT_STATUS_IS_OK(result))
436 d_printf("Successfully granted rights.\n");
439 if ( !NT_STATUS_IS_OK(result) ) {
440 d_fprintf(stderr, "Failed to grant privileges for %s (%s)\n",
441 argv[0], nt_errstr(result));
444 rpccli_lsa_close(pipe_hnd, mem_ctx, &dom_pol);
449 /********************************************************************
450 ********************************************************************/
452 static NTSTATUS rpc_rights_revoke_internal(const DOM_SID *domain_sid,
453 const char *domain_name,
454 struct cli_state *cli,
455 struct rpc_pipe_client *pipe_hnd,
461 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
466 d_printf("Usage: net rpc rights revoke <name|SID> <rights...>\n");
470 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
471 if (!NT_STATUS_IS_OK(result))
474 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True,
475 SEC_RIGHTS_MAXIMUM_ALLOWED,
478 if (!NT_STATUS_IS_OK(result))
481 result = rpccli_lsa_remove_account_rights(pipe_hnd, mem_ctx, &dom_pol, sid,
482 False, argc-1, argv+1);
484 if (!NT_STATUS_IS_OK(result))
487 d_printf("Successfully revoked rights.\n");
490 if ( !NT_STATUS_IS_OK(result) ) {
491 d_fprintf(stderr, "Failed to revoke privileges for %s (%s)",
492 argv[0], nt_errstr(result));
495 rpccli_lsa_close(pipe_hnd, mem_ctx, &dom_pol);
501 /********************************************************************
502 ********************************************************************/
504 static int rpc_rights_list( int argc, const char **argv )
506 return run_rpc_command( NULL, PI_LSARPC, 0,
507 rpc_rights_list_internal, argc, argv );
510 /********************************************************************
511 ********************************************************************/
513 static int rpc_rights_grant( int argc, const char **argv )
515 return run_rpc_command( NULL, PI_LSARPC, 0,
516 rpc_rights_grant_internal, argc, argv );
519 /********************************************************************
520 ********************************************************************/
522 static int rpc_rights_revoke( int argc, const char **argv )
524 return run_rpc_command( NULL, PI_LSARPC, 0,
525 rpc_rights_revoke_internal, argc, argv );
528 /********************************************************************
529 ********************************************************************/
531 static int net_help_rights( int argc, const char **argv )
533 d_printf("net rpc rights list [{accounts|privileges} [name|SID]] View available or assigned privileges\n");
534 d_printf("net rpc rights grant <name|SID> <right> Assign privilege[s]\n");
535 d_printf("net rpc rights revoke <name|SID> <right> Revoke privilege[s]\n");
537 d_printf("\nBoth 'grant' and 'revoke' require a SID and a list of privilege names.\n");
538 d_printf("For example\n");
539 d_printf("\n net rpc rights grant 'VALE\\biddle' SePrintOperatorPrivilege SeDiskOperatorPrivilege\n");
540 d_printf("\nwould grant the printer admin and disk manager rights to the user 'VALE\\biddle'\n\n");
546 /********************************************************************
547 ********************************************************************/
549 int net_rpc_rights(int argc, const char **argv)
551 struct functable func[] = {
552 {"list", rpc_rights_list},
553 {"grant", rpc_rights_grant},
554 {"revoke", rpc_rights_revoke},
559 return net_run_function( argc, argv, func, net_help_rights );
561 return net_help_rights( argc, argv );