Merge from vendor branch READLINE:
[dragonfly.git] / sys / kern / kern_acl.c
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 $
27  * $DragonFly: src/sys/kern/kern_acl.c,v 1.7 2004/10/12 19:20:46 dillon Exp $
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>
42 #include <sys/proc.h>
43 #include <sys/namei.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>
49
50 static MALLOC_DEFINE(M_ACL, "acl", "access control list");
51
52 static int vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp);
53 static int vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp);
54 static int vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp);
55
56 /*
57  * These calls wrap the real vnode operations, and are called by the 
58  * syscall code once the syscall has converted the path or file
59  * descriptor to a vnode (unlocked).  The aclp pointer is assumed
60  * still to point to userland, so this should not be consumed within
61  * the kernel except by syscall code.  Other code should directly
62  * invoke VOP_{SET,GET}ACL.
63  */
64
65 /*
66  * Given a vnode, set its ACL.
67  */
68 static int
69 vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
70 {
71         struct thread *td = curthread;
72         struct acl inkernacl;
73         struct ucred *ucred;
74         int error;
75
76         error = copyin(aclp, &inkernacl, sizeof(struct acl));
77         if (error)
78                 return(error);
79         KKASSERT(td->td_proc);
80         ucred = td->td_proc->p_ucred;
81
82         VOP_LEASE(vp, td, ucred, LEASE_WRITE);
83         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
84         error = VOP_SETACL(vp, type, &inkernacl, ucred, td);
85         VOP_UNLOCK(vp, 0, td);
86         return(error);
87 }
88
89 /*
90  * Given a vnode, get its ACL.
91  */
92 static int
93 vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
94 {
95         struct thread *td = curthread;
96         struct acl inkernelacl;
97         struct ucred *ucred;
98         int error;
99
100         KKASSERT(td->td_proc);
101         ucred = td->td_proc->p_ucred;
102         error = VOP_GETACL(vp, type, &inkernelacl, ucred, td);
103         if (error == 0)
104                 error = copyout(&inkernelacl, aclp, sizeof(struct acl));
105         return (error);
106 }
107
108 /*
109  * Given a vnode, delete its ACL.
110  */
111 static int
112 vacl_delete(struct vnode *vp, acl_type_t type)
113 {
114         struct thread *td = curthread;
115         struct ucred *ucred;
116         int error;
117
118         KKASSERT(td->td_proc);
119         ucred = td->td_proc->p_ucred;
120         VOP_LEASE(vp, td, ucred, LEASE_WRITE);
121         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
122         error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, ucred, td);
123         VOP_UNLOCK(vp, 0, td);
124         return (error);
125 }
126
127 /*
128  * Given a vnode, check whether an ACL is appropriate for it
129  */
130 static int
131 vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp)
132 {
133         struct thread *td = curthread;
134         struct ucred *ucred;
135         struct acl inkernelacl;
136         int error;
137
138         KKASSERT(td->td_proc);
139         ucred = td->td_proc->p_ucred;
140         error = copyin(aclp, &inkernelacl, sizeof(struct acl));
141         if (error)
142                 return(error);
143         error = VOP_ACLCHECK(vp, type, &inkernelacl, ucred, td);
144         return (error);
145 }
146
147 /*
148  * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
149  * Don't need to lock, as the vacl_ code will get/release any locks
150  * required.
151  */
152
153 /*
154  * Given a file path, get an ACL for it
155  */
156 int
157 __acl_get_file(struct __acl_get_file_args *uap)
158 {
159         struct thread *td = curthread;
160         struct nameidata nd;
161         int error;
162
163         /* what flags are required here -- possible not LOCKLEAF? */
164         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW,
165             UIO_USERSPACE, SCARG(uap, path), td);
166         error = namei(&nd);
167         if (error)
168                 return(error);
169         error = vacl_get_acl(nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
170         NDFREE(&nd, 0);
171         return (error);
172 }
173
174 /*
175  * Given a file path, set an ACL for it
176  */
177 int
178 __acl_set_file(struct __acl_set_file_args *uap)
179 {
180         struct thread *td = curthread;
181         struct nameidata nd;
182         int error;
183
184         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, 
185             UIO_USERSPACE, SCARG(uap, path), td);
186         error = namei(&nd);
187         if (error)
188                 return(error);
189         error = vacl_set_acl(nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
190         NDFREE(&nd, 0);
191         return (error);
192 }
193
194 /*
195  * Given a file descriptor, get an ACL for it
196  */
197 int
198 __acl_get_fd(struct __acl_get_fd_args *uap)
199 {
200         struct thread *td = curthread;
201         struct file *fp;
202         int error;
203
204         KKASSERT(td->td_proc);
205         error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
206         if (error)
207                 return(error);
208         return vacl_get_acl((struct vnode *)fp->f_data, SCARG(uap, type),
209             SCARG(uap, aclp));
210 }
211
212 /*
213  * Given a file descriptor, set an ACL for it
214  */
215 int
216 __acl_set_fd(struct __acl_set_fd_args *uap)
217 {
218         struct thread *td = curthread;
219         struct file *fp;
220         int error;
221
222         KKASSERT(td->td_proc);
223         error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
224         if (error)
225                 return(error);
226         return vacl_set_acl((struct vnode *)fp->f_data, SCARG(uap, type),
227             SCARG(uap, aclp));
228 }
229
230 /*
231  * Given a file path, delete an ACL from it.
232  */
233 int
234 __acl_delete_file(struct __acl_delete_file_args *uap)
235 {
236         struct thread *td = curthread;
237         struct nameidata nd;
238         int error;
239
240         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW,
241             UIO_USERSPACE, SCARG(uap, path), td);
242         error = namei(&nd);
243         if (error)
244                 return(error);
245         error = vacl_delete(nd.ni_vp, SCARG(uap, type));
246         NDFREE(&nd, 0);
247         return (error);
248 }
249
250 /*
251  * Given a file path, delete an ACL from it.
252  */
253 int
254 __acl_delete_fd(struct __acl_delete_fd_args *uap)
255 {
256         struct thread *td = curthread;
257         struct file *fp;
258         int error;
259
260         KKASSERT(td->td_proc);
261         error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
262         if (error)
263                 return(error);
264         error = vacl_delete((struct vnode *)fp->f_data, SCARG(uap, type));
265         return (error);
266 }
267
268 /*
269  * Given a file path, check an ACL for it
270  */
271 int
272 __acl_aclcheck_file(struct __acl_aclcheck_file_args *uap)
273 {
274         struct thread *td = curthread;
275         struct nameidata        nd;
276         int     error;
277
278         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW,
279             UIO_USERSPACE, SCARG(uap, path), td);
280         error = namei(&nd);
281         if (error)
282                 return(error);
283         error = vacl_aclcheck(nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
284         NDFREE(&nd, 0);
285         return (error);
286 }
287
288 /*
289  * Given a file descriptor, check an ACL for it
290  */
291 int
292 __acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap)
293 {
294         struct thread *td = curthread;
295         struct file *fp;
296         int error;
297
298         KKASSERT(td->td_proc);
299         error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
300         if (error)
301                 return(error);
302         return vacl_aclcheck((struct vnode *)fp->f_data, SCARG(uap, type),
303             SCARG(uap, aclp));
304 }