2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2000
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2000
8 Copyright (C) Jelmer Vernooij 2003
9 Copyright (C) Noel Power <noel.power@suse.com> 2013
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "popt_common_cmdline.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "../librpc/gen_ndr/ndr_lsa.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../libcli/security/security.h"
31 #include "libsmb/libsmb.h"
32 #include "libsmb/clirap.h"
33 #include "passdb/machine_sid.h"
34 #include "../librpc/gen_ndr/ndr_lsa_c.h"
37 static char DIRSEP_CHAR = '\\';
39 static int inheritance = 0;
42 static int query_sec_info = -1;
43 static int set_sec_info = -1;
44 static bool want_mxac;
46 static const char *domain_sid = NULL;
48 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
49 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP, REQUEST_INHERIT};
50 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
52 struct cacl_callback_state {
53 struct user_auth_info *auth_info;
54 struct cli_state *cli;
55 struct security_descriptor *aclsd;
56 struct security_acl *acl_to_add;
59 bool acl_no_propagate;
64 * if this dfs link is local to this share then we need to
65 * adjust targetpath. A local dfs link is prepended with
66 * '/$SERVER/$SHARE/path_from_args' The 'full' path is not
67 * suitable for passing to cli_list (and will fail)
69 static NTSTATUS local_cli_resolve_path(TALLOC_CTX* ctx,
71 struct cli_credentials *creds,
72 struct cli_state *rootcli,
74 struct cli_state **targetcli,
81 status = cli_resolve_path(ctx,
88 if (!NT_STATUS_IS_OK(status)) {
92 search = talloc_asprintf(ctx, "\\%s\\%s",
93 rootcli->server_domain,
96 return NT_STATUS_NO_MEMORY;
99 searchlen = strlen(search);
101 if (strncmp(*pp_targetpath, search, searchlen) == 0) {
102 *pp_targetpath += searchlen;
107 static NTSTATUS cli_lsa_lookup_domain_sid(struct cli_state *cli,
110 union lsa_PolicyInformation *info = NULL;
111 struct smbXcli_tcon *orig_tcon = NULL;
112 struct rpc_pipe_client *rpc_pipe = NULL;
113 struct policy_handle handle;
114 NTSTATUS status, result;
115 TALLOC_CTX *frame = talloc_stackframe();
117 if (cli_state_has_tcon(cli)) {
118 orig_tcon = cli_state_save_tcon(cli);
119 if (orig_tcon == NULL) {
120 status = NT_STATUS_NO_MEMORY;
125 status = cli_tree_connect(cli, "IPC$", "?????", NULL);
126 if (!NT_STATUS_IS_OK(status)) {
130 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc, &rpc_pipe);
131 if (!NT_STATUS_IS_OK(status)) {
135 status = rpccli_lsa_open_policy(rpc_pipe, frame, True,
136 GENERIC_EXECUTE_ACCESS, &handle);
137 if (!NT_STATUS_IS_OK(status)) {
141 status = dcerpc_lsa_QueryInfoPolicy2(rpc_pipe->binding_handle,
143 LSA_POLICY_INFO_DOMAIN,
146 if (any_nt_status_not_ok(status, result, &status)) {
150 *sid = *info->domain.sid;
153 TALLOC_FREE(rpc_pipe);
156 cli_state_restore_tcon(cli, orig_tcon);
161 static struct dom_sid *get_domain_sid(struct cli_state *cli)
164 struct dom_sid_buf buf;
166 struct dom_sid *sid = talloc(talloc_tos(), struct dom_sid);
168 DEBUG(0, ("Out of memory\n"));
173 if (!dom_sid_parse(domain_sid, sid)) {
174 DEBUG(0,("failed to parse domain sid\n"));
178 status = cli_lsa_lookup_domain_sid(cli, sid);
180 if (!NT_STATUS_IS_OK(status)) {
181 DEBUG(0,("failed to lookup domain sid: %s\n", nt_errstr(status)));
187 DEBUG(2,("Domain SID: %s\n", dom_sid_str_buf(sid, &buf)));
191 /* add an ACE to a list of ACEs in a struct security_acl */
192 static bool add_ace_with_ctx(TALLOC_CTX *ctx, struct security_acl **the_acl,
193 const struct security_ace *ace)
196 struct security_acl *acl = *the_acl;
199 acl = make_sec_acl(ctx, 3, 0, NULL);
205 if (acl->num_aces == UINT32_MAX) {
209 acl, struct security_ace, *ace, &acl->aces, &acl->num_aces);
214 static bool add_ace(struct security_acl **the_acl, struct security_ace *ace)
216 return add_ace_with_ctx(talloc_tos(), the_acl, ace);
219 /* parse a ascii version of a security descriptor */
220 static struct security_descriptor *sec_desc_parse(TALLOC_CTX *ctx, struct cli_state *cli, char *str)
224 struct security_descriptor *ret = NULL;
226 struct dom_sid owner_sid = { .num_auths = 0 };
227 bool have_owner = false;
228 struct dom_sid group_sid = { .num_auths = 0 };
229 bool have_group = false;
230 struct security_acl *dacl=NULL;
233 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
234 if (strncmp(tok,"REVISION:", 9) == 0) {
235 revision = strtol(tok+9, NULL, 16);
239 if (strncmp(tok,"OWNER:", 6) == 0) {
241 printf("Only specify owner once\n");
244 if (!StringToSid(cli, &owner_sid, tok+6)) {
245 printf("Failed to parse owner sid\n");
252 if (strncmp(tok,"GROUP:", 6) == 0) {
254 printf("Only specify group once\n");
257 if (!StringToSid(cli, &group_sid, tok+6)) {
258 printf("Failed to parse group sid\n");
265 if (strncmp(tok,"ACL:", 4) == 0) {
266 struct security_ace ace;
267 if (!parse_ace(cli, &ace, tok+4)) {
270 if(!add_ace(&dacl, &ace)) {
271 printf("Failed to add ACL %s\n", tok);
277 printf("Failed to parse token '%s' in security descriptor,\n", tok);
284 SEC_DESC_SELF_RELATIVE,
285 have_owner ? &owner_sid : NULL,
286 have_group ? &group_sid : NULL,
295 /*****************************************************
296 get fileinfo for filename
297 *******************************************************/
298 static uint16_t get_fileinfo(struct cli_state *cli, const char *filename)
300 uint16_t fnum = (uint16_t)-1;
302 struct smb_create_returns cr = {0};
304 /* The desired access below is the only one I could find that works
305 with NT4, W2KP and Samba */
307 status = cli_ntcreate(
309 filename, /* fname */
311 READ_CONTROL_ACCESS, /* CreatFlags */
312 0, /* FileAttributes */
314 FILE_SHARE_WRITE, /* ShareAccess */
315 FILE_OPEN, /* CreateDisposition */
316 0x0, /* CreateOptions */
317 0x0, /* SecurityFlags */
320 if (!NT_STATUS_IS_OK(status)) {
321 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
325 cli_close(cli, fnum);
326 return cr.file_attributes;
329 /*****************************************************
330 get sec desc for filename
331 *******************************************************/
332 static struct security_descriptor *get_secdesc_with_ctx(TALLOC_CTX *ctx,
333 struct cli_state *cli,
334 const char *filename)
336 uint16_t fnum = (uint16_t)-1;
337 struct security_descriptor *sd;
340 uint32_t desired_access = 0;
342 if (query_sec_info == -1) {
343 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
345 sec_info = query_sec_info;
348 if (sec_info & (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL)) {
349 desired_access |= SEC_STD_READ_CONTROL;
351 if (sec_info & SECINFO_SACL) {
352 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
355 if (desired_access == 0) {
356 desired_access |= SEC_STD_READ_CONTROL;
359 status = cli_ntcreate(cli, filename, 0, desired_access,
360 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
361 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
362 if (!NT_STATUS_IS_OK(status)) {
363 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
367 status = cli_query_security_descriptor(cli, fnum, sec_info,
370 cli_close(cli, fnum);
372 if (!NT_STATUS_IS_OK(status)) {
373 printf("Failed to get security descriptor: %s\n",
380 static struct security_descriptor *get_secdesc(struct cli_state *cli,
381 const char *filename)
383 return get_secdesc_with_ctx(talloc_tos(), cli, filename);
385 /*****************************************************
386 set sec desc for filename
387 *******************************************************/
388 static bool set_secdesc(struct cli_state *cli, const char *filename,
389 struct security_descriptor *sd)
391 uint16_t fnum = (uint16_t)-1;
394 uint32_t desired_access = 0;
397 if (set_sec_info == -1) {
400 if (sd->dacl || (sd->type & SEC_DESC_DACL_PRESENT)) {
401 sec_info |= SECINFO_DACL;
403 if (sd->sacl || (sd->type & SEC_DESC_SACL_PRESENT)) {
404 sec_info |= SECINFO_SACL;
407 sec_info |= SECINFO_OWNER;
410 sec_info |= SECINFO_GROUP;
413 sec_info = set_sec_info;
416 /* Make the desired_access more specific. */
417 if (sec_info & SECINFO_DACL) {
418 desired_access |= SEC_STD_WRITE_DAC;
420 if (sec_info & SECINFO_SACL) {
421 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
423 if (sec_info & (SECINFO_OWNER | SECINFO_GROUP)) {
424 desired_access |= SEC_STD_WRITE_OWNER;
427 status = cli_ntcreate(cli, filename, 0,
429 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
430 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
431 if (!NT_STATUS_IS_OK(status)) {
432 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
436 status = cli_set_security_descriptor(cli, fnum, sec_info, sd);
437 if (!NT_STATUS_IS_OK(status)) {
438 printf("ERROR: security descriptor set failed: %s\n",
443 cli_close(cli, fnum);
447 /*****************************************************
448 get maximum access for a file
449 *******************************************************/
450 static int cacl_mxac(struct cli_state *cli, const char *filename)
455 status = cli_query_mxac(cli, filename, &mxac);
456 if (!NT_STATUS_IS_OK(status)) {
457 printf("Failed to get mxac: %s\n", nt_errstr(status));
461 printf("Maximum access: 0x%x\n", mxac);
467 /*****************************************************
468 dump the acls for a file
469 *******************************************************/
470 static int cacl_dump(struct cli_state *cli, const char *filename, bool numeric)
472 struct security_descriptor *sd;
479 sd = get_secdesc(cli, filename);
485 char *str = sddl_encode(talloc_tos(), sd, get_domain_sid(cli));
492 sec_desc_print(cli, stdout, sd, numeric);
496 ret = cacl_mxac(cli, filename);
497 if (ret != EXIT_OK) {
505 /*****************************************************
506 Change the ownership or group ownership of a file. Just
507 because the NT docs say this can't be done :-). JRA.
508 *******************************************************/
510 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
511 const char *filename, const char *new_username)
514 struct security_descriptor *sd;
517 if (!StringToSid(cli, &sid, new_username))
518 return EXIT_PARSE_ERROR;
520 sd = make_sec_desc(talloc_tos(),
521 SECURITY_DESCRIPTOR_REVISION_1,
522 SEC_DESC_SELF_RELATIVE,
523 (change_mode == REQUEST_CHOWN) ? &sid : NULL,
524 (change_mode == REQUEST_CHGRP) ? &sid : NULL,
525 NULL, NULL, &sd_size);
527 if (!set_secdesc(cli, filename, sd)) {
535 /* The MSDN is contradictory over the ordering of ACE entries in an
536 ACL. However NT4 gives a "The information may have been modified
537 by a computer running Windows NT 5.0" if denied ACEs do not appear
538 before allowed ACEs. At
539 http://technet.microsoft.com/en-us/library/cc781716.aspx the
540 canonical order is specified as "Explicit Deny, Explicit Allow,
541 Inherited ACEs unchanged" */
543 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
545 if (security_ace_equal(ace1, ace2))
548 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
549 !(ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
551 if (!(ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
552 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
554 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
555 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
558 if (ace1->type != ace2->type)
559 return ace2->type - ace1->type;
561 if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
562 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
564 if (ace1->flags != ace2->flags)
565 return ace1->flags - ace2->flags;
567 if (ace1->access_mask != ace2->access_mask)
568 return ace1->access_mask - ace2->access_mask;
570 if (ace1->size != ace2->size)
571 return ace1->size - ace2->size;
573 return memcmp(ace1, ace2, sizeof(struct security_ace));
576 static void sort_acl(struct security_acl *the_acl)
579 if (!the_acl) return;
581 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
583 for (i=1;i<the_acl->num_aces;) {
584 if (security_ace_equal(&the_acl->aces[i-1],
585 &the_acl->aces[i])) {
587 the_acl->aces, i, the_acl->num_aces);
595 /*****************************************************
596 set the ACLs on a file given a security descriptor
597 *******************************************************/
599 static int cacl_set_from_sd(struct cli_state *cli, const char *filename,
600 struct security_descriptor *sd, enum acl_mode mode,
603 struct security_descriptor *old = NULL;
606 int result = EXIT_OK;
608 if (!sd) return EXIT_PARSE_ERROR;
609 if (test_args) return EXIT_OK;
611 if (mode != SMB_ACL_SET) {
613 * Do not fetch old ACL when it will be overwritten
614 * completely with a new one.
616 old = get_secdesc(cli, filename);
623 /* the logic here is rather more complex than I would like */
626 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
629 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
630 if (security_ace_equal(&sd->dacl->aces[i],
631 &old->dacl->aces[j])) {
633 for (k=j; k<old->dacl->num_aces-1;k++) {
634 old->dacl->aces[k] = old->dacl->aces[k+1];
636 old->dacl->num_aces--;
643 printf("ACL for ACE:");
644 print_ace(cli, stdout, &sd->dacl->aces[i],
646 printf(" not found\n");
652 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
655 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
656 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
657 &old->dacl->aces[j].trustee)) {
658 old->dacl->aces[j] = sd->dacl->aces[i];
666 SidToString(cli, str,
667 &sd->dacl->aces[i].trustee,
669 printf("ACL for SID %s not found\n", str);
674 old->owner_sid = sd->owner_sid;
678 old->group_sid = sd->group_sid;
684 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
685 add_ace(&old->dacl, &sd->dacl->aces[i]);
694 /* Denied ACE entries must come before allowed ones */
697 /* Create new security descriptor and set it */
699 /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
700 But if we're sending an owner, even if it's the same as the one
701 that already exists then W2K3 insists we open with WRITE_OWNER access.
702 I need to check that setting a SD with no owner set works against WNT
706 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
707 old->owner_sid, old->group_sid,
708 NULL, old->dacl, &sd_size);
710 if (!set_secdesc(cli, filename, sd)) {
711 result = EXIT_FAILED;
717 /*****************************************************
718 set the ACLs on a file given an ascii description
719 *******************************************************/
721 static int cacl_set(struct cli_state *cli, const char *filename,
722 char *the_acl, enum acl_mode mode, bool numeric)
724 struct security_descriptor *sd = NULL;
727 sd = sddl_decode(talloc_tos(), the_acl, get_global_sam_sid());
729 sd = sec_desc_parse(talloc_tos(), cli, the_acl);
733 return EXIT_PARSE_ERROR;
738 return cacl_set_from_sd(cli, filename, sd, mode, numeric);
741 /*****************************************************
742 set the inherit on a file
743 *******************************************************/
744 static int inherit(struct cli_state *cli, const char *filename,
747 struct security_descriptor *old,*sd;
750 int result = EXIT_OK;
752 old = get_secdesc(cli, filename);
758 oldattr = get_fileinfo(cli,filename);
760 if (strcmp(type,"allow")==0) {
761 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
762 SEC_DESC_DACL_PROTECTED) {
764 char *parentname,*temp;
765 struct security_descriptor *parent;
766 temp = talloc_strdup(talloc_tos(), filename);
768 old->type=old->type & (~SEC_DESC_DACL_PROTECTED);
770 /* look at parent and copy in all its inheritable ACL's. */
771 string_replace(temp, '\\', '/');
772 if (!parent_dirname(talloc_tos(),temp,&parentname,NULL)) {
775 string_replace(parentname, '/', '\\');
776 parent = get_secdesc(cli,parentname);
777 if (parent == NULL) {
780 for (i=0;i<parent->dacl->num_aces;i++) {
781 struct security_ace *ace=&parent->dacl->aces[i];
782 /* Add inherited flag to all aces */
783 ace->flags=ace->flags|
784 SEC_ACE_FLAG_INHERITED_ACE;
785 if ((oldattr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
786 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ==
787 SEC_ACE_FLAG_CONTAINER_INHERIT) {
788 add_ace(&old->dacl, ace);
791 if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) ==
792 SEC_ACE_FLAG_OBJECT_INHERIT) {
793 /* clear flags for files */
795 add_ace(&old->dacl, ace);
800 printf("Already set to inheritable permissions.\n");
803 } else if (strcmp(type,"remove")==0) {
804 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
805 SEC_DESC_DACL_PROTECTED) {
806 old->type=old->type | SEC_DESC_DACL_PROTECTED;
808 /* remove all inherited ACL's. */
811 struct security_acl *temp=old->dacl;
812 old->dacl=make_sec_acl(talloc_tos(), 3, 0, NULL);
813 for (i=temp->num_aces-1;i>=0;i--) {
814 struct security_ace *ace=&temp->aces[i];
815 /* Remove all ace with INHERITED flag set */
816 if ((ace->flags & SEC_ACE_FLAG_INHERITED_ACE) !=
817 SEC_ACE_FLAG_INHERITED_ACE) {
818 add_ace(&old->dacl,ace);
823 printf("Already set to no inheritable permissions.\n");
826 } else if (strcmp(type,"copy")==0) {
827 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
828 SEC_DESC_DACL_PROTECTED) {
829 old->type=old->type | SEC_DESC_DACL_PROTECTED;
832 * convert all inherited ACL's to non
837 for (i=0;i<old->dacl->num_aces;i++) {
838 struct security_ace *ace=&old->dacl->aces[i];
839 /* Remove INHERITED FLAG from all aces */
840 ace->flags=ace->flags&(~SEC_ACE_FLAG_INHERITED_ACE);
844 printf("Already set to no inheritable permissions.\n");
849 /* Denied ACE entries must come before allowed ones */
852 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
853 old->owner_sid, old->group_sid,
854 NULL, old->dacl, &sd_size);
856 if (!set_secdesc(cli, filename, sd)) {
857 result = EXIT_FAILED;
863 /*****************************************************
864 Return a connection to a server.
865 *******************************************************/
866 static struct cli_state *connect_one(struct cli_credentials *creds,
867 const char *server, const char *share)
869 struct cli_state *c = NULL;
873 nt_status = cli_full_connection_creds(&c, lp_netbios_name(), server,
878 if (!NT_STATUS_IS_OK(nt_status)) {
879 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
887 * Process resulting combination of mask & fname ensuring
888 * terminated with wildcard
890 static char *build_dirname(TALLOC_CTX *ctx,
891 const char *mask, char *dir, char *fname)
896 mask2 = talloc_strdup(ctx, mask);
900 p = strrchr_m(mask2, DIRSEP_CHAR);
906 mask2 = talloc_asprintf_append(mask2,
913 * Returns the a copy of the ACL flags in ace modified according
914 * to some inheritance rules.
915 * a) SEC_ACE_FLAG_INHERITED_ACE is propagated to children
916 * b) SEC_ACE_FLAG_INHERIT_ONLY is set on container children for OI (only)
917 * c) SEC_ACE_FLAG_OBJECT_INHERIT & SEC_ACE_FLAG_CONTAINER_INHERIT are
918 * stripped from flags to be propagated to non-container children
919 * d) SEC_ACE_FLAG_OBJECT_INHERIT & SEC_ACE_FLAG_CONTAINER_INHERIT are
920 * stripped from flags to be propagated if the NP flag
921 * SEC_ACE_FLAG_NO_PROPAGATE_INHERIT is present
924 static uint8_t get_flags_to_propagate(bool is_container,
925 struct security_ace *ace)
927 uint8_t newflags = ace->flags;
928 /* OBJECT inheritance */
929 bool acl_objinherit = (ace->flags &
930 SEC_ACE_FLAG_OBJECT_INHERIT) == SEC_ACE_FLAG_OBJECT_INHERIT;
931 /* CONTAINER inheritance */
932 bool acl_cntrinherit = (ace->flags &
933 SEC_ACE_FLAG_CONTAINER_INHERIT) ==
934 SEC_ACE_FLAG_CONTAINER_INHERIT;
935 /* PROHIBIT inheritance */
936 bool prohibit_inheritance = ((ace->flags &
937 SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) ==
938 SEC_ACE_FLAG_NO_PROPAGATE_INHERIT);
940 /* Assume we are not propagating the ACE */
942 newflags &= ~SEC_ACE_FLAG_INHERITED_ACE;
943 /* all children need to have the SEC_ACE_FLAG_INHERITED_ACE set */
944 if (acl_cntrinherit || acl_objinherit) {
946 * object inherit ( alone ) on a container needs
947 * SEC_ACE_FLAG_INHERIT_ONLY
950 if (acl_objinherit && !acl_cntrinherit) {
951 newflags |= SEC_ACE_FLAG_INHERIT_ONLY;
954 * this is tricky, the only time we would not
955 * propagate the ace for a container is if
956 * prohibit_inheritance is set and object inheritance
959 if ((prohibit_inheritance
961 && !acl_cntrinherit) == false) {
962 newflags |= SEC_ACE_FLAG_INHERITED_ACE;
966 * don't apply object/container inheritance flags to
969 newflags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT
970 | SEC_ACE_FLAG_CONTAINER_INHERIT
971 | SEC_ACE_FLAG_INHERIT_ONLY);
973 * only apply ace to file if object inherit
975 if (acl_objinherit) {
976 newflags |= SEC_ACE_FLAG_INHERITED_ACE;
980 /* if NP is specified strip NP and all OI/CI INHERIT flags */
981 if (prohibit_inheritance) {
982 newflags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT
983 | SEC_ACE_FLAG_CONTAINER_INHERIT
984 | SEC_ACE_FLAG_INHERIT_ONLY
985 | SEC_ACE_FLAG_NO_PROPAGATE_INHERIT);
992 * This function builds a new acl for 'caclfile', first it removes any
993 * existing inheritable ace(s) from the current acl of caclfile, secondly it
994 * applies any inheritable acls of the parent of caclfile ( inheritable acls of
995 * caclfile's parent are passed via acl_to_add member of cbstate )
998 static NTSTATUS propagate_inherited_aces(char *caclfile,
999 struct cacl_callback_state *cbstate)
1001 TALLOC_CTX *aclctx = NULL;
1005 struct security_descriptor *old = NULL;
1006 bool is_container = false;
1007 struct security_acl *acl_to_add = cbstate->acl_to_add;
1008 struct security_acl *acl_to_remove = NULL;
1011 aclctx = talloc_new(NULL);
1012 if (aclctx == NULL) {
1013 return NT_STATUS_NO_MEMORY;
1015 old = get_secdesc_with_ctx(aclctx, cbstate->cli, caclfile);
1018 status = NT_STATUS_UNSUCCESSFUL;
1022 /* inhibit propagation? */
1023 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
1024 SEC_DESC_DACL_PROTECTED){
1025 status = NT_STATUS_OK;
1029 fileattr = get_fileinfo(cbstate->cli, caclfile);
1030 is_container = (fileattr & FILE_ATTRIBUTE_DIRECTORY);
1032 /* find acl(s) that are inherited */
1033 for (j = 0; old->dacl && j < old->dacl->num_aces; j++) {
1035 if (old->dacl->aces[j].flags & SEC_ACE_FLAG_INHERITED_ACE) {
1036 if (!add_ace_with_ctx(aclctx, &acl_to_remove,
1037 &old->dacl->aces[j])) {
1038 status = NT_STATUS_NO_MEMORY;
1044 /* remove any acl(s) that are inherited */
1045 if (acl_to_remove) {
1046 for (i = 0; i < acl_to_remove->num_aces; i++) {
1047 struct security_ace ace = acl_to_remove->aces[i];
1048 for (j = 0; old->dacl && j < old->dacl->num_aces; j++) {
1050 if (security_ace_equal(&ace,
1051 &old->dacl->aces[j])) {
1053 for (k = j; k < old->dacl->num_aces-1;
1055 old->dacl->aces[k] =
1056 old->dacl->aces[k+1];
1058 old->dacl->num_aces--;
1064 /* propagate any inheritable ace to be added */
1066 for (i = 0; i < acl_to_add->num_aces; i++) {
1067 struct security_ace ace = acl_to_add->aces[i];
1068 bool is_objectinherit = (ace.flags &
1069 SEC_ACE_FLAG_OBJECT_INHERIT) ==
1070 SEC_ACE_FLAG_OBJECT_INHERIT;
1072 /* don't propagate flags to a file unless OI */
1073 if (!is_objectinherit && !is_container) {
1077 * adjust flags according to inheritance
1080 ace.flags = get_flags_to_propagate(is_container, &ace);
1081 is_inherited = (ace.flags &
1082 SEC_ACE_FLAG_INHERITED_ACE) ==
1083 SEC_ACE_FLAG_INHERITED_ACE;
1084 /* don't propagate non inherited flags */
1085 if (!is_inherited) {
1088 if (!add_ace_with_ctx(aclctx, &old->dacl, &ace)) {
1089 status = NT_STATUS_NO_MEMORY;
1095 result = cacl_set_from_sd(cbstate->cli, caclfile,
1097 SMB_ACL_SET, cbstate->numeric);
1098 if (result != EXIT_OK) {
1099 status = NT_STATUS_UNSUCCESSFUL;
1103 status = NT_STATUS_OK;
1105 TALLOC_FREE(aclctx);
1110 * Returns true if 'ace' contains SEC_ACE_FLAG_OBJECT_INHERIT or
1111 * SEC_ACE_FLAG_CONTAINER_INHERIT
1113 static bool is_inheritable_ace(struct security_ace *ace)
1115 uint8_t flags = ace->flags;
1116 if (flags & (SEC_ACE_FLAG_OBJECT_INHERIT
1117 | SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1123 /* This method does some basic sanity checking with respect to automatic
1124 * inheritance. e.g. it checks if it is possible to do a set, it detects illegal
1125 * attempts to set inherited permissions directly. Additionally this method
1126 * does some basic initialisation for instance it parses the ACL passed on the
1129 static NTSTATUS prepare_inheritance_propagation(TALLOC_CTX *ctx, char *filename,
1130 struct cacl_callback_state *cbstate)
1133 char *the_acl = cbstate->the_acl;
1134 struct cli_state *cli = cbstate->cli;
1135 enum acl_mode mode = cbstate->mode;
1136 struct security_descriptor *sd = NULL;
1137 struct security_descriptor *old = NULL;
1139 bool propagate = false;
1141 old = get_secdesc_with_ctx(ctx, cli, filename);
1143 return NT_STATUS_NO_MEMORY;
1146 /* parse acl passed on the command line */
1148 cbstate->aclsd = sddl_decode(ctx, the_acl,
1149 get_global_sam_sid());
1151 cbstate->aclsd = sec_desc_parse(ctx, cli, the_acl);
1154 if (!cbstate->aclsd) {
1155 result = NT_STATUS_UNSUCCESSFUL;
1159 sd = cbstate->aclsd;
1161 /* set operation if inheritance is enabled doesn't make sense */
1162 if (mode == SMB_ACL_SET && ((old->type & SEC_DESC_DACL_PROTECTED) !=
1163 SEC_DESC_DACL_PROTECTED)){
1164 d_printf("Inheritance enabled at %s, can't apply set operation\n",filename);
1165 result = NT_STATUS_UNSUCCESSFUL;
1171 * search command line acl for any illegal SEC_ACE_FLAG_INHERITED_ACE
1172 * flags that are set
1174 for (j = 0; sd->dacl && j < sd->dacl->num_aces; j++) {
1175 struct security_ace *ace = &sd->dacl->aces[j];
1176 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE) {
1177 d_printf("Illegal paramater %s\n", the_acl);
1178 result = NT_STATUS_UNSUCCESSFUL;
1182 if (is_inheritable_ace(ace)) {
1188 result = NT_STATUS_OK;
1190 cbstate->acl_no_propagate = !propagate;
1195 * This method builds inheritable ace(s) from filename (which should be
1196 * a container) that need propagating to children in order to provide
1197 * automatic inheritance. Those inheritable ace(s) are stored in
1198 * acl_to_add member of cbstate for later processing
1199 * (see propagate_inherited_aces)
1201 static NTSTATUS get_inheritable_aces(TALLOC_CTX *ctx, char *filename,
1202 struct cacl_callback_state *cbstate)
1205 struct cli_state *cli = NULL;
1206 struct security_descriptor *sd = NULL;
1207 struct security_acl *acl_to_add = NULL;
1211 sd = get_secdesc_with_ctx(ctx, cli, filename);
1214 return NT_STATUS_NO_MEMORY;
1218 * Check if any inheritance related flags are used, if not then
1219 * nothing to do. At the same time populate acls for inheritance
1220 * related ace(s) that need to be added to or deleted from children as
1221 * a result of inheritance propagation.
1224 for (j = 0; sd->dacl && j < sd->dacl->num_aces; j++) {
1225 struct security_ace *ace = &sd->dacl->aces[j];
1226 if (is_inheritable_ace(ace)) {
1227 bool added = add_ace_with_ctx(ctx, &acl_to_add, ace);
1229 result = NT_STATUS_NO_MEMORY;
1234 cbstate->acl_to_add = acl_to_add;
1235 result = NT_STATUS_OK;
1241 * Callback handler to handle child elements processed by cli_list, we attempt
1242 * to propagate inheritable ace(s) to each child via the function
1243 * propagate_inherited_aces. Children that are themselves directories are passed
1244 * to cli_list again ( to decend the directory structure )
1246 static NTSTATUS cacl_set_cb(struct file_info *f,
1247 const char *mask, void *state)
1249 struct cacl_callback_state *cbstate =
1250 (struct cacl_callback_state *)state;
1251 struct cli_state *cli = NULL;
1252 struct cli_credentials *creds = NULL;
1254 TALLOC_CTX *dirctx = NULL;
1256 struct cli_state *targetcli = NULL;
1259 char *dir_end = NULL;
1261 char *targetpath = NULL;
1262 char *caclfile = NULL;
1264 dirctx = talloc_new(NULL);
1266 status = NT_STATUS_NO_MEMORY;
1271 creds = get_cmdline_auth_info_creds(cbstate->auth_info);
1273 /* Work out the directory. */
1274 dir = talloc_strdup(dirctx, mask);
1276 status = NT_STATUS_NO_MEMORY;
1280 dir_end = strrchr(dir, DIRSEP_CHAR);
1281 if (dir_end != NULL) {
1285 if (!f->name || !f->name[0]) {
1286 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
1287 status = NT_STATUS_UNSUCCESSFUL;
1291 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
1292 struct cacl_callback_state dir_cbstate;
1293 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY
1294 | FILE_ATTRIBUTE_SYSTEM
1295 | FILE_ATTRIBUTE_HIDDEN;
1298 /* ignore special '.' & '..' */
1299 if ((f->name == NULL) || ISDOT(f->name) || ISDOTDOT(f->name)) {
1300 status = NT_STATUS_OK;
1304 mask2 = build_dirname(dirctx, mask, dir, f->name);
1305 if (mask2 == NULL) {
1306 status = NT_STATUS_NO_MEMORY;
1311 status = local_cli_resolve_path(dirctx, "", creds, cli,
1312 mask2, &targetcli, &targetpath);
1313 if (!NT_STATUS_IS_OK(status)) {
1318 * prepare path to caclfile, remove any existing wildcard
1319 * chars and convert path separators.
1322 caclfile = talloc_strdup(dirctx, targetpath);
1324 status = NT_STATUS_NO_MEMORY;
1327 dir_end = strrchr(caclfile, '*');
1328 if (dir_end != NULL) {
1332 string_replace(caclfile, '/', '\\');
1334 * make directory specific copy of cbstate here
1335 * (for this directory level) to be available as
1336 * the parent cbstate for the children of this directory.
1337 * Note: cbstate is overwritten for the current file being
1340 dir_cbstate = *cbstate;
1341 dir_cbstate.cli = targetcli;
1344 * propagate any inherited ace from our parent
1346 status = propagate_inherited_aces(caclfile, &dir_cbstate);
1347 if (!NT_STATUS_IS_OK(status)) {
1352 * get inheritable ace(s) for this dir/container
1353 * that will be propagated to its children
1355 status = get_inheritable_aces(dirctx, caclfile,
1357 if (!NT_STATUS_IS_OK(status)) {
1362 * ensure cacl_set_cb gets called for children
1363 * of this directory (targetpath)
1365 status = cli_list(targetcli, targetpath,
1366 attribute, cacl_set_cb,
1367 (void *)&dir_cbstate);
1369 if (!NT_STATUS_IS_OK(status)) {
1375 * build full path to caclfile and replace '/' with '\' so
1376 * other utility functions can deal with it
1379 targetpath = talloc_asprintf(dirctx, "%s/%s", dir, f->name);
1381 status = NT_STATUS_NO_MEMORY;
1384 string_replace(targetpath, '/', '\\');
1386 /* attempt to propagate any inherited ace to file caclfile */
1387 status = propagate_inherited_aces(targetpath, cbstate);
1389 if (!NT_STATUS_IS_OK(status)) {
1393 status = NT_STATUS_OK;
1395 if (!NT_STATUS_IS_OK(status)) {
1396 d_printf("error %s: processing %s\n",
1400 TALLOC_FREE(dirctx);
1406 * Wrapper around cl_list to decend the directory tree pointed to by 'filename',
1407 * helper callback function 'cacl_set_cb' handles the child elements processed
1410 static int inheritance_cacl_set(char *filename,
1411 struct cacl_callback_state *cbstate)
1417 struct cli_state *cli = cbstate->cli;
1418 TALLOC_CTX *ctx = NULL;
1419 bool isdirectory = false;
1420 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM
1421 | FILE_ATTRIBUTE_HIDDEN;
1422 char *save_share = NULL;
1423 ctx = talloc_init("inherit_set");
1425 d_printf("out of memory\n");
1426 result = EXIT_FAILED;
1430 /* ensure we have a filename that starts with '\' */
1431 if (!filename || *filename != DIRSEP_CHAR) {
1432 /* illegal or no filename */
1433 result = EXIT_FAILED;
1434 d_printf("illegal or missing name '%s'\n", filename);
1439 fileattr = get_fileinfo(cli, filename);
1440 isdirectory = (fileattr & FILE_ATTRIBUTE_DIRECTORY)
1441 == FILE_ATTRIBUTE_DIRECTORY;
1444 * if we've got as far as here then we have already evaluated
1453 /* make sure we have a trailing '\*' for directory */
1455 mask = talloc_strdup(ctx, filename);
1456 } else if (strlen(filename) > 1) {
1458 * if the passed file name doesn't have a trailing '\'
1461 char *name_end = strrchr(filename, DIRSEP_CHAR);
1462 if (name_end != filename + strlen(filename) + 1) {
1463 mask = talloc_asprintf(ctx, "%s\\*", filename);
1465 mask = talloc_strdup(ctx, filename);
1468 /* filename is a single '\', just append '*' */
1469 mask = talloc_asprintf_append(mask, "%s*", filename);
1473 result = EXIT_FAILED;
1478 * prepare for automatic propagation of the acl passed on the
1481 save_share = talloc_strdup(ctx, cli->share);
1482 if (save_share == NULL) {
1483 result = EXIT_FAILED;
1487 ntstatus = prepare_inheritance_propagation(ctx, filename,
1489 if (!NT_STATUS_IS_OK(ntstatus)) {
1490 d_printf("error: %s processing %s\n",
1491 nt_errstr(ntstatus), filename);
1492 result = EXIT_FAILED;
1497 * sec_desc_parse ends up calling a bunch of functions one of which
1498 * connects to IPC$ (which overwrites cli->share)
1499 * we need a new connection to the share here.
1500 * Note: This only is an issue when the share is a msdfs root
1501 * because the presence of cli->share gets expanded out
1502 * later on by cli_resolve_path (when it is constructing a path)
1504 ntstatus = cli_tree_connect_creds(cli,
1507 get_cmdline_auth_info_creds(cbstate->auth_info));
1509 if (!NT_STATUS_IS_OK(ntstatus)) {
1510 d_printf("error: %s processing %s\n",
1511 nt_errstr(ntstatus), filename);
1512 result = EXIT_FAILED;
1515 result = cacl_set_from_sd(cli, filename, cbstate->aclsd,
1516 cbstate->mode, cbstate->numeric);
1519 * strictly speaking it could be considered an error if a file was
1520 * specificied with '--propagate-inheritance'. However we really want
1521 * to eventually get rid of '--propagate-inheritance' so we will be
1522 * more forgiving here and instead just exit early.
1524 if (!isdirectory || (result != EXIT_OK)) {
1528 /* check if there is actually any need to propagate */
1529 if (cbstate->acl_no_propagate) {
1532 /* get inheritable attributes this parent container (e.g. filename) */
1533 ntstatus = get_inheritable_aces(ctx, filename, cbstate);
1534 if (NT_STATUS_IS_OK(ntstatus)) {
1535 /* process children */
1536 ntstatus = cli_list(cli, mask, attribute,
1541 if (!NT_STATUS_IS_OK(ntstatus)) {
1542 d_printf("error: %s processing %s\n",
1543 nt_errstr(ntstatus), filename);
1544 result = EXIT_FAILED;
1553 /****************************************************************************
1555 ****************************************************************************/
1556 int main(int argc, char *argv[])
1558 const char **argv_const = discard_const_p(const char *, argv);
1561 enum acl_mode mode = SMB_ACL_SET;
1562 static char *the_acl = NULL;
1563 enum chown_mode change_mode = REQUEST_NONE;
1566 char *filename = NULL;
1568 /* numeric is set when the user wants numeric SIDs and ACEs rather
1569 than going via LSA calls to resolve them */
1571 struct cli_state *targetcli = NULL;
1572 struct cli_credentials *creds = NULL;
1573 char *targetfile = NULL;
1576 struct poptOption long_options[] = {
1579 .longName = "delete",
1581 .argInfo = POPT_ARG_STRING,
1584 .descrip = "Delete an acl",
1585 .argDescrip = "ACL",
1588 .longName = "modify",
1590 .argInfo = POPT_ARG_STRING,
1593 .descrip = "Modify an acl",
1594 .argDescrip = "ACL",
1599 .argInfo = POPT_ARG_STRING,
1602 .descrip = "Add an acl",
1603 .argDescrip = "ACL",
1608 .argInfo = POPT_ARG_STRING,
1611 .descrip = "Set acls",
1612 .argDescrip = "ACLS",
1615 .longName = "chown",
1617 .argInfo = POPT_ARG_STRING,
1620 .descrip = "Change ownership of a file",
1621 .argDescrip = "USERNAME",
1624 .longName = "chgrp",
1626 .argInfo = POPT_ARG_STRING,
1629 .descrip = "Change group ownership of a file",
1630 .argDescrip = "GROUPNAME",
1633 .longName = "inherit",
1635 .argInfo = POPT_ARG_STRING,
1638 .descrip = "Inherit allow|remove|copy",
1641 .longName = "propagate-inheritance",
1643 .argInfo = POPT_ARG_NONE,
1644 .arg = &inheritance,
1646 .descrip = "Supports propagation of inheritable ACE(s) when used in conjunction with add, delete, set or modify",
1649 .longName = "numeric",
1651 .argInfo = POPT_ARG_NONE,
1654 .descrip = "Don't resolve sids or masks to names",
1659 .argInfo = POPT_ARG_NONE,
1662 .descrip = "Output and input acls in sddl format",
1665 .longName = "query-security-info",
1667 .argInfo = POPT_ARG_INT,
1668 .arg = &query_sec_info,
1670 .descrip = "The security-info flags for queries"
1673 .longName = "set-security-info",
1675 .argInfo = POPT_ARG_INT,
1676 .arg = &set_sec_info,
1678 .descrip = "The security-info flags for modifications"
1681 .longName = "test-args",
1683 .argInfo = POPT_ARG_NONE,
1686 .descrip = "Test arguments"
1689 .longName = "domain-sid",
1691 .argInfo = POPT_ARG_STRING,
1694 .descrip = "Domain SID for sddl",
1695 .argDescrip = "SID"},
1697 .longName = "max-protocol",
1699 .argInfo = POPT_ARG_STRING,
1702 .descrip = "Set the max protocol level",
1703 .argDescrip = "LEVEL",
1706 .longName = "maximum-access",
1708 .argInfo = POPT_ARG_NONE,
1711 .descrip = "Query maximum persmissions",
1714 POPT_COMMON_CONNECTION
1715 POPT_COMMON_CREDENTIALS
1719 struct cli_state *cli;
1720 TALLOC_CTX *frame = talloc_stackframe();
1721 const char *owner_username = "";
1726 /* set default debug level to 1 regardless of what smb.conf sets */
1727 setup_logging( "smbcacls", DEBUG_STDERR);
1728 lp_set_cmdline("log level", "1");
1732 popt_common_credentials_set_ignore_missing_conf();
1734 pc = poptGetContext("smbcacls", argc, argv_const, long_options, 0);
1736 poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
1737 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
1739 while ((opt = poptGetNextOpt(pc)) != -1) {
1742 the_acl = smb_xstrdup(poptGetOptArg(pc));
1747 the_acl = smb_xstrdup(poptGetOptArg(pc));
1748 mode = SMB_ACL_DELETE;
1752 the_acl = smb_xstrdup(poptGetOptArg(pc));
1753 mode = SMB_ACL_MODIFY;
1757 the_acl = smb_xstrdup(poptGetOptArg(pc));
1762 owner_username = poptGetOptArg(pc);
1763 change_mode = REQUEST_CHOWN;
1767 owner_username = poptGetOptArg(pc);
1768 change_mode = REQUEST_CHGRP;
1772 owner_username = poptGetOptArg(pc);
1773 change_mode = REQUEST_INHERIT;
1776 lp_set_cmdline("client max protocol", poptGetOptArg(pc));
1783 if (inheritance && !the_acl) {
1784 poptPrintUsage(pc, stderr, 0);
1788 if(!poptPeekArg(pc)) {
1789 poptPrintUsage(pc, stderr, 0);
1793 path = talloc_strdup(frame, poptGetArg(pc));
1798 if(!poptPeekArg(pc)) {
1799 poptPrintUsage(pc, stderr, 0);
1803 filename = talloc_strdup(frame, poptGetArg(pc));
1808 poptFreeContext(pc);
1809 popt_burn_cmdline_password(argc, argv);
1810 popt_common_credentials_post();
1812 string_replace(path,'/','\\');
1814 server = talloc_strdup(frame, path+2);
1818 share = strchr_m(server,'\\');
1819 if (share == NULL) {
1820 printf("Invalid argument\n");
1827 creds = get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
1829 /* Make connection to server */
1831 cli = connect_one(creds, server, share);
1836 popt_free_cmdline_auth_info();
1840 string_replace(filename, '/', '\\');
1841 if (filename[0] != '\\') {
1842 filename = talloc_asprintf(frame,
1850 status = local_cli_resolve_path(frame,
1857 if (!NT_STATUS_IS_OK(status)) {
1858 DEBUG(0,("cli_resolve_path failed for %s! (%s)\n", filename, nt_errstr(status)));
1862 /* Perform requested action */
1864 if (change_mode == REQUEST_INHERIT) {
1865 result = inherit(targetcli, targetfile, owner_username);
1866 } else if (change_mode != REQUEST_NONE) {
1867 result = owner_set(targetcli, change_mode, targetfile, owner_username);
1868 } else if (the_acl) {
1870 struct cacl_callback_state cbstate = {
1871 .auth_info = popt_get_cmdline_auth_info(),
1877 result = inheritance_cacl_set(targetfile, &cbstate);
1879 result = cacl_set(targetcli,
1886 result = cacl_dump(targetcli, targetfile, numeric);
1889 popt_free_cmdline_auth_info();