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 };
81 +static void expand_rsync_acl(rsync_acl *racl)
83 + /* First time through, 0 <= 0, so list is expanded.
84 + * (Diabolical, rsync guys!) */
85 + if (racl->malloced <= racl->count) {
87 + size_t new_size = racl->malloced + 10;
88 + new_ptr = realloc_array(racl->races, rsync_ace, new_size);
90 + rprintf(FINFO, "expand rsync_acl to %.0f bytes, did%s move\n",
91 + (double) new_size * sizeof racl->races[0],
92 + racl->races ? "" : " not");
95 + racl->races = new_ptr;
96 + racl->malloced = new_size;
99 + out_of_memory("expand_rsync_acl");
103 +static void rsync_acl_free(rsync_acl *racl)
106 + racl->races = NULL;
108 + racl->malloced = 0;
111 +static int rsync_ace_sorter(const void *r1, const void *r2)
113 + rsync_ace *race1 = (rsync_ace *)r1;
114 + SMB_ACL_TAG_T rtag1 = race1->tag_type;
115 + id_t rid1 = race1->id;
116 + rsync_ace *race2 = (rsync_ace *)r2;
117 + SMB_ACL_TAG_T rtag2 = race2->tag_type;
118 + id_t rid2 = race2->id;
119 + /* start at the extrema */
120 + if (rtag1 == SMB_ACL_USER_OBJ || rtag2 == SMB_ACL_MASK)
122 + if (rtag2 == SMB_ACL_USER_OBJ || rtag1 == SMB_ACL_MASK)
125 + if (rtag1 == SMB_ACL_OTHER)
127 + if (rtag2 == SMB_ACL_OTHER)
129 + /* only SMB_ACL_USERs and SMB_ACL_GROUP*s left */
130 + if (rtag1 == SMB_ACL_USER) {
132 + case SMB_ACL_GROUP:
133 + case SMB_ACL_GROUP_OBJ:
134 + case SMB_ACL_OTHER:
138 + return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
140 + if (rtag2 == SMB_ACL_USER)
142 + /* only SMB_ACL_GROUP*s to worry about; kick out GROUP_OBJs first */
143 + if (rtag1 == SMB_ACL_GROUP_OBJ)
145 + if (rtag2 == SMB_ACL_GROUP_OBJ)
147 + /* only SMB_ACL_GROUPs left */
148 + return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
151 +static void sort_rsync_acl(rsync_acl *racl)
155 + qsort((void **)racl->races, racl->count, sizeof racl->races[0],
156 + &rsync_ace_sorter);
159 +static BOOL unpack_smb_acl(rsync_acl *racl, SMB_ACL_T sacl)
161 + SMB_ACL_ENTRY_T entry;
163 + const char *errfun;
164 + *racl = rsync_acl_initializer;
165 + errfun = "sys_acl_get_entry";
166 + for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
168 + rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
169 + SMB_ACL_PERMSET_T permset;
172 + expand_rsync_acl(racl);
173 + race = &racl->races[racl->count++];
174 + if ((rc = sys_acl_get_tag_type(entry, &race->tag_type))) {
175 + errfun = "sys_acl_get_tag_type";
178 + if ((rc = sys_acl_get_permset(entry, &permset))) {
179 + errfun = "sys_acl_get_tag_type";
182 + race->access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
183 + | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
184 + | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
185 + switch (race->tag_type) {
187 + case SMB_ACL_GROUP:
192 + if (!(qualifier = sys_acl_get_qualifier(entry))) {
193 + errfun = "sys_acl_get_tag_type";
197 + race->id = *((id_t *)qualifier);
198 + sys_acl_free_qualifier(qualifier, race->tag_type);
201 + rprintf(FERROR, "unpack_smb_acl: %s(): %s\n",
202 + errfun, strerror(errno));
203 + rsync_acl_free(racl);
206 + sort_rsync_acl(racl);
210 +static BOOL rsync_acls_equal(const rsync_acl *racl1, const rsync_acl *racl2)
212 + rsync_ace *race1, *race2;
213 + size_t count = racl1->count;
214 + if (count != racl2->count)
216 + race1 = racl1->races;
217 + race2 = racl2->races;
218 + for (; count--; race1++, race2++) {
219 + if (race1->tag_type != race2->tag_type
220 + || race1->access != race2->access
221 + || ((race1->tag_type == SMB_ACL_USER
222 + || race1->tag_type == SMB_ACL_GROUP)
223 + && race1->id != race2->id))
235 +static rsync_acl_list _rsync_acl_lists[] = {
236 + { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
237 + { 0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
240 +static inline rsync_acl_list *rsync_acl_lists(SMB_ACL_TYPE_T type)
242 + return type == SMB_ACL_TYPE_ACCESS ? &_rsync_acl_lists[0]
243 + : &_rsync_acl_lists[1];
246 +static void expand_rsync_acl_list(rsync_acl_list *racl_list)
248 + /* First time through, 0 <= 0, so list is expanded.
249 + * (Diabolical, rsync guys!) */
250 + if (racl_list->malloced <= racl_list->count) {
251 + rsync_acl *new_ptr;
253 + if (racl_list->malloced < 1000)
254 + new_size = racl_list->malloced + 1000;
256 + new_size = racl_list->malloced * 2;
257 + new_ptr = realloc_array(racl_list->racls, rsync_acl, new_size);
258 + if (verbose >= 3) {
259 + rprintf(FINFO, "expand_rsync_acl_list to %.0f bytes, did%s move\n",
260 + (double) new_size * sizeof racl_list->racls[0],
261 + racl_list->racls ? "" : " not");
264 + racl_list->racls = new_ptr;
265 + racl_list->malloced = new_size;
267 + if (!racl_list->racls)
268 + out_of_memory("expand_rsync_acl_list");
273 +static void free_rsync_acl_list(rsync_acl_list *racl_list)
275 + /* Run this in reverse, so references are freed before referents,
276 + * although not currently necessary. */
277 + while (racl_list->count--) {
278 + rsync_acl *racl = &racl_list->racls[racl_list->count];
280 + rsync_acl_free(racl);
282 + free(racl_list->racls);
283 + racl_list->racls = NULL;
284 + racl_list->malloced = 0;
288 +static int find_matching_rsync_acl(SMB_ACL_TYPE_T type,
289 + const rsync_acl_list *racl_list,
290 + const rsync_acl *racl)
292 + static int access_match = -1, default_match = -1;
293 + int *match = (type == SMB_ACL_TYPE_ACCESS) ?
294 + &access_match : &default_match;
295 + size_t count = racl_list->count;
296 + /* If this is the first time through or we didn't match the last
297 + * time, then start at the end of the list, which should be the
298 + * best place to start hunting. */
300 + *match = racl_list->count - 1;
302 + if (rsync_acls_equal(&racl_list->racls[*match], racl))
305 + *match = racl_list->count - 1;
311 +/* The general strategy with the tag_type <-> character mapping is that
312 + * lowercase implies that no qualifier follows, where uppercase does.
313 + * A similar idiom for the acl type (access or default) itself, but
314 + * lowercase in this instance means there's no ACL following, so the
315 + * ACL is a repeat, so the receiver should reuse the last of the same
318 +static void send_rsync_acl(int f, const rsync_acl *racl)
321 + size_t count = racl->count;
322 + write_int(f, count);
323 + for (race = racl->races; count--; race++) {
325 + switch (race->tag_type) {
326 + case SMB_ACL_USER_OBJ:
332 + case SMB_ACL_GROUP_OBJ:
335 + case SMB_ACL_GROUP:
338 + case SMB_ACL_OTHER:
346 + "send_rsync_acl: unknown tag_type (%0x) on ACE; disregarding\n",
351 + write_byte(f, race->access);
352 + if (isupper((int)ch)) {
353 + write_int(f, race->id);
354 + /* FIXME: sorta wasteful: we should maybe buffer as
355 + * many ids as max(ACL_USER + ACL_GROUP) objects to
356 + * keep from making so many calls. */
365 +static rsync_acl _curr_rsync_acls[2];
368 +static const char *str_acl_type(SMB_ACL_TYPE_T type)
370 + return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS" :
371 + type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT" :
372 + "unknown SMB_ACL_TYPE_T";
375 +/* Generate the ACL(s) for this flist entry;
376 + * ACL(s) are either sent or cleaned-up by send_acl() below. */
378 +int make_acl(const struct file_struct *file, const char *fname)
380 + SMB_ACL_TYPE_T *type,
381 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
382 + rsync_acl *curr_racl;
383 + if (!preserve_acls || S_ISLNK(file->mode))
385 + for (type = &types[0], curr_racl = &_curr_rsync_acls[0];
386 + type < &types[0] + sizeof types / sizeof types[0]
387 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
388 + type++, curr_racl++) {
391 + *curr_racl = rsync_acl_initializer;
392 + if (!(sacl = sys_acl_get_file(fname, *type))) {
393 + rprintf(FERROR, "send_acl: sys_acl_get_file(%s, %s): %s\n",
394 + fname, str_acl_type(*type), strerror(errno));
397 + ok = unpack_smb_acl(curr_racl, sacl);
398 + sys_acl_free_acl(sacl);
405 +/* Send the make_acl()-generated ACLs for this flist entry,
406 + * or clean up after an flist entry that's not being sent (f == -1). */
408 +void send_acl(const struct file_struct *file, int f)
410 + SMB_ACL_TYPE_T *type,
411 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
412 + rsync_acl *curr_racl;
413 + if (!preserve_acls || S_ISLNK(file->mode))
415 + for (type = &types[0], curr_racl = &_curr_rsync_acls[0];
416 + type < &types[0] + sizeof types / sizeof types[0]
417 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
418 + type++, curr_racl++) {
420 + rsync_acl_list *racl_list = rsync_acl_lists(*type);
422 + rsync_acl_free(curr_racl);
425 + if ((index = find_matching_rsync_acl(*type, racl_list, curr_racl))
427 + write_byte(f, *type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
428 + write_int(f, index);
429 + rsync_acl_free(curr_racl);
431 + write_byte(f, *type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
432 + send_rsync_acl(f, curr_racl);
433 + expand_rsync_acl_list(racl_list);
434 + racl_list->racls[racl_list->count++] = *curr_racl;
439 +/* The below stuff is only used by the receiver: */
441 +/* structure to hold index to rsync_acl_list member corresponding to
442 + * flist->files[i] */
445 + const struct file_struct *file;
452 + file_acl_index *fileaclidxs;
453 +} file_acl_index_list;
455 +static file_acl_index_list _file_acl_index_lists[] = {
456 + {0, 0, NULL },/* SMB_ACL_TYPE_ACCESS */
457 + {0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
460 +static inline file_acl_index_list *file_acl_index_lists(SMB_ACL_TYPE_T type)
462 + return type == SMB_ACL_TYPE_ACCESS ?
463 + &_file_acl_index_lists[0] : &_file_acl_index_lists[1];
466 +static void expand_file_acl_index_list(file_acl_index_list *fileaclidx_list)
468 + /* First time through, 0 <= 0, so list is expanded.
469 + * (Diabolical, rsync guys!) */
470 + if (fileaclidx_list->malloced <= fileaclidx_list->count) {
471 + file_acl_index *new_ptr;
473 + if (fileaclidx_list->malloced < 1000)
474 + new_size = fileaclidx_list->malloced + 1000;
476 + new_size = fileaclidx_list->malloced * 2;
477 + new_ptr = realloc_array(fileaclidx_list->fileaclidxs, file_acl_index, new_size);
478 + if (verbose >= 3) {
479 + rprintf(FINFO, "expand_file_acl_index_list to %.0f bytes, did%s move\n",
480 + (double) new_size * sizeof fileaclidx_list->fileaclidxs[0],
481 + fileaclidx_list->fileaclidxs ? "" : " not");
484 + fileaclidx_list->fileaclidxs = new_ptr;
485 + fileaclidx_list->malloced = new_size;
487 + if (!fileaclidx_list->fileaclidxs)
488 + out_of_memory("expand_file_acl_index_list");
493 +static void free_file_acl_index_list(file_acl_index_list *fileaclidx_list)
495 + free(fileaclidx_list->fileaclidxs);
496 + fileaclidx_list->fileaclidxs = NULL;
497 + fileaclidx_list->malloced = 0;
501 +/* lists to hold the SMB_ACL_Ts corresponding to the rsync_acl_list entries */
509 +static smb_acl_list _smb_acl_lists[] = {
510 + { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
511 + { 0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
514 +static inline smb_acl_list *smb_acl_lists(SMB_ACL_TYPE_T type)
516 + return type == SMB_ACL_TYPE_ACCESS ? &_smb_acl_lists[0] :
517 + &_smb_acl_lists[1];
520 +static void expand_smb_acl_list(smb_acl_list *sacl_list)
522 + /* First time through, 0 <= 0, so list is expanded.
523 + * (Diabolical, rsync guys!) */
524 + if (sacl_list->malloced <= sacl_list->count) {
525 + SMB_ACL_T *new_ptr;
527 + if (sacl_list->malloced < 1000)
528 + new_size = sacl_list->malloced + 1000;
530 + new_size = sacl_list->malloced * 2;
531 + new_ptr = realloc_array(sacl_list->sacls, SMB_ACL_T, new_size);
532 + if (verbose >= 3) {
533 + rprintf(FINFO, "expand_smb_acl_list to %.0f bytes, did%s move\n",
534 + (double) new_size * sizeof sacl_list->sacls[0],
535 + sacl_list->sacls ? "" : " not");
538 + sacl_list->sacls = new_ptr;
539 + sacl_list->malloced = new_size;
541 + if (!sacl_list->sacls)
542 + out_of_memory("expand_smb_acl_list");
547 +static void free_smb_acl_list(SMB_ACL_TYPE_T type)
549 + smb_acl_list *sacl_list = smb_acl_lists(type);
550 + SMB_ACL_T *sacl = sacl_list->sacls;
551 + while (sacl_list->count--) {
553 + sys_acl_free_acl(*sacl++);
555 + free(sacl_list->sacls);
556 + sacl_list->sacls = NULL;
557 + sacl_list->malloced = 0;
561 +/* build an SMB_ACL_T corresponding to an rsync_acl */
562 +static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
564 + size_t count = racl->count;
565 + rsync_ace *race = racl->races;
566 + const char *errfun = NULL;
567 + *smb_acl = sys_acl_init(count);
569 + rprintf(FERROR, "pack_smb_acl: sys_acl_int(): %s\n",
573 + for (; count--; race++) {
574 + SMB_ACL_ENTRY_T entry;
575 + SMB_ACL_PERMSET_T permset;
576 + if (sys_acl_create_entry(smb_acl, &entry)) {
577 + errfun = "sys_acl_create)";
580 + if (sys_acl_set_tag_type(entry, race->tag_type)) {
581 + errfun = "sys_acl_set_tag";
584 + if (race->tag_type == SMB_ACL_USER ||
585 + race->tag_type == SMB_ACL_GROUP)
586 + if (sys_acl_set_qualifier(entry, (void*)&race->id)) {
587 + errfun = "sys_acl_set_qualfier";
590 + if (sys_acl_get_permset(entry, &permset)) {
591 + errfun = "sys_acl_get_permset";
594 + if (sys_acl_clear_perms(permset)) {
595 + errfun = "sys_acl_clear_perms";
598 + if (race->access & 4)
599 + if (sys_acl_add_perm(permset, SMB_ACL_READ)) {
600 + errfun = "sys_acl_add_perm";
603 + if (race->access & 2)
604 + if (sys_acl_add_perm(permset, SMB_ACL_WRITE)) {
605 + errfun = "sys_acl_add_perm";
608 + if (race->access & 1)
609 + if (sys_acl_add_perm(permset, SMB_ACL_EXECUTE)) {
610 + errfun = "sys_acl_add_perm";
613 + if (sys_acl_set_permset(entry, permset)) {
614 + errfun = "sys_acl_set_permset";
619 + sys_acl_free_acl(*smb_acl);
620 + rprintf(FERROR, "pack_smb_acl %s(): %s\n", errfun,
627 +static void receive_rsync_acl(rsync_acl *racl, int f)
630 + uchar required_mask_perm = 0;
632 + BOOL saw_mask = False;
633 + BOOL saw_user_obj = False, saw_group_obj = False,
635 + size_t count = read_int(f);
640 + uchar tag = read_byte(f);
641 + expand_rsync_acl(racl);
642 + race = &racl->races[racl->count++];
645 + race->tag_type = SMB_ACL_USER_OBJ;
646 + saw_user_obj = True;
649 + race->tag_type = SMB_ACL_USER;
652 + race->tag_type = SMB_ACL_GROUP_OBJ;
653 + saw_group_obj = True;
656 + race->tag_type = SMB_ACL_GROUP;
659 + race->tag_type = SMB_ACL_OTHER;
663 + race->tag_type = SMB_ACL_MASK;
667 + rprintf(FERROR, "receive_rsync_acl: unknown tag %c\n",
669 + exit_cleanup(RERR_STREAMIO);
671 + race->access = read_byte(f);
672 + if (race->access & ~ (4 | 2 | 1)) {
673 + rprintf(FERROR, "receive_rsync_acl: bogus permset %o\n",
675 + exit_cleanup(RERR_STREAMIO);
677 + if (race->tag_type == SMB_ACL_USER ||
678 + race->tag_type == SMB_ACL_GROUP) {
679 + race->id = read_int(f);
681 + required_mask_perm |= race->access;
685 + else if (race->tag_type == SMB_ACL_GROUP_OBJ)
686 + required_mask_perm |= race->access;
690 + if (!saw_user_obj) {
691 + expand_rsync_acl(racl);
692 + race = &racl->races[racl->count++];
693 + race->tag_type = SMB_ACL_USER_OBJ;
696 + if (!saw_group_obj) {
697 + expand_rsync_acl(racl);
698 + race = &racl->races[racl->count++];
699 + race->tag_type = SMB_ACL_GROUP_OBJ;
703 + expand_rsync_acl(racl);
704 + race = &racl->races[racl->count++];
705 + race->tag_type = SMB_ACL_OTHER;
710 + expand_rsync_acl(racl);
711 + race = &racl->races[racl->count++];
712 + race->tag_type = SMB_ACL_MASK;
713 + race->access = required_mask_perm;
716 + /* If we, a system without ACLS_NEED_MASK, received data from a
717 + * system that has masks, throw away the extraneous CLASS_OBJs. */
718 + if (saw_mask && racl->count == 4) {
719 + rsync_ace *group_obj_race = NULL, *mask_race = NULL;
722 + for (i = 0, p = racl->races; i < racl->count; i++, p++) {
723 + if (p->tag_type == SMB_ACL_MASK)
725 + else if (p->tag_type == SMB_ACL_GROUP_OBJ)
726 + group_obj_race = p;
728 + if (mask_race == NULL || group_obj_race == NULL) {
729 + rprintf(FERROR, "receive_rsync_acl: have four ACES "
730 + "and one's ACL_MASK but missing "
731 + "either it or ACL_GROUP_OBJ, "
732 + "when pruning ACL\n");
734 + /* mask off group perms with it first */
735 + group_obj_race->access &= mask_race->access;
736 + /* dump mask_race; re-slot any followers-on */
738 + if (mask_race != &racl->races[racl->count]) {
739 + *mask_race = racl->races[racl->count];
740 + saw_user_obj = False; /* force re-sort */
746 + if (!(saw_user_obj && saw_group_obj && saw_other && saw_mask))
748 + if (!(saw_user_obj && saw_group_obj && saw_other))
750 + sort_rsync_acl(racl);
753 +/* receive and build the rsync_acl_lists */
755 +void receive_acl(struct file_struct *file, int f)
757 + SMB_ACL_TYPE_T *type,
758 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
760 + if (!preserve_acls || S_ISLNK(file->mode))
762 + fname = f_name(file, NULL);
763 + for (type = &types[0];
764 + type < &types[0] + sizeof types / sizeof types[0]
765 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
767 + file_acl_index_list *fileaclidx_list =
768 + file_acl_index_lists(*type);
770 + expand_file_acl_index_list(fileaclidx_list);
772 + tag = read_byte(f);
773 + if (tag == 'A' || tag == 'a') {
774 + if (*type != SMB_ACL_TYPE_ACCESS) {
775 + rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
777 + exit_cleanup(RERR_STREAMIO);
779 + } else if (tag == 'D' || tag == 'd') {
780 + if (*type == SMB_ACL_TYPE_ACCESS) {
781 + rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
783 + exit_cleanup(RERR_STREAMIO);
786 + rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
788 + exit_cleanup(RERR_STREAMIO);
790 + if (tag == 'A' || tag == 'D') {
791 + rsync_acl racl = rsync_acl_initializer;
792 + rsync_acl_list *racl_list = rsync_acl_lists(*type);
793 + smb_acl_list *sacl_list = smb_acl_lists(*type);
794 + fileaclidx_list->fileaclidxs[fileaclidx_list->count].
795 + aclidx = racl_list->count;
796 + fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
798 + receive_rsync_acl(&racl, f);
799 + expand_rsync_acl_list(racl_list);
800 + racl_list->racls[racl_list->count++] = racl;
801 + expand_smb_acl_list(sacl_list);
802 + sacl_list->sacls[sacl_list->count++] = NULL;
804 + int index = read_int(f);
805 + rsync_acl_list *racl_list = rsync_acl_lists(*type);
806 + if ((size_t) index >= racl_list->count) {
807 + rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
809 + str_acl_type(*type),
811 + exit_cleanup(RERR_STREAMIO);
813 + fileaclidx_list->fileaclidxs[fileaclidx_list->count].
815 + fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
821 +static int file_acl_index_list_sorter(const void *f1, const void *f2)
823 + const file_acl_index *fileaclidx1 = (const file_acl_index *)f1;
824 + const file_acl_index *fileaclidx2 = (const file_acl_index *)f2;
825 + return fileaclidx1->file == fileaclidx2->file ? 0 :
826 + fileaclidx1->file < fileaclidx2->file ? -1 : 1;
829 +void sort_file_acl_index_lists()
831 + SMB_ACL_TYPE_T *type,
832 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
833 + if (!preserve_acls)
835 + for (type = &types[0];
836 + type < &types[0] + sizeof types / sizeof types[0];
839 + file_acl_index_list *fileaclidx_list =
840 + file_acl_index_lists(*type);
841 + if (!fileaclidx_list->count)
843 + qsort(fileaclidx_list->fileaclidxs, fileaclidx_list->count,
844 + sizeof fileaclidx_list->fileaclidxs[0],
845 + &file_acl_index_list_sorter);
849 +static int find_file_acl_index(const file_acl_index_list *fileaclidx_list,
850 + const struct file_struct *file) {
851 + int low = 0, high = fileaclidx_list->count;
852 + const struct file_struct *file_mid;
856 + int mid = (high + low) / 2;
857 + file_mid = fileaclidx_list->fileaclidxs[mid].file;
858 + if (file_mid == file)
859 + return fileaclidx_list->fileaclidxs[mid].aclidx;
860 + if (file_mid > file)
864 + } while (low < high);
866 + file_mid = fileaclidx_list->fileaclidxs[low].file;
867 + if (file_mid == file)
868 + return fileaclidx_list->fileaclidxs[low].aclidx;
871 + "find_file_acl_index: can't find entry for file in list\n");
872 + exit_cleanup(RERR_STREAMIO);
876 +/* for duplicating ACLs on backups when using backup_dir */
878 +int dup_acl(const char *orig, const char *bak, mode_t mode)
880 + SMB_ACL_TYPE_T *type,
881 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
883 + if (!preserve_acls)
885 + for (type = &types[0];
886 + type < &types[0] + sizeof types / sizeof types[0]
887 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(mode));
889 + SMB_ACL_T sacl_orig, sacl_bak;
890 + rsync_acl racl_orig, racl_bak;
891 + if (!(sacl_orig = sys_acl_get_file(orig, *type))) {
892 + rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s\n",
893 + orig, str_acl_type(*type), strerror(errno));
897 + if (!(sacl_bak = sys_acl_get_file(orig, *type))) {
898 + rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s. ignoring\n",
899 + bak, str_acl_type(*type), strerror(errno));
901 + /* try to forge on through */
903 + if (!unpack_smb_acl(&racl_orig, sacl_orig)) {
905 + goto out_with_sacls;
908 + if (!unpack_smb_acl(&racl_bak, sacl_bak)) {
910 + goto out_with_one_racl;
912 + if (rsync_acls_equal(&racl_orig, &racl_bak))
915 + ; /* presume they're unequal */
917 + if (*type == SMB_ACL_TYPE_DEFAULT && !racl_orig.count) {
918 + if (-1 == sys_acl_delete_def_file(bak)) {
919 + rprintf(FERROR, "dup_acl: sys_acl_delete_def_file(%s): %s\n",
920 + bak, strerror(errno));
923 + } else if (-1 == sys_acl_set_file(bak, *type, sacl_bak)) {
924 + rprintf(FERROR, "dup_acl: sys_acl_set_file(%s, %s): %s\n",
925 + bak, str_acl_type(*type), strerror(errno));
930 + rsync_acl_free(&racl_bak);
932 + rsync_acl_free(&racl_orig);
935 + sys_acl_free_acl(sacl_bak);
936 + /* out_with_one_sacl: */
938 + sys_acl_free_acl(sacl_orig);
943 +/* Stuff for redirecting calls to set_acl() from set_file_attrs()
944 + * for keep_backup(). */
945 +static const struct file_struct *backup_orig_file = NULL;
946 +static const char null_string[] = "";
947 +static const char *backup_orig_fname = null_string;
948 +static const char *backup_dest_fname = null_string;
949 +static SMB_ACL_T _backup_sacl[] = { NULL, NULL };
951 +void push_keep_backup_acl(const struct file_struct *file,
952 + const char *orig, const char *dest)
954 + if (preserve_acls) {
955 + SMB_ACL_TYPE_T *type,
956 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
958 + backup_orig_file = file;
959 + backup_orig_fname = orig;
960 + backup_dest_fname = dest;
961 + for (type = &types[0], sacl = &_backup_sacl[0];
962 + type < &types[0] + sizeof types / sizeof types[0];
964 + if (*type == SMB_ACL_TYPE_DEFAULT && !S_ISDIR(file->mode))
967 + if (!(*sacl = sys_acl_get_file(orig, *type))) {
968 + rprintf(FERROR, "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
969 + orig, str_acl_type(*type),
977 +static int set_keep_backup_acl()
979 + if (preserve_acls) {
980 + SMB_ACL_TYPE_T *type,
981 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
984 + for (type = &types[0], sacl = &_backup_sacl[0];
985 + type < &types[0] + sizeof types / sizeof types[0];
988 + if (-1 == sys_acl_set_file(backup_dest_fname,
991 + rprintf(FERROR, "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
993 + str_acl_type(*type),
1004 +void cleanup_keep_backup_acl()
1006 + if (preserve_acls) {
1007 + SMB_ACL_TYPE_T *type,
1008 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
1010 + backup_orig_file = NULL;
1011 + backup_orig_fname = null_string;
1012 + backup_dest_fname = null_string;
1013 + for (type = &types[0], sacl = &_backup_sacl[0];
1014 + type < &types[0] + sizeof types / sizeof types[0];
1017 + sys_acl_free_acl(*sacl);
1023 +/* set ACL on rsync-ed or keep_backup-ed file */
1025 +int set_acl(const char *fname, const struct file_struct *file)
1028 + SMB_ACL_TYPE_T *type,
1029 + types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
1030 + if (dry_run || !preserve_acls || S_ISLNK(file->mode))
1032 + if (file == backup_orig_file) {
1033 + if (!strcmp(fname, backup_dest_fname))
1034 + return set_keep_backup_acl();
1036 + for (type = &types[0];
1037 + type < &types[0] + sizeof types / sizeof types[0]
1038 + && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
1040 + SMB_ACL_T sacl_orig, *sacl_new;
1041 + rsync_acl racl_orig, *racl_new;
1042 + int aclidx = find_file_acl_index(file_acl_index_lists(*type),
1045 + racl_new = &(rsync_acl_lists(*type)->racls[aclidx]);
1046 + sacl_new = &(smb_acl_lists(*type)->sacls[aclidx]);
1047 + sacl_orig = sys_acl_get_file(fname, *type);
1049 + rprintf(FERROR, "set_acl: sys_acl_get_file(%s, %s): %s\n",
1050 + fname, str_acl_type(*type), strerror(errno));
1054 + ok = unpack_smb_acl(&racl_orig, sacl_orig);
1055 + sys_acl_free_acl(sacl_orig);
1060 + ok = rsync_acls_equal(&racl_orig, racl_new);
1061 + rsync_acl_free(&racl_orig);
1064 + if (*type == SMB_ACL_TYPE_DEFAULT && !racl_new->count) {
1065 + if (-1 == sys_acl_delete_def_file(fname)) {
1066 + rprintf(FERROR, "set_acl: sys_acl_delete_def_file(%s): %s\n",
1067 + fname, strerror(errno));
1073 + if (!pack_smb_acl(sacl_new, racl_new)) {
1077 + if (-1 == sys_acl_set_file(fname, *type, *sacl_new)) {
1078 + rprintf(FERROR, "set_acl: sys_acl_set_file(%s, %s): %s\n",
1079 + fname, str_acl_type(*type),
1091 +/* Enumeration functions for uid mapping: */
1093 +/* Context -- one and only one. Should be cycled through once on uid
1094 + * mapping and once on gid mapping. */
1095 +static rsync_acl_list *_enum_racl_lists[] = {
1096 + &_rsync_acl_lists[0], &_rsync_acl_lists[1], NULL
1099 +static rsync_acl_list **enum_racl_list = &_enum_racl_lists[0];
1100 +static size_t enum_racl_index = 0;
1101 +static size_t enum_race_index = 0;
1103 +/* This returns the next tag_type id from the given acl for the next entry,
1104 + * or it returns 0 if there are no more tag_type ids in the acl. */
1106 +static id_t next_ace_id(SMB_ACL_TAG_T tag_type, const rsync_acl *racl)
1108 + for (; enum_race_index < racl->count; enum_race_index++) {
1109 + rsync_ace *race = &racl->races[enum_race_index];
1110 + if (race->tag_type == tag_type)
1113 + enum_race_index = 0;
1117 +static id_t next_acl_id(SMB_ACL_TAG_T tag_type, const rsync_acl_list *racl_list)
1119 + for (; enum_racl_index < racl_list->count; enum_racl_index++) {
1120 + rsync_acl *racl = &racl_list->racls[enum_racl_index];
1121 + id_t id = next_ace_id(tag_type, racl);
1125 + enum_racl_index = 0;
1129 +static id_t next_acl_list_id(SMB_ACL_TAG_T tag_type)
1131 + for (; *enum_racl_list; enum_racl_list++) {
1132 + id_t id = next_acl_id(tag_type, *enum_racl_list);
1136 + enum_racl_list = &_enum_racl_lists[0];
1140 +id_t next_acl_uid()
1142 + return next_acl_list_id(SMB_ACL_USER);
1145 +id_t next_acl_gid()
1147 + return next_acl_list_id(SMB_ACL_GROUP);
1150 +/* referring to the global context enum_entry, sets the entry's id */
1151 +static void set_acl_id(id_t id)
1153 + (*enum_racl_list)->racls[enum_racl_index].races[enum_race_index++].id = id;
1156 +void acl_uid_map(id_t uid)
1161 +void acl_gid_map(id_t gid)
1166 +#define PERMS_SPLICE(perms,newbits,where) (((perms) & ~(7 << (where))) | ((newbits) << (where)))
1168 +int default_perms_for_dir(const char *dir)
1172 + BOOL ok, saw_mask = False;
1178 + perms = ACCESSPERMS & ~orig_umask;
1179 + /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1180 + sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1181 + if (sacl == NULL) {
1182 + /* Couldn't get an ACL. Darn. */
1185 + /* ACLs are disabled. We could yell at the user to turn them on, but... */
1189 + /* We're doing a dry run, so the containing directory
1190 + * wasn't actually created. Don't worry about it. */
1193 + /* Otherwise fall through. */
1195 + rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1196 + dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1202 + ok = unpack_smb_acl(&racl, sacl);
1203 + sys_acl_free_acl(sacl);
1205 + rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1209 + /* Look at each default ACL entry and possibly modify three bits of `perms' accordingly.
1210 + * If there's "no" default ACL, there will be zero entries and the umask-based perms is unchanged. */
1211 + for (i = 0; i < racl.count; i++) {
1212 + switch (racl.races[i].tag_type) {
1213 + case SMB_ACL_USER_OBJ:
1214 + perms = PERMS_SPLICE(perms, racl.races[i].access, 6);
1216 + case SMB_ACL_GROUP_OBJ:
1218 + perms = PERMS_SPLICE(perms, racl.races[i].access, 3);
1220 + case SMB_ACL_MASK:
1222 + perms = PERMS_SPLICE(perms, racl.races[i].access, 3);
1224 + case SMB_ACL_OTHER:
1225 + perms = PERMS_SPLICE(perms, racl.races[i].access, 0);
1231 + rsync_acl_free(&racl);
1233 + rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1237 +#endif /* SUPPORT_ACLS */
1240 @@ -132,6 +132,7 @@ static int make_bak_dir(char *fullpath)
1242 do_lchown(fullpath, st.st_uid, st.st_gid);
1243 do_chmod(fullpath, st.st_mode);
1244 + (void)DUP_ACL(end, fullpath, st.st_mode);
1248 @@ -185,6 +186,8 @@ static int keep_backup(char *fname)
1249 if (!(buf = get_backup_name(fname)))
1252 + PUSH_KEEP_BACKUP_ACL(file, fname, buf);
1254 /* Check to see if this is a device file, or link */
1255 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1256 || (preserve_specials && IS_SPECIAL(file->mode))) {
1257 @@ -260,6 +263,7 @@ static int keep_backup(char *fname)
1260 set_file_attrs(buf, file, NULL, 0);
1261 + CLEANUP_KEEP_BACKUP_ACL();
1265 --- old/configure.in
1266 +++ new/configure.in
1267 @@ -482,6 +482,11 @@ if test x"$ac_cv_func_strcasecmp" = x"no
1268 AC_CHECK_LIB(resolv, strcasecmp)
1271 +AC_CHECK_FUNCS(aclsort)
1272 +if test x"$ac_cv_func_aclsort" = x"no"; then
1273 + AC_CHECK_LIB(sec, aclsort)
1276 dnl At the moment we don't test for a broken memcmp(), because all we
1277 dnl need to do is test for equality, not comparison, and it seems that
1278 dnl every platform has a memcmp that can do at least that.
1279 @@ -738,6 +743,77 @@ AC_SUBST(OBJ_RESTORE)
1280 AC_SUBST(CC_SHOBJ_FLAG)
1281 AC_SUBST(BUILD_POPT)
1283 +AC_CHECK_HEADERS(sys/acl.h)
1284 +AC_CHECK_FUNCS(_acl __acl _facl __facl)
1285 +#################################################
1286 +# check for ACL support
1288 +AC_MSG_CHECKING(whether to support ACLs)
1289 +AC_ARG_ENABLE(acl-support,
1290 +AC_HELP_STRING([--enable-acl-support], [Include ACL support (default=no)]),
1291 +[ case "$enableval" in
1294 + case "$host_os" in
1296 + AC_MSG_RESULT(Using UnixWare ACLs)
1297 + AC_DEFINE(HAVE_UNIXWARE_ACLS, 1, [true if you have UnixWare ACLs])
1300 + AC_MSG_RESULT(Using solaris ACLs)
1301 + AC_DEFINE(HAVE_SOLARIS_ACLS, 1, [true if you have solaris ACLs])
1304 + AC_MSG_RESULT(Using HPUX ACLs)
1305 + AC_DEFINE(HAVE_HPUX_ACLS, 1, [true if you have HPUX ACLs])
1308 + AC_MSG_RESULT(Using IRIX ACLs)
1309 + AC_DEFINE(HAVE_IRIX_ACLS, 1, [true if you have IRIX ACLs])
1312 + AC_MSG_RESULT(Using AIX ACLs)
1313 + AC_DEFINE(HAVE_AIX_ACLS, 1, [true if you have AIX ACLs])
1316 + AC_MSG_RESULT(Using Tru64 ACLs)
1317 + AC_DEFINE(HAVE_TRU64_ACLS, 1, [true if you have Tru64 ACLs])
1318 + LIBS="$LIBS -lpacl"
1321 + AC_MSG_RESULT(ACLs requested -- running tests)
1322 + AC_CHECK_LIB(acl,acl_get_file)
1323 + AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1324 + AC_TRY_LINK([#include <sys/types.h>
1325 +#include <sys/acl.h>],
1326 +[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1327 +samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
1328 + if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1329 + AC_MSG_RESULT(Using posix ACLs)
1330 + AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
1331 + AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
1332 + AC_TRY_LINK([#include <sys/types.h>
1333 +#include <sys/acl.h>],
1334 +[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1335 +samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
1336 + if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1337 + AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1340 + AC_MSG_ERROR(Failed to find ACL support)
1347 + AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1350 + AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1354 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1359 @@ -966,6 +966,8 @@ static struct file_struct *send_file_nam
1360 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1363 + if (MAKE_ACL(file, fname) < 0)
1366 if (chmod_modes && !S_ISLNK(file->mode))
1367 file->mode = tweak_mode(file->mode, chmod_modes);
1368 @@ -977,6 +979,10 @@ static struct file_struct *send_file_nam
1369 if (file->basename[0]) {
1370 flist->files[flist->count++] = file;
1371 send_file_entry(file, f);
1372 + SEND_ACL(file, f);
1374 + /* Cleanup unsent ACL(s). */
1375 + SEND_ACL(file, -1);
1379 @@ -1365,6 +1371,8 @@ struct file_list *recv_file_list(int f)
1380 flags |= read_byte(f) << 8;
1381 file = receive_file_entry(flist, flags, f);
1383 + RECEIVE_ACL(file, f);
1385 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
1386 stats.total_size += file->length;
1388 @@ -1387,6 +1395,8 @@ struct file_list *recv_file_list(int f)
1390 clean_flist(flist, relative_paths, 1);
1392 + SORT_FILE_ACL_INDEX_LISTS();
1395 recv_uid_list(f, flist);
1399 @@ -85,6 +85,7 @@ extern long block_size; /* "long" becaus
1400 extern int max_delete;
1401 extern int force_delete;
1402 extern int one_file_system;
1403 +extern mode_t orig_umask;
1404 extern struct stats stats;
1405 extern dev_t filesystem_dev;
1406 extern char *backup_dir;
1407 @@ -753,6 +754,7 @@ static int try_dests_non(struct file_str
1410 static int phase = 0;
1411 +static int dflt_perms;
1413 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
1414 * make sure it exists, and has the right permissions/timestamp info. For
1415 @@ -844,6 +846,10 @@ static void recv_generator(char *fname,
1418 need_fuzzy_dirlist = 1;
1419 +#ifdef SUPPORT_ACLS
1420 + if (!preserve_perms)
1421 + dflt_perms = default_perms_for_dir(dn);
1424 parent_dirname = dn;
1426 @@ -871,7 +877,8 @@ static void recv_generator(char *fname,
1427 if (!preserve_perms) {
1428 int exists = statret == 0
1429 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1430 - file->mode = dest_mode(file->mode, st.st_mode, exists);
1431 + file->mode = dest_mode(file->mode, st.st_mode, dflt_perms,
1435 if (S_ISDIR(file->mode)) {
1436 @@ -905,6 +912,10 @@ static void recv_generator(char *fname,
1437 if (set_file_attrs(fname, file, statret ? NULL : &st, 0)
1438 && verbose && code && f_out != -1)
1439 rprintf(code, "%s/\n", fname);
1440 +#ifdef SUPPORT_ACLS
1442 + SET_ACL(fname, file);
1444 if (delete_during && f_out != -1 && !phase && dry_run < 2
1445 && (file->flags & FLAG_DEL_HERE))
1446 delete_in_dir(the_file_list, fname, file, &st);
1447 @@ -1342,6 +1353,8 @@ void generate_files(int f_out, struct fi
1448 * notice that and let us know via the redo pipe (or its closing). */
1451 + dflt_perms = (ACCESSPERMS & ~orig_umask);
1453 for (i = 0; i < flist->count; i++) {
1454 struct file_struct *file = flist->files[i];
1456 --- old/lib/sysacls.c
1457 +++ new/lib/sysacls.c
1460 + Unix SMB/CIFS implementation.
1461 + Samba system utilities for ACL support.
1462 + Copyright (C) Jeremy Allison 2000.
1464 + This program is free software; you can redistribute it and/or modify
1465 + it under the terms of the GNU General Public License as published by
1466 + the Free Software Foundation; either version 2 of the License, or
1467 + (at your option) any later version.
1469 + This program is distributed in the hope that it will be useful,
1470 + but WITHOUT ANY WARRANTY; without even the implied warranty of
1471 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1472 + GNU General Public License for more details.
1474 + You should have received a copy of the GNU General Public License
1475 + along with this program; if not, write to the Free Software
1476 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1480 +#include "sysacls.h" /****** ADDED ******/
1482 +/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
1483 +void SAFE_FREE(void *mem)
1489 +char *uidtoname(uid_t uid)
1491 + static char idbuf[12];
1492 + struct passwd *pw;
1494 + if ((pw = getpwuid(uid)) == NULL) {
1495 + slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
1498 + return pw->pw_name;
1500 +/****** EXTRAS -- END ******/
1503 + This file wraps all differing system ACL interfaces into a consistent
1504 + one based on the POSIX interface. It also returns the correct errors
1505 + for older UNIX systems that don't support ACLs.
1507 + The interfaces that each ACL implementation must support are as follows :
1509 + int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1510 + int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1511 + int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
1512 + void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1513 + SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1514 + SMB_ACL_T sys_acl_get_fd(int fd)
1515 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
1516 + int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
1517 + char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
1518 + SMB_ACL_T sys_acl_init( int count)
1519 + int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1520 + int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1521 + int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1522 + int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1523 + int sys_acl_valid( SMB_ACL_T theacl )
1524 + int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1525 + int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1526 + int sys_acl_delete_def_file(const char *path)
1528 + This next one is not POSIX complient - but we *have* to have it !
1529 + More POSIX braindamage.
1531 + int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1533 + The generic POSIX free is the following call. We split this into
1534 + several different free functions as we may need to add tag info
1535 + to structures when emulating the POSIX interface.
1537 + int sys_acl_free( void *obj_p)
1539 + The calls we actually use are :
1541 + int sys_acl_free_text(char *text) - free acl_to_text
1542 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
1543 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
1547 +#if defined(HAVE_POSIX_ACLS)
1549 +/* Identity mapping - easy. */
1551 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1553 + return acl_get_entry( the_acl, entry_id, entry_p);
1556 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1558 + return acl_get_tag_type( entry_d, tag_type_p);
1561 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1563 + return acl_get_permset( entry_d, permset_p);
1566 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1568 + return acl_get_qualifier( entry_d);
1571 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1573 + return acl_get_file( path_p, type);
1576 +SMB_ACL_T sys_acl_get_fd(int fd)
1578 + return acl_get_fd(fd);
1581 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1583 + return acl_clear_perms(permset);
1586 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1588 + return acl_add_perm(permset, perm);
1591 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1593 +#if defined(HAVE_ACL_GET_PERM_NP)
1595 + * Required for TrustedBSD-based ACL implementations where
1596 + * non-POSIX.1e functions are denoted by a _np (non-portable)
1599 + return acl_get_perm_np(permset, perm);
1601 + return acl_get_perm(permset, perm);
1605 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1607 + return acl_to_text( the_acl, plen);
1610 +SMB_ACL_T sys_acl_init( int count)
1612 + return acl_init(count);
1615 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1617 + return acl_create_entry(pacl, pentry);
1620 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1622 + return acl_set_tag_type(entry, tagtype);
1625 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1627 + return acl_set_qualifier(entry, qual);
1630 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1632 + return acl_set_permset(entry, permset);
1635 +int sys_acl_valid( SMB_ACL_T theacl )
1637 + return acl_valid(theacl);
1640 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1642 + return acl_set_file(name, acltype, theacl);
1645 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1647 + return acl_set_fd(fd, theacl);
1650 +int sys_acl_delete_def_file(const char *name)
1652 + return acl_delete_def_file(name);
1655 +int sys_acl_free_text(char *text)
1657 + return acl_free(text);
1660 +int sys_acl_free_acl(SMB_ACL_T the_acl)
1662 + return acl_free(the_acl);
1665 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
1667 + return acl_free(qual);
1670 +#elif defined(HAVE_TRU64_ACLS)
1672 + * The interface to DEC/Compaq Tru64 UNIX ACLs
1673 + * is based on Draft 13 of the POSIX spec which is
1674 + * slightly different from the Draft 16 interface.
1676 + * Also, some of the permset manipulation functions
1677 + * such as acl_clear_perm() and acl_add_perm() appear
1678 + * to be broken on Tru64 so we have to manipulate
1679 + * the permission bits in the permset directly.
1681 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1683 + SMB_ACL_ENTRY_T entry;
1685 + if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
1690 + if ((entry = acl_get_entry(the_acl)) != NULL) {
1695 + return errno ? -1 : 0;
1698 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1700 + return acl_get_tag_type( entry_d, tag_type_p);
1703 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1705 + return acl_get_permset( entry_d, permset_p);
1708 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1710 + return acl_get_qualifier( entry_d);
1713 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1715 + return acl_get_file((char *)path_p, type);
1718 +SMB_ACL_T sys_acl_get_fd(int fd)
1720 + return acl_get_fd(fd, ACL_TYPE_ACCESS);
1723 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1725 + *permset = 0; /* acl_clear_perm() is broken on Tru64 */
1730 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1732 + if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
1737 + *permset |= perm; /* acl_add_perm() is broken on Tru64 */
1742 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1744 + return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
1747 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1749 + return acl_to_text( the_acl, plen);
1752 +SMB_ACL_T sys_acl_init( int count)
1754 + return acl_init(count);
1757 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1759 + SMB_ACL_ENTRY_T entry;
1761 + if ((entry = acl_create_entry(pacl)) == NULL) {
1769 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1771 + return acl_set_tag_type(entry, tagtype);
1774 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1776 + return acl_set_qualifier(entry, qual);
1779 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1781 + return acl_set_permset(entry, permset);
1784 +int sys_acl_valid( SMB_ACL_T theacl )
1786 + acl_entry_t entry;
1788 + return acl_valid(theacl, &entry);
1791 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1793 + return acl_set_file((char *)name, acltype, theacl);
1796 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1798 + return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
1801 +int sys_acl_delete_def_file(const char *name)
1803 + return acl_delete_def_file((char *)name);
1806 +int sys_acl_free_text(char *text)
1809 + * (void) cast and explicit return 0 are for DEC UNIX
1810 + * which just #defines acl_free_text() to be free()
1812 + (void) acl_free_text(text);
1816 +int sys_acl_free_acl(SMB_ACL_T the_acl)
1818 + return acl_free(the_acl);
1821 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
1823 + return acl_free_qualifier(qual, tagtype);
1826 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
1829 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
1830 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
1834 + * Note that while this code implements sufficient functionality
1835 + * to support the sys_acl_* interfaces it does not provide all
1836 + * of the semantics of the POSIX ACL interfaces.
1838 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
1839 + * from a call to sys_acl_get_entry() should not be assumed to be
1840 + * valid after calling any of the following functions, which may
1841 + * reorder the entries in the ACL.
1844 + * sys_acl_set_file()
1845 + * sys_acl_set_fd()
1849 + * The only difference between Solaris and UnixWare / OpenUNIX is
1850 + * that the #defines for the ACL operations have different names
1852 +#if defined(HAVE_UNIXWARE_ACLS)
1854 +#define SETACL ACL_SET
1855 +#define GETACL ACL_GET
1856 +#define GETACLCNT ACL_CNT
1861 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1863 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
1868 + if (entry_p == NULL) {
1873 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
1877 + if (acl_d->next < 0) {
1882 + if (acl_d->next >= acl_d->count) {
1886 + *entry_p = &acl_d->acl[acl_d->next++];
1891 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
1893 + *type_p = entry_d->a_type;
1898 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1900 + *permset_p = &entry_d->a_perm;
1905 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
1907 + if (entry_d->a_type != SMB_ACL_USER
1908 + && entry_d->a_type != SMB_ACL_GROUP) {
1913 + return &entry_d->a_id;
1917 + * There is no way of knowing what size the ACL returned by
1918 + * GETACL will be unless you first call GETACLCNT which means
1919 + * making an additional system call.
1921 + * In the hope of avoiding the cost of the additional system
1922 + * call in most cases, we initially allocate enough space for
1923 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
1924 + * be too small then we use GETACLCNT to find out the actual
1925 + * size, reallocate the ACL buffer, and then call GETACL again.
1928 +#define INITIAL_ACL_SIZE 16
1930 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
1933 + int count; /* # of ACL entries allocated */
1934 + int naccess; /* # of access ACL entries */
1935 + int ndefault; /* # of default ACL entries */
1937 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
1942 + count = INITIAL_ACL_SIZE;
1943 + if ((acl_d = sys_acl_init(count)) == NULL) {
1948 + * If there isn't enough space for the ACL entries we use
1949 + * GETACLCNT to determine the actual number of ACL entries
1950 + * reallocate and try again. This is in a loop because it
1951 + * is possible that someone else could modify the ACL and
1952 + * increase the number of entries between the call to
1953 + * GETACLCNT and the call to GETACL.
1955 + while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
1956 + && errno == ENOSPC) {
1958 + sys_acl_free_acl(acl_d);
1960 + if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
1964 + if ((acl_d = sys_acl_init(count)) == NULL) {
1970 + sys_acl_free_acl(acl_d);
1975 + * calculate the number of access and default ACL entries
1977 + * Note: we assume that the acl() system call returned a
1978 + * well formed ACL which is sorted so that all of the
1979 + * access ACL entries preceed any default ACL entries
1981 + for (naccess = 0; naccess < count; naccess++) {
1982 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
1985 + ndefault = count - naccess;
1988 + * if the caller wants the default ACL we have to copy
1989 + * the entries down to the start of the acl[] buffer
1990 + * and mask out the ACL_DEFAULT flag from the type field
1992 + if (type == SMB_ACL_TYPE_DEFAULT) {
1995 + for (i = 0, j = naccess; i < ndefault; i++, j++) {
1996 + acl_d->acl[i] = acl_d->acl[j];
1997 + acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2000 + acl_d->count = ndefault;
2002 + acl_d->count = naccess;
2008 +SMB_ACL_T sys_acl_get_fd(int fd)
2011 + int count; /* # of ACL entries allocated */
2012 + int naccess; /* # of access ACL entries */
2014 + count = INITIAL_ACL_SIZE;
2015 + if ((acl_d = sys_acl_init(count)) == NULL) {
2019 + while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2020 + && errno == ENOSPC) {
2022 + sys_acl_free_acl(acl_d);
2024 + if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2028 + if ((acl_d = sys_acl_init(count)) == NULL) {
2034 + sys_acl_free_acl(acl_d);
2039 + * calculate the number of access ACL entries
2041 + for (naccess = 0; naccess < count; naccess++) {
2042 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2046 + acl_d->count = naccess;
2051 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2058 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2060 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2061 + && perm != SMB_ACL_EXECUTE) {
2066 + if (permset_d == NULL) {
2071 + *permset_d |= perm;
2076 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2078 + return *permset_d & perm;
2081 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2088 + * use an initial estimate of 20 bytes per ACL entry
2089 + * when allocating memory for the text representation
2093 + maxlen = 20 * acl_d->count;
2094 + if ((text = SMB_MALLOC(maxlen)) == NULL) {
2099 + for (i = 0; i < acl_d->count; i++) {
2100 + struct acl *ap = &acl_d->acl[i];
2101 + struct passwd *pw;
2110 + switch (ap->a_type) {
2112 + * for debugging purposes it's probably more
2113 + * useful to dump unknown tag types rather
2114 + * than just returning an error
2117 + slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2120 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2125 + case SMB_ACL_USER:
2126 + id = uidtoname(ap->a_id);
2127 + case SMB_ACL_USER_OBJ:
2131 + case SMB_ACL_GROUP:
2132 + if ((gr = getgrgid(ap->a_id)) == NULL) {
2133 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2139 + case SMB_ACL_GROUP_OBJ:
2143 + case SMB_ACL_OTHER:
2147 + case SMB_ACL_MASK:
2153 + perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2154 + perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2155 + perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2158 + /* <tag> : <qualifier> : rwx \n \0 */
2159 + nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2162 + * If this entry would overflow the buffer
2163 + * allocate enough additional memory for this
2164 + * entry and an estimate of another 20 bytes
2165 + * for each entry still to be processed
2167 + if ((len + nbytes) > maxlen) {
2168 + char *oldtext = text;
2170 + maxlen += nbytes + 20 * (acl_d->count - i);
2172 + if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2173 + SAFE_FREE(oldtext);
2179 + slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2180 + len += nbytes - 1;
2189 +SMB_ACL_T sys_acl_init(int count)
2199 + * note that since the definition of the structure pointed
2200 + * to by the SMB_ACL_T includes the first element of the
2201 + * acl[] array, this actually allocates an ACL with room
2202 + * for (count+1) entries
2204 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2209 + a->size = count + 1;
2217 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2220 + SMB_ACL_ENTRY_T entry_d;
2222 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2227 + if (acl_d->count >= acl_d->size) {
2232 + entry_d = &acl_d->acl[acl_d->count++];
2233 + entry_d->a_type = 0;
2234 + entry_d->a_id = -1;
2235 + entry_d->a_perm = 0;
2236 + *entry_p = entry_d;
2241 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2243 + switch (tag_type) {
2244 + case SMB_ACL_USER:
2245 + case SMB_ACL_USER_OBJ:
2246 + case SMB_ACL_GROUP:
2247 + case SMB_ACL_GROUP_OBJ:
2248 + case SMB_ACL_OTHER:
2249 + case SMB_ACL_MASK:
2250 + entry_d->a_type = tag_type;
2260 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2262 + if (entry_d->a_type != SMB_ACL_GROUP
2263 + && entry_d->a_type != SMB_ACL_USER) {
2268 + entry_d->a_id = *((id_t *)qual_p);
2273 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2275 + if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2279 + entry_d->a_perm = *permset_d;
2285 + * sort the ACL and check it for validity
2287 + * if it's a minimal ACL with only 4 entries then we
2288 + * need to recalculate the mask permissions to make
2289 + * sure that they are the same as the GROUP_OBJ
2290 + * permissions as required by the UnixWare acl() system call.
2292 + * (note: since POSIX allows minimal ACLs which only contain
2293 + * 3 entries - ie there is no mask entry - we should, in theory,
2294 + * check for this and add a mask entry if necessary - however
2295 + * we "know" that the caller of this interface always specifies
2296 + * a mask so, in practice "this never happens" (tm) - if it *does*
2297 + * happen aclsort() will fail and return an error and someone will
2298 + * have to fix it ...)
2301 +static int acl_sort(SMB_ACL_T acl_d)
2303 + int fixmask = (acl_d->count <= 4);
2305 + if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
2312 +int sys_acl_valid(SMB_ACL_T acl_d)
2314 + return acl_sort(acl_d);
2317 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
2320 + struct acl *acl_p;
2322 + struct acl *acl_buf = NULL;
2325 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2330 + if (acl_sort(acl_d) != 0) {
2334 + acl_p = &acl_d->acl[0];
2335 + acl_count = acl_d->count;
2338 + * if it's a directory there is extra work to do
2339 + * since the acl() system call will replace both
2340 + * the access ACLs and the default ACLs (if any)
2342 + if (stat(name, &s) != 0) {
2345 + if (S_ISDIR(s.st_mode)) {
2346 + SMB_ACL_T acc_acl;
2347 + SMB_ACL_T def_acl;
2348 + SMB_ACL_T tmp_acl;
2351 + if (type == SMB_ACL_TYPE_ACCESS) {
2353 + def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
2357 + acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
2360 + if (tmp_acl == NULL) {
2365 + * allocate a temporary buffer for the complete ACL
2367 + acl_count = acc_acl->count + def_acl->count;
2368 + acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
2370 + if (acl_buf == NULL) {
2371 + sys_acl_free_acl(tmp_acl);
2377 + * copy the access control and default entries into the buffer
2379 + memcpy(&acl_buf[0], &acc_acl->acl[0],
2380 + acc_acl->count * sizeof(acl_buf[0]));
2382 + memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
2383 + def_acl->count * sizeof(acl_buf[0]));
2386 + * set the ACL_DEFAULT flag on the default entries
2388 + for (i = acc_acl->count; i < acl_count; i++) {
2389 + acl_buf[i].a_type |= ACL_DEFAULT;
2392 + sys_acl_free_acl(tmp_acl);
2394 + } else if (type != SMB_ACL_TYPE_ACCESS) {
2399 + ret = acl(name, SETACL, acl_count, acl_p);
2401 + SAFE_FREE(acl_buf);
2406 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
2408 + if (acl_sort(acl_d) != 0) {
2412 + return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
2415 +int sys_acl_delete_def_file(const char *path)
2421 + * fetching the access ACL and rewriting it has
2422 + * the effect of deleting the default ACL
2424 + if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
2428 + ret = acl(path, SETACL, acl_d->count, acl_d->acl);
2430 + sys_acl_free_acl(acl_d);
2435 +int sys_acl_free_text(char *text)
2441 +int sys_acl_free_acl(SMB_ACL_T acl_d)
2447 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2452 +#elif defined(HAVE_HPUX_ACLS)
2456 + * Based on the Solaris/SCO code - with modifications.
2460 + * Note that while this code implements sufficient functionality
2461 + * to support the sys_acl_* interfaces it does not provide all
2462 + * of the semantics of the POSIX ACL interfaces.
2464 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2465 + * from a call to sys_acl_get_entry() should not be assumed to be
2466 + * valid after calling any of the following functions, which may
2467 + * reorder the entries in the ACL.
2470 + * sys_acl_set_file()
2471 + * sys_acl_set_fd()
2474 +/* This checks if the POSIX ACL system call is defined */
2475 +/* which basically corresponds to whether JFS 3.3 or */
2476 +/* higher is installed. If acl() was called when it */
2477 +/* isn't defined, it causes the process to core dump */
2478 +/* so it is important to check this and avoid acl() */
2479 +/* calls if it isn't there. */
2481 +static BOOL hpux_acl_call_presence(void)
2484 + shl_t handle = NULL;
2487 + static BOOL already_checked=0;
2489 + if(already_checked)
2493 + ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
2495 + if(ret_val != 0) {
2496 + DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
2497 + ret_val, errno, strerror(errno)));
2498 + DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
2502 + DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
2504 + already_checked = True;
2508 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2510 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2515 + if (entry_p == NULL) {
2520 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
2524 + if (acl_d->next < 0) {
2529 + if (acl_d->next >= acl_d->count) {
2533 + *entry_p = &acl_d->acl[acl_d->next++];
2538 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2540 + *type_p = entry_d->a_type;
2545 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2547 + *permset_p = &entry_d->a_perm;
2552 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2554 + if (entry_d->a_type != SMB_ACL_USER
2555 + && entry_d->a_type != SMB_ACL_GROUP) {
2560 + return &entry_d->a_id;
2564 + * There is no way of knowing what size the ACL returned by
2565 + * ACL_GET will be unless you first call ACL_CNT which means
2566 + * making an additional system call.
2568 + * In the hope of avoiding the cost of the additional system
2569 + * call in most cases, we initially allocate enough space for
2570 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2571 + * be too small then we use ACL_CNT to find out the actual
2572 + * size, reallocate the ACL buffer, and then call ACL_GET again.
2575 +#define INITIAL_ACL_SIZE 16
2577 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2580 + int count; /* # of ACL entries allocated */
2581 + int naccess; /* # of access ACL entries */
2582 + int ndefault; /* # of default ACL entries */
2584 + if(hpux_acl_call_presence() == False) {
2585 + /* Looks like we don't have the acl() system call on HPUX.
2586 + * May be the system doesn't have the latest version of JFS.
2591 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2596 + count = INITIAL_ACL_SIZE;
2597 + if ((acl_d = sys_acl_init(count)) == NULL) {
2602 + * If there isn't enough space for the ACL entries we use
2603 + * ACL_CNT to determine the actual number of ACL entries
2604 + * reallocate and try again. This is in a loop because it
2605 + * is possible that someone else could modify the ACL and
2606 + * increase the number of entries between the call to
2607 + * ACL_CNT and the call to ACL_GET.
2609 + while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
2611 + sys_acl_free_acl(acl_d);
2613 + if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
2617 + if ((acl_d = sys_acl_init(count)) == NULL) {
2623 + sys_acl_free_acl(acl_d);
2628 + * calculate the number of access and default ACL entries
2630 + * Note: we assume that the acl() system call returned a
2631 + * well formed ACL which is sorted so that all of the
2632 + * access ACL entries preceed any default ACL entries
2634 + for (naccess = 0; naccess < count; naccess++) {
2635 + if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2638 + ndefault = count - naccess;
2641 + * if the caller wants the default ACL we have to copy
2642 + * the entries down to the start of the acl[] buffer
2643 + * and mask out the ACL_DEFAULT flag from the type field
2645 + if (type == SMB_ACL_TYPE_DEFAULT) {
2648 + for (i = 0, j = naccess; i < ndefault; i++, j++) {
2649 + acl_d->acl[i] = acl_d->acl[j];
2650 + acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2653 + acl_d->count = ndefault;
2655 + acl_d->count = naccess;
2661 +SMB_ACL_T sys_acl_get_fd(int fd)
2664 + * HPUX doesn't have the facl call. Fake it using the path.... JRA.
2667 + files_struct *fsp = file_find_fd(fd);
2669 + if (fsp == NULL) {
2675 + * We know we're in the same conn context. So we
2676 + * can use the relative path.
2679 + return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
2682 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2689 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2691 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2692 + && perm != SMB_ACL_EXECUTE) {
2697 + if (permset_d == NULL) {
2702 + *permset_d |= perm;
2707 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2709 + return *permset_d & perm;
2712 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2719 + * use an initial estimate of 20 bytes per ACL entry
2720 + * when allocating memory for the text representation
2724 + maxlen = 20 * acl_d->count;
2725 + if ((text = SMB_MALLOC(maxlen)) == NULL) {
2730 + for (i = 0; i < acl_d->count; i++) {
2731 + struct acl *ap = &acl_d->acl[i];
2732 + struct passwd *pw;
2741 + switch (ap->a_type) {
2743 + * for debugging purposes it's probably more
2744 + * useful to dump unknown tag types rather
2745 + * than just returning an error
2748 + slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2751 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2756 + case SMB_ACL_USER:
2757 + id = uidtoname(ap->a_id);
2758 + case SMB_ACL_USER_OBJ:
2762 + case SMB_ACL_GROUP:
2763 + if ((gr = getgrgid(ap->a_id)) == NULL) {
2764 + slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2770 + case SMB_ACL_GROUP_OBJ:
2774 + case SMB_ACL_OTHER:
2778 + case SMB_ACL_MASK:
2784 + perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2785 + perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2786 + perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2789 + /* <tag> : <qualifier> : rwx \n \0 */
2790 + nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2793 + * If this entry would overflow the buffer
2794 + * allocate enough additional memory for this
2795 + * entry and an estimate of another 20 bytes
2796 + * for each entry still to be processed
2798 + if ((len + nbytes) > maxlen) {
2799 + char *oldtext = text;
2801 + maxlen += nbytes + 20 * (acl_d->count - i);
2803 + if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2810 + slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2811 + len += nbytes - 1;
2820 +SMB_ACL_T sys_acl_init(int count)
2830 + * note that since the definition of the structure pointed
2831 + * to by the SMB_ACL_T includes the first element of the
2832 + * acl[] array, this actually allocates an ACL with room
2833 + * for (count+1) entries
2835 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2840 + a->size = count + 1;
2848 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2851 + SMB_ACL_ENTRY_T entry_d;
2853 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2858 + if (acl_d->count >= acl_d->size) {
2863 + entry_d = &acl_d->acl[acl_d->count++];
2864 + entry_d->a_type = 0;
2865 + entry_d->a_id = -1;
2866 + entry_d->a_perm = 0;
2867 + *entry_p = entry_d;
2872 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2874 + switch (tag_type) {
2875 + case SMB_ACL_USER:
2876 + case SMB_ACL_USER_OBJ:
2877 + case SMB_ACL_GROUP:
2878 + case SMB_ACL_GROUP_OBJ:
2879 + case SMB_ACL_OTHER:
2880 + case SMB_ACL_MASK:
2881 + entry_d->a_type = tag_type;
2891 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2893 + if (entry_d->a_type != SMB_ACL_GROUP
2894 + && entry_d->a_type != SMB_ACL_USER) {
2899 + entry_d->a_id = *((id_t *)qual_p);
2904 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2906 + if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2910 + entry_d->a_perm = *permset_d;
2915 +/* Structure to capture the count for each type of ACE. */
2917 +struct hpux_acl_types {
2921 + int n_def_user_obj;
2926 + int n_def_group_obj;
2930 + int n_def_other_obj;
2933 + int n_def_class_obj;
2935 + int n_illegal_obj;
2939 + * Counts the different number of objects in a given array of ACL
2943 + * acl_count - Count of ACLs in the array of ACL strucutres.
2944 + * aclp - Array of ACL structures.
2945 + * acl_type_count - Pointer to acl_types structure. Should already be
2949 + * acl_type_count - This structure is filled up with counts of various
2953 +static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
2957 + memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
2959 + for(i=0;i<acl_count;i++) {
2960 + switch(aclp[i].a_type) {
2962 + acl_type_count->n_user++;
2965 + acl_type_count->n_user_obj++;
2967 + case DEF_USER_OBJ:
2968 + acl_type_count->n_def_user_obj++;
2971 + acl_type_count->n_group++;
2974 + acl_type_count->n_group_obj++;
2976 + case DEF_GROUP_OBJ:
2977 + acl_type_count->n_def_group_obj++;
2980 + acl_type_count->n_other_obj++;
2982 + case DEF_OTHER_OBJ:
2983 + acl_type_count->n_def_other_obj++;
2986 + acl_type_count->n_class_obj++;
2988 + case DEF_CLASS_OBJ:
2989 + acl_type_count->n_def_class_obj++;
2992 + acl_type_count->n_def_user++;
2995 + acl_type_count->n_def_group++;
2998 + acl_type_count->n_illegal_obj++;
3004 +/* swap_acl_entries: Swaps two ACL entries.
3006 + * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3009 +static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3011 + struct acl temp_acl;
3013 + temp_acl.a_type = aclp0->a_type;
3014 + temp_acl.a_id = aclp0->a_id;
3015 + temp_acl.a_perm = aclp0->a_perm;
3017 + aclp0->a_type = aclp1->a_type;
3018 + aclp0->a_id = aclp1->a_id;
3019 + aclp0->a_perm = aclp1->a_perm;
3021 + aclp1->a_type = temp_acl.a_type;
3022 + aclp1->a_id = temp_acl.a_id;
3023 + aclp1->a_perm = temp_acl.a_perm;
3026 +/* prohibited_duplicate_type
3027 + * Identifies if given ACL type can have duplicate entries or
3030 + * Inputs: acl_type - ACL Type.
3036 + * True - If the ACL type matches any of the prohibited types.
3037 + * False - If the ACL type doesn't match any of the prohibited types.
3040 +static BOOL hpux_prohibited_duplicate_type(int acl_type)
3042 + switch(acl_type) {
3053 +/* get_needed_class_perm
3054 + * Returns the permissions of a ACL structure only if the ACL
3055 + * type matches one of the pre-determined types for computing
3056 + * CLASS_OBJ permissions.
3058 + * Inputs: aclp - Pointer to ACL structure.
3061 +static int hpux_get_needed_class_perm(struct acl *aclp)
3063 + switch(aclp->a_type) {
3067 + case DEF_USER_OBJ:
3069 + case DEF_GROUP_OBJ:
3071 + case DEF_CLASS_OBJ:
3072 + case DEF_OTHER_OBJ:
3073 + return aclp->a_perm;
3079 +/* acl_sort for HPUX.
3080 + * Sorts the array of ACL structures as per the description in
3081 + * aclsort man page. Refer to aclsort man page for more details
3085 + * acl_count - Count of ACLs in the array of ACL structures.
3086 + * calclass - If this is not zero, then we compute the CLASS_OBJ
3088 + * aclp - Array of ACL structures.
3092 + * aclp - Sorted array of ACL structures.
3096 + * Returns 0 for success -1 for failure. Prints a message to the Samba
3097 + * debug log in case of failure.
3100 +static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3102 +#if !defined(HAVE_HPUX_ACLSORT)
3104 + * The aclsort() system call is availabe on the latest HPUX General
3105 + * Patch Bundles. So for HPUX, we developed our version of acl_sort
3106 + * function. Because, we don't want to update to a new
3107 + * HPUX GR bundle just for aclsort() call.
3110 + struct hpux_acl_types acl_obj_count;
3111 + int n_class_obj_perm = 0;
3115 + DEBUG(10,("Zero acl count passed. Returning Success\n"));
3119 + if(aclp == NULL) {
3120 + DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3124 + /* Count different types of ACLs in the ACLs array */
3126 + hpux_count_obj(acl_count, aclp, &acl_obj_count);
3128 + /* There should be only one entry each of type USER_OBJ, GROUP_OBJ,
3129 + * CLASS_OBJ and OTHER_OBJ
3132 + if( (acl_obj_count.n_user_obj != 1) ||
3133 + (acl_obj_count.n_group_obj != 1) ||
3134 + (acl_obj_count.n_class_obj != 1) ||
3135 + (acl_obj_count.n_other_obj != 1)
3137 + DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3138 +USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3142 + /* If any of the default objects are present, there should be only
3143 + * one of them each.
3146 + if( (acl_obj_count.n_def_user_obj > 1) || (acl_obj_count.n_def_group_obj > 1) ||
3147 + (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
3148 + DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3149 +or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3153 + /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl
3156 + * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3157 + * same ACL type, sort by ACL id.
3159 + * I am using the trival kind of sorting method here because, performance isn't
3160 + * really effected by the ACLs feature. More over there aren't going to be more
3161 + * than 17 entries on HPUX.
3164 + for(i=0; i<acl_count;i++) {
3165 + for (j=i+1; j<acl_count; j++) {
3166 + if( aclp[i].a_type > aclp[j].a_type ) {
3167 + /* ACL entries out of order, swap them */
3169 + hpux_swap_acl_entries((aclp+i), (aclp+j));
3171 + } else if ( aclp[i].a_type == aclp[j].a_type ) {
3173 + /* ACL entries of same type, sort by id */
3175 + if(aclp[i].a_id > aclp[j].a_id) {
3176 + hpux_swap_acl_entries((aclp+i), (aclp+j));
3177 + } else if (aclp[i].a_id == aclp[j].a_id) {
3178 + /* We have a duplicate entry. */
3179 + if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
3180 + DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3181 + aclp[i].a_type, aclp[i].a_id));
3190 + /* set the class obj permissions to the computed one. */
3192 + int n_class_obj_index = -1;
3194 + for(i=0;i<acl_count;i++) {
3195 + n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3197 + if(aclp[i].a_type == CLASS_OBJ)
3198 + n_class_obj_index = i;
3200 + aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3205 + return aclsort(acl_count, calclass, aclp);
3210 + * sort the ACL and check it for validity
3212 + * if it's a minimal ACL with only 4 entries then we
3213 + * need to recalculate the mask permissions to make
3214 + * sure that they are the same as the GROUP_OBJ
3215 + * permissions as required by the UnixWare acl() system call.
3217 + * (note: since POSIX allows minimal ACLs which only contain
3218 + * 3 entries - ie there is no mask entry - we should, in theory,
3219 + * check for this and add a mask entry if necessary - however
3220 + * we "know" that the caller of this interface always specifies
3221 + * a mask so, in practice "this never happens" (tm) - if it *does*
3222 + * happen aclsort() will fail and return an error and someone will
3223 + * have to fix it ...)
3226 +static int acl_sort(SMB_ACL_T acl_d)
3228 + int fixmask = (acl_d->count <= 4);
3230 + if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
3237 +int sys_acl_valid(SMB_ACL_T acl_d)
3239 + return acl_sort(acl_d);
3242 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3245 + struct acl *acl_p;
3247 + struct acl *acl_buf = NULL;
3250 + if(hpux_acl_call_presence() == False) {
3251 + /* Looks like we don't have the acl() system call on HPUX.
3252 + * May be the system doesn't have the latest version of JFS.
3258 + if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3263 + if (acl_sort(acl_d) != 0) {
3267 + acl_p = &acl_d->acl[0];
3268 + acl_count = acl_d->count;
3271 + * if it's a directory there is extra work to do
3272 + * since the acl() system call will replace both
3273 + * the access ACLs and the default ACLs (if any)
3275 + if (stat(name, &s) != 0) {
3278 + if (S_ISDIR(s.st_mode)) {
3279 + SMB_ACL_T acc_acl;
3280 + SMB_ACL_T def_acl;
3281 + SMB_ACL_T tmp_acl;
3284 + if (type == SMB_ACL_TYPE_ACCESS) {
3286 + def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3290 + acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3293 + if (tmp_acl == NULL) {
3298 + * allocate a temporary buffer for the complete ACL
3300 + acl_count = acc_acl->count + def_acl->count;
3301 + acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
3303 + if (acl_buf == NULL) {
3304 + sys_acl_free_acl(tmp_acl);
3310 + * copy the access control and default entries into the buffer
3312 + memcpy(&acl_buf[0], &acc_acl->acl[0],
3313 + acc_acl->count * sizeof(acl_buf[0]));
3315 + memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3316 + def_acl->count * sizeof(acl_buf[0]));
3319 + * set the ACL_DEFAULT flag on the default entries
3321 + for (i = acc_acl->count; i < acl_count; i++) {
3322 + acl_buf[i].a_type |= ACL_DEFAULT;
3325 + sys_acl_free_acl(tmp_acl);
3327 + } else if (type != SMB_ACL_TYPE_ACCESS) {
3332 + ret = acl(name, ACL_SET, acl_count, acl_p);
3341 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3344 + * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3347 + files_struct *fsp = file_find_fd(fd);
3349 + if (fsp == NULL) {
3354 + if (acl_sort(acl_d) != 0) {
3359 + * We know we're in the same conn context. So we
3360 + * can use the relative path.
3363 + return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
3366 +int sys_acl_delete_def_file(const char *path)
3372 + * fetching the access ACL and rewriting it has
3373 + * the effect of deleting the default ACL
3375 + if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3379 + ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
3381 + sys_acl_free_acl(acl_d);
3386 +int sys_acl_free_text(char *text)
3392 +int sys_acl_free_acl(SMB_ACL_T acl_d)
3398 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3403 +#elif defined(HAVE_IRIX_ACLS)
3405 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3407 + if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3412 + if (entry_p == NULL) {
3417 + if (entry_id == SMB_ACL_FIRST_ENTRY) {
3421 + if (acl_d->next < 0) {
3426 + if (acl_d->next >= acl_d->aclp->acl_cnt) {
3430 + *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
3435 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
3437 + *type_p = entry_d->ae_tag;
3442 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3444 + *permset_p = entry_d;
3449 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3451 + if (entry_d->ae_tag != SMB_ACL_USER
3452 + && entry_d->ae_tag != SMB_ACL_GROUP) {
3457 + return &entry_d->ae_id;
3460 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3464 + if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3468 + if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
3473 + a->freeaclp = True;
3477 +SMB_ACL_T sys_acl_get_fd(int fd)
3481 + if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3485 + if ((a->aclp = acl_get_fd(fd)) == NULL) {
3490 + a->freeaclp = True;
3494 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3496 + permset_d->ae_perm = 0;
3501 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3503 + if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3504 + && perm != SMB_ACL_EXECUTE) {
3509 + if (permset_d == NULL) {
3514 + permset_d->ae_perm |= perm;
3519 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3521 + return permset_d->ae_perm & perm;
3524 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3526 + return acl_to_text(acl_d->aclp, len_p);
3529 +SMB_ACL_T sys_acl_init(int count)
3538 + if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
3544 + a->freeaclp = False;
3545 + a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
3546 + a->aclp->acl_cnt = 0;
3552 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3555 + SMB_ACL_ENTRY_T entry_d;
3557 + if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3562 + if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
3567 + entry_d = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
3568 + entry_d->ae_tag = 0;
3569 + entry_d->ae_id = 0;
3570 + entry_d->ae_perm = 0;
3571 + *entry_p = entry_d;
3576 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3578 + switch (tag_type) {
3579 + case SMB_ACL_USER:
3580 + case SMB_ACL_USER_OBJ:
3581 + case SMB_ACL_GROUP:
3582 + case SMB_ACL_GROUP_OBJ:
3583 + case SMB_ACL_OTHER:
3584 + case SMB_ACL_MASK:
3585 + entry_d->ae_tag = tag_type;
3595 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3597 + if (entry_d->ae_tag != SMB_ACL_GROUP
3598 + && entry_d->ae_tag != SMB_ACL_USER) {
3603 + entry_d->ae_id = *((id_t *)qual_p);
3608 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3610 + if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3614 + entry_d->ae_perm = permset_d->ae_perm;
3619 +int sys_acl_valid(SMB_ACL_T acl_d)
3621 + return acl_valid(acl_d->aclp);
3624 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3626 + return acl_set_file(name, type, acl_d->aclp);
3629 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3631 + return acl_set_fd(fd, acl_d->aclp);
3634 +int sys_acl_delete_def_file(const char *name)
3636 + return acl_delete_def_file(name);
3639 +int sys_acl_free_text(char *text)
3641 + return acl_free(text);
3644 +int sys_acl_free_acl(SMB_ACL_T acl_d)
3646 + if (acl_d->freeaclp) {
3647 + acl_free(acl_d->aclp);
3653 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3658 +#elif defined(HAVE_AIX_ACLS)
3660 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
3662 +int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3664 + struct acl_entry_link *link;
3665 + struct new_acl_entry *entry;
3668 + DEBUG(10,("This is the count: %d\n",theacl->count));
3670 + /* Check if count was previously set to -1. *
3671 + * If it was, that means we reached the end *
3672 + * of the acl last time. */
3673 + if(theacl->count == -1)
3677 + /* To get to the next acl, traverse linked list until index *
3678 + * of acl matches the count we are keeping. This count is *
3679 + * incremented each time we return an acl entry. */
3681 + for(keep_going = 0; keep_going < theacl->count; keep_going++)
3682 + link = link->nextp;
3684 + entry = *entry_p = link->entryp;
3686 + DEBUG(10,("*entry_p is %d\n",entry_p));
3687 + DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
3689 + /* Increment count */
3691 + if(link->nextp == NULL)
3692 + theacl->count = -1;
3697 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
3699 + /* Initialize tag type */
3702 + DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
3704 + /* Depending on what type of entry we have, *
3705 + * return tag type. */
3706 + switch(entry_d->ace_id->id_type) {
3708 + *tag_type_p = SMB_ACL_USER;
3711 + *tag_type_p = SMB_ACL_GROUP;
3714 + case SMB_ACL_USER_OBJ:
3715 + case SMB_ACL_GROUP_OBJ:
3716 + case SMB_ACL_OTHER:
3717 + *tag_type_p = entry_d->ace_id->id_type;
3727 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3729 + DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
3730 + *permset_p = &entry_d->ace_access;
3731 + DEBUG(10,("**permset_p is %d\n",**permset_p));
3732 + if(!(**permset_p & S_IXUSR) &&
3733 + !(**permset_p & S_IWUSR) &&
3734 + !(**permset_p & S_IRUSR) &&
3735 + (**permset_p != 0))
3738 + DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
3742 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
3744 + return(entry_d->ace_id->id_data);
3747 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
3749 + struct acl *file_acl = (struct acl *)NULL;
3750 + struct acl_entry *acl_entry;
3751 + struct new_acl_entry *new_acl_entry;
3752 + struct ace_id *idp;
3753 + struct acl_entry_link *acl_entry_link;
3754 + struct acl_entry_link *acl_entry_link_head;
3759 + /* AIX has no DEFAULT */
3760 + if ( type == SMB_ACL_TYPE_DEFAULT )
3763 + /* Get the acl using statacl */
3765 + DEBUG(10,("Entering sys_acl_get_file\n"));
3766 + DEBUG(10,("path_p is %s\n",path_p));
3768 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
3770 + if(file_acl == NULL) {
3772 + DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
3776 + memset(file_acl,0,BUFSIZ);
3778 + rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
3780 + DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
3781 + SAFE_FREE(file_acl);
3785 + DEBUG(10,("Got facl and returned it\n"));
3787 + /* Point to the first acl entry in the acl */
3788 + acl_entry = file_acl->acl_ext;
3790 + /* Begin setting up the head of the linked list *
3791 + * that will be used for the storing the acl *
3792 + * in a way that is useful for the posix_acls.c *
3795 + acl_entry_link_head = acl_entry_link = sys_acl_init(0);
3796 + if(acl_entry_link_head == NULL)
3799 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3800 + if(acl_entry_link->entryp == NULL) {
3801 + SAFE_FREE(file_acl);
3803 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3807 + DEBUG(10,("acl_entry is %d\n",acl_entry));
3808 + DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
3810 + /* Check if the extended acl bit is on. *
3811 + * If it isn't, do not show the *
3812 + * contents of the acl since AIX intends *
3813 + * the extended info to remain unused */
3815 + if(file_acl->acl_mode & S_IXACL){
3816 + /* while we are not pointing to the very end */
3817 + while(acl_entry < acl_last(file_acl)) {
3818 + /* before we malloc anything, make sure this is */
3819 + /* a valid acl entry and one that we want to map */
3820 + idp = id_nxt(acl_entry->ace_id);
3821 + if((acl_entry->ace_type == ACC_SPECIFY ||
3822 + (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
3823 + acl_entry = acl_nxt(acl_entry);
3827 + idp = acl_entry->ace_id;
3829 + /* Check if this is the first entry in the linked list. *
3830 + * The first entry needs to keep prevp pointing to NULL *
3831 + * and already has entryp allocated. */
3833 + if(acl_entry_link_head->count != 0) {
3834 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
3836 + if(acl_entry_link->nextp == NULL) {
3837 + SAFE_FREE(file_acl);
3839 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3843 + acl_entry_link->nextp->prevp = acl_entry_link;
3844 + acl_entry_link = acl_entry_link->nextp;
3845 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3846 + if(acl_entry_link->entryp == NULL) {
3847 + SAFE_FREE(file_acl);
3849 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3852 + acl_entry_link->nextp = NULL;
3855 + acl_entry_link->entryp->ace_len = acl_entry->ace_len;
3857 + /* Don't really need this since all types are going *
3858 + * to be specified but, it's better than leaving it 0 */
3860 + acl_entry_link->entryp->ace_type = acl_entry->ace_type;
3862 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3864 + memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
3866 + /* The access in the acl entries must be left shifted by *
3867 + * three bites, because they will ultimately be compared *
3868 + * to S_IRUSR, S_IWUSR, and S_IXUSR. */
3870 + switch(acl_entry->ace_type){
3873 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3874 + acl_entry_link->entryp->ace_access <<= 6;
3875 + acl_entry_link_head->count++;
3878 + /* Since there is no way to return a DENY acl entry *
3879 + * change to PERMIT and then shift. */
3880 + DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
3881 + acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
3882 + DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
3883 + acl_entry_link->entryp->ace_access <<= 6;
3884 + acl_entry_link_head->count++;
3890 + DEBUG(10,("acl_entry = %d\n",acl_entry));
3891 + DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
3893 + acl_entry = acl_nxt(acl_entry);
3895 + } /* end of if enabled */
3897 + /* Since owner, group, other acl entries are not *
3898 + * part of the acl entries in an acl, they must *
3899 + * be dummied up to become part of the list. */
3901 + for( i = 1; i < 4; i++) {
3902 + DEBUG(10,("i is %d\n",i));
3903 + if(acl_entry_link_head->count != 0) {
3904 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
3905 + if(acl_entry_link->nextp == NULL) {
3906 + SAFE_FREE(file_acl);
3908 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3912 + acl_entry_link->nextp->prevp = acl_entry_link;
3913 + acl_entry_link = acl_entry_link->nextp;
3914 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3915 + if(acl_entry_link->entryp == NULL) {
3916 + SAFE_FREE(file_acl);
3918 + DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3923 + acl_entry_link->nextp = NULL;
3925 + new_acl_entry = acl_entry_link->entryp;
3926 + idp = new_acl_entry->ace_id;
3928 + new_acl_entry->ace_len = sizeof(struct acl_entry);
3929 + new_acl_entry->ace_type = ACC_PERMIT;
3930 + idp->id_len = sizeof(struct ace_id);
3931 + DEBUG(10,("idp->id_len = %d\n",idp->id_len));
3932 + memset(idp->id_data,0,sizeof(uid_t));
3936 + new_acl_entry->ace_access = file_acl->g_access << 6;
3937 + idp->id_type = SMB_ACL_GROUP_OBJ;
3941 + new_acl_entry->ace_access = file_acl->o_access << 6;
3942 + idp->id_type = SMB_ACL_OTHER;
3946 + new_acl_entry->ace_access = file_acl->u_access << 6;
3947 + idp->id_type = SMB_ACL_USER_OBJ;
3955 + acl_entry_link_head->count++;
3956 + DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
3959 + acl_entry_link_head->count = 0;
3960 + SAFE_FREE(file_acl);
3962 + return(acl_entry_link_head);
3965 +SMB_ACL_T sys_acl_get_fd(int fd)
3967 + struct acl *file_acl = (struct acl *)NULL;
3968 + struct acl_entry *acl_entry;
3969 + struct new_acl_entry *new_acl_entry;
3970 + struct ace_id *idp;
3971 + struct acl_entry_link *acl_entry_link;
3972 + struct acl_entry_link *acl_entry_link_head;
3977 + /* Get the acl using fstatacl */
3979 + DEBUG(10,("Entering sys_acl_get_fd\n"));
3980 + DEBUG(10,("fd is %d\n",fd));
3981 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
3983 + if(file_acl == NULL) {
3985 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
3989 + memset(file_acl,0,BUFSIZ);
3991 + rc = fstatacl(fd,0,file_acl,BUFSIZ);
3993 + DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
3994 + SAFE_FREE(file_acl);
3998 + DEBUG(10,("Got facl and returned it\n"));
4000 + /* Point to the first acl entry in the acl */
4002 + acl_entry = file_acl->acl_ext;
4003 + /* Begin setting up the head of the linked list *
4004 + * that will be used for the storing the acl *
4005 + * in a way that is useful for the posix_acls.c *
4008 + acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4009 + if(acl_entry_link_head == NULL){
4010 + SAFE_FREE(file_acl);
4014 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4016 + if(acl_entry_link->entryp == NULL) {
4018 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4019 + SAFE_FREE(file_acl);
4023 + DEBUG(10,("acl_entry is %d\n",acl_entry));
4024 + DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4026 + /* Check if the extended acl bit is on. *
4027 + * If it isn't, do not show the *
4028 + * contents of the acl since AIX intends *
4029 + * the extended info to remain unused */
4031 + if(file_acl->acl_mode & S_IXACL){
4032 + /* while we are not pointing to the very end */
4033 + while(acl_entry < acl_last(file_acl)) {
4034 + /* before we malloc anything, make sure this is */
4035 + /* a valid acl entry and one that we want to map */
4037 + idp = id_nxt(acl_entry->ace_id);
4038 + if((acl_entry->ace_type == ACC_SPECIFY ||
4039 + (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4040 + acl_entry = acl_nxt(acl_entry);
4044 + idp = acl_entry->ace_id;
4046 + /* Check if this is the first entry in the linked list. *
4047 + * The first entry needs to keep prevp pointing to NULL *
4048 + * and already has entryp allocated. */
4050 + if(acl_entry_link_head->count != 0) {
4051 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4052 + if(acl_entry_link->nextp == NULL) {
4054 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4055 + SAFE_FREE(file_acl);
4058 + acl_entry_link->nextp->prevp = acl_entry_link;
4059 + acl_entry_link = acl_entry_link->nextp;
4060 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4061 + if(acl_entry_link->entryp == NULL) {
4063 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4064 + SAFE_FREE(file_acl);
4068 + acl_entry_link->nextp = NULL;
4071 + acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4073 + /* Don't really need this since all types are going *
4074 + * to be specified but, it's better than leaving it 0 */
4076 + acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4077 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4079 + memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
4081 + /* The access in the acl entries must be left shifted by *
4082 + * three bites, because they will ultimately be compared *
4083 + * to S_IRUSR, S_IWUSR, and S_IXUSR. */
4085 + switch(acl_entry->ace_type){
4088 + acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4089 + acl_entry_link->entryp->ace_access <<= 6;
4090 + acl_entry_link_head->count++;
4093 + /* Since there is no way to return a DENY acl entry *
4094 + * change to PERMIT and then shift. */
4095 + DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4096 + acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4097 + DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4098 + acl_entry_link->entryp->ace_access <<= 6;
4099 + acl_entry_link_head->count++;
4105 + DEBUG(10,("acl_entry = %d\n",acl_entry));
4106 + DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4108 + acl_entry = acl_nxt(acl_entry);
4110 + } /* end of if enabled */
4112 + /* Since owner, group, other acl entries are not *
4113 + * part of the acl entries in an acl, they must *
4114 + * be dummied up to become part of the list. */
4116 + for( i = 1; i < 4; i++) {
4117 + DEBUG(10,("i is %d\n",i));
4118 + if(acl_entry_link_head->count != 0){
4119 + acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4120 + if(acl_entry_link->nextp == NULL) {
4122 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4123 + SAFE_FREE(file_acl);
4127 + acl_entry_link->nextp->prevp = acl_entry_link;
4128 + acl_entry_link = acl_entry_link->nextp;
4129 + acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4131 + if(acl_entry_link->entryp == NULL) {
4132 + SAFE_FREE(file_acl);
4134 + DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4139 + acl_entry_link->nextp = NULL;
4141 + new_acl_entry = acl_entry_link->entryp;
4142 + idp = new_acl_entry->ace_id;
4144 + new_acl_entry->ace_len = sizeof(struct acl_entry);
4145 + new_acl_entry->ace_type = ACC_PERMIT;
4146 + idp->id_len = sizeof(struct ace_id);
4147 + DEBUG(10,("idp->id_len = %d\n",idp->id_len));
4148 + memset(idp->id_data,0,sizeof(uid_t));
4152 + new_acl_entry->ace_access = file_acl->g_access << 6;
4153 + idp->id_type = SMB_ACL_GROUP_OBJ;
4157 + new_acl_entry->ace_access = file_acl->o_access << 6;
4158 + idp->id_type = SMB_ACL_OTHER;
4162 + new_acl_entry->ace_access = file_acl->u_access << 6;
4163 + idp->id_type = SMB_ACL_USER_OBJ;
4170 + acl_entry_link_head->count++;
4171 + DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4174 + acl_entry_link_head->count = 0;
4175 + SAFE_FREE(file_acl);
4177 + return(acl_entry_link_head);
4180 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
4182 + *permset = *permset & ~0777;
4186 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4189 + (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
4193 + DEBUG(10,("This is the permset now: %d\n",*permset));
4197 +char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
4202 +SMB_ACL_T sys_acl_init( int count)
4204 + struct acl_entry_link *theacl = NULL;
4206 + DEBUG(10,("Entering sys_acl_init\n"));
4208 + theacl = SMB_MALLOC_P(struct acl_entry_link);
4209 + if(theacl == NULL) {
4211 + DEBUG(0,("Error in sys_acl_init is %d\n",errno));
4215 + theacl->count = 0;
4216 + theacl->nextp = NULL;
4217 + theacl->prevp = NULL;
4218 + theacl->entryp = NULL;
4219 + DEBUG(10,("Exiting sys_acl_init\n"));
4223 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
4225 + struct acl_entry_link *theacl;
4226 + struct acl_entry_link *acl_entryp;
4227 + struct acl_entry_link *temp_entry;
4230 + DEBUG(10,("Entering the sys_acl_create_entry\n"));
4232 + theacl = acl_entryp = *pacl;
4234 + /* Get to the end of the acl before adding entry */
4236 + for(counting=0; counting < theacl->count; counting++){
4237 + DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4238 + temp_entry = acl_entryp;
4239 + acl_entryp = acl_entryp->nextp;
4242 + if(theacl->count != 0){
4243 + temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
4244 + if(acl_entryp == NULL) {
4246 + DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4250 + DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4251 + acl_entryp->prevp = temp_entry;
4252 + DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
4255 + *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
4256 + if(*pentry == NULL) {
4258 + DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4262 + memset(*pentry,0,sizeof(struct new_acl_entry));
4263 + acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
4264 + acl_entryp->entryp->ace_type = ACC_PERMIT;
4265 + acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
4266 + acl_entryp->nextp = NULL;
4268 + DEBUG(10,("Exiting sys_acl_create_entry\n"));
4272 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
4274 + DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
4275 + entry->ace_id->id_type = tagtype;
4276 + DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
4277 + DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
4280 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
4282 + DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
4283 + memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
4284 + DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
4288 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
4290 + DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
4291 + if(!(*permset & S_IXUSR) &&
4292 + !(*permset & S_IWUSR) &&
4293 + !(*permset & S_IRUSR) &&
4297 + entry->ace_access = *permset;
4298 + DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
4299 + DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
4303 +int sys_acl_valid( SMB_ACL_T theacl )
4306 + int group_obj = 0;
4307 + int other_obj = 0;
4308 + struct acl_entry_link *acl_entry;
4310 + for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
4311 + user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
4312 + group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
4313 + other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
4316 + DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
4318 + if(user_obj != 1 || group_obj != 1 || other_obj != 1)
4324 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
4326 + struct acl_entry_link *acl_entry_link = NULL;
4327 + struct acl *file_acl = NULL;
4328 + struct acl *file_acl_temp = NULL;
4329 + struct acl_entry *acl_entry = NULL;
4330 + struct ace_id *ace_id = NULL;
4337 + DEBUG(10,("Entering sys_acl_set_file\n"));
4338 + DEBUG(10,("File name is %s\n",name));
4340 + /* AIX has no default ACL */
4341 + if(acltype == SMB_ACL_TYPE_DEFAULT)
4344 + acl_length = BUFSIZ;
4345 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4347 + if(file_acl == NULL) {
4349 + DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4353 + memset(file_acl,0,BUFSIZ);
4355 + file_acl->acl_len = ACL_SIZ;
4356 + file_acl->acl_mode = S_IXACL;
4358 + for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4359 + acl_entry_link->entryp->ace_access >>= 6;
4360 + id_type = acl_entry_link->entryp->ace_id->id_type;
4363 + case SMB_ACL_USER_OBJ:
4364 + file_acl->u_access = acl_entry_link->entryp->ace_access;
4366 + case SMB_ACL_GROUP_OBJ:
4367 + file_acl->g_access = acl_entry_link->entryp->ace_access;
4369 + case SMB_ACL_OTHER:
4370 + file_acl->o_access = acl_entry_link->entryp->ace_access;
4372 + case SMB_ACL_MASK:
4376 + if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4377 + acl_length += sizeof(struct acl_entry);
4378 + file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
4379 + if(file_acl_temp == NULL) {
4380 + SAFE_FREE(file_acl);
4382 + DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4386 + memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4387 + SAFE_FREE(file_acl);
4388 + file_acl = file_acl_temp;
4391 + acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4392 + file_acl->acl_len += sizeof(struct acl_entry);
4393 + acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4394 + acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4396 + /* In order to use this, we'll need to wait until we can get denies */
4397 + /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4398 + acl_entry->ace_type = ACC_SPECIFY; */
4400 + acl_entry->ace_type = ACC_SPECIFY;
4402 + ace_id = acl_entry->ace_id;
4404 + ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4405 + DEBUG(10,("The id type is %d\n",ace_id->id_type));
4406 + ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4407 + memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4408 + memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
4411 + rc = chacl(name,file_acl,file_acl->acl_len);
4412 + DEBUG(10,("errno is %d\n",errno));
4413 + DEBUG(10,("return code is %d\n",rc));
4414 + SAFE_FREE(file_acl);
4415 + DEBUG(10,("Exiting the sys_acl_set_file\n"));
4419 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
4421 + struct acl_entry_link *acl_entry_link = NULL;
4422 + struct acl *file_acl = NULL;
4423 + struct acl *file_acl_temp = NULL;
4424 + struct acl_entry *acl_entry = NULL;
4425 + struct ace_id *ace_id = NULL;
4431 + DEBUG(10,("Entering sys_acl_set_fd\n"));
4432 + acl_length = BUFSIZ;
4433 + file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4435 + if(file_acl == NULL) {
4437 + DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4441 + memset(file_acl,0,BUFSIZ);
4443 + file_acl->acl_len = ACL_SIZ;
4444 + file_acl->acl_mode = S_IXACL;
4446 + for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4447 + acl_entry_link->entryp->ace_access >>= 6;
4448 + id_type = acl_entry_link->entryp->ace_id->id_type;
4449 + DEBUG(10,("The id_type is %d\n",id_type));
4452 + case SMB_ACL_USER_OBJ:
4453 + file_acl->u_access = acl_entry_link->entryp->ace_access;
4455 + case SMB_ACL_GROUP_OBJ:
4456 + file_acl->g_access = acl_entry_link->entryp->ace_access;
4458 + case SMB_ACL_OTHER:
4459 + file_acl->o_access = acl_entry_link->entryp->ace_access;
4461 + case SMB_ACL_MASK:
4465 + if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4466 + acl_length += sizeof(struct acl_entry);
4467 + file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
4468 + if(file_acl_temp == NULL) {
4469 + SAFE_FREE(file_acl);
4471 + DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4475 + memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4476 + SAFE_FREE(file_acl);
4477 + file_acl = file_acl_temp;
4480 + acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4481 + file_acl->acl_len += sizeof(struct acl_entry);
4482 + acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4483 + acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4485 + /* In order to use this, we'll need to wait until we can get denies */
4486 + /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4487 + acl_entry->ace_type = ACC_SPECIFY; */
4489 + acl_entry->ace_type = ACC_SPECIFY;
4491 + ace_id = acl_entry->ace_id;
4493 + ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4494 + DEBUG(10,("The id type is %d\n",ace_id->id_type));
4495 + ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4496 + memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4497 + memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
4500 + rc = fchacl(fd,file_acl,file_acl->acl_len);
4501 + DEBUG(10,("errno is %d\n",errno));
4502 + DEBUG(10,("return code is %d\n",rc));
4503 + SAFE_FREE(file_acl);
4504 + DEBUG(10,("Exiting sys_acl_set_fd\n"));
4508 +int sys_acl_delete_def_file(const char *name)
4510 + /* AIX has no default ACL */
4514 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4516 + return(*permset & perm);
4519 +int sys_acl_free_text(char *text)
4524 +int sys_acl_free_acl(SMB_ACL_T posix_acl)
4526 + struct acl_entry_link *acl_entry_link;
4528 + for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
4529 + SAFE_FREE(acl_entry_link->prevp->entryp);
4530 + SAFE_FREE(acl_entry_link->prevp);
4533 + SAFE_FREE(acl_entry_link->prevp->entryp);
4534 + SAFE_FREE(acl_entry_link->prevp);
4535 + SAFE_FREE(acl_entry_link->entryp);
4536 + SAFE_FREE(acl_entry_link);
4541 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4546 +#else /* No ACLs. */
4548 +int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
4554 +int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
4560 +int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
4566 +void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
4572 +SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
4575 + return (SMB_ACL_T)NULL;
4578 +SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
4581 + return (SMB_ACL_T)NULL;
4584 +int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
4590 +int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
4596 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4599 + return (permset & perm) ? 1 : 0;
4602 +char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
4608 +int sys_acl_free_text(UNUSED(char *text))
4614 +SMB_ACL_T sys_acl_init(UNUSED(int count))
4620 +int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
4626 +int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
4632 +int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
4638 +int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
4644 +int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
4650 +int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
4656 +int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
4662 +int sys_acl_delete_def_file(UNUSED(const char *name))
4668 +int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
4674 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
4680 +#endif /* No ACLs. */
4682 +/************************************************************************
4683 + Deliberately outside the ACL defines. Return 1 if this is a "no acls"
4685 +************************************************************************/
4687 +int no_acl_syscall_error(int err)
4689 +#if defined(ENOSYS)
4690 + if (err == ENOSYS) {
4694 +#if defined(ENOTSUP)
4695 + if (err == ENOTSUP) {
4701 --- old/lib/sysacls.h
4702 +++ new/lib/sysacls.h
4704 +#define SMB_MALLOC(cnt) new_array(char, cnt)
4705 +#define SMB_MALLOC_P(obj) new_array(obj, 1)
4706 +#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
4707 +#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
4708 +#define slprintf snprintf
4710 +int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
4711 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
4712 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
4713 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
4714 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
4715 +SMB_ACL_T sys_acl_get_fd(int fd);
4716 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
4717 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4718 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4719 +char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
4720 +SMB_ACL_T sys_acl_init(int count);
4721 +int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
4722 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
4723 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
4724 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
4725 +int sys_acl_valid(SMB_ACL_T theacl);
4726 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
4727 +int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
4728 +int sys_acl_delete_def_file(const char *name);
4729 +int sys_acl_free_text(char *text);
4730 +int sys_acl_free_acl(SMB_ACL_T the_acl);
4731 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
4734 @@ -58,7 +58,7 @@ BEGIN {
4738 -!/^OFF_T|^size_t|^off_t|^pid_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
4739 +!/^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/ {
4745 @@ -45,6 +45,7 @@ int copy_dirlinks = 0;
4747 int preserve_links = 0;
4748 int preserve_hard_links = 0;
4749 +int preserve_acls = 0;
4750 int preserve_perms = 0;
4751 int preserve_executability = 0;
4752 int preserve_devices = 0;
4753 @@ -194,6 +195,7 @@ static void print_rsync_version(enum log
4754 char const *got_socketpair = "no ";
4755 char const *have_inplace = "no ";
4756 char const *hardlinks = "no ";
4757 + char const *acls = "no ";
4758 char const *links = "no ";
4759 char const *ipv6 = "no ";
4760 STRUCT_STAT *dumstat;
4761 @@ -210,6 +212,10 @@ static void print_rsync_version(enum log
4765 +#ifdef SUPPORT_ACLS
4769 #ifdef SUPPORT_LINKS
4772 @@ -223,9 +229,9 @@ static void print_rsync_version(enum log
4773 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
4774 rprintf(f, "<http://rsync.samba.org/>\n");
4775 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
4776 - "%shard links, %ssymlinks, batchfiles,\n",
4777 + "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
4778 (int) (sizeof (OFF_T) * 8),
4779 - got_socketpair, hardlinks, links);
4780 + got_socketpair, hardlinks, acls, links);
4782 /* Note that this field may not have type ino_t. It depends
4783 * on the complicated interaction between largefile feature
4784 @@ -295,6 +301,9 @@ void usage(enum logcode F)
4785 rprintf(F," -H, --hard-links preserve hard links\n");
4786 rprintf(F," -p, --perms preserve permissions\n");
4787 rprintf(F," -E, --executability preserve the file's executability\n");
4788 +#ifdef SUPPORT_ACLS
4789 + rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
4791 rprintf(F," --chmod=CHMOD change destination permissions\n");
4792 rprintf(F," -o, --owner preserve owner (super-user only)\n");
4793 rprintf(F," -g, --group preserve group\n");
4794 @@ -410,6 +419,9 @@ static struct poptOption long_options[]
4795 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
4796 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
4797 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
4798 + {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
4799 + {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4800 + {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4801 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
4802 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
4803 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
4804 @@ -1068,6 +1080,23 @@ int parse_arguments(int *argc, const cha
4809 +#ifdef SUPPORT_ACLS
4810 + preserve_acls = preserve_perms = 1;
4813 + /* FIXME: this should probably be ignored with a
4814 + * warning and then countermeasures taken to
4815 + * restrict group and other access in the presence
4816 + * of any more restrictive ACLs, but this is safe
4818 + snprintf(err_buf,sizeof(err_buf),
4819 + "ACLs are not supported on this %s\n",
4820 + am_server ? "server" : "client");
4826 /* A large opt value means that set_refuse_options()
4827 * turned this option off. */
4828 @@ -1511,6 +1540,10 @@ void server_options(char **args,int *arg
4830 if (preserve_hard_links)
4832 +#ifdef SUPPORT_ACLS
4833 + if (preserve_acls)
4834 + argstr[x++] = 'A';
4841 @@ -46,6 +46,7 @@ extern int keep_partial;
4842 extern int checksum_seed;
4844 extern int delay_updates;
4845 +extern mode_t orig_umask;
4846 extern struct stats stats;
4847 extern char *log_format;
4848 extern char *tmpdir;
4849 @@ -344,6 +345,10 @@ int recv_files(int f_in, struct file_lis
4850 int itemizing = am_daemon ? daemon_log_format_has_i
4851 : !am_server && log_format_has_i;
4852 int max_phase = protocol_version >= 29 ? 2 : 1;
4853 + int dflt_perms = (ACCESSPERMS & ~orig_umask);
4854 +#ifdef SUPPORT_ACLS
4855 + char *parent_dirname = "";
4860 @@ -541,7 +546,16 @@ int recv_files(int f_in, struct file_lis
4861 * mode based on the local permissions and some heuristics. */
4862 if (!preserve_perms) {
4863 int exists = fd1 != -1;
4864 - file->mode = dest_mode(file->mode, st.st_mode, exists);
4865 +#ifdef SUPPORT_ACLS
4866 + char *dn = file->dirname ? file->dirname : ".";
4867 + if (parent_dirname != dn
4868 + && strcmp(parent_dirname, dn) != 0) {
4869 + dflt_perms = default_perms_for_dir(dn);
4870 + parent_dirname = dn;
4873 + file->mode = dest_mode(file->mode, st.st_mode,
4874 + dflt_perms, exists);
4877 /* We now check to see if we are writing file "inplace" */
4880 @@ -101,7 +101,8 @@ void free_sums(struct sum_struct *s)
4882 /* This is only called when we aren't preserving permissions. Figure out what
4883 * the permissions should be and return them merged back into the mode. */
4884 -mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int exists)
4885 +mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int dflt_perms,
4888 /* If the file already exists, we'll return the local permissions,
4889 * possibly tweaked by the --executability option. */
4890 @@ -116,7 +117,7 @@ mode_t dest_mode(mode_t flist_mode, mode
4891 cur_mode |= (cur_mode & 0444) >> 2;
4894 - cur_mode = flist_mode & ACCESSPERMS & ~orig_umask;
4895 + cur_mode = (flist_mode & ACCESSPERMS & dflt_perms) | S_IWUSR;
4896 if (daemon_chmod_modes && !S_ISLNK(flist_mode))
4897 cur_mode = tweak_mode(cur_mode, daemon_chmod_modes);
4898 return (flist_mode & ~CHMOD_BITS) | (cur_mode & CHMOD_BITS);
4899 @@ -217,6 +218,14 @@ int set_file_attrs(char *fname, struct f
4903 + /* If this is a directory, SET_ACL() will be called on the cleanup
4904 + * receive_generator() pass (if we called it here, we might clobber
4905 + * writability on the directory). Everything else is OK to do now. */
4906 + if (!S_ISDIR(st->st_mode)) {
4907 + if (SET_ACL(fname, file) == 0)
4911 if (verbose > 1 && flags & ATTRS_REPORT) {
4912 enum logcode code = daemon_log_format_has_i || dry_run
4916 @@ -658,6 +658,44 @@ struct chmod_mode_struct;
4918 #define UNUSED(x) x __attribute__((__unused__))
4920 +#if HAVE_POSIX_ACLS|HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|\
4921 + HAVE_HPUX_ACLS|HAVE_IRIX_ACLS|HAVE_AIX_ACLS|HAVE_TRU64_ACLS
4922 +#define SUPPORT_ACLS 1
4925 +#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
4926 +#define ACLS_NEED_MASK 1
4929 +#ifdef SUPPORT_ACLS
4930 +#ifdef HAVE_SYS_ACL_H
4931 +#include <sys/acl.h>
4933 +#define MAKE_ACL(file, fname) make_acl(file, fname)
4934 +#define SEND_ACL(file, f) send_acl(file, f)
4935 +#define RECEIVE_ACL(file, f) receive_acl(file, f)
4936 +#define SORT_FILE_ACL_INDEX_LISTS() sort_file_acl_index_lists()
4937 +#define SET_ACL(fname, file) set_acl(fname, file)
4938 +#define NEXT_ACL_UID() next_acl_uid()
4939 +#define ACL_UID_MAP(uid) acl_uid_map(uid)
4940 +#define PUSH_KEEP_BACKUP_ACL(file, orig, dest) \
4941 + push_keep_backup_acl(file, orig, dest)
4942 +#define CLEANUP_KEEP_BACKUP_ACL() cleanup_keep_backup_acl()
4943 +#define DUP_ACL(orig, dest, mode) dup_acl(orig, dest, mode)
4944 +#else /* SUPPORT_ACLS */
4945 +#define MAKE_ACL(file, fname) 1 /* checked return value */
4946 +#define SEND_ACL(file, f)
4947 +#define RECEIVE_ACL(file, f)
4948 +#define SORT_FILE_ACL_INDEX_LISTS()
4949 +#define SET_ACL(fname, file) 1 /* checked return value */
4950 +#define NEXT_ACL_UID()
4951 +#define ACL_UID_MAP(uid)
4952 +#define PUSH_KEEP_BACKUP_ACL(file, orig, dest)
4953 +#define CLEANUP_KEEP_BACKUP_ACL()
4954 +#define DUP_ACL(src, orig, mode) 1 /* checked return value */
4955 +#endif /* SUPPORT_ACLS */
4956 +#include "smb_acls.h"
4960 /* We have replacement versions of these if they're missing. */
4963 @@ -321,6 +321,7 @@ to the detailed description below for a
4964 -H, --hard-links preserve hard links
4965 -p, --perms preserve permissions
4966 -E, --executability preserve executability
4967 + -A, --acls preserve ACLs (implies -p) [non-standard]
4968 --chmod=CHMOD change destination permissions
4969 -o, --owner preserve owner (super-user only)
4970 -g, --group preserve group
4971 @@ -742,7 +743,9 @@ quote(itemize(
4972 permissions, though the bf(--executability) option might change just
4973 the execute permission for the file.
4974 it() New files get their "normal" permission bits set to the source
4975 - file's permissions masked with the receiving end's umask setting, and
4976 + file's permissions masked with the receiving directory's default
4977 + permissions (either the receiving process's umask, or the permissions
4978 + specified via the destination directory's default ACL), and
4979 their special permission bits disabled except in the case where a new
4980 directory inherits a setgid bit from its parent directory.
4982 @@ -773,9 +776,11 @@ The preservation of the destination's se
4983 directories when bf(--perms) is off was added in rsync 2.6.7. Older rsync
4984 versions erroneously preserved the three special permission bits for
4985 newly-created files when bf(--perms) was off, while overriding the
4986 -destination's setgid bit setting on a newly-created directory. (Keep in
4987 -mind that it is the version of the receiving rsync that affects this
4989 +destination's setgid bit setting on a newly-created directory. Default ACL
4990 +observance was added to the ACL patch for rsync 2.6.7, so older (or
4991 +non-ACL-enabled) rsyncs use the umask even if default ACLs are present.
4992 +(Keep in mind that it is the version of the receiving rsync that affects
4995 dit(bf(-E, --executability)) This option causes rsync to preserve the
4996 executability (or non-executability) of regular files when bf(--perms) is
4997 @@ -793,6 +798,10 @@ quote(itemize(
4999 If bf(--perms) is enabled, this option is ignored.
5001 +dit(bf(-A, --acls)) This option causes rsync to update the destination
5002 +ACLs to be the same as the source ACLs. This nonstandard option only
5003 +works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
5005 dit(bf(--chmod)) This option tells rsync to apply one or more
5006 comma-separated "chmod" strings to the permission of the files in the
5007 transfer. The resulting value is treated as though it was the permissions
5012 + Unix SMB/Netbios implementation.
5014 + Portable SMB ACL interface
5015 + Copyright (C) Jeremy Allison 2000
5017 + This program is free software; you can redistribute it and/or modify
5018 + it under the terms of the GNU General Public License as published by
5019 + the Free Software Foundation; either version 2 of the License, or
5020 + (at your option) any later version.
5022 + This program is distributed in the hope that it will be useful,
5023 + but WITHOUT ANY WARRANTY; without even the implied warranty of
5024 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5025 + GNU General Public License for more details.
5027 + You should have received a copy of the GNU General Public License
5028 + along with this program; if not, write to the Free Software
5029 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
5032 +#ifndef _SMB_ACLS_H
5033 +#define _SMB_ACLS_H
5035 +#if defined(HAVE_POSIX_ACLS)
5037 +/* This is an identity mapping (just remove the SMB_). */
5039 +#define SMB_ACL_TAG_T acl_tag_t
5040 +#define SMB_ACL_TYPE_T acl_type_t
5041 +#define SMB_ACL_PERMSET_T acl_permset_t
5042 +#define SMB_ACL_PERM_T acl_perm_t
5043 +#define SMB_ACL_READ ACL_READ
5044 +#define SMB_ACL_WRITE ACL_WRITE
5045 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5047 +/* Types of ACLs. */
5048 +#define SMB_ACL_USER ACL_USER
5049 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5050 +#define SMB_ACL_GROUP ACL_GROUP
5051 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5052 +#define SMB_ACL_OTHER ACL_OTHER
5053 +#define SMB_ACL_MASK ACL_MASK
5055 +#define SMB_ACL_T acl_t
5057 +#define SMB_ACL_ENTRY_T acl_entry_t
5059 +#define SMB_ACL_FIRST_ENTRY ACL_FIRST_ENTRY
5060 +#define SMB_ACL_NEXT_ENTRY ACL_NEXT_ENTRY
5062 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5063 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5065 +#elif defined(HAVE_TRU64_ACLS)
5067 +/* This is for DEC/Compaq Tru64 UNIX */
5069 +#define SMB_ACL_TAG_T acl_tag_t
5070 +#define SMB_ACL_TYPE_T acl_type_t
5071 +#define SMB_ACL_PERMSET_T acl_permset_t
5072 +#define SMB_ACL_PERM_T acl_perm_t
5073 +#define SMB_ACL_READ ACL_READ
5074 +#define SMB_ACL_WRITE ACL_WRITE
5075 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5077 +/* Types of ACLs. */
5078 +#define SMB_ACL_USER ACL_USER
5079 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5080 +#define SMB_ACL_GROUP ACL_GROUP
5081 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5082 +#define SMB_ACL_OTHER ACL_OTHER
5083 +#define SMB_ACL_MASK ACL_MASK
5085 +#define SMB_ACL_T acl_t
5087 +#define SMB_ACL_ENTRY_T acl_entry_t
5089 +#define SMB_ACL_FIRST_ENTRY 0
5090 +#define SMB_ACL_NEXT_ENTRY 1
5092 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5093 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5095 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
5097 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
5098 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
5101 +/* SVR4.2 ES/MP ACLs */
5102 +typedef int SMB_ACL_TAG_T;
5103 +typedef int SMB_ACL_TYPE_T;
5104 +typedef ushort *SMB_ACL_PERMSET_T;
5105 +typedef ushort SMB_ACL_PERM_T;
5106 +#define SMB_ACL_READ 4
5107 +#define SMB_ACL_WRITE 2
5108 +#define SMB_ACL_EXECUTE 1
5110 +/* Types of ACLs. */
5111 +#define SMB_ACL_USER USER
5112 +#define SMB_ACL_USER_OBJ USER_OBJ
5113 +#define SMB_ACL_GROUP GROUP
5114 +#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5115 +#define SMB_ACL_OTHER OTHER_OBJ
5116 +#define SMB_ACL_MASK CLASS_OBJ
5118 +typedef struct SMB_ACL_T {
5122 + struct acl acl[1];
5125 +typedef struct acl *SMB_ACL_ENTRY_T;
5127 +#define SMB_ACL_FIRST_ENTRY 0
5128 +#define SMB_ACL_NEXT_ENTRY 1
5130 +#define SMB_ACL_TYPE_ACCESS 0
5131 +#define SMB_ACL_TYPE_DEFAULT 1
5133 +#elif defined(HAVE_HPUX_ACLS)
5136 + * Based on the Solaris & UnixWare code.
5140 +#include <sys/aclv.h>
5142 +/* SVR4.2 ES/MP ACLs */
5143 +typedef int SMB_ACL_TAG_T;
5144 +typedef int SMB_ACL_TYPE_T;
5145 +typedef ushort *SMB_ACL_PERMSET_T;
5146 +typedef ushort SMB_ACL_PERM_T;
5147 +#define SMB_ACL_READ 4
5148 +#define SMB_ACL_WRITE 2
5149 +#define SMB_ACL_EXECUTE 1
5151 +/* Types of ACLs. */
5152 +#define SMB_ACL_USER USER
5153 +#define SMB_ACL_USER_OBJ USER_OBJ
5154 +#define SMB_ACL_GROUP GROUP
5155 +#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5156 +#define SMB_ACL_OTHER OTHER_OBJ
5157 +#define SMB_ACL_MASK CLASS_OBJ
5159 +typedef struct SMB_ACL_T {
5163 + struct acl acl[1];
5166 +typedef struct acl *SMB_ACL_ENTRY_T;
5168 +#define SMB_ACL_FIRST_ENTRY 0
5169 +#define SMB_ACL_NEXT_ENTRY 1
5171 +#define SMB_ACL_TYPE_ACCESS 0
5172 +#define SMB_ACL_TYPE_DEFAULT 1
5174 +#elif defined(HAVE_IRIX_ACLS)
5176 +#define SMB_ACL_TAG_T acl_tag_t
5177 +#define SMB_ACL_TYPE_T acl_type_t
5178 +#define SMB_ACL_PERMSET_T acl_permset_t
5179 +#define SMB_ACL_PERM_T acl_perm_t
5180 +#define SMB_ACL_READ ACL_READ
5181 +#define SMB_ACL_WRITE ACL_WRITE
5182 +#define SMB_ACL_EXECUTE ACL_EXECUTE
5184 +/* Types of ACLs. */
5185 +#define SMB_ACL_USER ACL_USER
5186 +#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5187 +#define SMB_ACL_GROUP ACL_GROUP
5188 +#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5189 +#define SMB_ACL_OTHER ACL_OTHER_OBJ
5190 +#define SMB_ACL_MASK ACL_MASK
5192 +typedef struct SMB_ACL_T {
5198 +#define SMB_ACL_ENTRY_T acl_entry_t
5200 +#define SMB_ACL_FIRST_ENTRY 0
5201 +#define SMB_ACL_NEXT_ENTRY 1
5203 +#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5204 +#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5206 +#elif defined(HAVE_AIX_ACLS)
5208 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
5210 +#include "/usr/include/acl.h"
5212 +typedef uint *SMB_ACL_PERMSET_T;
5214 +struct acl_entry_link{
5215 + struct acl_entry_link *prevp;
5216 + struct new_acl_entry *entryp;
5217 + struct acl_entry_link *nextp;
5221 +struct new_acl_entry{
5222 + unsigned short ace_len;
5223 + unsigned short ace_type;
5224 + unsigned int ace_access;
5225 + struct ace_id ace_id[1];
5228 +#define SMB_ACL_ENTRY_T struct new_acl_entry*
5229 +#define SMB_ACL_T struct acl_entry_link*
5231 +#define SMB_ACL_TAG_T unsigned short
5232 +#define SMB_ACL_TYPE_T int
5233 +#define SMB_ACL_PERM_T uint
5234 +#define SMB_ACL_READ S_IRUSR
5235 +#define SMB_ACL_WRITE S_IWUSR
5236 +#define SMB_ACL_EXECUTE S_IXUSR
5238 +/* Types of ACLs. */
5239 +#define SMB_ACL_USER ACEID_USER
5240 +#define SMB_ACL_USER_OBJ 3
5241 +#define SMB_ACL_GROUP ACEID_GROUP
5242 +#define SMB_ACL_GROUP_OBJ 4
5243 +#define SMB_ACL_OTHER 5
5244 +#define SMB_ACL_MASK 6
5247 +#define SMB_ACL_FIRST_ENTRY 1
5248 +#define SMB_ACL_NEXT_ENTRY 2
5250 +#define SMB_ACL_TYPE_ACCESS 0
5251 +#define SMB_ACL_TYPE_DEFAULT 1
5253 +#else /* No ACLs. */
5255 +/* No ACLS - fake it. */
5256 +#define SMB_ACL_TAG_T int
5257 +#define SMB_ACL_TYPE_T int
5258 +#define SMB_ACL_PERMSET_T mode_t
5259 +#define SMB_ACL_PERM_T mode_t
5260 +#define SMB_ACL_READ S_IRUSR
5261 +#define SMB_ACL_WRITE S_IWUSR
5262 +#define SMB_ACL_EXECUTE S_IXUSR
5264 +/* Types of ACLs. */
5265 +#define SMB_ACL_USER 0
5266 +#define SMB_ACL_USER_OBJ 1
5267 +#define SMB_ACL_GROUP 2
5268 +#define SMB_ACL_GROUP_OBJ 3
5269 +#define SMB_ACL_OTHER 4
5270 +#define SMB_ACL_MASK 5
5272 +typedef struct SMB_ACL_T {
5276 +typedef struct SMB_ACL_ENTRY_T {
5278 +} *SMB_ACL_ENTRY_T;
5280 +#define SMB_ACL_FIRST_ENTRY 0
5281 +#define SMB_ACL_NEXT_ENTRY 1
5283 +#define SMB_ACL_TYPE_ACCESS 0
5284 +#define SMB_ACL_TYPE_DEFAULT 1
5286 +#endif /* No ACLs. */
5287 +#endif /* _SMB_ACLS_H */
5288 --- old/testsuite/default-acls.test
5289 +++ new/testsuite/default-acls.test
5293 +# This program is distributable under the terms of the GNU GPL see
5296 +# Test that rsync obeys default ACLs. -- Matt McCutchen
5298 +. $srcdir/testsuite/rsync.fns
5300 +$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
5301 +setfacl -dm u::rwx,g::---,o::--- "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
5303 +# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
5305 + todir="$scratchdir/$1"
5307 + # FIXME This doesn't work on solaris...
5308 + setfacl -k "$todir"
5309 + [ "$2" ] && setfacl -dm "$2" "$todir"
5310 + # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
5311 + # even though the directory itself is outside the transfer
5312 + $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
5313 + check_perms "$todir/to" $4 "Target $1"
5314 + check_perms "$todir/to/dir" $4 "Target $1"
5315 + check_perms "$todir/to/file" $3 "Target $1"
5316 + check_perms "$todir/to/program" $4 "Target $1"
5317 + # Make sure get_local_name doesn't mess us up when transferring only one file
5318 + $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
5319 + check_perms "$todir/to/anotherfile" $3 "Target $1"
5320 + # Make sure we obey default ACLs when not transferring a regular file
5321 + $RSYNC -rvv "$scratchdir/dir/" "$todir/to/anotherdir/"
5322 + check_perms "$todir/to/anotherdir" $4 "Target $1"
5325 +mkdir "$scratchdir/dir"
5326 +echo "File!" >"$scratchdir/file"
5327 +echo "#!/bin/sh" >"$scratchdir/program"
5328 +chmod 777 "$scratchdir/dir"
5329 +chmod 666 "$scratchdir/file"
5330 +chmod 777 "$scratchdir/program"
5332 +# Test some target directories
5334 +testit da777 u::rwx,g::rwx,o::rwx rw-rw-rw- rwxrwxrwx
5335 +testit da775 u::rwx,g::rwx,o::r-x rw-rw-r-- rwxrwxr-x
5336 +testit da750 u::rwx,g::r-x,o::--- rw-r----- rwxr-x---
5337 +testit da770mask u::rwx,g::---,m::rwx,o::--- rw-rw---- rwxrwx---
5338 +testit noda1 '' rw------- rwx------
5340 +testit noda2 '' rw-rw-rw- rwxrwxrwx
5342 +testit noda3 '' rw-r--r-- rwxr-xr-x
5350 extern int preserve_uid;
5351 extern int preserve_gid;
5352 +extern int preserve_acls;
5353 extern int numeric_ids;
5356 @@ -274,7 +275,7 @@ void send_uid_list(int f)
5360 - if (preserve_uid) {
5361 + if (preserve_uid || preserve_acls) {
5363 /* we send sequences of uid/byte-length/name */
5364 for (list = uidlist; list; list = list->next) {
5365 @@ -291,7 +292,7 @@ void send_uid_list(int f)
5369 - if (preserve_gid) {
5370 + if (preserve_gid || preserve_acls) {
5372 for (list = gidlist; list; list = list->next) {
5374 @@ -312,7 +313,7 @@ void recv_uid_list(int f, struct file_li
5378 - if (preserve_uid && !numeric_ids) {
5379 + if ((preserve_uid || preserve_acls) && !numeric_ids) {
5380 /* read the uid list */
5381 while ((id = read_int(f)) != 0) {
5382 int len = read_byte(f);
5383 @@ -324,7 +325,7 @@ void recv_uid_list(int f, struct file_li
5387 - if (preserve_gid && !numeric_ids) {
5388 + if ((preserve_gid || preserve_acls) && !numeric_ids) {
5389 /* read the gid list */
5390 while ((id = read_int(f)) != 0) {
5391 int len = read_byte(f);
5392 @@ -336,6 +337,18 @@ void recv_uid_list(int f, struct file_li
5396 +#ifdef SUPPORT_ACLS
5397 + if (preserve_acls && !numeric_ids) {
5399 + /* The enumerations don't return 0 except to flag the last
5400 + * entry, since uidlist doesn't munge 0 anyway. */
5401 + while ((id = next_acl_uid(flist)) != 0)
5402 + acl_uid_map(match_uid(id));
5403 + while ((id = next_acl_gid(flist)) != 0)
5404 + acl_gid_map(match_gid(id));
5406 +#endif /* SUPPORT_ACLS */
5408 /* Now convert all the uids/gids from sender values to our values. */
5409 if (am_root && preserve_uid && !numeric_ids) {
5410 for (i = 0; i < flist->count; i++)