2 * Copyright (c) 2003-2008 Tim Kientzle
3 * Copyright (c) 2017 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 __FBSDID("$FreeBSD: head/lib/libarchive/test/test_acl_freebsd.c 189427 2009-03-06 04:21:23Z kientzle $");
29 #if HAVE_POSIX_ACL || HAVE_SUN_ACL
32 #include <acl/libacl.h>
33 #define ACL_GET_PERM acl_get_perm
34 #elif HAVE_ACL_GET_PERM_NP
35 #define ACL_GET_PERM acl_get_perm_np
38 static struct archive_test_acl_t acls2[] = {
39 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE | ARCHIVE_ENTRY_ACL_READ,
40 ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" },
41 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
42 ARCHIVE_ENTRY_ACL_USER, 77, "user77" },
43 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0,
44 ARCHIVE_ENTRY_ACL_USER, 78, "user78" },
45 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
46 ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" },
47 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0007,
48 ARCHIVE_ENTRY_ACL_GROUP, 78, "group78" },
49 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
50 ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_EXECUTE,
51 ARCHIVE_ENTRY_ACL_OTHER, -1, "" },
52 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
53 ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_READ | ARCHIVE_ENTRY_ACL_EXECUTE,
54 ARCHIVE_ENTRY_ACL_MASK, -1, "" },
59 acl_entry_get_perm(aclent_t *aclent)
61 acl_entry_get_perm(acl_entry_t aclent)
66 acl_permset_t opaque_ps;
70 if (aclent->a_perm & 1)
71 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
72 if (aclent->a_perm & 2)
73 permset |= ARCHIVE_ENTRY_ACL_WRITE;
74 if (aclent->a_perm & 4)
75 permset |= ARCHIVE_ENTRY_ACL_READ;
77 /* translate the silly opaque permset to a bitmap */
78 acl_get_permset(aclent, &opaque_ps);
79 if (ACL_GET_PERM(opaque_ps, ACL_EXECUTE))
80 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
81 if (ACL_GET_PERM(opaque_ps, ACL_WRITE))
82 permset |= ARCHIVE_ENTRY_ACL_WRITE;
83 if (ACL_GET_PERM(opaque_ps, ACL_READ))
84 permset |= ARCHIVE_ENTRY_ACL_READ;
91 acl_get_specific_entry(acl_t acl, acl_tag_t requested_tag_type, int requested_tag) {
92 int entry_id = ACL_FIRST_ENTRY;
93 acl_entry_t acl_entry;
94 acl_tag_t acl_tag_type;
96 while (1 == acl_get_entry(acl, entry_id, &acl_entry)) {
97 /* After the first time... */
98 entry_id = ACL_NEXT_ENTRY;
100 /* If this matches, return perm mask */
101 acl_get_tag_type(acl_entry, &acl_tag_type);
102 if (acl_tag_type == requested_tag_type) {
103 switch (acl_tag_type) {
105 if ((uid_t)requested_tag == *(uid_t *)(acl_get_qualifier(acl_entry))) {
106 return acl_entry_get_perm(acl_entry);
110 if ((gid_t)requested_tag == *(gid_t *)(acl_get_qualifier(acl_entry))) {
111 return acl_entry_get_perm(acl_entry);
117 return acl_entry_get_perm(acl_entry);
119 failure("Unexpected ACL tag type");
132 acl_match(aclent_t *aclent, struct archive_test_acl_t *myacl)
134 acl_match(acl_entry_t aclent, struct archive_test_acl_t *myacl)
143 if (myacl->permset != acl_entry_get_perm(aclent))
147 switch (aclent->a_type)
149 acl_get_tag_type(aclent, &tag_type);
159 if (myacl->tag != ARCHIVE_ENTRY_ACL_USER_OBJ) return (0);
167 if (myacl->tag != ARCHIVE_ENTRY_ACL_USER)
170 if ((uid_t)myacl->qual != aclent->a_id)
173 up = acl_get_qualifier(aclent);
176 if ((uid_t)myacl->qual != u)
186 if (myacl->tag != ARCHIVE_ENTRY_ACL_GROUP_OBJ) return (0);
194 if (myacl->tag != ARCHIVE_ENTRY_ACL_GROUP)
197 if ((gid_t)myacl->qual != aclent->a_id)
200 gp = acl_get_qualifier(aclent);
203 if ((gid_t)myacl->qual != g)
213 if (myacl->tag != ARCHIVE_ENTRY_ACL_MASK) return (0);
221 if (myacl->tag != ARCHIVE_ENTRY_ACL_OTHER) return (0);
229 compare_acls(acl_t *acl, struct archive_test_acl_t *myacls, int n)
231 compare_acls(acl_t acl, struct archive_test_acl_t *myacls, int n)
241 int entry_id = ACL_FIRST_ENTRY;
242 acl_entry_t acl_entry;
245 /* Count ACL entries in myacls array and allocate an indirect array. */
246 marker = malloc(sizeof(marker[0]) * n);
249 for (i = 0; i < n; i++)
253 * Iterate over acls in system acl object, try to match each
254 * one with an item in the myacls array.
257 for(e = 0; e < acl->acl_cnt; e++) {
258 acl_entry = &((aclent_t *)acl->acl_aclp)[e];
260 while (1 == acl_get_entry(acl, entry_id, &acl_entry)) {
261 /* After the first time... */
262 entry_id = ACL_NEXT_ENTRY;
265 /* Search for a matching entry (tag and qualifier) */
266 for (i = 0, matched = 0; i < n && !matched; i++) {
267 if (acl_match(acl_entry, &myacls[marker[i]])) {
268 /* We found a match; remove it. */
269 marker[i] = marker[n - 1];
275 /* TODO: Print out more details in this case. */
276 failure("ACL entry on file that shouldn't be there");
277 assert(matched == 1);
280 /* Dump entries in the myacls array that weren't in the system acl. */
281 for (i = 0; i < n; ++i) {
282 failure(" ACL entry missing from file: "
283 "type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s''\n",
284 myacls[marker[i]].type, myacls[marker[i]].permset,
285 myacls[marker[i]].tag, myacls[marker[i]].qual,
286 myacls[marker[i]].name);
287 assert(0); /* Record this as a failure. */
296 * Verify ACL restore-to-disk. This test is Platform-specific.
299 DEFINE_TEST(test_acl_platform_posix1e_restore)
301 #if !HAVE_SUN_ACL && !HAVE_POSIX_ACL
302 skipping("POSIX.1e ACLs are not supported on this platform");
303 #else /* HAVE_SUN_ACL || HAVE_POSIX_ACL */
306 struct archive_entry *ae;
316 * First, do a quick manual set/read of ACL data to
317 * verify that the local filesystem does support ACLs.
318 * If it doesn't, we'll simply skip the remaining tests.
321 n = acl_fromtext("user::rwx,user:1:rw-,group::rwx,group:15:r-x,other:rwx,mask:rwx", &acl);
322 failure("acl_fromtext(): errno = %d (%s)", errno, strerror(errno));
323 assertEqualInt(0, n);
325 acl = acl_from_text("u::rwx,u:1:rw,g::rwx,g:15:rx,o::rwx,m::rwx");
326 failure("acl_from_text(): errno = %d (%s)", errno, strerror(errno));
327 assert((void *)acl != NULL);
330 /* Create a test file and try ACL on it. */
331 fd = open("pretest", O_WRONLY | O_CREAT | O_EXCL, 0777);
332 failure("Could not create test file?!");
333 if (!assert(fd >= 0)) {
339 n = facl_get(fd, 0, &acl2);
344 if (errno == ENOSYS) {
345 skipping("POSIX.1e ACLs are not supported on this filesystem");
348 failure("facl_get(): errno = %d (%s)", errno, strerror(errno));
349 assertEqualInt(0, n);
351 if (acl2->acl_type != ACLENT_T) {
353 skipping("POSIX.1e ACLs are not supported on this filesystem");
359 n = facl_set(fd, acl);
361 func = "acl_set_fd()";
362 n = acl_set_fd(fd, acl);
369 if (errno == EOPNOTSUPP || errno == EINVAL)
373 skipping("POSIX.1e ACLs are not supported on this filesystem");
377 failure("%s: errno = %d (%s)", func, errno, strerror(errno));
378 assertEqualInt(0, n);
385 /* Create a write-to-disk object. */
386 assert(NULL != (a = archive_write_disk_new()));
387 archive_write_disk_set_options(a,
388 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL);
390 /* Populate an archive entry with some metadata, including ACL info */
391 ae = archive_entry_new();
393 archive_entry_set_pathname(ae, "test0");
394 archive_entry_set_mtime(ae, 123456, 7890);
395 archive_entry_set_size(ae, 0);
396 archive_test_set_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0]));
397 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
398 archive_entry_free(ae);
400 /* Close the archive. */
401 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
402 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
404 /* Verify the data on disk. */
405 assertEqualInt(0, stat("test0", &st));
406 assertEqualInt(st.st_mtime, 123456);
408 n = acl_get("test0", 0, &acl);
409 failure("acl_get(): errno = %d (%s)", errno, strerror(errno));
410 assertEqualInt(0, n);
412 acl = acl_get_file("test0", ACL_TYPE_ACCESS);
413 failure("acl_get_file(): errno = %d (%s)", errno, strerror(errno));
414 assert(acl != (acl_t)NULL);
416 compare_acls(acl, acls2, sizeof(acls2)/sizeof(acls2[0]));
418 #endif /* HAVE_SUN_ACL || HAVE_POSIX_ACL */
422 * Verify ACL read-from-disk. This test is Platform-specific.
424 DEFINE_TEST(test_acl_platform_posix1e_read)
426 #if !HAVE_SUN_ACL && !HAVE_POSIX_ACL
427 skipping("POSIX.1e ACLs are not supported on this platform");
430 struct archive_entry *ae;
431 int n, fd, flags, dflags;
432 char *func, *acl_text;
433 const char *acl1_text, *acl2_text, *acl3_text;
435 acl_t *acl, *acl1, *acl2, *acl3;
437 acl_t acl1, acl2, acl3;
441 * Manually construct a directory and two files with
442 * different ACLs. This also serves to verify that ACLs
443 * are supported on the local filesystem.
446 /* Create a test file f1 with acl1 */
448 acl1_text = "user::rwx,"
454 n = acl_fromtext(acl1_text, &acl1);
455 failure("acl_fromtext(): errno = %d (%s)", errno, strerror(errno));
456 assertEqualInt(0, n);
458 acl1_text = "user::rwx\n"
464 acl1 = acl_from_text(acl1_text);
465 failure("acl_from_text(): errno = %d (%s)", errno, strerror(errno));
466 assert((void *)acl1 != NULL);
468 fd = open("f1", O_WRONLY | O_CREAT | O_EXCL, 0777);
469 failure("Could not create test file?!");
470 if (!assert(fd >= 0)) {
475 /* Check if Solars filesystem supports POSIX.1e ACLs */
476 n = facl_get(fd, 0, &acl);
479 if (n != 0 && errno == ENOSYS) {
481 skipping("POSIX.1e ACLs are not supported on this filesystem");
484 failure("facl_get(): errno = %d (%s)", errno, strerror(errno));
485 assertEqualInt(0, n);
487 if (acl->acl_type != ACLENT_T) {
491 skipping("POSIX.1e ACLs are not supported on this filesystem");
496 n = facl_set(fd, acl1);
498 func = "acl_set_fd()";
499 n = acl_set_fd(fd, acl1);
507 if (errno == EOPNOTSUPP || errno == EINVAL)
511 skipping("POSIX.1e ACLs are not supported on this filesystem");
515 failure("%s: errno = %d (%s)", func, errno, strerror(errno));
516 assertEqualInt(0, n);
520 assertMakeDir("d", 0700);
523 * Create file d/f1 with acl2
525 * This differs from acl1 in the u:1: and g:15: permissions.
527 * This file deliberately has the same name but a different ACL.
528 * Github Issue #777 explains how libarchive's directory traversal
529 * did not always correctly enter directories before attempting
530 * to read ACLs, resulting in reading the ACL from a like-named
531 * file in the wrong directory.
534 acl2_text = "user::rwx,"
540 n = acl_fromtext(acl2_text, &acl2);
541 failure("acl_fromtext(): errno = %d (%s)", errno, strerror(errno));
542 assertEqualInt(0, n);
544 acl2_text = "user::rwx\n"
550 acl2 = acl_from_text(acl2_text);
551 failure("acl_from_text(): errno = %d (%s)", errno, strerror(errno));
552 assert((void *)acl2 != NULL);
554 fd = open("d/f1", O_WRONLY | O_CREAT | O_EXCL, 0777);
555 failure("Could not create test file?!");
556 if (!assert(fd >= 0)) {
562 n = facl_set(fd, acl2);
564 func = "acl_set_fd()";
565 n = acl_set_fd(fd, acl2);
570 failure("%s: errno = %d (%s)", func, errno, strerror(errno));
571 assertEqualInt(0, n);
574 /* Create directory d2 with default ACLs */
575 assertMakeDir("d2", 0755);
578 acl3_text = "user::rwx,"
585 "default:user:1:r--,"
586 "default:group::r-x,"
587 "default:group:15:r--,"
590 n = acl_fromtext(acl3_text, &acl3);
591 failure("acl_fromtext(): errno = %d (%s)", errno, strerror(errno));
592 assertEqualInt(0, n);
594 acl3_text = "user::rwx\n"
600 acl3 = acl_from_text(acl3_text);
601 failure("acl_from_text(): errno = %d (%s)", errno, strerror(errno));
602 assert((void *)acl3 != NULL);
607 n = acl_set("d2", acl3);
609 func = "acl_set_file()";
610 n = acl_set_file("d2", ACL_TYPE_DEFAULT, acl3);
614 failure("%s: errno = %d (%s)", func, errno, strerror(errno));
615 assertEqualInt(0, n);
617 /* Create a read-from-disk object. */
618 assert(NULL != (a = archive_read_disk_new()));
619 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, "."));
620 assert(NULL != (ae = archive_entry_new()));
623 flags = ARCHIVE_ENTRY_ACL_TYPE_POSIX1E
624 | ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA
625 | ARCHIVE_ENTRY_ACL_STYLE_SOLARIS;
628 flags = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
629 dflags = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
632 /* Walk the dir until we see both of the files */
633 while (ARCHIVE_OK == archive_read_next_header2(a, ae)) {
634 archive_read_disk_descend(a);
635 if (strcmp(archive_entry_pathname(ae), "./f1") == 0) {
636 acl_text = archive_entry_acl_to_text(ae, NULL, flags);
637 assertEqualString(acl_text, acl1_text);
639 } else if (strcmp(archive_entry_pathname(ae), "./d/f1") == 0) {
640 acl_text = archive_entry_acl_to_text(ae, NULL, flags);
641 assertEqualString(acl_text, acl2_text);
643 } else if (strcmp(archive_entry_pathname(ae), "./d2") == 0) {
644 acl_text = archive_entry_acl_to_text(ae, NULL, dflags);
645 assertEqualString(acl_text, acl3_text);
650 archive_entry_free(ae);
651 assertEqualInt(ARCHIVE_OK, archive_free(a));