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