2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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/>.
40 static int parsefile(struct cfstate *s, FILE *in);
42 static int doinclude(struct cfstate *s, char *spec)
47 char *fbk, *dir, *fspec;
55 fspec = sprintf3("%s/%s", dirname(dir), spec);
58 if(glob(fspec, 0, NULL, &globm))
60 for(i = 0; i < globm.gl_pathc; i++) {
61 if((inc = fopen(globm.gl_pathv[i], "r")) != NULL) {
62 s->file = globm.gl_pathv[i];
63 if(parsefile(s, inc)) {
79 static int parsefile(struct cfstate *s, FILE *in)
84 int ind, indst[80], indl;
91 if(fgets(line, sizeof(line), in) == NULL) {
97 for(p = line + strlen(line) - 1; p >= line; p--) {
104 for(ind = 0, p = line; *p; p++) {
107 } else if(*p == '\t') {
108 ind = ind - (ind % 8) + 8;
113 if(!eof && (!*p || (*p == '#')))
117 if(ind > indst[indl]) {
120 s->res = tokenize("start");
128 s->res = tokenize("end");
133 while(ind < indst[indl]) {
135 s->res = tokenize("end");
139 if(ind > indst[indl]) {
140 flog(LOG_WARNING, "%s:%i: unexpected indentation level", s->file, s->lno);
148 argc = calen(w = tokenize(line));
150 /* Shouldn't happen, but... */
156 if(!strcmp(w[0], "include")) {
157 for(i = 1; i < argc; i++) {
158 if(doinclude(s, w[i])) {
168 if(!strcmp(w[0], "start") ||
169 !strcmp(w[0], "end") ||
170 !strcmp(w[0], "eof")) {
171 flog(LOG_WARNING, "%s:%i: illegal directive: %s", s->file, s->lno, w[0]);
180 static void parsefn(struct muth *mt, va_list args)
182 vavar(struct cfstate *, s);
186 s->file = sstrdup(file);
190 s->res = tokenize("eof");
197 char **getcfline(struct cfstate *s)
202 s->argc = calen(s->argv = s->res);
207 struct cfstate *mkcfparser(FILE *in, char *name)
212 s->pf = mustart(parsefn, s, in, name);
216 void freecfparser(struct cfstate *s)
224 char *findstdconf(char *name)
226 char *path, *p, *p2, *t;
228 if((path = getenv("PATH")) == NULL)
230 path = sstrdup(path);
231 for(p = strtok(path, ":"); p != NULL; p = strtok(NULL, ":")) {
232 if((p2 = strrchr(p, '/')) == NULL)
235 if(!access(t = sprintf2("%s/etc/%s", p, name), R_OK)) {
245 static struct child *newchild(char *name, int type)
250 ch->name = sstrdup(name);
256 void freechild(struct child *ch)
267 void skipcfblock(struct cfstate *s)
273 if(!strcmp(w[0], "end") || !strcmp(w[0], "eof"))
278 struct child *parsechild(struct cfstate *s)
285 if(!strcmp(s->argv[0], "child")) {
288 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
292 ch = newchild(s->argv[1], CH_SOCKET);
293 } else if(!strcmp(s->argv[0], "fchild")) {
296 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
300 ch = newchild(s->argv[1], CH_FORK);
307 if(!strcmp(s->argv[0], "exec")) {
309 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", s->file, s->lno);
312 ch->argv = szmalloc(sizeof(*ch->argv) * s->argc);
313 for(i = 0; i < s->argc - 1; i++)
314 ch->argv[i] = sstrdup(s->argv[i + 1]);
315 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
318 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in child declaration", s->file, s->lno, s->argv[0]);
321 if(ch->argv == NULL) {
322 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", s->file, sl, ch->name);
329 int childhandle(struct child *ch, struct hthead *req, int fd)
331 if(ch->type == CH_SOCKET) {
333 ch->fd = stdmkchild(ch->argv);
334 if(sendreq(ch->fd, req, fd)) {
336 /* Assume that the child has crashed and restart it. */
338 ch->fd = stdmkchild(ch->argv);
339 if(!sendreq(ch->fd, req, fd))
342 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
347 } else if(ch->type == CH_FORK) {
348 if(stdforkserve(ch->argv, req, fd) < 0)