Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/planix-v0/sys/src/cmd/unix/9pfreebsd/mount_9fs/mount_9fs.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
/*
2
 * Copyright (c) 1992, 1993, 1994
3
 *	The Regents of the University of California.  All rights reserved.
4
 *
5
 * This code is derived from software contributed to Berkeley by
6
 * Rick Macklem at The University of Guelph.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. All advertising materials mentioning features or use of this software
17
 *    must display the following acknowledgement:
18
 *	This product includes software developed by the University of
19
 *	California, Berkeley and its contributors.
20
 * 4. Neither the name of the University nor the names of its contributors
21
 *    may be used to endorse or promote products derived from this software
22
 *    without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
 * SUCH DAMAGE.
35
 */
36
 
37
#ifndef lint
38
static const char copyright[] =
39
"@(#) Copyright (c) 1992, 1993, 1994\n\
40
	The Regents of the University of California.  All rights reserved.\n";
41
#endif /* not lint */
42
 
43
#ifndef lint
44
#if 0
45
static char sccsid[] = "@(#)mount_nfs.c	8.11 (Berkeley) 5/4/95";
46
#endif
47
static const char rcsid[] =
48
	"$Id: mount_nfs.c,v 1.29 1998/07/06 07:15:53 charnier Exp $";
49
#endif /* not lint */
50
 
51
#include <sys/param.h>
52
#include <sys/mount.h>
53
#include <sys/stat.h>
54
#include <sys/syslog.h>
55
 
56
#include <rpc/rpc.h>
57
#include <rpc/pmap_clnt.h>
58
#include <rpc/pmap_prot.h>
59
 
60
#ifdef ISO
61
#include <netiso/iso.h>
62
#endif
63
 
64
#ifdef NFSKERB
65
#include <kerberosIV/des.h>
66
#include <kerberosIV/krb.h>
67
#endif
68
 
69
#include <sys/vnode.h>
70
#include "9p.h"
71
#include "9auth.h"
72
#include "9fs.h"
73
#include <pwd.h>
74
 
75
#include <nfs/rpcv2.h>
76
#include <nfs/nfsproto.h>
77
#include <nfs/nfs.h>
78
#include <nfs/nqnfs.h>
79
 
80
#include <arpa/inet.h>
81
 
82
#include <ctype.h>
83
#include <err.h>
84
#include <errno.h>
85
#include <netdb.h>
86
#include <stdio.h>
87
#include <stdlib.h>
88
#include <string.h>
89
#include <strings.h>
90
#include <sysexits.h>
91
#include <unistd.h>
92
 
93
#include "mntopts.h"
94
 
95
#define	ALTF_BG		0x1
96
#define ALTF_NOCONN	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_RESVPORT	0x100
104
#define ALTF_SEQPACKET	0x200
105
#define ALTF_NQNFS	0x400
106
#define ALTF_SOFT	0x800
107
#define ALTF_TCP	0x1000
108
#define ALTF_PORT	0x2000
109
#define ALTF_NFSV2	0x4000
110
#define ALTF_ACREGMIN	0x8000
111
#define ALTF_ACREGMAX	0x10000
112
#define ALTF_ACDIRMIN	0x20000
113
#define ALTF_ACDIRMAX	0x40000
114
 
