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