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