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