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