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