2ea28d5f54c1fb82cd65e261daf0b6e22b462f3e
[ashd.git] / src / patplex.c
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 <signal.h>
24 #include <errno.h>
25 #include <ctype.h>
26 #include <regex.h>
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <utils.h>
32 #include <log.h>
33 #include <req.h>
34 #include <proc.h>
35 #include <resp.h>
36 #include <cf.h>
37
38 #define PAT_REST 0
39 #define PAT_URL 1
40 #define PAT_METHOD 2
41 #define PAT_HEADER 3
42 #define PAT_ALL 4
43 #define PAT_DEFAULT 5
44
45 #define PATFL_MSS 1
46
47 struct config {
48     struct child *children;
49     struct pattern *patterns;
50 };
51
52 struct rule {
53     int type;
54     int fl;
55     char *header;
56     regex_t *pattern;
57 };
58
59 struct pattern {
60     struct pattern *next;
61     char *childnm;
62     struct rule **rules;
63     char *restpat;
64 };
65
66 static struct config *gconfig, *lconfig;
67
68 static void freepattern(struct pattern *pat)
69 {
70     struct rule **rule;
71     
72     for(rule = pat->rules; *rule; rule++) {
73         if((*rule)->header != NULL)
74             free((*rule)->header);
75         if((*rule)->pattern != NULL) {
76             regfree((*rule)->pattern);
77             free((*rule)->pattern);
78         }
79         free(*rule);
80     }
81     if(pat->childnm != NULL)
82         free(pat->childnm);
83     free(pat);
84 }
85
86 static struct child *getchild(struct config *cf, char *name)
87 {
88     struct child *ch;
89     
90     for(ch = cf->children; ch; ch = ch->next) {
91         if(!strcmp(ch->name, name))
92             break;
93     }
94     return(ch);
95 }
96
97 static struct rule *newrule(struct pattern *pat)
98 {
99     int i;
100     struct rule *rule;
101     
102     for(i = 0; pat->rules[i]; i++);
103     pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
104     rule = pat->rules[i] = szmalloc(sizeof(*rule));
105     pat->rules[i + 1] = NULL;
106     return(rule);
107 }
108
109 static struct pattern *newpattern(void)
110 {
111     struct pattern *pat;
112     
113     omalloc(pat);
114     pat->rules = szmalloc(sizeof(*pat->rules));
115     return(pat);
116 }
117
118 static regex_t *regalloc(char *regex, int flags)
119 {
120     regex_t *ret;
121     int res;
122     char errbuf[256];
123     
124     omalloc(ret);
125     if((res = regcomp(ret, regex, flags | REG_EXTENDED)) != 0) {
126         regerror(res, ret, errbuf, sizeof(errbuf));
127         flog(LOG_WARNING, "%s: %s", regex, errbuf);
128         free(ret);
129         return(NULL);
130     }
131     return(ret);
132 }
133
134 static struct pattern *parsepattern(struct cfstate *s)
135 {
136     struct pattern *pat;
137     int sl;
138     struct rule *rule;
139     regex_t *regex;
140     int rxfl;
141     
142     if(!strcmp(s->argv[0], "match")) {
143         s->expstart = 1;
144         pat = newpattern();
145     } else {
146         return(NULL);
147     }
148     
149     sl = s->lno;
150     while(1) {
151         getcfline(s);
152         if(!strcmp(s->argv[0], "point") ||
153            !strcmp(s->argv[0], "url") ||
154            !strcmp(s->argv[0], "method")) {
155             if(s->argc < 2) {
156                 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
157                 continue;
158             }
159             if(s->argc >= 3) {
160                 if(strchr(s->argv[2], 'i'))
161                     rxfl |= REG_ICASE;
162             }
163             if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
164                 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
165                 continue;
166             }
167             rule = newrule(pat);
168             if(!strcmp(s->argv[0], "point"))
169                 rule->type = PAT_REST;
170             else if(!strcmp(s->argv[0], "url"))
171                 rule->type = PAT_URL;
172             else if(!strcmp(s->argv[0], "method"))
173                 rule->type = PAT_METHOD;
174             rule->pattern = regex;
175             if(s->argc >= 3) {
176                 if(strchr(s->argv[2], 's'))
177                     rule->fl |= PATFL_MSS;
178             }
179         } else if(!strcmp(s->argv[0], "header")) {
180             if(s->argc < 3) {
181                 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
182                 continue;
183             }
184             if(s->argc >= 4) {
185                 if(strchr(s->argv[3], 'i'))
186                     rxfl |= REG_ICASE;
187             }
188             if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
189                 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
190                 continue;
191             }
192             rule = newrule(pat);
193             rule->type = PAT_HEADER;
194             rule->header = sstrdup(s->argv[1]);
195             rule->pattern = regex;
196             if(s->argc >= 4) {
197                 if(strchr(s->argv[3], 's'))
198                     rule->fl |= PATFL_MSS;
199             }
200         } else if(!strcmp(s->argv[0], "all")) {
201             newrule(pat)->type = PAT_ALL;
202         } else if(!strcmp(s->argv[0], "default")) {
203             newrule(pat)->type = PAT_DEFAULT;
204         } else if(!strcmp(s->argv[0], "handler")) {
205             if(s->argc < 2) {
206                 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
207                 continue;
208             }
209             if(pat->childnm != NULL)
210                 free(pat->childnm);
211             pat->childnm = sstrdup(s->argv[1]);
212         } else if(!strcmp(s->argv[0], "restpat")) {
213             if(s->argc < 2) {
214                 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
215                 continue;
216             }
217             if(pat->restpat != NULL)
218                 free(pat->restpat);
219             pat->restpat = sstrdup(s->argv[1]);
220         } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
221             break;
222         } else {
223             flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
224         }
225     }
226     
227     if(pat->rules[0] == NULL) {
228         flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
229         freepattern(pat);
230         return(NULL);
231     }
232     if(pat->childnm == NULL) {
233         flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
234         freepattern(pat);
235         return(NULL);
236     }
237     return(pat);
238 }
239
240 static struct config *readconfig(char *filename)
241 {
242     struct cfstate *s;
243     struct config *cf;
244     struct child *ch;
245     struct pattern *pat;
246     FILE *in;
247     
248     if((in = fopen(filename, "r")) == NULL) {
249         flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
250         return(NULL);
251     }
252     s = mkcfparser(in, filename);
253     omalloc(cf);
254     
255     while(1) {
256         getcfline(s);
257         if((ch = parsechild(s)) != NULL) {
258             ch->next = cf->children;
259             cf->children = ch;
260         } else if((pat = parsepattern(s)) != NULL) {
261             pat->next = cf->patterns;
262             cf->patterns = pat;
263         } else if(!strcmp(s->argv[0], "eof")) {
264             break;
265         } else {
266             flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
267         }
268     }
269     
270     freecfparser(s);
271     fclose(in);
272     return(cf);
273 }
274
275 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
276 {
277     char *p, *p2, *hdr;
278     int mc;
279     struct charbuf buf;
280     
281     if(mstr == NULL)
282         mc = 0;
283     else
284         for(mc = 0; mstr[mc]; mc++);
285     bufinit(buf);
286     for(p = pat->restpat; *p; ) {
287         if(*p == '$') {
288             p++;
289             if((*p >= '0') && (*p <= '9')) {
290                 if(*p - '0' < mc)
291                     bufcatstr(buf, mstr[*p - '0']);
292                 p++;
293             } else if(*p == '_') {
294                 bufcatstr(buf, req->rest);
295                 p++;
296             } else if(*p == '$') {
297                 bufadd(buf, '$');
298                 p++;
299             } else if(*p == '{') {
300                 if((p2 = strchr(p, '}')) == NULL) {
301                     p++;
302                 } else {
303                     hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
304                     if(hdr)
305                         bufcatstr(buf, hdr);
306                 }
307             } else if(!*p) {
308             }
309         } else {
310             bufadd(buf, *(p++));
311         }
312     }
313     bufadd(buf, 0);
314     replrest(req, buf.b);
315     buffree(buf);
316 }
317
318 static char *findmatch(struct config *cf, struct hthead *req, int trydefault)
319 {
320     int i, o;
321     struct pattern *pat;
322     struct rule *rule;
323     int rmo, matched;
324     char *pstr;
325     char **mstr;
326     regmatch_t gr[10];
327     
328     mstr = NULL;
329     for(pat = cf->patterns; pat != NULL; pat = pat->next) {
330         rmo = -1;
331         for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
332             matched = 0;
333             if(rule->type == PAT_REST) {
334                 if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
335                     rmo = gr[0].rm_eo;
336                 else
337                     break;
338             } else if(rule->type == PAT_URL) {
339                 if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
340                     break;
341             } else if(rule->type == PAT_METHOD) {
342                 if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
343                     break;
344             } else if(rule->type == PAT_HEADER) {
345                 if(!(pstr = getheader(req, rule->header)))
346                     break;
347                 if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
348                     break;
349             } else if(rule->type == PAT_ALL) {
350             } else if(rule->type == PAT_DEFAULT) {
351                 if(!trydefault)
352                     break;
353             }
354             if(matched && (rule->fl & PATFL_MSS)) {
355                 if(mstr) {
356                     flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
357                     freeca(mstr);
358                 }
359                 for(o = 0; o < 10; o++) {
360                     if(gr[o].rm_so < 0)
361                         break;
362                 }
363                 mstr = szmalloc((o + 1) * sizeof(*mstr));
364                 for(o = 0; o < 10; o++) {
365                     if(gr[o].rm_so < 0)
366                         break;
367                     mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
368                     memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
369                     mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
370                 }
371             }
372         }
373         if(!rule) {
374             if(pat->restpat) {
375                 exprestpat(req, pat, mstr);
376             } else if(rmo != -1) {
377                 replrest(req, req->rest + rmo);
378             }
379             if(mstr)
380                 freeca(mstr);
381             return(pat->childnm);
382         }
383         if(mstr) {
384             freeca(mstr);
385             mstr = NULL;
386         }
387     }
388     return(NULL);
389 }
390
391 static void serve(struct hthead *req, int fd)
392 {
393     char *chnm;
394     struct child *ch;
395     
396     chnm = NULL;
397     if(chnm == NULL)
398         chnm = findmatch(lconfig, req, 0);
399     if(chnm == NULL)
400         chnm = findmatch(lconfig, req, 1);
401     if(gconfig != NULL) {
402         if(chnm == NULL)
403             chnm = findmatch(gconfig, req, 0);
404         if(chnm == NULL)
405             chnm = findmatch(gconfig, req, 1);
406     }
407     if(chnm == NULL) {
408         simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
409         return;
410     }
411     ch = NULL;
412     if(ch == NULL)
413         ch = getchild(lconfig, chnm);
414     if(gconfig != NULL) {
415         if(ch == NULL)
416             ch = getchild(gconfig, chnm);
417     }
418     if(ch == NULL) {
419         flog(LOG_ERR, "child %s requested, but was not declared", chnm);
420         simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", chnm);
421         return;
422     }
423     
424     if(childhandle(ch, req, fd))
425         simpleerror(fd, 500, "Server Error", "The request handler crashed.");
426 }
427
428 static void usage(FILE *out)
429 {
430     fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
431 }
432
433 int main(int argc, char **argv)
434 {
435     int c;
436     int nodef;
437     char *gcf;
438     struct hthead *req;
439     int fd;
440     
441     nodef = 0;
442     while((c = getopt(argc, argv, "hN")) >= 0) {
443         switch(c) {
444         case 'h':
445             usage(stdout);
446             exit(0);
447         case 'N':
448             nodef = 1;
449             break;
450         default:
451             usage(stderr);
452             exit(1);
453         }
454     }
455     if(argc - optind < 1) {
456         usage(stderr);
457         exit(1);
458     }
459     if(!nodef) {
460         if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
461             gconfig = readconfig(gcf);
462             free(gcf);
463         }
464     }
465     lconfig = readconfig(argv[optind]);
466     signal(SIGCHLD, SIG_IGN);
467     while(1) {
468         if((fd = recvreq(0, &req)) < 0) {
469             if(errno != 0)
470                 flog(LOG_ERR, "recvreq: %s", strerror(errno));
471             break;
472         }
473         serve(req, fd);
474         freehthead(req);
475         close(fd);
476     }
477     return(0);
478 }