commit - a01a91db06a943ef0cc8fbb7294786814a63b65c
commit + df5058c919cbd1538d0a04cb2a4c179c0291566f
blob - 2d1dc86cd84eeabfdb308272bb45fb5b97b0f597
blob + 502b994aa5a45f857935587e6708d3e31dd65196
--- fcgi.c
+++ fcgi.c
#include <assert.h>
#include <errno.h>
#include <string.h>
+
+#include "log.h"
/*
* Sometimes it can be useful to inspect the fastcgi traffic as
NULL, 0,
NI_NUMERICHOST);
if (e != 0)
- fatal("getnameinfo failed: %s (%s)",
- gai_strerror(e), strerror(errno));
+ fatalx("getnameinfo failed: %s", gai_strerror(e));
fcgi_begin_request(c->cgibev);
fcgi_send_param(c->cgibev, "GATEWAY_INTERFACE", "CGI/1.1");
blob - 5681ee2d5b65838617fc49d0275cd8bd132d7774
blob + 4ccbbdc0cd482bea2364d9b2181d0399b12eb14d
--- ge.c
+++ ge.c
#include <string.h>
#include <unistd.h>
+#include "log.h"
+
struct imsgbuf ibuf, logibuf;
struct conf conf;
return;
mkdirs(dname, mode);
if (mkdir(path, mode) != 0 && errno != EEXIST)
- fatal("can't mkdir %s: %s", path, strerror(errno));
+ fatal("can't mkdir %s", path);
}
/* $XDG_DATA_HOME/gmid */
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(host, service, &hints, &res0);
if (error)
- fatal("%s", gai_strerror(error));
+ fatalx("%s", gai_strerror(error));
for (res = res0; res; res = res->ai_next) {
sock = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
case 'p':
conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr);
if (errstr)
- fatal("port number is %s: %s", errstr, optarg);
+ fatalx("port number is %s: %s", errstr,
+ optarg);
break;
case 'V':
puts("Version: " GE_STRING);
/* setup tls */
if ((tlsconf = tls_config_new()) == NULL)
- fatal("tls_config_new"); /* XXX: fatalx */
+ fatal("tls_config_new");
/* optionally accept client certs but don't try to verify them */
tls_config_verify_client_optional(tlsconf);
tls_config_insecure_noverifycert(tlsconf);
if ((ctx = tls_server()) == NULL)
- fatal("tls_server failure"); /* XXX: fatalx */
+ fatal("tls_server failure");
if (tls_config_set_keypair_file(tlsconf, host->cert, host->key))
- fatal("can't load the keypair (%s, %s)",
- host->cert, host->key);
+ fatalx("can't load the keypair (%s, %s): %s",
+ host->cert, host->key, tls_config_error(tlsconf));
if (tls_configure(ctx, tlsconf) == -1)
- fatal("tls_configure: %s", tls_error(ctx));
+ fatalx("tls_configure: %s", tls_error(ctx));
/* start the server */
signal(SIGPIPE, SIG_IGN);
blob - a79ad60f00bc40923cf88bb881e8514d66183a59
blob + c7f3ed30ad9bd89825f289194b305268e8c706b2
--- gmid.c
+++ gmid.c
#include <signal.h>
#include <string.h>
+#include "log.h"
+
static const char *opts = "D:df:hnP:Vv";
static const struct option longopts[] = {
}
if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
- fatal("socket: %s", strerror(errno));
+ fatal("socket");
v = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
- fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
+ fatal("setsockopt(SO_REUSEADDR)");
v = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
- fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
+ fatal("setsockopt(SO_REUSEPORT)");
mark_nonblock(sock);
if (bind(sock, addr, len) == -1)
- fatal("bind: %s", strerror(errno));
+ fatal("bind");
if (listen(sock, 16) == -1)
- fatal("listen: %s", strerror(errno));
+ fatal("listen");
return sock;
}
{
if (*h->ocsp == '\0') {
if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
- fatal("failed to load the keypair (%s, %s)",
- h->cert, h->key);
+ fatalx("failed to load the keypair (%s, %s): %s",
+ h->cert, h->key, tls_config_error(tlsconf));
} else {
if (tls_config_add_keypair_ocsp_file(tlsconf, h->cert, h->key,
h->ocsp) == -1)
- fatal("failed to load the keypair (%s, %s, %s)",
- h->cert, h->key, h->ocsp);
+ fatalx("failed to load the keypair (%s, %s, %s): %s",
+ h->cert, h->key, h->ocsp,
+ tls_config_error(tlsconf));
}
}
tls_config_insecure_noverifycert(tlsconf);
if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
- fatal("tls_config_set_protocols");
+ fatalx("tls_config_set_protocols: %s",
+ tls_config_error(tlsconf));
if ((ctx = tls_server()) == NULL)
fatal("tls_server failure");
/* we need to set something, then we can add how many key we want */
if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
- fatal("tls_config_set_keypair_file failed for (%s, %s)",
- h->cert, h->key);
+ fatalx("tls_config_set_keypair_file failed for (%s, %s): %s",
+ h->cert, h->key, tls_config_error(tlsconf));
/* same for OCSP */
if (*h->ocsp != '\0' &&
tls_config_set_ocsp_staple_file(tlsconf, h->ocsp) == -1)
- fatal("tls_config_set_ocsp_staple_file failed for (%s)",
- h->ocsp);
+ fatalx("tls_config_set_ocsp_staple_file failed for (%s): %s",
+ h->ocsp, tls_config_error(tlsconf));
while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
add_keypair(h);
if (tls_configure(ctx, tlsconf) == -1)
- fatal("tls_configure: %s", tls_error(ctx));
+ fatalx("tls_configure: %s", tls_error(ctx));
}
void
struct passwd *pw = NULL;
if (*conf.chroot != '\0' && *conf.user == '\0')
- fatal("can't chroot without an user to switch to after.");
+ fatalx("can't chroot without an user to switch to after.");
if (*conf.user != '\0') {
if ((pw = getpwnam(conf.user)) == NULL)
- fatal("can't find user %s", conf.user);
+ fatalx("can't find user %s", conf.user);
}
if (*conf.chroot != '\0') {
if (chroot(conf.chroot) != 0 || chdir("/") != 0)
- fatal("%s: %s", conf.chroot, strerror(errno));
+ fatal("%s", conf.chroot);
}
if (pw != NULL) {
for (i = 0; i < conf.prefork; ++i) {
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
PF_UNSPEC, p) == -1)
- fatal("socketpair: %s", strerror(errno));
+ fatal("socketpair");
switch (fork()) {
case -1:
- fatal("fork: %s", strerror(errno));
+ fatal("fork");
case 0: /* child */
close(p[0]);
imsg_init(&servibuf[i], p[1]);
return -1;
if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
- fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
+ fatal("can't open pidfile %s", pidfile);
lock.l_start = 0;
lock.l_len = 0;
lock.l_whence = SEEK_SET;
if (fcntl(fd, F_SETLK, &lock) == -1)
- fatal("can't lock %s, gmid is already running?", pidfile);
+ fatalx("can't lock %s, gmid is already running?", pidfile);
if (ftruncate(fd, 0) == -1)
- fatal("ftruncate: %s: %s", pidfile, strerror(errno));
+ fatal("ftruncate %s", pidfile);
dprintf(fd, "%d\n", getpid());
switch (ch) {
case 'D':
if (cmdline_symset(optarg) == -1)
- fatal("could not parse macro definition: %s",
+ fatalx("could not parse macro definition: %s",
optarg);
break;
case 'd':
imsg_flush(&logibuf);
if (daemon(1, 1) == -1)
- fatal("daemon: %s", strerror(errno));
+ fatal("daemon");
}
sock4 = make_socket(conf.port, AF_INET);
blob - a0a44a38910a6ae12758345757b62f32b298041f
blob + e2a0128c08d8ba4f9708c9b559097668f0775c0d
--- gmid.h
+++ gmid.h
int cmdline_symset(char *);
/* log.c */
-void fatal(const char*, ...)
- __attribute__((format (printf, 1, 2)))
- __attribute__((__noreturn__));
-
-#define LOG_ATTR_FMT __attribute__((format (printf, 2, 3)))
-void log_err(struct client*, const char*, ...) LOG_ATTR_FMT;
-void log_warn(struct client*, const char*, ...) LOG_ATTR_FMT;
-void log_notice(struct client*, const char*, ...) LOG_ATTR_FMT;
-void log_info(struct client*, const char*, ...) LOG_ATTR_FMT;
-void log_debug(struct client*, const char*, ...) LOG_ATTR_FMT;
-void log_request(struct client*, char*, size_t);
-int logger_main(int, struct imsgbuf*);
/* mime.c */
void init_mime(struct mime*);
blob - 049c1cab5c75014495d1a085ffcc3f51cc8a0f3b
blob + 2b842240c42dbf2d5835ae49426ca89c3c509a0c
--- log.c
+++ log.c
#include <syslog.h>
#include <time.h>
+#include "log.h"
+
static struct event imsgev;
static FILE *log;
imsg_flush(&logibuf);
}
-void
-fatal(const char *fmt, ...)
+static __dead void
+fatal_impl(int use_err, const char *fmt, va_list ap)
{
struct pollfd pfd;
- va_list ap;
- int r;
- char *fmted;
+ char *str, *tmp;
+ int r, t, err;
- va_start(ap, fmt);
- if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
- send_log(IMSG_LOG, LOG_CRIT, fmted, r+1);
- free(fmted);
+ err = errno;
- /* wait for the logger process to shut down */
- pfd.fd = logibuf.fd;
- pfd.events = POLLIN;
- poll(&pfd, 1, 1000);
- }
- va_end(ap);
+ if ((r = vasprintf(&str, fmt, ap)) != -1) {
+ if (use_err &&
+ (t = asprintf(&tmp, "%s: %s", str, strerror(err))) !=
+ -1) {
+ free(str);
+ str = tmp;
+ r = t;
+ }
+ } else
+ str = NULL, r = 0;
+
+ send_log(IMSG_LOG, LOG_CRIT, str, r);
+ free(str);
+
+ /* wait for the logger process to shut down */
+ memset(&pfd, 0, sizeof(pfd));
+ pfd.fd = logibuf.fd;
+ pfd.events = POLLIN;
+ poll(&pfd, 1, 1000);
+
exit(1);
}
+void __dead
+fatal(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ fatal_impl(1, fmt, ap);
+ va_end(ap);
+}
+
+void __dead
+fatalx(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ fatal_impl(0, fmt, ap);
+ va_end(ap);
+}
+
static inline void
vlog(int priority, struct client *c,
const char *fmt, va_list ap)
sbuf, sizeof(sbuf),
NI_NUMERICHOST | NI_NUMERICSERV);
if (ec != 0)
- fatal("getnameinfo: %s: %s",
- gai_strerror(ec), strerror(errno));
+ fatal("getnameinfo: %s", gai_strerror(ec));
}
if (vasprintf(&fmted, fmt, ap) == -1)
sbuf, sizeof(sbuf),
NI_NUMERICHOST | NI_NUMERICSERV);
if (ec != 0)
- fatal("getnameinfo: %s", gai_strerror(ec));
+ fatalx("getnameinfo: %s", gai_strerror(ec));
if (c->iri.schema != NULL) {
/* serialize the IRI */
blob - 1586cf8944d2df4b4ac5f54a3c5aad03db82f13a
blob + 9197c8b131e1e9c670c9d7bf13635eaa8b9b1919
--- mime.c
+++ mime.c
#include <stdlib.h>
#include <string.h>
+#include "log.h"
+
void
init_mime(struct mime *mime)
{
mime->t = calloc(mime->cap, sizeof(struct etm));
if (mime->t == NULL)
- fatal("calloc: %s", strerror(errno));
+ fatal("calloc");
}
/* register mime for the given extension */
blob - /dev/null
blob + 842bbe5d1c365030f23efd658e2ed36ce8f00d53 (mode 644)
--- /dev/null
+++ log.h
+/*
+ * Copyright (c) 2023 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.
+ */
+
+void fatal(const char *, ...)
+ __attribute__((format (printf, 1, 2)))
+ __attribute__((__noreturn__));
+void fatalx(const char *, ...)
+ __attribute__((format (printf, 1, 2)))
+ __attribute__((__noreturn__));
+
+#define LOG_ATTR_FMT __attribute__((format (printf, 2, 3)))
+void log_err(struct client *, const char *, ...) LOG_ATTR_FMT;
+void log_warn(struct client *, const char *, ...) LOG_ATTR_FMT;
+void log_warnx(struct client *, const char *, ...) LOG_ATTR_FMT;
+void log_notice(struct client *, const char *, ...) LOG_ATTR_FMT;
+void log_info(struct client *, const char *, ...) LOG_ATTR_FMT;
+void log_debug(struct client *, const char *, ...) LOG_ATTR_FMT;
+void log_request(struct client *, char *, size_t);
+int logger_main(int, struct imsgbuf *);
blob - f4d770d69c1a08115598847a8480881c29d9badd
blob + a7a17fc07f058fb415d8c078ab09a35159c11406
--- parse.y
+++ parse.y
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+
+#include "log.h"
TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
static struct file {
blob - 0ac83d6ea4ec78bc22aa8787d5c9551ff2dba7d9
blob + 3e5fa347a81fa6b0904dcbb64ae65169eec1d971
--- proxy.c
+++ proxy.c
#include <ctype.h>
#include <errno.h>
#include <string.h>
+
+#include "log.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write,
proxy_error, c);
if (c->proxybev == NULL)
- fatal("can't allocate bufferevent: %s", strerror(errno));
+ fatal("can't allocate bufferevent");
if (!p->notls) {
event_set(&c->proxybev->ev_read, c->pfd, EV_READ,
blob - 1e95b8e6d6ea2b8422d7d24ca72dc41b35c2792f
blob + d0d05b1dfd036d3fcbed464ed83a602baa48dba1
--- sandbox.c
+++ sandbox.c
*/
#include "gmid.h"
+#include "log.h"
#if defined(__OpenBSD__)
blob - 083b5c2767cd31ffcd2a93844e3a0c04bd76da14
blob + 337318272840745460077e5075f3ba1e0acb5df6
--- server.c
+++ server.c
#include <limits.h>
#include <string.h>
+#include "log.h"
+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int shutting_down;
int flags;
if ((flags = fcntl(fd, F_GETFL)) == -1)
- fatal("fcntl(F_GETFL): %s", strerror(errno));
+ fatal("fcntl(F_GETFL)");
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
- fatal("fcntl(F_SETFL): %s", strerror(errno));
+ fatal("fcntl(F_SETFL)");
}
static void
c->bev = bufferevent_new(fd, client_read, client_write,
client_error, c);
if (c->bev == NULL)
- fatal("%s: failed to allocate client buffer: %s",
- __func__, strerror(errno));
+ fatal("%s: failed to allocate client buffer", __func__);
event_set(&c->bev->ev_read, c->fd, EV_READ,
client_tls_readcb, c->bev);
strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
break;
default:
- fatal("%s: unknown fmt specifier %c",
+ fatalx("%s: unknown fmt specifier %c",
__func__, *fmt);
}
}
if (errno == EWOULDBLOCK || errno == EAGAIN ||
errno == ECONNABORTED)
return;
- fatal("accept: %s", strerror(errno));
+ fatal("accept");
}
mark_nonblock(fd);
}
if (n == 0)
- fatal("connection closed."); /* XXX: fatalx */
+ fatalx("connection closed.");
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
signal_del(&sigusr2);
break;
default:
- /* XXX: fatalx */
- fatal("Unknown message %d", imsg.hdr.type);
+ fatalx("Unknown message %d", imsg.hdr.type);
}
imsg_free(&imsg);
}
continue;
l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY);
if (l->dirfd == -1)
- fatal("open %s for domain %s: %s", l->dir,
- h->domain, strerror(errno));
+ fatal("open %s for domain %s", l->dir,
+ h->domain);
}
}
}
{
drop_priv();
if (load_default_mime(&conf.mime) == -1)
- fatal("can't load default mime: %s", strerror(errno));
+ fatal("can't load default mime");
sort_mime(&conf.mime);
load_vhosts();
loop(ctx_, sock4, sock6, ibuf);
blob - f82e6ce0c2b9c46daa9335e06ec7a53a03742b14
blob + 7f9e74b0363de49ec1a83d74b08575c5fb0cdfe8
--- utils.c
+++ utils.c
#include <openssl/x509_vfy.h>
#include <openssl/x509v3.h>
+#include "log.h"
+
int
starts_with(const char *str, const char *prefix)
{
host);
if ((pkey = EVP_PKEY_new()) == NULL)
- fatal("couldn't create a new private key");
+ fatalx("couldn't create a new private key");
if ((rsa = RSA_new()) == NULL)
- fatal("couldn't generate rsa");
+ fatalx("couldn't generate rsa");
if ((e = BN_new()) == NULL)
- fatal("couldn't allocate a bignum");
+ fatalx("couldn't allocate a bignum");
BN_set_word(e, RSA_F4);
if (!RSA_generate_key_ex(rsa, 4096, e, NULL))
- fatal("couldn't generate a rsa key");
+ fatalx("couldn't generate a rsa key");
if (!EVP_PKEY_assign_RSA(pkey, rsa))
- fatal("couldn't assign the key");
+ fatalx("couldn't assign the key");
if ((x509 = X509_new()) == NULL)
- fatal("couldn't generate the X509 certificate");
+ fatalx("couldn't generate the X509 certificate");
ASN1_INTEGER_set(X509_get_serialNumber(x509), 0);
X509_gmtime_adj(X509_get_notBefore(x509), 0);
X509_set_version(x509, 3);
if (!X509_set_pubkey(x509, pkey))
- fatal("couldn't set the public key");
+ fatalx("couldn't set the public key");
name = X509_get_subject_name(x509);
if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, host, -1, -1, 0))
- fatal("couldn't add CN to cert");
+ fatalx("couldn't add CN to cert");
X509_set_issuer_name(x509, name);
if (!X509_sign(x509, pkey, EVP_sha256()))
- fatal("couldn't sign the certificate");
+ fatalx("couldn't sign the certificate");
if ((f = fopen(keypath, "w")) == NULL)
- fatal("fopen(%s): %s", keypath, strerror(errno));
+ fatal("can't open %s", keypath);
if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL))
- fatal("couldn't write private key");
+ fatalx("couldn't write private key");
fclose(f);
if ((f = fopen(certpath, "w")) == NULL)
- fatal("fopen(%s): %s", certpath, strerror(errno));
+ fatal("can't open %s", certpath);
if (!PEM_write_X509(f, x509))
- fatal("couldn't write cert");
+ fatalx("couldn't write cert");
fclose(f);
BN_free(e);