115
struct mntopt mopts[] = {
116
	MOPT_STDOPTS,
117
	MOPT_FORCE,
118
	MOPT_UPDATE,
119
	MOPT_ASYNC,
120
	{ "bg", 0, ALTF_BG, 1 },
121
	{ "conn", 1, ALTF_NOCONN, 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
	{ "resvport", 0, ALTF_RESVPORT, 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
	{ "port=", 0, ALTF_PORT, 1 },
138
	{ "nfsv2", 0, ALTF_NFSV2, 1 },
139
	{ "acregmin=", 0, ALTF_ACREGMIN, 1 },
140
	{ "acregmax=", 0, ALTF_ACREGMAX, 1 },
141
	{ "acdirmin=", 0, ALTF_ACDIRMIN, 1 },
142
	{ "acdirmax=", 0, ALTF_ACDIRMAX, 1 },
143
	{ NULL }
144
};
145
 
146
struct u9fs_args u9fsdefargs = {
147
        1,
148
	(struct sockaddr *)0,
149
	sizeof (struct sockaddr_in),
150
	SOCK_SEQPACKET,
151
	IPPROTO_IL,
152
	0,
153
	0,
154
	0,
155
	0,
156
	0,
157
	(char *)0,
158
	0,
159
	0,
160
	SOCK_SEQPACKET,
161
	IPPROTO_IL,
162
};
163
 
164
struct nfhret {
165
	u_long		stat;
166
	long		vers;
167
	long		auth;
168
	long		fhsize;
169
	u_char		nfh[NFSX_V3FHMAX];
170
};
171
#define	DEF_RETRY	10000
172
#define	BGRND	1
173
#define	ISBGRND	2
174
int retrycnt = DEF_RETRY;
175
int opflags = 0;
176
int nfsproto = IPPROTO_UDP;
177
int mnttcp_ok = 1;
178
u_short port_no = 0;
179
enum {
180
	ANY,
181
	V2,
182
	V3
183
} mountmode = ANY;
184
 
185
#ifdef NFSKERB
186
char inst[INST_SZ];
187
char realm[REALM_SZ];
188
struct {
189
	u_long		kind;
190
	KTEXT_ST	kt;
191
} ktick;
192
struct nfsrpc_nickverf kverf;
193
struct nfsrpc_fullblock kin, kout;
194
NFSKERBKEY_T kivec;
195
CREDENTIALS kcr;
196
struct timeval ktv;
197
NFSKERBKEYSCHED_T kerb_keysched;
198
#endif
199
 
200
int	getnfsargs __P((char *, struct u9fs_args *));
201
#ifdef ISO
202
struct	iso_addr *iso_addr __P((const char *));
203
#endif
204
void	set_rpc_maxgrouplist __P((int));
205
void	usage __P((void)) __dead2;
206
int	xdr_dir __P((XDR *, char *));
207
int	xdr_fh __P((XDR *, struct nfhret *));
208
 
209
void gethostaddr(char * hostp, struct sockaddr_in * saddr);
210
 
211
/*
212
 * Used to set mount flags with getmntopts.  Call with dir=TRUE to
213
 * initialize altflags from the current mount flags.  Call with
214
 * dir=FALSE to update mount flags with the new value of altflags after
215
 * the call to getmntopts.
216
 */
217
static void
218
setflags(int* altflags, int* nfsflags, int dir)
219
{
220
#define F2(af, nf)					\
221
	if (dir) {					\
222
		if (*nfsflags & NFSMNT_##nf)		\
223
			*altflags |= ALTF_##af;		\
224
		else					\
225
			*altflags &= ~ALTF_##af;	\
226
	} else {					\
227
		if (*altflags & ALTF_##af)		\
228
			*nfsflags |= NFSMNT_##nf;	\
229
		else					\
230
			*nfsflags &= ~NFSMNT_##nf;	\
231
	}
232
#define F(f)	F2(f,f)
233
 
234
	F(NOCONN);
235
	F(DUMBTIMR);
236
	F2(INTR, INT);
237
#ifdef NFSKERB
238
	F(KERB);
239
#endif
240
	F(RDIRPLUS);
241
	F(RESVPORT);
242
	F(NQNFS);
243
	F(SOFT);
244
 
245
#undef F
246
#undef F2
247
}
248
 
249
int
250
main(argc, argv)
251
	int argc;
252
	char *argv[];
253
{
254
	register int c;
255
	register struct u9fs_args *nfsargsp;
256
	struct u9fs_args u9fsargs;
257
	struct nfsd_cargs ncd;
258
	int mntflags, altflags, i, nfssvc_flag, num;
259
	char *name, *p, *spec;
260
	struct vfsconf vfc;
261
	int error = 0;
262
	static struct sockaddr_in authaddr;
263
 
264
#ifdef NFSKERB
265
	uid_t last_ruid;
266
 
267
	last_ruid = -1;
268
	(void)strcpy(realm, KRB_REALM);
269
	if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
270
	    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
271
	    ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
272
	    ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
273
		fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
274
#endif /* NFSKERB */
275
	retrycnt = DEF_RETRY;
276
 
277
	mntflags = 0;
278
	altflags = 0;
279
	u9fsargs = u9fsdefargs;
280
	nfsargsp = &u9fsargs;
281
	while ((c = getopt(argc, argv,
282
	    "23a:bcdD:g:I:iKL:lm:No:PpqR:r:sTt:w:x:Uu:")) != -1)
283
		switch (c) {
284
		case '2':
285
			mountmode = V2;
286
			break;
287
		case '3':
288
			mountmode = V3;
289
			break;
290
		case 'a':
291
			num = strtol(optarg, &p, 10);
292
			if (*p || num < 0)
293
				errx(1, "illegal -a value -- %s", optarg);
294
#if 0
295
			nfsargsp->readahead = num;
296
			nfsargsp->flags |= NFSMNT_READAHEAD;
297
#endif
298
			break;
299
		case 'b':
300
			opflags |= BGRND;
301
			break;
302
		case 'c':
303
			nfsargsp->flags |= NFSMNT_NOCONN;
304
			break;
305
		case 'D':
306
			num = strtol(optarg, &p, 10);
307
			if (*p || num <= 0)
308
				errx(1, "illegal -D value -- %s", optarg);
309
#if 0
310
			nfsargsp->deadthresh = num;
311
			nfsargsp->flags |= NFSMNT_DEADTHRESH;
312
#endif
313
			break;
314
		case 'd':
315
			nfsargsp->flags |= NFSMNT_DUMBTIMR;
316
			break;
317
		case 'g':
318
			num = strtol(optarg, &p, 10);
319
			if (*p || num <= 0)
320
				errx(1, "illegal -g value -- %s", optarg);
321
#ifdef __FreeBSD__
322
			set_rpc_maxgrouplist(num);
323
#endif
324
#if 0
325
			nfsargsp->maxgrouplist = num;
326
			nfsargsp->flags |= NFSMNT_MAXGRPS;
327
#endif
328
			break;
329
		case 'I':
330
			num = strtol(optarg, &p, 10);
331
			if (*p || num <= 0)
332
				errx(1, "illegal -I value -- %s", optarg);
333
			nfsargsp->readdirsize = num;
334
			nfsargsp->flags |= NFSMNT_READDIRSIZE;
335
			break;
336
		case 'i':
337
			nfsargsp->flags |= NFSMNT_INT;
338
			break;
339
#ifdef NFSKERB
340
		case 'K':
341
			nfsargsp->flags |= NFSMNT_KERB;
342
			break;
343
#endif
344
		case 'L':
345
			num = strtol(optarg, &p, 10);
346
			if (*p || num < 2)
347
				errx(1, "illegal -L value -- %s", optarg);
348
#if 0
349
			nfsargsp->leaseterm = num;
350
			nfsargsp->flags |= NFSMNT_LEASETERM;
351
#endif
352
			break;
353
		case 'l':
354
			nfsargsp->flags |= NFSMNT_RDIRPLUS;
355
			break;
356
#ifdef NFSKERB
357
		case 'm':
358
			(void)strncpy(realm, optarg, REALM_SZ - 1);
359
			realm[REALM_SZ - 1] = '\0';
360
			break;
361
#endif
362
		case 'N':
363
			nfsargsp->flags &= ~NFSMNT_RESVPORT;
364
			break;
365
		case 'o':
366
			altflags = 0;
367
			setflags(&altflags, &nfsargsp->flags, TRUE);
368
			if (mountmode == V2)
369
				altflags |= ALTF_NFSV2;
370
			else if (mountmode == V3)
371
				altflags |= ALTF_NFSV3;
372
			getmntopts(optarg, mopts, &mntflags, &altflags);
373
			setflags(&altflags, &nfsargsp->flags, FALSE);
374
			/*
375
			 * Handle altflags which don't map directly to
376
			 * mount flags.
377
			 */
378
			if(altflags & ALTF_BG)
379
				opflags |= BGRND;
380
			if(altflags & ALTF_MNTUDP)
381
				mnttcp_ok = 0;
382
#ifdef ISO
383
			if(altflags & ALTF_SEQPACKET)
384
				nfsargsp->sotype = SOCK_SEQPACKET;
385
#endif
386
			if(altflags & ALTF_TCP) {
387
				nfsargsp->sotype = SOCK_STREAM;
388
				nfsproto = IPPROTO_TCP;
389
			}
390
			if(altflags & ALTF_PORT)
391
				port_no = atoi(strstr(optarg, "port=") + 5);
392
			mountmode = ANY;
393
			if(altflags & ALTF_NFSV2)
394
				mountmode = V2;
395
			if(altflags & ALTF_NFSV3)
396
				mountmode = V3;
397
#if 0
398
			if(altflags & ALTF_ACREGMIN)
399
				nfsargsp->acregmin = atoi(strstr(optarg,
400
				    "acregmin=") + 9);
401
			if(altflags & ALTF_ACREGMAX)
402
				nfsargsp->acregmax = atoi(strstr(optarg,
403
				    "acregmax=") + 9);
404
			if(altflags & ALTF_ACDIRMIN)
405
				nfsargsp->acdirmin = atoi(strstr(optarg,
406
				    "acdirmin=") + 9);
407
			if(altflags & ALTF_ACDIRMAX)
408
				nfsargsp->acdirmax = atoi(strstr(optarg,
409
				    "acdirmax=") + 9);
410
#endif
411
			break;
412
		case 'P':
413
			/* obsolete for NFSMNT_RESVPORT, now default */
414
			break;
415
#ifdef ISO
416
		case 'p':
417
			nfsargsp->sotype = SOCK_SEQPACKET;
418
			break;
419
#endif
420
		case 'q':
421
			mountmode = V3;
422
			nfsargsp->flags |= NFSMNT_NQNFS;
423
			break;
424
		case 'R':
425
			num = strtol(optarg, &p, 10);
426
			if (*p || num <= 0)
427
				errx(1, "illegal -R value -- %s", optarg);
428
			retrycnt = num;
429
			break;
430
		case 'r':
431
			num = strtol(optarg, &p, 10);
432
			if (*p || num <= 0)
433
				errx(1, "illegal -r value -- %s", optarg);
434
			nfsargsp->rsize = num;
435
			nfsargsp->flags |= NFSMNT_RSIZE;
436
			break;
437
		case 's':
438
			nfsargsp->flags |= NFSMNT_SOFT;
439
			break;
440
		case 'T':
441
			nfsargsp->sotype = SOCK_STREAM;
442
			nfsproto = IPPROTO_TCP;
443
			break;
444
		case 't':
445
			num = strtol(optarg, &p, 10);
446
			if (*p || num <= 0)
447
				errx(1, "illegal -t value -- %s", optarg);
448
#if 0
449
			nfsargsp->timeo = num;
450
			nfsargsp->flags |= NFSMNT_TIMEO;
451
#endif
452
			break;
453
		case 'w':
454
			num = strtol(optarg, &p, 10);
455
			if (*p || num <= 0)
456
				errx(1, "illegal -w value -- %s", optarg);
457
			nfsargsp->wsize = num;
458
			nfsargsp->flags |= NFSMNT_WSIZE;
459
			break;
460
		case 'x':
461
			num = strtol(optarg, &p, 10);
462
			if (*p || num <= 0)
463
				errx(1, "illegal -x value -- %s", optarg);
464
#if 0
465
			nfsargsp->retrans = num;
466
			nfsargsp->flags |= NFSMNT_RETRANS;
467
#endif
468
			break;
469
		case 'U':
470
			mnttcp_ok = 0;
471
			break;
472
		case 'u':
473
		        if( (p = index(optarg, '@')) ) {
474
			  *p++ = 0;
475
			  strncpy(nfsargsp->uname, optarg, U9FS_NAMELEN);
476
			  gethostaddr(p, & authaddr);
477
			  authaddr.sin_family = AF_INET;
478
			  authaddr.sin_port = htons(U9AUTH_ILPORT);
479
			  nfsargsp->authaddr = (struct sockaddr *) & authaddr;
480
			  nfsargsp->authaddrlen = sizeof(authaddr);
481
			} else
482
			  strncpy(nfsargsp->uname, optarg, U9FS_NAMELEN);
483
 		        break;
484
		default:
485
			usage();
486
			break;
487
		}
488
	argc -= optind;
489
	argv += optind;
490
 
491
	if (argc != 2) {
492
		usage();
493
		/* NOTREACHED */
494
	}
495
 
496
	spec = *argv++;
497
	name = *argv;
498
 
499
	if (!getnfsargs(spec, nfsargsp))
500
		exit(1);
501
 
502
#ifdef __FreeBSD__
503
	error = getvfsbyname("u9fs", &vfc);
504
	if (error && vfsisloadable("nfs")) {
505
		if(vfsload("nfs"))
506
			err(EX_OSERR, "vfsload(nfs)");
507
		endvfsent();	/* clear cache */
508
		error = getvfsbyname("nfs", &vfc);
509
	}
510
	if (error)
511
		errx(EX_OSERR, "nfs filesystem is not available");
512
 
513
	if (mount(vfc.vfc_name, name, mntflags, nfsargsp))
514
		err(1, "%s", name);
515
#else
516
	if (mount("nfs", name, mntflags, nfsargsp))
517
		err(1, "%s", name);
518
#endif
519
	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
520
		if ((opflags & ISBGRND) == 0) {
521
			if ((i = fork())) {
522
				if (i == -1)
523
					err(1, "nqnfs 1");
524
				exit(0);
525
			}
526
			(void) setsid();
527
			(void) close(STDIN_FILENO);
528
			(void) close(STDOUT_FILENO);
529
			(void) close(STDERR_FILENO);
530
			(void) chdir("/");
531
		}
532
		openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
533
		nfssvc_flag = NFSSVC_MNTD;
534
		ncd.ncd_dirp = name;
535
		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
536
			if (errno != ENEEDAUTH) {
537
				syslog(LOG_ERR, "nfssvc err %m");
538
				continue;
539
			}
540
			nfssvc_flag =
541
			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
542
#ifdef NFSKERB
543
			/*
544
			 * Set up as ncd_authuid for the kerberos call.
545
			 * Must set ruid to ncd_authuid and reset the
546
			 * ticket name iff ncd_authuid is not the same
547
			 * as last time, so that the right ticket file
548
			 * is found.
549
			 * Get the Kerberos credential structure so that
550
			 * we have the session key and get a ticket for
551
			 * this uid.
552
			 * For more info see the IETF Draft "Authentication
553
			 * in ONC RPC".
554
			 */
555
			if (ncd.ncd_authuid != last_ruid) {
556
				char buf[512];
557
				(void)sprintf(buf, "%s%d",
558
					      TKT_ROOT, ncd.ncd_authuid);
559
				krb_set_tkt_string(buf);
560
				last_ruid = ncd.ncd_authuid;
561
			}
562
			setreuid(ncd.ncd_authuid, 0);
563
			kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
564
			if (kret == RET_NOTKT) {
565
		            kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
566
				DEFAULT_TKT_LIFE);
567
			    if (kret == KSUCCESS)
568
				kret = krb_get_cred(NFS_KERBSRV, inst, realm,
569
				    &kcr);
570
			}
571
			if (kret == KSUCCESS)
572
			    kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
573
				realm, 0);
574
 
575
			/*
576
			 * Fill in the AKN_FULLNAME authenticator and verifier.
577
			 * Along with the Kerberos ticket, we need to build
578
			 * the timestamp verifier and encrypt it in CBC mode.
579
			 */
580
			if (kret == KSUCCESS &&
581
			    ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
582
			    && gettimeofday(&ktv, (struct timezone *)0) == 0) {
583
			    ncd.ncd_authtype = RPCAUTH_KERB4;
584
			    ncd.ncd_authstr = (u_char *)&ktick;
585
			    ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
586
				3 * NFSX_UNSIGNED;
587
			    ncd.ncd_verfstr = (u_char *)&kverf;
588
			    ncd.ncd_verflen = sizeof (kverf);
589
			    memmove(ncd.ncd_key, kcr.session,
590
				sizeof (kcr.session));
591
			    kin.t1 = htonl(ktv.tv_sec);
592
			    kin.t2 = htonl(ktv.tv_usec);
593
			    kin.w1 = htonl(NFS_KERBTTL);
594
			    kin.w2 = htonl(NFS_KERBTTL - 1);
595
			    bzero((caddr_t)kivec, sizeof (kivec));
596
 
597
			    /*
598
			     * Encrypt kin in CBC mode using the session
599
			     * key in kcr.
600
			     */
601
			    XXX
602
 
603
			    /*
604
			     * Finally, fill the timestamp verifier into the
605
			     * authenticator and verifier.
606
			     */
607
			    ktick.kind = htonl(RPCAKN_FULLNAME);
608
			    kverf.kind = htonl(RPCAKN_FULLNAME);
609
			    NFS_KERBW1(ktick.kt) = kout.w1;
610
			    ktick.kt.length = htonl(ktick.kt.length);
611
			    kverf.verf.t1 = kout.t1;
612
			    kverf.verf.t2 = kout.t2;
613
			    kverf.verf.w2 = kout.w2;
614
			    nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
615
			}
616
			setreuid(0, 0);
617
#endif /* NFSKERB */
618
		}
619
	}
620
	exit(0);
621
}
622
 
