1 /*- 2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * This software was developed by Robert Watson for the TrustedBSD Project. 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 * acl_set_file -- set a file/directory ACL by name 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/types.h> 36 #include "namespace.h" 37 #include <sys/acl.h> 38 #include "un-namespace.h" 39 40 #include <errno.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include "acl_support.h" 46 47 /* 48 * For POSIX.1e-semantic ACLs, do a presort so the kernel doesn't have to 49 * (the POSIX.1e semantic code will reject unsorted ACL submission). If it's 50 * not a semantic that the library knows about, just submit it flat and 51 * assume the caller knows what they're up to. 52 */ 53 int 54 acl_set_file(const char *path_p, acl_type_t type, acl_t acl) 55 { 56 int error; 57 58 if (acl == NULL || path_p == NULL) { 59 errno = EINVAL; 60 return (-1); 61 } 62 type = _acl_type_unold(type); 63 if (_acl_type_not_valid_for_acl(acl, type)) { 64 errno = EINVAL; 65 return (-1); 66 } 67 if (_posix1e_acl(acl, type)) { 68 error = _posix1e_acl_sort(acl); 69 if (error) { 70 errno = error; 71 return (-1); 72 } 73 } 74 75 acl->ats_cur_entry = 0; 76 77 return (__acl_set_file(path_p, type, &acl->ats_acl)); 78 } 79 80 int 81 acl_set_link_np(const char *path_p, acl_type_t type, acl_t acl) 82 { 83 int error; 84 85 if (acl == NULL || path_p == NULL) { 86 errno = EINVAL; 87 return (-1); 88 } 89 type = _acl_type_unold(type); 90 if (_acl_type_not_valid_for_acl(acl, type)) { 91 errno = EINVAL; 92 return (-1); 93 } 94 if (_posix1e_acl(acl, type)) { 95 error = _posix1e_acl_sort(acl); 96 if (error) { 97 errno = error; 98 return (-1); 99 } 100 } 101 102 acl->ats_cur_entry = 0; 103 104 return (__acl_set_link(path_p, type, &acl->ats_acl)); 105 } 106 107 int 108 acl_set_fd(int fd, acl_t acl) 109 { 110 111 if (fpathconf(fd, _PC_ACL_NFS4) == 1) 112 return (acl_set_fd_np(fd, acl, ACL_TYPE_NFS4)); 113 114 return (acl_set_fd_np(fd, acl, ACL_TYPE_ACCESS)); 115 } 116 117 int 118 acl_set_fd_np(int fd, acl_t acl, acl_type_t type) 119 { 120 int error; 121 122 if (acl == NULL) { 123 errno = EINVAL; 124 return (-1); 125 } 126 type = _acl_type_unold(type); 127 if (_acl_type_not_valid_for_acl(acl, type)) { 128 errno = EINVAL; 129 return (-1); 130 } 131 if (_posix1e_acl(acl, type)) { 132 error = _posix1e_acl_sort(acl); 133 if (error) { 134 errno = error; 135 return (-1); 136 } 137 } 138 139 acl->ats_cur_entry = 0; 140 141 return (___acl_set_fd(fd, type, &acl->ats_acl)); 142 } 143 144 /* 145 * acl_set_permset() (23.4.23): sets the permissions of ACL entry entry_d 146 * with the permissions in permset_d 147 */ 148 int 149 acl_set_permset(acl_entry_t entry_d, acl_permset_t permset_d) 150 { 151 152 if (!entry_d) { 153 errno = EINVAL; 154 return (-1); 155 } 156 157 if ((*permset_d & ACL_POSIX1E_BITS) != *permset_d) { 158 if ((*permset_d & ACL_NFS4_PERM_BITS) != *permset_d) { 159 errno = EINVAL; 160 return (-1); 161 } 162 if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 163 errno = EINVAL; 164 return (-1); 165 } 166 _entry_brand_as(entry_d, ACL_BRAND_NFS4); 167 } 168 169 entry_d->ae_perm = *permset_d; 170 171 return (0); 172 } 173 174 /* 175 * acl_set_qualifier() sets the qualifier (ae_id) of the tag for 176 * ACL entry entry_d to the value referred to by tag_qualifier_p 177 */ 178 int 179 acl_set_qualifier(acl_entry_t entry_d, const void *tag_qualifier_p) 180 { 181 182 if (!entry_d || !tag_qualifier_p) { 183 errno = EINVAL; 184 return (-1); 185 } 186 switch(entry_d->ae_tag) { 187 case ACL_USER: 188 case ACL_GROUP: 189 entry_d->ae_id = *(uid_t *)tag_qualifier_p; 190 break; 191 default: 192 errno = EINVAL; 193 return (-1); 194 } 195 196 return (0); 197 } 198 199 /* 200 * acl_set_tag_type() sets the tag type for ACL entry entry_d to the 201 * value of tag_type 202 */ 203 int 204 acl_set_tag_type(acl_entry_t entry_d, acl_tag_t tag_type) 205 { 206 207 if (entry_d == NULL) { 208 errno = EINVAL; 209 return (-1); 210 } 211 212 switch(tag_type) { 213 case ACL_OTHER: 214 case ACL_MASK: 215 if (!_entry_brand_may_be(entry_d, ACL_BRAND_POSIX)) { 216 errno = EINVAL; 217 return (-1); 218 } 219 _entry_brand_as(entry_d, ACL_BRAND_POSIX); 220 break; 221 case ACL_EVERYONE: 222 if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 223 errno = EINVAL; 224 return (-1); 225 } 226 _entry_brand_as(entry_d, ACL_BRAND_NFS4); 227 break; 228 } 229 230 switch(tag_type) { 231 case ACL_USER_OBJ: 232 case ACL_USER: 233 case ACL_GROUP_OBJ: 234 case ACL_GROUP: 235 case ACL_MASK: 236 case ACL_OTHER: 237 case ACL_EVERYONE: 238 entry_d->ae_tag = tag_type; 239 return (0); 240 } 241 242 errno = EINVAL; 243 return (-1); 244 } 245 246 int 247 acl_set_entry_type_np(acl_entry_t entry_d, acl_entry_type_t entry_type) 248 { 249 250 if (entry_d == NULL) { 251 errno = EINVAL; 252 return (-1); 253 } 254 if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 255 errno = EINVAL; 256 return (-1); 257 } 258 _entry_brand_as(entry_d, ACL_BRAND_NFS4); 259 260 switch (entry_type) { 261 case ACL_ENTRY_TYPE_ALLOW: 262 case ACL_ENTRY_TYPE_DENY: 263 case ACL_ENTRY_TYPE_AUDIT: 264 case ACL_ENTRY_TYPE_ALARM: 265 entry_d->ae_entry_type = entry_type; 266 return (0); 267 } 268 269 errno = EINVAL; 270 return (-1); 271 } 272