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