2 * oexportfs - legacy exportfs for cpu and import
21 char *ealgs = "rc4_256 sha1";
22 char *aanfilter = "/bin/aan";
23 int encproto = Encnone;
26 static char *anstring = "tcp!*!0";
29 filter(int fd, char *cmd, char *host)
31 char addr[128], buf[256], *s, *file, *argv[16];
32 int lfd, p[2], len, argc;
35 /* Get a free port and post it to the client. */
36 if (announce(anstring, addr) < 0)
37 fatal("filter: Cannot announce %s: %r", anstring);
39 snprint(buf, sizeof(buf), "%s/local", addr);
40 if ((lfd = open(buf, OREAD)) < 0)
41 fatal("filter: Cannot open %s: %r", buf);
42 if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
43 fatal("filter: Cannot read %s: %r", buf);
46 if ((s = strchr(buf, '\n')) != nil)
48 if (write(fd, buf, len) != len)
49 fatal("filter: cannot write port; %r");
51 /* Read address string from connection */
52 if ((len = read(fd, buf, sizeof buf - 1)) < 0)
53 sysfatal("filter: cannot write port; %r");
56 if ((s = strrchr(buf, '!')) == nil)
57 sysfatal("filter: illegally formatted port %s", buf);
58 strecpy(addr, addr+sizeof(addr), netmkaddr(host, "tcp", s+1));
59 strecpy(strrchr(addr, '!'), addr+sizeof(addr), s);
62 DEBUG(2, "filter: %s\n", addr);
64 snprint(buf, sizeof(buf), "%s", cmd);
65 argc = tokenize(buf, argv, nelem(argv)-3);
67 sysfatal("filter: empty command");
75 if((s = strrchr(argv[0], '/')) != nil)
81 switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
83 fatal("filter: rfork; %r\n");
87 fatal("filter: Cannot dup to 1; %r");
89 fatal("filter: Cannot dup to 0; %r");
93 fatal("filter: exec; %r");
102 mksecret(char *t, uchar *f)
104 sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
105 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
111 fprint(2, "usage: %s [-adnsR] [-m msize] [-r root] "
112 "[-S srvfile] [-e 'crypt hash'] [-P exclusion-file] "
113 "[-A announce-string] [-B address]\n", argv0);
118 main(int argc, char **argv)
120 char buf[ERRMAX], ebuf[ERRMAX], initial[4], *ini, *srvfdfile;
121 char *srv, *na, *nsfile, *keyspec;
145 ealgs = EARGF(usage());
146 if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
151 keyspec = EARGF(usage());
155 messagesize = strtoul(EARGF(usage()), nil, 0);
163 srv = EARGF(usage());
171 anstring = EARGF(usage());
179 /* accepted but ignored, for backwards compatibility */
183 nsfile = EARGF(usage());
187 patternfile = EARGF(usage());
197 srvfdfile = EARGF(usage());
205 if(na == nil && doauth){
207 * We use p9any so we don't have to visit this code again, with the
208 * cost that this code is incompatible with the old world, which
209 * requires p9sk2. (The two differ in who talks first, so compatibility
212 ai = auth_proxy(0, auth_getkey, "proto=p9any role=server %s", keyspec);
214 fatal("auth_proxy: %r");
215 if(nonone && strcmp(ai->cuid, "none") == 0)
216 fatal("exportfs by none disallowed");
217 if(auth_chuid(ai, nsfile) < 0)
218 fatal("auth_chuid: %r");
219 else { /* chown network connection */
226 putenv("service", "exportfs");
229 if(srvfdfile != nil){
230 if((srvfd = open(srvfdfile, ORDWR)) < 0)
231 fatal("open %s: %r", srvfdfile);
236 fatal("-B requires -s");
238 if((fd = dial(netmkaddr(na, 0, "importfs"), 0, 0, 0)) < 0)
239 fatal("can't dial %s: %r", na);
241 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
252 if(srvfd >= 0 && srv != nil){
253 fprint(2, "%s: -S cannot be used with -r or -s\n", argv0);
257 DEBUG(2, "%s: started\n", argv0);
259 rfork(RFNOTEG|RFREND);
261 if(messagesize == 0){
262 messagesize = iounit(0);
264 messagesize = 8192+IOHDRSZ;
266 fhash = emallocz(sizeof(Fid*)*FHASHSIZE);
268 fmtinstall('F', fcallfmt);
271 * Get tree to serve from network connection,
272 * check we can get there and ack the connection
277 else if(srv != nil) {
280 errstr(ebuf, sizeof ebuf);
281 DEBUG(2, "chdir(\"%s\"): %s\n", srv, ebuf);
284 DEBUG(2, "invoked as server for %s", srv);
285 strncpy(buf, srv, sizeof buf);
289 n = read(0, buf, sizeof(buf)-1);
291 errstr(buf, sizeof buf);
292 fprint(0, "read(0): %s\n", buf);
293 DEBUG(2, "read(0): %s\n", buf);
298 errstr(ebuf, sizeof ebuf);
299 fprint(0, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
300 DEBUG(2, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
305 DEBUG(2, "\niniting root\n");
308 DEBUG(2, "%s: %s\n", argv0, buf);
310 if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
311 fatal("open ack write");
314 n = readn(0, initial, sizeof(initial));
316 fatal(nil); /* port scan or spurious open/close on exported /srv file (unmount) */
317 if(n < sizeof(initial))
318 fatal("can't read initial string: %r");
320 if(memcmp(ini, "impo", 4) == 0) {
321 char buf[128], *p, *args[3];
326 if((n = read(0, p, 1)) < 0)
327 fatal("can't read impo arguments: %r");
329 fatal("connection closed while reading arguments");
334 if(p >= buf + sizeof(buf))
335 fatal("import parameters too long");
338 if(tokenize(buf, args, nelem(args)) != 2)
339 fatal("impo arguments invalid: impo%s...", buf);
341 if(strcmp(args[0], "aan") == 0)
343 else if(strcmp(args[0], "nofilter") != 0)
344 fatal("import filter argument unsupported: %s", args[0]);
346 if(strcmp(args[1], "ssl") == 0)
348 else if(strcmp(args[1], "tls") == 0)
350 else if(strcmp(args[1], "clear") != 0)
351 fatal("import encryption proto unsupported: %s", args[1]);
353 if(encproto == Enctls)
354 fatal("%s: tls has not yet been implemented", argv[0]);
357 if(encproto != Encnone && ealgs != nil && ai != nil) {
358 uchar key[16], digest[SHA1dlen];
359 char fromclientsecret[21];
360 char fromserversecret[21];
364 fatal("secret too small for ssl");
365 memmove(key+4, ai->secret, 8);
367 /* exchange random numbers */
369 for(i = 0; i < 4; i++)
373 fatal("Protocol botch: old import");
374 if(readn(0, key, 4) != 4)
375 fatal("can't read key part; %r");
377 if(write(0, key+12, 4) != 4)
378 fatal("can't write key part; %r");
380 /* scramble into two secrets */
381 sha1(key, sizeof(key), digest, nil);
382 mksecret(fromclientsecret, digest);
383 mksecret(fromserversecret, digest+10);
386 filter(0, filterp, na);
390 fd = pushssl(0, ealgs, fromserversecret, fromclientsecret, nil);
392 fatal("can't establish ssl connection: %r");
400 fatal("Unsupported encryption protocol");
403 else if(filterp != nil) {
405 fatal("Protocol botch: don't know how to deal with this");
406 filter(0, filterp, na);
414 extern void (*fcalls[])(Fsrpc*);
417 memmove(r->buf, ini, BIT32SZ);
419 if(n <= BIT32SZ || n > messagesize)
420 fatal("bad length in 9P2000 message header");
422 if(readn(0, r->buf+BIT32SZ, n) != n)
426 if(convM2S(r->buf, n, &r->work) != n)
427 fatal("convM2S format error");
428 DEBUG(2, "%F\n", &r->work);
429 (fcalls[r->work.type])(r);