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 if (acl->acl_text_w != NULL) {
142 free(acl->acl_text_w);
143 acl->acl_text_w = NULL;
145 if (acl->acl_text != NULL) {
147 acl->acl_text = NULL;
151 acl->acl_state = 0; /* Not counting. */
155 archive_acl_copy(struct archive_acl *dest, struct archive_acl *src)
157 struct archive_acl_entry *ap, *ap2;
159 archive_acl_clear(dest);
161 dest->mode = src->mode;
164 ap2 = acl_new_entry(dest,
165 ap->type, ap->permset, ap->tag, ap->id);
167 archive_mstring_copy(&ap2->name, &ap->name);
173 archive_acl_add_entry(struct archive_acl *acl,
174 int type, int permset, int tag, int id, const char *name)
176 struct archive_acl_entry *ap;
178 if (acl_special(acl, type, permset, tag) == 0)
180 ap = acl_new_entry(acl, type, permset, tag, id);
183 return ARCHIVE_FAILED;
185 if (name != NULL && *name != '\0')
186 archive_mstring_copy_mbs(&ap->name, name);
188 archive_mstring_clean(&ap->name);
193 archive_acl_add_entry_w_len(struct archive_acl *acl,
194 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
196 struct archive_acl_entry *ap;
198 if (acl_special(acl, type, permset, tag) == 0)
200 ap = acl_new_entry(acl, type, permset, tag, id);
203 return ARCHIVE_FAILED;
205 if (name != NULL && *name != L'\0' && len > 0)
206 archive_mstring_copy_wcs_len(&ap->name, name, len);
208 archive_mstring_clean(&ap->name);
213 archive_acl_add_entry_len_l(struct archive_acl *acl,
214 int type, int permset, int tag, int id, const char *name, size_t len,
215 struct archive_string_conv *sc)
217 struct archive_acl_entry *ap;
220 if (acl_special(acl, type, permset, tag) == 0)
222 ap = acl_new_entry(acl, type, permset, tag, id);
225 return ARCHIVE_FAILED;
227 if (name != NULL && *name != '\0' && len > 0) {
228 r = archive_mstring_copy_mbs_len_l(&ap->name, name, len, sc);
231 archive_mstring_clean(&ap->name);
235 else if (errno == ENOMEM)
236 return (ARCHIVE_FATAL);
238 return (ARCHIVE_WARN);
242 * If this ACL entry is part of the standard POSIX permissions set,
243 * store the permissions in the stat structure and return zero.
246 acl_special(struct archive_acl *acl, int type, int permset, int tag)
248 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
249 && ((permset & ~007) == 0)) {
251 case ARCHIVE_ENTRY_ACL_USER_OBJ:
253 acl->mode |= (permset & 7) << 6;
255 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
257 acl->mode |= (permset & 7) << 3;
259 case ARCHIVE_ENTRY_ACL_OTHER:
261 acl->mode |= permset & 7;
269 * Allocate and populate a new ACL entry with everything but the
272 static struct archive_acl_entry *
273 acl_new_entry(struct archive_acl *acl,
274 int type, int permset, int tag, int id)
276 struct archive_acl_entry *ap, *aq;
278 /* Type argument must be a valid NFS4 or POSIX.1e type.
279 * The type must agree with anything already set and
280 * the permset must be compatible. */
281 if (type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
282 if (acl->acl_types & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
286 ~(ARCHIVE_ENTRY_ACL_PERMS_NFS4
287 | ARCHIVE_ENTRY_ACL_INHERITANCE_NFS4)) {
290 } else if (type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
291 if (acl->acl_types & ~ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
294 if (permset & ~ARCHIVE_ENTRY_ACL_PERMS_POSIX1E) {
301 /* Verify the tag is valid and compatible with NFS4 or POSIX.1e. */
303 case ARCHIVE_ENTRY_ACL_USER:
304 case ARCHIVE_ENTRY_ACL_USER_OBJ:
305 case ARCHIVE_ENTRY_ACL_GROUP:
306 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
307 /* Tags valid in both NFS4 and POSIX.1e */
309 case ARCHIVE_ENTRY_ACL_MASK:
310 case ARCHIVE_ENTRY_ACL_OTHER:
311 /* Tags valid only in POSIX.1e. */
312 if (type & ~ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) {
316 case ARCHIVE_ENTRY_ACL_EVERYONE:
317 /* Tags valid only in NFS4. */
318 if (type & ~ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
323 /* No other values are valid. */
327 if (acl->acl_text_w != NULL) {
328 free(acl->acl_text_w);
329 acl->acl_text_w = NULL;
331 if (acl->acl_text != NULL) {
333 acl->acl_text = NULL;
337 * If there's a matching entry already in the list, overwrite it.
338 * NFSv4 entries may be repeated and are not overwritten.
340 * TODO: compare names of no id is provided (needs more rework)
345 if (((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) == 0) &&
346 ap->type == type && ap->tag == tag && ap->id == id) {
347 if (id != -1 || (tag != ARCHIVE_ENTRY_ACL_USER &&
348 tag != ARCHIVE_ENTRY_ACL_GROUP)) {
349 ap->permset = permset;
357 /* Add a new entry to the end of the list. */
358 ap = (struct archive_acl_entry *)calloc(1, sizeof(*ap));
368 ap->permset = permset;
369 acl->acl_types |= type;
374 * Return a count of entries matching "want_type".
377 archive_acl_count(struct archive_acl *acl, int want_type)
380 struct archive_acl_entry *ap;
385 if ((ap->type & want_type) != 0)
390 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
396 * Return a bitmask of stored ACL types in an ACL list
399 archive_acl_types(struct archive_acl *acl)
401 return (acl->acl_types);
405 * Prepare for reading entries from the ACL data. Returns a count
406 * of entries matching "want_type", or zero if there are no
407 * non-extended ACL entries of that type.
410 archive_acl_reset(struct archive_acl *acl, int want_type)
414 count = archive_acl_count(acl, want_type);
417 * If the only entries are the three standard ones,
418 * then don't return any ACL data. (In this case,
419 * client can just use chmod(2) to set permissions.)
421 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
427 acl->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
430 acl->acl_p = acl->acl_head;
436 * Return the next ACL entry in the list. Fake entries for the
437 * standard permissions and include them in the returned list.
440 archive_acl_next(struct archive *a, struct archive_acl *acl, int want_type,
441 int *type, int *permset, int *tag, int *id, const char **name)
447 * The acl_state is either zero (no entries available), -1
448 * (reading from list), or an entry type (retrieve that type
449 * from ae_stat.aest_mode).
451 if (acl->acl_state == 0)
452 return (ARCHIVE_WARN);
454 /* The first three access entries are special. */
455 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
456 switch (acl->acl_state) {
457 case ARCHIVE_ENTRY_ACL_USER_OBJ:
458 *permset = (acl->mode >> 6) & 7;
459 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
460 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
461 acl->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
463 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
464 *permset = (acl->mode >> 3) & 7;
465 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
466 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
467 acl->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
469 case ARCHIVE_ENTRY_ACL_OTHER:
470 *permset = acl->mode & 7;
471 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
472 *tag = ARCHIVE_ENTRY_ACL_OTHER;
474 acl->acl_p = acl->acl_head;
481 while (acl->acl_p != NULL && (acl->acl_p->type & want_type) == 0)
482 acl->acl_p = acl->acl_p->next;
483 if (acl->acl_p == NULL) {
490 return (ARCHIVE_EOF); /* End of ACL entries. */
492 *type = acl->acl_p->type;
493 *permset = acl->acl_p->permset;
494 *tag = acl->acl_p->tag;
495 *id = acl->acl_p->id;
496 if (archive_mstring_get_mbs(a, &acl->acl_p->name, name) != 0) {
498 return (ARCHIVE_FATAL);
501 acl->acl_p = acl->acl_p->next;
506 * Determine what type of ACL do we want
509 archive_acl_text_want_type(struct archive_acl *acl, int flags)
513 /* Check if ACL is NFSv4 */
514 if ((acl->acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
515 /* NFSv4 should never mix with POSIX.1e */
516 if ((acl->acl_types & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0)
519 return (ARCHIVE_ENTRY_ACL_TYPE_NFS4);
522 /* Now deal with POSIX.1e ACLs */
525 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
526 want_type |= ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
527 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
528 want_type |= ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
530 /* By default we want both access and default ACLs */
532 return (ARCHIVE_ENTRY_ACL_TYPE_POSIX1E);
538 * Calculate ACL text string length
541 archive_acl_text_len(struct archive_acl *acl, int want_type, int flags,
542 int wide, struct archive *a, struct archive_string_conv *sc) {
543 struct archive_acl_entry *ap;
545 const wchar_t *wname;
546 int count, idlen, tmp, r;
552 for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
553 if ((ap->type & want_type) == 0)
556 * Filemode-mapping ACL entries are stored exclusively in
557 * ap->mode so they should not be in the list
559 if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
560 && (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
561 || ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
562 || ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
565 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0
566 && (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
567 length += 8; /* "default:" */
569 case ARCHIVE_ENTRY_ACL_USER_OBJ:
570 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
571 length += 6; /* "owner@" */
575 case ARCHIVE_ENTRY_ACL_USER:
576 case ARCHIVE_ENTRY_ACL_MASK:
577 length += 4; /* "user", "mask" */
579 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
580 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
581 length += 6; /* "group@" */
585 case ARCHIVE_ENTRY_ACL_GROUP:
586 case ARCHIVE_ENTRY_ACL_OTHER:
587 length += 5; /* "group", "other" */
589 case ARCHIVE_ENTRY_ACL_EVERYONE:
590 length += 9; /* "everyone@" */
593 length += 1; /* colon after tag */
594 if (ap->tag == ARCHIVE_ENTRY_ACL_USER ||
595 ap->tag == ARCHIVE_ENTRY_ACL_GROUP) {
597 r = archive_mstring_get_wcs(a, &ap->name,
599 if (r == 0 && wname != NULL)
600 length += wcslen(wname);
601 else if (r < 0 && errno == ENOMEM)
604 length += sizeof(uid_t) * 3 + 1;
606 r = archive_mstring_get_mbs_l(&ap->name, &name,
610 if (len > 0 && name != NULL)
613 length += sizeof(uid_t) * 3 + 1;
615 length += 1; /* colon after user or group name */
616 } else if (want_type != ARCHIVE_ENTRY_ACL_TYPE_NFS4)
617 length += 1; /* 2nd colon empty user,group or other */
619 if (((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) != 0)
620 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0)
621 && (ap->tag == ARCHIVE_ENTRY_ACL_OTHER
622 || ap->tag == ARCHIVE_ENTRY_ACL_MASK)) {
623 /* Solaris has no colon after other: and mask: */
627 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
628 /* rwxpdDaARWcCos:fdinSFI:deny */
630 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DENY) == 0)
631 length += 1; /* allow, alarm, audit */
633 length += 3; /* rwx */
635 if ((ap->tag == ARCHIVE_ENTRY_ACL_USER ||
636 ap->tag == ARCHIVE_ENTRY_ACL_GROUP) &&
637 (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID) != 0) {
638 length += 1; /* colon */
648 length ++; /* entry separator */
651 /* Add filemode-mapping access entries to the length */
652 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
653 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) != 0) {
654 /* "user::rwx\ngroup::rwx\nother:rwx\n" */
657 /* "user::rwx\ngroup::rwx\nother::rwx\n" */
660 } else if (count == 0)
663 /* The terminating character is included in count */
668 * Generate a wide text version of the ACL. The flags parameter controls
669 * the type and style of the generated ACL.
672 archive_acl_to_text_w(struct archive_acl *acl, ssize_t *text_len, int flags,
678 const wchar_t *wname;
679 const wchar_t *prefix;
681 struct archive_acl_entry *ap;
682 int id, r, want_type;
685 want_type = archive_acl_text_want_type(acl, flags);
687 /* Both NFSv4 and POSIX.1 types found */
691 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_POSIX1E)
692 flags |= ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;
694 length = archive_acl_text_len(acl, want_type, flags, 1, a, NULL);
699 if (flags & ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA)
704 /* Now, allocate the string and actually populate it. */
705 wp = ws = (wchar_t *)malloc(length * sizeof(wchar_t));
708 __archive_errx(1, "No memory");
713 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
714 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
715 ARCHIVE_ENTRY_ACL_USER_OBJ, flags, NULL,
716 acl->mode & 0700, -1);
718 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
719 ARCHIVE_ENTRY_ACL_GROUP_OBJ, flags, NULL,
720 acl->mode & 0070, -1);
722 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
723 ARCHIVE_ENTRY_ACL_OTHER, flags, NULL,
724 acl->mode & 0007, -1);
728 for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
729 if ((ap->type & want_type) == 0)
732 * Filemode-mapping ACL entries are stored exclusively in
733 * ap->mode so they should not be in the list
735 if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
736 && (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
737 || ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
738 || ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
740 if (ap->type == ARCHIVE_ENTRY_ACL_TYPE_DEFAULT &&
741 (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
742 prefix = L"default:";
745 r = archive_mstring_get_wcs(a, &ap->name, &wname);
749 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
753 append_entry_w(&wp, prefix, ap->type, ap->tag, flags,
754 wname, ap->permset, id);
756 } else if (r < 0 && errno == ENOMEM) {
762 /* Add terminating character */
767 if ((ssize_t)len > (length - 1))
768 __archive_errx(1, "Buffer overrun");
770 if (text_len != NULL)
777 append_id_w(wchar_t **wp, int id)
782 append_id_w(wp, id / 10);
783 *(*wp)++ = L"0123456789"[id % 10];
787 append_entry_w(wchar_t **wp, const wchar_t *prefix, int type,
788 int tag, int flags, const wchar_t *wname, int perm, int id)
792 if (prefix != NULL) {
797 case ARCHIVE_ENTRY_ACL_USER_OBJ:
800 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
801 wcscpy(*wp, L"owner@");
805 case ARCHIVE_ENTRY_ACL_USER:
806 wcscpy(*wp, L"user");
808 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
811 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
812 wcscpy(*wp, L"group@");
816 case ARCHIVE_ENTRY_ACL_GROUP:
817 wcscpy(*wp, L"group");
819 case ARCHIVE_ENTRY_ACL_MASK:
820 wcscpy(*wp, L"mask");
824 case ARCHIVE_ENTRY_ACL_OTHER:
825 wcscpy(*wp, L"other");
829 case ARCHIVE_ENTRY_ACL_EVERYONE:
830 wcscpy(*wp, L"everyone@");
837 if (((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) ||
838 tag == ARCHIVE_ENTRY_ACL_USER ||
839 tag == ARCHIVE_ENTRY_ACL_GROUP) {
843 } else if (tag == ARCHIVE_ENTRY_ACL_USER
844 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
846 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) == 0)
849 /* Solaris style has no second colon after other and mask */
850 if (((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) == 0)
851 || (tag != ARCHIVE_ENTRY_ACL_OTHER
852 && tag != ARCHIVE_ENTRY_ACL_MASK))
855 if ((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
856 /* POSIX.1e ACL perms */
857 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
858 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
859 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
861 /* NFSv4 ACL perms */
862 for (i = 0; i < nfsv4_acl_perm_map_size; i++) {
863 if (perm & nfsv4_acl_perm_map[i].perm)
864 *(*wp)++ = nfsv4_acl_perm_map[i].wc;
865 else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
869 for (i = 0; i < nfsv4_acl_flag_map_size; i++) {
870 if (perm & nfsv4_acl_flag_map[i].perm)
871 *(*wp)++ = nfsv4_acl_flag_map[i].wc;
872 else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
877 case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
878 wcscpy(*wp, L"allow");
880 case ARCHIVE_ENTRY_ACL_TYPE_DENY:
881 wcscpy(*wp, L"deny");
883 case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
884 wcscpy(*wp, L"audit");
886 case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
887 wcscpy(*wp, L"alarm");
901 * Generate a text version of the ACL. The flags parameter controls
902 * the type and style of the generated ACL.
905 archive_acl_to_text_l(struct archive_acl *acl, ssize_t *text_len, int flags,
906 struct archive_string_conv *sc)
914 struct archive_acl_entry *ap;
915 int id, r, want_type;
918 want_type = archive_acl_text_want_type(acl, flags);
920 /* Both NFSv4 and POSIX.1 types found */
924 if (want_type == ARCHIVE_ENTRY_ACL_TYPE_POSIX1E)
925 flags |= ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;
927 length = archive_acl_text_len(acl, want_type, flags, 0, NULL, sc);
932 if (flags & ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA)
937 /* Now, allocate the string and actually populate it. */
938 p = s = (char *)malloc(length * sizeof(char));
941 __archive_errx(1, "No memory");
946 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
947 append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
948 ARCHIVE_ENTRY_ACL_USER_OBJ, flags, NULL,
949 acl->mode & 0700, -1);
951 append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
952 ARCHIVE_ENTRY_ACL_GROUP_OBJ, flags, NULL,
953 acl->mode & 0070, -1);
955 append_entry(&p, NULL, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
956 ARCHIVE_ENTRY_ACL_OTHER, flags, NULL,
957 acl->mode & 0007, -1);
961 for (ap = acl->acl_head; ap != NULL; ap = ap->next) {
962 if ((ap->type & want_type) == 0)
965 * Filemode-mapping ACL entries are stored exclusively in
966 * ap->mode so they should not be in the list
968 if ((ap->type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS)
969 && (ap->tag == ARCHIVE_ENTRY_ACL_USER_OBJ
970 || ap->tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ
971 || ap->tag == ARCHIVE_ENTRY_ACL_OTHER))
973 if (ap->type == ARCHIVE_ENTRY_ACL_TYPE_DEFAULT &&
974 (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
978 r = archive_mstring_get_mbs_l(
979 &ap->name, &name, &len, sc);
987 (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)) {
992 append_entry(&p, prefix, ap->type, ap->tag, flags, name,
997 /* Add terminating character */
1002 if ((ssize_t)len > (length - 1))
1003 __archive_errx(1, "Buffer overrun");
1005 if (text_len != NULL)
1012 append_id(char **p, int id)
1017 append_id(p, id / 10);
1018 *(*p)++ = "0123456789"[id % 10];
1022 append_entry(char **p, const char *prefix, int type,
1023 int tag, int flags, const char *name, int perm, int id)
1027 if (prefix != NULL) {
1032 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1035 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
1036 strcpy(*p, "owner@");
1040 case ARCHIVE_ENTRY_ACL_USER:
1043 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1046 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
1047 strcpy(*p, "group@");
1051 case ARCHIVE_ENTRY_ACL_GROUP:
1052 strcpy(*p, "group");
1054 case ARCHIVE_ENTRY_ACL_MASK:
1059 case ARCHIVE_ENTRY_ACL_OTHER:
1060 strcpy(*p, "other");
1064 case ARCHIVE_ENTRY_ACL_EVERYONE:
1065 strcpy(*p, "everyone@");
1072 if (((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) ||
1073 tag == ARCHIVE_ENTRY_ACL_USER ||
1074 tag == ARCHIVE_ENTRY_ACL_GROUP) {
1078 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1079 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1081 if ((type & ARCHIVE_ENTRY_ACL_TYPE_NFS4) == 0)
1084 /* Solaris style has no second colon after other and mask */
1085 if (((flags & ARCHIVE_ENTRY_ACL_STYLE_SOLARIS) == 0)
1086 || (tag != ARCHIVE_ENTRY_ACL_OTHER
1087 && tag != ARCHIVE_ENTRY_ACL_MASK))
1090 if ((type & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
1091 /* POSIX.1e ACL perms */
1092 *(*p)++ = (perm & 0444) ? 'r' : '-';
1093 *(*p)++ = (perm & 0222) ? 'w' : '-';
1094 *(*p)++ = (perm & 0111) ? 'x' : '-';
1096 /* NFSv4 ACL perms */
1097 for (i = 0; i < nfsv4_acl_perm_map_size; i++) {
1098 if (perm & nfsv4_acl_perm_map[i].perm)
1099 *(*p)++ = nfsv4_acl_perm_map[i].c;
1100 else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
1104 for (i = 0; i < nfsv4_acl_flag_map_size; i++) {
1105 if (perm & nfsv4_acl_flag_map[i].perm)
1106 *(*p)++ = nfsv4_acl_flag_map[i].c;
1107 else if ((flags & ARCHIVE_ENTRY_ACL_STYLE_COMPACT) == 0)
1112 case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
1113 strcpy(*p, "allow");
1115 case ARCHIVE_ENTRY_ACL_TYPE_DENY:
1118 case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
1119 strcpy(*p, "audit");
1121 case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
1122 strcpy(*p, "alarm");
1134 * Parse a wide ACL text string.
1136 * The want_type argument may be one of the following:
1137 * ARCHIVE_ENTRY_ACL_TYPE_ACCESS - text is a POSIX.1e ACL of type ACCESS
1138 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT - text is a POSIX.1e ACL of type DEFAULT
1139 * ARCHIVE_ENTRY_ACL_TYPE_NFS4 - text is as a NFSv4 ACL
1141 * POSIX.1e ACL entries prefixed with "default:" are treated as
1142 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT unless type is ARCHIVE_ENTRY_ACL_TYPE_NFS4
1145 archive_acl_from_text_w(struct archive_acl *acl, const wchar_t *text,
1149 const wchar_t *start;
1153 const wchar_t *s, *st;
1155 int numfields, fields, n, r, sol, ret;
1156 int type, types, tag, permset, id;
1163 switch (want_type) {
1164 case ARCHIVE_ENTRY_ACL_TYPE_POSIX1E:
1165 want_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1167 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
1168 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
1171 case ARCHIVE_ENTRY_ACL_TYPE_NFS4:
1175 return (ARCHIVE_FATAL);
1178 while (text != NULL && *text != L'\0') {
1180 * Parse the fields out of the next entry,
1181 * advance 'text' to start of next entry.
1185 const wchar_t *start, *end;
1186 next_field_w(&text, &start, &end, &sep);
1187 if (fields < numfields) {
1188 field[fields].start = start;
1189 field[fields].end = end;
1192 } while (sep == L':');
1194 /* Set remaining fields to blank. */
1195 for (n = fields; n < numfields; ++n)
1196 field[n].start = field[n].end = NULL;
1198 if (field[0].start != NULL && *(field[0].start) == L'#') {
1199 /* Comment, skip entry */
1207 name.start = name.end = NULL;
1209 if (want_type != ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
1212 * Default keyword "default:user::rwx"
1213 * if found, we have one more field
1215 * We also support old Solaris extension:
1216 * "defaultuser::rwx" is the default ACL corresponding
1217 * to "user::rwx", etc. valid only for first field
1220 len = field[0].end - field[0].start;
1221 if (*s == L'd' && (len == 1 || (len >= 7
1222 && wmemcmp((s + 1), L"efault", 6) == 0))) {
1223 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1225 field[0].start += 7;
1231 /* Check for a numeric ID in field n+1 or n+3. */
1232 isint_w(field[n + 1].start, field[n + 1].end, &id);
1233 /* Field n+3 is optional. */
1234 if (id == -1 && fields > n+3)
1235 isint_w(field[n + 3].start, field[n + 3].end,
1240 st = field[n].start + 1;
1241 len = field[n].end - field[n].start;
1245 if (len == 1 || (len == 4
1246 && wmemcmp(st, L"ser", 3) == 0))
1247 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1250 if (len == 1 || (len == 5
1251 && wmemcmp(st, L"roup", 4) == 0))
1252 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1255 if (len == 1 || (len == 5
1256 && wmemcmp(st, L"ther", 4) == 0))
1257 tag = ARCHIVE_ENTRY_ACL_OTHER;
1260 if (len == 1 || (len == 4
1261 && wmemcmp(st, L"ask", 3) == 0))
1262 tag = ARCHIVE_ENTRY_ACL_MASK;
1269 case ARCHIVE_ENTRY_ACL_OTHER:
1270 case ARCHIVE_ENTRY_ACL_MASK:
1271 if (fields == (n + 2)
1272 && field[n + 1].start < field[n + 1].end
1273 && ismode_w(field[n + 1].start,
1274 field[n + 1].end, &permset)) {
1275 /* This is Solaris-style "other:rwx" */
1277 } else if (fields == (n + 3) &&
1278 field[n + 1].start < field[n + 1].end) {
1279 /* Invalid mask or other field */
1284 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1285 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1287 field[n + 1].start < field[n + 1].end) {
1288 name = field[n + 1];
1289 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
1290 tag = ARCHIVE_ENTRY_ACL_USER;
1292 tag = ARCHIVE_ENTRY_ACL_GROUP;
1296 /* Invalid tag, skip entry */
1302 * Without "default:" we expect mode in field 2
1303 * Exception: Solaris other and mask fields
1305 if (permset == 0 && !ismode_w(field[n + 2 - sol].start,
1306 field[n + 2 - sol].end, &permset)) {
1307 /* Invalid mode, skip entry */
1314 len = field[0].end - field[0].start;
1319 if (wmemcmp(s, L"user", 4) == 0)
1320 tag = ARCHIVE_ENTRY_ACL_USER;
1323 if (wmemcmp(s, L"group", 5) == 0)
1324 tag = ARCHIVE_ENTRY_ACL_GROUP;
1327 if (wmemcmp(s, L"owner@", 6) == 0)
1328 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1329 else if (wmemcmp(s, L"group@", len) == 0)
1330 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1333 if (wmemcmp(s, L"everyone@", 9) == 0)
1334 tag = ARCHIVE_ENTRY_ACL_EVERYONE;
1340 /* Invalid tag, skip entry */
1343 } else if (tag == ARCHIVE_ENTRY_ACL_USER ||
1344 tag == ARCHIVE_ENTRY_ACL_GROUP) {
1347 isint_w(name.start, name.end, &id);
1351 if (!is_nfs4_perms_w(field[1 + n].start,
1352 field[1 + n].end, &permset)) {
1353 /* Invalid NFSv4 perms, skip entry */
1357 if (!is_nfs4_flags_w(field[2 + n].start,
1358 field[2 + n].end, &permset)) {
1359 /* Invalid NFSv4 flags, skip entry */
1363 s = field[3 + n].start;
1364 len = field[3 + n].end - field[3 + n].start;
1367 if (wmemcmp(s, L"deny", 4) == 0)
1368 type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
1369 } else if (len == 5) {
1370 if (wmemcmp(s, L"allow", 5) == 0)
1371 type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
1372 else if (wmemcmp(s, L"audit", 5) == 0)
1373 type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
1374 else if (wmemcmp(s, L"alarm", 5) == 0)
1375 type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
1378 /* Invalid entry type, skip entry */
1382 isint_w(field[4 + n].start, field[4 + n].end, &id);
1385 /* Add entry to the internal list. */
1386 r = archive_acl_add_entry_w_len(acl, type, permset,
1387 tag, id, name.start, name.end - name.start);
1388 if (r < ARCHIVE_WARN)
1390 if (r != ARCHIVE_OK)
1396 archive_acl_reset(acl, types);
1402 * Parse a string to a positive decimal integer. Returns true if
1403 * the string is non-empty and consists only of decimal digits,
1407 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1412 while (start < end) {
1413 if (*start < '0' || *start > '9')
1415 if (n > (INT_MAX / 10) ||
1416 (n == INT_MAX / 10 && (*start - '0') > INT_MAX % 10)) {
1429 * Parse a string as a mode field. Returns true if
1430 * the string is non-empty and consists only of mode characters,
1434 ismode_w(const wchar_t *start, const wchar_t *end, int *permset)
1444 case L'r': case L'R':
1445 *permset |= ARCHIVE_ENTRY_ACL_READ;
1447 case L'w': case L'W':
1448 *permset |= ARCHIVE_ENTRY_ACL_WRITE;
1450 case L'x': case L'X':
1451 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1463 * Parse a string as a NFS4 ACL permission field.
1464 * Returns true if the string is non-empty and consists only of NFS4 ACL
1465 * permission characters, false otherwise
1468 is_nfs4_perms_w(const wchar_t *start, const wchar_t *end, int *permset)
1470 const wchar_t *p = start;
1475 *permset |= ARCHIVE_ENTRY_ACL_READ_DATA;
1478 *permset |= ARCHIVE_ENTRY_ACL_WRITE_DATA;
1481 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1484 *permset |= ARCHIVE_ENTRY_ACL_APPEND_DATA;
1487 *permset |= ARCHIVE_ENTRY_ACL_DELETE_CHILD;
1490 *permset |= ARCHIVE_ENTRY_ACL_DELETE;
1493 *permset |= ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES;
1496 *permset |= ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES;
1499 *permset |= ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS;
1502 *permset |= ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS;
1505 *permset |= ARCHIVE_ENTRY_ACL_READ_ACL;
1508 *permset |= ARCHIVE_ENTRY_ACL_WRITE_ACL;
1511 *permset |= ARCHIVE_ENTRY_ACL_WRITE_OWNER;
1514 *permset |= ARCHIVE_ENTRY_ACL_SYNCHRONIZE;
1526 * Parse a string as a NFS4 ACL flags field.
1527 * Returns true if the string is non-empty and consists only of NFS4 ACL
1528 * flag characters, false otherwise
1531 is_nfs4_flags_w(const wchar_t *start, const wchar_t *end, int *permset)
1533 const wchar_t *p = start;
1538 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT;
1541 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT;
1544 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY;
1548 ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT;
1551 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS;
1554 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS;
1557 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_INHERITED;
1569 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1570 * to point to just after the separator. *start points to the first
1571 * character of the matched text and *end just after the last
1572 * character of the matched identifier. In particular *end - *start
1573 * is the length of the field body, not including leading or trailing
1577 next_field_w(const wchar_t **wp, const wchar_t **start,
1578 const wchar_t **end, wchar_t *sep)
1580 /* Skip leading whitespace to find start of field. */
1581 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1586 /* Scan for the separator. */
1587 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1588 **wp != L'\n' && **wp != L'#') {
1593 /* Locate end of field, trim trailing whitespace if necessary */
1594 if (*wp == *start) {
1598 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1604 /* Handle in-field comments */
1606 while (**wp != L'\0' && **wp != L',' && **wp != L'\n') {
1612 /* Adjust scanner location. */
1618 * Parse an ACL text string.
1620 * The want_type argument may be one of the following:
1621 * ARCHIVE_ENTRY_ACL_TYPE_ACCESS - text is a POSIX.1e ACL of type ACCESS
1622 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT - text is a POSIX.1e ACL of type DEFAULT
1623 * ARCHIVE_ENTRY_ACL_TYPE_NFS4 - text is as a NFSv4 ACL
1625 * POSIX.1e ACL entries prefixed with "default:" are treated as
1626 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT unless type is ARCHIVE_ENTRY_ACL_TYPE_NFS4
1629 archive_acl_from_text_l(struct archive_acl *acl, const char *text,
1630 int want_type, struct archive_string_conv *sc)
1638 int numfields, fields, n, r, sol, ret;
1639 int type, types, tag, permset, id;
1643 switch (want_type) {
1644 case ARCHIVE_ENTRY_ACL_TYPE_POSIX1E:
1645 want_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1647 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
1648 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
1651 case ARCHIVE_ENTRY_ACL_TYPE_NFS4:
1655 return (ARCHIVE_FATAL);
1661 while (text != NULL && *text != '\0') {
1663 * Parse the fields out of the next entry,
1664 * advance 'text' to start of next entry.
1668 const char *start, *end;
1669 next_field(&text, &start, &end, &sep);
1670 if (fields < numfields) {
1671 field[fields].start = start;
1672 field[fields].end = end;
1675 } while (sep == ':');
1677 /* Set remaining fields to blank. */
1678 for (n = fields; n < numfields; ++n)
1679 field[n].start = field[n].end = NULL;
1681 if (field[0].start != NULL && *(field[0].start) == '#') {
1682 /* Comment, skip entry */
1690 name.start = name.end = NULL;
1692 if (want_type != ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
1695 * Default keyword "default:user::rwx"
1696 * if found, we have one more field
1698 * We also support old Solaris extension:
1699 * "defaultuser::rwx" is the default ACL corresponding
1700 * to "user::rwx", etc. valid only for first field
1703 len = field[0].end - field[0].start;
1704 if (*s == 'd' && (len == 1 || (len >= 7
1705 && memcmp((s + 1), "efault", 6) == 0))) {
1706 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1708 field[0].start += 7;
1714 /* Check for a numeric ID in field n+1 or n+3. */
1715 isint(field[n + 1].start, field[n + 1].end, &id);
1716 /* Field n+3 is optional. */
1717 if (id == -1 && fields > (n + 3))
1718 isint(field[n + 3].start, field[n + 3].end,
1723 st = field[n].start + 1;
1724 len = field[n].end - field[n].start;
1733 if (len == 1 || (len == 4
1734 && memcmp(st, "ser", 3) == 0))
1735 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1738 if (len == 1 || (len == 5
1739 && memcmp(st, "roup", 4) == 0))
1740 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1743 if (len == 1 || (len == 5
1744 && memcmp(st, "ther", 4) == 0))
1745 tag = ARCHIVE_ENTRY_ACL_OTHER;
1748 if (len == 1 || (len == 4
1749 && memcmp(st, "ask", 3) == 0))
1750 tag = ARCHIVE_ENTRY_ACL_MASK;
1757 case ARCHIVE_ENTRY_ACL_OTHER:
1758 case ARCHIVE_ENTRY_ACL_MASK:
1759 if (fields == (n + 2)
1760 && field[n + 1].start < field[n + 1].end
1761 && ismode(field[n + 1].start,
1762 field[n + 1].end, &permset)) {
1763 /* This is Solaris-style "other:rwx" */
1765 } else if (fields == (n + 3) &&
1766 field[n + 1].start < field[n + 1].end) {
1767 /* Invalid mask or other field */
1772 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1773 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1775 field[n + 1].start < field[n + 1].end) {
1776 name = field[n + 1];
1777 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
1778 tag = ARCHIVE_ENTRY_ACL_USER;
1780 tag = ARCHIVE_ENTRY_ACL_GROUP;
1784 /* Invalid tag, skip entry */
1790 * Without "default:" we expect mode in field 3
1791 * Exception: Solaris other and mask fields
1793 if (permset == 0 && !ismode(field[n + 2 - sol].start,
1794 field[n + 2 - sol].end, &permset)) {
1795 /* Invalid mode, skip entry */
1802 len = field[0].end - field[0].start;
1807 if (memcmp(s, "user", 4) == 0)
1808 tag = ARCHIVE_ENTRY_ACL_USER;
1811 if (memcmp(s, "group", 5) == 0)
1812 tag = ARCHIVE_ENTRY_ACL_GROUP;
1815 if (memcmp(s, "owner@", 6) == 0)
1816 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1817 else if (memcmp(s, "group@", 6) == 0)
1818 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1821 if (memcmp(s, "everyone@", 9) == 0)
1822 tag = ARCHIVE_ENTRY_ACL_EVERYONE;
1829 /* Invalid tag, skip entry */
1832 } else if (tag == ARCHIVE_ENTRY_ACL_USER ||
1833 tag == ARCHIVE_ENTRY_ACL_GROUP) {
1836 isint(name.start, name.end, &id);
1840 if (!is_nfs4_perms(field[1 + n].start,
1841 field[1 + n].end, &permset)) {
1842 /* Invalid NFSv4 perms, skip entry */
1846 if (!is_nfs4_flags(field[2 + n].start,
1847 field[2 + n].end, &permset)) {
1848 /* Invalid NFSv4 flags, skip entry */
1852 s = field[3 + n].start;
1853 len = field[3 + n].end - field[3 + n].start;
1856 if (memcmp(s, "deny", 4) == 0)
1857 type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
1858 } else if (len == 5) {
1859 if (memcmp(s, "allow", 5) == 0)
1860 type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
1861 else if (memcmp(s, "audit", 5) == 0)
1862 type = ARCHIVE_ENTRY_ACL_TYPE_AUDIT;
1863 else if (memcmp(s, "alarm", 5) == 0)
1864 type = ARCHIVE_ENTRY_ACL_TYPE_ALARM;
1867 /* Invalid entry type, skip entry */
1871 isint(field[4 + n].start, field[4 + n].end,
1875 /* Add entry to the internal list. */
1876 r = archive_acl_add_entry_len_l(acl, type, permset,
1877 tag, id, name.start, name.end - name.start, sc);
1878 if (r < ARCHIVE_WARN)
1880 if (r != ARCHIVE_OK)
1886 archive_acl_reset(acl, types);
1892 * Parse a string to a positive decimal integer. Returns true if
1893 * the string is non-empty and consists only of decimal digits,
1897 isint(const char *start, const char *end, int *result)
1902 while (start < end) {
1903 if (*start < '0' || *start > '9')
1905 if (n > (INT_MAX / 10) ||
1906 (n == INT_MAX / 10 && (*start - '0') > INT_MAX % 10)) {
1919 * Parse a string as a mode field. Returns true if
1920 * the string is non-empty and consists only of mode characters,
1924 ismode(const char *start, const char *end, int *permset)
1935 *permset |= ARCHIVE_ENTRY_ACL_READ;
1938 *permset |= ARCHIVE_ENTRY_ACL_WRITE;
1941 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1953 * Parse a string as a NFS4 ACL permission field.
1954 * Returns true if the string is non-empty and consists only of NFS4 ACL
1955 * permission characters, false otherwise
1958 is_nfs4_perms(const char *start, const char *end, int *permset)
1960 const char *p = start;
1965 *permset |= ARCHIVE_ENTRY_ACL_READ_DATA;
1968 *permset |= ARCHIVE_ENTRY_ACL_WRITE_DATA;
1971 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1974 *permset |= ARCHIVE_ENTRY_ACL_APPEND_DATA;
1977 *permset |= ARCHIVE_ENTRY_ACL_DELETE_CHILD;
1980 *permset |= ARCHIVE_ENTRY_ACL_DELETE;
1983 *permset |= ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES;
1986 *permset |= ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES;
1989 *permset |= ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS;
1992 *permset |= ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS;
1995 *permset |= ARCHIVE_ENTRY_ACL_READ_ACL;
1998 *permset |= ARCHIVE_ENTRY_ACL_WRITE_ACL;
2001 *permset |= ARCHIVE_ENTRY_ACL_WRITE_OWNER;
2004 *permset |= ARCHIVE_ENTRY_ACL_SYNCHRONIZE;
2016 * Parse a string as a NFS4 ACL flags field.
2017 * Returns true if the string is non-empty and consists only of NFS4 ACL
2018 * flag characters, false otherwise
2021 is_nfs4_flags(const char *start, const char *end, int *permset)
2023 const char *p = start;
2028 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT;
2031 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT;
2034 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY;
2038 ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT;
2041 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_SUCCESSFUL_ACCESS;
2044 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_FAILED_ACCESS;
2047 *permset |= ARCHIVE_ENTRY_ACL_ENTRY_INHERITED;
2059 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
2060 * to point to just after the separator. *start points to the first
2061 * character of the matched text and *end just after the last
2062 * character of the matched identifier. In particular *end - *start
2063 * is the length of the field body, not including leading or trailing
2067 next_field(const char **p, const char **start,
2068 const char **end, char *sep)
2070 /* Skip leading whitespace to find start of field. */
2071 while (**p == ' ' || **p == '\t' || **p == '\n') {
2076 /* Scan for the separator. */
2077 while (**p != '\0' && **p != ',' && **p != ':' && **p != '\n' &&
2083 /* Locate end of field, trim trailing whitespace if necessary */
2088 while (**end == ' ' || **end == '\t' || **end == '\n') {
2094 /* Handle in-field comments */
2096 while (**p != '\0' && **p != ',' && **p != '\n') {
2102 /* Adjust scanner location. */