2 * Copyright (c) 2003-2010 Tim Kientzle
3 * Copyright (c) 2016 Martin Matuska
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
40 #include "archive_acl_private.h"
41 #include "archive_entry.h"
42 #include "archive_private.h"
45 #define max(a, b) ((a)>(b)?(a):(b))
48 /* Good enough for simple equality testing, but not for sorting. */
49 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
52 static int acl_special(struct archive_acl *acl,
53 int type, int permset, int tag);
54 static struct archive_acl_entry *acl_new_entry(struct archive_acl *acl,
55 int type, int permset, int tag, int id);
56 static int archive_acl_add_entry_len_l(struct archive_acl *acl,
57 int type, int permset, int tag, int id, const char *name,
58 size_t len, struct archive_string_conv *sc);
59 static int archive_acl_text_want_type(struct archive_acl *acl, int flags);
60 static ssize_t archive_acl_text_len(struct archive_acl *acl, int want_type,
61 int flags, int wide, struct archive *a,
62 struct archive_string_conv *sc);
63 static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
64 static int ismode_w(const wchar_t *start, const wchar_t *end, int *result);
65 static int is_nfs4_flags_w(const wchar_t *start, const wchar_t *end,
67 static int is_nfs4_perms_w(const wchar_t *start, const wchar_t *end,
69 static void next_field_w(const wchar_t **wp, const wchar_t **start,
70 const wchar_t **end, wchar_t *sep);
71 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int type,
72 int tag, int flags, const wchar_t *wname, int perm, int id);
73 static void append_id_w(wchar_t **wp, int id);
74 static int isint(const char *start, const char *end, int *result);
75 static int ismode(const char *start, const char *end, int *result);
76 static int is_nfs4_flags(const char *start, const char *end,
78 static int is_nfs4_perms(const char *start, const char *end,
80 static void next_field(const char **p, const char **start,
81 const char **end, char *sep);
82 static void append_entry(char **p, const char *prefix, int type,
83 int tag, int flags, const char *name, int perm, int id);
84 static void append_id(char **p, int id);
90 } nfsv4_acl_perm_map[] = {
91 { ARCHIVE_ENTRY_ACL_READ_DATA | ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, 'r',
93 { ARCHIVE_ENTRY_ACL_WRITE_DATA | ARCHIVE_ENTRY_ACL_ADD_FILE, 'w',
95 { ARCHIVE_ENTRY_ACL_EXECUTE, 'x', L'x' },
96 { ARCHIVE_ENTRY_ACL_APPEND_DATA | ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY,
98 { ARCHIVE_ENTRY_ACL_DELETE, 'd', L'd' },
99 { ARCHIVE_ENTRY_ACL_DELETE_CHILD, 'D', L'D' },
100 { ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, 'a', L'a' },
101 { ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, 'A', L'A' },
102 { ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, 'R', L'R' },
103 { ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, 'W', L'W' },
104 { ARCHIVE_ENTRY_ACL_READ_ACL, 'c', L'c' },
105 { ARCHIVE_ENTRY_ACL_WRITE_ACL, 'C', L'C' },
106 { ARCHIVE_ENTRY_ACL_WRITE_OWNER, 'o', L'o' },
107 { ARCHIVE_ENTRY_ACL_SYNCHRONIZE, 's', L's' }
110 static const int nfsv4_acl_perm_map_size = (int)(sizeof(nfsv4_acl_perm_map) /
111 sizeof(nfsv4_acl_perm_map[0]));
113 static const struct {
117 } nfsv4_acl_flag_map[] = {
118 { ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, 'f', L'f' },
119 { ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, 'd', L'd' },
120 { ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, 'i', L'i' },
121 { ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, 'n', L'n' },
122 { ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS, 'S', L'S' },
123 { ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS, 'F', L'F' },
124 { ARCHIVE_ENTRY_ACL_ENTRY_INHERITED, 'I', L'I' }
127 static const int nfsv4_acl_flag_map_size = (int)(sizeof(nfsv4_acl_flag_map) /
128 sizeof(nfsv4_acl_flag_map[0]));
131 archive_acl_clear(struct archive_acl *acl)
133 struct archive_acl_entry *ap;
135 while (acl->acl_head != NULL) {
136 ap = acl->acl_head->next;
137 archive_mstring_clean(&acl->acl_head->name);
141 free(acl->acl_text_w);
142 acl->acl_text_w = NULL;
144 acl->acl_text = NULL;
147 acl->acl_state = 0; /* Not counting. */
151 archive_acl_copy(struct archive_acl *dest, struct archive_acl *src)
153 struct archive_acl_entry *ap, *ap2;
155 archive_acl_clear(dest);
157 dest->mode = src->mode;
160 ap2 = acl_new_entry(dest,
161 ap->type, ap->permset, ap->tag, ap->id);
163 archive_mstring_copy(&ap2->name, &ap->name);
169 archive_acl_add_entry(struct archive_acl *acl,
170 int type, int permset, int tag, int id, const char *name)
172 struct archive_acl_entry *ap;
174 if (acl_special(acl, type, permset, tag) == 0)
176 ap = acl_new_entry(acl, type, permset, tag, id);
179 return ARCHIVE_FAILED;
181 if (name != NULL && *name != '\0')
182 archive_mstring_copy_mbs(&ap->name, name);
184 archive_mstring_clean(&ap->name);
189 archive_acl_add_entry_w_len(struct archive_acl *acl,
190 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
192 struct archive_acl_entry *ap;
194 if (acl_special(acl, type, permset, tag) == 0)
196 ap = acl_new_entry(acl, type, permset, tag, id);
199 return ARCHIVE_FAILED;
201 if (name != NULL && *name != L'\0' && len > 0)
202 archive_mstring_copy_wcs_len(&ap->name, name, len);
204 archive_mstring_clean(&ap->name);
209 archive_acl_add_entry_len_l(struct archive_acl *acl,
210 int type, int permset, int tag, int id, const char *name, size_t len,
211 struct archive_string_conv *sc)
213 struct archive_acl_entry *ap;
216 if (acl_special(acl, type, permset, tag) == 0)
218 ap = acl_new_entry(acl, type, permset, tag, id);
221 return ARCHIVE_FAILED;
223 if (name != NULL && *name != '\0' && len > 0) {
224 r = archive_mstring_copy_mbs_len_l(&ap->name, name, len, sc);
227 archive_mstring_clean(&ap->name);
231 else if (errno == ENOMEM)
232 return (ARCHIVE_FATAL);
234 return (ARCHIVE_WARN);
238 * If this ACL entry is part of the standard POSIX permissions set,
239 * store the permissions in the stat structure and return zero.
242 acl_special(struct archive_acl *acl, int type, int permset, int tag)
244 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
245 && ((permset & ~007) == 0)) {
247 case ARCHIVE_ENTRY_ACL_USER_OBJ:
249 acl->mode |= (permset & 7) << 6;
251 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
253 acl->mode |= (permset & 7) << 3;
255 case ARCHIVE_ENTRY_ACL_OTHER:
257 acl->mode |= permset & 7;
265 * Allocate and populate a new ACL entry with everything but the
268 static struct archive_acl_entry *
269 acl_new_entry(struct archive_acl *acl,
270 int type, int permset, int tag, int id)
272 struct archive_acl_entry *ap, *aq;
274 /* Type argument must be a valid NFS4 or POSIX.1e type.
275 * The type must agree with anything already set and
276 * the permset must be compatible. */
277 if (type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
278 if (acl->acl_types & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
282 ~(ARCHIVE_ENTRY_ACL_PERMS_NFS4
283 | ARCHIVE_ENTRY_ACL_INHERITANCE_NFS4)) {
286 } else if (type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
287 if (acl->acl_types & ~ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
290 if (permset & ~ARCHIVE_ENTRY_ACL_PERMS_POSIX1E) {
297 /* Verify the tag is valid and compatible with NFS4 or POSIX.1e. */
299 case ARCHIVE_ENTRY_ACL_USER:
300 case ARCHIVE_ENTRY_ACL_USER_OBJ:
301 case ARCHIVE_ENTRY_ACL_GROUP:
302 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
303 /* Tags valid in both NFS4 and POSIX.1e */
305 case ARCHIVE_ENTRY_ACL_MASK:
306 case ARCHIVE_ENTRY_ACL_OTHER:
307 /* Tags valid only in POSIX.1e. */
308 if (type & ~ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
312 case ARCHIVE_ENTRY_ACL_EVERYONE:
313 /* Tags valid only in NFS4. */
314 if (type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
319 /* No other values are valid. */
323 free(acl->acl_text_w);
324 acl->acl_text_w = NULL;
326 acl->acl_text = NULL;
329 * If there's a matching entry already in the list, overwrite it.
330 * NFSv4 entries may be repeated and are not overwritten.
332 * TODO: compare names of no id is provided (needs more rework)
337 if (((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) == 0) &&
338 ap->type == type && ap->tag == tag && ap->id == id) {
339 if (id != -1 || (tag != ARCHIVE_ENTRY_ACL_USER &&
340 tag != ARCHIVE_ENTRY_ACL_GROUP)) {
341 ap->permset = permset;
349 /* Add a new entry to the end of the list. */
350 ap = (struct archive_acl_entry *)calloc(1, sizeof(*ap));
360 ap->permset = permset;
361 acl->acl_types |= type;
366 * Return a count of entries matching "want_type".
369 archive_acl_count(struct archive_acl *acl, int want_type)
372 struct archive_acl_entry *ap;
377 if ((ap->type & want_type) != 0)
382 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
388 * Return a bitmask of stored ACL types in an ACL list
391 archive_acl_types(struct archive_acl *acl)
393 return (acl->acl_types);
397 * Prepare for reading entries from the ACL data. Returns a count
398 * of entries matching "want_type", or zero if there are no
399 * non-extended ACL entries of that type.
402 archive_acl_reset(struct archive_acl *acl, int want_type)
406 count = archive_acl_count(acl, want_type);
409 * If the only entries are the three standard ones,
410 * then don't return any ACL data. (In this case,
411 * client can just use chmod(2) to set permissions.)
413 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
419 acl->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
422 acl->acl_p = acl->acl_head;
428 * Return the next ACL entry in the list. Fake entries for the
429 * standard permissions and include them in the returned list.
432 archive_acl_next(struct archive *a, struct archive_acl *acl, int want_type,
433 int *type, int *permset, int *tag, int *id, const char **name)
439 * The acl_state is either zero (no entries available), -1
440 * (reading from list), or an entry type (retrieve that type
441 * from ae_stat.aest_mode).
443 if (acl->acl_state == 0)
444 return (ARCHIVE_WARN);
446 /* The first three access entries are special. */
447 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
448 switch (acl->acl_state) {
449 case ARCHIVE_ENTRY_ACL_USER_OBJ:
450 *permset = (acl->mode >> 6) & 7;
451 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
452 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
453 acl->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
455 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
456 *permset = (acl->mode >> 3) & 7;
457 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
458 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
459 acl->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
461 case ARCHIVE_ENTRY_ACL_OTHER:
462 *permset = acl->mode & 7;
463 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
464 *tag = ARCHIVE_ENTRY_ACL_OTHER;
466 acl->acl_p = acl->acl_head;
473 while (acl->acl_p != NULL && (acl->acl_p->type & want_type) == 0)
474 acl->acl_p = acl->acl_p->next;
475 if (acl->acl_p == NULL) {
482 return (ARCHIVE_EOF); /* End of ACL entries. */
484 *type = acl->acl_p->type;
485 *permset = acl->acl_p->permset;
486 *tag = acl->acl_p->tag;
487 *id = acl->acl_p->id;
488 if (archive_mstring_get_mbs(a, &acl->acl_p->name, name) != 0) {
490 return (ARCHIVE_FATAL);
493 acl->acl_p = acl->acl_p->next;
498 * Determine what type of ACL do we want
501 archive_acl_text_want_type(struct archive_acl *acl, int flags)
505 /* Check if ACL is NFSv4 */
506 if ((acl->acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
507 /* NFSv4 should never mix with POSIX.1e */
508 if ((acl->acl_types & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0)
511 return (ARCHIVE_ENTRY_ACL_TYPE_NFS4);
514 /* Now deal with POSIX.1e ACLs */
517 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
518 want_type |= ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
519 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
520 want_type |= ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
522 /* By default we want both access and default ACLs */
524 return (ARCHIVE_ENTRY_ACL_TYPE_POSIX1E);
530 * Calculate ACL text string length
533 archive_acl_text_len(struct archive_acl *acl, int want_type, int flags,
534 int wide, struct archive *a, struct archive_string_conv *sc) {
535 struct archive_acl_entry *ap;
537 const wchar_t *wname;
538 int count, idlen, tmp, r;
544 for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
545 if ((ap->type & want_type) == 0)
548 * Filemode-mapping ACL entries are stored exclusively in
549 * ap->mode so they should not be in the list
551 if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
552 && (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
553 || ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
554 || ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
557 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0
558 && (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
559 length += 8; /* "default:" */
561 case ARCHIVE_ENTRY_ACL_USER_OBJ:
562 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
563 length += 6; /* "owner@" */
567 case ARCHIVE_ENTRY_ACL_USER:
568 case ARCHIVE_ENTRY_ACL_MASK:
569 length += 4; /* "user", "mask" */
571 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
572 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
573 length += 6; /* "group@" */
577 case ARCHIVE_ENTRY_ACL_GROUP:
578 case ARCHIVE_ENTRY_ACL_OTHER:
579 length += 5; /* "group", "other" */
581 case ARCHIVE_ENTRY_ACL_EVERYONE:
582 length += 9; /* "everyone@" */
585 length += 1; /* colon after tag */
586 if (ap->tag == ARCHIVE_ENTRY_ACL_USER ||
587 ap->tag == ARCHIVE_ENTRY_ACL_GROUP) {
589 r = archive_mstring_get_wcs(a, &ap->name,
591 if (r == 0 && wname != NULL)
592 length += wcslen(wname);
593 else if (r < 0 && errno == ENOMEM)
596 length += sizeof(uid_t) * 3 + 1;
598 r = archive_mstring_get_mbs_l(&ap->name, &name,
602 if (len > 0 && name != NULL)
605 length += sizeof(uid_t) * 3 + 1;
607 length += 1; /* colon after user or group name */
608 } else if (want_type != ARCHIVE_ENTRY_ACL_TYPE_NFS4)
609 length += 1; /* 2nd colon empty user,group or other */
611 if (((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) != 0)
612 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0)
613 && (ap->tag == ARCHIVE_ENTRY_ACL_OTHER
614 || ap->tag == ARCHIVE_ENTRY_ACL_MASK)) {
615 /* Solaris has no colon after other: and mask: */
619 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
620 /* rwxpdDaARWcCos:fdinSFI:deny */
622 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DENY) == 0)
623 length += 1; /* allow, alarm, audit */
625 length += 3; /* rwx */
627 if ((ap->tag == ARCHIVE_ENTRY_ACL_USER ||
628 ap->tag == ARCHIVE_ENTRY_ACL_GROUP) &&
629 (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID) != 0) {
630 length += 1; /* colon */
640 length ++; /* entry separator */
643 /* Add filemode-mapping access entries to the length */
644 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
645 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) != 0) {
646 /* "user::rwx\ngroup::rwx\nother:rwx\n" */
649 /* "user::rwx\ngroup::rwx\nother::rwx\n" */
652 } else if (count == 0)
655 /* The terminating character is included in count */
660 * Generate a wide text version of the ACL. The flags parameter controls
661 * the type and style of the generated ACL.
664 archive_acl_to_text_w(struct archive_acl *acl, ssize_t *text_len, int flags,
670 const wchar_t *wname;
671 const wchar_t *prefix;
673 struct archive_acl_entry *ap;
674 int id, r, want_type;
677 want_type = archive_acl_text_want_type(acl, flags);
679 /* Both NFSv4 and POSIX.1 types found */
683 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_POSIX1E)
684 flags |= ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;
686 length = archive_acl_text_len(acl, want_type, flags, 1, a, NULL);
691 if (flags & ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA)
696 /* Now, allocate the string and actually populate it. */
697 wp = ws = (wchar_t *)malloc(length * sizeof(wchar_t));
700 __archive_errx(1, "No memory");
705 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
706 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
707 ARCHIVE_ENTRY_ACL_USER_OBJ, flags, NULL,
708 acl->mode & 0700, -1);
710 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
711 ARCHIVE_ENTRY_ACL_GROUP_OBJ, flags, NULL,
712 acl->mode & 0070, -1);
714 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
715 ARCHIVE_ENTRY_ACL_OTHER, flags, NULL,
716 acl->mode & 0007, -1);
720 for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
721 if ((ap->type & want_type) == 0)
724 * Filemode-mapping ACL entries are stored exclusively in
725 * ap->mode so they should not be in the list
727 if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
728 && (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
729 || ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
730 || ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
732 if (ap->type == ARCHIVE_ENTRY_ACL_TYPE_DEFAULT &&
733 (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
734 prefix = L"default:";
737 r = archive_mstring_get_wcs(a, &ap->name, &wname);
741 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
745 append_entry_w(&wp, prefix, ap->type, ap->tag, flags,
746 wname, ap->permset, id);
748 } else if (r < 0 && errno == ENOMEM) {
754 /* Add terminating character */
759 if ((ssize_t)len > (length - 1))
760 __archive_errx(1, "Buffer overrun");
762 if (text_len != NULL)
769 append_id_w(wchar_t **wp, int id)
774 append_id_w(wp, id / 10);
775 *(*wp)++ = L"0123456789"[id % 10];
779 append_entry_w(wchar_t **wp, const wchar_t *prefix, int type,
780 int tag, int flags, const wchar_t *wname, int perm, int id)
784 if (prefix != NULL) {
789 case ARCHIVE_ENTRY_ACL_USER_OBJ:
792 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
793 wcscpy(*wp, L"owner@");
797 case ARCHIVE_ENTRY_ACL_USER:
798 wcscpy(*wp, L"user");
800 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
803 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
804 wcscpy(*wp, L"group@");
808 case ARCHIVE_ENTRY_ACL_GROUP:
809 wcscpy(*wp, L"group");
811 case ARCHIVE_ENTRY_ACL_MASK:
812 wcscpy(*wp, L"mask");
816 case ARCHIVE_ENTRY_ACL_OTHER:
817 wcscpy(*wp, L"other");
821 case ARCHIVE_ENTRY_ACL_EVERYONE:
822 wcscpy(*wp, L"everyone@");
829 if (((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) ||
830 tag == ARCHIVE_ENTRY_ACL_USER ||
831 tag == ARCHIVE_ENTRY_ACL_GROUP) {
835 } else if (tag == ARCHIVE_ENTRY_ACL_USER
836 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
838 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) == 0)
841 /* Solaris style has no second colon after other and mask */
842 if (((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) == 0)
843 || (tag != ARCHIVE_ENTRY_ACL_OTHER
844 && tag != ARCHIVE_ENTRY_ACL_MASK))
847 if ((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
848 /* POSIX.1e ACL perms */
849 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
850 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
851 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
853 /* NFSv4 ACL perms */
854 for (i = 0; i < nfsv4_acl_perm_map_size; i++) {
855 if (perm & nfsv4_acl_perm_map[i].perm)
856 *(*wp)++ = nfsv4_acl_perm_map[i].wc;
857 else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
861 for (i = 0; i < nfsv4_acl_flag_map_size; i++) {
862 if (perm & nfsv4_acl_flag_map[i].perm)
863 *(*wp)++ = nfsv4_acl_flag_map[i].wc;
864 else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
869 case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
870 wcscpy(*wp, L"allow");
872 case ARCHIVE_ENTRY_ACL_TYPE_DENY:
873 wcscpy(*wp, L"deny");
875 case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
876 wcscpy(*wp, L"audit");
878 case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
879 wcscpy(*wp, L"alarm");
893 * Generate a text version of the ACL. The flags parameter controls
894 * the type and style of the generated ACL.
897 archive_acl_to_text_l(struct archive_acl *acl, ssize_t *text_len, int flags,
898 struct archive_string_conv *sc)
906 struct archive_acl_entry *ap;
907 int id, r, want_type;
910 want_type = archive_acl_text_want_type(acl, flags);
912 /* Both NFSv4 and POSIX.1 types found */
916 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_POSIX1E)
917 flags |= ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;
919 length = archive_acl_text_len(acl, want_type, flags, 0, NULL, sc);
924 if (flags & ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA)
929 /* Now, allocate the string and actually populate it. */
930 p = s = (char *)malloc(length * sizeof(char));
933 __archive_errx(1, "No memory");
938 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
939 append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
940 ARCHIVE_ENTRY_ACL_USER_OBJ, flags, NULL,
941 acl->mode & 0700, -1);
943 append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
944 ARCHIVE_ENTRY_ACL_GROUP_OBJ, flags, NULL,
945 acl->mode & 0070, -1);
947 append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
948 ARCHIVE_ENTRY_ACL_OTHER, flags, NULL,
949 acl->mode & 0007, -1);
953 for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
954 if ((ap->type & want_type) == 0)
957 * Filemode-mapping ACL entries are stored exclusively in
958 * ap->mode so they should not be in the list
960 if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
961 && (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
962 || ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
963 || ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
965 if (ap->type == ARCHIVE_ENTRY_ACL_TYPE_DEFAULT &&
966 (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
970 r = archive_mstring_get_mbs_l(
971 &ap->name, &name, &len, sc);
979 (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)) {
984 append_entry(&p, prefix, ap->type, ap->tag, flags, name,
989 /* Add terminating character */
994 if ((ssize_t)len > (length - 1))
995 __archive_errx(1, "Buffer overrun");
997 if (text_len != NULL)
1004 append_id(char **p, int id)
1009 append_id(p, id / 10);
1010 *(*p)++ = "0123456789"[id % 10];
1014 append_entry(char **p, const char *prefix, int type,
1015 int tag, int flags, const char *name, int perm, int id)
1019 if (prefix != NULL) {
1024 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1027 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
1028 strcpy(*p, "owner@");
1032 case ARCHIVE_ENTRY_ACL_USER:
1035 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1038 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
1039 strcpy(*p, "group@");
1043 case ARCHIVE_ENTRY_ACL_GROUP:
1044 strcpy(*p, "group");
1046 case ARCHIVE_ENTRY_ACL_MASK:
1051 case ARCHIVE_ENTRY_ACL_OTHER:
1052 strcpy(*p, "other");
1056 case ARCHIVE_ENTRY_ACL_EVERYONE:
1057 strcpy(*p, "everyone@");
1064 if (((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) ||
1065 tag == ARCHIVE_ENTRY_ACL_USER ||
1066 tag == ARCHIVE_ENTRY_ACL_GROUP) {
1070 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1071 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1073 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) == 0)
1076 /* Solaris style has no second colon after other and mask */
1077 if (((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) == 0)
1078 || (tag != ARCHIVE_ENTRY_ACL_OTHER
1079 && tag != ARCHIVE_ENTRY_ACL_MASK))
1082 if ((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
1083 /* POSIX.1e ACL perms */
1084 *(*p)++ = (perm & 0444) ? 'r' : '-';
1085 *(*p)++ = (perm & 0222) ? 'w' : '-';
1086 *(*p)++ = (perm & 0111) ? 'x' : '-';
1088 /* NFSv4 ACL perms */
1089 for (i = 0; i < nfsv4_acl_perm_map_size; i++) {
1090 if (perm & nfsv4_acl_perm_map[i].perm)
1091 *(*p)++ = nfsv4_acl_perm_map[i].c;
1092 else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
1096 for (i = 0; i < nfsv4_acl_flag_map_size; i++) {
1097 if (perm & nfsv4_acl_flag_map[i].perm)
1098 *(*p)++ = nfsv4_acl_flag_map[i].c;
1099 else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
1104 case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
1105 strcpy(*p, "allow");
1107 case ARCHIVE_ENTRY_ACL_TYPE_DENY:
1110 case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
1111 strcpy(*p, "audit");
1113 case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
1114 strcpy(*p, "alarm");
1126 * Parse a wide ACL text string.
1128 * The want_type argument may be one of the following:
1129 * ARCHIVE_ENTRY_ACL_TYPE_ACCESS - text is a POSIX.1e ACL of type ACCESS
1130 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT - text is a POSIX.1e ACL of type DEFAULT
1131 * ARCHIVE_ENTRY_ACL_TYPE_NFS4 - text is as a NFSv4 ACL
1133 * POSIX.1e ACL entries prefixed with "default:" are treated as
1134 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT unless type is ARCHIVE_ENTRY_ACL_TYPE_NFS4
1137 archive_acl_from_text_w(struct archive_acl *acl, const wchar_t *text,
1141 const wchar_t *start;
1145 const wchar_t *s, *st;
1147 int numfields, fields, n, r, sol, ret;
1148 int type, types, tag, permset, id;
1155 switch (want_type) {
1156 case ARCHIVE_ENTRY_ACL_TYPE_POSIX1E:
1157 want_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1159 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
1160 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
1163 case ARCHIVE_ENTRY_ACL_TYPE_NFS4:
1167 return (ARCHIVE_FATAL);
1170 while (text != NULL && *text != L'\0') {
1172 * Parse the fields out of the next entry,
1173 * advance 'text' to start of next entry.
1177 const wchar_t *start, *end;
1178 next_field_w(&text, &start, &end, &sep);
1179 if (fields < numfields) {
1180 field[fields].start = start;
1181 field[fields].end = end;
1184 } while (sep == L':');
1186 /* Set remaining fields to blank. */
1187 for (n = fields; n < numfields; ++n)
1188 field[n].start = field[n].end = NULL;
1190 if (field[0].start != NULL && *(field[0].start) == L'#') {
1191 /* Comment, skip entry */
1199 name.start = name.end = NULL;
1201 if (want_type != ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
1204 * Default keyword "default:user::rwx"
1205 * if found, we have one more field
1207 * We also support old Solaris extension:
1208 * "defaultuser::rwx" is the default ACL corresponding
1209 * to "user::rwx", etc. valid only for first field
1212 len = field[0].end - field[0].start;
1213 if (*s == L'd' && (len == 1 || (len >= 7
1214 && wmemcmp((s + 1), L"efault", 6) == 0))) {
1215 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1217 field[0].start += 7;
1223 /* Check for a numeric ID in field n+1 or n+3. */
1224 isint_w(field[n + 1].start, field[n + 1].end, &id);
1225 /* Field n+3 is optional. */
1226 if (id == -1 && fields > n+3)
1227 isint_w(field[n + 3].start, field[n + 3].end,
1232 st = field[n].start + 1;
1233 len = field[n].end - field[n].start;
1237 if (len == 1 || (len == 4
1238 && wmemcmp(st, L"ser", 3) == 0))
1239 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1242 if (len == 1 || (len == 5
1243 && wmemcmp(st, L"roup", 4) == 0))
1244 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1247 if (len == 1 || (len == 5
1248 && wmemcmp(st, L"ther", 4) == 0))
1249 tag = ARCHIVE_ENTRY_ACL_OTHER;
1252 if (len == 1 || (len == 4
1253 && wmemcmp(st, L"ask", 3) == 0))
1254 tag = ARCHIVE_ENTRY_ACL_MASK;
1261 case ARCHIVE_ENTRY_ACL_OTHER:
1262 case ARCHIVE_ENTRY_ACL_MASK:
1263 if (fields == (n + 2)
1264 && field[n + 1].start < field[n + 1].end
1265 && ismode_w(field[n + 1].start,
1266 field[n + 1].end, &permset)) {
1267 /* This is Solaris-style "other:rwx" */
1269 } else if (fields == (n + 3) &&
1270 field[n + 1].start < field[n + 1].end) {
1271 /* Invalid mask or other field */
1276 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1277 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1279 field[n + 1].start < field[n + 1].end) {
1280 name = field[n + 1];
1281 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
1282 tag = ARCHIVE_ENTRY_ACL_USER;
1284 tag = ARCHIVE_ENTRY_ACL_GROUP;
1288 /* Invalid tag, skip entry */
1294 * Without "default:" we expect mode in field 2
1295 * Exception: Solaris other and mask fields
1297 if (permset == 0 && !ismode_w(field[n + 2 - sol].start,
1298 field[n + 2 - sol].end, &permset)) {
1299 /* Invalid mode, skip entry */
1306 len = field[0].end - field[0].start;
1311 if (wmemcmp(s, L"user", 4) == 0)
1312 tag = ARCHIVE_ENTRY_ACL_USER;
1315 if (wmemcmp(s, L"group", 5) == 0)
1316 tag = ARCHIVE_ENTRY_ACL_GROUP;
1319 if (wmemcmp(s, L"owner@", 6) == 0)
1320 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1321 else if (wmemcmp(s, L"group@", len) == 0)
1322 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1325 if (wmemcmp(s, L"everyone@", 9) == 0)
1326 tag = ARCHIVE_ENTRY_ACL_EVERYONE;
1332 /* Invalid tag, skip entry */
1335 } else if (tag == ARCHIVE_ENTRY_ACL_USER ||
1336 tag == ARCHIVE_ENTRY_ACL_GROUP) {
1339 isint_w(name.start, name.end, &id);
1343 if (!is_nfs4_perms_w(field[1 + n].start,
1344 field[1 + n].end, &permset)) {
1345 /* Invalid NFSv4 perms, skip entry */
1349 if (!is_nfs4_flags_w(field[2 + n].start,
1350 field[2 + n].end, &permset)) {
1351 /* Invalid NFSv4 flags, skip entry */
1355 s = field[3 + n].start;
1356 len = field[3 + n].end - field[3 + n].start;
1359 if (wmemcmp(s, L"deny", 4) == 0)
1360 type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
1361 } else if (len == 5) {
1362 if (wmemcmp(s, L"allow", 5) == 0)
1363 type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
1364 else if (wmemcmp(s, L"audit", 5) == 0)
1365 type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
1366 else if (wmemcmp(s, L"alarm", 5) == 0)
1367 type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
1370 /* Invalid entry type, skip entry */
1374 isint_w(field[4 + n].start, field[4 + n].end, &id);
1377 /* Add entry to the internal list. */
1378 r = archive_acl_add_entry_w_len(acl, type, permset,
1379 tag, id, name.start, name.end - name.start);
1380 if (r < ARCHIVE_WARN)
1382 if (r != ARCHIVE_OK)
1388 archive_acl_reset(acl, types);
1394 * Parse a string to a positive decimal integer. Returns true if
1395 * the string is non-empty and consists only of decimal digits,
1399 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1404 while (start < end) {
1405 if (*start < '0' || *start > '9')
1407 if (n > (INT_MAX / 10) ||
1408 (n == INT_MAX / 10 && (*start - '0') > INT_MAX % 10)) {
1421 * Parse a string as a mode field. Returns true if
1422 * the string is non-empty and consists only of mode characters,
1426 ismode_w(const wchar_t *start, const wchar_t *end, int *permset)
1436 case L'r': case L'R':
1437 *permset |= ARCHIVE_ENTRY_ACL_READ;
1439 case L'w': case L'W':
1440 *permset |= ARCHIVE_ENTRY_ACL_WRITE;
1442 case L'x': case L'X':
1443 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1455 * Parse a string as a NFS4 ACL permission field.
1456 * Returns true if the string is non-empty and consists only of NFS4 ACL
1457 * permission characters, false otherwise
1460 is_nfs4_perms_w(const wchar_t *start, const wchar_t *end, int *permset)
1462 const wchar_t *p = start;
1467 *permset |= ARCHIVE_ENTRY_ACL_READ_DATA;
1470 *permset |= ARCHIVE_ENTRY_ACL_WRITE_DATA;
1473 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1476 *permset |= ARCHIVE_ENTRY_ACL_APPEND_DATA;
1479 *permset |= ARCHIVE_ENTRY_ACL_DELETE_CHILD;
1482 *permset |= ARCHIVE_ENTRY_ACL_DELETE;
1485 *permset |= ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES;
1488 *permset |= ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES;
1491 *permset |= ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS;
1494 *permset |= ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS;
1497 *permset |= ARCHIVE_ENTRY_ACL_READ_ACL;
1500 *permset |= ARCHIVE_ENTRY_ACL_WRITE_ACL;
1503 *permset |= ARCHIVE_ENTRY_ACL_WRITE_OWNER;
1506 *permset |= ARCHIVE_ENTRY_ACL_SYNCHRONIZE;
1518 * Parse a string as a NFS4 ACL flags field.
1519 * Returns true if the string is non-empty and consists only of NFS4 ACL
1520 * flag characters, false otherwise
1523 is_nfs4_flags_w(const wchar_t *start, const wchar_t *end, int *permset)
1525 const wchar_t *p = start;
1530 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT;
1533 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT;
1536 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY;
1540 ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT;
1543 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS;
1546 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS;
1549 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_INHERITED;
1561 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1562 * to point to just after the separator. *start points to the first
1563 * character of the matched text and *end just after the last
1564 * character of the matched identifier. In particular *end - *start
1565 * is the length of the field body, not including leading or trailing
1569 next_field_w(const wchar_t **wp, const wchar_t **start,
1570 const wchar_t **end, wchar_t *sep)
1572 /* Skip leading whitespace to find start of field. */
1573 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1578 /* Scan for the separator. */
1579 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1580 **wp != L'\n' && **wp != L'#') {
1585 /* Locate end of field, trim trailing whitespace if necessary */
1586 if (*wp == *start) {
1590 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1596 /* Handle in-field comments */
1598 while (**wp != L'\0' && **wp != L',' && **wp != L'\n') {
1604 /* Adjust scanner location. */
1610 * Parse an ACL text string.
1612 * The want_type argument may be one of the following:
1613 * ARCHIVE_ENTRY_ACL_TYPE_ACCESS - text is a POSIX.1e ACL of type ACCESS
1614 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT - text is a POSIX.1e ACL of type DEFAULT
1615 * ARCHIVE_ENTRY_ACL_TYPE_NFS4 - text is as a NFSv4 ACL
1617 * POSIX.1e ACL entries prefixed with "default:" are treated as
1618 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT unless type is ARCHIVE_ENTRY_ACL_TYPE_NFS4
1621 archive_acl_from_text_l(struct archive_acl *acl, const char *text,
1622 int want_type, struct archive_string_conv *sc)
1630 int numfields, fields, n, r, sol, ret;
1631 int type, types, tag, permset, id;
1635 switch (want_type) {
1636 case ARCHIVE_ENTRY_ACL_TYPE_POSIX1E:
1637 want_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1639 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
1640 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
1643 case ARCHIVE_ENTRY_ACL_TYPE_NFS4:
1647 return (ARCHIVE_FATAL);
1653 while (text != NULL && *text != '\0') {
1655 * Parse the fields out of the next entry,
1656 * advance 'text' to start of next entry.
1660 const char *start, *end;
1661 next_field(&text, &start, &end, &sep);
1662 if (fields < numfields) {
1663 field[fields].start = start;
1664 field[fields].end = end;
1667 } while (sep == ':');
1669 /* Set remaining fields to blank. */
1670 for (n = fields; n < numfields; ++n)
1671 field[n].start = field[n].end = NULL;
1673 if (field[0].start != NULL && *(field[0].start) == '#') {
1674 /* Comment, skip entry */
1682 name.start = name.end = NULL;
1684 if (want_type != ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
1687 * Default keyword "default:user::rwx"
1688 * if found, we have one more field
1690 * We also support old Solaris extension:
1691 * "defaultuser::rwx" is the default ACL corresponding
1692 * to "user::rwx", etc. valid only for first field
1695 len = field[0].end - field[0].start;
1696 if (*s == 'd' && (len == 1 || (len >= 7
1697 && memcmp((s + 1), "efault", 6) == 0))) {
1698 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1700 field[0].start += 7;
1706 /* Check for a numeric ID in field n+1 or n+3. */
1707 isint(field[n + 1].start, field[n + 1].end, &id);
1708 /* Field n+3 is optional. */
1709 if (id == -1 && fields > (n + 3))
1710 isint(field[n + 3].start, field[n + 3].end,
1715 st = field[n].start + 1;
1716 len = field[n].end - field[n].start;
1725 if (len == 1 || (len == 4
1726 && memcmp(st, "ser", 3) == 0))
1727 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1730 if (len == 1 || (len == 5
1731 && memcmp(st, "roup", 4) == 0))
1732 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1735 if (len == 1 || (len == 5
1736 && memcmp(st, "ther", 4) == 0))
1737 tag = ARCHIVE_ENTRY_ACL_OTHER;
1740 if (len == 1 || (len == 4
1741 && memcmp(st, "ask", 3) == 0))
1742 tag = ARCHIVE_ENTRY_ACL_MASK;
1749 case ARCHIVE_ENTRY_ACL_OTHER:
1750 case ARCHIVE_ENTRY_ACL_MASK:
1751 if (fields == (n + 2)
1752 && field[n + 1].start < field[n + 1].end
1753 && ismode(field[n + 1].start,
1754 field[n + 1].end, &permset)) {
1755 /* This is Solaris-style "other:rwx" */
1757 } else if (fields == (n + 3) &&
1758 field[n + 1].start < field[n + 1].end) {
1759 /* Invalid mask or other field */
1764 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1765 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1767 field[n + 1].start < field[n + 1].end) {
1768 name = field[n + 1];
1769 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
1770 tag = ARCHIVE_ENTRY_ACL_USER;
1772 tag = ARCHIVE_ENTRY_ACL_GROUP;
1776 /* Invalid tag, skip entry */
1782 * Without "default:" we expect mode in field 3
1783 * Exception: Solaris other and mask fields
1785 if (permset == 0 && !ismode(field[n + 2 - sol].start,
1786 field[n + 2 - sol].end, &permset)) {
1787 /* Invalid mode, skip entry */
1794 len = field[0].end - field[0].start;
1799 if (memcmp(s, "user", 4) == 0)
1800 tag = ARCHIVE_ENTRY_ACL_USER;
1803 if (memcmp(s, "group", 5) == 0)
1804 tag = ARCHIVE_ENTRY_ACL_GROUP;
1807 if (memcmp(s, "owner@", 6) == 0)
1808 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1809 else if (memcmp(s, "group@", 6) == 0)
1810 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1813 if (memcmp(s, "everyone@", 9) == 0)
1814 tag = ARCHIVE_ENTRY_ACL_EVERYONE;
1821 /* Invalid tag, skip entry */
1824 } else if (tag == ARCHIVE_ENTRY_ACL_USER ||
1825 tag == ARCHIVE_ENTRY_ACL_GROUP) {
1828 isint(name.start, name.end, &id);
1832 if (!is_nfs4_perms(field[1 + n].start,
1833 field[1 + n].end, &permset)) {
1834 /* Invalid NFSv4 perms, skip entry */
1838 if (!is_nfs4_flags(field[2 + n].start,
1839 field[2 + n].end, &permset)) {
1840 /* Invalid NFSv4 flags, skip entry */
1844 s = field[3 + n].start;
1845 len = field[3 + n].end - field[3 + n].start;
1848 if (memcmp(s, "deny", 4) == 0)
1849 type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
1850 } else if (len == 5) {
1851 if (memcmp(s, "allow", 5) == 0)
1852 type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
1853 else if (memcmp(s, "audit", 5) == 0)
1854 type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
1855 else if (memcmp(s, "alarm", 5) == 0)
1856 type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
1859 /* Invalid entry type, skip entry */
1863 isint(field[4 + n].start, field[4 + n].end,
1867 /* Add entry to the internal list. */
1868 r = archive_acl_add_entry_len_l(acl, type, permset,
1869 tag, id, name.start, name.end - name.start, sc);
1870 if (r < ARCHIVE_WARN)
1872 if (r != ARCHIVE_OK)
1878 archive_acl_reset(acl, types);
1884 * Parse a string to a positive decimal integer. Returns true if
1885 * the string is non-empty and consists only of decimal digits,
1889 isint(const char *start, const char *end, int *result)
1894 while (start < end) {
1895 if (*start < '0' || *start > '9')
1897 if (n > (INT_MAX / 10) ||
1898 (n == INT_MAX / 10 && (*start - '0') > INT_MAX % 10)) {
1911 * Parse a string as a mode field. Returns true if
1912 * the string is non-empty and consists only of mode characters,
1916 ismode(const char *start, const char *end, int *permset)
1927 *permset |= ARCHIVE_ENTRY_ACL_READ;
1930 *permset |= ARCHIVE_ENTRY_ACL_WRITE;
1933 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1945 * Parse a string as a NFS4 ACL permission field.
1946 * Returns true if the string is non-empty and consists only of NFS4 ACL
1947 * permission characters, false otherwise
1950 is_nfs4_perms(const char *start, const char *end, int *permset)
1952 const char *p = start;
1957 *permset |= ARCHIVE_ENTRY_ACL_READ_DATA;
1960 *permset |= ARCHIVE_ENTRY_ACL_WRITE_DATA;
1963 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1966 *permset |= ARCHIVE_ENTRY_ACL_APPEND_DATA;
1969 *permset |= ARCHIVE_ENTRY_ACL_DELETE_CHILD;
1972 *permset |= ARCHIVE_ENTRY_ACL_DELETE;
1975 *permset |= ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES;
1978 *permset |= ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES;
1981 *permset |= ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS;
1984 *permset |= ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS;
1987 *permset |= ARCHIVE_ENTRY_ACL_READ_ACL;
1990 *permset |= ARCHIVE_ENTRY_ACL_WRITE_ACL;
1993 *permset |= ARCHIVE_ENTRY_ACL_WRITE_OWNER;
1996 *permset |= ARCHIVE_ENTRY_ACL_SYNCHRONIZE;
2008 * Parse a string as a NFS4 ACL flags field.
2009 * Returns true if the string is non-empty and consists only of NFS4 ACL
2010 * flag characters, false otherwise
2013 is_nfs4_flags(const char *start, const char *end, int *permset)
2015 const char *p = start;
2020 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT;
2023 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT;
2026 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY;
2030 ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT;
2033 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS;
2036 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS;
2039 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_INHERITED;
2051 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
2052 * to point to just after the separator. *start points to the first
2053 * character of the matched text and *end just after the last
2054 * character of the matched identifier. In particular *end - *start
2055 * is the length of the field body, not including leading or trailing
2059 next_field(const char **p, const char **start,
2060 const char **end, char *sep)
2062 /* Skip leading whitespace to find start of field. */
2063 while (**p == ' ' || **p == '\t' || **p == '\n') {
2068 /* Scan for the separator. */
2069 while (**p != '\0' && **p != ',' && **p != ':' && **p != '\n' &&
2075 /* Locate end of field, trim trailing whitespace if necessary */
2080 while (**end == ' ' || **end == '\t' || **end == '\n') {
2086 /* Handle in-field comments */
2088 while (**p != '\0' && **p != ',' && **p != '\n') {
2094 /* Adjust scanner location. */