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