exportfs.c 16 KB

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