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