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