Home | History | Annotate | Line # | Download | only in mount_nfs
getnfsargs.c revision 1.17
      1 /*	$NetBSD: getnfsargs.c,v 1.17 2017/02/03 21:56:01 christos 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\
     38  The Regents of the University of California.  All rights reserved.");
     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.17 2017/02/03 21:56:01 christos 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 #include <nfs/rpcv2.h>
     60 #include <nfs/nfsproto.h>
     61 #include <nfs/nfs.h>
     62 #include <nfs/nfsmount.h>
     63 
     64 #include <arpa/inet.h>
     65 
     66 #include <err.h>
     67 #include <errno.h>
     68 #include <fcntl.h>
     69 #include <netdb.h>
     70 #include <signal.h>
     71 #include <stdio.h>
     72 #include <stdlib.h>
     73 #include <string.h>
     74 #include <unistd.h>
     75 #include <util.h>
     76 
     77 #include "mount_nfs.h"
     78 
     79 int retrycnt = DEF_RETRY;
     80 int opflags = 0;
     81 int force2 = 0;
     82 int force3 = 0;
     83 int mnttcp_ok = 1;
     84 int port = 0;
     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 #ifndef MOUNTNFS_RETRYRPC
     98 #define MOUNTNFS_RETRYRPC 60
     99 #endif
    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 	static struct netbuf nfs_nb;
    108 	static struct sockaddr_storage nfs_ss;
    109 	struct netconfig *nconf;
    110 	const char *netid;
    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 	/*
    132 	 * Handle an internet host address.
    133 	 */
    134 	memset(&hints, 0, sizeof hints);
    135 	hints.ai_flags = AI_NUMERICHOST;
    136 	hints.ai_socktype = nfsargsp->sotype;
    137 	if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) != 0) {
    138 		hints.ai_flags = 0;
    139 		if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
    140 			warnx("can't get net id for host \"%s\": %s", hostp,
    141 			    gai_strerror(ecode));
    142 			return (0);
    143 		}
    144 	}
    145 
    146 	if ((nfsargsp->flags & NFSMNT_NFSV3) != 0) {
    147 		nfsvers = NFS_VER3;
    148 		mntvers = RPCMNT_VER3;
    149 	} else {
    150 		nfsvers = NFS_VER2;
    151 		mntvers = RPCMNT_VER1;
    152 	}
    153 	nfhret.stat = EACCES;	/* Mark not yet successful */
    154 
    155     for (ai = ai_nfs; ai; ai = ai->ai_next) {
    156 	/*
    157 	 * XXX. Need a generic (family, type, proto) -> nconf interface.
    158 	 * __rpc_*2nconf exist, maybe they should be exported.
    159 	 */
    160 	if (nfsargsp->sotype == SOCK_STREAM) {
    161 		if (ai->ai_family == AF_INET6)
    162 			netid = "tcp6";
    163 		else
    164 			netid = "tcp";
    165 	} else {
    166 		if (ai->ai_family == AF_INET6)
    167 			netid = "udp6";
    168 		else
    169 			netid = "udp";
    170 	}
    171 
    172 	nconf = getnetconfigent(netid);
    173 
    174 tryagain:
    175 	retryleft = retrycnt;
    176 
    177 	while (retryleft > 0) {
    178 		nfs_nb.buf = &nfs_ss;
    179 		nfs_nb.maxlen = sizeof nfs_ss;
    180 		if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
    181 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
    182 				nfhret.stat = rpc_createerr.cf_error.re_errno;
    183 				break;
    184 			}
    185 			if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
    186 				nfhret.stat = EPROTONOSUPPORT;
    187 				break;
    188 			}
    189 			if ((opflags & ISBGRND) == 0) {
    190 				char buf[64];
    191 
    192 				snprintf(buf, sizeof(buf),
    193 				    "%s: rpcbind to nfs on server",
    194 				    getprogname());
    195 				clnt_pcreateerror(buf);
    196 			}
    197 		} else {
    198 			pertry.tv_sec = 30;
    199 			pertry.tv_usec = 0;
    200 			/*
    201 			 * XXX relies on clnt_tcp_create to bind to a reserved
    202 			 * socket.
    203 			 */
    204 			printf("netid=%s\n", netid);
    205 			clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
    206 			     mnttcp_ok ? nconf : getnetconfigent(netid));
    207 			if (clp == NULL) {
    208 				if ((opflags & ISBGRND) == 0) {
    209 					clnt_pcreateerror(
    210 					    "Cannot MNT RPC (mountd)");
    211 				}
    212 			} else {
    213 				CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
    214 				    (char *)&pertry);
    215 				clp->cl_auth = authsys_create_default();
    216 				try.tv_sec = 30;
    217 				try.tv_usec = 0;
    218 				nfhret.auth = RPCAUTH_UNIX;
    219 				nfhret.vers = mntvers;
    220 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
    221 				    xdr_dir, spec, xdr_fh, &nfhret, try);
    222 				switch (clnt_stat) {
    223 				case RPC_PROGVERSMISMATCH:
    224 					if (nfsvers == NFS_VER3 && !force3) {
    225 						nfsvers = NFS_VER2;
    226 						mntvers = RPCMNT_VER1;
    227 						nfsargsp->flags &=
    228 							~NFSMNT_NFSV3;
    229 						goto tryagain;
    230 					} else {
    231 						errx(1, "%s", clnt_sperror(clp,
    232 							"MNT RPC"));
    233 					}
    234 				case RPC_SUCCESS:
    235 					auth_destroy(clp->cl_auth);
    236 					clnt_destroy(clp);
    237 					retryleft = 0;
    238 					break;
    239 				default:
    240 					/* XXX should give up on some errors */
    241 					if ((opflags & ISBGRND) == 0)
    242 						warnx("%s", clnt_sperror(clp,
    243 						    "bad MNT RPC"));
    244 					break;
    245 				}
    246 			}
    247 		}
    248 		if (--retryleft > 0) {
    249 			if (opflags & BGRND) {
    250 				opflags &= ~BGRND;
    251 				if ((i = fork()) != 0) {
    252 					if (i == -1)
    253 						err(1, "fork");
    254 					exit(0);
    255 				}
    256 				(void) setsid();
    257 				(void) close(STDIN_FILENO);
    258 				(void) close(STDOUT_FILENO);
    259 				(void) close(STDERR_FILENO);
    260 				(void) chdir("/");
    261 				opflags |= ISBGRND;
    262 			}
    263 			sleep(MOUNTNFS_RETRYRPC);
    264 		}
    265 	}
    266 	if (nfhret.stat == 0)
    267 		break;
    268     }
    269 	freeaddrinfo(ai_nfs);
    270 	if (nfhret.stat) {
    271 		if (opflags & ISBGRND)
    272 			exit(1);
    273 		errno = nfhret.stat;
    274 		warnx("can't access %s: %s", spec, strerror(nfhret.stat));
    275 		return (0);
    276 	} else {
    277 		nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
    278 		nfsargsp->addrlen = nfs_nb.len;
    279 		if (port != 0) {
    280 			struct sockaddr *sa = nfsargsp->addr;
    281 			switch (sa->sa_family) {
    282 			case AF_INET:
    283 				((struct sockaddr_in *)sa)->sin_port = port;
    284 				break;
    285 #ifdef INET6
    286 			case AF_INET6:
    287 				((struct sockaddr_in6 *)sa)->sin6_port = port;
    288 				break;
    289 #endif
    290 			default:
    291 				errx(1, "Unsupported socket family %d",
    292 				    sa->sa_family);
    293 			}
    294 		}
    295 	}
    296 	nfsargsp->fh = nfhret.nfh;
    297 	nfsargsp->fhsize = nfhret.fhsize;
    298 	nfsargsp->hostname = nam;
    299 	return (1);
    300 }
    301 
    302 /*
    303  * xdr routines for mount rpc's
    304  */
    305 static int
    306 xdr_dir(XDR *xdrsp, char *dirp)
    307 {
    308 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    309 }
    310 
    311 static int
    312 xdr_fh(XDR *xdrsp, struct nfhret *np)
    313 {
    314 	int i;
    315 	long auth, authcnt, authfnd = 0;
    316 
    317 	if (!xdr_u_long(xdrsp, &np->stat))
    318 		return (0);
    319 	if (np->stat)
    320 		return (1);
    321 	switch (np->vers) {
    322 	case 1:
    323 		np->fhsize = NFSX_V2FH;
    324 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
    325 	case 3:
    326 		if (!xdr_long(xdrsp, &np->fhsize))
    327 			return (0);
    328 		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
    329 			return (0);
    330 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
    331 			return (0);
    332 		if (!xdr_long(xdrsp, &authcnt))
    333 			return (0);
    334 		for (i = 0; i < authcnt; i++) {
    335 			if (!xdr_long(xdrsp, &auth))
    336 				return (0);
    337 			if (auth == np->auth)
    338 				authfnd++;
    339 		}
    340 		/*
    341 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
    342 		 * list to indicate RPCAUTH_UNIX.
    343 		 */
    344 		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
    345 			np->stat = EAUTH;
    346 		return (1);
    347 	};
    348 	return (0);
    349 }
    350