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