623
/*
624
 * Return RPC_SUCCESS if server responds.
625
 */
626
enum clnt_stat
627
pingnfsserver(addr, version, sotype)
628
	struct sockaddr_in *addr;
629
	int version;
630
	int sotype;
631
{
632
	struct sockaddr_in sin;
633
	int tport;
634
	CLIENT *clp;
635
	int so = RPC_ANYSOCK;
636
	enum clnt_stat stat;
637
	struct timeval pertry, try;
638
 
639
	sin = *addr;
640
 
641
	if ((tport = port_no ? port_no :
642
	     pmap_getport(&sin, RPCPROG_NFS, version, nfsproto)) == 0) {
643
		return rpc_createerr.cf_stat;
644
	}
645
 
646
	sin.sin_port = htons(tport);
647
 
648
	pertry.tv_sec = 10;
649
	pertry.tv_usec = 0;
650
	if (sotype == SOCK_STREAM)
651
		clp = clnttcp_create(&sin, RPCPROG_NFS, version,
652
				     &so, 0, 0);
653
	else
654
		clp = clntudp_create(&sin, RPCPROG_NFS, version,
655
				     pertry, &so);
656
	if (clp == NULL)
657
		return rpc_createerr.cf_stat;
658
 
659
	try.tv_sec = 10;
660
	try.tv_usec = 0;
661
	stat = clnt_call(clp, NFSPROC_NULL,
662
			 xdr_void, NULL, xdr_void, NULL, try);
663
 
664
	clnt_destroy(clp);
665
 
666
	return stat;
667
}
668
 
