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