xref: /dragonfly/sys/kern/kern_acl.c (revision 41c20dac)
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.3 2003/06/23 17:55:41 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/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 
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 proc *p = curproc;
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  */
88 static int
89 vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
90 {
91 	struct proc *p = curproc;
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  */
104 static int
105 vacl_delete(struct vnode *vp, acl_type_t type)
106 {
107 	struct proc *p = curproc;
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  */
120 static int
121 vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp)
122 {
123 	struct proc *p = curproc;
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  */
143 int
144 __acl_get_file(struct __acl_get_file_args *uap)
145 {
146 	struct proc *p = curproc;
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);
155 	error = vacl_get_acl(nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
156 	NDFREE(&nd, 0);
157 	return (error);
158 }
159 
160 /*
161  * Given a file path, set an ACL for it
162  */
163 int
164 __acl_set_file(struct __acl_set_file_args *uap)
165 {
166 	struct proc *p = curproc;
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(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  */
182 int
183 __acl_get_fd(struct __acl_get_fd_args *uap)
184 {
185 	struct proc *p = curproc;
186 	struct file *fp;
187 	int error;
188 
189 	error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
190 	if (error)
191 		return(error);
192 	return vacl_get_acl((struct vnode *)fp->f_data, SCARG(uap, type),
193 	    SCARG(uap, aclp));
194 }
195 
196 /*
197  * Given a file descriptor, set an ACL for it
198  */
199 int
200 __acl_set_fd(struct __acl_set_fd_args *uap)
201 {
202 	struct proc *p = curproc;
203 	struct file *fp;
204 	int error;
205 
206 	error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
207 	if (error)
208 		return(error);
209 	return vacl_set_acl((struct vnode *)fp->f_data, SCARG(uap, type),
210 	    SCARG(uap, aclp));
211 }
212 
213 /*
214  * Given a file path, delete an ACL from it.
215  */
216 int
217 __acl_delete_file(struct __acl_delete_file_args *uap)
218 {
219 	struct proc *p = curproc;
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);
227 	error = vacl_delete(nd.ni_vp, SCARG(uap, type));
228 	NDFREE(&nd, 0);
229 	return (error);
230 }
231 
232 /*
233  * Given a file path, delete an ACL from it.
234  */
235 int
236 __acl_delete_fd(struct __acl_delete_fd_args *uap)
237 {
238 	struct proc *p = curproc;
239 	struct file *fp;
240 	int error;
241 
242 	error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
243 	if (error)
244 		return(error);
245 	error = vacl_delete((struct vnode *)fp->f_data, SCARG(uap, type));
246 	return (error);
247 }
248 
249 /*
250  * Given a file path, check an ACL for it
251  */
252 int
253 __acl_aclcheck_file(struct __acl_aclcheck_file_args *uap)
254 {
255 	struct proc *p = curproc;
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);
263 	error = vacl_aclcheck(nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
264 	NDFREE(&nd, 0);
265 	return (error);
266 }
267 
268 /*
269  * Given a file descriptor, check an ACL for it
270  */
271 int
272 __acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap)
273 {
274 	struct proc *p = curproc;
275 	struct file *fp;
276 	int error;
277 
278 	error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
279 	if (error)
280 		return(error);
281 	return vacl_aclcheck((struct vnode *)fp->f_data, SCARG(uap, type),
282 	    SCARG(uap, aclp));
283 }
284