f6002f3361d752173b06f41e75ce9a3e75840fa5
[ashd.git] / src / dirplex / dirplex.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 <errno.h>
24 #include <sys/stat.h>
25 #include <ctype.h>
26 #include <dirent.h>
27 #include <time.h>
28 #include <sys/wait.h>
29 #include <sys/signal.h>
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 #include <utils.h>
35 #include <log.h>
36 #include <req.h>
37 #include <proc.h>
38 #include <resp.h>
39 #include <cf.h>
40
41 #include "dirplex.h"
42
43 time_t now;
44
45 static void chinit(void *idata)
46 {
47     char *twd = idata;
48     
49     if(twd != NULL) {
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
52          * traversed. */
53         if(chdir(twd))
54             exit(127);
55     }
56 }
57
58 static void childerror(struct hthead *req, int fd)
59 {
60     if(errno == EAGAIN)
61         simpleerror(fd, 500, "Server Error", "The request handler is overloaded.");
62     else
63         simpleerror(fd, 500, "Server Error", "The request handler crashed.");
64 }
65
66 static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
67 {
68     struct child *ch;
69     struct config *ccf;
70     struct headmod *head;
71     char *twd;
72
73     for(head = pat->headers; head != NULL; head = head->next) {
74         headrmheader(req, head->name);
75         headappheader(req, head->name, head->value);
76     }
77     if(!strncmp(path, "./", 2) && path[2])
78         path += 2;
79     if(pat->fchild) {
80         headappheader(req, "X-Ash-File", path);
81         stdforkserve(pat->fchild, req, fd, NULL, NULL);
82     } else {
83         if((ch = findchild(path, pat->childnm, &ccf)) == NULL) {
84             flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
85             simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
86             return;
87         }
88         if((twd = ccf?ccf->path:NULL) != NULL) {
89             if(!strcmp(twd, ".")) {
90                 twd = NULL;
91             } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) {
92                 /* Should be an impossible case under the current (and
93                  * foreseeable) scheme. */
94                 simpleerror(fd, 500, "Server Error", "An internal server error occurred.");
95                 return;
96             } else {
97                 path = path + strlen(twd) + 1;
98             }
99         }
100         headappheader(req, "X-Ash-File", path);
101         if(childhandle(ch, req, fd, chinit, twd))
102             childerror(req, fd);
103     }
104 }
105
106 static void handle404(struct hthead *req, int fd, char *path)
107 {
108     struct child *ch;
109     struct config *ccf;
110     char *tmp;
111     
112     tmp = sstrdup(path);
113     ch = findchild(tmp, ".notfound", &ccf);
114     if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
115         childerror(req, fd);
116     free(tmp);
117 }
118
119 static void handlefile(struct hthead *req, int fd, char *path)
120 {
121     struct pattern *pat;
122
123     if((pat = findmatch(path, 0, 0)) == NULL) {
124         handle404(req, fd, path);
125         return;
126     }
127     handle(req, fd, path, pat);
128 }
129
130 static char *findfile(char *path, char *name, struct stat *sb)
131 {
132     DIR *dir;
133     struct stat sbuf;
134     struct dirent *dent;
135     char *p, *fp, *ret;
136     
137     if(sb == NULL)
138         sb = &sbuf;
139     if((dir = opendir(path)) == NULL)
140         return(NULL);
141     ret = NULL;
142     while((dent = readdir(dir)) != NULL) {
143         /* Ignore backup files.
144          * XXX: There is probably a better and more extensible way to
145          * do this. */
146         if(dent->d_name[strlen(dent->d_name) - 1] == '~')
147             continue;
148         if((p = strchr(dent->d_name, '.')) == NULL)
149             continue;
150         if(p - dent->d_name != strlen(name))
151             continue;
152         if(strncmp(dent->d_name, name, strlen(name)))
153             continue;
154         fp = sprintf3("%s/%s", path, dent->d_name);
155         if(stat(fp, sb))
156             continue;
157         if(!S_ISREG(sb->st_mode))
158             continue;
159         ret = sstrdup(fp);
160         break;
161     }
162     closedir(dir);
163     return(ret);
164 }
165
166 static void handledir(struct hthead *req, int fd, char *path)
167 {
168     struct config **cfs;
169     int i, o;
170     struct stat sb;
171     char *inm, *ipath, *cpath;
172     struct pattern *pat;
173     
174     cpath = sprintf2("%s/", path);
175     cfs = getconfigs(cpath);
176     for(i = 0; cfs[i] != NULL; i++) {
177         if(cfs[i]->index != NULL) {
178             for(o = 0; cfs[i]->index[o] != NULL; o++) {
179                 inm = cfs[i]->index[o];
180                 ipath = sprintf2("%s/%s", path, inm);
181                 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
182                     handlefile(req, fd, ipath);
183                     free(ipath);
184                     goto out;
185                 }
186                 free(ipath);
187                 
188                 if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
189                     handlefile(req, fd, ipath);
190                     free(ipath);
191                     goto out;
192                 }
193             }
194             break;
195         }
196     }
197     if((pat = findmatch(cpath, 0, 1)) != NULL) {
198         handle(req, fd, cpath, pat);
199         goto out;
200     }
201     simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
202     
203 out:
204     free(cpath);
205 }
206
207 static int checkpath(struct hthead *req, int fd, char *path, char *rest);
208
209 static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
210 {
211     struct stat sb;
212     char *newpath;
213     int rv;
214     
215     if(*el == '.') {
216         handle404(req, fd, sprintf3("%s/", path));
217         return(1);
218     }
219     if(!stat(sprintf3("%s/%s", path, el), &sb)) {
220         if(S_ISDIR(sb.st_mode)) {
221             if(!*rest) {
222                 stdredir(req, fd, 301, sprintf3("%s/", el));
223                 return(1);
224             }
225             newpath = sprintf2("%s/%s", path, el);
226             rv = checkpath(req, fd, newpath, rest + 1);
227             free(newpath);
228             return(rv);
229         } else if(S_ISREG(sb.st_mode)) {
230             newpath = sprintf2("%s/%s", path, el);
231             replrest(req, rest);
232             handlefile(req, fd, newpath);
233             free(newpath);
234             return(1);
235         }
236         handle404(req, fd, sprintf3("%s/", path));
237         return(1);
238     }
239     if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
240         replrest(req, rest);
241         handlefile(req, fd, newpath);
242         free(newpath);
243         return(1);
244     }
245     return(0);
246 }
247
248 static int checkdir(struct hthead *req, int fd, char *path, char *rest)
249 {
250     char *cpath;
251     struct config *cf, *ccf;
252     struct child *ch;
253     
254     cf = getconfig(path);
255     if((cf->capture != NULL) && (cf->caproot || !cf->path || strcmp(cf->path, "."))) {
256         cpath = sprintf2("%s/", path);
257         if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) {
258             free(cpath);
259             flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture);
260             simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture);
261             return(1);
262         }
263         free(cpath);
264         if(*rest == '/')
265             rest++;
266         replrest(req, rest);
267         if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
268             childerror(req, fd);
269         return(1);
270     }
271     return(0);
272 }
273
274 static int checkpath(struct hthead *req, int fd, char *path, char *rest)
275 {
276     char *p, *el;
277     int rv;
278     
279     el = NULL;
280     rv = 0;
281     
282     if(!strncmp(path, "./", 2))
283         path += 2;
284     if(checkdir(req, fd, path, rest))
285         return(1);
286     
287     if((p = strchr(rest, '/')) == NULL) {
288         el = unquoteurl(rest);
289         rest = "";
290     } else {
291         char buf[p - rest + 1];
292         memcpy(buf, rest, p - rest);
293         buf[p - rest] = 0;
294         el = unquoteurl(buf);
295         rest = p;
296     }
297     if(el == NULL) {
298         simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
299         rv = 1;
300         goto out;
301     }
302     if(strchr(el, '/') || (!*el && *rest)) {
303         handle404(req, fd, sprintf3("%s/", path));
304         rv = 1;
305         goto out;
306     }
307     if(!*el) {
308         replrest(req, rest);
309         handledir(req, fd, path);
310         rv = 1;
311         goto out;
312     }
313     rv = checkentry(req, fd, path, rest, el);
314     
315 out:
316     if(el != NULL)
317         free(el);
318     return(rv);
319 }
320
321 static void serve(struct hthead *req, int fd)
322 {
323     now = time(NULL);
324     if(!checkpath(req, fd, ".", req->rest))
325         handle404(req, fd, ".");
326 }
327
328 static void chldhandler(int sig)
329 {
330     pid_t pid;
331     int st;
332     
333     while((pid = waitpid(-1, &st, WNOHANG)) > 0) {
334         if(WCOREDUMP(st))
335             flog(LOG_WARNING, "child process %i dumped core", pid);
336     }
337 }
338
339 static void sighandler(int sig)
340 {
341 }
342
343 static void usage(FILE *out)
344 {
345     fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
346 }
347
348 int main(int argc, char **argv)
349 {
350     int c;
351     int nodef;
352     char *gcf, *lcf, *clcf;
353     struct hthead *req;
354     int fd;
355     
356     nodef = 0;
357     lcf = NULL;
358     while((c = getopt(argc, argv, "hNc:")) >= 0) {
359         switch(c) {
360         case 'h':
361             usage(stdout);
362             exit(0);
363         case 'N':
364             nodef = 1;
365             break;
366         case 'c':
367             lcf = optarg;
368             break;
369         default:
370             usage(stderr);
371             exit(1);
372         }
373     }
374     if(argc - optind < 1) {
375         usage(stderr);
376         exit(1);
377     }
378     if(!nodef) {
379         if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
380             gconfig = readconfig(gcf);
381             free(gcf);
382         }
383     }
384     if(lcf != NULL) {
385         if(strchr(lcf, '/') == NULL) {
386             if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
387                 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
388                 exit(1);
389             }
390             if((lconfig = readconfig(clcf)) == NULL)
391                 exit(1);
392             free(clcf);
393         } else {
394             if((lconfig = readconfig(lcf)) == NULL)
395                 exit(1);
396         }
397     }
398     if(chdir(argv[optind])) {
399         flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
400         exit(1);
401     }
402     signal(SIGCHLD, chldhandler);
403     signal(SIGPIPE, sighandler);
404     while(1) {
405         if((fd = recvreq(0, &req)) < 0) {
406             if(errno != 0)
407                 flog(LOG_ERR, "recvreq: %s", strerror(errno));
408             break;
409         }
410         serve(req, fd);
411         freehthead(req);
412         close(fd);
413     }
414     return(0);
415 }