mount_nfs.c revision 1.34 1 /* $NetBSD: mount_nfs.c,v 1.34 2002/09/21 18:43:36 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 #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.34 2002/09/21 18:43:36 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/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 #include <util.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 static const struct mntopt mopts[] = {
112 MOPT_STDOPTS,
113 MOPT_FORCE,
114 MOPT_UPDATE,
115 MOPT_GETARGS,
116 { "bg", 0, ALTF_BG, 1 },
117 { "conn", 0, ALTF_CONN, 1 },
118 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
119 { "intr", 0, ALTF_INTR, 1 },
120 #ifdef NFSKERB
121 { "kerb", 0, ALTF_KERB, 1 },
122 #endif
123 { "nfsv3", 0, ALTF_NFSV3, 1 },
124 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
125 { "mntudp", 0, ALTF_MNTUDP, 1 },
126 { "noresport", 0, ALTF_NORESPORT, 1 },
127 #ifdef ISO
128 { "seqpacket", 0, ALTF_SEQPACKET, 1 },
129 #endif
130 { "nqnfs", 0, ALTF_NQNFS, 1 },
131 { "soft", 0, ALTF_SOFT, 1 },
132 { "tcp", 0, ALTF_TCP, 1 },
133 { "nfsv2", 0, ALTF_NFSV2, 1 },
134 { NULL }
135 };
136
137 struct nfs_args nfsdefargs = {
138 NFS_ARGSVERSION,
139 (struct sockaddr *)0,
140 sizeof (struct sockaddr_in),
141 SOCK_DGRAM,
142 0,
143 (u_char *)0,
144 0,
145 NFSMNT_NFSV3|NFSMNT_NOCONN|NFSMNT_RESVPORT,
146 NFS_WSIZE,
147 NFS_RSIZE,
148 NFS_READDIRSIZE,
149 10,
150 NFS_RETRANS,
151 NFS_MAXGRPS,
152 NFS_DEFRAHEAD,
153 NQ_DEFLEASE,
154 NQ_DEADTHRESH,
155 (char *)0,
156 };
157
158 struct nfhret {
159 u_long stat;
160 long vers;
161 long auth;
162 long fhsize;
163 u_char nfh[NFSX_V3FHMAX];
164 };
165 #define DEF_RETRY 10000
166 #define BGRND 1
167 #define ISBGRND 2
168 int retrycnt;
169 int opflags = 0;
170 int nfsproto = IPPROTO_UDP;
171 int force2 = 0;
172 int force3 = 0;
173 int mnttcp_ok = 1;
174
175 #ifdef NFSKERB
176 static char inst[INST_SZ];
177 static char realm[REALM_SZ];
178 static struct {
179 u_long kind;
180 KTEXT_ST kt;
181 } ktick;
182 static struct nfsrpc_nickverf kverf;
183 static struct nfsrpc_fullblock kin, kout;
184 static NFSKERBKEY_T kivec;
185 static CREDENTIALS kcr;
186 static struct timeval ktv;
187 static NFSKERBKEYSCHED_T kerb_keysched;
188 #endif
189
190 static void shownfsargs __P((const struct nfs_args *));
191 static int getnfsargs __P((char *, struct nfs_args *));
192 #ifdef ISO
193 static struct iso_addr *iso_addr __P((const char *));
194 #endif
195 int main __P((int, char *[]));
196 int mount_nfs __P((int argc, char **argv));
197 /* void set_rpc_maxgrouplist __P((int)); */
198 static void usage __P((void));
199 static int xdr_dir __P((XDR *, char *));
200 static int xdr_fh __P((XDR *, struct nfhret *));
201
202 #ifndef MOUNT_NOMAIN
203 int
204 main(argc, argv)
205 int argc;
206 char **argv;
207 {
208 return mount_nfs(argc, argv);
209 }
210 #endif
211
212 int
213 mount_nfs(argc, argv)
214 int argc;
215 char *argv[];
216 {
217 int c, retval;
218 struct nfs_args *nfsargsp;
219 struct nfs_args nfsargs;
220 struct nfsd_cargs ncd;
221 int mntflags, altflags, i, nfssvc_flag, num;
222 char *name, *p, *spec, *ospec;
223 #ifdef NFSKERB
224 uid_t last_ruid;
225
226 last_ruid = -1;
227 if (krb_get_lrealm(realm, 0) != KSUCCESS)
228 (void)strcpy(realm, KRB_REALM);
229 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
230 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
231 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
232 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
233 fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
234
235 #endif
236 retrycnt = DEF_RETRY;
237
238 mntflags = 0;
239 altflags = 0;
240 nfsargs = nfsdefargs;
241 nfsargsp = &nfsargs;
242 while ((c = getopt(argc, argv,
243 "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
244 switch (c) {
245 case '3':
246 if (force2)
247 errx(1, "-2 and -3 are mutually exclusive");
248 force3 = 1;
249 break;
250 case '2':
251 if (force3)
252 errx(1, "-2 and -3 are mutually exclusive");
253 force2 = 1;
254 nfsargsp->flags &= ~NFSMNT_NFSV3;
255 break;
256 case 'a':
257 num = strtol(optarg, &p, 10);
258 if (*p || num < 0)
259 errx(1, "illegal -a value -- %s", optarg);
260 nfsargsp->readahead = num;
261 nfsargsp->flags |= NFSMNT_READAHEAD;
262 break;
263 case 'b':
264 opflags |= BGRND;
265 break;
266 case 'c':
267 nfsargsp->flags |= NFSMNT_NOCONN;
268 break;
269 case 'C':
270 nfsargsp->flags &= ~NFSMNT_NOCONN;
271 break;
272 case 'D':
273 num = strtol(optarg, &p, 10);
274 if (*p || num <= 0)
275 errx(1, "illegal -D value -- %s", optarg);
276 nfsargsp->deadthresh = num;
277 nfsargsp->flags |= NFSMNT_DEADTHRESH;
278 break;
279 case 'd':
280 nfsargsp->flags |= NFSMNT_DUMBTIMR;
281 break;
282 #if 0 /* XXXX */
283 case 'g':
284 num = strtol(optarg, &p, 10);
285 if (*p || num <= 0)
286 errx(1, "illegal -g value -- %s", optarg);
287 set_rpc_maxgrouplist(num);
288 nfsargsp->maxgrouplist = num;
289 nfsargsp->flags |= NFSMNT_MAXGRPS;
290 break;
291 #endif
292 case 'I':
293 num = strtol(optarg, &p, 10);
294 if (*p || num <= 0)
295 errx(1, "illegal -I value -- %s", optarg);
296 nfsargsp->readdirsize = num;
297 nfsargsp->flags |= NFSMNT_READDIRSIZE;
298 break;
299 case 'i':
300 nfsargsp->flags |= NFSMNT_INT;
301 break;
302 #ifdef NFSKERB
303 case 'K':
304 nfsargsp->flags |= NFSMNT_KERB;
305 break;
306 #endif
307 case 'L':
308 num = strtol(optarg, &p, 10);
309 if (*p || num < 2)
310 errx(1, "illegal -L value -- %s", optarg);
311 nfsargsp->leaseterm = num;
312 nfsargsp->flags |= NFSMNT_LEASETERM;
313 break;
314 case 'l':
315 nfsargsp->flags |= NFSMNT_RDIRPLUS;
316 break;
317 #ifdef NFSKERB
318 case 'm':
319 (void)strncpy(realm, optarg, REALM_SZ - 1);
320 realm[REALM_SZ - 1] = '\0';
321 break;
322 #endif
323 case 'o':
324 getmntopts(optarg, mopts, &mntflags, &altflags);
325 if(altflags & ALTF_BG)
326 opflags |= BGRND;
327 if(altflags & ALTF_CONN)
328 nfsargsp->flags &= ~NFSMNT_NOCONN;
329 if(altflags & ALTF_DUMBTIMR)
330 nfsargsp->flags |= NFSMNT_DUMBTIMR;
331 if(altflags & ALTF_INTR)
332 nfsargsp->flags |= NFSMNT_INT;
333 #ifdef NFSKERB
334 if(altflags & ALTF_KERB)
335 nfsargsp->flags |= NFSMNT_KERB;
336 #endif
337 if(altflags & ALTF_NFSV3) {
338 if (force2)
339 errx(1,"conflicting version options");
340 force3 = 1;
341 }
342 if(altflags & ALTF_NFSV2) {
343 if (force3)
344 errx(1,"conflicting version options");
345 force2 = 1;
346 nfsargsp->flags &= ~NFSMNT_NFSV3;
347 }
348 if(altflags & ALTF_RDIRPLUS)
349 nfsargsp->flags |= NFSMNT_RDIRPLUS;
350 if(altflags & ALTF_MNTUDP)
351 mnttcp_ok = 0;
352 if(altflags & ALTF_NORESPORT)
353 nfsargsp->flags &= ~NFSMNT_RESVPORT;
354 #ifdef ISO
355 if(altflags & ALTF_SEQPACKET)
356 nfsargsp->sotype = SOCK_SEQPACKET;
357 #endif
358 if(altflags & ALTF_NQNFS) {
359 if (force2)
360 errx(1,"nqnfs only available with v3");
361 force3 = 1;
362 nfsargsp->flags |= NFSMNT_NQNFS;
363 }
364 if(altflags & ALTF_SOFT)
365 nfsargsp->flags |= NFSMNT_SOFT;
366 if(altflags & ALTF_TCP) {
367 nfsargsp->sotype = SOCK_STREAM;
368 nfsproto = IPPROTO_TCP;
369 }
370 altflags = 0;
371 break;
372 case 'P':
373 nfsargsp->flags |= NFSMNT_RESVPORT;
374 break;
375 case 'p':
376 nfsargsp->flags &= ~NFSMNT_RESVPORT;
377 break;
378 case 'q':
379 if (force2)
380 errx(1,"nqnfs only available with v3");
381 force3 = 1;
382 nfsargsp->flags |= NFSMNT_NQNFS;
383 break;
384 case 'R':
385 num = strtol(optarg, &p, 10);
386 if (*p || num <= 0)
387 errx(1, "illegal -R value -- %s", optarg);
388 retrycnt = num;
389 break;
390 case 'r':
391 num = strtol(optarg, &p, 10);
392 if (*p || num <= 0)
393 errx(1, "illegal -r value -- %s", optarg);
394 nfsargsp->rsize = num;
395 nfsargsp->flags |= NFSMNT_RSIZE;
396 break;
397 #ifdef ISO
398 case 'S':
399 nfsargsp->sotype = SOCK_SEQPACKET;
400 break;
401 #endif
402 case 's':
403 nfsargsp->flags |= NFSMNT_SOFT;
404 break;
405 case 'T':
406 nfsargsp->sotype = SOCK_STREAM;
407 nfsproto = IPPROTO_TCP;
408 break;
409 case 't':
410 num = strtol(optarg, &p, 10);
411 if (*p || num <= 0)
412 errx(1, "illegal -t value -- %s", optarg);
413 nfsargsp->timeo = num;
414 nfsargsp->flags |= NFSMNT_TIMEO;
415 break;
416 case 'w':
417 num = strtol(optarg, &p, 10);
418 if (*p || num <= 0)
419 errx(1, "illegal -w value -- %s", optarg);
420 nfsargsp->wsize = num;
421 nfsargsp->flags |= NFSMNT_WSIZE;
422 break;
423 case 'x':
424 num = strtol(optarg, &p, 10);
425 if (*p || num <= 0)
426 errx(1, "illegal -x value -- %s", optarg);
427 nfsargsp->retrans = num;
428 nfsargsp->flags |= NFSMNT_RETRANS;
429 break;
430 case 'X':
431 nfsargsp->flags |= NFSMNT_XLATECOOKIE;
432 break;
433 case 'U':
434 mnttcp_ok = 0;
435 break;
436 default:
437 usage();
438 break;
439 }
440 argc -= optind;
441 argv += optind;
442
443 if (argc != 2)
444 usage();
445
446 spec = *argv++;
447 name = *argv;
448 if((ospec = strdup(spec))==NULL) {
449 err(1,"strdup");
450 }
451
452 if ((mntflags & MNT_GETARGS) == 0) {
453 if (!getnfsargs(spec, nfsargsp))
454 exit(1);
455 }
456 if ((retval = mount(MOUNT_NFS, name, mntflags, nfsargsp))) {
457 /* Did we just default to v3 on a v2-only kernel?
458 * If so, default to v2 & try again */
459 if ((errno == EPROGMISMATCH) && !force3) {
460 nfsargsp->flags &= ~NFSMNT_NFSV3;
461 retval = mount(MOUNT_NFS, name, mntflags, nfsargsp);
462 }
463 }
464 if (retval)
465 err(1, "%s on %s", ospec, name);
466 if (mntflags & MNT_GETARGS) {
467 shownfsargs(nfsargsp);
468 return 0;
469 }
470
471 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
472 if ((opflags & ISBGRND) == 0) {
473 if ((i = fork()) != 0) {
474 if (i == -1)
475 err(1, "nqnfs 1");
476 exit(0);
477 }
478 (void) setsid();
479 (void) close(STDIN_FILENO);
480 (void) close(STDOUT_FILENO);
481 (void) close(STDERR_FILENO);
482 (void) chdir("/");
483 }
484 openlog("mount_nfs", LOG_PID, LOG_DAEMON);
485 nfssvc_flag = NFSSVC_MNTD;
486 ncd.ncd_dirp = name;
487 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
488 if (errno != ENEEDAUTH) {
489 syslog(LOG_ERR, "nfssvc err %m");
490 continue;
491 }
492 nfssvc_flag =
493 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
494 #ifdef NFSKERB
495 /*
496 * Set up as ncd_authuid for the kerberos call.
497 * Must set ruid to ncd_authuid and reset the
498 * ticket name iff ncd_authuid is not the same
499 * as last time, so that the right ticket file
500 * is found.
501 * Get the Kerberos credential structure so that
502 * we have the seesion key and get a ticket for
503 * this uid.
504 * For more info see the IETF Draft "Authentication
505 * in ONC RPC".
506 */
507 if (ncd.ncd_authuid != last_ruid) {
508 krb_set_tkt_string("");
509 last_ruid = ncd.ncd_authuid;
510 }
511 setreuid(ncd.ncd_authuid, 0);
512 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
513 if (kret == RET_NOTKT) {
514 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
515 DEFAULT_TKT_LIFE);
516 if (kret == KSUCCESS)
517 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
518 &kcr);
519 }
520 if (kret == KSUCCESS)
521 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
522 realm, 0);
523
524 /*
525 * Fill in the AKN_FULLNAME authenticator and verfier.
526 * Along with the Kerberos ticket, we need to build
527 * the timestamp verifier and encrypt it in CBC mode.
528 */
529 if (kret == KSUCCESS &&
530 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
531 && gettimeofday(&ktv, (struct timezone *)0) == 0) {
532 ncd.ncd_authtype = RPCAUTH_KERB4;
533 ncd.ncd_authstr = (u_char *)&ktick;
534 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
535 3 * NFSX_UNSIGNED;
536 ncd.ncd_verfstr = (u_char *)&kverf;
537 ncd.ncd_verflen = sizeof (kverf);
538 memmove(ncd.ncd_key, kcr.session,
539 sizeof (kcr.session));
540 kin.t1 = htonl(ktv.tv_sec);
541 kin.t2 = htonl(ktv.tv_usec);
542 kin.w1 = htonl(NFS_KERBTTL);
543 kin.w2 = htonl(NFS_KERBTTL - 1);
544 memset((caddr_t)kivec, 0, sizeof (kivec));
545
546 /*
547 * Encrypt kin in CBC mode using the session
548 * key in kcr.
549 */
550 XXX
551
552 /*
553 * Finally, fill the timestamp verifier into the
554 * authenticator and verifier.
555 */
556 ktick.kind = htonl(RPCAKN_FULLNAME);
557 kverf.kind = htonl(RPCAKN_FULLNAME);
558 NFS_KERBW1(ktick.kt) = kout.w1;
559 ktick.kt.length = htonl(ktick.kt.length);
560 kverf.verf.t1 = kout.t1;
561 kverf.verf.t2 = kout.t2;
562 kverf.verf.w2 = kout.w2;
563 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
564 }
565 setreuid(0, 0);
566 #endif /* NFSKERB */
567 }
568 }
569 exit(0);
570 }
571
572 static void
573 shownfsargs(nfsargsp)
574 const struct nfs_args *nfsargsp;
575 {
576 char fbuf[2048];
577
578 (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
579 printf("version=%d, addrlen=%d, sotype=%d, proto=%d, fhsize=%d, "
580 "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
581 "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
582 "deadthresh=%d\n",
583 nfsargsp->version,
584 nfsargsp->addrlen,
585 nfsargsp->sotype,
586 nfsargsp->proto,
587 nfsargsp->fhsize,
588 fbuf,
589 nfsargsp->wsize,
590 nfsargsp->rsize,
591 nfsargsp->readdirsize,
592 nfsargsp->timeo,
593 nfsargsp->retrans,
594 nfsargsp->maxgrouplist,
595 nfsargsp->readahead,
596 nfsargsp->leaseterm,
597 nfsargsp->deadthresh);
598 }
599
600 static int
601 getnfsargs(spec, nfsargsp)
602 char *spec;
603 struct nfs_args *nfsargsp;
604 {
605 CLIENT *clp;
606 struct addrinfo hints, *ai_nfs, *ai;
607 int ecode;
608 char host[NI_MAXHOST], serv[NI_MAXSERV];
609 static struct netbuf nfs_nb;
610 static struct sockaddr_storage nfs_ss;
611 struct netconfig *nconf;
612 char *netid;
613 #ifdef ISO
614 static struct sockaddr_iso isoaddr;
615 struct iso_addr *isop;
616 int isoflag = 0;
617 #endif
618 struct timeval pertry, try;
619 enum clnt_stat clnt_stat;
620 int i, nfsvers, mntvers, orgcnt;
621 char *hostp, *delimp;
622 #ifdef NFSKERB
623 char *cp;
624 #endif
625 static struct nfhret nfhret;
626 static char nam[MNAMELEN + 1];
627
628 strncpy(nam, spec, MNAMELEN);
629 nam[MNAMELEN] = '\0';
630 if ((delimp = strchr(spec, '@')) != NULL) {
631 hostp = delimp + 1;
632 } else if ((delimp = strrchr(spec, ':')) != NULL) {
633 hostp = spec;
634 spec = delimp + 1;
635 } else {
636 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
637 return (0);
638 }
639 *delimp = '\0';
640 /*
641 * DUMB!! Until the mount protocol works on iso transport, we must
642 * supply both an iso and an inet address for the host.
643 */
644 #ifdef ISO
645 if (!strncmp(hostp, "iso=", 4)) {
646 u_short isoport;
647
648 hostp += 4;
649 isoflag++;
650 if ((delimp = strchr(hostp, '+')) == NULL) {
651 warnx("no iso+inet address");
652 return (0);
653 }
654 *delimp = '\0';
655 if ((isop = iso_addr(hostp)) == NULL) {
656 warnx("bad ISO address");
657 return (0);
658 }
659 memset(&isoaddr, 0, sizeof (isoaddr));
660 memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
661 isoaddr.siso_len = sizeof (isoaddr);
662 isoaddr.siso_family = AF_ISO;
663 isoaddr.siso_tlen = 2;
664 isoport = htons(NFS_PORT);
665 memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
666 hostp = delimp + 1;
667 }
668 #endif /* ISO */
669
670 /*
671 * Handle an internet host address and reverse resolve it if
672 * doing Kerberos.
673 */
674 memset(&hints, 0, sizeof hints);
675 hints.ai_flags = AI_NUMERICHOST;
676 hints.ai_socktype = nfsargsp->sotype;
677 if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) == 0) {
678 if ((nfsargsp->flags & NFSMNT_KERB)) {
679 hints.ai_flags = 0;
680 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
681 sizeof host, serv, sizeof serv, 0) != 0) {
682 warnx("can't reverse resolve net address for "
683 "host \"%s\": %s", hostp,
684 gai_strerror(ecode));
685 return (0);
686 }
687 hostp = host;
688 }
689 } else {
690 hints.ai_flags = 0;
691 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
692 warnx("can't get net id for host \"%s\": %s", hostp,
693 gai_strerror(ecode));
694 return (0);
695 }
696 }
697 #ifdef NFSKERB
698 if (nfsargsp->flags & NFSMNT_KERB) {
699 strncpy(inst, hp->h_name, INST_SZ);
700 inst[INST_SZ - 1] = '\0';
701 if (cp = strchr(inst, '.'))
702 *cp = '\0';
703 }
704 #endif /* NFSKERB */
705
706 if (force2) {
707 nfsvers = NFS_VER2;
708 mntvers = RPCMNT_VER1;
709 } else {
710 nfsvers = NFS_VER3;
711 mntvers = RPCMNT_VER3;
712 }
713 orgcnt = retrycnt;
714 nfhret.stat = EACCES; /* Mark not yet successful */
715
716 ai = ai_nfs;
717 while (ai != NULL) {
718 /*
719 * XXX. Nead a generic (family, type, proto) -> nconf interface.
720 * __rpc_*2nconf exist, maybe they should be exported.
721 */
722 if (nfsargsp->sotype == SOCK_STREAM) {
723 if (ai->ai_family == AF_INET6)
724 netid = "tcp6";
725 else
726 netid = "tcp";
727 } else {
728 if (ai->ai_family == AF_INET6)
729 netid = "udp6";
730 else
731 netid = "udp";
732 }
733
734 nconf = getnetconfigent(netid);
735
736 tryagain:
737 retrycnt = orgcnt;
738
739 while (retrycnt > 0) {
740 nfs_nb.buf = &nfs_ss;
741 nfs_nb.maxlen = sizeof nfs_ss;
742 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
743 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
744 nfhret.stat = rpc_createerr.cf_error.re_errno;
745 break;
746 }
747 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
748 nfhret.stat = EPROTONOSUPPORT;
749 break;
750 }
751 if ((opflags & ISBGRND) == 0)
752 clnt_pcreateerror(
753 "mount_nfs: rpcbind to nfs on server");
754 } else {
755 pertry.tv_sec = 10;
756 pertry.tv_usec = 0;
757 /*
758 * XXX relies on clnt_tcp_create to bind to a reserved
759 * socket.
760 */
761 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
762 mnttcp_ok ? nconf : getnetconfigent("udp"));
763 if (clp == NULL) {
764 if ((opflags & ISBGRND) == 0) {
765 clnt_pcreateerror(
766 "Cannot MNT RPC (mountd)");
767 }
768 } else {
769 CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
770 (char *)&pertry);
771 clp->cl_auth = authsys_create_default();
772 try.tv_sec = 10;
773 try.tv_usec = 0;
774 if (nfsargsp->flags & NFSMNT_KERB)
775 nfhret.auth = RPCAUTH_KERB4;
776 else
777 nfhret.auth = RPCAUTH_UNIX;
778 nfhret.vers = mntvers;
779 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
780 xdr_dir, spec, xdr_fh, &nfhret, try);
781 switch (clnt_stat) {
782 case RPC_PROGVERSMISMATCH:
783 if (nfsvers == NFS_VER3 && !force3) {
784 nfsvers = NFS_VER2;
785 mntvers = RPCMNT_VER1;
786 nfsargsp->flags &=
787 ~NFSMNT_NFSV3;
788 goto tryagain;
789 } else {
790 errx(1, "%s", clnt_sperror(clp,
791 "MNT RPC"));
792 }
793 case RPC_SUCCESS:
794 auth_destroy(clp->cl_auth);
795 clnt_destroy(clp);
796 retrycnt = 0;
797 break;
798 default:
799 /* XXX should give up on some errors */
800 if ((opflags & ISBGRND) == 0)
801 warnx("%s", clnt_sperror(clp,
802 "bad MNT RPC"));
803 break;
804 }
805 }
806 }
807 if (--retrycnt > 0) {
808 if (opflags & BGRND) {
809 opflags &= ~BGRND;
810 if ((i = fork()) != 0) {
811 if (i == -1)
812 err(1, "nqnfs 2");
813 exit(0);
814 }
815 (void) setsid();
816 (void) close(STDIN_FILENO);
817 (void) close(STDOUT_FILENO);
818 (void) close(STDERR_FILENO);
819 (void) chdir("/");
820 opflags |= ISBGRND;
821 }
822 sleep(60);
823 }
824 }
825 if (nfhret.stat == 0)
826 break;
827 ai = ai->ai_next;
828 }
829 freeaddrinfo(ai_nfs);
830 if (nfhret.stat) {
831 if (opflags & ISBGRND)
832 exit(1);
833 errno = nfhret.stat;
834 warnx("can't access %s: %s", spec, strerror(nfhret.stat));
835 return (0);
836 }
837 #ifdef ISO
838 if (isoflag) {
839 nfsargsp->addr = (struct sockaddr *) &isoaddr;
840 nfsargsp->addrlen = sizeof (isoaddr);
841 } else
842 #endif /* ISO */
843 {
844 nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
845 nfsargsp->addrlen = nfs_nb.len;
846 }
847 nfsargsp->fh = nfhret.nfh;
848 nfsargsp->fhsize = nfhret.fhsize;
849 nfsargsp->hostname = nam;
850 return (1);
851 }
852
853 /*
854 * xdr routines for mount rpc's
855 */
856 static int
857 xdr_dir(xdrsp, dirp)
858 XDR *xdrsp;
859 char *dirp;
860 {
861 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
862 }
863
864 static int
865 xdr_fh(xdrsp, np)
866 XDR *xdrsp;
867 struct nfhret *np;
868 {
869 int i;
870 long auth, authcnt, authfnd = 0;
871
872 if (!xdr_u_long(xdrsp, &np->stat))
873 return (0);
874 if (np->stat)
875 return (1);
876 switch (np->vers) {
877 case 1:
878 np->fhsize = NFSX_V2FH;
879 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
880 case 3:
881 if (!xdr_long(xdrsp, &np->fhsize))
882 return (0);
883 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
884 return (0);
885 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
886 return (0);
887 if (!xdr_long(xdrsp, &authcnt))
888 return (0);
889 for (i = 0; i < authcnt; i++) {
890 if (!xdr_long(xdrsp, &auth))
891 return (0);
892 if (auth == np->auth)
893 authfnd++;
894 }
895 /*
896 * Some servers, such as DEC's OSF/1 return a nil authenticator
897 * list to indicate RPCAUTH_UNIX.
898 */
899 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
900 np->stat = EAUTH;
901 return (1);
902 };
903 return (0);
904 }
905
906 static void
907 usage()
908 {
909 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
910 "[-23bcCdiKlpPqsTUX] [-a maxreadahead] [-D deadthresh]",
911 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm] [-m realm]",
912 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
913 "\t[-w writesize] [-x retrans]",
914 "\trhost:path node");
915 exit(1);
916 }
917