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