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 && f >= 0) {
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 && f >= 0)
1419 +                       send_acl(&sx, f);
1420 +#endif
1421 +       } else {
1422 +#ifdef SUPPORT_ACLS
1423 +               if (preserve_acls && f >= 0)
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 @@ -348,22 +350,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 @@ -371,20 +378,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 @@ -637,7 +648,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 @@ -646,7 +657,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 @@ -654,16 +665,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 @@ -678,7 +693,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 @@ -686,7 +701,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 @@ -696,8 +711,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 @@ -713,8 +733,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 @@ -735,7 +760,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 @@ -767,24 +792,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 @@ -798,7 +823,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 @@ -815,7 +840,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 @@ -828,7 +857,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 @@ -859,7 +888,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 @@ -872,6 +909,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 @@ -893,7 +931,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 @@ -949,6 +988,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 @@ -956,7 +998,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 @@ -968,6 +1010,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 @@ -976,7 +1022,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 @@ -994,8 +1040,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 @@ -1004,8 +1051,8 @@ static void recv_generator(char *fname, 
1759                  * file of that name and it is *not* a directory, then
1760                  * we need to delete it.  If it doesn't exist, then
1761                  * (perhaps recursively) create it. */
1762 -               if (statret == 0 && !S_ISDIR(st.st_mode)) {
1763 -                       if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
1764 +               if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1765 +                       if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1766                                 return;
1767                         statret = -1;
1768                 }
1769 @@ -1014,14 +1061,14 @@ static void recv_generator(char *fname, 
1770                         dry_run++;
1771                 }
1772                 real_ret = statret;
1773 -               real_st = st;
1774 +               real_sx = sx;
1775                 if (new_root_dir) {
1776                         if (*fname == '.' && fname[1] == '\0')
1777                                 statret = -1;
1778                         new_root_dir = 0;
1779                 }
1780                 if (statret != 0 && basis_dir[0] != NULL) {
1781 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1782 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1783                                               itemizing, maybe_ATTRS_REPORT, code);
1784                         if (j == -2) {
1785                                 itemizing = 0;
1786 @@ -1030,7 +1077,11 @@ static void recv_generator(char *fname, 
1787                                 statret = 1;
1788                 }
1789                 if (itemizing && f_out != -1) {
1790 -                       itemize(file, ndx, statret, &st,
1791 +#ifdef SUPPORT_ACLS
1792 +                       if (preserve_acls && statret == 0)
1793 +                               get_acl(fname, &sx);
1794 +#endif
1795 +                       itemize(file, ndx, statret, &sx,
1796                                 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1797                 }
1798                 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1799 @@ -1050,21 +1101,21 @@ static void recv_generator(char *fname, 
1800                                 return;
1801                         }
1802                 }
1803 -               if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0)
1804 +               if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
1805                     && verbose && code != FNONE && f_out != -1)
1806                         rprintf(code, "%s/\n", fname);
1807                 if (real_ret != 0 && one_file_system)
1808 -                       real_st.st_dev = filesystem_dev;
1809 +                       real_sx.st.st_dev = filesystem_dev;
1810                 if (delete_during && f_out != -1 && !phase && dry_run < 2
1811                     && (file->flags & FLAG_DEL_HERE))
1812 -                       delete_in_dir(the_file_list, fname, file, &real_st);
1813 -               return;
1814 +                       delete_in_dir(the_file_list, fname, file, &real_sx.st);
1815 +               goto cleanup;
1816         }
1817  
1818         if (preserve_hard_links && file->link_u.links
1819 -           && hard_link_check(file, ndx, fname, statret, &st,
1820 +           && hard_link_check(file, ndx, fname, statret, &sx,
1821                                itemizing, code, HL_CHECK_MASTER))
1822 -               return;
1823 +               goto cleanup;
1824  
1825         if (preserve_links && S_ISLNK(file->mode)) {
1826  #ifdef SUPPORT_LINKS
1827 @@ -1082,15 +1133,15 @@ static void recv_generator(char *fname, 
1828                         char lnk[MAXPATHLEN];
1829                         int len;
1830  
1831 -                       if (!S_ISLNK(st.st_mode))
1832 +                       if (!S_ISLNK(sx.st.st_mode))
1833                                 statret = -1;
1834                         else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1835                               && strncmp(lnk, file->u.link, len) == 0
1836                               && file->u.link[len] == '\0') {
1837                                 /* The link is pointing to the right place. */
1838                                 if (itemizing)
1839 -                                       itemize(file, ndx, 0, &st, 0, 0, NULL);
1840 -                               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1841 +                                       itemize(file, ndx, 0, &sx, 0, 0, NULL);
1842 +                               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1843                                 if (preserve_hard_links && file->link_u.links)
1844                                         hard_link_cluster(file, ndx, itemizing, code);
1845                                 if (remove_source_files == 1)
1846 @@ -1099,10 +1150,10 @@ static void recv_generator(char *fname, 
1847                         }
1848                         /* Not the right symlink (or not a symlink), so
1849                          * delete it. */
1850 -                       if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
1851 +                       if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1852                                 return;
1853                 } else if (basis_dir[0] != NULL) {
1854 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1855 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1856                                               itemizing, maybe_ATTRS_REPORT, code);
1857                         if (j == -2) {
1858  #ifndef CAN_HARDLINK_SYMLINK
1859 @@ -1118,7 +1169,7 @@ static void recv_generator(char *fname, 
1860                                 statret = 1;
1861                 }
1862                 if (preserve_hard_links && file->link_u.links
1863 -                   && hard_link_check(file, ndx, fname, -1, &st,
1864 +                   && hard_link_check(file, ndx, fname, -1, &sx,
1865                                        itemizing, code, HL_SKIP))
1866                         return;
1867                 if (do_symlink(file->u.link, fname) != 0) {
1868 @@ -1127,7 +1178,7 @@ static void recv_generator(char *fname, 
1869                 } else {
1870                         set_file_attrs(fname, file, NULL, 0);
1871                         if (itemizing) {
1872 -                               itemize(file, ndx, statret, &st,
1873 +                               itemize(file, ndx, statret, &sx,
1874                                         ITEM_LOCAL_CHANGE, 0, NULL);
1875                         }
1876                         if (code != FNONE && verbose)
1877 @@ -1149,31 +1200,36 @@ static void recv_generator(char *fname, 
1878                 if (statret == 0) {
1879                         char *t;
1880                         if (IS_DEVICE(file->mode)) {
1881 -                               if (!IS_DEVICE(st.st_mode))
1882 +                               if (!IS_DEVICE(sx.st.st_mode))
1883                                         statret = -1;
1884                                 t = "device file";
1885                         } else {
1886 -                               if (!IS_SPECIAL(st.st_mode))
1887 +                               if (!IS_SPECIAL(sx.st.st_mode))
1888                                         statret = -1;
1889                                 t = "special file";
1890                         }
1891                         if (statret == 0
1892 -                        && (st.st_mode & ~CHMOD_BITS) == (file->mode & ~CHMOD_BITS)
1893 -                        && st.st_rdev == file->u.rdev) {
1894 +                        && (sx.st.st_mode & ~CHMOD_BITS) == (file->mode & ~CHMOD_BITS)
1895 +                        && sx.st.st_rdev == file->u.rdev) {
1896                                 /* The device or special file is identical. */
1897 -                               if (itemizing)
1898 -                                       itemize(file, ndx, 0, &st, 0, 0, NULL);
1899 -                               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1900 +                               if (itemizing) {
1901 +#ifdef SUPPORT_ACLS
1902 +                                       if (preserve_acls)
1903 +                                               get_acl(fname, &sx);
1904 +#endif
1905 +                                       itemize(file, ndx, 0, &sx, 0, 0, NULL);
1906 +                               }
1907 +                               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1908                                 if (preserve_hard_links && file->link_u.links)
1909                                         hard_link_cluster(file, ndx, itemizing, code);
1910                                 if (remove_source_files == 1)
1911                                         goto return_with_success;
1912 -                               return;
1913 +                               goto cleanup;
1914                         }
1915 -                       if (delete_item(fname, st.st_mode, t, del_opts) != 0)
1916 +                       if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1917                                 return;
1918                 } else if (basis_dir[0] != NULL) {
1919 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1920 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1921                                               itemizing, maybe_ATTRS_REPORT, code);
1922                         if (j == -2) {
1923  #ifndef CAN_HARDLINK_SPECIAL
1924 @@ -1189,7 +1245,7 @@ static void recv_generator(char *fname, 
1925                                 statret = 1;
1926                 }
1927                 if (preserve_hard_links && file->link_u.links
1928 -                   && hard_link_check(file, ndx, fname, -1, &st,
1929 +                   && hard_link_check(file, ndx, fname, -1, &sx,
1930                                        itemizing, code, HL_SKIP))
1931                         return;
1932                 if (verbose > 2) {
1933 @@ -1202,7 +1258,11 @@ static void recv_generator(char *fname, 
1934                 } else {
1935                         set_file_attrs(fname, file, NULL, 0);
1936                         if (itemizing) {
1937 -                               itemize(file, ndx, statret, &st,
1938 +#ifdef SUPPORT_ACLS
1939 +                               if (preserve_acls && statret == 0)
1940 +                                       get_acl(fname, &sx);
1941 +#endif
1942 +                               itemize(file, ndx, statret, &sx,
1943                                         ITEM_LOCAL_CHANGE, 0, NULL);
1944                         }
1945                         if (code != FNONE && verbose)
1946 @@ -1212,7 +1272,7 @@ static void recv_generator(char *fname, 
1947                         if (remove_source_files == 1)
1948                                 goto return_with_success;
1949                 }
1950 -               return;
1951 +               goto cleanup;
1952         }
1953  
1954         if (!S_ISREG(file->mode)) {
1955 @@ -1246,7 +1306,7 @@ static void recv_generator(char *fname, 
1956         }
1957  
1958         if (update_only && statret == 0
1959 -           && cmp_time(st.st_mtime, file->modtime) > 0) {
1960 +           && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1961                 if (verbose > 1)
1962                         rprintf(FINFO, "%s is newer\n", fname);
1963                 return;
1964 @@ -1255,20 +1315,20 @@ static void recv_generator(char *fname, 
1965         fnamecmp = fname;
1966         fnamecmp_type = FNAMECMP_FNAME;
1967  
1968 -       if (statret == 0 && !S_ISREG(st.st_mode)) {
1969 -               if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
1970 +       if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1971 +               if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1972                         return;
1973                 statret = -1;
1974                 stat_errno = ENOENT;
1975         }
1976  
1977         if (statret != 0 && basis_dir[0] != NULL) {
1978 -               int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
1979 +               int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1980                                       itemizing, maybe_ATTRS_REPORT, code);
1981                 if (j == -2) {
1982                         if (remove_source_files == 1)
1983                                 goto return_with_success;
1984 -                       return;
1985 +                       goto cleanup;
1986                 }
1987                 if (j >= 0) {
1988                         fnamecmp = fnamecmpbuf;
1989 @@ -1278,7 +1338,7 @@ static void recv_generator(char *fname, 
1990         }
1991  
1992         real_ret = statret;
1993 -       real_st = st;
1994 +       real_sx = sx;
1995  
1996         if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1997             && link_stat(partialptr, &partial_st, 0) == 0
1998 @@ -1297,7 +1357,7 @@ static void recv_generator(char *fname, 
1999                                 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
2000                                         fname, fnamecmpbuf);
2001                         }
2002 -                       st.st_size = fuzzy_file->length;
2003 +                       sx.st.st_size = fuzzy_file->length;
2004                         statret = 0;
2005                         fnamecmp = fnamecmpbuf;
2006                         fnamecmp_type = FNAMECMP_FUZZY;
2007 @@ -1306,7 +1366,7 @@ static void recv_generator(char *fname, 
2008  
2009         if (statret != 0) {
2010                 if (preserve_hard_links && file->link_u.links
2011 -                   && hard_link_check(file, ndx, fname, statret, &st,
2012 +                   && hard_link_check(file, ndx, fname, statret, &sx,
2013                                        itemizing, code, HL_SKIP))
2014                         return;
2015                 if (stat_errno == ENOENT)
2016 @@ -1316,39 +1376,52 @@ static void recv_generator(char *fname, 
2017                 return;
2018         }
2019  
2020 -       if (append_mode && st.st_size > file->length)
2021 +       if (append_mode && sx.st.st_size > file->length)
2022                 return;
2023  
2024         if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
2025                 ;
2026         else if (fnamecmp_type == FNAMECMP_FUZZY)
2027                 ;
2028 -       else if (unchanged_file(fnamecmp, file, &st)) {
2029 +       else if (unchanged_file(fnamecmp, file, &sx.st)) {
2030                 if (partialptr) {
2031                         do_unlink(partialptr);
2032                         handle_partial_dir(partialptr, PDIR_DELETE);
2033                 }
2034                 if (itemizing) {
2035 -                       itemize(file, ndx, real_ret, &real_st,
2036 +#ifdef SUPPORT_ACLS
2037 +                       if (preserve_acls && real_ret == 0)
2038 +                               get_acl(fnamecmp, &real_sx);
2039 +#endif
2040 +                       itemize(file, ndx, real_ret, &real_sx,
2041                                 0, 0, NULL);
2042 +#ifdef SUPPORT_ACLS
2043 +                       if (preserve_acls) {
2044 +                               if (fnamecmp_type == FNAMECMP_FNAME) {
2045 +                                       sx.acc_acl = real_sx.acc_acl;
2046 +                                       sx.def_acl = real_sx.def_acl;
2047 +                               } else
2048 +                                       free_acl(&real_sx);
2049 +                       }
2050 +#endif
2051                 }
2052 -               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
2053 +               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
2054                 if (preserve_hard_links && file->link_u.links)
2055                         hard_link_cluster(file, ndx, itemizing, code);
2056                 if (remove_source_files != 1)
2057 -                       return;
2058 +                       goto cleanup;
2059           return_with_success:
2060                 if (!dry_run) {
2061                         char numbuf[4];
2062                         SIVAL(numbuf, 0, ndx);
2063                         send_msg(MSG_SUCCESS, numbuf, 4);
2064                 }
2065 -               return;
2066 +               goto cleanup;
2067         }
2068  
2069    prepare_to_open:
2070         if (partialptr) {
2071 -               st = partial_st;
2072 +               sx.st = partial_st;
2073                 fnamecmp = partialptr;
2074                 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
2075                 statret = 0;
2076 @@ -1372,17 +1445,21 @@ static void recv_generator(char *fname, 
2077           pretend_missing:
2078                 /* pretend the file didn't exist */
2079                 if (preserve_hard_links && file->link_u.links
2080 -                   && hard_link_check(file, ndx, fname, statret, &st,
2081 +                   && hard_link_check(file, ndx, fname, statret, &sx,
2082                                        itemizing, code, HL_SKIP))
2083 -                       return;
2084 +                       goto cleanup;
2085                 statret = real_ret = -1;
2086 +#ifdef SUPPORT_ACLS
2087 +               if (preserve_acls && ACL_READY(sx))
2088 +                       free_acl(&sx);
2089 +#endif
2090                 goto notify_others;
2091         }
2092  
2093         if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
2094                 if (!(backupptr = get_backup_name(fname))) {
2095                         close(fd);
2096 -                       return;
2097 +                       goto cleanup;
2098                 }
2099                 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
2100                         close(fd);
2101 @@ -1393,7 +1470,7 @@ static void recv_generator(char *fname, 
2102                                 full_fname(backupptr));
2103                         free(back_file);
2104                         close(fd);
2105 -                       return;
2106 +                       goto cleanup;
2107                 }
2108                 if ((f_copy = do_open(backupptr,
2109                     O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
2110 @@ -1401,14 +1478,14 @@ static void recv_generator(char *fname, 
2111                                 full_fname(backupptr));
2112                         free(back_file);
2113                         close(fd);
2114 -                       return;
2115 +                       goto cleanup;
2116                 }
2117                 fnamecmp_type = FNAMECMP_BACKUP;
2118         }
2119  
2120         if (verbose > 3) {
2121                 rprintf(FINFO, "gen mapped %s of size %.0f\n",
2122 -                       fnamecmp, (double)st.st_size);
2123 +                       fnamecmp, (double)sx.st.st_size);
2124         }
2125  
2126         if (verbose > 2)
2127 @@ -1426,24 +1503,32 @@ static void recv_generator(char *fname, 
2128                         iflags |= ITEM_BASIS_TYPE_FOLLOWS;
2129                 if (fnamecmp_type == FNAMECMP_FUZZY)
2130                         iflags |= ITEM_XNAME_FOLLOWS;
2131 -               itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
2132 +#ifdef SUPPORT_ACLS
2133 +               if (preserve_acls && real_ret == 0)
2134 +                       get_acl(fnamecmp, &real_sx);
2135 +#endif
2136 +               itemize(file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
2137                         fuzzy_file ? fuzzy_file->basename : NULL);
2138 +#ifdef SUPPORT_ACLS
2139 +               if (preserve_acls)
2140 +                       free_acl(&real_sx);
2141 +#endif
2142         }
2143  
2144         if (!do_xfers) {
2145                 if (preserve_hard_links && file->link_u.links)
2146                         hard_link_cluster(file, ndx, itemizing, code);
2147 -               return;
2148 +               goto cleanup;
2149         }
2150         if (read_batch)
2151 -               return;
2152 +               goto cleanup;
2153  
2154         if (statret != 0 || whole_file) {
2155                 write_sum_head(f_out, NULL);
2156 -               return;
2157 +               goto cleanup;
2158         }
2159  
2160 -       generate_and_send_sums(fd, st.st_size, f_out, f_copy);
2161 +       generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
2162  
2163         if (f_copy >= 0) {
2164                 close(f_copy);
2165 @@ -1456,6 +1541,13 @@ static void recv_generator(char *fname, 
2166         }
2167  
2168         close(fd);
2169 +
2170 +  cleanup:
2171 +#ifdef SUPPORT_ACLS
2172 +       if (preserve_acls)
2173 +               free_acl(&sx);
2174 +#endif
2175 +       return;
2176  }
2177  
2178  void generate_files(int f_out, struct file_list *flist, char *local_name)
2179 @@ -1515,6 +1607,8 @@ void generate_files(int f_out, struct fi
2180          * notice that and let us know via the redo pipe (or its closing). */
2181         ignore_timeout = 1;
2182  
2183 +       dflt_perms = (ACCESSPERMS & ~orig_umask);
2184 +
2185         for (i = 0; i < flist->count; i++) {
2186                 struct file_struct *file = flist->files[i];
2187  
2188 --- old/hlink.c
2189 +++ new/hlink.c
2190 @@ -26,6 +26,7 @@
2191  extern int verbose;
2192  extern int do_xfers;
2193  extern int link_dest;
2194 +extern int preserve_acls;
2195  extern int make_backups;
2196  extern int remove_source_files;
2197  extern int stdout_format_has_i;
2198 @@ -147,15 +148,19 @@ void init_hard_links(void)
2199  
2200  #ifdef SUPPORT_HARD_LINKS
2201  static int maybe_hard_link(struct file_struct *file, int ndx,
2202 -                          char *fname, int statret, STRUCT_STAT *st,
2203 +                          char *fname, int statret, statx *sxp,
2204                            char *toname, STRUCT_STAT *to_st,
2205                            int itemizing, enum logcode code)
2206  {
2207         if (statret == 0) {
2208 -               if (st->st_dev == to_st->st_dev
2209 -                && st->st_ino == to_st->st_ino) {
2210 +               if (sxp->st.st_dev == to_st->st_dev
2211 +                && sxp->st.st_ino == to_st->st_ino) {
2212                         if (itemizing) {
2213 -                               itemize(file, ndx, statret, st,
2214 +#ifdef SUPPORT_ACLS
2215 +                               if (preserve_acls && !ACL_READY(*sxp))
2216 +                                       get_acl(fname, sxp);
2217 +#endif
2218 +                               itemize(file, ndx, statret, sxp,
2219                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
2220                                         0, "");
2221                         }
2222 @@ -170,13 +175,13 @@ static int maybe_hard_link(struct file_s
2223                         return -1;
2224                 }
2225         }
2226 -       return hard_link_one(file, ndx, fname, statret, st, toname,
2227 +       return hard_link_one(file, ndx, fname, statret, sxp, toname,
2228                              0, itemizing, code);
2229  }
2230  #endif
2231  
2232  int hard_link_check(struct file_struct *file, int ndx, char *fname,
2233 -                   int statret, STRUCT_STAT *st, int itemizing,
2234 +                   int statret, statx *sxp, int itemizing,
2235                     enum logcode code, int skip)
2236  {
2237  #ifdef SUPPORT_HARD_LINKS
2238 @@ -217,7 +222,7 @@ int hard_link_check(struct file_struct *
2239                                                  || st2.st_ino != st3.st_ino)
2240                                                         continue;
2241                                                 statret = 1;
2242 -                                               st = &st3;
2243 +                                               sxp->st = st3;
2244                                                 if (verbose < 2 || !stdout_format_has_i) {
2245                                                         itemizing = 0;
2246                                                         code = FNONE;
2247 @@ -227,12 +232,16 @@ int hard_link_check(struct file_struct *
2248                                         if (!unchanged_file(cmpbuf, file, &st3))
2249                                                 continue;
2250                                         statret = 1;
2251 -                                       st = &st3;
2252 -                                       if (unchanged_attrs(file, &st3))
2253 +                                       sxp->st = st3;
2254 +#ifdef SUPPORT_ACLS
2255 +                                       if (preserve_acls)
2256 +                                               get_acl(cmpbuf, sxp);
2257 +#endif
2258 +                                       if (unchanged_attrs(file, sxp))
2259                                                 break;
2260                                 } while (basis_dir[++j] != NULL);
2261                         }
2262 -                       maybe_hard_link(file, ndx, fname, statret, st,
2263 +                       maybe_hard_link(file, ndx, fname, statret, sxp,
2264                                         toname, &st2, itemizing, code);
2265                         if (remove_source_files == 1 && do_xfers) {
2266                                 char numbuf[4];
2267 @@ -250,7 +259,7 @@ int hard_link_check(struct file_struct *
2268  
2269  #ifdef SUPPORT_HARD_LINKS
2270  int hard_link_one(struct file_struct *file, int ndx, char *fname,
2271 -                 int statret, STRUCT_STAT *st, char *toname, int terse,
2272 +                 int statret, statx *sxp, char *toname, int terse,
2273                   int itemizing, enum logcode code)
2274  {
2275         if (do_link(toname, fname)) {
2276 @@ -266,7 +275,11 @@ int hard_link_one(struct file_struct *fi
2277         }
2278  
2279         if (itemizing) {
2280 -               itemize(file, ndx, statret, st,
2281 +#ifdef SUPPORT_ACLS
2282 +               if (preserve_acls && statret == 0 && !ACL_READY(*sxp))
2283 +                       get_acl(fname, sxp);
2284 +#endif
2285 +               itemize(file, ndx, statret, sxp,
2286                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
2287                         terse ? "" : toname);
2288         }
2289 @@ -283,11 +296,12 @@ void hard_link_cluster(struct file_struc
2290  #ifdef SUPPORT_HARD_LINKS
2291         char hlink1[MAXPATHLEN];
2292         char *hlink2;
2293 -       STRUCT_STAT st1, st2;
2294 +       statx sx;
2295 +       STRUCT_STAT st;
2296         int statret, ndx = master;
2297  
2298         file->F_HLINDEX = FINISHED_LINK;
2299 -       if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
2300 +       if (link_stat(f_name(file, hlink1), &st, 0) < 0)
2301                 return;
2302         if (!(file->flags & FLAG_HLINK_TOL)) {
2303                 while (!(file->flags & FLAG_HLINK_EOL)) {
2304 @@ -301,9 +315,13 @@ void hard_link_cluster(struct file_struc
2305                 if (file->F_HLINDEX != SKIPPED_LINK)
2306                         continue;
2307                 hlink2 = f_name(file, NULL);
2308 -               statret = link_stat(hlink2, &st2, 0);
2309 -               maybe_hard_link(file, ndx, hlink2, statret, &st2,
2310 -                               hlink1, &st1, itemizing, code);
2311 +               statret = link_stat(hlink2, &sx.st, 0);
2312 +               maybe_hard_link(file, ndx, hlink2, statret, &sx,
2313 +                               hlink1, &st, itemizing, code);
2314 +#ifdef SUPPORT_ACLS
2315 +               if (preserve_acls)
2316 +                       free_acl(&sx);
2317 +#endif
2318                 if (remove_source_files == 1 && do_xfers) {
2319                         char numbuf[4];
2320                         SIVAL(numbuf, 0, ndx);
2321 --- old/lib/sysacls.c
2322 +++ new/lib/sysacls.c
2323 @@ -0,0 +1,3251 @@
2324 +/* 
2325 +   Unix SMB/CIFS implementation.
2326 +   Samba system utilities for ACL support.
2327 +   Copyright (C) Jeremy Allison 2000.
2328 +   
2329 +   This program is free software; you can redistribute it and/or modify
2330 +   it under the terms of the GNU General Public License as published by
2331 +   the Free Software Foundation; either version 2 of the License, or
2332 +   (at your option) any later version.
2333 +   
2334 +   This program is distributed in the hope that it will be useful,
2335 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
2336 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2337 +   GNU General Public License for more details.
2338 +   
2339 +   You should have received a copy of the GNU General Public License
2340 +   along with this program; if not, write to the Free Software
2341 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2342 +*/
2343 +
2344 +#include "rsync.h"
2345 +#include "sysacls.h" /****** ADDED ******/
2346 +
2347 +#ifdef SUPPORT_ACLS
2348 +
2349 +/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
2350 +#ifdef DEBUG
2351 +#undef DEBUG
2352 +#endif
2353 +#define DEBUG(x,y)
2354 +
2355 +void SAFE_FREE(void *mem)
2356 +{
2357 +       if (mem)
2358 +               free(mem);
2359 +}
2360 +
2361 +char *uidtoname(uid_t uid)
2362 +{
2363 +       static char idbuf[12];
2364 +       struct passwd *pw;
2365 +
2366 +       if ((pw = getpwuid(uid)) == NULL) {
2367 +               slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
2368 +               return idbuf;
2369 +       }
2370 +       return pw->pw_name;
2371 +}
2372 +/****** EXTRAS -- END ******/
2373 +
2374 +/*
2375 + This file wraps all differing system ACL interfaces into a consistent
2376 + one based on the POSIX interface. It also returns the correct errors
2377 + for older UNIX systems that don't support ACLs.
2378 +
2379 + The interfaces that each ACL implementation must support are as follows :
2380 +
2381 + int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2382 + int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2383 + int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
2384 + void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2385 + SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2386 + SMB_ACL_T sys_acl_get_fd(int fd)
2387 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
2388 + int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
2389 + char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
2390 + SMB_ACL_T sys_acl_init( int count)
2391 + int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2392 + int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2393 + int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2394 + int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2395 + int sys_acl_valid( SMB_ACL_T theacl )
2396 + int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2397 + int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2398 + int sys_acl_delete_def_file(const char *path)
2399 +
2400 + This next one is not POSIX complient - but we *have* to have it !
2401 + More POSIX braindamage.
2402 +
2403 + int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2404 +
2405 + The generic POSIX free is the following call. We split this into
2406 + several different free functions as we may need to add tag info
2407 + to structures when emulating the POSIX interface.
2408 +
2409 + int sys_acl_free( void *obj_p)
2410 +
2411 + The calls we actually use are :
2412 +
2413 + int sys_acl_free_text(char *text) - free acl_to_text
2414 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
2415 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
2416 +
2417 +*/
2418 +
2419 +#if defined(HAVE_POSIX_ACLS)
2420 +
2421 +/* Identity mapping - easy. */
2422 +
2423 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2424 +{
2425 +       return acl_get_entry( the_acl, entry_id, entry_p);
2426 +}
2427 +
2428 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2429 +{
2430 +       return acl_get_tag_type( entry_d, tag_type_p);
2431 +}
2432 +
2433 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2434 +{
2435 +       return acl_get_permset( entry_d, permset_p);
2436 +}
2437 +
2438 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2439 +{
2440 +       return acl_get_qualifier( entry_d);
2441 +}
2442 +
2443 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2444 +{
2445 +       return acl_get_file( path_p, type);
2446 +}
2447 +
2448 +SMB_ACL_T sys_acl_get_fd(int fd)
2449 +{
2450 +       return acl_get_fd(fd);
2451 +}
2452 +
2453 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
2454 +{
2455 +       return acl_clear_perms(permset);
2456 +}
2457 +
2458 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2459 +{
2460 +       return acl_add_perm(permset, perm);
2461 +}
2462 +
2463 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2464 +{
2465 +#if defined(HAVE_ACL_GET_PERM_NP)
2466 +       /*
2467 +        * Required for TrustedBSD-based ACL implementations where
2468 +        * non-POSIX.1e functions are denoted by a _np (non-portable)
2469 +        * suffix.
2470 +        */
2471 +       return acl_get_perm_np(permset, perm);
2472 +#else
2473 +       return acl_get_perm(permset, perm);
2474 +#endif
2475 +}
2476 +
2477 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
2478 +{
2479 +       return acl_to_text( the_acl, plen);
2480 +}
2481 +
2482 +SMB_ACL_T sys_acl_init( int count)
2483 +{
2484 +       return acl_init(count);
2485 +}
2486 +
2487 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2488 +{
2489 +       return acl_create_entry(pacl, pentry);
2490 +}
2491 +
2492 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2493 +{
2494 +       return acl_set_tag_type(entry, tagtype);
2495 +}
2496 +
2497 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2498 +{
2499 +       return acl_set_qualifier(entry, qual);
2500 +}
2501 +
2502 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2503 +{
2504 +       return acl_set_permset(entry, permset);
2505 +}
2506 +
2507 +int sys_acl_valid( SMB_ACL_T theacl )
2508 +{
2509 +       return acl_valid(theacl);
2510 +}
2511 +
2512 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2513 +{
2514 +       return acl_set_file(name, acltype, theacl);
2515 +}
2516 +
2517 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2518 +{
2519 +       return acl_set_fd(fd, theacl);
2520 +}
2521 +
2522 +int sys_acl_delete_def_file(const char *name)
2523 +{
2524 +       return acl_delete_def_file(name);
2525 +}
2526 +
2527 +int sys_acl_free_text(char *text)
2528 +{
2529 +       return acl_free(text);
2530 +}
2531 +
2532 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
2533 +{
2534 +       return acl_free(the_acl);
2535 +}
2536 +
2537 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
2538 +{
2539 +       return acl_free(qual);
2540 +}
2541 +
2542 +#elif defined(HAVE_TRU64_ACLS)
2543 +/*
2544 + * The interface to DEC/Compaq Tru64 UNIX ACLs
2545 + * is based on Draft 13 of the POSIX spec which is
2546 + * slightly different from the Draft 16 interface.
2547 + * 
2548 + * Also, some of the permset manipulation functions
2549 + * such as acl_clear_perm() and acl_add_perm() appear
2550 + * to be broken on Tru64 so we have to manipulate
2551 + * the permission bits in the permset directly.
2552 + */
2553 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2554 +{
2555 +       SMB_ACL_ENTRY_T entry;
2556 +
2557 +       if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
2558 +               return -1;
2559 +       }
2560 +
2561 +       errno = 0;
2562 +       if ((entry = acl_get_entry(the_acl)) != NULL) {
2563 +               *entry_p = entry;
2564 +               return 1;
2565 +       }
2566 +
2567 +       return errno ? -1 : 0;
2568 +}
2569 +
2570 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2571 +{
2572 +       return acl_get_tag_type( entry_d, tag_type_p);
2573 +}
2574 +
2575 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2576 +{
2577 +       return acl_get_permset( entry_d, permset_p);
2578 +}
2579 +
2580 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2581 +{
2582 +       return acl_get_qualifier( entry_d);
2583 +}
2584 +
2585 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2586 +{
2587 +       return acl_get_file((char *)path_p, type);
2588 +}
2589 +
2590 +SMB_ACL_T sys_acl_get_fd(int fd)
2591 +{
2592 +       return acl_get_fd(fd, ACL_TYPE_ACCESS);
2593 +}
2594 +
2595 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
2596 +{
2597 +       *permset = 0;           /* acl_clear_perm() is broken on Tru64  */
2598 +
2599 +       return 0;
2600 +}
2601 +
2602 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2603 +{
2604 +       if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
2605 +               errno = EINVAL;
2606 +               return -1;
2607 +       }
2608 +
2609 +       *permset |= perm;       /* acl_add_perm() is broken on Tru64    */
2610 +
2611 +       return 0;
2612 +}
2613 +
2614 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2615 +{
2616 +       return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
2617 +}
2618 +
2619 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
2620 +{
2621 +       return acl_to_text( the_acl, plen);
2622 +}
2623 +
2624 +SMB_ACL_T sys_acl_init( int count)
2625 +{
2626 +       return acl_init(count);
2627 +}
2628 +
2629 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2630 +{
2631 +       SMB_ACL_ENTRY_T entry;
2632 +
2633 +       if ((entry = acl_create_entry(pacl)) == NULL) {
2634 +               return -1;
2635 +       }
2636 +
2637 +       *pentry = entry;
2638 +       return 0;
2639 +}
2640 +
2641 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2642 +{
2643 +       return acl_set_tag_type(entry, tagtype);
2644 +}
2645 +
2646 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2647 +{
2648 +       return acl_set_qualifier(entry, qual);
2649 +}
2650 +
2651 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2652 +{
2653 +       return acl_set_permset(entry, permset);
2654 +}
2655 +
2656 +int sys_acl_valid( SMB_ACL_T theacl )
2657 +{
2658 +       acl_entry_t     entry;
2659 +
2660 +       return acl_valid(theacl, &entry);
2661 +}
2662 +
2663 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2664 +{
2665 +       return acl_set_file((char *)name, acltype, theacl);
2666 +}
2667 +
2668 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2669 +{
2670 +       return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
2671 +}
2672 +
2673 +int sys_acl_delete_def_file(const char *name)
2674 +{
2675 +       return acl_delete_def_file((char *)name);
2676 +}
2677 +
2678 +int sys_acl_free_text(char *text)
2679 +{
2680 +       /*
2681 +        * (void) cast and explicit return 0 are for DEC UNIX
2682 +        *  which just #defines acl_free_text() to be free()
2683 +        */
2684 +       (void) acl_free_text(text);
2685 +       return 0;
2686 +}
2687 +
2688 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
2689 +{
2690 +       return acl_free(the_acl);
2691 +}
2692 +
2693 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2694 +{
2695 +       return acl_free_qualifier(qual, tagtype);
2696 +}
2697 +
2698 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
2699 +
2700 +/*
2701 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
2702 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
2703 + */
2704 +
2705 +/*
2706 + * Note that while this code implements sufficient functionality
2707 + * to support the sys_acl_* interfaces it does not provide all
2708 + * of the semantics of the POSIX ACL interfaces.
2709 + *
2710 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2711 + * from a call to sys_acl_get_entry() should not be assumed to be
2712 + * valid after calling any of the following functions, which may
2713 + * reorder the entries in the ACL.
2714 + *
2715 + *     sys_acl_valid()
2716 + *     sys_acl_set_file()
2717 + *     sys_acl_set_fd()
2718 + */
2719 +
2720 +/*
2721 + * The only difference between Solaris and UnixWare / OpenUNIX is
2722 + * that the #defines for the ACL operations have different names
2723 + */
2724 +#if defined(HAVE_UNIXWARE_ACLS)
2725 +
2726 +#define        SETACL          ACL_SET
2727 +#define        GETACL          ACL_GET
2728 +#define        GETACLCNT       ACL_CNT
2729 +
2730 +#endif
2731 +
2732 +
2733 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2734 +{
2735 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2736 +               errno = EINVAL;
2737 +               return -1;
2738 +       }
2739 +
2740 +       if (entry_p == NULL) {
2741 +               errno = EINVAL;
2742 +               return -1;
2743 +       }
2744 +
2745 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
2746 +               acl_d->next = 0;
2747 +       }
2748 +
2749 +       if (acl_d->next < 0) {
2750 +               errno = EINVAL;
2751 +               return -1;
2752 +       }
2753 +
2754 +       if (acl_d->next >= acl_d->count) {
2755 +               return 0;
2756 +       }
2757 +
2758 +       *entry_p = &acl_d->acl[acl_d->next++];
2759 +
2760 +       return 1;
2761 +}
2762 +
2763 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2764 +{
2765 +       *type_p = entry_d->a_type;
2766 +
2767 +       return 0;
2768 +}
2769 +
2770 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2771 +{
2772 +       *permset_p = &entry_d->a_perm;
2773 +
2774 +       return 0;
2775 +}
2776 +
2777 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2778 +{
2779 +       if (entry_d->a_type != SMB_ACL_USER
2780 +           && entry_d->a_type != SMB_ACL_GROUP) {
2781 +               errno = EINVAL;
2782 +               return NULL;
2783 +       }
2784 +
2785 +       return &entry_d->a_id;
2786 +}
2787 +
2788 +/*
2789 + * There is no way of knowing what size the ACL returned by
2790 + * GETACL will be unless you first call GETACLCNT which means
2791 + * making an additional system call.
2792 + *
2793 + * In the hope of avoiding the cost of the additional system
2794 + * call in most cases, we initially allocate enough space for
2795 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2796 + * be too small then we use GETACLCNT to find out the actual
2797 + * size, reallocate the ACL buffer, and then call GETACL again.
2798 + */
2799 +
2800 +#define        INITIAL_ACL_SIZE        16
2801 +
2802 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2803 +{
2804 +       SMB_ACL_T       acl_d;
2805 +       int             count;          /* # of ACL entries allocated   */
2806 +       int             naccess;        /* # of access ACL entries      */
2807 +       int             ndefault;       /* # of default ACL entries     */
2808 +
2809 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2810 +               errno = EINVAL;
2811 +               return NULL;
2812 +       }
2813 +
2814 +       count = INITIAL_ACL_SIZE;
2815 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2816 +               return NULL;
2817 +       }
2818 +
2819 +       /*
2820 +        * If there isn't enough space for the ACL entries we use
2821 +        * GETACLCNT to determine the actual number of ACL entries
2822 +        * reallocate and try again. This is in a loop because it
2823 +        * is possible that someone else could modify the ACL and
2824 +        * increase the number of entries between the call to
2825 +        * GETACLCNT and the call to GETACL.
2826 +        */
2827 +       while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
2828 +           && errno == ENOSPC) {
2829 +
2830 +               sys_acl_free_acl(acl_d);
2831 +
2832 +               if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
2833 +                       return NULL;
2834 +               }
2835 +
2836 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2837 +                       return NULL;
2838 +               }
2839 +       }
2840 +
2841 +       if (count < 0) {
2842 +               sys_acl_free_acl(acl_d);
2843 +               return NULL;
2844 +       }
2845 +
2846 +       /*
2847 +        * calculate the number of access and default ACL entries
2848 +        *
2849 +        * Note: we assume that the acl() system call returned a
2850 +        * well formed ACL which is sorted so that all of the
2851 +        * access ACL entries preceed any default ACL entries
2852 +        */
2853 +       for (naccess = 0; naccess < count; naccess++) {
2854 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2855 +                       break;
2856 +       }
2857 +       ndefault = count - naccess;
2858 +       
2859 +       /*
2860 +        * if the caller wants the default ACL we have to copy
2861 +        * the entries down to the start of the acl[] buffer
2862 +        * and mask out the ACL_DEFAULT flag from the type field
2863 +        */
2864 +       if (type == SMB_ACL_TYPE_DEFAULT) {
2865 +               int     i, j;
2866 +
2867 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
2868 +                       acl_d->acl[i] = acl_d->acl[j];
2869 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2870 +               }
2871 +
2872 +               acl_d->count = ndefault;
2873 +       } else {
2874 +               acl_d->count = naccess;
2875 +       }
2876 +
2877 +       return acl_d;
2878 +}
2879 +
2880 +SMB_ACL_T sys_acl_get_fd(int fd)
2881 +{
2882 +       SMB_ACL_T       acl_d;
2883 +       int             count;          /* # of ACL entries allocated   */
2884 +       int             naccess;        /* # of access ACL entries      */
2885 +
2886 +       count = INITIAL_ACL_SIZE;
2887 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2888 +               return NULL;
2889 +       }
2890 +
2891 +       while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2892 +           && errno == ENOSPC) {
2893 +
2894 +               sys_acl_free_acl(acl_d);
2895 +
2896 +               if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2897 +                       return NULL;
2898 +               }
2899 +
2900 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2901 +                       return NULL;
2902 +               }
2903 +       }
2904 +
2905 +       if (count < 0) {
2906 +               sys_acl_free_acl(acl_d);
2907 +               return NULL;
2908 +       }
2909 +
2910 +       /*
2911 +        * calculate the number of access ACL entries
2912 +        */
2913 +       for (naccess = 0; naccess < count; naccess++) {
2914 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2915 +                       break;
2916 +       }
2917 +       
2918 +       acl_d->count = naccess;
2919 +
2920 +       return acl_d;
2921 +}
2922 +
2923 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2924 +{
2925 +       *permset_d = 0;
2926 +
2927 +       return 0;
2928 +}
2929 +
2930 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2931 +{
2932 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2933 +           && perm != SMB_ACL_EXECUTE) {
2934 +               errno = EINVAL;
2935 +               return -1;
2936 +       }
2937 +
2938 +       if (permset_d == NULL) {
2939 +               errno = EINVAL;
2940 +               return -1;
2941 +       }
2942 +
2943 +       *permset_d |= perm;
2944 +
2945 +       return 0;
2946 +}
2947 +
2948 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2949 +{
2950 +       return *permset_d & perm;
2951 +}
2952 +
2953 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2954 +{
2955 +       int     i;
2956 +       int     len, maxlen;
2957 +       char    *text;
2958 +
2959 +       /*
2960 +        * use an initial estimate of 20 bytes per ACL entry
2961 +        * when allocating memory for the text representation
2962 +        * of the ACL
2963 +        */
2964 +       len     = 0;
2965 +       maxlen  = 20 * acl_d->count;
2966 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
2967 +               errno = ENOMEM;
2968 +               return NULL;
2969 +       }
2970 +
2971 +       for (i = 0; i < acl_d->count; i++) {
2972 +               struct acl      *ap     = &acl_d->acl[i];
2973 +               struct group    *gr;
2974 +               char            tagbuf[12];
2975 +               char            idbuf[12];
2976 +               char            *tag;
2977 +               char            *id     = "";
2978 +               char            perms[4];
2979 +               int             nbytes;
2980 +
2981 +               switch (ap->a_type) {
2982 +                       /*
2983 +                        * for debugging purposes it's probably more
2984 +                        * useful to dump unknown tag types rather
2985 +                        * than just returning an error
2986 +                        */
2987 +                       default:
2988 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2989 +                                       ap->a_type);
2990 +                               tag = tagbuf;
2991 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2992 +                                       (long)ap->a_id);
2993 +                               id = idbuf;
2994 +                               break;
2995 +
2996 +                       case SMB_ACL_USER:
2997 +                               id = uidtoname(ap->a_id);
2998 +                       case SMB_ACL_USER_OBJ:
2999 +                               tag = "user";
3000 +                               break;
3001 +
3002 +                       case SMB_ACL_GROUP:
3003 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
3004 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
3005 +                                               (long)ap->a_id);
3006 +                                       id = idbuf;
3007 +                               } else {
3008 +                                       id = gr->gr_name;
3009 +                               }
3010 +                       case SMB_ACL_GROUP_OBJ:
3011 +                               tag = "group";
3012 +                               break;
3013 +
3014 +                       case SMB_ACL_OTHER:
3015 +                               tag = "other";
3016 +                               break;
3017 +
3018 +                       case SMB_ACL_MASK:
3019 +                               tag = "mask";
3020 +                               break;
3021 +
3022 +               }
3023 +
3024 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
3025 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
3026 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
3027 +               perms[3] = '\0';
3028 +
3029 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
3030 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
3031 +
3032 +               /*
3033 +                * If this entry would overflow the buffer
3034 +                * allocate enough additional memory for this
3035 +                * entry and an estimate of another 20 bytes
3036 +                * for each entry still to be processed
3037 +                */
3038 +               if ((len + nbytes) > maxlen) {
3039 +                       char *oldtext = text;
3040 +
3041 +                       maxlen += nbytes + 20 * (acl_d->count - i);
3042 +
3043 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
3044 +                               SAFE_FREE(oldtext);
3045 +                               errno = ENOMEM;
3046 +                               return NULL;
3047 +                       }
3048 +               }
3049 +
3050 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
3051 +               len += nbytes - 1;
3052 +       }
3053 +
3054 +       if (len_p)
3055 +               *len_p = len;
3056 +
3057 +       return text;
3058 +}
3059 +
3060 +SMB_ACL_T sys_acl_init(int count)
3061 +{
3062 +       SMB_ACL_T       a;
3063 +
3064 +       if (count < 0) {
3065 +               errno = EINVAL;
3066 +               return NULL;
3067 +       }
3068 +
3069 +       /*
3070 +        * note that since the definition of the structure pointed
3071 +        * to by the SMB_ACL_T includes the first element of the
3072 +        * acl[] array, this actually allocates an ACL with room
3073 +        * for (count+1) entries
3074 +        */
3075 +       if ((a = (SMB_ACL_T)SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
3076 +               errno = ENOMEM;
3077 +               return NULL;
3078 +       }
3079 +
3080 +       a->size = count + 1;
3081 +       a->count = 0;
3082 +       a->next = -1;
3083 +
3084 +       return a;
3085 +}
3086 +
3087 +
3088 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3089 +{
3090 +       SMB_ACL_T       acl_d;
3091 +       SMB_ACL_ENTRY_T entry_d;
3092 +
3093 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3094 +               errno = EINVAL;
3095 +               return -1;
3096 +       }
3097 +
3098 +       if (acl_d->count >= acl_d->size) {
3099 +               errno = ENOSPC;
3100 +               return -1;
3101 +       }
3102 +
3103 +       entry_d         = &acl_d->acl[acl_d->count++];
3104 +       entry_d->a_type = 0;
3105 +       entry_d->a_id   = -1;
3106 +       entry_d->a_perm = 0;
3107 +       *entry_p        = entry_d;
3108 +
3109 +       return 0;
3110 +}
3111 +
3112 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3113 +{
3114 +       switch (tag_type) {
3115 +               case SMB_ACL_USER:
3116 +               case SMB_ACL_USER_OBJ:
3117 +               case SMB_ACL_GROUP:
3118 +               case SMB_ACL_GROUP_OBJ:
3119 +               case SMB_ACL_OTHER:
3120 +               case SMB_ACL_MASK:
3121 +                       entry_d->a_type = tag_type;
3122 +                       break;
3123 +               default:
3124 +                       errno = EINVAL;
3125 +                       return -1;
3126 +       }
3127 +
3128 +       return 0;
3129 +}
3130 +
3131 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3132 +{
3133 +       if (entry_d->a_type != SMB_ACL_GROUP
3134 +           && entry_d->a_type != SMB_ACL_USER) {
3135 +               errno = EINVAL;
3136 +               return -1;
3137 +       }
3138 +
3139 +       entry_d->a_id = *((id_t *)qual_p);
3140 +
3141 +       return 0;
3142 +}
3143 +
3144 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3145 +{
3146 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3147 +               return EINVAL;
3148 +       }
3149 +
3150 +       entry_d->a_perm = *permset_d;
3151 +
3152 +       return 0;
3153 +}
3154 +
3155 +/*
3156 + * sort the ACL and check it for validity
3157 + *
3158 + * if it's a minimal ACL with only 4 entries then we
3159 + * need to recalculate the mask permissions to make
3160 + * sure that they are the same as the GROUP_OBJ
3161 + * permissions as required by the UnixWare acl() system call.
3162 + *
3163 + * (note: since POSIX allows minimal ACLs which only contain
3164 + * 3 entries - ie there is no mask entry - we should, in theory,
3165 + * check for this and add a mask entry if necessary - however
3166 + * we "know" that the caller of this interface always specifies
3167 + * a mask so, in practice "this never happens" (tm) - if it *does*
3168 + * happen aclsort() will fail and return an error and someone will
3169 + * have to fix it ...)
3170 + */
3171 +
3172 +static int acl_sort(SMB_ACL_T acl_d)
3173 +{
3174 +       int     fixmask = (acl_d->count <= 4);
3175 +
3176 +       if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
3177 +               errno = EINVAL;
3178 +               return -1;
3179 +       }
3180 +       return 0;
3181 +}
3182
3183 +int sys_acl_valid(SMB_ACL_T acl_d)
3184 +{
3185 +       return acl_sort(acl_d);
3186 +}
3187 +
3188 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3189 +{
3190 +       struct stat     s;
3191 +       struct acl      *acl_p;
3192 +       int             acl_count;
3193 +       struct acl      *acl_buf        = NULL;
3194 +       int             ret;
3195 +
3196 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3197 +               errno = EINVAL;
3198 +               return -1;
3199 +       }
3200 +
3201 +       if (acl_sort(acl_d) != 0) {
3202 +               return -1;
3203 +       }
3204 +
3205 +       acl_p           = &acl_d->acl[0];
3206 +       acl_count       = acl_d->count;
3207 +
3208 +       /*
3209 +        * if it's a directory there is extra work to do
3210 +        * since the acl() system call will replace both
3211 +        * the access ACLs and the default ACLs (if any)
3212 +        */
3213 +       if (stat(name, &s) != 0) {
3214 +               return -1;
3215 +       }
3216 +       if (S_ISDIR(s.st_mode)) {
3217 +               SMB_ACL_T       acc_acl;
3218 +               SMB_ACL_T       def_acl;
3219 +               SMB_ACL_T       tmp_acl;
3220 +               int             i;
3221 +
3222 +               if (type == SMB_ACL_TYPE_ACCESS) {
3223 +                       acc_acl = acl_d;
3224 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3225 +
3226 +               } else {
3227 +                       def_acl = acl_d;
3228 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3229 +               }
3230 +
3231 +               if (tmp_acl == NULL) {
3232 +                       return -1;
3233 +               }
3234 +
3235 +               /*
3236 +                * allocate a temporary buffer for the complete ACL
3237 +                */
3238 +               acl_count = acc_acl->count + def_acl->count;
3239 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
3240 +
3241 +               if (acl_buf == NULL) {
3242 +                       sys_acl_free_acl(tmp_acl);
3243 +                       errno = ENOMEM;
3244 +                       return -1;
3245 +               }
3246 +
3247 +               /*
3248 +                * copy the access control and default entries into the buffer
3249 +                */
3250 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
3251 +                       acc_acl->count * sizeof(acl_buf[0]));
3252 +
3253 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3254 +                       def_acl->count * sizeof(acl_buf[0]));
3255 +
3256 +               /*
3257 +                * set the ACL_DEFAULT flag on the default entries
3258 +                */
3259 +               for (i = acc_acl->count; i < acl_count; i++) {
3260 +                       acl_buf[i].a_type |= ACL_DEFAULT;
3261 +               }
3262 +
3263 +               sys_acl_free_acl(tmp_acl);
3264 +
3265 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
3266 +               errno = EINVAL;
3267 +               return -1;
3268 +       }
3269 +
3270 +       ret = acl(name, SETACL, acl_count, acl_p);
3271 +
3272 +       SAFE_FREE(acl_buf);
3273 +
3274 +       return ret;
3275 +}
3276 +
3277 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3278 +{
3279 +       if (acl_sort(acl_d) != 0) {
3280 +               return -1;
3281 +       }
3282 +
3283 +       return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
3284 +}
3285 +
3286 +int sys_acl_delete_def_file(const char *path)
3287 +{
3288 +       SMB_ACL_T       acl_d;
3289 +       int             ret;
3290 +
3291 +       /*
3292 +        * fetching the access ACL and rewriting it has
3293 +        * the effect of deleting the default ACL
3294 +        */
3295 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3296 +               return -1;
3297 +       }
3298 +
3299 +       ret = acl(path, SETACL, acl_d->count, acl_d->acl);
3300 +
3301 +       sys_acl_free_acl(acl_d);
3302 +       
3303 +       return ret;
3304 +}
3305 +
3306 +int sys_acl_free_text(char *text)
3307 +{
3308 +       SAFE_FREE(text);
3309 +       return 0;
3310 +}
3311 +
3312 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
3313 +{
3314 +       SAFE_FREE(acl_d);
3315 +       return 0;
3316 +}
3317 +
3318 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
3319 +{
3320 +       return 0;
3321 +}
3322 +
3323 +#elif defined(HAVE_HPUX_ACLS)
3324 +#include <dl.h>
3325 +
3326 +/*
3327 + * Based on the Solaris/SCO code - with modifications.
3328 + */
3329 +
3330 +/*
3331 + * Note that while this code implements sufficient functionality
3332 + * to support the sys_acl_* interfaces it does not provide all
3333 + * of the semantics of the POSIX ACL interfaces.
3334 + *
3335 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
3336 + * from a call to sys_acl_get_entry() should not be assumed to be
3337 + * valid after calling any of the following functions, which may
3338 + * reorder the entries in the ACL.
3339 + *
3340 + *     sys_acl_valid()
3341 + *     sys_acl_set_file()
3342 + *     sys_acl_set_fd()
3343 + */
3344 +
3345 +/* This checks if the POSIX ACL system call is defined */
3346 +/* which basically corresponds to whether JFS 3.3 or   */
3347 +/* higher is installed. If acl() was called when it    */
3348 +/* isn't defined, it causes the process to core dump   */
3349 +/* so it is important to check this and avoid acl()    */
3350 +/* calls if it isn't there.                            */
3351 +
3352 +static BOOL hpux_acl_call_presence(void)
3353 +{
3354 +
3355 +       shl_t handle = NULL;
3356 +       void *value;
3357 +       int ret_val=0;
3358 +       static BOOL already_checked=0;
3359 +
3360 +       if(already_checked)
3361 +               return True;
3362 +
3363 +
3364 +       ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
3365 +
3366 +       if(ret_val != 0) {
3367 +               DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
3368 +                       ret_val, errno, strerror(errno)));
3369 +               DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
3370 +               return False;
3371 +       }
3372 +
3373 +       DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
3374 +
3375 +       already_checked = True;
3376 +       return True;
3377 +}
3378 +
3379 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3380 +{
3381 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3382 +               errno = EINVAL;
3383 +               return -1;
3384 +       }
3385 +
3386 +       if (entry_p == NULL) {
3387 +               errno = EINVAL;
3388 +               return -1;
3389 +       }
3390 +
3391 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
3392 +               acl_d->next = 0;
3393 +       }
3394 +
3395 +       if (acl_d->next < 0) {
3396 +               errno = EINVAL;
3397 +               return -1;
3398 +       }
3399 +
3400 +       if (acl_d->next >= acl_d->count) {
3401 +               return 0;
3402 +       }
3403 +
3404 +       *entry_p = &acl_d->acl[acl_d->next++];
3405 +
3406 +       return 1;
3407 +}
3408 +
3409 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
3410 +{
3411 +       *type_p = entry_d->a_type;
3412 +
3413 +       return 0;
3414 +}
3415 +
3416 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3417 +{
3418 +       *permset_p = &entry_d->a_perm;
3419 +
3420 +       return 0;
3421 +}
3422 +
3423 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3424 +{
3425 +       if (entry_d->a_type != SMB_ACL_USER
3426 +           && entry_d->a_type != SMB_ACL_GROUP) {
3427 +               errno = EINVAL;
3428 +               return NULL;
3429 +       }
3430 +
3431 +       return &entry_d->a_id;
3432 +}
3433 +
3434 +/*
3435 + * There is no way of knowing what size the ACL returned by
3436 + * ACL_GET will be unless you first call ACL_CNT which means
3437 + * making an additional system call.
3438 + *
3439 + * In the hope of avoiding the cost of the additional system
3440 + * call in most cases, we initially allocate enough space for
3441 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
3442 + * be too small then we use ACL_CNT to find out the actual
3443 + * size, reallocate the ACL buffer, and then call ACL_GET again.
3444 + */
3445 +
3446 +#define        INITIAL_ACL_SIZE        16
3447 +
3448 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3449 +{
3450 +       SMB_ACL_T       acl_d;
3451 +       int             count;          /* # of ACL entries allocated   */
3452 +       int             naccess;        /* # of access ACL entries      */
3453 +       int             ndefault;       /* # of default ACL entries     */
3454 +
3455 +       if(hpux_acl_call_presence() == False) {
3456 +               /* Looks like we don't have the acl() system call on HPUX. 
3457 +                * May be the system doesn't have the latest version of JFS.
3458 +                */
3459 +               return NULL; 
3460 +       }
3461 +
3462 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3463 +               errno = EINVAL;
3464 +               return NULL;
3465 +       }
3466 +
3467 +       count = INITIAL_ACL_SIZE;
3468 +       if ((acl_d = sys_acl_init(count)) == NULL) {
3469 +               return NULL;
3470 +       }
3471 +
3472 +       /*
3473 +        * If there isn't enough space for the ACL entries we use
3474 +        * ACL_CNT to determine the actual number of ACL entries
3475 +        * reallocate and try again. This is in a loop because it
3476 +        * is possible that someone else could modify the ACL and
3477 +        * increase the number of entries between the call to
3478 +        * ACL_CNT and the call to ACL_GET.
3479 +        */
3480 +       while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
3481 +
3482 +               sys_acl_free_acl(acl_d);
3483 +
3484 +               if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
3485 +                       return NULL;
3486 +               }
3487 +
3488 +               if ((acl_d = sys_acl_init(count)) == NULL) {
3489 +                       return NULL;
3490 +               }
3491 +       }
3492 +
3493 +       if (count < 0) {
3494 +               sys_acl_free_acl(acl_d);
3495 +               return NULL;
3496 +       }
3497 +
3498 +       /*
3499 +        * calculate the number of access and default ACL entries
3500 +        *
3501 +        * Note: we assume that the acl() system call returned a
3502 +        * well formed ACL which is sorted so that all of the
3503 +        * access ACL entries preceed any default ACL entries
3504 +        */
3505 +       for (naccess = 0; naccess < count; naccess++) {
3506 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
3507 +                       break;
3508 +       }
3509 +       ndefault = count - naccess;
3510 +       
3511 +       /*
3512 +        * if the caller wants the default ACL we have to copy
3513 +        * the entries down to the start of the acl[] buffer
3514 +        * and mask out the ACL_DEFAULT flag from the type field
3515 +        */
3516 +       if (type == SMB_ACL_TYPE_DEFAULT) {
3517 +               int     i, j;
3518 +
3519 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
3520 +                       acl_d->acl[i] = acl_d->acl[j];
3521 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
3522 +               }
3523 +
3524 +               acl_d->count = ndefault;
3525 +       } else {
3526 +               acl_d->count = naccess;
3527 +       }
3528 +
3529 +       return acl_d;
3530 +}
3531 +
3532 +SMB_ACL_T sys_acl_get_fd(int fd)
3533 +{
3534 +       /*
3535 +        * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3536 +        */
3537 +
3538 +       files_struct *fsp = file_find_fd(fd);
3539 +
3540 +       if (fsp == NULL) {
3541 +               errno = EBADF;
3542 +               return NULL;
3543 +       }
3544 +
3545 +       /*
3546 +        * We know we're in the same conn context. So we
3547 +        * can use the relative path.
3548 +        */
3549 +
3550 +       return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
3551 +}
3552 +
3553 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3554 +{
3555 +       *permset_d = 0;
3556 +
3557 +       return 0;
3558 +}
3559 +
3560 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3561 +{
3562 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3563 +           && perm != SMB_ACL_EXECUTE) {
3564 +               errno = EINVAL;
3565 +               return -1;
3566 +       }
3567 +
3568 +       if (permset_d == NULL) {
3569 +               errno = EINVAL;
3570 +               return -1;
3571 +       }
3572 +
3573 +       *permset_d |= perm;
3574 +
3575 +       return 0;
3576 +}
3577 +
3578 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3579 +{
3580 +       return *permset_d & perm;
3581 +}
3582 +
3583 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3584 +{
3585 +       int     i;
3586 +       int     len, maxlen;
3587 +       char    *text;
3588 +
3589 +       /*
3590 +        * use an initial estimate of 20 bytes per ACL entry
3591 +        * when allocating memory for the text representation
3592 +        * of the ACL
3593 +        */
3594 +       len     = 0;
3595 +       maxlen  = 20 * acl_d->count;
3596 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
3597 +               errno = ENOMEM;
3598 +               return NULL;
3599 +       }
3600 +
3601 +       for (i = 0; i < acl_d->count; i++) {
3602 +               struct acl      *ap     = &acl_d->acl[i];
3603 +               struct group    *gr;
3604 +               char            tagbuf[12];
3605 +               char            idbuf[12];
3606 +               char            *tag;
3607 +               char            *id     = "";
3608 +               char            perms[4];
3609 +               int             nbytes;
3610 +
3611 +               switch (ap->a_type) {
3612 +                       /*
3613 +                        * for debugging purposes it's probably more
3614 +                        * useful to dump unknown tag types rather
3615 +                        * than just returning an error
3616 +                        */
3617 +                       default:
3618 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
3619 +                                       ap->a_type);
3620 +                               tag = tagbuf;
3621 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
3622 +                                       (long)ap->a_id);
3623 +                               id = idbuf;
3624 +                               break;
3625 +
3626 +                       case SMB_ACL_USER:
3627 +                               id = uidtoname(ap->a_id);
3628 +                       case SMB_ACL_USER_OBJ:
3629 +                               tag = "user";
3630 +                               break;
3631 +
3632 +                       case SMB_ACL_GROUP:
3633 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
3634 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
3635 +                                               (long)ap->a_id);
3636 +                                       id = idbuf;
3637 +                               } else {
3638 +                                       id = gr->gr_name;
3639 +                               }
3640 +                       case SMB_ACL_GROUP_OBJ:
3641 +                               tag = "group";
3642 +                               break;
3643 +
3644 +                       case SMB_ACL_OTHER:
3645 +                               tag = "other";
3646 +                               break;
3647 +
3648 +                       case SMB_ACL_MASK:
3649 +                               tag = "mask";
3650 +                               break;
3651 +
3652 +               }
3653 +
3654 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
3655 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
3656 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
3657 +               perms[3] = '\0';
3658 +
3659 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
3660 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
3661 +
3662 +               /*
3663 +                * If this entry would overflow the buffer
3664 +                * allocate enough additional memory for this
3665 +                * entry and an estimate of another 20 bytes
3666 +                * for each entry still to be processed
3667 +                */
3668 +               if ((len + nbytes) > maxlen) {
3669 +                       char *oldtext = text;
3670 +
3671 +                       maxlen += nbytes + 20 * (acl_d->count - i);
3672 +
3673 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
3674 +                               free(oldtext);
3675 +                               errno = ENOMEM;
3676 +                               return NULL;
3677 +                       }
3678 +               }
3679 +
3680 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
3681 +               len += nbytes - 1;
3682 +       }
3683 +
3684 +       if (len_p)
3685 +               *len_p = len;
3686 +
3687 +       return text;
3688 +}
3689 +
3690 +SMB_ACL_T sys_acl_init(int count)
3691 +{
3692 +       SMB_ACL_T       a;
3693 +
3694 +       if (count < 0) {
3695 +               errno = EINVAL;
3696 +               return NULL;
3697 +       }
3698 +
3699 +       /*
3700 +        * note that since the definition of the structure pointed
3701 +        * to by the SMB_ACL_T includes the first element of the
3702 +        * acl[] array, this actually allocates an ACL with room
3703 +        * for (count+1) entries
3704 +        */
3705 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
3706 +               errno = ENOMEM;
3707 +               return NULL;
3708 +       }
3709 +
3710 +       a->size = count + 1;
3711 +       a->count = 0;
3712 +       a->next = -1;
3713 +
3714 +       return a;
3715 +}
3716 +
3717 +
3718 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3719 +{
3720 +       SMB_ACL_T       acl_d;
3721 +       SMB_ACL_ENTRY_T entry_d;
3722 +
3723 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3724 +               errno = EINVAL;
3725 +               return -1;
3726 +       }
3727 +
3728 +       if (acl_d->count >= acl_d->size) {
3729 +               errno = ENOSPC;
3730 +               return -1;
3731 +       }
3732 +
3733 +       entry_d         = &acl_d->acl[acl_d->count++];
3734 +       entry_d->a_type = 0;
3735 +       entry_d->a_id   = -1;
3736 +       entry_d->a_perm = 0;
3737 +       *entry_p        = entry_d;
3738 +
3739 +       return 0;
3740 +}
3741 +
3742 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3743 +{
3744 +       switch (tag_type) {
3745 +               case SMB_ACL_USER:
3746 +               case SMB_ACL_USER_OBJ:
3747 +               case SMB_ACL_GROUP:
3748 +               case SMB_ACL_GROUP_OBJ:
3749 +               case SMB_ACL_OTHER:
3750 +               case SMB_ACL_MASK:
3751 +                       entry_d->a_type = tag_type;
3752 +                       break;
3753 +               default:
3754 +                       errno = EINVAL;
3755 +                       return -1;
3756 +       }
3757 +
3758 +       return 0;
3759 +}
3760 +
3761 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3762 +{
3763 +       if (entry_d->a_type != SMB_ACL_GROUP
3764 +           && entry_d->a_type != SMB_ACL_USER) {
3765 +               errno = EINVAL;
3766 +               return -1;
3767 +       }
3768 +
3769 +       entry_d->a_id = *((id_t *)qual_p);
3770 +
3771 +       return 0;
3772 +}
3773 +
3774 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3775 +{
3776 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3777 +               return EINVAL;
3778 +       }
3779 +
3780 +       entry_d->a_perm = *permset_d;
3781 +
3782 +       return 0;
3783 +}
3784 +
3785 +/* Structure to capture the count for each type of ACE. */
3786 +
3787 +struct hpux_acl_types {
3788 +       int n_user;
3789 +       int n_def_user;
3790 +       int n_user_obj;
3791 +       int n_def_user_obj;
3792 +
3793 +       int n_group;
3794 +       int n_def_group;
3795 +       int n_group_obj;
3796 +       int n_def_group_obj;
3797 +
3798 +       int n_other;
3799 +       int n_other_obj;
3800 +       int n_def_other_obj;
3801 +
3802 +       int n_class_obj;
3803 +       int n_def_class_obj;
3804 +
3805 +       int n_illegal_obj;
3806 +};
3807 +
3808 +/* count_obj:
3809 + * Counts the different number of objects in a given array of ACL
3810 + * structures.
3811 + * Inputs:
3812 + *
3813 + * acl_count      - Count of ACLs in the array of ACL strucutres.
3814 + * aclp           - Array of ACL structures.
3815 + * acl_type_count - Pointer to acl_types structure. Should already be
3816 + *                  allocated.
3817 + * Output: 
3818 + *
3819 + * acl_type_count - This structure is filled up with counts of various 
3820 + *                  acl types.
3821 + */
3822 +
3823 +static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
3824 +{
3825 +       int i;
3826 +
3827 +       memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
3828 +
3829 +       for(i=0;i<acl_count;i++) {
3830 +               switch(aclp[i].a_type) {
3831 +               case USER: 
3832 +                       acl_type_count->n_user++;
3833 +                       break;
3834 +               case USER_OBJ: 
3835 +                       acl_type_count->n_user_obj++;
3836 +                       break;
3837 +               case DEF_USER_OBJ: 
3838 +                       acl_type_count->n_def_user_obj++;
3839 +                       break;
3840 +               case GROUP: 
3841 +                       acl_type_count->n_group++;
3842 +                       break;
3843 +               case GROUP_OBJ: 
3844 +                       acl_type_count->n_group_obj++;
3845 +                       break;
3846 +               case DEF_GROUP_OBJ: 
3847 +                       acl_type_count->n_def_group_obj++;
3848 +                       break;
3849 +               case OTHER_OBJ: 
3850 +                       acl_type_count->n_other_obj++;
3851 +                       break;
3852 +               case DEF_OTHER_OBJ: 
3853 +                       acl_type_count->n_def_other_obj++;
3854 +                       break;
3855 +               case CLASS_OBJ:
3856 +                       acl_type_count->n_class_obj++;
3857 +                       break;
3858 +               case DEF_CLASS_OBJ:
3859 +                       acl_type_count->n_def_class_obj++;
3860 +                       break;
3861 +               case DEF_USER:
3862 +                       acl_type_count->n_def_user++;
3863 +                       break;
3864 +               case DEF_GROUP:
3865 +                       acl_type_count->n_def_group++;
3866 +                       break;
3867 +               default: 
3868 +                       acl_type_count->n_illegal_obj++;
3869 +                       break;
3870 +               }
3871 +       }
3872 +}
3873 +
3874 +/* swap_acl_entries:  Swaps two ACL entries. 
3875 + *
3876 + * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3877 + */
3878 +
3879 +static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3880 +{
3881 +       struct acl temp_acl;
3882 +
3883 +       temp_acl.a_type = aclp0->a_type;
3884 +       temp_acl.a_id = aclp0->a_id;
3885 +       temp_acl.a_perm = aclp0->a_perm;
3886 +
3887 +       aclp0->a_type = aclp1->a_type;
3888 +       aclp0->a_id = aclp1->a_id;
3889 +       aclp0->a_perm = aclp1->a_perm;
3890 +
3891 +       aclp1->a_type = temp_acl.a_type;
3892 +       aclp1->a_id = temp_acl.a_id;
3893 +       aclp1->a_perm = temp_acl.a_perm;
3894 +}
3895 +
3896 +/* prohibited_duplicate_type
3897 + * Identifies if given ACL type can have duplicate entries or 
3898 + * not.
3899 + *
3900 + * Inputs: acl_type - ACL Type.
3901 + *
3902 + * Outputs: 
3903 + *
3904 + * Return.. 
3905 + *
3906 + * True - If the ACL type matches any of the prohibited types.
3907 + * False - If the ACL type doesn't match any of the prohibited types.
3908 + */ 
3909 +
3910 +static BOOL hpux_prohibited_duplicate_type(int acl_type)
3911 +{
3912 +       switch(acl_type) {
3913 +               case USER:
3914 +               case GROUP:
3915 +               case DEF_USER: 
3916 +               case DEF_GROUP:
3917 +                       return True;
3918 +               default:
3919 +                       return False;
3920 +       }
3921 +}
3922 +
3923 +/* get_needed_class_perm
3924 + * Returns the permissions of a ACL structure only if the ACL
3925 + * type matches one of the pre-determined types for computing 
3926 + * CLASS_OBJ permissions.
3927 + *
3928 + * Inputs: aclp - Pointer to ACL structure.
3929 + */
3930 +
3931 +static int hpux_get_needed_class_perm(struct acl *aclp)
3932 +{
3933 +       switch(aclp->a_type) {
3934 +               case USER: 
3935 +               case GROUP_OBJ: 
3936 +               case GROUP: 
3937 +               case DEF_USER_OBJ: 
3938 +               case DEF_USER:
3939 +               case DEF_GROUP_OBJ: 
3940 +               case DEF_GROUP:
3941 +               case DEF_CLASS_OBJ:
3942 +               case DEF_OTHER_OBJ: 
3943 +                       return aclp->a_perm;
3944 +               default: 
3945 +                       return 0;
3946 +       }
3947 +}
3948 +
3949 +/* acl_sort for HPUX.
3950 + * Sorts the array of ACL structures as per the description in
3951 + * aclsort man page. Refer to aclsort man page for more details
3952 + *
3953 + * Inputs:
3954 + *
3955 + * acl_count - Count of ACLs in the array of ACL structures.
3956 + * calclass  - If this is not zero, then we compute the CLASS_OBJ
3957 + *             permissions.
3958 + * aclp      - Array of ACL structures.
3959 + *
3960 + * Outputs:
3961 + *
3962 + * aclp     - Sorted array of ACL structures.
3963 + *
3964 + * Outputs:
3965 + *
3966 + * Returns 0 for success -1 for failure. Prints a message to the Samba
3967 + * debug log in case of failure.
3968 + */
3969 +
3970 +static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3971 +{
3972 +#if !defined(HAVE_HPUX_ACLSORT)
3973 +       /*
3974 +        * The aclsort() system call is availabe on the latest HPUX General
3975 +        * Patch Bundles. So for HPUX, we developed our version of acl_sort 
3976 +        * function. Because, we don't want to update to a new 
3977 +        * HPUX GR bundle just for aclsort() call.
3978 +        */
3979 +
3980 +       struct hpux_acl_types acl_obj_count;
3981 +       int n_class_obj_perm = 0;
3982 +       int i, j;
3983
3984 +       if(!acl_count) {
3985 +               DEBUG(10,("Zero acl count passed. Returning Success\n"));
3986 +               return 0;
3987 +       }
3988 +
3989 +       if(aclp == NULL) {
3990 +               DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3991 +               return -1;
3992 +       }
3993 +
3994 +       /* Count different types of ACLs in the ACLs array */
3995 +
3996 +       hpux_count_obj(acl_count, aclp, &acl_obj_count);
3997 +
3998 +       /* There should be only one entry each of type USER_OBJ, GROUP_OBJ, 
3999 +        * CLASS_OBJ and OTHER_OBJ 
4000 +        */
4001 +
4002 +       if( (acl_obj_count.n_user_obj  != 1) || 
4003 +               (acl_obj_count.n_group_obj != 1) || 
4004 +               (acl_obj_count.n_class_obj != 1) ||
4005 +               (acl_obj_count.n_other_obj != 1) 
4006 +       ) {
4007 +               DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
4008 +USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
4009 +               return -1;
4010 +       }
4011 +
4012 +       /* If any of the default objects are present, there should be only
4013 +        * one of them each.
4014 +        */
4015 +
4016 +       if( (acl_obj_count.n_def_user_obj  > 1) || (acl_obj_count.n_def_group_obj > 1) || 
4017 +                       (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
4018 +               DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
4019 +or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
4020 +               return -1;
4021 +       }
4022 +
4023 +       /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl 
4024 +        * structures.  
4025 +        *
4026 +        * Sorting crieteria - First sort by ACL type. If there are multiple entries of
4027 +        * same ACL type, sort by ACL id.
4028 +        *
4029 +        * I am using the trival kind of sorting method here because, performance isn't 
4030 +        * really effected by the ACLs feature. More over there aren't going to be more
4031 +        * than 17 entries on HPUX. 
4032 +        */
4033 +
4034 +       for(i=0; i<acl_count;i++) {
4035 +               for (j=i+1; j<acl_count; j++) {
4036 +                       if( aclp[i].a_type > aclp[j].a_type ) {
4037 +                               /* ACL entries out of order, swap them */
4038 +
4039 +                               hpux_swap_acl_entries((aclp+i), (aclp+j));
4040 +
4041 +                       } else if ( aclp[i].a_type == aclp[j].a_type ) {
4042 +
4043 +                               /* ACL entries of same type, sort by id */
4044 +
4045 +                               if(aclp[i].a_id > aclp[j].a_id) {
4046 +                                       hpux_swap_acl_entries((aclp+i), (aclp+j));
4047 +                               } else if (aclp[i].a_id == aclp[j].a_id) {
4048 +                                       /* We have a duplicate entry. */
4049 +                                       if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
4050 +                                               DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
4051 +                                                       aclp[i].a_type, aclp[i].a_id));
4052 +                                               return -1;
4053 +                                       }
4054 +                               }
4055 +
4056 +                       }
4057 +               }
4058 +       }
4059 +
4060 +       /* set the class obj permissions to the computed one. */
4061 +       if(calclass) {
4062 +               int n_class_obj_index = -1;
4063 +
4064 +               for(i=0;i<acl_count;i++) {
4065 +                       n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
4066 +
4067 +                       if(aclp[i].a_type == CLASS_OBJ)
4068 +                               n_class_obj_index = i;
4069 +               }
4070 +               aclp[n_class_obj_index].a_perm = n_class_obj_perm;
4071 +       }
4072 +
4073 +       return 0;
4074 +#else
4075 +       return aclsort(acl_count, calclass, aclp);
4076 +#endif
4077 +}
4078 +
4079 +/*
4080 + * sort the ACL and check it for validity
4081 + *
4082 + * if it's a minimal ACL with only 4 entries then we
4083 + * need to recalculate the mask permissions to make
4084 + * sure that they are the same as the GROUP_OBJ
4085 + * permissions as required by the UnixWare acl() system call.
4086 + *
4087 + * (note: since POSIX allows minimal ACLs which only contain
4088 + * 3 entries - ie there is no mask entry - we should, in theory,
4089 + * check for this and add a mask entry if necessary - however
4090 + * we "know" that the caller of this interface always specifies
4091 + * a mask so, in practice "this never happens" (tm) - if it *does*
4092 + * happen aclsort() will fail and return an error and someone will
4093 + * have to fix it ...)
4094 + */
4095 +
4096 +static int acl_sort(SMB_ACL_T acl_d)
4097 +{
4098 +       int fixmask = (acl_d->count <= 4);
4099 +
4100 +       if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
4101 +               errno = EINVAL;
4102 +               return -1;
4103 +       }
4104 +       return 0;
4105 +}
4106
4107 +int sys_acl_valid(SMB_ACL_T acl_d)
4108 +{
4109 +       return acl_sort(acl_d);
4110 +}
4111 +
4112 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
4113 +{
4114 +       struct stat     s;
4115 +       struct acl      *acl_p;
4116 +       int             acl_count;
4117 +       struct acl      *acl_buf        = NULL;
4118 +       int             ret;
4119 +
4120 +       if(hpux_acl_call_presence() == False) {
4121 +               /* Looks like we don't have the acl() system call on HPUX. 
4122 +                * May be the system doesn't have the latest version of JFS.
4123 +                */
4124 +               errno=ENOSYS;
4125 +               return -1; 
4126 +       }
4127 +
4128 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
4129 +               errno = EINVAL;
4130 +               return -1;
4131 +       }
4132 +
4133 +       if (acl_sort(acl_d) != 0) {
4134 +               return -1;
4135 +       }
4136 +
4137 +       acl_p           = &acl_d->acl[0];
4138 +       acl_count       = acl_d->count;
4139 +
4140 +       /*
4141 +        * if it's a directory there is extra work to do
4142 +        * since the acl() system call will replace both
4143 +        * the access ACLs and the default ACLs (if any)
4144 +        */
4145 +       if (stat(name, &s) != 0) {
4146 +               return -1;
4147 +       }
4148 +       if (S_ISDIR(s.st_mode)) {
4149 +               SMB_ACL_T       acc_acl;
4150 +               SMB_ACL_T       def_acl;
4151 +               SMB_ACL_T       tmp_acl;
4152 +               int             i;
4153 +
4154 +               if (type == SMB_ACL_TYPE_ACCESS) {
4155 +                       acc_acl = acl_d;
4156 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
4157 +
4158 +               } else {
4159 +                       def_acl = acl_d;
4160 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
4161 +               }
4162 +
4163 +               if (tmp_acl == NULL) {
4164 +                       return -1;
4165 +               }
4166 +
4167 +               /*
4168 +                * allocate a temporary buffer for the complete ACL
4169 +                */
4170 +               acl_count = acc_acl->count + def_acl->count;
4171 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
4172 +
4173 +               if (acl_buf == NULL) {
4174 +                       sys_acl_free_acl(tmp_acl);
4175 +                       errno = ENOMEM;
4176 +                       return -1;
4177 +               }
4178 +
4179 +               /*
4180 +                * copy the access control and default entries into the buffer
4181 +                */
4182 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
4183 +                       acc_acl->count * sizeof(acl_buf[0]));
4184 +
4185 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
4186 +                       def_acl->count * sizeof(acl_buf[0]));
4187 +
4188 +               /*
4189 +                * set the ACL_DEFAULT flag on the default entries
4190 +                */
4191 +               for (i = acc_acl->count; i < acl_count; i++) {
4192 +                       acl_buf[i].a_type |= ACL_DEFAULT;
4193 +               }
4194 +
4195 +               sys_acl_free_acl(tmp_acl);
4196 +
4197 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
4198 +               errno = EINVAL;
4199 +               return -1;
4200 +       }
4201 +
4202 +       ret = acl(name, ACL_SET, acl_count, acl_p);
4203 +
4204 +       if (acl_buf) {
4205 +               free(acl_buf);
4206 +       }
4207 +
4208 +       return ret;
4209 +}
4210 +
4211 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
4212 +{
4213 +       /*
4214 +        * HPUX doesn't have the facl call. Fake it using the path.... JRA.
4215 +        */
4216 +
4217 +       files_struct *fsp = file_find_fd(fd);
4218 +
4219 +       if (fsp == NULL) {
4220 +               errno = EBADF;
4221 +               return NULL;
4222 +       }
4223 +
4224 +       if (acl_sort(acl_d) != 0) {
4225 +               return -1;
4226 +       }
4227 +
4228 +       /*
4229 +        * We know we're in the same conn context. So we
4230 +        * can use the relative path.
4231 +        */
4232 +
4233 +       return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
4234 +}
4235 +
4236 +int sys_acl_delete_def_file(const char *path)
4237 +{
4238 +       SMB_ACL_T       acl_d;
4239 +       int             ret;
4240 +
4241 +       /*
4242 +        * fetching the access ACL and rewriting it has
4243 +        * the effect of deleting the default ACL
4244 +        */
4245 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
4246 +               return -1;
4247 +       }
4248 +
4249 +       ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
4250 +
4251 +       sys_acl_free_acl(acl_d);
4252 +       
4253 +       return ret;
4254 +}
4255 +
4256 +int sys_acl_free_text(char *text)
4257 +{
4258 +       free(text);
4259 +       return 0;
4260 +}
4261 +
4262 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
4263 +{
4264 +       free(acl_d);
4265 +       return 0;
4266 +}
4267 +
4268 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4269 +{
4270 +       return 0;
4271 +}
4272 +
4273 +#elif defined(HAVE_IRIX_ACLS)
4274 +
4275 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
4276 +{
4277 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
4278 +               errno = EINVAL;
4279 +               return -1;
4280 +       }
4281 +
4282 +       if (entry_p == NULL) {
4283 +               errno = EINVAL;
4284 +               return -1;
4285 +       }
4286 +
4287 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
4288 +               acl_d->next = 0;
4289 +       }
4290 +
4291 +       if (acl_d->next < 0) {
4292 +               errno = EINVAL;
4293 +               return -1;
4294 +       }
4295 +
4296 +       if (acl_d->next >= acl_d->aclp->acl_cnt) {
4297 +               return 0;
4298 +       }
4299 +
4300 +       *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
4301 +
4302 +       return 1;
4303 +}
4304 +
4305 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
4306 +{
4307 +       *type_p = entry_d->ae_tag;
4308 +
4309 +       return 0;
4310 +}
4311 +
4312 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
4313 +{
4314 +       *permset_p = entry_d;
4315 +
4316 +       return 0;
4317 +}
4318 +
4319 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
4320 +{
4321 +       if (entry_d->ae_tag != SMB_ACL_USER
4322 +           && entry_d->ae_tag != SMB_ACL_GROUP) {
4323 +               errno = EINVAL;
4324 +               return NULL;
4325 +       }
4326 +
4327 +       return &entry_d->ae_id;
4328 +}
4329 +
4330 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
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_file(path_p, type)) == NULL) {
4339 +               SAFE_FREE(a);
4340 +               return NULL;
4341 +       }
4342 +       a->next = -1;
4343 +       a->freeaclp = True;
4344 +       return a;
4345 +}
4346 +
4347 +SMB_ACL_T sys_acl_get_fd(int fd)
4348 +{
4349 +       SMB_ACL_T       a;
4350 +
4351 +       if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
4352 +               errno = ENOMEM;
4353 +               return NULL;
4354 +       }
4355 +       if ((a->aclp = acl_get_fd(fd)) == NULL) {
4356 +               SAFE_FREE(a);
4357 +               return NULL;
4358 +       }
4359 +       a->next = -1;
4360 +       a->freeaclp = True;
4361 +       return a;
4362 +}
4363 +
4364 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
4365 +{
4366 +       permset_d->ae_perm = 0;
4367 +
4368 +       return 0;
4369 +}
4370 +
4371 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
4372 +{
4373 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
4374 +           && perm != SMB_ACL_EXECUTE) {
4375 +               errno = EINVAL;
4376 +               return -1;
4377 +       }
4378 +
4379 +       if (permset_d == NULL) {
4380 +               errno = EINVAL;
4381 +               return -1;
4382 +       }
4383 +
4384 +       permset_d->ae_perm |= perm;
4385 +
4386 +       return 0;
4387 +}
4388 +
4389 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
4390 +{
4391 +       return permset_d->ae_perm & perm;
4392 +}
4393 +
4394 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
4395 +{
4396 +       return acl_to_text(acl_d->aclp, len_p);
4397 +}
4398 +
4399 +SMB_ACL_T sys_acl_init(int count)
4400 +{
4401 +       SMB_ACL_T       a;
4402 +
4403 +       if (count < 0) {
4404 +               errno = EINVAL;
4405 +               return NULL;
4406 +       }
4407 +
4408 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
4409 +               errno = ENOMEM;
4410 +               return NULL;
4411 +       }
4412 +
4413 +       a->next = -1;
4414 +       a->freeaclp = False;
4415 +       a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
4416 +       a->aclp->acl_cnt = 0;
4417 +
4418 +       return a;
4419 +}
4420 +
4421 +
4422 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
4423 +{
4424 +       SMB_ACL_T       acl_d;
4425 +       SMB_ACL_ENTRY_T entry_d;
4426 +
4427 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
4428 +               errno = EINVAL;
4429 +               return -1;
4430 +       }
4431 +
4432 +       if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
4433 +               errno = ENOSPC;
4434 +               return -1;
4435 +       }
4436 +
4437 +       entry_d         = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
4438 +       entry_d->ae_tag = 0;
4439 +       entry_d->ae_id  = 0;
4440 +       entry_d->ae_perm        = 0;
4441 +       *entry_p        = entry_d;
4442 +
4443 +       return 0;
4444 +}
4445 +
4446 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
4447 +{
4448 +       switch (tag_type) {
4449 +               case SMB_ACL_USER:
4450 +               case SMB_ACL_USER_OBJ:
4451 +               case SMB_ACL_GROUP:
4452 +               case SMB_ACL_GROUP_OBJ:
4453 +               case SMB_ACL_OTHER:
4454 +               case SMB_ACL_MASK:
4455 +                       entry_d->ae_tag = tag_type;
4456 +                       break;
4457 +               default:
4458 +                       errno = EINVAL;
4459 +                       return -1;
4460 +       }
4461 +
4462 +       return 0;
4463 +}
4464 +
4465 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
4466 +{
4467 +       if (entry_d->ae_tag != SMB_ACL_GROUP
4468 +           && entry_d->ae_tag != SMB_ACL_USER) {
4469 +               errno = EINVAL;
4470 +               return -1;
4471 +       }
4472 +
4473 +       entry_d->ae_id = *((id_t *)qual_p);
4474 +
4475 +       return 0;
4476 +}
4477 +
4478 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
4479 +{
4480 +       if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
4481 +               return EINVAL;
4482 +       }
4483 +
4484 +       entry_d->ae_perm = permset_d->ae_perm;
4485 +
4486 +       return 0;
4487 +}
4488 +
4489 +int sys_acl_valid(SMB_ACL_T acl_d)
4490 +{
4491 +       return acl_valid(acl_d->aclp);
4492 +}
4493 +
4494 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
4495 +{
4496 +       return acl_set_file(name, type, acl_d->aclp);
4497 +}
4498 +
4499 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
4500 +{
4501 +       return acl_set_fd(fd, acl_d->aclp);
4502 +}
4503 +
4504 +int sys_acl_delete_def_file(const char *name)
4505 +{
4506 +       return acl_delete_def_file(name);
4507 +}
4508 +
4509 +int sys_acl_free_text(char *text)
4510 +{
4511 +       return acl_free(text);
4512 +}
4513 +
4514 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
4515 +{
4516 +       if (acl_d->freeaclp) {
4517 +               acl_free(acl_d->aclp);
4518 +       }
4519 +       acl_free(acl_d);
4520 +       return 0;
4521 +}
4522 +
4523 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4524 +{
4525 +       return 0;
4526 +}
4527 +
4528 +#elif defined(HAVE_AIX_ACLS)
4529 +
4530 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
4531 +
4532 +int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
4533 +{
4534 +       struct acl_entry_link *link;
4535 +       struct new_acl_entry *entry;
4536 +       int keep_going;
4537 +
4538 +       DEBUG(10,("This is the count: %d\n",theacl->count));
4539 +
4540 +       /* Check if count was previously set to -1. *
4541 +        * If it was, that means we reached the end *
4542 +        * of the acl last time.                    */
4543 +       if(theacl->count == -1)
4544 +               return(0);
4545 +
4546 +       link = theacl;
4547 +       /* To get to the next acl, traverse linked list until index *
4548 +        * of acl matches the count we are keeping.  This count is  *
4549 +        * incremented each time we return an acl entry.            */
4550 +
4551 +       for(keep_going = 0; keep_going < theacl->count; keep_going++)
4552 +               link = link->nextp;
4553 +
4554 +       entry = *entry_p =  link->entryp;
4555 +
4556 +       DEBUG(10,("*entry_p is %d\n",entry_p));
4557 +       DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
4558 +
4559 +       /* Increment count */
4560 +       theacl->count++;
4561 +       if(link->nextp == NULL)
4562 +               theacl->count = -1;
4563 +
4564 +       return(1);
4565 +}
4566 +
4567 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
4568 +{
4569 +       /* Initialize tag type */
4570 +
4571 +       *tag_type_p = -1;
4572 +       DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
4573 +
4574 +       /* Depending on what type of entry we have, *
4575 +        * return tag type.                         */
4576 +       switch(entry_d->ace_id->id_type) {
4577 +       case ACEID_USER:
4578 +               *tag_type_p = SMB_ACL_USER;
4579 +               break;
4580 +       case ACEID_GROUP:
4581 +               *tag_type_p = SMB_ACL_GROUP;
4582 +               break;
4583 +
4584 +       case SMB_ACL_USER_OBJ:
4585 +       case SMB_ACL_GROUP_OBJ:
4586 +       case SMB_ACL_OTHER:
4587 +               *tag_type_p = entry_d->ace_id->id_type;
4588 +               break;
4589
4590 +       default:
4591 +               return(-1);
4592 +       }
4593 +
4594 +       return(0);
4595 +}
4596 +
4597 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
4598 +{
4599 +       DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
4600 +       *permset_p = &entry_d->ace_access;
4601 +       DEBUG(10,("**permset_p is %d\n",**permset_p));
4602 +       if(!(**permset_p & S_IXUSR) &&
4603 +               !(**permset_p & S_IWUSR) &&
4604 +               !(**permset_p & S_IRUSR) &&
4605 +               (**permset_p != 0))
4606 +                       return(-1);
4607 +
4608 +       DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
4609 +       return(0);
4610 +}
4611 +
4612 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
4613 +{
4614 +       return(entry_d->ace_id->id_data);
4615 +}
4616 +
4617 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
4618 +{
4619 +       struct acl *file_acl = (struct acl *)NULL;
4620 +       struct acl_entry *acl_entry;
4621 +       struct new_acl_entry *new_acl_entry;
4622 +       struct ace_id *idp;
4623 +       struct acl_entry_link *acl_entry_link;
4624 +       struct acl_entry_link *acl_entry_link_head;
4625 +       int i;
4626 +       int rc = 0;
4627 +       uid_t user_id;
4628 +
4629 +       /* AIX has no DEFAULT */
4630 +       if  ( type == SMB_ACL_TYPE_DEFAULT ) {
4631 +               errno = ENOTSUP;
4632 +               return NULL;
4633 +       }
4634 +
4635 +       /* Get the acl using statacl */
4636
4637 +       DEBUG(10,("Entering sys_acl_get_file\n"));
4638 +       DEBUG(10,("path_p is %s\n",path_p));
4639 +
4640 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4641
4642 +       if(file_acl == NULL) {
4643 +               errno=ENOMEM;
4644 +               DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
4645 +               return(NULL);
4646 +       }
4647 +
4648 +       memset(file_acl,0,BUFSIZ);
4649 +
4650 +       rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
4651 +       if(rc == -1) {
4652 +               DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
4653 +               SAFE_FREE(file_acl);
4654 +               return(NULL);
4655 +       }
4656 +
4657 +       DEBUG(10,("Got facl and returned it\n"));
4658 +
4659 +       /* Point to the first acl entry in the acl */
4660 +       acl_entry =  file_acl->acl_ext;
4661 +
4662 +       /* Begin setting up the head of the linked list *
4663 +        * that will be used for the storing the acl    *
4664 +        * in a way that is useful for the posix_acls.c *
4665 +        * code.                                          */
4666 +
4667 +       acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4668 +       if(acl_entry_link_head == NULL)
4669 +               return(NULL);
4670 +
4671 +       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4672 +       if(acl_entry_link->entryp == NULL) {
4673 +               SAFE_FREE(file_acl);
4674 +               errno = ENOMEM;
4675 +               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4676 +               return(NULL);
4677 +       }
4678 +
4679 +       DEBUG(10,("acl_entry is %d\n",acl_entry));
4680 +       DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4681 +
4682 +       /* Check if the extended acl bit is on.   *
4683 +        * If it isn't, do not show the           *
4684 +        * contents of the acl since AIX intends *
4685 +        * the extended info to remain unused     */
4686 +
4687 +       if(file_acl->acl_mode & S_IXACL){
4688 +               /* while we are not pointing to the very end */
4689 +               while(acl_entry < acl_last(file_acl)) {
4690 +                       /* before we malloc anything, make sure this is  */
4691 +                       /* a valid acl entry and one that we want to map */
4692 +                       idp = id_nxt(acl_entry->ace_id);
4693 +                       if((acl_entry->ace_type == ACC_SPECIFY ||
4694 +                               (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4695 +                                       acl_entry = acl_nxt(acl_entry);
4696 +                                       continue;
4697 +                       }
4698 +
4699 +                       idp = acl_entry->ace_id;
4700 +
4701 +                       /* Check if this is the first entry in the linked list. *
4702 +                        * The first entry needs to keep prevp pointing to NULL *
4703 +                        * and already has entryp allocated.                  */
4704 +
4705 +                       if(acl_entry_link_head->count != 0) {
4706 +                               acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4707 +
4708 +                               if(acl_entry_link->nextp == NULL) {
4709 +                                       SAFE_FREE(file_acl);
4710 +                                       errno = ENOMEM;
4711 +                                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4712 +                                       return(NULL);
4713 +                               }
4714 +
4715 +                               acl_entry_link->nextp->prevp = acl_entry_link;
4716 +                               acl_entry_link = acl_entry_link->nextp;
4717 +                               acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4718 +                               if(acl_entry_link->entryp == NULL) {
4719 +                                       SAFE_FREE(file_acl);
4720 +                                       errno = ENOMEM;
4721 +                                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4722 +                                       return(NULL);
4723 +                               }
4724 +                               acl_entry_link->nextp = NULL;
4725 +                       }
4726 +
4727 +                       acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4728 +
4729 +                       /* Don't really need this since all types are going *
4730 +                        * to be specified but, it's better than leaving it 0 */
4731 +
4732 +                       acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4733
4734 +                       acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4735
4736 +                       memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
4737 +
4738 +                       /* The access in the acl entries must be left shifted by *
4739 +                        * three bites, because they will ultimately be compared *
4740 +                        * to S_IRUSR, S_IWUSR, and S_IXUSR.                  */
4741 +
4742 +                       switch(acl_entry->ace_type){
4743 +                       case ACC_PERMIT:
4744 +                       case ACC_SPECIFY:
4745 +                               acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4746 +                               acl_entry_link->entryp->ace_access <<= 6;
4747 +                               acl_entry_link_head->count++;
4748 +                               break;
4749 +                       case ACC_DENY:
4750 +                               /* Since there is no way to return a DENY acl entry *
4751 +                                * change to PERMIT and then shift.                 */
4752 +                               DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4753 +                               acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4754 +                               DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4755 +                               acl_entry_link->entryp->ace_access <<= 6;
4756 +                               acl_entry_link_head->count++;
4757 +                               break;
4758 +                       default:
4759 +                               return(0);
4760 +                       }
4761 +
4762 +                       DEBUG(10,("acl_entry = %d\n",acl_entry));
4763 +                       DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4764
4765 +                       acl_entry = acl_nxt(acl_entry);
4766 +               }
4767 +       } /* end of if enabled */
4768 +
4769 +       /* Since owner, group, other acl entries are not *
4770 +        * part of the acl entries in an acl, they must  *
4771 +        * be dummied up to become part of the list.     */
4772 +
4773 +       for( i = 1; i < 4; i++) {
4774 +               DEBUG(10,("i is %d\n",i));
4775 +               if(acl_entry_link_head->count != 0) {
4776 +                       acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4777 +                       if(acl_entry_link->nextp == NULL) {
4778 +                               SAFE_FREE(file_acl);
4779 +                               errno = ENOMEM;
4780 +                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4781 +                               return(NULL);
4782 +                       }
4783 +
4784 +                       acl_entry_link->nextp->prevp = acl_entry_link;
4785 +                       acl_entry_link = acl_entry_link->nextp;
4786 +                       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4787 +                       if(acl_entry_link->entryp == NULL) {
4788 +                               SAFE_FREE(file_acl);
4789 +                               errno = ENOMEM;
4790 +                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4791 +                               return(NULL);
4792 +                       }
4793 +               }
4794 +
4795 +               acl_entry_link->nextp = NULL;
4796 +
4797 +               new_acl_entry = acl_entry_link->entryp;
4798 +               idp = new_acl_entry->ace_id;
4799 +
4800 +               new_acl_entry->ace_len = sizeof(struct acl_entry);
4801 +               new_acl_entry->ace_type = ACC_PERMIT;
4802 +               idp->id_len = sizeof(struct ace_id);
4803 +               DEBUG(10,("idp->id_len = %d\n",idp->id_len));
4804 +               memset(idp->id_data,0,sizeof(uid_t));
4805 +
4806 +               switch(i) {
4807 +               case 2:
4808 +                       new_acl_entry->ace_access = file_acl->g_access << 6;
4809 +                       idp->id_type = SMB_ACL_GROUP_OBJ;
4810 +                       break;
4811 +
4812 +               case 3:
4813 +                       new_acl_entry->ace_access = file_acl->o_access << 6;
4814 +                       idp->id_type = SMB_ACL_OTHER;
4815 +                       break;
4816
4817 +               case 1:
4818 +                       new_acl_entry->ace_access = file_acl->u_access << 6;
4819 +                       idp->id_type = SMB_ACL_USER_OBJ;
4820 +                       break;
4821
4822 +               default:
4823 +                       return(NULL);
4824 +
4825 +               }
4826 +
4827 +               acl_entry_link_head->count++;
4828 +               DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4829 +       }
4830 +
4831 +       acl_entry_link_head->count = 0;
4832 +       SAFE_FREE(file_acl);
4833 +
4834 +       return(acl_entry_link_head);
4835 +}
4836 +
4837 +SMB_ACL_T sys_acl_get_fd(int fd)
4838 +{
4839 +       struct acl *file_acl = (struct acl *)NULL;
4840 +       struct acl_entry *acl_entry;
4841 +       struct new_acl_entry *new_acl_entry;
4842 +       struct ace_id *idp;
4843 +       struct acl_entry_link *acl_entry_link;
4844 +       struct acl_entry_link *acl_entry_link_head;
4845 +       int i;
4846 +       int rc = 0;
4847 +       uid_t user_id;
4848 +
4849 +       /* Get the acl using fstatacl */
4850 +   
4851 +       DEBUG(10,("Entering sys_acl_get_fd\n"));
4852 +       DEBUG(10,("fd is %d\n",fd));
4853 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4854 +
4855 +       if(file_acl == NULL) {
4856 +               errno=ENOMEM;
4857 +               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4858 +               return(NULL);
4859 +       }
4860 +
4861 +       memset(file_acl,0,BUFSIZ);
4862 +
4863 +       rc = fstatacl(fd,0,file_acl,BUFSIZ);
4864 +       if(rc == -1) {
4865 +               DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
4866 +               SAFE_FREE(file_acl);
4867 +               return(NULL);
4868 +       }
4869 +
4870 +       DEBUG(10,("Got facl and returned it\n"));
4871 +
4872 +       /* Point to the first acl entry in the acl */
4873 +
4874 +       acl_entry =  file_acl->acl_ext;
4875 +       /* Begin setting up the head of the linked list *
4876 +        * that will be used for the storing the acl    *
4877 +        * in a way that is useful for the posix_acls.c *
4878 +        * code.                                        */
4879 +
4880 +       acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4881 +       if(acl_entry_link_head == NULL){
4882 +               SAFE_FREE(file_acl);
4883 +               return(NULL);
4884 +       }
4885 +
4886 +       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4887 +
4888 +       if(acl_entry_link->entryp == NULL) {
4889 +               errno = ENOMEM;
4890 +               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4891 +               SAFE_FREE(file_acl);
4892 +               return(NULL);
4893 +       }
4894 +
4895 +       DEBUG(10,("acl_entry is %d\n",acl_entry));
4896 +       DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4897
4898 +       /* Check if the extended acl bit is on.   *
4899 +        * If it isn't, do not show the           *
4900 +        * contents of the acl since AIX intends  *
4901 +        * the extended info to remain unused     */
4902
4903 +       if(file_acl->acl_mode & S_IXACL){
4904 +               /* while we are not pointing to the very end */
4905 +               while(acl_entry < acl_last(file_acl)) {
4906 +                       /* before we malloc anything, make sure this is  */
4907 +                       /* a valid acl entry and one that we want to map */
4908 +
4909 +                       idp = id_nxt(acl_entry->ace_id);
4910 +                       if((acl_entry->ace_type == ACC_SPECIFY ||
4911 +                               (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4912 +                                       acl_entry = acl_nxt(acl_entry);
4913 +                                       continue;
4914 +                       }
4915 +
4916 +                       idp = acl_entry->ace_id;
4917
4918 +                       /* Check if this is the first entry in the linked list. *
4919 +                        * The first entry needs to keep prevp pointing to NULL *
4920 +                        * and already has entryp allocated.                 */
4921 +
4922 +                       if(acl_entry_link_head->count != 0) {
4923 +                               acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4924 +                               if(acl_entry_link->nextp == NULL) {
4925 +                                       errno = ENOMEM;
4926 +                                       DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4927 +                                       SAFE_FREE(file_acl);
4928 +                                       return(NULL);
4929 +                               }
4930 +                               acl_entry_link->nextp->prevp = acl_entry_link;
4931 +                               acl_entry_link = acl_entry_link->nextp;
4932 +                               acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4933 +                               if(acl_entry_link->entryp == NULL) {
4934 +                                       errno = ENOMEM;
4935 +                                       DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4936 +                                       SAFE_FREE(file_acl);
4937 +                                       return(NULL);
4938 +                               }
4939 +
4940 +                               acl_entry_link->nextp = NULL;
4941 +                       }
4942 +
4943 +                       acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4944 +
4945 +                       /* Don't really need this since all types are going *
4946 +                        * to be specified but, it's better than leaving it 0 */
4947 +
4948 +                       acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4949 +                       acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4950 +
4951 +                       memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
4952 +
4953 +                       /* The access in the acl entries must be left shifted by *
4954 +                        * three bites, because they will ultimately be compared *
4955 +                        * to S_IRUSR, S_IWUSR, and S_IXUSR.                  */
4956 +
4957 +                       switch(acl_entry->ace_type){
4958 +                       case ACC_PERMIT:
4959 +                       case ACC_SPECIFY:
4960 +                               acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4961 +                               acl_entry_link->entryp->ace_access <<= 6;
4962 +                               acl_entry_link_head->count++;
4963 +                               break;
4964 +                       case ACC_DENY:
4965 +                               /* Since there is no way to return a DENY acl entry *
4966 +                                * change to PERMIT and then shift.                 */
4967 +                               DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4968 +                               acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4969 +                               DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4970 +                               acl_entry_link->entryp->ace_access <<= 6;
4971 +                               acl_entry_link_head->count++;
4972 +                               break;
4973 +                       default:
4974 +                               return(0);
4975 +                       }
4976 +
4977 +                       DEBUG(10,("acl_entry = %d\n",acl_entry));
4978 +                       DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4979
4980 +                       acl_entry = acl_nxt(acl_entry);
4981 +               }
4982 +       } /* end of if enabled */
4983 +
4984 +       /* Since owner, group, other acl entries are not *
4985 +        * part of the acl entries in an acl, they must  *
4986 +        * be dummied up to become part of the list.     */
4987 +
4988 +       for( i = 1; i < 4; i++) {
4989 +               DEBUG(10,("i is %d\n",i));
4990 +               if(acl_entry_link_head->count != 0){
4991 +                       acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4992 +                       if(acl_entry_link->nextp == NULL) {
4993 +                               errno = ENOMEM;
4994 +                               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4995 +                               SAFE_FREE(file_acl);
4996 +                               return(NULL);
4997 +                       }
4998 +
4999 +                       acl_entry_link->nextp->prevp = acl_entry_link;
5000 +                       acl_entry_link = acl_entry_link->nextp;
5001 +                       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
5002 +
5003 +                       if(acl_entry_link->entryp == NULL) {
5004 +                               SAFE_FREE(file_acl);
5005 +                               errno = ENOMEM;
5006 +                               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
5007 +                               return(NULL);
5008 +                       }
5009 +               }
5010 +
5011 +               acl_entry_link->nextp = NULL;
5012
5013 +               new_acl_entry = acl_entry_link->entryp;
5014 +               idp = new_acl_entry->ace_id;
5015
5016 +               new_acl_entry->ace_len = sizeof(struct acl_entry);
5017 +               new_acl_entry->ace_type = ACC_PERMIT;
5018 +               idp->id_len = sizeof(struct ace_id);
5019 +               DEBUG(10,("idp->id_len = %d\n",idp->id_len));
5020 +               memset(idp->id_data,0,sizeof(uid_t));
5021
5022 +               switch(i) {
5023 +               case 2:
5024 +                       new_acl_entry->ace_access = file_acl->g_access << 6;
5025 +                       idp->id_type = SMB_ACL_GROUP_OBJ;
5026 +                       break;
5027
5028 +               case 3:
5029 +                       new_acl_entry->ace_access = file_acl->o_access << 6;
5030 +                       idp->id_type = SMB_ACL_OTHER;
5031 +                       break;
5032
5033 +               case 1:
5034 +                       new_acl_entry->ace_access = file_acl->u_access << 6;
5035 +                       idp->id_type = SMB_ACL_USER_OBJ;
5036 +                       break;
5037
5038 +               default:
5039 +                       return(NULL);
5040 +               }
5041
5042 +               acl_entry_link_head->count++;
5043 +               DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
5044 +       }
5045 +
5046 +       acl_entry_link_head->count = 0;
5047 +       SAFE_FREE(file_acl);
5048
5049 +       return(acl_entry_link_head);
5050 +}
5051 +
5052 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
5053 +{
5054 +       *permset = *permset & ~0777;
5055 +       return(0);
5056 +}
5057 +
5058 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
5059 +{
5060 +       if((perm != 0) &&
5061 +                       (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
5062 +               return(-1);
5063 +
5064 +       *permset |= perm;
5065 +       DEBUG(10,("This is the permset now: %d\n",*permset));
5066 +       return(0);
5067 +}
5068 +
5069 +char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
5070 +{
5071 +       return(NULL);
5072 +}
5073 +
5074 +SMB_ACL_T sys_acl_init( int count)
5075 +{
5076 +       struct acl_entry_link *theacl = NULL;
5077
5078 +       DEBUG(10,("Entering sys_acl_init\n"));
5079 +
5080 +       theacl = SMB_MALLOC_P(struct acl_entry_link);
5081 +       if(theacl == NULL) {
5082 +               errno = ENOMEM;
5083 +               DEBUG(0,("Error in sys_acl_init is %d\n",errno));
5084 +               return(NULL);
5085 +       }
5086 +
5087 +       theacl->count = 0;
5088 +       theacl->nextp = NULL;
5089 +       theacl->prevp = NULL;
5090 +       theacl->entryp = NULL;
5091 +       DEBUG(10,("Exiting sys_acl_init\n"));
5092 +       return(theacl);
5093 +}
5094 +
5095 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
5096 +{
5097 +       struct acl_entry_link *theacl;
5098 +       struct acl_entry_link *acl_entryp;
5099 +       struct acl_entry_link *temp_entry;
5100 +       int counting;
5101 +
5102 +       DEBUG(10,("Entering the sys_acl_create_entry\n"));
5103 +
5104 +       theacl = acl_entryp = *pacl;
5105 +
5106 +       /* Get to the end of the acl before adding entry */
5107 +
5108 +       for(counting=0; counting < theacl->count; counting++){
5109 +               DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
5110 +               temp_entry = acl_entryp;
5111 +               acl_entryp = acl_entryp->nextp;
5112 +       }
5113 +
5114 +       if(theacl->count != 0){
5115 +               temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
5116 +               if(acl_entryp == NULL) {
5117 +                       errno = ENOMEM;
5118 +                       DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
5119 +                       return(-1);
5120 +               }
5121 +
5122 +               DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
5123 +               acl_entryp->prevp = temp_entry;
5124 +               DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
5125 +       }
5126 +
5127 +       *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
5128 +       if(*pentry == NULL) {
5129 +               errno = ENOMEM;
5130 +               DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
5131 +               return(-1);
5132 +       }
5133 +
5134 +       memset(*pentry,0,sizeof(struct new_acl_entry));
5135 +       acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
5136 +       acl_entryp->entryp->ace_type = ACC_PERMIT;
5137 +       acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
5138 +       acl_entryp->nextp = NULL;
5139 +       theacl->count++;
5140 +       DEBUG(10,("Exiting sys_acl_create_entry\n"));
5141 +       return(0);
5142 +}
5143 +
5144 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
5145 +{
5146 +       DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
5147 +       entry->ace_id->id_type = tagtype;
5148 +       DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
5149 +       DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
5150 +}
5151 +
5152 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
5153 +{
5154 +       DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
5155 +       memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
5156 +       DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
5157 +       return(0);
5158 +}
5159 +
5160 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
5161 +{
5162 +       DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
5163 +       if(!(*permset & S_IXUSR) &&
5164 +               !(*permset & S_IWUSR) &&
5165 +               !(*permset & S_IRUSR) &&
5166 +               (*permset != 0))
5167 +                       return(-1);
5168 +
5169 +       entry->ace_access = *permset;
5170 +       DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
5171 +       DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
5172 +       return(0);
5173 +}
5174 +
5175 +int sys_acl_valid( SMB_ACL_T theacl )
5176 +{
5177 +       int user_obj = 0;
5178 +       int group_obj = 0;
5179 +       int other_obj = 0;
5180 +       struct acl_entry_link *acl_entry;
5181 +
5182 +       for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
5183 +               user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
5184 +               group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
5185 +               other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
5186 +       }
5187 +
5188 +       DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
5189
5190 +       if(user_obj != 1 || group_obj != 1 || other_obj != 1)
5191 +               return(-1); 
5192 +
5193 +       return(0);
5194 +}
5195 +
5196 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
5197 +{
5198 +       struct acl_entry_link *acl_entry_link = NULL;
5199 +       struct acl *file_acl = NULL;
5200 +       struct acl *file_acl_temp = NULL;
5201 +       struct acl_entry *acl_entry = NULL;
5202 +       struct ace_id *ace_id = NULL;
5203 +       uint id_type;
5204 +       uint ace_access;
5205 +       uint user_id;
5206 +       uint acl_length;
5207 +       uint rc;
5208 +
5209 +       DEBUG(10,("Entering sys_acl_set_file\n"));
5210 +       DEBUG(10,("File name is %s\n",name));
5211
5212 +       /* AIX has no default ACL */
5213 +       if(acltype == SMB_ACL_TYPE_DEFAULT)
5214 +               return(0);
5215 +
5216 +       acl_length = BUFSIZ;
5217 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
5218 +
5219 +       if(file_acl == NULL) {
5220 +               errno = ENOMEM;
5221 +               DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
5222 +               return(-1);
5223 +       }
5224 +
5225 +       memset(file_acl,0,BUFSIZ);
5226 +
5227 +       file_acl->acl_len = ACL_SIZ;
5228 +       file_acl->acl_mode = S_IXACL;
5229 +
5230 +       for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
5231 +               acl_entry_link->entryp->ace_access >>= 6;
5232 +               id_type = acl_entry_link->entryp->ace_id->id_type;
5233 +
5234 +               switch(id_type) {
5235 +               case SMB_ACL_USER_OBJ:
5236 +                       file_acl->u_access = acl_entry_link->entryp->ace_access;
5237 +                       continue;
5238 +               case SMB_ACL_GROUP_OBJ:
5239 +                       file_acl->g_access = acl_entry_link->entryp->ace_access;
5240 +                       continue;
5241 +               case SMB_ACL_OTHER:
5242 +                       file_acl->o_access = acl_entry_link->entryp->ace_access;
5243 +                       continue;
5244 +               case SMB_ACL_MASK:
5245 +                       continue;
5246 +               }
5247 +
5248 +               if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
5249 +                       acl_length += sizeof(struct acl_entry);
5250 +                       file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
5251 +                       if(file_acl_temp == NULL) {
5252 +                               SAFE_FREE(file_acl);
5253 +                               errno = ENOMEM;
5254 +                               DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
5255 +                               return(-1);
5256 +                       }  
5257 +
5258 +                       memcpy(file_acl_temp,file_acl,file_acl->acl_len);
5259 +                       SAFE_FREE(file_acl);
5260 +                       file_acl = file_acl_temp;
5261 +               }
5262 +
5263 +               acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
5264 +               file_acl->acl_len += sizeof(struct acl_entry);
5265 +               acl_entry->ace_len = acl_entry_link->entryp->ace_len;
5266 +               acl_entry->ace_access = acl_entry_link->entryp->ace_access;
5267
5268 +               /* In order to use this, we'll need to wait until we can get denies */
5269 +               /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
5270 +               acl_entry->ace_type = ACC_SPECIFY; */
5271 +
5272 +               acl_entry->ace_type = ACC_SPECIFY;
5273
5274 +               ace_id = acl_entry->ace_id;
5275
5276 +               ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
5277 +               DEBUG(10,("The id type is %d\n",ace_id->id_type));
5278 +               ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
5279 +               memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
5280 +               memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
5281 +       }
5282 +
5283 +       rc = chacl(name,file_acl,file_acl->acl_len);
5284 +       DEBUG(10,("errno is %d\n",errno));
5285 +       DEBUG(10,("return code is %d\n",rc));
5286 +       SAFE_FREE(file_acl);
5287 +       DEBUG(10,("Exiting the sys_acl_set_file\n"));
5288 +       return(rc);
5289 +}
5290 +
5291 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
5292 +{
5293 +       struct acl_entry_link *acl_entry_link = NULL;
5294 +       struct acl *file_acl = NULL;
5295 +       struct acl *file_acl_temp = NULL;
5296 +       struct acl_entry *acl_entry = NULL;
5297 +       struct ace_id *ace_id = NULL;
5298 +       uint id_type;
5299 +       uint user_id;
5300 +       uint acl_length;
5301 +       uint rc;
5302
5303 +       DEBUG(10,("Entering sys_acl_set_fd\n"));
5304 +       acl_length = BUFSIZ;
5305 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
5306 +
5307 +       if(file_acl == NULL) {
5308 +               errno = ENOMEM;
5309 +               DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
5310 +               return(-1);
5311 +       }
5312 +
5313 +       memset(file_acl,0,BUFSIZ);
5314
5315 +       file_acl->acl_len = ACL_SIZ;
5316 +       file_acl->acl_mode = S_IXACL;
5317 +
5318 +       for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
5319 +               acl_entry_link->entryp->ace_access >>= 6;
5320 +               id_type = acl_entry_link->entryp->ace_id->id_type;
5321 +               DEBUG(10,("The id_type is %d\n",id_type));
5322 +
5323 +               switch(id_type) {
5324 +               case SMB_ACL_USER_OBJ:
5325 +                       file_acl->u_access = acl_entry_link->entryp->ace_access;
5326 +                       continue;
5327 +               case SMB_ACL_GROUP_OBJ:
5328 +                       file_acl->g_access = acl_entry_link->entryp->ace_access;
5329 +                       continue;
5330 +               case SMB_ACL_OTHER:
5331 +                       file_acl->o_access = acl_entry_link->entryp->ace_access;
5332 +                       continue;
5333 +               case SMB_ACL_MASK:
5334 +                       continue;
5335 +               }
5336 +
5337 +               if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
5338 +                       acl_length += sizeof(struct acl_entry);
5339 +                       file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
5340 +                       if(file_acl_temp == NULL) {
5341 +                               SAFE_FREE(file_acl);
5342 +                               errno = ENOMEM;
5343 +                               DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
5344 +                               return(-1);
5345 +                       }
5346 +
5347 +                       memcpy(file_acl_temp,file_acl,file_acl->acl_len);
5348 +                       SAFE_FREE(file_acl);
5349 +                       file_acl = file_acl_temp;
5350 +               }
5351 +
5352 +               acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
5353 +               file_acl->acl_len += sizeof(struct acl_entry);
5354 +               acl_entry->ace_len = acl_entry_link->entryp->ace_len;
5355 +               acl_entry->ace_access = acl_entry_link->entryp->ace_access;
5356
5357 +               /* In order to use this, we'll need to wait until we can get denies */
5358 +               /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
5359 +                       acl_entry->ace_type = ACC_SPECIFY; */
5360
5361 +               acl_entry->ace_type = ACC_SPECIFY;
5362
5363 +               ace_id = acl_entry->ace_id;
5364
5365 +               ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
5366 +               DEBUG(10,("The id type is %d\n",ace_id->id_type));
5367 +               ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
5368 +               memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
5369 +               memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
5370 +       }
5371
5372 +       rc = fchacl(fd,file_acl,file_acl->acl_len);
5373 +       DEBUG(10,("errno is %d\n",errno));
5374 +       DEBUG(10,("return code is %d\n",rc));
5375 +       SAFE_FREE(file_acl);
5376 +       DEBUG(10,("Exiting sys_acl_set_fd\n"));
5377 +       return(rc);
5378 +}
5379 +
5380 +int sys_acl_delete_def_file(const char *name)
5381 +{
5382 +       /* AIX has no default ACL */
5383 +       return 0;
5384 +}
5385 +
5386 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
5387 +{
5388 +       return(*permset & perm);
5389 +}
5390 +
5391 +int sys_acl_free_text(char *text)
5392 +{
5393 +       return(0);
5394 +}
5395 +
5396 +int sys_acl_free_acl(SMB_ACL_T posix_acl)
5397 +{
5398 +       struct acl_entry_link *acl_entry_link;
5399 +
5400 +       for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
5401 +               SAFE_FREE(acl_entry_link->prevp->entryp);
5402 +               SAFE_FREE(acl_entry_link->prevp);
5403 +       }
5404 +
5405 +       SAFE_FREE(acl_entry_link->prevp->entryp);
5406 +       SAFE_FREE(acl_entry_link->prevp);
5407 +       SAFE_FREE(acl_entry_link->entryp);
5408 +       SAFE_FREE(acl_entry_link);
5409
5410 +       return(0);
5411 +}
5412 +
5413 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
5414 +{
5415 +       return(0);
5416 +}
5417 +
5418 +#else /* No ACLs. */
5419 +
5420 +int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
5421 +{
5422 +       errno = ENOSYS;
5423 +       return -1;
5424 +}
5425 +
5426 +int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
5427 +{
5428 +       errno = ENOSYS;
5429 +       return -1;
5430 +}
5431 +
5432 +int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
5433 +{
5434 +       errno = ENOSYS;
5435 +       return -1;
5436 +}
5437 +
5438 +void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
5439 +{
5440 +       errno = ENOSYS;
5441 +       return NULL;
5442 +}
5443 +
5444 +SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
5445 +{
5446 +       errno = ENOSYS;
5447 +       return (SMB_ACL_T)NULL;
5448 +}
5449 +
5450 +SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
5451 +{
5452 +       errno = ENOSYS;
5453 +       return (SMB_ACL_T)NULL;
5454 +}
5455 +
5456 +int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
5457 +{
5458 +       errno = ENOSYS;
5459 +       return -1;
5460 +}
5461 +
5462 +int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
5463 +{
5464 +       errno = ENOSYS;
5465 +       return -1;
5466 +}
5467 +
5468 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
5469 +{
5470 +       errno = ENOSYS;
5471 +       return (permset & perm) ? 1 : 0;
5472 +}
5473 +
5474 +char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
5475 +{
5476 +       errno = ENOSYS;
5477 +       return NULL;
5478 +}
5479 +
5480 +int sys_acl_free_text(UNUSED(char *text))
5481 +{
5482 +       errno = ENOSYS;
5483 +       return -1;
5484 +}
5485 +
5486 +SMB_ACL_T sys_acl_init(UNUSED(int count))
5487 +{
5488 +       errno = ENOSYS;
5489 +       return NULL;
5490 +}
5491 +
5492 +int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
5493 +{
5494 +       errno = ENOSYS;
5495 +       return -1;
5496 +}
5497 +
5498 +int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
5499 +{
5500 +       errno = ENOSYS;
5501 +       return -1;
5502 +}
5503 +
5504 +int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
5505 +{
5506 +       errno = ENOSYS;
5507 +       return -1;
5508 +}
5509 +
5510 +int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
5511 +{
5512 +       errno = ENOSYS;
5513 +       return -1;
5514 +}
5515 +
5516 +int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
5517 +{
5518 +       errno = ENOSYS;
5519 +       return -1;
5520 +}
5521 +
5522 +int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
5523 +{
5524 +       errno = ENOSYS;
5525 +       return -1;
5526 +}
5527 +
5528 +int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
5529 +{
5530 +       errno = ENOSYS;
5531 +       return -1;
5532 +}
5533 +
5534 +int sys_acl_delete_def_file(UNUSED(const char *name))
5535 +{
5536 +       errno = ENOSYS;
5537 +       return -1;
5538 +}
5539 +
5540 +int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
5541 +{
5542 +       errno = ENOSYS;
5543 +       return -1;
5544 +}
5545 +
5546 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
5547 +{
5548 +       errno = ENOSYS;
5549 +       return -1;
5550 +}
5551 +
5552 +#endif /* No ACLs. */
5553 +
5554 +/************************************************************************
5555 + Deliberately outside the ACL defines. Return 1 if this is a "no acls"
5556 + errno, 0 if not.
5557 +************************************************************************/
5558 +
5559 +int no_acl_syscall_error(int err)
5560 +{
5561 +#if defined(ENOSYS)
5562 +       if (err == ENOSYS) {
5563 +               return 1;
5564 +       }
5565 +#endif
5566 +#if defined(ENOTSUP)
5567 +       if (err == ENOTSUP) {
5568 +               return 1;
5569 +       }
5570 +#endif
5571 +       return 0;
5572 +}
5573 +
5574 +#endif /* SUPPORT_ACLS */
5575 --- old/lib/sysacls.h
5576 +++ new/lib/sysacls.h
5577 @@ -0,0 +1,40 @@
5578 +#ifdef SUPPORT_ACLS
5579 +
5580 +#ifdef HAVE_SYS_ACL_H
5581 +#include <sys/acl.h>
5582 +#endif
5583 +#ifdef HAVE_ACL_LIBACL_H
5584 +#include <acl/libacl.h>
5585 +#endif
5586 +#include "smb_acls.h"
5587 +
5588 +#define SMB_MALLOC(cnt) new_array(char, cnt)
5589 +#define SMB_MALLOC_P(obj) new_array(obj, 1)
5590 +#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
5591 +#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
5592 +#define slprintf snprintf
5593 +
5594 +int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
5595 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
5596 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
5597 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
5598 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
5599 +SMB_ACL_T sys_acl_get_fd(int fd);
5600 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
5601 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
5602 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
5603 +char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
5604 +SMB_ACL_T sys_acl_init(int count);
5605 +int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
5606 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
5607 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
5608 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
5609 +int sys_acl_valid(SMB_ACL_T theacl);
5610 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
5611 +int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
5612 +int sys_acl_delete_def_file(const char *name);
5613 +int sys_acl_free_text(char *text);
5614 +int sys_acl_free_acl(SMB_ACL_T the_acl);
5615 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
5616 +
5617 +#endif /* SUPPORT_ACLS */
5618 --- old/log.c
5619 +++ new/log.c
5620 @@ -606,8 +606,10 @@ static void log_formatted(enum logcode c
5621                         n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
5622                         n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
5623                         n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
5624 -                       n[8] = '.';
5625 -                       n[9] = '\0';
5626 +                       n[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
5627 +                       n[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
5628 +                       n[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
5629 +                       n[11] = '\0';
5630  
5631                         if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
5632                                 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
5633 --- old/options.c
5634 +++ new/options.c
5635 @@ -47,6 +47,7 @@ int copy_dirlinks = 0;
5636  int copy_links = 0;
5637  int preserve_links = 0;
5638  int preserve_hard_links = 0;
5639 +int preserve_acls = 0;
5640  int preserve_perms = 0;
5641  int preserve_executability = 0;
5642  int preserve_devices = 0;
5643 @@ -199,6 +200,7 @@ static void print_rsync_version(enum log
5644         char const *got_socketpair = "no ";
5645         char const *have_inplace = "no ";
5646         char const *hardlinks = "no ";
5647 +       char const *acls = "no ";
5648         char const *links = "no ";
5649         char const *ipv6 = "no ";
5650         STRUCT_STAT *dumstat;
5651 @@ -215,6 +217,10 @@ static void print_rsync_version(enum log
5652         hardlinks = "";
5653  #endif
5654  
5655 +#ifdef SUPPORT_ACLS
5656 +       acls = "";
5657 +#endif
5658 +
5659  #ifdef SUPPORT_LINKS
5660         links = "";
5661  #endif
5662 @@ -227,17 +233,16 @@ static void print_rsync_version(enum log
5663                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
5664         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
5665         rprintf(f, "<http://rsync.samba.org/>\n");
5666 -       rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
5667 -               "%shard links, %ssymlinks, batchfiles,\n",
5668 -               (int) (sizeof (OFF_T) * 8),
5669 -               got_socketpair, hardlinks, links);
5670 +       rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, %shard links, %ssymlinks,\n",
5671 +               (int) (sizeof (OFF_T) * 8), got_socketpair, hardlinks, links);
5672 +
5673 +       rprintf(f, "              batchfiles, %sinplace, %sIPv6, %sACLs,\n",
5674 +               have_inplace, ipv6, acls);
5675  
5676         /* Note that this field may not have type ino_t.  It depends
5677          * on the complicated interaction between largefile feature
5678          * macros. */
5679 -       rprintf(f, "              %sinplace, %sIPv6, "
5680 -               "%d-bit system inums, %d-bit internal inums\n",
5681 -               have_inplace, ipv6,
5682 +       rprintf(f, "              %d-bit system inums, %d-bit internal inums\n",
5683                 (int) (sizeof dumstat->st_ino * 8),
5684                 (int) (sizeof (int64) * 8));
5685  #ifdef MAINTAINER_MODE
5686 @@ -284,7 +289,7 @@ void usage(enum logcode F)
5687    rprintf(F," -q, --quiet                 suppress non-error messages\n");
5688    rprintf(F,"     --no-motd               suppress daemon-mode MOTD (see manpage caveat)\n");
5689    rprintf(F," -c, --checksum              skip based on checksum, not mod-time & size\n");
5690 -  rprintf(F," -a, --archive               archive mode; same as -rlptgoD (no -H)\n");
5691 +  rprintf(F," -a, --archive               archive mode; same as -rlptgoD (no -H, -A)\n");
5692    rprintf(F,"     --no-OPTION             turn off an implied OPTION (e.g. --no-D)\n");
5693    rprintf(F," -r, --recursive             recurse into directories\n");
5694    rprintf(F," -R, --relative              use relative path names\n");
5695 @@ -306,6 +311,9 @@ void usage(enum logcode F)
5696    rprintf(F," -p, --perms                 preserve permissions\n");
5697    rprintf(F," -E, --executability         preserve the file's executability\n");
5698    rprintf(F,"     --chmod=CHMOD           affect file and/or directory permissions\n");
5699 +#ifdef SUPPORT_ACLS
5700 +  rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
5701 +#endif
5702    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
5703    rprintf(F," -g, --group                 preserve group\n");
5704    rprintf(F,"     --devices               preserve device files (super-user only)\n");
5705 @@ -425,6 +433,9 @@ static struct poptOption long_options[] 
5706    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
5707    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
5708    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
5709 +  {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
5710 +  {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
5711 +  {"no-A",             0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
5712    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
5713    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
5714    {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
5715 @@ -1089,6 +1100,24 @@ int parse_arguments(int *argc, const cha
5716                         usage(FINFO);
5717                         exit_cleanup(0);
5718  
5719 +               case 'A':
5720 +#ifdef SUPPORT_ACLS
5721 +                       preserve_acls++;
5722 +                       preserve_perms = 1;
5723 +                       break;
5724 +#else
5725 +                       /* FIXME: this should probably be ignored with a
5726 +                        * warning and then countermeasures taken to
5727 +                        * restrict group and other access in the presence
5728 +                        * of any more restrictive ACLs, but this is safe
5729 +                        * for now */
5730 +                       snprintf(err_buf,sizeof(err_buf),
5731 +                                 "ACLs are not supported on this %s\n",
5732 +                                am_server ? "server" : "client");
5733 +                       return 0;
5734 +#endif
5735 +
5736 +
5737                 default:
5738                         /* A large opt value means that set_refuse_options()
5739                          * turned this option off. */
5740 @@ -1530,6 +1559,10 @@ void server_options(char **args,int *arg
5741  
5742         if (preserve_hard_links)
5743                 argstr[x++] = 'H';
5744 +#ifdef SUPPORT_ACLS
5745 +       if (preserve_acls)
5746 +               argstr[x++] = 'A';
5747 +#endif
5748         if (preserve_uid)
5749                 argstr[x++] = 'o';
5750         if (preserve_gid)
5751 --- old/receiver.c
5752 +++ new/receiver.c
5753 @@ -47,6 +47,7 @@ extern int keep_partial;
5754  extern int checksum_seed;
5755  extern int inplace;
5756  extern int delay_updates;
5757 +extern mode_t orig_umask;
5758  extern struct stats stats;
5759  extern char *stdout_format;
5760  extern char *tmpdir;
5761 @@ -350,6 +351,10 @@ int recv_files(int f_in, struct file_lis
5762         int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
5763         enum logcode log_code = log_before_transfer ? FLOG : FINFO;
5764         int max_phase = protocol_version >= 29 ? 2 : 1;
5765 +       int dflt_perms = (ACCESSPERMS & ~orig_umask);
5766 +#ifdef SUPPORT_ACLS
5767 +       char *parent_dirname = "";
5768 +#endif
5769         int i, recv_ok;
5770  
5771         if (verbose > 2)
5772 @@ -553,7 +558,16 @@ int recv_files(int f_in, struct file_lis
5773                  * mode based on the local permissions and some heuristics. */
5774                 if (!preserve_perms) {
5775                         int exists = fd1 != -1;
5776 -                       file->mode = dest_mode(file->mode, st.st_mode, exists);
5777 +#ifdef SUPPORT_ACLS
5778 +                       char *dn = file->dirname ? file->dirname : ".";
5779 +                       if (parent_dirname != dn
5780 +                        && strcmp(parent_dirname, dn) != 0) {
5781 +                               dflt_perms = default_perms_for_dir(dn);
5782 +                               parent_dirname = dn;
5783 +                       }
5784 +#endif
5785 +                       file->mode = dest_mode(file->mode, st.st_mode,
5786 +                                              dflt_perms, exists);
5787                 }
5788  
5789                 /* We now check to see if we are writing the file "inplace" */
5790 --- old/rsync.c
5791 +++ new/rsync.c
5792 @@ -32,6 +32,7 @@
5793  
5794  extern int verbose;
5795  extern int dry_run;
5796 +extern int preserve_acls;
5797  extern int preserve_perms;
5798  extern int preserve_executability;
5799  extern int preserve_times;
5800 @@ -47,7 +48,6 @@ extern int preserve_gid;
5801  extern int inplace;
5802  extern int keep_dirlinks;
5803  extern int make_backups;
5804 -extern mode_t orig_umask;
5805  extern struct stats stats;
5806  extern struct chmod_mode_struct *daemon_chmod_modes;
5807  
5808 @@ -100,7 +100,8 @@ void free_sums(struct sum_struct *s)
5809  
5810  /* This is only called when we aren't preserving permissions.  Figure out what
5811   * the permissions should be and return them merged back into the mode. */
5812 -mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int exists)
5813 +mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
5814 +                int exists)
5815  {
5816         int new_mode;
5817         /* If the file already exists, we'll return the local permissions,
5818 @@ -117,56 +118,65 @@ mode_t dest_mode(mode_t flist_mode, mode
5819                                 new_mode |= (new_mode & 0444) >> 2;
5820                 }
5821         } else {
5822 -               /* Apply the umask and turn off special permissions. */
5823 -               new_mode = flist_mode & (~CHMOD_BITS | (ACCESSPERMS & ~orig_umask));
5824 +               /* Apply destination default permissions and turn
5825 +                * off special permissions. */
5826 +               new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
5827         }
5828         return new_mode;
5829  }
5830  
5831 -int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
5832 +int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
5833                    int flags)
5834  {
5835         int updated = 0;
5836 -       STRUCT_STAT st2;
5837 +       statx sx2;
5838         int change_uid, change_gid;
5839         mode_t new_mode = file->mode;
5840  
5841 -       if (!st) {
5842 +       if (!sxp) {
5843                 if (dry_run)
5844                         return 1;
5845 -               if (link_stat(fname, &st2, 0) < 0) {
5846 +               if (link_stat(fname, &sx2.st, 0) < 0) {
5847                         rsyserr(FERROR, errno, "stat %s failed",
5848                                 full_fname(fname));
5849                         return 0;
5850                 }
5851 -               st = &st2;
5852 +#ifdef SUPPORT_ACLS
5853 +               sx2.acc_acl = sx2.def_acl = NULL;
5854 +#endif
5855                 if (!preserve_perms && S_ISDIR(new_mode)
5856 -                && st->st_mode & S_ISGID) {
5857 +                && sx2.st.st_mode & S_ISGID) {
5858                         /* We just created this directory and its setgid
5859                          * bit is on, so make sure it stays on. */
5860                         new_mode |= S_ISGID;
5861                 }
5862 +               sxp = &sx2;
5863         }
5864  
5865 -       if (!preserve_times || (S_ISDIR(st->st_mode) && omit_dir_times))
5866 +#ifdef SUPPORT_ACLS
5867 +       if (preserve_acls && !ACL_READY(*sxp))
5868 +               get_acl(fname, sxp);
5869 +#endif
5870 +
5871 +       if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && omit_dir_times))
5872                 flags |= ATTRS_SKIP_MTIME;
5873         if (!(flags & ATTRS_SKIP_MTIME)
5874 -           && cmp_time(st->st_mtime, file->modtime) != 0) {
5875 -               int ret = set_modtime(fname, file->modtime, st->st_mode);
5876 +           && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
5877 +               int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
5878                 if (ret < 0) {
5879                         rsyserr(FERROR, errno, "failed to set times on %s",
5880                                 full_fname(fname));
5881 -                       return 0;
5882 +                       goto cleanup;
5883                 }
5884                 if (ret == 0) /* ret == 1 if symlink could not be set */
5885                         updated = 1;
5886         }
5887  
5888 -       change_uid = am_root && preserve_uid && st->st_uid != file->uid;
5889 +       change_uid = am_root && preserve_uid && sxp->st.st_uid != file->uid;
5890         change_gid = preserve_gid && file->gid != GID_NONE
5891 -               && st->st_gid != file->gid;
5892 +               && sxp->st.st_gid != file->gid;
5893  #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
5894 -       if (S_ISLNK(st->st_mode))
5895 +       if (S_ISLNK(sxp->st.st_mode))
5896                 ;
5897         else
5898  #endif
5899 @@ -176,45 +186,57 @@ int set_file_attrs(char *fname, struct f
5900                                 rprintf(FINFO,
5901                                         "set uid of %s from %ld to %ld\n",
5902                                         fname,
5903 -                                       (long)st->st_uid, (long)file->uid);
5904 +                                       (long)sxp->st.st_uid, (long)file->uid);
5905                         }
5906                         if (change_gid) {
5907                                 rprintf(FINFO,
5908                                         "set gid of %s from %ld to %ld\n",
5909                                         fname,
5910 -                                       (long)st->st_gid, (long)file->gid);
5911 +                                       (long)sxp->st.st_gid, (long)file->gid);
5912                         }
5913                 }
5914                 if (do_lchown(fname,
5915 -                   change_uid ? file->uid : st->st_uid,
5916 -                   change_gid ? file->gid : st->st_gid) != 0) {
5917 +                   change_uid ? file->uid : sxp->st.st_uid,
5918 +                   change_gid ? file->gid : sxp->st.st_gid) != 0) {
5919                         /* shouldn't have attempted to change uid or gid
5920                          * unless have the privilege */
5921                         rsyserr(FERROR, errno, "%s %s failed",
5922                             change_uid ? "chown" : "chgrp",
5923                             full_fname(fname));
5924 -                       return 0;
5925 +                       goto cleanup;
5926                 }
5927                 /* a lchown had been done - we have to re-stat if the
5928                  * destination had the setuid or setgid bits set due
5929                  * to the side effect of the chown call */
5930 -               if (st->st_mode & (S_ISUID | S_ISGID)) {
5931 -                       link_stat(fname, st,
5932 -                                 keep_dirlinks && S_ISDIR(st->st_mode));
5933 +               if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
5934 +                       link_stat(fname, &sxp->st,
5935 +                                 keep_dirlinks && S_ISDIR(sxp->st.st_mode));
5936                 }
5937                 updated = 1;
5938         }
5939  
5940         if (daemon_chmod_modes && !S_ISLNK(new_mode))
5941                 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
5942 +
5943 +#ifdef SUPPORT_ACLS
5944 +       /* It's OK to call set_acl() now, even for a dir, as the generator
5945 +        * will enable owner-writability using chmod, if necessary.
5946 +        * 
5947 +        * If set_acl() changes permission bits in the process of setting
5948 +        * an access ACL, it changes sxp->st.st_mode so we know whether we
5949 +        * need to chmod(). */
5950 +       if (preserve_acls && set_acl(fname, file, sxp) == 0)
5951 +               updated = 1;
5952 +#endif
5953 +
5954  #ifdef HAVE_CHMOD
5955 -       if ((st->st_mode & CHMOD_BITS) != (new_mode & CHMOD_BITS)) {
5956 +       if ((sxp->st.st_mode & CHMOD_BITS) != (new_mode & CHMOD_BITS)) {
5957                 int ret = do_chmod(fname, new_mode);
5958                 if (ret < 0) {
5959                         rsyserr(FERROR, errno,
5960                                 "failed to set permissions on %s",
5961                                 full_fname(fname));
5962 -                       return 0;
5963 +                       goto cleanup;
5964                 }
5965                 if (ret == 0) /* ret == 1 if symlink could not be set */
5966                         updated = 1;
5967 @@ -227,6 +249,11 @@ int set_file_attrs(char *fname, struct f
5968                 else
5969                         rprintf(FCLIENT, "%s is uptodate\n", fname);
5970         }
5971 +  cleanup:
5972 +#ifdef SUPPORT_ACLS
5973 +       if (preserve_acls && sxp == &sx2)
5974 +               free_acl(&sx2);
5975 +#endif
5976         return updated;
5977  }
5978  
5979 --- old/rsync.h
5980 +++ new/rsync.h
5981 @@ -493,6 +493,14 @@ struct idev {
5982  #define IN_LOOPBACKNET 127
5983  #endif
5984  
5985 +#ifndef HAVE_NO_ACLS
5986 +#define SUPPORT_ACLS 1
5987 +#endif
5988 +
5989 +#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
5990 +#define ACLS_NEED_MASK 1
5991 +#endif
5992 +
5993  #define GID_NONE ((gid_t)-1)
5994  
5995  #define HL_CHECK_MASTER        0
5996 @@ -654,6 +662,17 @@ struct stats {
5997  
5998  struct chmod_mode_struct;
5999  
6000 +#define EMPTY_ITEM_LIST {NULL, 0, 0}
6001 +
6002 +typedef struct {
6003 +       void *items;
6004 +       size_t count;
6005 +       size_t malloced;
6006 +} item_list;
6007 +
6008 +#define EXPAND_ITEM_LIST(lp, type, incr) \
6009 +       (type*)expand_item_list(lp, sizeof (type), #type, incr)
6010 +
6011  #include "byteorder.h"
6012  #include "lib/mdfour.h"
6013  #include "lib/wildmatch.h"
6014 @@ -667,6 +686,16 @@ struct chmod_mode_struct;
6015  #define UNUSED(x) x __attribute__((__unused__))
6016  #define NORETURN __attribute__((__noreturn__))
6017  
6018 +typedef struct {
6019 +    STRUCT_STAT st;
6020 +#ifdef SUPPORT_ACLS
6021 +    struct rsync_acl *acc_acl; /* access ACL */
6022 +    struct rsync_acl *def_acl; /* default ACL */
6023 +#endif
6024 +} statx;
6025 +
6026 +#define ACL_READY(sx) ((sx).acc_acl != NULL)
6027 +
6028  #include "proto.h"
6029  
6030  /* We have replacement versions of these if they're missing. */
6031 --- old/rsync.yo
6032 +++ new/rsync.yo
6033 @@ -301,7 +301,7 @@ to the detailed description below for a 
6034   -q, --quiet                 suppress non-error messages
6035       --no-motd               suppress daemon-mode MOTD (see caveat)
6036   -c, --checksum              skip based on checksum, not mod-time & size
6037 - -a, --archive               archive mode; same as -rlptgoD (no -H)
6038 + -a, --archive               archive mode; same as -rlptgoD (no -H, -A)
6039       --no-OPTION             turn off an implied OPTION (e.g. --no-D)
6040   -r, --recursive             recurse into directories
6041   -R, --relative              use relative path names
6042 @@ -323,6 +323,7 @@ to the detailed description below for a 
6043   -p, --perms                 preserve permissions
6044   -E, --executability         preserve executability
6045       --chmod=CHMOD           affect file and/or directory permissions
6046 + -A, --acls                  preserve ACLs (implies -p) [non-standard]
6047   -o, --owner                 preserve owner (super-user only)
6048   -g, --group                 preserve group
6049       --devices               preserve device files (super-user only)
6050 @@ -753,7 +754,9 @@ quote(itemization(
6051    permissions, though the bf(--executability) option might change just
6052    the execute permission for the file.
6053    it() New files get their "normal" permission bits set to the source
6054 -  file's permissions masked with the receiving end's umask setting, and
6055 +  file's permissions masked with the receiving directory's default
6056 +  permissions (either the receiving process's umask, or the permissions
6057 +  specified via the destination directory's default ACL), and
6058    their special permission bits disabled except in the case where a new
6059    directory inherits a setgid bit from its parent directory.
6060  ))
6061 @@ -784,9 +787,11 @@ The preservation of the destination's se
6062  directories when bf(--perms) is off was added in rsync 2.6.7.  Older rsync
6063  versions erroneously preserved the three special permission bits for
6064  newly-created files when bf(--perms) was off, while overriding the
6065 -destination's setgid bit setting on a newly-created directory.  (Keep in
6066 -mind that it is the version of the receiving rsync that affects this
6067 -behavior.)
6068 +destination's setgid bit setting on a newly-created directory.  Default ACL
6069 +observance was added to the ACL patch for rsync 2.6.7, so older (or
6070 +non-ACL-enabled) rsyncs use the umask even if default ACLs are present.
6071 +(Keep in mind that it is the version of the receiving rsync that affects
6072 +these behaviors.)
6073  
6074  dit(bf(-E, --executability)) This option causes rsync to preserve the
6075  executability (or non-executability) of regular files when bf(--perms) is
6076 @@ -804,6 +809,15 @@ quote(itemization(
6077  
6078  If bf(--perms) is enabled, this option is ignored.
6079  
6080 +dit(bf(-A, --acls)) This option causes rsync to update the destination
6081 +ACLs to be the same as the source ACLs.  This nonstandard option only
6082 +works if the remote rsync also supports it.  bf(--acls) implies bf(--perms).
6083 +
6084 +Note also that an optimization of the ACL-sending protocol used by this
6085 +version makes it incompatible with sending files to an older ACL-enabled
6086 +rsync unless you double the bf(--acls) option (e.g. bf(-AA)).  This
6087 +doubling is not needed when pulling files from an older rsync.
6088 +
6089  dit(bf(--chmod)) This option tells rsync to apply one or more
6090  comma-separated "chmod" strings to the permission of the files in the
6091  transfer.  The resulting value is treated as though it was the permissions
6092 @@ -1394,8 +1408,8 @@ if the receiving rsync is at least versi
6093  with older versions of rsync, but that also turns on the output of other
6094  verbose messages).
6095  
6096 -The "%i" escape has a cryptic output that is 9 letters long.  The general
6097 -format is like the string bf(YXcstpogz), where bf(Y) is replaced by the
6098 +The "%i" escape has a cryptic output that is 11 letters long.  The general
6099 +format is like the string bf(YXcstpoguax), where bf(Y) is replaced by the
6100  type of update being done, bf(X) is replaced by the file-type, and the
6101  other letters represent attributes that may be output if they are being
6102  modified.
6103 @@ -1444,7 +1458,11 @@ quote(itemization(
6104    sender's value (requires bf(--owner) and super-user privileges).
6105    it() A bf(g) means the group is different and is being updated to the
6106    sender's value (requires bf(--group) and the authority to set the group).
6107 -  it() The bf(z) slot is reserved for future use.
6108 +  it() The bf(u) slot is reserved for reporting update (access) time changes
6109 +  (a feature that is not yet released).
6110 +  it() The bf(a) means that the ACL information changed.
6111 +  it() The bf(x) slot is reserved for reporting extended attribute changes
6112 +  (a feature that is not yet released).
6113  ))
6114  
6115  One other output is possible:  when deleting files, the "%i" will output
6116 --- old/smb_acls.h
6117 +++ new/smb_acls.h
6118 @@ -0,0 +1,281 @@
6119 +/* 
6120 +   Unix SMB/Netbios implementation.
6121 +   Version 2.2.x
6122 +   Portable SMB ACL interface
6123 +   Copyright (C) Jeremy Allison 2000
6124 +   
6125 +   This program is free software; you can redistribute it and/or modify
6126 +   it under the terms of the GNU General Public License as published by
6127 +   the Free Software Foundation; either version 2 of the License, or
6128 +   (at your option) any later version.
6129 +   
6130 +   This program is distributed in the hope that it will be useful,
6131 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
6132 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6133 +   GNU General Public License for more details.
6134 +   
6135 +   You should have received a copy of the GNU General Public License
6136 +   along with this program; if not, write to the Free Software
6137 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
6138 +*/
6139 +
6140 +#ifndef _SMB_ACLS_H
6141 +#define _SMB_ACLS_H
6142 +
6143 +#if defined HAVE_POSIX_ACLS
6144 +
6145 +/* This is an identity mapping (just remove the SMB_). */
6146 +
6147 +#define SMB_ACL_TAG_T          acl_tag_t
6148 +#define SMB_ACL_TYPE_T         acl_type_t
6149 +#define SMB_ACL_PERMSET_T      acl_permset_t
6150 +#define SMB_ACL_PERM_T         acl_perm_t
6151 +#define SMB_ACL_READ           ACL_READ
6152 +#define SMB_ACL_WRITE          ACL_WRITE
6153 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
6154 +
6155 +/* Types of ACLs. */
6156 +#define SMB_ACL_USER           ACL_USER
6157 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
6158 +#define SMB_ACL_GROUP          ACL_GROUP
6159 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
6160 +#define SMB_ACL_OTHER          ACL_OTHER
6161 +#define SMB_ACL_MASK           ACL_MASK
6162 +
6163 +#define SMB_ACL_T              acl_t
6164 +
6165 +#define SMB_ACL_ENTRY_T                acl_entry_t
6166 +
6167 +#define SMB_ACL_FIRST_ENTRY    ACL_FIRST_ENTRY
6168 +#define SMB_ACL_NEXT_ENTRY     ACL_NEXT_ENTRY
6169 +
6170 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
6171 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
6172 +
6173 +#elif defined HAVE_TRU64_ACLS
6174 +
6175 +/* This is for DEC/Compaq Tru64 UNIX */
6176 +
6177 +#define SMB_ACL_TAG_T          acl_tag_t
6178 +#define SMB_ACL_TYPE_T         acl_type_t
6179 +#define SMB_ACL_PERMSET_T      acl_permset_t
6180 +#define SMB_ACL_PERM_T         acl_perm_t
6181 +#define SMB_ACL_READ           ACL_READ
6182 +#define SMB_ACL_WRITE          ACL_WRITE
6183 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
6184 +
6185 +/* Types of ACLs. */
6186 +#define SMB_ACL_USER           ACL_USER
6187 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
6188 +#define SMB_ACL_GROUP          ACL_GROUP
6189 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
6190 +#define SMB_ACL_OTHER          ACL_OTHER
6191 +#define SMB_ACL_MASK           ACL_MASK
6192 +
6193 +#define SMB_ACL_T              acl_t
6194 +
6195 +#define SMB_ACL_ENTRY_T                acl_entry_t
6196 +
6197 +#define SMB_ACL_FIRST_ENTRY    0
6198 +#define SMB_ACL_NEXT_ENTRY     1
6199 +
6200 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
6201 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
6202 +
6203 +#elif defined HAVE_UNIXWARE_ACLS || defined HAVE_SOLARIS_ACLS
6204 +/*
6205 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
6206 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
6207 + */
6208 +
6209 +/* SVR4.2 ES/MP ACLs */
6210 +typedef int SMB_ACL_TAG_T;
6211 +typedef int SMB_ACL_TYPE_T;
6212 +typedef ushort *SMB_ACL_PERMSET_T;
6213 +typedef ushort SMB_ACL_PERM_T;
6214 +#define SMB_ACL_READ           4
6215 +#define SMB_ACL_WRITE          2
6216 +#define SMB_ACL_EXECUTE                1
6217 +
6218 +/* Types of ACLs. */
6219 +#define SMB_ACL_USER           USER
6220 +#define SMB_ACL_USER_OBJ       USER_OBJ
6221 +#define SMB_ACL_GROUP          GROUP
6222 +#define SMB_ACL_GROUP_OBJ      GROUP_OBJ
6223 +#define SMB_ACL_OTHER          OTHER_OBJ
6224 +#define SMB_ACL_MASK           CLASS_OBJ
6225 +
6226 +typedef struct SMB_ACL_T {
6227 +       int size;
6228 +       int count;
6229 +       int next;
6230 +       struct acl acl[1];
6231 +} *SMB_ACL_T;
6232 +
6233 +typedef struct acl *SMB_ACL_ENTRY_T;
6234 +
6235 +#define SMB_ACL_FIRST_ENTRY    0
6236 +#define SMB_ACL_NEXT_ENTRY     1
6237 +
6238 +#define SMB_ACL_TYPE_ACCESS    0
6239 +#define SMB_ACL_TYPE_DEFAULT   1
6240 +
6241 +#ifdef __CYGWIN__
6242 +#define SMB_ACL_LOSES_SPECIAL_MODE_BITS
6243 +#endif
6244 +
6245 +#elif defined HAVE_HPUX_ACLS
6246 +
6247 +/*
6248 + * Based on the Solaris & UnixWare code.
6249 + */
6250 +
6251 +#undef GROUP
6252 +#include <sys/aclv.h>
6253 +
6254 +/* SVR4.2 ES/MP ACLs */
6255 +typedef int SMB_ACL_TAG_T;
6256 +typedef int SMB_ACL_TYPE_T;
6257 +typedef ushort *SMB_ACL_PERMSET_T;
6258 +typedef ushort SMB_ACL_PERM_T;
6259 +#define SMB_ACL_READ           4
6260 +#define SMB_ACL_WRITE          2
6261 +#define SMB_ACL_EXECUTE                1
6262 +
6263 +/* Types of ACLs. */
6264 +#define SMB_ACL_USER           USER
6265 +#define SMB_ACL_USER_OBJ       USER_OBJ
6266 +#define SMB_ACL_GROUP          GROUP
6267 +#define SMB_ACL_GROUP_OBJ      GROUP_OBJ
6268 +#define SMB_ACL_OTHER          OTHER_OBJ
6269 +#define SMB_ACL_MASK           CLASS_OBJ
6270 +
6271 +typedef struct SMB_ACL_T {
6272 +       int size;
6273 +       int count;
6274 +       int next;
6275 +       struct acl acl[1];
6276 +} *SMB_ACL_T;
6277 +
6278 +typedef struct acl *SMB_ACL_ENTRY_T;
6279 +
6280 +#define SMB_ACL_FIRST_ENTRY    0
6281 +#define SMB_ACL_NEXT_ENTRY     1
6282 +
6283 +#define SMB_ACL_TYPE_ACCESS    0
6284 +#define SMB_ACL_TYPE_DEFAULT   1
6285 +
6286 +#elif defined HAVE_IRIX_ACLS
6287 +
6288 +#define SMB_ACL_TAG_T          acl_tag_t
6289 +#define SMB_ACL_TYPE_T         acl_type_t
6290 +#define SMB_ACL_PERMSET_T      acl_permset_t
6291 +#define SMB_ACL_PERM_T         acl_perm_t
6292 +#define SMB_ACL_READ           ACL_READ
6293 +#define SMB_ACL_WRITE          ACL_WRITE
6294 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
6295 +
6296 +/* Types of ACLs. */
6297 +#define SMB_ACL_USER           ACL_USER
6298 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
6299 +#define SMB_ACL_GROUP          ACL_GROUP
6300 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
6301 +#define SMB_ACL_OTHER          ACL_OTHER_OBJ
6302 +#define SMB_ACL_MASK           ACL_MASK
6303 +
6304 +typedef struct SMB_ACL_T {
6305 +       int next;
6306 +       BOOL freeaclp;
6307 +       struct acl *aclp;
6308 +} *SMB_ACL_T;
6309 +
6310 +#define SMB_ACL_ENTRY_T                acl_entry_t
6311 +
6312 +#define SMB_ACL_FIRST_ENTRY    0
6313 +#define SMB_ACL_NEXT_ENTRY     1
6314 +
6315 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
6316 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
6317 +
6318 +#elif defined HAVE_AIX_ACLS
6319 +
6320 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
6321 +
6322 +#include "/usr/include/acl.h"
6323 +
6324 +typedef uint *SMB_ACL_PERMSET_T;
6325
6326 +struct acl_entry_link{
6327 +       struct acl_entry_link *prevp;
6328 +       struct new_acl_entry *entryp;
6329 +       struct acl_entry_link *nextp;
6330 +       int count;
6331 +};
6332 +
6333 +struct new_acl_entry{
6334 +       unsigned short ace_len;
6335 +       unsigned short ace_type;
6336 +       unsigned int ace_access;
6337 +       struct ace_id ace_id[1];
6338 +};
6339 +
6340 +#define SMB_ACL_ENTRY_T                struct new_acl_entry*
6341 +#define SMB_ACL_T              struct acl_entry_link*
6342
6343 +#define SMB_ACL_TAG_T          unsigned short
6344 +#define SMB_ACL_TYPE_T         int
6345 +#define SMB_ACL_PERM_T         uint
6346 +#define SMB_ACL_READ           S_IRUSR
6347 +#define SMB_ACL_WRITE          S_IWUSR
6348 +#define SMB_ACL_EXECUTE                S_IXUSR
6349 +
6350 +/* Types of ACLs. */
6351 +#define SMB_ACL_USER           ACEID_USER
6352 +#define SMB_ACL_USER_OBJ       3
6353 +#define SMB_ACL_GROUP          ACEID_GROUP
6354 +#define SMB_ACL_GROUP_OBJ      4
6355 +#define SMB_ACL_OTHER          5
6356 +#define SMB_ACL_MASK           6
6357 +
6358 +
6359 +#define SMB_ACL_FIRST_ENTRY    1
6360 +#define SMB_ACL_NEXT_ENTRY     2
6361 +
6362 +#define SMB_ACL_TYPE_ACCESS    0
6363 +#define SMB_ACL_TYPE_DEFAULT   1
6364 +
6365 +#else /* No ACLs. */
6366 +
6367 +/* No ACLS - fake it. */
6368 +#define SMB_ACL_TAG_T          int
6369 +#define SMB_ACL_TYPE_T         int
6370 +#define SMB_ACL_PERMSET_T      mode_t
6371 +#define SMB_ACL_PERM_T         mode_t
6372 +#define SMB_ACL_READ           S_IRUSR
6373 +#define SMB_ACL_WRITE          S_IWUSR
6374 +#define SMB_ACL_EXECUTE                S_IXUSR
6375 +
6376 +/* Types of ACLs. */
6377 +#define SMB_ACL_USER           0
6378 +#define SMB_ACL_USER_OBJ       1
6379 +#define SMB_ACL_GROUP          2
6380 +#define SMB_ACL_GROUP_OBJ      3
6381 +#define SMB_ACL_OTHER          4
6382 +#define SMB_ACL_MASK           5
6383 +
6384 +typedef struct SMB_ACL_T {
6385 +       int dummy;
6386 +} *SMB_ACL_T;
6387 +
6388 +typedef struct SMB_ACL_ENTRY_T {
6389 +       int dummy;
6390 +} *SMB_ACL_ENTRY_T;
6391 +
6392 +#define SMB_ACL_FIRST_ENTRY    0
6393 +#define SMB_ACL_NEXT_ENTRY     1
6394 +
6395 +#define SMB_ACL_TYPE_ACCESS    0
6396 +#define SMB_ACL_TYPE_DEFAULT   1
6397 +
6398 +#endif /* No ACLs. */
6399 +#endif /* _SMB_ACLS_H */
6400 --- old/testsuite/acls.test
6401 +++ new/testsuite/acls.test
6402 @@ -0,0 +1,34 @@
6403 +#! /bin/sh
6404 +
6405 +# This program is distributable under the terms of the GNU GPL (see
6406 +# COPYING).
6407 +
6408 +# Test that rsync handles basic ACL preservation.
6409 +
6410 +. $srcdir/testsuite/rsync.fns
6411 +
6412 +$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
6413 +case "$setfacl_nodef" in
6414 +true) test_skipped "I don't know how to use your setfacl command" ;;
6415 +esac
6416 +
6417 +makepath "$fromdir/foo"
6418 +echo something >"$fromdir/file1"
6419 +echo else >"$fromdir/file2"
6420 +
6421 +files='foo file1 file2'
6422 +
6423 +setfacl -m u:0:7 "$fromdir/foo" || test_skipped "Your filesystem has ACLs disabled"
6424 +setfacl -m u:0:5 "$fromdir/file1"
6425 +setfacl -m u:0:5 "$fromdir/file2"
6426 +
6427 +$RSYNC -avvA "$fromdir/" "$todir/"
6428 +
6429 +cd "$fromdir"
6430 +getfacl $files >"$scratchdir/acls.txt"
6431 +
6432 +cd "$todir"
6433 +getfacl $files | diff $diffopt "$scratchdir/acls.txt" -
6434 +
6435 +# The script would have aborted on error, so getting here means we've won.
6436 +exit 0
6437 --- old/testsuite/default-acls.test
6438 +++ new/testsuite/default-acls.test
6439 @@ -0,0 +1,65 @@
6440 +#! /bin/sh
6441 +
6442 +# This program is distributable under the terms of the GNU GPL (see
6443 +# COPYING).
6444 +
6445 +# Test that rsync obeys default ACLs. -- Matt McCutchen
6446 +
6447 +. $srcdir/testsuite/rsync.fns
6448 +
6449 +$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
6450 +case "$setfacl_nodef" in
6451 +true) test_skipped "I don't know how to use your setfacl command" ;;
6452 +*-k*) opts='-dm u::7,g::5,o:5' ;;
6453 +*) opts='-m d:u::7,d:g::5,d:o:5' ;;
6454 +esac
6455 +setfacl $opts "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
6456 +
6457 +# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
6458 +testit() {
6459 +    todir="$scratchdir/$1"
6460 +    mkdir "$todir"
6461 +    $setfacl_nodef "$todir"
6462 +    if [ "$2" ]; then
6463 +       case "$setfacl_nodef" in
6464 +       *-k*) opts="-dm $2" ;;
6465 +       *) opts="-m `echo $2 | sed 's/\([ugom]:\)/d:\1/g'`"
6466 +       esac
6467 +       setfacl $opts "$todir"
6468 +    fi
6469 +    # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
6470 +    # even though the directory itself is outside the transfer
6471 +    $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
6472 +    check_perms "$todir/to" $4 "Target $1"
6473 +    check_perms "$todir/to/dir" $4 "Target $1"
6474 +    check_perms "$todir/to/file" $3 "Target $1"
6475 +    check_perms "$todir/to/program" $4 "Target $1"
6476 +    # Make sure get_local_name doesn't mess us up when transferring only one file
6477 +    $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
6478 +    check_perms "$todir/to/anotherfile" $3 "Target $1"
6479 +    # Make sure we obey default ACLs when not transferring a regular file
6480 +    $RSYNC -rvv "$scratchdir/dir/" "$todir/to/anotherdir/"
6481 +    check_perms "$todir/to/anotherdir" $4 "Target $1"
6482 +}
6483 +
6484 +mkdir "$scratchdir/dir"
6485 +echo "File!" >"$scratchdir/file"
6486 +echo "#!/bin/sh" >"$scratchdir/program"
6487 +chmod 777 "$scratchdir/dir"
6488 +chmod 666 "$scratchdir/file"
6489 +chmod 777 "$scratchdir/program"
6490 +
6491 +# Test some target directories
6492 +umask 0077
6493 +testit da777 u::7,g::7,o:7 rw-rw-rw- rwxrwxrwx
6494 +testit da775 u::7,g::7,o:5 rw-rw-r-- rwxrwxr-x
6495 +testit da750 u::7,g::5,o:0 rw-r----- rwxr-x---
6496 +testit da770mask u::7,u:0:7,g::0,m:7,o:0 rw-rw---- rwxrwx---
6497 +testit noda1 '' rw------- rwx------
6498 +umask 0000
6499 +testit noda2 '' rw-rw-rw- rwxrwxrwx
6500 +umask 0022
6501 +testit noda3 '' rw-r--r-- rwxr-xr-x
6502 +
6503 +# Hooray
6504 +exit 0
6505 --- old/testsuite/devices.test
6506 +++ new/testsuite/devices.test
6507 @@ -42,14 +42,14 @@ touch -r "$fromdir/block" "$fromdir/bloc
6508  $RSYNC -ai "$fromdir/block" "$todir/block2" \
6509      | tee "$outfile"
6510  cat <<EOT >"$chkfile"
6511 -cD+++++++ block
6512 +cD+++++++++ block
6513  EOT
6514  diff $diffopt "$chkfile" "$outfile" || test_fail "test 1 failed"
6515  
6516  $RSYNC -ai "$fromdir/block2" "$todir/block" \
6517      | tee "$outfile"
6518  cat <<EOT >"$chkfile"
6519 -cD+++++++ block2
6520 +cD+++++++++ block2
6521  EOT
6522  diff $diffopt "$chkfile" "$outfile" || test_fail "test 2 failed"
6523  
6524 @@ -58,7 +58,7 @@ sleep 1
6525  $RSYNC -Di "$fromdir/block3" "$todir/block" \
6526      | tee "$outfile"
6527  cat <<EOT >"$chkfile"
6528 -cD..T.... block3
6529 +cD..T...... block3
6530  EOT
6531  diff $diffopt "$chkfile" "$outfile" || test_fail "test 3 failed"
6532  
6533 @@ -66,15 +66,15 @@ $RSYNC -aiHvv "$fromdir/" "$todir/" \
6534      | tee "$outfile"
6535  filter_outfile
6536  cat <<EOT >"$chkfile"
6537 -.d..t.... ./
6538 -cD..t.... block
6539 -cD        block2
6540 -cD+++++++ block3
6541 -hD+++++++ block2.5 => block3
6542 -cD+++++++ char
6543 -cD+++++++ char2
6544 -cD+++++++ char3
6545 -cS+++++++ fifo
6546 +.d..t...... ./
6547 +cD..t...... block
6548 +cD          block2
6549 +cD+++++++++ block3
6550 +hD+++++++++ block2.5 => block3
6551 +cD+++++++++ char
6552 +cD+++++++++ char2
6553 +cD+++++++++ char3
6554 +cS+++++++++ fifo
6555  EOT
6556  if test ! -b "$fromdir/block2.5"; then
6557      sed -e '/block2\.5/d' \
6558 @@ -94,15 +94,15 @@ if test -b "$fromdir/block2.5"; then
6559      $RSYNC -aii --link-dest="$todir" "$fromdir/" "$chkdir/" \
6560         | tee "$outfile"
6561      cat <<EOT >"$chkfile"
6562 -cd        ./
6563 -hD        block
6564 -hD        block2
6565 -hD        block2.5
6566 -hD        block3
6567 -hD        char
6568 -hD        char2
6569 -hD        char3
6570 -hS        fifo
6571 +cd          ./
6572 +hD          block
6573 +hD          block2
6574 +hD          block2.5
6575 +hD          block3
6576 +hD          char
6577 +hD          char2
6578 +hD          char3
6579 +hS          fifo
6580  EOT
6581      diff $diffopt "$chkfile" "$outfile" || test_fail "test 4 failed"
6582  fi
6583 --- old/testsuite/itemize.test
6584 +++ new/testsuite/itemize.test
6585 @@ -38,15 +38,15 @@ rm -f "$to2dir" "$to2dir.test"
6586  $RSYNC -iplr "$fromdir/" "$todir/" \
6587      | tee "$outfile"
6588  cat <<EOT >"$chkfile"
6589 -cd+++++++ ./
6590 -cd+++++++ bar/
6591 -cd+++++++ bar/baz/
6592 ->f+++++++ bar/baz/rsync
6593 -cd+++++++ foo/
6594 ->f+++++++ foo/config1
6595 ->f+++++++ foo/config2
6596 ->f+++++++ foo/extra
6597 -cL+++++++ foo/sym -> ../bar/baz/rsync
6598 +cd+++++++++ ./
6599 +cd+++++++++ bar/
6600 +cd+++++++++ bar/baz/
6601 +>f+++++++++ bar/baz/rsync
6602 +cd+++++++++ foo/
6603 +>f+++++++++ foo/config1
6604 +>f+++++++++ foo/config2
6605 +>f+++++++++ foo/extra
6606 +cL+++++++++ foo/sym -> ../bar/baz/rsync
6607  EOT
6608  diff $diffopt "$chkfile" "$outfile" || test_fail "test 1 failed"
6609  
6610 @@ -58,10 +58,10 @@ chmod 601 "$fromdir/foo/config2"
6611  $RSYNC -iplrH "$fromdir/" "$todir/" \
6612      | tee "$outfile"
6613  cat <<EOT >"$chkfile"
6614 ->f..T.... bar/baz/rsync
6615 ->f..T.... foo/config1
6616 ->f.sTp... foo/config2
6617 -hf..T.... foo/extra => foo/config1
6618 +>f..T...... bar/baz/rsync
6619 +>f..T...... foo/config1
6620 +>f.sTp..... foo/config2
6621 +hf..T...... foo/extra => foo/config1
6622  EOT
6623  diff $diffopt "$chkfile" "$outfile" || test_fail "test 2 failed"
6624  
6625 @@ -78,11 +78,11 @@ chmod 777 "$todir/bar/baz/rsync"
6626  $RSYNC -iplrtc "$fromdir/" "$todir/" \
6627      | tee "$outfile"
6628  cat <<EOT >"$chkfile"
6629 -.f..tp... bar/baz/rsync
6630 -.d..t.... foo/
6631 -.f..t.... foo/config1
6632 ->fcstp... foo/config2
6633 -cL..T.... foo/sym -> ../bar/baz/rsync
6634 +.f..tp..... bar/baz/rsync
6635 +.d..t...... foo/
6636 +.f..t...... foo/config1
6637 +>fcstp..... foo/config2
6638 +cL..T...... foo/sym -> ../bar/baz/rsync
6639  EOT
6640  diff $diffopt "$chkfile" "$outfile" || test_fail "test 3 failed"
6641  
6642 @@ -107,15 +107,15 @@ $RSYNC -ivvplrtH "$fromdir/" "$todir/" \
6643      | tee "$outfile"
6644  filter_outfile
6645  cat <<EOT >"$chkfile"
6646 -.d        ./
6647 -.d        bar/
6648 -.d        bar/baz/
6649 -.f...p... bar/baz/rsync
6650 -.d        foo/
6651 -.f        foo/config1
6652 ->f..t.... foo/config2
6653 -hf        foo/extra
6654 -.L        foo/sym -> ../bar/baz/rsync
6655 +.d          ./
6656 +.d          bar/
6657 +.d          bar/baz/
6658 +.f...p..... bar/baz/rsync
6659 +.d          foo/
6660 +.f          foo/config1
6661 +>f..t...... foo/config2
6662 +hf          foo/extra
6663 +.L          foo/sym -> ../bar/baz/rsync
6664  EOT
6665  diff $diffopt "$chkfile" "$outfile" || test_fail "test 5 failed"
6666  
6667 @@ -134,8 +134,8 @@ touch "$todir/foo/config2"
6668  $RSYNC -iplrtH "$fromdir/" "$todir/" \
6669      | tee "$outfile"
6670  cat <<EOT >"$chkfile"
6671 -.f...p... foo/config1
6672 ->f..t.... foo/config2
6673 +.f...p..... foo/config1
6674 +>f..t...... foo/config2
6675  EOT
6676  diff $diffopt "$chkfile" "$outfile" || test_fail "test 7 failed"
6677  
6678 @@ -143,15 +143,15 @@ $RSYNC -ivvplrtH --copy-dest=../to "$fro
6679      | tee "$outfile"
6680  filter_outfile
6681  cat <<EOT >"$chkfile"
6682 -cd        ./
6683 -cd        bar/
6684 -cd        bar/baz/
6685 -cf        bar/baz/rsync
6686 -cd        foo/
6687 -cf        foo/config1
6688 -cf        foo/config2
6689 -hf        foo/extra => foo/config1
6690 -cL        foo/sym -> ../bar/baz/rsync
6691 +cd          ./
6692 +cd          bar/
6693 +cd          bar/baz/
6694 +cf          bar/baz/rsync
6695 +cd          foo/
6696 +cf          foo/config1
6697 +cf          foo/config2
6698 +hf          foo/extra => foo/config1
6699 +cL          foo/sym -> ../bar/baz/rsync
6700  EOT
6701  diff $diffopt "$chkfile" "$outfile" || test_fail "test 8 failed"
6702  
6703 @@ -159,7 +159,7 @@ rm -rf "$to2dir"
6704  $RSYNC -iplrtH --copy-dest=../to "$fromdir/" "$to2dir/" \
6705      | tee "$outfile"
6706  cat <<EOT >"$chkfile"
6707 -hf        foo/extra => foo/config1
6708 +hf          foo/extra => foo/config1
6709  EOT
6710  diff $diffopt "$chkfile" "$outfile" || test_fail "test 9 failed"
6711  
6712 @@ -186,15 +186,15 @@ $RSYNC -ivvplrtH --link-dest="$todir" "$
6713      | tee "$outfile"
6714  filter_outfile
6715  cat <<EOT >"$chkfile"
6716 -cd        ./
6717 -cd        bar/
6718 -cd        bar/baz/
6719 -hf        bar/baz/rsync
6720 -cd        foo/
6721 -hf        foo/config1
6722 -hf        foo/config2
6723 -hf        foo/extra => foo/config1
6724 -$L        foo/sym -> ../bar/baz/rsync
6725 +cd          ./
6726 +cd          bar/
6727 +cd          bar/baz/
6728 +hf          bar/baz/rsync
6729 +cd          foo/
6730 +hf          foo/config1
6731 +hf          foo/config2
6732 +hf          foo/extra => foo/config1
6733 +$L          foo/sym -> ../bar/baz/rsync
6734  EOT
6735  diff $diffopt "$chkfile" "$outfile" || test_fail "test 11 failed"
6736  
6737 @@ -236,14 +236,14 @@ filter_outfile
6738  # TODO fix really-old problem when combining -H with --compare-dest:
6739  # missing output for foo/extra hard-link (and it might not be updated)!
6740  cat <<EOT >"$chkfile"
6741 -cd        ./
6742 -cd        bar/
6743 -cd        bar/baz/
6744 -.f        bar/baz/rsync
6745 -cd        foo/
6746 -.f        foo/config1
6747 -.f        foo/config2
6748 -.L        foo/sym -> ../bar/baz/rsync
6749 +cd          ./
6750 +cd          bar/
6751 +cd          bar/baz/
6752 +.f          bar/baz/rsync
6753 +cd          foo/
6754 +.f          foo/config1
6755 +.f          foo/config2
6756 +.L          foo/sym -> ../bar/baz/rsync
6757  EOT
6758  diff $diffopt "$chkfile" "$outfile" || test_fail "test 15 failed"
6759  
6760 --- old/uidlist.c
6761 +++ new/uidlist.c
6762 @@ -35,6 +35,7 @@
6763  extern int verbose;
6764  extern int preserve_uid;
6765  extern int preserve_gid;
6766 +extern int preserve_acls;
6767  extern int numeric_ids;
6768  extern int am_root;
6769  
6770 @@ -273,7 +274,7 @@ void send_uid_list(int f)
6771         if (numeric_ids)
6772                 return;
6773  
6774 -       if (preserve_uid) {
6775 +       if (preserve_uid || preserve_acls) {
6776                 int len;
6777                 /* we send sequences of uid/byte-length/name */
6778                 for (list = uidlist; list; list = list->next) {
6779 @@ -290,7 +291,7 @@ void send_uid_list(int f)
6780                 write_int(f, 0);
6781         }
6782  
6783 -       if (preserve_gid) {
6784 +       if (preserve_gid || preserve_acls) {
6785                 int len;
6786                 for (list = gidlist; list; list = list->next) {
6787                         if (!list->name)
6788 @@ -311,7 +312,7 @@ void recv_uid_list(int f, struct file_li
6789         int id, i;
6790         char *name;
6791  
6792 -       if (preserve_uid && !numeric_ids) {
6793 +       if ((preserve_uid || preserve_acls) && !numeric_ids) {
6794                 /* read the uid list */
6795                 while ((id = read_int(f)) != 0) {
6796                         int len = read_byte(f);
6797 @@ -323,7 +324,7 @@ void recv_uid_list(int f, struct file_li
6798                 }
6799         }
6800  
6801 -       if (preserve_gid && !numeric_ids) {
6802 +       if ((preserve_gid || preserve_acls) && !numeric_ids) {
6803                 /* read the gid list */
6804                 while ((id = read_int(f)) != 0) {
6805                         int len = read_byte(f);
6806 @@ -335,6 +336,16 @@ void recv_uid_list(int f, struct file_li
6807                 }
6808         }
6809  
6810 +#ifdef SUPPORT_ACLS
6811 +       if (preserve_acls && !numeric_ids) {
6812 +               id_t *id;
6813 +               while ((id = next_acl_uid(flist)) != NULL)
6814 +                       *id = match_uid(*id);
6815 +               while ((id = next_acl_gid(flist)) != NULL)
6816 +                       *id = match_gid(*id);
6817 +       }
6818 +#endif
6819 +
6820         /* Now convert all the uids/gids from sender values to our values. */
6821         if (am_root && preserve_uid && !numeric_ids) {
6822                 for (i = 0; i < flist->count; i++)
6823 --- old/util.c
6824 +++ new/util.c
6825 @@ -1468,3 +1468,31 @@ int bitbag_next_bit(struct bitbag *bb, i
6826  
6827         return -1;
6828  }
6829 +
6830 +void *expand_item_list(item_list *lp, size_t item_size,
6831 +                      const char *desc, int incr)
6832 +{
6833 +       /* First time through, 0 <= 0, so list is expanded. */
6834 +       if (lp->malloced <= lp->count) {
6835 +               void *new_ptr;
6836 +               size_t new_size = lp->malloced;
6837 +               if (incr < 0)
6838 +                       new_size -= incr; /* increase slowly */
6839 +               else if (new_size < (size_t)incr)
6840 +                       new_size += incr;
6841 +               else
6842 +                       new_size *= 2;
6843 +               new_ptr = realloc_array(lp->items, char, new_size * item_size);
6844 +               if (verbose >= 4) {
6845 +                       rprintf(FINFO, "[%s] expand %s to %.0f bytes, did%s move\n",
6846 +                               who_am_i(), desc, (double)new_size * item_size,
6847 +                               new_ptr == lp->items ? " not" : "");
6848 +               }
6849 +               if (!new_ptr)
6850 +                       out_of_memory("expand_item_list");
6851 +
6852 +               lp->items = new_ptr;
6853 +               lp->malloced = new_size;
6854 +       }
6855 +       return (char*)lp->items + (lp->count++ * item_size);
6856 +}