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"
62 +extern int preserve_acls;
65 +extern int orig_umask;
70 + SMB_ACL_TAG_T tag_type;
79 +static const rsync_acl rsync_acl_initializer = { 0, 0, NULL };
80 +static SMB_ACL_TYPE_T types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
82 +static void expand_rsync_acl(rsync_acl *racl)
84 + /* First time through, 0 <= 0, so list is expanded.
85 + * (Diabolical, rsync guys!) */
86 + if (racl->malloced <= racl->count) {
88 + size_t new_size = racl->malloced + 10;
89 + new_ptr = realloc_array(racl->races, rsync_ace, new_size);
91 + rprintf(FINFO, "expand rsync_acl to %.0f bytes, did%s move\n",
92 + (double) new_size * sizeof racl->races[0],
93 + racl->races ? "" : " not");
96 + racl->races = new_ptr;
97 + racl->malloced = new_size;
100 + out_of_memory("expand_rsync_acl");
104 +static void rsync_acl_free(rsync_acl *racl)
107 + racl->races = NULL;
109 + racl->malloced = 0;
112 +static int rsync_ace_sorter(const void *r1, const void *r2)
114 + rsync_ace *race1 = (rsync_ace *)r1;
115 + SMB_ACL_TAG_T rtag1 = race1->tag_type;
116 + id_t rid1 = race1->id;
117 + rsync_ace *race2 = (rsync_ace *)r2;
118 + SMB_ACL_TAG_T rtag2 = race2->tag_type;
119 + id_t rid2 = race2->id;
120 + /* start at the extrema */
121 + if (rtag1 == SMB_ACL_USER_OBJ || rtag2 == SMB_ACL_MASK)
123 + if (rtag2 == SMB_ACL_USER_OBJ || rtag1 == SMB_ACL_MASK)
126 + if (rtag1 == SMB_ACL_OTHER)
128 + if (rtag2 == SMB_ACL_OTHER)
130 + /* only SMB_ACL_USERs and SMB_ACL_GROUP*s left */
131 + if (rtag1 == SMB_ACL_USER) {
133 + case SMB_ACL_GROUP:
134 + case SMB_ACL_GROUP_OBJ:
135 + case SMB_ACL_OTHER:
139 + return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
141 + if (rtag2 == SMB_ACL_USER)
143 + /* only SMB_ACL_GROUP*s to worry about; kick out GROUP_OBJs first */
144 + if (rtag1 == SMB_ACL_GROUP_OBJ)
146 + if (rtag2 == SMB_ACL_GROUP_OBJ)
148 + /* only SMB_ACL_GROUPs left */
149 + return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
152 +static void sort_rsync_acl(rsync_acl *racl)
156 + qsort((void **)racl->races, racl->count, sizeof racl->races[0],
157 + &rsync_ace_sorter);
160 +static BOOL unpack_smb_acl(rsync_acl *racl, SMB_ACL_T sacl)
162 + SMB_ACL_ENTRY_T entry;
164 + const char *errfun;
165 + *racl = rsync_acl_initializer;
166 + errfun = "sys_acl_get_entry";
167 + for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
169 + rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
170 + SMB_ACL_PERMSET_T permset;
173 + expand_rsync_acl(racl);
174 + race = &racl->races[racl->count++];
175 + if ((rc = sys_acl_get_tag_type(entry, &race->tag_type))) {
176 + errfun = "sys_acl_get_tag_type";
179 + if ((rc = sys_acl_get_permset(entry, &permset))) {
180 + errfun = "sys_acl_get_tag_type";
183 + race->access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
184 + | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
185 + | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
186 + switch (race->tag_type) {
188 + case SMB_ACL_GROUP:
193 + if (!(qualifier = sys_acl_get_qualifier(entry))) {
194 + errfun = "sys_acl_get_tag_type";
198 + race->id = *((id_t *)qualifier);
199 + sys_acl_free_qualifier(qualifier, race->tag_type);
202 + rprintf(FERROR, "unpack_smb_acl: %s(): %s\n",
203 + errfun, strerror(errno));
204 + rsync_acl_free(racl);
207 + sort_rsync_acl(racl);
211 +static BOOL rsync_acls_equal(const rsync_acl *racl1, const rsync_acl *racl2)
213 + rsync_ace *race1, *race2;
214 + size_t count = racl1->count;
215 + if (count != racl2->count)
217 + race1 = racl1->races;
218 + race2 = racl2->races;
219 + for (; count--; race1++, race2++) {
220 + if (race1->tag_type != race2->tag_type
221 + || race1->access != race2->access
222 + || ((race1->tag_type == SMB_ACL_USER
223 + || race1->tag_type == SMB_ACL_GROUP)
224 + && race1->id != race2->id))
236 +static rsync_acl_list _rsync_acl_lists[] = {
237 + { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
238 + { 0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
241 +static inline rsync_acl_list *rsync_acl_lists(SMB_ACL_TYPE_T type)
243 + return type == SMB_ACL_TYPE_ACCESS ? &_rsync_acl_lists[0]
244 + : &_rsync_acl_lists[1];
247 +static void expand_rsync_acl_list(rsync_acl_list *racl_list)
249 + /* First time through, 0 <= 0, so list is expanded.
250 + * (Diabolical, rsync guys!) */
251 + if (racl_list->malloced <= racl_list->count) {
252 + rsync_acl *new_ptr;
254 + if (racl_list->malloced < 1000)
255 + new_size = racl_list->malloced + 1000;
257 + new_size = racl_list->malloced * 2;
258 + new_ptr = realloc_array(racl_list->racls, rsync_acl, new_size);
259 + if (verbose >= 3) {
260 + rprintf(FINFO, "expand_rsync_acl_list to %.0f bytes, did%s move\n",
261 + (double) new_size * sizeof racl_list->racls[0],
262 + racl_list->racls ? "" : " not");
265 + racl_list->racls = new_ptr;
266 + racl_list->malloced = new_size;
268 + if (!racl_list->racls)
269 + out_of_memory("expand_rsync_acl_list");
274 +static void free_rsync_acl_list(rsync_acl_list *racl_list)
276 + /* Run this in reverse, so references are freed before referents,
277 + * although not currently necessary. */
278 + while (racl_list->count--) {
279 + rsync_acl *racl = &racl_list->racls[racl_list->count];
281 + rsync_acl_free(racl);
283 + free(racl_list->racls);
284 + racl_list->racls = NULL;
285 + racl_list->malloced = 0;
289 +static int find_matching_rsync_acl(SMB_ACL_TYPE_T type,
290 + const rsync_acl_list *racl_list,
291 + const rsync_acl *racl)
293 + static int access_match = -1, default_match = -1;
294 + int *match = (type == SMB_ACL_TYPE_ACCESS) ?
295 + &access_match : &default_match;
296 + size_t count = racl_list->count;
297 + /* If this is the first time through or we didn't match the last
298 + * time, then start at the end of the list, which should be the
299 + * best place to start hunting. */
301 + *match = racl_list->count - 1;
303 + if (rsync_acls_equal(&racl_list->racls[*match], racl))
306 + *match = racl_list->count - 1;
312 +/* The general strategy with the tag_type <-> character mapping is that
313 + * lowercase implies that no qualifier follows, where uppercase does.
314 + * A similar idiom for the acl type (access or default) itself, but
315 + * lowercase in this instance means there's no ACL following, so the
316 + * ACL is a repeat, so the receiver should reuse the last of the same
319 +static void send_rsync_acl(int f, const rsync_acl *racl)
322 + size_t count = racl->count;
323 + write_int(f, count);
324 + for (race = racl->races; count--; race++) {
326 + switch (race->tag_type) {
327 + case SMB_ACL_USER_OBJ:
333 + case SMB_ACL_GROUP_OBJ:
336 + case SMB_ACL_GROUP:
339 + case SMB_ACL_OTHER:
347 + "send_rsync_acl: unknown tag_type (%02x) on ACE; disregarding\n",
352 + write_byte(f, race->access);
353 + if (isupper((int)ch)) {
354 + write_int(f, race->id);
355 + /* FIXME: sorta wasteful: we should maybe buffer as
356 + * many ids as max(ACL_USER + ACL_GROUP) objects to
357 + * keep from making so many calls. */
366 +static rsync_acl _curr_rsync_acls[2];
369 +static const char *str_acl_type(SMB_ACL_TYPE_T type)
371 + return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS" :
372 + type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT" :
373 + "unknown SMB_ACL_TYPE_T";
377 + * Overwrite racl with a new three-entry ACL from the given permissions.
379 +static void perms_to_acl(int perms, rsync_acl *racl)
382 + expand_rsync_acl(racl);
383 + racl->races[racl->count].tag_type = SMB_ACL_USER_OBJ;
384 + racl->races[racl->count++].access = (perms >> 6) & 7;
385 + expand_rsync_acl(racl);
386 + racl->races[racl->count].tag_type = SMB_ACL_GROUP_OBJ;
387 + racl->races[racl->count++].access = (perms >> 3) & 7;
388 + expand_rsync_acl(racl);
389 + racl->races[racl->count].tag_type = SMB_ACL_OTHER;
390 + racl->races[racl->count++].access = (perms >> 0) & 7;
393 +/* Generate the ACL(s) for this flist entry;
394 + * ACL(s) are either sent or cleaned-up by send_acl() below. */
396 +int make_acl(const struct file_struct *file, const char *fname)
398 + SMB_ACL_TYPE_T *type;
399 + rsync_acl *curr_racl;
401 + if (!preserve_acls || S_ISLNK(file->mode))
403 + for (type = &types[0], curr_racl = &_curr_rsync_acls[0];
404 + type < &types[0] + sizeof types / sizeof types[0]
405 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
406 + type++, curr_racl++) {
409 + *curr_racl = rsync_acl_initializer;
410 + if ((sacl = sys_acl_get_file(fname, *type)) != 0) {
411 + ok = unpack_smb_acl(curr_racl, sacl);
412 + sys_acl_free_acl(sacl);
415 + } else if (errno == ENOTSUP) {
416 + /* ACLs are not supported. Invent an access ACL from
417 + * permissions; let the default ACL default to empty. */
418 + if (*type == SMB_ACL_TYPE_ACCESS)
419 + perms_to_acl(file->mode & ACCESSPERMS, curr_racl);
421 + rprintf(FERROR, "send_acl: sys_acl_get_file(%s, %s): %s\n",
422 + fname, str_acl_type(*type), strerror(errno));
429 +/* Send the make_acl()-generated ACLs for this flist entry,
430 + * or clean up after an flist entry that's not being sent (f == -1). */
432 +void send_acl(const struct file_struct *file, int f)
434 + SMB_ACL_TYPE_T *type;
435 + rsync_acl *curr_racl;
437 + if (!preserve_acls || S_ISLNK(file->mode))
439 + for (type = &types[0], curr_racl = &_curr_rsync_acls[0];
440 + type < &types[0] + sizeof types / sizeof types[0]
441 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
442 + type++, curr_racl++) {
444 + rsync_acl_list *racl_list = rsync_acl_lists(*type);
446 + rsync_acl_free(curr_racl);
449 + if ((index = find_matching_rsync_acl(*type, racl_list, curr_racl))
451 + write_byte(f, *type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
452 + write_int(f, index);
453 + rsync_acl_free(curr_racl);
455 + write_byte(f, *type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
456 + send_rsync_acl(f, curr_racl);
457 + expand_rsync_acl_list(racl_list);
458 + racl_list->racls[racl_list->count++] = *curr_racl;
463 +/* The below stuff is only used by the receiver: */
465 +/* structure to hold index to rsync_acl_list member corresponding to
466 + * flist->files[i] */
469 + const struct file_struct *file;
476 + file_acl_index *fileaclidxs;
477 +} file_acl_index_list;
479 +static file_acl_index_list _file_acl_index_lists[] = {
480 + {0, 0, NULL },/* SMB_ACL_TYPE_ACCESS */
481 + {0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
484 +static inline file_acl_index_list *file_acl_index_lists(SMB_ACL_TYPE_T type)
486 + return type == SMB_ACL_TYPE_ACCESS ?
487 + &_file_acl_index_lists[0] : &_file_acl_index_lists[1];
490 +static void expand_file_acl_index_list(file_acl_index_list *fileaclidx_list)
492 + /* First time through, 0 <= 0, so list is expanded.
493 + * (Diabolical, rsync guys!) */
494 + if (fileaclidx_list->malloced <= fileaclidx_list->count) {
495 + file_acl_index *new_ptr;
497 + if (fileaclidx_list->malloced < 1000)
498 + new_size = fileaclidx_list->malloced + 1000;
500 + new_size = fileaclidx_list->malloced * 2;
501 + new_ptr = realloc_array(fileaclidx_list->fileaclidxs, file_acl_index, new_size);
502 + if (verbose >= 3) {
503 + rprintf(FINFO, "expand_file_acl_index_list to %.0f bytes, did%s move\n",
504 + (double) new_size * sizeof fileaclidx_list->fileaclidxs[0],
505 + fileaclidx_list->fileaclidxs ? "" : " not");
508 + fileaclidx_list->fileaclidxs = new_ptr;
509 + fileaclidx_list->malloced = new_size;
511 + if (!fileaclidx_list->fileaclidxs)
512 + out_of_memory("expand_file_acl_index_list");
517 +static void free_file_acl_index_list(file_acl_index_list *fileaclidx_list)
519 + free(fileaclidx_list->fileaclidxs);
520 + fileaclidx_list->fileaclidxs = NULL;
521 + fileaclidx_list->malloced = 0;
525 +/* lists to hold the SMB_ACL_Ts corresponding to the rsync_acl_list entries */
533 +static smb_acl_list _smb_acl_lists[] = {
534 + { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
535 + { 0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
538 +static inline smb_acl_list *smb_acl_lists(SMB_ACL_TYPE_T type)
540 + return type == SMB_ACL_TYPE_ACCESS ? &_smb_acl_lists[0] :
541 + &_smb_acl_lists[1];
544 +static void expand_smb_acl_list(smb_acl_list *sacl_list)
546 + /* First time through, 0 <= 0, so list is expanded.
547 + * (Diabolical, rsync guys!) */
548 + if (sacl_list->malloced <= sacl_list->count) {
549 + SMB_ACL_T *new_ptr;
551 + if (sacl_list->malloced < 1000)
552 + new_size = sacl_list->malloced + 1000;
554 + new_size = sacl_list->malloced * 2;
555 + new_ptr = realloc_array(sacl_list->sacls, SMB_ACL_T, new_size);
556 + if (verbose >= 3) {
557 + rprintf(FINFO, "expand_smb_acl_list to %.0f bytes, did%s move\n",
558 + (double) new_size * sizeof sacl_list->sacls[0],
559 + sacl_list->sacls ? "" : " not");
562 + sacl_list->sacls = new_ptr;
563 + sacl_list->malloced = new_size;
565 + if (!sacl_list->sacls)
566 + out_of_memory("expand_smb_acl_list");
571 +static void free_smb_acl_list(SMB_ACL_TYPE_T type)
573 + smb_acl_list *sacl_list = smb_acl_lists(type);
574 + SMB_ACL_T *sacl = sacl_list->sacls;
575 + while (sacl_list->count--) {
577 + sys_acl_free_acl(*sacl++);
579 + free(sacl_list->sacls);
580 + sacl_list->sacls = NULL;
581 + sacl_list->malloced = 0;
585 +/* build an SMB_ACL_T corresponding to an rsync_acl */
586 +static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
588 + size_t count = racl->count;
589 + rsync_ace *race = racl->races;
590 + const char *errfun = NULL;
591 + *smb_acl = sys_acl_init(count);
593 + rprintf(FERROR, "pack_smb_acl: sys_acl_int(): %s\n",
597 + for (; count--; race++) {
598 + SMB_ACL_ENTRY_T entry;
599 + SMB_ACL_PERMSET_T permset;
600 + if (sys_acl_create_entry(smb_acl, &entry)) {
601 + errfun = "sys_acl_create)";
604 + if (sys_acl_set_tag_type(entry, race->tag_type)) {
605 + errfun = "sys_acl_set_tag";
608 + if (race->tag_type == SMB_ACL_USER ||
609 + race->tag_type == SMB_ACL_GROUP)
610 + if (sys_acl_set_qualifier(entry, (void*)&race->id)) {
611 + errfun = "sys_acl_set_qualfier";
614 + if (sys_acl_get_permset(entry, &permset)) {
615 + errfun = "sys_acl_get_permset";
618 + if (sys_acl_clear_perms(permset)) {
619 + errfun = "sys_acl_clear_perms";
622 + if (race->access & 4)
623 + if (sys_acl_add_perm(permset, SMB_ACL_READ)) {
624 + errfun = "sys_acl_add_perm";
627 + if (race->access & 2)
628 + if (sys_acl_add_perm(permset, SMB_ACL_WRITE)) {
629 + errfun = "sys_acl_add_perm";
632 + if (race->access & 1)
633 + if (sys_acl_add_perm(permset, SMB_ACL_EXECUTE)) {
634 + errfun = "sys_acl_add_perm";
637 + if (sys_acl_set_permset(entry, permset)) {
638 + errfun = "sys_acl_set_permset";
643 + sys_acl_free_acl(*smb_acl);
644 + rprintf(FERROR, "pack_smb_acl %s(): %s\n", errfun,
651 +static void receive_rsync_acl(rsync_acl *racl, int f)
654 + uchar required_mask_perm = 0;
656 + BOOL saw_mask = False;
657 + BOOL saw_user_obj = False, saw_group_obj = False,
659 + size_t count = read_int(f);
664 + uchar tag = read_byte(f);
665 + expand_rsync_acl(racl);
666 + race = &racl->races[racl->count++];
669 + race->tag_type = SMB_ACL_USER_OBJ;
670 + saw_user_obj = True;
673 + race->tag_type = SMB_ACL_USER;
676 + race->tag_type = SMB_ACL_GROUP_OBJ;
677 + saw_group_obj = True;
680 + race->tag_type = SMB_ACL_GROUP;
683 + race->tag_type = SMB_ACL_OTHER;
687 + race->tag_type = SMB_ACL_MASK;
691 + rprintf(FERROR, "receive_rsync_acl: unknown tag %c\n",
693 + exit_cleanup(RERR_STREAMIO);
695 + race->access = read_byte(f);
696 + if (race->access & ~ (4 | 2 | 1)) {
697 + rprintf(FERROR, "receive_rsync_acl: bogus permset %o\n",
699 + exit_cleanup(RERR_STREAMIO);
701 + if (race->tag_type == SMB_ACL_USER ||
702 + race->tag_type == SMB_ACL_GROUP) {
703 + race->id = read_int(f);
705 + required_mask_perm |= race->access;
709 + else if (race->tag_type == SMB_ACL_GROUP_OBJ)
710 + required_mask_perm |= race->access;
714 + if (!saw_user_obj) {
715 + expand_rsync_acl(racl);
716 + race = &racl->races[racl->count++];
717 + race->tag_type = SMB_ACL_USER_OBJ;
720 + if (!saw_group_obj) {
721 + expand_rsync_acl(racl);
722 + race = &racl->races[racl->count++];
723 + race->tag_type = SMB_ACL_GROUP_OBJ;
727 + expand_rsync_acl(racl);
728 + race = &racl->races[racl->count++];
729 + race->tag_type = SMB_ACL_OTHER;
734 + expand_rsync_acl(racl);
735 + race = &racl->races[racl->count++];
736 + race->tag_type = SMB_ACL_MASK;
737 + race->access = required_mask_perm;
740 + /* If we, a system without ACLS_NEED_MASK, received data from a
741 + * system that has masks, throw away the extraneous CLASS_OBJs. */
742 + if (saw_mask && racl->count == 4) {
743 + rsync_ace *group_obj_race = NULL, *mask_race = NULL;
746 + for (i = 0, p = racl->races; i < racl->count; i++, p++) {
747 + if (p->tag_type == SMB_ACL_MASK)
749 + else if (p->tag_type == SMB_ACL_GROUP_OBJ)
750 + group_obj_race = p;
752 + if (mask_race == NULL || group_obj_race == NULL) {
753 + rprintf(FERROR, "receive_rsync_acl: have four ACES "
754 + "and one's ACL_MASK but missing "
755 + "either it or ACL_GROUP_OBJ, "
756 + "when pruning ACL\n");
758 + /* mask off group perms with it first */
759 + group_obj_race->access &= mask_race->access;
760 + /* dump mask_race; re-slot any followers-on */
762 + if (mask_race != &racl->races[racl->count]) {
763 + *mask_race = racl->races[racl->count];
764 + saw_user_obj = False; /* force re-sort */
770 + if (!(saw_user_obj && saw_group_obj && saw_other && saw_mask))
772 + if (!(saw_user_obj && saw_group_obj && saw_other))
774 + sort_rsync_acl(racl);
777 +/* receive and build the rsync_acl_lists */
779 +void receive_acl(struct file_struct *file, int f)
781 + SMB_ACL_TYPE_T *type;
784 + if (!preserve_acls || S_ISLNK(file->mode))
787 + fname = f_name(file, NULL);
788 + for (type = &types[0];
789 + type < &types[0] + sizeof types / sizeof types[0]
790 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
792 + file_acl_index_list *fileaclidx_list =
793 + file_acl_index_lists(*type);
795 + expand_file_acl_index_list(fileaclidx_list);
797 + tag = read_byte(f);
798 + if (tag == 'A' || tag == 'a') {
799 + if (*type != SMB_ACL_TYPE_ACCESS) {
800 + rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
802 + exit_cleanup(RERR_STREAMIO);
804 + } else if (tag == 'D' || tag == 'd') {
805 + if (*type == SMB_ACL_TYPE_ACCESS) {
806 + rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
808 + exit_cleanup(RERR_STREAMIO);
811 + rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
813 + exit_cleanup(RERR_STREAMIO);
815 + if (tag == 'A' || tag == 'D') {
816 + rsync_acl racl = rsync_acl_initializer;
817 + rsync_acl_list *racl_list = rsync_acl_lists(*type);
818 + smb_acl_list *sacl_list = smb_acl_lists(*type);
819 + fileaclidx_list->fileaclidxs[fileaclidx_list->count].
820 + aclidx = racl_list->count;
821 + fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
823 + receive_rsync_acl(&racl, f);
824 + expand_rsync_acl_list(racl_list);
825 + racl_list->racls[racl_list->count++] = racl;
826 + expand_smb_acl_list(sacl_list);
827 + sacl_list->sacls[sacl_list->count++] = NULL;
829 + int index = read_int(f);
830 + rsync_acl_list *racl_list = rsync_acl_lists(*type);
831 + if ((size_t) index >= racl_list->count) {
832 + rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
834 + str_acl_type(*type),
836 + exit_cleanup(RERR_STREAMIO);
838 + fileaclidx_list->fileaclidxs[fileaclidx_list->count].
840 + fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
846 +static int file_acl_index_list_sorter(const void *f1, const void *f2)
848 + const file_acl_index *fileaclidx1 = (const file_acl_index *)f1;
849 + const file_acl_index *fileaclidx2 = (const file_acl_index *)f2;
850 + return fileaclidx1->file == fileaclidx2->file ? 0 :
851 + fileaclidx1->file < fileaclidx2->file ? -1 : 1;
854 +void sort_file_acl_index_lists()
856 + SMB_ACL_TYPE_T *type;
858 + if (!preserve_acls)
861 + for (type = &types[0];
862 + type < &types[0] + sizeof types / sizeof types[0];
864 + file_acl_index_list *fileaclidx_list =
865 + file_acl_index_lists(*type);
866 + if (!fileaclidx_list->count)
868 + qsort(fileaclidx_list->fileaclidxs, fileaclidx_list->count,
869 + sizeof fileaclidx_list->fileaclidxs[0],
870 + &file_acl_index_list_sorter);
874 +static int find_file_acl_index(const file_acl_index_list *fileaclidx_list,
875 + const struct file_struct *file) {
876 + int low = 0, high = fileaclidx_list->count;
877 + const struct file_struct *file_mid;
881 + int mid = (high + low) / 2;
882 + file_mid = fileaclidx_list->fileaclidxs[mid].file;
883 + if (file_mid == file)
884 + return fileaclidx_list->fileaclidxs[mid].aclidx;
885 + if (file_mid > file)
889 + } while (low < high);
891 + file_mid = fileaclidx_list->fileaclidxs[low].file;
892 + if (file_mid == file)
893 + return fileaclidx_list->fileaclidxs[low].aclidx;
896 + "find_file_acl_index: can't find entry for file in list\n");
897 + exit_cleanup(RERR_STREAMIO);
901 +/* for duplicating ACLs on backups when using backup_dir */
903 +int dup_acl(const char *orig, const char *bak, mode_t mode)
905 + SMB_ACL_TYPE_T *type;
908 + if (!preserve_acls)
911 + for (type = &types[0];
912 + type < &types[0] + sizeof types / sizeof types[0]
913 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(mode));
915 + SMB_ACL_T sacl_orig, sacl_bak;
916 + rsync_acl racl_orig, racl_bak;
917 + if (!(sacl_orig = sys_acl_get_file(orig, *type))) {
918 + rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s\n",
919 + orig, str_acl_type(*type), strerror(errno));
923 + if (!(sacl_bak = sys_acl_get_file(orig, *type))) {
924 + rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s. ignoring\n",
925 + bak, str_acl_type(*type), strerror(errno));
927 + /* try to forge on through */
929 + if (!unpack_smb_acl(&racl_orig, sacl_orig)) {
931 + goto out_with_sacls;
934 + if (!unpack_smb_acl(&racl_bak, sacl_bak)) {
936 + goto out_with_one_racl;
938 + if (rsync_acls_equal(&racl_orig, &racl_bak))
941 + ; /* presume they're unequal */
943 + if (*type == SMB_ACL_TYPE_DEFAULT && !racl_orig.count) {
944 + if (sys_acl_delete_def_file(bak) < 0) {
945 + rprintf(FERROR, "dup_acl: sys_acl_delete_def_file(%s): %s\n",
946 + bak, strerror(errno));
949 + } else if (sys_acl_set_file(bak, *type, sacl_bak) < 0) {
950 + rprintf(FERROR, "dup_acl: sys_acl_set_file(%s, %s): %s\n",
951 + bak, str_acl_type(*type), strerror(errno));
956 + rsync_acl_free(&racl_bak);
958 + rsync_acl_free(&racl_orig);
961 + sys_acl_free_acl(sacl_bak);
962 + /* out_with_one_sacl: */
964 + sys_acl_free_acl(sacl_orig);
969 +/* Stuff for redirecting calls to set_acl() from set_file_attrs()
970 + * for keep_backup(). */
971 +static const struct file_struct *backup_orig_file = NULL;
972 +static const char null_string[] = "";
973 +static const char *backup_orig_fname = null_string;
974 +static const char *backup_dest_fname = null_string;
975 +static SMB_ACL_T _backup_sacl[] = { NULL, NULL };
977 +void push_keep_backup_acl(const struct file_struct *file,
978 + const char *orig, const char *dest)
980 + if (preserve_acls) {
981 + SMB_ACL_TYPE_T *type;
984 + backup_orig_file = file;
985 + backup_orig_fname = orig;
986 + backup_dest_fname = dest;
988 + for (type = &types[0], sacl = &_backup_sacl[0];
989 + type < &types[0] + sizeof types / sizeof types[0];
991 + if (*type == SMB_ACL_TYPE_DEFAULT && !S_ISDIR(file->mode))
994 + if (!(*sacl = sys_acl_get_file(orig, *type))) {
995 + rprintf(FERROR, "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
996 + orig, str_acl_type(*type),
1004 +static int set_keep_backup_acl()
1006 + if (preserve_acls) {
1007 + SMB_ACL_TYPE_T *type;
1011 + for (type = &types[0], sacl = &_backup_sacl[0];
1012 + type < &types[0] + sizeof types / sizeof types[0];
1015 + if (sys_acl_set_file(backup_dest_fname,
1016 + *type, *sacl) < 0) {
1017 + rprintf(FERROR, "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
1018 + backup_dest_fname,
1019 + str_acl_type(*type),
1030 +void cleanup_keep_backup_acl()
1032 + if (preserve_acls) {
1033 + SMB_ACL_TYPE_T *type;
1036 + backup_orig_file = NULL;
1037 + backup_orig_fname = null_string;
1038 + backup_dest_fname = null_string;
1040 + for (type = &types[0], sacl = &_backup_sacl[0];
1041 + type < &types[0] + sizeof types / sizeof types[0];
1044 + sys_acl_free_acl(*sacl);
1050 +/* set ACL on rsync-ed or keep_backup-ed file */
1052 +int set_acl(const char *fname, const struct file_struct *file)
1054 + int unchanged = 1;
1055 + SMB_ACL_TYPE_T *type;
1057 + if (dry_run || !preserve_acls || S_ISLNK(file->mode))
1060 + if (file == backup_orig_file) {
1061 + if (!strcmp(fname, backup_dest_fname))
1062 + return set_keep_backup_acl();
1064 + for (type = &types[0];
1065 + type < &types[0] + sizeof types / sizeof types[0]
1066 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
1068 + SMB_ACL_T sacl_orig, *sacl_new;
1069 + rsync_acl racl_orig, *racl_new;
1070 + int aclidx = find_file_acl_index(file_acl_index_lists(*type),
1073 + racl_new = &(rsync_acl_lists(*type)->racls[aclidx]);
1074 + sacl_new = &(smb_acl_lists(*type)->sacls[aclidx]);
1075 + sacl_orig = sys_acl_get_file(fname, *type);
1077 + rprintf(FERROR, "set_acl: sys_acl_get_file(%s, %s): %s\n",
1078 + fname, str_acl_type(*type), strerror(errno));
1082 + ok = unpack_smb_acl(&racl_orig, sacl_orig);
1083 + sys_acl_free_acl(sacl_orig);
1088 + ok = rsync_acls_equal(&racl_orig, racl_new);
1089 + rsync_acl_free(&racl_orig);
1092 + if (*type == SMB_ACL_TYPE_DEFAULT && !racl_new->count) {
1093 + if (sys_acl_delete_def_file(fname) < 0) {
1094 + rprintf(FERROR, "set_acl: sys_acl_delete_def_file(%s): %s\n",
1095 + fname, strerror(errno));
1101 + if (!pack_smb_acl(sacl_new, racl_new)) {
1105 + if (sys_acl_set_file(fname, *type, *sacl_new) < 0) {
1106 + rprintf(FERROR, "set_acl: sys_acl_set_file(%s, %s): %s\n",
1107 + fname, str_acl_type(*type),
1113 + if (unchanged == 1)
1119 +/* Enumeration functions for uid mapping: */
1121 +/* Context -- one and only one. Should be cycled through once on uid
1122 + * mapping and once on gid mapping. */
1123 +static rsync_acl_list *_enum_racl_lists[] = {
1124 + &_rsync_acl_lists[0], &_rsync_acl_lists[1], NULL
1127 +static rsync_acl_list **enum_racl_list = &_enum_racl_lists[0];
1128 +static size_t enum_racl_index = 0;
1129 +static size_t enum_race_index = 0;
1131 +/* This returns the next tag_type id from the given acl for the next entry,
1132 + * or it returns 0 if there are no more tag_type ids in the acl. */
1134 +static id_t next_ace_id(SMB_ACL_TAG_T tag_type, const rsync_acl *racl)
1136 + for (; enum_race_index < racl->count; enum_race_index++) {
1137 + rsync_ace *race = &racl->races[enum_race_index];
1138 + if (race->tag_type == tag_type)
1141 + enum_race_index = 0;
1145 +static id_t next_acl_id(SMB_ACL_TAG_T tag_type, const rsync_acl_list *racl_list)
1147 + for (; enum_racl_index < racl_list->count; enum_racl_index++) {
1148 + rsync_acl *racl = &racl_list->racls[enum_racl_index];
1149 + id_t id = next_ace_id(tag_type, racl);
1153 + enum_racl_index = 0;
1157 +static id_t next_acl_list_id(SMB_ACL_TAG_T tag_type)
1159 + for (; *enum_racl_list; enum_racl_list++) {
1160 + id_t id = next_acl_id(tag_type, *enum_racl_list);
1164 + enum_racl_list = &_enum_racl_lists[0];
1168 +id_t next_acl_uid()
1170 + return next_acl_list_id(SMB_ACL_USER);
1173 +id_t next_acl_gid()
1175 + return next_acl_list_id(SMB_ACL_GROUP);
1178 +/* referring to the global context enum_entry, sets the entry's id */
1179 +static void set_acl_id(id_t id)
1181 + (*enum_racl_list)->racls[enum_racl_index].races[enum_race_index++].id = id;
1184 +void acl_uid_map(id_t uid)
1189 +void acl_gid_map(id_t gid)
1194 +#define PERMS_SPLICE(perms,newbits,where) (((perms) & ~(7 << (where))) | ((newbits) << (where)))
1196 +int default_perms_for_dir(const char *dir)
1200 + BOOL ok, saw_mask = False;
1206 + perms = ACCESSPERMS & ~orig_umask;
1207 + /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1208 + sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1209 + if (sacl == NULL) {
1210 + /* Couldn't get an ACL. Darn. */
1213 + /* ACLs are disabled. We could yell at the user to turn them on, but... */
1217 + /* We're doing a dry run, so the containing directory
1218 + * wasn't actually created. Don't worry about it. */
1221 + /* Otherwise fall through. */
1223 + rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1224 + dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1230 + ok = unpack_smb_acl(&racl, sacl);
1231 + sys_acl_free_acl(sacl);
1233 + rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1237 + /* Look at each default ACL entry and possibly modify three bits of `perms' accordingly.
1238 + * If there's "no" default ACL, there will be zero entries and the umask-based perms is unchanged. */
1239 + for (i = 0; i < racl.count; i++) {
1240 + switch (racl.races[i].tag_type) {
1241 + case SMB_ACL_USER_OBJ:
1242 + perms = PERMS_SPLICE(perms, racl.races[i].access, 6);
1244 + case SMB_ACL_GROUP_OBJ:
1246 + perms = PERMS_SPLICE(perms, racl.races[i].access, 3);
1248 + case SMB_ACL_MASK:
1250 + perms = PERMS_SPLICE(perms, racl.races[i].access, 3);
1252 + case SMB_ACL_OTHER:
1253 + perms = PERMS_SPLICE(perms, racl.races[i].access, 0);
1259 + rsync_acl_free(&racl);
1261 + rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1265 +#endif /* SUPPORT_ACLS */
1268 @@ -132,6 +132,9 @@ static int make_bak_dir(char *fullpath)
1270 do_lchown(fullpath, st.st_uid, st.st_gid);
1271 do_chmod(fullpath, st.st_mode);
1272 +#ifdef SUPPORT_ACLS
1273 + dup_acl(end, fullpath, st.st_mode);
1278 @@ -185,6 +188,10 @@ static int keep_backup(char *fname)
1279 if (!(buf = get_backup_name(fname)))
1282 +#ifdef SUPPORT_ACLS
1283 + push_keep_backup_acl(file, fname, buf);
1286 /* Check to see if this is a device file, or link */
1287 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1288 || (preserve_specials && IS_SPECIAL(file->mode))) {
1289 @@ -260,6 +267,9 @@ static int keep_backup(char *fname)
1292 set_file_attrs(buf, file, NULL, 0);
1293 +#ifdef SUPPORT_ACLS
1294 + cleanup_keep_backup_acl();
1299 --- old/configure.in
1300 +++ new/configure.in
1301 @@ -482,6 +482,11 @@ if test x"$ac_cv_func_strcasecmp" = x"no
1302 AC_CHECK_LIB(resolv, strcasecmp)
1305 +AC_CHECK_FUNCS(aclsort)
1306 +if test x"$ac_cv_func_aclsort" = x"no"; then
1307 + AC_CHECK_LIB(sec, aclsort)
1310 dnl At the moment we don't test for a broken memcmp(), because all we
1311 dnl need to do is test for equality, not comparison, and it seems that
1312 dnl every platform has a memcmp that can do at least that.
1313 @@ -738,6 +743,77 @@ AC_SUBST(OBJ_RESTORE)
1314 AC_SUBST(CC_SHOBJ_FLAG)
1315 AC_SUBST(BUILD_POPT)
1317 +AC_CHECK_HEADERS(sys/acl.h)
1318 +AC_CHECK_FUNCS(_acl __acl _facl __facl)
1319 +#################################################
1320 +# check for ACL support
1322 +AC_MSG_CHECKING(whether to support ACLs)
1323 +AC_ARG_ENABLE(acl-support,
1324 +AC_HELP_STRING([--enable-acl-support], [Include ACL support (default=no)]),
1325 +[ case "$enableval" in
1328 + case "$host_os" in
1330 + AC_MSG_RESULT(Using UnixWare ACLs)
1331 + AC_DEFINE(HAVE_UNIXWARE_ACLS, 1, [true if you have UnixWare ACLs])
1334 + AC_MSG_RESULT(Using solaris ACLs)
1335 + AC_DEFINE(HAVE_SOLARIS_ACLS, 1, [true if you have solaris ACLs])
1338 + AC_MSG_RESULT(Using HPUX ACLs)
1339 + AC_DEFINE(HAVE_HPUX_ACLS, 1, [true if you have HPUX ACLs])
1342 + AC_MSG_RESULT(Using IRIX ACLs)
1343 + AC_DEFINE(HAVE_IRIX_ACLS, 1, [true if you have IRIX ACLs])
1346 + AC_MSG_RESULT(Using AIX ACLs)
1347 + AC_DEFINE(HAVE_AIX_ACLS, 1, [true if you have AIX ACLs])
1350 + AC_MSG_RESULT(Using Tru64 ACLs)
1351 + AC_DEFINE(HAVE_TRU64_ACLS, 1, [true if you have Tru64 ACLs])
1352 + LIBS="$LIBS -lpacl"
1355 + AC_MSG_RESULT(ACLs requested -- running tests)
1356 + AC_CHECK_LIB(acl,acl_get_file)
1357 + AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1358 + AC_TRY_LINK([#include <sys/types.h>
1359 +#include <sys/acl.h>],
1360 +[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1361 +samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
1362 + if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1363 + AC_MSG_RESULT(Using posix ACLs)
1364 + AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
1365 + AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
1366 + AC_TRY_LINK([#include <sys/types.h>
1367 +#include <sys/acl.h>],
1368 +[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1369 +samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
1370 + if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1371 + AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1374 + AC_MSG_ERROR(Failed to find ACL support)
1381 + AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1384 + AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1388 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1393 @@ -966,6 +966,10 @@ static struct file_struct *send_file_nam
1394 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1397 +#ifdef SUPPORT_ACLS
1398 + if (make_acl(file, fname) < 0)
1402 if (chmod_modes && !S_ISLNK(file->mode))
1403 file->mode = tweak_mode(file->mode, chmod_modes);
1404 @@ -977,6 +981,12 @@ static struct file_struct *send_file_nam
1405 if (file->basename[0]) {
1406 flist->files[flist->count++] = file;
1407 send_file_entry(file, f);
1408 +#ifdef SUPPORT_ACLS
1409 + send_acl(file, f);
1411 + /* Cleanup unsent ACL(s). */
1412 + send_acl(file, -1);
1417 @@ -1365,6 +1375,10 @@ struct file_list *recv_file_list(int f)
1418 flags |= read_byte(f) << 8;
1419 file = receive_file_entry(flist, flags, f);
1421 +#ifdef SUPPORT_ACLS
1422 + receive_acl(file, f);
1425 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1426 stats.total_size += file->length;
1428 @@ -1387,6 +1401,10 @@ struct file_list *recv_file_list(int f)
1430 clean_flist(flist, relative_paths, 1);
1432 +#ifdef SUPPORT_ACLS
1433 + sort_file_acl_index_lists();
1437 recv_uid_list(f, flist);
1441 @@ -85,6 +85,7 @@ extern long block_size; /* "long" becaus
1442 extern int max_delete;
1443 extern int force_delete;
1444 extern int one_file_system;
1445 +extern mode_t orig_umask;
1446 extern struct stats stats;
1447 extern dev_t filesystem_dev;
1448 extern char *backup_dir;
1449 @@ -753,6 +754,7 @@ static int try_dests_non(struct file_str
1452 static int phase = 0;
1453 +static int dflt_perms;
1455 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
1456 * make sure it exists, and has the right permissions/timestamp info. For
1457 @@ -844,6 +846,10 @@ static void recv_generator(char *fname,
1460 need_fuzzy_dirlist = 1;
1461 +#ifdef SUPPORT_ACLS
1462 + if (!preserve_perms)
1463 + dflt_perms = default_perms_for_dir(dn);
1466 parent_dirname = dn;
1468 @@ -871,7 +877,8 @@ static void recv_generator(char *fname,
1469 if (!preserve_perms) {
1470 int exists = statret == 0
1471 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1472 - file->mode = dest_mode(file->mode, st.st_mode, exists);
1473 + file->mode = dest_mode(file->mode, st.st_mode, dflt_perms,
1477 if (S_ISDIR(file->mode)) {
1478 @@ -1342,6 +1349,8 @@ void generate_files(int f_out, struct fi
1479 * notice that and let us know via the redo pipe (or its closing). */
1482 + dflt_perms = (ACCESSPERMS & ~orig_umask);
1484 for (i = 0; i < flist->count; i++) {
1485 struct file_struct *file = flist->files[i];
1487 --- old/lib/sysacls.c
1488 +++ new/lib/sysacls.c
1491 + Unix SMB/CIFS implementation.
1492 + Samba system utilities for ACL support.
1493 + Copyright (C) Jeremy Allison 2000.
1495 + This program is free software; you can redistribute it and/or modify
1496 + it under the terms of the GNU General Public License as published by
1497 + the Free Software Foundation; either version 2 of the License, or
1498 + (at your option) any later version.
1500 + This program is distributed in the hope that it will be useful,
1501 + but WITHOUT ANY WARRANTY; without even the implied warranty of
1502 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1503 + GNU General Public License for more details.
1505 + You should have received a copy of the GNU General Public License
1506 + along with this program; if not, write to the Free Software
1507 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1511 +#include "sysacls.h" /****** ADDED ******/
1513 +/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
1514 +void SAFE_FREE(void *mem)
1520 +char *uidtoname(uid_t uid)
1522 + static char idbuf[12];
1523 + struct passwd *pw;
1525 + if ((pw = getpwuid(uid)) == NULL) {
1526 + slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
1529 + return pw->pw_name;
1531 +/****** EXTRAS -- END ******/
1534 + This file wraps all differing system ACL interfaces into a consistent
1535 + one based on the POSIX interface. It also returns the correct errors
1536 + for older UNIX systems that don't support ACLs.
1538 + The interfaces that each ACL implementation must support are as follows :
1540 + int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1541 + int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1542 + int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
1543 + void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1544 + SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1545 + SMB_ACL_T sys_acl_get_fd(int fd)
1546 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
1547 + int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
1548 + char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
1549 + SMB_ACL_T sys_acl_init( int count)
1550 + int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1551 + int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1552 + int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1553 + int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1554 + int sys_acl_valid( SMB_ACL_T theacl )
1555 + int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1556 + int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1557 + int sys_acl_delete_def_file(const char *path)
1559 + This next one is not POSIX complient - but we *have* to have it !
1560 + More POSIX braindamage.
1562 + int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1564 + The generic POSIX free is the following call. We split this into
1565 + several different free functions as we may need to add tag info
1566 + to structures when emulating the POSIX interface.
1568 + int sys_acl_free( void *obj_p)
1570 + The calls we actually use are :
1572 + int sys_acl_free_text(char *text) - free acl_to_text
1573 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
1574 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
1578 +#if defined(HAVE_POSIX_ACLS)
1580 +/* Identity mapping - easy. */
1582 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1584 + return acl_get_entry( the_acl, entry_id, entry_p);
1587 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1589 + return acl_get_tag_type( entry_d, tag_type_p);
1592 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1594 + return acl_get_permset( entry_d, permset_p);
1597 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1599 + return acl_get_qualifier( entry_d);
1602 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1604 + return acl_get_file( path_p, type);
1607 +SMB_ACL_T sys_acl_get_fd(int fd)
1609 + return acl_get_fd(fd);
1612 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1614 + return acl_clear_perms(permset);
1617 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1619 + return acl_add_perm(permset, perm);
1622 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1624 +#if defined(HAVE_ACL_GET_PERM_NP)
1626 + * Required for TrustedBSD-based ACL implementations where
1627 + * non-POSIX.1e functions are denoted by a _np (non-portable)
1630 + return acl_get_perm_np(permset, perm);
1632 + return acl_get_perm(permset, perm);
1636 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1638 + return acl_to_text( the_acl, plen);
1641 +SMB_ACL_T sys_acl_init( int count)
1643 + return acl_init(count);
1646 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1648 + return acl_create_entry(pacl, pentry);
1651 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1653 + return acl_set_tag_type(entry, tagtype);
1656 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1658 + return acl_set_qualifier(entry, qual);
1661 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1663 + return acl_set_permset(entry, permset);
1666 +int sys_acl_valid( SMB_ACL_T theacl )
1668 + return acl_valid(theacl);
1671 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1673 + return acl_set_file(name, acltype, theacl);
1676 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1678 + return acl_set_fd(fd, theacl);
1681 +int sys_acl_delete_def_file(const char *name)
1683 + return acl_delete_def_file(name);
1686 +int sys_acl_free_text(char *text)
1688 + return acl_free(text);
1691 +int sys_acl_free_acl(SMB_ACL_T the_acl)
1693 + return acl_free(the_acl);
1696 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
1698 + return acl_free(qual);
1701 +#elif defined(HAVE_TRU64_ACLS)
1703 + * The interface to DEC/Compaq Tru64 UNIX ACLs
1704 + * is based on Draft 13 of the POSIX spec which is
1705 + * slightly different from the Draft 16 interface.
1707 + * Also, some of the permset manipulation functions
1708 + * such as acl_clear_perm() and acl_add_perm() appear
1709 + * to be broken on Tru64 so we have to manipulate
1710 + * the permission bits in the permset directly.
1712 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1714 + SMB_ACL_ENTRY_T entry;
1716 + if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
1721 + if ((entry = acl_get_entry(the_acl)) != NULL) {
1726 + return errno ? -1 : 0;
1729 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1731 + return acl_get_tag_type( entry_d, tag_type_p);
1734 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1736 + return acl_get_permset( entry_d, permset_p);
1739 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1741 + return acl_get_qualifier( entry_d);
1744 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1746 + return acl_get_file((char *)path_p, type);
1749 +SMB_ACL_T sys_acl_get_fd(int fd)
1751 + return acl_get_fd(fd, ACL_TYPE_ACCESS);
1754 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1756 + *permset = 0; /* acl_clear_perm() is broken on Tru64 */
1761 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1763 + if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
1768 + *permset |= perm; /* acl_add_perm() is broken on Tru64 */
1773 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1775 + return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
1778 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1780 + return acl_to_text( the_acl, plen);
1783 +SMB_ACL_T sys_acl_init( int count)
1785 + return acl_init(count);
1788 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1790 + SMB_ACL_ENTRY_T entry;
1792 + if ((entry = acl_create_entry(pacl)) == NULL) {
1800 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1802 + return acl_set_tag_type(entry, tagtype);
1805 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1807 + return acl_set_qualifier(entry, qual);
1810 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1812 + return acl_set_permset(entry, permset);
1815 +int sys_acl_valid( SMB_ACL_T theacl )
1817 + acl_entry_t entry;
1819 + return acl_valid(theacl, &entry);
1822 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1824 + return acl_set_file((char *)name, acltype, theacl);
1827 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1829 + return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
1832 +int sys_acl_delete_def_file(const char *name)
1834 + return acl_delete_def_file((char *)name);
1837 +int sys_acl_free_text(char *text)
1840 + * (void) cast and explicit return 0 are for DEC UNIX
1841 + * which just #defines acl_free_text() to be free()
1843 + (void) acl_free_text(text);
1847 +int sys_acl_free_acl(SMB_ACL_T the_acl)
1849 + return acl_free(the_acl);
1852 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
1854 + return acl_free_qualifier(qual, tagtype);
1857 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
1860 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
1861 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
1865 + * Note that while this code implements sufficient functionality
1866 + * to support the sys_acl_* interfaces it does not provide all
1867 + * of the semantics of the POSIX ACL interfaces.
1869 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
1870 + * from a call to sys_acl_get_entry() should not be assumed to be
1871 + * valid after calling any of the following functions, which may
1872 + * reorder the entries in the ACL.
1875 + * sys_acl_set_file()
1876 + * sys_acl_set_fd()
1880 + * The only difference between Solaris and UnixWare / OpenUNIX is
1881 + * that the #defines for the ACL operations have different names
1883 +#if defined(HAVE_UNIXWARE_ACLS)
1885 +#define SETACL ACL_SET
1886 +#define GETACL ACL_GET
1887 +#define GETACLCNT ACL_CNT
1892 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1894 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
1899 + if (entry_p == NULL) {
1904 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
1908 + if (acl_d->next < 0) {
1913 + if (acl_d->next >= acl_d->count) {
1917 + *entry_p = &acl_d->acl[acl_d->next++];
1922 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
1924 + *type_p = entry_d->a_type;
1929 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1931 + *permset_p = &entry_d->a_perm;
1936 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
1938 + if (entry_d->a_type != SMB_ACL_USER
1939 + && entry_d->a_type != SMB_ACL_GROUP) {
1944 + return &entry_d->a_id;
1948 + * There is no way of knowing what size the ACL returned by
1949 + * GETACL will be unless you first call GETACLCNT which means
1950 + * making an additional system call.
1952 + * In the hope of avoiding the cost of the additional system
1953 + * call in most cases, we initially allocate enough space for
1954 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
1955 + * be too small then we use GETACLCNT to find out the actual
1956 + * size, reallocate the ACL buffer, and then call GETACL again.
1959 +#define INITIAL_ACL_SIZE 16
1961 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
1964 + int count; /* # of ACL entries allocated */
1965 + int naccess; /* # of access ACL entries */
1966 + int ndefault; /* # of default ACL entries */
1968 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
1973 + count = INITIAL_ACL_SIZE;
1974 + if ((acl_d = sys_acl_init(count)) == NULL) {
1979 + * If there isn't enough space for the ACL entries we use
1980 + * GETACLCNT to determine the actual number of ACL entries
1981 + * reallocate and try again. This is in a loop because it
1982 + * is possible that someone else could modify the ACL and
1983 + * increase the number of entries between the call to
1984 + * GETACLCNT and the call to GETACL.
1986 + while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
1987 + && errno == ENOSPC) {
1989 + sys_acl_free_acl(acl_d);
1991 + if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
1995 + if ((acl_d = sys_acl_init(count)) == NULL) {
2001 + sys_acl_free_acl(acl_d);
2006 + * calculate the number of access and default ACL entries
2008 + * Note: we assume that the acl() system call returned a
2009 + * well formed ACL which is sorted so that all of the
2010 + * access ACL entries preceed any default ACL entries
2012 + for (naccess = 0; naccess < count; naccess++) {
2013 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2016 + ndefault = count - naccess;
2019 + * if the caller wants the default ACL we have to copy
2020 + * the entries down to the start of the acl[] buffer
2021 + * and mask out the ACL_DEFAULT flag from the type field
2023 + if (type == SMB_ACL_TYPE_DEFAULT) {
2026 + for (i = 0, j = naccess; i < ndefault; i++, j++) {
2027 + acl_d->acl[i] = acl_d->acl[j];
2028 + acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2031 + acl_d->count = ndefault;
2033 + acl_d->count = naccess;
2039 +SMB_ACL_T sys_acl_get_fd(int fd)
2042 + int count; /* # of ACL entries allocated */
2043 + int naccess; /* # of access ACL entries */
2045 + count = INITIAL_ACL_SIZE;
2046 + if ((acl_d = sys_acl_init(count)) == NULL) {
2050 + while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2051 + && errno == ENOSPC) {
2053 + sys_acl_free_acl(acl_d);
2055 + if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2059 + if ((acl_d = sys_acl_init(count)) == NULL) {
2065 + sys_acl_free_acl(acl_d);
2070 + * calculate the number of access ACL entries
2072 + for (naccess = 0; naccess < count; naccess++) {
2073 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2077 + acl_d->count = naccess;
2082 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2089 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2091 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2092 + && perm != SMB_ACL_EXECUTE) {
2097 + if (permset_d == NULL) {
2102 + *permset_d |= perm;
2107 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2109 + return *permset_d & perm;
2112 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2119 + * use an initial estimate of 20 bytes per ACL entry
2120 + * when allocating memory for the text representation
2124 + maxlen = 20 * acl_d->count;
2125 + if ((text = SMB_MALLOC(maxlen)) == NULL) {
2130 + for (i = 0; i < acl_d->count; i++) {
2131 + struct acl *ap = &acl_d->acl[i];
2132 + struct passwd *pw;
2141 + switch (ap->a_type) {
2143 + * for debugging purposes it's probably more
2144 + * useful to dump unknown tag types rather
2145 + * than just returning an error
2148 + slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2151 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2156 + case SMB_ACL_USER:
2157 + id = uidtoname(ap->a_id);
2158 + case SMB_ACL_USER_OBJ:
2162 + case SMB_ACL_GROUP:
2163 + if ((gr = getgrgid(ap->a_id)) == NULL) {
2164 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2170 + case SMB_ACL_GROUP_OBJ:
2174 + case SMB_ACL_OTHER:
2178 + case SMB_ACL_MASK:
2184 + perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2185 + perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2186 + perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2189 + /* <tag> : <qualifier> : rwx \n \0 */
2190 + nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2193 + * If this entry would overflow the buffer
2194 + * allocate enough additional memory for this
2195 + * entry and an estimate of another 20 bytes
2196 + * for each entry still to be processed
2198 + if ((len + nbytes) > maxlen) {
2199 + char *oldtext = text;
2201 + maxlen += nbytes + 20 * (acl_d->count - i);
2203 + if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2204 + SAFE_FREE(oldtext);
2210 + slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2211 + len += nbytes - 1;
2220 +SMB_ACL_T sys_acl_init(int count)
2230 + * note that since the definition of the structure pointed
2231 + * to by the SMB_ACL_T includes the first element of the
2232 + * acl[] array, this actually allocates an ACL with room
2233 + * for (count+1) entries
2235 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2240 + a->size = count + 1;
2248 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2251 + SMB_ACL_ENTRY_T entry_d;
2253 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2258 + if (acl_d->count >= acl_d->size) {
2263 + entry_d = &acl_d->acl[acl_d->count++];
2264 + entry_d->a_type = 0;
2265 + entry_d->a_id = -1;
2266 + entry_d->a_perm = 0;
2267 + *entry_p = entry_d;
2272 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2274 + switch (tag_type) {
2275 + case SMB_ACL_USER:
2276 + case SMB_ACL_USER_OBJ:
2277 + case SMB_ACL_GROUP:
2278 + case SMB_ACL_GROUP_OBJ:
2279 + case SMB_ACL_OTHER:
2280 + case SMB_ACL_MASK:
2281 + entry_d->a_type = tag_type;
2291 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2293 + if (entry_d->a_type != SMB_ACL_GROUP
2294 + && entry_d->a_type != SMB_ACL_USER) {
2299 + entry_d->a_id = *((id_t *)qual_p);
2304 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2306 + if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2310 + entry_d->a_perm = *permset_d;
2316 + * sort the ACL and check it for validity
2318 + * if it's a minimal ACL with only 4 entries then we
2319 + * need to recalculate the mask permissions to make
2320 + * sure that they are the same as the GROUP_OBJ
2321 + * permissions as required by the UnixWare acl() system call.
2323 + * (note: since POSIX allows minimal ACLs which only contain
2324 + * 3 entries - ie there is no mask entry - we should, in theory,
2325 + * check for this and add a mask entry if necessary - however
2326 + * we "know" that the caller of this interface always specifies
2327 + * a mask so, in practice "this never happens" (tm) - if it *does*
2328 + * happen aclsort() will fail and return an error and someone will
2329 + * have to fix it ...)
2332 +static int acl_sort(SMB_ACL_T acl_d)
2334 + int fixmask = (acl_d->count <= 4);
2336 + if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
2343 +int sys_acl_valid(SMB_ACL_T acl_d)
2345 + return acl_sort(acl_d);
2348 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
2351 + struct acl *acl_p;
2353 + struct acl *acl_buf = NULL;
2356 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2361 + if (acl_sort(acl_d) != 0) {
2365 + acl_p = &acl_d->acl[0];
2366 + acl_count = acl_d->count;
2369 + * if it's a directory there is extra work to do
2370 + * since the acl() system call will replace both
2371 + * the access ACLs and the default ACLs (if any)
2373 + if (stat(name, &s) != 0) {
2376 + if (S_ISDIR(s.st_mode)) {
2377 + SMB_ACL_T acc_acl;
2378 + SMB_ACL_T def_acl;
2379 + SMB_ACL_T tmp_acl;
2382 + if (type == SMB_ACL_TYPE_ACCESS) {
2384 + def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
2388 + acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
2391 + if (tmp_acl == NULL) {
2396 + * allocate a temporary buffer for the complete ACL
2398 + acl_count = acc_acl->count + def_acl->count;
2399 + acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
2401 + if (acl_buf == NULL) {
2402 + sys_acl_free_acl(tmp_acl);
2408 + * copy the access control and default entries into the buffer
2410 + memcpy(&acl_buf[0], &acc_acl->acl[0],
2411 + acc_acl->count * sizeof(acl_buf[0]));
2413 + memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
2414 + def_acl->count * sizeof(acl_buf[0]));
2417 + * set the ACL_DEFAULT flag on the default entries
2419 + for (i = acc_acl->count; i < acl_count; i++) {
2420 + acl_buf[i].a_type |= ACL_DEFAULT;
2423 + sys_acl_free_acl(tmp_acl);
2425 + } else if (type != SMB_ACL_TYPE_ACCESS) {
2430 + ret = acl(name, SETACL, acl_count, acl_p);
2432 + SAFE_FREE(acl_buf);
2437 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
2439 + if (acl_sort(acl_d) != 0) {
2443 + return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
2446 +int sys_acl_delete_def_file(const char *path)
2452 + * fetching the access ACL and rewriting it has
2453 + * the effect of deleting the default ACL
2455 + if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
2459 + ret = acl(path, SETACL, acl_d->count, acl_d->acl);
2461 + sys_acl_free_acl(acl_d);
2466 +int sys_acl_free_text(char *text)
2472 +int sys_acl_free_acl(SMB_ACL_T acl_d)
2478 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2483 +#elif defined(HAVE_HPUX_ACLS)
2487 + * Based on the Solaris/SCO code - with modifications.
2491 + * Note that while this code implements sufficient functionality
2492 + * to support the sys_acl_* interfaces it does not provide all
2493 + * of the semantics of the POSIX ACL interfaces.
2495 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2496 + * from a call to sys_acl_get_entry() should not be assumed to be
2497 + * valid after calling any of the following functions, which may
2498 + * reorder the entries in the ACL.
2501 + * sys_acl_set_file()
2502 + * sys_acl_set_fd()
2505 +/* This checks if the POSIX ACL system call is defined */
2506 +/* which basically corresponds to whether JFS 3.3 or */
2507 +/* higher is installed. If acl() was called when it */
2508 +/* isn't defined, it causes the process to core dump */
2509 +/* so it is important to check this and avoid acl() */
2510 +/* calls if it isn't there. */
2512 +static BOOL hpux_acl_call_presence(void)
2515 + shl_t handle = NULL;
2518 + static BOOL already_checked=0;
2520 + if(already_checked)
2524 + ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
2526 + if(ret_val != 0) {
2527 + DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
2528 + ret_val, errno, strerror(errno)));
2529 + DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
2533 + DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
2535 + already_checked = True;
2539 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2541 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2546 + if (entry_p == NULL) {
2551 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
2555 + if (acl_d->next < 0) {
2560 + if (acl_d->next >= acl_d->count) {
2564 + *entry_p = &acl_d->acl[acl_d->next++];
2569 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2571 + *type_p = entry_d->a_type;
2576 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2578 + *permset_p = &entry_d->a_perm;
2583 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2585 + if (entry_d->a_type != SMB_ACL_USER
2586 + && entry_d->a_type != SMB_ACL_GROUP) {
2591 + return &entry_d->a_id;
2595 + * There is no way of knowing what size the ACL returned by
2596 + * ACL_GET will be unless you first call ACL_CNT which means
2597 + * making an additional system call.
2599 + * In the hope of avoiding the cost of the additional system
2600 + * call in most cases, we initially allocate enough space for
2601 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2602 + * be too small then we use ACL_CNT to find out the actual
2603 + * size, reallocate the ACL buffer, and then call ACL_GET again.
2606 +#define INITIAL_ACL_SIZE 16
2608 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2611 + int count; /* # of ACL entries allocated */
2612 + int naccess; /* # of access ACL entries */
2613 + int ndefault; /* # of default ACL entries */
2615 + if(hpux_acl_call_presence() == False) {
2616 + /* Looks like we don't have the acl() system call on HPUX.
2617 + * May be the system doesn't have the latest version of JFS.
2622 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2627 + count = INITIAL_ACL_SIZE;
2628 + if ((acl_d = sys_acl_init(count)) == NULL) {
2633 + * If there isn't enough space for the ACL entries we use
2634 + * ACL_CNT to determine the actual number of ACL entries
2635 + * reallocate and try again. This is in a loop because it
2636 + * is possible that someone else could modify the ACL and
2637 + * increase the number of entries between the call to
2638 + * ACL_CNT and the call to ACL_GET.
2640 + while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
2642 + sys_acl_free_acl(acl_d);
2644 + if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
2648 + if ((acl_d = sys_acl_init(count)) == NULL) {
2654 + sys_acl_free_acl(acl_d);
2659 + * calculate the number of access and default ACL entries
2661 + * Note: we assume that the acl() system call returned a
2662 + * well formed ACL which is sorted so that all of the
2663 + * access ACL entries preceed any default ACL entries
2665 + for (naccess = 0; naccess < count; naccess++) {
2666 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2669 + ndefault = count - naccess;
2672 + * if the caller wants the default ACL we have to copy
2673 + * the entries down to the start of the acl[] buffer
2674 + * and mask out the ACL_DEFAULT flag from the type field
2676 + if (type == SMB_ACL_TYPE_DEFAULT) {
2679 + for (i = 0, j = naccess; i < ndefault; i++, j++) {
2680 + acl_d->acl[i] = acl_d->acl[j];
2681 + acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2684 + acl_d->count = ndefault;
2686 + acl_d->count = naccess;
2692 +SMB_ACL_T sys_acl_get_fd(int fd)
2695 + * HPUX doesn't have the facl call. Fake it using the path.... JRA.
2698 + files_struct *fsp = file_find_fd(fd);
2700 + if (fsp == NULL) {
2706 + * We know we're in the same conn context. So we
2707 + * can use the relative path.
2710 + return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
2713 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2720 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2722 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2723 + && perm != SMB_ACL_EXECUTE) {
2728 + if (permset_d == NULL) {
2733 + *permset_d |= perm;
2738 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2740 + return *permset_d & perm;
2743 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2750 + * use an initial estimate of 20 bytes per ACL entry
2751 + * when allocating memory for the text representation
2755 + maxlen = 20 * acl_d->count;
2756 + if ((text = SMB_MALLOC(maxlen)) == NULL) {
2761 + for (i = 0; i < acl_d->count; i++) {
2762 + struct acl *ap = &acl_d->acl[i];
2763 + struct passwd *pw;
2772 + switch (ap->a_type) {
2774 + * for debugging purposes it's probably more
2775 + * useful to dump unknown tag types rather
2776 + * than just returning an error
2779 + slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2782 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2787 + case SMB_ACL_USER:
2788 + id = uidtoname(ap->a_id);
2789 + case SMB_ACL_USER_OBJ:
2793 + case SMB_ACL_GROUP:
2794 + if ((gr = getgrgid(ap->a_id)) == NULL) {
2795 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2801 + case SMB_ACL_GROUP_OBJ:
2805 + case SMB_ACL_OTHER:
2809 + case SMB_ACL_MASK:
2815 + perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2816 + perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2817 + perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2820 + /* <tag> : <qualifier> : rwx \n \0 */
2821 + nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2824 + * If this entry would overflow the buffer
2825 + * allocate enough additional memory for this
2826 + * entry and an estimate of another 20 bytes
2827 + * for each entry still to be processed
2829 + if ((len + nbytes) > maxlen) {
2830 + char *oldtext = text;
2832 + maxlen += nbytes + 20 * (acl_d->count - i);
2834 + if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2841 + slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2842 + len += nbytes - 1;
2851 +SMB_ACL_T sys_acl_init(int count)
2861 + * note that since the definition of the structure pointed
2862 + * to by the SMB_ACL_T includes the first element of the
2863 + * acl[] array, this actually allocates an ACL with room
2864 + * for (count+1) entries
2866 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2871 + a->size = count + 1;
2879 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2882 + SMB_ACL_ENTRY_T entry_d;
2884 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2889 + if (acl_d->count >= acl_d->size) {
2894 + entry_d = &acl_d->acl[acl_d->count++];
2895 + entry_d->a_type = 0;
2896 + entry_d->a_id = -1;
2897 + entry_d->a_perm = 0;
2898 + *entry_p = entry_d;
2903 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2905 + switch (tag_type) {
2906 + case SMB_ACL_USER:
2907 + case SMB_ACL_USER_OBJ:
2908 + case SMB_ACL_GROUP:
2909 + case SMB_ACL_GROUP_OBJ:
2910 + case SMB_ACL_OTHER:
2911 + case SMB_ACL_MASK:
2912 + entry_d->a_type = tag_type;
2922 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2924 + if (entry_d->a_type != SMB_ACL_GROUP
2925 + && entry_d->a_type != SMB_ACL_USER) {
2930 + entry_d->a_id = *((id_t *)qual_p);
2935 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2937 + if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2941 + entry_d->a_perm = *permset_d;
2946 +/* Structure to capture the count for each type of ACE. */
2948 +struct hpux_acl_types {
2952 + int n_def_user_obj;
2957 + int n_def_group_obj;
2961 + int n_def_other_obj;
2964 + int n_def_class_obj;
2966 + int n_illegal_obj;
2970 + * Counts the different number of objects in a given array of ACL
2974 + * acl_count - Count of ACLs in the array of ACL strucutres.
2975 + * aclp - Array of ACL structures.
2976 + * acl_type_count - Pointer to acl_types structure. Should already be
2980 + * acl_type_count - This structure is filled up with counts of various
2984 +static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
2988 + memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
2990 + for(i=0;i<acl_count;i++) {
2991 + switch(aclp[i].a_type) {
2993 + acl_type_count->n_user++;
2996 + acl_type_count->n_user_obj++;
2998 + case DEF_USER_OBJ:
2999 + acl_type_count->n_def_user_obj++;
3002 + acl_type_count->n_group++;
3005 + acl_type_count->n_group_obj++;
3007 + case DEF_GROUP_OBJ:
3008 + acl_type_count->n_def_group_obj++;
3011 + acl_type_count->n_other_obj++;
3013 + case DEF_OTHER_OBJ:
3014 + acl_type_count->n_def_other_obj++;
3017 + acl_type_count->n_class_obj++;
3019 + case DEF_CLASS_OBJ:
3020 + acl_type_count->n_def_class_obj++;
3023 + acl_type_count->n_def_user++;
3026 + acl_type_count->n_def_group++;
3029 + acl_type_count->n_illegal_obj++;
3035 +/* swap_acl_entries: Swaps two ACL entries.
3037 + * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3040 +static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3042 + struct acl temp_acl;
3044 + temp_acl.a_type = aclp0->a_type;
3045 + temp_acl.a_id = aclp0->a_id;
3046 + temp_acl.a_perm = aclp0->a_perm;
3048 + aclp0->a_type = aclp1->a_type;
3049 + aclp0->a_id = aclp1->a_id;
3050 + aclp0->a_perm = aclp1->a_perm;
3052 + aclp1->a_type = temp_acl.a_type;
3053 + aclp1->a_id = temp_acl.a_id;
3054 + aclp1->a_perm = temp_acl.a_perm;
3057 +/* prohibited_duplicate_type
3058 + * Identifies if given ACL type can have duplicate entries or
3061 + * Inputs: acl_type - ACL Type.
3067 + * True - If the ACL type matches any of the prohibited types.
3068 + * False - If the ACL type doesn't match any of the prohibited types.
3071 +static BOOL hpux_prohibited_duplicate_type(int acl_type)
3073 + switch(acl_type) {
3084 +/* get_needed_class_perm
3085 + * Returns the permissions of a ACL structure only if the ACL
3086 + * type matches one of the pre-determined types for computing
3087 + * CLASS_OBJ permissions.
3089 + * Inputs: aclp - Pointer to ACL structure.
3092 +static int hpux_get_needed_class_perm(struct acl *aclp)
3094 + switch(aclp->a_type) {
3098 + case DEF_USER_OBJ:
3100 + case DEF_GROUP_OBJ:
3102 + case DEF_CLASS_OBJ:
3103 + case DEF_OTHER_OBJ:
3104 + return aclp->a_perm;
3110 +/* acl_sort for HPUX.
3111 + * Sorts the array of ACL structures as per the description in
3112 + * aclsort man page. Refer to aclsort man page for more details
3116 + * acl_count - Count of ACLs in the array of ACL structures.
3117 + * calclass - If this is not zero, then we compute the CLASS_OBJ
3119 + * aclp - Array of ACL structures.
3123 + * aclp - Sorted array of ACL structures.
3127 + * Returns 0 for success -1 for failure. Prints a message to the Samba
3128 + * debug log in case of failure.
3131 +static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3133 +#if !defined(HAVE_HPUX_ACLSORT)
3135 + * The aclsort() system call is availabe on the latest HPUX General
3136 + * Patch Bundles. So for HPUX, we developed our version of acl_sort
3137 + * function. Because, we don't want to update to a new
3138 + * HPUX GR bundle just for aclsort() call.
3141 + struct hpux_acl_types acl_obj_count;
3142 + int n_class_obj_perm = 0;
3146 + DEBUG(10,("Zero acl count passed. Returning Success\n"));
3150 + if(aclp == NULL) {
3151 + DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3155 + /* Count different types of ACLs in the ACLs array */
3157 + hpux_count_obj(acl_count, aclp, &acl_obj_count);
3159 + /* There should be only one entry each of type USER_OBJ, GROUP_OBJ,
3160 + * CLASS_OBJ and OTHER_OBJ
3163 + if( (acl_obj_count.n_user_obj != 1) ||
3164 + (acl_obj_count.n_group_obj != 1) ||
3165 + (acl_obj_count.n_class_obj != 1) ||
3166 + (acl_obj_count.n_other_obj != 1)
3168 + DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3169 +USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3173 + /* If any of the default objects are present, there should be only
3174 + * one of them each.
3177 + if( (acl_obj_count.n_def_user_obj > 1) || (acl_obj_count.n_def_group_obj > 1) ||
3178 + (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
3179 + DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3180 +or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3184 + /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl
3187 + * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3188 + * same ACL type, sort by ACL id.
3190 + * I am using the trival kind of sorting method here because, performance isn't
3191 + * really effected by the ACLs feature. More over there aren't going to be more
3192 + * than 17 entries on HPUX.
3195 + for(i=0; i<acl_count;i++) {
3196 + for (j=i+1; j<acl_count; j++) {
3197 + if( aclp[i].a_type > aclp[j].a_type ) {
3198 + /* ACL entries out of order, swap them */
3200 + hpux_swap_acl_entries((aclp+i), (aclp+j));
3202 + } else if ( aclp[i].a_type == aclp[j].a_type ) {
3204 + /* ACL entries of same type, sort by id */
3206 + if(aclp[i].a_id > aclp[j].a_id) {
3207 + hpux_swap_acl_entries((aclp+i), (aclp+j));
3208 + } else if (aclp[i].a_id == aclp[j].a_id) {
3209 + /* We have a duplicate entry. */
3210 + if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
3211 + DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3212 + aclp[i].a_type, aclp[i].a_id));
3221 + /* set the class obj permissions to the computed one. */
3223 + int n_class_obj_index = -1;
3225 + for(i=0;i<acl_count;i++) {
3226 + n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3228 + if(aclp[i].a_type == CLASS_OBJ)
3229 + n_class_obj_index = i;
3231 + aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3236 + return aclsort(acl_count, calclass, aclp);
3241 + * sort the ACL and check it for validity
3243 + * if it's a minimal ACL with only 4 entries then we
3244 + * need to recalculate the mask permissions to make
3245 + * sure that they are the same as the GROUP_OBJ
3246 + * permissions as required by the UnixWare acl() system call.
3248 + * (note: since POSIX allows minimal ACLs which only contain
3249 + * 3 entries - ie there is no mask entry - we should, in theory,
3250 + * check for this and add a mask entry if necessary - however
3251 + * we "know" that the caller of this interface always specifies
3252 + * a mask so, in practice "this never happens" (tm) - if it *does*
3253 + * happen aclsort() will fail and return an error and someone will
3254 + * have to fix it ...)
3257 +static int acl_sort(SMB_ACL_T acl_d)
3259 + int fixmask = (acl_d->count <= 4);
3261 + if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
3268 +int sys_acl_valid(SMB_ACL_T acl_d)
3270 + return acl_sort(acl_d);
3273 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3276 + struct acl *acl_p;
3278 + struct acl *acl_buf = NULL;
3281 + if(hpux_acl_call_presence() == False) {
3282 + /* Looks like we don't have the acl() system call on HPUX.
3283 + * May be the system doesn't have the latest version of JFS.
3289 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3294 + if (acl_sort(acl_d) != 0) {
3298 + acl_p = &acl_d->acl[0];
3299 + acl_count = acl_d->count;
3302 + * if it's a directory there is extra work to do
3303 + * since the acl() system call will replace both
3304 + * the access ACLs and the default ACLs (if any)
3306 + if (stat(name, &s) != 0) {
3309 + if (S_ISDIR(s.st_mode)) {
3310 + SMB_ACL_T acc_acl;
3311 + SMB_ACL_T def_acl;
3312 + SMB_ACL_T tmp_acl;
3315 + if (type == SMB_ACL_TYPE_ACCESS) {
3317 + def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3321 + acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3324 + if (tmp_acl == NULL) {
3329 + * allocate a temporary buffer for the complete ACL
3331 + acl_count = acc_acl->count + def_acl->count;
3332 + acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
3334 + if (acl_buf == NULL) {
3335 + sys_acl_free_acl(tmp_acl);
3341 + * copy the access control and default entries into the buffer
3343 + memcpy(&acl_buf[0], &acc_acl->acl[0],
3344 + acc_acl->count * sizeof(acl_buf[0]));
3346 + memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3347 + def_acl->count * sizeof(acl_buf[0]));
3350 + * set the ACL_DEFAULT flag on the default entries
3352 + for (i = acc_acl->count; i < acl_count; i++) {
3353 + acl_buf[i].a_type |= ACL_DEFAULT;
3356 + sys_acl_free_acl(tmp_acl);
3358 + } else if (type != SMB_ACL_TYPE_ACCESS) {
3363 + ret = acl(name, ACL_SET, acl_count, acl_p);
3372 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3375 + * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3378 + files_struct *fsp = file_find_fd(fd);
3380 + if (fsp == NULL) {
3385 + if (acl_sort(acl_d) != 0) {
3390 + * We know we're in the same conn context. So we
3391 + * can use the relative path.
3394 + return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
3397 +int sys_acl_delete_def_file(const char *path)
3403 + * fetching the access ACL and rewriting it has
3404 + * the effect of deleting the default ACL
3406 + if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3410 + ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
3412 + sys_acl_free_acl(acl_d);
3417 +int sys_acl_free_text(char *text)
3423 +int sys_acl_free_acl(SMB_ACL_T acl_d)
3429 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3434 +#elif defined(HAVE_IRIX_ACLS)
3436 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3438 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3443 + if (entry_p == NULL) {
3448 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
3452 + if (acl_d->next < 0) {
3457 + if (acl_d->next >= acl_d->aclp->acl_cnt) {
3461 + *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
3466 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
3468 + *type_p = entry_d->ae_tag;
3473 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3475 + *permset_p = entry_d;
3480 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3482 + if (entry_d->ae_tag != SMB_ACL_USER
3483 + && entry_d->ae_tag != SMB_ACL_GROUP) {
3488 + return &entry_d->ae_id;
3491 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3495 + if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3499 + if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
3504 + a->freeaclp = True;
3508 +SMB_ACL_T sys_acl_get_fd(int fd)
3512 + if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3516 + if ((a->aclp = acl_get_fd(fd)) == NULL) {
3521 + a->freeaclp = True;
3525 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3527 + permset_d->ae_perm = 0;
3532 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3534 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3535 + && perm != SMB_ACL_EXECUTE) {
3540 + if (permset_d == NULL) {
3545 + permset_d->ae_perm |= perm;
3550 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3552 + return permset_d->ae_perm & perm;
3555 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3557 + return acl_to_text(acl_d->aclp, len_p);
3560 +SMB_ACL_T sys_acl_init(int count)
3569 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
3575 + a->freeaclp = False;
3576 + a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
3577 + a->aclp->acl_cnt = 0;
3583 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3586 + SMB_ACL_ENTRY_T entry_d;
3588 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3593 + if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
3598 + entry_d = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
3599 + entry_d->ae_tag = 0;
3600 + entry_d->ae_id = 0;
3601 + entry_d->ae_perm = 0;
3602 + *entry_p = entry_d;
3607 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3609 + switch (tag_type) {
3610 + case SMB_ACL_USER:
3611 + case SMB_ACL_USER_OBJ:
3612 + case SMB_ACL_GROUP:
3613 + case SMB_ACL_GROUP_OBJ:
3614 + case SMB_ACL_OTHER:
3615 + case SMB_ACL_MASK:
3616 + entry_d->ae_tag = tag_type;
3626 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3628 + if (entry_d->ae_tag != SMB_ACL_GROUP
3629 + && entry_d->ae_tag != SMB_ACL_USER) {
3634 + entry_d->ae_id = *((id_t *)qual_p);
3639 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3641 + if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3645 + entry_d->ae_perm = permset_d->ae_perm;
3650 +int sys_acl_valid(SMB_ACL_T acl_d)
3652 + return acl_valid(acl_d->aclp);
3655 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3657 + return acl_set_file(name, type, acl_d->aclp);
3660 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3662 + return acl_set_fd(fd, acl_d->aclp);
3665 +int sys_acl_delete_def_file(const char *name)
3667 + return acl_delete_def_file(name);
3670 +int sys_acl_free_text(char *text)
3672 + return acl_free(text);
3675 +int sys_acl_free_acl(SMB_ACL_T acl_d)
3677 + if (acl_d->freeaclp) {
3678 + acl_free(acl_d->aclp);
3684 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3689 +#elif defined(HAVE_AIX_ACLS)
3691 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
3693 +int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3695 + struct acl_entry_link *link;
3696 + struct new_acl_entry *entry;
3699 + DEBUG(10,("This is the count: %d\n",theacl->count));
3701 + /* Check if count was previously set to -1. *
3702 + * If it was, that means we reached the end *
3703 + * of the acl last time. */
3704 + if(theacl->count == -1)
3708 + /* To get to the next acl, traverse linked list until index *
3709 + * of acl matches the count we are keeping. This count is *
3710 + * incremented each time we return an acl entry. */
3712 + for(keep_going = 0; keep_going < theacl->count; keep_going++)
3713 + link = link->nextp;
3715 + entry = *entry_p = link->entryp;
3717 + DEBUG(10,("*entry_p is %d\n",entry_p));
3718 + DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
3720 + /* Increment count */
3722 + if(link->nextp == NULL)
3723 + theacl->count = -1;
3728 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
3730 + /* Initialize tag type */
3733 + DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
3735 + /* Depending on what type of entry we have, *
3736 + * return tag type. */
3737 + switch(entry_d->ace_id->id_type) {
3739 + *tag_type_p = SMB_ACL_USER;
3742 + *tag_type_p = SMB_ACL_GROUP;
3745 + case SMB_ACL_USER_OBJ:
3746 + case SMB_ACL_GROUP_OBJ:
3747 + case SMB_ACL_OTHER:
3748 + *tag_type_p = entry_d->ace_id->id_type;
3758 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3760 + DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
3761 + *permset_p = &entry_d->ace_access;
3762 + DEBUG(10,("**permset_p is %d\n",**permset_p));
3763 + if(!(**permset_p & S_IXUSR) &&
3764 + !(**permset_p & S_IWUSR) &&
3765 + !(**permset_p & S_IRUSR) &&
3766 + (**permset_p != 0))
3769 + DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
3773 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
3775 + return(entry_d->ace_id->id_data);
3778 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
3780 + struct acl *file_acl = (struct acl *)NULL;
3781 + struct acl_entry *acl_entry;
3782 + struct new_acl_entry *new_acl_entry;
3783 + struct ace_id *idp;
3784 + struct acl_entry_link *acl_entry_link;
3785 + struct acl_entry_link *acl_entry_link_head;
3790 + /* AIX has no DEFAULT */
3791 + if ( type == SMB_ACL_TYPE_DEFAULT )
3794 + /* Get the acl using statacl */
3796 + DEBUG(10,("Entering sys_acl_get_file\n"));
3797 + DEBUG(10,("path_p is %s\n",path_p));
3799 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
3801 + if(file_acl == NULL) {
3803 + DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
3807 + memset(file_acl,0,BUFSIZ);
3809 + rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
3811 + DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
3812 + SAFE_FREE(file_acl);
3816 + DEBUG(10,("Got facl and returned it\n"));
3818 + /* Point to the first acl entry in the acl */
3819 + acl_entry = file_acl->acl_ext;
3821 + /* Begin setting up the head of the linked list *
3822 + * that will be used for the storing the acl *
3823 + * in a way that is useful for the posix_acls.c *
3826 + acl_entry_link_head = acl_entry_link = sys_acl_init(0);
3827 + if(acl_entry_link_head == NULL)
3830 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3831 + if(acl_entry_link->entryp == NULL) {
3832 + SAFE_FREE(file_acl);
3834 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3838 + DEBUG(10,("acl_entry is %d\n",acl_entry));
3839 + DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
3841 + /* Check if the extended acl bit is on. *
3842 + * If it isn't, do not show the *
3843 + * contents of the acl since AIX intends *
3844 + * the extended info to remain unused */
3846 + if(file_acl->acl_mode & S_IXACL){
3847 + /* while we are not pointing to the very end */
3848 + while(acl_entry < acl_last(file_acl)) {
3849 + /* before we malloc anything, make sure this is */
3850 + /* a valid acl entry and one that we want to map */
3851 + idp = id_nxt(acl_entry->ace_id);
3852 + if((acl_entry->ace_type == ACC_SPECIFY ||
3853 + (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
3854 + acl_entry = acl_nxt(acl_entry);
3858 + idp = acl_entry->ace_id;
3860 + /* Check if this is the first entry in the linked list. *
3861 + * The first entry needs to keep prevp pointing to NULL *
3862 + * and already has entryp allocated. */
3864 + if(acl_entry_link_head->count != 0) {
3865 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
3867 + if(acl_entry_link->nextp == NULL) {
3868 + SAFE_FREE(file_acl);
3870 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3874 + acl_entry_link->nextp->prevp = acl_entry_link;
3875 + acl_entry_link = acl_entry_link->nextp;
3876 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3877 + if(acl_entry_link->entryp == NULL) {
3878 + SAFE_FREE(file_acl);
3880 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3883 + acl_entry_link->nextp = NULL;
3886 + acl_entry_link->entryp->ace_len = acl_entry->ace_len;
3888 + /* Don't really need this since all types are going *
3889 + * to be specified but, it's better than leaving it 0 */
3891 + acl_entry_link->entryp->ace_type = acl_entry->ace_type;
3893 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3895 + memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
3897 + /* The access in the acl entries must be left shifted by *
3898 + * three bites, because they will ultimately be compared *
3899 + * to S_IRUSR, S_IWUSR, and S_IXUSR. */
3901 + switch(acl_entry->ace_type){
3904 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3905 + acl_entry_link->entryp->ace_access <<= 6;
3906 + acl_entry_link_head->count++;
3909 + /* Since there is no way to return a DENY acl entry *
3910 + * change to PERMIT and then shift. */
3911 + DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
3912 + acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
3913 + DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
3914 + acl_entry_link->entryp->ace_access <<= 6;
3915 + acl_entry_link_head->count++;
3921 + DEBUG(10,("acl_entry = %d\n",acl_entry));
3922 + DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
3924 + acl_entry = acl_nxt(acl_entry);
3926 + } /* end of if enabled */
3928 + /* Since owner, group, other acl entries are not *
3929 + * part of the acl entries in an acl, they must *
3930 + * be dummied up to become part of the list. */
3932 + for( i = 1; i < 4; i++) {
3933 + DEBUG(10,("i is %d\n",i));
3934 + if(acl_entry_link_head->count != 0) {
3935 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
3936 + if(acl_entry_link->nextp == NULL) {
3937 + SAFE_FREE(file_acl);
3939 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3943 + acl_entry_link->nextp->prevp = acl_entry_link;
3944 + acl_entry_link = acl_entry_link->nextp;
3945 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3946 + if(acl_entry_link->entryp == NULL) {
3947 + SAFE_FREE(file_acl);
3949 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3954 + acl_entry_link->nextp = NULL;
3956 + new_acl_entry = acl_entry_link->entryp;
3957 + idp = new_acl_entry->ace_id;
3959 + new_acl_entry->ace_len = sizeof(struct acl_entry);
3960 + new_acl_entry->ace_type = ACC_PERMIT;
3961 + idp->id_len = sizeof(struct ace_id);
3962 + DEBUG(10,("idp->id_len = %d\n",idp->id_len));
3963 + memset(idp->id_data,0,sizeof(uid_t));
3967 + new_acl_entry->ace_access = file_acl->g_access << 6;
3968 + idp->id_type = SMB_ACL_GROUP_OBJ;
3972 + new_acl_entry->ace_access = file_acl->o_access << 6;
3973 + idp->id_type = SMB_ACL_OTHER;
3977 + new_acl_entry->ace_access = file_acl->u_access << 6;
3978 + idp->id_type = SMB_ACL_USER_OBJ;
3986 + acl_entry_link_head->count++;
3987 + DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
3990 + acl_entry_link_head->count = 0;
3991 + SAFE_FREE(file_acl);
3993 + return(acl_entry_link_head);
3996 +SMB_ACL_T sys_acl_get_fd(int fd)
3998 + struct acl *file_acl = (struct acl *)NULL;
3999 + struct acl_entry *acl_entry;
4000 + struct new_acl_entry *new_acl_entry;
4001 + struct ace_id *idp;
4002 + struct acl_entry_link *acl_entry_link;
4003 + struct acl_entry_link *acl_entry_link_head;
4008 + /* Get the acl using fstatacl */
4010 + DEBUG(10,("Entering sys_acl_get_fd\n"));
4011 + DEBUG(10,("fd is %d\n",fd));
4012 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4014 + if(file_acl == NULL) {
4016 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4020 + memset(file_acl,0,BUFSIZ);
4022 + rc = fstatacl(fd,0,file_acl,BUFSIZ);
4024 + DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
4025 + SAFE_FREE(file_acl);
4029 + DEBUG(10,("Got facl and returned it\n"));
4031 + /* Point to the first acl entry in the acl */
4033 + acl_entry = file_acl->acl_ext;
4034 + /* Begin setting up the head of the linked list *
4035 + * that will be used for the storing the acl *
4036 + * in a way that is useful for the posix_acls.c *
4039 + acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4040 + if(acl_entry_link_head == NULL){
4041 + SAFE_FREE(file_acl);
4045 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4047 + if(acl_entry_link->entryp == NULL) {
4049 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4050 + SAFE_FREE(file_acl);
4054 + DEBUG(10,("acl_entry is %d\n",acl_entry));
4055 + DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4057 + /* Check if the extended acl bit is on. *
4058 + * If it isn't, do not show the *
4059 + * contents of the acl since AIX intends *
4060 + * the extended info to remain unused */
4062 + if(file_acl->acl_mode & S_IXACL){
4063 + /* while we are not pointing to the very end */
4064 + while(acl_entry < acl_last(file_acl)) {
4065 + /* before we malloc anything, make sure this is */
4066 + /* a valid acl entry and one that we want to map */
4068 + idp = id_nxt(acl_entry->ace_id);
4069 + if((acl_entry->ace_type == ACC_SPECIFY ||
4070 + (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4071 + acl_entry = acl_nxt(acl_entry);
4075 + idp = acl_entry->ace_id;
4077 + /* Check if this is the first entry in the linked list. *
4078 + * The first entry needs to keep prevp pointing to NULL *
4079 + * and already has entryp allocated. */
4081 + if(acl_entry_link_head->count != 0) {
4082 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4083 + if(acl_entry_link->nextp == NULL) {
4085 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4086 + SAFE_FREE(file_acl);
4089 + acl_entry_link->nextp->prevp = acl_entry_link;
4090 + acl_entry_link = acl_entry_link->nextp;
4091 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4092 + if(acl_entry_link->entryp == NULL) {
4094 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4095 + SAFE_FREE(file_acl);
4099 + acl_entry_link->nextp = NULL;
4102 + acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4104 + /* Don't really need this since all types are going *
4105 + * to be specified but, it's better than leaving it 0 */
4107 + acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4108 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4110 + memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
4112 + /* The access in the acl entries must be left shifted by *
4113 + * three bites, because they will ultimately be compared *
4114 + * to S_IRUSR, S_IWUSR, and S_IXUSR. */
4116 + switch(acl_entry->ace_type){
4119 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4120 + acl_entry_link->entryp->ace_access <<= 6;
4121 + acl_entry_link_head->count++;
4124 + /* Since there is no way to return a DENY acl entry *
4125 + * change to PERMIT and then shift. */
4126 + DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4127 + acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4128 + DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4129 + acl_entry_link->entryp->ace_access <<= 6;
4130 + acl_entry_link_head->count++;
4136 + DEBUG(10,("acl_entry = %d\n",acl_entry));
4137 + DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4139 + acl_entry = acl_nxt(acl_entry);
4141 + } /* end of if enabled */
4143 + /* Since owner, group, other acl entries are not *
4144 + * part of the acl entries in an acl, they must *
4145 + * be dummied up to become part of the list. */
4147 + for( i = 1; i < 4; i++) {
4148 + DEBUG(10,("i is %d\n",i));
4149 + if(acl_entry_link_head->count != 0){
4150 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4151 + if(acl_entry_link->nextp == NULL) {
4153 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4154 + SAFE_FREE(file_acl);
4158 + acl_entry_link->nextp->prevp = acl_entry_link;
4159 + acl_entry_link = acl_entry_link->nextp;
4160 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4162 + if(acl_entry_link->entryp == NULL) {
4163 + SAFE_FREE(file_acl);
4165 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4170 + acl_entry_link->nextp = NULL;
4172 + new_acl_entry = acl_entry_link->entryp;
4173 + idp = new_acl_entry->ace_id;
4175 + new_acl_entry->ace_len = sizeof(struct acl_entry);
4176 + new_acl_entry->ace_type = ACC_PERMIT;
4177 + idp->id_len = sizeof(struct ace_id);
4178 + DEBUG(10,("idp->id_len = %d\n",idp->id_len));
4179 + memset(idp->id_data,0,sizeof(uid_t));
4183 + new_acl_entry->ace_access = file_acl->g_access << 6;
4184 + idp->id_type = SMB_ACL_GROUP_OBJ;
4188 + new_acl_entry->ace_access = file_acl->o_access << 6;
4189 + idp->id_type = SMB_ACL_OTHER;
4193 + new_acl_entry->ace_access = file_acl->u_access << 6;
4194 + idp->id_type = SMB_ACL_USER_OBJ;
4201 + acl_entry_link_head->count++;
4202 + DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4205 + acl_entry_link_head->count = 0;
4206 + SAFE_FREE(file_acl);
4208 + return(acl_entry_link_head);
4211 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
4213 + *permset = *permset & ~0777;
4217 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4220 + (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
4224 + DEBUG(10,("This is the permset now: %d\n",*permset));
4228 +char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
4233 +SMB_ACL_T sys_acl_init( int count)
4235 + struct acl_entry_link *theacl = NULL;
4237 + DEBUG(10,("Entering sys_acl_init\n"));
4239 + theacl = SMB_MALLOC_P(struct acl_entry_link);
4240 + if(theacl == NULL) {
4242 + DEBUG(0,("Error in sys_acl_init is %d\n",errno));
4246 + theacl->count = 0;
4247 + theacl->nextp = NULL;
4248 + theacl->prevp = NULL;
4249 + theacl->entryp = NULL;
4250 + DEBUG(10,("Exiting sys_acl_init\n"));
4254 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
4256 + struct acl_entry_link *theacl;
4257 + struct acl_entry_link *acl_entryp;
4258 + struct acl_entry_link *temp_entry;
4261 + DEBUG(10,("Entering the sys_acl_create_entry\n"));
4263 + theacl = acl_entryp = *pacl;
4265 + /* Get to the end of the acl before adding entry */
4267 + for(counting=0; counting < theacl->count; counting++){
4268 + DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4269 + temp_entry = acl_entryp;
4270 + acl_entryp = acl_entryp->nextp;
4273 + if(theacl->count != 0){
4274 + temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
4275 + if(acl_entryp == NULL) {
4277 + DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4281 + DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4282 + acl_entryp->prevp = temp_entry;
4283 + DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
4286 + *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
4287 + if(*pentry == NULL) {
4289 + DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4293 + memset(*pentry,0,sizeof(struct new_acl_entry));
4294 + acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
4295 + acl_entryp->entryp->ace_type = ACC_PERMIT;
4296 + acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
4297 + acl_entryp->nextp = NULL;
4299 + DEBUG(10,("Exiting sys_acl_create_entry\n"));
4303 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
4305 + DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
4306 + entry->ace_id->id_type = tagtype;
4307 + DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
4308 + DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
4311 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
4313 + DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
4314 + memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
4315 + DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
4319 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
4321 + DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
4322 + if(!(*permset & S_IXUSR) &&
4323 + !(*permset & S_IWUSR) &&
4324 + !(*permset & S_IRUSR) &&
4328 + entry->ace_access = *permset;
4329 + DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
4330 + DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
4334 +int sys_acl_valid( SMB_ACL_T theacl )
4337 + int group_obj = 0;
4338 + int other_obj = 0;
4339 + struct acl_entry_link *acl_entry;
4341 + for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
4342 + user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
4343 + group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
4344 + other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
4347 + DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
4349 + if(user_obj != 1 || group_obj != 1 || other_obj != 1)
4355 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
4357 + struct acl_entry_link *acl_entry_link = NULL;
4358 + struct acl *file_acl = NULL;
4359 + struct acl *file_acl_temp = NULL;
4360 + struct acl_entry *acl_entry = NULL;
4361 + struct ace_id *ace_id = NULL;
4368 + DEBUG(10,("Entering sys_acl_set_file\n"));
4369 + DEBUG(10,("File name is %s\n",name));
4371 + /* AIX has no default ACL */
4372 + if(acltype == SMB_ACL_TYPE_DEFAULT)
4375 + acl_length = BUFSIZ;
4376 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4378 + if(file_acl == NULL) {
4380 + DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4384 + memset(file_acl,0,BUFSIZ);
4386 + file_acl->acl_len = ACL_SIZ;
4387 + file_acl->acl_mode = S_IXACL;
4389 + for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4390 + acl_entry_link->entryp->ace_access >>= 6;
4391 + id_type = acl_entry_link->entryp->ace_id->id_type;
4394 + case SMB_ACL_USER_OBJ:
4395 + file_acl->u_access = acl_entry_link->entryp->ace_access;
4397 + case SMB_ACL_GROUP_OBJ:
4398 + file_acl->g_access = acl_entry_link->entryp->ace_access;
4400 + case SMB_ACL_OTHER:
4401 + file_acl->o_access = acl_entry_link->entryp->ace_access;
4403 + case SMB_ACL_MASK:
4407 + if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4408 + acl_length += sizeof(struct acl_entry);
4409 + file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
4410 + if(file_acl_temp == NULL) {
4411 + SAFE_FREE(file_acl);
4413 + DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4417 + memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4418 + SAFE_FREE(file_acl);
4419 + file_acl = file_acl_temp;
4422 + acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4423 + file_acl->acl_len += sizeof(struct acl_entry);
4424 + acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4425 + acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4427 + /* In order to use this, we'll need to wait until we can get denies */
4428 + /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4429 + acl_entry->ace_type = ACC_SPECIFY; */
4431 + acl_entry->ace_type = ACC_SPECIFY;
4433 + ace_id = acl_entry->ace_id;
4435 + ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4436 + DEBUG(10,("The id type is %d\n",ace_id->id_type));
4437 + ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4438 + memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4439 + memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
4442 + rc = chacl(name,file_acl,file_acl->acl_len);
4443 + DEBUG(10,("errno is %d\n",errno));
4444 + DEBUG(10,("return code is %d\n",rc));
4445 + SAFE_FREE(file_acl);
4446 + DEBUG(10,("Exiting the sys_acl_set_file\n"));
4450 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
4452 + struct acl_entry_link *acl_entry_link = NULL;
4453 + struct acl *file_acl = NULL;
4454 + struct acl *file_acl_temp = NULL;
4455 + struct acl_entry *acl_entry = NULL;
4456 + struct ace_id *ace_id = NULL;
4462 + DEBUG(10,("Entering sys_acl_set_fd\n"));
4463 + acl_length = BUFSIZ;
4464 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4466 + if(file_acl == NULL) {
4468 + DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4472 + memset(file_acl,0,BUFSIZ);
4474 + file_acl->acl_len = ACL_SIZ;
4475 + file_acl->acl_mode = S_IXACL;
4477 + for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4478 + acl_entry_link->entryp->ace_access >>= 6;
4479 + id_type = acl_entry_link->entryp->ace_id->id_type;
4480 + DEBUG(10,("The id_type is %d\n",id_type));
4483 + case SMB_ACL_USER_OBJ:
4484 + file_acl->u_access = acl_entry_link->entryp->ace_access;
4486 + case SMB_ACL_GROUP_OBJ:
4487 + file_acl->g_access = acl_entry_link->entryp->ace_access;
4489 + case SMB_ACL_OTHER:
4490 + file_acl->o_access = acl_entry_link->entryp->ace_access;
4492 + case SMB_ACL_MASK:
4496 + if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4497 + acl_length += sizeof(struct acl_entry);
4498 + file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
4499 + if(file_acl_temp == NULL) {
4500 + SAFE_FREE(file_acl);
4502 + DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4506 + memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4507 + SAFE_FREE(file_acl);
4508 + file_acl = file_acl_temp;
4511 + acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4512 + file_acl->acl_len += sizeof(struct acl_entry);
4513 + acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4514 + acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4516 + /* In order to use this, we'll need to wait until we can get denies */
4517 + /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4518 + acl_entry->ace_type = ACC_SPECIFY; */
4520 + acl_entry->ace_type = ACC_SPECIFY;
4522 + ace_id = acl_entry->ace_id;
4524 + ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4525 + DEBUG(10,("The id type is %d\n",ace_id->id_type));
4526 + ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4527 + memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4528 + memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
4531 + rc = fchacl(fd,file_acl,file_acl->acl_len);
4532 + DEBUG(10,("errno is %d\n",errno));
4533 + DEBUG(10,("return code is %d\n",rc));
4534 + SAFE_FREE(file_acl);
4535 + DEBUG(10,("Exiting sys_acl_set_fd\n"));
4539 +int sys_acl_delete_def_file(const char *name)
4541 + /* AIX has no default ACL */
4545 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4547 + return(*permset & perm);
4550 +int sys_acl_free_text(char *text)
4555 +int sys_acl_free_acl(SMB_ACL_T posix_acl)
4557 + struct acl_entry_link *acl_entry_link;
4559 + for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
4560 + SAFE_FREE(acl_entry_link->prevp->entryp);
4561 + SAFE_FREE(acl_entry_link->prevp);
4564 + SAFE_FREE(acl_entry_link->prevp->entryp);
4565 + SAFE_FREE(acl_entry_link->prevp);
4566 + SAFE_FREE(acl_entry_link->entryp);
4567 + SAFE_FREE(acl_entry_link);
4572 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4577 +#else /* No ACLs. */
4579 +int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
4585 +int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
4591 +int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
4597 +void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
4603 +SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
4606 + return (SMB_ACL_T)NULL;
4609 +SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
4612 + return (SMB_ACL_T)NULL;
4615 +int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
4621 +int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
4627 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4630 + return (permset & perm) ? 1 : 0;
4633 +char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
4639 +int sys_acl_free_text(UNUSED(char *text))
4645 +SMB_ACL_T sys_acl_init(UNUSED(int count))
4651 +int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
4657 +int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
4663 +int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
4669 +int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
4675 +int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
4681 +int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
4687 +int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
4693 +int sys_acl_delete_def_file(UNUSED(const char *name))
4699 +int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
4705 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
4711 +#endif /* No ACLs. */
4713 +/************************************************************************
4714 + Deliberately outside the ACL defines. Return 1 if this is a "no acls"
4716 +************************************************************************/
4718 +int no_acl_syscall_error(int err)
4720 +#if defined(ENOSYS)
4721 + if (err == ENOSYS) {
4725 +#if defined(ENOTSUP)
4726 + if (err == ENOTSUP) {
4732 --- old/lib/sysacls.h
4733 +++ new/lib/sysacls.h
4735 +#define SMB_MALLOC(cnt) new_array(char, cnt)
4736 +#define SMB_MALLOC_P(obj) new_array(obj, 1)
4737 +#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
4738 +#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
4739 +#define slprintf snprintf
4741 +int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
4742 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
4743 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
4744 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
4745 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
4746 +SMB_ACL_T sys_acl_get_fd(int fd);
4747 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
4748 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4749 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4750 +char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
4751 +SMB_ACL_T sys_acl_init(int count);
4752 +int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
4753 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
4754 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
4755 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
4756 +int sys_acl_valid(SMB_ACL_T theacl);
4757 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
4758 +int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
4759 +int sys_acl_delete_def_file(const char *name);
4760 +int sys_acl_free_text(char *text);
4761 +int sys_acl_free_acl(SMB_ACL_T the_acl);
4762 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
4765 @@ -58,7 +58,7 @@ BEGIN {
4769 -!/^OFF_T|^size_t|^off_t|^pid_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
4770 +!/^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/ {
4776 @@ -45,6 +45,7 @@ int copy_dirlinks = 0;
4778 int preserve_links = 0;
4779 int preserve_hard_links = 0;
4780 +int preserve_acls = 0;
4781 int preserve_perms = 0;
4782 int preserve_executability = 0;
4783 int preserve_devices = 0;
4784 @@ -194,6 +195,7 @@ static void print_rsync_version(enum log
4785 char const *got_socketpair = "no ";
4786 char const *have_inplace = "no ";
4787 char const *hardlinks = "no ";
4788 + char const *acls = "no ";
4789 char const *links = "no ";
4790 char const *ipv6 = "no ";
4791 STRUCT_STAT *dumstat;
4792 @@ -210,6 +212,10 @@ static void print_rsync_version(enum log
4796 +#ifdef SUPPORT_ACLS
4800 #ifdef SUPPORT_LINKS
4803 @@ -223,9 +229,9 @@ static void print_rsync_version(enum log
4804 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
4805 rprintf(f, "<http://rsync.samba.org/>\n");
4806 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
4807 - "%shard links, %ssymlinks, batchfiles,\n",
4808 + "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
4809 (int) (sizeof (OFF_T) * 8),
4810 - got_socketpair, hardlinks, links);
4811 + got_socketpair, hardlinks, acls, links);
4813 /* Note that this field may not have type ino_t. It depends
4814 * on the complicated interaction between largefile feature
4815 @@ -295,6 +301,9 @@ void usage(enum logcode F)
4816 rprintf(F," -H, --hard-links preserve hard links\n");
4817 rprintf(F," -p, --perms preserve permissions\n");
4818 rprintf(F," -E, --executability preserve the file's executability\n");
4819 +#ifdef SUPPORT_ACLS
4820 + rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
4822 rprintf(F," --chmod=CHMOD change destination permissions\n");
4823 rprintf(F," -o, --owner preserve owner (super-user only)\n");
4824 rprintf(F," -g, --group preserve group\n");
4825 @@ -410,6 +419,9 @@ static struct poptOption long_options[]
4826 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
4827 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
4828 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
4829 + {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
4830 + {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4831 + {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4832 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
4833 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
4834 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
4835 @@ -1068,6 +1080,23 @@ int parse_arguments(int *argc, const cha
4840 +#ifdef SUPPORT_ACLS
4841 + preserve_acls = preserve_perms = 1;
4844 + /* FIXME: this should probably be ignored with a
4845 + * warning and then countermeasures taken to
4846 + * restrict group and other access in the presence
4847 + * of any more restrictive ACLs, but this is safe
4849 + snprintf(err_buf,sizeof(err_buf),
4850 + "ACLs are not supported on this %s\n",
4851 + am_server ? "server" : "client");
4857 /* A large opt value means that set_refuse_options()
4858 * turned this option off. */
4859 @@ -1511,6 +1540,10 @@ void server_options(char **args,int *arg
4861 if (preserve_hard_links)
4863 +#ifdef SUPPORT_ACLS
4864 + if (preserve_acls)
4865 + argstr[x++] = 'A';
4872 @@ -46,6 +46,7 @@ extern int keep_partial;
4873 extern int checksum_seed;
4875 extern int delay_updates;
4876 +extern mode_t orig_umask;
4877 extern struct stats stats;
4878 extern char *log_format;
4879 extern char *tmpdir;
4880 @@ -344,6 +345,10 @@ int recv_files(int f_in, struct file_lis
4881 int itemizing = am_daemon ? daemon_log_format_has_i
4882 : !am_server && log_format_has_i;
4883 int max_phase = protocol_version >= 29 ? 2 : 1;
4884 + int dflt_perms = (ACCESSPERMS & ~orig_umask);
4885 +#ifdef SUPPORT_ACLS
4886 + char *parent_dirname = "";
4891 @@ -541,7 +546,16 @@ int recv_files(int f_in, struct file_lis
4892 * mode based on the local permissions and some heuristics. */
4893 if (!preserve_perms) {
4894 int exists = fd1 != -1;
4895 - file->mode = dest_mode(file->mode, st.st_mode, exists);
4896 +#ifdef SUPPORT_ACLS
4897 + char *dn = file->dirname ? file->dirname : ".";
4898 + if (parent_dirname != dn
4899 + && strcmp(parent_dirname, dn) != 0) {
4900 + dflt_perms = default_perms_for_dir(dn);
4901 + parent_dirname = dn;
4904 + file->mode = dest_mode(file->mode, st.st_mode,
4905 + dflt_perms, exists);
4908 /* We now check to see if we are writing file "inplace" */
4911 @@ -101,7 +101,8 @@ void free_sums(struct sum_struct *s)
4913 /* This is only called when we aren't preserving permissions. Figure out what
4914 * the permissions should be and return them merged back into the mode. */
4915 -mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int exists)
4916 +mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int dflt_perms,
4919 /* If the file already exists, we'll return the local permissions,
4920 * possibly tweaked by the --executability option. */
4921 @@ -116,7 +117,7 @@ mode_t dest_mode(mode_t flist_mode, mode
4922 cur_mode |= (cur_mode & 0444) >> 2;
4925 - cur_mode = flist_mode & ACCESSPERMS & ~orig_umask;
4926 + cur_mode = flist_mode & ACCESSPERMS & dflt_perms;
4927 if (daemon_chmod_modes && !S_ISLNK(flist_mode))
4928 cur_mode = tweak_mode(cur_mode, daemon_chmod_modes);
4929 return (flist_mode & ~CHMOD_BITS) | (cur_mode & CHMOD_BITS);
4930 @@ -203,6 +204,13 @@ int set_file_attrs(char *fname, struct f
4934 +#ifdef SUPPORT_ACLS
4935 + /* It's OK to call set_acl() now, even for a dir, as the generator
4936 + * will enable owner-writability using chmod, if necessary. */
4937 + if (set_acl(fname, file) == 0)
4942 if ((st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
4943 int ret = do_chmod(fname, file->mode);
4946 @@ -658,6 +658,20 @@ struct chmod_mode_struct;
4948 #define UNUSED(x) x __attribute__((__unused__))
4950 +#if HAVE_POSIX_ACLS|HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|\
4951 + HAVE_HPUX_ACLS|HAVE_IRIX_ACLS|HAVE_AIX_ACLS|HAVE_TRU64_ACLS
4952 +#define SUPPORT_ACLS 1
4955 +#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
4956 +#define ACLS_NEED_MASK 1
4959 +#if defined SUPPORT_ACLS && defined HAVE_SYS_ACL_H
4960 +#include <sys/acl.h>
4962 +#include "smb_acls.h"
4966 /* We have replacement versions of these if they're missing. */
4969 @@ -321,6 +321,7 @@ to the detailed description below for a
4970 -H, --hard-links preserve hard links
4971 -p, --perms preserve permissions
4972 -E, --executability preserve executability
4973 + -A, --acls preserve ACLs (implies -p) [non-standard]
4974 --chmod=CHMOD change destination permissions
4975 -o, --owner preserve owner (super-user only)
4976 -g, --group preserve group
4977 @@ -742,7 +743,9 @@ quote(itemize(
4978 permissions, though the bf(--executability) option might change just
4979 the execute permission for the file.
4980 it() New files get their "normal" permission bits set to the source
4981 - file's permissions masked with the receiving end's umask setting, and
4982 + file's permissions masked with the receiving directory's default
4983 + permissions (either the receiving process's umask, or the permissions
4984 + specified via the destination directory's default ACL), and
4985 their special permission bits disabled except in the case where a new
4986 directory inherits a setgid bit from its parent directory.
4988 @@ -773,9 +776,11 @@ The preservation of the destination's se
4989 directories when bf(--perms) is off was added in rsync 2.6.7. Older rsync
4990 versions erroneously preserved the three special permission bits for
4991 newly-created files when bf(--perms) was off, while overriding the
4992 -destination's setgid bit setting on a newly-created directory. (Keep in
4993 -mind that it is the version of the receiving rsync that affects this
4995 +destination's setgid bit setting on a newly-created directory. Default ACL
4996 +observance was added to the ACL patch for rsync 2.6.7, so older (or
4997 +non-ACL-enabled) rsyncs use the umask even if default ACLs are present.
4998 +(Keep in mind that it is the version of the receiving rsync that affects
5001 dit(bf(-E, --executability)) This option causes rsync to preserve the
5002 executability (or non-executability) of regular files when bf(--perms) is
5003 @@ -793,6 +798,10 @@ quote(itemize(
5005 If bf(--perms) is enabled, this option is ignored.
5007 +dit(bf(-A, --acls)) This option causes rsync to update the destination
5008 +ACLs to be the same as the source ACLs. This nonstandard option only
5009 +works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
5011 dit(bf(--chmod)) This option tells rsync to apply one or more
5012 comma-separated "chmod" strings to the permission of the files in the
5013 transfer. The resulting value is treated as though it was the permissions
5018 + Unix SMB/Netbios implementation.
5020 + Portable SMB ACL interface
5021 + Copyright (C) Jeremy Allison 2000
5023 + This program is free software; you can redistribute it and/or modify
5024 + it under the terms of the GNU General Public License as published by
5025 + the Free Software Foundation; either version 2 of the License, or
5026 + (at your option) any later version.
5028 + This program is distributed in the hope that it will be useful,
5029 + but WITHOUT ANY WARRANTY; without even the implied warranty of
5030 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5031 + GNU General Public License for more details.
5033 + You should have received a copy of the GNU General Public License
5034 + along with this program; if not, write to the Free Software
5035 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
5038 +#ifndef _SMB_ACLS_H
5039 +#define _SMB_ACLS_H
5041 +#if defined HAVE_POSIX_ACLS
5043 +/* This is an identity mapping (just remove the SMB_). */
5045 +#define SMB_ACL_TAG_T acl_tag_t
5046 +#define SMB_ACL_TYPE_T acl_type_t
5047 +#define SMB_ACL_PERMSET_T acl_permset_t
5048 +#define SMB_ACL_PERM_T acl_perm_t
5049 +#define SMB_ACL_READ ACL_READ
5050 +#define SMB_ACL_WRITE ACL_WRITE
5051 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5053 +/* Types of ACLs. */
5054 +#define SMB_ACL_USER ACL_USER
5055 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5056 +#define SMB_ACL_GROUP ACL_GROUP
5057 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5058 +#define SMB_ACL_OTHER ACL_OTHER
5059 +#define SMB_ACL_MASK ACL_MASK
5061 +#define SMB_ACL_T acl_t
5063 +#define SMB_ACL_ENTRY_T acl_entry_t
5065 +#define SMB_ACL_FIRST_ENTRY ACL_FIRST_ENTRY
5066 +#define SMB_ACL_NEXT_ENTRY ACL_NEXT_ENTRY
5068 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5069 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5071 +#elif defined HAVE_TRU64_ACLS
5073 +/* This is for DEC/Compaq Tru64 UNIX */
5075 +#define SMB_ACL_TAG_T acl_tag_t
5076 +#define SMB_ACL_TYPE_T acl_type_t
5077 +#define SMB_ACL_PERMSET_T acl_permset_t
5078 +#define SMB_ACL_PERM_T acl_perm_t
5079 +#define SMB_ACL_READ ACL_READ
5080 +#define SMB_ACL_WRITE ACL_WRITE
5081 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5083 +/* Types of ACLs. */
5084 +#define SMB_ACL_USER ACL_USER
5085 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5086 +#define SMB_ACL_GROUP ACL_GROUP
5087 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5088 +#define SMB_ACL_OTHER ACL_OTHER
5089 +#define SMB_ACL_MASK ACL_MASK
5091 +#define SMB_ACL_T acl_t
5093 +#define SMB_ACL_ENTRY_T acl_entry_t
5095 +#define SMB_ACL_FIRST_ENTRY 0
5096 +#define SMB_ACL_NEXT_ENTRY 1
5098 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5099 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5101 +#elif defined HAVE_UNIXWARE_ACLS || defined HAVE_SOLARIS_ACLS
5103 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
5104 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
5107 +/* SVR4.2 ES/MP ACLs */
5108 +typedef int SMB_ACL_TAG_T;
5109 +typedef int SMB_ACL_TYPE_T;
5110 +typedef ushort *SMB_ACL_PERMSET_T;
5111 +typedef ushort SMB_ACL_PERM_T;
5112 +#define SMB_ACL_READ 4
5113 +#define SMB_ACL_WRITE 2
5114 +#define SMB_ACL_EXECUTE 1
5116 +/* Types of ACLs. */
5117 +#define SMB_ACL_USER USER
5118 +#define SMB_ACL_USER_OBJ USER_OBJ
5119 +#define SMB_ACL_GROUP GROUP
5120 +#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5121 +#define SMB_ACL_OTHER OTHER_OBJ
5122 +#define SMB_ACL_MASK CLASS_OBJ
5124 +typedef struct SMB_ACL_T {
5128 + struct acl acl[1];
5131 +typedef struct acl *SMB_ACL_ENTRY_T;
5133 +#define SMB_ACL_FIRST_ENTRY 0
5134 +#define SMB_ACL_NEXT_ENTRY 1
5136 +#define SMB_ACL_TYPE_ACCESS 0
5137 +#define SMB_ACL_TYPE_DEFAULT 1
5139 +#elif defined HAVE_HPUX_ACLS
5142 + * Based on the Solaris & UnixWare code.
5146 +#include <sys/aclv.h>
5148 +/* SVR4.2 ES/MP ACLs */
5149 +typedef int SMB_ACL_TAG_T;
5150 +typedef int SMB_ACL_TYPE_T;
5151 +typedef ushort *SMB_ACL_PERMSET_T;
5152 +typedef ushort SMB_ACL_PERM_T;
5153 +#define SMB_ACL_READ 4
5154 +#define SMB_ACL_WRITE 2
5155 +#define SMB_ACL_EXECUTE 1
5157 +/* Types of ACLs. */
5158 +#define SMB_ACL_USER USER
5159 +#define SMB_ACL_USER_OBJ USER_OBJ
5160 +#define SMB_ACL_GROUP GROUP
5161 +#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5162 +#define SMB_ACL_OTHER OTHER_OBJ
5163 +#define SMB_ACL_MASK CLASS_OBJ
5165 +typedef struct SMB_ACL_T {
5169 + struct acl acl[1];
5172 +typedef struct acl *SMB_ACL_ENTRY_T;
5174 +#define SMB_ACL_FIRST_ENTRY 0
5175 +#define SMB_ACL_NEXT_ENTRY 1
5177 +#define SMB_ACL_TYPE_ACCESS 0
5178 +#define SMB_ACL_TYPE_DEFAULT 1
5180 +#elif defined HAVE_IRIX_ACLS
5182 +#define SMB_ACL_TAG_T acl_tag_t
5183 +#define SMB_ACL_TYPE_T acl_type_t
5184 +#define SMB_ACL_PERMSET_T acl_permset_t
5185 +#define SMB_ACL_PERM_T acl_perm_t
5186 +#define SMB_ACL_READ ACL_READ
5187 +#define SMB_ACL_WRITE ACL_WRITE
5188 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5190 +/* Types of ACLs. */
5191 +#define SMB_ACL_USER ACL_USER
5192 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5193 +#define SMB_ACL_GROUP ACL_GROUP
5194 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5195 +#define SMB_ACL_OTHER ACL_OTHER_OBJ
5196 +#define SMB_ACL_MASK ACL_MASK
5198 +typedef struct SMB_ACL_T {
5204 +#define SMB_ACL_ENTRY_T acl_entry_t
5206 +#define SMB_ACL_FIRST_ENTRY 0
5207 +#define SMB_ACL_NEXT_ENTRY 1
5209 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5210 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5212 +#elif defined HAVE_AIX_ACLS
5214 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
5216 +#include "/usr/include/acl.h"
5218 +typedef uint *SMB_ACL_PERMSET_T;
5220 +struct acl_entry_link{
5221 + struct acl_entry_link *prevp;
5222 + struct new_acl_entry *entryp;
5223 + struct acl_entry_link *nextp;
5227 +struct new_acl_entry{
5228 + unsigned short ace_len;
5229 + unsigned short ace_type;
5230 + unsigned int ace_access;
5231 + struct ace_id ace_id[1];
5234 +#define SMB_ACL_ENTRY_T struct new_acl_entry*
5235 +#define SMB_ACL_T struct acl_entry_link*
5237 +#define SMB_ACL_TAG_T unsigned short
5238 +#define SMB_ACL_TYPE_T int
5239 +#define SMB_ACL_PERM_T uint
5240 +#define SMB_ACL_READ S_IRUSR
5241 +#define SMB_ACL_WRITE S_IWUSR
5242 +#define SMB_ACL_EXECUTE S_IXUSR
5244 +/* Types of ACLs. */
5245 +#define SMB_ACL_USER ACEID_USER
5246 +#define SMB_ACL_USER_OBJ 3
5247 +#define SMB_ACL_GROUP ACEID_GROUP
5248 +#define SMB_ACL_GROUP_OBJ 4
5249 +#define SMB_ACL_OTHER 5
5250 +#define SMB_ACL_MASK 6
5253 +#define SMB_ACL_FIRST_ENTRY 1
5254 +#define SMB_ACL_NEXT_ENTRY 2
5256 +#define SMB_ACL_TYPE_ACCESS 0
5257 +#define SMB_ACL_TYPE_DEFAULT 1
5259 +#else /* No ACLs. */
5261 +/* No ACLS - fake it. */
5262 +#define SMB_ACL_TAG_T int
5263 +#define SMB_ACL_TYPE_T int
5264 +#define SMB_ACL_PERMSET_T mode_t
5265 +#define SMB_ACL_PERM_T mode_t
5266 +#define SMB_ACL_READ S_IRUSR
5267 +#define SMB_ACL_WRITE S_IWUSR
5268 +#define SMB_ACL_EXECUTE S_IXUSR
5270 +/* Types of ACLs. */
5271 +#define SMB_ACL_USER 0
5272 +#define SMB_ACL_USER_OBJ 1
5273 +#define SMB_ACL_GROUP 2
5274 +#define SMB_ACL_GROUP_OBJ 3
5275 +#define SMB_ACL_OTHER 4
5276 +#define SMB_ACL_MASK 5
5278 +typedef struct SMB_ACL_T {
5282 +typedef struct SMB_ACL_ENTRY_T {
5284 +} *SMB_ACL_ENTRY_T;
5286 +#define SMB_ACL_FIRST_ENTRY 0
5287 +#define SMB_ACL_NEXT_ENTRY 1
5289 +#define SMB_ACL_TYPE_ACCESS 0
5290 +#define SMB_ACL_TYPE_DEFAULT 1
5292 +#endif /* No ACLs. */
5293 +#endif /* _SMB_ACLS_H */
5294 --- old/testsuite/default-acls.test
5295 +++ new/testsuite/default-acls.test
5299 +# This program is distributable under the terms of the GNU GPL see
5302 +# Test that rsync obeys default ACLs. -- Matt McCutchen
5304 +. $srcdir/testsuite/rsync.fns
5306 +$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
5307 +setfacl -dm u::rwx,g::---,o::--- "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
5309 +# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
5311 + todir="$scratchdir/$1"
5313 + # FIXME This doesn't work on solaris...
5314 + setfacl -k "$todir"
5315 + [ "$2" ] && setfacl -dm "$2" "$todir"
5316 + # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
5317 + # even though the directory itself is outside the transfer
5318 + $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
5319 + check_perms "$todir/to" $4 "Target $1"
5320 + check_perms "$todir/to/dir" $4 "Target $1"
5321 + check_perms "$todir/to/file" $3 "Target $1"
5322 + check_perms "$todir/to/program" $4 "Target $1"
5323 + # Make sure get_local_name doesn't mess us up when transferring only one file
5324 + $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
5325 + check_perms "$todir/to/anotherfile" $3 "Target $1"
5326 + # Make sure we obey default ACLs when not transferring a regular file
5327 + $RSYNC -rvv "$scratchdir/dir/" "$todir/to/anotherdir/"
5328 + check_perms "$todir/to/anotherdir" $4 "Target $1"
5331 +mkdir "$scratchdir/dir"
5332 +echo "File!" >"$scratchdir/file"
5333 +echo "#!/bin/sh" >"$scratchdir/program"
5334 +chmod 777 "$scratchdir/dir"
5335 +chmod 666 "$scratchdir/file"
5336 +chmod 777 "$scratchdir/program"
5338 +# Test some target directories
5340 +testit da777 u::rwx,g::rwx,o::rwx rw-rw-rw- rwxrwxrwx
5341 +testit da775 u::rwx,g::rwx,o::r-x rw-rw-r-- rwxrwxr-x
5342 +testit da750 u::rwx,g::r-x,o::--- rw-r----- rwxr-x---
5343 +testit da770mask u::rwx,g::---,m::rwx,o::--- rw-rw---- rwxrwx---
5344 +testit noda1 '' rw------- rwx------
5346 +testit noda2 '' rw-rw-rw- rwxrwxrwx
5348 +testit noda3 '' rw-r--r-- rwxr-xr-x
5356 extern int preserve_uid;
5357 extern int preserve_gid;
5358 +extern int preserve_acls;
5359 extern int numeric_ids;
5362 @@ -274,7 +275,7 @@ void send_uid_list(int f)
5366 - if (preserve_uid) {
5367 + if (preserve_uid || preserve_acls) {
5369 /* we send sequences of uid/byte-length/name */
5370 for (list = uidlist; list; list = list->next) {
5371 @@ -291,7 +292,7 @@ void send_uid_list(int f)
5375 - if (preserve_gid) {
5376 + if (preserve_gid || preserve_acls) {
5378 for (list = gidlist; list; list = list->next) {
5380 @@ -312,7 +313,7 @@ void recv_uid_list(int f, struct file_li
5384 - if (preserve_uid && !numeric_ids) {
5385 + if ((preserve_uid || preserve_acls) && !numeric_ids) {
5386 /* read the uid list */
5387 while ((id = read_int(f)) != 0) {
5388 int len = read_byte(f);
5389 @@ -324,7 +325,7 @@ void recv_uid_list(int f, struct file_li
5393 - if (preserve_gid && !numeric_ids) {
5394 + if ((preserve_gid || preserve_acls) && !numeric_ids) {
5395 /* read the gid list */
5396 while ((id = read_int(f)) != 0) {
5397 int len = read_byte(f);
5398 @@ -336,6 +337,18 @@ void recv_uid_list(int f, struct file_li
5402 +#ifdef SUPPORT_ACLS
5403 + if (preserve_acls && !numeric_ids) {
5405 + /* The enumerations don't return 0 except to flag the last
5406 + * entry, since uidlist doesn't munge 0 anyway. */
5407 + while ((id = next_acl_uid(flist)) != 0)
5408 + acl_uid_map(match_uid(id));
5409 + while ((id = next_acl_gid(flist)) != 0)
5410 + acl_gid_map(match_gid(id));
5412 +#endif /* SUPPORT_ACLS */
5414 /* Now convert all the uids/gids from sender values to our values. */
5415 if (am_root && preserve_uid && !numeric_ids) {
5416 for (i = 0; i < flist->count; i++)