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