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