669
int load_9uid(struct u9fs_args * nfsargsp)
670
{
671
  FILE * fd;
672
  char line[80], * cp;
673
  int nusers, siz, n;
674
  struct p9user * p9p, * p9alloc;
675
 
676
  if( (fd = fopen("/etc/9uid.conf", "r")) == 0 ) 
677
    errx(1, "fopen");
678
 
679
  siz = 128;
680
  if( (p9alloc = malloc(siz*sizeof(struct p9user))) == 0 )
681
    errx(1, "malloc");
682
 
683
  nusers = 0;
684
  p9p = p9alloc;
685
  while(1) {    
686
    if( nusers < siz ) {
687
      if ( fgets(line, 80, fd) == 0 )
688
	break;
689
      cp = line;
690
      if ( strsep(&cp, " \t") == 0 )
691
	errx(1, "bad format in 9uid.conf");
692
      p9p->p9_uid = atoi(cp);
693
      strncpy(p9p->p9_name, line, U9FS_NAMELEN);
694
      nusers++;
695
      p9p++;
696
    } else {
697
      if( (p9p = realloc(p9alloc, 2*siz*sizeof(struct p9user))) == 0 )
698
	errx(1, "realloc");
699
      p9alloc = p9p;
700
      p9p = p9alloc + siz;
701
      siz <<= 1;
702
    }
703
  }
704
 
705
  nfsargsp->nusers = nusers;
706
  nfsargsp->users = p9alloc;
707
 
708
  return 0;
709
}
710
 
