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