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