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