1 /* 2 * exportfs - Export a plan 9 name space across a network 3 */ 4 #include <u.h> 5 #include <libc.h> 6 #include <auth.h> 7 #include <fcall.h> 8 #include <libsec.h> 9 #define Extern 10 #include "exportfs.h" 11 12 #define QIDPATH ((1LL<<48)-1) 13 vlong newqid = 0; 14 15 enum { 16 Encnone, 17 Encssl, 18 Enctls, 19 }; 20 21 void (*fcalls[])(Fsrpc*) = 22 { 23 [Tversion] Xversion, 24 [Tauth] Xauth, 25 [Tflush] Xflush, 26 [Tattach] Xattach, 27 [Twalk] Xwalk, 28 [Topen] slave, 29 [Tcreate] Xcreate, 30 [Tclunk] Xclunk, 31 [Tread] slave, 32 [Twrite] slave, 33 [Tremove] Xremove, 34 [Tstat] Xstat, 35 [Twstat] Xwstat, 36 }; 37 38 /* accounting and debugging counters */ 39 int filecnt; 40 int freecnt; 41 int qidcnt; 42 int qfreecnt; 43 int ncollision; 44 45 int netfd; 46 int srvfd = -1; 47 int nonone = 1; 48 char *filterp; 49 char *ealgs = "rc4_256 sha1"; 50 char *aanfilter = "/bin/aan"; 51 int encproto = Encnone; 52 static void mksecret(char *, uchar *); 53 static int localread9pmsg(int, void *, uint, ulong *); 54 static char *anstring = "il!*!0"; 55 int filter(int, char *); 56 57 void 58 usage(void) 59 { 60 fprint(2, "usage: %s [-ads] [-f dbgfile] [-m msize] [-r root] [-S srvfile] [-e 'crypt hash'] [-A announce-string]\n", argv0); 61 fatal("usage"); 62 } 63 64 void 65 main(int argc, char **argv) 66 { 67 char buf[ERRMAX], ebuf[ERRMAX]; 68 Fsrpc *r; 69 int n; 70 char *dbfile, *srv, *file; 71 AuthInfo *ai; 72 ulong initial; 73 74 dbfile = "/tmp/exportdb"; 75 srv = nil; 76 srvfd = -1; 77 78 ai = nil; 79 ARGBEGIN{ 80 case 'a': 81 /* 82 * We use p9any so we don't have to visit this code again, with the 83 * cost that this code is incompatible with the old world, which 84 * requires p9sk2. (The two differ in who talks first, so compatibility 85 * is awkward.) 86 */ 87 ai = auth_proxy(0, auth_getkey, "proto=p9any role=server"); 88 if(ai == nil) 89 fatal("auth_proxy: %r"); 90 if(nonone && strcmp(ai->cuid, "none") == 0) 91 fatal("exportfs by none disallowed"); 92 if(auth_chuid(ai, nil) < 0) 93 fatal("auth_chuid: %r"); 94 if(newns(ai->cuid, 0) < 0) 95 fatal("newns"); 96 putenv("service", "exportfs"); 97 break; 98 99 case 'e': 100 ealgs = ARGF(); 101 if(ealgs == nil) 102 usage(); 103 if(*ealgs == 0 || strcmp(ealgs, "clear") == 0) 104 ealgs = nil; 105 break; 106 107 case 'S': 108 if(srvfd != -1) 109 usage(); 110 file = EARGF(usage()); 111 if((srvfd = open(file, ORDWR)) < 0) 112 sysfatal("open '%s': %r", file); 113 break; 114 115 case 'd': 116 dbg++; 117 break; 118 119 case 'f': 120 dbfile = EARGF(usage()); 121 break; 122 123 case 'F': 124 /* accepted but ignored, for backwards compatibility */ 125 break; 126 127 case 'm': 128 messagesize = strtoul(EARGF(usage()), nil, 0); 129 break; 130 131 case 'r': 132 srv = EARGF(usage()); 133 break; 134 135 case 's': 136 srv = "/"; 137 break; 138 139 case 'A': 140 anstring = EARGF(usage()); 141 break; 142 143 default: 144 usage(); 145 }ARGEND 146 USED(argc, argv); 147 148 if(dbg) { 149 n = create(dbfile, OWRITE|OTRUNC, 0666); 150 dup(n, DFD); 151 close(n); 152 } 153 154 if(srvfd >= 0 && srv){ 155 fprint(2, "exportfs: -S cannot be used with -r or -s\n"); 156 usage(); 157 } 158 159 DEBUG(DFD, "exportfs: started\n"); 160 161 rfork(RFNOTEG); 162 163 if(messagesize == 0){ 164 messagesize = iounit(netfd); 165 if(messagesize == 0) 166 messagesize = 8192+IOHDRSZ; 167 } 168 169 Workq = emallocz(sizeof(Fsrpc)*Nr_workbufs); 170 // for(i=0; i<Nr_workbufs; i++) 171 // Workq[i].buf = emallocz(messagesize); 172 fhash = emallocz(sizeof(Fid*)*FHASHSIZE); 173 174 fmtinstall('F', fcallfmt); 175 176 /* 177 * Get tree to serve from network connection, 178 * check we can get there and ack the connection 179 */ 180 if(srvfd != -1) { 181 /* do nothing */ 182 } 183 else if(srv) { 184 chdir(srv); 185 DEBUG(DFD, "invoked as server for %s", srv); 186 strncpy(buf, srv, sizeof buf); 187 } 188 else { 189 buf[0] = 0; 190 n = read(0, buf, sizeof(buf)-1); 191 if(n < 0) { 192 errstr(buf, sizeof buf); 193 fprint(0, "read(0): %s", buf); 194 DEBUG(DFD, "read(0): %s", buf); 195 exits(buf); 196 } 197 buf[n] = 0; 198 if(chdir(buf) < 0) { 199 errstr(ebuf, sizeof ebuf); 200 fprint(0, "chdir(%d:\"%s\"): %s", n, buf, ebuf); 201 DEBUG(DFD, "chdir(%d:\"%s\"): %s", n, buf, ebuf); 202 exits(ebuf); 203 } 204 } 205 206 DEBUG(DFD, "initing root\n"); 207 initroot(); 208 209 DEBUG(DFD, "exportfs: %s\n", buf); 210 211 if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2) 212 fatal("open ack write"); 213 214 if (readn(netfd, &initial, sizeof(ulong)) < sizeof(ulong)) 215 fatal("can't read initial string: %r\n"); 216 217 if (!strncmp((char *)&initial, "impo", sizeof(ulong))) { 218 char buf[128], *p, *args[3]; 219 220 // New import. Read import's parameters... 221 initial = 0; 222 223 p = buf; 224 while (p - buf < sizeof buf) { 225 if ((n = read(netfd, p, 1)) < 0) 226 fatal("can't read impo arguments: %r\n"); 227 228 if (n == 0) 229 fatal("connection closed while reading arguments\n"); 230 231 if (*p == '\n') 232 *p = '\0'; 233 if (*p++ == '\0') 234 break; 235 } 236 237 if (tokenize(buf, args, nelem(args)) != 2) 238 fatal("impo arguments invalid: impo%s...\n", buf); 239 240 if (!strcmp(args[0], "aan")) 241 filterp = aanfilter; 242 else if (strcmp(args[0], "nofilter")) 243 fatal("import filter argument unsupported: %s\n", args[0]); 244 245 if (!strcmp(args[1], "ssl")) 246 encproto = Encssl; 247 else if (!strcmp(args[1], "tls")) 248 encproto = Enctls; 249 else if (strcmp(args[1], "clear")) 250 fatal("import encryption proto unsupported: %s\n", args[1]); 251 252 if (encproto == Enctls) 253 sysfatal("%s: tls has not yet been implemented\n", argv[0]); 254 } 255 256 if (encproto != Encnone && ealgs && ai) { 257 uchar key[16]; 258 uchar digest[SHA1dlen]; 259 char fromclientsecret[21]; 260 char fromserversecret[21]; 261 int i; 262 263 memmove(key+4, ai->secret, ai->nsecret); 264 265 /* exchange random numbers */ 266 srand(truerand()); 267 for(i = 0; i < 4; i++) 268 key[i+12] = rand(); 269 270 if (initial) 271 fatal("Protocol botch: old import\n"); 272 if(readn(netfd, key, 4) != 4) 273 fatal("can't read key part; %r\n"); 274 275 if(write(netfd, key+12, 4) != 4) 276 fatal("can't write key part; %r\n"); 277 278 /* scramble into two secrets */ 279 sha1(key, sizeof(key), digest, nil); 280 mksecret(fromclientsecret, digest); 281 mksecret(fromserversecret, digest+10); 282 283 if (filterp) 284 netfd = filter(netfd, filterp); 285 286 switch (encproto) { 287 case Encssl: 288 netfd = pushssl(netfd, ealgs, fromserversecret, 289 fromclientsecret, nil); 290 break; 291 case Enctls: 292 default: 293 fatal("Unsupported encryption protocol\n"); 294 } 295 296 if(netfd < 0) 297 fatal("can't establish ssl connection: %r"); 298 } 299 else if (filterp) { 300 if (initial) 301 fatal("Protocol botch: don't know how to deal with this\n"); 302 netfd = filter(netfd, filterp); 303 } 304 305 /* 306 * Start serving file requests from the network 307 */ 308 for(;;) { 309 r = getsbuf(); 310 if(r == 0) 311 fatal("Out of service buffers"); 312 313 n = localread9pmsg(netfd, r->buf, messagesize, &initial); 314 if(n <= 0) 315 fatal(nil); 316 317 if(convM2S(r->buf, n, &r->work) == 0) 318 fatal("convM2S format error"); 319 320 DEBUG(DFD, "%F\n", &r->work); 321 (fcalls[r->work.type])(r); 322 } 323 } 324 325 // WARNING: Replace this with the original version as soon as all 326 // _old_ imports have been replaced with negotiating imports. Also 327 // cpu relies on this (which needs to be fixed!) -- pb. 328 static int 329 localread9pmsg(int fd, void *abuf, uint n, ulong *initial) 330 { 331 int m, len; 332 uchar *buf; 333 334 buf = abuf; 335 336 /* read count */ 337 assert(BIT32SZ == sizeof(ulong)); 338 if (*initial) { 339 memcpy(buf, initial, BIT32SZ); 340 *initial = 0; 341 } 342 else { 343 m = readn(fd, buf, BIT32SZ); 344 if(m != BIT32SZ){ 345 if(m < 0) 346 return -1; 347 return 0; 348 } 349 } 350 351 len = GBIT32(buf); 352 if(len <= BIT32SZ || len > n){ 353 werrstr("bad length in 9P2000 message header"); 354 return -1; 355 } 356 len -= BIT32SZ; 357 m = readn(fd, buf+BIT32SZ, len); 358 if(m < len) 359 return 0; 360 return BIT32SZ+m; 361 } 362 void 363 reply(Fcall *r, Fcall *t, char *err) 364 { 365 uchar *data; 366 int n; 367 368 t->tag = r->tag; 369 t->fid = r->fid; 370 if(err) { 371 t->type = Rerror; 372 t->ename = err; 373 } 374 else 375 t->type = r->type + 1; 376 377 DEBUG(DFD, "\t%F\n", t); 378 379 data = malloc(messagesize); /* not mallocz; no need to clear */ 380 if(data == nil) 381 fatal(Enomem); 382 n = convS2M(t, data, messagesize); 383 if(write(netfd, data, n)!=n) 384 {syslog(0, "exportfs", "short write: %r"); 385 fatal("mount write"); 386 } 387 free(data); 388 } 389 390 Fid * 391 getfid(int nr) 392 { 393 Fid *f; 394 395 for(f = fidhash(nr); f; f = f->next) 396 if(f->nr == nr) 397 return f; 398 399 return 0; 400 } 401 402 int 403 freefid(int nr) 404 { 405 Fid *f, **l; 406 char buf[128]; 407 408 l = &fidhash(nr); 409 for(f = *l; f; f = f->next) { 410 if(f->nr == nr) { 411 if(f->mid) { 412 sprint(buf, "/mnt/exportfs/%d", f->mid); 413 unmount(0, buf); 414 psmap[f->mid] = 0; 415 } 416 if(f->f) { 417 freefile(f->f); 418 f->f = nil; 419 } 420 *l = f->next; 421 f->next = fidfree; 422 fidfree = f; 423 return 1; 424 } 425 l = &f->next; 426 } 427 428 return 0; 429 } 430 431 Fid * 432 newfid(int nr) 433 { 434 Fid *new, **l; 435 int i; 436 437 l = &fidhash(nr); 438 for(new = *l; new; new = new->next) 439 if(new->nr == nr) 440 return 0; 441 442 if(fidfree == 0) { 443 fidfree = emallocz(sizeof(Fid) * Fidchunk); 444 445 for(i = 0; i < Fidchunk-1; i++) 446 fidfree[i].next = &fidfree[i+1]; 447 448 fidfree[Fidchunk-1].next = 0; 449 } 450 451 new = fidfree; 452 fidfree = new->next; 453 454 memset(new, 0, sizeof(Fid)); 455 new->next = *l; 456 *l = new; 457 new->nr = nr; 458 new->fid = -1; 459 new->mid = 0; 460 461 return new; 462 } 463 464 Fsrpc * 465 getsbuf(void) 466 { 467 static int ap; 468 int look, rounds; 469 Fsrpc *wb; 470 int small_instead_of_fast = 1; 471 472 if(small_instead_of_fast) 473 ap = 0; /* so we always start looking at the beginning and reuse buffers */ 474 475 for(rounds = 0; rounds < 10; rounds++) { 476 for(look = 0; look < Nr_workbufs; look++) { 477 if(++ap == Nr_workbufs) 478 ap = 0; 479 if(Workq[ap].busy == 0) 480 break; 481 } 482 483 if(look == Nr_workbufs){ 484 sleep(10 * rounds); 485 continue; 486 } 487 488 wb = &Workq[ap]; 489 wb->pid = 0; 490 wb->canint = 0; 491 wb->flushtag = NOTAG; 492 wb->busy = 1; 493 if(wb->buf == nil) /* allocate buffers dynamically to keep size down */ 494 wb->buf = emallocz(messagesize); 495 return wb; 496 } 497 fatal("No more work buffers"); 498 return nil; 499 } 500 501 void 502 freefile(File *f) 503 { 504 File *parent, *child; 505 506 Loop: 507 f->ref--; 508 if(f->ref > 0) 509 return; 510 freecnt++; 511 if(f->ref < 0) abort(); 512 DEBUG(DFD, "free %s\n", f->name); 513 /* delete from parent */ 514 parent = f->parent; 515 if(parent->child == f) 516 parent->child = f->childlist; 517 else{ 518 for(child=parent->child; child->childlist!=f; child=child->childlist) 519 if(child->childlist == nil) 520 fatal("bad child list"); 521 child->childlist = f->childlist; 522 } 523 freeqid(f->qidt); 524 free(f->name); 525 f->name = nil; 526 free(f); 527 f = parent; 528 if(f != nil) 529 goto Loop; 530 } 531 532 File * 533 file(File *parent, char *name) 534 { 535 Dir *dir; 536 char *path; 537 File *f; 538 539 DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name); 540 541 path = makepath(parent, name); 542 dir = dirstat(path); 543 free(path); 544 if(dir == nil) 545 return nil; 546 547 for(f = parent->child; f; f = f->childlist) 548 if(strcmp(name, f->name) == 0) 549 break; 550 551 if(f == nil){ 552 f = emallocz(sizeof(File)); 553 f->name = estrdup(name); 554 555 f->parent = parent; 556 f->childlist = parent->child; 557 parent->child = f; 558 parent->ref++; 559 f->ref = 0; 560 filecnt++; 561 } 562 f->ref++; 563 f->qid.type = dir->qid.type; 564 f->qid.vers = dir->qid.vers; 565 f->qidt = uniqueqid(dir); 566 f->qid.path = f->qidt->uniqpath; 567 568 f->inval = 0; 569 570 free(dir); 571 572 return f; 573 } 574 575 void 576 initroot(void) 577 { 578 Dir *dir; 579 580 root = emallocz(sizeof(File)); 581 root->name = estrdup("."); 582 583 dir = dirstat(root->name); 584 if(dir == nil) 585 fatal("root stat"); 586 587 root->ref = 1; 588 root->qid.vers = dir->qid.vers; 589 root->qidt = uniqueqid(dir); 590 root->qid.path = root->qidt->uniqpath; 591 root->qid.type = QTDIR; 592 free(dir); 593 594 psmpt = emallocz(sizeof(File)); 595 psmpt->name = estrdup("/"); 596 597 dir = dirstat(psmpt->name); 598 if(dir == nil) 599 return; 600 601 psmpt->ref = 1; 602 psmpt->qid.vers = dir->qid.vers; 603 psmpt->qidt = uniqueqid(dir); 604 psmpt->qid.path = psmpt->qidt->uniqpath; 605 free(dir); 606 607 psmpt = file(psmpt, "mnt"); 608 if(psmpt == 0) 609 return; 610 psmpt = file(psmpt, "exportfs"); 611 } 612 613 char* 614 makepath(File *p, char *name) 615 { 616 int i, n; 617 char *c, *s, *path, *seg[256]; 618 619 seg[0] = name; 620 n = strlen(name)+2; 621 for(i = 1; i < 256 && p; i++, p = p->parent){ 622 seg[i] = p->name; 623 n += strlen(p->name)+1; 624 } 625 path = malloc(n); 626 if(path == nil) 627 fatal("out of memory"); 628 s = path; 629 630 while(i--) { 631 for(c = seg[i]; *c; c++) 632 *s++ = *c; 633 *s++ = '/'; 634 } 635 while(s[-1] == '/') 636 s--; 637 *s = '\0'; 638 639 return path; 640 } 641 642 int 643 qidhash(vlong path) 644 { 645 int h, n; 646 647 h = 0; 648 for(n=0; n<64; n+=Nqidbits){ 649 h ^= path; 650 path >>= Nqidbits; 651 } 652 return h & (Nqidtab-1); 653 } 654 655 void 656 freeqid(Qidtab *q) 657 { 658 ulong h; 659 Qidtab *l; 660 661 q->ref--; 662 if(q->ref > 0) 663 return; 664 qfreecnt++; 665 h = qidhash(q->path); 666 if(qidtab[h] == q) 667 qidtab[h] = q->next; 668 else{ 669 for(l=qidtab[h]; l->next!=q; l=l->next) 670 if(l->next == nil) 671 fatal("bad qid list"); 672 l->next = q->next; 673 } 674 free(q); 675 } 676 677 Qidtab* 678 qidlookup(Dir *d) 679 { 680 ulong h; 681 Qidtab *q; 682 683 h = qidhash(d->qid.path); 684 for(q=qidtab[h]; q!=nil; q=q->next) 685 if(q->type==d->type && q->dev==d->dev && q->path==d->qid.path) 686 return q; 687 return nil; 688 } 689 690 int 691 qidexists(vlong path) 692 { 693 int h; 694 Qidtab *q; 695 696 for(h=0; h<Nqidtab; h++) 697 for(q=qidtab[h]; q!=nil; q=q->next) 698 if(q->uniqpath == path) 699 return 1; 700 return 0; 701 } 702 703 Qidtab* 704 uniqueqid(Dir *d) 705 { 706 ulong h; 707 vlong path; 708 Qidtab *q; 709 710 q = qidlookup(d); 711 if(q != nil){ 712 q->ref++; 713 return q; 714 } 715 path = d->qid.path; 716 while(qidexists(path)){ 717 DEBUG(DFD, "collision on %s\n", d->name); 718 /* collision: find a new one */ 719 ncollision++; 720 path &= QIDPATH; 721 ++newqid; 722 if(newqid >= (1<<16)){ 723 DEBUG(DFD, "collision wraparound\n"); 724 newqid = 1; 725 } 726 path |= newqid<<48; 727 DEBUG(DFD, "assign qid %.16llux\n", path); 728 } 729 q = mallocz(sizeof(Qidtab), 1); 730 if(q == nil) 731 fatal("no memory for qid table"); 732 qidcnt++; 733 q->ref = 1; 734 q->type = d->type; 735 q->dev = d->dev; 736 q->path = d->qid.path; 737 q->uniqpath = path; 738 h = qidhash(d->qid.path); 739 q->next = qidtab[h]; 740 qidtab[h] = q; 741 return q; 742 } 743 744 void 745 fatal(char *s, ...) 746 { 747 char buf[ERRMAX]; 748 va_list arg; 749 Proc *m; 750 751 if (s) { 752 va_start(arg, s); 753 vsnprint(buf, ERRMAX, s, arg); 754 va_end(arg); 755 } 756 757 /* Clear away the slave children */ 758 for(m = Proclist; m; m = m->next) 759 postnote(PNPROC, m->pid, "kill"); 760 761 DEBUG(DFD, "%s\n", buf); 762 if (s) 763 sysfatal(buf); 764 else 765 exits(nil); 766 } 767 768 void* 769 emallocz(uint n) 770 { 771 void *p; 772 773 p = mallocz(n, 1); 774 if(p == nil) 775 fatal(Enomem); 776 return p; 777 } 778 779 char* 780 estrdup(char *s) 781 { 782 char *t; 783 784 t = strdup(s); 785 if(t == nil) 786 fatal(Enomem); 787 return t; 788 } 789 790 /* Network on fd1, mount driver on fd0 */ 791 int 792 filter(int fd, char *cmd) 793 { 794 int p[2], lfd, len, nb, argc; 795 char newport[128], buf[128], devdir[40], *s, *file, *argv[16]; 796 797 // Get a free port and post it to the client. 798 if (announce(anstring, devdir) < 0) 799 sysfatal("filter: Cannot announce %s: %r\n", anstring); 800 801 snprint(buf, sizeof(buf), "%s/local", devdir); 802 buf[sizeof buf - 1] = '\0'; 803 if ((lfd = open(buf, OREAD)) < 0) 804 sysfatal("filter: Cannot open %s: %r\n", buf); 805 if ((len = read(lfd, newport, sizeof newport - 1)) < 0) 806 sysfatal("filter: Cannot read %s: %r\n", buf); 807 close(lfd); 808 newport[len] = '\0'; 809 810 if ((s = strchr(newport, '\n')) != nil) 811 *s = '\0'; 812 813 if ((nb = write(fd, newport, len)) < 0) 814 sysfatal("getport; cannot write port; %r"); 815 assert(nb == len); 816 817 argc = tokenize(cmd, argv, nelem(argv)-2); 818 if (argc == 0) 819 sysfatal("filter: empty command"); 820 argv[argc++] = buf; 821 argv[argc] = nil; 822 file = argv[0]; 823 if (s = strrchr(argv[0], '/')) 824 argv[0] = s+1; 825 826 if(pipe(p) < 0) 827 fatal("pipe"); 828 829 switch(rfork(RFNOWAIT|RFPROC|RFFDG)) { 830 case -1: 831 fatal("rfork record module"); 832 case 0: 833 if (dup(p[0], 1) < 0) 834 fatal("filter: Cannot dup to 1; %r\n"); 835 if (dup(p[0], 0) < 0) 836 fatal("filter: Cannot dup to 0; %r\n"); 837 close(p[0]); 838 close(p[1]); 839 exec(file, argv); 840 fatal("exec record module"); 841 default: 842 close(fd); 843 close(p[0]); 844 } 845 return p[1]; 846 } 847 848 static void 849 mksecret(char *t, uchar *f) 850 { 851 sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux", 852 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]); 853 } 854