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