Home | History | Annotate | Line # | Download | only in mount_nfs
mount_nfs.c revision 1.57
      1 /*	$NetBSD: mount_nfs.c,v 1.57 2006/12/27 12:43:10 yamt 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: mount_nfs.c,v 1.57 2006/12/27 12:43:10 yamt 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 #ifdef ISO
     56 #include <netiso/iso.h>
     57 #endif
     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 <ctype.h>
     67 #include <err.h>
     68 #include <errno.h>
     69 #include <fcntl.h>
     70 #include <netdb.h>
     71 #include <signal.h>
     72 #include <stdio.h>
     73 #include <stdlib.h>
     74 #include <string.h>
     75 #include <unistd.h>
     76 #include <util.h>
     77 
     78 #include <mntopts.h>
     79 
     80 #include "mount_nfs.h"
     81 
     82 #define	ALTF_BG		0x00000001
     83 #define ALTF_CONN	0x00000002
     84 #define ALTF_DUMBTIMR	0x00000004
     85 #define ALTF_INTR	0x00000008
     86 #define ALTF_NFSV3	0x00000020
     87 #define ALTF_RDIRPLUS	0x00000040
     88 #define	ALTF_MNTUDP	0x00000080
     89 #define ALTF_NORESPORT	0x00000100
     90 #define ALTF_SEQPACKET	0x00000200
     91 #define ALTF_NQNFS	0x00000400
     92 #define ALTF_SOFT	0x00000800
     93 #define ALTF_TCP	0x00001000
     94 #define ALTF_NFSV2	0x00002000
     95 #define ALTF_PORT	0x00004000
     96 #define ALTF_RSIZE	0x00008000
     97 #define ALTF_WSIZE	0x00010000
     98 #define ALTF_RDIRSIZE	0x00020000
     99 #define ALTF_MAXGRPS	0x00040000
    100 #define ALTF_LEASETERM	0x00080000
    101 #define ALTF_READAHEAD	0x00100000
    102 #define ALTF_DEADTHRESH	0x00200000
    103 #define ALTF_TIMEO	0x00400000
    104 #define ALTF_RETRANS	0x00800000
    105 
    106 static const struct mntopt mopts[] = {
    107 	MOPT_STDOPTS,
    108 	MOPT_FORCE,
    109 	MOPT_UPDATE,
    110 	MOPT_GETARGS,
    111 	{ "bg", 0, ALTF_BG, 1 },
    112 	{ "conn", 0, ALTF_CONN, 1 },
    113 	{ "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
    114 	{ "intr", 0, ALTF_INTR, 1 },
    115 	{ "nfsv3", 0, ALTF_NFSV3, 1 },
    116 	{ "rdirplus", 0, ALTF_RDIRPLUS, 1 },
    117 	{ "mntudp", 0, ALTF_MNTUDP, 1 },
    118 	{ "noresport", 0, ALTF_NORESPORT, 1 },
    119 #ifdef ISO
    120 	{ "seqpacket", 0, ALTF_SEQPACKET, 1 },
    121 #endif
    122 	{ "nqnfs", 0, ALTF_NQNFS, 1 },
    123 	{ "soft", 0, ALTF_SOFT, 1 },
    124 	{ "tcp", 0, ALTF_TCP, 1 },
    125 	{ "nfsv2", 0, ALTF_NFSV2, 1 },
    126 	{ "port", 0, ALTF_PORT, 1 },
    127 	{ "rsize", 0, ALTF_RSIZE, 1 },
    128 	{ "wsize", 0, ALTF_WSIZE, 1 },
    129 	{ "rdirsize", 0, ALTF_RDIRSIZE, 1 },
    130 	{ "maxgrps", 0, ALTF_MAXGRPS, 1 },
    131 	{ "leaseterm", 0, ALTF_LEASETERM, 1 },
    132 	{ "readahead", 0, ALTF_READAHEAD, 1 },
    133 	{ "deadthresh", 0, ALTF_DEADTHRESH, 1 },
    134 	{ "timeo", 0, ALTF_TIMEO, 1 },
    135 	MOPT_NULL,
    136 
    137 };
    138 
    139 struct nfs_args nfsdefargs = {
    140 	NFS_ARGSVERSION,
    141 	(struct sockaddr *)0,
    142 	sizeof (struct sockaddr_in),
    143 	SOCK_DGRAM,
    144 	0,
    145 	(u_char *)0,
    146 	0,
    147 	NFSMNT_NFSV3|NFSMNT_NOCONN|NFSMNT_RESVPORT,
    148 	NFS_WSIZE,
    149 	NFS_RSIZE,
    150 	NFS_READDIRSIZE,
    151 	10,
    152 	NFS_RETRANS,
    153 	NFS_MAXGRPS,
    154 	NFS_DEFRAHEAD,
    155 	0,	/* Ignored; lease term */
    156 	NFS_DEFDEADTHRESH,
    157 	(char *)0,
    158 };
    159 
    160 int retrycnt = 0;
    161 int opflags = 0;
    162 int nfsproto = IPPROTO_UDP;
    163 int force2 = 0;
    164 int force3 = 0;
    165 int mnttcp_ok = 1;
    166 int port = 0;
    167 
    168 static void	shownfsargs(const struct nfs_args *);
    169 #ifdef ISO
    170 static struct	iso_addr *iso_addr(const char *);
    171 #endif
    172 int	mount_nfs(int argc, char **argv);
    173 /* void	set_rpc_maxgrouplist(int); */
    174 static void	usage(void);
    175 
    176 #ifndef MOUNT_NOMAIN
    177 int
    178 main(int argc, char **argv)
    179 {
    180 	return mount_nfs(argc, argv);
    181 }
    182 #endif
    183 
    184 int
    185 mount_nfs(int argc, char *argv[])
    186 {
    187 	int c, retval;
    188 	struct nfs_args *nfsargsp;
    189 	struct nfs_args nfsargs;
    190 	struct sockaddr_storage sa;
    191 	int mntflags, altflags, num;
    192 	char name[MAXPATHLEN], *p, *spec;
    193 	mntoptparse_t mp;
    194 	retrycnt = DEF_RETRY;
    195 
    196 	mntflags = 0;
    197 	altflags = 0;
    198 	nfsargs = nfsdefargs;
    199 	nfsargsp = &nfsargs;
    200 	while ((c = getopt(argc, argv,
    201 	    "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
    202 		switch (c) {
    203 		case '3':
    204 		case 'q':
    205 			if (force2)
    206 				errx(1, "conflicting version options");
    207 			force3 = 1;
    208 			break;
    209 		case '2':
    210 			if (force3)
    211 				errx(1, "conflicting version options");
    212 			force2 = 1;
    213 			nfsargsp->flags &= ~NFSMNT_NFSV3;
    214 			break;
    215 		case 'a':
    216 			num = strtol(optarg, &p, 10);
    217 			if (*p || num < 0)
    218 				errx(1, "illegal -a value -- %s", optarg);
    219 			nfsargsp->readahead = num;
    220 			nfsargsp->flags |= NFSMNT_READAHEAD;
    221 			break;
    222 		case 'b':
    223 			opflags |= BGRND;
    224 			break;
    225 		case 'c':
    226 			nfsargsp->flags |= NFSMNT_NOCONN;
    227 			break;
    228 		case 'C':
    229 			nfsargsp->flags &= ~NFSMNT_NOCONN;
    230 			break;
    231 		case 'D':
    232 			num = strtol(optarg, &p, 10);
    233 			if (*p || num <= 0)
    234 				errx(1, "illegal -D value -- %s", optarg);
    235 			nfsargsp->deadthresh = num;
    236 			nfsargsp->flags |= NFSMNT_DEADTHRESH;
    237 			break;
    238 		case 'd':
    239 			nfsargsp->flags |= NFSMNT_DUMBTIMR;
    240 			break;
    241 #if 0 /* XXXX */
    242 		case 'g':
    243 			num = strtol(optarg, &p, 10);
    244 			if (*p || num <= 0)
    245 				errx(1, "illegal -g value -- %s", optarg);
    246 			set_rpc_maxgrouplist(num);
    247 			nfsargsp->maxgrouplist = num;
    248 			nfsargsp->flags |= NFSMNT_MAXGRPS;
    249 			break;
    250 #endif
    251 		case 'I':
    252 			num = strtol(optarg, &p, 10);
    253 			if (*p || num <= 0)
    254 				errx(1, "illegal -I value -- %s", optarg);
    255 			nfsargsp->readdirsize = num;
    256 			nfsargsp->flags |= NFSMNT_READDIRSIZE;
    257 			break;
    258 		case 'i':
    259 			nfsargsp->flags |= NFSMNT_INT;
    260 			break;
    261 		case 'L':
    262 			/* ignore */
    263 			break;
    264 		case 'l':
    265 			nfsargsp->flags |= NFSMNT_RDIRPLUS;
    266 			break;
    267 		case 'o':
    268 			mp = getmntopts(optarg, mopts, &mntflags, &altflags);
    269 			if (mp == NULL)
    270 				err(1, "getmntopts");
    271 			if (altflags & ALTF_BG)
    272 				opflags |= BGRND;
    273 			if (altflags & ALTF_CONN)
    274 				nfsargsp->flags &= ~NFSMNT_NOCONN;
    275 			if (altflags & ALTF_DUMBTIMR)
    276 				nfsargsp->flags |= NFSMNT_DUMBTIMR;
    277 			if (altflags & ALTF_INTR)
    278 				nfsargsp->flags |= NFSMNT_INT;
    279 			if (altflags & (ALTF_NFSV3|ALTF_NQNFS)) {
    280 				if (force2)
    281 					errx(1, "conflicting version options");
    282 				force3 = 1;
    283 			}
    284 			if (altflags & ALTF_NFSV2) {
    285 				if (force3)
    286 					errx(1, "conflicting version options");
    287 				force2 = 1;
    288 				nfsargsp->flags &= ~NFSMNT_NFSV3;
    289 			}
    290 			if (altflags & ALTF_RDIRPLUS)
    291 				nfsargsp->flags |= NFSMNT_RDIRPLUS;
    292 			if (altflags & ALTF_MNTUDP)
    293 				mnttcp_ok = 0;
    294 			if (altflags & ALTF_NORESPORT)
    295 				nfsargsp->flags &= ~NFSMNT_RESVPORT;
    296 #ifdef ISO
    297 			if (altflags & ALTF_SEQPACKET)
    298 				nfsargsp->sotype = SOCK_SEQPACKET;
    299 #endif
    300 			if (altflags & ALTF_SOFT)
    301 				nfsargsp->flags |= NFSMNT_SOFT;
    302 			if (altflags & ALTF_TCP) {
    303 				nfsargsp->sotype = SOCK_STREAM;
    304 				nfsproto = IPPROTO_TCP;
    305 			}
    306 			if (altflags & ALTF_PORT) {
    307 				port = getmntoptnum(mp, "port");
    308 			}
    309 			if (altflags & ALTF_RSIZE) {
    310 				nfsargsp->rsize =
    311 				    (int)getmntoptnum(mp, "rsize");
    312 				nfsargsp->flags |= NFSMNT_RSIZE;
    313 			}
    314 			if (altflags & ALTF_WSIZE) {
    315 				nfsargsp->wsize =
    316 				    (int)getmntoptnum(mp, "wsize");
    317 				nfsargsp->flags |= NFSMNT_WSIZE;
    318 			}
    319 			if (altflags & ALTF_RDIRSIZE) {
    320 				nfsargsp->rsize =
    321 				    (int)getmntoptnum(mp, "rdirsize");
    322 				nfsargsp->flags |= NFSMNT_READDIRSIZE;
    323 			}
    324 #if 0
    325 			if (altflags & ALTF_MAXGRPS) {
    326 				set_rpc_maxgrouplist(num);
    327 				nfsargsp->maxgrouplist =
    328 				    (int)getmntoptnum(mp, "maxgrps");
    329 				nfsargsp->flags |= NFSMNT_MAXGRPS;
    330 			}
    331 #endif
    332 			if (altflags & ALTF_LEASETERM) {
    333 				nfsargsp->leaseterm =
    334 				(int)getmntoptnum(mp, "leaseterm");
    335 				nfsargsp->flags |= NFSMNT_LEASETERM;
    336 			}
    337 			if (altflags & ALTF_READAHEAD) {
    338 				nfsargsp->readahead =
    339 				    (int)getmntoptnum(mp, "readahead");
    340 				nfsargsp->flags |= NFSMNT_READAHEAD;
    341 			}
    342 			if (altflags & ALTF_DEADTHRESH) {
    343 				nfsargsp->deadthresh =
    344 				    (int)getmntoptnum(mp, "deadthresh");
    345 				nfsargsp->flags |= NFSMNT_DEADTHRESH;
    346 			}
    347 			if (altflags & ALTF_TIMEO) {
    348 				nfsargsp->timeo =
    349 				    (int)getmntoptnum(mp, "timeo");
    350 				nfsargsp->flags |= NFSMNT_TIMEO;
    351 			}
    352 			if (altflags & ALTF_RETRANS) {
    353 				nfsargsp->retrans =
    354 				    (int)getmntoptnum(mp, "retrans");
    355 				nfsargsp->flags |= NFSMNT_RETRANS;
    356 			}
    357 			altflags = 0;
    358 			freemntopts(mp);
    359 			break;
    360 		case 'P':
    361 			nfsargsp->flags |= NFSMNT_RESVPORT;
    362 			break;
    363 		case 'p':
    364 			nfsargsp->flags &= ~NFSMNT_RESVPORT;
    365 			break;
    366 		case 'R':
    367 			num = strtol(optarg, &p, 10);
    368 			if (*p || num <= 0)
    369 				errx(1, "illegal -R value -- %s", optarg);
    370 			retrycnt = num;
    371 			break;
    372 		case 'r':
    373 			num = strtol(optarg, &p, 10);
    374 			if (*p || num <= 0)
    375 				errx(1, "illegal -r value -- %s", optarg);
    376 			nfsargsp->rsize = num;
    377 			nfsargsp->flags |= NFSMNT_RSIZE;
    378 			break;
    379 #ifdef ISO
    380 		case 'S':
    381 			nfsargsp->sotype = SOCK_SEQPACKET;
    382 			break;
    383 #endif
    384 		case 's':
    385 			nfsargsp->flags |= NFSMNT_SOFT;
    386 			break;
    387 		case 'T':
    388 			nfsargsp->sotype = SOCK_STREAM;
    389 			nfsproto = IPPROTO_TCP;
    390 			break;
    391 		case 't':
    392 			num = strtol(optarg, &p, 10);
    393 			if (*p || num <= 0)
    394 				errx(1, "illegal -t value -- %s", optarg);
    395 			nfsargsp->timeo = num;
    396 			nfsargsp->flags |= NFSMNT_TIMEO;
    397 			break;
    398 		case 'w':
    399 			num = strtol(optarg, &p, 10);
    400 			if (*p || num <= 0)
    401 				errx(1, "illegal -w value -- %s", optarg);
    402 			nfsargsp->wsize = num;
    403 			nfsargsp->flags |= NFSMNT_WSIZE;
    404 			break;
    405 		case 'x':
    406 			num = strtol(optarg, &p, 10);
    407 			if (*p || num <= 0)
    408 				errx(1, "illegal -x value -- %s", optarg);
    409 			nfsargsp->retrans = num;
    410 			nfsargsp->flags |= NFSMNT_RETRANS;
    411 			break;
    412 		case 'X':
    413 			nfsargsp->flags |= NFSMNT_XLATECOOKIE;
    414 			break;
    415 		case 'U':
    416 			mnttcp_ok = 0;
    417 			break;
    418 		default:
    419 			usage();
    420 			break;
    421 		}
    422 	argc -= optind;
    423 	argv += optind;
    424 
    425 	if (argc != 2)
    426 		usage();
    427 
    428 	spec = *argv++;
    429 	if (realpath(*argv, name) == NULL)           /* Check mounton path */
    430 		err(1, "realpath %s", *argv);
    431 	if (strncmp(*argv, name, MAXPATHLEN)) {
    432 		warnx("\"%s\" is a relative path.", *argv);
    433 		warnx("using \"%s\" instead.", name);
    434 	}
    435 
    436 retry:
    437 	if ((mntflags & MNT_GETARGS) != 0) {
    438 		memset(&sa, 0, sizeof(sa));
    439 		nfsargsp->addr = (struct sockaddr *)&sa;
    440 		nfsargsp->addrlen = sizeof(sa);
    441 	} else {
    442 		char *tspec;
    443 
    444 		if ((tspec = strdup(spec)) == NULL) {
    445 			err(1, "strdup");
    446 		}
    447 		if (!getnfsargs(tspec, nfsargsp)) {
    448 			exit(1);
    449 		}
    450 		free(tspec);
    451 	}
    452 	if ((retval = mount(MOUNT_NFS, name, mntflags, nfsargsp))) {
    453 		/* Did we just default to v3 on a v2-only kernel?
    454 		 * If so, default to v2 & try again */
    455 		if (errno == EPROGMISMATCH &&
    456 		    (nfsargsp->flags & NFSMNT_NFSV3) != 0 && !force3) {
    457 			/*
    458 			 * fall back to v2.  XXX lack of V3 umount.
    459 			 */
    460 			nfsargsp->flags &= ~NFSMNT_NFSV3;
    461 			goto retry;
    462 		}
    463 	}
    464 	if (retval)
    465 		err(1, "%s on %s", spec, name);
    466 	if (mntflags & MNT_GETARGS) {
    467 		shownfsargs(nfsargsp);
    468 		return (0);
    469 	}
    470 
    471 	exit(0);
    472 }
    473 
    474 static void
    475 shownfsargs(const struct nfs_args *nfsargsp)
    476 {
    477 	char fbuf[2048];
    478 	char host[NI_MAXHOST], serv[NI_MAXSERV];
    479 	int error;
    480 
    481 	(void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
    482 	if (nfsargsp->addr != NULL) {
    483 		error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
    484 		    sizeof(host), serv, sizeof(serv),
    485 		    NI_NUMERICHOST | NI_NUMERICSERV);
    486 		if (error != 0)
    487 			warnx("getnameinfo: %s", gai_strerror(error));
    488 	} else
    489 		error = -1;
    490 
    491 	if (error == 0)
    492 		printf("addr=%s, port=%s, addrlen=%d, ",
    493 		    host, serv, nfsargsp->addrlen);
    494 	printf("sotype=%d, proto=%d, fhsize=%d, "
    495 	    "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
    496 	    "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
    497 	    "deadthresh=%d\n",
    498 	    nfsargsp->sotype,
    499 	    nfsargsp->proto,
    500 	    nfsargsp->fhsize,
    501 	    fbuf,
    502 	    nfsargsp->wsize,
    503 	    nfsargsp->rsize,
    504 	    nfsargsp->readdirsize,
    505 	    nfsargsp->timeo,
    506 	    nfsargsp->retrans,
    507 	    nfsargsp->maxgrouplist,
    508 	    nfsargsp->readahead,
    509 	    nfsargsp->leaseterm,
    510 	    nfsargsp->deadthresh);
    511 }
    512 
    513 static void
    514 usage(void)
    515 {
    516 	(void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
    517 "[-23bCcdilPpqsTUX] [-a maxreadahead] [-D deadthresh]",
    518 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm]",
    519 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
    520 "\t[-w writesize] [-x retrans]",
    521 "\trhost:path node");
    522 	exit(1);
    523 }
    524