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(void *aclp, int aclcnt, 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 < aclcnt; e++) {
258 acl_entry = &((aclent_t *)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;
314 assertMakeFile("pretest", 0644, "a");
316 if (setTestAcl("pretest") != ARCHIVE_TEST_ACL_TYPE_POSIX1E) {
317 skipping("POSIX.1e ACLs are not writable on this filesystem");
321 /* Create a write-to-disk object. */
322 assert(NULL != (a = archive_write_disk_new()));
323 archive_write_disk_set_options(a,
324 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL);
326 /* Populate an archive entry with some metadata, including ACL info */
327 ae = archive_entry_new();
329 archive_entry_set_pathname(ae, "test0");
330 archive_entry_set_mtime(ae, 123456, 7890);
331 archive_entry_set_size(ae, 0);
332 assertEntrySetAcls(ae, acls2, sizeof(acls2)/sizeof(acls2[0]));
333 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
334 archive_entry_free(ae);
336 /* Close the archive. */
337 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
338 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
340 /* Verify the data on disk. */
341 assertEqualInt(0, stat("test0", &st));
342 assertEqualInt(st.st_mtime, 123456);
344 aclp = sunacl_get(GETACL, &aclcnt, 0, "test0");
345 failure("acl(): errno = %d (%s)", errno, strerror(errno));
346 assert(aclp != NULL);
348 acl = acl_get_file("test0", ACL_TYPE_ACCESS);
349 failure("acl_get_file(): errno = %d (%s)", errno, strerror(errno));
350 assert(acl != (acl_t)NULL);
353 compare_acls(aclp, aclcnt, acls2, sizeof(acls2)/sizeof(acls2[0]));
357 compare_acls(acl, acls2, sizeof(acls2)/sizeof(acls2[0]));
361 #endif /* HAVE_SUN_ACL || HAVE_POSIX_ACL */
365 * Verify ACL read-from-disk. This test is Platform-specific.
367 DEFINE_TEST(test_acl_platform_posix1e_read)
369 #if !HAVE_SUN_ACL && !HAVE_POSIX_ACL
370 skipping("POSIX.1e ACLs are not supported on this platform");
373 struct archive_entry *ae;
374 int n, fd, flags, dflags;
375 char *func, *acl_text;
376 const char *acl1_text, *acl2_text, *acl3_text;
381 acl_t acl1, acl2, acl3;
385 * Manually construct a directory and two files with
386 * different ACLs. This also serves to verify that ACLs
387 * are supported on the local filesystem.
390 /* Create a test file f1 with acl1 */
392 acl1_text = "user::rwx,"
399 { USER_OBJ, -1, 4 | 2 | 1 },
401 { GROUP_OBJ, -1, 4 | 2 | 1 },
402 { GROUP, 15, 4 | 1 },
403 { CLASS_OBJ, -1, 4 | 2 | 1 },
404 { OTHER_OBJ, -1, 4 | 2 | 1 }
407 acl1_text = "user::rwx\n"
413 acl1 = acl_from_text(acl1_text);
414 failure("acl_from_text(): errno = %d (%s)", errno, strerror(errno));
415 assert((void *)acl1 != NULL);
417 fd = open("f1", O_WRONLY | O_CREAT | O_EXCL, 0777);
418 failure("Could not create test file?!");
419 if (!assert(fd >= 0)) {
426 /* Check if Solaris filesystem supports POSIX.1e ACLs */
427 aclp = sunacl_get(GETACL, &aclcnt, fd, NULL);
430 if (errno == ENOSYS || errno == ENOTSUP) {
431 skipping("POSIX.1e ACLs are not supported on this filesystem");
434 failure("facl(): errno = %d (%s)", errno, strerror(errno));
435 assert(aclp != NULL);
438 n = facl(fd, SETACL, (int)(sizeof(aclp1)/sizeof(aclp1[0])), aclp1);
440 func = "acl_set_fd()";
441 n = acl_set_fd(fd, acl1);
449 if (errno == ENOSYS || errno == ENOTSUP)
451 if (errno == EOPNOTSUPP || errno == EINVAL)
455 skipping("POSIX.1e ACLs are not supported on this filesystem");
459 failure("%s: errno = %d (%s)", func, errno, strerror(errno));
460 assertEqualInt(0, n);
464 assertMakeDir("d", 0700);
467 * Create file d/f1 with acl2
469 * This differs from acl1 in the u:1: and g:15: permissions.
471 * This file deliberately has the same name but a different ACL.
472 * Github Issue #777 explains how libarchive's directory traversal
473 * did not always correctly enter directories before attempting
474 * to read ACLs, resulting in reading the ACL from a like-named
475 * file in the wrong directory.
478 acl2_text = "user::rwx,"
485 { USER_OBJ, -1, 4 | 2 | 1 },
487 { GROUP_OBJ, -1, 4 | 2 | 1},
489 { CLASS_OBJ, -1, 4 | 2 | 1},
493 acl2_text = "user::rwx\n"
499 acl2 = acl_from_text(acl2_text);
500 failure("acl_from_text(): errno = %d (%s)", errno, strerror(errno));
501 assert((void *)acl2 != NULL);
503 fd = open("d/f1", O_WRONLY | O_CREAT | O_EXCL, 0777);
504 failure("Could not create test file?!");
505 if (!assert(fd >= 0)) {
513 n = facl(fd, SETACL, (int)(sizeof(aclp2) / sizeof(aclp2[0])), aclp2);
515 func = "acl_set_fd()";
516 n = acl_set_fd(fd, acl2);
521 failure("%s: errno = %d (%s)", func, errno, strerror(errno));
522 assertEqualInt(0, n);
525 /* Create nested directory d2 with default ACLs */
526 assertMakeDir("d/d2", 0755);
529 acl3_text = "user::rwx,"
536 "default:user:1:r--,"
537 "default:group::r-x,"
538 "default:group:15:r--,"
542 { USER_OBJ, -1, 4 | 2 | 1 },
544 { GROUP_OBJ, -1, 4 | 1 },
546 { CLASS_OBJ, -1, 4 | 2 | 1 },
547 { OTHER_OBJ, -1, 4 | 1 },
548 { USER_OBJ | ACL_DEFAULT, -1, 4 | 2 | 1 },
549 { USER | ACL_DEFAULT, 1, 4 },
550 { GROUP_OBJ | ACL_DEFAULT, -1, 4 | 1 },
551 { GROUP | ACL_DEFAULT, 15, 4 },
552 { CLASS_OBJ | ACL_DEFAULT, -1, 4 | 2 | 1},
553 { OTHER_OBJ | ACL_DEFAULT, -1, 4 | 1 }
556 acl3_text = "user::rwx\n"
562 acl3 = acl_from_text(acl3_text);
563 failure("acl_from_text(): errno = %d (%s)", errno, strerror(errno));
564 assert((void *)acl3 != NULL);
569 n = acl("d/d2", SETACL, (int)(sizeof(aclp3) / sizeof(aclp3[0])), aclp3);
571 func = "acl_set_file()";
572 n = acl_set_file("d/d2", ACL_TYPE_DEFAULT, acl3);
575 failure("%s: errno = %d (%s)", func, errno, strerror(errno));
576 assertEqualInt(0, n);
578 /* Create a read-from-disk object. */
579 assert(NULL != (a = archive_read_disk_new()));
580 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, "."));
581 assert(NULL != (ae = archive_entry_new()));
584 flags = ARCHIVE_ENTRY_ACL_TYPE_POSIX1E
585 | ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA
586 | ARCHIVE_ENTRY_ACL_STYLE_SOLARIS;
589 flags = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
590 dflags = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
593 /* Walk the dir until we see both of the files */
594 while (ARCHIVE_OK == archive_read_next_header2(a, ae)) {
595 archive_read_disk_descend(a);
596 if (strcmp(archive_entry_pathname(ae), "./f1") == 0) {
597 acl_text = archive_entry_acl_to_text(ae, NULL, flags);
598 assertEqualString(acl_text, acl1_text);
600 } else if (strcmp(archive_entry_pathname(ae), "./d/f1") == 0) {
601 acl_text = archive_entry_acl_to_text(ae, NULL, flags);
602 assertEqualString(acl_text, acl2_text);
604 } else if (strcmp(archive_entry_pathname(ae), "./d/d2") == 0) {
605 acl_text = archive_entry_acl_to_text(ae, NULL, dflags);
606 assertEqualString(acl_text, acl3_text);
611 archive_entry_free(ae);
612 assertEqualInt(ARCHIVE_OK, archive_free(a));