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