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