xref: /freebsd/sys/kern/vfs_export.c (revision b52f49a9a0f22207ad5130ad8faba08de3ed23d8)
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$");
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 (argp->ex_addrlen > MLEN)
124 		return (EINVAL);
125 
126 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
127 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
128 	saddr = (struct sockaddr *) (np + 1);
129 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
130 		goto out;
131 	if (saddr->sa_len > argp->ex_addrlen)
132 		saddr->sa_len = argp->ex_addrlen;
133 	if (argp->ex_masklen) {
134 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
135 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
136 		if (error)
137 			goto out;
138 		if (smask->sa_len > argp->ex_masklen)
139 			smask->sa_len = argp->ex_masklen;
140 	}
141 	i = saddr->sa_family;
142 	if ((rnh = nep->ne_rtable[i]) == NULL) {
143 		/*
144 		 * Seems silly to initialize every AF when most are not used,
145 		 * do so on demand here
146 		 */
147 		for (dom = domains; dom; dom = dom->dom_next)
148 			if (dom->dom_family == i && dom->dom_rtattach) {
149 				dom->dom_rtattach((void **) &nep->ne_rtable[i],
150 				    dom->dom_rtoffset);
151 				break;
152 			}
153 		if ((rnh = nep->ne_rtable[i]) == NULL) {
154 			error = ENOBUFS;
155 			goto out;
156 		}
157 	}
158 	RADIX_NODE_HEAD_LOCK(rnh);
159 	rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
160 	RADIX_NODE_HEAD_UNLOCK(rnh);
161 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
162 		error = EPERM;
163 		goto out;
164 	}
165 	np->netc_exflags = argp->ex_flags;
166 	bzero(&np->netc_anon, sizeof(np->netc_anon));
167 	np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
168 	np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
169 	bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
170 	    sizeof(np->netc_anon.cr_groups));
171 	np->netc_anon.cr_ref = 1;
172 	return (0);
173 out:
174 	free(np, M_NETADDR);
175 	return (error);
176 }
177 
178 /* Helper for vfs_free_addrlist. */
179 /* ARGSUSED */
180 static int
181 vfs_free_netcred(rn, w)
182 	struct radix_node *rn;
183 	void *w;
184 {
185 	register struct radix_node_head *rnh = (struct radix_node_head *) w;
186 
187 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
188 	free(rn, M_NETADDR);
189 	return (0);
190 }
191 
192 /*
193  * Free the net address hash lists that are hanging off the mount points.
194  */
195 static void
196 vfs_free_addrlist(nep)
197 	struct netexport *nep;
198 {
199 	register int i;
200 	register struct radix_node_head *rnh;
201 
202 	for (i = 0; i <= AF_MAX; i++)
203 		if ((rnh = nep->ne_rtable[i])) {
204 			RADIX_NODE_HEAD_LOCK(rnh);
205 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
206 			RADIX_NODE_HEAD_DESTROY(rnh);
207 			free(rnh, M_RTABLE);
208 			nep->ne_rtable[i] = NULL;	/* not SMP safe XXX */
209 		}
210 }
211 
212 /*
213  * High level function to manipulate export options on a mount point
214  * and the passed in netexport.
215  * Struct export_args *argp is the variable used to twiddle options,
216  * the structure is described in sys/mount.h
217  */
218 int
219 vfs_export(mp, argp)
220 	struct mount *mp;
221 	struct export_args *argp;
222 {
223 	struct netexport *nep;
224 	int error;
225 
226 	nep = mp->mnt_export;
227 	if (argp->ex_flags & MNT_DELEXPORT) {
228 		if (nep == NULL)
229 			return (ENOENT);
230 		if (mp->mnt_flag & MNT_EXPUBLIC) {
231 			vfs_setpublicfs(NULL, NULL, NULL);
232 			mp->mnt_flag &= ~MNT_EXPUBLIC;
233 		}
234 		vfs_free_addrlist(nep);
235 		mp->mnt_export = NULL;
236 		free(nep, M_MOUNT);
237 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
238 	}
239 	if (argp->ex_flags & MNT_EXPORTED) {
240 		if (nep == NULL) {
241 			nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
242 			mp->mnt_export = nep;
243 		}
244 		if (argp->ex_flags & MNT_EXPUBLIC) {
245 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
246 				return (error);
247 			mp->mnt_flag |= MNT_EXPUBLIC;
248 		}
249 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
250 			return (error);
251 		mp->mnt_flag |= MNT_EXPORTED;
252 	}
253 	return (0);
254 }
255 
256 /*
257  * Set the publicly exported filesystem (WebNFS). Currently, only
258  * one public filesystem is possible in the spec (RFC 2054 and 2055)
259  */
260 int
261 vfs_setpublicfs(mp, nep, argp)
262 	struct mount *mp;
263 	struct netexport *nep;
264 	struct export_args *argp;
265 {
266 	int error;
267 	struct vnode *rvp;
268 	char *cp;
269 
270 	/*
271 	 * mp == NULL -> invalidate the current info, the FS is
272 	 * no longer exported. May be called from either vfs_export
273 	 * or unmount, so check if it hasn't already been done.
274 	 */
275 	if (mp == NULL) {
276 		if (nfs_pub.np_valid) {
277 			nfs_pub.np_valid = 0;
278 			if (nfs_pub.np_index != NULL) {
279 				FREE(nfs_pub.np_index, M_TEMP);
280 				nfs_pub.np_index = NULL;
281 			}
282 		}
283 		return (0);
284 	}
285 
286 	/*
287 	 * Only one allowed at a time.
288 	 */
289 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
290 		return (EBUSY);
291 
292 	/*
293 	 * Get real filehandle for root of exported FS.
294 	 */
295 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
296 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
297 
298 	if ((error = VFS_ROOT(mp, &rvp)))
299 		return (error);
300 
301 	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
302 		return (error);
303 
304 	vput(rvp);
305 
306 	/*
307 	 * If an indexfile was specified, pull it in.
308 	 */
309 	if (argp->ex_indexfile != NULL) {
310 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
311 		    M_WAITOK);
312 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
313 		    MAXNAMLEN, (size_t *)0);
314 		if (!error) {
315 			/*
316 			 * Check for illegal filenames.
317 			 */
318 			for (cp = nfs_pub.np_index; *cp; cp++) {
319 				if (*cp == '/') {
320 					error = EINVAL;
321 					break;
322 				}
323 			}
324 		}
325 		if (error) {
326 			FREE(nfs_pub.np_index, M_TEMP);
327 			return (error);
328 		}
329 	}
330 
331 	nfs_pub.np_mount = mp;
332 	nfs_pub.np_valid = 1;
333 	return (0);
334 }
335 
336 /*
337  * Used by the filesystems to determine if a given network address
338  * (passed in 'nam') is present in thier exports list, returns a pointer
339  * to struct netcred so that the filesystem can examine it for
340  * access rights (read/write/etc).
341  */
342 struct netcred *
343 vfs_export_lookup(mp, nam)
344 	register struct mount *mp;
345 	struct sockaddr *nam;
346 {
347 	struct netexport *nep;
348 	register struct netcred *np;
349 	register struct radix_node_head *rnh;
350 	struct sockaddr *saddr;
351 
352 	nep = mp->mnt_export;
353 	if (nep == NULL)
354 		return (NULL);
355 	np = NULL;
356 	if (mp->mnt_flag & MNT_EXPORTED) {
357 		/*
358 		 * Lookup in the export list first.
359 		 */
360 		if (nam != NULL) {
361 			saddr = nam;
362 			rnh = nep->ne_rtable[saddr->sa_family];
363 			if (rnh != NULL) {
364 				RADIX_NODE_HEAD_LOCK(rnh);
365 				np = (struct netcred *)
366 				    (*rnh->rnh_matchaddr)(saddr, rnh);
367 				RADIX_NODE_HEAD_UNLOCK(rnh);
368 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
369 					np = NULL;
370 			}
371 		}
372 		/*
373 		 * If no address match, use the default if it exists.
374 		 */
375 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
376 			np = &nep->ne_defexported;
377 	}
378 	return (np);
379 }
380 
381 /*
382  * XXX: This comment comes from the deprecated ufs_check_export()
383  * XXX: and may not entirely apply, but lacking something better:
384  * This is the generic part of fhtovp called after the underlying
385  * filesystem has validated the file handle.
386  *
387  * Verify that a host should have access to a filesystem.
388  */
389 
390 int
391 vfs_stdcheckexp(mp, nam, extflagsp, credanonp)
392 	struct mount *mp;
393 	struct sockaddr *nam;
394 	int *extflagsp;
395 	struct ucred **credanonp;
396 {
397 	struct netcred *np;
398 
399 	np = vfs_export_lookup(mp, nam);
400 	if (np == NULL)
401 		return (EACCES);
402 	*extflagsp = np->netc_exflags;
403 	*credanonp = &np->netc_anon;
404 	return (0);
405 }
406 
407