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 = strdup(nci->dir);
80 local = strdup(nci->lsys);
81 remote = strdup(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");
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){
227 fprint(2, "exportfs: -S cannot be used with -r or -s\n");
231 DEBUG(DFD, "exportfs: started\n");
235 if(messagesize == 0){
236 messagesize = iounit(netfd);
238 messagesize = 8192+IOHDRSZ;
241 Workq = emallocz(sizeof(Fsrpc)*Nr_workbufs);
242 fhash = emallocz(sizeof(Fid*)*FHASHSIZE);
244 fmtinstall('F', fcallfmt);
247 * Get tree to serve from network connection,
248 * check we can get there and ack the connection
255 errstr(ebuf, sizeof ebuf);
256 fprint(0, "chdir(\"%s\"): %s\n", srv, ebuf);
257 DEBUG(DFD, "chdir(\"%s\"): %s\n", srv, ebuf);
260 DEBUG(DFD, "invoked as server for %s", srv);
261 strncpy(buf, srv, sizeof buf);
266 n = read(0, buf, sizeof(buf)-1);
268 errstr(buf, sizeof buf);
269 fprint(0, "read(0): %s\n", buf);
270 DEBUG(DFD, "read(0): %s\n", buf);
275 errstr(ebuf, sizeof ebuf);
276 fprint(0, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
277 DEBUG(DFD, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
282 DEBUG(DFD, "\niniting root\n");
285 DEBUG(DFD, "exportfs: %s\n", buf);
287 if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
288 fatal("open ack write");
291 n = readn(netfd, initial, sizeof(initial));
293 fatal(nil); /* port scan or spurious open/close on exported /srv file (unmount) */
294 if (n < sizeof(initial))
295 fatal("can't read initial string: %r");
297 if (memcmp(ini, "impo", 4) == 0) {
298 char buf[128], *p, *args[3];
303 if ((n = read(netfd, p, 1)) < 0)
304 fatal("can't read impo arguments: %r");
306 fatal("connection closed while reading arguments");
311 if(p >= buf + sizeof(buf))
312 fatal("import parameters too long");
315 if (tokenize(buf, args, nelem(args)) != 2)
316 fatal("impo arguments invalid: impo%s...", buf);
318 if (strcmp(args[0], "aan") == 0)
320 else if (strcmp(args[0], "nofilter") != 0)
321 fatal("import filter argument unsupported: %s", args[0]);
323 if (strcmp(args[1], "ssl") == 0)
325 else if (strcmp(args[1], "tls") == 0)
327 else if (strcmp(args[1], "clear") != 0)
328 fatal("import encryption proto unsupported: %s", args[1]);
330 if (encproto == Enctls)
331 fatal("%s: tls has not yet been implemented", argv[0]);
334 if (encproto != Encnone && ealgs && ai) {
335 uchar key[16], digest[SHA1dlen];
336 char fromclientsecret[21];
337 char fromserversecret[21];
340 assert(ai->nsecret <= sizeof(key)-4);
341 memmove(key+4, ai->secret, ai->nsecret);
343 /* exchange random numbers */
345 for(i = 0; i < 4; i++)
349 fatal("Protocol botch: old import");
350 if(readn(netfd, key, 4) != 4)
351 fatal("can't read key part; %r");
353 if(write(netfd, key+12, 4) != 4)
354 fatal("can't write key part; %r");
356 /* scramble into two secrets */
357 sha1(key, sizeof(key), digest, nil);
358 mksecret(fromclientsecret, digest);
359 mksecret(fromserversecret, digest+10);
362 netfd = filter(netfd, filterp);
366 netfd = pushssl(netfd, ealgs, fromserversecret,
367 fromclientsecret, nil);
371 fatal("Unsupported encryption protocol");
375 fatal("can't establish ssl connection: %r");
379 fatal("Protocol botch: don't know how to deal with this");
380 netfd = filter(netfd, filterp);
387 * Start serving file requests from the network
392 fatal("Out of service buffers");
394 while((n = localread9pmsg(netfd, r->buf, messagesize, ini)) == 0)
398 if(convM2S(r->buf, n, &r->work) == 0)
399 fatal("convM2S format error");
401 DEBUG(DFD, "%F\n", &r->work);
402 (fcalls[r->work.type])(r);
408 * WARNING: Replace this with the original version as soon as all
409 * _old_ imports have been replaced with negotiating imports. Also
410 * cpu relies on this (which needs to be fixed!) -- pb.
413 localread9pmsg(int fd, void *abuf, uint n, void *ini)
422 memcpy(buf, ini, BIT32SZ);
424 m = readn(fd, buf, BIT32SZ);
433 if(len <= BIT32SZ || len > n){
434 werrstr("bad length in 9P2000 message header");
438 m = readn(fd, buf+BIT32SZ, len);
444 reply(Fcall *r, Fcall *t, char *err)
456 t->type = r->type + 1;
458 DEBUG(DFD, "\t%F\n", t);
460 data = malloc(messagesize); /* not mallocz; no need to clear */
463 n = convS2M(t, data, messagesize);
464 if(write(netfd, data, n)!=n){
465 syslog(0, "exportfs", "short write: %r");
466 fatal("mount write");
476 for(f = fidhash(nr); f; f = f->next)
490 for(f = *l; f; f = f->next) {
493 snprint(buf, sizeof(buf), "/mnt/exportfs/%d", f->mid);
523 for(new = *l; new; new = new->next)
528 fidfree = emallocz(sizeof(Fid) * Fidchunk);
530 for(i = 0; i < Fidchunk-1; i++)
531 fidfree[i].next = &fidfree[i+1];
533 fidfree[Fidchunk-1].next = 0;
539 memset(new, 0, sizeof(Fid));
555 int small_instead_of_fast = 1;
557 if(small_instead_of_fast)
558 ap = 0; /* so we always start looking at the beginning and reuse buffers */
560 for(rounds = 0; rounds < 10; rounds++) {
561 for(look = 0; look < Nr_workbufs; look++) {
562 if(++ap == Nr_workbufs)
564 if(Workq[ap].busy == 0)
568 if(look == Nr_workbufs){
576 wb->flushtag = NOTAG;
578 if(wb->buf == nil) /* allocate buffers dynamically to keep size down */
579 wb->buf = emallocz(messagesize);
582 fatal("No more work buffers");
589 File *parent, *child;
596 if(f->ref < 0) abort();
597 DEBUG(DFD, "free %s\n", f->name);
598 /* delete from parent */
600 if(parent->child == f)
601 parent->child = f->childlist;
603 for(child=parent->child; child->childlist!=f; child=child->childlist)
604 if(child->childlist == nil)
605 fatal("bad child list");
606 child->childlist = f->childlist;
618 file(File *parent, char *name)
624 DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
626 path = makepath(parent, name);
627 if(patternfile != nil && excludefile(path)){
636 for(f = parent->child; f; f = f->childlist)
637 if(strcmp(name, f->name) == 0)
641 f = emallocz(sizeof(File));
642 f->name = estrdup(name);
645 f->childlist = parent->child;
652 f->qid.type = dir->qid.type;
653 f->qid.vers = dir->qid.vers;
654 f->qidt = uniqueqid(dir);
655 f->qid.path = f->qidt->uniqpath;
669 root = emallocz(sizeof(File));
670 root->name = estrdup(".");
672 dir = dirstat(root->name);
677 root->qid.vers = dir->qid.vers;
678 root->qidt = uniqueqid(dir);
679 root->qid.path = root->qidt->uniqpath;
680 root->qid.type = QTDIR;
683 psmpt = emallocz(sizeof(File));
684 psmpt->name = estrdup("/");
686 dir = dirstat(psmpt->name);
691 psmpt->qid.vers = dir->qid.vers;
692 psmpt->qidt = uniqueqid(dir);
693 psmpt->qid.path = psmpt->qidt->uniqpath;
696 psmpt = file(psmpt, "mnt");
699 psmpt = file(psmpt, "exportfs");
703 makepath(File *p, char *name)
706 char *c, *s, *path, *seg[256];
710 for(i = 1; i < 256 && p; i++, p = p->parent){
712 n += strlen(p->name)+1;
716 fatal("out of memory");
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);
818 q = mallocz(sizeof(Qidtab), 1);
820 fatal("no memory for qid table");
825 q->path = d->qid.path;
827 h = qidhash(d->qid.path);
842 vsnprint(buf, ERRMAX, s, arg);
846 /* Clear away the slave children */
847 for(m = Proclist; m; m = m->next)
848 postnote(PNPROC, m->pid, "kill");
851 DEBUG(DFD, "%s\n", buf);
852 sysfatal("%s", buf); /* caution: buf could contain '%' */
865 setmalloctag(p, getcallerpc(&n));
877 setmalloctag(t, getcallerpc(&s));
881 /* Network on fd1, mount driver on fd0 */
883 filter(int fd, char *cmd)
885 char buf[128], devdir[40], *s, *file, *argv[16];
886 int p[2], lfd, len, argc;
888 /* Get a free port and post it to the client. */
889 if (announce(anstring, devdir) < 0)
890 fatal("filter: Cannot announce %s: %r", anstring);
892 snprint(buf, sizeof(buf), "%s/local", devdir);
893 if ((lfd = open(buf, OREAD)) < 0)
894 fatal("filter: Cannot open %s: %r", buf);
895 if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
896 fatal("filter: Cannot read %s: %r", buf);
899 if ((s = strchr(buf, '\n')) != nil)
901 if (write(fd, buf, len) != len)
902 fatal("filter: cannot write port; %r");
904 snprint(buf, sizeof(buf), "%s", cmd);
905 argc = tokenize(buf, argv, nelem(argv)-2);
907 fatal("filter: empty command");
908 argv[argc++] = devdir;
911 if (s = strrchr(argv[0], '/'))
915 fatal("filter: pipe; %r");
917 switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
919 fatal("filter: rfork; %r\n");
921 if (dup(p[0], 1) < 0)
922 fatal("filter: Cannot dup to 1; %r");
923 if (dup(p[0], 0) < 0)
924 fatal("filter: Cannot dup to 0; %r");
928 fatal("filter: exec; %r");
937 mksecret(char *t, uchar *f)
939 sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
940 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);