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