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