Fixed failing hunks.
[rsync/rsync-patches.git] / acls.diff
CommitLineData
fa26e11c
WD
1After applying this patch, run these commands for a successful build:
2
27e96866 3 ./prepare-source
f787c90c 4 ./configure --enable-acl-support
fa26e11c
WD
5 make
6
a5e3a4cc
WD
7See the --acls (-A) option in the revised man page for a note on using this
8latest ACL-enabling patch to send files to an older ACL-enabled rsync.
9
9a7eef96
WD
10--- old/Makefile.in
11+++ new/Makefile.in
4bf6f8c7 12@@ -25,15 +25,15 @@ VERSION=@VERSION@
fa26e11c
WD
13 .SUFFIXES:
14 .SUFFIXES: .c .o
15
16-HEADERS=byteorder.h config.h errcode.h proto.h rsync.h lib/pool_alloc.h
17+HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
18 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
0f6733d8
WD
19- lib/permstring.o lib/pool_alloc.o @LIBOBJS@
20+ lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
4bf6f8c7
WD
21 ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
22 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
fa26e11c
WD
23 OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
24 main.o checksum.o match.o syscall.o log.o backup.o
25 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
610969d1
WD
26- fileio.o batch.o clientname.o chmod.o
27+ fileio.o batch.o clientname.o chmod.o acls.o
fa26e11c
WD
28 OBJS3=progress.o pipe.o
29 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
30 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
9a7eef96
WD
31--- old/acls.c
32+++ new/acls.c
5d2612ca 33@@ -0,0 +1,1099 @@
c32e843a
WD
34+/*
35+ * Handle passing Access Control Lists between systems.
36+ *
37+ * Copyright (C) 1996 Andrew Tridgell
38+ * Copyright (C) 1996 Paul Mackerras
c32e843a
WD
39+ * Copyright (C) 2006 Wayne Davison
40+ *
41+ * This program is free software; you can redistribute it and/or modify
42+ * it under the terms of the GNU General Public License as published by
43+ * the Free Software Foundation; either version 2 of the License, or
44+ * (at your option) any later version.
45+ *
46+ * This program is distributed in the hope that it will be useful,
47+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
48+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49+ * GNU General Public License for more details.
50+ *
51+ * You should have received a copy of the GNU General Public License along
52+ * with this program; if not, write to the Free Software Foundation, Inc.,
53+ * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
54+ */
fa26e11c
WD
55+
56+#include "rsync.h"
0f6733d8 57+#include "lib/sysacls.h"
fa26e11c 58+
4df546eb 59+#ifdef SUPPORT_ACLS
fa26e11c 60+
fa26e11c
WD
61+extern int am_root;
62+extern int dry_run;
90fa6d68 63+extern int orig_umask;
a5e3a4cc 64+extern int preserve_acls;
0870c22a
WD
65+extern unsigned int file_struct_len;
66+
67+/* === ACL structures === */
fa26e11c
WD
68+
69+typedef struct {
70+ id_t id;
71+ uchar access;
c6437996 72+} id_access;
fa26e11c
WD
73+
74+typedef struct {
c6437996 75+ id_access *idas;
0870c22a
WD
76+ int count;
77+} ida_entries;
c6437996 78+
0870c22a
WD
79+#define NO_ENTRY ((uchar)0x80)
80+typedef struct rsync_acl {
81+ ida_entries users;
82+ ida_entries groups;
83+ /* These will be NO_ENTRY if there's no such entry. */
c6437996
WD
84+ uchar user_obj;
85+ uchar group_obj;
86+ uchar mask;
87+ uchar other;
fa26e11c
WD
88+} rsync_acl;
89+
0870c22a
WD
90+typedef struct {
91+ rsync_acl racl;
92+ SMB_ACL_T sacl;
93+} acl_duo;
94+
cd1d1a9d 95+static const rsync_acl empty_rsync_acl = {
0870c22a 96+ {NULL, 0}, {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
56294981 97+};
8db8e7d2 98+
6250eb3e
WD
99+static item_list access_acl_list = EMPTY_ITEM_LIST;
100+static item_list default_acl_list = EMPTY_ITEM_LIST;
0870c22a
WD
101+
102+/* === Calculations on ACL types === */
103+
104+static const char *str_acl_type(SMB_ACL_TYPE_T type)
105+{
106+ return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS"
107+ : type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT"
108+ : "unknown SMB_ACL_TYPE_T";
109+}
110+
8db8e7d2
WD
111+#define OTHER_TYPE(t) (SMB_ACL_TYPE_ACCESS+SMB_ACL_TYPE_DEFAULT-(t))
112+#define BUMP_TYPE(t) ((t = OTHER_TYPE(t)) == SMB_ACL_TYPE_DEFAULT)
fa26e11c 113+
56294981
WD
114+static int count_racl_entries(const rsync_acl *racl)
115+{
116+ return racl->users.count + racl->groups.count
0870c22a
WD
117+ + (racl->user_obj != NO_ENTRY)
118+ + (racl->group_obj != NO_ENTRY)
119+ + (racl->mask != NO_ENTRY)
120+ + (racl->other != NO_ENTRY);
56294981
WD
121+}
122+
123+static int calc_sacl_entries(const rsync_acl *racl)
9a7b72b5 124+{
0870c22a 125+ /* A System ACL always gets user/group/other permission entries. */
c6437996 126+ return racl->users.count + racl->groups.count
56294981
WD
127+#ifdef ACLS_NEED_MASK
128+ + 4;
129+#else
0870c22a 130+ + (racl->mask != NO_ENTRY) + 3;
56294981 131+#endif
c6437996
WD
132+}
133+
0870c22a
WD
134+/* Extracts and returns the permission bits from the ACL. This cannot be
135+ * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
9a7b72b5
WD
136+static int rsync_acl_get_perms(const rsync_acl *racl)
137+{
8eb3e3ae 138+ return (racl->user_obj << 6)
0870c22a 139+ + ((racl->mask != NO_ENTRY ? racl->mask : racl->group_obj) << 3)
8eb3e3ae 140+ + racl->other;
c6437996
WD
141+}
142+
0870c22a
WD
143+/* Removes the permission-bit entries from the ACL because these
144+ * can be reconstructed from the file's mode. */
9a7b72b5
WD
145+static void rsync_acl_strip_perms(rsync_acl *racl)
146+{
0870c22a
WD
147+ racl->user_obj = NO_ENTRY;
148+ if (racl->mask == NO_ENTRY)
149+ racl->group_obj = NO_ENTRY;
150+ else {
151+ if (racl->group_obj == racl->mask)
152+ racl->group_obj = NO_ENTRY;
153+ racl->mask = NO_ENTRY;
154+ }
155+ racl->other = NO_ENTRY;
c6437996 156+}
34a409bc 157+
0870c22a
WD
158+/* Given an empty rsync_acl, fake up the permission bits. */
159+static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
fa26e11c 160+{
0870c22a
WD
161+ racl->user_obj = (mode >> 6) & 7;
162+ racl->group_obj = (mode >> 3) & 7;
163+#ifdef ACLS_NEED_MASK
164+ racl->mask = (mode >> 3) & 7;
165+#endif
166+ racl->other = mode & 7;
167+}
168+
169+/* === Rsync ACL functions === */
170+
171+static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
172+{
173+ id_access *ida1, *ida2;
174+ int count = ial1->count;
175+ if (count != ial2->count)
176+ return False;
177+ ida1 = ial1->idas;
178+ ida2 = ial2->idas;
179+ for (; count--; ida1++, ida2++) {
180+ if (ida1->access != ida2->access || ida1->id != ida2->id)
181+ return False;
182+ }
183+ return True;
184+}
fa26e11c 185+
98ccc74a 186+static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
0870c22a
WD
187+{
188+ return (racl1->user_obj == racl2->user_obj
189+ && racl1->group_obj == racl2->group_obj
190+ && racl1->mask == racl2->mask
191+ && racl1->other == racl2->other
192+ && ida_entries_equal(&racl1->users, &racl2->users)
193+ && ida_entries_equal(&racl1->groups, &racl2->groups));
194+}
195+
196+/* Are the extended (non-permission-bit) entries equal? If so, the rest of
197+ * the ACL will be handled by the normal mode-preservation code. This is
198+ * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
199+ * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
200+ * that it might have several of its perm objects set to NO_ENTRY. */
98ccc74a
WD
201+static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
202+ const rsync_acl *racl2, mode_t m)
0870c22a
WD
203+{
204+ if ((racl1->mask ^ racl2->mask) & NO_ENTRY)
205+ return False; /* One has a mask and the other doesn't */
fa26e11c 206+
0870c22a
WD
207+ /* When there's a mask, the group_obj becomes an extended entry. */
208+ if (racl1->mask != NO_ENTRY) {
209+ /* A condensed rsync_acl with a mask can only have no
210+ * group_obj when it was identical to the mask. This
211+ * means that it was also identical to the group attrs
212+ * from the mode. */
213+ if (racl2->group_obj == NO_ENTRY) {
214+ if (racl1->group_obj != ((m >> 3) & 7))
215+ return False;
216+ } else if (racl1->group_obj != racl2->group_obj)
217+ return False;
fa26e11c 218+ }
0870c22a
WD
219+ return ida_entries_equal(&racl1->users, &racl2->users)
220+ && ida_entries_equal(&racl1->groups, &racl2->groups);
fa26e11c
WD
221+}
222+
223+static void rsync_acl_free(rsync_acl *racl)
224+{
8eb3e3ae
WD
225+ if (racl->users.idas)
226+ free(racl->users.idas);
227+ if (racl->groups.idas)
228+ free(racl->groups.idas);
229+ *racl = empty_rsync_acl;
fa26e11c
WD
230+}
231+
98ccc74a 232+void free_acl(statx *sxp)
0870c22a
WD
233+{
234+ if (sxp->acc_acl) {
235+ rsync_acl_free(sxp->acc_acl);
236+ free(sxp->acc_acl);
237+ sxp->acc_acl = NULL;
238+ }
239+ if (sxp->def_acl) {
240+ rsync_acl_free(sxp->def_acl);
241+ free(sxp->def_acl);
242+ sxp->def_acl = NULL;
243+ }
244+}
245+
c6437996 246+static int id_access_sorter(const void *r1, const void *r2)
fa26e11c 247+{
c6437996
WD
248+ id_access *ida1 = (id_access *)r1;
249+ id_access *ida2 = (id_access *)r2;
250+ id_t rid1 = ida1->id, rid2 = ida2->id;
fa26e11c
WD
251+ return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
252+}
253+
0870c22a 254+static void sort_ida_entries(ida_entries *idal)
fa26e11c 255+{
c6437996 256+ if (!idal->count)
fa26e11c 257+ return;
0870c22a 258+ qsort(idal->idas, idal->count, sizeof idal->idas[0], id_access_sorter);
fa26e11c
WD
259+}
260+
0870c22a
WD
261+/* Transfer the count id_access items out of the temp_ida_list into either
262+ * the users or groups ida_entries list in racl. */
263+static void save_idas(item_list *temp_ida_list, rsync_acl *racl, SMB_ACL_TAG_T type)
264+{
265+ id_access *idas;
266+ ida_entries *ent;
267+
268+ if (temp_ida_list->count) {
269+ int cnt = temp_ida_list->count;
270+ id_access *temp_idas = temp_ida_list->items;
271+ if (!(idas = new_array(id_access, cnt)))
272+ out_of_memory("save_idas");
273+ memcpy(idas, temp_idas, cnt * sizeof *temp_idas);
274+ } else
275+ idas = NULL;
276+
277+ ent = type == SMB_ACL_USER ? &racl->users : &racl->groups;
278+
279+ if (ent->count) {
280+ rprintf(FERROR, "save_idas: disjoint list found for type %d\n", type);
281+ exit_cleanup(RERR_UNSUPPORTED);
282+ }
283+ ent->count = temp_ida_list->count;
284+ ent->idas = idas;
285+
286+ /* Truncate the temporary list now that its idas have been saved. */
287+ temp_ida_list->count = 0;
288+}
289+
290+/* === System ACLs === */
291+
292+/* Unpack system acl -> rsync acl verbatim. Return whether we succeeded. */
fa26e11c
WD
293+static BOOL unpack_smb_acl(rsync_acl *racl, SMB_ACL_T sacl)
294+{
6250eb3e 295+ static item_list temp_ida_list = EMPTY_ITEM_LIST;
0870c22a 296+ SMB_ACL_TAG_T prior_list_type = 0;
fa26e11c 297+ SMB_ACL_ENTRY_T entry;
fa26e11c 298+ const char *errfun;
c6437996
WD
299+ int rc;
300+
cd1d1a9d 301+ *racl = empty_rsync_acl;
fa26e11c
WD
302+ errfun = "sys_acl_get_entry";
303+ for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
304+ rc == 1;
305+ rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
c6437996 306+ SMB_ACL_TAG_T tag_type;
fa26e11c 307+ SMB_ACL_PERMSET_T permset;
c6437996 308+ uchar access;
fa26e11c 309+ void *qualifier;
c6437996 310+ id_access *ida;
c6437996 311+ if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
fa26e11c
WD
312+ errfun = "sys_acl_get_tag_type";
313+ break;
314+ }
315+ if ((rc = sys_acl_get_permset(entry, &permset))) {
316+ errfun = "sys_acl_get_tag_type";
317+ break;
318+ }
c6437996
WD
319+ access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
320+ | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
321+ | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
0870c22a 322+ /* continue == done with entry; break == store in temporary ida list */
c6437996
WD
323+ switch (tag_type) {
324+ case SMB_ACL_USER_OBJ:
0870c22a 325+ if (racl->user_obj == NO_ENTRY)
c6437996
WD
326+ racl->user_obj = access;
327+ else
328+ rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
329+ continue;
fa26e11c 330+ case SMB_ACL_USER:
c6437996
WD
331+ break;
332+ case SMB_ACL_GROUP_OBJ:
0870c22a 333+ if (racl->group_obj == NO_ENTRY)
c6437996
WD
334+ racl->group_obj = access;
335+ else
336+ rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
337+ continue;
fa26e11c
WD
338+ case SMB_ACL_GROUP:
339+ break;
c6437996 340+ case SMB_ACL_MASK:
0870c22a 341+ if (racl->mask == NO_ENTRY)
c6437996
WD
342+ racl->mask = access;
343+ else
344+ rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
345+ continue;
346+ case SMB_ACL_OTHER:
0870c22a 347+ if (racl->other == NO_ENTRY)
c6437996
WD
348+ racl->other = access;
349+ else
350+ rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
351+ continue;
fa26e11c 352+ default:
c6437996 353+ rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
fa26e11c
WD
354+ continue;
355+ }
356+ if (!(qualifier = sys_acl_get_qualifier(entry))) {
357+ errfun = "sys_acl_get_tag_type";
358+ rc = EINVAL;
359+ break;
360+ }
0870c22a
WD
361+ if (tag_type != prior_list_type) {
362+ if (prior_list_type)
363+ save_idas(&temp_ida_list, racl, prior_list_type);
364+ prior_list_type = tag_type;
365+ }
645e162c 366+ ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
c6437996
WD
367+ ida->id = *((id_t *)qualifier);
368+ ida->access = access;
369+ sys_acl_free_qualifier(qualifier, tag_type);
fa26e11c
WD
370+ }
371+ if (rc) {
0870c22a 372+ rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
fa26e11c
WD
373+ rsync_acl_free(racl);
374+ return False;
375+ }
0870c22a
WD
376+ if (prior_list_type)
377+ save_idas(&temp_ida_list, racl, prior_list_type);
c6437996 378+
0870c22a
WD
379+ sort_ida_entries(&racl->users);
380+ sort_ida_entries(&racl->groups);
c6437996 381+
8eb3e3ae
WD
382+#ifdef ACLS_NEED_MASK
383+ if (!racl->users.count && !racl->groups.count) {
384+ /* Throw away a superfluous mask, but mask off the
385+ * group perms with it first. */
386+ racl->group_obj &= racl->mask;
0870c22a 387+ racl->mask = NO_ENTRY;
8eb3e3ae
WD
388+ }
389+#endif
390+
fa26e11c
WD
391+ return True;
392+}
393+
0870c22a 394+/* Synactic sugar for system calls */
fa26e11c 395+
0e879f7f 396+#define CALL_OR_ERROR(func,args,str) \
c6437996
WD
397+ do { \
398+ if (func args) { \
0e879f7f 399+ errfun = str; \
c6437996
WD
400+ goto error_exit; \
401+ } \
402+ } while (0)
403+
0e879f7f
WD
404+#define COE(func,args) CALL_OR_ERROR(func,args,#func)
405+#define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
406+
0870c22a 407+/* Store the permissions in the system ACL entry. */
0e879f7f 408+static int store_access_in_entry(uchar access, SMB_ACL_ENTRY_T entry)
fa26e11c 409+{
c6437996
WD
410+ const char *errfun = NULL;
411+ SMB_ACL_PERMSET_T permset;
412+
413+ COE( sys_acl_get_permset,(entry, &permset) );
414+ COE( sys_acl_clear_perms,(permset) );
415+ if (access & 4)
416+ COE( sys_acl_add_perm,(permset, SMB_ACL_READ) );
417+ if (access & 2)
418+ COE( sys_acl_add_perm,(permset, SMB_ACL_WRITE) );
419+ if (access & 1)
420+ COE( sys_acl_add_perm,(permset, SMB_ACL_EXECUTE) );
421+ COE( sys_acl_set_permset,(entry, permset) );
422+
0e879f7f 423+ return 0;
c6437996 424+
0e879f7f 425+ error_exit:
0870c22a 426+ rsyserr(FERROR, errno, "store_access_in_entry %s()", errfun);
0e879f7f 427+ return -1;
fa26e11c 428+}
fa26e11c 429+
0870c22a 430+/* Pack rsync acl -> system acl verbatim. Return whether we succeeded. */
c6437996 431+static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
fa26e11c 432+{
8eb3e3ae
WD
433+#ifdef ACLS_NEED_MASK
434+ uchar mask_bits;
435+#endif
c6437996
WD
436+ size_t count;
437+ id_access *ida;
fa26e11c 438+ const char *errfun = NULL;
c6437996 439+ SMB_ACL_ENTRY_T entry;
69aaf170 440+
56294981 441+ if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
0870c22a 442+ rsyserr(FERROR, errno, "pack_smb_acl: sys_acl_init()");
fa26e11c
WD
443+ return False;
444+ }
69aaf170 445+
c6437996
WD
446+ COE( sys_acl_create_entry,(smb_acl, &entry) );
447+ COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
56294981 448+ COE2( store_access_in_entry,(racl->user_obj & 7, entry) );
c6437996 449+
8eb3e3ae 450+ for (ida = racl->users.idas, count = racl->users.count; count--; ida++) {
c6437996
WD
451+ COE( sys_acl_create_entry,(smb_acl, &entry) );
452+ COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
453+ COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
0e879f7f 454+ COE2( store_access_in_entry,(ida->access, entry) );
c6437996
WD
455+ }
456+
457+ COE( sys_acl_create_entry,(smb_acl, &entry) );
458+ COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
56294981 459+ COE2( store_access_in_entry,(racl->group_obj & 7, entry) );
c6437996 460+
8eb3e3ae 461+ for (ida = racl->groups.idas, count = racl->groups.count; count--; ida++) {
c6437996
WD
462+ COE( sys_acl_create_entry,(smb_acl, &entry) );
463+ COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
464+ COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
0e879f7f 465+ COE2( store_access_in_entry,(ida->access, entry) );
c6437996 466+ }
8eb3e3ae
WD
467+
468+#ifdef ACLS_NEED_MASK
0870c22a 469+ mask_bits = racl->mask == NO_ENTRY ? racl->group_obj & 7 : racl->mask;
8eb3e3ae
WD
470+ COE( sys_acl_create_entry,(smb_acl, &entry) );
471+ COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
472+ COE2( store_access_in_entry,(mask_bits, entry) );
473+#else
0870c22a 474+ if (racl->mask != NO_ENTRY) {
c6437996
WD
475+ COE( sys_acl_create_entry,(smb_acl, &entry) );
476+ COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
0e879f7f 477+ COE2( store_access_in_entry,(racl->mask, entry) );
c6437996 478+ }
56294981 479+#endif
c6437996
WD
480+
481+ COE( sys_acl_create_entry,(smb_acl, &entry) );
482+ COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
56294981 483+ COE2( store_access_in_entry,(racl->other & 7, entry) );
c6437996
WD
484+
485+#ifdef DEBUG
486+ if (sys_acl_valid(*smb_acl) < 0)
0870c22a 487+ rprintf(FERROR, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
c6437996
WD
488+#endif
489+
490+ return True;
491+
0e879f7f
WD
492+ error_exit:
493+ if (errfun) {
0870c22a 494+ rsyserr(FERROR, errno, "pack_smb_acl %s()", errfun);
0e879f7f 495+ }
c6437996
WD
496+ sys_acl_free_acl(*smb_acl);
497+ return False;
498+}
499+
0870c22a
WD
500+static int find_matching_rsync_acl(SMB_ACL_TYPE_T type,
501+ const item_list *racl_list,
502+ const rsync_acl *racl)
c6437996 503+{
0870c22a
WD
504+ static int access_match = -1, default_match = -1;
505+ int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
506+ size_t count = racl_list->count;
c6437996 507+
0870c22a
WD
508+ /* If this is the first time through or we didn't match the last
509+ * time, then start at the end of the list, which should be the
510+ * best place to start hunting. */
511+ if (*match == -1)
512+ *match = racl_list->count - 1;
513+ while (count--) {
514+ rsync_acl *base = racl_list->items;
98ccc74a 515+ if (rsync_acl_equal(base + *match, racl))
0870c22a
WD
516+ return *match;
517+ if (!(*match)--)
518+ *match = racl_list->count - 1;
c6437996
WD
519+ }
520+
0870c22a
WD
521+ *match = -1;
522+ return *match;
523+}
524+
0870c22a 525+/* Return the ACL(s) for the given filename. */
98ccc74a 526+int get_acl(const char *fname, statx *sxp)
0870c22a
WD
527+{
528+ SMB_ACL_TYPE_T type;
529+
530+ if (S_ISLNK(sxp->st.st_mode))
531+ return 0;
532+
533+ type = SMB_ACL_TYPE_ACCESS;
534+ do {
535+ SMB_ACL_T sacl = sys_acl_get_file(fname, type);
536+ rsync_acl *racl = new(rsync_acl);
537+
538+ if (!racl)
98ccc74a 539+ out_of_memory("get_acl");
0870c22a
WD
540+ if (type == SMB_ACL_TYPE_ACCESS)
541+ sxp->acc_acl = racl;
542+ else
543+ sxp->def_acl = racl;
544+
545+ if (sacl) {
546+ BOOL ok = unpack_smb_acl(racl, sacl);
547+
548+ sys_acl_free_acl(sacl);
549+ if (!ok) {
98ccc74a 550+ free_acl(sxp);
0870c22a
WD
551+ return -1;
552+ }
553+ } else if (errno == ENOTSUP) {
554+ /* ACLs are not supported, so pretend we have a basic ACL. */
555+ *racl = empty_rsync_acl;
556+ if (type == SMB_ACL_TYPE_ACCESS)
557+ rsync_acl_fake_perms(racl, sxp->st.st_mode);
558+ } else {
98ccc74a 559+ rsyserr(FERROR, errno, "get_acl: sys_acl_get_file(%s, %s)",
0870c22a 560+ fname, str_acl_type(type));
98ccc74a 561+ free_acl(sxp);
0870c22a 562+ return -1;
56294981 563+ }
0870c22a
WD
564+ } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
565+
566+ return 0;
567+}
568+
569+/* === Send functions === */
570+
571+/* The general strategy with the tag_type <-> character mapping is that
572+ * lowercase implies that no qualifier follows, where uppercase does.
573+ * A similar idiom for the acl type (access or default) itself, but
574+ * lowercase in this instance means there's no ACL following, so the
575+ * ACL is a repeat, so the receiver should reuse the last of the same
576+ * type ACL. */
577+
578+/* Send the ida list over the file descriptor. */
579+static void send_ida_entries(int f, const ida_entries *idal, char tag_char)
580+{
581+ id_access *ida;
582+ size_t count = idal->count;
583+ for (ida = idal->idas; count--; ida++) {
584+ write_byte(f, tag_char);
585+ write_byte(f, ida->access);
586+ write_int(f, ida->id);
587+ /* FIXME: sorta wasteful: we should maybe buffer as
588+ * many ids as max(ACL_USER + ACL_GROUP) objects to
589+ * keep from making so many calls. */
590+ if (tag_char == 'U')
591+ add_uid(ida->id);
592+ else
593+ add_gid(ida->id);
fa26e11c 594+ }
0870c22a
WD
595+}
596+
597+/* Send an rsync acl over the file descriptor. */
598+static void send_rsync_acl(int f, const rsync_acl *racl)
599+{
600+ size_t count = count_racl_entries(racl);
601+ write_int(f, count);
602+ if (racl->user_obj != NO_ENTRY) {
603+ write_byte(f, 'u');
604+ write_byte(f, racl->user_obj);
605+ }
606+ send_ida_entries(f, &racl->users, 'U');
607+ if (racl->group_obj != NO_ENTRY) {
608+ write_byte(f, 'g');
609+ write_byte(f, racl->group_obj);
fa26e11c 610+ }
0870c22a
WD
611+ send_ida_entries(f, &racl->groups, 'G');
612+ if (racl->mask != NO_ENTRY) {
613+ write_byte(f, 'm');
614+ write_byte(f, racl->mask);
615+ }
616+ if (racl->other != NO_ENTRY) {
617+ write_byte(f, 'o');
618+ write_byte(f, racl->other);
619+ }
620+}
c6437996 621+
98ccc74a 622+/* Send the ACL from the statx structure down the indicated file descriptor.
0870c22a 623+ * This also frees the ACL data. */
98ccc74a 624+void send_acl(statx *sxp, int f)
0870c22a
WD
625+{
626+ SMB_ACL_TYPE_T type;
627+ rsync_acl *racl, *new_racl;
628+ item_list *racl_list;
5d2612ca 629+ int ndx;
53c1073a 630+
0870c22a
WD
631+ if (S_ISLNK(sxp->st.st_mode))
632+ return;
633+
634+ type = SMB_ACL_TYPE_ACCESS;
635+ racl = sxp->acc_acl;
636+ racl_list = &access_acl_list;
637+ do {
5d2612ca
WD
638+ if (!racl) {
639+ racl = new(rsync_acl);
640+ if (!racl)
641+ out_of_memory("send_acl");
642+ *racl = empty_rsync_acl;
643+ if (type == SMB_ACL_TYPE_ACCESS) {
644+ rsync_acl_fake_perms(racl, sxp->st.st_mode);
645+ sxp->acc_acl = racl;
646+ } else
647+ sxp->def_acl = racl;
648+ }
0870c22a
WD
649+
650+ /* Discard a superfluous mask. */
651+ if (racl->mask != NO_ENTRY && !racl->users.count && !racl->groups.count)
652+ racl->mask = NO_ENTRY;
653+ /* Avoid sending values that can be inferred from other data,
654+ * but only when preserve_acls == 1 (it is 2 when we must be
655+ * backward compatible with older acls.diff versions). */
656+ if (type == SMB_ACL_TYPE_ACCESS && preserve_acls == 1)
657+ rsync_acl_strip_perms(racl);
658+ if ((ndx = find_matching_rsync_acl(type, racl_list, racl)) != -1) {
659+ write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
660+ write_int(f, ndx);
0870c22a 661+ } else {
645e162c 662+ new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
0870c22a
WD
663+ write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
664+ send_rsync_acl(f, racl);
665+ *new_racl = *racl;
666+ *racl = empty_rsync_acl;
667+ }
668+ racl = sxp->def_acl;
669+ racl_list = &default_acl_list;
670+ } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
671+
98ccc74a 672+ free_acl(sxp);
fa26e11c
WD
673+}
674+
0870c22a
WD
675+/* === Receive functions === */
676+
56294981 677+static void receive_rsync_acl(rsync_acl *racl, int f, SMB_ACL_TYPE_T type)
fa26e11c 678+{
6250eb3e 679+ static item_list temp_ida_list = EMPTY_ITEM_LIST;
0870c22a 680+ SMB_ACL_TAG_T tag_type = 0, prior_list_type = 0;
090d78e1 681+ uchar computed_mask_bits = 0;
c6437996 682+ id_access *ida;
ad625644
WD
683+ size_t count;
684+
cd1d1a9d 685+ *racl = empty_rsync_acl;
c6437996 686+
ad625644 687+ if (!(count = read_int(f)))
fa26e11c 688+ return;
ad625644 689+
fa26e11c 690+ while (count--) {
dfadc68a 691+ char tag = read_byte(f);
c6437996
WD
692+ uchar access = read_byte(f);
693+ if (access & ~ (4 | 2 | 1)) {
694+ rprintf(FERROR, "receive_rsync_acl: bogus permset %o\n",
695+ access);
696+ exit_cleanup(RERR_STREAMIO);
697+ }
fa26e11c
WD
698+ switch (tag) {
699+ case 'u':
0870c22a 700+ if (racl->user_obj != NO_ENTRY) {
c6437996
WD
701+ rprintf(FERROR, "receive_rsync_acl: error: duplicate USER_OBJ entry\n");
702+ exit_cleanup(RERR_STREAMIO);
703+ }
704+ racl->user_obj = access;
705+ continue;
fa26e11c 706+ case 'U':
0870c22a 707+ tag_type = SMB_ACL_USER;
fa26e11c
WD
708+ break;
709+ case 'g':
0870c22a 710+ if (racl->group_obj != NO_ENTRY) {
c6437996
WD
711+ rprintf(FERROR, "receive_rsync_acl: error: duplicate GROUP_OBJ entry\n");
712+ exit_cleanup(RERR_STREAMIO);
713+ }
714+ racl->group_obj = access;
715+ continue;
fa26e11c 716+ case 'G':
0870c22a 717+ tag_type = SMB_ACL_GROUP;
fa26e11c
WD
718+ break;
719+ case 'm':
0870c22a 720+ if (racl->mask != NO_ENTRY) {
c6437996
WD
721+ rprintf(FERROR, "receive_rsync_acl: error: duplicate MASK entry\n");
722+ exit_cleanup(RERR_STREAMIO);
723+ }
724+ racl->mask = access;
725+ continue;
726+ case 'o':
0870c22a 727+ if (racl->other != NO_ENTRY) {
c6437996
WD
728+ rprintf(FERROR, "receive_rsync_acl: error: duplicate OTHER entry\n");
729+ exit_cleanup(RERR_STREAMIO);
730+ }
731+ racl->other = access;
732+ continue;
fa26e11c
WD
733+ default:
734+ rprintf(FERROR, "receive_rsync_acl: unknown tag %c\n",
735+ tag);
736+ exit_cleanup(RERR_STREAMIO);
737+ }
0870c22a
WD
738+ if (tag_type != prior_list_type) {
739+ if (prior_list_type)
740+ save_idas(&temp_ida_list, racl, prior_list_type);
741+ prior_list_type = tag_type;
742+ }
645e162c 743+ ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
c6437996
WD
744+ ida->access = access;
745+ ida->id = read_int(f);
090d78e1 746+ computed_mask_bits |= access;
c6437996 747+ }
0870c22a
WD
748+ if (prior_list_type)
749+ save_idas(&temp_ida_list, racl, prior_list_type);
c6437996 750+
56294981
WD
751+ if (type == SMB_ACL_TYPE_DEFAULT) {
752+ /* Ensure that these are never unset. */
0870c22a 753+ if (racl->user_obj == NO_ENTRY)
56294981 754+ racl->user_obj = 7;
0870c22a 755+ if (racl->group_obj == NO_ENTRY)
56294981 756+ racl->group_obj = 0;
0870c22a 757+ if (racl->other == NO_ENTRY)
56294981
WD
758+ racl->other = 0;
759+ }
8eb3e3ae 760+
c6437996 761+ if (!racl->users.count && !racl->groups.count) {
8eb3e3ae 762+ /* If we received a superfluous mask, throw it away. */
0870c22a 763+ if (racl->mask != NO_ENTRY) {
8eb3e3ae 764+ /* Mask off the group perms with it first. */
0870c22a
WD
765+ racl->group_obj &= racl->mask | NO_ENTRY;
766+ racl->mask = NO_ENTRY;
fa26e11c 767+ }
0870c22a 768+ } else if (racl->mask == NO_ENTRY) /* Must be non-empty with lists. */
56294981 769+ racl->mask = computed_mask_bits | (racl->group_obj & 7);
fa26e11c
WD
770+}
771+
0870c22a 772+/* Receive the ACL info the sender has included for this file-list entry. */
98ccc74a 773+void receive_acl(struct file_struct *file, int f)
fa26e11c 774+{
8db8e7d2 775+ SMB_ACL_TYPE_T type;
0870c22a
WD
776+ item_list *racl_list;
777+ char *ndx_ptr;
36aa3171 778+
162234a7 779+ if (S_ISLNK(file->mode))
fa26e11c 780+ return;
36aa3171 781+
8db8e7d2 782+ type = SMB_ACL_TYPE_ACCESS;
0870c22a
WD
783+ racl_list = &access_acl_list;
784+ ndx_ptr = (char*)file + file_struct_len;
8db8e7d2 785+ do {
0870c22a
WD
786+ char tag = read_byte(f);
787+ int ndx;
fa26e11c 788+
fa26e11c 789+ if (tag == 'A' || tag == 'a') {
8db8e7d2 790+ if (type != SMB_ACL_TYPE_ACCESS) {
fa26e11c 791+ rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
f4222aa5 792+ f_name(file, NULL));
fa26e11c
WD
793+ exit_cleanup(RERR_STREAMIO);
794+ }
795+ } else if (tag == 'D' || tag == 'd') {
8db8e7d2 796+ if (type == SMB_ACL_TYPE_ACCESS) {
fa26e11c 797+ rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
f4222aa5 798+ f_name(file, NULL));
fa26e11c
WD
799+ exit_cleanup(RERR_STREAMIO);
800+ }
801+ } else {
802+ rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
f4222aa5 803+ f_name(file, NULL), tag);
fa26e11c
WD
804+ exit_cleanup(RERR_STREAMIO);
805+ }
806+ if (tag == 'A' || tag == 'D') {
0870c22a
WD
807+ acl_duo *duo_item;
808+ ndx = racl_list->count;
645e162c 809+ duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
0870c22a
WD
810+ receive_rsync_acl(&duo_item->racl, f, type);
811+ duo_item->sacl = NULL;
fa26e11c 812+ } else {
0870c22a 813+ ndx = read_int(f);
d4531ffb 814+ if (ndx < 0 || (size_t)ndx >= racl_list->count) {
fa26e11c 815+ rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
0870c22a 816+ f_name(file, NULL), str_acl_type(type), ndx);
fa26e11c
WD
817+ exit_cleanup(RERR_STREAMIO);
818+ }
fa26e11c 819+ }
0870c22a
WD
820+ SIVAL(ndx_ptr, 0, ndx);
821+ racl_list = &default_acl_list;
822+ ndx_ptr += 4;
8db8e7d2 823+ } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
fa26e11c
WD
824+}
825+
5d2612ca
WD
826+/* Turn the ACL data in statx into cached ACL data, setting the index
827+ * values in the file struct. */
828+void cache_acl(struct file_struct *file, statx *sxp)
829+{
830+ SMB_ACL_TYPE_T type;
831+ rsync_acl *racl;
832+ item_list *racl_list;
833+ char *ndx_ptr;
834+ int ndx;
835+
836+ if (S_ISLNK(file->mode))
837+ return;
838+
839+ type = SMB_ACL_TYPE_ACCESS;
840+ racl = sxp->acc_acl;
841+ racl_list = &access_acl_list;
842+ ndx_ptr = (char*)file + file_struct_len;
843+ do {
844+ if (!racl)
845+ ndx = -1;
846+ else if ((ndx = find_matching_rsync_acl(type, racl_list, racl)) == -1) {
847+ acl_duo *new_duo;
848+ ndx = racl_list->count;
849+ new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
850+ new_duo->racl = *racl;
851+ new_duo->sacl = NULL;
852+ *racl = empty_rsync_acl;
853+ }
854+ SIVAL(ndx_ptr, 0, ndx);
855+ racl = sxp->def_acl;
856+ racl_list = &default_acl_list;
857+ ndx_ptr += 4;
858+ } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
859+
860+ free_acl(sxp);
861+}
862+
0870c22a 863+static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
fa26e11c 864+{
0870c22a
WD
865+ SMB_ACL_ENTRY_T entry;
866+ const char *errfun;
867+ int rc;
8db8e7d2 868+
0870c22a
WD
869+ if (S_ISDIR(mode)) {
870+ /* If the sticky bit is going on, it's not safe to allow all
871+ * the new ACLs to go into effect before it gets set. */
872+#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
873+ if (mode & S_ISVTX)
874+ mode &= ~0077;
875+#else
876+ if (mode & S_ISVTX && !(old_mode & S_ISVTX))
877+ mode &= ~0077;
878+ } else {
879+ /* If setuid or setgid is going off, it's not safe to allow all
880+ * the new ACLs to go into effect before they get cleared. */
881+ if ((old_mode & S_ISUID && !(mode & S_ISUID))
882+ || (old_mode & S_ISGID && !(mode & S_ISGID)))
883+ mode &= ~0077;
884+#endif
885+ }
8db8e7d2 886+
0870c22a
WD
887+ errfun = "sys_acl_get_entry";
888+ for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
889+ rc == 1;
890+ rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
891+ SMB_ACL_TAG_T tag_type;
892+ if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
893+ errfun = "sys_acl_get_tag_type";
8db8e7d2 894+ break;
fa26e11c 895+ }
0870c22a
WD
896+ switch (tag_type) {
897+ case SMB_ACL_USER_OBJ:
898+ COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
899+ break;
900+ case SMB_ACL_GROUP_OBJ:
901+ /* group is only empty when identical to group perms. */
902+ if (racl->group_obj != NO_ENTRY)
903+ break;
904+ COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
905+ break;
906+ case SMB_ACL_MASK:
907+#ifndef ACLS_NEED_MASK
908+ /* mask is only empty when we don't need it. */
909+ if (racl->mask == NO_ENTRY)
910+ break;
911+#endif
912+ COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
913+ break;
914+ case SMB_ACL_OTHER:
915+ COE2( store_access_in_entry,(mode & 7, entry) );
916+ break;
8db8e7d2 917+ }
0870c22a
WD
918+ }
919+ if (rc) {
920+ error_exit:
921+ if (errfun) {
922+ rsyserr(FERROR, errno, "change_sacl_perms: %s()",
923+ errfun);
fa26e11c 924+ }
0870c22a
WD
925+ return ~0u;
926+ }
8db8e7d2 927+
0870c22a
WD
928+#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
929+ /* Ensure that chmod() will be called to restore any lost setid bits. */
930+ if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
931+ && (old_mode & CHMOD_BITS) == (mode & CHMOD_BITS))
932+ old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
933+#endif
8db8e7d2 934+
0870c22a
WD
935+ /* Return the mode of the file on disk, as we will set them. */
936+ return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
fa26e11c
WD
937+}
938+
0870c22a 939+/* Set ACL on indicated filename.
c6437996
WD
940+ *
941+ * This sets extended access ACL entries and default ACLs. If convenient,
942+ * it sets permission bits along with the access ACLs and signals having
0870c22a 943+ * done so by modifying sxp->st.st_mode.
c6437996 944+ *
0870c22a
WD
945+ * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
946+ * with fname set to NULL to just check if the ACLs are unchanged. */
98ccc74a 947+int set_acl(const char *fname, const struct file_struct *file, statx *sxp)
fa26e11c 948+{
81ddc4dc 949+ int unchanged = 1;
8db8e7d2 950+ SMB_ACL_TYPE_T type;
0870c22a 951+ char *ndx_ptr;
36aa3171 952+
c6437996
WD
953+ if (S_ISLNK(file->mode))
954+ return 1;
36aa3171 955+
8db8e7d2 956+ type = SMB_ACL_TYPE_ACCESS;
0870c22a 957+ ndx_ptr = (char*)file + file_struct_len;
8db8e7d2 958+ do {
0870c22a
WD
959+ acl_duo *duo_item;
960+ BOOL eq;
5d2612ca 961+ int32 ndx = IVAL(ndx_ptr, 0);
0870c22a
WD
962+
963+ ndx_ptr += 4;
964+
965+ if (type == SMB_ACL_TYPE_ACCESS) {
5d2612ca
WD
966+ if (ndx < 0 || (size_t)ndx >= access_acl_list.count)
967+ continue;
0870c22a
WD
968+ duo_item = access_acl_list.items;
969+ duo_item += ndx;
5d2612ca
WD
970+ eq = sxp->acc_acl
971+ && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
0870c22a 972+ } else {
5d2612ca
WD
973+ if (ndx < 0 || (size_t)ndx >= default_acl_list.count)
974+ continue;
0870c22a
WD
975+ duo_item = default_acl_list.items;
976+ duo_item += ndx;
5d2612ca
WD
977+ eq = sxp->def_acl
978+ && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
fa26e11c 979+ }
0870c22a 980+ if (eq)
fa26e11c 981+ continue;
0870c22a 982+ if (!dry_run && fname) {
c6437996 983+ if (type == SMB_ACL_TYPE_DEFAULT
0870c22a 984+ && duo_item->racl.user_obj == NO_ENTRY) {
c6437996 985+ if (sys_acl_delete_def_file(fname) < 0) {
0870c22a
WD
986+ rsyserr(FERROR, errno, "set_acl: sys_acl_delete_def_file(%s)",
987+ fname);
c6437996
WD
988+ unchanged = -1;
989+ continue;
990+ }
991+ } else {
0870c22a
WD
992+ mode_t cur_mode = sxp->st.st_mode;
993+ if (!duo_item->sacl
994+ && !pack_smb_acl(&duo_item->sacl, &duo_item->racl)) {
c6437996
WD
995+ unchanged = -1;
996+ continue;
997+ }
998+ if (type == SMB_ACL_TYPE_ACCESS) {
0870c22a 999+ cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
c6437996 1000+ cur_mode, file->mode);
0e879f7f
WD
1001+ if (cur_mode == ~0u)
1002+ continue;
c6437996 1003+ }
0870c22a
WD
1004+ if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
1005+ rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
1006+ fname, str_acl_type(type));
c6437996
WD
1007+ unchanged = -1;
1008+ continue;
1009+ }
1010+ if (type == SMB_ACL_TYPE_ACCESS)
0870c22a 1011+ sxp->st.st_mode = cur_mode;
fa26e11c
WD
1012+ }
1013+ }
81ddc4dc
WD
1014+ if (unchanged == 1)
1015+ unchanged = 0;
8db8e7d2
WD
1016+ } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
1017+
81ddc4dc 1018+ return unchanged;
fa26e11c
WD
1019+}
1020+
0870c22a 1021+/* === Enumeration functions for uid mapping === */
fa26e11c 1022+
4edb99c8
WD
1023+/* Context -- one and only one. Should be cycled through once on uid
1024+ * mapping and once on gid mapping. */
0870c22a
WD
1025+static item_list *_enum_racl_lists[] = {
1026+ &access_acl_list, &default_acl_list, NULL
fa26e11c
WD
1027+};
1028+
0870c22a
WD
1029+static item_list **enum_racl_list = &_enum_racl_lists[0];
1030+static int enum_ida_index = 0;
fa26e11c 1031+static size_t enum_racl_index = 0;
fa26e11c 1032+
4edb99c8
WD
1033+/* This returns the next tag_type id from the given acl for the next entry,
1034+ * or it returns 0 if there are no more tag_type ids in the acl. */
c52977bc 1035+static id_t *next_ace_id(SMB_ACL_TAG_T tag_type, const rsync_acl *racl)
fa26e11c 1036+{
0870c22a 1037+ const ida_entries *idal = tag_type == SMB_ACL_USER ? &racl->users : &racl->groups;
c6437996
WD
1038+ if (enum_ida_index < idal->count) {
1039+ id_access *ida = &idal->idas[enum_ida_index++];
1040+ return &ida->id;
fa26e11c 1041+ }
c6437996 1042+ enum_ida_index = 0;
c52977bc 1043+ return NULL;
fa26e11c
WD
1044+}
1045+
0870c22a 1046+static id_t *next_acl_id(SMB_ACL_TAG_T tag_type, const item_list *racl_list)
fa26e11c
WD
1047+{
1048+ for (; enum_racl_index < racl_list->count; enum_racl_index++) {
0870c22a
WD
1049+ id_t *id;
1050+ acl_duo *duo_item = racl_list->items;
1051+ duo_item += enum_racl_index;
1052+ if ((id = next_ace_id(tag_type, &duo_item->racl)) != NULL)
fa26e11c
WD
1053+ return id;
1054+ }
1055+ enum_racl_index = 0;
c52977bc 1056+ return NULL;
fa26e11c
WD
1057+}
1058+
c52977bc 1059+static id_t *next_acl_list_id(SMB_ACL_TAG_T tag_type)
fa26e11c
WD
1060+{
1061+ for (; *enum_racl_list; enum_racl_list++) {
c52977bc 1062+ id_t *id = next_acl_id(tag_type, *enum_racl_list);
fa26e11c
WD
1063+ if (id)
1064+ return id;
1065+ }
1066+ enum_racl_list = &_enum_racl_lists[0];
c52977bc 1067+ return NULL;
fa26e11c
WD
1068+}
1069+
c52977bc 1070+id_t *next_acl_uid()
fa26e11c
WD
1071+{
1072+ return next_acl_list_id(SMB_ACL_USER);
1073+}
1074+
c52977bc 1075+id_t *next_acl_gid()
fa26e11c
WD
1076+{
1077+ return next_acl_list_id(SMB_ACL_GROUP);
1078+}
1079+
0870c22a 1080+/* This is used by dest_mode(). */
26c810d8 1081+int default_perms_for_dir(const char *dir)
90fa6d68
WD
1082+{
1083+ rsync_acl racl;
1084+ SMB_ACL_T sacl;
c6437996 1085+ BOOL ok;
26c810d8 1086+ int perms;
90fa6d68
WD
1087+
1088+ if (dir == NULL)
1089+ dir = ".";
26c810d8 1090+ perms = ACCESSPERMS & ~orig_umask;
90fa6d68
WD
1091+ /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1092+ sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1093+ if (sacl == NULL) {
1094+ /* Couldn't get an ACL. Darn. */
1095+ switch (errno) {
1096+ case ENOTSUP:
1097+ /* ACLs are disabled. We could yell at the user to turn them on, but... */
1098+ break;
1099+ case ENOENT:
1100+ if (dry_run) {
1101+ /* We're doing a dry run, so the containing directory
1102+ * wasn't actually created. Don't worry about it. */
1103+ break;
1104+ }
1105+ /* Otherwise fall through. */
1106+ default:
26c810d8 1107+ rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
90fa6d68
WD
1108+ dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1109+ }
26c810d8 1110+ return perms;
90fa6d68
WD
1111+ }
1112+
1113+ /* Convert it. */
1114+ ok = unpack_smb_acl(&racl, sacl);
1115+ sys_acl_free_acl(sacl);
1116+ if (!ok) {
26c810d8
WD
1117+ rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1118+ return perms;
90fa6d68
WD
1119+ }
1120+
c6437996 1121+ /* Apply the permission-bit entries of the default ACL, if any. */
0870c22a 1122+ if (racl.user_obj != NO_ENTRY) {
c6437996
WD
1123+ perms = rsync_acl_get_perms(&racl);
1124+ if (verbose > 2)
1125+ rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
90fa6d68 1126+ }
c6437996 1127+
90fa6d68 1128+ rsync_acl_free(&racl);
26c810d8 1129+ return perms;
90fa6d68
WD
1130+}
1131+
fa26e11c 1132+#endif /* SUPPORT_ACLS */
9a7eef96
WD
1133--- old/backup.c
1134+++ new/backup.c
c1471566 1135@@ -29,6 +29,7 @@ extern char *backup_suffix;
162234a7
WD
1136 extern char *backup_dir;
1137
1138 extern int am_root;
1139+extern int preserve_acls;
1140 extern int preserve_devices;
1141 extern int preserve_specials;
1142 extern int preserve_links;
0870c22a
WD
1143@@ -94,7 +95,8 @@ path
1144 ****************************************************************************/
1145 static int make_bak_dir(char *fullpath)
1146 {
1147- STRUCT_STAT st;
1148+ statx sx;
1149+ struct file_struct *file;
1150 char *rel = fullpath + backup_dir_len;
1151 char *end = rel + strlen(rel);
1152 char *p = end;
1153@@ -126,13 +128,24 @@ static int make_bak_dir(char *fullpath)
1154 if (p >= rel) {
1155 /* Try to transfer the directory settings of the
1156 * actual dir that the files are coming from. */
1157- if (do_stat(rel, &st) < 0) {
1158+ if (do_stat(rel, &sx.st) < 0) {
1159 rsyserr(FERROR, errno,
1160 "make_bak_dir stat %s failed",
1161 full_fname(rel));
fa26e11c 1162 } else {
0870c22a
WD
1163- do_lchown(fullpath, st.st_uid, st.st_gid);
1164- do_chmod(fullpath, st.st_mode);
1165+#ifdef SUPPORT_ACLS
1166+ sx.acc_acl = sx.def_acl = NULL;
1167+#endif
1168+ if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
1169+ continue;
e6a7303b 1170+#ifdef SUPPORT_ACLS
0870c22a 1171+ if (preserve_acls) {
98ccc74a
WD
1172+ get_acl(rel, &sx);
1173+ cache_acl(file, &sx);
0870c22a 1174+ }
e6a7303b 1175+#endif
0870c22a
WD
1176+ set_file_attrs(fullpath, file, NULL, 0);
1177+ free(file);
fa26e11c
WD
1178 }
1179 }
1180 *p = '/';
0870c22a
WD
1181@@ -170,15 +183,18 @@ static int robust_move(char *src, char *
1182 * We will move the file to be deleted into a parallel directory tree. */
1183 static int keep_backup(char *fname)
1184 {
1185- STRUCT_STAT st;
1186+ statx sx;
1187 struct file_struct *file;
1188 char *buf;
1189 int kept = 0;
1190 int ret_code;
1191
1192 /* return if no file to keep */
1193- if (do_lstat(fname, &st) < 0)
1194+ if (do_lstat(fname, &sx.st) < 0)
1195 return 1;
1196+#ifdef SUPPORT_ACLS
1197+ sx.acc_acl = sx.def_acl = NULL;
1198+#endif
1199
1200 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1201 return 1; /* the file could have disappeared */
1202@@ -186,6 +202,13 @@ static int keep_backup(char *fname)
172ad6c3 1203 if (!(buf = get_backup_name(fname)))
fa26e11c 1204 return 0;
fa26e11c 1205
e6a7303b 1206+#ifdef SUPPORT_ACLS
0870c22a 1207+ if (preserve_acls) {
98ccc74a
WD
1208+ get_acl(fname, &sx);
1209+ cache_acl(file, &sx);
0870c22a 1210+ }
e6a7303b 1211+#endif
fa26e11c 1212+
fa26e11c 1213 /* Check to see if this is a device file, or link */
90fa6d68
WD
1214 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1215 || (preserve_specials && IS_SPECIAL(file->mode))) {
0870c22a
WD
1216@@ -254,7 +277,7 @@ static int keep_backup(char *fname)
1217 if (robust_move(fname, buf) != 0) {
1218 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
1219 full_fname(fname), buf);
1220- } else if (st.st_nlink > 1) {
1221+ } else if (sx.st.st_nlink > 1) {
1222 /* If someone has hard-linked the file into the backup
1223 * dir, rename() might return success but do nothing! */
1224 robust_unlink(fname); /* Just in case... */
9a7eef96
WD
1225--- old/configure.in
1226+++ new/configure.in
afcb578c 1227@@ -482,6 +482,11 @@ if test x"$ac_cv_func_strcasecmp" = x"no
fa26e11c
WD
1228 AC_CHECK_LIB(resolv, strcasecmp)
1229 fi
1230
1231+AC_CHECK_FUNCS(aclsort)
1232+if test x"$ac_cv_func_aclsort" = x"no"; then
1233+ AC_CHECK_LIB(sec, aclsort)
1234+fi
1235+
1236 dnl At the moment we don't test for a broken memcmp(), because all we
1237 dnl need to do is test for equality, not comparison, and it seems that
1238 dnl every platform has a memcmp that can do at least that.
936b488a 1239@@ -746,6 +751,78 @@ AC_SUBST(OBJ_RESTORE)
fa26e11c
WD
1240 AC_SUBST(CC_SHOBJ_FLAG)
1241 AC_SUBST(BUILD_POPT)
125d7fca 1242
fa26e11c
WD
1243+AC_CHECK_HEADERS(sys/acl.h)
1244+AC_CHECK_FUNCS(_acl __acl _facl __facl)
1245+#################################################
1246+# check for ACL support
1247+
1248+AC_MSG_CHECKING(whether to support ACLs)
f787c90c
WD
1249+AC_ARG_ENABLE(acl-support,
1250+AC_HELP_STRING([--enable-acl-support], [Include ACL support (default=no)]),
3b05e91f 1251+[ case "$enableval" in
fa26e11c
WD
1252+ yes)
1253+
1254+ case "$host_os" in
1255+ *sysv5*)
1256+ AC_MSG_RESULT(Using UnixWare ACLs)
1257+ AC_DEFINE(HAVE_UNIXWARE_ACLS, 1, [true if you have UnixWare ACLs])
1258+ ;;
53c1073a 1259+ *solaris*|*cygwin*)
fa26e11c
WD
1260+ AC_MSG_RESULT(Using solaris ACLs)
1261+ AC_DEFINE(HAVE_SOLARIS_ACLS, 1, [true if you have solaris ACLs])
1262+ ;;
1263+ *hpux*)
1264+ AC_MSG_RESULT(Using HPUX ACLs)
1265+ AC_DEFINE(HAVE_HPUX_ACLS, 1, [true if you have HPUX ACLs])
1266+ ;;
1267+ *irix*)
1268+ AC_MSG_RESULT(Using IRIX ACLs)
1269+ AC_DEFINE(HAVE_IRIX_ACLS, 1, [true if you have IRIX ACLs])
1270+ ;;
1271+ *aix*)
1272+ AC_MSG_RESULT(Using AIX ACLs)
1273+ AC_DEFINE(HAVE_AIX_ACLS, 1, [true if you have AIX ACLs])
1274+ ;;
1275+ *osf*)
1276+ AC_MSG_RESULT(Using Tru64 ACLs)
1277+ AC_DEFINE(HAVE_TRU64_ACLS, 1, [true if you have Tru64 ACLs])
1278+ LIBS="$LIBS -lpacl"
1279+ ;;
1280+ *)
81549708 1281+ AC_MSG_RESULT(ACLs requested -- running tests)
fa26e11c
WD
1282+ AC_CHECK_LIB(acl,acl_get_file)
1283+ AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1284+ AC_TRY_LINK([#include <sys/types.h>
1285+#include <sys/acl.h>],
1286+[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1287+samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
03d25fc5 1288+ AC_MSG_CHECKING(ACL test results)
fa26e11c
WD
1289+ if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1290+ AC_MSG_RESULT(Using posix ACLs)
1291+ AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
81549708 1292+ AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
fa26e11c
WD
1293+ AC_TRY_LINK([#include <sys/types.h>
1294+#include <sys/acl.h>],
1295+[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1296+samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
81549708
WD
1297+ if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1298+ AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1299+ fi
1300+ else
1301+ AC_MSG_ERROR(Failed to find ACL support)
fa26e11c
WD
1302+ fi
1303+ ;;
1304+ esac
1305+ ;;
1306+ *)
1307+ AC_MSG_RESULT(no)
1308+ AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1309+ ;;
1310+ esac ],
1311+ AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1312+ AC_MSG_RESULT(no)
1313+)
125d7fca 1314+
fa26e11c
WD
1315 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1316 AC_OUTPUT
125d7fca 1317
9a7eef96
WD
1318--- old/flist.c
1319+++ new/flist.c
21158bc6 1320@@ -41,6 +41,7 @@ extern int curr_dir_depth;
162234a7
WD
1321 extern int one_file_system;
1322 extern int copy_dirlinks;
1323 extern int keep_dirlinks;
1324+extern int preserve_acls;
1325 extern int preserve_links;
1326 extern int preserve_hard_links;
1327 extern int preserve_devices;
21158bc6 1328@@ -134,6 +135,8 @@ static void list_file_entry(struct file_
0870c22a
WD
1329
1330 permstring(permbuf, f->mode);
1331
98ccc74a 1332+ /* TODO: indicate '+' if the entry has an ACL. */
0870c22a
WD
1333+
1334 #ifdef SUPPORT_LINKS
1335 if (preserve_links && S_ISLNK(f->mode)) {
1336 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
21158bc6 1337@@ -492,6 +495,9 @@ static struct file_struct *receive_file_
0870c22a
WD
1338 char thisname[MAXPATHLEN];
1339 unsigned int l1 = 0, l2 = 0;
1340 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
1341+#ifdef SUPPORT_ACLS
f0e2362d 1342+ int xtra_len;
0870c22a
WD
1343+#endif
1344 OFF_T file_length;
1345 char *basename, *dirname, *bp;
1346 struct file_struct *file;
21158bc6 1347@@ -595,13 +601,27 @@ static struct file_struct *receive_file_
0870c22a
WD
1348
1349 sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
1350
1351+#ifdef SUPPORT_ACLS
1352+ /* We need one or two index int32s when we're preserving ACLs. */
1353+ if (preserve_acls)
98ccc74a 1354+ xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4;
0870c22a 1355+ else
f0e2362d 1356+ xtra_len = 0;
0870c22a
WD
1357+#endif
1358+
1359 alloc_len = file_struct_len + dirname_len + basename_len
1360+#ifdef SUPPORT_ACLS
f0e2362d 1361+ + xtra_len
0870c22a
WD
1362+#endif
1363 + linkname_len + sum_len;
1364 bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
1365
1366 file = (struct file_struct *)bp;
1367 memset(bp, 0, file_struct_len);
1368 bp += file_struct_len;
1369+#ifdef SUPPORT_ACLS
f0e2362d 1370+ bp += xtra_len;
0870c22a
WD
1371+#endif
1372
1373 file->modtime = modtime;
1374 file->length = file_length;
21158bc6 1375@@ -694,6 +714,11 @@ static struct file_struct *receive_file_
0870c22a
WD
1376 read_buf(f, sum, checksum_len);
1377 }
1378
1379+#ifdef SUPPORT_ACLS
1380+ if (preserve_acls)
98ccc74a 1381+ receive_acl(file, f);
0870c22a
WD
1382+#endif
1383+
1384 return file;
1385 }
1386
21158bc6 1387@@ -943,6 +968,9 @@ static struct file_struct *send_file_nam
0870c22a
WD
1388 unsigned short flags)
1389 {
1390 struct file_struct *file;
1391+#ifdef SUPPORT_ACLS
1392+ statx sx;
1393+#endif
1394
1395 file = make_file(fname, flist, stp, flags,
1396 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
21158bc6 1397@@ -952,6 +980,15 @@ static struct file_struct *send_file_nam
cf51033a
WD
1398 if (chmod_modes && !S_ISLNK(file->mode))
1399 file->mode = tweak_mode(file->mode, chmod_modes);
1400
e6a7303b 1401+#ifdef SUPPORT_ACLS
0870c22a
WD
1402+ if (preserve_acls) {
1403+ sx.st.st_mode = file->mode;
1404+ sx.acc_acl = sx.def_acl = NULL;
98ccc74a 1405+ if (get_acl(fname, &sx) < 0)
0870c22a
WD
1406+ return NULL;
1407+ }
e6a7303b 1408+#endif
cf51033a
WD
1409+
1410 maybe_emit_filelist_progress(flist->count + flist_count_offset);
fa26e11c 1411
cf51033a 1412 flist_expand(flist);
21158bc6 1413@@ -959,6 +996,15 @@ static struct file_struct *send_file_nam
fa26e11c
WD
1414 if (file->basename[0]) {
1415 flist->files[flist->count++] = file;
e0e47893 1416 send_file_entry(file, f);
e6a7303b 1417+#ifdef SUPPORT_ACLS
162234a7 1418+ if (preserve_acls)
98ccc74a 1419+ send_acl(&sx, f);
162234a7 1420+#endif
fa26e11c 1421+ } else {
162234a7 1422+#ifdef SUPPORT_ACLS
0870c22a 1423+ if (preserve_acls)
98ccc74a 1424+ free_acl(&sx);
0870c22a
WD
1425+#endif
1426 }
1427 return file;
1428 }
1429--- old/generator.c
1430+++ new/generator.c
1431@@ -36,6 +36,7 @@ extern int recurse;
1432 extern int relative_paths;
1433 extern int implied_dirs;
1434 extern int keep_dirlinks;
1435+extern int preserve_acls;
1436 extern int preserve_links;
1437 extern int preserve_devices;
1438 extern int preserve_specials;
21158bc6 1439@@ -85,6 +86,7 @@ extern long block_size; /* "long" becaus
0870c22a
WD
1440 extern int max_delete;
1441 extern int force_delete;
1442 extern int one_file_system;
1443+extern mode_t orig_umask;
1444 extern struct stats stats;
1445 extern dev_t filesystem_dev;
1446 extern char *backup_dir;
21158bc6 1447@@ -317,22 +319,27 @@ static void do_delete_pass(struct file_l
0870c22a
WD
1448 rprintf(FINFO, " \r");
1449 }
1450
1451-int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
1452+int unchanged_attrs(struct file_struct *file, statx *sxp)
1453 {
1454 if (preserve_perms
1455- && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
1456+ && (sxp->st.st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
1457 return 0;
1458
1459- if (am_root && preserve_uid && st->st_uid != file->uid)
1460+ if (am_root && preserve_uid && sxp->st.st_uid != file->uid)
1461 return 0;
1462
1463- if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
1464+ if (preserve_gid && file->gid != GID_NONE && sxp->st.st_gid != file->gid)
1465 return 0;
1466
1467+#ifdef SUPPORT_ACLS
98ccc74a 1468+ if (preserve_acls && set_acl(NULL, file, sxp) == 0)
0870c22a
WD
1469+ return 0;
1470+#endif
1471+
1472 return 1;
1473 }
1474
1475-void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
1476+void itemize(struct file_struct *file, int ndx, int statret, statx *sxp,
1477 int32 iflags, uchar fnamecmp_type, char *xname)
1478 {
1479 if (statret >= 0) { /* A from-dest-dir statret can == 1! */
21158bc6 1480@@ -340,19 +347,23 @@ void itemize(struct file_struct *file, i
0870c22a
WD
1481 : S_ISDIR(file->mode) ? !omit_dir_times
1482 : !S_ISLNK(file->mode);
1483
1484- if (S_ISREG(file->mode) && file->length != st->st_size)
1485+ if (S_ISREG(file->mode) && file->length != sxp->st.st_size)
1486 iflags |= ITEM_REPORT_SIZE;
1487 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
1488 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
1489- || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
1490+ || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
1491 iflags |= ITEM_REPORT_TIME;
1492- if ((file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
1493+ if ((file->mode & CHMOD_BITS) != (sxp->st.st_mode & CHMOD_BITS))
1494 iflags |= ITEM_REPORT_PERMS;
1495- if (preserve_uid && am_root && file->uid != st->st_uid)
1496+ if (preserve_uid && am_root && file->uid != sxp->st.st_uid)
1497 iflags |= ITEM_REPORT_OWNER;
1498 if (preserve_gid && file->gid != GID_NONE
1499- && st->st_gid != file->gid)
1500+ && sxp->st.st_gid != file->gid)
1501 iflags |= ITEM_REPORT_GROUP;
1502+#ifdef SUPPORT_ACLS
98ccc74a 1503+ if (preserve_acls && set_acl(NULL, file, sxp) == 0)
0870c22a
WD
1504+ iflags |= ITEM_REPORT_ACL;
1505+#endif
1506 } else
1507 iflags |= ITEM_IS_NEW;
1508
21158bc6 1509@@ -603,7 +614,7 @@ void check_for_finished_hlinks(int itemi
0870c22a
WD
1510 * handling the file, -1 if no dest-linking occurred, or a non-negative
1511 * value if we found an alternate basis file. */
1512 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
1513- char *cmpbuf, STRUCT_STAT *stp, int itemizing,
1514+ char *cmpbuf, statx *sxp, int itemizing,
1515 int maybe_ATTRS_REPORT, enum logcode code)
1516 {
af7eb67a 1517 int save_ignore_times = ignore_times;
21158bc6 1518@@ -618,7 +629,7 @@ static int try_dests_reg(struct file_str
0870c22a
WD
1519
1520 do {
1521 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1522- if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
1523+ if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
1524 continue;
1525 switch (match_level) {
1526 case 0:
21158bc6 1527@@ -626,16 +637,20 @@ static int try_dests_reg(struct file_str
0870c22a
WD
1528 match_level = 1;
1529 /* FALL THROUGH */
1530 case 1:
1531- if (!unchanged_file(cmpbuf, file, stp))
1532+ if (!unchanged_file(cmpbuf, file, &sxp->st))
1533 continue;
1534 best_match = j;
1535 match_level = 2;
1536 /* FALL THROUGH */
1537 case 2:
1538- if (!unchanged_attrs(file, stp))
1539+#ifdef SUPPORT_ACLS
1540+ if (preserve_acls)
98ccc74a 1541+ get_acl(cmpbuf, sxp);
0870c22a
WD
1542+#endif
1543+ if (!unchanged_attrs(file, sxp))
1544 continue;
af7eb67a 1545 if (always_checksum
0870c22a
WD
1546- && cmp_time(stp->st_mtime, file->modtime))
1547+ && cmp_time(sxp->st.st_mtime, file->modtime))
1548 continue;
1549 best_match = j;
1550 match_level = 3;
21158bc6 1551@@ -653,22 +668,27 @@ static int try_dests_reg(struct file_str
0870c22a
WD
1552 if (j != best_match) {
1553 j = best_match;
1554 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1555- if (link_stat(cmpbuf, stp, 0) < 0)
1556+ if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1557 match_level = 0;
1558 }
1559
1560 #ifdef HAVE_LINK
1561 if (match_level == 3 && !copy_dest) {
1562 if (link_dest) {
1563- if (hard_link_one(file, ndx, fname, 0, stp,
1564+ if (hard_link_one(file, ndx, fname, 0, sxp,
1565 cmpbuf, 1,
1566 itemizing && verbose > 1,
1567 code) < 0)
1568 goto try_a_copy;
1569 if (preserve_hard_links && file->link_u.links)
1570 hard_link_cluster(file, ndx, itemizing, code);
1571- } else if (itemizing)
1572- itemize(file, ndx, 0, stp, 0, 0, NULL);
1573+ } else if (itemizing) {
1574+#ifdef SUPPORT_ACLS
1575+ if (preserve_acls && !ACL_READY(*sxp))
98ccc74a 1576+ get_acl(fname, sxp);
0870c22a
WD
1577+#endif
1578+ itemize(file, ndx, 0, sxp, 0, 0, NULL);
1579+ }
1580 if (verbose > 1 && maybe_ATTRS_REPORT) {
1581 code = daemon_log_format_has_i || dry_run
1582 ? FCLIENT : FINFO;
21158bc6 1583@@ -687,8 +707,13 @@ static int try_dests_reg(struct file_str
0870c22a
WD
1584 }
1585 return -1;
1586 }
1587- if (itemizing)
1588- itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
1589+ if (itemizing) {
1590+#ifdef SUPPORT_ACLS
1591+ if (preserve_acls && !ACL_READY(*sxp))
98ccc74a 1592+ get_acl(fname, sxp);
e6a7303b 1593+#endif
0870c22a
WD
1594+ itemize(file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
1595+ }
1596 set_file_attrs(fname, file, NULL, 0);
1597 if (maybe_ATTRS_REPORT
1598 && ((!itemizing && verbose && match_level == 2)
21158bc6 1599@@ -713,13 +738,18 @@ static int try_dests_non(struct file_str
0870c22a
WD
1600 enum logcode code)
1601 {
1602 char fnamebuf[MAXPATHLEN];
1603- STRUCT_STAT st;
1604+ statx sx;
1605 int i = 0;
fa26e11c 1606
0870c22a
WD
1607 do {
1608 pathjoin(fnamebuf, MAXPATHLEN, basis_dir[i], fname);
1609- if (link_stat(fnamebuf, &st, 0) < 0 || S_ISDIR(st.st_mode)
1610- || !unchanged_attrs(file, &st))
1611+ if (link_stat(fnamebuf, &sx.st, 0) < 0 || S_ISDIR(sx.st.st_mode))
1612+ continue;
e6a7303b 1613+#ifdef SUPPORT_ACLS
162234a7 1614+ if (preserve_acls)
98ccc74a 1615+ get_acl(fnamebuf, &sx);
e6a7303b 1616+#endif
0870c22a
WD
1617+ if (!unchanged_attrs(file, &sx))
1618 continue;
1619 if (S_ISLNK(file->mode)) {
1620 #ifdef SUPPORT_LINKS
21158bc6 1621@@ -732,10 +762,10 @@ static int try_dests_non(struct file_str
0870c22a
WD
1622 #endif
1623 continue;
1624 } else if (IS_SPECIAL(file->mode)) {
1625- if (!IS_SPECIAL(st.st_mode) || st.st_rdev != file->u.rdev)
1626+ if (!IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev != file->u.rdev)
1627 continue;
1628 } else if (IS_DEVICE(file->mode)) {
1629- if (!IS_DEVICE(st.st_mode) || st.st_rdev != file->u.rdev)
1630+ if (!IS_DEVICE(sx.st.st_mode) || sx.st.st_rdev != file->u.rdev)
1631 continue;
1632 } else {
1633 rprintf(FERROR,
21158bc6 1634@@ -764,7 +794,15 @@ static int try_dests_non(struct file_str
0870c22a
WD
1635 int changes = compare_dest ? 0 : ITEM_LOCAL_CHANGE
1636 + (link_dest ? ITEM_XNAME_FOLLOWS : 0);
1637 char *lp = link_dest ? "" : NULL;
1638- itemize(file, ndx, 0, &st, changes, 0, lp);
e6a7303b 1639+#ifdef SUPPORT_ACLS
0870c22a 1640+ if (preserve_acls)
98ccc74a 1641+ get_acl(fname, &sx);
e6a7303b 1642+#endif
0870c22a
WD
1643+ itemize(file, ndx, 0, &sx, changes, 0, lp);
1644+#ifdef SUPPORT_ACLS
1645+ if (preserve_acls)
98ccc74a 1646+ free_acl(&sx);
0870c22a
WD
1647+#endif
1648 }
1649 if (verbose > 1 && maybe_ATTRS_REPORT) {
1650 code = daemon_log_format_has_i || dry_run
21158bc6 1651@@ -778,6 +816,7 @@ static int try_dests_non(struct file_str
26c810d8
WD
1652 }
1653
1654 static int phase = 0;
1655+static int dflt_perms;
1656
1657 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
1658 * make sure it exists, and has the right permissions/timestamp info. For
21158bc6 1659@@ -799,7 +838,8 @@ static void recv_generator(char *fname,
0870c22a
WD
1660 static int need_fuzzy_dirlist = 0;
1661 struct file_struct *fuzzy_file = NULL;
1662 int fd = -1, f_copy = -1;
1663- STRUCT_STAT st, real_st, partial_st;
1664+ statx sx, real_sx;
1665+ STRUCT_STAT partial_st;
1666 struct file_struct *back_file = NULL;
1667 int statret, real_ret, stat_errno;
1668 char *fnamecmp, *partialptr, *backupptr = NULL;
21158bc6 1669@@ -850,6 +890,9 @@ static void recv_generator(char *fname,
0870c22a
WD
1670 dry_run--;
1671 missing_below = -1;
1672 }
1673+#ifdef SUPPORT_ACLS
1674+ sx.acc_acl = sx.def_acl = NULL;
1675+#endif
1676 if (dry_run > 1) {
1677 statret = -1;
1678 stat_errno = ENOENT;
21158bc6 1679@@ -857,7 +900,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1680 char *dn = file->dirname ? file->dirname : ".";
1681 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1682 if (relative_paths && !implied_dirs
21158bc6
WD
1683- && safe_stat(dn, &st) < 0
1684+ && safe_stat(dn, &sx.st) < 0
0870c22a
WD
1685 && create_directory_path(fname) < 0) {
1686 rsyserr(FERROR, errno,
1687 "recv_generator: mkdir %s failed",
21158bc6 1688@@ -869,6 +912,10 @@ static void recv_generator(char *fname,
01deb4dc
WD
1689 }
1690 if (fuzzy_basis)
63673ef2 1691 need_fuzzy_dirlist = 1;
26c810d8 1692+#ifdef SUPPORT_ACLS
01deb4dc
WD
1693+ if (!preserve_perms)
1694+ dflt_perms = default_perms_for_dir(dn);
26c810d8 1695+#endif
26c810d8 1696 }
01deb4dc 1697 parent_dirname = dn;
26c810d8 1698
21158bc6 1699@@ -877,7 +924,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1700 need_fuzzy_dirlist = 0;
1701 }
1702
1703- statret = link_stat(fname, &st,
1704+ statret = link_stat(fname, &sx.st,
1705 keep_dirlinks && S_ISDIR(file->mode));
1706 stat_errno = errno;
1707 }
21158bc6 1708@@ -895,8 +942,9 @@ static void recv_generator(char *fname,
0870c22a 1709 * mode based on the local permissions and some heuristics. */
90fa6d68
WD
1710 if (!preserve_perms) {
1711 int exists = statret == 0
0870c22a 1712- && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
90fa6d68 1713- file->mode = dest_mode(file->mode, st.st_mode, exists);
0870c22a
WD
1714+ && S_ISDIR(sx.st.st_mode) == S_ISDIR(file->mode);
1715+ file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
26c810d8 1716+ exists);
90fa6d68
WD
1717 }
1718
1719 if (S_ISDIR(file->mode)) {
21158bc6 1720@@ -905,8 +953,8 @@ static void recv_generator(char *fname,
0870c22a
WD
1721 * file of that name and it is *not* a directory, then
1722 * we need to delete it. If it doesn't exist, then
1723 * (perhaps recursively) create it. */
1724- if (statret == 0 && !S_ISDIR(st.st_mode)) {
1725- if (delete_item(fname, st.st_mode, del_opts) < 0)
1726+ if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1727+ if (delete_item(fname, sx.st.st_mode, del_opts) < 0)
1728 return;
1729 statret = -1;
1730 }
21158bc6 1731@@ -915,7 +963,11 @@ static void recv_generator(char *fname,
0870c22a
WD
1732 dry_run++;
1733 }
1734 if (itemizing && f_out != -1) {
1735- itemize(file, ndx, statret, &st,
1736+#ifdef SUPPORT_ACLS
1737+ if (preserve_acls && statret == 0)
98ccc74a 1738+ get_acl(fname, &sx);
0870c22a
WD
1739+#endif
1740+ itemize(file, ndx, statret, &sx,
1741 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1742 }
1743 if (statret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
21158bc6 1744@@ -927,19 +979,19 @@ static void recv_generator(char *fname,
0870c22a
WD
1745 full_fname(fname));
1746 }
1747 }
1748- if (set_file_attrs(fname, file, statret ? NULL : &st, 0)
1749+ if (set_file_attrs(fname, file, statret ? NULL : &sx, 0)
1750 && verbose && code && f_out != -1)
1751 rprintf(code, "%s/\n", fname);
1752 if (delete_during && f_out != -1 && !phase && dry_run < 2
1753 && (file->flags & FLAG_DEL_HERE))
1754- delete_in_dir(the_file_list, fname, file, &st);
1755- return;
1756+ delete_in_dir(the_file_list, fname, file, &sx.st);
1757+ goto cleanup;
1758 }
1759
1760 if (preserve_hard_links && file->link_u.links
1761- && hard_link_check(file, ndx, fname, statret, &st,
1762+ && hard_link_check(file, ndx, fname, statret, &sx,
1763 itemizing, code, HL_CHECK_MASTER))
1764- return;
1765+ goto cleanup;
1766
1767 if (preserve_links && S_ISLNK(file->mode)) {
1768 #ifdef SUPPORT_LINKS
21158bc6 1769@@ -957,7 +1009,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1770 char lnk[MAXPATHLEN];
1771 int len;
1772
1773- if (!S_ISDIR(st.st_mode)
1774+ if (!S_ISDIR(sx.st.st_mode)
1775 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
1776 lnk[len] = 0;
1777 /* A link already pointing to the
21158bc6 1778@@ -965,10 +1017,10 @@ static void recv_generator(char *fname,
0870c22a
WD
1779 * required. */
1780 if (strcmp(lnk, file->u.link) == 0) {
1781 if (itemizing) {
1782- itemize(file, ndx, 0, &st, 0,
1783+ itemize(file, ndx, 0, &sx, 0,
1784 0, NULL);
1785 }
1786- set_file_attrs(fname, file, &st,
1787+ set_file_attrs(fname, file, &sx,
1788 maybe_ATTRS_REPORT);
1789 if (preserve_hard_links
1790 && file->link_u.links) {
21158bc6 1791@@ -981,9 +1033,9 @@ static void recv_generator(char *fname,
0870c22a
WD
1792 }
1793 /* Not the right symlink (or not a symlink), so
1794 * delete it. */
1795- if (delete_item(fname, st.st_mode, del_opts) < 0)
1796+ if (delete_item(fname, sx.st.st_mode, del_opts) < 0)
1797 return;
1798- if (!S_ISLNK(st.st_mode))
1799+ if (!S_ISLNK(sx.st.st_mode))
1800 statret = -1;
1801 } else if (basis_dir[0] != NULL) {
1802 if (try_dests_non(file, fname, ndx, itemizing,
21158bc6 1803@@ -999,7 +1051,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1804 }
1805 }
1806 if (preserve_hard_links && file->link_u.links
1807- && hard_link_check(file, ndx, fname, -1, &st,
1808+ && hard_link_check(file, ndx, fname, -1, &sx,
1809 itemizing, code, HL_SKIP))
1810 return;
1811 if (do_symlink(file->u.link,fname) != 0) {
21158bc6 1812@@ -1008,7 +1060,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1813 } else {
1814 set_file_attrs(fname, file, NULL, 0);
1815 if (itemizing) {
1816- itemize(file, ndx, statret, &st,
1817+ itemize(file, ndx, statret, &sx,
1818 ITEM_LOCAL_CHANGE, 0, NULL);
1819 }
1820 if (code && verbose) {
21158bc6 1821@@ -1042,18 +1094,22 @@ static void recv_generator(char *fname,
0870c22a
WD
1822 itemizing = code = 0;
1823 }
1824 }
1825+#ifdef SUPPORT_ACLS
1826+ if (preserve_acls && statret == 0)
98ccc74a 1827+ get_acl(fname, &sx);
0870c22a
WD
1828+#endif
1829 if (statret != 0
1830- || (st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
1831- || st.st_rdev != file->u.rdev) {
1832+ || (sx.st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
1833+ || sx.st.st_rdev != file->u.rdev) {
1834 if (statret == 0
1835- && delete_item(fname, st.st_mode, del_opts) < 0)
1836- return;
1837+ && delete_item(fname, sx.st.st_mode, del_opts) < 0)
1838+ goto cleanup;
1839 if (preserve_hard_links && file->link_u.links
1840- && hard_link_check(file, ndx, fname, -1, &st,
1841+ && hard_link_check(file, ndx, fname, -1, &sx,
1842 itemizing, code, HL_SKIP))
1843- return;
1844- if ((IS_DEVICE(file->mode) && !IS_DEVICE(st.st_mode))
1845- || (IS_SPECIAL(file->mode) && !IS_SPECIAL(st.st_mode)))
1846+ goto cleanup;
1847+ if ((IS_DEVICE(file->mode) && !IS_DEVICE(sx.st.st_mode))
1848+ || (IS_SPECIAL(file->mode) && !IS_SPECIAL(sx.st.st_mode)))
1849 statret = -1;
1850 if (verbose > 2) {
1851 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
21158bc6 1852@@ -1066,7 +1122,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1853 } else {
1854 set_file_attrs(fname, file, NULL, 0);
1855 if (itemizing) {
1856- itemize(file, ndx, statret, &st,
1857+ itemize(file, ndx, statret, &sx,
1858 ITEM_LOCAL_CHANGE, 0, NULL);
1859 }
1860 if (code && verbose)
21158bc6 1861@@ -1078,12 +1134,12 @@ static void recv_generator(char *fname,
0870c22a
WD
1862 }
1863 } else {
1864 if (itemizing)
1865- itemize(file, ndx, statret, &st, 0, 0, NULL);
1866- set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1867+ itemize(file, ndx, statret, &sx, 0, 0, NULL);
1868+ set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1869 if (preserve_hard_links && file->link_u.links)
1870 hard_link_cluster(file, ndx, itemizing, code);
1871 }
1872- return;
1873+ goto cleanup;
1874 }
1875
1876 if (!S_ISREG(file->mode)) {
21158bc6 1877@@ -1117,7 +1173,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1878 }
1879
1880 if (update_only && statret == 0
1881- && cmp_time(st.st_mtime, file->modtime) > 0) {
1882+ && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1883 if (verbose > 1)
1884 rprintf(FINFO, "%s is newer\n", fname);
1885 return;
21158bc6 1886@@ -1126,18 +1182,18 @@ static void recv_generator(char *fname,
0870c22a
WD
1887 fnamecmp = fname;
1888 fnamecmp_type = FNAMECMP_FNAME;
1889
1890- if (statret == 0 && !S_ISREG(st.st_mode)) {
1891- if (delete_item(fname, st.st_mode, del_opts) != 0)
1892+ if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1893+ if (delete_item(fname, sx.st.st_mode, del_opts) != 0)
1894 return;
1895 statret = -1;
1896 stat_errno = ENOENT;
1897 }
1898
1899 if (statret != 0 && basis_dir[0] != NULL) {
1900- int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
1901+ int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1902 itemizing, maybe_ATTRS_REPORT, code);
1903 if (j == -2)
1904- return;
1905+ goto cleanup;
1906 if (j != -1) {
1907 fnamecmp = fnamecmpbuf;
1908 fnamecmp_type = j;
21158bc6 1909@@ -1146,7 +1202,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1910 }
1911
1912 real_ret = statret;
1913- real_st = st;
1914+ real_sx = sx;
1915
1916 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1917 && link_stat(partialptr, &partial_st, 0) == 0
21158bc6 1918@@ -1165,7 +1221,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1919 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1920 fname, fnamecmpbuf);
1921 }
1922- st.st_size = fuzzy_file->length;
1923+ sx.st.st_size = fuzzy_file->length;
1924 statret = 0;
1925 fnamecmp = fnamecmpbuf;
1926 fnamecmp_type = FNAMECMP_FUZZY;
21158bc6 1927@@ -1174,7 +1230,7 @@ static void recv_generator(char *fname,
0870c22a
WD
1928
1929 if (statret != 0) {
1930 if (preserve_hard_links && file->link_u.links
1931- && hard_link_check(file, ndx, fname, statret, &st,
1932+ && hard_link_check(file, ndx, fname, statret, &sx,
1933 itemizing, code, HL_SKIP))
1934 return;
1935 if (stat_errno == ENOENT)
21158bc6 1936@@ -1184,31 +1240,44 @@ static void recv_generator(char *fname,
0870c22a
WD
1937 return;
1938 }
1939
1940- if (append_mode && st.st_size > file->length)
1941+ if (append_mode && sx.st.st_size > file->length)
1942 return;
1943
1944 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1945 ;
1946 else if (fnamecmp_type == FNAMECMP_FUZZY)
1947 ;
1948- else if (unchanged_file(fnamecmp, file, &st)) {
1949+ else if (unchanged_file(fnamecmp, file, &sx.st)) {
1950 if (partialptr) {
1951 do_unlink(partialptr);
1952 handle_partial_dir(partialptr, PDIR_DELETE);
1953 }
1954 if (itemizing) {
1955- itemize(file, ndx, real_ret, &real_st,
1956+#ifdef SUPPORT_ACLS
1957+ if (preserve_acls && real_ret == 0)
98ccc74a 1958+ get_acl(fname, &real_sx);
0870c22a
WD
1959+#endif
1960+ itemize(file, ndx, real_ret, &real_sx,
1961 0, 0, NULL);
1962+#ifdef SUPPORT_ACLS
1963+ if (preserve_acls) {
1964+ if (fnamecmp_type == FNAMECMP_FNAME) {
1965+ sx.acc_acl = real_sx.acc_acl;
1966+ sx.def_acl = real_sx.def_acl;
1967+ } else
98ccc74a 1968+ free_acl(&real_sx);
0870c22a
WD
1969+ }
1970+#endif
1971 }
1972- set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1973+ set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1974 if (preserve_hard_links && file->link_u.links)
1975 hard_link_cluster(file, ndx, itemizing, code);
1976- return;
1977+ goto cleanup;
1978 }
1979
1980 prepare_to_open:
1981 if (partialptr) {
1982- st = partial_st;
1983+ sx.st = partial_st;
1984 fnamecmp = partialptr;
1985 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1986 statret = 0;
21158bc6 1987@@ -1232,17 +1301,21 @@ static void recv_generator(char *fname,
0870c22a
WD
1988 pretend_missing:
1989 /* pretend the file didn't exist */
1990 if (preserve_hard_links && file->link_u.links
1991- && hard_link_check(file, ndx, fname, statret, &st,
1992+ && hard_link_check(file, ndx, fname, statret, &sx,
1993 itemizing, code, HL_SKIP))
1994- return;
1995+ goto cleanup;
1996 statret = real_ret = -1;
1997+#ifdef SUPPORT_ACLS
1998+ if (preserve_acls && ACL_READY(sx))
98ccc74a 1999+ free_acl(&sx);
0870c22a
WD
2000+#endif
2001 goto notify_others;
2002 }
2003
2004 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
2005 if (!(backupptr = get_backup_name(fname))) {
2006 close(fd);
2007- return;
2008+ goto cleanup;
2009 }
2010 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
2011 close(fd);
21158bc6 2012@@ -1253,7 +1326,7 @@ static void recv_generator(char *fname,
0870c22a
WD
2013 full_fname(backupptr));
2014 free(back_file);
2015 close(fd);
2016- return;
2017+ goto cleanup;
2018 }
2019 if ((f_copy = do_open(backupptr,
2020 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
21158bc6 2021@@ -1261,14 +1334,14 @@ static void recv_generator(char *fname,
0870c22a
WD
2022 full_fname(backupptr));
2023 free(back_file);
2024 close(fd);
2025- return;
2026+ goto cleanup;
2027 }
2028 fnamecmp_type = FNAMECMP_BACKUP;
2029 }
2030
2031 if (verbose > 3) {
2032 rprintf(FINFO, "gen mapped %s of size %.0f\n",
2033- fnamecmp, (double)st.st_size);
2034+ fnamecmp, (double)sx.st.st_size);
2035 }
2036
2037 if (verbose > 2)
21158bc6 2038@@ -1286,24 +1359,32 @@ static void recv_generator(char *fname,
0870c22a
WD
2039 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
2040 if (fnamecmp_type == FNAMECMP_FUZZY)
2041 iflags |= ITEM_XNAME_FOLLOWS;
2042- itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
2043+#ifdef SUPPORT_ACLS
2044+ if (preserve_acls && real_ret == 0)
98ccc74a 2045+ get_acl(fname, &real_sx);
0870c22a
WD
2046+#endif
2047+ itemize(file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
2048 fuzzy_file ? fuzzy_file->basename : NULL);
2049+#ifdef SUPPORT_ACLS
2050+ if (preserve_acls)
98ccc74a 2051+ free_acl(&real_sx);
0870c22a
WD
2052+#endif
2053 }
2054
2055 if (!do_xfers) {
2056 if (preserve_hard_links && file->link_u.links)
2057 hard_link_cluster(file, ndx, itemizing, code);
2058- return;
2059+ goto cleanup;
2060 }
2061 if (read_batch)
2062- return;
2063+ goto cleanup;
2064
2065 if (statret != 0 || whole_file) {
2066 write_sum_head(f_out, NULL);
2067- return;
2068+ goto cleanup;
2069 }
2070
2071- generate_and_send_sums(fd, st.st_size, f_out, f_copy);
2072+ generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
2073
2074 if (f_copy >= 0) {
2075 close(f_copy);
21158bc6 2076@@ -1316,6 +1397,13 @@ static void recv_generator(char *fname,
0870c22a
WD
2077 }
2078
2079 close(fd);
2080+
2081+ cleanup:
2082+#ifdef SUPPORT_ACLS
2083+ if (preserve_acls)
98ccc74a 2084+ free_acl(&sx);
0870c22a
WD
2085+#endif
2086+ return;
2087 }
2088
2089 void generate_files(int f_out, struct file_list *flist, char *local_name)
21158bc6 2090@@ -1375,6 +1463,8 @@ void generate_files(int f_out, struct fi
26c810d8
WD
2091 * notice that and let us know via the redo pipe (or its closing). */
2092 ignore_timeout = 1;
2093
2094+ dflt_perms = (ACCESSPERMS & ~orig_umask);
2095+
2096 for (i = 0; i < flist->count; i++) {
2097 struct file_struct *file = flist->files[i];
2098
0870c22a
WD
2099--- old/hlink.c
2100+++ new/hlink.c
2101@@ -25,6 +25,7 @@
2102
2103 extern int verbose;
2104 extern int link_dest;
2105+extern int preserve_acls;
2106 extern int make_backups;
2107 extern int log_format_has_i;
2108 extern char *basis_dir[];
2109@@ -143,15 +144,19 @@ void init_hard_links(void)
2110
2111 #ifdef SUPPORT_HARD_LINKS
2112 static int maybe_hard_link(struct file_struct *file, int ndx,
2113- char *fname, int statret, STRUCT_STAT *st,
2114+ char *fname, int statret, statx *sxp,
2115 char *toname, STRUCT_STAT *to_st,
2116 int itemizing, enum logcode code)
2117 {
2118 if (statret == 0) {
2119- if (st->st_dev == to_st->st_dev
2120- && st->st_ino == to_st->st_ino) {
2121+ if (sxp->st.st_dev == to_st->st_dev
2122+ && sxp->st.st_ino == to_st->st_ino) {
2123 if (itemizing) {
2124- itemize(file, ndx, statret, st,
2125+#ifdef SUPPORT_ACLS
2126+ if (preserve_acls && !ACL_READY(*sxp))
98ccc74a 2127+ get_acl(fname, sxp);
0870c22a
WD
2128+#endif
2129+ itemize(file, ndx, statret, sxp,
2130 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
2131 0, "");
2132 }
2133@@ -166,13 +171,13 @@ static int maybe_hard_link(struct file_s
2134 return -1;
2135 }
2136 }
2137- return hard_link_one(file, ndx, fname, statret, st, toname,
2138+ return hard_link_one(file, ndx, fname, statret, sxp, toname,
2139 0, itemizing, code);
2140 }
2141 #endif
2142
2143 int hard_link_check(struct file_struct *file, int ndx, char *fname,
2144- int statret, STRUCT_STAT *st, int itemizing,
2145+ int statret, statx *sxp, int itemizing,
2146 enum logcode code, int skip)
2147 {
2148 #ifdef SUPPORT_HARD_LINKS
2149@@ -207,7 +212,7 @@ int hard_link_check(struct file_struct *
2150 || st2.st_ino != st3.st_ino)
2151 continue;
2152 statret = 1;
2153- st = &st3;
2154+ sxp->st = st3;
2155 if (verbose < 2 || !log_format_has_i)
2156 itemizing = code = 0;
2157 break;
2158@@ -215,12 +220,16 @@ int hard_link_check(struct file_struct *
2159 if (!unchanged_file(cmpbuf, file, &st3))
2160 continue;
2161 statret = 1;
2162- st = &st3;
2163- if (unchanged_attrs(file, &st3))
2164+ sxp->st = st3;
2165+#ifdef SUPPORT_ACLS
2166+ if (preserve_acls)
98ccc74a 2167+ get_acl(cmpbuf, sxp);
0870c22a
WD
2168+#endif
2169+ if (unchanged_attrs(file, sxp))
2170 break;
2171 } while (basis_dir[++j] != NULL);
2172 }
2173- maybe_hard_link(file, ndx, fname, statret, st,
2174+ maybe_hard_link(file, ndx, fname, statret, sxp,
2175 toname, &st2, itemizing, code);
2176 file->F_HLINDEX = FINISHED_LINK;
2177 } else
2178@@ -233,7 +242,7 @@ int hard_link_check(struct file_struct *
2179
2180 #ifdef SUPPORT_HARD_LINKS
2181 int hard_link_one(struct file_struct *file, int ndx, char *fname,
2182- int statret, STRUCT_STAT *st, char *toname, int terse,
2183+ int statret, statx *sxp, char *toname, int terse,
2184 int itemizing, enum logcode code)
2185 {
2186 if (do_link(toname, fname)) {
2187@@ -249,7 +258,11 @@ int hard_link_one(struct file_struct *fi
2188 }
2189
2190 if (itemizing) {
2191- itemize(file, ndx, statret, st,
2192+#ifdef SUPPORT_ACLS
2193+ if (preserve_acls && statret == 0 && !ACL_READY(*sxp))
98ccc74a 2194+ get_acl(fname, sxp);
0870c22a
WD
2195+#endif
2196+ itemize(file, ndx, statret, sxp,
2197 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
2198 terse ? "" : toname);
2199 }
2200@@ -266,11 +279,12 @@ void hard_link_cluster(struct file_struc
2201 #ifdef SUPPORT_HARD_LINKS
2202 char hlink1[MAXPATHLEN];
2203 char *hlink2;
2204- STRUCT_STAT st1, st2;
2205+ statx sx;
2206+ STRUCT_STAT st;
2207 int statret, ndx = master;
2208
2209 file->F_HLINDEX = FINISHED_LINK;
2210- if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
2211+ if (link_stat(f_name(file, hlink1), &st, 0) < 0)
2212 return;
2213 if (!(file->flags & FLAG_HLINK_TOL)) {
2214 while (!(file->flags & FLAG_HLINK_EOL)) {
2215@@ -284,9 +298,13 @@ void hard_link_cluster(struct file_struc
2216 if (file->F_HLINDEX != SKIPPED_LINK)
2217 continue;
2218 hlink2 = f_name(file, NULL);
2219- statret = link_stat(hlink2, &st2, 0);
2220- maybe_hard_link(file, ndx, hlink2, statret, &st2,
2221- hlink1, &st1, itemizing, code);
2222+ statret = link_stat(hlink2, &sx.st, 0);
2223+ maybe_hard_link(file, ndx, hlink2, statret, &sx,
2224+ hlink1, &st, itemizing, code);
2225+#ifdef SUPPORT_ACLS
2226+ if (preserve_acls)
98ccc74a 2227+ free_acl(&sx);
0870c22a
WD
2228+#endif
2229 file->F_HLINDEX = FINISHED_LINK;
2230 } while (!(file->flags & FLAG_HLINK_EOL));
2231 #endif
9a7eef96
WD
2232--- old/lib/sysacls.c
2233+++ new/lib/sysacls.c
0870c22a
WD
2234@@ -0,0 +1,3240 @@
2235+/*
2236+ Unix SMB/CIFS implementation.
2237+ Samba system utilities for ACL support.
2238+ Copyright (C) Jeremy Allison 2000.
2239+
2240+ This program is free software; you can redistribute it and/or modify
2241+ it under the terms of the GNU General Public License as published by
2242+ the Free Software Foundation; either version 2 of the License, or
2243+ (at your option) any later version.
2244+
2245+ This program is distributed in the hope that it will be useful,
2246+ but WITHOUT ANY WARRANTY; without even the implied warranty of
2247+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2248+ GNU General Public License for more details.
2249+
2250+ You should have received a copy of the GNU General Public License
2251+ along with this program; if not, write to the Free Software
2252+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2253+*/
fa26e11c 2254+
0f6733d8 2255+#include "rsync.h"
252945ef 2256+#include "sysacls.h" /****** ADDED ******/
fa26e11c 2257+
252945ef 2258+/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
0f6733d8
WD
2259+void SAFE_FREE(void *mem)
2260+{
2261+ if (mem)
2262+ free(mem);
2263+}
fa26e11c 2264+
5ca9317d
WD
2265+char *uidtoname(uid_t uid)
2266+{
2267+ static char idbuf[12];
2268+ struct passwd *pw;
2269+
2270+ if ((pw = getpwuid(uid)) == NULL) {
2271+ slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
2272+ return idbuf;
2273+ }
2274+ return pw->pw_name;
2275+}
252945ef 2276+/****** EXTRAS -- END ******/
5ca9317d 2277+
0f6733d8
WD
2278+/*
2279+ This file wraps all differing system ACL interfaces into a consistent
2280+ one based on the POSIX interface. It also returns the correct errors
2281+ for older UNIX systems that don't support ACLs.
fa26e11c 2282+
0f6733d8 2283+ The interfaces that each ACL implementation must support are as follows :
fa26e11c 2284+
0f6733d8
WD
2285+ int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2286+ int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2287+ int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
2288+ void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2289+ SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2290+ SMB_ACL_T sys_acl_get_fd(int fd)
2291+ int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
2292+ int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
2293+ char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
2294+ SMB_ACL_T sys_acl_init( int count)
2295+ int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2296+ int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2297+ int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2298+ int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2299+ int sys_acl_valid( SMB_ACL_T theacl )
2300+ int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2301+ int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2302+ int sys_acl_delete_def_file(const char *path)
fa26e11c 2303+
0f6733d8
WD
2304+ This next one is not POSIX complient - but we *have* to have it !
2305+ More POSIX braindamage.
fa26e11c 2306+
0f6733d8 2307+ int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 2308+
0f6733d8
WD
2309+ The generic POSIX free is the following call. We split this into
2310+ several different free functions as we may need to add tag info
2311+ to structures when emulating the POSIX interface.
fa26e11c 2312+
0f6733d8 2313+ int sys_acl_free( void *obj_p)
fa26e11c 2314+
0f6733d8 2315+ The calls we actually use are :
fa26e11c 2316+
0f6733d8
WD
2317+ int sys_acl_free_text(char *text) - free acl_to_text
2318+ int sys_acl_free_acl(SMB_ACL_T posix_acl)
2319+ int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
fa26e11c 2320+
0f6733d8 2321+*/
fa26e11c 2322+
0f6733d8 2323+#if defined(HAVE_POSIX_ACLS)
fa26e11c 2324+
0f6733d8 2325+/* Identity mapping - easy. */
fa26e11c 2326+
0f6733d8 2327+int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 2328+{
0f6733d8 2329+ return acl_get_entry( the_acl, entry_id, entry_p);
fa26e11c
WD
2330+}
2331+
0f6733d8 2332+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c 2333+{
0f6733d8 2334+ return acl_get_tag_type( entry_d, tag_type_p);
fa26e11c
WD
2335+}
2336+
0f6733d8 2337+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c 2338+{
0f6733d8 2339+ return acl_get_permset( entry_d, permset_p);
fa26e11c
WD
2340+}
2341+
0f6733d8 2342+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 2343+{
0f6733d8 2344+ return acl_get_qualifier( entry_d);
fa26e11c
WD
2345+}
2346+
0f6733d8 2347+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 2348+{
0f6733d8 2349+ return acl_get_file( path_p, type);
fa26e11c
WD
2350+}
2351+
2352+SMB_ACL_T sys_acl_get_fd(int fd)
2353+{
2354+ return acl_get_fd(fd);
2355+}
2356+
2357+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
2358+{
2359+ return acl_clear_perms(permset);
2360+}
2361+
0f6733d8 2362+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
2363+{
2364+ return acl_add_perm(permset, perm);
2365+}
2366+
0f6733d8 2367+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
2368+{
2369+#if defined(HAVE_ACL_GET_PERM_NP)
0f6733d8
WD
2370+ /*
2371+ * Required for TrustedBSD-based ACL implementations where
fa26e11c 2372+ * non-POSIX.1e functions are denoted by a _np (non-portable)
0f6733d8
WD
2373+ * suffix.
2374+ */
fa26e11c
WD
2375+ return acl_get_perm_np(permset, perm);
2376+#else
2377+ return acl_get_perm(permset, perm);
2378+#endif
2379+}
2380+
0f6733d8 2381+char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
fa26e11c 2382+{
0f6733d8 2383+ return acl_to_text( the_acl, plen);
fa26e11c
WD
2384+}
2385+
0f6733d8 2386+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
2387+{
2388+ return acl_init(count);
2389+}
2390+
0f6733d8 2391+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
2392+{
2393+ return acl_create_entry(pacl, pentry);
2394+}
2395+
0f6733d8 2396+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
2397+{
2398+ return acl_set_tag_type(entry, tagtype);
2399+}
2400+
0f6733d8 2401+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
2402+{
2403+ return acl_set_qualifier(entry, qual);
2404+}
2405+
0f6733d8 2406+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
2407+{
2408+ return acl_set_permset(entry, permset);
2409+}
2410+
0f6733d8 2411+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c
WD
2412+{
2413+ return acl_valid(theacl);
2414+}
2415+
2416+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2417+{
2418+ return acl_set_file(name, acltype, theacl);
2419+}
2420+
0f6733d8 2421+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
2422+{
2423+ return acl_set_fd(fd, theacl);
2424+}
2425+
2426+int sys_acl_delete_def_file(const char *name)
2427+{
2428+ return acl_delete_def_file(name);
2429+}
2430+
2431+int sys_acl_free_text(char *text)
2432+{
2433+ return acl_free(text);
2434+}
2435+
0f6733d8 2436+int sys_acl_free_acl(SMB_ACL_T the_acl)
fa26e11c
WD
2437+{
2438+ return acl_free(the_acl);
2439+}
2440+
1f839b40 2441+int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
2442+{
2443+ return acl_free(qual);
2444+}
2445+
2446+#elif defined(HAVE_TRU64_ACLS)
0f6733d8
WD
2447+/*
2448+ * The interface to DEC/Compaq Tru64 UNIX ACLs
fa26e11c
WD
2449+ * is based on Draft 13 of the POSIX spec which is
2450+ * slightly different from the Draft 16 interface.
0f6733d8 2451+ *
fa26e11c
WD
2452+ * Also, some of the permset manipulation functions
2453+ * such as acl_clear_perm() and acl_add_perm() appear
2454+ * to be broken on Tru64 so we have to manipulate
0f6733d8
WD
2455+ * the permission bits in the permset directly.
2456+ */
2457+int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 2458+{
0f6733d8 2459+ SMB_ACL_ENTRY_T entry;
fa26e11c
WD
2460+
2461+ if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
2462+ return -1;
2463+ }
2464+
2465+ errno = 0;
2466+ if ((entry = acl_get_entry(the_acl)) != NULL) {
2467+ *entry_p = entry;
2468+ return 1;
2469+ }
2470+
2471+ return errno ? -1 : 0;
2472+}
2473+
0f6733d8 2474+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c 2475+{
0f6733d8 2476+ return acl_get_tag_type( entry_d, tag_type_p);
fa26e11c
WD
2477+}
2478+
0f6733d8 2479+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c 2480+{
0f6733d8 2481+ return acl_get_permset( entry_d, permset_p);
fa26e11c
WD
2482+}
2483+
0f6733d8 2484+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 2485+{
0f6733d8 2486+ return acl_get_qualifier( entry_d);
fa26e11c
WD
2487+}
2488+
0f6733d8 2489+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c
WD
2490+{
2491+ return acl_get_file((char *)path_p, type);
2492+}
2493+
0f6733d8 2494+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c
WD
2495+{
2496+ return acl_get_fd(fd, ACL_TYPE_ACCESS);
2497+}
2498+
0f6733d8 2499+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
fa26e11c 2500+{
0f6733d8 2501+ *permset = 0; /* acl_clear_perm() is broken on Tru64 */
fa26e11c
WD
2502+
2503+ return 0;
2504+}
2505+
0f6733d8 2506+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
2507+{
2508+ if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
2509+ errno = EINVAL;
2510+ return -1;
2511+ }
2512+
0f6733d8 2513+ *permset |= perm; /* acl_add_perm() is broken on Tru64 */
fa26e11c
WD
2514+
2515+ return 0;
2516+}
2517+
0f6733d8 2518+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
2519+{
2520+ return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
2521+}
2522+
0f6733d8 2523+char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
fa26e11c 2524+{
0f6733d8 2525+ return acl_to_text( the_acl, plen);
fa26e11c
WD
2526+}
2527+
0f6733d8 2528+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
2529+{
2530+ return acl_init(count);
2531+}
2532+
0f6733d8 2533+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
2534+{
2535+ SMB_ACL_ENTRY_T entry;
2536+
2537+ if ((entry = acl_create_entry(pacl)) == NULL) {
2538+ return -1;
2539+ }
2540+
2541+ *pentry = entry;
2542+ return 0;
2543+}
2544+
0f6733d8 2545+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
2546+{
2547+ return acl_set_tag_type(entry, tagtype);
2548+}
2549+
0f6733d8 2550+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
2551+{
2552+ return acl_set_qualifier(entry, qual);
2553+}
2554+
0f6733d8 2555+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
2556+{
2557+ return acl_set_permset(entry, permset);
2558+}
2559+
0f6733d8 2560+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c 2561+{
0f6733d8 2562+ acl_entry_t entry;
fa26e11c
WD
2563+
2564+ return acl_valid(theacl, &entry);
2565+}
2566+
0f6733d8 2567+int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
fa26e11c
WD
2568+{
2569+ return acl_set_file((char *)name, acltype, theacl);
2570+}
2571+
0f6733d8 2572+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
2573+{
2574+ return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
2575+}
2576+
0f6733d8 2577+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
2578+{
2579+ return acl_delete_def_file((char *)name);
2580+}
2581+
0f6733d8 2582+int sys_acl_free_text(char *text)
fa26e11c 2583+{
0f6733d8
WD
2584+ /*
2585+ * (void) cast and explicit return 0 are for DEC UNIX
2586+ * which just #defines acl_free_text() to be free()
2587+ */
fa26e11c
WD
2588+ (void) acl_free_text(text);
2589+ return 0;
2590+}
2591+
0f6733d8 2592+int sys_acl_free_acl(SMB_ACL_T the_acl)
fa26e11c
WD
2593+{
2594+ return acl_free(the_acl);
2595+}
2596+
0f6733d8 2597+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
2598+{
2599+ return acl_free_qualifier(qual, tagtype);
2600+}
2601+
2602+#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
2603+
0f6733d8
WD
2604+/*
2605+ * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
2606+ * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
2607+ */
fa26e11c 2608+
0f6733d8
WD
2609+/*
2610+ * Note that while this code implements sufficient functionality
fa26e11c
WD
2611+ * to support the sys_acl_* interfaces it does not provide all
2612+ * of the semantics of the POSIX ACL interfaces.
2613+ *
2614+ * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2615+ * from a call to sys_acl_get_entry() should not be assumed to be
2616+ * valid after calling any of the following functions, which may
2617+ * reorder the entries in the ACL.
2618+ *
2619+ * sys_acl_valid()
2620+ * sys_acl_set_file()
2621+ * sys_acl_set_fd()
2622+ */
2623+
0f6733d8
WD
2624+/*
2625+ * The only difference between Solaris and UnixWare / OpenUNIX is
2626+ * that the #defines for the ACL operations have different names
2627+ */
fa26e11c
WD
2628+#if defined(HAVE_UNIXWARE_ACLS)
2629+
0f6733d8
WD
2630+#define SETACL ACL_SET
2631+#define GETACL ACL_GET
2632+#define GETACLCNT ACL_CNT
fa26e11c
WD
2633+
2634+#endif
2635+
2636+
0f6733d8 2637+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
2638+{
2639+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2640+ errno = EINVAL;
2641+ return -1;
2642+ }
2643+
2644+ if (entry_p == NULL) {
2645+ errno = EINVAL;
2646+ return -1;
2647+ }
2648+
2649+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
2650+ acl_d->next = 0;
2651+ }
2652+
2653+ if (acl_d->next < 0) {
2654+ errno = EINVAL;
2655+ return -1;
2656+ }
2657+
2658+ if (acl_d->next >= acl_d->count) {
2659+ return 0;
2660+ }
2661+
2662+ *entry_p = &acl_d->acl[acl_d->next++];
2663+
2664+ return 1;
2665+}
2666+
0f6733d8 2667+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
2668+{
2669+ *type_p = entry_d->a_type;
2670+
2671+ return 0;
2672+}
2673+
0f6733d8 2674+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
2675+{
2676+ *permset_p = &entry_d->a_perm;
2677+
2678+ return 0;
2679+}
2680+
0f6733d8 2681+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
2682+{
2683+ if (entry_d->a_type != SMB_ACL_USER
2684+ && entry_d->a_type != SMB_ACL_GROUP) {
2685+ errno = EINVAL;
2686+ return NULL;
2687+ }
2688+
2689+ return &entry_d->a_id;
2690+}
2691+
0f6733d8
WD
2692+/*
2693+ * There is no way of knowing what size the ACL returned by
fa26e11c
WD
2694+ * GETACL will be unless you first call GETACLCNT which means
2695+ * making an additional system call.
2696+ *
2697+ * In the hope of avoiding the cost of the additional system
2698+ * call in most cases, we initially allocate enough space for
2699+ * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2700+ * be too small then we use GETACLCNT to find out the actual
0f6733d8
WD
2701+ * size, reallocate the ACL buffer, and then call GETACL again.
2702+ */
fa26e11c 2703+
0f6733d8 2704+#define INITIAL_ACL_SIZE 16
fa26e11c 2705+
0f6733d8 2706+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 2707+{
0f6733d8
WD
2708+ SMB_ACL_T acl_d;
2709+ int count; /* # of ACL entries allocated */
2710+ int naccess; /* # of access ACL entries */
2711+ int ndefault; /* # of default ACL entries */
fa26e11c
WD
2712+
2713+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2714+ errno = EINVAL;
2715+ return NULL;
2716+ }
2717+
2718+ count = INITIAL_ACL_SIZE;
2719+ if ((acl_d = sys_acl_init(count)) == NULL) {
2720+ return NULL;
2721+ }
2722+
0f6733d8
WD
2723+ /*
2724+ * If there isn't enough space for the ACL entries we use
fa26e11c
WD
2725+ * GETACLCNT to determine the actual number of ACL entries
2726+ * reallocate and try again. This is in a loop because it
2727+ * is possible that someone else could modify the ACL and
2728+ * increase the number of entries between the call to
0f6733d8
WD
2729+ * GETACLCNT and the call to GETACL.
2730+ */
fa26e11c
WD
2731+ while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
2732+ && errno == ENOSPC) {
2733+
2734+ sys_acl_free_acl(acl_d);
2735+
2736+ if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
2737+ return NULL;
2738+ }
2739+
2740+ if ((acl_d = sys_acl_init(count)) == NULL) {
2741+ return NULL;
2742+ }
2743+ }
2744+
2745+ if (count < 0) {
2746+ sys_acl_free_acl(acl_d);
2747+ return NULL;
2748+ }
2749+
0f6733d8
WD
2750+ /*
2751+ * calculate the number of access and default ACL entries
fa26e11c
WD
2752+ *
2753+ * Note: we assume that the acl() system call returned a
2754+ * well formed ACL which is sorted so that all of the
0f6733d8
WD
2755+ * access ACL entries preceed any default ACL entries
2756+ */
fa26e11c
WD
2757+ for (naccess = 0; naccess < count; naccess++) {
2758+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2759+ break;
2760+ }
2761+ ndefault = count - naccess;
0f6733d8
WD
2762+
2763+ /*
2764+ * if the caller wants the default ACL we have to copy
fa26e11c 2765+ * the entries down to the start of the acl[] buffer
0f6733d8
WD
2766+ * and mask out the ACL_DEFAULT flag from the type field
2767+ */
fa26e11c 2768+ if (type == SMB_ACL_TYPE_DEFAULT) {
0f6733d8 2769+ int i, j;
fa26e11c
WD
2770+
2771+ for (i = 0, j = naccess; i < ndefault; i++, j++) {
2772+ acl_d->acl[i] = acl_d->acl[j];
2773+ acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2774+ }
2775+
2776+ acl_d->count = ndefault;
2777+ } else {
2778+ acl_d->count = naccess;
2779+ }
2780+
2781+ return acl_d;
2782+}
2783+
0f6733d8 2784+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 2785+{
0f6733d8
WD
2786+ SMB_ACL_T acl_d;
2787+ int count; /* # of ACL entries allocated */
2788+ int naccess; /* # of access ACL entries */
fa26e11c
WD
2789+
2790+ count = INITIAL_ACL_SIZE;
2791+ if ((acl_d = sys_acl_init(count)) == NULL) {
2792+ return NULL;
2793+ }
2794+
2795+ while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2796+ && errno == ENOSPC) {
2797+
2798+ sys_acl_free_acl(acl_d);
2799+
2800+ if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2801+ return NULL;
2802+ }
2803+
2804+ if ((acl_d = sys_acl_init(count)) == NULL) {
2805+ return NULL;
2806+ }
2807+ }
2808+
2809+ if (count < 0) {
2810+ sys_acl_free_acl(acl_d);
2811+ return NULL;
2812+ }
2813+
0f6733d8
WD
2814+ /*
2815+ * calculate the number of access ACL entries
2816+ */
fa26e11c
WD
2817+ for (naccess = 0; naccess < count; naccess++) {
2818+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2819+ break;
2820+ }
0f6733d8 2821+
fa26e11c
WD
2822+ acl_d->count = naccess;
2823+
2824+ return acl_d;
2825+}
2826+
0f6733d8 2827+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2828+{
2829+ *permset_d = 0;
2830+
2831+ return 0;
2832+}
2833+
0f6733d8 2834+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2835+{
2836+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2837+ && perm != SMB_ACL_EXECUTE) {
2838+ errno = EINVAL;
2839+ return -1;
2840+ }
2841+
2842+ if (permset_d == NULL) {
2843+ errno = EINVAL;
2844+ return -1;
2845+ }
2846+
2847+ *permset_d |= perm;
2848+
2849+ return 0;
2850+}
2851+
0f6733d8 2852+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2853+{
2854+ return *permset_d & perm;
2855+}
2856+
0f6733d8 2857+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c 2858+{
0f6733d8
WD
2859+ int i;
2860+ int len, maxlen;
2861+ char *text;
fa26e11c 2862+
0f6733d8
WD
2863+ /*
2864+ * use an initial estimate of 20 bytes per ACL entry
fa26e11c 2865+ * when allocating memory for the text representation
0f6733d8
WD
2866+ * of the ACL
2867+ */
2868+ len = 0;
2869+ maxlen = 20 * acl_d->count;
252945ef 2870+ if ((text = SMB_MALLOC(maxlen)) == NULL) {
fa26e11c
WD
2871+ errno = ENOMEM;
2872+ return NULL;
2873+ }
2874+
2875+ for (i = 0; i < acl_d->count; i++) {
0f6733d8 2876+ struct acl *ap = &acl_d->acl[i];
0f6733d8
WD
2877+ struct group *gr;
2878+ char tagbuf[12];
2879+ char idbuf[12];
2880+ char *tag;
2881+ char *id = "";
2882+ char perms[4];
2883+ int nbytes;
fa26e11c
WD
2884+
2885+ switch (ap->a_type) {
0f6733d8
WD
2886+ /*
2887+ * for debugging purposes it's probably more
fa26e11c 2888+ * useful to dump unknown tag types rather
0f6733d8
WD
2889+ * than just returning an error
2890+ */
fa26e11c 2891+ default:
0f6733d8 2892+ slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
fa26e11c
WD
2893+ ap->a_type);
2894+ tag = tagbuf;
0f6733d8 2895+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2896+ (long)ap->a_id);
2897+ id = idbuf;
2898+ break;
2899+
2900+ case SMB_ACL_USER:
5ca9317d 2901+ id = uidtoname(ap->a_id);
fa26e11c
WD
2902+ case SMB_ACL_USER_OBJ:
2903+ tag = "user";
2904+ break;
2905+
2906+ case SMB_ACL_GROUP:
2907+ if ((gr = getgrgid(ap->a_id)) == NULL) {
0f6733d8 2908+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2909+ (long)ap->a_id);
2910+ id = idbuf;
2911+ } else {
2912+ id = gr->gr_name;
2913+ }
2914+ case SMB_ACL_GROUP_OBJ:
2915+ tag = "group";
2916+ break;
2917+
2918+ case SMB_ACL_OTHER:
2919+ tag = "other";
2920+ break;
2921+
2922+ case SMB_ACL_MASK:
2923+ tag = "mask";
2924+ break;
2925+
2926+ }
2927+
2928+ perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2929+ perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2930+ perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2931+ perms[3] = '\0';
2932+
2933+ /* <tag> : <qualifier> : rwx \n \0 */
2934+ nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2935+
0f6733d8
WD
2936+ /*
2937+ * If this entry would overflow the buffer
fa26e11c
WD
2938+ * allocate enough additional memory for this
2939+ * entry and an estimate of another 20 bytes
0f6733d8
WD
2940+ * for each entry still to be processed
2941+ */
fa26e11c
WD
2942+ if ((len + nbytes) > maxlen) {
2943+ char *oldtext = text;
2944+
2945+ maxlen += nbytes + 20 * (acl_d->count - i);
2946+
252945ef 2947+ if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
0f6733d8 2948+ SAFE_FREE(oldtext);
fa26e11c
WD
2949+ errno = ENOMEM;
2950+ return NULL;
2951+ }
2952+ }
2953+
0f6733d8 2954+ slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
fa26e11c
WD
2955+ len += nbytes - 1;
2956+ }
2957+
2958+ if (len_p)
2959+ *len_p = len;
2960+
2961+ return text;
2962+}
2963+
0f6733d8 2964+SMB_ACL_T sys_acl_init(int count)
fa26e11c 2965+{
0f6733d8 2966+ SMB_ACL_T a;
fa26e11c
WD
2967+
2968+ if (count < 0) {
2969+ errno = EINVAL;
2970+ return NULL;
2971+ }
2972+
0f6733d8
WD
2973+ /*
2974+ * note that since the definition of the structure pointed
fa26e11c
WD
2975+ * to by the SMB_ACL_T includes the first element of the
2976+ * acl[] array, this actually allocates an ACL with room
0f6733d8
WD
2977+ * for (count+1) entries
2978+ */
53c1073a 2979+ if ((a = (SMB_ACL_T)SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
fa26e11c
WD
2980+ errno = ENOMEM;
2981+ return NULL;
2982+ }
2983+
2984+ a->size = count + 1;
2985+ a->count = 0;
2986+ a->next = -1;
2987+
2988+ return a;
2989+}
2990+
2991+
0f6733d8 2992+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 2993+{
0f6733d8
WD
2994+ SMB_ACL_T acl_d;
2995+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
2996+
2997+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2998+ errno = EINVAL;
2999+ return -1;
3000+ }
3001+
3002+ if (acl_d->count >= acl_d->size) {
3003+ errno = ENOSPC;
3004+ return -1;
3005+ }
3006+
0f6733d8
WD
3007+ entry_d = &acl_d->acl[acl_d->count++];
3008+ entry_d->a_type = 0;
3009+ entry_d->a_id = -1;
3010+ entry_d->a_perm = 0;
3011+ *entry_p = entry_d;
fa26e11c
WD
3012+
3013+ return 0;
3014+}
3015+
0f6733d8 3016+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
3017+{
3018+ switch (tag_type) {
3019+ case SMB_ACL_USER:
3020+ case SMB_ACL_USER_OBJ:
3021+ case SMB_ACL_GROUP:
3022+ case SMB_ACL_GROUP_OBJ:
3023+ case SMB_ACL_OTHER:
3024+ case SMB_ACL_MASK:
3025+ entry_d->a_type = tag_type;
3026+ break;
3027+ default:
3028+ errno = EINVAL;
3029+ return -1;
3030+ }
3031+
3032+ return 0;
3033+}
3034+
0f6733d8 3035+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
3036+{
3037+ if (entry_d->a_type != SMB_ACL_GROUP
3038+ && entry_d->a_type != SMB_ACL_USER) {
3039+ errno = EINVAL;
3040+ return -1;
3041+ }
3042+
3043+ entry_d->a_id = *((id_t *)qual_p);
3044+
3045+ return 0;
3046+}
3047+
0f6733d8 3048+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
3049+{
3050+ if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3051+ return EINVAL;
3052+ }
3053+
3054+ entry_d->a_perm = *permset_d;
3055+
3056+ return 0;
3057+}
3058+
0f6733d8
WD
3059+/*
3060+ * sort the ACL and check it for validity
fa26e11c 3061+ *
0f6733d8 3062+ * if it's a minimal ACL with only 4 entries then we
fa26e11c
WD
3063+ * need to recalculate the mask permissions to make
3064+ * sure that they are the same as the GROUP_OBJ
3065+ * permissions as required by the UnixWare acl() system call.
3066+ *
0f6733d8 3067+ * (note: since POSIX allows minimal ACLs which only contain
fa26e11c
WD
3068+ * 3 entries - ie there is no mask entry - we should, in theory,
3069+ * check for this and add a mask entry if necessary - however
3070+ * we "know" that the caller of this interface always specifies
3071+ * a mask so, in practice "this never happens" (tm) - if it *does*
3072+ * happen aclsort() will fail and return an error and someone will
0f6733d8
WD
3073+ * have to fix it ...)
3074+ */
fa26e11c
WD
3075+
3076+static int acl_sort(SMB_ACL_T acl_d)
3077+{
3078+ int fixmask = (acl_d->count <= 4);
3079+
3080+ if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
3081+ errno = EINVAL;
3082+ return -1;
3083+ }
3084+ return 0;
3085+}
0f6733d8
WD
3086+
3087+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
3088+{
3089+ return acl_sort(acl_d);
3090+}
3091+
0f6733d8 3092+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c 3093+{
0f6733d8
WD
3094+ struct stat s;
3095+ struct acl *acl_p;
3096+ int acl_count;
3097+ struct acl *acl_buf = NULL;
3098+ int ret;
fa26e11c
WD
3099+
3100+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3101+ errno = EINVAL;
3102+ return -1;
3103+ }
3104+
3105+ if (acl_sort(acl_d) != 0) {
3106+ return -1;
3107+ }
3108+
0f6733d8
WD
3109+ acl_p = &acl_d->acl[0];
3110+ acl_count = acl_d->count;
fa26e11c 3111+
0f6733d8
WD
3112+ /*
3113+ * if it's a directory there is extra work to do
3114+ * since the acl() system call will replace both
3115+ * the access ACLs and the default ACLs (if any)
3116+ */
fa26e11c
WD
3117+ if (stat(name, &s) != 0) {
3118+ return -1;
3119+ }
3120+ if (S_ISDIR(s.st_mode)) {
0f6733d8
WD
3121+ SMB_ACL_T acc_acl;
3122+ SMB_ACL_T def_acl;
3123+ SMB_ACL_T tmp_acl;
3124+ int i;
fa26e11c
WD
3125+
3126+ if (type == SMB_ACL_TYPE_ACCESS) {
3127+ acc_acl = acl_d;
3128+ def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3129+
3130+ } else {
3131+ def_acl = acl_d;
3132+ acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3133+ }
3134+
3135+ if (tmp_acl == NULL) {
3136+ return -1;
3137+ }
3138+
0f6733d8
WD
3139+ /*
3140+ * allocate a temporary buffer for the complete ACL
3141+ */
fa26e11c 3142+ acl_count = acc_acl->count + def_acl->count;
252945ef 3143+ acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
fa26e11c
WD
3144+
3145+ if (acl_buf == NULL) {
3146+ sys_acl_free_acl(tmp_acl);
3147+ errno = ENOMEM;
3148+ return -1;
3149+ }
3150+
0f6733d8
WD
3151+ /*
3152+ * copy the access control and default entries into the buffer
3153+ */
fa26e11c 3154+ memcpy(&acl_buf[0], &acc_acl->acl[0],
0f6733d8 3155+ acc_acl->count * sizeof(acl_buf[0]));
fa26e11c
WD
3156+
3157+ memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
0f6733d8 3158+ def_acl->count * sizeof(acl_buf[0]));
fa26e11c 3159+
0f6733d8
WD
3160+ /*
3161+ * set the ACL_DEFAULT flag on the default entries
3162+ */
fa26e11c
WD
3163+ for (i = acc_acl->count; i < acl_count; i++) {
3164+ acl_buf[i].a_type |= ACL_DEFAULT;
3165+ }
3166+
3167+ sys_acl_free_acl(tmp_acl);
3168+
3169+ } else if (type != SMB_ACL_TYPE_ACCESS) {
3170+ errno = EINVAL;
3171+ return -1;
3172+ }
3173+
3174+ ret = acl(name, SETACL, acl_count, acl_p);
3175+
0f6733d8 3176+ SAFE_FREE(acl_buf);
fa26e11c
WD
3177+
3178+ return ret;
3179+}
3180+
0f6733d8 3181+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c
WD
3182+{
3183+ if (acl_sort(acl_d) != 0) {
3184+ return -1;
3185+ }
3186+
3187+ return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
3188+}
3189+
0f6733d8 3190+int sys_acl_delete_def_file(const char *path)
fa26e11c 3191+{
0f6733d8
WD
3192+ SMB_ACL_T acl_d;
3193+ int ret;
fa26e11c 3194+
0f6733d8
WD
3195+ /*
3196+ * fetching the access ACL and rewriting it has
3197+ * the effect of deleting the default ACL
3198+ */
fa26e11c
WD
3199+ if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3200+ return -1;
3201+ }
3202+
3203+ ret = acl(path, SETACL, acl_d->count, acl_d->acl);
3204+
3205+ sys_acl_free_acl(acl_d);
0f6733d8 3206+
fa26e11c
WD
3207+ return ret;
3208+}
3209+
0f6733d8 3210+int sys_acl_free_text(char *text)
fa26e11c 3211+{
0f6733d8 3212+ SAFE_FREE(text);
fa26e11c
WD
3213+ return 0;
3214+}
3215+
0f6733d8 3216+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c 3217+{
0f6733d8 3218+ SAFE_FREE(acl_d);
fa26e11c
WD
3219+ return 0;
3220+}
3221+
53c1073a 3222+int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
3223+{
3224+ return 0;
3225+}
3226+
3227+#elif defined(HAVE_HPUX_ACLS)
3228+#include <dl.h>
3229+
0f6733d8
WD
3230+/*
3231+ * Based on the Solaris/SCO code - with modifications.
3232+ */
fa26e11c 3233+
0f6733d8
WD
3234+/*
3235+ * Note that while this code implements sufficient functionality
fa26e11c
WD
3236+ * to support the sys_acl_* interfaces it does not provide all
3237+ * of the semantics of the POSIX ACL interfaces.
3238+ *
3239+ * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
3240+ * from a call to sys_acl_get_entry() should not be assumed to be
3241+ * valid after calling any of the following functions, which may
3242+ * reorder the entries in the ACL.
3243+ *
3244+ * sys_acl_valid()
3245+ * sys_acl_set_file()
3246+ * sys_acl_set_fd()
3247+ */
3248+
0f6733d8
WD
3249+/* This checks if the POSIX ACL system call is defined */
3250+/* which basically corresponds to whether JFS 3.3 or */
3251+/* higher is installed. If acl() was called when it */
3252+/* isn't defined, it causes the process to core dump */
3253+/* so it is important to check this and avoid acl() */
3254+/* calls if it isn't there. */
fa26e11c
WD
3255+
3256+static BOOL hpux_acl_call_presence(void)
3257+{
3258+
3259+ shl_t handle = NULL;
3260+ void *value;
3261+ int ret_val=0;
3262+ static BOOL already_checked=0;
3263+
0f6733d8 3264+ if(already_checked)
fa26e11c
WD
3265+ return True;
3266+
3267+
3268+ ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
3269+
0f6733d8 3270+ if(ret_val != 0) {
fa26e11c
WD
3271+ DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
3272+ ret_val, errno, strerror(errno)));
3273+ DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
3274+ return False;
3275+ }
3276+
3277+ DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
3278+
3279+ already_checked = True;
3280+ return True;
3281+}
3282+
0f6733d8 3283+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
3284+{
3285+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3286+ errno = EINVAL;
3287+ return -1;
3288+ }
3289+
3290+ if (entry_p == NULL) {
3291+ errno = EINVAL;
3292+ return -1;
3293+ }
3294+
3295+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
3296+ acl_d->next = 0;
3297+ }
3298+
3299+ if (acl_d->next < 0) {
3300+ errno = EINVAL;
3301+ return -1;
3302+ }
3303+
3304+ if (acl_d->next >= acl_d->count) {
3305+ return 0;
3306+ }
3307+
3308+ *entry_p = &acl_d->acl[acl_d->next++];
3309+
3310+ return 1;
3311+}
3312+
0f6733d8 3313+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
3314+{
3315+ *type_p = entry_d->a_type;
3316+
3317+ return 0;
3318+}
3319+
0f6733d8 3320+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
3321+{
3322+ *permset_p = &entry_d->a_perm;
3323+
3324+ return 0;
3325+}
3326+
0f6733d8 3327+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
3328+{
3329+ if (entry_d->a_type != SMB_ACL_USER
3330+ && entry_d->a_type != SMB_ACL_GROUP) {
3331+ errno = EINVAL;
3332+ return NULL;
3333+ }
3334+
3335+ return &entry_d->a_id;
3336+}
3337+
0f6733d8
WD
3338+/*
3339+ * There is no way of knowing what size the ACL returned by
fa26e11c
WD
3340+ * ACL_GET will be unless you first call ACL_CNT which means
3341+ * making an additional system call.
3342+ *
3343+ * In the hope of avoiding the cost of the additional system
3344+ * call in most cases, we initially allocate enough space for
3345+ * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
3346+ * be too small then we use ACL_CNT to find out the actual
3347+ * size, reallocate the ACL buffer, and then call ACL_GET again.
3348+ */
3349+
0f6733d8 3350+#define INITIAL_ACL_SIZE 16
fa26e11c 3351+
0f6733d8 3352+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 3353+{
0f6733d8
WD
3354+ SMB_ACL_T acl_d;
3355+ int count; /* # of ACL entries allocated */
3356+ int naccess; /* # of access ACL entries */
3357+ int ndefault; /* # of default ACL entries */
fa26e11c 3358+
0f6733d8
WD
3359+ if(hpux_acl_call_presence() == False) {
3360+ /* Looks like we don't have the acl() system call on HPUX.
3361+ * May be the system doesn't have the latest version of JFS.
3362+ */
3363+ return NULL;
fa26e11c
WD
3364+ }
3365+
3366+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3367+ errno = EINVAL;
3368+ return NULL;
3369+ }
3370+
3371+ count = INITIAL_ACL_SIZE;
3372+ if ((acl_d = sys_acl_init(count)) == NULL) {
3373+ return NULL;
3374+ }
3375+
0f6733d8
WD
3376+ /*
3377+ * If there isn't enough space for the ACL entries we use
fa26e11c
WD
3378+ * ACL_CNT to determine the actual number of ACL entries
3379+ * reallocate and try again. This is in a loop because it
3380+ * is possible that someone else could modify the ACL and
3381+ * increase the number of entries between the call to
0f6733d8
WD
3382+ * ACL_CNT and the call to ACL_GET.
3383+ */
fa26e11c
WD
3384+ while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
3385+
3386+ sys_acl_free_acl(acl_d);
3387+
3388+ if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
3389+ return NULL;
3390+ }
3391+
3392+ if ((acl_d = sys_acl_init(count)) == NULL) {
3393+ return NULL;
3394+ }
3395+ }
3396+
3397+ if (count < 0) {
3398+ sys_acl_free_acl(acl_d);
3399+ return NULL;
3400+ }
3401+
0f6733d8
WD
3402+ /*
3403+ * calculate the number of access and default ACL entries
fa26e11c
WD
3404+ *
3405+ * Note: we assume that the acl() system call returned a
3406+ * well formed ACL which is sorted so that all of the
0f6733d8
WD
3407+ * access ACL entries preceed any default ACL entries
3408+ */
fa26e11c
WD
3409+ for (naccess = 0; naccess < count; naccess++) {
3410+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
3411+ break;
3412+ }
3413+ ndefault = count - naccess;
0f6733d8
WD
3414+
3415+ /*
3416+ * if the caller wants the default ACL we have to copy
fa26e11c 3417+ * the entries down to the start of the acl[] buffer
0f6733d8
WD
3418+ * and mask out the ACL_DEFAULT flag from the type field
3419+ */
fa26e11c 3420+ if (type == SMB_ACL_TYPE_DEFAULT) {
0f6733d8 3421+ int i, j;
fa26e11c
WD
3422+
3423+ for (i = 0, j = naccess; i < ndefault; i++, j++) {
3424+ acl_d->acl[i] = acl_d->acl[j];
3425+ acl_d->acl[i].a_type &= ~ACL_DEFAULT;
3426+ }
3427+
3428+ acl_d->count = ndefault;
3429+ } else {
3430+ acl_d->count = naccess;
3431+ }
3432+
3433+ return acl_d;
3434+}
3435+
0f6733d8 3436+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 3437+{
0f6733d8
WD
3438+ /*
3439+ * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3440+ */
fa26e11c
WD
3441+
3442+ files_struct *fsp = file_find_fd(fd);
3443+
3444+ if (fsp == NULL) {
3445+ errno = EBADF;
3446+ return NULL;
3447+ }
3448+
0f6733d8
WD
3449+ /*
3450+ * We know we're in the same conn context. So we
3451+ * can use the relative path.
3452+ */
fa26e11c 3453+
5ca9317d 3454+ return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
fa26e11c
WD
3455+}
3456+
0f6733d8 3457+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
3458+{
3459+ *permset_d = 0;
3460+
3461+ return 0;
3462+}
3463+
0f6733d8 3464+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
3465+{
3466+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3467+ && perm != SMB_ACL_EXECUTE) {
3468+ errno = EINVAL;
3469+ return -1;
3470+ }
3471+
3472+ if (permset_d == NULL) {
3473+ errno = EINVAL;
3474+ return -1;
3475+ }
3476+
3477+ *permset_d |= perm;
3478+
3479+ return 0;
3480+}
3481+
0f6733d8 3482+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
3483+{
3484+ return *permset_d & perm;
3485+}
3486+
0f6733d8 3487+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c 3488+{
0f6733d8
WD
3489+ int i;
3490+ int len, maxlen;
3491+ char *text;
fa26e11c 3492+
0f6733d8
WD
3493+ /*
3494+ * use an initial estimate of 20 bytes per ACL entry
3495+ * when allocating memory for the text representation
3496+ * of the ACL
3497+ */
3498+ len = 0;
3499+ maxlen = 20 * acl_d->count;
252945ef 3500+ if ((text = SMB_MALLOC(maxlen)) == NULL) {
fa26e11c
WD
3501+ errno = ENOMEM;
3502+ return NULL;
3503+ }
3504+
3505+ for (i = 0; i < acl_d->count; i++) {
0f6733d8 3506+ struct acl *ap = &acl_d->acl[i];
0f6733d8
WD
3507+ struct group *gr;
3508+ char tagbuf[12];
3509+ char idbuf[12];
3510+ char *tag;
3511+ char *id = "";
3512+ char perms[4];
3513+ int nbytes;
fa26e11c
WD
3514+
3515+ switch (ap->a_type) {
0f6733d8
WD
3516+ /*
3517+ * for debugging purposes it's probably more
fa26e11c 3518+ * useful to dump unknown tag types rather
0f6733d8
WD
3519+ * than just returning an error
3520+ */
fa26e11c 3521+ default:
0f6733d8 3522+ slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
fa26e11c
WD
3523+ ap->a_type);
3524+ tag = tagbuf;
0f6733d8 3525+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
3526+ (long)ap->a_id);
3527+ id = idbuf;
3528+ break;
3529+
3530+ case SMB_ACL_USER:
5ca9317d 3531+ id = uidtoname(ap->a_id);
fa26e11c
WD
3532+ case SMB_ACL_USER_OBJ:
3533+ tag = "user";
3534+ break;
3535+
3536+ case SMB_ACL_GROUP:
3537+ if ((gr = getgrgid(ap->a_id)) == NULL) {
0f6733d8 3538+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
3539+ (long)ap->a_id);
3540+ id = idbuf;
3541+ } else {
3542+ id = gr->gr_name;
3543+ }
3544+ case SMB_ACL_GROUP_OBJ:
3545+ tag = "group";
3546+ break;
3547+
3548+ case SMB_ACL_OTHER:
3549+ tag = "other";
3550+ break;
3551+
3552+ case SMB_ACL_MASK:
3553+ tag = "mask";
3554+ break;
3555+
3556+ }
3557+
3558+ perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
3559+ perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
3560+ perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
3561+ perms[3] = '\0';
3562+
3563+ /* <tag> : <qualifier> : rwx \n \0 */
3564+ nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
3565+
0f6733d8
WD
3566+ /*
3567+ * If this entry would overflow the buffer
fa26e11c
WD
3568+ * allocate enough additional memory for this
3569+ * entry and an estimate of another 20 bytes
0f6733d8
WD
3570+ * for each entry still to be processed
3571+ */
fa26e11c
WD
3572+ if ((len + nbytes) > maxlen) {
3573+ char *oldtext = text;
3574+
3575+ maxlen += nbytes + 20 * (acl_d->count - i);
3576+
252945ef 3577+ if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
fa26e11c
WD
3578+ free(oldtext);
3579+ errno = ENOMEM;
3580+ return NULL;
3581+ }
3582+ }
3583+
0f6733d8 3584+ slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
fa26e11c
WD
3585+ len += nbytes - 1;
3586+ }
3587+
3588+ if (len_p)
3589+ *len_p = len;
3590+
3591+ return text;
3592+}
3593+
0f6733d8 3594+SMB_ACL_T sys_acl_init(int count)
fa26e11c 3595+{
0f6733d8 3596+ SMB_ACL_T a;
fa26e11c
WD
3597+
3598+ if (count < 0) {
3599+ errno = EINVAL;
3600+ return NULL;
3601+ }
3602+
0f6733d8
WD
3603+ /*
3604+ * note that since the definition of the structure pointed
fa26e11c
WD
3605+ * to by the SMB_ACL_T includes the first element of the
3606+ * acl[] array, this actually allocates an ACL with room
0f6733d8
WD
3607+ * for (count+1) entries
3608+ */
252945ef 3609+ if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
fa26e11c
WD
3610+ errno = ENOMEM;
3611+ return NULL;
3612+ }
3613+
3614+ a->size = count + 1;
3615+ a->count = 0;
3616+ a->next = -1;
3617+
3618+ return a;
3619+}
3620+
3621+
0f6733d8 3622+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 3623+{
0f6733d8
WD
3624+ SMB_ACL_T acl_d;
3625+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
3626+
3627+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3628+ errno = EINVAL;
3629+ return -1;
3630+ }
3631+
3632+ if (acl_d->count >= acl_d->size) {
3633+ errno = ENOSPC;
3634+ return -1;
3635+ }
3636+
0f6733d8
WD
3637+ entry_d = &acl_d->acl[acl_d->count++];
3638+ entry_d->a_type = 0;
3639+ entry_d->a_id = -1;
3640+ entry_d->a_perm = 0;
3641+ *entry_p = entry_d;
fa26e11c
WD
3642+
3643+ return 0;
3644+}
3645+
0f6733d8 3646+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
3647+{
3648+ switch (tag_type) {
3649+ case SMB_ACL_USER:
3650+ case SMB_ACL_USER_OBJ:
3651+ case SMB_ACL_GROUP:
3652+ case SMB_ACL_GROUP_OBJ:
3653+ case SMB_ACL_OTHER:
3654+ case SMB_ACL_MASK:
3655+ entry_d->a_type = tag_type;
3656+ break;
3657+ default:
3658+ errno = EINVAL;
3659+ return -1;
3660+ }
3661+
3662+ return 0;
3663+}
3664+
0f6733d8 3665+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
3666+{
3667+ if (entry_d->a_type != SMB_ACL_GROUP
3668+ && entry_d->a_type != SMB_ACL_USER) {
3669+ errno = EINVAL;
3670+ return -1;
3671+ }
3672+
3673+ entry_d->a_id = *((id_t *)qual_p);
3674+
3675+ return 0;
3676+}
3677+
0f6733d8 3678+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
3679+{
3680+ if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3681+ return EINVAL;
3682+ }
3683+
3684+ entry_d->a_perm = *permset_d;
3685+
3686+ return 0;
3687+}
3688+
3689+/* Structure to capture the count for each type of ACE. */
3690+
3691+struct hpux_acl_types {
3692+ int n_user;
3693+ int n_def_user;
3694+ int n_user_obj;
3695+ int n_def_user_obj;
3696+
3697+ int n_group;
3698+ int n_def_group;
3699+ int n_group_obj;
3700+ int n_def_group_obj;
3701+
3702+ int n_other;
3703+ int n_other_obj;
3704+ int n_def_other_obj;
3705+
3706+ int n_class_obj;
3707+ int n_def_class_obj;
3708+
3709+ int n_illegal_obj;
3710+};
3711+
3712+/* count_obj:
3713+ * Counts the different number of objects in a given array of ACL
3714+ * structures.
3715+ * Inputs:
3716+ *
3717+ * acl_count - Count of ACLs in the array of ACL strucutres.
3718+ * aclp - Array of ACL structures.
3719+ * acl_type_count - Pointer to acl_types structure. Should already be
3720+ * allocated.
0f6733d8 3721+ * Output:
fa26e11c 3722+ *
0f6733d8 3723+ * acl_type_count - This structure is filled up with counts of various
fa26e11c
WD
3724+ * acl types.
3725+ */
3726+
3727+static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
3728+{
3729+ int i;
3730+
0f6733d8 3731+ memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
fa26e11c 3732+
0f6733d8
WD
3733+ for(i=0;i<acl_count;i++) {
3734+ switch(aclp[i].a_type) {
3735+ case USER:
fa26e11c
WD
3736+ acl_type_count->n_user++;
3737+ break;
0f6733d8 3738+ case USER_OBJ:
fa26e11c
WD
3739+ acl_type_count->n_user_obj++;
3740+ break;
0f6733d8 3741+ case DEF_USER_OBJ:
fa26e11c
WD
3742+ acl_type_count->n_def_user_obj++;
3743+ break;
0f6733d8 3744+ case GROUP:
fa26e11c
WD
3745+ acl_type_count->n_group++;
3746+ break;
0f6733d8 3747+ case GROUP_OBJ:
fa26e11c
WD
3748+ acl_type_count->n_group_obj++;
3749+ break;
0f6733d8 3750+ case DEF_GROUP_OBJ:
fa26e11c
WD
3751+ acl_type_count->n_def_group_obj++;
3752+ break;
0f6733d8 3753+ case OTHER_OBJ:
fa26e11c
WD
3754+ acl_type_count->n_other_obj++;
3755+ break;
0f6733d8 3756+ case DEF_OTHER_OBJ:
fa26e11c
WD
3757+ acl_type_count->n_def_other_obj++;
3758+ break;
3759+ case CLASS_OBJ:
3760+ acl_type_count->n_class_obj++;
3761+ break;
3762+ case DEF_CLASS_OBJ:
3763+ acl_type_count->n_def_class_obj++;
3764+ break;
3765+ case DEF_USER:
3766+ acl_type_count->n_def_user++;
3767+ break;
3768+ case DEF_GROUP:
3769+ acl_type_count->n_def_group++;
3770+ break;
0f6733d8 3771+ default:
fa26e11c
WD
3772+ acl_type_count->n_illegal_obj++;
3773+ break;
3774+ }
3775+ }
3776+}
3777+
0f6733d8 3778+/* swap_acl_entries: Swaps two ACL entries.
fa26e11c
WD
3779+ *
3780+ * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3781+ */
3782+
3783+static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3784+{
3785+ struct acl temp_acl;
3786+
3787+ temp_acl.a_type = aclp0->a_type;
3788+ temp_acl.a_id = aclp0->a_id;
3789+ temp_acl.a_perm = aclp0->a_perm;
3790+
3791+ aclp0->a_type = aclp1->a_type;
3792+ aclp0->a_id = aclp1->a_id;
3793+ aclp0->a_perm = aclp1->a_perm;
3794+
3795+ aclp1->a_type = temp_acl.a_type;
3796+ aclp1->a_id = temp_acl.a_id;
3797+ aclp1->a_perm = temp_acl.a_perm;
3798+}
3799+
3800+/* prohibited_duplicate_type
0f6733d8 3801+ * Identifies if given ACL type can have duplicate entries or
fa26e11c
WD
3802+ * not.
3803+ *
3804+ * Inputs: acl_type - ACL Type.
3805+ *
0f6733d8 3806+ * Outputs:
fa26e11c 3807+ *
0f6733d8 3808+ * Return..
fa26e11c
WD
3809+ *
3810+ * True - If the ACL type matches any of the prohibited types.
3811+ * False - If the ACL type doesn't match any of the prohibited types.
0f6733d8 3812+ */
fa26e11c
WD
3813+
3814+static BOOL hpux_prohibited_duplicate_type(int acl_type)
3815+{
0f6733d8 3816+ switch(acl_type) {
fa26e11c
WD
3817+ case USER:
3818+ case GROUP:
0f6733d8 3819+ case DEF_USER:
fa26e11c
WD
3820+ case DEF_GROUP:
3821+ return True;
0f6733d8
WD
3822+ default:
3823+ return False;
fa26e11c 3824+ }
fa26e11c
WD
3825+}
3826+
3827+/* get_needed_class_perm
3828+ * Returns the permissions of a ACL structure only if the ACL
0f6733d8 3829+ * type matches one of the pre-determined types for computing
fa26e11c
WD
3830+ * CLASS_OBJ permissions.
3831+ *
3832+ * Inputs: aclp - Pointer to ACL structure.
3833+ */
3834+
3835+static int hpux_get_needed_class_perm(struct acl *aclp)
3836+{
0f6733d8
WD
3837+ switch(aclp->a_type) {
3838+ case USER:
3839+ case GROUP_OBJ:
3840+ case GROUP:
3841+ case DEF_USER_OBJ:
fa26e11c 3842+ case DEF_USER:
0f6733d8 3843+ case DEF_GROUP_OBJ:
fa26e11c
WD
3844+ case DEF_GROUP:
3845+ case DEF_CLASS_OBJ:
0f6733d8 3846+ case DEF_OTHER_OBJ:
fa26e11c 3847+ return aclp->a_perm;
0f6733d8 3848+ default:
fa26e11c
WD
3849+ return 0;
3850+ }
3851+}
3852+
3853+/* acl_sort for HPUX.
3854+ * Sorts the array of ACL structures as per the description in
3855+ * aclsort man page. Refer to aclsort man page for more details
3856+ *
3857+ * Inputs:
3858+ *
3859+ * acl_count - Count of ACLs in the array of ACL structures.
3860+ * calclass - If this is not zero, then we compute the CLASS_OBJ
3861+ * permissions.
3862+ * aclp - Array of ACL structures.
3863+ *
3864+ * Outputs:
3865+ *
3866+ * aclp - Sorted array of ACL structures.
3867+ *
3868+ * Outputs:
3869+ *
3870+ * Returns 0 for success -1 for failure. Prints a message to the Samba
3871+ * debug log in case of failure.
3872+ */
3873+
3874+static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3875+{
3876+#if !defined(HAVE_HPUX_ACLSORT)
0f6733d8
WD
3877+ /*
3878+ * The aclsort() system call is availabe on the latest HPUX General
3879+ * Patch Bundles. So for HPUX, we developed our version of acl_sort
3880+ * function. Because, we don't want to update to a new
3881+ * HPUX GR bundle just for aclsort() call.
3882+ */
fa26e11c
WD
3883+
3884+ struct hpux_acl_types acl_obj_count;
3885+ int n_class_obj_perm = 0;
3886+ int i, j;
0f6733d8
WD
3887+
3888+ if(!acl_count) {
fa26e11c
WD
3889+ DEBUG(10,("Zero acl count passed. Returning Success\n"));
3890+ return 0;
3891+ }
3892+
0f6733d8 3893+ if(aclp == NULL) {
fa26e11c
WD
3894+ DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3895+ return -1;
3896+ }
3897+
3898+ /* Count different types of ACLs in the ACLs array */
3899+
3900+ hpux_count_obj(acl_count, aclp, &acl_obj_count);
3901+
0f6733d8
WD
3902+ /* There should be only one entry each of type USER_OBJ, GROUP_OBJ,
3903+ * CLASS_OBJ and OTHER_OBJ
fa26e11c
WD
3904+ */
3905+
0f6733d8
WD
3906+ if( (acl_obj_count.n_user_obj != 1) ||
3907+ (acl_obj_count.n_group_obj != 1) ||
3908+ (acl_obj_count.n_class_obj != 1) ||
3909+ (acl_obj_count.n_other_obj != 1)
3910+ ) {
fa26e11c
WD
3911+ DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3912+USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3913+ return -1;
3914+ }
3915+
3916+ /* If any of the default objects are present, there should be only
3917+ * one of them each.
3918+ */
3919+
0f6733d8
WD
3920+ if( (acl_obj_count.n_def_user_obj > 1) || (acl_obj_count.n_def_group_obj > 1) ||
3921+ (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
fa26e11c
WD
3922+ DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3923+or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3924+ return -1;
3925+ }
3926+
0f6733d8
WD
3927+ /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl
3928+ * structures.
fa26e11c
WD
3929+ *
3930+ * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3931+ * same ACL type, sort by ACL id.
3932+ *
0f6733d8 3933+ * I am using the trival kind of sorting method here because, performance isn't
fa26e11c 3934+ * really effected by the ACLs feature. More over there aren't going to be more
0f6733d8 3935+ * than 17 entries on HPUX.
fa26e11c
WD
3936+ */
3937+
0f6733d8 3938+ for(i=0; i<acl_count;i++) {
fa26e11c 3939+ for (j=i+1; j<acl_count; j++) {
0f6733d8 3940+ if( aclp[i].a_type > aclp[j].a_type ) {
fa26e11c
WD
3941+ /* ACL entries out of order, swap them */
3942+
3943+ hpux_swap_acl_entries((aclp+i), (aclp+j));
3944+
0f6733d8 3945+ } else if ( aclp[i].a_type == aclp[j].a_type ) {
fa26e11c
WD
3946+
3947+ /* ACL entries of same type, sort by id */
3948+
0f6733d8 3949+ if(aclp[i].a_id > aclp[j].a_id) {
fa26e11c
WD
3950+ hpux_swap_acl_entries((aclp+i), (aclp+j));
3951+ } else if (aclp[i].a_id == aclp[j].a_id) {
3952+ /* We have a duplicate entry. */
0f6733d8 3953+ if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
fa26e11c
WD
3954+ DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3955+ aclp[i].a_type, aclp[i].a_id));
3956+ return -1;
3957+ }
3958+ }
3959+
3960+ }
3961+ }
3962+ }
3963+
3964+ /* set the class obj permissions to the computed one. */
0f6733d8 3965+ if(calclass) {
fa26e11c
WD
3966+ int n_class_obj_index = -1;
3967+
0f6733d8 3968+ for(i=0;i<acl_count;i++) {
fa26e11c
WD
3969+ n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3970+
0f6733d8 3971+ if(aclp[i].a_type == CLASS_OBJ)
fa26e11c
WD
3972+ n_class_obj_index = i;
3973+ }
3974+ aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3975+ }
3976+
3977+ return 0;
3978+#else
3979+ return aclsort(acl_count, calclass, aclp);
3980+#endif
3981+}
3982+
0f6733d8
WD
3983+/*
3984+ * sort the ACL and check it for validity
fa26e11c 3985+ *
0f6733d8 3986+ * if it's a minimal ACL with only 4 entries then we
fa26e11c
WD
3987+ * need to recalculate the mask permissions to make
3988+ * sure that they are the same as the GROUP_OBJ
3989+ * permissions as required by the UnixWare acl() system call.
3990+ *
0f6733d8 3991+ * (note: since POSIX allows minimal ACLs which only contain
fa26e11c
WD
3992+ * 3 entries - ie there is no mask entry - we should, in theory,
3993+ * check for this and add a mask entry if necessary - however
3994+ * we "know" that the caller of this interface always specifies
3995+ * a mask so, in practice "this never happens" (tm) - if it *does*
3996+ * happen aclsort() will fail and return an error and someone will
0f6733d8
WD
3997+ * have to fix it ...)
3998+ */
fa26e11c
WD
3999+
4000+static int acl_sort(SMB_ACL_T acl_d)
4001+{
4002+ int fixmask = (acl_d->count <= 4);
4003+
4004+ if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
4005+ errno = EINVAL;
4006+ return -1;
4007+ }
4008+ return 0;
4009+}
0f6733d8
WD
4010+
4011+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
4012+{
4013+ return acl_sort(acl_d);
4014+}
4015+
0f6733d8 4016+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c 4017+{
0f6733d8
WD
4018+ struct stat s;
4019+ struct acl *acl_p;
4020+ int acl_count;
4021+ struct acl *acl_buf = NULL;
4022+ int ret;
fa26e11c 4023+
0f6733d8
WD
4024+ if(hpux_acl_call_presence() == False) {
4025+ /* Looks like we don't have the acl() system call on HPUX.
4026+ * May be the system doesn't have the latest version of JFS.
4027+ */
4028+ errno=ENOSYS;
4029+ return -1;
fa26e11c
WD
4030+ }
4031+
4032+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
4033+ errno = EINVAL;
4034+ return -1;
4035+ }
4036+
4037+ if (acl_sort(acl_d) != 0) {
4038+ return -1;
4039+ }
4040+
0f6733d8
WD
4041+ acl_p = &acl_d->acl[0];
4042+ acl_count = acl_d->count;
fa26e11c 4043+
0f6733d8
WD
4044+ /*
4045+ * if it's a directory there is extra work to do
4046+ * since the acl() system call will replace both
4047+ * the access ACLs and the default ACLs (if any)
4048+ */
fa26e11c
WD
4049+ if (stat(name, &s) != 0) {
4050+ return -1;
4051+ }
4052+ if (S_ISDIR(s.st_mode)) {
0f6733d8
WD
4053+ SMB_ACL_T acc_acl;
4054+ SMB_ACL_T def_acl;
4055+ SMB_ACL_T tmp_acl;
4056+ int i;
fa26e11c
WD
4057+
4058+ if (type == SMB_ACL_TYPE_ACCESS) {
4059+ acc_acl = acl_d;
4060+ def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
4061+
4062+ } else {
4063+ def_acl = acl_d;
4064+ acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
4065+ }
4066+
4067+ if (tmp_acl == NULL) {
4068+ return -1;
4069+ }
4070+
0f6733d8
WD
4071+ /*
4072+ * allocate a temporary buffer for the complete ACL
4073+ */
fa26e11c 4074+ acl_count = acc_acl->count + def_acl->count;
252945ef 4075+ acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
fa26e11c
WD
4076+
4077+ if (acl_buf == NULL) {
4078+ sys_acl_free_acl(tmp_acl);
4079+ errno = ENOMEM;
4080+ return -1;
4081+ }
4082+
0f6733d8
WD
4083+ /*
4084+ * copy the access control and default entries into the buffer
4085+ */
fa26e11c 4086+ memcpy(&acl_buf[0], &acc_acl->acl[0],
0f6733d8 4087+ acc_acl->count * sizeof(acl_buf[0]));
fa26e11c
WD
4088+
4089+ memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
0f6733d8 4090+ def_acl->count * sizeof(acl_buf[0]));
fa26e11c 4091+
0f6733d8
WD
4092+ /*
4093+ * set the ACL_DEFAULT flag on the default entries
4094+ */
fa26e11c
WD
4095+ for (i = acc_acl->count; i < acl_count; i++) {
4096+ acl_buf[i].a_type |= ACL_DEFAULT;
4097+ }
4098+
4099+ sys_acl_free_acl(tmp_acl);
4100+
4101+ } else if (type != SMB_ACL_TYPE_ACCESS) {
4102+ errno = EINVAL;
4103+ return -1;
4104+ }
4105+
4106+ ret = acl(name, ACL_SET, acl_count, acl_p);
4107+
0f6733d8
WD
4108+ if (acl_buf) {
4109+ free(acl_buf);
4110+ }
fa26e11c
WD
4111+
4112+ return ret;
4113+}
4114+
0f6733d8 4115+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c 4116+{
0f6733d8
WD
4117+ /*
4118+ * HPUX doesn't have the facl call. Fake it using the path.... JRA.
4119+ */
fa26e11c
WD
4120+
4121+ files_struct *fsp = file_find_fd(fd);
4122+
4123+ if (fsp == NULL) {
4124+ errno = EBADF;
4125+ return NULL;
4126+ }
4127+
4128+ if (acl_sort(acl_d) != 0) {
4129+ return -1;
4130+ }
4131+
0f6733d8
WD
4132+ /*
4133+ * We know we're in the same conn context. So we
4134+ * can use the relative path.
4135+ */
fa26e11c 4136+
5ca9317d 4137+ return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
fa26e11c
WD
4138+}
4139+
0f6733d8 4140+int sys_acl_delete_def_file(const char *path)
fa26e11c 4141+{
0f6733d8
WD
4142+ SMB_ACL_T acl_d;
4143+ int ret;
fa26e11c 4144+
0f6733d8
WD
4145+ /*
4146+ * fetching the access ACL and rewriting it has
4147+ * the effect of deleting the default ACL
4148+ */
fa26e11c
WD
4149+ if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
4150+ return -1;
4151+ }
4152+
4153+ ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
4154+
4155+ sys_acl_free_acl(acl_d);
0f6733d8 4156+
fa26e11c
WD
4157+ return ret;
4158+}
4159+
0f6733d8 4160+int sys_acl_free_text(char *text)
fa26e11c
WD
4161+{
4162+ free(text);
4163+ return 0;
4164+}
4165+
0f6733d8 4166+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c
WD
4167+{
4168+ free(acl_d);
4169+ return 0;
4170+}
4171+
0f6733d8 4172+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
4173+{
4174+ return 0;
4175+}
4176+
4177+#elif defined(HAVE_IRIX_ACLS)
4178+
0f6733d8 4179+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
4180+{
4181+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
4182+ errno = EINVAL;
4183+ return -1;
4184+ }
4185+
4186+ if (entry_p == NULL) {
4187+ errno = EINVAL;
4188+ return -1;
4189+ }
4190+
4191+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
4192+ acl_d->next = 0;
4193+ }
4194+
4195+ if (acl_d->next < 0) {
4196+ errno = EINVAL;
4197+ return -1;
4198+ }
4199+
4200+ if (acl_d->next >= acl_d->aclp->acl_cnt) {
4201+ return 0;
4202+ }
4203+
4204+ *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
4205+
4206+ return 1;
4207+}
4208+
0f6733d8 4209+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
4210+{
4211+ *type_p = entry_d->ae_tag;
4212+
4213+ return 0;
4214+}
4215+
0f6733d8 4216+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
4217+{
4218+ *permset_p = entry_d;
4219+
4220+ return 0;
4221+}
4222+
0f6733d8 4223+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
4224+{
4225+ if (entry_d->ae_tag != SMB_ACL_USER
4226+ && entry_d->ae_tag != SMB_ACL_GROUP) {
4227+ errno = EINVAL;
4228+ return NULL;
4229+ }
4230+
4231+ return &entry_d->ae_id;
4232+}
4233+
0f6733d8 4234+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 4235+{
0f6733d8 4236+ SMB_ACL_T a;
fa26e11c 4237+
252945ef 4238+ if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
fa26e11c
WD
4239+ errno = ENOMEM;
4240+ return NULL;
4241+ }
4242+ if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
0f6733d8 4243+ SAFE_FREE(a);
fa26e11c
WD
4244+ return NULL;
4245+ }
4246+ a->next = -1;
4247+ a->freeaclp = True;
4248+ return a;
4249+}
4250+
0f6733d8 4251+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 4252+{
0f6733d8 4253+ SMB_ACL_T a;
fa26e11c 4254+
252945ef 4255+ if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
fa26e11c
WD
4256+ errno = ENOMEM;
4257+ return NULL;
4258+ }
4259+ if ((a->aclp = acl_get_fd(fd)) == NULL) {
0f6733d8 4260+ SAFE_FREE(a);
fa26e11c
WD
4261+ return NULL;
4262+ }
4263+ a->next = -1;
4264+ a->freeaclp = True;
4265+ return a;
4266+}
4267+
0f6733d8 4268+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
4269+{
4270+ permset_d->ae_perm = 0;
4271+
4272+ return 0;
4273+}
4274+
0f6733d8 4275+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
4276+{
4277+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
4278+ && perm != SMB_ACL_EXECUTE) {
4279+ errno = EINVAL;
4280+ return -1;
4281+ }
4282+
4283+ if (permset_d == NULL) {
4284+ errno = EINVAL;
4285+ return -1;
4286+ }
4287+
4288+ permset_d->ae_perm |= perm;
4289+
4290+ return 0;
4291+}
4292+
0f6733d8 4293+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
4294+{
4295+ return permset_d->ae_perm & perm;
4296+}
4297+
0f6733d8 4298+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c
WD
4299+{
4300+ return acl_to_text(acl_d->aclp, len_p);
4301+}
4302+
0f6733d8 4303+SMB_ACL_T sys_acl_init(int count)
fa26e11c 4304+{
0f6733d8 4305+ SMB_ACL_T a;
fa26e11c
WD
4306+
4307+ if (count < 0) {
4308+ errno = EINVAL;
4309+ return NULL;
4310+ }
4311+
252945ef 4312+ if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
fa26e11c
WD
4313+ errno = ENOMEM;
4314+ return NULL;
4315+ }
4316+
4317+ a->next = -1;
4318+ a->freeaclp = False;
0f6733d8 4319+ a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
fa26e11c
WD
4320+ a->aclp->acl_cnt = 0;
4321+
4322+ return a;
4323+}
4324+
4325+
0f6733d8 4326+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 4327+{
0f6733d8
WD
4328+ SMB_ACL_T acl_d;
4329+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
4330+
4331+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
4332+ errno = EINVAL;
4333+ return -1;
4334+ }
4335+
4336+ if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
4337+ errno = ENOSPC;
4338+ return -1;
4339+ }
4340+
0f6733d8
WD
4341+ entry_d = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
4342+ entry_d->ae_tag = 0;
4343+ entry_d->ae_id = 0;
4344+ entry_d->ae_perm = 0;
4345+ *entry_p = entry_d;
fa26e11c
WD
4346+
4347+ return 0;
4348+}
4349+
0f6733d8 4350+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
4351+{
4352+ switch (tag_type) {
4353+ case SMB_ACL_USER:
4354+ case SMB_ACL_USER_OBJ:
4355+ case SMB_ACL_GROUP:
4356+ case SMB_ACL_GROUP_OBJ:
4357+ case SMB_ACL_OTHER:
4358+ case SMB_ACL_MASK:
4359+ entry_d->ae_tag = tag_type;
4360+ break;
4361+ default:
4362+ errno = EINVAL;
4363+ return -1;
4364+ }
4365+
4366+ return 0;
4367+}
4368+
0f6733d8 4369+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
4370+{
4371+ if (entry_d->ae_tag != SMB_ACL_GROUP
4372+ && entry_d->ae_tag != SMB_ACL_USER) {
4373+ errno = EINVAL;
4374+ return -1;
4375+ }
4376+
4377+ entry_d->ae_id = *((id_t *)qual_p);
4378+
4379+ return 0;
4380+}
4381+
0f6733d8 4382+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
4383+{
4384+ if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
4385+ return EINVAL;
4386+ }
4387+
4388+ entry_d->ae_perm = permset_d->ae_perm;
4389+
4390+ return 0;
4391+}
4392+
0f6733d8 4393+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
4394+{
4395+ return acl_valid(acl_d->aclp);
4396+}
4397+
0f6733d8 4398+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c
WD
4399+{
4400+ return acl_set_file(name, type, acl_d->aclp);
4401+}
4402+
0f6733d8 4403+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c
WD
4404+{
4405+ return acl_set_fd(fd, acl_d->aclp);
4406+}
4407+
0f6733d8 4408+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
4409+{
4410+ return acl_delete_def_file(name);
4411+}
4412+
0f6733d8 4413+int sys_acl_free_text(char *text)
fa26e11c
WD
4414+{
4415+ return acl_free(text);
4416+}
4417+
0f6733d8 4418+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c
WD
4419+{
4420+ if (acl_d->freeaclp) {
4421+ acl_free(acl_d->aclp);
4422+ }
4423+ acl_free(acl_d);
4424+ return 0;
4425+}
4426+
0f6733d8 4427+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
4428+{
4429+ return 0;
4430+}
4431+
4432+#elif defined(HAVE_AIX_ACLS)
4433+
4434+/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
4435+
0f6733d8 4436+int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
4437+{
4438+ struct acl_entry_link *link;
4439+ struct new_acl_entry *entry;
4440+ int keep_going;
4441+
4442+ DEBUG(10,("This is the count: %d\n",theacl->count));
4443+
0f6733d8
WD
4444+ /* Check if count was previously set to -1. *
4445+ * If it was, that means we reached the end *
4446+ * of the acl last time. */
4447+ if(theacl->count == -1)
4448+ return(0);
fa26e11c
WD
4449+
4450+ link = theacl;
0f6733d8
WD
4451+ /* To get to the next acl, traverse linked list until index *
4452+ * of acl matches the count we are keeping. This count is *
4453+ * incremented each time we return an acl entry. */
fa26e11c 4454+
0f6733d8 4455+ for(keep_going = 0; keep_going < theacl->count; keep_going++)
fa26e11c
WD
4456+ link = link->nextp;
4457+
4458+ entry = *entry_p = link->entryp;
4459+
4460+ DEBUG(10,("*entry_p is %d\n",entry_p));
4461+ DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
4462+
4463+ /* Increment count */
4464+ theacl->count++;
0f6733d8 4465+ if(link->nextp == NULL)
fa26e11c
WD
4466+ theacl->count = -1;
4467+
0f6733d8 4468+ return(1);
fa26e11c
WD
4469+}
4470+
0f6733d8 4471+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c
WD
4472+{
4473+ /* Initialize tag type */
4474+
4475+ *tag_type_p = -1;
4476+ DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
4477+
0f6733d8
WD
4478+ /* Depending on what type of entry we have, *
4479+ * return tag type. */
4480+ switch(entry_d->ace_id->id_type) {
fa26e11c
WD
4481+ case ACEID_USER:
4482+ *tag_type_p = SMB_ACL_USER;
4483+ break;
4484+ case ACEID_GROUP:
4485+ *tag_type_p = SMB_ACL_GROUP;
4486+ break;
4487+
4488+ case SMB_ACL_USER_OBJ:
4489+ case SMB_ACL_GROUP_OBJ:
4490+ case SMB_ACL_OTHER:
4491+ *tag_type_p = entry_d->ace_id->id_type;
4492+ break;
0f6733d8 4493+
fa26e11c 4494+ default:
0f6733d8 4495+ return(-1);
fa26e11c
WD
4496+ }
4497+
0f6733d8 4498+ return(0);
fa26e11c
WD
4499+}
4500+
0f6733d8 4501+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
4502+{
4503+ DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
4504+ *permset_p = &entry_d->ace_access;
4505+ DEBUG(10,("**permset_p is %d\n",**permset_p));
0f6733d8
WD
4506+ if(!(**permset_p & S_IXUSR) &&
4507+ !(**permset_p & S_IWUSR) &&
4508+ !(**permset_p & S_IRUSR) &&
4509+ (**permset_p != 0))
4510+ return(-1);
fa26e11c
WD
4511+
4512+ DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
0f6733d8 4513+ return(0);
fa26e11c
WD
4514+}
4515+
0f6733d8 4516+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 4517+{
0f6733d8 4518+ return(entry_d->ace_id->id_data);
fa26e11c
WD
4519+}
4520+
0f6733d8 4521+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c
WD
4522+{
4523+ struct acl *file_acl = (struct acl *)NULL;
4524+ struct acl_entry *acl_entry;
4525+ struct new_acl_entry *new_acl_entry;
4526+ struct ace_id *idp;
4527+ struct acl_entry_link *acl_entry_link;
4528+ struct acl_entry_link *acl_entry_link_head;
4529+ int i;
4530+ int rc = 0;
4531+ uid_t user_id;
4532+
252945ef
WD
4533+ /* AIX has no DEFAULT */
4534+ if ( type == SMB_ACL_TYPE_DEFAULT )
4535+ return NULL;
4536+
fa26e11c 4537+ /* Get the acl using statacl */
0f6733d8 4538+
fa26e11c
WD
4539+ DEBUG(10,("Entering sys_acl_get_file\n"));
4540+ DEBUG(10,("path_p is %s\n",path_p));
4541+
252945ef 4542+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
0f6733d8
WD
4543+
4544+ if(file_acl == NULL) {
fa26e11c
WD
4545+ errno=ENOMEM;
4546+ DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
0f6733d8 4547+ return(NULL);
fa26e11c
WD
4548+ }
4549+
4550+ memset(file_acl,0,BUFSIZ);
4551+
4552+ rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
0f6733d8 4553+ if(rc == -1) {
fa26e11c 4554+ DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
0f6733d8
WD
4555+ SAFE_FREE(file_acl);
4556+ return(NULL);
fa26e11c
WD
4557+ }
4558+
4559+ DEBUG(10,("Got facl and returned it\n"));
4560+
4561+ /* Point to the first acl entry in the acl */
4562+ acl_entry = file_acl->acl_ext;
4563+
0f6733d8
WD
4564+ /* Begin setting up the head of the linked list *
4565+ * that will be used for the storing the acl *
4566+ * in a way that is useful for the posix_acls.c *
4567+ * code. */
fa26e11c
WD
4568+
4569+ acl_entry_link_head = acl_entry_link = sys_acl_init(0);
0f6733d8
WD
4570+ if(acl_entry_link_head == NULL)
4571+ return(NULL);
fa26e11c 4572+
252945ef 4573+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
4574+ if(acl_entry_link->entryp == NULL) {
4575+ SAFE_FREE(file_acl);
fa26e11c
WD
4576+ errno = ENOMEM;
4577+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 4578+ return(NULL);
fa26e11c
WD
4579+ }
4580+
4581+ DEBUG(10,("acl_entry is %d\n",acl_entry));
4582+ DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4583+
0f6733d8
WD
4584+ /* Check if the extended acl bit is on. *
4585+ * If it isn't, do not show the *
4586+ * contents of the acl since AIX intends *
4587+ * the extended info to remain unused */
fa26e11c 4588+
0f6733d8 4589+ if(file_acl->acl_mode & S_IXACL){
fa26e11c 4590+ /* while we are not pointing to the very end */
0f6733d8 4591+ while(acl_entry < acl_last(file_acl)) {
fa26e11c
WD
4592+ /* before we malloc anything, make sure this is */
4593+ /* a valid acl entry and one that we want to map */
4594+ idp = id_nxt(acl_entry->ace_id);
0f6733d8
WD
4595+ if((acl_entry->ace_type == ACC_SPECIFY ||
4596+ (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4597+ acl_entry = acl_nxt(acl_entry);
4598+ continue;
fa26e11c
WD
4599+ }
4600+
4601+ idp = acl_entry->ace_id;
4602+
0f6733d8
WD
4603+ /* Check if this is the first entry in the linked list. *
4604+ * The first entry needs to keep prevp pointing to NULL *
4605+ * and already has entryp allocated. */
fa26e11c 4606+
0f6733d8 4607+ if(acl_entry_link_head->count != 0) {
252945ef 4608+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
fa26e11c 4609+
0f6733d8
WD
4610+ if(acl_entry_link->nextp == NULL) {
4611+ SAFE_FREE(file_acl);
fa26e11c
WD
4612+ errno = ENOMEM;
4613+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 4614+ return(NULL);
fa26e11c
WD
4615+ }
4616+
4617+ acl_entry_link->nextp->prevp = acl_entry_link;
4618+ acl_entry_link = acl_entry_link->nextp;
252945ef 4619+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
4620+ if(acl_entry_link->entryp == NULL) {
4621+ SAFE_FREE(file_acl);
fa26e11c
WD
4622+ errno = ENOMEM;
4623+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 4624+ return(NULL);
fa26e11c
WD
4625+ }
4626+ acl_entry_link->nextp = NULL;
4627+ }
4628+
4629+ acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4630+
0f6733d8
WD
4631+ /* Don't really need this since all types are going *
4632+ * to be specified but, it's better than leaving it 0 */
fa26e11c
WD
4633+
4634+ acl_entry_link->entryp->ace_type = acl_entry->ace_type;
0f6733d8 4635+
fa26e11c 4636+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
0f6733d8
WD
4637+
4638+ memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
fa26e11c 4639+
0f6733d8
WD
4640+ /* The access in the acl entries must be left shifted by *
4641+ * three bites, because they will ultimately be compared *
4642+ * to S_IRUSR, S_IWUSR, and S_IXUSR. */
fa26e11c 4643+
0f6733d8 4644+ switch(acl_entry->ace_type){
fa26e11c
WD
4645+ case ACC_PERMIT:
4646+ case ACC_SPECIFY:
4647+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4648+ acl_entry_link->entryp->ace_access <<= 6;
4649+ acl_entry_link_head->count++;
4650+ break;
4651+ case ACC_DENY:
0f6733d8
WD
4652+ /* Since there is no way to return a DENY acl entry *
4653+ * change to PERMIT and then shift. */
fa26e11c
WD
4654+ DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4655+ acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4656+ DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4657+ acl_entry_link->entryp->ace_access <<= 6;
4658+ acl_entry_link_head->count++;
4659+ break;
4660+ default:
0f6733d8 4661+ return(0);
fa26e11c
WD
4662+ }
4663+
4664+ DEBUG(10,("acl_entry = %d\n",acl_entry));
4665+ DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
0f6733d8 4666+
fa26e11c
WD
4667+ acl_entry = acl_nxt(acl_entry);
4668+ }
4669+ } /* end of if enabled */
4670+
0f6733d8
WD
4671+ /* Since owner, group, other acl entries are not *
4672+ * part of the acl entries in an acl, they must *
4673+ * be dummied up to become part of the list. */
fa26e11c 4674+
0f6733d8 4675+ for( i = 1; i < 4; i++) {
fa26e11c 4676+ DEBUG(10,("i is %d\n",i));
0f6733d8 4677+ if(acl_entry_link_head->count != 0) {
252945ef 4678+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8
WD
4679+ if(acl_entry_link->nextp == NULL) {
4680+ SAFE_FREE(file_acl);
fa26e11c
WD
4681+ errno = ENOMEM;
4682+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 4683+ return(NULL);
fa26e11c
WD
4684+ }
4685+
4686+ acl_entry_link->nextp->prevp = acl_entry_link;
4687+ acl_entry_link = acl_entry_link->nextp;
252945ef 4688+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
4689+ if(acl_entry_link->entryp == NULL) {
4690+ SAFE_FREE(file_acl);
fa26e11c
WD
4691+ errno = ENOMEM;
4692+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 4693+ return(NULL);
fa26e11c
WD
4694+ }
4695+ }
4696+
4697+ acl_entry_link->nextp = NULL;
4698+
4699+ new_acl_entry = acl_entry_link->entryp;
4700+ idp = new_acl_entry->ace_id;
4701+
0f6733d8 4702+ new_acl_entry->ace_len = sizeof(struct acl_entry);
fa26e11c 4703+ new_acl_entry->ace_type = ACC_PERMIT;
0f6733d8 4704+ idp->id_len = sizeof(struct ace_id);
fa26e11c 4705+ DEBUG(10,("idp->id_len = %d\n",idp->id_len));
0f6733d8 4706+ memset(idp->id_data,0,sizeof(uid_t));
fa26e11c 4707+
0f6733d8 4708+ switch(i) {
fa26e11c
WD
4709+ case 2:
4710+ new_acl_entry->ace_access = file_acl->g_access << 6;
4711+ idp->id_type = SMB_ACL_GROUP_OBJ;
4712+ break;
4713+
4714+ case 3:
4715+ new_acl_entry->ace_access = file_acl->o_access << 6;
4716+ idp->id_type = SMB_ACL_OTHER;
4717+ break;
0f6733d8 4718+
fa26e11c
WD
4719+ case 1:
4720+ new_acl_entry->ace_access = file_acl->u_access << 6;
4721+ idp->id_type = SMB_ACL_USER_OBJ;
4722+ break;
0f6733d8 4723+
fa26e11c 4724+ default:
0f6733d8 4725+ return(NULL);
fa26e11c
WD
4726+
4727+ }
4728+
4729+ acl_entry_link_head->count++;
4730+ DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4731+ }
4732+
4733+ acl_entry_link_head->count = 0;
0f6733d8 4734+ SAFE_FREE(file_acl);
fa26e11c 4735+
0f6733d8 4736+ return(acl_entry_link_head);
fa26e11c
WD
4737+}
4738+
0f6733d8 4739+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c
WD
4740+{
4741+ struct acl *file_acl = (struct acl *)NULL;
4742+ struct acl_entry *acl_entry;
4743+ struct new_acl_entry *new_acl_entry;
4744+ struct ace_id *idp;
4745+ struct acl_entry_link *acl_entry_link;
4746+ struct acl_entry_link *acl_entry_link_head;
4747+ int i;
4748+ int rc = 0;
4749+ uid_t user_id;
4750+
4751+ /* Get the acl using fstatacl */
0f6733d8 4752+
fa26e11c
WD
4753+ DEBUG(10,("Entering sys_acl_get_fd\n"));
4754+ DEBUG(10,("fd is %d\n",fd));
252945ef 4755+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 4756+
0f6733d8 4757+ if(file_acl == NULL) {
fa26e11c
WD
4758+ errno=ENOMEM;
4759+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8 4760+ return(NULL);
fa26e11c
WD
4761+ }
4762+
4763+ memset(file_acl,0,BUFSIZ);
4764+
4765+ rc = fstatacl(fd,0,file_acl,BUFSIZ);
0f6733d8 4766+ if(rc == -1) {
fa26e11c 4767+ DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
0f6733d8
WD
4768+ SAFE_FREE(file_acl);
4769+ return(NULL);
fa26e11c
WD
4770+ }
4771+
4772+ DEBUG(10,("Got facl and returned it\n"));
4773+
4774+ /* Point to the first acl entry in the acl */
4775+
4776+ acl_entry = file_acl->acl_ext;
0f6733d8
WD
4777+ /* Begin setting up the head of the linked list *
4778+ * that will be used for the storing the acl *
4779+ * in a way that is useful for the posix_acls.c *
4780+ * code. */
fa26e11c
WD
4781+
4782+ acl_entry_link_head = acl_entry_link = sys_acl_init(0);
0f6733d8
WD
4783+ if(acl_entry_link_head == NULL){
4784+ SAFE_FREE(file_acl);
4785+ return(NULL);
fa26e11c
WD
4786+ }
4787+
252945ef 4788+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
fa26e11c 4789+
0f6733d8 4790+ if(acl_entry_link->entryp == NULL) {
fa26e11c
WD
4791+ errno = ENOMEM;
4792+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4793+ SAFE_FREE(file_acl);
4794+ return(NULL);
fa26e11c
WD
4795+ }
4796+
4797+ DEBUG(10,("acl_entry is %d\n",acl_entry));
4798+ DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
0f6733d8
WD
4799+
4800+ /* Check if the extended acl bit is on. *
4801+ * If it isn't, do not show the *
4802+ * contents of the acl since AIX intends *
4803+ * the extended info to remain unused */
4804+
4805+ if(file_acl->acl_mode & S_IXACL){
fa26e11c 4806+ /* while we are not pointing to the very end */
0f6733d8 4807+ while(acl_entry < acl_last(file_acl)) {
fa26e11c
WD
4808+ /* before we malloc anything, make sure this is */
4809+ /* a valid acl entry and one that we want to map */
4810+
4811+ idp = id_nxt(acl_entry->ace_id);
0f6733d8
WD
4812+ if((acl_entry->ace_type == ACC_SPECIFY ||
4813+ (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4814+ acl_entry = acl_nxt(acl_entry);
4815+ continue;
fa26e11c
WD
4816+ }
4817+
4818+ idp = acl_entry->ace_id;
0f6733d8
WD
4819+
4820+ /* Check if this is the first entry in the linked list. *
4821+ * The first entry needs to keep prevp pointing to NULL *
4822+ * and already has entryp allocated. */
fa26e11c 4823+
0f6733d8 4824+ if(acl_entry_link_head->count != 0) {
252945ef 4825+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4826+ if(acl_entry_link->nextp == NULL) {
fa26e11c
WD
4827+ errno = ENOMEM;
4828+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4829+ SAFE_FREE(file_acl);
4830+ return(NULL);
fa26e11c
WD
4831+ }
4832+ acl_entry_link->nextp->prevp = acl_entry_link;
4833+ acl_entry_link = acl_entry_link->nextp;
252945ef 4834+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8 4835+ if(acl_entry_link->entryp == NULL) {
fa26e11c
WD
4836+ errno = ENOMEM;
4837+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4838+ SAFE_FREE(file_acl);
4839+ return(NULL);
fa26e11c
WD
4840+ }
4841+
4842+ acl_entry_link->nextp = NULL;
4843+ }
4844+
4845+ acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4846+
0f6733d8
WD
4847+ /* Don't really need this since all types are going *
4848+ * to be specified but, it's better than leaving it 0 */
fa26e11c
WD
4849+
4850+ acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4851+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4852+
0f6733d8 4853+ memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
fa26e11c 4854+
0f6733d8
WD
4855+ /* The access in the acl entries must be left shifted by *
4856+ * three bites, because they will ultimately be compared *
4857+ * to S_IRUSR, S_IWUSR, and S_IXUSR. */
fa26e11c 4858+
0f6733d8 4859+ switch(acl_entry->ace_type){
fa26e11c
WD
4860+ case ACC_PERMIT:
4861+ case ACC_SPECIFY:
4862+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4863+ acl_entry_link->entryp->ace_access <<= 6;
4864+ acl_entry_link_head->count++;
4865+ break;
4866+ case ACC_DENY:
0f6733d8
WD
4867+ /* Since there is no way to return a DENY acl entry *
4868+ * change to PERMIT and then shift. */
fa26e11c
WD
4869+ DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4870+ acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4871+ DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4872+ acl_entry_link->entryp->ace_access <<= 6;
4873+ acl_entry_link_head->count++;
4874+ break;
4875+ default:
0f6733d8 4876+ return(0);
fa26e11c
WD
4877+ }
4878+
4879+ DEBUG(10,("acl_entry = %d\n",acl_entry));
4880+ DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
0f6733d8 4881+
fa26e11c
WD
4882+ acl_entry = acl_nxt(acl_entry);
4883+ }
4884+ } /* end of if enabled */
4885+
0f6733d8
WD
4886+ /* Since owner, group, other acl entries are not *
4887+ * part of the acl entries in an acl, they must *
4888+ * be dummied up to become part of the list. */
fa26e11c 4889+
0f6733d8 4890+ for( i = 1; i < 4; i++) {
fa26e11c 4891+ DEBUG(10,("i is %d\n",i));
0f6733d8 4892+ if(acl_entry_link_head->count != 0){
252945ef 4893+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4894+ if(acl_entry_link->nextp == NULL) {
fa26e11c
WD
4895+ errno = ENOMEM;
4896+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4897+ SAFE_FREE(file_acl);
4898+ return(NULL);
fa26e11c
WD
4899+ }
4900+
4901+ acl_entry_link->nextp->prevp = acl_entry_link;
4902+ acl_entry_link = acl_entry_link->nextp;
252945ef 4903+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
fa26e11c 4904+
0f6733d8
WD
4905+ if(acl_entry_link->entryp == NULL) {
4906+ SAFE_FREE(file_acl);
fa26e11c
WD
4907+ errno = ENOMEM;
4908+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8 4909+ return(NULL);
fa26e11c
WD
4910+ }
4911+ }
4912+
4913+ acl_entry_link->nextp = NULL;
0f6733d8 4914+
fa26e11c
WD
4915+ new_acl_entry = acl_entry_link->entryp;
4916+ idp = new_acl_entry->ace_id;
0f6733d8
WD
4917+
4918+ new_acl_entry->ace_len = sizeof(struct acl_entry);
fa26e11c 4919+ new_acl_entry->ace_type = ACC_PERMIT;
0f6733d8 4920+ idp->id_len = sizeof(struct ace_id);
fa26e11c 4921+ DEBUG(10,("idp->id_len = %d\n",idp->id_len));
0f6733d8
WD
4922+ memset(idp->id_data,0,sizeof(uid_t));
4923+
4924+ switch(i) {
fa26e11c
WD
4925+ case 2:
4926+ new_acl_entry->ace_access = file_acl->g_access << 6;
4927+ idp->id_type = SMB_ACL_GROUP_OBJ;
4928+ break;
0f6733d8 4929+
fa26e11c
WD
4930+ case 3:
4931+ new_acl_entry->ace_access = file_acl->o_access << 6;
4932+ idp->id_type = SMB_ACL_OTHER;
4933+ break;
0f6733d8 4934+
fa26e11c
WD
4935+ case 1:
4936+ new_acl_entry->ace_access = file_acl->u_access << 6;
4937+ idp->id_type = SMB_ACL_USER_OBJ;
4938+ break;
0f6733d8 4939+
fa26e11c 4940+ default:
0f6733d8 4941+ return(NULL);
fa26e11c 4942+ }
0f6733d8 4943+
fa26e11c
WD
4944+ acl_entry_link_head->count++;
4945+ DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4946+ }
4947+
4948+ acl_entry_link_head->count = 0;
0f6733d8
WD
4949+ SAFE_FREE(file_acl);
4950+
4951+ return(acl_entry_link_head);
fa26e11c
WD
4952+}
4953+
0f6733d8 4954+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
fa26e11c
WD
4955+{
4956+ *permset = *permset & ~0777;
0f6733d8 4957+ return(0);
fa26e11c
WD
4958+}
4959+
0f6733d8 4960+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 4961+{
0f6733d8
WD
4962+ if((perm != 0) &&
4963+ (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
4964+ return(-1);
fa26e11c
WD
4965+
4966+ *permset |= perm;
4967+ DEBUG(10,("This is the permset now: %d\n",*permset));
0f6733d8 4968+ return(0);
fa26e11c
WD
4969+}
4970+
0f6733d8 4971+char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
fa26e11c 4972+{
0f6733d8 4973+ return(NULL);
fa26e11c
WD
4974+}
4975+
0f6733d8 4976+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
4977+{
4978+ struct acl_entry_link *theacl = NULL;
0f6733d8 4979+
fa26e11c
WD
4980+ DEBUG(10,("Entering sys_acl_init\n"));
4981+
252945ef 4982+ theacl = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4983+ if(theacl == NULL) {
fa26e11c
WD
4984+ errno = ENOMEM;
4985+ DEBUG(0,("Error in sys_acl_init is %d\n",errno));
0f6733d8 4986+ return(NULL);
fa26e11c
WD
4987+ }
4988+
4989+ theacl->count = 0;
4990+ theacl->nextp = NULL;
4991+ theacl->prevp = NULL;
4992+ theacl->entryp = NULL;
4993+ DEBUG(10,("Exiting sys_acl_init\n"));
0f6733d8 4994+ return(theacl);
fa26e11c
WD
4995+}
4996+
0f6733d8 4997+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
4998+{
4999+ struct acl_entry_link *theacl;
5000+ struct acl_entry_link *acl_entryp;
5001+ struct acl_entry_link *temp_entry;
5002+ int counting;
5003+
5004+ DEBUG(10,("Entering the sys_acl_create_entry\n"));
5005+
5006+ theacl = acl_entryp = *pacl;
5007+
5008+ /* Get to the end of the acl before adding entry */
5009+
0f6733d8 5010+ for(counting=0; counting < theacl->count; counting++){
fa26e11c
WD
5011+ DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
5012+ temp_entry = acl_entryp;
5013+ acl_entryp = acl_entryp->nextp;
5014+ }
5015+
0f6733d8 5016+ if(theacl->count != 0){
252945ef 5017+ temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 5018+ if(acl_entryp == NULL) {
fa26e11c
WD
5019+ errno = ENOMEM;
5020+ DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
0f6733d8 5021+ return(-1);
fa26e11c
WD
5022+ }
5023+
5024+ DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
5025+ acl_entryp->prevp = temp_entry;
5026+ DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
5027+ }
5028+
252945ef 5029+ *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8 5030+ if(*pentry == NULL) {
fa26e11c
WD
5031+ errno = ENOMEM;
5032+ DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
0f6733d8 5033+ return(-1);
fa26e11c
WD
5034+ }
5035+
0f6733d8
WD
5036+ memset(*pentry,0,sizeof(struct new_acl_entry));
5037+ acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
fa26e11c 5038+ acl_entryp->entryp->ace_type = ACC_PERMIT;
0f6733d8 5039+ acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
fa26e11c
WD
5040+ acl_entryp->nextp = NULL;
5041+ theacl->count++;
5042+ DEBUG(10,("Exiting sys_acl_create_entry\n"));
0f6733d8 5043+ return(0);
fa26e11c
WD
5044+}
5045+
0f6733d8 5046+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
5047+{
5048+ DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
5049+ entry->ace_id->id_type = tagtype;
5050+ DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
5051+ DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
5052+}
5053+
0f6733d8 5054+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
5055+{
5056+ DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
0f6733d8 5057+ memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
fa26e11c 5058+ DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
0f6733d8 5059+ return(0);
fa26e11c
WD
5060+}
5061+
0f6733d8 5062+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
5063+{
5064+ DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
0f6733d8
WD
5065+ if(!(*permset & S_IXUSR) &&
5066+ !(*permset & S_IWUSR) &&
5067+ !(*permset & S_IRUSR) &&
5068+ (*permset != 0))
5069+ return(-1);
fa26e11c
WD
5070+
5071+ entry->ace_access = *permset;
5072+ DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
5073+ DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
0f6733d8 5074+ return(0);
fa26e11c
WD
5075+}
5076+
0f6733d8 5077+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c
WD
5078+{
5079+ int user_obj = 0;
5080+ int group_obj = 0;
5081+ int other_obj = 0;
5082+ struct acl_entry_link *acl_entry;
5083+
0f6733d8 5084+ for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
fa26e11c
WD
5085+ user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
5086+ group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
5087+ other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
5088+ }
5089+
5090+ DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
0f6733d8
WD
5091+
5092+ if(user_obj != 1 || group_obj != 1 || other_obj != 1)
5093+ return(-1);
fa26e11c 5094+
0f6733d8 5095+ return(0);
fa26e11c
WD
5096+}
5097+
0f6733d8 5098+int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
fa26e11c
WD
5099+{
5100+ struct acl_entry_link *acl_entry_link = NULL;
5101+ struct acl *file_acl = NULL;
5102+ struct acl *file_acl_temp = NULL;
5103+ struct acl_entry *acl_entry = NULL;
5104+ struct ace_id *ace_id = NULL;
5105+ uint id_type;
5106+ uint ace_access;
5107+ uint user_id;
5108+ uint acl_length;
5109+ uint rc;
5110+
5111+ DEBUG(10,("Entering sys_acl_set_file\n"));
5112+ DEBUG(10,("File name is %s\n",name));
0f6733d8 5113+
fa26e11c 5114+ /* AIX has no default ACL */
0f6733d8
WD
5115+ if(acltype == SMB_ACL_TYPE_DEFAULT)
5116+ return(0);
fa26e11c
WD
5117+
5118+ acl_length = BUFSIZ;
252945ef 5119+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 5120+
0f6733d8 5121+ if(file_acl == NULL) {
fa26e11c
WD
5122+ errno = ENOMEM;
5123+ DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
0f6733d8 5124+ return(-1);
fa26e11c
WD
5125+ }
5126+
5127+ memset(file_acl,0,BUFSIZ);
5128+
5129+ file_acl->acl_len = ACL_SIZ;
5130+ file_acl->acl_mode = S_IXACL;
5131+
0f6733d8 5132+ for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
fa26e11c
WD
5133+ acl_entry_link->entryp->ace_access >>= 6;
5134+ id_type = acl_entry_link->entryp->ace_id->id_type;
5135+
0f6733d8 5136+ switch(id_type) {
fa26e11c
WD
5137+ case SMB_ACL_USER_OBJ:
5138+ file_acl->u_access = acl_entry_link->entryp->ace_access;
5139+ continue;
5140+ case SMB_ACL_GROUP_OBJ:
5141+ file_acl->g_access = acl_entry_link->entryp->ace_access;
5142+ continue;
5143+ case SMB_ACL_OTHER:
5144+ file_acl->o_access = acl_entry_link->entryp->ace_access;
5145+ continue;
5146+ case SMB_ACL_MASK:
5147+ continue;
5148+ }
5149+
0f6733d8
WD
5150+ if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
5151+ acl_length += sizeof(struct acl_entry);
252945ef 5152+ file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
0f6733d8
WD
5153+ if(file_acl_temp == NULL) {
5154+ SAFE_FREE(file_acl);
fa26e11c
WD
5155+ errno = ENOMEM;
5156+ DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
0f6733d8
WD
5157+ return(-1);
5158+ }
fa26e11c
WD
5159+
5160+ memcpy(file_acl_temp,file_acl,file_acl->acl_len);
0f6733d8 5161+ SAFE_FREE(file_acl);
fa26e11c
WD
5162+ file_acl = file_acl_temp;
5163+ }
5164+
5165+ acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
0f6733d8 5166+ file_acl->acl_len += sizeof(struct acl_entry);
fa26e11c
WD
5167+ acl_entry->ace_len = acl_entry_link->entryp->ace_len;
5168+ acl_entry->ace_access = acl_entry_link->entryp->ace_access;
0f6733d8 5169+
fa26e11c 5170+ /* In order to use this, we'll need to wait until we can get denies */
0f6733d8
WD
5171+ /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
5172+ acl_entry->ace_type = ACC_SPECIFY; */
fa26e11c
WD
5173+
5174+ acl_entry->ace_type = ACC_SPECIFY;
0f6733d8 5175+
fa26e11c 5176+ ace_id = acl_entry->ace_id;
0f6733d8 5177+
fa26e11c
WD
5178+ ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
5179+ DEBUG(10,("The id type is %d\n",ace_id->id_type));
5180+ ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
0f6733d8
WD
5181+ memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
5182+ memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
fa26e11c
WD
5183+ }
5184+
0f6733d8 5185+ rc = chacl(name,file_acl,file_acl->acl_len);
fa26e11c
WD
5186+ DEBUG(10,("errno is %d\n",errno));
5187+ DEBUG(10,("return code is %d\n",rc));
0f6733d8 5188+ SAFE_FREE(file_acl);
fa26e11c 5189+ DEBUG(10,("Exiting the sys_acl_set_file\n"));
0f6733d8 5190+ return(rc);
fa26e11c
WD
5191+}
5192+
0f6733d8 5193+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
5194+{
5195+ struct acl_entry_link *acl_entry_link = NULL;
5196+ struct acl *file_acl = NULL;
5197+ struct acl *file_acl_temp = NULL;
5198+ struct acl_entry *acl_entry = NULL;
5199+ struct ace_id *ace_id = NULL;
5200+ uint id_type;
5201+ uint user_id;
5202+ uint acl_length;
5203+ uint rc;
0f6733d8 5204+
fa26e11c
WD
5205+ DEBUG(10,("Entering sys_acl_set_fd\n"));
5206+ acl_length = BUFSIZ;
252945ef 5207+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 5208+
0f6733d8 5209+ if(file_acl == NULL) {
fa26e11c
WD
5210+ errno = ENOMEM;
5211+ DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
0f6733d8 5212+ return(-1);
fa26e11c
WD
5213+ }
5214+
5215+ memset(file_acl,0,BUFSIZ);
0f6733d8 5216+
fa26e11c
WD
5217+ file_acl->acl_len = ACL_SIZ;
5218+ file_acl->acl_mode = S_IXACL;
5219+
0f6733d8 5220+ for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
fa26e11c
WD
5221+ acl_entry_link->entryp->ace_access >>= 6;
5222+ id_type = acl_entry_link->entryp->ace_id->id_type;
5223+ DEBUG(10,("The id_type is %d\n",id_type));
5224+
0f6733d8 5225+ switch(id_type) {
fa26e11c
WD
5226+ case SMB_ACL_USER_OBJ:
5227+ file_acl->u_access = acl_entry_link->entryp->ace_access;
5228+ continue;
5229+ case SMB_ACL_GROUP_OBJ:
5230+ file_acl->g_access = acl_entry_link->entryp->ace_access;
5231+ continue;
5232+ case SMB_ACL_OTHER:
5233+ file_acl->o_access = acl_entry_link->entryp->ace_access;
5234+ continue;
5235+ case SMB_ACL_MASK:
5236+ continue;
5237+ }
5238+
0f6733d8
WD
5239+ if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
5240+ acl_length += sizeof(struct acl_entry);
252945ef 5241+ file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
0f6733d8
WD
5242+ if(file_acl_temp == NULL) {
5243+ SAFE_FREE(file_acl);
fa26e11c
WD
5244+ errno = ENOMEM;
5245+ DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
0f6733d8 5246+ return(-1);
fa26e11c
WD
5247+ }
5248+
5249+ memcpy(file_acl_temp,file_acl,file_acl->acl_len);
0f6733d8 5250+ SAFE_FREE(file_acl);
fa26e11c
WD
5251+ file_acl = file_acl_temp;
5252+ }
5253+
5254+ acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
0f6733d8 5255+ file_acl->acl_len += sizeof(struct acl_entry);
fa26e11c
WD
5256+ acl_entry->ace_len = acl_entry_link->entryp->ace_len;
5257+ acl_entry->ace_access = acl_entry_link->entryp->ace_access;
0f6733d8 5258+
fa26e11c 5259+ /* In order to use this, we'll need to wait until we can get denies */
0f6733d8 5260+ /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
fa26e11c 5261+ acl_entry->ace_type = ACC_SPECIFY; */
0f6733d8 5262+
fa26e11c 5263+ acl_entry->ace_type = ACC_SPECIFY;
0f6733d8 5264+
fa26e11c 5265+ ace_id = acl_entry->ace_id;
0f6733d8 5266+
fa26e11c
WD
5267+ ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
5268+ DEBUG(10,("The id type is %d\n",ace_id->id_type));
5269+ ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
0f6733d8
WD
5270+ memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
5271+ memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
fa26e11c 5272+ }
0f6733d8 5273+
fa26e11c
WD
5274+ rc = fchacl(fd,file_acl,file_acl->acl_len);
5275+ DEBUG(10,("errno is %d\n",errno));
5276+ DEBUG(10,("return code is %d\n",rc));
0f6733d8 5277+ SAFE_FREE(file_acl);
fa26e11c 5278+ DEBUG(10,("Exiting sys_acl_set_fd\n"));
0f6733d8 5279+ return(rc);
fa26e11c
WD
5280+}
5281+
0f6733d8 5282+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
5283+{
5284+ /* AIX has no default ACL */
5285+ return 0;
5286+}
5287+
0f6733d8 5288+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 5289+{
0f6733d8 5290+ return(*permset & perm);
fa26e11c
WD
5291+}
5292+
0f6733d8 5293+int sys_acl_free_text(char *text)
fa26e11c 5294+{
0f6733d8 5295+ return(0);
fa26e11c
WD
5296+}
5297+
0f6733d8 5298+int sys_acl_free_acl(SMB_ACL_T posix_acl)
fa26e11c
WD
5299+{
5300+ struct acl_entry_link *acl_entry_link;
5301+
0f6733d8
WD
5302+ for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
5303+ SAFE_FREE(acl_entry_link->prevp->entryp);
5304+ SAFE_FREE(acl_entry_link->prevp);
fa26e11c
WD
5305+ }
5306+
0f6733d8
WD
5307+ SAFE_FREE(acl_entry_link->prevp->entryp);
5308+ SAFE_FREE(acl_entry_link->prevp);
5309+ SAFE_FREE(acl_entry_link->entryp);
5310+ SAFE_FREE(acl_entry_link);
5311+
5312+ return(0);
fa26e11c
WD
5313+}
5314+
0f6733d8 5315+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c 5316+{
0f6733d8 5317+ return(0);
fa26e11c
WD
5318+}
5319+
5320+#else /* No ACLs. */
5321+
4df546eb 5322+int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
fa26e11c
WD
5323+{
5324+ errno = ENOSYS;
5325+ return -1;
5326+}
5327+
4df546eb 5328+int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
fa26e11c
WD
5329+{
5330+ errno = ENOSYS;
5331+ return -1;
5332+}
5333+
4df546eb 5334+int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
fa26e11c
WD
5335+{
5336+ errno = ENOSYS;
5337+ return -1;
5338+}
5339+
4df546eb 5340+void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
fa26e11c
WD
5341+{
5342+ errno = ENOSYS;
5343+ return NULL;
5344+}
5345+
4df546eb 5346+SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
fa26e11c
WD
5347+{
5348+ errno = ENOSYS;
5ca9317d 5349+ return (SMB_ACL_T)NULL;
fa26e11c
WD
5350+}
5351+
4df546eb 5352+SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
fa26e11c
WD
5353+{
5354+ errno = ENOSYS;
5ca9317d 5355+ return (SMB_ACL_T)NULL;
fa26e11c
WD
5356+}
5357+
4df546eb 5358+int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
fa26e11c
WD
5359+{
5360+ errno = ENOSYS;
5361+ return -1;
5362+}
5363+
4df546eb 5364+int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
fa26e11c
WD
5365+{
5366+ errno = ENOSYS;
5367+ return -1;
5368+}
5369+
0f6733d8 5370+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
5371+{
5372+ errno = ENOSYS;
0f6733d8 5373+ return (permset & perm) ? 1 : 0;
fa26e11c
WD
5374+}
5375+
4df546eb 5376+char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
fa26e11c
WD
5377+{
5378+ errno = ENOSYS;
5379+ return NULL;
5380+}
5381+
4df546eb 5382+int sys_acl_free_text(UNUSED(char *text))
fa26e11c
WD
5383+{
5384+ errno = ENOSYS;
5385+ return -1;
5386+}
5387+
4df546eb 5388+SMB_ACL_T sys_acl_init(UNUSED(int count))
fa26e11c
WD
5389+{
5390+ errno = ENOSYS;
5391+ return NULL;
5392+}
5393+
4df546eb 5394+int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
fa26e11c
WD
5395+{
5396+ errno = ENOSYS;
5397+ return -1;
5398+}
5399+
4df546eb 5400+int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
5401+{
5402+ errno = ENOSYS;
5403+ return -1;
5404+}
5405+
4df546eb 5406+int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
fa26e11c
WD
5407+{
5408+ errno = ENOSYS;
5409+ return -1;
5410+}
5411+
4df546eb 5412+int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
fa26e11c
WD
5413+{
5414+ errno = ENOSYS;
5415+ return -1;
5416+}
5417+
4df546eb 5418+int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
5419+{
5420+ errno = ENOSYS;
5421+ return -1;
5422+}
5423+
4df546eb 5424+int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
5425+{
5426+ errno = ENOSYS;
5427+ return -1;
5428+}
5429+
4df546eb 5430+int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
5431+{
5432+ errno = ENOSYS;
5433+ return -1;
5434+}
5435+
4df546eb 5436+int sys_acl_delete_def_file(UNUSED(const char *name))
fa26e11c
WD
5437+{
5438+ errno = ENOSYS;
5439+ return -1;
5440+}
5441+
4df546eb 5442+int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
fa26e11c
WD
5443+{
5444+ errno = ENOSYS;
5445+ return -1;
5446+}
5447+
4df546eb 5448+int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
5449+{
5450+ errno = ENOSYS;
5451+ return -1;
5452+}
5453+
5454+#endif /* No ACLs. */
252945ef
WD
5455+
5456+/************************************************************************
5457+ Deliberately outside the ACL defines. Return 1 if this is a "no acls"
5458+ errno, 0 if not.
5459+************************************************************************/
5460+
5461+int no_acl_syscall_error(int err)
5462+{
5463+#if defined(ENOSYS)
5464+ if (err == ENOSYS) {
5465+ return 1;
5466+ }
5467+#endif
5468+#if defined(ENOTSUP)
5469+ if (err == ENOTSUP) {
5470+ return 1;
5471+ }
5472+#endif
5473+ return 0;
5474+}
9a7eef96
WD
5475--- old/lib/sysacls.h
5476+++ new/lib/sysacls.h
a35da1c7
WD
5477@@ -0,0 +1,33 @@
5478+#if defined SUPPORT_ACLS && defined HAVE_SYS_ACL_H
5479+#include <sys/acl.h>
5480+#endif
5481+#include "smb_acls.h"
5482+
252945ef
WD
5483+#define SMB_MALLOC(cnt) new_array(char, cnt)
5484+#define SMB_MALLOC_P(obj) new_array(obj, 1)
5485+#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
5486+#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
5ca9317d 5487+#define slprintf snprintf
0f6733d8
WD
5488+
5489+int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
5490+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
5491+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
5492+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
5493+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
5494+SMB_ACL_T sys_acl_get_fd(int fd);
5495+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
5496+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
5497+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
5498+char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
5499+SMB_ACL_T sys_acl_init(int count);
5500+int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
5501+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
5502+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
5503+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
5504+int sys_acl_valid(SMB_ACL_T theacl);
5505+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
5506+int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
5507+int sys_acl_delete_def_file(const char *name);
5508+int sys_acl_free_text(char *text);
5509+int sys_acl_free_acl(SMB_ACL_T the_acl);
5510+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
0870c22a
WD
5511--- old/log.c
5512+++ new/log.c
21158bc6 5513@@ -600,8 +600,10 @@ static void log_formatted(enum logcode c
0870c22a
WD
5514 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
5515 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
5516 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
5517- n[8] = '.';
5518- n[9] = '\0';
5519+ n[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
5520+ n[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
5521+ n[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
5522+ n[11] = '\0';
5523
5524 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
5525 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
9a7eef96
WD
5526--- old/mkproto.awk
5527+++ new/mkproto.awk
0f6733d8
WD
5528@@ -58,7 +58,7 @@ BEGIN {
5529 next;
5530 }
5531
bc5988ec
WD
5532-!/^OFF_T|^size_t|^off_t|^pid_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
5533+!/^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/ {
0f6733d8
WD
5534 next;
5535 }
5536
9a7eef96
WD
5537--- old/options.c
5538+++ new/options.c
c1471566 5539@@ -47,6 +47,7 @@ int copy_dirlinks = 0;
0f6733d8
WD
5540 int copy_links = 0;
5541 int preserve_links = 0;
5542 int preserve_hard_links = 0;
5543+int preserve_acls = 0;
5544 int preserve_perms = 0;
90fa6d68 5545 int preserve_executability = 0;
0f6733d8 5546 int preserve_devices = 0;
c1471566 5547@@ -194,6 +195,7 @@ static void print_rsync_version(enum log
0f6733d8
WD
5548 char const *got_socketpair = "no ";
5549 char const *have_inplace = "no ";
5550 char const *hardlinks = "no ";
5551+ char const *acls = "no ";
5552 char const *links = "no ";
5553 char const *ipv6 = "no ";
5554 STRUCT_STAT *dumstat;
c1471566 5555@@ -210,6 +212,10 @@ static void print_rsync_version(enum log
0f6733d8
WD
5556 hardlinks = "";
5557 #endif
5558
09fb8f03 5559+#ifdef SUPPORT_ACLS
0f6733d8
WD
5560+ acls = "";
5561+#endif
5562+
09fb8f03 5563 #ifdef SUPPORT_LINKS
0f6733d8
WD
5564 links = "";
5565 #endif
c1471566 5566@@ -223,9 +229,9 @@ static void print_rsync_version(enum log
3b05e91f 5567 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
0f6733d8
WD
5568 rprintf(f, "<http://rsync.samba.org/>\n");
5569 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
bc5988ec
WD
5570- "%shard links, %ssymlinks, batchfiles,\n",
5571+ "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
0f6733d8
WD
5572 (int) (sizeof (OFF_T) * 8),
5573- got_socketpair, hardlinks, links);
5574+ got_socketpair, hardlinks, acls, links);
5575
5576 /* Note that this field may not have type ino_t. It depends
5577 * on the complicated interaction between largefile feature
c1471566 5578@@ -295,6 +301,9 @@ void usage(enum logcode F)
2578e2b6 5579 rprintf(F," -H, --hard-links preserve hard links\n");
0f6733d8 5580 rprintf(F," -p, --perms preserve permissions\n");
90fa6d68 5581 rprintf(F," -E, --executability preserve the file's executability\n");
25d385b9 5582+#ifdef SUPPORT_ACLS
0f6733d8 5583+ rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
25d385b9 5584+#endif
90fa6d68
WD
5585 rprintf(F," --chmod=CHMOD change destination permissions\n");
5586 rprintf(F," -o, --owner preserve owner (super-user only)\n");
0f6733d8 5587 rprintf(F," -g, --group preserve group\n");
c1471566 5588@@ -410,6 +419,9 @@ static struct poptOption long_options[]
489b0a72
WD
5589 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
5590 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
90fa6d68 5591 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
489b0a72
WD
5592+ {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
5593+ {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
5594+ {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
5595 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
5596 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
5597 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
f4222aa5 5598@@ -1070,6 +1082,24 @@ int parse_arguments(int *argc, const cha
3b05e91f
WD
5599 usage(FINFO);
5600 exit_cleanup(0);
0f6733d8
WD
5601
5602+ case 'A':
4df546eb 5603+#ifdef SUPPORT_ACLS
a5e3a4cc
WD
5604+ preserve_acls++;
5605+ preserve_perms = 1;
489b0a72 5606+ break;
0f6733d8
WD
5607+#else
5608+ /* FIXME: this should probably be ignored with a
5609+ * warning and then countermeasures taken to
5610+ * restrict group and other access in the presence
5611+ * of any more restrictive ACLs, but this is safe
5612+ * for now */
5613+ snprintf(err_buf,sizeof(err_buf),
5614+ "ACLs are not supported on this %s\n",
5615+ am_server ? "server" : "client");
5616+ return 0;
25d385b9 5617+#endif
0f6733d8
WD
5618+
5619+
5620 default:
5621 /* A large opt value means that set_refuse_options()
27a7053c 5622 * turned this option off. */
f4222aa5 5623@@ -1504,6 +1534,10 @@ void server_options(char **args,int *arg
c7bc7375 5624
0f6733d8
WD
5625 if (preserve_hard_links)
5626 argstr[x++] = 'H';
25d385b9 5627+#ifdef SUPPORT_ACLS
0f6733d8
WD
5628+ if (preserve_acls)
5629+ argstr[x++] = 'A';
25d385b9 5630+#endif
0f6733d8
WD
5631 if (preserve_uid)
5632 argstr[x++] = 'o';
5633 if (preserve_gid)
9a7eef96
WD
5634--- old/receiver.c
5635+++ new/receiver.c
c1471566 5636@@ -48,6 +48,7 @@ extern int keep_partial;
1a2fa68f
WD
5637 extern int checksum_seed;
5638 extern int inplace;
5639 extern int delay_updates;
5640+extern mode_t orig_umask;
5641 extern struct stats stats;
5642 extern char *log_format;
5643 extern char *tmpdir;
c1471566 5644@@ -346,6 +347,10 @@ int recv_files(int f_in, struct file_lis
26c810d8
WD
5645 int itemizing = am_daemon ? daemon_log_format_has_i
5646 : !am_server && log_format_has_i;
5647 int max_phase = protocol_version >= 29 ? 2 : 1;
5648+ int dflt_perms = (ACCESSPERMS & ~orig_umask);
5c8b4e6e 5649+#ifdef SUPPORT_ACLS
26c810d8 5650+ char *parent_dirname = "";
5c8b4e6e 5651+#endif
26c810d8
WD
5652 int i, recv_ok;
5653
5654 if (verbose > 2)
c1471566 5655@@ -543,7 +548,16 @@ int recv_files(int f_in, struct file_lis
90fa6d68
WD
5656 * mode based on the local permissions and some heuristics. */
5657 if (!preserve_perms) {
5658 int exists = fd1 != -1;
5659- file->mode = dest_mode(file->mode, st.st_mode, exists);
26c810d8 5660+#ifdef SUPPORT_ACLS
5c8b4e6e
WD
5661+ char *dn = file->dirname ? file->dirname : ".";
5662+ if (parent_dirname != dn
5663+ && strcmp(parent_dirname, dn) != 0) {
5664+ dflt_perms = default_perms_for_dir(dn);
5665+ parent_dirname = dn;
26c810d8
WD
5666+ }
5667+#endif
5668+ file->mode = dest_mode(file->mode, st.st_mode,
5669+ dflt_perms, exists);
90fa6d68
WD
5670 }
5671
5672 /* We now check to see if we are writing file "inplace" */
9a7eef96
WD
5673--- old/rsync.c
5674+++ new/rsync.c
162234a7
WD
5675@@ -33,6 +33,7 @@
5676 extern int verbose;
5677 extern int dry_run;
5678 extern int daemon_log_format_has_i;
5679+extern int preserve_acls;
5680 extern int preserve_perms;
5681 extern int preserve_executability;
5682 extern int preserve_times;
5683@@ -101,7 +102,8 @@ void free_sums(struct sum_struct *s)
90fa6d68
WD
5684
5685 /* This is only called when we aren't preserving permissions. Figure out what
5686 * the permissions should be and return them merged back into the mode. */
02929d4c
WD
5687-mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int exists)
5688+mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int dflt_perms,
26c810d8 5689+ int exists)
90fa6d68 5690 {
25d385b9 5691 /* If the file already exists, we'll return the local permissions,
90fa6d68 5692 * possibly tweaked by the --executability option. */
0870c22a 5693@@ -116,55 +118,63 @@ mode_t dest_mode(mode_t flist_mode, mode
02929d4c 5694 cur_mode |= (cur_mode & 0444) >> 2;
90fa6d68 5695 }
26c810d8 5696 } else
02929d4c 5697- cur_mode = flist_mode & ACCESSPERMS & ~orig_umask;
81ddc4dc 5698+ cur_mode = flist_mode & ACCESSPERMS & dflt_perms;
c769ea2c
WD
5699 if (daemon_chmod_modes && !S_ISLNK(flist_mode))
5700 cur_mode = tweak_mode(cur_mode, daemon_chmod_modes);
02929d4c 5701 return (flist_mode & ~CHMOD_BITS) | (cur_mode & CHMOD_BITS);
0870c22a
WD
5702 }
5703
5704-int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
5705+int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
5706 int flags)
5707 {
5708 int updated = 0;
5709- STRUCT_STAT st2;
5710+ statx sx2;
5711 int change_uid, change_gid;
5712
5713- if (!st) {
5714+ if (!sxp) {
5715 if (dry_run)
5716 return 1;
5717- if (link_stat(fname, &st2, 0) < 0) {
5718+ if (link_stat(fname, &sx2.st, 0) < 0) {
5719 rsyserr(FERROR, errno, "stat %s failed",
5720 full_fname(fname));
5721 return 0;
5722 }
5723- st = &st2;
5724+#ifdef SUPPORT_ACLS
5725+ sx2.acc_acl = sx2.def_acl = NULL;
5726+#endif
5727 if (!preserve_perms && S_ISDIR(file->mode)
5728- && st->st_mode & S_ISGID) {
5729+ && sx2.st.st_mode & S_ISGID) {
5730 /* We just created this directory and its setgid
5731 * bit is on, so make sure it stays on. */
5732 file->mode |= S_ISGID;
5733 }
5734+ sxp = &sx2;
5735 }
5736
5737- if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times))
5738+#ifdef SUPPORT_ACLS
5739+ if (preserve_acls && !ACL_READY(*sxp))
98ccc74a 5740+ get_acl(fname, sxp);
0870c22a
WD
5741+#endif
5742+
5743+ if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && omit_dir_times))
5744 flags |= ATTRS_SKIP_MTIME;
5745 if (!(flags & ATTRS_SKIP_MTIME)
5746- && cmp_time(st->st_mtime, file->modtime) != 0) {
5747- int ret = set_modtime(fname, file->modtime, st->st_mode);
5748+ && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
5749+ int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
5750 if (ret < 0) {
5751 rsyserr(FERROR, errno, "failed to set times on %s",
5752 full_fname(fname));
5753- return 0;
5754+ goto cleanup;
5755 }
5756 if (ret == 0) /* ret == 1 if symlink could not be set */
5757 updated = 1;
5758 }
5759
5760- change_uid = am_root && preserve_uid && st->st_uid != file->uid;
5761+ change_uid = am_root && preserve_uid && sxp->st.st_uid != file->uid;
5762 change_gid = preserve_gid && file->gid != GID_NONE
5763- && st->st_gid != file->gid;
5764+ && sxp->st.st_gid != file->gid;
5765 #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
5766- if (S_ISLNK(st->st_mode))
5767+ if (S_ISLNK(sxp->st.st_mode))
5768 ;
5769 else
5770 #endif
5771@@ -174,43 +184,55 @@ int set_file_attrs(char *fname, struct f
5772 rprintf(FINFO,
5773 "set uid of %s from %ld to %ld\n",
5774 fname,
5775- (long)st->st_uid, (long)file->uid);
5776+ (long)sxp->st.st_uid, (long)file->uid);
5777 }
5778 if (change_gid) {
5779 rprintf(FINFO,
5780 "set gid of %s from %ld to %ld\n",
5781 fname,
5782- (long)st->st_gid, (long)file->gid);
5783+ (long)sxp->st.st_gid, (long)file->gid);
5784 }
5785 }
5786 if (do_lchown(fname,
5787- change_uid ? file->uid : st->st_uid,
5788- change_gid ? file->gid : st->st_gid) != 0) {
5789+ change_uid ? file->uid : sxp->st.st_uid,
5790+ change_gid ? file->gid : sxp->st.st_gid) != 0) {
5791 /* shouldn't have attempted to change uid or gid
5792 * unless have the privilege */
5793 rsyserr(FERROR, errno, "%s %s failed",
5794 change_uid ? "chown" : "chgrp",
5795 full_fname(fname));
5796- return 0;
5797+ goto cleanup;
5798 }
5799 /* a lchown had been done - we have to re-stat if the
5800 * destination had the setuid or setgid bits set due
5801 * to the side effect of the chown call */
5802- if (st->st_mode & (S_ISUID | S_ISGID)) {
5803- link_stat(fname, st,
5804- keep_dirlinks && S_ISDIR(st->st_mode));
5805+ if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
5806+ link_stat(fname, &sxp->st,
5807+ keep_dirlinks && S_ISDIR(sxp->st.st_mode));
5808 }
c6437996
WD
5809 updated = 1;
5810 }
34a409bc 5811
c6437996 5812+#ifdef SUPPORT_ACLS
98ccc74a 5813+ /* It's OK to call set_acl() now, even for a dir, as the generator
c6437996
WD
5814+ * will enable owner-writability using chmod, if necessary.
5815+ *
98ccc74a 5816+ * If set_acl() changes permission bits in the process of setting
0870c22a 5817+ * an access ACL, it changes sxp->st.st_mode so we know whether we
98ccc74a
WD
5818+ * need to chmod(). */
5819+ if (preserve_acls && set_acl(fname, file, sxp) == 0)
c6437996
WD
5820+ updated = 1;
5821+#endif
5822+
34a409bc 5823 #ifdef HAVE_CHMOD
0870c22a
WD
5824- if ((st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
5825- int ret = do_chmod(fname, file->mode);
5826+ if ((sxp->st.st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
5827+ mode_t mode = file->mode;
5828+ int ret = do_chmod(fname, mode);
5829 if (ret < 0) {
5830 rsyserr(FERROR, errno,
5831 "failed to set permissions on %s",
5832 full_fname(fname));
5833- return 0;
5834+ goto cleanup;
5835 }
5836 if (ret == 0) /* ret == 1 if symlink could not be set */
5837 updated = 1;
5838@@ -225,6 +247,11 @@ int set_file_attrs(char *fname, struct f
5839 else
5840 rprintf(code, "%s is uptodate\n", fname);
5841 }
5842+ cleanup:
5843+#ifdef SUPPORT_ACLS
5844+ if (preserve_acls && sxp == &sx2)
98ccc74a 5845+ free_acl(&sx2);
0870c22a
WD
5846+#endif
5847 return updated;
5848 }
5849
9a7eef96
WD
5850--- old/rsync.h
5851+++ new/rsync.h
0870c22a
WD
5852@@ -485,6 +485,15 @@ struct idev {
5853 #define IN_LOOPBACKNET 127
5854 #endif
0f6733d8 5855
4df546eb
WD
5856+#if HAVE_POSIX_ACLS|HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|\
5857+ HAVE_HPUX_ACLS|HAVE_IRIX_ACLS|HAVE_AIX_ACLS|HAVE_TRU64_ACLS
5858+#define SUPPORT_ACLS 1
5859+#endif
0f6733d8 5860+
4df546eb
WD
5861+#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
5862+#define ACLS_NEED_MASK 1
5863+#endif
0f6733d8 5864+
0870c22a
WD
5865 #define GID_NONE ((gid_t)-1)
5866
5867 #define HL_CHECK_MASTER 0
6250eb3e
WD
5868@@ -645,6 +654,17 @@ struct stats {
5869
5870 struct chmod_mode_struct;
5871
5872+#define EMPTY_ITEM_LIST {NULL, 0, 0}
5873+
5874+typedef struct {
5875+ void *items;
5876+ size_t count;
5877+ size_t malloced;
5878+} item_list;
5879+
645e162c
WD
5880+#define EXPAND_ITEM_LIST(lp, type, incr) \
5881+ (type*)expand_item_list(lp, sizeof (type), #type, incr)
6250eb3e
WD
5882+
5883 #include "byteorder.h"
5884 #include "lib/mdfour.h"
5885 #include "lib/wildmatch.h"
a35da1c7 5886@@ -660,6 +680,16 @@ struct chmod_mode_struct;
0870c22a
WD
5887
5888 #define UNUSED(x) x __attribute__((__unused__))
5889
0870c22a
WD
5890+typedef struct {
5891+ STRUCT_STAT st;
5892+#ifdef SUPPORT_ACLS
5893+ struct rsync_acl *acc_acl; /* access ACL */
5894+ struct rsync_acl *def_acl; /* default ACL */
5895+#endif
5896+} statx;
5897+
5898+#define ACL_READY(sx) ((sx).acc_acl != NULL)
0f6733d8
WD
5899+
5900 #include "proto.h"
5901
5902 /* We have replacement versions of these if they're missing. */
9a7eef96
WD
5903--- old/rsync.yo
5904+++ new/rsync.yo
2578e2b6
WD
5905@@ -321,6 +321,7 @@ to the detailed description below for a
5906 -H, --hard-links preserve hard links
0f6733d8 5907 -p, --perms preserve permissions
90fa6d68
WD
5908 -E, --executability preserve executability
5909+ -A, --acls preserve ACLs (implies -p) [non-standard]
5910 --chmod=CHMOD change destination permissions
5911 -o, --owner preserve owner (super-user only)
0f6733d8 5912 -g, --group preserve group
2578e2b6 5913@@ -742,7 +743,9 @@ quote(itemize(
90fa6d68
WD
5914 permissions, though the bf(--executability) option might change just
5915 the execute permission for the file.
03baf100
WD
5916 it() New files get their "normal" permission bits set to the source
5917- file's permissions masked with the receiving end's umask setting, and
5918+ file's permissions masked with the receiving directory's default
5919+ permissions (either the receiving process's umask, or the permissions
5920+ specified via the destination directory's default ACL), and
5921 their special permission bits disabled except in the case where a new
5922 directory inherits a setgid bit from its parent directory.
90fa6d68 5923 ))
2578e2b6 5924@@ -773,9 +776,11 @@ The preservation of the destination's se
03baf100
WD
5925 directories when bf(--perms) is off was added in rsync 2.6.7. Older rsync
5926 versions erroneously preserved the three special permission bits for
5927 newly-created files when bf(--perms) was off, while overriding the
5928-destination's setgid bit setting on a newly-created directory. (Keep in
5929-mind that it is the version of the receiving rsync that affects this
5930-behavior.)
5931+destination's setgid bit setting on a newly-created directory. Default ACL
5932+observance was added to the ACL patch for rsync 2.6.7, so older (or
5933+non-ACL-enabled) rsyncs use the umask even if default ACLs are present.
5934+(Keep in mind that it is the version of the receiving rsync that affects
5935+these behaviors.)
90fa6d68 5936
90fa6d68
WD
5937 dit(bf(-E, --executability)) This option causes rsync to preserve the
5938 executability (or non-executability) of regular files when bf(--perms) is
b57f8e02 5939@@ -793,6 +798,15 @@ quote(itemize(
90fa6d68
WD
5940
5941 If bf(--perms) is enabled, this option is ignored.
5942
5943+dit(bf(-A, --acls)) This option causes rsync to update the destination
5944+ACLs to be the same as the source ACLs. This nonstandard option only
5945+works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
b57f8e02
WD
5946+
5947+Note also that an optimization of the ACL-sending protocol used by this
5948+version makes it incompatible with sending files to an older ACL-enabled
5949+rsync unless you double the bf(--acls) option (e.g. bf(-AA)). This
5950+doubling is not needed when pulling files from an older rsync.
90fa6d68
WD
5951+
5952 dit(bf(--chmod)) This option tells rsync to apply one or more
5953 comma-separated "chmod" strings to the permission of the files in the
5954 transfer. The resulting value is treated as though it was the permissions
0870c22a
WD
5955@@ -1372,8 +1386,8 @@ if the receiving rsync is at least versi
5956 with older versions of rsync, but that also turns on the output of other
5957 verbose messages).
5958
5959-The "%i" escape has a cryptic output that is 9 letters long. The general
5960-format is like the string bf(YXcstpogz), where bf(Y) is replaced by the
5961+The "%i" escape has a cryptic output that is 11 letters long. The general
5962+format is like the string bf(YXcstpoguax), where bf(Y) is replaced by the
5963 type of update being done, bf(X) is replaced by the file-type, and the
5964 other letters represent attributes that may be output if they are being
5965 modified.
5966@@ -1422,7 +1436,11 @@ quote(itemize(
5967 sender's value (requires bf(--owner) and super-user privileges).
5968 it() A bf(g) means the group is different and is being updated to the
5969 sender's value (requires bf(--group) and the authority to set the group).
5970- it() The bf(z) slot is reserved for future use.
5971+ it() The bf(u) slot is reserved for reporting update (access) time changes
5972+ (a feature that is not yet released).
5973+ it() The bf(a) means that the ACL information changed.
5974+ it() The bf(x) slot is reserved for reporting extended attribute changes
5975+ (a feature that is not yet released).
5976 ))
5977
5978 One other output is possible: when deleting files, the "%i" will output
9a7eef96
WD
5979--- old/smb_acls.h
5980+++ new/smb_acls.h
0870c22a
WD
5981@@ -0,0 +1,281 @@
5982+/*
5983+ Unix SMB/Netbios implementation.
5984+ Version 2.2.x
5985+ Portable SMB ACL interface
5986+ Copyright (C) Jeremy Allison 2000
5987+
5988+ This program is free software; you can redistribute it and/or modify
5989+ it under the terms of the GNU General Public License as published by
5990+ the Free Software Foundation; either version 2 of the License, or
5991+ (at your option) any later version.
5992+
5993+ This program is distributed in the hope that it will be useful,
5994+ but WITHOUT ANY WARRANTY; without even the implied warranty of
5995+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5996+ GNU General Public License for more details.
5997+
5998+ You should have received a copy of the GNU General Public License
5999+ along with this program; if not, write to the Free Software
6000+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
6001+*/
0f6733d8
WD
6002+
6003+#ifndef _SMB_ACLS_H
6004+#define _SMB_ACLS_H
6005+
e6a7303b 6006+#if defined HAVE_POSIX_ACLS
0f6733d8
WD
6007+
6008+/* This is an identity mapping (just remove the SMB_). */
6009+
6010+#define SMB_ACL_TAG_T acl_tag_t
6011+#define SMB_ACL_TYPE_T acl_type_t
6012+#define SMB_ACL_PERMSET_T acl_permset_t
6013+#define SMB_ACL_PERM_T acl_perm_t
6014+#define SMB_ACL_READ ACL_READ
6015+#define SMB_ACL_WRITE ACL_WRITE
6016+#define SMB_ACL_EXECUTE ACL_EXECUTE
6017+
6018+/* Types of ACLs. */
6019+#define SMB_ACL_USER ACL_USER
6020+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
6021+#define SMB_ACL_GROUP ACL_GROUP
6022+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
6023+#define SMB_ACL_OTHER ACL_OTHER
6024+#define SMB_ACL_MASK ACL_MASK
6025+
6026+#define SMB_ACL_T acl_t
6027+
6028+#define SMB_ACL_ENTRY_T acl_entry_t
6029+
6030+#define SMB_ACL_FIRST_ENTRY ACL_FIRST_ENTRY
6031+#define SMB_ACL_NEXT_ENTRY ACL_NEXT_ENTRY
6032+
6033+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
6034+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
6035+
e6a7303b 6036+#elif defined HAVE_TRU64_ACLS
0f6733d8
WD
6037+
6038+/* This is for DEC/Compaq Tru64 UNIX */
6039+
6040+#define SMB_ACL_TAG_T acl_tag_t
6041+#define SMB_ACL_TYPE_T acl_type_t
6042+#define SMB_ACL_PERMSET_T acl_permset_t
6043+#define SMB_ACL_PERM_T acl_perm_t
6044+#define SMB_ACL_READ ACL_READ
6045+#define SMB_ACL_WRITE ACL_WRITE
6046+#define SMB_ACL_EXECUTE ACL_EXECUTE
6047+
6048+/* Types of ACLs. */
6049+#define SMB_ACL_USER ACL_USER
6050+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
6051+#define SMB_ACL_GROUP ACL_GROUP
6052+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
6053+#define SMB_ACL_OTHER ACL_OTHER
6054+#define SMB_ACL_MASK ACL_MASK
6055+
6056+#define SMB_ACL_T acl_t
6057+
6058+#define SMB_ACL_ENTRY_T acl_entry_t
6059+
6060+#define SMB_ACL_FIRST_ENTRY 0
6061+#define SMB_ACL_NEXT_ENTRY 1
6062+
6063+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
6064+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
6065+
e6a7303b 6066+#elif defined HAVE_UNIXWARE_ACLS || defined HAVE_SOLARIS_ACLS
0f6733d8
WD
6067+/*
6068+ * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
6069+ * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
6070+ */
6071+
6072+/* SVR4.2 ES/MP ACLs */
6073+typedef int SMB_ACL_TAG_T;
6074+typedef int SMB_ACL_TYPE_T;
6075+typedef ushort *SMB_ACL_PERMSET_T;
6076+typedef ushort SMB_ACL_PERM_T;
6077+#define SMB_ACL_READ 4
6078+#define SMB_ACL_WRITE 2
6079+#define SMB_ACL_EXECUTE 1
6080+
6081+/* Types of ACLs. */
6082+#define SMB_ACL_USER USER
6083+#define SMB_ACL_USER_OBJ USER_OBJ
6084+#define SMB_ACL_GROUP GROUP
6085+#define SMB_ACL_GROUP_OBJ GROUP_OBJ
6086+#define SMB_ACL_OTHER OTHER_OBJ
6087+#define SMB_ACL_MASK CLASS_OBJ
6088+
6089+typedef struct SMB_ACL_T {
6090+ int size;
6091+ int count;
6092+ int next;
6093+ struct acl acl[1];
6094+} *SMB_ACL_T;
6095+
6096+typedef struct acl *SMB_ACL_ENTRY_T;
6097+
6098+#define SMB_ACL_FIRST_ENTRY 0
6099+#define SMB_ACL_NEXT_ENTRY 1
6100+
6101+#define SMB_ACL_TYPE_ACCESS 0
6102+#define SMB_ACL_TYPE_DEFAULT 1
6103+
53c1073a
WD
6104+#ifdef __CYGWIN__
6105+#define SMB_ACL_LOSES_SPECIAL_MODE_BITS
6106+#endif
6107+
e6a7303b 6108+#elif defined HAVE_HPUX_ACLS
0f6733d8
WD
6109+
6110+/*
6111+ * Based on the Solaris & UnixWare code.
6112+ */
6113+
6114+#undef GROUP
6115+#include <sys/aclv.h>
6116+
6117+/* SVR4.2 ES/MP ACLs */
6118+typedef int SMB_ACL_TAG_T;
6119+typedef int SMB_ACL_TYPE_T;
6120+typedef ushort *SMB_ACL_PERMSET_T;
6121+typedef ushort SMB_ACL_PERM_T;
6122+#define SMB_ACL_READ 4
6123+#define SMB_ACL_WRITE 2
6124+#define SMB_ACL_EXECUTE 1
6125+
6126+/* Types of ACLs. */
6127+#define SMB_ACL_USER USER
6128+#define SMB_ACL_USER_OBJ USER_OBJ
6129+#define SMB_ACL_GROUP GROUP
6130+#define SMB_ACL_GROUP_OBJ GROUP_OBJ
6131+#define SMB_ACL_OTHER OTHER_OBJ
6132+#define SMB_ACL_MASK CLASS_OBJ
6133+
6134+typedef struct SMB_ACL_T {
6135+ int size;
6136+ int count;
6137+ int next;
6138+ struct acl acl[1];
6139+} *SMB_ACL_T;
6140+
6141+typedef struct acl *SMB_ACL_ENTRY_T;
6142+
6143+#define SMB_ACL_FIRST_ENTRY 0
6144+#define SMB_ACL_NEXT_ENTRY 1
6145+
6146+#define SMB_ACL_TYPE_ACCESS 0
6147+#define SMB_ACL_TYPE_DEFAULT 1
6148+
e6a7303b 6149+#elif defined HAVE_IRIX_ACLS
0f6733d8
WD
6150+
6151+#define SMB_ACL_TAG_T acl_tag_t
6152+#define SMB_ACL_TYPE_T acl_type_t
6153+#define SMB_ACL_PERMSET_T acl_permset_t
6154+#define SMB_ACL_PERM_T acl_perm_t
6155+#define SMB_ACL_READ ACL_READ
6156+#define SMB_ACL_WRITE ACL_WRITE
6157+#define SMB_ACL_EXECUTE ACL_EXECUTE
6158+
6159+/* Types of ACLs. */
6160+#define SMB_ACL_USER ACL_USER
6161+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
6162+#define SMB_ACL_GROUP ACL_GROUP
6163+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
6164+#define SMB_ACL_OTHER ACL_OTHER_OBJ
6165+#define SMB_ACL_MASK ACL_MASK
6166+
6167+typedef struct SMB_ACL_T {
6168+ int next;
6169+ BOOL freeaclp;
6170+ struct acl *aclp;
6171+} *SMB_ACL_T;
6172+
6173+#define SMB_ACL_ENTRY_T acl_entry_t
6174+
6175+#define SMB_ACL_FIRST_ENTRY 0
6176+#define SMB_ACL_NEXT_ENTRY 1
6177+
6178+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
6179+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
6180+
e6a7303b 6181+#elif defined HAVE_AIX_ACLS
0f6733d8
WD
6182+
6183+/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
6184+
6185+#include "/usr/include/acl.h"
6186+
6187+typedef uint *SMB_ACL_PERMSET_T;
6188+
6189+struct acl_entry_link{
6190+ struct acl_entry_link *prevp;
6191+ struct new_acl_entry *entryp;
6192+ struct acl_entry_link *nextp;
6193+ int count;
6194+};
6195+
6196+struct new_acl_entry{
6197+ unsigned short ace_len;
6198+ unsigned short ace_type;
6199+ unsigned int ace_access;
6200+ struct ace_id ace_id[1];
6201+};
6202+
6203+#define SMB_ACL_ENTRY_T struct new_acl_entry*
6204+#define SMB_ACL_T struct acl_entry_link*
6205+
6206+#define SMB_ACL_TAG_T unsigned short
6207+#define SMB_ACL_TYPE_T int
6208+#define SMB_ACL_PERM_T uint
6209+#define SMB_ACL_READ S_IRUSR
6210+#define SMB_ACL_WRITE S_IWUSR
6211+#define SMB_ACL_EXECUTE S_IXUSR
6212+
6213+/* Types of ACLs. */
6214+#define SMB_ACL_USER ACEID_USER
6215+#define SMB_ACL_USER_OBJ 3
6216+#define SMB_ACL_GROUP ACEID_GROUP
6217+#define SMB_ACL_GROUP_OBJ 4
6218+#define SMB_ACL_OTHER 5
6219+#define SMB_ACL_MASK 6
6220+
6221+
6222+#define SMB_ACL_FIRST_ENTRY 1
6223+#define SMB_ACL_NEXT_ENTRY 2
6224+
6225+#define SMB_ACL_TYPE_ACCESS 0
6226+#define SMB_ACL_TYPE_DEFAULT 1
6227+
6228+#else /* No ACLs. */
6229+
6230+/* No ACLS - fake it. */
6231+#define SMB_ACL_TAG_T int
6232+#define SMB_ACL_TYPE_T int
6233+#define SMB_ACL_PERMSET_T mode_t
6234+#define SMB_ACL_PERM_T mode_t
6235+#define SMB_ACL_READ S_IRUSR
6236+#define SMB_ACL_WRITE S_IWUSR
6237+#define SMB_ACL_EXECUTE S_IXUSR
6238+
6239+/* Types of ACLs. */
6240+#define SMB_ACL_USER 0
6241+#define SMB_ACL_USER_OBJ 1
6242+#define SMB_ACL_GROUP 2
6243+#define SMB_ACL_GROUP_OBJ 3
6244+#define SMB_ACL_OTHER 4
6245+#define SMB_ACL_MASK 5
6246+
6247+typedef struct SMB_ACL_T {
6248+ int dummy;
6249+} *SMB_ACL_T;
6250+
6251+typedef struct SMB_ACL_ENTRY_T {
6252+ int dummy;
6253+} *SMB_ACL_ENTRY_T;
6254+
6255+#define SMB_ACL_FIRST_ENTRY 0
6256+#define SMB_ACL_NEXT_ENTRY 1
6257+
6258+#define SMB_ACL_TYPE_ACCESS 0
6259+#define SMB_ACL_TYPE_DEFAULT 1
6260+
6261+#endif /* No ACLs. */
6262+#endif /* _SMB_ACLS_H */
6250eb3e
WD
6263--- old/t_stub.c
6264+++ new/t_stub.c
21158bc6 6265@@ -79,3 +79,7 @@ struct filter_list_struct server_filter_
6250eb3e
WD
6266 return NULL;
6267 }
6268
6269+ const char *who_am_i(void)
6270+{
6271+ return "test";
6272+}
475c4a36
WD
6273--- old/testsuite/acls.test
6274+++ new/testsuite/acls.test
6275@@ -0,0 +1,34 @@
6276+#! /bin/sh
6277+
6278+# This program is distributable under the terms of the GNU GPL (see
6279+# COPYING).
6280+
6281+# Test that rsync handles basic ACL preservation.
6282+
6283+. $srcdir/testsuite/rsync.fns
6284+
6285+$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
6286+case "$setfacl_nodef" in
6287+true) test_skipped "I don't know how to use your setfacl command" ;;
6288+esac
6289+
6290+makepath "$fromdir/foo"
6291+echo something >"$fromdir/file1"
6292+echo else >"$fromdir/file2"
6293+
6294+files='foo file1 file2'
6295+
6296+setfacl -m u:0:7 "$fromdir/foo" || test_skipped "Your filesystem has ACLs disabled"
6297+setfacl -m u:0:5 "$fromdir/file1"
6298+setfacl -m u:0:5 "$fromdir/file2"
6299+
6300+$RSYNC -avvA "$fromdir/" "$todir/"
6301+
6302+cd "$fromdir"
6303+getfacl $files >"$scratchdir/acls.txt"
6304+
6305+cd "$todir"
6306+getfacl $files | diff $diffopt "$scratchdir/acls.txt" -
6307+
6308+# The script would have aborted on error, so getting here means we've won.
6309+exit 0
9a7eef96
WD
6310--- old/testsuite/default-acls.test
6311+++ new/testsuite/default-acls.test
475c4a36 6312@@ -0,0 +1,65 @@
90fa6d68
WD
6313+#! /bin/sh
6314+
475c4a36 6315+# This program is distributable under the terms of the GNU GPL (see
90fa6d68
WD
6316+# COPYING).
6317+
6318+# Test that rsync obeys default ACLs. -- Matt McCutchen
6319+
6320+. $srcdir/testsuite/rsync.fns
6321+
25d385b9 6322+$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
53c1073a 6323+case "$setfacl_nodef" in
475c4a36 6324+true) test_skipped "I don't know how to use your setfacl command" ;;
53c1073a
WD
6325+*-k*) opts='-dm u::7,g::5,o:5' ;;
6326+*) opts='-m d:u::7,d:g::5,d:o:5' ;;
6327+esac
6328+setfacl $opts "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
90fa6d68 6329+
90fa6d68 6330+# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
25d385b9 6331+testit() {
90fa6d68
WD
6332+ todir="$scratchdir/$1"
6333+ mkdir "$todir"
53c1073a
WD
6334+ $setfacl_nodef "$todir"
6335+ if [ "$2" ]; then
6336+ case "$setfacl_nodef" in
6337+ *-k*) opts="-dm $2" ;;
6338+ *) opts="-m `echo $2 | sed 's/\([ugom]:\)/d:\1/g'`"
6339+ esac
6340+ setfacl $opts "$todir"
6341+ fi
90fa6d68
WD
6342+ # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
6343+ # even though the directory itself is outside the transfer
25d385b9 6344+ $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
0251480d 6345+ check_perms "$todir/to" $4 "Target $1"
25d385b9 6346+ check_perms "$todir/to/dir" $4 "Target $1"
0251480d
WD
6347+ check_perms "$todir/to/file" $3 "Target $1"
6348+ check_perms "$todir/to/program" $4 "Target $1"
90fa6d68
WD
6349+ # Make sure get_local_name doesn't mess us up when transferring only one file
6350+ $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
0251480d 6351+ check_perms "$todir/to/anotherfile" $3 "Target $1"
bb81ee98 6352+ # Make sure we obey default ACLs when not transferring a regular file
2578e2b6 6353+ $RSYNC -rvv "$scratchdir/dir/" "$todir/to/anotherdir/"
bb81ee98 6354+ check_perms "$todir/to/anotherdir" $4 "Target $1"
90fa6d68
WD
6355+}
6356+
25d385b9 6357+mkdir "$scratchdir/dir"
0251480d 6358+echo "File!" >"$scratchdir/file"
25d385b9
WD
6359+echo "#!/bin/sh" >"$scratchdir/program"
6360+chmod 777 "$scratchdir/dir"
0251480d
WD
6361+chmod 666 "$scratchdir/file"
6362+chmod 777 "$scratchdir/program"
6363+
90fa6d68
WD
6364+# Test some target directories
6365+umask 0077
53c1073a
WD
6366+testit da777 u::7,g::7,o:7 rw-rw-rw- rwxrwxrwx
6367+testit da775 u::7,g::7,o:5 rw-rw-r-- rwxrwxr-x
6368+testit da750 u::7,g::5,o:0 rw-r----- rwxr-x---
6369+testit da770mask u::7,u:0:7,g::0,m:7,o:0 rw-rw---- rwxrwx---
0251480d 6370+testit noda1 '' rw------- rwx------
90fa6d68 6371+umask 0000
0251480d 6372+testit noda2 '' rw-rw-rw- rwxrwxrwx
90fa6d68 6373+umask 0022
0251480d 6374+testit noda3 '' rw-r--r-- rwxr-xr-x
90fa6d68
WD
6375+
6376+# Hooray
6377+exit 0
0870c22a
WD
6378--- old/testsuite/devices.test
6379+++ new/testsuite/devices.test
6380@@ -42,14 +42,14 @@ touch -r "$fromdir/block" "$fromdir/bloc
6381 $RSYNC -ai "$fromdir/block" "$todir/block2" \
6382 | tee "$outfile"
6383 cat <<EOT >"$chkfile"
6384-cD+++++++ block
6385+cD+++++++++ block
6386 EOT
6387 diff $diffopt "$chkfile" "$outfile" || test_fail "test 1 failed"
6388
6389 $RSYNC -ai "$fromdir/block2" "$todir/block" \
6390 | tee "$outfile"
6391 cat <<EOT >"$chkfile"
6392-cD+++++++ block2
6393+cD+++++++++ block2
6394 EOT
6395 diff $diffopt "$chkfile" "$outfile" || test_fail "test 2 failed"
6396
6397@@ -58,7 +58,7 @@ sleep 1
6398 $RSYNC -Di "$fromdir/block3" "$todir/block" \
6399 | tee "$outfile"
6400 cat <<EOT >"$chkfile"
6401-cD..T.... block3
6402+cD..T...... block3
6403 EOT
6404 diff $diffopt "$chkfile" "$outfile" || test_fail "test 3 failed"
6405
6406@@ -66,15 +66,15 @@ $RSYNC -aiHvv "$fromdir/" "$todir/" \
6407 | tee "$outfile"
6408 filter_outfile
6409 cat <<EOT >"$chkfile"
6410-.d..t.... ./
6411-cD..t.... block
6412-cD....... block2
6413-cD+++++++ block3
6414-hD+++++++ block2.5 => block3
6415-cD+++++++ char
6416-cD+++++++ char2
6417-cD+++++++ char3
6418-cS+++++++ fifo
6419+.d..t...... ./
6420+cD..t...... block
6421+cD......... block2
6422+cD+++++++++ block3
6423+hD+++++++++ block2.5 => block3
6424+cD+++++++++ char
6425+cD+++++++++ char2
6426+cD+++++++++ char3
6427+cS+++++++++ fifo
6428 EOT
6429 if test ! -b "$fromdir/block2.5"; then
6430 sed -e '/block2\.5/d' \
6431--- old/testsuite/itemize.test
6432+++ new/testsuite/itemize.test
6433@@ -29,14 +29,14 @@ ln "$fromdir/foo/config1" "$fromdir/foo/
6434 $RSYNC -iplr "$fromdir/" "$todir/" \
6435 | tee "$outfile"
6436 cat <<EOT >"$chkfile"
6437-cd+++++++ bar/
6438-cd+++++++ bar/baz/
6439->f+++++++ bar/baz/rsync
6440-cd+++++++ foo/
6441->f+++++++ foo/config1
6442->f+++++++ foo/config2
6443->f+++++++ foo/extra
6444-cL+++++++ foo/sym -> ../bar/baz/rsync
6445+cd+++++++++ bar/
6446+cd+++++++++ bar/baz/
6447+>f+++++++++ bar/baz/rsync
6448+cd+++++++++ foo/
6449+>f+++++++++ foo/config1
6450+>f+++++++++ foo/config2
6451+>f+++++++++ foo/extra
6452+cL+++++++++ foo/sym -> ../bar/baz/rsync
6453 EOT
6454 diff $diffopt "$chkfile" "$outfile" || test_fail "test 1 failed"
6455
6456@@ -48,10 +48,10 @@ chmod 601 "$fromdir/foo/config2"
6457 $RSYNC -iplrH "$fromdir/" "$todir/" \
6458 | tee "$outfile"
6459 cat <<EOT >"$chkfile"
6460->f..T.... bar/baz/rsync
6461->f..T.... foo/config1
6462->f.sTp... foo/config2
6463-hf..T.... foo/extra => foo/config1
6464+>f..T...... bar/baz/rsync
6465+>f..T...... foo/config1
6466+>f.sTp..... foo/config2
6467+hf..T...... foo/extra => foo/config1
6468 EOT
6469 diff $diffopt "$chkfile" "$outfile" || test_fail "test 2 failed"
6470
6471@@ -68,11 +68,11 @@ chmod 777 "$todir/bar/baz/rsync"
6472 $RSYNC -iplrtc "$fromdir/" "$todir/" \
6473 | tee "$outfile"
6474 cat <<EOT >"$chkfile"
6475-.f..tp... bar/baz/rsync
6476-.d..t.... foo/
6477-.f..t.... foo/config1
6478->fcstp... foo/config2
6479-cL..T.... foo/sym -> ../bar/baz/rsync
6480+.f..tp..... bar/baz/rsync
6481+.d..t...... foo/
6482+.f..t...... foo/config1
6483+>fcstp..... foo/config2
6484+cL..T...... foo/sym -> ../bar/baz/rsync
6485 EOT
6486 diff $diffopt "$chkfile" "$outfile" || test_fail "test 3 failed"
6487
6488@@ -97,15 +97,15 @@ $RSYNC -ivvplrtH "$fromdir/" "$todir/" \
6489 | tee "$outfile"
6490 filter_outfile
6491 cat <<EOT >"$chkfile"
6492-.d ./
6493-.d bar/
6494-.d bar/baz/
6495-.f...p... bar/baz/rsync
6496-.d foo/
6497-.f foo/config1
6498->f..t.... foo/config2
6499-hf foo/extra
6500-.L foo/sym -> ../bar/baz/rsync
6501+.d ./
6502+.d bar/
6503+.d bar/baz/
6504+.f...p..... bar/baz/rsync
6505+.d foo/
6506+.f foo/config1
6507+>f..t...... foo/config2
6508+hf foo/extra
6509+.L foo/sym -> ../bar/baz/rsync
6510 EOT
6511 diff $diffopt "$chkfile" "$outfile" || test_fail "test 5 failed"
6512
6513@@ -124,8 +124,8 @@ touch "$todir/foo/config2"
6514 $RSYNC -iplrtH "$fromdir/" "$todir/" \
6515 | tee "$outfile"
6516 cat <<EOT >"$chkfile"
6517-.f...p... foo/config1
6518->f..t.... foo/config2
6519+.f...p..... foo/config1
6520+>f..t...... foo/config2
6521 EOT
6522 diff $diffopt "$chkfile" "$outfile" || test_fail "test 7 failed"
6523
6524@@ -134,15 +134,15 @@ $RSYNC -ivvplrtH --copy-dest="$lddir" "$
6525 | tee "$outfile"
6526 filter_outfile
6527 cat <<EOT >"$chkfile"
6528-.d..t.... ./
6529-cd+++++++ bar/
6530-cd+++++++ bar/baz/
6531-cf bar/baz/rsync
6532-cd+++++++ foo/
6533-cf foo/config1
6534-cf foo/config2
6535-hf foo/extra => foo/config1
6536-cL..T.... foo/sym -> ../bar/baz/rsync
6537+.d..t...... ./
6538+cd+++++++++ bar/
6539+cd+++++++++ bar/baz/
6540+cf bar/baz/rsync
6541+cd+++++++++ foo/
6542+cf foo/config1
6543+cf foo/config2
6544+hf foo/extra => foo/config1
6545+cL..T...... foo/sym -> ../bar/baz/rsync
6546 EOT
6547 diff $diffopt "$chkfile" "$outfile" || test_fail "test 8 failed"
6548
6549@@ -150,11 +150,11 @@ rm -rf "$todir"
6550 $RSYNC -iplrtH --copy-dest="$lddir" "$fromdir/" "$todir/" \
6551 | tee "$outfile"
6552 cat <<EOT >"$chkfile"
6553-.d..t.... ./
6554-cd+++++++ bar/
6555-cd+++++++ bar/baz/
6556-cd+++++++ foo/
6557-hf foo/extra => foo/config1
6558+.d..t...... ./
6559+cd+++++++++ bar/
6560+cd+++++++++ bar/baz/
6561+cd+++++++++ foo/
6562+hf foo/extra => foo/config1
6563 EOT
6564 diff $diffopt "$chkfile" "$outfile" || test_fail "test 9 failed"
6565
6566@@ -181,15 +181,15 @@ $RSYNC -ivvplrtH --link-dest="$lddir" "$
6567 | tee "$outfile"
6568 filter_outfile
6569 cat <<EOT >"$chkfile"
6570-.d..t.... ./
6571-cd+++++++ bar/
6572-cd+++++++ bar/baz/
6573-hf bar/baz/rsync
6574-cd+++++++ foo/
6575-hf foo/config1
6576-hf foo/config2
6577-hf foo/extra => foo/config1
6578-hL foo/sym -> ../bar/baz/rsync
6579+.d..t...... ./
6580+cd+++++++++ bar/
6581+cd+++++++++ bar/baz/
6582+hf bar/baz/rsync
6583+cd+++++++++ foo/
6584+hf foo/config1
6585+hf foo/config2
6586+hf foo/extra => foo/config1
6587+hL foo/sym -> ../bar/baz/rsync
6588 EOT
6589 diff $diffopt "$chkfile" "$outfile" || test_fail "test 11 failed"
6590
6591@@ -197,10 +197,10 @@ rm -rf "$todir"
6592 $RSYNC -iplrtH --link-dest="$lddir" "$fromdir/" "$todir/" \
6593 | tee "$outfile"
6594 cat <<EOT >"$chkfile"
6595-.d..t.... ./
6596-cd+++++++ bar/
6597-cd+++++++ bar/baz/
6598-cd+++++++ foo/
6599+.d..t...... ./
6600+cd+++++++++ bar/
6601+cd+++++++++ bar/baz/
6602+cd+++++++++ foo/
6603 EOT
6604 diff $diffopt "$chkfile" "$outfile" || test_fail "test 12 failed"
6605
6606@@ -228,14 +228,14 @@ filter_outfile
6607 # TODO fix really-old problem when combining -H with --compare-dest:
6608 # missing output for foo/extra hard-link (and it might not be updated)!
6609 cat <<EOT >"$chkfile"
6610-.d..t.... ./
6611-cd+++++++ bar/
6612-cd+++++++ bar/baz/
6613-.f bar/baz/rsync
6614-cd+++++++ foo/
6615-.f foo/config1
6616-.f foo/config2
6617-.L foo/sym -> ../bar/baz/rsync
6618+.d..t...... ./
6619+cd+++++++++ bar/
6620+cd+++++++++ bar/baz/
6621+.f bar/baz/rsync
6622+cd+++++++++ foo/
6623+.f foo/config1
6624+.f foo/config2
6625+.L foo/sym -> ../bar/baz/rsync
6626 EOT
6627 diff $diffopt "$chkfile" "$outfile" || test_fail "test 14 failed"
6628
6629@@ -243,10 +243,10 @@ rm -rf "$todir"
6630 $RSYNC -iplrtH --compare-dest="$lddir" "$fromdir/" "$todir/" \
6631 | tee "$outfile"
6632 cat <<EOT >"$chkfile"
6633-.d..t.... ./
6634-cd+++++++ bar/
6635-cd+++++++ bar/baz/
6636-cd+++++++ foo/
6637+.d..t...... ./
6638+cd+++++++++ bar/
6639+cd+++++++++ bar/baz/
6640+cd+++++++++ foo/
6641 EOT
6642 diff $diffopt "$chkfile" "$outfile" || test_fail "test 15 failed"
6643
9a7eef96
WD
6644--- old/uidlist.c
6645+++ new/uidlist.c
c1471566 6646@@ -35,6 +35,7 @@
fa26e11c
WD
6647 extern int verbose;
6648 extern int preserve_uid;
6649 extern int preserve_gid;
6650+extern int preserve_acls;
6651 extern int numeric_ids;
6652 extern int am_root;
6653
c1471566 6654@@ -275,7 +276,7 @@ void send_uid_list(int f)
fa26e11c
WD
6655 if (numeric_ids)
6656 return;
6657
6658- if (preserve_uid) {
6659+ if (preserve_uid || preserve_acls) {
6660 int len;
6661 /* we send sequences of uid/byte-length/name */
6662 for (list = uidlist; list; list = list->next) {
c1471566 6663@@ -292,7 +293,7 @@ void send_uid_list(int f)
fa26e11c
WD
6664 write_int(f, 0);
6665 }
6666
6667- if (preserve_gid) {
6668+ if (preserve_gid || preserve_acls) {
6669 int len;
6670 for (list = gidlist; list; list = list->next) {
6671 if (!list->name)
c1471566 6672@@ -313,7 +314,7 @@ void recv_uid_list(int f, struct file_li
fa26e11c
WD
6673 int id, i;
6674 char *name;
6675
6676- if (preserve_uid && !numeric_ids) {
6677+ if ((preserve_uid || preserve_acls) && !numeric_ids) {
6678 /* read the uid list */
6679 while ((id = read_int(f)) != 0) {
6680 int len = read_byte(f);
c1471566 6681@@ -325,7 +326,7 @@ void recv_uid_list(int f, struct file_li
6849cd84 6682 }
fa26e11c
WD
6683 }
6684
fa26e11c
WD
6685- if (preserve_gid && !numeric_ids) {
6686+ if ((preserve_gid || preserve_acls) && !numeric_ids) {
6687 /* read the gid list */
6688 while ((id = read_int(f)) != 0) {
6689 int len = read_byte(f);
c1471566 6690@@ -337,6 +338,16 @@ void recv_uid_list(int f, struct file_li
fa26e11c
WD
6691 }
6692 }
125d7fca 6693
4edb99c8 6694+#ifdef SUPPORT_ACLS
fa26e11c 6695+ if (preserve_acls && !numeric_ids) {
c52977bc
WD
6696+ id_t *id;
6697+ while ((id = next_acl_uid(flist)) != NULL)
6698+ *id = match_uid(*id);
6699+ while ((id = next_acl_gid(flist)) != NULL)
6700+ *id = match_gid(*id);
fa26e11c 6701+ }
162234a7 6702+#endif
125d7fca 6703+
90fa6d68 6704 /* Now convert all the uids/gids from sender values to our values. */
125d7fca 6705 if (am_root && preserve_uid && !numeric_ids) {
90fa6d68 6706 for (i = 0; i < flist->count; i++)
6250eb3e
WD
6707--- old/util.c
6708+++ new/util.c
21158bc6 6709@@ -1515,3 +1515,31 @@ int bitbag_next_bit(struct bitbag *bb, i
6250eb3e
WD
6710
6711 return -1;
6712 }
6713+
6714+void *expand_item_list(item_list *lp, size_t item_size,
645e162c 6715+ const char *desc, int incr)
6250eb3e
WD
6716+{
6717+ /* First time through, 0 <= 0, so list is expanded. */
6718+ if (lp->malloced <= lp->count) {
6719+ void *new_ptr;
6720+ size_t new_size = lp->malloced;
645e162c
WD
6721+ if (incr < 0)
6722+ new_size -= incr; /* increase slowly */
98ccc74a 6723+ else if (new_size < (size_t)incr)
645e162c
WD
6724+ new_size += incr;
6725+ else
6726+ new_size *= 2;
6250eb3e
WD
6727+ new_ptr = realloc_array(lp->items, char, new_size * item_size);
6728+ if (verbose >= 4) {
6729+ rprintf(FINFO, "[%s] expand %s to %.0f bytes, did%s move\n",
6730+ who_am_i(), desc, (double)new_size * item_size,
6731+ new_ptr == lp->items ? " not" : "");
6732+ }
6733+ if (!new_ptr)
6734+ out_of_memory("expand_item_list");
6735+
6736+ lp->items = new_ptr;
6737+ lp->malloced = new_size;
6738+ }
6739+ return (char*)lp->items + (lp->count++ * item_size);
6740+}