Fixed a compilation error that crept into the code.
[rsync-patches.git] / acls.diff
1 To use this patch, run these commands for a successful build:
2
3     patch -p1 <patches/acls.diff
4     ./prepare-source
5     ./configure --enable-acl-support
6     make
7
8 See the --acls (-A) option in the revised man page for a note on using this
9 latest ACL-enabling patch to send files to an older ACL-enabled rsync.
10
11 --- old/Makefile.in
12 +++ new/Makefile.in
13 @@ -26,15 +26,15 @@ VERSION=@VERSION@
14  .SUFFIXES:
15  .SUFFIXES: .c .o
16  
17 -HEADERS=byteorder.h config.h errcode.h proto.h rsync.h lib/pool_alloc.h
18 +HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
19  LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
20 -       lib/permstring.o lib/pool_alloc.o @LIBOBJS@
21 +       lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
22  ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
23         zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
24  OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
25         util.o main.o checksum.o match.o syscall.o log.o backup.o
26  OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o \
27 -       fileio.o batch.o clientname.o chmod.o
28 +       fileio.o batch.o clientname.o chmod.o acls.o
29  OBJS3=progress.o pipe.o
30  DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
31  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
32 --- old/acls.c
33 +++ new/acls.c
34 @@ -0,0 +1,1093 @@
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 +
67 +/* === ACL structures === */
68 +
69 +typedef struct {
70 +       id_t id;
71 +       uchar access;
72 +} id_access;
73 +
74 +typedef struct {
75 +       id_access *idas;
76 +       int count;
77 +} ida_entries;
78 +
79 +#define NO_ENTRY ((uchar)0x80)
80 +typedef struct rsync_acl {
81 +       ida_entries users;
82 +       ida_entries groups;
83 +       /* These will be NO_ENTRY if there's no such entry. */
84 +       uchar user_obj;
85 +       uchar group_obj;
86 +       uchar mask;
87 +       uchar other;
88 +} rsync_acl;
89 +
90 +typedef struct {
91 +       rsync_acl racl;
92 +       SMB_ACL_T sacl;
93 +} acl_duo;
94 +
95 +static const rsync_acl empty_rsync_acl = {
96 +       {NULL, 0}, {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
97 +};
98 +
99 +static item_list access_acl_list = EMPTY_ITEM_LIST;
100 +static item_list default_acl_list = EMPTY_ITEM_LIST;
101 +
102 +/* === Calculations on ACL types === */
103 +
104 +static const char *str_acl_type(SMB_ACL_TYPE_T type)
105 +{
106 +       return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS"
107 +            : type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT"
108 +            : "unknown SMB_ACL_TYPE_T";
109 +}
110 +
111 +#define OTHER_TYPE(t) (SMB_ACL_TYPE_ACCESS+SMB_ACL_TYPE_DEFAULT-(t))
112 +#define BUMP_TYPE(t) ((t = OTHER_TYPE(t)) == SMB_ACL_TYPE_DEFAULT)
113 +
114 +static int count_racl_entries(const rsync_acl *racl)
115 +{
116 +       return racl->users.count + racl->groups.count
117 +            + (racl->user_obj != NO_ENTRY)
118 +            + (racl->group_obj != NO_ENTRY)
119 +            + (racl->mask != NO_ENTRY)
120 +            + (racl->other != NO_ENTRY);
121 +}
122 +
123 +static int calc_sacl_entries(const rsync_acl *racl)
124 +{
125 +       /* A System ACL always gets user/group/other permission entries. */
126 +       return racl->users.count + racl->groups.count
127 +#ifdef ACLS_NEED_MASK
128 +            + 4;
129 +#else
130 +            + (racl->mask != NO_ENTRY) + 3;
131 +#endif
132 +}
133 +
134 +/* Extracts and returns the permission bits from the ACL.  This cannot be
135 + * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
136 +static int rsync_acl_get_perms(const rsync_acl *racl)
137 +{
138 +       return (racl->user_obj << 6)
139 +            + ((racl->mask != NO_ENTRY ? racl->mask : racl->group_obj) << 3)
140 +            + racl->other;
141 +}
142 +
143 +/* Removes the permission-bit entries from the ACL because these
144 + * can be reconstructed from the file's mode. */
145 +static void rsync_acl_strip_perms(rsync_acl *racl)
146 +{
147 +       racl->user_obj = NO_ENTRY;
148 +       if (racl->mask == NO_ENTRY)
149 +               racl->group_obj = NO_ENTRY;
150 +       else {
151 +               if (racl->group_obj == racl->mask)
152 +                       racl->group_obj = NO_ENTRY;
153 +               racl->mask = NO_ENTRY;
154 +       }
155 +       racl->other = NO_ENTRY;
156 +}
157 +
158 +/* Given an empty rsync_acl, fake up the permission bits. */
159 +static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
160 +{
161 +       racl->user_obj = (mode >> 6) & 7;
162 +       racl->group_obj = (mode >> 3) & 7;
163 +       racl->other = mode & 7;
164 +}
165 +
166 +/* === Rsync ACL functions === */
167 +
168 +static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
169 +{
170 +       id_access *ida1, *ida2;
171 +       int count = ial1->count;
172 +       if (count != ial2->count)
173 +               return False;
174 +       ida1 = ial1->idas;
175 +       ida2 = ial2->idas;
176 +       for (; count--; ida1++, ida2++) {
177 +               if (ida1->access != ida2->access || ida1->id != ida2->id)
178 +                       return False;
179 +       }
180 +       return True;
181 +}
182 +
183 +static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
184 +{
185 +       return racl1->user_obj == racl2->user_obj
186 +           && racl1->group_obj == racl2->group_obj
187 +           && racl1->mask == racl2->mask
188 +           && racl1->other == racl2->other
189 +           && ida_entries_equal(&racl1->users, &racl2->users)
190 +           && ida_entries_equal(&racl1->groups, &racl2->groups);
191 +}
192 +
193 +/* Are the extended (non-permission-bit) entries equal?  If so, the rest of
194 + * the ACL will be handled by the normal mode-preservation code.  This is
195 + * only meaningful for access ACLs!  Note: the 1st arg is a fully-populated
196 + * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
197 + * that it might have several of its permission objects set to NO_ENTRY. */
198 +static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
199 +                                  const rsync_acl *racl2, mode_t m)
200 +{
201 +       if ((racl1->mask ^ racl2->mask) & NO_ENTRY)
202 +               return False; /* One has a mask and the other doesn't */
203 +
204 +       /* When there's a mask, the group_obj becomes an extended entry. */
205 +       if (racl1->mask != NO_ENTRY) {
206 +               /* A condensed rsync_acl with a mask can only have no
207 +                * group_obj when it was identical to the mask.  This
208 +                * means that it was also identical to the group attrs
209 +                * from the mode. */
210 +               if (racl2->group_obj == NO_ENTRY) {
211 +                       if (racl1->group_obj != ((m >> 3) & 7))
212 +                               return False;
213 +               } else if (racl1->group_obj != racl2->group_obj)
214 +                       return False;
215 +       }
216 +       return ida_entries_equal(&racl1->users, &racl2->users)
217 +           && ida_entries_equal(&racl1->groups, &racl2->groups);
218 +}
219 +
220 +static void rsync_acl_free(rsync_acl *racl)
221 +{
222 +       if (racl->users.idas)
223 +               free(racl->users.idas);
224 +       if (racl->groups.idas)
225 +               free(racl->groups.idas);
226 +       *racl = empty_rsync_acl;
227 +}
228 +
229 +void free_acl(statx *sxp)
230 +{
231 +       if (sxp->acc_acl) {
232 +               rsync_acl_free(sxp->acc_acl);
233 +               free(sxp->acc_acl);
234 +               sxp->acc_acl = NULL;
235 +       }
236 +       if (sxp->def_acl) {
237 +               rsync_acl_free(sxp->def_acl);
238 +               free(sxp->def_acl);
239 +               sxp->def_acl = NULL;
240 +       }
241 +}
242 +
243 +static int id_access_sorter(const void *r1, const void *r2)
244 +{
245 +       id_access *ida1 = (id_access *)r1;
246 +       id_access *ida2 = (id_access *)r2;
247 +       id_t rid1 = ida1->id, rid2 = ida2->id;
248 +       return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
249 +}
250 +
251 +static void sort_ida_entries(ida_entries *idal)
252 +{
253 +       if (!idal->count)
254 +               return;
255 +       qsort(idal->idas, idal->count, sizeof idal->idas[0], id_access_sorter);
256 +}
257 +
258 +/* Transfer the count id_access items out of the temp_ida_list into either
259 + * the users or groups ida_entries list in racl. */
260 +static void save_idas(item_list *temp_ida_list, rsync_acl *racl, SMB_ACL_TAG_T type)
261 +{
262 +       id_access *idas;
263 +       ida_entries *ent;
264 +
265 +       if (temp_ida_list->count) {
266 +               int cnt = temp_ida_list->count;
267 +               id_access *temp_idas = temp_ida_list->items;
268 +               if (!(idas = new_array(id_access, cnt)))
269 +                       out_of_memory("save_idas");
270 +               memcpy(idas, temp_idas, cnt * sizeof *temp_idas);
271 +       } else
272 +               idas = NULL;
273 +
274 +       ent = type == SMB_ACL_USER ? &racl->users : &racl->groups;
275 +
276 +       if (ent->count) {
277 +               rprintf(FERROR, "save_idas: disjoint list found for type %d\n", type);
278 +               exit_cleanup(RERR_UNSUPPORTED);
279 +       }
280 +       ent->count = temp_ida_list->count;
281 +       ent->idas = idas;
282 +
283 +       /* Truncate the temporary list now that its idas have been saved. */
284 +       temp_ida_list->count = 0;
285 +}
286 +
287 +/* === System ACLs === */
288 +
289 +/* Unpack system ACL -> rsync ACL verbatim.  Return whether we succeeded. */
290 +static BOOL unpack_smb_acl(rsync_acl *racl, SMB_ACL_T sacl)
291 +{
292 +       static item_list temp_ida_list = EMPTY_ITEM_LIST;
293 +       SMB_ACL_TAG_T prior_list_type = 0;
294 +       SMB_ACL_ENTRY_T entry;
295 +       const char *errfun;
296 +       int rc;
297 +
298 +       errfun = "sys_acl_get_entry";
299 +       for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
300 +            rc == 1;
301 +            rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
302 +               SMB_ACL_TAG_T tag_type;
303 +               SMB_ACL_PERMSET_T permset;
304 +               uchar access;
305 +               void *qualifier;
306 +               id_access *ida;
307 +               if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
308 +                       errfun = "sys_acl_get_tag_type";
309 +                       break;
310 +               }
311 +               if ((rc = sys_acl_get_permset(entry, &permset))) {
312 +                       errfun = "sys_acl_get_tag_type";
313 +                       break;
314 +               }
315 +               access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
316 +                      | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
317 +                      | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
318 +               /* continue == done with entry; break == store in temporary ida list */
319 +               switch (tag_type) {
320 +               case SMB_ACL_USER_OBJ:
321 +                       if (racl->user_obj == NO_ENTRY)
322 +                               racl->user_obj = access;
323 +                       else
324 +                               rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
325 +                       continue;
326 +               case SMB_ACL_USER:
327 +                       break;
328 +               case SMB_ACL_GROUP_OBJ:
329 +                       if (racl->group_obj == NO_ENTRY)
330 +                               racl->group_obj = access;
331 +                       else
332 +                               rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
333 +                       continue;
334 +               case SMB_ACL_GROUP:
335 +                       break;
336 +               case SMB_ACL_MASK:
337 +                       if (racl->mask == NO_ENTRY)
338 +                               racl->mask = access;
339 +                       else
340 +                               rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
341 +                       continue;
342 +               case SMB_ACL_OTHER:
343 +                       if (racl->other == NO_ENTRY)
344 +                               racl->other = access;
345 +                       else
346 +                               rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
347 +                       continue;
348 +               default:
349 +                       rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
350 +                       continue;
351 +               }
352 +               if (!(qualifier = sys_acl_get_qualifier(entry))) {
353 +                       errfun = "sys_acl_get_tag_type";
354 +                       rc = EINVAL;
355 +                       break;
356 +               }
357 +               if (tag_type != prior_list_type) {
358 +                       if (prior_list_type)
359 +                               save_idas(&temp_ida_list, racl, prior_list_type);
360 +                       prior_list_type = tag_type;
361 +               }
362 +               ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
363 +               ida->id = *((id_t *)qualifier);
364 +               ida->access = access;
365 +               sys_acl_free_qualifier(qualifier, tag_type);
366 +       }
367 +       if (rc) {
368 +               rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
369 +               rsync_acl_free(racl);
370 +               return False;
371 +       }
372 +       if (prior_list_type)
373 +               save_idas(&temp_ida_list, racl, prior_list_type);
374 +
375 +       sort_ida_entries(&racl->users);
376 +       sort_ida_entries(&racl->groups);
377 +
378 +#ifdef ACLS_NEED_MASK
379 +       if (!racl->users.count && !racl->groups.count && racl->mask != NO_ENTRY) {
380 +               /* Throw away a superfluous mask, but mask off the
381 +                * group perms with it first. */
382 +               racl->group_obj &= racl->mask;
383 +               racl->mask = NO_ENTRY;
384 +       }
385 +#endif
386 +
387 +       return True;
388 +}
389 +
390 +/* Synactic sugar for system calls */
391 +
392 +#define CALL_OR_ERROR(func,args,str) \
393 +       do { \
394 +               if (func args) { \
395 +                       errfun = str; \
396 +                       goto error_exit; \
397 +               } \
398 +       } while (0)
399 +
400 +#define COE(func,args) CALL_OR_ERROR(func,args,#func)
401 +#define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
402 +
403 +/* Store the permissions in the system ACL entry. */
404 +static int store_access_in_entry(uchar access, SMB_ACL_ENTRY_T entry)
405 +{
406 +       const char *errfun = NULL;
407 +       SMB_ACL_PERMSET_T permset;
408 +
409 +       COE( sys_acl_get_permset,(entry, &permset) );
410 +       COE( sys_acl_clear_perms,(permset) );
411 +       if (access & 4)
412 +               COE( sys_acl_add_perm,(permset, SMB_ACL_READ) );
413 +       if (access & 2)
414 +               COE( sys_acl_add_perm,(permset, SMB_ACL_WRITE) );
415 +       if (access & 1)
416 +               COE( sys_acl_add_perm,(permset, SMB_ACL_EXECUTE) );
417 +       COE( sys_acl_set_permset,(entry, permset) );
418 +
419 +       return 0;
420 +
421 +  error_exit:
422 +       rsyserr(FERROR, errno, "store_access_in_entry %s()", errfun);
423 +       return -1;
424 +}
425 +
426 +/* Pack rsync ACL -> system ACL verbatim.  Return whether we succeeded. */
427 +static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
428 +{
429 +#ifdef ACLS_NEED_MASK
430 +       uchar mask_bits;
431 +#endif
432 +       size_t count;
433 +       id_access *ida;
434 +       const char *errfun = NULL;
435 +       SMB_ACL_ENTRY_T entry;
436 +
437 +       if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
438 +               rsyserr(FERROR, errno, "pack_smb_acl: sys_acl_init()");
439 +               return False;
440 +       }
441 +
442 +       COE( sys_acl_create_entry,(smb_acl, &entry) );
443 +       COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
444 +       COE2( store_access_in_entry,(racl->user_obj & 7, entry) );
445 +
446 +       for (ida = racl->users.idas, count = racl->users.count; count--; ida++) {
447 +               COE( sys_acl_create_entry,(smb_acl, &entry) );
448 +               COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
449 +               COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
450 +               COE2( store_access_in_entry,(ida->access, entry) );
451 +       }
452 +
453 +       COE( sys_acl_create_entry,(smb_acl, &entry) );
454 +       COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
455 +       COE2( store_access_in_entry,(racl->group_obj & 7, entry) );
456 +
457 +       for (ida = racl->groups.idas, count = racl->groups.count; count--; ida++) {
458 +               COE( sys_acl_create_entry,(smb_acl, &entry) );
459 +               COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
460 +               COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
461 +               COE2( store_access_in_entry,(ida->access, entry) );
462 +       }
463 +
464 +#ifdef ACLS_NEED_MASK
465 +       mask_bits = racl->mask == NO_ENTRY ? racl->group_obj & 7 : racl->mask;
466 +       COE( sys_acl_create_entry,(smb_acl, &entry) );
467 +       COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
468 +       COE2( store_access_in_entry,(mask_bits, entry) );
469 +#else
470 +       if (racl->mask != NO_ENTRY) {
471 +               COE( sys_acl_create_entry,(smb_acl, &entry) );
472 +               COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
473 +               COE2( store_access_in_entry,(racl->mask, entry) );
474 +       }
475 +#endif
476 +
477 +       COE( sys_acl_create_entry,(smb_acl, &entry) );
478 +       COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
479 +       COE2( store_access_in_entry,(racl->other & 7, entry) );
480 +
481 +#ifdef DEBUG
482 +       if (sys_acl_valid(*smb_acl) < 0)
483 +               rprintf(FERROR, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
484 +#endif
485 +
486 +       return True;
487 +
488 +  error_exit:
489 +       if (errfun) {
490 +               rsyserr(FERROR, errno, "pack_smb_acl %s()", errfun);
491 +       }
492 +       sys_acl_free_acl(*smb_acl);
493 +       return False;
494 +}
495 +
496 +static int find_matching_rsync_acl(SMB_ACL_TYPE_T type,
497 +                                  const item_list *racl_list,
498 +                                  const rsync_acl *racl)
499 +{
500 +       static int access_match = -1, default_match = -1;
501 +       int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
502 +       size_t count = racl_list->count;
503 +
504 +       /* If this is the first time through or we didn't match the last
505 +        * time, then start at the end of the list, which should be the
506 +        * best place to start hunting. */
507 +       if (*match == -1)
508 +               *match = racl_list->count - 1;
509 +       while (count--) {
510 +               rsync_acl *base = racl_list->items;
511 +               if (rsync_acl_equal(base + *match, racl))
512 +                       return *match;
513 +               if (!(*match)--)
514 +                       *match = racl_list->count - 1;
515 +       }
516 +
517 +       *match = -1;
518 +       return *match;
519 +}
520 +
521 +/* Return the Access Control List for the given filename. */
522 +int get_acl(const char *fname, statx *sxp)
523 +{
524 +       SMB_ACL_TYPE_T type;
525 +
526 +       if (S_ISLNK(sxp->st.st_mode))
527 +               return 0;
528 +
529 +       type = SMB_ACL_TYPE_ACCESS;
530 +       do {
531 +               SMB_ACL_T sacl = sys_acl_get_file(fname, type);
532 +               rsync_acl *racl = new(rsync_acl);
533 +
534 +               if (!racl)
535 +                       out_of_memory("get_acl");
536 +               *racl = empty_rsync_acl;
537 +               if (type == SMB_ACL_TYPE_ACCESS)
538 +                       sxp->acc_acl = racl;
539 +               else
540 +                       sxp->def_acl = racl;
541 +
542 +               if (sacl) {
543 +                       BOOL ok = unpack_smb_acl(racl, sacl);
544 +
545 +                       sys_acl_free_acl(sacl);
546 +                       if (!ok) {
547 +                               free_acl(sxp);
548 +                               return -1;
549 +                       }
550 +               } else if (errno == ENOTSUP || errno == ENOSYS) {
551 +                       /* ACLs are not supported, so pretend we have a basic ACL. */
552 +                       if (type == SMB_ACL_TYPE_ACCESS)
553 +                               rsync_acl_fake_perms(racl, sxp->st.st_mode);
554 +               } else {
555 +                       rsyserr(FERROR, errno, "get_acl: sys_acl_get_file(%s, %s)",
556 +                               fname, str_acl_type(type));
557 +                       free_acl(sxp);
558 +                       return -1;
559 +               }
560 +       } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
561 +
562 +       return 0;
563 +}
564 +
565 +/* === Send functions === */
566 +
567 +/* The general strategy with the tag_type <-> character mapping is that
568 + * lowercase implies that no qualifier follows, where uppercase does.
569 + * A similar idiom for the ACL type (access or default) itself, but
570 + * lowercase in this instance means there's no ACL following, so the
571 + * ACL is a repeat, so the receiver should reuse the last of the same
572 + * type ACL. */
573 +
574 +/* Send the ida list over the file descriptor. */
575 +static void send_ida_entries(int f, const ida_entries *idal, char tag_char)
576 +{
577 +       id_access *ida;
578 +       size_t count = idal->count;
579 +       for (ida = idal->idas; count--; ida++) {
580 +               write_byte(f, tag_char);
581 +               write_byte(f, ida->access);
582 +               write_int(f, ida->id);
583 +               /* FIXME: sorta wasteful: we should maybe buffer as
584 +                * many ids as max(ACL_USER + ACL_GROUP) objects to
585 +                * keep from making so many calls. */
586 +               if (tag_char == 'U')
587 +                       add_uid(ida->id);
588 +               else
589 +                       add_gid(ida->id);
590 +       }
591 +}
592 +
593 +/* Send an rsync ACL over the file descriptor. */
594 +static void send_rsync_acl(int f, const rsync_acl *racl)
595 +{
596 +       size_t count = count_racl_entries(racl);
597 +       write_int(f, count);
598 +       if (racl->user_obj != NO_ENTRY) {
599 +               write_byte(f, 'u');
600 +               write_byte(f, racl->user_obj);
601 +       }
602 +       send_ida_entries(f, &racl->users, 'U');
603 +       if (racl->group_obj != NO_ENTRY) {
604 +               write_byte(f, 'g');
605 +               write_byte(f, racl->group_obj);
606 +       }
607 +       send_ida_entries(f, &racl->groups, 'G');
608 +       if (racl->mask != NO_ENTRY) {
609 +               write_byte(f, 'm');
610 +               write_byte(f, racl->mask);
611 +       }
612 +       if (racl->other != NO_ENTRY) {
613 +               write_byte(f, 'o');
614 +               write_byte(f, racl->other);
615 +       }
616 +}
617 +
618 +/* Send the ACL from the statx structure down the indicated file descriptor.
619 + * This also frees the ACL data. */
620 +void send_acl(statx *sxp, int f)
621 +{
622 +       SMB_ACL_TYPE_T type;
623 +       rsync_acl *racl, *new_racl;
624 +       item_list *racl_list;
625 +       int ndx;
626 +
627 +       if (S_ISLNK(sxp->st.st_mode))
628 +               return;
629 +
630 +       type = SMB_ACL_TYPE_ACCESS;
631 +       racl = sxp->acc_acl;
632 +       racl_list = &access_acl_list;
633 +       do {
634 +               if (!racl) {
635 +                       racl = new(rsync_acl);
636 +                       if (!racl)
637 +                               out_of_memory("send_acl");
638 +                       *racl = empty_rsync_acl;
639 +                       if (type == SMB_ACL_TYPE_ACCESS) {
640 +                               rsync_acl_fake_perms(racl, sxp->st.st_mode);
641 +                               sxp->acc_acl = racl;
642 +                       } else
643 +                               sxp->def_acl = racl;
644 +               }
645 +
646 +               /* Avoid sending values that can be inferred from other data. */
647 +               if (type == SMB_ACL_TYPE_ACCESS)
648 +                       rsync_acl_strip_perms(racl);
649 +               if ((ndx = find_matching_rsync_acl(type, racl_list, racl)) != -1) {
650 +                       write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
651 +                       write_int(f, ndx);
652 +               } else {
653 +                       new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
654 +                       write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
655 +                       send_rsync_acl(f, racl);
656 +                       *new_racl = *racl;
657 +                       *racl = empty_rsync_acl;
658 +               }
659 +               racl = sxp->def_acl;
660 +               racl_list = &default_acl_list;
661 +       } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
662 +
663 +       free_acl(sxp);
664 +}
665 +
666 +/* === Receive functions === */
667 +
668 +static void receive_rsync_acl(rsync_acl *racl, int f, SMB_ACL_TYPE_T type)
669 +{
670 +       static item_list temp_ida_list = EMPTY_ITEM_LIST;
671 +       SMB_ACL_TAG_T tag_type = 0, prior_list_type = 0;
672 +       uchar computed_mask_bits = 0;
673 +       id_access *ida;
674 +       size_t count;
675 +
676 +       if (!(count = read_int(f)))
677 +               return;
678 +
679 +       while (count--) {
680 +               char tag = read_byte(f);
681 +               uchar access = read_byte(f);
682 +               if (access & ~ (4 | 2 | 1)) {
683 +                       rprintf(FERROR, "receive_rsync_acl: bogus permset %o\n",
684 +                               access);
685 +                       exit_cleanup(RERR_STREAMIO);
686 +               }
687 +               switch (tag) {
688 +               case 'u':
689 +                       if (racl->user_obj != NO_ENTRY) {
690 +                               rprintf(FERROR, "receive_rsync_acl: error: duplicate USER_OBJ entry\n");
691 +                               exit_cleanup(RERR_STREAMIO);
692 +                       }
693 +                       racl->user_obj = access;
694 +                       continue;
695 +               case 'U':
696 +                       tag_type = SMB_ACL_USER;
697 +                       break;
698 +               case 'g':
699 +                       if (racl->group_obj != NO_ENTRY) {
700 +                               rprintf(FERROR, "receive_rsync_acl: error: duplicate GROUP_OBJ entry\n");
701 +                               exit_cleanup(RERR_STREAMIO);
702 +                       }
703 +                       racl->group_obj = access;
704 +                       continue;
705 +               case 'G':
706 +                       tag_type = SMB_ACL_GROUP;
707 +                       break;
708 +               case 'm':
709 +                       if (racl->mask != NO_ENTRY) {
710 +                               rprintf(FERROR, "receive_rsync_acl: error: duplicate MASK entry\n");
711 +                               exit_cleanup(RERR_STREAMIO);
712 +                       }
713 +                       racl->mask = access;
714 +                       continue;
715 +               case 'o':
716 +                       if (racl->other != NO_ENTRY) {
717 +                               rprintf(FERROR, "receive_rsync_acl: error: duplicate OTHER entry\n");
718 +                               exit_cleanup(RERR_STREAMIO);
719 +                       }
720 +                       racl->other = access;
721 +                       continue;
722 +               default:
723 +                       rprintf(FERROR, "receive_rsync_acl: unknown tag %c\n",
724 +                               tag);
725 +                       exit_cleanup(RERR_STREAMIO);
726 +               }
727 +               if (tag_type != prior_list_type) {
728 +                       if (prior_list_type)
729 +                               save_idas(&temp_ida_list, racl, prior_list_type);
730 +                       prior_list_type = tag_type;
731 +               }
732 +               ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
733 +               ida->access = access;
734 +               ida->id = read_int(f);
735 +               computed_mask_bits |= access;
736 +       }
737 +       if (prior_list_type)
738 +               save_idas(&temp_ida_list, racl, prior_list_type);
739 +
740 +       if (type == SMB_ACL_TYPE_DEFAULT) {
741 +               /* Ensure that these are never unset. */
742 +               if (racl->user_obj == NO_ENTRY)
743 +                       racl->user_obj = 7;
744 +               if (racl->group_obj == NO_ENTRY)
745 +                       racl->group_obj = 0;
746 +               if (racl->other == NO_ENTRY)
747 +                       racl->other = 0;
748 +       }
749 +
750 +       if (!racl->users.count && !racl->groups.count) {
751 +               /* If we received a superfluous mask, throw it away. */
752 +               if (racl->mask != NO_ENTRY) {
753 +                       /* Mask off the group perms with it first. */
754 +                       racl->group_obj &= racl->mask | NO_ENTRY;
755 +                       racl->mask = NO_ENTRY;
756 +               }
757 +       } else if (racl->mask == NO_ENTRY) /* Must be non-empty with lists. */
758 +               racl->mask = computed_mask_bits | (racl->group_obj & 7);
759 +}
760 +
761 +/* Receive the ACL info the sender has included for this file-list entry. */
762 +void receive_acl(struct file_struct *file, int f)
763 +{
764 +       SMB_ACL_TYPE_T type;
765 +       item_list *racl_list;
766 +
767 +       if (S_ISLNK(file->mode))
768 +               return;
769 +
770 +       type = SMB_ACL_TYPE_ACCESS;
771 +       racl_list = &access_acl_list;
772 +       do {
773 +               char tag = read_byte(f);
774 +               int ndx;
775 +
776 +               if (tag == 'A' || tag == 'a') {
777 +                       if (type != SMB_ACL_TYPE_ACCESS) {
778 +                               rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
779 +                                       f_name(file, NULL));
780 +                               exit_cleanup(RERR_STREAMIO);
781 +                       }
782 +               } else if (tag == 'D' || tag == 'd') {
783 +                       if (type == SMB_ACL_TYPE_ACCESS) {
784 +                               rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
785 +                                       f_name(file, NULL));
786 +                               exit_cleanup(RERR_STREAMIO);
787 +                       }
788 +               } else {
789 +                       rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
790 +                               f_name(file, NULL), tag);
791 +                       exit_cleanup(RERR_STREAMIO);
792 +               }
793 +               if (tag == 'A' || tag == 'D') {
794 +                       acl_duo *duo_item;
795 +                       ndx = racl_list->count;
796 +                       duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
797 +                       duo_item->racl = empty_rsync_acl;
798 +                       receive_rsync_acl(&duo_item->racl, f, type);
799 +                       duo_item->sacl = NULL;
800 +               } else {
801 +                       ndx = read_int(f);
802 +                       if (ndx < 0 || (size_t)ndx >= racl_list->count) {
803 +                               rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
804 +                                       f_name(file, NULL), str_acl_type(type), ndx);
805 +                               exit_cleanup(RERR_STREAMIO);
806 +                       }
807 +               }
808 +               if (type == SMB_ACL_TYPE_ACCESS)
809 +                       F_ACL(file) = ndx;
810 +               else
811 +                       F_DEF_ACL(file) = ndx;
812 +               racl_list = &default_acl_list;
813 +       } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
814 +}
815 +
816 +/* Turn the ACL data in statx into cached ACL data, setting the index
817 + * values in the file struct. */
818 +void cache_acl(struct file_struct *file, statx *sxp)
819 +{
820 +       SMB_ACL_TYPE_T type;
821 +       rsync_acl *racl;
822 +       item_list *racl_list;
823 +       int ndx;
824 +
825 +       if (S_ISLNK(file->mode))
826 +               return;
827 +
828 +       type = SMB_ACL_TYPE_ACCESS;
829 +       racl = sxp->acc_acl;
830 +       racl_list = &access_acl_list;
831 +       do {
832 +               if (!racl)
833 +                       ndx = -1;
834 +               else if ((ndx = find_matching_rsync_acl(type, racl_list, racl)) == -1) {
835 +                       acl_duo *new_duo;
836 +                       ndx = racl_list->count;
837 +                       new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
838 +                       new_duo->racl = *racl;
839 +                       new_duo->sacl = NULL;
840 +                       *racl = empty_rsync_acl;
841 +               }
842 +               if (type == SMB_ACL_TYPE_ACCESS)
843 +                       F_ACL(file) = ndx;
844 +               else
845 +                       F_DEF_ACL(file) = ndx;
846 +               racl = sxp->def_acl;
847 +               racl_list = &default_acl_list;
848 +       } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
849 +
850 +       free_acl(sxp);
851 +}
852 +
853 +static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
854 +{
855 +       SMB_ACL_ENTRY_T entry;
856 +       const char *errfun;
857 +       int rc;
858 +
859 +       if (S_ISDIR(mode)) {
860 +               /* If the sticky bit is going on, it's not safe to allow all
861 +                * the new ACL to go into effect before it gets set. */
862 +#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
863 +               if (mode & S_ISVTX)
864 +                       mode &= ~0077;
865 +#else
866 +               if (mode & S_ISVTX && !(old_mode & S_ISVTX))
867 +                       mode &= ~0077;
868 +       } else {
869 +               /* If setuid or setgid is going off, it's not safe to allow all
870 +                * the new ACL to go into effect before they get cleared. */
871 +               if ((old_mode & S_ISUID && !(mode & S_ISUID))
872 +                || (old_mode & S_ISGID && !(mode & S_ISGID)))
873 +                       mode &= ~0077;
874 +#endif
875 +       }
876 +
877 +       errfun = "sys_acl_get_entry";
878 +       for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
879 +            rc == 1;
880 +            rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
881 +               SMB_ACL_TAG_T tag_type;
882 +               if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
883 +                       errfun = "sys_acl_get_tag_type";
884 +                       break;
885 +               }
886 +               switch (tag_type) {
887 +               case SMB_ACL_USER_OBJ:
888 +                       COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
889 +                       break;
890 +               case SMB_ACL_GROUP_OBJ:
891 +                       /* group is only empty when identical to group perms. */
892 +                       if (racl->group_obj != NO_ENTRY)
893 +                               break;
894 +                       COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
895 +                       break;
896 +               case SMB_ACL_MASK:
897 +#ifndef ACLS_NEED_MASK
898 +                       /* mask is only empty when we don't need it. */
899 +                       if (racl->mask == NO_ENTRY)
900 +                               break;
901 +#endif
902 +                       COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
903 +                       break;
904 +               case SMB_ACL_OTHER:
905 +                       COE2( store_access_in_entry,(mode & 7, entry) );
906 +                       break;
907 +               }
908 +       }
909 +       if (rc) {
910 +         error_exit:
911 +               if (errfun) {
912 +                       rsyserr(FERROR, errno, "change_sacl_perms: %s()",
913 +                               errfun);
914 +               }
915 +               return (mode_t)~0;
916 +       }
917 +
918 +#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
919 +       /* Ensure that chmod() will be called to restore any lost setid bits. */
920 +       if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
921 +        && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
922 +               old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
923 +#endif
924 +
925 +       /* Return the mode of the file on disk, as we will set them. */
926 +       return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
927 +}
928 +
929 +/* Set ACL on indicated filename.
930 + *
931 + * This sets extended access ACL entries and default ACL.  If convenient,
932 + * it sets permission bits along with the access ACL and signals having
933 + * done so by modifying sxp->st.st_mode.
934 + *
935 + * Returns 1 for unchanged, 0 for changed, -1 for failed.  Call this
936 + * with fname set to NULL to just check if the ACL is unchanged. */
937 +int set_acl(const char *fname, const struct file_struct *file, statx *sxp)
938 +{
939 +       int unchanged = 1;
940 +       SMB_ACL_TYPE_T type;
941 +
942 +       if (!dry_run && (read_only || list_only)) {
943 +               errno = EROFS;
944 +               return -1;
945 +       }
946 +
947 +       if (S_ISLNK(file->mode))
948 +               return 1;
949 +
950 +       type = SMB_ACL_TYPE_ACCESS;
951 +       do {
952 +               acl_duo *duo_item;
953 +               int32 ndx;
954 +               BOOL eq;
955 +
956 +               if (type == SMB_ACL_TYPE_ACCESS) {
957 +                       ndx = F_ACL(file);
958 +                       if (ndx < 0 || (size_t)ndx >= access_acl_list.count)
959 +                               continue;
960 +                       duo_item = access_acl_list.items;
961 +                       duo_item += ndx;
962 +                       eq = sxp->acc_acl
963 +                           && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
964 +               } else {
965 +                       ndx = F_DEF_ACL(file);
966 +                       if (ndx < 0 || (size_t)ndx >= default_acl_list.count)
967 +                               continue;
968 +                       duo_item = default_acl_list.items;
969 +                       duo_item += ndx;
970 +                       eq = sxp->def_acl
971 +                           && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
972 +               }
973 +               if (eq)
974 +                       continue;
975 +               if (!dry_run && fname) {
976 +                       if (type == SMB_ACL_TYPE_DEFAULT
977 +                        && duo_item->racl.user_obj == NO_ENTRY) {
978 +                               if (sys_acl_delete_def_file(fname) < 0) {
979 +                                       rsyserr(FERROR, errno, "set_acl: sys_acl_delete_def_file(%s)",
980 +                                               fname);
981 +                                       unchanged = -1;
982 +                                       continue;
983 +                               }
984 +                       } else {
985 +                               mode_t cur_mode = sxp->st.st_mode;
986 +                               if (!duo_item->sacl
987 +                                && !pack_smb_acl(&duo_item->sacl, &duo_item->racl)) {
988 +                                       unchanged = -1;
989 +                                       continue;
990 +                               }
991 +                               if (type == SMB_ACL_TYPE_ACCESS) {
992 +                                       cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
993 +                                                                    cur_mode, file->mode);
994 +                                       if (cur_mode == (mode_t)~0)
995 +                                               continue;
996 +                               }
997 +                               if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
998 +                                       rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
999 +                                               fname, str_acl_type(type));
1000 +                                       unchanged = -1;
1001 +                                       continue;
1002 +                               }
1003 +                               if (type == SMB_ACL_TYPE_ACCESS)
1004 +                                       sxp->st.st_mode = cur_mode;
1005 +                       }
1006 +               }
1007 +               if (unchanged == 1)
1008 +                       unchanged = 0;
1009 +       } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
1010 +
1011 +       return unchanged;
1012 +}
1013 +
1014 +/* === Enumeration functions for uid mapping === */
1015 +
1016 +/* Context -- one and only one.  Should be cycled through once on uid
1017 + * mapping and once on gid mapping. */
1018 +static item_list *_enum_racl_lists[] = {
1019 +       &access_acl_list, &default_acl_list, NULL
1020 +};
1021 +
1022 +static item_list **enum_racl_list = &_enum_racl_lists[0];
1023 +static int enum_ida_index = 0;
1024 +static size_t enum_racl_index = 0;
1025 +
1026 +/* This returns the next tag_type id from the given ACL for the next entry,
1027 + * or it returns 0 if there are no more tag_type ids in the acl. */
1028 +static id_t *next_ace_id(SMB_ACL_TAG_T tag_type, const rsync_acl *racl)
1029 +{
1030 +       const ida_entries *idal = tag_type == SMB_ACL_USER ? &racl->users : &racl->groups;
1031 +       if (enum_ida_index < idal->count) {
1032 +               id_access *ida = &idal->idas[enum_ida_index++];
1033 +               return &ida->id;
1034 +       }
1035 +       enum_ida_index = 0;
1036 +       return NULL;
1037 +}
1038 +
1039 +static id_t *next_acl_id(SMB_ACL_TAG_T tag_type, const item_list *racl_list)
1040 +{
1041 +       for (; enum_racl_index < racl_list->count; enum_racl_index++) {
1042 +               id_t *id;
1043 +               acl_duo *duo_item = racl_list->items;
1044 +               duo_item += enum_racl_index;
1045 +               if ((id = next_ace_id(tag_type, &duo_item->racl)) != NULL)
1046 +                       return id;
1047 +       }
1048 +       enum_racl_index = 0;
1049 +       return NULL;
1050 +}
1051 +
1052 +static id_t *next_acl_list_id(SMB_ACL_TAG_T tag_type)
1053 +{
1054 +       for (; *enum_racl_list; enum_racl_list++) {
1055 +               id_t *id = next_acl_id(tag_type, *enum_racl_list);
1056 +               if (id)
1057 +                       return id;
1058 +       }
1059 +       enum_racl_list = &_enum_racl_lists[0];
1060 +       return NULL;
1061 +}
1062 +
1063 +id_t *next_acl_uid()
1064 +{
1065 +       return next_acl_list_id(SMB_ACL_USER);
1066 +}
1067 +
1068 +id_t *next_acl_gid()
1069 +{
1070 +       return next_acl_list_id(SMB_ACL_GROUP);
1071 +}
1072 +
1073 +/* This is used by dest_mode(). */
1074 +int default_perms_for_dir(const char *dir)
1075 +{
1076 +       rsync_acl racl;
1077 +       SMB_ACL_T sacl;
1078 +       BOOL ok;
1079 +       int perms;
1080 +
1081 +       if (dir == NULL)
1082 +               dir = ".";
1083 +       perms = ACCESSPERMS & ~orig_umask;
1084 +       /* Read the directory's default ACL.  If it has none, this will successfully return an empty ACL. */
1085 +       sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1086 +       if (sacl == NULL) {
1087 +               /* Couldn't get an ACL.  Darn. */
1088 +               switch (errno) {
1089 +               case ENOTSUP:
1090 +               case ENOSYS:
1091 +                       /* No ACLs are available. */
1092 +                       break;
1093 +               case ENOENT:
1094 +                       if (dry_run) {
1095 +                               /* We're doing a dry run, so the containing directory
1096 +                                * wasn't actually created.  Don't worry about it. */
1097 +                               break;
1098 +                       }
1099 +                       /* Otherwise fall through. */
1100 +               default:
1101 +                       rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1102 +                               dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1103 +               }
1104 +               return perms;
1105 +       }
1106 +
1107 +       /* Convert it. */
1108 +       racl = empty_rsync_acl;
1109 +       ok = unpack_smb_acl(&racl, sacl);
1110 +       sys_acl_free_acl(sacl);
1111 +       if (!ok) {
1112 +               rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1113 +               return perms;
1114 +       }
1115 +
1116 +       /* Apply the permission-bit entries of the default ACL, if any. */
1117 +       if (racl.user_obj != NO_ENTRY) {
1118 +               perms = rsync_acl_get_perms(&racl);
1119 +               if (verbose > 2)
1120 +                       rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1121 +       }
1122 +
1123 +       rsync_acl_free(&racl);
1124 +       return perms;
1125 +}
1126 +
1127 +#endif /* SUPPORT_ACLS */
1128 --- old/backup.c
1129 +++ new/backup.c
1130 @@ -23,6 +23,7 @@
1131  
1132  extern int verbose;
1133  extern int am_root;
1134 +extern int preserve_acls;
1135  extern int preserve_devices;
1136  extern int preserve_specials;
1137  extern int preserve_links;
1138 @@ -93,7 +94,8 @@ path
1139  ****************************************************************************/
1140  static int make_bak_dir(char *fullpath)
1141  {
1142 -       STRUCT_STAT st;
1143 +       statx sx;
1144 +       struct file_struct *file;
1145         char *rel = fullpath + backup_dir_len;
1146         char *end = rel + strlen(rel);
1147         char *p = end;
1148 @@ -125,15 +127,24 @@ static int make_bak_dir(char *fullpath)
1149                 if (p >= rel) {
1150                         /* Try to transfer the directory settings of the
1151                          * actual dir that the files are coming from. */
1152 -                       if (do_stat(rel, &st) < 0) {
1153 +                       if (do_stat(rel, &sx.st) < 0) {
1154                                 rsyserr(FERROR, errno,
1155                                         "make_bak_dir stat %s failed",
1156                                         full_fname(rel));
1157                         } else {
1158 -                               do_lchown(fullpath, st.st_uid, st.st_gid);
1159 -#ifdef HAVE_CHMOD
1160 -                               do_chmod(fullpath, st.st_mode);
1161 +#ifdef SUPPORT_ACLS
1162 +                               sx.acc_acl = sx.def_acl = NULL;
1163 +#endif
1164 +                               if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
1165 +                                       continue;
1166 +#ifdef SUPPORT_ACLS
1167 +                               if (preserve_acls) {
1168 +                                       get_acl(rel, &sx);
1169 +                                       cache_acl(file, &sx);
1170 +                               }
1171  #endif
1172 +                               set_file_attrs(fullpath, file, NULL, 0);
1173 +                               free(file);
1174                         }
1175                 }
1176                 *p = '/';
1177 @@ -171,15 +182,18 @@ static int robust_move(const char *src, 
1178   * We will move the file to be deleted into a parallel directory tree. */
1179  static int keep_backup(const char *fname)
1180  {
1181 -       STRUCT_STAT st;
1182 +       statx sx;
1183         struct file_struct *file;
1184         char *buf;
1185         int kept = 0;
1186         int ret_code;
1187  
1188         /* return if no file to keep */
1189 -       if (do_lstat(fname, &st) < 0)
1190 +       if (do_lstat(fname, &sx.st) < 0)
1191                 return 1;
1192 +#ifdef SUPPORT_ACLS
1193 +       sx.acc_acl = sx.def_acl = NULL;
1194 +#endif
1195  
1196         if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
1197                 return 1; /* the file could have disappeared */
1198 @@ -189,6 +203,13 @@ static int keep_backup(const char *fname
1199                 return 0;
1200         }
1201  
1202 +#ifdef SUPPORT_ACLS
1203 +       if (preserve_acls) {
1204 +               get_acl(fname, &sx);
1205 +               cache_acl(file, &sx);
1206 +       }
1207 +#endif
1208 +
1209         /* Check to see if this is a device file, or link */
1210         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1211          || (preserve_specials && IS_SPECIAL(file->mode))) {
1212 @@ -260,7 +281,7 @@ static int keep_backup(const char *fname
1213                 if (robust_move(fname, buf) != 0) {
1214                         rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
1215                                 full_fname(fname), buf);
1216 -               } else if (st.st_nlink > 1) {
1217 +               } else if (sx.st.st_nlink > 1) {
1218                         /* If someone has hard-linked the file into the backup
1219                          * dir, rename() might return success but do nothing! */
1220                         robust_unlink(fname); /* Just in case... */
1221 --- old/compat.c
1222 +++ new/compat.c
1223 @@ -48,6 +48,8 @@ void setup_protocol(int f_out,int f_in)
1224                 preserve_uid = ++flist_extra_cnt;
1225         if (preserve_gid)
1226                 preserve_gid = ++flist_extra_cnt;
1227 +       if (preserve_acls && !am_sender)
1228 +               preserve_acls = ++flist_extra_cnt;
1229  
1230         if (remote_protocol == 0) {
1231                 if (!read_batch)
1232 --- old/configure.in
1233 +++ new/configure.in
1234 @@ -537,6 +537,11 @@ if test x"$ac_cv_func_strcasecmp" = x"no
1235      AC_CHECK_LIB(resolv, strcasecmp)
1236  fi
1237  
1238 +AC_CHECK_FUNCS(aclsort)
1239 +if test x"$ac_cv_func_aclsort" = x"no"; then
1240 +    AC_CHECK_LIB(sec, aclsort)
1241 +fi
1242 +
1243  dnl At the moment we don't test for a broken memcmp(), because all we
1244  dnl need to do is test for equality, not comparison, and it seems that
1245  dnl every platform has a memcmp that can do at least that.
1246 @@ -801,6 +806,78 @@ AC_SUBST(OBJ_RESTORE)
1247  AC_SUBST(CC_SHOBJ_FLAG)
1248  AC_SUBST(BUILD_POPT)
1249  
1250 +AC_CHECK_HEADERS(sys/acl.h acl/libacl.h)
1251 +AC_CHECK_FUNCS(_acl __acl _facl __facl)
1252 +#################################################
1253 +# check for ACL support
1254 +
1255 +AC_MSG_CHECKING(whether to support ACLs)
1256 +AC_ARG_ENABLE(acl-support,
1257 +AC_HELP_STRING([--enable-acl-support], [Include ACL support (default=no)]),
1258 +[ case "$enableval" in
1259 +  yes)
1260 +
1261 +               case "$host_os" in
1262 +               *sysv5*)
1263 +                       AC_MSG_RESULT(Using UnixWare ACLs)
1264 +                       AC_DEFINE(HAVE_UNIXWARE_ACLS, 1, [true if you have UnixWare ACLs])
1265 +                       ;;
1266 +               *solaris*|*cygwin*)
1267 +                       AC_MSG_RESULT(Using solaris ACLs)
1268 +                       AC_DEFINE(HAVE_SOLARIS_ACLS, 1, [true if you have solaris ACLs])
1269 +                       ;;
1270 +               *hpux*)
1271 +                       AC_MSG_RESULT(Using HPUX ACLs)
1272 +                       AC_DEFINE(HAVE_HPUX_ACLS, 1, [true if you have HPUX ACLs])
1273 +                       ;;
1274 +               *irix*)
1275 +                       AC_MSG_RESULT(Using IRIX ACLs)
1276 +                       AC_DEFINE(HAVE_IRIX_ACLS, 1, [true if you have IRIX ACLs])
1277 +                       ;;
1278 +               *aix*)
1279 +                       AC_MSG_RESULT(Using AIX ACLs)
1280 +                       AC_DEFINE(HAVE_AIX_ACLS, 1, [true if you have AIX ACLs])
1281 +                       ;;
1282 +               *osf*)
1283 +                       AC_MSG_RESULT(Using Tru64 ACLs)
1284 +                       AC_DEFINE(HAVE_TRU64_ACLS, 1, [true if you have Tru64 ACLs])
1285 +                       LIBS="$LIBS -lpacl"
1286 +                       ;;
1287 +               *)
1288 +                   AC_MSG_RESULT(ACLs requested -- running tests)
1289 +                   AC_CHECK_LIB(acl,acl_get_file)
1290 +                       AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1291 +                       AC_TRY_LINK([#include <sys/types.h>
1292 +#include <sys/acl.h>],
1293 +[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1294 +samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
1295 +                       AC_MSG_CHECKING(ACL test results)
1296 +                       if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1297 +                           AC_MSG_RESULT(Using posix ACLs)
1298 +                           AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
1299 +                           AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
1300 +                               AC_TRY_LINK([#include <sys/types.h>
1301 +#include <sys/acl.h>],
1302 +[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1303 +samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
1304 +                           if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1305 +                               AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1306 +                           fi
1307 +                       else
1308 +                           AC_MSG_ERROR(Failed to find ACL support)
1309 +                       fi
1310 +                       ;;
1311 +               esac
1312 +               ;;
1313 +  *)
1314 +    AC_MSG_RESULT(no)
1315 +       AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1316 +    ;;
1317 +  esac ],
1318 +  AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1319 +  AC_MSG_RESULT(no)
1320 +)
1321 +
1322  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1323  AC_OUTPUT
1324  
1325 --- old/flist.c
1326 +++ new/flist.c
1327 @@ -41,6 +41,7 @@ extern int filesfrom_fd;
1328  extern int one_file_system;
1329  extern int copy_dirlinks;
1330  extern int keep_dirlinks;
1331 +extern int preserve_acls;
1332  extern int preserve_links;
1333  extern int preserve_hard_links;
1334  extern int preserve_devices;
1335 @@ -147,6 +148,8 @@ static void list_file_entry(struct file_
1336         permstring(permbuf, f->mode);
1337         len = F_LENGTH(f);
1338  
1339 +       /* TODO: indicate '+' if the entry has an ACL. */
1340 +
1341  #ifdef SUPPORT_LINKS
1342         if (preserve_links && S_ISLNK(f->mode)) {
1343                 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
1344 @@ -662,6 +665,12 @@ static struct file_struct *recv_file_ent
1345         }
1346  #endif
1347  
1348 +#ifdef SUPPORT_ACLS
1349 +       /* We need one or two index int32s when we're preserving ACLs. */
1350 +       if (preserve_acls)
1351 +               extra_len += (S_ISDIR(mode) ? 2 : 1) * EXTRA_LEN;
1352 +#endif
1353 +
1354         if (always_checksum && S_ISREG(mode))
1355                 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
1356  
1357 @@ -799,6 +808,11 @@ static struct file_struct *recv_file_ent
1358                         read_buf(f, bp, checksum_len);
1359         }
1360  
1361 +#ifdef SUPPORT_ACLS
1362 +       if (preserve_acls)
1363 +               receive_acl(file, f);
1364 +#endif
1365 +
1366         return file;
1367  }
1368  
1369 @@ -1068,6 +1082,9 @@ static struct file_struct *send_file_nam
1370                                           unsigned short flags)
1371  {
1372         struct file_struct *file;
1373 +#ifdef SUPPORT_ACLS
1374 +       statx sx;
1375 +#endif
1376  
1377         file = make_file(fname, flist, stp, flags,
1378                          f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1379 @@ -1077,12 +1094,26 @@ static struct file_struct *send_file_nam
1380         if (chmod_modes && !S_ISLNK(file->mode))
1381                 file->mode = tweak_mode(file->mode, chmod_modes);
1382  
1383 +#ifdef SUPPORT_ACLS
1384 +       if (preserve_acls && f >= 0) {
1385 +               sx.st.st_mode = file->mode;
1386 +               sx.acc_acl = sx.def_acl = NULL;
1387 +               if (get_acl(fname, &sx) < 0)
1388 +                       return NULL;
1389 +       }
1390 +#endif
1391 +
1392         maybe_emit_filelist_progress(flist->count + flist_count_offset);
1393  
1394         flist_expand(flist);
1395         flist->files[flist->count++] = file;
1396 -       if (f >= 0)
1397 +       if (f >= 0) {
1398                 send_file_entry(file, f, flist->count - 1);
1399 +#ifdef SUPPORT_ACLS
1400 +               if (preserve_acls)
1401 +                       send_acl(&sx, f);
1402 +#endif
1403 +       }
1404         return file;
1405  }
1406  
1407 --- old/generator.c
1408 +++ new/generator.c
1409 @@ -35,6 +35,7 @@ extern int do_progress;
1410  extern int relative_paths;
1411  extern int implied_dirs;
1412  extern int keep_dirlinks;
1413 +extern int preserve_acls;
1414  extern int preserve_links;
1415  extern int preserve_devices;
1416  extern int preserve_specials;
1417 @@ -87,6 +88,7 @@ extern int force_delete;
1418  extern int one_file_system;
1419  extern struct stats stats;
1420  extern dev_t filesystem_dev;
1421 +extern mode_t orig_umask;
1422  extern char *backup_dir;
1423  extern char *backup_suffix;
1424  extern int backup_suffix_len;
1425 @@ -518,21 +520,26 @@ static void do_delete_pass(struct file_l
1426                 rprintf(FINFO, "                    \r");
1427  }
1428  
1429 -int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
1430 +int unchanged_attrs(struct file_struct *file, statx *sxp)
1431  {
1432 -       if (preserve_perms && !BITS_EQUAL(st->st_mode, file->mode, CHMOD_BITS))
1433 +       if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
1434                 return 0;
1435  
1436 -       if (am_root && preserve_uid && st->st_uid != F_UID(file))
1437 +       if (am_root && preserve_uid && sxp->st.st_uid != F_UID(file))
1438                 return 0;
1439  
1440 -       if (preserve_gid && F_GID(file) != GID_NONE && st->st_gid != F_GID(file))
1441 +       if (preserve_gid && F_GID(file) != GID_NONE && sxp->st.st_gid != F_GID(file))
1442                 return 0;
1443  
1444 +#ifdef SUPPORT_ACLS
1445 +       if (preserve_acls && set_acl(NULL, file, sxp) == 0)
1446 +               return 0;
1447 +#endif
1448 +
1449         return 1;
1450  }
1451  
1452 -void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
1453 +void itemize(struct file_struct *file, int ndx, int statret, statx *sxp,
1454              int32 iflags, uchar fnamecmp_type, const char *xname)
1455  {
1456         if (statret >= 0) { /* A from-dest-dir statret can == 1! */
1457 @@ -540,20 +547,24 @@ void itemize(struct file_struct *file, i
1458                     : S_ISDIR(file->mode) ? !omit_dir_times
1459                     : !S_ISLNK(file->mode);
1460  
1461 -               if (S_ISREG(file->mode) && F_LENGTH(file) != st->st_size)
1462 +               if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
1463                         iflags |= ITEM_REPORT_SIZE;
1464                 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
1465                   && !(iflags & ITEM_MATCHED)
1466                   && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
1467 -                || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
1468 +                || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
1469                         iflags |= ITEM_REPORT_TIME;
1470 -               if (!BITS_EQUAL(st->st_mode, file->mode, CHMOD_BITS))
1471 +               if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
1472                         iflags |= ITEM_REPORT_PERMS;
1473 -               if (preserve_uid && am_root && F_UID(file) != st->st_uid)
1474 +               if (preserve_uid && am_root && F_UID(file) != sxp->st.st_uid)
1475                         iflags |= ITEM_REPORT_OWNER;
1476                 if (preserve_gid && F_GID(file) != GID_NONE
1477 -                   && st->st_gid != F_GID(file))
1478 +                   && sxp->st.st_gid != F_GID(file))
1479                         iflags |= ITEM_REPORT_GROUP;
1480 +#ifdef SUPPORT_ACLS
1481 +               if (preserve_acls && set_acl(NULL, file, sxp) == 0)
1482 +                       iflags |= ITEM_REPORT_ACL;
1483 +#endif
1484         } else
1485                 iflags |= ITEM_IS_NEW;
1486  
1487 @@ -808,7 +819,7 @@ void check_for_finished_hlinks(int itemi
1488   * handling the file, -1 if no dest-linking occurred, or a non-negative
1489   * value if we found an alternate basis file. */
1490  static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
1491 -                        char *cmpbuf, STRUCT_STAT *stp, int itemizing,
1492 +                        char *cmpbuf, statx *sxp, int itemizing,
1493                          enum logcode code)
1494  {
1495         int best_match = -1;
1496 @@ -817,7 +828,7 @@ static int try_dests_reg(struct file_str
1497  
1498         do {
1499                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1500 -               if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
1501 +               if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
1502                         continue;
1503                 switch (match_level) {
1504                 case 0:
1505 @@ -825,16 +836,20 @@ static int try_dests_reg(struct file_str
1506                         match_level = 1;
1507                         /* FALL THROUGH */
1508                 case 1:
1509 -                       if (!unchanged_file(cmpbuf, file, stp))
1510 +                       if (!unchanged_file(cmpbuf, file, &sxp->st))
1511                                 continue;
1512                         best_match = j;
1513                         match_level = 2;
1514                         /* FALL THROUGH */
1515                 case 2:
1516 -                       if (!unchanged_attrs(file, stp))
1517 +#ifdef SUPPORT_ACLS
1518 +                       if (preserve_acls)
1519 +                               get_acl(cmpbuf, sxp);
1520 +#endif
1521 +                       if (!unchanged_attrs(file, sxp))
1522                                 continue;
1523                         if (always_checksum && preserve_times
1524 -                        && cmp_time(stp->st_mtime, file->modtime))
1525 +                        && cmp_time(sxp->st.st_mtime, file->modtime))
1526                                 continue;
1527                         best_match = j;
1528                         match_level = 3;
1529 @@ -849,7 +864,7 @@ static int try_dests_reg(struct file_str
1530         if (j != best_match) {
1531                 j = best_match;
1532                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1533 -               if (link_stat(cmpbuf, stp, 0) < 0)
1534 +               if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1535                         return -1;
1536         }
1537  
1538 @@ -859,16 +874,25 @@ static int try_dests_reg(struct file_str
1539                         if (!hard_link_one(file, fname, cmpbuf, 1))
1540                                 goto try_a_copy;
1541                         if (preserve_hard_links && F_IS_HLINKED(file))
1542 -                               finish_hard_link(file, fname, stp, itemizing, code, j);
1543 +                               finish_hard_link(file, fname, &sxp->st, itemizing, code, j);
1544                         if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
1545 -                               itemize(file, ndx, 1, stp,
1546 +#ifdef SUPPORT_ACLS
1547 +                               if (preserve_acls && !ACL_READY(*sxp))
1548 +                                       get_acl(fname, sxp);
1549 +#endif
1550 +                               itemize(file, ndx, 1, sxp,
1551                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
1552                                         0, "");
1553                         }
1554                 } else
1555  #endif
1556 -               if (itemizing)
1557 -                       itemize(file, ndx, 0, stp, 0, 0, NULL);
1558 +               if (itemizing) {
1559 +#ifdef SUPPORT_ACLS
1560 +                       if (preserve_acls && !ACL_READY(*sxp))
1561 +                               get_acl(fname, sxp);
1562 +#endif
1563 +                       itemize(file, ndx, 0, sxp, 0, 0, NULL);
1564 +               }
1565                 if (verbose > 1 && maybe_ATTRS_REPORT)
1566                         rprintf(FCLIENT, "%s is uptodate\n", fname);
1567                 return -2;
1568 @@ -885,8 +909,13 @@ static int try_dests_reg(struct file_str
1569                         }
1570                         return -1;
1571                 }
1572 -               if (itemizing)
1573 -                       itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
1574 +               if (itemizing) {
1575 +#ifdef SUPPORT_ACLS
1576 +                       if (preserve_acls && !ACL_READY(*sxp))
1577 +                               get_acl(fname, sxp);
1578 +#endif
1579 +                       itemize(file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
1580 +               }
1581                 set_file_attrs(fname, file, NULL, 0);
1582                 if (maybe_ATTRS_REPORT
1583                  && ((!itemizing && verbose && match_level == 2)
1584 @@ -897,7 +926,7 @@ static int try_dests_reg(struct file_str
1585                 }
1586  #ifdef SUPPORT_HARD_LINKS
1587                 if (preserve_hard_links && F_IS_HLINKED(file))
1588 -                       finish_hard_link(file, fname, stp, itemizing, code, -1);
1589 +                       finish_hard_link(file, fname, &sxp->st, itemizing, code, -1);
1590  #endif
1591                 return -2;
1592         }
1593 @@ -909,7 +938,7 @@ static int try_dests_reg(struct file_str
1594   * handling the file, or -1 if no dest-linking occurred, or a non-negative
1595   * value if we found an alternate basis file. */
1596  static int try_dests_non(struct file_struct *file, char *fname, int ndx,
1597 -                        char *cmpbuf, STRUCT_STAT *stp, int itemizing,
1598 +                        char *cmpbuf, statx *sxp, int itemizing,
1599                          enum logcode code)
1600  {
1601         char lnk[MAXPATHLEN];
1602 @@ -942,24 +971,24 @@ static int try_dests_non(struct file_str
1603  
1604         do {
1605                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1606 -               if (link_stat(cmpbuf, stp, 0) < 0)
1607 +               if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1608                         continue;
1609                 switch (type) {
1610                 case TYPE_DIR:
1611 -                       if (!S_ISDIR(stp->st_mode))
1612 +                       if (!S_ISDIR(sxp->st.st_mode))
1613                                 continue;
1614                         break;
1615                 case TYPE_SPECIAL:
1616 -                       if (!IS_SPECIAL(stp->st_mode))
1617 +                       if (!IS_SPECIAL(sxp->st.st_mode))
1618                                 continue;
1619                         break;
1620                 case TYPE_DEVICE:
1621 -                       if (!IS_DEVICE(stp->st_mode))
1622 +                       if (!IS_DEVICE(sxp->st.st_mode))
1623                                 continue;
1624                         break;
1625  #ifdef SUPPORT_LINKS
1626                 case TYPE_SYMLINK:
1627 -                       if (!S_ISLNK(stp->st_mode))
1628 +                       if (!S_ISLNK(sxp->st.st_mode))
1629                                 continue;
1630                         break;
1631  #endif
1632 @@ -974,7 +1003,7 @@ static int try_dests_non(struct file_str
1633                 case TYPE_SPECIAL:
1634                 case TYPE_DEVICE:
1635                         devp = F_RDEV_P(file);
1636 -                       if (stp->st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1637 +                       if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
1638                                 continue;
1639                         break;
1640  #ifdef SUPPORT_LINKS
1641 @@ -991,7 +1020,11 @@ static int try_dests_non(struct file_str
1642                         match_level = 2;
1643                         best_match = j;
1644                 }
1645 -               if (unchanged_attrs(file, stp)) {
1646 +#ifdef SUPPORT_ACLS
1647 +               if (preserve_acls)
1648 +                       get_acl(cmpbuf, sxp);
1649 +#endif
1650 +               if (unchanged_attrs(file, sxp)) {
1651                         match_level = 3;
1652                         best_match = j;
1653                         break;
1654 @@ -1004,7 +1037,7 @@ static int try_dests_non(struct file_str
1655         if (j != best_match) {
1656                 j = best_match;
1657                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1658 -               if (link_stat(cmpbuf, stp, 0) < 0)
1659 +               if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1660                         return -1;
1661         }
1662  
1663 @@ -1035,7 +1068,15 @@ static int try_dests_non(struct file_str
1664                             : ITEM_LOCAL_CHANGE
1665                              + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1666                         char *lp = match_level == 3 ? "" : NULL;
1667 -                       itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp);
1668 +#ifdef SUPPORT_ACLS
1669 +                       if (preserve_acls)
1670 +                               get_acl(fname, sxp);
1671 +#endif
1672 +                       itemize(file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
1673 +#ifdef SUPPORT_ACLS
1674 +                       if (preserve_acls)
1675 +                               free_acl(sxp);
1676 +#endif
1677                 }
1678                 if (verbose > 1 && maybe_ATTRS_REPORT) {
1679                         rprintf(FCLIENT, "%s%s is uptodate\n",
1680 @@ -1048,6 +1089,7 @@ static int try_dests_non(struct file_str
1681  }
1682  
1683  static int phase = 0;
1684 +static int dflt_perms;
1685  
1686  /* Acts on the_file_list->file's ndx'th item, whose name is fname.  If a dir,
1687   * make sure it exists, and has the right permissions/timestamp info.  For
1688 @@ -1068,7 +1110,8 @@ static void recv_generator(char *fname, 
1689         static int need_fuzzy_dirlist = 0;
1690         struct file_struct *fuzzy_file = NULL;
1691         int fd = -1, f_copy = -1;
1692 -       STRUCT_STAT st, real_st, partial_st;
1693 +       statx sx, real_sx;
1694 +       STRUCT_STAT partial_st;
1695         struct file_struct *back_file = NULL;
1696         int statret, real_ret, stat_errno;
1697         char *fnamecmp, *partialptr, *backupptr = NULL;
1698 @@ -1124,6 +1167,9 @@ static void recv_generator(char *fname, 
1699                 } else if (!dry_run)
1700                         return;
1701         }
1702 +#ifdef SUPPORT_ACLS
1703 +       sx.acc_acl = sx.def_acl = NULL;
1704 +#endif
1705         if (dry_run > 1) {
1706                 statret = -1;
1707                 stat_errno = ENOENT;
1708 @@ -1131,7 +1177,7 @@ static void recv_generator(char *fname, 
1709                 const char *dn = file->dirname ? file->dirname : ".";
1710                 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1711                         if (relative_paths && !implied_dirs
1712 -                        && do_stat(dn, &st) < 0
1713 +                        && do_stat(dn, &sx.st) < 0
1714                          && create_directory_path(fname) < 0) {
1715                                 rsyserr(FERROR, errno,
1716                                         "recv_generator: mkdir %s failed",
1717 @@ -1143,6 +1189,10 @@ static void recv_generator(char *fname, 
1718                         }
1719                         if (fuzzy_basis)
1720                                 need_fuzzy_dirlist = 1;
1721 +#ifdef SUPPORT_ACLS
1722 +                       if (!preserve_perms)
1723 +                               dflt_perms = default_perms_for_dir(dn);
1724 +#endif
1725                 }
1726                 parent_dirname = dn;
1727  
1728 @@ -1152,7 +1202,7 @@ static void recv_generator(char *fname, 
1729                         need_fuzzy_dirlist = 0;
1730                 }
1731  
1732 -               statret = link_stat(fname, &st,
1733 +               statret = link_stat(fname, &sx.st,
1734                                     keep_dirlinks && S_ISDIR(file->mode));
1735                 stat_errno = errno;
1736         }
1737 @@ -1170,8 +1220,9 @@ static void recv_generator(char *fname, 
1738          * mode based on the local permissions and some heuristics. */
1739         if (!preserve_perms) {
1740                 int exists = statret == 0
1741 -                         && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1742 -               file->mode = dest_mode(file->mode, st.st_mode, exists);
1743 +                         && S_ISDIR(sx.st.st_mode) == S_ISDIR(file->mode);
1744 +               file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1745 +                                      exists);
1746         }
1747  
1748         if (S_ISDIR(file->mode)) {
1749 @@ -1180,8 +1231,8 @@ static void recv_generator(char *fname, 
1750                  * file of that name and it is *not* a directory, then
1751                  * we need to delete it.  If it doesn't exist, then
1752                  * (perhaps recursively) create it. */
1753 -               if (statret == 0 && !S_ISDIR(st.st_mode)) {
1754 -                       if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
1755 +               if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1756 +                       if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1757                                 return;
1758                         statret = -1;
1759                 }
1760 @@ -1190,14 +1241,14 @@ static void recv_generator(char *fname, 
1761                         dry_run++;
1762                 }
1763                 real_ret = statret;
1764 -               real_st = st;
1765 +               real_sx = sx;
1766                 if (new_root_dir) {
1767                         if (*fname == '.' && fname[1] == '\0')
1768                                 statret = -1;
1769                         new_root_dir = 0;
1770                 }
1771                 if (statret != 0 && basis_dir[0] != NULL) {
1772 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1773 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1774                                               itemizing, code);
1775                         if (j == -2) {
1776                                 itemizing = 0;
1777 @@ -1206,7 +1257,11 @@ static void recv_generator(char *fname, 
1778                                 statret = 1;
1779                 }
1780                 if (itemizing && f_out != -1) {
1781 -                       itemize(file, ndx, statret, &st,
1782 +#ifdef SUPPORT_ACLS
1783 +                       if (preserve_acls && statret == 0)
1784 +                               get_acl(fname, &sx);
1785 +#endif
1786 +                       itemize(file, ndx, statret, &sx,
1787                                 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1788                 }
1789                 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1790 @@ -1226,21 +1281,21 @@ static void recv_generator(char *fname, 
1791                                 return;
1792                         }
1793                 }
1794 -               if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0)
1795 +               if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
1796                     && verbose && code != FNONE && f_out != -1)
1797                         rprintf(code, "%s/\n", fname);
1798                 if (real_ret != 0 && one_file_system)
1799 -                       real_st.st_dev = filesystem_dev;
1800 +                       real_sx.st.st_dev = filesystem_dev;
1801                 if (delete_during && f_out != -1 && !phase && dry_run < 2
1802                     && (file->flags & FLAG_XFER_DIR))
1803 -                       delete_in_dir(the_file_list, fname, file, &real_st);
1804 -               return;
1805 +                       delete_in_dir(the_file_list, fname, file, &real_sx.st);
1806 +               goto cleanup;
1807         }
1808  
1809  #ifdef SUPPORT_HARD_LINKS
1810         if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1811 -        && hard_link_check(file, ndx, fname, statret, &st, itemizing, code))
1812 -               return;
1813 +        && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1814 +               goto cleanup;
1815  #endif
1816  
1817         if (preserve_links && S_ISLNK(file->mode)) {
1818 @@ -1260,17 +1315,17 @@ static void recv_generator(char *fname, 
1819                         char lnk[MAXPATHLEN];
1820                         int len;
1821  
1822 -                       if (!S_ISLNK(st.st_mode))
1823 +                       if (!S_ISLNK(sx.st.st_mode))
1824                                 statret = -1;
1825                         else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1826                               && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1827                                 /* The link is pointing to the right place. */
1828                                 if (itemizing)
1829 -                                       itemize(file, ndx, 0, &st, 0, 0, NULL);
1830 -                               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1831 +                                       itemize(file, ndx, 0, &sx, 0, 0, NULL);
1832 +                               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1833  #ifdef SUPPORT_HARD_LINKS
1834                                 if (preserve_hard_links && F_IS_HLINKED(file))
1835 -                                       finish_hard_link(file, fname, &st, itemizing, code, -1);
1836 +                                       finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1837  #endif
1838                                 if (remove_source_files == 1)
1839                                         goto return_with_success;
1840 @@ -1278,10 +1333,10 @@ static void recv_generator(char *fname, 
1841                         }
1842                         /* Not the right symlink (or not a symlink), so
1843                          * delete it. */
1844 -                       if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
1845 +                       if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1846                                 return;
1847                 } else if (basis_dir[0] != NULL) {
1848 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1849 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1850                                               itemizing, code);
1851                         if (j == -2) {
1852  #ifndef CAN_HARDLINK_SYMLINK
1853 @@ -1306,7 +1361,7 @@ static void recv_generator(char *fname, 
1854                 } else {
1855                         set_file_attrs(fname, file, NULL, 0);
1856                         if (itemizing) {
1857 -                               itemize(file, ndx, statret, &st,
1858 +                               itemize(file, ndx, statret, &sx,
1859                                         ITEM_LOCAL_CHANGE, 0, NULL);
1860                         }
1861                         if (code != FNONE && verbose)
1862 @@ -1332,33 +1387,38 @@ static void recv_generator(char *fname, 
1863                 if (statret == 0) {
1864                         char *t;
1865                         if (IS_DEVICE(file->mode)) {
1866 -                               if (!IS_DEVICE(st.st_mode))
1867 +                               if (!IS_DEVICE(sx.st.st_mode))
1868                                         statret = -1;
1869                                 t = "device file";
1870                         } else {
1871 -                               if (!IS_SPECIAL(st.st_mode))
1872 +                               if (!IS_SPECIAL(sx.st.st_mode))
1873                                         statret = -1;
1874                                 t = "special file";
1875                         }
1876                         if (statret == 0
1877 -                        && BITS_EQUAL(st.st_mode, file->mode, _S_IFMT)
1878 -                        && st.st_rdev == rdev) {
1879 +                        && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1880 +                        && sx.st.st_rdev == rdev) {
1881                                 /* The device or special file is identical. */
1882 -                               if (itemizing)
1883 -                                       itemize(file, ndx, 0, &st, 0, 0, NULL);
1884 -                               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1885 +                               if (itemizing) {
1886 +#ifdef SUPPORT_ACLS
1887 +                                       if (preserve_acls)
1888 +                                               get_acl(fname, &sx);
1889 +#endif
1890 +                                       itemize(file, ndx, 0, &sx, 0, 0, NULL);
1891 +                               }
1892 +                               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
1893  #ifdef SUPPORT_HARD_LINKS
1894                                 if (preserve_hard_links && F_IS_HLINKED(file))
1895 -                                       finish_hard_link(file, fname, &st, itemizing, code, -1);
1896 +                                       finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1897  #endif
1898                                 if (remove_source_files == 1)
1899                                         goto return_with_success;
1900 -                               return;
1901 +                               goto cleanup;
1902                         }
1903 -                       if (delete_item(fname, st.st_mode, t, del_opts) != 0)
1904 +                       if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1905                                 return;
1906                 } else if (basis_dir[0] != NULL) {
1907 -                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1908 +                       int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
1909                                               itemizing, code);
1910                         if (j == -2) {
1911  #ifndef CAN_HARDLINK_SPECIAL
1912 @@ -1388,7 +1448,11 @@ static void recv_generator(char *fname, 
1913                 } else {
1914                         set_file_attrs(fname, file, NULL, 0);
1915                         if (itemizing) {
1916 -                               itemize(file, ndx, statret, &st,
1917 +#ifdef SUPPORT_ACLS
1918 +                               if (preserve_acls && statret == 0)
1919 +                                       get_acl(fname, &sx);
1920 +#endif
1921 +                               itemize(file, ndx, statret, &sx,
1922                                         ITEM_LOCAL_CHANGE, 0, NULL);
1923                         }
1924                         if (code != FNONE && verbose)
1925 @@ -1400,7 +1464,7 @@ static void recv_generator(char *fname, 
1926                         if (remove_source_files == 1)
1927                                 goto return_with_success;
1928                 }
1929 -               return;
1930 +               goto cleanup;
1931         }
1932  
1933         if (!S_ISREG(file->mode)) {
1934 @@ -1434,7 +1498,7 @@ static void recv_generator(char *fname, 
1935         }
1936  
1937         if (update_only && statret == 0
1938 -           && cmp_time(st.st_mtime, file->modtime) > 0) {
1939 +           && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
1940                 if (verbose > 1)
1941                         rprintf(FINFO, "%s is newer\n", fname);
1942                 return;
1943 @@ -1443,20 +1507,20 @@ static void recv_generator(char *fname, 
1944         fnamecmp = fname;
1945         fnamecmp_type = FNAMECMP_FNAME;
1946  
1947 -       if (statret == 0 && !S_ISREG(st.st_mode)) {
1948 -               if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
1949 +       if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1950 +               if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1951                         return;
1952                 statret = -1;
1953                 stat_errno = ENOENT;
1954         }
1955  
1956         if (statret != 0 && basis_dir[0] != NULL) {
1957 -               int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
1958 +               int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
1959                                       itemizing, code);
1960                 if (j == -2) {
1961                         if (remove_source_files == 1)
1962                                 goto return_with_success;
1963 -                       return;
1964 +                       goto cleanup;
1965                 }
1966                 if (j >= 0) {
1967                         fnamecmp = fnamecmpbuf;
1968 @@ -1466,7 +1530,7 @@ static void recv_generator(char *fname, 
1969         }
1970  
1971         real_ret = statret;
1972 -       real_st = st;
1973 +       real_sx = sx;
1974  
1975         if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1976             && link_stat(partialptr, &partial_st, 0) == 0
1977 @@ -1485,7 +1549,7 @@ static void recv_generator(char *fname, 
1978                                 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1979                                         fname, fnamecmpbuf);
1980                         }
1981 -                       st.st_size = F_LENGTH(fuzzy_file);
1982 +                       sx.st.st_size = F_LENGTH(fuzzy_file);
1983                         statret = 0;
1984                         fnamecmp = fnamecmpbuf;
1985                         fnamecmp_type = FNAMECMP_FUZZY;
1986 @@ -1504,36 +1568,41 @@ static void recv_generator(char *fname, 
1987                 return;
1988         }
1989  
1990 -       if (append_mode && st.st_size > F_LENGTH(file))
1991 +       if (append_mode && sx.st.st_size > F_LENGTH(file))
1992                 return;
1993  
1994         if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1995                 ;
1996         else if (fnamecmp_type == FNAMECMP_FUZZY)
1997                 ;
1998 -       else if (unchanged_file(fnamecmp, file, &st)) {
1999 +       else if (unchanged_file(fnamecmp, file, &sx.st)) {
2000                 if (partialptr) {
2001                         do_unlink(partialptr);
2002                         handle_partial_dir(partialptr, PDIR_DELETE);
2003                 }
2004 -               if (itemizing)
2005 -                       itemize(file, ndx, statret, &st, 0, 0, NULL);
2006 -               set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
2007 +               if (itemizing) {
2008 +#ifdef SUPPORT_ACLS
2009 +                       if (preserve_acls && statret == 0)
2010 +                               get_acl(fnamecmp, &sx);
2011 +#endif
2012 +                       itemize(file, ndx, statret, &sx, 0, 0, NULL);
2013 +               }
2014 +               set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
2015  #ifdef SUPPORT_HARD_LINKS
2016                 if (preserve_hard_links && F_IS_HLINKED(file))
2017 -                       finish_hard_link(file, fname, &st, itemizing, code, -1);
2018 +                       finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
2019  #endif
2020                 if (remove_source_files != 1)
2021 -                       return;
2022 +                       goto cleanup;
2023           return_with_success:
2024                 if (!dry_run)
2025                         send_msg_int(MSG_SUCCESS, ndx);
2026 -               return;
2027 +               goto cleanup;
2028         }
2029  
2030    prepare_to_open:
2031         if (partialptr) {
2032 -               st = partial_st;
2033 +               sx.st = partial_st;
2034                 fnamecmp = partialptr;
2035                 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
2036                 statret = 0;
2037 @@ -1558,16 +1627,20 @@ static void recv_generator(char *fname, 
2038                 /* pretend the file didn't exist */
2039  #ifdef SUPPORT_HARD_LINKS
2040                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
2041 -                       return;
2042 +                       goto cleanup;
2043  #endif
2044                 statret = real_ret = -1;
2045 +#ifdef SUPPORT_ACLS
2046 +               if (preserve_acls && ACL_READY(sx))
2047 +                       free_acl(&sx);
2048 +#endif
2049                 goto notify_others;
2050         }
2051  
2052         if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
2053                 if (!(backupptr = get_backup_name(fname))) {
2054                         close(fd);
2055 -                       return;
2056 +                       goto cleanup;
2057                 }
2058                 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
2059                         close(fd);
2060 @@ -1578,7 +1651,7 @@ static void recv_generator(char *fname, 
2061                                 full_fname(backupptr));
2062                         unmake_file(back_file);
2063                         close(fd);
2064 -                       return;
2065 +                       goto cleanup;
2066                 }
2067                 if ((f_copy = do_open(backupptr,
2068                     O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
2069 @@ -1586,14 +1659,14 @@ static void recv_generator(char *fname, 
2070                                 full_fname(backupptr));
2071                         unmake_file(back_file);
2072                         close(fd);
2073 -                       return;
2074 +                       goto cleanup;
2075                 }
2076                 fnamecmp_type = FNAMECMP_BACKUP;
2077         }
2078  
2079         if (verbose > 3) {
2080                 rprintf(FINFO, "gen mapped %s of size %.0f\n",
2081 -                       fnamecmp, (double)st.st_size);
2082 +                       fnamecmp, (double)sx.st.st_size);
2083         }
2084  
2085         if (verbose > 2)
2086 @@ -1615,26 +1688,34 @@ static void recv_generator(char *fname, 
2087                         iflags |= ITEM_BASIS_TYPE_FOLLOWS;
2088                 if (fnamecmp_type == FNAMECMP_FUZZY)
2089                         iflags |= ITEM_XNAME_FOLLOWS;
2090 -               itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
2091 +#ifdef SUPPORT_ACLS
2092 +               if (preserve_acls && real_ret == 0)
2093 +                       get_acl(fnamecmp, &real_sx);
2094 +#endif
2095 +               itemize(file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
2096                         fuzzy_file ? fuzzy_file->basename : NULL);
2097 +#ifdef SUPPORT_ACLS
2098 +               if (preserve_acls)
2099 +                       free_acl(&real_sx);
2100 +#endif
2101         }
2102  
2103         if (!do_xfers) {
2104  #ifdef SUPPORT_HARD_LINKS
2105                 if (preserve_hard_links && F_IS_HLINKED(file))
2106 -                       finish_hard_link(file, fname, &st, itemizing, code, -1);
2107 +                       finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
2108  #endif
2109 -               return;
2110 +               goto cleanup;
2111         }
2112         if (read_batch)
2113 -               return;
2114 +               goto cleanup;
2115  
2116         if (statret != 0 || whole_file) {
2117                 write_sum_head(f_out, NULL);
2118 -               return;
2119 +               goto cleanup;
2120         }
2121  
2122 -       generate_and_send_sums(fd, st.st_size, f_out, f_copy);
2123 +       generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
2124  
2125         if (f_copy >= 0) {
2126                 close(f_copy);
2127 @@ -1647,6 +1728,13 @@ static void recv_generator(char *fname, 
2128         }
2129  
2130         close(fd);
2131 +
2132 +  cleanup:
2133 +#ifdef SUPPORT_ACLS
2134 +       if (preserve_acls)
2135 +               free_acl(&sx);
2136 +#endif
2137 +       return;
2138  }
2139  
2140  void generate_files(int f_out, struct file_list *flist, char *local_name)
2141 @@ -1713,6 +1801,8 @@ void generate_files(int f_out, struct fi
2142          * notice that and let us know via the redo pipe (or its closing). */
2143         ignore_timeout = 1;
2144  
2145 +       dflt_perms = (ACCESSPERMS & ~orig_umask);
2146 +
2147         for (i = 0; i < flist->count; i++) {
2148                 struct file_struct *file = flist->files[i];
2149  
2150 --- old/hlink.c
2151 +++ new/hlink.c
2152 @@ -27,6 +27,7 @@ extern int verbose;
2153  extern int dry_run;
2154  extern int do_xfers;
2155  extern int link_dest;
2156 +extern int preserve_acls;
2157  extern int make_backups;
2158  extern int protocol_version;
2159  extern int remove_source_files;
2160 @@ -269,15 +270,19 @@ void match_hard_links(void)
2161  }
2162  
2163  static int maybe_hard_link(struct file_struct *file, int ndx,
2164 -                          const char *fname, int statret, STRUCT_STAT *stp,
2165 +                          const char *fname, int statret, statx *sxp,
2166                            const char *oldname, STRUCT_STAT *old_stp,
2167                            const char *realname, int itemizing, enum logcode code)
2168  {
2169         if (statret == 0) {
2170 -               if (stp->st_dev == old_stp->st_dev
2171 -                && stp->st_ino == old_stp->st_ino) {
2172 +               if (sxp->st.st_dev == old_stp->st_dev
2173 +                && sxp->st.st_ino == old_stp->st_ino) {
2174                         if (itemizing) {
2175 -                               itemize(file, ndx, statret, stp,
2176 +#ifdef SUPPORT_ACLS
2177 +                               if (preserve_acls && !ACL_READY(*sxp))
2178 +                                       get_acl(fname, sxp);
2179 +#endif
2180 +                               itemize(file, ndx, statret, sxp,
2181                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
2182                                         0, "");
2183                         }
2184 @@ -298,7 +303,11 @@ static int maybe_hard_link(struct file_s
2185  
2186         if (hard_link_one(file, fname, oldname, 0)) {
2187                 if (itemizing) {
2188 -                       itemize(file, ndx, statret, stp,
2189 +#ifdef SUPPORT_ACLS
2190 +                       if (preserve_acls && statret == 0 && !ACL_READY(*sxp))
2191 +                               get_acl(fname, sxp);
2192 +#endif
2193 +                       itemize(file, ndx, statret, sxp,
2194                                 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
2195                                 realname);
2196                 }
2197 @@ -312,7 +321,7 @@ static int maybe_hard_link(struct file_s
2198  /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not.  Returns:
2199   * 0 = process the file, 1 = skip the file, -1 = error occurred. */
2200  int hard_link_check(struct file_struct *file, int ndx, const char *fname,
2201 -                   int statret, STRUCT_STAT *stp, int itemizing,
2202 +                   int statret, statx *sxp, int itemizing,
2203                     enum logcode code)
2204  {
2205         STRUCT_STAT prev_st;
2206 @@ -363,18 +372,20 @@ int hard_link_check(struct file_struct *
2207         if (statret < 0 && basis_dir[0] != NULL) {
2208                 /* If we match an alt-dest item, we don't output this as a change. */
2209                 char cmpbuf[MAXPATHLEN];
2210 -               STRUCT_STAT alt_st;
2211 +               statx alt_sx;
2212                 int j = 0;
2213 +#ifdef SUPPORT_ACLS
2214 +               alt_sx.acc_acl = alt_sx.def_acl = NULL;
2215 +#endif
2216                 do {
2217                         pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
2218 -                       if (link_stat(cmpbuf, &alt_st, 0) < 0)
2219 +                       if (link_stat(cmpbuf, &alt_sx.st, 0) < 0)
2220                                 continue;
2221                         if (link_dest) {
2222 -                               if (prev_st.st_dev != alt_st.st_dev
2223 -                                || prev_st.st_ino != alt_st.st_ino)
2224 +                               if (prev_st.st_dev != alt_sx.st.st_dev
2225 +                                || prev_st.st_ino != alt_sx.st.st_ino)
2226                                         continue;
2227                                 statret = 1;
2228 -                               *stp = alt_st;
2229                                 if (verbose < 2 || !stdout_format_has_i) {
2230                                         itemizing = 0;
2231                                         code = FNONE;
2232 @@ -383,16 +394,36 @@ int hard_link_check(struct file_struct *
2233                                 }
2234                                 break;
2235                         }
2236 -                       if (!unchanged_file(cmpbuf, file, &alt_st))
2237 +                       if (!unchanged_file(cmpbuf, file, &alt_sx.st))
2238                                 continue;
2239                         statret = 1;
2240 -                       *stp = alt_st;
2241 -                       if (unchanged_attrs(file, &alt_st))
2242 +#ifdef SUPPORT_ACLS
2243 +                       if (preserve_acls)
2244 +                               get_acl(cmpbuf, &alt_sx);
2245 +#endif
2246 +                       if (unchanged_attrs(file, &alt_sx))
2247                                 break;
2248                 } while (basis_dir[++j] != NULL);
2249 +               if (statret == 1) {
2250 +                       sxp->st = alt_sx.st;
2251 +#ifdef SUPPORT_ACLS
2252 +                       if (preserve_acls) {
2253 +                               if (!ACL_READY(*sxp))
2254 +                                       get_acl(cmpbuf, sxp);
2255 +                               else {
2256 +                                       sxp->acc_acl = alt_sx.acc_acl;
2257 +                                       sxp->def_acl = alt_sx.def_acl;
2258 +                               }
2259 +                       }
2260 +#endif
2261 +               }
2262 +#ifdef SUPPORT_ACLS
2263 +               else if (preserve_acls)
2264 +                       free_acl(&alt_sx);
2265 +#endif
2266         }
2267  
2268 -       if (maybe_hard_link(file, ndx, fname, statret, stp, prev_name, &prev_st,
2269 +       if (maybe_hard_link(file, ndx, fname, statret, sxp, prev_name, &prev_st,
2270                             realname, itemizing, code) < 0)
2271                 return -1;
2272  
2273 @@ -427,7 +458,8 @@ void finish_hard_link(struct file_struct
2274                       STRUCT_STAT *stp, int itemizing, enum logcode code,
2275                       int alt_dest)
2276  {
2277 -       STRUCT_STAT st, prev_st;
2278 +       statx prev_sx;
2279 +       STRUCT_STAT st;
2280         char alt_name[MAXPATHLEN], *prev_name;
2281         const char *our_name;
2282         int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
2283 @@ -451,14 +483,24 @@ void finish_hard_link(struct file_struct
2284         } else
2285                 our_name = fname;
2286  
2287 +#ifdef SUPPORT_ACLS
2288 +       prev_sx.acc_acl = prev_sx.def_acl = NULL;
2289 +#endif
2290 +
2291         while ((ndx = prev_ndx) >= 0) {
2292 +               int val;
2293                 file = FPTR(ndx);
2294                 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
2295                 prev_ndx = F_HL_PREV(file);
2296                 prev_name = f_name(file, NULL);
2297 -               prev_statret = link_stat(prev_name, &prev_st, 0);
2298 -               if (maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_st,
2299 -                                   our_name, stp, fname, itemizing, code) < 0)
2300 +               prev_statret = link_stat(prev_name, &prev_sx.st, 0);
2301 +               val = maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_sx,
2302 +                                     our_name, stp, fname, itemizing, code);
2303 +#ifdef SUPPORT_ACLS
2304 +               if (preserve_acls)
2305 +                       free_acl(&prev_sx);
2306 +#endif
2307 +               if (val < 0)
2308                         continue;
2309                 if (remove_source_files == 1 && do_xfers)
2310                         send_msg_int(MSG_SUCCESS, ndx);
2311 --- old/lib/sysacls.c
2312 +++ new/lib/sysacls.c
2313 @@ -0,0 +1,3251 @@
2314 +/* 
2315 +   Unix SMB/CIFS implementation.
2316 +   Samba system utilities for ACL support.
2317 +   Copyright (C) Jeremy Allison 2000.
2318 +   
2319 +   This program is free software; you can redistribute it and/or modify
2320 +   it under the terms of the GNU General Public License as published by
2321 +   the Free Software Foundation; either version 2 of the License, or
2322 +   (at your option) any later version.
2323 +   
2324 +   This program is distributed in the hope that it will be useful,
2325 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
2326 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2327 +   GNU General Public License for more details.
2328 +   
2329 +   You should have received a copy of the GNU General Public License
2330 +   along with this program; if not, write to the Free Software
2331 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2332 +*/
2333 +
2334 +#include "rsync.h"
2335 +#include "sysacls.h" /****** ADDED ******/
2336 +
2337 +#ifdef SUPPORT_ACLS
2338 +
2339 +/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
2340 +#ifdef DEBUG
2341 +#undef DEBUG
2342 +#endif
2343 +#define DEBUG(x,y)
2344 +
2345 +void SAFE_FREE(void *mem)
2346 +{
2347 +       if (mem)
2348 +               free(mem);
2349 +}
2350 +
2351 +char *uidtoname(uid_t uid)
2352 +{
2353 +       static char idbuf[12];
2354 +       struct passwd *pw;
2355 +
2356 +       if ((pw = getpwuid(uid)) == NULL) {
2357 +               slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
2358 +               return idbuf;
2359 +       }
2360 +       return pw->pw_name;
2361 +}
2362 +/****** EXTRAS -- END ******/
2363 +
2364 +/*
2365 + This file wraps all differing system ACL interfaces into a consistent
2366 + one based on the POSIX interface. It also returns the correct errors
2367 + for older UNIX systems that don't support ACLs.
2368 +
2369 + The interfaces that each ACL implementation must support are as follows :
2370 +
2371 + int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2372 + int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2373 + int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
2374 + void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2375 + SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2376 + SMB_ACL_T sys_acl_get_fd(int fd)
2377 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
2378 + int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
2379 + char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
2380 + SMB_ACL_T sys_acl_init( int count)
2381 + int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2382 + int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2383 + int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2384 + int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2385 + int sys_acl_valid( SMB_ACL_T theacl )
2386 + int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2387 + int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2388 + int sys_acl_delete_def_file(const char *path)
2389 +
2390 + This next one is not POSIX complient - but we *have* to have it !
2391 + More POSIX braindamage.
2392 +
2393 + int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2394 +
2395 + The generic POSIX free is the following call. We split this into
2396 + several different free functions as we may need to add tag info
2397 + to structures when emulating the POSIX interface.
2398 +
2399 + int sys_acl_free( void *obj_p)
2400 +
2401 + The calls we actually use are :
2402 +
2403 + int sys_acl_free_text(char *text) - free acl_to_text
2404 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
2405 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
2406 +
2407 +*/
2408 +
2409 +#if defined(HAVE_POSIX_ACLS)
2410 +
2411 +/* Identity mapping - easy. */
2412 +
2413 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2414 +{
2415 +       return acl_get_entry( the_acl, entry_id, entry_p);
2416 +}
2417 +
2418 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2419 +{
2420 +       return acl_get_tag_type( entry_d, tag_type_p);
2421 +}
2422 +
2423 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2424 +{
2425 +       return acl_get_permset( entry_d, permset_p);
2426 +}
2427 +
2428 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2429 +{
2430 +       return acl_get_qualifier( entry_d);
2431 +}
2432 +
2433 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2434 +{
2435 +       return acl_get_file( path_p, type);
2436 +}
2437 +
2438 +SMB_ACL_T sys_acl_get_fd(int fd)
2439 +{
2440 +       return acl_get_fd(fd);
2441 +}
2442 +
2443 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
2444 +{
2445 +       return acl_clear_perms(permset);
2446 +}
2447 +
2448 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2449 +{
2450 +       return acl_add_perm(permset, perm);
2451 +}
2452 +
2453 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2454 +{
2455 +#if defined(HAVE_ACL_GET_PERM_NP)
2456 +       /*
2457 +        * Required for TrustedBSD-based ACL implementations where
2458 +        * non-POSIX.1e functions are denoted by a _np (non-portable)
2459 +        * suffix.
2460 +        */
2461 +       return acl_get_perm_np(permset, perm);
2462 +#else
2463 +       return acl_get_perm(permset, perm);
2464 +#endif
2465 +}
2466 +
2467 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
2468 +{
2469 +       return acl_to_text( the_acl, plen);
2470 +}
2471 +
2472 +SMB_ACL_T sys_acl_init( int count)
2473 +{
2474 +       return acl_init(count);
2475 +}
2476 +
2477 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2478 +{
2479 +       return acl_create_entry(pacl, pentry);
2480 +}
2481 +
2482 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2483 +{
2484 +       return acl_set_tag_type(entry, tagtype);
2485 +}
2486 +
2487 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2488 +{
2489 +       return acl_set_qualifier(entry, qual);
2490 +}
2491 +
2492 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2493 +{
2494 +       return acl_set_permset(entry, permset);
2495 +}
2496 +
2497 +int sys_acl_valid( SMB_ACL_T theacl )
2498 +{
2499 +       return acl_valid(theacl);
2500 +}
2501 +
2502 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2503 +{
2504 +       return acl_set_file(name, acltype, theacl);
2505 +}
2506 +
2507 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2508 +{
2509 +       return acl_set_fd(fd, theacl);
2510 +}
2511 +
2512 +int sys_acl_delete_def_file(const char *name)
2513 +{
2514 +       return acl_delete_def_file(name);
2515 +}
2516 +
2517 +int sys_acl_free_text(char *text)
2518 +{
2519 +       return acl_free(text);
2520 +}
2521 +
2522 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
2523 +{
2524 +       return acl_free(the_acl);
2525 +}
2526 +
2527 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
2528 +{
2529 +       return acl_free(qual);
2530 +}
2531 +
2532 +#elif defined(HAVE_TRU64_ACLS)
2533 +/*
2534 + * The interface to DEC/Compaq Tru64 UNIX ACLs
2535 + * is based on Draft 13 of the POSIX spec which is
2536 + * slightly different from the Draft 16 interface.
2537 + * 
2538 + * Also, some of the permset manipulation functions
2539 + * such as acl_clear_perm() and acl_add_perm() appear
2540 + * to be broken on Tru64 so we have to manipulate
2541 + * the permission bits in the permset directly.
2542 + */
2543 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2544 +{
2545 +       SMB_ACL_ENTRY_T entry;
2546 +
2547 +       if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
2548 +               return -1;
2549 +       }
2550 +
2551 +       errno = 0;
2552 +       if ((entry = acl_get_entry(the_acl)) != NULL) {
2553 +               *entry_p = entry;
2554 +               return 1;
2555 +       }
2556 +
2557 +       return errno ? -1 : 0;
2558 +}
2559 +
2560 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2561 +{
2562 +       return acl_get_tag_type( entry_d, tag_type_p);
2563 +}
2564 +
2565 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2566 +{
2567 +       return acl_get_permset( entry_d, permset_p);
2568 +}
2569 +
2570 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
2571 +{
2572 +       return acl_get_qualifier( entry_d);
2573 +}
2574 +
2575 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
2576 +{
2577 +       return acl_get_file((char *)path_p, type);
2578 +}
2579 +
2580 +SMB_ACL_T sys_acl_get_fd(int fd)
2581 +{
2582 +       return acl_get_fd(fd, ACL_TYPE_ACCESS);
2583 +}
2584 +
2585 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
2586 +{
2587 +       *permset = 0;           /* acl_clear_perm() is broken on Tru64  */
2588 +
2589 +       return 0;
2590 +}
2591 +
2592 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2593 +{
2594 +       if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
2595 +               errno = EINVAL;
2596 +               return -1;
2597 +       }
2598 +
2599 +       *permset |= perm;       /* acl_add_perm() is broken on Tru64    */
2600 +
2601 +       return 0;
2602 +}
2603 +
2604 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2605 +{
2606 +       return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
2607 +}
2608 +
2609 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
2610 +{
2611 +       return acl_to_text( the_acl, plen);
2612 +}
2613 +
2614 +SMB_ACL_T sys_acl_init( int count)
2615 +{
2616 +       return acl_init(count);
2617 +}
2618 +
2619 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2620 +{
2621 +       SMB_ACL_ENTRY_T entry;
2622 +
2623 +       if ((entry = acl_create_entry(pacl)) == NULL) {
2624 +               return -1;
2625 +       }
2626 +
2627 +       *pentry = entry;
2628 +       return 0;
2629 +}
2630 +
2631 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2632 +{
2633 +       return acl_set_tag_type(entry, tagtype);
2634 +}
2635 +
2636 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
2637 +{
2638 +       return acl_set_qualifier(entry, qual);
2639 +}
2640 +
2641 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2642 +{
2643 +       return acl_set_permset(entry, permset);
2644 +}
2645 +
2646 +int sys_acl_valid( SMB_ACL_T theacl )
2647 +{
2648 +       acl_entry_t     entry;
2649 +
2650 +       return acl_valid(theacl, &entry);
2651 +}
2652 +
2653 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2654 +{
2655 +       return acl_set_file((char *)name, acltype, theacl);
2656 +}
2657 +
2658 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
2659 +{
2660 +       return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
2661 +}
2662 +
2663 +int sys_acl_delete_def_file(const char *name)
2664 +{
2665 +       return acl_delete_def_file((char *)name);
2666 +}
2667 +
2668 +int sys_acl_free_text(char *text)
2669 +{
2670 +       /*
2671 +        * (void) cast and explicit return 0 are for DEC UNIX
2672 +        *  which just #defines acl_free_text() to be free()
2673 +        */
2674 +       (void) acl_free_text(text);
2675 +       return 0;
2676 +}
2677 +
2678 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
2679 +{
2680 +       return acl_free(the_acl);
2681 +}
2682 +
2683 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2684 +{
2685 +       return acl_free_qualifier(qual, tagtype);
2686 +}
2687 +
2688 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
2689 +
2690 +/*
2691 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
2692 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
2693 + */
2694 +
2695 +/*
2696 + * Note that while this code implements sufficient functionality
2697 + * to support the sys_acl_* interfaces it does not provide all
2698 + * of the semantics of the POSIX ACL interfaces.
2699 + *
2700 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2701 + * from a call to sys_acl_get_entry() should not be assumed to be
2702 + * valid after calling any of the following functions, which may
2703 + * reorder the entries in the ACL.
2704 + *
2705 + *     sys_acl_valid()
2706 + *     sys_acl_set_file()
2707 + *     sys_acl_set_fd()
2708 + */
2709 +
2710 +/*
2711 + * The only difference between Solaris and UnixWare / OpenUNIX is
2712 + * that the #defines for the ACL operations have different names
2713 + */
2714 +#if defined(HAVE_UNIXWARE_ACLS)
2715 +
2716 +#define        SETACL          ACL_SET
2717 +#define        GETACL          ACL_GET
2718 +#define        GETACLCNT       ACL_CNT
2719 +
2720 +#endif
2721 +
2722 +
2723 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2724 +{
2725 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2726 +               errno = EINVAL;
2727 +               return -1;
2728 +       }
2729 +
2730 +       if (entry_p == NULL) {
2731 +               errno = EINVAL;
2732 +               return -1;
2733 +       }
2734 +
2735 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
2736 +               acl_d->next = 0;
2737 +       }
2738 +
2739 +       if (acl_d->next < 0) {
2740 +               errno = EINVAL;
2741 +               return -1;
2742 +       }
2743 +
2744 +       if (acl_d->next >= acl_d->count) {
2745 +               return 0;
2746 +       }
2747 +
2748 +       *entry_p = &acl_d->acl[acl_d->next++];
2749 +
2750 +       return 1;
2751 +}
2752 +
2753 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2754 +{
2755 +       *type_p = entry_d->a_type;
2756 +
2757 +       return 0;
2758 +}
2759 +
2760 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2761 +{
2762 +       *permset_p = &entry_d->a_perm;
2763 +
2764 +       return 0;
2765 +}
2766 +
2767 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2768 +{
2769 +       if (entry_d->a_type != SMB_ACL_USER
2770 +           && entry_d->a_type != SMB_ACL_GROUP) {
2771 +               errno = EINVAL;
2772 +               return NULL;
2773 +       }
2774 +
2775 +       return &entry_d->a_id;
2776 +}
2777 +
2778 +/*
2779 + * There is no way of knowing what size the ACL returned by
2780 + * GETACL will be unless you first call GETACLCNT which means
2781 + * making an additional system call.
2782 + *
2783 + * In the hope of avoiding the cost of the additional system
2784 + * call in most cases, we initially allocate enough space for
2785 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2786 + * be too small then we use GETACLCNT to find out the actual
2787 + * size, reallocate the ACL buffer, and then call GETACL again.
2788 + */
2789 +
2790 +#define        INITIAL_ACL_SIZE        16
2791 +
2792 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2793 +{
2794 +       SMB_ACL_T       acl_d;
2795 +       int             count;          /* # of ACL entries allocated   */
2796 +       int             naccess;        /* # of access ACL entries      */
2797 +       int             ndefault;       /* # of default ACL entries     */
2798 +
2799 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2800 +               errno = EINVAL;
2801 +               return NULL;
2802 +       }
2803 +
2804 +       count = INITIAL_ACL_SIZE;
2805 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2806 +               return NULL;
2807 +       }
2808 +
2809 +       /*
2810 +        * If there isn't enough space for the ACL entries we use
2811 +        * GETACLCNT to determine the actual number of ACL entries
2812 +        * reallocate and try again. This is in a loop because it
2813 +        * is possible that someone else could modify the ACL and
2814 +        * increase the number of entries between the call to
2815 +        * GETACLCNT and the call to GETACL.
2816 +        */
2817 +       while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
2818 +           && errno == ENOSPC) {
2819 +
2820 +               sys_acl_free_acl(acl_d);
2821 +
2822 +               if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
2823 +                       return NULL;
2824 +               }
2825 +
2826 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2827 +                       return NULL;
2828 +               }
2829 +       }
2830 +
2831 +       if (count < 0) {
2832 +               sys_acl_free_acl(acl_d);
2833 +               return NULL;
2834 +       }
2835 +
2836 +       /*
2837 +        * calculate the number of access and default ACL entries
2838 +        *
2839 +        * Note: we assume that the acl() system call returned a
2840 +        * well formed ACL which is sorted so that all of the
2841 +        * access ACL entries preceed any default ACL entries
2842 +        */
2843 +       for (naccess = 0; naccess < count; naccess++) {
2844 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2845 +                       break;
2846 +       }
2847 +       ndefault = count - naccess;
2848 +       
2849 +       /*
2850 +        * if the caller wants the default ACL we have to copy
2851 +        * the entries down to the start of the acl[] buffer
2852 +        * and mask out the ACL_DEFAULT flag from the type field
2853 +        */
2854 +       if (type == SMB_ACL_TYPE_DEFAULT) {
2855 +               int     i, j;
2856 +
2857 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
2858 +                       acl_d->acl[i] = acl_d->acl[j];
2859 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2860 +               }
2861 +
2862 +               acl_d->count = ndefault;
2863 +       } else {
2864 +               acl_d->count = naccess;
2865 +       }
2866 +
2867 +       return acl_d;
2868 +}
2869 +
2870 +SMB_ACL_T sys_acl_get_fd(int fd)
2871 +{
2872 +       SMB_ACL_T       acl_d;
2873 +       int             count;          /* # of ACL entries allocated   */
2874 +       int             naccess;        /* # of access ACL entries      */
2875 +
2876 +       count = INITIAL_ACL_SIZE;
2877 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2878 +               return NULL;
2879 +       }
2880 +
2881 +       while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2882 +           && errno == ENOSPC) {
2883 +
2884 +               sys_acl_free_acl(acl_d);
2885 +
2886 +               if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2887 +                       return NULL;
2888 +               }
2889 +
2890 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2891 +                       return NULL;
2892 +               }
2893 +       }
2894 +
2895 +       if (count < 0) {
2896 +               sys_acl_free_acl(acl_d);
2897 +               return NULL;
2898 +       }
2899 +
2900 +       /*
2901 +        * calculate the number of access ACL entries
2902 +        */
2903 +       for (naccess = 0; naccess < count; naccess++) {
2904 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2905 +                       break;
2906 +       }
2907 +       
2908 +       acl_d->count = naccess;
2909 +
2910 +       return acl_d;
2911 +}
2912 +
2913 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2914 +{
2915 +       *permset_d = 0;
2916 +
2917 +       return 0;
2918 +}
2919 +
2920 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2921 +{
2922 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2923 +           && perm != SMB_ACL_EXECUTE) {
2924 +               errno = EINVAL;
2925 +               return -1;
2926 +       }
2927 +
2928 +       if (permset_d == NULL) {
2929 +               errno = EINVAL;
2930 +               return -1;
2931 +       }
2932 +
2933 +       *permset_d |= perm;
2934 +
2935 +       return 0;
2936 +}
2937 +
2938 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2939 +{
2940 +       return *permset_d & perm;
2941 +}
2942 +
2943 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2944 +{
2945 +       int     i;
2946 +       int     len, maxlen;
2947 +       char    *text;
2948 +
2949 +       /*
2950 +        * use an initial estimate of 20 bytes per ACL entry
2951 +        * when allocating memory for the text representation
2952 +        * of the ACL
2953 +        */
2954 +       len     = 0;
2955 +       maxlen  = 20 * acl_d->count;
2956 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
2957 +               errno = ENOMEM;
2958 +               return NULL;
2959 +       }
2960 +
2961 +       for (i = 0; i < acl_d->count; i++) {
2962 +               struct acl      *ap     = &acl_d->acl[i];
2963 +               struct group    *gr;
2964 +               char            tagbuf[12];
2965 +               char            idbuf[12];
2966 +               char            *tag;
2967 +               char            *id     = "";
2968 +               char            perms[4];
2969 +               int             nbytes;
2970 +
2971 +               switch (ap->a_type) {
2972 +                       /*
2973 +                        * for debugging purposes it's probably more
2974 +                        * useful to dump unknown tag types rather
2975 +                        * than just returning an error
2976 +                        */
2977 +                       default:
2978 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2979 +                                       ap->a_type);
2980 +                               tag = tagbuf;
2981 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2982 +                                       (long)ap->a_id);
2983 +                               id = idbuf;
2984 +                               break;
2985 +
2986 +                       case SMB_ACL_USER:
2987 +                               id = uidtoname(ap->a_id);
2988 +                       case SMB_ACL_USER_OBJ:
2989 +                               tag = "user";
2990 +                               break;
2991 +
2992 +                       case SMB_ACL_GROUP:
2993 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
2994 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2995 +                                               (long)ap->a_id);
2996 +                                       id = idbuf;
2997 +                               } else {
2998 +                                       id = gr->gr_name;
2999 +                               }
3000 +                       case SMB_ACL_GROUP_OBJ:
3001 +                               tag = "group";
3002 +                               break;
3003 +
3004 +                       case SMB_ACL_OTHER:
3005 +                               tag = "other";
3006 +                               break;
3007 +
3008 +                       case SMB_ACL_MASK:
3009 +                               tag = "mask";
3010 +                               break;
3011 +
3012 +               }
3013 +
3014 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
3015 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
3016 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
3017 +               perms[3] = '\0';
3018 +
3019 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
3020 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
3021 +
3022 +               /*
3023 +                * If this entry would overflow the buffer
3024 +                * allocate enough additional memory for this
3025 +                * entry and an estimate of another 20 bytes
3026 +                * for each entry still to be processed
3027 +                */
3028 +               if ((len + nbytes) > maxlen) {
3029 +                       char *oldtext = text;
3030 +
3031 +                       maxlen += nbytes + 20 * (acl_d->count - i);
3032 +
3033 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
3034 +                               SAFE_FREE(oldtext);
3035 +                               errno = ENOMEM;
3036 +                               return NULL;
3037 +                       }
3038 +               }
3039 +
3040 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
3041 +               len += nbytes - 1;
3042 +       }
3043 +
3044 +       if (len_p)
3045 +               *len_p = len;
3046 +
3047 +       return text;
3048 +}
3049 +
3050 +SMB_ACL_T sys_acl_init(int count)
3051 +{
3052 +       SMB_ACL_T       a;
3053 +
3054 +       if (count < 0) {
3055 +               errno = EINVAL;
3056 +               return NULL;
3057 +       }
3058 +
3059 +       /*
3060 +        * note that since the definition of the structure pointed
3061 +        * to by the SMB_ACL_T includes the first element of the
3062 +        * acl[] array, this actually allocates an ACL with room
3063 +        * for (count+1) entries
3064 +        */
3065 +       if ((a = (SMB_ACL_T)SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
3066 +               errno = ENOMEM;
3067 +               return NULL;
3068 +       }
3069 +
3070 +       a->size = count + 1;
3071 +       a->count = 0;
3072 +       a->next = -1;
3073 +
3074 +       return a;
3075 +}
3076 +
3077 +
3078 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3079 +{
3080 +       SMB_ACL_T       acl_d;
3081 +       SMB_ACL_ENTRY_T entry_d;
3082 +
3083 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3084 +               errno = EINVAL;
3085 +               return -1;
3086 +       }
3087 +
3088 +       if (acl_d->count >= acl_d->size) {
3089 +               errno = ENOSPC;
3090 +               return -1;
3091 +       }
3092 +
3093 +       entry_d         = &acl_d->acl[acl_d->count++];
3094 +       entry_d->a_type = 0;
3095 +       entry_d->a_id   = -1;
3096 +       entry_d->a_perm = 0;
3097 +       *entry_p        = entry_d;
3098 +
3099 +       return 0;
3100 +}
3101 +
3102 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3103 +{
3104 +       switch (tag_type) {
3105 +               case SMB_ACL_USER:
3106 +               case SMB_ACL_USER_OBJ:
3107 +               case SMB_ACL_GROUP:
3108 +               case SMB_ACL_GROUP_OBJ:
3109 +               case SMB_ACL_OTHER:
3110 +               case SMB_ACL_MASK:
3111 +                       entry_d->a_type = tag_type;
3112 +                       break;
3113 +               default:
3114 +                       errno = EINVAL;
3115 +                       return -1;
3116 +       }
3117 +
3118 +       return 0;
3119 +}
3120 +
3121 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3122 +{
3123 +       if (entry_d->a_type != SMB_ACL_GROUP
3124 +           && entry_d->a_type != SMB_ACL_USER) {
3125 +               errno = EINVAL;
3126 +               return -1;
3127 +       }
3128 +
3129 +       entry_d->a_id = *((id_t *)qual_p);
3130 +
3131 +       return 0;
3132 +}
3133 +
3134 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3135 +{
3136 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3137 +               return EINVAL;
3138 +       }
3139 +
3140 +       entry_d->a_perm = *permset_d;
3141 +
3142 +       return 0;
3143 +}
3144 +
3145 +/*
3146 + * sort the ACL and check it for validity
3147 + *
3148 + * if it's a minimal ACL with only 4 entries then we
3149 + * need to recalculate the mask permissions to make
3150 + * sure that they are the same as the GROUP_OBJ
3151 + * permissions as required by the UnixWare acl() system call.
3152 + *
3153 + * (note: since POSIX allows minimal ACLs which only contain
3154 + * 3 entries - ie there is no mask entry - we should, in theory,
3155 + * check for this and add a mask entry if necessary - however
3156 + * we "know" that the caller of this interface always specifies
3157 + * a mask so, in practice "this never happens" (tm) - if it *does*
3158 + * happen aclsort() will fail and return an error and someone will
3159 + * have to fix it ...)
3160 + */
3161 +
3162 +static int acl_sort(SMB_ACL_T acl_d)
3163 +{
3164 +       int     fixmask = (acl_d->count <= 4);
3165 +
3166 +       if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
3167 +               errno = EINVAL;
3168 +               return -1;
3169 +       }
3170 +       return 0;
3171 +}
3172
3173 +int sys_acl_valid(SMB_ACL_T acl_d)
3174 +{
3175 +       return acl_sort(acl_d);
3176 +}
3177 +
3178 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3179 +{
3180 +       struct stat     s;
3181 +       struct acl      *acl_p;
3182 +       int             acl_count;
3183 +       struct acl      *acl_buf        = NULL;
3184 +       int             ret;
3185 +
3186 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3187 +               errno = EINVAL;
3188 +               return -1;
3189 +       }
3190 +
3191 +       if (acl_sort(acl_d) != 0) {
3192 +               return -1;
3193 +       }
3194 +
3195 +       acl_p           = &acl_d->acl[0];
3196 +       acl_count       = acl_d->count;
3197 +
3198 +       /*
3199 +        * if it's a directory there is extra work to do
3200 +        * since the acl() system call will replace both
3201 +        * the access ACLs and the default ACLs (if any)
3202 +        */
3203 +       if (stat(name, &s) != 0) {
3204 +               return -1;
3205 +       }
3206 +       if (S_ISDIR(s.st_mode)) {
3207 +               SMB_ACL_T       acc_acl;
3208 +               SMB_ACL_T       def_acl;
3209 +               SMB_ACL_T       tmp_acl;
3210 +               int             i;
3211 +
3212 +               if (type == SMB_ACL_TYPE_ACCESS) {
3213 +                       acc_acl = acl_d;
3214 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3215 +
3216 +               } else {
3217 +                       def_acl = acl_d;
3218 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3219 +               }
3220 +
3221 +               if (tmp_acl == NULL) {
3222 +                       return -1;
3223 +               }
3224 +
3225 +               /*
3226 +                * allocate a temporary buffer for the complete ACL
3227 +                */
3228 +               acl_count = acc_acl->count + def_acl->count;
3229 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
3230 +
3231 +               if (acl_buf == NULL) {
3232 +                       sys_acl_free_acl(tmp_acl);
3233 +                       errno = ENOMEM;
3234 +                       return -1;
3235 +               }
3236 +
3237 +               /*
3238 +                * copy the access control and default entries into the buffer
3239 +                */
3240 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
3241 +                       acc_acl->count * sizeof(acl_buf[0]));
3242 +
3243 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3244 +                       def_acl->count * sizeof(acl_buf[0]));
3245 +
3246 +               /*
3247 +                * set the ACL_DEFAULT flag on the default entries
3248 +                */
3249 +               for (i = acc_acl->count; i < acl_count; i++) {
3250 +                       acl_buf[i].a_type |= ACL_DEFAULT;
3251 +               }
3252 +
3253 +               sys_acl_free_acl(tmp_acl);
3254 +
3255 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
3256 +               errno = EINVAL;
3257 +               return -1;
3258 +       }
3259 +
3260 +       ret = acl(name, SETACL, acl_count, acl_p);
3261 +
3262 +       SAFE_FREE(acl_buf);
3263 +
3264 +       return ret;
3265 +}
3266 +
3267 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3268 +{
3269 +       if (acl_sort(acl_d) != 0) {
3270 +               return -1;
3271 +       }
3272 +
3273 +       return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
3274 +}
3275 +
3276 +int sys_acl_delete_def_file(const char *path)
3277 +{
3278 +       SMB_ACL_T       acl_d;
3279 +       int             ret;
3280 +
3281 +       /*
3282 +        * fetching the access ACL and rewriting it has
3283 +        * the effect of deleting the default ACL
3284 +        */
3285 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3286 +               return -1;
3287 +       }
3288 +
3289 +       ret = acl(path, SETACL, acl_d->count, acl_d->acl);
3290 +
3291 +       sys_acl_free_acl(acl_d);
3292 +       
3293 +       return ret;
3294 +}
3295 +
3296 +int sys_acl_free_text(char *text)
3297 +{
3298 +       SAFE_FREE(text);
3299 +       return 0;
3300 +}
3301 +
3302 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
3303 +{
3304 +       SAFE_FREE(acl_d);
3305 +       return 0;
3306 +}
3307 +
3308 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
3309 +{
3310 +       return 0;
3311 +}
3312 +
3313 +#elif defined(HAVE_HPUX_ACLS)
3314 +#include <dl.h>
3315 +