remove all vestiges of libowfat

This commit is contained in:
Neale Pickett 2012-02-14 17:23:32 -07:00
parent ef5f424643
commit 10258ec975
3 changed files with 110 additions and 218 deletions

View File

@ -1,6 +1,6 @@
VERSION := $(shell head -n 1 CHANGES | tr -d :) VERSION := $(shell head -n 1 CHANGES | tr -d :)
CFLAGS = -DFNORD='"fnord/$(VERSION)"' CFLAGS = -DFNORD='"fnord/$(VERSION)"' -Wall -Werror
all: fnord fnord-cgi fnord-idx all: fnord fnord-cgi fnord-idx

272
fnord.c
View File

@ -22,8 +22,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <limits.h> #include <limits.h>
#include "str.c"
/* /*
* Some things I use for debugging * Some things I use for debugging
*/ */
@ -222,9 +220,9 @@ dolog(off_t len)
sanitize(ua); sanitize(ua);
sanitize(refer); sanitize(refer);
fprintf(stderr, "%s %d %d %s %s %s %s\n", fprintf(stderr, "%s %ld %lu %s %s %s %s\n",
remote_ip ? remote_ip : "0.0.0.0", remote_ip ? remote_ip : "0.0.0.0",
retcode, len, host, ua, refer, url); retcode, (unsigned long)len, host, ua, refer, url);
} }
/* /*
@ -233,20 +231,16 @@ dolog(off_t len)
static void static void
badrequest(long code, const char *httpcomment, const char *message) badrequest(long code, const char *httpcomment, const char *message)
{ {
DUMP();
retcode = code; retcode = code;
DUMP();
dolog(0); dolog(0);
DUMP(); printf("HTTP/1.0 %ld %s\r\nConnection: close\r\n", code, httpcomment);
printf("HTTP/1.0 %d %s\r\nConnection: close\r\n", code, httpcomment);
if (message && message[0]) { if (message && message[0]) {
printf("Content-Length: %d\r\nContent-Type: text/html\r\n\r\n", printf("Content-Length: %lu\r\nContent-Type: text/html\r\n\r\n",
strlen(message)); (unsigned long) strlen(message));
fputs(message, stdout); fputs(message, stdout);
} else { } else {
printf("\r\n", stdout); fputs("\r\n", stdout);
} }
DUMP();
fflush(stdout); fflush(stdout);
exit(0); exit(0);
} }
@ -281,9 +275,11 @@ static const char *cgivars[CGIENVLEN] = {
static int static int
iscgivar(const char *s) iscgivar(const char *s)
{ {
int sl = strlen(s);
register unsigned int i = 0; register unsigned int i = 0;
for (; i < CGIENVLEN; i++) for (; i < CGIENVLEN; i++)
if (str_start(s, cgivars[i])) if (! strncmp(s, cgivars[i], sl))
return 1; return 1;
return 0; return 0;
} }
@ -297,13 +293,34 @@ elen(register const char *const *e)
return i; return i;
} }
char *
env_append(const char *key, const char *val)
{
static char buf[MAXHEADERLEN * 2 + PATH_MAX + 200];
static char *p = buf;
char *ret = p;
if (! key) {
p = buf;
return NULL;
}
p = stpcpy(p, key);
*(p++) = '=';
if (val) {
p = stpcpy(p, val) + 1;
} else {
*(p++) = 0;
}
return ret;
}
static void static void
do_cgi(const char *pathinfo, const char *const *envp) do_cgi(const char *pathinfo, const char *const *envp)
{ {
const char *method_name[] = { "?", "GET", "HEAD", "POST" }; const char *method_name[] = { "?", "GET", "HEAD", "POST" };
char cgi_env_buf[MAXHEADERLEN * 2 + PATH_MAX + 200];
register unsigned int en = elen(envp); register unsigned int en = elen(envp);
char *tmp = cgi_env_buf;
char **cgi_arg; char **cgi_arg;
register int i; register int i;
char **cgi_env = char **cgi_env =
@ -313,135 +330,31 @@ do_cgi(const char *pathinfo, const char *const *envp)
cgi_env[1] = "SERVER_PROTOCOL=HTTP/1.0"; cgi_env[1] = "SERVER_PROTOCOL=HTTP/1.0";
cgi_env[2] = "SERVER_SOFTWARE=" FNORD; cgi_env[2] = "SERVER_SOFTWARE=" FNORD;
cgi_env[3] = tmp; i = 3;
tmp += str_copy(tmp, "SERVER_NAME="); env_append(NULL, NULL);
tmp += str_copy(tmp, host); cgi_env[i++] = env_append("SERVER_NAME", host);
*tmp = 0; cgi_env[i++] = env_append("SERVER_PORT", port);
++tmp; cgi_env[i++] = env_append("REQUEST_METHOD", method_name[method]);
cgi_env[i++] = env_append("REQUEST_URI", uri);
cgi_env[4] = tmp; cgi_env[i++] = env_append("SCRIPT_NAME", url);
tmp += str_copy(tmp, "SERVER_PORT="); if (remote_ip) cgi_env[i++] = env_append("REMOTE_ADDR", remote_ip);
tmp += str_copy(tmp, port); if (remote_port) cgi_env[i++] = env_append("REMOTE_PORT", remote_port);
*tmp = 0; if (remote_ident) cgi_env[i++] = env_append("REMOTE_IDENT", remote_ident);
++tmp; if (ua) cgi_env[i++] = env_append("HTTP_USER_AGENT", ua);
if (cookie) cgi_env[i++] = env_append("HTTP_COOKIE", cookie);
cgi_env[5] = tmp; if (refer) cgi_env[i++] = env_append("HTTP_REFERER", refer);
tmp += str_copy(tmp, "REQUEST_METHOD="); if (accept_enc) cgi_env[i++] = env_append("HTTP_ACCEPT_ENCODING", accept_enc);
tmp += str_copy(tmp, method_name[method]); if (auth_type) cgi_env[i++] = env_append("AUTH_TYPE", auth_type);
*tmp = 0; if (content_type) cgi_env[i++] = env_append("CONTENT_TYPE", content_type);
++tmp; if (content_type) cgi_env[i++] = env_append("CONTENT_LENGTH", content_len);
if (args) cgi_env[i++] = env_append("QUERY_STRING", args);
cgi_env[6] = tmp;
tmp += str_copy(tmp, "REQUEST_URI=");
tmp += str_copy(tmp, uri);
*tmp = 0;
++tmp;
cgi_env[7] = tmp;
tmp += str_copy(tmp, "SCRIPT_NAME=");
tmp += str_copy(tmp, url);
*tmp = 0;
++tmp;
i = 7;
if (remote_ip) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "REMOTE_ADDR=");
tmp += str_copy(tmp, remote_ip);
*tmp = 0;
++tmp;
}
if (remote_port) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "REMOTE_PORT=");
tmp += str_copy(tmp, remote_port);
*tmp = 0;
++tmp;
}
if (remote_ident) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "REMOTE_IDENT=");
tmp += str_copy(tmp, remote_ident);
*tmp = 0;
++tmp;
}
if (ua) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "HTTP_USER_AGENT=");
tmp += str_copy(tmp, ua);
*tmp = 0;
++tmp;
}
if (cookie) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "HTTP_COOKIE=");
tmp += str_copy(tmp, cookie);
*tmp = 0;
++tmp;
}
if (refer) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "HTTP_REFERER=");
tmp += str_copy(tmp, refer);
*tmp = 0;
++tmp;
}
if (accept_enc) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "HTTP_ACCEPT_ENCODING=");
tmp += str_copy(tmp, accept_enc);
*tmp = 0;
++tmp;
}
if (auth_type) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "AUTH_TYPE=");
tmp += str_copy(tmp, auth_type);
*tmp = 0;
++tmp;
}
if (content_type) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "CONTENT_TYPE=");
tmp += str_copy(tmp, content_type);
*tmp = 0;
++tmp;
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "CONTENT_LENGTH=");
DUMP_s(content_len);
tmp += str_copy(tmp, content_len);
*tmp = 0;
++tmp;
}
if (args) {
cgi_env[++i] = tmp;
tmp += str_copy(tmp, "QUERY_STRING=");
tmp += str_copy(tmp, args);
*tmp = 0;
++tmp;
}
if (pathinfo) { if (pathinfo) {
cgi_env[++i] = tmp; char *rp = realpath(pathinfo, NULL);
tmp += str_copy(tmp, "PATH_INFO=");
tmp += str_copy(tmp, pathinfo); cgi_env[i++] = env_append("PATH_INFO", pathinfo);
*tmp = 0; if (! rp) rp = pathinfo;
++tmp; cgi_env[i++] = env_append("PATH_TRANSLATED", rp);
cgi_env[++i] = tmp; if (rp != pathinfo) free(rp);
tmp += str_copy(tmp, "PATH_TRANSLATED=");
tmp +=
realpath(pathinfo, tmp) ? strlen(tmp) : str_copy(tmp,
pathinfo);
++tmp;
} }
{ {
@ -475,17 +388,24 @@ do_cgi(const char *pathinfo, const char *const *envp)
cgi_arg[1] = 0; cgi_arg[1] = 0;
} }
{
char tmp[PATH_MAX];
i = strrchr(url, '/') - url; i = strrchr(url, '/') - url;
strncpy(tmp, url + 1, i); strncpy(tmp, url + 1, i);
tmp[i] = 0; tmp[i] = 0;
chdir(tmp); chdir(tmp);
}
{
char tmp[PATH_MAX];
/* /*
* program name * program name
*/ */
cgi_arg[0] = tmp; cgi_arg[0] = tmp;
tmp[0] = '.'; tmp[0] = '.';
tmp[str_copy(tmp + 1, url + i) + 1] = 0; strcpy(tmp + 1, url + i);
/* /*
* start cgi * start cgi
@ -493,6 +413,7 @@ do_cgi(const char *pathinfo, const char *const *envp)
execve(cgi_arg[0], cgi_arg, cgi_env); execve(cgi_arg[0], cgi_arg, cgi_env);
raise(SIGQUIT); /* gateway unavailable. */ raise(SIGQUIT); /* gateway unavailable. */
} }
}
static void static void
cgi_child(int sig) cgi_child(int sig)
@ -712,17 +633,13 @@ header(char *buf, int buflen, const char *hname)
for (i = 0; i < buflen - slen - 2; ++i) { for (i = 0; i < buflen - slen - 2; ++i) {
DUMPf("[%.*s] [%s]", slen, buf + i, hname); DUMPf("[%.*s] [%s]", slen, buf + i, hname);
if (!strncasecmp(buf + i, hname, slen)) { if (!strncasecmp(buf + i, hname, slen)) {
DUMP();
if (i && (buf[i - 1] && buf[i - 1] != '\n')) if (i && (buf[i - 1] && buf[i - 1] != '\n'))
continue; continue;
DUMP();
if (buf[i + slen] != ':' || buf[i + slen + 1] != ' ') if (buf[i + slen] != ':' || buf[i + slen + 1] != ' ')
continue; continue;
DUMP();
c = buf + i + slen + 2; c = buf + i + slen + 2;
i += slen + 2; i += slen + 2;
for (; i < buflen; ++i) { for (; i < buflen; ++i) {
DUMP_c(buf[i]);
if (buf[i] == 0 || buf[i] == '\n' || buf[i] == '\r') { if (buf[i] == 0 || buf[i] == '\n' || buf[i] == '\r') {
buf[i] = 0; buf[i] = 0;
break; break;
@ -1371,14 +1288,19 @@ get_ucspi_env(void)
{ {
char *ucspi = getenv("PROTO"); char *ucspi = getenv("PROTO");
if (ucspi) { if (ucspi) {
char *buf = alloca(strlen(ucspi) + 20); int protolen = strlen(ucspi);
unsigned int tmp = str_copy(buf, ucspi); char *buf = alloca(protolen + 20);
buf[tmp + str_copy(buf + tmp, "REMOTEIP")] = 0;
strcpy(buf, ucspi);
strcpy(buf + protolen, "REMOTEIP");
remote_ip = getenv(buf); remote_ip = getenv(buf);
#ifdef CGI #ifdef CGI
buf[tmp + str_copy(buf + tmp, "REMOTEPORT")] = 0; strcpy(buf + protolen, "REMOTEPORT");
remote_port = getenv(buf); remote_port = getenv(buf);
buf[tmp + str_copy(buf + tmp, "REMOTEINFO")] = 0;
strcpy(buf + protolen, "REMOTEINFO");
remote_ident = getenv(buf); remote_ident = getenv(buf);
#endif #endif
} }
@ -1430,6 +1352,7 @@ serve_read_write(int fd)
} }
todo -= olen; todo -= olen;
} }
return 0;
} }
static int static int
@ -1803,17 +1726,13 @@ main(int argc, char *argv[], const char *const *envp)
ip = "127.0.0.1"; ip = "127.0.0.1";
if (strlen(ip) + strlen(port) > 90) if (strlen(ip) + strlen(port) > 90)
exit(101); exit(101);
sprintf(Buf, "%s:%s", ip, port);
host = Buf; host = Buf;
i = str_copy(Buf, ip);
i += str_copy(Buf + i, ":");
i += str_copy(Buf + i, port);
#ifdef NORMALIZE_HOST #ifdef NORMALIZE_HOST
} else { } else {
char *colon = strchr(host, ':'); char *colon = strchr(host, ':');
if (*colon == 0) { if (*colon == 0) {
i = str_copy(Buf, host); sprintf(Buf, "%s:%s", host, port);
i += str_copy(Buf + i, ":");
i += str_copy(Buf + i, port);
host = Buf; host = Buf;
} }
#endif #endif
@ -1914,9 +1833,7 @@ main(int argc, char *argv[], const char *const *envp)
if (*nurl == '/') { if (*nurl == '/') {
int i; int i;
nurl = alloca(strlen(url) + 12); nurl = alloca(strlen(url) + 12);
i = str_copy(nurl, url); i = sprintf(nurl, "%sindex.html", url);
i += str_copy(nurl + i, "index.html");
nurl[i] = 0;
url = nurl; url = nurl;
nurl = url + i; nurl = url + i;
} }
@ -1973,18 +1890,19 @@ main(int argc, char *argv[], const char *const *envp)
/* /*
* look if file.gz is also there and acceptable * look if file.gz is also there and acceptable
*/ */
char *fnord = alloca(strlen(url) + 4); int ul = strlen(url);
int i, char *fnord = alloca(ul + 4);
fd2, int fd2,
trypng = 0; trypng = 0;
char *oldencoding = encoding; char *oldencoding = encoding;
char *oldmimetype = mimetype; char *oldmimetype = mimetype;
i = str_copy(fnord, url);
if (i > 4 && !strcmp(fnord + i - 4, ".gif")) { strcpy(fnord, url);
if (ul > 4 && !strcmp(fnord + ul - 4, ".gif")) {
trypng = 1; trypng = 1;
str_copy(fnord + i - 3, "png"); strcpy(fnord + ul - 3, "png");
} else } else
str_copy(fnord + i, ".gz"); strcpy(fnord + ul, ".gz");
fd2 = doit(buf, len, fnord, 0); fd2 = doit(buf, len, fnord, 0);
if (fd2 >= 0) { /* yeah! */ if (fd2 >= 0) { /* yeah! */
url = fnord; url = fnord;
@ -2015,9 +1933,9 @@ main(int argc, char *argv[], const char *const *envp)
} }
#endif #endif
if (encoding) { if (encoding) {
printf("Content-Encoding: %s\r\n"); printf("Content-Encoding: %s\r\n", encoding);
} }
printf("Content-Length: %lu\r\n", rangeend - rangestart); printf("Content-Length: %llu\r\n", (unsigned long long)(rangeend - rangestart));
printf("Last-Modified: "); printf("Last-Modified: ");
{ {
struct tm *x = gmtime(&st.st_mtime); struct tm *x = gmtime(&st.st_mtime);
@ -2033,8 +1951,10 @@ main(int argc, char *argv[], const char *const *envp)
} }
if (rangestart || rangeend != st.st_size) { if (rangestart || rangeend != st.st_size) {
printf printf
("Accept-Ranges: bytes\r\nContent-Range: bytes %lu-%lu/%lu\r\n", ("Accept-Ranges: bytes\r\nContent-Range: bytes %llu-%llu/%llu\r\n",
rangestart, rangeend - 1, st.st_size); (unsigned long long)rangestart,
(unsigned long long)rangeend - 1,
(unsigned long long)st.st_size);
} }
fputs("\r\n", stdout); fputs("\r\n", stdout);
if (method == GET || method == POST) { if (method == GET || method == POST) {
@ -2068,14 +1988,12 @@ main(int argc, char *argv[], const char *const *envp)
fflush(stdout); fflush(stdout);
} }
} }
#ifdef CHROOT
tuttikaputti:
#endif
switch (retcode) { switch (retcode) {
case 404: case 404:
{ {
char *space = alloca(strlen(url) + 2);
#ifdef INDEX_CGI #ifdef INDEX_CGI
char *space = alloca(strlen(url) + 2);
if (handleindexcgi(url, origurl, space)) if (handleindexcgi(url, origurl, space))
goto indexcgi; goto indexcgi;
#endif #endif

26
str.c
View File

@ -1,26 +0,0 @@
unsigned int
str_copy(char *out, const char *in)
{
char *s = out;
const char *t = in;
while (1) {
if (!(*s=*t)) break;
s += 1;
t += 1;
}
return s - out;
}
int
str_start(const char *s, const char *t)
{
char x;
while (1) {
x = *t++;
if (!x) return 1;
if (x != *s++) return 0;
}
}