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" 26229592Smm__FBSDID("$FreeBSD$"); 27228753Smm 28228753Smm/* 29228753Smm * Exercise the system-independent portion of the ACL support. 30228753Smm * Check that archive_entry objects can save and restore 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 79228753Smmstatic void 80228753Smmset_acls(struct archive_entry *ae, struct acl_t *acls, int n) 81228753Smm{ 82228753Smm int i; 83228753Smm 84228753Smm archive_entry_acl_clear(ae); 85228753Smm for (i = 0; i < n; i++) { 86228753Smm archive_entry_acl_add_entry(ae, 87228753Smm acls[i].type, acls[i].permset, acls[i].tag, acls[i].qual, 88228753Smm acls[i].name); 89228753Smm } 90228753Smm} 91228753Smm 92228753Smmstatic int 93228753Smmacl_match(struct acl_t *acl, int type, int permset, int tag, int qual, const char *name) 94228753Smm{ 95228753Smm if (type != acl->type) 96228753Smm return (0); 97228753Smm if (permset != acl->permset) 98228753Smm return (0); 99228753Smm if (tag != acl->tag) 100228753Smm return (0); 101228753Smm if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) 102228753Smm return (1); 103228753Smm if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) 104228753Smm return (1); 105228753Smm if (tag == ARCHIVE_ENTRY_ACL_OTHER) 106228753Smm return (1); 107228753Smm if (qual != acl->qual) 108228753Smm return (0); 109228753Smm if (name == NULL) { 110228753Smm if (acl->name == NULL || acl->name[0] == '\0') 111228753Smm return (1); 112228753Smm } 113228753Smm if (acl->name == NULL) { 114228753Smm if (name[0] == '\0') 115228753Smm return (1); 116228753Smm } 117228753Smm return (0 == strcmp(name, acl->name)); 118228753Smm} 119228753Smm 120228753Smmstatic void 121228753Smmcompare_acls(struct archive_entry *ae, struct acl_t *acls, int n, int mode) 122228753Smm{ 123228753Smm int *marker = malloc(sizeof(marker[0]) * n); 124228753Smm int i; 125228753Smm int r; 126228753Smm int type, permset, tag, qual; 127228753Smm int matched; 128228753Smm const char *name; 129228753Smm 130228753Smm for (i = 0; i < n; i++) 131228753Smm marker[i] = i; 132228753Smm 133228753Smm while (0 == (r = archive_entry_acl_next(ae, 134228753Smm ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 135228753Smm &type, &permset, &tag, &qual, &name))) { 136228753Smm for (i = 0, matched = 0; i < n && !matched; i++) { 137228753Smm if (acl_match(&acls[marker[i]], type, permset, 138228753Smm tag, qual, name)) { 139228753Smm /* We found a match; remove it. */ 140228753Smm marker[i] = marker[n - 1]; 141228753Smm n--; 142228753Smm matched = 1; 143228753Smm } 144228753Smm } 145228753Smm if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) { 146228753Smm if (!matched) printf("No match for user_obj perm\n"); 147228753Smm failure("USER_OBJ permset (%02o) != user mode (%02o)", 148228753Smm permset, 07 & (mode >> 6)); 149228753Smm assert((permset << 6) == (mode & 0700)); 150228753Smm } else if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) { 151228753Smm if (!matched) printf("No match for group_obj perm\n"); 152228753Smm failure("GROUP_OBJ permset %02o != group mode %02o", 153228753Smm permset, 07 & (mode >> 3)); 154228753Smm assert((permset << 3) == (mode & 0070)); 155228753Smm } else if (tag == ARCHIVE_ENTRY_ACL_OTHER) { 156228753Smm if (!matched) printf("No match for other perm\n"); 157228753Smm failure("OTHER permset (%02o) != other mode (%02o)", 158228753Smm permset, mode & 07); 159228753Smm assert((permset << 0) == (mode & 0007)); 160228753Smm } else { 161228753Smm failure("Could not find match for ACL " 162228753Smm "(type=%d,permset=%d,tag=%d,qual=%d,name=``%s'')", 163228753Smm type, permset, tag, qual, name); 164228753Smm assert(matched == 1); 165228753Smm } 166228753Smm } 167228753Smm#if ARCHIVE_VERSION_NUMBER < 1009000 168228753Smm /* Known broken before 1.9.0. */ 169228753Smm skipping("archive_entry_acl_next() exits with ARCHIVE_EOF"); 170228753Smm#else 171228753Smm assertEqualInt(ARCHIVE_EOF, r); 172228753Smm#endif 173228753Smm assert((mode & 0777) == (archive_entry_mode(ae) & 0777)); 174228753Smm failure("Could not find match for ACL " 175228753Smm "(type=%d,permset=%d,tag=%d,qual=%d,name=``%s'')", 176228753Smm acls[marker[0]].type, acls[marker[0]].permset, 177228753Smm acls[marker[0]].tag, acls[marker[0]].qual, acls[marker[0]].name); 178228753Smm assert(n == 0); /* Number of ACLs not matched should == 0 */ 179228753Smm free(marker); 180228753Smm} 181228753Smm 182228753SmmDEFINE_TEST(test_acl_basic) 183228753Smm{ 184228753Smm struct archive_entry *ae; 185228753Smm 186228753Smm /* Create a simple archive_entry. */ 187228753Smm assert((ae = archive_entry_new()) != NULL); 188228753Smm archive_entry_set_pathname(ae, "file"); 189228753Smm archive_entry_set_mode(ae, S_IFREG | 0777); 190228753Smm 191228753Smm /* Basic owner/owning group should just update mode bits. */ 192228753Smm set_acls(ae, acls0, sizeof(acls0)/sizeof(acls0[0])); 193228753Smm failure("Basic ACLs shouldn't be stored as extended ACLs"); 194228753Smm assert(0 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 195228753Smm failure("Basic ACLs should set mode to 0142, not %04o", 196228753Smm archive_entry_mode(ae)&0777); 197228753Smm assert((archive_entry_mode(ae) & 0777) == 0142); 198228753Smm 199228753Smm 200228753Smm /* With any extended ACL entry, we should read back a full set. */ 201228753Smm set_acls(ae, acls1, sizeof(acls1)/sizeof(acls1[0])); 202228753Smm failure("One extended ACL should flag all ACLs to be returned."); 203228753Smm assert(4 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 204228753Smm compare_acls(ae, acls1, sizeof(acls1)/sizeof(acls1[0]), 0142); 205228753Smm failure("Basic ACLs should set mode to 0142, not %04o", 206228753Smm archive_entry_mode(ae)&0777); 207228753Smm assert((archive_entry_mode(ae) & 0777) == 0142); 208228753Smm 209228753Smm 210228753Smm /* A more extensive set of ACLs. */ 211228753Smm set_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0])); 212228753Smm assertEqualInt(6, archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 213228753Smm compare_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0]), 0543); 214228753Smm failure("Basic ACLs should set mode to 0543, not %04o", 215228753Smm archive_entry_mode(ae)&0777); 216228753Smm assert((archive_entry_mode(ae) & 0777) == 0543); 217228753Smm 218228753Smm /* 219228753Smm * Check that clearing ACLs gets rid of them all by repeating 220228753Smm * the first test. 221228753Smm */ 222228753Smm set_acls(ae, acls0, sizeof(acls0)/sizeof(acls0[0])); 223228753Smm failure("Basic ACLs shouldn't be stored as extended ACLs"); 224228753Smm assert(0 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)); 225228753Smm failure("Basic ACLs should set mode to 0142, not %04o", 226228753Smm archive_entry_mode(ae)&0777); 227228753Smm assert((archive_entry_mode(ae) & 0777) == 0142); 228228753Smm archive_entry_free(ae); 229228753Smm} 230