FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_export.c
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
39 */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD: releng/5.2/sys/kern/vfs_export.c 118045 2003-07-26 07:23:24Z scottl $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/mount.h>
51 #include <net/radix.h>
52 #include <sys/domain.h>
53 #include <sys/dirent.h>
54 #include <sys/vnode.h>
55
56 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
57
58 static void vfs_free_addrlist(struct netexport *nep);
59 static int vfs_free_netcred(struct radix_node *rn, void *w);
60 static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
61 struct export_args *argp);
62
63 /*
64 * Network address lookup element
65 */
66 struct netcred {
67 struct radix_node netc_rnodes[2];
68 int netc_exflags;
69 struct ucred netc_anon;
70 };
71
72 /*
73 * Network export information
74 */
75 struct netexport {
76 struct netcred ne_defexported; /* Default export */
77 struct radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
78 };
79
80 /*
81 * Build hash lists of net addresses and hang them off the mount point.
82 * Called by ufs_mount() to set up the lists of export addresses.
83 */
84 static int
85 vfs_hang_addrlist(mp, nep, argp)
86 struct mount *mp;
87 struct netexport *nep;
88 struct export_args *argp;
89 {
90 register struct netcred *np;
91 register struct radix_node_head *rnh;
92 register int i;
93 struct radix_node *rn;
94 struct sockaddr *saddr, *smask = 0;
95 struct domain *dom;
96 int error;
97
98 /*
99 * XXX: This routine converts from a `struct xucred'
100 * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This
101 * operation is questionable; for example, what should be done
102 * with fields like cr_uidinfo and cr_prison? Currently, this
103 * routine does not touch them (leaves them as NULL).
104 */
105 if (argp->ex_anon.cr_version != XUCRED_VERSION)
106 return (EINVAL);
107
108 if (argp->ex_addrlen == 0) {
109 if (mp->mnt_flag & MNT_DEFEXPORTED)
110 return (EPERM);
111 np = &nep->ne_defexported;
112 np->netc_exflags = argp->ex_flags;
113 bzero(&np->netc_anon, sizeof(np->netc_anon));
114 np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
115 np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
116 bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
117 sizeof(np->netc_anon.cr_groups));
118 np->netc_anon.cr_ref = 1;
119 mp->mnt_flag |= MNT_DEFEXPORTED;
120 return (0);
121 }
122
123 #if MSIZE <= 256
124 if (argp->ex_addrlen > MLEN)
125 return (EINVAL);
126 #endif
127
128 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
129 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
130 saddr = (struct sockaddr *) (np + 1);
131 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
132 goto out;
133 if (saddr->sa_len > argp->ex_addrlen)
134 saddr->sa_len = argp->ex_addrlen;
135 if (argp->ex_masklen) {
136 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
137 error = copyin(argp->ex_mask, smask, argp->ex_masklen);
138 if (error)
139 goto out;
140 if (smask->sa_len > argp->ex_masklen)
141 smask->sa_len = argp->ex_masklen;
142 }
143 i = saddr->sa_family;
144 if ((rnh = nep->ne_rtable[i]) == NULL) {
145 /*
146 * Seems silly to initialize every AF when most are not used,
147 * do so on demand here
148 */
149 for (dom = domains; dom; dom = dom->dom_next)
150 if (dom->dom_family == i && dom->dom_rtattach) {
151 dom->dom_rtattach((void **) &nep->ne_rtable[i],
152 dom->dom_rtoffset);
153 break;
154 }
155 if ((rnh = nep->ne_rtable[i]) == NULL) {
156 error = ENOBUFS;
157 goto out;
158 }
159 }
160 RADIX_NODE_HEAD_LOCK(rnh);
161 rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
162 RADIX_NODE_HEAD_UNLOCK(rnh);
163 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
164 error = EPERM;
165 goto out;
166 }
167 np->netc_exflags = argp->ex_flags;
168 bzero(&np->netc_anon, sizeof(np->netc_anon));
169 np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
170 np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
171 bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
172 sizeof(np->netc_anon.cr_groups));
173 np->netc_anon.cr_ref = 1;
174 return (0);
175 out:
176 free(np, M_NETADDR);
177 return (error);
178 }
179
180 /* Helper for vfs_free_addrlist. */
181 /* ARGSUSED */
182 static int
183 vfs_free_netcred(rn, w)
184 struct radix_node *rn;
185 void *w;
186 {
187 register struct radix_node_head *rnh = (struct radix_node_head *) w;
188
189 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
190 free(rn, M_NETADDR);
191 return (0);
192 }
193
194 /*
195 * Free the net address hash lists that are hanging off the mount points.
196 */
197 static void
198 vfs_free_addrlist(nep)
199 struct netexport *nep;
200 {
201 register int i;
202 register struct radix_node_head *rnh;
203
204 for (i = 0; i <= AF_MAX; i++)
205 if ((rnh = nep->ne_rtable[i])) {
206 RADIX_NODE_HEAD_LOCK(rnh);
207 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
208 RADIX_NODE_HEAD_DESTROY(rnh);
209 free(rnh, M_RTABLE);
210 nep->ne_rtable[i] = NULL; /* not SMP safe XXX */
211 }
212 }
213
214 /*
215 * High level function to manipulate export options on a mount point
216 * and the passed in netexport.
217 * Struct export_args *argp is the variable used to twiddle options,
218 * the structure is described in sys/mount.h
219 */
220 int
221 vfs_export(mp, argp)
222 struct mount *mp;
223 struct export_args *argp;
224 {
225 struct netexport *nep;
226 int error;
227
228 nep = mp->mnt_export;
229 if (argp->ex_flags & MNT_DELEXPORT) {
230 if (nep == NULL)
231 return (ENOENT);
232 if (mp->mnt_flag & MNT_EXPUBLIC) {
233 vfs_setpublicfs(NULL, NULL, NULL);
234 mp->mnt_flag &= ~MNT_EXPUBLIC;
235 }
236 vfs_free_addrlist(nep);
237 mp->mnt_export = NULL;
238 free(nep, M_MOUNT);
239 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
240 }
241 if (argp->ex_flags & MNT_EXPORTED) {
242 if (nep == NULL) {
243 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
244 mp->mnt_export = nep;
245 }
246 if (argp->ex_flags & MNT_EXPUBLIC) {
247 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
248 return (error);
249 mp->mnt_flag |= MNT_EXPUBLIC;
250 }
251 if ((error = vfs_hang_addrlist(mp, nep, argp)))
252 return (error);
253 mp->mnt_flag |= MNT_EXPORTED;
254 }
255 return (0);
256 }
257
258 /*
259 * Set the publicly exported filesystem (WebNFS). Currently, only
260 * one public filesystem is possible in the spec (RFC 2054 and 2055)
261 */
262 int
263 vfs_setpublicfs(mp, nep, argp)
264 struct mount *mp;
265 struct netexport *nep;
266 struct export_args *argp;
267 {
268 int error;
269 struct vnode *rvp;
270 char *cp;
271
272 /*
273 * mp == NULL -> invalidate the current info, the FS is
274 * no longer exported. May be called from either vfs_export
275 * or unmount, so check if it hasn't already been done.
276 */
277 if (mp == NULL) {
278 if (nfs_pub.np_valid) {
279 nfs_pub.np_valid = 0;
280 if (nfs_pub.np_index != NULL) {
281 FREE(nfs_pub.np_index, M_TEMP);
282 nfs_pub.np_index = NULL;
283 }
284 }
285 return (0);
286 }
287
288 /*
289 * Only one allowed at a time.
290 */
291 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
292 return (EBUSY);
293
294 /*
295 * Get real filehandle for root of exported FS.
296 */
297 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
298 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
299
300 if ((error = VFS_ROOT(mp, &rvp)))
301 return (error);
302
303 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
304 return (error);
305
306 vput(rvp);
307
308 /*
309 * If an indexfile was specified, pull it in.
310 */
311 if (argp->ex_indexfile != NULL) {
312 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
313 M_WAITOK);
314 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
315 MAXNAMLEN, (size_t *)0);
316 if (!error) {
317 /*
318 * Check for illegal filenames.
319 */
320 for (cp = nfs_pub.np_index; *cp; cp++) {
321 if (*cp == '/') {
322 error = EINVAL;
323 break;
324 }
325 }
326 }
327 if (error) {
328 FREE(nfs_pub.np_index, M_TEMP);
329 return (error);
330 }
331 }
332
333 nfs_pub.np_mount = mp;
334 nfs_pub.np_valid = 1;
335 return (0);
336 }
337
338 /*
339 * Used by the filesystems to determine if a given network address
340 * (passed in 'nam') is present in thier exports list, returns a pointer
341 * to struct netcred so that the filesystem can examine it for
342 * access rights (read/write/etc).
343 */
344 struct netcred *
345 vfs_export_lookup(mp, nam)
346 register struct mount *mp;
347 struct sockaddr *nam;
348 {
349 struct netexport *nep;
350 register struct netcred *np;
351 register struct radix_node_head *rnh;
352 struct sockaddr *saddr;
353
354 nep = mp->mnt_export;
355 if (nep == NULL)
356 return (NULL);
357 np = NULL;
358 if (mp->mnt_flag & MNT_EXPORTED) {
359 /*
360 * Lookup in the export list first.
361 */
362 if (nam != NULL) {
363 saddr = nam;
364 rnh = nep->ne_rtable[saddr->sa_family];
365 if (rnh != NULL) {
366 RADIX_NODE_HEAD_LOCK(rnh);
367 np = (struct netcred *)
368 (*rnh->rnh_matchaddr)(saddr, rnh);
369 RADIX_NODE_HEAD_UNLOCK(rnh);
370 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
371 np = NULL;
372 }
373 }
374 /*
375 * If no address match, use the default if it exists.
376 */
377 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
378 np = &nep->ne_defexported;
379 }
380 return (np);
381 }
382
383 /*
384 * XXX: This comment comes from the deprecated ufs_check_export()
385 * XXX: and may not entirely apply, but lacking something better:
386 * This is the generic part of fhtovp called after the underlying
387 * filesystem has validated the file handle.
388 *
389 * Verify that a host should have access to a filesystem.
390 */
391
392 int
393 vfs_stdcheckexp(mp, nam, extflagsp, credanonp)
394 struct mount *mp;
395 struct sockaddr *nam;
396 int *extflagsp;
397 struct ucred **credanonp;
398 {
399 struct netcred *np;
400
401 np = vfs_export_lookup(mp, nam);
402 if (np == NULL)
403 return (EACCES);
404 *extflagsp = np->netc_exflags;
405 *credanonp = &np->netc_anon;
406 return (0);
407 }
408
Cache object: eee3816b921724e913a262cccba72269
|