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