| 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 <fnmatch.h> |
| 28 | #include <time.h> |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include <config.h> |
| 32 | #endif |
| 33 | #include <utils.h> |
| 34 | #include <mt.h> |
| 35 | #include <log.h> |
| 36 | #include <req.h> |
| 37 | #include <proc.h> |
| 38 | #include <resp.h> |
| 39 | #include <cf.h> |
| 40 | |
| 41 | #define PAT_BASENAME 0 |
| 42 | #define PAT_PATHNAME 1 |
| 43 | #define PAT_ALL 2 |
| 44 | #define PAT_DEFAULT 3 |
| 45 | |
| 46 | struct config { |
| 47 | struct config *next, *prev; |
| 48 | char *path; |
| 49 | time_t mtime, lastck; |
| 50 | struct child *children; |
| 51 | struct pattern *patterns; |
| 52 | }; |
| 53 | |
| 54 | struct rule { |
| 55 | int type; |
| 56 | char *pattern; |
| 57 | }; |
| 58 | |
| 59 | struct pattern { |
| 60 | struct pattern *next; |
| 61 | char *childnm; |
| 62 | struct rule **rules; |
| 63 | }; |
| 64 | |
| 65 | struct config *cflist; |
| 66 | static time_t now; |
| 67 | |
| 68 | static void freepattern(struct pattern *pat) |
| 69 | { |
| 70 | struct rule **rule; |
| 71 | |
| 72 | for(rule = pat->rules; *rule; rule++) { |
| 73 | if((*rule)->pattern != NULL) |
| 74 | free((*rule)->pattern); |
| 75 | free(*rule); |
| 76 | } |
| 77 | if(pat->childnm != NULL) |
| 78 | free(pat->childnm); |
| 79 | free(pat); |
| 80 | } |
| 81 | |
| 82 | static void freeconfig(struct config *cf) |
| 83 | { |
| 84 | struct child *ch, *nch; |
| 85 | struct pattern *pat, *npat; |
| 86 | |
| 87 | if(cf->prev != NULL) |
| 88 | cf->prev->next = cf->next; |
| 89 | if(cf->next != NULL) |
| 90 | cf->next->prev = cf->prev; |
| 91 | if(cf == cflist) |
| 92 | cflist = cf->next; |
| 93 | free(cf->path); |
| 94 | for(ch = cf->children; ch != NULL; ch = nch) { |
| 95 | nch = ch->next; |
| 96 | freechild(ch); |
| 97 | } |
| 98 | for(pat = cf->patterns; pat != NULL; pat = npat) { |
| 99 | npat = pat->next; |
| 100 | freepattern(pat); |
| 101 | } |
| 102 | free(cf); |
| 103 | } |
| 104 | |
| 105 | static struct child *getchild(struct config *cf, char *name) |
| 106 | { |
| 107 | struct child *ch; |
| 108 | |
| 109 | for(ch = cf->children; ch; ch = ch->next) { |
| 110 | if(!strcmp(ch->name, name)) |
| 111 | break; |
| 112 | } |
| 113 | return(ch); |
| 114 | } |
| 115 | |
| 116 | static struct rule *newrule(struct pattern *pat) |
| 117 | { |
| 118 | int i; |
| 119 | struct rule *rule; |
| 120 | |
| 121 | for(i = 0; pat->rules[i]; i++); |
| 122 | pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2)); |
| 123 | rule = pat->rules[i] = szmalloc(sizeof(*rule)); |
| 124 | pat->rules[i + 1] = NULL; |
| 125 | return(rule); |
| 126 | } |
| 127 | |
| 128 | static struct pattern *newpattern(void) |
| 129 | { |
| 130 | struct pattern *pat; |
| 131 | |
| 132 | omalloc(pat); |
| 133 | pat->rules = szmalloc(sizeof(*pat->rules)); |
| 134 | return(pat); |
| 135 | } |
| 136 | |
| 137 | static struct pattern *parsepattern(struct cfstate *s) |
| 138 | { |
| 139 | struct pattern *pat; |
| 140 | struct rule *rule; |
| 141 | int sl; |
| 142 | |
| 143 | if(!strcmp(s->argv[0], "match")) { |
| 144 | s->expstart = 1; |
| 145 | pat = newpattern(); |
| 146 | } else { |
| 147 | return(NULL); |
| 148 | } |
| 149 | |
| 150 | sl = s->lno; |
| 151 | while(1) { |
| 152 | getcfline(s); |
| 153 | if(!strcmp(s->argv[0], "filename")) { |
| 154 | if(s->argc < 2) { |
| 155 | flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno); |
| 156 | continue; |
| 157 | } |
| 158 | rule = newrule(pat); |
| 159 | rule->type = PAT_BASENAME; |
| 160 | rule->pattern = sstrdup(s->argv[1]); |
| 161 | } else if(!strcmp(s->argv[0], "pathname")) { |
| 162 | if(s->argc < 2) { |
| 163 | flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno); |
| 164 | continue; |
| 165 | } |
| 166 | rule = newrule(pat); |
| 167 | rule->type = PAT_PATHNAME; |
| 168 | rule->pattern = sstrdup(s->argv[1]); |
| 169 | } else if(!strcmp(s->argv[0], "all")) { |
| 170 | newrule(pat)->type = PAT_ALL; |
| 171 | } else if(!strcmp(s->argv[0], "default")) { |
| 172 | newrule(pat)->type = PAT_DEFAULT; |
| 173 | } else if(!strcmp(s->argv[0], "handler")) { |
| 174 | if(s->argc < 2) { |
| 175 | flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno); |
| 176 | continue; |
| 177 | } |
| 178 | if(pat->childnm != NULL) |
| 179 | free(pat->childnm); |
| 180 | pat->childnm = sstrdup(s->argv[1]); |
| 181 | } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) { |
| 182 | break; |
| 183 | } else { |
| 184 | flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if(pat->rules[0] == NULL) { |
| 189 | flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl); |
| 190 | freepattern(pat); |
| 191 | return(NULL); |
| 192 | } |
| 193 | if(pat->childnm == NULL) { |
| 194 | flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl); |
| 195 | freepattern(pat); |
| 196 | return(NULL); |
| 197 | } |
| 198 | return(pat); |
| 199 | } |
| 200 | |
| 201 | static struct config *emptyconfig(void) |
| 202 | { |
| 203 | struct config *cf; |
| 204 | |
| 205 | omalloc(cf); |
| 206 | return(cf); |
| 207 | } |
| 208 | |
| 209 | static struct config *readconfig(char *file) |
| 210 | { |
| 211 | struct cfstate *s; |
| 212 | FILE *in; |
| 213 | struct config *cf; |
| 214 | struct child *child; |
| 215 | struct pattern *pat; |
| 216 | |
| 217 | if((in = fopen(file, "r")) == NULL) { |
| 218 | flog(LOG_WARNING, "%s: %s", file, strerror(errno)); |
| 219 | return(NULL); |
| 220 | } |
| 221 | s = mkcfparser(in, file); |
| 222 | cf = emptyconfig(); |
| 223 | |
| 224 | while(1) { |
| 225 | getcfline(s); |
| 226 | if((child = parsechild(s)) != NULL) { |
| 227 | child->next = cf->children; |
| 228 | cf->children = child; |
| 229 | } else if((pat = parsepattern(s)) != NULL) { |
| 230 | pat->next = cf->patterns; |
| 231 | cf->patterns = pat; |
| 232 | } else if(!strcmp(s->argv[0], "eof")) { |
| 233 | break; |
| 234 | } else { |
| 235 | flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | freecfparser(s); |
| 240 | fclose(in); |
| 241 | return(cf); |
| 242 | } |
| 243 | |
| 244 | static struct config *getconfig(char *path) |
| 245 | { |
| 246 | struct config *cf; |
| 247 | struct stat sb; |
| 248 | char *fn; |
| 249 | time_t mtime; |
| 250 | |
| 251 | fn = sprintf3("%s/.htrc", path); |
| 252 | for(cf = cflist; cf != NULL; cf = cf->next) { |
| 253 | if(!strcmp(cf->path, path)) { |
| 254 | if(now - cf->lastck > 5) { |
| 255 | if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) { |
| 256 | freeconfig(cf); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | cf->lastck = now; |
| 261 | return(cf); |
| 262 | } |
| 263 | } |
| 264 | if(access(fn, R_OK) || stat(fn, &sb)) { |
| 265 | cf = emptyconfig(); |
| 266 | mtime = 0; |
| 267 | } else { |
| 268 | if((cf = readconfig(fn)) == NULL) |
| 269 | return(NULL); |
| 270 | mtime = sb.st_mtime; |
| 271 | } |
| 272 | cf->path = sstrdup(path); |
| 273 | cf->mtime = mtime; |
| 274 | cf->lastck = now; |
| 275 | cf->next = cflist; |
| 276 | cflist = cf; |
| 277 | return(cf); |
| 278 | } |
| 279 | |
| 280 | static struct config **getconfigs(char *file) |
| 281 | { |
| 282 | static struct config **ret = NULL; |
| 283 | struct { |
| 284 | struct config **b; |
| 285 | size_t s, d; |
| 286 | } buf; |
| 287 | struct config *cf; |
| 288 | char *tmp, *p; |
| 289 | |
| 290 | if(ret != NULL) |
| 291 | free(ret); |
| 292 | bufinit(buf); |
| 293 | tmp = sstrdup(file); |
| 294 | while(1) { |
| 295 | if((p = strrchr(tmp, '/')) == NULL) |
| 296 | break; |
| 297 | *p = 0; |
| 298 | if((cf = getconfig(tmp)) != NULL) |
| 299 | bufadd(buf, cf); |
| 300 | } |
| 301 | free(tmp); |
| 302 | if((cf = getconfig(".")) != NULL) |
| 303 | bufadd(buf, cf); |
| 304 | bufadd(buf, NULL); |
| 305 | return(ret = buf.b); |
| 306 | } |
| 307 | |
| 308 | static struct child *findchild(char *file, char *name) |
| 309 | { |
| 310 | int i; |
| 311 | struct config **cfs; |
| 312 | struct child *ch; |
| 313 | |
| 314 | cfs = getconfigs(file); |
| 315 | for(i = 0; cfs[i] != NULL; i++) { |
| 316 | if((ch = getchild(cfs[i], name)) != NULL) |
| 317 | break; |
| 318 | } |
| 319 | return(ch); |
| 320 | } |
| 321 | |
| 322 | static struct pattern *findmatch(char *file, int trydefault) |
| 323 | { |
| 324 | int i, c; |
| 325 | char *bn; |
| 326 | struct config **cfs; |
| 327 | struct pattern *pat; |
| 328 | struct rule *rule; |
| 329 | |
| 330 | if((bn = strrchr(file, '/')) != NULL) |
| 331 | bn++; |
| 332 | else |
| 333 | bn = file; |
| 334 | cfs = getconfigs(file); |
| 335 | for(c = 0; cfs[c] != NULL; c++) { |
| 336 | for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) { |
| 337 | for(i = 0; (rule = pat->rules[i]) != NULL; i++) { |
| 338 | if(rule->type == PAT_BASENAME) { |
| 339 | if(fnmatch(rule->pattern, bn, 0)) |
| 340 | break; |
| 341 | } else if(rule->type == PAT_PATHNAME) { |
| 342 | if(fnmatch(rule->pattern, file, FNM_PATHNAME)) |
| 343 | break; |
| 344 | } else if(rule->type == PAT_ALL) { |
| 345 | } else if(rule->type == PAT_DEFAULT) { |
| 346 | if(!trydefault) |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | if(!rule) |
| 351 | return(pat); |
| 352 | } |
| 353 | } |
| 354 | return(NULL); |
| 355 | } |
| 356 | |
| 357 | static void handlefile(struct hthead *req, int fd, char *path) |
| 358 | { |
| 359 | struct pattern *pat; |
| 360 | struct child *ch; |
| 361 | |
| 362 | headappheader(req, "X-Ash-File", path); |
| 363 | if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) { |
| 364 | simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); |
| 365 | return; |
| 366 | } |
| 367 | if((ch = findchild(path, pat->childnm)) == NULL) { |
| 368 | flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm); |
| 369 | simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | if(childhandle(ch, req, fd)) |
| 374 | simpleerror(fd, 500, "Server Error", "The request handler crashed."); |
| 375 | } |
| 376 | |
| 377 | static void handledir(struct hthead *req, int fd, char *path) |
| 378 | { |
| 379 | /* XXX: Todo */ |
| 380 | simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet."); |
| 381 | } |
| 382 | |
| 383 | static int checkdir(struct hthead *req, int fd, char *path) |
| 384 | { |
| 385 | return(0); |
| 386 | } |
| 387 | |
| 388 | static void serve(struct hthead *req, int fd) |
| 389 | { |
| 390 | char *p, *p2, *path, *tmp, *buf, *p3, *nm; |
| 391 | struct stat sb; |
| 392 | DIR *dir; |
| 393 | struct dirent *dent; |
| 394 | |
| 395 | now = time(NULL); |
| 396 | nm = req->rest; |
| 397 | path = sstrdup("."); |
| 398 | p = nm; |
| 399 | while(1) { |
| 400 | if((p2 = strchr(p, '/')) == NULL) { |
| 401 | } else { |
| 402 | *(p2++) = 0; |
| 403 | } |
| 404 | |
| 405 | if(!*p) { |
| 406 | if(p2 == NULL) { |
| 407 | if(stat(path, &sb)) { |
| 408 | flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno)); |
| 409 | simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition."); |
| 410 | goto fail; |
| 411 | } |
| 412 | break; |
| 413 | } else { |
| 414 | simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); |
| 415 | goto fail; |
| 416 | } |
| 417 | } |
| 418 | if(*p == '.') { |
| 419 | simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); |
| 420 | goto fail; |
| 421 | } |
| 422 | |
| 423 | getconfig(path); |
| 424 | |
| 425 | /* |
| 426 | * First, check the name verbatimely: |
| 427 | */ |
| 428 | buf = sprintf3("%s/%s", path, p); |
| 429 | if(!stat(buf, &sb)) { |
| 430 | if(S_ISDIR(sb.st_mode)) { |
| 431 | tmp = path; |
| 432 | if(!strcmp(path, ".")) |
| 433 | path = sstrdup(p); |
| 434 | else |
| 435 | path = sprintf2("%s/%s", path, p); |
| 436 | free(tmp); |
| 437 | if(p2 == NULL) { |
| 438 | stdredir(req, fd, 301, sprintf3("%s/", p)); |
| 439 | goto out; |
| 440 | } |
| 441 | if(checkdir(req, fd, path)) |
| 442 | break; |
| 443 | goto next; |
| 444 | } |
| 445 | if(S_ISREG(sb.st_mode)) { |
| 446 | tmp = path; |
| 447 | path = sprintf2("%s/%s", path, p); |
| 448 | free(tmp); |
| 449 | break; |
| 450 | } |
| 451 | simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); |
| 452 | goto fail; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Check the file extensionlessly: |
| 457 | */ |
| 458 | if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) { |
| 459 | while((dent = readdir(dir)) != NULL) { |
| 460 | buf = sprintf3("%s/%s", path, dent->d_name); |
| 461 | if((p3 = strchr(dent->d_name, '.')) != NULL) |
| 462 | *p3 = 0; |
| 463 | if(strcmp(dent->d_name, p)) |
| 464 | continue; |
| 465 | if(stat(buf, &sb)) |
| 466 | continue; |
| 467 | if(!S_ISREG(sb.st_mode)) |
| 468 | continue; |
| 469 | tmp = path; |
| 470 | path = sstrdup(buf); |
| 471 | free(tmp); |
| 472 | break; |
| 473 | } |
| 474 | closedir(dir); |
| 475 | if(dent != NULL) |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); |
| 480 | goto fail; |
| 481 | |
| 482 | next: |
| 483 | if(p2 == NULL) |
| 484 | break; |
| 485 | p = p2; |
| 486 | } |
| 487 | if(p2 == NULL) |
| 488 | replrest(req, ""); |
| 489 | else |
| 490 | replrest(req, p2); |
| 491 | if(!strncmp(path, "./", 2)) |
| 492 | memmove(path, path + 2, strlen(path + 2) + 1); |
| 493 | if(S_ISDIR(sb.st_mode)) { |
| 494 | handledir(req, fd, path); |
| 495 | } else if(S_ISREG(sb.st_mode)) { |
| 496 | handlefile(req, fd, path); |
| 497 | } else { |
| 498 | simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); |
| 499 | goto fail; |
| 500 | } |
| 501 | goto out; |
| 502 | |
| 503 | fail: |
| 504 | /* No special handling, for now at least. */ |
| 505 | out: |
| 506 | free(path); |
| 507 | } |
| 508 | |
| 509 | int main(int argc, char **argv) |
| 510 | { |
| 511 | struct hthead *req; |
| 512 | int fd; |
| 513 | |
| 514 | if(argc < 2) { |
| 515 | flog(LOG_ERR, "usage: dirplex DIR"); |
| 516 | exit(1); |
| 517 | } |
| 518 | if(chdir(argv[1])) { |
| 519 | flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno)); |
| 520 | exit(1); |
| 521 | } |
| 522 | signal(SIGCHLD, SIG_IGN); |
| 523 | while(1) { |
| 524 | if((fd = recvreq(0, &req)) < 0) { |
| 525 | if(errno != 0) |
| 526 | flog(LOG_ERR, "recvreq: %s", strerror(errno)); |
| 527 | break; |
| 528 | } |
| 529 | serve(req, fd); |
| 530 | freehthead(req); |
| 531 | close(fd); |
| 532 | } |
| 533 | return(0); |
| 534 | } |