From Matt:
[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 (%02x) 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 unchanged = 1;
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 +                       unchanged = -1;
1052 +                       continue;
1053 +               }
1054 +               ok = unpack_smb_acl(&racl_orig, sacl_orig);
1055 +               sys_acl_free_acl(sacl_orig);
1056 +               if (!ok) {
1057 +                       unchanged = -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 +                               unchanged = -1;
1069 +                               continue;
1070 +                       }
1071 +               } else {
1072 +                       if (!*sacl_new)
1073 +                               if (!pack_smb_acl(sacl_new, racl_new)) {
1074 +                                       unchanged = -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 +                               unchanged = -1;
1082 +                               continue;
1083 +                       }
1084 +               }
1085 +               if (unchanged == 1)
1086 +                       unchanged = 0;
1087 +       }
1088 +       return unchanged;
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 @@ -132,6 +132,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 @@ -185,6 +186,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 @@ -260,6 +263,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 @@ -966,6 +966,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 @@ -977,6 +979,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 @@ -1365,6 +1371,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 @@ -1387,6 +1395,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 @@ -85,6 +85,7 @@ extern long block_size; /* "long" becaus
1400  extern int max_delete;
1401  extern int force_delete;
1402  extern int one_file_system;
1403 +extern mode_t orig_umask;
1404  extern struct stats stats;
1405  extern dev_t filesystem_dev;
1406  extern char *backup_dir;
1407 @@ -753,6 +754,7 @@ static int try_dests_non(struct file_str
1408  }
1409  
1410  static int phase = 0;
1411 +static int dflt_perms;
1412  
1413  /* Acts on the_file_list->file's ndx'th item, whose name is fname.  If a dir,
1414   * make sure it exists, and has the right permissions/timestamp info.  For
1415 @@ -844,6 +846,10 @@ static void recv_generator(char *fname, 
1416                         }
1417                         if (fuzzy_basis)
1418                                 need_fuzzy_dirlist = 1;
1419 +#ifdef SUPPORT_ACLS
1420 +                       if (!preserve_perms)
1421 +                               dflt_perms = default_perms_for_dir(dn);
1422 +#endif
1423                 }
1424                 parent_dirname = dn;
1425  
1426 @@ -871,7 +877,8 @@ static void recv_generator(char *fname, 
1427         if (!preserve_perms) {
1428                 int exists = statret == 0
1429                           && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1430 -               file->mode = dest_mode(file->mode, st.st_mode, exists);
1431 +               file->mode = dest_mode(file->mode, st.st_mode, dflt_perms,
1432 +                                      exists);
1433         }
1434  
1435         if (S_ISDIR(file->mode)) {
1436 @@ -1342,6 +1349,8 @@ void generate_files(int f_out, struct fi
1437          * notice that and let us know via the redo pipe (or its closing). */
1438         ignore_timeout = 1;
1439  
1440 +       dflt_perms = (ACCESSPERMS & ~orig_umask);
1441 +
1442         for (i = 0; i < flist->count; i++) {
1443                 struct file_struct *file = flist->files[i];
1444  
1445 --- old/lib/sysacls.c
1446 +++ new/lib/sysacls.c
1447 @@ -0,0 +1,3242 @@
1448 +/* 
1449 +   Unix SMB/CIFS implementation.
1450 +   Samba system utilities for ACL support.
1451 +   Copyright (C) Jeremy Allison 2000.
1452 +   
1453 +   This program is free software; you can redistribute it and/or modify
1454 +   it under the terms of the GNU General Public License as published by
1455 +   the Free Software Foundation; either version 2 of the License, or
1456 +   (at your option) any later version.
1457 +   
1458 +   This program is distributed in the hope that it will be useful,
1459 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
1460 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1461 +   GNU General Public License for more details.
1462 +   
1463 +   You should have received a copy of the GNU General Public License
1464 +   along with this program; if not, write to the Free Software
1465 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1466 +*/
1467 +
1468 +#include "rsync.h"
1469 +#include "sysacls.h" /****** ADDED ******/
1470 +
1471 +/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
1472 +void SAFE_FREE(void *mem)
1473 +{
1474 +       if (mem)
1475 +               free(mem);
1476 +}
1477 +
1478 +char *uidtoname(uid_t uid)
1479 +{
1480 +       static char idbuf[12];
1481 +       struct passwd *pw;
1482 +
1483 +       if ((pw = getpwuid(uid)) == NULL) {
1484 +               slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
1485 +               return idbuf;
1486 +       }
1487 +       return pw->pw_name;
1488 +}
1489 +/****** EXTRAS -- END ******/
1490 +
1491 +/*
1492 + This file wraps all differing system ACL interfaces into a consistent
1493 + one based on the POSIX interface. It also returns the correct errors
1494 + for older UNIX systems that don't support ACLs.
1495 +
1496 + The interfaces that each ACL implementation must support are as follows :
1497 +
1498 + int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1499 + int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1500 + int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
1501 + void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1502 + SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1503 + SMB_ACL_T sys_acl_get_fd(int fd)
1504 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
1505 + int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
1506 + char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
1507 + SMB_ACL_T sys_acl_init( int count)
1508 + int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1509 + int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1510 + int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1511 + int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1512 + int sys_acl_valid( SMB_ACL_T theacl )
1513 + int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1514 + int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1515 + int sys_acl_delete_def_file(const char *path)
1516 +
1517 + This next one is not POSIX complient - but we *have* to have it !
1518 + More POSIX braindamage.
1519 +
1520 + int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1521 +
1522 + The generic POSIX free is the following call. We split this into
1523 + several different free functions as we may need to add tag info
1524 + to structures when emulating the POSIX interface.
1525 +
1526 + int sys_acl_free( void *obj_p)
1527 +
1528 + The calls we actually use are :
1529 +
1530 + int sys_acl_free_text(char *text) - free acl_to_text
1531 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
1532 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
1533 +
1534 +*/
1535 +
1536 +#if defined(HAVE_POSIX_ACLS)
1537 +
1538 +/* Identity mapping - easy. */
1539 +
1540 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1541 +{
1542 +       return acl_get_entry( the_acl, entry_id, entry_p);
1543 +}
1544 +
1545 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1546 +{
1547 +       return acl_get_tag_type( entry_d, tag_type_p);
1548 +}
1549 +
1550 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1551 +{
1552 +       return acl_get_permset( entry_d, permset_p);
1553 +}
1554 +
1555 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1556 +{
1557 +       return acl_get_qualifier( entry_d);
1558 +}
1559 +
1560 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1561 +{
1562 +       return acl_get_file( path_p, type);
1563 +}
1564 +
1565 +SMB_ACL_T sys_acl_get_fd(int fd)
1566 +{
1567 +       return acl_get_fd(fd);
1568 +}
1569 +
1570 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1571 +{
1572 +       return acl_clear_perms(permset);
1573 +}
1574 +
1575 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1576 +{
1577 +       return acl_add_perm(permset, perm);
1578 +}
1579 +
1580 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1581 +{
1582 +#if defined(HAVE_ACL_GET_PERM_NP)
1583 +       /*
1584 +        * Required for TrustedBSD-based ACL implementations where
1585 +        * non-POSIX.1e functions are denoted by a _np (non-portable)
1586 +        * suffix.
1587 +        */
1588 +       return acl_get_perm_np(permset, perm);
1589 +#else
1590 +       return acl_get_perm(permset, perm);
1591 +#endif
1592 +}
1593 +
1594 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1595 +{
1596 +       return acl_to_text( the_acl, plen);
1597 +}
1598 +
1599 +SMB_ACL_T sys_acl_init( int count)
1600 +{
1601 +       return acl_init(count);
1602 +}
1603 +
1604 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1605 +{
1606 +       return acl_create_entry(pacl, pentry);
1607 +}
1608 +
1609 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1610 +{
1611 +       return acl_set_tag_type(entry, tagtype);
1612 +}
1613 +
1614 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1615 +{
1616 +       return acl_set_qualifier(entry, qual);
1617 +}
1618 +
1619 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1620 +{
1621 +       return acl_set_permset(entry, permset);
1622 +}
1623 +
1624 +int sys_acl_valid( SMB_ACL_T theacl )
1625 +{
1626 +       return acl_valid(theacl);
1627 +}
1628 +
1629 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1630 +{
1631 +       return acl_set_file(name, acltype, theacl);
1632 +}
1633 +
1634 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1635 +{
1636 +       return acl_set_fd(fd, theacl);
1637 +}
1638 +
1639 +int sys_acl_delete_def_file(const char *name)
1640 +{
1641 +       return acl_delete_def_file(name);
1642 +}
1643 +
1644 +int sys_acl_free_text(char *text)
1645 +{
1646 +       return acl_free(text);
1647 +}
1648 +
1649 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
1650 +{
1651 +       return acl_free(the_acl);
1652 +}
1653 +
1654 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
1655 +{
1656 +       return acl_free(qual);
1657 +}
1658 +
1659 +#elif defined(HAVE_TRU64_ACLS)
1660 +/*
1661 + * The interface to DEC/Compaq Tru64 UNIX ACLs
1662 + * is based on Draft 13 of the POSIX spec which is
1663 + * slightly different from the Draft 16 interface.
1664 + * 
1665 + * Also, some of the permset manipulation functions
1666 + * such as acl_clear_perm() and acl_add_perm() appear
1667 + * to be broken on Tru64 so we have to manipulate
1668 + * the permission bits in the permset directly.
1669 + */
1670 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1671 +{
1672 +       SMB_ACL_ENTRY_T entry;
1673 +
1674 +       if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
1675 +               return -1;
1676 +       }
1677 +
1678 +       errno = 0;
1679 +       if ((entry = acl_get_entry(the_acl)) != NULL) {
1680 +               *entry_p = entry;
1681 +               return 1;
1682 +       }
1683 +
1684 +       return errno ? -1 : 0;
1685 +}
1686 +
1687 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1688 +{
1689 +       return acl_get_tag_type( entry_d, tag_type_p);
1690 +}
1691 +
1692 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1693 +{
1694 +       return acl_get_permset( entry_d, permset_p);
1695 +}
1696 +
1697 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1698 +{
1699 +       return acl_get_qualifier( entry_d);
1700 +}
1701 +
1702 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1703 +{
1704 +       return acl_get_file((char *)path_p, type);
1705 +}
1706 +
1707 +SMB_ACL_T sys_acl_get_fd(int fd)
1708 +{
1709 +       return acl_get_fd(fd, ACL_TYPE_ACCESS);
1710 +}
1711 +
1712 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1713 +{
1714 +       *permset = 0;           /* acl_clear_perm() is broken on Tru64  */
1715 +
1716 +       return 0;
1717 +}
1718 +
1719 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1720 +{
1721 +       if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
1722 +               errno = EINVAL;
1723 +               return -1;
1724 +       }
1725 +
1726 +       *permset |= perm;       /* acl_add_perm() is broken on Tru64    */
1727 +
1728 +       return 0;
1729 +}
1730 +
1731 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1732 +{
1733 +       return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
1734 +}
1735 +
1736 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1737 +{
1738 +       return acl_to_text( the_acl, plen);
1739 +}
1740 +
1741 +SMB_ACL_T sys_acl_init( int count)
1742 +{
1743 +       return acl_init(count);
1744 +}
1745 +
1746 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1747 +{
1748 +       SMB_ACL_ENTRY_T entry;
1749 +
1750 +       if ((entry = acl_create_entry(pacl)) == NULL) {
1751 +               return -1;
1752 +       }
1753 +
1754 +       *pentry = entry;
1755 +       return 0;
1756 +}
1757 +
1758 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1759 +{
1760 +       return acl_set_tag_type(entry, tagtype);
1761 +}
1762 +
1763 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1764 +{
1765 +       return acl_set_qualifier(entry, qual);
1766 +}
1767 +
1768 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1769 +{
1770 +       return acl_set_permset(entry, permset);
1771 +}
1772 +
1773 +int sys_acl_valid( SMB_ACL_T theacl )
1774 +{
1775 +       acl_entry_t     entry;
1776 +
1777 +       return acl_valid(theacl, &entry);
1778 +}
1779 +
1780 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1781 +{
1782 +       return acl_set_file((char *)name, acltype, theacl);
1783 +}
1784 +
1785 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1786 +{
1787 +       return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
1788 +}
1789 +
1790 +int sys_acl_delete_def_file(const char *name)
1791 +{
1792 +       return acl_delete_def_file((char *)name);
1793 +}
1794 +
1795 +int sys_acl_free_text(char *text)
1796 +{
1797 +       /*
1798 +        * (void) cast and explicit return 0 are for DEC UNIX
1799 +        *  which just #defines acl_free_text() to be free()
1800 +        */
1801 +       (void) acl_free_text(text);
1802 +       return 0;
1803 +}
1804 +
1805 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
1806 +{
1807 +       return acl_free(the_acl);
1808 +}
1809 +
1810 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
1811 +{
1812 +       return acl_free_qualifier(qual, tagtype);
1813 +}
1814 +
1815 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
1816 +
1817 +/*
1818 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
1819 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
1820 + */
1821 +
1822 +/*
1823 + * Note that while this code implements sufficient functionality
1824 + * to support the sys_acl_* interfaces it does not provide all
1825 + * of the semantics of the POSIX ACL interfaces.
1826 + *
1827 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
1828 + * from a call to sys_acl_get_entry() should not be assumed to be
1829 + * valid after calling any of the following functions, which may
1830 + * reorder the entries in the ACL.
1831 + *
1832 + *     sys_acl_valid()
1833 + *     sys_acl_set_file()
1834 + *     sys_acl_set_fd()
1835 + */
1836 +
1837 +/*
1838 + * The only difference between Solaris and UnixWare / OpenUNIX is
1839 + * that the #defines for the ACL operations have different names
1840 + */
1841 +#if defined(HAVE_UNIXWARE_ACLS)
1842 +
1843 +#define        SETACL          ACL_SET
1844 +#define        GETACL          ACL_GET
1845 +#define        GETACLCNT       ACL_CNT
1846 +
1847 +#endif
1848 +
1849 +
1850 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1851 +{
1852 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
1853 +               errno = EINVAL;
1854 +               return -1;
1855 +       }
1856 +
1857 +       if (entry_p == NULL) {
1858 +               errno = EINVAL;
1859 +               return -1;
1860 +       }
1861 +
1862 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
1863 +               acl_d->next = 0;
1864 +       }
1865 +
1866 +       if (acl_d->next < 0) {
1867 +               errno = EINVAL;
1868 +               return -1;
1869 +       }
1870 +
1871 +       if (acl_d->next >= acl_d->count) {
1872 +               return 0;
1873 +       }
1874 +
1875 +       *entry_p = &acl_d->acl[acl_d->next++];
1876 +
1877 +       return 1;
1878 +}
1879 +
1880 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
1881 +{
1882 +       *type_p = entry_d->a_type;
1883 +
1884 +       return 0;
1885 +}
1886 +
1887 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1888 +{
1889 +       *permset_p = &entry_d->a_perm;
1890 +
1891 +       return 0;
1892 +}
1893 +
1894 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
1895 +{
1896 +       if (entry_d->a_type != SMB_ACL_USER
1897 +           && entry_d->a_type != SMB_ACL_GROUP) {
1898 +               errno = EINVAL;
1899 +               return NULL;
1900 +       }
1901 +
1902 +       return &entry_d->a_id;
1903 +}
1904 +
1905 +/*
1906 + * There is no way of knowing what size the ACL returned by
1907 + * GETACL will be unless you first call GETACLCNT which means
1908 + * making an additional system call.
1909 + *
1910 + * In the hope of avoiding the cost of the additional system
1911 + * call in most cases, we initially allocate enough space for
1912 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
1913 + * be too small then we use GETACLCNT to find out the actual
1914 + * size, reallocate the ACL buffer, and then call GETACL again.
1915 + */
1916 +
1917 +#define        INITIAL_ACL_SIZE        16
1918 +
1919 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
1920 +{
1921 +       SMB_ACL_T       acl_d;
1922 +       int             count;          /* # of ACL entries allocated   */
1923 +       int             naccess;        /* # of access ACL entries      */
1924 +       int             ndefault;       /* # of default ACL entries     */
1925 +
1926 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
1927 +               errno = EINVAL;
1928 +               return NULL;
1929 +       }
1930 +
1931 +       count = INITIAL_ACL_SIZE;
1932 +       if ((acl_d = sys_acl_init(count)) == NULL) {
1933 +               return NULL;
1934 +       }
1935 +
1936 +       /*
1937 +        * If there isn't enough space for the ACL entries we use
1938 +        * GETACLCNT to determine the actual number of ACL entries
1939 +        * reallocate and try again. This is in a loop because it
1940 +        * is possible that someone else could modify the ACL and
1941 +        * increase the number of entries between the call to
1942 +        * GETACLCNT and the call to GETACL.
1943 +        */
1944 +       while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
1945 +           && errno == ENOSPC) {
1946 +
1947 +               sys_acl_free_acl(acl_d);
1948 +
1949 +               if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
1950 +                       return NULL;
1951 +               }
1952 +
1953 +               if ((acl_d = sys_acl_init(count)) == NULL) {
1954 +                       return NULL;
1955 +               }
1956 +       }
1957 +
1958 +       if (count < 0) {
1959 +               sys_acl_free_acl(acl_d);
1960 +               return NULL;
1961 +       }
1962 +
1963 +       /*
1964 +        * calculate the number of access and default ACL entries
1965 +        *
1966 +        * Note: we assume that the acl() system call returned a
1967 +        * well formed ACL which is sorted so that all of the
1968 +        * access ACL entries preceed any default ACL entries
1969 +        */
1970 +       for (naccess = 0; naccess < count; naccess++) {
1971 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
1972 +                       break;
1973 +       }
1974 +       ndefault = count - naccess;
1975 +       
1976 +       /*
1977 +        * if the caller wants the default ACL we have to copy
1978 +        * the entries down to the start of the acl[] buffer
1979 +        * and mask out the ACL_DEFAULT flag from the type field
1980 +        */
1981 +       if (type == SMB_ACL_TYPE_DEFAULT) {
1982 +               int     i, j;
1983 +
1984 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
1985 +                       acl_d->acl[i] = acl_d->acl[j];
1986 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
1987 +               }
1988 +
1989 +               acl_d->count = ndefault;
1990 +       } else {
1991 +               acl_d->count = naccess;
1992 +       }
1993 +
1994 +       return acl_d;
1995 +}
1996 +
1997 +SMB_ACL_T sys_acl_get_fd(int fd)
1998 +{
1999 +       SMB_ACL_T       acl_d;
2000 +       int             count;          /* # of ACL entries allocated   */
2001 +       int             naccess;        /* # of access ACL entries      */
2002 +
2003 +       count = INITIAL_ACL_SIZE;
2004 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2005 +               return NULL;
2006 +       }
2007 +
2008 +       while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2009 +           && errno == ENOSPC) {
2010 +
2011 +               sys_acl_free_acl(acl_d);
2012 +
2013 +               if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2014 +                       return NULL;
2015 +               }
2016 +
2017 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2018 +                       return NULL;
2019 +               }
2020 +       }
2021 +
2022 +       if (count < 0) {
2023 +               sys_acl_free_acl(acl_d);
2024 +               return NULL;
2025 +       }
2026 +
2027 +       /*
2028 +        * calculate the number of access ACL entries
2029 +        */
2030 +       for (naccess = 0; naccess < count; naccess++) {
2031 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2032 +                       break;
2033 +       }
2034 +       
2035 +       acl_d->count = naccess;
2036 +
2037 +       return acl_d;
2038 +}
2039 +
2040 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2041 +{
2042 +       *permset_d = 0;
2043 +
2044 +       return 0;
2045 +}
2046 +
2047 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2048 +{
2049 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2050 +           && perm != SMB_ACL_EXECUTE) {
2051 +               errno = EINVAL;
2052 +               return -1;
2053 +       }
2054 +
2055 +       if (permset_d == NULL) {
2056 +               errno = EINVAL;
2057 +               return -1;
2058 +       }
2059 +
2060 +       *permset_d |= perm;
2061 +
2062 +       return 0;
2063 +}
2064 +
2065 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2066 +{
2067 +       return *permset_d & perm;
2068 +}
2069 +
2070 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2071 +{
2072 +       int     i;
2073 +       int     len, maxlen;
2074 +       char    *text;
2075 +
2076 +       /*
2077 +        * use an initial estimate of 20 bytes per ACL entry
2078 +        * when allocating memory for the text representation
2079 +        * of the ACL
2080 +        */
2081 +       len     = 0;
2082 +       maxlen  = 20 * acl_d->count;
2083 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
2084 +               errno = ENOMEM;
2085 +               return NULL;
2086 +       }
2087 +
2088 +       for (i = 0; i < acl_d->count; i++) {
2089 +               struct acl      *ap     = &acl_d->acl[i];
2090 +               struct passwd   *pw;
2091 +               struct group    *gr;
2092 +               char            tagbuf[12];
2093 +               char            idbuf[12];
2094 +               char            *tag;
2095 +               char            *id     = "";
2096 +               char            perms[4];
2097 +               int             nbytes;
2098 +
2099 +               switch (ap->a_type) {
2100 +                       /*
2101 +                        * for debugging purposes it's probably more
2102 +                        * useful to dump unknown tag types rather
2103 +                        * than just returning an error
2104 +                        */
2105 +                       default:
2106 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2107 +                                       ap->a_type);
2108 +                               tag = tagbuf;
2109 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2110 +                                       (long)ap->a_id);
2111 +                               id = idbuf;
2112 +                               break;
2113 +
2114 +                       case SMB_ACL_USER:
2115 +                               id = uidtoname(ap->a_id);
2116 +                       case SMB_ACL_USER_OBJ:
2117 +                               tag = "user";
2118 +                               break;
2119 +
2120 +                       case SMB_ACL_GROUP:
2121 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
2122 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2123 +                                               (long)ap->a_id);
2124 +                                       id = idbuf;
2125 +                               } else {
2126 +                                       id = gr->gr_name;
2127 +                               }
2128 +                       case SMB_ACL_GROUP_OBJ:
2129 +                               tag = "group";
2130 +                               break;
2131 +
2132 +                       case SMB_ACL_OTHER:
2133 +                               tag = "other";
2134 +                               break;
2135 +
2136 +                       case SMB_ACL_MASK:
2137 +                               tag = "mask";
2138 +                               break;
2139 +
2140 +               }
2141 +
2142 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2143 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2144 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2145 +               perms[3] = '\0';
2146 +
2147 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
2148 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2149 +
2150 +               /*
2151 +                * If this entry would overflow the buffer
2152 +                * allocate enough additional memory for this
2153 +                * entry and an estimate of another 20 bytes
2154 +                * for each entry still to be processed
2155 +                */
2156 +               if ((len + nbytes) > maxlen) {
2157 +                       char *oldtext = text;
2158 +
2159 +                       maxlen += nbytes + 20 * (acl_d->count - i);
2160 +
2161 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2162 +                               SAFE_FREE(oldtext);
2163 +                               errno = ENOMEM;
2164 +                               return NULL;
2165 +                       }
2166 +               }
2167 +
2168 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2169 +               len += nbytes - 1;
2170 +       }
2171 +
2172 +       if (len_p)
2173 +               *len_p = len;
2174 +
2175 +       return text;
2176 +}
2177 +
2178 +SMB_ACL_T sys_acl_init(int count)
2179 +{
2180 +       SMB_ACL_T       a;
2181 +
2182 +       if (count < 0) {
2183 +               errno = EINVAL;
2184 +               return NULL;
2185 +       }
2186 +
2187 +       /*
2188 +        * note that since the definition of the structure pointed
2189 +        * to by the SMB_ACL_T includes the first element of the
2190 +        * acl[] array, this actually allocates an ACL with room
2191 +        * for (count+1) entries
2192 +        */
2193 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2194 +               errno = ENOMEM;
2195 +               return NULL;
2196 +       }
2197 +
2198 +       a->size = count + 1;
2199 +       a->count = 0;
2200 +       a->next = -1;
2201 +
2202 +       return a;
2203 +}
2204 +
2205 +
2206 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2207 +{
2208 +       SMB_ACL_T       acl_d;
2209 +       SMB_ACL_ENTRY_T entry_d;
2210 +
2211 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2212 +               errno = EINVAL;
2213 +               return -1;
2214 +       }
2215 +
2216 +       if (acl_d->count >= acl_d->size) {
2217 +               errno = ENOSPC;
2218 +               return -1;
2219 +       }
2220 +
2221 +       entry_d         = &acl_d->acl[acl_d->count++];
2222 +       entry_d->a_type = 0;
2223 +       entry_d->a_id   = -1;
2224 +       entry_d->a_perm = 0;
2225 +       *entry_p        = entry_d;
2226 +
2227 +       return 0;
2228 +}
2229 +
2230 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2231 +{
2232 +       switch (tag_type) {
2233 +               case SMB_ACL_USER:
2234 +               case SMB_ACL_USER_OBJ:
2235 +               case SMB_ACL_GROUP:
2236 +               case SMB_ACL_GROUP_OBJ:
2237 +               case SMB_ACL_OTHER:
2238 +               case SMB_ACL_MASK:
2239 +                       entry_d->a_type = tag_type;
2240 +                       break;
2241 +               default:
2242 +                       errno = EINVAL;
2243 +                       return -1;
2244 +       }
2245 +
2246 +       return 0;
2247 +}
2248 +
2249 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2250 +{
2251 +       if (entry_d->a_type != SMB_ACL_GROUP
2252 +           && entry_d->a_type != SMB_ACL_USER) {
2253 +               errno = EINVAL;
2254 +               return -1;
2255 +       }
2256 +
2257 +       entry_d->a_id = *((id_t *)qual_p);
2258 +
2259 +       return 0;
2260 +}
2261 +
2262 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2263 +{
2264 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2265 +               return EINVAL;
2266 +       }
2267 +
2268 +       entry_d->a_perm = *permset_d;
2269 +
2270 +       return 0;
2271 +}
2272 +
2273 +/*
2274 + * sort the ACL and check it for validity
2275 + *
2276 + * if it's a minimal ACL with only 4 entries then we
2277 + * need to recalculate the mask permissions to make
2278 + * sure that they are the same as the GROUP_OBJ
2279 + * permissions as required by the UnixWare acl() system call.
2280 + *
2281 + * (note: since POSIX allows minimal ACLs which only contain
2282 + * 3 entries - ie there is no mask entry - we should, in theory,
2283 + * check for this and add a mask entry if necessary - however
2284 + * we "know" that the caller of this interface always specifies
2285 + * a mask so, in practice "this never happens" (tm) - if it *does*
2286 + * happen aclsort() will fail and return an error and someone will
2287 + * have to fix it ...)
2288 + */
2289 +
2290 +static int acl_sort(SMB_ACL_T acl_d)
2291 +{
2292 +       int     fixmask = (acl_d->count <= 4);
2293 +
2294 +       if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
2295 +               errno = EINVAL;
2296 +               return -1;
2297 +       }
2298 +       return 0;
2299 +}
2300
2301 +int sys_acl_valid(SMB_ACL_T acl_d)
2302 +{
2303 +       return acl_sort(acl_d);
2304 +}
2305 +
2306 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
2307 +{
2308 +       struct stat     s;
2309 +       struct acl      *acl_p;
2310 +       int             acl_count;
2311 +       struct acl      *acl_buf        = NULL;
2312 +       int             ret;
2313 +
2314 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2315 +               errno = EINVAL;
2316 +               return -1;
2317 +       }
2318 +
2319 +       if (acl_sort(acl_d) != 0) {
2320 +               return -1;
2321 +       }
2322 +
2323 +       acl_p           = &acl_d->acl[0];
2324 +       acl_count       = acl_d->count;
2325 +
2326 +       /*
2327 +        * if it's a directory there is extra work to do
2328 +        * since the acl() system call will replace both
2329 +        * the access ACLs and the default ACLs (if any)
2330 +        */
2331 +       if (stat(name, &s) != 0) {
2332 +               return -1;
2333 +       }
2334 +       if (S_ISDIR(s.st_mode)) {
2335 +               SMB_ACL_T       acc_acl;
2336 +               SMB_ACL_T       def_acl;
2337 +               SMB_ACL_T       tmp_acl;
2338 +               int             i;
2339 +
2340 +               if (type == SMB_ACL_TYPE_ACCESS) {
2341 +                       acc_acl = acl_d;
2342 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
2343 +
2344 +               } else {
2345 +                       def_acl = acl_d;
2346 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
2347 +               }
2348 +
2349 +               if (tmp_acl == NULL) {
2350 +                       return -1;
2351 +               }
2352 +
2353 +               /*
2354 +                * allocate a temporary buffer for the complete ACL
2355 +                */
2356 +               acl_count = acc_acl->count + def_acl->count;
2357 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
2358 +
2359 +               if (acl_buf == NULL) {
2360 +                       sys_acl_free_acl(tmp_acl);
2361 +                       errno = ENOMEM;
2362 +                       return -1;
2363 +               }
2364 +
2365 +               /*
2366 +                * copy the access control and default entries into the buffer
2367 +                */
2368 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
2369 +                       acc_acl->count * sizeof(acl_buf[0]));
2370 +
2371 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
2372 +                       def_acl->count * sizeof(acl_buf[0]));
2373 +
2374 +               /*
2375 +                * set the ACL_DEFAULT flag on the default entries
2376 +                */
2377 +               for (i = acc_acl->count; i < acl_count; i++) {
2378 +                       acl_buf[i].a_type |= ACL_DEFAULT;
2379 +               }
2380 +
2381 +               sys_acl_free_acl(tmp_acl);
2382 +
2383 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
2384 +               errno = EINVAL;
2385 +               return -1;
2386 +       }
2387 +
2388 +       ret = acl(name, SETACL, acl_count, acl_p);
2389 +
2390 +       SAFE_FREE(acl_buf);
2391 +
2392 +       return ret;
2393 +}
2394 +
2395 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
2396 +{
2397 +       if (acl_sort(acl_d) != 0) {
2398 +               return -1;
2399 +       }
2400 +
2401 +       return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
2402 +}
2403 +
2404 +int sys_acl_delete_def_file(const char *path)
2405 +{
2406 +       SMB_ACL_T       acl_d;
2407 +       int             ret;
2408 +
2409 +       /*
2410 +        * fetching the access ACL and rewriting it has
2411 +        * the effect of deleting the default ACL
2412 +        */
2413 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
2414 +               return -1;
2415 +       }
2416 +
2417 +       ret = acl(path, SETACL, acl_d->count, acl_d->acl);
2418 +
2419 +       sys_acl_free_acl(acl_d);
2420 +       
2421 +       return ret;
2422 +}
2423 +
2424 +int sys_acl_free_text(char *text)
2425 +{
2426 +       SAFE_FREE(text);
2427 +       return 0;
2428 +}
2429 +
2430 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
2431 +{
2432 +       SAFE_FREE(acl_d);
2433 +       return 0;
2434 +}
2435 +
2436 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2437 +{
2438 +       return 0;
2439 +}
2440 +
2441 +#elif defined(HAVE_HPUX_ACLS)
2442 +#include <dl.h>
2443 +
2444 +/*
2445 + * Based on the Solaris/SCO code - with modifications.
2446 + */
2447 +
2448 +/*
2449 + * Note that while this code implements sufficient functionality
2450 + * to support the sys_acl_* interfaces it does not provide all
2451 + * of the semantics of the POSIX ACL interfaces.
2452 + *
2453 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2454 + * from a call to sys_acl_get_entry() should not be assumed to be
2455 + * valid after calling any of the following functions, which may
2456 + * reorder the entries in the ACL.
2457 + *
2458 + *     sys_acl_valid()
2459 + *     sys_acl_set_file()
2460 + *     sys_acl_set_fd()
2461 + */
2462 +
2463 +/* This checks if the POSIX ACL system call is defined */
2464 +/* which basically corresponds to whether JFS 3.3 or   */
2465 +/* higher is installed. If acl() was called when it    */
2466 +/* isn't defined, it causes the process to core dump   */
2467 +/* so it is important to check this and avoid acl()    */
2468 +/* calls if it isn't there.                            */
2469 +
2470 +static BOOL hpux_acl_call_presence(void)
2471 +{
2472 +
2473 +       shl_t handle = NULL;
2474 +       void *value;
2475 +       int ret_val=0;
2476 +       static BOOL already_checked=0;
2477 +
2478 +       if(already_checked)
2479 +               return True;
2480 +
2481 +
2482 +       ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
2483 +
2484 +       if(ret_val != 0) {
2485 +               DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
2486 +                       ret_val, errno, strerror(errno)));
2487 +               DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
2488 +               return False;
2489 +       }
2490 +
2491 +       DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
2492 +
2493 +       already_checked = True;
2494 +       return True;
2495 +}
2496 +
2497 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2498 +{
2499 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2500 +               errno = EINVAL;
2501 +               return -1;
2502 +       }
2503 +
2504 +       if (entry_p == NULL) {
2505 +               errno = EINVAL;
2506 +               return -1;
2507 +       }
2508 +
2509 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
2510 +               acl_d->next = 0;
2511 +       }
2512 +
2513 +       if (acl_d->next < 0) {
2514 +               errno = EINVAL;
2515 +               return -1;
2516 +       }
2517 +
2518 +       if (acl_d->next >= acl_d->count) {
2519 +               return 0;
2520 +       }
2521 +
2522 +       *entry_p = &acl_d->acl[acl_d->next++];
2523 +
2524 +       return 1;
2525 +}
2526 +
2527 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2528 +{
2529 +       *type_p = entry_d->a_type;
2530 +
2531 +       return 0;
2532 +}
2533 +
2534 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2535 +{
2536 +       *permset_p = &entry_d->a_perm;
2537 +
2538 +       return 0;
2539 +}
2540 +
2541 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2542 +{
2543 +       if (entry_d->a_type != SMB_ACL_USER
2544 +           && entry_d->a_type != SMB_ACL_GROUP) {
2545 +               errno = EINVAL;
2546 +               return NULL;
2547 +       }
2548 +
2549 +       return &entry_d->a_id;
2550 +}
2551 +
2552 +/*
2553 + * There is no way of knowing what size the ACL returned by
2554 + * ACL_GET will be unless you first call ACL_CNT which means
2555 + * making an additional system call.
2556 + *
2557 + * In the hope of avoiding the cost of the additional system
2558 + * call in most cases, we initially allocate enough space for
2559 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2560 + * be too small then we use ACL_CNT to find out the actual
2561 + * size, reallocate the ACL buffer, and then call ACL_GET again.
2562 + */
2563 +
2564 +#define        INITIAL_ACL_SIZE        16
2565 +
2566 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2567 +{
2568 +       SMB_ACL_T       acl_d;
2569 +       int             count;          /* # of ACL entries allocated   */
2570 +       int             naccess;        /* # of access ACL entries      */
2571 +       int             ndefault;       /* # of default ACL entries     */
2572 +
2573 +       if(hpux_acl_call_presence() == False) {
2574 +               /* Looks like we don't have the acl() system call on HPUX. 
2575 +                * May be the system doesn't have the latest version of JFS.
2576 +                */
2577 +               return NULL; 
2578 +       }
2579 +
2580 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2581 +               errno = EINVAL;
2582 +               return NULL;
2583 +       }
2584 +
2585 +       count = INITIAL_ACL_SIZE;
2586 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2587 +               return NULL;
2588 +       }
2589 +
2590 +       /*
2591 +        * If there isn't enough space for the ACL entries we use
2592 +        * ACL_CNT to determine the actual number of ACL entries
2593 +        * reallocate and try again. This is in a loop because it
2594 +        * is possible that someone else could modify the ACL and
2595 +        * increase the number of entries between the call to
2596 +        * ACL_CNT and the call to ACL_GET.
2597 +        */
2598 +       while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
2599 +
2600 +               sys_acl_free_acl(acl_d);
2601 +
2602 +               if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
2603 +                       return NULL;
2604 +               }
2605 +
2606 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2607 +                       return NULL;
2608 +               }
2609 +       }
2610 +
2611 +       if (count < 0) {
2612 +               sys_acl_free_acl(acl_d);
2613 +               return NULL;
2614 +       }
2615 +
2616 +       /*
2617 +        * calculate the number of access and default ACL entries
2618 +        *
2619 +        * Note: we assume that the acl() system call returned a
2620 +        * well formed ACL which is sorted so that all of the
2621 +        * access ACL entries preceed any default ACL entries
2622 +        */
2623 +       for (naccess = 0; naccess < count; naccess++) {
2624 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2625 +                       break;
2626 +       }
2627 +       ndefault = count - naccess;
2628 +       
2629 +       /*
2630 +        * if the caller wants the default ACL we have to copy
2631 +        * the entries down to the start of the acl[] buffer
2632 +        * and mask out the ACL_DEFAULT flag from the type field
2633 +        */
2634 +       if (type == SMB_ACL_TYPE_DEFAULT) {
2635 +               int     i, j;
2636 +
2637 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
2638 +                       acl_d->acl[i] = acl_d->acl[j];
2639 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2640 +               }
2641 +
2642 +               acl_d->count = ndefault;
2643 +       } else {
2644 +               acl_d->count = naccess;
2645 +       }
2646 +
2647 +       return acl_d;
2648 +}
2649 +
2650 +SMB_ACL_T sys_acl_get_fd(int fd)
2651 +{
2652 +       /*
2653 +        * HPUX doesn't have the facl call. Fake it using the path.... JRA.
2654 +        */
2655 +
2656 +       files_struct *fsp = file_find_fd(fd);
2657 +
2658 +       if (fsp == NULL) {
2659 +               errno = EBADF;
2660 +               return NULL;
2661 +       }
2662 +
2663 +       /*
2664 +        * We know we're in the same conn context. So we
2665 +        * can use the relative path.
2666 +        */
2667 +
2668 +       return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
2669 +}
2670 +
2671 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2672 +{
2673 +       *permset_d = 0;
2674 +
2675 +       return 0;
2676 +}
2677 +
2678 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2679 +{
2680 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2681 +           && perm != SMB_ACL_EXECUTE) {
2682 +               errno = EINVAL;
2683 +               return -1;
2684 +       }
2685 +
2686 +       if (permset_d == NULL) {
2687 +               errno = EINVAL;
2688 +               return -1;
2689 +       }
2690 +
2691 +       *permset_d |= perm;
2692 +
2693 +       return 0;
2694 +}
2695 +
2696 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2697 +{
2698 +       return *permset_d & perm;
2699 +}
2700 +
2701 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2702 +{
2703 +       int     i;
2704 +       int     len, maxlen;
2705 +       char    *text;
2706 +
2707 +       /*
2708 +        * use an initial estimate of 20 bytes per ACL entry
2709 +        * when allocating memory for the text representation
2710 +        * of the ACL
2711 +        */
2712 +       len     = 0;
2713 +       maxlen  = 20 * acl_d->count;
2714 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
2715 +               errno = ENOMEM;
2716 +               return NULL;
2717 +       }
2718 +
2719 +       for (i = 0; i < acl_d->count; i++) {
2720 +               struct acl      *ap     = &acl_d->acl[i];
2721 +               struct passwd   *pw;
2722 +               struct group    *gr;
2723 +               char            tagbuf[12];
2724 +               char            idbuf[12];
2725 +               char            *tag;
2726 +               char            *id     = "";
2727 +               char            perms[4];
2728 +               int             nbytes;
2729 +
2730 +               switch (ap->a_type) {
2731 +                       /*
2732 +                        * for debugging purposes it's probably more
2733 +                        * useful to dump unknown tag types rather
2734 +                        * than just returning an error
2735 +                        */
2736 +                       default:
2737 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2738 +                                       ap->a_type);
2739 +                               tag = tagbuf;
2740 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2741 +                                       (long)ap->a_id);
2742 +                               id = idbuf;
2743 +                               break;
2744 +
2745 +                       case SMB_ACL_USER:
2746 +                               id = uidtoname(ap->a_id);
2747 +                       case SMB_ACL_USER_OBJ:
2748 +                               tag = "user";
2749 +                               break;
2750 +
2751 +                       case SMB_ACL_GROUP:
2752 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
2753 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2754 +                                               (long)ap->a_id);
2755 +                                       id = idbuf;
2756 +                               } else {
2757 +                                       id = gr->gr_name;
2758 +                               }
2759 +                       case SMB_ACL_GROUP_OBJ:
2760 +                               tag = "group";
2761 +                               break;
2762 +
2763 +                       case SMB_ACL_OTHER:
2764 +                               tag = "other";
2765 +                               break;
2766 +
2767 +                       case SMB_ACL_MASK:
2768 +                               tag = "mask";
2769 +                               break;
2770 +
2771 +               }
2772 +
2773 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2774 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2775 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2776 +               perms[3] = '\0';
2777 +
2778 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
2779 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2780 +
2781 +               /*
2782 +                * If this entry would overflow the buffer
2783 +                * allocate enough additional memory for this
2784 +                * entry and an estimate of another 20 bytes
2785 +                * for each entry still to be processed
2786 +                */
2787 +               if ((len + nbytes) > maxlen) {
2788 +                       char *oldtext = text;
2789 +
2790 +                       maxlen += nbytes + 20 * (acl_d->count - i);
2791 +
2792 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2793 +                               free(oldtext);
2794 +                               errno = ENOMEM;
2795 +                               return NULL;
2796 +                       }
2797 +               }
2798 +
2799 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2800 +               len += nbytes - 1;
2801 +       }
2802 +
2803 +       if (len_p)
2804 +               *len_p = len;
2805 +
2806 +       return text;
2807 +}
2808 +
2809 +SMB_ACL_T sys_acl_init(int count)
2810 +{
2811 +       SMB_ACL_T       a;
2812 +
2813 +       if (count < 0) {
2814 +               errno = EINVAL;
2815 +               return NULL;
2816 +       }
2817 +
2818 +       /*
2819 +        * note that since the definition of the structure pointed
2820 +        * to by the SMB_ACL_T includes the first element of the
2821 +        * acl[] array, this actually allocates an ACL with room
2822 +        * for (count+1) entries
2823 +        */
2824 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2825 +               errno = ENOMEM;
2826 +               return NULL;
2827 +       }
2828 +
2829 +       a->size = count + 1;
2830 +       a->count = 0;
2831 +       a->next = -1;
2832 +
2833 +       return a;
2834 +}
2835 +
2836 +
2837 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2838 +{
2839 +       SMB_ACL_T       acl_d;
2840 +       SMB_ACL_ENTRY_T entry_d;
2841 +
2842 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2843 +               errno = EINVAL;
2844 +               return -1;
2845 +       }
2846 +
2847 +       if (acl_d->count >= acl_d->size) {
2848 +               errno = ENOSPC;
2849 +               return -1;
2850 +       }
2851 +
2852 +       entry_d         = &acl_d->acl[acl_d->count++];
2853 +       entry_d->a_type = 0;
2854 +       entry_d->a_id   = -1;
2855 +       entry_d->a_perm = 0;
2856 +       *entry_p        = entry_d;
2857 +
2858 +       return 0;
2859 +}
2860 +
2861 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2862 +{
2863 +       switch (tag_type) {
2864 +               case SMB_ACL_USER:
2865 +               case SMB_ACL_USER_OBJ:
2866 +               case SMB_ACL_GROUP:
2867 +               case SMB_ACL_GROUP_OBJ:
2868 +               case SMB_ACL_OTHER:
2869 +               case SMB_ACL_MASK:
2870 +                       entry_d->a_type = tag_type;
2871 +                       break;
2872 +               default:
2873 +                       errno = EINVAL;
2874 +                       return -1;
2875 +       }
2876 +
2877 +       return 0;
2878 +}
2879 +
2880 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2881 +{
2882 +       if (entry_d->a_type != SMB_ACL_GROUP
2883 +           && entry_d->a_type != SMB_ACL_USER) {
2884 +               errno = EINVAL;
2885 +               return -1;
2886 +       }
2887 +
2888 +       entry_d->a_id = *((id_t *)qual_p);
2889 +
2890 +       return 0;
2891 +}
2892 +
2893 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2894 +{
2895 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2896 +               return EINVAL;
2897 +       }
2898 +
2899 +       entry_d->a_perm = *permset_d;
2900 +
2901 +       return 0;
2902 +}
2903 +
2904 +/* Structure to capture the count for each type of ACE. */
2905 +
2906 +struct hpux_acl_types {
2907 +       int n_user;
2908 +       int n_def_user;
2909 +       int n_user_obj;
2910 +       int n_def_user_obj;
2911 +
2912 +       int n_group;
2913 +       int n_def_group;
2914 +       int n_group_obj;
2915 +       int n_def_group_obj;
2916 +
2917 +       int n_other;
2918 +       int n_other_obj;
2919 +       int n_def_other_obj;
2920 +
2921 +       int n_class_obj;
2922 +       int n_def_class_obj;
2923 +
2924 +       int n_illegal_obj;
2925 +};
2926 +
2927 +/* count_obj:
2928 + * Counts the different number of objects in a given array of ACL
2929 + * structures.
2930 + * Inputs:
2931 + *
2932 + * acl_count      - Count of ACLs in the array of ACL strucutres.
2933 + * aclp           - Array of ACL structures.
2934 + * acl_type_count - Pointer to acl_types structure. Should already be
2935 + *                  allocated.
2936 + * Output: 
2937 + *
2938 + * acl_type_count - This structure is filled up with counts of various 
2939 + *                  acl types.
2940 + */
2941 +
2942 +static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
2943 +{
2944 +       int i;
2945 +
2946 +       memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
2947 +
2948 +       for(i=0;i<acl_count;i++) {
2949 +               switch(aclp[i].a_type) {
2950 +               case USER: 
2951 +                       acl_type_count->n_user++;
2952 +                       break;
2953 +               case USER_OBJ: 
2954 +                       acl_type_count->n_user_obj++;
2955 +                       break;
2956 +               case DEF_USER_OBJ: 
2957 +                       acl_type_count->n_def_user_obj++;
2958 +                       break;
2959 +               case GROUP: 
2960 +                       acl_type_count->n_group++;
2961 +                       break;
2962 +               case GROUP_OBJ: 
2963 +                       acl_type_count->n_group_obj++;
2964 +                       break;
2965 +               case DEF_GROUP_OBJ: 
2966 +                       acl_type_count->n_def_group_obj++;
2967 +                       break;
2968 +               case OTHER_OBJ: 
2969 +                       acl_type_count->n_other_obj++;
2970 +                       break;
2971 +               case DEF_OTHER_OBJ: 
2972 +                       acl_type_count->n_def_other_obj++;
2973 +                       break;
2974 +               case CLASS_OBJ:
2975 +                       acl_type_count->n_class_obj++;
2976 +                       break;
2977 +               case DEF_CLASS_OBJ:
2978 +                       acl_type_count->n_def_class_obj++;
2979 +                       break;
2980 +               case DEF_USER:
2981 +                       acl_type_count->n_def_user++;
2982 +                       break;
2983 +               case DEF_GROUP:
2984 +                       acl_type_count->n_def_group++;
2985 +                       break;
2986 +               default: 
2987 +                       acl_type_count->n_illegal_obj++;
2988 +                       break;
2989 +               }
2990 +       }
2991 +}
2992 +
2993 +/* swap_acl_entries:  Swaps two ACL entries. 
2994 + *
2995 + * Inputs: aclp0, aclp1 - ACL entries to be swapped.
2996 + */
2997 +
2998 +static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
2999 +{
3000 +       struct acl temp_acl;
3001 +
3002 +       temp_acl.a_type = aclp0->a_type;
3003 +       temp_acl.a_id = aclp0->a_id;
3004 +       temp_acl.a_perm = aclp0->a_perm;
3005 +
3006 +       aclp0->a_type = aclp1->a_type;
3007 +       aclp0->a_id = aclp1->a_id;
3008 +       aclp0->a_perm = aclp1->a_perm;
3009 +
3010 +       aclp1->a_type = temp_acl.a_type;
3011 +       aclp1->a_id = temp_acl.a_id;
3012 +       aclp1->a_perm = temp_acl.a_perm;
3013 +}
3014 +
3015 +/* prohibited_duplicate_type
3016 + * Identifies if given ACL type can have duplicate entries or 
3017 + * not.
3018 + *
3019 + * Inputs: acl_type - ACL Type.
3020 + *
3021 + * Outputs: 
3022 + *
3023 + * Return.. 
3024 + *
3025 + * True - If the ACL type matches any of the prohibited types.
3026 + * False - If the ACL type doesn't match any of the prohibited types.
3027 + */ 
3028 +
3029 +static BOOL hpux_prohibited_duplicate_type(int acl_type)
3030 +{
3031 +       switch(acl_type) {
3032 +               case USER:
3033 +               case GROUP:
3034 +               case DEF_USER: 
3035 +               case DEF_GROUP:
3036 +                       return True;
3037 +               default:
3038 +                       return False;
3039 +       }
3040 +}
3041 +
3042 +/* get_needed_class_perm
3043 + * Returns the permissions of a ACL structure only if the ACL
3044 + * type matches one of the pre-determined types for computing 
3045 + * CLASS_OBJ permissions.
3046 + *
3047 + * Inputs: aclp - Pointer to ACL structure.
3048 + */
3049 +
3050 +static int hpux_get_needed_class_perm(struct acl *aclp)
3051 +{
3052 +       switch(aclp->a_type) {
3053 +               case USER: 
3054 +               case GROUP_OBJ: 
3055 +               case GROUP: 
3056 +               case DEF_USER_OBJ: 
3057 +               case DEF_USER:
3058 +               case DEF_GROUP_OBJ: 
3059 +               case DEF_GROUP:
3060 +               case DEF_CLASS_OBJ:
3061 +               case DEF_OTHER_OBJ: 
3062 +                       return aclp->a_perm;
3063 +               default: 
3064 +                       return 0;
3065 +       }
3066 +}
3067 +
3068 +/* acl_sort for HPUX.
3069 + * Sorts the array of ACL structures as per the description in
3070 + * aclsort man page. Refer to aclsort man page for more details
3071 + *
3072 + * Inputs:
3073 + *
3074 + * acl_count - Count of ACLs in the array of ACL structures.
3075 + * calclass  - If this is not zero, then we compute the CLASS_OBJ
3076 + *             permissions.
3077 + * aclp      - Array of ACL structures.
3078 + *
3079 + * Outputs:
3080 + *
3081 + * aclp     - Sorted array of ACL structures.
3082 + *
3083 + * Outputs:
3084 + *
3085 + * Returns 0 for success -1 for failure. Prints a message to the Samba
3086 + * debug log in case of failure.
3087 + */
3088 +
3089 +static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3090 +{
3091 +#if !defined(HAVE_HPUX_ACLSORT)
3092 +       /*
3093 +        * The aclsort() system call is availabe on the latest HPUX General
3094 +        * Patch Bundles. So for HPUX, we developed our version of acl_sort 
3095 +        * function. Because, we don't want to update to a new 
3096 +        * HPUX GR bundle just for aclsort() call.
3097 +        */
3098 +
3099 +       struct hpux_acl_types acl_obj_count;
3100 +       int n_class_obj_perm = 0;
3101 +       int i, j;
3102
3103 +       if(!acl_count) {
3104 +               DEBUG(10,("Zero acl count passed. Returning Success\n"));
3105 +               return 0;
3106 +       }
3107 +
3108 +       if(aclp == NULL) {
3109 +               DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3110 +               return -1;
3111 +       }
3112 +
3113 +       /* Count different types of ACLs in the ACLs array */
3114 +
3115 +       hpux_count_obj(acl_count, aclp, &acl_obj_count);
3116 +
3117 +       /* There should be only one entry each of type USER_OBJ, GROUP_OBJ, 
3118 +        * CLASS_OBJ and OTHER_OBJ 
3119 +        */
3120 +
3121 +       if( (acl_obj_count.n_user_obj  != 1) || 
3122 +               (acl_obj_count.n_group_obj != 1) || 
3123 +               (acl_obj_count.n_class_obj != 1) ||
3124 +               (acl_obj_count.n_other_obj != 1) 
3125 +       ) {
3126 +               DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3127 +USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3128 +               return -1;
3129 +       }
3130 +
3131 +       /* If any of the default objects are present, there should be only
3132 +        * one of them each.
3133 +        */
3134 +
3135 +       if( (acl_obj_count.n_def_user_obj  > 1) || (acl_obj_count.n_def_group_obj > 1) || 
3136 +                       (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
3137 +               DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3138 +or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3139 +               return -1;
3140 +       }
3141 +
3142 +       /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl 
3143 +        * structures.  
3144 +        *
3145 +        * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3146 +        * same ACL type, sort by ACL id.
3147 +        *
3148 +        * I am using the trival kind of sorting method here because, performance isn't 
3149 +        * really effected by the ACLs feature. More over there aren't going to be more
3150 +        * than 17 entries on HPUX. 
3151 +        */
3152 +
3153 +       for(i=0; i<acl_count;i++) {
3154 +               for (j=i+1; j<acl_count; j++) {
3155 +                       if( aclp[i].a_type > aclp[j].a_type ) {
3156 +                               /* ACL entries out of order, swap them */
3157 +
3158 +                               hpux_swap_acl_entries((aclp+i), (aclp+j));
3159 +
3160 +                       } else if ( aclp[i].a_type == aclp[j].a_type ) {
3161 +
3162 +                               /* ACL entries of same type, sort by id */
3163 +
3164 +                               if(aclp[i].a_id > aclp[j].a_id) {
3165 +                                       hpux_swap_acl_entries((aclp+i), (aclp+j));
3166 +                               } else if (aclp[i].a_id == aclp[j].a_id) {
3167 +                                       /* We have a duplicate entry. */
3168 +                                       if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
3169 +                                               DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3170 +                                                       aclp[i].a_type, aclp[i].a_id));
3171 +                                               return -1;
3172 +                                       }
3173 +                               }
3174 +
3175 +                       }
3176 +               }
3177 +       }
3178 +
3179 +       /* set the class obj permissions to the computed one. */
3180 +       if(calclass) {
3181 +               int n_class_obj_index = -1;
3182 +
3183 +               for(i=0;i<acl_count;i++) {
3184 +                       n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3185 +
3186 +                       if(aclp[i].a_type == CLASS_OBJ)
3187 +                               n_class_obj_index = i;
3188 +               }
3189 +               aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3190 +       }
3191 +
3192 +       return 0;
3193 +#else
3194 +       return aclsort(acl_count, calclass, aclp);
3195 +#endif
3196 +}
3197 +
3198 +/*
3199 + * sort the ACL and check it for validity
3200 + *
3201 + * if it's a minimal ACL with only 4 entries then we
3202 + * need to recalculate the mask permissions to make
3203 + * sure that they are the same as the GROUP_OBJ
3204 + * permissions as required by the UnixWare acl() system call.
3205 + *
3206 + * (note: since POSIX allows minimal ACLs which only contain
3207 + * 3 entries - ie there is no mask entry - we should, in theory,
3208 + * check for this and add a mask entry if necessary - however
3209 + * we "know" that the caller of this interface always specifies
3210 + * a mask so, in practice "this never happens" (tm) - if it *does*
3211 + * happen aclsort() will fail and return an error and someone will
3212 + * have to fix it ...)
3213 + */
3214 +
3215 +static int acl_sort(SMB_ACL_T acl_d)
3216 +{
3217 +       int fixmask = (acl_d->count <= 4);
3218 +
3219 +       if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
3220 +               errno = EINVAL;
3221 +               return -1;
3222 +       }
3223 +       return 0;
3224 +}
3225
3226 +int sys_acl_valid(SMB_ACL_T acl_d)
3227 +{
3228 +       return acl_sort(acl_d);
3229 +}
3230 +
3231 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3232 +{
3233 +       struct stat     s;
3234 +       struct acl      *acl_p;
3235 +       int             acl_count;
3236 +       struct acl      *acl_buf        = NULL;
3237 +       int             ret;
3238 +
3239 +       if(hpux_acl_call_presence() == False) {
3240 +               /* Looks like we don't have the acl() system call on HPUX. 
3241 +                * May be the system doesn't have the latest version of JFS.
3242 +                */
3243 +               errno=ENOSYS;
3244 +               return -1; 
3245 +       }
3246 +
3247 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3248 +               errno = EINVAL;
3249 +               return -1;
3250 +       }
3251 +
3252 +       if (acl_sort(acl_d) != 0) {
3253 +               return -1;
3254 +       }
3255 +
3256 +       acl_p           = &acl_d->acl[0];
3257 +       acl_count       = acl_d->count;
3258 +
3259 +       /*
3260 +        * if it's a directory there is extra work to do
3261 +        * since the acl() system call will replace both
3262 +        * the access ACLs and the default ACLs (if any)
3263 +        */
3264 +       if (stat(name, &s) != 0) {
3265 +               return -1;
3266 +       }
3267 +       if (S_ISDIR(s.st_mode)) {
3268 +               SMB_ACL_T       acc_acl;
3269 +               SMB_ACL_T       def_acl;
3270 +               SMB_ACL_T       tmp_acl;
3271 +               int             i;
3272 +
3273 +               if (type == SMB_ACL_TYPE_ACCESS) {
3274 +                       acc_acl = acl_d;
3275 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3276 +
3277 +               } else {
3278 +                       def_acl = acl_d;
3279 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3280 +               }
3281 +
3282 +               if (tmp_acl == NULL) {
3283 +                       return -1;
3284 +               }
3285 +
3286 +               /*
3287 +                * allocate a temporary buffer for the complete ACL
3288 +                */
3289 +               acl_count = acc_acl->count + def_acl->count;
3290 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
3291 +
3292 +               if (acl_buf == NULL) {
3293 +                       sys_acl_free_acl(tmp_acl);
3294 +                       errno = ENOMEM;
3295 +                       return -1;
3296 +               }
3297 +
3298 +               /*
3299 +                * copy the access control and default entries into the buffer
3300 +                */
3301 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
3302 +                       acc_acl->count * sizeof(acl_buf[0]));
3303 +
3304 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3305 +                       def_acl->count * sizeof(acl_buf[0]));
3306 +
3307 +               /*
3308 +                * set the ACL_DEFAULT flag on the default entries
3309 +                */
3310 +               for (i = acc_acl->count; i < acl_count; i++) {
3311 +                       acl_buf[i].a_type |= ACL_DEFAULT;
3312 +               }
3313 +
3314 +               sys_acl_free_acl(tmp_acl);
3315 +
3316 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
3317 +               errno = EINVAL;
3318 +               return -1;
3319 +       }
3320 +
3321 +       ret = acl(name, ACL_SET, acl_count, acl_p);
3322 +
3323 +       if (acl_buf) {
3324 +               free(acl_buf);
3325 +       }
3326 +
3327 +       return ret;
3328 +}
3329 +
3330 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3331 +{
3332 +       /*
3333 +        * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3334 +        */
3335 +
3336 +       files_struct *fsp = file_find_fd(fd);
3337 +
3338 +       if (fsp == NULL) {
3339 +               errno = EBADF;
3340 +               return NULL;
3341 +       }
3342 +
3343 +       if (acl_sort(acl_d) != 0) {
3344 +               return -1;
3345 +       }
3346 +
3347 +       /*
3348 +        * We know we're in the same conn context. So we
3349 +        * can use the relative path.
3350 +        */
3351 +
3352 +       return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
3353 +}
3354 +
3355 +int sys_acl_delete_def_file(const char *path)
3356 +{
3357 +       SMB_ACL_T       acl_d;
3358 +       int             ret;
3359 +
3360 +       /*
3361 +        * fetching the access ACL and rewriting it has
3362 +        * the effect of deleting the default ACL
3363 +        */
3364 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3365 +               return -1;
3366 +       }
3367 +
3368 +       ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
3369 +
3370 +       sys_acl_free_acl(acl_d);
3371 +       
3372 +       return ret;
3373 +}
3374 +
3375 +int sys_acl_free_text(char *text)
3376 +{
3377 +       free(text);
3378 +       return 0;
3379 +}
3380 +
3381 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
3382 +{
3383 +       free(acl_d);
3384 +       return 0;
3385 +}
3386 +
3387 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3388 +{
3389 +       return 0;
3390 +}
3391 +
3392 +#elif defined(HAVE_IRIX_ACLS)
3393 +
3394 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3395 +{
3396 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3397 +               errno = EINVAL;
3398 +               return -1;
3399 +       }
3400 +
3401 +       if (entry_p == NULL) {
3402 +               errno = EINVAL;
3403 +               return -1;
3404 +       }
3405 +
3406 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
3407 +               acl_d->next = 0;
3408 +       }
3409 +
3410 +       if (acl_d->next < 0) {
3411 +               errno = EINVAL;
3412 +               return -1;
3413 +       }
3414 +
3415 +       if (acl_d->next >= acl_d->aclp->acl_cnt) {
3416 +               return 0;
3417 +       }
3418 +
3419 +       *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
3420 +
3421 +       return 1;
3422 +}
3423 +
3424 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
3425 +{
3426 +       *type_p = entry_d->ae_tag;
3427 +
3428 +       return 0;
3429 +}
3430 +
3431 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3432 +{
3433 +       *permset_p = entry_d;
3434 +
3435 +       return 0;
3436 +}
3437 +
3438 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3439 +{
3440 +       if (entry_d->ae_tag != SMB_ACL_USER
3441 +           && entry_d->ae_tag != SMB_ACL_GROUP) {
3442 +               errno = EINVAL;
3443 +               return NULL;
3444 +       }
3445 +
3446 +       return &entry_d->ae_id;
3447 +}
3448 +
3449 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3450 +{
3451 +       SMB_ACL_T       a;
3452 +
3453 +       if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3454 +               errno = ENOMEM;
3455 +               return NULL;
3456 +       }
3457 +       if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
3458 +               SAFE_FREE(a);
3459 +               return NULL;
3460 +       }
3461 +       a->next = -1;
3462 +       a->freeaclp = True;
3463 +       return a;
3464 +}
3465 +
3466 +SMB_ACL_T sys_acl_get_fd(int fd)
3467 +{
3468 +       SMB_ACL_T       a;
3469 +
3470 +       if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3471 +               errno = ENOMEM;
3472 +               return NULL;
3473 +       }
3474 +       if ((a->aclp = acl_get_fd(fd)) == NULL) {
3475 +               SAFE_FREE(a);
3476 +               return NULL;
3477 +       }
3478 +       a->next = -1;
3479 +       a->freeaclp = True;
3480 +       return a;
3481 +}
3482 +
3483 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3484 +{
3485 +       permset_d->ae_perm = 0;
3486 +
3487 +       return 0;
3488 +}
3489 +
3490 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3491 +{
3492 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3493 +           && perm != SMB_ACL_EXECUTE) {
3494 +               errno = EINVAL;
3495 +               return -1;
3496 +       }
3497 +
3498 +       if (permset_d == NULL) {
3499 +               errno = EINVAL;
3500 +               return -1;
3501 +       }
3502 +
3503 +       permset_d->ae_perm |= perm;
3504 +
3505 +       return 0;
3506 +}
3507 +
3508 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3509 +{
3510 +       return permset_d->ae_perm & perm;
3511 +}
3512 +
3513 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3514 +{
3515 +       return acl_to_text(acl_d->aclp, len_p);
3516 +}
3517 +
3518 +SMB_ACL_T sys_acl_init(int count)
3519 +{
3520 +       SMB_ACL_T       a;
3521 +
3522 +       if (count < 0) {
3523 +               errno = EINVAL;
3524 +               return NULL;
3525 +       }
3526 +
3527 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
3528 +               errno = ENOMEM;
3529 +               return NULL;
3530 +       }
3531 +
3532 +       a->next = -1;
3533 +       a->freeaclp = False;
3534 +       a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
3535 +       a->aclp->acl_cnt = 0;
3536 +
3537 +       return a;
3538 +}
3539 +
3540 +
3541 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3542 +{
3543 +       SMB_ACL_T       acl_d;
3544 +       SMB_ACL_ENTRY_T entry_d;
3545 +
3546 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3547 +               errno = EINVAL;
3548 +               return -1;
3549 +       }
3550 +
3551 +       if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
3552 +               errno = ENOSPC;
3553 +               return -1;
3554 +       }
3555 +
3556 +       entry_d         = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
3557 +       entry_d->ae_tag = 0;
3558 +       entry_d->ae_id  = 0;
3559 +       entry_d->ae_perm        = 0;
3560 +       *entry_p        = entry_d;
3561 +
3562 +       return 0;
3563 +}
3564 +
3565 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3566 +{
3567 +       switch (tag_type) {
3568 +               case SMB_ACL_USER:
3569 +               case SMB_ACL_USER_OBJ:
3570 +               case SMB_ACL_GROUP:
3571 +               case SMB_ACL_GROUP_OBJ:
3572 +               case SMB_ACL_OTHER:
3573 +               case SMB_ACL_MASK:
3574 +                       entry_d->ae_tag = tag_type;
3575 +                       break;
3576 +               default:
3577 +                       errno = EINVAL;
3578 +                       return -1;
3579 +       }
3580 +
3581 +       return 0;
3582 +}
3583 +
3584 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3585 +{
3586 +       if (entry_d->ae_tag != SMB_ACL_GROUP
3587 +           && entry_d->ae_tag != SMB_ACL_USER) {
3588 +               errno = EINVAL;
3589 +               return -1;
3590 +       }
3591 +
3592 +       entry_d->ae_id = *((id_t *)qual_p);
3593 +
3594 +       return 0;
3595 +}
3596 +
3597 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3598 +{
3599 +       if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3600 +               return EINVAL;
3601 +       }
3602 +
3603 +       entry_d->ae_perm = permset_d->ae_perm;
3604 +
3605 +       return 0;
3606 +}
3607 +
3608 +int sys_acl_valid(SMB_ACL_T acl_d)
3609 +{
3610 +       return acl_valid(acl_d->aclp);
3611 +}
3612 +
3613 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3614 +{
3615 +       return acl_set_file(name, type, acl_d->aclp);
3616 +}
3617 +
3618 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3619 +{
3620 +       return acl_set_fd(fd, acl_d->aclp);
3621 +}
3622 +
3623 +int sys_acl_delete_def_file(const char *name)
3624 +{
3625 +       return acl_delete_def_file(name);
3626 +}
3627 +
3628 +int sys_acl_free_text(char *text)
3629 +{
3630 +       return acl_free(text);
3631 +}
3632 +
3633 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
3634 +{
3635 +       if (acl_d->freeaclp) {
3636 +               acl_free(acl_d->aclp);
3637 +       }
3638 +       acl_free(acl_d);
3639 +       return 0;
3640 +}
3641 +
3642 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3643 +{
3644 +       return 0;
3645 +}
3646 +
3647 +#elif defined(HAVE_AIX_ACLS)
3648 +
3649 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
3650 +
3651 +int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3652 +{
3653 +       struct acl_entry_link *link;
3654 +       struct new_acl_entry *entry;
3655 +       int keep_going;
3656 +
3657 +       DEBUG(10,("This is the count: %d\n",theacl->count));
3658 +
3659 +       /* Check if count was previously set to -1. *
3660 +        * If it was, that means we reached the end *
3661 +        * of the acl last time.                    */
3662 +       if(theacl->count == -1)
3663 +               return(0);
3664 +
3665 +       link = theacl;
3666 +       /* To get to the next acl, traverse linked list until index *
3667 +        * of acl matches the count we are keeping.  This count is  *
3668 +        * incremented each time we return an acl entry.            */
3669 +
3670 +       for(keep_going = 0; keep_going < theacl->count; keep_going++)
3671 +               link = link->nextp;
3672 +
3673 +       entry = *entry_p =  link->entryp;
3674 +
3675 +       DEBUG(10,("*entry_p is %d\n",entry_p));
3676 +       DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
3677 +
3678 +       /* Increment count */
3679 +       theacl->count++;
3680 +       if(link->nextp == NULL)
3681 +               theacl->count = -1;
3682 +
3683 +       return(1);
3684 +}
3685 +
3686 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
3687 +{
3688 +       /* Initialize tag type */
3689 +
3690 +       *tag_type_p = -1;
3691 +       DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
3692 +
3693 +       /* Depending on what type of entry we have, *
3694 +        * return tag type.                         */
3695 +       switch(entry_d->ace_id->id_type) {
3696 +       case ACEID_USER:
3697 +               *tag_type_p = SMB_ACL_USER;
3698 +               break;
3699 +       case ACEID_GROUP:
3700 +               *tag_type_p = SMB_ACL_GROUP;
3701 +               break;
3702 +
3703 +       case SMB_ACL_USER_OBJ:
3704 +       case SMB_ACL_GROUP_OBJ:
3705 +       case SMB_ACL_OTHER:
3706 +               *tag_type_p = entry_d->ace_id->id_type;
3707 +               break;
3708
3709 +       default:
3710 +               return(-1);
3711 +       }
3712 +
3713 +       return(0);
3714 +}
3715 +
3716 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3717 +{
3718 +       DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
3719 +       *permset_p = &entry_d->ace_access;
3720 +       DEBUG(10,("**permset_p is %d\n",**permset_p));
3721 +       if(!(**permset_p & S_IXUSR) &&
3722 +               !(**permset_p & S_IWUSR) &&
3723 +               !(**permset_p & S_IRUSR) &&
3724 +               (**permset_p != 0))
3725 +                       return(-1);
3726 +
3727 +       DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
3728 +       return(0);
3729 +}
3730 +
3731 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
3732 +{
3733 +       return(entry_d->ace_id->id_data);
3734 +}
3735 +
3736 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
3737 +{
3738 +       struct acl *file_acl = (struct acl *)NULL;
3739 +       struct acl_entry *acl_entry;
3740 +       struct new_acl_entry *new_acl_entry;
3741 +       struct ace_id *idp;
3742 +       struct acl_entry_link *acl_entry_link;
3743 +       struct acl_entry_link *acl_entry_link_head;
3744 +       int i;
3745 +       int rc = 0;
3746 +       uid_t user_id;
3747 +
3748 +       /* AIX has no DEFAULT */
3749 +       if  ( type == SMB_ACL_TYPE_DEFAULT )
3750 +               return NULL;
3751 +
3752 +       /* Get the acl using statacl */
3753
3754 +       DEBUG(10,("Entering sys_acl_get_file\n"));
3755 +       DEBUG(10,("path_p is %s\n",path_p));
3756 +
3757 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
3758
3759 +       if(file_acl == NULL) {
3760 +               errno=ENOMEM;
3761 +               DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
3762 +               return(NULL);
3763 +       }
3764 +
3765 +       memset(file_acl,0,BUFSIZ);
3766 +
3767 +       rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
3768 +       if(rc == -1) {
3769 +               DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
3770 +               SAFE_FREE(file_acl);
3771 +               return(NULL);
3772 +       }
3773 +
3774 +       DEBUG(10,("Got facl and returned it\n"));
3775 +
3776 +       /* Point to the first acl entry in the acl */
3777 +       acl_entry =  file_acl->acl_ext;
3778 +
3779 +       /* Begin setting up the head of the linked list *
3780 +        * that will be used for the storing the acl    *
3781 +        * in a way that is useful for the posix_acls.c *
3782 +        * code.                                          */
3783 +
3784 +       acl_entry_link_head = acl_entry_link = sys_acl_init(0);
3785 +       if(acl_entry_link_head == NULL)
3786 +               return(NULL);
3787 +
3788 +       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3789 +       if(acl_entry_link->entryp == NULL) {
3790 +               SAFE_FREE(file_acl);
3791 +               errno = ENOMEM;
3792 +               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3793 +               return(NULL);
3794 +       }
3795 +
3796 +       DEBUG(10,("acl_entry is %d\n",acl_entry));
3797 +       DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
3798 +
3799 +       /* Check if the extended acl bit is on.   *
3800 +        * If it isn't, do not show the           *
3801 +        * contents of the acl since AIX intends *
3802 +        * the extended info to remain unused     */
3803 +
3804 +       if(file_acl->acl_mode & S_IXACL){
3805 +               /* while we are not pointing to the very end */
3806 +               while(acl_entry < acl_last(file_acl)) {
3807 +                       /* before we malloc anything, make sure this is  */
3808 +                       /* a valid acl entry and one that we want to map */
3809 +                       idp = id_nxt(acl_entry->ace_id);
3810 +                       if((acl_entry->ace_type == ACC_SPECIFY ||
3811 +                               (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
3812 +                                       acl_entry = acl_nxt(acl_entry);
3813 +                                       continue;
3814 +                       }
3815 +
3816 +                       idp = acl_entry->ace_id;
3817 +
3818 +                       /* Check if this is the first entry in the linked list. *
3819 +                        * The first entry needs to keep prevp pointing to NULL *
3820 +                        * and already has entryp allocated.                  */
3821 +
3822 +                       if(acl_entry_link_head->count != 0) {
3823 +                               acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
3824 +
3825 +                               if(acl_entry_link->nextp == NULL) {
3826 +                                       SAFE_FREE(file_acl);
3827 +                                       errno = ENOMEM;
3828 +                                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3829 +                                       return(NULL);
3830 +                               }
3831 +
3832 +                               acl_entry_link->nextp->prevp = acl_entry_link;
3833 +                               acl_entry_link = acl_entry_link->nextp;
3834 +                               acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3835 +                               if(acl_entry_link->entryp == NULL) {
3836 +                                       SAFE_FREE(file_acl);
3837 +                                       errno = ENOMEM;
3838 +                                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3839 +                                       return(NULL);
3840 +                               }
3841 +                               acl_entry_link->nextp = NULL;
3842 +                       }
3843 +
3844 +                       acl_entry_link->entryp->ace_len = acl_entry->ace_len;
3845 +
3846 +                       /* Don't really need this since all types are going *
3847 +                        * to be specified but, it's better than leaving it 0 */
3848 +
3849 +                       acl_entry_link->entryp->ace_type = acl_entry->ace_type;
3850
3851 +                       acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3852
3853 +                       memcpy(acl_entry_link->entryp->ace_id,idp,sizeof(struct ace_id));
3854 +
3855 +                       /* The access in the acl entries must be left shifted by *
3856 +                        * three bites, because they will ultimately be compared *
3857 +                        * to S_IRUSR, S_IWUSR, and S_IXUSR.                  */
3858 +
3859 +                       switch(acl_entry->ace_type){
3860 +                       case ACC_PERMIT:
3861 +                       case ACC_SPECIFY:
3862 +                               acl_entry_link->entryp->ace_access = acl_entry->ace_access;
3863 +                               acl_entry_link->entryp->ace_access <<= 6;
3864 +                               acl_entry_link_head->count++;
3865 +                               break;
3866 +                       case ACC_DENY:
3867 +                               /* Since there is no way to return a DENY acl entry *
3868 +                                * change to PERMIT and then shift.                 */
3869 +                               DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
3870 +                               acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
3871 +                               DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
3872 +                               acl_entry_link->entryp->ace_access <<= 6;
3873 +                               acl_entry_link_head->count++;
3874 +                               break;
3875 +                       default:
3876 +                               return(0);
3877 +                       }
3878 +
3879 +                       DEBUG(10,("acl_entry = %d\n",acl_entry));
3880 +                       DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
3881
3882 +                       acl_entry = acl_nxt(acl_entry);
3883 +               }
3884 +       } /* end of if enabled */
3885 +
3886 +       /* Since owner, group, other acl entries are not *
3887 +        * part of the acl entries in an acl, they must  *
3888 +        * be dummied up to become part of the list.     */
3889 +
3890 +       for( i = 1; i < 4; i++) {
3891 +               DEBUG(10,("i is %d\n",i));
3892 +               if(acl_entry_link_head->count != 0) {
3893 +                       acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
3894 +                       if(acl_entry_link->nextp == NULL) {
3895 +                               SAFE_FREE(file_acl);
3896 +                               errno = ENOMEM;
3897 +                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3898 +                               return(NULL);
3899 +                       }
3900 +
3901 +                       acl_entry_link->nextp->prevp = acl_entry_link;
3902 +                       acl_entry_link = acl_entry_link->nextp;
3903 +                       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
3904 +                       if(acl_entry_link->entryp == NULL) {
3905 +                               SAFE_FREE(file_acl);
3906 +                               errno = ENOMEM;
3907 +                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
3908 +                               return(NULL);
3909 +                       }
3910 +               }
3911 +
3912 +               acl_entry_link->nextp = NULL;
3913 +
3914 +               new_acl_entry = acl_entry_link->entryp;
3915 +               idp = new_acl_entry->ace_id;
3916 +
3917 +               new_acl_entry->ace_len = sizeof(struct acl_entry);
3918 +               new_acl_entry->ace_type = ACC_PERMIT;
3919 +               idp->id_len = sizeof(struct ace_id);
3920 +               DEBUG(10,("idp->id_len = %d\n",idp->id_len));
3921 +               memset(idp->id_data,0,sizeof(uid_t));
3922 +
3923 +               switch(i) {
3924 +               case 2:
3925 +                       new_acl_entry->ace_access = file_acl->g_access << 6;
3926 +                       idp->id_type = SMB_ACL_GROUP_OBJ;
3927 +                       break;
3928 +
3929 +               case 3:
3930 +                       new_acl_entry->ace_access = file_acl->o_access << 6;
3931 +                       idp->id_type = SMB_ACL_OTHER;
3932 +                       break;
3933
3934 +               case 1:
3935 +                       new_acl_entry->ace_access = file_acl->u_access << 6;
3936 +                       idp->id_type = SMB_ACL_USER_OBJ;
3937 +                       break;
3938
3939 +               default:
3940 +                       return(NULL);
3941 +
3942 +               }
3943 +
3944 +               acl_entry_link_head->count++;
3945 +               DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
3946 +       }
3947 +
3948 +       acl_entry_link_head->count = 0;
3949 +       SAFE_FREE(file_acl);
3950 +
3951 +       return(acl_entry_link_head);
3952 +}
3953 +
3954 +SMB_ACL_T sys_acl_get_fd(int fd)
3955 +{
3956 +       struct acl *file_acl = (struct acl *)NULL;
3957 +       struct acl_entry *acl_entry;
3958 +       struct new_acl_entry *new_acl_entry;
3959 +       struct ace_id *idp;
3960 +       struct acl_entry_link *acl_entry_link;
3961 +       struct acl_entry_link *acl_entry_link_head;
3962 +       int i;
3963 +       int rc = 0;
3964 +       uid_t user_id;
3965 +
3966 +       /* Get the acl using fstatacl */
3967 +   
3968 +       DEBUG(10,("Entering sys_acl_get_fd\n"));
3969 +       DEBUG(10,("fd is %d\n",fd));
3970 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
3971 +
3972 +       if(file_acl == NULL) {
3973 +               errno=ENOMEM;
3974 +               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
3975 +               return(NULL);
3976 +       }
3977 +
3978 +       memset(file_acl,0,BUFSIZ);
3979 +
3980 +       rc = fstatacl(fd,0,file_acl,BUFSIZ);
3981 +       if(rc == -1) {
3982 +               DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
3983 +               SAFE_FREE(file_acl);
3984 +               return(NULL);
3985 +       }
3986 +
3987 +       DEBUG(10,("Got facl and returned it\n"));
3988 +
3989 +       /* Point to the first acl entry in the acl */
3990 +
3991 +       acl_entry =  file_acl->acl_ext;
3992 +       /* Begin setting up the head of the linked list *
3993 +        * that will be used for the storing the acl    *
3994 +        * in a way that is useful for the posix_acls.c *
3995 +        * code.                                        */
3996 +
3997 +       acl_entry_link_head = acl_entry_link = sys_acl_init(0);
3998 +       if(acl_entry_link_head == NULL){
3999 +               SAFE_FREE(file_acl);
4000 +               return(NULL);
4001 +       }
4002 +
4003 +       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4004 +
4005 +       if(acl_entry_link->entryp == NULL) {
4006 +               errno = ENOMEM;
4007 +               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4008 +               SAFE_FREE(file_acl);
4009 +               return(NULL);
4010 +       }
4011 +
4012 +       DEBUG(10,("acl_entry is %d\n",acl_entry));
4013 +       DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4014
4015 +       /* Check if the extended acl bit is on.   *
4016 +        * If it isn't, do not show the           *
4017 +        * contents of the acl since AIX intends  *
4018 +        * the extended info to remain unused     */
4019
4020 +       if(file_acl->acl_mode & S_IXACL){
4021 +               /* while we are not pointing to the very end */
4022 +               while(acl_entry < acl_last(file_acl)) {
4023 +                       /* before we malloc anything, make sure this is  */
4024 +                       /* a valid acl entry and one that we want to map */
4025 +
4026 +                       idp = id_nxt(acl_entry->ace_id);
4027 +                       if((acl_entry->ace_type == ACC_SPECIFY ||
4028 +                               (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
4029 +                                       acl_entry = acl_nxt(acl_entry);
4030 +                                       continue;
4031 +                       }
4032 +
4033 +                       idp = acl_entry->ace_id;
4034
4035 +                       /* Check if this is the first entry in the linked list. *
4036 +                        * The first entry needs to keep prevp pointing to NULL *
4037 +                        * and already has entryp allocated.                 */
4038 +
4039 +                       if(acl_entry_link_head->count != 0) {
4040 +                               acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4041 +                               if(acl_entry_link->nextp == NULL) {
4042 +                                       errno = ENOMEM;
4043 +                                       DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4044 +                                       SAFE_FREE(file_acl);
4045 +                                       return(NULL);
4046 +                               }
4047 +                               acl_entry_link->nextp->prevp = acl_entry_link;
4048 +                               acl_entry_link = acl_entry_link->nextp;
4049 +                               acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4050 +                               if(acl_entry_link->entryp == NULL) {
4051 +                                       errno = ENOMEM;
4052 +                                       DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4053 +                                       SAFE_FREE(file_acl);
4054 +                                       return(NULL);
4055 +                               }
4056 +
4057 +                               acl_entry_link->nextp = NULL;
4058 +                       }
4059 +
4060 +                       acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4061 +
4062 +                       /* Don't really need this since all types are going *
4063 +                        * to be specified but, it's better than leaving it 0 */
4064 +
4065 +                       acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4066 +                       acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4067 +
4068 +                       memcpy(acl_entry_link->entryp->ace_id, idp, sizeof(struct ace_id));
4069 +
4070 +                       /* The access in the acl entries must be left shifted by *
4071 +                        * three bites, because they will ultimately be compared *
4072 +                        * to S_IRUSR, S_IWUSR, and S_IXUSR.                  */
4073 +
4074 +                       switch(acl_entry->ace_type){
4075 +                       case ACC_PERMIT:
4076 +                       case ACC_SPECIFY:
4077 +                               acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4078 +                               acl_entry_link->entryp->ace_access <<= 6;
4079 +                               acl_entry_link_head->count++;
4080 +                               break;
4081 +                       case ACC_DENY:
4082 +                               /* Since there is no way to return a DENY acl entry *
4083 +                                * change to PERMIT and then shift.                 */
4084 +                               DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4085 +                               acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4086 +                               DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4087 +                               acl_entry_link->entryp->ace_access <<= 6;
4088 +                               acl_entry_link_head->count++;
4089 +                               break;
4090 +                       default:
4091 +                               return(0);
4092 +                       }
4093 +
4094 +                       DEBUG(10,("acl_entry = %d\n",acl_entry));
4095 +                       DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4096
4097 +                       acl_entry = acl_nxt(acl_entry);
4098 +               }
4099 +       } /* end of if enabled */
4100 +
4101 +       /* Since owner, group, other acl entries are not *
4102 +        * part of the acl entries in an acl, they must  *
4103 +        * be dummied up to become part of the list.     */
4104 +
4105 +       for( i = 1; i < 4; i++) {
4106 +               DEBUG(10,("i is %d\n",i));
4107 +               if(acl_entry_link_head->count != 0){
4108 +                       acl_entry_link->nextp = SMB_MALLOC_P(struct acl_entry_link);
4109 +                       if(acl_entry_link->nextp == NULL) {
4110 +                               errno = ENOMEM;
4111 +                               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4112 +                               SAFE_FREE(file_acl);
4113 +                               return(NULL);
4114 +                       }
4115 +
4116 +                       acl_entry_link->nextp->prevp = acl_entry_link;
4117 +                       acl_entry_link = acl_entry_link->nextp;
4118 +                       acl_entry_link->entryp = SMB_MALLOC_P(struct new_acl_entry);
4119 +
4120 +                       if(acl_entry_link->entryp == NULL) {
4121 +                               SAFE_FREE(file_acl);
4122 +                               errno = ENOMEM;
4123 +                               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4124 +                               return(NULL);
4125 +                       }
4126 +               }
4127 +
4128 +               acl_entry_link->nextp = NULL;
4129
4130 +               new_acl_entry = acl_entry_link->entryp;
4131 +               idp = new_acl_entry->ace_id;
4132
4133 +               new_acl_entry->ace_len = sizeof(struct acl_entry);
4134 +               new_acl_entry->ace_type = ACC_PERMIT;
4135 +               idp->id_len = sizeof(struct ace_id);
4136 +               DEBUG(10,("idp->id_len = %d\n",idp->id_len));
4137 +               memset(idp->id_data,0,sizeof(uid_t));
4138
4139 +               switch(i) {
4140 +               case 2:
4141 +                       new_acl_entry->ace_access = file_acl->g_access << 6;
4142 +                       idp->id_type = SMB_ACL_GROUP_OBJ;
4143 +                       break;
4144
4145 +               case 3:
4146 +                       new_acl_entry->ace_access = file_acl->o_access << 6;
4147 +                       idp->id_type = SMB_ACL_OTHER;
4148 +                       break;
4149
4150 +               case 1:
4151 +                       new_acl_entry->ace_access = file_acl->u_access << 6;
4152 +                       idp->id_type = SMB_ACL_USER_OBJ;
4153 +                       break;
4154
4155 +               default:
4156 +                       return(NULL);
4157 +               }
4158
4159 +               acl_entry_link_head->count++;
4160 +               DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4161 +       }
4162 +
4163 +       acl_entry_link_head->count = 0;
4164 +       SAFE_FREE(file_acl);
4165
4166 +       return(acl_entry_link_head);
4167 +}
4168 +
4169 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
4170 +{
4171 +       *permset = *permset & ~0777;
4172 +       return(0);
4173 +}
4174 +
4175 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4176 +{
4177 +       if((perm != 0) &&
4178 +                       (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
4179 +               return(-1);
4180 +
4181 +       *permset |= perm;
4182 +       DEBUG(10,("This is the permset now: %d\n",*permset));
4183 +       return(0);
4184 +}
4185 +
4186 +char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
4187 +{
4188 +       return(NULL);
4189 +}
4190 +
4191 +SMB_ACL_T sys_acl_init( int count)
4192 +{
4193 +       struct acl_entry_link *theacl = NULL;
4194
4195 +       DEBUG(10,("Entering sys_acl_init\n"));
4196 +
4197 +       theacl = SMB_MALLOC_P(struct acl_entry_link);
4198 +       if(theacl == NULL) {
4199 +               errno = ENOMEM;
4200 +               DEBUG(0,("Error in sys_acl_init is %d\n",errno));
4201 +               return(NULL);
4202 +       }
4203 +
4204 +       theacl->count = 0;
4205 +       theacl->nextp = NULL;
4206 +       theacl->prevp = NULL;
4207 +       theacl->entryp = NULL;
4208 +       DEBUG(10,("Exiting sys_acl_init\n"));
4209 +       return(theacl);
4210 +}
4211 +
4212 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
4213 +{
4214 +       struct acl_entry_link *theacl;
4215 +       struct acl_entry_link *acl_entryp;
4216 +       struct acl_entry_link *temp_entry;
4217 +       int counting;
4218 +
4219 +       DEBUG(10,("Entering the sys_acl_create_entry\n"));
4220 +
4221 +       theacl = acl_entryp = *pacl;
4222 +
4223 +       /* Get to the end of the acl before adding entry */
4224 +
4225 +       for(counting=0; counting < theacl->count; counting++){
4226 +               DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4227 +               temp_entry = acl_entryp;
4228 +               acl_entryp = acl_entryp->nextp;
4229 +       }
4230 +
4231 +       if(theacl->count != 0){
4232 +               temp_entry->nextp = acl_entryp = SMB_MALLOC_P(struct acl_entry_link);
4233 +               if(acl_entryp == NULL) {
4234 +                       errno = ENOMEM;
4235 +                       DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4236 +                       return(-1);
4237 +               }
4238 +
4239 +               DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4240 +               acl_entryp->prevp = temp_entry;
4241 +               DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
4242 +       }
4243 +
4244 +       *pentry = acl_entryp->entryp = SMB_MALLOC_P(struct new_acl_entry);
4245 +       if(*pentry == NULL) {
4246 +               errno = ENOMEM;
4247 +               DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4248 +               return(-1);
4249 +       }
4250 +
4251 +       memset(*pentry,0,sizeof(struct new_acl_entry));
4252 +       acl_entryp->entryp->ace_len = sizeof(struct acl_entry);
4253 +       acl_entryp->entryp->ace_type = ACC_PERMIT;
4254 +       acl_entryp->entryp->ace_id->id_len = sizeof(struct ace_id);
4255 +       acl_entryp->nextp = NULL;
4256 +       theacl->count++;
4257 +       DEBUG(10,("Exiting sys_acl_create_entry\n"));
4258 +       return(0);
4259 +}
4260 +
4261 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
4262 +{
4263 +       DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
4264 +       entry->ace_id->id_type = tagtype;
4265 +       DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
4266 +       DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
4267 +}
4268 +
4269 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
4270 +{
4271 +       DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
4272 +       memcpy(entry->ace_id->id_data,qual,sizeof(uid_t));
4273 +       DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
4274 +       return(0);
4275 +}
4276 +
4277 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
4278 +{
4279 +       DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
4280 +       if(!(*permset & S_IXUSR) &&
4281 +               !(*permset & S_IWUSR) &&
4282 +               !(*permset & S_IRUSR) &&
4283 +               (*permset != 0))
4284 +                       return(-1);
4285 +
4286 +       entry->ace_access = *permset;
4287 +       DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
4288 +       DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
4289 +       return(0);
4290 +}
4291 +
4292 +int sys_acl_valid( SMB_ACL_T theacl )
4293 +{
4294 +       int user_obj = 0;
4295 +       int group_obj = 0;
4296 +       int other_obj = 0;
4297 +       struct acl_entry_link *acl_entry;
4298 +
4299 +       for(acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
4300 +               user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
4301 +               group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
4302 +               other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
4303 +       }
4304 +
4305 +       DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
4306
4307 +       if(user_obj != 1 || group_obj != 1 || other_obj != 1)
4308 +               return(-1); 
4309 +
4310 +       return(0);
4311 +}
4312 +
4313 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
4314 +{
4315 +       struct acl_entry_link *acl_entry_link = NULL;
4316 +       struct acl *file_acl = NULL;
4317 +       struct acl *file_acl_temp = NULL;
4318 +       struct acl_entry *acl_entry = NULL;
4319 +       struct ace_id *ace_id = NULL;
4320 +       uint id_type;
4321 +       uint ace_access;
4322 +       uint user_id;
4323 +       uint acl_length;
4324 +       uint rc;
4325 +
4326 +       DEBUG(10,("Entering sys_acl_set_file\n"));
4327 +       DEBUG(10,("File name is %s\n",name));
4328
4329 +       /* AIX has no default ACL */
4330 +       if(acltype == SMB_ACL_TYPE_DEFAULT)
4331 +               return(0);
4332 +
4333 +       acl_length = BUFSIZ;
4334 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4335 +
4336 +       if(file_acl == NULL) {
4337 +               errno = ENOMEM;
4338 +               DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4339 +               return(-1);
4340 +       }
4341 +
4342 +       memset(file_acl,0,BUFSIZ);
4343 +
4344 +       file_acl->acl_len = ACL_SIZ;
4345 +       file_acl->acl_mode = S_IXACL;
4346 +
4347 +       for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4348 +               acl_entry_link->entryp->ace_access >>= 6;
4349 +               id_type = acl_entry_link->entryp->ace_id->id_type;
4350 +
4351 +               switch(id_type) {
4352 +               case SMB_ACL_USER_OBJ:
4353 +                       file_acl->u_access = acl_entry_link->entryp->ace_access;
4354 +                       continue;
4355 +               case SMB_ACL_GROUP_OBJ:
4356 +                       file_acl->g_access = acl_entry_link->entryp->ace_access;
4357 +                       continue;
4358 +               case SMB_ACL_OTHER:
4359 +                       file_acl->o_access = acl_entry_link->entryp->ace_access;
4360 +                       continue;
4361 +               case SMB_ACL_MASK:
4362 +                       continue;
4363 +               }
4364 +
4365 +               if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4366 +                       acl_length += sizeof(struct acl_entry);
4367 +                       file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
4368 +                       if(file_acl_temp == NULL) {
4369 +                               SAFE_FREE(file_acl);
4370 +                               errno = ENOMEM;
4371 +                               DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4372 +                               return(-1);
4373 +                       }  
4374 +
4375 +                       memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4376 +                       SAFE_FREE(file_acl);
4377 +                       file_acl = file_acl_temp;
4378 +               }
4379 +
4380 +               acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4381 +               file_acl->acl_len += sizeof(struct acl_entry);
4382 +               acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4383 +               acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4384
4385 +               /* In order to use this, we'll need to wait until we can get denies */
4386 +               /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4387 +               acl_entry->ace_type = ACC_SPECIFY; */
4388 +
4389 +               acl_entry->ace_type = ACC_SPECIFY;
4390
4391 +               ace_id = acl_entry->ace_id;
4392
4393 +               ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4394 +               DEBUG(10,("The id type is %d\n",ace_id->id_type));
4395 +               ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4396 +               memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4397 +               memcpy(acl_entry->ace_id->id_data, &user_id, sizeof(uid_t));
4398 +       }
4399 +
4400 +       rc = chacl(name,file_acl,file_acl->acl_len);
4401 +       DEBUG(10,("errno is %d\n",errno));
4402 +       DEBUG(10,("return code is %d\n",rc));
4403 +       SAFE_FREE(file_acl);
4404 +       DEBUG(10,("Exiting the sys_acl_set_file\n"));
4405 +       return(rc);
4406 +}
4407 +
4408 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
4409 +{
4410 +       struct acl_entry_link *acl_entry_link = NULL;
4411 +       struct acl *file_acl = NULL;
4412 +       struct acl *file_acl_temp = NULL;
4413 +       struct acl_entry *acl_entry = NULL;
4414 +       struct ace_id *ace_id = NULL;
4415 +       uint id_type;
4416 +       uint user_id;
4417 +       uint acl_length;
4418 +       uint rc;
4419
4420 +       DEBUG(10,("Entering sys_acl_set_fd\n"));
4421 +       acl_length = BUFSIZ;
4422 +       file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
4423 +
4424 +       if(file_acl == NULL) {
4425 +               errno = ENOMEM;
4426 +               DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4427 +               return(-1);
4428 +       }
4429 +
4430 +       memset(file_acl,0,BUFSIZ);
4431
4432 +       file_acl->acl_len = ACL_SIZ;
4433 +       file_acl->acl_mode = S_IXACL;
4434 +
4435 +       for(acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4436 +               acl_entry_link->entryp->ace_access >>= 6;
4437 +               id_type = acl_entry_link->entryp->ace_id->id_type;
4438 +               DEBUG(10,("The id_type is %d\n",id_type));
4439 +
4440 +               switch(id_type) {
4441 +               case SMB_ACL_USER_OBJ:
4442 +                       file_acl->u_access = acl_entry_link->entryp->ace_access;
4443 +                       continue;
4444 +               case SMB_ACL_GROUP_OBJ:
4445 +                       file_acl->g_access = acl_entry_link->entryp->ace_access;
4446 +                       continue;
4447 +               case SMB_ACL_OTHER:
4448 +                       file_acl->o_access = acl_entry_link->entryp->ace_access;
4449 +                       continue;
4450 +               case SMB_ACL_MASK:
4451 +                       continue;
4452 +               }
4453 +
4454 +               if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
4455 +                       acl_length += sizeof(struct acl_entry);
4456 +                       file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
4457 +                       if(file_acl_temp == NULL) {
4458 +                               SAFE_FREE(file_acl);
4459 +                               errno = ENOMEM;
4460 +                               DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4461 +                               return(-1);
4462 +                       }
4463 +
4464 +                       memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4465 +                       SAFE_FREE(file_acl);
4466 +                       file_acl = file_acl_temp;
4467 +               }
4468 +
4469 +               acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4470 +               file_acl->acl_len += sizeof(struct acl_entry);
4471 +               acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4472 +               acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4473
4474 +               /* In order to use this, we'll need to wait until we can get denies */
4475 +               /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4476 +                       acl_entry->ace_type = ACC_SPECIFY; */
4477
4478 +               acl_entry->ace_type = ACC_SPECIFY;
4479
4480 +               ace_id = acl_entry->ace_id;
4481
4482 +               ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4483 +               DEBUG(10,("The id type is %d\n",ace_id->id_type));
4484 +               ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4485 +               memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof(uid_t));
4486 +               memcpy(ace_id->id_data, &user_id, sizeof(uid_t));
4487 +       }
4488
4489 +       rc = fchacl(fd,file_acl,file_acl->acl_len);
4490 +       DEBUG(10,("errno is %d\n",errno));
4491 +       DEBUG(10,("return code is %d\n",rc));
4492 +       SAFE_FREE(file_acl);
4493 +       DEBUG(10,("Exiting sys_acl_set_fd\n"));
4494 +       return(rc);
4495 +}
4496 +
4497 +int sys_acl_delete_def_file(const char *name)
4498 +{
4499 +       /* AIX has no default ACL */
4500 +       return 0;
4501 +}
4502 +
4503 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4504 +{
4505 +       return(*permset & perm);
4506 +}
4507 +
4508 +int sys_acl_free_text(char *text)
4509 +{
4510 +       return(0);
4511 +}
4512 +
4513 +int sys_acl_free_acl(SMB_ACL_T posix_acl)
4514 +{
4515 +       struct acl_entry_link *acl_entry_link;
4516 +
4517 +       for(acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
4518 +               SAFE_FREE(acl_entry_link->prevp->entryp);
4519 +               SAFE_FREE(acl_entry_link->prevp);
4520 +       }
4521 +
4522 +       SAFE_FREE(acl_entry_link->prevp->entryp);
4523 +       SAFE_FREE(acl_entry_link->prevp);
4524 +       SAFE_FREE(acl_entry_link->entryp);
4525 +       SAFE_FREE(acl_entry_link);
4526
4527 +       return(0);
4528 +}
4529 +
4530 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4531 +{
4532 +       return(0);
4533 +}
4534 +
4535 +#else /* No ACLs. */
4536 +
4537 +int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
4538 +{
4539 +       errno = ENOSYS;
4540 +       return -1;
4541 +}
4542 +
4543 +int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
4544 +{
4545 +       errno = ENOSYS;
4546 +       return -1;
4547 +}
4548 +
4549 +int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
4550 +{
4551 +       errno = ENOSYS;
4552 +       return -1;
4553 +}
4554 +
4555 +void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
4556 +{
4557 +       errno = ENOSYS;
4558 +       return NULL;
4559 +}
4560 +
4561 +SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
4562 +{
4563 +       errno = ENOSYS;
4564 +       return (SMB_ACL_T)NULL;
4565 +}
4566 +
4567 +SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
4568 +{
4569 +       errno = ENOSYS;
4570 +       return (SMB_ACL_T)NULL;
4571 +}
4572 +
4573 +int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
4574 +{
4575 +       errno = ENOSYS;
4576 +       return -1;
4577 +}
4578 +
4579 +int sys_acl_add_perm( UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
4580 +{
4581 +       errno = ENOSYS;
4582 +       return -1;
4583 +}
4584 +
4585 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4586 +{
4587 +       errno = ENOSYS;
4588 +       return (permset & perm) ? 1 : 0;
4589 +}
4590 +
4591 +char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
4592 +{
4593 +       errno = ENOSYS;
4594 +       return NULL;
4595 +}
4596 +
4597 +int sys_acl_free_text(UNUSED(char *text))
4598 +{
4599 +       errno = ENOSYS;
4600 +       return -1;
4601 +}
4602 +
4603 +SMB_ACL_T sys_acl_init(UNUSED(int count))
4604 +{
4605 +       errno = ENOSYS;
4606 +       return NULL;
4607 +}
4608 +
4609 +int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
4610 +{
4611 +       errno = ENOSYS;
4612 +       return -1;
4613 +}
4614 +
4615 +int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
4616 +{
4617 +       errno = ENOSYS;
4618 +       return -1;
4619 +}
4620 +
4621 +int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
4622 +{
4623 +       errno = ENOSYS;
4624 +       return -1;
4625 +}
4626 +
4627 +int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
4628 +{
4629 +       errno = ENOSYS;
4630 +       return -1;
4631 +}
4632 +
4633 +int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
4634 +{
4635 +       errno = ENOSYS;
4636 +       return -1;
4637 +}
4638 +
4639 +int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
4640 +{
4641 +       errno = ENOSYS;
4642 +       return -1;
4643 +}
4644 +
4645 +int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
4646 +{
4647 +       errno = ENOSYS;
4648 +       return -1;
4649 +}
4650 +
4651 +int sys_acl_delete_def_file(UNUSED(const char *name))
4652 +{
4653 +       errno = ENOSYS;
4654 +       return -1;
4655 +}
4656 +
4657 +int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
4658 +{
4659 +       errno = ENOSYS;
4660 +       return -1;
4661 +}
4662 +
4663 +int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
4664 +{
4665 +       errno = ENOSYS;
4666 +       return -1;
4667 +}
4668 +
4669 +#endif /* No ACLs. */
4670 +
4671 +/************************************************************************
4672 + Deliberately outside the ACL defines. Return 1 if this is a "no acls"
4673 + errno, 0 if not.
4674 +************************************************************************/
4675 +
4676 +int no_acl_syscall_error(int err)
4677 +{
4678 +#if defined(ENOSYS)
4679 +       if (err == ENOSYS) {
4680 +               return 1;
4681 +       }
4682 +#endif
4683 +#if defined(ENOTSUP)
4684 +       if (err == ENOTSUP) {
4685 +               return 1;
4686 +       }
4687 +#endif
4688 +       return 0;
4689 +}
4690 --- old/lib/sysacls.h
4691 +++ new/lib/sysacls.h
4692 @@ -0,0 +1,28 @@
4693 +#define SMB_MALLOC(cnt) new_array(char, cnt)
4694 +#define SMB_MALLOC_P(obj) new_array(obj, 1)
4695 +#define SMB_MALLOC_ARRAY(obj, cnt) new_array(obj, cnt)
4696 +#define SMB_REALLOC(mem, cnt) realloc_array(mem, char, cnt)
4697 +#define slprintf snprintf
4698 +
4699 +int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p);
4700 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p);
4701 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p);
4702 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d);
4703 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type);
4704 +SMB_ACL_T sys_acl_get_fd(int fd);
4705 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
4706 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4707 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
4708 +char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen);
4709 +SMB_ACL_T sys_acl_init(int count);
4710 +int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry);
4711 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype);
4712 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual);
4713 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset);
4714 +int sys_acl_valid(SMB_ACL_T theacl);
4715 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl);
4716 +int sys_acl_set_fd(int fd, SMB_ACL_T theacl);
4717 +int sys_acl_delete_def_file(const char *name);
4718 +int sys_acl_free_text(char *text);
4719 +int sys_acl_free_acl(SMB_ACL_T the_acl);
4720 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype);
4721 --- old/mkproto.awk
4722 +++ new/mkproto.awk
4723 @@ -58,7 +58,7 @@ BEGIN {
4724    next;
4725  }
4726  
4727 -!/^OFF_T|^size_t|^off_t|^pid_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^uchar|^short|^struct|^BOOL|^void|^time|^const|^RETSIGTYPE/ {
4728 +!/^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/ {
4729    next;
4730  }
4731  
4732 --- old/options.c
4733 +++ new/options.c
4734 @@ -45,6 +45,7 @@ int copy_dirlinks = 0;
4735  int copy_links = 0;
4736  int preserve_links = 0;
4737  int preserve_hard_links = 0;
4738 +int preserve_acls = 0;
4739  int preserve_perms = 0;
4740  int preserve_executability = 0;
4741  int preserve_devices = 0;
4742 @@ -194,6 +195,7 @@ static void print_rsync_version(enum log
4743         char const *got_socketpair = "no ";
4744         char const *have_inplace = "no ";
4745         char const *hardlinks = "no ";
4746 +       char const *acls = "no ";
4747         char const *links = "no ";
4748         char const *ipv6 = "no ";
4749         STRUCT_STAT *dumstat;
4750 @@ -210,6 +212,10 @@ static void print_rsync_version(enum log
4751         hardlinks = "";
4752  #endif
4753  
4754 +#ifdef SUPPORT_ACLS
4755 +       acls = "";
4756 +#endif
4757 +
4758  #ifdef SUPPORT_LINKS
4759         links = "";
4760  #endif
4761 @@ -223,9 +229,9 @@ static void print_rsync_version(enum log
4762         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
4763         rprintf(f, "<http://rsync.samba.org/>\n");
4764         rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
4765 -               "%shard links, %ssymlinks, batchfiles,\n",
4766 +               "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
4767                 (int) (sizeof (OFF_T) * 8),
4768 -               got_socketpair, hardlinks, links);
4769 +               got_socketpair, hardlinks, acls, links);
4770  
4771         /* Note that this field may not have type ino_t.  It depends
4772          * on the complicated interaction between largefile feature
4773 @@ -295,6 +301,9 @@ void usage(enum logcode F)
4774    rprintf(F," -H, --hard-links            preserve hard links\n");
4775    rprintf(F," -p, --perms                 preserve permissions\n");
4776    rprintf(F," -E, --executability         preserve the file's executability\n");
4777 +#ifdef SUPPORT_ACLS
4778 +  rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
4779 +#endif
4780    rprintf(F,"     --chmod=CHMOD           change destination permissions\n");
4781    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
4782    rprintf(F," -g, --group                 preserve group\n");
4783 @@ -410,6 +419,9 @@ static struct poptOption long_options[] 
4784    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
4785    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
4786    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
4787 +  {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
4788 +  {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
4789 +  {"no-A",             0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
4790    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
4791    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
4792    {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
4793 @@ -1068,6 +1080,23 @@ int parse_arguments(int *argc, const cha
4794                         usage(FINFO);
4795                         exit_cleanup(0);
4796  
4797 +               case 'A':
4798 +#ifdef SUPPORT_ACLS
4799 +                       preserve_acls = preserve_perms = 1;
4800 +                       break;
4801 +#else
4802 +                       /* FIXME: this should probably be ignored with a
4803 +                        * warning and then countermeasures taken to
4804 +                        * restrict group and other access in the presence
4805 +                        * of any more restrictive ACLs, but this is safe
4806 +                        * for now */
4807 +                       snprintf(err_buf,sizeof(err_buf),
4808 +                                 "ACLs are not supported on this %s\n",
4809 +                                am_server ? "server" : "client");
4810 +                       return 0;
4811 +#endif
4812 +
4813 +
4814                 default:
4815                         /* A large opt value means that set_refuse_options()
4816                          * turned this option off. */
4817 @@ -1511,6 +1540,10 @@ void server_options(char **args,int *arg
4818  
4819         if (preserve_hard_links)
4820                 argstr[x++] = 'H';
4821 +#ifdef SUPPORT_ACLS
4822 +       if (preserve_acls)
4823 +               argstr[x++] = 'A';
4824 +#endif
4825         if (preserve_uid)
4826                 argstr[x++] = 'o';
4827         if (preserve_gid)
4828 --- old/receiver.c
4829 +++ new/receiver.c
4830 @@ -46,6 +46,7 @@ extern int keep_partial;
4831  extern int checksum_seed;
4832  extern int inplace;
4833  extern int delay_updates;
4834 +extern mode_t orig_umask;
4835  extern struct stats stats;
4836  extern char *log_format;
4837  extern char *tmpdir;
4838 @@ -344,6 +345,10 @@ int recv_files(int f_in, struct file_lis
4839         int itemizing = am_daemon ? daemon_log_format_has_i
4840                       : !am_server && log_format_has_i;
4841         int max_phase = protocol_version >= 29 ? 2 : 1;
4842 +       int dflt_perms = (ACCESSPERMS & ~orig_umask);
4843 +#ifdef SUPPORT_ACLS
4844 +       char *parent_dirname = "";
4845 +#endif
4846         int i, recv_ok;
4847  
4848         if (verbose > 2)
4849 @@ -541,7 +546,16 @@ int recv_files(int f_in, struct file_lis
4850                  * mode based on the local permissions and some heuristics. */
4851                 if (!preserve_perms) {
4852                         int exists = fd1 != -1;
4853 -                       file->mode = dest_mode(file->mode, st.st_mode, exists);
4854 +#ifdef SUPPORT_ACLS
4855 +                       char *dn = file->dirname ? file->dirname : ".";
4856 +                       if (parent_dirname != dn
4857 +                        && strcmp(parent_dirname, dn) != 0) {
4858 +                               dflt_perms = default_perms_for_dir(dn);
4859 +                               parent_dirname = dn;
4860 +                       }
4861 +#endif
4862 +                       file->mode = dest_mode(file->mode, st.st_mode,
4863 +                                              dflt_perms, exists);
4864                 }
4865  
4866                 /* We now check to see if we are writing file "inplace" */
4867 --- old/rsync.c
4868 +++ new/rsync.c
4869 @@ -101,7 +101,8 @@ void free_sums(struct sum_struct *s)
4870  
4871  /* This is only called when we aren't preserving permissions.  Figure out what
4872   * the permissions should be and return them merged back into the mode. */
4873 -mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int exists)
4874 +mode_t dest_mode(mode_t flist_mode, mode_t cur_mode, int dflt_perms,
4875 +                int exists)
4876  {
4877         /* If the file already exists, we'll return the local permissions,
4878          * possibly tweaked by the --executability option. */
4879 @@ -116,7 +117,7 @@ mode_t dest_mode(mode_t flist_mode, mode
4880                                 cur_mode |= (cur_mode & 0444) >> 2;
4881                 }
4882         } else
4883 -               cur_mode = flist_mode & ACCESSPERMS & ~orig_umask;
4884 +               cur_mode = flist_mode & ACCESSPERMS & dflt_perms;
4885         if (daemon_chmod_modes && !S_ISLNK(flist_mode))
4886                 cur_mode = tweak_mode(cur_mode, daemon_chmod_modes);
4887         return (flist_mode & ~CHMOD_BITS) | (cur_mode & CHMOD_BITS);
4888 @@ -217,6 +218,11 @@ int set_file_attrs(char *fname, struct f
4889         }
4890  #endif
4891  
4892 +       /* It's fine to call SET_ACL now; the generator will enable
4893 +        * writability on the directory using chmod if necessary. */
4894 +       if (SET_ACL(fname, file) == 0)
4895 +               updated = 1;
4896 +
4897         if (verbose > 1 && flags & ATTRS_REPORT) {
4898                 enum logcode code = daemon_log_format_has_i || dry_run
4899                                   ? FCLIENT : FINFO;
4900 --- old/rsync.h
4901 +++ new/rsync.h
4902 @@ -658,6 +658,44 @@ struct chmod_mode_struct;
4903  
4904  #define UNUSED(x) x __attribute__((__unused__))
4905  
4906 +#if HAVE_POSIX_ACLS|HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|\
4907 +    HAVE_HPUX_ACLS|HAVE_IRIX_ACLS|HAVE_AIX_ACLS|HAVE_TRU64_ACLS
4908 +#define SUPPORT_ACLS 1
4909 +#endif
4910 +
4911 +#if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
4912 +#define ACLS_NEED_MASK 1
4913 +#endif
4914 +
4915 +#ifdef SUPPORT_ACLS
4916 +#ifdef HAVE_SYS_ACL_H
4917 +#include <sys/acl.h>
4918 +#endif
4919 +#define MAKE_ACL(file, fname)                  make_acl(file, fname)
4920 +#define SEND_ACL(file, f)                      send_acl(file, f)
4921 +#define RECEIVE_ACL(file, f)                   receive_acl(file, f)
4922 +#define SORT_FILE_ACL_INDEX_LISTS()            sort_file_acl_index_lists()
4923 +#define SET_ACL(fname, file)                   set_acl(fname, file)
4924 +#define NEXT_ACL_UID()                         next_acl_uid()
4925 +#define ACL_UID_MAP(uid)                       acl_uid_map(uid)
4926 +#define PUSH_KEEP_BACKUP_ACL(file, orig, dest) \
4927 +                                       push_keep_backup_acl(file, orig, dest)
4928 +#define CLEANUP_KEEP_BACKUP_ACL()              cleanup_keep_backup_acl()
4929 +#define DUP_ACL(orig, dest, mode)              dup_acl(orig, dest, mode)
4930 +#else /* SUPPORT_ACLS */
4931 +#define MAKE_ACL(file, fname)                  1 /* checked return value */
4932 +#define SEND_ACL(file, f)
4933 +#define RECEIVE_ACL(file, f)
4934 +#define SORT_FILE_ACL_INDEX_LISTS()
4935 +#define SET_ACL(fname, file)                   1 /* checked return value */
4936 +#define NEXT_ACL_UID() 
4937 +#define ACL_UID_MAP(uid)
4938 +#define PUSH_KEEP_BACKUP_ACL(file, orig, dest)
4939 +#define CLEANUP_KEEP_BACKUP_ACL()
4940 +#define DUP_ACL(src, orig, mode)               1 /* checked return value */
4941 +#endif /* SUPPORT_ACLS */
4942 +#include "smb_acls.h"
4943 +
4944  #include "proto.h"
4945  
4946  /* We have replacement versions of these if they're missing. */
4947 --- old/rsync.yo
4948 +++ new/rsync.yo
4949 @@ -321,6 +321,7 @@ to the detailed description below for a 
4950   -H, --hard-links            preserve hard links
4951   -p, --perms                 preserve permissions
4952   -E, --executability         preserve executability
4953 + -A, --acls                  preserve ACLs (implies -p) [non-standard]
4954       --chmod=CHMOD           change destination permissions
4955   -o, --owner                 preserve owner (super-user only)
4956   -g, --group                 preserve group
4957 @@ -742,7 +743,9 @@ quote(itemize(
4958    permissions, though the bf(--executability) option might change just
4959    the execute permission for the file.
4960    it() New files get their "normal" permission bits set to the source
4961 -  file's permissions masked with the receiving end's umask setting, and
4962 +  file's permissions masked with the receiving directory's default
4963 +  permissions (either the receiving process's umask, or the permissions
4964 +  specified via the destination directory's default ACL), and
4965    their special permission bits disabled except in the case where a new
4966    directory inherits a setgid bit from its parent directory.
4967  ))
4968 @@ -773,9 +776,11 @@ The preservation of the destination's se
4969  directories when bf(--perms) is off was added in rsync 2.6.7.  Older rsync
4970  versions erroneously preserved the three special permission bits for
4971  newly-created files when bf(--perms) was off, while overriding the
4972 -destination's setgid bit setting on a newly-created directory.  (Keep in
4973 -mind that it is the version of the receiving rsync that affects this
4974 -behavior.)
4975 +destination's setgid bit setting on a newly-created directory.  Default ACL
4976 +observance was added to the ACL patch for rsync 2.6.7, so older (or
4977 +non-ACL-enabled) rsyncs use the umask even if default ACLs are present.
4978 +(Keep in mind that it is the version of the receiving rsync that affects
4979 +these behaviors.)
4980  
4981  dit(bf(-E, --executability)) This option causes rsync to preserve the
4982  executability (or non-executability) of regular files when bf(--perms) is
4983 @@ -793,6 +798,10 @@ quote(itemize(
4984  
4985  If bf(--perms) is enabled, this option is ignored.
4986  
4987 +dit(bf(-A, --acls)) This option causes rsync to update the destination
4988 +ACLs to be the same as the source ACLs.  This nonstandard option only
4989 +works if the remote rsync also supports it.  bf(--acls) implies bf(--perms).
4990 +
4991  dit(bf(--chmod)) This option tells rsync to apply one or more
4992  comma-separated "chmod" strings to the permission of the files in the
4993  transfer.  The resulting value is treated as though it was the permissions
4994 --- old/smb_acls.h
4995 +++ new/smb_acls.h
4996 @@ -0,0 +1,277 @@
4997 +/* 
4998 +   Unix SMB/Netbios implementation.
4999 +   Version 2.2.x
5000 +   Portable SMB ACL interface
5001 +   Copyright (C) Jeremy Allison 2000
5002 +   
5003 +   This program is free software; you can redistribute it and/or modify
5004 +   it under the terms of the GNU General Public License as published by
5005 +   the Free Software Foundation; either version 2 of the License, or
5006 +   (at your option) any later version.
5007 +   
5008 +   This program is distributed in the hope that it will be useful,
5009 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
5010 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5011 +   GNU General Public License for more details.
5012 +   
5013 +   You should have received a copy of the GNU General Public License
5014 +   along with this program; if not, write to the Free Software
5015 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
5016 +*/
5017 +
5018 +#ifndef _SMB_ACLS_H
5019 +#define _SMB_ACLS_H
5020 +
5021 +#if defined(HAVE_POSIX_ACLS)
5022 +
5023 +/* This is an identity mapping (just remove the SMB_). */
5024 +
5025 +#define SMB_ACL_TAG_T          acl_tag_t
5026 +#define SMB_ACL_TYPE_T         acl_type_t
5027 +#define SMB_ACL_PERMSET_T      acl_permset_t
5028 +#define SMB_ACL_PERM_T         acl_perm_t
5029 +#define SMB_ACL_READ           ACL_READ
5030 +#define SMB_ACL_WRITE          ACL_WRITE
5031 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
5032 +
5033 +/* Types of ACLs. */
5034 +#define SMB_ACL_USER           ACL_USER
5035 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
5036 +#define SMB_ACL_GROUP          ACL_GROUP
5037 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
5038 +#define SMB_ACL_OTHER          ACL_OTHER
5039 +#define SMB_ACL_MASK           ACL_MASK
5040 +
5041 +#define SMB_ACL_T              acl_t
5042 +
5043 +#define SMB_ACL_ENTRY_T                acl_entry_t
5044 +
5045 +#define SMB_ACL_FIRST_ENTRY    ACL_FIRST_ENTRY
5046 +#define SMB_ACL_NEXT_ENTRY     ACL_NEXT_ENTRY
5047 +
5048 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
5049 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
5050 +
5051 +#elif defined(HAVE_TRU64_ACLS)
5052 +
5053 +/* This is for DEC/Compaq Tru64 UNIX */
5054 +
5055 +#define SMB_ACL_TAG_T          acl_tag_t
5056 +#define SMB_ACL_TYPE_T         acl_type_t
5057 +#define SMB_ACL_PERMSET_T      acl_permset_t
5058 +#define SMB_ACL_PERM_T         acl_perm_t
5059 +#define SMB_ACL_READ           ACL_READ
5060 +#define SMB_ACL_WRITE          ACL_WRITE
5061 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
5062 +
5063 +/* Types of ACLs. */
5064 +#define SMB_ACL_USER           ACL_USER
5065 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
5066 +#define SMB_ACL_GROUP          ACL_GROUP
5067 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
5068 +#define SMB_ACL_OTHER          ACL_OTHER
5069 +#define SMB_ACL_MASK           ACL_MASK
5070 +
5071 +#define SMB_ACL_T              acl_t
5072 +
5073 +#define SMB_ACL_ENTRY_T                acl_entry_t
5074 +
5075 +#define SMB_ACL_FIRST_ENTRY    0
5076 +#define SMB_ACL_NEXT_ENTRY     1
5077 +
5078 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
5079 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
5080 +
5081 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
5082 +/*
5083 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
5084 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
5085 + */
5086 +
5087 +/* SVR4.2 ES/MP ACLs */
5088 +typedef int SMB_ACL_TAG_T;
5089 +typedef int SMB_ACL_TYPE_T;
5090 +typedef ushort *SMB_ACL_PERMSET_T;
5091 +typedef ushort SMB_ACL_PERM_T;
5092 +#define SMB_ACL_READ           4
5093 +#define SMB_ACL_WRITE          2
5094 +#define SMB_ACL_EXECUTE                1
5095 +
5096 +/* Types of ACLs. */
5097 +#define SMB_ACL_USER           USER
5098 +#define SMB_ACL_USER_OBJ       USER_OBJ
5099 +#define SMB_ACL_GROUP          GROUP
5100 +#define SMB_ACL_GROUP_OBJ      GROUP_OBJ
5101 +#define SMB_ACL_OTHER          OTHER_OBJ
5102 +#define SMB_ACL_MASK           CLASS_OBJ
5103 +
5104 +typedef struct SMB_ACL_T {
5105 +       int size;
5106 +       int count;
5107 +       int next;
5108 +       struct acl acl[1];
5109 +} *SMB_ACL_T;
5110 +
5111 +typedef struct acl *SMB_ACL_ENTRY_T;
5112 +
5113 +#define SMB_ACL_FIRST_ENTRY    0
5114 +#define SMB_ACL_NEXT_ENTRY     1
5115 +
5116 +#define SMB_ACL_TYPE_ACCESS    0
5117 +#define SMB_ACL_TYPE_DEFAULT   1
5118 +
5119 +#elif defined(HAVE_HPUX_ACLS)
5120 +
5121 +/*
5122 + * Based on the Solaris & UnixWare code.
5123 + */
5124 +
5125 +#undef GROUP
5126 +#include <sys/aclv.h>
5127 +
5128 +/* SVR4.2 ES/MP ACLs */
5129 +typedef int SMB_ACL_TAG_T;
5130 +typedef int SMB_ACL_TYPE_T;
5131 +typedef ushort *SMB_ACL_PERMSET_T;
5132 +typedef ushort SMB_ACL_PERM_T;
5133 +#define SMB_ACL_READ           4
5134 +#define SMB_ACL_WRITE          2
5135 +#define SMB_ACL_EXECUTE                1
5136 +
5137 +/* Types of ACLs. */
5138 +#define SMB_ACL_USER           USER
5139 +#define SMB_ACL_USER_OBJ       USER_OBJ
5140 +#define SMB_ACL_GROUP          GROUP
5141 +#define SMB_ACL_GROUP_OBJ      GROUP_OBJ
5142 +#define SMB_ACL_OTHER          OTHER_OBJ
5143 +#define SMB_ACL_MASK           CLASS_OBJ
5144 +
5145 +typedef struct SMB_ACL_T {
5146 +       int size;
5147 +       int count;
5148 +       int next;
5149 +       struct acl acl[1];
5150 +} *SMB_ACL_T;
5151 +
5152 +typedef struct acl *SMB_ACL_ENTRY_T;
5153 +
5154 +#define SMB_ACL_FIRST_ENTRY    0
5155 +#define SMB_ACL_NEXT_ENTRY     1
5156 +
5157 +#define SMB_ACL_TYPE_ACCESS    0
5158 +#define SMB_ACL_TYPE_DEFAULT   1
5159 +
5160 +#elif defined(HAVE_IRIX_ACLS)
5161 +
5162 +#define SMB_ACL_TAG_T          acl_tag_t
5163 +#define SMB_ACL_TYPE_T         acl_type_t
5164 +#define SMB_ACL_PERMSET_T      acl_permset_t
5165 +#define SMB_ACL_PERM_T         acl_perm_t
5166 +#define SMB_ACL_READ           ACL_READ
5167 +#define SMB_ACL_WRITE          ACL_WRITE
5168 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
5169 +
5170 +/* Types of ACLs. */
5171 +#define SMB_ACL_USER           ACL_USER
5172 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
5173 +#define SMB_ACL_GROUP          ACL_GROUP
5174 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
5175 +#define SMB_ACL_OTHER          ACL_OTHER_OBJ
5176 +#define SMB_ACL_MASK           ACL_MASK
5177 +
5178 +typedef struct SMB_ACL_T {
5179 +       int next;
5180 +       BOOL freeaclp;
5181 +       struct acl *aclp;
5182 +} *SMB_ACL_T;
5183 +
5184 +#define SMB_ACL_ENTRY_T                acl_entry_t
5185 +
5186 +#define SMB_ACL_FIRST_ENTRY    0
5187 +#define SMB_ACL_NEXT_ENTRY     1
5188 +
5189 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
5190 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
5191 +
5192 +#elif defined(HAVE_AIX_ACLS)
5193 +
5194 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
5195 +
5196 +#include "/usr/include/acl.h"
5197 +
5198 +typedef uint *SMB_ACL_PERMSET_T;
5199
5200 +struct acl_entry_link{
5201 +       struct acl_entry_link *prevp;
5202 +       struct new_acl_entry *entryp;
5203 +       struct acl_entry_link *nextp;
5204 +       int count;
5205 +};
5206 +
5207 +struct new_acl_entry{
5208 +       unsigned short ace_len;
5209 +       unsigned short ace_type;
5210 +       unsigned int ace_access;
5211 +       struct ace_id ace_id[1];
5212 +};
5213 +
5214 +#define SMB_ACL_ENTRY_T                struct new_acl_entry*
5215 +#define SMB_ACL_T              struct acl_entry_link*
5216
5217 +#define SMB_ACL_TAG_T          unsigned short
5218 +#define SMB_ACL_TYPE_T         int
5219 +#define SMB_ACL_PERM_T         uint
5220 +#define SMB_ACL_READ           S_IRUSR
5221 +#define SMB_ACL_WRITE          S_IWUSR
5222 +#define SMB_ACL_EXECUTE                S_IXUSR
5223 +
5224 +/* Types of ACLs. */
5225 +#define SMB_ACL_USER           ACEID_USER
5226 +#define SMB_ACL_USER_OBJ       3
5227 +#define SMB_ACL_GROUP          ACEID_GROUP
5228 +#define SMB_ACL_GROUP_OBJ      4
5229 +#define SMB_ACL_OTHER          5
5230 +#define SMB_ACL_MASK           6
5231 +
5232 +
5233 +#define SMB_ACL_FIRST_ENTRY    1
5234 +#define SMB_ACL_NEXT_ENTRY     2
5235 +
5236 +#define SMB_ACL_TYPE_ACCESS    0
5237 +#define SMB_ACL_TYPE_DEFAULT   1
5238 +
5239 +#else /* No ACLs. */
5240 +
5241 +/* No ACLS - fake it. */
5242 +#define SMB_ACL_TAG_T          int
5243 +#define SMB_ACL_TYPE_T         int
5244 +#define SMB_ACL_PERMSET_T      mode_t
5245 +#define SMB_ACL_PERM_T         mode_t
5246 +#define SMB_ACL_READ           S_IRUSR
5247 +#define SMB_ACL_WRITE          S_IWUSR
5248 +#define SMB_ACL_EXECUTE                S_IXUSR
5249 +
5250 +/* Types of ACLs. */
5251 +#define SMB_ACL_USER           0
5252 +#define SMB_ACL_USER_OBJ       1
5253 +#define SMB_ACL_GROUP          2
5254 +#define SMB_ACL_GROUP_OBJ      3
5255 +#define SMB_ACL_OTHER          4
5256 +#define SMB_ACL_MASK           5
5257 +
5258 +typedef struct SMB_ACL_T {
5259 +       int dummy;
5260 +} *SMB_ACL_T;
5261 +
5262 +typedef struct SMB_ACL_ENTRY_T {
5263 +       int dummy;
5264 +} *SMB_ACL_ENTRY_T;
5265 +
5266 +#define SMB_ACL_FIRST_ENTRY    0
5267 +#define SMB_ACL_NEXT_ENTRY     1
5268 +
5269 +#define SMB_ACL_TYPE_ACCESS    0
5270 +#define SMB_ACL_TYPE_DEFAULT   1
5271 +
5272 +#endif /* No ACLs. */
5273 +#endif /* _SMB_ACLS_H */
5274 --- old/testsuite/default-acls.test
5275 +++ new/testsuite/default-acls.test
5276 @@ -0,0 +1,55 @@
5277 +#! /bin/sh
5278 +
5279 +# This program is distributable under the terms of the GNU GPL see
5280 +# COPYING).
5281 +
5282 +# Test that rsync obeys default ACLs. -- Matt McCutchen
5283 +
5284 +. $srcdir/testsuite/rsync.fns
5285 +
5286 +$RSYNC --version | grep ", ACLs" >/dev/null || test_skipped "Rsync is configured without ACL support"
5287 +setfacl -dm u::rwx,g::---,o::--- "$scratchdir" || test_skipped "Your filesystem has ACLs disabled"
5288 +
5289 +# Call as: testit <dirname> <default-acl> <file-expected> <program-expected>
5290 +testit() {
5291 +    todir="$scratchdir/$1"
5292 +    mkdir "$todir"
5293 +    # FIXME This doesn't work on solaris...
5294 +    setfacl -k "$todir"
5295 +    [ "$2" ] && setfacl -dm "$2" "$todir"
5296 +    # Make sure we obey ACLs when creating a directory to hold multiple transferred files,
5297 +    # even though the directory itself is outside the transfer
5298 +    $RSYNC -rvv "$scratchdir/dir" "$scratchdir/file" "$scratchdir/program" "$todir/to/"
5299 +    check_perms "$todir/to" $4 "Target $1"
5300 +    check_perms "$todir/to/dir" $4 "Target $1"
5301 +    check_perms "$todir/to/file" $3 "Target $1"
5302 +    check_perms "$todir/to/program" $4 "Target $1"
5303 +    # Make sure get_local_name doesn't mess us up when transferring only one file
5304 +    $RSYNC -rvv "$scratchdir/file" "$todir/to/anotherfile"
5305 +    check_perms "$todir/to/anotherfile" $3 "Target $1"
5306 +    # Make sure we obey default ACLs when not transferring a regular file
5307 +    $RSYNC -rvv "$scratchdir/dir/" "$todir/to/anotherdir/"
5308 +    check_perms "$todir/to/anotherdir" $4 "Target $1"
5309 +}
5310 +
5311 +mkdir "$scratchdir/dir"
5312 +echo "File!" >"$scratchdir/file"
5313 +echo "#!/bin/sh" >"$scratchdir/program"
5314 +chmod 777 "$scratchdir/dir"
5315 +chmod 666 "$scratchdir/file"
5316 +chmod 777 "$scratchdir/program"
5317 +
5318 +# Test some target directories
5319 +umask 0077
5320 +testit da777 u::rwx,g::rwx,o::rwx rw-rw-rw- rwxrwxrwx
5321 +testit da775 u::rwx,g::rwx,o::r-x rw-rw-r-- rwxrwxr-x
5322 +testit da750 u::rwx,g::r-x,o::--- rw-r----- rwxr-x---
5323 +testit da770mask u::rwx,g::---,m::rwx,o::--- rw-rw---- rwxrwx---
5324 +testit noda1 '' rw------- rwx------
5325 +umask 0000
5326 +testit noda2 '' rw-rw-rw- rwxrwxrwx
5327 +umask 0022
5328 +testit noda3 '' rw-r--r-- rwxr-xr-x
5329 +
5330 +# Hooray
5331 +exit 0
5332 --- old/uidlist.c
5333 +++ new/uidlist.c
5334 @@ -34,6 +34,7 @@
5335  extern int verbose;
5336  extern int preserve_uid;
5337  extern int preserve_gid;
5338 +extern int preserve_acls;
5339  extern int numeric_ids;
5340  extern int am_root;
5341  
5342 @@ -274,7 +275,7 @@ void send_uid_list(int f)
5343         if (numeric_ids)
5344                 return;
5345  
5346 -       if (preserve_uid) {
5347 +       if (preserve_uid || preserve_acls) {
5348                 int len;
5349                 /* we send sequences of uid/byte-length/name */
5350                 for (list = uidlist; list; list = list->next) {
5351 @@ -291,7 +292,7 @@ void send_uid_list(int f)
5352                 write_int(f, 0);
5353         }
5354  
5355 -       if (preserve_gid) {
5356 +       if (preserve_gid || preserve_acls) {
5357                 int len;
5358                 for (list = gidlist; list; list = list->next) {
5359                         if (!list->name)
5360 @@ -312,7 +313,7 @@ void recv_uid_list(int f, struct file_li
5361         int id, i;
5362         char *name;
5363  
5364 -       if (preserve_uid && !numeric_ids) {
5365 +       if ((preserve_uid || preserve_acls) && !numeric_ids) {
5366                 /* read the uid list */
5367                 while ((id = read_int(f)) != 0) {
5368                         int len = read_byte(f);
5369 @@ -324,7 +325,7 @@ void recv_uid_list(int f, struct file_li
5370                 }
5371         }
5372  
5373 -       if (preserve_gid && !numeric_ids) {
5374 +       if ((preserve_gid || preserve_acls) && !numeric_ids) {
5375                 /* read the gid list */
5376                 while ((id = read_int(f)) != 0) {
5377                         int len = read_byte(f);
5378 @@ -336,6 +337,18 @@ void recv_uid_list(int f, struct file_li
5379                 }
5380         }
5381  
5382 +#ifdef SUPPORT_ACLS
5383 +       if (preserve_acls && !numeric_ids) {
5384 +               id_t id;
5385 +               /* The enumerations don't return 0 except to flag the last
5386 +                * entry, since uidlist doesn't munge 0 anyway. */
5387 +               while ((id = next_acl_uid(flist)) != 0)
5388 +                       acl_uid_map(match_uid(id));
5389 +               while ((id = next_acl_gid(flist)) != 0)
5390 +                       acl_gid_map(match_gid(id));
5391 +       }
5392 +#endif /* SUPPORT_ACLS */
5393 +
5394         /* Now convert all the uids/gids from sender values to our values. */
5395         if (am_root && preserve_uid && !numeric_ids) {
5396                 for (i = 0; i < flist->count; i++)