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