711
int
712
passtokey(char *key, char *p)
713
{
714
	u_char buf[U9FS_NAMELEN], *t;
715
	int i, n;
716
 
717
	n = strlen(p);
718
	if(n >= U9FS_NAMELEN)
719
		n = U9FS_NAMELEN-1;
720
	memset(buf, ' ', 8);
721
	t = buf;
722
	strncpy((char*)t, p, n);
723
	t[n] = '\0';
724
	memset(key, 0, U9AUTH_DESKEYLEN);
725
	for(;;){
726
		for(i = 0; i < U9AUTH_DESKEYLEN; i++)
727
			key[i] = (t[i] >> i) + (t[i+1] << (8 - (i+1)));
728
		if(n <= 8)
729
			return 1;
730
		n -= 8;
731
		t += 8;
732
		if(n < 8){
733
			t -= 8 - n;
734
			n = 8;
735
		}
736
		encrypt9(key, t, 8);
737
	}
738
	return 1;	/* not reached */
739
}
740
 
741
void load_9key(struct u9fs_args * nfsargsp)
742
{
743
  char * p;
744
 
745
  p = getpass("Plan 9 Password: ");
746
  passtokey(nfsargsp->key, p);
747
}
748
 
749
void gethostaddr(char * hostp, struct sockaddr_in * saddr)
750
{
751
  struct hostent *hp;
752
	/*
753
	 * Handle an internet host address and reverse resolve it if
754
	 * doing Kerberos.
755
	 */
756
	if (isdigit(*hostp)) {
757
		if ((saddr->sin_addr.s_addr = inet_addr(hostp)) == -1) {
758
			warnx("bad net address %s", hostp);
759
		}
760
	} else if ((hp = gethostbyname(hostp)) != NULL)
761
		memmove(&saddr->sin_addr, hp->h_addr, 
762
		    MIN(hp->h_length, sizeof(saddr->sin_addr)));
763
	else {
764
		warnx("can't get net id for host");
765
        }
766
}
767
 
