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