mount_nfs.c revision 1.46 1 /* $NetBSD: mount_nfs.c,v 1.46 2005/01/31 05:19:19 erh 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
44 #else
45 __RCSID("$NetBSD: mount_nfs.c,v 1.46 2005/01/31 05:19:19 erh Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <syslog.h>
54
55 #include <rpc/rpc.h>
56 #include <rpc/pmap_clnt.h>
57 #include <rpc/pmap_prot.h>
58
59 #ifdef ISO
60 #include <netiso/iso.h>
61 #endif
62
63 #ifdef NFSKERB
64 #include <des.h>
65 #include <kerberosIV/krb.h>
66 #endif
67
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfs/nfs.h>
71 #include <nfs/nqnfs.h>
72 #include <nfs/nfsmount.h>
73
74 #include <arpa/inet.h>
75
76 #include <ctype.h>
77 #include <err.h>
78 #include <errno.h>
79 #include <fcntl.h>
80 #include <netdb.h>
81 #include <signal.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <unistd.h>
86 #include <util.h>
87
88 #include <mntopts.h>
89
90 #define ALTF_BG 0x00000001
91 #define ALTF_CONN 0x00000002
92 #define ALTF_DUMBTIMR 0x00000004
93 #define ALTF_INTR 0x00000008
94 #define ALTF_KERB 0x00000010
95 #define ALTF_NFSV3 0x00000020
96 #define ALTF_RDIRPLUS 0x00000040
97 #define ALTF_MNTUDP 0x00000080
98 #define ALTF_NORESPORT 0x00000100
99 #define ALTF_SEQPACKET 0x00000200
100 #define ALTF_NQNFS 0x00000400
101 #define ALTF_SOFT 0x00000800
102 #define ALTF_TCP 0x00001000
103 #define ALTF_NFSV2 0x00002000
104 #define ALTF_PORT 0x00004000
105 #define ALTF_RSIZE 0x00008000
106 #define ALTF_WSIZE 0x00010000
107 #define ALTF_RDIRSIZE 0x00020000
108 #define ALTF_MAXGRPS 0x00040000
109 #define ALTF_LEASETERM 0x00080000
110 #define ALTF_READAHEAD 0x00100000
111 #define ALTF_DEADTHRESH 0x00200000
112 #define ALTF_TIMEO 0x00400000
113 #define ALTF_RETRANS 0x00800000
114
115 static const struct mntopt mopts[] = {
116 MOPT_STDOPTS,
117 MOPT_FORCE,
118 MOPT_UPDATE,
119 MOPT_GETARGS,
120 { "bg", 0, ALTF_BG, 1 },
121 { "conn", 0, ALTF_CONN, 1 },
122 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
123 { "intr", 0, ALTF_INTR, 1 },
124 #ifdef NFSKERB
125 { "kerb", 0, ALTF_KERB, 1 },
126 #endif
127 { "nfsv3", 0, ALTF_NFSV3, 1 },
128 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
129 { "mntudp", 0, ALTF_MNTUDP, 1 },
130 { "noresport", 0, ALTF_NORESPORT, 1 },
131 #ifdef ISO
132 { "seqpacket", 0, ALTF_SEQPACKET, 1 },
133 #endif
134 { "nqnfs", 0, ALTF_NQNFS, 1 },
135 { "soft", 0, ALTF_SOFT, 1 },
136 { "tcp", 0, ALTF_TCP, 1 },
137 { "nfsv2", 0, ALTF_NFSV2, 1 },
138 { "port", 0, ALTF_PORT, 1 },
139 { "rsize", 0, ALTF_RSIZE, 1 },
140 { "wsize", 0, ALTF_WSIZE, 1 },
141 { "rdirsize", 0, ALTF_RDIRSIZE, 1 },
142 { "maxgrps", 0, ALTF_MAXGRPS, 1 },
143 { "leaseterm", 0, ALTF_LEASETERM, 1 },
144 { "readahead", 0, ALTF_READAHEAD, 1 },
145 { "deadthresh", 0, ALTF_DEADTHRESH, 1 },
146 { "timeo", 0, ALTF_TIMEO, 1 },
147 { NULL }
148
149 };
150
151 struct nfs_args nfsdefargs = {
152 NFS_ARGSVERSION,
153 (struct sockaddr *)0,
154 sizeof (struct sockaddr_in),
155 SOCK_DGRAM,
156 0,
157 (u_char *)0,
158 0,
159 NFSMNT_NFSV3|NFSMNT_NOCONN|NFSMNT_RESVPORT,
160 NFS_WSIZE,
161 NFS_RSIZE,
162 NFS_READDIRSIZE,
163 10,
164 NFS_RETRANS,
165 NFS_MAXGRPS,
166 NFS_DEFRAHEAD,
167 NQ_DEFLEASE,
168 NQ_DEADTHRESH,
169 (char *)0,
170 };
171
172 struct nfhret {
173 u_long stat;
174 long vers;
175 long auth;
176 long fhsize;
177 u_char nfh[NFSX_V3FHMAX];
178 };
179 #define DEF_RETRY 10000
180 #define BGRND 1
181 #define ISBGRND 2
182 int retrycnt;
183 int opflags = 0;
184 int nfsproto = IPPROTO_UDP;
185 int force2 = 0;
186 int force3 = 0;
187 int mnttcp_ok = 1;
188 int port = 0;
189
190 #ifdef NFSKERB
191 static char inst[INST_SZ];
192 static char realm[REALM_SZ];
193 static struct {
194 u_long kind;
195 KTEXT_ST kt;
196 } ktick;
197 static struct nfsrpc_nickverf kverf;
198 static struct nfsrpc_fullblock kin, kout;
199 static NFSKERBKEY_T kivec;
200 static CREDENTIALS kcr;
201 static struct timeval ktv;
202 static NFSKERBKEYSCHED_T kerb_keysched;
203 #endif
204
205 static void shownfsargs __P((const struct nfs_args *));
206 static int getnfsargs __P((char *, struct nfs_args *));
207 #ifdef ISO
208 static struct iso_addr *iso_addr __P((const char *));
209 #endif
210 int main __P((int, char *[]));
211 int mount_nfs __P((int argc, char **argv));
212 /* void set_rpc_maxgrouplist __P((int)); */
213 static void usage __P((void));
214 static int xdr_dir __P((XDR *, char *));
215 static int xdr_fh __P((XDR *, struct nfhret *));
216
217 #ifndef MOUNT_NOMAIN
218 int
219 main(argc, argv)
220 int argc;
221 char **argv;
222 {
223 return mount_nfs(argc, argv);
224 }
225 #endif
226
227 int
228 mount_nfs(argc, argv)
229 int argc;
230 char *argv[];
231 {
232 int c, retval;
233 struct nfs_args *nfsargsp;
234 struct nfs_args nfsargs;
235 struct nfsd_cargs ncd;
236 struct sockaddr_storage sa;
237 int mntflags, altflags, i, nfssvc_flag, num;
238 char name[MAXPATHLEN], *p, *spec, *ospec;
239 mntoptparse_t mp;
240 #ifdef NFSKERB
241 uid_t last_ruid;
242
243 last_ruid = -1;
244 if (krb_get_lrealm(realm, 0) != KSUCCESS)
245 (void)strlcpy(realm, KRB_REALM, sizeof(realm));
246 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
247 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
248 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
249 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
250 warnx("Yikes! NFSKERB structs not packed!!\n");
251 #endif
252 retrycnt = DEF_RETRY;
253
254 mntflags = 0;
255 altflags = 0;
256 nfsargs = nfsdefargs;
257 nfsargsp = &nfsargs;
258 while ((c = getopt(argc, argv,
259 "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
260 switch (c) {
261 case '3':
262 if (force2)
263 errx(1, "-2 and -3 are mutually exclusive");
264 force3 = 1;
265 break;
266 case '2':
267 if (force3)
268 errx(1, "-2 and -3 are mutually exclusive");
269 force2 = 1;
270 nfsargsp->flags &= ~NFSMNT_NFSV3;
271 break;
272 case 'a':
273 num = strtol(optarg, &p, 10);
274 if (*p || num < 0)
275 errx(1, "illegal -a value -- %s", optarg);
276 nfsargsp->readahead = num;
277 nfsargsp->flags |= NFSMNT_READAHEAD;
278 break;
279 case 'b':
280 opflags |= BGRND;
281 break;
282 case 'c':
283 nfsargsp->flags |= NFSMNT_NOCONN;
284 break;
285 case 'C':
286 nfsargsp->flags &= ~NFSMNT_NOCONN;
287 break;
288 case 'D':
289 num = strtol(optarg, &p, 10);
290 if (*p || num <= 0)
291 errx(1, "illegal -D value -- %s", optarg);
292 nfsargsp->deadthresh = num;
293 nfsargsp->flags |= NFSMNT_DEADTHRESH;
294 break;
295 case 'd':
296 nfsargsp->flags |= NFSMNT_DUMBTIMR;
297 break;
298 #if 0 /* XXXX */
299 case 'g':
300 num = strtol(optarg, &p, 10);
301 if (*p || num <= 0)
302 errx(1, "illegal -g value -- %s", optarg);
303 set_rpc_maxgrouplist(num);
304 nfsargsp->maxgrouplist = num;
305 nfsargsp->flags |= NFSMNT_MAXGRPS;
306 break;
307 #endif
308 case 'I':
309 num = strtol(optarg, &p, 10);
310 if (*p || num <= 0)
311 errx(1, "illegal -I value -- %s", optarg);
312 nfsargsp->readdirsize = num;
313 nfsargsp->flags |= NFSMNT_READDIRSIZE;
314 break;
315 case 'i':
316 nfsargsp->flags |= NFSMNT_INT;
317 break;
318 #ifdef NFSKERB
319 case 'K':
320 nfsargsp->flags |= NFSMNT_KERB;
321 break;
322 #endif
323 case 'L':
324 num = strtol(optarg, &p, 10);
325 if (*p || num < 2)
326 errx(1, "illegal -L value -- %s", optarg);
327 nfsargsp->leaseterm = num;
328 nfsargsp->flags |= NFSMNT_LEASETERM;
329 break;
330 case 'l':
331 nfsargsp->flags |= NFSMNT_RDIRPLUS;
332 break;
333 #ifdef NFSKERB
334 case 'm':
335 (void)strlcpy(realm, optarg, sizeof(realm));
336 break;
337 #endif
338 case 'o':
339 mp = getmntopts(optarg, mopts, &mntflags, &altflags);
340 if (mp == NULL)
341 err(1, NULL);
342 if (altflags & ALTF_BG)
343 opflags |= BGRND;
344 if (altflags & ALTF_CONN)
345 nfsargsp->flags &= ~NFSMNT_NOCONN;
346 if (altflags & ALTF_DUMBTIMR)
347 nfsargsp->flags |= NFSMNT_DUMBTIMR;
348 if (altflags & ALTF_INTR)
349 nfsargsp->flags |= NFSMNT_INT;
350 #ifdef NFSKERB
351 if (altflags & ALTF_KERB)
352 nfsargsp->flags |= NFSMNT_KERB;
353 #endif
354 if (altflags & ALTF_NFSV3) {
355 if (force2)
356 errx(1, "conflicting version options");
357 force3 = 1;
358 }
359 if (altflags & ALTF_NFSV2) {
360 if (force3)
361 errx(1, "conflicting version options");
362 force2 = 1;
363 nfsargsp->flags &= ~NFSMNT_NFSV3;
364 }
365 if (altflags & ALTF_RDIRPLUS)
366 nfsargsp->flags |= NFSMNT_RDIRPLUS;
367 if (altflags & ALTF_MNTUDP)
368 mnttcp_ok = 0;
369 if (altflags & ALTF_NORESPORT)
370 nfsargsp->flags &= ~NFSMNT_RESVPORT;
371 #ifdef ISO
372 if (altflags & ALTF_SEQPACKET)
373 nfsargsp->sotype = SOCK_SEQPACKET;
374 #endif
375 if (altflags & ALTF_NQNFS) {
376 if (force2)
377 errx(1, "nqnfs only available with v3");
378 force3 = 1;
379 nfsargsp->flags |= NFSMNT_NQNFS;
380 }
381 if (altflags & ALTF_SOFT)
382 nfsargsp->flags |= NFSMNT_SOFT;
383 if (altflags & ALTF_TCP) {
384 nfsargsp->sotype = SOCK_STREAM;
385 nfsproto = IPPROTO_TCP;
386 }
387 if (altflags & ALTF_PORT) {
388 port = getmntoptnum(mp, "port");
389 }
390 if (altflags & ALTF_RSIZE) {
391 nfsargsp->rsize =
392 (int)getmntoptnum(mp, "rsize");
393 nfsargsp->flags |= NFSMNT_RSIZE;
394 }
395 if (altflags & ALTF_WSIZE) {
396 nfsargsp->wsize =
397 (int)getmntoptnum(mp, "wsize");
398 nfsargsp->flags |= NFSMNT_WSIZE;
399 }
400 if (altflags & ALTF_RDIRSIZE) {
401 nfsargsp->rsize =
402 (int)getmntoptnum(mp, "rdirsize");
403 nfsargsp->flags |= NFSMNT_READDIRSIZE;
404 }
405 #if 0
406 if (altflags & ALTF_MAXGRPS) {
407 set_rpc_maxgrouplist(num);
408 nfsargsp->maxgrouplist =
409 (int)getmntoptnum(mp, "maxgrps");
410 nfsargsp->flags |= NFSMNT_MAXGRPS;
411 }
412 #endif
413 if (altflags & ALTF_LEASETERM) {
414 nfsargsp->leaseterm =
415 (int)getmntoptnum(mp, "leaseterm");
416 nfsargsp->flags |= NFSMNT_LEASETERM;
417 }
418 if (altflags & ALTF_READAHEAD) {
419 nfsargsp->readahead =
420 (int)getmntoptnum(mp, "readahead");
421 nfsargsp->flags |= NFSMNT_READAHEAD;
422 }
423 if (altflags & ALTF_DEADTHRESH) {
424 nfsargsp->deadthresh =
425 (int)getmntoptnum(mp, "deadthresh");
426 nfsargsp->flags |= NFSMNT_DEADTHRESH;
427 }
428 if (altflags & ALTF_TIMEO) {
429 nfsargsp->timeo =
430 (int)getmntoptnum(mp, "timeo");
431 nfsargsp->flags |= NFSMNT_TIMEO;
432 }
433 if (altflags & ALTF_RETRANS) {
434 nfsargsp->retrans =
435 (int)getmntoptnum(mp, "retrans");
436 nfsargsp->flags |= NFSMNT_RETRANS;
437 }
438 altflags = 0;
439 freemntopts(mp);
440 break;
441 case 'P':
442 nfsargsp->flags |= NFSMNT_RESVPORT;
443 break;
444 case 'p':
445 nfsargsp->flags &= ~NFSMNT_RESVPORT;
446 break;
447 case 'q':
448 if (force2)
449 errx(1, "nqnfs only available with v3");
450 force3 = 1;
451 nfsargsp->flags |= NFSMNT_NQNFS;
452 break;
453 case 'R':
454 num = strtol(optarg, &p, 10);
455 if (*p || num <= 0)
456 errx(1, "illegal -R value -- %s", optarg);
457 retrycnt = num;
458 break;
459 case 'r':
460 num = strtol(optarg, &p, 10);
461 if (*p || num <= 0)
462 errx(1, "illegal -r value -- %s", optarg);
463 nfsargsp->rsize = num;
464 nfsargsp->flags |= NFSMNT_RSIZE;
465 break;
466 #ifdef ISO
467 case 'S':
468 nfsargsp->sotype = SOCK_SEQPACKET;
469 break;
470 #endif
471 case 's':
472 nfsargsp->flags |= NFSMNT_SOFT;
473 break;
474 case 'T':
475 nfsargsp->sotype = SOCK_STREAM;
476 nfsproto = IPPROTO_TCP;
477 break;
478 case 't':
479 num = strtol(optarg, &p, 10);
480 if (*p || num <= 0)
481 errx(1, "illegal -t value -- %s", optarg);
482 nfsargsp->timeo = num;
483 nfsargsp->flags |= NFSMNT_TIMEO;
484 break;
485 case 'w':
486 num = strtol(optarg, &p, 10);
487 if (*p || num <= 0)
488 errx(1, "illegal -w value -- %s", optarg);
489 nfsargsp->wsize = num;
490 nfsargsp->flags |= NFSMNT_WSIZE;
491 break;
492 case 'x':
493 num = strtol(optarg, &p, 10);
494 if (*p || num <= 0)
495 errx(1, "illegal -x value -- %s", optarg);
496 nfsargsp->retrans = num;
497 nfsargsp->flags |= NFSMNT_RETRANS;
498 break;
499 case 'X':
500 nfsargsp->flags |= NFSMNT_XLATECOOKIE;
501 break;
502 case 'U':
503 mnttcp_ok = 0;
504 break;
505 default:
506 usage();
507 break;
508 }
509 argc -= optind;
510 argv += optind;
511
512 if (argc != 2)
513 usage();
514
515 spec = *argv++;
516 if (realpath(*argv, name) == NULL) /* Check mounton path */
517 err(1, "realpath %s", *argv);
518 if (strncmp(*argv, name, MAXPATHLEN)) {
519 warnx("\"%s\" is a relative path.", *argv);
520 warnx("using \"%s\" instead.", name);
521 }
522
523 if ((ospec = strdup(spec)) == NULL) {
524 err(1, "strdup");
525 }
526
527 if ((mntflags & MNT_GETARGS) != 0) {
528 memset(&sa, 0, sizeof(sa));
529 nfsargsp->addr = (struct sockaddr *)&sa;
530 nfsargsp->addrlen = sizeof(sa);
531 } else {
532 if (!getnfsargs(spec, nfsargsp))
533 exit(1);
534 }
535 if ((retval = mount(MOUNT_NFS, name, mntflags, nfsargsp))) {
536 /* Did we just default to v3 on a v2-only kernel?
537 * If so, default to v2 & try again */
538 if ((errno == EPROGMISMATCH) && !force3) {
539 nfsargsp->flags &= ~NFSMNT_NFSV3;
540 retval = mount(MOUNT_NFS, name, mntflags, nfsargsp);
541 }
542 }
543 if (retval)
544 err(1, "%s on %s", ospec, name);
545 if (mntflags & MNT_GETARGS) {
546 shownfsargs(nfsargsp);
547 return (0);
548 }
549
550 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
551 if ((opflags & ISBGRND) == 0) {
552 if ((i = fork()) != 0) {
553 if (i == -1)
554 err(1, "nqnfs 1");
555 exit(0);
556 }
557 (void) setsid();
558 (void) close(STDIN_FILENO);
559 (void) close(STDOUT_FILENO);
560 (void) close(STDERR_FILENO);
561 (void) chdir("/");
562 }
563 openlog("mount_nfs", LOG_PID, LOG_DAEMON);
564 nfssvc_flag = NFSSVC_MNTD;
565 ncd.ncd_dirp = name;
566 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
567 if (errno != ENEEDAUTH) {
568 syslog(LOG_ERR, "nfssvc err %m");
569 continue;
570 }
571 nfssvc_flag =
572 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
573 #ifdef NFSKERB
574 /*
575 * Set up as ncd_authuid for the kerberos call.
576 * Must set ruid to ncd_authuid and reset the
577 * ticket name iff ncd_authuid is not the same
578 * as last time, so that the right ticket file
579 * is found.
580 * Get the Kerberos credential structure so that
581 * we have the seesion key and get a ticket for
582 * this uid.
583 * For more info see the IETF Draft "Authentication
584 * in ONC RPC".
585 */
586 if (ncd.ncd_authuid != last_ruid) {
587 krb_set_tkt_string("");
588 last_ruid = ncd.ncd_authuid;
589 }
590 setreuid(ncd.ncd_authuid, 0);
591 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
592 if (kret == RET_NOTKT) {
593 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
594 DEFAULT_TKT_LIFE);
595 if (kret == KSUCCESS)
596 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
597 &kcr);
598 }
599 if (kret == KSUCCESS)
600 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
601 realm, 0);
602
603 /*
604 * Fill in the AKN_FULLNAME authenticator and verfier.
605 * Along with the Kerberos ticket, we need to build
606 * the timestamp verifier and encrypt it in CBC mode.
607 */
608 if (kret == KSUCCESS &&
609 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
610 && gettimeofday(&ktv, (struct timezone *)0) == 0) {
611 ncd.ncd_authtype = RPCAUTH_KERB4;
612 ncd.ncd_authstr = (u_char *)&ktick;
613 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
614 3 * NFSX_UNSIGNED;
615 ncd.ncd_verfstr = (u_char *)&kverf;
616 ncd.ncd_verflen = sizeof (kverf);
617 memmove(ncd.ncd_key, kcr.session,
618 sizeof (kcr.session));
619 kin.t1 = htonl(ktv.tv_sec);
620 kin.t2 = htonl(ktv.tv_usec);
621 kin.w1 = htonl(NFS_KERBTTL);
622 kin.w2 = htonl(NFS_KERBTTL - 1);
623 memset((caddr_t)kivec, 0, sizeof (kivec));
624
625 /*
626 * Encrypt kin in CBC mode using the session
627 * key in kcr.
628 */
629 XXX
630
631 /*
632 * Finally, fill the timestamp verifier into the
633 * authenticator and verifier.
634 */
635 ktick.kind = htonl(RPCAKN_FULLNAME);
636 kverf.kind = htonl(RPCAKN_FULLNAME);
637 NFS_KERBW1(ktick.kt) = kout.w1;
638 ktick.kt.length = htonl(ktick.kt.length);
639 kverf.verf.t1 = kout.t1;
640 kverf.verf.t2 = kout.t2;
641 kverf.verf.w2 = kout.w2;
642 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
643 }
644 setreuid(0, 0);
645 #endif /* NFSKERB */
646 }
647 }
648 exit(0);
649 }
650
651 static void
652 shownfsargs(nfsargsp)
653 const struct nfs_args *nfsargsp;
654 {
655 char fbuf[2048];
656 char host[NI_MAXHOST], serv[NI_MAXSERV];
657 int error;
658
659 (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
660 if (nfsargsp->addr != NULL) {
661 error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
662 sizeof(host), serv, sizeof(serv),
663 NI_NUMERICHOST | NI_NUMERICSERV);
664 if (error != 0)
665 warnx("getnameinfo: %s", gai_strerror(error));
666 } else
667 error = -1;
668
669 if (error == 0)
670 printf("addr=%s, port=%s, addrlen=%d, ",
671 host, serv, nfsargsp->addrlen);
672 printf("sotype=%d, proto=%d, fhsize=%d, "
673 "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
674 "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
675 "deadthresh=%d\n",
676 nfsargsp->sotype,
677 nfsargsp->proto,
678 nfsargsp->fhsize,
679 fbuf,
680 nfsargsp->wsize,
681 nfsargsp->rsize,
682 nfsargsp->readdirsize,
683 nfsargsp->timeo,
684 nfsargsp->retrans,
685 nfsargsp->maxgrouplist,
686 nfsargsp->readahead,
687 nfsargsp->leaseterm,
688 nfsargsp->deadthresh);
689 }
690
691 static int
692 getnfsargs(spec, nfsargsp)
693 char *spec;
694 struct nfs_args *nfsargsp;
695 {
696 CLIENT *clp;
697 struct addrinfo hints, *ai_nfs, *ai;
698 int ecode;
699 char host[NI_MAXHOST], serv[NI_MAXSERV];
700 static struct netbuf nfs_nb;
701 static struct sockaddr_storage nfs_ss;
702 struct netconfig *nconf;
703 char *netid;
704 #ifdef ISO
705 static struct sockaddr_iso isoaddr;
706 struct iso_addr *isop;
707 int isoflag = 0;
708 #endif
709 struct timeval pertry, try;
710 enum clnt_stat clnt_stat;
711 int i, nfsvers, mntvers, orgcnt;
712 char *hostp, *delimp;
713 #ifdef NFSKERB
714 char *cp;
715 #endif
716 static struct nfhret nfhret;
717 static char nam[MNAMELEN + 1];
718
719 strncpy(nam, spec, MNAMELEN);
720 nam[MNAMELEN] = '\0';
721 if ((delimp = strchr(spec, '@')) != NULL) {
722 hostp = delimp + 1;
723 } else if ((delimp = strrchr(spec, ':')) != NULL) {
724 hostp = spec;
725 spec = delimp + 1;
726 } else {
727 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
728 return (0);
729 }
730 *delimp = '\0';
731 /*
732 * DUMB!! Until the mount protocol works on iso transport, we must
733 * supply both an iso and an inet address for the host.
734 */
735 #ifdef ISO
736 if (!strncmp(hostp, "iso=", 4)) {
737 u_short isoport;
738
739 hostp += 4;
740 isoflag++;
741 if ((delimp = strchr(hostp, '+')) == NULL) {
742 warnx("no iso+inet address");
743 return (0);
744 }
745 *delimp = '\0';
746 if ((isop = iso_addr(hostp)) == NULL) {
747 warnx("bad ISO address");
748 return (0);
749 }
750 memset(&isoaddr, 0, sizeof (isoaddr));
751 memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
752 isoaddr.siso_len = sizeof (isoaddr);
753 isoaddr.siso_family = AF_ISO;
754 isoaddr.siso_tlen = 2;
755 isoport = htons(NFS_PORT);
756 memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
757 hostp = delimp + 1;
758 }
759 #endif /* ISO */
760
761 /*
762 * Handle an internet host address and reverse resolve it if
763 * doing Kerberos.
764 */
765 memset(&hints, 0, sizeof hints);
766 hints.ai_flags = AI_NUMERICHOST;
767 hints.ai_socktype = nfsargsp->sotype;
768 if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) == 0) {
769 if ((nfsargsp->flags & NFSMNT_KERB)) {
770 hints.ai_flags = 0;
771 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
772 sizeof host, serv, sizeof serv, 0) != 0) {
773 warnx("can't reverse resolve net address for "
774 "host \"%s\": %s", hostp,
775 gai_strerror(ecode));
776 return (0);
777 }
778 hostp = host;
779 }
780 } else {
781 hints.ai_flags = 0;
782 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
783 warnx("can't get net id for host \"%s\": %s", hostp,
784 gai_strerror(ecode));
785 return (0);
786 }
787 }
788 #ifdef NFSKERB
789 if (nfsargsp->flags & NFSMNT_KERB) {
790 strncpy(inst, hp->h_name, INST_SZ);
791 inst[INST_SZ - 1] = '\0';
792 if (cp = strchr(inst, '.'))
793 *cp = '\0';
794 }
795 #endif /* NFSKERB */
796
797 if (force2) {
798 nfsvers = NFS_VER2;
799 mntvers = RPCMNT_VER1;
800 } else {
801 nfsvers = NFS_VER3;
802 mntvers = RPCMNT_VER3;
803 }
804 orgcnt = retrycnt;
805 nfhret.stat = EACCES; /* Mark not yet successful */
806
807 for (ai = ai_nfs; ai; ai = ai->ai_next) {
808 /*
809 * XXX. Nead a generic (family, type, proto) -> nconf interface.
810 * __rpc_*2nconf exist, maybe they should be exported.
811 */
812 if (nfsargsp->sotype == SOCK_STREAM) {
813 if (ai->ai_family == AF_INET6)
814 netid = "tcp6";
815 else
816 netid = "tcp";
817 } else {
818 if (ai->ai_family == AF_INET6)
819 netid = "udp6";
820 else
821 netid = "udp";
822 }
823
824 nconf = getnetconfigent(netid);
825
826 tryagain:
827 retrycnt = orgcnt;
828
829 while (retrycnt > 0) {
830 nfs_nb.buf = &nfs_ss;
831 nfs_nb.maxlen = sizeof nfs_ss;
832 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
833 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
834 nfhret.stat = rpc_createerr.cf_error.re_errno;
835 break;
836 }
837 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
838 nfhret.stat = EPROTONOSUPPORT;
839 break;
840 }
841 if ((opflags & ISBGRND) == 0)
842 clnt_pcreateerror(
843 "mount_nfs: rpcbind to nfs on server");
844 } else {
845 pertry.tv_sec = 10;
846 pertry.tv_usec = 0;
847 /*
848 * XXX relies on clnt_tcp_create to bind to a reserved
849 * socket.
850 */
851 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
852 mnttcp_ok ? nconf : getnetconfigent("udp"));
853 if (clp == NULL) {
854 if ((opflags & ISBGRND) == 0) {
855 clnt_pcreateerror(
856 "Cannot MNT RPC (mountd)");
857 }
858 } else {
859 CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
860 (char *)&pertry);
861 clp->cl_auth = authsys_create_default();
862 try.tv_sec = 10;
863 try.tv_usec = 0;
864 if (nfsargsp->flags & NFSMNT_KERB)
865 nfhret.auth = RPCAUTH_KERB4;
866 else
867 nfhret.auth = RPCAUTH_UNIX;
868 nfhret.vers = mntvers;
869 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
870 xdr_dir, spec, xdr_fh, &nfhret, try);
871 switch (clnt_stat) {
872 case RPC_PROGVERSMISMATCH:
873 if (nfsvers == NFS_VER3 && !force3) {
874 nfsvers = NFS_VER2;
875 mntvers = RPCMNT_VER1;
876 nfsargsp->flags &=
877 ~NFSMNT_NFSV3;
878 goto tryagain;
879 } else {
880 errx(1, "%s", clnt_sperror(clp,
881 "MNT RPC"));
882 }
883 case RPC_SUCCESS:
884 auth_destroy(clp->cl_auth);
885 clnt_destroy(clp);
886 retrycnt = 0;
887 break;
888 default:
889 /* XXX should give up on some errors */
890 if ((opflags & ISBGRND) == 0)
891 warnx("%s", clnt_sperror(clp,
892 "bad MNT RPC"));
893 break;
894 }
895 }
896 }
897 if (--retrycnt > 0) {
898 if (opflags & BGRND) {
899 opflags &= ~BGRND;
900 if ((i = fork()) != 0) {
901 if (i == -1)
902 err(1, "nqnfs 2");
903 exit(0);
904 }
905 (void) setsid();
906 (void) close(STDIN_FILENO);
907 (void) close(STDOUT_FILENO);
908 (void) close(STDERR_FILENO);
909 (void) chdir("/");
910 opflags |= ISBGRND;
911 }
912 sleep(60);
913 }
914 }
915 if (nfhret.stat == 0)
916 break;
917 }
918 freeaddrinfo(ai_nfs);
919 if (nfhret.stat) {
920 if (opflags & ISBGRND)
921 exit(1);
922 errno = nfhret.stat;
923 warnx("can't access %s: %s", spec, strerror(nfhret.stat));
924 return (0);
925 }
926 #ifdef ISO
927 if (isoflag) {
928 nfsargsp->addr = (struct sockaddr *) &isoaddr;
929 nfsargsp->addrlen = sizeof (isoaddr);
930 } else
931 #endif /* ISO */
932 {
933 nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
934 nfsargsp->addrlen = nfs_nb.len;
935 if (port != 0) {
936 struct sockaddr *sa = nfsargsp->addr;
937 switch (sa->sa_family) {
938 case AF_INET:
939 ((struct sockaddr_in *)sa)->sin_port = port;
940 #ifdef INET6
941 case AF_INET6:
942 ((struct sockaddr_in6 *)sa)->sin6_port = port;
943 break;
944 #endif
945 default:
946 errx(1, "Unsupported socket family %d",
947 sa->sa_family);
948 }
949 }
950 }
951 nfsargsp->fh = nfhret.nfh;
952 nfsargsp->fhsize = nfhret.fhsize;
953 nfsargsp->hostname = nam;
954 return (1);
955 }
956
957 /*
958 * xdr routines for mount rpc's
959 */
960 static int
961 xdr_dir(xdrsp, dirp)
962 XDR *xdrsp;
963 char *dirp;
964 {
965 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
966 }
967
968 static int
969 xdr_fh(xdrsp, np)
970 XDR *xdrsp;
971 struct nfhret *np;
972 {
973 int i;
974 long auth, authcnt, authfnd = 0;
975
976 if (!xdr_u_long(xdrsp, &np->stat))
977 return (0);
978 if (np->stat)
979 return (1);
980 switch (np->vers) {
981 case 1:
982 np->fhsize = NFSX_V2FH;
983 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
984 case 3:
985 if (!xdr_long(xdrsp, &np->fhsize))
986 return (0);
987 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
988 return (0);
989 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
990 return (0);
991 if (!xdr_long(xdrsp, &authcnt))
992 return (0);
993 for (i = 0; i < authcnt; i++) {
994 if (!xdr_long(xdrsp, &auth))
995 return (0);
996 if (auth == np->auth)
997 authfnd++;
998 }
999 /*
1000 * Some servers, such as DEC's OSF/1 return a nil authenticator
1001 * list to indicate RPCAUTH_UNIX.
1002 */
1003 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
1004 np->stat = EAUTH;
1005 return (1);
1006 };
1007 return (0);
1008 }
1009
1010 static void
1011 usage()
1012 {
1013 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
1014 "[-23bcCdiKlpPqsTUX] [-a maxreadahead] [-D deadthresh]",
1015 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm] [-m realm]",
1016 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
1017 "\t[-w writesize] [-x retrans]",
1018 "\trhost:path node");
1019 exit(1);
1020 }
1021