Home | History | Annotate | Line # | Download | only in nfs
nfs_srvsubs.c revision 1.2
      1 /*	$NetBSD: nfs_srvsubs.c,v 1.2 2009/03/14 21:04:25 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      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. 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  *	@(#)nfs_subs.c	8.8 (Berkeley) 5/22/95
     35  */
     36 
     37 /*
     38  * Copyright 2000 Wasabi Systems, Inc.
     39  * All rights reserved.
     40  *
     41  * Written by Frank van der Linden for Wasabi Systems, Inc.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *      This product includes software developed for the NetBSD Project by
     54  *      Wasabi Systems, Inc.
     55  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     56  *    or promote products derived from this software without specific prior
     57  *    written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: nfs_srvsubs.c,v 1.2 2009/03/14 21:04:25 dsl Exp $");
     74 
     75 #include <sys/param.h>
     76 #include <sys/proc.h>
     77 #include <sys/systm.h>
     78 #include <sys/kernel.h>
     79 #include <sys/kmem.h>
     80 #include <sys/mount.h>
     81 #include <sys/vnode.h>
     82 #include <sys/namei.h>
     83 #include <sys/mbuf.h>
     84 #include <sys/socket.h>
     85 #include <sys/stat.h>
     86 #include <sys/filedesc.h>
     87 #include <sys/time.h>
     88 #include <sys/dirent.h>
     89 #include <sys/once.h>
     90 #include <sys/kauth.h>
     91 #include <sys/atomic.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #include <nfs/rpcv2.h>
     96 #include <nfs/nfsproto.h>
     97 #include <nfs/nfsnode.h>
     98 #include <nfs/nfs.h>
     99 #include <nfs/xdr_subs.h>
    100 #include <nfs/nfsm_subs.h>
    101 #include <nfs/nfsmount.h>
    102 #include <nfs/nfsrtt.h>
    103 #include <nfs/nfs_var.h>
    104 
    105 #include <miscfs/specfs/specdev.h>
    106 
    107 #include <netinet/in.h>
    108 
    109 /*
    110  * Set up nameidata for a lookup() call and do it.
    111  *
    112  * If pubflag is set, this call is done for a lookup operation on the
    113  * public filehandle. In that case we allow crossing mountpoints and
    114  * absolute pathnames. However, the caller is expected to check that
    115  * the lookup result is within the public fs, and deny access if
    116  * it is not.
    117  */
    118 int
    119 nfs_namei(struct nameidata *ndp, nfsrvfh_t *nsfh, uint32_t len, struct nfssvc_sock *slp, struct mbuf *nam, struct mbuf **mdp, char **dposp, struct vnode **retdirp, struct lwp *l, int kerbflag, int pubflag)
    120 {
    121 	int i, rem;
    122 	struct mbuf *md;
    123 	char *fromcp, *tocp, *cp;
    124 	struct iovec aiov;
    125 	struct uio auio;
    126 	struct vnode *dp;
    127 	int error, rdonly, linklen;
    128 	struct componentname *cnp = &ndp->ni_cnd;
    129 
    130 	*retdirp = NULL;
    131 
    132 	if ((len + 1) > MAXPATHLEN)
    133 		return (ENAMETOOLONG);
    134 	if (len == 0)
    135 		return (EACCES);
    136 	cnp->cn_pnbuf = PNBUF_GET();
    137 
    138 	/*
    139 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
    140 	 * and set the various ndp fields appropriately.
    141 	 */
    142 	fromcp = *dposp;
    143 	tocp = cnp->cn_pnbuf;
    144 	md = *mdp;
    145 	rem = mtod(md, char *) + md->m_len - fromcp;
    146 	for (i = 0; i < len; i++) {
    147 		while (rem == 0) {
    148 			md = md->m_next;
    149 			if (md == NULL) {
    150 				error = EBADRPC;
    151 				goto out;
    152 			}
    153 			fromcp = mtod(md, void *);
    154 			rem = md->m_len;
    155 		}
    156 		if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
    157 			error = EACCES;
    158 			goto out;
    159 		}
    160 		*tocp++ = *fromcp++;
    161 		rem--;
    162 	}
    163 	*tocp = '\0';
    164 	*mdp = md;
    165 	*dposp = fromcp;
    166 	len = nfsm_rndup(len)-len;
    167 	if (len > 0) {
    168 		if (rem >= len)
    169 			*dposp += len;
    170 		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
    171 			goto out;
    172 	}
    173 
    174 	/*
    175 	 * Extract and set starting directory.
    176 	 */
    177 	error = nfsrv_fhtovp(nsfh, false, &dp, ndp->ni_cnd.cn_cred, slp,
    178 	    nam, &rdonly, kerbflag, pubflag);
    179 	if (error)
    180 		goto out;
    181 	if (dp->v_type != VDIR) {
    182 		vrele(dp);
    183 		error = ENOTDIR;
    184 		goto out;
    185 	}
    186 
    187 	if (rdonly)
    188 		cnp->cn_flags |= RDONLY;
    189 
    190 	*retdirp = dp;
    191 
    192 	if (pubflag) {
    193 		/*
    194 		 * Oh joy. For WebNFS, handle those pesky '%' escapes,
    195 		 * and the 'native path' indicator.
    196 		 */
    197 		cp = PNBUF_GET();
    198 		fromcp = cnp->cn_pnbuf;
    199 		tocp = cp;
    200 		if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
    201 			switch ((unsigned char)*fromcp) {
    202 			case WEBNFS_NATIVE_CHAR:
    203 				/*
    204 				 * 'Native' path for us is the same
    205 				 * as a path according to the NFS spec,
    206 				 * just skip the escape char.
    207 				 */
    208 				fromcp++;
    209 				break;
    210 			/*
    211 			 * More may be added in the future, range 0x80-0xff
    212 			 */
    213 			default:
    214 				error = EIO;
    215 				vrele(dp);
    216 				PNBUF_PUT(cp);
    217 				goto out;
    218 			}
    219 		}
    220 		/*
    221 		 * Translate the '%' escapes, URL-style.
    222 		 */
    223 		while (*fromcp != '\0') {
    224 			if (*fromcp == WEBNFS_ESC_CHAR) {
    225 				if (fromcp[1] != '\0' && fromcp[2] != '\0') {
    226 					fromcp++;
    227 					*tocp++ = HEXSTRTOI(fromcp);
    228 					fromcp += 2;
    229 					continue;
    230 				} else {
    231 					error = ENOENT;
    232 					vrele(dp);
    233 					PNBUF_PUT(cp);
    234 					goto out;
    235 				}
    236 			} else
    237 				*tocp++ = *fromcp++;
    238 		}
    239 		*tocp = '\0';
    240 		PNBUF_PUT(cnp->cn_pnbuf);
    241 		cnp->cn_pnbuf = cp;
    242 	}
    243 
    244 	ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
    245 	ndp->ni_segflg = UIO_SYSSPACE;
    246 	ndp->ni_rootdir = rootvnode;
    247 	ndp->ni_erootdir = NULL;
    248 
    249 	if (pubflag) {
    250 		ndp->ni_loopcnt = 0;
    251 		if (cnp->cn_pnbuf[0] == '/')
    252 			dp = rootvnode;
    253 	} else {
    254 		cnp->cn_flags |= NOCROSSMOUNT;
    255 	}
    256 
    257 	VREF(dp);
    258 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    259 
    260     for (;;) {
    261 	cnp->cn_nameptr = cnp->cn_pnbuf;
    262 	ndp->ni_startdir = dp;
    263 
    264 	/*
    265 	 * And call lookup() to do the real work
    266 	 */
    267 	error = lookup(ndp);
    268 	if (error) {
    269 		if (ndp->ni_dvp) {
    270 			vput(ndp->ni_dvp);
    271 		}
    272 		PNBUF_PUT(cnp->cn_pnbuf);
    273 		return (error);
    274 	}
    275 
    276 	/*
    277 	 * Check for encountering a symbolic link
    278 	 */
    279 	if ((cnp->cn_flags & ISSYMLINK) == 0) {
    280 		if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
    281 			if (ndp->ni_dvp == ndp->ni_vp) {
    282 				vrele(ndp->ni_dvp);
    283 			} else {
    284 				vput(ndp->ni_dvp);
    285 			}
    286 		}
    287 		if (cnp->cn_flags & (SAVENAME | SAVESTART))
    288 			cnp->cn_flags |= HASBUF;
    289 		else
    290 			PNBUF_PUT(cnp->cn_pnbuf);
    291 		return (0);
    292 	} else {
    293 		if (!pubflag) {
    294 			error = EINVAL;
    295 			break;
    296 		}
    297 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
    298 			error = ELOOP;
    299 			break;
    300 		}
    301 		if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
    302 			error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred);
    303 			if (error != 0)
    304 				break;
    305 		}
    306 		if (ndp->ni_pathlen > 1)
    307 			cp = PNBUF_GET();
    308 		else
    309 			cp = cnp->cn_pnbuf;
    310 		aiov.iov_base = cp;
    311 		aiov.iov_len = MAXPATHLEN;
    312 		auio.uio_iov = &aiov;
    313 		auio.uio_iovcnt = 1;
    314 		auio.uio_offset = 0;
    315 		auio.uio_rw = UIO_READ;
    316 		auio.uio_resid = MAXPATHLEN;
    317 		UIO_SETUP_SYSSPACE(&auio);
    318 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
    319 		if (error) {
    320 badlink:
    321 			if (ndp->ni_pathlen > 1)
    322 				PNBUF_PUT(cp);
    323 			break;
    324 		}
    325 		linklen = MAXPATHLEN - auio.uio_resid;
    326 		if (linklen == 0) {
    327 			error = ENOENT;
    328 			goto badlink;
    329 		}
    330 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
    331 			error = ENAMETOOLONG;
    332 			goto badlink;
    333 		}
    334 		if (ndp->ni_pathlen > 1) {
    335 			memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
    336 			PNBUF_PUT(cnp->cn_pnbuf);
    337 			cnp->cn_pnbuf = cp;
    338 		} else
    339 			cnp->cn_pnbuf[linklen] = '\0';
    340 		ndp->ni_pathlen += linklen;
    341 		vput(ndp->ni_vp);
    342 		dp = ndp->ni_dvp;
    343 
    344 		/*
    345 		 * Check if root directory should replace current directory.
    346 		 */
    347 		if (cnp->cn_pnbuf[0] == '/') {
    348 			vput(dp);
    349 			dp = ndp->ni_rootdir;
    350 			VREF(dp);
    351 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    352 		}
    353 	}
    354    }
    355 	vput(ndp->ni_dvp);
    356 	vput(ndp->ni_vp);
    357 	ndp->ni_vp = NULL;
    358 out:
    359 	PNBUF_PUT(cnp->cn_pnbuf);
    360 	return (error);
    361 }
    362 
    363 /*
    364  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
    365  * 	- look up fsid in mount list (if not found ret error)
    366  *	- get vp and export rights by calling VFS_FHTOVP()
    367  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
    368  *	- if not lockflag unlock it with VOP_UNLOCK()
    369  */
    370 int
    371 nfsrv_fhtovp(nfsrvfh_t *nsfh, int lockflag, struct vnode **vpp,
    372     kauth_cred_t cred, struct nfssvc_sock *slp, struct mbuf *nam, int *rdonlyp,
    373     int kerbflag, int pubflag)
    374 {
    375 	struct mount *mp;
    376 	kauth_cred_t credanon;
    377 	int error, exflags;
    378 	struct sockaddr_in *saddr;
    379 	fhandle_t *fhp;
    380 
    381 	fhp = NFSRVFH_FHANDLE(nsfh);
    382 	*vpp = (struct vnode *)0;
    383 
    384 	if (nfs_ispublicfh(nsfh)) {
    385 		if (!pubflag || !nfs_pub.np_valid)
    386 			return (ESTALE);
    387 		fhp = nfs_pub.np_handle;
    388 	}
    389 
    390 	error = netexport_check(&fhp->fh_fsid, nam, &mp, &exflags, &credanon);
    391 	if (error) {
    392 		return error;
    393 	}
    394 
    395 	error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
    396 	if (error)
    397 		return (error);
    398 
    399 	if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
    400 		saddr = mtod(nam, struct sockaddr_in *);
    401 		if ((saddr->sin_family == AF_INET) &&
    402 		    ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
    403 			vput(*vpp);
    404 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
    405 		}
    406 		if ((saddr->sin_family == AF_INET6) &&
    407 		    ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
    408 			vput(*vpp);
    409 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
    410 		}
    411 	}
    412 	/*
    413 	 * Check/setup credentials.
    414 	 */
    415 	if (exflags & MNT_EXKERB) {
    416 		if (!kerbflag) {
    417 			vput(*vpp);
    418 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
    419 		}
    420 	} else if (kerbflag) {
    421 		vput(*vpp);
    422 		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
    423 	} else if (kauth_cred_geteuid(cred) == 0 || /* NFS maproot, see below */
    424 	    (exflags & MNT_EXPORTANON)) {
    425 		/*
    426 		 * This is used by the NFS maproot option. While we can change
    427 		 * the secmodel on our own host, we can't change it on the
    428 		 * clients. As means of least surprise, we're doing the
    429 		 * traditional thing here.
    430 		 * Should look into adding a "mapprivileged" or similar where
    431 		 * the users can be explicitly specified...
    432 		 * [elad, yamt 2008-03-05]
    433 		 */
    434 		kauth_cred_clone(credanon, cred);
    435 	}
    436 	if (exflags & MNT_EXRDONLY)
    437 		*rdonlyp = 1;
    438 	else
    439 		*rdonlyp = 0;
    440 	if (!lockflag)
    441 		VOP_UNLOCK(*vpp, 0);
    442 	return (0);
    443 }
    444 
    445 /*
    446  * WebNFS: check if a filehandle is a public filehandle. For v3, this
    447  * means a length of 0, for v2 it means all zeroes.
    448  */
    449 int
    450 nfs_ispublicfh(const nfsrvfh_t *nsfh)
    451 {
    452 	const char *cp = (const void *)(NFSRVFH_DATA(nsfh));
    453 	int i;
    454 
    455 	if (NFSRVFH_SIZE(nsfh) == 0) {
    456 		return true;
    457 	}
    458 	if (NFSRVFH_SIZE(nsfh) != NFSX_V2FH) {
    459 		return false;
    460 	}
    461 	for (i = 0; i < NFSX_V2FH; i++)
    462 		if (*cp++ != 0)
    463 			return false;
    464 	return true;
    465 }
    466 
    467 int
    468 nfsrv_composefh(struct vnode *vp, nfsrvfh_t *nsfh, bool v3)
    469 {
    470 	int error;
    471 	size_t fhsize;
    472 
    473 	fhsize = NFSD_MAXFHSIZE;
    474 	error = vfs_composefh(vp, (void *)NFSRVFH_DATA(nsfh), &fhsize);
    475 	if (NFSX_FHTOOBIG_P(fhsize, v3)) {
    476 		error = EOPNOTSUPP;
    477 	}
    478 	if (error != 0) {
    479 		return error;
    480 	}
    481 	if (!v3 && fhsize < NFSX_V2FH) {
    482 		memset((char *)NFSRVFH_DATA(nsfh) + fhsize, 0,
    483 		    NFSX_V2FH - fhsize);
    484 		fhsize = NFSX_V2FH;
    485 	}
    486 	if ((fhsize % NFSX_UNSIGNED) != 0) {
    487 		return EOPNOTSUPP;
    488 	}
    489 	nsfh->nsfh_size = fhsize;
    490 	return 0;
    491 }
    492 
    493 int
    494 nfsrv_comparefh(const nfsrvfh_t *fh1, const nfsrvfh_t *fh2)
    495 {
    496 
    497 	if (NFSRVFH_SIZE(fh1) != NFSRVFH_SIZE(fh2)) {
    498 		return NFSRVFH_SIZE(fh2) - NFSRVFH_SIZE(fh1);
    499 	}
    500 	return memcmp(NFSRVFH_DATA(fh1), NFSRVFH_DATA(fh2), NFSRVFH_SIZE(fh1));
    501 }
    502 
    503 void
    504 nfsrv_copyfh(nfsrvfh_t *fh1, const nfsrvfh_t *fh2)
    505 {
    506 	size_t size;
    507 
    508 	fh1->nsfh_size = size = NFSRVFH_SIZE(fh2);
    509 	memcpy(NFSRVFH_DATA(fh1), NFSRVFH_DATA(fh2), size);
    510 }
    511