ums: fix incorrect mouse button reporting via evdev
[dragonfly.git] / sys / kern / kern_acl.c
blob1c9e0917fc54d954a0aae7ba8084c165fcb8003d
1 /*-
2 * Copyright (c) 1999, 2000 Robert N. M. Watson
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/sys/kern/kern_acl.c,v 1.2.2.1 2000/07/28 18:48:16 rwatson Exp $
27 * $DragonFly: src/sys/kern/kern_acl.c,v 1.17 2007/02/19 00:51:54 swildner Exp $
31 * Generic routines to support file system ACLs, at a syntactic level
32 * Semantics are the responsibility of the underlying file system
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysmsg.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/vnode.h>
41 #include <sys/lock.h>
42 #include <sys/proc.h>
43 #include <sys/nlookup.h>
44 #include <sys/file.h>
45 #include <sys/sysent.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48 #include <sys/acl.h>
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);
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.
64 * Given a vnode, set its ACL.
66 static int
67 vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
69 struct thread *td = curthread;
70 struct acl inkernacl;
71 struct ucred *ucred;
72 int error;
74 error = copyin(aclp, &inkernacl, sizeof(struct acl));
75 if (error)
76 return(error);
77 ucred = td->td_ucred;
79 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
80 error = VOP_SETACL(vp, type, &inkernacl, ucred);
81 vn_unlock(vp);
82 return(error);
86 * Given a vnode, get its ACL.
88 static int
89 vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
91 struct thread *td = curthread;
92 struct acl inkernelacl;
93 struct ucred *ucred;
94 int error;
96 ucred = td->td_ucred;
97 error = VOP_GETACL(vp, type, &inkernelacl, ucred);
98 if (error == 0)
99 error = copyout(&inkernelacl, aclp, sizeof(struct acl));
100 return (error);
104 * Given a vnode, delete its ACL.
106 static int
107 vacl_delete(struct vnode *vp, acl_type_t type)
109 struct thread *td = curthread;
110 struct ucred *ucred;
111 int error;
113 ucred = td->td_ucred;
114 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
115 error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, ucred);
116 vn_unlock(vp);
117 return (error);
121 * Given a vnode, check whether an ACL is appropriate for it
123 static int
124 vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp)
126 struct thread *td = curthread;
127 struct ucred *ucred;
128 struct acl inkernelacl;
129 int error;
131 ucred = td->td_ucred;
132 error = copyin(aclp, &inkernelacl, sizeof(struct acl));
133 if (error)
134 return(error);
135 error = VOP_ACLCHECK(vp, type, &inkernelacl, ucred);
136 return (error);
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.
146 * Given a file path, get an ACL for it
149 sys___acl_get_file(struct sysmsg *sysmsg,
150 const struct __acl_get_file_args *uap)
152 struct nlookupdata nd;
153 struct vnode *vp;
154 int error;
156 vp = NULL;
157 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
158 if (error == 0)
159 error = nlookup(&nd);
160 if (error == 0)
161 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
162 nlookup_done(&nd);
163 if (error == 0) {
164 error = vacl_get_acl(vp, uap->type, uap->aclp);
165 vrele(vp);
167 return (error);
171 * Given a file path, set an ACL for it
174 sys___acl_set_file(struct sysmsg *sysmsg,
175 const struct __acl_set_file_args *uap)
177 struct nlookupdata nd;
178 struct vnode *vp;
179 int error;
181 vp = NULL;
182 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
183 if (error == 0)
184 error = nlookup(&nd);
185 if (error == 0)
186 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
187 nlookup_done(&nd);
188 if (error == 0) {
189 error = vacl_set_acl(vp, uap->type, uap->aclp);
190 vrele(vp);
192 return (error);
196 * Given a file descriptor, get an ACL for it
199 sys___acl_get_fd(struct sysmsg *sysmsg,
200 const struct __acl_get_fd_args *uap)
202 struct thread *td = curthread;
203 struct file *fp;
204 int error;
206 if ((error = holdvnode(td, uap->filedes, &fp)) != 0)
207 return(error);
208 error = vacl_get_acl((struct vnode *)fp->f_data, uap->type, uap->aclp);
209 fdrop(fp);
211 return (error);
215 * Given a file descriptor, set an ACL for it
218 sys___acl_set_fd(struct sysmsg *sysmsg,
219 const struct __acl_set_fd_args *uap)
221 struct thread *td = curthread;
222 struct file *fp;
223 int error;
225 if ((error = holdvnode(td, uap->filedes, &fp)) != 0)
226 return(error);
227 error = vacl_set_acl((struct vnode *)fp->f_data, uap->type, uap->aclp);
228 fdrop(fp);
229 return (error);
233 * Given a file path, delete an ACL from it.
236 sys___acl_delete_file(struct sysmsg *sysmsg,
237 const struct __acl_delete_file_args *uap)
239 struct nlookupdata nd;
240 struct vnode *vp;
241 int error;
243 vp = NULL;
244 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
245 if (error == 0)
246 error = nlookup(&nd);
247 if (error == 0)
248 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
249 nlookup_done(&nd);
251 if (error == 0) {
252 error = vacl_delete(vp, uap->type);
253 vrele(vp);
255 return (error);
259 * Given a file path, delete an ACL from it.
262 sys___acl_delete_fd(struct sysmsg *sysmsg,
263 const struct __acl_delete_fd_args *uap)
265 struct thread *td = curthread;
266 struct file *fp;
267 int error;
269 KKASSERT(td->td_proc);
270 if ((error = holdvnode(td, uap->filedes, &fp)) != 0)
271 return(error);
272 error = vacl_delete((struct vnode *)fp->f_data, uap->type);
273 fdrop(fp);
274 return (error);
278 * Given a file path, check an ACL for it
281 sys___acl_aclcheck_file(struct sysmsg *sysmsg,
282 const struct __acl_aclcheck_file_args *uap)
284 struct nlookupdata nd;
285 struct vnode *vp;
286 int error;
288 vp = NULL;
289 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
290 if (error == 0)
291 error = nlookup(&nd);
292 if (error == 0)
293 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
294 nlookup_done(&nd);
296 if (error == 0) {
297 error = vacl_aclcheck(vp, uap->type, uap->aclp);
298 vrele(vp);
300 return (error);
304 * Given a file descriptor, check an ACL for it
307 sys___acl_aclcheck_fd(struct sysmsg *sysmsg,
308 const struct __acl_aclcheck_fd_args *uap)
310 struct thread *td = curthread;
311 struct file *fp;
312 int error;
314 KKASSERT(td->td_proc);
315 if ((error = holdvnode(td, uap->filedes, &fp)) != 0)
316 return(error);
317 error = vacl_aclcheck((struct vnode *)fp->f_data, uap->type, uap->aclp);
318 fdrop(fp);
319 return (error);