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