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