Made all the type loops more efficient.
[rsync/rsync-patches.git] / acls.diff
CommitLineData
fa26e11c
WD
1After applying this patch, run these commands for a successful build:
2
27e96866 3 ./prepare-source
f787c90c 4 ./configure --enable-acl-support
fa26e11c
WD
5 make
6
f05cc718
WD
7The program currently complains when the --acls (-A) option is used to copy
8from a disk that doesn't support ACLs. This should be changed to silently
9notice that no ACLs are available to copy. Of course, trying to write out
10ACLs to a non-ACL-supporting disk should complain.
fa26e11c 11
9a7eef96
WD
12--- old/Makefile.in
13+++ new/Makefile.in
4bf6f8c7 14@@ -25,15 +25,15 @@ VERSION=@VERSION@
fa26e11c
WD
15 .SUFFIXES:
16 .SUFFIXES: .c .o
17
18-HEADERS=byteorder.h config.h errcode.h proto.h rsync.h lib/pool_alloc.h
19+HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
20 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
0f6733d8
WD
21- lib/permstring.o lib/pool_alloc.o @LIBOBJS@
22+ lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
4bf6f8c7
WD
23 ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
24 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
fa26e11c
WD
25 OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
26 main.o checksum.o match.o syscall.o log.o backup.o
27 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
610969d1
WD
28- fileio.o batch.o clientname.o chmod.o
29+ fileio.o batch.o clientname.o chmod.o acls.o
fa26e11c
WD
30 OBJS3=progress.o pipe.o
31 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
32 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
9a7eef96
WD
33--- old/acls.c
34+++ new/acls.c
8db8e7d2 35@@ -0,0 +1,1233 @@
fa26e11c
WD
36+/* -*- c-file-style: "linux" -*-
37+ Copyright (C) Andrew Tridgell 1996
38+ Copyright (C) Paul Mackerras 1996
39+
40+ This program is free software; you can redistribute it and/or modify
41+ it under the terms of the GNU General Public License as published by
42+ the Free Software Foundation; either version 2 of the License, or
43+ (at your option) any later version.
44+
45+ This program is distributed in the hope that it will be useful,
46+ but WITHOUT ANY WARRANTY; without even the implied warranty of
47+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48+ GNU General Public License for more details.
49+
50+ You should have received a copy of the GNU General Public License
51+ along with this program; if not, write to the Free Software
52+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
53+*/
54+
55+/* handle passing ACLs between systems */
56+
57+#include "rsync.h"
0f6733d8 58+#include "lib/sysacls.h"
fa26e11c 59+
4df546eb 60+#ifdef SUPPORT_ACLS
fa26e11c
WD
61+
62+extern int preserve_acls;
63+extern int am_root;
64+extern int dry_run;
90fa6d68 65+extern int orig_umask;
fa26e11c
WD
66+
67+typedef struct {
68+ id_t id;
69+ uchar access;
70+ SMB_ACL_TAG_T tag_type;
71+} rsync_ace;
72+
73+typedef struct {
74+ size_t count;
75+ size_t malloced;
76+ rsync_ace *races;
77+} rsync_acl;
78+
79+static const rsync_acl rsync_acl_initializer = { 0, 0, NULL };
8db8e7d2
WD
80+
81+#define OTHER_TYPE(t) (SMB_ACL_TYPE_ACCESS+SMB_ACL_TYPE_DEFAULT-(t))
82+#define BUMP_TYPE(t) ((t = OTHER_TYPE(t)) == SMB_ACL_TYPE_DEFAULT)
fa26e11c
WD
83+
84+static void expand_rsync_acl(rsync_acl *racl)
85+{
4edb99c8
WD
86+ /* First time through, 0 <= 0, so list is expanded.
87+ * (Diabolical, rsync guys!) */
fa26e11c 88+ if (racl->malloced <= racl->count) {
0f6733d8
WD
89+ rsync_ace *new_ptr;
90+ size_t new_size = racl->malloced + 10;
91+ new_ptr = realloc_array(racl->races, rsync_ace, new_size);
fa26e11c
WD
92+ if (verbose >= 4) {
93+ rprintf(FINFO, "expand rsync_acl to %.0f bytes, did%s move\n",
0f6733d8 94+ (double) new_size * sizeof racl->races[0],
fa26e11c
WD
95+ racl->races ? "" : " not");
96+ }
97+
0f6733d8
WD
98+ racl->races = new_ptr;
99+ racl->malloced = new_size;
fa26e11c
WD
100+
101+ if (!racl->races)
102+ out_of_memory("expand_rsync_acl");
103+ }
104+}
105+
106+static void rsync_acl_free(rsync_acl *racl)
107+{
108+ free(racl->races);
109+ racl->races = NULL;
110+ racl->count = 0;
111+ racl->malloced = 0;
112+}
113+
114+static int rsync_ace_sorter(const void *r1, const void *r2)
115+{
116+ rsync_ace *race1 = (rsync_ace *)r1;
117+ SMB_ACL_TAG_T rtag1 = race1->tag_type;
118+ id_t rid1 = race1->id;
119+ rsync_ace *race2 = (rsync_ace *)r2;
120+ SMB_ACL_TAG_T rtag2 = race2->tag_type;
121+ id_t rid2 = race2->id;
122+ /* start at the extrema */
123+ if (rtag1 == SMB_ACL_USER_OBJ || rtag2 == SMB_ACL_MASK)
124+ return -1;
125+ if (rtag2 == SMB_ACL_USER_OBJ || rtag1 == SMB_ACL_MASK)
126+ return 1;
127+ /* work inwards */
128+ if (rtag1 == SMB_ACL_OTHER)
129+ return 1;
130+ if (rtag2 == SMB_ACL_OTHER)
131+ return -1;
132+ /* only SMB_ACL_USERs and SMB_ACL_GROUP*s left */
133+ if (rtag1 == SMB_ACL_USER) {
134+ switch (rtag2) {
135+ case SMB_ACL_GROUP:
136+ case SMB_ACL_GROUP_OBJ:
137+ case SMB_ACL_OTHER:
138+ return -1;
139+ }
140+ /* both USER */
141+ return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
142+ }
143+ if (rtag2 == SMB_ACL_USER)
144+ return 1;
145+ /* only SMB_ACL_GROUP*s to worry about; kick out GROUP_OBJs first */
146+ if (rtag1 == SMB_ACL_GROUP_OBJ)
147+ return -1;
148+ if (rtag2 == SMB_ACL_GROUP_OBJ)
149+ return 1;
150+ /* only SMB_ACL_GROUPs left */
151+ return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
152+}
153+
154+static void sort_rsync_acl(rsync_acl *racl)
155+{
156+ if (!racl->count)
157+ return;
158+ qsort((void **)racl->races, racl->count, sizeof racl->races[0],
159+ &rsync_ace_sorter);
160+}
161+
162+static BOOL unpack_smb_acl(rsync_acl *racl, SMB_ACL_T sacl)
163+{
164+ SMB_ACL_ENTRY_T entry;
165+ int rc;
166+ const char *errfun;
167+ *racl = rsync_acl_initializer;
168+ errfun = "sys_acl_get_entry";
169+ for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
170+ rc == 1;
171+ rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
172+ SMB_ACL_PERMSET_T permset;
173+ void *qualifier;
174+ rsync_ace *race;
175+ expand_rsync_acl(racl);
176+ race = &racl->races[racl->count++];
177+ if ((rc = sys_acl_get_tag_type(entry, &race->tag_type))) {
178+ errfun = "sys_acl_get_tag_type";
179+ break;
180+ }
181+ if ((rc = sys_acl_get_permset(entry, &permset))) {
182+ errfun = "sys_acl_get_tag_type";
183+ break;
184+ }
185+ race->access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
186+ | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
187+ | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
188+ switch (race->tag_type) {
189+ case SMB_ACL_USER:
190+ case SMB_ACL_GROUP:
191+ break;
192+ default:
193+ continue;
194+ }
195+ if (!(qualifier = sys_acl_get_qualifier(entry))) {
196+ errfun = "sys_acl_get_tag_type";
197+ rc = EINVAL;
198+ break;
199+ }
200+ race->id = *((id_t *)qualifier);
201+ sys_acl_free_qualifier(qualifier, race->tag_type);
202+ }
203+ if (rc) {
204+ rprintf(FERROR, "unpack_smb_acl: %s(): %s\n",
205+ errfun, strerror(errno));
206+ rsync_acl_free(racl);
207+ return False;
208+ }
209+ sort_rsync_acl(racl);
210+ return True;
211+}
212+
213+static BOOL rsync_acls_equal(const rsync_acl *racl1, const rsync_acl *racl2)
214+{
215+ rsync_ace *race1, *race2;
216+ size_t count = racl1->count;
217+ if (count != racl2->count)
218+ return False;
219+ race1 = racl1->races;
220+ race2 = racl2->races;
221+ for (; count--; race1++, race2++) {
222+ if (race1->tag_type != race2->tag_type
223+ || race1->access != race2->access
224+ || ((race1->tag_type == SMB_ACL_USER
225+ || race1->tag_type == SMB_ACL_GROUP)
226+ && race1->id != race2->id))
227+ return False;
228+ }
229+ return True;
230+}
231+
232+typedef struct {
233+ size_t count;
234+ size_t malloced;
235+ rsync_acl *racls;
236+} rsync_acl_list;
237+
238+static rsync_acl_list _rsync_acl_lists[] = {
239+ { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
240+ { 0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
241+};
242+
243+static inline rsync_acl_list *rsync_acl_lists(SMB_ACL_TYPE_T type)
244+{
245+ return type == SMB_ACL_TYPE_ACCESS ? &_rsync_acl_lists[0]
246+ : &_rsync_acl_lists[1];
247+}
248+
249+static void expand_rsync_acl_list(rsync_acl_list *racl_list)
250+{
4edb99c8
WD
251+ /* First time through, 0 <= 0, so list is expanded.
252+ * (Diabolical, rsync guys!) */
fa26e11c 253+ if (racl_list->malloced <= racl_list->count) {
0f6733d8 254+ rsync_acl *new_ptr;
fa26e11c
WD
255+ size_t new_size;
256+ if (racl_list->malloced < 1000)
0f6733d8 257+ new_size = racl_list->malloced + 1000;
fa26e11c 258+ else
0f6733d8
WD
259+ new_size = racl_list->malloced * 2;
260+ new_ptr = realloc_array(racl_list->racls, rsync_acl, new_size);
fa26e11c
WD
261+ if (verbose >= 3) {
262+ rprintf(FINFO, "expand_rsync_acl_list to %.0f bytes, did%s move\n",
0f6733d8 263+ (double) new_size * sizeof racl_list->racls[0],
fa26e11c
WD
264+ racl_list->racls ? "" : " not");
265+ }
266+
0f6733d8
WD
267+ racl_list->racls = new_ptr;
268+ racl_list->malloced = new_size;
fa26e11c
WD
269+
270+ if (!racl_list->racls)
271+ out_of_memory("expand_rsync_acl_list");
272+ }
273+}
274+
275+#if 0
276+static void free_rsync_acl_list(rsync_acl_list *racl_list)
277+{
4edb99c8
WD
278+ /* Run this in reverse, so references are freed before referents,
279+ * although not currently necessary. */
fa26e11c
WD
280+ while (racl_list->count--) {
281+ rsync_acl *racl = &racl_list->racls[racl_list->count];
282+ if (racl)
283+ rsync_acl_free(racl);
284+ }
285+ free(racl_list->racls);
286+ racl_list->racls = NULL;
287+ racl_list->malloced = 0;
288+}
289+#endif
290+
291+static int find_matching_rsync_acl(SMB_ACL_TYPE_T type,
292+ const rsync_acl_list *racl_list,
293+ const rsync_acl *racl)
294+{
295+ static int access_match = -1, default_match = -1;
296+ int *match = (type == SMB_ACL_TYPE_ACCESS) ?
297+ &access_match : &default_match;
298+ size_t count = racl_list->count;
4edb99c8 299+ /* If this is the first time through or we didn't match the last
fa26e11c 300+ * time, then start at the end of the list, which should be the
4edb99c8 301+ * best place to start hunting. */
fa26e11c
WD
302+ if (*match == -1)
303+ *match = racl_list->count - 1;
304+ while (count--) {
305+ if (rsync_acls_equal(&racl_list->racls[*match], racl))
306+ return *match;
307+ if (!(*match)--)
308+ *match = racl_list->count - 1;
309+ }
310+ *match = -1;
311+ return *match;
312+}
313+
4edb99c8 314+/* The general strategy with the tag_type <-> character mapping is that
fa26e11c 315+ * lowercase implies that no qualifier follows, where uppercase does.
4edb99c8 316+ * A similar idiom for the acl type (access or default) itself, but
fa26e11c
WD
317+ * lowercase in this instance means there's no ACL following, so the
318+ * ACL is a repeat, so the receiver should reuse the last of the same
4edb99c8 319+ * type ACL. */
fa26e11c
WD
320+
321+static void send_rsync_acl(int f, const rsync_acl *racl)
322+{
323+ rsync_ace *race;
324+ size_t count = racl->count;
325+ write_int(f, count);
326+ for (race = racl->races; count--; race++) {
327+ char ch;
328+ switch (race->tag_type) {
329+ case SMB_ACL_USER_OBJ:
330+ ch = 'u';
331+ break;
332+ case SMB_ACL_USER:
333+ ch = 'U';
334+ break;
335+ case SMB_ACL_GROUP_OBJ:
336+ ch = 'g';
337+ break;
338+ case SMB_ACL_GROUP:
339+ ch = 'G';
340+ break;
341+ case SMB_ACL_OTHER:
342+ ch = 'o';
343+ break;
344+ case SMB_ACL_MASK:
345+ ch = 'm';
346+ break;
347+ default:
348+ rprintf(FERROR,
e839d833 349+ "send_rsync_acl: unknown tag_type (%02x) on ACE; disregarding\n",
fa26e11c
WD
350+ race->tag_type);
351+ continue;
352+ }
353+ write_byte(f, ch);
354+ write_byte(f, race->access);
355+ if (isupper((int)ch)) {
356+ write_int(f, race->id);
4edb99c8 357+ /* FIXME: sorta wasteful: we should maybe buffer as
fa26e11c 358+ * many ids as max(ACL_USER + ACL_GROUP) objects to
4edb99c8 359+ * keep from making so many calls. */
fa26e11c
WD
360+ if (ch == 'U')
361+ add_uid(race->id);
362+ else
363+ add_gid(race->id);
364+ }
365+ }
366+}
367+
368+static rsync_acl _curr_rsync_acls[2];
369+
370+
371+static const char *str_acl_type(SMB_ACL_TYPE_T type)
372+{
373+ return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS" :
374+ type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT" :
375+ "unknown SMB_ACL_TYPE_T";
376+}
377+
b8fc785b
WD
378+/*
379+ * Overwrite racl with a new three-entry ACL from the given permissions.
380+ */
381+static void perms_to_acl(int perms, rsync_acl *racl)
382+{
383+ racl->count = 0;
384+ expand_rsync_acl(racl);
385+ racl->races[racl->count].tag_type = SMB_ACL_USER_OBJ;
386+ racl->races[racl->count++].access = (perms >> 6) & 7;
387+ expand_rsync_acl(racl);
388+ racl->races[racl->count].tag_type = SMB_ACL_GROUP_OBJ;
389+ racl->races[racl->count++].access = (perms >> 3) & 7;
390+ expand_rsync_acl(racl);
391+ racl->races[racl->count].tag_type = SMB_ACL_OTHER;
392+ racl->races[racl->count++].access = (perms >> 0) & 7;
393+}
394+
4edb99c8
WD
395+/* Generate the ACL(s) for this flist entry;
396+ * ACL(s) are either sent or cleaned-up by send_acl() below. */
fa26e11c 397+
ee12ff0e 398+int make_acl(const struct file_struct *file, const char *fname)
fa26e11c 399+{
8db8e7d2 400+ SMB_ACL_TYPE_T type;
fa26e11c 401+ rsync_acl *curr_racl;
36aa3171 402+
fa26e11c 403+ if (!preserve_acls || S_ISLNK(file->mode))
05b88206 404+ return 1;
8db8e7d2
WD
405+
406+ curr_racl = &_curr_rsync_acls[0];
407+ type = SMB_ACL_TYPE_ACCESS;
408+ do {
fa26e11c
WD
409+ SMB_ACL_T sacl;
410+ BOOL ok;
411+ *curr_racl = rsync_acl_initializer;
8db8e7d2 412+ if ((sacl = sys_acl_get_file(fname, type)) != 0) {
b8fc785b
WD
413+ ok = unpack_smb_acl(curr_racl, sacl);
414+ sys_acl_free_acl(sacl);
415+ if (!ok)
416+ return -1;
417+ } else if (errno == ENOTSUP) {
418+ /* ACLs are not supported. Invent an access ACL from
419+ * permissions; let the default ACL default to empty. */
8db8e7d2 420+ if (type == SMB_ACL_TYPE_ACCESS)
b8fc785b
WD
421+ perms_to_acl(file->mode & ACCESSPERMS, curr_racl);
422+ } else {
c171f097 423+ rprintf(FERROR, "send_acl: sys_acl_get_file(%s, %s): %s\n",
8db8e7d2 424+ fname, str_acl_type(type), strerror(errno));
05b88206 425+ return -1;
fa26e11c 426+ }
8db8e7d2
WD
427+ curr_racl++;
428+ } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
429+
05b88206 430+ return 0;
fa26e11c
WD
431+}
432+
4edb99c8
WD
433+/* Send the make_acl()-generated ACLs for this flist entry,
434+ * or clean up after an flist entry that's not being sent (f == -1). */
fa26e11c
WD
435+
436+void send_acl(const struct file_struct *file, int f)
437+{
8db8e7d2 438+ SMB_ACL_TYPE_T type;
fa26e11c 439+ rsync_acl *curr_racl;
36aa3171 440+
fa26e11c
WD
441+ if (!preserve_acls || S_ISLNK(file->mode))
442+ return;
8db8e7d2
WD
443+ curr_racl = &_curr_rsync_acls[0];
444+ type = SMB_ACL_TYPE_ACCESS;
445+ do {
fa26e11c 446+ int index;
8db8e7d2 447+ rsync_acl_list *racl_list = rsync_acl_lists(type);
fa26e11c
WD
448+ if (f == -1) {
449+ rsync_acl_free(curr_racl);
450+ continue;
451+ }
8db8e7d2 452+ if ((index = find_matching_rsync_acl(type, racl_list, curr_racl))
fa26e11c 453+ != -1) {
8db8e7d2 454+ write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
fa26e11c
WD
455+ write_int(f, index);
456+ rsync_acl_free(curr_racl);
457+ } else {
8db8e7d2 458+ write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
fa26e11c
WD
459+ send_rsync_acl(f, curr_racl);
460+ expand_rsync_acl_list(racl_list);
461+ racl_list->racls[racl_list->count++] = *curr_racl;
462+ }
8db8e7d2
WD
463+ curr_racl++;
464+ } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
fa26e11c
WD
465+}
466+
4edb99c8 467+/* The below stuff is only used by the receiver: */
fa26e11c
WD
468+
469+/* structure to hold index to rsync_acl_list member corresponding to
470+ * flist->files[i] */
471+
472+typedef struct {
473+ const struct file_struct *file;
474+ int aclidx;
475+} file_acl_index;
476+
477+typedef struct {
478+ size_t count;
479+ size_t malloced;
480+ file_acl_index *fileaclidxs;
481+} file_acl_index_list;
482+
483+static file_acl_index_list _file_acl_index_lists[] = {
484+ {0, 0, NULL },/* SMB_ACL_TYPE_ACCESS */
485+ {0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
486+};
487+
488+static inline file_acl_index_list *file_acl_index_lists(SMB_ACL_TYPE_T type)
489+{
490+ return type == SMB_ACL_TYPE_ACCESS ?
491+ &_file_acl_index_lists[0] : &_file_acl_index_lists[1];
492+}
493+
494+static void expand_file_acl_index_list(file_acl_index_list *fileaclidx_list)
495+{
4edb99c8
WD
496+ /* First time through, 0 <= 0, so list is expanded.
497+ * (Diabolical, rsync guys!) */
fa26e11c 498+ if (fileaclidx_list->malloced <= fileaclidx_list->count) {
0f6733d8 499+ file_acl_index *new_ptr;
fa26e11c
WD
500+ size_t new_size;
501+ if (fileaclidx_list->malloced < 1000)
0f6733d8 502+ new_size = fileaclidx_list->malloced + 1000;
fa26e11c 503+ else
0f6733d8
WD
504+ new_size = fileaclidx_list->malloced * 2;
505+ new_ptr = realloc_array(fileaclidx_list->fileaclidxs, file_acl_index, new_size);
fa26e11c
WD
506+ if (verbose >= 3) {
507+ rprintf(FINFO, "expand_file_acl_index_list to %.0f bytes, did%s move\n",
0f6733d8 508+ (double) new_size * sizeof fileaclidx_list->fileaclidxs[0],
fa26e11c
WD
509+ fileaclidx_list->fileaclidxs ? "" : " not");
510+ }
511+
0f6733d8
WD
512+ fileaclidx_list->fileaclidxs = new_ptr;
513+ fileaclidx_list->malloced = new_size;
fa26e11c
WD
514+
515+ if (!fileaclidx_list->fileaclidxs)
516+ out_of_memory("expand_file_acl_index_list");
517+ }
518+}
519+
520+#if 0
521+static void free_file_acl_index_list(file_acl_index_list *fileaclidx_list)
522+{
523+ free(fileaclidx_list->fileaclidxs);
524+ fileaclidx_list->fileaclidxs = NULL;
525+ fileaclidx_list->malloced = 0;
526+}
527+#endif
528+
529+/* lists to hold the SMB_ACL_Ts corresponding to the rsync_acl_list entries */
530+
531+typedef struct {
532+ size_t count;
533+ size_t malloced;
534+ SMB_ACL_T *sacls;
535+} smb_acl_list;
536+
537+static smb_acl_list _smb_acl_lists[] = {
538+ { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
539+ { 0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
540+};
541+
542+static inline smb_acl_list *smb_acl_lists(SMB_ACL_TYPE_T type)
543+{
544+ return type == SMB_ACL_TYPE_ACCESS ? &_smb_acl_lists[0] :
545+ &_smb_acl_lists[1];
546+}
547+
548+static void expand_smb_acl_list(smb_acl_list *sacl_list)
549+{
4edb99c8
WD
550+ /* First time through, 0 <= 0, so list is expanded.
551+ * (Diabolical, rsync guys!) */
fa26e11c 552+ if (sacl_list->malloced <= sacl_list->count) {
0f6733d8 553+ SMB_ACL_T *new_ptr;
fa26e11c
WD
554+ size_t new_size;
555+ if (sacl_list->malloced < 1000)
0f6733d8 556+ new_size = sacl_list->malloced + 1000;
fa26e11c 557+ else
0f6733d8
WD
558+ new_size = sacl_list->malloced * 2;
559+ new_ptr = realloc_array(sacl_list->sacls, SMB_ACL_T, new_size);
fa26e11c
WD
560+ if (verbose >= 3) {
561+ rprintf(FINFO, "expand_smb_acl_list to %.0f bytes, did%s move\n",
0f6733d8 562+ (double) new_size * sizeof sacl_list->sacls[0],
fa26e11c
WD
563+ sacl_list->sacls ? "" : " not");
564+ }
565+
0f6733d8
WD
566+ sacl_list->sacls = new_ptr;
567+ sacl_list->malloced = new_size;
fa26e11c
WD
568+
569+ if (!sacl_list->sacls)
570+ out_of_memory("expand_smb_acl_list");
571+ }
572+}
573+
574+#if 0
575+static void free_smb_acl_list(SMB_ACL_TYPE_T type)
576+{
577+ smb_acl_list *sacl_list = smb_acl_lists(type);
578+ SMB_ACL_T *sacl = sacl_list->sacls;
579+ while (sacl_list->count--) {
580+ if (*sacl)
581+ sys_acl_free_acl(*sacl++);
582+ }
583+ free(sacl_list->sacls);
584+ sacl_list->sacls = NULL;
585+ sacl_list->malloced = 0;
586+}
587+#endif
588+
589+/* build an SMB_ACL_T corresponding to an rsync_acl */
590+static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
591+{
592+ size_t count = racl->count;
593+ rsync_ace *race = racl->races;
594+ const char *errfun = NULL;
595+ *smb_acl = sys_acl_init(count);
596+ if (!*smb_acl) {
597+ rprintf(FERROR, "pack_smb_acl: sys_acl_int(): %s\n",
598+ strerror(errno));
599+ return False;
600+ }
601+ for (; count--; race++) {
602+ SMB_ACL_ENTRY_T entry;
603+ SMB_ACL_PERMSET_T permset;
604+ if (sys_acl_create_entry(smb_acl, &entry)) {
605+ errfun = "sys_acl_create)";
606+ break;
607+ }
608+ if (sys_acl_set_tag_type(entry, race->tag_type)) {
609+ errfun = "sys_acl_set_tag";
610+ break;
611+ }
612+ if (race->tag_type == SMB_ACL_USER ||
613+ race->tag_type == SMB_ACL_GROUP)
614+ if (sys_acl_set_qualifier(entry, (void*)&race->id)) {
615+ errfun = "sys_acl_set_qualfier";
616+ break;
617+ }
618+ if (sys_acl_get_permset(entry, &permset)) {
619+ errfun = "sys_acl_get_permset";
620+ break;
621+ }
622+ if (sys_acl_clear_perms(permset)) {
623+ errfun = "sys_acl_clear_perms";
624+ break;
625+ }
626+ if (race->access & 4)
627+ if (sys_acl_add_perm(permset, SMB_ACL_READ)) {
628+ errfun = "sys_acl_add_perm";
629+ break;
630+ }
631+ if (race->access & 2)
632+ if (sys_acl_add_perm(permset, SMB_ACL_WRITE)) {
633+ errfun = "sys_acl_add_perm";
634+ break;
635+ }
636+ if (race->access & 1)
637+ if (sys_acl_add_perm(permset, SMB_ACL_EXECUTE)) {
638+ errfun = "sys_acl_add_perm";
639+ break;
640+ }
641+ if (sys_acl_set_permset(entry, permset)) {
642+ errfun = "sys_acl_set_permset";
643+ break;
644+ }
645+ }
646+ if (errfun) {
647+ sys_acl_free_acl(*smb_acl);
648+ rprintf(FERROR, "pack_smb_acl %s(): %s\n", errfun,
649+ strerror(errno));
650+ return False;
651+ }
652+ return True;
653+}
654+
655+static void receive_rsync_acl(rsync_acl *racl, int f)
656+{
657+#if ACLS_NEED_MASK
658+ uchar required_mask_perm = 0;
fa26e11c 659+#endif
1ce58df1 660+ BOOL saw_mask = False;
fa26e11c
WD
661+ BOOL saw_user_obj = False, saw_group_obj = False,
662+ saw_other = False;
663+ size_t count = read_int(f);
664+ rsync_ace *race;
665+ if (!count)
666+ return;
667+ while (count--) {
668+ uchar tag = read_byte(f);
669+ expand_rsync_acl(racl);
670+ race = &racl->races[racl->count++];
671+ switch (tag) {
672+ case 'u':
673+ race->tag_type = SMB_ACL_USER_OBJ;
674+ saw_user_obj = True;
675+ break;
676+ case 'U':
677+ race->tag_type = SMB_ACL_USER;
678+ break;
679+ case 'g':
680+ race->tag_type = SMB_ACL_GROUP_OBJ;
681+ saw_group_obj = True;
682+ break;
683+ case 'G':
684+ race->tag_type = SMB_ACL_GROUP;
685+ break;
686+ case 'o':
687+ race->tag_type = SMB_ACL_OTHER;
688+ saw_other = True;
689+ break;
690+ case 'm':
691+ race->tag_type = SMB_ACL_MASK;
fa26e11c 692+ saw_mask = True;
fa26e11c
WD
693+ break;
694+ default:
695+ rprintf(FERROR, "receive_rsync_acl: unknown tag %c\n",
696+ tag);
697+ exit_cleanup(RERR_STREAMIO);
698+ }
699+ race->access = read_byte(f);
700+ if (race->access & ~ (4 | 2 | 1)) {
701+ rprintf(FERROR, "receive_rsync_acl: bogus permset %o\n",
702+ race->access);
703+ exit_cleanup(RERR_STREAMIO);
704+ }
705+ if (race->tag_type == SMB_ACL_USER ||
706+ race->tag_type == SMB_ACL_GROUP) {
707+ race->id = read_int(f);
708+#if ACLS_NEED_MASK
709+ required_mask_perm |= race->access;
710+#endif
711+ }
712+#if ACLS_NEED_MASK
713+ else if (race->tag_type == SMB_ACL_GROUP_OBJ)
714+ required_mask_perm |= race->access;
715+#endif
716+
717+ }
718+ if (!saw_user_obj) {
719+ expand_rsync_acl(racl);
720+ race = &racl->races[racl->count++];
721+ race->tag_type = SMB_ACL_USER_OBJ;
722+ race->access = 7;
723+ }
724+ if (!saw_group_obj) {
725+ expand_rsync_acl(racl);
726+ race = &racl->races[racl->count++];
727+ race->tag_type = SMB_ACL_GROUP_OBJ;
728+ race->access = 0;
729+ }
730+ if (!saw_other) {
731+ expand_rsync_acl(racl);
732+ race = &racl->races[racl->count++];
733+ race->tag_type = SMB_ACL_OTHER;
734+ race->access = 0;
735+ }
736+#if ACLS_NEED_MASK
737+ if (!saw_mask) {
738+ expand_rsync_acl(racl);
739+ race = &racl->races[racl->count++];
740+ race->tag_type = SMB_ACL_MASK;
741+ race->access = required_mask_perm;
742+ }
1ce58df1
WD
743+#else
744+ /* If we, a system without ACLS_NEED_MASK, received data from a
745+ * system that has masks, throw away the extraneous CLASS_OBJs. */
746+ if (saw_mask && racl->count == 4) {
747+ rsync_ace *group_obj_race = NULL, *mask_race = NULL;
748+ rsync_ace *p;
749+ size_t i;
750+ for (i = 0, p = racl->races; i < racl->count; i++, p++) {
751+ if (p->tag_type == SMB_ACL_MASK)
752+ mask_race = p;
753+ else if (p->tag_type == SMB_ACL_GROUP_OBJ)
754+ group_obj_race = p;
755+ }
756+ if (mask_race == NULL || group_obj_race == NULL) {
757+ rprintf(FERROR, "receive_rsync_acl: have four ACES "
758+ "and one's ACL_MASK but missing "
759+ "either it or ACL_GROUP_OBJ, "
760+ "when pruning ACL\n");
761+ } else {
762+ /* mask off group perms with it first */
763+ group_obj_race->access &= mask_race->access;
764+ /* dump mask_race; re-slot any followers-on */
765+ racl->count--;
766+ if (mask_race != &racl->races[racl->count]) {
767+ *mask_race = racl->races[racl->count];
768+ saw_user_obj = False; /* force re-sort */
769+ }
770+ }
771+ }
fa26e11c
WD
772+#endif
773+#if ACLS_NEED_MASK
774+ if (!(saw_user_obj && saw_group_obj && saw_other && saw_mask))
775+#else
776+ if (!(saw_user_obj && saw_group_obj && saw_other))
777+#endif
778+ sort_rsync_acl(racl);
779+}
780+
781+/* receive and build the rsync_acl_lists */
782+
783+void receive_acl(struct file_struct *file, int f)
784+{
8db8e7d2 785+ SMB_ACL_TYPE_T type;
fa26e11c 786+ char *fname;
36aa3171 787+
fa26e11c
WD
788+ if (!preserve_acls || S_ISLNK(file->mode))
789+ return;
36aa3171 790+
bd68c3c2 791+ fname = f_name(file, NULL);
8db8e7d2
WD
792+ type = SMB_ACL_TYPE_ACCESS;
793+ do {
fa26e11c 794+ file_acl_index_list *fileaclidx_list =
8db8e7d2 795+ file_acl_index_lists(type);
fa26e11c
WD
796+ uchar tag;
797+ expand_file_acl_index_list(fileaclidx_list);
798+
799+ tag = read_byte(f);
800+ if (tag == 'A' || tag == 'a') {
8db8e7d2 801+ if (type != SMB_ACL_TYPE_ACCESS) {
fa26e11c
WD
802+ rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
803+ fname);
804+ exit_cleanup(RERR_STREAMIO);
805+ }
806+ } else if (tag == 'D' || tag == 'd') {
8db8e7d2 807+ if (type == SMB_ACL_TYPE_ACCESS) {
fa26e11c
WD
808+ rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
809+ fname);
810+ exit_cleanup(RERR_STREAMIO);
811+ }
812+ } else {
813+ rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
814+ fname, tag);
815+ exit_cleanup(RERR_STREAMIO);
816+ }
817+ if (tag == 'A' || tag == 'D') {
818+ rsync_acl racl = rsync_acl_initializer;
8db8e7d2
WD
819+ rsync_acl_list *racl_list = rsync_acl_lists(type);
820+ smb_acl_list *sacl_list = smb_acl_lists(type);
fa26e11c
WD
821+ fileaclidx_list->fileaclidxs[fileaclidx_list->count].
822+ aclidx = racl_list->count;
823+ fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
824+ file = file;
825+ receive_rsync_acl(&racl, f);
826+ expand_rsync_acl_list(racl_list);
827+ racl_list->racls[racl_list->count++] = racl;
828+ expand_smb_acl_list(sacl_list);
829+ sacl_list->sacls[sacl_list->count++] = NULL;
830+ } else {
831+ int index = read_int(f);
8db8e7d2 832+ rsync_acl_list *racl_list = rsync_acl_lists(type);
fa26e11c
WD
833+ if ((size_t) index >= racl_list->count) {
834+ rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
835+ fname,
8db8e7d2 836+ str_acl_type(type),
fa26e11c
WD
837+ index);
838+ exit_cleanup(RERR_STREAMIO);
839+ }
840+ fileaclidx_list->fileaclidxs[fileaclidx_list->count].
841+ aclidx = index;
842+ fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
843+ file = file;
844+ }
8db8e7d2 845+ } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
fa26e11c
WD
846+}
847+
848+static int file_acl_index_list_sorter(const void *f1, const void *f2)
849+{
850+ const file_acl_index *fileaclidx1 = (const file_acl_index *)f1;
851+ const file_acl_index *fileaclidx2 = (const file_acl_index *)f2;
852+ return fileaclidx1->file == fileaclidx2->file ? 0 :
853+ fileaclidx1->file < fileaclidx2->file ? -1 : 1;
854+}
855+
856+void sort_file_acl_index_lists()
857+{
8db8e7d2 858+ SMB_ACL_TYPE_T type;
36aa3171 859+
fa26e11c
WD
860+ if (!preserve_acls)
861+ return;
36aa3171 862+
8db8e7d2
WD
863+ type = SMB_ACL_TYPE_ACCESS;
864+ do {
fa26e11c 865+ file_acl_index_list *fileaclidx_list =
8db8e7d2 866+ file_acl_index_lists(type);
fa26e11c
WD
867+ if (!fileaclidx_list->count)
868+ continue;
869+ qsort(fileaclidx_list->fileaclidxs, fileaclidx_list->count,
870+ sizeof fileaclidx_list->fileaclidxs[0],
871+ &file_acl_index_list_sorter);
8db8e7d2 872+ } while (BUMP_TYPE(type));
fa26e11c
WD
873+}
874+
875+static int find_file_acl_index(const file_acl_index_list *fileaclidx_list,
876+ const struct file_struct *file) {
877+ int low = 0, high = fileaclidx_list->count;
878+ const struct file_struct *file_mid;
879+ if (!high--)
880+ return -1;
881+ do {
882+ int mid = (high + low) / 2;
883+ file_mid = fileaclidx_list->fileaclidxs[mid].file;
884+ if (file_mid == file)
885+ return fileaclidx_list->fileaclidxs[mid].aclidx;
886+ if (file_mid > file)
887+ high = mid - 1;
888+ else
889+ low = mid + 1;
890+ } while (low < high);
891+ if (low == high) {
892+ file_mid = fileaclidx_list->fileaclidxs[low].file;
893+ if (file_mid == file)
894+ return fileaclidx_list->fileaclidxs[low].aclidx;
895+ }
896+ rprintf(FERROR,
897+ "find_file_acl_index: can't find entry for file in list\n");
898+ exit_cleanup(RERR_STREAMIO);
899+ return -1;
900+}
901+
902+/* for duplicating ACLs on backups when using backup_dir */
903+
904+int dup_acl(const char *orig, const char *bak, mode_t mode)
905+{
8db8e7d2 906+ SMB_ACL_TYPE_T type;
fa26e11c 907+ int ret = 0;
36aa3171 908+
fa26e11c 909+ if (!preserve_acls)
05b88206 910+ return 1;
36aa3171 911+
8db8e7d2
WD
912+ type = SMB_ACL_TYPE_ACCESS;
913+ do {
fa26e11c
WD
914+ SMB_ACL_T sacl_orig, sacl_bak;
915+ rsync_acl racl_orig, racl_bak;
8db8e7d2 916+ if (!(sacl_orig = sys_acl_get_file(orig, type))) {
c171f097 917+ rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s\n",
8db8e7d2 918+ orig, str_acl_type(type), strerror(errno));
fa26e11c
WD
919+ ret = -1;
920+ continue;
921+ }
8db8e7d2 922+ if (!(sacl_bak = sys_acl_get_file(orig, type))) {
c171f097 923+ rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s. ignoring\n",
8db8e7d2 924+ bak, str_acl_type(type), strerror(errno));
fa26e11c
WD
925+ ret = -1;
926+ /* try to forge on through */
927+ }
928+ if (!unpack_smb_acl(&racl_orig, sacl_orig)) {
929+ ret = -1;
930+ goto out_with_sacls;
931+ }
932+ if (sacl_bak) {
933+ if (!unpack_smb_acl(&racl_bak, sacl_bak)) {
934+ ret = -1;
935+ goto out_with_one_racl;
936+ }
937+ if (rsync_acls_equal(&racl_orig, &racl_bak))
938+ goto out_with_all;
939+ } else {
940+ ; /* presume they're unequal */
941+ }
8db8e7d2 942+ if (type == SMB_ACL_TYPE_DEFAULT && !racl_orig.count) {
e6a7303b 943+ if (sys_acl_delete_def_file(bak) < 0) {
fa26e11c
WD
944+ rprintf(FERROR, "dup_acl: sys_acl_delete_def_file(%s): %s\n",
945+ bak, strerror(errno));
946+ ret = -1;
947+ }
8db8e7d2 948+ } else if (sys_acl_set_file(bak, type, sacl_bak) < 0) {
c171f097 949+ rprintf(FERROR, "dup_acl: sys_acl_set_file(%s, %s): %s\n",
8db8e7d2 950+ bak, str_acl_type(type), strerror(errno));
fa26e11c
WD
951+ ret = -1;
952+ }
953+ out_with_all:
954+ if (sacl_bak)
955+ rsync_acl_free(&racl_bak);
956+ out_with_one_racl:
957+ rsync_acl_free(&racl_orig);
958+ out_with_sacls:
959+ if (sacl_bak)
960+ sys_acl_free_acl(sacl_bak);
961+ /* out_with_one_sacl: */
962+ if (sacl_orig)
963+ sys_acl_free_acl(sacl_orig);
8db8e7d2
WD
964+ } while (BUMP_TYPE(type) && S_ISDIR(mode));
965+
fa26e11c
WD
966+ return ret;
967+}
968+
90fa6d68 969+/* Stuff for redirecting calls to set_acl() from set_file_attrs()
4edb99c8 970+ * for keep_backup(). */
fa26e11c
WD
971+static const struct file_struct *backup_orig_file = NULL;
972+static const char null_string[] = "";
973+static const char *backup_orig_fname = null_string;
974+static const char *backup_dest_fname = null_string;
975+static SMB_ACL_T _backup_sacl[] = { NULL, NULL };
976+
977+void push_keep_backup_acl(const struct file_struct *file,
978+ const char *orig, const char *dest)
979+{
8db8e7d2
WD
980+ SMB_ACL_TYPE_T type;
981+ SMB_ACL_T *sacl;
982+
983+ if (!preserve_acls)
984+ return;
985+
986+ backup_orig_file = file;
987+ backup_orig_fname = orig;
988+ backup_dest_fname = dest;
989+
990+ sacl = &_backup_sacl[0];
991+ type = SMB_ACL_TYPE_ACCESS;
992+ do {
993+ if (type == SMB_ACL_TYPE_DEFAULT && !S_ISDIR(file->mode)) {
994+ *sacl = NULL;
995+ break;
fa26e11c 996+ }
8db8e7d2
WD
997+ if (!(*sacl = sys_acl_get_file(orig, type))) {
998+ rprintf(FERROR,
999+ "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
1000+ orig, str_acl_type(type),
1001+ strerror(errno));
1002+ }
1003+ } while (BUMP_TYPE(type));
fa26e11c
WD
1004+}
1005+
1006+static int set_keep_backup_acl()
1007+{
8db8e7d2
WD
1008+ SMB_ACL_TYPE_T type;
1009+ SMB_ACL_T *sacl;
1010+ int ret = 0;
1011+
1012+ if (!preserve_acls)
1013+ return 1;
1014+
1015+ sacl = &_backup_sacl[0];
1016+ type = SMB_ACL_TYPE_ACCESS;
1017+ do {
1018+ if (*sacl
1019+ && sys_acl_set_file(backup_dest_fname, type, *sacl) < 0) {
1020+ rprintf(FERROR,
1021+ "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
1022+ backup_dest_fname,
1023+ str_acl_type(type),
1024+ strerror(errno));
1025+ ret = -1;
fa26e11c 1026+ }
8db8e7d2
WD
1027+ } while (BUMP_TYPE(type));
1028+
1029+ return ret;
fa26e11c
WD
1030+}
1031+
1032+void cleanup_keep_backup_acl()
1033+{
8db8e7d2
WD
1034+ SMB_ACL_TYPE_T type;
1035+ SMB_ACL_T *sacl;
1036+
1037+ if (!preserve_acls)
1038+ return;
1039+
1040+ backup_orig_file = NULL;
1041+ backup_orig_fname = null_string;
1042+ backup_dest_fname = null_string;
1043+
1044+ sacl = &_backup_sacl[0];
1045+ type = SMB_ACL_TYPE_ACCESS;
1046+ do {
1047+ if (*sacl) {
1048+ sys_acl_free_acl(*sacl);
fa26e11c
WD
1049+ *sacl = NULL;
1050+ }
8db8e7d2 1051+ } while (BUMP_TYPE(type));
fa26e11c
WD
1052+}
1053+
1054+/* set ACL on rsync-ed or keep_backup-ed file */
1055+
1056+int set_acl(const char *fname, const struct file_struct *file)
1057+{
81ddc4dc 1058+ int unchanged = 1;
8db8e7d2 1059+ SMB_ACL_TYPE_T type;
36aa3171 1060+
fa26e11c 1061+ if (dry_run || !preserve_acls || S_ISLNK(file->mode))
05b88206 1062+ return 1;
36aa3171 1063+
fa26e11c
WD
1064+ if (file == backup_orig_file) {
1065+ if (!strcmp(fname, backup_dest_fname))
1066+ return set_keep_backup_acl();
1067+ }
8db8e7d2
WD
1068+ type = SMB_ACL_TYPE_ACCESS;
1069+ do {
fa26e11c
WD
1070+ SMB_ACL_T sacl_orig, *sacl_new;
1071+ rsync_acl racl_orig, *racl_new;
8db8e7d2 1072+ int aclidx = find_file_acl_index(file_acl_index_lists(type),
fa26e11c
WD
1073+ file);
1074+ BOOL ok;
8db8e7d2
WD
1075+ racl_new = &(rsync_acl_lists(type)->racls[aclidx]);
1076+ sacl_new = &(smb_acl_lists(type)->sacls[aclidx]);
1077+ sacl_orig = sys_acl_get_file(fname, type);
fa26e11c
WD
1078+ if (!sacl_orig) {
1079+ rprintf(FERROR, "set_acl: sys_acl_get_file(%s, %s): %s\n",
8db8e7d2 1080+ fname, str_acl_type(type), strerror(errno));
81ddc4dc 1081+ unchanged = -1;
fa26e11c
WD
1082+ continue;
1083+ }
1084+ ok = unpack_smb_acl(&racl_orig, sacl_orig);
1085+ sys_acl_free_acl(sacl_orig);
1086+ if (!ok) {
81ddc4dc 1087+ unchanged = -1;
fa26e11c
WD
1088+ continue;
1089+ }
1090+ ok = rsync_acls_equal(&racl_orig, racl_new);
1091+ rsync_acl_free(&racl_orig);
1092+ if (ok)
1093+ continue;
8db8e7d2 1094+ if (type == SMB_ACL_TYPE_DEFAULT && !racl_new->count) {
e6a7303b 1095+ if (sys_acl_delete_def_file(fname) < 0) {
fa26e11c
WD
1096+ rprintf(FERROR, "set_acl: sys_acl_delete_def_file(%s): %s\n",
1097+ fname, strerror(errno));
81ddc4dc 1098+ unchanged = -1;
fa26e11c
WD
1099+ continue;
1100+ }
1101+ } else {
1102+ if (!*sacl_new)
1103+ if (!pack_smb_acl(sacl_new, racl_new)) {
81ddc4dc 1104+ unchanged = -1;
fa26e11c
WD
1105+ continue;
1106+ }
8db8e7d2 1107+ if (sys_acl_set_file(fname, type, *sacl_new) < 0) {
fa26e11c 1108+ rprintf(FERROR, "set_acl: sys_acl_set_file(%s, %s): %s\n",
8db8e7d2 1109+ fname, str_acl_type(type),
fa26e11c 1110+ strerror(errno));
81ddc4dc 1111+ unchanged = -1;
fa26e11c
WD
1112+ continue;
1113+ }
1114+ }
81ddc4dc
WD
1115+ if (unchanged == 1)
1116+ unchanged = 0;
8db8e7d2
WD
1117+ } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
1118+
81ddc4dc 1119+ return unchanged;
fa26e11c
WD
1120+}
1121+
4edb99c8 1122+/* Enumeration functions for uid mapping: */
fa26e11c 1123+
4edb99c8
WD
1124+/* Context -- one and only one. Should be cycled through once on uid
1125+ * mapping and once on gid mapping. */
fa26e11c
WD
1126+static rsync_acl_list *_enum_racl_lists[] = {
1127+ &_rsync_acl_lists[0], &_rsync_acl_lists[1], NULL
1128+};
1129+
1130+static rsync_acl_list **enum_racl_list = &_enum_racl_lists[0];
1131+static size_t enum_racl_index = 0;
1132+static size_t enum_race_index = 0;
1133+
4edb99c8
WD
1134+/* This returns the next tag_type id from the given acl for the next entry,
1135+ * or it returns 0 if there are no more tag_type ids in the acl. */
fa26e11c
WD
1136+
1137+static id_t next_ace_id(SMB_ACL_TAG_T tag_type, const rsync_acl *racl)
1138+{
1139+ for (; enum_race_index < racl->count; enum_race_index++) {
1140+ rsync_ace *race = &racl->races[enum_race_index];
1141+ if (race->tag_type == tag_type)
1142+ return race->id;
1143+ }
1144+ enum_race_index = 0;
1145+ return 0;
1146+}
1147+
1148+static id_t next_acl_id(SMB_ACL_TAG_T tag_type, const rsync_acl_list *racl_list)
1149+{
1150+ for (; enum_racl_index < racl_list->count; enum_racl_index++) {
1151+ rsync_acl *racl = &racl_list->racls[enum_racl_index];
1152+ id_t id = next_ace_id(tag_type, racl);
1153+ if (id)
1154+ return id;
1155+ }
1156+ enum_racl_index = 0;
1157+ return 0;
1158+}
1159+
1160+static id_t next_acl_list_id(SMB_ACL_TAG_T tag_type)
1161+{
1162+ for (; *enum_racl_list; enum_racl_list++) {
1163+ id_t id = next_acl_id(tag_type, *enum_racl_list);
1164+ if (id)
1165+ return id;
1166+ }
1167+ enum_racl_list = &_enum_racl_lists[0];
1168+ return 0;
1169+}
1170+
1171+id_t next_acl_uid()
1172+{
1173+ return next_acl_list_id(SMB_ACL_USER);
1174+}
1175+
1176+id_t next_acl_gid()
1177+{
1178+ return next_acl_list_id(SMB_ACL_GROUP);
1179+}
1180+
1181+/* referring to the global context enum_entry, sets the entry's id */
1182+static void set_acl_id(id_t id)
1183+{
1184+ (*enum_racl_list)->racls[enum_racl_index].races[enum_race_index++].id = id;
1185+}
1186+
1187+void acl_uid_map(id_t uid)
1188+{
1189+ set_acl_id(uid);
1190+}
1191+
1192+void acl_gid_map(id_t gid)
1193+{
1194+ set_acl_id(gid);
1195+}
1196+
26c810d8 1197+#define PERMS_SPLICE(perms,newbits,where) (((perms) & ~(7 << (where))) | ((newbits) << (where)))
90fa6d68 1198+
26c810d8 1199+int default_perms_for_dir(const char *dir)
90fa6d68
WD
1200+{
1201+ rsync_acl racl;
1202+ SMB_ACL_T sacl;
1203+ BOOL ok, saw_mask = False;
90fa6d68 1204+ size_t i;
26c810d8 1205+ int perms;
90fa6d68
WD
1206+
1207+ if (dir == NULL)
1208+ dir = ".";
26c810d8 1209+ perms = ACCESSPERMS & ~orig_umask;
90fa6d68
WD
1210+ /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1211+ sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1212+ if (sacl == NULL) {
1213+ /* Couldn't get an ACL. Darn. */
1214+ switch (errno) {
1215+ case ENOTSUP:
1216+ /* ACLs are disabled. We could yell at the user to turn them on, but... */
1217+ break;
1218+ case ENOENT:
1219+ if (dry_run) {
1220+ /* We're doing a dry run, so the containing directory
1221+ * wasn't actually created. Don't worry about it. */
1222+ break;
1223+ }
1224+ /* Otherwise fall through. */
1225+ default:
26c810d8 1226+ rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
90fa6d68
WD
1227+ dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1228+ }
26c810d8 1229+ return perms;
90fa6d68
WD
1230+ }
1231+
1232+ /* Convert it. */
1233+ ok = unpack_smb_acl(&racl, sacl);
1234+ sys_acl_free_acl(sacl);
1235+ if (!ok) {
26c810d8
WD
1236+ rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1237+ return perms;
90fa6d68
WD
1238+ }
1239+
26c810d8
WD
1240+ /* Look at each default ACL entry and possibly modify three bits of `perms' accordingly.
1241+ * If there's "no" default ACL, there will be zero entries and the umask-based perms is unchanged. */
90fa6d68
WD
1242+ for (i = 0; i < racl.count; i++) {
1243+ switch (racl.races[i].tag_type) {
1244+ case SMB_ACL_USER_OBJ:
26c810d8 1245+ perms = PERMS_SPLICE(perms, racl.races[i].access, 6);
90fa6d68
WD
1246+ break;
1247+ case SMB_ACL_GROUP_OBJ:
1248+ if (!saw_mask)
26c810d8 1249+ perms = PERMS_SPLICE(perms, racl.races[i].access, 3);
90fa6d68
WD
1250+ break;
1251+ case SMB_ACL_MASK:
1252+ saw_mask = True;
26c810d8 1253+ perms = PERMS_SPLICE(perms, racl.races[i].access, 3);
90fa6d68
WD
1254+ break;
1255+ case SMB_ACL_OTHER:
26c810d8 1256+ perms = PERMS_SPLICE(perms, racl.races[i].access, 0);
90fa6d68
WD
1257+ break;
1258+ default:
1259+ break;
1260+ }
1261+ }
1262+ rsync_acl_free(&racl);
1263+ if (verbose > 2)
26c810d8
WD
1264+ rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1265+ return perms;
90fa6d68
WD
1266+}
1267+
fa26e11c 1268+#endif /* SUPPORT_ACLS */
9a7eef96
WD
1269--- old/backup.c
1270+++ new/backup.c
e6a7303b 1271@@ -132,6 +132,9 @@ static int make_bak_dir(char *fullpath)
fa26e11c
WD
1272 } else {
1273 do_lchown(fullpath, st.st_uid, st.st_gid);
1274 do_chmod(fullpath, st.st_mode);
e6a7303b
WD
1275+#ifdef SUPPORT_ACLS
1276+ dup_acl(end, fullpath, st.st_mode);
1277+#endif
fa26e11c
WD
1278 }
1279 }
1280 *p = '/';
e6a7303b 1281@@ -185,6 +188,10 @@ static int keep_backup(char *fname)
172ad6c3 1282 if (!(buf = get_backup_name(fname)))
fa26e11c 1283 return 0;
fa26e11c 1284
e6a7303b
WD
1285+#ifdef SUPPORT_ACLS
1286+ push_keep_backup_acl(file, fname, buf);
1287+#endif
fa26e11c 1288+
fa26e11c 1289 /* Check to see if this is a device file, or link */
90fa6d68
WD
1290 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1291 || (preserve_specials && IS_SPECIAL(file->mode))) {
e6a7303b 1292@@ -260,6 +267,9 @@ static int keep_backup(char *fname)
fa26e11c
WD
1293 }
1294 }
90fa6d68 1295 set_file_attrs(buf, file, NULL, 0);
e6a7303b
WD
1296+#ifdef SUPPORT_ACLS
1297+ cleanup_keep_backup_acl();
1298+#endif
fa26e11c
WD
1299 free(file);
1300
27a7053c 1301 if (verbose > 1) {
9a7eef96
WD
1302--- old/configure.in
1303+++ new/configure.in
afcb578c 1304@@ -482,6 +482,11 @@ if test x"$ac_cv_func_strcasecmp" = x"no
fa26e11c
WD
1305 AC_CHECK_LIB(resolv, strcasecmp)
1306 fi
1307
1308+AC_CHECK_FUNCS(aclsort)
1309+if test x"$ac_cv_func_aclsort" = x"no"; then
1310+ AC_CHECK_LIB(sec, aclsort)
1311+fi
1312+
1313 dnl At the moment we don't test for a broken memcmp(), because all we
1314 dnl need to do is test for equality, not comparison, and it seems that
1315 dnl every platform has a memcmp that can do at least that.
afcb578c 1316@@ -738,6 +743,77 @@ AC_SUBST(OBJ_RESTORE)
fa26e11c
WD
1317 AC_SUBST(CC_SHOBJ_FLAG)
1318 AC_SUBST(BUILD_POPT)
125d7fca 1319
fa26e11c
WD
1320+AC_CHECK_HEADERS(sys/acl.h)
1321+AC_CHECK_FUNCS(_acl __acl _facl __facl)
1322+#################################################
1323+# check for ACL support
1324+
1325+AC_MSG_CHECKING(whether to support ACLs)
f787c90c
WD
1326+AC_ARG_ENABLE(acl-support,
1327+AC_HELP_STRING([--enable-acl-support], [Include ACL support (default=no)]),
3b05e91f 1328+[ case "$enableval" in
fa26e11c
WD
1329+ yes)
1330+
1331+ case "$host_os" in
1332+ *sysv5*)
1333+ AC_MSG_RESULT(Using UnixWare ACLs)
1334+ AC_DEFINE(HAVE_UNIXWARE_ACLS, 1, [true if you have UnixWare ACLs])
1335+ ;;
1336+ *solaris*)
1337+ AC_MSG_RESULT(Using solaris ACLs)
1338+ AC_DEFINE(HAVE_SOLARIS_ACLS, 1, [true if you have solaris ACLs])
1339+ ;;
1340+ *hpux*)
1341+ AC_MSG_RESULT(Using HPUX ACLs)
1342+ AC_DEFINE(HAVE_HPUX_ACLS, 1, [true if you have HPUX ACLs])
1343+ ;;
1344+ *irix*)
1345+ AC_MSG_RESULT(Using IRIX ACLs)
1346+ AC_DEFINE(HAVE_IRIX_ACLS, 1, [true if you have IRIX ACLs])
1347+ ;;
1348+ *aix*)
1349+ AC_MSG_RESULT(Using AIX ACLs)
1350+ AC_DEFINE(HAVE_AIX_ACLS, 1, [true if you have AIX ACLs])
1351+ ;;
1352+ *osf*)
1353+ AC_MSG_RESULT(Using Tru64 ACLs)
1354+ AC_DEFINE(HAVE_TRU64_ACLS, 1, [true if you have Tru64 ACLs])
1355+ LIBS="$LIBS -lpacl"
1356+ ;;
1357+ *)
81549708 1358+ AC_MSG_RESULT(ACLs requested -- running tests)
fa26e11c
WD
1359+ AC_CHECK_LIB(acl,acl_get_file)
1360+ AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1361+ AC_TRY_LINK([#include <sys/types.h>
1362+#include <sys/acl.h>],
1363+[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1364+samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
1365+ if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1366+ AC_MSG_RESULT(Using posix ACLs)
1367+ AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
81549708 1368+ AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
fa26e11c
WD
1369+ AC_TRY_LINK([#include <sys/types.h>
1370+#include <sys/acl.h>],
1371+[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1372+samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
81549708
WD
1373+ if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1374+ AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1375+ fi
1376+ else
1377+ AC_MSG_ERROR(Failed to find ACL support)
fa26e11c
WD
1378+ fi
1379+ ;;
1380+ esac
1381+ ;;
1382+ *)
1383+ AC_MSG_RESULT(no)
1384+ AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1385+ ;;
1386+ esac ],
1387+ AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1388+ AC_MSG_RESULT(no)
1389+)
125d7fca 1390+
fa26e11c
WD
1391 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1392 AC_OUTPUT
125d7fca 1393
9a7eef96
WD
1394--- old/flist.c
1395+++ new/flist.c
e6a7303b 1396@@ -966,6 +966,10 @@ static struct file_struct *send_file_nam
e0e47893 1397 f == -2 ? SERVER_FILTERS : ALL_FILTERS);
09fb8f03 1398 if (!file)
5e048c14 1399 return NULL;
e6a7303b
WD
1400+#ifdef SUPPORT_ACLS
1401+ if (make_acl(file, fname) < 0)
4df546eb 1402+ return NULL;
e6a7303b 1403+#endif
fa26e11c 1404
3b05e91f 1405 if (chmod_modes && !S_ISLNK(file->mode))
e0e47893 1406 file->mode = tweak_mode(file->mode, chmod_modes);
e6a7303b 1407@@ -977,6 +981,12 @@ static struct file_struct *send_file_nam
fa26e11c
WD
1408 if (file->basename[0]) {
1409 flist->files[flist->count++] = file;
e0e47893 1410 send_file_entry(file, f);
e6a7303b
WD
1411+#ifdef SUPPORT_ACLS
1412+ send_acl(file, f);
fa26e11c
WD
1413+ } else {
1414+ /* Cleanup unsent ACL(s). */
e6a7303b
WD
1415+ send_acl(file, -1);
1416+#endif
fa26e11c 1417 }
5e048c14
WD
1418 return file;
1419 }
e6a7303b 1420@@ -1365,6 +1375,10 @@ struct file_list *recv_file_list(int f)
fa26e11c 1421 flags |= read_byte(f) << 8;
618752a7 1422 file = receive_file_entry(flist, flags, f);
fa26e11c 1423
e6a7303b
WD
1424+#ifdef SUPPORT_ACLS
1425+ receive_acl(file, f);
1426+#endif
fa26e11c 1427+
ae6834e1 1428 if (S_ISREG(file->mode) || S_ISLNK(file->mode))
618752a7 1429 stats.total_size += file->length;
fa26e11c 1430
e6a7303b 1431@@ -1387,6 +1401,10 @@ struct file_list *recv_file_list(int f)
fa26e11c
WD
1432
1433 clean_flist(flist, relative_paths, 1);
fa26e11c 1434
e6a7303b
WD
1435+#ifdef SUPPORT_ACLS
1436+ sort_file_acl_index_lists();
1437+#endif
125d7fca 1438+
09fb8f03 1439 if (f >= 0) {
90fa6d68
WD
1440 recv_uid_list(f, flist);
1441
9a7eef96
WD
1442--- old/generator.c
1443+++ new/generator.c
2578e2b6 1444@@ -85,6 +85,7 @@ extern long block_size; /* "long" becaus
1a2fa68f
WD
1445 extern int max_delete;
1446 extern int force_delete;
1447 extern int one_file_system;
1448+extern mode_t orig_umask;
1449 extern struct stats stats;
1450 extern dev_t filesystem_dev;
1451 extern char *backup_dir;
2578e2b6 1452@@ -753,6 +754,7 @@ static int try_dests_non(struct file_str
26c810d8
WD
1453 }
1454
1455 static int phase = 0;
1456+static int dflt_perms;
1457
1458 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
1459 * make sure it exists, and has the right permissions/timestamp info. For
1a2fa68f 1460@@ -844,6 +846,10 @@ static void recv_generator(char *fname,
01deb4dc
WD
1461 }
1462 if (fuzzy_basis)
63673ef2 1463 need_fuzzy_dirlist = 1;
26c810d8 1464+#ifdef SUPPORT_ACLS
01deb4dc
WD
1465+ if (!preserve_perms)
1466+ dflt_perms = default_perms_for_dir(dn);
26c810d8 1467+#endif
26c810d8 1468 }
01deb4dc 1469 parent_dirname = dn;
26c810d8 1470
1a2fa68f 1471@@ -871,7 +877,8 @@ static void recv_generator(char *fname,
90fa6d68
WD
1472 if (!preserve_perms) {
1473 int exists = statret == 0
1474 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1475- file->mode = dest_mode(file->mode, st.st_mode, exists);
26c810d8
WD
1476+ file->mode = dest_mode(file->mode, st.st_mode, dflt_perms,
1477+ exists);
90fa6d68
WD
1478 }
1479
1480 if (S_ISDIR(file->mode)) {
81ddc4dc 1481@@ -1342,6 +1349,8 @@ void generate_files(int f_out, struct fi
26c810d8
WD
1482 * notice that and let us know via the redo pipe (or its closing). */
1483 ignore_timeout = 1;
1484
1485+ dflt_perms = (ACCESSPERMS & ~orig_umask);
1486+
1487 for (i = 0; i < flist->count; i++) {
1488 struct file_struct *file = flist->files[i];
1489
9a7eef96
WD
1490--- old/lib/sysacls.c
1491+++ new/lib/sysacls.c
252945ef 1492@@ -0,0 +1,3242 @@
fa26e11c 1493+/*
5ca9317d 1494+ Unix SMB/CIFS implementation.
0f6733d8
WD
1495+ Samba system utilities for ACL support.
1496+ Copyright (C) Jeremy Allison 2000.
fa26e11c
WD
1497+
1498+ This program is free software; you can redistribute it and/or modify
1499+ it under the terms of the GNU General Public License as published by
1500+ the Free Software Foundation; either version 2 of the License, or
1501+ (at your option) any later version.
1502+
1503+ This program is distributed in the hope that it will be useful,
1504+ but WITHOUT ANY WARRANTY; without even the implied warranty of
1505+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1506+ GNU General Public License for more details.
1507+
1508+ You should have received a copy of the GNU General Public License
1509+ along with this program; if not, write to the Free Software
1510+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1511+*/
1512+
0f6733d8 1513+#include "rsync.h"
252945ef 1514+#include "sysacls.h" /****** ADDED ******/
fa26e11c 1515+
252945ef 1516+/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
0f6733d8
WD
1517+void SAFE_FREE(void *mem)
1518+{
1519+ if (mem)
1520+ free(mem);
1521+}
fa26e11c 1522+
5ca9317d
WD
1523+char *uidtoname(uid_t uid)
1524+{
1525+ static char idbuf[12];
1526+ struct passwd *pw;
1527+
1528+ if ((pw = getpwuid(uid)) == NULL) {
1529+ slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
1530+ return idbuf;
1531+ }
1532+ return pw->pw_name;
1533+}
252945ef 1534+/****** EXTRAS -- END ******/
5ca9317d 1535+
0f6733d8
WD
1536+/*
1537+ This file wraps all differing system ACL interfaces into a consistent
1538+ one based on the POSIX interface. It also returns the correct errors
1539+ for older UNIX systems that don't support ACLs.
fa26e11c 1540+
0f6733d8 1541+ The interfaces that each ACL implementation must support are as follows :
fa26e11c 1542+
0f6733d8
WD
1543+ int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1544+ int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1545+ int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
1546+ void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1547+ SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1548+ SMB_ACL_T sys_acl_get_fd(int fd)
1549+ int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
1550+ int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
1551+ char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
1552+ SMB_ACL_T sys_acl_init( int count)
1553+ int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1554+ int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1555+ int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1556+ int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1557+ int sys_acl_valid( SMB_ACL_T theacl )
1558+ int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1559+ int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1560+ int sys_acl_delete_def_file(const char *path)
fa26e11c 1561+
0f6733d8
WD
1562+ This next one is not POSIX complient - but we *have* to have it !
1563+ More POSIX braindamage.
fa26e11c 1564+
0f6733d8 1565+ int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 1566+
0f6733d8
WD
1567+ The generic POSIX free is the following call. We split this into
1568+ several different free functions as we may need to add tag info
1569+ to structures when emulating the POSIX interface.
fa26e11c 1570+
0f6733d8 1571+ int sys_acl_free( void *obj_p)
fa26e11c 1572+
0f6733d8 1573+ The calls we actually use are :
fa26e11c 1574+
0f6733d8
WD
1575+ int sys_acl_free_text(char *text) - free acl_to_text
1576+ int sys_acl_free_acl(SMB_ACL_T posix_acl)
1577+ int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
fa26e11c 1578+
0f6733d8 1579+*/
fa26e11c 1580+
0f6733d8 1581+#if defined(HAVE_POSIX_ACLS)
fa26e11c 1582+
0f6733d8 1583+/* Identity mapping - easy. */
fa26e11c 1584+
0f6733d8 1585+int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 1586+{
0f6733d8 1587+ return acl_get_entry( the_acl, entry_id, entry_p);
fa26e11c
WD
1588+}
1589+
0f6733d8 1590+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c 1591+{
0f6733d8 1592+ return acl_get_tag_type( entry_d, tag_type_p);
fa26e11c
WD
1593+}
1594+
0f6733d8 1595+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c 1596+{
0f6733d8 1597+ return acl_get_permset( entry_d, permset_p);
fa26e11c
WD
1598+}
1599+
0f6733d8 1600+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 1601+{
0f6733d8 1602+ return acl_get_qualifier( entry_d);
fa26e11c
WD
1603+}
1604+
0f6733d8 1605+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 1606+{
0f6733d8 1607+ return acl_get_file( path_p, type);
fa26e11c
WD
1608+}
1609+
1610+SMB_ACL_T sys_acl_get_fd(int fd)
1611+{
1612+ return acl_get_fd(fd);
1613+}
1614+
1615+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1616+{
1617+ return acl_clear_perms(permset);
1618+}
1619+
0f6733d8 1620+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
1621+{
1622+ return acl_add_perm(permset, perm);
1623+}
1624+
0f6733d8 1625+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
1626+{
1627+#if defined(HAVE_ACL_GET_PERM_NP)
0f6733d8
WD
1628+ /*
1629+ * Required for TrustedBSD-based ACL implementations where
fa26e11c 1630+ * non-POSIX.1e functions are denoted by a _np (non-portable)
0f6733d8
WD
1631+ * suffix.
1632+ */
fa26e11c
WD
1633+ return acl_get_perm_np(permset, perm);
1634+#else
1635+ return acl_get_perm(permset, perm);
1636+#endif
1637+}
1638+
0f6733d8 1639+char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
fa26e11c 1640+{
0f6733d8 1641+ return acl_to_text( the_acl, plen);
fa26e11c
WD
1642+}
1643+
0f6733d8 1644+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
1645+{
1646+ return acl_init(count);
1647+}
1648+
0f6733d8 1649+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
1650+{
1651+ return acl_create_entry(pacl, pentry);
1652+}
1653+
0f6733d8 1654+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
1655+{
1656+ return acl_set_tag_type(entry, tagtype);
1657+}
1658+
0f6733d8 1659+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
1660+{
1661+ return acl_set_qualifier(entry, qual);
1662+}
1663+
0f6733d8 1664+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
1665+{
1666+ return acl_set_permset(entry, permset);
1667+}
1668+
0f6733d8 1669+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c
WD
1670+{
1671+ return acl_valid(theacl);
1672+}
1673+
1674+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1675+{
1676+ return acl_set_file(name, acltype, theacl);
1677+}
1678+
0f6733d8 1679+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
1680+{
1681+ return acl_set_fd(fd, theacl);
1682+}
1683+
1684+int sys_acl_delete_def_file(const char *name)
1685+{
1686+ return acl_delete_def_file(name);
1687+}
1688+
1689+int sys_acl_free_text(char *text)
1690+{
1691+ return acl_free(text);
1692+}
1693+
0f6733d8 1694+int sys_acl_free_acl(SMB_ACL_T the_acl)
fa26e11c
WD
1695+{
1696+ return acl_free(the_acl);
1697+}
1698+
1f839b40 1699+int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
1700+{
1701+ return acl_free(qual);
1702+}
1703+
1704+#elif defined(HAVE_TRU64_ACLS)
0f6733d8
WD
1705+/*
1706+ * The interface to DEC/Compaq Tru64 UNIX ACLs
fa26e11c
WD
1707+ * is based on Draft 13 of the POSIX spec which is
1708+ * slightly different from the Draft 16 interface.
0f6733d8 1709+ *
fa26e11c
WD
1710+ * Also, some of the permset manipulation functions
1711+ * such as acl_clear_perm() and acl_add_perm() appear
1712+ * to be broken on Tru64 so we have to manipulate
0f6733d8
WD
1713+ * the permission bits in the permset directly.
1714+ */
1715+int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 1716+{
0f6733d8 1717+ SMB_ACL_ENTRY_T entry;
fa26e11c
WD
1718+
1719+ if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
1720+ return -1;
1721+ }
1722+
1723+ errno = 0;
1724+ if ((entry = acl_get_entry(the_acl)) != NULL) {
1725+ *entry_p = entry;
1726+ return 1;
1727+ }
1728+
1729+ return errno ? -1 : 0;
1730+}
1731+
0f6733d8 1732+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c 1733+{
0f6733d8 1734+ return acl_get_tag_type( entry_d, tag_type_p);
fa26e11c
WD
1735+}
1736+
0f6733d8 1737+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c 1738+{
0f6733d8 1739+ return acl_get_permset( entry_d, permset_p);
fa26e11c
WD
1740+}
1741+
0f6733d8 1742+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 1743+{
0f6733d8 1744+ return acl_get_qualifier( entry_d);
fa26e11c
WD
1745+}
1746+
0f6733d8 1747+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c
WD
1748+{
1749+ return acl_get_file((char *)path_p, type);
1750+}
1751+
0f6733d8 1752+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c
WD
1753+{
1754+ return acl_get_fd(fd, ACL_TYPE_ACCESS);
1755+}
1756+
0f6733d8 1757+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
fa26e11c 1758+{
0f6733d8 1759+ *permset = 0; /* acl_clear_perm() is broken on Tru64 */
fa26e11c
WD
1760+
1761+ return 0;
1762+}
1763+
0f6733d8 1764+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
1765+{
1766+ if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
1767+ errno = EINVAL;
1768+ return -1;
1769+ }
1770+
0f6733d8 1771+ *permset |= perm; /* acl_add_perm() is broken on Tru64 */
fa26e11c
WD
1772+
1773+ return 0;
1774+}
1775+
0f6733d8 1776+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
1777+{
1778+ return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
1779+}
1780+
0f6733d8 1781+char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
fa26e11c 1782+{
0f6733d8 1783+ return acl_to_text( the_acl, plen);
fa26e11c
WD
1784+}
1785+
0f6733d8 1786+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
1787+{
1788+ return acl_init(count);
1789+}
1790+
0f6733d8 1791+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
1792+{
1793+ SMB_ACL_ENTRY_T entry;
1794+
1795+ if ((entry = acl_create_entry(pacl)) == NULL) {
1796+ return -1;
1797+ }
1798+
1799+ *pentry = entry;
1800+ return 0;
1801+}
1802+
0f6733d8 1803+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
1804+{
1805+ return acl_set_tag_type(entry, tagtype);
1806+}
1807+
0f6733d8 1808+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
1809+{
1810+ return acl_set_qualifier(entry, qual);
1811+}
1812+
0f6733d8 1813+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
1814+{
1815+ return acl_set_permset(entry, permset);
1816+}
1817+
0f6733d8 1818+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c 1819+{
0f6733d8 1820+ acl_entry_t entry;
fa26e11c
WD
1821+
1822+ return acl_valid(theacl, &entry);
1823+}
1824+
0f6733d8 1825+int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
fa26e11c
WD
1826+{
1827+ return acl_set_file((char *)name, acltype, theacl);
1828+}
1829+
0f6733d8 1830+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
1831+{
1832+ return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
1833+}
1834+
0f6733d8 1835+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
1836+{
1837+ return acl_delete_def_file((char *)name);
1838+}
1839+
0f6733d8 1840+int sys_acl_free_text(char *text)
fa26e11c 1841+{
0f6733d8
WD
1842+ /*
1843+ * (void) cast and explicit return 0 are for DEC UNIX
1844+ * which just #defines acl_free_text() to be free()
1845+ */
fa26e11c
WD
1846+ (void) acl_free_text(text);
1847+ return 0;
1848+}
1849+
0f6733d8 1850+int sys_acl_free_acl(SMB_ACL_T the_acl)
fa26e11c
WD
1851+{
1852+ return acl_free(the_acl);
1853+}
1854+
0f6733d8 1855+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
1856+{
1857+ return acl_free_qualifier(qual, tagtype);
1858+}
1859+
1860+#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
1861+
0f6733d8
WD
1862+/*
1863+ * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
1864+ * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
1865+ */
fa26e11c 1866+
0f6733d8
WD
1867+/*
1868+ * Note that while this code implements sufficient functionality
fa26e11c
WD
1869+ * to support the sys_acl_* interfaces it does not provide all
1870+ * of the semantics of the POSIX ACL interfaces.
1871+ *
1872+ * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
1873+ * from a call to sys_acl_get_entry() should not be assumed to be
1874+ * valid after calling any of the following functions, which may
1875+ * reorder the entries in the ACL.
1876+ *
1877+ * sys_acl_valid()
1878+ * sys_acl_set_file()
1879+ * sys_acl_set_fd()
1880+ */
1881+
0f6733d8
WD
1882+/*
1883+ * The only difference between Solaris and UnixWare / OpenUNIX is
1884+ * that the #defines for the ACL operations have different names
1885+ */
fa26e11c
WD
1886+#if defined(HAVE_UNIXWARE_ACLS)
1887+
0f6733d8
WD
1888+#define SETACL ACL_SET
1889+#define GETACL ACL_GET
1890+#define GETACLCNT ACL_CNT
fa26e11c
WD
1891+
1892+#endif
1893+
1894+
0f6733d8 1895+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
1896+{
1897+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
1898+ errno = EINVAL;
1899+ return -1;
1900+ }
1901+
1902+ if (entry_p == NULL) {
1903+ errno = EINVAL;
1904+ return -1;
1905+ }
1906+
1907+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
1908+ acl_d->next = 0;
1909+ }
1910+
1911+ if (acl_d->next < 0) {
1912+ errno = EINVAL;
1913+ return -1;
1914+ }
1915+
1916+ if (acl_d->next >= acl_d->count) {
1917+ return 0;
1918+ }
1919+
1920+ *entry_p = &acl_d->acl[acl_d->next++];
1921+
1922+ return 1;
1923+}
1924+
0f6733d8 1925+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
1926+{
1927+ *type_p = entry_d->a_type;
1928+
1929+ return 0;
1930+}
1931+
0f6733d8 1932+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
1933+{
1934+ *permset_p = &entry_d->a_perm;
1935+
1936+ return 0;
1937+}
1938+
0f6733d8 1939+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
1940+{
1941+ if (entry_d->a_type != SMB_ACL_USER
1942+ && entry_d->a_type != SMB_ACL_GROUP) {
1943+ errno = EINVAL;
1944+ return NULL;
1945+ }
1946+
1947+ return &entry_d->a_id;
1948+}
1949+
0f6733d8
WD
1950+/*
1951+ * There is no way of knowing what size the ACL returned by
fa26e11c
WD
1952+ * GETACL will be unless you first call GETACLCNT which means
1953+ * making an additional system call.
1954+ *
1955+ * In the hope of avoiding the cost of the additional system
1956+ * call in most cases, we initially allocate enough space for
1957+ * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
1958+ * be too small then we use GETACLCNT to find out the actual
0f6733d8
WD
1959+ * size, reallocate the ACL buffer, and then call GETACL again.
1960+ */
fa26e11c 1961+
0f6733d8 1962+#define INITIAL_ACL_SIZE 16
fa26e11c 1963+
0f6733d8 1964+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 1965+{
0f6733d8
WD
1966+ SMB_ACL_T acl_d;
1967+ int count; /* # of ACL entries allocated */
1968+ int naccess; /* # of access ACL entries */
1969+ int ndefault; /* # of default ACL entries */
fa26e11c
WD
1970+
1971+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
1972+ errno = EINVAL;
1973+ return NULL;
1974+ }
1975+
1976+ count = INITIAL_ACL_SIZE;
1977+ if ((acl_d = sys_acl_init(count)) == NULL) {
1978+ return NULL;
1979+ }
1980+
0f6733d8
WD
1981+ /*
1982+ * If there isn't enough space for the ACL entries we use
fa26e11c
WD
1983+ * GETACLCNT to determine the actual number of ACL entries
1984+ * reallocate and try again. This is in a loop because it
1985+ * is possible that someone else could modify the ACL and
1986+ * increase the number of entries between the call to
0f6733d8
WD
1987+ * GETACLCNT and the call to GETACL.
1988+ */
fa26e11c
WD
1989+ while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
1990+ && errno == ENOSPC) {
1991+
1992+ sys_acl_free_acl(acl_d);
1993+
1994+ if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
1995+ return NULL;
1996+ }
1997+
1998+ if ((acl_d = sys_acl_init(count)) == NULL) {
1999+ return NULL;
2000+ }
2001+ }
2002+
2003+ if (count < 0) {
2004+ sys_acl_free_acl(acl_d);
2005+ return NULL;
2006+ }
2007+
0f6733d8
WD
2008+ /*
2009+ * calculate the number of access and default ACL entries
fa26e11c
WD
2010+ *
2011+ * Note: we assume that the acl() system call returned a
2012+ * well formed ACL which is sorted so that all of the
0f6733d8
WD
2013+ * access ACL entries preceed any default ACL entries
2014+ */
fa26e11c
WD
2015+ for (naccess = 0; naccess < count; naccess++) {
2016+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2017+ break;
2018+ }
2019+ ndefault = count - naccess;
0f6733d8
WD
2020+
2021+ /*
2022+ * if the caller wants the default ACL we have to copy
fa26e11c 2023+ * the entries down to the start of the acl[] buffer
0f6733d8
WD
2024+ * and mask out the ACL_DEFAULT flag from the type field
2025+ */
fa26e11c 2026+ if (type == SMB_ACL_TYPE_DEFAULT) {
0f6733d8 2027+ int i, j;
fa26e11c
WD
2028+
2029+ for (i = 0, j = naccess; i < ndefault; i++, j++) {
2030+ acl_d->acl[i] = acl_d->acl[j];
2031+ acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2032+ }
2033+
2034+ acl_d->count = ndefault;
2035+ } else {
2036+ acl_d->count = naccess;
2037+ }
2038+
2039+ return acl_d;
2040+}
2041+
0f6733d8 2042+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 2043+{
0f6733d8
WD
2044+ SMB_ACL_T acl_d;
2045+ int count; /* # of ACL entries allocated */
2046+ int naccess; /* # of access ACL entries */
fa26e11c
WD
2047+
2048+ count = INITIAL_ACL_SIZE;
2049+ if ((acl_d = sys_acl_init(count)) == NULL) {
2050+ return NULL;
2051+ }
2052+
2053+ while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2054+ && errno == ENOSPC) {
2055+
2056+ sys_acl_free_acl(acl_d);
2057+
2058+ if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2059+ return NULL;
2060+ }
2061+
2062+ if ((acl_d = sys_acl_init(count)) == NULL) {
2063+ return NULL;
2064+ }
2065+ }
2066+
2067+ if (count < 0) {
2068+ sys_acl_free_acl(acl_d);
2069+ return NULL;
2070+ }
2071+
0f6733d8
WD
2072+ /*
2073+ * calculate the number of access ACL entries
2074+ */
fa26e11c
WD
2075+ for (naccess = 0; naccess < count; naccess++) {
2076+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2077+ break;
2078+ }
0f6733d8 2079+
fa26e11c
WD
2080+ acl_d->count = naccess;
2081+
2082+ return acl_d;
2083+}
2084+
0f6733d8 2085+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2086+{
2087+ *permset_d = 0;
2088+
2089+ return 0;
2090+}
2091+
0f6733d8 2092+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2093+{
2094+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2095+ && perm != SMB_ACL_EXECUTE) {
2096+ errno = EINVAL;
2097+ return -1;
2098+ }
2099+
2100+ if (permset_d == NULL) {
2101+ errno = EINVAL;
2102+ return -1;
2103+ }
2104+
2105+ *permset_d |= perm;
2106+
2107+ return 0;
2108+}
2109+
0f6733d8 2110+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2111+{
2112+ return *permset_d & perm;
2113+}
2114+
0f6733d8 2115+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c 2116+{
0f6733d8
WD
2117+ int i;
2118+ int len, maxlen;
2119+ char *text;
fa26e11c 2120+
0f6733d8
WD
2121+ /*
2122+ * use an initial estimate of 20 bytes per ACL entry
fa26e11c 2123+ * when allocating memory for the text representation
0f6733d8
WD
2124+ * of the ACL
2125+ */
2126+ len = 0;
2127+ maxlen = 20 * acl_d->count;
252945ef 2128+ if ((text = SMB_MALLOC(maxlen)) == NULL) {
fa26e11c
WD
2129+ errno = ENOMEM;
2130+ return NULL;
2131+ }
2132+
2133+ for (i = 0; i < acl_d->count; i++) {
0f6733d8
WD
2134+ struct acl *ap = &acl_d->acl[i];
2135+ struct passwd *pw;
2136+ struct group *gr;
2137+ char tagbuf[12];
2138+ char idbuf[12];
2139+ char *tag;
2140+ char *id = "";
2141+ char perms[4];
2142+ int nbytes;
fa26e11c
WD
2143+
2144+ switch (ap->a_type) {
0f6733d8
WD
2145+ /*
2146+ * for debugging purposes it's probably more
fa26e11c 2147+ * useful to dump unknown tag types rather
0f6733d8
WD
2148+ * than just returning an error
2149+ */
fa26e11c 2150+ default:
0f6733d8 2151+ slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
fa26e11c
WD
2152+ ap->a_type);
2153+ tag = tagbuf;
0f6733d8 2154+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2155+ (long)ap->a_id);
2156+ id = idbuf;
2157+ break;
2158+
2159+ case SMB_ACL_USER:
5ca9317d 2160+ id = uidtoname(ap->a_id);
fa26e11c
WD
2161+ case SMB_ACL_USER_OBJ:
2162+ tag = "user";
2163+ break;
2164+
2165+ case SMB_ACL_GROUP:
2166+ if ((gr = getgrgid(ap->a_id)) == NULL) {
0f6733d8 2167+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2168+ (long)ap->a_id);
2169+ id = idbuf;
2170+ } else {
2171+ id = gr->gr_name;
2172+ }
2173+ case SMB_ACL_GROUP_OBJ:
2174+ tag = "group";
2175+ break;
2176+
2177+ case SMB_ACL_OTHER:
2178+ tag = "other";
2179+ break;
2180+
2181+ case SMB_ACL_MASK:
2182+ tag = "mask";
2183+ break;
2184+
2185+ }
2186+
2187+ perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2188+ perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2189+ perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2190+ perms[3] = '\0';
2191+
2192+ /* <tag> : <qualifier> : rwx \n \0 */
2193+ nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2194+
0f6733d8
WD
2195+ /*
2196+ * If this entry would overflow the buffer
fa26e11c
WD
2197+ * allocate enough additional memory for this
2198+ * entry and an estimate of another 20 bytes
0f6733d8
WD
2199+ * for each entry still to be processed
2200+ */
fa26e11c
WD
2201+ if ((len + nbytes) > maxlen) {
2202+ char *oldtext = text;
2203+
2204+ maxlen += nbytes + 20 * (acl_d->count - i);
2205+
252945ef 2206+ if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
0f6733d8 2207+ SAFE_FREE(oldtext);
fa26e11c
WD
2208+ errno = ENOMEM;
2209+ return NULL;
2210+ }
2211+ }
2212+
0f6733d8 2213+ slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
fa26e11c
WD
2214+ len += nbytes - 1;
2215+ }
2216+
2217+ if (len_p)
2218+ *len_p = len;
2219+
2220+ return text;
2221+}
2222+
0f6733d8 2223+SMB_ACL_T sys_acl_init(int count)
fa26e11c 2224+{
0f6733d8 2225+ SMB_ACL_T a;
fa26e11c
WD
2226+
2227+ if (count < 0) {
2228+ errno = EINVAL;
2229+ return NULL;
2230+ }
2231+
0f6733d8
WD
2232+ /*
2233+ * note that since the definition of the structure pointed
fa26e11c
WD
2234+ * to by the SMB_ACL_T includes the first element of the
2235+ * acl[] array, this actually allocates an ACL with room
0f6733d8
WD
2236+ * for (count+1) entries
2237+ */
252945ef 2238+ if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
fa26e11c
WD
2239+ errno = ENOMEM;
2240+ return NULL;
2241+ }
2242+
2243+ a->size = count + 1;
2244+ a->count = 0;
2245+ a->next = -1;
2246+
2247+ return a;
2248+}
2249+
2250+
0f6733d8 2251+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 2252+{
0f6733d8
WD
2253+ SMB_ACL_T acl_d;
2254+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
2255+
2256+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2257+ errno = EINVAL;
2258+ return -1;
2259+ }
2260+
2261+ if (acl_d->count >= acl_d->size) {
2262+ errno = ENOSPC;
2263+ return -1;
2264+ }
2265+
0f6733d8
WD
2266+ entry_d = &acl_d->acl[acl_d->count++];
2267+ entry_d->a_type = 0;
2268+ entry_d->a_id = -1;
2269+ entry_d->a_perm = 0;
2270+ *entry_p = entry_d;
fa26e11c
WD
2271+
2272+ return 0;
2273+}
2274+
0f6733d8 2275+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
2276+{
2277+ switch (tag_type) {
2278+ case SMB_ACL_USER:
2279+ case SMB_ACL_USER_OBJ:
2280+ case SMB_ACL_GROUP:
2281+ case SMB_ACL_GROUP_OBJ:
2282+ case SMB_ACL_OTHER:
2283+ case SMB_ACL_MASK:
2284+ entry_d->a_type = tag_type;
2285+ break;
2286+ default:
2287+ errno = EINVAL;
2288+ return -1;
2289+ }
2290+
2291+ return 0;
2292+}
2293+
0f6733d8 2294+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
2295+{
2296+ if (entry_d->a_type != SMB_ACL_GROUP
2297+ && entry_d->a_type != SMB_ACL_USER) {
2298+ errno = EINVAL;
2299+ return -1;
2300+ }
2301+
2302+ entry_d->a_id = *((id_t *)qual_p);
2303+
2304+ return 0;
2305+}
2306+
0f6733d8 2307+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2308+{
2309+ if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2310+ return EINVAL;
2311+ }
2312+
2313+ entry_d->a_perm = *permset_d;
2314+
2315+ return 0;
2316+}
2317+
0f6733d8
WD
2318+/*
2319+ * sort the ACL and check it for validity
fa26e11c 2320+ *
0f6733d8 2321+ * if it's a minimal ACL with only 4 entries then we
fa26e11c
WD
2322+ * need to recalculate the mask permissions to make
2323+ * sure that they are the same as the GROUP_OBJ
2324+ * permissions as required by the UnixWare acl() system call.
2325+ *
0f6733d8 2326+ * (note: since POSIX allows minimal ACLs which only contain
fa26e11c
WD
2327+ * 3 entries - ie there is no mask entry - we should, in theory,
2328+ * check for this and add a mask entry if necessary - however
2329+ * we "know" that the caller of this interface always specifies
2330+ * a mask so, in practice "this never happens" (tm) - if it *does*
2331+ * happen aclsort() will fail and return an error and someone will
0f6733d8
WD
2332+ * have to fix it ...)
2333+ */
fa26e11c
WD
2334+
2335+static int acl_sort(SMB_ACL_T acl_d)
2336+{
2337+ int fixmask = (acl_d->count <= 4);
2338+
2339+ if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
2340+ errno = EINVAL;
2341+ return -1;
2342+ }
2343+ return 0;
2344+}
0f6733d8
WD
2345+
2346+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
2347+{
2348+ return acl_sort(acl_d);
2349+}
2350+
0f6733d8 2351+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c 2352+{
0f6733d8
WD
2353+ struct stat s;
2354+ struct acl *acl_p;
2355+ int acl_count;
2356+ struct acl *acl_buf = NULL;
2357+ int ret;
fa26e11c
WD
2358+
2359+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2360+ errno = EINVAL;
2361+ return -1;
2362+ }
2363+
2364+ if (acl_sort(acl_d) != 0) {
2365+ return -1;
2366+ }
2367+
0f6733d8
WD
2368+ acl_p = &acl_d->acl[0];
2369+ acl_count = acl_d->count;
fa26e11c 2370+
0f6733d8
WD
2371+ /*
2372+ * if it's a directory there is extra work to do
2373+ * since the acl() system call will replace both
2374+ * the access ACLs and the default ACLs (if any)
2375+ */
fa26e11c
WD
2376+ if (stat(name, &s) != 0) {
2377+ return -1;
2378+ }
2379+ if (S_ISDIR(s.st_mode)) {
0f6733d8
WD
2380+ SMB_ACL_T acc_acl;
2381+ SMB_ACL_T def_acl;
2382+ SMB_ACL_T tmp_acl;
2383+ int i;
fa26e11c
WD
2384+
2385+ if (type == SMB_ACL_TYPE_ACCESS) {
2386+ acc_acl = acl_d;
2387+ def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
2388+
2389+ } else {
2390+ def_acl = acl_d;
2391+ acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
2392+ }
2393+
2394+ if (tmp_acl == NULL) {
2395+ return -1;
2396+ }
2397+
0f6733d8
WD
2398+ /*
2399+ * allocate a temporary buffer for the complete ACL
2400+ */
fa26e11c 2401+ acl_count = acc_acl->count + def_acl->count;
252945ef 2402+ acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
fa26e11c
WD
2403+
2404+ if (acl_buf == NULL) {
2405+ sys_acl_free_acl(tmp_acl);
2406+ errno = ENOMEM;
2407+ return -1;
2408+ }
2409+
0f6733d8
WD
2410+ /*
2411+ * copy the access control and default entries into the buffer
2412+ */
fa26e11c 2413+ memcpy(&acl_buf[0], &acc_acl->acl[0],
0f6733d8 2414+ acc_acl->count * sizeof(acl_buf[0]));
fa26e11c
WD
2415+
2416+ memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
0f6733d8 2417+ def_acl->count * sizeof(acl_buf[0]));
fa26e11c 2418+
0f6733d8
WD
2419+ /*
2420+ * set the ACL_DEFAULT flag on the default entries
2421+ */
fa26e11c
WD
2422+ for (i = acc_acl->count; i < acl_count; i++) {
2423+ acl_buf[i].a_type |= ACL_DEFAULT;
2424+ }
2425+
2426+ sys_acl_free_acl(tmp_acl);
2427+
2428+ } else if (type != SMB_ACL_TYPE_ACCESS) {
2429+ errno = EINVAL;
2430+ return -1;
2431+ }
2432+
2433+ ret = acl(name, SETACL, acl_count, acl_p);
2434+
0f6733d8 2435+ SAFE_FREE(acl_buf);
fa26e11c
WD
2436+
2437+ return ret;
2438+}
2439+
0f6733d8 2440+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c
WD
2441+{
2442+ if (acl_sort(acl_d) != 0) {
2443+ return -1;
2444+ }
2445+
2446+ return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
2447+}
2448+
0f6733d8 2449+int sys_acl_delete_def_file(const char *path)
fa26e11c 2450+{
0f6733d8
WD
2451+ SMB_ACL_T acl_d;
2452+ int ret;
fa26e11c 2453+
0f6733d8
WD
2454+ /*
2455+ * fetching the access ACL and rewriting it has
2456+ * the effect of deleting the default ACL
2457+ */
fa26e11c
WD
2458+ if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
2459+ return -1;
2460+ }
2461+
2462+ ret = acl(path, SETACL, acl_d->count, acl_d->acl);
2463+
2464+ sys_acl_free_acl(acl_d);
0f6733d8 2465+
fa26e11c
WD
2466+ return ret;
2467+}
2468+
0f6733d8 2469+int sys_acl_free_text(char *text)
fa26e11c 2470+{
0f6733d8 2471+ SAFE_FREE(text);
fa26e11c
WD
2472+ return 0;
2473+}
2474+
0f6733d8 2475+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c 2476+{
0f6733d8 2477+ SAFE_FREE(acl_d);
fa26e11c
WD
2478+ return 0;
2479+}
2480+
0f6733d8 2481+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
2482+{
2483+ return 0;
2484+}
2485+
2486+#elif defined(HAVE_HPUX_ACLS)
2487+#include <dl.h>
2488+
0f6733d8
WD
2489+/*
2490+ * Based on the Solaris/SCO code - with modifications.
2491+ */
fa26e11c 2492+
0f6733d8
WD
2493+/*
2494+ * Note that while this code implements sufficient functionality
fa26e11c
WD
2495+ * to support the sys_acl_* interfaces it does not provide all
2496+ * of the semantics of the POSIX ACL interfaces.
2497+ *
2498+ * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2499+ * from a call to sys_acl_get_entry() should not be assumed to be
2500+ * valid after calling any of the following functions, which may
2501+ * reorder the entries in the ACL.
2502+ *
2503+ * sys_acl_valid()
2504+ * sys_acl_set_file()
2505+ * sys_acl_set_fd()
2506+ */
2507+
0f6733d8
WD
2508+/* This checks if the POSIX ACL system call is defined */
2509+/* which basically corresponds to whether JFS 3.3 or */
2510+/* higher is installed. If acl() was called when it */
2511+/* isn't defined, it causes the process to core dump */
2512+/* so it is important to check this and avoid acl() */
2513+/* calls if it isn't there. */
fa26e11c
WD
2514+
2515+static BOOL hpux_acl_call_presence(void)
2516+{
2517+
2518+ shl_t handle = NULL;
2519+ void *value;
2520+ int ret_val=0;
2521+ static BOOL already_checked=0;
2522+
0f6733d8 2523+ if(already_checked)
fa26e11c
WD
2524+ return True;
2525+
2526+
2527+ ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
2528+
0f6733d8 2529+ if(ret_val != 0) {
fa26e11c
WD
2530+ DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
2531+ ret_val, errno, strerror(errno)));
2532+ DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
2533+ return False;
2534+ }
2535+
2536+ DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
2537+
2538+ already_checked = True;
2539+ return True;
2540+}
2541+
0f6733d8 2542+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
2543+{
2544+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2545+ errno = EINVAL;
2546+ return -1;
2547+ }
2548+
2549+ if (entry_p == NULL) {
2550+ errno = EINVAL;
2551+ return -1;
2552+ }
2553+
2554+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
2555+ acl_d->next = 0;
2556+ }
2557+
2558+ if (acl_d->next < 0) {
2559+ errno = EINVAL;
2560+ return -1;
2561+ }
2562+
2563+ if (acl_d->next >= acl_d->count) {
2564+ return 0;
2565+ }
2566+
2567+ *entry_p = &acl_d->acl[acl_d->next++];
2568+
2569+ return 1;
2570+}
2571+
0f6733d8 2572+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
2573+{
2574+ *type_p = entry_d->a_type;
2575+
2576+ return 0;
2577+}
2578+
0f6733d8 2579+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
2580+{
2581+ *permset_p = &entry_d->a_perm;
2582+
2583+ return 0;
2584+}
2585+
0f6733d8 2586+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
2587+{
2588+ if (entry_d->a_type != SMB_ACL_USER
2589+ && entry_d->a_type != SMB_ACL_GROUP) {
2590+ errno = EINVAL;
2591+ return NULL;
2592+ }
2593+
2594+ return &entry_d->a_id;
2595+}
2596+
0f6733d8
WD
2597+/*
2598+ * There is no way of knowing what size the ACL returned by
fa26e11c
WD
2599+ * ACL_GET will be unless you first call ACL_CNT which means
2600+ * making an additional system call.
2601+ *
2602+ * In the hope of avoiding the cost of the additional system
2603+ * call in most cases, we initially allocate enough space for
2604+ * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2605+ * be too small then we use ACL_CNT to find out the actual
2606+ * size, reallocate the ACL buffer, and then call ACL_GET again.
2607+ */
2608+
0f6733d8 2609+#define INITIAL_ACL_SIZE 16
fa26e11c 2610+
0f6733d8 2611+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 2612+{
0f6733d8
WD
2613+ SMB_ACL_T acl_d;
2614+ int count; /* # of ACL entries allocated */
2615+ int naccess; /* # of access ACL entries */
2616+ int ndefault; /* # of default ACL entries */
fa26e11c 2617+
0f6733d8
WD
2618+ if(hpux_acl_call_presence() == False) {
2619+ /* Looks like we don't have the acl() system call on HPUX.
2620+ * May be the system doesn't have the latest version of JFS.
2621+ */
2622+ return NULL;
fa26e11c
WD
2623+ }
2624+
2625+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2626+ errno = EINVAL;
2627+ return NULL;
2628+ }
2629+
2630+ count = INITIAL_ACL_SIZE;
2631+ if ((acl_d = sys_acl_init(count)) == NULL) {
2632+ return NULL;
2633+ }
2634+
0f6733d8
WD
2635+ /*
2636+ * If there isn't enough space for the ACL entries we use
fa26e11c
WD
2637+ * ACL_CNT to determine the actual number of ACL entries
2638+ * reallocate and try again. This is in a loop because it
2639+ * is possible that someone else could modify the ACL and
2640+ * increase the number of entries between the call to
0f6733d8
WD
2641+ * ACL_CNT and the call to ACL_GET.
2642+ */
fa26e11c
WD
2643+ while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
2644+
2645+ sys_acl_free_acl(acl_d);
2646+
2647+ if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
2648+ return NULL;
2649+ }
2650+
2651+ if ((acl_d = sys_acl_init(count)) == NULL) {
2652+ return NULL;
2653+ }
2654+ }
2655+
2656+ if (count < 0) {
2657+ sys_acl_free_acl(acl_d);
2658+ return NULL;
2659+ }
2660+
0f6733d8
WD
2661+ /*
2662+ * calculate the number of access and default ACL entries
fa26e11c
WD
2663+ *
2664+ * Note: we assume that the acl() system call returned a
2665+ * well formed ACL which is sorted so that all of the
0f6733d8
WD
2666+ * access ACL entries preceed any default ACL entries
2667+ */
fa26e11c
WD
2668+ for (naccess = 0; naccess < count; naccess++) {
2669+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2670+ break;
2671+ }
2672+ ndefault = count - naccess;
0f6733d8
WD
2673+
2674+ /*
2675+ * if the caller wants the default ACL we have to copy
fa26e11c 2676+ * the entries down to the start of the acl[] buffer
0f6733d8
WD
2677+ * and mask out the ACL_DEFAULT flag from the type field
2678+ */
fa26e11c 2679+ if (type == SMB_ACL_TYPE_DEFAULT) {
0f6733d8 2680+ int i, j;
fa26e11c
WD
2681+
2682+ for (i = 0, j = naccess; i < ndefault; i++, j++) {
2683+ acl_d->acl[i] = acl_d->acl[j];
2684+ acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2685+ }
2686+
2687+ acl_d->count = ndefault;
2688+ } else {
2689+ acl_d->count = naccess;
2690+ }
2691+
2692+ return acl_d;
2693+}
2694+
0f6733d8 2695+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 2696+{
0f6733d8
WD
2697+ /*
2698+ * HPUX doesn't have the facl call. Fake it using the path.... JRA.
2699+ */
fa26e11c
WD
2700+
2701+ files_struct *fsp = file_find_fd(fd);
2702+
2703+ if (fsp == NULL) {
2704+ errno = EBADF;
2705+ return NULL;
2706+ }
2707+
0f6733d8
WD
2708+ /*
2709+ * We know we're in the same conn context. So we
2710+ * can use the relative path.
2711+ */
fa26e11c 2712+
5ca9317d 2713+ return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
fa26e11c
WD
2714+}
2715+
0f6733d8 2716+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2717+{
2718+ *permset_d = 0;
2719+
2720+ return 0;
2721+}
2722+
0f6733d8 2723+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2724+{
2725+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2726+ && perm != SMB_ACL_EXECUTE) {
2727+ errno = EINVAL;
2728+ return -1;
2729+ }
2730+
2731+ if (permset_d == NULL) {
2732+ errno = EINVAL;
2733+ return -1;
2734+ }
2735+
2736+ *permset_d |= perm;
2737+
2738+ return 0;
2739+}
2740+
0f6733d8 2741+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2742+{
2743+ return *permset_d & perm;
2744+}
2745+
0f6733d8 2746+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c 2747+{
0f6733d8
WD
2748+ int i;
2749+ int len, maxlen;
2750+ char *text;
fa26e11c 2751+
0f6733d8
WD
2752+ /*
2753+ * use an initial estimate of 20 bytes per ACL entry
2754+ * when allocating memory for the text representation
2755+ * of the ACL
2756+ */
2757+ len = 0;
2758+ maxlen = 20 * acl_d->count;
252945ef 2759+ if ((text = SMB_MALLOC(maxlen)) == NULL) {
fa26e11c
WD
2760+ errno = ENOMEM;
2761+ return NULL;
2762+ }
2763+
2764+ for (i = 0; i < acl_d->count; i++) {
0f6733d8
WD
2765+ struct acl *ap = &acl_d->acl[i];
2766+ struct passwd *pw;
2767+ struct group *gr;
2768+ char tagbuf[12];
2769+ char idbuf[12];
2770+ char *tag;
2771+ char *id = "";
2772+ char perms[4];
2773+ int nbytes;
fa26e11c
WD
2774+
2775+ switch (ap->a_type) {
0f6733d8
WD
2776+ /*
2777+ * for debugging purposes it's probably more
fa26e11c 2778+ * useful to dump unknown tag types rather
0f6733d8
WD
2779+ * than just returning an error
2780+ */
fa26e11c 2781+ default:
0f6733d8 2782+ slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
fa26e11c
WD
2783+ ap->a_type);
2784+ tag = tagbuf;
0f6733d8 2785+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2786+ (long)ap->a_id);
2787+ id = idbuf;
2788+ break;
2789+
2790+ case SMB_ACL_USER:
5ca9317d 2791+ id = uidtoname(ap->a_id);
fa26e11c
WD
2792+ case SMB_ACL_USER_OBJ:
2793+ tag = "user";
2794+ break;
2795+
2796+ case SMB_ACL_GROUP:
2797+ if ((gr = getgrgid(ap->a_id)) == NULL) {
0f6733d8 2798+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2799+ (long)ap->a_id);
2800+ id = idbuf;
2801+ } else {
2802+ id = gr->gr_name;
2803+ }
2804+ case SMB_ACL_GROUP_OBJ:
2805+ tag = "group";
2806+ break;
2807+
2808+ case SMB_ACL_OTHER:
2809+ tag = "other";
2810+ break;
2811+
2812+ case SMB_ACL_MASK:
2813+ tag = "mask";
2814+ break;
2815+
2816+ }
2817+
2818+ perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2819+ perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2820+ perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2821+ perms[3] = '\0';
2822+
2823+ /* <tag> : <qualifier> : rwx \n \0 */
2824+ nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2825+
0f6733d8
WD
2826+ /*
2827+ * If this entry would overflow the buffer
fa26e11c
WD
2828+ * allocate enough additional memory for this
2829+ * entry and an estimate of another 20 bytes
0f6733d8
WD
2830+ * for each entry still to be processed
2831+ */
fa26e11c
WD
2832+ if ((len + nbytes) > maxlen) {
2833+ char *oldtext = text;
2834+
2835+ maxlen += nbytes + 20 * (acl_d->count - i);
2836+
252945ef 2837+ if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
fa26e11c
WD
2838+ free(oldtext);
2839+ errno = ENOMEM;
2840+ return NULL;
2841+ }
2842+ }
2843+
0f6733d8 2844+ slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
fa26e11c
WD
2845+ len += nbytes - 1;
2846+ }
2847+
2848+ if (len_p)
2849+ *len_p = len;
2850+
2851+ return text;
2852+}
2853+
0f6733d8 2854+SMB_ACL_T sys_acl_init(int count)
fa26e11c 2855+{
0f6733d8 2856+ SMB_ACL_T a;
fa26e11c
WD
2857+
2858+ if (count < 0) {
2859+ errno = EINVAL;
2860+ return NULL;
2861+ }
2862+
0f6733d8
WD
2863+ /*
2864+ * note that since the definition of the structure pointed
fa26e11c
WD
2865+ * to by the SMB_ACL_T includes the first element of the
2866+ * acl[] array, this actually allocates an ACL with room
0f6733d8
WD
2867+ * for (count+1) entries
2868+ */
252945ef 2869+ if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
fa26e11c
WD
2870+ errno = ENOMEM;
2871+ return NULL;
2872+ }
2873+
2874+ a->size = count + 1;
2875+ a->count = 0;
2876+ a->next = -1;
2877+
2878+ return a;
2879+}
2880+
2881+
0f6733d8 2882+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 2883+{
0f6733d8
WD
2884+ SMB_ACL_T acl_d;
2885+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
2886+
2887+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2888+ errno = EINVAL;
2889+ return -1;
2890+ }
2891+
2892+ if (acl_d->count >= acl_d->size) {
2893+ errno = ENOSPC;
2894+ return -1;
2895+ }
2896+
0f6733d8
WD
2897+ entry_d = &acl_d->acl[acl_d->count++];
2898+ entry_d->a_type = 0;
2899+ entry_d->a_id = -1;
2900+ entry_d->a_perm = 0;
2901+ *entry_p = entry_d;
fa26e11c
WD
2902+
2903+ return 0;
2904+}
2905+
0f6733d8 2906+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
2907+{
2908+ switch (tag_type) {
2909+ case SMB_ACL_USER:
2910+ case SMB_ACL_USER_OBJ:
2911+ case SMB_ACL_GROUP:
2912+ case SMB_ACL_GROUP_OBJ:
2913+ case SMB_ACL_OTHER:
2914+ case SMB_ACL_MASK:
2915+ entry_d->a_type = tag_type;
2916+ break;
2917+ default:
2918+ errno = EINVAL;
2919+ return -1;
2920+ }
2921+
2922+ return 0;
2923+}
2924+
0f6733d8 2925+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
2926+{
2927+ if (entry_d->a_type != SMB_ACL_GROUP
2928+ && entry_d->a_type != SMB_ACL_USER) {
2929+ errno = EINVAL;
2930+ return -1;
2931+ }
2932+
2933+ entry_d->a_id = *((id_t *)qual_p);
2934+
2935+ return 0;
2936+}
2937+
0f6733d8 2938+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2939+{
2940+ if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2941+ return EINVAL;
2942+ }
2943+
2944+ entry_d->a_perm = *permset_d;
2945+
2946+ return 0;
2947+}
2948+
2949+/* Structure to capture the count for each type of ACE. */
2950+
2951+struct hpux_acl_types {
2952+ int n_user;
2953+ int n_def_user;
2954+ int n_user_obj;
2955+ int n_def_user_obj;
2956+
2957+ int n_group;
2958+ int n_def_group;
2959+ int n_group_obj;
2960+ int n_def_group_obj;
2961+
2962+ int n_other;
2963+ int n_other_obj;
2964+ int n_def_other_obj;
2965+
2966+ int n_class_obj;
2967+ int n_def_class_obj;
2968+
2969+ int n_illegal_obj;
2970+};
2971+
2972+/* count_obj:
2973+ * Counts the different number of objects in a given array of ACL
2974+ * structures.
2975+ * Inputs:
2976+ *
2977+ * acl_count - Count of ACLs in the array of ACL strucutres.
2978+ * aclp - Array of ACL structures.
2979+ * acl_type_count - Pointer to acl_types structure. Should already be
2980+ * allocated.
0f6733d8 2981+ * Output:
fa26e11c 2982+ *
0f6733d8 2983+ * acl_type_count - This structure is filled up with counts of various
fa26e11c
WD
2984+ * acl types.
2985+ */
2986+
2987+static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
2988+{
2989+ int i;
2990+
0f6733d8 2991+ memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
fa26e11c 2992+
0f6733d8
WD
2993+ for(i=0;i<acl_count;i++) {
2994+ switch(aclp[i].a_type) {
2995+ case USER:
fa26e11c
WD
2996+ acl_type_count->n_user++;
2997+ break;
0f6733d8 2998+ case USER_OBJ:
fa26e11c
WD
2999+ acl_type_count->n_user_obj++;
3000+ break;
0f6733d8 3001+ case DEF_USER_OBJ:
fa26e11c
WD
3002+ acl_type_count->n_def_user_obj++;
3003+ break;
0f6733d8 3004+ case GROUP:
fa26e11c
WD
3005+ acl_type_count->n_group++;
3006+ break;
0f6733d8 3007+ case GROUP_OBJ:
fa26e11c
WD
3008+ acl_type_count->n_group_obj++;
3009+ break;
0f6733d8 3010+ case DEF_GROUP_OBJ:
fa26e11c
WD
3011+ acl_type_count->n_def_group_obj++;
3012+ break;
0f6733d8 3013+ case OTHER_OBJ:
fa26e11c
WD
3014+ acl_type_count->n_other_obj++;
3015+ break;
0f6733d8 3016+ case DEF_OTHER_OBJ:
fa26e11c
WD
3017+ acl_type_count->n_def_other_obj++;
3018+ break;
3019+ case CLASS_OBJ:
3020+ acl_type_count->n_class_obj++;
3021+ break;
3022+ case DEF_CLASS_OBJ:
3023+ acl_type_count->n_def_class_obj++;
3024+ break;
3025+ case DEF_USER:
3026+ acl_type_count->n_def_user++;
3027+ break;
3028+ case DEF_GROUP:
3029+ acl_type_count->n_def_group++;
3030+ break;
0f6733d8 3031+ default:
fa26e11c
WD
3032+ acl_type_count->n_illegal_obj++;
3033+ break;
3034+ }
3035+ }
3036+}
3037+
0f6733d8 3038+/* swap_acl_entries: Swaps two ACL entries.
fa26e11c
WD
3039+ *
3040+ * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3041+ */
3042+
3043+static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3044+{
3045+ struct acl temp_acl;
3046+
3047+ temp_acl.a_type = aclp0->a_type;
3048+ temp_acl.a_id = aclp0->a_id;
3049+ temp_acl.a_perm = aclp0->a_perm;
3050+
3051+ aclp0->a_type = aclp1->a_type;
3052+ aclp0->a_id = aclp1->a_id;
3053+ aclp0->a_perm = aclp1->a_perm;
3054+
3055+ aclp1->a_type = temp_acl.a_type;
3056+ aclp1->a_id = temp_acl.a_id;
3057+ aclp1->a_perm = temp_acl.a_perm;
3058+}
3059+
3060+/* prohibited_duplicate_type
0f6733d8 3061+ * Identifies if given ACL type can have duplicate entries or
fa26e11c
WD
3062+ * not.
3063+ *
3064+ * Inputs: acl_type - ACL Type.
3065+ *
0f6733d8 3066+ * Outputs:
fa26e11c 3067+ *
0f6733d8 3068+ * Return..
fa26e11c
WD
3069+ *
3070+ * True - If the ACL type matches any of the prohibited types.
3071+ * False - If the ACL type doesn't match any of the prohibited types.
0f6733d8 3072+ */
fa26e11c
WD
3073+
3074+static BOOL hpux_prohibited_duplicate_type(int acl_type)
3075+{
0f6733d8 3076+ switch(acl_type) {
fa26e11c
WD
3077+ case USER:
3078+ case GROUP:
0f6733d8 3079+ case DEF_USER:
fa26e11c
WD
3080+ case DEF_GROUP:
3081+ return True;
0f6733d8
WD
3082+ default:
3083+ return False;
fa26e11c 3084+ }
fa26e11c
WD
3085+}
3086+
3087+/* get_needed_class_perm
3088+ * Returns the permissions of a ACL structure only if the ACL
0f6733d8 3089+ * type matches one of the pre-determined types for computing
fa26e11c
WD
3090+ * CLASS_OBJ permissions.
3091+ *
3092+ * Inputs: aclp - Pointer to ACL structure.
3093+ */
3094+
3095+static int hpux_get_needed_class_perm(struct acl *aclp)
3096+{
0f6733d8
WD
3097+ switch(aclp->a_type) {
3098+ case USER:
3099+ case GROUP_OBJ:
3100+ case GROUP:
3101+ case DEF_USER_OBJ:
fa26e11c 3102+ case DEF_USER:
0f6733d8 3103+ case DEF_GROUP_OBJ:
fa26e11c
WD
3104+ case DEF_GROUP:
3105+ case DEF_CLASS_OBJ:
0f6733d8 3106+ case DEF_OTHER_OBJ:
fa26e11c 3107+ return aclp->a_perm;
0f6733d8 3108+ default:
fa26e11c
WD
3109+ return 0;
3110+ }
3111+}
3112+
3113+/* acl_sort for HPUX.
3114+ * Sorts the array of ACL structures as per the description in
3115+ * aclsort man page. Refer to aclsort man page for more details
3116+ *
3117+ * Inputs:
3118+ *
3119+ * acl_count - Count of ACLs in the array of ACL structures.
3120+ * calclass - If this is not zero, then we compute the CLASS_OBJ
3121+ * permissions.
3122+ * aclp - Array of ACL structures.
3123+ *
3124+ * Outputs:
3125+ *
3126+ * aclp - Sorted array of ACL structures.
3127+ *
3128+ * Outputs:
3129+ *
3130+ * Returns 0 for success -1 for failure. Prints a message to the Samba
3131+ * debug log in case of failure.
3132+ */
3133+
3134+static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3135+{
3136+#if !defined(HAVE_HPUX_ACLSORT)
0f6733d8
WD
3137+ /*
3138+ * The aclsort() system call is availabe on the latest HPUX General
3139+ * Patch Bundles. So for HPUX, we developed our version of acl_sort
3140+ * function. Because, we don't want to update to a new
3141+ * HPUX GR bundle just for aclsort() call.
3142+ */
fa26e11c
WD
3143+
3144+ struct hpux_acl_types acl_obj_count;
3145+ int n_class_obj_perm = 0;
3146+ int i, j;
0f6733d8
WD
3147+
3148+ if(!acl_count) {
fa26e11c
WD
3149+ DEBUG(10,("Zero acl count passed. Returning Success\n"));
3150+ return 0;
3151+ }
3152+
0f6733d8 3153+ if(aclp == NULL) {
fa26e11c
WD
3154+ DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3155+ return -1;
3156+ }
3157+
3158+ /* Count different types of ACLs in the ACLs array */
3159+
3160+ hpux_count_obj(acl_count, aclp, &acl_obj_count);
3161+
0f6733d8
WD
3162+ /* There should be only one entry each of type USER_OBJ, GROUP_OBJ,
3163+ * CLASS_OBJ and OTHER_OBJ
fa26e11c
WD
3164+ */
3165+
0f6733d8
WD
3166+ if( (acl_obj_count.n_user_obj != 1) ||
3167+ (acl_obj_count.n_group_obj != 1) ||
3168+ (acl_obj_count.n_class_obj != 1) ||
3169+ (acl_obj_count.n_other_obj != 1)
3170+ ) {
fa26e11c
WD
3171+ DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3172+USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3173+ return -1;
3174+ }
3175+
3176+ /* If any of the default objects are present, there should be only
3177+ * one of them each.
3178+ */
3179+
0f6733d8
WD
3180+ if( (acl_obj_count.n_def_user_obj > 1) || (acl_obj_count.n_def_group_obj > 1) ||
3181+ (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
fa26e11c
WD
3182+ DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3183+or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3184+ return -1;
3185+ }
3186+
0f6733d8
WD
3187+ /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl
3188+ * structures.
fa26e11c
WD
3189+ *
3190+ * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3191+ * same ACL type, sort by ACL id.
3192+ *
0f6733d8 3193+ * I am using the trival kind of sorting method here because, performance isn't
fa26e11c 3194+ * really effected by the ACLs feature. More over there aren't going to be more
0f6733d8 3195+ * than 17 entries on HPUX.
fa26e11c
WD
3196+ */
3197+
0f6733d8 3198+ for(i=0; i<acl_count;i++) {
fa26e11c 3199+ for (j=i+1; j<acl_count; j++) {
0f6733d8 3200+ if( aclp[i].a_type > aclp[j].a_type ) {
fa26e11c
WD
3201+ /* ACL entries out of order, swap them */
3202+
3203+ hpux_swap_acl_entries((aclp+i), (aclp+j));
3204+
0f6733d8 3205+ } else if ( aclp[i].a_type == aclp[j].a_type ) {
fa26e11c
WD
3206+
3207+ /* ACL entries of same type, sort by id */
3208+
0f6733d8 3209+ if(aclp[i].a_id > aclp[j].a_id) {
fa26e11c
WD
3210+ hpux_swap_acl_entries((aclp+i), (aclp+j));
3211+ } else if (aclp[i].a_id == aclp[j].a_id) {
3212+ /* We have a duplicate entry. */
0f6733d8 3213+ if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
fa26e11c
WD
3214+ DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3215+ aclp[i].a_type, aclp[i].a_id));
3216+ return -1;
3217+ }
3218+ }
3219+
3220+ }
3221+ }
3222+ }
3223+
3224+ /* set the class obj permissions to the computed one. */
0f6733d8 3225+ if(calclass) {
fa26e11c
WD
3226+ int n_class_obj_index = -1;
3227+
0f6733d8 3228+ for(i=0;i<acl_count;i++) {
fa26e11c
WD
3229+ n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3230+
0f6733d8 3231+ if(aclp[i].a_type == CLASS_OBJ)
fa26e11c
WD
3232+ n_class_obj_index = i;
3233+ }
3234+ aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3235+ }
3236+
3237+ return 0;
3238+#else
3239+ return aclsort(acl_count, calclass, aclp);
3240+#endif
3241+}
3242+
0f6733d8
WD
3243+/*
3244+ * sort the ACL and check it for validity
fa26e11c 3245+ *
0f6733d8 3246+ * if it's a minimal ACL with only 4 entries then we
fa26e11c
WD
3247+ * need to recalculate the mask permissions to make
3248+ * sure that they are the same as the GROUP_OBJ
3249+ * permissions as required by the UnixWare acl() system call.
3250+ *
0f6733d8 3251+ * (note: since POSIX allows minimal ACLs which only contain
fa26e11c
WD
3252+ * 3 entries - ie there is no mask entry - we should, in theory,
3253+ * check for this and add a mask entry if necessary - however
3254+ * we "know" that the caller of this interface always specifies
3255+ * a mask so, in practice "this never happens" (tm) - if it *does*
3256+ * happen aclsort() will fail and return an error and someone will
0f6733d8
WD
3257+ * have to fix it ...)
3258+ */
fa26e11c
WD
3259+
3260+static int acl_sort(SMB_ACL_T acl_d)
3261+{
3262+ int fixmask = (acl_d->count <= 4);
3263+
3264+ if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
3265+ errno = EINVAL;
3266+ return -1;
3267+ }
3268+ return 0;
3269+}
0f6733d8
WD
3270+
3271+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
3272+{
3273+ return acl_sort(acl_d);
3274+}
3275+
0f6733d8 3276+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c 3277+{
0f6733d8
WD
3278+ struct stat s;
3279+ struct acl *acl_p;
3280+ int acl_count;
3281+ struct acl *acl_buf = NULL;
3282+ int ret;
fa26e11c 3283+
0f6733d8
WD
3284+ if(hpux_acl_call_presence() == False) {
3285+ /* Looks like we don't have the acl() system call on HPUX.
3286+ * May be the system doesn't have the latest version of JFS.
3287+ */
3288+ errno=ENOSYS;
3289+ return -1;
fa26e11c
WD
3290+ }
3291+
3292+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3293+ errno = EINVAL;
3294+ return -1;
3295+ }
3296+
3297+ if (acl_sort(acl_d) != 0) {
3298+ return -1;
3299+ }
3300+
0f6733d8
WD
3301+ acl_p = &acl_d->acl[0];
3302+ acl_count = acl_d->count;
fa26e11c 3303+
0f6733d8
WD
3304+ /*
3305+ * if it's a directory there is extra work to do
3306+ * since the acl() system call will replace both
3307+ * the access ACLs and the default ACLs (if any)
3308+ */
fa26e11c
WD
3309+ if (stat(name, &s) != 0) {
3310+ return -1;
3311+ }
3312+ if (S_ISDIR(s.st_mode)) {
0f6733d8
WD
3313+ SMB_ACL_T acc_acl;
3314+ SMB_ACL_T def_acl;
3315+ SMB_ACL_T tmp_acl;
3316+ int i;
fa26e11c
WD
3317+
3318+ if (type == SMB_ACL_TYPE_ACCESS) {
3319+ acc_acl = acl_d;
3320+ def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3321+
3322+ } else {
3323+ def_acl = acl_d;
3324+ acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3325+ }
3326+
3327+ if (tmp_acl == NULL) {
3328+ return -1;
3329+ }
3330+
0f6733d8
WD
3331+ /*
3332+ * allocate a temporary buffer for the complete ACL
3333+ */
fa26e11c 3334+ acl_count = acc_acl->count + def_acl->count;
252945ef 3335+ acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
fa26e11c
WD
3336+
3337+ if (acl_buf == NULL) {
3338+ sys_acl_free_acl(tmp_acl);
3339+ errno = ENOMEM;
3340+ return -1;
3341+ }
3342+
0f6733d8
WD
3343+ /*
3344+ * copy the access control and default entries into the buffer
3345+ */
fa26e11c 3346+ memcpy(&acl_buf[0], &acc_acl->acl[0],
0f6733d8 3347+ acc_acl->count * sizeof(acl_buf[0]));
fa26e11c
WD
3348+
3349+ memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
0f6733d8 3350+ def_acl->count * sizeof(acl_buf[0]));
fa26e11c 3351+
0f6733d8
WD
3352+ /*
3353+ * set the ACL_DEFAULT flag on the default entries
3354+ */
fa26e11c
WD
3355+ for (i = acc_acl->count; i < acl_count; i++) {
3356+ acl_buf[i].a_type |= ACL_DEFAULT;
3357+ }
3358+
3359+ sys_acl_free_acl(tmp_acl);
3360+
3361+ } else if (type != SMB_ACL_TYPE_ACCESS) {
3362+ errno = EINVAL;
3363+ return -1;
3364+ }
3365+
3366+ ret = acl(name, ACL_SET, acl_count, acl_p);
3367+
0f6733d8
WD
3368+ if (acl_buf) {
3369+ free(acl_buf);
3370+ }
fa26e11c
WD
3371+
3372+ return ret;
3373+}
3374+
0f6733d8 3375+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c 3376+{
0f6733d8
WD
3377+ /*
3378+ * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3379+ */
fa26e11c
WD
3380+
3381+ files_struct *fsp = file_find_fd(fd);
3382+
3383+ if (fsp == NULL) {
3384+ errno = EBADF;
3385+ return NULL;
3386+ }
3387+
3388+ if (acl_sort(acl_d) != 0) {
3389+ return -1;
3390+ }
3391+
0f6733d8
WD
3392+ /*
3393+ * We know we're in the same conn context. So we
3394+ * can use the relative path.
3395+ */
fa26e11c 3396+
5ca9317d 3397+ return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
fa26e11c
WD
3398+}
3399+
0f6733d8 3400+int sys_acl_delete_def_file(const char *path)
fa26e11c 3401+{
0f6733d8
WD
3402+ SMB_ACL_T acl_d;
3403+ int ret;
fa26e11c 3404+
0f6733d8
WD
3405+ /*
3406+ * fetching the access ACL and rewriting it has
3407+ * the effect of deleting the default ACL
3408+ */
fa26e11c
WD
3409+ if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3410+ return -1;
3411+ }
3412+
3413+ ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
3414+
3415+ sys_acl_free_acl(acl_d);
0f6733d8 3416+
fa26e11c
WD
3417+ return ret;
3418+}
3419+
0f6733d8 3420+int sys_acl_free_text(char *text)
fa26e11c
WD
3421+{
3422+ free(text);
3423+ return 0;
3424+}
3425+
0f6733d8 3426+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c
WD
3427+{
3428+ free(acl_d);
3429+ return 0;
3430+}
3431+
0f6733d8 3432+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
3433+{
3434+ return 0;
3435+}
3436+
3437+#elif defined(HAVE_IRIX_ACLS)
3438+
0f6733d8 3439+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
3440+{
3441+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3442+ errno = EINVAL;
3443+ return -1;
3444+ }
3445+
3446+ if (entry_p == NULL) {
3447+ errno = EINVAL;
3448+ return -1;
3449+ }
3450+
3451+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
3452+ acl_d->next = 0;
3453+ }
3454+
3455+ if (acl_d->next < 0) {
3456+ errno = EINVAL;
3457+ return -1;
3458+ }
3459+
3460+ if (acl_d->next >= acl_d->aclp->acl_cnt) {
3461+ return 0;
3462+ }
3463+
3464+ *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
3465+
3466+ return 1;
3467+}
3468+
0f6733d8 3469+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
3470+{
3471+ *type_p = entry_d->ae_tag;
3472+
3473+ return 0;
3474+}
3475+
0f6733d8 3476+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
3477+{
3478+ *permset_p = entry_d;
3479+
3480+ return 0;
3481+}
3482+
0f6733d8 3483+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
3484+{
3485+ if (entry_d->ae_tag != SMB_ACL_USER
3486+ && entry_d->ae_tag != SMB_ACL_GROUP) {
3487+ errno = EINVAL;
3488+ return NULL;
3489+ }
3490+
3491+ return &entry_d->ae_id;
3492+}
3493+
0f6733d8 3494+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 3495+{
0f6733d8 3496+ SMB_ACL_T a;
fa26e11c 3497+
252945ef 3498+ if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
fa26e11c
WD
3499+ errno = ENOMEM;
3500+ return NULL;
3501+ }
3502+ if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
0f6733d8 3503+ SAFE_FREE(a);
fa26e11c
WD
3504+ return NULL;
3505+ }
3506+ a->next = -1;
3507+ a->freeaclp = True;
3508+ return a;
3509+}
3510+
0f6733d8 3511+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 3512+{
0f6733d8 3513+ SMB_ACL_T a;
fa26e11c 3514+
252945ef 3515+ if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
fa26e11c
WD
3516+ errno = ENOMEM;
3517+ return NULL;
3518+ }
3519+ if ((a->aclp = acl_get_fd(fd)) == NULL) {
0f6733d8 3520+ SAFE_FREE(a);
fa26e11c
WD
3521+ return NULL;
3522+ }
3523+ a->next = -1;
3524+ a->freeaclp = True;
3525+ return a;
3526+}
3527+
0f6733d8 3528+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
3529+{
3530+ permset_d->ae_perm = 0;
3531+
3532+ return 0;
3533+}
3534+
0f6733d8 3535+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
3536+{
3537+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3538+ && perm != SMB_ACL_EXECUTE) {
3539+ errno = EINVAL;
3540+ return -1;
3541+ }
3542+
3543+ if (permset_d == NULL) {
3544+ errno = EINVAL;
3545+ return -1;
3546+ }
3547+
3548+ permset_d->ae_perm |= perm;
3549+
3550+ return 0;
3551+}
3552+
0f6733d8 3553+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
3554+{
3555+ return permset_d->ae_perm & perm;
3556+}
3557+
0f6733d8 3558+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c
WD
3559+{
3560+ return acl_to_text(acl_d->aclp, len_p);
3561+}
3562+
0f6733d8 3563+SMB_ACL_T sys_acl_init(int count)
fa26e11c 3564+{
0f6733d8 3565+ SMB_ACL_T a;
fa26e11c
WD
3566+
3567+ if (count < 0) {
3568+ errno = EINVAL;
3569+ return NULL;
3570+ }
3571+
252945ef 3572+ if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
fa26e11c
WD
3573+ errno = ENOMEM;
3574+ return NULL;
3575+ }
3576+
3577+ a->next = -1;
3578+ a->freeaclp = False;
0f6733d8 3579+ a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
fa26e11c
WD
3580+ a->aclp->acl_cnt = 0;
3581+
3582+ return a;
3583+}
3584+
3585+
0f6733d8 3586+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 3587+{
0f6733d8
WD
3588+ SMB_ACL_T acl_d;
3589+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
3590+
3591+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3592+ errno = EINVAL;
3593+ return -1;
3594+ }
3595+
3596+ if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
3597+ errno = ENOSPC;
3598+ return -1;
3599+ }
3600+
0f6733d8
WD
3601+ entry_d = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
3602+ entry_d->ae_tag = 0;
3603+ entry_d->ae_id = 0;
3604+ entry_d->ae_perm = 0;
3605+ *entry_p = entry_d;
fa26e11c
WD
3606+
3607+ return 0;
3608+}
3609+
0f6733d8 3610+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
3611+{
3612+ switch (tag_type) {
3613+ case SMB_ACL_USER:
3614+ case SMB_ACL_USER_OBJ:
3615+ case SMB_ACL_GROUP:
3616+ case SMB_ACL_GROUP_OBJ:
3617+ case SMB_ACL_OTHER:
3618+ case SMB_ACL_MASK:
3619+ entry_d->ae_tag = tag_type;
3620+ break;
3621+ default:
3622+ errno = EINVAL;
3623+ return -1;
3624+ }
3625+
3626+ return 0;
3627+}
3628+
0f6733d8 3629+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
3630+{
3631+ if (entry_d->ae_tag != SMB_ACL_GROUP
3632+ && entry_d->ae_tag != SMB_ACL_USER) {
3633+ errno = EINVAL;
3634+ return -1;
3635+ }
3636+
3637+ entry_d->ae_id = *((id_t *)qual_p);
3638+
3639+ return 0;
3640+}
3641+
0f6733d8 3642+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
3643+{
3644+ if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3645+ return EINVAL;
3646+ }
3647+
3648+ entry_d->ae_perm = permset_d->ae_perm;
3649+
3650+ return 0;
3651+}
3652+
0f6733d8 3653+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
3654+{
3655+ return acl_valid(acl_d->aclp);
3656+}
3657+
0f6733d8 3658+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c
WD
3659+{
3660+ return acl_set_file(name, type, acl_d->aclp);
3661+}
3662+
0f6733d8 3663+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c
WD
3664+{
3665+ return acl_set_fd(fd, acl_d->aclp);
3666+}
3667+
0f6733d8 3668+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
3669+{
3670+ return acl_delete_def_file(name);
3671+}
3672+
0f6733d8 3673+int sys_acl_free_text(char *text)
fa26e11c
WD
3674+{
3675+ return acl_free(text);
3676+}
3677+
0f6733d8 3678+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c
WD
3679+{
3680+ if (acl_d->freeaclp) {
3681+ acl_free(acl_d->aclp);
3682+ }
3683+ acl_free(acl_d);
3684+ return 0;
3685+}
3686+
0f6733d8 3687+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
3688+{
3689+ return 0;
3690+}
3691+
3692+#elif defined(HAVE_AIX_ACLS)
3693+
3694+/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
3695+
0f6733d8 3696+int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
3697+{
3698+ struct acl_entry_link *link;
3699+ struct new_acl_entry *entry;
3700+ int keep_going;
3701+
3702+ DEBUG(10,("This is the count: %d\n",theacl->count));
3703+
0f6733d8
WD
3704+ /* Check if count was previously set to -1. *
3705+ * If it was, that means we reached the end *
3706+ * of the acl last time. */
3707+ if(theacl->count == -1)
3708+ return(0);
fa26e11c
WD
3709+
3710+ link = theacl;
0f6733d8
WD
3711+ /* To get to the next acl, traverse linked list until index *
3712+ * of acl matches the count we are keeping. This count is *
3713+ * incremented each time we return an acl entry. */
fa26e11c 3714+
0f6733d8 3715+ for(keep_going = 0; keep_going < theacl->count; keep_going++)
fa26e11c
WD
3716+ link = link->nextp;
3717+
3718+ entry = *entry_p = link->entryp;
3719+
3720+ DEBUG(10,("*entry_p is %d\n",entry_p));
3721+ DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
3722+
3723+ /* Increment count */
3724+ theacl->count++;
0f6733d8 3725+ if(link->nextp == NULL)
fa26e11c
WD
3726+ theacl->count = -1;
3727+
0f6733d8 3728+ return(1);
fa26e11c
WD
3729+}
3730+
0f6733d8 3731+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c
WD
3732+{
3733+ /* Initialize tag type */
3734+
3735+ *tag_type_p = -1;
3736+ DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
3737+
0f6733d8
WD
3738+ /* Depending on what type of entry we have, *
3739+ * return tag type. */
3740+ switch(entry_d->ace_id->id_type) {
fa26e11c
WD
3741+ case ACEID_USER:
3742+ *tag_type_p = SMB_ACL_USER;
3743+ break;
3744+ case ACEID_GROUP:
3745+ *tag_type_p = SMB_ACL_GROUP;
3746+ break;
3747+
3748+ case SMB_ACL_USER_OBJ:
3749+ case SMB_ACL_GROUP_OBJ:
3750+ case SMB_ACL_OTHER:
3751+ *tag_type_p = entry_d->ace_id->id_type;
3752+ break;
0f6733d8 3753+
fa26e11c 3754+ default:
0f6733d8 3755+ return(-1);
fa26e11c
WD
3756+ }
3757+
0f6733d8 3758+ return(0);
fa26e11c
WD
3759+}
3760+
0f6733d8 3761+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
3762+{
3763+ DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
3764+ *permset_p = &entry_d->ace_access;
3765+ DEBUG(10,("**permset_p is %d\n",**permset_p));
0f6733d8
WD
3766+ if(!(**permset_p & S_IXUSR) &&
3767+ !(**permset_p & S_IWUSR) &&
3768+ !(**permset_p & S_IRUSR) &&
3769+ (**permset_p != 0))
3770+ return(-1);
fa26e11c
WD
3771+
3772+ DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
0f6733d8 3773+ return(0);
fa26e11c
WD
3774+}
3775+
0f6733d8 3776+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 3777+{
0f6733d8 3778+ return(entry_d->ace_id->id_data);
fa26e11c
WD
3779+}
3780+
0f6733d8 3781+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c
WD
3782+{
3783+ struct acl *file_acl = (struct acl *)NULL;
3784+ struct acl_entry *acl_entry;
3785+ struct new_acl_entry *new_acl_entry;
3786+ struct ace_id *idp;
3787+ struct acl_entry_link *acl_entry_link;
3788+ struct acl_entry_link *acl_entry_link_head;
3789+ int i;
3790+ int rc = 0;
3791+ uid_t user_id;
3792+
252945ef
WD
3793+ /* AIX has no DEFAULT */
3794+ if ( type == SMB_ACL_TYPE_DEFAULT )
3795+ return NULL;
3796+
fa26e11c 3797+ /* Get the acl using statacl */
0f6733d8 3798+
fa26e11c
WD
3799+ DEBUG(10,("Entering sys_acl_get_file\n"));
3800+ DEBUG(10,("path_p is %s\n",path_p));
3801+
252945ef 3802+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
0f6733d8
WD
3803+
3804+ if(file_acl == NULL) {
fa26e11c
WD
3805+ errno=ENOMEM;
3806+ DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
0f6733d8 3807+ return(NULL);
fa26e11c
WD
3808+ }
3809+
3810+ memset(file_acl,0,BUFSIZ);
3811+
3812+ rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
0f6733d8 3813+ if(rc == -1) {
fa26e11c 3814+ DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
0f6733d8
WD
3815+ SAFE_FREE(file_acl);
3816+ return(NULL);
fa26e11c
WD
3817+ }
3818+
3819+ DEBUG(10,("Got facl and returned it\n"));
3820+
3821+ /* Point to the first acl entry in the acl */
3822+ acl_entry = file_acl->acl_ext;
3823+
0f6733d8
WD
3824+ /* Begin setting up the head of the linked list *
3825+ * that will be used for the storing the acl *
3826+ * in a way that is useful for the posix_acls.c *
3827+ * code. */
fa26e11c
WD
3828+
3829+ acl_entry_link_head = acl_entry_link = sys_acl_init(0);
0f6733d8
WD
3830+ if(acl_entry_link_head == NULL)
3831+ return(NULL);
fa26e11c 3832+
252945ef 3833+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
3834+ if(acl_entry_link->entryp == NULL) {
3835+ SAFE_FREE(file_acl);
fa26e11c
WD
3836+ errno = ENOMEM;
3837+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3838+ return(NULL);
fa26e11c
WD
3839+ }
3840+
3841+ DEBUG(10,("acl_entry is %d\n",acl_entry));
3842+ DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
3843+
0f6733d8
WD
3844+ /* Check if the extended acl bit is on. *
3845+ * If it isn't, do not show the *
3846+ * contents of the acl since AIX intends *
3847+ * the extended info to remain unused */
fa26e11c 3848+
0f6733d8 3849+ if(file_acl->acl_mode & S_IXACL){
fa26e11c 3850+ /* while we are not pointing to the very end */
0f6733d8 3851+ while(acl_entry < acl_last(file_acl)) {
fa26e11c
WD
3852+ /* before we malloc anything, make sure this is */
3853+ /* a valid acl entry and one that we want to map */
3854+ idp = id_nxt(acl_entry->ace_id);
0f6733d8
WD
3855+ if((acl_entry->ace_type == ACC_SPECIFY ||
3856+ (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
3857+ acl_entry = acl_nxt(acl_entry);
3858+ continue;
fa26e11c
WD
3859+ }
3860+
3861+ idp = acl_entry->ace_id;
3862+
0f6733d8
WD
3863+ /* Check if this is the first entry in the linked list. *
3864+ * The first entry needs to keep prevp pointing to NULL *
3865+ * and already has entryp allocated. */
fa26e11c 3866+
0f6733d8 3867+ if(acl_entry_link_head->count != 0) {
252945ef 3868+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
fa26e11c 3869+
0f6733d8
WD
3870+ if(acl_entry_link->nextp == NULL) {
3871+ SAFE_FREE(file_acl);
fa26e11c
WD
3872+ errno = ENOMEM;
3873+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3874+ return(NULL);
fa26e11c
WD
3875+ }
3876+
3877+ acl_entry_link->nextp->prevp = acl_entry_link;
3878+ acl_entry_link = acl_entry_link->nextp;
252945ef 3879+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
3880+ if(acl_entry_link->entryp == NULL) {
3881+ SAFE_FREE(file_acl);
fa26e11c
WD
3882+ errno = ENOMEM;
3883+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3884+ return(NULL);
fa26e11c
WD
3885+ }
3886+ acl_entry_link->nextp = NULL;
3887+ }
3888+
3889+ acl_entry_link->entryp->ace_len = acl_entry->ace_len;
3890+
0f6733d8
WD
3891+ /* Don't really need this since all types are going *
3892+ * to be specified but, it's better than leaving it 0 */
fa26e11c
WD
3893+
3894+ acl_entry_link->entryp->ace_type = acl_entry->ace_type;
0f6733d8 3895+
fa26e11c 3896+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
0f6733d8
WD
3897+
3898+ memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
fa26e11c 3899+
0f6733d8
WD
3900+ /* The access in the acl entries must be left shifted by *
3901+ * three bites, because they will ultimately be compared *
3902+ * to S_IRUSR, S_IWUSR, and S_IXUSR. */
fa26e11c 3903+
0f6733d8 3904+ switch(acl_entry->ace_type){
fa26e11c
WD
3905+ case ACC_PERMIT:
3906+ case ACC_SPECIFY:
3907+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3908+ acl_entry_link->entryp->ace_access <<= 6;
3909+ acl_entry_link_head->count++;
3910+ break;
3911+ case ACC_DENY:
0f6733d8
WD
3912+ /* Since there is no way to return a DENY acl entry *
3913+ * change to PERMIT and then shift. */
fa26e11c
WD
3914+ DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
3915+ acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
3916+ DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
3917+ acl_entry_link->entryp->ace_access <<= 6;
3918+ acl_entry_link_head->count++;
3919+ break;
3920+ default:
0f6733d8 3921+ return(0);
fa26e11c
WD
3922+ }
3923+
3924+ DEBUG(10,("acl_entry = %d\n",acl_entry));
3925+ DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
0f6733d8 3926+
fa26e11c
WD
3927+ acl_entry = acl_nxt(acl_entry);
3928+ }
3929+ } /* end of if enabled */
3930+
0f6733d8
WD
3931+ /* Since owner, group, other acl entries are not *
3932+ * part of the acl entries in an acl, they must *
3933+ * be dummied up to become part of the list. */
fa26e11c 3934+
0f6733d8 3935+ for( i = 1; i < 4; i++) {
fa26e11c 3936+ DEBUG(10,("i is %d\n",i));
0f6733d8 3937+ if(acl_entry_link_head->count != 0) {
252945ef 3938+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8
WD
3939+ if(acl_entry_link->nextp == NULL) {
3940+ SAFE_FREE(file_acl);
fa26e11c
WD
3941+ errno = ENOMEM;
3942+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3943+ return(NULL);
fa26e11c
WD
3944+ }
3945+
3946+ acl_entry_link->nextp->prevp = acl_entry_link;
3947+ acl_entry_link = acl_entry_link->nextp;
252945ef 3948+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
3949+ if(acl_entry_link->entryp == NULL) {
3950+ SAFE_FREE(file_acl);
fa26e11c
WD
3951+ errno = ENOMEM;
3952+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3953+ return(NULL);
fa26e11c
WD
3954+ }
3955+ }
3956+
3957+ acl_entry_link->nextp = NULL;
3958+
3959+ new_acl_entry = acl_entry_link->entryp;
3960+ idp = new_acl_entry->ace_id;
3961+
0f6733d8 3962+ new_acl_entry->ace_len = sizeof(struct acl_entry);
fa26e11c 3963+ new_acl_entry->ace_type = ACC_PERMIT;
0f6733d8 3964+ idp->id_len = sizeof(struct ace_id);
fa26e11c 3965+ DEBUG(10,("idp->id_len = %d\n",idp->id_len));
0f6733d8 3966+ memset(idp->id_data,0,sizeof(uid_t));
fa26e11c 3967+
0f6733d8 3968+ switch(i) {
fa26e11c
WD
3969+ case 2:
3970+ new_acl_entry->ace_access = file_acl->g_access << 6;
3971+ idp->id_type = SMB_ACL_GROUP_OBJ;
3972+ break;
3973+
3974+ case 3:
3975+ new_acl_entry->ace_access = file_acl->o_access << 6;
3976+ idp->id_type = SMB_ACL_OTHER;
3977+ break;
0f6733d8 3978+
fa26e11c
WD
3979+ case 1:
3980+ new_acl_entry->ace_access = file_acl->u_access << 6;
3981+ idp->id_type = SMB_ACL_USER_OBJ;
3982+ break;
0f6733d8 3983+
fa26e11c 3984+ default:
0f6733d8 3985+ return(NULL);
fa26e11c
WD
3986+
3987+ }
3988+
3989+ acl_entry_link_head->count++;
3990+ DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
3991+ }
3992+
3993+ acl_entry_link_head->count = 0;
0f6733d8 3994+ SAFE_FREE(file_acl);
fa26e11c 3995+
0f6733d8 3996+ return(acl_entry_link_head);
fa26e11c
WD
3997+}
3998+
0f6733d8 3999+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c
WD
4000+{
4001+ struct acl *file_acl = (struct acl *)NULL;
4002+ struct acl_entry *acl_entry;
4003+ struct new_acl_entry *new_acl_entry;
4004+ struct ace_id *idp;
4005+ struct acl_entry_link *acl_entry_link;
4006+ struct acl_entry_link *acl_entry_link_head;
4007+ int i;
4008+ int rc = 0;
4009+ uid_t user_id;
4010+
4011+ /* Get the acl using fstatacl */
0f6733d8 4012+
fa26e11c
WD
4013+ DEBUG(10,("Entering sys_acl_get_fd\n"));
4014+ DEBUG(10,("fd is %d\n",fd));
252945ef 4015+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 4016+
0f6733d8 4017+ if(file_acl == NULL) {
fa26e11c
WD
4018+ errno=ENOMEM;
4019+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8 4020+ return(NULL);
fa26e11c
WD
4021+ }
4022+
4023+ memset(file_acl,0,BUFSIZ);
4024+
4025+ rc = fstatacl(fd,0,file_acl,BUFSIZ);
0f6733d8 4026+ if(rc == -1) {
fa26e11c 4027+ DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
0f6733d8
WD
4028+ SAFE_FREE(file_acl);
4029+ return(NULL);
fa26e11c
WD
4030+ }
4031+
4032+ DEBUG(10,("Got facl and returned it\n"));
4033+
4034+ /* Point to the first acl entry in the acl */
4035+
4036+ acl_entry = file_acl->acl_ext;
0f6733d8
WD
4037+ /* Begin setting up the head of the linked list *
4038+ * that will be used for the storing the acl *
4039+ * in a way that is useful for the posix_acls.c *
4040+ * code. */
fa26e11c
WD
4041+
4042+ acl_entry_link_head = acl_entry_link = sys_acl_init(0);
0f6733d8
WD
4043+ if(acl_entry_link_head == NULL){
4044+ SAFE_FREE(file_acl);
4045+ return(NULL);
fa26e11c
WD
4046+ }
4047+
252945ef 4048+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
fa26e11c 4049+
0f6733d8 4050+ if(acl_entry_link->entryp == NULL) {
fa26e11c
WD
4051+ errno = ENOMEM;
4052+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4053+ SAFE_FREE(file_acl);
4054+ return(NULL);
fa26e11c
WD
4055+ }
4056+
4057+ DEBUG(10,("acl_entry is %d\n",acl_entry));
4058+ DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
0f6733d8
WD
4059+
4060+ /* Check if the extended acl bit is on. *
4061+ * If it isn't, do not show the *
4062+ * contents of the acl since AIX intends *
4063+ * the extended info to remain unused */
4064+
4065+ if(file_acl->acl_mode & S_IXACL){
fa26e11c 4066+ /* while we are not pointing to the very end */
0f6733d8 4067+ while(acl_entry < acl_last(file_acl)) {
fa26e11c
WD
4068+ /* before we malloc anything, make sure this is */
4069+ /* a valid acl entry and one that we want to map */
4070+
4071+ idp = id_nxt(acl_entry->ace_id);
0f6733d8
WD
4072+ if((acl_entry->ace_type == ACC_SPECIFY ||
4073+ (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4074+ acl_entry = acl_nxt(acl_entry);
4075+ continue;
fa26e11c
WD
4076+ }
4077+
4078+ idp = acl_entry->ace_id;
0f6733d8
WD
4079+
4080+ /* Check if this is the first entry in the linked list. *
4081+ * The first entry needs to keep prevp pointing to NULL *
4082+ * and already has entryp allocated. */
fa26e11c 4083+
0f6733d8 4084+ if(acl_entry_link_head->count != 0) {
252945ef 4085+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4086+ if(acl_entry_link->nextp == NULL) {
fa26e11c
WD
4087+ errno = ENOMEM;
4088+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4089+ SAFE_FREE(file_acl);
4090+ return(NULL);
fa26e11c
WD
4091+ }
4092+ acl_entry_link->nextp->prevp = acl_entry_link;
4093+ acl_entry_link = acl_entry_link->nextp;
252945ef 4094+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8 4095+ if(acl_entry_link->entryp == NULL) {
fa26e11c
WD
4096+ errno = ENOMEM;
4097+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4098+ SAFE_FREE(file_acl);
4099+ return(NULL);
fa26e11c
WD
4100+ }
4101+
4102+ acl_entry_link->nextp = NULL;
4103+ }
4104+
4105+ acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4106+
0f6733d8
WD
4107+ /* Don't really need this since all types are going *
4108+ * to be specified but, it's better than leaving it 0 */
fa26e11c
WD
4109+
4110+ acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4111+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4112+
0f6733d8 4113+ memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
fa26e11c 4114+
0f6733d8
WD
4115+ /* The access in the acl entries must be left shifted by *
4116+ * three bites, because they will ultimately be compared *
4117+ * to S_IRUSR, S_IWUSR, and S_IXUSR. */
fa26e11c 4118+
0f6733d8 4119+ switch(acl_entry->ace_type){
fa26e11c
WD
4120+ case ACC_PERMIT:
4121+ case ACC_SPECIFY:
4122+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4123+ acl_entry_link->entryp->ace_access <<= 6;
4124+ acl_entry_link_head->count++;
4125+ break;
4126+ case ACC_DENY:
0f6733d8
WD
4127+ /* Since there is no way to return a DENY acl entry *
4128+ * change to PERMIT and then shift. */
fa26e11c
WD
4129+ DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4130+ acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4131+ DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4132+ acl_entry_link->entryp->ace_access <<= 6;
4133+ acl_entry_link_head->count++;
4134+ break;
4135+ default:
0f6733d8 4136+ return(0);
fa26e11c
WD
4137+ }
4138+
4139+ DEBUG(10,("acl_entry = %d\n",acl_entry));
4140+ DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
0f6733d8 4141+
fa26e11c
WD
4142+ acl_entry = acl_nxt(acl_entry);
4143+ }
4144+ } /* end of if enabled */
4145+
0f6733d8
WD
4146+ /* Since owner, group, other acl entries are not *
4147+ * part of the acl entries in an acl, they must *
4148+ * be dummied up to become part of the list. */
fa26e11c 4149+
0f6733d8 4150+ for( i = 1; i < 4; i++) {
fa26e11c 4151+ DEBUG(10,("i is %d\n",i));
0f6733d8 4152+ if(acl_entry_link_head->count != 0){
252945ef 4153+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4154+ if(acl_entry_link->nextp == NULL) {
fa26e11c
WD
4155+ errno = ENOMEM;
4156+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4157+ SAFE_FREE(file_acl);
4158+ return(NULL);
fa26e11c
WD
4159+ }
4160+
4161+ acl_entry_link->nextp->prevp = acl_entry_link;
4162+ acl_entry_link = acl_entry_link->nextp;
252945ef 4163+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
fa26e11c 4164+
0f6733d8
WD
4165+ if(acl_entry_link->entryp == NULL) {
4166+ SAFE_FREE(file_acl);
fa26e11c
WD
4167+ errno = ENOMEM;
4168+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8 4169+ return(NULL);
fa26e11c
WD
4170+ }
4171+ }
4172+
4173+ acl_entry_link->nextp = NULL;
0f6733d8 4174+
fa26e11c
WD
4175+ new_acl_entry = acl_entry_link->entryp;
4176+ idp = new_acl_entry->ace_id;
0f6733d8
WD
4177+
4178+ new_acl_entry->ace_len = sizeof(struct acl_entry);
fa26e11c 4179+ new_acl_entry->ace_type = ACC_PERMIT;
0f6733d8 4180+ idp->id_len = sizeof(struct ace_id);
fa26e11c 4181+ DEBUG(10,("idp->id_len = %d\n",idp->id_len));
0f6733d8
WD
4182+ memset(idp->id_data,0,sizeof(uid_t));
4183+
4184+ switch(i) {
fa26e11c
WD
4185+ case 2:
4186+ new_acl_entry->ace_access = file_acl->g_access << 6;
4187+ idp->id_type = SMB_ACL_GROUP_OBJ;
4188+ break;
0f6733d8 4189+
fa26e11c
WD
4190+ case 3:
4191+ new_acl_entry->ace_access = file_acl->o_access << 6;
4192+ idp->id_type = SMB_ACL_OTHER;
4193+ break;
0f6733d8 4194+
fa26e11c
WD
4195+ case 1:
4196+ new_acl_entry->ace_access = file_acl->u_access << 6;
4197+ idp->id_type = SMB_ACL_USER_OBJ;
4198+ break;
0f6733d8 4199+
fa26e11c 4200+ default:
0f6733d8 4201+ return(NULL);
fa26e11c 4202+ }
0f6733d8 4203+
fa26e11c
WD
4204+ acl_entry_link_head->count++;
4205+ DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4206+ }
4207+
4208+ acl_entry_link_head->count = 0;
0f6733d8
WD
4209+ SAFE_FREE(file_acl);
4210+
4211+ return(acl_entry_link_head);
fa26e11c
WD
4212+}
4213+
0f6733d8 4214+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
fa26e11c
WD
4215+{
4216+ *permset = *permset & ~0777;
0f6733d8 4217+ return(0);
fa26e11c
WD
4218+}
4219+
0f6733d8 4220+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 4221+{
0f6733d8
WD
4222+ if((perm != 0) &&
4223+ (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
4224+ return(-1);
fa26e11c
WD
4225+
4226+ *permset |= perm;
4227+ DEBUG(10,("This is the permset now: %d\n",*permset));
0f6733d8 4228+ return(0);
fa26e11c
WD
4229+}
4230+
0f6733d8 4231+char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
fa26e11c 4232+{
0f6733d8 4233+ return(NULL);
fa26e11c
WD
4234+}
4235+
0f6733d8 4236+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
4237+{
4238+ struct acl_entry_link *theacl = NULL;
0f6733d8 4239+
fa26e11c
WD
4240+ DEBUG(10,("Entering sys_acl_init\n"));
4241+
252945ef 4242+ theacl = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4243+ if(theacl == NULL) {
fa26e11c
WD
4244+ errno = ENOMEM;
4245+ DEBUG(0,("Error in sys_acl_init is %d\n",errno));
0f6733d8 4246+ return(NULL);
fa26e11c
WD
4247+ }
4248+
4249+ theacl->count = 0;
4250+ theacl->nextp = NULL;
4251+ theacl->prevp = NULL;
4252+ theacl->entryp = NULL;
4253+ DEBUG(10,("Exiting sys_acl_init\n"));
0f6733d8 4254+ return(theacl);
fa26e11c
WD
4255+}
4256+
0f6733d8 4257+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
4258+{
4259+ struct acl_entry_link *theacl;
4260+ struct acl_entry_link *acl_entryp;
4261+ struct acl_entry_link *temp_entry;
4262+ int counting;
4263+
4264+ DEBUG(10,("Entering the sys_acl_create_entry\n"));
4265+
4266+ theacl = acl_entryp = *pacl;
4267+
4268+ /* Get to the end of the acl before adding entry */
4269+
0f6733d8 4270+ for(counting=0; counting < theacl->count; counting++){
fa26e11c
WD
4271+ DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4272+ temp_entry = acl_entryp;
4273+ acl_entryp = acl_entryp->nextp;
4274+ }
4275+
0f6733d8 4276+ if(theacl->count != 0){
252945ef 4277+ temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4278+ if(acl_entryp == NULL) {
fa26e11c
WD
4279+ errno = ENOMEM;
4280+ DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
0f6733d8 4281+ return(-1);
fa26e11c
WD
4282+ }
4283+
4284+ DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4285+ acl_entryp->prevp = temp_entry;
4286+ DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
4287+ }
4288+
252945ef 4289+ *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8 4290+ if(*pentry == NULL) {
fa26e11c
WD
4291+ errno = ENOMEM;
4292+ DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
0f6733d8 4293+ return(-1);
fa26e11c
WD
4294+ }
4295+
0f6733d8
WD
4296+ memset(*pentry,0,sizeof(struct new_acl_entry));
4297+ acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
fa26e11c 4298+ acl_entryp->entryp->ace_type = ACC_PERMIT;
0f6733d8 4299+ acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
fa26e11c
WD
4300+ acl_entryp->nextp = NULL;
4301+ theacl->count++;
4302+ DEBUG(10,("Exiting sys_acl_create_entry\n"));
0f6733d8 4303+ return(0);
fa26e11c
WD
4304+}
4305+
0f6733d8 4306+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
4307+{
4308+ DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
4309+ entry->ace_id->id_type = tagtype;
4310+ DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
4311+ DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
4312+}
4313+
0f6733d8 4314+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
4315+{
4316+ DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
0f6733d8 4317+ memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
fa26e11c 4318+ DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
0f6733d8 4319+ return(0);
fa26e11c
WD
4320+}
4321+
0f6733d8 4322+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
4323+{
4324+ DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
0f6733d8
WD
4325+ if(!(*permset & S_IXUSR) &&
4326+ !(*permset & S_IWUSR) &&
4327+ !(*permset & S_IRUSR) &&
4328+ (*permset != 0))
4329+ return(-1);
fa26e11c
WD
4330+
4331+ entry->ace_access = *permset;
4332+ DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
4333+ DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
0f6733d8 4334+ return(0);
fa26e11c
WD
4335+}
4336+
0f6733d8 4337+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c
WD
4338+{
4339+ int user_obj = 0;
4340+ int group_obj = 0;
4341+ int other_obj = 0;
4342+ struct acl_entry_link *acl_entry;
4343+
0f6733d8 4344+ for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
fa26e11c
WD
4345+ user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
4346+ group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
4347+ other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
4348+ }
4349+
4350+ DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
0f6733d8
WD
4351+
4352+ if(user_obj != 1 || group_obj != 1 || other_obj != 1)
4353+ return(-1);
fa26e11c 4354+
0f6733d8 4355+ return(0);
fa26e11c
WD
4356+}
4357+
0f6733d8 4358+int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
fa26e11c
WD
4359+{
4360+ struct acl_entry_link *acl_entry_link = NULL;
4361+ struct acl *file_acl = NULL;
4362+ struct acl *file_acl_temp = NULL;
4363+ struct acl_entry *acl_entry = NULL;
4364+ struct ace_id *ace_id = NULL;
4365+ uint id_type;
4366+ uint ace_access;
4367+ uint user_id;
4368+ uint acl_length;
4369+ uint rc;
4370+
4371+ DEBUG(10,("Entering sys_acl_set_file\n"));
4372+ DEBUG(10,("File name is %s\n",name));
0f6733d8 4373+
fa26e11c 4374+ /* AIX has no default ACL */
0f6733d8
WD
4375+ if(acltype == SMB_ACL_TYPE_DEFAULT)
4376+ return(0);
fa26e11c
WD
4377+
4378+ acl_length = BUFSIZ;
252945ef 4379+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 4380+
0f6733d8 4381+ if(file_acl == NULL) {
fa26e11c
WD
4382+ errno = ENOMEM;
4383+ DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
0f6733d8 4384+ return(-1);
fa26e11c
WD
4385+ }
4386+
4387+ memset(file_acl,0,BUFSIZ);
4388+
4389+ file_acl->acl_len = ACL_SIZ;
4390+ file_acl->acl_mode = S_IXACL;
4391+
0f6733d8 4392+ for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
fa26e11c
WD
4393+ acl_entry_link->entryp->ace_access >>= 6;
4394+ id_type = acl_entry_link->entryp->ace_id->id_type;
4395+
0f6733d8 4396+ switch(id_type) {
fa26e11c
WD
4397+ case SMB_ACL_USER_OBJ:
4398+ file_acl->u_access = acl_entry_link->entryp->ace_access;
4399+ continue;
4400+ case SMB_ACL_GROUP_OBJ:
4401+ file_acl->g_access = acl_entry_link->entryp->ace_access;
4402+ continue;
4403+ case SMB_ACL_OTHER:
4404+ file_acl->o_access = acl_entry_link->entryp->ace_access;
4405+ continue;
4406+ case SMB_ACL_MASK:
4407+ continue;
4408+ }
4409+
0f6733d8
WD
4410+ if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4411+ acl_length += sizeof(struct acl_entry);
252945ef 4412+ file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
0f6733d8
WD
4413+ if(file_acl_temp == NULL) {
4414+ SAFE_FREE(file_acl);
fa26e11c
WD
4415+ errno = ENOMEM;
4416+ DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
0f6733d8
WD
4417+ return(-1);
4418+ }
fa26e11c
WD
4419+
4420+ memcpy(file_acl_temp,file_acl,file_acl->acl_len);
0f6733d8 4421+ SAFE_FREE(file_acl);
fa26e11c
WD
4422+ file_acl = file_acl_temp;
4423+ }
4424+
4425+ acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
0f6733d8 4426+ file_acl->acl_len += sizeof(struct acl_entry);
fa26e11c
WD
4427+ acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4428+ acl_entry->ace_access = acl_entry_link->entryp->ace_access;
0f6733d8 4429+
fa26e11c 4430+ /* In order to use this, we'll need to wait until we can get denies */
0f6733d8
WD
4431+ /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4432+ acl_entry->ace_type = ACC_SPECIFY; */
fa26e11c
WD
4433+
4434+ acl_entry->ace_type = ACC_SPECIFY;
0f6733d8 4435+
fa26e11c 4436+ ace_id = acl_entry->ace_id;
0f6733d8 4437+
fa26e11c
WD
4438+ ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4439+ DEBUG(10,("The id type is %d\n",ace_id->id_type));
4440+ ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
0f6733d8
WD
4441+ memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4442+ memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
fa26e11c
WD
4443+ }
4444+
0f6733d8 4445+ rc = chacl(name,file_acl,file_acl->acl_len);
fa26e11c
WD
4446+ DEBUG(10,("errno is %d\n",errno));
4447+ DEBUG(10,("return code is %d\n",rc));
0f6733d8 4448+ SAFE_FREE(file_acl);
fa26e11c 4449+ DEBUG(10,("Exiting the sys_acl_set_file\n"));
0f6733d8 4450+ return(rc);
fa26e11c
WD
4451+}
4452+
0f6733d8 4453+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
4454+{
4455+ struct acl_entry_link *acl_entry_link = NULL;
4456+ struct acl *file_acl = NULL;
4457+ struct acl *file_acl_temp = NULL;
4458+ struct acl_entry *acl_entry = NULL;
4459+ struct ace_id *ace_id = NULL;
4460+ uint id_type;
4461+ uint user_id;
4462+ uint acl_length;
4463+ uint rc;
0f6733d8 4464+
fa26e11c
WD
4465+ DEBUG(10,("Entering sys_acl_set_fd\n"));
4466+ acl_length = BUFSIZ;
252945ef 4467+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 4468+
0f6733d8 4469+ if(file_acl == NULL) {
fa26e11c
WD
4470+ errno = ENOMEM;
4471+ DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
0f6733d8 4472+ return(-1);
fa26e11c
WD
4473+ }
4474+
4475+ memset(file_acl,0,BUFSIZ);
0f6733d8 4476+
fa26e11c
WD
4477+ file_acl->acl_len = ACL_SIZ;
4478+ file_acl->acl_mode = S_IXACL;
4479+
0f6733d8 4480+ for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
fa26e11c
WD
4481+ acl_entry_link->entryp->ace_access >>= 6;
4482+ id_type = acl_entry_link->entryp->ace_id->id_type;
4483+ DEBUG(10,("The id_type is %d\n",id_type));
4484+
0f6733d8 4485+ switch(id_type) {
fa26e11c
WD
4486+ case SMB_ACL_USER_OBJ:
4487+ file_acl->u_access = acl_entry_link->entryp->ace_access;
4488+ continue;
4489+ case SMB_ACL_GROUP_OBJ:
4490+ file_acl->g_access = acl_entry_link->entryp->ace_access;
4491+ continue;
4492+ case SMB_ACL_OTHER:
4493+ file_acl->o_access = acl_entry_link->entryp->ace_access;
4494+ continue;
4495+ case SMB_ACL_MASK:
4496+ continue;
4497+ }
4498+
0f6733d8
WD
4499+ if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4500+ acl_length += sizeof(struct acl_entry);
252945ef 4501+ file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
0f6733d8
WD
4502+ if(file_acl_temp == NULL) {
4503+ SAFE_FREE(file_acl);
fa26e11c
WD
4504+ errno = ENOMEM;
4505+ DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
0f6733d8 4506+ return(-1);
fa26e11c
WD
4507+ }
4508+
4509+ memcpy(file_acl_temp,file_acl,file_acl->acl_len);
0f6733d8 4510+ SAFE_FREE(file_acl);
fa26e11c
WD
4511+ file_acl = file_acl_temp;
4512+ }
4513+
4514+ acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
0f6733d8 4515+ file_acl->acl_len += sizeof(struct acl_entry);
fa26e11c
WD
4516+ acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4517+ acl_entry->ace_access = acl_entry_link->entryp->ace_access;
0f6733d8 4518+
fa26e11c 4519+ /* In order to use this, we'll need to wait until we can get denies */
0f6733d8 4520+ /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
fa26e11c 4521+ acl_entry->ace_type = ACC_SPECIFY; */
0f6733d8 4522+
fa26e11c 4523+ acl_entry->ace_type = ACC_SPECIFY;
0f6733d8 4524+
fa26e11c 4525+ ace_id = acl_entry->ace_id;
0f6733d8 4526+
fa26e11c
WD
4527+ ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4528+ DEBUG(10,("The id type is %d\n",ace_id->id_type));
4529+ ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
0f6733d8
WD
4530+ memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4531+ memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
fa26e11c 4532+ }
0f6733d8 4533+
fa26e11c
WD
4534+ rc = fchacl(fd,file_acl,file_acl->acl_len);
4535+ DEBUG(10,("errno is %d\n",errno));
4536+ DEBUG(10,("return code is %d\n",rc));
0f6733d8 4537+ SAFE_FREE(file_acl);
fa26e11c 4538+ DEBUG(10,("Exiting sys_acl_set_fd\n"));
0f6733d8 4539+ return(rc);
fa26e11c
WD
4540+}
4541+
0f6733d8 4542+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
4543+{
4544+ /* AIX has no default ACL */
4545+ return 0;
4546+}
4547+
0f6733d8 4548+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 4549+{
0f6733d8 4550+ return(*permset & perm);
fa26e11c
WD
4551+}
4552+
0f6733d8 4553+int sys_acl_free_text(char *text)
fa26e11c 4554+{
0f6733d8 4555+ return(0);
fa26e11c
WD
4556+}
4557+
0f6733d8 4558+int sys_acl_free_acl(SMB_ACL_T posix_acl)
fa26e11c
WD
4559+{
4560+ struct acl_entry_link *acl_entry_link;
4561+
0f6733d8
WD
4562+ for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
4563+ SAFE_FREE(acl_entry_link->prevp->entryp);
4564+ SAFE_FREE(acl_entry_link->prevp);
fa26e11c
WD
4565+ }
4566+
0f6733d8
WD
4567+ SAFE_FREE(acl_entry_link->prevp->entryp);
4568+ SAFE_FREE(acl_entry_link->prevp);
4569+ SAFE_FREE(acl_entry_link->entryp);
4570+ SAFE_FREE(acl_entry_link);
4571+
4572+ return(0);
fa26e11c
WD
4573+}
4574+
0f6733d8 4575+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c 4576+{
0f6733d8 4577+ return(0);
fa26e11c
WD
4578+}
4579+
4580+#else /* No ACLs. */
4581+
4df546eb 4582+int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
fa26e11c
WD
4583+{
4584+ errno = ENOSYS;
4585+ return -1;
4586+}
4587+
4df546eb 4588+int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
fa26e11c
WD
4589+{
4590+ errno = ENOSYS;
4591+ return -1;
4592+}
4593+
4df546eb 4594+int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
fa26e11c
WD
4595+{
4596+ errno = ENOSYS;
4597+ return -1;
4598+}
4599+
4df546eb 4600+void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
fa26e11c
WD
4601+{
4602+ errno = ENOSYS;
4603+ return NULL;
4604+}
4605+
4df546eb 4606+SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
fa26e11c
WD
4607+{
4608+ errno = ENOSYS;
5ca9317d 4609+ return (SMB_ACL_T)NULL;
fa26e11c
WD
4610+}
4611+
4df546eb 4612+SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
fa26e11c
WD
4613+{
4614+ errno = ENOSYS;
5ca9317d 4615+ return (SMB_ACL_T)NULL;
fa26e11c
WD
4616+}
4617+
4df546eb 4618+int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
fa26e11c
WD
4619+{
4620+ errno = ENOSYS;
4621+ return -1;
4622+}
4623+
4df546eb 4624+int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
fa26e11c
WD
4625+{
4626+ errno = ENOSYS;
4627+ return -1;
4628+}
4629+
0f6733d8 4630+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
4631+{
4632+ errno = ENOSYS;
0f6733d8 4633+ return (permset & perm) ? 1 : 0;
fa26e11c
WD
4634+}
4635+
4df546eb 4636+char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
fa26e11c
WD
4637+{
4638+ errno = ENOSYS;
4639+ return NULL;
4640+}
4641+
4df546eb 4642+int sys_acl_free_text(UNUSED(char *text))
fa26e11c
WD
4643+{
4644+ errno = ENOSYS;
4645+ return -1;
4646+}
4647+
4df546eb 4648+SMB_ACL_T sys_acl_init(UNUSED(int count))
fa26e11c
WD
4649+{
4650+ errno = ENOSYS;
4651+ return NULL;
4652+}
4653+
4df546eb 4654+int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
fa26e11c
WD
4655+{
4656+ errno = ENOSYS;
4657+ return -1;
4658+}
4659+
4df546eb 4660+int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
4661+{
4662+ errno = ENOSYS;
4663+ return -1;
4664+}
4665+
4df546eb 4666+int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
fa26e11c
WD
4667+{
4668+ errno = ENOSYS;
4669+ return -1;
4670+}
4671+
4df546eb 4672+int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
fa26e11c
WD
4673+{
4674+ errno = ENOSYS;
4675+ return -1;
4676+}
4677+
4df546eb 4678+int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
4679+{
4680+ errno = ENOSYS;
4681+ return -1;
4682+}
4683+
4df546eb 4684+int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
4685+{
4686+ errno = ENOSYS;
4687+ return -1;
4688+}
4689+
4df546eb 4690+int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
4691+{
4692+ errno = ENOSYS;
4693+ return -1;
4694+}
4695+
4df546eb 4696+int sys_acl_delete_def_file(UNUSED(const char *name))
fa26e11c
WD
4697+{
4698+ errno = ENOSYS;
4699+ return -1;
4700+}
4701+
4df546eb 4702+int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
fa26e11c
WD
4703+{
4704+ errno = ENOSYS;
4705+ return -1;
4706+}
4707+
4df546eb 4708+int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
4709+{
4710+ errno = ENOSYS;
4711+ return -1;
4712+}
4713+
4714+#endif /* No ACLs. */
252945ef
WD
4715+
4716+/************************************************************************
4717+ Deliberately outside the ACL defines. Return 1 if this is a "no acls"
4718+ errno, 0 if not.
4719+************************************************************************/
4720+
4721+int no_acl_syscall_error(int err)
4722+{
4723+#if defined(ENOSYS)
4724+ if (err == ENOSYS) {
4725+ return 1;
4726+ }
4727+#endif
4728+#if defined(ENOTSUP)
4729+ if (err == ENOTSUP) {
4730+ return 1;
4731+ }
4732+#endif
4733+ return 0;
4734+}
9a7eef96
WD
4735--- old/lib/sysacls.h
4736+++ new/lib/sysacls.h
252945ef
WD
4737@@ -0,0 +1,28 @@
4738+#define SMB_MALLOC(cnt) new_array(char, cnt)
4739+#define SMB_MALLOC_P(obj) new_array(obj, 1)
4740+#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
4741+#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
5ca9317d 4742+#define slprintf snprintf
0f6733d8
WD
4743+
4744+int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
4745+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
4746+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
4747+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
4748+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
4749+SMB_ACL_T sys_acl_get_fd(int fd);
4750+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
4751+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4752+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4753+char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
4754+SMB_ACL_T sys_acl_init(int count);
4755+int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
4756+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
4757+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
4758+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
4759+int sys_acl_valid(SMB_ACL_T theacl);
4760+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
4761+int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
4762+int sys_acl_delete_def_file(const char *name);
4763+int sys_acl_free_text(char *text);
4764+int sys_acl_free_acl(SMB_ACL_T the_acl);
4765+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
9a7eef96
WD
4766--- old/mkproto.awk
4767+++ new/mkproto.awk
0f6733d8
WD
4768@@ -58,7 +58,7 @@ BEGIN {
4769 next;
4770 }
4771
bc5988ec
WD
4772-!/^OFF_T|^size_t|^off_t|^pid_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
4773+!/^OFF_T|^size_t|^off_t|^pid_t|^id_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
0f6733d8
WD
4774 next;
4775 }
4776
9a7eef96
WD
4777--- old/options.c
4778+++ new/options.c
2578e2b6 4779@@ -45,6 +45,7 @@ int copy_dirlinks = 0;
0f6733d8
WD
4780 int copy_links = 0;
4781 int preserve_links = 0;
4782 int preserve_hard_links = 0;
4783+int preserve_acls = 0;
4784 int preserve_perms = 0;
90fa6d68 4785 int preserve_executability = 0;
0f6733d8 4786 int preserve_devices = 0;
2578e2b6 4787@@ -194,6 +195,7 @@ static void print_rsync_version(enum log
0f6733d8
WD
4788 char const *got_socketpair = "no ";
4789 char const *have_inplace = "no ";
4790 char const *hardlinks = "no ";
4791+ char const *acls = "no ";
4792 char const *links = "no ";
4793 char const *ipv6 = "no ";
4794 STRUCT_STAT *dumstat;
2578e2b6 4795@@ -210,6 +212,10 @@ static void print_rsync_version(enum log
0f6733d8
WD
4796 hardlinks = "";
4797 #endif
4798
09fb8f03 4799+#ifdef SUPPORT_ACLS
0f6733d8
WD
4800+ acls = "";
4801+#endif
4802+
09fb8f03 4803 #ifdef SUPPORT_LINKS
0f6733d8
WD
4804 links = "";
4805 #endif
2578e2b6 4806@@ -223,9 +229,9 @@ static void print_rsync_version(enum log
3b05e91f 4807 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
0f6733d8
WD
4808 rprintf(f, "<http://rsync.samba.org/>\n");
4809 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
bc5988ec
WD
4810- "%shard links, %ssymlinks, batchfiles,\n",
4811+ "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
0f6733d8
WD
4812 (int) (sizeof (OFF_T) * 8),
4813- got_socketpair, hardlinks, links);
4814+ got_socketpair, hardlinks, acls, links);
4815
4816 /* Note that this field may not have type ino_t. It depends
4817 * on the complicated interaction between largefile feature
2578e2b6
WD
4818@@ -295,6 +301,9 @@ void usage(enum logcode F)
4819 rprintf(F," -H, --hard-links preserve hard links\n");
0f6733d8 4820 rprintf(F," -p, --perms preserve permissions\n");
90fa6d68 4821 rprintf(F," -E, --executability preserve the file's executability\n");
25d385b9 4822+#ifdef SUPPORT_ACLS
0f6733d8 4823+ rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
25d385b9 4824+#endif
90fa6d68
WD
4825 rprintf(F," --chmod=CHMOD change destination permissions\n");
4826 rprintf(F," -o, --owner preserve owner (super-user only)\n");
0f6733d8 4827 rprintf(F," -g, --group preserve group\n");
2578e2b6 4828@@ -410,6 +419,9 @@ static struct poptOption long_options[]
489b0a72
WD
4829 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
4830 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
90fa6d68 4831 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
489b0a72
WD
4832+ {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
4833+ {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4834+ {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4835 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
4836 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
4837 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
2578e2b6 4838@@ -1068,6 +1080,23 @@ int parse_arguments(int *argc, const cha
3b05e91f
WD
4839 usage(FINFO);
4840 exit_cleanup(0);
0f6733d8
WD
4841
4842+ case 'A':
4df546eb 4843+#ifdef SUPPORT_ACLS
25d385b9 4844+ preserve_acls = preserve_perms = 1;
489b0a72 4845+ break;
0f6733d8
WD
4846+#else
4847+ /* FIXME: this should probably be ignored with a
4848+ * warning and then countermeasures taken to
4849+ * restrict group and other access in the presence
4850+ * of any more restrictive ACLs, but this is safe
4851+ * for now */
4852+ snprintf(err_buf,sizeof(err_buf),
4853+ "ACLs are not supported on this %s\n",
4854+ am_server ? "server" : "client");
4855+ return 0;
25d385b9 4856+#endif
0f6733d8
WD
4857+
4858+
4859 default:
4860 /* A large opt value means that set_refuse_options()
27a7053c 4861 * turned this option off. */
2578e2b6 4862@@ -1511,6 +1540,10 @@ void server_options(char **args,int *arg
c7bc7375 4863
0f6733d8
WD
4864 if (preserve_hard_links)
4865 argstr[x++] = 'H';
25d385b9 4866+#ifdef SUPPORT_ACLS
0f6733d8
WD
4867+ if (preserve_acls)
4868+ argstr[x++] = 'A';
25d385b9 4869+#endif
0f6733d8
WD
4870 if (preserve_uid)
4871 argstr[x++] = 'o';
4872 if (preserve_gid)
9a7eef96
WD
4873--- old/receiver.c
4874+++ new/receiver.c
2578e2b6 4875@@ -46,6 +46,7 @@ extern int keep_partial;
1a2fa68f
WD
4876 extern int checksum_seed;
4877 extern int inplace;
4878 extern int delay_updates;
4879+extern mode_t orig_umask;
4880 extern struct stats stats;
4881 extern char *log_format;
4882 extern char *tmpdir;
2578e2b6 4883@@ -344,6 +345,10 @@ int recv_files(int f_in, struct file_lis
26c810d8
WD
4884 int itemizing = am_daemon ? daemon_log_format_has_i
4885 : !am_server && log_format_has_i;
4886 int max_phase = protocol_version >= 29 ? 2 : 1;
4887+ int dflt_perms = (ACCESSPERMS & ~orig_umask);
5c8b4e6e 4888+#ifdef SUPPORT_ACLS
26c810d8 4889+ char *parent_dirname = "";
5c8b4e6e 4890+#endif
26c810d8
WD
4891 int i, recv_ok;
4892
4893 if (verbose > 2)
2578e2b6 4894@@ -541,7 +546,16 @@ int recv_files(int f_in, struct file_lis
90fa6d68
WD
4895 * mode based on the local permissions and some heuristics. */
4896 if (!preserve_perms) {
4897 int exists = fd1 != -1;
4898- file->mode = dest_mode(file->mode, st.st_mode, exists);
26c810d8 4899+#ifdef SUPPORT_ACLS
5c8b4e6e
WD
4900+ char *dn = file->dirname ? file->dirname : ".";
4901+ if (parent_dirname != dn
4902+ && strcmp(parent_dirname, dn) != 0) {
4903+ dflt_perms = default_perms_for_dir(dn);
4904+ parent_dirname = dn;
26c810d8
WD
4905+ }
4906+#endif
4907+ file->mode = dest_mode(file->mode, st.st_mode,
4908+ dflt_perms, exists);
90fa6d68
WD
4909 }
4910
4911 /* We now check to see if we are writing file "inplace" */
9a7eef96
WD
4912--- old/rsync.c
4913+++ new/rsync.c
c769ea2c 4914@@ -101,7 +101,8 @@ void free_sums(struct sum_struct *s)
90fa6d68
WD
4915
4916 /* This is only called when we aren't preserving permissions. Figure out what
4917 * the permissions should be and return them merged back into the mode. */
02929d4c
WD
4918-mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int exists)
4919+mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int dflt_perms,
26c810d8 4920+ int exists)
90fa6d68 4921 {
25d385b9 4922 /* If the file already exists, we'll return the local permissions,
90fa6d68 4923 * possibly tweaked by the --executability option. */
c769ea2c 4924@@ -116,7 +117,7 @@ mode_t dest_mode(mode_t flist_mode, mode
02929d4c 4925 cur_mode |= (cur_mode & 0444) >> 2;
90fa6d68 4926 }
26c810d8 4927 } else
02929d4c 4928- cur_mode = flist_mode & ACCESSPERMS & ~orig_umask;
81ddc4dc 4929+ cur_mode = flist_mode & ACCESSPERMS & dflt_perms;
c769ea2c
WD
4930 if (daemon_chmod_modes && !S_ISLNK(flist_mode))
4931 cur_mode = tweak_mode(cur_mode, daemon_chmod_modes);
02929d4c 4932 return (flist_mode & ~CHMOD_BITS) | (cur_mode & CHMOD_BITS);
36aa3171
WD
4933@@ -203,6 +204,13 @@ int set_file_attrs(char *fname, struct f
4934 updated = 1;
0f6733d8 4935 }
0f6733d8 4936
e6a7303b 4937+#ifdef SUPPORT_ACLS
36aa3171
WD
4938+ /* It's OK to call set_acl() now, even for a dir, as the generator
4939+ * will enable owner-writability using chmod, if necessary. */
e6a7303b 4940+ if (set_acl(fname, file) == 0)
81ddc4dc 4941+ updated = 1;
e6a7303b 4942+#endif
0f6733d8 4943+
36aa3171
WD
4944 #ifdef HAVE_CHMOD
4945 if ((st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
4946 int ret = do_chmod(fname, file->mode);
9a7eef96
WD
4947--- old/rsync.h
4948+++ new/rsync.h
e6a7303b 4949@@ -658,6 +658,20 @@ struct chmod_mode_struct;
bc5988ec
WD
4950
4951 #define UNUSED(x) x __attribute__((__unused__))
0f6733d8 4952
4df546eb
WD
4953+#if HAVE_POSIX_ACLS|HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|\
4954+ HAVE_HPUX_ACLS|HAVE_IRIX_ACLS|HAVE_AIX_ACLS|HAVE_TRU64_ACLS
4955+#define SUPPORT_ACLS 1
4956+#endif
0f6733d8 4957+
4df546eb
WD
4958+#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
4959+#define ACLS_NEED_MASK 1
4960+#endif
0f6733d8 4961+
e6a7303b 4962+#if defined SUPPORT_ACLS && defined HAVE_SYS_ACL_H
0f6733d8
WD
4963+#include <sys/acl.h>
4964+#endif
0f6733d8
WD
4965+#include "smb_acls.h"
4966+
4967 #include "proto.h"
4968
4969 /* We have replacement versions of these if they're missing. */
9a7eef96
WD
4970--- old/rsync.yo
4971+++ new/rsync.yo
2578e2b6
WD
4972@@ -321,6 +321,7 @@ to the detailed description below for a
4973 -H, --hard-links preserve hard links
0f6733d8 4974 -p, --perms preserve permissions
90fa6d68
WD
4975 -E, --executability preserve executability
4976+ -A, --acls preserve ACLs (implies -p) [non-standard]
4977 --chmod=CHMOD change destination permissions
4978 -o, --owner preserve owner (super-user only)
0f6733d8 4979 -g, --group preserve group
2578e2b6 4980@@ -742,7 +743,9 @@ quote(itemize(
90fa6d68
WD
4981 permissions, though the bf(--executability) option might change just
4982 the execute permission for the file.
03baf100
WD
4983 it() New files get their "normal" permission bits set to the source
4984- file's permissions masked with the receiving end's umask setting, and
4985+ file's permissions masked with the receiving directory's default
4986+ permissions (either the receiving process's umask, or the permissions
4987+ specified via the destination directory's default ACL), and
4988 their special permission bits disabled except in the case where a new
4989 directory inherits a setgid bit from its parent directory.
90fa6d68 4990 ))
2578e2b6 4991@@ -773,9 +776,11 @@ The preservation of the destination's se
03baf100
WD
4992 directories when bf(--perms) is off was added in rsync 2.6.7. Older rsync
4993 versions erroneously preserved the three special permission bits for
4994 newly-created files when bf(--perms) was off, while overriding the
4995-destination's setgid bit setting on a newly-created directory. (Keep in
4996-mind that it is the version of the receiving rsync that affects this
4997-behavior.)
4998+destination's setgid bit setting on a newly-created directory. Default ACL
4999+observance was added to the ACL patch for rsync 2.6.7, so older (or
5000+non-ACL-enabled) rsyncs use the umask even if default ACLs are present.
5001+(Keep in mind that it is the version of the receiving rsync that affects
5002+these behaviors.)
90fa6d68 5003
90fa6d68
WD
5004 dit(bf(-E, --executability)) This option causes rsync to preserve the
5005 executability (or non-executability) of regular files when bf(--perms) is
2578e2b6 5006@@ -793,6 +798,10 @@ quote(itemize(
90fa6d68
WD
5007
5008 If bf(--perms) is enabled, this option is ignored.
5009
5010+dit(bf(-A, --acls)) This option causes rsync to update the destination
5011+ACLs to be the same as the source ACLs. This nonstandard option only
5012+works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
5013+
5014 dit(bf(--chmod)) This option tells rsync to apply one or more
5015 comma-separated "chmod" strings to the permission of the files in the
5016 transfer. The resulting value is treated as though it was the permissions
9a7eef96
WD
5017--- old/smb_acls.h
5018+++ new/smb_acls.h
0f6733d8
WD
5019@@ -0,0 +1,277 @@
5020+/*
5021+ Unix SMB/Netbios implementation.
5022+ Version 2.2.x
5023+ Portable SMB ACL interface
5024+ Copyright (C) Jeremy Allison 2000
5025+
5026+ This program is free software; you can redistribute it and/or modify
5027+ it under the terms of the GNU General Public License as published by
5028+ the Free Software Foundation; either version 2 of the License, or
5029+ (at your option) any later version.
5030+
5031+ This program is distributed in the hope that it will be useful,
5032+ but WITHOUT ANY WARRANTY; without even the implied warranty of
5033+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5034+ GNU General Public License for more details.
5035+
5036+ You should have received a copy of the GNU General Public License
5037+ along with this program; if not, write to the Free Software
5038+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
5039+*/
5040+
5041+#ifndef _SMB_ACLS_H
5042+#define _SMB_ACLS_H
5043+
e6a7303b 5044+#if defined HAVE_POSIX_ACLS
0f6733d8
WD
5045+
5046+/* This is an identity mapping (just remove the SMB_). */
5047+
5048+#define SMB_ACL_TAG_T acl_tag_t
5049+#define SMB_ACL_TYPE_T acl_type_t
5050+#define SMB_ACL_PERMSET_T acl_permset_t
5051+#define SMB_ACL_PERM_T acl_perm_t
5052+#define SMB_ACL_READ ACL_READ
5053+#define SMB_ACL_WRITE ACL_WRITE
5054+#define SMB_ACL_EXECUTE ACL_EXECUTE
5055+
5056+/* Types of ACLs. */
5057+#define SMB_ACL_USER ACL_USER
5058+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5059+#define SMB_ACL_GROUP ACL_GROUP
5060+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5061+#define SMB_ACL_OTHER ACL_OTHER
5062+#define SMB_ACL_MASK ACL_MASK
5063+
5064+#define SMB_ACL_T acl_t
5065+
5066+#define SMB_ACL_ENTRY_T acl_entry_t
5067+
5068+#define SMB_ACL_FIRST_ENTRY ACL_FIRST_ENTRY
5069+#define SMB_ACL_NEXT_ENTRY ACL_NEXT_ENTRY
5070+
5071+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5072+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5073+
e6a7303b 5074+#elif defined HAVE_TRU64_ACLS
0f6733d8
WD
5075+
5076+/* This is for DEC/Compaq Tru64 UNIX */
5077+
5078+#define SMB_ACL_TAG_T acl_tag_t
5079+#define SMB_ACL_TYPE_T acl_type_t
5080+#define SMB_ACL_PERMSET_T acl_permset_t
5081+#define SMB_ACL_PERM_T acl_perm_t
5082+#define SMB_ACL_READ ACL_READ
5083+#define SMB_ACL_WRITE ACL_WRITE
5084+#define SMB_ACL_EXECUTE ACL_EXECUTE
5085+
5086+/* Types of ACLs. */
5087+#define SMB_ACL_USER ACL_USER
5088+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5089+#define SMB_ACL_GROUP ACL_GROUP
5090+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5091+#define SMB_ACL_OTHER ACL_OTHER
5092+#define SMB_ACL_MASK ACL_MASK
5093+
5094+#define SMB_ACL_T acl_t
5095+
5096+#define SMB_ACL_ENTRY_T acl_entry_t
5097+
5098+#define SMB_ACL_FIRST_ENTRY 0
5099+#define SMB_ACL_NEXT_ENTRY 1
5100+
5101+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5102+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5103+
e6a7303b 5104+#elif defined HAVE_UNIXWARE_ACLS || defined HAVE_SOLARIS_ACLS
0f6733d8
WD
5105+/*
5106+ * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
5107+ * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
5108+ */
5109+
5110+/* SVR4.2 ES/MP ACLs */
5111+typedef int SMB_ACL_TAG_T;
5112+typedef int SMB_ACL_TYPE_T;
5113+typedef ushort *SMB_ACL_PERMSET_T;
5114+typedef ushort SMB_ACL_PERM_T;
5115+#define SMB_ACL_READ 4
5116+#define SMB_ACL_WRITE 2
5117+#define SMB_ACL_EXECUTE 1
5118+
5119+/* Types of ACLs. */
5120+#define SMB_ACL_USER USER
5121+#define SMB_ACL_USER_OBJ USER_OBJ
5122+#define SMB_ACL_GROUP GROUP
5123+#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5124+#define SMB_ACL_OTHER OTHER_OBJ
5125+#define SMB_ACL_MASK CLASS_OBJ
5126+
5127+typedef struct SMB_ACL_T {
5128+ int size;
5129+ int count;
5130+ int next;
5131+ struct acl acl[1];
5132+} *SMB_ACL_T;
5133+
5134+typedef struct acl *SMB_ACL_ENTRY_T;
5135+
5136+#define SMB_ACL_FIRST_ENTRY 0
5137+#define SMB_ACL_NEXT_ENTRY 1
5138+
5139+#define SMB_ACL_TYPE_ACCESS 0
5140+#define SMB_ACL_TYPE_DEFAULT 1
5141+
e6a7303b 5142+#elif defined HAVE_HPUX_ACLS
0f6733d8
WD
5143+
5144+/*
5145+ * Based on the Solaris & UnixWare code.
5146+ */
5147+
5148+#undef GROUP
5149+#include <sys/aclv.h>
5150+
5151+/* SVR4.2 ES/MP ACLs */
5152+typedef int SMB_ACL_TAG_T;
5153+typedef int SMB_ACL_TYPE_T;
5154+typedef ushort *SMB_ACL_PERMSET_T;
5155+typedef ushort SMB_ACL_PERM_T;
5156+#define SMB_ACL_READ 4
5157+#define SMB_ACL_WRITE 2
5158+#define SMB_ACL_EXECUTE 1
5159+
5160+/* Types of ACLs. */
5161+#define SMB_ACL_USER USER
5162+#define SMB_ACL_USER_OBJ USER_OBJ
5163+#define SMB_ACL_GROUP GROUP
5164+#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5165+#define SMB_ACL_OTHER OTHER_OBJ
5166+#define SMB_ACL_MASK CLASS_OBJ
5167+
5168+typedef struct SMB_ACL_T {
5169+ int size;
5170+ int count;
5171+ int next;
5172+ struct acl acl[1];
5173+} *SMB_ACL_T;
5174+
5175+typedef struct acl *SMB_ACL_ENTRY_T;
5176+
5177+#define SMB_ACL_FIRST_ENTRY 0
5178+#define SMB_ACL_NEXT_ENTRY 1
5179+
5180+#define SMB_ACL_TYPE_ACCESS 0
5181+#define SMB_ACL_TYPE_DEFAULT 1
5182+
e6a7303b 5183+#elif defined HAVE_IRIX_ACLS
0f6733d8
WD
5184+
5185+#define SMB_ACL_TAG_T acl_tag_t
5186+#define SMB_ACL_TYPE_T acl_type_t
5187+#define SMB_ACL_PERMSET_T acl_permset_t
5188+#define SMB_ACL_PERM_T acl_perm_t
5189+#define SMB_ACL_READ ACL_READ
5190+#define SMB_ACL_WRITE ACL_WRITE
5191+#define SMB_ACL_EXECUTE ACL_EXECUTE
5192+
5193+/* Types of ACLs. */
5194+#define SMB_ACL_USER ACL_USER
5195+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5196+#define SMB_ACL_GROUP ACL_GROUP
5197+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5198+#define SMB_ACL_OTHER ACL_OTHER_OBJ
5199+#define SMB_ACL_MASK ACL_MASK
5200+
5201+typedef struct SMB_ACL_T {
5202+ int next;
5203+ BOOL freeaclp;
5204+ struct acl *aclp;
5205+} *SMB_ACL_T;
5206+
5207+#define SMB_ACL_ENTRY_T acl_entry_t
5208+
5209+#define SMB_ACL_FIRST_ENTRY 0
5210+#define SMB_ACL_NEXT_ENTRY 1
5211+
5212+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5213+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5214+
e6a7303b 5215+#elif defined HAVE_AIX_ACLS
0f6733d8
WD
5216+
5217+/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
5218+
5219+#include "/usr/include/acl.h"
5220+
5221+typedef uint *SMB_ACL_PERMSET_T;
5222+
5223+struct acl_entry_link{
5224+ struct acl_entry_link *prevp;
5225+ struct new_acl_entry *entryp;
5226+ struct acl_entry_link *nextp;
5227+ int count;
5228+};
5229+
5230+struct new_acl_entry{
5231+ unsigned short ace_len;
5232+ unsigned short ace_type;
5233+ unsigned int ace_access;
5234+ struct ace_id ace_id[1];
5235+};
5236+
5237+#define SMB_ACL_ENTRY_T struct new_acl_entry*
5238+#define SMB_ACL_T struct acl_entry_link*
5239+
5240+#define SMB_ACL_TAG_T unsigned short
5241+#define SMB_ACL_TYPE_T int
5242+#define SMB_ACL_PERM_T uint
5243+#define SMB_ACL_READ S_IRUSR
5244+#define SMB_ACL_WRITE S_IWUSR
5245+#define SMB_ACL_EXECUTE S_IXUSR
5246+
5247+/* Types of ACLs. */
5248+#define SMB_ACL_USER ACEID_USER
5249+#define SMB_ACL_USER_OBJ 3
5250+#define SMB_ACL_GROUP ACEID_GROUP
5251+#define SMB_ACL_GROUP_OBJ 4
5252+#define SMB_ACL_OTHER 5
5253+#define SMB_ACL_MASK 6
5254+
5255+
5256+#define SMB_ACL_FIRST_ENTRY 1
5257+#define SMB_ACL_NEXT_ENTRY 2
5258+
5259+#define SMB_ACL_TYPE_ACCESS 0
5260+#define SMB_ACL_TYPE_DEFAULT 1
5261+
5262+#else /* No ACLs. */
5263+
5264+/* No ACLS - fake it. */
5265+#define SMB_ACL_TAG_T int
5266+#define SMB_ACL_TYPE_T int
5267+#define SMB_ACL_PERMSET_T mode_t
5268+#define SMB_ACL_PERM_T mode_t
5269+#define SMB_ACL_READ S_IRUSR
5270+#define SMB_ACL_WRITE S_IWUSR
5271+#define SMB_ACL_EXECUTE S_IXUSR
5272+
5273+/* Types of ACLs. */
5274+#define SMB_ACL_USER 0
5275+#define SMB_ACL_USER_OBJ 1
5276+#define SMB_ACL_GROUP 2
5277+#define SMB_ACL_GROUP_OBJ 3
5278+#define SMB_ACL_OTHER 4
5279+#define SMB_ACL_MASK 5
5280+
5281+typedef struct SMB_ACL_T {
5282+ int dummy;
5283+} *SMB_ACL_T;
5284+
5285+typedef struct SMB_ACL_ENTRY_T {
5286+ int dummy;
5287+} *SMB_ACL_ENTRY_T;
5288+
5289+#define SMB_ACL_FIRST_ENTRY 0
5290+#define SMB_ACL_NEXT_ENTRY 1
5291+
5292+#define SMB_ACL_TYPE_ACCESS 0
5293+#define SMB_ACL_TYPE_DEFAULT 1
5294+
5295+#endif /* No ACLs. */
5296+#endif /* _SMB_ACLS_H */
9a7eef96
WD
5297--- old/testsuite/default-acls.test
5298+++ new/testsuite/default-acls.test
bb81ee98 5299@@ -0,0 +1,55 @@
90fa6d68
WD
5300+#! /bin/sh
5301+
5302+# This program is distributable under the terms of the GNU GPL see
5303+# COPYING).
5304+
5305+# Test that rsync obeys default ACLs. -- Matt McCutchen
5306+
5307+. $srcdir/testsuite/rsync.fns
5308+
25d385b9 5309+$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
90fa6d68
WD
5310+setfacl -dm u::rwx,g::---,o::--- "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
5311+
90fa6d68 5312+# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
25d385b9 5313+testit() {
90fa6d68
WD
5314+ todir="$scratchdir/$1"
5315+ mkdir "$todir"
25d385b9 5316+ # FIXME This doesn't work on solaris...
90fa6d68 5317+ setfacl -k "$todir"
90fa6d68
WD
5318+ [ "$2" ] && setfacl -dm "$2" "$todir"
5319+ # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
5320+ # even though the directory itself is outside the transfer
25d385b9 5321+ $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
0251480d 5322+ check_perms "$todir/to" $4 "Target $1"
25d385b9 5323+ check_perms "$todir/to/dir" $4 "Target $1"
0251480d
WD
5324+ check_perms "$todir/to/file" $3 "Target $1"
5325+ check_perms "$todir/to/program" $4 "Target $1"
90fa6d68
WD
5326+ # Make sure get_local_name doesn't mess us up when transferring only one file
5327+ $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
0251480d 5328+ check_perms "$todir/to/anotherfile" $3 "Target $1"
bb81ee98 5329+ # Make sure we obey default ACLs when not transferring a regular file
2578e2b6 5330+ $RSYNC -rvv "$scratchdir/dir/" "$todir/to/anotherdir/"
bb81ee98 5331+ check_perms "$todir/to/anotherdir" $4 "Target $1"
90fa6d68
WD
5332+}
5333+
25d385b9 5334+mkdir "$scratchdir/dir"
0251480d 5335+echo "File!" >"$scratchdir/file"
25d385b9
WD
5336+echo "#!/bin/sh" >"$scratchdir/program"
5337+chmod 777 "$scratchdir/dir"
0251480d
WD
5338+chmod 666 "$scratchdir/file"
5339+chmod 777 "$scratchdir/program"
5340+
90fa6d68
WD
5341+# Test some target directories
5342+umask 0077
0251480d
WD
5343+testit da777 u::rwx,g::rwx,o::rwx rw-rw-rw- rwxrwxrwx
5344+testit da775 u::rwx,g::rwx,o::r-x rw-rw-r-- rwxrwxr-x
5345+testit da750 u::rwx,g::r-x,o::--- rw-r----- rwxr-x---
5346+testit da770mask u::rwx,g::---,m::rwx,o::--- rw-rw---- rwxrwx---
5347+testit noda1 '' rw------- rwx------
90fa6d68 5348+umask 0000
0251480d 5349+testit noda2 '' rw-rw-rw- rwxrwxrwx
90fa6d68 5350+umask 0022
0251480d 5351+testit noda3 '' rw-r--r-- rwxr-xr-x
90fa6d68
WD
5352+
5353+# Hooray
5354+exit 0
9a7eef96
WD
5355--- old/uidlist.c
5356+++ new/uidlist.c
fa26e11c
WD
5357@@ -34,6 +34,7 @@
5358 extern int verbose;
5359 extern int preserve_uid;
5360 extern int preserve_gid;
5361+extern int preserve_acls;
5362 extern int numeric_ids;
5363 extern int am_root;
5364
5365@@ -274,7 +275,7 @@ void send_uid_list(int f)
5366 if (numeric_ids)
5367 return;
5368
5369- if (preserve_uid) {
5370+ if (preserve_uid || preserve_acls) {
5371 int len;
5372 /* we send sequences of uid/byte-length/name */
5373 for (list = uidlist; list; list = list->next) {
5374@@ -291,7 +292,7 @@ void send_uid_list(int f)
5375 write_int(f, 0);
5376 }
5377
5378- if (preserve_gid) {
5379+ if (preserve_gid || preserve_acls) {
5380 int len;
5381 for (list = gidlist; list; list = list->next) {
5382 if (!list->name)
5383@@ -312,7 +313,7 @@ void recv_uid_list(int f, struct file_li
5384 int id, i;
5385 char *name;
5386
5387- if (preserve_uid && !numeric_ids) {
5388+ if ((preserve_uid || preserve_acls) && !numeric_ids) {
5389 /* read the uid list */
5390 while ((id = read_int(f)) != 0) {
5391 int len = read_byte(f);
6849cd84
WD
5392@@ -324,7 +325,7 @@ void recv_uid_list(int f, struct file_li
5393 }
fa26e11c
WD
5394 }
5395
fa26e11c
WD
5396- if (preserve_gid && !numeric_ids) {
5397+ if ((preserve_gid || preserve_acls) && !numeric_ids) {
5398 /* read the gid list */
5399 while ((id = read_int(f)) != 0) {
5400 int len = read_byte(f);
6849cd84 5401@@ -336,6 +337,18 @@ void recv_uid_list(int f, struct file_li
fa26e11c
WD
5402 }
5403 }
125d7fca 5404
4edb99c8 5405+#ifdef SUPPORT_ACLS
fa26e11c
WD
5406+ if (preserve_acls && !numeric_ids) {
5407+ id_t id;
4edb99c8
WD
5408+ /* The enumerations don't return 0 except to flag the last
5409+ * entry, since uidlist doesn't munge 0 anyway. */
5410+ while ((id = next_acl_uid(flist)) != 0)
fa26e11c 5411+ acl_uid_map(match_uid(id));
4edb99c8 5412+ while ((id = next_acl_gid(flist)) != 0)
fa26e11c
WD
5413+ acl_gid_map(match_gid(id));
5414+ }
5415+#endif /* SUPPORT_ACLS */
125d7fca 5416+
90fa6d68 5417 /* Now convert all the uids/gids from sender values to our values. */
125d7fca 5418 if (am_root && preserve_uid && !numeric_ids) {
90fa6d68 5419 for (i = 0; i < flist->count; i++)