xref: /openbsd/sbin/mount_nfs/mount_nfs.c (revision 9b7c3dbb)
1 /*	$OpenBSD: mount_nfs.c,v 1.53 2016/05/27 19:45:04 deraadt Exp $	*/
2 /*	$NetBSD: mount_nfs.c,v 1.12.4.1 1996/05/25 22:48:05 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/mount.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/stat.h>
41 #include <syslog.h>
42 
43 #include <rpc/rpc.h>
44 #include <rpc/pmap_clnt.h>
45 #include <rpc/pmap_prot.h>
46 
47 #include <nfs/rpcv2.h>
48 #include <nfs/nfsproto.h>
49 #include <nfs/nfs.h>
50 
51 #include <arpa/inet.h>
52 
53 #include <ctype.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <netdb.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <limits.h>
64 
65 #include "mntopts.h"
66 
67 #define	ALTF_BG		0x1
68 #define ALTF_NOCONN	0x2
69 #define ALTF_DUMBTIMR	0x4
70 #define ALTF_INTR	0x8
71 #define ALTF_NFSV3	0x20
72 #define ALTF_RDIRPLUS	0x40
73 #define	ALTF_MNTUDP	0x80
74 #define ALTF_RESVPORT	0x100
75 #define ALTF_SEQPACKET	0x200
76 #define ALTF_SOFT	0x800
77 #define ALTF_TCP	0x1000
78 #define ALTF_PORT	0x2000
79 #define ALTF_NFSV2	0x4000
80 #define ALTF_NOAC       0x8000
81 #define ALTF_ACREGMIN	0x10000
82 #define ALTF_ACREGMAX	0x20000
83 #define ALTF_ACDIRMIN	0x40000
84 #define ALTF_ACDIRMAX	0x80000
85 
86 const struct mntopt mopts[] = {
87 	MOPT_STDOPTS,
88 	MOPT_WXALLOWED,
89 	MOPT_FORCE,
90 	MOPT_UPDATE,
91 	MOPT_SYNC,
92 	{ "bg", ALTF_BG, 0 },
93 	{ "conn", ALTF_NOCONN, MFLAG_INVERSE },
94 	{ "dumbtimer", ALTF_DUMBTIMR, 0 },
95 	{ "intr", ALTF_INTR, 0 },
96 	{ "nfsv3", ALTF_NFSV3, 0 },
97 	{ "rdirplus", ALTF_RDIRPLUS, 0 },
98 	{ "mntudp", ALTF_MNTUDP, 0 },
99 	{ "resvport", ALTF_RESVPORT, 0 },
100 	{ "soft", ALTF_SOFT, 0 },
101 	{ "tcp", ALTF_TCP, 0 },
102 	{ "port", ALTF_PORT, MFLAG_INTVAL },
103 	{ "nfsv2", ALTF_NFSV2, 0 },
104 	{ "ac", ALTF_NOAC, MFLAG_INVERSE },
105 	{ "acregmin", ALTF_ACREGMIN, MFLAG_INTVAL },
106 	{ "acregmax", ALTF_ACREGMAX, MFLAG_INTVAL },
107 	{ "acdirmin", ALTF_ACDIRMIN, MFLAG_INTVAL },
108 	{ "acdirmax", ALTF_ACDIRMAX, MFLAG_INTVAL },
109 	{ NULL }
110 };
111 
112 struct nfs_args nfsdefargs = {
113 	NFS_ARGSVERSION,
114 	NULL,
115 	sizeof (struct sockaddr_in),
116 	SOCK_DGRAM,
117 	0,
118 	NULL,
119 	0,
120 	NFSMNT_NFSV3,
121 	NFS_WSIZE,
122 	NFS_RSIZE,
123 	NFS_READDIRSIZE,
124 	10,
125 	NFS_RETRANS,
126 	NFS_MAXGRPS,
127 	NFS_DEFRAHEAD,
128 	0,
129 	0,
130 	NULL,
131 	0,
132 	0,
133 	0,
134 	0
135 };
136 
137 struct nfhret {
138 	u_long		stat;
139 	long		vers;
140 	long		auth;
141 	long		fhsize;
142 	u_char		nfh[NFSX_V3FHMAX];
143 };
144 #define	DEF_RETRY	10000
145 #define	BGRND	1
146 #define	ISBGRND	2
147 int retrycnt;
148 int opflags = 0;
149 int nfsproto = IPPROTO_UDP;
150 int mnttcp_ok = 1;
151 u_short port_no = 0;
152 int force2 = 0;
153 int force3 = 0;
154 
155 int	getnfsargs(char *, struct nfs_args *);
156 void	set_rpc_maxgrouplist(int);
157 __dead	void usage(void);
158 int	xdr_dir(XDR *, char *);
159 int	xdr_fh(XDR *, struct nfhret *);
160 
161 int
162 main(int argc, char *argv[])
163 {
164 	int c;
165 	struct nfs_args *nfsargsp;
166 	struct nfs_args nfsargs;
167 	int mntflags, num;
168 	char name[PATH_MAX], *options = NULL, *spec;
169 	const char *p;
170 	union mntval value;
171 
172 	retrycnt = DEF_RETRY;
173 
174 	mntflags = 0;
175 	nfsargs = nfsdefargs;
176 	nfsargsp = &nfsargs;
177 	while ((c = getopt(argc, argv,
178 	    "23a:bcdD:g:I:iL:lo:PR:r:sTt:w:x:U")) != -1)
179 		switch (c) {
180 		case '3':
181 			if (force2)
182 				errx(1, "-2 and -3 are mutually exclusive");
183 			force3 = 1;
184 			break;
185 		case '2':
186 			if (force3)
187 				errx(1, "-2 and -3 are mutually exclusive");
188 			force2 = 1;
189 			nfsargsp->flags &= ~NFSMNT_NFSV3;
190 			break;
191 		case 'a':
192 			num = (int) strtonum(optarg, 0, 4, &p);
193 			if (p)
194 				errx(1, "illegal -a value %s: %s", optarg, p);
195 			nfsargsp->readahead = num;
196 			nfsargsp->flags |= NFSMNT_READAHEAD;
197 			break;
198 		case 'b':
199 			opflags |= BGRND;
200 			break;
201 		case 'c':
202 			nfsargsp->flags |= NFSMNT_NOCONN;
203 			break;
204 		case 'D':
205 			/* backward compatibility */
206 			break;
207 		case 'd':
208 			nfsargsp->flags |= NFSMNT_DUMBTIMR;
209 			break;
210 		case 'g':
211 			num = (int) strtonum(optarg, 1, NGROUPS_MAX, &p);
212 			if (p)
213 				errx(1, "illegal -g value %s: %s", optarg, p);
214 			set_rpc_maxgrouplist(num);
215 			nfsargsp->maxgrouplist = num;
216 			nfsargsp->flags |= NFSMNT_MAXGRPS;
217 			break;
218 		case 'I':
219 			num = (int) strtonum(optarg, 1, INT_MAX, &p);
220 			if (p)
221 				errx(1, "illegal -I value %s: %s", optarg, p);
222 			nfsargsp->readdirsize = num;
223 			nfsargsp->flags |= NFSMNT_READDIRSIZE;
224 			break;
225 		case 'i':
226 			nfsargsp->flags |= NFSMNT_INT;
227 			break;
228 		case 'L':
229 			/* backward compatibility */
230 			break;
231 		case 'l':
232 			nfsargsp->flags |= NFSMNT_RDIRPLUS;
233 			break;
234 		case 'o':
235 			options = optarg;
236 			while (options != NULL) {
237 				switch (getmntopt(&options, &value, mopts,
238 				    &mntflags)) {
239 				case ALTF_BG:
240 					opflags |= BGRND;
241 					break;
242 				case ALTF_NOCONN:
243 					nfsargsp->flags |= NFSMNT_NOCONN;
244 					break;
245 				case ALTF_DUMBTIMR:
246 					nfsargsp->flags |= NFSMNT_DUMBTIMR;
247 					break;
248 				case ALTF_INTR:
249 					nfsargsp->flags |= NFSMNT_INT;
250 					break;
251 				case ALTF_NFSV3:
252 					if (force2)
253 						errx(1,
254 						    "conflicting version options");
255 					force3 = 1;
256 					break;
257 				case ALTF_NFSV2:
258 					if (force3)
259 						errx(1,
260 						    "conflicting version options");
261 					force2 = 1;
262 					nfsargsp->flags &= ~NFSMNT_NFSV3;
263 					break;
264 				case ALTF_RDIRPLUS:
265 					nfsargsp->flags |= NFSMNT_RDIRPLUS;
266 					break;
267 				case ALTF_MNTUDP:
268 					mnttcp_ok = 0;
269 					break;
270 				case ALTF_RESVPORT:
271 					nfsargsp->flags |= NFSMNT_RESVPORT;
272 					break;
273 				case ALTF_SOFT:
274 					nfsargsp->flags |= NFSMNT_SOFT;
275 					break;
276 				case ALTF_TCP:
277 					nfsargsp->sotype = SOCK_STREAM;
278 					nfsproto = IPPROTO_TCP;
279 					break;
280 				case ALTF_PORT:
281 					port_no = value.ival;
282 					break;
283 				case ALTF_NOAC:
284 					nfsargsp->flags |= (NFSMNT_ACREGMIN |
285 					    NFSMNT_ACREGMAX | NFSMNT_ACDIRMIN |
286 					    NFSMNT_ACDIRMAX);
287 					nfsargsp->acregmin = 0;
288 					nfsargsp->acregmax = 0;
289 					nfsargsp->acdirmin = 0;
290 					nfsargsp->acdirmax = 0;
291 					break;
292 				case ALTF_ACREGMIN:
293 					nfsargsp->flags |= NFSMNT_ACREGMIN;
294 					nfsargsp->acregmin = value.ival;
295 					break;
296 				case ALTF_ACREGMAX:
297 					nfsargsp->flags |= NFSMNT_ACREGMAX;
298 					nfsargsp->acregmax = value.ival;
299 					break;
300 				case ALTF_ACDIRMIN:
301 					nfsargsp->flags |= NFSMNT_ACDIRMIN;
302 					nfsargsp->acdirmin = value.ival;
303 					break;
304 				case ALTF_ACDIRMAX:
305 					nfsargsp->flags |= NFSMNT_ACDIRMAX;
306 					nfsargsp->acdirmax = value.ival;
307 					break;
308 				}
309 			}
310 			break;
311 		case 'P':
312 			/* backward compatibility */
313 			break;
314 		case 'R':
315 			num = (int) strtonum(optarg, 1, INT_MAX, &p);
316 			if (p)
317 				errx(1, "illegal -R value %s: %s", optarg, p);
318 			retrycnt = num;
319 			break;
320 		case 'r':
321 			num = (int) strtonum(optarg, 1, INT_MAX, &p);
322 			if (p)
323 				errx(1, "illegal -r value %s: %s", optarg, p);
324 			nfsargsp->rsize = num;
325 			nfsargsp->flags |= NFSMNT_RSIZE;
326 			break;
327 		case 's':
328 			nfsargsp->flags |= NFSMNT_SOFT;
329 			break;
330 		case 'T':
331 			nfsargsp->sotype = SOCK_STREAM;
332 			nfsproto = IPPROTO_TCP;
333 			break;
334 		case 't':
335 			num = (int) strtonum(optarg, 1, INT_MAX, &p);
336 			if (p)
337 				errx(1, "illegal -t value %s: %s", optarg, p);
338 			nfsargsp->timeo = num;
339 			nfsargsp->flags |= NFSMNT_TIMEO;
340 			break;
341 		case 'w':
342 			num = (int) strtonum(optarg, 1, INT_MAX, &p);
343 			if (p)
344 				errx(1, "illegal -w value %s: %s", optarg, p);
345 			nfsargsp->wsize = num;
346 			nfsargsp->flags |= NFSMNT_WSIZE;
347 			break;
348 		case 'x':
349 			num = (int) strtonum(optarg, 1, INT_MAX, &p);
350 			if (p)
351 				errx(1, "illegal -x value %s: %s", optarg, p);
352 			nfsargsp->retrans = num;
353 			nfsargsp->flags |= NFSMNT_RETRANS;
354 			break;
355 		case 'U':
356 			mnttcp_ok = 0;
357 			break;
358 		default:
359 			usage();
360 			/* NOTREACHED */
361 		}
362 	argc -= optind;
363 	argv += optind;
364 
365 	if (argc != 2)
366 		usage();
367 
368 	spec = *argv++;
369 	if (realpath(*argv, name) == NULL)
370 		err(1, "realpath %s", *argv);
371 
372 	if (!getnfsargs(spec, nfsargsp))
373 		exit(1);
374 	if (mount(MOUNT_NFS, name, mntflags, nfsargsp)) {
375 		if (errno == EOPNOTSUPP)
376 			errx(1, "%s: Filesystem not supported by kernel", name);
377 		else
378 			err(1, "%s", name);
379 	}
380 	exit(0);
381 }
382 
383 int
384 getnfsargs(char *spec, struct nfs_args *nfsargsp)
385 {
386 	CLIENT *clp;
387 	struct hostent *hp;
388 	static struct sockaddr_in saddr;
389 	struct timeval pertry, try;
390 	enum clnt_stat clnt_stat;
391 	int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt;
392 	char *hostp, *delimp;
393 	u_short tport;
394 	static struct nfhret nfhret;
395 	static char nam[MNAMELEN + 1];
396 
397 	if (strlcpy(nam, spec, sizeof(nam)) >= sizeof(nam)) {
398 		errx(1, "hostname too long");
399 	}
400 
401 	if ((delimp = strchr(spec, '@')) != NULL) {
402 		hostp = delimp + 1;
403 	} else if ((delimp = strchr(spec, ':')) != NULL) {
404 		hostp = spec;
405 		spec = delimp + 1;
406 	} else {
407 		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
408 		return (0);
409 	}
410 	*delimp = '\0';
411 
412 	/*
413 	 * Handle an internet host address
414 	 */
415 	if (inet_aton(hostp, &saddr.sin_addr) == 0) {
416 		hp = gethostbyname(hostp);
417 		if (hp == NULL) {
418 			warnx("can't resolve address for host %s", hostp);
419 			return (0);
420 		}
421 		memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
422 	}
423 
424 	if (force2) {
425 		nfsvers = NFS_VER2;
426 		mntvers = RPCMNT_VER1;
427 	} else {
428 		nfsvers = NFS_VER3;
429 		mntvers = RPCMNT_VER3;
430 	}
431 	orgcnt = retrycnt;
432 tryagain:
433 	nfhret.stat = EACCES;	/* Mark not yet successful */
434 	while (retrycnt > 0) {
435 		saddr.sin_family = AF_INET;
436 		saddr.sin_port = htons(PMAPPORT);
437 		if ((tport = port_no ? port_no : pmap_getport(&saddr,
438 		    RPCPROG_NFS, nfsvers, nfsargsp->sotype == SOCK_STREAM ?
439 		    IPPROTO_TCP : IPPROTO_UDP)) == 0) {
440 			if ((opflags & ISBGRND) == 0)
441 				clnt_pcreateerror("NFS Portmap");
442 		} else {
443 			saddr.sin_port = 0;
444 			pertry.tv_sec = 10;
445 			pertry.tv_usec = 0;
446 			if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
447 			    clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
448 				&so, 0, 0);
449 			else
450 			    clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
451 				pertry, &so);
452 			if (clp == NULL) {
453 				if ((opflags & ISBGRND) == 0)
454 					clnt_pcreateerror("Cannot MNT RPC");
455 			} else {
456 				clp->cl_auth = authunix_create_default();
457 				try.tv_sec = 10;
458 				try.tv_usec = 0;
459 				nfhret.auth = RPCAUTH_UNIX;
460 				nfhret.vers = mntvers;
461 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
462 				    xdr_dir, spec, xdr_fh, &nfhret, try);
463 				if (clnt_stat != RPC_SUCCESS) {
464 					if (clnt_stat == RPC_PROGVERSMISMATCH) {
465 						if (nfsvers == NFS_VER3 &&
466 						    !force3) {
467 							retrycnt = orgcnt;
468 							nfsvers = NFS_VER2;
469 							mntvers = RPCMNT_VER1;
470 							nfsargsp->flags &=
471 							    ~NFSMNT_NFSV3;
472 							goto tryagain;
473 						} else {
474 							warnx("%s",
475 							    clnt_sperror(clp,
476 								"MNT RPC"));
477 						}
478 					}
479 					if ((opflags & ISBGRND) == 0)
480 						warnx("%s", clnt_sperror(clp,
481 						    "bad MNT RPC"));
482 				} else {
483 					auth_destroy(clp->cl_auth);
484 					clnt_destroy(clp);
485 					retrycnt = 0;
486 				}
487 			}
488 		}
489 		if (--retrycnt > 0) {
490 			if (opflags & BGRND) {
491 				opflags &= ~BGRND;
492 				if ((i = fork())) {
493 					if (i == -1)
494 						err(1, "fork");
495 					exit(0);
496 				}
497 				(void) setsid();
498 				(void) close(STDIN_FILENO);
499 				(void) close(STDOUT_FILENO);
500 				(void) close(STDERR_FILENO);
501 				(void) chdir("/");
502 				opflags |= ISBGRND;
503 			}
504 			sleep(60);
505 		}
506 	}
507 	if (nfhret.stat) {
508 		if (opflags & ISBGRND)
509 			exit(1);
510 		warnc(nfhret.stat, "can't access %s", spec);
511 		return (0);
512 	}
513 	saddr.sin_port = htons(tport);
514 	nfsargsp->addr = (struct sockaddr *) &saddr;
515 	nfsargsp->addrlen = sizeof (saddr);
516 	nfsargsp->fh = nfhret.nfh;
517 	nfsargsp->fhsize = nfhret.fhsize;
518 	nfsargsp->hostname = nam;
519 	return (1);
520 }
521 
522 /*
523  * xdr routines for mount rpc's
524  */
525 int
526 xdr_dir(XDR *xdrsp, char *dirp)
527 {
528 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
529 }
530 
531 int
532 xdr_fh(XDR *xdrsp, struct nfhret *np)
533 {
534 	int i;
535 	long auth, authcnt, authfnd = 0;
536 
537 	if (!xdr_u_long(xdrsp, &np->stat))
538 		return (0);
539 	if (np->stat)
540 		return (1);
541 	switch (np->vers) {
542 	case 1:
543 		np->fhsize = NFSX_V2FH;
544 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
545 	case 3:
546 		if (!xdr_long(xdrsp, &np->fhsize))
547 			return (0);
548 		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
549 			return (0);
550 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
551 			return (0);
552 		if (!xdr_long(xdrsp, &authcnt))
553 			return (0);
554 		for (i = 0; i < authcnt; i++) {
555 			if (!xdr_long(xdrsp, &auth))
556 				return (0);
557 			if (auth == np->auth)
558 				authfnd++;
559 		}
560 		/*
561 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
562 		 * list to indicate RPCAUTH_UNIX.
563 		 */
564 		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
565 			np->stat = EAUTH;
566 		return (1);
567 	};
568 	return (0);
569 }
570 
571 __dead void
572 usage(void)
573 {
574 	extern char *__progname;
575 
576 	(void)fprintf(stderr,
577 	    "usage: %s [-23bcdilsTU] [-a maxreadahead] [-g maxgroups]\n"
578 	    "\t[-I readdirsize] [-o options] [-R retrycnt] [-r readsize]\n"
579 	    "\t[-t timeout] [-w writesize] [-x retrans] rhost:path node\n",
580 	    __progname);
581 	exit(1);
582 }
583