mount_nfs.c revision 1.23 1 /* $NetBSD: mount_nfs.c,v 1.23 1999/06/25 19:28:37 perseant 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.23 1999/06/25 19:28:37 perseant 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/stat.h>
57 #include <syslog.h>
58
59 #include <rpc/rpc.h>
60 #include <rpc/pmap_clnt.h>
61 #include <rpc/pmap_prot.h>
62
63 #ifdef ISO
64 #include <netiso/iso.h>
65 #endif
66
67 #ifdef NFSKERB
68 #include <kerberosIV/des.h>
69 #include <kerberosIV/krb.h>
70 #endif
71
72 #include <nfs/rpcv2.h>
73 #include <nfs/nfsproto.h>
74 #define _KERNEL
75 #include <nfs/nfs.h>
76 #undef _KERNEL
77 #include <nfs/nqnfs.h>
78 #include <nfs/nfsmount.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 <string.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_NORESPORT 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 { "noresport", 0, ALTF_NORESPORT, 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|NFSMNT_RESVPORT,
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 int main __P((int, char *[]));
193 void set_rpc_maxgrouplist __P((int));
194 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 int c;
204 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, *ospec;
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:UX")) != -1)
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_NORESPORT)
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 case 'p':
362 nfsargsp->flags &= ~NFSMNT_RESVPORT;
363 break;
364 case 'q':
365 if (force2)
366 errx(1,"nqnfs only available with v3");
367 force3 = 1;
368 nfsargsp->flags |= NFSMNT_NQNFS;
369 break;
370 case 'R':
371 num = strtol(optarg, &p, 10);
372 if (*p || num <= 0)
373 errx(1, "illegal -R value -- %s", optarg);
374 retrycnt = num;
375 break;
376 case 'r':
377 num = strtol(optarg, &p, 10);
378 if (*p || num <= 0)
379 errx(1, "illegal -r value -- %s", optarg);
380 nfsargsp->rsize = num;
381 nfsargsp->flags |= NFSMNT_RSIZE;
382 break;
383 #ifdef ISO
384 case 'S':
385 nfsargsp->sotype = SOCK_SEQPACKET;
386 break;
387 #endif
388 case 's':
389 nfsargsp->flags |= NFSMNT_SOFT;
390 break;
391 case 'T':
392 nfsargsp->sotype = SOCK_STREAM;
393 nfsproto = IPPROTO_TCP;
394 break;
395 case 't':
396 num = strtol(optarg, &p, 10);
397 if (*p || num <= 0)
398 errx(1, "illegal -t value -- %s", optarg);
399 nfsargsp->timeo = num;
400 nfsargsp->flags |= NFSMNT_TIMEO;
401 break;
402 case 'w':
403 num = strtol(optarg, &p, 10);
404 if (*p || num <= 0)
405 errx(1, "illegal -w value -- %s", optarg);
406 nfsargsp->wsize = num;
407 nfsargsp->flags |= NFSMNT_WSIZE;
408 break;
409 case 'x':
410 num = strtol(optarg, &p, 10);
411 if (*p || num <= 0)
412 errx(1, "illegal -x value -- %s", optarg);
413 nfsargsp->retrans = num;
414 nfsargsp->flags |= NFSMNT_RETRANS;
415 break;
416 case 'X':
417 nfsargsp->flags |= NFSMNT_XLATECOOKIE;
418 break;
419 case 'U':
420 mnttcp_ok = 0;
421 break;
422 default:
423 usage();
424 break;
425 }
426 argc -= optind;
427 argv += optind;
428
429 if (argc != 2)
430 usage();
431
432 spec = *argv++;
433 name = *argv;
434 if((ospec = strdup(spec))==NULL) {
435 err(1,"strdup");
436 }
437
438 if (!getnfsargs(spec, nfsargsp))
439 exit(1);
440 if (mount(MOUNT_NFS, name, mntflags, nfsargsp))
441 err(1, "%s on %s", ospec, name);
442 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
443 if ((opflags & ISBGRND) == 0) {
444 if ((i = fork()) != 0) {
445 if (i == -1)
446 err(1, "nqnfs 1");
447 exit(0);
448 }
449 (void) setsid();
450 (void) close(STDIN_FILENO);
451 (void) close(STDOUT_FILENO);
452 (void) close(STDERR_FILENO);
453 (void) chdir("/");
454 }
455 openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
456 nfssvc_flag = NFSSVC_MNTD;
457 ncd.ncd_dirp = name;
458 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
459 if (errno != ENEEDAUTH) {
460 syslog(LOG_ERR, "nfssvc err %m");
461 continue;
462 }
463 nfssvc_flag =
464 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
465 #ifdef NFSKERB
466 /*
467 * Set up as ncd_authuid for the kerberos call.
468 * Must set ruid to ncd_authuid and reset the
469 * ticket name iff ncd_authuid is not the same
470 * as last time, so that the right ticket file
471 * is found.
472 * Get the Kerberos credential structure so that
473 * we have the seesion key and get a ticket for
474 * this uid.
475 * For more info see the IETF Draft "Authentication
476 * in ONC RPC".
477 */
478 if (ncd.ncd_authuid != last_ruid) {
479 krb_set_tkt_string("");
480 last_ruid = ncd.ncd_authuid;
481 }
482 setreuid(ncd.ncd_authuid, 0);
483 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
484 if (kret == RET_NOTKT) {
485 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
486 DEFAULT_TKT_LIFE);
487 if (kret == KSUCCESS)
488 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
489 &kcr);
490 }
491 if (kret == KSUCCESS)
492 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
493 realm, 0);
494
495 /*
496 * Fill in the AKN_FULLNAME authenticator and verfier.
497 * Along with the Kerberos ticket, we need to build
498 * the timestamp verifier and encrypt it in CBC mode.
499 */
500 if (kret == KSUCCESS &&
501 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
502 && gettimeofday(&ktv, (struct timezone *)0) == 0) {
503 ncd.ncd_authtype = RPCAUTH_KERB4;
504 ncd.ncd_authstr = (u_char *)&ktick;
505 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
506 3 * NFSX_UNSIGNED;
507 ncd.ncd_verfstr = (u_char *)&kverf;
508 ncd.ncd_verflen = sizeof (kverf);
509 memmove(ncd.ncd_key, kcr.session,
510 sizeof (kcr.session));
511 kin.t1 = htonl(ktv.tv_sec);
512 kin.t2 = htonl(ktv.tv_usec);
513 kin.w1 = htonl(NFS_KERBTTL);
514 kin.w2 = htonl(NFS_KERBTTL - 1);
515 memset((caddr_t)kivec, 0, sizeof (kivec));
516
517 /*
518 * Encrypt kin in CBC mode using the session
519 * key in kcr.
520 */
521 XXX
522
523 /*
524 * Finally, fill the timestamp verifier into the
525 * authenticator and verifier.
526 */
527 ktick.kind = htonl(RPCAKN_FULLNAME);
528 kverf.kind = htonl(RPCAKN_FULLNAME);
529 NFS_KERBW1(ktick.kt) = kout.w1;
530 ktick.kt.length = htonl(ktick.kt.length);
531 kverf.verf.t1 = kout.t1;
532 kverf.verf.t2 = kout.t2;
533 kverf.verf.w2 = kout.w2;
534 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
535 }
536 setreuid(0, 0);
537 #endif /* NFSKERB */
538 }
539 }
540 exit(0);
541 }
542
543 int
544 getnfsargs(spec, nfsargsp)
545 char *spec;
546 struct nfs_args *nfsargsp;
547 {
548 CLIENT *clp;
549 struct hostent *hp;
550 static struct sockaddr_in saddr;
551 #ifdef ISO
552 static struct sockaddr_iso isoaddr;
553 struct iso_addr *isop;
554 int isoflag = 0;
555 #endif
556 struct timeval pertry, try;
557 enum clnt_stat clnt_stat;
558 int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt;
559 char *hostp, *delimp;
560 #ifdef NFSKERB
561 char *cp;
562 #endif
563 u_short tport = 0;
564 static struct nfhret nfhret;
565 static char nam[MNAMELEN + 1];
566
567 strncpy(nam, spec, MNAMELEN);
568 nam[MNAMELEN] = '\0';
569 if ((delimp = strchr(spec, '@')) != NULL) {
570 hostp = delimp + 1;
571 } else if ((delimp = strchr(spec, ':')) != NULL) {
572 hostp = spec;
573 spec = delimp + 1;
574 } else {
575 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
576 return (0);
577 }
578 *delimp = '\0';
579 /*
580 * DUMB!! Until the mount protocol works on iso transport, we must
581 * supply both an iso and an inet address for the host.
582 */
583 #ifdef ISO
584 if (!strncmp(hostp, "iso=", 4)) {
585 u_short isoport;
586
587 hostp += 4;
588 isoflag++;
589 if ((delimp = strchr(hostp, '+')) == NULL) {
590 warnx("no iso+inet address");
591 return (0);
592 }
593 *delimp = '\0';
594 if ((isop = iso_addr(hostp)) == NULL) {
595 warnx("bad ISO address");
596 return (0);
597 }
598 memset(&isoaddr, 0, sizeof (isoaddr));
599 memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
600 isoaddr.siso_len = sizeof (isoaddr);
601 isoaddr.siso_family = AF_ISO;
602 isoaddr.siso_tlen = 2;
603 isoport = htons(NFS_PORT);
604 memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
605 hostp = delimp + 1;
606 }
607 #endif /* ISO */
608
609 /*
610 * Handle an internet host address and reverse resolve it if
611 * doing Kerberos.
612 */
613 if (inet_aton(hostp, &saddr.sin_addr) != 0) {
614 if ((nfsargsp->flags & NFSMNT_KERB)) {
615 if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
616 sizeof (u_long), AF_INET)) == (struct hostent *)0) {
617 warnx("can't reverse resolve net address");
618 return (0);
619 }
620 }
621 } else {
622 hp = gethostbyname(hostp);
623 if (hp == NULL) {
624 warnx("can't get net id for host");
625 return (0);
626 }
627 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
628 }
629 #ifdef NFSKERB
630 if (nfsargsp->flags & NFSMNT_KERB) {
631 strncpy(inst, hp->h_name, INST_SZ);
632 inst[INST_SZ - 1] = '\0';
633 if (cp = strchr(inst, '.'))
634 *cp = '\0';
635 }
636 #endif /* NFSKERB */
637
638 if (force2) {
639 nfsvers = NFS_VER2;
640 mntvers = RPCMNT_VER1;
641 } else {
642 nfsvers = NFS_VER3;
643 mntvers = RPCMNT_VER3;
644 }
645 orgcnt = retrycnt;
646 tryagain:
647 nfhret.stat = EACCES; /* Mark not yet successful */
648 while (retrycnt > 0) {
649 saddr.sin_family = AF_INET;
650 saddr.sin_port = htons(PMAPPORT);
651 if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
652 nfsvers, nfsproto )) == 0) {
653 if ((opflags & ISBGRND) == 0)
654 clnt_pcreateerror("NFS Portmap");
655 } else {
656 saddr.sin_port = 0;
657 pertry.tv_sec = 10;
658 pertry.tv_usec = 0;
659 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
660 clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
661 &so, 0, 0);
662 else
663 clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
664 pertry, &so);
665 if (clp == NULL) {
666 if ((opflags & ISBGRND) == 0)
667 clnt_pcreateerror("Cannot MNT RPC");
668 } else {
669 clp->cl_auth = authunix_create_default();
670 try.tv_sec = 10;
671 try.tv_usec = 0;
672 if (nfsargsp->flags & NFSMNT_KERB)
673 nfhret.auth = RPCAUTH_KERB4;
674 else
675 nfhret.auth = RPCAUTH_UNIX;
676 nfhret.vers = mntvers;
677 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
678 xdr_dir, spec, xdr_fh, &nfhret, try);
679 switch (clnt_stat) {
680 case RPC_PROGVERSMISMATCH:
681 if (nfsvers == NFS_VER3 && !force3) {
682 retrycnt = orgcnt;
683 nfsvers = NFS_VER2;
684 mntvers = RPCMNT_VER1;
685 nfsargsp->flags &=
686 ~NFSMNT_NFSV3;
687 goto tryagain;
688 } else {
689 errx(1, "%s", clnt_sperror(clp,
690 "MNT RPC"));
691 }
692 case RPC_SUCCESS:
693 auth_destroy(clp->cl_auth);
694 clnt_destroy(clp);
695 retrycnt = 0;
696 break;
697 default:
698 /* XXX should give up on some errors */
699 if ((opflags & ISBGRND) == 0)
700 warnx("%s", clnt_sperror(clp,
701 "bad MNT RPC"));
702 break;
703 }
704 }
705 }
706 if (--retrycnt > 0) {
707 if (opflags & BGRND) {
708 opflags &= ~BGRND;
709 if ((i = fork()) != 0) {
710 if (i == -1)
711 err(1, "nqnfs 2");
712 exit(0);
713 }
714 (void) setsid();
715 (void) close(STDIN_FILENO);
716 (void) close(STDOUT_FILENO);
717 (void) close(STDERR_FILENO);
718 (void) chdir("/");
719 opflags |= ISBGRND;
720 }
721 sleep(60);
722 }
723 }
724 if (nfhret.stat) {
725 if (opflags & ISBGRND)
726 exit(1);
727 errno = nfhret.stat;
728 warnx("can't access %s: %s", spec, strerror(nfhret.stat));
729 return (0);
730 }
731 saddr.sin_port = htons(tport);
732 #ifdef ISO
733 if (isoflag) {
734 nfsargsp->addr = (struct sockaddr *) &isoaddr;
735 nfsargsp->addrlen = sizeof (isoaddr);
736 } else
737 #endif /* ISO */
738 {
739 nfsargsp->addr = (struct sockaddr *) &saddr;
740 nfsargsp->addrlen = sizeof (saddr);
741 }
742 nfsargsp->fh = nfhret.nfh;
743 nfsargsp->fhsize = nfhret.fhsize;
744 nfsargsp->hostname = nam;
745 return (1);
746 }
747
748 /*
749 * xdr routines for mount rpc's
750 */
751 int
752 xdr_dir(xdrsp, dirp)
753 XDR *xdrsp;
754 char *dirp;
755 {
756 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
757 }
758
759 int
760 xdr_fh(xdrsp, np)
761 XDR *xdrsp;
762 struct nfhret *np;
763 {
764 int i;
765 long auth, authcnt, authfnd = 0;
766
767 if (!xdr_u_long(xdrsp, &np->stat))
768 return (0);
769 if (np->stat)
770 return (1);
771 switch (np->vers) {
772 case 1:
773 np->fhsize = NFSX_V2FH;
774 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
775 case 3:
776 if (!xdr_long(xdrsp, &np->fhsize))
777 return (0);
778 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
779 return (0);
780 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
781 return (0);
782 if (!xdr_long(xdrsp, &authcnt))
783 return (0);
784 for (i = 0; i < authcnt; i++) {
785 if (!xdr_long(xdrsp, &auth))
786 return (0);
787 if (auth == np->auth)
788 authfnd++;
789 }
790 /*
791 * Some servers, such as DEC's OSF/1 return a nil authenticator
792 * list to indicate RPCAUTH_UNIX.
793 */
794 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
795 np->stat = EAUTH;
796 return (1);
797 };
798 return (0);
799 }
800
801 void
802 usage()
803 {
804 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n",
805 "[-23bcCdiKklMPqsT] [-a maxreadahead] [-D deadthresh]",
806 "\t[-g maxgroups] [-L leaseterm] [-m realm] [-o options] [-R retrycnt]",
807 "\t[-r readsize] [-t timeout] [-w writesize] [-x retrans]",
808 "\trhost:path node");
809 exit(1);
810 }
811