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