Imported Upstream version 3.10.0
[platform/upstream/cmake.git] / Utilities / cmlibarchive / libarchive / archive_disk_acl_sunos.c
1 /*-
2  * Copyright (c) 2017 Martin Matuska
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27
28 #if ARCHIVE_ACL_SUNOS
29
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
38 #endif
39 #ifdef HAVE_SYS_ACL_H
40 #define _ACL_PRIVATE /* For debugging */
41 #include <sys/acl.h>
42 #endif
43
44 #include "archive_entry.h"
45 #include "archive_private.h"
46 #include "archive_read_disk_private.h"
47 #include "archive_write_disk_private.h"
48
49 typedef struct {
50         const int a_perm;       /* Libarchive permission or flag */
51         const int p_perm;       /* Platform permission or flag */
52 } acl_perm_map_t;
53
54 static const acl_perm_map_t acl_posix_perm_map[] = {
55         {ARCHIVE_ENTRY_ACL_EXECUTE, S_IXOTH },
56         {ARCHIVE_ENTRY_ACL_WRITE, S_IWOTH },
57         {ARCHIVE_ENTRY_ACL_READ, S_IROTH }
58 };
59
60 static const int acl_posix_perm_map_size =
61     (int)(sizeof(acl_posix_perm_map)/sizeof(acl_posix_perm_map[0]));
62
63 #if ARCHIVE_ACL_SUNOS_NFS4
64 static const acl_perm_map_t acl_nfs4_perm_map[] = {
65         {ARCHIVE_ENTRY_ACL_EXECUTE, ACE_EXECUTE},
66         {ARCHIVE_ENTRY_ACL_READ_DATA, ACE_READ_DATA},
67         {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACE_LIST_DIRECTORY},
68         {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACE_WRITE_DATA},
69         {ARCHIVE_ENTRY_ACL_ADD_FILE, ACE_ADD_FILE},
70         {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACE_APPEND_DATA},
71         {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACE_ADD_SUBDIRECTORY},
72         {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACE_READ_NAMED_ATTRS},
73         {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACE_WRITE_NAMED_ATTRS},
74         {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACE_DELETE_CHILD},
75         {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACE_READ_ATTRIBUTES},
76         {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACE_WRITE_ATTRIBUTES},
77         {ARCHIVE_ENTRY_ACL_DELETE, ACE_DELETE},
78         {ARCHIVE_ENTRY_ACL_READ_ACL, ACE_READ_ACL},
79         {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACE_WRITE_ACL},
80         {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACE_WRITE_OWNER},
81         {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACE_SYNCHRONIZE}
82 };
83
84 static const int acl_nfs4_perm_map_size =
85     (int)(sizeof(acl_nfs4_perm_map)/sizeof(acl_nfs4_perm_map[0]));
86
87 static const acl_perm_map_t acl_nfs4_flag_map[] = {
88         {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACE_FILE_INHERIT_ACE},
89         {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACE_DIRECTORY_INHERIT_ACE},
90         {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACE_NO_PROPAGATE_INHERIT_ACE},
91         {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACE_INHERIT_ONLY_ACE},
92         {ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS, ACE_SUCCESSFUL_ACCESS_ACE_FLAG},
93         {ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS, ACE_FAILED_ACCESS_ACE_FLAG},
94 #ifdef ACE_INHERITED_ACE
95         {ARCHIVE_ENTRY_ACL_ENTRY_INHERITED, ACE_INHERITED_ACE}
96 #endif
97 };
98
99 const int acl_nfs4_flag_map_size =
100     (int)(sizeof(acl_nfs4_flag_map)/sizeof(acl_nfs4_flag_map[0]));
101
102 #endif /* ARCHIVE_ACL_SUNOS_NFS4 */
103
104 static void *
105 sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
106 {
107         int cnt, cntcmd;
108         size_t size;
109         void *aclp;
110
111         if (cmd == GETACL) {
112                 cntcmd = GETACLCNT;
113                 size = sizeof(aclent_t);
114         }
115 #if ARCHIVE_ACL_SUNOS_NFS4
116         else if (cmd == ACE_GETACL) {
117                 cntcmd = ACE_GETACLCNT;
118                 size = sizeof(ace_t);
119         }
120 #endif
121         else {
122                 errno = EINVAL;
123                 *aclcnt = -1;
124                 return (NULL);
125         }
126
127         aclp = NULL;
128         cnt = -2;
129
130         while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) {
131                 if (path != NULL)
132                         cnt = acl(path, cntcmd, 0, NULL);
133                 else
134                         cnt = facl(fd, cntcmd, 0, NULL);
135
136                 if (cnt > 0) {
137                         if (aclp == NULL)
138                                 aclp = malloc(cnt * size);
139                         else
140                                 aclp = realloc(NULL, cnt * size);
141                         if (aclp != NULL) {
142                                 if (path != NULL)
143                                         cnt = acl(path, cmd, cnt, aclp);
144                                 else
145                                         cnt = facl(fd, cmd, cnt, aclp);
146                         }
147                 } else {
148                         if (aclp != NULL) {
149                                 free(aclp);
150                                 aclp = NULL;
151                         }
152                         break;
153                 }
154         }
155
156         *aclcnt = cnt;
157         return (aclp);
158 }
159
160 /*
161  * Check if acl is trivial
162  * This is a FreeBSD acl_is_trivial_np() implementation for Solaris
163  */
164 static int
165 sun_acl_is_trivial(void *aclp, int aclcnt, mode_t mode, int is_nfs4,
166     int is_dir, int *trivialp)
167 {
168 #if ARCHIVE_ACL_SUNOS_NFS4
169         int i, p;
170         const uint32_t rperm = ACE_READ_DATA;
171         const uint32_t wperm = ACE_WRITE_DATA | ACE_APPEND_DATA;
172         const uint32_t eperm = ACE_EXECUTE;
173         const uint32_t pubset = ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
174             ACE_READ_ACL | ACE_SYNCHRONIZE;
175         const uint32_t ownset = pubset | ACE_WRITE_ATTRIBUTES |
176             ACE_WRITE_NAMED_ATTRS | ACE_WRITE_ACL | ACE_WRITE_OWNER;
177
178         ace_t *ace;
179         ace_t tace[6];
180 #endif
181
182         if (aclp == NULL || trivialp == NULL)
183                 return (-1);
184
185         *trivialp = 0;
186
187         /*
188          * POSIX.1e ACLs marked with ACL_IS_TRIVIAL are compatible with
189          * FreeBSD acl_is_trivial_np(). On Solaris they have 4 entries,
190          * including mask.
191          */
192         if (!is_nfs4) {
193                 if (aclcnt == 4)
194                         *trivialp = 1;
195                 return (0);
196         }
197
198 #if ARCHIVE_ACL_SUNOS_NFS4
199         /*
200          * Continue with checking NFSv4 ACLs
201          *
202          * Create list of trivial ace's to be compared
203          */
204
205         /* owner@ allow pre */
206         tace[0].a_flags = ACE_OWNER;
207         tace[0].a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
208         tace[0].a_access_mask = 0;
209
210         /* owner@ deny */
211         tace[1].a_flags = ACE_OWNER;
212         tace[1].a_type = ACE_ACCESS_DENIED_ACE_TYPE;
213         tace[1].a_access_mask = 0;
214
215         /* group@ deny */
216         tace[2].a_flags = ACE_GROUP | ACE_IDENTIFIER_GROUP;
217         tace[2].a_type = ACE_ACCESS_DENIED_ACE_TYPE;
218         tace[2].a_access_mask = 0;
219
220         /* owner@ allow */
221         tace[3].a_flags = ACE_OWNER;
222         tace[3].a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
223         tace[3].a_access_mask = ownset;
224
225         /* group@ allow */
226         tace[4].a_flags = ACE_GROUP | ACE_IDENTIFIER_GROUP;
227         tace[4].a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
228         tace[4].a_access_mask = pubset;
229
230         /* everyone@ allow */
231         tace[5].a_flags = ACE_EVERYONE;
232         tace[5].a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
233         tace[5].a_access_mask = pubset;
234
235         /* Permissions for everyone@ */
236         if (mode & 0004)
237                 tace[5].a_access_mask |= rperm;
238         if (mode & 0002)
239                 tace[5].a_access_mask |= wperm;
240         if (mode & 0001)
241                 tace[5].a_access_mask |= eperm;
242
243         /* Permissions for group@ */
244         if (mode & 0040)
245                 tace[4].a_access_mask |= rperm;
246         else if (mode & 0004)
247                 tace[2].a_access_mask |= rperm;
248         if (mode & 0020)
249                 tace[4].a_access_mask |= wperm;
250         else if (mode & 0002)
251                 tace[2].a_access_mask |= wperm;
252         if (mode & 0010)
253                 tace[4].a_access_mask |= eperm;
254         else if (mode & 0001)
255                 tace[2].a_access_mask |= eperm;
256
257         /* Permissions for owner@ */
258         if (mode & 0400) {
259                 tace[3].a_access_mask |= rperm;
260                 if (!(mode & 0040) && (mode & 0004))
261                         tace[0].a_access_mask |= rperm;
262         } else if ((mode & 0040) || (mode & 0004))
263                 tace[1].a_access_mask |= rperm;
264         if (mode & 0200) {
265                 tace[3].a_access_mask |= wperm;
266                 if (!(mode & 0020) && (mode & 0002))
267                         tace[0].a_access_mask |= wperm;
268         } else if ((mode & 0020) || (mode & 0002))
269                 tace[1].a_access_mask |= wperm;
270         if (mode & 0100) {
271                 tace[3].a_access_mask |= eperm;
272                 if (!(mode & 0010) && (mode & 0001))
273                         tace[0].a_access_mask |= eperm;
274         } else if ((mode & 0010) || (mode & 0001))
275                 tace[1].a_access_mask |= eperm;
276
277         /* Check if the acl count matches */
278         p = 3;
279         for (i = 0; i < 3; i++) {
280                 if (tace[i].a_access_mask != 0)
281                         p++;
282         }
283         if (aclcnt != p)
284                 return (0);
285
286         p = 0;
287         for (i = 0; i < 6; i++) {
288                 if (tace[i].a_access_mask != 0) {
289                         ace = &((ace_t *)aclp)[p];
290                         /*
291                          * Illumos added ACE_DELETE_CHILD to write perms for
292                          * directories. We have to check against that, too.
293                          */
294                         if (ace->a_flags != tace[i].a_flags ||
295                             ace->a_type != tace[i].a_type ||
296                             (ace->a_access_mask != tace[i].a_access_mask &&
297                             (!is_dir || (tace[i].a_access_mask & wperm) == 0 ||
298                             ace->a_access_mask !=
299                             (tace[i].a_access_mask | ACE_DELETE_CHILD))))
300                                 return (0);
301                         p++;
302                 }
303         }
304
305         *trivialp = 1;
306 #else   /* !ARCHIVE_ACL_SUNOS_NFS4 */
307         (void)is_dir;   /* UNUSED */
308         (void)aclp;     /* UNUSED */
309 #endif  /* !ARCHIVE_ACL_SUNOS_NFS4 */
310         return (0);
311 }
312
313 /*
314  * Translate Solaris POSIX.1e and NFSv4 ACLs into libarchive internal ACL
315  */
316 static int
317 translate_acl(struct archive_read_disk *a,
318     struct archive_entry *entry, void *aclp, int aclcnt,
319     int default_entry_acl_type)
320 {
321         int e, i;
322         int ae_id, ae_tag, ae_perm;
323         int entry_acl_type;
324         const char *ae_name;
325         aclent_t *aclent;
326 #if ARCHIVE_ACL_SUNOS_NFS4
327         ace_t *ace;
328 #endif
329
330         if (aclcnt <= 0)
331                 return (ARCHIVE_OK);
332
333         for (e = 0; e < aclcnt; e++) {
334                 ae_name = NULL;
335                 ae_tag = 0;
336                 ae_perm = 0;
337
338 #if ARCHIVE_ACL_SUNOS_NFS4
339                 if (default_entry_acl_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
340                         ace = &((ace_t *)aclp)[e];
341                         ae_id = ace->a_who;
342
343                         switch(ace->a_type) {
344                         case ACE_ACCESS_ALLOWED_ACE_TYPE:
345                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
346                                 break;
347                         case ACE_ACCESS_DENIED_ACE_TYPE:
348                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
349                                 break;
350                         case ACE_SYSTEM_AUDIT_ACE_TYPE:
351                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
352                                 break;
353                         case ACE_SYSTEM_ALARM_ACE_TYPE:
354                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
355                                 break;
356                         default:
357                                 /* Unknown entry type, skip */
358                                 continue;
359                         }
360
361                         if ((ace->a_flags & ACE_OWNER) != 0)
362                                 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
363                         else if ((ace->a_flags & ACE_GROUP) != 0)
364                                 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
365                         else if ((ace->a_flags & ACE_EVERYONE) != 0)
366                                 ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
367                         else if ((ace->a_flags & ACE_IDENTIFIER_GROUP) != 0) {
368                                 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
369                                 ae_name = archive_read_disk_gname(&a->archive,
370                                     ae_id);
371                         } else {
372                                 ae_tag = ARCHIVE_ENTRY_ACL_USER;
373                                 ae_name = archive_read_disk_uname(&a->archive,
374                                     ae_id);
375                         }
376
377                         for (i = 0; i < acl_nfs4_flag_map_size; ++i) {
378                                 if ((ace->a_flags &
379                                     acl_nfs4_flag_map[i].p_perm) != 0)
380                                         ae_perm |= acl_nfs4_flag_map[i].a_perm;
381                         }
382
383                         for (i = 0; i < acl_nfs4_perm_map_size; ++i) {
384                                 if ((ace->a_access_mask &
385                                     acl_nfs4_perm_map[i].p_perm) != 0)
386                                         ae_perm |= acl_nfs4_perm_map[i].a_perm;
387                         }
388                 } else
389 #endif  /* ARCHIVE_ACL_SUNOS_NFS4 */
390                 if (default_entry_acl_type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
391                         aclent = &((aclent_t *)aclp)[e];
392                         if ((aclent->a_type & ACL_DEFAULT) != 0)
393                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
394                         else
395                                 entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
396                         ae_id = aclent->a_id;
397
398                         switch(aclent->a_type) {
399                         case DEF_USER:
400                         case USER:
401                                 ae_name = archive_read_disk_uname(&a->archive,
402                                     ae_id);
403                                 ae_tag = ARCHIVE_ENTRY_ACL_USER;
404                                 break;
405                         case DEF_GROUP:
406                         case GROUP:
407                                 ae_name = archive_read_disk_gname(&a->archive,
408                                     ae_id);
409                                 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
410                                 break;
411                         case DEF_CLASS_OBJ:
412                         case CLASS_OBJ:
413                                 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
414                                 break;
415                         case DEF_USER_OBJ:
416                         case USER_OBJ:
417                                 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
418                                 break;
419                         case DEF_GROUP_OBJ:
420                         case GROUP_OBJ:
421                                 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
422                                 break;
423                         case DEF_OTHER_OBJ:
424                         case OTHER_OBJ:
425                                 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
426                                 break;
427                         default:
428                                 /* Unknown tag type, skip */
429                                 continue;
430                         }
431
432                         for (i = 0; i < acl_posix_perm_map_size; ++i) {
433                                 if ((aclent->a_perm &
434                                     acl_posix_perm_map[i].p_perm) != 0)
435                                         ae_perm |= acl_posix_perm_map[i].a_perm;
436                         }
437                 } else
438                         return (ARCHIVE_WARN);
439
440                 archive_entry_acl_add_entry(entry, entry_acl_type,
441                     ae_perm, ae_tag, ae_id, ae_name);
442         }
443         return (ARCHIVE_OK);
444 }
445
446 static int
447 set_acl(struct archive *a, int fd, const char *name,
448     struct archive_acl *abstract_acl,
449     int ae_requested_type, const char *tname)
450 {
451         aclent_t         *aclent;
452 #if ARCHIVE_ACL_SUNOS_NFS4
453         ace_t            *ace;
454 #endif
455         int              cmd, e, r;
456         void             *aclp;
457         int              ret;
458         int              ae_type, ae_permset, ae_tag, ae_id;
459         int              perm_map_size;
460         const acl_perm_map_t    *perm_map;
461         uid_t            ae_uid;
462         gid_t            ae_gid;
463         const char      *ae_name;
464         int              entries;
465         int              i;
466
467         ret = ARCHIVE_OK;
468         entries = archive_acl_reset(abstract_acl, ae_requested_type);
469         if (entries == 0)
470                 return (ARCHIVE_OK);
471
472
473         switch (ae_requested_type) {
474         case ARCHIVE_ENTRY_ACL_TYPE_POSIX1E:
475                 cmd = SETACL;
476                 aclp = malloc(entries * sizeof(aclent_t));
477                 break;
478 #if ARCHIVE_ACL_SUNOS_NFS4
479         case ARCHIVE_ENTRY_ACL_TYPE_NFS4:
480                 cmd = ACE_SETACL;
481                 aclp = malloc(entries * sizeof(ace_t));
482
483                 break;
484 #endif
485         default:
486                 errno = ENOENT;
487                 archive_set_error(a, errno, "Unsupported ACL type");
488                 return (ARCHIVE_FAILED);
489         }
490
491         if (aclp == NULL) {
492                 archive_set_error(a, errno,
493                     "Can't allocate memory for acl buffer");
494                 return (ARCHIVE_FAILED);
495         }
496
497         e = 0;
498
499         while (archive_acl_next(a, abstract_acl, ae_requested_type, &ae_type,
500                    &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
501                 aclent = NULL;
502 #if ARCHIVE_ACL_SUNOS_NFS4
503                 ace = NULL;
504 #endif
505                 if (cmd == SETACL) {
506                         aclent = &((aclent_t *)aclp)[e];
507                         aclent->a_id = -1;
508                         aclent->a_type = 0;
509                         aclent->a_perm = 0;
510                 }
511 #if ARCHIVE_ACL_SUNOS_NFS4
512                 else {  /* cmd == ACE_SETACL */
513                         ace = &((ace_t *)aclp)[e];
514                         ace->a_who = -1;
515                         ace->a_access_mask = 0;
516                         ace->a_flags = 0;
517                 }
518 #endif  /* ARCHIVE_ACL_SUNOS_NFS4 */
519
520                 switch (ae_tag) {
521                 case ARCHIVE_ENTRY_ACL_USER:
522                         ae_uid = archive_write_disk_uid(a, ae_name, ae_id);
523                         if (aclent != NULL) {
524                                 aclent->a_id = ae_uid;
525                                 aclent->a_type |= USER;
526                         }
527 #if ARCHIVE_ACL_SUNOS_NFS4
528                         else {
529                                 ace->a_who = ae_uid;
530                         }
531 #endif
532                         break;
533                 case ARCHIVE_ENTRY_ACL_GROUP:
534                         ae_gid = archive_write_disk_gid(a, ae_name, ae_id);
535                         if (aclent != NULL) {
536                                 aclent->a_id = ae_gid;
537                                 aclent->a_type |= GROUP;
538                         }
539 #if ARCHIVE_ACL_SUNOS_NFS4
540                         else {
541                                 ace->a_who = ae_gid;
542                                 ace->a_flags |= ACE_IDENTIFIER_GROUP;
543                         }
544 #endif
545                         break;
546                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
547                         if (aclent != NULL)
548                                 aclent->a_type |= USER_OBJ;
549 #if ARCHIVE_ACL_SUNOS_NFS4
550                         else {
551                                 ace->a_flags |= ACE_OWNER;
552                         }
553 #endif
554                         break;
555                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
556                         if (aclent != NULL)
557                                 aclent->a_type |= GROUP_OBJ;
558 #if ARCHIVE_ACL_SUNOS_NFS4
559                         else {
560                                 ace->a_flags |= ACE_GROUP;
561                                 ace->a_flags |= ACE_IDENTIFIER_GROUP;
562                         }
563 #endif
564                         break;
565                 case ARCHIVE_ENTRY_ACL_MASK:
566                         if (aclent != NULL)
567                                 aclent->a_type |= CLASS_OBJ;
568                         break;
569                 case ARCHIVE_ENTRY_ACL_OTHER:
570                         if (aclent != NULL)
571                                 aclent->a_type |= OTHER_OBJ;
572                         break;
573 #if ARCHIVE_ACL_SUNOS_NFS4
574                 case ARCHIVE_ENTRY_ACL_EVERYONE:
575                         if (ace != NULL)
576                                 ace->a_flags |= ACE_EVERYONE;
577                         break;
578 #endif
579                 default:
580                         archive_set_error(a, ARCHIVE_ERRNO_MISC,
581                             "Unsupported ACL tag");
582                         ret = ARCHIVE_FAILED;
583                         goto exit_free;
584                 }
585
586                 r = 0;
587                 switch (ae_type) {
588 #if ARCHIVE_ACL_SUNOS_NFS4
589                 case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
590                         if (ace != NULL)
591                                 ace->a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
592                         else
593                                 r = -1;
594                         break;
595                 case ARCHIVE_ENTRY_ACL_TYPE_DENY:
596                         if (ace != NULL)
597                                 ace->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
598                         else
599                                 r = -1;
600                         break;
601                 case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
602                         if (ace != NULL)
603                                 ace->a_type = ACE_SYSTEM_AUDIT_ACE_TYPE;
604                         else
605                                 r = -1;
606                         break;
607                 case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
608                         if (ace != NULL)
609                                 ace->a_type = ACE_SYSTEM_ALARM_ACE_TYPE;
610                         else
611                                 r = -1;
612                         break;
613 #endif
614                 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
615                         if (aclent == NULL)
616                                 r = -1;
617                         break;
618                 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
619                         if (aclent != NULL)
620                                 aclent->a_type |= ACL_DEFAULT;
621                         else
622                                 r = -1;
623                         break;
624                 default:
625                         archive_set_error(a, ARCHIVE_ERRNO_MISC,
626                             "Unsupported ACL entry type");
627                         ret = ARCHIVE_FAILED;
628                         goto exit_free;
629                 }
630
631                 if (r != 0) {
632                         errno = EINVAL;
633                         archive_set_error(a, errno,
634                             "Failed to set ACL entry type");
635                         ret = ARCHIVE_FAILED;
636                         goto exit_free;
637                 }
638
639 #if ARCHIVE_ACL_SUNOS_NFS4
640                 if (ae_requested_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
641                         perm_map_size = acl_nfs4_perm_map_size;
642                         perm_map = acl_nfs4_perm_map;
643                 } else {
644 #endif
645                         perm_map_size = acl_posix_perm_map_size;
646                         perm_map = acl_posix_perm_map;
647 #if ARCHIVE_ACL_SUNOS_NFS4
648                 }
649 #endif
650                 for (i = 0; i < perm_map_size; ++i) {
651                         if (ae_permset & perm_map[i].a_perm) {
652 #if ARCHIVE_ACL_SUNOS_NFS4
653                                 if (ae_requested_type ==
654                                     ARCHIVE_ENTRY_ACL_TYPE_NFS4)
655                                         ace->a_access_mask |=
656                                             perm_map[i].p_perm;
657                                 else
658 #endif
659                                         aclent->a_perm |= perm_map[i].p_perm;
660                         }
661                 }
662
663 #if ARCHIVE_ACL_SUNOS_NFS4
664                 if (ae_requested_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
665                         for (i = 0; i < acl_nfs4_flag_map_size; ++i) {
666                                 if (ae_permset & acl_nfs4_flag_map[i].a_perm) {
667                                         ace->a_flags |=
668                                             acl_nfs4_flag_map[i].p_perm;
669                                 }
670                         }
671                 }
672 #endif
673         e++;
674         }
675
676         /* Try restoring the ACL through 'fd' if we can. */
677         if (fd >= 0) {
678                 if (facl(fd, cmd, entries, aclp) == 0)
679                         ret = ARCHIVE_OK;
680                 else {
681                         if (errno == EOPNOTSUPP) {
682                                 /* Filesystem doesn't support ACLs */
683                                 ret = ARCHIVE_OK;
684                         } else {
685                                 archive_set_error(a, errno,
686                                     "Failed to set acl on fd: %s", tname);
687                                 ret = ARCHIVE_WARN;
688                         }
689                 }
690         } else if (acl(name, cmd, entries, aclp) != 0) {
691                 if (errno == EOPNOTSUPP) {
692                         /* Filesystem doesn't support ACLs */
693                         ret = ARCHIVE_OK;
694                 } else {
695                         archive_set_error(a, errno, "Failed to set acl: %s",
696                             tname);
697                         ret = ARCHIVE_WARN;
698                 }
699         }
700 exit_free:
701         free(aclp);
702         return (ret);
703 }
704
705 int
706 archive_read_disk_entry_setup_acls(struct archive_read_disk *a,
707     struct archive_entry *entry, int *fd)
708 {
709         const char      *accpath;
710         void            *aclp;
711         int             aclcnt;
712         int             r;
713
714         accpath = NULL;
715
716         if (*fd < 0) {
717                 accpath = archive_read_disk_entry_setup_path(a, entry, fd);
718                 if (accpath == NULL)
719                         return (ARCHIVE_WARN);
720         }
721
722         archive_entry_acl_clear(entry);
723
724         aclp = NULL;
725
726 #if ARCHIVE_ACL_SUNOS_NFS4
727         if (*fd >= 0)
728                 aclp = sunacl_get(ACE_GETACL, &aclcnt, *fd, NULL);
729         else if ((!a->follow_symlinks)
730             && (archive_entry_filetype(entry) == AE_IFLNK))
731                 /* We can't get the ACL of a symlink, so we assume it can't
732                    have one. */
733                 aclp = NULL;
734         else
735                 aclp = sunacl_get(ACE_GETACL, &aclcnt, 0, accpath);
736
737         if (aclp != NULL && sun_acl_is_trivial(aclp, aclcnt,
738             archive_entry_mode(entry), 1, S_ISDIR(archive_entry_mode(entry)),
739             &r) == 0 && r == 1) {
740                 free(aclp);
741                 aclp = NULL;
742                 return (ARCHIVE_OK);
743         }
744
745         if (aclp != NULL) {
746                 r = translate_acl(a, entry, aclp, aclcnt,
747                     ARCHIVE_ENTRY_ACL_TYPE_NFS4);
748                 free(aclp);
749                 aclp = NULL;
750
751                 if (r != ARCHIVE_OK) {
752                         archive_set_error(&a->archive, errno,
753                             "Couldn't translate NFSv4 ACLs");
754                 }
755                 return (r);
756         }
757 #endif  /* ARCHIVE_ACL_SUNOS_NFS4 */
758
759         /* Retrieve POSIX.1e ACLs from file. */
760         if (*fd >= 0)
761                 aclp = sunacl_get(GETACL, &aclcnt, *fd, NULL);
762         else if ((!a->follow_symlinks)
763             && (archive_entry_filetype(entry) == AE_IFLNK))
764                 /* We can't get the ACL of a symlink, so we assume it can't
765                    have one. */
766                 aclp = NULL;
767         else
768                 aclp = sunacl_get(GETACL, &aclcnt, 0, accpath);
769
770         /* Ignore "trivial" ACLs that just mirror the file mode. */
771         if (aclp != NULL && sun_acl_is_trivial(aclp, aclcnt,
772             archive_entry_mode(entry), 0, S_ISDIR(archive_entry_mode(entry)),
773             &r) == 0 && r == 1) {
774                 free(aclp);
775                 aclp = NULL;
776         }
777
778         if (aclp != NULL)
779         {
780                 r = translate_acl(a, entry, aclp, aclcnt,
781                     ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
782                 free(aclp);
783                 aclp = NULL;
784
785                 if (r != ARCHIVE_OK) {
786                         archive_set_error(&a->archive, errno,
787                             "Couldn't translate access ACLs");
788                         return (r);
789                 }
790         }
791
792         return (ARCHIVE_OK);
793 }
794
795 int
796 archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
797     struct archive_acl *abstract_acl, __LA_MODE_T mode)
798 {
799         int             ret = ARCHIVE_OK;
800
801         (void)mode;     /* UNUSED */
802
803         if ((archive_acl_types(abstract_acl)
804             & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
805                 /* Solaris writes POSIX.1e access and default ACLs together */
806                 ret = set_acl(a, fd, name, abstract_acl,
807                     ARCHIVE_ENTRY_ACL_TYPE_POSIX1E, "posix1e");
808
809                 /* Simultaneous POSIX.1e and NFSv4 is not supported */
810                 return (ret);
811         }
812 #if ARCHIVE_ACL_SUNOS_NFS4
813         else if ((archive_acl_types(abstract_acl) &
814             ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
815                 ret = set_acl(a, fd, name, abstract_acl,
816                     ARCHIVE_ENTRY_ACL_TYPE_NFS4, "nfs4");
817         }
818 #endif
819         return (ret);
820 }
821 #endif  /* ARCHIVE_ACL_SUNOS */