summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ratfs/main.c
blob: 7c83c892d11362792a7481ba7857251819ec63ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "ratfs.h"

#define	SRVFILE		"/srv/ratify"
#define MOUNTPOINT	"/mail/ratify"
#define	CTLFILE		"/mail/lib/blocked"
#define	CONFFILE	"/mail/lib/smtpd.conf.ext"

typedef struct Filetree	Filetree;

	/* prototype file tree */
struct	Filetree
{
	int	level;
	char	*name;
	ushort	type;
	int	mode;
	ulong	qid;
};

	/* names of first-level directories - must be in order of level*/
Filetree	filetree[] =
{
	0,	"/",		Directory,	0555|DMDIR,	Qroot,
	1,	"allow",	Addrdir,	0555|DMDIR,	Qallow,
	1,	"delay",	Addrdir,	0555|DMDIR,	Qdelay,
	1,	"block",	Addrdir,	0555|DMDIR,	Qblock,
	1,	"dial",		Addrdir,	0555|DMDIR,	Qdial,
	1,	"deny",		Addrdir,	0555|DMDIR,	Qdeny,
	1,	"trusted",	Trusted,	0777|DMDIR,	Qtrusted,	/* creation allowed */
	1,	"ctl",		Ctlfile,	0222,		Qctl,
	2,	"ip",		IPaddr,		0555|DMDIR,	Qaddr,
	2,	"account",	Acctaddr,	0555|DMDIR,	Qaddr,
	0,	0,		0,		0,		0,
	
};

int	debugfd = -1;
int	trustedqid = Qtrustedfile;
char	*ctlfile =	CTLFILE;
char	*conffile =	CONFFILE;

#pragma	varargck	type	"I"	Cidraddr*

static	int	ipconv(Fmt*);
static	void	post(int, char*);
static	void	setroot(void);

