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