proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[dragonfly.git] / sys / kern / kern_acl.c
CommitLineData
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 $
41c20dac 27 * $DragonFly: src/sys/kern/kern_acl.c,v 1.3 2003/06/23 17:55:41 dillon 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>
42#include <sys/namei.h>
43#include <sys/file.h>
44#include <sys/proc.h>
45#include <sys/sysent.h>
46#include <sys/errno.h>
47#include <sys/stat.h>
48#include <sys/acl.h>
49
50static MALLOC_DEFINE(M_ACL, "acl", "access control list");
51
41c20dac
MD
52static int vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp);
53static int vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp);
54static int vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp);
984263bc
MD
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 */
68static int
41c20dac 69vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
984263bc 70{
41c20dac 71 struct proc *p = curproc;
984263bc
MD
72 struct acl inkernacl;
73 int error;
74
75 error = copyin(aclp, &inkernacl, sizeof(struct acl));
76 if (error)
77 return(error);
78 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
79 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
80 error = VOP_SETACL(vp, type, &inkernacl, p->p_ucred, p);
81 VOP_UNLOCK(vp, 0, p);
82 return(error);
83}
84
85/*
86 * Given a vnode, get its ACL.
87 */
88static int
41c20dac 89vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
984263bc 90{
41c20dac 91 struct proc *p = curproc;
984263bc
MD
92 struct acl inkernelacl;
93 int error;
94
95 error = VOP_GETACL(vp, type, &inkernelacl, p->p_ucred, p);
96 if (error == 0)
97 error = copyout(&inkernelacl, aclp, sizeof(struct acl));
98 return (error);
99}
100
101/*
102 * Given a vnode, delete its ACL.
103 */
104static int
41c20dac 105vacl_delete(struct vnode *vp, acl_type_t type)
984263bc 106{
41c20dac 107 struct proc *p = curproc;
984263bc
MD
108 int error;
109
110 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
111 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
112 error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, p->p_ucred, p);
113 VOP_UNLOCK(vp, 0, p);
114 return (error);
115}
116
117/*
118 * Given a vnode, check whether an ACL is appropriate for it
119 */
120static int
41c20dac 121vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp)
984263bc 122{
41c20dac 123 struct proc *p = curproc;
984263bc
MD
124 struct acl inkernelacl;
125 int error;
126
127 error = copyin(aclp, &inkernelacl, sizeof(struct acl));
128 if (error)
129 return(error);
130 error = VOP_ACLCHECK(vp, type, &inkernelacl, p->p_ucred, p);
131 return (error);
132}
133
134/*
135 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
136 * Don't need to lock, as the vacl_ code will get/release any locks
137 * required.
138 */
139
140/*
141 * Given a file path, get an ACL for it
142 */
143int
41c20dac 144__acl_get_file(struct __acl_get_file_args *uap)
984263bc 145{
41c20dac 146 struct proc *p = curproc;
984263bc
MD
147 struct nameidata nd;
148 int error;
149
150 /* what flags are required here -- possible not LOCKLEAF? */
151 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
152 error = namei(&nd);
153 if (error)
154 return(error);
41c20dac 155 error = vacl_get_acl(nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
984263bc
MD
156 NDFREE(&nd, 0);
157 return (error);
158}
159
160/*
161 * Given a file path, set an ACL for it
162 */
163int
41c20dac 164__acl_set_file(struct __acl_set_file_args *uap)
984263bc 165{
41c20dac 166 struct proc *p = curproc;
984263bc
MD
167 struct nameidata nd;
168 int error;
169
170 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
171 error = namei(&nd);
172 if (error)
173 return(error);
41c20dac 174 error = vacl_set_acl(nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
984263bc
MD
175 NDFREE(&nd, 0);
176 return (error);
177}
178
179/*
180 * Given a file descriptor, get an ACL for it
181 */
182int
41c20dac 183__acl_get_fd(struct __acl_get_fd_args *uap)
984263bc 184{
41c20dac 185 struct proc *p = curproc;
984263bc
MD
186 struct file *fp;
187 int error;
188
189 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
190 if (error)
191 return(error);
41c20dac 192 return vacl_get_acl((struct vnode *)fp->f_data, SCARG(uap, type),
984263bc
MD
193 SCARG(uap, aclp));
194}
195
196/*
197 * Given a file descriptor, set an ACL for it
198 */
199int
41c20dac 200__acl_set_fd(struct __acl_set_fd_args *uap)
984263bc 201{
41c20dac 202 struct proc *p = curproc;
984263bc
MD
203 struct file *fp;
204 int error;
205
206 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
207 if (error)
208 return(error);
41c20dac 209 return vacl_set_acl((struct vnode *)fp->f_data, SCARG(uap, type),
984263bc
MD
210 SCARG(uap, aclp));
211}
212
213/*
214 * Given a file path, delete an ACL from it.
215 */
216int
41c20dac 217__acl_delete_file(struct __acl_delete_file_args *uap)
984263bc 218{
41c20dac 219 struct proc *p = curproc;
984263bc
MD
220 struct nameidata nd;
221 int error;
222
223 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
224 error = namei(&nd);
225 if (error)
226 return(error);
41c20dac 227 error = vacl_delete(nd.ni_vp, SCARG(uap, type));
984263bc
MD
228 NDFREE(&nd, 0);
229 return (error);
230}
231
232/*
233 * Given a file path, delete an ACL from it.
234 */
235int
41c20dac 236__acl_delete_fd(struct __acl_delete_fd_args *uap)
984263bc 237{
41c20dac 238 struct proc *p = curproc;
984263bc
MD
239 struct file *fp;
240 int error;
241
242 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
243 if (error)
244 return(error);
41c20dac 245 error = vacl_delete((struct vnode *)fp->f_data, SCARG(uap, type));
984263bc
MD
246 return (error);
247}
248
249/*
250 * Given a file path, check an ACL for it
251 */
252int
41c20dac 253__acl_aclcheck_file(struct __acl_aclcheck_file_args *uap)
984263bc 254{
41c20dac 255 struct proc *p = curproc;
984263bc
MD
256 struct nameidata nd;
257 int error;
258
259 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
260 error = namei(&nd);
261 if (error)
262 return(error);
41c20dac 263 error = vacl_aclcheck(nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
984263bc
MD
264 NDFREE(&nd, 0);
265 return (error);
266}
267
268/*
269 * Given a file descriptor, check an ACL for it
270 */
271int
41c20dac 272__acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap)
984263bc 273{
41c20dac 274 struct proc *p = curproc;
984263bc
MD
275 struct file *fp;
276 int error;
277
278 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
279 if (error)
280 return(error);
41c20dac 281 return vacl_aclcheck((struct vnode *)fp->f_data, SCARG(uap, type),
984263bc
MD
282 SCARG(uap, aclp));
283}