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