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 "rpc_client/rpc_client.h"
23 #include "../librpc/gen_ndr/ndr_lsa_c.h"
24 #include "rpc_client/cli_lsarpc.h"
25 #include "rpc_client/init_lsa.h"
26 #include "../libcli/security/security.h"
28 /********************************************************************
29 ********************************************************************/
31 static NTSTATUS sid_to_name(struct rpc_pipe_client *pipe_hnd,
36 struct policy_handle pol;
37 enum lsa_SidType *sid_types = NULL;
38 NTSTATUS status, result;
39 char **domains = NULL, **names = NULL;
40 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
42 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
43 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
45 if ( !NT_STATUS_IS_OK(status) )
48 status = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &pol, 1, sid, &domains, &names, &sid_types);
50 if ( NT_STATUS_IS_OK(status) ) {
52 fstr_sprintf( name, "%s\\%s", domains[0], names[0] );
54 fstrcpy( name, names[0] );
57 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
61 /********************************************************************
62 ********************************************************************/
64 static NTSTATUS name_to_sid(struct rpc_pipe_client *pipe_hnd,
66 struct dom_sid *sid, const char *name)
68 struct policy_handle pol;
69 enum lsa_SidType *sid_types;
70 NTSTATUS status, result;
72 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
74 /* maybe its a raw SID */
75 if ( strncmp(name, "S-", 2) == 0 && string_to_sid(sid, name) ) {
79 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
80 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
82 if ( !NT_STATUS_IS_OK(status) )
85 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &pol, 1, &name,
86 NULL, 1, &sids, &sid_types);
88 if ( NT_STATUS_IS_OK(status) )
89 sid_copy( sid, &sids[0] );
91 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
95 /********************************************************************
96 ********************************************************************/
98 static NTSTATUS enum_privileges(struct rpc_pipe_client *pipe_hnd,
100 struct policy_handle *pol )
102 NTSTATUS status, result;
103 uint32 enum_context = 0;
104 uint32 pref_max_length=0x1000;
107 uint16 lang_id_sys=0;
109 struct lsa_StringLarge *description = NULL;
110 struct lsa_PrivArray priv_array;
111 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
113 status = dcerpc_lsa_EnumPrivs(b, ctx,
120 if ( !NT_STATUS_IS_OK(status) )
122 if (!NT_STATUS_IS_OK(result)) {
128 for (i = 0; i < priv_array.count; i++) {
130 struct lsa_String lsa_name;
133 priv_array.privs[i].name.string ? priv_array.privs[i].name.string : "*unknown*" );
135 /* try to get the description */
137 init_lsa_String(&lsa_name, priv_array.privs[i].name.string);
139 status = dcerpc_lsa_LookupPrivDisplayName(b, ctx,
147 if (!NT_STATUS_IS_OK(status)) {
148 d_printf("??????\n");
151 if (!NT_STATUS_IS_OK(result)) {
152 d_printf("??????\n");
156 d_printf("%s\n", description->string);
162 /********************************************************************
163 ********************************************************************/
165 static NTSTATUS check_privilege_for_user(struct rpc_pipe_client *pipe_hnd,
167 struct policy_handle *pol,
171 NTSTATUS status, result;
172 struct lsa_RightSet rights;
174 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
176 status = dcerpc_lsa_EnumAccountRights(b, ctx,
181 if (!NT_STATUS_IS_OK(status)) {
184 if (!NT_STATUS_IS_OK(result)) {
188 if (rights.count == 0) {
189 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
192 for (i = 0; i < rights.count; i++) {
193 if (strcasecmp_m(rights.names[i].string, right) == 0) {
198 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
201 /********************************************************************
202 ********************************************************************/
204 static NTSTATUS enum_privileges_for_user(struct rpc_pipe_client *pipe_hnd,
206 struct policy_handle *pol,
207 struct dom_sid *sid )
209 NTSTATUS status, result;
210 struct lsa_RightSet rights;
212 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
214 status = dcerpc_lsa_EnumAccountRights(b, ctx,
219 if (!NT_STATUS_IS_OK(status))
221 if (!NT_STATUS_IS_OK(result))
224 if (rights.count == 0) {
225 d_printf(_("No privileges assigned\n"));
228 for (i = 0; i < rights.count; i++) {
229 printf("%s\n", rights.names[i].string);
235 /********************************************************************
236 ********************************************************************/
238 static NTSTATUS enum_accounts_for_privilege(struct rpc_pipe_client *pipe_hnd,
240 struct policy_handle *pol,
241 const char *privilege)
243 NTSTATUS status, result;
244 uint32 enum_context=0;
245 uint32 pref_max_length=0x1000;
246 struct lsa_SidArray sid_array;
249 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
251 status = dcerpc_lsa_EnumAccounts(b, ctx,
257 if (!NT_STATUS_IS_OK(status))
259 if (!NT_STATUS_IS_OK(result))
262 d_printf("%s:\n", privilege);
264 for ( i=0; i<sid_array.num_sids; i++ ) {
266 status = check_privilege_for_user(pipe_hnd, ctx, pol,
267 sid_array.sids[i].sid,
270 if ( ! NT_STATUS_IS_OK(status)) {
271 if ( ! NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
277 /* try to convert the SID to a name. Fall back to
278 printing the raw SID if necessary */
279 status = sid_to_name( pipe_hnd, ctx, sid_array.sids[i].sid, name );
280 if ( !NT_STATUS_IS_OK (status) )
281 sid_to_fstring(name, sid_array.sids[i].sid);
283 d_printf(" %s\n", name);
289 /********************************************************************
290 ********************************************************************/
292 static NTSTATUS enum_privileges_for_accounts(struct rpc_pipe_client *pipe_hnd,
294 struct policy_handle *pol)
296 NTSTATUS status, result;
297 uint32 enum_context=0;
298 uint32 pref_max_length=0x1000;
299 struct lsa_SidArray sid_array;
302 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
304 status = dcerpc_lsa_EnumAccounts(b, ctx,
310 if (!NT_STATUS_IS_OK(status))
312 if (!NT_STATUS_IS_OK(result))
315 for ( i=0; i<sid_array.num_sids; i++ ) {
317 /* try to convert the SID to a name. Fall back to
318 printing the raw SID if necessary */
320 status = sid_to_name(pipe_hnd, ctx, sid_array.sids[i].sid, name);
321 if ( !NT_STATUS_IS_OK (status) )
322 sid_to_fstring(name, sid_array.sids[i].sid);
324 d_printf("%s\n", name);
326 status = enum_privileges_for_user(pipe_hnd, ctx, pol,
327 sid_array.sids[i].sid);
328 if ( !NT_STATUS_IS_OK(status) )
337 /********************************************************************
338 ********************************************************************/
340 static NTSTATUS rpc_rights_list_internal(struct net_context *c,
341 const struct dom_sid *domain_sid,
342 const char *domain_name,
343 struct cli_state *cli,
344 struct rpc_pipe_client *pipe_hnd,
349 struct policy_handle pol;
350 NTSTATUS status, result;
353 struct lsa_String lsa_name;
354 struct lsa_StringLarge *description = NULL;
356 uint16 lang_id_sys = 0;
358 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
360 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
361 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
363 if ( !NT_STATUS_IS_OK(status) )
366 /* backwards compatibility; just list available privileges if no arguement */
369 status = enum_privileges(pipe_hnd, mem_ctx, &pol );
373 if (strequal(argv[0], "privileges")) {
376 if (argv[1] == NULL) {
377 status = enum_privileges(pipe_hnd, mem_ctx, &pol );
381 while ( argv[i] != NULL ) {
382 fstrcpy(privname, argv[i]);
383 init_lsa_String(&lsa_name, argv[i]);
386 /* verify that this is a valid privilege for error reporting */
387 status = dcerpc_lsa_LookupPrivDisplayName(b, mem_ctx,
395 if (!NT_STATUS_IS_OK(status)) {
399 if ( !NT_STATUS_IS_OK(result) ) {
400 if ( NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_PRIVILEGE))
401 d_fprintf(stderr, _("No such privilege "
402 "exists: %s.\n"), privname);
404 d_fprintf(stderr, _("Error resolving "
405 "privilege display name "
411 status = enum_accounts_for_privilege(pipe_hnd, mem_ctx, &pol, privname);
412 if (!NT_STATUS_IS_OK(status)) {
413 d_fprintf(stderr, _("Error enumerating "
414 "accounts for privilege %s [%s].\n"),
415 privname, nt_errstr(status));
422 /* special case to enumerate all privileged SIDs with associated rights */
424 if (strequal( argv[0], "accounts")) {
427 if (argv[1] == NULL) {
428 status = enum_privileges_for_accounts(pipe_hnd, mem_ctx, &pol);
432 while (argv[i] != NULL) {
433 status = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[i]);
434 if (!NT_STATUS_IS_OK(status)) {
437 status = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid);
438 if (!NT_STATUS_IS_OK(status)) {
446 /* backward comaptibility: if no keyword provided, treat the key
447 as an account name */
449 d_printf("%s net rpc rights list [[accounts|privileges] "
450 "[name|SID]]\n", _("Usage:"));
451 status = NT_STATUS_OK;
455 status = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
456 if (!NT_STATUS_IS_OK(status)) {
459 status = enum_privileges_for_user(pipe_hnd, mem_ctx, &pol, &sid );
462 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
467 /********************************************************************
468 ********************************************************************/
470 static NTSTATUS rpc_rights_grant_internal(struct net_context *c,
471 const struct dom_sid *domain_sid,
472 const char *domain_name,
473 struct cli_state *cli,
474 struct rpc_pipe_client *pipe_hnd,
479 struct policy_handle dom_pol;
480 NTSTATUS status, result;
481 struct lsa_RightSet rights;
483 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
490 _(" net rpc rights grant <name|SID> <rights...>\n"));
494 status = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
495 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
496 status = NT_STATUS_NO_SUCH_USER;
498 if (!NT_STATUS_IS_OK(status))
501 status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
502 SEC_FLAG_MAXIMUM_ALLOWED,
505 if (!NT_STATUS_IS_OK(status))
508 rights.count = argc-1;
509 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
512 return NT_STATUS_NO_MEMORY;
515 for (i=0; i<argc-1; i++) {
516 init_lsa_StringLarge(&rights.names[i], argv[i+1]);
519 status = dcerpc_lsa_AddAccountRights(b, mem_ctx,
524 if (!NT_STATUS_IS_OK(status))
526 if (!NT_STATUS_IS_OK(result)) {
531 d_printf(_("Successfully granted rights.\n"));
534 if ( !NT_STATUS_IS_OK(status) ) {
535 d_fprintf(stderr, _("Failed to grant privileges for %s (%s)\n"),
536 argv[0], nt_errstr(status));
539 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
544 /********************************************************************
545 ********************************************************************/
547 static NTSTATUS rpc_rights_revoke_internal(struct net_context *c,
548 const struct dom_sid *domain_sid,
549 const char *domain_name,
550 struct cli_state *cli,
551 struct rpc_pipe_client *pipe_hnd,
556 struct policy_handle dom_pol;
557 NTSTATUS status, result;
558 struct lsa_RightSet rights;
561 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
566 _(" net rpc rights revoke <name|SID> <rights...>\n"));
570 status = name_to_sid(pipe_hnd, mem_ctx, &sid, argv[0]);
571 if (!NT_STATUS_IS_OK(status))
574 status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true,
575 SEC_FLAG_MAXIMUM_ALLOWED,
578 if (!NT_STATUS_IS_OK(status))
581 rights.count = argc-1;
582 rights.names = TALLOC_ARRAY(mem_ctx, struct lsa_StringLarge,
585 return NT_STATUS_NO_MEMORY;
588 for (i=0; i<argc-1; i++) {
589 init_lsa_StringLarge(&rights.names[i], argv[i+1]);
592 status = dcerpc_lsa_RemoveAccountRights(b, mem_ctx,
598 if (!NT_STATUS_IS_OK(status))
600 if (!NT_STATUS_IS_OK(result)) {
605 d_printf(_("Successfully revoked rights.\n"));
608 if ( !NT_STATUS_IS_OK(status) ) {
609 d_fprintf(stderr,_("Failed to revoke privileges for %s (%s)\n"),
610 argv[0], nt_errstr(status));
613 dcerpc_lsa_Close(b, mem_ctx, &dom_pol, &result);
619 /********************************************************************
620 ********************************************************************/
622 static int rpc_rights_list(struct net_context *c, int argc, const char **argv )
624 if (c->display_usage) {
627 _("net rpc rights list [{accounts|privileges} "
629 " View available/assigned privileges\n"));
633 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
634 rpc_rights_list_internal, argc, argv );
637 /********************************************************************
638 ********************************************************************/
640 static int rpc_rights_grant(struct net_context *c, int argc, const char **argv )
642 if (c->display_usage) {
645 _("net rpc rights grant <name|SID> <right>\n"
646 " Assign privilege[s]\n"));
647 d_printf(_("For example:\n"
648 " net rpc rights grant 'VALE\\biddle' "
649 "SePrintOperatorPrivilege SeDiskOperatorPrivilege\n"
650 " would grant the printer admin and disk manager "
651 "rights to the user 'VALE\\biddle'\n"));
655 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
656 rpc_rights_grant_internal, argc, argv );
659 /********************************************************************
660 ********************************************************************/
662 static int rpc_rights_revoke(struct net_context *c, int argc, const char **argv)
664 if (c->display_usage) {
667 _("net rpc rights revoke <name|SID> <right>\n"
668 " Revoke privilege[s]\n"));
669 d_printf(_("For example:\n"
670 " net rpc rights revoke 'VALE\\biddle' "
671 "SePrintOperatorPrivilege SeDiskOperatorPrivilege\n"
672 " would revoke the printer admin and disk manager"
673 " rights from the user 'VALE\\biddle'\n"));
677 return run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
678 rpc_rights_revoke_internal, argc, argv );
681 /********************************************************************
682 ********************************************************************/
684 int net_rpc_rights(struct net_context *c, int argc, const char **argv)
686 struct functable func[] = {
691 N_("View available/assigned privileges"),
692 N_("net rpc rights list\n"
693 " View available/assigned privileges")
699 N_("Assign privilege[s]"),
700 N_("net rpc rights grant\n"
701 " Assign privilege[s]")
707 N_("Revoke privilege[s]"),
708 N_("net rpc rights revoke\n"
709 " Revoke privilege[s]")
711 {NULL, NULL, 0, NULL, NULL}
714 return net_run_function(c, argc, argv, "net rpc rights", func);
717 static NTSTATUS rpc_sh_rights_list(struct net_context *c,
718 TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
719 struct rpc_pipe_client *pipe_hnd,
720 int argc, const char **argv)
722 return rpc_rights_list_internal(c, ctx->domain_sid, ctx->domain_name,
723 ctx->cli, pipe_hnd, mem_ctx,
727 static NTSTATUS rpc_sh_rights_grant(struct net_context *c,
729 struct rpc_sh_ctx *ctx,
730 struct rpc_pipe_client *pipe_hnd,
731 int argc, const char **argv)
733 return rpc_rights_grant_internal(c, ctx->domain_sid, ctx->domain_name,
734 ctx->cli, pipe_hnd, mem_ctx,
738 static NTSTATUS rpc_sh_rights_revoke(struct net_context *c,
740 struct rpc_sh_ctx *ctx,
741 struct rpc_pipe_client *pipe_hnd,
742 int argc, const char **argv)
744 return rpc_rights_revoke_internal(c, ctx->domain_sid, ctx->domain_name,
745 ctx->cli, pipe_hnd, mem_ctx,
749 struct rpc_sh_cmd *net_rpc_rights_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
750 struct rpc_sh_ctx *ctx)
752 static struct rpc_sh_cmd cmds[] = {
754 { "list", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_list,
755 N_("View available or assigned privileges") },
757 { "grant", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_grant,
758 N_("Assign privilege[s]") },
760 { "revoke", NULL, &ndr_table_lsarpc.syntax_id, rpc_sh_rights_revoke,
761 N_("Revoke privilege[s]") },
763 { NULL, NULL, 0, NULL, NULL }