mount_nfs.c revision 1.54 1 /* $NetBSD: mount_nfs.c,v 1.54 2006/12/27 11:05:04 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.54 2006/12/27 11:05:04 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/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 case 'q':
208 if (force2)
209 errx(1, "conflicting version options");
210 force3 = 1;
211 break;
212 case '2':
213 if (force3)
214 errx(1, "conflicting version options");
215 force2 = 1;
216 nfsargsp->flags &= ~NFSMNT_NFSV3;
217 break;
218 case 'a':
219 num = strtol(optarg, &p, 10);
220 if (*p || num < 0)
221 errx(1, "illegal -a value -- %s", optarg);
222 nfsargsp->readahead = num;
223 nfsargsp->flags |= NFSMNT_READAHEAD;
224 break;
225 case 'b':
226 opflags |= BGRND;
227 break;
228 case 'c':
229 nfsargsp->flags |= NFSMNT_NOCONN;
230 break;
231 case 'C':
232 nfsargsp->flags &= ~NFSMNT_NOCONN;
233 break;
234 case 'D':
235 num = strtol(optarg, &p, 10);
236 if (*p || num <= 0)
237 errx(1, "illegal -D value -- %s", optarg);
238 nfsargsp->deadthresh = num;
239 nfsargsp->flags |= NFSMNT_DEADTHRESH;
240 break;
241 case 'd':
242 nfsargsp->flags |= NFSMNT_DUMBTIMR;
243 break;
244 #if 0 /* XXXX */
245 case 'g':
246 num = strtol(optarg, &p, 10);
247 if (*p || num <= 0)
248 errx(1, "illegal -g value -- %s", optarg);
249 set_rpc_maxgrouplist(num);
250 nfsargsp->maxgrouplist = num;
251 nfsargsp->flags |= NFSMNT_MAXGRPS;
252 break;
253 #endif
254 case 'I':
255 num = strtol(optarg, &p, 10);
256 if (*p || num <= 0)
257 errx(1, "illegal -I value -- %s", optarg);
258 nfsargsp->readdirsize = num;
259 nfsargsp->flags |= NFSMNT_READDIRSIZE;
260 break;
261 case 'i':
262 nfsargsp->flags |= NFSMNT_INT;
263 break;
264 case 'L':
265 /* ignore */
266 break;
267 case 'l':
268 nfsargsp->flags |= NFSMNT_RDIRPLUS;
269 break;
270 case 'o':
271 mp = getmntopts(optarg, mopts, &mntflags, &altflags);
272 if (mp == NULL)
273 err(1, "getmntopts");
274 if (altflags & ALTF_BG)
275 opflags |= BGRND;
276 if (altflags & ALTF_CONN)
277 nfsargsp->flags &= ~NFSMNT_NOCONN;
278 if (altflags & ALTF_DUMBTIMR)
279 nfsargsp->flags |= NFSMNT_DUMBTIMR;
280 if (altflags & ALTF_INTR)
281 nfsargsp->flags |= NFSMNT_INT;
282 if (altflags & (ALTF_NFSV3|ALTF_NQNFS)) {
283 if (force2)
284 errx(1, "conflicting version options");
285 force3 = 1;
286 }
287 if (altflags & ALTF_NFSV2) {
288 if (force3)
289 errx(1, "conflicting version options");
290 force2 = 1;
291 nfsargsp->flags &= ~NFSMNT_NFSV3;
292 }
293 if (altflags & ALTF_RDIRPLUS)
294 nfsargsp->flags |= NFSMNT_RDIRPLUS;
295 if (altflags & ALTF_MNTUDP)
296 mnttcp_ok = 0;
297 if (altflags & ALTF_NORESPORT)
298 nfsargsp->flags &= ~NFSMNT_RESVPORT;
299 #ifdef ISO
300 if (altflags & ALTF_SEQPACKET)
301 nfsargsp->sotype = SOCK_SEQPACKET;
302 #endif
303 if (altflags & ALTF_SOFT)
304 nfsargsp->flags |= NFSMNT_SOFT;
305 if (altflags & ALTF_TCP) {
306 nfsargsp->sotype = SOCK_STREAM;
307 nfsproto = IPPROTO_TCP;
308 }
309 if (altflags & ALTF_PORT) {
310 port = getmntoptnum(mp, "port");
311 }
312 if (altflags & ALTF_RSIZE) {
313 nfsargsp->rsize =
314 (int)getmntoptnum(mp, "rsize");
315 nfsargsp->flags |= NFSMNT_RSIZE;
316 }
317 if (altflags & ALTF_WSIZE) {
318 nfsargsp->wsize =
319 (int)getmntoptnum(mp, "wsize");
320 nfsargsp->flags |= NFSMNT_WSIZE;
321 }
322 if (altflags & ALTF_RDIRSIZE) {
323 nfsargsp->rsize =
324 (int)getmntoptnum(mp, "rdirsize");
325 nfsargsp->flags |= NFSMNT_READDIRSIZE;
326 }
327 #if 0
328 if (altflags & ALTF_MAXGRPS) {
329 set_rpc_maxgrouplist(num);
330 nfsargsp->maxgrouplist =
331 (int)getmntoptnum(mp, "maxgrps");
332 nfsargsp->flags |= NFSMNT_MAXGRPS;
333 }
334 #endif
335 if (altflags & ALTF_LEASETERM) {
336 nfsargsp->leaseterm =
337 (int)getmntoptnum(mp, "leaseterm");
338 nfsargsp->flags |= NFSMNT_LEASETERM;
339 }
340 if (altflags & ALTF_READAHEAD) {
341 nfsargsp->readahead =
342 (int)getmntoptnum(mp, "readahead");
343 nfsargsp->flags |= NFSMNT_READAHEAD;
344 }
345 if (altflags & ALTF_DEADTHRESH) {
346 nfsargsp->deadthresh =
347 (int)getmntoptnum(mp, "deadthresh");
348 nfsargsp->flags |= NFSMNT_DEADTHRESH;
349 }
350 if (altflags & ALTF_TIMEO) {
351 nfsargsp->timeo =
352 (int)getmntoptnum(mp, "timeo");
353 nfsargsp->flags |= NFSMNT_TIMEO;
354 }
355 if (altflags & ALTF_RETRANS) {
356 nfsargsp->retrans =
357 (int)getmntoptnum(mp, "retrans");
358 nfsargsp->flags |= NFSMNT_RETRANS;
359 }
360 altflags = 0;
361 freemntopts(mp);
362 break;
363 case 'P':
364 nfsargsp->flags |= NFSMNT_RESVPORT;
365 break;
366 case 'p':
367 nfsargsp->flags &= ~NFSMNT_RESVPORT;
368 break;
369 case 'R':
370 num = strtol(optarg, &p, 10);
371 if (*p || num <= 0)
372 errx(1, "illegal -R value -- %s", optarg);
373 retrycnt = num;
374 break;
375 case 'r':
376 num = strtol(optarg, &p, 10);
377 if (*p || num <= 0)
378 errx(1, "illegal -r value -- %s", optarg);
379 nfsargsp->rsize = num;
380 nfsargsp->flags |= NFSMNT_RSIZE;
381 break;
382 #ifdef ISO
383 case 'S':
384 nfsargsp->sotype = SOCK_SEQPACKET;
385 break;
386 #endif
387 case 's':
388 nfsargsp->flags |= NFSMNT_SOFT;
389 break;
390 case 'T':
391 nfsargsp->sotype = SOCK_STREAM;
392 nfsproto = IPPROTO_TCP;
393 break;
394 case 't':
395 num = strtol(optarg, &p, 10);
396 if (*p || num <= 0)
397 errx(1, "illegal -t value -- %s", optarg);
398 nfsargsp->timeo = num;
399 nfsargsp->flags |= NFSMNT_TIMEO;
400 break;
401 case 'w':
402 num = strtol(optarg, &p, 10);
403 if (*p || num <= 0)
404 errx(1, "illegal -w value -- %s", optarg);
405 nfsargsp->wsize = num;
406 nfsargsp->flags |= NFSMNT_WSIZE;
407 break;
408 case 'x':
409 num = strtol(optarg, &p, 10);
410 if (*p || num <= 0)
411 errx(1, "illegal -x value -- %s", optarg);
412 nfsargsp->retrans = num;
413 nfsargsp->flags |= NFSMNT_RETRANS;
414 break;
415 case 'X':
416 nfsargsp->flags |= NFSMNT_XLATECOOKIE;
417 break;
418 case 'U':
419 mnttcp_ok = 0;
420 break;
421 default:
422 usage();
423 break;
424 }
425 argc -= optind;
426 argv += optind;
427
428 if (argc != 2)
429 usage();
430
431 spec = *argv++;
432 if (realpath(*argv, name) == NULL) /* Check mounton path */
433 err(1, "realpath %s", *argv);
434 if (strncmp(*argv, name, MAXPATHLEN)) {
435 warnx("\"%s\" is a relative path.", *argv);
436 warnx("using \"%s\" instead.", name);
437 }
438
439 retry:
440 if ((mntflags & MNT_GETARGS) != 0) {
441 memset(&sa, 0, sizeof(sa));
442 nfsargsp->addr = (struct sockaddr *)&sa;
443 nfsargsp->addrlen = sizeof(sa);
444 } else {
445 char *tspec;
446
447 if ((tspec = strdup(spec)) == NULL) {
448 err(1, "strdup");
449 }
450 if (!getnfsargs(tspec, nfsargsp)) {
451 exit(1);
452 }
453 free(tspec);
454 }
455 if ((retval = mount(MOUNT_NFS, name, mntflags, nfsargsp))) {
456 /* Did we just default to v3 on a v2-only kernel?
457 * If so, default to v2 & try again */
458 if (errno == EPROGMISMATCH &&
459 (nfsargsp->flags & NFSMNT_NFSV3) != 0 && !force3) {
460 /*
461 * fall back to v2. XXX lack of V3 umount.
462 */
463 nfsargsp->flags &= ~NFSMNT_NFSV3;
464 goto retry;
465 }
466 }
467 if (retval)
468 err(1, "%s on %s", spec, name);
469 if (mntflags & MNT_GETARGS) {
470 shownfsargs(nfsargsp);
471 return (0);
472 }
473
474 if (nfsargsp->flags & NFSMNT_KERB) {
475 if ((opflags & ISBGRND) == 0) {
476 if ((i = fork()) != 0) {
477 if (i == -1)
478 err(1, "nqnfs 1");
479 exit(0);
480 }
481 (void) setsid();
482 (void) close(STDIN_FILENO);
483 (void) close(STDOUT_FILENO);
484 (void) close(STDERR_FILENO);
485 (void) chdir("/");
486 }
487 openlog("mount_nfs", LOG_PID, LOG_DAEMON);
488 nfssvc_flag = NFSSVC_MNTD;
489 ncd.ncd_dirp = name;
490 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
491 if (errno != ENEEDAUTH) {
492 syslog(LOG_ERR, "nfssvc err %m");
493 continue;
494 }
495 nfssvc_flag =
496 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
497 }
498 }
499 exit(0);
500 }
501
502 static void
503 shownfsargs(const struct nfs_args *nfsargsp)
504 {
505 char fbuf[2048];
506 char host[NI_MAXHOST], serv[NI_MAXSERV];
507 int error;
508
509 (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
510 if (nfsargsp->addr != NULL) {
511 error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
512 sizeof(host), serv, sizeof(serv),
513 NI_NUMERICHOST | NI_NUMERICSERV);
514 if (error != 0)
515 warnx("getnameinfo: %s", gai_strerror(error));
516 } else
517 error = -1;
518
519 if (error == 0)
520 printf("addr=%s, port=%s, addrlen=%d, ",
521 host, serv, nfsargsp->addrlen);
522 printf("sotype=%d, proto=%d, fhsize=%d, "
523 "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
524 "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
525 "deadthresh=%d\n",
526 nfsargsp->sotype,
527 nfsargsp->proto,
528 nfsargsp->fhsize,
529 fbuf,
530 nfsargsp->wsize,
531 nfsargsp->rsize,
532 nfsargsp->readdirsize,
533 nfsargsp->timeo,
534 nfsargsp->retrans,
535 nfsargsp->maxgrouplist,
536 nfsargsp->readahead,
537 nfsargsp->leaseterm,
538 nfsargsp->deadthresh);
539 }
540
541 static void
542 usage(void)
543 {
544 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
545 "[-23bcCdilpPqsTUX] [-a maxreadahead] [-D deadthresh]",
546 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm]",
547 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
548 "\t[-w writesize] [-x retrans]",
549 "\trhost:path node");
550 exit(1);
551 }
552