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