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