Fixed some build problems that crept into the code.
[rsync/rsync-patches.git] / acls.diff
1 To use this patch, run these commands for a successful build:
2
3     patch -p1 <patches/acls.diff
4     ./prepare-source
5     ./configure --enable-acl-support
6     make
7
8 See the --acls (-A) option in the revised man page for a note on using this
9 latest ACL-enabling patch to send files to an older ACL-enabled rsync.
10
11 --- old/Makefile.in
12 +++ new/Makefile.in
13 @@ -26,15 +26,15 @@ VERSION=@VERSION@
14  .SUFFIXES:
15  .SUFFIXES: .c .o
16  
17 -HEADERS=byteorder.h config.h errcode.h proto.h rsync.h lib/pool_alloc.h
18 +HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
19  LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
20 -       lib/permstring.o lib/pool_alloc.o @LIBOBJS@
21 +       lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
22  ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
23         zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
24  OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
25         util.o main.o checksum.o match.o syscall.o log.o backup.o
26  OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o \
27 -       fileio.o batch.o clientname.o chmod.o
28 +       fileio.o batch.o clientname.o chmod.o acls.o
29  OBJS3=progress.o pipe.o
30  DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
31  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
32 --- old/acls.c
33 +++ new/acls.c
34 @@ -0,0 +1,1094 @@
35 +/*
36 + * Handle passing Access Control Lists between systems.
37 + *
38 + * Copyright (C) 1996 Andrew Tridgell
39 + * Copyright (C) 1996 Paul Mackerras
40 + * Copyright (C) 2006 Wayne Davison
41 + *
42 + * This program is free software; you can redistribute it and/or modify
43 + * it under the terms of the GNU General Public License as published by
44 + * the Free Software Foundation; either version 2 of the License, or
45 + * (at your option) any later version.
46 + *
47 + * This program is distributed in the hope that it will be useful,
48 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50 + * GNU General Public License for more details.
51 + *
52 + * You should have received a copy of the GNU General Public License along
53 + * with this program; if not, write to the Free Software Foundation, Inc.,
54 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
55 + */
56 +
57 +#include "rsync.h"
58 +#include "lib/sysacls.h"
59 +
60 +#ifdef SUPPORT_ACLS
61 +
62 +extern int dry_run;
63 +extern int read_only;
64 +extern int list_only;
65 +extern int orig_umask;
66 +extern int protocol_version;
67 +
68 +/* === ACL structures === */
69 +
70 +typedef struct {
71 +       id_t id;
72 +       uchar access;
73 +} id_access;
74 +
75 +typedef struct {
76 +       id_access *idas;
77 +       int count;
78 +} ida_entries;
79 +
80 +#define NO_ENTRY ((uchar)0x80)
81 +typedef struct rsync_acl {
82 +       ida_entries users;
83 +       ida_entries groups;
84 +       /* These will be NO_ENTRY if there's no such entry. */
85 +       uchar user_obj;
86 +       uchar group_obj;
87 +       uchar mask;
88 +       uchar other;
89 +} rsync_acl;
90 +
91 +typedef struct {
92 +       rsync_acl racl;
93 +       SMB_ACL_T sacl;
94 +} acl_duo;
95 +
96 +static const rsync_acl empty_rsync_acl = {
97 +       {NULL, 0}, {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
98 +};
99 +
100 +static item_list access_acl_list = EMPTY_ITEM_LIST;
101 +static item_list default_acl_list = EMPTY_ITEM_LIST;
102 +
103 +/* === Calculations on ACL types === */
104 +
105 +static const char *str_acl_type(SMB_ACL_TYPE_T type)
106 +{
107 +       return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS"
108 +            : type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT"
109 +            : "unknown SMB_ACL_TYPE_T";
110 +}
111 +
112 +#define OTHER_TYPE(t) (SMB_ACL_TYPE_ACCESS+SMB_ACL_TYPE_DEFAULT-(t))
113 +#define BUMP_TYPE(t) ((t = OTHER_TYPE(t)) == SMB_ACL_TYPE_DEFAULT)
114 +
115 +static int count_racl_entries(const rsync_acl *racl)
116 +{
117 +       return racl->users.count + racl->groups.count
118 +            + (racl->user_obj != NO_ENTRY)
119 +            + (racl->group_obj != NO_ENTRY)
120 +            + (racl->mask != NO_ENTRY)
121 +            + (racl->other != NO_ENTRY);
122 +}
123 +
124 +static int calc_sacl_entries(const rsync_acl *racl)
125 +{
126 +       /* A System ACL always gets user/group/other permission entries. */
127 +       return racl->users.count + racl->groups.count
128 +#ifdef ACLS_NEED_MASK
129 +            + 4;
130 +#else
131 +            + (racl->mask != NO_ENTRY) + 3;
132 +#endif
133 +}
134 +
135 +/* Extracts and returns the permission bits from the ACL.  This cannot be
136 + * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
137 +static int rsync_acl_get_perms(const rsync_acl *racl)
138 +{
139 +       return (racl->user_obj << 6)
140 +            + ((racl->mask != NO_ENTRY ? racl->mask : racl->group_obj) << 3)
141 +            + racl->other;
142 +}
143 +
144 +/* Removes the permission-bit entries from the ACL because these
145 + * can be reconstructed from the file's mode. */
146 +static void rsync_acl_strip_perms(rsync_acl *racl)
147 +{
148 +       racl->user_obj = NO_ENTRY;
149 +       if (racl->mask == NO_ENTRY)
150 +               racl->group_obj = NO_ENTRY;
151 +       else {
152 +               if (racl->group_obj == racl->mask)
153 +                       racl->group_obj = NO_ENTRY;
154 +               racl->mask = NO_ENTRY;
155 +       }
156 +       racl->other = NO_ENTRY;
157 +}
158 +
159 +/* Given an empty rsync_acl, fake up the permission bits. */
160 +static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
161 +{
162 +       racl->user_obj = (mode >> 6) & 7;
163 +       racl->group_obj = (mode >> 3) & 7;
164 +       racl->other = mode & 7;
165 +}
166 +
167 +/* === Rsync ACL functions === */
168 +
169 +static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
170 +{
171 +       id_access *ida1, *ida2;
172 +       int count = ial1->count;
173 +       if (count != ial2->count)
174 +               return False;
175 +       ida1 = ial1->idas;
176 +       ida2 = ial2->idas;
177 +       for (; count--; ida1++, ida2++) {
178 +               if (ida1->access != ida2->access || ida1->id != ida2->id)
179 +                       return False;
180 +       }
181 +       return True;
182 +}
183 +
184 +static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
185 +{
186 +       return racl1->user_obj == racl2->user_obj
187 +           && racl1->group_obj == racl2->group_obj
188 +           && racl1->mask == racl2->mask
189 +           && racl1->other == racl2->other
190 +           && ida_entries_equal(&racl1->users, &racl2->users)
191 +           && ida_entries_equal(&racl1->groups, &racl2->groups);
192 +}
193 +
194 +/* Are the extended (non-permission-bit) entries equal?  If so, the rest of
195 + * the ACL will be handled by the normal mode-preservation code.  This is
196 + * only meaningful for access ACLs!  Note: the 1st arg is a fully-populated
197 + * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
198 + * that it might have several of its permission objects set to NO_ENTRY. */
199 +static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
200 +                                  const rsync_acl *racl2, mode_t m)
201 +{
202 +       if ((racl1->mask ^ racl2->mask) & NO_ENTRY)
203 +               return False; /* One has a mask and the other doesn't */
204 +
205 +       /* When there's a mask, the group_obj becomes an extended entry. */
206 +       if (racl1->mask != NO_ENTRY) {
207 +               /* A condensed rsync_acl with a mask can only have no
208 +                * group_obj when it was identical to the mask.  This
209 +                * means that it was also identical to the group attrs
210 +                * from the mode. */
211 +               if (racl2->group_obj == NO_ENTRY) {
212 +                       if (racl1->group_obj != ((m >> 3) & 7))
213 +                               return False;
214 +               } else if (racl1->group_obj != racl2->group_obj)
215 +                       return False;
216 +       }
217 +       return ida_entries_equal(&racl1->users, &racl2->users)
218 +           && ida_entries_equal(&racl1->groups, &racl2->groups);
219 +}
220 +
221 +static void rsync_acl_free(rsync_acl *racl)
222 +{
223 +       if (racl->users.idas)
224 +               free(racl->users.idas);
225 +       if (racl->groups.idas)
226 +               free(racl->groups.idas);
227 +       *racl = empty_rsync_acl;
228 +}
229 +
230 +void free_acl(statx *sxp)
231 +{
232 +       if (sxp->acc_acl) {
233 +               rsync_acl_free(sxp->acc_acl);
234 +               free(sxp->acc_acl);
235 +               sxp->acc_acl = NULL;
236 +       }
237 +       if (sxp->def_acl) {
238 +               rsync_acl_free(sxp->def_acl);
239 +               free(sxp->def_acl);
240 +               sxp->def_acl = NULL;
241 +       }
242 +}
243 +
244 +static int id_access_sorter(const void *r1, const void *r2)
245 +{
246 +       id_access *ida1 = (id_access *)r1;
247 +       id_access *ida2 = (id_access *)r2;
248 +       id_t rid1 = ida1->id, rid2 = ida2->id;
249 +       return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
250 +}
251 +
252 +static void sort_ida_entries(ida_entries *idal)
253 +{
254 +       if (!idal->count)
255 +               return;
256 +       qsort(idal->idas, idal->count, sizeof idal->idas[0], id_access_sorter);
257 +}
258 +
259 +/* Transfer the count id_access items out of the temp_ida_list into either
260 + * the users or groups ida_entries list in racl. */
261 +static void save_idas(item_list *temp_ida_list, rsync_acl *racl, SMB_ACL_TAG_T type)
262 +{
263 +       id_access *idas;
264 +       ida_entries *ent;
265 +
266 +       if (temp_ida_list->count) {
267 +               int cnt = temp_ida_list->count;
268 +               id_access *temp_idas = temp_ida_list->items;
269 +               if (!(idas = new_array(id_access, cnt)))
270 +                       out_of_memory("save_idas");
271 +               memcpy(idas, temp_idas, cnt * sizeof *temp_idas);
272 +       } else
273 +               idas = NULL;
274 +
275 +       ent = type == SMB_ACL_USER ? &racl->users : &racl->groups;
276 +
277 +       if (ent->count) {
278 +               rprintf(FERROR, "save_idas: disjoint list found for type %d\n", type);
279 +               exit_cleanup(RERR_UNSUPPORTED);
280 +       }
281 +       ent->count = temp_ida_list->count;
282 +       ent->idas = idas;
283 +
284 +       /* Truncate the temporary list now that its idas have been saved. */
285 +       temp_ida_list->count = 0;
286 +}
287 +
288 +/* === System ACLs === */
289 +
290 +/* Unpack system ACL -> rsync ACL verbatim.  Return whether we succeeded. */
291 +static BOOL unpack_smb_acl(rsync_acl *racl, SMB_ACL_T sacl)
292 +{
293 +       static item_list temp_ida_list = EMPTY_ITEM_LIST;
294 +       SMB_ACL_TAG_T prior_list_type = 0;
295 +       SMB_ACL_ENTRY_T entry;
296 +       const char *errfun;
297 +       int rc;
298 +
299 +       errfun = "sys_acl_get_entry";
300 +       for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
301 +            rc == 1;
302 +            rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
303 +               SMB_ACL_TAG_T tag_type;
304 +               SMB_ACL_PERMSET_T permset;
305 +               uchar access;
306 +               void *qualifier;
307 +               id_access *ida;
308 +               if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
309 +                       errfun = "sys_acl_get_tag_type";
310 +                       break;
311 +               }
312 +               if ((rc = sys_acl_get_permset(entry, &permset))) {
313 +                       errfun = "sys_acl_get_tag_type";
314 +                       break;
315 +               }
316 +               access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
317 +                      | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
318 +                      | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
319 +               /* continue == done with entry; break == store in temporary ida list */
320 +               switch (tag_type) {
321 +               case SMB_ACL_USER_OBJ:
322 +                       if (racl->user_obj == NO_ENTRY)
323 +                               racl->user_obj = access;
324 +                       else
325 +                               rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
326 +                       continue;
327 +               case SMB_ACL_USER:
328 +                       break;
329 +               case SMB_ACL_GROUP_OBJ:
330 +                       if (racl->group_obj == NO_ENTRY)
331 +                               racl->group_obj = access;
332 +                       else
333 +                               rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
334 +                       continue;
335 +               case SMB_ACL_GROUP:
336 +                       break;
337 +               case SMB_ACL_MASK:
338 +                       if (racl->mask == NO_ENTRY)
339 +                               racl->mask = access;
340 +                       else
341 +                               rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
342 +                       continue;
343 +               case SMB_ACL_OTHER:
344 +                       if (racl->other == NO_ENTRY)
345 +                               racl->other = access;
346 +                       else
347 +                               rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
348 +                       continue;
349 +               default:
350 +                       rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
351 +                       continue;
352 +               }
353 +               if (!(qualifier = sys_acl_get_qualifier(entry))) {
354 +                       errfun = "sys_acl_get_tag_type";
355 +                       rc = EINVAL;
356 +                       break;
357 +               }
358 +               if (tag_type != prior_list_type) {
359 +                       if (prior_list_type)
360 +                               save_idas(&temp_ida_list, racl, prior_list_type);
361 +                       prior_list_type = tag_type;
362 +               }
363 +               ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
364 +               ida->id = *((id_t *)qualifier);
365 +               ida->access = access;
366 +               sys_acl_free_qualifier(qualifier, tag_type);
367 +       }
368 +       if (rc) {
369 +               rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
370 +               rsync_acl_free(racl);
371 +               return False;
372 +       }
373 +       if (prior_list_type)
374 +               save_idas(&temp_ida_list, racl, prior_list_type);
375 +
376 +       sort_ida_entries(&racl->users);
377 +       sort_ida_entries(&racl->groups);
378 +
379 +#ifdef ACLS_NEED_MASK
380 +       if (!racl->users.count && !racl->groups.count && racl->mask != NO_ENTRY) {
381 +               /* Throw away a superfluous mask, but mask off the
382 +                * group perms with it first. */
383 +               racl->group_obj &= racl->mask;
384 +               racl->mask = NO_ENTRY;
385 +       }
386 +#endif
387 +
388 +       return True;
389 +}
390 +
391 +/* Synactic sugar for system calls */
392 +
393 +#define CALL_OR_ERROR(func,args,str) \
394 +       do { \
395 +               if (func args) { \
396 +                       errfun = str; \
397 +                       goto error_exit; \
398 +               } \
399 +       } while (0)
400 +
401 +#define COE(func,args) CALL_OR_ERROR(func,args,#func)
402 +#define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
403 +
404 +/* Store the permissions in the system ACL entry. */
405 +static int store_access_in_entry(uchar access, SMB_ACL_ENTRY_T entry)
406 +{
407 +       const char *errfun = NULL;
408 +       SMB_ACL_PERMSET_T permset;
409 +
410 +       COE( sys_acl_get_permset,(entry, &permset) );
411 +       COE( sys_acl_clear_perms,(permset) );
412 +       if (access & 4)
413 +               COE( sys_acl_add_perm,(permset, SMB_ACL_READ) );
414 +       if (access & 2)
415 +               COE( sys_acl_add_perm,(permset, SMB_ACL_WRITE) );
416 +       if (access & 1)
417 +               COE( sys_acl_add_perm,(permset, SMB_ACL_EXECUTE) );
418 +       COE( sys_acl_set_permset,(entry, permset) );
419 +
420 +       return 0;
421 +
422 +  error_exit:
423 +       rsyserr(FERROR, errno, "store_access_in_entry %s()", errfun);
424 +       return -1;
425 +}
426 +
427 +/* Pack rsync ACL -> system ACL verbatim.  Return whether we succeeded. */
428 +static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
429 +{
430 +#ifdef ACLS_NEED_MASK
431 +       uchar mask_bits;
432 +#endif
433 +       size_t count;
434 +       id_access *ida;
435 +       const char *errfun = NULL;
436 +       SMB_ACL_ENTRY_T entry;
437 +
438 +       if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
439 +               rsyserr(FERROR, errno, "pack_smb_acl: sys_acl_init()");
440 +               return False;
441 +       }
442 +
443 +       COE( sys_acl_create_entry,(smb_acl, &entry) );
444 +       COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
445 +       COE2( store_access_in_entry,(racl->user_obj & 7, entry) );
446 +
447 +       for (ida = racl->users.idas, count = racl->users.count; count--; ida++) {
448 +               COE( sys_acl_create_entry,(smb_acl, &entry) );
449 +               COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
450 +               COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
451 +               COE2( store_access_in_entry,(ida->access, entry) );
452 +       }
453 +
454 +       COE( sys_acl_create_entry,(smb_acl, &entry) );
455 +       COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
456 +       COE2( store_access_in_entry,(racl->group_obj & 7, entry) );
457 +
458 +       for (ida = racl->groups.idas, count = racl->groups.count; count--; ida++) {
459 +               COE( sys_acl_create_entry,(smb_acl, &entry) );
460 +               COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
461 +               COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
462 +               COE2( store_access_in_entry,(ida->access, entry) );
463 +       }
464 +
465 +#ifdef ACLS_NEED_MASK
466 +       mask_bits = racl->mask == NO_ENTRY ? racl->group_obj & 7 : racl->mask;
467 +       COE( sys_acl_create_entry,(smb_acl, &entry) );
468 +       COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
469 +       COE2( store_access_in_entry,(mask_bits, entry) );
470 +#else
471 +       if (racl->mask != NO_ENTRY) {
472 +               COE( sys_acl_create_entry,(smb_acl, &entry) );
473 +               COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
474 +               COE2( store_access_in_entry,(racl->mask, entry) );
475 +       }
476 +#endif
477 +
478 +       COE( sys_acl_create_entry,(smb_acl, &entry) );
479 +       COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
480 +       COE2( store_access_in_entry,(racl->other & 7, entry) );
481 +
482 +#ifdef DEBUG
483 +       if (sys_acl_valid(*smb_acl) < 0)
484 +               rprintf(FERROR, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
485 +#endif
486 +
487 +       return True;
488 +
489 +  error_exit:
490 +       if (errfun) {
491 +               rsyserr(FERROR, errno, "pack_smb_acl %s()", errfun);
492 +       }
493 +       sys_acl_free_acl(*smb_acl);
494 +       return False;
495 +}
496 +
497 +static int find_matching_rsync_acl(SMB_ACL_TYPE_T type,
498 +                                  const item_list *racl_list,
499 +                                  const rsync_acl *racl)
500 +{
501 +       static int access_match = -1, default_match = -1;
502 +       int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
503 +       size_t count = racl_list->count;
504 +
505 +       /* If this is the first time through or we didn't match the last
506 +        * time, then start at the end of the list, which should be the
507 +        * best place to start hunting. */
508 +       if (*match == -1)
509 +               *match = racl_list->count - 1;
510 +       while (count--) {
511 +               rsync_acl *base = racl_list->items;
512 +               if (rsync_acl_equal(base + *match, racl))
513 +                       return *match;
514 +               if (!(*match)--)
515 +                       *match = racl_list->count - 1;
516 +       }
517 +
518 +       *match = -1;
519 +       return *match;
520 +}
521 +
522 +/* Return the Access Control List for the given filename. */
523 +int get_acl(const char *fname, statx *sxp)
524 +{
525 +       SMB_ACL_TYPE_T type;
526 +
527 +       if (S_ISLNK(sxp->st.st_mode))
528 +               return 0;
529 +
530 +       type = SMB_ACL_TYPE_ACCESS;
531 +       do {
532 +               SMB_ACL_T sacl = sys_acl_get_file(fname, type);
533 +               rsync_acl *racl = new(rsync_acl);
534 +
535 +               if (!racl)
536 +                       out_of_memory("get_acl");
537 +               *racl = empty_rsync_acl;
538 +               if (type == SMB_ACL_TYPE_ACCESS)
539 +                       sxp->acc_acl = racl;
540 +               else
541 +                       sxp->def_acl = racl;
542 +
543 +               if (sacl) {
544 +                       BOOL ok = unpack_smb_acl(racl, sacl);
545 +
546 +                       sys_acl_free_acl(sacl);
547 +                       if (!ok) {
548 +                               free_acl(sxp);
549 +                               return -1;
550 +                       }
551 +               } else if (errno == ENOTSUP || errno == ENOSYS) {
552 +                       /* ACLs are not supported, so pretend we have a basic ACL. */
553 +                       if (type == SMB_ACL_TYPE_ACCESS)
554 +                               rsync_acl_fake_perms(racl, sxp->st.st_mode);
555 +               } else {
556 +                       rsyserr(FERROR, errno, "get_acl: sys_acl_get_file(%s, %s)",
557 +                               fname, str_acl_type(type));
558 +                       free_acl(sxp);
559 +                       return -1;
560 +               }
561 +       } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
562 +
563 +       return 0;
564 +}
565 +
566 +/* === Send functions === */
567 +
568 +/* The general strategy with the tag_type <-> character mapping is that
569 + * lowercase implies that no qualifier follows, where uppercase does.
570 + * A similar idiom for the ACL type (access or default) itself, but
571 + * lowercase in this instance means there's no ACL following, so the
572 + * ACL is a repeat, so the receiver should reuse the last of the same
573 + * type ACL. */
574 +
575 +/* Send the ida list over the file descriptor. */
576 +static void send_ida_entries(int f, const ida_entries *idal, char tag_char)
577 +{
578 +       id_access *ida;
579 +       size_t count = idal->count;
580 +       for (ida = idal->idas; count--; ida++) {
581 +               write_byte(f, tag_char);
582 +               write_byte(f, ida->access);
583 +               write_int(f, ida->id);
584 +               /* FIXME: sorta wasteful: we should maybe buffer as
585 +                * many ids as max(ACL_USER + ACL_GROUP) objects to
586 +                * keep from making so many calls. */
587 +               if (tag_char == 'U')
588 +                       add_uid(ida->id);
589 +               else
590 +                       add_gid(ida->id);
591 +       }
592 +}
593 +
594 +/* Send an rsync ACL over the file descriptor. */
595 +static void send_rsync_acl(int f, const rsync_acl *racl)
596 +{
597 +       size_t count = count_racl_entries(racl);
598 +       write_int(f, count);
599 +       if (racl->user_obj != NO_ENTRY) {
600 +               write_byte(f, 'u');
601 +               write_byte(f, racl->user_obj);
602 +       }
603 +       send_ida_entries(f, &racl->users, 'U');
604 +       if (racl->group_obj != NO_ENTRY) {
605 +               write_byte(f, 'g');
606 +               write_byte(f, racl->group_obj);
607 +       }
608 +       send_ida_entries(f, &racl->groups, 'G');
609 +       if (racl->mask != NO_ENTRY) {
610 +               write_byte(f, 'm');
611 +               write_byte(f, racl->mask);
612 +       }
613 +       if (racl->other != NO_ENTRY) {
614 +               write_byte(f, 'o');
615 +               write_byte(f, racl->other);
616 +       }
617 +}
618 +
619 +/* Send the ACL from the statx structure down the indicated file descriptor.
620 + * This also frees the ACL data. */
621 +void send_acl(statx *sxp, int f)
622 +{
623 +       SMB_ACL_TYPE_T type;
624 +       rsync_acl *racl, *new_racl;
625 +       item_list *racl_list;
626 +       int ndx;
627 +
628 +       if (S_ISLNK(sxp->st.st_mode))
629 +               return;
630 +
631 +       type = SMB_ACL_TYPE_ACCESS;
632 +       racl = sxp->acc_acl;
633 +       racl_list = &access_acl_list;
634 +       do {
635 +               if (!racl) {
636 +                       racl = new(rsync_acl);
637 +                       if (!racl)
638 +                               out_of_memory("send_acl");
639 +                       *racl = empty_rsync_acl;
640 +                       if (type == SMB_ACL_TYPE_ACCESS) {
641 +                               rsync_acl_fake_perms(racl, sxp->st.st_mode);
642 +                               sxp->acc_acl = racl;
643 +                       } else
644 +                               sxp->def_acl = racl;
645 +               }
646 +
647 +               /* Avoid sending values that can be inferred from other data. */
648 +               if (type == SMB_ACL_TYPE_ACCESS && protocol_version >= 30)
649 +                       rsync_acl_strip_perms(racl);
650 +               if ((ndx = find_matching_rsync_acl(type, racl_list, racl)) != -1) {
651 +                       write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
652 +                       write_int(f, ndx);
653 +               } else {
654 +                       new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
655 +                       write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
656 +                       send_rsync_acl(f, racl);
657 +                       *new_racl = *racl;
658 +                       *racl = empty_rsync_acl;
659 +               }
660 +               racl = sxp->def_acl;
661 +               racl_list = &default_acl_list;
662 +       } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
663 +
664 +       free_acl(sxp);
665 +}
666 +
667 +/* === Receive functions === */
668 +
669 +static void receive_rsync_acl(rsync_acl *racl, int f, SMB_ACL_TYPE_T type)
670 +{
671 +       static item_list temp_ida_list = EMPTY_ITEM_LIST;
672 +       SMB_ACL_TAG_T tag_type = 0, prior_list_type = 0;
673 +       uchar computed_mask_bits = 0;
674 +       id_access *ida;
675 +       size_t count;
676 +
677 +       if (!(count = read_int(f)))
678 +               return;
679 +
680 +       while (count--) {
681 +               char tag = read_byte(f);
682 +               uchar access = read_byte(f);
683 +               if (access & ~ (4 | 2 | 1)) {
684 +                       rprintf(FERROR, "receive_rsync_acl: bogus permset %o\n",
685 +                               access);
686 +                       exit_cleanup(RERR_STREAMIO);
687 +               }
688 +               switch (tag) {
689 +               case 'u':
690 +                       if (racl->user_obj != NO_ENTRY) {
691 +                               rprintf(FERROR, "receive_rsync_acl: error: duplicate USER_OBJ entry\n");
692 +                               exit_cleanup(RERR_STREAMIO);
693 +                       }
694 +                       racl->user_obj = access;
695 +                       continue;
696 +               case 'U':
697 +                       tag_type = SMB_ACL_USER;
698 +                       break;
699 +               case 'g':
700 +                       if (racl->group_obj != NO_ENTRY) {
701 +                               rprintf(FERROR, "receive_rsync_acl: error: duplicate GROUP_OBJ entry\n");
702 +                               exit_cleanup(RERR_STREAMIO);
703 +                       }
704 +                       racl->group_obj = access;
705 +                       continue;
706 +               case 'G':
707 +                       tag_type = SMB_ACL_GROUP;
708 +                       break;
709 +               case 'm':
710 +                       if (racl->mask != NO_ENTRY) {
711 +                               rprintf(FERROR, "receive_rsync_acl: error: duplicate MASK entry\n");
712 +                               exit_cleanup(RERR_STREAMIO);
713 +                       }
714 +                       racl->mask = access;
715 +                       continue;
716 +               case 'o':
717 +                       if (racl->other != NO_ENTRY) {
718 +                               rprintf(FERROR, "receive_rsync_acl: error: duplicate OTHER entry\n");
719 +                               exit_cleanup(RERR_STREAMIO);
720 +                       }
721 +                       racl->other = access;
722 +                       continue;
723 +               default:
724 +                       rprintf(FERROR, "receive_rsync_acl: unknown tag %c\n",
725 +                               tag);
726 +                       exit_cleanup(RERR_STREAMIO);
727 +               }
728 +               if (tag_type != prior_list_type) {
729 +                       if (prior_list_type)
730 +                               save_idas(&temp_ida_list, racl, prior_list_type);
731 +                       prior_list_type = tag_type;
732 +               }
733 +               ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
734 +               ida->access = access;
735 +               ida->id = read_int(f);
736 +               computed_mask_bits |= access;
737 +       }
738 +       if (prior_list_type)
739 +               save_idas(&temp_ida_list, racl, prior_list_type);
740 +
741 +       if (type == SMB_ACL_TYPE_DEFAULT) {
742 +               /* Ensure that these are never unset. */
743 +               if (racl->user_obj == NO_ENTRY)
744 +                       racl->user_obj = 7;
745 +               if (racl->group_obj == NO_ENTRY)
746 +                       racl->group_obj = 0;
747 +               if (racl->other == NO_ENTRY)
748 +                       racl->other = 0;
749 +       }
750 +
751 +       if (!racl->users.count && !racl->groups.count) {
752 +               /* If we received a superfluous mask, throw it away. */
753 +               if (racl->mask != NO_ENTRY) {
754 +                       /* Mask off the group perms with it first. */
755 +                       racl->group_obj &= racl->mask | NO_ENTRY;
756 +                       racl->mask = NO_ENTRY;
757 +               }
758 +       } else if (racl->mask == NO_ENTRY) /* Must be non-empty with lists. */
759 +               racl->mask = computed_mask_bits | (racl->group_obj & 7);
760 +}
761 +
762 +/* Receive the ACL info the sender has included for this file-list entry. */
763 +void receive_acl(struct file_struct *file, int f)
764 +{
765 +       SMB_ACL_TYPE_T type;
766 +       item_list *racl_list;
767 +
768 +       if (S_ISLNK(file->mode))
769 +               return;
770 +
771 +       type = SMB_ACL_TYPE_ACCESS;
772 +       racl_list = &access_acl_list;
773 +       do {
774 +               char tag = read_byte(f);
775 +               int ndx;
776 +
777 +               if (tag == 'A' || tag == 'a') {
778 +                       if (type != SMB_ACL_TYPE_ACCESS) {
779 +                               rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
780 +                                       f_name(file, NULL));
781 +                               exit_cleanup(RERR_STREAMIO);
782 +                       }
783 +               } else if (tag == 'D' || tag == 'd') {
784 +                       if (type == SMB_ACL_TYPE_ACCESS) {
785 +                               rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
786 +                                       f_name(file, NULL));
787 +                               exit_cleanup(RERR_STREAMIO);
788 +                       }
789 +               } else {
790 +                       rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
791 +                               f_name(file, NULL), tag);
792 +                       exit_cleanup(RERR_STREAMIO);
793 +               }
794 +               if (tag == 'A' || tag == 'D') {
795 +                       acl_duo *duo_item;
796 +                       ndx = racl_list->count;
797 +                       duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
798 +                       duo_item->racl = empty_rsync_acl;
799 +                       receive_rsync_acl(&duo_item->racl, f, type);
800 +                       duo_item->sacl = NULL;
801 +               } else {
802 +                       ndx = read_int(f);
803 +                       if (ndx < 0 || (size_t)ndx >= racl_list->count) {
804 +                               rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
805 +                                       f_name(file, NULL), str_acl_type(type), ndx);
806 +                               exit_cleanup(RERR_STREAMIO);
807 +                       }
808 +               }
809 +               if (type == SMB_ACL_TYPE_ACCESS)
810 +                       F_ACL(file) = ndx;
811 +               else
812 +                       F_DEF_ACL(file) = ndx;
813 +               racl_list = &default_acl_list;
814 +       } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
815 +}
816 +
817 +/* Turn the ACL data in statx into cached ACL data, setting the index
818 + * values in the file struct. */
819 +void cache_acl(struct file_struct *file, statx *sxp)
820 +{
821 +       SMB_ACL_TYPE_T type;
822 +       rsync_acl *racl;
823 +       item_list *racl_list;
824 +       int ndx;
825 +
826 +       if (S_ISLNK(file->mode))
827 +               return;
828 +
829 +       type = SMB_ACL_TYPE_ACCESS;
830 +       racl = sxp->acc_acl;
831 +       racl_list = &access_acl_list;
832 +       do {
833 +               if (!racl)
834 +                       ndx = -1;
835 +               else if ((ndx = find_matching_rsync_acl(type, racl_list, racl)) == -1) {
836 +                       acl_duo *new_duo;
837 +                       ndx = racl_list->count;
838 +                       new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
839 +                       new_duo->racl = *racl;
840 +                       new_duo->sacl = NULL;
841 +                       *racl = empty_rsync_acl;
842 +               }
843 +               if (type == SMB_ACL_TYPE_ACCESS)
844 +                       F_ACL(file) = ndx;
845 +               else
846 +                       F_DEF_ACL(file) = ndx;
847 +               racl = sxp->def_acl;
848 +               racl_list = &default_acl_list;
849 +       } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
850 +
851 +       free_acl(sxp);
852 +}
853 +
854 +static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
855 +{
856 +       SMB_ACL_ENTRY_T entry;
857 +       const char *errfun;
858 +       int rc;
859 +
860 +       if (S_ISDIR(mode)) {
861 +               /* If the sticky bit is going on, it's not safe to allow all
862 +                * the new ACL to go into effect before it gets set. */
863 +#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
864 +               if (mode & S_ISVTX)
865 +                       mode &= ~0077;
866 +#else
867 +               if (mode & S_ISVTX && !(old_mode & S_ISVTX))
868 +                       mode &= ~0077;
869 +       } else {
870 +               /* If setuid or setgid is going off, it's not safe to allow all
871 +                * the new ACL to go into effect before they get cleared. */
872 +               if ((old_mode & S_ISUID && !(mode & S_ISUID))
873 +                || (old_mode & S_ISGID && !(mode & S_ISGID)))
874 +                       mode &= ~0077;
875 +#endif
876 +       }
877 +
878 +       errfun = "sys_acl_get_entry";
879 +       for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
880 +            rc == 1;
881 +            rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
882 +               SMB_ACL_TAG_T tag_type;
883 +               if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
884 +                       errfun = "sys_acl_get_tag_type";
885 +                       break;
886 +               }
887 +               switch (tag_type) {
888 +               case SMB_ACL_USER_OBJ:
889 +                       COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
890 +                       break;
891 +               case SMB_ACL_GROUP_OBJ:
892 +                       /* group is only empty when identical to group perms. */
893 +                       if (racl->group_obj != NO_ENTRY)
894 +                               break;
895 +                       COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
896 +                       break;
897 +               case SMB_ACL_MASK:
898 +#ifndef ACLS_NEED_MASK
899 +                       /* mask is only empty when we don't need it. */
900 +                       if (racl->mask == NO_ENTRY)
901 +                               break;
902 +#endif
903 +                       COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
904 +                       break;
905 +               case SMB_ACL_OTHER:
906 +                       COE2( store_access_in_entry,(mode & 7, entry) );
907 +                       break;
908 +               }
909 +       }
910 +       if (rc) {
911 +         error_exit:
912 +               if (errfun) {
913 +                       rsyserr(FERROR, errno, "change_sacl_perms: %s()",
914 +                               errfun);
915 +               }
916 +               return (mode_t)~0;
917 +       }
918 +
919 +#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
920 +       /* Ensure that chmod() will be called to restore any lost setid bits. */
921 +       if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
922 +        && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
923 +               old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
924 +#endif
925 +
926 +       /* Return the mode of the file on disk, as we will set them. */
927 +       return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
928 +}
929 +
930 +/* Set ACL on indicated filename.
931 + *
932 + * This sets extended access ACL entries and default ACL.  If convenient,
933 + * it sets permission bits along with the access ACL and signals having
934 + * done so by modifying sxp->st.st_mode.
935 + *
936 + * Returns 1 for unchanged, 0 for changed, -1 for failed.  Call this
937 + * with fname set to NULL to just check if the ACL is unchanged. */
938 +int set_acl(const char *fname, const struct file_struct *file, statx *sxp)
939 +{
940 +       int unchanged = 1;
941 +       SMB_ACL_TYPE_T type;
942 +
943 +       if (!dry_run && (read_only || list_only)) {
944 +               errno = EROFS;
945 +               return -1;
946 +       }
947 +
948 +       if (S_ISLNK(file->mode))
949 +               return 1;
950 +
951 +       type = SMB_ACL_TYPE_ACCESS;
952 +       do {
953 +               acl_duo *duo_item;
954 +               int32 ndx;
955 +               BOOL eq;
956 +
957 +               if (type == SMB_ACL_TYPE_ACCESS) {
958 +                       ndx = F_ACL(file);
959 +                       if (ndx < 0 || (size_t)ndx >= access_acl_list.count)
960 +                               continue;
961 +                       duo_item = access_acl_list.items;
962 +                       duo_item += ndx;
963 +                       eq = sxp->acc_acl
964 +                           && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
965 +               } else {
966 +                       ndx = F_DEF_ACL(file);
967 +                       if (ndx < 0 || (size_t)ndx >= default_acl_list.count)
968 +                               continue;
969 +                       duo_item = default_acl_list.items;
970 +                       duo_item += ndx;
971 +                       eq = sxp->def_acl
972 +                           && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
973 +               }
974 +               if (eq)
975 +                       continue;
976 +               if (!dry_run && fname) {
977 +                       if (type == SMB_ACL_TYPE_DEFAULT
978 +                        && duo_item->racl.user_obj == NO_ENTRY) {
979 +                               if (sys_acl_delete_def_file(fname) < 0) {
980 +                                       rsyserr(FERROR, errno, "set_acl: sys_acl_delete_def_file(%s)",
981 +                                               fname);
982 +                                       unchanged = -1;
983 +                                       continue;
984 +                               }
985 +                       } else {
986 +                               mode_t cur_mode = sxp->st.st_mode;
987 +                               if (!duo_item->sacl
988 +                                && !pack_smb_acl(&duo_item->sacl, &duo_item->racl)) {
989 +                                       unchanged = -1;
990 +                                       continue;
991 +                               }
992 +                               if (type == SMB_ACL_TYPE_ACCESS) {
993 +                                       cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
994 +                                                                    cur_mode, file->mode);
995 +                                       if (cur_mode == (mode_t)~0)
996 +                                               continue;
997 +                               }
998 +                               if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
999 +                                       rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
1000 +                                               fname, str_acl_type(type));
1001 +                                       unchanged = -1;
1002 +                                       continue;
1003 +                               }
1004 +                               if (type == SMB_ACL_TYPE_ACCESS)
1005 +                                       sxp->st.st_mode = cur_mode;
1006 +                       }
1007 +               }
1008 +               if (unchanged == 1)
1009 +                       unchanged = 0;
1010 +       } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
1011 +
1012 +       return unchanged;
1013 +}
1014 +
1015 +/* === Enumeration functions for uid mapping === */
1016 +
1017 +/* Context -- one and only one.  Should be cycled through once on uid
1018 + * mapping and once on gid mapping. */
1019 +static item_list *_enum_racl_lists[] = {
1020 +       &access_acl_list, &default_acl_list, NULL
1021 +};
1022 +
1023 +static item_list **enum_racl_list = &_enum_racl_lists[0];
1024 +static int enum_ida_index = 0;
1025 +static size_t enum_racl_index = 0;
1026 +
1027 +/* This returns the next tag_type id from the given ACL for the next entry,
1028 + * or it returns 0 if there are no more tag_type ids in the acl. */
1029 +static id_t *next_ace_id(SMB_ACL_TAG_T tag_type, const rsync_acl *racl)
1030 +{
1031 +       const ida_entries *idal = tag_type == SMB_ACL_USER ? &racl->users : &racl->groups;
1032 +       if (enum_ida_index < idal->count) {
1033 +               id_access *ida = &idal->idas[enum_ida_index++];
1034 +               return &ida->id;
1035 +       }
1036 +       enum_ida_index = 0;
1037 +       return NULL;
1038 +}
1039 +
1040 +static id_t *next_acl_id(SMB_ACL_TAG_T tag_type, const item_list *racl_list)
1041 +{
1042 +       for (; enum_racl_index < racl_list->count; enum_racl_index++) {
1043 +               id_t *id;
1044 +               acl_duo *duo_item = racl_list->items;
1045 +               duo_item += enum_racl_index;
1046 +               if ((id = next_ace_id(tag_type, &duo_item->racl)) != NULL)
1047 +                       return id;
1048 +       }
1049 +       enum_racl_index = 0;
1050 +       return NULL;
1051 +}
1052 +
1053 +static id_t *next_acl_list_id(SMB_ACL_TAG_T tag_type)
1054 +{
1055 +       for (; *enum_racl_list; enum_racl_list++) {
1056 +               id_t *id = next_acl_id(tag_type, *enum_racl_list);
1057 +               if (id)
1058 +                       return id;
1059 +       }
1060 +       enum_racl_list = &_enum_racl_lists[0];
1061 +       return NULL;
1062 +}
1063 +
1064 +id_t *next_acl_uid()
1065 +{
1066 +       return next_acl_list_id(SMB_ACL_USER);
1067 +}
1068 +
1069 +id_t *next_acl_gid()
1070 +{
1071 +       return next_acl_list_id(SMB_ACL_GROUP);
1072 +}
1073 +
1074 +/* This is used by dest_mode(). */
1075 +int default_perms_for_dir(const char *dir)
1076 +{
1077 +       rsync_acl racl;
1078 +       SMB_ACL_T sacl;
1079 +       BOOL ok;
1080 +       int perms;
1081 +
1082 +       if (dir == NULL)
1083 +               dir = ".";
1084 +       perms = ACCESSPERMS & ~orig_umask;
1085 +       /* Read the directory's default ACL.  If it has none, this will successfully return an empty ACL. */
1086 +       sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1087 +       if (sacl == NULL) {
1088 +               /* Couldn't get an ACL.  Darn. */
1089 +               switch (errno) {
1090 +               case ENOTSUP:
1091 +               case ENOSYS:
1092 +                       /* No ACLs are available. */
1093 +                       break;
1094 +               case ENOENT:
1095 +                       if (dry_run) {
1096 +                               /* We're doing a dry run, so the containing directory
1097 +                                * wasn't actually created.  Don't worry about it. */
1098 +                               break;
1099 +                       }
1100 +                       /* Otherwise fall through. */
1101 +               default:
1102 +                       rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1103 +                               dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1104 +               }
1105 +               return perms;
1106 +       }
1107 +
1108 +       /* Convert it. */
1109 +       racl = empty_rsync_acl;
1110 +       ok = unpack_smb_acl(&racl, sacl);
1111 +       sys_acl_free_acl(sacl);
1112 +       if (!ok) {
1113 +               rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1114 +               return perms;
1115 +       }
1116 +
1117 +       /* Apply the permission-bit entries of the default ACL, if any. */
1118 +       if (racl.user_obj != NO_ENTRY) {
1119 +               perms = rsync_acl_get_perms(&racl);
1120 +               if (verbose > 2)
1121 +                       rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1122 +       }
1123 +
1124 +       rsync_acl_free(&racl);
1125 +       return perms;
1126 +}
1127 +
1128 +#endif /* SUPPORT_ACLS */
1129 --- old/backup.c
1130 +++ new/backup.c
1131 @@ -22,6 +22,7 @@
1132  
1133  extern int verbose;
1134  extern int am_root;
1135 +extern int preserve_acls;
1136  extern int preserve_devices;
1137  extern int preserve_specials;
1138  extern int preserve_links;
1139 @@ -92,7 +93,8 @@ path
1140  ****************************************************************************/
1141  static int make_bak_dir(char *fullpath)
1142  {
1143 -       STRUCT_STAT st;
1144 +       statx sx;
1145 +       struct file_struct *file;
1146         char *rel = fullpath + backup_dir_len;
1147         char *end = rel + strlen(rel);
1148         char *p = end;
1149 @@ -124,15 +126,24 @@ static int make_bak_dir(char *fullpath)
1150                 if (p >= rel) {
1151                         /* Try to transfer the directory settings of the
1152                          * actual dir that the files are coming from. */
1153 -                       if (do_stat(rel, &st) < 0) {
1154 +                       if (do_stat(rel, &sx.st) < 0) {
1155                                 rsyserr(FERROR, errno,
1156                                         "make_bak_dir stat %s failed",
1157                                         full_fname(rel));
1158                         } else {
1159 -                               do_lchown(fullpath, st.st_uid, st.st_gid);
1160 -#ifdef HAVE_CHMOD
1161 -                               do_chmod(fullpath, st.st_mode);
1162 +#ifdef SUPPORT_ACLS
1163 +                               sx.acc_acl = sx.def_acl = NULL;
1164 +#endif
1165 +                               if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
1166 +                                       continue;
1167 +#ifdef SUPPORT_ACLS
1168 +                               if (preserve_acls) {
1169 +                                       get_acl(rel, &sx);
1170 +                                       cache_acl(file, &sx);
1171 +                               }
1172  #endif
1173 +                               set_file_attrs(fullpath, file, NULL, 0);
1174 +                               free(file);
1175                         }
1176                 }
1177                 *p = '/';
1178 @@ -170,15 +181,18 @@ static int robust_move(const char *src, 
1179   * We will move the file to be deleted into a parallel directory tree. */
1180  static int keep_backup(const char *fname)
1181  {
1182 -       STRUCT_STAT st;
1183 +       statx sx;
1184         struct file_struct *file;
1185         char *buf;
1186         int kept = 0;
1187         int ret_code;
1188  
1189         /* return if no file to keep */
1190 -       if (do_lstat(fname, &st) < 0)
1191 +       if (do_lstat(fname, &sx.st) < 0)
1192                 return 1;
1193 +#ifdef SUPPORT_ACLS
1194 +       sx.acc_acl = sx.def_acl = NULL;
1195 +#endif
1196  
1197         if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1198                 return 1; /* the file could have disappeared */
1199 @@ -188,6 +202,13 @@ static int keep_backup(const char *fname
1200                 return 0;
1201         }
1202  
1203 +#ifdef SUPPORT_ACLS
1204 +       if (preserve_acls) {
1205 +               get_acl(fname, &sx);
1206 +               cache_acl(file, &sx);
1207 +       }
1208 +#endif
1209 +
1210         /* Check to see if this is a device file, or link */
1211         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1212          || (preserve_specials && IS_SPECIAL(file->mode))) {
1213 @@ -259,7 +280,7 @@ static int keep_backup(const char *fname
1214                 if (robust_move(fname, buf) != 0) {
1215                         rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
1216                                 full_fname(fname), buf);
1217 -               } else if (st.st_nlink > 1) {
1218 +               } else if (sx.st.st_nlink > 1) {
1219                         /* If someone has hard-linked the file into the backup
1220                          * dir, rename() might return success but do nothing! */
1221                         robust_unlink(fname); /* Just in case... */
1222 --- old/compat.c
1223 +++ new/compat.c
1224 @@ -55,6 +55,8 @@ void setup_protocol(int f_out,int f_in)
1225                 preserve_uid = ++file_extra_cnt;
1226         if (preserve_gid)
1227                 preserve_gid = ++file_extra_cnt;
1228 +       if (preserve_acls && !am_sender)
1229 +               preserve_acls = ++file_extra_cnt;
1230  
1231         if (remote_protocol == 0) {
1232                 if (!read_batch)
1233 --- old/configure.in
1234 +++ new/configure.in
1235 @@ -542,6 +542,11 @@ if test x"$ac_cv_func_strcasecmp" = x"no
1236      AC_CHECK_LIB(resolv, strcasecmp)
1237  fi
1238  
1239 +AC_CHECK_FUNCS(aclsort)
1240 +if test x"$ac_cv_func_aclsort" = x"no"; then
1241 +    AC_CHECK_LIB(sec, aclsort)
1242 +fi
1243 +
1244  dnl At the moment we don't test for a broken memcmp(), because all we
1245  dnl need to do is test for equality, not comparison, and it seems that
1246  dnl every platform has a memcmp that can do at least that.
1247 @@ -806,6 +811,78 @@ AC_SUBST(OBJ_RESTORE)
1248  AC_SUBST(CC_SHOBJ_FLAG)
1249  AC_SUBST(BUILD_POPT)
1250  
1251 +AC_CHECK_HEADERS(sys/acl.h acl/libacl.h)
1252 +AC_CHECK_FUNCS(_acl __acl _facl __facl)
1253 +#################################################
1254 +# check for ACL support
1255 +
1256 +AC_MSG_CHECKING(whether to support ACLs)
1257 +AC_ARG_ENABLE(acl-support,
1258 +AC_HELP_STRING([--enable-acl-support], [Include ACL support (default=no)]),
1259 +[ case "$enableval" in
1260 +  yes)
1261 +
1262 +               case "$host_os" in
1263 +               *sysv5*)
1264 +                       AC_MSG_RESULT(Using UnixWare ACLs)
1265 +                       AC_DEFINE(HAVE_UNIXWARE_ACLS, 1, [true if you have UnixWare ACLs])
1266 +                       ;;
1267 +               *solaris*|*cygwin*)
1268 +                       AC_MSG_RESULT(Using solaris ACLs)
1269 +                       AC_DEFINE(HAVE_SOLARIS_ACLS, 1, [true if you have solaris ACLs])
1270 +                       ;;
1271 +               *hpux*)
1272 +                       AC_MSG_RESULT(Using HPUX ACLs)
1273 +                       AC_DEFINE(HAVE_HPUX_ACLS, 1, [true if you have HPUX ACLs])
1274 +                       ;;
1275 +               *irix*)
1276 +                       AC_MSG_RESULT(Using IRIX ACLs)
1277 +                       AC_DEFINE(HAVE_IRIX_ACLS, 1, [true if you have IRIX ACLs])
1278 +                       ;;
1279 +               *aix*)
1280 +                       AC_MSG_RESULT(Using AIX ACLs)
1281 +                       AC_DEFINE(HAVE_AIX_ACLS, 1, [true if you have AIX ACLs])
1282 +                       ;;
1283 +               *osf*)
1284 +                       AC_MSG_RESULT(Using Tru64 ACLs)
1285 +                       AC_DEFINE(HAVE_TRU64_ACLS, 1, [true if you have Tru64 ACLs])
1286 +                       LIBS="$LIBS -lpacl"
1287 +                       ;;
1288 +               *)
1289 +                   AC_MSG_RESULT(ACLs requested -- running tests)
1290 +                   AC_CHECK_LIB(acl,acl_get_file)
1291 +                       AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1292 +                       AC_TRY_LINK([#include <sys/types.h>
1293 +#include <sys/acl.h>],
1294 +[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1295 +samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
1296 +                       AC_MSG_CHECKING(ACL test results)
1297 +                       if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1298 +                           AC_MSG_RESULT(Using posix ACLs)
1299 +                           AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
1300 +                           AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
1301 +                               AC_TRY_LINK([#include <sys/types.h>
1302 +#include <sys/acl.h>],
1303 +[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1304 +samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
1305 +                           if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1306 +                               AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1307 +                           fi
1308 +                       else
1309 +                           AC_MSG_ERROR(Failed to find ACL support)
1310 +                       fi
1311 +                       ;;
1312 +               esac
1313 +               ;;
1314 +  *)
1315 +    AC_MSG_RESULT(no)
1316 +       AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1317 +    ;;
1318 +  esac ],
1319 +  AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1320 +  AC_MSG_RESULT(no)
1321 +)
1322 +
1323  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1324  AC_OUTPUT
1325  
1326 --- old/flist.c
1327 +++ new/flist.c
1328 @@ -41,6 +41,7 @@ extern int filesfrom_fd;
1329  extern int one_file_system;
1330  extern int copy_dirlinks;
1331  extern int keep_dirlinks;
1332 +extern int preserve_acls;
1333  extern int preserve_links;
1334  extern int preserve_hard_links;
1335  extern int preserve_devices;
1336 @@ -152,6 +153,8 @@ static void list_file_entry(struct file_
1337         permstring(permbuf, f->mode);
1338         len = F_LENGTH(f);
1339  
1340 +       /* TODO: indicate '+' if the entry has an ACL. */
1341 +
1342  #ifdef SUPPORT_LINKS
1343         if (preserve_links && S_ISLNK(f->mode)) {
1344                 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
1345 @@ -714,6 +717,12 @@ static struct file_struct *recv_file_ent
1346         }
1347  #endif
1348  
1349 +#ifdef SUPPORT_ACLS
1350 +       /* We need one or two index int32s when we're preserving ACLs. */
1351 +       if (preserve_acls)
1352 +               extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
1353 +#endif
1354 +
1355         if (always_checksum && S_ISREG(mode))
1356                 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
1357  
1358 @@ -851,6 +860,11 @@ static struct file_struct *recv_file_ent
1359                         read_buf(f, bp, checksum_len);
1360         }
1361  
1362 +#ifdef SUPPORT_ACLS
1363 +       if (preserve_acls)
1364 +               receive_acl(file, f);
1365 +#endif
1366 +
1367         if (S_ISREG(mode) || S_ISLNK(mode))
1368                 stats.total_size += file_length;
1369  
1370 @@ -1122,6 +1136,9 @@ static struct file_struct *send_file_nam
1371                                           int flags, int filter_flags)
1372  {
1373         struct file_struct *file;
1374 +#ifdef SUPPORT_ACLS
1375 +       statx sx;
1376 +#endif
1377  
1378         file = make_file(fname, flist, stp, flags, filter_flags);
1379         if (!file)
1380 @@ -1130,12 +1147,26 @@ static struct file_struct *send_file_nam
1381         if (chmod_modes && !S_ISLNK(file->mode))
1382                 file->mode = tweak_mode(file->mode, chmod_modes);
1383  
1384 +#ifdef SUPPORT_ACLS
1385 +       if (preserve_acls && f >= 0) {
1386 +               sx.st.st_mode = file->mode;
1387 +               sx.acc_acl = sx.def_acl = NULL;
1388 +               if (get_acl(fname, &sx) < 0)
1389 +                       return NULL;
1390 +       }
1391 +#endif
1392 +
1393         maybe_emit_filelist_progress(flist->count + flist_count_offset);
1394  
1395         flist_expand(flist);
1396         flist->files[flist->count++] = file;
1397 -       if (f >= 0)
1398 +       if (f >= 0) {
1399                 send_file_entry(f, file, flist->count - 1);
1400 +#ifdef SUPPORT_ACLS
1401 +               if (preserve_acls)
1402 +                       send_acl(&sx, f);
1403 +#endif
1404 +       }
1405         return file;
1406  }
1407  
1408 --- old/generator.c
1409 +++ new/generator.c
1410 @@ -35,6 +35,7 @@ extern int do_progress;
1411  extern int relative_paths;
1412  extern int implied_dirs;
1413  extern int keep_dirlinks;
1414 +extern int preserve_acls;
1415  extern int preserve_links;
1416  extern int preserve_devices;
1417  extern int preserve_specials;
1418 @@ -89,6 +90,7 @@ extern int force_delete;
1419  extern int one_file_system;
1420  extern struct stats stats;
1421  extern dev_t filesystem_dev;
1422 +extern mode_t orig_umask;
1423  extern char *backup_dir;
1424  extern char *backup_suffix;
1425  extern int backup_suffix_len;
1426 @@ -511,22 +513,27 @@ static void do_delete_pass(struct file_l
1427                 rprintf(FINFO, "                    \r");
1428  }
1429  
1430 -int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
1431 +int unchanged_attrs(struct file_struct *file, statx *sxp)
1432  {
1433 -       if (preserve_perms && !BITS_EQUAL(st->st_mode, file->mode, CHMOD_BITS))
1434 +       if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
1435                 return 0;
1436  
1437 -       if (am_root && preserve_uid && st->st_uid != F_UID(file))
1438 +       if (am_root && preserve_uid && sxp->st.st_uid != F_UID(file))
1439                 return 0;
1440  
1441 -       if (preserve_gid && F_GID(file) != GID_NONE && st->st_gid != F_GID(file))
1442 +       if (preserve_gid && F_GID(file) != GID_NONE && sxp->st.st_gid != F_GID(file))
1443                 return 0;
1444  
1445 +#ifdef SUPPORT_ACLS
1446 +       if (preserve_acls && set_acl(NULL, file, sxp) == 0)
1447 +               return 0;
1448 +#endif
1449 +
1450         return 1;
1451  }
1452  
1453  void itemize(struct file_struct *file, int ndx, int statret,
1454 -            STRUCT_STAT *st, int32 iflags, uchar fnamecmp_type,
1455 +            statx *sxp, int32 iflags, uchar fnamecmp_type,
1456              const char *xname)
1457  {
1458         if (statret >= 0) { /* A from-dest-dir statret can == 1! */
1459 @@ -534,20 +541,24 @@ void itemize(struct file_struct *file, i
1460                     : S_ISDIR(file->mode) ? !omit_dir_times
1461                     : !S_ISLNK(file->mode);
1462  
1463 -               if (S_ISREG(file->mode) && F_LENGTH(file) != st->st_size)
1464 +               if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
1465                         iflags |= ITEM_REPORT_SIZE;
1466                 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
1467                   && !(iflags & ITEM_MATCHED)
1468                   && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
1469 -                || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
1470 +                || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
1471                         iflags |= ITEM_REPORT_TIME;
1472 -               if (!BITS_EQUAL(st->st_mode, file->mode, CHMOD_BITS))
1473 +               if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
1474                         iflags |= ITEM_REPORT_PERMS;
1475 -               if (preserve_uid && am_root && F_UID(file) != st->st_uid)
1476 +               if (preserve_uid && am_root && F_UID(file) != sxp->st.st_uid)
1477                         iflags |= ITEM_REPORT_OWNER;
1478                 if (preserve_gid && F_GID(file) != GID_NONE
1479 -                   && st->st_gid != F_GID(file))
1480 +                   && sxp->st.st_gid != F_GID(file))
1481                         iflags |= ITEM_REPORT_GROUP;
1482 +#ifdef SUPPORT_ACLS
1483 +               if (preserve_acls && set_acl(NULL, file, sxp) == 0)
1484 +                       iflags |= ITEM_REPORT_ACL;
1485 +#endif
1486         } else
1487                 iflags |= ITEM_IS_NEW;
1488  
1489 @@ -783,7 +794,7 @@ static int find_fuzzy(struct file_struct
1490   * handling the file, -1 if no dest-linking occurred, or a non-negative
1491   * value if we found an alternate basis file. */
1492  static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
1493 -                        char *cmpbuf, STRUCT_STAT *stp, int itemizing,
1494 +                        char *cmpbuf, statx *sxp, int itemizing,
1495                          enum logcode code)
1496  {
1497         int best_match = -1;
1498 @@ -792,7 +803,7 @@ static int try_dests_reg(struct file_str
1499  
1500         do {
1501                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1502 -               if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
1503 +               if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
1504                         continue;
1505                 switch (match_level) {
1506                 case 0:
1507 @@ -800,16 +811,20 @@ static int try_dests_reg(struct file_str
1508                         match_level = 1;
1509                         /* FALL THROUGH */
1510                 case 1:
1511 -                       if (!unchanged_file(cmpbuf, file, stp))
1512 +                       if (!unchanged_file(cmpbuf, file, &sxp->st))
1513                                 continue;
1514                         best_match = j;
1515                         match_level = 2;
1516                         /* FALL THROUGH */
1517                 case 2:
1518 -                       if (!unchanged_attrs(file, stp))
1519 +#ifdef SUPPORT_ACLS
1520 +                       if (preserve_acls)
1521 +                               get_acl(cmpbuf, sxp);
1522 +#endif
1523 +                       if (!unchanged_attrs(file, sxp))
1524                                 continue;
1525                         if (always_checksum > 0 && preserve_times
1526 -                        && cmp_time(stp->st_mtime, file->modtime))
1527 +                        && cmp_time(sxp->st.st_mtime, file->modtime))
1528                                 continue;
1529                         best_match = j;
1530                         match_level = 3;
1531 @@ -824,7 +839,7 @@ static int try_dests_reg(struct file_str
1532         if (j != best_match) {
1533                 j = best_match;
1534                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1535 -               if (link_stat(cmpbuf, stp, 0) < 0)
1536 +               if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1537                         return -1;
1538         }
1539  
1540 @@ -834,16 +849,25 @@ static int try_dests_reg(struct file_str
1541                         if (!hard_link_one(file, fname, cmpbuf, 1))
1542                                 goto try_a_copy;
1543                         if (preserve_hard_links && F_IS_HLINKED(file))
1544 -                               finish_hard_link(file, fname, stp, itemizing, code, j);
1545 +                               finish_hard_link(file, fname, &sxp->st, itemizing, code, j);
1546                         if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
1547 -                               itemize(file, ndx, 1, stp,
1548 +#ifdef SUPPORT_ACLS
1549 +                               if (preserve_acls && !ACL_READY(*sxp))
1550 +                                       get_acl(fname, sxp);
1551 +#endif
1552 +                               itemize(file, ndx, 1, sxp,
1553                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
1554                                         0, "");
1555                         }
1556                 } else
1557  #endif
1558 -               if (itemizing)
1559 -                       itemize(file, ndx, 0, stp, 0, 0, NULL);
1560 +               if (itemizing) {
1561 +#ifdef SUPPORT_ACLS
1562 +                       if (preserve_acls && !ACL_READY(*sxp))
1563 +                               get_acl(fname, sxp);
1564 +#endif
1565 +                       itemize(file, ndx, 0, sxp, 0, 0, NULL);
1566 +               }
1567                 if (verbose > 1 && maybe_ATTRS_REPORT)
1568                         rprintf(FCLIENT, "%s is uptodate\n", fname);
1569                 return -2;
1570 @@ -860,8 +884,13 @@ static int try_dests_reg(struct file_str
1571                         }
1572                         return -1;
1573                 }
1574 -               if (itemizing)
1575 -                       itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
1576 +               if (itemizing) {
1577 +#ifdef SUPPORT_ACLS
1578 +                       if (preserve_acls && !ACL_READY(*sxp))
1579 +                               get_acl(fname, sxp);
1580 +#endif
1581 +                       itemize(file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
1582 +               }
1583                 set_file_attrs(fname, file, NULL, 0);
1584                 if (maybe_ATTRS_REPORT
1585                  && ((!itemizing && verbose && match_level == 2)
1586 @@ -872,7 +901,7 @@ static int try_dests_reg(struct file_str
1587                 }
1588  #ifdef SUPPORT_HARD_LINKS
1589                 if (preserve_hard_links && F_IS_HLINKED(file))
1590 -                       finish_hard_link(file, fname, stp, itemizing, code, -1);
1591 +                       finish_hard_link(file, fname, &sxp->st, itemizing, code, -1);
1592  #endif
1593                 return -2;
1594         }
1595 @@ -884,7 +913,7 @@ static int try_dests_reg(struct file_str
1596   * handling the file, or -1 if no dest-linking occurred, or a non-negative
1597   * value if we found an alternate basis file. */
1598  static int try_dests_non(struct file_struct *file, char *fname, int ndx,
1599 -                        char *cmpbuf, STRUCT_STAT *stp, int itemizing,
1600 +                        char *cmpbuf, statx *sxp, int itemizing,
1601                          enum logcode code)
1602  {
1603         char lnk[MAXPATHLEN];
1604 @@ -917,24 +946,24 @@ static int try_dests_non(struct file_str
1605  
1606         do {
1607                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1608 -               if (link_stat(cmpbuf, stp, 0) < 0)
1609 +               if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1610                         continue;
1611                 switch (type) {
1612                 case TYPE_DIR:
1613 -                       if (!S_ISDIR(stp->st_mode))
1614 +                       if (!S_ISDIR(sxp->st.st_mode))
1615                                 continue;
1616                         break;
1617                 case TYPE_SPECIAL:
1618 -                       if (!IS_SPECIAL(stp->st_mode))
1619 +                       if (!IS_SPECIAL(sxp->st.st_mode))
1620                                 continue;
1621                         break;
1622                 case TYPE_DEVICE:
1623 -                       if (!IS_DEVICE(stp->st_mode))
1624 +                       if (!IS_DEVICE(sxp->st.st_mode))
1625                                 continue;
1626                         break;
1627  #ifdef SUPPORT_LINKS
1628                 case TYPE_SYMLINK:
1629 -                       if (!S_ISLNK(stp->st_mode))
1630 +                       if (!S_ISLNK(sxp->st.st_mode))
1631                                 continue;
1632                         break;
1633  #endif
1634 @@ -949,7 +978,7 @@ static int try_dests_non(struct file_str
1635                 case TYPE_SPECIAL:
1636                 case TYPE_DEVICE:
1637                         devp = F_RDEV_P(file);
1638 -                       if (stp->st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1639 +                       if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1640                                 continue;
1641                         break;
1642  #ifdef SUPPORT_LINKS
1643 @@ -966,7 +995,11 @@ static int try_dests_non(struct file_str
1644                         match_level = 2;
1645                         best_match = j;
1646                 }
1647 -               if (unchanged_attrs(file, stp)) {
1648 +#ifdef SUPPORT_ACLS
1649 +               if (preserve_acls)
1650 +                       get_acl(cmpbuf, sxp);
1651 +#endif
1652 +               if (unchanged_attrs(file, sxp)) {
1653                         match_level = 3;
1654                         best_match = j;
1655                         break;
1656 @@ -979,7 +1012,7 @@ static int try_dests_non(struct file_str
1657         if (j != best_match) {
1658                 j = best_match;
1659                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1660 -               if (link_stat(cmpbuf, stp, 0) < 0)
1661 +               if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1662                         return -1;
1663         }
1664  
1665 @@ -1010,7 +1043,15 @@ static int try_dests_non(struct file_str
1666                             : ITEM_LOCAL_CHANGE
1667                              + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1668                         char *lp = match_level == 3 ? "" : NULL;
1669 -                       itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp);
1670 +#ifdef SUPPORT_ACLS
1671 +                       if (preserve_acls)
1672 +                               get_acl(fname, sxp);
1673 +#endif
1674 +                       itemize(file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1675 +#ifdef SUPPORT_ACLS
1676 +                       if (preserve_acls)
1677 +                               free_acl(sxp);
1678 +#endif
1679                 }
1680                 if (verbose > 1 && maybe_ATTRS_REPORT) {
1681                         rprintf(FCLIENT, "%s%s is uptodate\n",
1682 @@ -1023,6 +1064,7 @@ static int try_dests_non(struct file_str
1683  }
1684  
1685  static int phase = 0;
1686 +static int dflt_perms;
1687  
1688  /* Acts on the indicated item in cur_flist whose name is fname.  If a dir,
1689   * make sure it exists, and has the right permissions/timestamp info.  For
1690 @@ -1043,7 +1085,8 @@ static void recv_generator(char *fname, 
1691         static int need_fuzzy_dirlist = 0;
1692         struct file_struct *fuzzy_file = NULL;
1693         int fd = -1, f_copy = -1;
1694 -       STRUCT_STAT st, real_st, partial_st;
1695 +       statx sx, real_sx;
1696 +       STRUCT_STAT partial_st;
1697         struct file_struct *back_file = NULL;
1698         int statret, real_ret, stat_errno;
1699         char *fnamecmp, *partialptr, *backupptr = NULL;
1700 @@ -1088,6 +1131,9 @@ static void recv_generator(char *fname, 
1701                         return;
1702                 }
1703         }
1704 +#ifdef SUPPORT_ACLS
1705 +       sx.acc_acl = sx.def_acl = NULL;
1706 +#endif
1707         if (dry_run > 1) {
1708                 if (fuzzy_dirlist) {
1709                         flist_free(fuzzy_dirlist);
1710 @@ -1100,7 +1146,7 @@ static void recv_generator(char *fname, 
1711                 const char *dn = file->dirname ? file->dirname : ".";
1712                 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1713                         if (relative_paths && !implied_dirs
1714 -                        && do_stat(dn, &st) < 0
1715 +                        && do_stat(dn, &sx.st) < 0
1716                          && create_directory_path(fname) < 0) {
1717                                 rsyserr(FERROR, errno,
1718                                         "recv_generator: mkdir %s failed",
1719 @@ -1112,6 +1158,10 @@ static void recv_generator(char *fname, 
1720                         }
1721                         if (fuzzy_basis)
1722                                 need_fuzzy_dirlist = 1;
1723 +#ifdef SUPPORT_ACLS
1724 +                       if (!preserve_perms)
1725 +                               dflt_perms = default_perms_for_dir(dn);
1726 +#endif
1727                 }
1728                 parent_dirname = dn;
1729  
1730 @@ -1121,7 +1171,7 @@ static void recv_generator(char *fname, 
1731                         need_fuzzy_dirlist = 0;
1732                 }
1733  
1734 -               statret = link_stat(fname, &st,
1735 +               statret = link_stat(fname, &sx.st,
1736                                     keep_dirlinks && S_ISDIR(file->mode));
1737                 stat_errno = errno;
1738         }
1739 @@ -1149,8 +1199,8 @@ static void recv_generator(char *fname, 
1740                  * file of that name and it is *not* a directory, then
1741                  * we need to delete it.  If it doesn't exist, then
1742                  * (perhaps recursively) create it. */
1743 -               if (statret == 0 && !S_ISDIR(st.st_mode)) {
1744 -                       if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
1745 +               if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1746 +                       if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1747                                 return;
1748                         statret = -1;
1749                 }
1750 @@ -1159,18 +1209,18 @@ static void recv_generator(char *fname, 
1751                         dry_run++;
1752                 }
1753                 real_ret = statret;
1754 -               real_st = st;
1755 +               real_sx = sx;
1756                 if (new_root_dir) {
1757                         if (*fname == '.' && fname[1] == '\0')
1758                                 statret = -1;
1759                         new_root_dir = 0;
1760                 }
1761                 if (!preserve_perms) { /* See comment in non-dir code below. */
1762 -                       file->mode = dest_mode(file->mode, st.st_mode,
1763 -                                              statret == 0);
1764 +                       file->mode = dest_mode(file->mode, sx.st.st_mode,
1765 +                                              dflt_perms, statret == 0);
1766                 }
1767                 if (statret != 0 && basis_dir[0] != NULL) {
1768 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1769 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1770                                               itemizing, code);
1771                         if (j == -2) {
1772                                 itemizing = 0;
1773 @@ -1179,7 +1229,11 @@ static void recv_generator(char *fname, 
1774                                 statret = 1;
1775                 }
1776                 if (itemizing && f_out != -1) {
1777 -                       itemize(file, ndx, statret, &st,
1778 +#ifdef SUPPORT_ACLS
1779 +                       if (preserve_acls && statret == 0)
1780 +                               get_acl(fname, &sx);
1781 +#endif
1782 +                       itemize(file, ndx, statret, &sx,
1783                                 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1784                 }
1785                 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1786 @@ -1193,38 +1247,39 @@ static void recv_generator(char *fname, 
1787                                     "*** Skipping any contents from this failed directory ***\n");
1788                                 missing_below = F_DEPTH(file);
1789                                 file->flags |= FLAG_MISSING_DIR;
1790 -                               return;
1791 +                               goto cleanup;
1792                         }
1793                 }
1794 -               if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0)
1795 +               if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
1796                     && verbose && code != FNONE && f_out != -1)
1797                         rprintf(code, "%s/\n", fname);
1798                 if (real_ret != 0 && one_file_system)
1799 -                       real_st.st_dev = filesystem_dev;
1800 +                       real_sx.st.st_dev = filesystem_dev;
1801                 if (inc_recurse) {
1802                         if (one_file_system) {
1803                                 uint32 *devp = F_DIRDEV_P(file);
1804 -                               DEV_MAJOR(devp) = major(real_st.st_dev);
1805 -                               DEV_MINOR(devp) = minor(real_st.st_dev);
1806 +                               DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1807 +                               DEV_MINOR(devp) = minor(real_sx.st.st_dev);
1808                         }
1809                 }
1810                 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1811                     && (file->flags & FLAG_XFER_DIR))
1812 -                       delete_in_dir(cur_flist, fname, file, &real_st.st_dev);
1813 -               return;
1814 +                       delete_in_dir(cur_flist, fname, file, &real_sx.st.st_dev);
1815 +               goto cleanup;
1816         }
1817  
1818         /* If we're not preserving permissions, change the file-list's
1819          * mode based on the local permissions and some heuristics. */
1820         if (!preserve_perms) {
1821 -               int exists = statret == 0 && !S_ISDIR(st.st_mode);
1822 -               file->mode = dest_mode(file->mode, st.st_mode, exists);
1823 +               int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1824 +               file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1825 +                                      exists);
1826         }
1827  
1828  #ifdef SUPPORT_HARD_LINKS
1829         if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1830 -        && hard_link_check(file, ndx, fname, statret, &st, itemizing, code))
1831 -               return;
1832 +        && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1833 +               goto cleanup;
1834  #endif
1835  
1836         if (preserve_links && S_ISLNK(file->mode)) {
1837 @@ -1244,28 +1299,28 @@ static void recv_generator(char *fname, 
1838                         char lnk[MAXPATHLEN];
1839                         int len;
1840  
1841 -                       if (!S_ISLNK(st.st_mode))
1842 +                       if (!S_ISLNK(sx.st.st_mode))
1843                                 statret = -1;
1844                         else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1845                               && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1846                                 /* The link is pointing to the right place. */
1847                                 if (itemizing)
1848 -                                       itemize(file, ndx, 0, &st, 0, 0, NULL);
1849 -                               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1850 +                                       itemize(file, ndx, 0, &sx, 0, 0, NULL);
1851 +                               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1852  #ifdef SUPPORT_HARD_LINKS
1853                                 if (preserve_hard_links && F_IS_HLINKED(file))
1854 -                                       finish_hard_link(file, fname, &st, itemizing, code, -1);
1855 +                                       finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1856  #endif
1857                                 if (remove_source_files == 1)
1858                                         goto return_with_success;
1859 -                               return;
1860 +                               goto cleanup;
1861                         }
1862                         /* Not the right symlink (or not a symlink), so
1863                          * delete it. */
1864 -                       if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
1865 -                               return;
1866 +                       if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1867 +                               goto cleanup;
1868                 } else if (basis_dir[0] != NULL) {
1869 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1870 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1871                                               itemizing, code);
1872                         if (j == -2) {
1873  #ifndef CAN_HARDLINK_SYMLINK
1874 @@ -1274,7 +1329,7 @@ static void recv_generator(char *fname, 
1875                                 } else
1876  #endif
1877                                 if (!copy_dest)
1878 -                                       return;
1879 +                                       goto cleanup;
1880                                 itemizing = 0;
1881                                 code = FNONE;
1882                         } else if (j >= 0)
1883 @@ -1282,7 +1337,7 @@ static void recv_generator(char *fname, 
1884                 }
1885  #ifdef SUPPORT_HARD_LINKS
1886                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1887 -                       return;
1888 +                       goto cleanup;
1889  #endif
1890                 if (do_symlink(sl, fname) != 0) {
1891                         rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1892 @@ -1290,7 +1345,7 @@ static void recv_generator(char *fname, 
1893                 } else {
1894                         set_file_attrs(fname, file, NULL, 0);
1895                         if (itemizing) {
1896 -                               itemize(file, ndx, statret, &st,
1897 +                               itemize(file, ndx, statret, &sx,
1898                                         ITEM_LOCAL_CHANGE, 0, NULL);
1899                         }
1900                         if (code != FNONE && verbose)
1901 @@ -1306,7 +1361,7 @@ static void recv_generator(char *fname, 
1902                                 goto return_with_success;
1903                 }
1904  #endif
1905 -               return;
1906 +               goto cleanup;
1907         }
1908  
1909         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1910 @@ -1316,33 +1371,38 @@ static void recv_generator(char *fname, 
1911                 if (statret == 0) {
1912                         char *t;
1913                         if (IS_DEVICE(file->mode)) {
1914 -                               if (!IS_DEVICE(st.st_mode))
1915 +                               if (!IS_DEVICE(sx.st.st_mode))
1916                                         statret = -1;
1917                                 t = "device file";
1918                         } else {
1919 -                               if (!IS_SPECIAL(st.st_mode))
1920 +                               if (!IS_SPECIAL(sx.st.st_mode))
1921                                         statret = -1;
1922                                 t = "special file";
1923                         }
1924                         if (statret == 0
1925 -                        && BITS_EQUAL(st.st_mode, file->mode, _S_IFMT)
1926 -                        && st.st_rdev == rdev) {
1927 +                        && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1928 +                        && sx.st.st_rdev == rdev) {
1929                                 /* The device or special file is identical. */
1930 -                               if (itemizing)
1931 -                                       itemize(file, ndx, 0, &st, 0, 0, NULL);
1932 -                               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1933 +                               if (itemizing) {
1934 +#ifdef SUPPORT_ACLS
1935 +                                       if (preserve_acls)
1936 +                                               get_acl(fname, &sx);
1937 +#endif
1938 +                                       itemize(file, ndx, 0, &sx, 0, 0, NULL);
1939 +                               }
1940 +                               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1941  #ifdef SUPPORT_HARD_LINKS
1942                                 if (preserve_hard_links && F_IS_HLINKED(file))
1943 -                                       finish_hard_link(file, fname, &st, itemizing, code, -1);
1944 +                                       finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1945  #endif
1946                                 if (remove_source_files == 1)
1947                                         goto return_with_success;
1948 -                               return;
1949 +                               goto cleanup;
1950                         }
1951 -                       if (delete_item(fname, st.st_mode, t, del_opts) != 0)
1952 -                               return;
1953 +                       if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1954 +                               goto cleanup;
1955                 } else if (basis_dir[0] != NULL) {
1956 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1957 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1958                                               itemizing, code);
1959                         if (j == -2) {
1960  #ifndef CAN_HARDLINK_SPECIAL
1961 @@ -1351,7 +1411,7 @@ static void recv_generator(char *fname, 
1962                                 } else
1963  #endif
1964                                 if (!copy_dest)
1965 -                                       return;
1966 +                                       goto cleanup;
1967                                 itemizing = 0;
1968                                 code = FNONE;
1969                         } else if (j >= 0)
1970 @@ -1359,7 +1419,7 @@ static void recv_generator(char *fname, 
1971                 }
1972  #ifdef SUPPORT_HARD_LINKS
1973                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1974 -                       return;
1975 +                       goto cleanup;
1976  #endif
1977                 if (verbose > 2) {
1978                         rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1979 @@ -1372,7 +1432,11 @@ static void recv_generator(char *fname, 
1980                 } else {
1981                         set_file_attrs(fname, file, NULL, 0);
1982                         if (itemizing) {
1983 -                               itemize(file, ndx, statret, &st,
1984 +#ifdef SUPPORT_ACLS
1985 +                               if (preserve_acls && statret == 0)
1986 +                                       get_acl(fname, &sx);
1987 +#endif
1988 +                               itemize(file, ndx, statret, &sx,
1989                                         ITEM_LOCAL_CHANGE, 0, NULL);
1990                         }
1991                         if (code != FNONE && verbose)
1992 @@ -1384,14 +1448,14 @@ static void recv_generator(char *fname, 
1993                         if (remove_source_files == 1)
1994                                 goto return_with_success;
1995                 }
1996 -               return;
1997 +               goto cleanup;
1998         }
1999  
2000         if (!S_ISREG(file->mode)) {
2001                 if (solo_file)
2002                         fname = f_name(file, NULL);
2003                 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
2004 -               return;
2005 +               goto cleanup;
2006         }
2007  
2008         if (max_size > 0 && F_LENGTH(file) > max_size) {
2009 @@ -1400,7 +1464,7 @@ static void recv_generator(char *fname, 
2010                                 fname = f_name(file, NULL);
2011                         rprintf(FINFO, "%s is over max-size\n", fname);
2012                 }
2013 -               return;
2014 +               goto cleanup;
2015         }
2016         if (min_size > 0 && F_LENGTH(file) < min_size) {
2017                 if (verbose > 1) {
2018 @@ -1408,39 +1472,39 @@ static void recv_generator(char *fname, 
2019                                 fname = f_name(file, NULL);
2020                         rprintf(FINFO, "%s is under min-size\n", fname);
2021                 }
2022 -               return;
2023 +               goto cleanup;
2024         }
2025  
2026         if (ignore_existing > 0 && statret == 0) {
2027                 if (verbose > 1)
2028                         rprintf(FINFO, "%s exists\n", fname);
2029 -               return;
2030 +               goto cleanup;
2031         }
2032  
2033         if (update_only > 0 && statret == 0
2034 -           && cmp_time(st.st_mtime, file->modtime) > 0) {
2035 +           && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
2036                 if (verbose > 1)
2037                         rprintf(FINFO, "%s is newer\n", fname);
2038 -               return;
2039 +               goto cleanup;
2040         }
2041  
2042         fnamecmp = fname;
2043         fnamecmp_type = FNAMECMP_FNAME;
2044  
2045 -       if (statret == 0 && !S_ISREG(st.st_mode)) {
2046 -               if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
2047 -                       return;
2048 +       if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
2049 +               if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
2050 +                       goto cleanup;
2051                 statret = -1;
2052                 stat_errno = ENOENT;
2053         }
2054  
2055         if (statret != 0 && basis_dir[0] != NULL) {
2056 -               int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
2057 +               int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
2058                                       itemizing, code);
2059                 if (j == -2) {
2060                         if (remove_source_files == 1)
2061                                 goto return_with_success;
2062 -                       return;
2063 +                       goto cleanup;
2064                 }
2065                 if (j >= 0) {
2066                         fnamecmp = fnamecmpbuf;
2067 @@ -1450,7 +1514,7 @@ static void recv_generator(char *fname, 
2068         }
2069  
2070         real_ret = statret;
2071 -       real_st = st;
2072 +       real_sx = sx;
2073  
2074         if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
2075             && link_stat(partialptr, &partial_st, 0) == 0
2076 @@ -1469,7 +1533,7 @@ static void recv_generator(char *fname, 
2077                                 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
2078                                         fname, fnamecmpbuf);
2079                         }
2080 -                       st.st_size = F_LENGTH(fuzzy_file);
2081 +                       sx.st.st_size = F_LENGTH(fuzzy_file);
2082                         statret = 0;
2083                         fnamecmp = fnamecmpbuf;
2084                         fnamecmp_type = FNAMECMP_FUZZY;
2085 @@ -1479,45 +1543,50 @@ static void recv_generator(char *fname, 
2086         if (statret != 0) {
2087  #ifdef SUPPORT_HARD_LINKS
2088                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
2089 -                       return;
2090 +                       goto cleanup;
2091  #endif
2092                 if (stat_errno == ENOENT)
2093                         goto notify_others;
2094                 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
2095                         full_fname(fname));
2096 -               return;
2097 +               goto cleanup;
2098         }
2099  
2100 -       if (append_mode > 0 && st.st_size > F_LENGTH(file))
2101 -               return;
2102 +       if (append_mode > 0 && sx.st.st_size > F_LENGTH(file))
2103 +               goto cleanup;
2104  
2105         if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
2106                 ;
2107         else if (fnamecmp_type == FNAMECMP_FUZZY)
2108                 ;
2109 -       else if (unchanged_file(fnamecmp, file, &st)) {
2110 +       else if (unchanged_file(fnamecmp, file, &sx.st)) {
2111                 if (partialptr) {
2112                         do_unlink(partialptr);
2113                         handle_partial_dir(partialptr, PDIR_DELETE);
2114                 }
2115 -               if (itemizing)
2116 -                       itemize(file, ndx, statret, &st, 0, 0, NULL);
2117 -               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
2118 +               if (itemizing) {
2119 +#ifdef SUPPORT_ACLS
2120 +                       if (preserve_acls && statret == 0)
2121 +                               get_acl(fnamecmp, &sx);
2122 +#endif
2123 +                       itemize(file, ndx, statret, &sx, 0, 0, NULL);
2124 +               }
2125 +               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
2126  #ifdef SUPPORT_HARD_LINKS
2127                 if (preserve_hard_links && F_IS_HLINKED(file))
2128 -                       finish_hard_link(file, fname, &st, itemizing, code, -1);
2129 +                       finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
2130  #endif
2131                 if (remove_source_files != 1)
2132 -                       return;
2133 +                       goto cleanup;
2134           return_with_success:
2135                 if (!dry_run)
2136                         send_msg_int(MSG_SUCCESS, ndx);
2137 -               return;
2138 +               goto cleanup;
2139         }
2140  
2141    prepare_to_open:
2142         if (partialptr) {
2143 -               st = partial_st;
2144 +               sx.st = partial_st;
2145                 fnamecmp = partialptr;
2146                 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
2147                 statret = 0;
2148 @@ -1542,16 +1611,20 @@ static void recv_generator(char *fname, 
2149                 /* pretend the file didn't exist */
2150  #ifdef SUPPORT_HARD_LINKS
2151                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
2152 -                       return;
2153 +                       goto cleanup;
2154  #endif
2155                 statret = real_ret = -1;
2156 +#ifdef SUPPORT_ACLS
2157 +               if (preserve_acls && ACL_READY(sx))
2158 +                       free_acl(&sx);
2159 +#endif
2160                 goto notify_others;
2161         }
2162  
2163         if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
2164                 if (!(backupptr = get_backup_name(fname))) {
2165                         close(fd);
2166 -                       return;
2167 +                       goto cleanup;
2168                 }
2169                 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
2170                         close(fd);
2171 @@ -1562,7 +1635,7 @@ static void recv_generator(char *fname, 
2172                                 full_fname(backupptr));
2173                         unmake_file(back_file);
2174                         close(fd);
2175 -                       return;
2176 +                       goto cleanup;
2177                 }
2178                 if ((f_copy = do_open(backupptr,
2179                     O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
2180 @@ -1570,14 +1643,14 @@ static void recv_generator(char *fname, 
2181                                 full_fname(backupptr));
2182                         unmake_file(back_file);
2183                         close(fd);
2184 -                       return;
2185 +                       goto cleanup;
2186                 }
2187                 fnamecmp_type = FNAMECMP_BACKUP;
2188         }
2189  
2190         if (verbose > 3) {
2191                 rprintf(FINFO, "gen mapped %s of size %.0f\n",
2192 -                       fnamecmp, (double)st.st_size);
2193 +                       fnamecmp, (double)sx.st.st_size);
2194         }
2195  
2196         if (verbose > 2)
2197 @@ -1601,26 +1674,34 @@ static void recv_generator(char *fname, 
2198                         iflags |= ITEM_BASIS_TYPE_FOLLOWS;
2199                 if (fnamecmp_type == FNAMECMP_FUZZY)
2200                         iflags |= ITEM_XNAME_FOLLOWS;
2201 -               itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
2202 +#ifdef SUPPORT_ACLS
2203 +               if (preserve_acls && real_ret == 0)
2204 +                       get_acl(fnamecmp, &real_sx);
2205 +#endif
2206 +               itemize(file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
2207                         fuzzy_file ? fuzzy_file->basename : NULL);
2208 +#ifdef SUPPORT_ACLS
2209 +               if (preserve_acls)
2210 +                       free_acl(&real_sx);
2211 +#endif
2212         }
2213  
2214         if (!do_xfers) {
2215  #ifdef SUPPORT_HARD_LINKS
2216                 if (preserve_hard_links && F_IS_HLINKED(file))
2217 -                       finish_hard_link(file, fname, &st, itemizing, code, -1);
2218 +                       finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
2219  #endif
2220 -               return;
2221 +               goto cleanup;
2222         }
2223         if (read_batch)
2224 -               return;
2225 +               goto cleanup;
2226  
2227         if (statret != 0 || whole_file) {
2228                 write_sum_head(f_out, NULL);
2229 -               return;
2230 +               goto cleanup;
2231         }
2232  
2233 -       generate_and_send_sums(fd, st.st_size, f_out, f_copy);
2234 +       generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
2235  
2236         if (f_copy >= 0) {
2237                 close(f_copy);
2238 @@ -1633,6 +1714,13 @@ static void recv_generator(char *fname, 
2239         }
2240  
2241         close(fd);
2242 +
2243 +  cleanup:
2244 +#ifdef SUPPORT_ACLS
2245 +       if (preserve_acls)
2246 +               free_acl(&sx);
2247 +#endif
2248 +       return;
2249  }
2250  
2251  static void touch_up_dirs(struct file_list *flist, int ndx)
2252 @@ -1803,6 +1891,8 @@ void generate_files(int f_out, const cha
2253          * notice that and let us know via the redo pipe (or its closing). */
2254         ignore_timeout = 1;
2255  
2256 +       dflt_perms = (ACCESSPERMS & ~orig_umask);
2257 +
2258         do {
2259                 if (inc_recurse && delete_during && cur_flist->ndx_start) {
2260                         struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
2261 --- old/hlink.c
2262 +++ new/hlink.c
2263 @@ -26,6 +26,7 @@ extern int verbose;
2264  extern int dry_run;
2265  extern int do_xfers;
2266  extern int link_dest;
2267 +extern int preserve_acls;
2268  extern int make_backups;
2269  extern int protocol_version;
2270  extern int remove_source_files;
2271 @@ -267,15 +268,19 @@ void match_hard_links(void)
2272  }
2273  
2274  static int maybe_hard_link(struct file_struct *file, int ndx,
2275 -                          const char *fname, int statret, STRUCT_STAT *stp,
2276 +                          const char *fname, int statret, statx *sxp,
2277                            const char *oldname, STRUCT_STAT *old_stp,
2278                            const char *realname, int itemizing, enum logcode code)
2279  {
2280         if (statret == 0) {
2281 -               if (stp->st_dev == old_stp->st_dev
2282 -                && stp->st_ino == old_stp->st_ino) {
2283 +               if (sxp->st.st_dev == old_stp->st_dev
2284 +                && sxp->st.st_ino == old_stp->st_ino) {
2285                         if (itemizing) {
2286 -                               itemize(file, ndx, statret, stp,
2287 +#ifdef SUPPORT_ACLS
2288 +                               if (preserve_acls && !ACL_READY(*sxp))
2289 +                                       get_acl(fname, sxp);
2290 +#endif
2291 +                               itemize(file, ndx, statret, sxp,
2292                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
2293                                         0, "");
2294                         }
2295 @@ -296,7 +301,11 @@ static int maybe_hard_link(struct file_s
2296  
2297         if (hard_link_one(file, fname, oldname, 0)) {
2298                 if (itemizing) {
2299 -                       itemize(file, ndx, statret, stp,
2300 +#ifdef SUPPORT_ACLS
2301 +                       if (preserve_acls && statret == 0 && !ACL_READY(*sxp))
2302 +                               get_acl(fname, sxp);
2303 +#endif
2304 +                       itemize(file, ndx, statret, sxp,
2305                                 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
2306                                 realname);
2307                 }
2308 @@ -310,7 +319,7 @@ static int maybe_hard_link(struct file_s
2309  /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not.  Returns:
2310   * 0 = process the file, 1 = skip the file, -1 = error occurred. */
2311  int hard_link_check(struct file_struct *file, int ndx, const char *fname,
2312 -                   int statret, STRUCT_STAT *stp, int itemizing,
2313 +                   int statret, statx *sxp, int itemizing,
2314                     enum logcode code)
2315  {
2316         STRUCT_STAT prev_st;
2317 @@ -361,18 +370,20 @@ int hard_link_check(struct file_struct *
2318         if (statret < 0 && basis_dir[0] != NULL) {
2319                 /* If we match an alt-dest item, we don't output this as a change. */
2320                 char cmpbuf[MAXPATHLEN];
2321 -               STRUCT_STAT alt_st;
2322 +               statx alt_sx;
2323                 int j = 0;
2324 +#ifdef SUPPORT_ACLS
2325 +               alt_sx.acc_acl = alt_sx.def_acl = NULL;
2326 +#endif
2327                 do {
2328                         pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
2329 -                       if (link_stat(cmpbuf, &alt_st, 0) < 0)
2330 +                       if (link_stat(cmpbuf, &alt_sx.st, 0) < 0)
2331                                 continue;
2332                         if (link_dest) {
2333 -                               if (prev_st.st_dev != alt_st.st_dev
2334 -                                || prev_st.st_ino != alt_st.st_ino)
2335 +                               if (prev_st.st_dev != alt_sx.st.st_dev
2336 +                                || prev_st.st_ino != alt_sx.st.st_ino)
2337                                         continue;
2338                                 statret = 1;
2339 -                               *stp = alt_st;
2340                                 if (verbose < 2 || !stdout_format_has_i) {
2341                                         itemizing = 0;
2342                                         code = FNONE;
2343 @@ -381,16 +392,36 @@ int hard_link_check(struct file_struct *
2344                                 }
2345                                 break;
2346                         }
2347 -                       if (!unchanged_file(cmpbuf, file, &alt_st))
2348 +                       if (!unchanged_file(cmpbuf, file, &alt_sx.st))
2349                                 continue;
2350                         statret = 1;
2351 -                       *stp = alt_st;
2352 -                       if (unchanged_attrs(file, &alt_st))
2353 +#ifdef SUPPORT_ACLS
2354 +                       if (preserve_acls)
2355 +                               get_acl(cmpbuf, &alt_sx);
2356 +#endif
2357 +                       if (unchanged_attrs(file, &alt_sx))
2358                                 break;
2359                 } while (basis_dir[++j] != NULL);
2360 +               if (statret == 1) {
2361 +                       sxp->st = alt_sx.st;
2362 +#ifdef SUPPORT_ACLS
2363 +                       if (preserve_acls) {
2364 +                               if (!ACL_READY(*sxp))
2365 +                                       get_acl(cmpbuf, sxp);
2366 +                               else {
2367 +                                       sxp->acc_acl = alt_sx.acc_acl;
2368 +                                       sxp->def_acl = alt_sx.def_acl;
2369 +                               }
2370 +                       }
2371 +#endif
2372 +               }
2373 +#ifdef SUPPORT_ACLS
2374 +               else if (preserve_acls)
2375 +                       free_acl(&alt_sx);
2376 +#endif
2377         }
2378  
2379 -       if (maybe_hard_link(file, ndx, fname, statret, stp, prev_name, &prev_st,
2380 +       if (maybe_hard_link(file, ndx, fname, statret, sxp, prev_name, &prev_st,
2381                             realname, itemizing, code) < 0)
2382                 return -1;
2383  
2384 @@ -425,7 +456,8 @@ void finish_hard_link(struct file_struct
2385                       STRUCT_STAT *stp, int itemizing, enum logcode code,
2386                       int alt_dest)
2387  {
2388 -       STRUCT_STAT st, prev_st;
2389 +       statx prev_sx;
2390 +       STRUCT_STAT st;
2391         char alt_name[MAXPATHLEN], *prev_name;
2392         const char *our_name;
2393         int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
2394 @@ -449,14 +481,24 @@ void finish_hard_link(struct file_struct
2395         } else
2396                 our_name = fname;
2397  
2398 +#ifdef SUPPORT_ACLS
2399 +       prev_sx.acc_acl = prev_sx.def_acl = NULL;
2400 +#endif
2401 +
2402         while ((ndx = prev_ndx) >= 0) {
2403 +               int val;
2404                 file = FPTR(ndx);
2405                 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
2406                 prev_ndx = F_HL_PREV(file);
2407                 prev_name = f_name(file, NULL);
2408 -               prev_statret = link_stat(prev_name, &prev_st, 0);
2409 -               if (maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_st,
2410 -                                   our_name, stp, fname, itemizing, code) < 0)
2411 +               prev_statret = link_stat(prev_name, &prev_sx.st, 0);
2412 +               val = maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_sx,
2413 +                                     our_name, stp, fname, itemizing, code);
2414 +#ifdef SUPPORT_ACLS
2415 +               if (preserve_acls)
2416 +                       free_acl(&prev_sx);
2417 +#endif
2418 +               if (val < 0)
2419                         continue;
2420                 if (remove_source_files == 1 && do_xfers)
2421                         send_msg_int(MSG_SUCCESS, ndx);
2422 --- old/lib/sysacls.c
2423 +++ new/lib/sysacls.c
2424 @@ -0,0 +1,3251 @@
2425 +/* 
2426 +   Unix SMB/CIFS implementation.
2427 +   Samba system utilities for ACL support.
2428 +   Copyright (C) Jeremy Allison 2000.
2429 +   
2430 +   This program is free software; you can redistribute it and/or modify
2431 +   it under the terms of the GNU General Public License as published by
2432 +   the Free Software Foundation; either version 2 of the License, or
2433 +   (at your option) any later version.
2434 +   
2435 +   This program is distributed in the hope that it will be useful,
2436 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
2437 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2438 +   GNU General Public License for more details.
2439 +   
2440 +   You should have received a copy of the GNU General Public License
2441 +   along with this program; if not, write to the Free Software
2442 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2443 +*/
2444 +
2445 +#include "rsync.h"
2446 +#include "sysacls.h" /****** ADDED ******/
2447 +
2448 +#ifdef SUPPORT_ACLS
2449 +
2450 +/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
2451 +#ifdef DEBUG
2452 +#undef DEBUG
2453 +#endif
2454 +#define DEBUG(x,y)
2455 +
2456 +void SAFE_FREE(void *mem)
2457 +{
2458 +       if (mem)
2459 +               free(mem);
2460 +}
2461 +
2462 +char *uidtoname(uid_t uid)
2463 +{
2464 +       static char idbuf[12];
2465 +       struct passwd *pw;
2466 +
2467 +       if ((pw = getpwuid(uid)) == NULL) {
2468 +               slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
2469 +               return idbuf;
2470 +       }
2471 +       return pw->pw_name;
2472 +}
2473 +/****** EXTRAS -- END ******/
2474 +
2475 +/*
2476 + This file wraps all differing system ACL interfaces into a consistent
2477 + one based on the POSIX interface. It also returns the correct errors
2478 + for older UNIX systems that don't support ACLs.
2479 +
2480 + The interfaces that each ACL implementation must support are as follows :
2481 +
2482 + int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2483 + int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2484 + int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
2485 + void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2486 + SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2487 + SMB_ACL_T sys_acl_get_fd(int fd)
2488 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
2489 + int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
2490 + char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
2491 + SMB_ACL_T sys_acl_init( int count)
2492 + int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2493 + int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2494 + int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2495 + int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2496 + int sys_acl_valid( SMB_ACL_T theacl )
2497 + int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2498 + int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2499 + int sys_acl_delete_def_file(const char *path)
2500 +
2501 + This next one is not POSIX complient - but we *have* to have it !
2502 + More POSIX braindamage.
2503 +
2504 + int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2505 +
2506 + The generic POSIX free is the following call. We split this into
2507 + several different free functions as we may need to add tag info
2508 + to structures when emulating the POSIX interface.
2509 +
2510 + int sys_acl_free( void *obj_p)
2511 +
2512 + The calls we actually use are :
2513 +
2514 + int sys_acl_free_text(char *text) - free acl_to_text
2515 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
2516 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
2517 +
2518 +*/
2519 +
2520 +#if defined(HAVE_POSIX_ACLS)
2521 +
2522 +/* Identity mapping - easy. */
2523 +
2524 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2525 +{
2526 +       return acl_get_entry( the_acl, entry_id, entry_p);
2527 +}
2528 +
2529 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2530 +{
2531 +       return acl_get_tag_type( entry_d, tag_type_p);
2532 +}
2533 +
2534 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2535 +{
2536 +       return acl_get_permset( entry_d, permset_p);
2537 +}
2538 +
2539 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2540 +{
2541 +       return acl_get_qualifier( entry_d);
2542 +}
2543 +
2544 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2545 +{
2546 +       return acl_get_file( path_p, type);
2547 +}
2548 +
2549 +SMB_ACL_T sys_acl_get_fd(int fd)
2550 +{
2551 +       return acl_get_fd(fd);
2552 +}
2553 +
2554 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
2555 +{
2556 +       return acl_clear_perms(permset);
2557 +}
2558 +
2559 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2560 +{
2561 +       return acl_add_perm(permset, perm);
2562 +}
2563 +
2564 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2565 +{
2566 +#if defined(HAVE_ACL_GET_PERM_NP)
2567 +       /*
2568 +        * Required for TrustedBSD-based ACL implementations where
2569 +        * non-POSIX.1e functions are denoted by a _np (non-portable)
2570 +        * suffix.
2571 +        */
2572 +       return acl_get_perm_np(permset, perm);
2573 +#else
2574 +       return acl_get_perm(permset, perm);
2575 +#endif
2576 +}
2577 +
2578 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
2579 +{
2580 +       return acl_to_text( the_acl, plen);
2581 +}
2582 +
2583 +SMB_ACL_T sys_acl_init( int count)
2584 +{
2585 +       return acl_init(count);
2586 +}
2587 +
2588 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2589 +{
2590 +       return acl_create_entry(pacl, pentry);
2591 +}
2592 +
2593 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2594 +{
2595 +       return acl_set_tag_type(entry, tagtype);
2596 +}
2597 +
2598 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2599 +{
2600 +       return acl_set_qualifier(entry, qual);
2601 +}
2602 +
2603 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2604 +{
2605 +       return acl_set_permset(entry, permset);
2606 +}
2607 +
2608 +int sys_acl_valid( SMB_ACL_T theacl )
2609 +{
2610 +       return acl_valid(theacl);
2611 +}
2612 +
2613 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2614 +{
2615 +       return acl_set_file(name, acltype, theacl);
2616 +}
2617 +
2618 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2619 +{
2620 +       return acl_set_fd(fd, theacl);
2621 +}
2622 +
2623 +int sys_acl_delete_def_file(const char *name)
2624 +{
2625 +       return acl_delete_def_file(name);
2626 +}
2627 +
2628 +int sys_acl_free_text(char *text)
2629 +{
2630 +       return acl_free(text);
2631 +}
2632 +
2633 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
2634 +{
2635 +       return acl_free(the_acl);
2636 +}
2637 +
2638 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
2639 +{
2640 +       return acl_free(qual);
2641 +}
2642 +
2643 +#elif defined(HAVE_TRU64_ACLS)
2644 +/*
2645 + * The interface to DEC/Compaq Tru64 UNIX ACLs
2646 + * is based on Draft 13 of the POSIX spec which is
2647 + * slightly different from the Draft 16 interface.
2648 + * 
2649 + * Also, some of the permset manipulation functions
2650 + * such as acl_clear_perm() and acl_add_perm() appear
2651 + * to be broken on Tru64 so we have to manipulate
2652 + * the permission bits in the permset directly.
2653 + */
2654 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2655 +{
2656 +       SMB_ACL_ENTRY_T entry;
2657 +
2658 +       if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
2659 +               return -1;
2660 +       }
2661 +
2662 +       errno = 0;
2663 +       if ((entry = acl_get_entry(the_acl)) != NULL) {
2664 +               *entry_p = entry;
2665 +               return 1;
2666 +       }
2667 +
2668 +       return errno ? -1 : 0;
2669 +}
2670 +
2671 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2672 +{
2673 +       return acl_get_tag_type( entry_d, tag_type_p);
2674 +}
2675 +
2676 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2677 +{
2678 +       return acl_get_permset( entry_d, permset_p);
2679 +}
2680 +
2681 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2682 +{
2683 +       return acl_get_qualifier( entry_d);
2684 +}
2685 +
2686 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2687 +{
2688 +       return acl_get_file((char *)path_p, type);
2689 +}
2690 +
2691 +SMB_ACL_T sys_acl_get_fd(int fd)
2692 +{
2693 +       return acl_get_fd(fd, ACL_TYPE_ACCESS);
2694 +}
2695 +
2696 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
2697 +{
2698 +       *permset = 0;           /* acl_clear_perm() is broken on Tru64  */
2699 +
2700 +       return 0;
2701 +}
2702 +
2703 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2704 +{
2705 +       if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
2706 +               errno = EINVAL;
2707 +               return -1;
2708 +       }
2709 +
2710 +       *permset |= perm;       /* acl_add_perm() is broken on Tru64    */
2711 +
2712 +       return 0;
2713 +}
2714 +
2715 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2716 +{
2717 +       return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
2718 +}
2719 +
2720 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
2721 +{
2722 +       return acl_to_text( the_acl, plen);
2723 +}
2724 +
2725 +SMB_ACL_T sys_acl_init( int count)
2726 +{
2727 +       return acl_init(count);
2728 +}
2729 +
2730 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2731 +{
2732 +       SMB_ACL_ENTRY_T entry;
2733 +
2734 +       if ((entry = acl_create_entry(pacl)) == NULL) {
2735 +               return -1;
2736 +       }
2737 +
2738 +       *pentry = entry;
2739 +       return 0;
2740 +}
2741 +
2742 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2743 +{
2744 +       return acl_set_tag_type(entry, tagtype);
2745 +}
2746 +
2747 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2748 +{
2749 +       return acl_set_qualifier(entry, qual);
2750 +}
2751 +
2752 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2753 +{
2754 +       return acl_set_permset(entry, permset);
2755 +}
2756 +
2757 +int sys_acl_valid( SMB_ACL_T theacl )
2758 +{
2759 +       acl_entry_t     entry;
2760 +
2761 +       return acl_valid(theacl, &entry);
2762 +}
2763 +
2764 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2765 +{
2766 +       return acl_set_file((char *)name, acltype, theacl);
2767 +}
2768 +
2769 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2770 +{
2771 +       return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
2772 +}
2773 +
2774 +int sys_acl_delete_def_file(const char *name)
2775 +{
2776 +       return acl_delete_def_file((char *)name);
2777 +}
2778 +
2779 +int sys_acl_free_text(char *text)
2780 +{
2781 +       /*
2782 +        * (void) cast and explicit return 0 are for DEC UNIX
2783 +        *  which just #defines acl_free_text() to be free()
2784 +        */
2785 +       (void) acl_free_text(text);
2786 +       return 0;
2787 +}
2788 +
2789 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
2790 +{
2791 +       return acl_free(the_acl);
2792 +}
2793 +
2794 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2795 +{
2796 +       return acl_free_qualifier(qual, tagtype);
2797 +}
2798 +
2799 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
2800 +
2801 +/*
2802 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
2803 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
2804 + */
2805 +
2806 +/*
2807 + * Note that while this code implements sufficient functionality
2808 + * to support the sys_acl_* interfaces it does not provide all
2809 + * of the semantics of the POSIX ACL interfaces.
2810 + *
2811 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2812 + * from a call to sys_acl_get_entry() should not be assumed to be
2813 + * valid after calling any of the following functions, which may
2814 + * reorder the entries in the ACL.
2815 + *
2816 + *     sys_acl_valid()
2817 + *     sys_acl_set_file()
2818 + *     sys_acl_set_fd()
2819 + */
2820 +
2821 +/*
2822 + * The only difference between Solaris and UnixWare / OpenUNIX is
2823 + * that the #defines for the ACL operations have different names
2824 + */
2825 +#if defined(HAVE_UNIXWARE_ACLS)
2826 +
2827 +#define        SETACL          ACL_SET
2828 +#define        GETACL          ACL_GET
2829 +#define        GETACLCNT       ACL_CNT
2830 +
2831 +#endif
2832 +
2833 +
2834 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2835 +{
2836 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2837 +               errno = EINVAL;
2838 +               return -1;
2839 +       }
2840 +
2841 +       if (entry_p == NULL) {
2842 +               errno = EINVAL;
2843 +               return -1;
2844 +       }
2845 +
2846 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
2847 +               acl_d->next = 0;
2848 +       }
2849 +
2850 +       if (acl_d->next < 0) {
2851 +               errno = EINVAL;
2852 +               return -1;
2853 +       }
2854 +
2855 +       if (acl_d->next >= acl_d->count) {
2856 +               return 0;
2857 +       }
2858 +
2859 +       *entry_p = &acl_d->acl[acl_d->next++];
2860 +
2861 +       return 1;
2862 +}
2863 +
2864 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2865 +{
2866 +       *type_p = entry_d->a_type;
2867 +
2868 +       return 0;
2869 +}
2870 +
2871 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2872 +{
2873 +       *permset_p = &entry_d->a_perm;
2874 +
2875 +       return 0;
2876 +}
2877 +
2878 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2879 +{
2880 +       if (entry_d->a_type != SMB_ACL_USER
2881 +           && entry_d->a_type != SMB_ACL_GROUP) {
2882 +               errno = EINVAL;
2883 +               return NULL;
2884 +       }
2885 +
2886 +       return &entry_d->a_id;
2887 +}
2888 +
2889 +/*
2890 + * There is no way of knowing what size the ACL returned by
2891 + * GETACL will be unless you first call GETACLCNT which means
2892 + * making an additional system call.
2893 + *
2894 + * In the hope of avoiding the cost of the additional system
2895 + * call in most cases, we initially allocate enough space for
2896 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2897 + * be too small then we use GETACLCNT to find out the actual
2898 + * size, reallocate the ACL buffer, and then call GETACL again.
2899 + */
2900 +
2901 +#define        INITIAL_ACL_SIZE        16
2902 +
2903 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2904 +{
2905 +       SMB_ACL_T       acl_d;
2906 +       int             count;          /* # of ACL entries allocated   */
2907 +       int             naccess;        /* # of access ACL entries      */
2908 +       int             ndefault;       /* # of default ACL entries     */
2909 +
2910 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2911 +               errno = EINVAL;
2912 +               return NULL;
2913 +       }
2914 +
2915 +       count = INITIAL_ACL_SIZE;
2916 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2917 +               return NULL;
2918 +       }
2919 +
2920 +       /*
2921 +        * If there isn't enough space for the ACL entries we use
2922 +        * GETACLCNT to determine the actual number of ACL entries
2923 +        * reallocate and try again. This is in a loop because it
2924 +        * is possible that someone else could modify the ACL and
2925 +        * increase the number of entries between the call to
2926 +        * GETACLCNT and the call to GETACL.
2927 +        */
2928 +       while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
2929 +           && errno == ENOSPC) {
2930 +
2931 +               sys_acl_free_acl(acl_d);
2932 +
2933 +               if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
2934 +                       return NULL;
2935 +               }
2936 +
2937 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2938 +                       return NULL;
2939 +               }
2940 +       }
2941 +
2942 +       if (count < 0) {
2943 +               sys_acl_free_acl(acl_d);
2944 +               return NULL;
2945 +       }
2946 +
2947 +       /*
2948 +        * calculate the number of access and default ACL entries
2949 +        *
2950 +        * Note: we assume that the acl() system call returned a
2951 +        * well formed ACL which is sorted so that all of the
2952 +        * access ACL entries preceed any default ACL entries
2953 +        */
2954 +       for (naccess = 0; naccess < count; naccess++) {
2955 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2956 +                       break;
2957 +       }
2958 +       ndefault = count - naccess;
2959 +       
2960 +       /*
2961 +        * if the caller wants the default ACL we have to copy
2962 +        * the entries down to the start of the acl[] buffer
2963 +        * and mask out the ACL_DEFAULT flag from the type field
2964 +        */
2965 +       if (type == SMB_ACL_TYPE_DEFAULT) {
2966 +               int     i, j;
2967 +
2968 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
2969 +                       acl_d->acl[i] = acl_d->acl[j];
2970 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2971 +               }
2972 +
2973 +               acl_d->count = ndefault;
2974 +       } else {
2975 +               acl_d->count = naccess;
2976 +       }
2977 +
2978 +       return acl_d;
2979 +}
2980 +
2981 +SMB_ACL_T sys_acl_get_fd(int fd)
2982 +{
2983 +       SMB_ACL_T       acl_d;
2984 +       int             count;          /* # of ACL entries allocated   */
2985 +       int             naccess;        /* # of access ACL entries      */
2986 +
2987 +       count = INITIAL_ACL_SIZE;
2988 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2989 +               return NULL;
2990 +       }
2991 +
2992 +       while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2993 +           && errno == ENOSPC) {
2994 +
2995 +               sys_acl_free_acl(acl_d);
2996 +
2997 +               if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2998 +                       return NULL;
2999 +               }
3000 +
3001 +               if ((acl_d = sys_acl_init(count)) == NULL) {
3002 +                       return NULL;
3003 +               }
3004 +       }
3005 +
3006 +       if (count < 0) {
3007 +               sys_acl_free_acl(acl_d);
3008 +               return NULL;
3009 +       }
3010 +
3011 +       /*
3012 +        * calculate the number of access ACL entries
3013 +        */
3014 +       for (naccess = 0; naccess < count; naccess++) {
3015 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
3016 +                       break;
3017 +       }
3018 +       
3019 +       acl_d->count = naccess;
3020 +
3021 +       return acl_d;
3022 +}
3023 +
3024 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3025 +{
3026 +       *permset_d = 0;
3027 +
3028 +       return 0;
3029 +}
3030 +
3031 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3032 +{
3033 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3034 +           && perm != SMB_ACL_EXECUTE) {
3035 +               errno = EINVAL;
3036 +               return -1;
3037 +       }
3038 +
3039 +       if (permset_d == NULL) {
3040 +               errno = EINVAL;
3041 +               return -1;
3042 +       }
3043 +
3044 +       *permset_d |= perm;
3045 +
3046 +       return 0;
3047 +}
3048 +
3049 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3050 +{
3051 +       return *permset_d & perm;
3052 +}
3053 +
3054 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3055 +{
3056 +       int     i;
3057 +       int     len, maxlen;
3058 +       char    *text;
3059 +
3060 +       /*
3061 +        * use an initial estimate of 20 bytes per ACL entry
3062 +        * when allocating memory for the text representation
3063 +        * of the ACL
3064 +        */
3065 +       len     = 0;
3066 +       maxlen  = 20 * acl_d->count;
3067 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
3068 +               errno = ENOMEM;
3069 +               return NULL;
3070 +       }
3071 +
3072 +       for (i = 0; i < acl_d->count; i++) {
3073 +               struct acl      *ap     = &acl_d->acl[i];
3074 +               struct group    *gr;
3075 +               char            tagbuf[12];
3076 +               char            idbuf[12];
3077 +               char            *tag;
3078 +               char            *id     = "";
3079 +               char            perms[4];
3080 +               int             nbytes;
3081 +
3082 +               switch (ap->a_type) {
3083 +                       /*
3084 +                        * for debugging purposes it's probably more
3085 +                        * useful to dump unknown tag types rather
3086 +                        * than just returning an error
3087 +                        */
3088 +                       default:
3089 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
3090 +                                       ap->a_type);
3091 +                               tag = tagbuf;
3092 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
3093 +                                       (long)ap->a_id);
3094 +                               id = idbuf;
3095 +                               break;
3096 +
3097 +                       case SMB_ACL_USER:
3098 +                               id = uidtoname(ap->a_id);
3099 +                       case SMB_ACL_USER_OBJ:
3100 +                               tag = "user";
3101 +                               break;
3102 +
3103 +                       case SMB_ACL_GROUP:
3104 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
3105 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
3106 +                                               (long)ap->a_id);
3107 +                                       id = idbuf;
3108 +                               } else {
3109 +                                       id = gr->gr_name;
3110 +                               }
3111 +                       case SMB_ACL_GROUP_OBJ:
3112 +                               tag = "group";
3113 +                               break;
3114 +
3115 +                       case SMB_ACL_OTHER:
3116 +                               tag = "other";
3117 +                               break;
3118 +
3119 +                       case SMB_ACL_MASK:
3120 +                               tag = "mask";
3121 +                               break;
3122 +
3123 +               }
3124 +
3125 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
3126 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
3127 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
3128 +               perms[3] = '\0';
3129 +
3130 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
3131 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
3132 +
3133 +               /*
3134 +                * If this entry would overflow the buffer
3135 +                * allocate enough additional memory for this
3136 +                * entry and an estimate of another 20 bytes
3137 +                * for each entry still to be processed
3138 +                */
3139 +               if ((len + nbytes) > maxlen) {
3140 +                       char *oldtext = text;
3141 +
3142 +                       maxlen += nbytes + 20 * (acl_d->count - i);
3143 +
3144 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
3145 +                               SAFE_FREE(oldtext);
3146 +                               errno = ENOMEM;
3147 +                               return NULL;
3148 +                       }
3149 +               }
3150 +
3151 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
3152 +               len += nbytes - 1;
3153 +       }
3154 +
3155 +       if (len_p)
3156 +               *len_p = len;
3157 +
3158 +       return text;
3159 +}
3160 +
3161 +SMB_ACL_T sys_acl_init(int count)
3162 +{
3163 +       SMB_ACL_T       a;
3164 +
3165 +       if (count < 0) {
3166 +               errno = EINVAL;
3167 +               return NULL;
3168 +       }
3169 +
3170 +       /*
3171 +        * note that since the definition of the structure pointed
3172 +        * to by the SMB_ACL_T includes the first element of the
3173 +        * acl[] array, this actually allocates an ACL with room
3174 +        * for (count+1) entries
3175 +        */
3176 +       if ((a = (SMB_ACL_T)SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
3177 +               errno = ENOMEM;
3178 +               return NULL;
3179 +       }
3180 +
3181 +       a->size = count + 1;
3182 +       a->count = 0;
3183 +       a->next = -1;
3184 +
3185 +       return a;
3186 +}
3187 +
3188 +
3189 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3190 +{
3191 +       SMB_ACL_T       acl_d;
3192 +       SMB_ACL_ENTRY_T entry_d;
3193 +
3194 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3195 +               errno = EINVAL;
3196 +               return -1;
3197 +       }
3198 +
3199 +       if (acl_d->count >= acl_d->size) {
3200 +               errno = ENOSPC;
3201 +               return -1;
3202 +       }
3203 +
3204 +       entry_d         = &acl_d->acl[acl_d->count++];
3205 +       entry_d->a_type = 0;
3206 +       entry_d->a_id   = -1;
3207 +       entry_d->a_perm = 0;
3208 +       *entry_p        = entry_d;
3209 +
3210 +       return 0;
3211 +}
3212 +
3213 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3214 +{
3215 +       switch (tag_type) {
3216 +               case SMB_ACL_USER:
3217 +               case SMB_ACL_USER_OBJ:
3218 +               case SMB_ACL_GROUP:
3219 +               case SMB_ACL_GROUP_OBJ:
3220 +               case SMB_ACL_OTHER:
3221 +               case SMB_ACL_MASK:
3222 +                       entry_d->a_type = tag_type;
3223 +                       break;
3224 +               default:
3225 +                       errno = EINVAL;
3226 +                       return -1;
3227 +       }
3228 +
3229 +       return 0;
3230 +}
3231 +
3232 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3233 +{
3234 +       if (entry_d->a_type != SMB_ACL_GROUP
3235 +           && entry_d->a_type != SMB_ACL_USER) {
3236 +               errno = EINVAL;
3237 +               return -1;
3238 +       }
3239 +
3240 +       entry_d->a_id = *((id_t *)qual_p);
3241 +
3242 +       return 0;
3243 +}
3244 +
3245 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3246 +{
3247 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3248 +               return EINVAL;
3249 +       }
3250 +
3251 +       entry_d->a_perm = *permset_d;
3252 +
3253 +       return 0;
3254 +}
3255 +
3256 +/*
3257 + * sort the ACL and check it for validity
3258 + *
3259 + * if it's a minimal ACL with only 4 entries then we
3260 + * need to recalculate the mask permissions to make
3261 + * sure that they are the same as the GROUP_OBJ
3262 + * permissions as required by the UnixWare acl() system call.
3263 + *
3264 + * (note: since POSIX allows minimal ACLs which only contain
3265 + * 3 entries - ie there is no mask entry - we should, in theory,
3266 + * check for this and add a mask entry if necessary - however
3267 + * we "know" that the caller of this interface always specifies
3268 + * a mask so, in practice "this never happens" (tm) - if it *does*
3269 + * happen aclsort() will fail and return an error and someone will
3270 + * have to fix it ...)
3271 + */
3272 +
3273 +static int acl_sort(SMB_ACL_T acl_d)
3274 +{
3275 +       int     fixmask = (acl_d->count <= 4);
3276 +
3277 +       if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
3278 +               errno = EINVAL;
3279 +               return -1;
3280 +       }
3281 +       return 0;
3282 +}
3283
3284 +int sys_acl_valid(SMB_ACL_T acl_d)
3285 +{
3286 +       return acl_sort(acl_d);
3287 +}
3288 +
3289 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3290 +{
3291 +       struct stat     s;
3292 +       struct acl      *acl_p;
3293 +       int             acl_count;
3294 +       struct acl      *acl_buf        = NULL;
3295 +       int             ret;
3296 +
3297 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3298 +               errno = EINVAL;
3299 +               return -1;
3300 +       }
3301 +
3302 +       if (acl_sort(acl_d) != 0) {
3303 +               return -1;
3304 +       }
3305 +
3306 +       acl_p           = &acl_d->acl[0];
3307 +       acl_count       = acl_d->count;
3308 +
3309 +       /*
3310 +        * if it's a directory there is extra work to do
3311 +        * since the acl() system call will replace both
3312 +        * the access ACLs and the default ACLs (if any)
3313 +        */
3314 +       if (stat(name, &s) != 0) {
3315 +               return -1;
3316 +       }
3317 +       if (S_ISDIR(s.st_mode)) {
3318 +               SMB_ACL_T       acc_acl;
3319 +               SMB_ACL_T       def_acl;
3320 +               SMB_ACL_T       tmp_acl;
3321 +               int             i;
3322 +
3323 +               if (type == SMB_ACL_TYPE_ACCESS) {
3324 +                       acc_acl = acl_d;
3325 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3326 +
3327 +               } else {
3328 +                       def_acl = acl_d;
3329 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3330 +               }
3331 +
3332 +               if (tmp_acl == NULL) {
3333 +                       return -1;
3334 +               }
3335 +
3336 +               /*
3337 +                * allocate a temporary buffer for the complete ACL
3338 +                */
3339 +               acl_count = acc_acl->count + def_acl->count;
3340 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
3341 +
3342 +               if (acl_buf == NULL) {
3343 +                       sys_acl_free_acl(tmp_acl);
3344 +                       errno = ENOMEM;
3345 +                       return -1;
3346 +               }
3347 +
3348 +               /*
3349 +                * copy the access control and default entries into the buffer
3350 +                */
3351 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
3352 +                       acc_acl->count * sizeof(acl_buf[0]));
3353 +
3354 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3355 +                       def_acl->count * sizeof(acl_buf[0]));
3356 +
3357 +               /*
3358 +                * set the ACL_DEFAULT flag on the default entries
3359 +                */
3360 +               for (i = acc_acl->count; i < acl_count; i++) {
3361 +                       acl_buf[i].a_type |= ACL_DEFAULT;
3362 +               }
3363 +
3364 +               sys_acl_free_acl(tmp_acl);
3365 +
3366 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
3367 +               errno = EINVAL;
3368 +               return -1;
3369 +       }
3370 +
3371 +       ret = acl(name, SETACL, acl_count, acl_p);
3372 +
3373 +       SAFE_FREE(acl_buf);
3374 +
3375 +       return ret;
3376 +}
3377 +
3378 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3379 +{
3380 +       if (acl_sort(acl_d) != 0) {
3381 +               return -1;
3382 +       }
3383 +
3384 +       return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
3385 +}
3386 +
3387 +int sys_acl_delete_def_file(const char *path)
3388 +{
3389 +       SMB_ACL_T       acl_d;
3390 +       int             ret;
3391 +
3392 +       /*
3393 +        * fetching the access ACL and rewriting it has
3394 +        * the effect of deleting the default ACL
3395 +        */
3396 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3397 +               return -1;
3398 +       }
3399 +
3400 +       ret = acl(path, SETACL, acl_d->count, acl_d->acl);
3401 +
3402 +       sys_acl_free_acl(acl_d);
3403 +       
3404 +       return ret;
3405 +}
3406 +
3407 +int sys_acl_free_text(char *text)
3408 +{
3409 +       SAFE_FREE(text);
3410 +       return 0;
3411 +}
3412 +
3413 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
3414 +{
3415 +       SAFE_FREE(acl_d);
3416 +       return 0;
3417 +}
3418 +
3419 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
3420 +{
3421 +       return 0;
3422 +}
3423 +
3424 +#elif defined(HAVE_HPUX_ACLS)
3425 +#include <dl.h>
3426 +
3427 +/*
3428 + * Based on the Solaris/SCO code - with modifications.
3429 + */
3430 +
3431 +/*
3432 + * Note that while this code implements sufficient functionality
3433 + * to support the sys_acl_* interfaces it does not provide all
3434 + * of the semantics of the POSIX ACL interfaces.
3435 + *
3436 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
3437 + * from a call to sys_acl_get_entry() should not be assumed to be
3438 + * valid after calling any of the following functions, which may
3439 + * reorder the entries in the ACL.
3440 + *
3441 + *     sys_acl_valid()
3442 + *     sys_acl_set_file()
3443 + *     sys_acl_set_fd()
3444 + */
3445 +
3446 +/* This checks if the POSIX ACL system call is defined */
3447 +/* which basically corresponds to whether JFS 3.3 or   */
3448 +/* higher is installed. If acl() was called when it    */
3449 +/* isn't defined, it causes the process to core dump   */
3450 +/* so it is important to check this and avoid acl()    */
3451 +/* calls if it isn't there.                            */
3452 +
3453 +static BOOL hpux_acl_call_presence(void)
3454 +{
3455 +
3456 +       shl_t handle = NULL;
3457 +       void *value;
3458 +       int ret_val=0;
3459 +       static BOOL already_checked=0;
3460 +
3461 +       if(already_checked)
3462 +               return True;
3463 +
3464 +
3465 +       ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
3466 +
3467 +       if(ret_val != 0) {
3468 +               DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
3469 +                       ret_val, errno, strerror(errno)));
3470 +               DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
3471 +               return False;
3472 +       }
3473 +
3474 +       DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
3475 +
3476 +       already_checked = True;
3477 +       return True;
3478 +}
3479 +
3480 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3481 +{
3482 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3483 +               errno = EINVAL;
3484 +               return -1;
3485 +       }
3486 +
3487 +       if (entry_p == NULL) {
3488 +               errno = EINVAL;
3489 +               return -1;
3490 +       }
3491 +
3492 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
3493 +               acl_d->next = 0;
3494 +       }
3495 +
3496 +       if (acl_d->next < 0) {
3497 +               errno = EINVAL;
3498 +               return -1;
3499 +       }
3500 +
3501 +       if (acl_d->next >= acl_d->count) {
3502 +               return 0;
3503 +       }
3504 +
3505 +       *entry_p = &acl_d->acl[acl_d->next++];
3506 +
3507 +       return 1;
3508 +}
3509 +
3510 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
3511 +{
3512 +       *type_p = entry_d->a_type;
3513 +
3514 +       return 0;
3515 +}
3516 +
3517 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3518 +{
3519 +       *permset_p = &entry_d->a_perm;
3520 +
3521 +       return 0;
3522 +}
3523 +
3524 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3525 +{
3526 +       if (entry_d->a_type != SMB_ACL_USER
3527 +           && entry_d->a_type != SMB_ACL_GROUP) {
3528 +               errno = EINVAL;
3529 +               return NULL;
3530 +       }
3531 +
3532 +       return &entry_d->a_id;
3533 +}
3534 +
3535 +/*
3536 + * There is no way of knowing what size the ACL returned by
3537 + * ACL_GET will be unless you first call ACL_CNT which means
3538 + * making an additional system call.
3539 + *
3540 + * In the hope of avoiding the cost of the additional system
3541 + * call in most cases, we initially allocate enough space for
3542 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
3543 + * be too small then we use ACL_CNT to find out the actual
3544 + * size, reallocate the ACL buffer, and then call ACL_GET again.
3545 + */
3546 +
3547 +#define        INITIAL_ACL_SIZE        16
3548 +
3549 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3550 +{
3551 +       SMB_ACL_T       acl_d;
3552 +       int             count;          /* # of ACL entries allocated   */
3553 +       int             naccess;        /* # of access ACL entries      */
3554 +       int             ndefault;       /* # of default ACL entries     */
3555 +
3556 +       if(hpux_acl_call_presence() == False) {
3557 +               /* Looks like we don't have the acl() system call on HPUX. 
3558 +                * May be the system doesn't have the latest version of JFS.
3559 +                */
3560 +               return NULL; 
3561 +       }
3562 +
3563 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3564 +               errno = EINVAL;
3565 +               return NULL;
3566 +       }
3567 +
3568 +       count = INITIAL_ACL_SIZE;
3569 +       if ((acl_d = sys_acl_init(count)) == NULL) {
3570 +               return NULL;
3571 +       }
3572 +
3573 +       /*
3574 +        * If there isn't enough space for the ACL entries we use
3575 +        * ACL_CNT to determine the actual number of ACL entries
3576 +        * reallocate and try again. This is in a loop because it
3577 +        * is possible that someone else could modify the ACL and
3578 +        * increase the number of entries between the call to
3579 +        * ACL_CNT and the call to ACL_GET.
3580 +        */
3581 +       while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
3582 +
3583 +               sys_acl_free_acl(acl_d);
3584 +
3585 +               if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
3586 +                       return NULL;
3587 +               }
3588 +
3589 +               if ((acl_d = sys_acl_init(count)) == NULL) {
3590 +                       return NULL;
3591 +               }
3592 +       }
3593 +
3594 +       if (count < 0) {
3595 +               sys_acl_free_acl(acl_d);
3596 +               return NULL;
3597 +       }
3598 +
3599 +       /*
3600 +        * calculate the number of access and default ACL entries
3601 +        *
3602 +        * Note: we assume that the acl() system call returned a
3603 +        * well formed ACL which is sorted so that all of the
3604 +        * access ACL entries preceed any default ACL entries
3605 +        */
3606 +       for (naccess = 0; naccess < count; naccess++) {
3607 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
3608 +                       break;
3609 +       }
3610 +       ndefault = count - naccess;
3611 +       
3612 +       /*
3613 +        * if the caller wants the default ACL we have to copy
3614 +        * the entries down to the start of the acl[] buffer
3615 +        * and mask out the ACL_DEFAULT flag from the type field
3616 +        */
3617 +       if (type == SMB_ACL_TYPE_DEFAULT) {
3618 +               int     i, j;
3619 +
3620 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
3621 +                       acl_d->acl[i] = acl_d->acl[j];
3622 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
3623 +               }
3624 +
3625 +               acl_d->count = ndefault;
3626 +       } else {
3627 +               acl_d->count = naccess;
3628 +       }
3629 +
3630 +       return acl_d;
3631 +}
3632 +
3633 +SMB_ACL_T sys_acl_get_fd(int fd)
3634 +{
3635 +       /*
3636 +        * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3637 +        */
3638 +
3639 +       files_struct *fsp = file_find_fd(fd);
3640 +
3641 +       if (fsp == NULL) {
3642 +               errno = EBADF;
3643 +               return NULL;
3644 +       }
3645 +
3646 +       /*
3647 +        * We know we're in the same conn context. So we
3648 +        * can use the relative path.
3649 +        */
3650 +
3651 +       return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
3652 +}
3653 +
3654 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3655 +{
3656 +       *permset_d = 0;
3657 +
3658 +       return 0;
3659 +}
3660 +
3661 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3662 +{
3663 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3664 +           && perm != SMB_ACL_EXECUTE) {
3665 +               errno = EINVAL;
3666 +               return -1;
3667 +       }
3668 +
3669 +       if (permset_d == NULL) {
3670 +               errno = EINVAL;
3671 +               return -1;
3672 +       }
3673 +
3674 +       *permset_d |= perm;
3675 +
3676 +       return 0;
3677 +}
3678 +
3679 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3680 +{
3681 +       return *permset_d & perm;
3682 +}
3683 +
3684 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3685 +{
3686 +       int     i;
3687 +       int     len, maxlen;
3688 +       char    *text;
3689 +
3690 +       /*
3691 +        * use an initial estimate of 20 bytes per ACL entry
3692 +        * when allocating memory for the text representation
3693 +        * of the ACL
3694 +        */
3695 +       len     = 0;
3696 +       maxlen  = 20 * acl_d->count;
3697 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
3698 +               errno = ENOMEM;
3699 +               return NULL;
3700 +       }
3701 +
3702 +       for (i = 0; i < acl_d->count; i++) {
3703 +               struct acl      *ap     = &acl_d->acl[i];
3704 +               struct group    *gr;
3705 +               char            tagbuf[12];
3706 +               char            idbuf[12];
3707 +               char            *tag;
3708 +               char            *id     = "";
3709 +               char            perms[4];
3710 +               int             nbytes;
3711 +
3712 +               switch (ap->a_type) {
3713 +                       /*
3714 +                        * for debugging purposes it's probably more
3715 +                        * useful to dump unknown tag types rather
3716 +                        * than just returning an error
3717 +                        */
3718 +                       default:
3719 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
3720 +                                       ap->a_type);
3721 +                               tag = tagbuf;
3722 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
3723 +                                       (long)ap->a_id);
3724 +                               id = idbuf;
3725 +                               break;
3726 +
3727 +                       case SMB_ACL_USER:
3728 +                               id = uidtoname(ap->a_id);
3729 +                       case SMB_ACL_USER_OBJ:
3730 +                               tag = "user";
3731 +                               break;
3732 +
3733 +                       case SMB_ACL_GROUP:
3734 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
3735 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
3736 +                                               (long)ap->a_id);
3737 +                                       id = idbuf;
3738 +                               } else {
3739 +                                       id = gr->gr_name;
3740 +                               }
3741 +                       case SMB_ACL_GROUP_OBJ:
3742 +                               tag = "group";
3743 +                               break;
3744 +
3745 +                       case SMB_ACL_OTHER:
3746 +                               tag = "other";
3747 +                               break;
3748 +
3749 +                       case SMB_ACL_MASK:
3750 +                               tag = "mask";
3751 +                               break;
3752 +
3753 +               }
3754 +
3755 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
3756 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
3757 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
3758 +               perms[3] = '\0';
3759 +
3760 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
3761 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
3762 +
3763 +               /*
3764 +                * If this entry would overflow the buffer
3765 +                * allocate enough additional memory for this
3766 +                * entry and an estimate of another 20 bytes
3767 +                * for each entry still to be processed
3768 +                */
3769 +               if ((len + nbytes) > maxlen) {
3770 +                       char *oldtext = text;
3771 +
3772 +                       maxlen += nbytes + 20 * (acl_d->count - i);
3773 +
3774 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
3775 +                               free(oldtext);
3776 +                               errno = ENOMEM;
3777 +                               return NULL;
3778 +                       }
3779 +               }
3780 +
3781 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
3782 +               len += nbytes - 1;
3783 +       }
3784 +
3785 +       if (len_p)
3786 +               *len_p = len;
3787 +
3788 +       return text;
3789 +}
3790 +
3791 +SMB_ACL_T sys_acl_init(int count)
3792 +{
3793 +       SMB_ACL_T       a;
3794 +
3795 +       if (count < 0) {
3796 +               errno = EINVAL;
3797 +               return NULL;
3798 +       }
3799 +
3800 +       /*
3801 +        * note that since the definition of the structure pointed
3802 +        * to by the SMB_ACL_T includes the first element of the
3803 +        * acl[] array, this actually allocates an ACL with room
3804 +        * for (count+1) entries
3805 +        */
3806 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
3807 +               errno = ENOMEM;
3808 +               return NULL;
3809 +       }
3810 +
3811 +       a->size = count + 1;
3812 +       a->count = 0;
3813 +       a->next = -1;
3814 +
3815 +       return a;
3816 +}
3817 +
3818 +
3819 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3820 +{
3821 +       SMB_ACL_T       acl_d;
3822 +       SMB_ACL_ENTRY_T entry_d;
3823 +
3824 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3825 +               errno = EINVAL;
3826 +               return -1;
3827 +       }
3828 +
3829 +       if (acl_d->count >= acl_d->size) {
3830 +               errno = ENOSPC;
3831 +               return -1;
3832 +       }
3833 +
3834 +       entry_d         = &acl_d->acl[acl_d->count++];
3835 +       entry_d->a_type = 0;
3836 +       entry_d->a_id   = -1;
3837 +       entry_d->a_perm = 0;
3838 +       *entry_p        = entry_d;
3839 +
3840 +       return 0;
3841 +}
3842 +
3843 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3844 +{
3845 +       switch (tag_type) {
3846 +               case SMB_ACL_USER:
3847 +               case SMB_ACL_USER_OBJ:
3848 +               case SMB_ACL_GROUP:
3849 +               case SMB_ACL_GROUP_OBJ:
3850 +               case SMB_ACL_OTHER:
3851 +               case SMB_ACL_MASK:
3852 +                       entry_d->a_type = tag_type;
3853 +                       break;
3854 +               default:
3855 +                       errno = EINVAL;
3856 +                       return -1;
3857 +       }
3858 +
3859 +       return 0;
3860 +}
3861 +
3862 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3863 +{
3864 +       if (entry_d->a_type != SMB_ACL_GROUP
3865 +           && entry_d->a_type != SMB_ACL_USER) {
3866 +               errno = EINVAL;
3867 +               return -1;
3868 +       }
3869 +
3870 +       entry_d->a_id = *((id_t *)qual_p);
3871 +
3872 +       return 0;
3873 +}
3874 +
3875 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3876 +{
3877 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3878 +               return EINVAL;
3879 +       }
3880 +
3881 +       entry_d->a_perm = *permset_d;
3882 +
3883 +       return 0;
3884 +}
3885 +
3886 +/* Structure to capture the count for each type of ACE. */
3887 +
3888 +struct hpux_acl_types {
3889 +       int n_user;
3890 +       int n_def_user;
3891 +       int n_user_obj;
3892 +       int n_def_user_obj;
3893 +
3894 +       int n_group;
3895 +       int n_def_group;
3896 +       int n_group_obj;
3897 +       int n_def_group_obj;
3898 +
3899 +       int n_other;
3900 +       int n_other_obj;
3901 +       int n_def_other_obj;
3902 +
3903 +       int n_class_obj;
3904 +       int n_def_class_obj;
3905 +
3906 +       int n_illegal_obj;
3907 +};
3908 +
3909 +/* count_obj:
3910 + * Counts the different number of objects in a given array of ACL
3911 + * structures.
3912 + * Inputs:
3913 + *
3914 + * acl_count      - Count of ACLs in the array of ACL strucutres.
3915 + * aclp           - Array of ACL structures.
3916 + * acl_type_count - Pointer to acl_types structure. Should already be
3917 + *                  allocated.
3918 + * Output: 
3919 + *
3920 + * acl_type_count - This structure is filled up with counts of various 
3921 + *                  acl types.
3922 + */
3923 +
3924 +static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
3925 +{
3926 +       int i;
3927 +
3928 +       memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
3929 +
3930 +       for(i=0;i<acl_count;i++) {
3931 +               switch(aclp[i].a_type) {
3932 +               case USER: 
3933 +                       acl_type_count->n_user++;
3934 +                       break;
3935 +               case USER_OBJ: 
3936 +                       acl_type_count->n_user_obj++;
3937 +                       break;
3938 +               case DEF_USER_OBJ: 
3939 +                       acl_type_count->n_def_user_obj++;
3940 +                       break;
3941 +               case GROUP: 
3942 +                       acl_type_count->n_group++;
3943 +                       break;
3944 +               case GROUP_OBJ: 
3945 +                       acl_type_count->n_group_obj++;
3946 +                       break;
3947 +               case DEF_GROUP_OBJ: 
3948 +                       acl_type_count->n_def_group_obj++;
3949 +                       break;
3950 +               case OTHER_OBJ: 
3951 +                       acl_type_count->n_other_obj++;
3952 +                       break;
3953 +               case DEF_OTHER_OBJ: 
3954 +                       acl_type_count->n_def_other_obj++;
3955 +                       break;
3956 +               case CLASS_OBJ:
3957 +                       acl_type_count->n_class_obj++;
3958 +                       break;
3959 +               case DEF_CLASS_OBJ:
3960 +                       acl_type_count->n_def_class_obj++;
3961 +                       break;
3962 +               case DEF_USER:
3963 +                       acl_type_count->n_def_user++;
3964 +                       break;
3965 +               case DEF_GROUP:
3966 +                       acl_type_count->n_def_group++;
3967 +                       break;
3968 +               default: 
3969 +                       acl_type_count->n_illegal_obj++;
3970 +                       break;
3971 +               }
3972 +       }
3973 +}
3974 +
3975 +/* swap_acl_entries:  Swaps two ACL entries. 
3976 + *
3977 + * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3978 + */
3979 +
3980 +static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3981 +{
3982 +       struct acl temp_acl;
3983 +
3984 +       temp_acl.a_type = aclp0->a_type;
3985 +       temp_acl.a_id = aclp0->a_id;
3986 +       temp_acl.a_perm = aclp0->a_perm;
3987 +
3988 +       aclp0->a_type = aclp1->a_type;
3989 +       aclp0->a_id = aclp1->a_id;
3990 +       aclp0->a_perm = aclp1->a_perm;
3991 +
3992 +       aclp1->a_type = temp_acl.a_type;
3993 +       aclp1->a_id = temp_acl.a_id;
3994 +       aclp1->a_perm = temp_acl.a_perm;
3995 +}
3996 +
3997 +/* prohibited_duplicate_type
3998 + * Identifies if given ACL type can have duplicate entries or 
3999 + * not.
4000 + *
4001 + * Inputs: acl_type - ACL Type.
4002 + *
4003 + * Outputs: 
4004 + *
4005 + * Return.. 
4006 + *
4007 + * True - If the ACL type matches any of the prohibited types.
4008 + * False - If the ACL type doesn't match any of the prohibited types.
4009 + */ 
4010 +
4011 +static BOOL hpux_prohibited_duplicate_type(int acl_type)
4012 +{
4013 +       switch(acl_type) {
4014 +               case USER:
4015 +               case GROUP:
4016 +               case DEF_USER: 
4017 +               case DEF_GROUP:
4018 +                       return True;
4019 +               default:
4020 +                       return False;
4021 +       }
4022 +}
4023 +
4024 +/* get_needed_class_perm
4025 + * Returns the permissions of a ACL structure only if the ACL
4026 + * type matches one of the pre-determined types for computing 
4027 + * CLASS_OBJ permissions.
4028 + *
4029 + * Inputs: aclp - Pointer to ACL structure.
4030 + */
4031 +
4032 +static int hpux_get_needed_class_perm(struct acl *aclp)
4033 +{
4034 +       switch(aclp->a_type) {
4035 +               case USER: 
4036 +               case GROUP_OBJ: 
4037 +               case GROUP: 
4038 +               case DEF_USER_OBJ: 
4039 +               case DEF_USER:
4040 +               case DEF_GROUP_OBJ: 
4041 +               case DEF_GROUP:
4042 +               case DEF_CLASS_OBJ:
4043 +               case DEF_OTHER_OBJ: 
4044 +                       return aclp->a_perm;
4045 +               default: 
4046 +                       return 0;
4047 +       }
4048 +}
4049 +
4050 +/* acl_sort for HPUX.
4051 + * Sorts the array of ACL structures as per the description in
4052 + * aclsort man page. Refer to aclsort man page for more details
4053 + *
4054 + * Inputs:
4055 + *
4056 + * acl_count - Count of ACLs in the array of ACL structures.
4057 + * calclass  - If this is not zero, then we compute the CLASS_OBJ
4058 + *             permissions.
4059 + * aclp      - Array of ACL structures.
4060 + *
4061 + * Outputs:
4062 + *
4063 + * aclp     - Sorted array of ACL structures.
4064 + *
4065 + * Outputs:
4066 + *
4067 + * Returns 0 for success -1 for failure. Prints a message to the Samba
4068 + * debug log in case of failure.
4069 + */
4070 +
4071 +static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
4072 +{
4073 +#if !defined(HAVE_HPUX_ACLSORT)
4074 +       /*
4075 +        * The aclsort() system call is availabe on the latest HPUX General
4076 +        * Patch Bundles. So for HPUX, we developed our version of acl_sort 
4077 +        * function. Because, we don't want to update to a new 
4078 +        * HPUX GR bundle just for aclsort() call.
4079 +        */
4080 +
4081 +       struct hpux_acl_types acl_obj_count;
4082 +       int n_class_obj_perm = 0;
4083 +       int i, j;
4084
4085 +       if(!acl_count) {
4086 +               DEBUG(10,("Zero acl count passed. Returning Success\n"));
4087 +               return 0;
4088 +       }
4089 +
4090 +       if(aclp == NULL) {
4091 +               DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
4092 +               return -1;
4093 +       }
4094 +
4095 +       /* Count different types of ACLs in the ACLs array */
4096 +
4097 +       hpux_count_obj(acl_count, aclp, &acl_obj_count);
4098 +
4099 +       /* There should be only one entry each of type USER_OBJ, GROUP_OBJ, 
4100 +        * CLASS_OBJ and OTHER_OBJ 
4101 +        */
4102 +
4103 +       if( (acl_obj_count.n_user_obj  != 1) || 
4104 +               (acl_obj_count.n_group_obj != 1) || 
4105 +               (acl_obj_count.n_class_obj != 1) ||
4106 +               (acl_obj_count.n_other_obj != 1) 
4107 +       ) {
4108 +               DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
4109 +USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
4110 +               return -1;
4111 +       }
4112 +
4113 +       /* If any of the default objects are present, there should be only
4114 +        * one of them each.
4115 +        */
4116 +
4117 +       if( (acl_obj_count.n_def_user_obj  > 1) || (acl_obj_count.n_def_group_obj > 1) || 
4118 +                       (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
4119 +               DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
4120 +or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
4121 +               return -1;
4122 +       }
4123 +
4124 +       /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl 
4125 +        * structures.  
4126 +        *
4127 +        * Sorting crieteria - First sort by ACL type. If there are multiple entries of
4128 +        * same ACL type, sort by ACL id.
4129 +        *
4130 +        * I am using the trival kind of sorting method here because, performance isn't 
4131 +        * really effected by the ACLs feature. More over there aren't going to be more
4132 +        * than 17 entries on HPUX. 
4133 +        */
4134 +
4135 +       for(i=0; i<acl_count;i++) {
4136 +               for (j=i+1; j<acl_count; j++) {
4137 +                       if( aclp[i].a_type > aclp[j].a_type ) {
4138 +                               /* ACL entries out of order, swap them */
4139 +
4140 +                               hpux_swap_acl_entries((aclp+i), (aclp+j));
4141 +
4142 +                       } else if ( aclp[i].a_type == aclp[j].a_type ) {
4143 +
4144 +                               /* ACL entries of same type, sort by id */
4145 +
4146 +                               if(aclp[i].a_id > aclp[j].a_id) {
4147 +                                       hpux_swap_acl_entries((aclp+i), (aclp+j));
4148 +                               } else if (aclp[i].a_id == aclp[j].a_id) {
4149 +                                       /* We have a duplicate entry. */
4150 +                                       if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
4151 +                                               DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
4152 +                                                       aclp[i].a_type, aclp[i].a_id));
4153 +                                               return -1;
4154 +                                       }
4155 +                               }
4156 +
4157 +                       }
4158 +               }
4159 +       }
4160 +
4161 +       /* set the class obj permissions to the computed one. */
4162 +       if(calclass) {
4163 +               int n_class_obj_index = -1;
4164 +
4165 +               for(i=0;i<acl_count;i++) {
4166 +                       n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
4167 +
4168 +                       if(aclp[i].a_type == CLASS_OBJ)
4169 +                               n_class_obj_index = i;
4170 +               }
4171 +               aclp[n_class_obj_index].a_perm = n_class_obj_perm;
4172 +       }
4173 +
4174 +       return 0;
4175 +#else
4176 +       return aclsort(acl_count, calclass, aclp);
4177 +#endif
4178 +}
4179 +
4180 +/*
4181 + * sort the ACL and check it for validity
4182 + *
4183 + * if it's a minimal ACL with only 4 entries then we
4184 + * need to recalculate the mask permissions to make
4185 + * sure that they are the same as the GROUP_OBJ
4186 + * permissions as required by the UnixWare acl() system call.
4187 + *
4188 + * (note: since POSIX allows minimal ACLs which only contain
4189 + * 3 entries - ie there is no mask entry - we should, in theory,
4190 + * check for this and add a mask entry if necessary - however
4191 + * we "know" that the caller of this interface always specifies
4192 + * a mask so, in practice "this never happens" (tm) - if it *does*
4193 + * happen aclsort() will fail and return an error and someone will
4194 + * have to fix it ...)
4195 + */
4196 +
4197 +static int acl_sort(SMB_ACL_T acl_d)
4198 +{
4199 +       int fixmask = (acl_d->count <= 4);
4200 +
4201 +       if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
4202 +               errno = EINVAL;
4203 +               return -1;
4204 +       }
4205 +       return 0;
4206 +}
4207
4208 +int sys_acl_valid(SMB_ACL_T acl_d)
4209 +{
4210 +       return acl_sort(acl_d);
4211 +}
4212 +
4213 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
4214 +{
4215 +       struct stat     s;
4216 +       struct acl      *acl_p;
4217 +       int             acl_count;
4218 +       struct acl      *acl_buf        = NULL;
4219 +       int             ret;
4220 +
4221 +       if(hpux_acl_call_presence() == False) {
4222 +               /* Looks like we don't have the acl() system call on HPUX. 
4223 +                * May be the system doesn't have the latest version of JFS.
4224 +                */
4225 +               errno=ENOSYS;
4226 +               return -1; 
4227 +       }
4228 +
4229 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
4230 +               errno = EINVAL;
4231 +               return -1;
4232 +       }
4233 +
4234 +       if (acl_sort(acl_d) != 0) {
4235 +               return -1;
4236 +       }
4237 +
4238 +       acl_p           = &acl_d->acl[0];
4239 +       acl_count       = acl_d->count;
4240 +
4241 +       /*
4242 +        * if it's a directory there is extra work to do
4243 +        * since the acl() system call will replace both
4244 +        * the access ACLs and the default ACLs (if any)
4245 +        */
4246 +       if (stat(name, &s) != 0) {
4247 +               return -1;
4248 +       }
4249 +       if (S_ISDIR(s.st_mode)) {
4250 +               SMB_ACL_T       acc_acl;
4251 +               SMB_ACL_T       def_acl;
4252 +               SMB_ACL_T       tmp_acl;
4253 +               int             i;
4254 +
4255 +               if (type == SMB_ACL_TYPE_ACCESS) {
4256 +                       acc_acl = acl_d;
4257 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
4258 +
4259 +               } else {
4260 +                       def_acl = acl_d;
4261 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
4262 +               }
4263 +
4264 +               if (tmp_acl == NULL) {
4265 +                       return -1;
4266 +               }
4267 +
4268 +               /*
4269 +                * allocate a temporary buffer for the complete ACL
4270 +                */
4271 +               acl_count = acc_acl->count + def_acl->count;
4272 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
4273 +
4274 +               if (acl_buf == NULL) {
4275 +                       sys_acl_free_acl(tmp_acl);
4276 +                       errno = ENOMEM;
4277 +                       return -1;
4278 +               }
4279 +
4280 +               /*
4281 +                * copy the access control and default entries into the buffer
4282 +                */
4283 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
4284 +                       acc_acl->count * sizeof(acl_buf[0]));
4285 +
4286 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
4287 +                       def_acl->count * sizeof(acl_buf[0]));
4288 +
4289 +               /*
4290 +                * set the ACL_DEFAULT flag on the default entries
4291 +                */
4292 +               for (i = acc_acl->count; i < acl_count; i++) {
4293 +                       acl_buf[i].a_type |= ACL_DEFAULT;
4294 +               }
4295 +
4296 +               sys_acl_free_acl(tmp_acl);
4297 +
4298 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
4299 +               errno = EINVAL;
4300 +               return -1;
4301 +       }
4302 +
4303 +       ret = acl(name, ACL_SET, acl_count, acl_p);
4304 +
4305 +       if (acl_buf) {
4306 +               free(acl_buf);
4307 +       }
4308 +
4309 +       return ret;
4310 +}
4311 +
4312 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
4313 +{
4314 +       /*
4315 +        * HPUX doesn't have the facl call. Fake it using the path.... JRA.
4316 +        */
4317 +
4318 +       files_struct *fsp = file_find_fd(fd);
4319 +
4320 +       if (fsp == NULL) {
4321 +               errno = EBADF;
4322 +               return NULL;
4323 +       }
4324 +
4325 +       if (acl_sort(acl_d) != 0) {
4326 +               return -1;
4327 +       }
4328 +
4329 +       /*
4330 +        * We know we're in the same conn context. So we
4331 +        * can use the relative path.
4332 +        */
4333 +
4334 +       return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
4335 +}
4336 +
4337 +int sys_acl_delete_def_file(const char *path)
4338 +{
4339 +       SMB_ACL_T       acl_d;
4340 +       int             ret;
4341 +
4342 +       /*
4343 +        * fetching the access ACL and rewriting it has
4344 +        * the effect of deleting the default ACL
4345 +        */
4346 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
4347 +               return -1;
4348 +       }
4349 +
4350 +       ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
4351 +
4352 +       sys_acl_free_acl(acl_d);
4353 +       
4354 +       return ret;
4355 +}
4356 +
4357 +int sys_acl_free_text(char *text)
4358 +{
4359 +       free(text);
4360 +       return 0;
4361 +}
4362 +
4363 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
4364 +{
4365 +       free(acl_d);
4366 +       return 0;
4367 +}
4368 +
4369 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4370 +{
4371 +       return 0;
4372 +}
4373 +
4374 +#elif defined(HAVE_IRIX_ACLS)
4375 +
4376 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
4377 +{
4378 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
4379 +               errno = EINVAL;
4380 +               return -1;
4381 +       }
4382 +
4383 +       if (entry_p == NULL) {
4384 +               errno = EINVAL;
4385 +               return -1;
4386 +       }
4387 +
4388 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
4389 +               acl_d->next = 0;
4390 +       }
4391 +
4392 +       if (acl_d->next < 0) {
4393 +               errno = EINVAL;
4394 +               return -1;
4395 +       }
4396 +
4397 +       if (acl_d->next >= acl_d->aclp->acl_cnt) {
4398 +               return 0;
4399 +       }
4400 +
4401 +       *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
4402 +
4403 +       return 1;
4404 +}
4405 +
4406 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
4407 +{
4408 +       *type_p = entry_d->ae_tag;
4409 +
4410 +       return 0;
4411 +}
4412 +
4413 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
4414 +{
4415 +       *permset_p = entry_d;
4416 +
4417 +       return 0;
4418 +}
4419 +
4420 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
4421 +{
4422 +       if (entry_d->ae_tag != SMB_ACL_USER
4423 +           && entry_d->ae_tag != SMB_ACL_GROUP) {
4424 +               errno = EINVAL;
4425 +               return NULL;
4426 +       }
4427 +
4428 +       return &entry_d->ae_id;
4429 +}
4430 +
4431 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
4432 +{
4433 +       SMB_ACL_T       a;
4434 +
4435 +       if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
4436 +               errno = ENOMEM;
4437 +               return NULL;
4438 +       }
4439 +       if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
4440 +               SAFE_FREE(a);
4441 +               return NULL;
4442 +       }
4443 +       a->next = -1;
4444 +       a->freeaclp = True;
4445 +       return a;
4446 +}
4447 +
4448 +SMB_ACL_T sys_acl_get_fd(int fd)
4449 +{
4450 +       SMB_ACL_T       a;
4451 +
4452 +       if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
4453 +               errno = ENOMEM;
4454 +               return NULL;
4455 +       }
4456 +       if ((a->aclp = acl_get_fd(fd)) == NULL) {
4457 +               SAFE_FREE(a);
4458 +               return NULL;
4459 +       }
4460 +       a->next = -1;
4461 +       a->freeaclp = True;
4462 +       return a;
4463 +}
4464 +
4465 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
4466 +{
4467 +       permset_d->ae_perm = 0;
4468 +
4469 +       return 0;
4470 +}
4471 +
4472 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
4473 +{
4474 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
4475 +           && perm != SMB_ACL_EXECUTE) {
4476 +               errno = EINVAL;
4477 +               return -1;
4478 +       }
4479 +
4480 +       if (permset_d == NULL) {
4481 +               errno = EINVAL;
4482 +               return -1;
4483 +       }
4484 +
4485 +       permset_d->ae_perm |= perm;
4486 +
4487 +       return 0;
4488 +}
4489 +
4490 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
4491 +{
4492 +       return permset_d->ae_perm & perm;
4493 +}
4494 +
4495 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
4496 +{
4497 +       return acl_to_text(acl_d->aclp, len_p);
4498 +}
4499 +
4500 +SMB_ACL_T sys_acl_init(int count)
4501 +{
4502 +       SMB_ACL_T       a;
4503 +
4504 +       if (count < 0) {
4505 +               errno = EINVAL;
4506 +               return NULL;
4507 +       }
4508 +
4509 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
4510 +               errno = ENOMEM;
4511 +               return NULL;
4512 +       }
4513 +
4514 +       a->next = -1;
4515 +       a->freeaclp = False;
4516 +       a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
4517 +       a->aclp->acl_cnt = 0;
4518 +
4519 +       return a;
4520 +}
4521 +
4522 +
4523 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
4524 +{
4525 +       SMB_ACL_T       acl_d;
4526 +       SMB_ACL_ENTRY_T entry_d;
4527 +
4528 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
4529 +               errno = EINVAL;
4530 +               return -1;
4531 +       }
4532 +
4533 +       if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
4534 +               errno = ENOSPC;
4535 +               return -1;
4536 +       }
4537 +
4538 +       entry_d         = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
4539 +       entry_d->ae_tag = 0;
4540 +       entry_d->ae_id  = 0;
4541 +       entry_d->ae_perm        = 0;
4542 +       *entry_p        = entry_d;
4543 +
4544 +       return 0;
4545 +}
4546 +
4547 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
4548 +{
4549 +       switch (tag_type) {
4550 +               case SMB_ACL_USER:
4551 +               case SMB_ACL_USER_OBJ:
4552 +               case SMB_ACL_GROUP:
4553 +               case SMB_ACL_GROUP_OBJ:
4554 +               case SMB_ACL_OTHER:
4555 +               case SMB_ACL_MASK:
4556 +                       entry_d->ae_tag = tag_type;
4557 +                       break;
4558 +               default:
4559 +                       errno = EINVAL;
4560 +                       return -1;
4561 +       }
4562 +
4563 +       return 0;
4564 +}
4565 +
4566 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
4567 +{
4568 +       if (entry_d->ae_tag != SMB_ACL_GROUP
4569 +           && entry_d->ae_tag != SMB_ACL_USER) {
4570 +               errno = EINVAL;
4571 +               return -1;
4572 +       }
4573 +
4574 +       entry_d->ae_id = *((id_t *)qual_p);
4575 +
4576 +       return 0;
4577 +}
4578 +
4579 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
4580 +{
4581 +       if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
4582 +               return EINVAL;
4583 +       }
4584 +
4585 +       entry_d->ae_perm = permset_d->ae_perm;
4586 +
4587 +       return 0;
4588 +}
4589 +
4590 +int sys_acl_valid(SMB_ACL_T acl_d)
4591 +{
4592 +       return acl_valid(acl_d->aclp);
4593 +}
4594 +
4595 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
4596 +{
4597 +       return acl_set_file(name, type, acl_d->aclp);
4598 +}
4599 +
4600 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
4601 +{
4602 +       return acl_set_fd(fd, acl_d->aclp);
4603 +}
4604 +
4605 +int sys_acl_delete_def_file(const char *name)
4606 +{
4607 +       return acl_delete_def_file(name);
4608 +}
4609 +
4610 +int sys_acl_free_text(char *text)
4611 +{
4612 +       return acl_free(text);
4613 +}
4614 +
4615 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
4616 +{
4617 +       if (acl_d->freeaclp) {
4618 +               acl_free(acl_d->aclp);
4619 +       }
4620 +       acl_free(acl_d);
4621 +       return 0;
4622 +}
4623 +
4624 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4625 +{
4626 +       return 0;
4627 +}
4628 +
4629 +#elif defined(HAVE_AIX_ACLS)
4630 +
4631 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
4632 +
4633 +int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
4634 +{
4635 +       struct acl_entry_link *link;
4636 +       struct new_acl_entry *entry;
4637 +       int keep_going;
4638 +
4639 +       DEBUG(10,("This is the count: %d\n",theacl->count));
4640 +
4641 +       /* Check if count was previously set to -1. *
4642 +        * If it was, that means we reached the end *
4643 +        * of the acl last time.                    */
4644 +       if(theacl->count == -1)
4645 +               return(0);
4646 +
4647 +       link = theacl;
4648 +       /* To get to the next acl, traverse linked list until index *
4649 +        * of acl matches the count we are keeping.  This count is  *
4650 +        * incremented each time we return an acl entry.            */
4651 +
4652 +       for(keep_going = 0; keep_going < theacl->count; keep_going++)
4653 +               link = link->nextp;
4654 +
4655 +       entry = *entry_p =  link->entryp;
4656 +
4657 +       DEBUG(10,("*entry_p is %d\n",entry_p));
4658 +       DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
4659 +
4660 +       /* Increment count */
4661 +       theacl->count++;
4662 +       if(link->nextp == NULL)
4663 +               theacl->count = -1;
4664 +
4665 +       return(1);
4666 +}
4667 +
4668 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
4669 +{
4670 +       /* Initialize tag type */
4671 +
4672 +       *tag_type_p = -1;
4673 +       DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
4674 +
4675 +       /* Depending on what type of entry we have, *
4676 +        * return tag type.                         */
4677 +       switch(entry_d->ace_id->id_type) {
4678 +       case ACEID_USER:
4679 +               *tag_type_p = SMB_ACL_USER;
4680 +               break;
4681 +       case ACEID_GROUP:
4682 +               *tag_type_p = SMB_ACL_GROUP;
4683 +               break;
4684 +
4685 +       case SMB_ACL_USER_OBJ:
4686 +       case SMB_ACL_GROUP_OBJ:
4687 +       case SMB_ACL_OTHER:
4688 +               *tag_type_p = entry_d->ace_id->id_type;
4689 +               break;
4690
4691 +       default:
4692 +               return(-1);
4693 +       }
4694 +
4695 +       return(0);
4696 +}
4697 +
4698 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
4699 +{
4700 +       DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
4701 +       *permset_p = &entry_d->ace_access;
4702 +       DEBUG(10,("**permset_p is %d\n",**permset_p));
4703 +       if(!(**permset_p & S_IXUSR) &&
4704 +               !(**permset_p & S_IWUSR) &&
4705 +               !(**permset_p & S_IRUSR) &&
4706 +               (**permset_p != 0))
4707 +                       return(-1);
4708 +
4709 +       DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
4710 +       return(0);
4711 +}
4712 +
4713 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
4714 +{
4715 +       return(entry_d->ace_id->id_data);
4716 +}
4717 +
4718 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
4719 +{
4720 +       struct acl *file_acl = (struct acl *)NULL;
4721 +       struct acl_entry *acl_entry;
4722 +       struct new_acl_entry *new_acl_entry;
4723 +       struct ace_id *idp;
4724 +       struct acl_entry_link *acl_entry_link;
4725 +       struct acl_entry_link *acl_entry_link_head;
4726 +       int i;
4727 +       int rc = 0;
4728 +       uid_t user_id;
4729 +
4730 +       /* AIX has no DEFAULT */
4731 +       if  ( type == SMB_ACL_TYPE_DEFAULT ) {
4732 +               errno = ENOTSUP;
4733 +               return NULL;
4734 +       }
4735 +
4736 +       /* Get the acl using statacl */
4737
4738 +       DEBUG(10,("Entering sys_acl_get_file\n"));
4739 +       DEBUG(10,("path_p is %s\n",path_p));
4740 +
4741 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4742
4743 +       if(file_acl == NULL) {
4744 +               errno=ENOMEM;
4745 +               DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
4746 +               return(NULL);
4747 +       }
4748 +
4749 +       memset(file_acl,0,BUFSIZ);
4750 +
4751 +       rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
4752 +       if(rc == -1) {
4753 +               DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
4754 +               SAFE_FREE(file_acl);
4755 +               return(NULL);
4756 +       }
4757 +
4758 +       DEBUG(10,("Got facl and returned it\n"));
4759 +
4760 +       /* Point to the first acl entry in the acl */
4761 +       acl_entry =  file_acl->acl_ext;
4762 +
4763 +       /* Begin setting up the head of the linked list *
4764 +        * that will be used for the storing the acl    *
4765 +        * in a way that is useful for the posix_acls.c *
4766 +        * code.                                          */
4767 +
4768 +       acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4769 +       if(acl_entry_link_head == NULL)
4770 +               return(NULL);
4771 +
4772 +       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4773 +       if(acl_entry_link->entryp == NULL) {
4774 +               SAFE_FREE(file_acl);
4775 +               errno = ENOMEM;
4776 +               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4777 +               return(NULL);
4778 +       }
4779 +
4780 +       DEBUG(10,("acl_entry is %d\n",acl_entry));
4781 +       DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4782 +
4783 +       /* Check if the extended acl bit is on.   *
4784 +        * If it isn't, do not show the           *
4785 +        * contents of the acl since AIX intends *
4786 +        * the extended info to remain unused     */
4787 +
4788 +       if(file_acl->acl_mode & S_IXACL){
4789 +               /* while we are not pointing to the very end */
4790 +               while(acl_entry < acl_last(file_acl)) {
4791 +                       /* before we malloc anything, make sure this is  */
4792 +                       /* a valid acl entry and one that we want to map */
4793 +                       idp = id_nxt(acl_entry->ace_id);
4794 +                       if((acl_entry->ace_type == ACC_SPECIFY ||
4795 +                               (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4796 +                                       acl_entry = acl_nxt(acl_entry);
4797 +                                       continue;
4798 +                       }
4799 +
4800 +                       idp = acl_entry->ace_id;
4801 +
4802 +                       /* Check if this is the first entry in the linked list. *
4803 +                        * The first entry needs to keep prevp pointing to NULL *
4804 +                        * and already has entryp allocated.                  */
4805 +
4806 +                       if(acl_entry_link_head->count != 0) {
4807 +                               acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4808 +
4809 +                               if(acl_entry_link->nextp == NULL) {
4810 +                                       SAFE_FREE(file_acl);
4811 +                                       errno = ENOMEM;
4812 +                                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4813 +                                       return(NULL);
4814 +                               }
4815 +
4816 +                               acl_entry_link->nextp->prevp = acl_entry_link;
4817 +                               acl_entry_link = acl_entry_link->nextp;
4818 +                               acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4819 +                               if(acl_entry_link->entryp == NULL) {
4820 +                                       SAFE_FREE(file_acl);
4821 +                                       errno = ENOMEM;
4822 +                                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4823 +                                       return(NULL);
4824 +                               }
4825 +                               acl_entry_link->nextp = NULL;
4826 +                       }
4827 +
4828 +                       acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4829 +
4830 +                       /* Don't really need this since all types are going *
4831 +                        * to be specified but, it's better than leaving it 0 */
4832 +
4833 +                       acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4834
4835 +                       acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4836
4837 +                       memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
4838 +
4839 +                       /* The access in the acl entries must be left shifted by *
4840 +                        * three bites, because they will ultimately be compared *
4841 +                        * to S_IRUSR, S_IWUSR, and S_IXUSR.                  */
4842 +
4843 +                       switch(acl_entry->ace_type){
4844 +                       case ACC_PERMIT:
4845 +                       case ACC_SPECIFY:
4846 +                               acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4847 +                               acl_entry_link->entryp->ace_access <<= 6;
4848 +                               acl_entry_link_head->count++;
4849 +                               break;
4850 +                       case ACC_DENY:
4851 +                               /* Since there is no way to return a DENY acl entry *
4852 +                                * change to PERMIT and then shift.                 */
4853 +                               DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4854 +                               acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4855 +                               DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4856 +                               acl_entry_link->entryp->ace_access <<= 6;
4857 +                               acl_entry_link_head->count++;
4858 +                               break;
4859 +                       default:
4860 +                               return(0);
4861 +                       }
4862 +
4863 +                       DEBUG(10,("acl_entry = %d\n",acl_entry));
4864 +                       DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4865
4866 +                       acl_entry = acl_nxt(acl_entry);
4867 +               }
4868 +       } /* end of if enabled */
4869 +
4870 +       /* Since owner, group, other acl entries are not *
4871 +        * part of the acl entries in an acl, they must  *
4872 +        * be dummied up to become part of the list.     */
4873 +
4874 +       for( i = 1; i < 4; i++) {
4875 +               DEBUG(10,("i is %d\n",i));
4876 +               if(acl_entry_link_head->count != 0) {
4877 +                       acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4878 +                       if(acl_entry_link->nextp == NULL) {
4879 +                               SAFE_FREE(file_acl);
4880 +                               errno = ENOMEM;
4881 +                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4882 +                               return(NULL);
4883 +                       }
4884 +
4885 +                       acl_entry_link->nextp->prevp = acl_entry_link;
4886 +                       acl_entry_link = acl_entry_link->nextp;
4887 +                       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4888 +                       if(acl_entry_link->entryp == NULL) {
4889 +                               SAFE_FREE(file_acl);
4890 +                               errno = ENOMEM;
4891 +                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4892 +                               return(NULL);
4893 +                       }
4894 +               }
4895 +
4896 +               acl_entry_link->nextp = NULL;
4897 +
4898 +               new_acl_entry = acl_entry_link->entryp;
4899 +               idp = new_acl_entry->ace_id;
4900 +
4901 +               new_acl_entry->ace_len = sizeof(struct acl_entry);
4902 +               new_acl_entry->ace_type = ACC_PERMIT;
4903 +               idp->id_len = sizeof(struct ace_id);
4904 +               DEBUG(10,("idp->id_len = %d\n",idp->id_len));
4905 +               memset(idp->id_data,0,sizeof(uid_t));
4906 +
4907 +               switch(i) {
4908 +               case 2:
4909 +                       new_acl_entry->ace_access = file_acl->g_access << 6;
4910 +                       idp->id_type = SMB_ACL_GROUP_OBJ;
4911 +                       break;
4912 +
4913 +               case 3:
4914 +                       new_acl_entry->ace_access = file_acl->o_access << 6;
4915 +                       idp->id_type = SMB_ACL_OTHER;
4916 +                       break;
4917
4918 +               case 1:
4919 +                       new_acl_entry->ace_access = file_acl->u_access << 6;
4920 +                       idp->id_type = SMB_ACL_USER_OBJ;
4921 +                       break;
4922
4923 +               default:
4924 +                       return(NULL);
4925 +
4926 +               }
4927 +
4928 +               acl_entry_link_head->count++;
4929 +               DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4930 +       }
4931 +
4932 +       acl_entry_link_head->count = 0;
4933 +       SAFE_FREE(file_acl);
4934 +
4935 +       return(acl_entry_link_head);
4936 +}
4937 +
4938 +SMB_ACL_T sys_acl_get_fd(int fd)
4939 +{
4940 +       struct acl *file_acl = (struct acl *)NULL;
4941 +       struct acl_entry *acl_entry;
4942 +       struct new_acl_entry *new_acl_entry;
4943 +       struct ace_id *idp;
4944 +       struct acl_entry_link *acl_entry_link;
4945 +       struct acl_entry_link *acl_entry_link_head;
4946 +       int i;
4947 +       int rc = 0;
4948 +       uid_t user_id;
4949 +
4950 +       /* Get the acl using fstatacl */
4951 +   
4952 +       DEBUG(10,("Entering sys_acl_get_fd\n"));
4953 +       DEBUG(10,("fd is %d\n",fd));
4954 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4955 +
4956 +       if(file_acl == NULL) {
4957 +               errno=ENOMEM;
4958 +               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4959 +               return(NULL);
4960 +       }
4961 +
4962 +       memset(file_acl,0,BUFSIZ);
4963 +
4964 +       rc = fstatacl(fd,0,file_acl,BUFSIZ);
4965 +       if(rc == -1) {
4966 +               DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
4967 +               SAFE_FREE(file_acl);
4968 +               return(NULL);
4969 +       }
4970 +
4971 +       DEBUG(10,("Got facl and returned it\n"));
4972 +
4973 +       /* Point to the first acl entry in the acl */
4974 +
4975 +       acl_entry =  file_acl->acl_ext;
4976 +       /* Begin setting up the head of the linked list *
4977 +        * that will be used for the storing the acl    *
4978 +        * in a way that is useful for the posix_acls.c *
4979 +        * code.                                        */
4980 +
4981 +       acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4982 +       if(acl_entry_link_head == NULL){
4983 +               SAFE_FREE(file_acl);
4984 +               return(NULL);
4985 +       }
4986 +
4987 +       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4988 +
4989 +       if(acl_entry_link->entryp == NULL) {
4990 +               errno = ENOMEM;
4991 +               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4992 +               SAFE_FREE(file_acl);
4993 +               return(NULL);
4994 +       }
4995 +
4996 +       DEBUG(10,("acl_entry is %d\n",acl_entry));
4997 +       DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4998
4999 +       /* Check if the extended acl bit is on.   *
5000 +        * If it isn't, do not show the           *
5001 +        * contents of the acl since AIX intends  *
5002 +        * the extended info to remain unused     */
5003
5004 +       if(file_acl->acl_mode & S_IXACL){
5005 +               /* while we are not pointing to the very end */
5006 +               while(acl_entry < acl_last(file_acl)) {
5007 +                       /* before we malloc anything, make sure this is  */
5008 +                       /* a valid acl entry and one that we want to map */
5009 +
5010 +                       idp = id_nxt(acl_entry->ace_id);
5011 +                       if((acl_entry->ace_type == ACC_SPECIFY ||
5012 +                               (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
5013 +                                       acl_entry = acl_nxt(acl_entry);
5014 +                                       continue;
5015 +                       }
5016 +
5017 +                       idp = acl_entry->ace_id;
5018
5019 +                       /* Check if this is the first entry in the linked list. *
5020 +                        * The first entry needs to keep prevp pointing to NULL *
5021 +                        * and already has entryp allocated.                 */
5022 +
5023 +                       if(acl_entry_link_head->count != 0) {
5024 +                               acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
5025 +                               if(acl_entry_link->nextp == NULL) {
5026 +                                       errno = ENOMEM;
5027 +                                       DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
5028 +                                       SAFE_FREE(file_acl);
5029 +                                       return(NULL);
5030 +                               }
5031 +                               acl_entry_link->nextp->prevp = acl_entry_link;
5032 +                               acl_entry_link = acl_entry_link->nextp;
5033 +                               acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
5034 +                               if(acl_entry_link->entryp == NULL) {
5035 +                                       errno = ENOMEM;
5036 +                                       DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
5037 +                                       SAFE_FREE(file_acl);
5038 +                                       return(NULL);
5039 +                               }
5040 +
5041 +                               acl_entry_link->nextp = NULL;
5042 +                       }
5043 +
5044 +                       acl_entry_link->entryp->ace_len = acl_entry->ace_len;
5045 +
5046 +                       /* Don't really need this since all types are going *
5047 +                        * to be specified but, it's better than leaving it 0 */
5048 +
5049 +                       acl_entry_link->entryp->ace_type = acl_entry->ace_type;
5050 +                       acl_entry_link->entryp->ace_access = acl_entry->ace_access;
5051 +
5052 +                       memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
5053 +
5054 +                       /* The access in the acl entries must be left shifted by *
5055 +                        * three bites, because they will ultimately be compared *
5056 +                        * to S_IRUSR, S_IWUSR, and S_IXUSR.                  */
5057 +
5058 +                       switch(acl_entry->ace_type){
5059 +                       case ACC_PERMIT:
5060 +                       case ACC_SPECIFY:
5061 +                               acl_entry_link->entryp->ace_access = acl_entry->ace_access;
5062 +                               acl_entry_link->entryp->ace_access <<= 6;
5063 +                               acl_entry_link_head->count++;
5064 +                               break;
5065 +                       case ACC_DENY:
5066 +                               /* Since there is no way to return a DENY acl entry *
5067 +                                * change to PERMIT and then shift.                 */
5068 +                               DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
5069 +                               acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
5070 +                               DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
5071 +                               acl_entry_link->entryp->ace_access <<= 6;
5072 +                               acl_entry_link_head->count++;
5073 +                               break;
5074 +                       default:
5075 +                               return(0);
5076 +                       }
5077 +
5078 +                       DEBUG(10,("acl_entry = %d\n",acl_entry));
5079 +                       DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
5080
5081 +                       acl_entry = acl_nxt(acl_entry);
5082 +               }
5083 +       } /* end of if enabled */
5084 +
5085 +       /* Since owner, group, other acl entries are not *
5086 +        * part of the acl entries in an acl, they must  *
5087 +        * be dummied up to become part of the list.     */
5088 +
5089 +       for( i = 1; i < 4; i++) {
5090 +               DEBUG(10,("i is %d\n",i));
5091 +               if(acl_entry_link_head->count != 0){
5092 +                       acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
5093 +                       if(acl_entry_link->nextp == NULL) {
5094 +                               errno = ENOMEM;
5095 +                               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
5096 +                               SAFE_FREE(file_acl);
5097 +                               return(NULL);
5098 +                       }
5099 +
5100 +                       acl_entry_link->nextp->prevp = acl_entry_link;
5101 +                       acl_entry_link = acl_entry_link->nextp;
5102 +                       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
5103 +
5104 +                       if(acl_entry_link->entryp == NULL) {
5105 +                               SAFE_FREE(file_acl);
5106 +                               errno = ENOMEM;
5107 +                               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
5108 +                               return(NULL);
5109 +                       }
5110 +               }
5111 +
5112 +               acl_entry_link->nextp = NULL;
5113
5114 +               new_acl_entry = acl_entry_link->entryp;
5115 +               idp = new_acl_entry->ace_id;
5116
5117 +               new_acl_entry->ace_len = sizeof(struct acl_entry);
5118 +               new_acl_entry->ace_type = ACC_PERMIT;
5119 +               idp->id_len = sizeof(struct ace_id);
5120 +               DEBUG(10,("idp->id_len = %d\n",idp->id_len));
5121 +               memset(idp->id_data,0,sizeof(uid_t));
5122
5123 +               switch(i) {
5124 +               case 2:
5125 +                       new_acl_entry->ace_access = file_acl->g_access << 6;
5126 +                       idp->id_type = SMB_ACL_GROUP_OBJ;
5127 +                       break;
5128
5129 +               case 3:
5130 +                       new_acl_entry->ace_access = file_acl->o_access << 6;
5131 +                       idp->id_type = SMB_ACL_OTHER;
5132 +                       break;
5133
5134 +               case 1:
5135 +                       new_acl_entry->ace_access = file_acl->u_access << 6;
5136 +                       idp->id_type = SMB_ACL_USER_OBJ;
5137 +                       break;
5138
5139 +               default:
5140 +                       return(NULL);
5141 +               }
5142
5143 +               acl_entry_link_head->count++;
5144 +               DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
5145 +       }
5146 +
5147 +       acl_entry_link_head->count = 0;
5148 +       SAFE_FREE(file_acl);
5149
5150 +       return(acl_entry_link_head);
5151 +}
5152 +
5153 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
5154 +{
5155 +       *permset = *permset & ~0777;
5156 +       return(0);
5157 +}
5158 +
5159 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
5160 +{
5161 +       if((perm != 0) &&
5162 +                       (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
5163 +               return(-1);
5164 +
5165 +       *permset |= perm;
5166 +       DEBUG(10,("This is the permset now: %d\n",*permset));
5167 +       return(0);
5168 +}
5169 +
5170 +char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
5171 +{
5172 +       return(NULL);
5173 +}
5174 +
5175 +SMB_ACL_T sys_acl_init( int count)
5176 +{
5177 +       struct acl_entry_link *theacl = NULL;
5178
5179 +       DEBUG(10,("Entering sys_acl_init\n"));
5180 +
5181 +       theacl = SMB_MALLOC_P(struct acl_entry_link);
5182 +       if(theacl == NULL) {
5183 +               errno = ENOMEM;
5184 +               DEBUG(0,("Error in sys_acl_init is %d\n",errno));
5185 +               return(NULL);
5186 +       }
5187 +
5188 +       theacl->count = 0;
5189 +       theacl->nextp = NULL;
5190 +       theacl->prevp = NULL;
5191 +       theacl->entryp = NULL;
5192 +       DEBUG(10,("Exiting sys_acl_init\n"));
5193 +       return(theacl);
5194 +}
5195 +
5196 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
5197 +{
5198 +       struct acl_entry_link *theacl;
5199 +       struct acl_entry_link *acl_entryp;
5200 +       struct acl_entry_link *temp_entry;
5201 +       int counting;
5202 +
5203 +       DEBUG(10,("Entering the sys_acl_create_entry\n"));
5204 +
5205 +       theacl = acl_entryp = *pacl;
5206 +
5207 +       /* Get to the end of the acl before adding entry */
5208 +
5209 +       for(counting=0; counting < theacl->count; counting++){
5210 +               DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
5211 +               temp_entry = acl_entryp;
5212 +               acl_entryp = acl_entryp->nextp;
5213 +       }
5214 +
5215 +       if(theacl->count != 0){
5216 +               temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
5217 +               if(acl_entryp == NULL) {
5218 +                       errno = ENOMEM;
5219 +                       DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
5220 +                       return(-1);
5221 +               }
5222 +
5223 +               DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
5224 +               acl_entryp->prevp = temp_entry;
5225 +               DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
5226 +       }
5227 +
5228 +       *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
5229 +       if(*pentry == NULL) {
5230 +               errno = ENOMEM;
5231 +               DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
5232 +               return(-1);
5233 +       }
5234 +
5235 +       memset(*pentry,0,sizeof(struct new_acl_entry));
5236 +       acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
5237 +       acl_entryp->entryp->ace_type = ACC_PERMIT;
5238 +       acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
5239 +       acl_entryp->nextp = NULL;
5240 +       theacl->count++;
5241 +       DEBUG(10,("Exiting sys_acl_create_entry\n"));
5242 +       return(0);
5243 +}
5244 +
5245 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
5246 +{
5247 +       DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
5248 +       entry->ace_id->id_type = tagtype;
5249 +       DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
5250 +       DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
5251 +}
5252 +
5253 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
5254 +{
5255 +       DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
5256 +       memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
5257 +       DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
5258 +       return(0);
5259 +}
5260 +
5261 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
5262 +{
5263 +       DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
5264 +       if(!(*permset & S_IXUSR) &&
5265 +               !(*permset & S_IWUSR) &&
5266 +               !(*permset & S_IRUSR) &&
5267 +               (*permset != 0))
5268 +                       return(-1);
5269 +
5270 +       entry->ace_access = *permset;
5271 +       DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
5272 +       DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
5273 +       return(0);
5274 +}
5275 +
5276 +int sys_acl_valid( SMB_ACL_T theacl )
5277 +{
5278 +       int user_obj = 0;
5279 +       int group_obj = 0;
5280 +       int other_obj = 0;
5281 +       struct acl_entry_link *acl_entry;
5282 +
5283 +       for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
5284 +               user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
5285 +               group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
5286 +               other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
5287 +       }
5288 +
5289 +       DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
5290
5291 +       if(user_obj != 1 || group_obj != 1 || other_obj != 1)
5292 +               return(-1); 
5293 +
5294 +       return(0);
5295 +}
5296 +
5297 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
5298 +{
5299 +       struct acl_entry_link *acl_entry_link = NULL;
5300 +       struct acl *file_acl = NULL;
5301 +       struct acl *file_acl_temp = NULL;
5302 +       struct acl_entry *acl_entry = NULL;
5303 +       struct ace_id *ace_id = NULL;
5304 +       uint id_type;
5305 +       uint ace_access;
5306 +       uint user_id;
5307 +       uint acl_length;
5308 +       uint rc;
5309 +
5310 +       DEBUG(10,("Entering sys_acl_set_file\n"));
5311 +       DEBUG(10,("File name is %s\n",name));
5312
5313 +       /* AIX has no default ACL */
5314 +       if(acltype == SMB_ACL_TYPE_DEFAULT)
5315 +               return(0);
5316 +
5317 +       acl_length = BUFSIZ;
5318 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
5319 +
5320 +       if(file_acl == NULL) {
5321 +               errno = ENOMEM;
5322 +               DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
5323 +               return(-1);
5324 +       }
5325 +
5326 +       memset(file_acl,0,BUFSIZ);
5327 +
5328 +       file_acl->acl_len = ACL_SIZ;
5329 +       file_acl->acl_mode = S_IXACL;
5330 +
5331 +       for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
5332 +               acl_entry_link->entryp->ace_access >>= 6;
5333 +               id_type = acl_entry_link->entryp->ace_id->id_type;
5334 +
5335 +               switch(id_type) {
5336 +               case SMB_ACL_USER_OBJ:
5337 +                       file_acl->u_access = acl_entry_link->entryp->ace_access;
5338 +                       continue;
5339 +               case SMB_ACL_GROUP_OBJ:
5340 +                       file_acl->g_access = acl_entry_link->entryp->ace_access;
5341 +                       continue;
5342 +               case SMB_ACL_OTHER:
5343 +                       file_acl->o_access = acl_entry_link->entryp->ace_access;
5344 +                       continue;
5345 +               case SMB_ACL_MASK:
5346 +                       continue;
5347 +               }
5348 +
5349 +               if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
5350 +                       acl_length += sizeof(struct acl_entry);
5351 +                       file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
5352 +                       if(file_acl_temp == NULL) {
5353 +                               SAFE_FREE(file_acl);
5354 +                               errno = ENOMEM;
5355 +                               DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
5356 +                               return(-1);
5357 +                       }  
5358 +
5359 +                       memcpy(file_acl_temp,file_acl,file_acl->acl_len);
5360 +                       SAFE_FREE(file_acl);
5361 +                       file_acl = file_acl_temp;
5362 +               }
5363 +
5364 +               acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
5365 +               file_acl->acl_len += sizeof(struct acl_entry);
5366 +               acl_entry->ace_len = acl_entry_link->entryp->ace_len;
5367 +               acl_entry->ace_access = acl_entry_link->entryp->ace_access;
5368
5369 +               /* In order to use this, we'll need to wait until we can get denies */
5370 +               /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
5371 +               acl_entry->ace_type = ACC_SPECIFY; */
5372 +
5373 +               acl_entry->ace_type = ACC_SPECIFY;
5374
5375 +               ace_id = acl_entry->ace_id;
5376
5377 +               ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
5378 +               DEBUG(10,("The id type is %d\n",ace_id->id_type));
5379 +               ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
5380 +               memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
5381 +               memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
5382 +       }
5383 +
5384 +       rc = chacl(name,file_acl,file_acl->acl_len);
5385 +       DEBUG(10,("errno is %d\n",errno));
5386 +       DEBUG(10,("return code is %d\n",rc));
5387 +       SAFE_FREE(file_acl);
5388 +       DEBUG(10,("Exiting the sys_acl_set_file\n"));
5389 +       return(rc);
5390 +}
5391 +
5392 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
5393 +{
5394 +       struct acl_entry_link *acl_entry_link = NULL;
5395 +       struct acl *file_acl = NULL;
5396 +       struct acl *file_acl_temp = NULL;
5397 +       struct acl_entry *acl_entry = NULL;
5398 +       struct ace_id *ace_id = NULL;
5399 +       uint id_type;
5400 +       uint user_id;
5401 +       uint acl_length;
5402 +       uint rc;
5403
5404 +       DEBUG(10,("Entering sys_acl_set_fd\n"));
5405 +       acl_length = BUFSIZ;
5406 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
5407 +
5408 +       if(file_acl == NULL) {
5409 +               errno = ENOMEM;
5410 +               DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
5411 +               return(-1);
5412 +       }
5413 +
5414 +       memset(file_acl,0,BUFSIZ);
5415
5416 +       file_acl->acl_len = ACL_SIZ;
5417 +       file_acl->acl_mode = S_IXACL;
5418 +
5419 +       for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
5420 +               acl_entry_link->entryp->ace_access >>= 6;
5421 +               id_type = acl_entry_link->entryp->ace_id->id_type;
5422 +               DEBUG(10,("The id_type is %d\n",id_type));
5423 +
5424 +               switch(id_type) {
5425 +               case SMB_ACL_USER_OBJ:
5426 +                       file_acl->u_access = acl_entry_link->entryp->ace_access;
5427 +                       continue;
5428 +               case SMB_ACL_GROUP_OBJ:
5429 +                       file_acl->g_access = acl_entry_link->entryp->ace_access;
5430 +                       continue;
5431 +               case SMB_ACL_OTHER:
5432 +                       file_acl->o_access = acl_entry_link->entryp->ace_access;
5433 +                       continue;
5434 +               case SMB_ACL_MASK:
5435 +                       continue;
5436 +               }
5437 +
5438 +               if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
5439 +                       acl_length += sizeof(struct acl_entry);
5440 +                       file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
5441 +                       if(file_acl_temp == NULL) {
5442 +                               SAFE_FREE(file_acl);
5443 +                               errno = ENOMEM;
5444 +                               DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
5445 +                               return(-1);
5446 +                       }
5447 +
5448 +                       memcpy(file_acl_temp,file_acl,file_acl->acl_len);
5449 +                       SAFE_FREE(file_acl);
5450 +                       file_acl = file_acl_temp;
5451 +               }
5452 +
5453 +               acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
5454 +               file_acl->acl_len += sizeof(struct acl_entry);
5455 +               acl_entry->ace_len = acl_entry_link->entryp->ace_len;
5456 +               acl_entry->ace_access = acl_entry_link->entryp->ace_access;
5457
5458 +               /* In order to use this, we'll need to wait until we can get denies */
5459 +               /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
5460 +                       acl_entry->ace_type = ACC_SPECIFY; */
5461
5462 +               acl_entry->ace_type = ACC_SPECIFY;
5463
5464 +               ace_id = acl_entry->ace_id;
5465
5466 +               ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
5467 +               DEBUG(10,("The id type is %d\n",ace_id->id_type));
5468 +               ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
5469 +               memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
5470 +               memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
5471 +       }
5472
5473 +       rc = fchacl(fd,file_acl,file_acl->acl_len);
5474 +       DEBUG(10,("errno is %d\n",errno));
5475 +       DEBUG(10,("return code is %d\n",rc));
5476 +       SAFE_FREE(file_acl);
5477 +       DEBUG(10,("Exiting sys_acl_set_fd\n"));
5478 +       return(rc);
5479 +}
5480 +
5481 +int sys_acl_delete_def_file(const char *name)
5482 +{
5483 +       /* AIX has no default ACL */
5484 +       return 0;
5485 +}
5486 +
5487 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
5488 +{
5489 +       return(*permset & perm);
5490 +}
5491 +
5492 +int sys_acl_free_text(char *text)
5493 +{
5494 +       return(0);
5495 +}
5496 +
5497 +int sys_acl_free_acl(SMB_ACL_T posix_acl)
5498 +{
5499 +       struct acl_entry_link *acl_entry_link;
5500 +
5501 +       for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
5502 +               SAFE_FREE(acl_entry_link->prevp->entryp);
5503 +               SAFE_FREE(acl_entry_link->prevp);
5504 +       }
5505 +
5506 +       SAFE_FREE(acl_entry_link->prevp->entryp);
5507 +       SAFE_FREE(acl_entry_link->prevp);
5508 +       SAFE_FREE(acl_entry_link->entryp);
5509 +       SAFE_FREE(acl_entry_link);
5510
5511 +       return(0);
5512 +}
5513 +
5514 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
5515 +{
5516 +       return(0);
5517 +}
5518 +
5519 +#else /* No ACLs. */
5520 +
5521 +int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
5522 +{
5523 +       errno = ENOSYS;
5524 +       return -1;
5525 +}
5526 +
5527 +int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
5528 +{
5529 +       errno = ENOSYS;
5530 +       return -1;
5531 +}
5532 +
5533 +int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
5534 +{
5535 +       errno = ENOSYS;
5536 +       return -1;
5537 +}
5538 +
5539 +void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
5540 +{
5541 +       errno = ENOSYS;
5542 +       return NULL;
5543 +}
5544 +
5545 +SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
5546 +{
5547 +       errno = ENOSYS;
5548 +       return (SMB_ACL_T)NULL;
5549 +}
5550 +
5551 +SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
5552 +{
5553 +       errno = ENOSYS;
5554 +       return (SMB_ACL_T)NULL;
5555 +}
5556 +
5557 +int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
5558 +{
5559 +       errno = ENOSYS;
5560 +       return -1;
5561 +}
5562 +
5563 +int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
5564 +{
5565 +       errno = ENOSYS;
5566 +       return -1;
5567 +}
5568 +
5569 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
5570 +{
5571 +       errno = ENOSYS;
5572 +       return (permset & perm) ? 1 : 0;
5573 +}
5574 +
5575 +char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
5576 +{
5577 +       errno = ENOSYS;
5578 +       return NULL;
5579 +}
5580 +
5581 +int sys_acl_free_text(UNUSED(char *text))
5582 +{
5583 +       errno = ENOSYS;
5584 +       return -1;
5585 +}
5586 +
5587 +SMB_ACL_T sys_acl_init(UNUSED(int count))
5588 +{
5589 +       errno = ENOSYS;
5590 +       return NULL;
5591 +}
5592 +
5593 +int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
5594 +{
5595 +       errno = ENOSYS;
5596 +       return -1;
5597 +}
5598 +
5599 +int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
5600 +{
5601 +       errno = ENOSYS;
5602 +       return -1;
5603 +}
5604 +
5605 +int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
5606 +{
5607 +       errno = ENOSYS;
5608 +       return -1;
5609 +}
5610 +
5611 +int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
5612 +{
5613 +       errno = ENOSYS;
5614 +       return -1;
5615 +}
5616 +
5617 +int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
5618 +{
5619 +       errno = ENOSYS;
5620 +       return -1;
5621 +}
5622 +
5623 +int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
5624 +{
5625 +       errno = ENOSYS;
5626 +       return -1;
5627 +}
5628 +
5629 +int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
5630 +{
5631 +       errno = ENOSYS;
5632 +       return -1;
5633 +}
5634 +
5635 +int sys_acl_delete_def_file(UNUSED(const char *name))
5636 +{
5637 +       errno = ENOSYS;
5638 +       return -1;
5639 +}
5640 +
5641 +int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
5642 +{
5643 +       errno = ENOSYS;
5644 +       return -1;
5645 +}
5646 +
5647 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
5648 +{
5649 +       errno = ENOSYS;
5650 +       return -1;
5651 +}
5652 +
5653 +#endif /* No ACLs. */
5654 +
5655 +/************************************************************************
5656 + Deliberately outside the ACL defines. Return 1 if this is a "no acls"
5657 + errno, 0 if not.
5658 +************************************************************************/
5659 +
5660 +int no_acl_syscall_error(int err)
5661 +{
5662 +#if defined(ENOSYS)
5663 +       if (err == ENOSYS) {
5664 +               return 1;
5665 +       }
5666 +#endif
5667 +#if defined(ENOTSUP)
5668 +       if (err == ENOTSUP) {
5669 +               return 1;
5670 +       }
5671 +#endif
5672 +       return 0;
5673 +}
5674 +
5675 +#endif /* SUPPORT_ACLS */
5676 --- old/lib/sysacls.h
5677 +++ new/lib/sysacls.h
5678 @@ -0,0 +1,40 @@
5679 +#ifdef SUPPORT_ACLS
5680 +
5681 +#ifdef HAVE_SYS_ACL_H
5682 +#include <sys/acl.h>
5683 +#endif
5684 +#ifdef HAVE_ACL_LIBACL_H
5685 +#include <acl/libacl.h>
5686 +#endif
5687 +#include "smb_acls.h"
5688 +
5689 +#define SMB_MALLOC(cnt) new_array(char, cnt)
5690 +#define SMB_MALLOC_P(obj) new_array(obj, 1)
5691 +#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
5692 +#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
5693 +#define slprintf snprintf
5694 +
5695 +int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
5696 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
5697 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
5698 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
5699 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
5700 +SMB_ACL_T sys_acl_get_fd(int fd);
5701 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
5702 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
5703 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
5704 +char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
5705 +SMB_ACL_T sys_acl_init(int count);
5706 +int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
5707 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
5708 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
5709 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
5710 +int sys_acl_valid(SMB_ACL_T theacl);
5711 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
5712 +int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
5713 +int sys_acl_delete_def_file(const char *name);
5714 +int sys_acl_free_text(char *text);
5715 +int sys_acl_free_acl(SMB_ACL_T the_acl);
5716 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
5717 +
5718 +#endif /* SUPPORT_ACLS */
5719 --- old/log.c
5720 +++ new/log.c
5721 @@ -624,8 +624,10 @@ static void log_formatted(enum logcode c
5722                         c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
5723                         c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
5724                         c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
5725 -                       c[8] = '.';
5726 -                       c[9] = '\0';
5727 +                       c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
5728 +                       c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
5729 +                       c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
5730 +                       c[11] = '\0';
5731  
5732                         if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
5733                                 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
5734 --- old/options.c
5735 +++ new/options.c
5736 @@ -46,6 +46,7 @@ int copy_dirlinks = 0;
5737  int copy_links = 0;
5738  int preserve_links = 0;
5739  int preserve_hard_links = 0;
5740 +int preserve_acls = 0;
5741  int preserve_perms = 0;
5742  int preserve_executability = 0;
5743  int preserve_devices = 0;
5744 @@ -198,6 +199,7 @@ static void print_rsync_version(enum log
5745         char const *got_socketpair = "no ";
5746         char const *have_inplace = "no ";
5747         char const *hardlinks = "no ";
5748 +       char const *acls = "no ";
5749         char const *links = "no ";
5750         char const *ipv6 = "no ";
5751         STRUCT_STAT *dumstat;
5752 @@ -214,6 +216,10 @@ static void print_rsync_version(enum log
5753         hardlinks = "";
5754  #endif
5755  
5756 +#ifdef SUPPORT_ACLS
5757 +       acls = "";
5758 +#endif
5759 +
5760  #ifdef SUPPORT_LINKS
5761         links = "";
5762  #endif
5763 @@ -232,8 +238,8 @@ static void print_rsync_version(enum log
5764                 (int)(sizeof (int64) * 8));
5765         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
5766                 got_socketpair, hardlinks, links, ipv6, have_inplace);
5767 -       rprintf(f, "    %sappend\n",
5768 -               have_inplace);
5769 +       rprintf(f, "    %sappend, %sACLs\n",
5770 +               have_inplace, acls);
5771  
5772  #ifdef MAINTAINER_MODE
5773         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
5774 @@ -279,7 +285,7 @@ void usage(enum logcode F)
5775    rprintf(F," -q, --quiet                 suppress non-error messages\n");
5776    rprintf(F,"     --no-motd               suppress daemon-mode MOTD (see manpage caveat)\n");
5777    rprintf(F," -c, --checksum              skip based on checksum, not mod-time & size\n");
5778 -  rprintf(F," -a, --archive               archive mode; same as -rlptgoD (no -H)\n");
5779 +  rprintf(F," -a, --archive               archive mode; same as -rlptgoD (no -H, -A)\n");
5780    rprintf(F,"     --no-OPTION             turn off an implied OPTION (e.g. --no-D)\n");
5781    rprintf(F," -r, --recursive             recurse into directories\n");
5782    rprintf(F," -R, --relative              use relative path names\n");
5783 @@ -301,6 +307,9 @@ void usage(enum logcode F)
5784    rprintf(F," -p, --perms                 preserve permissions\n");
5785    rprintf(F," -E, --executability         preserve the file's executability\n");
5786    rprintf(F,"     --chmod=CHMOD           affect file and/or directory permissions\n");
5787 +#ifdef SUPPORT_ACLS
5788 +  rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
5789 +#endif
5790    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
5791    rprintf(F," -g, --group                 preserve group\n");
5792    rprintf(F,"     --devices               preserve device files (super-user only)\n");
5793 @@ -421,6 +430,9 @@ static struct poptOption long_options[] 
5794    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
5795    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
5796    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
5797 +  {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
5798 +  {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
5799 +  {"no-A",             0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
5800    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
5801    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
5802    {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
5803 @@ -1092,6 +1104,24 @@ int parse_arguments(int *argc, const cha
5804                         usage(FINFO);
5805                         exit_cleanup(0);
5806  
5807 +               case 'A':
5808 +#ifdef SUPPORT_ACLS
5809 +                       preserve_acls = 1;
5810 +                       preserve_perms = 1;
5811 +                       break;
5812 +#else
5813 +                       /* FIXME: this should probably be ignored with a
5814 +                        * warning and then countermeasures taken to
5815 +                        * restrict group and other access in the presence
5816 +                        * of any more restrictive ACLs, but this is safe
5817 +                        * for now */
5818 +                       snprintf(err_buf,sizeof(err_buf),
5819 +                                 "ACLs are not supported on this %s\n",
5820 +                                am_server ? "server" : "client");
5821 +                       return 0;
5822 +#endif
5823 +
5824 +
5825                 default:
5826                         /* A large opt value means that set_refuse_options()
5827                          * turned this option off. */
5828 @@ -1555,6 +1585,10 @@ void server_options(char **args,int *arg
5829                 argstr[x++] = 'p';
5830         else if (preserve_executability && am_sender)
5831                 argstr[x++] = 'E';
5832 +#ifdef SUPPORT_ACLS
5833 +       if (preserve_acls)
5834 +               argstr[x++] = 'A';
5835 +#endif
5836         if (recurse)
5837                 argstr[x++] = 'r';
5838         if (always_checksum)
5839 --- old/receiver.c
5840 +++ new/receiver.c
5841 @@ -47,6 +47,7 @@ extern int keep_partial;
5842  extern int checksum_seed;
5843  extern int inplace;
5844  extern int delay_updates;
5845 +extern mode_t orig_umask;
5846  extern struct stats stats;
5847  extern char *tmpdir;
5848  extern char *partial_dir;
5849 @@ -347,6 +348,10 @@ int recv_files(int f_in, char *local_nam
5850         int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
5851         enum logcode log_code = log_before_transfer ? FLOG : FINFO;
5852         int max_phase = protocol_version >= 29 ? 2 : 1;
5853 +       int dflt_perms = (ACCESSPERMS & ~orig_umask);
5854 +#ifdef SUPPORT_ACLS
5855 +       const char *parent_dirname = "";
5856 +#endif
5857         int ndx, recv_ok;
5858  
5859         if (verbose > 2)
5860 @@ -562,7 +567,16 @@ int recv_files(int f_in, char *local_nam
5861                  * mode based on the local permissions and some heuristics. */
5862                 if (!preserve_perms) {
5863                         int exists = fd1 != -1;
5864 -                       file->mode = dest_mode(file->mode, st.st_mode, exists);
5865 +#ifdef SUPPORT_ACLS
5866 +                       const char *dn = file->dirname ? file->dirname : ".";
5867 +                       if (parent_dirname != dn
5868 +                        && strcmp(parent_dirname, dn) != 0) {
5869 +                               dflt_perms = default_perms_for_dir(dn);
5870 +                               parent_dirname = dn;
5871 +                       }
5872 +#endif
5873 +                       file->mode = dest_mode(file->mode, st.st_mode,
5874 +                                              dflt_perms, exists);
5875                 }
5876  
5877                 /* We now check to see if we are writing the file "inplace" */
5878 --- old/rsync.c
5879 +++ new/rsync.c
5880 @@ -31,6 +31,7 @@
5881  
5882  extern int verbose;
5883  extern int dry_run;
5884 +extern int preserve_acls;
5885  extern int preserve_perms;
5886  extern int preserve_executability;
5887  extern int preserve_times;
5888 @@ -49,7 +50,6 @@ extern int inplace;
5889  extern int flist_eof;
5890  extern int keep_dirlinks;
5891  extern int make_backups;
5892 -extern mode_t orig_umask;
5893  extern struct file_list *cur_flist, *first_flist, *dir_flist;
5894  extern struct chmod_mode_struct *daemon_chmod_modes;
5895  
5896 @@ -203,7 +203,8 @@ void free_sums(struct sum_struct *s)
5897  
5898  /* This is only called when we aren't preserving permissions.  Figure out what
5899   * the permissions should be and return them merged back into the mode. */
5900 -mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int exists)
5901 +mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
5902 +                int exists)
5903  {
5904         int new_mode;
5905         /* If the file already exists, we'll return the local permissions,
5906 @@ -220,56 +221,65 @@ mode_t dest_mode(mode_t flist_mode, mode
5907                                 new_mode |= (new_mode & 0444) >> 2;
5908                 }
5909         } else {
5910 -               /* Apply the umask and turn off special permissions. */
5911 -               new_mode = flist_mode & (~CHMOD_BITS | (ACCESSPERMS & ~orig_umask));
5912 +               /* Apply destination default permissions and turn
5913 +                * off special permissions. */
5914 +               new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
5915         }
5916         return new_mode;
5917  }
5918  
5919 -int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
5920 +int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
5921                    int flags)
5922  {
5923         int updated = 0;
5924 -       STRUCT_STAT st2;
5925 +       statx sx2;
5926         int change_uid, change_gid;
5927         mode_t new_mode = file->mode;
5928  
5929 -       if (!st) {
5930 +       if (!sxp) {
5931                 if (dry_run)
5932                         return 1;
5933 -               if (link_stat(fname, &st2, 0) < 0) {
5934 +               if (link_stat(fname, &sx2.st, 0) < 0) {
5935                         rsyserr(FERROR, errno, "stat %s failed",
5936                                 full_fname(fname));
5937                         return 0;
5938                 }
5939 -               st = &st2;
5940 +#ifdef SUPPORT_ACLS
5941 +               sx2.acc_acl = sx2.def_acl = NULL;
5942 +#endif
5943                 if (!preserve_perms && S_ISDIR(new_mode)
5944 -                && st->st_mode & S_ISGID) {
5945 +                && sx2.st.st_mode & S_ISGID) {
5946                         /* We just created this directory and its setgid
5947                          * bit is on, so make sure it stays on. */
5948                         new_mode |= S_ISGID;
5949                 }
5950 +               sxp = &sx2;
5951         }
5952  
5953 -       if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times))
5954 +#ifdef SUPPORT_ACLS
5955 +       if (preserve_acls && !ACL_READY(*sxp))
5956 +               get_acl(fname, sxp);
5957 +#endif
5958 +
5959 +       if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && omit_dir_times))
5960                 flags |= ATTRS_SKIP_MTIME;
5961         if (!(flags & ATTRS_SKIP_MTIME)
5962 -           && cmp_time(st->st_mtime, file->modtime) != 0) {
5963 -               int ret = set_modtime(fname, file->modtime, st->st_mode);
5964 +           && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
5965 +               int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
5966                 if (ret < 0) {
5967                         rsyserr(FERROR, errno, "failed to set times on %s",
5968                                 full_fname(fname));
5969 -                       return 0;
5970 +                       goto cleanup;
5971                 }
5972                 if (ret == 0) /* ret == 1 if symlink could not be set */
5973                         updated = 1;
5974         }
5975  
5976 -       change_uid = am_root && preserve_uid && st->st_uid != F_UID(file);
5977 +       change_uid = am_root && preserve_uid && sxp->st.st_uid != F_UID(file);
5978         change_gid = preserve_gid && F_GID(file) != GID_NONE
5979 -               && st->st_gid != F_GID(file);
5980 +               && sxp->st.st_gid != F_GID(file);
5981  #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
5982 -       if (S_ISLNK(st->st_mode))
5983 +       if (S_ISLNK(sxp->st.st_mode))
5984                 ;
5985         else
5986  #endif
5987 @@ -279,45 +289,57 @@ int set_file_attrs(char *fname, struct f
5988                                 rprintf(FINFO,
5989                                         "set uid of %s from %ld to %ld\n",
5990                                         fname,
5991 -                                       (long)st->st_uid, (long)F_UID(file));
5992 +                                       (long)sxp->st.st_uid, (long)F_UID(file));
5993                         }
5994                         if (change_gid) {
5995                                 rprintf(FINFO,
5996                                         "set gid of %s from %ld to %ld\n",
5997                                         fname,
5998 -                                       (long)st->st_gid, (long)F_GID(file));
5999 +                                       (long)sxp->st.st_gid, (long)F_GID(file));
6000                         }
6001                 }
6002                 if (do_lchown(fname,
6003 -                   change_uid ? F_UID(file) : st->st_uid,
6004 -                   change_gid ? F_GID(file) : st->st_gid) != 0) {
6005 +                   change_uid ? F_UID(file) : sxp->st.st_uid,
6006 +                   change_gid ? F_GID(file) : sxp->st.st_gid) != 0) {
6007                         /* shouldn't have attempted to change uid or gid
6008                          * unless have the privilege */
6009                         rsyserr(FERROR, errno, "%s %s failed",
6010                             change_uid ? "chown" : "chgrp",
6011                             full_fname(fname));
6012 -                       return 0;
6013 +                       goto cleanup;
6014                 }
6015                 /* a lchown had been done - we have to re-stat if the
6016                  * destination had the setuid or setgid bits set due
6017                  * to the side effect of the chown call */
6018 -               if (st->st_mode & (S_ISUID | S_ISGID)) {
6019 -                       link_stat(fname, st,
6020 -                                 keep_dirlinks && S_ISDIR(st->st_mode));
6021 +               if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
6022 +                       link_stat(fname, &sxp->st,
6023 +                                 keep_dirlinks && S_ISDIR(sxp->st.st_mode));
6024                 }
6025                 updated = 1;
6026         }
6027  
6028         if (daemon_chmod_modes && !S_ISLNK(new_mode))
6029                 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
6030 +
6031 +#ifdef SUPPORT_ACLS
6032 +       /* It's OK to call set_acl() now, even for a dir, as the generator
6033 +        * will enable owner-writability using chmod, if necessary.
6034 +        * 
6035 +        * If set_acl() changes permission bits in the process of setting
6036 +        * an access ACL, it changes sxp->st.st_mode so we know whether we
6037 +        * need to chmod(). */
6038 +       if (preserve_acls && set_acl(fname, file, sxp) == 0)
6039 +               updated = 1;
6040 +#endif
6041 +
6042  #ifdef HAVE_CHMOD
6043 -       if (!BITS_EQUAL(st->st_mode, new_mode, CHMOD_BITS)) {
6044 +       if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
6045                 int ret = do_chmod(fname, new_mode);
6046                 if (ret < 0) {
6047                         rsyserr(FERROR, errno,
6048                                 "failed to set permissions on %s",
6049                                 full_fname(fname));
6050 -                       return 0;
6051 +                       goto cleanup;
6052                 }
6053                 if (ret == 0) /* ret == 1 if symlink could not be set */
6054                         updated = 1;
6055 @@ -330,6 +352,11 @@ int set_file_attrs(char *fname, struct f
6056                 else
6057                         rprintf(FCLIENT, "%s is uptodate\n", fname);
6058         }
6059 +  cleanup:
6060 +#ifdef SUPPORT_ACLS
6061 +       if (preserve_acls && sxp == &sx2)
6062 +               free_acl(&sx2);
6063 +#endif
6064         return updated;
6065  }
6066  
6067 --- old/rsync.h
6068 +++ new/rsync.h
6069 @@ -547,6 +547,14 @@ struct idev_node {
6070  #define IN_LOOPBACKNET 127
6071  #endif
6072  
6073 +#ifndef HAVE_NO_ACLS
6074 +#define SUPPORT_ACLS 1
6075 +#endif
6076 +
6077 +#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
6078 +#define ACLS_NEED_MASK 1
6079 +#endif
6080 +
6081  #define GID_NONE ((gid_t)-1)
6082  
6083  union file_extras {
6084 @@ -566,6 +574,7 @@ struct file_struct {
6085  extern int file_extra_cnt;
6086  extern int preserve_uid;
6087  extern int preserve_gid;
6088 +extern int preserve_acls;
6089  
6090  #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
6091  #define EXTRA_LEN (sizeof (union file_extras))
6092 @@ -598,10 +607,12 @@ extern int preserve_gid;
6093  /* When the associated option is on, all entries will have these present: */
6094  #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
6095  #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
6096 +#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
6097  
6098  /* These items are per-entry optional and mutally exclusive: */
6099  #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
6100  #define F_HL_PREV(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
6101 +#define F_DEF_ACL(f) OPT_EXTRA(f, LEN64_BUMP(f))->unum
6102  #define F_DIRDEV_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) + 2 - 1)->unum)
6103  #define F_DIRNODE_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) + 3 - 1)->num)
6104  
6105 @@ -753,6 +764,17 @@ struct stats {
6106  
6107  struct chmod_mode_struct;
6108  
6109 +#define EMPTY_ITEM_LIST {NULL, 0, 0}
6110 +
6111 +typedef struct {
6112 +       void *items;
6113 +       size_t count;
6114 +       size_t malloced;
6115 +} item_list;
6116 +
6117 +#define EXPAND_ITEM_LIST(lp, type, incr) \
6118 +       (type*)expand_item_list(lp, sizeof (type), #type, incr)
6119 +
6120  #include "byteorder.h"
6121  #include "lib/mdfour.h"
6122  #include "lib/wildmatch.h"
6123 @@ -771,6 +793,16 @@ struct chmod_mode_struct;
6124  #define NORETURN __attribute__((__noreturn__))
6125  #endif
6126  
6127 +typedef struct {
6128 +    STRUCT_STAT st;
6129 +#ifdef SUPPORT_ACLS
6130 +    struct rsync_acl *acc_acl; /* access ACL */
6131 +    struct rsync_acl *def_acl; /* default ACL */
6132 +#endif
6133 +} statx;
6134 +
6135 +#define ACL_READY(sx) ((sx).acc_acl != NULL)
6136 +
6137  #include "proto.h"
6138  
6139  /* We have replacement versions of these if they're missing. */
6140 --- old/rsync.yo
6141 +++ new/rsync.yo
6142 @@ -301,7 +301,7 @@ to the detailed description below for a 
6143   -q, --quiet                 suppress non-error messages
6144       --no-motd               suppress daemon-mode MOTD (see caveat)
6145   -c, --checksum              skip based on checksum, not mod-time & size
6146 - -a, --archive               archive mode; same as -rlptgoD (no -H)
6147 + -a, --archive               archive mode; same as -rlptgoD (no -H, -A)
6148       --no-OPTION             turn off an implied OPTION (e.g. --no-D)
6149   -r, --recursive             recurse into directories
6150   -R, --relative              use relative path names
6151 @@ -323,6 +323,7 @@ to the detailed description below for a 
6152   -p, --perms                 preserve permissions
6153   -E, --executability         preserve executability
6154       --chmod=CHMOD           affect file and/or directory permissions
6155 + -A, --acls                  preserve ACLs (implies -p) [non-standard]
6156   -o, --owner                 preserve owner (super-user only)
6157   -g, --group                 preserve group
6158       --devices               preserve device files (super-user only)
6159 @@ -771,7 +772,9 @@ quote(itemization(
6160    permissions, though the bf(--executability) option might change just
6161    the execute permission for the file.
6162    it() New files get their "normal" permission bits set to the source
6163 -  file's permissions masked with the receiving end's umask setting, and
6164 +  file's permissions masked with the receiving directory's default
6165 +  permissions (either the receiving process's umask, or the permissions
6166 +  specified via the destination directory's default ACL), and
6167    their special permission bits disabled except in the case where a new
6168    directory inherits a setgid bit from its parent directory.
6169  ))
6170 @@ -802,9 +805,11 @@ The preservation of the destination's se
6171  directories when bf(--perms) is off was added in rsync 2.6.7.  Older rsync
6172  versions erroneously preserved the three special permission bits for
6173  newly-created files when bf(--perms) was off, while overriding the
6174 -destination's setgid bit setting on a newly-created directory.  (Keep in
6175 -mind that it is the version of the receiving rsync that affects this
6176 -behavior.)
6177 +destination's setgid bit setting on a newly-created directory.  Default ACL
6178 +observance was added to the ACL patch for rsync 2.6.7, so older (or
6179 +non-ACL-enabled) rsyncs use the umask even if default ACLs are present.
6180 +(Keep in mind that it is the version of the receiving rsync that affects
6181 +these behaviors.)
6182  
6183  dit(bf(-E, --executability)) This option causes rsync to preserve the
6184  executability (or non-executability) of regular files when bf(--perms) is
6185 @@ -822,6 +827,14 @@ quote(itemization(
6186  
6187  If bf(--perms) is enabled, this option is ignored.
6188  
6189 +dit(bf(-A, --acls)) This option causes rsync to update the destination
6190 +ACLs to be the same as the source ACLs.  This nonstandard option only
6191 +works if the remote rsync also supports it.  bf(--acls) implies bf(--perms).
6192 +
6193 +The ACL-sending protocol used by this version was first introduced in
6194 +the patch that was shipped with 2.6.8.  Sending ACLs to an older version
6195 +of the ACL patch is not supported.
6196 +
6197  dit(bf(--chmod)) This option tells rsync to apply one or more
6198  comma-separated "chmod" strings to the permission of the files in the
6199  transfer.  The resulting value is treated as though it was the permissions
6200 @@ -1432,8 +1445,8 @@ if the receiving rsync is at least versi
6201  with older versions of rsync, but that also turns on the output of other
6202  verbose messages).
6203  
6204 -The "%i" escape has a cryptic output that is 9 letters long.  The general
6205 -format is like the string bf(YXcstpogz), where bf(Y) is replaced by the
6206 +The "%i" escape has a cryptic output that is 11 letters long.  The general
6207 +format is like the string bf(YXcstpoguax), where bf(Y) is replaced by the
6208  type of update being done, bf(X) is replaced by the file-type, and the
6209  other letters represent attributes that may be output if they are being
6210  modified.
6211 @@ -1482,7 +1495,11 @@ quote(itemization(
6212    sender's value (requires bf(--owner) and super-user privileges).
6213    it() A bf(g) means the group is different and is being updated to the
6214    sender's value (requires bf(--group) and the authority to set the group).
6215 -  it() The bf(z) slot is reserved for future use.
6216 +  it() The bf(u) slot is reserved for reporting update (access) time changes
6217 +  (a feature that is not yet released).
6218 +  it() The bf(a) means that the ACL information changed.
6219 +  it() The bf(x) slot is reserved for reporting extended attribute changes
6220 +  (a feature that is not yet released).
6221  ))
6222  
6223  One other output is possible:  when deleting files, the "%i" will output
6224 --- old/smb_acls.h
6225 +++ new/smb_acls.h
6226 @@ -0,0 +1,281 @@
6227 +/* 
6228 +   Unix SMB/Netbios implementation.
6229 +   Version 2.2.x
6230 +   Portable SMB ACL interface
6231 +   Copyright (C) Jeremy Allison 2000
6232 +   
6233 +   This program is free software; you can redistribute it and/or modify
6234 +   it under the terms of the GNU General Public License as published by
6235 +   the Free Software Foundation; either version 2 of the License, or
6236 +   (at your option) any later version.
6237 +   
6238 +   This program is distributed in the hope that it will be useful,
6239 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
6240 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6241 +   GNU General Public License for more details.
6242 +   
6243 +   You should have received a copy of the GNU General Public License
6244 +   along with this program; if not, write to the Free Software
6245 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
6246 +*/
6247 +
6248 +#ifndef _SMB_ACLS_H
6249 +#define _SMB_ACLS_H
6250 +
6251 +#if defined HAVE_POSIX_ACLS
6252 +
6253 +/* This is an identity mapping (just remove the SMB_). */
6254 +
6255 +#define SMB_ACL_TAG_T          acl_tag_t
6256 +#define SMB_ACL_TYPE_T         acl_type_t
6257 +#define SMB_ACL_PERMSET_T      acl_permset_t
6258 +#define SMB_ACL_PERM_T         acl_perm_t
6259 +#define SMB_ACL_READ           ACL_READ
6260 +#define SMB_ACL_WRITE          ACL_WRITE
6261 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
6262 +
6263 +/* Types of ACLs. */
6264 +#define SMB_ACL_USER           ACL_USER
6265 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
6266 +#define SMB_ACL_GROUP          ACL_GROUP
6267 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
6268 +#define SMB_ACL_OTHER          ACL_OTHER
6269 +#define SMB_ACL_MASK           ACL_MASK
6270 +
6271 +#define SMB_ACL_T              acl_t
6272 +
6273 +#define SMB_ACL_ENTRY_T                acl_entry_t
6274 +
6275 +#define SMB_ACL_FIRST_ENTRY    ACL_FIRST_ENTRY
6276 +#define SMB_ACL_NEXT_ENTRY     ACL_NEXT_ENTRY
6277 +
6278 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
6279 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
6280 +
6281 +#elif defined HAVE_TRU64_ACLS
6282 +
6283 +/* This is for DEC/Compaq Tru64 UNIX */
6284 +
6285 +#define SMB_ACL_TAG_T          acl_tag_t
6286 +#define SMB_ACL_TYPE_T         acl_type_t
6287 +#define SMB_ACL_PERMSET_T      acl_permset_t
6288 +#define SMB_ACL_PERM_T         acl_perm_t
6289 +#define SMB_ACL_READ           ACL_READ
6290 +#define SMB_ACL_WRITE          ACL_WRITE
6291 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
6292 +
6293 +/* Types of ACLs. */
6294 +#define SMB_ACL_USER           ACL_USER
6295 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
6296 +#define SMB_ACL_GROUP          ACL_GROUP
6297 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
6298 +#define SMB_ACL_OTHER          ACL_OTHER
6299 +#define SMB_ACL_MASK           ACL_MASK
6300 +
6301 +#define SMB_ACL_T              acl_t
6302 +
6303 +#define SMB_ACL_ENTRY_T                acl_entry_t
6304 +
6305 +#define SMB_ACL_FIRST_ENTRY    0
6306 +#define SMB_ACL_NEXT_ENTRY     1
6307 +
6308 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
6309 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
6310 +
6311 +#elif defined HAVE_UNIXWARE_ACLS || defined HAVE_SOLARIS_ACLS
6312 +/*
6313 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
6314 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
6315 + */
6316 +
6317 +/* SVR4.2 ES/MP ACLs */
6318 +typedef int SMB_ACL_TAG_T;
6319 +typedef int SMB_ACL_TYPE_T;
6320 +typedef ushort *SMB_ACL_PERMSET_T;
6321 +typedef ushort SMB_ACL_PERM_T;
6322 +#define SMB_ACL_READ           4
6323 +#define SMB_ACL_WRITE          2
6324 +#define SMB_ACL_EXECUTE                1
6325 +
6326 +/* Types of ACLs. */
6327 +#define SMB_ACL_USER           USER
6328 +#define SMB_ACL_USER_OBJ       USER_OBJ
6329 +#define SMB_ACL_GROUP          GROUP
6330 +#define SMB_ACL_GROUP_OBJ      GROUP_OBJ
6331 +#define SMB_ACL_OTHER          OTHER_OBJ
6332 +#define SMB_ACL_MASK           CLASS_OBJ
6333 +
6334 +typedef struct SMB_ACL_T {
6335 +       int size;
6336 +       int count;
6337 +       int next;
6338 +       struct acl acl[1];
6339 +} *SMB_ACL_T;
6340 +
6341 +typedef struct acl *SMB_ACL_ENTRY_T;
6342 +
6343 +#define SMB_ACL_FIRST_ENTRY    0
6344 +#define SMB_ACL_NEXT_ENTRY     1
6345 +
6346 +#define SMB_ACL_TYPE_ACCESS    0
6347 +#define SMB_ACL_TYPE_DEFAULT   1
6348 +
6349 +#ifdef __CYGWIN__
6350 +#define SMB_ACL_LOSES_SPECIAL_MODE_BITS
6351 +#endif
6352 +
6353 +#elif defined HAVE_HPUX_ACLS
6354 +
6355 +/*
6356 + * Based on the Solaris & UnixWare code.
6357 + */
6358 +
6359 +#undef GROUP
6360 +#include <sys/aclv.h>
6361 +
6362 +/* SVR4.2 ES/MP ACLs */
6363 +typedef int SMB_ACL_TAG_T;
6364 +typedef int SMB_ACL_TYPE_T;
6365 +typedef ushort *SMB_ACL_PERMSET_T;
6366 +typedef ushort SMB_ACL_PERM_T;
6367 +#define SMB_ACL_READ           4
6368 +#define SMB_ACL_WRITE          2
6369 +#define SMB_ACL_EXECUTE                1
6370 +
6371 +/* Types of ACLs. */
6372 +#define SMB_ACL_USER           USER
6373 +#define SMB_ACL_USER_OBJ       USER_OBJ
6374 +#define SMB_ACL_GROUP          GROUP
6375 +#define SMB_ACL_GROUP_OBJ      GROUP_OBJ
6376 +#define SMB_ACL_OTHER          OTHER_OBJ
6377 +#define SMB_ACL_MASK           CLASS_OBJ
6378 +
6379 +typedef struct SMB_ACL_T {
6380 +       int size;
6381 +       int count;
6382 +       int next;
6383 +       struct acl acl[1];
6384 +} *SMB_ACL_T;
6385 +
6386 +typedef struct acl *SMB_ACL_ENTRY_T;
6387 +
6388 +#define SMB_ACL_FIRST_ENTRY    0
6389 +#define SMB_ACL_NEXT_ENTRY     1
6390 +
6391 +#define SMB_ACL_TYPE_ACCESS    0
6392 +#define SMB_ACL_TYPE_DEFAULT   1
6393 +
6394 +#elif defined HAVE_IRIX_ACLS
6395 +
6396 +#define SMB_ACL_TAG_T          acl_tag_t
6397 +#define SMB_ACL_TYPE_T         acl_type_t
6398 +#define SMB_ACL_PERMSET_T      acl_permset_t
6399 +#define SMB_ACL_PERM_T         acl_perm_t
6400 +#define SMB_ACL_READ           ACL_READ
6401 +#define SMB_ACL_WRITE          ACL_WRITE
6402 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
6403 +
6404 +/* Types of ACLs. */
6405 +#define SMB_ACL_USER           ACL_USER
6406 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
6407 +#define SMB_ACL_GROUP          ACL_GROUP
6408 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
6409 +#define SMB_ACL_OTHER          ACL_OTHER_OBJ
6410 +#define SMB_ACL_MASK           ACL_MASK
6411 +
6412 +typedef struct SMB_ACL_T {
6413 +       int next;
6414 +       BOOL freeaclp;
6415 +       struct acl *aclp;
6416 +} *SMB_ACL_T;
6417 +
6418 +#define SMB_ACL_ENTRY_T                acl_entry_t
6419 +
6420 +#define SMB_ACL_FIRST_ENTRY    0
6421 +#define SMB_ACL_NEXT_ENTRY     1
6422 +
6423 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
6424 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
6425 +
6426 +#elif defined HAVE_AIX_ACLS
6427 +
6428 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
6429 +
6430 +#include "/usr/include/acl.h"
6431 +
6432 +typedef uint *SMB_ACL_PERMSET_T;
6433
6434 +struct acl_entry_link{
6435 +       struct acl_entry_link *prevp;
6436 +       struct new_acl_entry *entryp;
6437 +       struct acl_entry_link *nextp;
6438 +       int count;
6439 +};
6440 +
6441 +struct new_acl_entry{
6442 +       unsigned short ace_len;
6443 +       unsigned short ace_type;
6444 +       unsigned int ace_access;
6445 +       struct ace_id ace_id[1];
6446 +};
6447 +
6448 +#define SMB_ACL_ENTRY_T                struct new_acl_entry*
6449 +#define SMB_ACL_T              struct acl_entry_link*
6450
6451 +#define SMB_ACL_TAG_T          unsigned short
6452 +#define SMB_ACL_TYPE_T         int
6453 +#define SMB_ACL_PERM_T         uint
6454 +#define SMB_ACL_READ           S_IRUSR
6455 +#define SMB_ACL_WRITE          S_IWUSR
6456 +#define SMB_ACL_EXECUTE                S_IXUSR
6457 +
6458 +/* Types of ACLs. */
6459 +#define SMB_ACL_USER           ACEID_USER
6460 +#define SMB_ACL_USER_OBJ       3
6461 +#define SMB_ACL_GROUP          ACEID_GROUP
6462 +#define SMB_ACL_GROUP_OBJ      4
6463 +#define SMB_ACL_OTHER          5
6464 +#define SMB_ACL_MASK           6
6465 +
6466 +
6467 +#define SMB_ACL_FIRST_ENTRY    1
6468 +#define SMB_ACL_NEXT_ENTRY     2
6469 +
6470 +#define SMB_ACL_TYPE_ACCESS    0
6471 +#define SMB_ACL_TYPE_DEFAULT   1
6472 +
6473 +#else /* No ACLs. */
6474 +
6475 +/* No ACLS - fake it. */
6476 +#define SMB_ACL_TAG_T          int
6477 +#define SMB_ACL_TYPE_T         int
6478 +#define SMB_ACL_PERMSET_T      mode_t
6479 +#define SMB_ACL_PERM_T         mode_t
6480 +#define SMB_ACL_READ           S_IRUSR
6481 +#define SMB_ACL_WRITE          S_IWUSR
6482 +#define SMB_ACL_EXECUTE                S_IXUSR
6483 +
6484 +/* Types of ACLs. */
6485 +#define SMB_ACL_USER           0
6486 +#define SMB_ACL_USER_OBJ       1
6487 +#define SMB_ACL_GROUP          2
6488 +#define SMB_ACL_GROUP_OBJ      3
6489 +#define SMB_ACL_OTHER          4
6490 +#define SMB_ACL_MASK           5
6491 +
6492 +typedef struct SMB_ACL_T {
6493 +       int dummy;
6494 +} *SMB_ACL_T;
6495 +
6496 +typedef struct SMB_ACL_ENTRY_T {
6497 +       int dummy;
6498 +} *SMB_ACL_ENTRY_T;
6499 +
6500 +#define SMB_ACL_FIRST_ENTRY    0
6501 +#define SMB_ACL_NEXT_ENTRY     1
6502 +
6503 +#define SMB_ACL_TYPE_ACCESS    0
6504 +#define SMB_ACL_TYPE_DEFAULT   1
6505 +
6506 +#endif /* No ACLs. */
6507 +#endif /* _SMB_ACLS_H */
6508 --- old/testsuite/acls.test
6509 +++ new/testsuite/acls.test
6510 @@ -0,0 +1,34 @@
6511 +#! /bin/sh
6512 +
6513 +# This program is distributable under the terms of the GNU GPL (see
6514 +# COPYING).
6515 +
6516 +# Test that rsync handles basic ACL preservation.
6517 +
6518 +. $srcdir/testsuite/rsync.fns
6519 +
6520 +$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
6521 +case "$setfacl_nodef" in
6522 +true) test_skipped "I don't know how to use your setfacl command" ;;
6523 +esac
6524 +
6525 +makepath "$fromdir/foo"
6526 +echo something >"$fromdir/file1"
6527 +echo else >"$fromdir/file2"
6528 +
6529 +files='foo file1 file2'
6530 +
6531 +setfacl -m u:0:7 "$fromdir/foo" || test_skipped "Your filesystem has ACLs disabled"
6532 +setfacl -m u:0:5 "$fromdir/file1"
6533 +setfacl -m u:0:5 "$fromdir/file2"
6534 +
6535 +$RSYNC -avvA "$fromdir/" "$todir/"
6536 +
6537 +cd "$fromdir"
6538 +getfacl $files >"$scratchdir/acls.txt"
6539 +
6540 +cd "$todir"
6541 +getfacl $files | diff $diffopt "$scratchdir/acls.txt" -
6542 +
6543 +# The script would have aborted on error, so getting here means we've won.
6544 +exit 0
6545 --- old/testsuite/default-acls.test
6546 +++ new/testsuite/default-acls.test
6547 @@ -0,0 +1,65 @@
6548 +#! /bin/sh
6549 +
6550 +# This program is distributable under the terms of the GNU GPL (see
6551 +# COPYING).
6552 +
6553 +# Test that rsync obeys default ACLs. -- Matt McCutchen
6554 +
6555 +. $srcdir/testsuite/rsync.fns
6556 +
6557 +$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
6558 +case "$setfacl_nodef" in
6559 +true) test_skipped "I don't know how to use your setfacl command" ;;
6560 +*-k*) opts='-dm u::7,g::5,o:5' ;;
6561 +*) opts='-m d:u::7,d:g::5,d:o:5' ;;
6562 +esac
6563 +setfacl $opts "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
6564 +
6565 +# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
6566 +testit() {
6567 +    todir="$scratchdir/$1"
6568 +    mkdir "$todir"
6569 +    $setfacl_nodef "$todir"
6570 +    if [ "$2" ]; then
6571 +       case "$setfacl_nodef" in
6572 +       *-k*) opts="-dm $2" ;;
6573 +       *) opts="-m `echo $2 | sed 's/\([ugom]:\)/d:\1/g'`"
6574 +       esac
6575 +       setfacl $opts "$todir"
6576 +    fi
6577 +    # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
6578 +    # even though the directory itself is outside the transfer
6579 +    $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
6580 +    check_perms "$todir/to" $4 "Target $1"
6581 +    check_perms "$todir/to/dir" $4 "Target $1"
6582 +    check_perms "$todir/to/file" $3 "Target $1"
6583 +    check_perms "$todir/to/program" $4 "Target $1"
6584 +    # Make sure get_local_name doesn't mess us up when transferring only one file
6585 +    $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
6586 +    check_perms "$todir/to/anotherfile" $3 "Target $1"
6587 +    # Make sure we obey default ACLs when not transferring a regular file
6588 +    $RSYNC -rvv "$scratchdir/dir/" "$todir/to/anotherdir/"
6589 +    check_perms "$todir/to/anotherdir" $4 "Target $1"
6590 +}
6591 +
6592 +mkdir "$scratchdir/dir"
6593 +echo "File!" >"$scratchdir/file"
6594 +echo "#!/bin/sh" >"$scratchdir/program"
6595 +chmod 777 "$scratchdir/dir"
6596 +chmod 666 "$scratchdir/file"
6597 +chmod 777 "$scratchdir/program"
6598 +
6599 +# Test some target directories
6600 +umask 0077
6601 +testit da777 u::7,g::7,o:7 rw-rw-rw- rwxrwxrwx
6602 +testit da775 u::7,g::7,o:5 rw-rw-r-- rwxrwxr-x
6603 +testit da750 u::7,g::5,o:0 rw-r----- rwxr-x---
6604 +testit da770mask u::7,u:0:7,g::0,m:7,o:0 rw-rw---- rwxrwx---
6605 +testit noda1 '' rw------- rwx------
6606 +umask 0000
6607 +testit noda2 '' rw-rw-rw- rwxrwxrwx
6608 +umask 0022
6609 +testit noda3 '' rw-r--r-- rwxr-xr-x
6610 +
6611 +# Hooray
6612 +exit 0
6613 --- old/testsuite/devices.test
6614 +++ new/testsuite/devices.test
6615 @@ -42,14 +42,14 @@ touch -r "$fromdir/block" "$fromdir/bloc
6616  $RSYNC -ai "$fromdir/block" "$todir/block2" \
6617      | tee "$outfile"
6618  cat <<EOT >"$chkfile"
6619 -cD+++++++ block
6620 +cD+++++++++ block
6621  EOT
6622  diff $diffopt "$chkfile" "$outfile" || test_fail "test 1 failed"
6623  
6624  $RSYNC -ai "$fromdir/block2" "$todir/block" \
6625      | tee "$outfile"
6626  cat <<EOT >"$chkfile"
6627 -cD+++++++ block2
6628 +cD+++++++++ block2
6629  EOT
6630  diff $diffopt "$chkfile" "$outfile" || test_fail "test 2 failed"
6631  
6632 @@ -58,7 +58,7 @@ sleep 1
6633  $RSYNC -Di "$fromdir/block3" "$todir/block" \
6634      | tee "$outfile"
6635  cat <<EOT >"$chkfile"
6636 -cD..T.... block3
6637 +cD..T...... block3
6638  EOT
6639  diff $diffopt "$chkfile" "$outfile" || test_fail "test 3 failed"
6640  
6641 @@ -66,15 +66,15 @@ $RSYNC -aiHvv "$fromdir/" "$todir/" \
6642      | tee "$outfile"
6643  filter_outfile
6644  cat <<EOT >"$chkfile"
6645 -.d..t.... ./
6646 -cD..t.... block
6647 -cD        block2
6648 -cD+++++++ block3
6649 -hD+++++++ block2.5 => block3
6650 -cD+++++++ char
6651 -cD+++++++ char2
6652 -cD+++++++ char3
6653 -cS+++++++ fifo
6654 +.d..t...... ./
6655 +cD..t...... block
6656 +cD          block2
6657 +cD+++++++++ block3
6658 +hD+++++++++ block2.5 => block3
6659 +cD+++++++++ char
6660 +cD+++++++++ char2
6661 +cD+++++++++ char3
6662 +cS+++++++++ fifo
6663  EOT
6664  if test ! -b "$fromdir/block2.5"; then
6665      sed -e '/block2\.5/d' \
6666 @@ -94,15 +94,15 @@ if test -b "$fromdir/block2.5"; then
6667      $RSYNC -aii --link-dest="$todir" "$fromdir/" "$chkdir/" \
6668         | tee "$outfile"
6669      cat <<EOT >"$chkfile"
6670 -cd        ./
6671 -hD        block
6672 -hD        block2
6673 -hD        block2.5
6674 -hD        block3
6675 -hD        char
6676 -hD        char2
6677 -hD        char3
6678 -hS        fifo
6679 +cd          ./
6680 +hD          block
6681 +hD          block2
6682 +hD          block2.5
6683 +hD          block3
6684 +hD          char
6685 +hD          char2
6686 +hD          char3
6687 +hS          fifo
6688  EOT
6689      diff $diffopt "$chkfile" "$outfile" || test_fail "test 4 failed"
6690  fi
6691 --- old/testsuite/itemize.test
6692 +++ new/testsuite/itemize.test
6693 @@ -47,16 +47,16 @@ rm -f "$to2dir" "$to2dir.test"
6694  $RSYNC -iplr "$fromdir/" "$todir/" \
6695      | tee "$outfile"
6696  sed -e "$sed_cmd" <<EOT >"$chkfile"
6697 -cd+++++++ ./
6698 -cd+++++++ bar/
6699 -cd+++++++ foo/_P30_
6700 -cd+++++++ bar/baz/
6701 ->f+++++++ bar/baz/rsync
6702 -cd+++++++ foo/_P29_
6703 ->f+++++++ foo/config1
6704 ->f+++++++ foo/config2
6705 ->f+++++++ foo/extra
6706 -cL+++++++ foo/sym -> ../bar/baz/rsync
6707 +cd+++++++++ ./
6708 +cd+++++++++ bar/
6709 +cd+++++++++ foo/_P30_
6710 +cd+++++++++ bar/baz/
6711 +>f+++++++++ bar/baz/rsync
6712 +cd+++++++++ foo/_P29_
6713 +>f+++++++++ foo/config1
6714 +>f+++++++++ foo/config2
6715 +>f+++++++++ foo/extra
6716 +cL+++++++++ foo/sym -> ../bar/baz/rsync
6717  EOT
6718  diff $diffopt "$chkfile" "$outfile" || test_fail "test 1 failed"
6719  
6720 @@ -68,10 +68,10 @@ chmod 601 "$fromdir/foo/config2"
6721  $RSYNC -iplrH "$fromdir/" "$todir/" \
6722      | tee "$outfile"
6723  sed -e "$sed_cmd" <<EOT >"$chkfile"
6724 ->f..T.... bar/baz/rsync
6725 ->f..T.... foo/config1
6726 ->f.sTp... foo/config2
6727 -hf..T.... foo/extra => foo/config1
6728 +>f..T...... bar/baz/rsync
6729 +>f..T...... foo/config1
6730 +>f.sTp..... foo/config2
6731 +hf..T...... foo/extra => foo/config1
6732  EOT
6733  diff $diffopt "$chkfile" "$outfile" || test_fail "test 2 failed"
6734  
6735 @@ -88,12 +88,12 @@ chmod 777 "$todir/bar/baz/rsync"
6736  $RSYNC -iplrtc "$fromdir/" "$todir/" \
6737      | tee "$outfile"
6738  sed -e "$sed_cmd" <<EOT >"$chkfile"
6739 -.d..t.... foo/_P30_
6740 -.f..tp... bar/baz/rsync
6741 -.d..t.... foo/_P29_
6742 -.f..t.... foo/config1
6743 ->fcstp... foo/config2
6744 -cL..T.... foo/sym -> ../bar/baz/rsync
6745 +.d..t...... foo/_P30_
6746 +.f..tp..... bar/baz/rsync
6747 +.d..t...... foo/_P29_
6748 +.f..t...... foo/config1
6749 +>fcstp..... foo/config2
6750 +cL..T...... foo/sym -> ../bar/baz/rsync
6751  EOT
6752  diff $diffopt "$chkfile" "$outfile" || test_fail "test 3 failed"
6753  
6754 @@ -118,15 +118,15 @@ $RSYNC -ivvplrtH "$fromdir/" "$todir/" \
6755      | tee "$outfile"
6756  filter_outfile
6757  sed -e "$sed_cmd" <<EOT >"$chkfile"
6758 -.d        ./
6759 -.d        bar/
6760 -.d        bar/baz/
6761 -.f...p... bar/baz/rsync
6762 -.d        foo/
6763 -.f        foo/config1
6764 ->f..t.... foo/config2
6765 -hf        foo/extra
6766 -.L        foo/sym -> ../bar/baz/rsync
6767 +.d          ./
6768 +.d          bar/
6769 +.d          bar/baz/
6770 +.f...p..... bar/baz/rsync
6771 +.d          foo/
6772 +.f          foo/config1
6773 +>f..t...... foo/config2
6774 +hf          foo/extra
6775 +.L          foo/sym -> ../bar/baz/rsync
6776  EOT
6777  diff $diffopt "$chkfile" "$outfile" || test_fail "test 5 failed"
6778  
6779 @@ -145,8 +145,8 @@ touch "$todir/foo/config2"
6780  $RSYNC -iplrtH "$fromdir/" "$todir/" \
6781      | tee "$outfile"
6782  sed -e "$sed_cmd" <<EOT >"$chkfile"
6783 -.f...p... foo/config1
6784 ->f..t.... foo/config2
6785 +.f...p..... foo/config1
6786 +>f..t...... foo/config2
6787  EOT
6788  diff $diffopt "$chkfile" "$outfile" || test_fail "test 7 failed"
6789  
6790 @@ -154,15 +154,15 @@ $RSYNC -ivvplrtH --copy-dest=../to "$fro
6791      | tee "$outfile"
6792  filter_outfile
6793  sed -e "$sed_cmd" <<EOT >"$chkfile"
6794 -cd        ./
6795 -cd        bar/
6796 -cd        bar/baz/
6797 -cf        bar/baz/rsync
6798 -cd        foo/
6799 -cf        foo/config1
6800 -cf        foo/config2
6801 -hf        foo/extra => foo/config1
6802 -cL        foo/sym -> ../bar/baz/rsync
6803 +cd          ./
6804 +cd          bar/
6805 +cd          bar/baz/
6806 +cf          bar/baz/rsync
6807 +cd          foo/
6808 +cf          foo/config1
6809 +cf          foo/config2
6810 +hf          foo/extra => foo/config1
6811 +cL          foo/sym -> ../bar/baz/rsync
6812  EOT
6813  diff $diffopt "$chkfile" "$outfile" || test_fail "test 8 failed"
6814  
6815 @@ -170,7 +170,7 @@ rm -rf "$to2dir"
6816  $RSYNC -iplrtH --copy-dest=../to "$fromdir/" "$to2dir/" \
6817      | tee "$outfile"
6818  sed -e "$sed_cmd" <<EOT >"$chkfile"
6819 -hf        foo/extra => foo/config1
6820 +hf          foo/extra => foo/config1
6821  EOT
6822  diff $diffopt "$chkfile" "$outfile" || test_fail "test 9 failed"
6823  
6824 @@ -196,15 +196,15 @@ $RSYNC -ivvplrtH --link-dest="$todir" "$
6825      | tee "$outfile"
6826  filter_outfile
6827  sed -e "$sed_cmd" <<EOT >"$chkfile"
6828 -cd        ./
6829 -cd        bar/
6830 -cd        bar/baz/
6831 -hf        bar/baz/rsync
6832 -cd        foo/
6833 -hf        foo/config1
6834 -hf        foo/config2
6835 -hf        foo/extra => foo/config1
6836 -$L        foo/sym -> ../bar/baz/rsync
6837 +cd          ./
6838 +cd          bar/
6839 +cd          bar/baz/
6840 +hf          bar/baz/rsync
6841 +cd          foo/
6842 +hf          foo/config1
6843 +hf          foo/config2
6844 +hf          foo/extra => foo/config1
6845 +$L          foo/sym -> ../bar/baz/rsync
6846  EOT
6847  diff $diffopt "$chkfile" "$outfile" || test_fail "test 11 failed"
6848  
6849 @@ -244,15 +244,15 @@ $RSYNC -ivvplrtH --compare-dest="$todir"
6850      | tee "$outfile"
6851  filter_outfile
6852  sed -e "$sed_cmd" <<EOT >"$chkfile"
6853 -cd        ./
6854 -cd        bar/
6855 -cd        bar/baz/
6856 -.f        bar/baz/rsync
6857 -cd        foo/
6858 -.f        foo/config1
6859 -.f        foo/config2
6860 -.f        foo/extra
6861 -.L        foo/sym -> ../bar/baz/rsync
6862 +cd          ./
6863 +cd          bar/
6864 +cd          bar/baz/
6865 +.f          bar/baz/rsync
6866 +cd          foo/
6867 +.f          foo/config1
6868 +.f          foo/config2
6869 +.f          foo/extra
6870 +.L          foo/sym -> ../bar/baz/rsync
6871  EOT
6872  diff $diffopt "$chkfile" "$outfile" || test_fail "test 15 failed"
6873  
6874 --- old/uidlist.c
6875 +++ new/uidlist.c
6876 @@ -35,6 +35,7 @@ extern int verbose;
6877  extern int am_root;
6878  extern int preserve_uid;
6879  extern int preserve_gid;
6880 +extern int preserve_acls;
6881  extern int numeric_ids;
6882  
6883  struct idlist {
6884 @@ -271,7 +272,7 @@ void send_uid_list(int f)
6885  {
6886         struct idlist *list;
6887  
6888 -       if (preserve_uid) {
6889 +       if (preserve_uid || preserve_acls) {
6890                 int len;
6891                 /* we send sequences of uid/byte-length/name */
6892                 for (list = uidlist; list; list = list->next) {
6893 @@ -288,7 +289,7 @@ void send_uid_list(int f)
6894                 write_int(f, 0);
6895         }
6896  
6897 -       if (preserve_gid) {
6898 +       if (preserve_gid || preserve_acls) {
6899                 int len;
6900                 for (list = gidlist; list; list = list->next) {
6901                         if (!list->name)
6902 @@ -328,18 +329,28 @@ void recv_uid_list(int f, struct file_li
6903  {
6904         int id, i;
6905  
6906 -       if (preserve_uid && !numeric_ids) {
6907 +       if ((preserve_uid || preserve_acls) && !numeric_ids) {
6908                 /* read the uid list */
6909                 while ((id = read_int(f)) != 0)
6910                         recv_user_name(f, (uid_t)id);
6911         }
6912  
6913 -       if (preserve_gid && !numeric_ids) {
6914 +       if ((preserve_gid || preserve_acls) && !numeric_ids) {
6915                 /* read the gid list */
6916                 while ((id = read_int(f)) != 0)
6917                         recv_group_name(f, (gid_t)id);
6918         }
6919  
6920 +#ifdef SUPPORT_ACLS
6921 +       if (preserve_acls && !numeric_ids) {
6922 +               id_t *id;
6923 +               while ((id = next_acl_uid(flist)) != NULL)
6924 +                       *id = match_uid(*id);
6925 +               while ((id = next_acl_gid(flist)) != NULL)
6926 +                       *id = match_gid(*id);
6927 +       }
6928 +#endif
6929 +
6930         /* Now convert all the uids/gids from sender values to our values. */
6931         if (am_root && preserve_uid && !numeric_ids) {
6932                 for (i = 0; i < flist->count; i++)
6933 --- old/util.c
6934 +++ new/util.c
6935 @@ -1466,3 +1466,31 @@ int bitbag_next_bit(struct bitbag *bb, i
6936  
6937         return -1;
6938  }
6939 +
6940 +void *expand_item_list(item_list *lp, size_t item_size,
6941 +                      const char *desc, int incr)
6942 +{
6943 +       /* First time through, 0 <= 0, so list is expanded. */
6944 +       if (lp->malloced <= lp->count) {
6945 +               void *new_ptr;
6946 +               size_t new_size = lp->malloced;
6947 +               if (incr < 0)
6948 +                       new_size -= incr; /* increase slowly */
6949 +               else if (new_size < (size_t)incr)
6950 +                       new_size += incr;
6951 +               else
6952 +                       new_size *= 2;
6953 +               new_ptr = realloc_array(lp->items, char, new_size * item_size);
6954 +               if (verbose >= 4) {
6955 +                       rprintf(FINFO, "[%s] expand %s to %.0f bytes, did%s move\n",
6956 +                               who_am_i(), desc, (double)new_size * item_size,
6957 +                               new_ptr == lp->items ? " not" : "");
6958 +               }
6959 +               if (!new_ptr)
6960 +                       out_of_memory("expand_item_list");
6961 +
6962 +               lp->items = new_ptr;
6963 +               lp->malloced = new_size;
6964 +       }
6965 +       return (char*)lp->items + (lp->count++ * item_size);
6966 +}