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