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