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