summaryrefslogtreecommitdiff
path: root/sys/src/cmd/exportfs/oexportfs.c
blob: e9fa819c7fa32c5e0acf389dccc842f933f3fd1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * oexportfs - legacy exportfs for cpu and import
 */
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include <libsec.h>
#define Extern
#include "exportfs.h"

enum {
	Encnone,
	Encssl,
	Enctls,
};

int	srvfd = -1;
int	nonone = 1;
char	*filterp;
char	*ealgs = "rc4_256 sha1";
char	*aanfilter = "/bin/aan";
int	encproto = Encnone;
int	readonly;

static char *anstring  = "tcp!*!0";

static void
filter(int fd, char *cmd, char *host)
{
	char addr[128], buf[256], *s, *file, *argv[16];
	int lfd, p[2], len, argc;

	if(host == nil){
		/* Get a free port and post it to the client. */
		if (announce(anstring, addr) < 0)
			fatal("filter: Cannot announce %s: %r", anstring);

		snprint(buf, sizeof(buf), "%s/local", addr);
		if ((lfd = open(buf, OREAD)) < 0)
			fatal("filter: Cannot open %s: %r", buf);
		if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
			fatal("filter: Cannot read %s: %r", buf);
		close(lfd);
		buf[len] = '\0';
		if ((s = strchr(buf, '\n')) != nil)
			len = s - buf;
		if (write(fd, buf, len) != len) 
			fatal("filter: cannot write port; %r");
	} else {
		/* Read address string from connection */
		if ((len = read(fd, buf, sizeof buf - 1)) < 0)
			sysfatal("filter: cannot write port; %r");
		buf[len] = '\0';

		if ((s = strrchr(buf, '!')) == nil)
			sysfatal("filter: illegally formatted port %s", buf);
		strecpy(addr, addr+sizeof(addr), netmkaddr(host, "tcp", s+1));
		strecpy(strrchr(addr, '!'), addr+sizeof(addr), s);
	}

	DEBUG(2, "filter: %s\n", addr);

	snprint(buf, sizeof(buf), "%s", cmd);
	argc = tokenize(buf, argv, nelem(argv)-3);
	if (argc == 0)
		sysfatal("filter: empty command");

	if(host != nil)
		argv[argc++] = "-c";
	argv[argc++] = addr;
	argv[argc] = nil;

	file = argv[0];
	if((s = strrchr(argv[0], '/')) != nil)
		argv[0] = s+1;

	if(pipe(p) < 0)
		sysfatal("pipe: %r");

	switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
	case -1:
		fatal("filter: rfork; %r\n");
	case 0:
		close(fd);
		if (dup(p[0], 1) < 0)
			fatal("filter: Cannot dup to 1; %r");
		if (dup(p[0], 0) < 0)
			fatal("filter: Cannot dup to 0; %r");
		close(p[0]);
		close(p[1]);
		exec(file, argv);
		fatal("filter: exec; %r");
	default:
		dup(p[1], fd);
		close(p[0]);
		close(p[1]);
	}
}

static void
mksecret(char *t, uchar *f)
{
	sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
		f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
}

void
usage(void)
{
	fprint(2, "usage: %s [-adnsR] [-f dbgfile] [-m msize] [-r root] "
		"[-S srvfile] [-e 'crypt hash'] [-P exclusion-file] "
		"[-A announce-string] [-B address]\n", argv0);
	fatal("usage");
}

