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