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 putenv("service", "exportfs");
193 if(srvfdfile != nil){
194 if((srvfd = open(srvfdfile, ORDWR)) < 0)
195 fatal("open %s: %r", srvfdfile);
200 fatal("-B requires -s");
204 if((fd = dial(netmkaddr(na, 0, "importfs"), 0, 0, 0)) < 0)
205 fatal("can't dial %s: %r", na);
207 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
219 n = create(dbfile, OWRITE|OTRUNC, 0666);
224 if(srvfd >= 0 && srv != nil){
225 fprint(2, "exportfs: -S cannot be used with -r or -s\n");
229 DEBUG(DFD, "exportfs: started\n");
231 rfork(RFNOTEG|RFREND);
233 if(messagesize == 0){
234 messagesize = iounit(0);
236 messagesize = 8192+IOHDRSZ;
238 fhash = emallocz(sizeof(Fid*)*FHASHSIZE);
240 fmtinstall('F', fcallfmt);
243 * Get tree to serve from network connection,
244 * check we can get there and ack the connection
249 else if(srv != nil) {
252 errstr(ebuf, sizeof ebuf);
256 r->work.type = Rerror;
257 r->work.ename = ebuf;
258 n = convS2M(&r->work, r->buf, messagesize);
260 DEBUG(DFD, "chdir(\"%s\"): %s\n", srv, ebuf);
263 DEBUG(DFD, "invoked as server for %s", srv);
264 strncpy(buf, srv, sizeof buf);
269 n = read(0, buf, sizeof(buf)-1);
271 errstr(buf, sizeof buf);
272 fprint(0, "read(0): %s\n", buf);
273 DEBUG(DFD, "read(0): %s\n", buf);
278 errstr(ebuf, sizeof ebuf);
279 fprint(0, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
280 DEBUG(DFD, "chdir(%d:\"%s\"): %s\n", n, buf, ebuf);
285 DEBUG(DFD, "\niniting root\n");
288 DEBUG(DFD, "exportfs: %s\n", buf);
290 if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
291 fatal("open ack write");
294 n = readn(0, initial, sizeof(initial));
296 fatal(nil); /* port scan or spurious open/close on exported /srv file (unmount) */
297 if(n < sizeof(initial))
298 fatal("can't read initial string: %r");
300 if(memcmp(ini, "impo", 4) == 0) {
301 char buf[128], *p, *args[3];
306 if((n = read(0, p, 1)) < 0)
307 fatal("can't read impo arguments: %r");
309 fatal("connection closed while reading arguments");
314 if(p >= buf + sizeof(buf))
315 fatal("import parameters too long");
318 if(tokenize(buf, args, nelem(args)) != 2)
319 fatal("impo arguments invalid: impo%s...", buf);
321 if(strcmp(args[0], "aan") == 0)
323 else if(strcmp(args[0], "nofilter") != 0)
324 fatal("import filter argument unsupported: %s", args[0]);
326 if(strcmp(args[1], "ssl") == 0)
328 else if(strcmp(args[1], "tls") == 0)
330 else if(strcmp(args[1], "clear") != 0)
331 fatal("import encryption proto unsupported: %s", args[1]);
333 if(encproto == Enctls)
334 fatal("%s: tls has not yet been implemented", argv[0]);
337 if(encproto != Encnone && ealgs != nil && ai != nil) {
338 uchar key[16], digest[SHA1dlen];
339 char fromclientsecret[21];
340 char fromserversecret[21];
344 fatal("secret too small for ssl");
345 memmove(key+4, ai->secret, 8);
347 /* exchange random numbers */
349 for(i = 0; i < 4; i++)
353 fatal("Protocol botch: old import");
354 if(readn(0, key, 4) != 4)
355 fatal("can't read key part; %r");
357 if(write(0, key+12, 4) != 4)
358 fatal("can't write key part; %r");
360 /* scramble into two secrets */
361 sha1(key, sizeof(key), digest, nil);
362 mksecret(fromclientsecret, digest);
363 mksecret(fromserversecret, digest+10);
366 filter(0, filterp, na);
370 fd = pushssl(0, ealgs, fromserversecret, fromclientsecret, nil);
372 fatal("can't establish ssl connection: %r");
380 fatal("Unsupported encryption protocol");
383 else if(filterp != nil) {
385 fatal("Protocol botch: don't know how to deal with this");
386 filter(0, filterp, na);
395 memmove(r->buf, ini, BIT32SZ);
397 if(n <= BIT32SZ || n > messagesize)
398 fatal("bad length in 9P2000 message header");
400 if(readn(0, r->buf+BIT32SZ, n) != n)
407 * Start serving file requests from the network
411 n = read9pmsg(0, r->buf, messagesize);
415 if(convM2S(r->buf, n, &r->work) != n)
416 fatal("convM2S format error");
418 DEBUG(DFD, "%F\n", &r->work);
419 (fcalls[r->work.type])(r);
424 reply(Fcall *r, Fcall *t, char *err)
436 t->type = r->type + 1;
438 DEBUG(DFD, "\t%F\n", t);
440 data = malloc(messagesize); /* not mallocz; no need to clear */
443 n = convS2M(t, data, messagesize);
444 if(write(0, data, n) != n){
445 /* not fatal, might have got a note due to flush */
446 fprint(2, "exportfs: short write in reply: %r\n");
456 for(f = fidhash(nr); f != nil; f = f->next)
470 for(f = *l; f != nil; f = f->next) {
473 snprint(buf, sizeof(buf), "/mnt/exportfs/%d", f->mid);
503 for(new = *l; new != nil; new = new->next)
508 fidfree = emallocz(sizeof(Fid) * Fidchunk);
510 for(i = 0; i < Fidchunk-1; i++)
511 fidfree[i].next = &fidfree[i+1];
513 fidfree[Fidchunk-1].next = nil;
519 memset(new, 0, sizeof(Fid));
546 sbufalloc.free = w->next;
553 w = emallocz(sizeof(*w) + messagesize);
564 w->next = sbufalloc.free;
573 File *parent, *child;
575 while(--f->ref == 0){
577 DEBUG(DFD, "free %s\n", f->name);
578 /* delete from parent */
580 if(parent->child == f)
581 parent->child = f->childlist;
583 for(child = parent->child; child->childlist != f; child = child->childlist) {
584 if(child->childlist == nil)
585 fatal("bad child list");
587 child->childlist = f->childlist;
597 file(File *parent, char *name)
603 DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
605 path = makepath(parent, name);
606 if(patternfile != nil && excludefile(path)){
615 for(f = parent->child; f != nil; f = f->childlist)
616 if(strcmp(name, f->name) == 0)
620 f = emallocz(sizeof(File));
621 f->name = estrdup(name);
624 f->childlist = parent->child;
631 f->qid.type = dir->qid.type;
632 f->qid.vers = dir->qid.vers;
633 f->qidt = uniqueqid(dir);
634 f->qid.path = f->qidt->uniqpath;
648 root = emallocz(sizeof(File));
649 root->name = estrdup(".");
651 dir = dirstat(root->name);
656 root->qid.vers = dir->qid.vers;
657 root->qidt = uniqueqid(dir);
658 root->qid.path = root->qidt->uniqpath;
659 root->qid.type = QTDIR;
662 psmpt = emallocz(sizeof(File));
663 psmpt->name = estrdup("/");
665 dir = dirstat(psmpt->name);
670 psmpt->qid.vers = dir->qid.vers;
671 psmpt->qidt = uniqueqid(dir);
672 psmpt->qid.path = psmpt->qidt->uniqpath;
675 psmpt = file(psmpt, "mnt");
678 psmpt = file(psmpt, "exportfs");
682 makepath(File *p, char *name)
685 char *c, *s, *path, *seg[256];
689 for(i = 1; i < 256 && p; i++, p = p->parent){
691 n += strlen(p->name)+1;
697 for(c = seg[i]; *c; c++)
714 for(n=0; n<64; n+=Nqidbits){
718 return h & (Nqidtab-1);
730 h = qidhash(q->path);
734 for(l=qidtab[h]; l->next!=q; l=l->next)
736 fatal("bad qid list");
748 h = qidhash(d->qid.path);
749 for(q=qidtab[h]; q!=nil; q=q->next)
750 if(q->type==d->type && q->dev==d->dev && q->path==d->qid.path)
756 qidexists(vlong path)
761 for(h=0; h<Nqidtab; h++)
762 for(q=qidtab[h]; q!=nil; q=q->next)
763 if(q->uniqpath == path)
781 while(qidexists(path)){
782 DEBUG(DFD, "collision on %s\n", d->name);
783 /* collision: find a new one */
787 if(newqid >= (1<<16)){
788 DEBUG(DFD, "collision wraparound\n");
792 DEBUG(DFD, "assign qid %.16llux\n", path);
795 q = emallocz(sizeof(Qidtab));
799 q->path = d->qid.path;
801 h = qidhash(d->qid.path);
816 vsnprint(buf, ERRMAX, s, arg);
820 /* Clear away the slave children */
821 for(m = Proclist; m != nil; m = m->next)
822 postnote(PNPROC, m->pid, "kill");
825 DEBUG(DFD, "%s\n", buf);
826 sysfatal("%s", buf); /* caution: buf could contain '%' */
839 setmalloctag(p, getcallerpc(&n));
851 setmalloctag(t, getcallerpc(&s));
856 filter(int fd, char *cmd, char *host)
858 char addr[128], buf[256], *s, *file, *argv[16];
859 int lfd, p[2], len, argc;
862 /* Get a free port and post it to the client. */
863 if (announce(anstring, addr) < 0)
864 fatal("filter: Cannot announce %s: %r", anstring);
866 snprint(buf, sizeof(buf), "%s/local", addr);
867 if ((lfd = open(buf, OREAD)) < 0)
868 fatal("filter: Cannot open %s: %r", buf);
869 if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
870 fatal("filter: Cannot read %s: %r", buf);
873 if ((s = strchr(buf, '\n')) != nil)
875 if (write(fd, buf, len) != len)
876 fatal("filter: cannot write port; %r");
878 /* Read address string from connection */
879 if ((len = read(fd, buf, sizeof buf - 1)) < 0)
880 sysfatal("filter: cannot write port; %r");
883 if ((s = strrchr(buf, '!')) == nil)
884 sysfatal("filter: illegally formatted port %s", buf);
885 strecpy(addr, addr+sizeof(addr), netmkaddr(host, "tcp", s+1));
886 strecpy(strrchr(addr, '!'), addr+sizeof(addr), s);
889 DEBUG(DFD, "filter: %s\n", addr);
891 snprint(buf, sizeof(buf), "%s", cmd);
892 argc = tokenize(buf, argv, nelem(argv)-3);
894 sysfatal("filter: empty command");
902 if((s = strrchr(argv[0], '/')) != nil)
906 sysfatal("pipe: %r");
908 switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
910 fatal("filter: rfork; %r\n");
913 if (dup(p[0], 1) < 0)
914 fatal("filter: Cannot dup to 1; %r");
915 if (dup(p[0], 0) < 0)
916 fatal("filter: Cannot dup to 0; %r");
920 fatal("filter: exec; %r");
929 mksecret(char *t, uchar *f)
931 sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
932 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);