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