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