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