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