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