mount_nfs.c revision 1.27 1 /* $NetBSD: mount_nfs.c,v 1.27 2000/06/26 21:53:34 bjh21 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.27 2000/06/26 21:53:34 bjh21 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 force2 = 0;
170 int force3 = 0;
171 int mnttcp_ok = 1;
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 addrinfo hints, *ai_nfs, *ai;
550 int ecode;
551 char host[NI_MAXHOST], serv[NI_MAXSERV];
552 static struct netbuf nfs_nb;
553 static struct sockaddr_storage nfs_ss;
554 struct netconfig *nconf;
555 char *netid;
556 #ifdef ISO
557 static struct sockaddr_iso isoaddr;
558 struct iso_addr *isop;
559 int isoflag = 0;
560 #endif
561 struct timeval pertry, try;
562 enum clnt_stat clnt_stat;
563 int i, nfsvers, mntvers, orgcnt;
564 char *hostp, *delimp;
565 #ifdef NFSKERB
566 char *cp;
567 #endif
568 static struct nfhret nfhret;
569 static char nam[MNAMELEN + 1];
570
571 strncpy(nam, spec, MNAMELEN);
572 nam[MNAMELEN] = '\0';
573 if ((delimp = strchr(spec, '@')) != NULL) {
574 hostp = delimp + 1;
575 } else if ((delimp = strrchr(spec, ':')) != NULL) {
576 hostp = spec;
577 spec = delimp + 1;
578 } else {
579 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
580 return (0);
581 }
582 *delimp = '\0';
583 /*
584 * DUMB!! Until the mount protocol works on iso transport, we must
585 * supply both an iso and an inet address for the host.
586 */
587 #ifdef ISO
588 if (!strncmp(hostp, "iso=", 4)) {
589 u_short isoport;
590
591 hostp += 4;
592 isoflag++;
593 if ((delimp = strchr(hostp, '+')) == NULL) {
594 warnx("no iso+inet address");
595 return (0);
596 }
597 *delimp = '\0';
598 if ((isop = iso_addr(hostp)) == NULL) {
599 warnx("bad ISO address");
600 return (0);
601 }
602 memset(&isoaddr, 0, sizeof (isoaddr));
603 memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
604 isoaddr.siso_len = sizeof (isoaddr);
605 isoaddr.siso_family = AF_ISO;
606 isoaddr.siso_tlen = 2;
607 isoport = htons(NFS_PORT);
608 memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
609 hostp = delimp + 1;
610 }
611 #endif /* ISO */
612
613 /*
614 * Handle an internet host address and reverse resolve it if
615 * doing Kerberos.
616 */
617 memset(&hints, 0, sizeof hints);
618 hints.ai_flags = AI_NUMERICHOST;
619 hints.ai_socktype = nfsargsp->sotype;
620 if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) == 0) {
621 if ((nfsargsp->flags & NFSMNT_KERB)) {
622 hints.ai_flags = 0;
623 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
624 sizeof host, serv, sizeof serv, 0) != 0) {
625 warnx("can't reverse resolve net address");
626 return (0);
627 }
628 hostp = host;
629 }
630 } else {
631 hints.ai_flags = 0;
632 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
633 warnx("can't get net id for host/nfs: %s",
634 gai_strerror(ecode));
635 return (0);
636 }
637 }
638 #ifdef NFSKERB
639 if (nfsargsp->flags & NFSMNT_KERB) {
640 strncpy(inst, hp->h_name, INST_SZ);
641 inst[INST_SZ - 1] = '\0';
642 if (cp = strchr(inst, '.'))
643 *cp = '\0';
644 }
645 #endif /* NFSKERB */
646
647 if (force2) {
648 nfsvers = NFS_VER2;
649 mntvers = RPCMNT_VER1;
650 } else {
651 nfsvers = NFS_VER3;
652 mntvers = RPCMNT_VER3;
653 }
654 orgcnt = retrycnt;
655 nfhret.stat = EACCES; /* Mark not yet successful */
656
657 ai = ai_nfs;
658 while (ai != NULL) {
659 /*
660 * XXX. Nead a generic (family, type, proto) -> nconf interface.
661 * __rpc_*2nconf exist, maybe they should be exported.
662 */
663 if (nfsargsp->sotype == SOCK_STREAM) {
664 if (ai->ai_family == AF_INET6)
665 netid = "tcp6";
666 else
667 netid = "tcp";
668 } else {
669 if (ai->ai_family == AF_INET6)
670 netid = "udp6";
671 else
672 netid = "udp";
673 }
674
675 nconf = getnetconfigent(netid);
676
677 tryagain:
678 retrycnt = orgcnt;
679
680 while (retrycnt > 0) {
681 nfs_nb.buf = &nfs_ss;
682 nfs_nb.maxlen = sizeof nfs_ss;
683 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
684 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
685 nfhret.stat = rpc_createerr.cf_error.re_errno;
686 break;
687 }
688 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
689 nfhret.stat = EPROTONOSUPPORT;
690 break;
691 }
692 if ((opflags & ISBGRND) == 0)
693 clnt_pcreateerror(
694 "mount_nfs: rpcbind on server");
695 } else {
696 pertry.tv_sec = 10;
697 pertry.tv_usec = 0;
698 /*
699 * XXX relies on clnt_tcp_create to bind to a reserved
700 * socket.
701 */
702 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
703 mnttcp_ok ? nconf : getnetconfigent("udp"));
704 if (clp == NULL) {
705 if ((opflags & ISBGRND) == 0)
706 clnt_pcreateerror("Cannot MNT RPC");
707 } else {
708 CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
709 (char *)&pertry);
710 clp->cl_auth = authsys_create_default();
711 try.tv_sec = 10;
712 try.tv_usec = 0;
713 if (nfsargsp->flags & NFSMNT_KERB)
714 nfhret.auth = RPCAUTH_KERB4;
715 else
716 nfhret.auth = RPCAUTH_UNIX;
717 nfhret.vers = mntvers;
718 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
719 xdr_dir, spec, xdr_fh, &nfhret, try);
720 switch (clnt_stat) {
721 case RPC_PROGVERSMISMATCH:
722 if (nfsvers == NFS_VER3 && !force3) {
723 nfsvers = NFS_VER2;
724 mntvers = RPCMNT_VER1;
725 nfsargsp->flags &=
726 ~NFSMNT_NFSV3;
727 goto tryagain;
728 } else {
729 errx(1, "%s", clnt_sperror(clp,
730 "MNT RPC"));
731 }
732 case RPC_SUCCESS:
733 auth_destroy(clp->cl_auth);
734 clnt_destroy(clp);
735 retrycnt = 0;
736 break;
737 default:
738 /* XXX should give up on some errors */
739 if ((opflags & ISBGRND) == 0)
740 warnx("%s", clnt_sperror(clp,
741 "bad MNT RPC"));
742 break;
743 }
744 }
745 }
746 if (--retrycnt > 0) {
747 if (opflags & BGRND) {
748 opflags &= ~BGRND;
749 if ((i = fork()) != 0) {
750 if (i == -1)
751 err(1, "nqnfs 2");
752 exit(0);
753 }
754 (void) setsid();
755 (void) close(STDIN_FILENO);
756 (void) close(STDOUT_FILENO);
757 (void) close(STDERR_FILENO);
758 (void) chdir("/");
759 opflags |= ISBGRND;
760 }
761 sleep(60);
762 }
763 }
764 if (nfhret.stat == 0)
765 break;
766 ai = ai->ai_next;
767 }
768 freeaddrinfo(ai_nfs);
769 if (nfhret.stat) {
770 if (opflags & ISBGRND)
771 exit(1);
772 errno = nfhret.stat;
773 warnx("can't access %s: %s", spec, strerror(nfhret.stat));
774 return (0);
775 }
776 #ifdef ISO
777 if (isoflag) {
778 nfsargsp->addr = (struct sockaddr *) &isoaddr;
779 nfsargsp->addrlen = sizeof (isoaddr);
780 } else
781 #endif /* ISO */
782 {
783 nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
784 nfsargsp->addrlen = nfs_nb.len;
785 }
786 nfsargsp->fh = nfhret.nfh;
787 nfsargsp->fhsize = nfhret.fhsize;
788 nfsargsp->hostname = nam;
789 return (1);
790 }
791
792 /*
793 * xdr routines for mount rpc's
794 */
795 int
796 xdr_dir(xdrsp, dirp)
797 XDR *xdrsp;
798 char *dirp;
799 {
800 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
801 }
802
803 int
804 xdr_fh(xdrsp, np)
805 XDR *xdrsp;
806 struct nfhret *np;
807 {
808 int i;
809 long auth, authcnt, authfnd = 0;
810
811 if (!xdr_u_long(xdrsp, &np->stat))
812 return (0);
813 if (np->stat)
814 return (1);
815 switch (np->vers) {
816 case 1:
817 np->fhsize = NFSX_V2FH;
818 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
819 case 3:
820 if (!xdr_long(xdrsp, &np->fhsize))
821 return (0);
822 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
823 return (0);
824 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
825 return (0);
826 if (!xdr_long(xdrsp, &authcnt))
827 return (0);
828 for (i = 0; i < authcnt; i++) {
829 if (!xdr_long(xdrsp, &auth))
830 return (0);
831 if (auth == np->auth)
832 authfnd++;
833 }
834 /*
835 * Some servers, such as DEC's OSF/1 return a nil authenticator
836 * list to indicate RPCAUTH_UNIX.
837 */
838 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
839 np->stat = EAUTH;
840 return (1);
841 };
842 return (0);
843 }
844
845 void
846 usage()
847 {
848 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
849 "[-23bcCdiKlpPqsTUX] [-a maxreadahead] [-D deadthresh]",
850 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm] [-m realm]",
851 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
852 "\t[-w writesize] [-x retrans]",
853 "\trhost:path node");
854 exit(1);
855 }
856