ftpfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <auth.h>
  4. #include <fcall.h>
  5. #include <String.h>
  6. #include "ftpfs.h"
  7. /* an active fid */
  8. typedef struct Fid Fid;
  9. struct Fid
  10. {
  11. int fid;
  12. Node *node; /* path to remote file */
  13. int busy;
  14. Fid *next;
  15. int open;
  16. };
  17. Fid *fids; /* linked list of fids */
  18. char errstring[128]; /* error to return */
  19. int mfd; /* fd for 9fs */
  20. int messagesize = 4*1024*IOHDRSZ;
  21. uchar mdata[8*1024*IOHDRSZ];
  22. uchar mbuf[8*1024*IOHDRSZ];
  23. Fcall rhdr;
  24. Fcall thdr;
  25. int debug;
  26. int usenlst;
  27. int usetls;
  28. char *ext;
  29. int quiet;
  30. int kapid = -1;
  31. int dying; /* set when any process decides to die */
  32. int dokeepalive;
  33. char *rflush(Fid*), *rnop(Fid*), *rversion(Fid*),
  34. *rattach(Fid*), *rclone(Fid*), *rwalk(Fid*),
  35. *rclwalk(Fid*), *ropen(Fid*), *rcreate(Fid*),
  36. *rread(Fid*), *rwrite(Fid*), *rclunk(Fid*),
  37. *rremove(Fid*), *rstat(Fid*), *rwstat(Fid*),
  38. *rauth(Fid*);;
  39. void mountinit(char*);
  40. void io(void);
  41. int readpdir(Node*);
  42. char *(*fcalls[])(Fid*) = {
  43. [Tflush] rflush,
  44. [Tversion] rversion,
  45. [Tattach] rattach,
  46. [Tauth] rauth,
  47. [Twalk] rwalk,
  48. [Topen] ropen,
  49. [Tcreate] rcreate,
  50. [Tread] rread,
  51. [Twrite] rwrite,
  52. [Tclunk] rclunk,
  53. [Tremove] rremove,
  54. [Tstat] rstat,
  55. [Twstat] rwstat,
  56. };
  57. OS oslist[] = {
  58. { Plan9, "Plan 9", },
  59. { Plan9, "Plan9", },
  60. { Plan9, "UNIX Type: L8 Version: Plan 9", },
  61. { Unix, "SUN", },
  62. { Unix, "UNIX", },
  63. { VMS, "VMS", },
  64. { VM, "VM", },
  65. { Tops, "TOPS", },
  66. { MVS, "MVS", },
  67. { NetWare, "NetWare", },
  68. { NetWare, "NETWARE", },
  69. { OS½, "OS/2", },
  70. { TSO, "TSO", },
  71. { NT, "Windows_NT", }, /* DOS like interface */
  72. { NT, "WINDOWS_NT", }, /* Unix like interface */
  73. { Unknown, 0 },
  74. };
  75. char *nouid = "?uid?";
  76. #define S2P(x) (((ulong)(x)) & 0xffffff)
  77. char *nosuchfile = "file does not exist";
  78. char *keyspec = "";
  79. void
  80. usage(void)
  81. {
  82. fprint(2, "ftpfs [-/dqnt] [-a passwd] [-m mountpoint] [-e ext] [-o os] [-r root] [net!]address\n");
  83. exits("usage");
  84. }
  85. void
  86. main(int argc, char *argv[])
  87. {
  88. char *mountroot = 0;
  89. char *mountpoint = "/n/ftp";
  90. char *cpassword = 0;
  91. char *cp;
  92. int p[2];
  93. OS *o;
  94. defos = Unix;
  95. user = strdup(getuser());
  96. usetls = 0;
  97. ARGBEGIN {
  98. case '/':
  99. mountroot = "/";
  100. break;
  101. case 'a':
  102. cpassword = ARGF();
  103. break;
  104. case 'd':
  105. debug = 1;
  106. break;
  107. case 'k':
  108. keyspec = EARGF(usage());
  109. break;
  110. case 'K':
  111. dokeepalive = 1;
  112. break;
  113. case 'm':
  114. mountpoint = ARGF();
  115. break;
  116. case 'n':
  117. usenlst = 1;
  118. break;
  119. case 'e':
  120. ext = ARGF();
  121. break;
  122. case 't':
  123. usetls = 1;
  124. break;
  125. case 'o':
  126. cp = ARGF();
  127. for(o = oslist; o->os != Unknown; o++)
  128. if(strncmp(cp, o->name, strlen(o->name)) == 0){
  129. defos = o->os;
  130. break;
  131. }
  132. break;
  133. case 'r':
  134. mountroot = ARGF();
  135. break;
  136. case 'q':
  137. quiet = 1;
  138. break;
  139. } ARGEND
  140. if(argc != 1)
  141. usage();
  142. /* get a pipe to mount and run 9fs on */
  143. if(pipe(p) < 0)
  144. fatal("pipe failed: %r");
  145. mfd = p[0];
  146. /* initial handshakes with remote side */
  147. hello(*argv);
  148. if(cpassword == 0)
  149. rlogin(*argv, keyspec);
  150. else
  151. clogin("anonymous", cpassword);
  152. preamble(mountroot);
  153. /* start the 9fs protocol */
  154. switch(rfork(RFPROC|RFNAMEG|RFENVG|RFFDG|RFNOTEG|RFREND)){
  155. case -1:
  156. fatal("fork: %r");
  157. case 0:
  158. /* seal off standard input/output */
  159. close(0);
  160. open("/dev/null", OREAD);
  161. close(1);
  162. open("/dev/null", OWRITE);
  163. close(p[1]);
  164. fmtinstall('F', fcallfmt); /* debugging */
  165. fmtinstall('D', dirfmt); /* expected by %F */
  166. fmtinstall('M', dirmodefmt); /* expected by %F */
  167. io();
  168. quit();
  169. break;
  170. default:
  171. close(p[0]);
  172. if(mount(p[1], -1, mountpoint, MREPL|MCREATE, "") < 0)
  173. fatal("mount failed: %r");
  174. }
  175. exits(0);
  176. }
  177. /*
  178. * lookup an fid. if not found, create a new one.
  179. */
  180. Fid *
  181. newfid(int fid)
  182. {
  183. Fid *f, *ff;
  184. ff = 0;
  185. for(f = fids; f; f = f->next){
  186. if(f->fid == fid){
  187. if(f->busy)
  188. return f;
  189. else{
  190. ff = f;
  191. break;
  192. }
  193. } else if(!ff && !f->busy)
  194. ff = f;
  195. }
  196. if(ff == 0){
  197. ff = mallocz(sizeof(*f), 1);
  198. ff->next = fids;
  199. fids = ff;
  200. }
  201. ff->node = nil;
  202. ff->fid = fid;
  203. return ff;
  204. }
  205. /*
  206. * a process that sends keep alive messages to
  207. * keep the server from shutting down the connection
  208. */
  209. int
  210. kaproc(void)
  211. {
  212. int pid;
  213. if(!dokeepalive)
  214. return -1;
  215. switch(pid = rfork(RFPROC|RFMEM)){
  216. case -1:
  217. return -1;
  218. case 0:
  219. break;
  220. default:
  221. return pid;
  222. }
  223. while(!dying){
  224. sleep(5000);
  225. nop();
  226. }
  227. _exits(0);
  228. return -1;
  229. }
  230. void
  231. io(void)
  232. {
  233. char *err, buf[ERRMAX];
  234. int n;
  235. kapid = kaproc();
  236. while(!dying){
  237. n = read9pmsg(mfd, mdata, messagesize);
  238. if(n <= 0){
  239. errstr(buf, sizeof buf);
  240. if(buf[0]=='\0' || strstr(buf, "hungup"))
  241. exits("");
  242. fatal("mount read: %s\n", buf);
  243. }
  244. if(convM2S(mdata, n, &thdr) == 0)
  245. continue;
  246. if(debug)
  247. fprint(2, "<-%F\n", &thdr);/**/
  248. if(!fcalls[thdr.type])
  249. err = "bad fcall type";
  250. else
  251. err = (*fcalls[thdr.type])(newfid(thdr.fid));
  252. if(err){
  253. rhdr.type = Rerror;
  254. rhdr.ename = err;
  255. }else{
  256. rhdr.type = thdr.type + 1;
  257. rhdr.fid = thdr.fid;
  258. }
  259. rhdr.tag = thdr.tag;
  260. if(debug)
  261. fprint(2, "->%F\n", &rhdr);/**/
  262. n = convS2M(&rhdr, mdata, messagesize);
  263. if(write(mfd, mdata, n) != n)
  264. fatal("mount write");
  265. }
  266. }
  267. char*
  268. rnop(Fid *f)
  269. {
  270. USED(f);
  271. return 0;
  272. }
  273. char*
  274. rversion(Fid*)
  275. {
  276. if(thdr.msize > sizeof(mdata))
  277. rhdr.msize = messagesize;
  278. else
  279. rhdr.msize = thdr.msize;
  280. messagesize = thdr.msize;
  281. if(strncmp(thdr.version, "9P2000", 6) != 0)
  282. return "unknown 9P version";
  283. rhdr.version = "9P2000";
  284. return nil;
  285. }
  286. char*
  287. rflush(Fid*)
  288. {
  289. return 0;
  290. }
  291. char*
  292. rauth(Fid*)
  293. {
  294. return "auth unimplemented";
  295. }
  296. char*
  297. rattach(Fid *f)
  298. {
  299. f->busy = 1;
  300. f->node = remroot;
  301. rhdr.qid = f->node->d->qid;
  302. return 0;
  303. }
  304. char*
  305. rwalk(Fid *f)
  306. {
  307. Node *np;
  308. Fid *nf;
  309. char **elems;
  310. int i, nelems;
  311. char *err;
  312. Node *node;
  313. /* clone fid */
  314. nf = nil;
  315. if(thdr.newfid != thdr.fid){
  316. nf = newfid(thdr.newfid);
  317. if(nf->busy)
  318. return "newfid in use";
  319. nf->busy = 1;
  320. nf->node = f->node;
  321. f = nf;
  322. }
  323. err = nil;
  324. elems = thdr.wname;
  325. nelems = thdr.nwname;
  326. node = f->node;
  327. rhdr.nwqid = 0;
  328. if(nelems > 0){
  329. /* walk fid */
  330. for(i=0; i<nelems && i<MAXWELEM; i++){
  331. if((node->d->qid.type & QTDIR) == 0){
  332. err = "not a directory";
  333. break;
  334. }
  335. if(strcmp(elems[i], ".") == 0){
  336. Found:
  337. rhdr.wqid[i] = node->d->qid;
  338. rhdr.nwqid++;
  339. continue;
  340. }
  341. if(strcmp(elems[i], "..") == 0){
  342. node = node->parent;
  343. goto Found;
  344. }
  345. if(strcmp(elems[i], ".flush.ftpfs") == 0){
  346. /* hack to flush the cache */
  347. invalidate(node);
  348. readdir(node);
  349. goto Found;
  350. }
  351. /* some top level names are special */
  352. if((os == Tops || os == VM || os == VMS) && node == remroot){
  353. np = newtopsdir(elems[i]);
  354. if(np){
  355. node = np;
  356. goto Found;
  357. } else {
  358. err = nosuchfile;
  359. break;
  360. }
  361. }
  362. /* everything else */
  363. node = extendpath(node, s_copy(elems[i]));
  364. if(ISCACHED(node->parent)){
  365. /* the cache of the parent is good, believe it */
  366. if(!ISVALID(node)){
  367. err = nosuchfile;
  368. break;
  369. }
  370. if(node->parent->chdirunknown || (node->d->mode & DMSYML))
  371. fixsymbolic(node);
  372. } else if(!ISVALID(node)){
  373. /* this isn't a valid node, try cd'ing */
  374. if(changedir(node) == 0){
  375. node->d->qid.type = QTDIR;
  376. node->d->mode |= DMDIR;
  377. }else{
  378. node->d->qid.type = QTFILE;
  379. node->d->mode &= ~DMDIR;
  380. }
  381. }
  382. goto Found;
  383. }
  384. if(i == 0 && err == 0)
  385. err = "file does not exist";
  386. }
  387. /* clunk a newly cloned fid if the walk didn't succeed */
  388. if(nf != nil && (err != nil || rhdr.nwqid<nelems)){
  389. nf->busy = 0;
  390. nf->fid = 0;
  391. }
  392. /* if it all worked, point the fid to the enw node */
  393. if(err == nil)
  394. f->node = node;
  395. return err;
  396. }
  397. char *
  398. ropen(Fid *f)
  399. {
  400. int mode;
  401. mode = thdr.mode;
  402. if(f->node->d->qid.type & QTDIR)
  403. if(mode != OREAD)
  404. return "permission denied";
  405. if(mode & OTRUNC){
  406. uncache(f->node);
  407. uncache(f->node->parent);
  408. filedirty(f->node);
  409. } else {
  410. /* read the remote file or directory */
  411. if(!ISCACHED(f->node)){
  412. filefree(f->node);
  413. if(f->node->d->qid.type & QTDIR){
  414. invalidate(f->node);
  415. if(readdir(f->node) < 0)
  416. return nosuchfile;
  417. } else {
  418. if(readfile(f->node) < 0)
  419. return errstring;
  420. }
  421. CACHED(f->node);
  422. }
  423. }
  424. rhdr.qid = f->node->d->qid;
  425. f->open = 1;
  426. f->node->opens++;
  427. return 0;
  428. }
  429. char*
  430. rcreate(Fid *f)
  431. {
  432. char *name;
  433. if((f->node->d->qid.type&QTDIR) == 0)
  434. return "not a directory";
  435. name = thdr.name;
  436. f->node = extendpath(f->node, s_copy(name));
  437. uncache(f->node);
  438. if(thdr.perm & DMDIR){
  439. if(createdir(f->node) < 0)
  440. return "permission denied";
  441. } else
  442. filedirty(f->node);
  443. invalidate(f->node->parent);
  444. uncache(f->node->parent);
  445. rhdr.qid = f->node->d->qid;
  446. f->open = 1;
  447. f->node->opens++;
  448. return 0;
  449. }
  450. char*
  451. rread(Fid *f)
  452. {
  453. long off;
  454. int n, cnt, rv;
  455. Node *np;
  456. rhdr.count = 0;
  457. off = thdr.offset;
  458. cnt = thdr.count;
  459. if(cnt > messagesize-IOHDRSZ)
  460. cnt = messagesize-IOHDRSZ;
  461. if(f->node->d->qid.type & QTDIR){
  462. rv = 0;
  463. for(np = f->node->children; np != nil; np = np->sibs){
  464. if(!ISVALID(np))
  465. continue;
  466. if(off <= BIT16SZ)
  467. break;
  468. n = convD2M(np->d, mbuf, messagesize-IOHDRSZ);
  469. off -= n;
  470. }
  471. for(; rv < cnt && np != nil; np = np->sibs){
  472. if(!ISVALID(np))
  473. continue;
  474. if(np->d->mode & DMSYML)
  475. fixsymbolic(np);
  476. n = convD2M(np->d, mbuf + rv, cnt-rv);
  477. if(n <= BIT16SZ)
  478. break;
  479. rv += n;
  480. }
  481. } else {
  482. /* reread file if it's fallen out of the cache */
  483. if(!ISCACHED(f->node))
  484. if(readfile(f->node) < 0)
  485. return errstring;
  486. CACHED(f->node);
  487. rv = fileread(f->node, (char*)mbuf, off, cnt);
  488. if(rv < 0)
  489. return errstring;
  490. }
  491. rhdr.data = (char*)mbuf;
  492. rhdr.count = rv;
  493. return 0;
  494. }
  495. char*
  496. rwrite(Fid *f)
  497. {
  498. long off;
  499. int cnt;
  500. if(f->node->d->qid.type & QTDIR)
  501. return "directories are not writable";
  502. rhdr.count = 0;
  503. off = thdr.offset;
  504. cnt = thdr.count;
  505. cnt = filewrite(f->node, thdr.data, off, cnt);
  506. if(cnt < 0)
  507. return errstring;
  508. filedirty(f->node);
  509. rhdr.count = cnt;
  510. return 0;
  511. }
  512. char *
  513. rclunk(Fid *f)
  514. {
  515. if(fileisdirty(f->node)){
  516. if(createfile(f->node) < 0)
  517. fprint(2, "ftpfs: couldn't create %s\n", f->node->d->name);
  518. fileclean(f->node);
  519. uncache(f->node);
  520. }
  521. if(f->open){
  522. f->open = 0;
  523. f->node->opens--;
  524. }
  525. f->busy = 0;
  526. return 0;
  527. }
  528. /*
  529. * remove is an implicit clunk
  530. */
  531. char *
  532. rremove(Fid *f)
  533. {
  534. char *e;
  535. e = nil;
  536. if(QTDIR & f->node->d->qid.type){
  537. if(removedir(f->node) < 0)
  538. e = errstring;
  539. } else {
  540. if(removefile(f->node) < 0)
  541. e = errstring;
  542. }
  543. uncache(f->node->parent);
  544. uncache(f->node);
  545. if(e == nil)
  546. INVALID(f->node);
  547. f->busy = 0;
  548. return e;
  549. }
  550. char *
  551. rstat(Fid *f)
  552. {
  553. Node *p;
  554. p = f->node->parent;
  555. if(!ISCACHED(p)){
  556. invalidate(p);
  557. readdir(p);
  558. CACHED(p);
  559. }
  560. if(!ISVALID(f->node))
  561. return nosuchfile;
  562. if(p->chdirunknown)
  563. fixsymbolic(f->node);
  564. rhdr.nstat = convD2M(f->node->d, mbuf, messagesize-IOHDRSZ);
  565. rhdr.stat = mbuf;
  566. return 0;
  567. }
  568. char *
  569. rwstat(Fid *f)
  570. {
  571. USED(f);
  572. return "wstat not implemented";
  573. }
  574. /*
  575. * print message and die
  576. */
  577. void
  578. fatal(char *fmt, ...)
  579. {
  580. va_list arg;
  581. char buf[8*1024];
  582. dying = 1;
  583. va_start(arg, fmt);
  584. vseprint(buf, buf + (sizeof(buf)-1) / sizeof(*buf), fmt, arg);
  585. va_end(arg);
  586. fprint(2, "ftpfs: %s\n", buf);
  587. if(kapid > 0)
  588. postnote(PNGROUP, kapid, "die");
  589. exits(buf);
  590. }
  591. /*
  592. * like strncpy but make sure there's a terminating null
  593. */
  594. void*
  595. safecpy(void *to, void *from, int n)
  596. {
  597. char *a = ((char*)to) + n - 1;
  598. strncpy(to, from, n);
  599. *a = 0;
  600. return to;
  601. }
  602. /*
  603. * set the error string
  604. */
  605. int
  606. seterr(char *fmt, ...)
  607. {
  608. va_list arg;
  609. va_start(arg, fmt);
  610. vsnprint(errstring, sizeof errstring, fmt, arg);
  611. va_end(arg);
  612. return -1;
  613. }
  614. /*
  615. * create a new node
  616. */
  617. Node*
  618. newnode(Node *parent, String *name)
  619. {
  620. Node *np;
  621. static ulong path;
  622. Dir d;
  623. np = mallocz(sizeof(Node), 1);
  624. if(np == 0)
  625. fatal("out of memory");
  626. np->children = 0;
  627. if(parent){
  628. np->parent = parent;
  629. np->sibs = parent->children;
  630. parent->children = np;
  631. np->depth = parent->depth + 1;
  632. d.dev = 0; /* not stat'd */
  633. } else {
  634. /* the root node */
  635. np->parent = np;
  636. np->sibs = 0;
  637. np->depth = 0;
  638. d.dev = 1;
  639. }
  640. np->remname = name;
  641. d.name = s_to_c(name);
  642. d.atime = time(0);
  643. d.mtime = d.atime;
  644. d.uid = nouid;
  645. d.gid = nouid;
  646. d.muid = nouid;
  647. np->fp = 0;
  648. d.qid.path = ++path;
  649. d.qid.vers = 0;
  650. d.qid.type = QTFILE;
  651. d.type = 0;
  652. np->d = reallocdir(&d, 0);
  653. return np;
  654. }
  655. /*
  656. * walk one down the local mirror of the remote directory tree
  657. */
  658. Node*
  659. extendpath(Node *parent, String *elem)
  660. {
  661. Node *np;
  662. for(np = parent->children; np; np = np->sibs)
  663. if(strcmp(s_to_c(np->remname), s_to_c(elem)) == 0){
  664. s_free(elem);
  665. return np;
  666. }
  667. return newnode(parent, elem);
  668. }
  669. /*
  670. * flush the cached file, write it back if it's dirty
  671. */
  672. void
  673. uncache(Node *np)
  674. {
  675. if(fileisdirty(np))
  676. createfile(np);
  677. filefree(np);
  678. UNCACHED(np);
  679. }
  680. /*
  681. * invalidate all children of a node
  682. */
  683. void
  684. invalidate(Node *node)
  685. {
  686. Node *np;
  687. if(node->opens)
  688. return; /* don't invalidate something that's open */
  689. uncachedir(node, 0);
  690. /* invalidate children */
  691. for(np = node->children; np; np = np->sibs){
  692. if(np->opens)
  693. continue; /* don't invalidate something that's open */
  694. UNCACHED(np);
  695. invalidate(np);
  696. np->d->dev = 0;
  697. }
  698. }
  699. /*
  700. * make a top level tops-20 directory. They are automaticly valid.
  701. */
  702. Node*
  703. newtopsdir(char *name)
  704. {
  705. Node *np;
  706. np = extendpath(remroot, s_copy(name));
  707. if(!ISVALID(np)){
  708. np->d->qid.type = QTDIR;
  709. np->d->atime = time(0);
  710. np->d->mtime = np->d->atime;
  711. np->d->uid = "?uid?";
  712. np->d->gid = "?uid?";
  713. np->d->muid = "?uid?";
  714. np->d->mode = DMDIR|0777;
  715. np->d->length = 0;
  716. np->d = reallocdir(np->d, 1);
  717. if(changedir(np) >= 0)
  718. VALID(np);
  719. }
  720. return np;
  721. }
  722. /*
  723. * figure out if a symbolic link is to a directory or a file
  724. */
  725. void
  726. fixsymbolic(Node *node)
  727. {
  728. if(changedir(node) == 0){
  729. node->d->mode |= DMDIR;
  730. node->d->qid.type = QTDIR;
  731. } else
  732. node->d->qid.type = QTFILE;
  733. node->d->mode &= ~DMSYML;
  734. }