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