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