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