kernel - use new td_ucred in numerous places
[dragonfly.git] / sys / kern / kern_acl.c
... / ...
CommitLineData
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.17 2007/02/19 00:51:54 swildner 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/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>
49
50static int vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp);
51static int vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp);
52static int vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp);
53
54/*
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.
61 */
62
63/*
64 * Given a vnode, set its ACL.
65 */
66static int
67vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
68{
69 struct thread *td = curthread;
70 struct acl inkernacl;
71 struct ucred *ucred;
72 int error;
73
74 error = copyin(aclp, &inkernacl, sizeof(struct acl));
75 if (error)
76 return(error);
77 ucred = td->td_ucred;
78
79 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
80 error = VOP_SETACL(vp, type, &inkernacl, ucred);
81 vn_unlock(vp);
82 return(error);
83}
84
85/*
86 * Given a vnode, get its ACL.
87 */
88static int
89vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
90{
91 struct thread *td = curthread;
92 struct acl inkernelacl;
93 struct ucred *ucred;
94 int error;
95
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);
101}
102
103/*
104 * Given a vnode, delete its ACL.
105 */
106static int
107vacl_delete(struct vnode *vp, acl_type_t type)
108{
109 struct thread *td = curthread;
110 struct ucred *ucred;
111 int error;
112
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);
118}
119
120/*
121 * Given a vnode, check whether an ACL is appropriate for it
122 */
123static int
124vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp)
125{
126 struct thread *td = curthread;
127 struct ucred *ucred;
128 struct acl inkernelacl;
129 int error;
130
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);
137}
138
139/*
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.
143 */
144
145/*
146 * Given a file path, get an ACL for it
147 *
148 * MPALMOSTSAFE
149 */
150int
151sys___acl_get_file(struct __acl_get_file_args *uap)
152{
153 struct nlookupdata nd;
154 struct vnode *vp;
155 int error;
156
157 vp = NULL;
158 get_mplock();
159 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
160 if (error == 0)
161 error = nlookup(&nd);
162 if (error == 0)
163 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
164 nlookup_done(&nd);
165 if (error == 0) {
166 error = vacl_get_acl(vp, uap->type, uap->aclp);
167 vrele(vp);
168 }
169 rel_mplock();
170
171 return (error);
172}
173
174/*
175 * Given a file path, set an ACL for it
176 *
177 * MPALMOSTSAFE
178 */
179int
180sys___acl_set_file(struct __acl_set_file_args *uap)
181{
182 struct nlookupdata nd;
183 struct vnode *vp;
184 int error;
185
186 vp = NULL;
187 get_mplock();
188 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
189 if (error == 0)
190 error = nlookup(&nd);
191 if (error == 0)
192 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
193 nlookup_done(&nd);
194 if (error == 0) {
195 error = vacl_set_acl(vp, uap->type, uap->aclp);
196 vrele(vp);
197 }
198 rel_mplock();
199
200 return (error);
201}
202
203/*
204 * Given a file descriptor, get an ACL for it
205 *
206 * MPALMOSTSAFE
207 */
208int
209sys___acl_get_fd(struct __acl_get_fd_args *uap)
210{
211 struct thread *td = curthread;
212 struct file *fp;
213 int error;
214
215 KKASSERT(td->td_proc);
216 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0)
217 return(error);
218 get_mplock();
219 error = vacl_get_acl((struct vnode *)fp->f_data, uap->type, uap->aclp);
220 rel_mplock();
221 fdrop(fp);
222
223 return (error);
224}
225
226/*
227 * Given a file descriptor, set an ACL for it
228 *
229 * MPALMOSTSAFE
230 */
231int
232sys___acl_set_fd(struct __acl_set_fd_args *uap)
233{
234 struct thread *td = curthread;
235 struct file *fp;
236 int error;
237
238 KKASSERT(td->td_proc);
239 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0)
240 return(error);
241 get_mplock();
242 error = vacl_set_acl((struct vnode *)fp->f_data, uap->type, uap->aclp);
243 rel_mplock();
244 fdrop(fp);
245 return (error);
246}
247
248/*
249 * Given a file path, delete an ACL from it.
250 *
251 * MPALMOSTSAFE
252 */
253int
254sys___acl_delete_file(struct __acl_delete_file_args *uap)
255{
256 struct nlookupdata nd;
257 struct vnode *vp;
258 int error;
259
260 vp = NULL;
261 get_mplock();
262 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
263 if (error == 0)
264 error = nlookup(&nd);
265 if (error == 0)
266 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
267 nlookup_done(&nd);
268
269 if (error == 0) {
270 error = vacl_delete(vp, uap->type);
271 vrele(vp);
272 }
273 rel_mplock();
274
275 return (error);
276}
277
278/*
279 * Given a file path, delete an ACL from it.
280 *
281 * MPALMOSTSAFE
282 */
283int
284sys___acl_delete_fd(struct __acl_delete_fd_args *uap)
285{
286 struct thread *td = curthread;
287 struct file *fp;
288 int error;
289
290 KKASSERT(td->td_proc);
291 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0)
292 return(error);
293 get_mplock();
294 error = vacl_delete((struct vnode *)fp->f_data, uap->type);
295 rel_mplock();
296 fdrop(fp);
297 return (error);
298}
299
300/*
301 * Given a file path, check an ACL for it
302 *
303 * MPALMOSTSAFE
304 */
305int
306sys___acl_aclcheck_file(struct __acl_aclcheck_file_args *uap)
307{
308 struct nlookupdata nd;
309 struct vnode *vp;
310 int error;
311
312 vp = NULL;
313 get_mplock();
314 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
315 if (error == 0)
316 error = nlookup(&nd);
317 if (error == 0)
318 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
319 nlookup_done(&nd);
320
321 if (error == 0) {
322 error = vacl_aclcheck(vp, uap->type, uap->aclp);
323 vrele(vp);
324 }
325 rel_mplock();
326 return (error);
327}
328
329/*
330 * Given a file descriptor, check an ACL for it
331 *
332 * MPALMOSTSAFE
333 */
334int
335sys___acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap)
336{
337 struct thread *td = curthread;
338 struct file *fp;
339 int error;
340
341 KKASSERT(td->td_proc);
342 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0)
343 return(error);
344 get_mplock();
345 error = vacl_aclcheck((struct vnode *)fp->f_data, uap->type, uap->aclp);
346 rel_mplock();
347 fdrop(fp);
348 return (error);
349}
350