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