xref: /freebsd/share/man/man9/VOP_ACCESS.9 (revision 952d112864d8008aa87278a30a539d888a8493cd)
1.\" -*- nroff -*-
2.\" -*- nroff -*-
3.\"
4.\" Copyright (c) 1996 Doug Rabson
5.\"
6.\" All rights reserved.
7.\"
8.\" This program is free software.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
20.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
23.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29.\"
30.\" $Id: VOP_ACCESS.9,v 1.2 1997/03/04 06:20:41 mpp Exp $
31.\"
32.Dd July 24, 1996
33.Os
34.Dt VOP_ACCESS 9
35.Sh NAME
36.Nm VOP_ACCESS
37.Nd check access permissions of a file or Unix domain socket
38.Sh SYNOPSIS
39.Fd #include <sys/vnode.h>
40.Ft int
41.Fn VOP_ACCESS "struct vnode *vp" "int mode" "struct ucred *cred" "struct proc *p"
42.Sh DESCRIPTION
43This entry point checks the access permissions of the file against the
44given credentials.
45.Pp
46Its arguments are:
47.Bl -tag -width mode
48.It Ar vp
49the vnode of the file to check
50.It Ar mode
51the type of access required
52.It Ar cred
53the user credentials to check
54.It Ar p
55the process which is checking
56.El
57.Pp
58The
59.Fa mode
60is a mask which can contain
61.Dv VREAD ,
62.Dv VWRITE or
63.Dv VEXEC.
64.Sh LOCKS
65The vnode should be locked on entry.
66.Sh RETURN VALUES
67If the file is accessible in the specified way, then zero is returned,
68otherwise an appropriate error code is returned.
69.Sh PSEUDOCODE
70.Bd -literal
71int
72vop_access(struct vnode *vp, int mode, struct ucred *cred, struct proc *p)
73{
74    int error;
75
76    /*
77     * Disallow write attempts on read-only file systems;
78     * unless the file is a socket, fifo, or a block or
79     * character device resident on the file system.
80     */
81    if (mode & VWRITE) {
82	switch (vp->v_type) {
83	case VDIR:
84	case VLNK:
85	case VREG:
86	    if (vp->v_mount->mnt_flag & MNT_RDONLY)
87		return EROFS;
88
89	    break;
90	}
91    }
92
93    /* If immutable bit set, nobody gets to write it. */
94    if ((mode & VWRITE) && vp has immutable bit set)
95	return EPERM;
96
97    /* Otherwise, user id 0 always gets access. */
98    if (cred->cr_uid == 0)
99	return 0;
100
101    mask = 0;
102
103    /* Otherwise, check the owner. */
104    if (cred->cr_uid == owner of vp) {
105	if (mode & VEXEC)
106	    mask |= S_IXUSR;
107	if (mode & VREAD)
108	    mask |= S_IRUSR;
109	if (mode & VWRITE)
110	    mask |= S_IWUSR;
111	return (((mode of vp) & mask) == mask ? 0 : EACCES);
112    }
113
114    /* Otherwise, check the groups. */
115    for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
116	if (group of vp == *gp) {
117	    if (mode & VEXEC)
118		mask |= S_IXGRP;
119	    if (mode & VREAD)
120		mask |= S_IRGRP;
121	    if (mode & VWRITE)
122		mask |= S_IWGRP;
123	    return (((mode of vp) & mask) == mask ? 0 : EACCES);
124	}
125
126    /* Otherwise, check everyone else. */
127    if (mode & VEXEC)
128	mask |= S_IXOTH;
129    if (mode & VREAD)
130	mask |= S_IROTH;
131    if (mode & VWRITE)
132	mask |= S_IWOTH;
133    return (((mode of vp) & mask) == mask ? 0 : EACCES);
134}
135.Ed
136.Sh ERRORS
137.Bl -tag -width Er
138.It Bq Er EPERM
139An attempt was made to change an immutable file
140.It Bq Er EACCES
141Permission denied
142.El
143.Sh SEE ALSO
144.Xr vnode 9
145.Sh AUTHORS
146This man page was written by Doug Rabson.
147