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