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