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