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