main.c 6.4 KB

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