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