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