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