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