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