void
usage(void)
{
	fprint(2, "ratfs [-d] [-c conffile] [-f ctlfile] [-m mountpoint]\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	char *mountpoint = MOUNTPOINT;
	int p[2];

	ARGBEGIN {
	case 'c':
		conffile = ARGF();
		break;
	case 'd':
		debugfd = 2;		/* stderr*/
		break;
	case 'f':
		ctlfile = ARGF();
		break;
	case 'm':
		mountpoint = ARGF();
		break;
	} ARGEND
	if(argc != 0)
		usage();

	fmtinstall('I', ipconv);
	setroot();
	getconf();
	reload();

	/* get a pipe and mount it in /srv */
	if(pipe(p) < 0)
		fatal("pipe failed: %r");
	srvfd = p[0];
	post(p[1], mountpoint);

	/* start the 9fs protocol */
	switch(rfork(RFPROC|RFNAMEG|RFENVG|RFFDG|RFNOTEG|RFREND)){
	case -1:
		fatal("fork: %r");
	case 0:
		/* seal off standard input/output */
		close(0);
		open("/dev/null", OREAD);
		close(1);
		open("/dev/null", OWRITE);

		close(p[1]);
		fmtinstall('F', fcallfmt); /* debugging */
		io();
		fprint(2, "ratfs dying\n");
		break;
	default:
		close(p[0]);
		if(mount(p[1], -1, mountpoint, MREPL|MCREATE, "") < 0)
			fatal("mount failed: %r");
	}
	exits(0);
}

static void
setroot(void)
{
	Filetree *fp;
	Node *np;
	int qid;

	root = 0;
	qid = Qaddr;
	for(fp = filetree; fp->name; fp++) {
		switch(fp->level) {
		case 0:		/* root */
		case 1:		/* second level directory */
			newnode(root, fp->name, fp->type, fp->mode, fp->qid);
			break;
		case 2:		/* lay down the Ipaddr and Acctaddr subdirectories */
			for (np = root->children; np; np = np->sibs){
				if(np->d.type == Addrdir)
					newnode(np, fp->name, fp->type, fp->mode, qid++);
			}
			break;
		default:
			fatal("bad filetree");
		}
	}
	dummy.d.type = Dummynode;
	dummy.d.mode = 0444;
	dummy.d.uid = "upas";
	dummy.d.gid = "upas";
	dummy.d.atime = dummy.d.mtime = time(0);
	dummy.d.qid.path = Qdummy;				/* for now */
}

static void
post(int fd, char *mountpoint)
{

	int f;
	char buf[128];

	if(access(SRVFILE,0) >= 0){
		/*
		 * If we can open and mount the /srv node,
		 * another server is already running, so just exit.
		 */
		f = open(SRVFILE, ORDWR);
		if(f >= 0 && mount(f, -1, mountpoint, MREPL|MCREATE, "") >= 0){
				unmount(0, mountpoint);
				close(f);
				exits(0);
		}
		remove(SRVFILE);
	}

	/*
	 * create the server node and post our pipe to it
	 */
	f = create(SRVFILE, OWRITE, 0666);
	if(f < 0)
		fatal("can't create %s", SRVFILE);

	sprint(buf, "%d", fd);
	if(write(f, buf, strlen(buf)) != strlen(buf))
		fatal("can't write %s", SRVFILE);

	close(f);
}

/*
 *  print message and die
 */
void
fatal(char *fmt, ...)
{
	va_list arg;
	char buf[8*1024];

	va_start(arg, fmt);
	vseprint(buf, buf + (sizeof(buf)-1) / sizeof(*buf), fmt, arg);
	va_end(arg);

	fprint(2, "%s: %s\n", argv0, buf);
	exits(buf);
}

/*
 *  create a new directory node
 */
Node*
newnode(Node *parent, char *name, ushort type, int mode, ulong qid)
{
	Node *np;

	np = mallocz(sizeof(Node), 1);
	if(np == 0)
		fatal("out of memory");
	np->d.name = atom(name);
	np->d.type = type;
	np->d.mode = mode;
	np->d.mtime = np->d.atime = time(0);
	np->d.uid = atom("upas");
	np->d.gid = atom("upas");
	np->d.muid = atom("upas");
	if(np->d.mode&DMDIR)
		np->d.qid.type = QTDIR;
	np->d.qid.path = qid;
	np->d.qid.vers = 0;
	if(parent){
		np->parent = parent;
		np->sibs = parent->children;
		parent->children = np;
		parent->count++;
	} else {
		/* the root node */
		root = np;
		np->parent = np;
		np->children = 0;
		np->sibs = 0;
	}
	return np;
}

void
printnode(Node *np)
{
	fprint(debugfd, "Node at %p: %s (%s %s)", np, np->d.name, np->d.uid, np->d.gid);
	if(np->d.qid.type&QTDIR)
		fprint(debugfd, " QTDIR");
	fprint(debugfd, "\n");
	fprint(debugfd,"\tQID: %llud.%lud Mode: %lo Type: %d\n", np->d.qid.path,
			np->d.qid.vers, np->d.mode, np->d.type);
	fprint(debugfd, "\tMod: %.15s  Acc: %.15s Count: %d\n", ctime(np->d.mtime)+4,
			ctime(np->d.atime)+4, np->count);
	switch(np->d.type)
	{
	case Directory:
		fprint(debugfd, "\tDirectory Child: %p", np->children);
		break;
	case Addrdir:
		fprint(debugfd, "\tAddrdir Child: %p", np->children);
		break;
	case IPaddr:
		fprint(debugfd, "\tIPaddr Base: %p Alloc: %d BaseQid %lud", np->addrs,
			np->allocated, np->baseqid);
		break;
	case Acctaddr:
		fprint(debugfd, "\tAcctaddr Base: %p Alloc: %d BaseQid %lud", np->addrs,
			np->allocated, np->baseqid);
		break;
	case Trusted:
		fprint(debugfd, "\tTrusted Child: %p", np->children);
		break;
	case Trustedperm:
		fprint(debugfd, "\tPerm Trustedfile: %I", &np->ip);
		break;
	case Trustedtemp:
		fprint(debugfd, "\tTemp Trustedfile: %I", &np->ip);
		break;
	case Ctlfile:
		fprint(debugfd, "\tCtlfile");
		break;
	case Dummynode:
		fprint(debugfd, "\tDummynode");
		break;
	default:
		fprint(debugfd, "\tUnknown Node Type\n\n");
		return;
	}
	fprint(debugfd, " Parent %p Sib: %p\n\n", np->parent, np->sibs);
}

void
printfid(Fid *fp)
{
	fprint(debugfd, "FID: %d (%s %s) Busy: %d Open: %d\n", fp->fid, fp->name,
		fp->uid, fp->busy, fp->open);
	printnode(fp->node);
}

void
printtree(Node *np)
{
	printnode(np);
	if(np->d.type == IPaddr
		|| np->d.type == Acctaddr
		|| np->d.type == Trustedperm
		|| np->d.type == Trustedtemp)
			return;
	for (np = np->children; np; np = np->sibs)
		printtree(np);
}

static int
ipconv(Fmt *f)
{
	Cidraddr *ip;
	int i, j;
	char *p;

	ip = va_arg(f->args, Cidraddr*);
	p = (char*)&ip->ipaddr;
	i = 0;
	for (j = ip->mask; j; j <<= 1)
		i++;
	return fmtprint(f, "%d.%d.%d.%d/%d", p[3]&0xff, p[2]&0xff, p[1]&0xff, p[0]&0xff, i);
}