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