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