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