mount_nfs.c revision 1.45 1 /* $NetBSD: mount_nfs.c,v 1.45 2003/08/07 10:04:29 agc 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.45 2003/08/07 10:04:29 agc 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, *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 name = *argv;
517 if ((ospec = strdup(spec)) == NULL) {
518 err(1, "strdup");
519 }
520
521 if ((mntflags & MNT_GETARGS) != 0) {
522 memset(&sa, 0, sizeof(sa));
523 nfsargsp->addr = (struct sockaddr *)&sa;
524 nfsargsp->addrlen = sizeof(sa);
525 } else {
526 if (!getnfsargs(spec, nfsargsp))
527 exit(1);
528 }
529 if ((retval = mount(MOUNT_NFS, name, mntflags, nfsargsp))) {
530 /* Did we just default to v3 on a v2-only kernel?
531 * If so, default to v2 & try again */
532 if ((errno == EPROGMISMATCH) && !force3) {
533 nfsargsp->flags &= ~NFSMNT_NFSV3;
534 retval = mount(MOUNT_NFS, name, mntflags, nfsargsp);
535 }
536 }
537 if (retval)
538 err(1, "%s on %s", ospec, name);
539 if (mntflags & MNT_GETARGS) {
540 shownfsargs(nfsargsp);
541 return (0);
542 }
543
544 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
545 if ((opflags & ISBGRND) == 0) {
546 if ((i = fork()) != 0) {
547 if (i == -1)
548 err(1, "nqnfs 1");
549 exit(0);
550 }
551 (void) setsid();
552 (void) close(STDIN_FILENO);
553 (void) close(STDOUT_FILENO);
554 (void) close(STDERR_FILENO);
555 (void) chdir("/");
556 }
557 openlog("mount_nfs", LOG_PID, LOG_DAEMON);
558 nfssvc_flag = NFSSVC_MNTD;
559 ncd.ncd_dirp = name;
560 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
561 if (errno != ENEEDAUTH) {
562 syslog(LOG_ERR, "nfssvc err %m");
563 continue;
564 }
565 nfssvc_flag =
566 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
567 #ifdef NFSKERB
568 /*
569 * Set up as ncd_authuid for the kerberos call.
570 * Must set ruid to ncd_authuid and reset the
571 * ticket name iff ncd_authuid is not the same
572 * as last time, so that the right ticket file
573 * is found.
574 * Get the Kerberos credential structure so that
575 * we have the seesion key and get a ticket for
576 * this uid.
577 * For more info see the IETF Draft "Authentication
578 * in ONC RPC".
579 */
580 if (ncd.ncd_authuid != last_ruid) {
581 krb_set_tkt_string("");
582 last_ruid = ncd.ncd_authuid;
583 }
584 setreuid(ncd.ncd_authuid, 0);
585 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
586 if (kret == RET_NOTKT) {
587 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
588 DEFAULT_TKT_LIFE);
589 if (kret == KSUCCESS)
590 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
591 &kcr);
592 }
593 if (kret == KSUCCESS)
594 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
595 realm, 0);
596
597 /*
598 * Fill in the AKN_FULLNAME authenticator and verfier.
599 * Along with the Kerberos ticket, we need to build
600 * the timestamp verifier and encrypt it in CBC mode.
601 */
602 if (kret == KSUCCESS &&
603 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
604 && gettimeofday(&ktv, (struct timezone *)0) == 0) {
605 ncd.ncd_authtype = RPCAUTH_KERB4;
606 ncd.ncd_authstr = (u_char *)&ktick;
607 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
608 3 * NFSX_UNSIGNED;
609 ncd.ncd_verfstr = (u_char *)&kverf;
610 ncd.ncd_verflen = sizeof (kverf);
611 memmove(ncd.ncd_key, kcr.session,
612 sizeof (kcr.session));
613 kin.t1 = htonl(ktv.tv_sec);
614 kin.t2 = htonl(ktv.tv_usec);
615 kin.w1 = htonl(NFS_KERBTTL);
616 kin.w2 = htonl(NFS_KERBTTL - 1);
617 memset((caddr_t)kivec, 0, sizeof (kivec));
618
619 /*
620 * Encrypt kin in CBC mode using the session
621 * key in kcr.
622 */
623 XXX
624
625 /*
626 * Finally, fill the timestamp verifier into the
627 * authenticator and verifier.
628 */
629 ktick.kind = htonl(RPCAKN_FULLNAME);
630 kverf.kind = htonl(RPCAKN_FULLNAME);
631 NFS_KERBW1(ktick.kt) = kout.w1;
632 ktick.kt.length = htonl(ktick.kt.length);
633 kverf.verf.t1 = kout.t1;
634 kverf.verf.t2 = kout.t2;
635 kverf.verf.w2 = kout.w2;
636 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
637 }
638 setreuid(0, 0);
639 #endif /* NFSKERB */
640 }
641 }
642 exit(0);
643 }
644
645 static void
646 shownfsargs(nfsargsp)
647 const struct nfs_args *nfsargsp;
648 {
649 char fbuf[2048];
650 char host[NI_MAXHOST], serv[NI_MAXSERV];
651 int error;
652
653 (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
654 if (nfsargsp->addr != NULL) {
655 error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
656 sizeof(host), serv, sizeof(serv),
657 NI_NUMERICHOST | NI_NUMERICSERV);
658 if (error != 0)
659 warnx("getnameinfo: %s", gai_strerror(error));
660 } else
661 error = -1;
662
663 if (error == 0)
664 printf("addr=%s, port=%s, addrlen=%d, ",
665 host, serv, nfsargsp->addrlen);
666 printf("sotype=%d, proto=%d, fhsize=%d, "
667 "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
668 "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
669 "deadthresh=%d\n",
670 nfsargsp->sotype,
671 nfsargsp->proto,
672 nfsargsp->fhsize,
673 fbuf,
674 nfsargsp->wsize,
675 nfsargsp->rsize,
676 nfsargsp->readdirsize,
677 nfsargsp->timeo,
678 nfsargsp->retrans,
679 nfsargsp->maxgrouplist,
680 nfsargsp->readahead,
681 nfsargsp->leaseterm,
682 nfsargsp->deadthresh);
683 }
684
685 static int
686 getnfsargs(spec, nfsargsp)
687 char *spec;
688 struct nfs_args *nfsargsp;
689 {
690 CLIENT *clp;
691 struct addrinfo hints, *ai_nfs, *ai;
692 int ecode;
693 char host[NI_MAXHOST], serv[NI_MAXSERV];
694 static struct netbuf nfs_nb;
695 static struct sockaddr_storage nfs_ss;
696 struct netconfig *nconf;
697 char *netid;
698 #ifdef ISO
699 static struct sockaddr_iso isoaddr;
700 struct iso_addr *isop;
701 int isoflag = 0;
702 #endif
703 struct timeval pertry, try;
704 enum clnt_stat clnt_stat;
705 int i, nfsvers, mntvers, orgcnt;
706 char *hostp, *delimp;
707 #ifdef NFSKERB
708 char *cp;
709 #endif
710 static struct nfhret nfhret;
711 static char nam[MNAMELEN + 1];
712
713 strncpy(nam, spec, MNAMELEN);
714 nam[MNAMELEN] = '\0';
715 if ((delimp = strchr(spec, '@')) != NULL) {
716 hostp = delimp + 1;
717 } else if ((delimp = strrchr(spec, ':')) != NULL) {
718 hostp = spec;
719 spec = delimp + 1;
720 } else {
721 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
722 return (0);
723 }
724 *delimp = '\0';
725 /*
726 * DUMB!! Until the mount protocol works on iso transport, we must
727 * supply both an iso and an inet address for the host.
728 */
729 #ifdef ISO
730 if (!strncmp(hostp, "iso=", 4)) {
731 u_short isoport;
732
733 hostp += 4;
734 isoflag++;
735 if ((delimp = strchr(hostp, '+')) == NULL) {
736 warnx("no iso+inet address");
737 return (0);
738 }
739 *delimp = '\0';
740 if ((isop = iso_addr(hostp)) == NULL) {
741 warnx("bad ISO address");
742 return (0);
743 }
744 memset(&isoaddr, 0, sizeof (isoaddr));
745 memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
746 isoaddr.siso_len = sizeof (isoaddr);
747 isoaddr.siso_family = AF_ISO;
748 isoaddr.siso_tlen = 2;
749 isoport = htons(NFS_PORT);
750 memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
751 hostp = delimp + 1;
752 }
753 #endif /* ISO */
754
755 /*
756 * Handle an internet host address and reverse resolve it if
757 * doing Kerberos.
758 */
759 memset(&hints, 0, sizeof hints);
760 hints.ai_flags = AI_NUMERICHOST;
761 hints.ai_socktype = nfsargsp->sotype;
762 if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) == 0) {
763 if ((nfsargsp->flags & NFSMNT_KERB)) {
764 hints.ai_flags = 0;
765 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
766 sizeof host, serv, sizeof serv, 0) != 0) {
767 warnx("can't reverse resolve net address for "
768 "host \"%s\": %s", hostp,
769 gai_strerror(ecode));
770 return (0);
771 }
772 hostp = host;
773 }
774 } else {
775 hints.ai_flags = 0;
776 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
777 warnx("can't get net id for host \"%s\": %s", hostp,
778 gai_strerror(ecode));
779 return (0);
780 }
781 }
782 #ifdef NFSKERB
783 if (nfsargsp->flags & NFSMNT_KERB) {
784 strncpy(inst, hp->h_name, INST_SZ);
785 inst[INST_SZ - 1] = '\0';
786 if (cp = strchr(inst, '.'))
787 *cp = '\0';
788 }
789 #endif /* NFSKERB */
790
791 if (force2) {
792 nfsvers = NFS_VER2;
793 mntvers = RPCMNT_VER1;
794 } else {
795 nfsvers = NFS_VER3;
796 mntvers = RPCMNT_VER3;
797 }
798 orgcnt = retrycnt;
799 nfhret.stat = EACCES; /* Mark not yet successful */
800
801 for (ai = ai_nfs; ai; ai = ai->ai_next) {
802 /*
803 * XXX. Nead a generic (family, type, proto) -> nconf interface.
804 * __rpc_*2nconf exist, maybe they should be exported.
805 */
806 if (nfsargsp->sotype == SOCK_STREAM) {
807 if (ai->ai_family == AF_INET6)
808 netid = "tcp6";
809 else
810 netid = "tcp";
811 } else {
812 if (ai->ai_family == AF_INET6)
813 netid = "udp6";
814 else
815 netid = "udp";
816 }
817
818 nconf = getnetconfigent(netid);
819
820 tryagain:
821 retrycnt = orgcnt;
822
823 while (retrycnt > 0) {
824 nfs_nb.buf = &nfs_ss;
825 nfs_nb.maxlen = sizeof nfs_ss;
826 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
827 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
828 nfhret.stat = rpc_createerr.cf_error.re_errno;
829 break;
830 }
831 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
832 nfhret.stat = EPROTONOSUPPORT;
833 break;
834 }
835 if ((opflags & ISBGRND) == 0)
836 clnt_pcreateerror(
837 "mount_nfs: rpcbind to nfs on server");
838 } else {
839 pertry.tv_sec = 10;
840 pertry.tv_usec = 0;
841 /*
842 * XXX relies on clnt_tcp_create to bind to a reserved
843 * socket.
844 */
845 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
846 mnttcp_ok ? nconf : getnetconfigent("udp"));
847 if (clp == NULL) {
848 if ((opflags & ISBGRND) == 0) {
849 clnt_pcreateerror(
850 "Cannot MNT RPC (mountd)");
851 }
852 } else {
853 CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
854 (char *)&pertry);
855 clp->cl_auth = authsys_create_default();
856 try.tv_sec = 10;
857 try.tv_usec = 0;
858 if (nfsargsp->flags & NFSMNT_KERB)
859 nfhret.auth = RPCAUTH_KERB4;
860 else
861 nfhret.auth = RPCAUTH_UNIX;
862 nfhret.vers = mntvers;
863 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
864 xdr_dir, spec, xdr_fh, &nfhret, try);
865 switch (clnt_stat) {
866 case RPC_PROGVERSMISMATCH:
867 if (nfsvers == NFS_VER3 && !force3) {
868 nfsvers = NFS_VER2;
869 mntvers = RPCMNT_VER1;
870 nfsargsp->flags &=
871 ~NFSMNT_NFSV3;
872 goto tryagain;
873 } else {
874 errx(1, "%s", clnt_sperror(clp,
875 "MNT RPC"));
876 }
877 case RPC_SUCCESS:
878 auth_destroy(clp->cl_auth);
879 clnt_destroy(clp);
880 retrycnt = 0;
881 break;
882 default:
883 /* XXX should give up on some errors */
884 if ((opflags & ISBGRND) == 0)
885 warnx("%s", clnt_sperror(clp,
886 "bad MNT RPC"));
887 break;
888 }
889 }
890 }
891 if (--retrycnt > 0) {
892 if (opflags & BGRND) {
893 opflags &= ~BGRND;
894 if ((i = fork()) != 0) {
895 if (i == -1)
896 err(1, "nqnfs 2");
897 exit(0);
898 }
899 (void) setsid();
900 (void) close(STDIN_FILENO);
901 (void) close(STDOUT_FILENO);
902 (void) close(STDERR_FILENO);
903 (void) chdir("/");
904 opflags |= ISBGRND;
905 }
906 sleep(60);
907 }
908 }
909 if (nfhret.stat == 0)
910 break;
911 }
912 freeaddrinfo(ai_nfs);
913 if (nfhret.stat) {
914 if (opflags & ISBGRND)
915 exit(1);
916 errno = nfhret.stat;
917 warnx("can't access %s: %s", spec, strerror(nfhret.stat));
918 return (0);
919 }
920 #ifdef ISO
921 if (isoflag) {
922 nfsargsp->addr = (struct sockaddr *) &isoaddr;
923 nfsargsp->addrlen = sizeof (isoaddr);
924 } else
925 #endif /* ISO */
926 {
927 nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
928 nfsargsp->addrlen = nfs_nb.len;
929 if (port != 0) {
930 struct sockaddr *sa = nfsargsp->addr;
931 switch (sa->sa_family) {
932 case AF_INET:
933 ((struct sockaddr_in *)sa)->sin_port = port;
934 #ifdef INET6
935 case AF_INET6:
936 ((struct sockaddr_in6 *)sa)->sin6_port = port;
937 break;
938 #endif
939 default:
940 errx(1, "Unsupported socket family %d",
941 sa->sa_family);
942 }
943 }
944 }
945 nfsargsp->fh = nfhret.nfh;
946 nfsargsp->fhsize = nfhret.fhsize;
947 nfsargsp->hostname = nam;
948 return (1);
949 }
950
951 /*
952 * xdr routines for mount rpc's
953 */
954 static int
955 xdr_dir(xdrsp, dirp)
956 XDR *xdrsp;
957 char *dirp;
958 {
959 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
960 }
961
962 static int
963 xdr_fh(xdrsp, np)
964 XDR *xdrsp;
965 struct nfhret *np;
966 {
967 int i;
968 long auth, authcnt, authfnd = 0;
969
970 if (!xdr_u_long(xdrsp, &np->stat))
971 return (0);
972 if (np->stat)
973 return (1);
974 switch (np->vers) {
975 case 1:
976 np->fhsize = NFSX_V2FH;
977 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
978 case 3:
979 if (!xdr_long(xdrsp, &np->fhsize))
980 return (0);
981 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
982 return (0);
983 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
984 return (0);
985 if (!xdr_long(xdrsp, &authcnt))
986 return (0);
987 for (i = 0; i < authcnt; i++) {
988 if (!xdr_long(xdrsp, &auth))
989 return (0);
990 if (auth == np->auth)
991 authfnd++;
992 }
993 /*
994 * Some servers, such as DEC's OSF/1 return a nil authenticator
995 * list to indicate RPCAUTH_UNIX.
996 */
997 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
998 np->stat = EAUTH;
999 return (1);
1000 };
1001 return (0);
1002 }
1003
1004 static void
1005 usage()
1006 {
1007 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
1008 "[-23bcCdiKlpPqsTUX] [-a maxreadahead] [-D deadthresh]",
1009 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm] [-m realm]",
1010 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
1011 "\t[-w writesize] [-x retrans]",
1012 "\trhost:path node");
1013 exit(1);
1014 }
1015