2020-10-02 19:39:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Omar Polo <op@omarpolo.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-10-07 13:50:18 +02:00
|
|
|
#include <errno.h>
|
2020-10-02 19:39:00 +02:00
|
|
|
#include <fcntl.h>
|
2020-12-21 14:38:31 +01:00
|
|
|
#include <limits.h>
|
2021-01-10 23:29:22 +01:00
|
|
|
#include <netdb.h>
|
2020-11-03 15:48:26 +01:00
|
|
|
#include <signal.h>
|
2021-01-10 16:50:27 +01:00
|
|
|
#include <stdarg.h>
|
2020-10-02 19:39:00 +02:00
|
|
|
#include <string.h>
|
2020-12-20 20:03:55 +01:00
|
|
|
|
2020-12-24 16:48:39 +01:00
|
|
|
#include "gmid.h"
|
2020-10-07 14:38:27 +02:00
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
struct vhost hosts[HOSTSLEN];
|
|
|
|
|
|
|
|
int goterror;
|
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
int exfd;
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
struct conf conf;
|
2020-11-06 16:18:38 +01:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
void
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
if (conf.foreground) {
|
2021-01-10 16:50:27 +01:00
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
} else
|
|
|
|
vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
void
|
2021-01-10 23:29:22 +01:00
|
|
|
logs(int priority, struct client *c,
|
|
|
|
const char *fmt, ...)
|
2021-01-10 16:50:27 +01:00
|
|
|
{
|
2021-01-10 23:29:22 +01:00
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
|
|
|
char *fmted, *s;
|
|
|
|
size_t len;
|
|
|
|
int ec;
|
2021-01-10 16:50:27 +01:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
if (c == NULL) {
|
|
|
|
strncpy(hbuf, "<internal>", sizeof(hbuf));
|
|
|
|
sbuf[0] = '\0';
|
|
|
|
} else {
|
|
|
|
len = sizeof(c->addr);
|
|
|
|
ec = getnameinfo((struct sockaddr*)&c->addr, len,
|
2021-01-15 16:21:51 +01:00
|
|
|
hbuf, sizeof(hbuf),
|
|
|
|
sbuf, sizeof(sbuf),
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
2021-01-15 15:03:45 +01:00
|
|
|
if (ec != 0)
|
|
|
|
fatal("getnameinfo: %s", gai_strerror(ec));
|
|
|
|
}
|
2021-01-10 23:29:22 +01:00
|
|
|
|
|
|
|
if (vasprintf(&fmted, fmt, ap) == -1)
|
|
|
|
fatal("vasprintf: %s", strerror(errno));
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
if (conf.foreground)
|
2021-01-10 23:29:22 +01:00
|
|
|
fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
|
|
|
|
else {
|
|
|
|
if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
|
|
|
|
fatal("asprintf: %s", strerror(errno));
|
|
|
|
syslog(priority | LOG_DAEMON, "%s", s);
|
|
|
|
free(s);
|
|
|
|
}
|
2021-01-10 16:50:27 +01:00
|
|
|
|
2021-01-10 23:29:22 +01:00
|
|
|
free(fmted);
|
2021-01-10 16:50:27 +01:00
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:54:44 +01:00
|
|
|
/* strchr, but with a bound */
|
|
|
|
static char *
|
|
|
|
gmid_strnchr(char *s, int c, size_t len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
if (s[i] == c)
|
|
|
|
return &s[i];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
log_request(struct client *c, char *meta, size_t l)
|
|
|
|
{
|
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
|
|
|
|
char *t;
|
|
|
|
size_t len;
|
|
|
|
int ec;
|
|
|
|
|
|
|
|
len = sizeof(c->addr);
|
|
|
|
ec = getnameinfo((struct sockaddr*)&c->addr, len,
|
|
|
|
hbuf, sizeof(hbuf),
|
|
|
|
sbuf, sizeof(sbuf),
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
if (ec != 0)
|
|
|
|
fatal("getnameinfo: %s", gai_strerror(ec));
|
|
|
|
|
2021-01-21 09:26:21 +01:00
|
|
|
if (c->iri.schema != NULL) {
|
|
|
|
/* serialize the IRI */
|
|
|
|
strlcpy(b, c->iri.schema, sizeof(b));
|
|
|
|
strlcat(b, "://", sizeof(b));
|
|
|
|
strlcat(b, c->iri.host, sizeof(b));
|
|
|
|
strlcat(b, "/", sizeof(b));
|
|
|
|
strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
|
|
|
|
if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
|
|
|
|
strlcat(b, "?", sizeof(b));
|
|
|
|
strlcat(b, c->iri.query, sizeof(b));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strlcpy(b, c->req, sizeof(b));
|
2021-01-20 19:54:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
|
|
|
|
t = meta + len;
|
|
|
|
|
|
|
|
if (conf.foreground)
|
|
|
|
fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
|
|
|
|
(int)(t - meta), meta);
|
|
|
|
else
|
|
|
|
syslog(LOG_INFO | LOG_DAEMON, "%s:%s GET %s %.*s",
|
|
|
|
hbuf, sbuf, b, (int)(t - meta), meta);
|
|
|
|
}
|
|
|
|
|
2020-11-06 16:18:38 +01:00
|
|
|
void
|
2020-12-28 09:55:56 +01:00
|
|
|
sig_handler(int sig)
|
2020-11-06 16:18:38 +01:00
|
|
|
{
|
|
|
|
(void)sig;
|
|
|
|
}
|
|
|
|
|
2020-11-06 18:05:44 +01:00
|
|
|
int
|
|
|
|
starts_with(const char *str, const char *prefix)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; prefix[i] != '\0'; ++i)
|
|
|
|
if (str[i] != prefix[i])
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-14 10:06:24 +02:00
|
|
|
ssize_t
|
|
|
|
filesize(int fd)
|
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
if ((len = lseek(fd, 0, SEEK_END)) == -1)
|
|
|
|
return -1;
|
|
|
|
if (lseek(fd, 0, SEEK_SET) == -1)
|
|
|
|
return -1;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
char *
|
|
|
|
absolutify_path(const char *path)
|
2020-10-02 19:39:00 +02:00
|
|
|
{
|
2021-01-18 00:23:58 +01:00
|
|
|
char *wd, *r;
|
2020-11-10 14:07:36 +01:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
if (*path == '/')
|
|
|
|
return strdup(path);
|
2020-11-10 14:07:36 +01:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
wd = getcwd(NULL, 0);
|
|
|
|
if (asprintf(&r, "%s/%s", wd, path) == -1)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("asprintf: %s", strerror(errno));
|
2021-01-18 00:23:58 +01:00
|
|
|
free(wd);
|
|
|
|
return r;
|
2020-11-10 14:07:36 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
void
|
|
|
|
yyerror(const char *msg)
|
2020-11-10 14:07:36 +01:00
|
|
|
{
|
2021-01-18 00:23:58 +01:00
|
|
|
goterror = 1;
|
|
|
|
fprintf(stderr, "%d: %s\n", yylineno, msg);
|
2020-10-14 10:06:24 +02:00
|
|
|
}
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
int
|
|
|
|
parse_portno(const char *p)
|
2020-10-14 10:06:24 +02:00
|
|
|
{
|
2021-01-18 00:23:58 +01:00
|
|
|
char *ep;
|
|
|
|
long lval;
|
2020-10-14 10:06:24 +02:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
errno = 0;
|
|
|
|
lval = strtol(p, &ep, 10);
|
|
|
|
if (p[0] == '\0' || *ep != '\0')
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("not a number: %s", p);
|
2021-01-18 00:23:58 +01:00
|
|
|
if (lval < 0 || lval > UINT16_MAX)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("port number out of range for domain %s: %ld", p, lval);
|
2021-01-18 00:23:58 +01:00
|
|
|
return lval;
|
2020-10-02 19:39:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-01-18 00:23:58 +01:00
|
|
|
parse_conf(const char *path)
|
2020-10-02 19:39:00 +02:00
|
|
|
{
|
2021-01-18 00:23:58 +01:00
|
|
|
if ((yyin = fopen(path, "r")) == NULL)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("cannot open config %s", path);
|
2021-01-18 00:23:58 +01:00
|
|
|
yyparse();
|
|
|
|
fclose(yyin);
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
if (goterror)
|
|
|
|
exit(1);
|
2020-10-02 19:39:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-01-18 00:23:58 +01:00
|
|
|
load_vhosts(struct tls_config *tlsconf)
|
2021-01-13 19:40:18 +01:00
|
|
|
{
|
2021-01-15 10:17:43 +01:00
|
|
|
struct vhost *h;
|
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
/* we need to set something, then we can add how many key we want */
|
|
|
|
if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_config_set_keypair_file failed");
|
2021-01-15 10:17:43 +01:00
|
|
|
|
|
|
|
for (h = hosts; h->domain != NULL; ++h) {
|
2021-01-18 00:23:58 +01:00
|
|
|
if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("failed to load the keypair (%s, %s)",
|
2021-01-18 00:23:58 +01:00
|
|
|
h->cert, h->key);
|
2020-10-07 13:50:18 +02:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("open %s for domain %s", h->dir, h->domain);
|
2020-10-07 13:50:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
int
|
2020-10-15 13:45:57 +02:00
|
|
|
make_socket(int port, int family)
|
2020-10-02 19:39:00 +02:00
|
|
|
{
|
|
|
|
int sock, v;
|
2020-10-15 13:45:57 +02:00
|
|
|
struct sockaddr_in addr4;
|
|
|
|
struct sockaddr_in6 addr6;
|
|
|
|
struct sockaddr *addr;
|
|
|
|
socklen_t len;
|
|
|
|
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
bzero(&addr4, sizeof(addr4));
|
|
|
|
addr4.sin_family = family;
|
|
|
|
addr4.sin_port = htons(port);
|
|
|
|
addr4.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr = (struct sockaddr*)&addr4;
|
|
|
|
len = sizeof(addr4);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
bzero(&addr6, sizeof(addr6));
|
|
|
|
addr6.sin6_family = AF_INET6;
|
|
|
|
addr6.sin6_port = htons(port);
|
|
|
|
addr6.sin6_addr = in6addr_any;
|
|
|
|
addr = (struct sockaddr*)&addr6;
|
|
|
|
len = sizeof(addr6);
|
|
|
|
break;
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2020-10-15 13:45:57 +02:00
|
|
|
default:
|
|
|
|
/* unreachable */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("socket: %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
|
|
|
v = 1;
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
|
|
|
v = 1;
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2020-10-07 13:50:18 +02:00
|
|
|
mark_nonblock(sock);
|
|
|
|
|
2020-10-15 13:45:57 +02:00
|
|
|
if (bind(sock, addr, len) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("bind: %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
|
|
|
if (listen(sock, 16) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("listen: %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
int
|
|
|
|
listener_main()
|
|
|
|
{
|
|
|
|
int sock4, sock6;
|
|
|
|
struct tls *ctx = NULL;
|
|
|
|
struct tls_config *tlsconf;
|
|
|
|
|
2021-01-18 19:18:57 +01:00
|
|
|
load_default_mime();
|
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
if ((tlsconf = tls_config_new()) == NULL)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_config_new");
|
2021-01-16 20:41:34 +01:00
|
|
|
|
|
|
|
/* optionally accept client certs, but don't try to verify them */
|
|
|
|
tls_config_verify_client_optional(tlsconf);
|
|
|
|
tls_config_insecure_noverifycert(tlsconf);
|
|
|
|
|
|
|
|
if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_config_set_protocols");
|
2021-01-16 20:41:34 +01:00
|
|
|
|
|
|
|
if ((ctx = tls_server()) == NULL)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_server failure");
|
2021-01-16 20:41:34 +01:00
|
|
|
|
|
|
|
load_vhosts(tlsconf);
|
|
|
|
|
|
|
|
if (tls_configure(ctx, tlsconf) == -1)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_configure: %s", tls_error(ctx));
|
2021-01-16 20:41:34 +01:00
|
|
|
|
|
|
|
if (!conf.foreground && daemon(0, 1) == -1)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
sock4 = make_socket(conf.port, AF_INET);
|
|
|
|
sock6 = -1;
|
|
|
|
if (conf.ipv6)
|
|
|
|
sock6 = make_socket(conf.port, AF_INET6);
|
|
|
|
|
|
|
|
sandbox();
|
|
|
|
loop(ctx, sock4, sock6);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
void
|
|
|
|
usage(const char *me)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2021-01-15 10:17:43 +01:00
|
|
|
"USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
|
|
|
|
"[-p port] [-x cgi-bin]\n",
|
2020-10-02 19:39:00 +02:00
|
|
|
me);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2021-01-16 20:41:34 +01:00
|
|
|
int ch, p[2];
|
2021-01-15 10:17:43 +01:00
|
|
|
const char *config_path = NULL;
|
|
|
|
size_t i;
|
2021-01-15 15:03:45 +01:00
|
|
|
int conftest = 0;
|
2020-11-06 16:18:38 +01:00
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
bzero(hosts, sizeof(hosts));
|
|
|
|
for (i = 0; i < HOSTSLEN; ++i)
|
|
|
|
hosts[i].dirfd = -1;
|
2020-12-21 15:42:06 +01:00
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
conf.foreground = 1;
|
|
|
|
conf.port = 1965;
|
|
|
|
conf.ipv6 = 0;
|
2021-01-15 19:55:05 +01:00
|
|
|
conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
|
2020-11-06 10:58:30 +01:00
|
|
|
|
2021-01-18 19:18:57 +01:00
|
|
|
init_mime();
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
|
2020-10-02 19:39:00 +02:00
|
|
|
switch (ch) {
|
2021-01-11 13:08:50 +01:00
|
|
|
case '6':
|
2021-01-15 10:17:43 +01:00
|
|
|
conf.ipv6 = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
hosts[0].cert = optarg;
|
2021-01-11 13:08:50 +01:00
|
|
|
break;
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
case 'c':
|
2021-01-15 10:17:43 +01:00
|
|
|
config_path = optarg;
|
2020-10-02 19:39:00 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
2021-01-15 10:17:43 +01:00
|
|
|
free((char*)hosts[0].dir);
|
|
|
|
if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("absolutify_path");
|
2020-10-02 19:39:00 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-02 21:18:01 +01:00
|
|
|
case 'f':
|
2021-01-15 10:17:43 +01:00
|
|
|
conf.foreground = 1;
|
2020-12-02 21:18:01 +01:00
|
|
|
break;
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
case 'h':
|
|
|
|
usage(*argv);
|
|
|
|
return 0;
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
case 'K':
|
|
|
|
hosts[0].key = optarg;
|
2020-10-02 19:39:00 +02:00
|
|
|
break;
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
case 'n':
|
|
|
|
conftest = 1;
|
2020-11-18 09:12:27 +01:00
|
|
|
break;
|
2021-01-15 10:17:43 +01:00
|
|
|
|
|
|
|
case 'p':
|
|
|
|
conf.port = parse_portno(optarg);
|
2021-01-17 10:34:06 +01:00
|
|
|
break;
|
2020-11-18 09:12:27 +01:00
|
|
|
|
2020-11-06 13:01:31 +01:00
|
|
|
case 'x':
|
2021-01-15 10:17:43 +01:00
|
|
|
/* drop the starting / (if any) */
|
|
|
|
if (*optarg == '/')
|
|
|
|
optarg++;
|
|
|
|
hosts[0].cgi = optarg;
|
2020-11-06 13:01:31 +01:00
|
|
|
break;
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
if (config_path != NULL) {
|
|
|
|
if (hosts[0].cert != NULL || hosts[0].key != NULL ||
|
|
|
|
hosts[0].dir != NULL)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("can't specify options in conf mode");
|
2021-01-15 10:17:43 +01:00
|
|
|
parse_conf(config_path);
|
|
|
|
} else {
|
|
|
|
if (hosts[0].cert == NULL || hosts[0].key == NULL ||
|
|
|
|
hosts[0].dir == NULL)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("missing cert, key or root directory to serve");
|
2021-01-15 10:17:43 +01:00
|
|
|
hosts[0].domain = "*";
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:15:58 +01:00
|
|
|
if (conftest) {
|
|
|
|
puts("config OK");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-15 10:17:43 +01:00
|
|
|
|
2020-12-28 09:55:56 +01:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
|
|
|
#ifdef SIGINFO
|
|
|
|
signal(SIGINFO, sig_handler);
|
|
|
|
#endif
|
|
|
|
signal(SIGUSR2, sig_handler);
|
|
|
|
|
2021-01-17 12:56:50 +01:00
|
|
|
if (!conf.foreground) {
|
2020-12-28 09:55:56 +01:00
|
|
|
signal(SIGHUP, SIG_IGN);
|
2021-01-17 12:56:50 +01:00
|
|
|
if (daemon(1, 1) == -1)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("daemon: %s", strerror(errno));
|
2021-01-17 12:56:50 +01:00
|
|
|
}
|
2020-12-28 09:55:56 +01:00
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
|
|
|
|
fatal("socketpair: %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
fatal("fork: %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
case 0: /* child */
|
|
|
|
close(p[0]);
|
|
|
|
exfd = p[1];
|
|
|
|
listener_main();
|
|
|
|
_exit(0);
|
2021-01-11 16:28:25 +01:00
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
default: /* parent */
|
|
|
|
close(p[1]);
|
|
|
|
return executor_main(p[0]);
|
|
|
|
}
|
2020-10-02 19:39:00 +02:00
|
|
|
}
|