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