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/ndr_lsa_c.h"
23 #include "rpc_client/cli_lsarpc.h"
24 #include "rpc_client/init_lsa.h"
25 #include "../libcli/security/security.h"
27 /********************************************************************
28 ********************************************************************/
30 static NTSTATUS sid_to_name(struct rpc_pipe_client *pipe_hnd,
35 struct policy_handle pol;
36 enum lsa_SidType *sid_types = NULL;
37 NTSTATUS status, result;
38 char **domains = NULL, **names = NULL;
39 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
41 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
42 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
44 if ( !NT_STATUS_IS_OK(status) )
47 status = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &pol, 1, sid, &domains, &names, &sid_types);
49 if ( NT_STATUS_IS_OK(status) ) {
51 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
53 fstrcpy( name, names[0] );
56 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
60 /********************************************************************
61 ********************************************************************/
63 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
65 struct dom_sid *sid, const char *name)
67 struct policy_handle pol;
68 enum lsa_SidType *sid_types;
69 NTSTATUS status, result;
71 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
73 /* maybe its a raw SID */
74 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
78 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
79 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
81 if ( !NT_STATUS_IS_OK(status) )
84 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &pol, 1, &name,
85 NULL, 1, &sids, &sid_types);
87 if ( NT_STATUS_IS_OK(status) )
88 sid_copy( sid, &sids[0] );
90 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
94 /********************************************************************
95 ********************************************************************/
97 static NTSTATUS enum_privileges(struct rpc_pipe_client *pipe_hnd,
99 struct policy_handle *pol )
101 NTSTATUS status, result;
102 uint32 enum_context = 0;
103 uint32 pref_max_length=0x1000;
106 uint16 lang_id_sys=0;
108 struct lsa_StringLarge *description = NULL;
109 struct lsa_PrivArray priv_array;
110 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
112 status = dcerpc_lsa_EnumPrivs(b, ctx,
119 if ( !NT_STATUS_IS_OK(status) )
121 if (!NT_STATUS_IS_OK(result)) {
127 for (i = 0; i < priv_array.count; i++) {
129 struct lsa_String lsa_name;
132 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*" );
134 /* try to get the description */
136 init_lsa_String(&lsa_name, priv_array.privs[i].name.string);
138 status = dcerpc_lsa_LookupPrivDisplayName(b, ctx,
146 if (!NT_STATUS_IS_OK(status)) {
147 d_printf("??????\n");
150 if (!NT_STATUS_IS_OK(result)) {
151 d_printf("??????\n");
155 d_printf("%s\n", description->string);
161 /********************************************************************
162 ********************************************************************/
164 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
166 struct policy_handle *pol,
170 NTSTATUS status, result;
171 struct lsa_RightSet rights;
173 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
175 status = dcerpc_lsa_EnumAccountRights(b, ctx,
180 if (!NT_STATUS_IS_OK(status)) {
183 if (!NT_STATUS_IS_OK(result)) {
187 if (rights.count == 0) {
188 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
191 for (i = 0; i < rights.count; i++) {
192 if (StrCaseCmp(rights.names[i].string, right) == 0) {
197 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
200 /********************************************************************
201 ********************************************************************/
203 static NTSTATUS enum_privileges_for_user(struct rpc_pipe_client *pipe_hnd,
205 struct policy_handle *pol,
206 struct dom_sid *sid )
208 NTSTATUS status, result;
209 struct lsa_RightSet rights;
211 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
213 status = dcerpc_lsa_EnumAccountRights(b, ctx,
218 if (!NT_STATUS_IS_OK(status))
220 if (!NT_STATUS_IS_OK(result))
223 if (rights.count == 0) {
224 d_printf(_("No privileges assigned\n"));
227 for (i = 0; i < rights.count; i++) {
228 printf("%s\n", rights.names[i].string);
234 /********************************************************************
235 ********************************************************************/
237 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
239 struct policy_handle *pol,
240 const char *privilege)
242 NTSTATUS status, result;
243 uint32 enum_context=0;
244 uint32 pref_max_length=0x1000;
245 struct lsa_SidArray sid_array;
248 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
250 status = dcerpc_lsa_EnumAccounts(b, ctx,
256 if (!NT_STATUS_IS_OK(status))
258 if (!NT_STATUS_IS_OK(result))
261 d_printf("%s:\n", privilege);
263 for ( i=0; i<sid_array.num_sids; i++ ) {
265 status = check_privilege_for_user(pipe_hnd, ctx, pol,
266 sid_array.sids[i].sid,
269 if ( ! NT_STATUS_IS_OK(status)) {
270 if ( ! NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
276 /* try to convert the SID to a name. Fall back to
277 printing the raw SID if necessary */
278 status = sid_to_name( pipe_hnd, ctx, sid_array.sids[i].sid, name );
279 if ( !NT_STATUS_IS_OK (status) )
280 sid_to_fstring(name, sid_array.sids[i].sid);
282 d_printf(" %s\n", name);
288 /********************************************************************
289 ********************************************************************/
291 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
293 struct policy_handle *pol)
295 NTSTATUS status, result;
296 uint32 enum_context=0;
297 uint32 pref_max_length=0x1000;
298 struct lsa_SidArray sid_array;
301 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
303 status = dcerpc_lsa_EnumAccounts(b, ctx,
309 if (!NT_STATUS_IS_OK(status))
311 if (!NT_STATUS_IS_OK(result))
314 for ( i=0; i<sid_array.num_sids; i++ ) {
316 /* try to convert the SID to a name. Fall back to
317 printing the raw SID if necessary */
319 status = sid_to_name(pipe_hnd, ctx, sid_array.sids[i].sid, name);
320 if ( !NT_STATUS_IS_OK (status) )
321 sid_to_fstring(name, sid_array.sids[i].sid);
323 d_printf("%s\n", name);
325 status = enum_privileges_for_user(pipe_hnd, ctx, pol,
326 sid_array.sids[i].sid);
327 if ( !NT_STATUS_IS_OK(status) )
336 /********************************************************************
337 ********************************************************************/
339 static NTSTATUS rpc_rights_list_internal(struct net_context *c,
340 const struct dom_sid *domain_sid,
341 const char *domain_name,
342 struct cli_state *cli,
343 struct rpc_pipe_client *pipe_hnd,
348 struct policy_handle pol;
349 NTSTATUS status, result;
352 struct lsa_String lsa_name;
353 struct lsa_StringLarge *description = NULL;
355 uint16 lang_id_sys = 0;
357 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
359 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
360 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
362 if ( !NT_STATUS_IS_OK(status) )
365 /* backwards compatibility; just list available privileges if no arguement */
368 status = enum_privileges(pipe_hnd, mem_ctx, &pol );
372 if (strequal(argv[0], "privileges")) {
375 if (argv[1] == NULL) {
376 status = enum_privileges(pipe_hnd, mem_ctx, &pol );
380 while ( argv[i] != NULL ) {
381 fstrcpy(privname, argv[i]);
382 init_lsa_String(&lsa_name, argv[i]);
385 /* verify that this is a valid privilege for error reporting */
386 status = dcerpc_lsa_LookupPrivDisplayName(b, mem_ctx,
394 if (!NT_STATUS_IS_OK(status)) {
398 if ( !NT_STATUS_IS_OK(result) ) {
399 if ( NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_PRIVILEGE))
400 d_fprintf(stderr, _("No such privilege "
401 "exists: %s.\n"), privname);
403 d_fprintf(stderr, _("Error resolving "
404 "privilege display name "
410 status = enum_accounts_for_privilege(pipe_hnd, mem_ctx, &pol, privname);
411 if (!NT_STATUS_IS_OK(status)) {
412 d_fprintf(stderr, _("Error enumerating "
413 "accounts for privilege %s [%s].\n"),
414 privname, nt_errstr(status));
421 /* special case to enumerate all privileged SIDs with associated rights */
423 if (strequal( argv[0], "accounts")) {
426 if (argv[1] == NULL) {
427 status = enum_privileges_for_accounts(pipe_hnd, mem_ctx, &pol);
431 while (argv[i] != NULL) {
432 status = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[i]);
433 if (!NT_STATUS_IS_OK(status)) {
436 status = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid);
437 if (!NT_STATUS_IS_OK(status)) {
445 /* backward comaptibility: if no keyword provided, treat the key
446 as an account name */
448 d_printf("%s net rpc rights list [[accounts|privileges] "
449 "[name|SID]]\n", _("Usage:"));
450 status = NT_STATUS_OK;
454 status = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
455 if (!NT_STATUS_IS_OK(status)) {
458 status = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid );
461 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
466 /********************************************************************
467 ********************************************************************/
469 static NTSTATUS rpc_rights_grant_internal(struct net_context *c,
470 const struct dom_sid *domain_sid,
471 const char *domain_name,
472 struct cli_state *cli,
473 struct rpc_pipe_client *pipe_hnd,
478 struct policy_handle dom_pol;
479 NTSTATUS status, result;
480 struct lsa_RightSet rights;
482 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
489 _(" net rpc rights grant <name|SID> <rights...>\n"));
493 status = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
494 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
495 status = NT_STATUS_NO_SUCH_USER;
497 if (!NT_STATUS_IS_OK(status))
500 status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
501 SEC_FLAG_MAXIMUM_ALLOWED,
504 if (!NT_STATUS_IS_OK(status))
507 rights.count = argc-1;
508 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
511 return NT_STATUS_NO_MEMORY;
514 for (i=0; i<argc-1; i++) {
515 init_lsa_StringLarge(&rights.names[i], argv[i+1]);
518 status = dcerpc_lsa_AddAccountRights(b, mem_ctx,
523 if (!NT_STATUS_IS_OK(status))
525 if (!NT_STATUS_IS_OK(result)) {
530 d_printf(_("Successfully granted rights.\n"));
533 if ( !NT_STATUS_IS_OK(status) ) {
534 d_fprintf(stderr, _("Failed to grant privileges for %s (%s)\n"),
535 argv[0], nt_errstr(status));
538 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
543 /********************************************************************
544 ********************************************************************/
546 static NTSTATUS rpc_rights_revoke_internal(struct net_context *c,
547 const struct dom_sid *domain_sid,
548 const char *domain_name,
549 struct cli_state *cli,
550 struct rpc_pipe_client *pipe_hnd,
555 struct policy_handle dom_pol;
556 NTSTATUS status, result;
557 struct lsa_RightSet rights;
560 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
565 _(" net rpc rights revoke <name|SID> <rights...>\n"));
569 status = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
570 if (!NT_STATUS_IS_OK(status))
573 status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
574 SEC_FLAG_MAXIMUM_ALLOWED,
577 if (!NT_STATUS_IS_OK(status))
580 rights.count = argc-1;
581 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
584 return NT_STATUS_NO_MEMORY;
587 for (i=0; i<argc-1; i++) {
588 init_lsa_StringLarge(&rights.names[i], argv[i+1]);
591 status = dcerpc_lsa_RemoveAccountRights(b, mem_ctx,
597 if (!NT_STATUS_IS_OK(status))
599 if (!NT_STATUS_IS_OK(result)) {
604 d_printf(_("Successfully revoked rights.\n"));
607 if ( !NT_STATUS_IS_OK(status) ) {
608 d_fprintf(stderr,_("Failed to revoke privileges for %s (%s)\n"),
609 argv[0], nt_errstr(status));
612 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
618 /********************************************************************
619 ********************************************************************/
621 static int rpc_rights_list(struct net_context *c, int argc, const char **argv )
623 if (c->display_usage) {
626 _("net rpc rights list [{accounts|privileges} "
628 " View available/assigned privileges\n"));
632 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
633 rpc_rights_list_internal, argc, argv );
636 /********************************************************************
637 ********************************************************************/
639 static int rpc_rights_grant(struct net_context *c, int argc, const char **argv )
641 if (c->display_usage) {
644 _("net rpc rights grant <name|SID> <right>\n"
645 " Assign privilege[s]\n"));
646 d_printf(_("For example:\n"
647 " net rpc rights grant 'VALE\\biddle' "
648 "SePrintOperatorPrivilege SeDiskOperatorPrivilege\n"
649 " would grant the printer admin and disk manager "
650 "rights to the user 'VALE\\biddle'\n"));
654 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
655 rpc_rights_grant_internal, argc, argv );
658 /********************************************************************
659 ********************************************************************/
661 static int rpc_rights_revoke(struct net_context *c, int argc, const char **argv)
663 if (c->display_usage) {
666 _("net rpc rights revoke <name|SID> <right>\n"
667 " Revoke privilege[s]\n"));
668 d_printf(_("For example:\n"
669 " net rpc rights revoke 'VALE\\biddle' "
670 "SePrintOperatorPrivilege SeDiskOperatorPrivilege\n"
671 " would revoke the printer admin and disk manager"
672 " rights from the user 'VALE\\biddle'\n"));
676 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
677 rpc_rights_revoke_internal, argc, argv );
680 /********************************************************************
681 ********************************************************************/
683 int net_rpc_rights(struct net_context *c, int argc, const char **argv)
685 struct functable func[] = {
690 N_("View available/assigned privileges"),
691 N_("net rpc rights list\n"
692 " View available/assigned privileges")
698 N_("Assign privilege[s]"),
699 N_("net rpc rights grant\n"
700 " Assign privilege[s]")
706 N_("Revoke privilege[s]"),
707 N_("net rpc rights revoke\n"
708 " Revoke privilege[s]")
710 {NULL, NULL, 0, NULL, NULL}
713 return net_run_function(c, argc, argv, "net rpc rights", func);
716 static NTSTATUS rpc_sh_rights_list(struct net_context *c,
717 TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
718 struct rpc_pipe_client *pipe_hnd,
719 int argc, const char **argv)
721 return rpc_rights_list_internal(c, ctx->domain_sid, ctx->domain_name,
722 ctx->cli, pipe_hnd, mem_ctx,
726 static NTSTATUS rpc_sh_rights_grant(struct net_context *c,
728 struct rpc_sh_ctx *ctx,
729 struct rpc_pipe_client *pipe_hnd,
730 int argc, const char **argv)
732 return rpc_rights_grant_internal(c, ctx->domain_sid, ctx->domain_name,
733 ctx->cli, pipe_hnd, mem_ctx,
737 static NTSTATUS rpc_sh_rights_revoke(struct net_context *c,
739 struct rpc_sh_ctx *ctx,
740 struct rpc_pipe_client *pipe_hnd,
741 int argc, const char **argv)
743 return rpc_rights_revoke_internal(c, ctx->domain_sid, ctx->domain_name,
744 ctx->cli, pipe_hnd, mem_ctx,
748 struct rpc_sh_cmd *net_rpc_rights_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
749 struct rpc_sh_ctx *ctx)
751 static struct rpc_sh_cmd cmds[] = {
753 { "list", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_list,
754 N_("View available or assigned privileges") },
756 { "grant", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_grant,
757 N_("Assign privilege[s]") },
759 { "revoke", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_revoke,
760 N_("Revoke privilege[s]") },
762 { NULL, NULL, 0, NULL, NULL }