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