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