xref: /dragonfly/sbin/mount_nfs/mount_nfs.c (revision 1de703da)
1 /*
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
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.
23  *
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.
35  *
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.2 2003/06/17 04:27:33 dillon Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <sys/syslog.h>
46 
47 #include <rpc/rpc.h>
48 #include <rpc/pmap_clnt.h>
49 #include <rpc/pmap_prot.h>
50 
51 #ifdef NFSKERB
52 #include <kerberosIV/des.h>
53 #include <kerberosIV/krb.h>
54 #endif
55 
56 #include <nfs/rpcv2.h>
57 #include <nfs/nfsproto.h>
58 #include <nfs/nfs.h>
59 #include <nfs/nqnfs.h>
60 
61 #include <arpa/inet.h>
62 
63 #include <ctype.h>
64 #include <err.h>
65 #include <errno.h>
66 #include <netdb.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <strings.h>
70 #include <sysexits.h>
71 #include <unistd.h>
72 
73 #include "mntopts.h"
74 #include "mounttab.h"
75 
76 #define	ALTF_BG		0x1
77 #define ALTF_NOCONN	0x2
78 #define ALTF_DUMBTIMR	0x4
79 #define ALTF_INTR	0x8
80 #define ALTF_KERB	0x10
81 #define ALTF_NFSV3	0x20
82 #define ALTF_RDIRPLUS	0x40
83 #define	ALTF_MNTUDP	0x80
84 #define ALTF_RESVPORT	0x100
85 #define ALTF_SEQPACKET	0x200
86 #define ALTF_NQNFS	0x400
87 #define ALTF_SOFT	0x800
88 #define ALTF_TCP	0x1000
89 #define ALTF_PORT	0x2000
90 #define ALTF_NFSV2	0x4000
91 #define ALTF_ACREGMIN	0x8000
92 #define ALTF_ACREGMAX	0x10000
93 #define ALTF_ACDIRMIN	0x20000
94 #define ALTF_ACDIRMAX	0x40000
95 
96 struct mntopt mopts[] = {
97 	MOPT_STDOPTS,
98 	MOPT_FORCE,
99 	MOPT_UPDATE,
100 	MOPT_ASYNC,
101 	{ "bg", 0, ALTF_BG, 1 },
102 	{ "conn", 1, ALTF_NOCONN, 1 },
103 	{ "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
104 	{ "intr", 0, ALTF_INTR, 1 },
105 #ifdef NFSKERB
106 	{ "kerb", 0, ALTF_KERB, 1 },
107 #endif
108 	{ "nfsv3", 0, ALTF_NFSV3, 1 },
109 	{ "rdirplus", 0, ALTF_RDIRPLUS, 1 },
110 	{ "mntudp", 0, ALTF_MNTUDP, 1 },
111 	{ "resvport", 0, ALTF_RESVPORT, 1 },
112 	{ "nqnfs", 0, ALTF_NQNFS, 1 },
113 	{ "soft", 0, ALTF_SOFT, 1 },
114 	{ "tcp", 0, ALTF_TCP, 1 },
115 	{ "port=", 0, ALTF_PORT, 1 },
116 	{ "nfsv2", 0, ALTF_NFSV2, 1 },
117 	{ "acregmin=", 0, ALTF_ACREGMIN, 1 },
118 	{ "acregmax=", 0, ALTF_ACREGMAX, 1 },
119 	{ "acdirmin=", 0, ALTF_ACDIRMIN, 1 },
120 	{ "acdirmax=", 0, ALTF_ACDIRMAX, 1 },
121 	{ NULL }
122 };
123 
124 struct nfs_args nfsdefargs = {
125 	NFS_ARGSVERSION,
126 	(struct sockaddr *)0,
127 	sizeof (struct sockaddr_in),
128 	SOCK_DGRAM,
129 	0,
130 	(u_char *)0,
131 	0,
132 	NFSMNT_RESVPORT,
133 	NFS_WSIZE,
134 	NFS_RSIZE,
135 	NFS_READDIRSIZE,
136 	10,
137 	NFS_RETRANS,
138 	NFS_MAXGRPS,
139 	NFS_DEFRAHEAD,
140 	NQ_DEFLEASE,
141 	NQ_DEADTHRESH,
142 	(char *)0,
143 	/* args version 4 */
144 	NFS_MINATTRTIMO,
145 	NFS_MAXATTRTIMO,
146 	NFS_MINDIRATTRTIMO,
147 	NFS_MAXDIRATTRTIMO,
148 };
149 
150 struct nfhret {
151 	u_long		stat;
152 	long		vers;
153 	long		auth;
154 	long		fhsize;
155 	u_char		nfh[NFSX_V3FHMAX];
156 };
157 #define	BGRND	1
158 #define	ISBGRND	2
159 int retrycnt = -1;
160 int opflags = 0;
161 int nfsproto = IPPROTO_UDP;
162 int mnttcp_ok = 1;
163 u_short port_no = 0;
164 enum mountmode {
165 	ANY,
166 	V2,
167 	V3
168 } mountmode = ANY;
169 
170 #ifdef NFSKERB
171 char inst[INST_SZ];
172 char realm[REALM_SZ];
173 struct {
174 	u_long		kind;
175 	KTEXT_ST	kt;
176 } ktick;
177 struct nfsrpc_nickverf kverf;
178 struct nfsrpc_fullblock kin, kout;
179 NFSKERBKEY_T kivec;
180 CREDENTIALS kcr;
181 struct timeval ktv;
182 NFSKERBKEYSCHED_T kerb_keysched;
183 #endif
184 
185 /* Return codes for nfs_tryproto. */
186 enum tryret {
187 	TRYRET_SUCCESS,
188 	TRYRET_TIMEOUT,		/* No response received. */
189 	TRYRET_REMOTEERR,	/* Error received from remote server. */
190 	TRYRET_LOCALERR		/* Local failure. */
191 };
192 
193 int	getnfsargs __P((char *, struct nfs_args *));
194 void	set_rpc_maxgrouplist __P((int));
195 void	usage __P((void)) __dead2;
196 int	xdr_dir __P((XDR *, char *));
197 int	xdr_fh __P((XDR *, struct nfhret *));
198 enum tryret nfs_tryproto(struct nfs_args *nfsargsp, struct sockaddr_in *sin,
199     char *hostp, char *spec, char **errstr);
200 enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
201 
202 /*
203  * Used to set mount flags with getmntopts.  Call with dir=TRUE to
204  * initialize altflags from the current mount flags.  Call with
205  * dir=FALSE to update mount flags with the new value of altflags after
206  * the call to getmntopts.
207  */
208 static void
209 set_flags(int* altflags, int* nfsflags, int dir)
210 {
211 #define F2(af, nf)					\
212 	if (dir) {					\
213 		if (*nfsflags & NFSMNT_##nf)		\
214 			*altflags |= ALTF_##af;		\
215 		else					\
216 			*altflags &= ~ALTF_##af;	\
217 	} else {					\
218 		if (*altflags & ALTF_##af)		\
219 			*nfsflags |= NFSMNT_##nf;	\
220 		else					\
221 			*nfsflags &= ~NFSMNT_##nf;	\
222 	}
223 #define F(f)	F2(f,f)
224 
225 	F(NOCONN);
226 	F(DUMBTIMR);
227 	F2(INTR, INT);
228 #ifdef NFSKERB
229 	F(KERB);
230 #endif
231 	F(RDIRPLUS);
232 	F(RESVPORT);
233 	F(NQNFS);
234 	F(SOFT);
235 	F(ACREGMIN);
236 	F(ACREGMAX);
237 	F(ACDIRMIN);
238 	F(ACDIRMAX);
239 
240 #undef F
241 #undef F2
242 }
243 
244 int
245 main(argc, argv)
246 	int argc;
247 	char *argv[];
248 {
249 	register int c;
250 	register struct nfs_args *nfsargsp;
251 	struct nfs_args nfsargs;
252 	struct nfsd_cargs ncd;
253 	int mntflags, altflags, nfssvc_flag, num;
254 	char *name, *p, *spec;
255 	char mntpath[MAXPATHLEN];
256 	struct vfsconf vfc;
257 	int error = 0;
258 #ifdef NFSKERB
259 	uid_t last_ruid;
260 
261 	last_ruid = -1;
262 	(void)strcpy(realm, KRB_REALM);
263 	if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
264 	    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
265 	    ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
266 	    ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
267 		fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
268 #endif /* NFSKERB */
269 
270 	mntflags = 0;
271 	altflags = 0;
272 	nfsargs = nfsdefargs;
273 	nfsargsp = &nfsargs;
274 	while ((c = getopt(argc, argv,
275 	    "23a:bcdD:g:I:iKL:lm:No:PqR:r:sTt:w:x:U")) != -1)
276 		switch (c) {
277 		case '2':
278 			mountmode = V2;
279 			break;
280 		case '3':
281 			mountmode = V3;
282 			break;
283 		case 'a':
284 			num = strtol(optarg, &p, 10);
285 			if (*p || num < 0)
286 				errx(1, "illegal -a value -- %s", optarg);
287 			nfsargsp->readahead = num;
288 			nfsargsp->flags |= NFSMNT_READAHEAD;
289 			break;
290 		case 'b':
291 			opflags |= BGRND;
292 			break;
293 		case 'c':
294 			nfsargsp->flags |= NFSMNT_NOCONN;
295 			break;
296 		case 'D':
297 			num = strtol(optarg, &p, 10);
298 			if (*p || num <= 0)
299 				errx(1, "illegal -D value -- %s", optarg);
300 			nfsargsp->deadthresh = num;
301 			nfsargsp->flags |= NFSMNT_DEADTHRESH;
302 			break;
303 		case 'd':
304 			nfsargsp->flags |= NFSMNT_DUMBTIMR;
305 			break;
306 		case 'g':
307 			num = strtol(optarg, &p, 10);
308 			if (*p || num <= 0)
309 				errx(1, "illegal -g value -- %s", optarg);
310 			set_rpc_maxgrouplist(num);
311 			nfsargsp->maxgrouplist = num;
312 			nfsargsp->flags |= NFSMNT_MAXGRPS;
313 			break;
314 		case 'I':
315 			num = strtol(optarg, &p, 10);
316 			if (*p || num <= 0)
317 				errx(1, "illegal -I value -- %s", optarg);
318 			nfsargsp->readdirsize = num;
319 			nfsargsp->flags |= NFSMNT_READDIRSIZE;
320 			break;
321 		case 'i':
322 			nfsargsp->flags |= NFSMNT_INT;
323 			break;
324 #ifdef NFSKERB
325 		case 'K':
326 			nfsargsp->flags |= NFSMNT_KERB;
327 			break;
328 #endif
329 		case 'L':
330 			num = strtol(optarg, &p, 10);
331 			if (*p || num < 2)
332 				errx(1, "illegal -L value -- %s", optarg);
333 			nfsargsp->leaseterm = num;
334 			nfsargsp->flags |= NFSMNT_LEASETERM;
335 			break;
336 		case 'l':
337 			nfsargsp->flags |= NFSMNT_RDIRPLUS;
338 			break;
339 #ifdef NFSKERB
340 		case 'm':
341 			(void)strncpy(realm, optarg, REALM_SZ - 1);
342 			realm[REALM_SZ - 1] = '\0';
343 			break;
344 #endif
345 		case 'N':
346 			nfsargsp->flags &= ~NFSMNT_RESVPORT;
347 			break;
348 		case 'o':
349 			altflags = 0;
350 			set_flags(&altflags, &nfsargsp->flags, TRUE);
351 			if (mountmode == V2)
352 				altflags |= ALTF_NFSV2;
353 			else if (mountmode == V3)
354 				altflags |= ALTF_NFSV3;
355 			getmntopts(optarg, mopts, &mntflags, &altflags);
356 			set_flags(&altflags, &nfsargsp->flags, FALSE);
357 			/*
358 			 * Handle altflags which don't map directly to
359 			 * mount flags.
360 			 */
361 			if(altflags & ALTF_BG)
362 				opflags |= BGRND;
363 			if(altflags & ALTF_MNTUDP)
364 				mnttcp_ok = 0;
365 			if(altflags & ALTF_TCP) {
366 				nfsargsp->sotype = SOCK_STREAM;
367 				nfsproto = IPPROTO_TCP;
368 			}
369 			if(altflags & ALTF_PORT)
370 				port_no = atoi(strstr(optarg, "port=") + 5);
371 			mountmode = ANY;
372 			if(altflags & ALTF_NFSV2)
373 				mountmode = V2;
374 			if(altflags & ALTF_NFSV3)
375 				mountmode = V3;
376 			if(altflags & ALTF_ACREGMIN)
377 				nfsargsp->acregmin = atoi(strstr(optarg,
378 				    "acregmin=") + 9);
379 			if(altflags & ALTF_ACREGMAX)
380 				nfsargsp->acregmax = atoi(strstr(optarg,
381 				    "acregmax=") + 9);
382 			if(altflags & ALTF_ACDIRMIN)
383 				nfsargsp->acdirmin = atoi(strstr(optarg,
384 				    "acdirmin=") + 9);
385 			if(altflags & ALTF_ACDIRMAX)
386 				nfsargsp->acdirmax = atoi(strstr(optarg,
387 				    "acdirmax=") + 9);
388 			break;
389 		case 'P':
390 			/* obsolete for NFSMNT_RESVPORT, now default */
391 			break;
392 		case 'q':
393 			mountmode = V3;
394 			nfsargsp->flags |= NFSMNT_NQNFS;
395 			break;
396 		case 'R':
397 			num = strtol(optarg, &p, 10);
398 			if (*p || num < 0)
399 				errx(1, "illegal -R value -- %s", optarg);
400 			retrycnt = num;
401 			break;
402 		case 'r':
403 			num = strtol(optarg, &p, 10);
404 			if (*p || num <= 0)
405 				errx(1, "illegal -r value -- %s", optarg);
406 			nfsargsp->rsize = num;
407 			nfsargsp->flags |= NFSMNT_RSIZE;
408 			break;
409 		case 's':
410 			nfsargsp->flags |= NFSMNT_SOFT;
411 			break;
412 		case 'T':
413 			nfsargsp->sotype = SOCK_STREAM;
414 			nfsproto = IPPROTO_TCP;
415 			break;
416 		case 't':
417 			num = strtol(optarg, &p, 10);
418 			if (*p || num <= 0)
419 				errx(1, "illegal -t value -- %s", optarg);
420 			nfsargsp->timeo = num;
421 			nfsargsp->flags |= NFSMNT_TIMEO;
422 			break;
423 		case 'w':
424 			num = strtol(optarg, &p, 10);
425 			if (*p || num <= 0)
426 				errx(1, "illegal -w value -- %s", optarg);
427 			nfsargsp->wsize = num;
428 			nfsargsp->flags |= NFSMNT_WSIZE;
429 			break;
430 		case 'x':
431 			num = strtol(optarg, &p, 10);
432 			if (*p || num <= 0)
433 				errx(1, "illegal -x value -- %s", optarg);
434 			nfsargsp->retrans = num;
435 			nfsargsp->flags |= NFSMNT_RETRANS;
436 			break;
437 		case 'U':
438 			mnttcp_ok = 0;
439 			break;
440 		default:
441 			usage();
442 			break;
443 		}
444 	argc -= optind;
445 	argv += optind;
446 
447 	if (argc != 2) {
448 		usage();
449 		/* NOTREACHED */
450 	}
451 
452 	spec = *argv++;
453 	name = *argv;
454 
455 	if (retrycnt == -1)
456 		/* The default is to keep retrying forever. */
457 		retrycnt = 0;
458 	if (!getnfsargs(spec, nfsargsp))
459 		exit(1);
460 
461 	/* resolve the mountpoint with realpath(3) */
462 	(void)checkpath(name, mntpath);
463 
464 	error = getvfsbyname("nfs", &vfc);
465 	if (error && vfsisloadable("nfs")) {
466 		if(vfsload("nfs"))
467 			err(EX_OSERR, "vfsload(nfs)");
468 		endvfsent();	/* clear cache */
469 		error = getvfsbyname("nfs", &vfc);
470 	}
471 	if (error)
472 		errx(EX_OSERR, "nfs filesystem is not available");
473 
474 	if (mount(vfc.vfc_name, mntpath, mntflags, nfsargsp))
475 		err(1, "%s", mntpath);
476 	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
477 		if ((opflags & ISBGRND) == 0) {
478 			if (daemon(0, 0) != 0)
479 				err(1, "daemon");
480 		}
481 		openlog("mount_nfs", LOG_PID, LOG_DAEMON);
482 		nfssvc_flag = NFSSVC_MNTD;
483 		ncd.ncd_dirp = mntpath;
484 		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
485 			if (errno != ENEEDAUTH) {
486 				syslog(LOG_ERR, "nfssvc err %m");
487 				continue;
488 			}
489 			nfssvc_flag =
490 			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
491 #ifdef NFSKERB
492 			/*
493 			 * Set up as ncd_authuid for the kerberos call.
494 			 * Must set ruid to ncd_authuid and reset the
495 			 * ticket name iff ncd_authuid is not the same
496 			 * as last time, so that the right ticket file
497 			 * is found.
498 			 * Get the Kerberos credential structure so that
499 			 * we have the session key and get a ticket for
500 			 * this uid.
501 			 * For more info see the IETF Draft "Authentication
502 			 * in ONC RPC".
503 			 */
504 			if (ncd.ncd_authuid != last_ruid) {
505 				char buf[512];
506 				(void)sprintf(buf, "%s%d",
507 					      TKT_ROOT, ncd.ncd_authuid);
508 				krb_set_tkt_string(buf);
509 				last_ruid = ncd.ncd_authuid;
510 			}
511 			setreuid(ncd.ncd_authuid, 0);
512 			kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
513 			if (kret == RET_NOTKT) {
514 		            kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
515 				DEFAULT_TKT_LIFE);
516 			    if (kret == KSUCCESS)
517 				kret = krb_get_cred(NFS_KERBSRV, inst, realm,
518 				    &kcr);
519 			}
520 			if (kret == KSUCCESS)
521 			    kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
522 				realm, 0);
523 
524 			/*
525 			 * Fill in the AKN_FULLNAME authenticator and verifier.
526 			 * Along with the Kerberos ticket, we need to build
527 			 * the timestamp verifier and encrypt it in CBC mode.
528 			 */
529 			if (kret == KSUCCESS &&
530 			    ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
531 			    && gettimeofday(&ktv, (struct timezone *)0) == 0) {
532 			    ncd.ncd_authtype = RPCAUTH_KERB4;
533 			    ncd.ncd_authstr = (u_char *)&ktick;
534 			    ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
535 				3 * NFSX_UNSIGNED;
536 			    ncd.ncd_verfstr = (u_char *)&kverf;
537 			    ncd.ncd_verflen = sizeof (kverf);
538 			    memmove(ncd.ncd_key, kcr.session,
539 				sizeof (kcr.session));
540 			    kin.t1 = htonl(ktv.tv_sec);
541 			    kin.t2 = htonl(ktv.tv_usec);
542 			    kin.w1 = htonl(NFS_KERBTTL);
543 			    kin.w2 = htonl(NFS_KERBTTL - 1);
544 			    bzero((caddr_t)kivec, sizeof (kivec));
545 
546 			    /*
547 			     * Encrypt kin in CBC mode using the session
548 			     * key in kcr.
549 			     */
550 			    XXX
551 
552 			    /*
553 			     * Finally, fill the timestamp verifier into the
554 			     * authenticator and verifier.
555 			     */
556 			    ktick.kind = htonl(RPCAKN_FULLNAME);
557 			    kverf.kind = htonl(RPCAKN_FULLNAME);
558 			    NFS_KERBW1(ktick.kt) = kout.w1;
559 			    ktick.kt.length = htonl(ktick.kt.length);
560 			    kverf.verf.t1 = kout.t1;
561 			    kverf.verf.t2 = kout.t2;
562 			    kverf.verf.w2 = kout.w2;
563 			    nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
564 			}
565 			setreuid(0, 0);
566 #endif /* NFSKERB */
567 		}
568 	}
569 	exit(0);
570 }
571 
572 int
573 getnfsargs(spec, nfsargsp)
574 	char *spec;
575 	struct nfs_args *nfsargsp;
576 {
577 	struct hostent *hp;
578 	struct sockaddr_in saddr;
579 	enum tryret ret;
580 	int speclen, remoteerr;
581 	char *hostp, *delimp, *errstr;
582 #ifdef NFSKERB
583 	char *cp;
584 #endif
585 	size_t len;
586 	static char nam[MNAMELEN + 1];
587 
588 	if ((delimp = strrchr(spec, ':')) != NULL) {
589 		hostp = spec;
590 		spec = delimp + 1;
591 	} else if ((delimp = strrchr(spec, '@')) != NULL) {
592 		warnx("path@server syntax is deprecated, use server:path");
593 		hostp = delimp + 1;
594 	} else {
595 		warnx("no <host>:<dirpath> nfs-name");
596 		return (0);
597 	}
598 	*delimp = '\0';
599 
600 	/*
601 	 * If there has been a trailing slash at mounttime it seems
602 	 * that some mountd implementations fail to remove the mount
603 	 * entries from their mountlist while unmounting.
604 	 */
605 	for (speclen = strlen(spec);
606 		speclen > 1 && spec[speclen - 1] == '/';
607 		speclen--)
608 		spec[speclen - 1] = '\0';
609 	if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
610 		warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
611 		return (0);
612 	}
613 	/* Make both '@' and ':' notations equal */
614 	if (*hostp != '\0') {
615 		len = strlen(hostp);
616 		memmove(nam, hostp, len);
617 		nam[len] = ':';
618 		memmove(nam + len + 1, spec, speclen);
619 		nam[len + speclen + 1] = '\0';
620 	}
621 
622 	/*
623 	 * Handle an internet host address and reverse resolve it if
624 	 * doing Kerberos.
625 	 */
626 	bzero(&saddr, sizeof saddr);
627 	saddr.sin_family = AF_INET;
628 	saddr.sin_len = sizeof saddr;
629 	if (port_no != 0)
630 		saddr.sin_port = htons(port_no);
631 	if (isdigit(*hostp)) {
632 		if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
633 			warnx("bad net address %s", hostp);
634 			return (0);
635 		}
636 	} else if ((hp = gethostbyname(hostp)) != NULL)
637 		memmove(&saddr.sin_addr, hp->h_addr,
638 		    MIN(hp->h_length, sizeof(saddr.sin_addr)));
639 	else {
640 		warnx("can't get net id for host");
641 		return (0);
642         }
643 #ifdef NFSKERB
644 	if ((nfsargsp->flags & NFSMNT_KERB)) {
645 		if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
646 		    sizeof (u_long), AF_INET)) == (struct hostent *)0) {
647 			warnx("can't reverse resolve net address");
648 			return (0);
649 		}
650 		memmove(&saddr.sin_addr, hp->h_addr,
651 		    MIN(hp->h_length, sizeof(saddr.sin_addr)));
652 		strncpy(inst, hp->h_name, INST_SZ);
653 		inst[INST_SZ - 1] = '\0';
654 		if (cp = strchr(inst, '.'))
655 			*cp = '\0';
656 	}
657 #endif /* NFSKERB */
658 
659 	ret = TRYRET_LOCALERR;
660 	for (;;) {
661 		remoteerr = 0;
662 		ret = nfs_tryproto(nfsargsp, &saddr, hostp, spec, &errstr);
663 		if (ret == TRYRET_SUCCESS)
664 			break;
665 		if (ret != TRYRET_LOCALERR)
666 			remoteerr = 1;
667 		if ((opflags & ISBGRND) == 0)
668 			fprintf(stderr, "%s\n", errstr);
669 
670 		/* Exit if all errors were local. */
671 		if (!remoteerr)
672 			exit(1);
673 
674 		/*
675 		 * If retrycnt == 0, we are to keep retrying forever.
676 		 * Otherwise decrement it, and exit if it hits zero.
677 		 */
678 		if (retrycnt != 0 && --retrycnt == 0)
679 			exit(1);
680 
681 		if ((opflags & (BGRND | ISBGRND)) == BGRND) {
682 			warnx("Cannot immediately mount %s:%s, backgrounding",
683 			    hostp, spec);
684 			opflags |= ISBGRND;
685 			if (daemon(0, 0) != 0)
686 				err(1, "daemon");
687 		}
688 		sleep(60);
689 	}
690 	nfsargsp->hostname = nam;
691 	/* Add mounted filesystem to PATH_MOUNTTAB */
692 	if (!add_mtab(hostp, spec))
693 		warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
694 	return (1);
695 }
696 
697 /*
698  * Try to set up the NFS arguments according to the address
699  * (and possibly port) specified by `sinp'.
700  *
701  * Returns TRYRET_SUCCESS if successful, or:
702  *   TRYRET_TIMEOUT		The server did not respond.
703  *   TRYRET_REMOTEERR		The server reported an error.
704  *   TRYRET_LOCALERR		Local failure.
705  *
706  * In all error cases, *errstr will be set to a statically-allocated string
707  * describing the error.
708  */
709 enum tryret
710 nfs_tryproto(struct nfs_args *nfsargsp, struct sockaddr_in *sinp, char *hostp,
711     char *spec, char **errstr)
712 {
713 	static char errbuf[256];
714 	struct sockaddr_in sin, tmpsin;
715 	struct nfhret nfhret;
716 	struct timeval try;
717 	struct rpc_err rpcerr;
718 	CLIENT *clp;
719 	int doconnect, nfsvers, mntvers, so;
720 	enum clnt_stat stat;
721 	enum mountmode trymntmode;
722 
723 	trymntmode = mountmode;
724 	errbuf[0] = '\0';
725 	*errstr = errbuf;
726 	sin = tmpsin = *sinp;
727 
728 tryagain:
729 	if (trymntmode == V2) {
730 		nfsvers = 2;
731 		mntvers = 1;
732 	} else {
733 		nfsvers = 3;
734 		mntvers = 3;
735 	}
736 
737 	/* Check that the server (nfsd) responds on the port we have chosen. */
738 	try.tv_sec = 10;
739 	try.tv_usec = 0;
740 	so = RPC_ANYSOCK;
741 	if (nfsargsp->sotype == SOCK_STREAM)
742 		clp = clnttcp_create(&sin, RPCPROG_NFS, nfsvers, &so, 0, 0);
743 	else
744 		clp = clntudp_create(&sin, RPCPROG_NFS, nfsvers, try, &so);
745 	if (clp == NULL) {
746 		snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
747 		    hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
748 		return (returncode(rpc_createerr.cf_stat,
749 		    &rpc_createerr.cf_error));
750 	}
751 	if (nfsargsp->sotype == SOCK_DGRAM &&
752 	    !(nfsargsp->flags & NFSMNT_NOCONN)) {
753 		/*
754 		 * Use connect(), to match what the kernel does. This
755 		 * catches cases where the server responds from the
756 		 * wrong source address.
757 		 */
758 		doconnect = 1;
759 		if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
760 			clnt_destroy(clp);
761 			snprintf(errbuf, sizeof errbuf,
762 			    "%s:%s: CLSET_CONNECT failed", hostp, spec);
763 			return (TRYRET_LOCALERR);
764 		}
765 	}
766 
767 	try.tv_sec = 10;
768 	try.tv_usec = 0;
769 	stat = clnt_call(clp, NFSPROC_NULL, xdr_void, NULL, xdr_void, NULL,
770 	    try);
771 	if (stat != RPC_SUCCESS) {
772 		if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
773 			clnt_destroy(clp);
774 			trymntmode = V2;
775 			goto tryagain;
776 		}
777 		clnt_geterr(clp, &rpcerr);
778 		snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
779 		    hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
780 		clnt_destroy(clp);
781 		return (returncode(stat, &rpcerr));
782 	}
783 	clnt_destroy(clp);
784 
785 	/* Send the RPCMNT_MOUNT RPC to get the root filehandle. */
786 	tmpsin.sin_port = 0;
787 	try.tv_sec = 10;
788 	try.tv_usec = 0;
789 	so = RPC_ANYSOCK;
790 	if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
791 		clp = clnttcp_create(&tmpsin, RPCPROG_MNT, mntvers, &so, 0, 0);
792 	else
793 		clp = clntudp_create(&tmpsin, RPCPROG_MNT, mntvers, try, &so);
794 	if (clp == NULL) {
795 		snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
796 		    hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
797 		return (returncode(rpc_createerr.cf_stat,
798 		    &rpc_createerr.cf_error));
799 	}
800 	clp->cl_auth = authunix_create_default();
801 	if (nfsargsp->flags & NFSMNT_KERB)
802 		nfhret.auth = RPCAUTH_KERB4;
803 	else
804 		nfhret.auth = RPCAUTH_UNIX;
805 	nfhret.vers = mntvers;
806 	stat = clnt_call(clp, RPCMNT_MOUNT, xdr_dir, spec, xdr_fh, &nfhret,
807 	    try);
808 	auth_destroy(clp->cl_auth);
809 	if (stat != RPC_SUCCESS) {
810 		if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
811 			clnt_destroy(clp);
812 			trymntmode = V2;
813 			goto tryagain;
814 		}
815 		clnt_geterr(clp, &rpcerr);
816 		snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
817 		    hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
818 		clnt_destroy(clp);
819 		return (returncode(stat, &rpcerr));
820 	}
821 	clnt_destroy(clp);
822 
823 	if (nfhret.stat != 0) {
824 		snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
825 		    hostp, spec, strerror(nfhret.stat));
826 		return (TRYRET_REMOTEERR);
827 	}
828 
829 	/*
830 	 * Store the filehandle and server address in nfsargsp, making
831 	 * sure to copy any locally allocated structures.
832 	 */
833 	nfsargsp->addrlen = sin.sin_len;
834 	nfsargsp->addr = malloc(nfsargsp->addrlen);
835 	nfsargsp->fhsize = nfhret.fhsize;
836 	nfsargsp->fh = malloc(nfsargsp->fhsize);
837 	if (nfsargsp->addr == NULL || nfsargsp->fh == NULL)
838 		err(1, "malloc");
839 	bcopy(&sin, nfsargsp->addr, nfsargsp->addrlen);
840 	bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize);
841 
842 	if (nfsvers == 3)
843 		nfsargsp->flags |= NFSMNT_NFSV3;
844 	else
845 		nfsargsp->flags &= ~NFSMNT_NFSV3;
846 
847 	return (TRYRET_SUCCESS);
848 }
849 
850 /*
851  * Catagorise a RPC return status and error into an `enum tryret'
852  * return code.
853  */
854 enum tryret
855 returncode(enum clnt_stat stat, struct rpc_err *rpcerr)
856 {
857 	switch (stat) {
858 	case RPC_TIMEDOUT:
859 		return (TRYRET_TIMEOUT);
860 	case RPC_PMAPFAILURE:
861 	case RPC_PROGNOTREGISTERED:
862 	case RPC_PROGVERSMISMATCH:
863 	/* XXX, these can be local or remote. */
864 	case RPC_CANTSEND:
865 	case RPC_CANTRECV:
866 		return (TRYRET_REMOTEERR);
867 	case RPC_SYSTEMERROR:
868 		switch (rpcerr->re_errno) {
869 		case ETIMEDOUT:
870 			return (TRYRET_TIMEOUT);
871 		case ENOMEM:
872 			break;
873 		default:
874 			return (TRYRET_REMOTEERR);
875 		}
876 		/* FALLTHROUGH */
877 	default:
878 		break;
879 	}
880 	return (TRYRET_LOCALERR);
881 }
882 
883 /*
884  * xdr routines for mount rpc's
885  */
886 int
887 xdr_dir(xdrsp, dirp)
888 	XDR *xdrsp;
889 	char *dirp;
890 {
891 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
892 }
893 
894 int
895 xdr_fh(xdrsp, np)
896 	XDR *xdrsp;
897 	register struct nfhret *np;
898 {
899 	register int i;
900 	long auth, authcnt, authfnd = 0;
901 
902 	if (!xdr_u_long(xdrsp, &np->stat))
903 		return (0);
904 	if (np->stat)
905 		return (1);
906 	switch (np->vers) {
907 	case 1:
908 		np->fhsize = NFSX_V2FH;
909 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
910 	case 3:
911 		if (!xdr_long(xdrsp, &np->fhsize))
912 			return (0);
913 		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
914 			return (0);
915 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
916 			return (0);
917 		if (!xdr_long(xdrsp, &authcnt))
918 			return (0);
919 		for (i = 0; i < authcnt; i++) {
920 			if (!xdr_long(xdrsp, &auth))
921 				return (0);
922 			if (auth == np->auth)
923 				authfnd++;
924 		}
925 		/*
926 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
927 		 * list to indicate RPCAUTH_UNIX.
928 		 */
929 		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
930 			np->stat = EAUTH;
931 		return (1);
932 	};
933 	return (0);
934 }
935 
936 void
937 usage()
938 {
939 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
940 "usage: mount_nfs [-23KNPTUbcdilqs] [-D deadthresh] [-I readdirsize]",
941 "                 [-L leaseterm] [-R retrycnt] [-a maxreadahead]",
942 "                 [-g maxgroups] [-m realm] [-o options] [-r readsize]",
943 "                 [-t timeout] [-w writesize] [-x retrans] rhost:path node");
944 	exit(1);
945 }
946