Tweaked the text of a few error messages.
[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-08-19 19:53:27
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,77 @@ 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_MSG_RESULT(ACLs requested -- running tests)
1234 +                   AC_CHECK_LIB(acl,acl_get_file)
1235 +                       AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1236 +                       AC_TRY_LINK([#include <sys/types.h>
1237 +#include <sys/acl.h>],
1238 +[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1239 +samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
1240 +                       if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1241 +                           AC_MSG_RESULT(Using posix ACLs)
1242 +                           AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
1243 +                           AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
1244 +                               AC_TRY_LINK([#include <sys/types.h>
1245 +#include <sys/acl.h>],
1246 +[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1247 +samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
1248 +                           if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1249 +                               AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1250 +                           fi
1251 +                       else
1252 +                           AC_MSG_ERROR(Failed to find ACL support)
1253 +                       fi
1254 +                       ;;
1255 +               esac
1256 +               ;;
1257 +  *)
1258 +    AC_MSG_RESULT(no)
1259 +       AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1260 +    ;;
1261 +  esac ],
1262 +  AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1263 +  AC_MSG_RESULT(no)
1264 +)
1265 +
1266  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1267  AC_OUTPUT
1268  
1269 --- orig/flist.c        2004-08-12 18:34:38
1270 +++ flist.c     2004-07-03 20:11:58
1271 @@ -944,6 +944,8 @@ void send_file_name(int f, struct file_l
1272  
1273         if (!file)
1274                 return;
1275 +       if (!MAKE_ACL(file, fname))
1276 +               return;
1277  
1278         maybe_emit_filelist_progress(flist);
1279  
1280 @@ -952,6 +954,10 @@ void send_file_name(int f, struct file_l
1281         if (file->basename[0]) {
1282                 flist->files[flist->count++] = file;
1283                 send_file_entry(file, f, base_flags);
1284 +               SEND_ACL(file, f);
1285 +       } else {
1286 +               /* Cleanup unsent ACL(s). */
1287 +               SEND_ACL(file, -1);
1288         }
1289  
1290         if (recursive && S_ISDIR(file->mode)
1291 @@ -1268,6 +1274,8 @@ struct file_list *recv_file_list(int f)
1292                         flags |= read_byte(f) << 8;
1293                 receive_file_entry(&flist->files[i], flags, flist, f);
1294  
1295 +               RECEIVE_ACL(flist->files[i], f);
1296 +
1297                 if (S_ISREG(flist->files[i]->mode))
1298                         stats.total_size += flist->files[i]->length;
1299  
1300 @@ -1290,6 +1298,8 @@ struct file_list *recv_file_list(int f)
1301  
1302         clean_flist(flist, relative_paths, 1);
1303  
1304 +       SORT_FILE_ACL_INDEX_LISTS();
1305 +
1306         if (f != -1) {
1307                 /* Now send the uid/gid list. This was introduced in
1308                  * protocol version 15 */
1309 --- orig/generator.c    2004-08-05 18:24:21
1310 +++ generator.c 2004-07-03 20:11:58
1311 @@ -332,6 +332,10 @@ static void recv_generator(char *fname, 
1312                 if (set_perms(fname, file, statret ? NULL : &st, 0)
1313                     && verbose && f_out != -1)
1314                         rprintf(FINFO, "%s/\n", safe_fname(fname));
1315 +#if SUPPORT_ACLS
1316 +               if (f_out == -1)
1317 +                       SET_ACL(fname, file);
1318 +#endif
1319                 return;
1320         }
1321  
1322 --- orig/mkproto.awk    2004-01-01 21:10:50
1323 +++ mkproto.awk 2004-06-30 00:04:06
1324 @@ -58,7 +58,7 @@ BEGIN {
1325    next;
1326  }
1327  
1328 -!/^OFF_T|^size_t|^off_t|^pid_t|^unsigned|^mode_t|^DIR|^user|^int|^char|^uint|^struct|^BOOL|^void|^time|^const/ {
1329 +!/^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/ {
1330    next;
1331  }
1332  
1333 --- orig/options.c      2004-08-12 18:34:38
1334 +++ options.c   2004-08-19 17:38:57
1335 @@ -43,6 +43,7 @@ int keep_dirlinks = 0;
1336  int copy_links = 0;
1337  int preserve_links = 0;
1338  int preserve_hard_links = 0;
1339 +int preserve_acls = 0;
1340  int preserve_perms = 0;
1341  int preserve_devices = 0;
1342  int preserve_uid = 0;
1343 @@ -152,6 +153,7 @@ static void print_rsync_version(enum log
1344         char const *got_socketpair = "no ";
1345         char const *have_inplace = "no ";
1346         char const *hardlinks = "no ";
1347 +       char const *acls = "no ";
1348         char const *links = "no ";
1349         char const *ipv6 = "no ";
1350         STRUCT_STAT *dumstat;
1351 @@ -168,6 +170,10 @@ static void print_rsync_version(enum log
1352         hardlinks = "";
1353  #endif
1354  
1355 +#if SUPPORT_ACLS
1356 +       acls = "";
1357 +#endif
1358 +
1359  #if SUPPORT_LINKS
1360         links = "";
1361  #endif
1362 @@ -182,9 +188,9 @@ static void print_rsync_version(enum log
1363                 "Copyright (C) 1996-2004 by Andrew Tridgell and others\n");
1364         rprintf(f, "<http://rsync.samba.org/>\n");
1365         rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
1366 -               "%shard links, %ssymlinks, batchfiles, \n",
1367 +               "%shard links, %sACLs, %ssymlinks, batchfiles, \n",
1368                 (int) (sizeof (OFF_T) * 8),
1369 -               got_socketpair, hardlinks, links);
1370 +               got_socketpair, hardlinks, acls, links);
1371  
1372         /* Note that this field may not have type ino_t.  It depends
1373          * on the complicated interaction between largefile feature
1374 @@ -249,6 +255,7 @@ void usage(enum logcode F)
1375    rprintf(F,"     --safe-links            ignore \"unsafe\" symlinks\n");
1376    rprintf(F," -H, --hard-links            preserve hard links\n");
1377    rprintf(F," -p, --perms                 preserve permissions\n");
1378 +  rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
1379    rprintf(F," -o, --owner                 preserve owner (root only)\n");
1380    rprintf(F," -g, --group                 preserve group\n");
1381    rprintf(F," -D, --devices               preserve devices (root only)\n");
1382 @@ -358,6 +365,7 @@ static struct poptOption long_options[] 
1383    {"no-whole-file",    0,  POPT_ARG_VAL,    &whole_file, 0, 0, 0 },
1384    {"copy-unsafe-links", 0, POPT_ARG_NONE,   &copy_unsafe_links, 0, 0, 0 },
1385    {"perms",           'p', POPT_ARG_NONE,   &preserve_perms, 0, 0, 0 },
1386 +  {"acls",            'A', POPT_ARG_NONE,   0,              'A', 0, 0 },
1387    {"owner",           'o', POPT_ARG_NONE,   &preserve_uid, 0, 0, 0 },
1388    {"group",           'g', POPT_ARG_NONE,   &preserve_gid, 0, 0, 0 },
1389    {"devices",         'D', POPT_ARG_NONE,   &preserve_devices, 0, 0, 0 },
1390 @@ -620,6 +628,24 @@ int parse_arguments(int *argc, const cha
1391                         return 0;
1392  #endif
1393  
1394 +               case 'A':
1395 +#if SUPPORT_ACLS
1396 +                       preserve_acls = 1;
1397 +                       preserve_perms = 1;
1398 +#else
1399 +                       /* FIXME: this should probably be ignored with a
1400 +                        * warning and then countermeasures taken to
1401 +                        * restrict group and other access in the presence
1402 +                        * of any more restrictive ACLs, but this is safe
1403 +                        * for now */
1404 +                       snprintf(err_buf,sizeof(err_buf),
1405 +                                 "ACLs are not supported on this %s\n",
1406 +                                am_server ? "server" : "client");
1407 +                       return 0;
1408 +#endif /* SUPPORT_ACLS */
1409 +                       break;
1410 +
1411 +
1412                 default:
1413                         /* A large opt value means that set_refuse_options()
1414                          * turned this option off (opt-BASE is its index). */
1415 @@ -932,6 +958,8 @@ void server_options(char **args,int *arg
1416  
1417         if (preserve_hard_links)
1418                 argstr[x++] = 'H';
1419 +       if (preserve_acls)
1420 +               argstr[x++] = 'A';
1421         if (preserve_uid)
1422                 argstr[x++] = 'o';
1423         if (preserve_gid)
1424 --- orig/rsync.c        2004-08-09 21:07:10
1425 +++ rsync.c     2004-07-03 20:11:58
1426 @@ -207,6 +207,14 @@ int set_perms(char *fname,struct file_st
1427         }
1428  #endif
1429  
1430 +       /* If this is a directory, SET_ACL() will be called on the cleanup
1431 +        * receive_generator() pass--if we called it here, we might clobber
1432 +        * writability on the directory. everything else is OK to do now. */
1433 +       if (!S_ISDIR(st->st_mode)) {
1434 +               if (SET_ACL(fname, file) == 0)
1435 +                       updated = 1;
1436 +       }
1437 +
1438         if (verbose > 1 && flags & PERMS_REPORT) {
1439                 if (updated)
1440                         rprintf(FINFO,"%s\n",fname);
1441 --- orig/rsync.h        2004-08-03 15:41:32
1442 +++ rsync.h     2004-07-03 20:11:58
1443 @@ -541,6 +541,40 @@ static inline int flist_up(struct file_l
1444  #include "lib/permstring.h"
1445  #include "lib/addrinfo.h"
1446  
1447 +#define SUPPORT_ACLS HAVE_POSIX_ACLS|HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|\
1448 +               HAVE_HPUX_ACLS|HAVE_IRIX_ACLS|HAVE_AIX_ACLS|HAVE_TRU64_ACLS
1449 +
1450 +#define ACLS_NEED_MASK HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
1451 +
1452 +#if SUPPORT_ACLS
1453 +#ifdef HAVE_SYS_ACL_H
1454 +#include <sys/acl.h>
1455 +#endif
1456 +#define MAKE_ACL(file, fname)                  make_acl(file, fname)
1457 +#define SEND_ACL(file, f)                      send_acl(file, f)
1458 +#define RECEIVE_ACL(file, f)                   receive_acl(file, f)
1459 +#define SORT_FILE_ACL_INDEX_LISTS()            sort_file_acl_index_lists()
1460 +#define SET_ACL(fname, file)                   set_acl(fname, file)
1461 +#define NEXT_ACL_UID()                         next_acl_uid()
1462 +#define ACL_UID_MAP(uid)                       acl_uid_map(uid)
1463 +#define PUSH_KEEP_BACKUP_ACL(file, orig, dest) \
1464 +                                       push_keep_backup_acl(file, orig, dest)
1465 +#define CLEANUP_KEEP_BACKUP_ACL()              cleanup_keep_backup_acl()
1466 +#define DUP_ACL(orig, dest, mode)              dup_acl(orig, dest, mode)
1467 +#else /* SUPPORT_ACLS */
1468 +#define MAKE_ACL(file, fname)                  1 /* checked return value */
1469 +#define SEND_ACL(file, f)
1470 +#define RECEIVE_ACL(file, f)
1471 +#define SORT_FILE_ACL_INDEX_LISTS()
1472 +#define SET_ACL(fname, file)                   0 /* checked return value */
1473 +#define NEXT_ACL_UID() 
1474 +#define ACL_UID_MAP(uid)
1475 +#define PUSH_KEEP_BACKUP_ACL(file, orig, dest)
1476 +#define CLEANUP_KEEP_BACKUP_ACL()
1477 +#define DUP_ACL(src, orig, mode)               0 /* checked return value */
1478 +#endif /* SUPPORT_ACLS */
1479 +#include "smb_acls.h"
1480 +
1481  #include "proto.h"
1482  
1483  /* We have replacement versions of these if they're missing. */
1484 --- orig/rsync.yo       2004-08-19 16:30:47
1485 +++ rsync.yo    2004-07-03 20:11:58
1486 @@ -326,6 +326,7 @@ verb(
1487       --safe-links            ignore "unsafe" symlinks
1488   -H, --hard-links            preserve hard links
1489   -p, --perms                 preserve permissions
1490 + -A, --acls                  preserve ACLs (implies -p) [local option]
1491   -o, --owner                 preserve owner (root only)
1492   -g, --group                 preserve group
1493   -D, --devices               preserve devices (root only)
1494 @@ -578,6 +579,11 @@ source file's permissions and the umask 
1495  other files (including updated files) retain their existing permissions
1496  (which is the same behavior as other file-copy utilities, such as cp).
1497  
1498 +dit(bf(-A, --acls)) This option causes rsync to update the remote
1499 +ACLs to be the same as the local ACLs.  This will work only if the
1500 +remote machine's rsync supports this option also. This is a non-standard
1501 +option.
1502 +
1503  dit(bf(-o, --owner)) This option causes rsync to set the owner of the
1504  destination file to be the same as the source file.  On most systems,
1505  only the super-user can set file ownership.  By default, the preservation
1506 --- orig/smb_acls.h     2004-06-30 00:04:07
1507 +++ smb_acls.h  2004-06-30 00:04:07
1508 @@ -0,0 +1,277 @@
1509 +/* 
1510 +   Unix SMB/Netbios implementation.
1511 +   Version 2.2.x
1512 +   Portable SMB ACL interface
1513 +   Copyright (C) Jeremy Allison 2000
1514 +   
1515 +   This program is free software; you can redistribute it and/or modify
1516 +   it under the terms of the GNU General Public License as published by
1517 +   the Free Software Foundation; either version 2 of the License, or
1518 +   (at your option) any later version.
1519 +   
1520 +   This program is distributed in the hope that it will be useful,
1521 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
1522 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1523 +   GNU General Public License for more details.
1524 +   
1525 +   You should have received a copy of the GNU General Public License
1526 +   along with this program; if not, write to the Free Software
1527 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1528 +*/
1529 +
1530 +#ifndef _SMB_ACLS_H
1531 +#define _SMB_ACLS_H
1532 +
1533 +#if defined(HAVE_POSIX_ACLS)
1534 +
1535 +/* This is an identity mapping (just remove the SMB_). */
1536 +
1537 +#define SMB_ACL_TAG_T          acl_tag_t
1538 +#define SMB_ACL_TYPE_T         acl_type_t
1539 +#define SMB_ACL_PERMSET_T      acl_permset_t
1540 +#define SMB_ACL_PERM_T         acl_perm_t
1541 +#define SMB_ACL_READ           ACL_READ
1542 +#define SMB_ACL_WRITE          ACL_WRITE
1543 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
1544 +
1545 +/* Types of ACLs. */
1546 +#define SMB_ACL_USER           ACL_USER
1547 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
1548 +#define SMB_ACL_GROUP          ACL_GROUP
1549 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
1550 +#define SMB_ACL_OTHER          ACL_OTHER
1551 +#define SMB_ACL_MASK           ACL_MASK
1552 +
1553 +#define SMB_ACL_T              acl_t
1554 +
1555 +#define SMB_ACL_ENTRY_T                acl_entry_t
1556 +
1557 +#define SMB_ACL_FIRST_ENTRY    ACL_FIRST_ENTRY
1558 +#define SMB_ACL_NEXT_ENTRY     ACL_NEXT_ENTRY
1559 +
1560 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
1561 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
1562 +
1563 +#elif defined(HAVE_TRU64_ACLS)
1564 +
1565 +/* This is for DEC/Compaq Tru64 UNIX */
1566 +
1567 +#define SMB_ACL_TAG_T          acl_tag_t
1568 +#define SMB_ACL_TYPE_T         acl_type_t
1569 +#define SMB_ACL_PERMSET_T      acl_permset_t
1570 +#define SMB_ACL_PERM_T         acl_perm_t
1571 +#define SMB_ACL_READ           ACL_READ
1572 +#define SMB_ACL_WRITE          ACL_WRITE
1573 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
1574 +
1575 +/* Types of ACLs. */
1576 +#define SMB_ACL_USER           ACL_USER
1577 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
1578 +#define SMB_ACL_GROUP          ACL_GROUP
1579 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
1580 +#define SMB_ACL_OTHER          ACL_OTHER
1581 +#define SMB_ACL_MASK           ACL_MASK
1582 +
1583 +#define SMB_ACL_T              acl_t
1584 +
1585 +#define SMB_ACL_ENTRY_T                acl_entry_t
1586 +
1587 +#define SMB_ACL_FIRST_ENTRY    0
1588 +#define SMB_ACL_NEXT_ENTRY     1
1589 +
1590 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
1591 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
1592 +
1593 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
1594 +/*
1595 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
1596 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
1597 + */
1598 +
1599 +/* SVR4.2 ES/MP ACLs */
1600 +typedef int SMB_ACL_TAG_T;
1601 +typedef int SMB_ACL_TYPE_T;
1602 +typedef ushort *SMB_ACL_PERMSET_T;
1603 +typedef ushort SMB_ACL_PERM_T;
1604 +#define SMB_ACL_READ           4
1605 +#define SMB_ACL_WRITE          2
1606 +#define SMB_ACL_EXECUTE                1
1607 +
1608 +/* Types of ACLs. */
1609 +#define SMB_ACL_USER           USER
1610 +#define SMB_ACL_USER_OBJ       USER_OBJ
1611 +#define SMB_ACL_GROUP          GROUP
1612 +#define SMB_ACL_GROUP_OBJ      GROUP_OBJ
1613 +#define SMB_ACL_OTHER          OTHER_OBJ
1614 +#define SMB_ACL_MASK           CLASS_OBJ
1615 +
1616 +typedef struct SMB_ACL_T {
1617 +       int size;
1618 +       int count;
1619 +       int next;
1620 +       struct acl acl[1];
1621 +} *SMB_ACL_T;
1622 +
1623 +typedef struct acl *SMB_ACL_ENTRY_T;
1624 +
1625 +#define SMB_ACL_FIRST_ENTRY    0
1626 +#define SMB_ACL_NEXT_ENTRY     1
1627 +
1628 +#define SMB_ACL_TYPE_ACCESS    0
1629 +#define SMB_ACL_TYPE_DEFAULT   1
1630 +
1631 +#elif defined(HAVE_HPUX_ACLS)
1632 +
1633 +/*
1634 + * Based on the Solaris & UnixWare code.
1635 + */
1636 +
1637 +#undef GROUP
1638 +#include <sys/aclv.h>
1639 +
1640 +/* SVR4.2 ES/MP ACLs */
1641 +typedef int SMB_ACL_TAG_T;
1642 +typedef int SMB_ACL_TYPE_T;
1643 +typedef ushort *SMB_ACL_PERMSET_T;
1644 +typedef ushort SMB_ACL_PERM_T;
1645 +#define SMB_ACL_READ           4
1646 +#define SMB_ACL_WRITE          2
1647 +#define SMB_ACL_EXECUTE                1
1648 +
1649 +/* Types of ACLs. */
1650 +#define SMB_ACL_USER           USER
1651 +#define SMB_ACL_USER_OBJ       USER_OBJ
1652 +#define SMB_ACL_GROUP          GROUP
1653 +#define SMB_ACL_GROUP_OBJ      GROUP_OBJ
1654 +#define SMB_ACL_OTHER          OTHER_OBJ
1655 +#define SMB_ACL_MASK           CLASS_OBJ
1656 +
1657 +typedef struct SMB_ACL_T {
1658 +       int size;
1659 +       int count;
1660 +       int next;
1661 +       struct acl acl[1];
1662 +} *SMB_ACL_T;
1663 +
1664 +typedef struct acl *SMB_ACL_ENTRY_T;
1665 +
1666 +#define SMB_ACL_FIRST_ENTRY    0
1667 +#define SMB_ACL_NEXT_ENTRY     1
1668 +
1669 +#define SMB_ACL_TYPE_ACCESS    0
1670 +#define SMB_ACL_TYPE_DEFAULT   1
1671 +
1672 +#elif defined(HAVE_IRIX_ACLS)
1673 +
1674 +#define SMB_ACL_TAG_T          acl_tag_t
1675 +#define SMB_ACL_TYPE_T         acl_type_t
1676 +#define SMB_ACL_PERMSET_T      acl_permset_t
1677 +#define SMB_ACL_PERM_T         acl_perm_t
1678 +#define SMB_ACL_READ           ACL_READ
1679 +#define SMB_ACL_WRITE          ACL_WRITE
1680 +#define SMB_ACL_EXECUTE                ACL_EXECUTE
1681 +
1682 +/* Types of ACLs. */
1683 +#define SMB_ACL_USER           ACL_USER
1684 +#define SMB_ACL_USER_OBJ       ACL_USER_OBJ
1685 +#define SMB_ACL_GROUP          ACL_GROUP
1686 +#define SMB_ACL_GROUP_OBJ      ACL_GROUP_OBJ
1687 +#define SMB_ACL_OTHER          ACL_OTHER_OBJ
1688 +#define SMB_ACL_MASK           ACL_MASK
1689 +
1690 +typedef struct SMB_ACL_T {
1691 +       int next;
1692 +       BOOL freeaclp;
1693 +       struct acl *aclp;
1694 +} *SMB_ACL_T;
1695 +
1696 +#define SMB_ACL_ENTRY_T                acl_entry_t
1697 +
1698 +#define SMB_ACL_FIRST_ENTRY    0
1699 +#define SMB_ACL_NEXT_ENTRY     1
1700 +
1701 +#define SMB_ACL_TYPE_ACCESS    ACL_TYPE_ACCESS
1702 +#define SMB_ACL_TYPE_DEFAULT   ACL_TYPE_DEFAULT
1703 +
1704 +#elif defined(HAVE_AIX_ACLS)
1705 +
1706 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
1707 +
1708 +#include "/usr/include/acl.h"
1709 +
1710 +typedef uint *SMB_ACL_PERMSET_T;
1711
1712 +struct acl_entry_link{
1713 +       struct acl_entry_link *prevp;
1714 +       struct new_acl_entry *entryp;
1715 +       struct acl_entry_link *nextp;
1716 +       int count;
1717 +};
1718 +
1719 +struct new_acl_entry{
1720 +       unsigned short ace_len;
1721 +       unsigned short ace_type;
1722 +       unsigned int ace_access;
1723 +       struct ace_id ace_id[1];
1724 +};
1725 +
1726 +#define SMB_ACL_ENTRY_T                struct new_acl_entry*
1727 +#define SMB_ACL_T              struct acl_entry_link*
1728
1729 +#define SMB_ACL_TAG_T          unsigned short
1730 +#define SMB_ACL_TYPE_T         int
1731 +#define SMB_ACL_PERM_T         uint
1732 +#define SMB_ACL_READ           S_IRUSR
1733 +#define SMB_ACL_WRITE          S_IWUSR
1734 +#define SMB_ACL_EXECUTE                S_IXUSR
1735 +
1736 +/* Types of ACLs. */
1737 +#define SMB_ACL_USER           ACEID_USER
1738 +#define SMB_ACL_USER_OBJ       3
1739 +#define SMB_ACL_GROUP          ACEID_GROUP
1740 +#define SMB_ACL_GROUP_OBJ      4
1741 +#define SMB_ACL_OTHER          5
1742 +#define SMB_ACL_MASK           6
1743 +
1744 +
1745 +#define SMB_ACL_FIRST_ENTRY    1
1746 +#define SMB_ACL_NEXT_ENTRY     2
1747 +
1748 +#define SMB_ACL_TYPE_ACCESS    0
1749 +#define SMB_ACL_TYPE_DEFAULT   1
1750 +
1751 +#else /* No ACLs. */
1752 +
1753 +/* No ACLS - fake it. */
1754 +#define SMB_ACL_TAG_T          int
1755 +#define SMB_ACL_TYPE_T         int
1756 +#define SMB_ACL_PERMSET_T      mode_t
1757 +#define SMB_ACL_PERM_T         mode_t
1758 +#define SMB_ACL_READ           S_IRUSR
1759 +#define SMB_ACL_WRITE          S_IWUSR
1760 +#define SMB_ACL_EXECUTE                S_IXUSR
1761 +
1762 +/* Types of ACLs. */
1763 +#define SMB_ACL_USER           0
1764 +#define SMB_ACL_USER_OBJ       1
1765 +#define SMB_ACL_GROUP          2
1766 +#define SMB_ACL_GROUP_OBJ      3
1767 +#define SMB_ACL_OTHER          4
1768 +#define SMB_ACL_MASK           5
1769 +
1770 +typedef struct SMB_ACL_T {
1771 +       int dummy;
1772 +} *SMB_ACL_T;
1773 +
1774 +typedef struct SMB_ACL_ENTRY_T {
1775 +       int dummy;
1776 +} *SMB_ACL_ENTRY_T;
1777 +
1778 +#define SMB_ACL_FIRST_ENTRY    0
1779 +#define SMB_ACL_NEXT_ENTRY     1
1780 +
1781 +#define SMB_ACL_TYPE_ACCESS    0
1782 +#define SMB_ACL_TYPE_DEFAULT   1
1783 +
1784 +#endif /* No ACLs. */
1785 +#endif /* _SMB_ACLS_H */
1786 --- orig/sysacls.c      2004-08-19 17:38:21
1787 +++ sysacls.c   2004-08-19 17:38:21
1788 @@ -0,0 +1,3117 @@
1789 +/*
1790 +   Unix SMB/Netbios implementation.
1791 +   Version 2.2.
1792 +   Samba system utilities for ACL support.
1793 +   Copyright (C) Jeremy Allison 2000.
1794 +
1795 +   This program is free software; you can redistribute it and/or modify
1796 +   it under the terms of the GNU General Public License as published by
1797 +   the Free Software Foundation; either version 2 of the License, or
1798 +   (at your option) any later version.
1799 +
1800 +   This program is distributed in the hope that it will be useful,
1801 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
1802 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1803 +   GNU General Public License for more details.
1804 +
1805 +   You should have received a copy of the GNU General Public License
1806 +   along with this program; if not, write to the Free Software
1807 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1808 +*/
1809 +
1810 +#include "rsync.h"
1811 +
1812 +/*
1813 + This file wraps all differing system ACL interfaces into a consistent
1814 + one based on the POSIX interface. It also returns the correct errors
1815 + for older UNIX systems that don't support ACLs.
1816 +
1817 + The interfaces that each ACL implementation must support are as follows:
1818 +
1819 + int sys_acl_get_entry(SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1820 + int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1821 + int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
1822 + void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
1823 + SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
1824 + SMB_ACL_T sys_acl_get_fd(int fd)
1825 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
1826 + int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
1827 + char *sys_acl_to_text(SMB_ACL_T theacl, ssize_t *plen)
1828 + SMB_ACL_T sys_acl_init(int count)
1829 + int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1830 + int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1831 + int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual)
1832 + int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1833 + int sys_acl_valid(SMB_ACL_T theacl)
1834 + int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1835 + int sys_acl_set_fd(int fd, SMB_ACL_T theacl)
1836 + int sys_acl_delete_def_file(const char *path)
1837 +
1838 + This next one is not POSIX complient - but we *have* to have it !
1839 + More POSIX braindamage.
1840 +
1841 + int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1842 +
1843 + The generic POSIX free is the following call. We split this into
1844 + several different free functions as we may need to add tag info
1845 + to structures when emulating the POSIX interface.
1846 +
1847 + int sys_acl_free(void *obj_p)
1848 +
1849 + The calls we actually use are:
1850 +
1851 + int sys_acl_free_text(char *text) - free acl_to_text
1852 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
1853 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
1854 +
1855 +*/
1856 +
1857 +#if defined(HAVE_POSIX_ACLS)
1858 +
1859 +/* Identity mapping - easy. */
1860 +
1861 +int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1862 +{
1863 +       return acl_get_entry(the_acl, entry_id, entry_p);
1864 +}
1865 +
1866 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1867 +{
1868 +       return acl_get_tag_type(entry_d, tag_type_p);
1869 +}
1870 +
1871 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1872 +{
1873 +       return acl_get_permset(entry_d, permset_p);
1874 +}
1875 +
1876 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
1877 +{
1878 +       return acl_get_qualifier(entry_d);
1879 +}
1880 +
1881 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
1882 +{
1883 +       return acl_get_file(path_p, type);
1884 +}
1885 +
1886 +SMB_ACL_T sys_acl_get_fd(int fd)
1887 +{
1888 +       return acl_get_fd(fd);
1889 +}
1890 +
1891 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1892 +{
1893 +       return acl_clear_perms(permset);
1894 +}
1895 +
1896 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1897 +{
1898 +       return acl_add_perm(permset, perm);
1899 +}
1900 +
1901 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1902 +{
1903 +#if defined(HAVE_ACL_GET_PERM_NP)
1904 +       /* Required for TrustedBSD-based ACL implementations where
1905 +        * non-POSIX.1e functions are denoted by a _np (non-portable)
1906 +        * suffix. */
1907 +       return acl_get_perm_np(permset, perm);
1908 +#else
1909 +       return acl_get_perm(permset, perm);
1910 +#endif
1911 +}
1912 +
1913 +char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen)
1914 +{
1915 +       return acl_to_text(the_acl, plen);
1916 +}
1917 +
1918 +SMB_ACL_T sys_acl_init(int count)
1919 +{
1920 +       return acl_init(count);
1921 +}
1922 +
1923 +int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1924 +{
1925 +       return acl_create_entry(pacl, pentry);
1926 +}
1927 +
1928 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1929 +{
1930 +       return acl_set_tag_type(entry, tagtype);
1931 +}
1932 +
1933 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual)
1934 +{
1935 +       return acl_set_qualifier(entry, qual);
1936 +}
1937 +
1938 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1939 +{
1940 +       return acl_set_permset(entry, permset);
1941 +}
1942 +
1943 +int sys_acl_valid(SMB_ACL_T theacl)
1944 +{
1945 +       return acl_valid(theacl);
1946 +}
1947 +
1948 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1949 +{
1950 +       return acl_set_file(name, acltype, theacl);
1951 +}
1952 +
1953 +int sys_acl_set_fd(int fd, SMB_ACL_T theacl)
1954 +{
1955 +       return acl_set_fd(fd, theacl);
1956 +}
1957 +
1958 +int sys_acl_delete_def_file(const char *name)
1959 +{
1960 +       return acl_delete_def_file(name);
1961 +}
1962 +
1963 +int sys_acl_free_text(char *text)
1964 +{
1965 +       return acl_free(text);
1966 +}
1967 +
1968 +int sys_acl_free_acl(SMB_ACL_T the_acl)
1969 +{
1970 +       return acl_free(the_acl);
1971 +}
1972 +
1973 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
1974 +{
1975 +       return acl_free(qual);
1976 +}
1977 +
1978 +#elif defined(HAVE_TRU64_ACLS)
1979 +/* The interface to DEC/Compaq Tru64 UNIX ACLs
1980 + * is based on Draft 13 of the POSIX spec which is
1981 + * slightly different from the Draft 16 interface.
1982 + *
1983 + * Also, some of the permset manipulation functions
1984 + * such as acl_clear_perm() and acl_add_perm() appear
1985 + * to be broken on Tru64 so we have to manipulate
1986 + * the permission bits in the permset directly. */
1987 + int sys_acl_get_entry(SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1988 +{
1989 +       SMB_ACL_ENTRY_T entry;
1990 +
1991 +       if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
1992 +               return -1;
1993 +       }
1994 +
1995 +       errno = 0;
1996 +       if ((entry = acl_get_entry(the_acl)) != NULL) {
1997 +               *entry_p = entry;
1998 +               return 1;
1999 +       }
2000 +
2001 +       return errno ? -1 : 0;
2002 +}
2003 +
2004 + int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
2005 +{
2006 +       return acl_get_tag_type(entry_d, tag_type_p);
2007 +}
2008 +
2009 + int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2010 +{
2011 +       return acl_get_permset(entry_d, permset_p);
2012 +}
2013 +
2014 + void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2015 +{
2016 +       return acl_get_qualifier(entry_d);
2017 +}
2018 +
2019 + SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2020 +{
2021 +       return acl_get_file((char *)path_p, type);
2022 +}
2023 +
2024 + SMB_ACL_T sys_acl_get_fd(int fd)
2025 +{
2026 +       return acl_get_fd(fd, ACL_TYPE_ACCESS);
2027 +}
2028 +
2029 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
2030 +{
2031 +       *permset = 0;           /* acl_clear_perm() is broken on Tru64 */
2032 +
2033 +       return 0;
2034 +}
2035 +
2036 + int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2037 +{
2038 +       if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
2039 +               errno = EINVAL;
2040 +               return -1;
2041 +       }
2042 +
2043 +       *permset |= perm;       /* acl_add_perm() is broken on Tru64 */
2044 +
2045 +       return 0;
2046 +}
2047 +
2048 + int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
2049 +{
2050 +       return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
2051 +}
2052 +
2053 + char *sys_acl_to_text(SMB_ACL_T the_acl, ssize_t *plen)
2054 +{
2055 +       return acl_to_text(the_acl, plen);
2056 +}
2057 +
2058 + SMB_ACL_T sys_acl_init(int count)
2059 +{
2060 +       return acl_init(count);
2061 +}
2062 +
2063 + int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
2064 +{
2065 +       SMB_ACL_ENTRY_T entry;
2066 +
2067 +       if ((entry = acl_create_entry(pacl)) == NULL) {
2068 +               return -1;
2069 +       }
2070 +
2071 +       *pentry = entry;
2072 +       return 0;
2073 +}
2074 +
2075 + int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
2076 +{
2077 +       return acl_set_tag_type(entry, tagtype);
2078 +}
2079 +
2080 + int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual)
2081 +{
2082 +       return acl_set_qualifier(entry, qual);
2083 +}
2084 +
2085 + int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
2086 +{
2087 +       return acl_set_permset(entry, permset);
2088 +}
2089 +
2090 + int sys_acl_valid(SMB_ACL_T theacl)
2091 +{
2092 +       acl_entry_t entry;
2093 +
2094 +       return acl_valid(theacl, &entry);
2095 +}
2096 +
2097 + int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2098 +{
2099 +       return acl_set_file((char *)name, acltype, theacl);
2100 +}
2101 +
2102 + int sys_acl_set_fd(int fd, SMB_ACL_T theacl)
2103 +{
2104 +       return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
2105 +}
2106 +
2107 + int sys_acl_delete_def_file(const char *name)
2108 +{
2109 +       return acl_delete_def_file((char *)name);
2110 +}
2111 +
2112 + int sys_acl_free_text(char *text)
2113 +{
2114 +       /* (void) cast and explicit return 0 are for DEC UNIX
2115 +        *  which just #defines acl_free_text() to be free(). */
2116 +       (void) acl_free_text(text);
2117 +       return 0;
2118 +}
2119 +
2120 + int sys_acl_free_acl(SMB_ACL_T the_acl)
2121 +{
2122 +       return acl_free(the_acl);
2123 +}
2124 +
2125 + int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2126 +{
2127 +       return acl_free_qualifier(qual, tagtype);
2128 +}
2129 +
2130 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
2131 +
2132 +/* Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
2133 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris. */
2134 +
2135 +/* Note that while this code implements sufficient functionality
2136 + * to support the sys_acl_* interfaces it does not provide all
2137 + * of the semantics of the POSIX ACL interfaces.
2138 + *
2139 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2140 + * from a call to sys_acl_get_entry() should not be assumed to be
2141 + * valid after calling any of the following functions, which may
2142 + * reorder the entries in the ACL.
2143 + *
2144 + *     sys_acl_valid()
2145 + *     sys_acl_set_file()
2146 + *     sys_acl_set_fd()
2147 + */
2148 +
2149 +/* The only difference between Solaris and UnixWare / OpenUNIX is
2150 + * that the #defines for the ACL operations have different names. */
2151 +#if defined(HAVE_UNIXWARE_ACLS)
2152 +
2153 +#define SETACL         ACL_SET
2154 +#define GETACL         ACL_GET
2155 +#define GETACLCNT      ACL_CNT
2156 +
2157 +#endif
2158 +
2159 +
2160 + int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2161 +{
2162 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2163 +               errno = EINVAL;
2164 +               return -1;
2165 +       }
2166 +
2167 +       if (entry_p == NULL) {
2168 +               errno = EINVAL;
2169 +               return -1;
2170 +       }
2171 +
2172 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
2173 +               acl_d->next = 0;
2174 +       }
2175 +
2176 +       if (acl_d->next < 0) {
2177 +               errno = EINVAL;
2178 +               return -1;
2179 +       }
2180 +
2181 +       if (acl_d->next >= acl_d->count) {
2182 +               return 0;
2183 +       }
2184 +
2185 +       *entry_p = &acl_d->acl[acl_d->next++];
2186 +
2187 +       return 1;
2188 +}
2189 +
2190 + int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2191 +{
2192 +       *type_p = entry_d->a_type;
2193 +
2194 +       return 0;
2195 +}
2196 +
2197 + int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2198 +{
2199 +       *permset_p = &entry_d->a_perm;
2200 +
2201 +       return 0;
2202 +}
2203 +
2204 + void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2205 +{
2206 +       if (entry_d->a_type != SMB_ACL_USER
2207 +           && entry_d->a_type != SMB_ACL_GROUP) {
2208 +               errno = EINVAL;
2209 +               return NULL;
2210 +       }
2211 +
2212 +       return &entry_d->a_id;
2213 +}
2214 +
2215 +/* There is no way of knowing what size the ACL returned by
2216 + * GETACL will be unless you first call GETACLCNT which means
2217 + * making an additional system call.
2218 + *
2219 + * In the hope of avoiding the cost of the additional system
2220 + * call in most cases, we initially allocate enough space for
2221 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2222 + * be too small then we use GETACLCNT to find out the actual
2223 + * size, reallocate the ACL buffer, and then call GETACL again. */
2224 +
2225 +#define INITIAL_ACL_SIZE       16
2226 +
2227 + SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2228 +{
2229 +       SMB_ACL_T acl_d;
2230 +       int count;              /* # of ACL entries allocated */
2231 +       int naccess;            /* # of access ACL entries */
2232 +       int ndefault;           /* # of default ACL entries */
2233 +
2234 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2235 +               errno = EINVAL;
2236 +               return NULL;
2237 +       }
2238 +
2239 +       count = INITIAL_ACL_SIZE;
2240 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2241 +               return NULL;
2242 +       }
2243 +
2244 +       /* If there isn't enough space for the ACL entries we use
2245 +        * GETACLCNT to determine the actual number of ACL entries
2246 +        * reallocate and try again. This is in a loop because it
2247 +        * is possible that someone else could modify the ACL and
2248 +        * increase the number of entries between the call to
2249 +        * GETACLCNT and the call to GETACL. */
2250 +       while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
2251 +           && errno == ENOSPC) {
2252 +
2253 +               sys_acl_free_acl(acl_d);
2254 +
2255 +               if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
2256 +                       return NULL;
2257 +               }
2258 +
2259 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2260 +                       return NULL;
2261 +               }
2262 +       }
2263 +
2264 +       if (count < 0) {
2265 +               sys_acl_free_acl(acl_d);
2266 +               return NULL;
2267 +       }
2268 +
2269 +       /* Calculate the number of access and default ACL entries.
2270 +        *
2271 +        * Note: we assume that the acl() system call returned a
2272 +        * well formed ACL which is sorted so that all of the
2273 +        * access ACL entries preceed any default ACL entries. */
2274 +       for (naccess = 0; naccess < count; naccess++) {
2275 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2276 +                       break;
2277 +       }
2278 +       ndefault = count - naccess;
2279 +
2280 +       /* If the caller wants the default ACL we have to copy
2281 +        * the entries down to the start of the acl[] buffer
2282 +        * and mask out the ACL_DEFAULT flag from the type field. */
2283 +       if (type == SMB_ACL_TYPE_DEFAULT) {
2284 +               int i, j;
2285 +
2286 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
2287 +                       acl_d->acl[i] = acl_d->acl[j];
2288 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2289 +               }
2290 +
2291 +               acl_d->count = ndefault;
2292 +       } else {
2293 +               acl_d->count = naccess;
2294 +       }
2295 +
2296 +       return acl_d;
2297 +}
2298 +
2299 + SMB_ACL_T sys_acl_get_fd(int fd)
2300 +{
2301 +       SMB_ACL_T acl_d;
2302 +       int count;              /* # of ACL entries allocated */
2303 +       int naccess;            /* # of access ACL entries */
2304 +
2305 +       count = INITIAL_ACL_SIZE;
2306 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2307 +               return NULL;
2308 +       }
2309 +
2310 +       while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
2311 +           && errno == ENOSPC) {
2312 +
2313 +               sys_acl_free_acl(acl_d);
2314 +
2315 +               if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
2316 +                       return NULL;
2317 +               }
2318 +
2319 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2320 +                       return NULL;
2321 +               }
2322 +       }
2323 +
2324 +       if (count < 0) {
2325 +               sys_acl_free_acl(acl_d);
2326 +               return NULL;
2327 +       }
2328 +
2329 +       /* Calculate the number of access ACL entries. */
2330 +       for (naccess = 0; naccess < count; naccess++) {
2331 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2332 +                       break;
2333 +       }
2334 +
2335 +       acl_d->count = naccess;
2336 +
2337 +       return acl_d;
2338 +}
2339 +
2340 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2341 +{
2342 +       *permset_d = 0;
2343 +
2344 +       return 0;
2345 +}
2346 +
2347 + int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2348 +{
2349 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2350 +           && perm != SMB_ACL_EXECUTE) {
2351 +               errno = EINVAL;
2352 +               return -1;
2353 +       }
2354 +
2355 +       if (permset_d == NULL) {
2356 +               errno = EINVAL;
2357 +               return -1;
2358 +       }
2359 +
2360 +       *permset_d |= perm;
2361 +
2362 +       return 0;
2363 +}
2364 +
2365 + int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2366 +{
2367 +       return *permset_d & perm;
2368 +}
2369 +
2370 + char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2371 +{
2372 +       int i;
2373 +       int len, maxlen;
2374 +       char *text;
2375 +
2376 +       /* Use an initial estimate of 20 bytes per ACL entry
2377 +        * when allocating memory for the text representation
2378 +        * of the ACL. */
2379 +       len = 0;
2380 +       maxlen = 20 * acl_d->count;
2381 +       if ((text = malloc(maxlen)) == NULL) {
2382 +               errno = ENOMEM;
2383 +               return NULL;
2384 +       }
2385 +
2386 +       for (i = 0; i < acl_d->count; i++) {
2387 +               struct acl *ap = &acl_d->acl[i];
2388 +               struct passwd *pw;
2389 +               struct group *gr;
2390 +               char tagbuf[12];
2391 +               char idbuf[12];
2392 +               char *tag;
2393 +               char *id = "";
2394 +               char perms[4];
2395 +               int nbytes;
2396 +
2397 +               switch (ap->a_type) {
2398 +                       /* For debugging purposes it's probably more
2399 +                        * useful to dump unknown tag types rather
2400 +                        * than just returning an error. */
2401 +                       default:
2402 +                               snprintf(tagbuf, sizeof tagbuf - 1, "0x%x",
2403 +                                       ap->a_type);
2404 +                               tag = tagbuf;
2405 +                               snprintf(idbuf, sizeof idbuf - 1, "%ld",
2406 +                                       (long)ap->a_id);
2407 +                               id = idbuf;
2408 +                               break;
2409 +
2410 +                       case SMB_ACL_USER:
2411 +                               if ((pw = getpwuid(ap->a_id)) == NULL) {
2412 +                                       snprintf(idbuf, sizeof idbuf - 1, "%ld",
2413 +                                               (long)ap->a_id);
2414 +                                       id = idbuf;
2415 +                               } else {
2416 +                                       id = pw->pw_name;
2417 +                               }
2418 +                       case SMB_ACL_USER_OBJ:
2419 +                               tag = "user";
2420 +                               break;
2421 +
2422 +                       case SMB_ACL_GROUP:
2423 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
2424 +                                       snprintf(idbuf, sizeof idbuf - 1, "%ld",
2425 +                                               (long)ap->a_id);
2426 +                                       id = idbuf;
2427 +                               } else {
2428 +                                       id = gr->gr_name;
2429 +                               }
2430 +                       case SMB_ACL_GROUP_OBJ:
2431 +                               tag = "group";
2432 +                               break;
2433 +
2434 +                       case SMB_ACL_OTHER:
2435 +                               tag = "other";
2436 +                               break;
2437 +
2438 +                       case SMB_ACL_MASK:
2439 +                               tag = "mask";
2440 +                               break;
2441 +
2442 +               }
2443 +
2444 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2445 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2446 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2447 +               perms[3] = '\0';
2448 +
2449 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
2450 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2451 +
2452 +               /* If this entry would overflow the buffer
2453 +                * allocate enough additional memory for this
2454 +                * entry and an estimate of another 20 bytes
2455 +                * for each entry still to be processed. */
2456 +               if ((len + nbytes) > maxlen) {
2457 +                       char *oldtext = text;
2458 +
2459 +                       maxlen += nbytes + 20 * (acl_d->count - i);
2460 +
2461 +                       if ((text = Realloc(oldtext, maxlen)) == NULL) {
2462 +                               free(oldtext);
2463 +                               errno = ENOMEM;
2464 +                               return NULL;
2465 +                       }
2466 +               }
2467 +
2468 +               snprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2469 +               len += nbytes - 1;
2470 +       }
2471 +
2472 +       if (len_p)
2473 +               *len_p = len;
2474 +
2475 +       return text;
2476 +}
2477 +
2478 + SMB_ACL_T sys_acl_init(int count)
2479 +{
2480 +       SMB_ACL_T a;
2481 +
2482 +       if (count < 0) {
2483 +               errno = EINVAL;
2484 +               return NULL;
2485 +       }
2486 +
2487 +       /* Note that since the definition of the structure pointed
2488 +        * to by the SMB_ACL_T includes the first element of the
2489 +        * acl[] array, this actually allocates an ACL with room
2490 +        * for (count+1) entries. */
2491 +       if ((a = malloc(sizeof a[0] + count * sizeof (struct acl))) == NULL) {
2492 +               errno = ENOMEM;
2493 +               return NULL;
2494 +       }
2495 +
2496 +       a->size = count + 1;
2497 +       a->count = 0;
2498 +       a->next = -1;
2499 +
2500 +       return a;
2501 +}
2502 +
2503 +
2504 + int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2505 +{
2506 +       SMB_ACL_T acl_d;
2507 +       SMB_ACL_ENTRY_T entry_d;
2508 +
2509 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2510 +               errno = EINVAL;
2511 +               return -1;
2512 +       }
2513 +
2514 +       if (acl_d->count >= acl_d->size) {
2515 +               errno = ENOSPC;
2516 +               return -1;
2517 +       }
2518 +
2519 +       entry_d = &acl_d->acl[acl_d->count++];
2520 +       entry_d->a_type = 0;
2521 +       entry_d->a_id = -1;
2522 +       entry_d->a_perm = 0;
2523 +       *entry_p = entry_d;
2524 +
2525 +       return 0;
2526 +}
2527 +
2528 + int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2529 +{
2530 +       switch (tag_type) {
2531 +               case SMB_ACL_USER:
2532 +               case SMB_ACL_USER_OBJ:
2533 +               case SMB_ACL_GROUP:
2534 +               case SMB_ACL_GROUP_OBJ:
2535 +               case SMB_ACL_OTHER:
2536 +               case SMB_ACL_MASK:
2537 +                       entry_d->a_type = tag_type;
2538 +                       break;
2539 +               default:
2540 +                       errno = EINVAL;
2541 +                       return -1;
2542 +       }
2543 +
2544 +       return 0;
2545 +}
2546 +
2547 + int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2548 +{
2549 +       if (entry_d->a_type != SMB_ACL_GROUP
2550 +           && entry_d->a_type != SMB_ACL_USER) {
2551 +               errno = EINVAL;
2552 +               return -1;
2553 +       }
2554 +
2555 +       entry_d->a_id = *((id_t *)qual_p);
2556 +
2557 +       return 0;
2558 +}
2559 +
2560 + int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2561 +{
2562 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2563 +               return EINVAL;
2564 +       }
2565 +
2566 +       entry_d->a_perm = *permset_d;
2567 +
2568 +       return 0;
2569 +}
2570 +
2571 +/* Sort the ACL and check it for validity.
2572 + *
2573 + * If it's a minimal ACL with only 4 entries then we
2574 + * need to recalculate the mask permissions to make
2575 + * sure that they are the same as the GROUP_OBJ
2576 + * permissions as required by the UnixWare acl() system call.
2577 + *
2578 + * (Note: since POSIX allows minimal ACLs which only contain
2579 + * 3 entries - ie there is no mask entry - we should, in theory,
2580 + * check for this and add a mask entry if necessary - however
2581 + * we "know" that the caller of this interface always specifies
2582 + * a mask so, in practice "this never happens" (tm) - if it *does*
2583 + * happen aclsort() will fail and return an error and someone will
2584 + * have to fix it ...) */
2585 +
2586 +static int acl_sort(SMB_ACL_T acl_d)
2587 +{
2588 +       int     fixmask = (acl_d->count <= 4);
2589 +
2590 +       if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
2591 +               errno = EINVAL;
2592 +               return -1;
2593 +       }
2594 +       return 0;
2595 +}
2596 +
2597 + int sys_acl_valid(SMB_ACL_T acl_d)
2598 +{
2599 +       return acl_sort(acl_d);
2600 +}
2601 +
2602 + int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
2603 +{
2604 +       struct stat s;
2605 +       struct acl *acl_p;
2606 +       int acl_count;
2607 +       struct acl *acl_buf = NULL;
2608 +       int ret;
2609 +
2610 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2611 +               errno = EINVAL;
2612 +               return -1;
2613 +       }
2614 +
2615 +       if (acl_sort(acl_d) != 0) {
2616 +               return -1;
2617 +       }
2618 +
2619 +       acl_p = &acl_d->acl[0];
2620 +       acl_count = acl_d->count;
2621 +
2622 +       /* If it's a directory there is extra work to do since the acl()
2623 +        * system call will replace both the access ACLs and the default
2624 +        * ACLs (if any). */
2625 +       if (stat(name, &s) != 0) {
2626 +               return -1;
2627 +       }
2628 +       if (S_ISDIR(s.st_mode)) {
2629 +               SMB_ACL_T acc_acl;
2630 +               SMB_ACL_T def_acl;
2631 +               SMB_ACL_T tmp_acl;
2632 +               int i;
2633 +
2634 +               if (type == SMB_ACL_TYPE_ACCESS) {
2635 +                       acc_acl = acl_d;
2636 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
2637 +
2638 +               } else {
2639 +                       def_acl = acl_d;
2640 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
2641 +               }
2642 +
2643 +               if (tmp_acl == NULL) {
2644 +                       return -1;
2645 +               }
2646 +
2647 +               /* Allocate a temporary buffer for the complete ACL. */
2648 +               acl_count = acc_acl->count + def_acl->count;
2649 +               acl_p = acl_buf = malloc(acl_count * sizeof acl_buf[0]);
2650 +
2651 +               if (acl_buf == NULL) {
2652 +                       sys_acl_free_acl(tmp_acl);
2653 +                       errno = ENOMEM;
2654 +                       return -1;
2655 +               }
2656 +
2657 +               /* Copy the access control and default entries into the buffer. */
2658 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
2659 +                       acc_acl->count * sizeof acl_buf[0]);
2660 +
2661 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
2662 +                       def_acl->count * sizeof acl_buf[0]);
2663 +
2664 +               /* Set the ACL_DEFAULT flag on the default entries. */
2665 +               for (i = acc_acl->count; i < acl_count; i++) {
2666 +                       acl_buf[i].a_type |= ACL_DEFAULT;
2667 +               }
2668 +
2669 +               sys_acl_free_acl(tmp_acl);
2670 +
2671 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
2672 +               errno = EINVAL;
2673 +               return -1;
2674 +       }
2675 +
2676 +       ret = acl(name, SETACL, acl_count, acl_p);
2677 +
2678 +       free(acl_buf);
2679 +
2680 +       return ret;
2681 +}
2682 +
2683 + int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
2684 +{
2685 +       if (acl_sort(acl_d) != 0) {
2686 +               return -1;
2687 +       }
2688 +
2689 +       return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
2690 +}
2691 +
2692 + int sys_acl_delete_def_file(const char *path)
2693 +{
2694 +       SMB_ACL_T acl_d;
2695 +       int ret;
2696 +
2697 +       /* Fetching the access ACL and rewriting it has the effect of
2698 +        * deleting the default ACL. */
2699 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
2700 +               return -1;
2701 +       }
2702 +
2703 +       ret = acl(path, SETACL, acl_d->count, acl_d->acl);
2704 +
2705 +       sys_acl_free_acl(acl_d);
2706 +
2707 +       return ret;
2708 +}
2709 +
2710 + int sys_acl_free_text(char *text)
2711 +{
2712 +       free(text);
2713 +       return 0;
2714 +}
2715 +
2716 + int sys_acl_free_acl(SMB_ACL_T acl_d)
2717 +{
2718 +       free(acl_d);
2719 +       return 0;
2720 +}
2721 +
2722 + int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2723 +{
2724 +       return 0;
2725 +}
2726 +
2727 +#elif defined(HAVE_HPUX_ACLS)
2728 +#include <dl.h>
2729 +
2730 +/* Based on the Solaris/SCO code - with modifications. */
2731 +
2732 +/* Note that while this code implements sufficient functionality
2733 + * to support the sys_acl_* interfaces it does not provide all
2734 + * of the semantics of the POSIX ACL interfaces.
2735 + *
2736 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2737 + * from a call to sys_acl_get_entry() should not be assumed to be
2738 + * valid after calling any of the following functions, which may
2739 + * reorder the entries in the ACL.
2740 + *
2741 + *     sys_acl_valid()
2742 + *     sys_acl_set_file()
2743 + *     sys_acl_set_fd()
2744 + */
2745 +
2746 +/* This checks if the POSIX ACL system call is defined which basically
2747 + * corresponds to whether JFS 3.3 or higher is installed. If acl() was
2748 + * called when it isn't defined, it causes the process to core dump so
2749 + * it is important to check this and avoid acl() calls if it isn't
2750 + * there. */
2751 +
2752 +static BOOL hpux_acl_call_presence(void)
2753 +{
2754 +
2755 +       shl_t handle = NULL;
2756 +       void *value;
2757 +       int ret_val=0;
2758 +       static BOOL already_checked=0;
2759 +
2760 +       if (already_checked)
2761 +               return True;
2762 +
2763 +
2764 +       ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
2765 +
2766 +       if (ret_val != 0) {
2767 +               DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
2768 +                       ret_val, errno, strerror(errno)));
2769 +               DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
2770 +               return False;
2771 +       }
2772 +
2773 +       DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
2774 +
2775 +       already_checked = True;
2776 +       return True;
2777 +}
2778 +
2779 + int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2780 +{
2781 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2782 +               errno = EINVAL;
2783 +               return -1;
2784 +       }
2785 +
2786 +       if (entry_p == NULL) {
2787 +               errno = EINVAL;
2788 +               return -1;
2789 +       }
2790 +
2791 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
2792 +               acl_d->next = 0;
2793 +       }
2794 +
2795 +       if (acl_d->next < 0) {
2796 +               errno = EINVAL;
2797 +               return -1;
2798 +       }
2799 +
2800 +       if (acl_d->next >= acl_d->count) {
2801 +               return 0;
2802 +       }
2803 +
2804 +       *entry_p = &acl_d->acl[acl_d->next++];
2805 +
2806 +       return 1;
2807 +}
2808 +
2809 + int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2810 +{
2811 +       *type_p = entry_d->a_type;
2812 +
2813 +       return 0;
2814 +}
2815 +
2816 + int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2817 +{
2818 +       *permset_p = &entry_d->a_perm;
2819 +
2820 +       return 0;
2821 +}
2822 +
2823 + void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2824 +{
2825 +       if (entry_d->a_type != SMB_ACL_USER
2826 +           && entry_d->a_type != SMB_ACL_GROUP) {
2827 +               errno = EINVAL;
2828 +               return NULL;
2829 +       }
2830 +
2831 +       return &entry_d->a_id;
2832 +}
2833 +
2834 +/* There is no way of knowing what size the ACL returned by
2835 + * ACL_GET will be unless you first call ACL_CNT which means
2836 + * making an additional system call.
2837 + *
2838 + * In the hope of avoiding the cost of the additional system
2839 + * call in most cases, we initially allocate enough space for
2840 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2841 + * be too small then we use ACL_CNT to find out the actual
2842 + * size, reallocate the ACL buffer, and then call ACL_GET again.
2843 + */
2844 +
2845 +#define INITIAL_ACL_SIZE       16
2846 +
2847 + SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2848 +{
2849 +       SMB_ACL_T acl_d;
2850 +       int count;              /* # of ACL entries allocated */
2851 +       int naccess;            /* # of access ACL entries */
2852 +       int ndefault;           /* # of default ACL entries */
2853 +
2854 +       if (hpux_acl_call_presence() == False) {
2855 +               /* Looks like we don't have the acl() system call on HPUX.
2856 +                * May be the system doesn't have the latest version of JFS. */
2857 +               return NULL;
2858 +       }
2859 +
2860 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2861 +               errno = EINVAL;
2862 +               return NULL;
2863 +       }
2864 +
2865 +       count = INITIAL_ACL_SIZE;
2866 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2867 +               return NULL;
2868 +       }
2869 +
2870 +       /* If there isn't enough space for the ACL entries we use
2871 +        * ACL_CNT to determine the actual number of ACL entries
2872 +        * reallocate and try again. This is in a loop because it
2873 +        * is possible that someone else could modify the ACL and
2874 +        * increase the number of entries between the call to
2875 +        * ACL_CNT and the call to ACL_GET. */
2876 +       while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
2877 +
2878 +               sys_acl_free_acl(acl_d);
2879 +
2880 +               if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
2881 +                       return NULL;
2882 +               }
2883 +
2884 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2885 +                       return NULL;
2886 +               }
2887 +       }
2888 +
2889 +       if (count < 0) {
2890 +               sys_acl_free_acl(acl_d);
2891 +               return NULL;
2892 +       }
2893 +
2894 +       /* Calculate the number of access and default ACL entries.
2895 +        *
2896 +        * Note: we assume that the acl() system call returned a
2897 +        * well formed ACL which is sorted so that all of the
2898 +        * access ACL entries preceed any default ACL entries. */
2899 +       for (naccess = 0; naccess < count; naccess++) {
2900 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2901 +                       break;
2902 +       }
2903 +       ndefault = count - naccess;
2904 +
2905 +       /* If the caller wants the default ACL we have to copy
2906 +        * the entries down to the start of the acl[] buffer
2907 +        * and mask out the ACL_DEFAULT flag from the type field. */
2908 +       if (type == SMB_ACL_TYPE_DEFAULT) {
2909 +               int i, j;
2910 +
2911 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
2912 +                       acl_d->acl[i] = acl_d->acl[j];
2913 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2914 +               }
2915 +
2916 +               acl_d->count = ndefault;
2917 +       } else {
2918 +               acl_d->count = naccess;
2919 +       }
2920 +
2921 +       return acl_d;
2922 +}
2923 +
2924 + SMB_ACL_T sys_acl_get_fd(int fd)
2925 +{
2926 +       /* HPUX doesn't have the facl call. Fake it using the path.... JRA. */
2927 +
2928 +       files_struct *fsp = file_find_fd(fd);
2929 +
2930 +       if (fsp == NULL) {
2931 +               errno = EBADF;
2932 +               return NULL;
2933 +       }
2934 +
2935 +       /* We know we're in the same conn context. So we can use the
2936 +        * relative path. */
2937 +
2938 +       return sys_acl_get_file(dos_to_unix_static(fsp->fsp_name), SMB_ACL_TYPE_ACCESS);
2939 +}
2940 +
2941 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2942 +{
2943 +       *permset_d = 0;
2944 +
2945 +       return 0;
2946 +}
2947 +
2948 + int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2949 +{
2950 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2951 +           && perm != SMB_ACL_EXECUTE) {
2952 +               errno = EINVAL;
2953 +               return -1;
2954 +       }
2955 +
2956 +       if (permset_d == NULL) {
2957 +               errno = EINVAL;
2958 +               return -1;
2959 +       }
2960 +
2961 +       *permset_d |= perm;
2962 +
2963 +       return 0;
2964 +}
2965 +
2966 + int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2967 +{
2968 +       return *permset_d & perm;
2969 +}
2970 +
2971 + char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2972 +{
2973 +       int i;
2974 +       int len, maxlen;
2975 +       char *text;
2976 +
2977 +       /* Use an initial estimate of 20 bytes per ACL entry when
2978 +        * allocating memory for the text representation of the ACL. */
2979 +       len = 0;
2980 +       maxlen = 20 * acl_d->count;
2981 +       if ((text = malloc(maxlen)) == NULL) {
2982 +               errno = ENOMEM;
2983 +               return NULL;
2984 +       }
2985 +
2986 +       for (i = 0; i < acl_d->count; i++) {
2987 +               struct acl *ap = &acl_d->acl[i];
2988 +               struct passwd *pw;
2989 +               struct group *gr;
2990 +               char tagbuf[12];
2991 +               char idbuf[12];
2992 +               char *tag;
2993 +               char *id = "";
2994 +               char perms[4];
2995 +               int nbytes;
2996 +
2997 +               switch (ap->a_type) {
2998 +                       /* For debugging purposes it's probably more
2999 +                        * useful to dump unknown tag types rather
3000 +                        * than just returning an error. */
3001 +                       default:
3002 +                               snprintf(tagbuf, sizeof tagbuf - 1, "0x%x",
3003 +                                       ap->a_type);
3004 +                               tag = tagbuf;
3005 +                               snprintf(idbuf, sizeof idbuf - 1, "%ld",
3006 +                                       (long)ap->a_id);
3007 +                               id = idbuf;
3008 +                               break;
3009 +
3010 +                       case SMB_ACL_USER:
3011 +                               if ((pw = getpwuid(ap->a_id)) == NULL) {
3012 +                                       snprintf(idbuf, sizeof idbuf - 1, "%ld",
3013 +                                               (long)ap->a_id);
3014 +                                       id = idbuf;
3015 +                               } else {
3016 +                                       id = pw->pw_name;
3017 +                               }
3018 +                       case SMB_ACL_USER_OBJ:
3019 +                               tag = "user";
3020 +                               break;
3021 +
3022 +                       case SMB_ACL_GROUP:
3023 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
3024 +                                       snprintf(idbuf, sizeof idbuf - 1, "%ld",
3025 +                                               (long)ap->a_id);
3026 +                                       id = idbuf;
3027 +                               } else {
3028 +                                       id = gr->gr_name;
3029 +                               }
3030 +                       case SMB_ACL_GROUP_OBJ:
3031 +                               tag = "group";
3032 +                               break;
3033 +
3034 +                       case SMB_ACL_OTHER:
3035 +                               tag = "other";
3036 +                               break;
3037 +
3038 +                       case SMB_ACL_MASK:
3039 +                               tag = "mask";
3040 +                               break;
3041 +
3042 +               }
3043 +
3044 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
3045 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
3046 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
3047 +               perms[3] = '\0';
3048 +
3049 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
3050 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
3051 +
3052 +               /* If this entry would overflow the buffer
3053 +                * allocate enough additional memory for this
3054 +                * entry and an estimate of another 20 bytes
3055 +                * for each entry still to be processed. */
3056 +               if ((len + nbytes) > maxlen) {
3057 +                       char *oldtext = text;
3058 +
3059 +                       maxlen += nbytes + 20 * (acl_d->count - i);
3060 +
3061 +                       if ((text = Realloc(oldtext, maxlen)) == NULL) {
3062 +                               free(oldtext);
3063 +                               errno = ENOMEM;
3064 +                               return NULL;
3065 +                       }
3066 +               }
3067 +
3068 +               snprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
3069 +               len += nbytes - 1;
3070 +       }
3071 +
3072 +       if (len_p)
3073 +               *len_p = len;
3074 +
3075 +       return text;
3076 +}
3077 +
3078 + SMB_ACL_T sys_acl_init(int count)
3079 +{
3080 +       SMB_ACL_T a;
3081 +
3082 +       if (count < 0) {
3083 +               errno = EINVAL;
3084 +               return NULL;
3085 +       }
3086 +
3087 +       /* Note that since the definition of the structure pointed
3088 +        * to by the SMB_ACL_T includes the first element of the
3089 +        * acl[] array, this actually allocates an ACL with room
3090 +        * for (count+1) entries. */
3091 +       if ((a = malloc(sizeof a[0] + count * sizeof (struct acl))) == NULL) {
3092 +               errno = ENOMEM;
3093 +               return NULL;
3094 +       }
3095 +
3096 +       a->size = count + 1;
3097 +       a->count = 0;
3098 +       a->next = -1;
3099 +
3100 +       return a;
3101 +}
3102 +
3103 +
3104 + int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3105 +{
3106 +       SMB_ACL_T acl_d;
3107 +       SMB_ACL_ENTRY_T entry_d;
3108 +
3109 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3110 +               errno = EINVAL;
3111 +               return -1;
3112 +       }
3113 +
3114 +       if (acl_d->count >= acl_d->size) {
3115 +               errno = ENOSPC;
3116 +               return -1;
3117 +       }
3118 +
3119 +       entry_d = &acl_d->acl[acl_d->count++];
3120 +       entry_d->a_type = 0;
3121 +       entry_d->a_id = -1;
3122 +       entry_d->a_perm = 0;
3123 +       *entry_p = entry_d;
3124 +
3125 +       return 0;
3126 +}
3127 +
3128 + int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3129 +{
3130 +       switch (tag_type) {
3131 +               case SMB_ACL_USER:
3132 +               case SMB_ACL_USER_OBJ:
3133 +               case SMB_ACL_GROUP:
3134 +               case SMB_ACL_GROUP_OBJ:
3135 +               case SMB_ACL_OTHER:
3136 +               case SMB_ACL_MASK:
3137 +                       entry_d->a_type = tag_type;
3138 +                       break;
3139 +               default:
3140 +                       errno = EINVAL;
3141 +                       return -1;
3142 +       }
3143 +
3144 +       return 0;
3145 +}
3146 +
3147 + int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3148 +{
3149 +       if (entry_d->a_type != SMB_ACL_GROUP
3150 +           && entry_d->a_type != SMB_ACL_USER) {
3151 +               errno = EINVAL;
3152 +               return -1;
3153 +       }
3154 +
3155 +       entry_d->a_id = *((id_t *)qual_p);
3156 +
3157 +       return 0;
3158 +}
3159 +
3160 + int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3161 +{
3162 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3163 +               return EINVAL;
3164 +       }
3165 +
3166 +       entry_d->a_perm = *permset_d;
3167 +
3168 +       return 0;
3169 +}
3170 +
3171 +/* Structure to capture the count for each type of ACE. */
3172 +
3173 +struct hpux_acl_types {
3174 +       int n_user;
3175 +       int n_def_user;
3176 +       int n_user_obj;
3177 +       int n_def_user_obj;
3178 +
3179 +       int n_group;
3180 +       int n_def_group;
3181 +       int n_group_obj;
3182 +       int n_def_group_obj;
3183 +
3184 +       int n_other;
3185 +       int n_other_obj;
3186 +       int n_def_other_obj;
3187 +
3188 +       int n_class_obj;
3189 +       int n_def_class_obj;
3190 +
3191 +       int n_illegal_obj;
3192 +};
3193 +
3194 +/* count_obj:
3195 + * Counts the different number of objects in a given array of ACL
3196 + * structures.
3197 + * Inputs:
3198 + *
3199 + * acl_count      - Count of ACLs in the array of ACL strucutres.
3200 + * aclp           - Array of ACL structures.
3201 + * acl_type_count - Pointer to acl_types structure. Should already be
3202 + *                  allocated.
3203 + * Output:
3204 + *
3205 + * acl_type_count - This structure is filled up with counts of various
3206 + *                  acl types.
3207 + */
3208 +
3209 +static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
3210 +{
3211 +       int i;
3212 +
3213 +       memset(acl_type_count, 0, sizeof (struct hpux_acl_types));
3214 +
3215 +       for (i=0;i<acl_count;i++) {
3216 +               switch (aclp[i].a_type) {
3217 +               case USER:
3218 +                       acl_type_count->n_user++;
3219 +                       break;
3220 +               case USER_OBJ:
3221 +                       acl_type_count->n_user_obj++;
3222 +                       break;
3223 +               case DEF_USER_OBJ:
3224 +                       acl_type_count->n_def_user_obj++;
3225 +                       break;
3226 +               case GROUP:
3227 +                       acl_type_count->n_group++;
3228 +                       break;
3229 +               case GROUP_OBJ:
3230 +                       acl_type_count->n_group_obj++;
3231 +                       break;
3232 +               case DEF_GROUP_OBJ:
3233 +                       acl_type_count->n_def_group_obj++;
3234 +                       break;
3235 +               case OTHER_OBJ:
3236 +                       acl_type_count->n_other_obj++;
3237 +                       break;
3238 +               case DEF_OTHER_OBJ:
3239 +                       acl_type_count->n_def_other_obj++;
3240 +                       break;
3241 +               case CLASS_OBJ:
3242 +                       acl_type_count->n_class_obj++;
3243 +                       break;
3244 +               case DEF_CLASS_OBJ:
3245 +                       acl_type_count->n_def_class_obj++;
3246 +                       break;
3247 +               case DEF_USER:
3248 +                       acl_type_count->n_def_user++;
3249 +                       break;
3250 +               case DEF_GROUP:
3251 +                       acl_type_count->n_def_group++;
3252 +                       break;
3253 +               default:
3254 +                       acl_type_count->n_illegal_obj++;
3255 +                       break;
3256 +               }
3257 +       }
3258 +}
3259 +
3260 +/* swap_acl_entries:  Swaps two ACL entries.
3261 + *
3262 + * Inputs: aclp0, aclp1 - ACL entries to be swapped.
3263 + */
3264 +
3265 +static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
3266 +{
3267 +       struct acl temp_acl;
3268 +
3269 +       temp_acl.a_type = aclp0->a_type;
3270 +       temp_acl.a_id = aclp0->a_id;
3271 +       temp_acl.a_perm = aclp0->a_perm;
3272 +
3273 +       aclp0->a_type = aclp1->a_type;
3274 +       aclp0->a_id = aclp1->a_id;
3275 +       aclp0->a_perm = aclp1->a_perm;
3276 +
3277 +       aclp1->a_type = temp_acl.a_type;
3278 +       aclp1->a_id = temp_acl.a_id;
3279 +       aclp1->a_perm = temp_acl.a_perm;
3280 +}
3281 +
3282 +/* prohibited_duplicate_type
3283 + * Identifies if given ACL type can have duplicate entries or
3284 + * not.
3285 + *
3286 + * Inputs: acl_type - ACL Type.
3287 + *
3288 + * Outputs:
3289 + *
3290 + * Return..
3291 + *
3292 + * True - If the ACL type matches any of the prohibited types.
3293 + * False - If the ACL type doesn't match any of the prohibited types.
3294 + */
3295 +
3296 +static BOOL hpux_prohibited_duplicate_type(int acl_type)
3297 +{
3298 +       switch (acl_type) {
3299 +               case USER:
3300 +               case GROUP:
3301 +               case DEF_USER:
3302 +               case DEF_GROUP:
3303 +                       return True;
3304 +       }
3305 +       return False;
3306 +}
3307 +
3308 +/* get_needed_class_perm
3309 + * Returns the permissions of a ACL structure only if the ACL
3310 + * type matches one of the pre-determined types for computing
3311 + * CLASS_OBJ permissions.
3312 + *
3313 + * Inputs: aclp - Pointer to ACL structure.
3314 + */
3315 +
3316 +static int hpux_get_needed_class_perm(struct acl *aclp)
3317 +{
3318 +       switch (aclp->a_type) {
3319 +               case USER:
3320 +               case GROUP_OBJ:
3321 +               case GROUP:
3322 +               case DEF_USER_OBJ:
3323 +               case DEF_USER:
3324 +               case DEF_GROUP_OBJ:
3325 +               case DEF_GROUP:
3326 +               case DEF_CLASS_OBJ:
3327 +               case DEF_OTHER_OBJ:
3328 +                       return aclp->a_perm;
3329 +               default:
3330 +                       return 0;
3331 +       }
3332 +}
3333 +
3334 +/* acl_sort for HPUX.
3335 + * Sorts the array of ACL structures as per the description in
3336 + * aclsort man page. Refer to aclsort man page for more details
3337 + *
3338 + * Inputs:
3339 + *
3340 + * acl_count - Count of ACLs in the array of ACL structures.
3341 + * calclass  - If this is not zero, then we compute the CLASS_OBJ
3342 + *             permissions.
3343 + * aclp      - Array of ACL structures.
3344 + *
3345 + * Outputs:
3346 + *
3347 + * aclp     - Sorted array of ACL structures.
3348 + *
3349 + * Outputs:
3350 + *
3351 + * Returns 0 for success -1 for failure. Prints a message to the Samba
3352 + * debug log in case of failure.
3353 + */
3354 +
3355 +static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
3356 +{
3357 +#if !defined(HAVE_HPUX_ACLSORT)
3358 +       /* The aclsort() system call is availabe on the latest HPUX General
3359 +        * Patch Bundles. So for HPUX, we developed our version of acl_sort
3360 +        * function. Because, we don't want to update to a new
3361 +        * HPUX GR bundle just for aclsort() call. */
3362 +
3363 +       struct hpux_acl_types acl_obj_count;
3364 +       int n_class_obj_perm = 0;
3365 +       int i, j;
3366 +
3367 +       if (!acl_count) {
3368 +               DEBUG(10,("Zero acl count passed. Returning Success\n"));
3369 +               return 0;
3370 +       }
3371 +
3372 +       if (aclp == NULL) {
3373 +               DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3374 +               return -1;
3375 +       }
3376 +
3377 +       /* Count different types of ACLs in the ACLs array */
3378 +
3379 +       hpux_count_obj(acl_count, aclp, &acl_obj_count);
3380 +
3381 +       /* There should be only one entry each of type USER_OBJ, GROUP_OBJ,
3382 +        * CLASS_OBJ and OTHER_OBJ
3383 +        */
3384 +
3385 +       if (acl_obj_count.n_user_obj != 1
3386 +           || acl_obj_count.n_group_obj != 1
3387 +           || acl_obj_count.n_class_obj != 1
3388 +           || acl_obj_count.n_other_obj != 1) {
3389 +               DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3390 +USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3391 +               return -1;
3392 +       }
3393 +
3394 +       /* If any of the default objects are present, there should be only
3395 +        * one of them each.
3396 +        */
3397 +
3398 +       if (acl_obj_count.n_def_user_obj > 1 || acl_obj_count.n_def_group_obj > 1 ||
3399 +                       acl_obj_count.n_def_other_obj > 1 || acl_obj_count.n_def_class_obj > 1) {
3400 +               DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3401 +or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3402 +               return -1;
3403 +       }
3404 +
3405 +       /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl
3406 +        * structures.
3407 +        *
3408 +        * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3409 +        * same ACL type, sort by ACL id.
3410 +        *
3411 +        * I am using the trival kind of sorting method here because, performance isn't
3412 +        * really effected by the ACLs feature. More over there aren't going to be more
3413 +        * than 17 entries on HPUX.
3414 +        */
3415 +
3416 +       for (i=0; i<acl_count;i++) {
3417 +               for (j=i+1; j<acl_count; j++) {
3418 +                       if (aclp[i].a_type > aclp[j].a_type) {
3419 +                               /* ACL entries out of order, swap them */
3420 +
3421 +                               hpux_swap_acl_entries((aclp+i), (aclp+j));
3422 +
3423 +                       } else if (aclp[i].a_type == aclp[j].a_type) {
3424 +
3425 +                               /* ACL entries of same type, sort by id */
3426 +
3427 +                               if (aclp[i].a_id > aclp[j].a_id) {
3428 +                                       hpux_swap_acl_entries((aclp+i), (aclp+j));
3429 +                               } else if (aclp[i].a_id == aclp[j].a_id) {
3430 +                                       /* We have a duplicate entry. */
3431 +                                       if (hpux_prohibited_duplicate_type(aclp[i].a_type)) {
3432 +                                               DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3433 +                                                       aclp[i].a_type, aclp[i].a_id));
3434 +                                               return -1;
3435 +                                       }
3436 +                               }
3437 +
3438 +                       }
3439 +               }
3440 +       }
3441 +
3442 +       /* set the class obj permissions to the computed one. */
3443 +       if (calclass) {
3444 +               int n_class_obj_index = -1;
3445 +
3446 +               for (i=0;i<acl_count;i++) {
3447 +                       n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3448 +
3449 +                       if (aclp[i].a_type == CLASS_OBJ)
3450 +                               n_class_obj_index = i;
3451 +               }
3452 +               aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3453 +       }
3454 +
3455 +       return 0;
3456 +#else
3457 +       return aclsort(acl_count, calclass, aclp);
3458 +#endif
3459 +}
3460 +
3461 +/* Sort the ACL and check it for validity.
3462 + *
3463 + * If it's a minimal ACL with only 4 entries then we
3464 + * need to recalculate the mask permissions to make
3465 + * sure that they are the same as the GROUP_OBJ
3466 + * permissions as required by the UnixWare acl() system call.
3467 + *
3468 + * (Note: since POSIX allows minimal ACLs which only contain
3469 + * 3 entries - ie there is no mask entry - we should, in theory,
3470 + * check for this and add a mask entry if necessary - however
3471 + * we "know" that the caller of this interface always specifies
3472 + * a mask so, in practice "this never happens" (tm) - if it *does*
3473 + * happen aclsort() will fail and return an error and someone will
3474 + * have to fix it ...) */
3475 +
3476 +static int acl_sort(SMB_ACL_T acl_d)
3477 +{
3478 +       int fixmask = (acl_d->count <= 4);
3479 +
3480 +       if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
3481 +               errno = EINVAL;
3482 +               return -1;
3483 +       }
3484 +       return 0;
3485 +}
3486 +
3487 + int sys_acl_valid(SMB_ACL_T acl_d)
3488 +{
3489 +       return acl_sort(acl_d);
3490 +}
3491 +
3492 + int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3493 +{
3494 +       struct stat s;
3495 +       struct acl *acl_p;
3496 +       int acl_count;
3497 +       struct acl *acl_buf = NULL;
3498 +       int ret;
3499 +
3500 +       if (hpux_acl_call_presence() == False) {
3501 +               /* Looks like we don't have the acl() system call on HPUX.
3502 +                * May be the system doesn't have the latest version of JFS. */
3503 +               errno = ENOSYS;
3504 +               return -1;
3505 +       }
3506 +
3507 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3508 +               errno = EINVAL;
3509 +               return -1;
3510 +       }
3511 +
3512 +       if (acl_sort(acl_d) != 0) {
3513 +               return -1;
3514 +       }
3515 +
3516 +       acl_p = &acl_d->acl[0];
3517 +       acl_count = acl_d->count;
3518 +
3519 +       /* If it's a directory there is extra work to do since the acl()
3520 +        * system call will replace both the access ACLs and the default
3521 +        * ACLs (if any). */
3522 +       if (stat(name, &s) != 0) {
3523 +               return -1;
3524 +       }
3525 +       if (S_ISDIR(s.st_mode)) {
3526 +               SMB_ACL_T acc_acl;
3527 +               SMB_ACL_T def_acl;
3528 +               SMB_ACL_T tmp_acl;
3529 +               int i;
3530 +
3531 +               if (type == SMB_ACL_TYPE_ACCESS) {
3532 +                       acc_acl = acl_d;
3533 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3534 +
3535 +               } else {
3536 +                       def_acl = acl_d;
3537 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3538 +               }
3539 +
3540 +               if (tmp_acl == NULL) {
3541 +                       return -1;
3542 +               }
3543 +
3544 +               /* Allocate a temporary buffer for the complete ACL. */
3545 +               acl_count = acc_acl->count + def_acl->count;
3546 +               acl_p = acl_buf = malloc(acl_count * sizeof acl_buf[0]);
3547 +
3548 +               if (acl_buf == NULL) {
3549 +                       sys_acl_free_acl(tmp_acl);
3550 +                       errno = ENOMEM;
3551 +                       return -1;
3552 +               }
3553 +
3554 +               /* Copy the access control and default entries into the buffer. */
3555 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
3556 +                       acc_acl->count * sizeof acl_buf[0]);
3557 +
3558 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3559 +                       def_acl->count * sizeof acl_buf[0]);
3560 +
3561 +               /* Set the ACL_DEFAULT flag on the default entries. */
3562 +               for (i = acc_acl->count; i < acl_count; i++) {
3563 +                       acl_buf[i].a_type |= ACL_DEFAULT;
3564 +               }
3565 +
3566 +               sys_acl_free_acl(tmp_acl);
3567 +
3568 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
3569 +               errno = EINVAL;
3570 +               return -1;
3571 +       }
3572 +
3573 +       ret = acl(name, ACL_SET, acl_count, acl_p);
3574 +
3575 +       free(acl_buf);
3576 +
3577 +       return ret;
3578 +}
3579 +
3580 + int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3581 +{
3582 +       /* HPUX doesn't have the facl call. Fake it using the path.... JRA. */
3583 +
3584 +       files_struct *fsp = file_find_fd(fd);
3585 +
3586 +       if (fsp == NULL) {
3587 +               errno = EBADF;
3588 +               return NULL;
3589 +       }
3590 +
3591 +       if (acl_sort(acl_d) != 0) {
3592 +               return -1;
3593 +       }
3594 +
3595 +       /* We know we're in the same conn context. So we can use the
3596 +        * relative path. */
3597 +
3598 +       return sys_acl_set_file(dos_to_unix_static(fsp->fsp_name), SMB_ACL_TYPE_ACCESS, acl_d);
3599 +}
3600 +
3601 + int sys_acl_delete_def_file(const char *path)
3602 +{
3603 +       SMB_ACL_T acl_d;
3604 +       int ret;
3605 +
3606 +       /* Fetching the access ACL and rewriting it has the effect of
3607 +        * deleting the default ACL. */
3608 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3609 +               return -1;
3610 +       }
3611 +
3612 +       ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
3613 +
3614 +       sys_acl_free_acl(acl_d);
3615 +
3616 +       return ret;
3617 +}
3618 +
3619 + int sys_acl_free_text(char *text)
3620 +{
3621 +       free(text);
3622 +       return 0;
3623 +}
3624 +
3625 + int sys_acl_free_acl(SMB_ACL_T acl_d)
3626 +{
3627 +       free(acl_d);
3628 +       return 0;
3629 +}
3630 +
3631 + int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3632 +{
3633 +       return 0;
3634 +}
3635 +
3636 +#elif defined(HAVE_IRIX_ACLS)
3637 +
3638 + int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3639 +{
3640 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3641 +               errno = EINVAL;
3642 +               return -1;
3643 +       }
3644 +
3645 +       if (entry_p == NULL) {
3646 +               errno = EINVAL;
3647 +               return -1;
3648 +       }
3649 +
3650 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
3651 +               acl_d->next = 0;
3652 +       }
3653 +
3654 +       if (acl_d->next < 0) {
3655 +               errno = EINVAL;
3656 +               return -1;
3657 +       }
3658 +
3659 +       if (acl_d->next >= acl_d->aclp->acl_cnt) {
3660 +               return 0;
3661 +       }
3662 +
3663 +       *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
3664 +
3665 +       return 1;
3666 +}
3667 +
3668 + int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
3669 +{
3670 +       *type_p = entry_d->ae_tag;
3671 +
3672 +       return 0;
3673 +}
3674 +
3675 + int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3676 +{
3677 +       *permset_p = entry_d;
3678 +
3679 +       return 0;
3680 +}
3681 +
3682 + void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3683 +{
3684 +       if (entry_d->ae_tag != SMB_ACL_USER
3685 +           && entry_d->ae_tag != SMB_ACL_GROUP) {
3686 +               errno = EINVAL;
3687 +               return NULL;
3688 +       }
3689 +
3690 +       return &entry_d->ae_id;
3691 +}
3692 +
3693 + SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3694 +{
3695 +       SMB_ACL_T a;
3696 +
3697 +       if ((a = malloc(sizeof a[0])) == NULL) {
3698 +               errno = ENOMEM;
3699 +               return NULL;
3700 +       }
3701 +       if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
3702 +               free(a);
3703 +               return NULL;
3704 +       }
3705 +       a->next = -1;
3706 +       a->freeaclp = True;
3707 +       return a;
3708 +}
3709 +
3710 + SMB_ACL_T sys_acl_get_fd(int fd)
3711 +{
3712 +       SMB_ACL_T a;
3713 +
3714 +       if ((a = malloc(sizeof a[0])) == NULL) {
3715 +               errno = ENOMEM;
3716 +               return NULL;
3717 +       }
3718 +       if ((a->aclp = acl_get_fd(fd)) == NULL) {
3719 +               free(a);
3720 +               return NULL;
3721 +       }
3722 +       a->next = -1;
3723 +       a->freeaclp = True;
3724 +       return a;
3725 +}
3726 +
3727 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3728 +{
3729 +       permset_d->ae_perm = 0;
3730 +
3731 +       return 0;
3732 +}
3733 +
3734 + int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3735 +{
3736 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3737 +           && perm != SMB_ACL_EXECUTE) {
3738 +               errno = EINVAL;
3739 +               return -1;
3740 +       }
3741 +
3742 +       if (permset_d == NULL) {
3743 +               errno = EINVAL;
3744 +               return -1;
3745 +       }
3746 +
3747 +       permset_d->ae_perm |= perm;
3748 +
3749 +       return 0;
3750 +}
3751 +
3752 + int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3753 +{
3754 +       return permset_d->ae_perm & perm;
3755 +}
3756 +
3757 + char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3758 +{
3759 +       return acl_to_text(acl_d->aclp, len_p);
3760 +}
3761 +
3762 + SMB_ACL_T sys_acl_init(int count)
3763 +{
3764 +       SMB_ACL_T a;
3765 +
3766 +       if (count < 0) {
3767 +               errno = EINVAL;
3768 +               return NULL;
3769 +       }
3770 +
3771 +       if ((a = malloc(sizeof a[0] + sizeof (struct acl))) == NULL) {
3772 +               errno = ENOMEM;
3773 +               return NULL;
3774 +       }
3775 +
3776 +       a->next = -1;
3777 +       a->freeaclp = False;
3778 +       a->aclp = (struct acl *)(&a->aclp + sizeof (struct acl *));
3779 +       a->aclp->acl_cnt = 0;
3780 +
3781 +       return a;
3782 +}
3783 +
3784 +
3785 + int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3786 +{
3787 +       SMB_ACL_T acl_d;
3788 +       SMB_ACL_ENTRY_T entry_d;
3789 +
3790 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3791 +               errno = EINVAL;
3792 +               return -1;
3793 +       }
3794 +
3795 +       if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
3796 +               errno = ENOSPC;
3797 +               return -1;
3798 +       }
3799 +
3800 +       entry_d = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
3801 +       entry_d->ae_tag = 0;
3802 +       entry_d->ae_id = 0;
3803 +       entry_d->ae_perm = 0;
3804 +       *entry_p = entry_d;
3805 +
3806 +       return 0;
3807 +}
3808 +
3809 + int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
3810 +{
3811 +       switch (tag_type) {
3812 +               case SMB_ACL_USER:
3813 +               case SMB_ACL_USER_OBJ:
3814 +               case SMB_ACL_GROUP:
3815 +               case SMB_ACL_GROUP_OBJ:
3816 +               case SMB_ACL_OTHER:
3817 +               case SMB_ACL_MASK:
3818 +                       entry_d->ae_tag = tag_type;
3819 +                       break;
3820 +               default:
3821 +                       errno = EINVAL;
3822 +                       return -1;
3823 +       }
3824 +
3825 +       return 0;
3826 +}
3827 +
3828 + int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
3829 +{
3830 +       if (entry_d->ae_tag != SMB_ACL_GROUP
3831 +           && entry_d->ae_tag != SMB_ACL_USER) {
3832 +               errno = EINVAL;
3833 +               return -1;
3834 +       }
3835 +
3836 +       entry_d->ae_id = *((id_t *)qual_p);
3837 +
3838 +       return 0;
3839 +}
3840 +
3841 + int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
3842 +{
3843 +       if (permset_d->ae_perm & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
3844 +               return EINVAL;
3845 +       }
3846 +
3847 +       entry_d->ae_perm = permset_d->ae_perm;
3848 +
3849 +       return 0;
3850 +}
3851 +
3852 + int sys_acl_valid(SMB_ACL_T acl_d)
3853 +{
3854 +       return acl_valid(acl_d->aclp);
3855 +}
3856 +
3857 + int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3858 +{
3859 +       return acl_set_file(name, type, acl_d->aclp);
3860 +}
3861 +
3862 + int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3863 +{
3864 +       return acl_set_fd(fd, acl_d->aclp);
3865 +}
3866 +
3867 + int sys_acl_delete_def_file(const char *name)
3868 +{
3869 +       return acl_delete_def_file(name);
3870 +}
3871 +
3872 + int sys_acl_free_text(char *text)
3873 +{
3874 +       return acl_free(text);
3875 +}
3876 +
3877 + int sys_acl_free_acl(SMB_ACL_T acl_d)
3878 +{
3879 +       if (acl_d->freeaclp) {
3880 +               acl_free(acl_d->aclp);
3881 +       }
3882 +       acl_free(acl_d);
3883 +       return 0;
3884 +}
3885 +
3886 + int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3887 +{
3888 +       return 0;
3889 +}
3890 +
3891 +#elif defined(HAVE_AIX_ACLS)
3892 +
3893 +/* Donated by Medha Date, mdate@austin.ibm.com, for IBM */
3894 +
3895 + int sys_acl_get_entry(SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3896 +{
3897 +       struct acl_entry_link *link;
3898 +       struct new_acl_entry *entry;
3899 +       int keep_going;
3900 +
3901 +       DEBUG(10,("This is the count: %d\n",theacl->count));
3902 +
3903 +       /* Check if count was previously set to -1.  If it was, that
3904 +        * means we reached the end of the acl last time. */
3905 +       if (theacl->count == -1)
3906 +               return 0;
3907 +
3908 +       link = theacl;
3909 +       /* To get to the next acl, traverse linked list until index of
3910 +        * acl matches the count we are keeping.  This count is
3911 +        * incremented each time we return an acl entry. */
3912 +
3913 +       for (keep_going = 0; keep_going < theacl->count; keep_going++)
3914 +               link = link->nextp;
3915 +
3916 +       entry = *entry_p =  link->entryp;
3917 +
3918 +       DEBUG(10,("*entry_p is %d\n",entry_p));
3919 +       DEBUG(10,("*entry_p->ace_access is %d\n",entry->ace_access));
3920 +
3921 +       /* Increment count */
3922 +       theacl->count++;
3923 +       if (link->nextp == NULL)
3924 +               theacl->count = -1;
3925 +
3926 +       return 1;
3927 +}
3928 +
3929 + int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
3930 +{
3931 +       /* Initialize tag type */
3932 +
3933 +       *tag_type_p = -1;
3934 +       DEBUG(10,("the tagtype is %d\n",entry_d->ace_id->id_type));
3935 +
3936 +       /* Depending on what type of entry we have, return tag type. */
3937 +       switch (entry_d->ace_id->id_type) {
3938 +       case ACEID_USER:
3939 +               *tag_type_p = SMB_ACL_USER;
3940 +               break;
3941 +       case ACEID_GROUP:
3942 +               *tag_type_p = SMB_ACL_GROUP;
3943 +               break;
3944 +
3945 +       case SMB_ACL_USER_OBJ:
3946 +       case SMB_ACL_GROUP_OBJ:
3947 +       case SMB_ACL_OTHER:
3948 +               *tag_type_p = entry_d->ace_id->id_type;
3949 +               break;
3950 +
3951 +       default:
3952 +               return -1;
3953 +       }
3954 +
3955 +       return 0;
3956 +}
3957 +
3958 + int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3959 +{
3960 +       DEBUG(10,("Starting AIX sys_acl_get_permset\n"));
3961 +       *permset_p = &entry_d->ace_access;
3962 +       DEBUG(10,("**permset_p is %d\n",**permset_p));
3963 +       if (!(**permset_p & S_IXUSR)
3964 +           && !(**permset_p & S_IWUSR)
3965 +           && !(**permset_p & S_IRUSR)
3966 +           && **permset_p != 0)
3967 +                       return -1;
3968 +
3969 +       DEBUG(10,("Ending AIX sys_acl_get_permset\n"));
3970 +       return 0;
3971 +}
3972 +
3973 + void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3974 +{
3975 +       return entry_d->ace_id->id_data;
3976 +}
3977 +
3978 + SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3979 +{
3980 +       struct acl *file_acl = (struct acl *)NULL;
3981 +       struct acl_entry *acl_entry;
3982 +       struct new_acl_entry *new_acl_entry;
3983 +       struct ace_id *idp;
3984 +       struct acl_entry_link *acl_entry_link;
3985 +       struct acl_entry_link *acl_entry_link_head;
3986 +       int i;
3987 +       int rc = 0;
3988 +       uid_t user_id;
3989 +
3990 +       /* Get the acl using statacl */
3991 +
3992 +       DEBUG(10,("Entering sys_acl_get_file\n"));
3993 +       DEBUG(10,("path_p is %s\n",path_p));
3994 +
3995 +       file_acl = (struct acl *)malloc(BUFSIZ);
3996 +
3997 +       if (file_acl == NULL) {
3998 +               errno=ENOMEM;
3999 +               DEBUG(0,("Error in AIX sys_acl_get_file: %d\n",errno));
4000 +               return NULL;
4001 +       }
4002 +
4003 +       memset(file_acl,0,BUFSIZ);
4004 +
4005 +       rc = statacl((char *)path_p,0,file_acl,BUFSIZ);
4006 +       if (rc == -1) {
4007 +               DEBUG(0,("statacl returned %d with errno %d\n",rc,errno));
4008 +               free(file_acl);
4009 +               return NULL;
4010 +       }
4011 +
4012 +       DEBUG(10,("Got facl and returned it\n"));
4013 +
4014 +       /* Point to the first acl entry in the acl */
4015 +       acl_entry =  file_acl->acl_ext;
4016 +
4017 +       /* Begin setting up the head of the linked list that will be
4018 +        * used for the storing the acl in a way that is useful for the
4019 +        * posix_acls.c code. */
4020 +
4021 +       acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4022 +       if (acl_entry_link_head == NULL)
4023 +               return NULL;
4024 +
4025 +       acl_entry_link->entryp = (struct new_acl_entry *)malloc(sizeof (struct new_acl_entry));
4026 +       if (acl_entry_link->entryp == NULL) {
4027 +               free(file_acl);
4028 +               errno = ENOMEM;
4029 +               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4030 +               return NULL;
4031 +       }
4032 +
4033 +       DEBUG(10,("acl_entry is %d\n",acl_entry));
4034 +       DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4035 +
4036 +       /* Check if the extended acl bit is on.  If it isn't, do not
4037 +        * show the contents of the acl since AIX intends the extended
4038 +        * info to remain unused. */
4039 +
4040 +       if (file_acl->acl_mode & S_IXACL){
4041 +               /* while we are not pointing to the very end */
4042 +               while (acl_entry < acl_last(file_acl)) {
4043 +                       /* before we malloc anything, make sure this is  */
4044 +                       /* a valid acl entry and one that we want to map */
4045 +                       idp = id_nxt(acl_entry->ace_id);
4046 +                       if ((acl_entry->ace_type == ACC_SPECIFY
4047 +                         || acl_entry->ace_type == ACC_PERMIT)
4048 +                        && idp != id_last(acl_entry)) {
4049 +                               acl_entry = acl_nxt(acl_entry);
4050 +                               continue;
4051 +                       }
4052 +
4053 +                       idp = acl_entry->ace_id;
4054 +
4055 +                       /* Check if this is the first entry in the linked list.
4056 +                        * The first entry needs to keep prevp pointing to NULL
4057 +                        * and already has entryp allocated. */
4058 +
4059 +                       if (acl_entry_link_head->count != 0) {
4060 +                               acl_entry_link->nextp = (struct acl_entry_link *)malloc(sizeof (struct acl_entry_link));
4061 +
4062 +                               if (acl_entry_link->nextp == NULL) {
4063 +                                       free(file_acl);
4064 +                                       errno = ENOMEM;
4065 +                                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4066 +                                       return NULL;
4067 +                               }
4068 +
4069 +                               acl_entry_link->nextp->prevp = acl_entry_link;
4070 +                               acl_entry_link = acl_entry_link->nextp;
4071 +                               acl_entry_link->entryp = (struct new_acl_entry *)malloc(sizeof (struct new_acl_entry));
4072 +                               if (acl_entry_link->entryp == NULL) {
4073 +                                       free(file_acl);
4074 +                                       errno = ENOMEM;
4075 +                                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4076 +                                       return NULL;
4077 +                               }
4078 +                               acl_entry_link->nextp = NULL;
4079 +                       }
4080 +
4081 +                       acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4082 +
4083 +                       /* Don't really need this since all types are going
4084 +                        * to be specified but, it's better than leaving it 0. */
4085 +
4086 +                       acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4087 +
4088 +                       acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4089 +
4090 +                       memcpy(acl_entry_link->entryp->ace_id,idp,sizeof (struct ace_id));
4091 +
4092 +                       /* The access in the acl entries must be left shifted by
4093 +                        * three bites, because they will ultimately be compared
4094 +                        * to S_IRUSR, S_IWUSR, and S_IXUSR. */
4095 +
4096 +                       switch (acl_entry->ace_type){
4097 +                       case ACC_PERMIT:
4098 +                       case ACC_SPECIFY:
4099 +                               acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4100 +                               acl_entry_link->entryp->ace_access <<= 6;
4101 +                               acl_entry_link_head->count++;
4102 +                               break;
4103 +                       case ACC_DENY:
4104 +                               /* Since there is no way to return a DENY acl entry
4105 +                                * change to PERMIT and then shift. */
4106 +                               DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4107 +                               acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4108 +                               DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4109 +                               acl_entry_link->entryp->ace_access <<= 6;
4110 +                               acl_entry_link_head->count++;
4111 +                               break;
4112 +                       default:
4113 +                               return 0;
4114 +                       }
4115 +
4116 +                       DEBUG(10,("acl_entry = %d\n",acl_entry));
4117 +                       DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4118 +
4119 +                       acl_entry = acl_nxt(acl_entry);
4120 +               }
4121 +       } /* end of if enabled */
4122 +
4123 +       /* Since owner, group, other acl entries are not part of the acl
4124 +        * entries in an acl, they must be dummied up to become part of
4125 +        * the list. */
4126 +
4127 +       for (i = 1; i < 4; i++) {
4128 +               DEBUG(10,("i is %d\n",i));
4129 +               if (acl_entry_link_head->count != 0) {
4130 +                       acl_entry_link->nextp = (struct acl_entry_link *)malloc(sizeof (struct acl_entry_link));
4131 +                       if (acl_entry_link->nextp == NULL) {
4132 +                               free(file_acl);
4133 +                               errno = ENOMEM;
4134 +                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4135 +                               return NULL;
4136 +                       }
4137 +
4138 +                       acl_entry_link->nextp->prevp = acl_entry_link;
4139 +                       acl_entry_link = acl_entry_link->nextp;
4140 +                       acl_entry_link->entryp = (struct new_acl_entry *)malloc(sizeof (struct new_acl_entry));
4141 +                       if (acl_entry_link->entryp == NULL) {
4142 +                               free(file_acl);
4143 +                               errno = ENOMEM;
4144 +                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
4145 +                               return NULL;
4146 +                       }
4147 +               }
4148 +
4149 +               acl_entry_link->nextp = NULL;
4150 +
4151 +               new_acl_entry = acl_entry_link->entryp;
4152 +               idp = new_acl_entry->ace_id;
4153 +
4154 +               new_acl_entry->ace_len = sizeof (struct acl_entry);
4155 +               new_acl_entry->ace_type = ACC_PERMIT;
4156 +               idp->id_len = sizeof (struct ace_id);
4157 +               DEBUG(10,("idp->id_len = %d\n",idp->id_len));
4158 +               memset(idp->id_data,0,sizeof (uid_t));
4159 +
4160 +               switch (i) {
4161 +               case 2:
4162 +                       new_acl_entry->ace_access = file_acl->g_access << 6;
4163 +                       idp->id_type = SMB_ACL_GROUP_OBJ;
4164 +                       break;
4165 +
4166 +               case 3:
4167 +                       new_acl_entry->ace_access = file_acl->o_access << 6;
4168 +                       idp->id_type = SMB_ACL_OTHER;
4169 +                       break;
4170 +
4171 +               case 1:
4172 +                       new_acl_entry->ace_access = file_acl->u_access << 6;
4173 +                       idp->id_type = SMB_ACL_USER_OBJ;
4174 +                       break;
4175 +
4176 +               default:
4177 +                       return NULL;
4178 +
4179 +               }
4180 +
4181 +               acl_entry_link_head->count++;
4182 +               DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4183 +       }
4184 +
4185 +       acl_entry_link_head->count = 0;
4186 +       free(file_acl);
4187 +
4188 +       return acl_entry_link_head;
4189 +}
4190 +
4191 + SMB_ACL_T sys_acl_get_fd(int fd)
4192 +{
4193 +       struct acl *file_acl = (struct acl *)NULL;
4194 +       struct acl_entry *acl_entry;
4195 +       struct new_acl_entry *new_acl_entry;
4196 +       struct ace_id *idp;
4197 +       struct acl_entry_link *acl_entry_link;
4198 +       struct acl_entry_link *acl_entry_link_head;
4199 +       int i;
4200 +       int rc = 0;
4201 +       uid_t user_id;
4202 +
4203 +       /* Get the acl using fstatacl */
4204 +
4205 +       DEBUG(10,("Entering sys_acl_get_fd\n"));
4206 +       DEBUG(10,("fd is %d\n",fd));
4207 +       file_acl = (struct acl *)malloc(BUFSIZ);
4208 +
4209 +       if (file_acl == NULL) {
4210 +               errno=ENOMEM;
4211 +               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4212 +               return NULL;
4213 +       }
4214 +
4215 +       memset(file_acl,0,BUFSIZ);
4216 +
4217 +       rc = fstatacl(fd,0,file_acl,BUFSIZ);
4218 +       if (rc == -1) {
4219 +               DEBUG(0,("The fstatacl call returned %d with errno %d\n",rc,errno));
4220 +               free(file_acl);
4221 +               return NULL;
4222 +       }
4223 +
4224 +       DEBUG(10,("Got facl and returned it\n"));
4225 +
4226 +       /* Point to the first acl entry in the acl */
4227 +
4228 +       acl_entry =  file_acl->acl_ext;
4229 +
4230 +       /* Begin setting up the head of the linked list that will be
4231 +        * used for the storing the acl in a way that is useful for the
4232 +        * posix_acls.c code. */
4233 +
4234 +       acl_entry_link_head = acl_entry_link = sys_acl_init(0);
4235 +       if (acl_entry_link_head == NULL){
4236 +               free(file_acl);
4237 +               return NULL;
4238 +       }
4239 +
4240 +       acl_entry_link->entryp = (struct new_acl_entry *)malloc(sizeof (struct new_acl_entry));
4241 +
4242 +       if (acl_entry_link->entryp == NULL) {
4243 +               errno = ENOMEM;
4244 +               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4245 +               free(file_acl);
4246 +               return NULL;
4247 +       }
4248 +
4249 +       DEBUG(10,("acl_entry is %d\n",acl_entry));
4250 +       DEBUG(10,("acl_last(file_acl) id %d\n",acl_last(file_acl)));
4251 +
4252 +       /* Check if the extended acl bit is on.  If it isn't, do not
4253 +        * show the contents of the acl since AIX intends the extended
4254 +        * info to remain unused. */
4255 +
4256 +       if (file_acl->acl_mode & S_IXACL){
4257 +               /* while we are not pointing to the very end */
4258 +               while (acl_entry < acl_last(file_acl)) {
4259 +                       /* before we malloc anything, make sure this is  */
4260 +                       /* a valid acl entry and one that we want to map */
4261 +
4262 +                       idp = id_nxt(acl_entry->ace_id);
4263 +                       if ((acl_entry->ace_type == ACC_SPECIFY
4264 +                         || acl_entry->ace_type == ACC_PERMIT)
4265 +                        && (idp != id_last(acl_entry))) {
4266 +                               acl_entry = acl_nxt(acl_entry);
4267 +                               continue;
4268 +                       }
4269 +
4270 +                       idp = acl_entry->ace_id;
4271 +
4272 +                       /* Check if this is the first entry in the linked list.
4273 +                        * The first entry needs to keep prevp pointing to NULL
4274 +                        * and already has entryp allocated. */
4275 +
4276 +                       if (acl_entry_link_head->count != 0) {
4277 +                               acl_entry_link->nextp = (struct acl_entry_link *)malloc(sizeof (struct acl_entry_link));
4278 +                               if (acl_entry_link->nextp == NULL) {
4279 +                                       errno = ENOMEM;
4280 +                                       DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4281 +                                       free(file_acl);
4282 +                                       return NULL;
4283 +                               }
4284 +                               acl_entry_link->nextp->prevp = acl_entry_link;
4285 +                               acl_entry_link = acl_entry_link->nextp;
4286 +                               acl_entry_link->entryp = (struct new_acl_entry *)malloc(sizeof (struct new_acl_entry));
4287 +                               if (acl_entry_link->entryp == NULL) {
4288 +                                       errno = ENOMEM;
4289 +                                       DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4290 +                                       free(file_acl);
4291 +                                       return NULL;
4292 +                               }
4293 +
4294 +                               acl_entry_link->nextp = NULL;
4295 +                       }
4296 +
4297 +                       acl_entry_link->entryp->ace_len = acl_entry->ace_len;
4298 +
4299 +                       /* Don't really need this since all types are going
4300 +                        * to be specified but, it's better than leaving it 0. */
4301 +
4302 +                       acl_entry_link->entryp->ace_type = acl_entry->ace_type;
4303 +                       acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4304 +
4305 +                       memcpy(acl_entry_link->entryp->ace_id, idp, sizeof (struct ace_id));
4306 +
4307 +                       /* The access in the acl entries must be left shifted by
4308 +                        * three bites, because they will ultimately be compared
4309 +                        * to S_IRUSR, S_IWUSR, and S_IXUSR. */
4310 +
4311 +                       switch (acl_entry->ace_type){
4312 +                       case ACC_PERMIT:
4313 +                       case ACC_SPECIFY:
4314 +                               acl_entry_link->entryp->ace_access = acl_entry->ace_access;
4315 +                               acl_entry_link->entryp->ace_access <<= 6;
4316 +                               acl_entry_link_head->count++;
4317 +                               break;
4318 +                       case ACC_DENY:
4319 +                               /* Since there is no way to return a DENY acl entry
4320 +                                * change to PERMIT and then shift. */
4321 +                               DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
4322 +                               acl_entry_link->entryp->ace_access = ~acl_entry->ace_access & 7;
4323 +                               DEBUG(10,("acl_entry_link->entryp->ace_access is %d\n",acl_entry_link->entryp->ace_access));
4324 +                               acl_entry_link->entryp->ace_access <<= 6;
4325 +                               acl_entry_link_head->count++;
4326 +                               break;
4327 +                       default:
4328 +                               return 0;
4329 +                       }
4330 +
4331 +                       DEBUG(10,("acl_entry = %d\n",acl_entry));
4332 +                       DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
4333 +
4334 +                       acl_entry = acl_nxt(acl_entry);
4335 +               }
4336 +       } /* end of if enabled */
4337 +
4338 +       /* Since owner, group, other acl entries are not
4339 +        * part of the acl entries in an acl, they must
4340 +        * be dummied up to become part of the list. */
4341 +
4342 +       for (i = 1; i < 4; i++) {
4343 +               DEBUG(10,("i is %d\n",i));
4344 +               if (acl_entry_link_head->count != 0){
4345 +                       acl_entry_link->nextp = (struct acl_entry_link *)malloc(sizeof (struct acl_entry_link));
4346 +                       if (acl_entry_link->nextp == NULL) {
4347 +                               errno = ENOMEM;
4348 +                               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4349 +                               free(file_acl);
4350 +                               return NULL;
4351 +                       }
4352 +
4353 +                       acl_entry_link->nextp->prevp = acl_entry_link;
4354 +                       acl_entry_link = acl_entry_link->nextp;
4355 +                       acl_entry_link->entryp = (struct new_acl_entry *)malloc(sizeof (struct new_acl_entry));
4356 +
4357 +                       if (acl_entry_link->entryp == NULL) {
4358 +                               free(file_acl);
4359 +                               errno = ENOMEM;
4360 +                               DEBUG(0,("Error in sys_acl_get_fd is %d\n",errno));
4361 +                               return NULL;
4362 +                       }
4363 +               }
4364 +
4365 +               acl_entry_link->nextp = NULL;
4366 +
4367 +               new_acl_entry = acl_entry_link->entryp;
4368 +               idp = new_acl_entry->ace_id;
4369 +
4370 +               new_acl_entry->ace_len = sizeof (struct acl_entry);
4371 +               new_acl_entry->ace_type = ACC_PERMIT;
4372 +               idp->id_len = sizeof (struct ace_id);
4373 +               DEBUG(10,("idp->id_len = %d\n",idp->id_len));
4374 +               memset(idp->id_data,0,sizeof (uid_t));
4375 +
4376 +               switch (i) {
4377 +               case 2:
4378 +                       new_acl_entry->ace_access = file_acl->g_access << 6;
4379 +                       idp->id_type = SMB_ACL_GROUP_OBJ;
4380 +                       break;
4381 +
4382 +               case 3:
4383 +                       new_acl_entry->ace_access = file_acl->o_access << 6;
4384 +                       idp->id_type = SMB_ACL_OTHER;
4385 +                       break;
4386 +
4387 +               case 1:
4388 +                       new_acl_entry->ace_access = file_acl->u_access << 6;
4389 +                       idp->id_type = SMB_ACL_USER_OBJ;
4390 +                       break;
4391 +
4392 +               default:
4393 +                       return NULL;
4394 +               }
4395 +
4396 +               acl_entry_link_head->count++;
4397 +               DEBUG(10,("new_acl_entry->ace_access = %d\n",new_acl_entry->ace_access));
4398 +       }
4399 +
4400 +       acl_entry_link_head->count = 0;
4401 +       free(file_acl);
4402 +
4403 +       return acl_entry_link_head;
4404 +}
4405 +
4406 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
4407 +{
4408 +       *permset = *permset & ~0777;
4409 +       return 0;
4410 +}
4411 +
4412 + int sys_acl_add_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4413 +{
4414 +       if (perm != 0 && (perm & (S_IXUSR | S_IWUSR | S_IRUSR)) == 0)
4415 +               return -1;
4416 +
4417 +       *permset |= perm;
4418 +       DEBUG(10,("This is the permset now: %d\n",*permset));
4419 +       return 0;
4420 +}
4421 +
4422 + char *sys_acl_to_text(SMB_ACL_T theacl, ssize_t *plen)
4423 +{
4424 +       return NULL;
4425 +}
4426 +
4427 + SMB_ACL_T sys_acl_init(int count)
4428 +{
4429 +       struct acl_entry_link *theacl = NULL;
4430 +
4431 +       DEBUG(10,("Entering sys_acl_init\n"));
4432 +
4433 +       theacl = (struct acl_entry_link *)malloc(sizeof (struct acl_entry_link));
4434 +       if (theacl == NULL) {
4435 +               errno = ENOMEM;
4436 +               DEBUG(0,("Error in sys_acl_init is %d\n",errno));
4437 +               return NULL;
4438 +       }
4439 +
4440 +       theacl->count = 0;
4441 +       theacl->nextp = NULL;
4442 +       theacl->prevp = NULL;
4443 +       theacl->entryp = NULL;
4444 +       DEBUG(10,("Exiting sys_acl_init\n"));
4445 +       return theacl;
4446 +}
4447 +
4448 + int sys_acl_create_entry(SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
4449 +{
4450 +       struct acl_entry_link *theacl;
4451 +       struct acl_entry_link *acl_entryp;
4452 +       struct acl_entry_link *temp_entry;
4453 +       int counting;
4454 +
4455 +       DEBUG(10,("Entering the sys_acl_create_entry\n"));
4456 +
4457 +       theacl = acl_entryp = *pacl;
4458 +
4459 +       /* Get to the end of the acl before adding entry */
4460 +
4461 +       for (counting=0; counting < theacl->count; counting++){
4462 +               DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4463 +               temp_entry = acl_entryp;
4464 +               acl_entryp = acl_entryp->nextp;
4465 +       }
4466 +
4467 +       if (theacl->count != 0){
4468 +               temp_entry->nextp = acl_entryp = (struct acl_entry_link *)malloc(sizeof (struct acl_entry_link));
4469 +               if (acl_entryp == NULL) {
4470 +                       errno = ENOMEM;
4471 +                       DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4472 +                       return -1;
4473 +               }
4474 +
4475 +               DEBUG(10,("The acl_entryp is %d\n",acl_entryp));
4476 +               acl_entryp->prevp = temp_entry;
4477 +               DEBUG(10,("The acl_entryp->prevp is %d\n",acl_entryp->prevp));
4478 +       }
4479 +
4480 +       *pentry = acl_entryp->entryp = (struct new_acl_entry *)malloc(sizeof (struct new_acl_entry));
4481 +       if (*pentry == NULL) {
4482 +               errno = ENOMEM;
4483 +               DEBUG(0,("Error in sys_acl_create_entry is %d\n",errno));
4484 +               return -1;
4485 +       }
4486 +
4487 +       memset(*pentry,0,sizeof (struct new_acl_entry));
4488 +       acl_entryp->entryp->ace_len = sizeof (struct acl_entry);
4489 +       acl_entryp->entryp->ace_type = ACC_PERMIT;
4490 +       acl_entryp->entryp->ace_id->id_len = sizeof (struct ace_id);
4491 +       acl_entryp->nextp = NULL;
4492 +       theacl->count++;
4493 +       DEBUG(10,("Exiting sys_acl_create_entry\n"));
4494 +       return 0;
4495 +}
4496 +
4497 + int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
4498 +{
4499 +       DEBUG(10,("Starting AIX sys_acl_set_tag_type\n"));
4500 +       entry->ace_id->id_type = tagtype;
4501 +       DEBUG(10,("The tag type is %d\n",entry->ace_id->id_type));
4502 +       DEBUG(10,("Ending AIX sys_acl_set_tag_type\n"));
4503 +}
4504 +
4505 + int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry, void *qual)
4506 +{
4507 +       DEBUG(10,("Starting AIX sys_acl_set_qualifier\n"));
4508 +       memcpy(entry->ace_id->id_data,qual,sizeof (uid_t));
4509 +       DEBUG(10,("Ending AIX sys_acl_set_qualifier\n"));
4510 +       return 0;
4511 +}
4512 +
4513 + int sys_acl_set_permset(SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
4514 +{
4515 +       DEBUG(10,("Starting AIX sys_acl_set_permset\n"));
4516 +       if (!(*permset & S_IXUSR)
4517 +        && !(*permset & S_IWUSR)
4518 +        && !(*permset & S_IRUSR)
4519 +        && *permset != 0)
4520 +               return -1;
4521 +
4522 +       entry->ace_access = *permset;
4523 +       DEBUG(10,("entry->ace_access = %d\n",entry->ace_access));
4524 +       DEBUG(10,("Ending AIX sys_acl_set_permset\n"));
4525 +       return 0;
4526 +}
4527 +
4528 + int sys_acl_valid(SMB_ACL_T theacl)
4529 +{
4530 +       int user_obj = 0;
4531 +       int group_obj = 0;
4532 +       int other_obj = 0;
4533 +       struct acl_entry_link *acl_entry;
4534 +
4535 +       for (acl_entry=theacl; acl_entry != NULL; acl_entry = acl_entry->nextp) {
4536 +               user_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_USER_OBJ);
4537 +               group_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_GROUP_OBJ);
4538 +               other_obj += (acl_entry->entryp->ace_id->id_type == SMB_ACL_OTHER);
4539 +       }
4540 +
4541 +       DEBUG(10,("user_obj=%d, group_obj=%d, other_obj=%d\n",user_obj,group_obj,other_obj));
4542 +
4543 +       if (user_obj != 1 || group_obj != 1 || other_obj != 1)
4544 +               return -1;
4545 +
4546 +       return 0;
4547 +}
4548 +
4549 + int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
4550 +{
4551 +       struct acl_entry_link *acl_entry_link = NULL;
4552 +       struct acl *file_acl = NULL;
4553 +       struct acl *file_acl_temp = NULL;
4554 +       struct acl_entry *acl_entry = NULL;
4555 +       struct ace_id *ace_id = NULL;
4556 +       uint id_type;
4557 +       uint ace_access;
4558 +       uint user_id;
4559 +       uint acl_length;
4560 +       uint rc;
4561 +
4562 +       DEBUG(10,("Entering sys_acl_set_file\n"));
4563 +       DEBUG(10,("File name is %s\n",name));
4564 +
4565 +       /* AIX has no default ACL */
4566 +       if (acltype == SMB_ACL_TYPE_DEFAULT)
4567 +               return 0;
4568 +
4569 +       acl_length = BUFSIZ;
4570 +       file_acl = (struct acl *)malloc(BUFSIZ);
4571 +
4572 +       if (file_acl == NULL) {
4573 +               errno = ENOMEM;
4574 +               DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4575 +               return -1;
4576 +       }
4577 +
4578 +       memset(file_acl,0,BUFSIZ);
4579 +
4580 +       file_acl->acl_len = ACL_SIZ;
4581 +       file_acl->acl_mode = S_IXACL;
4582 +
4583 +       for (acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4584 +               acl_entry_link->entryp->ace_access >>= 6;
4585 +               id_type = acl_entry_link->entryp->ace_id->id_type;
4586 +
4587 +               switch (id_type) {
4588 +               case SMB_ACL_USER_OBJ:
4589 +                       file_acl->u_access = acl_entry_link->entryp->ace_access;
4590 +                       continue;
4591 +               case SMB_ACL_GROUP_OBJ:
4592 +                       file_acl->g_access = acl_entry_link->entryp->ace_access;
4593 +                       continue;
4594 +               case SMB_ACL_OTHER:
4595 +                       file_acl->o_access = acl_entry_link->entryp->ace_access;
4596 +                       continue;
4597 +               case SMB_ACL_MASK:
4598 +                       continue;
4599 +               }
4600 +
4601 +               if ((file_acl->acl_len + sizeof (struct acl_entry)) > acl_length) {
4602 +                       acl_length += sizeof (struct acl_entry);
4603 +                       file_acl_temp = (struct acl *)malloc(acl_length);
4604 +                       if (file_acl_temp == NULL) {
4605 +                               free(file_acl);
4606 +                               errno = ENOMEM;
4607 +                               DEBUG(0,("Error in sys_acl_set_file is %d\n",errno));
4608 +                               return -1;
4609 +                       }
4610 +
4611 +                       memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4612 +                       free(file_acl);
4613 +                       file_acl = file_acl_temp;
4614 +               }
4615 +
4616 +               acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4617 +               file_acl->acl_len += sizeof (struct acl_entry);
4618 +               acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4619 +               acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4620 +
4621 +               /* In order to use this, we'll need to wait until we can get denies */
4622 +               /* if (!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4623 +                       acl_entry->ace_type = ACC_SPECIFY; */
4624 +
4625 +               acl_entry->ace_type = ACC_SPECIFY;
4626 +
4627 +               ace_id = acl_entry->ace_id;
4628 +
4629 +               ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4630 +               DEBUG(10,("The id type is %d\n",ace_id->id_type));
4631 +               ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4632 +               memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof (uid_t));
4633 +               memcpy(acl_entry->ace_id->id_data, &user_id, sizeof (uid_t));
4634 +       }
4635 +
4636 +       rc = chacl((char *)name,file_acl,file_acl->acl_len);
4637 +       DEBUG(10,("errno is %d\n",errno));
4638 +       DEBUG(10,("return code is %d\n",rc));
4639 +       free(file_acl);
4640 +       DEBUG(10,("Exiting the sys_acl_set_file\n"));
4641 +       return rc;
4642 +}
4643 +
4644 + int sys_acl_set_fd(int fd, SMB_ACL_T theacl)
4645 +{
4646 +       struct acl_entry_link *acl_entry_link = NULL;
4647 +       struct acl *file_acl = NULL;
4648 +       struct acl *file_acl_temp = NULL;
4649 +       struct acl_entry *acl_entry = NULL;
4650 +       struct ace_id *ace_id = NULL;
4651 +       uint id_type;
4652 +       uint user_id;
4653 +       uint acl_length;
4654 +       uint rc;
4655 +
4656 +       DEBUG(10,("Entering sys_acl_set_fd\n"));
4657 +       acl_length = BUFSIZ;
4658 +       file_acl = (struct acl *)malloc(BUFSIZ);
4659 +
4660 +       if (file_acl == NULL) {
4661 +               errno = ENOMEM;
4662 +               DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4663 +               return -1;
4664 +       }
4665 +
4666 +       memset(file_acl,0,BUFSIZ);
4667 +
4668 +       file_acl->acl_len = ACL_SIZ;
4669 +       file_acl->acl_mode = S_IXACL;
4670 +
4671 +       for (acl_entry_link=theacl; acl_entry_link != NULL; acl_entry_link = acl_entry_link->nextp) {
4672 +               acl_entry_link->entryp->ace_access >>= 6;
4673 +               id_type = acl_entry_link->entryp->ace_id->id_type;
4674 +               DEBUG(10,("The id_type is %d\n",id_type));
4675 +
4676 +               switch (id_type) {
4677 +               case SMB_ACL_USER_OBJ:
4678 +                       file_acl->u_access = acl_entry_link->entryp->ace_access;
4679 +                       continue;
4680 +               case SMB_ACL_GROUP_OBJ:
4681 +                       file_acl->g_access = acl_entry_link->entryp->ace_access;
4682 +                       continue;
4683 +               case SMB_ACL_OTHER:
4684 +                       file_acl->o_access = acl_entry_link->entryp->ace_access;
4685 +                       continue;
4686 +               case SMB_ACL_MASK:
4687 +                       continue;
4688 +               }
4689 +
4690 +               if ((file_acl->acl_len + sizeof (struct acl_entry)) > acl_length) {
4691 +                       acl_length += sizeof (struct acl_entry);
4692 +                       file_acl_temp = (struct acl *)malloc(acl_length);
4693 +                       if (file_acl_temp == NULL) {
4694 +                               free(file_acl);
4695 +                               errno = ENOMEM;
4696 +                               DEBUG(0,("Error in sys_acl_set_fd is %d\n",errno));
4697 +                               return -1;
4698 +                       }
4699 +
4700 +                       memcpy(file_acl_temp,file_acl,file_acl->acl_len);
4701 +                       free(file_acl);
4702 +                       file_acl = file_acl_temp;
4703 +               }
4704 +
4705 +               acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
4706 +               file_acl->acl_len += sizeof (struct acl_entry);
4707 +               acl_entry->ace_len = acl_entry_link->entryp->ace_len;
4708 +               acl_entry->ace_access = acl_entry_link->entryp->ace_access;
4709 +
4710 +               /* In order to use this, we'll need to wait until we can get denies */
4711 +               /* if (!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
4712 +                       acl_entry->ace_type = ACC_SPECIFY; */
4713 +
4714 +               acl_entry->ace_type = ACC_SPECIFY;
4715 +
4716 +               ace_id = acl_entry->ace_id;
4717 +
4718 +               ace_id->id_type = acl_entry_link->entryp->ace_id->id_type;
4719 +               DEBUG(10,("The id type is %d\n",ace_id->id_type));
4720 +               ace_id->id_len = acl_entry_link->entryp->ace_id->id_len;
4721 +               memcpy(&user_id, acl_entry_link->entryp->ace_id->id_data, sizeof (uid_t));
4722 +               memcpy(ace_id->id_data, &user_id, sizeof (uid_t));
4723 +       }
4724 +
4725 +       rc = fchacl(fd,file_acl,file_acl->acl_len);
4726 +       DEBUG(10,("errno is %d\n",errno));
4727 +       DEBUG(10,("return code is %d\n",rc));
4728 +       free(file_acl);
4729 +       DEBUG(10,("Exiting sys_acl_set_fd\n"));
4730 +       return rc;
4731 +}
4732 +
4733 + int sys_acl_delete_def_file(const char *name)
4734 +{
4735 +       /* AIX has no default ACL */
4736 +       return 0;
4737 +}
4738 +
4739 + int sys_acl_get_perm(SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
4740 +{
4741 +       return *permset & perm;
4742 +}
4743 +
4744 + int sys_acl_free_text(char *text)
4745 +{
4746 +       return 0;
4747 +}
4748 +
4749 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
4750 +{
4751 +       struct acl_entry_link *acl_entry_link;
4752 +
4753 +       for (acl_entry_link = posix_acl->nextp; acl_entry_link->nextp != NULL; acl_entry_link = acl_entry_link->nextp) {
4754 +               free(acl_entry_link->prevp->entryp);
4755 +               free(acl_entry_link->prevp);
4756 +       }
4757 +
4758 +       free(acl_entry_link->prevp->entryp);
4759 +       free(acl_entry_link->prevp);
4760 +       free(acl_entry_link->entryp);
4761 +       free(acl_entry_link);
4762 +
4763 +       return 0;
4764 +}
4765 +
4766 + int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
4767 +{
4768 +       return 0;
4769 +}
4770 +
4771 +#else /* No ACLs. */
4772 +
4773 + int sys_acl_get_entry(UNUSED(SMB_ACL_T the_acl), UNUSED(int entry_id), UNUSED(SMB_ACL_ENTRY_T *entry_p))
4774 +{
4775 +       errno = ENOSYS;
4776 +       return -1;
4777 +}
4778 +
4779 + int sys_acl_get_tag_type(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_TAG_T *tag_type_p))
4780 +{
4781 +       errno = ENOSYS;
4782 +       return -1;
4783 +}
4784 +
4785 + int sys_acl_get_permset(UNUSED(SMB_ACL_ENTRY_T entry_d), UNUSED(SMB_ACL_PERMSET_T *permset_p))
4786 +{
4787 +       errno = ENOSYS;
4788 +       return -1;
4789 +}
4790 +
4791 + void *sys_acl_get_qualifier(UNUSED(SMB_ACL_ENTRY_T entry_d))
4792 +{
4793 +       errno = ENOSYS;
4794 +       return NULL;
4795 +}
4796 +
4797 + SMB_ACL_T sys_acl_get_file(UNUSED(const char *path_p), UNUSED(SMB_ACL_TYPE_T type))
4798 +{
4799 +       errno = ENOSYS;
4800 +       return 0;
4801 +}
4802 +
4803 + SMB_ACL_T sys_acl_get_fd(UNUSED(int fd))
4804 +{
4805 +       errno = ENOSYS;
4806 +       return 0;
4807 +}
4808 +
4809 + int sys_acl_clear_perms(UNUSED(SMB_ACL_PERMSET_T permset))
4810 +{
4811 +       errno = ENOSYS;
4812 +       return -1;
4813 +}
4814 +
4815 + int sys_acl_add_perm(UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
4816 +{
4817 +       errno = ENOSYS;
4818 +       return -1;
4819 +}
4820 +
4821 + int sys_acl_get_perm(UNUSED(SMB_ACL_PERMSET_T permset), UNUSED(SMB_ACL_PERM_T perm))
4822 +{
4823 +       errno = ENOSYS;
4824 +       return permset & perm ? 1 : 0;
4825 +}
4826 +
4827 + char *sys_acl_to_text(UNUSED(SMB_ACL_T the_acl), UNUSED(ssize_t *plen))
4828 +{
4829 +       errno = ENOSYS;
4830 +       return NULL;
4831 +}
4832 +
4833 + int sys_acl_free_text(UNUSED(char *text))
4834 +{
4835 +       errno = ENOSYS;
4836 +       return -1;
4837 +}
4838 +
4839 + SMB_ACL_T sys_acl_init(UNUSED(int count))
4840 +{
4841 +       errno = ENOSYS;
4842 +       return NULL;
4843 +}
4844 +
4845 + int sys_acl_create_entry(UNUSED(SMB_ACL_T *pacl), UNUSED(SMB_ACL_ENTRY_T *pentry))
4846 +{
4847 +       errno = ENOSYS;
4848 +       return -1;
4849 +}
4850 +
4851 + int sys_acl_set_tag_type(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_TAG_T tagtype))
4852 +{
4853 +       errno = ENOSYS;
4854 +       return -1;
4855 +}
4856 +
4857 + int sys_acl_set_qualifier(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(void *qual))
4858 +{
4859 +       errno = ENOSYS;
4860 +       return -1;
4861 +}
4862 +
4863 + int sys_acl_set_permset(UNUSED(SMB_ACL_ENTRY_T entry), UNUSED(SMB_ACL_PERMSET_T permset))
4864 +{
4865 +       errno = ENOSYS;
4866 +       return -1;
4867 +}
4868 +
4869 + int sys_acl_valid(UNUSED(SMB_ACL_T theacl))
4870 +{
4871 +       errno = ENOSYS;
4872 +       return -1;
4873 +}
4874 +
4875 + int sys_acl_set_file(UNUSED(const char *name), UNUSED(SMB_ACL_TYPE_T acltype), UNUSED(SMB_ACL_T theacl))
4876 +{
4877 +       errno = ENOSYS;
4878 +       return -1;
4879 +}
4880 +
4881 + int sys_acl_set_fd(UNUSED(int fd), UNUSED(SMB_ACL_T theacl))
4882 +{
4883 +       errno = ENOSYS;
4884 +       return -1;
4885 +}
4886 +
4887 + int sys_acl_delete_def_file(UNUSED(const char *name))
4888 +{
4889 +       errno = ENOSYS;
4890 +       return -1;
4891 +}
4892 +
4893 + int sys_acl_free_acl(UNUSED(SMB_ACL_T the_acl))
4894 +{
4895 +       errno = ENOSYS;
4896 +       return -1;
4897 +}
4898 +
4899 + int sys_acl_free_qualifier(UNUSED(void *qual), UNUSED(SMB_ACL_TAG_T tagtype))
4900 +{
4901 +       errno = ENOSYS;
4902 +       return -1;
4903 +}
4904 +
4905 +#endif /* No ACLs. */
4906 --- orig/uidlist.c      2004-04-29 19:37:25
4907 +++ uidlist.c   2004-07-03 20:11:58
4908 @@ -34,6 +34,7 @@
4909  extern int verbose;
4910  extern int preserve_uid;
4911  extern int preserve_gid;
4912 +extern int preserve_acls;
4913  extern int numeric_ids;
4914  extern int am_root;
4915  
4916 @@ -274,7 +275,7 @@ void send_uid_list(int f)
4917         if (numeric_ids)
4918                 return;
4919  
4920 -       if (preserve_uid) {
4921 +       if (preserve_uid || preserve_acls) {
4922                 int len;
4923                 /* we send sequences of uid/byte-length/name */
4924                 for (list = uidlist; list; list = list->next) {
4925 @@ -291,7 +292,7 @@ void send_uid_list(int f)
4926                 write_int(f, 0);
4927         }
4928  
4929 -       if (preserve_gid) {
4930 +       if (preserve_gid || preserve_acls) {
4931                 int len;
4932                 for (list = gidlist; list; list = list->next) {
4933                         if (!list->name)
4934 @@ -312,7 +313,7 @@ void recv_uid_list(int f, struct file_li
4935         int id, i;
4936         char *name;
4937  
4938 -       if (preserve_uid && !numeric_ids) {
4939 +       if ((preserve_uid || preserve_acls) && !numeric_ids) {
4940                 /* read the uid list */
4941                 while ((id = read_int(f)) != 0) {
4942                         int len = read_byte(f);
4943 @@ -325,7 +326,7 @@ void recv_uid_list(int f, struct file_li
4944         }
4945  
4946  
4947 -       if (preserve_gid && !numeric_ids) {
4948 +       if ((preserve_gid || preserve_acls) && !numeric_ids) {
4949                 /* read the gid list */
4950                 while ((id = read_int(f)) != 0) {
4951                         int len = read_byte(f);
4952 @@ -337,6 +338,18 @@ void recv_uid_list(int f, struct file_li
4953                 }
4954         }
4955  
4956 +#if SUPPORT_ACLS
4957 +       if (preserve_acls && !numeric_ids) {
4958 +               id_t id;
4959 +               /* the enumerations don't return 0 except to flag the last
4960 +                * entry, since uidlist doesn't munge 0 anyway */
4961 +               while ((id = next_acl_uid(flist)))
4962 +                       acl_uid_map(match_uid(id));
4963 +               while ((id = next_acl_gid(flist)))
4964 +                       acl_gid_map(match_gid(id));
4965 +       }
4966 +#endif /* SUPPORT_ACLS */
4967 +
4968         /* now convert the uid/gid of all files in the list to the mapped
4969          * uid/gid */
4970         if (am_root && preserve_uid && !numeric_ids) {