Fixed failing hunks.
[rsync-patches.git] / acls.diff
1 After applying this patch, run these commands for a successful build:
2
3     autoconf
4     autoheader
5     ./configure --enable-acl-support
6     make proto
7     make
8
9 The program currently complains when the --acls (-A) option is used to copy
10 from a disk that doesn't support ACLs.  This should be changed to silently 
11 notice that no ACLs are available to copy.  Of course, trying to write out
12 ACLs to a non-ACL-supporting disk should complain.
13
14 --- orig/Makefile.in    2005-11-07 04:29:00
15 +++ Makefile.in 2005-11-07 04:31:05
16 @@ -25,15 +25,15 @@ VERSION=@VERSION@
17  .SUFFIXES:
18  .SUFFIXES: .c .o
19  
20 -HEADERS=byteorder.h config.h errcode.h proto.h rsync.h lib/pool_alloc.h
21 +HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
22  LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
23 -       lib/permstring.o lib/pool_alloc.o @LIBOBJS@
24 +       lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
25  ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
26         zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
27  OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
28         main.o checksum.o match.o syscall.o log.o backup.o
29  OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
30 -       fileio.o batch.o clientname.o chmod.o
31 +       fileio.o batch.o clientname.o chmod.o acls.o
32  OBJS3=progress.o pipe.o
33  DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
34  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
35 --- orig/acls.c 2005-07-29 02:48:46
36 +++ acls.c      2005-07-29 02:48:46
37 @@ -0,0 +1,1130 @@
38 +/* -*- c-file-style: "linux" -*-
39 +   Copyright (C) Andrew Tridgell 1996
40 +   Copyright (C) Paul Mackerras 1996
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 preserve_acls;
65 +extern int am_root;
66 +extern int dry_run;
67 +
68 +typedef struct {
69 +       id_t id;
70 +       uchar access;
71 +       SMB_ACL_TAG_T tag_type;
72 +} rsync_ace;
73 +
74 +typedef struct {
75 +       size_t count;
76 +       size_t malloced;
77 +       rsync_ace *races;
78 +} rsync_acl;
79 +
80 +static const rsync_acl rsync_acl_initializer = { 0, 0, NULL };
81 +
82 +static void expand_rsync_acl(rsync_acl *racl)
83 +{
84 +       /* First time through, 0 <= 0, so list is expanded.
85 +        * (Diabolical, rsync guys!) */
86 +       if (racl->malloced <= racl->count) {
87 +               rsync_ace *new_ptr;
88 +               size_t new_size = racl->malloced + 10;
89 +               new_ptr = realloc_array(racl->races, rsync_ace, new_size);
90 +               if (verbose >= 4) {
91 +                       rprintf(FINFO, "expand rsync_acl to %.0f bytes, did%s move\n",
92 +                               (double) new_size * sizeof racl->races[0],
93 +                               racl->races ? "" : " not");
94 +               }
95 +
96 +               racl->races = new_ptr;
97 +               racl->malloced = new_size;
98 +
99 +               if (!racl->races)
100 +                       out_of_memory("expand_rsync_acl");
101 +       }
102 +}
103 +
104 +static void rsync_acl_free(rsync_acl *racl)
105 +{
106 +       free(racl->races);
107 +       racl->races = NULL;
108 +       racl->count = 0;
109 +       racl->malloced = 0;
110 +}
111 +
112 +static int rsync_ace_sorter(const void *r1, const void *r2)
113 +{
114 +       rsync_ace *race1 = (rsync_ace *)r1;
115 +       SMB_ACL_TAG_T rtag1 = race1->tag_type;
116 +       id_t rid1 = race1->id;
117 +       rsync_ace *race2 = (rsync_ace *)r2;
118 +       SMB_ACL_TAG_T rtag2 = race2->tag_type;
119 +       id_t rid2 = race2->id;
120 +       /* start at the extrema */
121 +       if (rtag1 == SMB_ACL_USER_OBJ || rtag2 == SMB_ACL_MASK)
122 +               return -1;
123 +       if (rtag2 == SMB_ACL_USER_OBJ || rtag1 == SMB_ACL_MASK)
124 +               return 1;
125 +       /* work inwards */
126 +       if (rtag1 == SMB_ACL_OTHER)
127 +               return 1;
128 +       if (rtag2 == SMB_ACL_OTHER)
129 +               return -1;
130 +       /* only SMB_ACL_USERs and SMB_ACL_GROUP*s left */
131 +       if (rtag1 == SMB_ACL_USER) {
132 +               switch (rtag2) {
133 +               case SMB_ACL_GROUP:
134 +               case SMB_ACL_GROUP_OBJ:
135 +               case SMB_ACL_OTHER:
136 +                       return -1;
137 +               }
138 +               /* both USER */
139 +               return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
140 +       }
141 +       if (rtag2 == SMB_ACL_USER)
142 +               return 1;
143 +       /* only SMB_ACL_GROUP*s to worry about; kick out GROUP_OBJs first */
144 +       if (rtag1 == SMB_ACL_GROUP_OBJ)
145 +               return -1;
146 +       if (rtag2 == SMB_ACL_GROUP_OBJ)
147 +               return 1;
148 +       /* only SMB_ACL_GROUPs left */
149 +       return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
150 +}
151 +
152 +static void sort_rsync_acl(rsync_acl *racl)
153 +{
154 +       if (!racl->count)
155 +               return;
156 +       qsort((void **)racl->races, racl->count, sizeof racl->races[0],
157 +             &rsync_ace_sorter);
158 +}
159 +
160 +static BOOL unpack_smb_acl(rsync_acl *racl, SMB_ACL_T sacl)
161 +{
162 +       SMB_ACL_ENTRY_T entry;
163 +       int rc;
164 +       const char *errfun;
165 +       *racl = rsync_acl_initializer;
166 +       errfun = "sys_acl_get_entry";
167 +       for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
168 +            rc == 1;
169 +            rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
170 +               SMB_ACL_PERMSET_T permset;
171 +               void *qualifier;
172 +               rsync_ace *race;
173 +               expand_rsync_acl(racl);
174 +               race = &racl->races[racl->count++];
175 +               if ((rc = sys_acl_get_tag_type(entry, &race->tag_type))) {
176 +                       errfun = "sys_acl_get_tag_type";
177 +                       break;
178 +               }
179 +               if ((rc = sys_acl_get_permset(entry, &permset))) {
180 +                       errfun = "sys_acl_get_tag_type";
181 +                       break;
182 +               }
183 +               race->access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
184 +                            | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
185 +                            | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
186 +               switch (race->tag_type) {
187 +               case SMB_ACL_USER:
188 +               case SMB_ACL_GROUP:
189 +                       break;
190 +               default:
191 +                       continue;
192 +               }
193 +               if (!(qualifier = sys_acl_get_qualifier(entry))) {
194 +                       errfun = "sys_acl_get_tag_type";
195 +                       rc = EINVAL;
196 +                       break;
197 +               }
198 +               race->id = *((id_t *)qualifier);
199 +               sys_acl_free_qualifier(qualifier, race->tag_type);
200 +       }
201 +       if (rc) {
202 +               rprintf(FERROR, "unpack_smb_acl: %s(): %s\n",
203 +                       errfun, strerror(errno));
204 +               rsync_acl_free(racl);
205 +               return False;
206 +       }
207 +       sort_rsync_acl(racl);
208 +       return True;
209 +}
210 +
211 +static BOOL rsync_acls_equal(const rsync_acl *racl1, const rsync_acl *racl2)
212 +{
213 +       rsync_ace *race1, *race2;
214 +       size_t count = racl1->count;
215 +       if (count != racl2->count)
216 +               return False;
217 +       race1 = racl1->races;
218 +       race2 = racl2->races;
219 +       for (; count--; race1++, race2++) {
220 +               if (race1->tag_type != race2->tag_type
221 +                || race1->access != race2->access
222 +                || ((race1->tag_type == SMB_ACL_USER
223 +                  || race1->tag_type == SMB_ACL_GROUP)
224 +                 && race1->id != race2->id))
225 +                       return False;
226 +       }
227 +       return True;
228 +}
229 +
230 +typedef struct {
231 +       size_t count;
232 +       size_t malloced;
233 +       rsync_acl *racls;
234 +} rsync_acl_list;
235 +
236 +static rsync_acl_list _rsync_acl_lists[] = {
237 +       { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
238 +       { 0, 0, NULL }  /* SMB_ACL_TYPE_DEFAULT */
239 +};
240 +
241 +static inline rsync_acl_list *rsync_acl_lists(SMB_ACL_TYPE_T type)
242 +{
243 +       return type == SMB_ACL_TYPE_ACCESS ? &_rsync_acl_lists[0]
244 +           : &_rsync_acl_lists[1];
245 +}
246 +
247 +static void expand_rsync_acl_list(rsync_acl_list *racl_list)
248 +{
249 +       /* First time through, 0 <= 0, so list is expanded.
250 +        * (Diabolical, rsync guys!) */
251 +       if (racl_list->malloced <= racl_list->count) {
252 +               rsync_acl *new_ptr;
253 +               size_t new_size;
254 +               if (racl_list->malloced < 1000)
255 +                       new_size = racl_list->malloced + 1000;
256 +               else
257 +                       new_size = racl_list->malloced * 2;
258 +               new_ptr = realloc_array(racl_list->racls, rsync_acl, new_size);
259 +               if (verbose >= 3) {
260 +                       rprintf(FINFO, "expand_rsync_acl_list to %.0f bytes, did%s move\n",
261 +                               (double) new_size * sizeof racl_list->racls[0],
262 +                               racl_list->racls ? "" : " not");
263 +               }
264 +
265 +               racl_list->racls = new_ptr;
266 +               racl_list->malloced = new_size;
267 +
268 +               if (!racl_list->racls)
269 +                       out_of_memory("expand_rsync_acl_list");
270 +       }
271 +}
272 +
273 +#if 0
274 +static void free_rsync_acl_list(rsync_acl_list *racl_list)
275 +{
276 +       /* Run this in reverse, so references are freed before referents,
277 +        * although not currently necessary. */
278 +       while (racl_list->count--) {
279 +               rsync_acl *racl = &racl_list->racls[racl_list->count];
280 +               if (racl)
281 +                       rsync_acl_free(racl);
282 +       }
283 +       free(racl_list->racls);
284 +       racl_list->racls = NULL;
285 +       racl_list->malloced = 0;
286 +}
287 +#endif
288 +
289 +static int find_matching_rsync_acl(SMB_ACL_TYPE_T type,
290 +                                  const rsync_acl_list *racl_list,
291 +                                  const rsync_acl *racl)
292 +{
293 +       static int access_match = -1, default_match = -1;
294 +       int *match = (type == SMB_ACL_TYPE_ACCESS) ?
295 +                       &access_match : &default_match;
296 +       size_t count = racl_list->count;
297 +       /* If this is the first time through or we didn't match the last
298 +        * time, then start at the end of the list, which should be the
299 +        * best place to start hunting. */
300 +       if (*match == -1)
301 +               *match = racl_list->count - 1;
302 +       while (count--) {
303 +               if (rsync_acls_equal(&racl_list->racls[*match], racl))
304 +                       return *match;
305 +               if (!(*match)--)
306 +                       *match = racl_list->count - 1;
307 +       }
308 +       *match = -1;
309 +       return *match;
310 +}
311 +
312 +/* The general strategy with the tag_type <-> character mapping is that
313 + * lowercase implies that no qualifier follows, where uppercase does.
314 + * A similar idiom for the acl type (access or default) itself, but
315 + * lowercase in this instance means there's no ACL following, so the
316 + * ACL is a repeat, so the receiver should reuse the last of the same
317 + * type ACL. */
318 +
319 +static void send_rsync_acl(int f, const rsync_acl *racl)
320 +{
321 +       rsync_ace *race;
322 +       size_t count = racl->count;
323 +       write_int(f, count);
324 +       for (race = racl->races; count--; race++) {
325 +               char ch;
326 +               switch (race->tag_type) {
327 +               case SMB_ACL_USER_OBJ:
328 +                       ch = 'u';
329 +                       break;
330 +               case SMB_ACL_USER:
331 +                       ch = 'U';
332 +                       break;
333 +               case SMB_ACL_GROUP_OBJ:
334 +                       ch = 'g';
335 +                       break;
336 +               case SMB_ACL_GROUP:
337 +                       ch = 'G';
338 +                       break;
339 +               case SMB_ACL_OTHER:
340 +                       ch = 'o';
341 +                       break;
342 +               case SMB_ACL_MASK:
343 +                       ch = 'm';
344 +                       break;
345 +               default:
346 +                       rprintf(FERROR,
347 +                               "send_rsync_acl: unknown tag_type (%0x) on ACE; disregarding\n",
348 +                               race->tag_type);
349 +                       continue;
350 +               }
351 +               write_byte(f, ch);
352 +               write_byte(f, race->access);
353 +               if (isupper((int)ch)) {
354 +                       write_int(f, race->id);
355 +                       /* FIXME: sorta wasteful: we should maybe buffer as
356 +                        * many ids as max(ACL_USER + ACL_GROUP) objects to
357 +                        * keep from making so many calls. */
358 +                       if (ch == 'U')
359 +                               add_uid(race->id);
360 +                       else
361 +                               add_gid(race->id);
362 +               }
363 +       }
364 +}
365 +
366 +static rsync_acl _curr_rsync_acls[2];
367 +
368 +
369 +static const char *str_acl_type(SMB_ACL_TYPE_T type)
370 +{
371 +       return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS" :
372 +               type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT" :
373 +               "unknown SMB_ACL_TYPE_T";
374 +}
375 +
376 +/* Generate the ACL(s) for this flist entry;
377 + * ACL(s) are either sent or cleaned-up by send_acl() below. */
378 +
379 +int make_acl(const struct file_struct *file, const char *fname)
380 +{
381 +       SMB_ACL_TYPE_T *type,
382 +               types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
383 +       rsync_acl *curr_racl;
384 +       if (!preserve_acls || S_ISLNK(file->mode))
385 +               return 1;
386 +       for (type = &types[0], curr_racl = &_curr_rsync_acls[0];
387 +            type < &types[0] + sizeof types / sizeof types[0]
388 +               && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
389 +            type++, curr_racl++) {
390 +               SMB_ACL_T sacl;
391 +               BOOL ok;
392 +               *curr_racl = rsync_acl_initializer;
393 +               if (!(sacl = sys_acl_get_file(fname, *type))) {
394 +                       rprintf(FERROR, "send_acl: sys_acl_get_file(%s, %s): %s\n",
395 +                               fname, str_acl_type(*type), strerror(errno));
396 +                       return -1;
397 +               }
398 +               ok = unpack_smb_acl(curr_racl, sacl);
399 +               sys_acl_free_acl(sacl);
400 +               if (!ok)
401 +                       return -1;
402 +       }
403 +       return 0;
404 +}
405 +
406 +/* Send the make_acl()-generated ACLs for this flist entry,
407 + * or clean up after an flist entry that's not being sent (f == -1). */
408 +
409 +void send_acl(const struct file_struct *file, int f)
410 +{
411 +       SMB_ACL_TYPE_T *type,
412 +               types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
413 +       rsync_acl *curr_racl;
414 +       if (!preserve_acls || S_ISLNK(file->mode))
415 +               return;
416 +       for (type = &types[0], curr_racl = &_curr_rsync_acls[0];
417 +            type < &types[0] + sizeof types / sizeof types[0]
418 +               && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
419 +            type++, curr_racl++) {
420 +               int index;
421 +               rsync_acl_list *racl_list = rsync_acl_lists(*type);
422 +               if (f == -1) {
423 +                       rsync_acl_free(curr_racl);
424 +                       continue;
425 +               }
426 +               if ((index = find_matching_rsync_acl(*type, racl_list, curr_racl))
427 +                   != -1) {
428 +                       write_byte(f, *type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
429 +                       write_int(f, index);
430 +                       rsync_acl_free(curr_racl);
431 +               } else {
432 +                       write_byte(f, *type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
433 +                       send_rsync_acl(f, curr_racl);
434 +                       expand_rsync_acl_list(racl_list);
435 +                       racl_list->racls[racl_list->count++] = *curr_racl;
436 +               }
437 +       }
438 +}
439 +
440 +/* The below stuff is only used by the receiver: */
441 +
442 +/* structure to hold index to rsync_acl_list member corresponding to
443 + * flist->files[i] */
444 +
445 +typedef struct {
446 +       const struct file_struct *file;
447 +       int aclidx;
448 +} file_acl_index;
449 +
450 +typedef struct {
451 +       size_t count;
452 +       size_t malloced;
453 +       file_acl_index *fileaclidxs;
454 +} file_acl_index_list;
455 +
456 +static file_acl_index_list _file_acl_index_lists[] = {
457 +       {0, 0, NULL },/* SMB_ACL_TYPE_ACCESS */
458 +       {0, 0, NULL } /* SMB_ACL_TYPE_DEFAULT */
459 +};
460 +
461 +static inline file_acl_index_list *file_acl_index_lists(SMB_ACL_TYPE_T type)
462 +{
463 +       return type == SMB_ACL_TYPE_ACCESS ?
464 +               &_file_acl_index_lists[0] : &_file_acl_index_lists[1];
465 +}
466 +
467 +static void expand_file_acl_index_list(file_acl_index_list *fileaclidx_list)
468 +{
469 +       /* First time through, 0 <= 0, so list is expanded.
470 +        * (Diabolical, rsync guys!) */
471 +       if (fileaclidx_list->malloced <= fileaclidx_list->count) {
472 +               file_acl_index *new_ptr;
473 +               size_t new_size;
474 +               if (fileaclidx_list->malloced < 1000)
475 +                       new_size = fileaclidx_list->malloced + 1000;
476 +               else
477 +                       new_size = fileaclidx_list->malloced * 2;
478 +               new_ptr = realloc_array(fileaclidx_list->fileaclidxs, file_acl_index, new_size);
479 +               if (verbose >= 3) {
480 +                       rprintf(FINFO, "expand_file_acl_index_list to %.0f bytes, did%s move\n",
481 +                               (double) new_size * sizeof fileaclidx_list->fileaclidxs[0],
482 +                               fileaclidx_list->fileaclidxs ? "" : " not");
483 +               }
484 +
485 +               fileaclidx_list->fileaclidxs = new_ptr;
486 +               fileaclidx_list->malloced = new_size;
487 +
488 +               if (!fileaclidx_list->fileaclidxs)
489 +                       out_of_memory("expand_file_acl_index_list");
490 +       }
491 +}
492 +
493 +#if 0
494 +static void free_file_acl_index_list(file_acl_index_list *fileaclidx_list)
495 +{
496 +       free(fileaclidx_list->fileaclidxs);
497 +       fileaclidx_list->fileaclidxs = NULL;
498 +       fileaclidx_list->malloced = 0;
499 +}
500 +#endif
501 +
502 +/* lists to hold the SMB_ACL_Ts corresponding to the rsync_acl_list entries */
503 +
504 +typedef struct {
505 +       size_t count;
506 +       size_t malloced;
507 +       SMB_ACL_T *sacls;
508 +} smb_acl_list;
509 +
510 +static smb_acl_list _smb_acl_lists[] = {
511 +       { 0, 0, NULL }, /* SMB_ACL_TYPE_ACCESS */
512 +       { 0, 0, NULL }  /* SMB_ACL_TYPE_DEFAULT */
513 +};
514 +
515 +static inline smb_acl_list *smb_acl_lists(SMB_ACL_TYPE_T type)
516 +{
517 +       return type == SMB_ACL_TYPE_ACCESS ? &_smb_acl_lists[0] :
518 +               &_smb_acl_lists[1];
519 +}
520 +
521 +static void expand_smb_acl_list(smb_acl_list *sacl_list)
522 +{
523 +       /* First time through, 0 <= 0, so list is expanded.
524 +        * (Diabolical, rsync guys!) */
525 +       if (sacl_list->malloced <= sacl_list->count) {
526 +               SMB_ACL_T *new_ptr;
527 +               size_t new_size;
528 +               if (sacl_list->malloced < 1000)
529 +                       new_size = sacl_list->malloced + 1000;
530 +               else
531 +                       new_size = sacl_list->malloced * 2;
532 +               new_ptr = realloc_array(sacl_list->sacls, SMB_ACL_T, new_size);
533 +               if (verbose >= 3) {
534 +                       rprintf(FINFO, "expand_smb_acl_list to %.0f bytes, did%s move\n",
535 +                               (double) new_size * sizeof sacl_list->sacls[0],
536 +                               sacl_list->sacls ? "" : " not");
537 +               }
538 +
539 +               sacl_list->sacls = new_ptr;
540 +               sacl_list->malloced = new_size;
541 +
542 +               if (!sacl_list->sacls)
543 +                       out_of_memory("expand_smb_acl_list");
544 +       }
545 +}
546 +
547 +#if 0
548 +static void free_smb_acl_list(SMB_ACL_TYPE_T type)
549 +{
550 +       smb_acl_list *sacl_list = smb_acl_lists(type);
551 +       SMB_ACL_T *sacl = sacl_list->sacls;
552 +       while (sacl_list->count--) {
553 +               if (*sacl)
554 +                       sys_acl_free_acl(*sacl++);
555 +       }
556 +       free(sacl_list->sacls);
557 +       sacl_list->sacls = NULL;
558 +       sacl_list->malloced = 0;
559 +}
560 +#endif
561 +
562 +/* build an SMB_ACL_T corresponding to an rsync_acl */
563 +static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
564 +{
565 +       size_t count = racl->count;
566 +       rsync_ace *race = racl->races;
567 +       const char *errfun = NULL;
568 +       *smb_acl = sys_acl_init(count);
569 +       if (!*smb_acl) {
570 +               rprintf(FERROR, "pack_smb_acl: sys_acl_int(): %s\n",
571 +                       strerror(errno));
572 +               return False;
573 +       }
574 +       for (; count--; race++) {
575 +               SMB_ACL_ENTRY_T entry;
576 +               SMB_ACL_PERMSET_T permset;
577 +               if (sys_acl_create_entry(smb_acl, &entry)) {
578 +                       errfun = "sys_acl_create)";
579 +                       break;
580 +               }
581 +               if (sys_acl_set_tag_type(entry, race->tag_type)) {
582 +                       errfun = "sys_acl_set_tag";
583 +                       break;
584 +               }
585 +               if (race->tag_type == SMB_ACL_USER ||
586 +                   race->tag_type == SMB_ACL_GROUP)
587 +                       if (sys_acl_set_qualifier(entry, (void*)&race->id)) {
588 +                               errfun = "sys_acl_set_qualfier";
589 +                               break;
590 +                       }
591 +               if (sys_acl_get_permset(entry, &permset)) {
592 +                       errfun = "sys_acl_get_permset";
593 +                       break;
594 +               }
595 +               if (sys_acl_clear_perms(permset)) {
596 +                       errfun = "sys_acl_clear_perms";
597 +                       break;
598 +               }
599 +               if (race->access & 4)
600 +                       if (sys_acl_add_perm(permset, SMB_ACL_READ)) {
601 +                               errfun = "sys_acl_add_perm";
602 +                               break;
603 +                       }
604 +               if (race->access & 2)
605 +                       if (sys_acl_add_perm(permset, SMB_ACL_WRITE)) {
606 +                               errfun = "sys_acl_add_perm";
607 +                               break;
608 +                       }
609 +               if (race->access & 1)
610 +                       if (sys_acl_add_perm(permset, SMB_ACL_EXECUTE)) {
611 +                               errfun = "sys_acl_add_perm";
612 +                               break;
613 +                       }
614 +               if (sys_acl_set_permset(entry, permset)) {
615 +                       errfun = "sys_acl_set_permset";
616 +                       break;
617 +               }
618 +       }
619 +       if (errfun) {
620 +               sys_acl_free_acl(*smb_acl);
621 +               rprintf(FERROR, "pack_smb_acl %s(): %s\n", errfun,
622 +                       strerror(errno));
623 +               return False;
624 +       }
625 +       return True;
626 +}
627 +
628 +static void receive_rsync_acl(rsync_acl *racl, int f)
629 +{
630 +#if ACLS_NEED_MASK
631 +       uchar required_mask_perm = 0;
632 +#endif
633 +       BOOL saw_mask = False;
634 +       BOOL saw_user_obj = False, saw_group_obj = False,
635 +               saw_other = False;
636 +       size_t count = read_int(f);
637 +       rsync_ace *race;
638 +       if (!count)
639 +               return;
640 +       while (count--) {
641 +               uchar tag = read_byte(f);
642 +               expand_rsync_acl(racl);
643 +               race = &racl->races[racl->count++];
644 +               switch (tag) {
645 +               case 'u':
646 +                       race->tag_type = SMB_ACL_USER_OBJ;
647 +                       saw_user_obj = True;
648 +                       break;
649 +               case 'U':
650 +                       race->tag_type = SMB_ACL_USER;
651 +                       break;
652 +               case 'g':
653 +                       race->tag_type = SMB_ACL_GROUP_OBJ;
654 +                       saw_group_obj = True;
655 +                       break;
656 +               case 'G':
657 +                       race->tag_type = SMB_ACL_GROUP;
658 +                       break;
659 +               case 'o':
660 +                       race->tag_type = SMB_ACL_OTHER;
661 +                       saw_other = True;
662 +                       break;
663 +               case 'm':
664 +                       race->tag_type = SMB_ACL_MASK;
665 +                       saw_mask = True;
666 +                       break;
667 +               default:
668 +                       rprintf(FERROR, "receive_rsync_acl: unknown tag %c\n",
669 +                               tag);
670 +                       exit_cleanup(RERR_STREAMIO);
671 +               }
672 +               race->access = read_byte(f);
673 +               if (race->access & ~ (4 | 2 | 1)) {
674 +                       rprintf(FERROR, "receive_rsync_acl: bogus permset %o\n",
675 +                               race->access);
676 +                       exit_cleanup(RERR_STREAMIO);
677 +               }
678 +               if (race->tag_type == SMB_ACL_USER ||
679 +                   race->tag_type == SMB_ACL_GROUP) {
680 +                       race->id = read_int(f);
681 +#if ACLS_NEED_MASK
682 +                       required_mask_perm |= race->access;
683 +#endif
684 +               }
685 +#if ACLS_NEED_MASK
686 +               else if (race->tag_type == SMB_ACL_GROUP_OBJ)
687 +                       required_mask_perm |= race->access;
688 +#endif
689 +
690 +       }
691 +       if (!saw_user_obj) {
692 +               expand_rsync_acl(racl);
693 +               race = &racl->races[racl->count++];
694 +               race->tag_type = SMB_ACL_USER_OBJ;
695 +               race->access = 7;
696 +       }
697 +       if (!saw_group_obj) {
698 +               expand_rsync_acl(racl);
699 +               race = &racl->races[racl->count++];
700 +               race->tag_type = SMB_ACL_GROUP_OBJ;
701 +               race->access = 0;
702 +       }
703 +       if (!saw_other) {
704 +               expand_rsync_acl(racl);
705 +               race = &racl->races[racl->count++];
706 +               race->tag_type = SMB_ACL_OTHER;
707 +               race->access = 0;
708 +       }
709 +#if ACLS_NEED_MASK
710 +       if (!saw_mask) {
711 +               expand_rsync_acl(racl);
712 +               race = &racl->races[racl->count++];
713 +               race->tag_type = SMB_ACL_MASK;
714 +               race->access = required_mask_perm;
715 +       }
716 +#else
717 +       /* If we, a system without ACLS_NEED_MASK, received data from a
718 +        * system that has masks, throw away the extraneous CLASS_OBJs. */
719 +       if (saw_mask && racl->count == 4) {
720 +               rsync_ace *group_obj_race = NULL, *mask_race = NULL;
721 +               rsync_ace *p;
722 +               size_t i;
723 +               for (i = 0, p = racl->races; i < racl->count; i++, p++) {
724 +                       if (p->tag_type == SMB_ACL_MASK)
725 +                               mask_race = p;
726 +                       else if (p->tag_type == SMB_ACL_GROUP_OBJ)
727 +                               group_obj_race = p;
728 +               }
729 +               if (mask_race == NULL || group_obj_race == NULL) {
730 +                       rprintf(FERROR, "receive_rsync_acl: have four ACES "
731 +                                       "and one's ACL_MASK but missing "
732 +                                       "either it or ACL_GROUP_OBJ, "
733 +                                       "when pruning ACL\n");
734 +               } else {
735 +                       /* mask off group perms with it first */
736 +                       group_obj_race->access &= mask_race->access;
737 +                       /* dump mask_race; re-slot any followers-on */
738 +                       racl->count--;
739 +                       if (mask_race != &racl->races[racl->count]) {
740 +                               *mask_race = racl->races[racl->count];
741 +                               saw_user_obj = False; /* force re-sort */
742 +                       }
743 +               }
744 +       }
745 +#endif
746 +#if ACLS_NEED_MASK
747 +       if (!(saw_user_obj && saw_group_obj && saw_other && saw_mask))
748 +#else
749 +       if (!(saw_user_obj && saw_group_obj && saw_other))
750 +#endif
751 +               sort_rsync_acl(racl);
752 +}
753 +
754 +/* receive and build the rsync_acl_lists */
755 +
756 +void receive_acl(struct file_struct *file, int f)
757 +{
758 +       SMB_ACL_TYPE_T *type,
759 +               types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
760 +       char *fname;
761 +       if (!preserve_acls || S_ISLNK(file->mode))
762 +               return;
763 +       fname = f_name(file);
764 +       for (type = &types[0];
765 +            type < &types[0] + sizeof types / sizeof types[0]
766 +               && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
767 +            type++) {
768 +               file_acl_index_list *fileaclidx_list =
769 +                       file_acl_index_lists(*type);
770 +               uchar tag;
771 +               expand_file_acl_index_list(fileaclidx_list);
772 +
773 +               tag = read_byte(f);
774 +               if (tag == 'A' || tag == 'a') {
775 +                       if (*type != SMB_ACL_TYPE_ACCESS) {
776 +                               rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
777 +                                       fname);
778 +                               exit_cleanup(RERR_STREAMIO);
779 +                       }
780 +               } else if (tag == 'D' || tag == 'd') {
781 +                       if (*type == SMB_ACL_TYPE_ACCESS) {
782 +                               rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
783 +                                       fname);
784 +                               exit_cleanup(RERR_STREAMIO);
785 +                       }
786 +               } else {
787 +                       rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
788 +                               fname, tag);
789 +                       exit_cleanup(RERR_STREAMIO);
790 +               }
791 +               if (tag == 'A' || tag == 'D') {
792 +                       rsync_acl racl = rsync_acl_initializer;
793 +                       rsync_acl_list *racl_list = rsync_acl_lists(*type);
794 +                       smb_acl_list *sacl_list = smb_acl_lists(*type);
795 +                       fileaclidx_list->fileaclidxs[fileaclidx_list->count].
796 +                               aclidx = racl_list->count;
797 +                       fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
798 +                               file = file;
799 +                       receive_rsync_acl(&racl, f);
800 +                       expand_rsync_acl_list(racl_list);
801 +                       racl_list->racls[racl_list->count++] = racl;
802 +                       expand_smb_acl_list(sacl_list);
803 +                       sacl_list->sacls[sacl_list->count++] = NULL;
804 +               } else {
805 +                       int index = read_int(f);
806 +                       rsync_acl_list *racl_list = rsync_acl_lists(*type);
807 +                       if ((size_t) index >= racl_list->count) {
808 +                               rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
809 +                                       fname,
810 +                                       str_acl_type(*type),
811 +                                       index);
812 +                               exit_cleanup(RERR_STREAMIO);
813 +                       }
814 +                       fileaclidx_list->fileaclidxs[fileaclidx_list->count].
815 +                               aclidx = index;
816 +                       fileaclidx_list->fileaclidxs[fileaclidx_list->count++].
817 +                               file = file;
818 +               }
819 +       }
820 +}
821 +
822 +static int file_acl_index_list_sorter(const void *f1, const void *f2)
823 +{
824 +       const file_acl_index *fileaclidx1 = (const file_acl_index *)f1;
825 +       const file_acl_index *fileaclidx2 = (const file_acl_index *)f2;
826 +       return fileaclidx1->file == fileaclidx2->file ? 0 :
827 +               fileaclidx1->file < fileaclidx2->file ? -1 : 1;
828 +}
829 +
830 +void sort_file_acl_index_lists()
831 +{
832 +       SMB_ACL_TYPE_T *type,
833 +               types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
834 +       if (!preserve_acls)
835 +               return;
836 +       for (type = &types[0];
837 +            type < &types[0] + sizeof types / sizeof types[0];
838 +            type++)
839 +       {
840 +               file_acl_index_list *fileaclidx_list =
841 +                       file_acl_index_lists(*type);
842 +               if (!fileaclidx_list->count)
843 +                       continue;
844 +               qsort(fileaclidx_list->fileaclidxs, fileaclidx_list->count,
845 +                     sizeof fileaclidx_list->fileaclidxs[0],
846 +                     &file_acl_index_list_sorter);
847 +       }
848 +}
849 +
850 +static int find_file_acl_index(const file_acl_index_list *fileaclidx_list,
851 +                              const struct file_struct *file) {
852 +       int low = 0, high = fileaclidx_list->count;
853 +       const struct file_struct *file_mid;
854 +       if (!high--)
855 +               return -1;
856 +       do {
857 +               int mid = (high + low) / 2;
858 +               file_mid = fileaclidx_list->fileaclidxs[mid].file;
859 +               if (file_mid == file)
860 +                       return fileaclidx_list->fileaclidxs[mid].aclidx;
861 +               if (file_mid > file)
862 +                       high = mid - 1;
863 +               else
864 +                       low = mid + 1;
865 +       } while (low < high);
866 +       if (low == high) {
867 +               file_mid = fileaclidx_list->fileaclidxs[low].file;
868 +               if (file_mid == file)
869 +                       return fileaclidx_list->fileaclidxs[low].aclidx;
870 +       }
871 +       rprintf(FERROR,
872 +               "find_file_acl_index: can't find entry for file in list\n");
873 +       exit_cleanup(RERR_STREAMIO);
874 +       return -1;
875 +}
876 +
877 +/* for duplicating ACLs on backups when using backup_dir */
878 +
879 +int dup_acl(const char *orig, const char *bak, mode_t mode)
880 +{
881 +       SMB_ACL_TYPE_T *type,
882 +               types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
883 +       int ret = 0;
884 +       if (!preserve_acls)
885 +               return 1;
886 +       for (type = &types[0];
887 +            type < &types[0] + sizeof types / sizeof types[0]
888 +                && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(mode));
889 +            type++) {
890 +               SMB_ACL_T sacl_orig, sacl_bak;
891 +               rsync_acl racl_orig, racl_bak;
892 +               if (!(sacl_orig = sys_acl_get_file(orig, *type))) {
893 +                       rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s\n",
894 +                               orig, str_acl_type(*type), strerror(errno));
895 +                       ret = -1;
896 +                       continue;
897 +               }
898 +               if (!(sacl_bak = sys_acl_get_file(orig, *type))) {
899 +                       rprintf(FERROR, "dup_acl: sys_acl_get_file(%s, %s): %s. ignoring\n",
900 +                               bak, str_acl_type(*type), strerror(errno));
901 +                       ret = -1;
902 +                       /* try to forge on through */
903 +               }
904 +               if (!unpack_smb_acl(&racl_orig, sacl_orig)) {
905 +                       ret = -1;
906 +                       goto out_with_sacls;
907 +               }
908 +               if (sacl_bak) {
909 +                       if (!unpack_smb_acl(&racl_bak, sacl_bak)) {
910 +                               ret = -1;
911 +                               goto out_with_one_racl;
912 +                       }
913 +                       if (rsync_acls_equal(&racl_orig, &racl_bak))
914 +                               goto out_with_all;
915 +               } else {
916 +                       ; /* presume they're unequal */
917 +               }
918 +               if (*type == SMB_ACL_TYPE_DEFAULT && !racl_orig.count) {
919 +                       if (-1 == sys_acl_delete_def_file(bak)) {
920 +                               rprintf(FERROR, "dup_acl: sys_acl_delete_def_file(%s): %s\n",
921 +                                       bak, strerror(errno));
922 +                               ret = -1;
923 +                       }
924 +               } else if (-1 == sys_acl_set_file(bak, *type, sacl_bak)) {
925 +                       rprintf(FERROR, "dup_acl: sys_acl_set_file(%s, %s): %s\n",
926 +                               bak, str_acl_type(*type), strerror(errno));
927 +                       ret = -1;
928 +               }
929 +               out_with_all:
930 +                       if (sacl_bak)
931 +                               rsync_acl_free(&racl_bak);
932 +               out_with_one_racl:
933 +                       rsync_acl_free(&racl_orig);
934 +               out_with_sacls:
935 +                       if (sacl_bak)
936 +                               sys_acl_free_acl(sacl_bak);
937 +               /* out_with_one_sacl: */
938 +                       if (sacl_orig)
939 +                               sys_acl_free_acl(sacl_orig);
940 +       }
941 +       return ret;
942 +}
943 +
944 +/* Stuff for redirecting calls to set_acl() from set_perms()
945 + * for keep_backup(). */
946 +static const struct file_struct *backup_orig_file = NULL;
947 +static const char null_string[] = "";
948 +static const char *backup_orig_fname = null_string;
949 +static const char *backup_dest_fname = null_string;
950 +static SMB_ACL_T _backup_sacl[] = { NULL, NULL };
951 +
952 +void push_keep_backup_acl(const struct file_struct *file,
953 +                         const char *orig, const char *dest)
954 +{
955 +       if (preserve_acls) {
956 +               SMB_ACL_TYPE_T *type,
957 +                       types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
958 +               SMB_ACL_T *sacl;
959 +               backup_orig_file = file;
960 +               backup_orig_fname = orig;
961 +               backup_dest_fname = dest;
962 +               for (type = &types[0], sacl = &_backup_sacl[0];
963 +                    type < &types[0] + sizeof types / sizeof types[0];
964 +                    type++) {
965 +                       if (*type == SMB_ACL_TYPE_DEFAULT && !S_ISDIR(file->mode))
966 +                               *sacl = NULL;
967 +                       else {
968 +                               if (!(*sacl = sys_acl_get_file(orig, *type))) {
969 +                                       rprintf(FERROR, "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
970 +                                               orig, str_acl_type(*type),
971 +                                               strerror(errno));
972 +                               }
973 +                       }
974 +               }
975 +       }
976 +}
977 +
978 +static int set_keep_backup_acl()
979 +{
980 +       if (preserve_acls) {
981 +               SMB_ACL_TYPE_T *type,
982 +                       types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
983 +               SMB_ACL_T *sacl;
984 +               int ret = 0;
985 +               for (type = &types[0], sacl = &_backup_sacl[0];
986 +                    type < &types[0] + sizeof types / sizeof types[0];
987 +                    type++) {
988 +                       if (*sacl) {
989 +                               if (-1 == sys_acl_set_file(backup_dest_fname,
990 +                                                          *type, *sacl))
991 +                               {
992 +                                       rprintf(FERROR, "push_keep_backup_acl: sys_acl_get_file(%s, %s): %s\n",
993 +                                               backup_dest_fname,
994 +                                               str_acl_type(*type),
995 +                                               strerror(errno));
996 +                                       ret = -1;
997 +                               }
998 +                       }
999 +               }
1000 +               return ret;
1001 +       }
1002 +       return 1;
1003 +}
1004 +
1005 +void cleanup_keep_backup_acl()
1006 +{
1007 +       if (preserve_acls) {
1008 +               SMB_ACL_TYPE_T *type,
1009 +                       types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
1010 +               SMB_ACL_T *sacl;
1011 +               backup_orig_file = NULL;
1012 +               backup_orig_fname = null_string;
1013 +               backup_dest_fname = null_string;
1014 +               for (type = &types[0], sacl = &_backup_sacl[0];
1015 +                    type < &types[0] + sizeof types / sizeof types[0];
1016 +                    type++) {
1017 +                       if (*sacl)
1018 +                               sys_acl_free_acl(*sacl);
1019 +                       *sacl = NULL;
1020 +               }
1021 +       }
1022 +}
1023 +
1024 +/* set ACL on rsync-ed or keep_backup-ed file */
1025 +
1026 +int set_acl(const char *fname, const struct file_struct *file)
1027 +{
1028 +       int updated = 0;
1029 +       SMB_ACL_TYPE_T *type,
1030 +               types[] = {SMB_ACL_TYPE_ACCESS, SMB_ACL_TYPE_DEFAULT};
1031 +       if (dry_run || !preserve_acls || S_ISLNK(file->mode))
1032 +               return 1;
1033 +       if (file == backup_orig_file) {
1034 +               if (!strcmp(fname, backup_dest_fname))
1035 +                       return set_keep_backup_acl();
1036 +       }
1037 +       for (type = &types[0];
1038 +            type < &types[0] + sizeof  types / sizeof types[0]
1039 +               && (*type == SMB_ACL_TYPE_ACCESS || S_ISDIR(file->mode));
1040 +            type++) {
1041 +               SMB_ACL_T sacl_orig, *sacl_new;
1042 +               rsync_acl racl_orig, *racl_new;
1043 +               int aclidx = find_file_acl_index(file_acl_index_lists(*type),
1044 +                                                file);
1045 +               BOOL ok;
1046 +               racl_new = &(rsync_acl_lists(*type)->racls[aclidx]);
1047 +               sacl_new = &(smb_acl_lists(*type)->sacls[aclidx]);
1048 +               sacl_orig = sys_acl_get_file(fname, *type);
1049 +               if (!sacl_orig) {
1050 +                       rprintf(FERROR, "set_acl: sys_acl_get_file(%s, %s): %s\n",
1051 +                               fname, str_acl_type(*type), strerror(errno));
1052 +                       updated = -1;
1053 +                       continue;
1054 +               }
1055 +               ok = unpack_smb_acl(&racl_orig, sacl_orig);
1056 +               sys_acl_free_acl(sacl_orig);
1057 +               if (!ok) {
1058 +                       updated = -1;
1059 +                       continue;
1060 +               }
1061 +               ok = rsync_acls_equal(&racl_orig, racl_new);
1062 +               rsync_acl_free(&racl_orig);
1063 +               if (ok)
1064 +                       continue;
1065 +               if (*type == SMB_ACL_TYPE_DEFAULT && !racl_new->count) {
1066 +                       if (-1 == sys_acl_delete_def_file(fname)) {
1067 +                               rprintf(FERROR, "set_acl: sys_acl_delete_def_file(%s): %s\n",
1068 +                                       fname, strerror(errno));
1069 +                               updated = -1;
1070 +                               continue;
1071 +                       }
1072 +               } else {
1073 +                       if (!*sacl_new)
1074 +                               if (!pack_smb_acl(sacl_new, racl_new)) {
1075 +                                       updated = -1;
1076 +                                       continue;
1077 +                               }
1078 +                       if (-1 == sys_acl_set_file(fname, *type, *sacl_new)) {
1079 +                               rprintf(FERROR, "set_acl: sys_acl_set_file(%s, %s): %s\n",
1080 +                                       fname, str_acl_type(*type),
1081 +                                       strerror(errno));
1082 +                               updated = -1;
1083 +                               continue;
1084 +                       }
1085 +               }
1086 +               if (!updated)
1087 +                       updated = 1;
1088 +       }
1089 +       return updated;
1090 +}
1091 +
1092 +/* Enumeration functions for uid mapping: */
1093 +
1094 +/* Context -- one and only one.  Should be cycled through once on uid
1095 + * mapping and once on gid mapping. */
1096 +static rsync_acl_list *_enum_racl_lists[] = {
1097 +       &_rsync_acl_lists[0], &_rsync_acl_lists[1], NULL
1098 +};
1099 +
1100 +static rsync_acl_list **enum_racl_list = &_enum_racl_lists[0];
1101 +static size_t enum_racl_index = 0;
1102 +static size_t enum_race_index = 0;
1103 +
1104 +/* This returns the next tag_type id from the given acl for the next entry,
1105 + * or it returns 0 if there are no more tag_type ids in the acl. */
1106 +
1107 +static id_t next_ace_id(SMB_ACL_TAG_T tag_type, const rsync_acl *racl)
1108 +{
1109 +       for (; enum_race_index < racl->count; enum_race_index++) {
1110 +               rsync_ace *race = &racl->races[enum_race_index];
1111 +               if (race->tag_type == tag_type)
1112 +                       return race->id;
1113 +       }
1114 +       enum_race_index = 0;
1115 +       return 0;
1116 +}
1117 +
1118 +static id_t next_acl_id(SMB_ACL_TAG_T tag_type, const rsync_acl_list *racl_list)
1119 +{
1120 +       for (; enum_racl_index < racl_list->count; enum_racl_index++) {
1121 +               rsync_acl *racl = &racl_list->racls[enum_racl_index];
1122 +               id_t id = next_ace_id(tag_type, racl);
1123 +               if (id)
1124 +                       return id;
1125 +       }
1126 +       enum_racl_index = 0;
1127 +       return 0;
1128 +}
1129 +
1130 +static id_t next_acl_list_id(SMB_ACL_TAG_T tag_type)
1131 +{
1132 +       for (; *enum_racl_list; enum_racl_list++) {
1133 +               id_t id = next_acl_id(tag_type, *enum_racl_list);
1134 +               if (id)
1135 +                       return id;
1136 +       }
1137 +       enum_racl_list = &_enum_racl_lists[0];
1138 +       return 0;
1139 +}
1140 +
1141 +id_t next_acl_uid()
1142 +{
1143 +       return next_acl_list_id(SMB_ACL_USER);
1144 +}
1145 +
1146 +id_t next_acl_gid()
1147 +{
1148 +       return next_acl_list_id(SMB_ACL_GROUP);
1149 +}
1150 +
1151 +/* referring to the global context enum_entry, sets the entry's id */
1152 +static void set_acl_id(id_t id)
1153 +{
1154 +       (*enum_racl_list)->racls[enum_racl_index].races[enum_race_index++].id = id;
1155 +}
1156 +
1157 +void acl_uid_map(id_t uid)
1158 +{
1159 +       set_acl_id(uid);
1160 +}
1161 +
1162 +void acl_gid_map(id_t gid)
1163 +{
1164 +       set_acl_id(gid);
1165 +}
1166 +
1167 +#endif /* SUPPORT_ACLS */
1168 --- orig/backup.c       2005-06-10 21:33:27
1169 +++ backup.c    2004-10-06 00:13:09
1170 @@ -135,6 +135,7 @@ static int make_bak_dir(char *fullpath)
1171                         } else {
1172                                 do_lchown(fullpath, st.st_uid, st.st_gid);
1173                                 do_chmod(fullpath, st.st_mode);
1174 +                               (void)DUP_ACL(end, fullpath, st.st_mode);
1175                         }
1176                 }
1177                 *p = '/';
1178 @@ -187,6 +188,8 @@ static int keep_backup(char *fname)
1179         if (!(buf = get_backup_name(fname)))
1180                 return 0;
1181  
1182 +       PUSH_KEEP_BACKUP_ACL(file, fname, buf);
1183 +
1184         /* Check to see if this is a device file, or link */
1185         if (IS_DEVICE(file->mode) && am_root && preserve_devices) {
1186                 do_unlink(buf);
1187 @@ -261,6 +264,7 @@ static int keep_backup(char *fname)
1188                 }
1189         }
1190         set_perms(buf, file, NULL, 0);
1191 +       CLEANUP_KEEP_BACKUP_ACL();
1192         free(file);
1193  
1194         if (verbose > 1) {
1195 --- orig/configure.in   2005-09-24 17:40:30
1196 +++ configure.in        2004-08-19 19:53:27
1197 @@ -489,6 +489,11 @@ if test x"$ac_cv_func_strcasecmp" = x"no
1198      AC_CHECK_LIB(resolv, strcasecmp)
1199  fi
1200  
1201 +AC_CHECK_FUNCS(aclsort)
1202 +if test x"$ac_cv_func_aclsort" = x"no"; then
1203 +    AC_CHECK_LIB(sec, aclsort)
1204 +fi
1205 +
1206  dnl At the moment we don't test for a broken memcmp(), because all we
1207  dnl need to do is test for equality, not comparison, and it seems that
1208  dnl every platform has a memcmp that can do at least that.
1209 @@ -746,6 +751,77 @@ AC_SUBST(OBJ_RESTORE)
1210  AC_SUBST(CC_SHOBJ_FLAG)
1211  AC_SUBST(BUILD_POPT)
1212  
1213 +AC_CHECK_HEADERS(sys/acl.h)
1214 +AC_CHECK_FUNCS(_acl __acl _facl __facl)
1215 +#################################################
1216 +# check for ACL support
1217 +
1218 +AC_MSG_CHECKING(whether to support ACLs)
1219 +AC_ARG_ENABLE(acl-support,
1220 +AC_HELP_STRING([--enable-acl-support], [Include ACL support (default=no)]),
1221 +[ case "$withval" in
1222 +  yes)
1223 +
1224 +               case "$host_os" in
1225 +               *sysv5*)
1226 +                       AC_MSG_RESULT(Using UnixWare ACLs)
1227 +                       AC_DEFINE(HAVE_UNIXWARE_ACLS, 1, [true if you have UnixWare ACLs])
1228 +                       ;;
1229 +               *solaris*)
1230 +                       AC_MSG_RESULT(Using solaris ACLs)
1231 +                       AC_DEFINE(HAVE_SOLARIS_ACLS, 1, [true if you have solaris ACLs])
1232 +                       ;;
1233 +               *hpux*)
1234 +                       AC_MSG_RESULT(Using HPUX ACLs)
1235 +                       AC_DEFINE(HAVE_HPUX_ACLS, 1, [true if you have HPUX ACLs])
1236 +                       ;;
1237 +               *irix*)
1238 +                       AC_MSG_RESULT(Using IRIX ACLs)
1239 +                       AC_DEFINE(HAVE_IRIX_ACLS, 1, [true if you have IRIX ACLs])
1240 +                       ;;
1241 +               *aix*)
1242 +                       AC_MSG_RESULT(Using AIX ACLs)
1243 +                       AC_DEFINE(HAVE_AIX_ACLS, 1, [true if you have AIX ACLs])
1244 +                       ;;
1245 +               *osf*)
1246 +                       AC_MSG_RESULT(Using Tru64 ACLs)
1247 +                       AC_DEFINE(HAVE_TRU64_ACLS, 1, [true if you have Tru64 ACLs])
1248 +                       LIBS="$LIBS -lpacl"
1249 +                       ;;
1250 +               *)
1251 +                   AC_MSG_RESULT(ACLs requested -- running tests)
1252 +                   AC_CHECK_LIB(acl,acl_get_file)
1253 +                       AC_CACHE_CHECK([for ACL support],samba_cv_HAVE_POSIX_ACLS,[
1254 +                       AC_TRY_LINK([#include <sys/types.h>
1255 +#include <sys/acl.h>],
1256 +[ acl_t acl; int entry_id; acl_entry_t *entry_p; return acl_get_entry( acl, entry_id, entry_p);],
1257 +samba_cv_HAVE_POSIX_ACLS=yes,samba_cv_HAVE_POSIX_ACLS=no)])
1258 +                       if test x"$samba_cv_HAVE_POSIX_ACLS" = x"yes"; then
1259 +                           AC_MSG_RESULT(Using posix ACLs)
1260 +                           AC_DEFINE(HAVE_POSIX_ACLS, 1, [true if you have posix ACLs])
1261 +                           AC_CACHE_CHECK([for acl_get_perm_np],samba_cv_HAVE_ACL_GET_PERM_NP,[
1262 +                               AC_TRY_LINK([#include <sys/types.h>
1263 +#include <sys/acl.h>],
1264 +[ acl_permset_t permset_d; acl_perm_t perm; return acl_get_perm_np( permset_d, perm);],
1265 +samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_cv_HAVE_ACL_GET_PERM_NP=no)])
1266 +                           if test x"$samba_cv_HAVE_ACL_GET_PERM_NP" = x"yes"; then
1267 +                               AC_DEFINE(HAVE_ACL_GET_PERM_NP, 1, [true if you have acl_get_perm_np])
1268 +                           fi
1269 +                       else
1270 +                           AC_MSG_ERROR(Failed to find ACL support)
1271 +                       fi
1272 +                       ;;
1273 +               esac
1274 +               ;;
1275 +  *)
1276 +    AC_MSG_RESULT(no)
1277 +       AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1278 +    ;;
1279 +  esac ],
1280 +  AC_DEFINE(HAVE_NO_ACLS, 1, [true if you don't have ACLs])
1281 +  AC_MSG_RESULT(no)
1282 +)
1283 +
1284  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
1285  AC_OUTPUT
1286  
1287 --- orig/flist.c        2005-11-07 04:29:01
1288 +++ flist.c     2005-07-29 02:49:06
1289 @@ -960,6 +960,8 @@ static struct file_struct *send_file_nam
1290         file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS);
1291         if (!file)
1292                 return NULL;
1293 +       if (MAKE_ACL(file, fname) < 0)
1294 +               return NULL;
1295  
1296         maybe_emit_filelist_progress(flist->count + flist_count_offset);
1297  
1298 @@ -968,6 +970,10 @@ static struct file_struct *send_file_nam
1299         if (file->basename[0]) {
1300                 flist->files[flist->count++] = file;
1301                 send_file_entry(file, f, base_flags);
1302 +               SEND_ACL(file, f);
1303 +       } else {
1304 +               /* Cleanup unsent ACL(s). */
1305 +               SEND_ACL(file, -1);
1306         }
1307         return file;
1308  }
1309 @@ -1323,6 +1329,8 @@ struct file_list *recv_file_list(int f)
1310                         flags |= read_byte(f) << 8;
1311                 file = receive_file_entry(flist, flags, f);
1312  
1313 +               RECEIVE_ACL(file, f);
1314 +
1315                 if (S_ISREG(file->mode))
1316                         stats.total_size += file->length;
1317  
1318 @@ -1345,6 +1353,8 @@ struct file_list *recv_file_list(int f)
1319  
1320         clean_flist(flist, relative_paths, 1);
1321  
1322 +       SORT_FILE_ACL_INDEX_LISTS();
1323 +
1324         if (f >= 0) {
1325                 /* Now send the uid/gid list. This was introduced in
1326                  * protocol version 15 */
1327 --- orig/generator.c    2005-10-30 22:30:28
1328 +++ generator.c 2005-05-12 23:34:00
1329 @@ -725,6 +725,10 @@ static void recv_generator(char *fname, 
1330                 if (set_perms(fname, file, statret ? NULL : &st, 0)
1331                     && verbose && code && f_out != -1)
1332                         rprintf(code, "%s/\n", safe_fname(fname));
1333 +#ifdef SUPPORT_ACLS
1334 +               if (f_out == -1)
1335 +                       SET_ACL(fname, file);
1336 +#endif
1337                 if (delete_during && f_out != -1 && !phase && dry_run < 2
1338                     && (file->flags & FLAG_DEL_HERE))
1339                         delete_in_dir(the_file_list, fname, file);
1340 --- orig/lib/sysacls.c  2005-05-16 23:27:53
1341 +++ lib/sysacls.c       2005-05-16 23:27:53
1342 @@ -0,0 +1,3242 @@
1343 +/* 
1344 +   Unix SMB/CIFS implementation.
1345 +   Samba system utilities for ACL support.
1346 +   Copyright (C) Jeremy Allison 2000.
1347 +   
1348 +   This program is free software; you can redistribute it and/or modify
1349 +   it under the terms of the GNU General Public License as published by
1350 +   the Free Software Foundation; either version 2 of the License, or
1351 +   (at your option) any later version.
1352 +   
1353 +   This program is distributed in the hope that it will be useful,
1354 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
1355 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1356 +   GNU General Public License for more details.
1357 +   
1358 +   You should have received a copy of the GNU General Public License
1359 +   along with this program; if not, write to the Free Software
1360 +   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1361 +*/
1362 +
1363 +#include "rsync.h"
1364 +#include "sysacls.h" /****** ADDED ******/
1365 +
1366 +/****** EXTRAS -- THESE ITEMS ARE NOT FROM THE SAMBA SOURCE ******/
1367 +void SAFE_FREE(void *mem)
1368 +{
1369 +       if (mem)
1370 +               free(mem);
1371 +}
1372 +
1373 +char *uidtoname(uid_t uid)
1374 +{
1375 +       static char idbuf[12];
1376 +       struct passwd *pw;
1377 +
1378 +       if ((pw = getpwuid(uid)) == NULL) {
1379 +               slprintf(idbuf, sizeof(idbuf)-1, "%ld", (long)uid);
1380 +               return idbuf;
1381 +       }
1382 +       return pw->pw_name;
1383 +}
1384 +/****** EXTRAS -- END ******/
1385 +
1386 +/*
1387 + This file wraps all differing system ACL interfaces into a consistent
1388 + one based on the POSIX interface. It also returns the correct errors
1389 + for older UNIX systems that don't support ACLs.
1390 +
1391 + The interfaces that each ACL implementation must support are as follows :
1392 +
1393 + int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1394 + int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1395 + int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p
1396 + void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1397 + SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1398 + SMB_ACL_T sys_acl_get_fd(int fd)
1399 + int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset);
1400 + int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm);
1401 + char *sys_acl_to_text( SMB_ACL_T theacl, ssize_t *plen)
1402 + SMB_ACL_T sys_acl_init( int count)
1403 + int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1404 + int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1405 + int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1406 + int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1407 + int sys_acl_valid( SMB_ACL_T theacl )
1408 + int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1409 + int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1410 + int sys_acl_delete_def_file(const char *path)
1411 +
1412 + This next one is not POSIX complient - but we *have* to have it !
1413 + More POSIX braindamage.
1414 +
1415 + int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1416 +
1417 + The generic POSIX free is the following call. We split this into
1418 + several different free functions as we may need to add tag info
1419 + to structures when emulating the POSIX interface.
1420 +
1421 + int sys_acl_free( void *obj_p)
1422 +
1423 + The calls we actually use are :
1424 +
1425 + int sys_acl_free_text(char *text) - free acl_to_text
1426 + int sys_acl_free_acl(SMB_ACL_T posix_acl)
1427 + int sys_acl_free_qualifier(void *qualifier, SMB_ACL_TAG_T tagtype)
1428 +
1429 +*/
1430 +
1431 +#if defined(HAVE_POSIX_ACLS)
1432 +
1433 +/* Identity mapping - easy. */
1434 +
1435 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1436 +{
1437 +       return acl_get_entry( the_acl, entry_id, entry_p);
1438 +}
1439 +
1440 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1441 +{
1442 +       return acl_get_tag_type( entry_d, tag_type_p);
1443 +}
1444 +
1445 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1446 +{
1447 +       return acl_get_permset( entry_d, permset_p);
1448 +}
1449 +
1450 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1451 +{
1452 +       return acl_get_qualifier( entry_d);
1453 +}
1454 +
1455 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1456 +{
1457 +       return acl_get_file( path_p, type);
1458 +}
1459 +
1460 +SMB_ACL_T sys_acl_get_fd(int fd)
1461 +{
1462 +       return acl_get_fd(fd);
1463 +}
1464 +
1465 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1466 +{
1467 +       return acl_clear_perms(permset);
1468 +}
1469 +
1470 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1471 +{
1472 +       return acl_add_perm(permset, perm);
1473 +}
1474 +
1475 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1476 +{
1477 +#if defined(HAVE_ACL_GET_PERM_NP)
1478 +       /*
1479 +        * Required for TrustedBSD-based ACL implementations where
1480 +        * non-POSIX.1e functions are denoted by a _np (non-portable)
1481 +        * suffix.
1482 +        */
1483 +       return acl_get_perm_np(permset, perm);
1484 +#else
1485 +       return acl_get_perm(permset, perm);
1486 +#endif
1487 +}
1488 +
1489 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1490 +{
1491 +       return acl_to_text( the_acl, plen);
1492 +}
1493 +
1494 +SMB_ACL_T sys_acl_init( int count)
1495 +{
1496 +       return acl_init(count);
1497 +}
1498 +
1499 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1500 +{
1501 +       return acl_create_entry(pacl, pentry);
1502 +}
1503 +
1504 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1505 +{
1506 +       return acl_set_tag_type(entry, tagtype);
1507 +}
1508 +
1509 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1510 +{
1511 +       return acl_set_qualifier(entry, qual);
1512 +}
1513 +
1514 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1515 +{
1516 +       return acl_set_permset(entry, permset);
1517 +}
1518 +
1519 +int sys_acl_valid( SMB_ACL_T theacl )
1520 +{
1521 +       return acl_valid(theacl);
1522 +}
1523 +
1524 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1525 +{
1526 +       return acl_set_file(name, acltype, theacl);
1527 +}
1528 +
1529 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1530 +{
1531 +       return acl_set_fd(fd, theacl);
1532 +}
1533 +
1534 +int sys_acl_delete_def_file(const char *name)
1535 +{
1536 +       return acl_delete_def_file(name);
1537 +}
1538 +
1539 +int sys_acl_free_text(char *text)
1540 +{
1541 +       return acl_free(text);
1542 +}
1543 +
1544 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
1545 +{
1546 +       return acl_free(the_acl);
1547 +}
1548 +
1549 +int sys_acl_free_qualifier(void *qual, UNUSED(SMB_ACL_TAG_T tagtype))
1550 +{
1551 +       return acl_free(qual);
1552 +}
1553 +
1554 +#elif defined(HAVE_TRU64_ACLS)
1555 +/*
1556 + * The interface to DEC/Compaq Tru64 UNIX ACLs
1557 + * is based on Draft 13 of the POSIX spec which is
1558 + * slightly different from the Draft 16 interface.
1559 + * 
1560 + * Also, some of the permset manipulation functions
1561 + * such as acl_clear_perm() and acl_add_perm() appear
1562 + * to be broken on Tru64 so we have to manipulate
1563 + * the permission bits in the permset directly.
1564 + */
1565 +int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1566 +{
1567 +       SMB_ACL_ENTRY_T entry;
1568 +
1569 +       if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {
1570 +               return -1;
1571 +       }
1572 +
1573 +       errno = 0;
1574 +       if ((entry = acl_get_entry(the_acl)) != NULL) {
1575 +               *entry_p = entry;
1576 +               return 1;
1577 +       }
1578 +
1579 +       return errno ? -1 : 0;
1580 +}
1581 +
1582 +int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1583 +{
1584 +       return acl_get_tag_type( entry_d, tag_type_p);
1585 +}
1586 +
1587 +int sys_acl_get_permset( SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1588 +{
1589 +       return acl_get_permset( entry_d, permset_p);
1590 +}
1591 +
1592 +void *sys_acl_get_qualifier( SMB_ACL_ENTRY_T entry_d)
1593 +{
1594 +       return acl_get_qualifier( entry_d);
1595 +}
1596 +
1597 +SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type)
1598 +{
1599 +       return acl_get_file((char *)path_p, type);
1600 +}
1601 +
1602 +SMB_ACL_T sys_acl_get_fd(int fd)
1603 +{
1604 +       return acl_get_fd(fd, ACL_TYPE_ACCESS);
1605 +}
1606 +
1607 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset)
1608 +{
1609 +       *permset = 0;           /* acl_clear_perm() is broken on Tru64  */
1610 +
1611 +       return 0;
1612 +}
1613 +
1614 +int sys_acl_add_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1615 +{
1616 +       if (perm & ~(SMB_ACL_READ | SMB_ACL_WRITE | SMB_ACL_EXECUTE)) {
1617 +               errno = EINVAL;
1618 +               return -1;
1619 +       }
1620 +
1621 +       *permset |= perm;       /* acl_add_perm() is broken on Tru64    */
1622 +
1623 +       return 0;
1624 +}
1625 +
1626 +int sys_acl_get_perm( SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1627 +{
1628 +       return *permset & perm; /* Tru64 doesn't have acl_get_perm() */
1629 +}
1630 +
1631 +char *sys_acl_to_text( SMB_ACL_T the_acl, ssize_t *plen)
1632 +{
1633 +       return acl_to_text( the_acl, plen);
1634 +}
1635 +
1636 +SMB_ACL_T sys_acl_init( int count)
1637 +{
1638 +       return acl_init(count);
1639 +}
1640 +
1641 +int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1642 +{
1643 +       SMB_ACL_ENTRY_T entry;
1644 +
1645 +       if ((entry = acl_create_entry(pacl)) == NULL) {
1646 +               return -1;
1647 +       }
1648 +
1649 +       *pentry = entry;
1650 +       return 0;
1651 +}
1652 +
1653 +int sys_acl_set_tag_type( SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1654 +{
1655 +       return acl_set_tag_type(entry, tagtype);
1656 +}
1657 +
1658 +int sys_acl_set_qualifier( SMB_ACL_ENTRY_T entry, void *qual)
1659 +{
1660 +       return acl_set_qualifier(entry, qual);
1661 +}
1662 +
1663 +int sys_acl_set_permset( SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1664 +{
1665 +       return acl_set_permset(entry, permset);
1666 +}
1667 +
1668 +int sys_acl_valid( SMB_ACL_T theacl )
1669 +{
1670 +       acl_entry_t     entry;
1671 +
1672 +       return acl_valid(theacl, &entry);
1673 +}
1674 +
1675 +int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1676 +{
1677 +       return acl_set_file((char *)name, acltype, theacl);
1678 +}
1679 +
1680 +int sys_acl_set_fd( int fd, SMB_ACL_T theacl)
1681 +{
1682 +       return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);
1683 +}
1684 +
1685 +int sys_acl_delete_def_file(const char *name)
1686 +{
1687 +       return acl_delete_def_file((char *)name);
1688 +}
1689 +
1690 +int sys_acl_free_text(char *text)
1691 +{
1692 +       /*
1693 +        * (void) cast and explicit return 0 are for DEC UNIX
1694 +        *  which just #defines acl_free_text() to be free()
1695 +        */
1696 +       (void) acl_free_text(text);
1697 +       return 0;
1698 +}
1699 +
1700 +int sys_acl_free_acl(SMB_ACL_T the_acl) 
1701 +{
1702 +       return acl_free(the_acl);
1703 +}
1704 +
1705 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
1706 +{
1707 +       return acl_free_qualifier(qual, tagtype);
1708 +}
1709 +
1710 +#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS)
1711 +
1712 +/*
1713 + * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX.
1714 + * Modified by Toomas Soome <tsoome@ut.ee> for Solaris.
1715 + */
1716 +
1717 +/*
1718 + * Note that while this code implements sufficient functionality
1719 + * to support the sys_acl_* interfaces it does not provide all
1720 + * of the semantics of the POSIX ACL interfaces.
1721 + *
1722 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
1723 + * from a call to sys_acl_get_entry() should not be assumed to be
1724 + * valid after calling any of the following functions, which may
1725 + * reorder the entries in the ACL.
1726 + *
1727 + *     sys_acl_valid()
1728 + *     sys_acl_set_file()
1729 + *     sys_acl_set_fd()
1730 + */
1731 +
1732 +/*
1733 + * The only difference between Solaris and UnixWare / OpenUNIX is
1734 + * that the #defines for the ACL operations have different names
1735 + */
1736 +#if defined(HAVE_UNIXWARE_ACLS)
1737 +
1738 +#define        SETACL          ACL_SET
1739 +#define        GETACL          ACL_GET
1740 +#define        GETACLCNT       ACL_CNT
1741 +
1742 +#endif
1743 +
1744 +
1745 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1746 +{
1747 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
1748 +               errno = EINVAL;
1749 +               return -1;
1750 +       }
1751 +
1752 +       if (entry_p == NULL) {
1753 +               errno = EINVAL;
1754 +               return -1;
1755 +       }
1756 +
1757 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
1758 +               acl_d->next = 0;
1759 +       }
1760 +
1761 +       if (acl_d->next < 0) {
1762 +               errno = EINVAL;
1763 +               return -1;
1764 +       }
1765 +
1766 +       if (acl_d->next >= acl_d->count) {
1767 +               return 0;
1768 +       }
1769 +
1770 +       *entry_p = &acl_d->acl[acl_d->next++];
1771 +
1772 +       return 1;
1773 +}
1774 +
1775 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
1776 +{
1777 +       *type_p = entry_d->a_type;
1778 +
1779 +       return 0;
1780 +}
1781 +
1782 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1783 +{
1784 +       *permset_p = &entry_d->a_perm;
1785 +
1786 +       return 0;
1787 +}
1788 +
1789 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
1790 +{
1791 +       if (entry_d->a_type != SMB_ACL_USER
1792 +           && entry_d->a_type != SMB_ACL_GROUP) {
1793 +               errno = EINVAL;
1794 +               return NULL;
1795 +       }
1796 +
1797 +       return &entry_d->a_id;
1798 +}
1799 +
1800 +/*
1801 + * There is no way of knowing what size the ACL returned by
1802 + * GETACL will be unless you first call GETACLCNT which means
1803 + * making an additional system call.
1804 + *
1805 + * In the hope of avoiding the cost of the additional system
1806 + * call in most cases, we initially allocate enough space for
1807 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
1808 + * be too small then we use GETACLCNT to find out the actual
1809 + * size, reallocate the ACL buffer, and then call GETACL again.
1810 + */
1811 +
1812 +#define        INITIAL_ACL_SIZE        16
1813 +
1814 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
1815 +{
1816 +       SMB_ACL_T       acl_d;
1817 +       int             count;          /* # of ACL entries allocated   */
1818 +       int             naccess;        /* # of access ACL entries      */
1819 +       int             ndefault;       /* # of default ACL entries     */
1820 +
1821 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
1822 +               errno = EINVAL;
1823 +               return NULL;
1824 +       }
1825 +
1826 +       count = INITIAL_ACL_SIZE;
1827 +       if ((acl_d = sys_acl_init(count)) == NULL) {
1828 +               return NULL;
1829 +       }
1830 +
1831 +       /*
1832 +        * If there isn't enough space for the ACL entries we use
1833 +        * GETACLCNT to determine the actual number of ACL entries
1834 +        * reallocate and try again. This is in a loop because it
1835 +        * is possible that someone else could modify the ACL and
1836 +        * increase the number of entries between the call to
1837 +        * GETACLCNT and the call to GETACL.
1838 +        */
1839 +       while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0
1840 +           && errno == ENOSPC) {
1841 +
1842 +               sys_acl_free_acl(acl_d);
1843 +
1844 +               if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {
1845 +                       return NULL;
1846 +               }
1847 +
1848 +               if ((acl_d = sys_acl_init(count)) == NULL) {
1849 +                       return NULL;
1850 +               }
1851 +       }
1852 +
1853 +       if (count < 0) {
1854 +               sys_acl_free_acl(acl_d);
1855 +               return NULL;
1856 +       }
1857 +
1858 +       /*
1859 +        * calculate the number of access and default ACL entries
1860 +        *
1861 +        * Note: we assume that the acl() system call returned a
1862 +        * well formed ACL which is sorted so that all of the
1863 +        * access ACL entries preceed any default ACL entries
1864 +        */
1865 +       for (naccess = 0; naccess < count; naccess++) {
1866 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
1867 +                       break;
1868 +       }
1869 +       ndefault = count - naccess;
1870 +       
1871 +       /*
1872 +        * if the caller wants the default ACL we have to copy
1873 +        * the entries down to the start of the acl[] buffer
1874 +        * and mask out the ACL_DEFAULT flag from the type field
1875 +        */
1876 +       if (type == SMB_ACL_TYPE_DEFAULT) {
1877 +               int     i, j;
1878 +
1879 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
1880 +                       acl_d->acl[i] = acl_d->acl[j];
1881 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
1882 +               }
1883 +
1884 +               acl_d->count = ndefault;
1885 +       } else {
1886 +               acl_d->count = naccess;
1887 +       }
1888 +
1889 +       return acl_d;
1890 +}
1891 +
1892 +SMB_ACL_T sys_acl_get_fd(int fd)
1893 +{
1894 +       SMB_ACL_T       acl_d;
1895 +       int             count;          /* # of ACL entries allocated   */
1896 +       int             naccess;        /* # of access ACL entries      */
1897 +
1898 +       count = INITIAL_ACL_SIZE;
1899 +       if ((acl_d = sys_acl_init(count)) == NULL) {
1900 +               return NULL;
1901 +       }
1902 +
1903 +       while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0
1904 +           && errno == ENOSPC) {
1905 +
1906 +               sys_acl_free_acl(acl_d);
1907 +
1908 +               if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {
1909 +                       return NULL;
1910 +               }
1911 +
1912 +               if ((acl_d = sys_acl_init(count)) == NULL) {
1913 +                       return NULL;
1914 +               }
1915 +       }
1916 +
1917 +       if (count < 0) {
1918 +               sys_acl_free_acl(acl_d);
1919 +               return NULL;
1920 +       }
1921 +
1922 +       /*
1923 +        * calculate the number of access ACL entries
1924 +        */
1925 +       for (naccess = 0; naccess < count; naccess++) {
1926 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
1927 +                       break;
1928 +       }
1929 +       
1930 +       acl_d->count = naccess;
1931 +
1932 +       return acl_d;
1933 +}
1934 +
1935 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
1936 +{
1937 +       *permset_d = 0;
1938 +
1939 +       return 0;
1940 +}
1941 +
1942 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
1943 +{
1944 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
1945 +           && perm != SMB_ACL_EXECUTE) {
1946 +               errno = EINVAL;
1947 +               return -1;
1948 +       }
1949 +
1950 +       if (permset_d == NULL) {
1951 +               errno = EINVAL;
1952 +               return -1;
1953 +       }
1954 +
1955 +       *permset_d |= perm;
1956 +
1957 +       return 0;
1958 +}
1959 +
1960 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
1961 +{
1962 +       return *permset_d & perm;
1963 +}
1964 +
1965 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
1966 +{
1967 +       int     i;
1968 +       int     len, maxlen;
1969 +       char    *text;
1970 +
1971 +       /*
1972 +        * use an initial estimate of 20 bytes per ACL entry
1973 +        * when allocating memory for the text representation
1974 +        * of the ACL
1975 +        */
1976 +       len     = 0;
1977 +       maxlen  = 20 * acl_d->count;
1978 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
1979 +               errno = ENOMEM;
1980 +               return NULL;
1981 +       }
1982 +
1983 +       for (i = 0; i < acl_d->count; i++) {
1984 +               struct acl      *ap     = &acl_d->acl[i];
1985 +               struct passwd   *pw;
1986 +               struct group    *gr;
1987 +               char            tagbuf[12];
1988 +               char            idbuf[12];
1989 +               char            *tag;
1990 +               char            *id     = "";
1991 +               char            perms[4];
1992 +               int             nbytes;
1993 +
1994 +               switch (ap->a_type) {
1995 +                       /*
1996 +                        * for debugging purposes it's probably more
1997 +                        * useful to dump unknown tag types rather
1998 +                        * than just returning an error
1999 +                        */
2000 +                       default:
2001 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2002 +                                       ap->a_type);
2003 +                               tag = tagbuf;
2004 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2005 +                                       (long)ap->a_id);
2006 +                               id = idbuf;
2007 +                               break;
2008 +
2009 +                       case SMB_ACL_USER:
2010 +                               id = uidtoname(ap->a_id);
2011 +                       case SMB_ACL_USER_OBJ:
2012 +                               tag = "user";
2013 +                               break;
2014 +
2015 +                       case SMB_ACL_GROUP:
2016 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
2017 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2018 +                                               (long)ap->a_id);
2019 +                                       id = idbuf;
2020 +                               } else {
2021 +                                       id = gr->gr_name;
2022 +                               }
2023 +                       case SMB_ACL_GROUP_OBJ:
2024 +                               tag = "group";
2025 +                               break;
2026 +
2027 +                       case SMB_ACL_OTHER:
2028 +                               tag = "other";
2029 +                               break;
2030 +
2031 +                       case SMB_ACL_MASK:
2032 +                               tag = "mask";
2033 +                               break;
2034 +
2035 +               }
2036 +
2037 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2038 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2039 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2040 +               perms[3] = '\0';
2041 +
2042 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
2043 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2044 +
2045 +               /*
2046 +                * If this entry would overflow the buffer
2047 +                * allocate enough additional memory for this
2048 +                * entry and an estimate of another 20 bytes
2049 +                * for each entry still to be processed
2050 +                */
2051 +               if ((len + nbytes) > maxlen) {
2052 +                       char *oldtext = text;
2053 +
2054 +                       maxlen += nbytes + 20 * (acl_d->count - i);
2055 +
2056 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2057 +                               SAFE_FREE(oldtext);
2058 +                               errno = ENOMEM;
2059 +                               return NULL;
2060 +                       }
2061 +               }
2062 +
2063 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2064 +               len += nbytes - 1;
2065 +       }
2066 +
2067 +       if (len_p)
2068 +               *len_p = len;
2069 +
2070 +       return text;
2071 +}
2072 +
2073 +SMB_ACL_T sys_acl_init(int count)
2074 +{
2075 +       SMB_ACL_T       a;
2076 +
2077 +       if (count < 0) {
2078 +               errno = EINVAL;
2079 +               return NULL;
2080 +       }
2081 +
2082 +       /*
2083 +        * note that since the definition of the structure pointed
2084 +        * to by the SMB_ACL_T includes the first element of the
2085 +        * acl[] array, this actually allocates an ACL with room
2086 +        * for (count+1) entries
2087 +        */
2088 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2089 +               errno = ENOMEM;
2090 +               return NULL;
2091 +       }
2092 +
2093 +       a->size = count + 1;
2094 +       a->count = 0;
2095 +       a->next = -1;
2096 +
2097 +       return a;
2098 +}
2099 +
2100 +
2101 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2102 +{
2103 +       SMB_ACL_T       acl_d;
2104 +       SMB_ACL_ENTRY_T entry_d;
2105 +
2106 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2107 +               errno = EINVAL;
2108 +               return -1;
2109 +       }
2110 +
2111 +       if (acl_d->count >= acl_d->size) {
2112 +               errno = ENOSPC;
2113 +               return -1;
2114 +       }
2115 +
2116 +       entry_d         = &acl_d->acl[acl_d->count++];
2117 +       entry_d->a_type = 0;
2118 +       entry_d->a_id   = -1;
2119 +       entry_d->a_perm = 0;
2120 +       *entry_p        = entry_d;
2121 +
2122 +       return 0;
2123 +}
2124 +
2125 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2126 +{
2127 +       switch (tag_type) {
2128 +               case SMB_ACL_USER:
2129 +               case SMB_ACL_USER_OBJ:
2130 +               case SMB_ACL_GROUP:
2131 +               case SMB_ACL_GROUP_OBJ:
2132 +               case SMB_ACL_OTHER:
2133 +               case SMB_ACL_MASK:
2134 +                       entry_d->a_type = tag_type;
2135 +                       break;
2136 +               default:
2137 +                       errno = EINVAL;
2138 +                       return -1;
2139 +       }
2140 +
2141 +       return 0;
2142 +}
2143 +
2144 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2145 +{
2146 +       if (entry_d->a_type != SMB_ACL_GROUP
2147 +           && entry_d->a_type != SMB_ACL_USER) {
2148 +               errno = EINVAL;
2149 +               return -1;
2150 +       }
2151 +
2152 +       entry_d->a_id = *((id_t *)qual_p);
2153 +
2154 +       return 0;
2155 +}
2156 +
2157 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2158 +{
2159 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2160 +               return EINVAL;
2161 +       }
2162 +
2163 +       entry_d->a_perm = *permset_d;
2164 +
2165 +       return 0;
2166 +}
2167 +
2168 +/*
2169 + * sort the ACL and check it for validity
2170 + *
2171 + * if it's a minimal ACL with only 4 entries then we
2172 + * need to recalculate the mask permissions to make
2173 + * sure that they are the same as the GROUP_OBJ
2174 + * permissions as required by the UnixWare acl() system call.
2175 + *
2176 + * (note: since POSIX allows minimal ACLs which only contain
2177 + * 3 entries - ie there is no mask entry - we should, in theory,
2178 + * check for this and add a mask entry if necessary - however
2179 + * we "know" that the caller of this interface always specifies
2180 + * a mask so, in practice "this never happens" (tm) - if it *does*
2181 + * happen aclsort() will fail and return an error and someone will
2182 + * have to fix it ...)
2183 + */
2184 +
2185 +static int acl_sort(SMB_ACL_T acl_d)
2186 +{
2187 +       int     fixmask = (acl_d->count <= 4);
2188 +
2189 +       if (aclsort(acl_d->count, fixmask, acl_d->acl) != 0) {
2190 +               errno = EINVAL;
2191 +               return -1;
2192 +       }
2193 +       return 0;
2194 +}
2195
2196 +int sys_acl_valid(SMB_ACL_T acl_d)
2197 +{
2198 +       return acl_sort(acl_d);
2199 +}
2200 +
2201 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
2202 +{
2203 +       struct stat     s;
2204 +       struct acl      *acl_p;
2205 +       int             acl_count;
2206 +       struct acl      *acl_buf        = NULL;
2207 +       int             ret;
2208 +
2209 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2210 +               errno = EINVAL;
2211 +               return -1;
2212 +       }
2213 +
2214 +       if (acl_sort(acl_d) != 0) {
2215 +               return -1;
2216 +       }
2217 +
2218 +       acl_p           = &acl_d->acl[0];
2219 +       acl_count       = acl_d->count;
2220 +
2221 +       /*
2222 +        * if it's a directory there is extra work to do
2223 +        * since the acl() system call will replace both
2224 +        * the access ACLs and the default ACLs (if any)
2225 +        */
2226 +       if (stat(name, &s) != 0) {
2227 +               return -1;
2228 +       }
2229 +       if (S_ISDIR(s.st_mode)) {
2230 +               SMB_ACL_T       acc_acl;
2231 +               SMB_ACL_T       def_acl;
2232 +               SMB_ACL_T       tmp_acl;
2233 +               int             i;
2234 +
2235 +               if (type == SMB_ACL_TYPE_ACCESS) {
2236 +                       acc_acl = acl_d;
2237 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
2238 +
2239 +               } else {
2240 +                       def_acl = acl_d;
2241 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
2242 +               }
2243 +
2244 +               if (tmp_acl == NULL) {
2245 +                       return -1;
2246 +               }
2247 +
2248 +               /*
2249 +                * allocate a temporary buffer for the complete ACL
2250 +                */
2251 +               acl_count = acc_acl->count + def_acl->count;
2252 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
2253 +
2254 +               if (acl_buf == NULL) {
2255 +                       sys_acl_free_acl(tmp_acl);
2256 +                       errno = ENOMEM;
2257 +                       return -1;
2258 +               }
2259 +
2260 +               /*
2261 +                * copy the access control and default entries into the buffer
2262 +                */
2263 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
2264 +                       acc_acl->count * sizeof(acl_buf[0]));
2265 +
2266 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
2267 +                       def_acl->count * sizeof(acl_buf[0]));
2268 +
2269 +               /*
2270 +                * set the ACL_DEFAULT flag on the default entries
2271 +                */
2272 +               for (i = acc_acl->count; i < acl_count; i++) {
2273 +                       acl_buf[i].a_type |= ACL_DEFAULT;
2274 +               }
2275 +
2276 +               sys_acl_free_acl(tmp_acl);
2277 +
2278 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
2279 +               errno = EINVAL;
2280 +               return -1;
2281 +       }
2282 +
2283 +       ret = acl(name, SETACL, acl_count, acl_p);
2284 +
2285 +       SAFE_FREE(acl_buf);
2286 +
2287 +       return ret;
2288 +}
2289 +
2290 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
2291 +{
2292 +       if (acl_sort(acl_d) != 0) {
2293 +               return -1;
2294 +       }
2295 +
2296 +       return facl(fd, SETACL, acl_d->count, &acl_d->acl[0]);
2297 +}
2298 +
2299 +int sys_acl_delete_def_file(const char *path)
2300 +{
2301 +       SMB_ACL_T       acl_d;
2302 +       int             ret;
2303 +
2304 +       /*
2305 +        * fetching the access ACL and rewriting it has
2306 +        * the effect of deleting the default ACL
2307 +        */
2308 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
2309 +               return -1;
2310 +       }
2311 +
2312 +       ret = acl(path, SETACL, acl_d->count, acl_d->acl);
2313 +
2314 +       sys_acl_free_acl(acl_d);
2315 +       
2316 +       return ret;
2317 +}
2318 +
2319 +int sys_acl_free_text(char *text)
2320 +{
2321 +       SAFE_FREE(text);
2322 +       return 0;
2323 +}
2324 +
2325 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
2326 +{
2327 +       SAFE_FREE(acl_d);
2328 +       return 0;
2329 +}
2330 +
2331 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
2332 +{
2333 +       return 0;
2334 +}
2335 +
2336 +#elif defined(HAVE_HPUX_ACLS)
2337 +#include <dl.h>
2338 +
2339 +/*
2340 + * Based on the Solaris/SCO code - with modifications.
2341 + */
2342 +
2343 +/*
2344 + * Note that while this code implements sufficient functionality
2345 + * to support the sys_acl_* interfaces it does not provide all
2346 + * of the semantics of the POSIX ACL interfaces.
2347 + *
2348 + * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
2349 + * from a call to sys_acl_get_entry() should not be assumed to be
2350 + * valid after calling any of the following functions, which may
2351 + * reorder the entries in the ACL.
2352 + *
2353 + *     sys_acl_valid()
2354 + *     sys_acl_set_file()
2355 + *     sys_acl_set_fd()
2356 + */
2357 +
2358 +/* This checks if the POSIX ACL system call is defined */
2359 +/* which basically corresponds to whether JFS 3.3 or   */
2360 +/* higher is installed. If acl() was called when it    */
2361 +/* isn't defined, it causes the process to core dump   */
2362 +/* so it is important to check this and avoid acl()    */
2363 +/* calls if it isn't there.                            */
2364 +
2365 +static BOOL hpux_acl_call_presence(void)
2366 +{
2367 +
2368 +       shl_t handle = NULL;
2369 +       void *value;
2370 +       int ret_val=0;
2371 +       static BOOL already_checked=0;
2372 +
2373 +       if(already_checked)
2374 +               return True;
2375 +
2376 +
2377 +       ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
2378 +
2379 +       if(ret_val != 0) {
2380 +               DEBUG(5, ("hpux_acl_call_presence: shl_findsym() returned %d, errno = %d, error %s\n",
2381 +                       ret_val, errno, strerror(errno)));
2382 +               DEBUG(5,("hpux_acl_call_presence: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
2383 +               return False;
2384 +       }
2385 +
2386 +       DEBUG(10,("hpux_acl_call_presence: acl() system call is present. We have JFS 3.3 or above \n"));
2387 +
2388 +       already_checked = True;
2389 +       return True;
2390 +}
2391 +
2392 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
2393 +{
2394 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
2395 +               errno = EINVAL;
2396 +               return -1;
2397 +       }
2398 +
2399 +       if (entry_p == NULL) {
2400 +               errno = EINVAL;
2401 +               return -1;
2402 +       }
2403 +
2404 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
2405 +               acl_d->next = 0;
2406 +       }
2407 +
2408 +       if (acl_d->next < 0) {
2409 +               errno = EINVAL;
2410 +               return -1;
2411 +       }
2412 +
2413 +       if (acl_d->next >= acl_d->count) {
2414 +               return 0;
2415 +       }
2416 +
2417 +       *entry_p = &acl_d->acl[acl_d->next++];
2418 +
2419 +       return 1;
2420 +}
2421 +
2422 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
2423 +{
2424 +       *type_p = entry_d->a_type;
2425 +
2426 +       return 0;
2427 +}
2428 +
2429 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
2430 +{
2431 +       *permset_p = &entry_d->a_perm;
2432 +
2433 +       return 0;
2434 +}
2435 +
2436 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
2437 +{
2438 +       if (entry_d->a_type != SMB_ACL_USER
2439 +           && entry_d->a_type != SMB_ACL_GROUP) {
2440 +               errno = EINVAL;
2441 +               return NULL;
2442 +       }
2443 +
2444 +       return &entry_d->a_id;
2445 +}
2446 +
2447 +/*
2448 + * There is no way of knowing what size the ACL returned by
2449 + * ACL_GET will be unless you first call ACL_CNT which means
2450 + * making an additional system call.
2451 + *
2452 + * In the hope of avoiding the cost of the additional system
2453 + * call in most cases, we initially allocate enough space for
2454 + * an ACL with INITIAL_ACL_SIZE entries. If this turns out to
2455 + * be too small then we use ACL_CNT to find out the actual
2456 + * size, reallocate the ACL buffer, and then call ACL_GET again.
2457 + */
2458 +
2459 +#define        INITIAL_ACL_SIZE        16
2460 +
2461 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
2462 +{
2463 +       SMB_ACL_T       acl_d;
2464 +       int             count;          /* # of ACL entries allocated   */
2465 +       int             naccess;        /* # of access ACL entries      */
2466 +       int             ndefault;       /* # of default ACL entries     */
2467 +
2468 +       if(hpux_acl_call_presence() == False) {
2469 +               /* Looks like we don't have the acl() system call on HPUX. 
2470 +                * May be the system doesn't have the latest version of JFS.
2471 +                */
2472 +               return NULL; 
2473 +       }
2474 +
2475 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
2476 +               errno = EINVAL;
2477 +               return NULL;
2478 +       }
2479 +
2480 +       count = INITIAL_ACL_SIZE;
2481 +       if ((acl_d = sys_acl_init(count)) == NULL) {
2482 +               return NULL;
2483 +       }
2484 +
2485 +       /*
2486 +        * If there isn't enough space for the ACL entries we use
2487 +        * ACL_CNT to determine the actual number of ACL entries
2488 +        * reallocate and try again. This is in a loop because it
2489 +        * is possible that someone else could modify the ACL and
2490 +        * increase the number of entries between the call to
2491 +        * ACL_CNT and the call to ACL_GET.
2492 +        */
2493 +       while ((count = acl(path_p, ACL_GET, count, &acl_d->acl[0])) < 0 && errno == ENOSPC) {
2494 +
2495 +               sys_acl_free_acl(acl_d);
2496 +
2497 +               if ((count = acl(path_p, ACL_CNT, 0, NULL)) < 0) {
2498 +                       return NULL;
2499 +               }
2500 +
2501 +               if ((acl_d = sys_acl_init(count)) == NULL) {
2502 +                       return NULL;
2503 +               }
2504 +       }
2505 +
2506 +       if (count < 0) {
2507 +               sys_acl_free_acl(acl_d);
2508 +               return NULL;
2509 +       }
2510 +
2511 +       /*
2512 +        * calculate the number of access and default ACL entries
2513 +        *
2514 +        * Note: we assume that the acl() system call returned a
2515 +        * well formed ACL which is sorted so that all of the
2516 +        * access ACL entries preceed any default ACL entries
2517 +        */
2518 +       for (naccess = 0; naccess < count; naccess++) {
2519 +               if (acl_d->acl[naccess].a_type & ACL_DEFAULT)
2520 +                       break;
2521 +       }
2522 +       ndefault = count - naccess;
2523 +       
2524 +       /*
2525 +        * if the caller wants the default ACL we have to copy
2526 +        * the entries down to the start of the acl[] buffer
2527 +        * and mask out the ACL_DEFAULT flag from the type field
2528 +        */
2529 +       if (type == SMB_ACL_TYPE_DEFAULT) {
2530 +               int     i, j;
2531 +
2532 +               for (i = 0, j = naccess; i < ndefault; i++, j++) {
2533 +                       acl_d->acl[i] = acl_d->acl[j];
2534 +                       acl_d->acl[i].a_type &= ~ACL_DEFAULT;
2535 +               }
2536 +
2537 +               acl_d->count = ndefault;
2538 +       } else {
2539 +               acl_d->count = naccess;
2540 +       }
2541 +
2542 +       return acl_d;
2543 +}
2544 +
2545 +SMB_ACL_T sys_acl_get_fd(int fd)
2546 +{
2547 +       /*
2548 +        * HPUX doesn't have the facl call. Fake it using the path.... JRA.
2549 +        */
2550 +
2551 +       files_struct *fsp = file_find_fd(fd);
2552 +
2553 +       if (fsp == NULL) {
2554 +               errno = EBADF;
2555 +               return NULL;
2556 +       }
2557 +
2558 +       /*
2559 +        * We know we're in the same conn context. So we
2560 +        * can use the relative path.
2561 +        */
2562 +
2563 +       return sys_acl_get_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS);
2564 +}
2565 +
2566 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
2567 +{
2568 +       *permset_d = 0;
2569 +
2570 +       return 0;
2571 +}
2572 +
2573 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2574 +{
2575 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
2576 +           && perm != SMB_ACL_EXECUTE) {
2577 +               errno = EINVAL;
2578 +               return -1;
2579 +       }
2580 +
2581 +       if (permset_d == NULL) {
2582 +               errno = EINVAL;
2583 +               return -1;
2584 +       }
2585 +
2586 +       *permset_d |= perm;
2587 +
2588 +       return 0;
2589 +}
2590 +
2591 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
2592 +{
2593 +       return *permset_d & perm;
2594 +}
2595 +
2596 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
2597 +{
2598 +       int     i;
2599 +       int     len, maxlen;
2600 +       char    *text;
2601 +
2602 +       /*
2603 +        * use an initial estimate of 20 bytes per ACL entry
2604 +        * when allocating memory for the text representation
2605 +        * of the ACL
2606 +        */
2607 +       len     = 0;
2608 +       maxlen  = 20 * acl_d->count;
2609 +       if ((text = SMB_MALLOC(maxlen)) == NULL) {
2610 +               errno = ENOMEM;
2611 +               return NULL;
2612 +       }
2613 +
2614 +       for (i = 0; i < acl_d->count; i++) {
2615 +               struct acl      *ap     = &acl_d->acl[i];
2616 +               struct passwd   *pw;
2617 +               struct group    *gr;
2618 +               char            tagbuf[12];
2619 +               char            idbuf[12];
2620 +               char            *tag;
2621 +               char            *id     = "";
2622 +               char            perms[4];
2623 +               int             nbytes;
2624 +
2625 +               switch (ap->a_type) {
2626 +                       /*
2627 +                        * for debugging purposes it's probably more
2628 +                        * useful to dump unknown tag types rather
2629 +                        * than just returning an error
2630 +                        */
2631 +                       default:
2632 +                               slprintf(tagbuf, sizeof(tagbuf)-1, "0x%x",
2633 +                                       ap->a_type);
2634 +                               tag = tagbuf;
2635 +                               slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2636 +                                       (long)ap->a_id);
2637 +                               id = idbuf;
2638 +                               break;
2639 +
2640 +                       case SMB_ACL_USER:
2641 +                               id = uidtoname(ap->a_id);
2642 +                       case SMB_ACL_USER_OBJ:
2643 +                               tag = "user";
2644 +                               break;
2645 +
2646 +                       case SMB_ACL_GROUP:
2647 +                               if ((gr = getgrgid(ap->a_id)) == NULL) {
2648 +                                       slprintf(idbuf, sizeof(idbuf)-1, "%ld",
2649 +                                               (long)ap->a_id);
2650 +                                       id = idbuf;
2651 +                               } else {
2652 +                                       id = gr->gr_name;
2653 +                               }
2654 +                       case SMB_ACL_GROUP_OBJ:
2655 +                               tag = "group";
2656 +                               break;
2657 +
2658 +                       case SMB_ACL_OTHER:
2659 +                               tag = "other";
2660 +                               break;
2661 +
2662 +                       case SMB_ACL_MASK:
2663 +                               tag = "mask";
2664 +                               break;
2665 +
2666 +               }
2667 +
2668 +               perms[0] = (ap->a_perm & SMB_ACL_READ) ? 'r' : '-';
2669 +               perms[1] = (ap->a_perm & SMB_ACL_WRITE) ? 'w' : '-';
2670 +               perms[2] = (ap->a_perm & SMB_ACL_EXECUTE) ? 'x' : '-';
2671 +               perms[3] = '\0';
2672 +
2673 +               /*          <tag>      :  <qualifier>   :  rwx \n  \0 */
2674 +               nbytes = strlen(tag) + 1 + strlen(id) + 1 + 3 + 1 + 1;
2675 +
2676 +               /*
2677 +                * If this entry would overflow the buffer
2678 +                * allocate enough additional memory for this
2679 +                * entry and an estimate of another 20 bytes
2680 +                * for each entry still to be processed
2681 +                */
2682 +               if ((len + nbytes) > maxlen) {
2683 +                       char *oldtext = text;
2684 +
2685 +                       maxlen += nbytes + 20 * (acl_d->count - i);
2686 +
2687 +                       if ((text = SMB_REALLOC(oldtext, maxlen)) == NULL) {
2688 +                               free(oldtext);
2689 +                               errno = ENOMEM;
2690 +                               return NULL;
2691 +                       }
2692 +               }
2693 +
2694 +               slprintf(&text[len], nbytes-1, "%s:%s:%s\n", tag, id, perms);
2695 +               len += nbytes - 1;
2696 +       }
2697 +
2698 +       if (len_p)
2699 +               *len_p = len;
2700 +
2701 +       return text;
2702 +}
2703 +
2704 +SMB_ACL_T sys_acl_init(int count)
2705 +{
2706 +       SMB_ACL_T       a;
2707 +
2708 +       if (count < 0) {
2709 +               errno = EINVAL;
2710 +               return NULL;
2711 +       }
2712 +
2713 +       /*
2714 +        * note that since the definition of the structure pointed
2715 +        * to by the SMB_ACL_T includes the first element of the
2716 +        * acl[] array, this actually allocates an ACL with room
2717 +        * for (count+1) entries
2718 +        */
2719 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + count * sizeof(struct acl))) == NULL) {
2720 +               errno = ENOMEM;
2721 +               return NULL;
2722 +       }
2723 +
2724 +       a->size = count + 1;
2725 +       a->count = 0;
2726 +       a->next = -1;
2727 +
2728 +       return a;
2729 +}
2730 +
2731 +
2732 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
2733 +{
2734 +       SMB_ACL_T       acl_d;
2735 +       SMB_ACL_ENTRY_T entry_d;
2736 +
2737 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
2738 +               errno = EINVAL;
2739 +               return -1;
2740 +       }
2741 +
2742 +       if (acl_d->count >= acl_d->size) {
2743 +               errno = ENOSPC;
2744 +               return -1;
2745 +       }
2746 +
2747 +       entry_d         = &acl_d->acl[acl_d->count++];
2748 +       entry_d->a_type = 0;
2749 +       entry_d->a_id   = -1;
2750 +       entry_d->a_perm = 0;
2751 +       *entry_p        = entry_d;
2752 +
2753 +       return 0;
2754 +}
2755 +
2756 +int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T tag_type)
2757 +{
2758 +       switch (tag_type) {
2759 +               case SMB_ACL_USER:
2760 +               case SMB_ACL_USER_OBJ:
2761 +               case SMB_ACL_GROUP:
2762 +               case SMB_ACL_GROUP_OBJ:
2763 +               case SMB_ACL_OTHER:
2764 +               case SMB_ACL_MASK:
2765 +                       entry_d->a_type = tag_type;
2766 +                       break;
2767 +               default:
2768 +                       errno = EINVAL;
2769 +                       return -1;
2770 +       }
2771 +
2772 +       return 0;
2773 +}
2774 +
2775 +int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d, void *qual_p)
2776 +{
2777 +       if (entry_d->a_type != SMB_ACL_GROUP
2778 +           && entry_d->a_type != SMB_ACL_USER) {
2779 +               errno = EINVAL;
2780 +               return -1;
2781 +       }
2782 +
2783 +       entry_d->a_id = *((id_t *)qual_p);
2784 +
2785 +       return 0;
2786 +}
2787 +
2788 +int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T permset_d)
2789 +{
2790 +       if (*permset_d & ~(SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE)) {
2791 +               return EINVAL;
2792 +       }
2793 +
2794 +       entry_d->a_perm = *permset_d;
2795 +
2796 +       return 0;
2797 +}
2798 +
2799 +/* Structure to capture the count for each type of ACE. */
2800 +
2801 +struct hpux_acl_types {
2802 +       int n_user;
2803 +       int n_def_user;
2804 +       int n_user_obj;
2805 +       int n_def_user_obj;
2806 +
2807 +       int n_group;
2808 +       int n_def_group;
2809 +       int n_group_obj;
2810 +       int n_def_group_obj;
2811 +
2812 +       int n_other;
2813 +       int n_other_obj;
2814 +       int n_def_other_obj;
2815 +
2816 +       int n_class_obj;
2817 +       int n_def_class_obj;
2818 +
2819 +       int n_illegal_obj;
2820 +};
2821 +
2822 +/* count_obj:
2823 + * Counts the different number of objects in a given array of ACL
2824 + * structures.
2825 + * Inputs:
2826 + *
2827 + * acl_count      - Count of ACLs in the array of ACL strucutres.
2828 + * aclp           - Array of ACL structures.
2829 + * acl_type_count - Pointer to acl_types structure. Should already be
2830 + *                  allocated.
2831 + * Output: 
2832 + *
2833 + * acl_type_count - This structure is filled up with counts of various 
2834 + *                  acl types.
2835 + */
2836 +
2837 +static int hpux_count_obj(int acl_count, struct acl *aclp, struct hpux_acl_types *acl_type_count)
2838 +{
2839 +       int i;
2840 +
2841 +       memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
2842 +
2843 +       for(i=0;i<acl_count;i++) {
2844 +               switch(aclp[i].a_type) {
2845 +               case USER: 
2846 +                       acl_type_count->n_user++;
2847 +                       break;
2848 +               case USER_OBJ: 
2849 +                       acl_type_count->n_user_obj++;
2850 +                       break;
2851 +               case DEF_USER_OBJ: 
2852 +                       acl_type_count->n_def_user_obj++;
2853 +                       break;
2854 +               case GROUP: 
2855 +                       acl_type_count->n_group++;
2856 +                       break;
2857 +               case GROUP_OBJ: 
2858 +                       acl_type_count->n_group_obj++;
2859 +                       break;
2860 +               case DEF_GROUP_OBJ: 
2861 +                       acl_type_count->n_def_group_obj++;
2862 +                       break;
2863 +               case OTHER_OBJ: 
2864 +                       acl_type_count->n_other_obj++;
2865 +                       break;
2866 +               case DEF_OTHER_OBJ: 
2867 +                       acl_type_count->n_def_other_obj++;
2868 +                       break;
2869 +               case CLASS_OBJ:
2870 +                       acl_type_count->n_class_obj++;
2871 +                       break;
2872 +               case DEF_CLASS_OBJ:
2873 +                       acl_type_count->n_def_class_obj++;
2874 +                       break;
2875 +               case DEF_USER:
2876 +                       acl_type_count->n_def_user++;
2877 +                       break;
2878 +               case DEF_GROUP:
2879 +                       acl_type_count->n_def_group++;
2880 +                       break;
2881 +               default: 
2882 +                       acl_type_count->n_illegal_obj++;
2883 +                       break;
2884 +               }
2885 +       }
2886 +}
2887 +
2888 +/* swap_acl_entries:  Swaps two ACL entries. 
2889 + *
2890 + * Inputs: aclp0, aclp1 - ACL entries to be swapped.
2891 + */
2892 +
2893 +static void hpux_swap_acl_entries(struct acl *aclp0, struct acl *aclp1)
2894 +{
2895 +       struct acl temp_acl;
2896 +
2897 +       temp_acl.a_type = aclp0->a_type;
2898 +       temp_acl.a_id = aclp0->a_id;
2899 +       temp_acl.a_perm = aclp0->a_perm;
2900 +
2901 +       aclp0->a_type = aclp1->a_type;
2902 +       aclp0->a_id = aclp1->a_id;
2903 +       aclp0->a_perm = aclp1->a_perm;
2904 +
2905 +       aclp1->a_type = temp_acl.a_type;
2906 +       aclp1->a_id = temp_acl.a_id;
2907 +       aclp1->a_perm = temp_acl.a_perm;
2908 +}
2909 +
2910 +/* prohibited_duplicate_type
2911 + * Identifies if given ACL type can have duplicate entries or 
2912 + * not.
2913 + *
2914 + * Inputs: acl_type - ACL Type.
2915 + *
2916 + * Outputs: 
2917 + *
2918 + * Return.. 
2919 + *
2920 + * True - If the ACL type matches any of the prohibited types.
2921 + * False - If the ACL type doesn't match any of the prohibited types.
2922 + */ 
2923 +
2924 +static BOOL hpux_prohibited_duplicate_type(int acl_type)
2925 +{
2926 +       switch(acl_type) {
2927 +               case USER:
2928 +               case GROUP:
2929 +               case DEF_USER: 
2930 +               case DEF_GROUP:
2931 +                       return True;
2932 +               default:
2933 +                       return False;
2934 +       }
2935 +}
2936 +
2937 +/* get_needed_class_perm
2938 + * Returns the permissions of a ACL structure only if the ACL
2939 + * type matches one of the pre-determined types for computing 
2940 + * CLASS_OBJ permissions.
2941 + *
2942 + * Inputs: aclp - Pointer to ACL structure.
2943 + */
2944 +
2945 +static int hpux_get_needed_class_perm(struct acl *aclp)
2946 +{
2947 +       switch(aclp->a_type) {
2948 +               case USER: 
2949 +               case GROUP_OBJ: 
2950 +               case GROUP: 
2951 +               case DEF_USER_OBJ: 
2952 +               case DEF_USER:
2953 +               case DEF_GROUP_OBJ: 
2954 +               case DEF_GROUP:
2955 +               case DEF_CLASS_OBJ:
2956 +               case DEF_OTHER_OBJ: 
2957 +                       return aclp->a_perm;
2958 +               default: 
2959 +                       return 0;
2960 +       }
2961 +}
2962 +
2963 +/* acl_sort for HPUX.
2964 + * Sorts the array of ACL structures as per the description in
2965 + * aclsort man page. Refer to aclsort man page for more details
2966 + *
2967 + * Inputs:
2968 + *
2969 + * acl_count - Count of ACLs in the array of ACL structures.
2970 + * calclass  - If this is not zero, then we compute the CLASS_OBJ
2971 + *             permissions.
2972 + * aclp      - Array of ACL structures.
2973 + *
2974 + * Outputs:
2975 + *
2976 + * aclp     - Sorted array of ACL structures.
2977 + *
2978 + * Outputs:
2979 + *
2980 + * Returns 0 for success -1 for failure. Prints a message to the Samba
2981 + * debug log in case of failure.
2982 + */
2983 +
2984 +static int hpux_acl_sort(int acl_count, int calclass, struct acl *aclp)
2985 +{
2986 +#if !defined(HAVE_HPUX_ACLSORT)
2987 +       /*
2988 +        * The aclsort() system call is availabe on the latest HPUX General
2989 +        * Patch Bundles. So for HPUX, we developed our version of acl_sort 
2990 +        * function. Because, we don't want to update to a new 
2991 +        * HPUX GR bundle just for aclsort() call.
2992 +        */
2993 +
2994 +       struct hpux_acl_types acl_obj_count;
2995 +       int n_class_obj_perm = 0;
2996 +       int i, j;
2997
2998 +       if(!acl_count) {
2999 +               DEBUG(10,("Zero acl count passed. Returning Success\n"));
3000 +               return 0;
3001 +       }
3002 +
3003 +       if(aclp == NULL) {
3004 +               DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
3005 +               return -1;
3006 +       }
3007 +
3008 +       /* Count different types of ACLs in the ACLs array */
3009 +
3010 +       hpux_count_obj(acl_count, aclp, &acl_obj_count);
3011 +
3012 +       /* There should be only one entry each of type USER_OBJ, GROUP_OBJ, 
3013 +        * CLASS_OBJ and OTHER_OBJ 
3014 +        */
3015 +
3016 +       if( (acl_obj_count.n_user_obj  != 1) || 
3017 +               (acl_obj_count.n_group_obj != 1) || 
3018 +               (acl_obj_count.n_class_obj != 1) ||
3019 +               (acl_obj_count.n_other_obj != 1) 
3020 +       ) {
3021 +               DEBUG(0,("hpux_acl_sort: More than one entry or no entries for \
3022 +USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
3023 +               return -1;
3024 +       }
3025 +
3026 +       /* If any of the default objects are present, there should be only
3027 +        * one of them each.
3028 +        */
3029 +
3030 +       if( (acl_obj_count.n_def_user_obj  > 1) || (acl_obj_count.n_def_group_obj > 1) || 
3031 +                       (acl_obj_count.n_def_other_obj > 1) || (acl_obj_count.n_def_class_obj > 1) ) {
3032 +               DEBUG(0,("hpux_acl_sort: More than one entry for DEF_CLASS_OBJ \
3033 +or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
3034 +               return -1;
3035 +       }
3036 +
3037 +       /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl 
3038 +        * structures.  
3039 +        *
3040 +        * Sorting crieteria - First sort by ACL type. If there are multiple entries of
3041 +        * same ACL type, sort by ACL id.
3042 +        *
3043 +        * I am using the trival kind of sorting method here because, performance isn't 
3044 +        * really effected by the ACLs feature. More over there aren't going to be more
3045 +        * than 17 entries on HPUX. 
3046 +        */
3047 +
3048 +       for(i=0; i<acl_count;i++) {
3049 +               for (j=i+1; j<acl_count; j++) {
3050 +                       if( aclp[i].a_type > aclp[j].a_type ) {
3051 +                               /* ACL entries out of order, swap them */
3052 +
3053 +                               hpux_swap_acl_entries((aclp+i), (aclp+j));
3054 +
3055 +                       } else if ( aclp[i].a_type == aclp[j].a_type ) {
3056 +
3057 +                               /* ACL entries of same type, sort by id */
3058 +
3059 +                               if(aclp[i].a_id > aclp[j].a_id) {
3060 +                                       hpux_swap_acl_entries((aclp+i), (aclp+j));
3061 +                               } else if (aclp[i].a_id == aclp[j].a_id) {
3062 +                                       /* We have a duplicate entry. */
3063 +                                       if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
3064 +                                               DEBUG(0, ("hpux_acl_sort: Duplicate entry: Type(hex): %x Id: %d\n",
3065 +                                                       aclp[i].a_type, aclp[i].a_id));
3066 +                                               return -1;
3067 +                                       }
3068 +                               }
3069 +
3070 +                       }
3071 +               }
3072 +       }
3073 +
3074 +       /* set the class obj permissions to the computed one. */
3075 +       if(calclass) {
3076 +               int n_class_obj_index = -1;
3077 +
3078 +               for(i=0;i<acl_count;i++) {
3079 +                       n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
3080 +
3081 +                       if(aclp[i].a_type == CLASS_OBJ)
3082 +                               n_class_obj_index = i;
3083 +               }
3084 +               aclp[n_class_obj_index].a_perm = n_class_obj_perm;
3085 +       }
3086 +
3087 +       return 0;
3088 +#else
3089 +       return aclsort(acl_count, calclass, aclp);
3090 +#endif
3091 +}
3092 +
3093 +/*
3094 + * sort the ACL and check it for validity
3095 + *
3096 + * if it's a minimal ACL with only 4 entries then we
3097 + * need to recalculate the mask permissions to make
3098 + * sure that they are the same as the GROUP_OBJ
3099 + * permissions as required by the UnixWare acl() system call.
3100 + *
3101 + * (note: since POSIX allows minimal ACLs which only contain
3102 + * 3 entries - ie there is no mask entry - we should, in theory,
3103 + * check for this and add a mask entry if necessary - however
3104 + * we "know" that the caller of this interface always specifies
3105 + * a mask so, in practice "this never happens" (tm) - if it *does*
3106 + * happen aclsort() will fail and return an error and someone will
3107 + * have to fix it ...)
3108 + */
3109 +
3110 +static int acl_sort(SMB_ACL_T acl_d)
3111 +{
3112 +       int fixmask = (acl_d->count <= 4);
3113 +
3114 +       if (hpux_acl_sort(acl_d->count, fixmask, acl_d->acl) != 0) {
3115 +               errno = EINVAL;
3116 +               return -1;
3117 +       }
3118 +       return 0;
3119 +}
3120
3121 +int sys_acl_valid(SMB_ACL_T acl_d)
3122 +{
3123 +       return acl_sort(acl_d);
3124 +}
3125 +
3126 +int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T type, SMB_ACL_T acl_d)
3127 +{
3128 +       struct stat     s;
3129 +       struct acl      *acl_p;
3130 +       int             acl_count;
3131 +       struct acl      *acl_buf        = NULL;
3132 +       int             ret;
3133 +
3134 +       if(hpux_acl_call_presence() == False) {
3135 +               /* Looks like we don't have the acl() system call on HPUX. 
3136 +                * May be the system doesn't have the latest version of JFS.
3137 +                */
3138 +               errno=ENOSYS;
3139 +               return -1; 
3140 +       }
3141 +
3142 +       if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
3143 +               errno = EINVAL;
3144 +               return -1;
3145 +       }
3146 +
3147 +       if (acl_sort(acl_d) != 0) {
3148 +               return -1;
3149 +       }
3150 +
3151 +       acl_p           = &acl_d->acl[0];
3152 +       acl_count       = acl_d->count;
3153 +
3154 +       /*
3155 +        * if it's a directory there is extra work to do
3156 +        * since the acl() system call will replace both
3157 +        * the access ACLs and the default ACLs (if any)
3158 +        */
3159 +       if (stat(name, &s) != 0) {
3160 +               return -1;
3161 +       }
3162 +       if (S_ISDIR(s.st_mode)) {
3163 +               SMB_ACL_T       acc_acl;
3164 +               SMB_ACL_T       def_acl;
3165 +               SMB_ACL_T       tmp_acl;
3166 +               int             i;
3167 +
3168 +               if (type == SMB_ACL_TYPE_ACCESS) {
3169 +                       acc_acl = acl_d;
3170 +                       def_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_DEFAULT);
3171 +
3172 +               } else {
3173 +                       def_acl = acl_d;
3174 +                       acc_acl = tmp_acl = sys_acl_get_file(name, SMB_ACL_TYPE_ACCESS);
3175 +               }
3176 +
3177 +               if (tmp_acl == NULL) {
3178 +                       return -1;
3179 +               }
3180 +
3181 +               /*
3182 +                * allocate a temporary buffer for the complete ACL
3183 +                */
3184 +               acl_count = acc_acl->count + def_acl->count;
3185 +               acl_p = acl_buf = SMB_MALLOC_ARRAY(struct acl, acl_count);
3186 +
3187 +               if (acl_buf == NULL) {
3188 +                       sys_acl_free_acl(tmp_acl);
3189 +                       errno = ENOMEM;
3190 +                       return -1;
3191 +               }
3192 +
3193 +               /*
3194 +                * copy the access control and default entries into the buffer
3195 +                */
3196 +               memcpy(&acl_buf[0], &acc_acl->acl[0],
3197 +                       acc_acl->count * sizeof(acl_buf[0]));
3198 +
3199 +               memcpy(&acl_buf[acc_acl->count], &def_acl->acl[0],
3200 +                       def_acl->count * sizeof(acl_buf[0]));
3201 +
3202 +               /*
3203 +                * set the ACL_DEFAULT flag on the default entries
3204 +                */
3205 +               for (i = acc_acl->count; i < acl_count; i++) {
3206 +                       acl_buf[i].a_type |= ACL_DEFAULT;
3207 +               }
3208 +
3209 +               sys_acl_free_acl(tmp_acl);
3210 +
3211 +       } else if (type != SMB_ACL_TYPE_ACCESS) {
3212 +               errno = EINVAL;
3213 +               return -1;
3214 +       }
3215 +
3216 +       ret = acl(name, ACL_SET, acl_count, acl_p);
3217 +
3218 +       if (acl_buf) {
3219 +               free(acl_buf);
3220 +       }
3221 +
3222 +       return ret;
3223 +}
3224 +
3225 +int sys_acl_set_fd(int fd, SMB_ACL_T acl_d)
3226 +{
3227 +       /*
3228 +        * HPUX doesn't have the facl call. Fake it using the path.... JRA.
3229 +        */
3230 +
3231 +       files_struct *fsp = file_find_fd(fd);
3232 +
3233 +       if (fsp == NULL) {
3234 +               errno = EBADF;
3235 +               return NULL;
3236 +       }
3237 +
3238 +       if (acl_sort(acl_d) != 0) {
3239 +               return -1;
3240 +       }
3241 +
3242 +       /*
3243 +        * We know we're in the same conn context. So we
3244 +        * can use the relative path.
3245 +        */
3246 +
3247 +       return sys_acl_set_file(fsp->fsp_name, SMB_ACL_TYPE_ACCESS, acl_d);
3248 +}
3249 +
3250 +int sys_acl_delete_def_file(const char *path)
3251 +{
3252 +       SMB_ACL_T       acl_d;
3253 +       int             ret;
3254 +
3255 +       /*
3256 +        * fetching the access ACL and rewriting it has
3257 +        * the effect of deleting the default ACL
3258 +        */
3259 +       if ((acl_d = sys_acl_get_file(path, SMB_ACL_TYPE_ACCESS)) == NULL) {
3260 +               return -1;
3261 +       }
3262 +
3263 +       ret = acl(path, ACL_SET, acl_d->count, acl_d->acl);
3264 +
3265 +       sys_acl_free_acl(acl_d);
3266 +       
3267 +       return ret;
3268 +}
3269 +
3270 +int sys_acl_free_text(char *text)
3271 +{
3272 +       free(text);
3273 +       return 0;
3274 +}
3275 +
3276 +int sys_acl_free_acl(SMB_ACL_T acl_d) 
3277 +{
3278 +       free(acl_d);
3279 +       return 0;
3280 +}
3281 +
3282 +int sys_acl_free_qualifier(void *qual, SMB_ACL_TAG_T tagtype)
3283 +{
3284 +       return 0;
3285 +}
3286 +
3287 +#elif defined(HAVE_IRIX_ACLS)
3288 +
3289 +int sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p)
3290 +{
3291 +       if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {
3292 +               errno = EINVAL;
3293 +               return -1;
3294 +       }
3295 +
3296 +       if (entry_p == NULL) {
3297 +               errno = EINVAL;
3298 +               return -1;
3299 +       }
3300 +
3301 +       if (entry_id == SMB_ACL_FIRST_ENTRY) {
3302 +               acl_d->next = 0;
3303 +       }
3304 +
3305 +       if (acl_d->next < 0) {
3306 +               errno = EINVAL;
3307 +               return -1;
3308 +       }
3309 +
3310 +       if (acl_d->next >= acl_d->aclp->acl_cnt) {
3311 +               return 0;
3312 +       }
3313 +
3314 +       *entry_p = &acl_d->aclp->acl_entry[acl_d->next++];
3315 +
3316 +       return 1;
3317 +}
3318 +
3319 +int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p)
3320 +{
3321 +       *type_p = entry_d->ae_tag;
3322 +
3323 +       return 0;
3324 +}
3325 +
3326 +int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
3327 +{
3328 +       *permset_p = entry_d;
3329 +
3330 +       return 0;
3331 +}
3332 +
3333 +void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d)
3334 +{
3335 +       if (entry_d->ae_tag != SMB_ACL_USER
3336 +           && entry_d->ae_tag != SMB_ACL_GROUP) {
3337 +               errno = EINVAL;
3338 +               return NULL;
3339 +       }
3340 +
3341 +       return &entry_d->ae_id;
3342 +}
3343 +
3344 +SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type)
3345 +{
3346 +       SMB_ACL_T       a;
3347 +
3348 +       if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3349 +               errno = ENOMEM;
3350 +               return NULL;
3351 +       }
3352 +       if ((a->aclp = acl_get_file(path_p, type)) == NULL) {
3353 +               SAFE_FREE(a);
3354 +               return NULL;
3355 +       }
3356 +       a->next = -1;
3357 +       a->freeaclp = True;
3358 +       return a;
3359 +}
3360 +
3361 +SMB_ACL_T sys_acl_get_fd(int fd)
3362 +{
3363 +       SMB_ACL_T       a;
3364 +
3365 +       if ((a = SMB_MALLOC_P(struct SMB_ACL_T)) == NULL) {
3366 +               errno = ENOMEM;
3367 +               return NULL;
3368 +       }
3369 +       if ((a->aclp = acl_get_fd(fd)) == NULL) {
3370 +               SAFE_FREE(a);
3371 +               return NULL;
3372 +       }
3373 +       a->next = -1;
3374 +       a->freeaclp = True;
3375 +       return a;
3376 +}
3377 +
3378 +int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d)
3379 +{
3380 +       permset_d->ae_perm = 0;
3381 +
3382 +       return 0;
3383 +}
3384 +
3385 +int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3386 +{
3387 +       if (perm != SMB_ACL_READ && perm != SMB_ACL_WRITE
3388 +           && perm != SMB_ACL_EXECUTE) {
3389 +               errno = EINVAL;
3390 +               return -1;
3391 +       }
3392 +
3393 +       if (permset_d == NULL) {
3394 +               errno = EINVAL;
3395 +               return -1;
3396 +       }
3397 +
3398 +       permset_d->ae_perm |= perm;
3399 +
3400 +       return 0;
3401 +}
3402 +
3403 +int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d, SMB_ACL_PERM_T perm)
3404 +{
3405 +       return permset_d->ae_perm & perm;
3406 +}
3407 +
3408 +char *sys_acl_to_text(SMB_ACL_T acl_d, ssize_t *len_p)
3409 +{
3410 +       return acl_to_text(acl_d->aclp, len_p);
3411 +}
3412 +
3413 +SMB_ACL_T sys_acl_init(int count)
3414 +{
3415 +       SMB_ACL_T       a;
3416 +
3417 +       if (count < 0) {
3418 +               errno = EINVAL;
3419 +               return NULL;
3420 +       }
3421 +
3422 +       if ((a = SMB_MALLOC(sizeof(struct SMB_ACL_T) + sizeof(struct acl))) == NULL) {
3423 +               errno = ENOMEM;
3424 +               return NULL;
3425 +       }
3426 +
3427 +       a->next = -1;
3428 +       a->freeaclp = False;
3429 +       a->aclp = (struct acl *)(&a->aclp + sizeof(struct acl *));
3430 +       a->aclp->acl_cnt = 0;
3431 +
3432 +       return a;
3433 +}
3434 +
3435 +
3436 +int sys_acl_create_entry(SMB_ACL_T *acl_p, SMB_ACL_ENTRY_T *entry_p)
3437 +{
3438 +       SMB_ACL_T       acl_d;
3439 +       SMB_ACL_ENTRY_T entry_d;
3440 +
3441 +       if (acl_p == NULL || entry_p == NULL || (acl_d = *acl_p) == NULL) {
3442 +               errno = EINVAL;
3443 +               return -1;
3444 +       }
3445 +
3446 +       if (acl_d->aclp->acl_cnt >= ACL_MAX_ENTRIES) {
3447 +               errno = ENOSPC;
3448 +               return -1;
3449 +       }
3450 +
3451 +       entry_d         = &acl_d->aclp->acl_entry[acl_d->aclp->acl_cnt++];
3452 +       entry_d->ae_tag = 0;
3453 +      &nbs