Commit | Line | Data |
---|---|---|
992ce9ef 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> | |
992ce9ef | 21 | #include <unistd.h> |
600a1ce7 | 22 | #include <string.h> |
992ce9ef | 23 | #include <errno.h> |
992ce9ef | 24 | #include <fnmatch.h> |
600a1ce7 | 25 | #include <sys/stat.h> |
992ce9ef FT |
26 | |
27 | #ifdef HAVE_CONFIG_H | |
28 | #include <config.h> | |
29 | #endif | |
30 | #include <utils.h> | |
992ce9ef | 31 | #include <log.h> |
06c1a718 | 32 | #include <cf.h> |
992ce9ef | 33 | |
600a1ce7 | 34 | #include "dirplex.h" |
992ce9ef | 35 | |
0fc6fd13 | 36 | static struct config *cflist; |
600a1ce7 | 37 | struct config *gconfig, *lconfig; |
992ce9ef | 38 | |
1eba4f97 FT |
39 | static void freerule(struct rule *rule) |
40 | { | |
41 | freeca(rule->patterns); | |
42 | free(rule); | |
43 | } | |
44 | ||
992ce9ef FT |
45 | static void freepattern(struct pattern *pat) |
46 | { | |
47 | struct rule **rule; | |
48 | ||
1eba4f97 FT |
49 | for(rule = pat->rules; *rule; rule++) |
50 | freerule(*rule); | |
992ce9ef FT |
51 | if(pat->childnm != NULL) |
52 | free(pat->childnm); | |
624f637d | 53 | freeca(pat->fchild); |
992ce9ef FT |
54 | free(pat); |
55 | } | |
56 | ||
57 | static void freeconfig(struct config *cf) | |
58 | { | |
59 | struct child *ch, *nch; | |
60 | struct pattern *pat, *npat; | |
61 | ||
62 | if(cf->prev != NULL) | |
63 | cf->prev->next = cf->next; | |
64 | if(cf->next != NULL) | |
65 | cf->next->prev = cf->prev; | |
66 | if(cf == cflist) | |
67 | cflist = cf->next; | |
0fc6fd13 FT |
68 | if(cf->path != NULL) |
69 | free(cf->path); | |
992ce9ef FT |
70 | for(ch = cf->children; ch != NULL; ch = nch) { |
71 | nch = ch->next; | |
72 | freechild(ch); | |
73 | } | |
74 | for(pat = cf->patterns; pat != NULL; pat = npat) { | |
75 | npat = pat->next; | |
76 | freepattern(pat); | |
77 | } | |
6373daf9 | 78 | freeca(cf->index); |
992ce9ef FT |
79 | free(cf); |
80 | } | |
81 | ||
600a1ce7 | 82 | struct child *getchild(struct config *cf, char *name) |
992ce9ef FT |
83 | { |
84 | struct child *ch; | |
85 | ||
86 | for(ch = cf->children; ch; ch = ch->next) { | |
87 | if(!strcmp(ch->name, name)) | |
88 | break; | |
89 | } | |
90 | return(ch); | |
91 | } | |
92 | ||
93 | static struct rule *newrule(struct pattern *pat) | |
94 | { | |
95 | int i; | |
96 | struct rule *rule; | |
97 | ||
98 | for(i = 0; pat->rules[i]; i++); | |
99 | pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2)); | |
cceb3611 | 100 | rule = pat->rules[i] = szmalloc(sizeof(*rule)); |
992ce9ef FT |
101 | pat->rules[i + 1] = NULL; |
102 | return(rule); | |
103 | } | |
104 | ||
105 | static struct pattern *newpattern(void) | |
106 | { | |
107 | struct pattern *pat; | |
108 | ||
109 | omalloc(pat); | |
110 | pat->rules = szmalloc(sizeof(*pat->rules)); | |
111 | return(pat); | |
112 | } | |
113 | ||
1eba4f97 FT |
114 | static char **cadup(char **w) |
115 | { | |
116 | char **ret; | |
117 | int i, l; | |
118 | ||
119 | l = calen(w); | |
120 | ret = smalloc(sizeof(*ret) * (l + 1)); | |
121 | for(i = 0; i < l; i++) | |
122 | ret[i] = sstrdup(w[i]); | |
123 | ret[i] = NULL; | |
124 | return(ret); | |
125 | } | |
126 | ||
06c1a718 FT |
127 | static struct pattern *parsepattern(struct cfstate *s) |
128 | { | |
129 | struct pattern *pat; | |
130 | struct rule *rule; | |
1eba4f97 | 131 | int sl; |
06c1a718 FT |
132 | |
133 | if(!strcmp(s->argv[0], "match")) { | |
134 | s->expstart = 1; | |
135 | pat = newpattern(); | |
136 | } else { | |
137 | return(NULL); | |
138 | } | |
139 | ||
8232cebe FT |
140 | if((s->argc > 1) && !strcmp(s->argv[1], "directory")) |
141 | pat->type = PT_DIR; | |
06c1a718 FT |
142 | sl = s->lno; |
143 | while(1) { | |
144 | getcfline(s); | |
145 | if(!strcmp(s->argv[0], "filename")) { | |
146 | if(s->argc < 2) { | |
147 | flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno); | |
148 | continue; | |
149 | } | |
150 | rule = newrule(pat); | |
151 | rule->type = PAT_BASENAME; | |
1eba4f97 | 152 | rule->patterns = cadup(s->argv + 1); |
06c1a718 FT |
153 | } else if(!strcmp(s->argv[0], "pathname")) { |
154 | if(s->argc < 2) { | |
155 | flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno); | |
156 | continue; | |
157 | } | |
158 | rule = newrule(pat); | |
159 | rule->type = PAT_PATHNAME; | |
1eba4f97 | 160 | rule->patterns = cadup(s->argv + 1); |
06c1a718 FT |
161 | } else if(!strcmp(s->argv[0], "all")) { |
162 | newrule(pat)->type = PAT_ALL; | |
163 | } else if(!strcmp(s->argv[0], "default")) { | |
164 | newrule(pat)->type = PAT_DEFAULT; | |
165 | } else if(!strcmp(s->argv[0], "handler")) { | |
166 | if(s->argc < 2) { | |
167 | flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno); | |
168 | continue; | |
169 | } | |
170 | if(pat->childnm != NULL) | |
171 | free(pat->childnm); | |
172 | pat->childnm = sstrdup(s->argv[1]); | |
624f637d | 173 | } else if(!strcmp(s->argv[0], "fork")) { |
1eba4f97 | 174 | pat->fchild = cadup(s->argv + 1); |
06c1a718 FT |
175 | } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) { |
176 | break; | |
177 | } else { | |
178 | flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]); | |
179 | } | |
180 | } | |
181 | ||
182 | if(pat->rules[0] == NULL) { | |
183 | flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl); | |
184 | freepattern(pat); | |
185 | return(NULL); | |
186 | } | |
624f637d | 187 | if((pat->childnm == NULL) && (pat->fchild == NULL)) { |
06c1a718 FT |
188 | flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl); |
189 | freepattern(pat); | |
190 | return(NULL); | |
191 | } | |
192 | return(pat); | |
193 | } | |
194 | ||
17e55136 FT |
195 | static struct config *emptyconfig(void) |
196 | { | |
197 | struct config *cf; | |
198 | ||
199 | omalloc(cf); | |
200 | return(cf); | |
201 | } | |
202 | ||
600a1ce7 | 203 | struct config *readconfig(char *file) |
992ce9ef | 204 | { |
06c1a718 FT |
205 | struct cfstate *s; |
206 | FILE *in; | |
992ce9ef | 207 | struct config *cf; |
992ce9ef FT |
208 | struct child *child; |
209 | struct pattern *pat; | |
992ce9ef | 210 | |
17e55136 FT |
211 | if((in = fopen(file, "r")) == NULL) { |
212 | flog(LOG_WARNING, "%s: %s", file, strerror(errno)); | |
992ce9ef | 213 | return(NULL); |
06c1a718 | 214 | } |
17e55136 FT |
215 | s = mkcfparser(in, file); |
216 | cf = emptyconfig(); | |
06c1a718 FT |
217 | |
218 | while(1) { | |
219 | getcfline(s); | |
220 | if((child = parsechild(s)) != NULL) { | |
221 | child->next = cf->children; | |
222 | cf->children = child; | |
223 | } else if((pat = parsepattern(s)) != NULL) { | |
224 | pat->next = cf->patterns; | |
225 | cf->patterns = pat; | |
6373daf9 FT |
226 | } else if(!strcmp(s->argv[0], "index-file")) { |
227 | freeca(cf->index); | |
228 | cf->index = NULL; | |
229 | if(s->argc > 1) | |
230 | cf->index = cadup(s->argv + 1); | |
06c1a718 FT |
231 | } else if(!strcmp(s->argv[0], "eof")) { |
232 | break; | |
233 | } else { | |
234 | flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]); | |
992ce9ef | 235 | } |
06c1a718 | 236 | } |
992ce9ef | 237 | |
06c1a718 FT |
238 | freecfparser(s); |
239 | fclose(in); | |
992ce9ef FT |
240 | return(cf); |
241 | } | |
242 | ||
a756e349 FT |
243 | static void mergechildren(struct config *dst, struct config *src) |
244 | { | |
245 | struct child *ch1, *ch2; | |
246 | ||
247 | for(ch1 = dst->children; ch1 != NULL; ch1 = ch1->next) { | |
248 | for(ch2 = src->children; ch2 != NULL; ch2 = ch2->next) { | |
249 | if(!strcmp(ch1->name, ch2->name)) { | |
250 | ch1->fd = ch2->fd; | |
251 | ch2->fd = -1; | |
252 | break; | |
253 | } | |
254 | } | |
255 | } | |
256 | } | |
257 | ||
600a1ce7 | 258 | struct config *getconfig(char *path) |
992ce9ef | 259 | { |
a756e349 | 260 | struct config *cf, *ocf; |
992ce9ef | 261 | struct stat sb; |
17e55136 FT |
262 | char *fn; |
263 | time_t mtime; | |
992ce9ef | 264 | |
17e55136 | 265 | fn = sprintf3("%s/.htrc", path); |
992ce9ef FT |
266 | for(cf = cflist; cf != NULL; cf = cf->next) { |
267 | if(!strcmp(cf->path, path)) { | |
17e55136 | 268 | if(now - cf->lastck > 5) { |
a756e349 | 269 | if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) |
17e55136 | 270 | break; |
992ce9ef | 271 | } |
17e55136 | 272 | cf->lastck = now; |
992ce9ef FT |
273 | return(cf); |
274 | } | |
275 | } | |
a756e349 | 276 | ocf = cf; |
17e55136 FT |
277 | if(access(fn, R_OK) || stat(fn, &sb)) { |
278 | cf = emptyconfig(); | |
279 | mtime = 0; | |
280 | } else { | |
281 | if((cf = readconfig(fn)) == NULL) | |
282 | return(NULL); | |
283 | mtime = sb.st_mtime; | |
992ce9ef | 284 | } |
a756e349 FT |
285 | if(ocf != NULL) { |
286 | mergechildren(cf, ocf); | |
287 | freeconfig(ocf); | |
288 | } | |
17e55136 FT |
289 | cf->path = sstrdup(path); |
290 | cf->mtime = mtime; | |
291 | cf->lastck = now; | |
292 | cf->next = cflist; | |
d245c327 FT |
293 | cf->prev = NULL; |
294 | if(cflist != NULL) | |
295 | cflist->prev = cf; | |
17e55136 | 296 | cflist = cf; |
992ce9ef FT |
297 | return(cf); |
298 | } | |
299 | ||
600a1ce7 | 300 | struct config **getconfigs(char *file) |
992ce9ef | 301 | { |
8dce6d19 FT |
302 | static struct config **ret = NULL; |
303 | struct { | |
304 | struct config **b; | |
305 | size_t s, d; | |
306 | } buf; | |
992ce9ef | 307 | struct config *cf; |
8dce6d19 FT |
308 | char *tmp, *p; |
309 | ||
310 | if(ret != NULL) | |
311 | free(ret); | |
312 | bufinit(buf); | |
313 | tmp = sstrdup(file); | |
992ce9ef | 314 | while(1) { |
8dce6d19 | 315 | if((p = strrchr(tmp, '/')) == NULL) |
992ce9ef | 316 | break; |
8dce6d19 FT |
317 | *p = 0; |
318 | if((cf = getconfig(tmp)) != NULL) | |
319 | bufadd(buf, cf); | |
320 | } | |
321 | free(tmp); | |
322 | if((cf = getconfig(".")) != NULL) | |
323 | bufadd(buf, cf); | |
0fc6fd13 FT |
324 | if(lconfig != NULL) |
325 | bufadd(buf, lconfig); | |
326 | if(gconfig != NULL) | |
327 | bufadd(buf, gconfig); | |
8dce6d19 FT |
328 | bufadd(buf, NULL); |
329 | return(ret = buf.b); | |
330 | } | |
331 | ||
da75c835 | 332 | struct child *findchild(char *file, char *name, struct config **cf) |
8dce6d19 FT |
333 | { |
334 | int i; | |
335 | struct config **cfs; | |
336 | struct child *ch; | |
337 | ||
338 | cfs = getconfigs(file); | |
339 | for(i = 0; cfs[i] != NULL; i++) { | |
da75c835 FT |
340 | if((ch = getchild(cfs[i], name)) != NULL) { |
341 | if(cf != NULL) | |
342 | *cf = cfs[i]; | |
343 | return(ch); | |
344 | } | |
992ce9ef | 345 | } |
da75c835 | 346 | return(NULL); |
992ce9ef FT |
347 | } |
348 | ||
600a1ce7 | 349 | struct pattern *findmatch(char *file, int trydefault, int dir) |
992ce9ef | 350 | { |
1eba4f97 | 351 | int i, o, c; |
8dce6d19 FT |
352 | char *bn; |
353 | struct config **cfs; | |
992ce9ef FT |
354 | struct pattern *pat; |
355 | struct rule *rule; | |
356 | ||
357 | if((bn = strrchr(file, '/')) != NULL) | |
358 | bn++; | |
359 | else | |
360 | bn = file; | |
8dce6d19 FT |
361 | cfs = getconfigs(file); |
362 | for(c = 0; cfs[c] != NULL; c++) { | |
363 | for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) { | |
8232cebe FT |
364 | if(!dir && (pat->type == PT_DIR)) |
365 | continue; | |
366 | if(dir && (pat->type != PT_DIR)) | |
367 | continue; | |
992ce9ef FT |
368 | for(i = 0; (rule = pat->rules[i]) != NULL; i++) { |
369 | if(rule->type == PAT_BASENAME) { | |
1eba4f97 FT |
370 | for(o = 0; rule->patterns[o] != NULL; o++) { |
371 | if(!fnmatch(rule->patterns[o], bn, 0)) | |
372 | break; | |
373 | } | |
374 | if(rule->patterns[o] == NULL) | |
992ce9ef FT |
375 | break; |
376 | } else if(rule->type == PAT_PATHNAME) { | |
1eba4f97 FT |
377 | for(o = 0; rule->patterns[o] != NULL; o++) { |
378 | if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME)) | |
379 | break; | |
380 | } | |
381 | if(rule->patterns[o] == NULL) | |
992ce9ef FT |
382 | break; |
383 | } else if(rule->type == PAT_ALL) { | |
384 | } else if(rule->type == PAT_DEFAULT) { | |
385 | if(!trydefault) | |
386 | break; | |
387 | } | |
388 | } | |
389 | if(!rule) | |
8dce6d19 | 390 | return(pat); |
992ce9ef FT |
391 | } |
392 | } | |
8232cebe FT |
393 | if(!trydefault) |
394 | return(findmatch(file, 1, dir)); | |
8dce6d19 | 395 | return(NULL); |
992ce9ef | 396 | } |