| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /*- |
| 2 | * Copyright (c) 1999, 2000 Robert N. M. Watson | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 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. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 24 | * SUCH DAMAGE. | |
| 25 | * | |
| 26 | * $FreeBSD: src/sys/kern/kern_acl.c,v 1.2.2.1 2000/07/28 18:48:16 rwatson Exp $ | |
| 31e4e90f | 27 | * $DragonFly: src/sys/kern/kern_acl.c,v 1.17 2007/02/19 00:51:54 swildner Exp $ |
| 984263bc MD |
28 | */ |
| 29 | ||
| 30 | /* | |
| 31 | * Generic routines to support file system ACLs, at a syntactic level | |
| 32 | * Semantics are the responsibility of the underlying file system | |
| 33 | */ | |
| 34 | ||
| 35 | #include <sys/param.h> | |
| 36 | #include <sys/systm.h> | |
| 37 | #include <sys/sysproto.h> | |
| 38 | #include <sys/kernel.h> | |
| 39 | #include <sys/malloc.h> | |
| 40 | #include <sys/vnode.h> | |
| 41 | #include <sys/lock.h> | |
| dadab5e9 | 42 | #include <sys/proc.h> |
| fad57d0e | 43 | #include <sys/nlookup.h> |
| 984263bc | 44 | #include <sys/file.h> |
| 984263bc MD |
45 | #include <sys/sysent.h> |
| 46 | #include <sys/errno.h> | |
| 47 | #include <sys/stat.h> | |
| 48 | #include <sys/acl.h> | |
| 49 | ||
| 41c20dac MD |
50 | static int vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp); |
| 51 | static int vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp); | |
| 52 | static int vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp); | |
| 984263bc MD |
53 | |
| 54 | /* | |
| 55 | * These calls wrap the real vnode operations, and are called by the | |
| 56 | * syscall code once the syscall has converted the path or file | |
| 57 | * descriptor to a vnode (unlocked). The aclp pointer is assumed | |
| 58 | * still to point to userland, so this should not be consumed within | |
| 59 | * the kernel except by syscall code. Other code should directly | |
| 60 | * invoke VOP_{SET,GET}ACL. | |
| 61 | */ | |
| 62 | ||
| 63 | /* | |
| 64 | * Given a vnode, set its ACL. | |
| 65 | */ | |
| 66 | static int | |
| 41c20dac | 67 | vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp) |
| 984263bc | 68 | { |
| dadab5e9 | 69 | struct thread *td = curthread; |
| 984263bc | 70 | struct acl inkernacl; |
| dadab5e9 | 71 | struct ucred *ucred; |
| 984263bc MD |
72 | int error; |
| 73 | ||
| 74 | error = copyin(aclp, &inkernacl, sizeof(struct acl)); | |
| 75 | if (error) | |
| 76 | return(error); | |
| 9910d07b | 77 | ucred = td->td_ucred; |
| dadab5e9 | 78 | |
| ca466bae | 79 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 87de5057 | 80 | error = VOP_SETACL(vp, type, &inkernacl, ucred); |
| a11aaa81 | 81 | vn_unlock(vp); |
| 984263bc MD |
82 | return(error); |
| 83 | } | |
| 84 | ||
| 85 | /* | |
| 86 | * Given a vnode, get its ACL. | |
| 87 | */ | |
| 88 | static int | |
| 41c20dac | 89 | vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp) |
| 984263bc | 90 | { |
| dadab5e9 | 91 | struct thread *td = curthread; |
| 984263bc | 92 | struct acl inkernelacl; |
| dadab5e9 | 93 | struct ucred *ucred; |
| 984263bc MD |
94 | int error; |
| 95 | ||
| 9910d07b | 96 | ucred = td->td_ucred; |
| 87de5057 | 97 | error = VOP_GETACL(vp, type, &inkernelacl, ucred); |
| 984263bc MD |
98 | if (error == 0) |
| 99 | error = copyout(&inkernelacl, aclp, sizeof(struct acl)); | |
| 100 | return (error); | |
| 101 | } | |
| 102 | ||
| 103 | /* | |
| 104 | * Given a vnode, delete its ACL. | |
| 105 | */ | |
| 106 | static int | |
| 41c20dac | 107 | vacl_delete(struct vnode *vp, acl_type_t type) |
| 984263bc | 108 | { |
| dadab5e9 MD |
109 | struct thread *td = curthread; |
| 110 | struct ucred *ucred; | |
| 984263bc MD |
111 | int error; |
| 112 | ||
| 9910d07b | 113 | ucred = td->td_ucred; |
| ca466bae | 114 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 87de5057 | 115 | error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, ucred); |
| a11aaa81 | 116 | vn_unlock(vp); |
| 984263bc MD |
117 | return (error); |
| 118 | } | |
| 119 | ||
| 120 | /* | |
| 121 | * Given a vnode, check whether an ACL is appropriate for it | |
| 122 | */ | |
| 123 | static int | |
| 41c20dac | 124 | vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp) |
| 984263bc | 125 | { |
| dadab5e9 MD |
126 | struct thread *td = curthread; |
| 127 | struct ucred *ucred; | |
| 984263bc MD |
128 | struct acl inkernelacl; |
| 129 | int error; | |
| 130 | ||
| 9910d07b | 131 | ucred = td->td_ucred; |
| 984263bc MD |
132 | error = copyin(aclp, &inkernelacl, sizeof(struct acl)); |
| 133 | if (error) | |
| 134 | return(error); | |
| 87de5057 | 135 | error = VOP_ACLCHECK(vp, type, &inkernelacl, ucred); |
| 984263bc MD |
136 | return (error); |
| 137 | } | |
| 138 | ||
| 139 | /* | |
| 140 | * syscalls -- convert the path/fd to a vnode, and call vacl_whatever. | |
| 141 | * Don't need to lock, as the vacl_ code will get/release any locks | |
| 142 | * required. | |
| 143 | */ | |
| 144 | ||
| 145 | /* | |
| 146 | * Given a file path, get an ACL for it | |
| 3919ced0 MD |
147 | * |
| 148 | * MPALMOSTSAFE | |
| 984263bc MD |
149 | */ |
| 150 | int | |
| 753fd850 | 151 | sys___acl_get_file(struct __acl_get_file_args *uap) |
| 984263bc | 152 | { |
| fad57d0e MD |
153 | struct nlookupdata nd; |
| 154 | struct vnode *vp; | |
| 984263bc MD |
155 | int error; |
| 156 | ||
| fad57d0e | 157 | vp = NULL; |
| 3919ced0 | 158 | get_mplock(); |
| ab2eb4eb | 159 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e MD |
160 | if (error == 0) |
| 161 | error = nlookup(&nd); | |
| 162 | if (error == 0) | |
| 28623bf9 | 163 | error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); |
| fad57d0e MD |
164 | nlookup_done(&nd); |
| 165 | if (error == 0) { | |
| ab2eb4eb | 166 | error = vacl_get_acl(vp, uap->type, uap->aclp); |
| fad57d0e MD |
167 | vrele(vp); |
| 168 | } | |
| 3919ced0 MD |
169 | rel_mplock(); |
| 170 | ||
| 984263bc MD |
171 | return (error); |
| 172 | } | |
| 173 | ||
| 174 | /* | |
| 175 | * Given a file path, set an ACL for it | |
| 3919ced0 MD |
176 | * |
| 177 | * MPALMOSTSAFE | |
| 984263bc MD |
178 | */ |
| 179 | int | |
| 753fd850 | 180 | sys___acl_set_file(struct __acl_set_file_args *uap) |
| 984263bc | 181 | { |
| fad57d0e MD |
182 | struct nlookupdata nd; |
| 183 | struct vnode *vp; | |
| 984263bc MD |
184 | int error; |
| 185 | ||
| fad57d0e | 186 | vp = NULL; |
| 3919ced0 | 187 | get_mplock(); |
| ab2eb4eb | 188 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e MD |
189 | if (error == 0) |
| 190 | error = nlookup(&nd); | |
| 191 | if (error == 0) | |
| 28623bf9 | 192 | error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); |
| fad57d0e MD |
193 | nlookup_done(&nd); |
| 194 | if (error == 0) { | |
| ab2eb4eb | 195 | error = vacl_set_acl(vp, uap->type, uap->aclp); |
| fad57d0e MD |
196 | vrele(vp); |
| 197 | } | |
| 3919ced0 MD |
198 | rel_mplock(); |
| 199 | ||
| 984263bc MD |
200 | return (error); |
| 201 | } | |
| 202 | ||
| 203 | /* | |
| 204 | * Given a file descriptor, get an ACL for it | |
| 3919ced0 MD |
205 | * |
| 206 | * MPALMOSTSAFE | |
| 984263bc MD |
207 | */ |
| 208 | int | |
| 753fd850 | 209 | sys___acl_get_fd(struct __acl_get_fd_args *uap) |
| 984263bc | 210 | { |
| dadab5e9 | 211 | struct thread *td = curthread; |
| 984263bc MD |
212 | struct file *fp; |
| 213 | int error; | |
| 214 | ||
| dadab5e9 | 215 | KKASSERT(td->td_proc); |
| 5b287bba | 216 | if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) |
| 984263bc | 217 | return(error); |
| 3919ced0 | 218 | get_mplock(); |
| 5b287bba | 219 | error = vacl_get_acl((struct vnode *)fp->f_data, uap->type, uap->aclp); |
| 3919ced0 | 220 | rel_mplock(); |
| 5b287bba | 221 | fdrop(fp); |
| 3919ced0 | 222 | |
| 5b287bba | 223 | return (error); |
| 984263bc MD |
224 | } |
| 225 | ||
| 226 | /* | |
| 227 | * Given a file descriptor, set an ACL for it | |
| 3919ced0 MD |
228 | * |
| 229 | * MPALMOSTSAFE | |
| 984263bc MD |
230 | */ |
| 231 | int | |
| 753fd850 | 232 | sys___acl_set_fd(struct __acl_set_fd_args *uap) |
| 984263bc | 233 | { |
| dadab5e9 | 234 | struct thread *td = curthread; |
| 984263bc MD |
235 | struct file *fp; |
| 236 | int error; | |
| 237 | ||
| dadab5e9 | 238 | KKASSERT(td->td_proc); |
| 5b287bba | 239 | if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) |
| 984263bc | 240 | return(error); |
| 3919ced0 | 241 | get_mplock(); |
| 5b287bba | 242 | error = vacl_set_acl((struct vnode *)fp->f_data, uap->type, uap->aclp); |
| 3919ced0 | 243 | rel_mplock(); |
| 5b287bba MD |
244 | fdrop(fp); |
| 245 | return (error); | |
| 984263bc MD |
246 | } |
| 247 | ||
| 248 | /* | |
| 249 | * Given a file path, delete an ACL from it. | |
| 3919ced0 MD |
250 | * |
| 251 | * MPALMOSTSAFE | |
| 984263bc MD |
252 | */ |
| 253 | int | |
| 753fd850 | 254 | sys___acl_delete_file(struct __acl_delete_file_args *uap) |
| 984263bc | 255 | { |
| fad57d0e MD |
256 | struct nlookupdata nd; |
| 257 | struct vnode *vp; | |
| 984263bc MD |
258 | int error; |
| 259 | ||
| fad57d0e | 260 | vp = NULL; |
| 3919ced0 | 261 | get_mplock(); |
| ab2eb4eb | 262 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e MD |
263 | if (error == 0) |
| 264 | error = nlookup(&nd); | |
| 265 | if (error == 0) | |
| 28623bf9 | 266 | error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); |
| fad57d0e MD |
267 | nlookup_done(&nd); |
| 268 | ||
| 269 | if (error == 0) { | |
| ab2eb4eb | 270 | error = vacl_delete(vp, uap->type); |
| fad57d0e MD |
271 | vrele(vp); |
| 272 | } | |
| 3919ced0 MD |
273 | rel_mplock(); |
| 274 | ||
| 984263bc MD |
275 | return (error); |
| 276 | } | |
| 277 | ||
| 278 | /* | |
| 279 | * Given a file path, delete an ACL from it. | |
| 3919ced0 MD |
280 | * |
| 281 | * MPALMOSTSAFE | |
| 984263bc MD |
282 | */ |
| 283 | int | |
| 753fd850 | 284 | sys___acl_delete_fd(struct __acl_delete_fd_args *uap) |
| 984263bc | 285 | { |
| dadab5e9 | 286 | struct thread *td = curthread; |
| 984263bc MD |
287 | struct file *fp; |
| 288 | int error; | |
| 289 | ||
| dadab5e9 | 290 | KKASSERT(td->td_proc); |
| 5b287bba | 291 | if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) |
| 984263bc | 292 | return(error); |
| 3919ced0 | 293 | get_mplock(); |
| ab2eb4eb | 294 | error = vacl_delete((struct vnode *)fp->f_data, uap->type); |
| 3919ced0 | 295 | rel_mplock(); |
| 5b287bba | 296 | fdrop(fp); |
| 984263bc MD |
297 | return (error); |
| 298 | } | |
| 299 | ||
| 300 | /* | |
| 301 | * Given a file path, check an ACL for it | |
| 3919ced0 MD |
302 | * |
| 303 | * MPALMOSTSAFE | |
| 984263bc MD |
304 | */ |
| 305 | int | |
| 753fd850 | 306 | sys___acl_aclcheck_file(struct __acl_aclcheck_file_args *uap) |
| 984263bc | 307 | { |
| fad57d0e MD |
308 | struct nlookupdata nd; |
| 309 | struct vnode *vp; | |
| 310 | int error; | |
| 984263bc | 311 | |
| fad57d0e | 312 | vp = NULL; |
| 3919ced0 | 313 | get_mplock(); |
| ab2eb4eb | 314 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e MD |
315 | if (error == 0) |
| 316 | error = nlookup(&nd); | |
| 317 | if (error == 0) | |
| 28623bf9 | 318 | error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); |
| fad57d0e MD |
319 | nlookup_done(&nd); |
| 320 | ||
| 321 | if (error == 0) { | |
| ab2eb4eb | 322 | error = vacl_aclcheck(vp, uap->type, uap->aclp); |
| fad57d0e MD |
323 | vrele(vp); |
| 324 | } | |
| 3919ced0 | 325 | rel_mplock(); |
| 984263bc MD |
326 | return (error); |
| 327 | } | |
| 328 | ||
| 329 | /* | |
| 330 | * Given a file descriptor, check an ACL for it | |
| 3919ced0 MD |
331 | * |
| 332 | * MPALMOSTSAFE | |
| 984263bc MD |
333 | */ |
| 334 | int | |
| 753fd850 | 335 | sys___acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap) |
| 984263bc | 336 | { |
| dadab5e9 | 337 | struct thread *td = curthread; |
| 984263bc MD |
338 | struct file *fp; |
| 339 | int error; | |
| 340 | ||
| dadab5e9 | 341 | KKASSERT(td->td_proc); |
| 5b287bba | 342 | if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) |
| 984263bc | 343 | return(error); |
| 3919ced0 | 344 | get_mplock(); |
| 5b287bba | 345 | error = vacl_aclcheck((struct vnode *)fp->f_data, uap->type, uap->aclp); |
| 3919ced0 | 346 | rel_mplock(); |
| 5b287bba MD |
347 | fdrop(fp); |
| 348 | return (error); | |
| 984263bc | 349 | } |
| 5b287bba | 350 |