1 After applying this patch, run these commands for a successful build:
4 ./configure --enable-acl-support
7 The program currently complains when the --acls (-A) option is used to copy
8 from a disk that doesn't support ACLs. This should be changed to silently
9 notice that no ACLs are available to copy. Of course, trying to write out
10 ACLs to a non-ACL-supporting disk should complain.
14 @@ -25,15 +25,15 @@ VERSION=@VERSION@
18 -HEADERS=byteorder.h config.h errcode.h proto.h rsync.h lib/pool_alloc.h
19 +HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
20 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
21 - lib/permstring.o lib/pool_alloc.o @LIBOBJS@
22 + lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
23 ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
24 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
25 OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
26 main.o checksum.o match.o syscall.o log.o backup.o
27 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
28 - fileio.o batch.o clientname.o chmod.o
29 + fileio.o batch.o clientname.o chmod.o acls.o
30 OBJS3=progress.o pipe.o
31 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
32 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
36 +/* -*- c-file-style: "linux" -*-
37 + Copyright (C) Andrew Tridgell 1996
38 + Copyright (C) Paul Mackerras 1996
40 + This program is free software; you can redistribute it and/or modify
41 + it under the terms of the GNU General Public License as published by
42 + the Free Software Foundation; either version 2 of the License, or
43 + (at your option) any later version.
45 + This program is distributed in the hope that it will be useful,
46 + but WITHOUT ANY WARRANTY; without even the implied warranty of
47 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 + GNU General Public License for more details.
50 + You should have received a copy of the GNU General Public License
51 + along with this program; if not, write to the Free Software
52 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
55 +/* handle passing ACLs between systems */
58 +#include "lib/sysacls.h"
64 +extern int orig_umask;
69 + SMB_ACL_TAG_T tag_type;
78 +static const rsync_acl rsync_acl_initializer = { 0, 0, NULL };
80 +#define OTHER_TYPE(t) (SMB_ACL_TYPE_ACCESS+SMB_ACL_TYPE_DEFAULT-(t))
81 +#define BUMP_TYPE(t) ((t = OTHER_TYPE(t)) == SMB_ACL_TYPE_DEFAULT)
83 +static void expand_rsync_acl(rsync_acl *racl)
85 + /* First time through, 0 <= 0, so list is expanded.
86 + * (Diabolical, rsync guys!) */
87 + if (racl->malloced <= racl->count) {
89 + size_t new_size = racl->malloced + 10;
90 + new_ptr = realloc_array(racl->races, rsync_ace, new_size);
92 + rprintf(FINFO, "expand rsync_acl to %.0f bytes, did%s move\n",
93 + (double) new_size * sizeof racl->races[0],
94 + racl->races ? "" : " not");
97 + racl->races = new_ptr;
98 + racl->malloced = new_size;
101 + out_of_memory("expand_rsync_acl");
105 +static void rsync_acl_free(rsync_acl *racl)
108 + racl->races = NULL;
110 + racl->malloced = 0;
113 +static int rsync_ace_sorter(const void *r1, const void *r2)
115 + rsync_ace *race1 = (rsync_ace *)r1;
116 + SMB_ACL_TAG_T rtag1 = race1->tag_type;
117 + id_t rid1 = race1->id;
118 + rsync_ace *race2 = (rsync_ace *)r2;
119 + SMB_ACL_TAG_T rtag2 = race2->tag_type;
120 + id_t rid2 = race2->id;
121 + /* start at the extrema */
122 + if (rtag1 == SMB_ACL_USER_OBJ || rtag2 == SMB_ACL_MASK)
124 + if (rtag2 == SMB_ACL_USER_OBJ || rtag1 == SMB_ACL_MASK)
127 + if (rtag1 == SMB_ACL_OTHER)
129 + if (rtag2 == SMB_ACL_OTHER)
131 + /* only SMB_ACL_USERs and SMB_ACL_GROUP*s left */
132 + if (rtag1 == SMB_ACL_USER) {
134 + case SMB_ACL_GROUP:
135 + case SMB_ACL_GROUP_OBJ:
136 + case SMB_ACL_OTHER:
140 + return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
142 + if (rtag2 == SMB_ACL_USER)
144 + /* only SMB_ACL_GROUP*s to worry about; kick out GROUP_OBJs first */
145 + if (rtag1 == SMB_ACL_GROUP_OBJ)
147 + if (rtag2 == SMB_ACL_GROUP_OBJ)
149 + /* only SMB_ACL_GROUPs left */
150 + return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
153 +static void sort_rsync_acl(rsync_acl *racl)
157 + qsort((void **)racl->races, racl->count, sizeof racl->races[0],
158 + &rsync_ace_sorter);
161 +static BOOL unpack_smb_acl(rsync_acl *racl, SMB_ACL_T sacl)
163 + SMB_ACL_ENTRY_T entry;
165 + const char *errfun;
166 + *racl = rsync_acl_initializer;
167 + errfun = "sys_acl_get_entry";
168 + for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
170 + rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
171 + SMB_ACL_PERMSET_T permset;
174 + expand_rsync_acl(racl);
175 + race = &racl->races[racl->count++];
176 + if ((rc = sys_acl_get_tag_type(entry, &race->tag_type))) {
177 + errfun = "sys_acl_get_tag_type";
180 + if ((rc = sys_acl_get_permset(entry, &permset))) {
181 + errfun = "sys_acl_get_tag_type";
184 + race->access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
185 + | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
186 + | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
187 + switch (race->tag_type) {
189 + case SMB_ACL_GROUP:
194 + if (!(qualifier = sys_acl_get_qualifier(entry))) {
195 + errfun = "sys_acl_get_tag_type";
199 + race->id = *((id_t *)qualifier);
200 + sys_acl_free_qualifier(qualifier, race->tag_type);
203 + rprintf(FERROR, "unpack_smb_acl: %s(): %s\n",
204 + errfun, strerror(errno));
205 + rsync_acl_free(racl);
208 + sort_rsync_acl(racl);
212 +static BOOL rsync_acls_equal(const rsync_acl *racl1, const rsync_acl *racl2)
214 + rsync_ace *race1, *race2;
215 + size_t count = racl1->count;
216 + if (count != racl2->count)
218 + race1 = racl1->races;
219 + race2 = racl2->races;
220 + for (; count--; race1++, race2++) {
221 + if (race1->tag_type != race2->tag_type
222 + || race1->access != race2->access
223 + || ((race1->tag_type == SMB_ACL_USER
224 + || race1->tag_type == SMB_ACL_GROUP)
225 + && race1->id != race2->id))
237 +static rsync_acl_list _rsync_acl_lists[] = {
238 + { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
239 + { 0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
242 +static inline rsync_acl_list *rsync_acl_lists(SMB_ACL_TYPE_T type)
244 + return type == SMB_ACL_TYPE_ACCESS ? &_rsync_acl_lists[0]
245 + : &_rsync_acl_lists[1];
248 +static void expand_rsync_acl_list(rsync_acl_list *racl_list)
250 + /* First time through, 0 <= 0, so list is expanded.
251 + * (Diabolical, rsync guys!) */
252 + if (racl_list->malloced <= racl_list->count) {
253 + rsync_acl *new_ptr;
255 + if (racl_list->malloced < 1000)
256 + new_size = racl_list->malloced + 1000;
258 + new_size = racl_list->malloced * 2;
259 + new_ptr = realloc_array(racl_list->racls, rsync_acl, new_size);
260 + if (verbose >= 3) {
261 + rprintf(FINFO, "expand_rsync_acl_list to %.0f bytes, did%s move\n",
262 + (double) new_size * sizeof racl_list->racls[0],
263 + racl_list->racls ? "" : " not");
266 + racl_list->racls = new_ptr;
267 + racl_list->malloced = new_size;
269 + if (!racl_list->racls)
270 + out_of_memory("expand_rsync_acl_list");
275 +static void free_rsync_acl_list(rsync_acl_list *racl_list)
277 + /* Run this in reverse, so references are freed before referents,
278 + * although not currently necessary. */
279 + while (racl_list->count--) {
280 + rsync_acl *racl = &racl_list->racls[racl_list->count];
282 + rsync_acl_free(racl);
284 + free(racl_list->racls);
285 + racl_list->racls = NULL;
286 + racl_list->malloced = 0;
290 +static int find_matching_rsync_acl(SMB_ACL_TYPE_T type,
291 + const rsync_acl_list *racl_list,
292 + const rsync_acl *racl)
294 + static int access_match = -1, default_match = -1;
295 + int *match = (type == SMB_ACL_TYPE_ACCESS) ?
296 + &access_match : &default_match;
297 + size_t count = racl_list->count;
298 + /* If this is the first time through or we didn't match the last
299 + * time, then start at the end of the list, which should be the
300 + * best place to start hunting. */
302 + *match = racl_list->count - 1;
304 + if (rsync_acls_equal(&racl_list->racls[*match], racl))
307 + *match = racl_list->count - 1;
313 +/* The general strategy with the tag_type <-> character mapping is that
314 + * lowercase implies that no qualifier follows, where uppercase does.
315 + * A similar idiom for the acl type (access or default) itself, but
316 + * lowercase in this instance means there's no ACL following, so the
317 + * ACL is a repeat, so the receiver should reuse the last of the same
320 +static void send_rsync_acl(int f, const rsync_acl *racl)
323 + size_t count = racl->count;
324 + write_int(f, count);
325 + for (race = racl->races; count--; race++) {
327 + switch (race->tag_type) {
328 + case SMB_ACL_USER_OBJ:
334 + case SMB_ACL_GROUP_OBJ:
337 + case SMB_ACL_GROUP:
340 + case SMB_ACL_OTHER:
348 + "send_rsync_acl: unknown tag_type (%02x) on ACE; disregarding\n",
353 + write_byte(f, race->access);
354 + if (isupper((int)ch)) {
355 + write_int(f, race->id);
356 + /* FIXME: sorta wasteful: we should maybe buffer as
357 + * many ids as max(ACL_USER + ACL_GROUP) objects to
358 + * keep from making so many calls. */
367 +static rsync_acl _curr_rsync_acls[2];
370 +static const char *str_acl_type(SMB_ACL_TYPE_T type)
372 + return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS" :
373 + type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT" :
374 + "unknown SMB_ACL_TYPE_T";
378 + * Overwrite racl with a new three-entry ACL from the given permissions.
380 +static void perms_to_acl(int perms, rsync_acl *racl)
383 + expand_rsync_acl(racl);
384 + racl->races[racl->count].tag_type = SMB_ACL_USER_OBJ;
385 + racl->races[racl->count++].access = (perms >> 6) & 7;
386 + expand_rsync_acl(racl);
387 + racl->races[racl->count].tag_type = SMB_ACL_GROUP_OBJ;
388 + racl->races[racl->count++].access = (perms >> 3) & 7;
389 + expand_rsync_acl(racl);
390 + racl->races[racl->count].tag_type = SMB_ACL_OTHER;
391 + racl->races[racl->count++].access = (perms >> 0) & 7;
394 +/* Generate the ACL(s) for this flist entry;
395 + * ACL(s) are either sent or cleaned-up by send_acl() below. */
397 +int make_acl(const struct file_struct *file, const char *fname)
399 + SMB_ACL_TYPE_T type;
400 + rsync_acl *curr_racl;
402 + if (S_ISLNK(file->mode))
405 + curr_racl = &_curr_rsync_acls[0];
406 + type = SMB_ACL_TYPE_ACCESS;
410 + *curr_racl = rsync_acl_initializer;
411 + if ((sacl = sys_acl_get_file(fname, type)) != 0) {
412 + ok = unpack_smb_acl(curr_racl, sacl);
413 + sys_acl_free_acl(sacl);
416 + } else if (errno == ENOTSUP) {
417 + /* ACLs are not supported. Invent an access ACL from
418 + * permissions; let the default ACL default to empty. */
419 + if (type == SMB_ACL_TYPE_ACCESS)
420 + perms_to_acl(file->mode & ACCESSPERMS, curr_racl);
422 + rprintf(FERROR, "send_acl: sys_acl_get_file(%s, %s): %s\n",
423 + fname, str_acl_type(type), strerror(errno));
427 + } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
432 +/* Send the make_acl()-generated ACLs for this flist entry,
433 + * or clean up after an flist entry that's not being sent (f == -1). */
435 +void send_acl(const struct file_struct *file, int f)
437 + SMB_ACL_TYPE_T type;
438 + rsync_acl *curr_racl;
440 + if (S_ISLNK(file->mode))
443 + curr_racl = &_curr_rsync_acls[0];
444 + type = SMB_ACL_TYPE_ACCESS;
447 + rsync_acl_list *racl_list = rsync_acl_lists(type);
449 + rsync_acl_free(curr_racl);
452 + if ((index = find_matching_rsync_acl(type, racl_list, curr_racl))
454 + write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
455 + write_int(f, index);
456 + rsync_acl_free(curr_racl);
458 + write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
459 + send_rsync_acl(f, curr_racl);
460 + expand_rsync_acl_list(racl_list);
461 + racl_list->racls[racl_list->count++] = *curr_racl;
464 + } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
467 +/* The below stuff is only used by the receiver: */
469 +/* structure to hold index to rsync_acl_list member corresponding to
470 + * flist->files[i] */
473 + const struct file_struct *file;
480 + file_acl_index *fileaclidxs;
481 +} file_acl_index_list;
483 +static file_acl_index_list _file_acl_index_lists[] = {
484 + {0, 0, NULL },/* SMB_ACL_TYPE_ACCESS */
485 + {0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
488 +static inline file_acl_index_list *file_acl_index_lists(SMB_ACL_TYPE_T type)
490 + return type == SMB_ACL_TYPE_ACCESS ?
491 + &_file_acl_index_lists[0] : &_file_acl_index_lists[1];
494 +static void expand_file_acl_index_list(file_acl_index_list *fileaclidx_list)
496 + /* First time through, 0 <= 0, so list is expanded.
497 + * (Diabolical, rsync guys!) */
498 + if (fileaclidx_list->malloced <= fileaclidx_list->count) {
499 + file_acl_index *new_ptr;
501 + if (fileaclidx_list->malloced < 1000)
502 + new_size = fileaclidx_list->malloced + 1000;
504 + new_size = fileaclidx_list->malloced * 2;
505 + new_ptr = realloc_array(fileaclidx_list->fileaclidxs, file_acl_index, new_size);
506 + if (verbose >= 3) {
507 + rprintf(FINFO, "expand_file_acl_index_list to %.0f bytes, did%s move\n",
508 + (double) new_size * sizeof fileaclidx_list->fileaclidxs[0],
509 + fileaclidx_list->fileaclidxs ? "" : " not");
512 + fileaclidx_list->fileaclidxs = new_ptr;
513 + fileaclidx_list->malloced = new_size;
515 + if (!fileaclidx_list->fileaclidxs)
516 + out_of_memory("expand_file_acl_index_list");
521 +static void free_file_acl_index_list(file_acl_index_list *fileaclidx_list)
523 + free(fileaclidx_list->fileaclidxs);
524 + fileaclidx_list->fileaclidxs = NULL;
525 + fileaclidx_list->malloced = 0;
529 +/* lists to hold the SMB_ACL_Ts corresponding to the rsync_acl_list entries */
537 +static smb_acl_list _smb_acl_lists[] = {
538 + { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
539 + { 0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
542 +static inline smb_acl_list *smb_acl_lists(SMB_ACL_TYPE_T type)
544 + return type == SMB_ACL_TYPE_ACCESS ? &_smb_acl_lists[0] :
545 + &_smb_acl_lists[1];
548 +static void expand_smb_acl_list(smb_acl_list *sacl_list)
550 + /* First time through, 0 <= 0, so list is expanded.
551 + * (Diabolical, rsync guys!) */
552 + if (sacl_list->malloced <= sacl_list->count) {
553 + SMB_ACL_T *new_ptr;
555 + if (sacl_list->malloced < 1000)
556 + new_size = sacl_list->malloced + 1000;
558 + new_size = sacl_list->malloced * 2;
559 + new_ptr = realloc_array(sacl_list->sacls, SMB_ACL_T, new_size);
560 + if (verbose >= 3) {
561 + rprintf(FINFO, "expand_smb_acl_list to %.0f bytes, did%s move\n",
562 + (double) new_size * sizeof sacl_list->sacls[0],
563 + sacl_list->sacls ? "" : " not");
566 + sacl_list->sacls = new_ptr;
567 + sacl_list->malloced = new_size;
569 + if (!sacl_list->sacls)
570 + out_of_memory("expand_smb_acl_list");
575 +static void free_smb_acl_list(SMB_ACL_TYPE_T type)
577 + smb_acl_list *sacl_list = smb_acl_lists(type);
578 + SMB_ACL_T *sacl = sacl_list->sacls;
579 + while (sacl_list->count--) {
581 + sys_acl_free_acl(*sacl++);
583 + free(sacl_list->sacls);
584 + sacl_list->sacls = NULL;
585 + sacl_list->malloced = 0;
589 +/* build an SMB_ACL_T corresponding to an rsync_acl */
590 +static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
592 + size_t count = racl->count;
593 + rsync_ace *race = racl->races;
594 + const char *errfun = NULL;
595 + *smb_acl = sys_acl_init(count);
597 + rprintf(FERROR, "pack_smb_acl: sys_acl_int(): %s\n",
601 + for (; count--; race++) {
602 + SMB_ACL_ENTRY_T entry;
603 + SMB_ACL_PERMSET_T permset;
604 + if (sys_acl_create_entry(smb_acl, &entry)) {
605 + errfun = "sys_acl_create)";
608 + if (sys_acl_set_tag_type(entry, race->tag_type)) {
609 + errfun = "sys_acl_set_tag";
612 + if (race->tag_type == SMB_ACL_USER ||
613 + race->tag_type == SMB_ACL_GROUP)
614 + if (sys_acl_set_qualifier(entry, (void*)&race->id)) {
615 + errfun = "sys_acl_set_qualfier";
618 + if (sys_acl_get_permset(entry, &permset)) {
619 + errfun = "sys_acl_get_permset";
622 + if (sys_acl_clear_perms(permset)) {
623 + errfun = "sys_acl_clear_perms";
626 + if (race->access & 4)
627 + if (sys_acl_add_perm(permset, SMB_ACL_READ)) {
628 + errfun = "sys_acl_add_perm";
631 + if (race->access & 2)
632 + if (sys_acl_add_perm(permset, SMB_ACL_WRITE)) {
633 + errfun = "sys_acl_add_perm";
636 + if (race->access & 1)
637 + if (sys_acl_add_perm(permset, SMB_ACL_EXECUTE)) {
638 + errfun = "sys_acl_add_perm";
641 + if (sys_acl_set_permset(entry, permset)) {
642 + errfun = "sys_acl_set_permset";
647 + sys_acl_free_acl(*smb_acl);
648 + rprintf(FERROR, "pack_smb_acl %s(): %s\n", errfun,
655 +static void receive_rsync_acl(rsync_acl *racl, int f)
658 + uchar required_mask_perm = 0;
660 + BOOL saw_mask = False;
661 + BOOL saw_user_obj = False, saw_group_obj = False,
663 + size_t count = read_int(f);
668 + uchar tag = read_byte(f);
669 + expand_rsync_acl(racl);
670 + race = &racl->races[racl->count++];
673 + race->tag_type = SMB_ACL_USER_OBJ;
674 + saw_user_obj = True;
677 + race->tag_type = SMB_ACL_USER;
680 + race->tag_type = SMB_ACL_GROUP_OBJ;
681 + saw_group_obj = True;
684 + race->tag_type = SMB_ACL_GROUP;
687 + race->tag_type = SMB_ACL_OTHER;
691 + race->tag_type = SMB_ACL_MASK;
695 + rprintf(FERROR, "receive_rsync_acl: unknown tag %c\n",
697 + exit_cleanup(RERR_STREAMIO);
699 + race->access = read_byte(f);
700 + if (race->access & ~ (4 | 2 | 1)) {
701 + rprintf(FERROR, "receive_rsync_acl: bogus permset %o\n",
703 + exit_cleanup(RERR_STREAMIO);
705 + if (race->tag_type == SMB_ACL_USER ||
706 + race->tag_type == SMB_ACL_GROUP) {
707 + race->id = read_int(f);
709 + required_mask_perm |= race->access;
713 + else if (race->tag_type == SMB_ACL_GROUP_OBJ)
714 + required_mask_perm |= race->access;
718 + if (!saw_user_obj) {
719 + expand_rsync_acl(racl);
720 + race = &racl->races[racl->count++];
721 + race->tag_type = SMB_ACL_USER_OBJ;
724 + if (!saw_group_obj) {
725 + expand_rsync_acl(racl);
726 + race = &racl->races[racl->count++];
727 + race->tag_type = SMB_ACL_GROUP_OBJ;
731 + expand_rsync_acl(racl);
732 + race = &racl->races[racl->count++];
733 + race->tag_type = SMB_ACL_OTHER;
738 + expand_rsync_acl(racl);
739 + race = &racl->races[racl->count++];
740 + race->tag_type = SMB_ACL_MASK;
741 + race->access = required_mask_perm;
744 + /* If we, a system without ACLS_NEED_MASK, received data from a
745 + * system that has masks, throw away the extraneous CLASS_OBJs. */
746 + if (saw_mask && racl->count == 4) {
747 + rsync_ace *group_obj_race = NULL, *mask_race = NULL;
750 + for (i = 0, p = racl->races; i < racl->count; i++, p++) {
751 + if (p->tag_type == SMB_ACL_MASK)
753 + else if (p->tag_type == SMB_ACL_GROUP_OBJ)
754 + group_obj_race = p;
756 + if (mask_race == NULL || group_obj_race == NULL) {
757 + rprintf(FERROR, "receive_rsync_acl: have four ACES "
758 + "and one's ACL_MASK but missing "
759 + "either it or ACL_GROUP_OBJ, "
760 + "when pruning ACL\n");
762 + /* mask off group perms with it first */
763 + group_obj_race->access &= mask_race->access;
764 + /* dump mask_race; re-slot any followers-on */
766 + if (mask_race != &racl->races[racl->count]) {
767 + *mask_race = racl->races[racl->count];
768 + saw_user_obj = False; /* force re-sort */
774 + if (!(saw_user_obj && saw_group_obj && saw_other && saw_mask))
776 + if (!(saw_user_obj && saw_group_obj && saw_other))
778 + sort_rsync_acl(racl);
781 +/* receive and build the rsync_acl_lists */
783 +void receive_acl(struct file_struct *file, int f)
785 + SMB_ACL_TYPE_T type;
788 + if (S_ISLNK(file->mode))
791 + fname = f_name(file, NULL);
792 + type = SMB_ACL_TYPE_ACCESS;
794 + file_acl_index_list *fileaclidx_list =
795 + file_acl_index_lists(type);
797 + expand_file_acl_index_list(fileaclidx_list);
799 + tag = read_byte(f);
800 + if (tag == 'A' || tag == 'a') {
801 + if (type != SMB_ACL_TYPE_ACCESS) {
802 + rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
804 + exit_cleanup(RERR_STREAMIO);
806 + } else if (tag == 'D' || tag == 'd') {
807 + if (type == SMB_ACL_TYPE_ACCESS) {
808 + rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
810 + exit_cleanup(RERR_STREAMIO);
813 + rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
815 + exit_cleanup(RERR_STREAMIO);
817 + if (tag == 'A' || tag == 'D') {
818 + rsync_acl racl = rsync_acl_initializer;
819 + rsync_acl_list *racl_list = rsync_acl_lists(type);
820 + smb_acl_list *sacl_list = smb_acl_lists(type);
821 + fileaclidx_list->fileaclidxs[fileaclidx_list->count].
822 + aclidx = racl_list->count;
823 + fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
825 + receive_rsync_acl(&racl, f);
826 + expand_rsync_acl_list(racl_list);
827 + racl_list->racls[racl_list->count++] = racl;
828 + expand_smb_acl_list(sacl_list);
829 + sacl_list->sacls[sacl_list->count++] = NULL;
831 + int index = read_int(f);
832 + rsync_acl_list *racl_list = rsync_acl_lists(type);
833 + if ((size_t) index >= racl_list->count) {
834 + rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
836 + str_acl_type(type),
838 + exit_cleanup(RERR_STREAMIO);
840 + fileaclidx_list->fileaclidxs[fileaclidx_list->count].
842 + fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
845 + } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
848 +static int file_acl_index_list_sorter(const void *f1, const void *f2)
850 + const file_acl_index *fileaclidx1 = (const file_acl_index *)f1;
851 + const file_acl_index *fileaclidx2 = (const file_acl_index *)f2;
852 + return fileaclidx1->file == fileaclidx2->file ? 0 :
853 + fileaclidx1->file < fileaclidx2->file ? -1 : 1;
856 +void sort_file_acl_index_lists()
858 + SMB_ACL_TYPE_T type;
860 + type = SMB_ACL_TYPE_ACCESS;
862 + file_acl_index_list *fileaclidx_list =
863 + file_acl_index_lists(type);
864 + if (!fileaclidx_list->count)
866 + qsort(fileaclidx_list->fileaclidxs, fileaclidx_list->count,
867 + sizeof fileaclidx_list->fileaclidxs[0],
868 + &file_acl_index_list_sorter);
869 + } while (BUMP_TYPE(type));
872 +static int find_file_acl_index(const file_acl_index_list *fileaclidx_list,
873 + const struct file_struct *file) {
874 + int low = 0, high = fileaclidx_list->count;
875 + const struct file_struct *file_mid;
879 + int mid = (high + low) / 2;
880 + file_mid = fileaclidx_list->fileaclidxs[mid].file;
881 + if (file_mid == file)
882 + return fileaclidx_list->fileaclidxs[mid].aclidx;
883 + if (file_mid > file)
887 + } while (low < high);
889 + file_mid = fileaclidx_list->fileaclidxs[low].file;
890 + if (file_mid == file)
891 + return fileaclidx_list->fileaclidxs[low].aclidx;
894 + "find_file_acl_index: can't find entry for file in list\n");
895 + exit_cleanup(RERR_STREAMIO);
899 +/* for duplicating ACLs on backups when using backup_dir */
901 +int dup_acl(const char *orig, const char *bak, mode_t mode)
903 + SMB_ACL_TYPE_T type;
906 + type = SMB_ACL_TYPE_ACCESS;
908 + SMB_ACL_T sacl_orig, sacl_bak;
909 + rsync_acl racl_orig, racl_bak;
910 + if (!(sacl_orig = sys_acl_get_file(orig, type))) {
911 + rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s\n",
912 + orig, str_acl_type(type), strerror(errno));
916 + if (!(sacl_bak = sys_acl_get_file(orig, type))) {
917 + rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s. ignoring\n",
918 + bak, str_acl_type(type), strerror(errno));
920 + /* try to forge on through */
922 + if (!unpack_smb_acl(&racl_orig, sacl_orig)) {
924 + goto out_with_sacls;
927 + if (!unpack_smb_acl(&racl_bak, sacl_bak)) {
929 + goto out_with_one_racl;
931 + if (rsync_acls_equal(&racl_orig, &racl_bak))
934 + ; /* presume they're unequal */
936 + if (type == SMB_ACL_TYPE_DEFAULT && !racl_orig.count) {
937 + if (sys_acl_delete_def_file(bak) < 0) {
938 + rprintf(FERROR, "dup_acl: sys_acl_delete_def_file(%s): %s\n",
939 + bak, strerror(errno));
942 + } else if (sys_acl_set_file(bak, type, sacl_bak) < 0) {
943 + rprintf(FERROR, "dup_acl: sys_acl_set_file(%s, %s): %s\n",
944 + bak, str_acl_type(type), strerror(errno));
949 + rsync_acl_free(&racl_bak);
951 + rsync_acl_free(&racl_orig);
954 + sys_acl_free_acl(sacl_bak);
955 + /* out_with_one_sacl: */
957 + sys_acl_free_acl(sacl_orig);
958 + } while (BUMP_TYPE(type) && S_ISDIR(mode));
963 +/* Stuff for redirecting calls to set_acl() from set_file_attrs()
964 + * for keep_backup(). */
965 +static const struct file_struct *backup_orig_file = NULL;
966 +static const char null_string[] = "";
967 +static const char *backup_orig_fname = null_string;
968 +static const char *backup_dest_fname = null_string;
969 +static SMB_ACL_T _backup_sacl[] = { NULL, NULL };
971 +void push_keep_backup_acl(const struct file_struct *file,
972 + const char *orig, const char *dest)
974 + SMB_ACL_TYPE_T type;
977 + backup_orig_file = file;
978 + backup_orig_fname = orig;
979 + backup_dest_fname = dest;
981 + sacl = &_backup_sacl[0];
982 + type = SMB_ACL_TYPE_ACCESS;
984 + if (type == SMB_ACL_TYPE_DEFAULT && !S_ISDIR(file->mode)) {
988 + if (!(*sacl = sys_acl_get_file(orig, type))) {
990 + "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
991 + orig, str_acl_type(type),
994 + } while (BUMP_TYPE(type));
997 +static int set_keep_backup_acl()
999 + SMB_ACL_TYPE_T type;
1003 + sacl = &_backup_sacl[0];
1004 + type = SMB_ACL_TYPE_ACCESS;
1007 + && sys_acl_set_file(backup_dest_fname, type, *sacl) < 0) {
1009 + "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
1010 + backup_dest_fname,
1011 + str_acl_type(type),
1015 + } while (BUMP_TYPE(type));
1020 +void cleanup_keep_backup_acl()
1022 + SMB_ACL_TYPE_T type;
1025 + backup_orig_file = NULL;
1026 + backup_orig_fname = null_string;
1027 + backup_dest_fname = null_string;
1029 + sacl = &_backup_sacl[0];
1030 + type = SMB_ACL_TYPE_ACCESS;
1033 + sys_acl_free_acl(*sacl);
1036 + } while (BUMP_TYPE(type));
1039 +/* set ACL on rsync-ed or keep_backup-ed file */
1041 +int set_acl(const char *fname, const struct file_struct *file)
1043 + int unchanged = 1;
1044 + SMB_ACL_TYPE_T type;
1046 + if (dry_run || S_ISLNK(file->mode))
1047 + return 1; /* FIXME: --dry-run needs to compute this value */
1049 + if (file == backup_orig_file) {
1050 + if (!strcmp(fname, backup_dest_fname))
1051 + return set_keep_backup_acl();
1053 + type = SMB_ACL_TYPE_ACCESS;
1055 + SMB_ACL_T sacl_orig, *sacl_new;
1056 + rsync_acl racl_orig, *racl_new;
1057 + int aclidx = find_file_acl_index(file_acl_index_lists(type),
1060 + racl_new = &(rsync_acl_lists(type)->racls[aclidx]);
1061 + sacl_new = &(smb_acl_lists(type)->sacls[aclidx]);
1062 + sacl_orig = sys_acl_get_file(fname, type);
1064 + rprintf(FERROR, "set_acl: sys_acl_get_file(%s, %s): %s\n",
1065 + fname, str_acl_type(type), strerror(errno));
1069 + ok = unpack_smb_acl(&racl_orig, sacl_orig);
1070 + sys_acl_free_acl(sacl_orig);
1075 + ok = rsync_acls_equal(&racl_orig, racl_new);
1076 + rsync_acl_free(&racl_orig);
1079 + if (type == SMB_ACL_TYPE_DEFAULT && !racl_new->count) {
1080 + if (sys_acl_delete_def_file(fname) < 0) {
1081 + rprintf(FERROR, "set_acl: sys_acl_delete_def_file(%s): %s\n",
1082 + fname, strerror(errno));
1088 + if (!pack_smb_acl(sacl_new, racl_new)) {
1092 + if (sys_acl_set_file(fname, type, *sacl_new) < 0) {
1093 + rprintf(FERROR, "set_acl: sys_acl_set_file(%s, %s): %s\n",
1094 + fname, str_acl_type(type),
1100 + if (unchanged == 1)
1102 + } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
1107 +/* Enumeration functions for uid mapping: */
1109 +/* Context -- one and only one. Should be cycled through once on uid
1110 + * mapping and once on gid mapping. */
1111 +static rsync_acl_list *_enum_racl_lists[] = {
1112 + &_rsync_acl_lists[0], &_rsync_acl_lists[1], NULL
1115 +static rsync_acl_list **enum_racl_list = &_enum_racl_lists[0];
1116 +static size_t enum_racl_index = 0;
1117 +static size_t enum_race_index = 0;
1119 +/* This returns the next tag_type id from the given acl for the next entry,
1120 + * or it returns 0 if there are no more tag_type ids in the acl. */
1122 +static id_t next_ace_id(SMB_ACL_TAG_T tag_type, const rsync_acl *racl)
1124 + for (; enum_race_index < racl->count; enum_race_index++) {
1125 + rsync_ace *race = &racl->races[enum_race_index];
1126 + if (race->tag_type == tag_type)
1129 + enum_race_index = 0;
1133 +static id_t next_acl_id(SMB_ACL_TAG_T tag_type, const rsync_acl_list *racl_list)
1135 + for (; enum_racl_index < racl_list->count; enum_racl_index++) {
1136 + rsync_acl *racl = &racl_list->racls[enum_racl_index];
1137 + id_t id = next_ace_id(tag_type, racl);
1141 + enum_racl_index = 0;
1145 +static id_t next_acl_list_id(SMB_ACL_TAG_T tag_type)
1147 + for (; *enum_racl_list; enum_racl_list++) {
1148 + id_t id = next_acl_id(tag_type, *enum_racl_list);
1152 + enum_racl_list = &_enum_racl_lists[0];
1156 +id_t next_acl_uid()
1158 + return next_acl_list_id(SMB_ACL_USER);
1161 +id_t next_acl_gid()
1163 + return next_acl_list_id(SMB_ACL_GROUP);
1166 +/* referring to the global context enum_entry, sets the entry's id */
1167 +static void set_acl_id(id_t id)
1169 + (*enum_racl_list)->racls[enum_racl_index].races[enum_race_index++].id = id;
1172 +void acl_uid_map(id_t uid)
1177 +void acl_gid_map(id_t gid)
1182 +#define PERMS_SPLICE(perms,newbits,where) (((perms) & ~(7 << (where))) | ((newbits) << (where)))
1184 +int default_perms_for_dir(const char *dir)
1188 + BOOL ok, saw_mask = False;
1194 + perms = ACCESSPERMS & ~orig_umask;
1195 + /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1196 + sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1197 + if (sacl == NULL) {
1198 + /* Couldn't get an ACL. Darn. */
1201 + /* ACLs are disabled. We could yell at the user to turn them on, but... */
1205 + /* We're doing a dry run, so the containing directory
1206 + * wasn't actually created. Don't worry about it. */
1209 + /* Otherwise fall through. */
1211 + rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1212 + dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1218 + ok = unpack_smb_acl(&racl, sacl);
1219 + sys_acl_free_acl(sacl);
1221 + rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1225 + /* Look at each default ACL entry and possibly modify three bits of `perms' accordingly.
1226 + * If there's "no" default ACL, there will be zero entries and the umask-based perms is unchanged. */
1227 + for (i = 0; i < racl.count; i++) {
1228 + switch (racl.races[i].tag_type) {
1229 + case SMB_ACL_USER_OBJ:
1230 + perms = PERMS_SPLICE(perms, racl.races[i].access, 6);
1232 + case SMB_ACL_GROUP_OBJ:
1234 + perms = PERMS_SPLICE(perms, racl.races[i].access, 3);
1236 + case SMB_ACL_MASK:
1238 + perms = PERMS_SPLICE(perms, racl.races[i].access, 3);
1240 + case SMB_ACL_OTHER:
1241 + perms = PERMS_SPLICE(perms, racl.races[i].access, 0);
1247 + rsync_acl_free(&racl);
1249 + rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1253 +#endif /* SUPPORT_ACLS */
1256 @@ -28,6 +28,7 @@ extern char *backup_suffix;
1257 extern char *backup_dir;
1260 +extern int preserve_acls;
1261 extern int preserve_devices;
1262 extern int preserve_specials;
1263 extern int preserve_links;
1264 @@ -132,6 +133,10 @@ static int make_bak_dir(char *fullpath)
1266 do_lchown(fullpath, st.st_uid, st.st_gid);
1267 do_chmod(fullpath, st.st_mode);
1268 +#ifdef SUPPORT_ACLS
1269 + if (preserve_acls)
1270 + dup_acl(end, fullpath, st.st_mode);
1275 @@ -185,6 +190,11 @@ static int keep_backup(char *fname)
1276 if (!(buf = get_backup_name(fname)))
1279 +#ifdef SUPPORT_ACLS
1280 + if (preserve_acls)
1281 + push_keep_backup_acl(file, fname, buf);
1284 /* Check to see if this is a device file, or link */
1285 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1286 || (preserve_specials && IS_SPECIAL(file->mode))) {
1287 @@ -260,6 +270,10 @@ static int keep_backup(char *fname)
1290 set_file_attrs(buf, file, NULL, 0);
1291 +#ifdef SUPPORT_ACLS
1292 + if (preserve_acls)
1293 + cleanup_keep_backup_acl();
1298 --- old/configure.in
1299 +++ new/configure.in
1300 @@ -482,6 +482,11 @@ if test x"$ac_cv_func_strcasecmp" = x"no
1301 AC_CHECK_LIB(resolv, strcasecmp)
1304 +AC_CHECK_FUNCS(aclsort)
1305 +if test x"$ac_cv_func_aclsort" = x"no"; then
1306 + AC_CHECK_LIB(sec, aclsort)
1309 dnl At the moment we don't test for a broken memcmp(), because all we
1310 dnl need to do is test for equality, not comparison, and it seems that
1311 dnl every platform has a memcmp that can do at least that.
1312 @@ -738,6 +743,77 @@ AC_SUBST(OBJ_RESTORE)
1313 AC_SUBST(CC_SHOBJ_FLAG)
1314 AC_SUBST(BUILD_POPT)
1316 +AC_CHECK_HEADERS(sys/acl.h)
1317 +AC_CHECK_FUNCS(_acl __acl _facl __facl)
1318 +#################################################
1319 +# check for ACL support
1321 +AC_MSG_CHECKING(whether to support ACLs)
1322 +AC_ARG_ENABLE(acl-support,
1323 +AC_HELP_STRING([--enable-acl-support], [Include ACL support (default=no)]),
1324 +[ case "$enableval" in
1327 + case "$host_os" in
1329 + AC_MSG_RESULT(Using UnixWare ACLs)
1330 + AC_DEFINE(HAVE_UNIXWARE_ACLS, 1, [true if you have UnixWare ACLs])
1333 + AC_MSG_RESULT(Using solaris ACLs)
1334 + AC_DEFINE(HAVE_SOLARIS_ACLS, 1, [true if you have solaris ACLs])
1337 + AC_MSG_RESULT(Using HPUX ACLs)
1338 + AC_DEFINE(HAVE_HPUX_ACLS, 1, [true if you have HPUX ACLs])
1341 + AC_MSG_RESULT(Using IRIX ACLs)
1342 + AC_DEFINE(HAVE_IRIX_ACLS, 1, [true if you have IRIX ACLs])
1345 + AC_MSG_RESULT(Using AIX ACLs)
1346 + AC_DEFINE(HAVE_AIX_ACLS, 1, [true if you have AIX ACLs])
1349 + AC_MSG_RESULT(Using Tru64 ACLs)
1350 + AC_DEFINE(HAVE_TRU64_ACLS, 1, [true if you have Tru64 ACLs])
1351 + LIBS="$LIBS -lpacl"
1354 + AC_MSG_RESULT(ACLs requested -- running tests)
1355 + AC_CHECK_LIB(acl,acl_get_file)
1356 + AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1357 + AC_TRY_LINK([#include <sys/types.h>
1358 +#include <sys/acl.h>],
1359 +[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1360 +samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
1361 + if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1362 + AC_MSG_RESULT(Using posix ACLs)
1363 + AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
1364 + AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
1365 + AC_TRY_LINK([#include <sys/types.h>
1366 +#include <sys/acl.h>],
1367 +[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1368 +samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
1369 + if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1370 + AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1373 + AC_MSG_ERROR(Failed to find ACL support)
1380 + AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1383 + AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1387 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1392 @@ -44,6 +44,7 @@ extern int filesfrom_fd;
1393 extern int one_file_system;
1394 extern int copy_dirlinks;
1395 extern int keep_dirlinks;
1396 +extern int preserve_acls;
1397 extern int preserve_links;
1398 extern int preserve_hard_links;
1399 extern int preserve_devices;
1400 @@ -966,6 +967,10 @@ static struct file_struct *send_file_nam
1401 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1404 +#ifdef SUPPORT_ACLS
1405 + if (preserve_acls && make_acl(file, fname) < 0)
1409 if (chmod_modes && !S_ISLNK(file->mode))
1410 file->mode = tweak_mode(file->mode, chmod_modes);
1411 @@ -977,6 +982,16 @@ static struct file_struct *send_file_nam
1412 if (file->basename[0]) {
1413 flist->files[flist->count++] = file;
1414 send_file_entry(file, f);
1415 +#ifdef SUPPORT_ACLS
1416 + if (preserve_acls)
1417 + send_acl(file, f);
1420 +#ifdef SUPPORT_ACLS
1421 + /* Cleanup unsent ACL(s). */
1422 + if (preserve_acls)
1423 + send_acl(file, -1);
1428 @@ -1365,6 +1380,11 @@ struct file_list *recv_file_list(int f)
1429 flags |= read_byte(f) << 8;
1430 file = receive_file_entry(flist, flags, f);
1432 +#ifdef SUPPORT_ACLS
1433 + if (preserve_acls)
1434 + receive_acl(file, f);
1437 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1438 stats.total_size += file->length;
1440 @@ -1387,6 +1407,11 @@ struct file_list *recv_file_list(int f)
1442 clean_flist(flist, relative_paths, 1);
1444 +#ifdef SUPPORT_ACLS
1445 + if (preserve_acls)
1446 + sort_file_acl_index_lists();
1450 recv_uid_list(f, flist);
1454 @@ -85,6 +85,7 @@ extern long block_size; /* "long" becaus
1455 extern int max_delete;
1456 extern int force_delete;
1457 extern int one_file_system;
1458 +extern mode_t orig_umask;
1459 extern struct stats stats;
1460 extern dev_t filesystem_dev;
1461 extern char *backup_dir;
1462 @@ -753,6 +754,7 @@ static int try_dests_non(struct file_str
1465 static int phase = 0;
1466 +static int dflt_perms;
1468 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
1469 * make sure it exists, and has the right permissions/timestamp info. For
1470 @@ -844,6 +846,10 @@ static void recv_generator(char *fname,
1473 need_fuzzy_dirlist = 1;
1474 +#ifdef SUPPORT_ACLS
1475 + if (!preserve_perms)
1476 + dflt_perms = default_perms_for_dir(dn);
1479 parent_dirname = dn;
1481 @@ -871,7 +877,8 @@ static void recv_generator(char *fname,
1482 if (!preserve_perms) {
1483 int exists = statret == 0
1484 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1485 - file->mode = dest_mode(file->mode, st.st_mode, exists);
1486 + file->mode = dest_mode(file->mode, st.st_mode, dflt_perms,
1490 if (S_ISDIR(file->mode)) {
1491 @@ -1342,6 +1349,8 @@ void generate_files(int f_out, struct fi
1492 * notice that and let us know via the redo pipe (or its closing). */
1495 + dflt_perms = (ACCESSPERMS & ~orig_umask);
1497 for (i = 0; i < flist->count; i++) {
1498 struct file_struct *file = flist->files[i];
1500 --- old/lib/sysacls.c
1501 +++ new/lib/sysacls.c
1504 + Unix SMB/CIFS implementation.
1505 + Samba system utilities for ACL support.
1506 + Copyright (C) Jeremy Allison 2000.
1508 + This program is free software; you can redistribute it and/or modify
1509 + it under the terms of the GNU General Public License as published by
1510 + the Free Software Foundation; either version 2 of the License, or
1511 + (at your option) any later version.
1513 + This program is distributed in the hope that it will be useful,
1514 + but WITHOUT ANY WARRANTY; without even the implied warranty of
1515 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1516 + GNU General Public License for more details.
1518 + You should have received a copy of the GNU General Public License
1519 + along with this program; if not, write to the Free Software
1520 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1524 +#include "sysacls.h" /****** ADDED ******/
1526 +/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
1527 +void SAFE_FREE(void *mem)
1533 +char *uidtoname(uid_t uid)
1535 + static char idbuf[12];
1536 + struct passwd *pw;
1538 + if ((pw = getpwuid(uid)) == NULL) {
1539 + slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
1542 + return pw->pw_name;
1544 +/****** EXTRAS -- END ******/
1547 + This file wraps all differing system ACL interfaces into a consistent
1548 + one based on the POSIX interface. It also returns the correct errors
1549 + for older UNIX systems that don't support ACLs.
1551 + The interfaces that each ACL implementation must support are as follows :
1553 + int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1554 + int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1555 + int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
1556 + void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1557 + SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1558 + SMB_ACL_T sys_acl_get_fd(int fd)
1559 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
1560 + int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
1561 + char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
1562 + SMB_ACL_T sys_acl_init( int count)
1563 + int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1564 + int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1565 + int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1566 + int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1567 + int sys_acl_valid( SMB_ACL_T theacl )
1568 + int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1569 + int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1570 + int sys_acl_delete_def_file(const char *path)
1572 + This next one is not POSIX complient - but we *have* to have it !
1573 + More POSIX braindamage.
1575 + int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1577 + The generic POSIX free is the following call. We split this into
1578 + several different free functions as we may need to add tag info
1579 + to structures when emulating the POSIX interface.
1581 + int sys_acl_free( void *obj_p)
1583 + The calls we actually use are :
1585 + int sys_acl_free_text(char *text) - free acl_to_text
1586 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
1587 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
1591 +#if defined(HAVE_POSIX_ACLS)
1593 +/* Identity mapping - easy. */
1595 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1597 + return acl_get_entry( the_acl, entry_id, entry_p);
1600 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1602 + return acl_get_tag_type( entry_d, tag_type_p);
1605 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1607 + return acl_get_permset( entry_d, permset_p);
1610 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1612 + return acl_get_qualifier( entry_d);
1615 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1617 + return acl_get_file( path_p, type);
1620 +SMB_ACL_T sys_acl_get_fd(int fd)
1622 + return acl_get_fd(fd);
1625 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1627 + return acl_clear_perms(permset);
1630 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1632 + return acl_add_perm(permset, perm);
1635 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1637 +#if defined(HAVE_ACL_GET_PERM_NP)
1639 + * Required for TrustedBSD-based ACL implementations where
1640 + * non-POSIX.1e functions are denoted by a _np (non-portable)
1643 + return acl_get_perm_np(permset, perm);
1645 + return acl_get_perm(permset, perm);
1649 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1651 + return acl_to_text( the_acl, plen);
1654 +SMB_ACL_T sys_acl_init( int count)
1656 + return acl_init(count);
1659 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1661 + return acl_create_entry(pacl, pentry);
1664 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1666 + return acl_set_tag_type(entry, tagtype);
1669 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1671 + return acl_set_qualifier(entry, qual);
1674 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1676 + return acl_set_permset(entry, permset);
1679 +int sys_acl_valid( SMB_ACL_T theacl )
1681 + return acl_valid(theacl);
1684 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1686 + return acl_set_file(name, acltype, theacl);
1689 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1691 + return acl_set_fd(fd, theacl);
1694 +int sys_acl_delete_def_file(const char *name)
1696 + return acl_delete_def_file(name);
1699 +int sys_acl_free_text(char *text)
1701 + return acl_free(text);
1704 +int sys_acl_free_acl(SMB_ACL_T the_acl)
1706 + return acl_free(the_acl);
1709 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
1711 + return acl_free(qual);
1714 +#elif defined(HAVE_TRU64_ACLS)
1716 + * The interface to DEC/Compaq Tru64 UNIX ACLs
1717 + * is based on Draft 13 of the POSIX spec which is
1718 + * slightly different from the Draft 16 interface.
1720 + * Also, some of the permset manipulation functions
1721 + * such as acl_clear_perm() and acl_add_perm() appear
1722 + * to be broken on Tru64 so we have to manipulate
1723 + * the permission bits in the permset directly.
1725 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1727 + SMB_ACL_ENTRY_T entry;
1729 + if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
1734 + if ((entry = acl_get_entry(the_acl)) != NULL) {
1739 + return errno ? -1 : 0;
1742 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1744 + return acl_get_tag_type( entry_d, tag_type_p);
1747 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1749 + return acl_get_permset( entry_d, permset_p);
1752 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1754 + return acl_get_qualifier( entry_d);
1757 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1759 + return acl_get_file((char *)path_p, type);
1762 +SMB_ACL_T sys_acl_get_fd(int fd)
1764 + return acl_get_fd(fd, ACL_TYPE_ACCESS);
1767 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1769 + *permset = 0; /* acl_clear_perm() is broken on Tru64 */
1774 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1776 + if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
1781 + *permset |= perm; /* acl_add_perm() is broken on Tru64 */
1786 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1788 + return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
1791 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1793 + return acl_to_text( the_acl, plen);
1796 +SMB_ACL_T sys_acl_init( int count)
1798 + return acl_init(count);
1801 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1803 + SMB_ACL_ENTRY_T entry;
1805 + if ((entry = acl_create_entry(pacl)) == NULL) {
1813 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1815 + return acl_set_tag_type(entry, tagtype);
1818 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1820 + return acl_set_qualifier(entry, qual);
1823 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1825 + return acl_set_permset(entry, permset);
1828 +int sys_acl_valid( SMB_ACL_T theacl )
1830 + acl_entry_t entry;
1832 + return acl_valid(theacl, &entry);
1835 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1837 + return acl_set_file((char *)name, acltype, theacl);
1840 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1842 + return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
1845 +int sys_acl_delete_def_file(const char *name)
1847 + return acl_delete_def_file((char *)name);
1850 +int sys_acl_free_text(char *text)
1853 + * (void) cast and explicit return 0 are for DEC UNIX
1854 + * which just #defines acl_free_text() to be free()
1856 + (void) acl_free_text(text);
1860 +int sys_acl_free_acl(SMB_ACL_T the_acl)
1862 + return acl_free(the_acl);
1865 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
1867 + return acl_free_qualifier(qual, tagtype);
1870 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
1873 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
1874 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
1878 + * Note that while this code implements sufficient functionality
1879 + * to support the sys_acl_* interfaces it does not provide all
1880 + * of the semantics of the POSIX ACL interfaces.
1882 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
1883 + * from a call to sys_acl_get_entry() should not be assumed to be
1884 + * valid after calling any of the following functions, which may
1885 + * reorder the entries in the ACL.
1888 + * sys_acl_set_file()
1889 + * sys_acl_set_fd()
1893 + * The only difference between Solaris and UnixWare / OpenUNIX is
1894 + * that the #defines for the ACL operations have different names
1896 +#if defined(HAVE_UNIXWARE_ACLS)
1898 +#define SETACL ACL_SET
1899 +#define GETACL ACL_GET
1900 +#define GETACLCNT ACL_CNT
1905 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1907 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
1912 + if (entry_p == NULL) {
1917 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
1921 + if (acl_d->next < 0) {
1926 + if (acl_d->next >= acl_d->count) {
1930 + *entry_p = &acl_d->acl[acl_d->next++];
1935 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
1937 + *type_p = entry_d->a_type;
1942 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1944 + *permset_p = &entry_d->a_perm;
1949 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
1951 + if (entry_d->a_type != SMB_ACL_USER
1952 + && entry_d->a_type != SMB_ACL_GROUP) {
1957 + return &entry_d->a_id;
1961 + * There is no way of knowing what size the ACL returned by
1962 + * GETACL will be unless you first call GETACLCNT which means
1963 + * making an additional system call.
1965 + * In the hope of avoiding the cost of the additional system
1966 + * call in most cases, we initially allocate enough space for
1967 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
1968 + * be too small then we use GETACLCNT to find out the actual
1969 + * size, reallocate the ACL buffer, and then call GETACL again.
1972 +#define INITIAL_ACL_SIZE 16
1974 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
1977 + int count; /* # of ACL entries allocated */
1978 + int naccess; /* # of access ACL entries */
1979 + int ndefault; /* # of default ACL entries */
1981 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
1986 + count = INITIAL_ACL_SIZE;
1987 + if ((acl_d = sys_acl_init(count)) == NULL) {
1992 + * If there isn't enough space for the ACL entries we use
1993 + * GETACLCNT to determine the actual number of ACL entries
1994 + * reallocate and try again. This is in a loop because it
1995 + * is possible that someone else could modify the ACL and
1996 + * increase the number of entries between the call to
1997 + * GETACLCNT and the call to GETACL.
1999 + while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
2000 + && errno == ENOSPC) {
2002 + sys_acl_free_acl(acl_d);
2004 + if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
2008 + if ((acl_d = sys_acl_init(count)) == NULL) {
2014 + sys_acl_free_acl(acl_d);
2019 + * calculate the number of access and default ACL entries
2021 + * Note: we assume that the acl() system call returned a
2022 + * well formed ACL which is sorted so that all of the
2023 + * access ACL entries preceed any default ACL entries
2025 + for (naccess = 0; naccess < count; naccess++) {
2026 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2029 + ndefault = count - naccess;
2032 + * if the caller wants the default ACL we have to copy
2033 + * the entries down to the start of the acl[] buffer
2034 + * and mask out the ACL_DEFAULT flag from the type field
2036 + if (type == SMB_ACL_TYPE_DEFAULT) {
2039 + for (i = 0, j = naccess; i < ndefault; i++, j++) {
2040 + acl_d->acl[i] = acl_d->acl[j];
2041 + acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2044 + acl_d->count = ndefault;
2046 + acl_d->count = naccess;
2052 +SMB_ACL_T sys_acl_get_fd(int fd)
2055 + int count; /* # of ACL entries allocated */
2056 + int naccess; /* # of access ACL entries */
2058 + count = INITIAL_ACL_SIZE;
2059 + if ((acl_d = sys_acl_init(count)) == NULL) {
2063 + while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2064 + && errno == ENOSPC) {
2066 + sys_acl_free_acl(acl_d);
2068 + if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2072 + if ((acl_d = sys_acl_init(count)) == NULL) {
2078 + sys_acl_free_acl(acl_d);
2083 + * calculate the number of access ACL entries
2085 + for (naccess = 0; naccess < count; naccess++) {
2086 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2090 + acl_d->count = naccess;
2095 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2102 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2104 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2105 + && perm != SMB_ACL_EXECUTE) {
2110 + if (permset_d == NULL) {
2115 + *permset_d |= perm;
2120 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2122 + return *permset_d & perm;
2125 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2132 + * use an initial estimate of 20 bytes per ACL entry
2133 + * when allocating memory for the text representation
2137 + maxlen = 20 * acl_d->count;
2138 + if ((text = SMB_MALLOC(maxlen)) == NULL) {
2143 + for (i = 0; i < acl_d->count; i++) {
2144 + struct acl *ap = &acl_d->acl[i];
2145 + struct passwd *pw;
2154 + switch (ap->a_type) {
2156 + * for debugging purposes it's probably more
2157 + * useful to dump unknown tag types rather
2158 + * than just returning an error
2161 + slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2164 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2169 + case SMB_ACL_USER:
2170 + id = uidtoname(ap->a_id);
2171 + case SMB_ACL_USER_OBJ:
2175 + case SMB_ACL_GROUP:
2176 + if ((gr = getgrgid(ap->a_id)) == NULL) {
2177 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2183 + case SMB_ACL_GROUP_OBJ:
2187 + case SMB_ACL_OTHER:
2191 + case SMB_ACL_MASK:
2197 + perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2198 + perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2199 + perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2202 + /* <tag> : <qualifier> : rwx \n \0 */
2203 + nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2206 + * If this entry would overflow the buffer
2207 + * allocate enough additional memory for this
2208 + * entry and an estimate of another 20 bytes
2209 + * for each entry still to be processed
2211 + if ((len + nbytes) > maxlen) {
2212 + char *oldtext = text;
2214 + maxlen += nbytes + 20 * (acl_d->count - i);
2216 + if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2217 + SAFE_FREE(oldtext);
2223 + slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2224 + len += nbytes - 1;
2233 +SMB_ACL_T sys_acl_init(int count)
2243 + * note that since the definition of the structure pointed
2244 + * to by the SMB_ACL_T includes the first element of the
2245 + * acl[] array, this actually allocates an ACL with room
2246 + * for (count+1) entries
2248 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2253 + a->size = count + 1;
2261 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2264 + SMB_ACL_ENTRY_T entry_d;
2266 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2271 + if (acl_d->count >= acl_d->size) {
2276 + entry_d = &acl_d->acl[acl_d->count++];
2277 + entry_d->a_type = 0;
2278 + entry_d->a_id = -1;
2279 + entry_d->a_perm = 0;
2280 + *entry_p = entry_d;
2285 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2287 + switch (tag_type) {
2288 + case SMB_ACL_USER:
2289 + case SMB_ACL_USER_OBJ:
2290 + case SMB_ACL_GROUP:
2291 + case SMB_ACL_GROUP_OBJ:
2292 + case SMB_ACL_OTHER:
2293 + case SMB_ACL_MASK:
2294 + entry_d->a_type = tag_type;
2304 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2306 + if (entry_d->a_type != SMB_ACL_GROUP
2307 + && entry_d->a_type != SMB_ACL_USER) {
2312 + entry_d->a_id = *((id_t *)qual_p);
2317 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2319 + if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2323 + entry_d->a_perm = *permset_d;
2329 + * sort the ACL and check it for validity
2331 + * if it's a minimal ACL with only 4 entries then we
2332 + * need to recalculate the mask permissions to make
2333 + * sure that they are the same as the GROUP_OBJ
2334 + * permissions as required by the UnixWare acl() system call.
2336 + * (note: since POSIX allows minimal ACLs which only contain
2337 + * 3 entries - ie there is no mask entry - we should, in theory,
2338 + * check for this and add a mask entry if necessary - however
2339 + * we "know" that the caller of this interface always specifies
2340 + * a mask so, in practice "this never happens" (tm) - if it *does*
2341 + * happen aclsort() will fail and return an error and someone will
2342 + * have to fix it ...)
2345 +static int acl_sort(SMB_ACL_T acl_d)
2347 + int fixmask = (acl_d->count <= 4);
2349 + if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
2356 +int sys_acl_valid(SMB_ACL_T acl_d)
2358 + return acl_sort(acl_d);
2361 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
2364 + struct acl *acl_p;
2366 + struct acl *acl_buf = NULL;
2369 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2374 + if (acl_sort(acl_d) != 0) {
2378 + acl_p = &acl_d->acl[0];
2379 + acl_count = acl_d->count;
2382 + * if it's a directory there is extra work to do
2383 + * since the acl() system call will replace both
2384 + * the access ACLs and the default ACLs (if any)
2386 + if (stat(name, &s) != 0) {
2389 + if (S_ISDIR(s.st_mode)) {
2390 + SMB_ACL_T acc_acl;
2391 + SMB_ACL_T def_acl;
2392 + SMB_ACL_T tmp_acl;
2395 + if (type == SMB_ACL_TYPE_ACCESS) {
2397 + def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
2401 + acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
2404 + if (tmp_acl == NULL) {
2409 + * allocate a temporary buffer for the complete ACL
2411 + acl_count = acc_acl->count + def_acl->count;
2412 + acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
2414 + if (acl_buf == NULL) {
2415 + sys_acl_free_acl(tmp_acl);
2421 + * copy the access control and default entries into the buffer
2423 + memcpy(&acl_buf[0], &acc_acl->acl[0],
2424 + acc_acl->count * sizeof(acl_buf[0]));
2426 + memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
2427 + def_acl->count * sizeof(acl_buf[0]));
2430 + * set the ACL_DEFAULT flag on the default entries
2432 + for (i = acc_acl->count; i < acl_count; i++) {
2433 + acl_buf[i].a_type |= ACL_DEFAULT;
2436 + sys_acl_free_acl(tmp_acl);
2438 + } else if (type != SMB_ACL_TYPE_ACCESS) {
2443 + ret = acl(name, SETACL, acl_count, acl_p);
2445 + SAFE_FREE(acl_buf);
2450 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
2452 + if (acl_sort(acl_d) != 0) {
2456 + return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
2459 +int sys_acl_delete_def_file(const char *path)
2465 + * fetching the access ACL and rewriting it has
2466 + * the effect of deleting the default ACL
2468 + if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
2472 + ret = acl(path, SETACL, acl_d->count, acl_d->acl);
2474 + sys_acl_free_acl(acl_d);
2479 +int sys_acl_free_text(char *text)
2485 +int sys_acl_free_acl(SMB_ACL_T acl_d)
2491 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2496 +#elif defined(HAVE_HPUX_ACLS)
2500 + * Based on the Solaris/SCO code - with modifications.
2504 + * Note that while this code implements sufficient functionality
2505 + * to support the sys_acl_* interfaces it does not provide all
2506 + * of the semantics of the POSIX ACL interfaces.
2508 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2509 + * from a call to sys_acl_get_entry() should not be assumed to be
2510 + * valid after calling any of the following functions, which may
2511 + * reorder the entries in the ACL.
2514 + * sys_acl_set_file()
2515 + * sys_acl_set_fd()
2518 +/* This checks if the POSIX ACL system call is defined */
2519 +/* which basically corresponds to whether JFS 3.3 or */
2520 +/* higher is installed. If acl() was called when it */
2521 +/* isn't defined, it causes the process to core dump */
2522 +/* so it is important to check this and avoid acl() */
2523 +/* calls if it isn't there. */
2525 +static BOOL hpux_acl_call_presence(void)
2528 + shl_t handle = NULL;
2531 + static BOOL already_checked=0;
2533 + if(already_checked)
2537 + ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
2539 + if(ret_val != 0) {
2540 + DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
2541 + ret_val, errno, strerror(errno)));
2542 + DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
2546 + DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
2548 + already_checked = True;
2552 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2554 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2559 + if (entry_p == NULL) {
2564 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
2568 + if (acl_d->next < 0) {
2573 + if (acl_d->next >= acl_d->count) {
2577 + *entry_p = &acl_d->acl[acl_d->next++];
2582 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2584 + *type_p = entry_d->a_type;
2589 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2591 + *permset_p = &entry_d->a_perm;
2596 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2598 + if (entry_d->a_type != SMB_ACL_USER
2599 + && entry_d->a_type != SMB_ACL_GROUP) {
2604 + return &entry_d->a_id;
2608 + * There is no way of knowing what size the ACL returned by
2609 + * ACL_GET will be unless you first call ACL_CNT which means
2610 + * making an additional system call.
2612 + * In the hope of avoiding the cost of the additional system
2613 + * call in most cases, we initially allocate enough space for
2614 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2615 + * be too small then we use ACL_CNT to find out the actual
2616 + * size, reallocate the ACL buffer, and then call ACL_GET again.
2619 +#define INITIAL_ACL_SIZE 16
2621 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2624 + int count; /* # of ACL entries allocated */
2625 + int naccess; /* # of access ACL entries */
2626 + int ndefault; /* # of default ACL entries */
2628 + if(hpux_acl_call_presence() == False) {
2629 + /* Looks like we don't have the acl() system call on HPUX.
2630 + * May be the system doesn't have the latest version of JFS.
2635 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2640 + count = INITIAL_ACL_SIZE;
2641 + if ((acl_d = sys_acl_init(count)) == NULL) {
2646 + * If there isn't enough space for the ACL entries we use
2647 + * ACL_CNT to determine the actual number of ACL entries
2648 + * reallocate and try again. This is in a loop because it
2649 + * is possible that someone else could modify the ACL and
2650 + * increase the number of entries between the call to
2651 + * ACL_CNT and the call to ACL_GET.
2653 + while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
2655 + sys_acl_free_acl(acl_d);
2657 + if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
2661 + if ((acl_d = sys_acl_init(count)) == NULL) {
2667 + sys_acl_free_acl(acl_d);
2672 + * calculate the number of access and default ACL entries
2674 + * Note: we assume that the acl() system call returned a
2675 + * well formed ACL which is sorted so that all of the
2676 + * access ACL entries preceed any default ACL entries
2678 + for (naccess = 0; naccess < count; naccess++) {
2679 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2682 + ndefault = count - naccess;
2685 + * if the caller wants the default ACL we have to copy
2686 + * the entries down to the start of the acl[] buffer
2687 + * and mask out the ACL_DEFAULT flag from the type field
2689 + if (type == SMB_ACL_TYPE_DEFAULT) {
2692 + for (i = 0, j = naccess; i < ndefault; i++, j++) {
2693 + acl_d->acl[i] = acl_d->acl[j];
2694 + acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2697 + acl_d->count = ndefault;
2699 + acl_d->count = naccess;
2705 +SMB_ACL_T sys_acl_get_fd(int fd)
2708 + * HPUX doesn't have the facl call. Fake it using the path.... JRA.
2711 + files_struct *fsp = file_find_fd(fd);
2713 + if (fsp == NULL) {
2719 + * We know we're in the same conn context. So we
2720 + * can use the relative path.
2723 + return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
2726 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2733 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2735 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2736 + && perm != SMB_ACL_EXECUTE) {
2741 + if (permset_d == NULL) {
2746 + *permset_d |= perm;
2751 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2753 + return *permset_d & perm;
2756 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2763 + * use an initial estimate of 20 bytes per ACL entry
2764 + * when allocating memory for the text representation
2768 + maxlen = 20 * acl_d->count;
2769 + if ((text = SMB_MALLOC(maxlen)) == NULL) {
2774 + for (i = 0; i < acl_d->count; i++) {
2775 + struct acl *ap = &acl_d->acl[i];
2776 + struct passwd *pw;
2785 + switch (ap->a_type) {
2787 + * for debugging purposes it's probably more
2788 + * useful to dump unknown tag types rather
2789 + * than just returning an error
2792 + slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2795 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2800 + case SMB_ACL_USER:
2801 + id = uidtoname(ap->a_id);
2802 + case SMB_ACL_USER_OBJ:
2806 + case SMB_ACL_GROUP:
2807 + if ((gr = getgrgid(ap->a_id)) == NULL) {
2808 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2814 + case SMB_ACL_GROUP_OBJ:
2818 + case SMB_ACL_OTHER:
2822 + case SMB_ACL_MASK:
2828 + perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2829 + perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2830 + perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2833 + /* <tag> : <qualifier> : rwx \n \0 */
2834 + nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2837 + * If this entry would overflow the buffer
2838 + * allocate enough additional memory for this
2839 + * entry and an estimate of another 20 bytes
2840 + * for each entry still to be processed
2842 + if ((len + nbytes) > maxlen) {
2843 + char *oldtext = text;
2845 + maxlen += nbytes + 20 * (acl_d->count - i);
2847 + if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2854 + slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2855 + len += nbytes - 1;
2864 +SMB_ACL_T sys_acl_init(int count)
2874 + * note that since the definition of the structure pointed
2875 + * to by the SMB_ACL_T includes the first element of the
2876 + * acl[] array, this actually allocates an ACL with room
2877 + * for (count+1) entries
2879 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2884 + a->size = count + 1;
2892 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2895 + SMB_ACL_ENTRY_T entry_d;
2897 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2902 + if (acl_d->count >= acl_d->size) {
2907 + entry_d = &acl_d->acl[acl_d->count++];
2908 + entry_d->a_type = 0;
2909 + entry_d->a_id = -1;
2910 + entry_d->a_perm = 0;
2911 + *entry_p = entry_d;
2916 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2918 + switch (tag_type) {
2919 + case SMB_ACL_USER:
2920 + case SMB_ACL_USER_OBJ:
2921 + case SMB_ACL_GROUP:
2922 + case SMB_ACL_GROUP_OBJ:
2923 + case SMB_ACL_OTHER:
2924 + case SMB_ACL_MASK:
2925 + entry_d->a_type = tag_type;
2935 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2937 + if (entry_d->a_type != SMB_ACL_GROUP
2938 + && entry_d->a_type != SMB_ACL_USER) {
2943 + entry_d->a_id = *((id_t *)qual_p);
2948 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2950 + if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2954 + entry_d->a_perm = *permset_d;
2959 +/* Structure to capture the count for each type of ACE. */
2961 +struct hpux_acl_types {
2965 + int n_def_user_obj;
2970 + int n_def_group_obj;
2974 + int n_def_other_obj;
2977 + int n_def_class_obj;
2979 + int n_illegal_obj;
2983 + * Counts the different number of objects in a given array of ACL
2987 + * acl_count - Count of ACLs in the array of ACL strucutres.
2988 + * aclp - Array of ACL structures.
2989 + * acl_type_count - Pointer to acl_types structure. Should already be
2993 + * acl_type_count - This structure is filled up with counts of various
2997 +static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
3001 + memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
3003 + for(i=0;i<acl_count;i++) {
3004 + switch(aclp[i].a_type) {
3006 + acl_type_count->n_user++;
3009 + acl_type_count->n_user_obj++;
3011 + case DEF_USER_OBJ:
3012 + acl_type_count->n_def_user_obj++;
3015 + acl_type_count->n_group++;
3018 + acl_type_count->n_group_obj++;
3020 + case DEF_GROUP_OBJ:
3021 + acl_type_count->n_def_group_obj++;
3024 + acl_type_count->n_other_obj++;
3026 + case DEF_OTHER_OBJ:
3027 + acl_type_count->n_def_other_obj++;
3030 + acl_type_count->n_class_obj++;
3032 + case DEF_CLASS_OBJ:
3033 + acl_type_count->n_def_class_obj++;
3036 + acl_type_count->n_def_user++;
3039 + acl_type_count->n_def_group++;
3042 + acl_type_count->n_illegal_obj++;
3048 +/* swap_acl_entries: Swaps two ACL entries.
3050 + * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3053 +static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3055 + struct acl temp_acl;
3057 + temp_acl.a_type = aclp0->a_type;
3058 + temp_acl.a_id = aclp0->a_id;
3059 + temp_acl.a_perm = aclp0->a_perm;
3061 + aclp0->a_type = aclp1->a_type;
3062 + aclp0->a_id = aclp1->a_id;
3063 + aclp0->a_perm = aclp1->a_perm;
3065 + aclp1->a_type = temp_acl.a_type;
3066 + aclp1->a_id = temp_acl.a_id;
3067 + aclp1->a_perm = temp_acl.a_perm;
3070 +/* prohibited_duplicate_type
3071 + * Identifies if given ACL type can have duplicate entries or
3074 + * Inputs: acl_type - ACL Type.
3080 + * True - If the ACL type matches any of the prohibited types.
3081 + * False - If the ACL type doesn't match any of the prohibited types.
3084 +static BOOL hpux_prohibited_duplicate_type(int acl_type)
3086 + switch(acl_type) {
3097 +/* get_needed_class_perm
3098 + * Returns the permissions of a ACL structure only if the ACL
3099 + * type matches one of the pre-determined types for computing
3100 + * CLASS_OBJ permissions.
3102 + * Inputs: aclp - Pointer to ACL structure.
3105 +static int hpux_get_needed_class_perm(struct acl *aclp)
3107 + switch(aclp->a_type) {
3111 + case DEF_USER_OBJ:
3113 + case DEF_GROUP_OBJ:
3115 + case DEF_CLASS_OBJ:
3116 + case DEF_OTHER_OBJ:
3117 + return aclp->a_perm;
3123 +/* acl_sort for HPUX.
3124 + * Sorts the array of ACL structures as per the description in
3125 + * aclsort man page. Refer to aclsort man page for more details
3129 + * acl_count - Count of ACLs in the array of ACL structures.
3130 + * calclass - If this is not zero, then we compute the CLASS_OBJ
3132 + * aclp - Array of ACL structures.
3136 + * aclp - Sorted array of ACL structures.
3140 + * Returns 0 for success -1 for failure. Prints a message to the Samba
3141 + * debug log in case of failure.
3144 +static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3146 +#if !defined(HAVE_HPUX_ACLSORT)
3148 + * The aclsort() system call is availabe on the latest HPUX General
3149 + * Patch Bundles. So for HPUX, we developed our version of acl_sort
3150 + * function. Because, we don't want to update to a new
3151 + * HPUX GR bundle just for aclsort() call.
3154 + struct hpux_acl_types acl_obj_count;
3155 + int n_class_obj_perm = 0;
3159 + DEBUG(10,("Zero acl count passed. Returning Success\n"));
3163 + if(aclp == NULL) {
3164 + DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3168 + /* Count different types of ACLs in the ACLs array */
3170 + hpux_count_obj(acl_count, aclp, &acl_obj_count);
3172 + /* There should be only one entry each of type USER_OBJ, GROUP_OBJ,
3173 + * CLASS_OBJ and OTHER_OBJ
3176 + if( (acl_obj_count.n_user_obj != 1) ||
3177 + (acl_obj_count.n_group_obj != 1) ||
3178 + (acl_obj_count.n_class_obj != 1) ||
3179 + (acl_obj_count.n_other_obj != 1)
3181 + DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3182 +USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3186 + /* If any of the default objects are present, there should be only
3187 + * one of them each.
3190 + if( (acl_obj_count.n_def_user_obj > 1) || (acl_obj_count.n_def_group_obj > 1) ||
3191 + (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
3192 + DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3193 +or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3197 + /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl
3200 + * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3201 + * same ACL type, sort by ACL id.
3203 + * I am using the trival kind of sorting method here because, performance isn't
3204 + * really effected by the ACLs feature. More over there aren't going to be more
3205 + * than 17 entries on HPUX.
3208 + for(i=0; i<acl_count;i++) {
3209 + for (j=i+1; j<acl_count; j++) {
3210 + if( aclp[i].a_type > aclp[j].a_type ) {
3211 + /* ACL entries out of order, swap them */
3213 + hpux_swap_acl_entries((aclp+i), (aclp+j));
3215 + } else if ( aclp[i].a_type == aclp[j].a_type ) {
3217 + /* ACL entries of same type, sort by id */
3219 + if(aclp[i].a_id > aclp[j].a_id) {
3220 + hpux_swap_acl_entries((aclp+i), (aclp+j));
3221 + } else if (aclp[i].a_id == aclp[j].a_id) {
3222 + /* We have a duplicate entry. */
3223 + if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
3224 + DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3225 + aclp[i].a_type, aclp[i].a_id));
3234 + /* set the class obj permissions to the computed one. */
3236 + int n_class_obj_index = -1;
3238 + for(i=0;i<acl_count;i++) {
3239 + n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3241 + if(aclp[i].a_type == CLASS_OBJ)
3242 + n_class_obj_index = i;
3244 + aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3249 + return aclsort(acl_count, calclass, aclp);
3254 + * sort the ACL and check it for validity
3256 + * if it's a minimal ACL with only 4 entries then we
3257 + * need to recalculate the mask permissions to make
3258 + * sure that they are the same as the GROUP_OBJ
3259 + * permissions as required by the UnixWare acl() system call.
3261 + * (note: since POSIX allows minimal ACLs which only contain
3262 + * 3 entries - ie there is no mask entry - we should, in theory,
3263 + * check for this and add a mask entry if necessary - however
3264 + * we "know" that the caller of this interface always specifies
3265 + * a mask so, in practice "this never happens" (tm) - if it *does*
3266 + * happen aclsort() will fail and return an error and someone will
3267 + * have to fix it ...)
3270 +static int acl_sort(SMB_ACL_T acl_d)
3272 + int fixmask = (acl_d->count <= 4);
3274 + if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
3281 +int sys_acl_valid(SMB_ACL_T acl_d)
3283 + return acl_sort(acl_d);
3286 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3289 + struct acl *acl_p;
3291 + struct acl *acl_buf = NULL;
3294 + if(hpux_acl_call_presence() == False) {
3295 + /* Looks like we don't have the acl() system call on HPUX.
3296 + * May be the system doesn't have the latest version of JFS.
3302 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3307 + if (acl_sort(acl_d) != 0) {
3311 + acl_p = &acl_d->acl[0];
3312 + acl_count = acl_d->count;
3315 + * if it's a directory there is extra work to do
3316 + * since the acl() system call will replace both
3317 + * the access ACLs and the default ACLs (if any)
3319 + if (stat(name, &s) != 0) {
3322 + if (S_ISDIR(s.st_mode)) {
3323 + SMB_ACL_T acc_acl;
3324 + SMB_ACL_T def_acl;
3325 + SMB_ACL_T tmp_acl;
3328 + if (type == SMB_ACL_TYPE_ACCESS) {
3330 + def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3334 + acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3337 + if (tmp_acl == NULL) {
3342 + * allocate a temporary buffer for the complete ACL
3344 + acl_count = acc_acl->count + def_acl->count;
3345 + acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
3347 + if (acl_buf == NULL) {
3348 + sys_acl_free_acl(tmp_acl);
3354 + * copy the access control and default entries into the buffer
3356 + memcpy(&acl_buf[0], &acc_acl->acl[0],
3357 + acc_acl->count * sizeof(acl_buf[0]));
3359 + memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3360 + def_acl->count * sizeof(acl_buf[0]));
3363 + * set the ACL_DEFAULT flag on the default entries
3365 + for (i = acc_acl->count; i < acl_count; i++) {
3366 + acl_buf[i].a_type |= ACL_DEFAULT;
3369 + sys_acl_free_acl(tmp_acl);
3371 + } else if (type != SMB_ACL_TYPE_ACCESS) {
3376 + ret = acl(name, ACL_SET, acl_count, acl_p);
3385 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3388 + * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3391 + files_struct *fsp = file_find_fd(fd);
3393 + if (fsp == NULL) {
3398 + if (acl_sort(acl_d) != 0) {
3403 + * We know we're in the same conn context. So we
3404 + * can use the relative path.
3407 + return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
3410 +int sys_acl_delete_def_file(const char *path)
3416 + * fetching the access ACL and rewriting it has
3417 + * the effect of deleting the default ACL
3419 + if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3423 + ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
3425 + sys_acl_free_acl(acl_d);
3430 +int sys_acl_free_text(char *text)
3436 +int sys_acl_free_acl(SMB_ACL_T acl_d)
3442 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3447 +#elif defined(HAVE_IRIX_ACLS)
3449 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3451 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3456 + if (entry_p == NULL) {
3461 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
3465 + if (acl_d->next < 0) {
3470 + if (acl_d->next >= acl_d->aclp->acl_cnt) {
3474 + *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
3479 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
3481 + *type_p = entry_d->ae_tag;
3486 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3488 + *permset_p = entry_d;
3493 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3495 + if (entry_d->ae_tag != SMB_ACL_USER
3496 + && entry_d->ae_tag != SMB_ACL_GROUP) {
3501 + return &entry_d->ae_id;
3504 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3508 + if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3512 + if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
3517 + a->freeaclp = True;
3521 +SMB_ACL_T sys_acl_get_fd(int fd)
3525 + if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3529 + if ((a->aclp = acl_get_fd(fd)) == NULL) {
3534 + a->freeaclp = True;
3538 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3540 + permset_d->ae_perm = 0;
3545 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3547 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3548 + && perm != SMB_ACL_EXECUTE) {
3553 + if (permset_d == NULL) {
3558 + permset_d->ae_perm |= perm;
3563 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3565 + return permset_d->ae_perm & perm;
3568 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3570 + return acl_to_text(acl_d->aclp, len_p);
3573 +SMB_ACL_T sys_acl_init(int count)
3582 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
3588 + a->freeaclp = False;
3589 + a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
3590 + a->aclp->acl_cnt = 0;
3596 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3599 + SMB_ACL_ENTRY_T entry_d;
3601 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3606 + if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
3611 + entry_d = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
3612 + entry_d->ae_tag = 0;
3613 + entry_d->ae_id = 0;
3614 + entry_d->ae_perm = 0;
3615 + *entry_p = entry_d;
3620 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3622 + switch (tag_type) {
3623 + case SMB_ACL_USER:
3624 + case SMB_ACL_USER_OBJ:
3625 + case SMB_ACL_GROUP:
3626 + case SMB_ACL_GROUP_OBJ:
3627 + case SMB_ACL_OTHER:
3628 + case SMB_ACL_MASK:
3629 + entry_d->ae_tag = tag_type;
3639 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3641 + if (entry_d->ae_tag != SMB_ACL_GROUP
3642 + && entry_d->ae_tag != SMB_ACL_USER) {
3647 + entry_d->ae_id = *((id_t *)qual_p);
3652 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3654 + if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3658 + entry_d->ae_perm = permset_d->ae_perm;
3663 +int sys_acl_valid(SMB_ACL_T acl_d)
3665 + return acl_valid(acl_d->aclp);
3668 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3670 + return acl_set_file(name, type, acl_d->aclp);
3673 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3675 + return acl_set_fd(fd, acl_d->aclp);
3678 +int sys_acl_delete_def_file(const char *name)
3680 + return acl_delete_def_file(name);
3683 +int sys_acl_free_text(char *text)
3685 + return acl_free(text);
3688 +int sys_acl_free_acl(SMB_ACL_T acl_d)
3690 + if (acl_d->freeaclp) {
3691 + acl_free(acl_d->aclp);
3697 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3702 +#elif defined(HAVE_AIX_ACLS)
3704 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
3706 +int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3708 + struct acl_entry_link *link;
3709 + struct new_acl_entry *entry;
3712 + DEBUG(10,("This is the count: %d\n",theacl->count));
3714 + /* Check if count was previously set to -1. *
3715 + * If it was, that means we reached the end *
3716 + * of the acl last time. */
3717 + if(theacl->count == -1)
3721 + /* To get to the next acl, traverse linked list until index *
3722 + * of acl matches the count we are keeping. This count is *
3723 + * incremented each time we return an acl entry. */
3725 + for(keep_going = 0; keep_going < theacl->count; keep_going++)
3726 + link = link->nextp;
3728 + entry = *entry_p = link->entryp;
3730 + DEBUG(10,("*entry_p is %d\n",entry_p));
3731 + DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
3733 + /* Increment count */
3735 + if(link->nextp == NULL)
3736 + theacl->count = -1;
3741 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
3743 + /* Initialize tag type */
3746 + DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
3748 + /* Depending on what type of entry we have, *
3749 + * return tag type. */
3750 + switch(entry_d->ace_id->id_type) {
3752 + *tag_type_p = SMB_ACL_USER;
3755 + *tag_type_p = SMB_ACL_GROUP;
3758 + case SMB_ACL_USER_OBJ:
3759 + case SMB_ACL_GROUP_OBJ:
3760 + case SMB_ACL_OTHER:
3761 + *tag_type_p = entry_d->ace_id->id_type;
3771 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3773 + DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
3774 + *permset_p = &entry_d->ace_access;
3775 + DEBUG(10,("**permset_p is %d\n",**permset_p));
3776 + if(!(**permset_p & S_IXUSR) &&
3777 + !(**permset_p & S_IWUSR) &&
3778 + !(**permset_p & S_IRUSR) &&
3779 + (**permset_p != 0))
3782 + DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
3786 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
3788 + return(entry_d->ace_id->id_data);
3791 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
3793 + struct acl *file_acl = (struct acl *)NULL;
3794 + struct acl_entry *acl_entry;
3795 + struct new_acl_entry *new_acl_entry;
3796 + struct ace_id *idp;
3797 + struct acl_entry_link *acl_entry_link;
3798 + struct acl_entry_link *acl_entry_link_head;
3803 + /* AIX has no DEFAULT */
3804 + if ( type == SMB_ACL_TYPE_DEFAULT )
3807 + /* Get the acl using statacl */
3809 + DEBUG(10,("Entering sys_acl_get_file\n"));
3810 + DEBUG(10,("path_p is %s\n",path_p));
3812 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
3814 + if(file_acl == NULL) {
3816 + DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
3820 + memset(file_acl,0,BUFSIZ);
3822 + rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
3824 + DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
3825 + SAFE_FREE(file_acl);
3829 + DEBUG(10,("Got facl and returned it\n"));
3831 + /* Point to the first acl entry in the acl */
3832 + acl_entry = file_acl->acl_ext;
3834 + /* Begin setting up the head of the linked list *
3835 + * that will be used for the storing the acl *
3836 + * in a way that is useful for the posix_acls.c *
3839 + acl_entry_link_head = acl_entry_link = sys_acl_init(0);
3840 + if(acl_entry_link_head == NULL)
3843 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3844 + if(acl_entry_link->entryp == NULL) {
3845 + SAFE_FREE(file_acl);
3847 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3851 + DEBUG(10,("acl_entry is %d\n",acl_entry));
3852 + DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
3854 + /* Check if the extended acl bit is on. *
3855 + * If it isn't, do not show the *
3856 + * contents of the acl since AIX intends *
3857 + * the extended info to remain unused */
3859 + if(file_acl->acl_mode & S_IXACL){
3860 + /* while we are not pointing to the very end */
3861 + while(acl_entry < acl_last(file_acl)) {
3862 + /* before we malloc anything, make sure this is */
3863 + /* a valid acl entry and one that we want to map */
3864 + idp = id_nxt(acl_entry->ace_id);
3865 + if((acl_entry->ace_type == ACC_SPECIFY ||
3866 + (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
3867 + acl_entry = acl_nxt(acl_entry);
3871 + idp = acl_entry->ace_id;
3873 + /* Check if this is the first entry in the linked list. *
3874 + * The first entry needs to keep prevp pointing to NULL *
3875 + * and already has entryp allocated. */
3877 + if(acl_entry_link_head->count != 0) {
3878 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
3880 + if(acl_entry_link->nextp == NULL) {
3881 + SAFE_FREE(file_acl);
3883 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3887 + acl_entry_link->nextp->prevp = acl_entry_link;
3888 + acl_entry_link = acl_entry_link->nextp;
3889 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3890 + if(acl_entry_link->entryp == NULL) {
3891 + SAFE_FREE(file_acl);
3893 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3896 + acl_entry_link->nextp = NULL;
3899 + acl_entry_link->entryp->ace_len = acl_entry->ace_len;
3901 + /* Don't really need this since all types are going *
3902 + * to be specified but, it's better than leaving it 0 */
3904 + acl_entry_link->entryp->ace_type = acl_entry->ace_type;
3906 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3908 + memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
3910 + /* The access in the acl entries must be left shifted by *
3911 + * three bites, because they will ultimately be compared *
3912 + * to S_IRUSR, S_IWUSR, and S_IXUSR. */
3914 + switch(acl_entry->ace_type){
3917 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3918 + acl_entry_link->entryp->ace_access <<= 6;
3919 + acl_entry_link_head->count++;
3922 + /* Since there is no way to return a DENY acl entry *
3923 + * change to PERMIT and then shift. */
3924 + DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
3925 + acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
3926 + DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
3927 + acl_entry_link->entryp->ace_access <<= 6;
3928 + acl_entry_link_head->count++;
3934 + DEBUG(10,("acl_entry = %d\n",acl_entry));
3935 + DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
3937 + acl_entry = acl_nxt(acl_entry);
3939 + } /* end of if enabled */
3941 + /* Since owner, group, other acl entries are not *
3942 + * part of the acl entries in an acl, they must *
3943 + * be dummied up to become part of the list. */
3945 + for( i = 1; i < 4; i++) {
3946 + DEBUG(10,("i is %d\n",i));
3947 + if(acl_entry_link_head->count != 0) {
3948 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
3949 + if(acl_entry_link->nextp == NULL) {
3950 + SAFE_FREE(file_acl);
3952 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3956 + acl_entry_link->nextp->prevp = acl_entry_link;
3957 + acl_entry_link = acl_entry_link->nextp;
3958 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3959 + if(acl_entry_link->entryp == NULL) {
3960 + SAFE_FREE(file_acl);
3962 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3967 + acl_entry_link->nextp = NULL;
3969 + new_acl_entry = acl_entry_link->entryp;
3970 + idp = new_acl_entry->ace_id;
3972 + new_acl_entry->ace_len = sizeof(struct acl_entry);
3973 + new_acl_entry->ace_type = ACC_PERMIT;
3974 + idp->id_len = sizeof(struct ace_id);
3975 + DEBUG(10,("idp->id_len = %d\n",idp->id_len));
3976 + memset(idp->id_data,0,sizeof(uid_t));
3980 + new_acl_entry->ace_access = file_acl->g_access << 6;
3981 + idp->id_type = SMB_ACL_GROUP_OBJ;
3985 + new_acl_entry->ace_access = file_acl->o_access << 6;
3986 + idp->id_type = SMB_ACL_OTHER;
3990 + new_acl_entry->ace_access = file_acl->u_access << 6;
3991 + idp->id_type = SMB_ACL_USER_OBJ;
3999 + acl_entry_link_head->count++;
4000 + DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4003 + acl_entry_link_head->count = 0;
4004 + SAFE_FREE(file_acl);
4006 + return(acl_entry_link_head);
4009 +SMB_ACL_T sys_acl_get_fd(int fd)
4011 + struct acl *file_acl = (struct acl *)NULL;
4012 + struct acl_entry *acl_entry;
4013 + struct new_acl_entry *new_acl_entry;
4014 + struct ace_id *idp;
4015 + struct acl_entry_link *acl_entry_link;
4016 + struct acl_entry_link *acl_entry_link_head;
4021 + /* Get the acl using fstatacl */
4023 + DEBUG(10,("Entering sys_acl_get_fd\n"));
4024 + DEBUG(10,("fd is %d\n",fd));
4025 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4027 + if(file_acl == NULL) {
4029 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4033 + memset(file_acl,0,BUFSIZ);
4035 + rc = fstatacl(fd,0,file_acl,BUFSIZ);
4037 + DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
4038 + SAFE_FREE(file_acl);
4042 + DEBUG(10,("Got facl and returned it\n"));
4044 + /* Point to the first acl entry in the acl */
4046 + acl_entry = file_acl->acl_ext;
4047 + /* Begin setting up the head of the linked list *
4048 + * that will be used for the storing the acl *
4049 + * in a way that is useful for the posix_acls.c *
4052 + acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4053 + if(acl_entry_link_head == NULL){
4054 + SAFE_FREE(file_acl);
4058 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4060 + if(acl_entry_link->entryp == NULL) {
4062 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4063 + SAFE_FREE(file_acl);
4067 + DEBUG(10,("acl_entry is %d\n",acl_entry));
4068 + DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4070 + /* Check if the extended acl bit is on. *
4071 + * If it isn't, do not show the *
4072 + * contents of the acl since AIX intends *
4073 + * the extended info to remain unused */
4075 + if(file_acl->acl_mode & S_IXACL){
4076 + /* while we are not pointing to the very end */
4077 + while(acl_entry < acl_last(file_acl)) {
4078 + /* before we malloc anything, make sure this is */
4079 + /* a valid acl entry and one that we want to map */
4081 + idp = id_nxt(acl_entry->ace_id);
4082 + if((acl_entry->ace_type == ACC_SPECIFY ||
4083 + (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4084 + acl_entry = acl_nxt(acl_entry);
4088 + idp = acl_entry->ace_id;
4090 + /* Check if this is the first entry in the linked list. *
4091 + * The first entry needs to keep prevp pointing to NULL *
4092 + * and already has entryp allocated. */
4094 + if(acl_entry_link_head->count != 0) {
4095 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4096 + if(acl_entry_link->nextp == NULL) {
4098 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4099 + SAFE_FREE(file_acl);
4102 + acl_entry_link->nextp->prevp = acl_entry_link;
4103 + acl_entry_link = acl_entry_link->nextp;
4104 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4105 + if(acl_entry_link->entryp == NULL) {
4107 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4108 + SAFE_FREE(file_acl);
4112 + acl_entry_link->nextp = NULL;
4115 + acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4117 + /* Don't really need this since all types are going *
4118 + * to be specified but, it's better than leaving it 0 */
4120 + acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4121 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4123 + memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
4125 + /* The access in the acl entries must be left shifted by *
4126 + * three bites, because they will ultimately be compared *
4127 + * to S_IRUSR, S_IWUSR, and S_IXUSR. */
4129 + switch(acl_entry->ace_type){
4132 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4133 + acl_entry_link->entryp->ace_access <<= 6;
4134 + acl_entry_link_head->count++;
4137 + /* Since there is no way to return a DENY acl entry *
4138 + * change to PERMIT and then shift. */
4139 + DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4140 + acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4141 + DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4142 + acl_entry_link->entryp->ace_access <<= 6;
4143 + acl_entry_link_head->count++;
4149 + DEBUG(10,("acl_entry = %d\n",acl_entry));
4150 + DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4152 + acl_entry = acl_nxt(acl_entry);
4154 + } /* end of if enabled */
4156 + /* Since owner, group, other acl entries are not *
4157 + * part of the acl entries in an acl, they must *
4158 + * be dummied up to become part of the list. */
4160 + for( i = 1; i < 4; i++) {
4161 + DEBUG(10,("i is %d\n",i));
4162 + if(acl_entry_link_head->count != 0){
4163 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4164 + if(acl_entry_link->nextp == NULL) {
4166 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4167 + SAFE_FREE(file_acl);
4171 + acl_entry_link->nextp->prevp = acl_entry_link;
4172 + acl_entry_link = acl_entry_link->nextp;
4173 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4175 + if(acl_entry_link->entryp == NULL) {
4176 + SAFE_FREE(file_acl);
4178 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4183 + acl_entry_link->nextp = NULL;
4185 + new_acl_entry = acl_entry_link->entryp;
4186 + idp = new_acl_entry->ace_id;
4188 + new_acl_entry->ace_len = sizeof(struct acl_entry);
4189 + new_acl_entry->ace_type = ACC_PERMIT;
4190 + idp->id_len = sizeof(struct ace_id);
4191 + DEBUG(10,("idp->id_len = %d\n",idp->id_len));
4192 + memset(idp->id_data,0,sizeof(uid_t));
4196 + new_acl_entry->ace_access = file_acl->g_access << 6;
4197 + idp->id_type = SMB_ACL_GROUP_OBJ;
4201 + new_acl_entry->ace_access = file_acl->o_access << 6;
4202 + idp->id_type = SMB_ACL_OTHER;
4206 + new_acl_entry->ace_access = file_acl->u_access << 6;
4207 + idp->id_type = SMB_ACL_USER_OBJ;
4214 + acl_entry_link_head->count++;
4215 + DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4218 + acl_entry_link_head->count = 0;
4219 + SAFE_FREE(file_acl);
4221 + return(acl_entry_link_head);
4224 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
4226 + *permset = *permset & ~0777;
4230 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4233 + (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
4237 + DEBUG(10,("This is the permset now: %d\n",*permset));
4241 +char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
4246 +SMB_ACL_T sys_acl_init( int count)
4248 + struct acl_entry_link *theacl = NULL;
4250 + DEBUG(10,("Entering sys_acl_init\n"));
4252 + theacl = SMB_MALLOC_P(struct acl_entry_link);
4253 + if(theacl == NULL) {
4255 + DEBUG(0,("Error in sys_acl_init is %d\n",errno));
4259 + theacl->count = 0;
4260 + theacl->nextp = NULL;
4261 + theacl->prevp = NULL;
4262 + theacl->entryp = NULL;
4263 + DEBUG(10,("Exiting sys_acl_init\n"));
4267 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
4269 + struct acl_entry_link *theacl;
4270 + struct acl_entry_link *acl_entryp;
4271 + struct acl_entry_link *temp_entry;
4274 + DEBUG(10,("Entering the sys_acl_create_entry\n"));
4276 + theacl = acl_entryp = *pacl;
4278 + /* Get to the end of the acl before adding entry */
4280 + for(counting=0; counting < theacl->count; counting++){
4281 + DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4282 + temp_entry = acl_entryp;
4283 + acl_entryp = acl_entryp->nextp;
4286 + if(theacl->count != 0){
4287 + temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
4288 + if(acl_entryp == NULL) {
4290 + DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4294 + DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4295 + acl_entryp->prevp = temp_entry;
4296 + DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
4299 + *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
4300 + if(*pentry == NULL) {
4302 + DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4306 + memset(*pentry,0,sizeof(struct new_acl_entry));
4307 + acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
4308 + acl_entryp->entryp->ace_type = ACC_PERMIT;
4309 + acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
4310 + acl_entryp->nextp = NULL;
4312 + DEBUG(10,("Exiting sys_acl_create_entry\n"));
4316 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
4318 + DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
4319 + entry->ace_id->id_type = tagtype;
4320 + DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
4321 + DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
4324 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
4326 + DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
4327 + memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
4328 + DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
4332 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
4334 + DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
4335 + if(!(*permset & S_IXUSR) &&
4336 + !(*permset & S_IWUSR) &&
4337 + !(*permset & S_IRUSR) &&
4341 + entry->ace_access = *permset;
4342 + DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
4343 + DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
4347 +int sys_acl_valid( SMB_ACL_T theacl )
4350 + int group_obj = 0;
4351 + int other_obj = 0;
4352 + struct acl_entry_link *acl_entry;
4354 + for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
4355 + user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
4356 + group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
4357 + other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
4360 + DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
4362 + if(user_obj != 1 || group_obj != 1 || other_obj != 1)
4368 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
4370 + struct acl_entry_link *acl_entry_link = NULL;
4371 + struct acl *file_acl = NULL;
4372 + struct acl *file_acl_temp = NULL;
4373 + struct acl_entry *acl_entry = NULL;
4374 + struct ace_id *ace_id = NULL;
4381 + DEBUG(10,("Entering sys_acl_set_file\n"));
4382 + DEBUG(10,("File name is %s\n",name));
4384 + /* AIX has no default ACL */
4385 + if(acltype == SMB_ACL_TYPE_DEFAULT)
4388 + acl_length = BUFSIZ;
4389 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4391 + if(file_acl == NULL) {
4393 + DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4397 + memset(file_acl,0,BUFSIZ);
4399 + file_acl->acl_len = ACL_SIZ;
4400 + file_acl->acl_mode = S_IXACL;
4402 + for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4403 + acl_entry_link->entryp->ace_access >>= 6;
4404 + id_type = acl_entry_link->entryp->ace_id->id_type;
4407 + case SMB_ACL_USER_OBJ:
4408 + file_acl->u_access = acl_entry_link->entryp->ace_access;
4410 + case SMB_ACL_GROUP_OBJ:
4411 + file_acl->g_access = acl_entry_link->entryp->ace_access;
4413 + case SMB_ACL_OTHER:
4414 + file_acl->o_access = acl_entry_link->entryp->ace_access;
4416 + case SMB_ACL_MASK:
4420 + if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4421 + acl_length += sizeof(struct acl_entry);
4422 + file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
4423 + if(file_acl_temp == NULL) {
4424 + SAFE_FREE(file_acl);
4426 + DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4430 + memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4431 + SAFE_FREE(file_acl);
4432 + file_acl = file_acl_temp;
4435 + acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4436 + file_acl->acl_len += sizeof(struct acl_entry);
4437 + acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4438 + acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4440 + /* In order to use this, we'll need to wait until we can get denies */
4441 + /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4442 + acl_entry->ace_type = ACC_SPECIFY; */
4444 + acl_entry->ace_type = ACC_SPECIFY;
4446 + ace_id = acl_entry->ace_id;
4448 + ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4449 + DEBUG(10,("The id type is %d\n",ace_id->id_type));
4450 + ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4451 + memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4452 + memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
4455 + rc = chacl(name,file_acl,file_acl->acl_len);
4456 + DEBUG(10,("errno is %d\n",errno));
4457 + DEBUG(10,("return code is %d\n",rc));
4458 + SAFE_FREE(file_acl);
4459 + DEBUG(10,("Exiting the sys_acl_set_file\n"));
4463 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
4465 + struct acl_entry_link *acl_entry_link = NULL;
4466 + struct acl *file_acl = NULL;
4467 + struct acl *file_acl_temp = NULL;
4468 + struct acl_entry *acl_entry = NULL;
4469 + struct ace_id *ace_id = NULL;
4475 + DEBUG(10,("Entering sys_acl_set_fd\n"));
4476 + acl_length = BUFSIZ;
4477 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4479 + if(file_acl == NULL) {
4481 + DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4485 + memset(file_acl,0,BUFSIZ);
4487 + file_acl->acl_len = ACL_SIZ;
4488 + file_acl->acl_mode = S_IXACL;
4490 + for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4491 + acl_entry_link->entryp->ace_access >>= 6;
4492 + id_type = acl_entry_link->entryp->ace_id->id_type;
4493 + DEBUG(10,("The id_type is %d\n",id_type));
4496 + case SMB_ACL_USER_OBJ:
4497 + file_acl->u_access = acl_entry_link->entryp->ace_access;
4499 + case SMB_ACL_GROUP_OBJ:
4500 + file_acl->g_access = acl_entry_link->entryp->ace_access;
4502 + case SMB_ACL_OTHER:
4503 + file_acl->o_access = acl_entry_link->entryp->ace_access;
4505 + case SMB_ACL_MASK:
4509 + if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4510 + acl_length += sizeof(struct acl_entry);
4511 + file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
4512 + if(file_acl_temp == NULL) {
4513 + SAFE_FREE(file_acl);
4515 + DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4519 + memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4520 + SAFE_FREE(file_acl);
4521 + file_acl = file_acl_temp;
4524 + acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4525 + file_acl->acl_len += sizeof(struct acl_entry);
4526 + acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4527 + acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4529 + /* In order to use this, we'll need to wait until we can get denies */
4530 + /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4531 + acl_entry->ace_type = ACC_SPECIFY; */
4533 + acl_entry->ace_type = ACC_SPECIFY;
4535 + ace_id = acl_entry->ace_id;
4537 + ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4538 + DEBUG(10,("The id type is %d\n",ace_id->id_type));
4539 + ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4540 + memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4541 + memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
4544 + rc = fchacl(fd,file_acl,file_acl->acl_len);
4545 + DEBUG(10,("errno is %d\n",errno));
4546 + DEBUG(10,("return code is %d\n",rc));
4547 + SAFE_FREE(file_acl);
4548 + DEBUG(10,("Exiting sys_acl_set_fd\n"));
4552 +int sys_acl_delete_def_file(const char *name)
4554 + /* AIX has no default ACL */
4558 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4560 + return(*permset & perm);
4563 +int sys_acl_free_text(char *text)
4568 +int sys_acl_free_acl(SMB_ACL_T posix_acl)
4570 + struct acl_entry_link *acl_entry_link;
4572 + for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
4573 + SAFE_FREE(acl_entry_link->prevp->entryp);
4574 + SAFE_FREE(acl_entry_link->prevp);
4577 + SAFE_FREE(acl_entry_link->prevp->entryp);
4578 + SAFE_FREE(acl_entry_link->prevp);
4579 + SAFE_FREE(acl_entry_link->entryp);
4580 + SAFE_FREE(acl_entry_link);
4585 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4590 +#else /* No ACLs. */
4592 +int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
4598 +int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
4604 +int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
4610 +void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
4616 +SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
4619 + return (SMB_ACL_T)NULL;
4622 +SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
4625 + return (SMB_ACL_T)NULL;
4628 +int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
4634 +int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
4640 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4643 + return (permset & perm) ? 1 : 0;
4646 +char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
4652 +int sys_acl_free_text(UNUSED(char *text))
4658 +SMB_ACL_T sys_acl_init(UNUSED(int count))
4664 +int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
4670 +int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
4676 +int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
4682 +int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
4688 +int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
4694 +int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
4700 +int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
4706 +int sys_acl_delete_def_file(UNUSED(const char *name))
4712 +int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
4718 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
4724 +#endif /* No ACLs. */
4726 +/************************************************************************
4727 + Deliberately outside the ACL defines. Return 1 if this is a "no acls"
4729 +************************************************************************/
4731 +int no_acl_syscall_error(int err)
4733 +#if defined(ENOSYS)
4734 + if (err == ENOSYS) {
4738 +#if defined(ENOTSUP)
4739 + if (err == ENOTSUP) {
4745 --- old/lib/sysacls.h
4746 +++ new/lib/sysacls.h
4748 +#define SMB_MALLOC(cnt) new_array(char, cnt)
4749 +#define SMB_MALLOC_P(obj) new_array(obj, 1)
4750 +#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
4751 +#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
4752 +#define slprintf snprintf
4754 +int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
4755 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
4756 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
4757 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
4758 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
4759 +SMB_ACL_T sys_acl_get_fd(int fd);
4760 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
4761 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4762 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4763 +char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
4764 +SMB_ACL_T sys_acl_init(int count);
4765 +int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
4766 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
4767 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
4768 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
4769 +int sys_acl_valid(SMB_ACL_T theacl);
4770 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
4771 +int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
4772 +int sys_acl_delete_def_file(const char *name);
4773 +int sys_acl_free_text(char *text);
4774 +int sys_acl_free_acl(SMB_ACL_T the_acl);
4775 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
4778 @@ -58,7 +58,7 @@ BEGIN {
4782 -!/^OFF_T|^size_t|^off_t|^pid_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
4783 +!/^OFF_T|^size_t|^off_t|^pid_t|^id_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
4789 @@ -45,6 +45,7 @@ int copy_dirlinks = 0;
4791 int preserve_links = 0;
4792 int preserve_hard_links = 0;
4793 +int preserve_acls = 0;
4794 int preserve_perms = 0;
4795 int preserve_executability = 0;
4796 int preserve_devices = 0;
4797 @@ -194,6 +195,7 @@ static void print_rsync_version(enum log
4798 char const *got_socketpair = "no ";
4799 char const *have_inplace = "no ";
4800 char const *hardlinks = "no ";
4801 + char const *acls = "no ";
4802 char const *links = "no ";
4803 char const *ipv6 = "no ";
4804 STRUCT_STAT *dumstat;
4805 @@ -210,6 +212,10 @@ static void print_rsync_version(enum log
4809 +#ifdef SUPPORT_ACLS
4813 #ifdef SUPPORT_LINKS
4816 @@ -223,9 +229,9 @@ static void print_rsync_version(enum log
4817 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
4818 rprintf(f, "<http://rsync.samba.org/>\n");
4819 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
4820 - "%shard links, %ssymlinks, batchfiles,\n",
4821 + "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
4822 (int) (sizeof (OFF_T) * 8),
4823 - got_socketpair, hardlinks, links);
4824 + got_socketpair, hardlinks, acls, links);
4826 /* Note that this field may not have type ino_t. It depends
4827 * on the complicated interaction between largefile feature
4828 @@ -295,6 +301,9 @@ void usage(enum logcode F)
4829 rprintf(F," -H, --hard-links preserve hard links\n");
4830 rprintf(F," -p, --perms preserve permissions\n");
4831 rprintf(F," -E, --executability preserve the file's executability\n");
4832 +#ifdef SUPPORT_ACLS
4833 + rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
4835 rprintf(F," --chmod=CHMOD change destination permissions\n");
4836 rprintf(F," -o, --owner preserve owner (super-user only)\n");
4837 rprintf(F," -g, --group preserve group\n");
4838 @@ -410,6 +419,9 @@ static struct poptOption long_options[]
4839 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
4840 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
4841 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
4842 + {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
4843 + {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4844 + {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4845 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
4846 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
4847 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
4848 @@ -1068,6 +1080,23 @@ int parse_arguments(int *argc, const cha
4853 +#ifdef SUPPORT_ACLS
4854 + preserve_acls = preserve_perms = 1;
4857 + /* FIXME: this should probably be ignored with a
4858 + * warning and then countermeasures taken to
4859 + * restrict group and other access in the presence
4860 + * of any more restrictive ACLs, but this is safe
4862 + snprintf(err_buf,sizeof(err_buf),
4863 + "ACLs are not supported on this %s\n",
4864 + am_server ? "server" : "client");
4870 /* A large opt value means that set_refuse_options()
4871 * turned this option off. */
4872 @@ -1511,6 +1540,10 @@ void server_options(char **args,int *arg
4874 if (preserve_hard_links)
4876 +#ifdef SUPPORT_ACLS
4877 + if (preserve_acls)
4878 + argstr[x++] = 'A';
4885 @@ -46,6 +46,7 @@ extern int keep_partial;
4886 extern int checksum_seed;
4888 extern int delay_updates;
4889 +extern mode_t orig_umask;
4890 extern struct stats stats;
4891 extern char *log_format;
4892 extern char *tmpdir;
4893 @@ -344,6 +345,10 @@ int recv_files(int f_in, struct file_lis
4894 int itemizing = am_daemon ? daemon_log_format_has_i
4895 : !am_server && log_format_has_i;
4896 int max_phase = protocol_version >= 29 ? 2 : 1;
4897 + int dflt_perms = (ACCESSPERMS & ~orig_umask);
4898 +#ifdef SUPPORT_ACLS
4899 + char *parent_dirname = "";
4904 @@ -541,7 +546,16 @@ int recv_files(int f_in, struct file_lis
4905 * mode based on the local permissions and some heuristics. */
4906 if (!preserve_perms) {
4907 int exists = fd1 != -1;
4908 - file->mode = dest_mode(file->mode, st.st_mode, exists);
4909 +#ifdef SUPPORT_ACLS
4910 + char *dn = file->dirname ? file->dirname : ".";
4911 + if (parent_dirname != dn
4912 + && strcmp(parent_dirname, dn) != 0) {
4913 + dflt_perms = default_perms_for_dir(dn);
4914 + parent_dirname = dn;
4917 + file->mode = dest_mode(file->mode, st.st_mode,
4918 + dflt_perms, exists);
4921 /* We now check to see if we are writing file "inplace" */
4927 extern int daemon_log_format_has_i;
4928 +extern int preserve_acls;
4929 extern int preserve_perms;
4930 extern int preserve_executability;
4931 extern int preserve_times;
4932 @@ -101,7 +102,8 @@ void free_sums(struct sum_struct *s)
4934 /* This is only called when we aren't preserving permissions. Figure out what
4935 * the permissions should be and return them merged back into the mode. */
4936 -mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int exists)
4937 +mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int dflt_perms,
4940 /* If the file already exists, we'll return the local permissions,
4941 * possibly tweaked by the --executability option. */
4942 @@ -116,7 +118,7 @@ mode_t dest_mode(mode_t flist_mode, mode
4943 cur_mode |= (cur_mode & 0444) >> 2;
4946 - cur_mode = flist_mode & ACCESSPERMS & ~orig_umask;
4947 + cur_mode = flist_mode & ACCESSPERMS & dflt_perms;
4948 if (daemon_chmod_modes && !S_ISLNK(flist_mode))
4949 cur_mode = tweak_mode(cur_mode, daemon_chmod_modes);
4950 return (flist_mode & ~CHMOD_BITS) | (cur_mode & CHMOD_BITS);
4951 @@ -217,6 +219,13 @@ int set_file_attrs(char *fname, struct f
4955 +#ifdef SUPPORT_ACLS
4956 + /* It's OK to call set_acl() now, even for a dir, as the generator
4957 + * will enable owner-writability using chmod, if necessary. */
4958 + if (preserve_acls && set_acl(fname, file) == 0)
4962 if (verbose > 1 && flags & ATTRS_REPORT) {
4963 enum logcode code = daemon_log_format_has_i || dry_run
4967 @@ -658,6 +658,20 @@ struct chmod_mode_struct;
4969 #define UNUSED(x) x __attribute__((__unused__))
4971 +#if HAVE_POSIX_ACLS|HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|\
4972 + HAVE_HPUX_ACLS|HAVE_IRIX_ACLS|HAVE_AIX_ACLS|HAVE_TRU64_ACLS
4973 +#define SUPPORT_ACLS 1
4976 +#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
4977 +#define ACLS_NEED_MASK 1
4980 +#if defined SUPPORT_ACLS && defined HAVE_SYS_ACL_H
4981 +#include <sys/acl.h>
4983 +#include "smb_acls.h"
4987 /* We have replacement versions of these if they're missing. */
4990 @@ -321,6 +321,7 @@ to the detailed description below for a
4991 -H, --hard-links preserve hard links
4992 -p, --perms preserve permissions
4993 -E, --executability preserve executability
4994 + -A, --acls preserve ACLs (implies -p) [non-standard]
4995 --chmod=CHMOD change destination permissions
4996 -o, --owner preserve owner (super-user only)
4997 -g, --group preserve group
4998 @@ -742,7 +743,9 @@ quote(itemize(
4999 permissions, though the bf(--executability) option might change just
5000 the execute permission for the file.
5001 it() New files get their "normal" permission bits set to the source
5002 - file's permissions masked with the receiving end's umask setting, and
5003 + file's permissions masked with the receiving directory's default
5004 + permissions (either the receiving process's umask, or the permissions
5005 + specified via the destination directory's default ACL), and
5006 their special permission bits disabled except in the case where a new
5007 directory inherits a setgid bit from its parent directory.
5009 @@ -773,9 +776,11 @@ The preservation of the destination's se
5010 directories when bf(--perms) is off was added in rsync 2.6.7. Older rsync
5011 versions erroneously preserved the three special permission bits for
5012 newly-created files when bf(--perms) was off, while overriding the
5013 -destination's setgid bit setting on a newly-created directory. (Keep in
5014 -mind that it is the version of the receiving rsync that affects this
5016 +destination's setgid bit setting on a newly-created directory. Default ACL
5017 +observance was added to the ACL patch for rsync 2.6.7, so older (or
5018 +non-ACL-enabled) rsyncs use the umask even if default ACLs are present.
5019 +(Keep in mind that it is the version of the receiving rsync that affects
5022 dit(bf(-E, --executability)) This option causes rsync to preserve the
5023 executability (or non-executability) of regular files when bf(--perms) is
5024 @@ -793,6 +798,10 @@ quote(itemize(
5026 If bf(--perms) is enabled, this option is ignored.
5028 +dit(bf(-A, --acls)) This option causes rsync to update the destination
5029 +ACLs to be the same as the source ACLs. This nonstandard option only
5030 +works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
5032 dit(bf(--chmod)) This option tells rsync to apply one or more
5033 comma-separated "chmod" strings to the permission of the files in the
5034 transfer. The resulting value is treated as though it was the permissions
5039 + Unix SMB/Netbios implementation.
5041 + Portable SMB ACL interface
5042 + Copyright (C) Jeremy Allison 2000
5044 + This program is free software; you can redistribute it and/or modify
5045 + it under the terms of the GNU General Public License as published by
5046 + the Free Software Foundation; either version 2 of the License, or
5047 + (at your option) any later version.
5049 + This program is distributed in the hope that it will be useful,
5050 + but WITHOUT ANY WARRANTY; without even the implied warranty of
5051 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5052 + GNU General Public License for more details.
5054 + You should have received a copy of the GNU General Public License
5055 + along with this program; if not, write to the Free Software
5056 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
5059 +#ifndef _SMB_ACLS_H
5060 +#define _SMB_ACLS_H
5062 +#if defined HAVE_POSIX_ACLS
5064 +/* This is an identity mapping (just remove the SMB_). */
5066 +#define SMB_ACL_TAG_T acl_tag_t
5067 +#define SMB_ACL_TYPE_T acl_type_t
5068 +#define SMB_ACL_PERMSET_T acl_permset_t
5069 +#define SMB_ACL_PERM_T acl_perm_t
5070 +#define SMB_ACL_READ ACL_READ
5071 +#define SMB_ACL_WRITE ACL_WRITE
5072 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5074 +/* Types of ACLs. */
5075 +#define SMB_ACL_USER ACL_USER
5076 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5077 +#define SMB_ACL_GROUP ACL_GROUP
5078 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5079 +#define SMB_ACL_OTHER ACL_OTHER
5080 +#define SMB_ACL_MASK ACL_MASK
5082 +#define SMB_ACL_T acl_t
5084 +#define SMB_ACL_ENTRY_T acl_entry_t
5086 +#define SMB_ACL_FIRST_ENTRY ACL_FIRST_ENTRY
5087 +#define SMB_ACL_NEXT_ENTRY ACL_NEXT_ENTRY
5089 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5090 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5092 +#elif defined HAVE_TRU64_ACLS
5094 +/* This is for DEC/Compaq Tru64 UNIX */
5096 +#define SMB_ACL_TAG_T acl_tag_t
5097 +#define SMB_ACL_TYPE_T acl_type_t
5098 +#define SMB_ACL_PERMSET_T acl_permset_t
5099 +#define SMB_ACL_PERM_T acl_perm_t
5100 +#define SMB_ACL_READ ACL_READ
5101 +#define SMB_ACL_WRITE ACL_WRITE
5102 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5104 +/* Types of ACLs. */
5105 +#define SMB_ACL_USER ACL_USER
5106 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5107 +#define SMB_ACL_GROUP ACL_GROUP
5108 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5109 +#define SMB_ACL_OTHER ACL_OTHER
5110 +#define SMB_ACL_MASK ACL_MASK
5112 +#define SMB_ACL_T acl_t
5114 +#define SMB_ACL_ENTRY_T acl_entry_t
5116 +#define SMB_ACL_FIRST_ENTRY 0
5117 +#define SMB_ACL_NEXT_ENTRY 1
5119 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5120 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5122 +#elif defined HAVE_UNIXWARE_ACLS || defined HAVE_SOLARIS_ACLS
5124 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
5125 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
5128 +/* SVR4.2 ES/MP ACLs */
5129 +typedef int SMB_ACL_TAG_T;
5130 +typedef int SMB_ACL_TYPE_T;
5131 +typedef ushort *SMB_ACL_PERMSET_T;
5132 +typedef ushort SMB_ACL_PERM_T;
5133 +#define SMB_ACL_READ 4
5134 +#define SMB_ACL_WRITE 2
5135 +#define SMB_ACL_EXECUTE 1
5137 +/* Types of ACLs. */
5138 +#define SMB_ACL_USER USER
5139 +#define SMB_ACL_USER_OBJ USER_OBJ
5140 +#define SMB_ACL_GROUP GROUP
5141 +#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5142 +#define SMB_ACL_OTHER OTHER_OBJ
5143 +#define SMB_ACL_MASK CLASS_OBJ
5145 +typedef struct SMB_ACL_T {
5149 + struct acl acl[1];
5152 +typedef struct acl *SMB_ACL_ENTRY_T;
5154 +#define SMB_ACL_FIRST_ENTRY 0
5155 +#define SMB_ACL_NEXT_ENTRY 1
5157 +#define SMB_ACL_TYPE_ACCESS 0
5158 +#define SMB_ACL_TYPE_DEFAULT 1
5160 +#elif defined HAVE_HPUX_ACLS
5163 + * Based on the Solaris & UnixWare code.
5167 +#include <sys/aclv.h>
5169 +/* SVR4.2 ES/MP ACLs */
5170 +typedef int SMB_ACL_TAG_T;
5171 +typedef int SMB_ACL_TYPE_T;
5172 +typedef ushort *SMB_ACL_PERMSET_T;
5173 +typedef ushort SMB_ACL_PERM_T;
5174 +#define SMB_ACL_READ 4
5175 +#define SMB_ACL_WRITE 2
5176 +#define SMB_ACL_EXECUTE 1
5178 +/* Types of ACLs. */
5179 +#define SMB_ACL_USER USER
5180 +#define SMB_ACL_USER_OBJ USER_OBJ
5181 +#define SMB_ACL_GROUP GROUP
5182 +#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5183 +#define SMB_ACL_OTHER OTHER_OBJ
5184 +#define SMB_ACL_MASK CLASS_OBJ
5186 +typedef struct SMB_ACL_T {
5190 + struct acl acl[1];
5193 +typedef struct acl *SMB_ACL_ENTRY_T;
5195 +#define SMB_ACL_FIRST_ENTRY 0
5196 +#define SMB_ACL_NEXT_ENTRY 1
5198 +#define SMB_ACL_TYPE_ACCESS 0
5199 +#define SMB_ACL_TYPE_DEFAULT 1
5201 +#elif defined HAVE_IRIX_ACLS
5203 +#define SMB_ACL_TAG_T acl_tag_t
5204 +#define SMB_ACL_TYPE_T acl_type_t
5205 +#define SMB_ACL_PERMSET_T acl_permset_t
5206 +#define SMB_ACL_PERM_T acl_perm_t
5207 +#define SMB_ACL_READ ACL_READ
5208 +#define SMB_ACL_WRITE ACL_WRITE
5209 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5211 +/* Types of ACLs. */
5212 +#define SMB_ACL_USER ACL_USER
5213 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5214 +#define SMB_ACL_GROUP ACL_GROUP
5215 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5216 +#define SMB_ACL_OTHER ACL_OTHER_OBJ
5217 +#define SMB_ACL_MASK ACL_MASK
5219 +typedef struct SMB_ACL_T {
5225 +#define SMB_ACL_ENTRY_T acl_entry_t
5227 +#define SMB_ACL_FIRST_ENTRY 0
5228 +#define SMB_ACL_NEXT_ENTRY 1
5230 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5231 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5233 +#elif defined HAVE_AIX_ACLS
5235 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
5237 +#include "/usr/include/acl.h"
5239 +typedef uint *SMB_ACL_PERMSET_T;
5241 +struct acl_entry_link{
5242 + struct acl_entry_link *prevp;
5243 + struct new_acl_entry *entryp;
5244 + struct acl_entry_link *nextp;
5248 +struct new_acl_entry{
5249 + unsigned short ace_len;
5250 + unsigned short ace_type;
5251 + unsigned int ace_access;
5252 + struct ace_id ace_id[1];
5255 +#define SMB_ACL_ENTRY_T struct new_acl_entry*
5256 +#define SMB_ACL_T struct acl_entry_link*
5258 +#define SMB_ACL_TAG_T unsigned short
5259 +#define SMB_ACL_TYPE_T int
5260 +#define SMB_ACL_PERM_T uint
5261 +#define SMB_ACL_READ S_IRUSR
5262 +#define SMB_ACL_WRITE S_IWUSR
5263 +#define SMB_ACL_EXECUTE S_IXUSR
5265 +/* Types of ACLs. */
5266 +#define SMB_ACL_USER ACEID_USER
5267 +#define SMB_ACL_USER_OBJ 3
5268 +#define SMB_ACL_GROUP ACEID_GROUP
5269 +#define SMB_ACL_GROUP_OBJ 4
5270 +#define SMB_ACL_OTHER 5
5271 +#define SMB_ACL_MASK 6
5274 +#define SMB_ACL_FIRST_ENTRY 1
5275 +#define SMB_ACL_NEXT_ENTRY 2
5277 +#define SMB_ACL_TYPE_ACCESS 0
5278 +#define SMB_ACL_TYPE_DEFAULT 1
5280 +#else /* No ACLs. */
5282 +/* No ACLS - fake it. */
5283 +#define SMB_ACL_TAG_T int
5284 +#define SMB_ACL_TYPE_T int
5285 +#define SMB_ACL_PERMSET_T mode_t
5286 +#define SMB_ACL_PERM_T mode_t
5287 +#define SMB_ACL_READ S_IRUSR
5288 +#define SMB_ACL_WRITE S_IWUSR
5289 +#define SMB_ACL_EXECUTE S_IXUSR
5291 +/* Types of ACLs. */
5292 +#define SMB_ACL_USER 0
5293 +#define SMB_ACL_USER_OBJ 1
5294 +#define SMB_ACL_GROUP 2
5295 +#define SMB_ACL_GROUP_OBJ 3
5296 +#define SMB_ACL_OTHER 4
5297 +#define SMB_ACL_MASK 5
5299 +typedef struct SMB_ACL_T {
5303 +typedef struct SMB_ACL_ENTRY_T {
5305 +} *SMB_ACL_ENTRY_T;
5307 +#define SMB_ACL_FIRST_ENTRY 0
5308 +#define SMB_ACL_NEXT_ENTRY 1
5310 +#define SMB_ACL_TYPE_ACCESS 0
5311 +#define SMB_ACL_TYPE_DEFAULT 1
5313 +#endif /* No ACLs. */
5314 +#endif /* _SMB_ACLS_H */
5315 --- old/testsuite/default-acls.test
5316 +++ new/testsuite/default-acls.test
5320 +# This program is distributable under the terms of the GNU GPL see
5323 +# Test that rsync obeys default ACLs. -- Matt McCutchen
5325 +. $srcdir/testsuite/rsync.fns
5327 +$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
5328 +setfacl -dm u::rwx,g::---,o::--- "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
5330 +# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
5332 + todir="$scratchdir/$1"
5334 + # FIXME This doesn't work on solaris...
5335 + setfacl -k "$todir"
5336 + [ "$2" ] && setfacl -dm "$2" "$todir"
5337 + # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
5338 + # even though the directory itself is outside the transfer
5339 + $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
5340 + check_perms "$todir/to" $4 "Target $1"
5341 + check_perms "$todir/to/dir" $4 "Target $1"
5342 + check_perms "$todir/to/file" $3 "Target $1"
5343 + check_perms "$todir/to/program" $4 "Target $1"
5344 + # Make sure get_local_name doesn't mess us up when transferring only one file
5345 + $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
5346 + check_perms "$todir/to/anotherfile" $3 "Target $1"
5347 + # Make sure we obey default ACLs when not transferring a regular file
5348 + $RSYNC -rvv "$scratchdir/dir/" "$todir/to/anotherdir/"
5349 + check_perms "$todir/to/anotherdir" $4 "Target $1"
5352 +mkdir "$scratchdir/dir"
5353 +echo "File!" >"$scratchdir/file"
5354 +echo "#!/bin/sh" >"$scratchdir/program"
5355 +chmod 777 "$scratchdir/dir"
5356 +chmod 666 "$scratchdir/file"
5357 +chmod 777 "$scratchdir/program"
5359 +# Test some target directories
5361 +testit da777 u::rwx,g::rwx,o::rwx rw-rw-rw- rwxrwxrwx
5362 +testit da775 u::rwx,g::rwx,o::r-x rw-rw-r-- rwxrwxr-x
5363 +testit da750 u::rwx,g::r-x,o::--- rw-r----- rwxr-x---
5364 +testit da770mask u::rwx,g::---,m::rwx,o::--- rw-rw---- rwxrwx---
5365 +testit noda1 '' rw------- rwx------
5367 +testit noda2 '' rw-rw-rw- rwxrwxrwx
5369 +testit noda3 '' rw-r--r-- rwxr-xr-x
5377 extern int preserve_uid;
5378 extern int preserve_gid;
5379 +extern int preserve_acls;
5380 extern int numeric_ids;
5383 @@ -274,7 +275,7 @@ void send_uid_list(int f)
5387 - if (preserve_uid) {
5388 + if (preserve_uid || preserve_acls) {
5390 /* we send sequences of uid/byte-length/name */
5391 for (list = uidlist; list; list = list->next) {
5392 @@ -291,7 +292,7 @@ void send_uid_list(int f)
5396 - if (preserve_gid) {
5397 + if (preserve_gid || preserve_acls) {
5399 for (list = gidlist; list; list = list->next) {
5401 @@ -312,7 +313,7 @@ void recv_uid_list(int f, struct file_li
5405 - if (preserve_uid && !numeric_ids) {
5406 + if ((preserve_uid || preserve_acls) && !numeric_ids) {
5407 /* read the uid list */
5408 while ((id = read_int(f)) != 0) {
5409 int len = read_byte(f);
5410 @@ -324,7 +325,7 @@ void recv_uid_list(int f, struct file_li
5414 - if (preserve_gid && !numeric_ids) {
5415 + if ((preserve_gid || preserve_acls) && !numeric_ids) {
5416 /* read the gid list */
5417 while ((id = read_int(f)) != 0) {
5418 int len = read_byte(f);
5419 @@ -336,6 +337,18 @@ void recv_uid_list(int f, struct file_li
5423 +#ifdef SUPPORT_ACLS
5424 + if (preserve_acls && !numeric_ids) {
5426 + /* The enumerations don't return 0 except to flag the last
5427 + * entry, since uidlist doesn't munge 0 anyway. */
5428 + while ((id = next_acl_uid(flist)) != 0)
5429 + acl_uid_map(match_uid(id));
5430 + while ((id = next_acl_gid(flist)) != 0)
5431 + acl_gid_map(match_gid(id));
5435 /* Now convert all the uids/gids from sender values to our values. */
5436 if (am_root && preserve_uid && !numeric_ids) {
5437 for (i = 0; i < flist->count; i++)