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