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