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");
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;
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
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 && ai) {
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");
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 syslog(0, "exportfs", "short write: %r");
464 fatal("mount write");
474 for(f = fidhash(nr); f; f = f->next)
488 for(f = *l; f; f = f->next) {
491 snprint(buf, sizeof(buf), "/mnt/exportfs/%d", f->mid);
521 for(new = *l; new; 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 = 0;
537 memset(new, 0, sizeof(Fid));
564 sbufalloc.free = w->next;
571 w = emallocz(sizeof(*w) + messagesize);
586 w->next = sbufalloc.free;
595 File *parent, *child;
602 if(f->ref < 0) abort();
603 DEBUG(DFD, "free %s\n", f->name);
604 /* delete from parent */
606 if(parent->child == f)
607 parent->child = f->childlist;
609 for(child=parent->child; child->childlist!=f; child=child->childlist)
610 if(child->childlist == nil)
611 fatal("bad child list");
612 child->childlist = f->childlist;
624 file(File *parent, char *name)
630 DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
632 path = makepath(parent, name);
633 if(patternfile != nil && excludefile(path)){
642 for(f = parent->child; f; f = f->childlist)
643 if(strcmp(name, f->name) == 0)
647 f = emallocz(sizeof(File));
648 f->name = estrdup(name);
651 f->childlist = parent->child;
658 f->qid.type = dir->qid.type;
659 f->qid.vers = dir->qid.vers;
660 f->qidt = uniqueqid(dir);
661 f->qid.path = f->qidt->uniqpath;
675 root = emallocz(sizeof(File));
676 root->name = estrdup(".");
678 dir = dirstat(root->name);
683 root->qid.vers = dir->qid.vers;
684 root->qidt = uniqueqid(dir);
685 root->qid.path = root->qidt->uniqpath;
686 root->qid.type = QTDIR;
689 psmpt = emallocz(sizeof(File));
690 psmpt->name = estrdup("/");
692 dir = dirstat(psmpt->name);
697 psmpt->qid.vers = dir->qid.vers;
698 psmpt->qidt = uniqueqid(dir);
699 psmpt->qid.path = psmpt->qidt->uniqpath;
702 psmpt = file(psmpt, "mnt");
705 psmpt = file(psmpt, "exportfs");
709 makepath(File *p, char *name)
712 char *c, *s, *path, *seg[256];
716 for(i = 1; i < 256 && p; i++, p = p->parent){
718 n += strlen(p->name)+1;
724 for(c = seg[i]; *c; c++)
741 for(n=0; n<64; n+=Nqidbits){
745 return h & (Nqidtab-1);
758 h = qidhash(q->path);
762 for(l=qidtab[h]; l->next!=q; l=l->next)
764 fatal("bad qid list");
776 h = qidhash(d->qid.path);
777 for(q=qidtab[h]; q!=nil; q=q->next)
778 if(q->type==d->type && q->dev==d->dev && q->path==d->qid.path)
784 qidexists(vlong path)
789 for(h=0; h<Nqidtab; h++)
790 for(q=qidtab[h]; q!=nil; q=q->next)
791 if(q->uniqpath == path)
809 while(qidexists(path)){
810 DEBUG(DFD, "collision on %s\n", d->name);
811 /* collision: find a new one */
815 if(newqid >= (1<<16)){
816 DEBUG(DFD, "collision wraparound\n");
820 DEBUG(DFD, "assign qid %.16llux\n", path);
822 q = mallocz(sizeof(Qidtab), 1);
824 fatal("no memory for qid table");
829 q->path = d->qid.path;
831 h = qidhash(d->qid.path);
846 vsnprint(buf, ERRMAX, s, arg);
850 /* Clear away the slave children */
851 for(m = Proclist; m; m = m->next)
852 postnote(PNPROC, m->pid, "kill");
855 DEBUG(DFD, "%s\n", buf);
856 sysfatal("%s", buf); /* caution: buf could contain '%' */
869 setmalloctag(p, getcallerpc(&n));
881 setmalloctag(t, getcallerpc(&s));
885 /* Network on fd1, mount driver on fd0 */
887 filter(int fd, char *cmd)
889 char buf[128], devdir[40], *s, *file, *argv[16];
890 int p[2], lfd, len, argc;
892 /* Get a free port and post it to the client. */
893 if (announce(anstring, devdir) < 0)
894 fatal("filter: Cannot announce %s: %r", anstring);
896 snprint(buf, sizeof(buf), "%s/local", devdir);
897 if ((lfd = open(buf, OREAD)) < 0)
898 fatal("filter: Cannot open %s: %r", buf);
899 if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
900 fatal("filter: Cannot read %s: %r", buf);
903 if ((s = strchr(buf, '\n')) != nil)
905 if (write(fd, buf, len) != len)
906 fatal("filter: cannot write port; %r");
908 snprint(buf, sizeof(buf), "%s", cmd);
909 argc = tokenize(buf, argv, nelem(argv)-2);
911 fatal("filter: empty command");
912 argv[argc++] = devdir;
915 if (s = strrchr(argv[0], '/'))
919 fatal("filter: pipe; %r");
921 switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
923 fatal("filter: rfork; %r\n");
925 if (dup(p[0], 1) < 0)
926 fatal("filter: Cannot dup to 1; %r");
927 if (dup(p[0], 0) < 0)
928 fatal("filter: Cannot dup to 0; %r");
932 fatal("filter: exec; %r");
941 mksecret(char *t, uchar *f)
943 sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
944 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);