main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "ratfs.h"
  10. #define SRVFILE "/srv/ratify"
  11. #define MOUNTPOINT "/mail/ratify"
  12. #define CTLFILE "/mail/lib/blocked"
  13. #define CONFFILE "/mail/lib/smtpd.conf.ext"
  14. typedef struct Filetree Filetree;
  15. /* prototype file tree */
  16. struct Filetree
  17. {
  18. int level;
  19. char *name;
  20. uint16_t type;
  21. int mode;
  22. uint32_t qid;
  23. };
  24. /* names of first-level directories - must be in order of level*/
  25. Filetree filetree[] =
  26. {
  27. 0, "/", Directory, 0555|DMDIR, Qroot,
  28. 1, "allow", Addrdir, 0555|DMDIR, Qallow,
  29. 1, "delay", Addrdir, 0555|DMDIR, Qdelay,
  30. 1, "block", Addrdir, 0555|DMDIR, Qblock,
  31. 1, "dial", Addrdir, 0555|DMDIR, Qdial,
  32. 1, "deny", Addrdir, 0555|DMDIR, Qdeny,
  33. 1, "trusted", Trusted, 0777|DMDIR, Qtrusted, /* creation allowed */
  34. 1, "ctl", Ctlfile, 0222, Qctl,
  35. 2, "ip", IPaddr, 0555|DMDIR, Qaddr,
  36. 2, "account", Acctaddr, 0555|DMDIR, Qaddr,
  37. 0, 0, 0, 0, 0,
  38. };
  39. int debugfd = -1;
  40. int trustedqid = Qtrustedfile;
  41. char *ctlfile = CTLFILE;
  42. char *conffile = CONFFILE;
  43. #pragma varargck type "I" Cidraddr*
  44. static int ipconv(Fmt*);
  45. static void post(int, char*);
  46. static void setroot(void);
  47. void
  48. usage(void)
  49. {
  50. fprint(2, "ratfs [-d] [-c conffile] [-f ctlfile] [-m mountpoint]\n");
  51. exits("usage");
  52. }
  53. void
  54. main(int argc, char *argv[])
  55. {
  56. char *mountpoint = MOUNTPOINT;
  57. int p[2];
  58. ARGBEGIN {
  59. case 'c':
  60. conffile = ARGF();
  61. break;
  62. case 'd':
  63. debugfd = 2; /* stderr*/
  64. break;
  65. case 'f':
  66. ctlfile = ARGF();
  67. break;
  68. case 'm':
  69. mountpoint = ARGF();
  70. break;
  71. } ARGEND
  72. if(argc != 0)
  73. usage();
  74. fmtinstall('I', ipconv);
  75. setroot();
  76. getconf();
  77. reload();
  78. /* get a pipe and mount it in /srv */
  79. if(pipe(p) < 0)
  80. fatal("pipe failed: %r");
  81. srvfd = p[0];
  82. post(p[1], mountpoint);
  83. /* start the 9fs protocol */
  84. switch(rfork(RFPROC|RFNAMEG|RFENVG|RFFDG|RFNOTEG|RFREND)){
  85. case -1:
  86. fatal("fork: %r");
  87. case 0:
  88. /* seal off standard input/output */
  89. close(0);
  90. open("/dev/null", OREAD);
  91. close(1);
  92. open("/dev/null", OWRITE);
  93. close(p[1]);
  94. fmtinstall('F', fcallfmt); /* debugging */
  95. io();
  96. fprint(2, "ratfs dying\n");
  97. break;
  98. default:
  99. close(p[0]);
  100. if(mount(p[1], -1, mountpoint, MREPL|MCREATE, "") < 0)
  101. fatal("mount failed: %r");
  102. }
  103. exits(0);
  104. }
  105. static void
  106. setroot(void)
  107. {
  108. Filetree *fp;
  109. Node *np;
  110. int qid;
  111. root = 0;
  112. qid = Qaddr;
  113. for(fp = filetree; fp->name; fp++) {
  114. switch(fp->level) {
  115. case 0: /* root */
  116. case 1: /* second level directory */
  117. newnode(root, fp->name, fp->type, fp->mode, fp->qid);
  118. break;
  119. case 2: /* lay down the Ipaddr and Acctaddr subdirectories */
  120. for (np = root->children; np; np = np->sibs){
  121. if(np->d.type == Addrdir)
  122. newnode(np, fp->name, fp->type, fp->mode, qid++);
  123. }
  124. break;
  125. default:
  126. fatal("bad filetree");
  127. }
  128. }
  129. dummy.d.type = Dummynode;
  130. dummy.d.mode = 0444;
  131. dummy.d.uid = "upas";
  132. dummy.d.gid = "upas";
  133. dummy.d.atime = dummy.d.mtime = time(0);
  134. dummy.d.qid.path = Qdummy; /* for now */
  135. }
  136. static void
  137. post(int fd, char *mountpoint)
  138. {
  139. int f;
  140. char buf[128];
  141. if(access(SRVFILE,0) >= 0){
  142. /*
  143. * If we can open and mount the /srv node,
  144. * another server is already running, so just exit.
  145. */
  146. f = open(SRVFILE, ORDWR);
  147. if(f >= 0 && mount(f, -1, mountpoint, MREPL|MCREATE, "", 'M') >= 0){
  148. unmount(0, mountpoint);
  149. close(f);
  150. exits(0);
  151. }
  152. remove(SRVFILE);
  153. }
  154. /*
  155. * create the server node and post our pipe to it
  156. */
  157. f = create(SRVFILE, OWRITE, 0666);
  158. if(f < 0)
  159. fatal("can't create %s", SRVFILE);
  160. sprint(buf, "%d", fd);
  161. if(write(f, buf, strlen(buf)) != strlen(buf))
  162. fatal("can't write %s", SRVFILE);
  163. close(f);
  164. }
  165. /*
  166. * print message and die
  167. */
  168. void
  169. fatal(char *fmt, ...)
  170. {
  171. va_list arg;
  172. char buf[8*1024];
  173. va_start(arg, fmt);
  174. vseprint(buf, buf + (sizeof(buf)-1) / sizeof(*buf), fmt, arg);
  175. va_end(arg);
  176. fprint(2, "%s: %s\n", argv0, buf);
  177. exits(buf);
  178. }
  179. /*
  180. * create a new directory node
  181. */
  182. Node*
  183. newnode(Node *parent, char *name, uint16_t type, int mode, uint32_t qid)
  184. {
  185. Node *np;
  186. np = mallocz(sizeof(Node), 1);
  187. if(np == 0)
  188. fatal("out of memory");
  189. np->d.name = atom(name);
  190. np->d.type = type;
  191. np->d.mode = mode;
  192. np->d.mtime = np->d.atime = time(0);
  193. np->d.uid = atom("upas");
  194. np->d.gid = atom("upas");
  195. np->d.muid = atom("upas");
  196. if(np->d.mode&DMDIR)
  197. np->d.qid.type = QTDIR;
  198. np->d.qid.path = qid;
  199. np->d.qid.vers = 0;
  200. if(parent){
  201. np->parent = parent;
  202. np->sibs = parent->children;
  203. parent->children = np;
  204. parent->count++;
  205. } else {
  206. /* the root node */
  207. root = np;
  208. np->parent = np;
  209. np->children = 0;
  210. np->sibs = 0;
  211. }
  212. return np;
  213. }
  214. void
  215. printnode(Node *np)
  216. {
  217. fprint(debugfd, "Node at %p: %s (%s %s)", np, np->d.name, np->d.uid, np->d.gid);
  218. if(np->d.qid.type&QTDIR)
  219. fprint(debugfd, " QTDIR");
  220. fprint(debugfd, "\n");
  221. fprint(debugfd,"\tQID: %llu.%lu Mode: %lo Type: %d\n", np->d.qid.path,
  222. np->d.qid.vers, np->d.mode, np->d.type);
  223. fprint(debugfd, "\tMod: %.15s Acc: %.15s Count: %d\n", ctime(np->d.mtime)+4,
  224. ctime(np->d.atime)+4, np->count);
  225. switch(np->d.type)
  226. {
  227. case Directory:
  228. fprint(debugfd, "\tDirectory Child: %p", np->children);
  229. break;
  230. case Addrdir:
  231. fprint(debugfd, "\tAddrdir Child: %p", np->children);
  232. break;
  233. case IPaddr:
  234. fprint(debugfd, "\tIPaddr Base: %p Alloc: %d BaseQid %lu", np->addrs,
  235. np->allocated, np->baseqid);
  236. break;
  237. case Acctaddr:
  238. fprint(debugfd, "\tAcctaddr Base: %p Alloc: %d BaseQid %lu", np->addrs,
  239. np->allocated, np->baseqid);
  240. break;
  241. case Trusted:
  242. fprint(debugfd, "\tTrusted Child: %p", np->children);
  243. break;
  244. case Trustedperm:
  245. fprint(debugfd, "\tPerm Trustedfile: %I", &np->ip);
  246. break;
  247. case Trustedtemp:
  248. fprint(debugfd, "\tTemp Trustedfile: %I", &np->ip);
  249. break;
  250. case Ctlfile:
  251. fprint(debugfd, "\tCtlfile");
  252. break;
  253. case Dummynode:
  254. fprint(debugfd, "\tDummynode");
  255. break;
  256. default:
  257. fprint(debugfd, "\tUnknown Node Type\n\n");
  258. return;
  259. }
  260. fprint(debugfd, " Parent %p Sib: %p\n\n", np->parent, np->sibs);
  261. }
  262. void
  263. printfid(Fid *fp)
  264. {
  265. fprint(debugfd, "FID: %d (%s %s) Busy: %d Open: %d\n", fp->fid, fp->name,
  266. fp->uid, fp->busy, fp->open);
  267. printnode(fp->node);
  268. }
  269. void
  270. printtree(Node *np)
  271. {
  272. printnode(np);
  273. if(np->d.type == IPaddr
  274. || np->d.type == Acctaddr
  275. || np->d.type == Trustedperm
  276. || np->d.type == Trustedtemp)
  277. return;
  278. for (np = np->children; np; np = np->sibs)
  279. printtree(np);
  280. }
  281. static int
  282. ipconv(Fmt *f)
  283. {
  284. Cidraddr *ip;
  285. int i, j;
  286. char *p;
  287. ip = va_arg(f->args, Cidraddr*);
  288. p = (char*)&ip->ipaddr;
  289. i = 0;
  290. for (j = ip->mask; j; j <<= 1)
  291. i++;
  292. return fmtprint(f, "%d.%d.%d.%d/%d", p[3]&0xff, p[2]&0xff, p[1]&0xff, p[0]&0xff, i);
  293. }