2 * Copyright (c) 1999, 2000 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.2 2003/06/17 04:28:41 dillon Exp $
31 * Generic routines to support file system ACLs, at a syntactic level
32 * Semantics are the responsibility of the underlying file system
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>
42 #include <sys/namei.h>
45 #include <sys/sysent.h>
46 #include <sys/errno.h>
50 static MALLOC_DEFINE(M_ACL, "acl", "access control list");
52 static int vacl_set_acl(struct proc *p, struct vnode *vp, acl_type_t type,
54 static int vacl_get_acl(struct proc *p, struct vnode *vp, acl_type_t type,
56 static int vacl_aclcheck(struct proc *p, struct vnode *vp, acl_type_t type,
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.
69 * Given a vnode, set its ACL.
72 vacl_set_acl(struct proc *p, struct vnode *vp, acl_type_t type,
78 error = copyin(aclp, &inkernacl, sizeof(struct acl));
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);
89 * Given a vnode, get its ACL.
92 vacl_get_acl(struct proc *p, struct vnode *vp, acl_type_t type,
95 struct acl inkernelacl;
98 error = VOP_GETACL(vp, type, &inkernelacl, p->p_ucred, p);
100 error = copyout(&inkernelacl, aclp, sizeof(struct acl));
105 * Given a vnode, delete its ACL.
108 vacl_delete(struct proc *p, struct vnode *vp, acl_type_t type)
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);
120 * Given a vnode, check whether an ACL is appropriate for it
123 vacl_aclcheck(struct proc *p, struct vnode *vp, acl_type_t type,
126 struct acl inkernelacl;
129 error = copyin(aclp, &inkernelacl, sizeof(struct acl));
132 error = VOP_ACLCHECK(vp, type, &inkernelacl, p->p_ucred, p);
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
143 * Given a file path, get an ACL for it
146 __acl_get_file(struct proc *p, struct __acl_get_file_args *uap)
151 /* what flags are required here -- possible not LOCKLEAF? */
152 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
156 error = vacl_get_acl(p, nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
162 * Given a file path, set an ACL for it
165 __acl_set_file(struct proc *p, struct __acl_set_file_args *uap)
170 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
174 error = vacl_set_acl(p, nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
180 * Given a file descriptor, get an ACL for it
183 __acl_get_fd(struct proc *p, struct __acl_get_fd_args *uap)
188 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
191 return vacl_get_acl(p, (struct vnode *)fp->f_data, SCARG(uap, type),
196 * Given a file descriptor, set an ACL for it
199 __acl_set_fd(struct proc *p, struct __acl_set_fd_args *uap)
204 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
207 return vacl_set_acl(p, (struct vnode *)fp->f_data, SCARG(uap, type),
212 * Given a file path, delete an ACL from it.
215 __acl_delete_file(struct proc *p, struct __acl_delete_file_args *uap)
220 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
224 error = vacl_delete(p, nd.ni_vp, SCARG(uap, type));
230 * Given a file path, delete an ACL from it.
233 __acl_delete_fd(struct proc *p, struct __acl_delete_fd_args *uap)
238 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
241 error = vacl_delete(p, (struct vnode *)fp->f_data, SCARG(uap, type));
246 * Given a file path, check an ACL for it
249 __acl_aclcheck_file(struct proc *p, struct __acl_aclcheck_file_args *uap)
254 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
258 error = vacl_aclcheck(p, nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
264 * Given a file descriptor, check an ACL for it
267 __acl_aclcheck_fd(struct proc *p, struct __acl_aclcheck_fd_args *uap)
272 error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
275 return vacl_aclcheck(p, (struct vnode *)fp->f_data, SCARG(uap, type),