Fixed config parsing bug.
[ashd.git] / src / dirplex.c
CommitLineData
992ce9ef
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23#include <errno.h>
24#include <sys/stat.h>
25#include <ctype.h>
26#include <dirent.h>
27#include <fnmatch.h>
17e55136 28#include <time.h>
992ce9ef
FT
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <utils.h>
34#include <mt.h>
35#include <log.h>
36#include <req.h>
37#include <proc.h>
d422fdfd 38#include <resp.h>
06c1a718 39#include <cf.h>
992ce9ef 40
992ce9ef
FT
41#define PAT_BASENAME 0
42#define PAT_PATHNAME 1
43#define PAT_ALL 2
44#define PAT_DEFAULT 3
45
46struct config {
47 struct config *next, *prev;
48 char *path;
17e55136 49 time_t mtime, lastck;
992ce9ef
FT
50 struct child *children;
51 struct pattern *patterns;
52};
53
992ce9ef
FT
54struct rule {
55 int type;
56 char *pattern;
57};
58
59struct pattern {
60 struct pattern *next;
61 char *childnm;
62 struct rule **rules;
63};
64
65struct config *cflist;
17e55136 66static time_t now;
992ce9ef 67
992ce9ef
FT
68static void freepattern(struct pattern *pat)
69{
70 struct rule **rule;
71
72 for(rule = pat->rules; *rule; rule++) {
73 if((*rule)->pattern != NULL)
74 free((*rule)->pattern);
75 free(*rule);
76 }
77 if(pat->childnm != NULL)
78 free(pat->childnm);
79 free(pat);
80}
81
82static void freeconfig(struct config *cf)
83{
84 struct child *ch, *nch;
85 struct pattern *pat, *npat;
86
87 if(cf->prev != NULL)
88 cf->prev->next = cf->next;
89 if(cf->next != NULL)
90 cf->next->prev = cf->prev;
91 if(cf == cflist)
92 cflist = cf->next;
93 free(cf->path);
94 for(ch = cf->children; ch != NULL; ch = nch) {
95 nch = ch->next;
96 freechild(ch);
97 }
98 for(pat = cf->patterns; pat != NULL; pat = npat) {
99 npat = pat->next;
100 freepattern(pat);
101 }
102 free(cf);
103}
104
992ce9ef
FT
105static struct child *getchild(struct config *cf, char *name)
106{
107 struct child *ch;
108
109 for(ch = cf->children; ch; ch = ch->next) {
110 if(!strcmp(ch->name, name))
111 break;
112 }
113 return(ch);
114}
115
116static struct rule *newrule(struct pattern *pat)
117{
118 int i;
119 struct rule *rule;
120
121 for(i = 0; pat->rules[i]; i++);
122 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
cceb3611 123 rule = pat->rules[i] = szmalloc(sizeof(*rule));
992ce9ef
FT
124 pat->rules[i + 1] = NULL;
125 return(rule);
126}
127
128static struct pattern *newpattern(void)
129{
130 struct pattern *pat;
131
132 omalloc(pat);
133 pat->rules = szmalloc(sizeof(*pat->rules));
134 return(pat);
135}
136
06c1a718
FT
137static struct pattern *parsepattern(struct cfstate *s)
138{
139 struct pattern *pat;
140 struct rule *rule;
141 int sl;
142
143 if(!strcmp(s->argv[0], "match")) {
144 s->expstart = 1;
145 pat = newpattern();
146 } else {
147 return(NULL);
148 }
149
150 sl = s->lno;
151 while(1) {
152 getcfline(s);
153 if(!strcmp(s->argv[0], "filename")) {
154 if(s->argc < 2) {
155 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
156 continue;
157 }
158 rule = newrule(pat);
159 rule->type = PAT_BASENAME;
160 rule->pattern = sstrdup(s->argv[1]);
161 } else if(!strcmp(s->argv[0], "pathname")) {
162 if(s->argc < 2) {
163 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
164 continue;
165 }
166 rule = newrule(pat);
167 rule->type = PAT_PATHNAME;
168 rule->pattern = sstrdup(s->argv[1]);
169 } else if(!strcmp(s->argv[0], "all")) {
170 newrule(pat)->type = PAT_ALL;
171 } else if(!strcmp(s->argv[0], "default")) {
172 newrule(pat)->type = PAT_DEFAULT;
173 } else if(!strcmp(s->argv[0], "handler")) {
174 if(s->argc < 2) {
175 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
176 continue;
177 }
178 if(pat->childnm != NULL)
179 free(pat->childnm);
180 pat->childnm = sstrdup(s->argv[1]);
181 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
182 break;
183 } else {
184 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
185 }
186 }
187
188 if(pat->rules[0] == NULL) {
189 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
190 freepattern(pat);
191 return(NULL);
192 }
193 if(pat->childnm == NULL) {
194 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
195 freepattern(pat);
196 return(NULL);
197 }
198 return(pat);
199}
200
17e55136
FT
201static struct config *emptyconfig(void)
202{
203 struct config *cf;
204
205 omalloc(cf);
206 return(cf);
207}
208
209static struct config *readconfig(char *file)
992ce9ef 210{
06c1a718
FT
211 struct cfstate *s;
212 FILE *in;
992ce9ef 213 struct config *cf;
992ce9ef
FT
214 struct child *child;
215 struct pattern *pat;
992ce9ef 216
17e55136
FT
217 if((in = fopen(file, "r")) == NULL) {
218 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
992ce9ef 219 return(NULL);
06c1a718 220 }
17e55136
FT
221 s = mkcfparser(in, file);
222 cf = emptyconfig();
06c1a718
FT
223
224 while(1) {
225 getcfline(s);
226 if((child = parsechild(s)) != NULL) {
227 child->next = cf->children;
228 cf->children = child;
229 } else if((pat = parsepattern(s)) != NULL) {
230 pat->next = cf->patterns;
231 cf->patterns = pat;
232 } else if(!strcmp(s->argv[0], "eof")) {
233 break;
234 } else {
235 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
992ce9ef 236 }
06c1a718 237 }
992ce9ef 238
06c1a718
FT
239 freecfparser(s);
240 fclose(in);
992ce9ef
FT
241 return(cf);
242}
243
244static struct config *getconfig(char *path)
245{
246 struct config *cf;
247 struct stat sb;
17e55136
FT
248 char *fn;
249 time_t mtime;
992ce9ef 250
17e55136 251 fn = sprintf3("%s/.htrc", path);
992ce9ef
FT
252 for(cf = cflist; cf != NULL; cf = cf->next) {
253 if(!strcmp(cf->path, path)) {
17e55136
FT
254 if(now - cf->lastck > 5) {
255 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
256 freeconfig(cf);
257 break;
258 }
992ce9ef 259 }
17e55136 260 cf->lastck = now;
992ce9ef
FT
261 return(cf);
262 }
263 }
17e55136
FT
264 if(access(fn, R_OK) || stat(fn, &sb)) {
265 cf = emptyconfig();
266 mtime = 0;
267 } else {
268 if((cf = readconfig(fn)) == NULL)
269 return(NULL);
270 mtime = sb.st_mtime;
992ce9ef 271 }
17e55136
FT
272 cf->path = sstrdup(path);
273 cf->mtime = mtime;
274 cf->lastck = now;
275 cf->next = cflist;
276 cflist = cf;
992ce9ef
FT
277 return(cf);
278}
279
8dce6d19 280static struct config **getconfigs(char *file)
992ce9ef 281{
8dce6d19
FT
282 static struct config **ret = NULL;
283 struct {
284 struct config **b;
285 size_t s, d;
286 } buf;
992ce9ef 287 struct config *cf;
8dce6d19
FT
288 char *tmp, *p;
289
290 if(ret != NULL)
291 free(ret);
292 bufinit(buf);
293 tmp = sstrdup(file);
992ce9ef 294 while(1) {
8dce6d19 295 if((p = strrchr(tmp, '/')) == NULL)
992ce9ef 296 break;
8dce6d19
FT
297 *p = 0;
298 if((cf = getconfig(tmp)) != NULL)
299 bufadd(buf, cf);
300 }
301 free(tmp);
302 if((cf = getconfig(".")) != NULL)
303 bufadd(buf, cf);
304 bufadd(buf, NULL);
305 return(ret = buf.b);
306}
307
308static struct child *findchild(char *file, char *name)
309{
310 int i;
311 struct config **cfs;
312 struct child *ch;
313
314 cfs = getconfigs(file);
315 for(i = 0; cfs[i] != NULL; i++) {
316 if((ch = getchild(cfs[i], name)) != NULL)
992ce9ef
FT
317 break;
318 }
992ce9ef
FT
319 return(ch);
320}
321
322static struct pattern *findmatch(char *file, int trydefault)
323{
8dce6d19
FT
324 int i, c;
325 char *bn;
326 struct config **cfs;
992ce9ef
FT
327 struct pattern *pat;
328 struct rule *rule;
329
330 if((bn = strrchr(file, '/')) != NULL)
331 bn++;
332 else
333 bn = file;
8dce6d19
FT
334 cfs = getconfigs(file);
335 for(c = 0; cfs[c] != NULL; c++) {
336 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
992ce9ef
FT
337 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
338 if(rule->type == PAT_BASENAME) {
339 if(fnmatch(rule->pattern, bn, 0))
340 break;
341 } else if(rule->type == PAT_PATHNAME) {
342 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
343 break;
344 } else if(rule->type == PAT_ALL) {
345 } else if(rule->type == PAT_DEFAULT) {
346 if(!trydefault)
347 break;
348 }
349 }
350 if(!rule)
8dce6d19 351 return(pat);
992ce9ef
FT
352 }
353 }
8dce6d19 354 return(NULL);
992ce9ef
FT
355}
356
54cefaba 357static void handlefile(struct hthead *req, int fd, char *path)
992ce9ef 358{
992ce9ef
FT
359 struct pattern *pat;
360 struct child *ch;
54cefaba
FT
361
362 headappheader(req, "X-Ash-File", path);
363 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
d58ed97c 364 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
992ce9ef
FT
365 return;
366 }
54cefaba 367 if((ch = findchild(path, pat->childnm)) == NULL) {
992ce9ef 368 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
d422fdfd 369 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
992ce9ef
FT
370 return;
371 }
372
d1b065b5
FT
373 if(childhandle(ch, req, fd))
374 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
54cefaba
FT
375}
376
377static void handledir(struct hthead *req, int fd, char *path)
378{
d422fdfd 379 /* XXX: Todo */
06c1a718 380 simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet.");
54cefaba
FT
381}
382
383static int checkdir(struct hthead *req, int fd, char *path)
384{
385 return(0);
386}
387
388static void serve(struct hthead *req, int fd)
389{
390 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
391 struct stat sb;
392 DIR *dir;
393 struct dirent *dent;
992ce9ef 394
17e55136 395 now = time(NULL);
54cefaba
FT
396 nm = req->rest;
397 path = sstrdup(".");
398 p = nm;
399 while(1) {
400 if((p2 = strchr(p, '/')) == NULL) {
401 } else {
402 *(p2++) = 0;
403 }
404
405 if(!*p) {
406 if(p2 == NULL) {
407 if(stat(path, &sb)) {
408 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
d422fdfd 409 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
54cefaba
FT
410 goto fail;
411 }
412 break;
413 } else {
d422fdfd 414 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
54cefaba
FT
415 goto fail;
416 }
417 }
d422fdfd
FT
418 if(*p == '.') {
419 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
54cefaba 420 goto fail;
d422fdfd 421 }
54cefaba
FT
422
423 getconfig(path);
424
425 /*
426 * First, check the name verbatimely:
427 */
428 buf = sprintf3("%s/%s", path, p);
429 if(!stat(buf, &sb)) {
430 if(S_ISDIR(sb.st_mode)) {
431 tmp = path;
432 if(!strcmp(path, "."))
433 path = sstrdup(p);
434 else
435 path = sprintf2("%s/%s", path, p);
436 free(tmp);
755faed0
FT
437 if(p2 == NULL) {
438 stdredir(req, fd, 301, sprintf3("%s/", p));
439 goto out;
440 }
54cefaba
FT
441 if(checkdir(req, fd, path))
442 break;
443 goto next;
444 }
445 if(S_ISREG(sb.st_mode)) {
446 tmp = path;
447 path = sprintf2("%s/%s", path, p);
448 free(tmp);
449 break;
450 }
d422fdfd 451 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
54cefaba
FT
452 goto fail;
453 }
454
455 /*
456 * Check the file extensionlessly:
457 */
458 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
459 while((dent = readdir(dir)) != NULL) {
460 buf = sprintf3("%s/%s", path, dent->d_name);
461 if((p3 = strchr(dent->d_name, '.')) != NULL)
462 *p3 = 0;
463 if(strcmp(dent->d_name, p))
464 continue;
465 if(stat(buf, &sb))
466 continue;
467 if(!S_ISREG(sb.st_mode))
468 continue;
469 tmp = path;
470 path = sstrdup(buf);
471 free(tmp);
472 break;
473 }
474 closedir(dir);
475 if(dent != NULL)
476 break;
477 }
478
d422fdfd 479 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
54cefaba
FT
480 goto fail;
481
482 next:
483 if(p2 == NULL)
484 break;
485 p = p2;
486 }
487 if(p2 == NULL)
488 replrest(req, "");
489 else
490 replrest(req, p2);
491 if(!strncmp(path, "./", 2))
492 memmove(path, path + 2, strlen(path + 2) + 1);
493 if(S_ISDIR(sb.st_mode)) {
494 handledir(req, fd, path);
495 } else if(S_ISREG(sb.st_mode)) {
496 handlefile(req, fd, path);
497 } else {
d422fdfd 498 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
54cefaba
FT
499 goto fail;
500 }
501 goto out;
502
503fail:
d422fdfd 504 /* No special handling, for now at least. */
54cefaba
FT
505out:
506 free(path);
992ce9ef
FT
507}
508
509int main(int argc, char **argv)
510{
511 struct hthead *req;
512 int fd;
513
514 if(argc < 2) {
515 flog(LOG_ERR, "usage: dirplex DIR");
516 exit(1);
517 }
518 if(chdir(argv[1])) {
519 flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
520 exit(1);
521 }
522 signal(SIGCHLD, SIG_IGN);
523 while(1) {
524 if((fd = recvreq(0, &req)) < 0) {
525 if(errno != 0)
526 flog(LOG_ERR, "recvreq: %s", strerror(errno));
527 break;
528 }
529 serve(req, fd);
530 freehthead(req);
531 close(fd);
532 }
533 return(0);
534}