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