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