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