Remove PC98 keymaps.
[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 $
fad57d0e 27 * $DragonFly: src/sys/kern/kern_acl.c,v 1.8 2004/11/12 00:09:23 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>
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
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{
dadab5e9 71 struct thread *td = curthread;
984263bc 72 struct acl inkernacl;
dadab5e9 73 struct ucred *ucred;
984263bc
MD
74 int error;
75
76 error = copyin(aclp, &inkernacl, sizeof(struct acl));
77 if (error)
78 return(error);
dadab5e9
MD
79 KKASSERT(td->td_proc);
80 ucred = td->td_proc->p_ucred;
81
82 VOP_LEASE(vp, td, ucred, LEASE_WRITE);
5fd012e0 83 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
dadab5e9 84 error = VOP_SETACL(vp, type, &inkernacl, ucred, td);
5fd012e0 85 VOP_UNLOCK(vp, 0, td);
984263bc
MD
86 return(error);
87}
88
89/*
90 * Given a vnode, get its ACL.
91 */
92static int
41c20dac 93vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
984263bc 94{
dadab5e9 95 struct thread *td = curthread;
984263bc 96 struct acl inkernelacl;
dadab5e9 97 struct ucred *ucred;
984263bc
MD
98 int error;
99
dadab5e9
MD
100 KKASSERT(td->td_proc);
101 ucred = td->td_proc->p_ucred;
102 error = VOP_GETACL(vp, type, &inkernelacl, ucred, td);
984263bc
MD
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 */
111static int
41c20dac 112vacl_delete(struct vnode *vp, acl_type_t type)
984263bc 113{
dadab5e9
MD
114 struct thread *td = curthread;
115 struct ucred *ucred;
984263bc
MD
116 int error;
117
dadab5e9
MD
118 KKASSERT(td->td_proc);
119 ucred = td->td_proc->p_ucred;
120 VOP_LEASE(vp, td, ucred, LEASE_WRITE);
5fd012e0 121 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
dadab5e9 122 error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, ucred, td);
5fd012e0 123 VOP_UNLOCK(vp, 0, td);
984263bc
MD
124 return (error);
125}
126
127/*
128 * Given a vnode, check whether an ACL is appropriate for it
129 */
130static int
41c20dac 131vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp)
984263bc 132{
dadab5e9
MD
133 struct thread *td = curthread;
134 struct ucred *ucred;
984263bc
MD
135 struct acl inkernelacl;
136 int error;
137
dadab5e9
MD
138 KKASSERT(td->td_proc);
139 ucred = td->td_proc->p_ucred;
984263bc
MD
140 error = copyin(aclp, &inkernelacl, sizeof(struct acl));
141 if (error)
142 return(error);
dadab5e9 143 error = VOP_ACLCHECK(vp, type, &inkernelacl, ucred, td);
984263bc
MD
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 */
156int
41c20dac 157__acl_get_file(struct __acl_get_file_args *uap)
984263bc 158{
fad57d0e
MD
159 struct nlookupdata nd;
160 struct vnode *vp;
984263bc
MD
161 int error;
162
fad57d0e
MD
163 vp = NULL;
164 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
165 if (error == 0)
166 error = nlookup(&nd);
167 if (error == 0)
168 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
169 nlookup_done(&nd);
170 if (error == 0) {
171 error = vacl_get_acl(vp, SCARG(uap, type), SCARG(uap, aclp));
172 vrele(vp);
173 }
984263bc
MD
174 return (error);
175}
176
177/*
178 * Given a file path, set an ACL for it
179 */
180int
41c20dac 181__acl_set_file(struct __acl_set_file_args *uap)
984263bc 182{
fad57d0e
MD
183 struct nlookupdata nd;
184 struct vnode *vp;
984263bc
MD
185 int error;
186
fad57d0e
MD
187 vp = NULL;
188 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
189 if (error == 0)
190 error = nlookup(&nd);
191 if (error == 0)
192 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
193 nlookup_done(&nd);
194 if (error == 0) {
195 error = vacl_set_acl(vp, SCARG(uap, type), SCARG(uap, aclp));
196 vrele(vp);
197 }
984263bc
MD
198 return (error);
199}
200
201/*
202 * Given a file descriptor, get an ACL for it
203 */
204int
41c20dac 205__acl_get_fd(struct __acl_get_fd_args *uap)
984263bc 206{
dadab5e9 207 struct thread *td = curthread;
984263bc
MD
208 struct file *fp;
209 int error;
210
dadab5e9
MD
211 KKASSERT(td->td_proc);
212 error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
984263bc
MD
213 if (error)
214 return(error);
41c20dac 215 return vacl_get_acl((struct vnode *)fp->f_data, SCARG(uap, type),
984263bc
MD
216 SCARG(uap, aclp));
217}
218
219/*
220 * Given a file descriptor, set an ACL for it
221 */
222int
41c20dac 223__acl_set_fd(struct __acl_set_fd_args *uap)
984263bc 224{
dadab5e9 225 struct thread *td = curthread;
984263bc
MD
226 struct file *fp;
227 int error;
228
dadab5e9
MD
229 KKASSERT(td->td_proc);
230 error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
984263bc
MD
231 if (error)
232 return(error);
41c20dac 233 return vacl_set_acl((struct vnode *)fp->f_data, SCARG(uap, type),
984263bc
MD
234 SCARG(uap, aclp));
235}
236
237/*
238 * Given a file path, delete an ACL from it.
239 */
240int
41c20dac 241__acl_delete_file(struct __acl_delete_file_args *uap)
984263bc 242{
fad57d0e
MD
243 struct nlookupdata nd;
244 struct vnode *vp;
984263bc
MD
245 int error;
246
fad57d0e
MD
247 vp = NULL;
248 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
249 if (error == 0)
250 error = nlookup(&nd);
251 if (error == 0)
252 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
253 nlookup_done(&nd);
254
255 if (error == 0) {
256 error = vacl_delete(vp, SCARG(uap, type));
257 vrele(vp);
258 }
984263bc
MD
259 return (error);
260}
261
262/*
263 * Given a file path, delete an ACL from it.
264 */
265int
41c20dac 266__acl_delete_fd(struct __acl_delete_fd_args *uap)
984263bc 267{
dadab5e9 268 struct thread *td = curthread;
984263bc
MD
269 struct file *fp;
270 int error;
271
dadab5e9
MD
272 KKASSERT(td->td_proc);
273 error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
984263bc
MD
274 if (error)
275 return(error);
41c20dac 276 error = vacl_delete((struct vnode *)fp->f_data, SCARG(uap, type));
984263bc
MD
277 return (error);
278}
279
280/*
281 * Given a file path, check an ACL for it
282 */
283int
41c20dac 284__acl_aclcheck_file(struct __acl_aclcheck_file_args *uap)
984263bc 285{
fad57d0e
MD
286 struct nlookupdata nd;
287 struct vnode *vp;
288 int error;
984263bc 289
fad57d0e
MD
290 vp = NULL;
291 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
292 if (error == 0)
293 error = nlookup(&nd);
294 if (error == 0)
295 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
296 nlookup_done(&nd);
297
298 if (error == 0) {
299 error = vacl_aclcheck(vp, SCARG(uap, type), SCARG(uap, aclp));
300 vrele(vp);
301 }
984263bc
MD
302 return (error);
303}
304
305/*
306 * Given a file descriptor, check an ACL for it
307 */
308int
41c20dac 309__acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap)
984263bc 310{
dadab5e9 311 struct thread *td = curthread;
984263bc
MD
312 struct file *fp;
313 int error;
314
dadab5e9
MD
315 KKASSERT(td->td_proc);
316 error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
984263bc
MD
317 if (error)
318 return(error);
41c20dac 319 return vacl_aclcheck((struct vnode *)fp->f_data, SCARG(uap, type),
984263bc
MD
320 SCARG(uap, aclp));
321}