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 DEBUG(DFD, "invoked as server for %s", srv);
256 strncpy(buf, srv, sizeof buf);
261 n = read(0, buf, sizeof(buf)-1);
263 errstr(buf, sizeof buf);
264 fprint(0, "read(0): %s", buf);
265 DEBUG(DFD, "read(0): %s", buf);
270 errstr(ebuf, sizeof ebuf);
271 fprint(0, "chdir(%d:\"%s\"): %s", n, buf, ebuf);
272 DEBUG(DFD, "chdir(%d:\"%s\"): %s", n, buf, ebuf);
277 DEBUG(DFD, "\niniting root\n");
280 DEBUG(DFD, "exportfs: %s\n", buf);
282 if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
283 fatal("open ack write");
286 if (readn(netfd, initial, sizeof(initial)) < sizeof(initial))
287 fatal("can't read initial string: %r");
289 if (memcmp(ini, "impo", 4) == 0) {
290 char buf[128], *p, *args[3];
295 if ((n = read(netfd, p, 1)) < 0)
296 fatal("can't read impo arguments: %r");
298 fatal("connection closed while reading arguments");
303 if(p >= buf + sizeof(buf))
304 fatal("import parameters too long");
307 if (tokenize(buf, args, nelem(args)) != 2)
308 fatal("impo arguments invalid: impo%s...", buf);
310 if (strcmp(args[0], "aan") == 0)
312 else if (strcmp(args[0], "nofilter") != 0)
313 fatal("import filter argument unsupported: %s", args[0]);
315 if (strcmp(args[1], "ssl") == 0)
317 else if (strcmp(args[1], "tls") == 0)
319 else if (strcmp(args[1], "clear") != 0)
320 fatal("import encryption proto unsupported: %s", args[1]);
322 if (encproto == Enctls)
323 fatal("%s: tls has not yet been implemented", argv[0]);
326 if (encproto != Encnone && ealgs && ai) {
327 uchar key[16], digest[SHA1dlen];
328 char fromclientsecret[21];
329 char fromserversecret[21];
332 assert(ai->nsecret <= sizeof(key)-4);
333 memmove(key+4, ai->secret, ai->nsecret);
335 /* exchange random numbers */
337 for(i = 0; i < 4; i++)
341 fatal("Protocol botch: old import");
342 if(readn(netfd, key, 4) != 4)
343 fatal("can't read key part; %r");
345 if(write(netfd, key+12, 4) != 4)
346 fatal("can't write key part; %r");
348 /* scramble into two secrets */
349 sha1(key, sizeof(key), digest, nil);
350 mksecret(fromclientsecret, digest);
351 mksecret(fromserversecret, digest+10);
354 netfd = filter(netfd, filterp);
358 netfd = pushssl(netfd, ealgs, fromserversecret,
359 fromclientsecret, nil);
363 fatal("Unsupported encryption protocol");
367 fatal("can't establish ssl connection: %r");
371 fatal("Protocol botch: don't know how to deal with this");
372 netfd = filter(netfd, filterp);
379 * Start serving file requests from the network
384 fatal("Out of service buffers");
386 n = localread9pmsg(netfd, r->buf, messagesize, ini);
389 if(convM2S(r->buf, n, &r->work) == 0)
390 fatal("convM2S format error");
392 DEBUG(DFD, "%F\n", &r->work);
393 (fcalls[r->work.type])(r);
399 * WARNING: Replace this with the original version as soon as all
400 * _old_ imports have been replaced with negotiating imports. Also
401 * cpu relies on this (which needs to be fixed!) -- pb.
404 localread9pmsg(int fd, void *abuf, uint n, void *ini)
413 memcpy(buf, ini, BIT32SZ);
415 m = readn(fd, buf, BIT32SZ);
424 if(len <= BIT32SZ || len > n){
425 werrstr("bad length in 9P2000 message header");
429 m = readn(fd, buf+BIT32SZ, len);
435 reply(Fcall *r, Fcall *t, char *err)
447 t->type = r->type + 1;
449 DEBUG(DFD, "\t%F\n", t);
451 data = malloc(messagesize); /* not mallocz; no need to clear */
454 n = convS2M(t, data, messagesize);
455 if(write(netfd, data, n)!=n){
456 syslog(0, "exportfs", "short write: %r");
457 fatal("mount write");
467 for(f = fidhash(nr); f; f = f->next)
481 for(f = *l; f; f = f->next) {
484 snprint(buf, sizeof(buf), "/mnt/exportfs/%d", f->mid);
514 for(new = *l; new; new = new->next)
519 fidfree = emallocz(sizeof(Fid) * Fidchunk);
521 for(i = 0; i < Fidchunk-1; i++)
522 fidfree[i].next = &fidfree[i+1];
524 fidfree[Fidchunk-1].next = 0;
530 memset(new, 0, sizeof(Fid));
546 int small_instead_of_fast = 1;
548 if(small_instead_of_fast)
549 ap = 0; /* so we always start looking at the beginning and reuse buffers */
551 for(rounds = 0; rounds < 10; rounds++) {
552 for(look = 0; look < Nr_workbufs; look++) {
553 if(++ap == Nr_workbufs)
555 if(Workq[ap].busy == 0)
559 if(look == Nr_workbufs){
567 wb->flushtag = NOTAG;
569 if(wb->buf == nil) /* allocate buffers dynamically to keep size down */
570 wb->buf = emallocz(messagesize);
573 fatal("No more work buffers");
580 File *parent, *child;
587 if(f->ref < 0) abort();
588 DEBUG(DFD, "free %s\n", f->name);
589 /* delete from parent */
591 if(parent->child == f)
592 parent->child = f->childlist;
594 for(child=parent->child; child->childlist!=f; child=child->childlist)
595 if(child->childlist == nil)
596 fatal("bad child list");
597 child->childlist = f->childlist;
609 file(File *parent, char *name)
615 DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
617 path = makepath(parent, name);
618 if(patternfile != nil && excludefile(path)){
627 for(f = parent->child; f; f = f->childlist)
628 if(strcmp(name, f->name) == 0)
632 f = emallocz(sizeof(File));
633 f->name = estrdup(name);
636 f->childlist = parent->child;
643 f->qid.type = dir->qid.type;
644 f->qid.vers = dir->qid.vers;
645 f->qidt = uniqueqid(dir);
646 f->qid.path = f->qidt->uniqpath;
660 root = emallocz(sizeof(File));
661 root->name = estrdup(".");
663 dir = dirstat(root->name);
668 root->qid.vers = dir->qid.vers;
669 root->qidt = uniqueqid(dir);
670 root->qid.path = root->qidt->uniqpath;
671 root->qid.type = QTDIR;
674 psmpt = emallocz(sizeof(File));
675 psmpt->name = estrdup("/");
677 dir = dirstat(psmpt->name);
682 psmpt->qid.vers = dir->qid.vers;
683 psmpt->qidt = uniqueqid(dir);
684 psmpt->qid.path = psmpt->qidt->uniqpath;
687 psmpt = file(psmpt, "mnt");
690 psmpt = file(psmpt, "exportfs");
694 makepath(File *p, char *name)
697 char *c, *s, *path, *seg[256];
701 for(i = 1; i < 256 && p; i++, p = p->parent){
703 n += strlen(p->name)+1;
707 fatal("out of memory");
711 for(c = seg[i]; *c; c++)
728 for(n=0; n<64; n+=Nqidbits){
732 return h & (Nqidtab-1);
745 h = qidhash(q->path);
749 for(l=qidtab[h]; l->next!=q; l=l->next)
751 fatal("bad qid list");
763 h = qidhash(d->qid.path);
764 for(q=qidtab[h]; q!=nil; q=q->next)
765 if(q->type==d->type && q->dev==d->dev && q->path==d->qid.path)
771 qidexists(vlong path)
776 for(h=0; h<Nqidtab; h++)
777 for(q=qidtab[h]; q!=nil; q=q->next)
778 if(q->uniqpath == path)
796 while(qidexists(path)){
797 DEBUG(DFD, "collision on %s\n", d->name);
798 /* collision: find a new one */
802 if(newqid >= (1<<16)){
803 DEBUG(DFD, "collision wraparound\n");
807 DEBUG(DFD, "assign qid %.16llux\n", path);
809 q = mallocz(sizeof(Qidtab), 1);
811 fatal("no memory for qid table");
816 q->path = d->qid.path;
818 h = qidhash(d->qid.path);
833 vsnprint(buf, ERRMAX, s, arg);
837 /* Clear away the slave children */
838 for(m = Proclist; m; m = m->next)
839 postnote(PNPROC, m->pid, "kill");
841 DEBUG(DFD, "%s\n", buf);
843 sysfatal("%s", buf); /* caution: buf could contain '%' */
856 setmalloctag(p, getcallerpc(&n));
868 setmalloctag(t, getcallerpc(&s));
872 /* Network on fd1, mount driver on fd0 */
874 filter(int fd, char *cmd)
876 char buf[128], devdir[40], *s, *file, *argv[16];
877 int p[2], lfd, len, argc;
879 /* Get a free port and post it to the client. */
880 if (announce(anstring, devdir) < 0)
881 fatal("filter: Cannot announce %s: %r", anstring);
883 snprint(buf, sizeof(buf), "%s/local", devdir);
884 if ((lfd = open(buf, OREAD)) < 0)
885 fatal("filter: Cannot open %s: %r", buf);
886 if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
887 fatal("filter: Cannot read %s: %r", buf);
890 if ((s = strchr(buf, '\n')) != nil)
892 if (write(fd, buf, len) != len)
893 fatal("filter: cannot write port; %r");
895 snprint(buf, sizeof(buf), "%s", cmd);
896 argc = tokenize(buf, argv, nelem(argv)-2);
898 fatal("filter: empty command");
899 argv[argc++] = devdir;
902 if (s = strrchr(argv[0], '/'))
906 fatal("filter: pipe; %r");
908 switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG)) {
910 fatal("filter: rfork; %r\n");
912 if (dup(p[0], 1) < 0)
913 fatal("filter: Cannot dup to 1; %r");
914 if (dup(p[0], 0) < 0)
915 fatal("filter: Cannot dup to 0; %r");
919 fatal("filter: exec; %r");
928 mksecret(char *t, uchar *f)
930 sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
931 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);