768
int
769
getnfsargs(spec, nfsargsp)
770
	char *spec;
771
	struct u9fs_args *nfsargsp;
772
{
773
	register CLIENT *clp;
774
	struct hostent *hp;
775
	static struct sockaddr_in saddr;
776
#ifdef ISO
777
	static struct sockaddr_iso isoaddr;
778
	struct iso_addr *isop;
779
	int isoflag = 0;
780
#endif
781
	struct timeval pertry, try;
782
	enum clnt_stat clnt_stat;
783
	int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt;
784
	char *hostp, *delimp;
785
#ifdef NFSKERB
786
	char *cp;
787
#endif
788
	u_short tport;
789
	static struct nfhret nfhret;
790
	static char nam[MNAMELEN + 1];
791
 
792
	strncpy(nam, spec, MNAMELEN);
793
	nam[MNAMELEN] = '\0';
794
	if ((delimp = strchr(spec, '@')) != NULL) {
795
		hostp = delimp + 1;
796
	} else if ((delimp = strchr(spec, ':')) != NULL) {
797
		hostp = spec;
798
		spec = delimp + 1;
799
	} else {
800
		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
801
		return (0);
802
	}
803
	*delimp = '\0';
804
	/*
805
	 * DUMB!! Until the mount protocol works on iso transport, we must
806
	 * supply both an iso and an inet address for the host.
807
	 */
808
#ifdef ISO
809
	if (!strncmp(hostp, "iso=", 4)) {
810
		u_short isoport;
811
 
812
		hostp += 4;
813
		isoflag++;
814
		if ((delimp = strchr(hostp, '+')) == NULL) {
815
			warnx("no iso+inet address");
816
			return (0);
817
		}
818
		*delimp = '\0';
819
		if ((isop = iso_addr(hostp)) == NULL) {
820
			warnx("bad ISO address");
821
			return (0);
822
		}
823
		memset(&isoaddr, 0, sizeof (isoaddr));
824
		memmove(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
825
		isoaddr.siso_len = sizeof (isoaddr);
826
		isoaddr.siso_family = AF_ISO;
827
		isoaddr.siso_tlen = 2;
828
		isoport = htons(NFS_PORT);
829
		memmove(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
830
		hostp = delimp + 1;
831
	}
832
#endif /* ISO */
833
 
834
	gethostaddr(hostp, & saddr);
835
#ifdef NFSKERB
836
	if ((nfsargsp->flags & NFSMNT_KERB)) {
837
		if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
838
		    sizeof (u_long), AF_INET)) == (struct hostent *)0) {
839
			warnx("can't reverse resolve net address");
840
			return (0);
841
		}
842
		memmove(&saddr.sin_addr, hp->h_addr, 
843
		    MIN(hp->h_length, sizeof(saddr.sin_addr)));
844
		strncpy(inst, hp->h_name, INST_SZ);
845
		inst[INST_SZ - 1] = '\0';
846
		if (cp = strchr(inst, '.'))
847
			*cp = '\0';
848
	}
