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/>.
30 #include <sys/signal.h>
46 static void chinit(void *idata)
51 /* This should never be able to fail other than for critical
52 * I/O errors or some such, since the path has already been
59 static void childerror(struct hthead *req, int fd)
62 simpleerror(fd, 500, "Server Error", "The request handler is overloaded.");
64 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
67 static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
74 for(head = pat->headers; head != NULL; head = head->next) {
75 headrmheader(req, head->name);
76 headappheader(req, head->name, head->value);
78 if(!strncmp(path, "./", 2) && path[2])
81 headappheader(req, "X-Ash-File", path);
82 stdforkserve(pat->fchild, req, fd, NULL, NULL);
84 if((ch = findchild(path, pat->childnm, &ccf)) == NULL) {
85 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
86 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
89 if((twd = ccf?ccf->path:NULL) != NULL) {
90 if(!strcmp(twd, ".")) {
92 } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) {
93 /* Should be an impossible case under the current (and
94 * foreseeable) scheme. */
95 simpleerror(fd, 500, "Server Error", "An internal server error occurred.");
98 path = path + strlen(twd) + 1;
101 headappheader(req, "X-Ash-File", path);
102 if(childhandle(ch, req, fd, chinit, twd))
107 static void handle404(struct hthead *req, int fd, char *path)
113 char tmp[strlen(path) + 1];
115 if((pat = findmatch(tmp, 0, PT_NOTFOUND)) != NULL) {
116 handle(req, fd, tmp, pat);
118 ch = findchild(tmp, ".notfound", &ccf);
119 if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
124 static void handlefile(struct hthead *req, int fd, char *path)
128 if((pat = findmatch(path, 0, PT_FILE)) == NULL) {
129 handle404(req, fd, path);
132 handle(req, fd, path, pat);
135 static int checkaccess(char *path, char *name)
141 cfs = getconfigs(sprintf3("%s/", path));
142 for(i = 0; cfs[i] != NULL; i++) {
143 if(cfs[i]->dotallow != NULL) {
144 for(o = 0; cfs[i]->dotallow[o] != NULL; o++) {
145 if(!fnmatch(cfs[i]->dotallow[o], name, 0))
156 static char *findfile(char *path, char *name, struct stat *sb)
165 if((dir = opendir(path)) == NULL)
168 while((dent = readdir(dir)) != NULL) {
169 /* Ignore backup files.
170 * XXX: There is probably a better and more extensible way to
172 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
174 if((p = strchr(dent->d_name, '.')) == NULL)
176 if(p - dent->d_name != strlen(name))
178 if(strncmp(dent->d_name, name, strlen(name)))
180 fp = sprintf2("%s/%s", path, dent->d_name);
185 if(!S_ISREG(sb->st_mode)) {
189 if(!checkaccess(path, dent->d_name)) {
200 static void handledir(struct hthead *req, int fd, char *path)
205 char *inm, *ipath, *cpath;
208 cpath = sprintf2("%s/", path);
209 cfs = getconfigs(cpath);
210 for(i = 0; cfs[i] != NULL; i++) {
211 if(cfs[i]->index != NULL) {
212 for(o = 0; cfs[i]->index[o] != NULL; o++) {
213 inm = cfs[i]->index[o];
214 ipath = sprintf2("%s/%s", path, inm);
215 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
216 handlefile(req, fd, ipath);
222 if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
223 handlefile(req, fd, ipath);
231 if((pat = findmatch(cpath, 0, PT_DIR)) != NULL) {
232 handle(req, fd, cpath, pat);
235 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
241 static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final);
243 static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el, int final)
249 if(!stat(sprintf3("%s/%s", path, el), &sb)) {
250 if(!checkaccess(path, el))
252 if(S_ISDIR(sb.st_mode)) {
254 stdredir(req, fd, 301, sprintf3("%s/", el));
257 newpath = sprintf2("%s/%s", path, el);
258 rv = checkpath(req, fd, newpath, rest + 1, final);
261 } else if(S_ISREG(sb.st_mode)) {
262 newpath = sprintf2("%s/%s", path, el);
264 handlefile(req, fd, newpath);
268 handle404(req, fd, sprintf3("%s/", path));
271 if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
273 handlefile(req, fd, newpath);
280 static int checkdir(struct hthead *req, int fd, char *path, char *rest)
282 char *cpath, *newpath;
283 struct config *cf, *ccf;
288 cf = getconfig(path);
289 if((cf->capture != NULL) && (cf->caproot || !cf->path || strcmp(cf->path, "."))) {
290 cpath = sprintf2("%s/", path);
291 if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) {
293 flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture);
294 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture);
301 if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
305 if(cf->reparse != NULL) {
306 newpath = (cf->reparse[0] == '/')?sstrdup(cf->reparse):sprintf2("%s/%s", path, cf->reparse);
307 rv = stat(newpath, &sb);
308 if(!rv && S_ISDIR(sb.st_mode)) {
309 rv = checkpath(req, fd, newpath, rest, !cf->parsecomb);
310 } else if(!rv && S_ISREG(sb.st_mode)) {
312 handlefile(req, fd, newpath);
324 static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final)
332 if(!strncmp(path, "./", 2))
334 if(checkdir(req, fd, path, rest))
337 if((p = strchr(rest, '/')) == NULL) {
338 el = unquoteurl(rest);
341 char buf[p - rest + 1];
342 memcpy(buf, rest, p - rest);
344 el = unquoteurl(buf);
348 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
352 if(strchr(el, '/') || (!*el && *rest)) {
358 handledir(req, fd, path);
362 rv = checkentry(req, fd, path, rest, el, final);
366 handle404(req, fd, sprintf3("%s/", path));
374 static void serve(struct hthead *req, int fd)
377 checkpath(req, fd, ".", req->rest, 1);
380 static void chldhandler(int sig)
385 while((pid = waitpid(-1, &st, WNOHANG)) > 0) {
387 flog(LOG_WARNING, "child process %i dumped core", pid);
391 static void sighandler(int sig)
395 static void usage(FILE *out)
397 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
400 int main(int argc, char **argv)
404 char *gcf, *lcf, *clcf;
410 while((c = getopt(argc, argv, "hNc:")) >= 0) {
426 if(argc - optind < 1) {
431 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
432 gconfig = readconfig(gcf);
437 if(strchr(lcf, '/') == NULL) {
438 if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
439 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
442 if((lconfig = readconfig(clcf)) == NULL)
446 if((lconfig = readconfig(lcf)) == NULL)
450 if(chdir(argv[optind])) {
451 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
454 signal(SIGCHLD, chldhandler);
455 signal(SIGPIPE, sighandler);
457 if((fd = recvreq(0, &req)) < 0) {
459 flog(LOG_ERR, "recvreq: %s", strerror(errno));