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 "lib/cmdline/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"
36 #include "lib/param/param.h"
38 static char DIRSEP_CHAR = '\\';
40 static int inheritance = 0;
41 static const char *save_file = NULL;
42 static const char *restore_file = NULL;
46 static int query_sec_info = -1;
47 static int set_sec_info = -1;
48 static bool want_mxac;
50 static const char *domain_sid = NULL;
52 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
53 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP, REQUEST_INHERIT};
54 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
56 struct cacl_callback_state {
57 struct cli_credentials *creds;
58 struct cli_state *cli;
59 struct security_descriptor *aclsd;
60 struct security_acl *acl_to_add;
63 bool acl_no_propagate;
67 static NTSTATUS cli_lsa_lookup_domain_sid(struct cli_state *cli,
70 union lsa_PolicyInformation *info = NULL;
71 struct smbXcli_tcon *orig_tcon = NULL;
72 char *orig_share = NULL;
73 struct rpc_pipe_client *rpc_pipe = NULL;
74 struct policy_handle handle;
75 NTSTATUS status, result;
76 TALLOC_CTX *frame = talloc_stackframe();
78 if (cli_state_has_tcon(cli)) {
79 cli_state_save_tcon_share(cli, &orig_tcon, &orig_share);
82 status = cli_tree_connect(cli, "IPC$", "?????", NULL);
83 if (!NT_STATUS_IS_OK(status)) {
87 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc, &rpc_pipe);
88 if (!NT_STATUS_IS_OK(status)) {
92 status = rpccli_lsa_open_policy(rpc_pipe, frame, True,
93 GENERIC_EXECUTE_ACCESS, &handle);
94 if (!NT_STATUS_IS_OK(status)) {
98 status = dcerpc_lsa_QueryInfoPolicy2(rpc_pipe->binding_handle,
100 LSA_POLICY_INFO_DOMAIN,
103 if (any_nt_status_not_ok(status, result, &status)) {
107 *sid = *info->domain.sid;
110 TALLOC_FREE(rpc_pipe);
113 cli_state_restore_tcon_share(cli, orig_tcon, orig_share);
118 static struct dom_sid *get_domain_sid(struct cli_state *cli)
121 struct dom_sid_buf buf;
123 struct dom_sid *sid = talloc(talloc_tos(), struct dom_sid);
125 DEBUG(0, ("Out of memory\n"));
130 if (!dom_sid_parse(domain_sid, sid)) {
131 DEBUG(0,("failed to parse domain sid\n"));
135 status = cli_lsa_lookup_domain_sid(cli, sid);
137 if (!NT_STATUS_IS_OK(status)) {
138 DEBUG(0,("failed to lookup domain sid: %s\n", nt_errstr(status)));
144 DEBUG(2,("Domain SID: %s\n", dom_sid_str_buf(sid, &buf)));
148 /* add an ACE to a list of ACEs in a struct security_acl */
149 static bool add_ace_with_ctx(TALLOC_CTX *ctx, struct security_acl **the_acl,
150 const struct security_ace *ace)
153 struct security_acl *acl = *the_acl;
156 acl = make_sec_acl(ctx, 3, 0, NULL);
162 if (acl->num_aces == UINT32_MAX) {
166 acl, struct security_ace, *ace, &acl->aces, &acl->num_aces);
171 static bool add_ace(struct security_acl **the_acl, struct security_ace *ace)
173 return add_ace_with_ctx(talloc_tos(), the_acl, ace);
176 /* parse a ascii version of a security descriptor */
177 static struct security_descriptor *sec_desc_parse(TALLOC_CTX *ctx, struct cli_state *cli, char *str)
181 struct security_descriptor *ret = NULL;
183 struct dom_sid owner_sid = { .num_auths = 0 };
184 bool have_owner = false;
185 struct dom_sid group_sid = { .num_auths = 0 };
186 bool have_group = false;
187 struct security_acl *dacl=NULL;
190 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
191 if (strncmp(tok,"REVISION:", 9) == 0) {
192 revision = strtol(tok+9, NULL, 16);
196 if (strncmp(tok,"OWNER:", 6) == 0) {
198 printf("Only specify owner once\n");
201 if (!StringToSid(cli, &owner_sid, tok+6)) {
202 printf("Failed to parse owner sid\n");
209 if (strncmp(tok,"GROUP:", 6) == 0) {
211 printf("Only specify group once\n");
214 if (!StringToSid(cli, &group_sid, tok+6)) {
215 printf("Failed to parse group sid\n");
222 if (strncmp(tok,"ACL:", 4) == 0) {
223 struct security_ace ace;
224 if (!parse_ace(cli, &ace, tok+4)) {
227 if(!add_ace(&dacl, &ace)) {
228 printf("Failed to add ACL %s\n", tok);
234 printf("Failed to parse token '%s' in security descriptor,\n", tok);
241 SEC_DESC_SELF_RELATIVE,
242 have_owner ? &owner_sid : NULL,
243 have_group ? &group_sid : NULL,
252 /*****************************************************
253 get fileinfo for filename
254 *******************************************************/
255 static uint16_t get_fileinfo(struct cli_state *cli, const char *filename)
257 uint16_t fnum = (uint16_t)-1;
259 struct smb_create_returns cr = {0};
261 /* The desired access below is the only one I could find that works
262 with NT4, W2KP and Samba */
264 status = cli_ntcreate(
266 filename, /* fname */
268 READ_CONTROL_ACCESS, /* CreatFlags */
269 0, /* FileAttributes */
271 FILE_SHARE_WRITE, /* ShareAccess */
272 FILE_OPEN, /* CreateDisposition */
273 0x0, /* CreateOptions */
274 0x0, /* SecurityFlags */
277 if (!NT_STATUS_IS_OK(status)) {
278 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
282 cli_close(cli, fnum);
283 return cr.file_attributes;
286 /*****************************************************
287 get sec desc for filename
288 *******************************************************/
289 static struct security_descriptor *get_secdesc_with_ctx(TALLOC_CTX *ctx,
290 struct cli_state *cli,
291 const char *filename)
293 uint16_t fnum = (uint16_t)-1;
294 struct security_descriptor *sd;
297 uint32_t desired_access = 0;
299 if (query_sec_info == -1) {
300 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
302 sec_info = query_sec_info;
305 if (sec_info & (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL)) {
306 desired_access |= SEC_STD_READ_CONTROL;
308 if (sec_info & SECINFO_SACL) {
309 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
312 if (desired_access == 0) {
313 desired_access |= SEC_STD_READ_CONTROL;
316 status = cli_ntcreate(cli, filename, 0, desired_access,
317 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
318 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
319 if (!NT_STATUS_IS_OK(status)) {
320 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
324 status = cli_query_security_descriptor(cli, fnum, sec_info,
327 cli_close(cli, fnum);
329 if (!NT_STATUS_IS_OK(status)) {
330 printf("Failed to get security descriptor: %s\n",
337 static struct security_descriptor *get_secdesc(struct cli_state *cli,
338 const char *filename)
340 return get_secdesc_with_ctx(talloc_tos(), cli, filename);
342 /*****************************************************
343 set sec desc for filename
344 *******************************************************/
345 static bool set_secdesc(struct cli_state *cli, const char *filename,
346 struct security_descriptor *sd)
348 uint16_t fnum = (uint16_t)-1;
351 uint32_t desired_access = 0;
354 if (set_sec_info == -1) {
357 if (sd->dacl || (sd->type & SEC_DESC_DACL_PRESENT)) {
358 sec_info |= SECINFO_DACL;
360 if (sd->sacl || (sd->type & SEC_DESC_SACL_PRESENT)) {
361 sec_info |= SECINFO_SACL;
364 sec_info |= SECINFO_OWNER;
367 sec_info |= SECINFO_GROUP;
370 sec_info = set_sec_info;
373 /* Make the desired_access more specific. */
374 if (sec_info & SECINFO_DACL) {
375 desired_access |= SEC_STD_WRITE_DAC;
377 if (sec_info & SECINFO_SACL) {
378 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
380 if (sec_info & (SECINFO_OWNER | SECINFO_GROUP)) {
381 desired_access |= SEC_STD_WRITE_OWNER;
384 status = cli_ntcreate(cli, filename, 0,
386 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
387 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
388 if (!NT_STATUS_IS_OK(status)) {
389 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
393 status = cli_set_security_descriptor(cli, fnum, sec_info, sd);
394 if (!NT_STATUS_IS_OK(status)) {
395 printf("ERROR: security descriptor set failed: %s\n",
400 cli_close(cli, fnum);
404 /*****************************************************
405 get maximum access for a file
406 *******************************************************/
407 static int cacl_mxac(struct cli_state *cli, const char *filename)
412 status = cli_query_mxac(cli, filename, &mxac);
413 if (!NT_STATUS_IS_OK(status)) {
414 printf("Failed to get mxac: %s\n", nt_errstr(status));
418 printf("Maximum access: 0x%x\n", mxac);
424 /*****************************************************
425 dump the acls for a file
426 *******************************************************/
427 static int cacl_dump(struct cli_state *cli, const char *filename, bool numeric)
429 struct security_descriptor *sd;
436 sd = get_secdesc(cli, filename);
442 char *str = sddl_encode(talloc_tos(), sd, get_domain_sid(cli));
449 sec_desc_print(cli, stdout, sd, numeric);
453 ret = cacl_mxac(cli, filename);
454 if (ret != EXIT_OK) {
462 /*****************************************************
463 Change the ownership or group ownership of a file. Just
464 because the NT docs say this can't be done :-). JRA.
465 *******************************************************/
467 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
468 const char *filename, const char *new_username)
471 struct security_descriptor *sd;
474 if (!StringToSid(cli, &sid, new_username))
475 return EXIT_PARSE_ERROR;
477 sd = make_sec_desc(talloc_tos(),
478 SECURITY_DESCRIPTOR_REVISION_1,
479 SEC_DESC_SELF_RELATIVE,
480 (change_mode == REQUEST_CHOWN) ? &sid : NULL,
481 (change_mode == REQUEST_CHGRP) ? &sid : NULL,
482 NULL, NULL, &sd_size);
484 if (!set_secdesc(cli, filename, sd)) {
492 /* The MSDN is contradictory over the ordering of ACE entries in an
493 ACL. However NT4 gives a "The information may have been modified
494 by a computer running Windows NT 5.0" if denied ACEs do not appear
495 before allowed ACEs. At
496 http://technet.microsoft.com/en-us/library/cc781716.aspx the
497 canonical order is specified as "Explicit Deny, Explicit Allow,
498 Inherited ACEs unchanged" */
500 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
502 if (security_ace_equal(ace1, ace2))
505 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
506 !(ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
508 if (!(ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
509 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
511 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
512 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
515 if (ace1->type != ace2->type)
516 return ace2->type - ace1->type;
518 if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
519 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
521 if (ace1->flags != ace2->flags)
522 return ace1->flags - ace2->flags;
524 if (ace1->access_mask != ace2->access_mask)
525 return ace1->access_mask - ace2->access_mask;
527 if (ace1->size != ace2->size)
528 return ace1->size - ace2->size;
530 return memcmp(ace1, ace2, sizeof(struct security_ace));
533 static void sort_acl(struct security_acl *the_acl)
536 if (!the_acl) return;
538 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
540 for (i=1;i<the_acl->num_aces;) {
541 if (security_ace_equal(&the_acl->aces[i-1],
542 &the_acl->aces[i])) {
544 the_acl->aces, i, the_acl->num_aces);
552 /*****************************************************
553 set the ACLs on a file given a security descriptor
554 *******************************************************/
556 static int cacl_set_from_sd(struct cli_state *cli, const char *filename,
557 struct security_descriptor *sd, enum acl_mode mode,
560 struct security_descriptor *old = NULL;
563 int result = EXIT_OK;
565 if (!sd) return EXIT_PARSE_ERROR;
566 if (test_args) return EXIT_OK;
568 if (mode != SMB_ACL_SET) {
570 * Do not fetch old ACL when it will be overwritten
571 * completely with a new one.
573 old = get_secdesc(cli, filename);
580 /* the logic here is rather more complex than I would like */
583 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
586 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
587 if (security_ace_equal(&sd->dacl->aces[i],
588 &old->dacl->aces[j])) {
590 for (k=j; k<old->dacl->num_aces-1;k++) {
591 old->dacl->aces[k] = old->dacl->aces[k+1];
593 old->dacl->num_aces--;
600 printf("ACL for ACE:");
601 print_ace(cli, stdout, &sd->dacl->aces[i],
603 printf(" not found\n");
609 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
612 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
613 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
614 &old->dacl->aces[j].trustee)) {
615 old->dacl->aces[j] = sd->dacl->aces[i];
623 SidToString(cli, str,
624 &sd->dacl->aces[i].trustee,
626 printf("ACL for SID %s not found\n", str);
631 old->owner_sid = sd->owner_sid;
635 old->group_sid = sd->group_sid;
641 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
642 add_ace(&old->dacl, &sd->dacl->aces[i]);
651 /* Denied ACE entries must come before allowed ones */
654 /* Create new security descriptor and set it */
656 /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
657 But if we're sending an owner, even if it's the same as the one
658 that already exists then W2K3 insists we open with WRITE_OWNER access.
659 I need to check that setting a SD with no owner set works against WNT
663 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
664 old->owner_sid, old->group_sid,
665 NULL, old->dacl, &sd_size);
667 if (!set_secdesc(cli, filename, sd)) {
668 result = EXIT_FAILED;
674 /*****************************************************
675 set the ACLs on a file given an ascii description
676 *******************************************************/
678 static int cacl_set(struct cli_state *cli, const char *filename,
679 char *the_acl, enum acl_mode mode, bool numeric)
681 struct security_descriptor *sd = NULL;
684 sd = sddl_decode(talloc_tos(), the_acl, get_domain_sid(cli));
686 sd = sec_desc_parse(talloc_tos(), cli, the_acl);
690 return EXIT_PARSE_ERROR;
695 return cacl_set_from_sd(cli, filename, sd, mode, numeric);
698 /*****************************************************
699 set the inherit on a file
700 *******************************************************/
701 static int inherit(struct cli_state *cli, const char *filename,
704 struct security_descriptor *old,*sd;
707 int result = EXIT_OK;
709 old = get_secdesc(cli, filename);
715 oldattr = get_fileinfo(cli,filename);
717 if (strcmp(type,"allow")==0) {
718 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
719 SEC_DESC_DACL_PROTECTED) {
721 char *parentname,*temp;
722 struct security_descriptor *parent;
723 temp = talloc_strdup(talloc_tos(), filename);
725 old->type=old->type & (~SEC_DESC_DACL_PROTECTED);
727 /* look at parent and copy in all its inheritable ACL's. */
728 string_replace(temp, '\\', '/');
729 if (!parent_dirname(talloc_tos(),temp,&parentname,NULL)) {
732 string_replace(parentname, '/', '\\');
733 parent = get_secdesc(cli,parentname);
734 if (parent == NULL) {
737 for (i=0;i<parent->dacl->num_aces;i++) {
738 struct security_ace *ace=&parent->dacl->aces[i];
739 /* Add inherited flag to all aces */
740 ace->flags=ace->flags|
741 SEC_ACE_FLAG_INHERITED_ACE;
742 if ((oldattr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
743 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ==
744 SEC_ACE_FLAG_CONTAINER_INHERIT) {
745 add_ace(&old->dacl, ace);
748 if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) ==
749 SEC_ACE_FLAG_OBJECT_INHERIT) {
750 /* clear flags for files */
752 add_ace(&old->dacl, ace);
757 printf("Already set to inheritable permissions.\n");
760 } else if (strcmp(type,"remove")==0) {
761 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
762 SEC_DESC_DACL_PROTECTED) {
763 old->type=old->type | SEC_DESC_DACL_PROTECTED;
765 /* remove all inherited ACL's. */
768 struct security_acl *temp=old->dacl;
769 old->dacl=make_sec_acl(talloc_tos(), 3, 0, NULL);
770 for (i=temp->num_aces-1;i>=0;i--) {
771 struct security_ace *ace=&temp->aces[i];
772 /* Remove all ace with INHERITED flag set */
773 if ((ace->flags & SEC_ACE_FLAG_INHERITED_ACE) !=
774 SEC_ACE_FLAG_INHERITED_ACE) {
775 add_ace(&old->dacl,ace);
780 printf("Already set to no inheritable permissions.\n");
783 } else if (strcmp(type,"copy")==0) {
784 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
785 SEC_DESC_DACL_PROTECTED) {
786 old->type=old->type | SEC_DESC_DACL_PROTECTED;
789 * convert all inherited ACL's to non
794 for (i=0;i<old->dacl->num_aces;i++) {
795 struct security_ace *ace=&old->dacl->aces[i];
796 /* Remove INHERITED FLAG from all aces */
797 ace->flags=ace->flags&(~SEC_ACE_FLAG_INHERITED_ACE);
801 printf("Already set to no inheritable permissions.\n");
806 /* Denied ACE entries must come before allowed ones */
809 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
810 old->owner_sid, old->group_sid,
811 NULL, old->dacl, &sd_size);
813 if (!set_secdesc(cli, filename, sd)) {
814 result = EXIT_FAILED;
820 /*****************************************************
821 Return a connection to a server.
822 *******************************************************/
823 static struct cli_state *connect_one(struct cli_credentials *creds,
824 const char *server, const char *share)
826 struct cli_state *c = NULL;
830 nt_status = cli_full_connection_creds(&c, lp_netbios_name(), server,
835 if (!NT_STATUS_IS_OK(nt_status)) {
836 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
844 * Process resulting combination of mask & fname ensuring
845 * terminated with wildcard
847 static char *build_dirname(TALLOC_CTX *ctx,
848 const char *mask, char *dir, char *fname)
853 mask2 = talloc_strdup(ctx, mask);
857 p = strrchr_m(mask2, DIRSEP_CHAR);
863 mask2 = talloc_asprintf_append(mask2,
870 * Returns a copy of the ACL flags in ace modified according
871 * to some inheritance rules.
872 * a) SEC_ACE_FLAG_INHERITED_ACE is propagated to children
873 * b) SEC_ACE_FLAG_INHERIT_ONLY is set on container children for OI (only)
874 * c) SEC_ACE_FLAG_OBJECT_INHERIT & SEC_ACE_FLAG_CONTAINER_INHERIT are
875 * stripped from flags to be propagated to non-container children
876 * d) SEC_ACE_FLAG_OBJECT_INHERIT & SEC_ACE_FLAG_CONTAINER_INHERIT are
877 * stripped from flags to be propagated if the NP flag
878 * SEC_ACE_FLAG_NO_PROPAGATE_INHERIT is present
881 static uint8_t get_flags_to_propagate(bool is_container,
882 struct security_ace *ace)
884 uint8_t newflags = ace->flags;
885 /* OBJECT inheritance */
886 bool acl_objinherit = (ace->flags &
887 SEC_ACE_FLAG_OBJECT_INHERIT) == SEC_ACE_FLAG_OBJECT_INHERIT;
888 /* CONTAINER inheritance */
889 bool acl_cntrinherit = (ace->flags &
890 SEC_ACE_FLAG_CONTAINER_INHERIT) ==
891 SEC_ACE_FLAG_CONTAINER_INHERIT;
892 /* PROHIBIT inheritance */
893 bool prohibit_inheritance = ((ace->flags &
894 SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) ==
895 SEC_ACE_FLAG_NO_PROPAGATE_INHERIT);
897 /* Assume we are not propagating the ACE */
899 newflags &= ~SEC_ACE_FLAG_INHERITED_ACE;
900 /* all children need to have the SEC_ACE_FLAG_INHERITED_ACE set */
901 if (acl_cntrinherit || acl_objinherit) {
903 * object inherit ( alone ) on a container needs
904 * SEC_ACE_FLAG_INHERIT_ONLY
907 if (acl_objinherit && !acl_cntrinherit) {
908 newflags |= SEC_ACE_FLAG_INHERIT_ONLY;
911 * this is tricky, the only time we would not
912 * propagate the ace for a container is if
913 * prohibit_inheritance is set and object inheritance
916 if ((prohibit_inheritance
918 && !acl_cntrinherit) == false) {
919 newflags |= SEC_ACE_FLAG_INHERITED_ACE;
923 * don't apply object/container inheritance flags to
926 newflags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT
927 | SEC_ACE_FLAG_CONTAINER_INHERIT
928 | SEC_ACE_FLAG_INHERIT_ONLY);
930 * only apply ace to file if object inherit
932 if (acl_objinherit) {
933 newflags |= SEC_ACE_FLAG_INHERITED_ACE;
937 /* if NP is specified strip NP and all OI/CI INHERIT flags */
938 if (prohibit_inheritance) {
939 newflags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT
940 | SEC_ACE_FLAG_CONTAINER_INHERIT
941 | SEC_ACE_FLAG_INHERIT_ONLY
942 | SEC_ACE_FLAG_NO_PROPAGATE_INHERIT);
949 * This function builds a new acl for 'caclfile', first it removes any
950 * existing inheritable ace(s) from the current acl of caclfile, secondly it
951 * applies any inheritable acls of the parent of caclfile ( inheritable acls of
952 * caclfile's parent are passed via acl_to_add member of cbstate )
955 static NTSTATUS propagate_inherited_aces(char *caclfile,
956 struct cacl_callback_state *cbstate)
958 TALLOC_CTX *aclctx = NULL;
962 struct security_descriptor *old = NULL;
963 bool is_container = false;
964 struct security_acl *acl_to_add = cbstate->acl_to_add;
965 struct security_acl *acl_to_remove = NULL;
968 aclctx = talloc_new(NULL);
969 if (aclctx == NULL) {
970 return NT_STATUS_NO_MEMORY;
972 old = get_secdesc_with_ctx(aclctx, cbstate->cli, caclfile);
975 status = NT_STATUS_UNSUCCESSFUL;
979 /* inhibit propagation? */
980 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
981 SEC_DESC_DACL_PROTECTED){
982 status = NT_STATUS_OK;
986 fileattr = get_fileinfo(cbstate->cli, caclfile);
987 is_container = (fileattr & FILE_ATTRIBUTE_DIRECTORY);
989 /* find acl(s) that are inherited */
990 for (j = 0; old->dacl && j < old->dacl->num_aces; j++) {
992 if (old->dacl->aces[j].flags & SEC_ACE_FLAG_INHERITED_ACE) {
993 if (!add_ace_with_ctx(aclctx, &acl_to_remove,
994 &old->dacl->aces[j])) {
995 status = NT_STATUS_NO_MEMORY;
1001 /* remove any acl(s) that are inherited */
1002 if (acl_to_remove) {
1003 for (i = 0; i < acl_to_remove->num_aces; i++) {
1004 struct security_ace ace = acl_to_remove->aces[i];
1005 for (j = 0; old->dacl && j < old->dacl->num_aces; j++) {
1007 if (security_ace_equal(&ace,
1008 &old->dacl->aces[j])) {
1010 for (k = j; k < old->dacl->num_aces-1;
1012 old->dacl->aces[k] =
1013 old->dacl->aces[k+1];
1015 old->dacl->num_aces--;
1021 /* propagate any inheritable ace to be added */
1023 for (i = 0; i < acl_to_add->num_aces; i++) {
1024 struct security_ace ace = acl_to_add->aces[i];
1025 bool is_objectinherit = (ace.flags &
1026 SEC_ACE_FLAG_OBJECT_INHERIT) ==
1027 SEC_ACE_FLAG_OBJECT_INHERIT;
1029 /* don't propagate flags to a file unless OI */
1030 if (!is_objectinherit && !is_container) {
1034 * adjust flags according to inheritance
1037 ace.flags = get_flags_to_propagate(is_container, &ace);
1038 is_inherited = (ace.flags &
1039 SEC_ACE_FLAG_INHERITED_ACE) ==
1040 SEC_ACE_FLAG_INHERITED_ACE;
1041 /* don't propagate non inherited flags */
1042 if (!is_inherited) {
1045 if (!add_ace_with_ctx(aclctx, &old->dacl, &ace)) {
1046 status = NT_STATUS_NO_MEMORY;
1052 result = cacl_set_from_sd(cbstate->cli, caclfile,
1054 SMB_ACL_SET, cbstate->numeric);
1055 if (result != EXIT_OK) {
1056 status = NT_STATUS_UNSUCCESSFUL;
1060 status = NT_STATUS_OK;
1062 TALLOC_FREE(aclctx);
1067 * Returns true if 'ace' contains SEC_ACE_FLAG_OBJECT_INHERIT or
1068 * SEC_ACE_FLAG_CONTAINER_INHERIT
1070 static bool is_inheritable_ace(struct security_ace *ace)
1072 uint8_t flags = ace->flags;
1073 if (flags & (SEC_ACE_FLAG_OBJECT_INHERIT
1074 | SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1080 /* This method does some basic sanity checking with respect to automatic
1081 * inheritance. e.g. it checks if it is possible to do a set, it detects illegal
1082 * attempts to set inherited permissions directly. Additionally this method
1083 * does some basic initialisation for instance it parses the ACL passed on the
1086 static NTSTATUS prepare_inheritance_propagation(TALLOC_CTX *ctx, char *filename,
1087 struct cacl_callback_state *cbstate)
1090 char *the_acl = cbstate->the_acl;
1091 struct cli_state *cli = cbstate->cli;
1092 enum acl_mode mode = cbstate->mode;
1093 struct security_descriptor *sd = NULL;
1094 struct security_descriptor *old = NULL;
1096 bool propagate = false;
1098 old = get_secdesc_with_ctx(ctx, cli, filename);
1100 return NT_STATUS_NO_MEMORY;
1103 /* parse acl passed on the command line */
1105 cbstate->aclsd = sddl_decode(ctx, the_acl,
1106 get_domain_sid(cli));
1108 cbstate->aclsd = sec_desc_parse(ctx, cli, the_acl);
1111 if (!cbstate->aclsd) {
1112 result = NT_STATUS_UNSUCCESSFUL;
1116 sd = cbstate->aclsd;
1118 /* set operation if inheritance is enabled doesn't make sense */
1119 if (mode == SMB_ACL_SET && ((old->type & SEC_DESC_DACL_PROTECTED) !=
1120 SEC_DESC_DACL_PROTECTED)){
1121 d_printf("Inheritance enabled at %s, can't apply set operation\n",filename);
1122 result = NT_STATUS_UNSUCCESSFUL;
1128 * search command line acl for any illegal SEC_ACE_FLAG_INHERITED_ACE
1129 * flags that are set
1131 for (j = 0; sd->dacl && j < sd->dacl->num_aces; j++) {
1132 struct security_ace *ace = &sd->dacl->aces[j];
1133 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE) {
1134 d_printf("Illegal parameter %s\n", the_acl);
1135 result = NT_STATUS_UNSUCCESSFUL;
1139 if (is_inheritable_ace(ace)) {
1145 result = NT_STATUS_OK;
1147 cbstate->acl_no_propagate = !propagate;
1152 * This method builds inheritable ace(s) from filename (which should be
1153 * a container) that need propagating to children in order to provide
1154 * automatic inheritance. Those inheritable ace(s) are stored in
1155 * acl_to_add member of cbstate for later processing
1156 * (see propagate_inherited_aces)
1158 static NTSTATUS get_inheritable_aces(TALLOC_CTX *ctx, char *filename,
1159 struct cacl_callback_state *cbstate)
1162 struct cli_state *cli = NULL;
1163 struct security_descriptor *sd = NULL;
1164 struct security_acl *acl_to_add = NULL;
1168 sd = get_secdesc_with_ctx(ctx, cli, filename);
1171 return NT_STATUS_NO_MEMORY;
1175 * Check if any inheritance related flags are used, if not then
1176 * nothing to do. At the same time populate acls for inheritance
1177 * related ace(s) that need to be added to or deleted from children as
1178 * a result of inheritance propagation.
1181 for (j = 0; sd->dacl && j < sd->dacl->num_aces; j++) {
1182 struct security_ace *ace = &sd->dacl->aces[j];
1183 if (is_inheritable_ace(ace)) {
1184 bool added = add_ace_with_ctx(ctx, &acl_to_add, ace);
1186 result = NT_STATUS_NO_MEMORY;
1191 cbstate->acl_to_add = acl_to_add;
1192 result = NT_STATUS_OK;
1198 * Callback handler to handle child elements processed by cli_list, we attempt
1199 * to propagate inheritable ace(s) to each child via the function
1200 * propagate_inherited_aces. Children that are themselves directories are passed
1201 * to cli_list again ( to descend the directory structure )
1203 static NTSTATUS cacl_set_cb(struct file_info *f,
1204 const char *mask, void *state)
1206 struct cacl_callback_state *cbstate =
1207 (struct cacl_callback_state *)state;
1208 struct cli_state *cli = NULL;
1209 struct cli_credentials *creds = NULL;
1211 TALLOC_CTX *dirctx = NULL;
1213 struct cli_state *targetcli = NULL;
1216 char *dir_end = NULL;
1218 char *targetpath = NULL;
1219 char *caclfile = NULL;
1221 dirctx = talloc_new(NULL);
1223 status = NT_STATUS_NO_MEMORY;
1228 creds = cbstate->creds;
1230 /* Work out the directory. */
1231 dir = talloc_strdup(dirctx, mask);
1233 status = NT_STATUS_NO_MEMORY;
1237 dir_end = strrchr(dir, DIRSEP_CHAR);
1238 if (dir_end != NULL) {
1242 if (!f->name || !f->name[0]) {
1243 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
1244 status = NT_STATUS_UNSUCCESSFUL;
1248 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
1249 struct cacl_callback_state dir_cbstate;
1250 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY
1251 | FILE_ATTRIBUTE_SYSTEM
1252 | FILE_ATTRIBUTE_HIDDEN;
1255 /* ignore special '.' & '..' */
1256 if ((f->name == NULL) || ISDOT(f->name) || ISDOTDOT(f->name)) {
1257 status = NT_STATUS_OK;
1261 mask2 = build_dirname(dirctx, mask, dir, f->name);
1262 if (mask2 == NULL) {
1263 status = NT_STATUS_NO_MEMORY;
1268 status = cli_resolve_path(dirctx, "", creds, cli,
1269 mask2, &targetcli, &targetpath);
1270 if (!NT_STATUS_IS_OK(status)) {
1275 * prepare path to caclfile, remove any existing wildcard
1276 * chars and convert path separators.
1279 caclfile = talloc_strdup(dirctx, targetpath);
1281 status = NT_STATUS_NO_MEMORY;
1284 dir_end = strrchr(caclfile, '*');
1285 if (dir_end != NULL) {
1289 string_replace(caclfile, '/', '\\');
1291 * make directory specific copy of cbstate here
1292 * (for this directory level) to be available as
1293 * the parent cbstate for the children of this directory.
1294 * Note: cbstate is overwritten for the current file being
1297 dir_cbstate = *cbstate;
1298 dir_cbstate.cli = targetcli;
1301 * propagate any inherited ace from our parent
1303 status = propagate_inherited_aces(caclfile, &dir_cbstate);
1304 if (!NT_STATUS_IS_OK(status)) {
1309 * get inheritable ace(s) for this dir/container
1310 * that will be propagated to its children
1312 status = get_inheritable_aces(dirctx, caclfile,
1314 if (!NT_STATUS_IS_OK(status)) {
1319 * ensure cacl_set_cb gets called for children
1320 * of this directory (targetpath)
1322 status = cli_list(targetcli, targetpath,
1323 attribute, cacl_set_cb,
1324 (void *)&dir_cbstate);
1326 if (!NT_STATUS_IS_OK(status)) {
1332 * build full path to caclfile and replace '/' with '\' so
1333 * other utility functions can deal with it
1336 targetpath = talloc_asprintf(dirctx, "%s/%s", dir, f->name);
1338 status = NT_STATUS_NO_MEMORY;
1341 string_replace(targetpath, '/', '\\');
1343 /* attempt to propagate any inherited ace to file caclfile */
1344 status = propagate_inherited_aces(targetpath, cbstate);
1346 if (!NT_STATUS_IS_OK(status)) {
1350 status = NT_STATUS_OK;
1352 if (!NT_STATUS_IS_OK(status)) {
1353 d_printf("error %s: processing %s\n",
1357 TALLOC_FREE(dirctx);
1363 * Wrapper around cl_list to descend the directory tree pointed to by 'filename',
1364 * helper callback function 'cacl_set_cb' handles the child elements processed
1367 static int inheritance_cacl_set(char *filename,
1368 struct cacl_callback_state *cbstate)
1374 struct cli_state *cli = cbstate->cli;
1375 TALLOC_CTX *ctx = NULL;
1376 bool isdirectory = false;
1377 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM
1378 | FILE_ATTRIBUTE_HIDDEN;
1379 ctx = talloc_init("inherit_set");
1381 d_printf("out of memory\n");
1382 result = EXIT_FAILED;
1386 /* ensure we have a filename that starts with '\' */
1387 if (!filename || *filename != DIRSEP_CHAR) {
1388 /* illegal or no filename */
1389 result = EXIT_FAILED;
1390 d_printf("illegal or missing name '%s'\n", filename);
1395 fileattr = get_fileinfo(cli, filename);
1396 isdirectory = (fileattr & FILE_ATTRIBUTE_DIRECTORY)
1397 == FILE_ATTRIBUTE_DIRECTORY;
1400 * if we've got as far as here then we have already evaluated
1409 /* make sure we have a trailing '\*' for directory */
1411 mask = talloc_strdup(ctx, filename);
1412 } else if (strlen(filename) > 1) {
1414 * if the passed file name doesn't have a trailing '\'
1417 char *name_end = strrchr(filename, DIRSEP_CHAR);
1418 if (name_end != filename + strlen(filename) + 1) {
1419 mask = talloc_asprintf(ctx, "%s\\*", filename);
1421 mask = talloc_strdup(ctx, filename);
1424 /* filename is a single '\', just append '*' */
1425 mask = talloc_asprintf_append(mask, "%s*", filename);
1429 result = EXIT_FAILED;
1434 * prepare for automatic propagation of the acl passed on the
1438 ntstatus = prepare_inheritance_propagation(ctx, filename,
1440 if (!NT_STATUS_IS_OK(ntstatus)) {
1441 d_printf("error: %s processing %s\n",
1442 nt_errstr(ntstatus), filename);
1443 result = EXIT_FAILED;
1447 result = cacl_set_from_sd(cli, filename, cbstate->aclsd,
1448 cbstate->mode, cbstate->numeric);
1451 * strictly speaking it could be considered an error if a file was
1452 * specified with '--propagate-inheritance'. However we really want
1453 * to eventually get rid of '--propagate-inheritance' so we will be
1454 * more forgiving here and instead just exit early.
1456 if (!isdirectory || (result != EXIT_OK)) {
1460 /* check if there is actually any need to propagate */
1461 if (cbstate->acl_no_propagate) {
1464 /* get inheritable attributes this parent container (e.g. filename) */
1465 ntstatus = get_inheritable_aces(ctx, filename, cbstate);
1466 if (NT_STATUS_IS_OK(ntstatus)) {
1467 /* process children */
1468 ntstatus = cli_list(cli, mask, attribute,
1473 if (!NT_STATUS_IS_OK(ntstatus)) {
1474 d_printf("error: %s processing %s\n",
1475 nt_errstr(ntstatus), filename);
1476 result = EXIT_FAILED;
1486 struct diritem *prev, *next;
1488 * dirname and targetpath below are sanitized,
1490 * + start and end with '\'
1491 * + have no trailing '*'
1492 * + all '/' have been converted to '\'
1496 struct cli_state *targetcli;
1499 struct save_restore_stats
1505 struct dump_context {
1506 struct diritem *list;
1507 struct cli_credentials *creds;
1508 struct cli_state *cli;
1509 struct save_restore_stats *stats;
1511 struct diritem *dir;
1515 static int write_dacl(struct dump_context *ctx,
1516 struct cli_state *cli,
1517 const char *filename,
1518 const char *origfname)
1520 struct security_descriptor *sd = NULL;
1522 const char *output_fmt = "%s\r\n%s\r\n";
1523 const char *tmp = NULL;
1524 char *out_str = NULL;
1525 uint8_t *dest = NULL;
1530 TALLOC_CTX *frame = talloc_stackframe();
1536 if (ctx->save_fd < 0) {
1537 DBG_ERR("error processing %s no file descriptor\n", filename);
1538 result = EXIT_FAILED;
1542 sd = get_secdesc(cli, filename);
1544 result = EXIT_FAILED;
1548 sd->owner_sid = NULL;
1549 sd->group_sid = NULL;
1551 str = sddl_encode(frame, sd, get_domain_sid(cli));
1553 DBG_ERR("error processing %s couldn't encode DACL\n", filename);
1554 result = EXIT_FAILED;
1558 * format of icacls save file is
1559 * a line containing the path of the file/dir
1560 * followed by a line containing the sddl format
1562 * The format of the strings are null terminated
1563 * 16-bit Unicode. Each line is terminated by "\r\n"
1567 /* skip leading '\' */
1568 if (tmp[0] == '\\') {
1571 out_str = talloc_asprintf(frame, output_fmt, tmp, str);
1573 if (out_str == NULL) {
1574 result = EXIT_FAILED;
1578 s_len = strlen(out_str);
1580 ok = convert_string_talloc(out_str,
1584 s_len, (void **)(void *)&dest, &d_len);
1586 DBG_ERR("error processing %s out of memory\n", tmp);
1587 result = EXIT_FAILED;
1591 if (write(ctx->save_fd, dest, d_len) != d_len) {
1592 DBG_ERR("error processing %s failed to write to file.\n", tmp);
1593 result = EXIT_FAILED;
1596 fsync(ctx->save_fd);
1599 ctx->stats->success += 1;
1600 fprintf(stdout, "Successfully processed file: %s\n", tmp);
1603 if (result != EXIT_OK) {
1604 ctx->stats->failure += 1;
1610 * Sanitize directory name.
1611 * Given a directory name 'dir' ensure it;
1614 * o doesn't end with trailing '*'
1615 * o ensure all '/' are converted to '\'
1618 static char *sanitize_dirname(TALLOC_CTX *ctx,
1622 char *name_end = NULL;
1624 mask = talloc_strdup(ctx, dir);
1625 name_end = strrchr(mask, '*');
1630 name_end = strrchr(mask, DIRSEP_CHAR);
1632 if (strlen(mask) > 0 && name_end != mask + (strlen(mask) - 1)) {
1633 mask = talloc_asprintf(ctx, "%s\\", mask);
1636 string_replace(mask, '/', '\\');
1641 * Process each entry (child) of a directory.
1642 * Each entry, regardless of whether it is itself a file or directory
1643 * has it's dacl written to the restore/save file.
1644 * Each directory is saved to context->list (for further processing)
1645 * write_dacl will update the stats (success/fail)
1647 static NTSTATUS cacl_dump_dacl_cb(struct file_info *f,
1648 const char *mask, void *state)
1650 struct dump_context *ctx = talloc_get_type_abort(state,
1651 struct dump_context);
1656 char *targetpath = NULL;
1657 char *unresolved = NULL;
1660 * if we have already encountered an error
1663 if (!NT_STATUS_IS_OK(ctx->status)) {
1667 if (!f->name || !f->name[0]) {
1668 DBG_ERR("Empty dir name returned. Possible server "
1669 "misconfiguration.\n");
1670 status = NT_STATUS_UNSUCCESSFUL;
1674 mask2 = sanitize_dirname(ctx, mask);
1676 status = NT_STATUS_NO_MEMORY;
1679 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
1680 struct diritem *item = NULL;
1682 /* ignore special '.' & '..' */
1683 if ((f->name == NULL) || ISDOT(f->name) || ISDOTDOT(f->name)) {
1684 status = NT_STATUS_OK;
1688 /* Work out the directory. */
1689 unresolved = sanitize_dirname(ctx, ctx->dir->dirname);
1691 status = NT_STATUS_NO_MEMORY;
1695 unresolved = talloc_asprintf(ctx, "%s%s", unresolved, f->name);
1697 if (unresolved == NULL) {
1698 status = NT_STATUS_NO_MEMORY;
1702 item = talloc_zero(ctx, struct diritem);
1704 status = NT_STATUS_NO_MEMORY;
1708 item->dirname = unresolved;
1710 mask2 = talloc_asprintf(ctx, "%s%s", mask2, f->name);
1712 status = NT_STATUS_NO_MEMORY;
1716 status = cli_resolve_path(ctx, "", ctx->creds, ctx->cli,
1717 mask2, &item->targetcli, &targetpath);
1719 if (!NT_STATUS_IS_OK(status)) {
1720 DBG_ERR("error failed to resolve: %s\n",
1725 item->targetpath = sanitize_dirname(ctx, targetpath);
1726 if (!item->targetpath) {
1727 status = NT_STATUS_NO_MEMORY;
1733 item->targetpath, unresolved) != EXIT_OK) {
1734 status = NT_STATUS_UNSUCCESSFUL;
1736 * cli_list happily ignores error encountered
1737 * when processing the callback so we need
1738 * to save any error status encountered while
1739 * processing directories (so we can stop recursing
1740 * those as soon as possible).
1741 * Changing the current behaviour of the callback
1742 * handling by cli_list would be I think be too
1745 ctx->status = status;
1749 DLIST_ADD_END(ctx->list, item);
1752 unresolved = sanitize_dirname(ctx, ctx->dir->dirname);
1754 status = NT_STATUS_NO_MEMORY;
1758 unresolved = talloc_asprintf(ctx, "%s%s", unresolved, f->name);
1761 status = NT_STATUS_NO_MEMORY;
1765 * build full path to the file and replace '/' with '\' so
1766 * other utility functions can deal with it
1769 targetpath = talloc_asprintf(ctx, "%s%s", mask2, f->name);
1772 status = NT_STATUS_NO_MEMORY;
1777 ctx->dir->targetcli,
1778 targetpath, unresolved) != EXIT_OK) {
1779 status = NT_STATUS_UNSUCCESSFUL;
1781 * cli_list happily ignores error encountered
1782 * when processing the callback so we need
1783 * to save any error status encountered while
1784 * processing directories (so we can stop recursing
1785 * those as soon as possible).
1786 * Changing the current behaviour of the callback
1787 * handling by cli_list would be I think be too
1790 ctx->status = status;
1794 status = NT_STATUS_OK;
1796 if (!NT_STATUS_IS_OK(status)) {
1797 DBG_ERR("error %s: processing %s\n",
1798 nt_errstr(status), targetpath);
1804 * dump_ctx contains a list of directories to be processed
1805 * + each directory 'dir' is scanned by cli_list, the cli_list
1806 * callback 'cacl_dump_dacl_cb' writes out the dacl of each
1807 * child of 'dir' (regardless of whether it is a dir or file)
1808 * to the restore/save file. Additionally any directories encountered
1809 * are returned in the passed in dump_ctx->list member
1810 * + the directory list returned from cli_list is passed and processed
1811 * by recursively calling dump_dacl_dirtree
1814 static int dump_dacl_dirtree(struct dump_context *dump_ctx)
1816 struct diritem *item = NULL;
1817 struct dump_context *new_dump_ctx = NULL;
1819 for (item = dump_ctx->list; item; item = item->next) {
1820 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY
1821 | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1825 new_dump_ctx = talloc_zero(dump_ctx, struct dump_context);
1827 if (new_dump_ctx == NULL) {
1828 DBG_ERR("out of memory\n");
1829 result = EXIT_FAILED;
1833 if (item->targetcli == NULL) {
1834 status = cli_resolve_path(new_dump_ctx,
1841 if (!NT_STATUS_IS_OK(status)) {
1842 DBG_ERR("failed to resolve path %s "
1844 item->dirname, nt_errstr(status));
1845 result = EXIT_FAILED;
1849 new_dump_ctx->creds = dump_ctx->creds;
1850 new_dump_ctx->save_fd = dump_ctx->save_fd;
1851 new_dump_ctx->stats = dump_ctx->stats;
1852 new_dump_ctx->dir = item;
1853 new_dump_ctx->cli = item->targetcli;
1855 mask = talloc_asprintf(new_dump_ctx, "%s*",
1856 new_dump_ctx->dir->targetpath);
1857 status = cli_list(new_dump_ctx->dir->targetcli,
1859 attribute, cacl_dump_dacl_cb, new_dump_ctx);
1861 if (!NT_STATUS_IS_OK(status) ||
1862 !NT_STATUS_IS_OK(new_dump_ctx->status)) {
1864 if (!NT_STATUS_IS_OK(status)) {
1866 * cli_list failed for some reason
1867 * so we need to update the failure stat
1869 new_dump_ctx->stats->failure += 1;
1872 /* cacl_dump_dacl_cb should have updated stat */
1873 tmpstatus = new_dump_ctx->status;
1875 DBG_ERR("error %s: processing %s\n",
1876 nt_errstr(tmpstatus), item->dirname);
1877 result = EXIT_FAILED;
1880 result = dump_dacl_dirtree(new_dump_ctx);
1881 if (result != EXIT_OK) {
1888 TALLOC_FREE(new_dump_ctx);
1892 static int cacl_dump_dacl(struct cli_state *cli,
1893 struct cli_credentials *creds,
1898 TALLOC_CTX *ctx = NULL;
1899 bool isdirectory = false;
1901 struct dump_context *dump_ctx = NULL;
1902 struct save_restore_stats stats = {0};
1903 struct diritem *item = NULL;
1904 struct cli_state *targetcli = NULL;
1905 char *targetpath = NULL;
1908 ctx = talloc_init("cacl_dump");
1910 DBG_ERR("out of memory\n");
1911 result = EXIT_FAILED;
1915 dump_ctx = talloc_zero(ctx, struct dump_context);
1916 if (dump_ctx == NULL) {
1917 DBG_ERR("out of memory\n");
1918 result = EXIT_FAILED;
1922 dump_ctx->save_fd = open(save_file,
1923 O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1925 if (dump_ctx->save_fd < 0) {
1926 result = EXIT_FAILED;
1930 dump_ctx->creds = creds;
1931 dump_ctx->cli = cli;
1932 dump_ctx->stats = &stats;
1934 /* ensure we have a filename that starts with '\' */
1935 if (!filename || *filename != DIRSEP_CHAR) {
1936 /* illegal or no filename */
1937 result = EXIT_FAILED;
1938 DBG_ERR("illegal or missing name '%s'\n", filename);
1942 status = cli_resolve_path(dump_ctx, "",
1945 filename, &targetcli, &targetpath);
1946 if (!NT_STATUS_IS_OK(status)) {
1947 DBG_ERR("failed resolve %s\n", filename);
1948 result = EXIT_FAILED;
1952 fileattr = get_fileinfo(targetcli, targetpath);
1953 isdirectory = (fileattr & FILE_ATTRIBUTE_DIRECTORY)
1954 == FILE_ATTRIBUTE_DIRECTORY;
1957 * if we've got as far as here then we have already evaluated
1966 /* make sure we have a trailing '\*' for directory */
1968 mask = talloc_strdup(ctx, filename);
1969 } else if (strlen(filename) > 1) {
1970 mask = sanitize_dirname(ctx, filename);
1972 /* filename is a single '\' */
1973 mask = talloc_strdup(ctx, filename);
1976 result = EXIT_FAILED;
1980 write_dacl(dump_ctx, targetcli, targetpath, filename);
1981 if (isdirectory && recurse) {
1982 item = talloc_zero(dump_ctx, struct diritem);
1984 result = EXIT_FAILED;
1987 item->dirname = mask;
1988 DLIST_ADD_END(dump_ctx->list, item);
1989 dump_dacl_dirtree(dump_ctx);
1992 fprintf(stdout, "Successfully processed %d files: "
1993 "Failed processing %d files\n",
1994 dump_ctx->stats->success, dump_ctx->stats->failure);
1997 if (dump_ctx && dump_ctx->save_fd) {
1998 close(dump_ctx->save_fd);
2004 /****************************************************************************
2006 ****************************************************************************/
2007 int main(int argc, char *argv[])
2009 const char **argv_const = discard_const_p(const char *, argv);
2012 enum acl_mode mode = SMB_ACL_SET;
2013 static char *the_acl = NULL;
2014 enum chown_mode change_mode = REQUEST_NONE;
2017 char *filename = NULL;
2019 /* numeric is set when the user wants numeric SIDs and ACEs rather
2020 than going via LSA calls to resolve them */
2022 struct cli_state *targetcli = NULL;
2023 struct cli_credentials *creds = NULL;
2024 char *targetfile = NULL;
2027 struct loadparm_context *lp_ctx = NULL;
2029 struct poptOption long_options[] = {
2032 .longName = "delete",
2034 .argInfo = POPT_ARG_STRING,
2037 .descrip = "Delete an acl",
2038 .argDescrip = "ACL",
2041 .longName = "modify",
2043 .argInfo = POPT_ARG_STRING,
2046 .descrip = "Modify an acl",
2047 .argDescrip = "ACL",
2052 .argInfo = POPT_ARG_STRING,
2055 .descrip = "Add an acl",
2056 .argDescrip = "ACL",
2061 .argInfo = POPT_ARG_STRING,
2064 .descrip = "Set acls",
2065 .argDescrip = "ACLS",
2068 .longName = "chown",
2070 .argInfo = POPT_ARG_STRING,
2073 .descrip = "Change ownership of a file",
2074 .argDescrip = "USERNAME",
2077 .longName = "chgrp",
2079 .argInfo = POPT_ARG_STRING,
2082 .descrip = "Change group ownership of a file",
2083 .argDescrip = "GROUPNAME",
2086 .longName = "inherit",
2088 .argInfo = POPT_ARG_STRING,
2091 .descrip = "Inherit allow|remove|copy",
2094 .longName = "propagate-inheritance",
2096 .argInfo = POPT_ARG_NONE,
2097 .arg = &inheritance,
2099 .descrip = "Supports propagation of inheritable ACE(s) when used in conjunction with add, delete, set or modify",
2104 .argInfo = POPT_ARG_STRING,
2107 .descrip = "stores the DACLs in sddl format of the "
2108 "specified file or folder for later use "
2109 "with restore. SACLS, owner or integrity"
2110 " labels are not stored",
2113 .longName = "restore",
2115 .argInfo = POPT_ARG_STRING,
2116 .arg = &restore_file,
2118 .descrip = "applies the stored DACLS to files in "
2122 .longName = "recurse",
2124 .argInfo = POPT_ARG_NONE,
2127 .descrip = "indicates the operation is performed "
2128 "on directory and all files/directories"
2129 " below. (only applies to save option)",
2132 .longName = "numeric",
2134 .argInfo = POPT_ARG_NONE,
2137 .descrip = "Don't resolve sids or masks to names",
2142 .argInfo = POPT_ARG_NONE,
2145 .descrip = "Output and input acls in sddl format",
2148 .longName = "query-security-info",
2150 .argInfo = POPT_ARG_INT,
2151 .arg = &query_sec_info,
2153 .descrip = "The security-info flags for queries"
2156 .longName = "set-security-info",
2158 .argInfo = POPT_ARG_INT,
2159 .arg = &set_sec_info,
2161 .descrip = "The security-info flags for modifications"
2164 .longName = "test-args",
2166 .argInfo = POPT_ARG_NONE,
2169 .descrip = "Test arguments"
2172 .longName = "domain-sid",
2174 .argInfo = POPT_ARG_STRING,
2177 .descrip = "Domain SID for sddl",
2178 .argDescrip = "SID"},
2180 .longName = "maximum-access",
2182 .argInfo = POPT_ARG_NONE,
2185 .descrip = "Query maximum permissions",
2188 POPT_COMMON_CONNECTION
2189 POPT_COMMON_CREDENTIALS
2195 struct cli_state *cli;
2196 TALLOC_CTX *frame = talloc_stackframe();
2197 const char *owner_username = "";
2202 ok = samba_cmdline_init(frame,
2203 SAMBA_CMDLINE_CONFIG_CLIENT,
2204 false /* require_smbconf */);
2206 DBG_ERR("Failed to init cmdline parser!\n");
2210 lp_ctx = samba_cmdline_get_lp_ctx();
2211 /* set default debug level to 1 regardless of what smb.conf sets */
2212 lpcfg_set_cmdline(lp_ctx, "log level", "1");
2216 pc = samba_popt_get_context(getprogname(),
2222 DBG_ERR("Failed to setup popt context!\n");
2227 poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
2228 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
2230 while ((opt = poptGetNextOpt(pc)) != -1) {
2233 the_acl = smb_xstrdup(poptGetOptArg(pc));
2238 the_acl = smb_xstrdup(poptGetOptArg(pc));
2239 mode = SMB_ACL_DELETE;
2243 the_acl = smb_xstrdup(poptGetOptArg(pc));
2244 mode = SMB_ACL_MODIFY;
2248 the_acl = smb_xstrdup(poptGetOptArg(pc));
2253 owner_username = poptGetOptArg(pc);
2254 change_mode = REQUEST_CHOWN;
2258 owner_username = poptGetOptArg(pc);
2259 change_mode = REQUEST_CHGRP;
2263 owner_username = poptGetOptArg(pc);
2264 change_mode = REQUEST_INHERIT;
2267 lpcfg_set_cmdline(lp_ctx, "client max protocol", poptGetOptArg(pc));
2272 case POPT_ERROR_BADOPT:
2273 fprintf(stderr, "\nInvalid option %s: %s\n\n",
2274 poptBadOption(pc, 0), poptStrerror(opt));
2275 poptPrintUsage(pc, stderr, 0);
2279 if (inheritance && !the_acl) {
2280 poptPrintUsage(pc, stderr, 0);
2284 if(!poptPeekArg(pc)) {
2285 poptPrintUsage(pc, stderr, 0);
2289 path = talloc_strdup(frame, poptGetArg(pc));
2294 if (strncmp(path, "\\\\", 2) && strncmp(path, "//", 2)) {
2295 printf("Invalid argument: %s\n", path);
2299 if(!poptPeekArg(pc)) {
2300 poptPrintUsage(pc, stderr, 0);
2304 filename = talloc_strdup(frame, poptGetArg(pc));
2309 poptFreeContext(pc);
2310 samba_cmdline_burn(argc, argv);
2312 string_replace(path,'/','\\');
2314 server = talloc_strdup(frame, path+2);
2318 share = strchr_m(server,'\\');
2319 if (share == NULL) {
2320 printf("Invalid argument\n");
2327 creds = samba_cmdline_get_creds();
2329 /* Make connection to server */
2331 cli = connect_one(creds, server, share);
2339 string_replace(filename, '/', '\\');
2340 if (filename[0] != '\\') {
2341 filename = talloc_asprintf(frame,
2349 status = cli_resolve_path(frame,
2356 if (!NT_STATUS_IS_OK(status)) {
2357 DEBUG(0,("cli_resolve_path failed for %s! (%s)\n", filename, nt_errstr(status)));
2361 /* Perform requested action */
2363 if (change_mode == REQUEST_INHERIT) {
2364 result = inherit(targetcli, targetfile, owner_username);
2365 } else if (change_mode != REQUEST_NONE) {
2366 result = owner_set(targetcli, change_mode, targetfile, owner_username);
2367 } else if (the_acl) {
2369 struct cacl_callback_state cbstate = {
2376 result = inheritance_cacl_set(targetfile, &cbstate);
2378 result = cacl_set(targetcli,
2387 result = cacl_dump_dacl(cli, creds, filename);
2389 result = cacl_dump(targetcli, targetfile, numeric);