s4:librpc/rpc: add DCERPC_SCHANNEL_AES support
[Samba.git] / source3 / utils / smbcacls.c
blob3d18bee9d51751403c6dd8e654b0efe6c1a223f2
1 /*
2 Unix SMB/CIFS implementation.
3 ACL get/set utility
5 Copyright (C) Andrew Tridgell 2000
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2000
8 Copyright (C) Jelmer Vernooij 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "popt_common.h"
26 #include "rpc_client/cli_pipe.h"
27 #include "../librpc/gen_ndr/ndr_lsa.h"
28 #include "rpc_client/cli_lsarpc.h"
29 #include "../libcli/security/security.h"
30 #include "libsmb/libsmb.h"
31 #include "libsmb/clirap.h"
32 #include "passdb/machine_sid.h"
33 #include "../librpc/gen_ndr/ndr_lsa_c.h"
35 static int test_args;
37 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
39 /* numeric is set when the user wants numeric SIDs and ACEs rather
40 than going via LSA calls to resolve them */
41 static int numeric;
43 static int sddl;
45 static const char *domain_sid = NULL;
47 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
48 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP, REQUEST_INHERIT};
49 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
51 struct perm_value {
52 const char *perm;
53 uint32 mask;
56 /* These values discovered by inspection */
58 static const struct perm_value special_values[] = {
59 { "R", 0x00120089 },
60 { "W", 0x00120116 },
61 { "X", 0x001200a0 },
62 { "D", 0x00010000 },
63 { "P", 0x00040000 },
64 { "O", 0x00080000 },
65 { NULL, 0 },
68 static const struct perm_value standard_values[] = {
69 { "READ", 0x001200a9 },
70 { "CHANGE", 0x001301bf },
71 { "FULL", 0x001f01ff },
72 { NULL, 0 },
75 /* Open cli connection and policy handle */
77 static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
78 const struct dom_sid *sid,
79 TALLOC_CTX *mem_ctx,
80 enum lsa_SidType *type,
81 char **domain, char **name)
83 uint16 orig_cnum = cli_state_get_tid(cli);
84 struct rpc_pipe_client *p = NULL;
85 struct policy_handle handle;
86 NTSTATUS status;
87 TALLOC_CTX *frame = talloc_stackframe();
88 enum lsa_SidType *types;
89 char **domains;
90 char **names;
92 status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
93 if (!NT_STATUS_IS_OK(status)) {
94 goto tcon_fail;
97 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
98 &p);
99 if (!NT_STATUS_IS_OK(status)) {
100 goto fail;
103 status = rpccli_lsa_open_policy(p, talloc_tos(), True,
104 GENERIC_EXECUTE_ACCESS, &handle);
105 if (!NT_STATUS_IS_OK(status)) {
106 goto fail;
109 status = rpccli_lsa_lookup_sids(p, talloc_tos(), &handle, 1, sid,
110 &domains, &names, &types);
111 if (!NT_STATUS_IS_OK(status)) {
112 goto fail;
115 *type = types[0];
116 *domain = talloc_move(mem_ctx, &domains[0]);
117 *name = talloc_move(mem_ctx, &names[0]);
119 status = NT_STATUS_OK;
120 fail:
121 TALLOC_FREE(p);
122 cli_tdis(cli);
123 tcon_fail:
124 cli_state_set_tid(cli, orig_cnum);
125 TALLOC_FREE(frame);
126 return status;
129 static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
130 const char *name,
131 enum lsa_SidType *type,
132 struct dom_sid *sid)
134 uint16 orig_cnum = cli_state_get_tid(cli);
135 struct rpc_pipe_client *p;
136 struct policy_handle handle;
137 NTSTATUS status;
138 TALLOC_CTX *frame = talloc_stackframe();
139 struct dom_sid *sids;
140 enum lsa_SidType *types;
142 status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
143 if (!NT_STATUS_IS_OK(status)) {
144 goto tcon_fail;
147 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
148 &p);
149 if (!NT_STATUS_IS_OK(status)) {
150 goto fail;
153 status = rpccli_lsa_open_policy(p, talloc_tos(), True,
154 GENERIC_EXECUTE_ACCESS, &handle);
155 if (!NT_STATUS_IS_OK(status)) {
156 goto fail;
159 status = rpccli_lsa_lookup_names(p, talloc_tos(), &handle, 1, &name,
160 NULL, 1, &sids, &types);
161 if (!NT_STATUS_IS_OK(status)) {
162 goto fail;
165 *type = types[0];
166 *sid = sids[0];
168 status = NT_STATUS_OK;
169 fail:
170 TALLOC_FREE(p);
171 cli_tdis(cli);
172 tcon_fail:
173 cli_state_set_tid(cli, orig_cnum);
174 TALLOC_FREE(frame);
175 return status;
179 static NTSTATUS cli_lsa_lookup_domain_sid(struct cli_state *cli,
180 struct dom_sid *sid)
182 union lsa_PolicyInformation *info = NULL;
183 uint16 orig_cnum = cli_state_get_tid(cli);
184 struct rpc_pipe_client *rpc_pipe = NULL;
185 struct policy_handle handle;
186 NTSTATUS status, result;
187 TALLOC_CTX *frame = talloc_stackframe();
188 const struct ndr_syntax_id *lsarpc_syntax = &ndr_table_lsarpc.syntax_id;
190 status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
191 if (!NT_STATUS_IS_OK(status)) {
192 goto done;
195 status = cli_rpc_pipe_open_noauth(cli, lsarpc_syntax, &rpc_pipe);
196 if (!NT_STATUS_IS_OK(status)) {
197 goto tdis;
200 status = rpccli_lsa_open_policy(rpc_pipe, frame, True,
201 GENERIC_EXECUTE_ACCESS, &handle);
202 if (!NT_STATUS_IS_OK(status)) {
203 goto tdis;
206 status = dcerpc_lsa_QueryInfoPolicy2(rpc_pipe->binding_handle,
207 frame, &handle,
208 LSA_POLICY_INFO_DOMAIN,
209 &info, &result);
211 if (any_nt_status_not_ok(status, result, &status)) {
212 goto tdis;
215 *sid = *info->domain.sid;
217 tdis:
218 TALLOC_FREE(rpc_pipe);
219 cli_tdis(cli);
220 done:
221 cli_state_set_tid(cli, orig_cnum);
222 TALLOC_FREE(frame);
223 return status;
226 static struct dom_sid *get_domain_sid(struct cli_state *cli)
228 NTSTATUS status;
230 struct dom_sid *sid = talloc(talloc_tos(), struct dom_sid);
231 if (sid == NULL) {
232 DEBUG(0, ("Out of memory\n"));
233 return NULL;
236 if (domain_sid) {
237 if (!dom_sid_parse(domain_sid, sid)) {
238 DEBUG(0,("failed to parse domain sid\n"));
239 TALLOC_FREE(sid);
241 } else {
242 status = cli_lsa_lookup_domain_sid(cli, sid);
244 if (!NT_STATUS_IS_OK(status)) {
245 DEBUG(0,("failed to lookup domain sid: %s\n", nt_errstr(status)));
246 TALLOC_FREE(sid);
251 DEBUG(2,("Domain SID: %s\n", sid_string_dbg(sid)));
252 return sid;
256 /* convert a SID to a string, either numeric or username/group */
257 static void SidToString(struct cli_state *cli, fstring str, const struct dom_sid *sid)
259 char *domain = NULL;
260 char *name = NULL;
261 enum lsa_SidType type;
262 NTSTATUS status;
264 sid_to_fstring(str, sid);
266 if (numeric) {
267 return;
270 status = cli_lsa_lookup_sid(cli, sid, talloc_tos(), &type,
271 &domain, &name);
273 if (!NT_STATUS_IS_OK(status)) {
274 return;
277 if (*domain) {
278 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
279 domain, lp_winbind_separator(), name);
280 } else {
281 fstrcpy(str, name);
285 /* convert a string to a SID, either numeric or username/group */
286 static bool StringToSid(struct cli_state *cli, struct dom_sid *sid, const char *str)
288 enum lsa_SidType type;
290 if (string_to_sid(sid, str)) {
291 return true;
294 return NT_STATUS_IS_OK(cli_lsa_lookup_name(cli, str, &type, sid));
297 static void print_ace_flags(FILE *f, uint8_t flags)
299 char *str = talloc_strdup(NULL, "");
301 if (!str) {
302 goto out;
305 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
306 str = talloc_asprintf(str, "%s%s",
307 str, "OI|");
308 if (!str) {
309 goto out;
312 if (flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
313 str = talloc_asprintf(str, "%s%s",
314 str, "CI|");
315 if (!str) {
316 goto out;
319 if (flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
320 str = talloc_asprintf(str, "%s%s",
321 str, "NP|");
322 if (!str) {
323 goto out;
326 if (flags & SEC_ACE_FLAG_INHERIT_ONLY) {
327 str = talloc_asprintf(str, "%s%s",
328 str, "IO|");
329 if (!str) {
330 goto out;
333 if (flags & SEC_ACE_FLAG_INHERITED_ACE) {
334 str = talloc_asprintf(str, "%s%s",
335 str, "I|");
336 if (!str) {
337 goto out;
340 /* Ignore define SEC_ACE_FLAG_SUCCESSFUL_ACCESS ( 0x40 )
341 and SEC_ACE_FLAG_FAILED_ACCESS ( 0x80 ) as they're
342 audit ace flags. */
344 if (str[strlen(str)-1] == '|') {
345 str[strlen(str)-1] = '\0';
346 fprintf(f, "/%s/", str);
347 } else {
348 fprintf(f, "/0x%x/", flags);
350 TALLOC_FREE(str);
351 return;
353 out:
354 fprintf(f, "/0x%x/", flags);
357 /* print an ACE on a FILE, using either numeric or ascii representation */
358 static void print_ace(struct cli_state *cli, FILE *f, struct security_ace *ace)
360 const struct perm_value *v;
361 fstring sidstr;
362 int do_print = 0;
363 uint32 got_mask;
365 SidToString(cli, sidstr, &ace->trustee);
367 fprintf(f, "%s:", sidstr);
369 if (numeric) {
370 fprintf(f, "%d/0x%x/0x%08x",
371 ace->type, ace->flags, ace->access_mask);
372 return;
375 /* Ace type */
377 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
378 fprintf(f, "ALLOWED");
379 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
380 fprintf(f, "DENIED");
381 } else {
382 fprintf(f, "%d", ace->type);
385 print_ace_flags(f, ace->flags);
387 /* Standard permissions */
389 for (v = standard_values; v->perm; v++) {
390 if (ace->access_mask == v->mask) {
391 fprintf(f, "%s", v->perm);
392 return;
396 /* Special permissions. Print out a hex value if we have
397 leftover bits in the mask. */
399 got_mask = ace->access_mask;
401 again:
402 for (v = special_values; v->perm; v++) {
403 if ((ace->access_mask & v->mask) == v->mask) {
404 if (do_print) {
405 fprintf(f, "%s", v->perm);
407 got_mask &= ~v->mask;
411 if (!do_print) {
412 if (got_mask != 0) {
413 fprintf(f, "0x%08x", ace->access_mask);
414 } else {
415 do_print = 1;
416 goto again;
421 static bool parse_ace_flags(const char *str, unsigned int *pflags)
423 const char *p = str;
424 *pflags = 0;
426 while (*p) {
427 if (strnequal(p, "OI", 2)) {
428 *pflags |= SEC_ACE_FLAG_OBJECT_INHERIT;
429 p += 2;
430 } else if (strnequal(p, "CI", 2)) {
431 *pflags |= SEC_ACE_FLAG_CONTAINER_INHERIT;
432 p += 2;
433 } else if (strnequal(p, "NP", 2)) {
434 *pflags |= SEC_ACE_FLAG_NO_PROPAGATE_INHERIT;
435 p += 2;
436 } else if (strnequal(p, "IO", 2)) {
437 *pflags |= SEC_ACE_FLAG_INHERIT_ONLY;
438 p += 2;
439 } else if (*p == 'I') {
440 *pflags |= SEC_ACE_FLAG_INHERITED_ACE;
441 p += 1;
442 } else if (*p) {
443 return false;
446 switch (*p) {
447 case '|':
448 p++;
449 case '\0':
450 continue;
451 default:
452 return false;
455 return true;
458 /* parse an ACE in the same format as print_ace() */
459 static bool parse_ace(struct cli_state *cli, struct security_ace *ace,
460 const char *orig_str)
462 char *p;
463 const char *cp;
464 char *tok;
465 unsigned int atype = 0;
466 unsigned int aflags = 0;
467 unsigned int amask = 0;
468 struct dom_sid sid;
469 uint32_t mask;
470 const struct perm_value *v;
471 char *str = SMB_STRDUP(orig_str);
472 TALLOC_CTX *frame = talloc_stackframe();
474 if (!str) {
475 TALLOC_FREE(frame);
476 return False;
479 ZERO_STRUCTP(ace);
480 p = strchr_m(str,':');
481 if (!p) {
482 printf("ACE '%s': missing ':'.\n", orig_str);
483 SAFE_FREE(str);
484 TALLOC_FREE(frame);
485 return False;
487 *p = '\0';
488 p++;
489 /* Try to parse numeric form */
491 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
492 StringToSid(cli, &sid, str)) {
493 goto done;
496 /* Try to parse text form */
498 if (!StringToSid(cli, &sid, str)) {
499 printf("ACE '%s': failed to convert '%s' to SID\n",
500 orig_str, str);
501 SAFE_FREE(str);
502 TALLOC_FREE(frame);
503 return False;
506 cp = p;
507 if (!next_token_talloc(frame, &cp, &tok, "/")) {
508 printf("ACE '%s': failed to find '/' character.\n",
509 orig_str);
510 SAFE_FREE(str);
511 TALLOC_FREE(frame);
512 return False;
515 if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
516 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
517 } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
518 atype = SEC_ACE_TYPE_ACCESS_DENIED;
519 } else {
520 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
521 orig_str, tok);
522 SAFE_FREE(str);
523 TALLOC_FREE(frame);
524 return False;
527 /* Only numeric form accepted for flags at present */
529 if (!next_token_talloc(frame, &cp, &tok, "/")) {
530 printf("ACE '%s': bad flags entry at '%s'\n",
531 orig_str, tok);
532 SAFE_FREE(str);
533 TALLOC_FREE(frame);
534 return False;
537 if (tok[0] < '0' || tok[0] > '9') {
538 if (!parse_ace_flags(tok, &aflags)) {
539 printf("ACE '%s': bad named flags entry at '%s'\n",
540 orig_str, tok);
541 SAFE_FREE(str);
542 TALLOC_FREE(frame);
543 return False;
545 } else if (strnequal(tok, "0x", 2)) {
546 if (!sscanf(tok, "%x", &aflags)) {
547 printf("ACE '%s': bad hex flags entry at '%s'\n",
548 orig_str, tok);
549 SAFE_FREE(str);
550 TALLOC_FREE(frame);
551 return False;
553 } else {
554 if (!sscanf(tok, "%i", &aflags)) {
555 printf("ACE '%s': bad integer flags entry at '%s'\n",
556 orig_str, tok);
557 SAFE_FREE(str);
558 TALLOC_FREE(frame);
559 return False;
563 if (!next_token_talloc(frame, &cp, &tok, "/")) {
564 printf("ACE '%s': missing / at '%s'\n",
565 orig_str, tok);
566 SAFE_FREE(str);
567 TALLOC_FREE(frame);
568 return False;
571 if (strncmp(tok, "0x", 2) == 0) {
572 if (sscanf(tok, "%i", &amask) != 1) {
573 printf("ACE '%s': bad hex number at '%s'\n",
574 orig_str, tok);
575 SAFE_FREE(str);
576 TALLOC_FREE(frame);
577 return False;
579 goto done;
582 for (v = standard_values; v->perm; v++) {
583 if (strcmp(tok, v->perm) == 0) {
584 amask = v->mask;
585 goto done;
589 p = tok;
591 while(*p) {
592 bool found = False;
594 for (v = special_values; v->perm; v++) {
595 if (v->perm[0] == *p) {
596 amask |= v->mask;
597 found = True;
601 if (!found) {
602 printf("ACE '%s': bad permission value at '%s'\n",
603 orig_str, p);
604 SAFE_FREE(str);
605 TALLOC_FREE(frame);
606 return False;
608 p++;
611 if (*p) {
612 TALLOC_FREE(frame);
613 SAFE_FREE(str);
614 return False;
617 done:
618 mask = amask;
619 init_sec_ace(ace, &sid, atype, mask, aflags);
620 TALLOC_FREE(frame);
621 SAFE_FREE(str);
622 return True;
625 /* add an ACE to a list of ACEs in a struct security_acl */
626 static bool add_ace(struct security_acl **the_acl, struct security_ace *ace)
628 struct security_acl *new_ace;
629 struct security_ace *aces;
630 if (! *the_acl) {
631 return (((*the_acl) = make_sec_acl(talloc_tos(), 3, 1, ace))
632 != NULL);
635 if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
636 return False;
638 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
639 security_ace));
640 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
641 new_ace = make_sec_acl(talloc_tos(),(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
642 SAFE_FREE(aces);
643 (*the_acl) = new_ace;
644 return True;
647 /* parse a ascii version of a security descriptor */
648 static struct security_descriptor *sec_desc_parse(TALLOC_CTX *ctx, struct cli_state *cli, char *str)
650 const char *p = str;
651 char *tok;
652 struct security_descriptor *ret = NULL;
653 size_t sd_size;
654 struct dom_sid *grp_sid=NULL, *owner_sid=NULL;
655 struct security_acl *dacl=NULL;
656 int revision=1;
658 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
659 if (strncmp(tok,"REVISION:", 9) == 0) {
660 revision = strtol(tok+9, NULL, 16);
661 continue;
664 if (strncmp(tok,"OWNER:", 6) == 0) {
665 if (owner_sid) {
666 printf("Only specify owner once\n");
667 goto done;
669 owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
670 if (!owner_sid ||
671 !StringToSid(cli, owner_sid, tok+6)) {
672 printf("Failed to parse owner sid\n");
673 goto done;
675 continue;
678 if (strncmp(tok,"GROUP:", 6) == 0) {
679 if (grp_sid) {
680 printf("Only specify group once\n");
681 goto done;
683 grp_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
684 if (!grp_sid ||
685 !StringToSid(cli, grp_sid, tok+6)) {
686 printf("Failed to parse group sid\n");
687 goto done;
689 continue;
692 if (strncmp(tok,"ACL:", 4) == 0) {
693 struct security_ace ace;
694 if (!parse_ace(cli, &ace, tok+4)) {
695 goto done;
697 if(!add_ace(&dacl, &ace)) {
698 printf("Failed to add ACL %s\n", tok);
699 goto done;
701 continue;
704 printf("Failed to parse token '%s' in security descriptor,\n", tok);
705 goto done;
708 ret = make_sec_desc(ctx,revision, SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid,
709 NULL, dacl, &sd_size);
711 done:
712 SAFE_FREE(grp_sid);
713 SAFE_FREE(owner_sid);
715 return ret;
718 static const struct {
719 uint16_t mask;
720 const char *str;
721 const char *desc;
722 } sec_desc_ctrl_bits[] = {
723 {SEC_DESC_OWNER_DEFAULTED, "OD", "Owner Defaulted"},
724 {SEC_DESC_GROUP_DEFAULTED, "GD", "Group Defaulted"},
725 {SEC_DESC_DACL_PRESENT, "DP", "DACL Present"},
726 {SEC_DESC_DACL_DEFAULTED, "DD", "DACL Defaulted"},
727 {SEC_DESC_SACL_PRESENT, "SP", "SACL Present"},
728 {SEC_DESC_SACL_DEFAULTED, "SD", "SACL Defaulted"},
729 {SEC_DESC_DACL_TRUSTED, "DT", "DACL Trusted"},
730 {SEC_DESC_SERVER_SECURITY, "SS", "Server Security"},
731 {SEC_DESC_DACL_AUTO_INHERIT_REQ, "DR", "DACL Inheritance Required"},
732 {SEC_DESC_SACL_AUTO_INHERIT_REQ, "SR", "SACL Inheritance Required"},
733 {SEC_DESC_DACL_AUTO_INHERITED, "DI", "DACL Auto Inherited"},
734 {SEC_DESC_SACL_AUTO_INHERITED, "SI", "SACL Auto Inherited"},
735 {SEC_DESC_DACL_PROTECTED, "PD", "DACL Protected"},
736 {SEC_DESC_SACL_PROTECTED, "PS", "SACL Protected"},
737 {SEC_DESC_RM_CONTROL_VALID, "RM", "RM Control Valid"},
738 {SEC_DESC_SELF_RELATIVE , "SR", "Self Relative"},
741 static void print_acl_ctrl(FILE *file, uint16_t ctrl)
743 int i;
744 const char* separator = "";
746 fprintf(file, "CONTROL:");
747 if (numeric) {
748 fprintf(file, "0x%x\n", ctrl);
749 return;
752 for (i = ARRAY_SIZE(sec_desc_ctrl_bits) - 1; i >= 0; i--) {
753 if (ctrl & sec_desc_ctrl_bits[i].mask) {
754 fprintf(file, "%s%s", separator, sec_desc_ctrl_bits[i].str);
755 separator = "|";
758 fputc('\n', file);
761 /* print a ascii version of a security descriptor on a FILE handle */
762 static void sec_desc_print(struct cli_state *cli, FILE *f, struct security_descriptor *sd)
764 fstring sidstr;
765 uint32 i;
767 fprintf(f, "REVISION:%d\n", sd->revision);
768 print_acl_ctrl(f, sd->type);
770 /* Print owner and group sid */
772 if (sd->owner_sid) {
773 SidToString(cli, sidstr, sd->owner_sid);
774 } else {
775 fstrcpy(sidstr, "");
778 fprintf(f, "OWNER:%s\n", sidstr);
780 if (sd->group_sid) {
781 SidToString(cli, sidstr, sd->group_sid);
782 } else {
783 fstrcpy(sidstr, "");
786 fprintf(f, "GROUP:%s\n", sidstr);
788 /* Print aces */
789 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
790 struct security_ace *ace = &sd->dacl->aces[i];
791 fprintf(f, "ACL:");
792 print_ace(cli, f, ace);
793 fprintf(f, "\n");
798 /*****************************************************
799 get fileinfo for filename
800 *******************************************************/
801 static uint16 get_fileinfo(struct cli_state *cli, const char *filename)
803 uint16_t fnum = (uint16_t)-1;
804 uint16 mode = 0;
805 NTSTATUS status;
807 /* The desired access below is the only one I could find that works
808 with NT4, W2KP and Samba */
810 status = cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ,
811 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
812 FILE_OPEN, 0x0, 0x0, &fnum);
813 if (!NT_STATUS_IS_OK(status)) {
814 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
815 return 0;
818 status = cli_qfileinfo_basic(cli, fnum, &mode, NULL, NULL, NULL,
819 NULL, NULL, NULL);
820 if (!NT_STATUS_IS_OK(status)) {
821 printf("Failed to file info %s: %s\n", filename,
822 nt_errstr(status));
825 cli_close(cli, fnum);
827 return mode;
830 /*****************************************************
831 get sec desc for filename
832 *******************************************************/
833 static struct security_descriptor *get_secdesc(struct cli_state *cli, const char *filename)
835 uint16_t fnum = (uint16_t)-1;
836 struct security_descriptor *sd;
837 NTSTATUS status;
839 /* The desired access below is the only one I could find that works
840 with NT4, W2KP and Samba */
842 status = cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ,
843 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
844 FILE_OPEN, 0x0, 0x0, &fnum);
845 if (!NT_STATUS_IS_OK(status)) {
846 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
847 return NULL;
850 status = cli_query_secdesc(cli, fnum, talloc_tos(), &sd);
852 cli_close(cli, fnum);
854 if (!NT_STATUS_IS_OK(status)) {
855 printf("Failed to get security descriptor: %s\n",
856 nt_errstr(status));
857 return NULL;
859 return sd;
862 /*****************************************************
863 set sec desc for filename
864 *******************************************************/
865 static bool set_secdesc(struct cli_state *cli, const char *filename,
866 struct security_descriptor *sd)
868 uint16_t fnum = (uint16_t)-1;
869 bool result=true;
870 NTSTATUS status;
871 uint32_t desired_access = 0;
873 /* Make the desired_access more specific. */
874 if (sd->dacl) {
875 desired_access |= WRITE_DAC_ACCESS;
877 if (sd->sacl) {
878 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
880 if (sd->owner_sid || sd->group_sid) {
881 desired_access |= WRITE_OWNER_ACCESS;
884 status = cli_ntcreate(cli, filename, 0,
885 desired_access,
886 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
887 FILE_OPEN, 0x0, 0x0, &fnum);
888 if (!NT_STATUS_IS_OK(status)) {
889 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
890 return false;
893 status = cli_set_secdesc(cli, fnum, sd);
894 if (!NT_STATUS_IS_OK(status)) {
895 printf("ERROR: security description set failed: %s\n",
896 nt_errstr(status));
897 result=false;
900 cli_close(cli, fnum);
901 return result;
904 /*****************************************************
905 dump the acls for a file
906 *******************************************************/
907 static int cacl_dump(struct cli_state *cli, const char *filename)
909 struct security_descriptor *sd;
911 if (test_args) {
912 return EXIT_OK;
915 sd = get_secdesc(cli, filename);
916 if (sd == NULL) {
917 return EXIT_FAILED;
920 if (sddl) {
921 char *str = sddl_encode(talloc_tos(), sd, get_domain_sid(cli));
922 if (str == NULL) {
923 return EXIT_FAILED;
925 printf("%s\n", str);
926 TALLOC_FREE(str);
927 } else {
928 sec_desc_print(cli, stdout, sd);
931 return EXIT_OK;
934 /*****************************************************
935 Change the ownership or group ownership of a file. Just
936 because the NT docs say this can't be done :-). JRA.
937 *******************************************************/
939 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
940 const char *filename, const char *new_username)
942 struct dom_sid sid;
943 struct security_descriptor *sd, *old;
944 size_t sd_size;
946 if (!StringToSid(cli, &sid, new_username))
947 return EXIT_PARSE_ERROR;
949 old = get_secdesc(cli, filename);
951 if (!old) {
952 return EXIT_FAILED;
955 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
956 (change_mode == REQUEST_CHOWN) ? &sid : NULL,
957 (change_mode == REQUEST_CHGRP) ? &sid : NULL,
958 NULL, NULL, &sd_size);
960 if (!set_secdesc(cli, filename, sd)) {
961 return EXIT_FAILED;
964 return EXIT_OK;
968 /* The MSDN is contradictory over the ordering of ACE entries in an
969 ACL. However NT4 gives a "The information may have been modified
970 by a computer running Windows NT 5.0" if denied ACEs do not appear
971 before allowed ACEs. At
972 http://technet.microsoft.com/en-us/library/cc781716.aspx the
973 canonical order is specified as "Explicit Deny, Explicit Allow,
974 Inherited ACEs unchanged" */
976 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
978 if (sec_ace_equal(ace1, ace2))
979 return 0;
981 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
982 !(ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
983 return 1;
984 if (!(ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
985 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
986 return -1;
987 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
988 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
989 return ace1 - ace2;
991 if (ace1->type != ace2->type)
992 return ace2->type - ace1->type;
994 if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
995 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
997 if (ace1->flags != ace2->flags)
998 return ace1->flags - ace2->flags;
1000 if (ace1->access_mask != ace2->access_mask)
1001 return ace1->access_mask - ace2->access_mask;
1003 if (ace1->size != ace2->size)
1004 return ace1->size - ace2->size;
1006 return memcmp(ace1, ace2, sizeof(struct security_ace));
1009 static void sort_acl(struct security_acl *the_acl)
1011 uint32 i;
1012 if (!the_acl) return;
1014 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
1016 for (i=1;i<the_acl->num_aces;) {
1017 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
1018 int j;
1019 for (j=i; j<the_acl->num_aces-1; j++) {
1020 the_acl->aces[j] = the_acl->aces[j+1];
1022 the_acl->num_aces--;
1023 } else {
1024 i++;
1029 /*****************************************************
1030 set the ACLs on a file given an ascii description
1031 *******************************************************/
1033 static int cacl_set(struct cli_state *cli, const char *filename,
1034 char *the_acl, enum acl_mode mode)
1036 struct security_descriptor *sd, *old;
1037 uint32 i, j;
1038 size_t sd_size;
1039 int result = EXIT_OK;
1041 if (sddl) {
1042 sd = sddl_decode(talloc_tos(), the_acl, get_domain_sid(cli));
1043 } else {
1044 sd = sec_desc_parse(talloc_tos(), cli, the_acl);
1047 if (!sd) return EXIT_PARSE_ERROR;
1048 if (test_args) return EXIT_OK;
1050 old = get_secdesc(cli, filename);
1052 if (!old) {
1053 return EXIT_FAILED;
1056 /* the logic here is rather more complex than I would like */
1057 switch (mode) {
1058 case SMB_ACL_DELETE:
1059 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1060 bool found = False;
1062 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1063 if (sec_ace_equal(&sd->dacl->aces[i],
1064 &old->dacl->aces[j])) {
1065 uint32 k;
1066 for (k=j; k<old->dacl->num_aces-1;k++) {
1067 old->dacl->aces[k] = old->dacl->aces[k+1];
1069 old->dacl->num_aces--;
1070 found = True;
1071 break;
1075 if (!found) {
1076 printf("ACL for ACE:");
1077 print_ace(cli, stdout, &sd->dacl->aces[i]);
1078 printf(" not found\n");
1081 break;
1083 case SMB_ACL_MODIFY:
1084 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1085 bool found = False;
1087 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1088 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
1089 &old->dacl->aces[j].trustee)) {
1090 old->dacl->aces[j] = sd->dacl->aces[i];
1091 found = True;
1095 if (!found) {
1096 fstring str;
1098 SidToString(cli, str,
1099 &sd->dacl->aces[i].trustee);
1100 printf("ACL for SID %s not found\n", str);
1104 if (sd->owner_sid) {
1105 old->owner_sid = sd->owner_sid;
1108 if (sd->group_sid) {
1109 old->group_sid = sd->group_sid;
1112 break;
1114 case SMB_ACL_ADD:
1115 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1116 add_ace(&old->dacl, &sd->dacl->aces[i]);
1118 break;
1120 case SMB_ACL_SET:
1121 old = sd;
1122 break;
1125 /* Denied ACE entries must come before allowed ones */
1126 sort_acl(old->dacl);
1128 /* Create new security descriptor and set it */
1130 /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
1131 But if we're sending an owner, even if it's the same as the one
1132 that already exists then W2K3 insists we open with WRITE_OWNER access.
1133 I need to check that setting a SD with no owner set works against WNT
1134 and W2K. JRA.
1137 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
1138 old->owner_sid, old->group_sid,
1139 NULL, old->dacl, &sd_size);
1141 if (!set_secdesc(cli, filename, sd)) {
1142 result = EXIT_FAILED;
1145 return result;
1148 /*****************************************************
1149 set the inherit on a file
1150 *******************************************************/
1151 static int inherit(struct cli_state *cli, const char *filename,
1152 const char *type)
1154 struct security_descriptor *old,*sd;
1155 uint32 oldattr;
1156 size_t sd_size;
1157 int result = EXIT_OK;
1159 old = get_secdesc(cli, filename);
1161 if (!old) {
1162 return EXIT_FAILED;
1165 oldattr = get_fileinfo(cli,filename);
1167 if (strcmp(type,"allow")==0) {
1168 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
1169 SEC_DESC_DACL_PROTECTED) {
1170 int i;
1171 char *parentname,*temp;
1172 struct security_descriptor *parent;
1173 temp = talloc_strdup(talloc_tos(), filename);
1175 old->type=old->type & (~SEC_DESC_DACL_PROTECTED);
1177 /* look at parent and copy in all its inheritable ACL's. */
1178 string_replace(temp, '\\', '/');
1179 if (!parent_dirname(talloc_tos(),temp,&parentname,NULL)) {
1180 return EXIT_FAILED;
1182 string_replace(parentname, '/', '\\');
1183 parent = get_secdesc(cli,parentname);
1184 if (parent == NULL) {
1185 return EXIT_FAILED;
1187 for (i=0;i<parent->dacl->num_aces;i++) {
1188 struct security_ace *ace=&parent->dacl->aces[i];
1189 /* Add inherited flag to all aces */
1190 ace->flags=ace->flags|
1191 SEC_ACE_FLAG_INHERITED_ACE;
1192 if ((oldattr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
1193 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ==
1194 SEC_ACE_FLAG_CONTAINER_INHERIT) {
1195 add_ace(&old->dacl, ace);
1197 } else {
1198 if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) ==
1199 SEC_ACE_FLAG_OBJECT_INHERIT) {
1200 /* clear flags for files */
1201 ace->flags=0;
1202 add_ace(&old->dacl, ace);
1206 } else {
1207 printf("Already set to inheritable permissions.\n");
1208 return EXIT_FAILED;
1210 } else if (strcmp(type,"remove")==0) {
1211 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
1212 SEC_DESC_DACL_PROTECTED) {
1213 old->type=old->type | SEC_DESC_DACL_PROTECTED;
1215 /* remove all inherited ACL's. */
1216 if (old->dacl) {
1217 int i;
1218 struct security_acl *temp=old->dacl;
1219 old->dacl=make_sec_acl(talloc_tos(), 3, 0, NULL);
1220 for (i=temp->num_aces-1;i>=0;i--) {
1221 struct security_ace *ace=&temp->aces[i];
1222 /* Remove all ace with INHERITED flag set */
1223 if ((ace->flags & SEC_ACE_FLAG_INHERITED_ACE) !=
1224 SEC_ACE_FLAG_INHERITED_ACE) {
1225 add_ace(&old->dacl,ace);
1229 } else {
1230 printf("Already set to no inheritable permissions.\n");
1231 return EXIT_FAILED;
1233 } else if (strcmp(type,"copy")==0) {
1234 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
1235 SEC_DESC_DACL_PROTECTED) {
1236 old->type=old->type | SEC_DESC_DACL_PROTECTED;
1238 /* convert all inherited ACL's to non inherated ACL's. */
1239 if (old->dacl) {
1240 int i;
1241 for (i=0;i<old->dacl->num_aces;i++) {
1242 struct security_ace *ace=&old->dacl->aces[i];
1243 /* Remove INHERITED FLAG from all aces */
1244 ace->flags=ace->flags&(~SEC_ACE_FLAG_INHERITED_ACE);
1247 } else {
1248 printf("Already set to no inheritable permissions.\n");
1249 return EXIT_FAILED;
1253 /* Denied ACE entries must come before allowed ones */
1254 sort_acl(old->dacl);
1256 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
1257 old->owner_sid, old->group_sid,
1258 NULL, old->dacl, &sd_size);
1260 if (!set_secdesc(cli, filename, sd)) {
1261 result = EXIT_FAILED;
1264 return result;
1267 /*****************************************************
1268 Return a connection to a server.
1269 *******************************************************/
1270 static struct cli_state *connect_one(struct user_auth_info *auth_info,
1271 const char *server, const char *share)
1273 struct cli_state *c = NULL;
1274 NTSTATUS nt_status;
1275 uint32_t flags = 0;
1277 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
1278 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1279 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1282 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
1283 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
1284 return NULL;
1287 set_cmdline_auth_info_getpass(auth_info);
1289 nt_status = cli_full_connection(&c, lp_netbios_name(), server,
1290 NULL, 0,
1291 share, "?????",
1292 get_cmdline_auth_info_username(auth_info),
1293 lp_workgroup(),
1294 get_cmdline_auth_info_password(auth_info),
1295 flags,
1296 get_cmdline_auth_info_signing_state(auth_info));
1297 if (!NT_STATUS_IS_OK(nt_status)) {
1298 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
1299 return NULL;
1302 if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
1303 nt_status = cli_cm_force_encryption(c,
1304 get_cmdline_auth_info_username(auth_info),
1305 get_cmdline_auth_info_password(auth_info),
1306 lp_workgroup(),
1307 share);
1308 if (!NT_STATUS_IS_OK(nt_status)) {
1309 cli_shutdown(c);
1310 c = NULL;
1314 return c;
1317 /****************************************************************************
1318 main program
1319 ****************************************************************************/
1320 int main(int argc, const char *argv[])
1322 char *share;
1323 int opt;
1324 enum acl_mode mode = SMB_ACL_SET;
1325 static char *the_acl = NULL;
1326 enum chown_mode change_mode = REQUEST_NONE;
1327 int result;
1328 char *path;
1329 char *filename = NULL;
1330 poptContext pc;
1331 struct poptOption long_options[] = {
1332 POPT_AUTOHELP
1333 { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
1334 { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
1335 { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an acl", "ACL" },
1336 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
1337 { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
1338 { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
1339 { "inherit", 'I', POPT_ARG_STRING, NULL, 'I', "Inherit allow|remove|copy" },
1340 { "numeric", 0, POPT_ARG_NONE, &numeric, 1, "Don't resolve sids or masks to names" },
1341 { "sddl", 0, POPT_ARG_NONE, &sddl, 1, "Output and input acls in sddl format" },
1342 { "test-args", 't', POPT_ARG_NONE, &test_args, 1, "Test arguments"},
1343 { "domain-sid", 0, POPT_ARG_STRING, &domain_sid, 0, "Domain SID for sddl", "SID"},
1344 POPT_COMMON_SAMBA
1345 POPT_COMMON_CONNECTION
1346 POPT_COMMON_CREDENTIALS
1347 POPT_TABLEEND
1350 struct cli_state *cli;
1351 TALLOC_CTX *frame = talloc_stackframe();
1352 const char *owner_username = "";
1353 char *server;
1354 struct user_auth_info *auth_info;
1356 load_case_tables();
1358 /* set default debug level to 1 regardless of what smb.conf sets */
1359 setup_logging( "smbcacls", DEBUG_STDERR);
1360 lp_set_cmdline("log level", "1");
1362 setlinebuf(stdout);
1364 lp_load_global(get_dyn_CONFIGFILE());
1365 load_interfaces();
1367 auth_info = user_auth_info_init(frame);
1368 if (auth_info == NULL) {
1369 exit(1);
1371 popt_common_set_auth_info(auth_info);
1373 pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
1375 poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
1376 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
1378 while ((opt = poptGetNextOpt(pc)) != -1) {
1379 switch (opt) {
1380 case 'S':
1381 the_acl = smb_xstrdup(poptGetOptArg(pc));
1382 mode = SMB_ACL_SET;
1383 break;
1385 case 'D':
1386 the_acl = smb_xstrdup(poptGetOptArg(pc));
1387 mode = SMB_ACL_DELETE;
1388 break;
1390 case 'M':
1391 the_acl = smb_xstrdup(poptGetOptArg(pc));
1392 mode = SMB_ACL_MODIFY;
1393 break;
1395 case 'a':
1396 the_acl = smb_xstrdup(poptGetOptArg(pc));
1397 mode = SMB_ACL_ADD;
1398 break;
1400 case 'C':
1401 owner_username = poptGetOptArg(pc);
1402 change_mode = REQUEST_CHOWN;
1403 break;
1405 case 'G':
1406 owner_username = poptGetOptArg(pc);
1407 change_mode = REQUEST_CHGRP;
1408 break;
1410 case 'I':
1411 owner_username = poptGetOptArg(pc);
1412 change_mode = REQUEST_INHERIT;
1413 break;
1417 /* Make connection to server */
1418 if(!poptPeekArg(pc)) {
1419 poptPrintUsage(pc, stderr, 0);
1420 return -1;
1423 path = talloc_strdup(frame, poptGetArg(pc));
1424 if (!path) {
1425 return -1;
1428 if(!poptPeekArg(pc)) {
1429 poptPrintUsage(pc, stderr, 0);
1430 return -1;
1433 filename = talloc_strdup(frame, poptGetArg(pc));
1434 if (!filename) {
1435 return -1;
1438 string_replace(path,'/','\\');
1440 server = talloc_strdup(frame, path+2);
1441 if (!server) {
1442 return -1;
1444 share = strchr_m(server,'\\');
1445 if (!share) {
1446 printf("Invalid argument: %s\n", share);
1447 return -1;
1450 *share = 0;
1451 share++;
1453 if (!test_args) {
1454 cli = connect_one(auth_info, server, share);
1455 if (!cli) {
1456 exit(EXIT_FAILED);
1458 } else {
1459 exit(0);
1462 string_replace(filename, '/', '\\');
1463 if (filename[0] != '\\') {
1464 filename = talloc_asprintf(frame,
1465 "\\%s",
1466 filename);
1467 if (!filename) {
1468 return -1;
1472 /* Perform requested action */
1474 if (change_mode == REQUEST_INHERIT) {
1475 result = inherit(cli, filename, owner_username);
1476 } else if (change_mode != REQUEST_NONE) {
1477 result = owner_set(cli, change_mode, filename, owner_username);
1478 } else if (the_acl) {
1479 result = cacl_set(cli, filename, the_acl, mode);
1480 } else {
1481 result = cacl_dump(cli, filename);
1484 TALLOC_FREE(frame);
1486 return result;