2018-02-04 20:27:33 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
2018-03-04 23:14:25 +00:00
|
|
|
#include <limits.h>
|
2018-02-04 20:27:33 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "http.h"
|
|
|
|
#include "sock.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static char *udsname;
|
|
|
|
|
|
|
|
static void
|
2018-02-05 16:15:29 +00:00
|
|
|
serve(int infd, struct sockaddr_storage *in_sa)
|
2018-02-04 20:27:33 +00:00
|
|
|
{
|
|
|
|
struct request r;
|
|
|
|
time_t t;
|
|
|
|
enum status status;
|
|
|
|
char inaddr[INET6_ADDRSTRLEN /* > INET_ADDRSTRLEN */];
|
2018-04-02 23:23:00 +00:00
|
|
|
char tstmp[21];
|
2018-02-04 20:27:33 +00:00
|
|
|
|
2018-02-05 16:15:29 +00:00
|
|
|
/* set connection timeout */
|
|
|
|
if (sock_set_timeout(infd, 30)) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-02-04 20:27:33 +00:00
|
|
|
|
2018-02-05 16:15:29 +00:00
|
|
|
/* handle request */
|
|
|
|
if (!(status = http_get_request(infd, &r))) {
|
|
|
|
status = http_send_response(infd, &r);
|
|
|
|
}
|
2018-02-04 20:27:33 +00:00
|
|
|
|
2018-02-05 16:15:29 +00:00
|
|
|
/* write output to log */
|
|
|
|
t = time(NULL);
|
2018-04-02 23:23:00 +00:00
|
|
|
if (!strftime(tstmp, sizeof(tstmp), "%Y-%m-%dT%H:%M:%SZ",
|
2018-02-05 16:15:29 +00:00
|
|
|
gmtime(&t))) {
|
|
|
|
warn("strftime: Exceeded buffer capacity");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (sock_get_inaddr_str(in_sa, inaddr, LEN(inaddr))) {
|
|
|
|
goto cleanup;
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
2018-02-05 16:15:29 +00:00
|
|
|
printf("%s\t%s\t%d\t%s\t%s\n", tstmp, inaddr, status,
|
|
|
|
r.field[REQ_HOST], r.target);
|
|
|
|
cleanup:
|
|
|
|
/* clean up and finish */
|
|
|
|
shutdown(infd, SHUT_RD);
|
|
|
|
shutdown(infd, SHUT_WR);
|
|
|
|
close(infd);
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
if (udsname)
|
|
|
|
sock_rem_uds(udsname);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sigcleanup(int sig)
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
kill(0, sig);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handlesignals(void(*hdl)(int))
|
|
|
|
{
|
2019-05-30 21:15:47 +00:00
|
|
|
struct sigaction sa = {
|
|
|
|
.sa_handler = hdl,
|
|
|
|
};
|
2018-02-04 20:27:33 +00:00
|
|
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
sigaction(SIGQUIT, &sa, NULL);
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:50:39 +00:00
|
|
|
static int
|
|
|
|
spacetok(const char *s, char **t, size_t tlen)
|
|
|
|
{
|
|
|
|
const char *tok;
|
|
|
|
size_t i, j, toki, spaces;
|
|
|
|
|
|
|
|
/* fill token-array with NULL-pointers */
|
|
|
|
for (i = 0; i < tlen; i++) {
|
|
|
|
t[i] = NULL;
|
|
|
|
}
|
|
|
|
toki = 0;
|
|
|
|
|
|
|
|
/* don't allow NULL string or leading spaces */
|
|
|
|
if (!s || *s == ' ') {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
start:
|
|
|
|
/* skip spaces */
|
|
|
|
for (; *s == ' '; s++)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* don't allow trailing spaces */
|
|
|
|
if (*s == '\0') {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* consume token */
|
|
|
|
for (tok = s, spaces = 0; ; s++) {
|
|
|
|
if (*s == '\\' && *(s + 1) == ' ') {
|
|
|
|
spaces++;
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
} else if (*s == ' ') {
|
|
|
|
/* end of token */
|
|
|
|
goto token;
|
|
|
|
} else if (*s == '\0') {
|
|
|
|
/* end of string */
|
|
|
|
goto token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token:
|
|
|
|
if (toki >= tlen) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!(t[toki] = malloc(s - tok - spaces + 1))) {
|
|
|
|
die("malloc:");
|
|
|
|
}
|
|
|
|
for (i = 0, j = 0; j < s - tok - spaces + 1; i++, j++) {
|
|
|
|
if (tok[i] == '\\' && tok[i + 1] == ' ') {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
t[toki][j] = tok[i];
|
|
|
|
}
|
|
|
|
t[toki][s - tok - spaces] = '\0';
|
|
|
|
toki++;
|
|
|
|
|
|
|
|
if (*s == ' ') {
|
|
|
|
s++;
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
for (i = 0; i < tlen; i++) {
|
|
|
|
free(t[i]);
|
|
|
|
t[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-02-04 20:27:33 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2018-03-04 23:14:25 +00:00
|
|
|
const char *opts = "[-u user] [-g group] [-n num] [-d dir] [-l] "
|
2018-03-05 08:51:29 +00:00
|
|
|
"[-i file] [-v vhost] ... [-m map] ...";
|
2018-03-04 23:14:25 +00:00
|
|
|
|
2020-04-21 15:04:37 +00:00
|
|
|
die("usage: %s -p port [-h host] %s\n"
|
2018-03-05 08:51:29 +00:00
|
|
|
" %s -U file [-p port] %s", argv0,
|
2018-03-04 23:14:25 +00:00
|
|
|
opts, argv0, opts);
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct group *grp = NULL;
|
2018-02-05 16:15:29 +00:00
|
|
|
struct passwd *pwd = NULL;
|
2018-02-04 20:27:33 +00:00
|
|
|
struct rlimit rlim;
|
2018-02-05 16:15:29 +00:00
|
|
|
struct sockaddr_storage in_sa;
|
|
|
|
pid_t cpid, wpid, spid;
|
2018-03-04 23:30:53 +00:00
|
|
|
size_t i;
|
2018-02-05 16:15:29 +00:00
|
|
|
socklen_t in_sa_len;
|
2018-03-04 23:30:53 +00:00
|
|
|
int insock, status = 0, infd;
|
2018-03-04 23:14:25 +00:00
|
|
|
const char *err;
|
2019-02-24 20:50:39 +00:00
|
|
|
char *tok[4];
|
2018-03-04 23:14:25 +00:00
|
|
|
|
|
|
|
/* defaults */
|
|
|
|
int maxnprocs = 512;
|
|
|
|
char *servedir = ".";
|
|
|
|
char *user = "nobody";
|
|
|
|
char *group = "nogroup";
|
|
|
|
|
|
|
|
s.host = s.port = NULL;
|
|
|
|
s.vhost = NULL;
|
|
|
|
s.map = NULL;
|
|
|
|
s.vhost_len = s.map_len = 0;
|
|
|
|
s.docindex = "index.html";
|
|
|
|
s.listdirs = 0;
|
2018-02-04 20:27:33 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2019-02-23 12:50:59 +00:00
|
|
|
case 'd':
|
|
|
|
servedir = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
group = EARGF(usage());
|
|
|
|
break;
|
2018-02-04 20:27:33 +00:00
|
|
|
case 'h':
|
2018-03-04 23:14:25 +00:00
|
|
|
s.host = EARGF(usage());
|
2018-02-04 20:27:33 +00:00
|
|
|
break;
|
2019-02-23 12:50:59 +00:00
|
|
|
case 'i':
|
|
|
|
s.docindex = EARGF(usage());
|
|
|
|
if (strchr(s.docindex, '/')) {
|
|
|
|
die("The document index must not contain '/'");
|
|
|
|
}
|
2018-02-04 20:27:33 +00:00
|
|
|
break;
|
2019-02-23 12:50:59 +00:00
|
|
|
case 'l':
|
|
|
|
s.listdirs = 1;
|
2018-02-04 20:27:33 +00:00
|
|
|
break;
|
2019-02-23 12:50:59 +00:00
|
|
|
case 'm':
|
2019-02-24 20:50:39 +00:00
|
|
|
if (spacetok(EARGF(usage()), tok, 3) || !tok[0] || !tok[1]) {
|
|
|
|
usage();
|
2019-02-23 12:50:59 +00:00
|
|
|
}
|
|
|
|
if (!(s.map = reallocarray(s.map, ++s.map_len,
|
|
|
|
sizeof(struct map)))) {
|
|
|
|
die("reallocarray:");
|
|
|
|
}
|
2019-02-24 20:50:39 +00:00
|
|
|
s.map[s.map_len - 1].from = tok[0];
|
|
|
|
s.map[s.map_len - 1].to = tok[1];
|
|
|
|
s.map[s.map_len - 1].chost = tok[2];
|
2018-03-04 23:14:25 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
maxnprocs = strtonum(EARGF(usage()), 1, INT_MAX, &err);
|
|
|
|
if (err) {
|
|
|
|
die("strtonum '%s': %s", EARGF(usage()), err);
|
|
|
|
}
|
|
|
|
break;
|
2019-02-23 12:50:59 +00:00
|
|
|
case 'p':
|
|
|
|
s.port = EARGF(usage());
|
2018-03-04 23:14:25 +00:00
|
|
|
break;
|
2019-02-23 12:50:59 +00:00
|
|
|
case 'U':
|
|
|
|
udsname = EARGF(usage());
|
2018-03-04 23:14:25 +00:00
|
|
|
break;
|
2019-02-23 12:50:59 +00:00
|
|
|
case 'u':
|
|
|
|
user = EARGF(usage());
|
2018-02-04 20:27:33 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
2019-02-24 20:50:39 +00:00
|
|
|
if (spacetok(EARGF(usage()), tok, 4) || !tok[0] || !tok[1] ||
|
|
|
|
!tok[2]) {
|
|
|
|
usage();
|
2018-03-04 23:14:25 +00:00
|
|
|
}
|
2018-04-02 00:54:59 +00:00
|
|
|
if (!(s.vhost = reallocarray(s.vhost, ++s.vhost_len,
|
2018-03-04 23:14:25 +00:00
|
|
|
sizeof(struct vhost)))) {
|
|
|
|
die("reallocarray:");
|
|
|
|
}
|
2019-02-24 20:50:39 +00:00
|
|
|
s.vhost[s.vhost_len - 1].chost = tok[0];
|
|
|
|
s.vhost[s.vhost_len - 1].regex = tok[1];
|
|
|
|
s.vhost[s.vhost_len - 1].dir = tok[2];
|
|
|
|
s.vhost[s.vhost_len - 1].prefix = tok[3];
|
2018-02-04 20:27:33 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND
|
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:04:37 +00:00
|
|
|
/* can't have both host and UDS but must have one of port or UDS*/
|
|
|
|
if ((s.host && udsname) || !(s.port || udsname)) {
|
2018-03-04 23:14:25 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2018-02-04 20:27:33 +00:00
|
|
|
if (udsname && (!access(udsname, F_OK) || errno != ENOENT)) {
|
|
|
|
die("UNIX-domain socket: %s", errno ?
|
|
|
|
strerror(errno) : "file exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compile and check the supplied vhost regexes */
|
2018-03-04 23:14:25 +00:00
|
|
|
for (i = 0; i < s.vhost_len; i++) {
|
|
|
|
if (regcomp(&s.vhost[i].re, s.vhost[i].regex,
|
|
|
|
REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
|
|
|
|
die("regcomp '%s': invalid regex",
|
|
|
|
s.vhost[i].regex);
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* raise the process limit */
|
|
|
|
rlim.rlim_cur = rlim.rlim_max = maxnprocs;
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &rlim) < 0) {
|
|
|
|
die("setrlimit RLIMIT_NPROC:");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* validate user and group */
|
|
|
|
errno = 0;
|
|
|
|
if (user && !(pwd = getpwnam(user))) {
|
2018-02-04 20:40:51 +00:00
|
|
|
die("getpwnam '%s': %s", user, errno ? strerror(errno) :
|
|
|
|
"Entry not found");
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
if (group && !(grp = getgrnam(group))) {
|
2018-02-04 20:40:51 +00:00
|
|
|
die("getgrnam '%s': %s", group, errno ? strerror(errno) :
|
|
|
|
"Entry not found");
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 02:08:08 +00:00
|
|
|
/* Open a new process group */
|
|
|
|
setpgid(0,0);
|
|
|
|
|
2018-02-04 20:27:33 +00:00
|
|
|
handlesignals(sigcleanup);
|
|
|
|
|
|
|
|
/* bind socket */
|
|
|
|
insock = udsname ? sock_get_uds(udsname, pwd->pw_uid, grp->gr_gid) :
|
2018-03-04 23:14:25 +00:00
|
|
|
sock_get_ips(s.host, s.port);
|
2018-02-04 20:27:33 +00:00
|
|
|
|
|
|
|
switch (cpid = fork()) {
|
|
|
|
case -1:
|
|
|
|
warn("fork:");
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
/* restore default handlers */
|
|
|
|
handlesignals(SIG_DFL);
|
|
|
|
|
|
|
|
/* reap children automatically */
|
|
|
|
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
|
|
|
|
die("signal: Failed to set SIG_IGN on SIGCHLD");
|
|
|
|
}
|
|
|
|
|
2019-09-23 14:56:28 +00:00
|
|
|
/* limit ourselves to reading the servedir and block further unveils */
|
|
|
|
eunveil(servedir, "r");
|
|
|
|
eunveil(NULL, NULL);
|
|
|
|
|
2018-02-04 20:27:33 +00:00
|
|
|
/* chroot */
|
|
|
|
if (chdir(servedir) < 0) {
|
|
|
|
die("chdir '%s':", servedir);
|
|
|
|
}
|
|
|
|
if (chroot(".") < 0) {
|
|
|
|
die("chroot .:");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drop root */
|
|
|
|
if (grp && setgroups(1, &(grp->gr_gid)) < 0) {
|
|
|
|
die("setgroups:");
|
|
|
|
}
|
|
|
|
if (grp && setgid(grp->gr_gid) < 0) {
|
|
|
|
die("setgid:");
|
|
|
|
}
|
|
|
|
if (pwd && setuid(pwd->pw_uid) < 0) {
|
|
|
|
die("setuid:");
|
|
|
|
}
|
2019-09-23 14:56:28 +00:00
|
|
|
|
|
|
|
if (udsname) {
|
|
|
|
epledge("stdio rpath proc unix", NULL);
|
|
|
|
} else {
|
|
|
|
epledge("stdio rpath proc inet", NULL);
|
|
|
|
}
|
|
|
|
|
2018-02-04 20:27:33 +00:00
|
|
|
if (getuid() == 0) {
|
2018-02-04 20:40:51 +00:00
|
|
|
die("Won't run as root user", argv0);
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
if (getgid() == 0) {
|
2018-02-04 20:40:51 +00:00
|
|
|
die("Won't run as root group", argv0);
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 16:15:29 +00:00
|
|
|
/* accept incoming connections */
|
|
|
|
while (1) {
|
|
|
|
in_sa_len = sizeof(in_sa);
|
|
|
|
if ((infd = accept(insock, (struct sockaddr *)&in_sa,
|
|
|
|
&in_sa_len)) < 0) {
|
|
|
|
warn("accept:");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fork and handle */
|
|
|
|
switch ((spid = fork())) {
|
|
|
|
case 0:
|
|
|
|
serve(infd, &in_sa);
|
2018-02-23 21:29:00 +00:00
|
|
|
exit(0);
|
2018-02-05 16:15:29 +00:00
|
|
|
break;
|
2018-02-23 21:40:47 +00:00
|
|
|
case -1:
|
|
|
|
warn("fork:");
|
|
|
|
/* fallthrough */
|
2018-02-05 16:15:29 +00:00
|
|
|
default:
|
|
|
|
/* close the connection in the parent */
|
|
|
|
close(infd);
|
|
|
|
}
|
|
|
|
}
|
2018-02-04 20:27:33 +00:00
|
|
|
exit(0);
|
|
|
|
default:
|
2019-09-23 14:56:28 +00:00
|
|
|
/* limit ourselves even further while we are waiting */
|
|
|
|
if (udsname) {
|
2020-03-20 19:35:34 +00:00
|
|
|
eunveil(udsname, "c");
|
|
|
|
eunveil(NULL, NULL);
|
2019-09-23 14:56:28 +00:00
|
|
|
epledge("stdio cpath", NULL);
|
|
|
|
} else {
|
2020-03-20 19:35:34 +00:00
|
|
|
eunveil("/", "");
|
|
|
|
eunveil(NULL, NULL);
|
2019-09-23 14:56:28 +00:00
|
|
|
epledge("stdio", NULL);
|
|
|
|
}
|
|
|
|
|
2018-02-04 20:27:33 +00:00
|
|
|
while ((wpid = wait(&status)) > 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
return status;
|
|
|
|
}
|