/* Configuration file stuff */ #include "httpd.h" #undef exit /* we're not in a subprocess here */ static char *readline(char *buf, size_t maxlen, FILE *f) { do { buf = fgets(buf, maxlen, f); } while (buf && *buf == '#' || *buf == '\n'); if (buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; return buf; } static char *fixpath(char *path) { char *newpath; if (!strncmp(path, "./", 2)) { if (!servroot) { outerr("./ in path without ServerRoot defined"); exit(20); } newpath = salloc(strlen(servroot) + strlen(path)); sprintf(newpath, "%s%s", servroot, path+2); } else { newpath = salloc(strlen(path) + 2); strcpy(newpath, path); } if (path[strlen(path)-1] != '/') strcat(newpath, "/"); return newpath; } /* Returns 1 if conf/httpd.conf was found and readable, 0 otherwise. */ int readconf() { FILE *f; char buf[256], buf0[256]; char *t, *u; Alias *a; DocType *dt; f = fopen("conf/httpd.conf", "r"); if (!f) return 0; while (readline(buf, sizeof(buf), f)) { strcpy(buf0, buf); t = strtok(buf, " \t"); if (!strcmp(t, "ServerRoot")) { if (t = strtok(NULL, " \t")) { servroot = NULL; servroot = fixpath(t); } else { outerr("Bad ServerRoot directive: `%s'", buf0); } } else if (!strcmp(t, "DocumentRoot")) { if (t = strtok(NULL, " \t")) { docroot = fixpath(t); } else { outerr("Bad DocumentRoot directive: `%s'", buf0); } } else if (!strcmp(t, "UserDir")) { if (t = strtok(NULL, " \t")) { userdir = fixpath(t); } else { outerr("Bad UserDir directive: `%s'", buf0); } } else if (!strcmp(t, "DirectoryIndex")) { if (t = strtok(NULL, " \t")) { dirindex = strdup(t); } else { outerr("Bad DirectoryIndex directive: `%s'", buf0); } } else if (!strcmp(t, "Alias")) { if ((t = strtok(NULL, " \t")) && (u = strtok(NULL, " \t"))) { a = salloc(sizeof(*a)); a->next = aliases; a->prev = NULL; aliases = a; a->http_path = strdup(t); a->real_path = fixpath(u); } else { outerr("Bad Alias directive: `%s'", buf0); } } else if (!strcmp(t, "ScriptAlias")) { if ((t = strtok(NULL, " \t")) && (u = strtok(NULL, " \t"))) { a = salloc(sizeof(*a)); a->next = script_aliases; a->prev = NULL; script_aliases = a; a->http_path = strdup(t); a->real_path = fixpath(u); } else { outerr("Bad ScriptAlias directive: `%s'", buf0); } } else { outerr("Unknown configuration directive: `%s'0", buf0); } } fclose(f); if (f = fopen("conf/mime.types", "r")) { while (readline(buf, sizeof(buf), f)) { dt = salloc(sizeof(*dt)); dt->next = mime_types; dt->prev = NULL; if (t = strtok(buf, " \t")) { dt->type = strdup(t); } else { free(dt); continue; } dt->numext = 0; dt->ext = NULL; while (t = strtok(NULL, " \t")) { dt->ext = srealloc(dt->ext, ++dt->numext * sizeof(char *)); dt->ext[dt->numext-1] = strdup(t); } mime_types = dt; } } return 1; }