849
#endif /* NFSKERB */
850
 
851
	orgcnt = retrycnt;
852
tryagain:
853
	if (mountmode == ANY || mountmode == V3) {
854
		nfsvers = 3;
855
		mntvers = 3;
856
		nfsargsp->flags |= NFSMNT_NFSV3;
857
	} else {
858
		nfsvers = 2;
859
		mntvers = 1;
860
		nfsargsp->flags &= ~NFSMNT_NFSV3;
861
	}
862
	tport = port_no ? port_no : U9FS_PORT;
863
 
864
#if 0
865
	nfhret.stat = EACCES;	/* Mark not yet successful */
866
	while (retrycnt > 0) {
867
		saddr.sin_family = AF_INET;
868
		saddr.sin_port = htons(PMAPPORT);
869
		if ((tport = port_no ? port_no :
870
		     pmap_getport(&saddr, RPCPROG_NFS,
871
		    		  nfsvers, nfsproto)) == 0) {
872
			if ((opflags & ISBGRND) == 0)
873
				clnt_pcreateerror("NFS Portmap");
874
		} else {
875
			/*
876
			 * First ping the nfs server to see if it supports
877
			 * the version of the protocol we want to use.
878
			 */
879
			clnt_stat = pingnfsserver(&saddr, nfsvers,
880
						  nfsargsp->sotype);
881
			if (clnt_stat == RPC_PROGVERSMISMATCH) {
882
				if (mountmode == ANY) {
883
					mountmode = V2;
884
					goto tryagain;
885
				} else {
886
					errx(1, "can't contact NFS server");
887
				}
888
			}
889
			saddr.sin_port = 0;
890
			pertry.tv_sec = 10;
891
			pertry.tv_usec = 0;
892
			if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
893
			    clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
894
				&so, 0, 0);
895
			else
896
			    clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
897
				pertry, &so);
898
			if (clp == NULL) {
899
				if ((opflags & ISBGRND) == 0)
900
					clnt_pcreateerror("Cannot MNT RPC");
901
			} else {
902
				clp->cl_auth = authunix_create_default();
903
				try.tv_sec = 10;
904
				try.tv_usec = 0;
905
				if (nfsargsp->flags & NFSMNT_KERB)
906
				    nfhret.auth = RPCAUTH_KERB4;
907
				else
908
				    nfhret.auth = RPCAUTH_UNIX;
909
				nfhret.vers = mntvers;
910
				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
911
				    xdr_dir, spec, xdr_fh, &nfhret, try);
