proto.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #include "ratfs.h"
  2. /*
  3. * 9P protocol interface
  4. */
  5. enum {
  6. RELOAD = 0, /* commands written to ctl file */
  7. RDEBUG,
  8. RNODEBUG,
  9. RNONE,
  10. };
  11. static void rflush(Fcall*), rnop(Fcall*),
  12. rauth(Fcall*), rattach(Fcall*),
  13. rclone(Fcall*), rwalk(Fcall*),
  14. rclwalk(Fcall*), ropen(Fcall*),
  15. rcreate(Fcall*), rread(Fcall*),
  16. rwrite(Fcall*), rclunk(Fcall*),
  17. rremove(Fcall*), rstat(Fcall*),
  18. rwstat(Fcall*), rversion(Fcall*);
  19. static Fid* newfid(int);
  20. static void reply(Fcall*, char*);
  21. static void (*fcalls[])(Fcall*) = {
  22. [Tversion] rversion,
  23. [Tflush] rflush,
  24. [Tauth] rauth,
  25. [Tattach] rattach,
  26. [Twalk] rwalk,
  27. [Topen] ropen,
  28. [Tcreate] rcreate,
  29. [Tread] rread,
  30. [Twrite] rwrite,
  31. [Tclunk] rclunk,
  32. [Tremove] rremove,
  33. [Tstat] rstat,
  34. [Twstat] rwstat,
  35. };
  36. static Keyword cmds[] = {
  37. "reload", RELOAD,
  38. "debug", RDEBUG,
  39. "nodebug", RNODEBUG,
  40. 0, RNONE,
  41. };
  42. /*
  43. * Main protocol loop
  44. */
  45. void
  46. io(void)
  47. {
  48. Fcall rhdr;
  49. int n;
  50. for(;;){
  51. n = read9pmsg(srvfd, rbuf, sizeof rbuf-1);
  52. if(n <= 0)
  53. fatal("mount read");
  54. if(convM2S(rbuf, n, &rhdr) == 0){
  55. if(debugfd >= 0)
  56. fprint(2, "%s: malformed message\n", argv0);
  57. continue;
  58. }
  59. if(debugfd >= 0)
  60. fprint(debugfd, "<-%F\n", &rhdr);/**/
  61. if(!fcalls[rhdr.type])
  62. reply(&rhdr, "bad fcall type");
  63. else
  64. (*fcalls[rhdr.type])(&rhdr);
  65. }
  66. }
  67. /*
  68. * write a protocol reply to the client
  69. */
  70. static void
  71. reply(Fcall *r, char *error)
  72. {
  73. int n;
  74. if(error == nil)
  75. r->type++;
  76. else {
  77. r->type = Rerror;
  78. r->ename = error;
  79. }
  80. if(debugfd >= 0)
  81. fprint(debugfd, "->%F\n", r);/**/
  82. n = convS2M(r, rbuf, sizeof rbuf);
  83. if(n == 0)
  84. sysfatal("convS2M: %r");
  85. if(write(srvfd, rbuf, n) < 0)
  86. sysfatal("reply: %r");
  87. }
  88. /*
  89. * lookup a fid. if not found, create a new one.
  90. */
  91. static Fid*
  92. newfid(int fid)
  93. {
  94. Fid *f, *ff;
  95. static Fid *fids;
  96. ff = 0;
  97. for(f = fids; f; f = f->next){
  98. if(f->fid == fid){
  99. if(!f->busy)
  100. f->node = 0;
  101. return f;
  102. } else if(!ff && !f->busy)
  103. ff = f;
  104. }
  105. if(ff == 0){
  106. ff = mallocz(sizeof(*f), 1);
  107. ff->next = fids;
  108. fids = ff;
  109. }
  110. ff->node = 0;
  111. ff->fid = fid;
  112. return ff;
  113. }
  114. static void
  115. rversion(Fcall *f)
  116. {
  117. f->version = "9P2000";
  118. if(f->msize > MAXRPC)
  119. f->msize = MAXRPC;
  120. reply(f, 0);
  121. }
  122. static void
  123. rauth(Fcall *f)
  124. {
  125. reply(f, "ratfs: authentication not required");
  126. }
  127. static void
  128. rflush(Fcall *f)
  129. {
  130. reply(f, 0);
  131. }
  132. static void
  133. rattach(Fcall *f)
  134. {
  135. Fid *fidp;
  136. Dir *d;
  137. if((d=dirstat(conffile)) != nil && d->mtime > lastconftime)
  138. getconf();
  139. free(d);
  140. if((d=dirstat(ctlfile)) != nil && d->mtime > lastctltime)
  141. reload();
  142. free(d);
  143. cleantrusted();
  144. fidp = newfid(f->fid);
  145. fidp->busy = 1;
  146. fidp->node = root;
  147. fidp->name = root->d.name;
  148. fidp->uid = atom(f->uname);
  149. f->qid = root->d.qid;
  150. reply(f,0);
  151. }
  152. static void
  153. rclone(Fcall *f)
  154. {
  155. Fid *fidp, *nf;
  156. fidp = newfid(f->fid);
  157. if(fidp->node && fidp->node->d.type == Dummynode){
  158. reply(f, "can't clone an address");
  159. return;
  160. }
  161. nf = newfid(f->newfid);
  162. nf->busy = 1;
  163. nf->node = fidp->node;
  164. nf->uid = fidp->uid;
  165. nf->name = fidp->name;
  166. if(debugfd >= 0)
  167. printfid(nf);
  168. reply(f,0);
  169. }
  170. static void
  171. rwalk(Fcall *f)
  172. {
  173. int i, j;
  174. Fcall r;
  175. Fid *fidp, *nf;
  176. char *err;
  177. fidp = newfid(f->fid);
  178. if(fidp->node && fidp->node->d.type == Dummynode){
  179. reply(f, "can't walk an address node");
  180. return;
  181. }
  182. if(f->fid == f->newfid)
  183. nf = fidp;
  184. else{
  185. nf = newfid(f->newfid);
  186. nf->busy = 1;
  187. nf->node = fidp->node;
  188. nf->uid = fidp->uid;
  189. nf->name = fidp->name;
  190. if(debugfd >= 0)
  191. printfid(nf);
  192. }
  193. err = nil;
  194. for(i=0; i<f->nwname; i++){
  195. err = walk(f->wname[i], nf);
  196. if(err)
  197. break;
  198. r.wqid[i] = nf->node->d.qid;
  199. }
  200. if(i < f->nwname && f->fid != f->newfid){
  201. nf->busy = 0;
  202. nf->node = 0;
  203. nf->name = 0;
  204. nf->uid = 0;
  205. }
  206. if(i > 0 && i < f->nwname && f->fid == f->newfid){
  207. /*
  208. * try to put things back;
  209. * we never get this sort of call from the kernel
  210. */
  211. for(j=0; j<i; j++)
  212. walk("..", nf);
  213. }
  214. memmove(f->wqid, r.wqid, sizeof f->wqid);
  215. f->nwqid = i;
  216. if(err && i==0)
  217. reply(f, err);
  218. else
  219. reply(f, 0);
  220. }
  221. /*
  222. * We don't have to do full permission checking because most files
  223. * have restricted semantics:
  224. * The ctl file is only writable
  225. * All others, including directories, are only readable
  226. */
  227. static void
  228. ropen(Fcall *f)
  229. {
  230. Fid *fidp;
  231. int mode;
  232. fidp = newfid(f->fid);
  233. if(debugfd >= 0)
  234. printfid(fidp);
  235. mode = f->mode&(OREAD|OWRITE|ORDWR);
  236. if(fidp->node->d.type == Ctlfile) {
  237. if(mode != OWRITE) {
  238. reply(f, "permission denied");
  239. return;
  240. }
  241. } else
  242. if (mode != OREAD) {
  243. reply(f, "permission denied or operation not supported");
  244. return;
  245. }
  246. f->qid = fidp->node->d.qid;
  247. fidp->open = 1;
  248. reply(f, 0);
  249. }
  250. static int
  251. permitted(Fid *fp, Node *np, int mask)
  252. {
  253. int mode;
  254. mode = np->d.mode;
  255. return (fp->uid==np->d.uid && (mode&(mask<<6)))
  256. || (fp->uid==np->d.gid && (mode&(mask<<3)))
  257. || (mode&mask);
  258. }
  259. /*
  260. * creates are only allowed in the "trusted" subdirectory
  261. * we also assume that the groupid == the uid
  262. */
  263. static void
  264. rcreate(Fcall *f)
  265. {
  266. Fid *fidp;
  267. Node *np;
  268. fidp = newfid(f->fid);
  269. np = fidp->node;
  270. if((np->d.mode&DMDIR) == 0){
  271. reply(f, "not a directory");
  272. return;
  273. }
  274. if(!permitted(fidp, np, AWRITE)) {
  275. reply(f, "permission denied");
  276. return;
  277. }
  278. /* Ignore the supplied mode and force it to be non-writable */
  279. np = newnode(np, f->name, Trustedtemp, 0444, trustedqid++);
  280. if(trustedqid >= Qaddrfile) /* wrap QIDs */
  281. trustedqid = Qtrustedfile;
  282. cidrparse(&np->ip, f->name);
  283. f->qid = np->d.qid;
  284. np->d.uid = fidp->uid;
  285. np->d.gid = np->d.uid;
  286. np->d.muid = np->d.muid;
  287. fidp->node = np;
  288. fidp->open = 1;
  289. reply(f, 0);
  290. return;
  291. }
  292. /*
  293. * only directories can be read. everthing else returns EOF.
  294. */
  295. static void
  296. rread(Fcall *f)
  297. {
  298. long cnt;
  299. Fid *fidp;
  300. cnt = f->count;
  301. f->count = 0;
  302. fidp = newfid(f->fid);
  303. f->data = (char*)rbuf+IOHDRSZ;
  304. if(fidp->open == 0) {
  305. reply(f, "file not open");
  306. return;
  307. }
  308. if ((fidp->node->d.mode&DMDIR) == 0){
  309. reply(f, 0); /*EOF*/
  310. return;
  311. }
  312. if(cnt > MAXRPC)
  313. cnt = MAXRPC;
  314. if(f->offset == 0)
  315. fidp->dirindex = 0;
  316. switch(fidp->node->d.type) {
  317. case Directory:
  318. case Addrdir:
  319. case Trusted:
  320. f->count = dread(fidp, cnt);
  321. break;
  322. case IPaddr:
  323. case Acctaddr:
  324. f->count = hread(fidp, cnt);
  325. break;
  326. default:
  327. reply(f, "can't read this type of file");
  328. return;
  329. }
  330. reply(f, 0);
  331. }
  332. /*
  333. * only the 'ctl' file in the top level directory is writable
  334. */
  335. static void
  336. rwrite(Fcall *f)
  337. {
  338. Fid *fidp;
  339. int n;
  340. char *err, *argv[10];
  341. fidp = newfid(f->fid);
  342. if(fidp->node->d.mode & DMDIR){
  343. reply(f, "directories are not writable");
  344. return;
  345. }
  346. if(fidp->open == 0) {
  347. reply(f, "file not open");
  348. return;
  349. }
  350. if (!permitted(fidp, fidp->node, AWRITE)) {
  351. reply(f, "permission denied");
  352. return;
  353. }
  354. f->data[f->count] = 0; /* the extra byte in rbuf leaves room */
  355. n = tokenize(f->data, argv, 10);
  356. err = 0;
  357. switch(findkey(argv[0], cmds)){
  358. case RELOAD:
  359. getconf();
  360. reload();
  361. break;
  362. case RDEBUG:
  363. if(n > 1){
  364. debugfd = create(argv[1], OWRITE, 0666);
  365. if(debugfd < 0)
  366. err = "create failed";
  367. } else
  368. debugfd = 2;
  369. break;
  370. case RNODEBUG:
  371. if(debugfd >= 0)
  372. close(debugfd);
  373. debugfd = -1;
  374. break;
  375. default:
  376. err = "unknown command";
  377. break;
  378. }
  379. reply(f, err);
  380. }
  381. static void
  382. rclunk(Fcall *f)
  383. {
  384. Fid *fidp;
  385. fidp = newfid(f->fid);
  386. fidp->open = 0;
  387. fidp->busy = 0;
  388. fidp->node = 0;
  389. fidp->name = 0;
  390. fidp->uid = 0;
  391. reply(f, 0);
  392. }
  393. /*
  394. * no files or directories are removable; this becomes clunk;
  395. */
  396. static void
  397. rremove(Fcall *f)
  398. {
  399. Fid *fidp;
  400. Node *dir, *np;
  401. fidp = newfid(f->fid);
  402. /*
  403. * only trusted temporary files can be removed
  404. * and only by their owner.
  405. */
  406. if(fidp->node->d.type != Trustedtemp){
  407. reply(f, "can't be removed");
  408. return;
  409. }
  410. if(fidp->uid != fidp->node->d.uid){
  411. reply(f, "permission denied");
  412. return;
  413. }
  414. dir = fidp->node->parent;
  415. for(np = dir->children; np; np = np->sibs)
  416. if(np->sibs == fidp->node)
  417. break;
  418. if(np)
  419. np->sibs = fidp->node->sibs;
  420. else
  421. dir->children = fidp->node->sibs;
  422. dir->count--;
  423. free(fidp->node);
  424. fidp->node = 0;
  425. fidp->open = 0;
  426. fidp->busy = 0;
  427. fidp->name = 0;
  428. fidp->uid = 0;
  429. reply(f, 0);
  430. }
  431. static void
  432. rstat(Fcall *f)
  433. {
  434. Fid *fidp;
  435. fidp = newfid(f->fid);
  436. if (fidp->node->d.type == Dummynode)
  437. dummy.d.name = fidp->name;
  438. f->stat = (uchar*)rbuf+4+1+2+2; /* knows about stat(5) */
  439. f->nstat = convD2M(&fidp->node->d, f->stat, MAXRPC);
  440. if(f->nstat <= BIT16SZ)
  441. reply(f, "ratfs: convD2M");
  442. else
  443. reply(f, 0);
  444. return;
  445. }
  446. static void
  447. rwstat(Fcall *f)
  448. {
  449. reply(f, "wstat not implemented");
  450. }