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