void
main(int argc, char **argv)
{
	char buf[ERRMAX], ebuf[ERRMAX], initial[4], *ini, *srvfdfile;
	char *dbfile, *srv, *na, *nsfile, *keyspec;
	int doauth, n, fd;
	AuthInfo *ai;
	Fsrpc *r;

	dbfile = "/tmp/exportdb";
	srv = nil;
	srvfd = -1;
	srvfdfile = nil;
	na = nil;
	nsfile = nil;
	keyspec = "";
	doauth = 0;

	ai = nil;
	ARGBEGIN{
	case 'a':
		doauth = 1;
		break;

	case 'd':
		dbg++;
		break;

	case 'e':
		ealgs = EARGF(usage());
		if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
			ealgs = nil;
		break;

	case 'f':
		dbfile = EARGF(usage());
		break;

	case 'k':
		keyspec = EARGF(usage());
		break;

	case 'm':
		messagesize = strtoul(EARGF(usage()), nil, 0);
		break;

	case 'n':
		nonone = 0;
		break;

	case 'r':
		srv = EARGF(usage());
		break;

	case 's':
		srv = "/";
		break;

	case 'A':
		anstring = EARGF(usage());
		break;

	case 'B':
		na = EARGF(usage());
		break;

	case 'F':
		/* accepted but ignored, for backwards compatibility */
		break;

	case 'N':
		nsfile = EARGF(usage());
		break;

	case 'P':
		patternfile = EARGF(usage());
		break;

	case 'R':
		readonly = 1;
		break;

	case 'S':
		if(srvfdfile != nil)
			usage();
		srvfdfile = EARGF(usage());
		break;

	default:
		usage();
	}ARGEND
	USED(argc, argv);

	if(na == nil && doauth){
		/*
		 * We use p9any so we don't have to visit this code again, with the
		 * cost that this code is incompatible with the old world, which
		 * requires p9sk2. (The two differ in who talks first, so compatibility
		 * is awkward.)
		 */
		ai = auth_proxy(0, auth_getkey, "proto=p9any role=server %s", keyspec);
		if(ai == nil)
			fatal("auth_proxy: %r");
		if(nonone && strcmp(ai->cuid, "none") == 0)
			fatal("exportfs by none disallowed");
		if(auth_chuid(ai, nsfile) < 0)
			fatal("auth_chuid: %r");
		else {	/* chown network connection */
			Dir nd;
			nulldir(&nd);
			nd.mode = 0660;
			nd.uid = ai->cuid;
			dirfwstat(0, &nd);
		}
		putenv("service", "exportfs");
	}

	if(srvfdfile != nil){
		if((srvfd = open(srvfdfile, ORDWR)) < 0)
			fatal("open %s: %r", srvfdfile);
	}

	if(na != nil){
		if(srv == nil)
			fatal("-B requires -s");

		if((fd = dial(netmkaddr(na, 0, "importfs"), 0, 0, 0)) < 0)
			fatal("can't dial %s: %r", na);
	
		ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
		if(ai == nil)
			fatal("%r: %s", na);

		dup(fd, 0);
		dup(fd, 1);
		close(fd);
	}

	exclusions();

	if(dbg) {
		n = create(dbfile, OWRITE|OTRUNC, 0666);
		dup(n, 2);
		close(n);
	}

	if(srvfd >= 0 && srv != nil){
		fprint(2, "%s: -S cannot be used with -r or -s\n", argv0);
		usage();
	}

	DEBUG(2, "%s: started\n", argv0);

	rfork(RFNOTEG|RFREND);

	if(messagesize == 0){
		messagesize = iounit(0);
		if(messagesize == 0)
			messagesize = 8192+IOHDRSZ;
	}
	fhash = emallocz(sizeof(Fid*)*FHASHSIZE);

	fmtinstall('F', fcallfmt);

	/*
	 * Get tree to serve from network connection,
	 * check we can get there and ack the connection
 	 */
	if(srvfd != -1) {
		/* do nothing */
	}
	else if(srv != nil) {
		if(chdir(srv) < 0) {
			ebuf[0] = '\0';
			errstr(ebuf, sizeof ebuf);
			DEBUG(2, "chdir(\"%s\"): %s\n", srv, ebuf);
			mounterror(ebuf);
		}
		DEBUG(2, "invoked as server for %s", srv);
		strncpy(buf, srv, sizeof buf);
	}
	else {
		buf[0] = 0;
		n = read(0, buf, sizeof(buf)-1);
		if(n < 0) {
			errstr(buf, sizeof buf);
			fprint(0, "read(0): %s\n", buf);
			DEBUG(2, "read(0): %s\n", buf);
			exits(buf);
		}
		buf[n] = 0;
		if(chdir(buf) < 0) {
			errstr(ebuf, sizeof ebuf);
			fprint(0, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
			DEBUG(2, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
			exits(ebuf);
		}
	}

	DEBUG(2, "\niniting root\n");
	initroot();

	DEBUG(2, "%s: %s\n", argv0, buf);

	if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
		fatal("open ack write");

	ini = initial;
	n = readn(0, initial, sizeof(initial));
	if(n == 0)
		fatal(nil);	/* port scan or spurious open/close on exported /srv file (unmount) */
	if(n < sizeof(initial))
		fatal("can't read initial string: %r");

	if(memcmp(ini, "impo", 4) == 0) {
		char buf[128], *p, *args[3];

		ini = nil;
		p = buf;
		for(;;){
			if((n = read(0, p, 1)) < 0)
				fatal("can't read impo arguments: %r");
			if(n == 0)
				fatal("connection closed while reading arguments");
			if(*p == '\n') 
				*p = '\0';
			if(*p++ == '\0')
				break;
			if(p >= buf + sizeof(buf))
				fatal("import parameters too long");
		}
		
		if(tokenize(buf, args, nelem(args)) != 2)
			fatal("impo arguments invalid: impo%s...", buf);

		if(strcmp(args[0], "aan") == 0)
			filterp = aanfilter;
		else if(strcmp(args[0], "nofilter") != 0)
			fatal("import filter argument unsupported: %s", args[0]);

		if(strcmp(args[1], "ssl") == 0)
			encproto = Encssl;
		else if(strcmp(args[1], "tls") == 0)
			encproto = Enctls;
		else if(strcmp(args[1], "clear") != 0)
			fatal("import encryption proto unsupported: %s", args[1]);

		if(encproto == Enctls)
			fatal("%s: tls has not yet been implemented", argv[0]);
	}

	if(encproto != Encnone && ealgs != nil && ai != nil) {
		uchar key[16], digest[SHA1dlen];
		char fromclientsecret[21];
		char fromserversecret[21];
		int i;

		if(ai->nsecret < 8)
			fatal("secret too small for ssl");
		memmove(key+4, ai->secret, 8);

		/* exchange random numbers */
		srand(truerand());
		for(i = 0; i < 4; i++)
			key[i+12] = rand();

		if(ini != nil) 
			fatal("Protocol botch: old import");
		if(readn(0, key, 4) != 4)
			fatal("can't read key part; %r");

		if(write(0, key+12, 4) != 4)
			fatal("can't write key part; %r");

		/* scramble into two secrets */
		sha1(key, sizeof(key), digest, nil);
		mksecret(fromclientsecret, digest);
		mksecret(fromserversecret, digest+10);

		if(filterp != nil)
			filter(0, filterp, na);

		switch(encproto) {
		case Encssl:
			fd = pushssl(0, ealgs, fromserversecret, fromclientsecret, nil);
			if(fd < 0)
				fatal("can't establish ssl connection: %r");
			if(fd != 0){
				dup(fd, 0);
				close(fd);
			}
			break;
		case Enctls:
		default:
			fatal("Unsupported encryption protocol");
		}
	}
	else if(filterp != nil) {
		if(ini != nil)
			fatal("Protocol botch: don't know how to deal with this");
		filter(0, filterp, na);
	}
	dup(0, 1);

	if(ai != nil)
		auth_freeAI(ai);

	if(ini != nil){
		extern void (*fcalls[])(Fsrpc*);

		r = getsbuf();
		memmove(r->buf, ini, BIT32SZ);
		n = GBIT32(r->buf);
		if(n <= BIT32SZ || n > messagesize)
			fatal("bad length in 9P2000 message header");
		n -= BIT32SZ;
		if(readn(0, r->buf+BIT32SZ, n) != n)
			fatal(nil);
		n += BIT32SZ;

		if(convM2S(r->buf, n, &r->work) != n)
			fatal("convM2S format error");
		DEBUG(2, "%F\n", &r->work);
		(fcalls[r->work.type])(r);
	}
	io();
}