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