1 /* $OpenBSD: mount_nfs.c,v 1.56 2024/08/19 05:58:41 florian Exp $ */
2 /* $NetBSD: mount_nfs.c,v 1.12.4.1 1996/05/25 22:48:05 fvdl Exp $ */
3
4 /*
5 * Copyright (c) 1992, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Rick Macklem at The University of Guelph.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/types.h>
37 #include <sys/mount.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <syslog.h>
41
42 #include <rpc/rpc.h>
43 #include <rpc/pmap_clnt.h>
44 #include <rpc/pmap_prot.h>
45
46 #include <nfs/rpcv2.h>
47 #include <nfs/nfsproto.h>
48 #include <nfs/nfs.h>
49
50 #include <arpa/inet.h>
51
52 #include <ctype.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <netdb.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <limits.h>
63
64 #include "mntopts.h"
65
66 #define ALTF_BG 0x1
67 #define ALTF_NOCONN 0x2
68 #define ALTF_DUMBTIMR 0x4
69 #define ALTF_INTR 0x8
70 #define ALTF_NFSV3 0x20
71 #define ALTF_RDIRPLUS 0x40
72 #define ALTF_MNTUDP 0x80
73 #define ALTF_RESVPORT 0x100
74 #define ALTF_SEQPACKET 0x200
75 #define ALTF_SOFT 0x800
76 #define ALTF_TCP 0x1000
77 #define ALTF_PORT 0x2000
78 #define ALTF_NFSV2 0x4000
79 #define ALTF_NOAC 0x8000
80 #define ALTF_ACREGMIN 0x10000
81 #define ALTF_ACREGMAX 0x20000
82 #define ALTF_ACDIRMIN 0x40000
83 #define ALTF_ACDIRMAX 0x80000
84
85 const struct mntopt mopts[] = {
86 MOPT_STDOPTS,
87 MOPT_WXALLOWED,
88 MOPT_FORCE,
89 MOPT_UPDATE,
90 MOPT_SYNC,
91 { "bg", ALTF_BG, 0 },
92 { "conn", ALTF_NOCONN, MFLAG_INVERSE },
93 { "dumbtimer", ALTF_DUMBTIMR, 0 },
94 { "intr", ALTF_INTR, 0 },
95 { "nfsv3", ALTF_NFSV3, 0 },
96 { "rdirplus", ALTF_RDIRPLUS, 0 },
97 { "mntudp", ALTF_MNTUDP, 0 },
98 { "resvport", ALTF_RESVPORT, 0 },
99 { "soft", ALTF_SOFT, 0 },
100 { "tcp", ALTF_TCP, 0 },
101 { "port", ALTF_PORT, MFLAG_INTVAL },
102 { "nfsv2", ALTF_NFSV2, 0 },
103 { "ac", ALTF_NOAC, MFLAG_INVERSE },
104 { "acregmin", ALTF_ACREGMIN, MFLAG_INTVAL },
105 { "acregmax", ALTF_ACREGMAX, MFLAG_INTVAL },
106 { "acdirmin", ALTF_ACDIRMIN, MFLAG_INTVAL },
107 { "acdirmax", ALTF_ACDIRMAX, MFLAG_INTVAL },
108 { NULL }
109 };
110
111 struct nfs_args nfsdefargs = {
112 NFS_ARGSVERSION,
113 NULL,
114 sizeof (struct sockaddr_in),
115 SOCK_DGRAM,
116 0,
117 NULL,
118 0,
119 NFSMNT_NFSV3,
120 NFS_WSIZE,
121 NFS_RSIZE,
122 NFS_READDIRSIZE,
123 10,
124 NFS_RETRANS,
125 NFS_MAXGRPS,
126 NFS_DEFRAHEAD,
127 0,
128 0,
129 NULL,
130 0,
131 0,
132 0,
133 0
134 };
135
136 struct nfhret {
137 u_long stat;
138 long vers;
139 long auth;
140 long fhsize;
141 u_char nfh[NFSX_V3FHMAX];
142 };
143 #define DEF_RETRY 10000
144 #define BGRND 1
145 #define ISBGRND 2
146 int retrycnt;
147 int opflags = 0;
148 int nfsproto = IPPROTO_UDP;
149 int mnttcp_ok = 1;
150 u_short port_no = 0;
151 int force2 = 0;
152 int force3 = 0;
153
154 int getnfsargs(char *, struct nfs_args *);
155 void set_rpc_maxgrouplist(int);
156 __dead void usage(void);
157 int xdr_dir(XDR *, char *);
158 int xdr_fh(XDR *, struct nfhret *);
159
160 int
main(int argc,char * argv[])161 main(int argc, char *argv[])
162 {
163 int c;
164 struct nfs_args *nfsargsp;
165 struct nfs_args nfsargs;
166 int mntflags, num;
167 char name[PATH_MAX], *options = NULL, *spec;
168 const char *p;
169 union mntval value;
170
171 retrycnt = DEF_RETRY;
172
173 mntflags = 0;
174 nfsargs = nfsdefargs;
175 nfsargsp = &nfsargs;
176 while ((c = getopt(argc, argv,
177 "23a:bcdD:g:I:iL:lo:PR:r:sTt:w:x:U")) != -1)
178 switch (c) {
179 case '3':
180 if (force2)
181 errx(1, "-2 and -3 are mutually exclusive");
182 force3 = 1;
183 break;
184 case '2':
185 if (force3)
186 errx(1, "-2 and -3 are mutually exclusive");
187 force2 = 1;
188 nfsargsp->flags &= ~NFSMNT_NFSV3;
189 break;
190 case 'a':
191 num = (int) strtonum(optarg, 0, 4, &p);
192 if (p)
193 errx(1, "illegal -a value %s: %s", optarg, p);
194 nfsargsp->readahead = num;
195 nfsargsp->flags |= NFSMNT_READAHEAD;
196 break;
197 case 'b':
198 opflags |= BGRND;
199 break;
200 case 'c':
201 nfsargsp->flags |= NFSMNT_NOCONN;
202 break;
203 case 'D':
204 /* backward compatibility */
205 break;
206 case 'd':
207 nfsargsp->flags |= NFSMNT_DUMBTIMR;
208 break;
209 case 'g':
210 num = (int) strtonum(optarg, 1, NGROUPS_MAX, &p);
211 if (p)
212 errx(1, "illegal -g value %s: %s", optarg, p);
213 set_rpc_maxgrouplist(num);
214 nfsargsp->maxgrouplist = num;
215 nfsargsp->flags |= NFSMNT_MAXGRPS;
216 break;
217 case 'I':
218 num = (int) strtonum(optarg, 1, INT_MAX, &p);
219 if (p)
220 errx(1, "illegal -I value %s: %s", optarg, p);
221 nfsargsp->readdirsize = num;
222 nfsargsp->flags |= NFSMNT_READDIRSIZE;
223 break;
224 case 'i':
225 nfsargsp->flags |= NFSMNT_INT;
226 break;
227 case 'L':
228 /* backward compatibility */
229 break;
230 case 'l':
231 nfsargsp->flags |= NFSMNT_RDIRPLUS;
232 break;
233 case 'o':
234 options = optarg;
235 while (options != NULL) {
236 switch (getmntopt(&options, &value, mopts,
237 &mntflags)) {
238 case ALTF_BG:
239 opflags |= BGRND;
240 break;
241 case ALTF_NOCONN:
242 nfsargsp->flags |= NFSMNT_NOCONN;
243 break;
244 case ALTF_DUMBTIMR:
245 nfsargsp->flags |= NFSMNT_DUMBTIMR;
246 break;
247 case ALTF_INTR:
248 nfsargsp->flags |= NFSMNT_INT;
249 break;
250 case ALTF_NFSV3:
251 if (force2)
252 errx(1,
253 "conflicting version options");
254 force3 = 1;
255 break;
256 case ALTF_NFSV2:
257 if (force3)
258 errx(1,
259 "conflicting version options");
260 force2 = 1;
261 nfsargsp->flags &= ~NFSMNT_NFSV3;
262 break;
263 case ALTF_RDIRPLUS:
264 nfsargsp->flags |= NFSMNT_RDIRPLUS;
265 break;
266 case ALTF_MNTUDP:
267 mnttcp_ok = 0;
268 break;
269 case ALTF_RESVPORT:
270 nfsargsp->flags |= NFSMNT_RESVPORT;
271 break;
272 case ALTF_SOFT:
273 nfsargsp->flags |= NFSMNT_SOFT;
274 break;
275 case ALTF_TCP:
276 nfsargsp->sotype = SOCK_STREAM;
277 nfsproto = IPPROTO_TCP;
278 break;
279 case ALTF_PORT:
280 port_no = value.ival;
281 break;
282 case ALTF_NOAC:
283 nfsargsp->flags |= (NFSMNT_ACREGMIN |
284 NFSMNT_ACREGMAX | NFSMNT_ACDIRMIN |
285 NFSMNT_ACDIRMAX);
286 nfsargsp->acregmin = 0;
287 nfsargsp->acregmax = 0;
288 nfsargsp->acdirmin = 0;
289 nfsargsp->acdirmax = 0;
290 break;
291 case ALTF_ACREGMIN:
292 nfsargsp->flags |= NFSMNT_ACREGMIN;
293 nfsargsp->acregmin = value.ival;
294 break;
295 case ALTF_ACREGMAX:
296 nfsargsp->flags |= NFSMNT_ACREGMAX;
297 nfsargsp->acregmax = value.ival;
298 break;
299 case ALTF_ACDIRMIN:
300 nfsargsp->flags |= NFSMNT_ACDIRMIN;
301 nfsargsp->acdirmin = value.ival;
302 break;
303 case ALTF_ACDIRMAX:
304 nfsargsp->flags |= NFSMNT_ACDIRMAX;
305 nfsargsp->acdirmax = value.ival;
306 break;
307 }
308 }
309 break;
310 case 'P':
311 /* backward compatibility */
312 break;
313 case 'R':
314 num = (int) strtonum(optarg, 1, INT_MAX, &p);
315 if (p)
316 errx(1, "illegal -R value %s: %s", optarg, p);
317 retrycnt = num;
318 break;
319 case 'r':
320 num = (int) strtonum(optarg, 1, INT_MAX, &p);
321 if (p)
322 errx(1, "illegal -r value %s: %s", optarg, p);
323 nfsargsp->rsize = num;
324 nfsargsp->flags |= NFSMNT_RSIZE;
325 break;
326 case 's':
327 nfsargsp->flags |= NFSMNT_SOFT;
328 break;
329 case 'T':
330 nfsargsp->sotype = SOCK_STREAM;
331 nfsproto = IPPROTO_TCP;
332 break;
333 case 't':
334 num = (int) strtonum(optarg, 1, INT_MAX, &p);
335 if (p)
336 errx(1, "illegal -t value %s: %s", optarg, p);
337 nfsargsp->timeo = num;
338 nfsargsp->flags |= NFSMNT_TIMEO;
339 break;
340 case 'w':
341 num = (int) strtonum(optarg, 1, INT_MAX, &p);
342 if (p)
343 errx(1, "illegal -w value %s: %s", optarg, p);
344 nfsargsp->wsize = num;
345 nfsargsp->flags |= NFSMNT_WSIZE;
346 break;
347 case 'x':
348 num = (int) strtonum(optarg, 1, INT_MAX, &p);
349 if (p)
350 errx(1, "illegal -x value %s: %s", optarg, p);
351 nfsargsp->retrans = num;
352 nfsargsp->flags |= NFSMNT_RETRANS;
353 break;
354 case 'U':
355 mnttcp_ok = 0;
356 break;
357 default:
358 usage();
359 /* NOTREACHED */
360 }
361 argc -= optind;
362 argv += optind;
363
364 if (argc != 2)
365 usage();
366
367 spec = *argv++;
368 if (realpath(*argv, name) == NULL)
369 err(1, "realpath %s", *argv);
370
371 if (!getnfsargs(spec, nfsargsp))
372 exit(1);
373 if (mount(MOUNT_NFS, name, mntflags, nfsargsp)) {
374 if (errno == EOPNOTSUPP)
375 errx(1, "%s: Filesystem not supported by kernel", name);
376 else
377 err(1, "%s", name);
378 }
379 exit(0);
380 }
381
382 int
getnfsargs(char * spec,struct nfs_args * nfsargsp)383 getnfsargs(char *spec, struct nfs_args *nfsargsp)
384 {
385 CLIENT *clp;
386 struct addrinfo hints, *res;
387 static struct sockaddr_in saddr;
388 struct timeval pertry, try;
389 enum clnt_stat clnt_stat;
390 int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt;
391 char *hostp, *delimp;
392 u_short tport;
393 static struct nfhret nfhret;
394 static char nam[MNAMELEN + 1];
395
396 if (strlcpy(nam, spec, sizeof(nam)) >= sizeof(nam)) {
397 errx(1, "hostname too long");
398 }
399
400 if ((delimp = strchr(spec, '@')) != NULL) {
401 hostp = delimp + 1;
402 } else if ((delimp = strchr(spec, ':')) != NULL) {
403 hostp = spec;
404 spec = delimp + 1;
405 } else {
406 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
407 return (0);
408 }
409 *delimp = '\0';
410
411 /*
412 * Handle an internet host address
413 */
414 memset(&hints, 0, sizeof(hints));
415 hints.ai_family = AF_INET;
416
417 if (getaddrinfo(hostp, NULL, &hints, &res) != 0) {
418 warnx("can't resolve address for host %s", hostp);
419 return (0);
420 }
421 saddr.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
422 freeaddrinfo(res);
423
424 if (force2) {
425 nfsvers = NFS_VER2;
426 mntvers = RPCMNT_VER1;
427 } else {
428 nfsvers = NFS_VER3;
429 mntvers = RPCMNT_VER3;
430 }
431 orgcnt = retrycnt;
432 tryagain:
433 nfhret.stat = EACCES; /* Mark not yet successful */
434 while (retrycnt > 0) {
435 saddr.sin_family = AF_INET;
436 saddr.sin_port = htons(PMAPPORT);
437 if ((tport = port_no ? port_no : pmap_getport(&saddr,
438 RPCPROG_NFS, nfsvers, nfsargsp->sotype == SOCK_STREAM ?
439 IPPROTO_TCP : IPPROTO_UDP)) == 0) {
440 if ((opflags & ISBGRND) == 0)
441 clnt_pcreateerror("NFS Portmap");
442 } else {
443 saddr.sin_port = 0;
444 pertry.tv_sec = 10;
445 pertry.tv_usec = 0;
446 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
447 clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
448 &so, 0, 0);
449 else
450 clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
451 pertry, &so);
452 if (clp == NULL) {
453 if ((opflags & ISBGRND) == 0)
454 clnt_pcreateerror("Cannot MNT RPC");
455 } else {
456 clp->cl_auth = authunix_create_default();
457 try.tv_sec = 10;
458 try.tv_usec = 0;
459 nfhret.auth = RPCAUTH_UNIX;
460 nfhret.vers = mntvers;
461 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
462 xdr_dir, spec, xdr_fh, &nfhret, try);
463 if (clnt_stat != RPC_SUCCESS) {
464 if (clnt_stat == RPC_PROGVERSMISMATCH) {
465 if (nfsvers == NFS_VER3 &&
466 !force3) {
467 retrycnt = orgcnt;
468 nfsvers = NFS_VER2;
469 mntvers = RPCMNT_VER1;
470 nfsargsp->flags &=
471 ~NFSMNT_NFSV3;
472 goto tryagain;
473 } else {
474 warnx("%s",
475 clnt_sperror(clp,
476 "MNT RPC"));
477 }
478 }
479 if ((opflags & ISBGRND) == 0)
480 warnx("%s", clnt_sperror(clp,
481 "bad MNT RPC"));
482 } else {
483 auth_destroy(clp->cl_auth);
484 clnt_destroy(clp);
485 retrycnt = 0;
486 }
487 }
488 }
489 if (--retrycnt > 0) {
490 if (opflags & BGRND) {
491 opflags &= ~BGRND;
492 if ((i = fork())) {
493 if (i == -1)
494 err(1, "fork");
495 exit(0);
496 }
497 (void) setsid();
498 (void) close(STDIN_FILENO);
499 (void) close(STDOUT_FILENO);
500 (void) close(STDERR_FILENO);
501 (void) chdir("/");
502 opflags |= ISBGRND;
503 }
504 sleep(60);
505 }
506 }
507 if (nfhret.stat) {
508 if (opflags & ISBGRND)
509 exit(1);
510 warnc(nfhret.stat, "can't access %s", spec);
511 return (0);
512 }
513 saddr.sin_port = htons(tport);
514 nfsargsp->addr = (struct sockaddr *) &saddr;
515 nfsargsp->addrlen = sizeof (saddr);
516 nfsargsp->fh = nfhret.nfh;
517 nfsargsp->fhsize = nfhret.fhsize;
518 nfsargsp->hostname = nam;
519 return (1);
520 }
521
522 /*
523 * xdr routines for mount rpc's
524 */
525 int
xdr_dir(XDR * xdrsp,char * dirp)526 xdr_dir(XDR *xdrsp, char *dirp)
527 {
528 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
529 }
530
531 int
xdr_fh(XDR * xdrsp,struct nfhret * np)532 xdr_fh(XDR *xdrsp, struct nfhret *np)
533 {
534 int i;
535 long auth, authcnt, authfnd = 0;
536
537 if (!xdr_u_long(xdrsp, &np->stat))
538 return (0);
539 if (np->stat)
540 return (1);
541 switch (np->vers) {
542 case 1:
543 np->fhsize = NFSX_V2FH;
544 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
545 case 3:
546 if (!xdr_long(xdrsp, &np->fhsize))
547 return (0);
548 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
549 return (0);
550 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
551 return (0);
552 if (!xdr_long(xdrsp, &authcnt))
553 return (0);
554 for (i = 0; i < authcnt; i++) {
555 if (!xdr_long(xdrsp, &auth))
556 return (0);
557 if (auth == np->auth)
558 authfnd++;
559 }
560 /*
561 * Some servers, such as DEC's OSF/1 return a nil authenticator
562 * list to indicate RPCAUTH_UNIX.
563 */
564 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
565 np->stat = EAUTH;
566 return (1);
567 }
568 return (0);
569 }
570
571 __dead void
usage(void)572 usage(void)
573 {
574 extern char *__progname;
575
576 (void)fprintf(stderr,
577 "usage: %s [-23bcdilsTU] [-a maxreadahead] [-g maxgroups]\n"
578 "\t[-I readdirsize] [-o options] [-R retrycnt] [-r readsize]\n"
579 "\t[-t timeout] [-w writesize] [-x retrans] rhost:path node\n",
580 __progname);
581 exit(1);
582 }
583