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