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