]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/exportfs/exportfs.c
exportfs: reverting previous changes
[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)
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){
196                 if((srvfd = open(srvfdfile, ORDWR)) < 0)
197                         fatal("open %s: %r", srvfdfile);
198         }
199
200         if(na){
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){
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);
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) {
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 && ai) {
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) 
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)
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) {
376                 if (ini)
377                         fatal("Protocol botch: don't know how to deal with this");
378                 netfd = filter(netfd, filterp);
379         }
380
381         if(ai)
382                 auth_freeAI(ai);
383
384         /*
385          * Start serving file requests from the network
386          */
387         for(;;) {
388                 r = getsbuf();
389                 if(r == 0)
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)
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) {
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                 syslog(0, "exportfs", "short write: %r");
464                 fatal("mount write");
465         }
466         free(data);
467 }
468
469 Fid *
470 getfid(int nr)
471 {
472         Fid *f;
473
474         for(f = fidhash(nr); f; f = f->next)
475                 if(f->nr == nr)
476                         return f;
477
478         return 0;
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; 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) {
496                                 freefile(f->f);
497                                 f->f = nil;
498                         }
499                         if(f->dir){
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; new = new->next)
522                 if(new->nr == nr)
523                         return 0;
524
525         if(fidfree == 0) {
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 = 0;
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 != 0){
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->pid = 0;
574         w->canint = 0;
575         w->flushtag = NOTAG;
576         return w;
577 }
578
579 void
580 putsbuf(Fsrpc *w)
581 {
582         w->pid = 0;
583         w->canint = 0;
584         w->flushtag = NOTAG;
585         lock(&sbufalloc);
586         w->next = sbufalloc.free;
587         sbufalloc.free = w;
588         sbufalloc.nfree++;
589         unlock(&sbufalloc);
590 }
591
592 void
593 freefile(File *f)
594 {
595         File *parent, *child;
596
597 Loop:
598         f->ref--;
599         if(f->ref > 0)
600                 return;
601         freecnt++;
602         if(f->ref < 0) abort();
603         DEBUG(DFD, "free %s\n", f->name);
604         /* delete from parent */
605         parent = f->parent;
606         if(parent->child == f)
607                 parent->child = f->childlist;
608         else{
609                 for(child=parent->child; child->childlist!=f; child=child->childlist)
610                         if(child->childlist == nil)
611                                 fatal("bad child list");
612                 child->childlist = f->childlist;
613         }
614         freeqid(f->qidt);
615         free(f->name);
616         f->name = nil;
617         free(f);
618         f = parent;
619         if(f != nil)
620                 goto Loop;
621 }
622
623 File *
624 file(File *parent, char *name)
625 {
626         Dir *dir;
627         char *path;
628         File *f;
629
630         DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
631
632         path = makepath(parent, name);
633         if(patternfile != nil && excludefile(path)){
634                 free(path);
635                 return nil;
636         }
637         dir = dirstat(path);
638         free(path);
639         if(dir == nil)
640                 return nil;
641
642         for(f = parent->child; f; f = f->childlist)
643                 if(strcmp(name, f->name) == 0)
644                         break;
645
646         if(f == nil){
647                 f = emallocz(sizeof(File));
648                 f->name = estrdup(name);
649
650                 f->parent = parent;
651                 f->childlist = parent->child;
652                 parent->child = f;
653                 parent->ref++;
654                 f->ref = 0;
655                 filecnt++;
656         }
657         f->ref++;
658         f->qid.type = dir->qid.type;
659         f->qid.vers = dir->qid.vers;
660         f->qidt = uniqueqid(dir);
661         f->qid.path = f->qidt->uniqpath;
662
663         f->inval = 0;
664
665         free(dir);
666
667         return f;
668 }
669
670 void
671 initroot(void)
672 {
673         Dir *dir;
674
675         root = emallocz(sizeof(File));
676         root->name = estrdup(".");
677
678         dir = dirstat(root->name);
679         if(dir == nil)
680                 fatal("root stat");
681
682         root->ref = 1;
683         root->qid.vers = dir->qid.vers;
684         root->qidt = uniqueqid(dir);
685         root->qid.path = root->qidt->uniqpath;
686         root->qid.type = QTDIR;
687         free(dir);
688
689         psmpt = emallocz(sizeof(File));
690         psmpt->name = estrdup("/");
691
692         dir = dirstat(psmpt->name);
693         if(dir == nil)
694                 return;
695
696         psmpt->ref = 1;
697         psmpt->qid.vers = dir->qid.vers;
698         psmpt->qidt = uniqueqid(dir);
699         psmpt->qid.path = psmpt->qidt->uniqpath;
700         free(dir);
701
702         psmpt = file(psmpt, "mnt");
703         if(psmpt == 0)
704                 return;
705         psmpt = file(psmpt, "exportfs");
706 }
707
708 char*
709 makepath(File *p, char *name)
710 {
711         int i, n;
712         char *c, *s, *path, *seg[256];
713
714         seg[0] = name;
715         n = strlen(name)+2;
716         for(i = 1; i < 256 && p; i++, p = p->parent){
717                 seg[i] = p->name;
718                 n += strlen(p->name)+1;
719         }
720         path = emallocz(n);
721         s = path;
722
723         while(i--) {
724                 for(c = seg[i]; *c; c++)
725                         *s++ = *c;
726                 *s++ = '/';
727         }
728         while(s[-1] == '/')
729                 s--;
730         *s = '\0';
731
732         return path;
733 }
734
735 int
736 qidhash(vlong path)
737 {
738         int h, n;
739
740         h = 0;
741         for(n=0; n<64; n+=Nqidbits){
742                 h ^= path;
743                 path >>= Nqidbits;
744         }
745         return h & (Nqidtab-1);
746 }
747
748 void
749 freeqid(Qidtab *q)
750 {
751         ulong h;
752         Qidtab *l;
753
754         q->ref--;
755         if(q->ref > 0)
756                 return;
757         qfreecnt++;
758         h = qidhash(q->path);
759         if(qidtab[h] == q)
760                 qidtab[h] = q->next;
761         else{
762                 for(l=qidtab[h]; l->next!=q; l=l->next)
763                         if(l->next == nil)
764                                 fatal("bad qid list");
765                 l->next = q->next;
766         }
767         free(q);
768 }
769
770 Qidtab*
771 qidlookup(Dir *d)
772 {
773         ulong h;
774         Qidtab *q;
775
776         h = qidhash(d->qid.path);
777         for(q=qidtab[h]; q!=nil; q=q->next)
778                 if(q->type==d->type && q->dev==d->dev && q->path==d->qid.path)
779                         return q;
780         return nil;
781 }
782
783 int
784 qidexists(vlong path)
785 {
786         int h;
787         Qidtab *q;
788
789         for(h=0; h<Nqidtab; h++)
790                 for(q=qidtab[h]; q!=nil; q=q->next)
791                         if(q->uniqpath == path)
792                                 return 1;
793         return 0;
794 }
795
796 Qidtab*
797 uniqueqid(Dir *d)
798 {
799         ulong h;
800         vlong path;
801         Qidtab *q;
802
803         q = qidlookup(d);
804         if(q != nil){
805                 q->ref++;
806                 return q;
807         }
808         path = d->qid.path;
809         while(qidexists(path)){
810                 DEBUG(DFD, "collision on %s\n", d->name);
811                 /* collision: find a new one */
812                 ncollision++;
813                 path &= QIDPATH;
814                 ++newqid;
815                 if(newqid >= (1<<16)){
816                         DEBUG(DFD, "collision wraparound\n");
817                         newqid = 1;
818                 }
819                 path |= newqid<<48;
820                 DEBUG(DFD, "assign qid %.16llux\n", path);
821         }
822         q = mallocz(sizeof(Qidtab), 1);
823         if(q == nil)
824                 fatal("no memory for qid table");
825         qidcnt++;
826         q->ref = 1;
827         q->type = d->type;
828         q->dev = d->dev;
829         q->path = d->qid.path;
830         q->uniqpath = path;
831         h = qidhash(d->qid.path);
832         q->next = qidtab[h];
833         qidtab[h] = q;
834         return q;
835 }
836
837 void
838 fatal(char *s, ...)
839 {
840         char buf[ERRMAX];
841         va_list arg;
842         Proc *m;
843
844         if (s) {
845                 va_start(arg, s);
846                 vsnprint(buf, ERRMAX, s, arg);
847                 va_end(arg);
848         }
849
850         /* Clear away the slave children */
851         for(m = Proclist; m; m = m->next)
852                 postnote(PNPROC, m->pid, "kill");
853
854         if (s) {
855                 DEBUG(DFD, "%s\n", buf);
856                 sysfatal("%s", buf);    /* caution: buf could contain '%' */
857         } else
858                 exits(nil);
859 }
860
861 void*
862 emallocz(uint n)
863 {
864         void *p;
865
866         p = mallocz(n, 1);
867         if(p == nil)
868                 fatal(Enomem);
869         setmalloctag(p, getcallerpc(&n));
870         return p;
871 }
872
873 char*
874 estrdup(char *s)
875 {
876         char *t;
877
878         t = strdup(s);
879         if(t == nil)
880                 fatal(Enomem);
881         setmalloctag(t, getcallerpc(&s));
882         return t;
883 }
884
885 /* Network on fd1, mount driver on fd0 */
886 int
887 filter(int fd, char *cmd)
888 {
889         char buf[128], devdir[40], *s, *file, *argv[16];
890         int p[2], lfd, len, argc;
891
892         /* Get a free port and post it to the client. */
893         if (announce(anstring, devdir) < 0)
894                 fatal("filter: Cannot announce %s: %r", anstring);
895
896         snprint(buf, sizeof(buf), "%s/local", devdir);
897         if ((lfd = open(buf, OREAD)) < 0)
898                 fatal("filter: Cannot open %s: %r", buf);
899         if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
900                 fatal("filter: Cannot read %s: %r", buf);
901         close(lfd);
902         buf[len] = '\0';
903         if ((s = strchr(buf, '\n')) != nil)
904                 len = s - buf;
905         if (write(fd, buf, len) != len) 
906                 fatal("filter: cannot write port; %r");
907
908         snprint(buf, sizeof(buf), "%s", cmd);
909         argc = tokenize(buf, argv, nelem(argv)-2);
910         if (argc == 0)
911                 fatal("filter: empty command");
912         argv[argc++] = devdir;
913         argv[argc] = nil;
914         file = argv[0];
915         if (s = strrchr(argv[0], '/'))
916                 argv[0] = s+1;
917
918         if(pipe(p) < 0)
919                 fatal("filter: pipe; %r");
920
921         switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
922         case -1:
923                 fatal("filter: rfork; %r\n");
924         case 0:
925                 if (dup(p[0], 1) < 0)
926                         fatal("filter: Cannot dup to 1; %r");
927                 if (dup(p[0], 0) < 0)
928                         fatal("filter: Cannot dup to 0; %r");
929                 close(p[0]);
930                 close(p[1]);
931                 exec(file, argv);
932                 fatal("filter: exec; %r");
933         default:
934                 close(fd);
935                 close(p[0]);
936         }
937         return p[1];    
938 }
939
940 static void
941 mksecret(char *t, uchar *f)
942 {
943         sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
944                 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
945 }