Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sys / kern / vfs_helper.c
1 /*
2  * (The copyright below applies to ufs_access())
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993, 1995
5  *      The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the University of
23  *      California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * @(#)ufs_vnops.c      8.27 (Berkeley) 5/27/95
41  * $DragonFly: src/sys/kern/vfs_helper.c,v 1.1 2007/11/02 19:54:14 dillon Exp $
42  */
43
44 #include "opt_quota.h"
45 #include "opt_suiddir.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/stat.h>
52 #include <sys/mount.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/file.h>           /* XXX */
56
57 /*
58  * vop_helper_access()
59  *
60  *      Provide standard UNIX semanics for VOP_ACCESS, but without the quota
61  *      code.  This procedure was basically pulled out of UFS.
62  */
63 int
64 vop_helper_access(struct vop_access_args *ap, uid_t ino_uid, gid_t ino_gid,
65                   mode_t ino_mode, u_int32_t ino_flags)
66 {
67         struct vnode *vp = ap->a_vp;
68         struct ucred *cred = ap->a_cred;
69         mode_t mask, mode = ap->a_mode;
70         gid_t *gp;
71         int i;
72 #ifdef QUOTA
73         /*int error;*/
74 #endif
75
76         /*
77          * Disallow write attempts on read-only filesystems;
78          * unless the file is a socket, fifo, or a block or
79          * character device resident on the filesystem.
80          */
81         if (mode & VWRITE) {
82                 switch (vp->v_type) {
83                 case VDIR:
84                 case VLNK:
85                 case VREG:
86                 case VDATABASE:
87                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
88                                 return (EROFS);
89 #ifdef QUOTA
90                         /* check quota here XXX */
91 #endif
92                         break;
93                 default:
94                         break;
95                 }
96         }
97
98         /* If immutable bit set, nobody gets to write it. */
99         if ((mode & VWRITE) && (ino_flags & IMMUTABLE))
100                 return (EPERM);
101
102         /* Otherwise, user id 0 always gets access. */
103         if (cred->cr_uid == 0)
104                 return (0);
105
106         mask = 0;
107
108         /* Otherwise, check the owner. */
109         if (cred->cr_uid == ino_uid) {
110                 if (mode & VEXEC)
111                         mask |= S_IXUSR;
112                 if (mode & VREAD)
113                         mask |= S_IRUSR;
114                 if (mode & VWRITE)
115                         mask |= S_IWUSR;
116                 return ((ino_mode & mask) == mask ? 0 : EACCES);
117         }
118
119         /* Otherwise, check the groups. */
120         for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
121                 if (ino_gid == *gp) {
122                         if (mode & VEXEC)
123                                 mask |= S_IXGRP;
124                         if (mode & VREAD)
125                                 mask |= S_IRGRP;
126                         if (mode & VWRITE)
127                                 mask |= S_IWGRP;
128                         return ((ino_mode & mask) == mask ? 0 : EACCES);
129                 }
130
131         /* Otherwise, check everyone else. */
132         if (mode & VEXEC)
133                 mask |= S_IXOTH;
134         if (mode & VREAD)
135                 mask |= S_IROTH;
136         if (mode & VWRITE)
137                 mask |= S_IWOTH;
138         return ((ino_mode & mask) == mask ? 0 : EACCES);
139 }
140