- A couple more chunks of code in options.c needed to only be
[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
5c8b4e6e
WD
1400+++ generator.c 2006-02-03 05:44:04
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
5c8b4e6e 1432@@ -831,15 +832,20 @@ 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)) {
5c8b4e6e 1437+ if ((fuzzy_basis || !preserve_perms) && S_ISREG(file->mode)) {
26c810d8
WD
1438 char *dn = file->dirname ? file->dirname : ".";
1439- if (fuzzy_dirname != dn
1440- && strcmp(fuzzy_dirname, dn) != 0) {
1441+ if (parent_dirname != dn
1442+ && strcmp(parent_dirname, dn) != 0) {
1443 if (fuzzy_dirlist)
1444 flist_free(fuzzy_dirlist);
1445- fuzzy_dirlist = get_dirlist(dn, -1, 1);
1446+ if (fuzzy_basis)
1447+ fuzzy_dirlist = get_dirlist(dn, -1, 1);
1448+#ifdef SUPPORT_ACLS
5c8b4e6e 1449+ if (!preserve_perms)
26c810d8
WD
1450+ dflt_perms = default_perms_for_dir(dn);
1451+#endif
1452 }
1453- fuzzy_dirname = dn;
1454+ parent_dirname = dn;
1455 }
1456
1457 statret = link_stat(fname, &st,
5c8b4e6e 1458@@ -861,7 +867,8 @@ static void recv_generator(char *fname,
90fa6d68
WD
1459 if (!preserve_perms) {
1460 int exists = statret == 0
1461 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1462- file->mode = dest_mode(file->mode, st.st_mode, exists);
26c810d8
WD
1463+ file->mode = dest_mode(file->mode, st.st_mode, dflt_perms,
1464+ exists);
90fa6d68
WD
1465 }
1466
1467 if (S_ISDIR(file->mode)) {
5c8b4e6e 1468@@ -895,6 +902,10 @@ static void recv_generator(char *fname,
90fa6d68 1469 if (set_file_attrs(fname, file, statret ? NULL : &st, 0)
1d15f8d9 1470 && verbose && code && f_out != -1)
93ca4d27 1471 rprintf(code, "%s/\n", fname);
4df546eb 1472+#ifdef SUPPORT_ACLS
fa26e11c
WD
1473+ if (f_out == -1)
1474+ SET_ACL(fname, file);
1475+#endif
8f72c7ae 1476 if (delete_during && f_out != -1 && !phase && dry_run < 2
a7219d20 1477 && (file->flags & FLAG_DEL_HERE))
3b05e91f 1478 delete_in_dir(the_file_list, fname, file, &st);
5c8b4e6e 1479@@ -1330,6 +1341,8 @@ void generate_files(int f_out, struct fi
26c810d8
WD
1480 * notice that and let us know via the redo pipe (or its closing). */
1481 ignore_timeout = 1;
1482
1483+ dflt_perms = (ACCESSPERMS & ~orig_umask);
1484+
1485 for (i = 0; i < flist->count; i++) {
1486 struct file_struct *file = flist->files[i];
1487
252945ef
WD
1488--- orig/lib/sysacls.c 2005-05-16 23:27:53
1489+++ lib/sysacls.c 2005-05-16 23:27:53
1490@@ -0,0 +1,3242 @@
fa26e11c 1491+/*
5ca9317d 1492+ Unix SMB/CIFS implementation.
0f6733d8
WD
1493+ Samba system utilities for ACL support.
1494+ Copyright (C) Jeremy Allison 2000.
fa26e11c
WD
1495+
1496+ This program is free software; you can redistribute it and/or modify
1497+ it under the terms of the GNU General Public License as published by
1498+ the Free Software Foundation; either version 2 of the License, or
1499+ (at your option) any later version.
1500+
1501+ This program is distributed in the hope that it will be useful,
1502+ but WITHOUT ANY WARRANTY; without even the implied warranty of
1503+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1504+ GNU General Public License for more details.
1505+
1506+ You should have received a copy of the GNU General Public License
1507+ along with this program; if not, write to the Free Software
1508+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1509+*/
1510+
0f6733d8 1511+#include "rsync.h"
252945ef 1512+#include "sysacls.h" /****** ADDED ******/
fa26e11c 1513+
252945ef 1514+/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
0f6733d8
WD
1515+void SAFE_FREE(void *mem)
1516+{
1517+ if (mem)
1518+ free(mem);
1519+}
fa26e11c 1520+
5ca9317d
WD
1521+char *uidtoname(uid_t uid)
1522+{
1523+ static char idbuf[12];
1524+ struct passwd *pw;
1525+
1526+ if ((pw = getpwuid(uid)) == NULL) {
1527+ slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
1528+ return idbuf;
1529+ }
1530+ return pw->pw_name;
1531+}
252945ef 1532+/****** EXTRAS -- END ******/
5ca9317d 1533+
0f6733d8
WD
1534+/*
1535+ This file wraps all differing system ACL interfaces into a consistent
1536+ one based on the POSIX interface. It also returns the correct errors
1537+ for older UNIX systems that don't support ACLs.
fa26e11c 1538+
0f6733d8 1539+ The interfaces that each ACL implementation must support are as follows :
fa26e11c 1540+
0f6733d8
WD
1541+ int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1542+ int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1543+ int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
1544+ void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1545+ SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1546+ SMB_ACL_T sys_acl_get_fd(int fd)
1547+ int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
1548+ int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
1549+ char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
1550+ SMB_ACL_T sys_acl_init( int count)
1551+ int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1552+ int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1553+ int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1554+ int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1555+ int sys_acl_valid( SMB_ACL_T theacl )
1556+ int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1557+ int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1558+ int sys_acl_delete_def_file(const char *path)
fa26e11c 1559+
0f6733d8
WD
1560+ This next one is not POSIX complient - but we *have* to have it !
1561+ More POSIX braindamage.
fa26e11c 1562+
0f6733d8 1563+ int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 1564+
0f6733d8
WD
1565+ The generic POSIX free is the following call. We split this into
1566+ several different free functions as we may need to add tag info
1567+ to structures when emulating the POSIX interface.
fa26e11c 1568+
0f6733d8 1569+ int sys_acl_free( void *obj_p)
fa26e11c 1570+
0f6733d8 1571+ The calls we actually use are :
fa26e11c 1572+
0f6733d8
WD
1573+ int sys_acl_free_text(char *text) - free acl_to_text
1574+ int sys_acl_free_acl(SMB_ACL_T posix_acl)
1575+ int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
fa26e11c 1576+
0f6733d8 1577+*/
fa26e11c 1578+
0f6733d8 1579+#if defined(HAVE_POSIX_ACLS)
fa26e11c 1580+
0f6733d8 1581+/* Identity mapping - easy. */
fa26e11c 1582+
0f6733d8 1583+int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 1584+{
0f6733d8 1585+ return acl_get_entry( the_acl, entry_id, entry_p);
fa26e11c
WD
1586+}
1587+
0f6733d8 1588+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c 1589+{
0f6733d8 1590+ return acl_get_tag_type( entry_d, tag_type_p);
fa26e11c
WD
1591+}
1592+
0f6733d8 1593+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c 1594+{
0f6733d8 1595+ return acl_get_permset( entry_d, permset_p);
fa26e11c
WD
1596+}
1597+
0f6733d8 1598+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 1599+{
0f6733d8 1600+ return acl_get_qualifier( entry_d);
fa26e11c
WD
1601+}
1602+
0f6733d8 1603+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 1604+{
0f6733d8 1605+ return acl_get_file( path_p, type);
fa26e11c
WD
1606+}
1607+
1608+SMB_ACL_T sys_acl_get_fd(int fd)
1609+{
1610+ return acl_get_fd(fd);
1611+}
1612+
1613+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1614+{
1615+ return acl_clear_perms(permset);
1616+}
1617+
0f6733d8 1618+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
1619+{
1620+ return acl_add_perm(permset, perm);
1621+}
1622+
0f6733d8 1623+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
1624+{
1625+#if defined(HAVE_ACL_GET_PERM_NP)
0f6733d8
WD
1626+ /*
1627+ * Required for TrustedBSD-based ACL implementations where
fa26e11c 1628+ * non-POSIX.1e functions are denoted by a _np (non-portable)
0f6733d8
WD
1629+ * suffix.
1630+ */
fa26e11c
WD
1631+ return acl_get_perm_np(permset, perm);
1632+#else
1633+ return acl_get_perm(permset, perm);
1634+#endif
1635+}
1636+
0f6733d8 1637+char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
fa26e11c 1638+{
0f6733d8 1639+ return acl_to_text( the_acl, plen);
fa26e11c
WD
1640+}
1641+
0f6733d8 1642+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
1643+{
1644+ return acl_init(count);
1645+}
1646+
0f6733d8 1647+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
1648+{
1649+ return acl_create_entry(pacl, pentry);
1650+}
1651+
0f6733d8 1652+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
1653+{
1654+ return acl_set_tag_type(entry, tagtype);
1655+}
1656+
0f6733d8 1657+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
1658+{
1659+ return acl_set_qualifier(entry, qual);
1660+}
1661+
0f6733d8 1662+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
1663+{
1664+ return acl_set_permset(entry, permset);
1665+}
1666+
0f6733d8 1667+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c
WD
1668+{
1669+ return acl_valid(theacl);
1670+}
1671+
1672+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1673+{
1674+ return acl_set_file(name, acltype, theacl);
1675+}
1676+
0f6733d8 1677+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
1678+{
1679+ return acl_set_fd(fd, theacl);
1680+}
1681+
1682+int sys_acl_delete_def_file(const char *name)
1683+{
1684+ return acl_delete_def_file(name);
1685+}
1686+
1687+int sys_acl_free_text(char *text)
1688+{
1689+ return acl_free(text);
1690+}
1691+
0f6733d8 1692+int sys_acl_free_acl(SMB_ACL_T the_acl)
fa26e11c
WD
1693+{
1694+ return acl_free(the_acl);
1695+}
1696+
1f839b40 1697+int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
1698+{
1699+ return acl_free(qual);
1700+}
1701+
1702+#elif defined(HAVE_TRU64_ACLS)
0f6733d8
WD
1703+/*
1704+ * The interface to DEC/Compaq Tru64 UNIX ACLs
fa26e11c
WD
1705+ * is based on Draft 13 of the POSIX spec which is
1706+ * slightly different from the Draft 16 interface.
0f6733d8 1707+ *
fa26e11c
WD
1708+ * Also, some of the permset manipulation functions
1709+ * such as acl_clear_perm() and acl_add_perm() appear
1710+ * to be broken on Tru64 so we have to manipulate
0f6733d8
WD
1711+ * the permission bits in the permset directly.
1712+ */
1713+int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 1714+{
0f6733d8 1715+ SMB_ACL_ENTRY_T entry;
fa26e11c
WD
1716+
1717+ if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
1718+ return -1;
1719+ }
1720+
1721+ errno = 0;
1722+ if ((entry = acl_get_entry(the_acl)) != NULL) {
1723+ *entry_p = entry;
1724+ return 1;
1725+ }
1726+
1727+ return errno ? -1 : 0;
1728+}
1729+
0f6733d8 1730+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c 1731+{
0f6733d8 1732+ return acl_get_tag_type( entry_d, tag_type_p);
fa26e11c
WD
1733+}
1734+
0f6733d8 1735+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c 1736+{
0f6733d8 1737+ return acl_get_permset( entry_d, permset_p);
fa26e11c
WD
1738+}
1739+
0f6733d8 1740+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 1741+{
0f6733d8 1742+ return acl_get_qualifier( entry_d);
fa26e11c
WD
1743+}
1744+
0f6733d8 1745+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c
WD
1746+{
1747+ return acl_get_file((char *)path_p, type);
1748+}
1749+
0f6733d8 1750+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c
WD
1751+{
1752+ return acl_get_fd(fd, ACL_TYPE_ACCESS);
1753+}
1754+
0f6733d8 1755+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
fa26e11c 1756+{
0f6733d8 1757+ *permset = 0; /* acl_clear_perm() is broken on Tru64 */
fa26e11c
WD
1758+
1759+ return 0;
1760+}
1761+
0f6733d8 1762+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
1763+{
1764+ if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
1765+ errno = EINVAL;
1766+ return -1;
1767+ }
1768+
0f6733d8 1769+ *permset |= perm; /* acl_add_perm() is broken on Tru64 */
fa26e11c
WD
1770+
1771+ return 0;
1772+}
1773+
0f6733d8 1774+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
1775+{
1776+ return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
1777+}
1778+
0f6733d8 1779+char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
fa26e11c 1780+{
0f6733d8 1781+ return acl_to_text( the_acl, plen);
fa26e11c
WD
1782+}
1783+
0f6733d8 1784+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
1785+{
1786+ return acl_init(count);
1787+}
1788+
0f6733d8 1789+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
1790+{
1791+ SMB_ACL_ENTRY_T entry;
1792+
1793+ if ((entry = acl_create_entry(pacl)) == NULL) {
1794+ return -1;
1795+ }
1796+
1797+ *pentry = entry;
1798+ return 0;
1799+}
1800+
0f6733d8 1801+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
1802+{
1803+ return acl_set_tag_type(entry, tagtype);
1804+}
1805+
0f6733d8 1806+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
1807+{
1808+ return acl_set_qualifier(entry, qual);
1809+}
1810+
0f6733d8 1811+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
1812+{
1813+ return acl_set_permset(entry, permset);
1814+}
1815+
0f6733d8 1816+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c 1817+{
0f6733d8 1818+ acl_entry_t entry;
fa26e11c
WD
1819+
1820+ return acl_valid(theacl, &entry);
1821+}
1822+
0f6733d8 1823+int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
fa26e11c
WD
1824+{
1825+ return acl_set_file((char *)name, acltype, theacl);
1826+}
1827+
0f6733d8 1828+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
1829+{
1830+ return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
1831+}
1832+
0f6733d8 1833+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
1834+{
1835+ return acl_delete_def_file((char *)name);
1836+}
1837+
0f6733d8 1838+int sys_acl_free_text(char *text)
fa26e11c 1839+{
0f6733d8
WD
1840+ /*
1841+ * (void) cast and explicit return 0 are for DEC UNIX
1842+ * which just #defines acl_free_text() to be free()
1843+ */
fa26e11c
WD
1844+ (void) acl_free_text(text);
1845+ return 0;
1846+}
1847+
0f6733d8 1848+int sys_acl_free_acl(SMB_ACL_T the_acl)
fa26e11c
WD
1849+{
1850+ return acl_free(the_acl);
1851+}
1852+
0f6733d8 1853+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
1854+{
1855+ return acl_free_qualifier(qual, tagtype);
1856+}
1857+
1858+#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
1859+
0f6733d8
WD
1860+/*
1861+ * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
1862+ * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
1863+ */
fa26e11c 1864+
0f6733d8
WD
1865+/*
1866+ * Note that while this code implements sufficient functionality
fa26e11c
WD
1867+ * to support the sys_acl_* interfaces it does not provide all
1868+ * of the semantics of the POSIX ACL interfaces.
1869+ *
1870+ * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
1871+ * from a call to sys_acl_get_entry() should not be assumed to be
1872+ * valid after calling any of the following functions, which may
1873+ * reorder the entries in the ACL.
1874+ *
1875+ * sys_acl_valid()
1876+ * sys_acl_set_file()
1877+ * sys_acl_set_fd()
1878+ */
1879+
0f6733d8
WD
1880+/*
1881+ * The only difference between Solaris and UnixWare / OpenUNIX is
1882+ * that the #defines for the ACL operations have different names
1883+ */
fa26e11c
WD
1884+#if defined(HAVE_UNIXWARE_ACLS)
1885+
0f6733d8
WD
1886+#define SETACL ACL_SET
1887+#define GETACL ACL_GET
1888+#define GETACLCNT ACL_CNT
fa26e11c
WD
1889+
1890+#endif
1891+
1892+
0f6733d8 1893+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
1894+{
1895+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
1896+ errno = EINVAL;
1897+ return -1;
1898+ }
1899+
1900+ if (entry_p == NULL) {
1901+ errno = EINVAL;
1902+ return -1;
1903+ }
1904+
1905+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
1906+ acl_d->next = 0;
1907+ }
1908+
1909+ if (acl_d->next < 0) {
1910+ errno = EINVAL;
1911+ return -1;
1912+ }
1913+
1914+ if (acl_d->next >= acl_d->count) {
1915+ return 0;
1916+ }
1917+
1918+ *entry_p = &acl_d->acl[acl_d->next++];
1919+
1920+ return 1;
1921+}
1922+
0f6733d8 1923+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
1924+{
1925+ *type_p = entry_d->a_type;
1926+
1927+ return 0;
1928+}
1929+
0f6733d8 1930+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
1931+{
1932+ *permset_p = &entry_d->a_perm;
1933+
1934+ return 0;
1935+}
1936+
0f6733d8 1937+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
1938+{
1939+ if (entry_d->a_type != SMB_ACL_USER
1940+ && entry_d->a_type != SMB_ACL_GROUP) {
1941+ errno = EINVAL;
1942+ return NULL;
1943+ }
1944+
1945+ return &entry_d->a_id;
1946+}
1947+
0f6733d8
WD
1948+/*
1949+ * There is no way of knowing what size the ACL returned by
fa26e11c
WD
1950+ * GETACL will be unless you first call GETACLCNT which means
1951+ * making an additional system call.
1952+ *
1953+ * In the hope of avoiding the cost of the additional system
1954+ * call in most cases, we initially allocate enough space for
1955+ * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
1956+ * be too small then we use GETACLCNT to find out the actual
0f6733d8
WD
1957+ * size, reallocate the ACL buffer, and then call GETACL again.
1958+ */
fa26e11c 1959+
0f6733d8 1960+#define INITIAL_ACL_SIZE 16
fa26e11c 1961+
0f6733d8 1962+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 1963+{
0f6733d8
WD
1964+ SMB_ACL_T acl_d;
1965+ int count; /* # of ACL entries allocated */
1966+ int naccess; /* # of access ACL entries */
1967+ int ndefault; /* # of default ACL entries */
fa26e11c
WD
1968+
1969+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
1970+ errno = EINVAL;
1971+ return NULL;
1972+ }
1973+
1974+ count = INITIAL_ACL_SIZE;
1975+ if ((acl_d = sys_acl_init(count)) == NULL) {
1976+ return NULL;
1977+ }
1978+
0f6733d8
WD
1979+ /*
1980+ * If there isn't enough space for the ACL entries we use
fa26e11c
WD
1981+ * GETACLCNT to determine the actual number of ACL entries
1982+ * reallocate and try again. This is in a loop because it
1983+ * is possible that someone else could modify the ACL and
1984+ * increase the number of entries between the call to
0f6733d8
WD
1985+ * GETACLCNT and the call to GETACL.
1986+ */
fa26e11c
WD
1987+ while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
1988+ && errno == ENOSPC) {
1989+
1990+ sys_acl_free_acl(acl_d);
1991+
1992+ if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
1993+ return NULL;
1994+ }
1995+
1996+ if ((acl_d = sys_acl_init(count)) == NULL) {
1997+ return NULL;
1998+ }
1999+ }
2000+
2001+ if (count < 0) {
2002+ sys_acl_free_acl(acl_d);
2003+ return NULL;
2004+ }
2005+
0f6733d8
WD
2006+ /*
2007+ * calculate the number of access and default ACL entries
fa26e11c
WD
2008+ *
2009+ * Note: we assume that the acl() system call returned a
2010+ * well formed ACL which is sorted so that all of the
0f6733d8
WD
2011+ * access ACL entries preceed any default ACL entries
2012+ */
fa26e11c
WD
2013+ for (naccess = 0; naccess < count; naccess++) {
2014+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2015+ break;
2016+ }
2017+ ndefault = count - naccess;
0f6733d8
WD
2018+
2019+ /*
2020+ * if the caller wants the default ACL we have to copy
fa26e11c 2021+ * the entries down to the start of the acl[] buffer
0f6733d8
WD
2022+ * and mask out the ACL_DEFAULT flag from the type field
2023+ */
fa26e11c 2024+ if (type == SMB_ACL_TYPE_DEFAULT) {
0f6733d8 2025+ int i, j;
fa26e11c
WD
2026+
2027+ for (i = 0, j = naccess; i < ndefault; i++, j++) {
2028+ acl_d->acl[i] = acl_d->acl[j];
2029+ acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2030+ }
2031+
2032+ acl_d->count = ndefault;
2033+ } else {
2034+ acl_d->count = naccess;
2035+ }
2036+
2037+ return acl_d;
2038+}
2039+
0f6733d8 2040+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 2041+{
0f6733d8
WD
2042+ SMB_ACL_T acl_d;
2043+ int count; /* # of ACL entries allocated */
2044+ int naccess; /* # of access ACL entries */
fa26e11c
WD
2045+
2046+ count = INITIAL_ACL_SIZE;
2047+ if ((acl_d = sys_acl_init(count)) == NULL) {
2048+ return NULL;
2049+ }
2050+
2051+ while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2052+ && errno == ENOSPC) {
2053+
2054+ sys_acl_free_acl(acl_d);
2055+
2056+ if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2057+ return NULL;
2058+ }
2059+
2060+ if ((acl_d = sys_acl_init(count)) == NULL) {
2061+ return NULL;
2062+ }
2063+ }
2064+
2065+ if (count < 0) {
2066+ sys_acl_free_acl(acl_d);
2067+ return NULL;
2068+ }
2069+
0f6733d8
WD
2070+ /*
2071+ * calculate the number of access ACL entries
2072+ */
fa26e11c
WD
2073+ for (naccess = 0; naccess < count; naccess++) {
2074+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2075+ break;
2076+ }
0f6733d8 2077+
fa26e11c
WD
2078+ acl_d->count = naccess;
2079+
2080+ return acl_d;
2081+}
2082+
0f6733d8 2083+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2084+{
2085+ *permset_d = 0;
2086+
2087+ return 0;
2088+}
2089+
0f6733d8 2090+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2091+{
2092+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2093+ && perm != SMB_ACL_EXECUTE) {
2094+ errno = EINVAL;
2095+ return -1;
2096+ }
2097+
2098+ if (permset_d == NULL) {
2099+ errno = EINVAL;
2100+ return -1;
2101+ }
2102+
2103+ *permset_d |= perm;
2104+
2105+ return 0;
2106+}
2107+
0f6733d8 2108+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2109+{
2110+ return *permset_d & perm;
2111+}
2112+
0f6733d8 2113+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c 2114+{
0f6733d8
WD
2115+ int i;
2116+ int len, maxlen;
2117+ char *text;
fa26e11c 2118+
0f6733d8
WD
2119+ /*
2120+ * use an initial estimate of 20 bytes per ACL entry
fa26e11c 2121+ * when allocating memory for the text representation
0f6733d8
WD
2122+ * of the ACL
2123+ */
2124+ len = 0;
2125+ maxlen = 20 * acl_d->count;
252945ef 2126+ if ((text = SMB_MALLOC(maxlen)) == NULL) {
fa26e11c
WD
2127+ errno = ENOMEM;
2128+ return NULL;
2129+ }
2130+
2131+ for (i = 0; i < acl_d->count; i++) {
0f6733d8
WD
2132+ struct acl *ap = &acl_d->acl[i];
2133+ struct passwd *pw;
2134+ struct group *gr;
2135+ char tagbuf[12];
2136+ char idbuf[12];
2137+ char *tag;
2138+ char *id = "";
2139+ char perms[4];
2140+ int nbytes;
fa26e11c
WD
2141+
2142+ switch (ap->a_type) {
0f6733d8
WD
2143+ /*
2144+ * for debugging purposes it's probably more
fa26e11c 2145+ * useful to dump unknown tag types rather
0f6733d8
WD
2146+ * than just returning an error
2147+ */
fa26e11c 2148+ default:
0f6733d8 2149+ slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
fa26e11c
WD
2150+ ap->a_type);
2151+ tag = tagbuf;
0f6733d8 2152+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2153+ (long)ap->a_id);
2154+ id = idbuf;
2155+ break;
2156+
2157+ case SMB_ACL_USER:
5ca9317d 2158+ id = uidtoname(ap->a_id);
fa26e11c
WD
2159+ case SMB_ACL_USER_OBJ:
2160+ tag = "user";
2161+ break;
2162+
2163+ case SMB_ACL_GROUP:
2164+ if ((gr = getgrgid(ap->a_id)) == NULL) {
0f6733d8 2165+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2166+ (long)ap->a_id);
2167+ id = idbuf;
2168+ } else {
2169+ id = gr->gr_name;
2170+ }
2171+ case SMB_ACL_GROUP_OBJ:
2172+ tag = "group";
2173+ break;
2174+
2175+ case SMB_ACL_OTHER:
2176+ tag = "other";
2177+ break;
2178+
2179+ case SMB_ACL_MASK:
2180+ tag = "mask";
2181+ break;
2182+
2183+ }
2184+
2185+ perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2186+ perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2187+ perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2188+ perms[3] = '\0';
2189+
2190+ /* <tag> : <qualifier> : rwx \n \0 */
2191+ nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2192+
0f6733d8
WD
2193+ /*
2194+ * If this entry would overflow the buffer
fa26e11c
WD
2195+ * allocate enough additional memory for this
2196+ * entry and an estimate of another 20 bytes
0f6733d8
WD
2197+ * for each entry still to be processed
2198+ */
fa26e11c
WD
2199+ if ((len + nbytes) > maxlen) {
2200+ char *oldtext = text;
2201+
2202+ maxlen += nbytes + 20 * (acl_d->count - i);
2203+
252945ef 2204+ if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
0f6733d8 2205+ SAFE_FREE(oldtext);
fa26e11c
WD
2206+ errno = ENOMEM;
2207+ return NULL;
2208+ }
2209+ }
2210+
0f6733d8 2211+ slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
fa26e11c
WD
2212+ len += nbytes - 1;
2213+ }
2214+
2215+ if (len_p)
2216+ *len_p = len;
2217+
2218+ return text;
2219+}
2220+
0f6733d8 2221+SMB_ACL_T sys_acl_init(int count)
fa26e11c 2222+{
0f6733d8 2223+ SMB_ACL_T a;
fa26e11c
WD
2224+
2225+ if (count < 0) {
2226+ errno = EINVAL;
2227+ return NULL;
2228+ }
2229+
0f6733d8
WD
2230+ /*
2231+ * note that since the definition of the structure pointed
fa26e11c
WD
2232+ * to by the SMB_ACL_T includes the first element of the
2233+ * acl[] array, this actually allocates an ACL with room
0f6733d8
WD
2234+ * for (count+1) entries
2235+ */
252945ef 2236+ if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
fa26e11c
WD
2237+ errno = ENOMEM;
2238+ return NULL;
2239+ }
2240+
2241+ a->size = count + 1;
2242+ a->count = 0;
2243+ a->next = -1;
2244+
2245+ return a;
2246+}
2247+
2248+
0f6733d8 2249+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 2250+{
0f6733d8
WD
2251+ SMB_ACL_T acl_d;
2252+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
2253+
2254+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2255+ errno = EINVAL;
2256+ return -1;
2257+ }
2258+
2259+ if (acl_d->count >= acl_d->size) {
2260+ errno = ENOSPC;
2261+ return -1;
2262+ }
2263+
0f6733d8
WD
2264+ entry_d = &acl_d->acl[acl_d->count++];
2265+ entry_d->a_type = 0;
2266+ entry_d->a_id = -1;
2267+ entry_d->a_perm = 0;
2268+ *entry_p = entry_d;
fa26e11c
WD
2269+
2270+ return 0;
2271+}
2272+
0f6733d8 2273+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
2274+{
2275+ switch (tag_type) {
2276+ case SMB_ACL_USER:
2277+ case SMB_ACL_USER_OBJ:
2278+ case SMB_ACL_GROUP:
2279+ case SMB_ACL_GROUP_OBJ:
2280+ case SMB_ACL_OTHER:
2281+ case SMB_ACL_MASK:
2282+ entry_d->a_type = tag_type;
2283+ break;
2284+ default:
2285+ errno = EINVAL;
2286+ return -1;
2287+ }
2288+
2289+ return 0;
2290+}
2291+
0f6733d8 2292+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
2293+{
2294+ if (entry_d->a_type != SMB_ACL_GROUP
2295+ && entry_d->a_type != SMB_ACL_USER) {
2296+ errno = EINVAL;
2297+ return -1;
2298+ }
2299+
2300+ entry_d->a_id = *((id_t *)qual_p);
2301+
2302+ return 0;
2303+}
2304+
0f6733d8 2305+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2306+{
2307+ if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2308+ return EINVAL;
2309+ }
2310+
2311+ entry_d->a_perm = *permset_d;
2312+
2313+ return 0;
2314+}
2315+
0f6733d8
WD
2316+/*
2317+ * sort the ACL and check it for validity
fa26e11c 2318+ *
0f6733d8 2319+ * if it's a minimal ACL with only 4 entries then we
fa26e11c
WD
2320+ * need to recalculate the mask permissions to make
2321+ * sure that they are the same as the GROUP_OBJ
2322+ * permissions as required by the UnixWare acl() system call.
2323+ *
0f6733d8 2324+ * (note: since POSIX allows minimal ACLs which only contain
fa26e11c
WD
2325+ * 3 entries - ie there is no mask entry - we should, in theory,
2326+ * check for this and add a mask entry if necessary - however
2327+ * we "know" that the caller of this interface always specifies
2328+ * a mask so, in practice "this never happens" (tm) - if it *does*
2329+ * happen aclsort() will fail and return an error and someone will
0f6733d8
WD
2330+ * have to fix it ...)
2331+ */
fa26e11c
WD
2332+
2333+static int acl_sort(SMB_ACL_T acl_d)
2334+{
2335+ int fixmask = (acl_d->count <= 4);
2336+
2337+ if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
2338+ errno = EINVAL;
2339+ return -1;
2340+ }
2341+ return 0;
2342+}
0f6733d8
WD
2343+
2344+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
2345+{
2346+ return acl_sort(acl_d);
2347+}
2348+
0f6733d8 2349+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c 2350+{
0f6733d8
WD
2351+ struct stat s;
2352+ struct acl *acl_p;
2353+ int acl_count;
2354+ struct acl *acl_buf = NULL;
2355+ int ret;
fa26e11c
WD
2356+
2357+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2358+ errno = EINVAL;
2359+ return -1;
2360+ }
2361+
2362+ if (acl_sort(acl_d) != 0) {
2363+ return -1;
2364+ }
2365+
0f6733d8
WD
2366+ acl_p = &acl_d->acl[0];
2367+ acl_count = acl_d->count;
fa26e11c 2368+
0f6733d8
WD
2369+ /*
2370+ * if it's a directory there is extra work to do
2371+ * since the acl() system call will replace both
2372+ * the access ACLs and the default ACLs (if any)
2373+ */
fa26e11c
WD
2374+ if (stat(name, &s) != 0) {
2375+ return -1;
2376+ }
2377+ if (S_ISDIR(s.st_mode)) {
0f6733d8
WD
2378+ SMB_ACL_T acc_acl;
2379+ SMB_ACL_T def_acl;
2380+ SMB_ACL_T tmp_acl;
2381+ int i;
fa26e11c
WD
2382+
2383+ if (type == SMB_ACL_TYPE_ACCESS) {
2384+ acc_acl = acl_d;
2385+ def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
2386+
2387+ } else {
2388+ def_acl = acl_d;
2389+ acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
2390+ }
2391+
2392+ if (tmp_acl == NULL) {
2393+ return -1;
2394+ }
2395+
0f6733d8
WD
2396+ /*
2397+ * allocate a temporary buffer for the complete ACL
2398+ */
fa26e11c 2399+ acl_count = acc_acl->count + def_acl->count;
252945ef 2400+ acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
fa26e11c
WD
2401+
2402+ if (acl_buf == NULL) {
2403+ sys_acl_free_acl(tmp_acl);
2404+ errno = ENOMEM;
2405+ return -1;
2406+ }
2407+
0f6733d8
WD
2408+ /*
2409+ * copy the access control and default entries into the buffer
2410+ */
fa26e11c 2411+ memcpy(&acl_buf[0], &acc_acl->acl[0],
0f6733d8 2412+ acc_acl->count * sizeof(acl_buf[0]));
fa26e11c
WD
2413+
2414+ memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
0f6733d8 2415+ def_acl->count * sizeof(acl_buf[0]));
fa26e11c 2416+
0f6733d8
WD
2417+ /*
2418+ * set the ACL_DEFAULT flag on the default entries
2419+ */
fa26e11c
WD
2420+ for (i = acc_acl->count; i < acl_count; i++) {
2421+ acl_buf[i].a_type |= ACL_DEFAULT;
2422+ }
2423+
2424+ sys_acl_free_acl(tmp_acl);
2425+
2426+ } else if (type != SMB_ACL_TYPE_ACCESS) {
2427+ errno = EINVAL;
2428+ return -1;
2429+ }
2430+
2431+ ret = acl(name, SETACL, acl_count, acl_p);
2432+
0f6733d8 2433+ SAFE_FREE(acl_buf);
fa26e11c
WD
2434+
2435+ return ret;
2436+}
2437+
0f6733d8 2438+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c
WD
2439+{
2440+ if (acl_sort(acl_d) != 0) {
2441+ return -1;
2442+ }
2443+
2444+ return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
2445+}
2446+
0f6733d8 2447+int sys_acl_delete_def_file(const char *path)
fa26e11c 2448+{
0f6733d8
WD
2449+ SMB_ACL_T acl_d;
2450+ int ret;
fa26e11c 2451+
0f6733d8
WD
2452+ /*
2453+ * fetching the access ACL and rewriting it has
2454+ * the effect of deleting the default ACL
2455+ */
fa26e11c
WD
2456+ if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
2457+ return -1;
2458+ }
2459+
2460+ ret = acl(path, SETACL, acl_d->count, acl_d->acl);
2461+
2462+ sys_acl_free_acl(acl_d);
0f6733d8 2463+
fa26e11c
WD
2464+ return ret;
2465+}
2466+
0f6733d8 2467+int sys_acl_free_text(char *text)
fa26e11c 2468+{
0f6733d8 2469+ SAFE_FREE(text);
fa26e11c
WD
2470+ return 0;
2471+}
2472+
0f6733d8 2473+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c 2474+{
0f6733d8 2475+ SAFE_FREE(acl_d);
fa26e11c
WD
2476+ return 0;
2477+}
2478+
0f6733d8 2479+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
2480+{
2481+ return 0;
2482+}
2483+
2484+#elif defined(HAVE_HPUX_ACLS)
2485+#include <dl.h>
2486+
0f6733d8
WD
2487+/*
2488+ * Based on the Solaris/SCO code - with modifications.
2489+ */
fa26e11c 2490+
0f6733d8
WD
2491+/*
2492+ * Note that while this code implements sufficient functionality
fa26e11c
WD
2493+ * to support the sys_acl_* interfaces it does not provide all
2494+ * of the semantics of the POSIX ACL interfaces.
2495+ *
2496+ * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2497+ * from a call to sys_acl_get_entry() should not be assumed to be
2498+ * valid after calling any of the following functions, which may
2499+ * reorder the entries in the ACL.
2500+ *
2501+ * sys_acl_valid()
2502+ * sys_acl_set_file()
2503+ * sys_acl_set_fd()
2504+ */
2505+
0f6733d8
WD
2506+/* This checks if the POSIX ACL system call is defined */
2507+/* which basically corresponds to whether JFS 3.3 or */
2508+/* higher is installed. If acl() was called when it */
2509+/* isn't defined, it causes the process to core dump */
2510+/* so it is important to check this and avoid acl() */
2511+/* calls if it isn't there. */
fa26e11c
WD
2512+
2513+static BOOL hpux_acl_call_presence(void)
2514+{
2515+
2516+ shl_t handle = NULL;
2517+ void *value;
2518+ int ret_val=0;
2519+ static BOOL already_checked=0;
2520+
0f6733d8 2521+ if(already_checked)
fa26e11c
WD
2522+ return True;
2523+
2524+
2525+ ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
2526+
0f6733d8 2527+ if(ret_val != 0) {
fa26e11c
WD
2528+ DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
2529+ ret_val, errno, strerror(errno)));
2530+ DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
2531+ return False;
2532+ }
2533+
2534+ DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
2535+
2536+ already_checked = True;
2537+ return True;
2538+}
2539+
0f6733d8 2540+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
2541+{
2542+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2543+ errno = EINVAL;
2544+ return -1;
2545+ }
2546+
2547+ if (entry_p == NULL) {
2548+ errno = EINVAL;
2549+ return -1;
2550+ }
2551+
2552+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
2553+ acl_d->next = 0;
2554+ }
2555+
2556+ if (acl_d->next < 0) {
2557+ errno = EINVAL;
2558+ return -1;
2559+ }
2560+
2561+ if (acl_d->next >= acl_d->count) {
2562+ return 0;
2563+ }
2564+
2565+ *entry_p = &acl_d->acl[acl_d->next++];
2566+
2567+ return 1;
2568+}
2569+
0f6733d8 2570+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
2571+{
2572+ *type_p = entry_d->a_type;
2573+
2574+ return 0;
2575+}
2576+
0f6733d8 2577+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
2578+{
2579+ *permset_p = &entry_d->a_perm;
2580+
2581+ return 0;
2582+}
2583+
0f6733d8 2584+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
2585+{
2586+ if (entry_d->a_type != SMB_ACL_USER
2587+ && entry_d->a_type != SMB_ACL_GROUP) {
2588+ errno = EINVAL;
2589+ return NULL;
2590+ }
2591+
2592+ return &entry_d->a_id;
2593+}
2594+
0f6733d8
WD
2595+/*
2596+ * There is no way of knowing what size the ACL returned by
fa26e11c
WD
2597+ * ACL_GET will be unless you first call ACL_CNT which means
2598+ * making an additional system call.
2599+ *
2600+ * In the hope of avoiding the cost of the additional system
2601+ * call in most cases, we initially allocate enough space for
2602+ * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2603+ * be too small then we use ACL_CNT to find out the actual
2604+ * size, reallocate the ACL buffer, and then call ACL_GET again.
2605+ */
2606+
0f6733d8 2607+#define INITIAL_ACL_SIZE 16
fa26e11c 2608+
0f6733d8 2609+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 2610+{
0f6733d8
WD
2611+ SMB_ACL_T acl_d;
2612+ int count; /* # of ACL entries allocated */
2613+ int naccess; /* # of access ACL entries */
2614+ int ndefault; /* # of default ACL entries */
fa26e11c 2615+
0f6733d8
WD
2616+ if(hpux_acl_call_presence() == False) {
2617+ /* Looks like we don't have the acl() system call on HPUX.
2618+ * May be the system doesn't have the latest version of JFS.
2619+ */
2620+ return NULL;
fa26e11c
WD
2621+ }
2622+
2623+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2624+ errno = EINVAL;
2625+ return NULL;
2626+ }
2627+
2628+ count = INITIAL_ACL_SIZE;
2629+ if ((acl_d = sys_acl_init(count)) == NULL) {
2630+ return NULL;
2631+ }
2632+
0f6733d8
WD
2633+ /*
2634+ * If there isn't enough space for the ACL entries we use
fa26e11c
WD
2635+ * ACL_CNT to determine the actual number of ACL entries
2636+ * reallocate and try again. This is in a loop because it
2637+ * is possible that someone else could modify the ACL and
2638+ * increase the number of entries between the call to
0f6733d8
WD
2639+ * ACL_CNT and the call to ACL_GET.
2640+ */
fa26e11c
WD
2641+ while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
2642+
2643+ sys_acl_free_acl(acl_d);
2644+
2645+ if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
2646+ return NULL;
2647+ }
2648+
2649+ if ((acl_d = sys_acl_init(count)) == NULL) {
2650+ return NULL;
2651+ }
2652+ }
2653+
2654+ if (count < 0) {
2655+ sys_acl_free_acl(acl_d);
2656+ return NULL;
2657+ }
2658+
0f6733d8
WD
2659+ /*
2660+ * calculate the number of access and default ACL entries
fa26e11c
WD
2661+ *
2662+ * Note: we assume that the acl() system call returned a
2663+ * well formed ACL which is sorted so that all of the
0f6733d8
WD
2664+ * access ACL entries preceed any default ACL entries
2665+ */
fa26e11c
WD
2666+ for (naccess = 0; naccess < count; naccess++) {
2667+ if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2668+ break;
2669+ }
2670+ ndefault = count - naccess;
0f6733d8
WD
2671+
2672+ /*
2673+ * if the caller wants the default ACL we have to copy
fa26e11c 2674+ * the entries down to the start of the acl[] buffer
0f6733d8
WD
2675+ * and mask out the ACL_DEFAULT flag from the type field
2676+ */
fa26e11c 2677+ if (type == SMB_ACL_TYPE_DEFAULT) {
0f6733d8 2678+ int i, j;
fa26e11c
WD
2679+
2680+ for (i = 0, j = naccess; i < ndefault; i++, j++) {
2681+ acl_d->acl[i] = acl_d->acl[j];
2682+ acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2683+ }
2684+
2685+ acl_d->count = ndefault;
2686+ } else {
2687+ acl_d->count = naccess;
2688+ }
2689+
2690+ return acl_d;
2691+}
2692+
0f6733d8 2693+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 2694+{
0f6733d8
WD
2695+ /*
2696+ * HPUX doesn't have the facl call. Fake it using the path.... JRA.
2697+ */
fa26e11c
WD
2698+
2699+ files_struct *fsp = file_find_fd(fd);
2700+
2701+ if (fsp == NULL) {
2702+ errno = EBADF;
2703+ return NULL;
2704+ }
2705+
0f6733d8
WD
2706+ /*
2707+ * We know we're in the same conn context. So we
2708+ * can use the relative path.
2709+ */
fa26e11c 2710+
5ca9317d 2711+ return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
fa26e11c
WD
2712+}
2713+
0f6733d8 2714+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2715+{
2716+ *permset_d = 0;
2717+
2718+ return 0;
2719+}
2720+
0f6733d8 2721+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2722+{
2723+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2724+ && perm != SMB_ACL_EXECUTE) {
2725+ errno = EINVAL;
2726+ return -1;
2727+ }
2728+
2729+ if (permset_d == NULL) {
2730+ errno = EINVAL;
2731+ return -1;
2732+ }
2733+
2734+ *permset_d |= perm;
2735+
2736+ return 0;
2737+}
2738+
0f6733d8 2739+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
2740+{
2741+ return *permset_d & perm;
2742+}
2743+
0f6733d8 2744+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c 2745+{
0f6733d8
WD
2746+ int i;
2747+ int len, maxlen;
2748+ char *text;
fa26e11c 2749+
0f6733d8
WD
2750+ /*
2751+ * use an initial estimate of 20 bytes per ACL entry
2752+ * when allocating memory for the text representation
2753+ * of the ACL
2754+ */
2755+ len = 0;
2756+ maxlen = 20 * acl_d->count;
252945ef 2757+ if ((text = SMB_MALLOC(maxlen)) == NULL) {
fa26e11c
WD
2758+ errno = ENOMEM;
2759+ return NULL;
2760+ }
2761+
2762+ for (i = 0; i < acl_d->count; i++) {
0f6733d8
WD
2763+ struct acl *ap = &acl_d->acl[i];
2764+ struct passwd *pw;
2765+ struct group *gr;
2766+ char tagbuf[12];
2767+ char idbuf[12];
2768+ char *tag;
2769+ char *id = "";
2770+ char perms[4];
2771+ int nbytes;
fa26e11c
WD
2772+
2773+ switch (ap->a_type) {
0f6733d8
WD
2774+ /*
2775+ * for debugging purposes it's probably more
fa26e11c 2776+ * useful to dump unknown tag types rather
0f6733d8
WD
2777+ * than just returning an error
2778+ */
fa26e11c 2779+ default:
0f6733d8 2780+ slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
fa26e11c
WD
2781+ ap->a_type);
2782+ tag = tagbuf;
0f6733d8 2783+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2784+ (long)ap->a_id);
2785+ id = idbuf;
2786+ break;
2787+
2788+ case SMB_ACL_USER:
5ca9317d 2789+ id = uidtoname(ap->a_id);
fa26e11c
WD
2790+ case SMB_ACL_USER_OBJ:
2791+ tag = "user";
2792+ break;
2793+
2794+ case SMB_ACL_GROUP:
2795+ if ((gr = getgrgid(ap->a_id)) == NULL) {
0f6733d8 2796+ slprintf(idbuf, sizeof(idbuf)-1, "%ld",
fa26e11c
WD
2797+ (long)ap->a_id);
2798+ id = idbuf;
2799+ } else {
2800+ id = gr->gr_name;
2801+ }
2802+ case SMB_ACL_GROUP_OBJ:
2803+ tag = "group";
2804+ break;
2805+
2806+ case SMB_ACL_OTHER:
2807+ tag = "other";
2808+ break;
2809+
2810+ case SMB_ACL_MASK:
2811+ tag = "mask";
2812+ break;
2813+
2814+ }
2815+
2816+ perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2817+ perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2818+ perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2819+ perms[3] = '\0';
2820+
2821+ /* <tag> : <qualifier> : rwx \n \0 */
2822+ nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2823+
0f6733d8
WD
2824+ /*
2825+ * If this entry would overflow the buffer
fa26e11c
WD
2826+ * allocate enough additional memory for this
2827+ * entry and an estimate of another 20 bytes
0f6733d8
WD
2828+ * for each entry still to be processed
2829+ */
fa26e11c
WD
2830+ if ((len + nbytes) > maxlen) {
2831+ char *oldtext = text;
2832+
2833+ maxlen += nbytes + 20 * (acl_d->count - i);
2834+
252945ef 2835+ if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
fa26e11c
WD
2836+ free(oldtext);
2837+ errno = ENOMEM;
2838+ return NULL;
2839+ }
2840+ }
2841+
0f6733d8 2842+ slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
fa26e11c
WD
2843+ len += nbytes - 1;
2844+ }
2845+
2846+ if (len_p)
2847+ *len_p = len;
2848+
2849+ return text;
2850+}
2851+
0f6733d8 2852+SMB_ACL_T sys_acl_init(int count)
fa26e11c 2853+{
0f6733d8 2854+ SMB_ACL_T a;
fa26e11c
WD
2855+
2856+ if (count < 0) {
2857+ errno = EINVAL;
2858+ return NULL;
2859+ }
2860+
0f6733d8
WD
2861+ /*
2862+ * note that since the definition of the structure pointed
fa26e11c
WD
2863+ * to by the SMB_ACL_T includes the first element of the
2864+ * acl[] array, this actually allocates an ACL with room
0f6733d8
WD
2865+ * for (count+1) entries
2866+ */
252945ef 2867+ if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
fa26e11c
WD
2868+ errno = ENOMEM;
2869+ return NULL;
2870+ }
2871+
2872+ a->size = count + 1;
2873+ a->count = 0;
2874+ a->next = -1;
2875+
2876+ return a;
2877+}
2878+
2879+
0f6733d8 2880+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 2881+{
0f6733d8
WD
2882+ SMB_ACL_T acl_d;
2883+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
2884+
2885+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2886+ errno = EINVAL;
2887+ return -1;
2888+ }
2889+
2890+ if (acl_d->count >= acl_d->size) {
2891+ errno = ENOSPC;
2892+ return -1;
2893+ }
2894+
0f6733d8
WD
2895+ entry_d = &acl_d->acl[acl_d->count++];
2896+ entry_d->a_type = 0;
2897+ entry_d->a_id = -1;
2898+ entry_d->a_perm = 0;
2899+ *entry_p = entry_d;
fa26e11c
WD
2900+
2901+ return 0;
2902+}
2903+
0f6733d8 2904+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
2905+{
2906+ switch (tag_type) {
2907+ case SMB_ACL_USER:
2908+ case SMB_ACL_USER_OBJ:
2909+ case SMB_ACL_GROUP:
2910+ case SMB_ACL_GROUP_OBJ:
2911+ case SMB_ACL_OTHER:
2912+ case SMB_ACL_MASK:
2913+ entry_d->a_type = tag_type;
2914+ break;
2915+ default:
2916+ errno = EINVAL;
2917+ return -1;
2918+ }
2919+
2920+ return 0;
2921+}
2922+
0f6733d8 2923+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
2924+{
2925+ if (entry_d->a_type != SMB_ACL_GROUP
2926+ && entry_d->a_type != SMB_ACL_USER) {
2927+ errno = EINVAL;
2928+ return -1;
2929+ }
2930+
2931+ entry_d->a_id = *((id_t *)qual_p);
2932+
2933+ return 0;
2934+}
2935+
0f6733d8 2936+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
2937+{
2938+ if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2939+ return EINVAL;
2940+ }
2941+
2942+ entry_d->a_perm = *permset_d;
2943+
2944+ return 0;
2945+}
2946+
2947+/* Structure to capture the count for each type of ACE. */
2948+
2949+struct hpux_acl_types {
2950+ int n_user;
2951+ int n_def_user;
2952+ int n_user_obj;
2953+ int n_def_user_obj;
2954+
2955+ int n_group;
2956+ int n_def_group;
2957+ int n_group_obj;
2958+ int n_def_group_obj;
2959+
2960+ int n_other;
2961+ int n_other_obj;
2962+ int n_def_other_obj;
2963+
2964+ int n_class_obj;
2965+ int n_def_class_obj;
2966+
2967+ int n_illegal_obj;
2968+};
2969+
2970+/* count_obj:
2971+ * Counts the different number of objects in a given array of ACL
2972+ * structures.
2973+ * Inputs:
2974+ *
2975+ * acl_count - Count of ACLs in the array of ACL strucutres.
2976+ * aclp - Array of ACL structures.
2977+ * acl_type_count - Pointer to acl_types structure. Should already be
2978+ * allocated.
0f6733d8 2979+ * Output:
fa26e11c 2980+ *
0f6733d8 2981+ * acl_type_count - This structure is filled up with counts of various
fa26e11c
WD
2982+ * acl types.
2983+ */
2984+
2985+static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
2986+{
2987+ int i;
2988+
0f6733d8 2989+ memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
fa26e11c 2990+
0f6733d8
WD
2991+ for(i=0;i<acl_count;i++) {
2992+ switch(aclp[i].a_type) {
2993+ case USER:
fa26e11c
WD
2994+ acl_type_count->n_user++;
2995+ break;
0f6733d8 2996+ case USER_OBJ:
fa26e11c
WD
2997+ acl_type_count->n_user_obj++;
2998+ break;
0f6733d8 2999+ case DEF_USER_OBJ:
fa26e11c
WD
3000+ acl_type_count->n_def_user_obj++;
3001+ break;
0f6733d8 3002+ case GROUP:
fa26e11c
WD
3003+ acl_type_count->n_group++;
3004+ break;
0f6733d8 3005+ case GROUP_OBJ:
fa26e11c
WD
3006+ acl_type_count->n_group_obj++;
3007+ break;
0f6733d8 3008+ case DEF_GROUP_OBJ:
fa26e11c
WD
3009+ acl_type_count->n_def_group_obj++;
3010+ break;
0f6733d8 3011+ case OTHER_OBJ:
fa26e11c
WD
3012+ acl_type_count->n_other_obj++;
3013+ break;
0f6733d8 3014+ case DEF_OTHER_OBJ:
fa26e11c
WD
3015+ acl_type_count->n_def_other_obj++;
3016+ break;
3017+ case CLASS_OBJ:
3018+ acl_type_count->n_class_obj++;
3019+ break;
3020+ case DEF_CLASS_OBJ:
3021+ acl_type_count->n_def_class_obj++;
3022+ break;
3023+ case DEF_USER:
3024+ acl_type_count->n_def_user++;
3025+ break;
3026+ case DEF_GROUP:
3027+ acl_type_count->n_def_group++;
3028+ break;
0f6733d8 3029+ default:
fa26e11c
WD
3030+ acl_type_count->n_illegal_obj++;
3031+ break;
3032+ }
3033+ }
3034+}
3035+
0f6733d8 3036+/* swap_acl_entries: Swaps two ACL entries.
fa26e11c
WD
3037+ *
3038+ * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3039+ */
3040+
3041+static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3042+{
3043+ struct acl temp_acl;
3044+
3045+ temp_acl.a_type = aclp0->a_type;
3046+ temp_acl.a_id = aclp0->a_id;
3047+ temp_acl.a_perm = aclp0->a_perm;
3048+
3049+ aclp0->a_type = aclp1->a_type;
3050+ aclp0->a_id = aclp1->a_id;
3051+ aclp0->a_perm = aclp1->a_perm;
3052+
3053+ aclp1->a_type = temp_acl.a_type;
3054+ aclp1->a_id = temp_acl.a_id;
3055+ aclp1->a_perm = temp_acl.a_perm;
3056+}
3057+
3058+/* prohibited_duplicate_type
0f6733d8 3059+ * Identifies if given ACL type can have duplicate entries or
fa26e11c
WD
3060+ * not.
3061+ *
3062+ * Inputs: acl_type - ACL Type.
3063+ *
0f6733d8 3064+ * Outputs:
fa26e11c 3065+ *
0f6733d8 3066+ * Return..
fa26e11c
WD
3067+ *
3068+ * True - If the ACL type matches any of the prohibited types.
3069+ * False - If the ACL type doesn't match any of the prohibited types.
0f6733d8 3070+ */
fa26e11c
WD
3071+
3072+static BOOL hpux_prohibited_duplicate_type(int acl_type)
3073+{
0f6733d8 3074+ switch(acl_type) {
fa26e11c
WD
3075+ case USER:
3076+ case GROUP:
0f6733d8 3077+ case DEF_USER:
fa26e11c
WD
3078+ case DEF_GROUP:
3079+ return True;
0f6733d8
WD
3080+ default:
3081+ return False;
fa26e11c 3082+ }
fa26e11c
WD
3083+}
3084+
3085+/* get_needed_class_perm
3086+ * Returns the permissions of a ACL structure only if the ACL
0f6733d8 3087+ * type matches one of the pre-determined types for computing
fa26e11c
WD
3088+ * CLASS_OBJ permissions.
3089+ *
3090+ * Inputs: aclp - Pointer to ACL structure.
3091+ */
3092+
3093+static int hpux_get_needed_class_perm(struct acl *aclp)
3094+{
0f6733d8
WD
3095+ switch(aclp->a_type) {
3096+ case USER:
3097+ case GROUP_OBJ:
3098+ case GROUP:
3099+ case DEF_USER_OBJ:
fa26e11c 3100+ case DEF_USER:
0f6733d8 3101+ case DEF_GROUP_OBJ:
fa26e11c
WD
3102+ case DEF_GROUP:
3103+ case DEF_CLASS_OBJ:
0f6733d8 3104+ case DEF_OTHER_OBJ:
fa26e11c 3105+ return aclp->a_perm;
0f6733d8 3106+ default:
fa26e11c
WD
3107+ return 0;
3108+ }
3109+}
3110+
3111+/* acl_sort for HPUX.
3112+ * Sorts the array of ACL structures as per the description in
3113+ * aclsort man page. Refer to aclsort man page for more details
3114+ *
3115+ * Inputs:
3116+ *
3117+ * acl_count - Count of ACLs in the array of ACL structures.
3118+ * calclass - If this is not zero, then we compute the CLASS_OBJ
3119+ * permissions.
3120+ * aclp - Array of ACL structures.
3121+ *
3122+ * Outputs:
3123+ *
3124+ * aclp - Sorted array of ACL structures.
3125+ *
3126+ * Outputs:
3127+ *
3128+ * Returns 0 for success -1 for failure. Prints a message to the Samba
3129+ * debug log in case of failure.
3130+ */
3131+
3132+static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3133+{
3134+#if !defined(HAVE_HPUX_ACLSORT)
0f6733d8
WD
3135+ /*
3136+ * The aclsort() system call is availabe on the latest HPUX General
3137+ * Patch Bundles. So for HPUX, we developed our version of acl_sort
3138+ * function. Because, we don't want to update to a new
3139+ * HPUX GR bundle just for aclsort() call.
3140+ */
fa26e11c
WD
3141+
3142+ struct hpux_acl_types acl_obj_count;
3143+ int n_class_obj_perm = 0;
3144+ int i, j;
0f6733d8
WD
3145+
3146+ if(!acl_count) {
fa26e11c
WD
3147+ DEBUG(10,("Zero acl count passed. Returning Success\n"));
3148+ return 0;
3149+ }
3150+
0f6733d8 3151+ if(aclp == NULL) {
fa26e11c
WD
3152+ DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3153+ return -1;
3154+ }
3155+
3156+ /* Count different types of ACLs in the ACLs array */
3157+
3158+ hpux_count_obj(acl_count, aclp, &acl_obj_count);
3159+
0f6733d8
WD
3160+ /* There should be only one entry each of type USER_OBJ, GROUP_OBJ,
3161+ * CLASS_OBJ and OTHER_OBJ
fa26e11c
WD
3162+ */
3163+
0f6733d8
WD
3164+ if( (acl_obj_count.n_user_obj != 1) ||
3165+ (acl_obj_count.n_group_obj != 1) ||
3166+ (acl_obj_count.n_class_obj != 1) ||
3167+ (acl_obj_count.n_other_obj != 1)
3168+ ) {
fa26e11c
WD
3169+ DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3170+USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3171+ return -1;
3172+ }
3173+
3174+ /* If any of the default objects are present, there should be only
3175+ * one of them each.
3176+ */
3177+
0f6733d8
WD
3178+ if( (acl_obj_count.n_def_user_obj > 1) || (acl_obj_count.n_def_group_obj > 1) ||
3179+ (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
fa26e11c
WD
3180+ DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3181+or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3182+ return -1;
3183+ }
3184+
0f6733d8
WD
3185+ /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl
3186+ * structures.
fa26e11c
WD
3187+ *
3188+ * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3189+ * same ACL type, sort by ACL id.
3190+ *
0f6733d8 3191+ * I am using the trival kind of sorting method here because, performance isn't
fa26e11c 3192+ * really effected by the ACLs feature. More over there aren't going to be more
0f6733d8 3193+ * than 17 entries on HPUX.
fa26e11c
WD
3194+ */
3195+
0f6733d8 3196+ for(i=0; i<acl_count;i++) {
fa26e11c 3197+ for (j=i+1; j<acl_count; j++) {
0f6733d8 3198+ if( aclp[i].a_type > aclp[j].a_type ) {
fa26e11c
WD
3199+ /* ACL entries out of order, swap them */
3200+
3201+ hpux_swap_acl_entries((aclp+i), (aclp+j));
3202+
0f6733d8 3203+ } else if ( aclp[i].a_type == aclp[j].a_type ) {
fa26e11c
WD
3204+
3205+ /* ACL entries of same type, sort by id */
3206+
0f6733d8 3207+ if(aclp[i].a_id > aclp[j].a_id) {
fa26e11c
WD
3208+ hpux_swap_acl_entries((aclp+i), (aclp+j));
3209+ } else if (aclp[i].a_id == aclp[j].a_id) {
3210+ /* We have a duplicate entry. */
0f6733d8 3211+ if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
fa26e11c
WD
3212+ DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3213+ aclp[i].a_type, aclp[i].a_id));
3214+ return -1;
3215+ }
3216+ }
3217+
3218+ }
3219+ }
3220+ }
3221+
3222+ /* set the class obj permissions to the computed one. */
0f6733d8 3223+ if(calclass) {
fa26e11c
WD
3224+ int n_class_obj_index = -1;
3225+
0f6733d8 3226+ for(i=0;i<acl_count;i++) {
fa26e11c
WD
3227+ n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3228+
0f6733d8 3229+ if(aclp[i].a_type == CLASS_OBJ)
fa26e11c
WD
3230+ n_class_obj_index = i;
3231+ }
3232+ aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3233+ }
3234+
3235+ return 0;
3236+#else
3237+ return aclsort(acl_count, calclass, aclp);
3238+#endif
3239+}
3240+
0f6733d8
WD
3241+/*
3242+ * sort the ACL and check it for validity
fa26e11c 3243+ *
0f6733d8 3244+ * if it's a minimal ACL with only 4 entries then we
fa26e11c
WD
3245+ * need to recalculate the mask permissions to make
3246+ * sure that they are the same as the GROUP_OBJ
3247+ * permissions as required by the UnixWare acl() system call.
3248+ *
0f6733d8 3249+ * (note: since POSIX allows minimal ACLs which only contain
fa26e11c
WD
3250+ * 3 entries - ie there is no mask entry - we should, in theory,
3251+ * check for this and add a mask entry if necessary - however
3252+ * we "know" that the caller of this interface always specifies
3253+ * a mask so, in practice "this never happens" (tm) - if it *does*
3254+ * happen aclsort() will fail and return an error and someone will
0f6733d8
WD
3255+ * have to fix it ...)
3256+ */
fa26e11c
WD
3257+
3258+static int acl_sort(SMB_ACL_T acl_d)
3259+{
3260+ int fixmask = (acl_d->count <= 4);
3261+
3262+ if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
3263+ errno = EINVAL;
3264+ return -1;
3265+ }
3266+ return 0;
3267+}
0f6733d8
WD
3268+
3269+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
3270+{
3271+ return acl_sort(acl_d);
3272+}
3273+
0f6733d8 3274+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c 3275+{
0f6733d8
WD
3276+ struct stat s;
3277+ struct acl *acl_p;
3278+ int acl_count;
3279+ struct acl *acl_buf = NULL;
3280+ int ret;
fa26e11c 3281+
0f6733d8
WD
3282+ if(hpux_acl_call_presence() == False) {
3283+ /* Looks like we don't have the acl() system call on HPUX.
3284+ * May be the system doesn't have the latest version of JFS.
3285+ */
3286+ errno=ENOSYS;
3287+ return -1;
fa26e11c
WD
3288+ }
3289+
3290+ if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3291+ errno = EINVAL;
3292+ return -1;
3293+ }
3294+
3295+ if (acl_sort(acl_d) != 0) {
3296+ return -1;
3297+ }
3298+
0f6733d8
WD
3299+ acl_p = &acl_d->acl[0];
3300+ acl_count = acl_d->count;
fa26e11c 3301+
0f6733d8
WD
3302+ /*
3303+ * if it's a directory there is extra work to do
3304+ * since the acl() system call will replace both
3305+ * the access ACLs and the default ACLs (if any)
3306+ */
fa26e11c
WD
3307+ if (stat(name, &s) != 0) {
3308+ return -1;
3309+ }
3310+ if (S_ISDIR(s.st_mode)) {
0f6733d8
WD
3311+ SMB_ACL_T acc_acl;
3312+ SMB_ACL_T def_acl;
3313+ SMB_ACL_T tmp_acl;
3314+ int i;
fa26e11c
WD
3315+
3316+ if (type == SMB_ACL_TYPE_ACCESS) {
3317+ acc_acl = acl_d;
3318+ def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3319+
3320+ } else {
3321+ def_acl = acl_d;
3322+ acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3323+ }
3324+
3325+ if (tmp_acl == NULL) {
3326+ return -1;
3327+ }
3328+
0f6733d8
WD
3329+ /*
3330+ * allocate a temporary buffer for the complete ACL
3331+ */
fa26e11c 3332+ acl_count = acc_acl->count + def_acl->count;
252945ef 3333+ acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
fa26e11c
WD
3334+
3335+ if (acl_buf == NULL) {
3336+ sys_acl_free_acl(tmp_acl);
3337+ errno = ENOMEM;
3338+ return -1;
3339+ }
3340+
0f6733d8
WD
3341+ /*
3342+ * copy the access control and default entries into the buffer
3343+ */
fa26e11c 3344+ memcpy(&acl_buf[0], &acc_acl->acl[0],
0f6733d8 3345+ acc_acl->count * sizeof(acl_buf[0]));
fa26e11c
WD
3346+
3347+ memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
0f6733d8 3348+ def_acl->count * sizeof(acl_buf[0]));
fa26e11c 3349+
0f6733d8
WD
3350+ /*
3351+ * set the ACL_DEFAULT flag on the default entries
3352+ */
fa26e11c
WD
3353+ for (i = acc_acl->count; i < acl_count; i++) {
3354+ acl_buf[i].a_type |= ACL_DEFAULT;
3355+ }
3356+
3357+ sys_acl_free_acl(tmp_acl);
3358+
3359+ } else if (type != SMB_ACL_TYPE_ACCESS) {
3360+ errno = EINVAL;
3361+ return -1;
3362+ }
3363+
3364+ ret = acl(name, ACL_SET, acl_count, acl_p);
3365+
0f6733d8
WD
3366+ if (acl_buf) {
3367+ free(acl_buf);
3368+ }
fa26e11c
WD
3369+
3370+ return ret;
3371+}
3372+
0f6733d8 3373+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c 3374+{
0f6733d8
WD
3375+ /*
3376+ * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3377+ */
fa26e11c
WD
3378+
3379+ files_struct *fsp = file_find_fd(fd);
3380+
3381+ if (fsp == NULL) {
3382+ errno = EBADF;
3383+ return NULL;
3384+ }
3385+
3386+ if (acl_sort(acl_d) != 0) {
3387+ return -1;
3388+ }
3389+
0f6733d8
WD
3390+ /*
3391+ * We know we're in the same conn context. So we
3392+ * can use the relative path.
3393+ */
fa26e11c 3394+
5ca9317d 3395+ return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
fa26e11c
WD
3396+}
3397+
0f6733d8 3398+int sys_acl_delete_def_file(const char *path)
fa26e11c 3399+{
0f6733d8
WD
3400+ SMB_ACL_T acl_d;
3401+ int ret;
fa26e11c 3402+
0f6733d8
WD
3403+ /*
3404+ * fetching the access ACL and rewriting it has
3405+ * the effect of deleting the default ACL
3406+ */
fa26e11c
WD
3407+ if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3408+ return -1;
3409+ }
3410+
3411+ ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
3412+
3413+ sys_acl_free_acl(acl_d);
0f6733d8 3414+
fa26e11c
WD
3415+ return ret;
3416+}
3417+
0f6733d8 3418+int sys_acl_free_text(char *text)
fa26e11c
WD
3419+{
3420+ free(text);
3421+ return 0;
3422+}
3423+
0f6733d8 3424+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c
WD
3425+{
3426+ free(acl_d);
3427+ return 0;
3428+}
3429+
0f6733d8 3430+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
3431+{
3432+ return 0;
3433+}
3434+
3435+#elif defined(HAVE_IRIX_ACLS)
3436+
0f6733d8 3437+int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
3438+{
3439+ if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3440+ errno = EINVAL;
3441+ return -1;
3442+ }
3443+
3444+ if (entry_p == NULL) {
3445+ errno = EINVAL;
3446+ return -1;
3447+ }
3448+
3449+ if (entry_id == SMB_ACL_FIRST_ENTRY) {
3450+ acl_d->next = 0;
3451+ }
3452+
3453+ if (acl_d->next < 0) {
3454+ errno = EINVAL;
3455+ return -1;
3456+ }
3457+
3458+ if (acl_d->next >= acl_d->aclp->acl_cnt) {
3459+ return 0;
3460+ }
3461+
3462+ *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
3463+
3464+ return 1;
3465+}
3466+
0f6733d8 3467+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
fa26e11c
WD
3468+{
3469+ *type_p = entry_d->ae_tag;
3470+
3471+ return 0;
3472+}
3473+
0f6733d8 3474+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
3475+{
3476+ *permset_p = entry_d;
3477+
3478+ return 0;
3479+}
3480+
0f6733d8 3481+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
fa26e11c
WD
3482+{
3483+ if (entry_d->ae_tag != SMB_ACL_USER
3484+ && entry_d->ae_tag != SMB_ACL_GROUP) {
3485+ errno = EINVAL;
3486+ return NULL;
3487+ }
3488+
3489+ return &entry_d->ae_id;
3490+}
3491+
0f6733d8 3492+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c 3493+{
0f6733d8 3494+ SMB_ACL_T a;
fa26e11c 3495+
252945ef 3496+ if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
fa26e11c
WD
3497+ errno = ENOMEM;
3498+ return NULL;
3499+ }
3500+ if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
0f6733d8 3501+ SAFE_FREE(a);
fa26e11c
WD
3502+ return NULL;
3503+ }
3504+ a->next = -1;
3505+ a->freeaclp = True;
3506+ return a;
3507+}
3508+
0f6733d8 3509+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c 3510+{
0f6733d8 3511+ SMB_ACL_T a;
fa26e11c 3512+
252945ef 3513+ if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
fa26e11c
WD
3514+ errno = ENOMEM;
3515+ return NULL;
3516+ }
3517+ if ((a->aclp = acl_get_fd(fd)) == NULL) {
0f6733d8 3518+ SAFE_FREE(a);
fa26e11c
WD
3519+ return NULL;
3520+ }
3521+ a->next = -1;
3522+ a->freeaclp = True;
3523+ return a;
3524+}
3525+
0f6733d8 3526+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
3527+{
3528+ permset_d->ae_perm = 0;
3529+
3530+ return 0;
3531+}
3532+
0f6733d8 3533+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
3534+{
3535+ if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3536+ && perm != SMB_ACL_EXECUTE) {
3537+ errno = EINVAL;
3538+ return -1;
3539+ }
3540+
3541+ if (permset_d == NULL) {
3542+ errno = EINVAL;
3543+ return -1;
3544+ }
3545+
3546+ permset_d->ae_perm |= perm;
3547+
3548+ return 0;
3549+}
3550+
0f6733d8 3551+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
fa26e11c
WD
3552+{
3553+ return permset_d->ae_perm & perm;
3554+}
3555+
0f6733d8 3556+char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
fa26e11c
WD
3557+{
3558+ return acl_to_text(acl_d->aclp, len_p);
3559+}
3560+
0f6733d8 3561+SMB_ACL_T sys_acl_init(int count)
fa26e11c 3562+{
0f6733d8 3563+ SMB_ACL_T a;
fa26e11c
WD
3564+
3565+ if (count < 0) {
3566+ errno = EINVAL;
3567+ return NULL;
3568+ }
3569+
252945ef 3570+ if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
fa26e11c
WD
3571+ errno = ENOMEM;
3572+ return NULL;
3573+ }
3574+
3575+ a->next = -1;
3576+ a->freeaclp = False;
0f6733d8 3577+ a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
fa26e11c
WD
3578+ a->aclp->acl_cnt = 0;
3579+
3580+ return a;
3581+}
3582+
3583+
0f6733d8 3584+int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
fa26e11c 3585+{
0f6733d8
WD
3586+ SMB_ACL_T acl_d;
3587+ SMB_ACL_ENTRY_T entry_d;
fa26e11c
WD
3588+
3589+ if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3590+ errno = EINVAL;
3591+ return -1;
3592+ }
3593+
3594+ if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
3595+ errno = ENOSPC;
3596+ return -1;
3597+ }
3598+
0f6733d8
WD
3599+ entry_d = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
3600+ entry_d->ae_tag = 0;
3601+ entry_d->ae_id = 0;
3602+ entry_d->ae_perm = 0;
3603+ *entry_p = entry_d;
fa26e11c
WD
3604+
3605+ return 0;
3606+}
3607+
0f6733d8 3608+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
fa26e11c
WD
3609+{
3610+ switch (tag_type) {
3611+ case SMB_ACL_USER:
3612+ case SMB_ACL_USER_OBJ:
3613+ case SMB_ACL_GROUP:
3614+ case SMB_ACL_GROUP_OBJ:
3615+ case SMB_ACL_OTHER:
3616+ case SMB_ACL_MASK:
3617+ entry_d->ae_tag = tag_type;
3618+ break;
3619+ default:
3620+ errno = EINVAL;
3621+ return -1;
3622+ }
3623+
3624+ return 0;
3625+}
3626+
0f6733d8 3627+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
fa26e11c
WD
3628+{
3629+ if (entry_d->ae_tag != SMB_ACL_GROUP
3630+ && entry_d->ae_tag != SMB_ACL_USER) {
3631+ errno = EINVAL;
3632+ return -1;
3633+ }
3634+
3635+ entry_d->ae_id = *((id_t *)qual_p);
3636+
3637+ return 0;
3638+}
3639+
0f6733d8 3640+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
fa26e11c
WD
3641+{
3642+ if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3643+ return EINVAL;
3644+ }
3645+
3646+ entry_d->ae_perm = permset_d->ae_perm;
3647+
3648+ return 0;
3649+}
3650+
0f6733d8 3651+int sys_acl_valid(SMB_ACL_T acl_d)
fa26e11c
WD
3652+{
3653+ return acl_valid(acl_d->aclp);
3654+}
3655+
0f6733d8 3656+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
fa26e11c
WD
3657+{
3658+ return acl_set_file(name, type, acl_d->aclp);
3659+}
3660+
0f6733d8 3661+int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
fa26e11c
WD
3662+{
3663+ return acl_set_fd(fd, acl_d->aclp);
3664+}
3665+
0f6733d8 3666+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
3667+{
3668+ return acl_delete_def_file(name);
3669+}
3670+
0f6733d8 3671+int sys_acl_free_text(char *text)
fa26e11c
WD
3672+{
3673+ return acl_free(text);
3674+}
3675+
0f6733d8 3676+int sys_acl_free_acl(SMB_ACL_T acl_d)
fa26e11c
WD
3677+{
3678+ if (acl_d->freeaclp) {
3679+ acl_free(acl_d->aclp);
3680+ }
3681+ acl_free(acl_d);
3682+ return 0;
3683+}
3684+
0f6733d8 3685+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
3686+{
3687+ return 0;
3688+}
3689+
3690+#elif defined(HAVE_AIX_ACLS)
3691+
3692+/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
3693+
0f6733d8 3694+int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
fa26e11c
WD
3695+{
3696+ struct acl_entry_link *link;
3697+ struct new_acl_entry *entry;
3698+ int keep_going;
3699+
3700+ DEBUG(10,("This is the count: %d\n",theacl->count));
3701+
0f6733d8
WD
3702+ /* Check if count was previously set to -1. *
3703+ * If it was, that means we reached the end *
3704+ * of the acl last time. */
3705+ if(theacl->count == -1)
3706+ return(0);
fa26e11c
WD
3707+
3708+ link = theacl;
0f6733d8
WD
3709+ /* To get to the next acl, traverse linked list until index *
3710+ * of acl matches the count we are keeping. This count is *
3711+ * incremented each time we return an acl entry. */
fa26e11c 3712+
0f6733d8 3713+ for(keep_going = 0; keep_going < theacl->count; keep_going++)
fa26e11c
WD
3714+ link = link->nextp;
3715+
3716+ entry = *entry_p = link->entryp;
3717+
3718+ DEBUG(10,("*entry_p is %d\n",entry_p));
3719+ DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
3720+
3721+ /* Increment count */
3722+ theacl->count++;
0f6733d8 3723+ if(link->nextp == NULL)
fa26e11c
WD
3724+ theacl->count = -1;
3725+
0f6733d8 3726+ return(1);
fa26e11c
WD
3727+}
3728+
0f6733d8 3729+int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
fa26e11c
WD
3730+{
3731+ /* Initialize tag type */
3732+
3733+ *tag_type_p = -1;
3734+ DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
3735+
0f6733d8
WD
3736+ /* Depending on what type of entry we have, *
3737+ * return tag type. */
3738+ switch(entry_d->ace_id->id_type) {
fa26e11c
WD
3739+ case ACEID_USER:
3740+ *tag_type_p = SMB_ACL_USER;
3741+ break;
3742+ case ACEID_GROUP:
3743+ *tag_type_p = SMB_ACL_GROUP;
3744+ break;
3745+
3746+ case SMB_ACL_USER_OBJ:
3747+ case SMB_ACL_GROUP_OBJ:
3748+ case SMB_ACL_OTHER:
3749+ *tag_type_p = entry_d->ace_id->id_type;
3750+ break;
0f6733d8 3751+
fa26e11c 3752+ default:
0f6733d8 3753+ return(-1);
fa26e11c
WD
3754+ }
3755+
0f6733d8 3756+ return(0);
fa26e11c
WD
3757+}
3758+
0f6733d8 3759+int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
fa26e11c
WD
3760+{
3761+ DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
3762+ *permset_p = &entry_d->ace_access;
3763+ DEBUG(10,("**permset_p is %d\n",**permset_p));
0f6733d8
WD
3764+ if(!(**permset_p & S_IXUSR) &&
3765+ !(**permset_p & S_IWUSR) &&
3766+ !(**permset_p & S_IRUSR) &&
3767+ (**permset_p != 0))
3768+ return(-1);
fa26e11c
WD
3769+
3770+ DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
0f6733d8 3771+ return(0);
fa26e11c
WD
3772+}
3773+
0f6733d8 3774+void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
fa26e11c 3775+{
0f6733d8 3776+ return(entry_d->ace_id->id_data);
fa26e11c
WD
3777+}
3778+
0f6733d8 3779+SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
fa26e11c
WD
3780+{
3781+ struct acl *file_acl = (struct acl *)NULL;
3782+ struct acl_entry *acl_entry;
3783+ struct new_acl_entry *new_acl_entry;
3784+ struct ace_id *idp;
3785+ struct acl_entry_link *acl_entry_link;
3786+ struct acl_entry_link *acl_entry_link_head;
3787+ int i;
3788+ int rc = 0;
3789+ uid_t user_id;
3790+
252945ef
WD
3791+ /* AIX has no DEFAULT */
3792+ if ( type == SMB_ACL_TYPE_DEFAULT )
3793+ return NULL;
3794+
fa26e11c 3795+ /* Get the acl using statacl */
0f6733d8 3796+
fa26e11c
WD
3797+ DEBUG(10,("Entering sys_acl_get_file\n"));
3798+ DEBUG(10,("path_p is %s\n",path_p));
3799+
252945ef 3800+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
0f6733d8
WD
3801+
3802+ if(file_acl == NULL) {
fa26e11c
WD
3803+ errno=ENOMEM;
3804+ DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
0f6733d8 3805+ return(NULL);
fa26e11c
WD
3806+ }
3807+
3808+ memset(file_acl,0,BUFSIZ);
3809+
3810+ rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
0f6733d8 3811+ if(rc == -1) {
fa26e11c 3812+ DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
0f6733d8
WD
3813+ SAFE_FREE(file_acl);
3814+ return(NULL);
fa26e11c
WD
3815+ }
3816+
3817+ DEBUG(10,("Got facl and returned it\n"));
3818+
3819+ /* Point to the first acl entry in the acl */
3820+ acl_entry = file_acl->acl_ext;
3821+
0f6733d8
WD
3822+ /* Begin setting up the head of the linked list *
3823+ * that will be used for the storing the acl *
3824+ * in a way that is useful for the posix_acls.c *
3825+ * code. */
fa26e11c
WD
3826+
3827+ acl_entry_link_head = acl_entry_link = sys_acl_init(0);
0f6733d8
WD
3828+ if(acl_entry_link_head == NULL)
3829+ return(NULL);
fa26e11c 3830+
252945ef 3831+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
3832+ if(acl_entry_link->entryp == NULL) {
3833+ SAFE_FREE(file_acl);
fa26e11c
WD
3834+ errno = ENOMEM;
3835+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3836+ return(NULL);
fa26e11c
WD
3837+ }
3838+
3839+ DEBUG(10,("acl_entry is %d\n",acl_entry));
3840+ DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
3841+
0f6733d8
WD
3842+ /* Check if the extended acl bit is on. *
3843+ * If it isn't, do not show the *
3844+ * contents of the acl since AIX intends *
3845+ * the extended info to remain unused */
fa26e11c 3846+
0f6733d8 3847+ if(file_acl->acl_mode & S_IXACL){
fa26e11c 3848+ /* while we are not pointing to the very end */
0f6733d8 3849+ while(acl_entry < acl_last(file_acl)) {
fa26e11c
WD
3850+ /* before we malloc anything, make sure this is */
3851+ /* a valid acl entry and one that we want to map */
3852+ idp = id_nxt(acl_entry->ace_id);
0f6733d8
WD
3853+ if((acl_entry->ace_type == ACC_SPECIFY ||
3854+ (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
3855+ acl_entry = acl_nxt(acl_entry);
3856+ continue;
fa26e11c
WD
3857+ }
3858+
3859+ idp = acl_entry->ace_id;
3860+
0f6733d8
WD
3861+ /* Check if this is the first entry in the linked list. *
3862+ * The first entry needs to keep prevp pointing to NULL *
3863+ * and already has entryp allocated. */
fa26e11c 3864+
0f6733d8 3865+ if(acl_entry_link_head->count != 0) {
252945ef 3866+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
fa26e11c 3867+
0f6733d8
WD
3868+ if(acl_entry_link->nextp == NULL) {
3869+ SAFE_FREE(file_acl);
fa26e11c
WD
3870+ errno = ENOMEM;
3871+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3872+ return(NULL);
fa26e11c
WD
3873+ }
3874+
3875+ acl_entry_link->nextp->prevp = acl_entry_link;
3876+ acl_entry_link = acl_entry_link->nextp;
252945ef 3877+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
3878+ if(acl_entry_link->entryp == NULL) {
3879+ SAFE_FREE(file_acl);
fa26e11c
WD
3880+ errno = ENOMEM;
3881+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3882+ return(NULL);
fa26e11c
WD
3883+ }
3884+ acl_entry_link->nextp = NULL;
3885+ }
3886+
3887+ acl_entry_link->entryp->ace_len = acl_entry->ace_len;
3888+
0f6733d8
WD
3889+ /* Don't really need this since all types are going *
3890+ * to be specified but, it's better than leaving it 0 */
fa26e11c
WD
3891+
3892+ acl_entry_link->entryp->ace_type = acl_entry->ace_type;
0f6733d8 3893+
fa26e11c 3894+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
0f6733d8
WD
3895+
3896+ memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
fa26e11c 3897+
0f6733d8
WD
3898+ /* The access in the acl entries must be left shifted by *
3899+ * three bites, because they will ultimately be compared *
3900+ * to S_IRUSR, S_IWUSR, and S_IXUSR. */
fa26e11c 3901+
0f6733d8 3902+ switch(acl_entry->ace_type){
fa26e11c
WD
3903+ case ACC_PERMIT:
3904+ case ACC_SPECIFY:
3905+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3906+ acl_entry_link->entryp->ace_access <<= 6;
3907+ acl_entry_link_head->count++;
3908+ break;
3909+ case ACC_DENY:
0f6733d8
WD
3910+ /* Since there is no way to return a DENY acl entry *
3911+ * change to PERMIT and then shift. */
fa26e11c
WD
3912+ DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
3913+ acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
3914+ DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
3915+ acl_entry_link->entryp->ace_access <<= 6;
3916+ acl_entry_link_head->count++;
3917+ break;
3918+ default:
0f6733d8 3919+ return(0);
fa26e11c
WD
3920+ }
3921+
3922+ DEBUG(10,("acl_entry = %d\n",acl_entry));
3923+ DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
0f6733d8 3924+
fa26e11c
WD
3925+ acl_entry = acl_nxt(acl_entry);
3926+ }
3927+ } /* end of if enabled */
3928+
0f6733d8
WD
3929+ /* Since owner, group, other acl entries are not *
3930+ * part of the acl entries in an acl, they must *
3931+ * be dummied up to become part of the list. */
fa26e11c 3932+
0f6733d8 3933+ for( i = 1; i < 4; i++) {
fa26e11c 3934+ DEBUG(10,("i is %d\n",i));
0f6733d8 3935+ if(acl_entry_link_head->count != 0) {
252945ef 3936+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8
WD
3937+ if(acl_entry_link->nextp == NULL) {
3938+ SAFE_FREE(file_acl);
fa26e11c
WD
3939+ errno = ENOMEM;
3940+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3941+ return(NULL);
fa26e11c
WD
3942+ }
3943+
3944+ acl_entry_link->nextp->prevp = acl_entry_link;
3945+ acl_entry_link = acl_entry_link->nextp;
252945ef 3946+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8
WD
3947+ if(acl_entry_link->entryp == NULL) {
3948+ SAFE_FREE(file_acl);
fa26e11c
WD
3949+ errno = ENOMEM;
3950+ DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
0f6733d8 3951+ return(NULL);
fa26e11c
WD
3952+ }
3953+ }
3954+
3955+ acl_entry_link->nextp = NULL;
3956+
3957+ new_acl_entry = acl_entry_link->entryp;
3958+ idp = new_acl_entry->ace_id;
3959+
0f6733d8 3960+ new_acl_entry->ace_len = sizeof(struct acl_entry);
fa26e11c 3961+ new_acl_entry->ace_type = ACC_PERMIT;
0f6733d8 3962+ idp->id_len = sizeof(struct ace_id);
fa26e11c 3963+ DEBUG(10,("idp->id_len = %d\n",idp->id_len));
0f6733d8 3964+ memset(idp->id_data,0,sizeof(uid_t));
fa26e11c 3965+
0f6733d8 3966+ switch(i) {
fa26e11c
WD
3967+ case 2:
3968+ new_acl_entry->ace_access = file_acl->g_access << 6;
3969+ idp->id_type = SMB_ACL_GROUP_OBJ;
3970+ break;
3971+
3972+ case 3:
3973+ new_acl_entry->ace_access = file_acl->o_access << 6;
3974+ idp->id_type = SMB_ACL_OTHER;
3975+ break;
0f6733d8 3976+
fa26e11c
WD
3977+ case 1:
3978+ new_acl_entry->ace_access = file_acl->u_access << 6;
3979+ idp->id_type = SMB_ACL_USER_OBJ;
3980+ break;
0f6733d8 3981+
fa26e11c 3982+ default:
0f6733d8 3983+ return(NULL);
fa26e11c
WD
3984+
3985+ }
3986+
3987+ acl_entry_link_head->count++;
3988+ DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
3989+ }
3990+
3991+ acl_entry_link_head->count = 0;
0f6733d8 3992+ SAFE_FREE(file_acl);
fa26e11c 3993+
0f6733d8 3994+ return(acl_entry_link_head);
fa26e11c
WD
3995+}
3996+
0f6733d8 3997+SMB_ACL_T sys_acl_get_fd(int fd)
fa26e11c
WD
3998+{
3999+ struct acl *file_acl = (struct acl *)NULL;
4000+ struct acl_entry *acl_entry;
4001+ struct new_acl_entry *new_acl_entry;
4002+ struct ace_id *idp;
4003+ struct acl_entry_link *acl_entry_link;
4004+ struct acl_entry_link *acl_entry_link_head;
4005+ int i;
4006+ int rc = 0;
4007+ uid_t user_id;
4008+
4009+ /* Get the acl using fstatacl */
0f6733d8 4010+
fa26e11c
WD
4011+ DEBUG(10,("Entering sys_acl_get_fd\n"));
4012+ DEBUG(10,("fd is %d\n",fd));
252945ef 4013+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 4014+
0f6733d8 4015+ if(file_acl == NULL) {
fa26e11c
WD
4016+ errno=ENOMEM;
4017+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8 4018+ return(NULL);
fa26e11c
WD
4019+ }
4020+
4021+ memset(file_acl,0,BUFSIZ);
4022+
4023+ rc = fstatacl(fd,0,file_acl,BUFSIZ);
0f6733d8 4024+ if(rc == -1) {
fa26e11c 4025+ DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
0f6733d8
WD
4026+ SAFE_FREE(file_acl);
4027+ return(NULL);
fa26e11c
WD
4028+ }
4029+
4030+ DEBUG(10,("Got facl and returned it\n"));
4031+
4032+ /* Point to the first acl entry in the acl */
4033+
4034+ acl_entry = file_acl->acl_ext;
0f6733d8
WD
4035+ /* Begin setting up the head of the linked list *
4036+ * that will be used for the storing the acl *
4037+ * in a way that is useful for the posix_acls.c *
4038+ * code. */
fa26e11c
WD
4039+
4040+ acl_entry_link_head = acl_entry_link = sys_acl_init(0);
0f6733d8
WD
4041+ if(acl_entry_link_head == NULL){
4042+ SAFE_FREE(file_acl);
4043+ return(NULL);
fa26e11c
WD
4044+ }
4045+
252945ef 4046+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
fa26e11c 4047+
0f6733d8 4048+ if(acl_entry_link->entryp == NULL) {
fa26e11c
WD
4049+ errno = ENOMEM;
4050+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4051+ SAFE_FREE(file_acl);
4052+ return(NULL);
fa26e11c
WD
4053+ }
4054+
4055+ DEBUG(10,("acl_entry is %d\n",acl_entry));
4056+ DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
0f6733d8
WD
4057+
4058+ /* Check if the extended acl bit is on. *
4059+ * If it isn't, do not show the *
4060+ * contents of the acl since AIX intends *
4061+ * the extended info to remain unused */
4062+
4063+ if(file_acl->acl_mode & S_IXACL){
fa26e11c 4064+ /* while we are not pointing to the very end */
0f6733d8 4065+ while(acl_entry < acl_last(file_acl)) {
fa26e11c
WD
4066+ /* before we malloc anything, make sure this is */
4067+ /* a valid acl entry and one that we want to map */
4068+
4069+ idp = id_nxt(acl_entry->ace_id);
0f6733d8
WD
4070+ if((acl_entry->ace_type == ACC_SPECIFY ||
4071+ (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4072+ acl_entry = acl_nxt(acl_entry);
4073+ continue;
fa26e11c
WD
4074+ }
4075+
4076+ idp = acl_entry->ace_id;
0f6733d8
WD
4077+
4078+ /* Check if this is the first entry in the linked list. *
4079+ * The first entry needs to keep prevp pointing to NULL *
4080+ * and already has entryp allocated. */
fa26e11c 4081+
0f6733d8 4082+ if(acl_entry_link_head->count != 0) {
252945ef 4083+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4084+ if(acl_entry_link->nextp == NULL) {
fa26e11c
WD
4085+ errno = ENOMEM;
4086+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4087+ SAFE_FREE(file_acl);
4088+ return(NULL);
fa26e11c
WD
4089+ }
4090+ acl_entry_link->nextp->prevp = acl_entry_link;
4091+ acl_entry_link = acl_entry_link->nextp;
252945ef 4092+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8 4093+ if(acl_entry_link->entryp == NULL) {
fa26e11c
WD
4094+ errno = ENOMEM;
4095+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4096+ SAFE_FREE(file_acl);
4097+ return(NULL);
fa26e11c
WD
4098+ }
4099+
4100+ acl_entry_link->nextp = NULL;
4101+ }
4102+
4103+ acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4104+
0f6733d8
WD
4105+ /* Don't really need this since all types are going *
4106+ * to be specified but, it's better than leaving it 0 */
fa26e11c
WD
4107+
4108+ acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4109+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4110+
0f6733d8 4111+ memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
fa26e11c 4112+
0f6733d8
WD
4113+ /* The access in the acl entries must be left shifted by *
4114+ * three bites, because they will ultimately be compared *
4115+ * to S_IRUSR, S_IWUSR, and S_IXUSR. */
fa26e11c 4116+
0f6733d8 4117+ switch(acl_entry->ace_type){
fa26e11c
WD
4118+ case ACC_PERMIT:
4119+ case ACC_SPECIFY:
4120+ acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4121+ acl_entry_link->entryp->ace_access <<= 6;
4122+ acl_entry_link_head->count++;
4123+ break;
4124+ case ACC_DENY:
0f6733d8
WD
4125+ /* Since there is no way to return a DENY acl entry *
4126+ * change to PERMIT and then shift. */
fa26e11c
WD
4127+ DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4128+ acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4129+ DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4130+ acl_entry_link->entryp->ace_access <<= 6;
4131+ acl_entry_link_head->count++;
4132+ break;
4133+ default:
0f6733d8 4134+ return(0);
fa26e11c
WD
4135+ }
4136+
4137+ DEBUG(10,("acl_entry = %d\n",acl_entry));
4138+ DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
0f6733d8 4139+
fa26e11c
WD
4140+ acl_entry = acl_nxt(acl_entry);
4141+ }
4142+ } /* end of if enabled */
4143+
0f6733d8
WD
4144+ /* Since owner, group, other acl entries are not *
4145+ * part of the acl entries in an acl, they must *
4146+ * be dummied up to become part of the list. */
fa26e11c 4147+
0f6733d8 4148+ for( i = 1; i < 4; i++) {
fa26e11c 4149+ DEBUG(10,("i is %d\n",i));
0f6733d8 4150+ if(acl_entry_link_head->count != 0){
252945ef 4151+ acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4152+ if(acl_entry_link->nextp == NULL) {
fa26e11c
WD
4153+ errno = ENOMEM;
4154+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8
WD
4155+ SAFE_FREE(file_acl);
4156+ return(NULL);
fa26e11c
WD
4157+ }
4158+
4159+ acl_entry_link->nextp->prevp = acl_entry_link;
4160+ acl_entry_link = acl_entry_link->nextp;
252945ef 4161+ acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
fa26e11c 4162+
0f6733d8
WD
4163+ if(acl_entry_link->entryp == NULL) {
4164+ SAFE_FREE(file_acl);
fa26e11c
WD
4165+ errno = ENOMEM;
4166+ DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
0f6733d8 4167+ return(NULL);
fa26e11c
WD
4168+ }
4169+ }
4170+
4171+ acl_entry_link->nextp = NULL;
0f6733d8 4172+
fa26e11c
WD
4173+ new_acl_entry = acl_entry_link->entryp;
4174+ idp = new_acl_entry->ace_id;
0f6733d8
WD
4175+
4176+ new_acl_entry->ace_len = sizeof(struct acl_entry);
fa26e11c 4177+ new_acl_entry->ace_type = ACC_PERMIT;
0f6733d8 4178+ idp->id_len = sizeof(struct ace_id);
fa26e11c 4179+ DEBUG(10,("idp->id_len = %d\n",idp->id_len));
0f6733d8
WD
4180+ memset(idp->id_data,0,sizeof(uid_t));
4181+
4182+ switch(i) {
fa26e11c
WD
4183+ case 2:
4184+ new_acl_entry->ace_access = file_acl->g_access << 6;
4185+ idp->id_type = SMB_ACL_GROUP_OBJ;
4186+ break;
0f6733d8 4187+
fa26e11c
WD
4188+ case 3:
4189+ new_acl_entry->ace_access = file_acl->o_access << 6;
4190+ idp->id_type = SMB_ACL_OTHER;
4191+ break;
0f6733d8 4192+
fa26e11c
WD
4193+ case 1:
4194+ new_acl_entry->ace_access = file_acl->u_access << 6;
4195+ idp->id_type = SMB_ACL_USER_OBJ;
4196+ break;
0f6733d8 4197+
fa26e11c 4198+ default:
0f6733d8 4199+ return(NULL);
fa26e11c 4200+ }
0f6733d8 4201+
fa26e11c
WD
4202+ acl_entry_link_head->count++;
4203+ DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4204+ }
4205+
4206+ acl_entry_link_head->count = 0;
0f6733d8
WD
4207+ SAFE_FREE(file_acl);
4208+
4209+ return(acl_entry_link_head);
fa26e11c
WD
4210+}
4211+
0f6733d8 4212+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
fa26e11c
WD
4213+{
4214+ *permset = *permset & ~0777;
0f6733d8 4215+ return(0);
fa26e11c
WD
4216+}
4217+
0f6733d8 4218+int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 4219+{
0f6733d8
WD
4220+ if((perm != 0) &&
4221+ (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
4222+ return(-1);
fa26e11c
WD
4223+
4224+ *permset |= perm;
4225+ DEBUG(10,("This is the permset now: %d\n",*permset));
0f6733d8 4226+ return(0);
fa26e11c
WD
4227+}
4228+
0f6733d8 4229+char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
fa26e11c 4230+{
0f6733d8 4231+ return(NULL);
fa26e11c
WD
4232+}
4233+
0f6733d8 4234+SMB_ACL_T sys_acl_init( int count)
fa26e11c
WD
4235+{
4236+ struct acl_entry_link *theacl = NULL;
0f6733d8 4237+
fa26e11c
WD
4238+ DEBUG(10,("Entering sys_acl_init\n"));
4239+
252945ef 4240+ theacl = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4241+ if(theacl == NULL) {
fa26e11c
WD
4242+ errno = ENOMEM;
4243+ DEBUG(0,("Error in sys_acl_init is %d\n",errno));
0f6733d8 4244+ return(NULL);
fa26e11c
WD
4245+ }
4246+
4247+ theacl->count = 0;
4248+ theacl->nextp = NULL;
4249+ theacl->prevp = NULL;
4250+ theacl->entryp = NULL;
4251+ DEBUG(10,("Exiting sys_acl_init\n"));
0f6733d8 4252+ return(theacl);
fa26e11c
WD
4253+}
4254+
0f6733d8 4255+int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
fa26e11c
WD
4256+{
4257+ struct acl_entry_link *theacl;
4258+ struct acl_entry_link *acl_entryp;
4259+ struct acl_entry_link *temp_entry;
4260+ int counting;
4261+
4262+ DEBUG(10,("Entering the sys_acl_create_entry\n"));
4263+
4264+ theacl = acl_entryp = *pacl;
4265+
4266+ /* Get to the end of the acl before adding entry */
4267+
0f6733d8 4268+ for(counting=0; counting < theacl->count; counting++){
fa26e11c
WD
4269+ DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4270+ temp_entry = acl_entryp;
4271+ acl_entryp = acl_entryp->nextp;
4272+ }
4273+
0f6733d8 4274+ if(theacl->count != 0){
252945ef 4275+ temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
0f6733d8 4276+ if(acl_entryp == NULL) {
fa26e11c
WD
4277+ errno = ENOMEM;
4278+ DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
0f6733d8 4279+ return(-1);
fa26e11c
WD
4280+ }
4281+
4282+ DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4283+ acl_entryp->prevp = temp_entry;
4284+ DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
4285+ }
4286+
252945ef 4287+ *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
0f6733d8 4288+ if(*pentry == NULL) {
fa26e11c
WD
4289+ errno = ENOMEM;
4290+ DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
0f6733d8 4291+ return(-1);
fa26e11c
WD
4292+ }
4293+
0f6733d8
WD
4294+ memset(*pentry,0,sizeof(struct new_acl_entry));
4295+ acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
fa26e11c 4296+ acl_entryp->entryp->ace_type = ACC_PERMIT;
0f6733d8 4297+ acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
fa26e11c
WD
4298+ acl_entryp->nextp = NULL;
4299+ theacl->count++;
4300+ DEBUG(10,("Exiting sys_acl_create_entry\n"));
0f6733d8 4301+ return(0);
fa26e11c
WD
4302+}
4303+
0f6733d8 4304+int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
fa26e11c
WD
4305+{
4306+ DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
4307+ entry->ace_id->id_type = tagtype;
4308+ DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
4309+ DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
4310+}
4311+
0f6733d8 4312+int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
fa26e11c
WD
4313+{
4314+ DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
0f6733d8 4315+ memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
fa26e11c 4316+ DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
0f6733d8 4317+ return(0);
fa26e11c
WD
4318+}
4319+
0f6733d8 4320+int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
fa26e11c
WD
4321+{
4322+ DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
0f6733d8
WD
4323+ if(!(*permset & S_IXUSR) &&
4324+ !(*permset & S_IWUSR) &&
4325+ !(*permset & S_IRUSR) &&
4326+ (*permset != 0))
4327+ return(-1);
fa26e11c
WD
4328+
4329+ entry->ace_access = *permset;
4330+ DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
4331+ DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
0f6733d8 4332+ return(0);
fa26e11c
WD
4333+}
4334+
0f6733d8 4335+int sys_acl_valid( SMB_ACL_T theacl )
fa26e11c
WD
4336+{
4337+ int user_obj = 0;
4338+ int group_obj = 0;
4339+ int other_obj = 0;
4340+ struct acl_entry_link *acl_entry;
4341+
0f6733d8 4342+ for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
fa26e11c
WD
4343+ user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
4344+ group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
4345+ other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
4346+ }
4347+
4348+ DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
0f6733d8
WD
4349+
4350+ if(user_obj != 1 || group_obj != 1 || other_obj != 1)
4351+ return(-1);
fa26e11c 4352+
0f6733d8 4353+ return(0);
fa26e11c
WD
4354+}
4355+
0f6733d8 4356+int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
fa26e11c
WD
4357+{
4358+ struct acl_entry_link *acl_entry_link = NULL;
4359+ struct acl *file_acl = NULL;
4360+ struct acl *file_acl_temp = NULL;
4361+ struct acl_entry *acl_entry = NULL;
4362+ struct ace_id *ace_id = NULL;
4363+ uint id_type;
4364+ uint ace_access;
4365+ uint user_id;
4366+ uint acl_length;
4367+ uint rc;
4368+
4369+ DEBUG(10,("Entering sys_acl_set_file\n"));
4370+ DEBUG(10,("File name is %s\n",name));
0f6733d8 4371+
fa26e11c 4372+ /* AIX has no default ACL */
0f6733d8
WD
4373+ if(acltype == SMB_ACL_TYPE_DEFAULT)
4374+ return(0);
fa26e11c
WD
4375+
4376+ acl_length = BUFSIZ;
252945ef 4377+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 4378+
0f6733d8 4379+ if(file_acl == NULL) {
fa26e11c
WD
4380+ errno = ENOMEM;
4381+ DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
0f6733d8 4382+ return(-1);
fa26e11c
WD
4383+ }
4384+
4385+ memset(file_acl,0,BUFSIZ);
4386+
4387+ file_acl->acl_len = ACL_SIZ;
4388+ file_acl->acl_mode = S_IXACL;
4389+
0f6733d8 4390+ for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
fa26e11c
WD
4391+ acl_entry_link->entryp->ace_access >>= 6;
4392+ id_type = acl_entry_link->entryp->ace_id->id_type;
4393+
0f6733d8 4394+ switch(id_type) {
fa26e11c
WD
4395+ case SMB_ACL_USER_OBJ:
4396+ file_acl->u_access = acl_entry_link->entryp->ace_access;
4397+ continue;
4398+ case SMB_ACL_GROUP_OBJ:
4399+ file_acl->g_access = acl_entry_link->entryp->ace_access;
4400+ continue;
4401+ case SMB_ACL_OTHER:
4402+ file_acl->o_access = acl_entry_link->entryp->ace_access;
4403+ continue;
4404+ case SMB_ACL_MASK:
4405+ continue;
4406+ }
4407+
0f6733d8
WD
4408+ if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4409+ acl_length += sizeof(struct acl_entry);
252945ef 4410+ file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
0f6733d8
WD
4411+ if(file_acl_temp == NULL) {
4412+ SAFE_FREE(file_acl);
fa26e11c
WD
4413+ errno = ENOMEM;
4414+ DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
0f6733d8
WD
4415+ return(-1);
4416+ }
fa26e11c
WD
4417+
4418+ memcpy(file_acl_temp,file_acl,file_acl->acl_len);
0f6733d8 4419+ SAFE_FREE(file_acl);
fa26e11c
WD
4420+ file_acl = file_acl_temp;
4421+ }
4422+
4423+ acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
0f6733d8 4424+ file_acl->acl_len += sizeof(struct acl_entry);
fa26e11c
WD
4425+ acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4426+ acl_entry->ace_access = acl_entry_link->entryp->ace_access;
0f6733d8 4427+
fa26e11c 4428+ /* In order to use this, we'll need to wait until we can get denies */
0f6733d8
WD
4429+ /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4430+ acl_entry->ace_type = ACC_SPECIFY; */
fa26e11c
WD
4431+
4432+ acl_entry->ace_type = ACC_SPECIFY;
0f6733d8 4433+
fa26e11c 4434+ ace_id = acl_entry->ace_id;
0f6733d8 4435+
fa26e11c
WD
4436+ ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4437+ DEBUG(10,("The id type is %d\n",ace_id->id_type));
4438+ ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
0f6733d8
WD
4439+ memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4440+ memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
fa26e11c
WD
4441+ }
4442+
0f6733d8 4443+ rc = chacl(name,file_acl,file_acl->acl_len);
fa26e11c
WD
4444+ DEBUG(10,("errno is %d\n",errno));
4445+ DEBUG(10,("return code is %d\n",rc));
0f6733d8 4446+ SAFE_FREE(file_acl);
fa26e11c 4447+ DEBUG(10,("Exiting the sys_acl_set_file\n"));
0f6733d8 4448+ return(rc);
fa26e11c
WD
4449+}
4450+
0f6733d8 4451+int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
fa26e11c
WD
4452+{
4453+ struct acl_entry_link *acl_entry_link = NULL;
4454+ struct acl *file_acl = NULL;
4455+ struct acl *file_acl_temp = NULL;
4456+ struct acl_entry *acl_entry = NULL;
4457+ struct ace_id *ace_id = NULL;
4458+ uint id_type;
4459+ uint user_id;
4460+ uint acl_length;
4461+ uint rc;
0f6733d8 4462+
fa26e11c
WD
4463+ DEBUG(10,("Entering sys_acl_set_fd\n"));
4464+ acl_length = BUFSIZ;
252945ef 4465+ file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
fa26e11c 4466+
0f6733d8 4467+ if(file_acl == NULL) {
fa26e11c
WD
4468+ errno = ENOMEM;
4469+ DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
0f6733d8 4470+ return(-1);
fa26e11c
WD
4471+ }
4472+
4473+ memset(file_acl,0,BUFSIZ);
0f6733d8 4474+
fa26e11c
WD
4475+ file_acl->acl_len = ACL_SIZ;
4476+ file_acl->acl_mode = S_IXACL;
4477+
0f6733d8 4478+ for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
fa26e11c
WD
4479+ acl_entry_link->entryp->ace_access >>= 6;
4480+ id_type = acl_entry_link->entryp->ace_id->id_type;
4481+ DEBUG(10,("The id_type is %d\n",id_type));
4482+
0f6733d8 4483+ switch(id_type) {
fa26e11c
WD
4484+ case SMB_ACL_USER_OBJ:
4485+ file_acl->u_access = acl_entry_link->entryp->ace_access;
4486+ continue;
4487+ case SMB_ACL_GROUP_OBJ:
4488+ file_acl->g_access = acl_entry_link->entryp->ace_access;
4489+ continue;
4490+ case SMB_ACL_OTHER:
4491+ file_acl->o_access = acl_entry_link->entryp->ace_access;
4492+ continue;
4493+ case SMB_ACL_MASK:
4494+ continue;
4495+ }
4496+
0f6733d8
WD
4497+ if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4498+ acl_length += sizeof(struct acl_entry);
252945ef 4499+ file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
0f6733d8
WD
4500+ if(file_acl_temp == NULL) {
4501+ SAFE_FREE(file_acl);
fa26e11c
WD
4502+ errno = ENOMEM;
4503+ DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
0f6733d8 4504+ return(-1);
fa26e11c
WD
4505+ }
4506+
4507+ memcpy(file_acl_temp,file_acl,file_acl->acl_len);
0f6733d8 4508+ SAFE_FREE(file_acl);
fa26e11c
WD
4509+ file_acl = file_acl_temp;
4510+ }
4511+
4512+ acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
0f6733d8 4513+ file_acl->acl_len += sizeof(struct acl_entry);
fa26e11c
WD
4514+ acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4515+ acl_entry->ace_access = acl_entry_link->entryp->ace_access;
0f6733d8 4516+
fa26e11c 4517+ /* In order to use this, we'll need to wait until we can get denies */
0f6733d8 4518+ /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
fa26e11c 4519+ acl_entry->ace_type = ACC_SPECIFY; */
0f6733d8 4520+
fa26e11c 4521+ acl_entry->ace_type = ACC_SPECIFY;
0f6733d8 4522+
fa26e11c 4523+ ace_id = acl_entry->ace_id;
0f6733d8 4524+
fa26e11c
WD
4525+ ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4526+ DEBUG(10,("The id type is %d\n",ace_id->id_type));
4527+ ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
0f6733d8
WD
4528+ memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4529+ memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
fa26e11c 4530+ }
0f6733d8 4531+
fa26e11c
WD
4532+ rc = fchacl(fd,file_acl,file_acl->acl_len);
4533+ DEBUG(10,("errno is %d\n",errno));
4534+ DEBUG(10,("return code is %d\n",rc));
0f6733d8 4535+ SAFE_FREE(file_acl);
fa26e11c 4536+ DEBUG(10,("Exiting sys_acl_set_fd\n"));
0f6733d8 4537+ return(rc);
fa26e11c
WD
4538+}
4539+
0f6733d8 4540+int sys_acl_delete_def_file(const char *name)
fa26e11c
WD
4541+{
4542+ /* AIX has no default ACL */
4543+ return 0;
4544+}
4545+
0f6733d8 4546+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c 4547+{
0f6733d8 4548+ return(*permset & perm);
fa26e11c
WD
4549+}
4550+
0f6733d8 4551+int sys_acl_free_text(char *text)
fa26e11c 4552+{
0f6733d8 4553+ return(0);
fa26e11c
WD
4554+}
4555+
0f6733d8 4556+int sys_acl_free_acl(SMB_ACL_T posix_acl)
fa26e11c
WD
4557+{
4558+ struct acl_entry_link *acl_entry_link;
4559+
0f6733d8
WD
4560+ for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
4561+ SAFE_FREE(acl_entry_link->prevp->entryp);
4562+ SAFE_FREE(acl_entry_link->prevp);
fa26e11c
WD
4563+ }
4564+
0f6733d8
WD
4565+ SAFE_FREE(acl_entry_link->prevp->entryp);
4566+ SAFE_FREE(acl_entry_link->prevp);
4567+ SAFE_FREE(acl_entry_link->entryp);
4568+ SAFE_FREE(acl_entry_link);
4569+
4570+ return(0);
fa26e11c
WD
4571+}
4572+
0f6733d8 4573+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
fa26e11c 4574+{
0f6733d8 4575+ return(0);
fa26e11c
WD
4576+}
4577+
4578+#else /* No ACLs. */
4579+
4df546eb 4580+int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
fa26e11c
WD
4581+{
4582+ errno = ENOSYS;
4583+ return -1;
4584+}
4585+
4df546eb 4586+int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
fa26e11c
WD
4587+{
4588+ errno = ENOSYS;
4589+ return -1;
4590+}
4591+
4df546eb 4592+int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
fa26e11c
WD
4593+{
4594+ errno = ENOSYS;
4595+ return -1;
4596+}
4597+
4df546eb 4598+void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
fa26e11c
WD
4599+{
4600+ errno = ENOSYS;
4601+ return NULL;
4602+}
4603+
4df546eb 4604+SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
fa26e11c
WD
4605+{
4606+ errno = ENOSYS;
5ca9317d 4607+ return (SMB_ACL_T)NULL;
fa26e11c
WD
4608+}
4609+
4df546eb 4610+SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
fa26e11c
WD
4611+{
4612+ errno = ENOSYS;
5ca9317d 4613+ return (SMB_ACL_T)NULL;
fa26e11c
WD
4614+}
4615+
4df546eb 4616+int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
fa26e11c
WD
4617+{
4618+ errno = ENOSYS;
4619+ return -1;
4620+}
4621+
4df546eb 4622+int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
fa26e11c
WD
4623+{
4624+ errno = ENOSYS;
4625+ return -1;
4626+}
4627+
0f6733d8 4628+int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
fa26e11c
WD
4629+{
4630+ errno = ENOSYS;
0f6733d8 4631+ return (permset & perm) ? 1 : 0;
fa26e11c
WD
4632+}
4633+
4df546eb 4634+char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
fa26e11c
WD
4635+{
4636+ errno = ENOSYS;
4637+ return NULL;
4638+}
4639+
4df546eb 4640+int sys_acl_free_text(UNUSED(char *text))
fa26e11c
WD
4641+{
4642+ errno = ENOSYS;
4643+ return -1;
4644+}
4645+
4df546eb 4646+SMB_ACL_T sys_acl_init(UNUSED(int count))
fa26e11c
WD
4647+{
4648+ errno = ENOSYS;
4649+ return NULL;
4650+}
4651+
4df546eb 4652+int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
fa26e11c
WD
4653+{
4654+ errno = ENOSYS;
4655+ return -1;
4656+}
4657+
4df546eb 4658+int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
4659+{
4660+ errno = ENOSYS;
4661+ return -1;
4662+}
4663+
4df546eb 4664+int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
fa26e11c
WD
4665+{
4666+ errno = ENOSYS;
4667+ return -1;
4668+}
4669+
4df546eb 4670+int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
fa26e11c
WD
4671+{
4672+ errno = ENOSYS;
4673+ return -1;
4674+}
4675+
4df546eb 4676+int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
4677+{
4678+ errno = ENOSYS;
4679+ return -1;
4680+}
4681+
4df546eb 4682+int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
4683+{
4684+ errno = ENOSYS;
4685+ return -1;
4686+}
4687+
4df546eb 4688+int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
fa26e11c
WD
4689+{
4690+ errno = ENOSYS;
4691+ return -1;
4692+}
4693+
4df546eb 4694+int sys_acl_delete_def_file(UNUSED(const char *name))
fa26e11c
WD
4695+{
4696+ errno = ENOSYS;
4697+ return -1;
4698+}
4699+
4df546eb 4700+int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
fa26e11c
WD
4701+{
4702+ errno = ENOSYS;
4703+ return -1;
4704+}
4705+
4df546eb 4706+int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
fa26e11c
WD
4707+{
4708+ errno = ENOSYS;
4709+ return -1;
4710+}
4711+
4712+#endif /* No ACLs. */
252945ef
WD
4713+
4714+/************************************************************************
4715+ Deliberately outside the ACL defines. Return 1 if this is a "no acls"
4716+ errno, 0 if not.
4717+************************************************************************/
4718+
4719+int no_acl_syscall_error(int err)
4720+{
4721+#if defined(ENOSYS)
4722+ if (err == ENOSYS) {
4723+ return 1;
4724+ }
4725+#endif
4726+#if defined(ENOTSUP)
4727+ if (err == ENOTSUP) {
4728+ return 1;
4729+ }
4730+#endif
4731+ return 0;
4732+}
4733--- orig/lib/sysacls.h 2005-05-16 23:25:51
4734+++ lib/sysacls.h 2005-05-16 23:25:51
4735@@ -0,0 +1,28 @@
4736+#define SMB_MALLOC(cnt) new_array(char, cnt)
4737+#define SMB_MALLOC_P(obj) new_array(obj, 1)
4738+#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
4739+#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
5ca9317d 4740+#define slprintf snprintf
0f6733d8
WD
4741+
4742+int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
4743+int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
4744+int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
4745+void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
4746+SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
4747+SMB_ACL_T sys_acl_get_fd(int fd);
4748+int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
4749+int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4750+int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4751+char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
4752+SMB_ACL_T sys_acl_init(int count);
4753+int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
4754+int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
4755+int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
4756+int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
4757+int sys_acl_valid(SMB_ACL_T theacl);
4758+int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
4759+int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
4760+int sys_acl_delete_def_file(const char *name);
4761+int sys_acl_free_text(char *text);
4762+int sys_acl_free_acl(SMB_ACL_T the_acl);
4763+int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
bc5988ec
WD
4764--- orig/mkproto.awk 2006-02-02 02:41:09
4765+++ mkproto.awk 2006-02-02 02:41:47
0f6733d8
WD
4766@@ -58,7 +58,7 @@ BEGIN {
4767 next;
4768 }
4769
bc5988ec
WD
4770-!/^OFF_T|^size_t|^off_t|^pid_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
4771+!/^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
4772 next;
4773 }
4774
25d385b9
WD
4775--- orig/options.c 2006-02-03 23:51:57
4776+++ options.c 2006-02-04 19:58:53
610969d1 4777@@ -44,6 +44,7 @@ int keep_dirlinks = 0;
0f6733d8
WD
4778 int copy_links = 0;
4779 int preserve_links = 0;
4780 int preserve_hard_links = 0;
4781+int preserve_acls = 0;
4782 int preserve_perms = 0;
90fa6d68 4783 int preserve_executability = 0;
0f6733d8 4784 int preserve_devices = 0;
90fa6d68 4785@@ -193,6 +194,7 @@ static void print_rsync_version(enum log
0f6733d8
WD
4786 char const *got_socketpair = "no ";
4787 char const *have_inplace = "no ";
4788 char const *hardlinks = "no ";
4789+ char const *acls = "no ";
4790 char const *links = "no ";
4791 char const *ipv6 = "no ";
4792 STRUCT_STAT *dumstat;
90fa6d68 4793@@ -209,6 +211,10 @@ static void print_rsync_version(enum log
0f6733d8
WD
4794 hardlinks = "";
4795 #endif
4796
09fb8f03 4797+#ifdef SUPPORT_ACLS
0f6733d8
WD
4798+ acls = "";
4799+#endif
4800+
09fb8f03 4801 #ifdef SUPPORT_LINKS
0f6733d8
WD
4802 links = "";
4803 #endif
90fa6d68 4804@@ -222,9 +228,9 @@ static void print_rsync_version(enum log
3b05e91f 4805 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
0f6733d8
WD
4806 rprintf(f, "<http://rsync.samba.org/>\n");
4807 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
bc5988ec
WD
4808- "%shard links, %ssymlinks, batchfiles,\n",
4809+ "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
0f6733d8
WD
4810 (int) (sizeof (OFF_T) * 8),
4811- got_socketpair, hardlinks, links);
4812+ got_socketpair, hardlinks, acls, links);
4813
4814 /* Note that this field may not have type ino_t. It depends
4815 * on the complicated interaction between largefile feature
25d385b9 4816@@ -293,6 +299,9 @@ void usage(enum logcode F)
be73a66e 4817 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
0f6733d8 4818 rprintf(F," -p, --perms preserve permissions\n");
90fa6d68 4819 rprintf(F," -E, --executability preserve the file's executability\n");
25d385b9 4820+#ifdef SUPPORT_ACLS
0f6733d8 4821+ rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
25d385b9 4822+#endif
90fa6d68
WD
4823 rprintf(F," --chmod=CHMOD change destination permissions\n");
4824 rprintf(F," -o, --owner preserve owner (super-user only)\n");
0f6733d8 4825 rprintf(F," -g, --group preserve group\n");
25d385b9 4826@@ -409,6 +418,9 @@ static struct poptOption long_options[]
489b0a72
WD
4827 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
4828 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
90fa6d68 4829 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
489b0a72
WD
4830+ {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
4831+ {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4832+ {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
4833 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
4834 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
4835 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
25d385b9 4836@@ -1057,6 +1069,23 @@ int parse_arguments(int *argc, const cha
3b05e91f
WD
4837 usage(FINFO);
4838 exit_cleanup(0);
0f6733d8
WD
4839
4840+ case 'A':
4df546eb 4841+#ifdef SUPPORT_ACLS
25d385b9 4842+ preserve_acls = preserve_perms = 1;
489b0a72 4843+ break;
0f6733d8
WD
4844+#else
4845+ /* FIXME: this should probably be ignored with a
4846+ * warning and then countermeasures taken to
4847+ * restrict group and other access in the presence
4848+ * of any more restrictive ACLs, but this is safe
4849+ * for now */
4850+ snprintf(err_buf,sizeof(err_buf),
4851+ "ACLs are not supported on this %s\n",
4852+ am_server ? "server" : "client");
4853+ return 0;
25d385b9 4854+#endif
0f6733d8
WD
4855+
4856+
4857 default:
4858 /* A large opt value means that set_refuse_options()
27a7053c 4859 * turned this option off. */
25d385b9 4860@@ -1497,6 +1526,10 @@ void server_options(char **args,int *arg
c7bc7375 4861
0f6733d8
WD
4862 if (preserve_hard_links)
4863 argstr[x++] = 'H';
25d385b9 4864+#ifdef SUPPORT_ACLS
0f6733d8
WD
4865+ if (preserve_acls)
4866+ argstr[x++] = 'A';
25d385b9 4867+#endif
0f6733d8
WD
4868 if (preserve_uid)
4869 argstr[x++] = 'o';
4870 if (preserve_gid)
90fa6d68 4871--- orig/receiver.c 2006-01-31 02:30:18
5c8b4e6e
WD
4872+++ receiver.c 2006-02-03 05:46:43
4873@@ -410,6 +410,10 @@ int recv_files(int f_in, struct file_lis
26c810d8
WD
4874 int itemizing = am_daemon ? daemon_log_format_has_i
4875 : !am_server && log_format_has_i;
4876 int max_phase = protocol_version >= 29 ? 2 : 1;
4877+ int dflt_perms = (ACCESSPERMS & ~orig_umask);
5c8b4e6e 4878+#ifdef SUPPORT_ACLS
26c810d8 4879+ char *parent_dirname = "";
5c8b4e6e 4880+#endif
26c810d8
WD
4881 int i, recv_ok;
4882
4883 if (verbose > 2)
5c8b4e6e 4884@@ -607,7 +611,16 @@ int recv_files(int f_in, struct file_lis
90fa6d68
WD
4885 * mode based on the local permissions and some heuristics. */
4886 if (!preserve_perms) {
4887 int exists = fd1 != -1;
4888- file->mode = dest_mode(file->mode, st.st_mode, exists);
26c810d8 4889+#ifdef SUPPORT_ACLS
5c8b4e6e
WD
4890+ char *dn = file->dirname ? file->dirname : ".";
4891+ if (parent_dirname != dn
4892+ && strcmp(parent_dirname, dn) != 0) {
4893+ dflt_perms = default_perms_for_dir(dn);
4894+ parent_dirname = dn;
26c810d8
WD
4895+ }
4896+#endif
4897+ file->mode = dest_mode(file->mode, st.st_mode,
4898+ dflt_perms, exists);
90fa6d68
WD
4899 }
4900
4901 /* We now check to see if we are writing file "inplace" */
25d385b9
WD
4902--- orig/rsync.c 2006-02-04 19:52:05
4903+++ rsync.c 2006-02-04 19:53:13
4904@@ -54,7 +54,8 @@ void free_sums(struct sum_struct *s)
90fa6d68
WD
4905
4906 /* This is only called when we aren't preserving permissions. Figure out what
4907 * the permissions should be and return them merged back into the mode. */
4908-mode_t dest_mode(mode_t flist_mode, mode_t dest_mode, int exists)
26c810d8
WD
4909+mode_t dest_mode(mode_t flist_mode, mode_t dest_mode, int dflt_perms,
4910+ int exists)
90fa6d68 4911 {
25d385b9 4912 /* If the file already exists, we'll return the local permissions,
90fa6d68 4913 * possibly tweaked by the --executability option. */
25d385b9 4914@@ -69,7 +70,7 @@ mode_t dest_mode(mode_t flist_mode, mode
90fa6d68
WD
4915 dest_mode |= (dest_mode & 0444) >> 2;
4916 }
26c810d8 4917 } else
25d385b9
WD
4918- dest_mode = flist_mode & ACCESSPERMS & ~orig_umask;
4919+ dest_mode = (flist_mode & ACCESSPERMS & dflt_perms) | S_IWUSR;
90fa6d68
WD
4920 return (flist_mode & ~CHMOD_BITS) | (dest_mode & CHMOD_BITS);
4921 }
4922
25d385b9 4923@@ -168,6 +169,14 @@ int set_file_attrs(char *fname, struct f
0f6733d8
WD
4924 }
4925 #endif
4926
4927+ /* If this is a directory, SET_ACL() will be called on the cleanup
4edb99c8
WD
4928+ * receive_generator() pass (if we called it here, we might clobber
4929+ * writability on the directory). Everything else is OK to do now. */
0f6733d8
WD
4930+ if (!S_ISDIR(st->st_mode)) {
4931+ if (SET_ACL(fname, file) == 0)
4932+ updated = 1;
4933+ }
4934+
90fa6d68 4935 if (verbose > 1 && flags & ATTRS_REPORT) {
1d15f8d9
WD
4936 enum logcode code = daemon_log_format_has_i || dry_run
4937 ? FCLIENT : FINFO;
25d385b9 4938--- orig/rsync.h 2006-02-03 20:00:36
26c810d8 4939+++ rsync.h 2006-01-31 19:27:00
5c8b4e6e 4940@@ -657,6 +657,44 @@ struct chmod_mode_struct;
bc5988ec
WD
4941
4942 #define UNUSED(x) x __attribute__((__unused__))
0f6733d8 4943
4df546eb
WD
4944+#if HAVE_POSIX_ACLS|HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|\
4945+ HAVE_HPUX_ACLS|HAVE_IRIX_ACLS|HAVE_AIX_ACLS|HAVE_TRU64_ACLS
4946+#define SUPPORT_ACLS 1
4947+#endif
0f6733d8 4948+
4df546eb
WD
4949+#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
4950+#define ACLS_NEED_MASK 1
4951+#endif
0f6733d8 4952+
4df546eb
WD
4953+#ifdef SUPPORT_ACLS
4954+#ifdef HAVE_SYS_ACL_H
0f6733d8
WD
4955+#include <sys/acl.h>
4956+#endif
4957+#define MAKE_ACL(file, fname) make_acl(file, fname)
4958+#define SEND_ACL(file, f) send_acl(file, f)
4959+#define RECEIVE_ACL(file, f) receive_acl(file, f)
4960+#define SORT_FILE_ACL_INDEX_LISTS() sort_file_acl_index_lists()
4961+#define SET_ACL(fname, file) set_acl(fname, file)
4962+#define NEXT_ACL_UID() next_acl_uid()
4963+#define ACL_UID_MAP(uid) acl_uid_map(uid)
4964+#define PUSH_KEEP_BACKUP_ACL(file, orig, dest) \
4965+ push_keep_backup_acl(file, orig, dest)
4966+#define CLEANUP_KEEP_BACKUP_ACL() cleanup_keep_backup_acl()
4967+#define DUP_ACL(orig, dest, mode) dup_acl(orig, dest, mode)
4968+#else /* SUPPORT_ACLS */
4969+#define MAKE_ACL(file, fname) 1 /* checked return value */
4970+#define SEND_ACL(file, f)
4971+#define RECEIVE_ACL(file, f)
4972+#define SORT_FILE_ACL_INDEX_LISTS()
05b88206 4973+#define SET_ACL(fname, file) 1 /* checked return value */
0f6733d8
WD
4974+#define NEXT_ACL_UID()
4975+#define ACL_UID_MAP(uid)
4976+#define PUSH_KEEP_BACKUP_ACL(file, orig, dest)
4977+#define CLEANUP_KEEP_BACKUP_ACL()
05b88206 4978+#define DUP_ACL(src, orig, mode) 1 /* checked return value */
0f6733d8
WD
4979+#endif /* SUPPORT_ACLS */
4980+#include "smb_acls.h"
4981+
4982 #include "proto.h"
4983
4984 /* We have replacement versions of these if they're missing. */
25d385b9 4985--- orig/rsync.yo 2006-02-03 23:51:58
90fa6d68
WD
4986+++ rsync.yo 2006-01-31 03:14:05
4987@@ -317,6 +317,7 @@ to the detailed description below for a
be73a66e 4988 -K, --keep-dirlinks treat symlinked dir on receiver as dir
0f6733d8 4989 -p, --perms preserve permissions
90fa6d68
WD
4990 -E, --executability preserve executability
4991+ -A, --acls preserve ACLs (implies -p) [non-standard]
4992 --chmod=CHMOD change destination permissions
4993 -o, --owner preserve owner (super-user only)
0f6733d8 4994 -g, --group preserve group
90fa6d68
WD
4995@@ -691,14 +692,23 @@ quote(itemize(
4996 permissions, though the bf(--executability) option might change just
4997 the execute permission for the file.
4998 it() Each new file gets its permissions set based on the source file's
4999- permissions, but masked by the receiving end's umask setting (including
5000+ permissions, but masked by the receiving end's destination-default
5001+ permissions (which is either based on the ACL of the destination
5002+ directory, if available, or the receiving end's umask setting) and
5003+ includes
5004 the stripping of the three special permission bits).
5005+ Hint: Using bf(--chmod=ugo=rwX) without bf(--perms) will cause new
5006+ files to get all the destination-default permissions.
5007 ))
5008
5009 Thus, when bf(--perms) and bf(--executability) are both disabled,
5010 rsync's behavior is the same as that of other file-copy utilities,
5011 such as bf(cp)(1) and bf(tar)(1).
5012
5013+This version of rsync observes default ACLs; patched versions of rsync only
5014+applied the umask, and could thus set wrong permissions in the presence of
5015+default ACLs.
5016+
5017 dit(bf(-E, --executability)) This option causes rsync to preserve the
5018 executability (or non-executability) of regular files when bf(--perms) is
5019 not enabled. A regular file is considered to be executable if at least one
5020@@ -713,6 +723,10 @@ quote(itemize(
5021
5022 If bf(--perms) is enabled, this option is ignored.
5023
5024+dit(bf(-A, --acls)) This option causes rsync to update the destination
5025+ACLs to be the same as the source ACLs. This nonstandard option only
5026+works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
5027+
5028 dit(bf(--chmod)) This option tells rsync to apply one or more
5029 comma-separated "chmod" strings to the permission of the files in the
5030 transfer. The resulting value is treated as though it was the permissions
0f6733d8
WD
5031--- orig/smb_acls.h 2004-06-30 00:04:07
5032+++ smb_acls.h 2004-06-30 00:04:07
5033@@ -0,0 +1,277 @@
5034+/*
5035+ Unix SMB/Netbios implementation.
5036+ Version 2.2.x
5037+ Portable SMB ACL interface
5038+ Copyright (C) Jeremy Allison 2000
5039+
5040+ This program is free software; you can redistribute it and/or modify
5041+ it under the terms of the GNU General Public License as published by
5042+ the Free Software Foundation; either version 2 of the License, or
5043+ (at your option) any later version.
5044+
5045+ This program is distributed in the hope that it will be useful,
5046+ but WITHOUT ANY WARRANTY; without even the implied warranty of
5047+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5048+ GNU General Public License for more details.
5049+
5050+ You should have received a copy of the GNU General Public License
5051+ along with this program; if not, write to the Free Software
5052+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
5053+*/
5054+
5055+#ifndef _SMB_ACLS_H
5056+#define _SMB_ACLS_H
5057+
5058+#if defined(HAVE_POSIX_ACLS)
5059+
5060+/* This is an identity mapping (just remove the SMB_). */
5061+
5062+#define SMB_ACL_TAG_T acl_tag_t
5063+#define SMB_ACL_TYPE_T acl_type_t
5064+#define SMB_ACL_PERMSET_T acl_permset_t
5065+#define SMB_ACL_PERM_T acl_perm_t
5066+#define SMB_ACL_READ ACL_READ
5067+#define SMB_ACL_WRITE ACL_WRITE
5068+#define SMB_ACL_EXECUTE ACL_EXECUTE
5069+
5070+/* Types of ACLs. */
5071+#define SMB_ACL_USER ACL_USER
5072+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5073+#define SMB_ACL_GROUP ACL_GROUP
5074+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5075+#define SMB_ACL_OTHER ACL_OTHER
5076+#define SMB_ACL_MASK ACL_MASK
5077+
5078+#define SMB_ACL_T acl_t
5079+
5080+#define SMB_ACL_ENTRY_T acl_entry_t
5081+
5082+#define SMB_ACL_FIRST_ENTRY ACL_FIRST_ENTRY
5083+#define SMB_ACL_NEXT_ENTRY ACL_NEXT_ENTRY
5084+
5085+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5086+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5087+
5088+#elif defined(HAVE_TRU64_ACLS)
5089+
5090+/* This is for DEC/Compaq Tru64 UNIX */
5091+
5092+#define SMB_ACL_TAG_T acl_tag_t
5093+#define SMB_ACL_TYPE_T acl_type_t
5094+#define SMB_ACL_PERMSET_T acl_permset_t
5095+#define SMB_ACL_PERM_T acl_perm_t
5096+#define SMB_ACL_READ ACL_READ
5097+#define SMB_ACL_WRITE ACL_WRITE
5098+#define SMB_ACL_EXECUTE ACL_EXECUTE
5099+
5100+/* Types of ACLs. */
5101+#define SMB_ACL_USER ACL_USER
5102+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5103+#define SMB_ACL_GROUP ACL_GROUP
5104+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5105+#define SMB_ACL_OTHER ACL_OTHER
5106+#define SMB_ACL_MASK ACL_MASK
5107+
5108+#define SMB_ACL_T acl_t
5109+
5110+#define SMB_ACL_ENTRY_T acl_entry_t
5111+
5112+#define SMB_ACL_FIRST_ENTRY 0
5113+#define SMB_ACL_NEXT_ENTRY 1
5114+
5115+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5116+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5117+
5118+#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
5119+/*
5120+ * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
5121+ * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
5122+ */
5123+
5124+/* SVR4.2 ES/MP ACLs */
5125+typedef int SMB_ACL_TAG_T;
5126+typedef int SMB_ACL_TYPE_T;
5127+typedef ushort *SMB_ACL_PERMSET_T;
5128+typedef ushort SMB_ACL_PERM_T;
5129+#define SMB_ACL_READ 4
5130+#define SMB_ACL_WRITE 2
5131+#define SMB_ACL_EXECUTE 1
5132+
5133+/* Types of ACLs. */
5134+#define SMB_ACL_USER USER
5135+#define SMB_ACL_USER_OBJ USER_OBJ
5136+#define SMB_ACL_GROUP GROUP
5137+#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5138+#define SMB_ACL_OTHER OTHER_OBJ
5139+#define SMB_ACL_MASK CLASS_OBJ
5140+
5141+typedef struct SMB_ACL_T {
5142+ int size;
5143+ int count;
5144+ int next;
5145+ struct acl acl[1];
5146+} *SMB_ACL_T;
5147+
5148+typedef struct acl *SMB_ACL_ENTRY_T;
5149+
5150+#define SMB_ACL_FIRST_ENTRY 0
5151+#define SMB_ACL_NEXT_ENTRY 1
5152+
5153+#define SMB_ACL_TYPE_ACCESS 0
5154+#define SMB_ACL_TYPE_DEFAULT 1
5155+
5156+#elif defined(HAVE_HPUX_ACLS)
5157+
5158+/*
5159+ * Based on the Solaris & UnixWare code.
5160+ */
5161+
5162+#undef GROUP
5163+#include <sys/aclv.h>
5164+
5165+/* SVR4.2 ES/MP ACLs */
5166+typedef int SMB_ACL_TAG_T;
5167+typedef int SMB_ACL_TYPE_T;
5168+typedef ushort *SMB_ACL_PERMSET_T;
5169+typedef ushort SMB_ACL_PERM_T;
5170+#define SMB_ACL_READ 4
5171+#define SMB_ACL_WRITE 2
5172+#define SMB_ACL_EXECUTE 1
5173+
5174+/* Types of ACLs. */
5175+#define SMB_ACL_USER USER
5176+#define SMB_ACL_USER_OBJ USER_OBJ
5177+#define SMB_ACL_GROUP GROUP
5178+#define SMB_ACL_GROUP_OBJ GROUP_OBJ
5179+#define SMB_ACL_OTHER OTHER_OBJ
5180+#define SMB_ACL_MASK CLASS_OBJ
5181+
5182+typedef struct SMB_ACL_T {
5183+ int size;
5184+ int count;
5185+ int next;
5186+ struct acl acl[1];
5187+} *SMB_ACL_T;
5188+
5189+typedef struct acl *SMB_ACL_ENTRY_T;
5190+
5191+#define SMB_ACL_FIRST_ENTRY 0
5192+#define SMB_ACL_NEXT_ENTRY 1
5193+
5194+#define SMB_ACL_TYPE_ACCESS 0
5195+#define SMB_ACL_TYPE_DEFAULT 1
5196+
5197+#elif defined(HAVE_IRIX_ACLS)
5198+
5199+#define SMB_ACL_TAG_T acl_tag_t
5200+#define SMB_ACL_TYPE_T acl_type_t
5201+#define SMB_ACL_PERMSET_T acl_permset_t
5202+#define SMB_ACL_PERM_T acl_perm_t
5203+#define SMB_ACL_READ ACL_READ
5204+#define SMB_ACL_WRITE ACL_WRITE
5205+#define SMB_ACL_EXECUTE ACL_EXECUTE
5206+
5207+/* Types of ACLs. */
5208+#define SMB_ACL_USER ACL_USER
5209+#define SMB_ACL_USER_OBJ ACL_USER_OBJ
5210+#define SMB_ACL_GROUP ACL_GROUP
5211+#define SMB_ACL_GROUP_OBJ ACL_GROUP_OBJ
5212+#define SMB_ACL_OTHER ACL_OTHER_OBJ
5213+#define SMB_ACL_MASK ACL_MASK
5214+
5215+typedef struct SMB_ACL_T {
5216+ int next;
5217+ BOOL freeaclp;
5218+ struct acl *aclp;
5219+} *SMB_ACL_T;
5220+
5221+#define SMB_ACL_ENTRY_T acl_entry_t
5222+
5223+#define SMB_ACL_FIRST_ENTRY 0
5224+#define SMB_ACL_NEXT_ENTRY 1
5225+
5226+#define SMB_ACL_TYPE_ACCESS ACL_TYPE_ACCESS
5227+#define SMB_ACL_TYPE_DEFAULT ACL_TYPE_DEFAULT
5228+
5229+#elif defined(HAVE_AIX_ACLS)
5230+
5231+/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
5232+
5233+#include "/usr/include/acl.h"
5234+
5235+typedef uint *SMB_ACL_PERMSET_T;
5236+
5237+struct acl_entry_link{
5238+ struct acl_entry_link *prevp;
5239+ struct new_acl_entry *entryp;
5240+ struct acl_entry_link *nextp;
5241+ int count;
5242+};
5243+
5244+struct new_acl_entry{
5245+ unsigned short ace_len;
5246+ unsigned short ace_type;
5247+ unsigned int ace_access;
5248+ struct ace_id ace_id[1];
5249+};
5250+
5251+#define SMB_ACL_ENTRY_T struct new_acl_entry*
5252+#define SMB_ACL_T struct acl_entry_link*
5253+
5254+#define SMB_ACL_TAG_T unsigned short
5255+#define SMB_ACL_TYPE_T int
5256+#define SMB_ACL_PERM_T uint
5257+#define SMB_ACL_READ S_IRUSR
5258+#define SMB_ACL_WRITE S_IWUSR
5259+#define SMB_ACL_EXECUTE S_IXUSR
5260+
5261+/* Types of ACLs. */
5262+#define SMB_ACL_USER ACEID_USER
5263+#define SMB_ACL_USER_OBJ 3
5264+#define SMB_ACL_GROUP ACEID_GROUP
5265+#define SMB_ACL_GROUP_OBJ 4
5266+#define SMB_ACL_OTHER 5
5267+#define SMB_ACL_MASK 6
5268+
5269+
5270+#define SMB_ACL_FIRST_ENTRY 1
5271+#define SMB_ACL_NEXT_ENTRY 2
5272+
5273+#define SMB_ACL_TYPE_ACCESS 0
5274+#define SMB_ACL_TYPE_DEFAULT 1
5275+
5276+#else /* No ACLs. */
5277+
5278+/* No ACLS - fake it. */
5279+#define SMB_ACL_TAG_T int
5280+#define SMB_ACL_TYPE_T int
5281+#define SMB_ACL_PERMSET_T mode_t
5282+#define SMB_ACL_PERM_T mode_t
5283+#define SMB_ACL_READ S_IRUSR
5284+#define SMB_ACL_WRITE S_IWUSR
5285+#define SMB_ACL_EXECUTE S_IXUSR
5286+
5287+/* Types of ACLs. */
5288+#define SMB_ACL_USER 0
5289+#define SMB_ACL_USER_OBJ 1
5290+#define SMB_ACL_GROUP 2
5291+#define SMB_ACL_GROUP_OBJ 3
5292+#define SMB_ACL_OTHER 4
5293+#define SMB_ACL_MASK 5
5294+
5295+typedef struct SMB_ACL_T {
5296+ int dummy;
5297+} *SMB_ACL_T;
5298+
5299+typedef struct SMB_ACL_ENTRY_T {
5300+ int dummy;
5301+} *SMB_ACL_ENTRY_T;
5302+
5303+#define SMB_ACL_FIRST_ENTRY 0
5304+#define SMB_ACL_NEXT_ENTRY 1
5305+
5306+#define SMB_ACL_TYPE_ACCESS 0
5307+#define SMB_ACL_TYPE_DEFAULT 1
5308+
5309+#endif /* No ACLs. */
5310+#endif /* _SMB_ACLS_H */
25d385b9
WD
5311--- orig/testsuite/default-acls.test 2006-02-04 19:56:21
5312+++ testsuite/default-acls.test 2006-02-04 19:56:21
5313@@ -0,0 +1,52 @@
90fa6d68
WD
5314+#! /bin/sh
5315+
5316+# This program is distributable under the terms of the GNU GPL see
5317+# COPYING).
5318+
5319+# Test that rsync obeys default ACLs. -- Matt McCutchen
5320+
5321+. $srcdir/testsuite/rsync.fns
5322+
25d385b9 5323+$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
90fa6d68
WD
5324+setfacl -dm u::rwx,g::---,o::--- "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
5325+
90fa6d68 5326+# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
25d385b9 5327+testit() {
90fa6d68
WD
5328+ todir="$scratchdir/$1"
5329+ mkdir "$todir"
25d385b9 5330+ # FIXME This doesn't work on solaris...
90fa6d68 5331+ setfacl -k "$todir"
90fa6d68
WD
5332+ [ "$2" ] && setfacl -dm "$2" "$todir"
5333+ # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
5334+ # even though the directory itself is outside the transfer
25d385b9 5335+ $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
0251480d 5336+ check_perms "$todir/to" $4 "Target $1"
25d385b9 5337+ check_perms "$todir/to/dir" $4 "Target $1"
0251480d
WD
5338+ check_perms "$todir/to/file" $3 "Target $1"
5339+ check_perms "$todir/to/program" $4 "Target $1"
90fa6d68
WD
5340+ # Make sure get_local_name doesn't mess us up when transferring only one file
5341+ $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
0251480d 5342+ check_perms "$todir/to/anotherfile" $3 "Target $1"
90fa6d68
WD
5343+}
5344+
25d385b9 5345+mkdir "$scratchdir/dir"
0251480d 5346+echo "File!" >"$scratchdir/file"
25d385b9
WD
5347+echo "#!/bin/sh" >"$scratchdir/program"
5348+chmod 777 "$scratchdir/dir"
0251480d
WD
5349+chmod 666 "$scratchdir/file"
5350+chmod 777 "$scratchdir/program"
5351+
90fa6d68
WD
5352+# Test some target directories
5353+umask 0077
0251480d
WD
5354+testit da777 u::rwx,g::rwx,o::rwx rw-rw-rw- rwxrwxrwx
5355+testit da775 u::rwx,g::rwx,o::r-x rw-rw-r-- rwxrwxr-x
5356+testit da750 u::rwx,g::r-x,o::--- rw-r----- rwxr-x---
5357+testit da770mask u::rwx,g::---,m::rwx,o::--- rw-rw---- rwxrwx---
5358+testit noda1 '' rw------- rwx------
90fa6d68 5359+umask 0000
0251480d 5360+testit noda2 '' rw-rw-rw- rwxrwxrwx
90fa6d68 5361+umask 0022
0251480d 5362+testit noda3 '' rw-r--r-- rwxr-xr-x
90fa6d68
WD
5363+
5364+# Hooray
5365+exit 0
5366--- orig/uidlist.c 2006-01-25 17:15:13
5367+++ uidlist.c 2006-01-25 17:45:21
fa26e11c
WD
5368@@ -34,6 +34,7 @@
5369 extern int verbose;
5370 extern int preserve_uid;
5371 extern int preserve_gid;
5372+extern int preserve_acls;
5373 extern int numeric_ids;
5374 extern int am_root;
5375
5376@@ -274,7 +275,7 @@ void send_uid_list(int f)
5377 if (numeric_ids)
5378 return;
5379
5380- if (preserve_uid) {
5381+ if (preserve_uid || preserve_acls) {
5382 int len;
5383 /* we send sequences of uid/byte-length/name */
5384 for (list = uidlist; list; list = list->next) {
5385@@ -291,7 +292,7 @@ void send_uid_list(int f)
5386 write_int(f, 0);
5387 }
5388
5389- if (preserve_gid) {
5390+ if (preserve_gid || preserve_acls) {
5391 int len;
5392 for (list = gidlist; list; list = list->next) {
5393 if (!list->name)
5394@@ -312,7 +313,7 @@ void recv_uid_list(int f, struct file_li
5395 int id, i;
5396 char *name;
5397
5398- if (preserve_uid && !numeric_ids) {
5399+ if ((preserve_uid || preserve_acls) && !numeric_ids) {
5400 /* read the uid list */
5401 while ((id = read_int(f)) != 0) {
5402 int len = read_byte(f);
6849cd84
WD
5403@@ -324,7 +325,7 @@ void recv_uid_list(int f, struct file_li
5404 }
fa26e11c
WD
5405 }
5406
fa26e11c
WD
5407- if (preserve_gid && !numeric_ids) {
5408+ if ((preserve_gid || preserve_acls) && !numeric_ids) {
5409 /* read the gid list */
5410 while ((id = read_int(f)) != 0) {
5411 int len = read_byte(f);
6849cd84 5412@@ -336,6 +337,18 @@ void recv_uid_list(int f, struct file_li
fa26e11c
WD
5413 }
5414 }
125d7fca 5415
4edb99c8 5416+#ifdef SUPPORT_ACLS
fa26e11c
WD
5417+ if (preserve_acls && !numeric_ids) {
5418+ id_t id;
4edb99c8
WD
5419+ /* The enumerations don't return 0 except to flag the last
5420+ * entry, since uidlist doesn't munge 0 anyway. */
5421+ while ((id = next_acl_uid(flist)) != 0)
fa26e11c 5422+ acl_uid_map(match_uid(id));
4edb99c8 5423+ while ((id = next_acl_gid(flist)) != 0)
fa26e11c
WD
5424+ acl_gid_map(match_gid(id));
5425+ }
5426+#endif /* SUPPORT_ACLS */
125d7fca 5427+
90fa6d68 5428 /* Now convert all the uids/gids from sender values to our values. */
125d7fca 5429 if (am_root && preserve_uid && !numeric_ids) {
90fa6d68 5430 for (i = 0; i < flist->count; i++)