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