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