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