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