Kernel - Use built-in suffix handling for "hw.physmem" kenv.
[dragonfly.git] / sbin / mount_nfs / mount_nfs.c
blobd7f690ec4bd300549603f1a4838dab5ae5816790
1 /*
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)mount_nfs.c 8.11 (Berkeley) 5/4/95
38 * $FreeBSD: src/sbin/mount_nfs/mount_nfs.c,v 1.36.2.6 2003/05/13 14:45:40 trhodes Exp $
39 * $DragonFly: src/sbin/mount_nfs/mount_nfs.c,v 1.14 2008/07/14 22:22:40 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/socket.h>
45 #include <sys/stat.h>
46 #include <sys/syslog.h>
48 #include <rpc/rpc.h>
49 #include <rpc/pmap_clnt.h>
50 #include <rpc/pmap_prot.h>
52 #include <vfs/nfs/rpcv2.h>
53 #include <vfs/nfs/nfsproto.h>
54 #include <vfs/nfs/nfs.h>
56 #include <arpa/inet.h>
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <netdb.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <sysexits.h>
67 #include <unistd.h>
69 #include "mntopts.h"
70 #include "mounttab.h"
72 #define ALTF_BG 0x1
73 #define ALTF_NOCONN 0x2
74 #define ALTF_DUMBTIMR 0x4
75 #define ALTF_INTR 0x8
76 #define ALTF_KERB 0x10
77 #define ALTF_NFSV3 0x20
78 #define ALTF_RDIRPLUS 0x40
79 #define ALTF_UNUSED080 0x80
80 #define ALTF_RESVPORT 0x100
81 #define ALTF_SEQPACKET 0x200
82 #define ALTF_UNUSED400 0x400
83 #define ALTF_SOFT 0x800
84 #define ALTF_TCP 0x1000
85 #define ALTF_PORT 0x2000
86 #define ALTF_NFSV2 0x4000
87 #define ALTF_ACREGMIN 0x8000
88 #define ALTF_ACREGMAX 0x10000
89 #define ALTF_ACDIRMIN 0x20000
90 #define ALTF_ACDIRMAX 0x40000
92 struct mntopt mopts[] = {
93 MOPT_STDOPTS,
94 MOPT_FORCE,
95 MOPT_UPDATE,
96 MOPT_ASYNC,
97 { "bg", 0, ALTF_BG, 1 },
98 { "conn", 1, ALTF_NOCONN, 1 },
99 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
100 { "intr", 0, ALTF_INTR, 1 },
101 #ifdef NFSKERB
102 { "kerb", 0, ALTF_KERB, 1 },
103 #endif
104 { "nfsv3", 0, ALTF_NFSV3, 1 },
105 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
106 { "mntudp", 1, ALTF_TCP, 1 },
107 { "resvport", 0, ALTF_RESVPORT, 1 },
108 { "soft", 0, ALTF_SOFT, 1 },
109 { "tcp", 0, ALTF_TCP, 1 },
110 { "udp", 1, ALTF_TCP, 1 },
111 { "port=", 0, ALTF_PORT, 1 },
112 { "nfsv2", 0, ALTF_NFSV2, 1 },
113 { "acregmin=", 0, ALTF_ACREGMIN, 1 },
114 { "acregmax=", 0, ALTF_ACREGMAX, 1 },
115 { "acdirmin=", 0, ALTF_ACDIRMIN, 1 },
116 { "acdirmax=", 0, ALTF_ACDIRMAX, 1 },
117 MOPT_NULL
120 struct nfs_args nfsdefargs = {
121 NFS_ARGSVERSION,
122 NULL,
123 sizeof (struct sockaddr_in),
126 NULL,
128 NFSMNT_RESVPORT,
129 NFS_WSIZE,
130 NFS_RSIZE,
131 NFS_READDIRSIZE,
133 NFS_RETRANS,
134 NFS_MAXGRPS,
135 NFS_DEFRAHEAD,
137 NFS_DEADTHRESH,
138 NULL,
139 /* args version 4 */
140 NFS_MINATTRTIMO,
141 NFS_MAXATTRTIMO,
142 NFS_MINDIRATTRTIMO,
143 NFS_MAXDIRATTRTIMO,
146 /* Table for af,sotype -> netid conversions. */
147 struct nc_protos {
148 const char *netid;
149 int af;
150 int sotype;
151 } nc_protos[] = {
152 {"udp", AF_INET, SOCK_DGRAM},
153 {"tcp", AF_INET, SOCK_STREAM},
154 {"udp6", AF_INET6, SOCK_DGRAM},
155 {"tcp6", AF_INET6, SOCK_STREAM},
156 {NULL, 0, 0}
159 struct nfhret {
160 u_long stat;
161 long vers;
162 long auth;
163 long fhsize;
164 u_char nfh[NFSX_V3FHMAX];
166 #define BGRND 1
167 #define ISBGRND 2
168 int retrycnt = -1;
169 int opflags = 0;
170 int nfsproto;
171 char *portspec = NULL; /* Server nfs port; NULL means look up via rpcbind. */
172 enum mountmode {
173 ANY,
176 } mountmode = ANY;
178 #ifdef NFSKERB
179 char inst[INST_SZ];
180 char realm[REALM_SZ];
181 struct {
182 u_long kind;
183 KTEXT_ST kt;
184 } ktick;
185 struct nfsrpc_nickverf kverf;
186 struct nfsrpc_fullblock kin, kout;
187 NFSKERBKEY_T kivec;
188 CREDENTIALS kcr;
189 struct timeval ktv;
190 NFSKERBKEYSCHED_T kerb_keysched;
191 #endif
193 /* Return codes for nfs_tryproto. */
194 enum tryret {
195 TRYRET_SUCCESS,
196 TRYRET_TIMEOUT, /* No response received. */
197 TRYRET_REMOTEERR, /* Error received from remote server. */
198 TRYRET_LOCALERR /* Local failure. */
201 #if 0
202 void set_rpc_maxgrouplist(int);
203 #endif
204 static struct netconfig *getnetconf_cached(const char *);
205 static const char *netidbytype(int, int);
206 static int getnfsargs(char *, struct nfs_args *);
207 static void usage(void) __dead2;
208 static int xdr_dir(XDR *, char *);
209 static int xdr_fh(XDR *, struct nfhret *);
210 static enum tryret
211 nfs_tryproto(struct nfs_args *, struct addrinfo *,
212 char *, char *, char **);
213 static enum tryret
214 returncode(enum clnt_stat, struct rpc_err *);
217 * Used to set mount flags with getmntopts. Call with dir=TRUE to
218 * initialize altflags from the current mount flags. Call with
219 * dir=FALSE to update mount flags with the new value of altflags after
220 * the call to getmntopts.
222 static void
223 set_flags(int* altflags, int* nfsflags, int dir)
225 #define F2(af, nf) \
226 if (dir) { \
227 if (*nfsflags & NFSMNT_##nf) \
228 *altflags |= ALTF_##af; \
229 else \
230 *altflags &= ~ALTF_##af; \
231 } else { \
232 if (*altflags & ALTF_##af) \
233 *nfsflags |= NFSMNT_##nf; \
234 else \
235 *nfsflags &= ~NFSMNT_##nf; \
237 #define F(f) F2(f,f)
239 F(NOCONN);
240 F(DUMBTIMR);
241 F2(INTR, INT);
242 #ifdef NFSKERB
243 F(KERB);
244 #endif
245 F(RDIRPLUS);
246 F(RESVPORT);
247 F(SOFT);
248 F(ACREGMIN);
249 F(ACREGMAX);
250 F(ACDIRMIN);
251 F(ACDIRMAX);
253 #undef F
254 #undef F2
258 main(int argc, char **argv)
260 int c;
261 struct nfs_args *nfsargsp;
262 struct nfs_args nfsargs;
263 struct nfsd_cargs ncd;
264 int mntflags, altflags, nfssvc_flag, num;
265 char *name, *p, *spec;
266 char mntpath[MAXPATHLEN];
267 struct vfsconf vfc;
268 int error = 0;
269 #ifdef NFSKERB
270 uid_t last_ruid;
272 last_ruid = -1;
273 strcpy(realm, KRB_REALM);
274 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
275 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
276 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
277 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
278 fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
279 #endif /* NFSKERB */
281 mntflags = 0;
282 altflags = ALTF_TCP;
283 nfsargs = nfsdefargs;
284 nfsargsp = &nfsargs;
285 if (altflags & ALTF_TCP) {
286 nfsproto = IPPROTO_TCP;
287 nfsargsp->sotype = SOCK_STREAM;
288 } else {
289 nfsproto = IPPROTO_UDP;
290 nfsargsp->sotype = SOCK_DGRAM;
292 while ((c = getopt(argc, argv,
293 "23a:bcdD:g:I:iKlm:No:PR:r:sTt:w:x:U")) != -1)
294 switch (c) {
295 case '2':
296 mountmode = V2;
297 break;
298 case '3':
299 mountmode = V3;
300 break;
301 case 'a':
302 num = strtol(optarg, &p, 10);
303 if (*p || num < 0)
304 errx(1, "illegal -a value -- %s", optarg);
305 nfsargsp->readahead = num;
306 nfsargsp->flags |= NFSMNT_READAHEAD;
307 break;
308 case 'b':
309 opflags |= BGRND;
310 break;
311 case 'c':
312 nfsargsp->flags |= NFSMNT_NOCONN;
313 break;
314 case 'D':
315 num = strtol(optarg, &p, 10);
316 if (*p || num <= 0)
317 errx(1, "illegal -D value -- %s", optarg);
318 nfsargsp->deadthresh = num;
319 nfsargsp->flags |= NFSMNT_DEADTHRESH;
320 break;
321 case 'd':
322 nfsargsp->flags |= NFSMNT_DUMBTIMR;
323 break;
324 #if 0 /* XXXX */
325 case 'g':
326 num = strtol(optarg, &p, 10);
327 if (*p || num <= 0)
328 errx(1, "illegal -g value -- %s", optarg);
329 set_rpc_maxgrouplist(num);
330 nfsargsp->maxgrouplist = num;
331 nfsargsp->flags |= NFSMNT_MAXGRPS;
332 break;
333 #endif
334 case 'I':
335 num = strtol(optarg, &p, 10);
336 if (*p || num <= 0)
337 errx(1, "illegal -I value -- %s", optarg);
338 nfsargsp->readdirsize = num;
339 nfsargsp->flags |= NFSMNT_READDIRSIZE;
340 break;
341 case 'i':
342 nfsargsp->flags |= NFSMNT_INT;
343 break;
344 #ifdef NFSKERB
345 case 'K':
346 nfsargsp->flags |= NFSMNT_KERB;
347 break;
348 #endif
349 case 'l':
350 nfsargsp->flags |= NFSMNT_RDIRPLUS;
351 break;
352 #ifdef NFSKERB
353 case 'm':
354 strncpy(realm, optarg, REALM_SZ - 1);
355 realm[REALM_SZ - 1] = '\0';
356 break;
357 #endif
358 case 'N':
359 nfsargsp->flags &= ~NFSMNT_RESVPORT;
360 break;
361 case 'o':
362 altflags = 0;
363 set_flags(&altflags, &nfsargsp->flags, TRUE);
364 if (nfsproto == IPPROTO_TCP)
365 altflags |= ALTF_TCP;
366 if (mountmode == V2)
367 altflags |= ALTF_NFSV2;
368 else if (mountmode == V3)
369 altflags |= ALTF_NFSV3;
370 getmntopts(optarg, mopts, &mntflags, &altflags);
371 set_flags(&altflags, &nfsargsp->flags, FALSE);
373 * Handle altflags which don't map directly to
374 * mount flags.
376 if(altflags & ALTF_BG)
377 opflags |= BGRND;
378 if (altflags & ALTF_TCP) {
379 nfsproto = IPPROTO_TCP;
380 nfsargsp->sotype = SOCK_STREAM;
381 } else {
382 nfsproto = IPPROTO_UDP;
383 nfsargsp->sotype = SOCK_DGRAM;
385 if(altflags & ALTF_PORT) {
387 * XXX Converting from a string to an int
388 * and back again is silly, and we should
389 * allow /etc/services names.
391 asprintf(&portspec, "%d",
392 atoi(strstr(optarg, "port=") + 5));
393 if (portspec == NULL)
394 err(1, "asprintf");
396 mountmode = ANY;
397 if(altflags & ALTF_NFSV2)
398 mountmode = V2;
399 if(altflags & ALTF_NFSV3)
400 mountmode = V3;
401 if(altflags & ALTF_ACREGMIN)
402 nfsargsp->acregmin = atoi(strstr(optarg,
403 "acregmin=") + 9);
404 if(altflags & ALTF_ACREGMAX)
405 nfsargsp->acregmax = atoi(strstr(optarg,
406 "acregmax=") + 9);
407 if(altflags & ALTF_ACDIRMIN)
408 nfsargsp->acdirmin = atoi(strstr(optarg,
409 "acdirmin=") + 9);
410 if(altflags & ALTF_ACDIRMAX)
411 nfsargsp->acdirmax = atoi(strstr(optarg,
412 "acdirmax=") + 9);
413 break;
414 case 'P':
415 /* obsolete for NFSMNT_RESVPORT, now default */
416 break;
417 case 'R':
418 num = strtol(optarg, &p, 10);
419 if (*p || num < 0)
420 errx(1, "illegal -R value -- %s", optarg);
421 retrycnt = num;
422 break;
423 case 'r':
424 num = strtol(optarg, &p, 10);
425 if (*p || num <= 0)
426 errx(1, "illegal -r value -- %s", optarg);
427 nfsargsp->rsize = num;
428 nfsargsp->flags |= NFSMNT_RSIZE;
429 break;
430 case 's':
431 nfsargsp->flags |= NFSMNT_SOFT;
432 break;
433 case 'T':
434 nfsargsp->sotype = SOCK_STREAM;
435 nfsproto = IPPROTO_TCP;
436 altflags |= ALTF_TCP;
437 break;
438 case 't':
439 num = strtol(optarg, &p, 10);
440 if (*p || num <= 0)
441 errx(1, "illegal -t value -- %s", optarg);
442 nfsargsp->timeo = num;
443 nfsargsp->flags |= NFSMNT_TIMEO;
444 break;
445 case 'w':
446 num = strtol(optarg, &p, 10);
447 if (*p || num <= 0)
448 errx(1, "illegal -w value -- %s", optarg);
449 nfsargsp->wsize = num;
450 nfsargsp->flags |= NFSMNT_WSIZE;
451 break;
452 case 'x':
453 num = strtol(optarg, &p, 10);
454 if (*p || num <= 0)
455 errx(1, "illegal -x value -- %s", optarg);
456 nfsargsp->retrans = num;
457 nfsargsp->flags |= NFSMNT_RETRANS;
458 break;
459 case 'U':
460 nfsargsp->sotype = SOCK_DGRAM;
461 nfsproto = IPPROTO_UDP;
462 altflags &= ~ALTF_TCP;
463 break;
464 default:
465 usage();
466 break;
468 argc -= optind;
469 argv += optind;
471 if (argc != 2) {
472 usage();
473 /* NOTREACHED */
476 spec = *argv++;
477 name = *argv;
479 if (retrycnt == -1)
480 /* The default is to keep retrying forever. */
481 retrycnt = 0;
482 if (!getnfsargs(spec, nfsargsp))
483 exit(1);
485 /* resolve the mountpoint with realpath(3) */
486 checkpath(name, mntpath);
488 error = getvfsbyname("nfs", &vfc);
489 if (error && vfsisloadable("nfs")) {
490 if(vfsload("nfs"))
491 err(EX_OSERR, "vfsload(nfs)");
492 endvfsent(); /* clear cache */
493 error = getvfsbyname("nfs", &vfc);
495 if (error)
496 errx(EX_OSERR, "nfs filesystem is not available");
498 if (mount(vfc.vfc_name, mntpath, mntflags, nfsargsp))
499 err(1, "%s", mntpath);
500 if (nfsargsp->flags & NFSMNT_KERB) {
501 if ((opflags & ISBGRND) == 0) {
502 if (daemon(0, 0) != 0)
503 err(1, "daemon");
505 openlog("mount_nfs", LOG_PID, LOG_DAEMON);
506 nfssvc_flag = NFSSVC_MNTD;
507 ncd.ncd_dirp = mntpath;
508 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
509 if (errno != ENEEDAUTH) {
510 syslog(LOG_ERR, "nfssvc err %m");
511 continue;
513 nfssvc_flag =
514 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
515 #ifdef NFSKERB
517 * Set up as ncd_authuid for the kerberos call.
518 * Must set ruid to ncd_authuid and reset the
519 * ticket name iff ncd_authuid is not the same
520 * as last time, so that the right ticket file
521 * is found.
522 * Get the Kerberos credential structure so that
523 * we have the session key and get a ticket for
524 * this uid.
525 * For more info see the IETF Draft "Authentication
526 * in ONC RPC".
528 if (ncd.ncd_authuid != last_ruid) {
529 char buf[512];
530 sprintf(buf, "%s%d", TKT_ROOT, ncd.ncd_authuid);
531 krb_set_tkt_string(buf);
532 last_ruid = ncd.ncd_authuid;
534 setreuid(ncd.ncd_authuid, 0);
535 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
536 if (kret == RET_NOTKT) {
537 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
538 DEFAULT_TKT_LIFE);
539 if (kret == KSUCCESS)
540 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
541 &kcr);
543 if (kret == KSUCCESS)
544 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
545 realm, 0);
548 * Fill in the AKN_FULLNAME authenticator and verifier.
549 * Along with the Kerberos ticket, we need to build
550 * the timestamp verifier and encrypt it in CBC mode.
552 if (kret == KSUCCESS &&
553 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
554 && gettimeofday(&ktv, NULL) == 0) {
555 ncd.ncd_authtype = RPCAUTH_KERB4;
556 ncd.ncd_authstr = (u_char *)&ktick;
557 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
558 3 * NFSX_UNSIGNED;
559 ncd.ncd_verfstr = (u_char *)&kverf;
560 ncd.ncd_verflen = sizeof (kverf);
561 memmove(ncd.ncd_key, kcr.session,
562 sizeof (kcr.session));
563 kin.t1 = htonl(ktv.tv_sec);
564 kin.t2 = htonl(ktv.tv_usec);
565 kin.w1 = htonl(NFS_KERBTTL);
566 kin.w2 = htonl(NFS_KERBTTL - 1);
567 bzero((caddr_t)kivec, sizeof (kivec));
570 * Encrypt kin in CBC mode using the session
571 * key in kcr.
576 * Finally, fill the timestamp verifier into the
577 * authenticator and verifier.
579 ktick.kind = htonl(RPCAKN_FULLNAME);
580 kverf.kind = htonl(RPCAKN_FULLNAME);
581 NFS_KERBW1(ktick.kt) = kout.w1;
582 ktick.kt.length = htonl(ktick.kt.length);
583 kverf.verf.t1 = kout.t1;
584 kverf.verf.t2 = kout.t2;
585 kverf.verf.w2 = kout.w2;
586 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
588 setreuid(0, 0);
589 #endif /* NFSKERB */
592 exit(0);
595 static int
596 getnfsargs(char *spec, struct nfs_args *nfsargsp)
598 struct addrinfo hints, *ai_nfs, *ai;
599 #ifdef NFSKERB
600 char host[NI_MAXHOST], serv[NI_MAXSERV];
601 #endif
602 enum tryret ret;
603 int ecode, speclen, remoteerr;
604 char *hostp, *delimp, *errstr;
605 #ifdef NFSKERB
606 char *cp;
607 #endif
608 size_t len;
609 static char nam[MNAMELEN + 1];
611 if ((delimp = strrchr(spec, ':')) != NULL) {
612 hostp = spec;
613 spec = delimp + 1;
614 } else if ((delimp = strrchr(spec, '@')) != NULL) {
615 warnx("path@server syntax is deprecated, use server:path");
616 hostp = delimp + 1;
617 } else {
618 warnx("no <host>:<dirpath> nfs-name");
619 return (0);
621 *delimp = '\0';
624 * If there has been a trailing slash at mounttime it seems
625 * that some mountd implementations fail to remove the mount
626 * entries from their mountlist while unmounting.
628 for (speclen = strlen(spec);
629 speclen > 1 && spec[speclen - 1] == '/';
630 speclen--)
631 spec[speclen - 1] = '\0';
632 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
633 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
634 return (0);
636 /* Make both '@' and ':' notations equal */
637 if (*hostp != '\0') {
638 len = strlen(hostp);
639 memmove(nam, hostp, len);
640 nam[len] = ':';
641 memmove(nam + len + 1, spec, speclen);
642 nam[len + speclen + 1] = '\0';
646 * Handle an internet host address and reverse resolve it if
647 * doing Kerberos.
649 memset(&hints, 0, sizeof hints);
650 hints.ai_flags = AI_NUMERICHOST;
651 hints.ai_socktype = nfsargsp->sotype;
652 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) == 0) {
653 #ifdef NFSKERB
654 if ((nfsargsp->flags & NFSMNT_KERB)) {
655 hints.ai_flags = 0;
656 if (getnameinfo(ai_nfs->ai_addr, ai_nfs->ai_addrlen,
657 host, sizeof host, serv, sizeof serv, 0) != 0) {
658 warnx("can't reverse resolve net address");
659 return (0);
661 hostp = host;
663 #endif /* NFSKERB */
664 } else {
665 hints.ai_flags = 0;
666 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
667 != 0) {
668 if (portspec == NULL)
669 errx(1, "%s: %s", hostp, gai_strerror(ecode));
670 else
671 errx(1, "%s:%s: %s", hostp, portspec,
672 gai_strerror(ecode));
673 return (0);
676 #ifdef NFSKERB
677 if (nfsargsp->flags & NFSMNT_KERB) {
678 strncpy(inst, hp->h_name, INST_SZ);
679 inst[INST_SZ - 1] = '\0';
680 if (cp = strchr(inst, '.'))
681 *cp = '\0';
683 #endif /* NFSKERB */
685 ret = TRYRET_LOCALERR;
686 for (;;) {
688 * Try each entry returned by getaddrinfo(). Note the
689 * occurence of remote errors by setting `remoteerr'.
691 remoteerr = 0;
692 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
693 ret = nfs_tryproto(nfsargsp, ai, hostp, spec, &errstr);
694 if (ret == TRYRET_SUCCESS)
695 break;
696 if (ret != TRYRET_LOCALERR)
697 remoteerr = 1;
698 if ((opflags & ISBGRND) == 0)
699 fprintf(stderr, "%s\n", errstr);
701 if (ret == TRYRET_SUCCESS)
702 break;
704 /* Exit if all errors were local. */
705 if (!remoteerr)
706 exit(1);
709 * If retrycnt == 0, we are to keep retrying forever.
710 * Otherwise decrement it, and exit if it hits zero.
712 if (retrycnt != 0 && --retrycnt == 0)
713 exit(1);
715 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
716 warnx("Cannot immediately mount %s:%s, backgrounding",
717 hostp, spec);
718 opflags |= ISBGRND;
719 if (daemon(0, 0) != 0)
720 err(1, "daemon");
722 sleep(60);
724 freeaddrinfo(ai_nfs);
725 nfsargsp->hostname = nam;
726 /* Add mounted filesystem to PATH_MOUNTTAB */
727 if (!add_mtab(hostp, spec))
728 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
729 return (1);
733 * Try to set up the NFS arguments according to the address
734 * family, protocol (and possibly port) specified in `ai'.
736 * Returns TRYRET_SUCCESS if successful, or:
737 * TRYRET_TIMEOUT The server did not respond.
738 * TRYRET_REMOTEERR The server reported an error.
739 * TRYRET_LOCALERR Local failure.
741 * In all error cases, *errstr will be set to a statically-allocated string
742 * describing the error.
744 static enum tryret
745 nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, char *hostp,
746 char *spec, char **errstr)
748 static char errbuf[256];
749 struct sockaddr_storage nfs_ss;
750 struct netbuf nfs_nb;
751 struct nfhret nfhret;
752 struct timeval try;
753 struct rpc_err rpcerr;
754 CLIENT *clp;
755 struct netconfig *nconf, *nconf_mnt;
756 const char *netid, *netid_mnt;
757 int doconnect, nfsvers, mntvers;
758 enum clnt_stat status;
759 enum mountmode trymntmode;
761 trymntmode = mountmode;
762 errbuf[0] = '\0';
763 *errstr = errbuf;
765 if ((netid = netidbytype(ai->ai_family, nfsargsp->sotype)) == NULL) {
766 snprintf(errbuf, sizeof errbuf,
767 "af %d sotype %d not supported", ai->ai_family,
768 nfsargsp->sotype);
769 return (TRYRET_LOCALERR);
771 if ((nconf = getnetconf_cached(netid)) == NULL) {
772 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
773 return (TRYRET_LOCALERR);
776 netid_mnt = netid;
777 nconf_mnt = nconf;
779 tryagain:
780 if (trymntmode == V2) {
781 nfsvers = 2;
782 mntvers = 1;
783 } else {
784 nfsvers = 3;
785 mntvers = 3;
788 if (portspec != NULL) {
789 /* `ai' contains the complete nfsd sockaddr. */
790 nfs_nb.buf = ai->ai_addr;
791 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
792 } else {
793 /* Ask the remote rpcbind. */
794 nfs_nb.buf = &nfs_ss;
795 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
797 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb,
798 hostp)) {
799 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
800 trymntmode == ANY) {
801 trymntmode = V2;
802 goto tryagain;
804 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
805 netid, hostp, spec,
806 clnt_spcreateerror("RPCPROG_NFS"));
807 return (returncode(rpc_createerr.cf_stat,
808 &rpc_createerr.cf_error));
812 /* Check that the server (nfsd) responds on the port we have chosen. */
813 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, RPCPROG_NFS, nfsvers,
814 0, 0);
815 if (clp == NULL) {
816 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
817 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
818 return (returncode(rpc_createerr.cf_stat,
819 &rpc_createerr.cf_error));
821 if (nfsargsp->sotype == SOCK_DGRAM &&
822 !(nfsargsp->flags & NFSMNT_NOCONN)) {
824 * Use connect(), to match what the kernel does. This
825 * catches cases where the server responds from the
826 * wrong source address.
828 doconnect = 1;
829 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
830 clnt_destroy(clp);
831 snprintf(errbuf, sizeof errbuf,
832 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
833 spec);
834 return (TRYRET_LOCALERR);
838 try.tv_sec = 10;
839 try.tv_usec = 0;
840 status = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL,
841 (xdrproc_t)xdr_void, NULL, try);
842 if (status != RPC_SUCCESS) {
843 if (status == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
844 clnt_destroy(clp);
845 trymntmode = V2;
846 goto tryagain;
848 clnt_geterr(clp, &rpcerr);
849 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
850 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
851 clnt_destroy(clp);
852 return (returncode(status, &rpcerr));
854 clnt_destroy(clp);
856 /* Send the RPCMNT_MOUNT RPC to get the root filehandle. */
857 try.tv_sec = 10;
858 try.tv_usec = 0;
859 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers, nconf_mnt);
860 if (clp == NULL) {
861 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
862 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
863 return (returncode(rpc_createerr.cf_stat,
864 &rpc_createerr.cf_error));
866 clp->cl_auth = authsys_create_default();
867 if (nfsargsp->flags & NFSMNT_KERB)
868 nfhret.auth = RPCAUTH_KERB4;
869 else
870 nfhret.auth = RPCAUTH_UNIX;
871 nfhret.vers = mntvers;
872 status = clnt_call(clp, RPCMNT_MOUNT, (xdrproc_t)xdr_dir, spec,
873 (xdrproc_t)xdr_fh, &nfhret, try);
874 auth_destroy(clp->cl_auth);
875 if (status != RPC_SUCCESS) {
876 if (status == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
877 clnt_destroy(clp);
878 trymntmode = V2;
879 goto tryagain;
881 clnt_geterr(clp, &rpcerr);
882 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
883 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
884 clnt_destroy(clp);
885 return (returncode(status, &rpcerr));
887 clnt_destroy(clp);
889 if (nfhret.stat != 0) {
890 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
891 hostp, spec, strerror(nfhret.stat));
892 return (TRYRET_REMOTEERR);
896 * Store the filehandle and server address in nfsargsp, making
897 * sure to copy any locally allocated structures.
899 nfsargsp->addrlen = nfs_nb.len;
900 nfsargsp->addr = malloc(nfsargsp->addrlen);
901 nfsargsp->fhsize = nfhret.fhsize;
902 nfsargsp->fh = malloc(nfsargsp->fhsize);
903 if (nfsargsp->addr == NULL || nfsargsp->fh == NULL)
904 err(1, "malloc");
905 bcopy(nfs_nb.buf, nfsargsp->addr, nfsargsp->addrlen);
906 bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize);
908 if (nfsvers == 3)
909 nfsargsp->flags |= NFSMNT_NFSV3;
910 else
911 nfsargsp->flags &= ~NFSMNT_NFSV3;
913 return (TRYRET_SUCCESS);
918 * Catagorise a RPC return status and error into an `enum tryret'
919 * return code.
921 static enum tryret
922 returncode(enum clnt_stat status, struct rpc_err *rpcerr)
924 switch (status) {
925 case RPC_TIMEDOUT:
926 return (TRYRET_TIMEOUT);
927 case RPC_PMAPFAILURE:
928 case RPC_PROGNOTREGISTERED:
929 case RPC_PROGVERSMISMATCH:
930 /* XXX, these can be local or remote. */
931 case RPC_CANTSEND:
932 case RPC_CANTRECV:
933 return (TRYRET_REMOTEERR);
934 case RPC_SYSTEMERROR:
935 switch (rpcerr->re_errno) {
936 case ETIMEDOUT:
937 return (TRYRET_TIMEOUT);
938 case ENOMEM:
939 break;
940 default:
941 return (TRYRET_REMOTEERR);
943 /* FALLTHROUGH */
944 default:
945 break;
947 return (TRYRET_LOCALERR);
951 * Look up a netid based on an address family and socket type.
952 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
954 * XXX there should be a library function for this.
956 static const char *
957 netidbytype(int af, int sotype)
959 struct nc_protos *p;
961 for (p = nc_protos; p->netid != NULL; p++) {
962 if (af != p->af || sotype != p->sotype)
963 continue;
964 return (p->netid);
966 return (NULL);
970 * Look up a netconfig entry based on a netid, and cache the result so
971 * that we don't need to remember to call freenetconfigent().
973 * Otherwise it behaves just like getnetconfigent(), so nc_*error()
974 * work on failure.
976 static struct netconfig *
977 getnetconf_cached(const char *netid)
979 static struct nc_entry {
980 struct netconfig *nconf;
981 struct nc_entry *next;
982 } *head;
983 struct nc_entry *p;
984 struct netconfig *nconf;
986 for (p = head; p != NULL; p = p->next)
987 if (strcmp(netid, p->nconf->nc_netid) == 0)
988 return (p->nconf);
990 if ((nconf = getnetconfigent(netid)) == NULL)
991 return (NULL);
992 if ((p = malloc(sizeof(*p))) == NULL)
993 err(1, "malloc");
994 p->nconf = nconf;
995 p->next = head;
996 head = p;
998 return (p->nconf);
1002 * xdr routines for mount rpc's
1004 static int
1005 xdr_dir(XDR *xdrsp, char *dirp)
1007 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
1010 static int
1011 xdr_fh(XDR *xdrsp, struct nfhret *np)
1013 int i;
1014 long auth, authcnt, authfnd = 0;
1016 if (!xdr_u_long(xdrsp, &np->stat))
1017 return (0);
1018 if (np->stat)
1019 return (1);
1020 switch (np->vers) {
1021 case 1:
1022 np->fhsize = NFSX_V2FH;
1023 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
1024 case 3:
1025 if (!xdr_long(xdrsp, &np->fhsize))
1026 return (0);
1027 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
1028 return (0);
1029 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1030 return (0);
1031 if (!xdr_long(xdrsp, &authcnt))
1032 return (0);
1033 for (i = 0; i < authcnt; i++) {
1034 if (!xdr_long(xdrsp, &auth))
1035 return (0);
1036 if (auth == np->auth)
1037 authfnd++;
1040 * Some servers, such as DEC's OSF/1 return a nil authenticator
1041 * list to indicate RPCAUTH_UNIX.
1043 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
1044 np->stat = EAUTH;
1045 return (1);
1047 return (0);
1050 static void
1051 usage(void)
1053 fprintf(stderr, "%s\n%s\n%s\n%s\n",
1054 "usage: mount_nfs [-23KNPTUbcdils] [-D deadthresh] [-I readdirsize]",
1055 " [-R retrycnt] [-a maxreadahead]",
1056 " [-g maxgroups] [-m realm] [-o options] [-r readsize]",
1057 " [-t timeout] [-w writesize] [-x retrans] rhost:path node");
1058 exit(1);