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