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/>.
29 #include <sys/signal.h>
45 static void chinit(void *idata)
50 /* This should never be able to fail other than for critical
51 * I/O errors or some such, since the path has already been
58 static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
65 for(head = pat->headers; head != NULL; head = head->next) {
66 headrmheader(req, head->name);
67 headappheader(req, head->name, head->value);
69 if(!strncmp(path, "./", 2) && path[2])
72 headappheader(req, "X-Ash-File", path);
73 stdforkserve(pat->fchild, req, fd, NULL, NULL);
75 if((ch = findchild(path, pat->childnm, &ccf)) == NULL) {
76 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
77 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
80 if((twd = ccf?ccf->path:NULL) != NULL) {
81 if(!strcmp(twd, ".")) {
83 } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) {
84 /* Should be an impossible case under the current (and
85 * foreseeable) scheme. */
86 simpleerror(fd, 500, "Server Error", "An internal server error occurred.");
89 path = path + strlen(twd) + 1;
92 headappheader(req, "X-Ash-File", path);
93 if(childhandle(ch, req, fd, chinit, twd))
94 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
98 static void handle404(struct hthead *req, int fd, char *path)
105 ch = findchild(tmp, ".notfound", &ccf);
106 if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
107 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
111 static void handlefile(struct hthead *req, int fd, char *path)
115 if((pat = findmatch(path, 0, 0)) == NULL) {
116 handle404(req, fd, path);
119 handle(req, fd, path, pat);
122 static char *findfile(char *path, char *name, struct stat *sb)
131 if((dir = opendir(path)) == NULL)
134 while((dent = readdir(dir)) != NULL) {
135 /* Ignore backup files.
136 * XXX: There is probably a better and more extensible way to
138 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
140 if((p = strchr(dent->d_name, '.')) == NULL)
142 if(p - dent->d_name != strlen(name))
144 if(strncmp(dent->d_name, name, strlen(name)))
146 fp = sprintf3("%s/%s", path, dent->d_name);
149 if(!S_ISREG(sb->st_mode))
158 static void handledir(struct hthead *req, int fd, char *path)
163 char *inm, *ipath, *cpath;
166 cpath = sprintf2("%s/", path);
167 cfs = getconfigs(cpath);
168 for(i = 0; cfs[i] != NULL; i++) {
169 if(cfs[i]->index != NULL) {
170 for(o = 0; cfs[i]->index[o] != NULL; o++) {
171 inm = cfs[i]->index[o];
172 ipath = sprintf2("%s/%s", path, inm);
173 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
174 handlefile(req, fd, ipath);
180 if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
181 handlefile(req, fd, ipath);
189 if((pat = findmatch(cpath, 0, 1)) != NULL) {
190 handle(req, fd, cpath, pat);
193 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
199 static int checkpath(struct hthead *req, int fd, char *path, char *rest);
201 static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
208 handle404(req, fd, sprintf3("%s/", path));
211 if(!stat(sprintf3("%s/%s", path, el), &sb)) {
212 if(S_ISDIR(sb.st_mode)) {
214 stdredir(req, fd, 301, sprintf3("%s/", el));
217 newpath = sprintf2("%s/%s", path, el);
218 rv = checkpath(req, fd, newpath, rest + 1);
221 } else if(S_ISREG(sb.st_mode)) {
222 newpath = sprintf2("%s/%s", path, el);
224 handlefile(req, fd, newpath);
228 handle404(req, fd, sprintf3("%s/", path));
231 if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
233 handlefile(req, fd, newpath);
240 static int checkdir(struct hthead *req, int fd, char *path, char *rest)
243 struct config *cf, *ccf;
246 cf = getconfig(path);
247 if(cf->capture != NULL) {
248 cpath = sprintf2("%s/", path);
249 if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) {
251 flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture);
252 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture);
259 if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
260 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
266 static int checkpath(struct hthead *req, int fd, char *path, char *rest)
274 if(!strncmp(path, "./", 2))
276 if(checkdir(req, fd, path, rest))
279 if((p = strchr(rest, '/')) == NULL) {
280 el = unquoteurl(rest);
283 char buf[p - rest + 1];
284 memcpy(buf, rest, p - rest);
286 el = unquoteurl(buf);
290 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
294 if(strchr(el, '/') || (!*el && *rest)) {
295 handle404(req, fd, sprintf3("%s/", path));
301 handledir(req, fd, path);
305 rv = checkentry(req, fd, path, rest, el);
313 static void serve(struct hthead *req, int fd)
316 if(!checkpath(req, fd, ".", req->rest))
317 handle404(req, fd, ".");
320 static void chldhandler(int sig)
325 while((pid = waitpid(-1, &st, WNOHANG)) > 0) {
327 flog(LOG_WARNING, "child process %i dumped core", pid);
331 static void sighandler(int sig)
335 static void usage(FILE *out)
337 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
340 int main(int argc, char **argv)
344 char *gcf, *lcf, *clcf;
350 while((c = getopt(argc, argv, "hNc:")) >= 0) {
366 if(argc - optind < 1) {
371 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
372 gconfig = readconfig(gcf);
377 if(strchr(lcf, '/') == NULL) {
378 if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
379 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
382 if((lconfig = readconfig(clcf)) == NULL)
386 if((lconfig = readconfig(lcf)) == NULL)
390 if(chdir(argv[optind])) {
391 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
394 signal(SIGCHLD, chldhandler);
395 signal(SIGPIPE, sighandler);
397 if((fd = recvreq(0, &req)) < 0) {
399 flog(LOG_ERR, "recvreq: %s", strerror(errno));