diff --git a/fcgi.c b/fcgi.c index 2d1dc86..502b994 100644 --- a/fcgi.c +++ b/fcgi.c @@ -20,6 +20,8 @@ #include #include +#include "log.h" + /* * Sometimes it can be useful to inspect the fastcgi traffic as * received by gmid. @@ -355,8 +357,7 @@ fcgi_req(struct client *c) 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"); diff --git a/ge.c b/ge.c index 5681ee2..4ccbbdc 100644 --- a/ge.c +++ b/ge.c @@ -27,6 +27,8 @@ #include #include +#include "log.h" + struct imsgbuf ibuf, logibuf; struct conf conf; @@ -86,7 +88,7 @@ mkdirs(const char *path, mode_t mode) 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 */ @@ -150,7 +152,7 @@ serve(const char *host, int port, const char *dir, struct tls *ctx) 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); @@ -223,7 +225,8 @@ main(int argc, char **argv) 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); @@ -276,21 +279,21 @@ main(int argc, char **argv) /* 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); diff --git a/gmid.c b/gmid.c index a79ad60..c7f3ed3 100644 --- a/gmid.c +++ b/gmid.c @@ -28,6 +28,8 @@ #include #include +#include "log.h" + static const char *opts = "D:df:hnP:Vv"; static const struct option longopts[] = { @@ -92,23 +94,23 @@ make_socket(int port, int family) } 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; } @@ -118,13 +120,14 @@ add_keypair(struct vhost *h) { 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)); } } @@ -141,7 +144,8 @@ setup_tls(void) 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"); @@ -150,20 +154,20 @@ setup_tls(void) /* 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 @@ -255,16 +259,16 @@ drop_priv(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) { @@ -325,11 +329,11 @@ serve(void) 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]); @@ -352,7 +356,7 @@ write_pidfile(const char *pidfile) 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; @@ -360,10 +364,10 @@ write_pidfile(const char *pidfile) 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()); @@ -383,7 +387,7 @@ main(int argc, char **argv) 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': @@ -433,7 +437,7 @@ main(int argc, char **argv) imsg_flush(&logibuf); if (daemon(1, 1) == -1) - fatal("daemon: %s", strerror(errno)); + fatal("daemon"); } sock4 = make_socket(conf.port, AF_INET); diff --git a/gmid.h b/gmid.h index a0a44a3..e2a0128 100644 --- a/gmid.h +++ b/gmid.h @@ -308,18 +308,6 @@ void print_conf(void); 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*); diff --git a/log.c b/log.c index 049c1ca..2b84224 100644 --- a/log.c +++ b/log.c @@ -30,6 +30,8 @@ #include #include +#include "log.h" + static struct event imsgev; static FILE *log; @@ -85,26 +87,56 @@ send_log(int type, int priority, const char *msg, size_t len) 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; + + err = errno; + + 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); - if ((r = vasprintf(&fmted, fmt, ap)) != -1) { - send_log(IMSG_LOG, LOG_CRIT, fmted, r+1); - free(fmted); - - /* wait for the logger process to shut down */ - pfd.fd = logibuf.fd; - pfd.events = POLLIN; - poll(&pfd, 1, 1000); - } + 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); - exit(1); } static inline void @@ -126,8 +158,7 @@ vlog(int priority, struct client *c, 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) @@ -224,7 +255,7 @@ log_request(struct client *c, char *meta, size_t l) 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 */ diff --git a/log.h b/log.h new file mode 100644 index 0000000..842bbe5 --- /dev/null +++ b/log.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Omar Polo + * + * 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 *); diff --git a/mime.c b/mime.c index 1586cf8..9197c8b 100644 --- a/mime.c +++ b/mime.c @@ -20,6 +20,8 @@ #include #include +#include "log.h" + void init_mime(struct mime *mime) { @@ -28,7 +30,7 @@ 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 */ diff --git a/parse.y b/parse.y index f4d770d..a7a17fc 100644 --- a/parse.y +++ b/parse.y @@ -33,6 +33,8 @@ #include #include +#include "log.h" + TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); static struct file { TAILQ_ENTRY(file) entry; diff --git a/proxy.c b/proxy.c index 0ac83d6..3e5fa34 100644 --- a/proxy.c +++ b/proxy.c @@ -20,6 +20,8 @@ #include #include +#include "log.h" + #define MIN(a, b) ((a) < (b) ? (a) : (b)) static const struct timeval handshake_timeout = { 5, 0 }; @@ -240,7 +242,7 @@ proxy_enqueue_req(struct client *c) 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, diff --git a/sandbox.c b/sandbox.c index 1e95b8e..d0d05b1 100644 --- a/sandbox.c +++ b/sandbox.c @@ -15,6 +15,7 @@ */ #include "gmid.h" +#include "log.h" #if defined(__OpenBSD__) diff --git a/server.c b/server.c index 083b5c2..3373182 100644 --- a/server.c +++ b/server.c @@ -28,6 +28,8 @@ #include #include +#include "log.h" + #define MIN(a, b) ((a) < (b) ? (a) : (b)) int shutting_down; @@ -367,9 +369,9 @@ mark_nonblock(int fd) 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 @@ -399,8 +401,7 @@ handle_handshake(int fd, short ev, void *d) 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); @@ -504,7 +505,7 @@ fmt_sbuf(const char *fmt, struct client *c, const char *path) strlcat(c->sbuf, c->domain, sizeof(c->sbuf)); break; default: - fatal("%s: unknown fmt specifier %c", + fatalx("%s: unknown fmt specifier %c", __func__, *fmt); } } @@ -1283,7 +1284,7 @@ do_accept(int sock, short et, void *d) if (errno == EWOULDBLOCK || errno == EAGAIN || errno == ECONNABORTED) return; - fatal("accept: %s", strerror(errno)); + fatal("accept"); } mark_nonblock(fd); @@ -1329,7 +1330,7 @@ handle_dispatch_imsg(int fd, short ev, void *d) } if (n == 0) - fatal("connection closed."); /* XXX: fatalx */ + fatalx("connection closed."); for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) @@ -1354,8 +1355,7 @@ handle_dispatch_imsg(int fd, short ev, void *d) signal_del(&sigusr2); break; default: - /* XXX: fatalx */ - fatal("Unknown message %d", imsg.hdr.type); + fatalx("Unknown message %d", imsg.hdr.type); } imsg_free(&imsg); } @@ -1416,8 +1416,8 @@ load_vhosts(void) 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); } } } @@ -1427,7 +1427,7 @@ server_main(struct tls *ctx_, struct imsgbuf *ibuf, int sock4, int sock6) { 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); diff --git a/utils.c b/utils.c index f82e6ce..7f9e74b 100644 --- a/utils.c +++ b/utils.c @@ -24,6 +24,8 @@ #include #include +#include "log.h" + int starts_with(const char *str, const char *prefix) { @@ -122,23 +124,23 @@ gen_certificate(const char *hostname, const char *certpath, const char *keypath) 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); @@ -146,26 +148,26 @@ gen_certificate(const char *hostname, const char *certpath, const char *keypath) 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);