2 * Copyright (c) 2003-2008 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 __FBSDID("$FreeBSD: head/lib/libarchive/test/test_acl_freebsd.c 189427 2009-03-06 04:21:23Z kientzle $");
28 #if defined(__FreeBSD__) && __FreeBSD__ > 4
32 int type; /* Type of ACL: "access" or "default" */
33 int permset; /* Permissions for this class of users. */
34 int tag; /* Owner, User, Owning group, group, other, etc. */
35 int qual; /* GID or UID of user/group, depending on tag. */
36 const char *name; /* Name of user/group, depending on tag. */
39 static struct myacl_t acls2[] = {
40 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE | ARCHIVE_ENTRY_ACL_READ,
41 ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" },
42 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
43 ARCHIVE_ENTRY_ACL_USER, 77, "user77" },
44 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0,
45 ARCHIVE_ENTRY_ACL_USER, 78, "user78" },
46 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
47 ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" },
48 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0007,
49 ARCHIVE_ENTRY_ACL_GROUP, 78, "group78" },
50 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
51 ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_EXECUTE,
52 ARCHIVE_ENTRY_ACL_OTHER, -1, "" },
53 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
54 ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_READ | ARCHIVE_ENTRY_ACL_EXECUTE,
55 ARCHIVE_ENTRY_ACL_MASK, -1, "" },
60 set_acls(struct archive_entry *ae, struct myacl_t *acls)
64 archive_entry_acl_clear(ae);
65 for (i = 0; acls[i].name != NULL; i++) {
66 archive_entry_acl_add_entry(ae,
67 acls[i].type, acls[i].permset, acls[i].tag, acls[i].qual,
73 acl_entry_get_perm(acl_entry_t aclent) {
75 acl_permset_t opaque_ps;
77 /* translate the silly opaque permset to a bitmap */
78 acl_get_permset(aclent, &opaque_ps);
79 if (acl_get_perm_np(opaque_ps, ACL_EXECUTE))
80 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
81 if (acl_get_perm_np(opaque_ps, ACL_WRITE))
82 permset |= ARCHIVE_ENTRY_ACL_WRITE;
83 if (acl_get_perm_np(opaque_ps, ACL_READ))
84 permset |= ARCHIVE_ENTRY_ACL_READ;
90 acl_get_specific_entry(acl_t acl, acl_tag_t requested_tag_type, int requested_tag) {
91 int entry_id = ACL_FIRST_ENTRY;
92 acl_entry_t acl_entry;
93 acl_tag_t acl_tag_type;
95 while (1 == acl_get_entry(acl, entry_id, &acl_entry)) {
96 /* After the first time... */
97 entry_id = ACL_NEXT_ENTRY;
99 /* If this matches, return perm mask */
100 acl_get_tag_type(acl_entry, &acl_tag_type);
101 if (acl_tag_type == requested_tag_type) {
102 switch (acl_tag_type) {
104 if ((uid_t)requested_tag == *(uid_t *)(acl_get_qualifier(acl_entry))) {
105 return acl_entry_get_perm(acl_entry);
109 if ((gid_t)requested_tag == *(gid_t *)(acl_get_qualifier(acl_entry))) {
110 return acl_entry_get_perm(acl_entry);
116 return acl_entry_get_perm(acl_entry);
118 failure("Unexpected ACL tag type");
130 acl_match(acl_entry_t aclent, struct myacl_t *myacl)
136 if (myacl->permset != acl_entry_get_perm(aclent))
139 acl_get_tag_type(aclent, &tag_type);
142 if (myacl->tag != ARCHIVE_ENTRY_ACL_USER_OBJ) return (0);
145 if (myacl->tag != ARCHIVE_ENTRY_ACL_USER)
147 up = acl_get_qualifier(aclent);
150 if ((uid_t)myacl->qual != u)
154 if (myacl->tag != ARCHIVE_ENTRY_ACL_GROUP_OBJ) return (0);
157 if (myacl->tag != ARCHIVE_ENTRY_ACL_GROUP)
159 gp = acl_get_qualifier(aclent);
162 if ((gid_t)myacl->qual != g)
166 if (myacl->tag != ARCHIVE_ENTRY_ACL_MASK) return (0);
169 if (myacl->tag != ARCHIVE_ENTRY_ACL_OTHER) return (0);
176 compare_acls(acl_t acl, struct myacl_t *myacls)
179 int entry_id = ACL_FIRST_ENTRY;
182 acl_entry_t acl_entry;
184 /* Count ACL entries in myacls array and allocate an indirect array. */
185 for (n = 0; myacls[n].name != NULL; ++n)
188 marker = malloc(sizeof(marker[0]) * n);
191 for (i = 0; i < n; i++)
197 * Iterate over acls in system acl object, try to match each
198 * one with an item in the myacls array.
200 while (1 == acl_get_entry(acl, entry_id, &acl_entry)) {
201 /* After the first time... */
202 entry_id = ACL_NEXT_ENTRY;
204 /* Search for a matching entry (tag and qualifier) */
205 for (i = 0, matched = 0; i < n && !matched; i++) {
206 if (acl_match(acl_entry, &myacls[marker[i]])) {
207 /* We found a match; remove it. */
208 marker[i] = marker[n - 1];
214 /* TODO: Print out more details in this case. */
215 failure("ACL entry on file that shouldn't be there");
216 assert(matched == 1);
219 /* Dump entries in the myacls array that weren't in the system acl. */
220 for (i = 0; i < n; ++i) {
221 failure(" ACL entry missing from file: "
222 "type=%d,permset=%d,tag=%d,qual=%d,name=``%s''\n",
223 myacls[marker[i]].type, myacls[marker[i]].permset,
224 myacls[marker[i]].tag, myacls[marker[i]].qual,
225 myacls[marker[i]].name);
226 assert(0); /* Record this as a failure. */
235 * Verify ACL restore-to-disk. This test is FreeBSD-specific.
238 DEFINE_TEST(test_acl_freebsd_posix1e_restore)
240 #if !defined(__FreeBSD__)
241 skipping("FreeBSD-specific ACL restore test");
242 #elif __FreeBSD__ < 5
243 skipping("ACL restore supported only on FreeBSD 5.0 and later");
247 struct archive_entry *ae;
252 * First, do a quick manual set/read of ACL data to
253 * verify that the local filesystem does support ACLs.
254 * If it doesn't, we'll simply skip the remaining tests.
256 acl = acl_from_text("u::rwx,u:1:rw,g::rwx,g:15:rx,o::rwx,m::rwx");
257 assert((void *)acl != NULL);
258 /* Create a test file and try to set an ACL on it. */
259 fd = open("pretest", O_WRONLY | O_CREAT | O_EXCL, 0777);
260 failure("Could not create test file?!");
261 if (!assert(fd >= 0)) {
266 n = acl_set_fd(fd, acl);
268 if (n != 0 && errno == EOPNOTSUPP) {
270 skipping("ACL tests require that ACL support be enabled on the filesystem");
273 if (n != 0 && errno == EINVAL) {
275 skipping("This filesystem does not support POSIX.1e ACLs");
278 failure("acl_set_fd(): errno = %d (%s)",
279 errno, strerror(errno));
280 assertEqualInt(0, n);
283 /* Create a write-to-disk object. */
284 assert(NULL != (a = archive_write_disk_new()));
285 archive_write_disk_set_options(a,
286 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL);
288 /* Populate an archive entry with some metadata, including ACL info */
289 ae = archive_entry_new();
291 archive_entry_set_pathname(ae, "test0");
292 archive_entry_set_mtime(ae, 123456, 7890);
293 archive_entry_set_size(ae, 0);
295 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
296 archive_entry_free(ae);
298 /* Close the archive. */
299 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
300 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
302 /* Verify the data on disk. */
303 assertEqualInt(0, stat("test0", &st));
304 assertEqualInt(st.st_mtime, 123456);
305 acl = acl_get_file("test0", ACL_TYPE_ACCESS);
306 assert(acl != (acl_t)NULL);
307 compare_acls(acl, acls2);
313 * Verify ACL reaed-from-disk. This test is FreeBSD-specific.
315 DEFINE_TEST(test_acl_freebsd_posix1e_read)
317 #if !defined(__FreeBSD__)
318 skipping("FreeBSD-specific ACL read test");
319 #elif __FreeBSD__ < 5
320 skipping("ACL read supported only on FreeBSD 5.0 and later");
323 struct archive_entry *ae;
325 const char *acl1_text, *acl2_text;
329 * Manually construct a directory and two files with
330 * different ACLs. This also serves to verify that ACLs
331 * are supported on the local filesystem.
334 /* Create a test file f1 with acl1 */
335 acl1_text = "user::rwx,group::rwx,other::rwx,user:1:rw-,group:15:r-x,mask::rwx";
336 acl1 = acl_from_text(acl1_text);
337 assert((void *)acl1 != NULL);
338 fd = open("f1", O_WRONLY | O_CREAT | O_EXCL, 0777);
339 failure("Could not create test file?!");
340 if (!assert(fd >= 0)) {
344 n = acl_set_fd(fd, acl1);
346 if (n != 0 && errno == EOPNOTSUPP) {
348 skipping("ACL tests require that ACL support be enabled on the filesystem");
351 if (n != 0 && errno == EINVAL) {
353 skipping("This filesystem does not support POSIX.1e ACLs");
356 failure("acl_set_fd(): errno = %d (%s)",
357 errno, strerror(errno));
358 assertEqualInt(0, n);
361 assertMakeDir("d", 0700);
364 * Create file d/f1 with acl2
366 * This differs from acl1 in the u:1: and g:15: permissions.
368 * This file deliberately has the same name but a different ACL.
369 * Github Issue #777 explains how libarchive's directory traversal
370 * did not always correctly enter directories before attempting
371 * to read ACLs, resulting in reading the ACL from a like-named
372 * file in the wrong directory.
374 acl2_text = "user::rwx,group::rwx,other::---,user:1:r--,group:15:r--,mask::rwx";
375 acl2 = acl_from_text(acl2_text);
376 assert((void *)acl2 != NULL);
377 fd = open("d/f1", O_WRONLY | O_CREAT | O_EXCL, 0777);
378 failure("Could not create test file?!");
379 if (!assert(fd >= 0)) {
383 n = acl_set_fd(fd, acl2);
385 if (n != 0 && errno == EOPNOTSUPP) {
387 skipping("ACL tests require that ACL support be enabled on the filesystem");
390 if (n != 0 && errno == EINVAL) {
392 skipping("This filesystem does not support POSIX.1e ACLs");
395 failure("acl_set_fd(): errno = %d (%s)",
396 errno, strerror(errno));
397 assertEqualInt(0, n);
400 /* Create a read-from-disk object. */
401 assert(NULL != (a = archive_read_disk_new()));
402 assertEqualIntA(a, ARCHIVE_OK, archive_read_disk_open(a, "."));
403 assert(NULL != (ae = archive_entry_new()));
405 /* Walk the dir until we see both of the files */
406 while (ARCHIVE_OK == archive_read_next_header2(a, ae)) {
407 archive_read_disk_descend(a);
408 if (strcmp(archive_entry_pathname(ae), "./f1") == 0) {
409 assertEqualString(archive_entry_acl_text(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS), acl1_text);
411 } else if (strcmp(archive_entry_pathname(ae), "./d/f1") == 0) {
412 assertEqualString(archive_entry_acl_text(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS), acl2_text);