2009-08-15 18:56:11 +00:00
|
|
|
/* See LICENSE file for license details. */
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2011-06-26 09:57:34 +00:00
|
|
|
#include <grp.h>
|
2009-08-15 18:56:11 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-06-26 09:57:34 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netinet/in.h>
|
2009-08-15 18:56:11 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#define LENGTH(x) (sizeof x / sizeof x[0])
|
2010-03-28 11:31:30 +00:00
|
|
|
#define MAXBUFLEN 1024
|
2011-06-26 09:57:34 +00:00
|
|
|
#define MIN(x,y) ((x) < (y) ? (x) : (y))
|
2009-08-15 18:56:11 +00:00
|
|
|
|
2010-03-28 01:57:44 +00:00
|
|
|
enum {
|
|
|
|
GET = 4,
|
|
|
|
HEAD = 5,
|
|
|
|
};
|
|
|
|
|
2009-08-15 18:56:11 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *extension;
|
|
|
|
const char *mimetype;
|
|
|
|
} MimeType;
|
|
|
|
|
2011-02-16 20:29:55 +00:00
|
|
|
typedef struct _Param Param;
|
|
|
|
struct _Param {
|
|
|
|
const char *key;
|
|
|
|
const char *value;
|
|
|
|
Param *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int type;
|
|
|
|
int fd;
|
|
|
|
const char *hostname;
|
|
|
|
const char *resource;
|
|
|
|
Param *params;
|
|
|
|
} Request;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *hostname;
|
|
|
|
const char *resource;
|
|
|
|
void (*handle)(const Request *r);
|
|
|
|
} RequestHandler;
|
|
|
|
|
2009-08-15 18:56:11 +00:00
|
|
|
static const char HttpOk[] = "200 OK";
|
2014-02-15 13:31:14 +00:00
|
|
|
static const char HttpMoved[] = "301 Moved Permanently";
|
2009-08-15 18:56:11 +00:00
|
|
|
static const char HttpUnauthorized[] = "401 Unauthorized";
|
|
|
|
static const char HttpNotFound[] = "404 Not Found";
|
|
|
|
static const char texthtml[] = "text/html";
|
|
|
|
|
2010-03-28 01:57:44 +00:00
|
|
|
static ssize_t writetext(const char *buf);
|
|
|
|
static ssize_t writedata(const char *buf, size_t buflen);
|
2011-02-13 12:46:34 +00:00
|
|
|
static void atomiclog(int fd, const char *errstr, va_list ap);
|
2009-08-15 18:56:11 +00:00
|
|
|
static void die(const char *errstr, ...);
|
2011-02-13 12:46:34 +00:00
|
|
|
static void logmsg(const char *errstr, ...);
|
|
|
|
static void logerrmsg(const char *errstr, ...);
|
2009-08-15 18:56:11 +00:00
|
|
|
static void response(void);
|
2010-03-28 01:09:14 +00:00
|
|
|
static void responsecgi(void);
|
2009-08-15 18:56:11 +00:00
|
|
|
static void responsedir(void);
|
|
|
|
static void responsedirdata(DIR *d);
|
|
|
|
static void responsefile(void);
|
|
|
|
static void responsefiledata(int fd, off_t size);
|
|
|
|
static int request(void);
|
|
|
|
static void serve(int fd);
|
|
|
|
static void sighandler(int sig);
|
|
|
|
static char *tstamp(void);
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
static char location[256];
|
|
|
|
static int running = 1;
|
2010-03-28 11:31:30 +00:00
|
|
|
static char host[NI_MAXHOST];
|
|
|
|
static char reqbuf[MAXBUFLEN+1];
|
|
|
|
static char resbuf[MAXBUFLEN+1];
|
|
|
|
static char reqhost[256];
|
2011-02-16 20:29:55 +00:00
|
|
|
static int fd;
|
|
|
|
static Request req;
|
2009-08-15 18:56:11 +00:00
|
|
|
|
|
|
|
ssize_t
|
2010-03-28 01:57:44 +00:00
|
|
|
writedata(const char *buf, size_t buf_len) {
|
2009-08-15 18:56:11 +00:00
|
|
|
ssize_t r, offset = 0;
|
|
|
|
|
2010-03-28 01:57:44 +00:00
|
|
|
while(offset < buf_len) {
|
2011-02-16 20:29:55 +00:00
|
|
|
if((r = write(req.fd, buf + offset, buf_len - offset)) == -1) {
|
2011-02-13 12:46:34 +00:00
|
|
|
logerrmsg("client %s closed connection\n", host);
|
2009-08-15 18:56:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
offset += r;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2010-03-28 01:57:44 +00:00
|
|
|
ssize_t
|
|
|
|
writetext(const char *buf) {
|
|
|
|
return writedata(buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
2011-02-13 12:46:34 +00:00
|
|
|
void
|
|
|
|
atomiclog(int fd, const char *errstr, va_list ap) {
|
|
|
|
static char buf[512];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
assemble the message in buf and write it in one pass
|
|
|
|
to avoid interleaved concurrent writes on a shared fd.
|
|
|
|
*/
|
|
|
|
n = snprintf(buf, sizeof buf, "%s: ", tstamp());
|
|
|
|
n += vsnprintf(buf + n, sizeof buf - n, errstr, ap);
|
|
|
|
if (n >= sizeof buf)
|
|
|
|
n = sizeof buf - 1;
|
|
|
|
write(fd, buf, n);
|
|
|
|
}
|
|
|
|
|
2009-08-15 18:56:11 +00:00
|
|
|
void
|
|
|
|
logmsg(const char *errstr, ...) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, errstr);
|
2011-02-13 12:46:34 +00:00
|
|
|
atomiclog(STDOUT_FILENO, errstr, ap);
|
2009-08-15 18:56:11 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
logerrmsg(const char *errstr, ...) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, errstr);
|
2011-02-13 12:46:34 +00:00
|
|
|
atomiclog(STDERR_FILENO, errstr, ap);
|
2009-08-15 18:56:11 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
die(const char *errstr, ...) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, errstr);
|
2011-02-13 12:46:34 +00:00
|
|
|
atomiclog(STDERR_FILENO, errstr, ap);
|
2009-08-15 18:56:11 +00:00
|
|
|
va_end(ap);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
responsehdr(const char *status) {
|
2010-03-28 11:31:30 +00:00
|
|
|
if(snprintf(resbuf, MAXBUFLEN,
|
2009-08-15 18:56:11 +00:00
|
|
|
"HTTP/1.1 %s\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Date: %s\r\n"
|
|
|
|
"Server: quark-"VERSION"\r\n",
|
2010-03-28 11:31:30 +00:00
|
|
|
status, tstamp()) >= MAXBUFLEN)
|
2009-08-15 18:56:11 +00:00
|
|
|
{
|
|
|
|
logerrmsg("snprintf failed, buffer size exceeded");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
return writetext(resbuf);
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
responsecontentlen(off_t size) {
|
2010-03-28 11:31:30 +00:00
|
|
|
if(snprintf(resbuf, MAXBUFLEN,
|
2009-08-15 18:56:11 +00:00
|
|
|
"Content-Length: %lu\r\n",
|
2010-03-28 11:31:30 +00:00
|
|
|
size) >= MAXBUFLEN)
|
2009-08-15 18:56:11 +00:00
|
|
|
{
|
|
|
|
logerrmsg("snprintf failed, buffer sizeof exceeded");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
return writetext(resbuf);
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
responselocation(const char *location, const char *pathinfo) {
|
2010-03-28 11:31:30 +00:00
|
|
|
if(snprintf(resbuf, MAXBUFLEN,
|
2009-08-15 18:56:11 +00:00
|
|
|
"Location: %s%s\r\n",
|
2010-03-28 11:31:30 +00:00
|
|
|
location, pathinfo) >= MAXBUFLEN)
|
2009-08-15 18:56:11 +00:00
|
|
|
{
|
|
|
|
logerrmsg("snprintf failed, buffer sizeof exceeded");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
return writetext(resbuf);
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
responsecontenttype(const char *mimetype) {
|
2010-03-28 11:31:30 +00:00
|
|
|
if(snprintf(resbuf, MAXBUFLEN,
|
2009-08-15 18:56:11 +00:00
|
|
|
"Content-Type: %s\r\n",
|
2010-03-28 11:31:30 +00:00
|
|
|
mimetype) >= MAXBUFLEN)
|
2009-08-15 18:56:11 +00:00
|
|
|
{
|
|
|
|
logerrmsg("snprintf failed, buffer sizeof exceeded");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
return writetext(resbuf);
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
responsefiledata(int fd, off_t size) {
|
2011-06-26 09:57:34 +00:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
for(; (n = read(fd, buf, MIN(size, sizeof buf))) > 0; size -= n)
|
|
|
|
if(write(req.fd, buf, n) != n)
|
|
|
|
logerrmsg("error writing to client %s: %s\n", host, strerror(errno));
|
|
|
|
if(n == -1)
|
|
|
|
logerrmsg("error reading from file: %s\n", strerror(errno));
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
responsefile(void) {
|
|
|
|
const char *mimetype = "unknown";
|
|
|
|
char *p;
|
|
|
|
int i, ffd;
|
|
|
|
struct stat st;
|
|
|
|
|
2011-02-13 12:46:34 +00:00
|
|
|
if(stat(reqbuf, &st) == -1 || (ffd = open(reqbuf, O_RDONLY)) == -1) {
|
2010-03-28 11:31:30 +00:00
|
|
|
logerrmsg("%s requests unknown path %s\n", host, reqbuf);
|
2009-08-15 18:56:11 +00:00
|
|
|
if(responsehdr(HttpNotFound) != -1
|
2010-03-28 01:57:44 +00:00
|
|
|
&& responsecontenttype(texthtml) != -1)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
return;
|
2011-02-16 20:29:55 +00:00
|
|
|
if(req.type == GET)
|
2010-03-28 01:57:44 +00:00
|
|
|
writetext("\r\n<html><body>404 Not Found</body></html>\r\n");
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if((p = strrchr(reqbuf, '.'))) {
|
|
|
|
p++;
|
|
|
|
for(i = 0; i < LENGTH(servermimes); i++)
|
|
|
|
if(!strcmp(servermimes[i].extension, p)) {
|
|
|
|
mimetype = servermimes[i].mimetype;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(responsehdr(HttpOk) != -1
|
|
|
|
&& responsecontentlen(st.st_size) != -1
|
2010-03-28 01:57:44 +00:00
|
|
|
&& responsecontenttype(mimetype) != -1)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
return;
|
2011-02-16 20:29:55 +00:00
|
|
|
if(req.type == GET && writetext("\r\n") != -1)
|
2009-08-15 18:56:11 +00:00
|
|
|
responsefiledata(ffd, st.st_size);
|
|
|
|
close(ffd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
responsedirdata(DIR *d) {
|
|
|
|
struct dirent *e;
|
|
|
|
|
|
|
|
if(responsehdr(HttpOk) != -1
|
2010-03-28 01:57:44 +00:00
|
|
|
&& responsecontenttype(texthtml) != -1)
|
|
|
|
;
|
2009-08-15 18:56:11 +00:00
|
|
|
else
|
|
|
|
return;
|
2011-02-16 20:29:55 +00:00
|
|
|
if(req.type == GET) {
|
2010-03-28 01:57:44 +00:00
|
|
|
if(writetext("\r\n<html><body><a href='..'>..</a><br>\r\n") == -1)
|
2009-08-15 18:56:11 +00:00
|
|
|
return;
|
2010-03-28 01:57:44 +00:00
|
|
|
while((e = readdir(d))) {
|
|
|
|
if(e->d_name[0] == '.') /* ignore hidden files, ., .. */
|
|
|
|
continue;
|
2010-03-28 11:31:30 +00:00
|
|
|
if(snprintf(resbuf, MAXBUFLEN, "<a href='%s%s'>%s</a><br>\r\n",
|
|
|
|
reqbuf, e->d_name, e->d_name) >= MAXBUFLEN)
|
2010-03-28 01:57:44 +00:00
|
|
|
{
|
|
|
|
logerrmsg("snprintf failed, buffer sizeof exceeded");
|
|
|
|
return;
|
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
if(writetext(resbuf) == -1)
|
2010-03-28 01:57:44 +00:00
|
|
|
return;
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
2010-03-28 01:57:44 +00:00
|
|
|
writetext("</body></html>\r\n");
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
responsedir(void) {
|
|
|
|
ssize_t len = strlen(reqbuf);
|
|
|
|
DIR *d;
|
|
|
|
|
2010-03-28 11:31:30 +00:00
|
|
|
if((reqbuf[len - 1] != '/') && (len + 1 < MAXBUFLEN)) {
|
2009-08-15 18:56:11 +00:00
|
|
|
/* add directory terminator if necessary */
|
|
|
|
reqbuf[len++] = '/';
|
|
|
|
reqbuf[len] = 0;
|
2010-03-28 11:31:30 +00:00
|
|
|
logmsg("redirecting %s to %s%s\n", host, location, reqbuf);
|
2009-08-15 18:56:11 +00:00
|
|
|
if(responsehdr(HttpMoved) != -1
|
|
|
|
&& responselocation(location, reqbuf) != -1
|
2010-03-28 01:57:44 +00:00
|
|
|
&& responsecontenttype(texthtml) != -1)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
return;
|
2011-02-16 20:29:55 +00:00
|
|
|
if(req.type == GET)
|
2010-03-28 01:57:44 +00:00
|
|
|
writetext("\r\n<html><body>301 Moved Permanently</a></body></html>\r\n");
|
2009-08-15 18:56:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
if(len + strlen(docindex) + 1 < MAXBUFLEN)
|
2009-08-15 18:56:11 +00:00
|
|
|
memcpy(reqbuf + len, docindex, strlen(docindex) + 1);
|
|
|
|
if(access(reqbuf, R_OK) == -1) { /* directory mode */
|
|
|
|
reqbuf[len] = 0; /* cut off docindex again */
|
|
|
|
if((d = opendir(reqbuf))) {
|
|
|
|
responsedirdata(d);
|
|
|
|
closedir(d);
|
|
|
|
}
|
2011-02-13 12:46:34 +00:00
|
|
|
else
|
|
|
|
logerrmsg("client %s requests %s but opendir failed: %s\n", host, reqbuf, strerror(errno));
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
responsefile(); /* docindex */
|
2010-03-28 01:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
responsecgi(void) {
|
|
|
|
FILE *cgi;
|
|
|
|
int r;
|
2009-08-15 18:56:11 +00:00
|
|
|
|
2011-02-16 20:29:55 +00:00
|
|
|
if(req.type == GET)
|
2010-03-28 01:57:44 +00:00
|
|
|
setenv("REQUEST_METHOD", "GET", 1);
|
2011-02-16 20:29:55 +00:00
|
|
|
else if(req.type == HEAD)
|
2010-03-28 01:57:44 +00:00
|
|
|
setenv("REQUEST_METHOD", "HEAD", 1);
|
|
|
|
else
|
|
|
|
return;
|
2010-03-28 11:31:30 +00:00
|
|
|
if(*reqhost)
|
|
|
|
setenv("SERVER_NAME", reqhost, 1);
|
2010-03-28 01:09:14 +00:00
|
|
|
setenv("SCRIPT_NAME", cgi_script, 1);
|
2010-03-28 01:57:44 +00:00
|
|
|
setenv("REQUEST_URI", reqbuf, 1);
|
2010-03-28 11:31:30 +00:00
|
|
|
logmsg("CGI SERVER_NAME=%s SCRIPT_NAME=%s REQUEST_URI=%s\n", reqhost, cgi_script, reqbuf);
|
2011-02-13 12:46:34 +00:00
|
|
|
if(chdir(cgi_dir) == -1)
|
|
|
|
logerrmsg("chdir to cgi directory %s failed: %s\n", cgi_dir, strerror(errno));
|
2010-03-28 01:09:14 +00:00
|
|
|
if((cgi = popen(cgi_script, "r"))) {
|
|
|
|
if(responsehdr(HttpOk) == -1)
|
|
|
|
return;
|
2010-03-28 11:31:30 +00:00
|
|
|
while((r = fread(resbuf, 1, MAXBUFLEN, cgi)) > 0) {
|
|
|
|
if(writedata(resbuf, r) == -1) {
|
2010-03-28 01:09:14 +00:00
|
|
|
pclose(cgi);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(cgi);
|
|
|
|
}
|
|
|
|
else {
|
2010-03-28 11:31:30 +00:00
|
|
|
logerrmsg("%s requests %s, but cannot run cgi script %s\n", host, cgi_script, reqbuf);
|
2010-03-28 01:09:14 +00:00
|
|
|
if(responsehdr(HttpNotFound) != -1
|
2010-03-28 01:57:44 +00:00
|
|
|
&& responsecontenttype(texthtml) != -1)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
return;
|
2011-02-16 20:29:55 +00:00
|
|
|
if(req.type == GET)
|
2010-03-28 01:57:44 +00:00
|
|
|
writetext("\r\n<html><body>404 Not Found</body></html>\r\n");
|
2010-03-28 01:09:14 +00:00
|
|
|
}
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
response(void) {
|
|
|
|
char *p;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
for(p = reqbuf; *p; p++)
|
2010-03-28 11:31:30 +00:00
|
|
|
if(*p == '\\' || (*p == '/' && *(p + 1) == '.')) { /* don't serve bogus or hidden files */
|
|
|
|
logerrmsg("%s requests bogus or hidden file %s\n", host, reqbuf);
|
2009-08-15 18:56:11 +00:00
|
|
|
if(responsehdr(HttpUnauthorized) != -1
|
2010-03-28 01:57:44 +00:00
|
|
|
&& responsecontenttype(texthtml) != -1)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
return;
|
2011-02-16 20:29:55 +00:00
|
|
|
if(req.type == GET)
|
2010-03-28 01:57:44 +00:00
|
|
|
writetext("\r\n<html><body>401 Unauthorized</body></html>\r\n");
|
2009-08-15 18:56:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
logmsg("%s requests: %s\n", host, reqbuf);
|
2010-03-28 01:09:14 +00:00
|
|
|
if(cgi_mode)
|
|
|
|
responsecgi();
|
|
|
|
else {
|
2011-02-13 12:46:34 +00:00
|
|
|
if(stat(reqbuf, &st) != -1 && S_ISDIR(st.st_mode))
|
2010-03-28 01:09:14 +00:00
|
|
|
responsedir();
|
|
|
|
else
|
|
|
|
responsefile();
|
|
|
|
}
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
request(void) {
|
|
|
|
char *p, *res;
|
2009-08-15 19:01:53 +00:00
|
|
|
int r;
|
2010-03-28 01:09:14 +00:00
|
|
|
size_t offset = 0;
|
2009-08-15 18:56:11 +00:00
|
|
|
|
2010-03-28 11:31:30 +00:00
|
|
|
do { /* MAXBUFLEN byte of reqbuf is emergency 0 terminator */
|
2011-02-16 20:29:55 +00:00
|
|
|
if((r = read(req.fd, reqbuf + offset, MAXBUFLEN - offset)) == -1) {
|
2010-03-28 11:31:30 +00:00
|
|
|
logerrmsg("read: %s\n", strerror(errno));
|
2010-03-28 01:09:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
offset += r;
|
|
|
|
reqbuf[offset] = 0;
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
while(r > 0 && offset < MAXBUFLEN && (!strstr(reqbuf, "\r\n") || !strstr(reqbuf, "\n")));
|
|
|
|
if((res = strstr(reqbuf, "Host:"))) {
|
|
|
|
for(res = res + 5; *res && (*res == ' ' || *res == '\t'); res++);
|
|
|
|
if(!*res)
|
|
|
|
goto invalid_request;
|
2011-02-13 12:46:34 +00:00
|
|
|
for(p = res; *p && *p != ' ' && *p != '\t' && *p != '\r' && *p != '\n'; p++);
|
2010-03-28 11:31:30 +00:00
|
|
|
if(!*p)
|
|
|
|
goto invalid_request;
|
|
|
|
*p = 0;
|
|
|
|
if(p - res > sizeof reqhost)
|
|
|
|
goto invalid_request;
|
|
|
|
memcpy(reqhost, res, p - res);
|
|
|
|
reqhost[p - res] = 0;
|
|
|
|
}
|
2010-03-28 01:09:14 +00:00
|
|
|
for(p = reqbuf; *p && *p != '\r' && *p != '\n'; p++);
|
2009-08-15 18:56:11 +00:00
|
|
|
if(*p == '\r' || *p == '\n') {
|
|
|
|
*p = 0;
|
2009-08-15 21:59:41 +00:00
|
|
|
/* check command */
|
2010-03-28 01:57:44 +00:00
|
|
|
if(!strncmp(reqbuf, "GET ", 4) && reqbuf[4] == '/')
|
2011-02-16 20:29:55 +00:00
|
|
|
req.type = GET;
|
2010-03-28 11:31:30 +00:00
|
|
|
else if(!strncmp(reqbuf, "HEAD ", 5) && reqbuf[5] == '/')
|
2011-02-16 20:29:55 +00:00
|
|
|
req.type = HEAD;
|
2010-03-28 01:57:44 +00:00
|
|
|
else
|
2009-08-15 21:59:41 +00:00
|
|
|
goto invalid_request;
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
2009-08-15 21:59:41 +00:00
|
|
|
else
|
|
|
|
goto invalid_request;
|
2009-08-15 18:56:11 +00:00
|
|
|
/* determine path */
|
2011-02-16 20:29:55 +00:00
|
|
|
for(res = reqbuf + req.type; *res && *(res + 1) == '/'; res++); /* strip '/' */
|
2010-03-28 11:31:30 +00:00
|
|
|
if(!*res)
|
2010-03-28 01:09:14 +00:00
|
|
|
goto invalid_request;
|
|
|
|
for(p = res; *p && *p != ' ' && *p != '\t'; p++);
|
2010-03-28 11:31:30 +00:00
|
|
|
if(!*p)
|
2010-03-28 01:09:14 +00:00
|
|
|
goto invalid_request;
|
2009-08-15 18:56:11 +00:00
|
|
|
*p = 0;
|
|
|
|
memmove(reqbuf, res, (p - res) + 1);
|
|
|
|
return 0;
|
2009-08-15 21:59:41 +00:00
|
|
|
invalid_request:
|
2010-03-28 11:31:30 +00:00
|
|
|
logerrmsg("%s performs invalid request %s\n", host, reqbuf);
|
2009-08-15 21:59:41 +00:00
|
|
|
return -1;
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
serve(int fd) {
|
|
|
|
int result;
|
|
|
|
socklen_t salen;
|
|
|
|
struct sockaddr sa;
|
|
|
|
|
|
|
|
while(running) {
|
2011-02-13 12:46:34 +00:00
|
|
|
salen = sizeof sa;
|
2011-02-16 20:29:55 +00:00
|
|
|
if((req.fd = accept(fd, &sa, &salen)) == -1) {
|
2009-08-15 21:11:26 +00:00
|
|
|
/* el cheapo socket release */
|
2010-03-28 11:31:30 +00:00
|
|
|
logerrmsg("cannot accept: %s, sleep a second...\n", strerror(errno));
|
2009-08-15 21:25:22 +00:00
|
|
|
sleep(1);
|
2009-08-15 21:11:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-02-13 12:46:34 +00:00
|
|
|
result = fork();
|
|
|
|
if(result == 0) {
|
2009-08-15 18:56:11 +00:00
|
|
|
close(fd);
|
2010-03-28 11:31:30 +00:00
|
|
|
host[0] = 0;
|
|
|
|
getnameinfo(&sa, salen, host, sizeof host, NULL, 0, NI_NOFQDN);
|
2009-08-15 18:56:11 +00:00
|
|
|
result = request();
|
2011-02-16 20:29:55 +00:00
|
|
|
shutdown(req.fd, SHUT_RD);
|
2009-08-15 18:56:11 +00:00
|
|
|
if(result == 0)
|
|
|
|
response();
|
2011-02-16 20:29:55 +00:00
|
|
|
shutdown(req.fd, SHUT_WR);
|
|
|
|
close(req.fd);
|
2009-08-15 18:56:11 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-02-13 12:46:34 +00:00
|
|
|
} else if (result == -1)
|
|
|
|
logerrmsg("fork failed: %s\n", strerror(errno));
|
2011-02-16 20:29:55 +00:00
|
|
|
close(req.fd);
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
2010-03-28 11:31:30 +00:00
|
|
|
logmsg("shutting down\n");
|
2009-08-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sighandler(int sig) {
|
2009-08-15 21:11:26 +00:00
|
|
|
static const char *signame[64] = {
|
|
|
|
[SIGHUP] = "SIGHUP",
|
|
|
|
[SIGINT] = "SIGINT",
|
|
|
|
[SIGQUIT] = "SIGQUIT",
|
|
|
|
[SIGABRT] = "SIGABRT",
|
|
|
|
[SIGTERM] = "SIGTERM",
|
|
|
|
[SIGCHLD] = "SIGCHLD"
|
|
|
|
};
|
2009-08-15 18:56:11 +00:00
|
|
|
switch(sig) {
|
|
|
|
default: break;
|
|
|
|
case SIGHUP:
|
|
|
|
case SIGINT:
|
|
|
|
case SIGQUIT:
|
|
|
|
case SIGABRT:
|
|
|
|
case SIGTERM:
|
2010-03-28 11:31:30 +00:00
|
|
|
logerrmsg("received signal %s, closing down\n", signame[sig] ? signame[sig] : "");
|
2009-08-15 18:56:11 +00:00
|
|
|
close(fd);
|
|
|
|
running = 0;
|
|
|
|
break;
|
|
|
|
case SIGCHLD:
|
|
|
|
while(0 < waitpid(-1, NULL, WNOHANG));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
tstamp(void) {
|
|
|
|
static char res[25];
|
|
|
|
time_t t = time(NULL);
|
|
|
|
|
|
|
|
memcpy(res, asctime(gmtime(&t)), 24);
|
|
|
|
res[24] = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
|
|
|
struct addrinfo hints, *ai;
|
2011-02-13 12:46:34 +00:00
|
|
|
struct passwd *upwd;
|
|
|
|
struct group *gpwd;
|
2009-08-15 18:56:11 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* arguments */
|
|
|
|
for(i = 1; i < argc; i++)
|
2010-03-28 11:31:30 +00:00
|
|
|
if(!strcmp(argv[i], "-v"))
|
2011-02-16 20:29:55 +00:00
|
|
|
die("quark-"VERSION", © 2009-2011 Anselm R Garbe\n");
|
2009-08-15 18:56:11 +00:00
|
|
|
else
|
2010-03-28 11:31:30 +00:00
|
|
|
die("usage: quark [-v]\n");
|
2009-08-15 18:56:11 +00:00
|
|
|
|
|
|
|
/* sanity checks */
|
2011-06-26 09:51:42 +00:00
|
|
|
if(user)
|
|
|
|
if(!(upwd = getpwnam(user)))
|
|
|
|
die("error: invalid user %s\n", user);
|
|
|
|
if(group)
|
|
|
|
if(!(gpwd = getgrnam(group)))
|
|
|
|
die("error: invalid group %s\n", group);
|
2009-08-15 18:56:11 +00:00
|
|
|
|
2009-08-16 12:33:02 +00:00
|
|
|
signal(SIGCHLD, sighandler);
|
|
|
|
signal(SIGHUP, sighandler);
|
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
signal(SIGQUIT, sighandler);
|
|
|
|
signal(SIGABRT, sighandler);
|
|
|
|
signal(SIGTERM, sighandler);
|
|
|
|
signal(SIGKILL, sighandler);
|
|
|
|
|
2009-08-15 18:56:11 +00:00
|
|
|
/* init */
|
|
|
|
setbuf(stdout, NULL); /* unbuffered stdout */
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
if((i = getaddrinfo(servername, serverport, &hints, &ai)))
|
|
|
|
die("error: getaddrinfo: %s\n", gai_strerror(i));
|
|
|
|
if((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) {
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
die("error: socket: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
if(bind(fd, ai->ai_addr, ai->ai_addrlen) == -1) {
|
|
|
|
close(fd);
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
die("error: bind: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
if(listen(fd, SOMAXCONN) == -1) {
|
|
|
|
close(fd);
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
die("error: listen: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!strcmp(serverport, "80"))
|
|
|
|
i = snprintf(location, sizeof location, "http://%s", servername);
|
|
|
|
else
|
|
|
|
i = snprintf(location, sizeof location, "http://%s:%s", servername, serverport);
|
|
|
|
if(i >= sizeof location) {
|
|
|
|
close(fd);
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
die("error: location too long\n");
|
|
|
|
}
|
|
|
|
|
2011-02-13 12:46:34 +00:00
|
|
|
if(chdir(docroot) == -1)
|
|
|
|
die("error: chdir %s: %s\n", docroot, strerror(errno));
|
|
|
|
if(chroot(".") == -1)
|
|
|
|
die("error: chroot .: %s\n", strerror(errno));
|
2009-08-15 18:56:11 +00:00
|
|
|
|
2011-06-26 09:51:42 +00:00
|
|
|
if(gpwd)
|
|
|
|
if(setgid(gpwd->gr_gid) == -1)
|
|
|
|
die("error: cannot set group id\n");
|
|
|
|
if(upwd)
|
|
|
|
if(setuid(upwd->pw_uid) == -1)
|
|
|
|
die("error: cannot set user id\n");
|
2009-08-15 18:56:11 +00:00
|
|
|
|
|
|
|
if(getuid() == 0)
|
|
|
|
die("error: won't run with root permissions, choose another user\n");
|
|
|
|
if(getgid() == 0)
|
|
|
|
die("error: won't run with root permissions, choose another group\n");
|
|
|
|
|
2010-03-28 11:31:30 +00:00
|
|
|
logmsg("listening on %s:%s using %s as root directory\n", servername, serverport, docroot);
|
2009-08-15 18:56:11 +00:00
|
|
|
|
|
|
|
serve(fd); /* main loop */
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
return 0;
|
|
|
|
}
|