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 */
48 char *ealgs = "rc4_256 sha1";
49 char *aanfilter = "/bin/aan";
50 int encproto = Encnone;
53 static void mksecret(char *, uchar *);
54 static char *anstring = "tcp!*!0";
56 char *netdir = "", *local = "", *remote = "";
58 void filter(int, char *, char *);
63 fprint(2, "usage: %s [-adnsR] [-f dbgfile] [-m msize] [-r root] "
64 "[-S srvfile] [-e 'crypt hash'] [-P exclusion-file] "
65 "[-A announce-string] [-B address]\n", argv0);
74 nci = getnetconninfo(nil, fd);
77 netdir = estrdup(nci->dir);
78 local = estrdup(nci->lsys);
79 remote = estrdup(nci->rsys);
84 main(int argc, char **argv)
86 char buf[ERRMAX], ebuf[ERRMAX], initial[4], *ini, *srvfdfile;
87 char *dbfile, *srv, *na, *nsfile, *keyspec;
92 dbfile = "/tmp/exportdb";
112 ealgs = EARGF(usage());
113 if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
118 dbfile = EARGF(usage());
122 keyspec = EARGF(usage());
126 messagesize = strtoul(EARGF(usage()), nil, 0);
134 srv = EARGF(usage());
142 anstring = EARGF(usage());
150 /* accepted but ignored, for backwards compatibility */
154 nsfile = EARGF(usage());
158 patternfile = EARGF(usage());
168 srvfdfile = EARGF(usage());
176 if(na == nil && doauth){
178 * We use p9any so we don't have to visit this code again, with the
179 * cost that this code is incompatible with the old world, which
180 * requires p9sk2. (The two differ in who talks first, so compatibility
183 ai = auth_proxy(0, auth_getkey, "proto=p9any role=server %s", keyspec);
185 fatal("auth_proxy: %r");
186 if(nonone && strcmp(ai->cuid, "none") == 0)
187 fatal("exportfs by none disallowed");
188 if(auth_chuid(ai, nsfile) < 0)
189 fatal("auth_chuid: %r");
190 else { /* chown network connection */
197 putenv("service", "exportfs");
200 if(srvfdfile != nil){
201 if((srvfd = open(srvfdfile, ORDWR)) < 0)
202 fatal("open %s: %r", srvfdfile);
207 fatal("-B requires -s");
211 if((fd = dial(netmkaddr(na, 0, "importfs"), 0, 0, 0)) < 0)
212 fatal("can't dial %s: %r", na);
214 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
226 n = create(dbfile, OWRITE|OTRUNC, 0666);
231 if(srvfd >= 0 && srv != nil){
232 fprint(2, "exportfs: -S cannot be used with -r or -s\n");
236 DEBUG(DFD, "exportfs: started\n");
238 rfork(RFNOTEG|RFREND);
240 if(messagesize == 0){
241 messagesize = iounit(0);
243 messagesize = 8192+IOHDRSZ;
245 fhash = emallocz(sizeof(Fid*)*FHASHSIZE);
247 fmtinstall('F', fcallfmt);
250 * Get tree to serve from network connection,
251 * check we can get there and ack the connection
256 else if(srv != nil) {
259 errstr(ebuf, sizeof ebuf);
263 r->work.type = Rerror;
264 r->work.ename = ebuf;
265 n = convS2M(&r->work, r->buf, messagesize);
267 DEBUG(DFD, "chdir(\"%s\"): %s\n", srv, ebuf);
270 DEBUG(DFD, "invoked as server for %s", srv);
271 strncpy(buf, srv, sizeof buf);
276 n = read(0, buf, sizeof(buf)-1);
278 errstr(buf, sizeof buf);
279 fprint(0, "read(0): %s\n", buf);
280 DEBUG(DFD, "read(0): %s\n", buf);
285 errstr(ebuf, sizeof ebuf);
286 fprint(0, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
287 DEBUG(DFD, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
292 DEBUG(DFD, "\niniting root\n");
295 DEBUG(DFD, "exportfs: %s\n", buf);
297 if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
298 fatal("open ack write");
301 n = readn(0, initial, sizeof(initial));
303 fatal(nil); /* port scan or spurious open/close on exported /srv file (unmount) */
304 if(n < sizeof(initial))
305 fatal("can't read initial string: %r");
307 if(memcmp(ini, "impo", 4) == 0) {
308 char buf[128], *p, *args[3];
313 if((n = read(0, p, 1)) < 0)
314 fatal("can't read impo arguments: %r");
316 fatal("connection closed while reading arguments");
321 if(p >= buf + sizeof(buf))
322 fatal("import parameters too long");
325 if(tokenize(buf, args, nelem(args)) != 2)
326 fatal("impo arguments invalid: impo%s...", buf);
328 if(strcmp(args[0], "aan") == 0)
330 else if(strcmp(args[0], "nofilter") != 0)
331 fatal("import filter argument unsupported: %s", args[0]);
333 if(strcmp(args[1], "ssl") == 0)
335 else if(strcmp(args[1], "tls") == 0)
337 else if(strcmp(args[1], "clear") != 0)
338 fatal("import encryption proto unsupported: %s", args[1]);
340 if(encproto == Enctls)
341 fatal("%s: tls has not yet been implemented", argv[0]);
344 if(encproto != Encnone && ealgs != nil && ai != nil) {
345 uchar key[16], digest[SHA1dlen];
346 char fromclientsecret[21];
347 char fromserversecret[21];
351 fatal("secret too small for ssl");
352 memmove(key+4, ai->secret, 8);
354 /* exchange random numbers */
356 for(i = 0; i < 4; i++)
360 fatal("Protocol botch: old import");
361 if(readn(0, key, 4) != 4)
362 fatal("can't read key part; %r");
364 if(write(0, key+12, 4) != 4)
365 fatal("can't write key part; %r");
367 /* scramble into two secrets */
368 sha1(key, sizeof(key), digest, nil);
369 mksecret(fromclientsecret, digest);
370 mksecret(fromserversecret, digest+10);
373 filter(0, filterp, na);
377 fd = pushssl(0, ealgs, fromserversecret, fromclientsecret, nil);
379 fatal("can't establish ssl connection: %r");
387 fatal("Unsupported encryption protocol");
390 else if(filterp != nil) {
392 fatal("Protocol botch: don't know how to deal with this");
393 filter(0, filterp, na);
402 memmove(r->buf, ini, BIT32SZ);
404 if(n <= BIT32SZ || n > messagesize)
405 fatal("bad length in 9P2000 message header");
407 if(readn(0, r->buf+BIT32SZ, n) != n)
414 * Start serving file requests from the network
418 n = read9pmsg(0, r->buf, messagesize);
422 if(convM2S(r->buf, n, &r->work) != n)
423 fatal("convM2S format error");
425 DEBUG(DFD, "%F\n", &r->work);
426 (fcalls[r->work.type])(r);
431 reply(Fcall *r, Fcall *t, char *err)
443 t->type = r->type + 1;
445 DEBUG(DFD, "\t%F\n", t);
447 data = malloc(messagesize); /* not mallocz; no need to clear */
450 n = convS2M(t, data, messagesize);
451 if(write(0, data, n) != n){
452 /* not fatal, might have got a note due to flush */
453 fprint(2, "exportfs: short write in reply: %r\n");
463 for(f = fidhash(nr); f != nil; f = f->next)
477 for(f = *l; f != nil; f = f->next) {
480 snprint(buf, sizeof(buf), "/mnt/exportfs/%d", f->mid);
510 for(new = *l; new != nil; new = new->next)
515 fidfree = emallocz(sizeof(Fid) * Fidchunk);
517 for(i = 0; i < Fidchunk-1; i++)
518 fidfree[i].next = &fidfree[i+1];
520 fidfree[Fidchunk-1].next = nil;
526 memset(new, 0, sizeof(Fid));
553 sbufalloc.free = w->next;
560 w = emallocz(sizeof(*w) + messagesize);
571 w->next = sbufalloc.free;
580 File *parent, *child;
582 while(--f->ref == 0){
584 DEBUG(DFD, "free %s\n", f->name);
585 /* delete from parent */
587 if(parent->child == f)
588 parent->child = f->childlist;
590 for(child = parent->child; child->childlist != f; child = child->childlist) {
591 if(child->childlist == nil)
592 fatal("bad child list");
594 child->childlist = f->childlist;
604 file(File *parent, char *name)
610 DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
612 path = makepath(parent, name);
613 if(patternfile != nil && excludefile(path)){
622 for(f = parent->child; f != nil; f = f->childlist)
623 if(strcmp(name, f->name) == 0)
627 f = emallocz(sizeof(File));
628 f->name = estrdup(name);
631 f->childlist = parent->child;
638 f->qid.type = dir->qid.type;
639 f->qid.vers = dir->qid.vers;
640 f->qidt = uniqueqid(dir);
641 f->qid.path = f->qidt->uniqpath;
655 root = emallocz(sizeof(File));
656 root->name = estrdup(".");
658 dir = dirstat(root->name);
663 root->qid.vers = dir->qid.vers;
664 root->qidt = uniqueqid(dir);
665 root->qid.path = root->qidt->uniqpath;
666 root->qid.type = QTDIR;
669 psmpt = emallocz(sizeof(File));
670 psmpt->name = estrdup("/");
672 dir = dirstat(psmpt->name);
677 psmpt->qid.vers = dir->qid.vers;
678 psmpt->qidt = uniqueqid(dir);
679 psmpt->qid.path = psmpt->qidt->uniqpath;
682 psmpt = file(psmpt, "mnt");
685 psmpt = file(psmpt, "exportfs");
689 makepath(File *p, char *name)
692 char *c, *s, *path, *seg[256];
696 for(i = 1; i < 256 && p; i++, p = p->parent){
698 n += strlen(p->name)+1;
704 for(c = seg[i]; *c; c++)
721 for(n=0; n<64; n+=Nqidbits){
725 return h & (Nqidtab-1);
737 h = qidhash(q->path);
741 for(l=qidtab[h]; l->next!=q; l=l->next)
743 fatal("bad qid list");
755 h = qidhash(d->qid.path);
756 for(q=qidtab[h]; q!=nil; q=q->next)
757 if(q->type==d->type && q->dev==d->dev && q->path==d->qid.path)
763 qidexists(vlong path)
768 for(h=0; h<Nqidtab; h++)
769 for(q=qidtab[h]; q!=nil; q=q->next)
770 if(q->uniqpath == path)
788 while(qidexists(path)){
789 DEBUG(DFD, "collision on %s\n", d->name);
790 /* collision: find a new one */
794 if(newqid >= (1<<16)){
795 DEBUG(DFD, "collision wraparound\n");
799 DEBUG(DFD, "assign qid %.16llux\n", path);
802 q = emallocz(sizeof(Qidtab));
806 q->path = d->qid.path;
808 h = qidhash(d->qid.path);
823 vsnprint(buf, ERRMAX, s, arg);
827 /* Clear away the slave children */
828 for(m = Proclist; m != nil; m = m->next)
829 postnote(PNPROC, m->pid, "kill");
832 DEBUG(DFD, "%s\n", buf);
833 sysfatal("%s", buf); /* caution: buf could contain '%' */
846 setmalloctag(p, getcallerpc(&n));
858 setmalloctag(t, getcallerpc(&s));
863 filter(int fd, char *cmd, char *host)
865 char addr[128], buf[256], *s, *file, *argv[16];
866 int lfd, p[2], len, argc;
869 /* Get a free port and post it to the client. */
870 if (announce(anstring, addr) < 0)
871 fatal("filter: Cannot announce %s: %r", anstring);
873 snprint(buf, sizeof(buf), "%s/local", addr);
874 if ((lfd = open(buf, OREAD)) < 0)
875 fatal("filter: Cannot open %s: %r", buf);
876 if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
877 fatal("filter: Cannot read %s: %r", buf);
880 if ((s = strchr(buf, '\n')) != nil)
882 if (write(fd, buf, len) != len)
883 fatal("filter: cannot write port; %r");
885 /* Read address string from connection */
886 if ((len = read(fd, buf, sizeof buf - 1)) < 0)
887 sysfatal("filter: cannot write port; %r");
890 if ((s = strrchr(buf, '!')) == nil)
891 sysfatal("filter: illegally formatted port %s", buf);
892 strecpy(addr, addr+sizeof(addr), netmkaddr(host, "tcp", s+1));
893 strecpy(strrchr(addr, '!'), addr+sizeof(addr), s);
896 DEBUG(DFD, "filter: %s\n", addr);
898 snprint(buf, sizeof(buf), "%s", cmd);
899 argc = tokenize(buf, argv, nelem(argv)-3);
901 sysfatal("filter: empty command");
909 if((s = strrchr(argv[0], '/')) != nil)
913 sysfatal("pipe: %r");
915 switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
917 fatal("filter: rfork; %r\n");
920 if (dup(p[0], 1) < 0)
921 fatal("filter: Cannot dup to 1; %r");
922 if (dup(p[0], 0) < 0)
923 fatal("filter: Cannot dup to 0; %r");
927 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]);