[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / utils / net_rpc_rights.c
blob11c4991e7d76d7b3916d6ab7ad62c1c889875d30
1 /*
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. */
20 #include "includes.h"
21 #include "utils/net.h"
23 /********************************************************************
24 ********************************************************************/
26 static NTSTATUS sid_to_name(struct rpc_pipe_client *pipe_hnd,
27 TALLOC_CTX *mem_ctx,
28 DOM_SID *sid,
29 fstring name)
31 POLICY_HND pol;
32 enum lsa_SidType *sid_types;
33 NTSTATUS result;
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) )
40 return 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) ) {
45 if ( *domains[0] )
46 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
47 else
48 fstrcpy( name, names[0] );
51 rpccli_lsa_close(pipe_hnd, mem_ctx, &pol);
52 return result;
55 /********************************************************************
56 ********************************************************************/
58 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
59 TALLOC_CTX *mem_ctx,
60 DOM_SID *sid, const char *name)
62 POLICY_HND pol;
63 enum lsa_SidType *sid_types;
64 NTSTATUS result;
65 DOM_SID *sids;
67 /* maybe its a raw SID */
68 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
69 return NT_STATUS_OK;
72 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
73 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
75 if ( !NT_STATUS_IS_OK(result) )
76 return result;
78 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &pol, 1, &name,
79 NULL, &sids, &sid_types);
81 if ( NT_STATUS_IS_OK(result) )
82 sid_copy( sid, &sids[0] );
84 rpccli_lsa_close(pipe_hnd, mem_ctx, &pol);
85 return result;
88 /********************************************************************
89 ********************************************************************/
91 static NTSTATUS enum_privileges(struct rpc_pipe_client *pipe_hnd,
92 TALLOC_CTX *ctx,
93 POLICY_HND *pol )
95 NTSTATUS result;
96 uint32 enum_context = 0;
97 uint32 pref_max_length=0x1000;
98 uint32 count=0;
99 char **privs_name;
100 uint32 *privs_high;
101 uint32 *privs_low;
102 int i;
103 uint16 lang_id=0;
104 uint16 lang_id_sys=0;
105 uint16 lang_id_desc;
106 fstring description;
108 result = rpccli_lsa_enum_privilege(pipe_hnd, ctx, pol, &enum_context,
109 pref_max_length, &count, &privs_name, &privs_high, &privs_low);
111 if ( !NT_STATUS_IS_OK(result) )
112 return result;
114 /* Print results */
116 for (i = 0; i < count; i++) {
117 d_printf("%30s ", privs_name[i] ? privs_name[i] : "*unknown*" );
119 /* try to get the description */
121 if ( !NT_STATUS_IS_OK(rpccli_lsa_get_dispname(pipe_hnd, ctx, pol,
122 privs_name[i], lang_id, lang_id_sys, description, &lang_id_desc)) )
124 d_printf("??????\n");
125 continue;
128 d_printf("%s\n", description );
131 return NT_STATUS_OK;
134 /********************************************************************
135 ********************************************************************/
137 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
138 TALLOC_CTX *ctx,
139 POLICY_HND *pol,
140 DOM_SID *sid,
141 const char *right)
143 NTSTATUS result;
144 uint32 count;
145 char **rights;
146 int i;
148 result = rpccli_lsa_enum_account_rights(pipe_hnd, ctx, pol, sid, &count, &rights);
150 if (!NT_STATUS_IS_OK(result)) {
151 return result;
154 if (count == 0) {
155 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
158 for (i = 0; i < count; i++) {
159 if (StrCaseCmp(rights[i], right) == 0) {
160 return NT_STATUS_OK;
164 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
167 /********************************************************************
168 ********************************************************************/
170 static NTSTATUS enum_privileges_for_user(struct rpc_pipe_client *pipe_hnd,
171 TALLOC_CTX *ctx,
172 POLICY_HND *pol,
173 DOM_SID *sid )
175 NTSTATUS result;
176 uint32 count;
177 char **rights;
178 int i;
180 result = rpccli_lsa_enum_account_rights(pipe_hnd, ctx, pol, sid, &count, &rights);
182 if (!NT_STATUS_IS_OK(result))
183 return result;
185 if ( count == 0 )
186 d_printf("No privileges assigned\n");
188 for (i = 0; i < count; i++) {
189 printf("%s\n", rights[i]);
192 return NT_STATUS_OK;
195 /********************************************************************
196 ********************************************************************/
198 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
199 TALLOC_CTX *ctx,
200 POLICY_HND *pol,
201 const char *privilege)
203 NTSTATUS result;
204 uint32 enum_context=0;
205 uint32 pref_max_length=0x1000;
206 DOM_SID *sids;
207 uint32 count=0;
208 int i;
209 fstring name;
211 result = rpccli_lsa_enum_sids(pipe_hnd, ctx, pol, &enum_context,
212 pref_max_length, &count, &sids);
214 if (!NT_STATUS_IS_OK(result))
215 return result;
217 d_printf("%s:\n", privilege);
219 for ( i=0; i<count; i++ ) {
222 result = check_privilege_for_user( pipe_hnd, ctx, pol, &sids[i], privilege);
224 if ( ! NT_STATUS_IS_OK(result)) {
225 if ( ! NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
226 return result;
228 continue;
231 /* try to convert the SID to a name. Fall back to
232 printing the raw SID if necessary */
233 result = sid_to_name( pipe_hnd, ctx, &sids[i], name );
234 if ( !NT_STATUS_IS_OK (result) )
235 fstrcpy( name, sid_string_static(&sids[i]) );
237 d_printf(" %s\n", name);
240 return NT_STATUS_OK;
243 /********************************************************************
244 ********************************************************************/
246 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
247 TALLOC_CTX *ctx,
248 POLICY_HND *pol)
250 NTSTATUS result;
251 uint32 enum_context=0;
252 uint32 pref_max_length=0x1000;
253 DOM_SID *sids;
254 uint32 count=0;
255 int i;
256 fstring name;
258 result = rpccli_lsa_enum_sids(pipe_hnd, ctx, pol, &enum_context,
259 pref_max_length, &count, &sids);
261 if (!NT_STATUS_IS_OK(result))
262 return result;
264 for ( i=0; i<count; i++ ) {
266 /* try to convert the SID to a name. Fall back to
267 printing the raw SID if necessary */
269 result = sid_to_name(pipe_hnd, ctx, &sids[i], name );
270 if ( !NT_STATUS_IS_OK (result) )
271 fstrcpy( name, sid_string_static(&sids[i]) );
273 d_printf("%s\n", name);
275 result = enum_privileges_for_user(pipe_hnd, ctx, pol, &sids[i] );
277 if ( !NT_STATUS_IS_OK(result) )
278 return result;
280 d_printf("\n");
283 return NT_STATUS_OK;
286 /********************************************************************
287 ********************************************************************/
289 static NTSTATUS rpc_rights_list_internal(const DOM_SID *domain_sid,
290 const char *domain_name,
291 struct cli_state *cli,
292 struct rpc_pipe_client *pipe_hnd,
293 TALLOC_CTX *mem_ctx,
294 int argc,
295 const char **argv )
297 POLICY_HND pol;
298 NTSTATUS result;
299 DOM_SID sid;
300 fstring privname;
301 fstring description;
302 uint16 lang_id = 0;
303 uint16 lang_id_sys = 0;
304 uint16 lang_id_desc;
307 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
308 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
310 if ( !NT_STATUS_IS_OK(result) )
311 return result;
313 /* backwards compatibility; just list available privileges if no arguement */
315 if (argc == 0) {
316 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
317 goto done;
320 if (strequal(argv[0], "privileges")) {
321 int i = 1;
323 if (argv[1] == NULL) {
324 result = enum_privileges(pipe_hnd, mem_ctx, &pol );
325 goto done;
328 while ( argv[i] != NULL ) {
329 fstrcpy( privname, argv[i] );
330 i++;
332 /* verify that this is a valid privilege for error reporting */
334 result = rpccli_lsa_get_dispname(pipe_hnd, mem_ctx, &pol, privname, lang_id,
335 lang_id_sys, description, &lang_id_desc);
337 if ( !NT_STATUS_IS_OK(result) ) {
338 if ( NT_STATUS_EQUAL( result, NT_STATUS_NO_SUCH_PRIVILEGE ) )
339 d_fprintf(stderr, "No such privilege exists: %s.\n", privname);
340 else
341 d_fprintf(stderr, "Error resolving privilege display name [%s].\n", nt_errstr(result));
342 continue;
345 result = enum_accounts_for_privilege(pipe_hnd, mem_ctx, &pol, privname);
346 if (!NT_STATUS_IS_OK(result)) {
347 d_fprintf(stderr, "Error enumerating accounts for privilege %s [%s].\n",
348 privname, nt_errstr(result));
349 continue;
352 goto done;
355 /* special case to enumerate all privileged SIDs with associated rights */
357 if (strequal( argv[0], "accounts")) {
358 int i = 1;
360 if (argv[1] == NULL) {
361 result = enum_privileges_for_accounts(pipe_hnd, mem_ctx, &pol);
362 goto done;
365 while (argv[i] != NULL) {
366 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[i]);
367 if (!NT_STATUS_IS_OK(result)) {
368 goto done;
370 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid);
371 if (!NT_STATUS_IS_OK(result)) {
372 goto done;
374 i++;
376 goto done;
379 /* backward comaptibility: if no keyword provided, treat the key
380 as an account name */
381 if (argc > 1) {
382 d_printf("Usage: net rpc rights list [[accounts|privileges] [name|SID]]\n");
383 result = NT_STATUS_OK;
384 goto done;
387 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
388 if (!NT_STATUS_IS_OK(result)) {
389 goto done;
391 result = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid );
393 done:
394 rpccli_lsa_close(pipe_hnd, mem_ctx, &pol);
396 return result;
399 /********************************************************************
400 ********************************************************************/
402 static NTSTATUS rpc_rights_grant_internal(const DOM_SID *domain_sid,
403 const char *domain_name,
404 struct cli_state *cli,
405 struct rpc_pipe_client *pipe_hnd,
406 TALLOC_CTX *mem_ctx,
407 int argc,
408 const char **argv )
410 POLICY_HND dom_pol;
411 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
413 DOM_SID sid;
415 if (argc < 2 ) {
416 d_printf("Usage: net rpc rights grant <name|SID> <rights...>\n");
417 return NT_STATUS_OK;
420 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
421 if (!NT_STATUS_IS_OK(result))
422 return result;
424 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True,
425 SEC_RIGHTS_MAXIMUM_ALLOWED,
426 &dom_pol);
428 if (!NT_STATUS_IS_OK(result))
429 return result;
431 result = rpccli_lsa_add_account_rights(pipe_hnd, mem_ctx, &dom_pol, sid,
432 argc-1, argv+1);
434 if (!NT_STATUS_IS_OK(result))
435 goto done;
437 d_printf("Successfully granted rights.\n");
439 done:
440 if ( !NT_STATUS_IS_OK(result) ) {
441 d_fprintf(stderr, "Failed to grant privileges for %s (%s)\n",
442 argv[0], nt_errstr(result));
445 rpccli_lsa_close(pipe_hnd, mem_ctx, &dom_pol);
447 return result;
450 /********************************************************************
451 ********************************************************************/
453 static NTSTATUS rpc_rights_revoke_internal(const DOM_SID *domain_sid,
454 const char *domain_name,
455 struct cli_state *cli,
456 struct rpc_pipe_client *pipe_hnd,
457 TALLOC_CTX *mem_ctx,
458 int argc,
459 const char **argv )
461 POLICY_HND dom_pol;
462 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
464 DOM_SID sid;
466 if (argc < 2 ) {
467 d_printf("Usage: net rpc rights revoke <name|SID> <rights...>\n");
468 return NT_STATUS_OK;
471 result = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
472 if (!NT_STATUS_IS_OK(result))
473 return result;
475 result = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True,
476 SEC_RIGHTS_MAXIMUM_ALLOWED,
477 &dom_pol);
479 if (!NT_STATUS_IS_OK(result))
480 return result;
482 result = rpccli_lsa_remove_account_rights(pipe_hnd, mem_ctx, &dom_pol, sid,
483 False, argc-1, argv+1);
485 if (!NT_STATUS_IS_OK(result))
486 goto done;
488 d_printf("Successfully revoked rights.\n");
490 done:
491 if ( !NT_STATUS_IS_OK(result) ) {
492 d_fprintf(stderr, "Failed to revoke privileges for %s (%s)\n",
493 argv[0], nt_errstr(result));
496 rpccli_lsa_close(pipe_hnd, mem_ctx, &dom_pol);
498 return result;
502 /********************************************************************
503 ********************************************************************/
505 static int rpc_rights_list( int argc, const char **argv )
507 return run_rpc_command( NULL, PI_LSARPC, 0,
508 rpc_rights_list_internal, argc, argv );
511 /********************************************************************
512 ********************************************************************/
514 static int rpc_rights_grant( int argc, const char **argv )
516 return run_rpc_command( NULL, PI_LSARPC, 0,
517 rpc_rights_grant_internal, argc, argv );
520 /********************************************************************
521 ********************************************************************/
523 static int rpc_rights_revoke( int argc, const char **argv )
525 return run_rpc_command( NULL, PI_LSARPC, 0,
526 rpc_rights_revoke_internal, argc, argv );
529 /********************************************************************
530 ********************************************************************/
532 static int net_help_rights( int argc, const char **argv )
534 d_printf("net rpc rights list [{accounts|privileges} [name|SID]] View available or assigned privileges\n");
535 d_printf("net rpc rights grant <name|SID> <right> Assign privilege[s]\n");
536 d_printf("net rpc rights revoke <name|SID> <right> Revoke privilege[s]\n");
538 d_printf("\nBoth 'grant' and 'revoke' require a SID and a list of privilege names.\n");
539 d_printf("For example\n");
540 d_printf("\n net rpc rights grant 'VALE\\biddle' SePrintOperatorPrivilege SeDiskOperatorPrivilege\n");
541 d_printf("\nwould grant the printer admin and disk manager rights to the user 'VALE\\biddle'\n\n");
544 return -1;
547 /********************************************************************
548 ********************************************************************/
550 int net_rpc_rights(int argc, const char **argv)
552 struct functable func[] = {
553 {"list", rpc_rights_list},
554 {"grant", rpc_rights_grant},
555 {"revoke", rpc_rights_revoke},
556 {NULL, NULL}
559 if ( argc )
560 return net_run_function( argc, argv, func, net_help_rights );
562 return net_help_rights( argc, argv );
565 static NTSTATUS rpc_sh_rights_list(TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
566 struct rpc_pipe_client *pipe_hnd,
567 int argc, const char **argv)
569 return rpc_rights_list_internal(ctx->domain_sid, ctx->domain_name,
570 ctx->cli, pipe_hnd, mem_ctx,
571 argc, argv);
574 static NTSTATUS rpc_sh_rights_grant(TALLOC_CTX *mem_ctx,
575 struct rpc_sh_ctx *ctx,
576 struct rpc_pipe_client *pipe_hnd,
577 int argc, const char **argv)
579 return rpc_rights_grant_internal(ctx->domain_sid, ctx->domain_name,
580 ctx->cli, pipe_hnd, mem_ctx,
581 argc, argv);
584 static NTSTATUS rpc_sh_rights_revoke(TALLOC_CTX *mem_ctx,
585 struct rpc_sh_ctx *ctx,
586 struct rpc_pipe_client *pipe_hnd,
587 int argc, const char **argv)
589 return rpc_rights_revoke_internal(ctx->domain_sid, ctx->domain_name,
590 ctx->cli, pipe_hnd, mem_ctx,
591 argc, argv);
594 struct rpc_sh_cmd *net_rpc_rights_cmds(TALLOC_CTX *mem_ctx,
595 struct rpc_sh_ctx *ctx)
597 static struct rpc_sh_cmd cmds[] = {
599 { "list", NULL, PI_LSARPC, rpc_sh_rights_list,
600 "View available or assigned privileges" },
602 { "grant", NULL, PI_LSARPC, rpc_sh_rights_grant,
603 "Assign privilege[s]" },
605 { "revoke", NULL, PI_LSARPC, rpc_sh_rights_revoke,
606 "Revoke privilege[s]" },
608 { NULL, NULL, 0, NULL, NULL }
611 return cmds;