ftpfs.c 14 KB

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