1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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 #if 0
36 #ifndef lint
37 static const char copyright[] =
38 "@(#) Copyright (c) 1992, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
44 #endif /* not lint */
45 #endif
46 #include <sys/cdefs.h>
47 #include <sys/param.h>
48 #include <sys/linker.h>
49 #include <sys/module.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <sys/syslog.h>
54 #include <sys/uio.h>
55
56 #include <rpc/rpc.h>
57 #include <rpc/pmap_clnt.h>
58 #include <rpc/pmap_prot.h>
59 #include <rpcsvc/nfs_prot.h>
60 #include <rpcsvc/mount.h>
61
62 #include <fs/nfs/nfsproto.h>
63 #include <fs/nfs/nfsv4_errstr.h>
64
65 #include <arpa/inet.h>
66 #include <net/route.h>
67 #include <net/if.h>
68
69 #include <ctype.h>
70 #include <err.h>
71 #include <errno.h>
72 #include <fcntl.h>
73 #include <netdb.h>
74 #include <stdbool.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <strings.h>
79 #include <sysexits.h>
80 #include <unistd.h>
81
82 #include "mntopts.h"
83 #include "mounttab.h"
84
85 /* Table for af,sotype -> netid conversions. */
86 static struct nc_protos {
87 const char *netid;
88 int af;
89 int sotype;
90 } nc_protos[] = {
91 {"udp", AF_INET, SOCK_DGRAM},
92 {"tcp", AF_INET, SOCK_STREAM},
93 {"udp6", AF_INET6, SOCK_DGRAM},
94 {"tcp6", AF_INET6, SOCK_STREAM},
95 {NULL, 0, 0}
96 };
97
98 struct nfhret {
99 u_long stat;
100 long vers;
101 long auth;
102 long fhsize;
103 u_char nfh[NFS3_FHSIZE];
104 };
105 #define BGRND 0x01
106 #define ISBGRND 0x02
107 #define OF_NOINET4 0x04
108 #define OF_NOINET6 0x08
109 #define BGRNDNOW 0x10
110 static int retrycnt = -1;
111 static int opflags = 0;
112 static int nfsproto = IPPROTO_TCP;
113 static int mnttcp_ok = 1;
114 static int noconn = 0;
115 /* The 'portspec' is the server nfs port; NULL means look up via rpcbind. */
116 static const char *portspec = NULL;
117 static struct sockaddr *addr;
118 static int addrlen = 0;
119 static u_char *fh = NULL;
120 static int fhsize = 0;
121 static int secflavor = -1;
122 static int got_principal = 0;
123 static in_port_t mntproto_port = 0;
124
125 static enum mountmode {
126 ANY,
127 V2,
128 V3,
129 V4
130 } mountmode = ANY;
131
132 /* Return codes for nfs_tryproto. */
133 enum tryret {
134 TRYRET_SUCCESS,
135 TRYRET_TIMEOUT, /* No response received. */
136 TRYRET_REMOTEERR, /* Error received from remote server. */
137 TRYRET_LOCALERR /* Local failure. */
138 };
139
140 static int sec_name_to_num(const char *sec);
141 static const char *sec_num_to_name(int num);
142 static int getnfsargs(char **, char **, struct iovec **iov, int *iovlen);
143 /* void set_rpc_maxgrouplist(int); */
144 static struct netconfig *getnetconf_cached(const char *netid);
145 static const char *netidbytype(int af, int sotype);
146 static void usage(void) __dead2;
147 static int xdr_dir(XDR *, char *);
148 static int xdr_fh(XDR *, struct nfhret *);
149 static enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec,
150 char **errstr, struct iovec **iov, int *iovlen);
151 static enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
152
153 int
main(int argc,char * argv[])154 main(int argc, char *argv[])
155 {
156 int c;
157 struct iovec *iov;
158 int num, iovlen;
159 char *host, *mntname, *p, *spec, *tmp;
160 char mntpath[MAXPATHLEN], errmsg[255];
161 char hostname[MAXHOSTNAMELEN + 1], gssn[MAXHOSTNAMELEN + 50];
162 const char *gssname, *nmount_errstr;
163 bool softintr;
164
165 softintr = false;
166 iov = NULL;
167 iovlen = 0;
168 memset(errmsg, 0, sizeof(errmsg));
169 gssname = NULL;
170
171 while ((c = getopt(argc, argv,
172 "23a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1)
173 switch (c) {
174 case '2':
175 mountmode = V2;
176 break;
177 case '3':
178 mountmode = V3;
179 break;
180 case 'a':
181 printf("-a deprecated, use -o readahead=<value>\n");
182 build_iovec(&iov, &iovlen, "readahead", optarg, (size_t)-1);
183 break;
184 case 'b':
185 opflags |= BGRND;
186 break;
187 case 'c':
188 printf("-c deprecated, use -o noconn\n");
189 build_iovec(&iov, &iovlen, "noconn", NULL, 0);
190 noconn = 1;
191 break;
192 case 'D':
193 printf("-D deprecated, use -o deadthresh=<value>\n");
194 build_iovec(&iov, &iovlen, "deadthresh", optarg, (size_t)-1);
195 break;
196 case 'd':
197 printf("-d deprecated, use -o dumbtimer");
198 build_iovec(&iov, &iovlen, "dumbtimer", NULL, 0);
199 break;
200 case 'g':
201 printf("-g deprecated, use -o maxgroups");
202 num = strtol(optarg, &p, 10);
203 if (*p || num <= 0)
204 errx(1, "illegal -g value -- %s", optarg);
205 //set_rpc_maxgrouplist(num);
206 build_iovec(&iov, &iovlen, "maxgroups", optarg, (size_t)-1);
207 break;
208 case 'I':
209 printf("-I deprecated, use -o readdirsize=<value>\n");
210 build_iovec(&iov, &iovlen, "readdirsize", optarg, (size_t)-1);
211 break;
212 case 'i':
213 printf("-i deprecated, use -o intr\n");
214 build_iovec(&iov, &iovlen, "intr", NULL, 0);
215 softintr = true;
216 break;
217 case 'L':
218 printf("-L deprecated, use -o nolockd\n");
219 build_iovec(&iov, &iovlen, "nolockd", NULL, 0);
220 break;
221 case 'l':
222 printf("-l deprecated, -o rdirplus\n");
223 build_iovec(&iov, &iovlen, "rdirplus", NULL, 0);
224 break;
225 case 'N':
226 printf("-N deprecated, do not specify -o resvport\n");
227 break;
228 case 'o': {
229 int pass_flag_to_nmount;
230 char *opt = optarg;
231 while (opt) {
232 char *pval = NULL;
233 char *pnextopt = NULL;
234 const char *val = "";
235 pass_flag_to_nmount = 1;
236 pnextopt = strchr(opt, ',');
237 if (pnextopt != NULL) {
238 *pnextopt = '\0';
239 pnextopt++;
240 }
241 pval = strchr(opt, '=');
242 if (pval != NULL) {
243 *pval = '\0';
244 val = pval + 1;
245 }
246 if (strcmp(opt, "bg") == 0) {
247 opflags |= BGRND;
248 pass_flag_to_nmount=0;
249 } else if (strcmp(opt, "bgnow") == 0) {
250 opflags |= BGRNDNOW;
251 pass_flag_to_nmount=0;
252 } else if (strcmp(opt, "fg") == 0) {
253 /* same as not specifying -o bg */
254 pass_flag_to_nmount=0;
255 } else if (strcmp(opt, "gssname") == 0) {
256 pass_flag_to_nmount = 0;
257 gssname = val;
258 } else if (strcmp(opt, "mntudp") == 0) {
259 mnttcp_ok = 0;
260 nfsproto = IPPROTO_UDP;
261 } else if (strcmp(opt, "udp") == 0) {
262 nfsproto = IPPROTO_UDP;
263 } else if (strcmp(opt, "tcp") == 0) {
264 nfsproto = IPPROTO_TCP;
265 } else if (strcmp(opt, "noinet4") == 0) {
266 pass_flag_to_nmount=0;
267 opflags |= OF_NOINET4;
268 } else if (strcmp(opt, "noinet6") == 0) {
269 pass_flag_to_nmount=0;
270 opflags |= OF_NOINET6;
271 } else if (strcmp(opt, "noconn") == 0) {
272 noconn = 1;
273 } else if (strcmp(opt, "nfsv2") == 0) {
274 pass_flag_to_nmount=0;
275 mountmode = V2;
276 } else if (strcmp(opt, "nfsv3") == 0) {
277 mountmode = V3;
278 } else if (strcmp(opt, "nfsv4") == 0) {
279 pass_flag_to_nmount=0;
280 mountmode = V4;
281 nfsproto = IPPROTO_TCP;
282 if (portspec == NULL)
283 portspec = "2049";
284 } else if (strcmp(opt, "port") == 0) {
285 pass_flag_to_nmount=0;
286 asprintf(&tmp, "%d", atoi(val));
287 if (tmp == NULL)
288 err(1, "asprintf");
289 portspec = tmp;
290 } else if (strcmp(opt, "principal") == 0) {
291 got_principal = 1;
292 } else if (strcmp(opt, "proto") == 0) {
293 pass_flag_to_nmount=0;
294 if (strcmp(val, "tcp") == 0) {
295 nfsproto = IPPROTO_TCP;
296 opflags |= OF_NOINET6;
297 build_iovec(&iov, &iovlen,
298 "tcp", NULL, 0);
299 } else if (strcmp(val, "udp") == 0) {
300 mnttcp_ok = 0;
301 nfsproto = IPPROTO_UDP;
302 opflags |= OF_NOINET6;
303 build_iovec(&iov, &iovlen,
304 "udp", NULL, 0);
305 } else if (strcmp(val, "tcp6") == 0) {
306 nfsproto = IPPROTO_TCP;
307 opflags |= OF_NOINET4;
308 build_iovec(&iov, &iovlen,
309 "tcp", NULL, 0);
310 } else if (strcmp(val, "udp6") == 0) {
311 mnttcp_ok = 0;
312 nfsproto = IPPROTO_UDP;
313 opflags |= OF_NOINET4;
314 build_iovec(&iov, &iovlen,
315 "udp", NULL, 0);
316 } else {
317 errx(1,
318 "illegal proto value -- %s",
319 val);
320 }
321 } else if (strcmp(opt, "sec") == 0) {
322 /*
323 * Don't add this option to
324 * the iovec yet - we will
325 * negotiate which sec flavor
326 * to use with the remote
327 * mountd.
328 */
329 pass_flag_to_nmount=0;
330 secflavor = sec_name_to_num(val);
331 if (secflavor < 0) {
332 errx(1,
333 "illegal sec value -- %s",
334 val);
335 }
336 } else if (strcmp(opt, "retrycnt") == 0) {
337 pass_flag_to_nmount=0;
338 num = strtol(val, &p, 10);
339 if (*p || num < 0)
340 errx(1, "illegal retrycnt value -- %s", val);
341 retrycnt = num;
342 } else if (strcmp(opt, "maxgroups") == 0) {
343 num = strtol(val, &p, 10);
344 if (*p || num <= 0)
345 errx(1, "illegal maxgroups value -- %s", val);
346 //set_rpc_maxgrouplist(num);
347 } else if (strcmp(opt, "vers") == 0) {
348 num = strtol(val, &p, 10);
349 if (*p || num <= 0)
350 errx(1, "illegal vers value -- "
351 "%s", val);
352 switch (num) {
353 case 2:
354 mountmode = V2;
355 break;
356 case 3:
357 mountmode = V3;
358 build_iovec(&iov, &iovlen,
359 "nfsv3", NULL, 0);
360 break;
361 case 4:
362 mountmode = V4;
363 nfsproto = IPPROTO_TCP;
364 if (portspec == NULL)
365 portspec = "2049";
366 break;
367 default:
368 errx(1, "illegal nfs version "
369 "value -- %s", val);
370 }
371 pass_flag_to_nmount=0;
372 } else if (strcmp(opt, "soft") == 0) {
373 softintr = true;
374 } else if (strcmp(opt, "intr") == 0) {
375 softintr = true;
376 } else if (strcmp(opt, "mountport") == 0) {
377 num = strtol(val, &p, 10);
378 if (*p || num <= 0 || num > IPPORT_MAX)
379 errx(1, "illegal port num -- "
380 "%s", val);
381 mntproto_port = num;
382 pass_flag_to_nmount=0;
383 }
384 if (pass_flag_to_nmount) {
385 build_iovec(&iov, &iovlen, opt,
386 __DECONST(void *, val),
387 strlen(val) + 1);
388 }
389 opt = pnextopt;
390 }
391 }
392 break;
393 case 'P':
394 /* obsolete for -o noresvport now default */
395 printf("-P deprecated, use -o noresvport\n");
396 build_iovec(&iov, &iovlen, "noresvport", NULL, 0);
397 break;
398 case 'R':
399 printf("-R deprecated, use -o retrycnt=<retrycnt>\n");
400 num = strtol(optarg, &p, 10);
401 if (*p || num < 0)
402 errx(1, "illegal -R value -- %s", optarg);
403 retrycnt = num;
404 break;
405 case 'r':
406 printf("-r deprecated, use -o rsize=<rsize>\n");
407 build_iovec(&iov, &iovlen, "rsize", optarg, (size_t)-1);
408 break;
409 case 's':
410 printf("-s deprecated, use -o soft\n");
411 build_iovec(&iov, &iovlen, "soft", NULL, 0);
412 softintr = true;
413 break;
414 case 'T':
415 nfsproto = IPPROTO_TCP;
416 printf("-T deprecated, use -o tcp\n");
417 break;
418 case 't':
419 printf("-t deprecated, use -o timeout=<value>\n");
420 build_iovec(&iov, &iovlen, "timeout", optarg, (size_t)-1);
421 break;
422 case 'w':
423 printf("-w deprecated, use -o wsize=<value>\n");
424 build_iovec(&iov, &iovlen, "wsize", optarg, (size_t)-1);
425 break;
426 case 'x':
427 printf("-x deprecated, use -o retrans=<value>\n");
428 build_iovec(&iov, &iovlen, "retrans", optarg, (size_t)-1);
429 break;
430 case 'U':
431 printf("-U deprecated, use -o mntudp\n");
432 mnttcp_ok = 0;
433 nfsproto = IPPROTO_UDP;
434 build_iovec(&iov, &iovlen, "mntudp", NULL, 0);
435 break;
436 default:
437 usage();
438 break;
439 }
440 argc -= optind;
441 argv += optind;
442
443 if ((opflags & (BGRND | BGRNDNOW)) == (BGRND | BGRNDNOW))
444 errx(1, "Options bg and bgnow are mutually exclusive");
445
446 if (argc != 2) {
447 usage();
448 /* NOTREACHED */
449 }
450
451 /* Warn that NFSv4 mounts only work correctly as hard mounts. */
452 if (mountmode == V4 && softintr)
453 warnx("Warning, options soft and/or intr cannot be safely used"
454 " for NFSv4. See the BUGS section of mount_nfs(8)");
455
456 spec = *argv++;
457 mntname = *argv;
458
459 if (retrycnt == -1)
460 /* The default is to keep retrying forever. */
461 retrycnt = 0;
462
463 if (modfind("nfscl") < 0) {
464 /* Not present in kernel, try loading it */
465 if (kldload("nfscl") < 0 ||
466 modfind("nfscl") < 0)
467 errx(1, "nfscl is not available");
468 }
469
470 /*
471 * Add the fqdn to the gssname, as required.
472 */
473 if (gssname != NULL) {
474 if (strchr(gssname, '@') == NULL &&
475 gethostname(hostname, MAXHOSTNAMELEN) == 0) {
476 snprintf(gssn, sizeof (gssn), "%s@%s", gssname,
477 hostname);
478 gssname = gssn;
479 }
480 build_iovec(&iov, &iovlen, "gssname",
481 __DECONST(void *, gssname), strlen(gssname) + 1);
482 }
483
484 if (!getnfsargs(&spec, &host, &iov, &iovlen))
485 exit(1);
486
487 /* resolve the mountpoint with realpath(3) */
488 if (checkpath(mntname, mntpath) != 0)
489 err(1, "%s", mntpath);
490
491 build_iovec_argf(&iov, &iovlen, "fstype", "nfs");
492 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
493 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
494
495 if (nmount(iov, iovlen, 0)) {
496 nmount_errstr = nfsv4_geterrstr(errno);
497 if (mountmode == V4 && nmount_errstr != NULL)
498 errx(1, "nmount: %s, %s", mntpath, nmount_errstr);
499 else
500 err(1, "nmount: %s%s%s", mntpath, errmsg[0] ? ", " : "",
501 errmsg);
502 } else if (mountmode != V4 && !add_mtab(host, spec)) {
503 /* Add mounted file system to PATH_MOUNTTAB */
504 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, host, spec);
505 }
506
507 exit(0);
508 }
509
510 static int
sec_name_to_num(const char * sec)511 sec_name_to_num(const char *sec)
512 {
513 if (!strcmp(sec, "krb5"))
514 return (RPCSEC_GSS_KRB5);
515 if (!strcmp(sec, "krb5i"))
516 return (RPCSEC_GSS_KRB5I);
517 if (!strcmp(sec, "krb5p"))
518 return (RPCSEC_GSS_KRB5P);
519 if (!strcmp(sec, "sys"))
520 return (AUTH_SYS);
521 return (-1);
522 }
523
524 static const char *
sec_num_to_name(int flavor)525 sec_num_to_name(int flavor)
526 {
527 switch (flavor) {
528 case RPCSEC_GSS_KRB5:
529 return ("krb5");
530 case RPCSEC_GSS_KRB5I:
531 return ("krb5i");
532 case RPCSEC_GSS_KRB5P:
533 return ("krb5p");
534 case AUTH_SYS:
535 return ("sys");
536 }
537 return (NULL);
538 }
539
540 /*
541 * Wait for RTM_IFINFO message with interface that is IFF_UP and with
542 * link on, or until timeout expires. Returns seconds left.
543 */
544 static time_t
rtm_ifinfo_sleep(time_t sec)545 rtm_ifinfo_sleep(time_t sec)
546 {
547 char buf[2048] __aligned(__alignof(struct if_msghdr));
548 fd_set rfds;
549 struct timeval tv, start;
550 ssize_t nread;
551 int n, s;
552
553 s = socket(PF_ROUTE, SOCK_RAW, 0);
554 if (s < 0)
555 err(EX_OSERR, "socket");
556 (void)gettimeofday(&start, NULL);
557
558 for (tv.tv_sec = sec, tv.tv_usec = 0;
559 tv.tv_sec > 0;
560 (void)gettimeofday(&tv, NULL),
561 tv.tv_sec = sec - (tv.tv_sec - start.tv_sec)) {
562 FD_ZERO(&rfds);
563 FD_SET(s, &rfds);
564 n = select(s + 1, &rfds, NULL, NULL, &tv);
565 if (n == 0)
566 continue;
567 if (n == -1) {
568 if (errno == EINTR)
569 continue;
570 else
571 err(EX_SOFTWARE, "select");
572 }
573 nread = read(s, buf, 2048);
574 if (nread < 0)
575 err(EX_OSERR, "read");
576 if ((size_t)nread >= sizeof(struct if_msghdr)) {
577 struct if_msghdr *ifm;
578
579 ifm = (struct if_msghdr *)buf;
580 if (ifm->ifm_version == RTM_VERSION &&
581 ifm->ifm_type == RTM_IFINFO &&
582 (ifm->ifm_flags & IFF_UP) &&
583 ifm->ifm_data.ifi_link_state != LINK_STATE_DOWN)
584 break;
585 }
586 }
587
588 close(s);
589
590 return (tv.tv_sec);
591 }
592
593 static int
getnfsargs(char ** specp,char ** hostpp,struct iovec ** iov,int * iovlen)594 getnfsargs(char **specp, char **hostpp, struct iovec **iov, int *iovlen)
595 {
596 struct addrinfo hints, *ai_nfs, *ai;
597 enum tryret ret;
598 int ecode, speclen, remoteerr, offset, have_bracket = 0;
599 char *hostp, *delimp, *errstr, *spec;
600 size_t len;
601 static char nam[MNAMELEN + 1], pname[MAXHOSTNAMELEN + 5];
602
603 spec = *specp;
604 if (*spec == '[' && (delimp = strchr(spec + 1, ']')) != NULL &&
605 *(delimp + 1) == ':') {
606 hostp = spec + 1;
607 spec = delimp + 2;
608 have_bracket = 1;
609 } else if ((delimp = strrchr(spec, ':')) != NULL) {
610 hostp = spec;
611 spec = delimp + 1;
612 } else if ((delimp = strrchr(spec, '@')) != NULL) {
613 warnx("path@server syntax is deprecated, use server:path");
614 hostp = delimp + 1;
615 } else {
616 warnx("no <host>:<dirpath> nfs-name");
617 return (0);
618 }
619 *delimp = '\0';
620
621 /*
622 * If there has been a trailing slash at mounttime it seems
623 * that some mountd implementations fail to remove the mount
624 * entries from their mountlist while unmounting.
625 */
626 for (speclen = strlen(spec);
627 speclen > 1 && spec[speclen - 1] == '/';
628 speclen--)
629 spec[speclen - 1] = '\0';
630 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
631 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
632 return (0);
633 }
634 /* Make both '@' and ':' notations equal */
635 if (*hostp != '\0') {
636 len = strlen(hostp);
637 offset = 0;
638 if (have_bracket)
639 nam[offset++] = '[';
640 memmove(nam + offset, hostp, len);
641 if (have_bracket)
642 nam[len + offset++] = ']';
643 nam[len + offset++] = ':';
644 memmove(nam + len + offset, spec, speclen);
645 nam[len + speclen + offset] = '\0';
646 }
647
648 /*
649 * Handle an internet host address.
650 */
651 memset(&hints, 0, sizeof hints);
652 hints.ai_flags = AI_NUMERICHOST;
653 if (nfsproto == IPPROTO_TCP)
654 hints.ai_socktype = SOCK_STREAM;
655 else if (nfsproto == IPPROTO_UDP)
656 hints.ai_socktype = SOCK_DGRAM;
657
658 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
659 hints.ai_flags = AI_CANONNAME;
660 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
661 != 0) {
662 if (portspec == NULL)
663 errx(1, "%s: %s", hostp, gai_strerror(ecode));
664 else
665 errx(1, "%s:%s: %s", hostp, portspec,
666 gai_strerror(ecode));
667 return (0);
668 }
669
670 /*
671 * For a Kerberized nfs mount where the "principal"
672 * argument has not been set, add it here.
673 */
674 if (got_principal == 0 && secflavor != AUTH_SYS &&
675 ai_nfs->ai_canonname != NULL) {
676 snprintf(pname, sizeof (pname), "nfs@%s",
677 ai_nfs->ai_canonname);
678 build_iovec(iov, iovlen, "principal", pname,
679 strlen(pname) + 1);
680 }
681 }
682
683 if ((opflags & (BGRNDNOW | ISBGRND)) == BGRNDNOW) {
684 warnx("Mount %s:%s, backgrounding",
685 hostp, spec);
686 opflags |= ISBGRND;
687 if (daemon(0, 0) != 0)
688 err(1, "daemon");
689 }
690
691 ret = TRYRET_LOCALERR;
692 for (;;) {
693 /*
694 * Try each entry returned by getaddrinfo(). Note the
695 * occurrence of remote errors by setting `remoteerr'.
696 */
697 remoteerr = 0;
698 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
699 if ((ai->ai_family == AF_INET6) &&
700 (opflags & OF_NOINET6))
701 continue;
702 if ((ai->ai_family == AF_INET) &&
703 (opflags & OF_NOINET4))
704 continue;
705 ret = nfs_tryproto(ai, hostp, spec, &errstr, iov,
706 iovlen);
707 if (ret == TRYRET_SUCCESS)
708 break;
709 if (ret != TRYRET_LOCALERR)
710 remoteerr = 1;
711 if ((opflags & ISBGRND) == 0)
712 fprintf(stderr, "%s\n", errstr);
713 }
714 if (ret == TRYRET_SUCCESS)
715 break;
716
717 /* Exit if all errors were local. */
718 if (!remoteerr)
719 exit(1);
720
721 /*
722 * If retrycnt == 0, we are to keep retrying forever.
723 * Otherwise decrement it, and exit if it hits zero.
724 */
725 if (retrycnt != 0 && --retrycnt == 0)
726 exit(1);
727
728 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
729 warnx("Cannot immediately mount %s:%s, backgrounding",
730 hostp, spec);
731 opflags |= ISBGRND;
732 if (daemon(0, 0) != 0)
733 err(1, "daemon");
734 }
735 /*
736 * If rtm_ifinfo_sleep() returns non-zero, don't count
737 * that as a retry attempt.
738 */
739 if (rtm_ifinfo_sleep(60) && retrycnt != 0)
740 retrycnt++;
741 }
742 freeaddrinfo(ai_nfs);
743
744 build_iovec(iov, iovlen, "hostname", nam, (size_t)-1);
745
746 *specp = spec;
747 *hostpp = hostp;
748 return (1);
749 }
750
751 /*
752 * Try to set up the NFS arguments according to the address
753 * family, protocol (and possibly port) specified in `ai'.
754 *
755 * Returns TRYRET_SUCCESS if successful, or:
756 * TRYRET_TIMEOUT The server did not respond.
757 * TRYRET_REMOTEERR The server reported an error.
758 * TRYRET_LOCALERR Local failure.
759 *
760 * In all error cases, *errstr will be set to a statically-allocated string
761 * describing the error.
762 */
763 static enum tryret
nfs_tryproto(struct addrinfo * ai,char * hostp,char * spec,char ** errstr,struct iovec ** iov,int * iovlen)764 nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr,
765 struct iovec **iov, int *iovlen)
766 {
767 static char errbuf[256];
768 struct sockaddr_storage nfs_ss;
769 struct netbuf nfs_nb;
770 struct nfhret nfhret;
771 struct timeval try;
772 struct rpc_err rpcerr;
773 CLIENT *clp;
774 struct netconfig *nconf, *nconf_mnt;
775 const char *netid, *netid_mnt, *secname;
776 int doconnect, nfsvers, mntvers, sotype;
777 enum clnt_stat clntstat;
778 enum mountmode trymntmode;
779
780 sotype = 0;
781 trymntmode = mountmode;
782 errbuf[0] = '\0';
783 *errstr = errbuf;
784
785 if (nfsproto == IPPROTO_TCP)
786 sotype = SOCK_STREAM;
787 else if (nfsproto == IPPROTO_UDP)
788 sotype = SOCK_DGRAM;
789
790 if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) {
791 snprintf(errbuf, sizeof errbuf,
792 "af %d sotype %d not supported", ai->ai_family, sotype);
793 return (TRYRET_LOCALERR);
794 }
795 if ((nconf = getnetconf_cached(netid)) == NULL) {
796 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
797 return (TRYRET_LOCALERR);
798 }
799 /* The RPCPROG_MNT netid may be different. */
800 if (mnttcp_ok) {
801 netid_mnt = netid;
802 nconf_mnt = nconf;
803 } else {
804 if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM))
805 == NULL) {
806 snprintf(errbuf, sizeof errbuf,
807 "af %d sotype SOCK_DGRAM not supported",
808 ai->ai_family);
809 return (TRYRET_LOCALERR);
810 }
811 if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) {
812 snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt,
813 nc_sperror());
814 return (TRYRET_LOCALERR);
815 }
816 }
817
818 tryagain:
819 if (trymntmode == V4) {
820 nfsvers = 4;
821 mntvers = 3; /* Workaround for GCC. */
822 } else if (trymntmode == V2) {
823 nfsvers = 2;
824 mntvers = 1;
825 } else {
826 nfsvers = 3;
827 mntvers = 3;
828 }
829
830 if (portspec != NULL) {
831 /* `ai' contains the complete nfsd sockaddr. */
832 nfs_nb.buf = ai->ai_addr;
833 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
834 } else {
835 /* Ask the remote rpcbind. */
836 nfs_nb.buf = &nfs_ss;
837 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
838
839 if (!rpcb_getaddr(NFS_PROGRAM, nfsvers, nconf, &nfs_nb,
840 hostp)) {
841 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
842 trymntmode == ANY) {
843 trymntmode = V2;
844 goto tryagain;
845 }
846 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
847 netid, hostp, spec,
848 clnt_spcreateerror("RPCPROG_NFS"));
849 return (returncode(rpc_createerr.cf_stat,
850 &rpc_createerr.cf_error));
851 }
852 }
853
854 /* Check that the server (nfsd) responds on the port we have chosen. */
855 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, NFS_PROGRAM, nfsvers,
856 0, 0);
857 if (clp == NULL) {
858 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
859 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
860 return (returncode(rpc_createerr.cf_stat,
861 &rpc_createerr.cf_error));
862 }
863 if (sotype == SOCK_DGRAM && noconn == 0) {
864 /*
865 * Use connect(), to match what the kernel does. This
866 * catches cases where the server responds from the
867 * wrong source address.
868 */
869 doconnect = 1;
870 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
871 clnt_destroy(clp);
872 snprintf(errbuf, sizeof errbuf,
873 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
874 spec);
875 return (TRYRET_LOCALERR);
876 }
877 }
878
879 try.tv_sec = 10;
880 try.tv_usec = 0;
881 clntstat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL,
882 (xdrproc_t)xdr_void, NULL, try);
883 if (clntstat != RPC_SUCCESS) {
884 if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
885 clnt_destroy(clp);
886 trymntmode = V2;
887 goto tryagain;
888 }
889 clnt_geterr(clp, &rpcerr);
890 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
891 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
892 clnt_destroy(clp);
893 return (returncode(clntstat, &rpcerr));
894 }
895 clnt_destroy(clp);
896
897 /*
898 * For NFSv4, there is no mount protocol.
899 */
900 if (trymntmode == V4) {
901 /*
902 * Store the server address in nfsargsp, making
903 * sure to copy any locally allocated structures.
904 */
905 addrlen = nfs_nb.len;
906 addr = malloc(addrlen);
907 if (addr == NULL)
908 err(1, "malloc");
909 bcopy(nfs_nb.buf, addr, addrlen);
910
911 build_iovec(iov, iovlen, "addr", addr, addrlen);
912 secname = sec_num_to_name(secflavor);
913 if (secname != NULL) {
914 build_iovec(iov, iovlen, "sec",
915 __DECONST(void *, secname), (size_t)-1);
916 }
917 build_iovec(iov, iovlen, "nfsv4", NULL, 0);
918 build_iovec(iov, iovlen, "dirpath", spec, (size_t)-1);
919
920 return (TRYRET_SUCCESS);
921 }
922
923 /*
924 * malloc() and copy the address, so that it can be used for
925 * nfsargs below.
926 */
927 addrlen = nfs_nb.len;
928 addr = malloc(addrlen);
929 if (addr == NULL)
930 err(1, "malloc");
931 bcopy(nfs_nb.buf, addr, addrlen);
932
933 /* Send the MOUNTPROC_MNT RPC to get the root filehandle. */
934 try.tv_sec = 10;
935 try.tv_usec = 0;
936 if (mntproto_port != 0) {
937 struct sockaddr *sad;
938 struct sockaddr_in *sin;
939 struct sockaddr_in6 *sin6;
940
941 sad = (struct sockaddr *)nfs_nb.buf;
942 switch (sad->sa_family) {
943 case AF_INET:
944 sin = (struct sockaddr_in *)nfs_nb.buf;
945 sin->sin_port = htons(mntproto_port);
946 break;
947 case AF_INET6:
948 sin6 = (struct sockaddr_in6 *)nfs_nb.buf;
949 sin6->sin6_port = htons(mntproto_port);
950 break;
951 default:
952 snprintf(errbuf, sizeof(errbuf),
953 "Mnt port bad addr family %d\n", sad->sa_family);
954 return (TRYRET_LOCALERR);
955 }
956 clp = clnt_tli_create(RPC_ANYFD, nconf_mnt, &nfs_nb, MOUNTPROG,
957 mntvers, 0, 0);
958 } else {
959 /* Get the Mount protocol port# via rpcbind. */
960 clp = clnt_tp_create(hostp, MOUNTPROG, mntvers, nconf_mnt);
961 }
962 if (clp == NULL) {
963 free(addr);
964 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
965 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
966 return (returncode(rpc_createerr.cf_stat,
967 &rpc_createerr.cf_error));
968 }
969 clp->cl_auth = authsys_create_default();
970 nfhret.auth = secflavor;
971 nfhret.vers = mntvers;
972 clntstat = clnt_call(clp, MOUNTPROC_MNT, (xdrproc_t)xdr_dir, spec,
973 (xdrproc_t)xdr_fh, &nfhret, try);
974 auth_destroy(clp->cl_auth);
975 if (clntstat != RPC_SUCCESS) {
976 free(addr);
977 if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
978 clnt_destroy(clp);
979 trymntmode = V2;
980 goto tryagain;
981 }
982 clnt_geterr(clp, &rpcerr);
983 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
984 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
985 clnt_destroy(clp);
986 return (returncode(clntstat, &rpcerr));
987 }
988 clnt_destroy(clp);
989
990 if (nfhret.stat != 0) {
991 free(addr);
992 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
993 hostp, spec, strerror(nfhret.stat));
994 return (TRYRET_REMOTEERR);
995 }
996
997 /*
998 * Store the filehandle and server address in nfsargsp, making
999 * sure to copy any locally allocated structures.
1000 */
1001 fhsize = nfhret.fhsize;
1002 fh = malloc(fhsize);
1003 if (fh == NULL)
1004 err(1, "malloc");
1005 bcopy(nfhret.nfh, fh, fhsize);
1006
1007 build_iovec(iov, iovlen, "addr", addr, addrlen);
1008 build_iovec(iov, iovlen, "fh", fh, fhsize);
1009 secname = sec_num_to_name(nfhret.auth);
1010 if (secname) {
1011 build_iovec(iov, iovlen, "sec",
1012 __DECONST(void *, secname), (size_t)-1);
1013 }
1014 if (nfsvers == 3)
1015 build_iovec(iov, iovlen, "nfsv3", NULL, 0);
1016
1017 return (TRYRET_SUCCESS);
1018 }
1019
1020 /*
1021 * Catagorise a RPC return status and error into an `enum tryret'
1022 * return code.
1023 */
1024 static enum tryret
returncode(enum clnt_stat clntstat,struct rpc_err * rpcerr)1025 returncode(enum clnt_stat clntstat, struct rpc_err *rpcerr)
1026 {
1027
1028 switch (clntstat) {
1029 case RPC_TIMEDOUT:
1030 return (TRYRET_TIMEOUT);
1031 case RPC_PMAPFAILURE:
1032 case RPC_PROGNOTREGISTERED:
1033 case RPC_PROGVERSMISMATCH:
1034 /* XXX, these can be local or remote. */
1035 case RPC_CANTSEND:
1036 case RPC_CANTRECV:
1037 return (TRYRET_REMOTEERR);
1038 case RPC_SYSTEMERROR:
1039 switch (rpcerr->re_errno) {
1040 case ETIMEDOUT:
1041 return (TRYRET_TIMEOUT);
1042 case ENOMEM:
1043 break;
1044 default:
1045 return (TRYRET_REMOTEERR);
1046 }
1047 /* FALLTHROUGH */
1048 default:
1049 break;
1050 }
1051 return (TRYRET_LOCALERR);
1052 }
1053
1054 /*
1055 * Look up a netid based on an address family and socket type.
1056 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
1057 *
1058 * XXX there should be a library function for this.
1059 */
1060 static const char *
netidbytype(int af,int sotype)1061 netidbytype(int af, int sotype)
1062 {
1063 struct nc_protos *p;
1064
1065 for (p = nc_protos; p->netid != NULL; p++) {
1066 if (af != p->af || sotype != p->sotype)
1067 continue;
1068 return (p->netid);
1069 }
1070 return (NULL);
1071 }
1072
1073 /*
1074 * Look up a netconfig entry based on a netid, and cache the result so
1075 * that we don't need to remember to call freenetconfigent().
1076 *
1077 * Otherwise it behaves just like getnetconfigent(), so nc_*error()
1078 * work on failure.
1079 */
1080 static struct netconfig *
getnetconf_cached(const char * netid)1081 getnetconf_cached(const char *netid)
1082 {
1083 static struct nc_entry {
1084 struct netconfig *nconf;
1085 struct nc_entry *next;
1086 } *head;
1087 struct nc_entry *p;
1088 struct netconfig *nconf;
1089
1090 for (p = head; p != NULL; p = p->next)
1091 if (strcmp(netid, p->nconf->nc_netid) == 0)
1092 return (p->nconf);
1093
1094 if ((nconf = getnetconfigent(netid)) == NULL)
1095 return (NULL);
1096 if ((p = malloc(sizeof(*p))) == NULL)
1097 err(1, "malloc");
1098 p->nconf = nconf;
1099 p->next = head;
1100 head = p;
1101
1102 return (p->nconf);
1103 }
1104
1105 /*
1106 * xdr routines for mount rpc's
1107 */
1108 static int
xdr_dir(XDR * xdrsp,char * dirp)1109 xdr_dir(XDR *xdrsp, char *dirp)
1110 {
1111 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1112 }
1113
1114 static int
xdr_fh(XDR * xdrsp,struct nfhret * np)1115 xdr_fh(XDR *xdrsp, struct nfhret *np)
1116 {
1117 int i;
1118 long auth, authcnt, authfnd = 0;
1119
1120 if (!xdr_u_long(xdrsp, &np->stat))
1121 return (0);
1122 if (np->stat)
1123 return (1);
1124 switch (np->vers) {
1125 case 1:
1126 np->fhsize = NFS_FHSIZE;
1127 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFS_FHSIZE));
1128 case 3:
1129 if (!xdr_long(xdrsp, &np->fhsize))
1130 return (0);
1131 if (np->fhsize <= 0 || np->fhsize > NFS3_FHSIZE)
1132 return (0);
1133 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1134 return (0);
1135 if (!xdr_long(xdrsp, &authcnt))
1136 return (0);
1137 for (i = 0; i < authcnt; i++) {
1138 if (!xdr_long(xdrsp, &auth))
1139 return (0);
1140 if (np->auth == -1) {
1141 np->auth = auth;
1142 authfnd++;
1143 } else if (auth == np->auth) {
1144 authfnd++;
1145 }
1146 }
1147 /*
1148 * Some servers, such as DEC's OSF/1 return a nil authenticator
1149 * list to indicate RPCAUTH_UNIX.
1150 */
1151 if (authcnt == 0 && np->auth == -1)
1152 np->auth = AUTH_SYS;
1153 if (!authfnd && (authcnt > 0 || np->auth != AUTH_SYS))
1154 np->stat = EAUTH;
1155 return (1);
1156 }
1157 return (0);
1158 }
1159
1160 static void
usage(void)1161 usage(void)
1162 {
1163 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1164 "usage: mount_nfs [-23bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]",
1165 " [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]",
1166 " [-r readsize] [-t timeout] [-w writesize] [-x retrans]",
1167 " rhost:path node");
1168 exit(1);
1169 }
1170