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