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