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