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