1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm#include "test.h" 26228753Smm__FBSDID("$FreeBSD: src/lib/libarchive/test/test_acl_basic.c,v 1.6 2008/10/19 00:13:57 kientzle Exp $"); 27228753Smm 28228753Smm/* 29228753Smm * Exercise the system-independent portion of the ACL support. 30231200Smm * Check that archive_entry objects can save and restore POSIX.1e-style ACL data. 31228753Smm * 32228753Smm * This should work on all systems, regardless of whether local 33228753Smm * filesystems support ACLs or not. 34228753Smm */ 35228753Smm 36228753Smmstruct acl_t { 37228753Smm int type; /* Type of ACL: "access" or "default" */ 38228753Smm int permset; /* Permissions for this class of users. */ 39228753Smm int tag; /* Owner, User, Owning group, group, other, etc. */ 40228753Smm int qual; /* GID or UID of user/group, depending on tag. */ 41228753Smm const char *name; /* Name of user/group, depending on tag. */ 42228753Smm}; 43228753Smm 44228753Smmstatic struct acl_t acls0[] = { 45228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE, 46228753Smm ARCHIVE_ENTRY_ACL_USER_OBJ, 0, "" }, 47228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ, 48228753Smm ARCHIVE_ENTRY_ACL_GROUP_OBJ, 0, "" }, 49228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE, 50228753Smm ARCHIVE_ENTRY_ACL_OTHER, 0, "" }, 51228753Smm}; 52228753Smm 53228753Smmstatic struct acl_t acls1[] = { 54228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE, 55228753Smm ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" }, 56228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ, 57228753Smm ARCHIVE_ENTRY_ACL_USER, 77, "user77" }, 58228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ, 59228753Smm ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" }, 60228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE, 61228753Smm ARCHIVE_ENTRY_ACL_OTHER, -1, "" }, 62228753Smm}; 63228753Smm 64228753Smmstatic struct acl_t acls2[] = { 65228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE | ARCHIVE_ENTRY_ACL_READ, 66228753Smm ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" }, 67228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ, 68228753Smm ARCHIVE_ENTRY_ACL_USER, 77, "user77" }, 69228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0, 70228753Smm ARCHIVE_ENTRY_ACL_USER, 78, "user78" }, 71228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ, 72228753Smm ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" }, 73228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0007, 74228753Smm ARCHIVE_ENTRY_ACL_GROUP, 78, "group78" }, 75228753Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_EXECUTE, 76228753Smm ARCHIVE_ENTRY_ACL_OTHER, -1, "" }, 77228753Smm}; 78228753Smm 79231200Smm/* 80231200Smm * NFS4 entry types; attempts to set these on top of POSIX.1e 81231200Smm * attributes should fail. 82231200Smm */ 83231200Smmstatic struct acl_t acls_nfs4[] = { 84231200Smm /* NFS4 types */ 85231200Smm { ARCHIVE_ENTRY_ACL_TYPE_ALLOW, ARCHIVE_ENTRY_ACL_READ, 86231200Smm ARCHIVE_ENTRY_ACL_USER, 78, "" }, 87231200Smm { ARCHIVE_ENTRY_ACL_TYPE_DENY, ARCHIVE_ENTRY_ACL_READ, 88231200Smm ARCHIVE_ENTRY_ACL_USER, 78, "" }, 89231200Smm { ARCHIVE_ENTRY_ACL_TYPE_AUDIT, ARCHIVE_ENTRY_ACL_READ, 90231200Smm ARCHIVE_ENTRY_ACL_USER, 78, "" }, 91231200Smm { ARCHIVE_ENTRY_ACL_TYPE_ALARM, ARCHIVE_ENTRY_ACL_READ, 92231200Smm ARCHIVE_ENTRY_ACL_USER, 78, "" }, 93231200Smm 94231200Smm /* NFS4 tags */ 95231200Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ, 96231200Smm ARCHIVE_ENTRY_ACL_EVERYONE, -1, "" }, 97231200Smm 98231200Smm /* NFS4 inheritance markers */ 99231200Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 100231200Smm ARCHIVE_ENTRY_ACL_READ | ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, 101231200Smm ARCHIVE_ENTRY_ACL_USER, 79, "" }, 102231200Smm { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 103231200Smm ARCHIVE_ENTRY_ACL_READ | ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, 104231200Smm ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" }, 105231200Smm}; 106231200Smm 107228753Smmstatic void 108228753Smmset_acls(struct archive_entry *ae, struct acl_t *acls, int n) 109228753Smm{ 110228753Smm int i; 111228753Smm 112228753Smm archive_entry_acl_clear(ae); 113228753Smm for (i = 0; i < n; i++) { 114228753Smm archive_entry_acl_add_entry(ae, 115228753Smm acls[i].type, acls[i].permset, acls[i].tag, acls[i].qual, 116228753Smm acls[i].name); 117228753Smm } 118228753Smm} 119228753Smm 120228753Smmstatic int 121228753Smmacl_match(struct acl_t *acl, int type, int permset, int tag, int qual, const char *name) 122228753Smm{ 123228753Smm if (type != acl->type) 124228753Smm return (0); 125228753Smm if (permset != acl->permset) 126228753Smm return (0); 127228753Smm if (tag != acl->tag) 128228753Smm return (0); 129228753Smm if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) 130228753Smm return (1); 131228753Smm if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) 132228753Smm return (1); 133228753Smm if (tag == ARCHIVE_ENTRY_ACL_OTHER) 134228753Smm return (1); 135228753Smm if (qual != acl->qual) 136228753Smm return (0); 137228753Smm if (name == NULL) { 138228753Smm if (acl->name == NULL || acl->name[0] == '\0') 139228753Smm return (1); 140248616Smm return (0); 141228753Smm } 142228753Smm if (acl->name == NULL) { 143228753Smm if (name[0] == '\0') 144228753Smm return (1); 145248616Smm return (0); 146228753Smm } 147228753Smm return (0 == strcmp(name, acl->name)); 148228753Smm} 149228753Smm 150228753Smmstatic void 151228753Smmcompare_acls(struct archive_entry *ae, struct acl_t *acls, int n, int mode) 152228753Smm{ 153228753Smm int *marker = malloc(sizeof(marker[0]) * n); 154228753Smm int i; 155228753Smm int r; 156228753Smm int type, permset, tag, qual; 157228753Smm int matched; 158228753Smm const char *name; 159228753Smm 160228753Smm for (i = 0; i < n; i++) 161228753Smm marker[i] = i; 162228753Smm 163228753Smm while (0 == (r = archive_entry_acl_next(ae, 164228753Smm ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 165228753Smm &type, &permset, &tag, &qual, &name))) { 166228753Smm for (i = 0, matched = 0; i < n && !matched; i++) { 167228753Smm if (acl_match(&acls[marker[i]], type, permset, 168228753Smm tag, qual, name)) { 169228753Smm /* We found a match; remove it. */ 170228753Smm marker[i] = marker[n - 1]; 171228753Smm n--; 172228753Smm matched = 1; 173228753Smm } 174228753Smm } 175228753Smm if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) { 176228753Smm if (!matched) printf("No match for user_obj perm\n"); 177228753Smm failure("USER_OBJ permset (%02o) != user mode (%02o)", 178228753Smm permset, 07 & (mode >> 6)); 179228753Smm assert((permset << 6) == (mode & 0700)); 180228753Smm } else if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) { 181228753Smm if (!matched) printf("No match for group_obj perm\n"); 182228753Smm failure("GROUP_OBJ permset %02o != group mode %02o", 183228753Smm permset, 07 & (mode >> 3)); 184228753Smm assert((permset << 3) == (mode & 0070)); 185228753Smm } else if (tag == ARCHIVE_ENTRY_ACL_OTHER) { 186228753Smm if (!matched) printf("No match for other perm\n"); 187228753Smm failure("OTHER permset (%02o) != other mode (%02o)", 188228753Smm permset, mode & 07); 189228753Smm assert((permset << 0) == (mode & 0007)); 190228753Smm } else { 191228753Smm failure("Could not find match for ACL " 192228753Smm "(type=%d,permset=%d,tag=%d,qual=%d,name=``%s'')", 193228753Smm type, permset, tag, qual, name); 194228753Smm assert(matched == 1); 195228753Smm } 196228753Smm } 197228753Smm assertEqualInt(ARCHIVE_EOF, r); 198232153Smm assert((mode_t)(mode & 0777) == (archive_entry_mode(ae) & 0777)); 199228753Smm failure("Could not find match for ACL " 200228753Smm "(type=%d,permset=%d,tag=%d,qual=%d,name=``%s'')", 201228753Smm acls[marker[0]].type, acls[marker[0]].permset, 202228753Smm acls[marker[0]].tag, acls[marker[0]].qual, acls[marker[0]].name); 203228753Smm assert(n == 0); /* Number of ACLs not matched should == 0 */ 204228753Smm free(marker); 205228753Smm} 206228753Smm 207231200SmmDEFINE_TEST(test_acl_posix1e) 208228753Smm{ 209228753Smm struct archive_entry *ae; 210231200Smm int i; 211228753Smm 212228753Smm /* Create a simple archive_entry. */ 213228753Smm assert((ae = archive_entry_new()) != NULL); 214228753Smm archive_entry_set_pathname(ae, "file"); 215228753Smm archive_entry_set_mode(ae, S_IFREG | 0777); 216228753Smm 217228753Smm /* Basic owner/owning group should just update mode bits. */ 218231200Smm 219231200Smm /* 220231200Smm * Note: This features of libarchive's ACL implementation 221231200Smm * shouldn't be relied on and should probably be removed. It 222231200Smm * was done to identify trivial ACLs so we could avoid 223231200Smm * triggering unnecessary extensions. It's better to identify 224231200Smm * trivial ACLs at the point they are being read from disk. 225231200Smm */ 226228753Smm set_acls(ae, acls0, sizeof(acls0)/sizeof(acls0[0])); 227228753Smm failure("Basic ACLs shouldn't be stored as extended ACLs"); 228228753Smm assert(0 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 229228753Smm failure("Basic ACLs should set mode to 0142, not %04o", 230228753Smm archive_entry_mode(ae)&0777); 231228753Smm assert((archive_entry_mode(ae) & 0777) == 0142); 232228753Smm 233228753Smm 234228753Smm /* With any extended ACL entry, we should read back a full set. */ 235228753Smm set_acls(ae, acls1, sizeof(acls1)/sizeof(acls1[0])); 236228753Smm failure("One extended ACL should flag all ACLs to be returned."); 237228753Smm assert(4 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 238228753Smm compare_acls(ae, acls1, sizeof(acls1)/sizeof(acls1[0]), 0142); 239228753Smm failure("Basic ACLs should set mode to 0142, not %04o", 240228753Smm archive_entry_mode(ae)&0777); 241228753Smm assert((archive_entry_mode(ae) & 0777) == 0142); 242228753Smm 243228753Smm 244228753Smm /* A more extensive set of ACLs. */ 245228753Smm set_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0])); 246228753Smm assertEqualInt(6, archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 247228753Smm compare_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0]), 0543); 248228753Smm failure("Basic ACLs should set mode to 0543, not %04o", 249228753Smm archive_entry_mode(ae)&0777); 250228753Smm assert((archive_entry_mode(ae) & 0777) == 0543); 251228753Smm 252228753Smm /* 253228753Smm * Check that clearing ACLs gets rid of them all by repeating 254228753Smm * the first test. 255228753Smm */ 256228753Smm set_acls(ae, acls0, sizeof(acls0)/sizeof(acls0[0])); 257228753Smm failure("Basic ACLs shouldn't be stored as extended ACLs"); 258228753Smm assert(0 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 259228753Smm failure("Basic ACLs should set mode to 0142, not %04o", 260228753Smm archive_entry_mode(ae)&0777); 261228753Smm assert((archive_entry_mode(ae) & 0777) == 0142); 262231200Smm 263231200Smm /* 264231200Smm * Different types of malformed ACL entries that should 265231200Smm * fail when added to existing POSIX.1e ACLs. 266231200Smm */ 267231200Smm set_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0])); 268232153Smm for (i = 0; i < (int)(sizeof(acls_nfs4)/sizeof(acls_nfs4[0])); ++i) { 269231200Smm struct acl_t *p = &acls_nfs4[i]; 270231200Smm failure("Malformed ACL test #%d", i); 271231200Smm assertEqualInt(ARCHIVE_FAILED, 272231200Smm archive_entry_acl_add_entry(ae, 273231200Smm p->type, p->permset, p->tag, p->qual, p->name)); 274231200Smm assertEqualInt(6, 275231200Smm archive_entry_acl_reset(ae, 276231200Smm ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 277231200Smm } 278228753Smm archive_entry_free(ae); 279228753Smm} 280