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