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