gmid/ge.c

321 lines
7.1 KiB
C
Raw Normal View History

2022-09-07 22:47:33 +02:00
/*
* Copyright (c) 2022 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.
*/
#include "gmid.h"
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
2023-06-06 10:11:30 +02:00
#include <locale.h>
2022-09-07 22:47:33 +02:00
#include <libgen.h>
#include <signal.h>
#include <string.h>
2023-06-06 13:46:40 +02:00
#include <syslog.h>
2022-09-07 22:47:33 +02:00
#include <unistd.h>
2023-06-06 13:46:40 +02:00
#include "log.h"
2023-06-08 18:22:03 +02:00
int privsep_process;
2022-09-07 22:47:33 +02:00
static const struct option opts[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0},
};
2023-06-08 18:22:03 +02:00
void
log_request(struct client *c, char *meta, size_t l)
{
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
const char *t;
int ec;
ec = getnameinfo((struct sockaddr*)&c->raddr, c->raddrlen,
2023-06-08 18:22:03 +02:00
hbuf, sizeof(hbuf),
sbuf, sizeof(sbuf),
NI_NUMERICHOST | NI_NUMERICSERV);
if (ec != 0)
fatalx("getnameinfo: %s", gai_strerror(ec));
if (c->iri.schema != NULL) {
/* serialize the IRI */
strlcpy(b, c->iri.schema, sizeof(b));
strlcat(b, "://", sizeof(b));
/* log the decoded host name, but if it was invalid
* use the raw one. */
if (*c->domain != '\0')
strlcat(b, c->domain, sizeof(b));
else
strlcat(b, c->iri.host, sizeof(b));
if (*c->iri.path != '/')
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 {
if ((t = c->req) == NULL)
t = "";
strlcpy(b, t, sizeof(b));
}
if ((t = memchr(meta, '\r', l)) == NULL)
2023-06-23 23:03:24 +02:00
t = meta + l;
2023-06-08 18:22:03 +02:00
fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
(int)(t-meta), meta);
}
2022-09-07 22:47:33 +02:00
void
load_local_cert(struct vhost *h, const char *hostname, const char *dir)
{
char *cert, *key;
if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
2023-06-06 13:52:43 +02:00
fatal("asprintf");
2022-09-07 22:47:33 +02:00
if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
2023-06-06 13:52:43 +02:00
fatal("asprintf");
2022-09-07 22:47:33 +02:00
if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
gen_certificate(hostname, cert, key);
2023-06-08 18:22:03 +02:00
h->cert = tls_load_file(cert, &h->certlen, NULL);
if (h->cert == NULL)
fatal("can't load %s", cert);
h->key = tls_load_file(key, &h->keylen, NULL);
if (h->key == NULL)
fatal("can't load %s", key);
strlcpy(h->domain, hostname, sizeof(h->domain));
2022-09-07 22:47:33 +02:00
}
/* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
static void
pdirname(const char *path, char *dn)
{
char p[PATH_MAX+1];
char *t;
strlcpy(p, path, sizeof(p));
t = dirname(p);
memmove(dn, t, strlen(t)+1);
}
static void
mkdirs(const char *path, mode_t mode)
{
char dname[PATH_MAX+1];
pdirname(path, dname);
if (!strcmp(dname, "/"))
return;
mkdirs(dname, mode);
if (mkdir(path, mode) != 0 && errno != EEXIST)
fatal("can't mkdir %s", path);
2022-09-07 22:47:33 +02:00
}
/* $XDG_DATA_HOME/gmid */
char *
data_dir(void)
{
const char *home, *xdg;
char *t;
if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
if ((home = getenv("HOME")) == NULL)
2023-06-06 13:52:43 +02:00
fatalx("XDG_DATA_HOME and HOME both empty");
2022-09-07 22:47:33 +02:00
if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
2023-06-06 13:52:43 +02:00
fatalx("asprintf");
2022-09-07 22:47:33 +02:00
} else {
if (asprintf(&t, "%s/gmid", xdg) == -1)
2023-06-06 13:52:43 +02:00
fatal("asprintf");
2022-09-07 22:47:33 +02:00
}
mkdirs(t, 0755);
return t;
}
static int
2023-06-09 19:18:04 +02:00
serve(struct conf *conf, const char *host, int port, const char *dir)
2022-09-07 22:47:33 +02:00
{
struct addrinfo hints, *res, *res0;
2023-06-06 10:22:18 +02:00
int r, error, saved_errno, sock = -1;
2022-09-07 22:47:33 +02:00
const char *cause = NULL;
char service[32];
2023-06-06 10:22:18 +02:00
r = snprintf(service, sizeof(service), "%d", port);
if (r < 0 || (size_t)r >= sizeof(service))
2022-09-07 22:47:33 +02:00
fatal("snprintf");
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(host, service, &hints, &res0);
if (error)
fatalx("%s", gai_strerror(error));
2022-09-07 22:47:33 +02:00
for (res = res0; res; res = res->ai_next) {
sock = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if (sock == -1) {
cause = "socket";
continue;
}
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
cause = "bind";
saved_errno = errno;
close(sock);
errno = saved_errno;
continue;
}
if (listen(sock, 5) == -1)
fatal("listen");
/*
* for the time being, we're happy as soon as
* something binds.
*/
break;
}
if (sock == -1)
fatal("%s", cause);
freeaddrinfo(res0);
2023-06-08 18:22:03 +02:00
event_init();
/* cheating */
2023-06-09 19:18:04 +02:00
conf->sock4 = sock;
event_set(&conf->evsock4, conf->sock4, EV_READ|EV_PERSIST,
do_accept, conf);
2023-06-08 18:22:03 +02:00
server_init(NULL, NULL, NULL);
2023-06-09 19:18:04 +02:00
if (server_configure_done(conf) == -1)
2023-06-08 18:22:03 +02:00
fatalx("server configuration failed");
2023-06-06 13:46:40 +02:00
log_info("serving %s on port %d", dir, port);
2023-06-08 18:22:03 +02:00
event_dispatch();
log_info("quitting");
return 0;
2022-09-07 22:47:33 +02:00
}
static __dead void
usage(void)
{
fprintf(stderr,
"Version: " GE_STRING "\n"
2022-09-07 22:47:33 +02:00
"Usage: %s [-hVv] [-d certs-dir] [-H hostname] [-p port] [dir]\n",
getprogname());
exit(1);
}
int
main(int argc, char **argv)
{
2023-06-09 19:18:04 +02:00
struct conf *conf;
2022-09-07 22:47:33 +02:00
struct vhost *host;
struct location *loc;
const char *errstr, *certs_dir = NULL, *hostname = "localhost";
char path[PATH_MAX];
int ch;
2023-06-06 10:11:30 +02:00
setlocale(LC_CTYPE, "");
2023-06-06 13:46:40 +02:00
log_init(1, LOG_DAEMON);
log_setverbose(0);
2023-06-09 19:18:04 +02:00
conf = config_new();
2022-09-07 22:47:33 +02:00
/* ge doesn't do privsep so no privsep crypto engine. */
conf->use_privsep_crypto = 0;
2022-09-07 22:47:33 +02:00
while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) {
switch (ch) {
case 'd':
certs_dir = optarg;
break;
case 'H':
hostname = optarg;
break;
case 'h':
usage();
break;
case 'p':
2023-06-09 19:18:04 +02:00
conf->port = strtonum(optarg, 0, UINT16_MAX, &errstr);
2022-09-07 22:47:33 +02:00
if (errstr)
fatalx("port number is %s: %s", errstr,
optarg);
2022-09-07 22:47:33 +02:00
break;
case 'V':
puts("Version: " GE_STRING);
2022-09-07 22:47:33 +02:00
return 0;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (argc > 1)
usage();
/* prepare the configuration */
2023-06-09 19:18:04 +02:00
init_mime(&conf->mime);
2022-09-07 22:47:33 +02:00
if (certs_dir == NULL)
certs_dir = data_dir();
/* set up the implicit vhost and location */
host = xcalloc(1, sizeof(*host));
2023-06-09 19:18:04 +02:00
TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
2022-09-07 22:47:33 +02:00
loc = xcalloc(1, sizeof(*loc));
loc->fcgi = -1;
TAILQ_INSERT_HEAD(&host->locations, loc, locations);
load_local_cert(host, hostname, certs_dir);
strlcpy(host->domain, "*", sizeof(host->domain));
2022-09-07 22:47:33 +02:00
loc->auto_index = 1;
strlcpy(loc->match, "*", sizeof(loc->match));
2022-09-07 22:47:33 +02:00
if (*argv == NULL) {
if (getcwd(path, sizeof(path)) == NULL)
fatal("getcwd");
strlcpy(loc->dir, path, sizeof(loc->dir));
} else {
char *tmp;
tmp = absolutify_path(*argv);
strlcpy(loc->dir, tmp, sizeof(loc->dir));
free(tmp);
}
2022-09-07 22:47:33 +02:00
/* start the server */
signal(SIGPIPE, SIG_IGN);
setproctitle("%s", loc->dir);
2023-06-09 19:18:04 +02:00
return serve(conf, hostname, conf->port, loc->dir);
2022-09-07 22:47:33 +02:00
}