dirplex: Merge children with identical names when reloading configuration.
[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>
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
42time_t now;
43
44static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
45{
46 struct child *ch;
47
48 headappheader(req, "X-Ash-File", path);
49 if(pat->fchild) {
6a7a868e 50 stdforkserve(pat->fchild, req, fd, NULL, NULL);
600a1ce7
FT
51 } else {
52 if((ch = findchild(path, pat->childnm)) == NULL) {
53 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
54 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
55 return;
56 }
6a7a868e 57 if(childhandle(ch, req, fd, NULL, NULL))
600a1ce7
FT
58 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
59 }
60}
61
62static void handlefile(struct hthead *req, int fd, char *path)
63{
64 struct pattern *pat;
65
66 if((pat = findmatch(path, 0, 0)) == NULL) {
67 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
68 return;
69 }
70 handle(req, fd, path, pat);
71}
72
73static char *findfile(char *path, char *name, struct stat *sb)
74{
75 DIR *dir;
76 struct stat sbuf;
77 struct dirent *dent;
78 char *p, *fp, *ret;
79
80 if(sb == NULL)
81 sb = &sbuf;
82 if((dir = opendir(path)) == NULL)
83 return(NULL);
84 ret = NULL;
85 while((dent = readdir(dir)) != NULL) {
86 /* Ignore backup files.
87 * XXX: There is probably a better and more extensible way to
88 * do this. */
89 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
90 continue;
91 if((p = strchr(dent->d_name, '.')) == NULL)
92 continue;
93 if(p - dent->d_name != strlen(name))
94 continue;
95 if(strncmp(dent->d_name, name, strlen(name)))
96 continue;
97 fp = sprintf3("%s/%s", path, dent->d_name);
98 if(stat(fp, sb))
99 continue;
100 if(!S_ISREG(sb->st_mode))
101 continue;
102 ret = sstrdup(fp);
103 break;
104 }
105 closedir(dir);
106 return(ret);
107}
108
109static void handledir(struct hthead *req, int fd, char *path)
110{
111 struct config **cfs;
112 int i, o;
113 struct stat sb;
114 char *inm, *ipath, *cpath;
115 struct pattern *pat;
116
117 cpath = sprintf2("%s/", path);
118 cfs = getconfigs(cpath);
119 for(i = 0; cfs[i] != NULL; i++) {
120 if(cfs[i]->index != NULL) {
121 for(o = 0; cfs[i]->index[o] != NULL; o++) {
122 inm = cfs[i]->index[o];
123 ipath = sprintf2("%s/%s", path, inm);
124 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
125 handlefile(req, fd, ipath);
126 free(ipath);
127 goto out;
128 }
129 free(ipath);
130
131 if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
132 handlefile(req, fd, ipath);
133 free(ipath);
134 goto out;
135 }
136 }
137 break;
138 }
139 }
140 if((pat = findmatch(cpath, 0, 1)) != NULL) {
141 handle(req, fd, cpath, pat);
142 goto out;
143 }
144 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
145
146out:
147 free(cpath);
148}
149
150static int checkpath(struct hthead *req, int fd, char *path, char *rest);
151
152static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
153{
154 struct stat sb;
155 char *newpath;
156 int rv;
157
158 if(!el == '.') {
159 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
160 return(1);
161 }
162 if(!stat(sprintf3("%s/%s", path, el), &sb)) {
163 if(S_ISDIR(sb.st_mode)) {
164 if(!*rest) {
165 stdredir(req, fd, 301, sprintf3("%s/", el));
166 return(1);
167 }
168 newpath = sprintf2("%s/%s", path, el);
169 rv = checkpath(req, fd, newpath, rest + 1);
170 free(newpath);
171 return(rv);
172 } else if(S_ISREG(sb.st_mode)) {
173 newpath = sprintf2("%s/%s", path, el);
174 replrest(req, rest);
175 handlefile(req, fd, newpath);
176 free(newpath);
177 return(1);
178 }
179 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
180 return(1);
181 }
182 if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
183 replrest(req, rest);
184 handlefile(req, fd, newpath);
185 free(newpath);
186 return(1);
187 }
188 return(0);
189}
190
191static int checkpath(struct hthead *req, int fd, char *path, char *rest)
192{
193 struct config *cf;
194 char *p, *el;
195 int rv;
196
197 el = NULL;
198 rv = 0;
199
200 if(!strncmp(path, "./", 2))
201 path += 2;
202 cf = getconfig(path);
203
204 if((p = strchr(rest, '/')) == NULL) {
205 el = unquoteurl(rest);
206 rest = "";
207 } else {
208 char buf[p - rest + 1];
209 memcpy(buf, rest, p - rest);
210 buf[p - rest] = 0;
211 el = unquoteurl(buf);
212 rest = p;
213 }
214 if(el == NULL) {
215 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
216 rv = 1;
217 goto out;
218 }
219 if(strchr(el, '/') || (!*el && *rest)) {
220 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
221 rv = 1;
222 goto out;
223 }
224 if(!*el) {
225 replrest(req, rest);
226 handledir(req, fd, path);
227 return(1);
228 }
229 rv = checkentry(req, fd, path, rest, el);
230
231out:
232 if(el != NULL)
233 free(el);
234 return(rv);
235}
236
237static void serve(struct hthead *req, int fd)
238{
239 now = time(NULL);
240 if(!checkpath(req, fd, ".", req->rest))
241 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
242}
243
244static void usage(FILE *out)
245{
246 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
247}
248
249int main(int argc, char **argv)
250{
251 int c;
252 int nodef;
253 char *gcf, *lcf, *clcf;
254 struct hthead *req;
255 int fd;
256
257 nodef = 0;
258 lcf = NULL;
259 while((c = getopt(argc, argv, "hNc:")) >= 0) {
260 switch(c) {
261 case 'h':
262 usage(stdout);
263 exit(0);
264 case 'N':
265 nodef = 1;
266 break;
267 case 'c':
268 lcf = optarg;
269 break;
270 default:
271 usage(stderr);
272 exit(1);
273 }
274 }
275 if(argc - optind < 1) {
276 usage(stderr);
277 exit(1);
278 }
279 if(!nodef) {
280 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
281 gconfig = readconfig(gcf);
282 free(gcf);
283 }
284 }
285 if(lcf != NULL) {
286 if(strchr(lcf, '/') == NULL) {
287 if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
288 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
289 exit(1);
290 }
291 if((lconfig = readconfig(clcf)) == NULL)
292 exit(1);
293 free(clcf);
294 } else {
295 if((lconfig = readconfig(lcf)) == NULL)
296 exit(1);
297 }
298 }
299 if(chdir(argv[optind])) {
300 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
301 exit(1);
302 }
303 signal(SIGCHLD, SIG_IGN);
304 while(1) {
305 if((fd = recvreq(0, &req)) < 0) {
306 if(errno != 0)
307 flog(LOG_ERR, "recvreq: %s", strerror(errno));
308 break;
309 }
310 serve(req, fd);
311 freehthead(req);
312 close(fd);
313 }
314 return(0);
315}