2 * exportfs - Export a plan 9 name space across a network
12 #define QIDPATH ((1LL<<48)-1)
21 void (*fcalls[])(Fsrpc*) =
38 /* accounting and debugging counters */
45 int netfd; /* initially stdin */
49 char *ealgs = "rc4_256 sha1";
50 char *aanfilter = "/bin/aan";
51 int encproto = Encnone;
54 static void mksecret(char *, uchar *);
55 static int localread9pmsg(int, void *, uint, void *);
56 static char *anstring = "tcp!*!0";
58 char *netdir = "", *local = "", *remote = "";
60 int filter(int, char *);
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);
76 nci = getnetconninfo(nil, fd);
79 netdir = estrdup(nci->dir);
80 local = estrdup(nci->lsys);
81 remote = estrdup(nci->rsys);
86 main(int argc, char **argv)
88 char buf[ERRMAX], ebuf[ERRMAX], initial[4], *ini, *srvfdfile;
89 char *dbfile, *srv, *na, *nsfile, *keyspec;
94 dbfile = "/tmp/exportdb";
114 ealgs = EARGF(usage());
115 if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
120 dbfile = EARGF(usage());
124 keyspec = EARGF(usage());
128 messagesize = strtoul(EARGF(usage()), nil, 0);
136 srv = EARGF(usage());
144 anstring = EARGF(usage());
152 /* accepted but ignored, for backwards compatibility */
156 nsfile = EARGF(usage());
160 patternfile = EARGF(usage());
170 srvfdfile = EARGF(usage());
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
185 ai = auth_proxy(0, auth_getkey, "proto=p9any role=server %s", keyspec);
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");
195 if(srvfdfile != nil){
196 if((srvfd = open(srvfdfile, ORDWR)) < 0)
197 fatal("open %s: %r", srvfdfile);
202 fatal("-B requires -s");
206 if((fd = dial(netmkaddr(na, 0, "importfs"), 0, 0, 0)) < 0)
207 fatal("can't dial %s: %r", na);
209 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
221 n = create(dbfile, OWRITE|OTRUNC, 0666);
226 if(srvfd >= 0 && srv != nil){
227 fprint(2, "exportfs: -S cannot be used with -r or -s\n");
231 DEBUG(DFD, "exportfs: started\n");
233 rfork(RFNOTEG|RFREND);
235 if(messagesize == 0){
236 messagesize = iounit(netfd);
238 messagesize = 8192+IOHDRSZ;
240 fhash = emallocz(sizeof(Fid*)*FHASHSIZE);
242 fmtinstall('F', fcallfmt);
245 * Get tree to serve from network connection,
246 * check we can get there and ack the connection
251 else if(srv != nil) {
253 errstr(ebuf, sizeof ebuf);
254 fprint(0, "chdir(\"%s\"): %s\n", srv, ebuf);
255 DEBUG(DFD, "chdir(\"%s\"): %s\n", srv, ebuf);
258 DEBUG(DFD, "invoked as server for %s", srv);
259 strncpy(buf, srv, sizeof buf);
264 n = read(0, buf, sizeof(buf)-1);
266 errstr(buf, sizeof buf);
267 fprint(0, "read(0): %s\n", buf);
268 DEBUG(DFD, "read(0): %s\n", buf);
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);
280 DEBUG(DFD, "\niniting root\n");
283 DEBUG(DFD, "exportfs: %s\n", buf);
285 if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
286 fatal("open ack write");
289 n = readn(netfd, initial, sizeof(initial));
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");
295 if(memcmp(ini, "impo", 4) == 0) {
296 char buf[128], *p, *args[3];
301 if((n = read(netfd, p, 1)) < 0)
302 fatal("can't read impo arguments: %r");
304 fatal("connection closed while reading arguments");
309 if(p >= buf + sizeof(buf))
310 fatal("import parameters too long");
313 if(tokenize(buf, args, nelem(args)) != 2)
314 fatal("impo arguments invalid: impo%s...", buf);
316 if(strcmp(args[0], "aan") == 0)
318 else if(strcmp(args[0], "nofilter") != 0)
319 fatal("import filter argument unsupported: %s", args[0]);
321 if(strcmp(args[1], "ssl") == 0)
323 else if(strcmp(args[1], "tls") == 0)
325 else if(strcmp(args[1], "clear") != 0)
326 fatal("import encryption proto unsupported: %s", args[1]);
328 if(encproto == Enctls)
329 fatal("%s: tls has not yet been implemented", argv[0]);
332 if(encproto != Encnone && ealgs != nil && ai != nil) {
333 uchar key[16], digest[SHA1dlen];
334 char fromclientsecret[21];
335 char fromserversecret[21];
338 assert(ai->nsecret <= sizeof(key)-4);
339 memmove(key+4, ai->secret, ai->nsecret);
341 /* exchange random numbers */
343 for(i = 0; i < 4; i++)
347 fatal("Protocol botch: old import");
348 if(readn(netfd, key, 4) != 4)
349 fatal("can't read key part; %r");
351 if(write(netfd, key+12, 4) != 4)
352 fatal("can't write key part; %r");
354 /* scramble into two secrets */
355 sha1(key, sizeof(key), digest, nil);
356 mksecret(fromclientsecret, digest);
357 mksecret(fromserversecret, digest+10);
360 netfd = filter(netfd, filterp);
364 netfd = pushssl(netfd, ealgs, fromserversecret,
365 fromclientsecret, nil);
369 fatal("Unsupported encryption protocol");
373 fatal("can't establish ssl connection: %r");
375 else if(filterp != nil) {
377 fatal("Protocol botch: don't know how to deal with this");
378 netfd = filter(netfd, filterp);
385 * Start serving file requests from the network
390 fatal("Out of service buffers");
392 while((n = localread9pmsg(netfd, r->buf, messagesize, ini)) == 0)
396 if(convM2S(r->buf, n, &r->work) == 0)
397 fatal("convM2S format error");
399 DEBUG(DFD, "%F\n", &r->work);
400 (fcalls[r->work.type])(r);
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.
411 localread9pmsg(int fd, void *abuf, uint n, void *ini)
420 memcpy(buf, ini, BIT32SZ);
422 m = readn(fd, buf, BIT32SZ);
431 if(len <= BIT32SZ || len > n){
432 werrstr("bad length in 9P2000 message header");
436 m = readn(fd, buf+BIT32SZ, len);
442 reply(Fcall *r, Fcall *t, char *err)
454 t->type = r->type + 1;
456 DEBUG(DFD, "\t%F\n", t);
458 data = malloc(messagesize); /* not mallocz; no need to clear */
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");
474 for(f = fidhash(nr); f != nil; f = f->next)
488 for(f = *l; f != nil; f = f->next) {
491 snprint(buf, sizeof(buf), "/mnt/exportfs/%d", f->mid);
521 for(new = *l; new != nil; new = new->next)
526 fidfree = emallocz(sizeof(Fid) * Fidchunk);
528 for(i = 0; i < Fidchunk-1; i++)
529 fidfree[i].next = &fidfree[i+1];
531 fidfree[Fidchunk-1].next = nil;
537 memset(new, 0, sizeof(Fid));
564 sbufalloc.free = w->next;
571 w = emallocz(sizeof(*w) + messagesize);
582 w->next = sbufalloc.free;
591 File *parent, *child;
598 if(f->ref < 0) abort();
599 DEBUG(DFD, "free %s\n", f->name);
600 /* delete from parent */
602 if(parent->child == f)
603 parent->child = f->childlist;
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;
620 file(File *parent, char *name)
626 DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
628 path = makepath(parent, name);
629 if(patternfile != nil && excludefile(path)){
638 for(f = parent->child; f != nil; f = f->childlist)
639 if(strcmp(name, f->name) == 0)
643 f = emallocz(sizeof(File));
644 f->name = estrdup(name);
647 f->childlist = parent->child;
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;
671 root = emallocz(sizeof(File));
672 root->name = estrdup(".");
674 dir = dirstat(root->name);
679 root->qid.vers = dir->qid.vers;
680 root->qidt = uniqueqid(dir);
681 root->qid.path = root->qidt->uniqpath;
682 root->qid.type = QTDIR;
685 psmpt = emallocz(sizeof(File));
686 psmpt->name = estrdup("/");
688 dir = dirstat(psmpt->name);
693 psmpt->qid.vers = dir->qid.vers;
694 psmpt->qidt = uniqueqid(dir);
695 psmpt->qid.path = psmpt->qidt->uniqpath;
698 psmpt = file(psmpt, "mnt");
701 psmpt = file(psmpt, "exportfs");
705 makepath(File *p, char *name)
708 char *c, *s, *path, *seg[256];
712 for(i = 1; i < 256 && p; i++, p = p->parent){
714 n += strlen(p->name)+1;
720 for(c = seg[i]; *c; c++)
737 for(n=0; n<64; n+=Nqidbits){
741 return h & (Nqidtab-1);
754 h = qidhash(q->path);
758 for(l=qidtab[h]; l->next!=q; l=l->next)
760 fatal("bad qid list");
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)
780 qidexists(vlong path)
785 for(h=0; h<Nqidtab; h++)
786 for(q=qidtab[h]; q!=nil; q=q->next)
787 if(q->uniqpath == path)
805 while(qidexists(path)){
806 DEBUG(DFD, "collision on %s\n", d->name);
807 /* collision: find a new one */
811 if(newqid >= (1<<16)){
812 DEBUG(DFD, "collision wraparound\n");
816 DEBUG(DFD, "assign qid %.16llux\n", path);
819 q = emallocz(sizeof(Qidtab));
823 q->path = d->qid.path;
825 h = qidhash(d->qid.path);
840 vsnprint(buf, ERRMAX, s, arg);
844 /* Clear away the slave children */
845 for(m = Proclist; m != nil; m = m->next)
846 postnote(PNPROC, m->pid, "kill");
849 DEBUG(DFD, "%s\n", buf);
850 sysfatal("%s", buf); /* caution: buf could contain '%' */
863 setmalloctag(p, getcallerpc(&n));
875 setmalloctag(t, getcallerpc(&s));
879 /* Network on fd1, mount driver on fd0 */
881 filter(int fd, char *cmd)
883 char buf[128], devdir[40], *s, *file, *argv[16];
884 int p[2], lfd, len, argc;
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);
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);
897 if ((s = strchr(buf, '\n')) != nil)
899 if (write(fd, buf, len) != len)
900 fatal("filter: cannot write port; %r");
902 snprint(buf, sizeof(buf), "%s", cmd);
903 argc = tokenize(buf, argv, nelem(argv)-2);
905 fatal("filter: empty command");
906 argv[argc++] = devdir;
909 if (s = strrchr(argv[0], '/'))
913 fatal("filter: pipe; %r");
915 switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
917 fatal("filter: rfork; %r\n");
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");
926 fatal("filter: exec; %r");
936 mksecret(char *t, uchar *f)
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]);