3 #define SRVFILE "/srv/ratify"
4 #define MOUNTPOINT "/mail/ratify"
5 #define CTLFILE "/mail/lib/blocked"
6 #define CONFFILE "/mail/lib/smtpd.conf.ext"
8 typedef struct Filetree Filetree;
10 /* prototype file tree */
20 /* names of first-level directories - must be in order of level*/
23 0, "/", Directory, 0555|DMDIR, Qroot,
24 1, "allow", Addrdir, 0555|DMDIR, Qallow,
25 1, "delay", Addrdir, 0555|DMDIR, Qdelay,
26 1, "block", Addrdir, 0555|DMDIR, Qblock,
27 1, "dial", Addrdir, 0555|DMDIR, Qdial,
28 1, "deny", Addrdir, 0555|DMDIR, Qdeny,
29 1, "trusted", Trusted, 0777|DMDIR, Qtrusted, /* creation allowed */
30 1, "ctl", Ctlfile, 0222, Qctl,
31 2, "ip", IPaddr, 0555|DMDIR, Qaddr,
32 2, "account", Acctaddr, 0555|DMDIR, Qaddr,
38 int trustedqid = Qtrustedfile;
39 char *ctlfile = CTLFILE;
40 char *conffile = CONFFILE;
42 #pragma varargck type "I" Cidraddr*
44 static int ipconv(Fmt*);
45 static void post(int, char*);
46 static void setroot(void);
51 fprint(2, "ratfs [-d] [-c conffile] [-f ctlfile] [-m mountpoint]\n");
56 main(int argc, char *argv[])
58 char *mountpoint = MOUNTPOINT;
66 debugfd = 2; /* stderr*/
78 fmtinstall('I', ipconv);
83 /* get a pipe and mount it in /srv */
85 fatal("pipe failed: %r");
87 post(p[1], mountpoint);
89 /* start the 9fs protocol */
90 switch(rfork(RFPROC|RFNAMEG|RFENVG|RFFDG|RFNOTEG|RFREND)){
94 /* seal off standard input/output */
96 open("/dev/null", OREAD);
98 open("/dev/null", OWRITE);
101 fmtinstall('F', fcallfmt); /* debugging */
103 fprint(2, "ratfs dying\n");
107 if(mount(p[1], -1, mountpoint, MREPL|MCREATE, "") < 0)
108 fatal("mount failed: %r");
122 for(fp = filetree; fp->name; fp++) {
125 case 1: /* second level directory */
126 newnode(root, fp->name, fp->type, fp->mode, fp->qid);
128 case 2: /* lay down the Ipaddr and Acctaddr subdirectories */
129 for (np = root->children; np; np = np->sibs){
130 if(np->d.type == Addrdir)
131 newnode(np, fp->name, fp->type, fp->mode, qid++);
135 fatal("bad filetree");
138 dummy.d.type = Dummynode;
140 dummy.d.uid = "upas";
141 dummy.d.gid = "upas";
142 dummy.d.atime = dummy.d.mtime = time(0);
143 dummy.d.qid.path = Qdummy; /* for now */
147 post(int fd, char *mountpoint)
153 if(access(SRVFILE,0) >= 0){
155 * If we can open and mount the /srv node,
156 * another server is already running, so just exit.
158 f = open(SRVFILE, ORDWR);
159 if(f >= 0 && mount(f, -1, mountpoint, MREPL|MCREATE, "") >= 0){
160 unmount(0, mountpoint);
168 * create the server node and post our pipe to it
170 f = create(SRVFILE, OWRITE, 0666);
172 fatal("can't create %s", SRVFILE);
174 sprint(buf, "%d", fd);
175 if(write(f, buf, strlen(buf)) != strlen(buf))
176 fatal("can't write %s", SRVFILE);
182 * print message and die
185 fatal(char *fmt, ...)
191 vseprint(buf, buf + (sizeof(buf)-1) / sizeof(*buf), fmt, arg);
194 fprint(2, "%s: %s\n", argv0, buf);
199 * create a new directory node
202 newnode(Node *parent, char *name, ushort type, int mode, ulong qid)
206 np = mallocz(sizeof(Node), 1);
208 fatal("out of memory");
209 np->d.name = atom(name);
212 np->d.mtime = np->d.atime = time(0);
213 np->d.uid = atom("upas");
214 np->d.gid = atom("upas");
215 np->d.muid = atom("upas");
217 np->d.qid.type = QTDIR;
218 np->d.qid.path = qid;
222 np->sibs = parent->children;
223 parent->children = np;
238 fprint(debugfd, "Node at %p: %s (%s %s)", np, np->d.name, np->d.uid, np->d.gid);
239 if(np->d.qid.type&QTDIR)
240 fprint(debugfd, " QTDIR");
241 fprint(debugfd, "\n");
242 fprint(debugfd,"\tQID: %llud.%lud Mode: %lo Type: %d\n", np->d.qid.path,
243 np->d.qid.vers, np->d.mode, np->d.type);
244 fprint(debugfd, "\tMod: %.15s Acc: %.15s Count: %d\n", ctime(np->d.mtime)+4,
245 ctime(np->d.atime)+4, np->count);
249 fprint(debugfd, "\tDirectory Child: %p", np->children);
252 fprint(debugfd, "\tAddrdir Child: %p", np->children);
255 fprint(debugfd, "\tIPaddr Base: %p Alloc: %d BaseQid %lud", np->addrs,
256 np->allocated, np->baseqid);
259 fprint(debugfd, "\tAcctaddr Base: %p Alloc: %d BaseQid %lud", np->addrs,
260 np->allocated, np->baseqid);
263 fprint(debugfd, "\tTrusted Child: %p", np->children);
266 fprint(debugfd, "\tPerm Trustedfile: %I", &np->ip);
269 fprint(debugfd, "\tTemp Trustedfile: %I", &np->ip);
272 fprint(debugfd, "\tCtlfile");
275 fprint(debugfd, "\tDummynode");
278 fprint(debugfd, "\tUnknown Node Type\n\n");
281 fprint(debugfd, " Parent %p Sib: %p\n\n", np->parent, np->sibs);
287 fprint(debugfd, "FID: %d (%s %s) Busy: %d Open: %d\n", fp->fid, fp->name,
288 fp->uid, fp->busy, fp->open);
296 if(np->d.type == IPaddr
297 || np->d.type == Acctaddr
298 || np->d.type == Trustedperm
299 || np->d.type == Trustedtemp)
301 for (np = np->children; np; np = np->sibs)
312 ip = va_arg(f->args, Cidraddr*);
313 p = (char*)&ip->ipaddr;
315 for (j = ip->mask; j; j <<= 1)
317 return fmtprint(f, "%d.%d.%d.%d/%d", p[3]&0xff, p[2]&0xff, p[1]&0xff, p[0]&0xff, i);