xref: /netbsd-src/sys/miscfs/umapfs/umap_subr.c (revision 06be8101a16cc95f40783b3cb7afd12112103a9a)
1 /*	$NetBSD: umap_subr.c,v 1.18 2001/11/10 13:33:45 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 National Aeronautics & Space Administration
5  * All rights reserved.
6  *
7  * This software was written by William Studenmund of the
8  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
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  * 3. Neither the name of the National Aeronautics & Space Administration
19  *    nor the names of its contributors may be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*
36  * Copyright (c) 1992, 1993, 1995
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * This code is derived from software donated to Berkeley by
40  * Jan-Simon Pendry.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *	This product includes software developed by the University of
53  *	California, Berkeley and its contributors.
54  * 4. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	from: Id: lofs_subr.c, v 1.11 1992/05/30 10:05:43 jsp Exp
71  *	@(#)umap_subr.c	8.9 (Berkeley) 5/14/95
72  */
73 
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: umap_subr.c,v 1.18 2001/11/10 13:33:45 lukem Exp $");
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80 #include <sys/time.h>
81 #include <sys/types.h>
82 #include <sys/vnode.h>
83 #include <sys/mount.h>
84 #include <sys/namei.h>
85 #include <sys/malloc.h>
86 #include <miscfs/specfs/specdev.h>
87 #include <miscfs/umapfs/umap.h>
88 
89 u_long umap_findid __P((u_long, u_long [][2], int));
90 int umap_node_alloc __P((struct mount *, struct vnode *,
91 				struct vnode **));
92 
93 /*
94  * umap_findid is called by various routines in umap_vnodeops.c to
95  * find a user or group id in a map.
96  */
97 u_long
98 umap_findid(id, map, nentries)
99 	u_long id;
100 	u_long map[][2];
101 	int nentries;
102 {
103 	int i;
104 
105 	/* Find uid entry in map */
106 	i = 0;
107 	while ((i<nentries) && ((map[i][0]) != id))
108 		i++;
109 
110 	if (i < nentries)
111 		return (map[i][1]);
112 	else
113 		return (-1);
114 
115 }
116 
117 /*
118  * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to
119  * find a user or group id in a map, in reverse.
120  */
121 u_long
122 umap_reverse_findid(id, map, nentries)
123 	u_long id;
124 	u_long map[][2];
125 	int nentries;
126 {
127 	int i;
128 
129 	/* Find uid entry in map */
130 	i = 0;
131 	while ((i<nentries) && ((map[i][1]) != id))
132 		i++;
133 
134 	if (i < nentries)
135 		return (map[i][0]);
136 	else
137 		return (-1);
138 
139 }
140 
141 /* umap_mapids maps all of the ids in a credential, both user and group. */
142 
143 void
144 umap_mapids(v_mount, credp)
145 	struct mount *v_mount;
146 	struct ucred *credp;
147 {
148 	int i, unentries, gnentries;
149 	uid_t uid;
150 	gid_t gid;
151 	u_long (*usermap)[2], (*groupmap)[2];
152 
153 	if (credp == NOCRED)
154 		return;
155 
156 	unentries =  MOUNTTOUMAPMOUNT(v_mount)->info_nentries;
157 	usermap =  MOUNTTOUMAPMOUNT(v_mount)->info_mapdata;
158 	gnentries =  MOUNTTOUMAPMOUNT(v_mount)->info_gnentries;
159 	groupmap =  MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata;
160 
161 	/* Find uid entry in map */
162 
163 	uid = (uid_t) umap_findid(credp->cr_uid, usermap, unentries);
164 
165 	if (uid != -1)
166 		credp->cr_uid = uid;
167 	else
168 		credp->cr_uid = (uid_t) NOBODY;
169 
170 #if 1
171 	/* cr_gid is the same as cr_groups[0] in 4BSD, but not in NetBSD */
172 
173 	/* Find gid entry in map */
174 
175 	gid = (gid_t) umap_findid(credp->cr_gid, groupmap, gnentries);
176 
177 	if (gid != -1)
178 		credp->cr_gid = gid;
179 	else
180 		credp->cr_gid = NULLGROUP;
181 #endif
182 
183 	/* Now we must map each of the set of groups in the cr_groups
184 		structure. */
185 
186 	for(i=0; i < credp->cr_ngroups; i++) {
187 		gid = (gid_t) umap_findid(credp->cr_groups[i],
188 					  groupmap, gnentries);
189 
190 		if (gid != -1)
191 			credp->cr_groups[i] = gid;
192 		else
193 			credp->cr_groups[i] = NULLGROUP;
194 	}
195 }
196