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