xref: /plan9/sys/src/cmd/exportfs/exportfs.c (revision d6adc60ea17984a31bfad70ccf174dedf45cbee8)
1 /*
2  * exportfs - Export a plan 9 name space across a network
3  */
4 #include <u.h>
5 #include <libc.h>
6 #include <auth.h>
7 #include <fcall.h>
8 #include <libsec.h>
9 #define Extern
10 #include "exportfs.h"
11 
12 #define QIDPATH	((1LL<<48)-1)
13 vlong newqid = 0;
14 
15 enum {
16 	Encnone,
17 	Encssl,
18 	Enctls,
19 };
20 
21 void (*fcalls[])(Fsrpc*) =
22 {
23 	[Tversion]	Xversion,
24 	[Tauth]	Xauth,
25 	[Tflush]	Xflush,
26 	[Tattach]	Xattach,
27 	[Twalk]		Xwalk,
28 	[Topen]		slave,
29 	[Tcreate]	Xcreate,
30 	[Tclunk]	Xclunk,
31 	[Tread]		slave,
32 	[Twrite]	slave,
33 	[Tremove]	Xremove,
34 	[Tstat]		Xstat,
35 	[Twstat]	Xwstat,
36 };
37 
38 /* accounting and debugging counters */
39 int	filecnt;
40 int	freecnt;
41 int	qidcnt;
42 int	qfreecnt;
43 int	ncollision;
44 
45 int	netfd;
46 int	srvfd = -1;
47 int	nonone = 1;
48 char	*filterp;
49 char	*ealgs = "rc4_256 sha1";
50 char	*aanfilter = "/bin/aan";
51 int	encproto = Encnone;
52 int	readonly;
53 
54 static void	mksecret(char *, uchar *);
55 static int localread9pmsg(int, void *, uint, ulong *);
56 static char *anstring  = "tcp!*!0";
57 int	filter(int, char *);
58 
59 void
60 usage(void)
61 {
62 	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);
63 	fatal("usage");
64 }
65 
66 void
67 main(int argc, char **argv)
68 {
69 	char buf[ERRMAX], ebuf[ERRMAX], *srvfdfile;
70 	Fsrpc *r;
71 	int doauth, n, fd;
72 	char *dbfile, *srv, *na, *nsfile, *keyspec;
73 	AuthInfo *ai;
74 	ulong initial;
75 
76 	dbfile = "/tmp/exportdb";
77 	srv = nil;
78 	srvfd = -1;
79 	srvfdfile = nil;
80 	na = nil;
81 	nsfile = nil;
82 	keyspec = "";
83 	doauth = 0;
84 
85 	ai = nil;
86 	ARGBEGIN{
87 	case 'a':
88 		doauth = 1;
89 		break;
90 
91 	case 'k':
92 		keyspec = EARGF(usage());
93 		break;
94 
95 	case 'e':
96 		ealgs = EARGF(usage());
97 		if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
98 			ealgs = nil;
99 		break;
100 
101 	case 'S':
102 		if(srvfdfile)
103 			usage();
104 		srvfdfile = EARGF(usage());
105 		break;
106 
107 	case 'd':
108 		dbg++;
109 		break;
110 
111 	case 'f':
112 		dbfile = EARGF(usage());
113 		break;
114 
115 	case 'F':
116 		/* accepted but ignored, for backwards compatibility */
117 		break;
118 
119 	case 'm':
120 		messagesize = strtoul(EARGF(usage()), nil, 0);
121 		break;
122 
123 	case 'n':
124 		nonone = 0;
125 		break;
126 
127 	case 'N':
128 		nsfile = EARGF(usage());
129 		break;
130 
131 	case 'r':
132 		srv = EARGF(usage());
133 		break;
134 
135 	case 's':
136 		srv = "/";
137 		break;
138 
139 	case 'P':
140 		patternfile = EARGF(usage());
141 		break;
142 
143 	case 'A':
144 		anstring = EARGF(usage());
145 		break;
146 
147 	case 'R':
148 		readonly = 1;
149 		break;
150 
151 	case 'B':
152 		na = EARGF(usage());
153 		break;
154 
155 	default:
156 		usage();
157 	}ARGEND
158 	USED(argc, argv);
159 
160 	if(doauth){
161 		/*
162 		 * We use p9any so we don't have to visit this code again, with the
163 		 * cost that this code is incompatible with the old world, which
164 		 * requires p9sk2. (The two differ in who talks first, so compatibility
165 		 * is awkward.)
166 		 */
167 		ai = auth_proxy(0, auth_getkey, "proto=p9any role=server %s", keyspec);
168 		if(ai == nil)
169 			fatal("auth_proxy: %r");
170 		if(nonone && strcmp(ai->cuid, "none") == 0)
171 			fatal("exportfs by none disallowed");
172 		if(auth_chuid(ai, nsfile) < 0)
173 			fatal("auth_chuid: %r");
174 		putenv("service", "exportfs");
175 	}
176 
177 	if(srvfdfile){
178 		if((srvfd = open(srvfdfile, ORDWR)) < 0)
179 			sysfatal("open '%s': %r", srvfdfile);
180 	}
181 
182 	if(na){
183 		if(srv == nil)
184 			sysfatal("-B requires -s");
185 
186 		if((fd = dial(netmkaddr(na, 0, "importfs"), 0, 0, 0)) < 0)
187 			sysfatal("can't dial %s: %r", na);
188 
189 		ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
190 		if(ai == nil)
191 			sysfatal("%r: %s", na);
192 
193 		dup(fd, 0);
194 		dup(fd, 1);
195 		close(fd);
196 	}
197 
198 	exclusions();
199 
200 	if(dbg) {
201 		n = create(dbfile, OWRITE|OTRUNC, 0666);
202 		dup(n, DFD);
203 		close(n);
204 	}
205 
206 	if(srvfd >= 0 && srv){
207 		fprint(2, "exportfs: -S cannot be used with -r or -s\n");
208 		usage();
209 	}
210 
211 	DEBUG(DFD, "exportfs: started\n");
212 
213 	rfork(RFNOTEG);
214 
215 	if(messagesize == 0){
216 		messagesize = iounit(netfd);
217 		if(messagesize == 0)
218 			messagesize = 8192+IOHDRSZ;
219 	}
220 
221 	Workq = emallocz(sizeof(Fsrpc)*Nr_workbufs);
222 //	for(i=0; i<Nr_workbufs; i++)
223 //		Workq[i].buf = emallocz(messagesize);
224 	fhash = emallocz(sizeof(Fid*)*FHASHSIZE);
225 
226 	fmtinstall('F', fcallfmt);
227 
228 	/*
229 	 * Get tree to serve from network connection,
230 	 * check we can get there and ack the connection
231  	 */
232 	if(srvfd != -1) {
233 		/* do nothing */
234 	}
235 	else if(srv) {
236 		chdir(srv);
237 		DEBUG(DFD, "invoked as server for %s", srv);
238 		strncpy(buf, srv, sizeof buf);
239 	}
240 	else {
241 		buf[0] = 0;
242 		n = read(0, buf, sizeof(buf)-1);
243 		if(n < 0) {
244 			errstr(buf, sizeof buf);
245 			fprint(0, "read(0): %s", buf);
246 			DEBUG(DFD, "read(0): %s", buf);
247 			exits(buf);
248 		}
249 		buf[n] = 0;
250 		if(chdir(buf) < 0) {
251 			errstr(ebuf, sizeof ebuf);
252 			fprint(0, "chdir(%d:\"%s\"): %s", n, buf, ebuf);
253 			DEBUG(DFD, "chdir(%d:\"%s\"): %s", n, buf, ebuf);
254 			exits(ebuf);
255 		}
256 	}
257 
258 	DEBUG(DFD, "\niniting root\n");
259 	initroot();
260 
261 	DEBUG(DFD, "exportfs: %s\n", buf);
262 
263 	if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
264 		fatal("open ack write");
265 
266 	if (readn(netfd, &initial, sizeof(ulong)) < sizeof(ulong))
267 		fatal("can't read initial string: %r\n");
268 
269 	if (!strncmp((char *)&initial, "impo", sizeof(ulong))) {
270 		char buf[128], *p, *args[3];
271 
272 		// New import.  Read import's parameters...
273 		initial = 0;
274 
275 		p = buf;
276 		while (p - buf < sizeof buf) {
277 			if ((n = read(netfd, p, 1)) < 0)
278 				fatal("can't read impo arguments: %r\n");
279 
280 			if (n == 0)
281 				fatal("connection closed while reading arguments\n");
282 
283 			if (*p == '\n')
284 				*p = '\0';
285 			if (*p++ == '\0')
286 				break;
287 		}
288 
289 		if (tokenize(buf, args, nelem(args)) != 2)
290 			fatal("impo arguments invalid: impo%s...\n", buf);
291 
292 		if (!strcmp(args[0], "aan"))
293 			filterp = aanfilter;
294 		else if (strcmp(args[0], "nofilter"))
295 			fatal("import filter argument unsupported: %s\n", args[0]);
296 
297 		if (!strcmp(args[1], "ssl"))
298 			encproto = Encssl;
299 		else if (!strcmp(args[1], "tls"))
300 			encproto = Enctls;
301 		else if (strcmp(args[1], "clear"))
302 			fatal("import encryption proto unsupported: %s\n", args[1]);
303 
304 		if (encproto == Enctls)
305 			sysfatal("%s: tls has not yet been implemented\n", argv[0]);
306 	}
307 
308 	if (encproto != Encnone && ealgs && ai) {
309 		uchar key[16];
310 		uchar digest[SHA1dlen];
311 		char fromclientsecret[21];
312 		char fromserversecret[21];
313 		int i;
314 
315 		memmove(key+4, ai->secret, ai->nsecret);
316 
317 		/* exchange random numbers */
318 		srand(truerand());
319 		for(i = 0; i < 4; i++)
320 			key[i+12] = rand();
321 
322 		if (initial)
323 			fatal("Protocol botch: old import\n");
324 		if(readn(netfd, key, 4) != 4)
325 			fatal("can't read key part; %r\n");
326 
327 		if(write(netfd, key+12, 4) != 4)
328 			fatal("can't write key part; %r\n");
329 
330 		/* scramble into two secrets */
331 		sha1(key, sizeof(key), digest, nil);
332 		mksecret(fromclientsecret, digest);
333 		mksecret(fromserversecret, digest+10);
334 
335 		if (filterp)
336 			netfd = filter(netfd, filterp);
337 
338 		switch (encproto) {
339 		case Encssl:
340 			netfd = pushssl(netfd, ealgs, fromserversecret,
341 						fromclientsecret, nil);
342 			break;
343 		case Enctls:
344 		default:
345 			fatal("Unsupported encryption protocol\n");
346 		}
347 
348 		if(netfd < 0)
349 			fatal("can't establish ssl connection: %r");
350 	}
351 	else if (filterp) {
352 		if (initial)
353 			fatal("Protocol botch: don't know how to deal with this\n");
354 		netfd = filter(netfd, filterp);
355 	}
356 
357 	/*
358 	 * Start serving file requests from the network
359 	 */
360 	for(;;) {
361 		r = getsbuf();
362 		if(r == 0)
363 			fatal("Out of service buffers");
364 
365 		n = localread9pmsg(netfd, r->buf, messagesize, &initial);
366 		if(n <= 0)
367 			fatal(nil);
368 
369 		if(convM2S(r->buf, n, &r->work) == 0)
370 			fatal("convM2S format error");
371 
372 		DEBUG(DFD, "%F\n", &r->work);
373 		(fcalls[r->work.type])(r);
374 	}
375 }
376 
377 // WARNING: Replace this with the original version as soon as all
378 // _old_ imports have been replaced with negotiating imports.  Also
379 // cpu relies on this (which needs to be fixed!) -- pb.
380 static int
381 localread9pmsg(int fd, void *abuf, uint n, ulong *initial)
382 {
383 	int m, len;
384 	uchar *buf;
385 
386 	buf = abuf;
387 
388 	/* read count */
389 	assert(BIT32SZ == sizeof(ulong));
390 	if (*initial) {
391 		memcpy(buf, initial, BIT32SZ);
392 		*initial = 0;
393 	}
394 	else {
395 		m = readn(fd, buf, BIT32SZ);
396 		if(m != BIT32SZ){
397 			if(m < 0)
398 				return -1;
399 			return 0;
400 		}
401 	}
402 
403 	len = GBIT32(buf);
404 	if(len <= BIT32SZ || len > n){
405 		werrstr("bad length in 9P2000 message header");
406 		return -1;
407 	}
408 	len -= BIT32SZ;
409 	m = readn(fd, buf+BIT32SZ, len);
410 	if(m < len)
411 		return 0;
412 	return BIT32SZ+m;
413 }
414 void
415 reply(Fcall *r, Fcall *t, char *err)
416 {
417 	uchar *data;
418 	int n;
419 
420 	t->tag = r->tag;
421 	t->fid = r->fid;
422 	if(err) {
423 		t->type = Rerror;
424 		t->ename = err;
425 	}
426 	else
427 		t->type = r->type + 1;
428 
429 	DEBUG(DFD, "\t%F\n", t);
430 
431 	data = malloc(messagesize);	/* not mallocz; no need to clear */
432 	if(data == nil)
433 		fatal(Enomem);
434 	n = convS2M(t, data, messagesize);
435 	if(write(netfd, data, n)!=n)
436 {syslog(0, "exportfs", "short write: %r");
437 		fatal("mount write");
438 }
439 	free(data);
440 }
441 
442 Fid *
443 getfid(int nr)
444 {
445 	Fid *f;
446 
447 	for(f = fidhash(nr); f; f = f->next)
448 		if(f->nr == nr)
449 			return f;
450 
451 	return 0;
452 }
453 
454 int
455 freefid(int nr)
456 {
457 	Fid *f, **l;
458 	char buf[128];
459 
460 	l = &fidhash(nr);
461 	for(f = *l; f; f = f->next) {
462 		if(f->nr == nr) {
463 			if(f->mid) {
464 				sprint(buf, "/mnt/exportfs/%d", f->mid);
465 				unmount(0, buf);
466 				psmap[f->mid] = 0;
467 			}
468 			if(f->f) {
469 				freefile(f->f);
470 				f->f = nil;
471 			}
472 			if(f->dir){
473 				free(f->dir);
474 				f->dir = nil;
475 			}
476 			*l = f->next;
477 			f->next = fidfree;
478 			fidfree = f;
479 			return 1;
480 		}
481 		l = &f->next;
482 	}
483 
484 	return 0;
485 }
486 
487 Fid *
488 newfid(int nr)
489 {
490 	Fid *new, **l;
491 	int i;
492 
493 	l = &fidhash(nr);
494 	for(new = *l; new; new = new->next)
495 		if(new->nr == nr)
496 			return 0;
497 
498 	if(fidfree == 0) {
499 		fidfree = emallocz(sizeof(Fid) * Fidchunk);
500 
501 		for(i = 0; i < Fidchunk-1; i++)
502 			fidfree[i].next = &fidfree[i+1];
503 
504 		fidfree[Fidchunk-1].next = 0;
505 	}
506 
507 	new = fidfree;
508 	fidfree = new->next;
509 
510 	memset(new, 0, sizeof(Fid));
511 	new->next = *l;
512 	*l = new;
513 	new->nr = nr;
514 	new->fid = -1;
515 	new->mid = 0;
516 
517 	return new;
518 }
519 
520 Fsrpc *
521 getsbuf(void)
522 {
523 	static int ap;
524 	int look, rounds;
525 	Fsrpc *wb;
526 	int small_instead_of_fast = 1;
527 
528 	if(small_instead_of_fast)
529 		ap = 0;	/* so we always start looking at the beginning and reuse buffers */
530 
531 	for(rounds = 0; rounds < 10; rounds++) {
532 		for(look = 0; look < Nr_workbufs; look++) {
533 			if(++ap == Nr_workbufs)
534 				ap = 0;
535 			if(Workq[ap].busy == 0)
536 				break;
537 		}
538 
539 		if(look == Nr_workbufs){
540 			sleep(10 * rounds);
541 			continue;
542 		}
543 
544 		wb = &Workq[ap];
545 		wb->pid = 0;
546 		wb->canint = 0;
547 		wb->flushtag = NOTAG;
548 		wb->busy = 1;
549 		if(wb->buf == nil)	/* allocate buffers dynamically to keep size down */
550 			wb->buf = emallocz(messagesize);
551 		return wb;
552 	}
553 	fatal("No more work buffers");
554 	return nil;
555 }
556 
557 void
558 freefile(File *f)
559 {
560 	File *parent, *child;
561 
562 Loop:
563 	f->ref--;
564 	if(f->ref > 0)
565 		return;
566 	freecnt++;
567 	if(f->ref < 0) abort();
568 	DEBUG(DFD, "free %s\n", f->name);
569 	/* delete from parent */
570 	parent = f->parent;
571 	if(parent->child == f)
572 		parent->child = f->childlist;
573 	else{
574 		for(child=parent->child; child->childlist!=f; child=child->childlist)
575 			if(child->childlist == nil)
576 				fatal("bad child list");
577 		child->childlist = f->childlist;
578 	}
579 	freeqid(f->qidt);
580 	free(f->name);
581 	f->name = nil;
582 	free(f);
583 	f = parent;
584 	if(f != nil)
585 		goto Loop;
586 }
587 
588 File *
589 file(File *parent, char *name)
590 {
591 	Dir *dir;
592 	char *path;
593 	File *f;
594 
595 	DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
596 
597 	path = makepath(parent, name);
598 	if(patternfile != nil && excludefile(path)){
599 		free(path);
600 		return nil;
601 	}
602 	dir = dirstat(path);
603 	free(path);
604 	if(dir == nil)
605 		return nil;
606 
607 	for(f = parent->child; f; f = f->childlist)
608 		if(strcmp(name, f->name) == 0)
609 			break;
610 
611 	if(f == nil){
612 		f = emallocz(sizeof(File));
613 		f->name = estrdup(name);
614 
615 		f->parent = parent;
616 		f->childlist = parent->child;
617 		parent->child = f;
618 		parent->ref++;
619 		f->ref = 0;
620 		filecnt++;
621 	}
622 	f->ref++;
623 	f->qid.type = dir->qid.type;
624 	f->qid.vers = dir->qid.vers;
625 	f->qidt = uniqueqid(dir);
626 	f->qid.path = f->qidt->uniqpath;
627 
628 	f->inval = 0;
629 
630 	free(dir);
631 
632 	return f;
633 }
634 
635 void
636 initroot(void)
637 {
638 	Dir *dir;
639 
640 	root = emallocz(sizeof(File));
641 	root->name = estrdup(".");
642 
643 	dir = dirstat(root->name);
644 	if(dir == nil)
645 		fatal("root stat");
646 
647 	root->ref = 1;
648 	root->qid.vers = dir->qid.vers;
649 	root->qidt = uniqueqid(dir);
650 	root->qid.path = root->qidt->uniqpath;
651 	root->qid.type = QTDIR;
652 	free(dir);
653 
654 	psmpt = emallocz(sizeof(File));
655 	psmpt->name = estrdup("/");
656 
657 	dir = dirstat(psmpt->name);
658 	if(dir == nil)
659 		return;
660 
661 	psmpt->ref = 1;
662 	psmpt->qid.vers = dir->qid.vers;
663 	psmpt->qidt = uniqueqid(dir);
664 	psmpt->qid.path = psmpt->qidt->uniqpath;
665 	free(dir);
666 
667 	psmpt = file(psmpt, "mnt");
668 	if(psmpt == 0)
669 		return;
670 	psmpt = file(psmpt, "exportfs");
671 }
672 
673 char*
674 makepath(File *p, char *name)
675 {
676 	int i, n;
677 	char *c, *s, *path, *seg[256];
678 
679 	seg[0] = name;
680 	n = strlen(name)+2;
681 	for(i = 1; i < 256 && p; i++, p = p->parent){
682 		seg[i] = p->name;
683 		n += strlen(p->name)+1;
684 	}
685 	path = malloc(n);
686 	if(path == nil)
687 		fatal("out of memory");
688 	s = path;
689 
690 	while(i--) {
691 		for(c = seg[i]; *c; c++)
692 			*s++ = *c;
693 		*s++ = '/';
694 	}
695 	while(s[-1] == '/')
696 		s--;
697 	*s = '\0';
698 
699 	return path;
700 }
701 
702 int
703 qidhash(vlong path)
704 {
705 	int h, n;
706 
707 	h = 0;
708 	for(n=0; n<64; n+=Nqidbits){
709 		h ^= path;
710 		path >>= Nqidbits;
711 	}
712 	return h & (Nqidtab-1);
713 }
714 
715 void
716 freeqid(Qidtab *q)
717 {
718 	ulong h;
719 	Qidtab *l;
720 
721 	q->ref--;
722 	if(q->ref > 0)
723 		return;
724 	qfreecnt++;
725 	h = qidhash(q->path);
726 	if(qidtab[h] == q)
727 		qidtab[h] = q->next;
728 	else{
729 		for(l=qidtab[h]; l->next!=q; l=l->next)
730 			if(l->next == nil)
731 				fatal("bad qid list");
732 		l->next = q->next;
733 	}
734 	free(q);
735 }
736 
737 Qidtab*
738 qidlookup(Dir *d)
739 {
740 	ulong h;
741 	Qidtab *q;
742 
743 	h = qidhash(d->qid.path);
744 	for(q=qidtab[h]; q!=nil; q=q->next)
745 		if(q->type==d->type && q->dev==d->dev && q->path==d->qid.path)
746 			return q;
747 	return nil;
748 }
749 
750 int
751 qidexists(vlong path)
752 {
753 	int h;
754 	Qidtab *q;
755 
756 	for(h=0; h<Nqidtab; h++)
757 		for(q=qidtab[h]; q!=nil; q=q->next)
758 			if(q->uniqpath == path)
759 				return 1;
760 	return 0;
761 }
762 
763 Qidtab*
764 uniqueqid(Dir *d)
765 {
766 	ulong h;
767 	vlong path;
768 	Qidtab *q;
769 
770 	q = qidlookup(d);
771 	if(q != nil){
772 		q->ref++;
773 		return q;
774 	}
775 	path = d->qid.path;
776 	while(qidexists(path)){
777 		DEBUG(DFD, "collision on %s\n", d->name);
778 		/* collision: find a new one */
779 		ncollision++;
780 		path &= QIDPATH;
781 		++newqid;
782 		if(newqid >= (1<<16)){
783 			DEBUG(DFD, "collision wraparound\n");
784 			newqid = 1;
785 		}
786 		path |= newqid<<48;
787 		DEBUG(DFD, "assign qid %.16llux\n", path);
788 	}
789 	q = mallocz(sizeof(Qidtab), 1);
790 	if(q == nil)
791 		fatal("no memory for qid table");
792 	qidcnt++;
793 	q->ref = 1;
794 	q->type = d->type;
795 	q->dev = d->dev;
796 	q->path = d->qid.path;
797 	q->uniqpath = path;
798 	h = qidhash(d->qid.path);
799 	q->next = qidtab[h];
800 	qidtab[h] = q;
801 	return q;
802 }
803 
804 void
805 fatal(char *s, ...)
806 {
807 	char buf[ERRMAX];
808 	va_list arg;
809 	Proc *m;
810 
811 	if (s) {
812 		va_start(arg, s);
813 		vsnprint(buf, ERRMAX, s, arg);
814 		va_end(arg);
815 	}
816 
817 	/* Clear away the slave children */
818 	for(m = Proclist; m; m = m->next)
819 		postnote(PNPROC, m->pid, "kill");
820 
821 	DEBUG(DFD, "%s\n", buf);
822 	if (s)
823 		sysfatal(buf);
824 	else
825 		exits(nil);
826 }
827 
828 void*
829 emallocz(uint n)
830 {
831 	void *p;
832 
833 	p = mallocz(n, 1);
834 	if(p == nil)
835 		fatal(Enomem);
836 	return p;
837 }
838 
839 char*
840 estrdup(char *s)
841 {
842 	char *t;
843 
844 	t = strdup(s);
845 	if(t == nil)
846 		fatal(Enomem);
847 	return t;
848 }
849 
850 /* Network on fd1, mount driver on fd0 */
851 int
852 filter(int fd, char *cmd)
853 {
854 	int p[2], lfd, len, nb, argc;
855 	char newport[128], buf[128], devdir[40], *s, *file, *argv[16];
856 
857 	// Get a free port and post it to the client.
858 	if (announce(anstring, devdir) < 0)
859 		sysfatal("filter: Cannot announce %s: %r\n", anstring);
860 
861 	snprint(buf, sizeof(buf), "%s/local", devdir);
862 	buf[sizeof buf - 1] = '\0';
863 	if ((lfd = open(buf, OREAD)) < 0)
864 		sysfatal("filter: Cannot open %s: %r\n", buf);
865 	if ((len = read(lfd, newport, sizeof newport - 1)) < 0)
866 		sysfatal("filter: Cannot read %s: %r\n", buf);
867 	close(lfd);
868 	newport[len] = '\0';
869 
870 	if ((s = strchr(newport, '\n')) != nil)
871 		*s = '\0';
872 
873 	if ((nb = write(fd, newport, len)) < 0)
874 		sysfatal("getport; cannot write port; %r");
875 	assert(nb == len);
876 
877 	argc = tokenize(cmd, argv, nelem(argv)-2);
878 	if (argc == 0)
879 		sysfatal("filter: empty command");
880 	argv[argc++] = buf;
881 	argv[argc] = nil;
882 	file = argv[0];
883 	if (s = strrchr(argv[0], '/'))
884 		argv[0] = s+1;
885 
886 	if(pipe(p) < 0)
887 		fatal("pipe");
888 
889 	switch(rfork(RFNOWAIT|RFPROC|RFFDG)) {
890 	case -1:
891 		fatal("rfork record module");
892 	case 0:
893 		if (dup(p[0], 1) < 0)
894 			fatal("filter: Cannot dup to 1; %r\n");
895 		if (dup(p[0], 0) < 0)
896 			fatal("filter: Cannot dup to 0; %r\n");
897 		close(p[0]);
898 		close(p[1]);
899 		exec(file, argv);
900 		fatal("exec record module");
901 	default:
902 		close(fd);
903 		close(p[0]);
904 	}
905 	return p[1];
906 }
907 
908 static void
909 mksecret(char *t, uchar *f)
910 {
911 	sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
912 		f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
913 }
914