Home | History | Annotate | Line # | Download | only in mount_nfs
getnfsargs.c revision 1.8
      1 /*	$NetBSD: getnfsargs.c,v 1.8 2006/12/27 12:43:10 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993, 1994
      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 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
     38 	The Regents of the University of California.  All rights reserved.\n");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)mount_nfs.c	8.11 (Berkeley) 5/4/95";
     44 #else
     45 __RCSID("$NetBSD: getnfsargs.c,v 1.8 2006/12/27 12:43:10 yamt Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/param.h>
     50 #include <sys/mount.h>
     51 #include <sys/socket.h>
     52 #include <sys/stat.h>
     53 #include <syslog.h>
     54 
     55 #include <rpc/rpc.h>
     56 #include <rpc/pmap_clnt.h>
     57 #include <rpc/pmap_prot.h>
     58 
     59 #ifdef ISO
     60 #include <netiso/iso.h>
     61 #endif
     62 
     63 
     64 #include <nfs/rpcv2.h>
     65 #include <nfs/nfsproto.h>
     66 #include <nfs/nfs.h>
     67 #include <nfs/nfsmount.h>
     68 
     69 #include <arpa/inet.h>
     70 
     71 #include <ctype.h>
     72 #include <err.h>
     73 #include <errno.h>
     74 #include <fcntl.h>
     75 #include <netdb.h>
     76 #include <signal.h>
     77 #include <stdio.h>
     78 #include <stdlib.h>
     79 #include <string.h>
     80 #include <unistd.h>
     81 #include <util.h>
     82 
     83 #include "mount_nfs.h"
     84 
     85 struct nfhret {
     86 	u_long		stat;
     87 	long		vers;
     88 	long		auth;
     89 	long		fhsize;
     90 	u_char		nfh[NFSX_V3FHMAX];
     91 };
     92 
     93 static int	xdr_dir(XDR *, char *);
     94 static int	xdr_fh(XDR *, struct nfhret *);
     95 
     96 int
     97 getnfsargs(char *spec, struct nfs_args *nfsargsp)
     98 {
     99 	CLIENT *clp;
    100 	struct addrinfo hints, *ai_nfs, *ai;
    101 	int ecode;
    102 	static struct netbuf nfs_nb;
    103 	static struct sockaddr_storage nfs_ss;
    104 	struct netconfig *nconf;
    105 	const char *netid;
    106 #ifdef ISO
    107 	static struct sockaddr_iso isoaddr;
    108 	struct iso_addr *isop;
    109 	int isoflag = 0;
    110 #endif
    111 	struct timeval pertry, try;
    112 	enum clnt_stat clnt_stat;
    113 	int i, nfsvers, mntvers;
    114 	int retryleft;
    115 	char *hostp, *delimp;
    116 	static struct nfhret nfhret;
    117 	static char nam[MNAMELEN + 1];
    118 
    119 	strlcpy(nam, spec, sizeof(nam));
    120 	if ((delimp = strchr(spec, '@')) != NULL) {
    121 		hostp = delimp + 1;
    122 	} else if ((delimp = strrchr(spec, ':')) != NULL) {
    123 		hostp = spec;
    124 		spec = delimp + 1;
    125 	} else {
    126 		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
    127 		return (0);
    128 	}
    129 	*delimp = '\0';
    130 	/*
    131 	 * DUMB!! Until the mount protocol works on iso transport, we must
    132 	 * supply both an iso and an inet address for the host.
    133 	 */
    134 #ifdef ISO
    135 	if (!strncmp(hostp, "iso=", 4)) {
    136 		u_short isoport;
    137 
    138 		hostp += 4;
    139 		isoflag++;
    140 		if ((delimp = strchr(hostp, '+')) == NULL) {
    141 			warnx("no iso+inet address");
    142 			return (0);
    143 		}
    144 		*delimp = '\0';
    145 		if ((isop = iso_addr(hostp)) == NULL) {
    146 			warnx("bad ISO address");
    147 			return (0);
    148 		}
    149 		memset(&isoaddr, 0, sizeof (isoaddr));
    150 		memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
    151 		isoaddr.siso_len = sizeof (isoaddr);
    152 		isoaddr.siso_family = AF_ISO;
    153 		isoaddr.siso_tlen = 2;
    154 		isoport = htons(NFS_PORT);
    155 		memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
    156 		hostp = delimp + 1;
    157 	}
    158 #endif /* ISO */
    159 
    160 	/*
    161 	 * Handle an internet host address.
    162 	 */
    163 	memset(&hints, 0, sizeof hints);
    164 	hints.ai_flags = AI_NUMERICHOST;
    165 	hints.ai_socktype = nfsargsp->sotype;
    166 	if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) != 0) {
    167 		hints.ai_flags = 0;
    168 		if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
    169 			warnx("can't get net id for host \"%s\": %s", hostp,
    170 			    gai_strerror(ecode));
    171 			return (0);
    172 		}
    173 	}
    174 
    175 	if ((nfsargsp->flags & NFSMNT_NFSV3) != 0) {
    176 		nfsvers = NFS_VER3;
    177 		mntvers = RPCMNT_VER3;
    178 	} else {
    179 		nfsvers = NFS_VER2;
    180 		mntvers = RPCMNT_VER1;
    181 	}
    182 	nfhret.stat = EACCES;	/* Mark not yet successful */
    183 
    184     for (ai = ai_nfs; ai; ai = ai->ai_next) {
    185 	/*
    186 	 * XXX. Nead a generic (family, type, proto) -> nconf interface.
    187 	 * __rpc_*2nconf exist, maybe they should be exported.
    188 	 */
    189 	if (nfsargsp->sotype == SOCK_STREAM) {
    190 		if (ai->ai_family == AF_INET6)
    191 			netid = "tcp6";
    192 		else
    193 			netid = "tcp";
    194 	} else {
    195 		if (ai->ai_family == AF_INET6)
    196 			netid = "udp6";
    197 		else
    198 			netid = "udp";
    199 	}
    200 
    201 	nconf = getnetconfigent(netid);
    202 
    203 tryagain:
    204 	retryleft = retrycnt;
    205 
    206 	while (retryleft > 0) {
    207 		nfs_nb.buf = &nfs_ss;
    208 		nfs_nb.maxlen = sizeof nfs_ss;
    209 		if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
    210 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
    211 				nfhret.stat = rpc_createerr.cf_error.re_errno;
    212 				break;
    213 			}
    214 			if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
    215 				nfhret.stat = EPROTONOSUPPORT;
    216 				break;
    217 			}
    218 			if ((opflags & ISBGRND) == 0)
    219 				clnt_pcreateerror(
    220 				    "mount_nfs: rpcbind to nfs on server");
    221 		} else {
    222 			pertry.tv_sec = 10;
    223 			pertry.tv_usec = 0;
    224 			/*
    225 			 * XXX relies on clnt_tcp_create to bind to a reserved
    226 			 * socket.
    227 			 */
    228 			clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
    229 			     mnttcp_ok ? nconf : getnetconfigent("udp"));
    230 			if (clp == NULL) {
    231 				if ((opflags & ISBGRND) == 0) {
    232 					clnt_pcreateerror(
    233 					    "Cannot MNT RPC (mountd)");
    234 				}
    235 			} else {
    236 				CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
    237 				    (char *)&pertry);
    238 				clp->cl_auth = authsys_create_default();
    239 				try.tv_sec = 10;
    240 				try.tv_usec = 0;
    241 				nfhret.auth = RPCAUTH_UNIX;
    242 				nfhret.vers = mntvers;
    243 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
    244 				    xdr_dir, spec, xdr_fh, &nfhret, try);
    245 				switch (clnt_stat) {
    246 				case RPC_PROGVERSMISMATCH:
    247 					if (nfsvers == NFS_VER3 && !force3) {
    248 						nfsvers = NFS_VER2;
    249 						mntvers = RPCMNT_VER1;
    250 						nfsargsp->flags &=
    251 							~NFSMNT_NFSV3;
    252 						goto tryagain;
    253 					} else {
    254 						errx(1, "%s", clnt_sperror(clp,
    255 							"MNT RPC"));
    256 					}
    257 				case RPC_SUCCESS:
    258 					auth_destroy(clp->cl_auth);
    259 					clnt_destroy(clp);
    260 					retryleft = 0;
    261 					break;
    262 				default:
    263 					/* XXX should give up on some errors */
    264 					if ((opflags & ISBGRND) == 0)
    265 						warnx("%s", clnt_sperror(clp,
    266 						    "bad MNT RPC"));
    267 					break;
    268 				}
    269 			}
    270 		}
    271 		if (--retryleft > 0) {
    272 			if (opflags & BGRND) {
    273 				opflags &= ~BGRND;
    274 				if ((i = fork()) != 0) {
    275 					if (i == -1)
    276 						err(1, "nqnfs 2");
    277 					exit(0);
    278 				}
    279 				(void) setsid();
    280 				(void) close(STDIN_FILENO);
    281 				(void) close(STDOUT_FILENO);
    282 				(void) close(STDERR_FILENO);
    283 				(void) chdir("/");
    284 				opflags |= ISBGRND;
    285 			}
    286 			sleep(60);
    287 		}
    288 	}
    289 	if (nfhret.stat == 0)
    290 		break;
    291     }
    292 	freeaddrinfo(ai_nfs);
    293 	if (nfhret.stat) {
    294 		if (opflags & ISBGRND)
    295 			exit(1);
    296 		errno = nfhret.stat;
    297 		warnx("can't access %s: %s", spec, strerror(nfhret.stat));
    298 		return (0);
    299 	}
    300 #ifdef ISO
    301 	if (isoflag) {
    302 		nfsargsp->addr = (struct sockaddr *) &isoaddr;
    303 		nfsargsp->addrlen = sizeof (isoaddr);
    304 	} else
    305 #endif /* ISO */
    306 	{
    307 		nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
    308 		nfsargsp->addrlen = nfs_nb.len;
    309 		if (port != 0) {
    310 			struct sockaddr *sa = nfsargsp->addr;
    311 			switch (sa->sa_family) {
    312 			case AF_INET:
    313 				((struct sockaddr_in *)sa)->sin_port = port;
    314 				break;
    315 #ifdef INET6
    316 			case AF_INET6:
    317 				((struct sockaddr_in6 *)sa)->sin6_port = port;
    318 				break;
    319 #endif
    320 			default:
    321 				errx(1, "Unsupported socket family %d",
    322 				    sa->sa_family);
    323 			}
    324 		}
    325 	}
    326 	nfsargsp->fh = nfhret.nfh;
    327 	nfsargsp->fhsize = nfhret.fhsize;
    328 	nfsargsp->hostname = nam;
    329 	return (1);
    330 }
    331 
    332 /*
    333  * xdr routines for mount rpc's
    334  */
    335 static int
    336 xdr_dir(XDR *xdrsp, char *dirp)
    337 {
    338 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    339 }
    340 
    341 static int
    342 xdr_fh(XDR *xdrsp, struct nfhret *np)
    343 {
    344 	int i;
    345 	long auth, authcnt, authfnd = 0;
    346 
    347 	if (!xdr_u_long(xdrsp, &np->stat))
    348 		return (0);
    349 	if (np->stat)
    350 		return (1);
    351 	switch (np->vers) {
    352 	case 1:
    353 		np->fhsize = NFSX_V2FH;
    354 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
    355 	case 3:
    356 		if (!xdr_long(xdrsp, &np->fhsize))
    357 			return (0);
    358 		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
    359 			return (0);
    360 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
    361 			return (0);
    362 		if (!xdr_long(xdrsp, &authcnt))
    363 			return (0);
    364 		for (i = 0; i < authcnt; i++) {
    365 			if (!xdr_long(xdrsp, &auth))
    366 				return (0);
    367 			if (auth == np->auth)
    368 				authfnd++;
    369 		}
    370 		/*
    371 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
    372 		 * list to indicate RPCAUTH_UNIX.
    373 		 */
    374 		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
    375 			np->stat = EAUTH;
    376 		return (1);
    377 	};
    378 	return (0);
    379 }
    380