main.c 6.7 KB

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