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