1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Chris D. Faulhaber 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <errno.h> 30 #include <stdio.h> 31 #include <assert.h> 32 #include <sys/acl.h> 33 #include <sys/stat.h> 34 35 #include "acl_support.h" 36 37 /* 38 * These routines from sys/kern/subr_acl_nfs4.c are used by both kernel 39 * and libc. 40 */ 41 void acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp); 42 void acl_nfs4_trivial_from_mode_libc(struct acl *aclp, int file_owner_id, 43 int canonical_six); 44 45 static acl_t 46 _nfs4_acl_strip_np(const acl_t aclp, int canonical_six) 47 { 48 acl_t newacl; 49 mode_t mode = 0; 50 51 newacl = acl_init(ACL_MAX_ENTRIES); 52 if (newacl == NULL) { 53 errno = ENOMEM; 54 return (NULL); 55 } 56 57 _acl_brand_as(newacl, ACL_BRAND_NFS4); 58 59 acl_nfs4_sync_mode_from_acl(&mode, &(aclp->ats_acl)); 60 acl_nfs4_trivial_from_mode_libc(&(newacl->ats_acl), mode, canonical_six); 61 62 return (newacl); 63 } 64 65 static acl_t 66 _posix1e_acl_strip_np(const acl_t aclp, int recalculate_mask) 67 { 68 acl_t acl_new, acl_old; 69 acl_entry_t entry, entry_new; 70 acl_tag_t tag; 71 int entry_id, have_mask_entry; 72 73 assert(_acl_brand(aclp) == ACL_BRAND_POSIX); 74 75 acl_old = acl_dup(aclp); 76 if (acl_old == NULL) 77 return (NULL); 78 79 assert(_acl_brand(acl_old) == ACL_BRAND_POSIX); 80 81 have_mask_entry = 0; 82 acl_new = acl_init(ACL_MAX_ENTRIES); 83 if (acl_new == NULL) { 84 acl_free(acl_old); 85 return (NULL); 86 } 87 tag = ACL_UNDEFINED_TAG; 88 89 /* only save the default user/group/other entries */ 90 entry_id = ACL_FIRST_ENTRY; 91 while (acl_get_entry(acl_old, entry_id, &entry) == 1) { 92 entry_id = ACL_NEXT_ENTRY; 93 94 assert(_entry_brand(entry) == ACL_BRAND_POSIX); 95 96 if (acl_get_tag_type(entry, &tag) == -1) 97 goto fail; 98 99 switch(tag) { 100 case ACL_USER_OBJ: 101 case ACL_GROUP_OBJ: 102 case ACL_OTHER: 103 if (acl_create_entry(&acl_new, &entry_new) == -1) 104 goto fail; 105 if (acl_copy_entry(entry_new, entry) == -1) 106 goto fail; 107 assert(_entry_brand(entry_new) == ACL_BRAND_POSIX); 108 break; 109 case ACL_MASK: 110 have_mask_entry = 1; 111 break; 112 default: 113 break; 114 } 115 } 116 117 assert(_acl_brand(acl_new) == ACL_BRAND_POSIX); 118 119 if (have_mask_entry && recalculate_mask) { 120 if (acl_calc_mask(&acl_new) == -1) 121 goto fail; 122 } 123 124 return (acl_new); 125 126 fail: 127 acl_free(acl_new); 128 acl_free(acl_old); 129 130 return (NULL); 131 } 132 133 acl_t 134 acl_strip_np(const acl_t aclp, int recalculate_mask) 135 { 136 switch (_acl_brand(aclp)) { 137 case ACL_BRAND_NFS4: 138 return (_nfs4_acl_strip_np(aclp, 0)); 139 140 case ACL_BRAND_POSIX: 141 return (_posix1e_acl_strip_np(aclp, recalculate_mask)); 142 143 default: 144 errno = EINVAL; 145 return (NULL); 146 } 147 } 148 149 /* 150 * Return 1, if ACL is trivial, 0 otherwise. 151 * 152 * ACL is trivial, iff its meaning could be fully expressed using just file 153 * mode. In other words, ACL is trivial iff it doesn't have "+" to the right 154 * of the mode bits in "ls -l" output ;-) 155 */ 156 int 157 acl_is_trivial_np(const acl_t aclp, int *trivialp) 158 { 159 acl_t tmpacl; 160 int differs; 161 162 if (aclp == NULL || trivialp == NULL) { 163 errno = EINVAL; 164 return (-1); 165 } 166 167 switch (_acl_brand(aclp)) { 168 case ACL_BRAND_POSIX: 169 if (aclp->ats_acl.acl_cnt == 3) 170 *trivialp = 1; 171 else 172 *trivialp = 0; 173 174 return (0); 175 176 case ACL_BRAND_NFS4: 177 /* 178 * If the ACL has more than canonical six entries, 179 * it's non trivial by definition. 180 */ 181 if (aclp->ats_acl.acl_cnt > 6) { 182 *trivialp = 0; 183 return (0); 184 } 185 186 /* 187 * Calculate trivial ACL - using acl_strip_np(3) - and compare 188 * with the original. 189 */ 190 tmpacl = _nfs4_acl_strip_np(aclp, 0); 191 if (tmpacl == NULL) 192 return (-1); 193 194 differs = _acl_differs(aclp, tmpacl); 195 acl_free(tmpacl); 196 197 if (differs == 0) { 198 *trivialp = 1; 199 return (0); 200 } 201 202 /* 203 * Try again with an old-style, "canonical six" trivial ACL. 204 */ 205 tmpacl = _nfs4_acl_strip_np(aclp, 1); 206 if (tmpacl == NULL) 207 return (-1); 208 209 differs = _acl_differs(aclp, tmpacl); 210 acl_free(tmpacl); 211 212 if (differs) 213 *trivialp = 0; 214 else 215 *trivialp = 1; 216 217 return (0); 218 219 default: 220 errno = EINVAL; 221 return (-1); 222 } 223 } 224