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