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/>.
26 #include <sys/socket.h>
48 static int parsefile(struct cfstate *s, FILE *in);
49 static void stdmerge(struct child *old, struct child *new);
50 static int stdhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata);
51 static void stddestroy(struct child *ch);
53 static int doinclude(struct cfstate *s, char *spec)
58 char *fbk, *dir, *fspec;
66 fspec = sprintf3("%s/%s", dirname(dir), spec);
69 if(glob(fspec, 0, NULL, &globm))
71 for(i = 0; i < globm.gl_pathc; i++) {
72 if((inc = fopen(globm.gl_pathv[i], "r")) != NULL) {
73 s->file = globm.gl_pathv[i];
74 if(parsefile(s, inc)) {
90 static int parsefile(struct cfstate *s, FILE *in)
95 int ind, indst[80], indl;
102 if(fgets(line, sizeof(line), in) == NULL) {
108 for(p = line + strlen(line) - 1; p >= line; p--) {
115 for(ind = 0, p = line; *p; p++) {
118 } else if(*p == '\t') {
119 ind = ind - (ind % 8) + 8;
124 if(!eof && (!*p || (*p == '#')))
128 if(ind > indst[indl]) {
131 s->res = tokenize("start");
139 s->res = tokenize("end");
144 while(ind < indst[indl]) {
146 s->res = tokenize("end");
150 if(ind > indst[indl]) {
151 flog(LOG_WARNING, "%s:%i: unexpected indentation level", s->file, s->lno);
159 argc = calen(w = tokenize(line));
161 /* Shouldn't happen, but... */
167 if(!strcmp(w[0], "include")) {
168 for(i = 1; i < argc; i++) {
169 if(doinclude(s, w[i])) {
179 if(!strcmp(w[0], "start") ||
180 !strcmp(w[0], "end") ||
181 !strcmp(w[0], "eof")) {
182 flog(LOG_WARNING, "%s:%i: illegal directive: %s", s->file, s->lno, w[0]);
191 static void parsefn(struct muth *mt, va_list args)
193 vavar(struct cfstate *, s);
197 s->file = sstrdup(file);
201 s->res = tokenize("eof");
208 char **getcfline(struct cfstate *s)
213 s->argc = calen(s->argv = s->res);
218 struct cfstate *mkcfparser(FILE *in, char *name)
223 s->pf = mustart(parsefn, s, in, name);
227 void freecfparser(struct cfstate *s)
235 char *findstdconf(char *name)
237 char *home, *path, *p, *p2, *t;
239 if((home = getenv("HOME")) != NULL) {
240 if(!access(t = sprintf2("%s/.ashd/etc/%s", home, name), R_OK))
244 if((path = getenv("PATH")) != NULL) {
245 path = sstrdup(path);
246 for(p = strtok(path, ":"); p != NULL; p = strtok(NULL, ":")) {
247 if((p2 = strrchr(p, '/')) == NULL)
250 if(!access(t = sprintf2("%s/etc/%s", p, name), R_OK)) {
261 struct child *newchild(char *name, struct chandler *iface, void *pdata)
266 ch->name = sstrdup(name);
272 void freechild(struct child *ch)
274 if(ch->iface->destroy != NULL)
275 ch->iface->destroy(ch);
281 void mergechildren(struct child *dst, struct child *src)
283 struct child *ch1, *ch2;
285 for(ch1 = dst; ch1 != NULL; ch1 = ch1->next) {
286 for(ch2 = src; ch2 != NULL; ch2 = ch2->next) {
287 if(ch1->iface->merge && !strcmp(ch1->name, ch2->name)) {
288 ch1->iface->merge(ch1, ch2);
295 void skipcfblock(struct cfstate *s)
301 if(!strcmp(w[0], "end") || !strcmp(w[0], "eof"))
306 static struct chandler stdhandler = {
309 .destroy = stddestroy,
312 static char **expandargs(struct stdchild *sd)
315 char **ret, *p, *p2, *p3, *np, *env;
318 ret = szmalloc(sizeof(*ret) * (calen(sd->argv) + 1));
320 for(i = 0; sd->argv[i] != NULL; i++) {
321 if((p = strchr(sd->argv[i], '$')) == NULL) {
322 ret[i] = sstrdup(sd->argv[i]);
325 for(p2 = sd->argv[i]; p != NULL; p2 = np, p = strchr(np, '$')) {
326 bufcat(exp, p2, p - p2);
328 if((p3 = strchr((p += 2), '}')) == NULL)
332 for(p3 = ++p; *p3; p3++) {
333 if(!(((*p3 >= 'a') && (*p3 <= 'z')) ||
334 ((*p3 >= 'A') && (*p3 <= 'Z')) ||
335 ((*p3 >= '0') && (*p3 <= '9')) ||
342 char temp[(p3 - p) + 1];
343 memcpy(temp, p, p3 - p);
345 if((env = getenv(temp)) != NULL)
349 ret[i] = sstrdup(exp.b);
357 static int stdhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
359 struct stdchild *sd = ch->pdata;
363 void stdinit(void *data)
367 for(i = 0; sd->envp[i]; i += 2)
368 putenv(sprintf2("%s=%s", sd->envp[i], sd->envp[i + 1]));
373 if(sd->type == CH_SOCKET) {
375 args = expandargs(sd);
376 sd->fd = stdmkchild(args, stdinit, idata);
379 if(sendreq2(sd->fd, req, fd, MSG_NOSIGNAL | MSG_DONTWAIT)) {
381 if((serr == EPIPE) || (serr == ECONNRESET)) {
382 /* Assume that the child has crashed and restart it. */
384 args = expandargs(sd);
385 sd->fd = stdmkchild(args, stdinit, idata);
387 if(!sendreq2(sd->fd, req, fd, MSG_NOSIGNAL | MSG_DONTWAIT))
390 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(serr));
397 } else if(sd->type == CH_FORK) {
398 args = expandargs(sd);
399 if(stdforkserve(args, req, fd, chinit, idata) < 0) {
408 static void stdmerge(struct child *dst, struct child *src)
410 struct stdchild *od, *nd;
412 if(src->iface == &stdhandler) {
420 static void stddestroy(struct child *ch)
422 struct stdchild *d = ch->pdata;
433 struct child *parsechild(struct cfstate *s)
437 struct charvbuf envbuf;
442 if(!strcmp(s->argv[0], "child")) {
445 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
449 ch = newchild(s->argv[1], &stdhandler, omalloc(d));
451 } else if(!strcmp(s->argv[0], "fchild")) {
454 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
458 ch = newchild(s->argv[1], &stdhandler, omalloc(d));
468 if(!strcmp(s->argv[0], "exec")) {
470 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", s->file, s->lno);
473 d->argv = szmalloc(sizeof(*d->argv) * s->argc);
474 for(i = 0; i < s->argc - 1; i++)
475 d->argv[i] = sstrdup(s->argv[i + 1]);
476 } else if(!strcmp(s->argv[0], "env")) {
478 flog(LOG_WARNING, "%s:%i: too few parameters to `env'", s->file, s->lno);
481 bufadd(envbuf, sstrdup(s->argv[1]));
482 bufadd(envbuf, sstrdup(s->argv[2]));
483 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
486 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in child declaration", s->file, s->lno, s->argv[0]);
489 bufadd(envbuf, NULL);
491 if(d->argv == NULL) {
492 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", s->file, sl, ch->name);
499 int childhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
501 return(ch->iface->handle(ch, req, fd, chinit, idata));