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