912
				if (clnt_stat != RPC_SUCCESS) {
913
					if (clnt_stat == RPC_PROGVERSMISMATCH) {
914
						if (mountmode == ANY) {
915
							mountmode = V2;
916
							goto tryagain;
917
						} else {
918
							errx(1, "%s",
919
							     clnt_sperror(clp, "MNT RPC"));
920
						}
921
					}
922
					if ((opflags & ISBGRND) == 0)
923
						warnx("%s", clnt_sperror(clp,
924
						    "bad MNT RPC"));
925
				} else {
926
					auth_destroy(clp->cl_auth);
927
					clnt_destroy(clp);
928
					retrycnt = 0;
929
				}
930
			}
931
		}
932
		if (--retrycnt > 0) {
933
			if (opflags & BGRND) {
934
				opflags &= ~BGRND;
935
				if ((i = fork())) {
936
					if (i == -1)
937
						err(1, "nqnfs 2");
938
					exit(0);
939
				}
940
				(void) setsid();
941
				(void) close(STDIN_FILENO);
942
				(void) close(STDOUT_FILENO);
943
				(void) close(STDERR_FILENO);
944
				(void) chdir("/");
945
				opflags |= ISBGRND;
946
			}
947
			sleep(60);
948
		}
949
	}
950
	if (nfhret.stat) {
951
		if (opflags & ISBGRND)
952
			exit(1);
953
		warnx("can't access %s: %s", spec, strerror(nfhret.stat));
954
		return (0);
955
	}
956
#endif
957
	saddr.sin_family = AF_INET;
958
	saddr.sin_port = htons(tport);
959
#ifdef ISO
960
	if (isoflag) {
961
		nfsargsp->addr = (struct sockaddr *) &isoaddr;
962
		nfsargsp->addrlen = sizeof (isoaddr);
963
	} else
964
#endif /* ISO */
965
	{
966
		nfsargsp->addr = (struct sockaddr *) &saddr;
967
		nfsargsp->addrlen = sizeof (saddr);
968
	}
969
#if 0
970
	nfsargsp->fh = nfhret.nfh;
971
#endif
972
	nfsargsp->fhsize = nfhret.fhsize;
973
	nfsargsp->hostname = nam;
974
 
975
	load_9key(nfsargsp);
976
	if( load_9uid(nfsargsp) )
977
	  errx(1, "can't load 9uid.conf");
978
 
979
	return (1);
980
}
981
 
982
/*
983
 * xdr routines for mount rpc's
984
 */
985
int
986
xdr_dir(xdrsp, dirp)
987
	XDR *xdrsp;
988
	char *dirp;
989
{
990
	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
991
}
992
 
993
int
994
xdr_fh(xdrsp, np)
995
	XDR *xdrsp;
996
	register struct nfhret *np;
997
{
998
	register int i;
999
	long auth, authcnt, authfnd = 0;
1000
 
1001
	if (!xdr_u_long(xdrsp, &np->stat))
1002
		return (0);
1003
	if (np->stat)
1004
		return (1);
1005
	switch (np->vers) {
1006
	case 1:
1007
		np->fhsize = NFSX_V2FH;
1008
		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
1009
	case 3:
1010
		if (!xdr_long(xdrsp, &np->fhsize))
1011
			return (0);
1012
		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
1013
			return (0);
1014
		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1015
			return (0);
1016
		if (!xdr_long(xdrsp, &authcnt))
1017
			return (0);
1018
		for (i = 0; i < authcnt; i++) {
1019
			if (!xdr_long(xdrsp, &auth))
1020
				return (0);
1021
			if (auth == np->auth)
1022
				authfnd++;
1023
		}
1024
		/*
1025
		 * Some servers, such as DEC's OSF/1 return a nil authenticator
1026
		 * list to indicate RPCAUTH_UNIX.
1027
		 */
1028
		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
1029
			np->stat = EAUTH;
1030
		return (1);
1031
	};
1032
	return (0);
1033
}
1034
 
1035
void
1036
usage()
1037
{
1038
	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1039
"usage: mount_nfs [-23KNPTUbcdilqs] [-D deadthresh] [-I readdirsize]",
1040
"                 [-L leaseterm] [-R retrycnt] [-a maxreadahead]",
1041
"                 [-g maxgroups] [-m realm] [-o options] [-r readsize]",
1042
"                 [-t timeout] [-w writesize] [-x retrans] rhost:path node");
1043
	exit(1);
1044
}
1045