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