commit c74c6691dc039dde5b1e331ff94da6e8ea2bbf41 from: Tracey Emery via: Tracey Emery date: Sun Nov 8 15:39:43 2026 UTC Refactor thingsd for modern imsg, removing proc abstraction layer. - Extend copyright notices to 2026 and update author email domain. - Replace legacy proc‑based messaging with imsg event helpers (imsgev, imsg_compose_event, etc.). - Remove obsolete proc headers and related code paths across client, socket, and configuration modules. - Simplify TLS initialization: drop unused keypair addition, tighten error handling, and ensure proper memory checks. - Adjust socket creation signatures to use sockaddr_storage directly. - Introduce one‑shot timeout constant and improve client subscription handling. - Update documentation files (README, man pages) to reflect OpenBSD 7.7 requirement and corrected parity options. - Clean up dead code, improve naming, and add missing initializations for new structures. These changes modernize the codebase, fix several bugs related to TLS handshakes and client management. commit - 70b4b935ca57bbd9ba5ea767d759579b251a6b67 commit + c74c6691dc039dde5b1e331ff94da6e8ea2bbf41 blob - ac610607f059527ced5fe5d2b1521639694c5289 blob + f3e4c7bfdc6d3687dfb2360558afaf5b2e078e62 --- LICENSE.md +++ LICENSE.md @@ -1,4 +1,4 @@ -Copyright (c) 2019 Tracey Emery +Copyright (c) 2019-2026 Tracey Emery Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above blob - aa6ac9837614cafc73330204ff0b94337a054544 blob + 9143e85aa097b5693ab7c25b0cb24a1cd20541ff --- README.md +++ README.md @@ -21,7 +21,7 @@ about packet data. Prerequisites ------------- -* OpenBSD 6.4 or higher +* OpenBSD 7.7 or higher Files ----- @@ -41,7 +41,7 @@ Compile and install. make doas make install - doas addgroup _thingsd + doas groupadd _thingsd doas useradd -d /var/empty -L daemon -g _thingsd -s /sbin/nologin _thingsd doas cp examples/etc/rc.d/thingsd /etc/rc.d/ doas chown _thingsd /dev/[thingsd.conf devices] blob - 28a54ea56f7d60965a2f1844dcc18046820f15b8 blob + 7925a64cc6e7a6878bfece959c24e3cdab257e64 --- thingsctl/parser.c +++ thingsctl/parser.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020-2021 Tracey Emery + * Copyright (c) 2019, 2020-2026 Tracey Emery * Copyright (c) 2004 Esben Norby * Copyright (c) 2003, 2004 Henning Brauer * @@ -31,7 +31,6 @@ #include #include -#include "proc.h" #include "thingsd.h" #include "parser.h" @@ -177,9 +176,12 @@ match_token(const char *word, const struct token *tabl case CLIENTNAME: case SOCKETNAME: if (!match && word != NULL && strlen(word) > 0) { - memset(res->name, 0, - sizeof(res->name)); - memcpy(&res->name, word, sizeof(res->name)); + if (strlcpy(res->name, word, + sizeof(res->name)) >= sizeof(res->name)) { + fprintf(stderr, "name too long: %s\n", + word); + return (NULL); + } match++; t = &table[i]; if (t->value) blob - 9653c711f3fe8d54f5ff0c3bbc65424e2710e0e5 blob + 03e6b1e94b5193200b97861e49d591ae6864fbd5 --- thingsctl/parser.h +++ thingsctl/parser.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020-2021 Tracey Emery + * Copyright (c) 2019, 2020-2021 Tracey Emery * Copyright (c) 2004 Esben Norby * Copyright (c) 2003, 2004 Henning Brauer * blob - 163c4341b7e9445c742f1acf71e5391c6f176492 blob + 9390f3d8d4e77b68d18690320d6758459097005d --- thingsctl/thingsctl.8 +++ thingsctl/thingsctl.8 @@ -1,5 +1,5 @@ .\" -.\" Copyright (c) 2016-2019 Tracey Emery +.\" Copyright (c) 2016-2019 Tracey Emery .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above blob - fc82ae778f297bd56c6fea165bbf6365cb2bc28b blob + a3372f5c0dd0469103b07f87f089faa52f7877af --- thingsctl/thingsctl.c +++ thingsctl/thingsctl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020-2021 Tracey Emery + * Copyright (c) 2019, 2020-2026 Tracey Emery * Copyright (c) 2005 Claudio Jeker * Copyright (c) 2004, 2005 Esben Norby * Copyright (c) 2003 Henning Brauer @@ -36,7 +36,6 @@ #include #include -#include "proc.h" #include "thingsd.h" #include "parser.h" @@ -95,7 +94,9 @@ main(int argc, char *argv[]) memset(&sun, 0, sizeof(sun)); sun.sun_family = AF_UNIX; - memcpy(&sun.sun_path, sockname, sizeof(sun.sun_path)); + if (strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path)) >= + sizeof(sun.sun_path)) + errx(1, "socket path too long: %s", sockname); if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) err(1, "connect: %s", sockname); @@ -126,7 +127,7 @@ main(int argc, char *argv[]) switch (res->action) { case KILL_CLIENT: imsg_compose(ibuf, IMSG_KILL_CLIENT, 0, 0, -1, - res->name, strlen(res->name)); + res->name, sizeof(res->name)); printf("\nKill request sent for client '%s'.\n", res->name); done = 1; break; @@ -195,8 +196,8 @@ main(int argc, char *argv[]) errx(1, "imsg_read error"); if (n == 0) { if (res->action == SHOW_PACKETS) - errx(1, "Bad thing name request, or thingsd " \ - "shutdown.\n"); + errx(1, "Bad thing name request, or thingsd " + "shutdown."); else errx(1, "pipe closed"); } @@ -226,7 +227,9 @@ main(int argc, char *argv[]) done = 1; break; } - printf("%s\n", (char *)imsg.data); + printf("%.*s\n", + (int)(imsg.hdr.len - IMSG_HEADER_SIZE), + (char *)imsg.data); break; case SHOW_THINGSD: done = show_parent_msg(&imsg); @@ -328,7 +331,7 @@ list_things_msg(struct imsg *imsg) printf("\tParity:\t\t\t%s\n", nti->conf.parity); break; } - if (strncmp(nti->conf.tcp_iface, "", PKT_BUFF) == 0) + if (nti->conf.tcp_iface[0] == '\0') printf("\tBind Interface:\t\tall\n"); else printf("\tBind Interface:\t\t%s\n", blob - 18a8f5cc7ae4a5d0a1371eb62a70e606b9dfa0b3 blob + c14e674add016add99152a43dbcc68de8fc197e5 --- thingsd/Makefile +++ thingsd/Makefile @@ -1,6 +1,6 @@ PROG = thingsd SRCS = parse.y -SRCS += config.c control.c log.c thingsd.c proc.c +SRCS += config.c control.c log.c thingsd.c SRCS += sockets.c tls.c client.c MAN = thingsd.8 thingsd.conf.5 blob - 7cf3fdc8ae66434c30e781cf46428d9468615549 blob + ab5122d0fd8fa95d2d753f9c7db311ff9975d0a0 --- thingsd/client.c +++ thingsd/client.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery + * Copyright (c) 2016, 2019, 2020-2026 Tracey Emery * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,11 +27,9 @@ #include #include -#include "proc.h" #include "thingsd.h" -extern volatile int client_inflight; -extern enum privsep_procid privsep_process; +#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) void client_write_to_things(struct packages *); @@ -39,51 +37,38 @@ void client_del(struct client *client) { struct socket *sock = client->sock; - struct socket *psock = NULL, *csock = NULL; - struct client *tclient, *uclient; + struct package *package, *tpkg; - TAILQ_FOREACH_SAFE(tclient, sock->clients, entry, uclient) { - if (client->id == tclient->id) - TAILQ_REMOVE(sock->clients, client, entry); - } - log_debug("%s: disconnecting client %s (%d)", __func__, client->name, - client->fd); - if (client->tls) - tls_free(client->tls_ctx); + TAILQ_REMOVE(sock->clients, client, entry); - if (evtimer_initialized(&client->timeout)) - evtimer_del(&client->timeout); - if (client->subscribed == 0) - log_debug("%s: client disconnected", __func__); + log_debug("%s: unsubscribed client disconnected (%d)", __func__, + client->fd); else log_debug("client disconnected: %s", client->name); + if (evtimer_initialized(&client->timeout)) + evtimer_del(&client->timeout); + if (client->bev != NULL) bufferevent_free(client->bev); - client_inflight--; + if (client->tls) { + tls_close(client->tls_ctx); + tls_free(client->tls_ctx); + } - if (client->subscribed == 0) - goto done; - - /* account for parent/child client counts */ - if (sock->conf.child_id) { - TAILQ_FOREACH(csock, thingsd_env->sockets, entry) - if (csock->conf.id == sock->conf.child_id) - break; + if (client->packages != NULL) { + TAILQ_FOREACH_SAFE(package, client->packages, entry, tpkg) { + TAILQ_REMOVE(client->packages, package, entry); + free(package); + } + free(client->packages); } - if (sock->conf.parent_id) { - TAILQ_FOREACH(psock, thingsd_env->sockets, entry) - if (psock->conf.id == sock->conf.parent_id) - break; - } - sock->client_cnt--; - if (csock != NULL) - csock->client_cnt--; - if (psock != NULL) - psock->client_cnt--; -done: + + if (client->subscribed) + sock->client_cnt--; + close(client->fd); free(client); } @@ -92,106 +77,70 @@ void client_rd(struct bufferevent *bev, void *arg) { struct client *client = (struct client *)arg; - size_t len; - char *pkt = NULL, *npkt = NULL; - struct package *package = NULL; + struct evbuffer *evb = EVBUFFER_INPUT(bev); + struct package *package; + size_t len, n; + u_char *pkt; + len = EVBUFFER_LENGTH(evb); - client->evb = EVBUFFER_INPUT(bev); - len = EVBUFFER_LENGTH(client->evb); - if (client->subscribed == 0) { log_debug("%s: client connection established (%d)", __func__, client->fd); /* allow one shot at subscription so we don't get hammered */ - pkt = calloc(len, sizeof(*pkt)); - if (pkt == NULL) - return; - - npkt = calloc(len, sizeof(*npkt)); - if (npkt == NULL) { - free(pkt); - return; - } - bufferevent_disable(client->bev, EV_READ); - evbuffer_remove(client->evb, pkt, len); - /* peak into packet and ensure it's a subscription packet */ - if (pkt[0] == 0x7E && pkt[1] == 0x7E && pkt[2] == 0x7E) { + /* parse_buf() needs a NUL terminated buffer */ + if ((pkt = calloc(1, len + 1)) == NULL) { + evbuffer_drain(evb, len); + return; + } + evbuffer_remove(evb, pkt, len); + + if (len >= 3 && pkt[0] == 0x7E && pkt[1] == 0x7E && + pkt[2] == 0x7E) { log_debug("%s: subscription packet received", __func__); - memmove(npkt, pkt+3, len-3); - parse_buf(client, npkt, len-3); + parse_buf(client, pkt + 3); if (client->subscribed) { bufferevent_enable(client->bev, EV_READ | EV_WRITE); if (evtimer_initialized(&client->timeout)) - evtimer_del(&client->timeout); + evtimer_del(&client->timeout); } } - } else if (client->subscribed) { - /* write to things */ - if ((package = calloc(1, sizeof(*package))) == NULL) + free(pkt); + return; + } + + while ((len = EVBUFFER_LENGTH(evb)) > 0) { + if ((package = calloc(1, sizeof(*package))) == NULL) { + evbuffer_drain(evb, len); return; - evbuffer_remove(client->evb, package->pkt, len); + } + n = MINIMUM(len, sizeof(package->pkt)); + evbuffer_remove(evb, package->pkt, n); package->thing_id = client->thing_id; - package->len = len; + package->len = n; TAILQ_INSERT_TAIL(thingsd_env->packages, package, entry); - client_write_to_things(thingsd_env->packages); - evbuffer_drain(client->evb, len); } - free(pkt); - free(npkt); + client_write_to_things(thingsd_env->packages); } void client_write_to_things(struct packages *packages) { - struct privsep *ps = thingsd_env->thingsd_ps; - struct package *package, *tpkg, p; - unsigned int id, what; - int fd = -1, n, m; - struct iovec iov[6]; - size_t c; + struct package *package, *tpkg; TAILQ_FOREACH_SAFE(package, packages, entry, tpkg) { - for (id = 0; id < PROC_MAX; id++) { - what = ps->ps_what[id]; - - if ((what & CONFIG_SOCKS) == 0 || id == privsep_process) - continue; - - memcpy(&p, package, sizeof(p)); - - c = 0; - iov[c].iov_base = &p; - iov[c++].iov_len = sizeof(p); - if (id == PROC_PARENT) { - /* XXX imsg code will close the fd after 1st call */ - n = -1; - proc_range(ps, id, &n, &m); - for (n = 0; n < m; n++) { - /* send thing fd */ - if (proc_composev_imsg(ps, id, n, - IMSG_DIST_CLIENT_PACKAGE, -1, fd, - iov, c) != 0) { - log_warn("%s: failed to compose" - " IMSG_DIST_THING_PACKAGE" - " imsg", - __func__); - return; - } - if (proc_flush_imsg(ps, id, n) == -1) { - log_warn("%s: failed to flush " - "IMSG_DIST_CLIENT_PACKAGE " - "imsg", - __func__); - return; - } - } - } + if (imsg_compose_event(socks_iev_parent, + IMSG_DIST_CLIENT_PACKAGE, 0, 0, -1, package, + sizeof(*package)) == -1) { + log_warn("%s: failed to compose " + "IMSG_DIST_CLIENT_PACKAGE imsg", __func__); + return; } + TAILQ_REMOVE(packages, package, entry); free(package); } blob - 19cc2b7944e2e1ab2aa381ca7646b44f698218ae blob + 9e795c88908f44330fea214c5c8cc7b10b01a90d --- thingsd/config.c +++ thingsd/config.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021 Tracey Emery + * Copyright (c) 2020-2026 Tracey Emery * Copyright (c) 2015 Reyk Floeter * * Permission to use, copy, modify, and distribute this software for any @@ -37,11 +37,8 @@ #include #include -#include "proc.h" #include "thingsd.h" -extern enum privsep_procid privsep_process; - int config_setsocks_tls(struct thingsd *, struct socket *); static int config_settls(struct thingsd *, struct socket *, @@ -54,37 +51,23 @@ static int int config_init(struct thingsd *env) { - struct privsep *ps = env->thingsd_ps; - unsigned int what; + if ((env->things = calloc(1, sizeof(*env->things))) == NULL) + return (-1); + if ((env->sockets = calloc(1, sizeof(*env->sockets))) == NULL) + return (-1); + if ((env->packages = calloc(1, sizeof(*env->packages))) == NULL) + return (-1); + if ((env->dead_things = calloc(1, sizeof(*env->dead_things))) == NULL) + return (-1); + env->packet_clients = calloc(1, sizeof(*env->packet_clients)); + if (env->packet_clients == NULL) + return (-1); + TAILQ_INIT(env->things); + TAILQ_INIT(env->sockets); + TAILQ_INIT(env->packages); + TAILQ_INIT(env->dead_things); + TAILQ_INIT(env->packet_clients); - /* Global configuration. */ - if (privsep_process == PROC_PARENT) - env->prefork_socks = SOCKS_NUMPROC; - - ps->ps_what[PROC_PARENT] = CONFIG_ALL; - ps->ps_what[PROC_SOCKS] = CONFIG_SOCKS; - - /* Other configuration. */ - what = ps->ps_what[privsep_process]; - if (what & CONFIG_SOCKS) { - if ((env->things = calloc(1, sizeof(*env->things))) == NULL) - return (-1); - if ((env->sockets = calloc(1, sizeof(*env->sockets))) == NULL) - return (-1); - if ((env->packages = calloc(1, sizeof(*env->packages))) == NULL) - return (-1); - if ((env->dead_things = calloc(1, - sizeof(*env->dead_things))) == NULL) - return (-1); - env->packet_clients = calloc(1, sizeof(*env->packet_clients)); - if (env->packet_clients == NULL) - return (-1); - TAILQ_INIT(env->things); - TAILQ_INIT(env->sockets); - TAILQ_INIT(env->packages); - TAILQ_INIT(env->dead_things); - TAILQ_INIT(env->packet_clients); - } return (0); } @@ -92,9 +75,7 @@ int config_getcfg(struct thingsd *env, struct imsg *imsg) { /* nothing to do but tell parent configuration is done */ - if (privsep_process != PROC_PARENT) - proc_compose(env->thingsd_ps, PROC_PARENT, - IMSG_CFG_DONE, NULL, 0); + imsg_compose_event(socks_iev_parent, IMSG_CFG_DONE, 0, 0, -1, NULL, 0); return (0); } @@ -114,13 +95,45 @@ config_getsocks(struct thingsd *env, struct imsg *imsg return (-1); } - /* create a new socket */ if ((sock = calloc(1, sizeof(*sock))) == NULL) return (-1); + if ((sock->clients = calloc(1, sizeof(*sock->clients))) == NULL) { + free(sock); + return (-1); + } + TAILQ_INIT(sock->clients); memcpy(&sock->conf, &sock_conf, sizeof(sock->conf)); - sock->fd = imsg_get_fd(imsg); + /* + * The pointers in the config belong to the parent's address space. + * TLS material arrives separately via IMSG_CFG_TLS, the file names + * are only ever used by the parent. + */ + sock->conf.tls_cert = NULL; + sock->conf.tls_cert_file = NULL; + sock->conf.tls_cert_len = 0; + sock->conf.tls_key = NULL; + sock->conf.tls_key_file = NULL; + sock->conf.tls_key_len = 0; + sock->conf.tls_ca = NULL; + sock->conf.tls_ca_file = NULL; + sock->conf.tls_ca_len = 0; + sock->conf.tls_crl = NULL; + sock->conf.tls_crl_file = NULL; + sock->conf.tls_crl_len = 0; + sock->conf.tls_ocsp_staple = NULL; + sock->conf.tls_ocsp_staple_file = NULL; + sock->conf.tls_ocsp_staple_len = 0; + + if ((sock->fd = imsg_get_fd(imsg)) == -1) { + log_warnx("%s: listener %d arrived without a descriptor", + __func__, sock->conf.id); + free(sock->clients); + free(sock); + return (-1); + } + TAILQ_INSERT_TAIL(env->sockets, sock, entry); return (0); @@ -129,58 +142,44 @@ config_getsocks(struct thingsd *env, struct imsg *imsg int config_setsocks(struct thingsd *env, struct socket *sock) { - struct privsep *ps = env->thingsd_ps; struct socket_config u; - int fd = -1, n, m; - struct iovec iov[6]; - size_t c; - unsigned int id, what; + int fd, n, ret; /* setup socket in priv process */ if (sockets_privinit(sock) == -1) return (-1); - for (id = 0; id < PROC_MAX; id++) { - what = ps->ps_what[id]; + memcpy(&u, &sock->conf, sizeof(u)); - if ((what & CONFIG_SOCKS) == 0 || id == privsep_process) - continue; - - memcpy(&u, &sock->conf, sizeof(u)); - - c = 0; - iov[c].iov_base = &u; - iov[c++].iov_len = sizeof(u); - if (id == PROC_SOCKS) { - /* XXX imsg code will close the fd after 1st call */ - n = -1; - proc_range(ps, id, &n, &m); - for (n = 0; n < m; n++) { - /* send thing fd */ - if (sock->fd == -1) - fd = -1; - else if ((fd = dup(sock->fd)) == -1) - return (-1); - if (proc_composev_imsg(ps, id, n, - IMSG_CFG_SOCKS, -1, fd, iov, - c) != 0) { - log_warn("%s: failed to compose " - "IMSG_CFG_SOCKS imsg", - __func__); - return (-1); - } - if (proc_flush_imsg(ps, id, n) == -1) { - log_warn("%s: failed to flush " - "IMSG_CFG_SOCKS imsg", - __func__); - return (-1); - } - } - /* Configure TLS if necessary. */ - config_setsocks_tls(env, sock); + /* + * imsg closes the passed descriptor once sent, so every sockets + * process gets its own dup. The flush is synchronous because the + * parent pledges without sendfd once configuration is finished. + */ + for (n = 0; n < env->prefork_socks; n++) { + if (sock->fd == -1) + fd = -1; + else if ((fd = dup(sock->fd)) == -1) + return (-1); + if (imsg_compose(&parent_iev_socks[n].ibuf, IMSG_CFG_SOCKS, 0, + 0, fd, &u, sizeof(u)) == -1) { + log_warn("%s: failed to compose IMSG_CFG_SOCKS imsg", + __func__); + return (-1); } + do + ret = imsgbuf_flush(&parent_iev_socks[n].ibuf); + while (ret == -1 && errno == EAGAIN); + if (ret == -1) { + log_warn("%s: failed to flush IMSG_CFG_SOCKS imsg", + __func__); + return (-1); + } } + /* Configure TLS if necessary. */ + config_setsocks_tls(env, sock); + /* Close thing socket early to prevent fd exhaustion in thingsd. */ if (sock->fd != -1) { close(sock->fd); @@ -193,18 +192,17 @@ static int config_settls(struct thingsd *env, struct socket *sock, enum tls_config_type type, const char *label, uint8_t *data, size_t len) { - struct privsep *ps = env->thingsd_ps; struct socket_config *sock_conf = &sock->conf; struct tls_config tls; struct iovec iov[2]; size_t c; + int n; if (data == NULL || len == 0) return (0); - DPRINTF("%s: sending tls %s for \"%s[%u]\" to %s fd %d", __func__, - label, sock_conf->name, sock_conf->id, ps->ps_title[PROC_SOCKS], - sock->fd); + log_debug("%s: sending tls %s for \"%s[%u]\" to sockets fd %d", + __func__, label, sock_conf->name, sock_conf->id, sock->fd); memset(&tls, 0, sizeof(tls)); tls.id = sock_conf->id; @@ -223,10 +221,14 @@ config_settls(struct thingsd *env, struct socket *sock iov[c].iov_base = data; iov[c++].iov_len = tls.tls_chunk_len; - if (proc_composev(ps, PROC_SOCKS, IMSG_CFG_TLS, iov, c) != 0) { - log_warn("%s: failed to compose IMSG_CFG_TLS imsg for " - "`%s'", __func__, sock_conf->name); - return (-1); + for (n = 0; n < env->prefork_socks; n++) { + if (imsg_composev_event(&parent_iev_socks[n], + IMSG_CFG_TLS, 0, 0, -1, iov, c) == -1) { + log_warn("%s: failed to compose IMSG_CFG_TLS " + "imsg for `%s'", __func__, + sock_conf->name); + return (-1); + } } tls.tls_chunk_offset += tls.tls_chunk_len; @@ -275,18 +277,13 @@ config_gettls(struct thingsd *env, struct socket_confi struct tls_config *tls_conf, const char *label, uint8_t *data, size_t len, uint8_t **outdata, size_t *outlen) { -#ifdef DEBUG - struct privsep *ps = env->thingsd_ps; -#endif + log_debug("%s: getting tls %s (%zu:%zu@%zu) for \"%s[%u]\"", + __func__, label, tls_conf->tls_len, len, + tls_conf->tls_chunk_offset, sock_conf->name, sock_conf->id); - DPRINTF("%s: %s %d getting tls %s (%zu:%zu@%zu) for \"%s[%u]\"", - __func__, ps->ps_title[privsep_process], ps->ps_instance, label, - tls_conf->tls_len, len, tls_conf->tls_chunk_offset, sock_conf->name, - sock_conf->id); - if (tls_conf->tls_chunk_offset == 0) { - *outdata = NULL; free(*outdata); + *outdata = NULL; *outlen = 0; if ((*outdata = calloc(1, tls_conf->tls_len)) == NULL) goto fail; @@ -387,47 +384,3 @@ config_getsocks_tls(struct thingsd *env, struct imsg * fail: return (-1); } - -void -config_purge(struct thingsd *env, unsigned int reset) -{ - struct privsep *ps = env->thingsd_ps; - struct thing *thing; - unsigned int what; - - what = ps->ps_what[privsep_process]; - if (what & CONFIG_SOCKS) { - while ((thing = TAILQ_FIRST(env->things)) != NULL) { - TAILQ_REMOVE(env->things, thing, entry); - free(thing); - } - } -} - -int -config_setreset(struct thingsd *env, unsigned int reset) -{ - struct privsep *ps = env->thingsd_ps; - unsigned int id; - - for (id = 0; id < PROC_MAX; id++) { - if (id == privsep_process) - continue; - proc_compose(ps, id, IMSG_CTL_RESET, &reset, sizeof(reset)); - } - - return (0); -} - -int -config_getreset(struct thingsd *env, struct imsg *imsg) -{ - unsigned int mode; - - IMSG_SIZE_CHECK(imsg, &mode); - memcpy(&mode, imsg->data, sizeof(mode)); - - config_purge(env, mode); - - return (0); -} blob - d7fa4f99c86b4ee296c19214dc9ec022480622b5 blob + b787c8badc63e179b4331ace198c4de19d658f54 --- thingsd/control.c +++ thingsd/control.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021 Tracey Emery + * Copyright (c) 2020-2026 Tracey Emery * Copyright (c) 2010-2015 Reyk Floeter * Copyright (c) 2003, 2004 Henning Brauer * @@ -30,160 +30,320 @@ #include #include #include +#include #include #include #include #include -#include "proc.h" #include "thingsd.h" #define CONTROL_BACKLOG 5 -int sockets_proc, clients_proc; +struct control_sock { + const char *cs_name; + struct event cs_ev; + struct event cs_evt; + int cs_fd; + int cs_restricted; +}; -struct ctl_connlist ctl_conns; +struct ctl_conn { + TAILQ_ENTRY(ctl_conn) entry; + uint8_t flags; +#define CTL_CONN_NOTIFY 0x01 + struct imsgev iev; + struct sockpeercred peercred; + /* sockets processes still to answer this conn's list request */ + int pending_sockets; + int pending_clients; +}; +TAILQ_HEAD(ctl_connlist, ctl_conn); + +static struct ctl_connlist ctl_conns; +static struct control_sock control_cs; +static struct imsgev *control_iev_parent; +static struct imsgev *control_iev_socks; +static struct event ev_sigint; +static struct event ev_sigterm; +static struct event ev_sighup; +static struct event ev_sigpipe; +static struct event ev_sigusr1; + struct ctl_conn *control_connbyfd(int); +int control_init(struct control_sock *); +int control_listen(struct control_sock *); +void control_cleanup(struct control_sock *); void control_accept(int, short, void *); -void control_close(struct privsep *, struct imsg *, int, - struct control_sock *); +void control_close(int, struct control_sock *); void control_dispatch_imsg(int, short, void *); -int control_dispatch_thingsd(int, struct privsep_proc *, struct imsg *); -int control_dispatch_sockets(int, struct privsep_proc *, struct imsg *); +void control_dispatch_thingsd(int, short, void *); +void control_dispatch_sockets(int, short, void *); void control_imsg_forward(struct imsg *); -void control_run(struct privsep *, struct privsep_proc *, void *); +void control_sighdlr(int, short, void *); +__dead void + control_shutdown(void); -static struct privsep_proc procs[] = { - { "thingsd", PROC_PARENT, control_dispatch_thingsd }, - { "sockets", PROC_SOCKS, control_dispatch_sockets }, -}; - -void -control(struct privsep *ps, struct privsep_proc *p) +__dead void +control(int fd_parent, int *fd_socks) { - proc_run(ps, p, procs, nitems(procs), control_run, NULL); -} + struct passwd *pw; + int n; -void -control_run(struct privsep *ps, struct privsep_proc *p, void *arg) -{ - if (pledge("stdio cpath unix recvfd", NULL) == -1) + log_procinit("control"); + + /* the socket is bound as root, before the chroot */ + control_cs.cs_name = THINGSD_SOCKET; + if (control_init(&control_cs) == -1) + fatalx("%s: control_init", __func__); + + if ((pw = getpwnam(THINGSD_USER)) == NULL) + fatal("unknown user %s", THINGSD_USER); + + if (chroot(pw->pw_dir) == -1) + fatal("%s: chroot", __func__); + if (chdir("/") == -1) + fatal("%s: chdir(\"/\")", __func__); + + setproctitle("%s", "control"); + + if (setgroups(1, &pw->pw_gid) || + setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || + setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) + fatal("%s: cannot drop privileges", __func__); + + event_init(); + + signal_set(&ev_sigint, SIGINT, control_sighdlr, NULL); + signal_set(&ev_sigterm, SIGTERM, control_sighdlr, NULL); + signal_set(&ev_sighup, SIGHUP, control_sighdlr, NULL); + signal_set(&ev_sigpipe, SIGPIPE, control_sighdlr, NULL); + signal_set(&ev_sigusr1, SIGUSR1, control_sighdlr, NULL); + signal_add(&ev_sigint, NULL); + signal_add(&ev_sigterm, NULL); + signal_add(&ev_sighup, NULL); + signal_add(&ev_sigpipe, NULL); + signal_add(&ev_sigusr1, NULL); + + if ((control_iev_parent = calloc(1, sizeof(*control_iev_parent))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(control_iev_parent, fd_parent, + control_dispatch_thingsd); + + if ((control_iev_socks = calloc(thingsd_env->prefork_socks, + sizeof(*control_iev_socks))) == NULL) + fatal("%s: calloc", __func__); + for (n = 0; n < thingsd_env->prefork_socks; n++) + imsg_event_init(&control_iev_socks[n], fd_socks[n], + control_dispatch_sockets); + + TAILQ_INIT(&ctl_conns); + if (control_listen(&control_cs) == -1) + fatalx("%s: control_listen", __func__); + + if (pledge("stdio cpath unix", NULL) == -1) fatal("pledge"); + + event_dispatch(); + + control_shutdown(); } -int -control_dispatch_sockets(int fd, struct privsep_proc *p, struct imsg *imsg) +void +control_sighdlr(int sig, short event, void *arg) { - struct ctl_conn *c; - struct privsep *ps = p->p_ps; - struct control_sock cs = ps->ps_csock; - int bad = 0; + switch (sig) { + case SIGINT: + case SIGTERM: + control_shutdown(); + break; + case SIGHUP: + case SIGPIPE: + case SIGUSR1: + /* ignore */ + break; + default: + fatalx("%s: unexpected signal", __func__); + } +} - switch (imsg->hdr.type) { - case IMSG_BAD_THING: - bad = 1; - case IMSG_GET_INFO_SOCKETS_END_DATA: - case IMSG_GET_INFO_CLIENTS_END_DATA: - if (imsg->hdr.type == IMSG_GET_INFO_SOCKETS_END_DATA) - sockets_proc++; - if (imsg->hdr.type == IMSG_GET_INFO_CLIENTS_END_DATA) - clients_proc++; +__dead void +control_shutdown(void) +{ + struct ctl_conn *c; + int n; - if (sockets_proc == thingsd_env->prefork_socks) { - sockets_proc = 0; - goto dispatch; - } + control_cleanup(&control_cs); - if (clients_proc == thingsd_env->prefork_socks) { - clients_proc = 0; - goto dispatch; + while ((c = TAILQ_FIRST(&ctl_conns)) != NULL) { + TAILQ_REMOVE(&ctl_conns, c, entry); + event_del(&c->iev.ev); + imsgbuf_clear(&c->iev.ibuf); + close(c->iev.ibuf.fd); + free(c); + } + + event_del(&control_iev_parent->ev); + imsgbuf_clear(&control_iev_parent->ibuf); + close(control_iev_parent->ibuf.fd); + free(control_iev_parent); + for (n = 0; n < thingsd_env->prefork_socks; n++) { + event_del(&control_iev_socks[n].ev); + imsgbuf_clear(&control_iev_socks[n].ibuf); + close(control_iev_socks[n].ibuf.fd); + } + free(control_iev_socks); + + log_info("control exiting, pid %d", getpid()); + exit(0); +} + +void +control_dispatch_sockets(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + struct ctl_conn *c; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost sockets process", __func__); + control_shutdown(); } - break; -dispatch: - c = control_connbyfd(imsg->hdr.peerid); - if (c == NULL) { - log_warnx("%s: fd %d: not found", - __func__, imsg->hdr.peerid); - return (-1); + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost sockets process", __func__); + control_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); } + } - if (bad) { - control_close(ps, imsg, imsg->hdr.peerid, &cs); + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) break; - } - imsg_compose_event(&c->iev, imsg->hdr.type, - 0, 0, -1, imsg->data, IMSG_DATA_SIZE(imsg)); - break; - case IMSG_GET_INFO_SOCKETS_DATA: - case IMSG_GET_INFO_CLIENTS_DATA: - c = control_connbyfd(imsg->hdr.peerid); - if (c == NULL) { - log_warnx("%s: fd %d: not found", - __func__, imsg->hdr.peerid); - return (-1); - } + switch (imsg.hdr.type) { + case IMSG_GET_INFO_SOCKETS_END_DATA: + case IMSG_GET_INFO_CLIENTS_END_DATA: + case IMSG_GET_INFO_SOCKETS_DATA: + case IMSG_GET_INFO_CLIENTS_DATA: + c = control_connbyfd(imsg.hdr.peerid); + if (c == NULL) { + log_warnx("%s: fd %d: not found", + __func__, imsg.hdr.peerid); + break; + } - if (bad) { - control_close(ps, imsg, imsg->hdr.peerid, &cs); + /* + * Every sockets process answers the request, so the + * end marker is only passed on once all have replied. + */ + if (imsg.hdr.type == IMSG_GET_INFO_SOCKETS_END_DATA && + --c->pending_sockets > 0) + break; + if (imsg.hdr.type == IMSG_GET_INFO_CLIENTS_END_DATA && + --c->pending_clients > 0) + break; + + imsg_compose_event(&c->iev, imsg.hdr.type, + 0, 0, -1, imsg.data, IMSG_DATA_SIZE(&imsg)); break; + default: + log_warnx("%s: unexpected imsg %d from sockets", + __func__, imsg.hdr.type); + break; } - - imsg_compose_event(&c->iev, imsg->hdr.type, - 0, 0, -1, imsg->data, IMSG_DATA_SIZE(imsg)); - break; - default: - return (-1); + imsg_free(&imsg); } - return (0); + imsg_event_add(iev); } -int -control_dispatch_thingsd(int fd, struct privsep_proc *p, struct imsg *imsg) +void +control_dispatch_thingsd(int fd, short event, void *arg) { - struct ctl_conn *c; - struct privsep *ps = p->p_ps; - struct control_sock cs = ps->ps_csock; - int bad = 0; + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + struct ctl_conn *c; + int n, bad; - switch (imsg->hdr.type) { - case IMSG_BAD_THING: - bad = 1; - case IMSG_SHOW_PACKETS_DATA: - case IMSG_SHOW_PACKETS_END_DATA: - case IMSG_GET_INFO_THINGSD_DATA: - case IMSG_GET_INFO_THINGSD_END_DATA: - case IMSG_GET_INFO_THINGS_DATA: - case IMSG_GET_INFO_THINGS_END_DATA: - c = control_connbyfd(imsg->hdr.peerid); - if (c == NULL) { - log_warnx("%s: fd %d: not found", - __func__, imsg->hdr.peerid); - return (-1); + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost parent process", __func__); + control_shutdown(); } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost parent process", __func__); + control_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); + } + } - if (bad) { - control_close(ps, imsg, imsg->hdr.peerid, &cs); + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) break; - } - imsg_compose_event(&c->iev, imsg->hdr.type, - 0, 0, -1, imsg->data, IMSG_DATA_SIZE(imsg)); - break; - case IMSG_CTL_RESET: - case IMSG_CFG_DONE: - case IMSG_CTL_START: - /* don't do anything */ - break; - default: - return (-1); + bad = 0; + switch (imsg.hdr.type) { + case IMSG_BAD_THING: + bad = 1; + /* FALLTHROUGH */ + case IMSG_SHOW_PACKETS_DATA: + case IMSG_SHOW_PACKETS_END_DATA: + case IMSG_GET_INFO_THINGSD_DATA: + case IMSG_GET_INFO_THINGSD_END_DATA: + case IMSG_GET_INFO_THINGS_DATA: + case IMSG_GET_INFO_THINGS_END_DATA: + c = control_connbyfd(imsg.hdr.peerid); + if (c == NULL) { + log_warnx("%s: fd %d: not found", + __func__, imsg.hdr.peerid); + break; + } + + if (bad) { + control_close(imsg.hdr.peerid, &control_cs); + break; + } + + imsg_compose_event(&c->iev, imsg.hdr.type, + 0, 0, -1, imsg.data, IMSG_DATA_SIZE(&imsg)); + break; + case IMSG_CFG_DONE: + case IMSG_CTL_START: + /* don't do anything */ + break; + default: + log_warnx("%s: unexpected imsg %d from parent", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); } - return (0); + imsg_event_add(iev); } int -control_init(struct privsep *ps, struct control_sock *cs) +control_init(struct control_sock *cs) { struct sockaddr_un sun; int fd; @@ -198,8 +358,15 @@ control_init(struct privsep *ps, struct control_sock * return (-1); } + memset(&sun, 0, sizeof(sun)); sun.sun_family = AF_UNIX; - memcpy(&sun.sun_path, cs->cs_name, sizeof(sun.sun_path)); + if (strlcpy(sun.sun_path, cs->cs_name, sizeof(sun.sun_path)) >= + sizeof(sun.sun_path)) { + log_warnx("%s: socket path too long: %s", __func__, + cs->cs_name); + close(fd); + return (-1); + } if (unlink(cs->cs_name) == -1) if (errno != ENOENT) { @@ -233,7 +400,6 @@ control_init(struct privsep *ps, struct control_sock * } cs->cs_fd = fd; - cs->cs_env = ps; return (0); } @@ -265,6 +431,7 @@ control_cleanup(struct control_sock *cs) event_del(&cs->cs_ev); event_del(&cs->cs_evt); + close(cs->cs_fd); } /* ARGSUSED */ @@ -307,6 +474,7 @@ control_accept(int listenfd, short event, void *arg) return; } + len = sizeof(c->peercred); if (getsockopt(connfd, SOL_SOCKET, SO_PEERCRED, &c->peercred, &len) != 0) { log_warn("%s: failed to get peer credentials", __func__); @@ -324,10 +492,9 @@ control_accept(int listenfd, short event, void *arg) c->iev.handler = control_dispatch_imsg; c->iev.events = EV_READ; - c->iev.data = cs; event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, - c->iev.handler, c->iev.data); + c->iev.handler, &c->iev); event_add(&c->iev.ev, NULL); TAILQ_INSERT_TAIL(&ctl_conns, c, entry); @@ -347,8 +514,7 @@ control_connbyfd(int fd) } void -control_close(struct privsep *ps, struct imsg *imsg, int fd, - struct control_sock *cs) +control_close(int fd, struct control_sock *cs) { struct ctl_conn *c; @@ -358,9 +524,9 @@ control_close(struct privsep *ps, struct imsg *imsg, i return; } - if (proc_compose_imsg(ps, PROC_PARENT, -1, - IMSG_SHOW_PACKETS_END_DATA, 0, -1, - &fd, sizeof(fd)) == -1) + /* let the parent drop any packet echo registered for this conn */ + if (imsg_compose_event(control_iev_parent, IMSG_SHOW_PACKETS_END_DATA, + 0, 0, -1, &fd, sizeof(fd)) == -1) log_warn("fd not sent to thingsd to close"); imsgbuf_clear(&c->iev.ibuf); @@ -382,16 +548,14 @@ control_close(struct privsep *ps, struct imsg *imsg, i void control_dispatch_imsg(int fd, short event, void *arg) { - struct thingsd_control_info nci; + struct thingsd_control_info nci; + struct control_sock *cs = &control_cs; + struct ctl_conn *c; + struct imsg imsg; + int n, v, ret = 0; + uid_t euid; + gid_t egid; - struct control_sock *cs = arg; - struct privsep *ps = cs->cs_env; - struct ctl_conn *c; - struct imsg imsg; - int n, v, ret = 0; - uid_t euid; - gid_t egid; - if (getpeereid(fd, &euid, &egid) == -1) return; @@ -404,14 +568,14 @@ control_dispatch_imsg(int fd, short event, void *arg) if (event & EV_READ) { n = imsgbuf_read(&c->iev.ibuf); if (n == -1 || n == 0) { - control_close(ps, &imsg, fd, cs); + control_close(fd, cs); return; } } if (event & EV_WRITE) { if (imsgbuf_write(&c->iev.ibuf) == -1) { - control_close(ps, &imsg, fd, cs); + control_close(fd, cs); return; } } @@ -419,7 +583,7 @@ control_dispatch_imsg(int fd, short event, void *arg) for (;;) { n = imsgbuf_get(&c->iev.ibuf, &imsg); if (n == -1) { - control_close(ps, &imsg, fd, cs); + control_close(fd, cs); return; } @@ -460,7 +624,7 @@ control_dispatch_imsg(int fd, short event, void *arg) IMSG_SIZE_CHECK(&imsg, &v); memcpy(&v, imsg.data, sizeof(v)); log_setverbose(v); - proc_forward_imsg(ps, &imsg, PROC_PARENT, -1); + imsg_forward_event(control_iev_parent, &imsg); break; case IMSG_GET_INFO_CONTROL_REQUEST: nci.verbose = log_getverbose(); @@ -477,29 +641,31 @@ control_dispatch_imsg(int fd, short event, void *arg) case IMSG_GET_INFO_THINGS_REQUEST: case IMSG_GET_INFO_CLIENTS_REQUEST: case IMSG_GET_INFO_SOCKETS_REQUEST: + /* the conn fd is the peerid replies are routed by */ imsg.hdr.peerid = fd; if (euid == 0 && - imsg.hdr.type == - IMSG_GET_INFO_THINGS_REQUEST) - imsg.hdr.type = - IMSG_GET_INFO_THINGS_REQUEST_ROOT; + imsg.hdr.type == IMSG_GET_INFO_THINGS_REQUEST) + imsg.hdr.type = IMSG_GET_INFO_THINGS_REQUEST_ROOT; - if (proc_compose_imsg(ps, PROC_PARENT, -1, - imsg.hdr.type, imsg.hdr.peerid, -1, - imsg.data, IMSG_DATA_SIZE(&imsg)) == -1) { - control_close(ps, &imsg, fd, cs); + if (imsg.hdr.type == IMSG_GET_INFO_SOCKETS_REQUEST) + c->pending_sockets = thingsd_env->prefork_socks; + if (imsg.hdr.type == IMSG_GET_INFO_CLIENTS_REQUEST) + c->pending_clients = thingsd_env->prefork_socks; + + if (imsg_forward_event(control_iev_parent, + &imsg) == -1) { + imsg_free(&imsg); + control_close(fd, cs); return; } break; - case IMSG_CTL_RESET: - proc_forward_imsg(ps, &imsg, PROC_PARENT, -1); - break; default: log_debug("%s: error handling imsg %d", __func__, imsg.hdr.type); - control_close(ps, &imsg, fd, cs); - break; + imsg_free(&imsg); + control_close(fd, cs); + return; } imsg_free(&imsg); } @@ -508,10 +674,11 @@ control_dispatch_imsg(int fd, short event, void *arg) return; fail: + imsg_free(&imsg); imsg_compose_event(&c->iev, IMSG_CTL_FAIL, 0, 0, -1, &ret, sizeof(ret)); imsgbuf_flush(&c->iev.ibuf); - control_close(ps, &imsg, fd, cs); + control_close(fd, cs); } void blob - eb88d359aff455ebbdc09e5aa12643167ac9ba59 blob + e032b2cec8e8ee36e30fda2bb5118da98a1e8f56 --- thingsd/examples/etc/thingsd.conf +++ thingsd/examples/etc/thingsd.conf @@ -59,7 +59,7 @@ thing "software_thing4" { udp "my_listener" { receive on port 8080 listen on port 51000 - bing udp interface "re0" + bind udp interface "re0" password "Sister" } } blob - 313037de26dee3679d4f939d6491c516725611eb blob + fca5f21b6415bca631faf17c607243db62428de0 --- thingsd/parse.y +++ thingsd/parse.y @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2021 Tracey Emery + * Copyright (c) 2016-2026 Tracey Emery * Copyright (c) 2020 Matthias Pressfreund * Copyright (c) 2007 - 2015 Reyk Floeter * Copyright (c) 2008 Gilles Chehade @@ -50,7 +50,6 @@ #include #include -#include "proc.h" #include "thingsd.h" TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); @@ -90,8 +89,6 @@ int getservice(char *); int symset(const char *, const char *, int); char *symget(const char *); -void clear_config(struct thingsd *xconf); - static int errors; int get_addrs(const char *, struct addresslist *, in_port_t); @@ -99,14 +96,21 @@ int get_addrs(const char *, struct addresslist *, in static struct thingsd *thingsd; static struct thing *new_thing; -const int baudrates[18] = {50, 75, 110, 134, 150, 200, +const int baudrates[] = {50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, - 38400, 57600, 76800, 115200}; -const char *parity[4] = {"none", "odd", "even", "space"}; -struct client *pclient, *tclient; -char my_name[THINGSD_MAXTEXT]; -int my_fd, pkt_len; + 19200, 38400, 57600, 76800, 115200}; +const char *parity[] = {"none", "odd", "even"}; +struct client *pclient; +/* + * The same grammar parses thingsd.conf in the parent and subscription + * packets in the sockets processes. yylex() hands out one of these + * before anything else so a packet can only ever reach the subscribe + * rule. + */ +static int start_token; +static int parsing_pkt; + typedef struct { union { long long number; @@ -118,6 +122,7 @@ typedef struct { %} +%token CONFIG_START PKT_START %token BAUD BIND CA CERTIFICATE CIPHERS CLIENT CLIENTS CONNECT CONNECTION CRL %token DATA DHE ECDHE ERROR HARDWARE INCLUDE INTERFACE IPADDR KEY %token LISTEN LOCATION MAX NAME OCSP ON OPTIONAL PARITY PASSWORD PERSISTENT @@ -132,25 +137,28 @@ typedef struct { %% -grammar : /* empty */ - | grammar include '\n' - | grammar '\n' - | grammar conf_main '\n' - | grammar dosub - | grammar error '\n' { file->errors++; } - | grammar thing '\n' - | grammar varset '\n' +grammar : CONFIG_START config + | PKT_START optnl dosub optnl ; +config : /* empty */ + | config include '\n' + | config '\n' + | config conf_main '\n' + | config error '\n' { file->errors++; } + | config thing '\n' + | config varset '\n' + ; + conf_main : bindopts1 | maxclients | PREFORK NUMBER { - thingsd->prefork_socks = $2; - if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) { + if ($2 <= 0 || $2 > THINGSD_MAX_PREFORK) { yyerror("invalid number of preforked " "sockets: %lld", $2); YYERROR; } + thingsd->prefork_socks = $2; } | thingretry ; @@ -162,7 +170,6 @@ bindopts1 : BIND TCP INTERFACE STRING { yyerror("%s: thingsd tcp_iface truncated", __func__); free($4); - free(thingsd); YYERROR; } if (strlcpy(thingsd->udp_iface, $4, @@ -171,7 +178,6 @@ bindopts1 : BIND TCP INTERFACE STRING { yyerror("%s: thingsd udp_iface truncated", __func__); free($4); - free(thingsd); YYERROR; } free($4); @@ -185,7 +191,6 @@ bindopts1 : BIND TCP INTERFACE STRING { yyerror("%s: thingsd udp_iface truncated", __func__); free($4); - free(thingsd); YYERROR; } free($4); @@ -199,7 +204,6 @@ bindopts2 : BIND TCP INTERFACE STRING { yyerror("%s: new_thing tcp_iface truncated", __func__); free($4); - free(new_thing); YYERROR; } if (strlen(new_thing->conf.udp_iface) == 0) { @@ -209,7 +213,6 @@ bindopts2 : BIND TCP INTERFACE STRING { yyerror("%s: new_thing udp_iface " "truncated", __func__); free($4); - free(new_thing); YYERROR; } } @@ -222,7 +225,6 @@ bindopts2 : BIND TCP INTERFACE STRING { yyerror("%s: new_thing udp_iface truncated", __func__); free($4); - free(new_thing); YYERROR; } free($4); @@ -274,9 +276,9 @@ locationopts : /* empty */ locationopts1 : bindopts2 | BAUD NUMBER { - int bc; - const int bauds = (sizeof(baudrates) / - sizeof(const int)); + size_t bc; + const size_t bauds = (sizeof(baudrates) / + sizeof(baudrates[0])); new_thing->conf.baud = -1; for (bc = 0; bc < bauds; bc++) { @@ -324,15 +326,14 @@ locationopts1 : bindopts2 yyerror("%s: new_thing password truncated", __func__); free($2); - free(new_thing); YYERROR; } free($2); } | PARITY STRING { - int pc; - const int parities = (sizeof(parity) / - sizeof(const char *)); + size_t pc; + const size_t parities = (sizeof(parity) / + sizeof(parity[0])); for (pc = 0; pc < parities; pc++) { if (strcmp($2, parity[pc]) == 0) { @@ -342,7 +343,6 @@ locationopts1 : bindopts2 yyerror("%s: new_thing parity " "truncated", __func__); free($2); - free(new_thing); YYERROR; } continue; @@ -359,7 +359,7 @@ locationopts1 : bindopts2 if ($2 > 1 || $2 < 0) { yyerror("software syntax error"); YYERROR; - } else + } else if ($2 > 0) new_thing->conf.sw_ctl = 1; } | STOP NUMBER { @@ -382,19 +382,30 @@ locationopts2 : locationopts2 locationopts1 nl ; maxclients : MAX CLIENTS NUMBER { + if ($3 < 0) { + yyerror("invalid max clients: %lld", $3); + YYERROR; + } thingsd->max_clients = $3; } ; maxclientssub : MAX CLIENTS NUMBER { + if ($3 < 0) { + yyerror("invalid max clients: %lld", $3); + YYERROR; + } new_thing->conf.max_clients = $3; } ; name : NAME optcomma STRING { - memset(&my_name, 0, sizeof(my_name)); - memcpy(&pclient->name, $3, sizeof(pclient->name)); - memcpy(&my_name, $3, sizeof(my_name)); + if (strlcpy(pclient->name, $3, + sizeof(pclient->name)) >= sizeof(pclient->name)) { + yyerror("client name too long"); + free($3); + YYERROR; + } free($3); } ; @@ -459,7 +470,6 @@ socketopts1 : CONNECT ON PORT port { yyerror("%s: new_thing password truncated", __func__); free($2); - free(new_thing); YYERROR; } free($2); @@ -503,74 +513,55 @@ subopts : { subthings : THING '{' STRING optcomma STRING '}' { struct socket *sock = pclient->sock, *tsock; - struct socket *psock = NULL, *csock = NULL; struct client *clt; + if (pclient->subscribed) + goto done; + + if (pclient->name[0] == '\0') { + log_debug("subscription without client name"); + goto done; + } + /* check thing name */ if ((strcmp(sock->conf.thing_name, $3)) != 0) goto done; /* check we have the password */ - if (strcmp(sock->conf.password, $5) != 0) + if (strlen(sock->conf.password) != strlen($5) || + timingsafe_bcmp(sock->conf.password, $5, + strlen($5)) != 0) goto done; - /* check that ports match */ - if (sock->conf.port != pclient->port) - goto done; - /* - * XXX: check from here down don't actually work - * when prefork is > 1 - * come back and figure this out later - * probably a way to loop through the processes and - * check all + * XXX: the duplicate name and max clients checks + * only see this sockets process, so with prefork + * greater than one they are per process limits. */ - - /* check for duplicate name on socket */ TAILQ_FOREACH(tsock, thingsd_env->sockets, entry) { TAILQ_FOREACH(clt, tsock->clients, entry) { - if (strcmp(clt->name, my_name) == 0 && - clt->subscribed) { - log_warnx("client exists"); + if (clt->subscribed && strcmp(clt->name, + pclient->name) == 0) { + log_warnx("client exists: %s", + pclient->name); goto done; } } } - /* check parent/child counts */ - if (sock->conf.child_id) { - TAILQ_FOREACH(csock, thingsd_env->sockets, - entry) - if (csock->conf.id == - sock->conf.child_id) - break; - } - if (sock->conf.parent_id) { - TAILQ_FOREACH(psock, thingsd_env->sockets, - entry) - if (psock->conf.id == - sock->conf.parent_id) - break; - } - - /* if counts are fine, subscribe client */ if (sock->conf.max_clients > 0 && - (sock->client_cnt + 1 > - sock->conf.max_clients)) { + sockets_thing_client_cnt(sock->conf.thing_id) >= + sock->conf.max_clients) { log_debug("max clients reached for listener %d", sock->conf.id); - } else { - pclient->subscribed = 1; - - sock->client_cnt++; - if (csock != NULL) - csock->client_cnt++; - if (psock != NULL) - psock->client_cnt++; - - log_debug("client %s subscribed to %s", - pclient->name, $3); + goto done; } + + pclient->subscribed = 1; + sock->client_cnt++; + + log_debug("client %s subscribed to %s", + pclient->name, $3); done: free($3); free($5); @@ -582,16 +573,18 @@ subthings2 : subthings2 subthings ; thing : THING STRING { - thing_id++; + if (strlen($2) >= THINGSD_MAXNAME) { + yyerror("thing name too long: %s", $2); + free($2); + YYERROR; + } + + thing_id++; new_thing = thingsd_conf_new_thing(thingsd_env, NULL, $2, thing_id); - - if (strlcpy(new_thing->conf.name, $2, - sizeof(new_thing->conf.name)) >= - sizeof(new_thing->conf.name)) { - yyerror("thing name truncated"); + if (new_thing == NULL) { + yyerror("thing %s already defined", $2); free($2); - free(new_thing); YYERROR; } @@ -600,7 +593,6 @@ thing : THING STRING { sizeof(new_thing->conf.name)) { yyerror("thing name truncated"); free($2); - free(new_thing); YYERROR; } @@ -616,7 +608,6 @@ thing : THING STRING { yyerror("%s: new_thing tcp_iface " "truncated", __func__); - free(new_thing); YYERROR; } } else @@ -643,7 +634,6 @@ thing : THING STRING { yyerror("%s: new_thing udp_iface " "truncated", __func__); - free(new_thing); YYERROR; } } else @@ -754,7 +744,6 @@ thingopts1 : IPADDR STRING { yyerror("%s: new_thing ipaddr truncated", __func__); free($2); - free(new_thing); YYERROR; } new_thing->conf.type = S_TCP; @@ -767,7 +756,6 @@ thingopts1 : IPADDR STRING { yyerror("%s: new_thing location truncated", __func__); free($2); - free(new_thing); YYERROR; } new_thing->conf.type = S_DEV; @@ -780,7 +768,6 @@ thingopts1 : IPADDR STRING { yyerror("%s: new_thing udp truncated", __func__); free($2); - free(new_thing); YYERROR; } new_thing->conf.type = S_UDP; @@ -918,7 +905,10 @@ yyerror(const char *fmt, ...) if (vasprintf(&msg, fmt, ap) == -1) fatalx("yyerror vasprintf"); va_end(ap); - logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg); + if (parsing_pkt) + log_debug("%s: %s", file->name, msg); + else + logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg); free(msg); return (0); } @@ -1003,11 +993,8 @@ lgetc(int quotec) if (parsebuf) { /* Read character from the parsebuffer instead of input. */ if (parseindex >= 0) { - if (parsebuf == NULL) - return (0); - if (parseindex > pkt_len) - return (0); - if ((c = parsebuf[parseindex++]) != '\0') + c = parsebuf[parseindex++]; + if (c != '\0') return (c); parsebuf = NULL; } else @@ -1017,13 +1004,15 @@ lgetc(int quotec) if (pushback_index) return (pushback_buffer[--pushback_index]); - if (quotec) { - if (parseindex > 0) { - /* malformed packet */ + /* a packet has no stream behind it, the buffer is all there is */ + if (parsing_pkt) { + if (quotec) yyerror("reached end of packet while parsing " - "quoted string"); - return (-1); - } + "quoted string"); + return (EOF); + } + + if (quotec) { c = getc(file->stream); if (c == EOF) yyerror("reached end of file while parsing " @@ -1093,6 +1082,12 @@ yylex(void) int quotec, next, c; int token; + if (start_token) { + token = start_token; + start_token = 0; + return (token); + } + top: p = buf; while ((c = lgetc(0)) == ' ' || c == '\t') @@ -1339,9 +1334,11 @@ parse_config(const char *filename, struct thingsd *env thingsd = env; thingsd->conn_retry = CONN_RETRY; - /* thingsd->dead_things->run = 1; */ + thingsd->prefork_socks = SOCKS_NUMPROC; thingsd->run = 1; + parsing_pkt = 0; + start_token = CONFIG_START; yyparse(); errors = file->errors; closefile(file); @@ -1419,7 +1416,8 @@ cmdline_symset(char *s) if ((sym = malloc(len)) == NULL) fatal("%s: malloc", __func__); - memcpy(&sym, s, len); + memcpy(sym, s, len - 1); + sym[len - 1] = '\0'; ret = symset(sym, val + 1, 1); free(sym); @@ -1442,36 +1440,29 @@ symget(const char *nam) } int -parse_buf(struct client *client, u_char *pkt, int len) +parse_buf(struct client *client, u_char *pkt) { - pkt_len = len; pclient = client; - my_fd = client->fd; if ((file = newbuff(pkt)) == NULL) return (-1); + /* nothing from a previous packet may leak into this one */ + pushback_index = 0; + parseindex = 0; + parsing_pkt = 1; + start_token = PKT_START; + yyparse(); errors = file->errors; closebuff(file); + parsebuf = NULL; + pclient = NULL; return (errors ? -1 : 0); } -void -clear_config(struct thingsd *xconf) -{ - struct thing *thing; - - while ((thing = TAILQ_FIRST(xconf->things)) != NULL) { - TAILQ_REMOVE(xconf->things, thing, entry); - free(thing); - } - - free(xconf); -} - int getservice(char *n) { blob - 4417dd0e55b3c89efebb76345cd94e981c439a68 (mode 644) blob + /dev/null --- thingsd/proc.c +++ /dev/null @@ -1,864 +0,0 @@ -/* - * Copyright (c) 2010 - 2016 Reyk Floeter - * Copyright (c) 2008 Pierre-Yves Ritschard - * - * 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 -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "proc.h" -#include "thingsd.h" - -void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int, - int, char **); -void proc_setup(struct privsep *, struct privsep_proc *, unsigned int); -void proc_open(struct privsep *, int, int); -void proc_accept(struct privsep *, int, enum privsep_procid, - unsigned int); -void proc_close(struct privsep *); -int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid); -void proc_shutdown(struct privsep_proc *); -void proc_sig_handler(int, short, void *); -void proc_range(struct privsep *, enum privsep_procid, int *, int *); -int proc_dispatch_null(int, struct privsep_proc *, struct imsg *); - -enum privsep_procid privsep_process; - -int -proc_ispeer(struct privsep_proc *procs, unsigned int nproc, - enum privsep_procid type) -{ - unsigned int i; - - for (i = 0; i < nproc; i++) - if (procs[i].p_id == type) - return (1); - return (0); -} - -enum privsep_procid -proc_getid(struct privsep_proc *procs, unsigned int nproc, - const char *proc_name) -{ - struct privsep_proc *p; - unsigned int proc; - - for (proc = 0; proc < nproc; proc++) { - p = &procs[proc]; - if (strcmp(p->p_title, proc_name)) - continue; - - return (p->p_id); - } - - return (PROC_MAX); -} - -void -proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, - int debug, int argc, char **argv) -{ - unsigned int proc, nargc, i, proc_i; - char **nargv; - struct privsep_proc *p; - char num[32]; - int fd; - - /* Prepare the new process argv. */ - nargv = calloc(argc + 5, sizeof(char *)); - if (nargv == NULL) - fatal("%s: calloc", __func__); - - /* Copy call argument first. */ - nargc = 0; - nargv[nargc++] = argv[0]; - - /* Set process name argument and save the position. */ - nargv[nargc++] = "-P"; - proc_i = nargc; - nargc++; - - /* Point process instance arg to stack and copy the original args. */ - nargv[nargc++] = "-I"; - nargv[nargc++] = num; - for (i = 1; i < (unsigned int) argc; i++) - nargv[nargc++] = argv[i]; - - nargv[nargc] = NULL; - - for (proc = 0; proc < nproc; proc++) { - p = &procs[proc]; - - /* Update args with process title. */ - nargv[proc_i] = (char *)(uintptr_t)p->p_title; - - /* Fire children processes. */ - for (i = 0; i < ps->ps_instances[p->p_id]; i++) { - /* Update the process instance number. */ - snprintf(num, sizeof(num), "%u", i); - - fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0]; - ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1; - - switch (fork()) { - case -1: - fatal("%s: fork", __func__); - break; - case 0: - /* First create a new session */ - if (setsid() == -1) - fatal("setsid"); - - /* Prepare parent socket. */ - if (fd != PROC_PARENT_SOCK_FILENO) { - if (dup2(fd, PROC_PARENT_SOCK_FILENO) - == -1) - fatal("dup2"); - } else if (fcntl(fd, F_SETFD, 0) == -1) - fatal("fcntl"); - - /* Daemons detach from terminal. */ - if (!debug && (fd = - open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { - (void)dup2(fd, STDIN_FILENO); - (void)dup2(fd, STDOUT_FILENO); - (void)dup2(fd, STDERR_FILENO); - if (fd > 2) - (void)close(fd); - } - - execvp(argv[0], nargv); - fatal("%s: execvp", __func__); - break; - default: - /* Close child end. */ - close(fd); - break; - } - } - } - free(nargv); -} - -void -proc_connect(struct privsep *ps) -{ - struct imsgev *iev; - unsigned int src, dst, inst; - - /* Don't distribute any sockets if we are not really going to run. */ - if (ps->ps_noaction) - return; - - for (dst = 0; dst < PROC_MAX; dst++) { - /* We don't communicate with ourselves. */ - if (dst == PROC_PARENT) - continue; - - for (inst = 0; inst < ps->ps_instances[dst]; inst++) { - iev = &ps->ps_ievs[dst][inst]; - if (imsgbuf_init(&iev->ibuf, - ps->ps_pp->pp_pipes[dst][inst]) == -1) - fatal(NULL); - imsgbuf_allow_fdpass(&iev->ibuf); - event_set(&iev->ev, iev->ibuf.fd, iev->events, - iev->handler, iev->data); - event_add(&iev->ev, NULL); - } - } - - /* Distribute the socketpair()s for everyone. */ - for (src = 0; src < PROC_MAX; src++) - for (dst = src; dst < PROC_MAX; dst++) { - /* Parent already distributed its fds. */ - if (src == PROC_PARENT || dst == PROC_PARENT) - continue; - - proc_open(ps, src, dst); - } -} - -void -proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, - int debug, int argc, char **argv, enum privsep_procid proc_id) -{ - struct privsep_proc *p = NULL; - struct privsep_pipes *pa, *pb; - unsigned int proc; - unsigned int dst; - int fds[2]; - - /* Don't initiate anything if we are not really going to run. */ - if (ps->ps_noaction) - return; - - if (proc_id == PROC_PARENT) { - privsep_process = PROC_PARENT; - proc_setup(ps, procs, nproc); - - /* - * Create the children sockets so we can use them - * to distribute the rest of the socketpair()s using - * proc_connect() later. - */ - for (dst = 0; dst < PROC_MAX; dst++) { - /* Don't create socket for ourselves. */ - if (dst == PROC_PARENT) - continue; - - for (proc = 0; proc < ps->ps_instances[dst]; proc++) { - pa = &ps->ps_pipes[PROC_PARENT][0]; - pb = &ps->ps_pipes[dst][proc]; - if (socketpair(AF_UNIX, - SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, - PF_UNSPEC, fds) == -1) - fatal("%s: socketpair", __func__); - - pa->pp_pipes[dst][proc] = fds[0]; - pb->pp_pipes[PROC_PARENT][0] = fds[1]; - } - } - - /* Engage! */ - proc_exec(ps, procs, nproc, debug, argc, argv); - return; - } - - /* Initialize a child */ - for (proc = 0; proc < nproc; proc++) { - if (procs[proc].p_id != proc_id) - continue; - p = &procs[proc]; - break; - } - if (p == NULL || p->p_init == NULL) - fatalx("%s: process %d missing process initialization", - __func__, proc_id); - - p->p_init(ps, p); - - fatalx("failed to initiate child process"); -} - -void -proc_accept(struct privsep *ps, int fd, enum privsep_procid dst, - unsigned int n) -{ - struct privsep_pipes *pp = ps->ps_pp; - struct imsgev *iev; - - if (ps->ps_ievs[dst] == NULL) { -#if DEBUG > 1 - log_debug("%s: %s src %d %d to dst %d %d not connected", - __func__, ps->ps_title[privsep_process], - privsep_process, ps->ps_instance + 1, - dst, n + 1); -#endif - close(fd); - return; - } - - if (pp->pp_pipes[dst][n] != -1) { - log_warnx("%s: duplicated descriptor", __func__); - close(fd); - return; - } else - pp->pp_pipes[dst][n] = fd; - - iev = &ps->ps_ievs[dst][n]; - if (imsgbuf_init(&iev->ibuf, fd) == -1) - fatal(NULL); - imsgbuf_allow_fdpass(&iev->ibuf); - event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data); - event_add(&iev->ev, NULL); -} - -void -proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc) -{ - unsigned int i, j, src, dst, id; - struct privsep_pipes *pp; - - /* Initialize parent title, ps_instances and procs. */ - ps->ps_title[PROC_PARENT] = "parent"; - - for (src = 0; src < PROC_MAX; src++) - /* Default to 1 process instance */ - if (ps->ps_instances[src] < 1) - ps->ps_instances[src] = 1; - - for (src = 0; src < nproc; src++) { - procs[src].p_ps = ps; - if (procs[src].p_cb == NULL) - procs[src].p_cb = proc_dispatch_null; - - id = procs[src].p_id; - ps->ps_title[id] = procs[src].p_title; - if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id], - sizeof(struct imsgev))) == NULL) - fatal("%s: calloc", __func__); - - /* With this set up, we are ready to call imsg_init(). */ - for (i = 0; i < ps->ps_instances[id]; i++) { - ps->ps_ievs[id][i].handler = proc_dispatch; - ps->ps_ievs[id][i].events = EV_READ; - ps->ps_ievs[id][i].proc = &procs[src]; - ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i]; - } - } - - /* - * Allocate pipes for all process instances (incl. parent) - * - * - ps->ps_pipes: N:M mapping - * N source processes connected to M destination processes: - * [src][instances][dst][instances], for example - * [PROC_RELAY][3][PROC_CA][3] - * - * - ps->ps_pp: per-process 1:M part of ps->ps_pipes - * Each process instance has a destination array of socketpair fds: - * [dst][instances], for example - * [PROC_PARENT][0] - */ - for (src = 0; src < PROC_MAX; src++) { - /* Allocate destination array for each process */ - if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src], - sizeof(struct privsep_pipes))) == NULL) - fatal("%s: calloc", __func__); - - for (i = 0; i < ps->ps_instances[src]; i++) { - pp = &ps->ps_pipes[src][i]; - - for (dst = 0; dst < PROC_MAX; dst++) { - /* Allocate maximum fd integers */ - if ((pp->pp_pipes[dst] = - calloc(ps->ps_instances[dst], - sizeof(int))) == NULL) - fatal("%s: calloc", __func__); - - /* Mark fd as unused */ - for (j = 0; j < ps->ps_instances[dst]; j++) - pp->pp_pipes[dst][j] = -1; - } - } - } - - ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance]; -} - -void -proc_kill(struct privsep *ps) -{ - char *cause; - pid_t pid; - int len, status; - - if (privsep_process != PROC_PARENT) - return; - - proc_close(ps); - - do { - pid = waitpid(WAIT_ANY, &status, 0); - if (pid <= 0) - continue; - - if (WIFSIGNALED(status)) { - len = asprintf(&cause, "terminated; signal %d", - WTERMSIG(status)); - } else if (WIFEXITED(status)) { - if (WEXITSTATUS(status) != 0) - len = asprintf(&cause, "exited abnormally"); - else - len = 0; - } else - len = -1; - - if (len == 0) { - /* child exited OK, don't print a warning message */ - } else if (len != -1) { - log_warnx("lost child: pid %u %s", pid, cause); - free(cause); - } else - log_warnx("lost child: pid %u", pid); - } while (pid != -1 || errno == EINTR); -} - -void -proc_open(struct privsep *ps, int src, int dst) -{ - struct privsep_pipes *pa, *pb; - struct privsep_fd pf; - int fds[2]; - unsigned int i, j; - - /* Exchange pipes between process. */ - for (i = 0; i < ps->ps_instances[src]; i++) { - for (j = 0; j < ps->ps_instances[dst]; j++) { - /* Don't create sockets for ourself. */ - if (src == dst && i == j) - continue; - - pa = &ps->ps_pipes[src][i]; - pb = &ps->ps_pipes[dst][j]; - if (socketpair(AF_UNIX, - SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, - PF_UNSPEC, fds) == -1) - fatal("%s: socketpair", __func__); - - pa->pp_pipes[dst][j] = fds[0]; - pb->pp_pipes[src][i] = fds[1]; - - pf.pf_procid = src; - pf.pf_instance = i; - if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD, - -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1) - fatal("%s: proc_compose_imsg", __func__); - - pf.pf_procid = dst; - pf.pf_instance = j; - if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD, - -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1) - fatal("%s: proc_compose_imsg", __func__); - - /* - * We have to flush to send the descriptors and close - * them to avoid the fd ramp on startup. - */ - if (proc_flush_imsg(ps, src, i) == -1 || - proc_flush_imsg(ps, dst, j) == -1) - fatal("%s: imsg_flush", __func__); - } - } -} - -void -proc_close(struct privsep *ps) -{ - unsigned int dst, n; - struct privsep_pipes *pp; - - if (ps == NULL) - return; - - pp = ps->ps_pp; - - for (dst = 0; dst < PROC_MAX; dst++) { - if (ps->ps_ievs[dst] == NULL) - continue; - - for (n = 0; n < ps->ps_instances[dst]; n++) { - if (pp->pp_pipes[dst][n] == -1) - continue; - - /* Cancel the fd, close and invalidate the fd */ - event_del(&(ps->ps_ievs[dst][n].ev)); - imsgbuf_clear(&(ps->ps_ievs[dst][n].ibuf)); - close(pp->pp_pipes[dst][n]); - pp->pp_pipes[dst][n] = -1; - } - free(ps->ps_ievs[dst]); - } -} - -void -proc_shutdown(struct privsep_proc *p) -{ - struct privsep *ps = p->p_ps; - - if (p->p_id == PROC_CONTROL && ps) - control_cleanup(&ps->ps_csock); - - if (p->p_shutdown != NULL) - (*p->p_shutdown)(); - - proc_close(ps); - - log_info("%s exiting, pid %d", p->p_title, getpid()); - - exit(0); -} - -void -proc_sig_handler(int sig, short event, void *arg) -{ - struct privsep_proc *p = arg; - - switch (sig) { - case SIGINT: - case SIGTERM: - proc_shutdown(p); - break; - case SIGCHLD: - case SIGHUP: - case SIGPIPE: - case SIGUSR1: - /* ignore */ - break; - default: - fatalx("%s: unexpected signal", __func__); - /* NOTREACHED */ - } -} - -void -proc_run(struct privsep *ps, struct privsep_proc *p, - struct privsep_proc *procs, unsigned int nproc, - void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg) -{ - struct passwd *pw; - const char *root; - struct control_sock *rcs; - - log_procinit(p->p_title); - - /* Set the process group of the current process */ - setpgid(0, 0); - - if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { - if (control_init(ps, &ps->ps_csock) == -1) - fatalx("%s: control_init", __func__); - TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry) - if (control_init(ps, rcs) == -1) - fatalx("%s: control_init", __func__); - } - - /* Use non-standard user */ - if (p->p_pw != NULL) - pw = p->p_pw; - else - pw = ps->ps_pw; - - /* Change root directory */ - if (p->p_chroot != NULL) - root = p->p_chroot; - else - root = pw->pw_dir; - - if (chroot(root) == -1) - fatal("%s: chroot", __func__); - if (chdir("/") == -1) - fatal("%s: chdir(\"/\")", __func__); - - privsep_process = p->p_id; - - setproctitle("%s", p->p_title); - - if (setgroups(1, &pw->pw_gid) || - setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || - setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) - fatal("%s: cannot drop privileges", __func__); - - event_init(); - - signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p); - signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p); - signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p); - signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p); - signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p); - signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p); - - signal_add(&ps->ps_evsigint, NULL); - signal_add(&ps->ps_evsigterm, NULL); - signal_add(&ps->ps_evsigchld, NULL); - signal_add(&ps->ps_evsighup, NULL); - signal_add(&ps->ps_evsigpipe, NULL); - signal_add(&ps->ps_evsigusr1, NULL); - - proc_setup(ps, procs, nproc); - proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0); - if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { - TAILQ_INIT(&ctl_conns); - if (control_listen(&ps->ps_csock) == -1) - fatalx("%s: control_listen", __func__); - TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry) - if (control_listen(rcs) == -1) - fatalx("%s: control_listen", __func__); - } - - DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title, - ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid()); - - if (run != NULL) - run(ps, p, arg); - - event_dispatch(); - - proc_shutdown(p); -} - -void -proc_dispatch(int fd, short event, void *arg) -{ - struct imsgev *iev = arg; - struct privsep_proc *p = iev->proc; - struct privsep *ps = p->p_ps; - struct imsgbuf *ibuf; - struct imsg imsg; - int n, verbose; - const char *title; - struct privsep_fd pf; - - title = ps->ps_title[privsep_process]; - ibuf = &iev->ibuf; - - if (event & EV_READ) { - if ((n = imsgbuf_read(ibuf)) == -1) - fatal("%s: imsg_read", __func__); - if (n == 0) { - /* this pipe is dead, so remove the event handler */ - event_del(&iev->ev); - event_loopexit(NULL); - return; - } - } - - if (event & EV_WRITE) { - if ((n = imsgbuf_write(ibuf)) == -1) { - if (errno == EPIPE) { - /* this pipe is dead */ - event_del(&iev->ev); - event_loopexit(NULL); - return; - } - fatal("%s: msgbuf_write", __func__); - } - } - - for (;;) { - if ((n = imsgbuf_get(ibuf, &imsg)) == -1) - fatal("%s: imsgbuf_get", __func__); - if (n == 0) - break; - -#if DEBUG > 1 - log_debug("%s: %s %d got imsg %d peerid %d from %s %d", - __func__, title, ps->ps_instance + 1, - imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid); -#endif - - /* - * Check the message with the program callback - */ - if ((p->p_cb)(fd, p, &imsg) == 0) { - /* Message was handled by the callback, continue */ - imsg_free(&imsg); - continue; - } - - /* - * Generic message handling - */ - switch (imsg.hdr.type) { - case IMSG_CTL_VERBOSE: - IMSG_SIZE_CHECK(&imsg, &verbose); - memcpy(&verbose, imsg.data, sizeof(verbose)); - log_setverbose(verbose); - break; - case IMSG_CTL_PROCFD: - IMSG_SIZE_CHECK(&imsg, &pf); - memcpy(&pf, imsg.data, sizeof(pf)); - proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid, - pf.pf_instance); - break; - default: - /* - * change from fatalx - * just because it's bad doesn't mean we should crash - */ - log_warnx("%s: %s %d got invalid imsg %d peerid %d " - "from %s %d", - __func__, title, ps->ps_instance + 1, - imsg.hdr.type, imsg.hdr.peerid, - p->p_title, imsg.hdr.pid); - } - imsg_free(&imsg); - } - imsg_event_add(iev); -} - -int -proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg) -{ - return (-1); -} - -/* - * imsg helper functions - */ - -void -imsg_event_add(struct imsgev *iev) -{ - if (iev->handler == NULL) { - imsgbuf_flush(&iev->ibuf); - return; - } - - iev->events = EV_READ; - if (imsgbuf_queuelen(&iev->ibuf) > 0) - iev->events |= EV_WRITE; - - event_del(&iev->ev); - event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data); - event_add(&iev->ev, NULL); -} - -int -imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid, - pid_t pid, int fd, void *data, uint16_t datalen) -{ - int ret; - - if ((ret = imsg_compose(&iev->ibuf, type, peerid, - pid, fd, data, datalen)) == -1) - return (ret); - imsg_event_add(iev); - return (ret); -} - -int -imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid, - pid_t pid, int fd, const struct iovec *iov, int iovcnt) -{ - int ret; - - if ((ret = imsg_composev(&iev->ibuf, type, peerid, - pid, fd, iov, iovcnt)) == -1) - return (ret); - imsg_event_add(iev); - return (ret); -} - -void -proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m) -{ - if (*n == -1) { - /* Use a range of all target instances */ - *n = 0; - *m = ps->ps_instances[id]; - } else { - /* Use only a single slot of the specified peer process */ - *m = *n + 1; - } -} - -int -proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n, - uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen) -{ - int m; - - proc_range(ps, id, &n, &m); - for (; n < m; n++) { - if (imsg_compose_event(&ps->ps_ievs[id][n], - type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1) - return (-1); - } - - return (0); -} - -int -proc_compose(struct privsep *ps, enum privsep_procid id, - uint16_t type, void *data, uint16_t datalen) -{ - return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen)); -} - -int -proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n, - uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt) -{ - int m; - - proc_range(ps, id, &n, &m); - for (; n < m; n++) - if (imsg_composev_event(&ps->ps_ievs[id][n], - type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1) - return (-1); - - return (0); -} - -int -proc_composev(struct privsep *ps, enum privsep_procid id, - uint16_t type, const struct iovec *iov, int iovcnt) -{ - return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt)); -} - -int -proc_forward_imsg(struct privsep *ps, struct imsg *imsg, - enum privsep_procid id, int n) -{ - return (proc_compose_imsg(ps, id, n, imsg->hdr.type, - imsg->hdr.peerid, imsg_get_fd(imsg), imsg->data, - IMSG_DATA_SIZE(imsg))); -} - -struct imsgbuf * -proc_ibuf(struct privsep *ps, enum privsep_procid id, int n) -{ - int m; - - proc_range(ps, id, &n, &m); - return (&ps->ps_ievs[id][n].ibuf); -} - -struct imsgev * -proc_iev(struct privsep *ps, enum privsep_procid id, int n) -{ - int m; - - proc_range(ps, id, &n, &m); - return (&ps->ps_ievs[id][n]); -} - -/* This function should only be called with care as it breaks async I/O */ -int -proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n) -{ - struct imsgbuf *ibuf; - int m, ret = 0; - - proc_range(ps, id, &n, &m); - for (; n < m; n++) { - if ((ibuf = proc_ibuf(ps, id, n)) == NULL) - return (-1); - do { - ret = imsgbuf_flush(ibuf); - } while (ret == -1 && errno == EAGAIN); - if (ret == -1) - break; - imsg_event_add(&ps->ps_ievs[id][n]); - } - - return (ret); -} blob - 99340c3a5cf75f7edec9d22a35af02b89678136e (mode 644) blob + /dev/null --- thingsd/proc.h +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright (c) 2010-2015 Reyk Floeter - * - * 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. - */ - -enum { - IMSG_NONE, - IMSG_CTL_OK, - IMSG_CTL_FAIL, - IMSG_CTL_VERBOSE, - IMSG_CTL_NOTIFY, - IMSG_CTL_RESET, - IMSG_CTL_PROCFD, - IMSG_PROC_MAX -}; - -/* imsg */ -struct imsgev { - struct imsgbuf ibuf; - void (*handler)(int, short, void *); - struct event ev; - struct privsep_proc *proc; - void *data; - short events; -}; - -#define IMSG_SIZE_CHECK(imsg, p) do { \ - if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \ - fatalx("bad length imsg received (%s)", #p); \ -} while (0) -#define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE) - -/* control socket */ -struct control_sock { - const char *cs_name; - struct event cs_ev; - struct event cs_evt; - int cs_fd; - int cs_restricted; - void *cs_env; - - TAILQ_ENTRY(control_sock) cs_entry; -}; -TAILQ_HEAD(control_socks, control_sock); - -struct ctl_conn { - TAILQ_ENTRY(ctl_conn) entry; - uint8_t flags; - unsigned int waiting; -#define CTL_CONN_NOTIFY 0x01 - struct imsgev iev; - struct sockpeercred peercred; - -}; -TAILQ_HEAD(ctl_connlist, ctl_conn); -extern struct ctl_connlist ctl_conns; - -/* privsep */ -enum privsep_procid { - PROC_PARENT = 0, - PROC_CONTROL, - PROC_SOCKS, - PROC_MAX, -}; - -#define CONFIG_RELOAD 0x00 -#define CONFIG_SOCKS 0x01 -#define CONFIG_ALL 0xff - -struct privsep_pipes { - int *pp_pipes[PROC_MAX]; -}; - -struct privsep { - struct privsep_pipes *ps_pipes[PROC_MAX]; - struct privsep_pipes *ps_pp; - - struct imsgev *ps_ievs[PROC_MAX]; - const char *ps_title[PROC_MAX]; - uint8_t ps_what[PROC_MAX]; - - struct passwd *ps_pw; - int ps_noaction; - - struct control_sock ps_csock; - struct control_socks ps_rcsocks; - - unsigned int ps_instances[PROC_MAX]; - unsigned int ps_instance; - - /* Event and signal handlers */ - struct event ps_evsigint; - struct event ps_evsigterm; - struct event ps_evsigchld; - struct event ps_evsighup; - struct event ps_evsigpipe; - struct event ps_evsigusr1; - - void *ps_env; -}; - -struct privsep_proc { - const char *p_title; - enum privsep_procid p_id; - int (*p_cb)(int, struct privsep_proc *, - struct imsg *); - void (*p_init)(struct privsep *, - struct privsep_proc *); - void (*p_shutdown)(void); - const char *p_chroot; - struct passwd *p_pw; - struct privsep *p_ps; -}; - -struct privsep_fd { - enum privsep_procid pf_procid; - unsigned int pf_instance; -}; - -#if DEBUG -#define DPRINTF log_debug -#else -#define DPRINTF(x...) do {} while(0) -#endif - -#define PROC_PARENT_SOCK_FILENO 3 -#define PROC_MAX_INSTANCES 32 - -/* proc.c */ -void proc_range(struct privsep *, enum privsep_procid, int *, int *); -void proc_init(struct privsep *, struct privsep_proc *, unsigned int, - int, int, char **, enum privsep_procid); -void proc_kill(struct privsep *); -void proc_connect(struct privsep *ps); -void proc_dispatch(int, short event, void *); -void proc_run(struct privsep *, struct privsep_proc *, - struct privsep_proc *, unsigned int, - void (*)(struct privsep *, struct privsep_proc *, void *), void *); -void imsg_event_add(struct imsgev *); -int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, - pid_t, int, void *, uint16_t); -int imsg_composev_event(struct imsgev *, uint16_t, uint32_t, - pid_t, int, const struct iovec *, int); -int proc_compose_imsg(struct privsep *, enum privsep_procid, int, - uint16_t, uint32_t, int, void *, uint16_t); -int proc_compose(struct privsep *, enum privsep_procid, - uint16_t, void *data, uint16_t); -int proc_composev_imsg(struct privsep *, enum privsep_procid, int, - uint16_t, uint32_t, int, const struct iovec *, int); -int proc_composev(struct privsep *, enum privsep_procid, - uint16_t, const struct iovec *, int); -int proc_forward_imsg(struct privsep *, struct imsg *, - enum privsep_procid, int); -struct imsgbuf * - proc_ibuf(struct privsep *, enum privsep_procid, int); -struct imsgev * - proc_iev(struct privsep *, enum privsep_procid, int); -enum privsep_procid - proc_getid(struct privsep_proc *, unsigned int, const char *); -int proc_flush_imsg(struct privsep *, enum privsep_procid, int); - -/* control.c */ -void control(struct privsep *, struct privsep_proc *); -int control_init(struct privsep *, struct control_sock *); -int control_listen(struct control_sock *); -void control_cleanup(struct control_sock *); - -/* log.c */ -void log_init(int, int); -void log_procinit(const char *); -void log_setverbose(int); -int log_getverbose(void); -void log_warn(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_warnx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_info(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_debug(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void logit(int, const char *, ...) - __attribute__((__format__ (printf, 2, 3))); -void vlog(int, const char *, va_list) - __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -__dead void fatalx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); blob - 36badfe0b651edfe3804aa06235601cd44ee1411 blob + ec20c295457b4f7fcc1f5b5c6f631b4a97249234 --- thingsd/sockets.c +++ thingsd/sockets.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2019-2021 Tracey Emery + * Copyright (c) 2016, 2019-2026 Tracey Emery * Copyright (c) 2015 Mike Larkin * Copyright (c) 2007 - 2015 Reyk Floeter * @@ -31,77 +31,112 @@ #include #include #include +#include +#include #include #include #include #include #include -#include "proc.h" #include "thingsd.h" -#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) +#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) #define FD_RESERVE 5 -#define FD_NEEDED 6 -volatile int client_inflight = 0; - void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, void *); -int sockets_dispatch_thingsd(int, struct privsep_proc *, struct imsg *); int sockets_socket_af(struct sockaddr_storage *, in_port_t); -int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int, - volatile int *); -int sockets_dispatch_control(int, struct privsep_proc *, struct imsg *); +int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int); -void sockets_dup_new_socket(struct socket *, struct socket *); -void sockets_run(struct privsep *, struct privsep_proc *, void *); +void sockets_dispatch_thingsd(int, short, void *); +void sockets_dispatch_control(int, short, void *); +void sockets_sighdlr(int, short, void *); +__dead void + sockets_shutdown(void); void sockets_socket_conn(int, short, void *); void sockets_launch(void); void sockets_client_sub_timeout(int, short, void *); void sockets_client_accept_paused(int, short, void *); void sockets_write_to_clients(struct package *); -void sockets_show_info(struct privsep *, struct imsg *); -void sockets_show_clients_info(struct privsep *, struct imsg *); -void sockets_kill_client(struct privsep *, struct imsg *); +void sockets_show_info(struct imsg *); +void sockets_show_clients_info(struct imsg *); +void sockets_kill_client(struct imsg *); void sockets_tls_readcb(int, short, void *); void sockets_tls_writecb(int, short, void *); struct socket - *sockets_conf_new_socket(struct thingsd *, struct thing *, int, int); + *sockets_conf_new_socket(struct thingsd *, struct thing *, int, + struct address *, int); +struct imsgev *socks_iev_parent; +static struct imsgev *socks_iev_control; -extern enum privsep_procid privsep_process; +static struct event ev_sigint; +static struct event ev_sigterm; +static struct event ev_sighup; +static struct event ev_sigpipe; +static struct event ev_sigusr1; -static struct privsep_proc procs[] = { - { "control", PROC_CONTROL, sockets_dispatch_control, control }, - { "thingsd", PROC_PARENT, sockets_dispatch_thingsd }, -}; - -void -sockets(struct privsep *ps, struct privsep_proc *p) +__dead void +sockets(int fd_parent, int fd_control) { - proc_run(ps, p, procs, nitems(procs), sockets_run, NULL); -} + struct passwd *pw; -void -sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg) -{ - if (config_init(ps->ps_env) == -1) + log_procinit("sockets"); + + if ((pw = getpwnam(THINGSD_USER)) == NULL) + fatal("unknown user %s", THINGSD_USER); + + if (chroot(pw->pw_dir) == -1) + fatal("%s: chroot", __func__); + if (chdir("/") == -1) + fatal("%s: chdir(\"/\")", __func__); + + setproctitle("%s", "sockets"); + + if (setgroups(1, &pw->pw_gid) || + setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || + setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) + fatal("%s: cannot drop privileges", __func__); + + /* start from empty lists, the listeners arrive from the parent */ + if (config_init(thingsd_env) == -1) fatal("failed to initialize configuration"); - p->p_shutdown = sockets_shutdown; + sockets_socket_rlimit(); - sockets_socket_rlimit(-1); + event_init(); - signal_del(&ps->ps_evsigchld); - signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps); - signal_add(&ps->ps_evsigchld, NULL); + signal_set(&ev_sigint, SIGINT, sockets_sighdlr, NULL); + signal_set(&ev_sigterm, SIGTERM, sockets_sighdlr, NULL); + signal_set(&ev_sighup, SIGHUP, sockets_sighdlr, NULL); + signal_set(&ev_sigpipe, SIGPIPE, sockets_sighdlr, NULL); + signal_set(&ev_sigusr1, SIGUSR1, sockets_sighdlr, NULL); + signal_add(&ev_sigint, NULL); + signal_add(&ev_sigterm, NULL); + signal_add(&ev_sighup, NULL); + signal_add(&ev_sigpipe, NULL); + signal_add(&ev_sigusr1, NULL); + if ((socks_iev_parent = calloc(1, sizeof(*socks_iev_parent))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(socks_iev_parent, fd_parent, sockets_dispatch_thingsd); + + if ((socks_iev_control = calloc(1, sizeof(*socks_iev_control))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(socks_iev_control, fd_control, + sockets_dispatch_control); + if (pledge("stdio inet recvfd", NULL) == -1) fatal("pledge"); + + event_dispatch(); + + sockets_shutdown(); } void @@ -113,12 +148,10 @@ sockets_launch(void) log_debug("%s: configuring thing listener %d (%d)", __func__, sock->conf.id, sock->fd); - socket_tls_init(sock); + if (socket_tls_init(sock) == -1) + fatalx("%s: tls init failed for listener %d", __func__, + sock->conf.id); - if ((sock->clients = calloc(1, sizeof(*sock->clients))) == NULL) - fatalx("%s: calloc", __func__); - TAILQ_INIT(sock->clients); - event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST, sockets_socket_conn, sock); @@ -138,7 +171,7 @@ sockets_privinit(struct socket *sock) log_debug("%s: initializing thing listener %d", __func__, sock->conf.id); - if ((sock->fd = sockets_create_socket(sock->conf.al, sock->conf.port, + if ((sock->fd = sockets_create_socket(&sock->conf.ss, sock->conf.port, S_TCP)) == -1) { log_warnx("%s: create sock socket failed", __func__); return (-1); @@ -147,205 +180,113 @@ sockets_privinit(struct socket *sock) return (0); } -int -sockets_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg) +void +sockets_dispatch_control(int fd, short event, void *arg) { - struct privsep *ps = p->p_ps; - int res = 0, cmd = 0; + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; - switch (imsg->hdr.type) { - default: - return (-1); + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost control process", __func__); + sockets_shutdown(); + } } - - switch (cmd) { - case 0: - break; - default: - if (proc_compose_imsg(ps, PROC_CONTROL, -1, cmd, - imsg->hdr.peerid, -1, &res, sizeof(res)) == -1) - return (-1); - break; + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost control process", __func__); + sockets_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); + } } - return (0); + /* control only ever listens to us, requests arrive via the parent */ + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + log_warnx("%s: unexpected imsg %d from control", __func__, + imsg.hdr.type); + imsg_free(&imsg); + } + imsg_event_add(iev); } +/* + * Every bound address gets its own listener. The first one for a thing is + * the parent, the rest point back at it so thingsctl can group them. + */ void sockets_parse_sockets(struct thingsd *env) { struct thing *thing; - struct socket *sock, *new_sock = NULL; + struct socket *sock, *parent; struct address *a; - int sock_id = 0, ipv4 = 0, ipv6 = 0; + int sock_id = 0, n; TAILQ_FOREACH(thing, env->things, entry) { - sock_id++; - new_sock = sockets_conf_new_socket(env, thing, sock_id, 0); - TAILQ_INSERT_TAIL(env->sockets, new_sock, entry); - - /* add ipv6 children */ - TAILQ_FOREACH(sock, env->sockets, entry) { - ipv4 = ipv6 = 0; - - TAILQ_FOREACH(a, sock->conf.al, entry) { - if (a->ss.ss_family == AF_INET) - ipv4 = 1; - if (a->ss.ss_family == AF_INET6) - ipv6 = 1; + parent = NULL; + n = 0; + TAILQ_FOREACH(a, thing->conf.tcp_al, entry) { + sock = sockets_conf_new_socket(env, thing, ++sock_id, + a, n++); + if (parent == NULL) { + parent = sock; + thing->conf.sock_id = sock->conf.id; + } else { + sock->conf.parent_id = parent->conf.id; + parent->conf.child_id = sock->conf.id; } - - /* create ipv6 sock */ - if (ipv4 == 1 && ipv6 == 1) { - sock_id++; - sock->conf.child_id = sock_id; - new_sock = sockets_conf_new_socket(env, thing, - sock_id, 1); - sockets_dup_new_socket(sock, new_sock); - TAILQ_INSERT_TAIL(env->sockets, new_sock, - entry); - continue; - } + TAILQ_INSERT_TAIL(env->sockets, sock, entry); } } } -void -sockets_dup_new_socket(struct socket *p_sock, struct socket *sock) -{ - struct address *a, *acp; - - sock->conf.parent_id = p_sock->conf.id; - sock->conf.thing_id = p_sock->conf.thing_id; - sock->conf.ipv4 = 0; - sock->conf.ipv6 = 1; - - if (strlcpy(sock->conf.password, p_sock->conf.password, - sizeof(sock->conf.password)) >= sizeof(sock->conf.password)) - fatalx("%s: strlcpy", __func__); - - sock->conf.port = p_sock->conf.port; - - sock->conf.tls = p_sock->conf.tls; - sock->conf.tls_protocols = p_sock->conf.tls_protocols; - sock->conf.tls_flags = p_sock->conf.tls_flags; - sock->conf.max_clients = p_sock->conf.max_clients; - - memcpy(&sock->conf.thing_name, p_sock->conf.thing_name, - sizeof(sock->conf.thing_name)); - - snprintf(sock->conf.name, THINGSD_MAXTEXT, "%s_child", - p_sock->conf.thing_name); - - TAILQ_FOREACH(a, p_sock->conf.al, entry) { - if (a->ss.ss_family == AF_INET) - continue; - - if ((acp = calloc(1, sizeof(*acp))) == NULL) - fatal("%s: calloc", __func__); - memcpy(&acp->ss, &a->ss, sizeof(acp->ss)); - acp->ipproto = a->ipproto; - acp->prefixlen = a->prefixlen; - acp->port = a->port; - if (strlen(a->ifname) != 0) { - if (strlcpy(acp->ifname, a->ifname, - sizeof(acp->ifname)) >= sizeof(acp->ifname)) { - fatalx("%s: interface name truncated", - __func__); - } - } - - TAILQ_INSERT_TAIL(sock->conf.al, acp, entry); - } - - if(sock->conf.tls == 0) - return; - - if((sock->conf.tls_cert_file = - strdup(p_sock->conf.tls_cert_file)) == NULL) - fatal("%s: strdup", __func__); - - if((sock->conf.tls_key_file = - strdup(p_sock->conf.tls_key_file)) == NULL) - fatal("%s: strdup", __func__); - - if (strlcpy(sock->conf.tls_ciphers, p_sock->conf.tls_ciphers, - sizeof(sock->conf.tls_ciphers)) >= sizeof(sock->conf.tls_ciphers)) - fatalx("%s: strlcpy", __func__); - - if (strlcpy(sock->conf.tls_dhe_params, p_sock->conf.tls_dhe_params, - sizeof(sock->conf.tls_dhe_params)) >= - sizeof(sock->conf.tls_dhe_params)) - fatalx("%s: strlcpy", __func__); - - if (strlcpy(sock->conf.tls_ecdhe_curves, p_sock->conf.tls_ecdhe_curves, - sizeof(sock->conf.tls_ecdhe_curves)) >= - sizeof(sock->conf.tls_ecdhe_curves)) - fatalx("%s: strlcpy", __func__); -} - struct socket * sockets_conf_new_socket(struct thingsd *env, struct thing *thing, int id, - int is_dup) + struct address *a, int n) { struct socket *sock; - struct address *a, *acp; if ((sock = calloc(1, sizeof(*sock))) == NULL) fatal("%s: calloc", __func__); - if ((sock->conf.al = calloc(1, - sizeof(*sock->conf.al))) == NULL) + if ((sock->clients = calloc(1, sizeof(*sock->clients))) == NULL) fatal("%s: calloc", __func__); + TAILQ_INIT(sock->clients); - TAILQ_INIT(sock->conf.al); - - sock->conf.parent_id = 0; + sock->fd = -1; sock->conf.id = id; - sock->conf.ipv4 = 1; sock->conf.thing_id = thing->conf.id; sock->conf.max_clients = thing->conf.max_clients; - memset(sock->conf.tls_ciphers, 0, - sizeof(sock->conf.tls_ciphers)); - memset(sock->conf.tls_dhe_params, 0, - sizeof(sock->conf.tls_dhe_params)); - memset(sock->conf.tls_ecdhe_curves, 0, - sizeof(sock->conf.tls_ecdhe_curves)); + memcpy(&sock->conf.ss, &a->ss, sizeof(sock->conf.ss)); + sock->conf.ipv4 = (a->ss.ss_family == AF_INET); + sock->conf.ipv6 = (a->ss.ss_family == AF_INET6); - if (is_dup) - goto done; + if (strlcpy(sock->conf.iface, a->ifname, sizeof(sock->conf.iface)) >= + sizeof(sock->conf.iface)) + fatalx("%s: interface name truncated", __func__); - snprintf(sock->conf.name, THINGSD_MAXTEXT, "%s_parent", - thing->conf.name); + if (n == 0) + snprintf(sock->conf.name, sizeof(sock->conf.name), "%s_parent", + thing->conf.name); + else + snprintf(sock->conf.name, sizeof(sock->conf.name), "%s_child%d", + thing->conf.name, n); if (strlcpy(sock->conf.thing_name, thing->conf.name, - sizeof(sock->conf.thing_name)) >= sizeof(sock->conf.thing_name)) { - free(sock->conf.al); - free(sock); + sizeof(sock->conf.thing_name)) >= sizeof(sock->conf.thing_name)) fatalx("%s: strlcpy", __func__); - } - TAILQ_FOREACH(a, thing->conf.tcp_al, entry) { - if ((acp = calloc(1, sizeof(*acp))) == NULL) - fatal("%s: calloc", __func__); - memcpy(&acp->ss, &a->ss, sizeof(acp->ss)); - acp->ipproto = a->ipproto; - acp->prefixlen = a->prefixlen; - acp->port = a->port; - if (strlen(a->ifname) != 0) { - if (strlcpy(acp->ifname, a->ifname, - sizeof(acp->ifname)) >= sizeof(acp->ifname)) { - fatalx("%s: interface name truncated", - __func__); - } - } - - TAILQ_INSERT_TAIL(sock->conf.al, acp, entry); - } - - thing->conf.sock_id = id; - if (strlcpy(sock->conf.password, thing->conf.password, sizeof(sock->conf.password)) >= sizeof(sock->conf.password)) fatalx("%s: strlcpy", __func__); @@ -356,17 +297,31 @@ sockets_conf_new_socket(struct thingsd *env, struct th sock->conf.tls_protocols = thing->conf.tls_protocols; sock->conf.tls_flags = thing->conf.tls_flags; - if(sock->conf.tls == 0) - goto done; + if (sock->conf.tls == 0) + return (sock); - if((sock->conf.tls_cert_file = + if ((sock->conf.tls_cert_file = strdup(thing->conf.tls_cert_file)) == NULL) fatal("%s: strdup", __func__); - if((sock->conf.tls_key_file = + if ((sock->conf.tls_key_file = strdup(thing->conf.tls_key_file)) == NULL) fatal("%s: strdup", __func__); + if (thing->conf.tls_ca_file != NULL && + (sock->conf.tls_ca_file = strdup(thing->conf.tls_ca_file)) == NULL) + fatal("%s: strdup", __func__); + + if (thing->conf.tls_crl_file != NULL && + (sock->conf.tls_crl_file = + strdup(thing->conf.tls_crl_file)) == NULL) + fatal("%s: strdup", __func__); + + if (thing->conf.tls_ocsp_staple_file != NULL && + (sock->conf.tls_ocsp_staple_file = + strdup(thing->conf.tls_ocsp_staple_file)) == NULL) + fatal("%s: strdup", __func__); + if (strlcpy(sock->conf.tls_ciphers, thing->conf.tls_ciphers, sizeof(sock->conf.tls_ciphers)) >= sizeof(sock->conf.tls_ciphers)) fatalx("%s: strlcpy", __func__); @@ -380,7 +335,7 @@ sockets_conf_new_socket(struct thingsd *env, struct th sizeof(sock->conf.tls_ecdhe_curves)) >= sizeof(sock->conf.tls_ecdhe_curves)) fatalx("%s: strlcpy", __func__); -done: + return (sock); } @@ -406,136 +361,101 @@ sockets_socket_af(struct sockaddr_storage *ss, in_port } int -sockets_create_socket(struct addresslist *al, in_port_t port, int type) +sockets_create_socket(struct sockaddr_storage *ss, in_port_t port, int type) { - struct addrinfo hints; - struct address *a; - int fd = -1, o_val = 1, flags; + char hbuf[NI_MAXHOST]; + int fd, o_val = 1, socktype; + if (sockets_socket_af(ss, port) == -1) { + log_warnx("%s: unsupported address family %d", __func__, + ss->ss_family); + return (-1); + } + + socktype = (type == S_UDP) ? SOCK_DGRAM : SOCK_STREAM; + + if ((fd = socket(ss->ss_family, socktype | SOCK_NONBLOCK, 0)) == -1) { + log_warn("%s: socket", __func__); + return (-1); + } + + if (getnameinfo((struct sockaddr *)ss, ss->ss_len, hbuf, sizeof(hbuf), + NULL, 0, NI_NUMERICHOST) != 0) + strlcpy(hbuf, "?", sizeof(hbuf)); + log_debug("%s: opening socket (%d) on %s port %d", __func__, fd, hbuf, + ntohs(port)); + + if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val, + sizeof(o_val)) == -1) { + log_warn("%s: setsockopt", __func__); + close(fd); + return (-1); + } + + if (bind(fd, (struct sockaddr *)ss, ss->ss_len) == -1) { + log_warn("%s: can't bind %s port %d", __func__, hbuf, + ntohs(port)); + close(fd); + return (-1); + } + + if (type == S_TCP && listen(fd, SOMAXCONN) == -1) { + log_warn("%s: unable to listen on %s port %d", __func__, hbuf, + ntohs(port)); + close(fd); + return (-1); + } + + return (fd); +} + +/* + * Start a non-blocking connect to a thing. The caller learns about the + * outcome through the fd becoming writable (or erroring), so the event + * loop is never stalled by an unreachable host. + */ +int +sockets_open_client(const char *host, in_port_t port) +{ + struct addrinfo hints, *res0, *res; + char serv[NI_MAXSERV]; + int fd = -1, error, save_errno; + + snprintf(serv, sizeof(serv), "%u", ntohs(port)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; - switch (type) { - case S_UDP: - hints.ai_socktype = SOCK_DGRAM; - hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; - hints.ai_protocol = 0; - break; - case S_TCP: - default: - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags |= AI_PASSIVE; - break; - } + hints.ai_socktype = SOCK_STREAM; - TAILQ_FOREACH(a, al, entry) { - if (sockets_socket_af(&a->ss, port) == -1) { - log_warnx("%s: sockets_socket_af", __func__); - goto fail; - } - - fd = socket(a->ss.ss_family, hints.ai_socktype, - a->ipproto); - log_debug("%s: opening socket (%d) for %s", __func__, - fd, a->ifname); - - if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val, - sizeof(int)) == -1) { - log_warn("%s: setsockopt error", __func__); - return (-1); - } - - /* non-blocking */ - flags = fcntl(fd, F_GETFL); - flags |= O_NONBLOCK; - fcntl(fd, F_SETFL, flags); - - if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) { - close(fd); - log_info("%s: can't bind to port %d", __func__, - ntohs(port)); - goto fail; - } - if (type == S_TCP) { - if (listen(fd, SOMAXCONN) == -1) { - log_warn("%s, unable to listen on socket", - __func__); - goto fail; - } - } - } - - free(a); - return (fd); -fail: - free(a); - return (-1); -} - -int -sockets_open_client(char *ip_addr, in_port_t port) -{ - int fd = -1; - - struct addresslist al; - struct address *a; - - TAILQ_INIT(&al); - if (host(ip_addr, &al, 1, port, ip_addr, -1) <= 0) { - log_warnx("invalid listen ip: %s", ip_addr); + if ((error = getaddrinfo(host, serv, &hints, &res0)) != 0) { + log_warnx("%s: %s: %s", __func__, host, gai_strerror(error)); return (-1); } - TAILQ_FOREACH(a, &al, entry) { - if (sockets_socket_af(&a->ss, port) == -1) { - log_warnx("%s: sockets_socket_af", __func__); - return (-1); - } - - if ((fd = socket(a->ss.ss_family, SOCK_STREAM, - a->ipproto)) == -1) + for (res = res0; res != NULL; res = res->ai_next) { + if ((fd = socket(res->ai_family, + res->ai_socktype | SOCK_NONBLOCK, res->ai_protocol)) == -1) continue; - if ((connect(fd, (struct sockaddr *)&a->ss, - a->ss.ss_len)) == -1) { - if (errno != EINPROGRESS) { - close(fd); - return (-1); - } - } + if (connect(fd, res->ai_addr, res->ai_addrlen) == 0 || + errno == EINPROGRESS) + break; + + save_errno = errno; + close(fd); + fd = -1; + errno = save_errno; } if (fd == -1) - log_warnx("can't connect ip: %s", ip_addr); + log_warn("%s: can't connect to %s port %u", __func__, host, + ntohs(port)); - free(a); - return fd; + freeaddrinfo(res0); + return (fd); } struct socket * -sockets_new_socket(char *port) -{ - struct socket *sock; - - if ((sock = calloc(1, sizeof(*sock))) == NULL) - fatal("%s: calloc", __func__); - - return (sock); -}; - -struct socket * -sockets_get_socket(struct thingsd *env, int val) -{ - struct socket *sock = NULL; - - TAILQ_FOREACH(sock, env->sockets, entry) { - if (sock->fd == val) - return sock; - } - - return NULL; -} - -struct socket * sockets_get_socket_byid(struct thingsd *env, int val) { struct socket *sock = NULL; @@ -548,6 +468,20 @@ sockets_get_socket_byid(struct thingsd *env, int val) return NULL; } +/* subscribed clients across every listener of a thing in this process */ +size_t +sockets_thing_client_cnt(int id) +{ + struct socket *sock; + size_t cnt = 0; + + TAILQ_FOREACH(sock, thingsd_env->sockets, entry) + if (sock->conf.thing_id == id) + cnt += sock->client_cnt; + + return (cnt); +} + void sockets_write_to_clients(struct package *package) { @@ -555,96 +489,84 @@ sockets_write_to_clients(struct package *package) struct client *client = NULL; TAILQ_FOREACH(sock, thingsd_env->sockets, entry) { - if (sock->conf.thing_id == package->thing_id && - sock->client_cnt > 0) { - TAILQ_FOREACH(client, sock->clients, entry) { - if (client->subscribed) { - bufferevent_write(client->bev, - package->pkt, package->len); - } - } + if (sock->conf.thing_id != package->thing_id) + continue; + TAILQ_FOREACH(client, sock->clients, entry) { + if (client->subscribed) + bufferevent_write(client->bev, package->pkt, + package->len); } } } void -sockets_show_clients_info(struct privsep *ps, struct imsg *imsg) +sockets_show_clients_info(struct imsg *imsg) { - char filter[THINGSD_MAXNAME]; struct client *client, nci; struct socket *sock; + char filter[THINGSD_MAXNAME]; - memcpy(filter, imsg->data, sizeof(filter)); + memset(&nci, 0, sizeof(nci)); + imsg_get_string(imsg, filter, sizeof(filter)); TAILQ_FOREACH(sock, thingsd_env->sockets, entry) { - if (sock->client_cnt == 0) - continue; TAILQ_FOREACH(client, sock->clients, entry) { - if (filter[0] == '\0' || strcmp(filter, - client->name) == 0) { + if (filter[0] != '\0' && + strcmp(filter, client->name) != 0) + continue; - memcpy(&nci.name, client->name, - sizeof(nci.name)); + memcpy(nci.name, client->name, sizeof(nci.name)); - nci.subscribed = client->subscribed; - nci.fd = client->fd; - nci.port = client->port; - nci.tls = client->tls; + nci.subscribed = client->subscribed; + nci.fd = client->fd; + nci.port = client->port; + nci.tls = client->tls; - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_GET_INFO_CLIENTS_DATA, - imsg->hdr.peerid, -1, &nci, - sizeof(nci)) == -1) - return; - - } + if (imsg_compose_event(socks_iev_control, + IMSG_GET_INFO_CLIENTS_DATA, imsg->hdr.peerid, 0, + -1, &nci, sizeof(nci)) == -1) + return; } } - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_GET_INFO_CLIENTS_END_DATA, imsg->hdr.peerid, - -1, &nci, sizeof(nci)) == -1) - return; + imsg_compose_event(socks_iev_control, IMSG_GET_INFO_CLIENTS_END_DATA, + imsg->hdr.peerid, 0, -1, &nci, sizeof(nci)); } void -sockets_show_info(struct privsep *ps, struct imsg *imsg) +sockets_show_info(struct imsg *imsg) { - char filter[THINGSD_MAXNAME]; struct socket *socket, nsi; + char filter[THINGSD_MAXNAME]; - memcpy(filter, imsg->data, sizeof(filter)); + memset(&nsi, 0, sizeof(nsi)); + imsg_get_string(imsg, filter, sizeof(filter)); TAILQ_FOREACH(socket, thingsd_env->sockets, entry) { - if (filter[0] == '\0' || strcmp(filter, - socket->conf.name) == 0) { + if (filter[0] != '\0' && strcmp(filter, socket->conf.name) != 0) + continue; - memcpy(&nsi.conf.name, socket->conf.name, - sizeof(nsi.conf.name)); + memcpy(nsi.conf.name, socket->conf.name, + sizeof(nsi.conf.name)); - nsi.fd = socket->fd; - nsi.conf.port = socket->conf.port; - nsi.conf.tls = socket->conf.tls; - nsi.client_cnt = socket->client_cnt; - nsi.conf.max_clients = socket->conf.max_clients; + nsi.fd = socket->fd; + nsi.conf.port = socket->conf.port; + nsi.conf.tls = socket->conf.tls; + nsi.client_cnt = socket->client_cnt; + nsi.conf.max_clients = socket->conf.max_clients; - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_GET_INFO_SOCKETS_DATA, - imsg->hdr.peerid, -1, &nsi, - sizeof(nsi)) == -1) - return; - - } - } - - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_GET_INFO_SOCKETS_END_DATA, imsg->hdr.peerid, + if (imsg_compose_event(socks_iev_control, + IMSG_GET_INFO_SOCKETS_DATA, imsg->hdr.peerid, 0, -1, &nsi, sizeof(nsi)) == -1) return; + } + + imsg_compose_event(socks_iev_control, IMSG_GET_INFO_SOCKETS_END_DATA, + imsg->hdr.peerid, 0, -1, &nsi, sizeof(nsi)); } void -sockets_socket_rlimit(int maxfd) +sockets_socket_rlimit(void) { struct rlimit rl; @@ -657,22 +579,21 @@ sockets_socket_rlimit(int maxfd) * Allow the maximum number of open file descriptors for this * login class (which should be the class "daemon" by default). */ - if (maxfd == -1) - rl.rlim_cur = rl.rlim_max; - else - rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd); + rl.rlim_cur = rl.rlim_max; if (setrlimit(RLIMIT_NOFILE, &rl) == -1) fatal("%s: failed to set resource limit", __func__); } void -sockets_kill_client(struct privsep *ps, struct imsg *imsg) +sockets_kill_client(struct imsg *imsg) { struct socket *sock; struct client *client; - char client_name[THINGSD_MAXTEXT]; + char client_name[THINGSD_MAXNAME]; - memcpy(client_name, imsg->data, sizeof(client_name)); + imsg_get_string(imsg, client_name, sizeof(client_name)); + if (client_name[0] == '\0') + return; TAILQ_FOREACH(sock, thingsd_env->sockets, entry) { TAILQ_FOREACH(client, sock->clients, entry) { @@ -680,109 +601,118 @@ sockets_kill_client(struct privsep *ps, struct imsg *i log_debug("Control killed client: %s", client_name); client_del(client); - break; + return; } } } } -int -sockets_dispatch_thingsd(int fd, struct privsep_proc *p, struct imsg *imsg) +void +sockets_dispatch_thingsd(int fd, short event, void *arg) { - struct package *package = NULL; - struct privsep *ps = p->p_ps; - int res = 0, cmd = 0, verbose; - unsigned int mode; + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + struct package *package; + int n, verbose; - switch (imsg->hdr.type) { - case IMSG_KILL_CLIENT: - sockets_kill_client(ps, imsg); - break; - case IMSG_DIST_THING_PACKAGE: - IMSG_SIZE_CHECK(imsg, package); - package = (struct package *)imsg->data; - sockets_write_to_clients(package); - break; - case IMSG_CFG_SOCKS: - config_getsocks(thingsd_env, imsg); - break; - case IMSG_CFG_DONE: - config_getcfg(thingsd_env, imsg); - break; - case IMSG_CTL_RESET: - IMSG_SIZE_CHECK(imsg, &mode); - memcpy(&mode, imsg->data, sizeof(mode)); - - config_getreset(thingsd_env, imsg); - break; - case IMSG_CFG_TLS: - config_getsocks_tls(thingsd_env, imsg); - break; - case IMSG_CTL_VERBOSE: - IMSG_SIZE_CHECK(imsg, &verbose); - memcpy(&verbose, imsg->data, sizeof(verbose)); - log_setverbose(verbose); - break; - case IMSG_CTL_START: - sockets_launch(); - break; - case IMSG_GET_INFO_CLIENTS_REQUEST: - sockets_show_clients_info(ps, imsg); - break; - case IMSG_GET_INFO_SOCKETS_REQUEST: - sockets_show_info(ps, imsg); - break; - default: - return (-1); + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost parent process", __func__); + sockets_shutdown(); + } } - - switch (cmd) { - case 0: - break; - default: - if (proc_compose_imsg(ps, PROC_PARENT, -1, cmd, - imsg->hdr.peerid, -1, &res, sizeof(res)) == -1) - return (-1); - break; + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost parent process", __func__); + sockets_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); + } } - return (0); + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_KILL_CLIENT: + sockets_kill_client(&imsg); + break; + case IMSG_DIST_THING_PACKAGE: + IMSG_SIZE_CHECK(&imsg, package); + package = (struct package *)imsg.data; + if (package->len < 0 || + (size_t)package->len > sizeof(package->pkt)) + fatalx("%s: bad package length", __func__); + sockets_write_to_clients(package); + break; + case IMSG_CFG_SOCKS: + config_getsocks(thingsd_env, &imsg); + break; + case IMSG_CFG_DONE: + config_getcfg(thingsd_env, &imsg); + break; + case IMSG_CFG_TLS: + config_getsocks_tls(thingsd_env, &imsg); + break; + case IMSG_CTL_VERBOSE: + IMSG_SIZE_CHECK(&imsg, &verbose); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + break; + case IMSG_CTL_START: + sockets_launch(); + break; + case IMSG_GET_INFO_CLIENTS_REQUEST: + sockets_show_clients_info(&imsg); + break; + case IMSG_GET_INFO_SOCKETS_REQUEST: + sockets_show_info(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from parent", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); } void sockets_sighdlr(int sig, short event, void *arg) { switch (sig) { + case SIGINT: + case SIGTERM: + sockets_shutdown(); + break; + case SIGHUP: + case SIGPIPE: + case SIGUSR1: + /* ignore */ + break; default: - fatalx("unexpected signal"); + fatalx("%s: unexpected signal", __func__); } } -void +__dead void sockets_shutdown(void) { struct socket *sock = NULL, *tsock; struct client *client, *tclient; - /* clean up sockets */ TAILQ_FOREACH_SAFE(sock, thingsd_env->sockets, entry, tsock) { - /* clean up clients */ - TAILQ_FOREACH_SAFE(client, sock->clients, entry, tclient) { - if (client->tls) { - if (event_initialized(&client->ev)) - event_del(&client->ev); - tls_free(client->tls_ctx); - } else { - if (client->bev != NULL) - bufferevent_disable(client->bev, - EV_READ | EV_WRITE); - if (client->bev != NULL) - bufferevent_free(client->bev); - } - close(client->fd); - TAILQ_REMOVE(sock->clients, client, entry); - free(client); - } + TAILQ_FOREACH_SAFE(client, sock->clients, entry, tclient) + client_del(client); + free(sock->clients); if (sock->conf.tls) { tls_config_free(sock->tls_config); @@ -790,48 +720,44 @@ sockets_shutdown(void) free(sock->conf.tls_cert); free(sock->conf.tls_key); free(sock->conf.tls_ca); - free(sock->conf.tls_ca_file); free(sock->conf.tls_crl); - free(sock->conf.tls_crl_file); free(sock->conf.tls_ocsp_staple); - free(sock->conf.tls_ocsp_staple_file); } if (event_initialized(&sock->ev)) event_del(&sock->ev); - evtimer_del(&sock->pause); - close(sock->fd); + if (evtimer_initialized(&sock->pause)) + evtimer_del(&sock->pause); + if (sock->fd != -1) + close(sock->fd); TAILQ_REMOVE(thingsd_env->sockets, sock, entry); free(sock); } free(thingsd_env->sockets); -} + event_del(&socks_iev_parent->ev); + imsgbuf_clear(&socks_iev_parent->ibuf); + close(socks_iev_parent->ibuf.fd); + free(socks_iev_parent); + event_del(&socks_iev_control->ev); + imsgbuf_clear(&socks_iev_control->ibuf); + close(socks_iev_control->ibuf.fd); + free(socks_iev_control); -int -sockets_client_cmp(struct client *a, struct client *b) -{ - return ((int)a->id - b->id); + log_info("sockets exiting, pid %d", getpid()); + exit(0); } int sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen, - int reserve, volatile int *counter) + int reserve) { - int ret; - - if (getdtablecount() + reserve + - ((*counter + 1) * FD_NEEDED) >= getdtablesize()) { - log_debug("inflight fds exceeded"); + if (getdtablecount() + reserve >= getdtablesize()) { + log_debug("%s: descriptor table nearly full", __func__); errno = EMFILE; - return -1; + return (-1); } - if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC)) - > -1) { - (*counter)++; - log_debug("inflight incremented, now %d", *counter); - } - return ret; + return (accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC)); } void @@ -860,8 +786,6 @@ sockets_socket_conn(int fd, short event, void *arg) struct socket *sock = (struct socket *)arg; int client_fd; socklen_t len = sizeof(ss); - evbuffercb clientrd = client_rd; - evbuffercb clientwr = client_wr; backoff.tv_sec = 1; backoff.tv_usec = 0; @@ -869,95 +793,70 @@ sockets_socket_conn(int fd, short event, void *arg) subtime.tv_sec = CLIENT_SUB_TIME_SEC; subtime.tv_usec = CLIENT_SUB_TIME_USEC; - sock = sockets_get_socket(thingsd_env, fd); - if (sock == NULL) - return; - log_debug("%s: client connection started on listener %d", __func__, sock->conf.id); - event_add(&sock->ev, NULL); - client_fd = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len, - FD_RESERVE, &client_inflight); + FD_RESERVE); if (client_fd == -1) { switch (errno) { case EINTR: case EWOULDBLOCK: case ECONNABORTED: - return; + break; case EMFILE: case ENFILE: event_del(&sock->ev); evtimer_add(&sock->pause, &backoff); - return; + break; default: - log_warnx("client accept failed"); + log_warn("%s: accept", __func__); } + return; } - client = calloc(1, sizeof(*client)); - if (client == NULL) - goto err2; + if (sock->conf.max_clients > 0 && + sockets_thing_client_cnt(sock->conf.thing_id) >= + sock->conf.max_clients) { + log_debug("%s: listener %d max clients reached", __func__, + sock->conf.id); + close(client_fd); + return; + } + if ((client = calloc(1, sizeof(*client))) == NULL) + goto err; + + client->fd = client_fd; client->port = sock->conf.port; - - memset(&client->timeout, 0, sizeof(struct event)); - evtimer_set(&client->timeout, sockets_client_sub_timeout, client); - - if (sock->conf.tls) { - if (tls_accept_socket(sock->tls_ctx, &client->tls_ctx, - client_fd) == -1) { - log_warnx("tls accept failed: %s", - tls_error(sock->tls_ctx)); - goto err; - } - } - - client->packages = calloc(1, sizeof(*client->packages)); - if (client->packages == NULL) - goto err2; - - TAILQ_INIT(client->packages); - - if (sock->conf.max_clients > 0) - if (sock->client_cnt > sock->conf.max_clients) { - log_debug("%s: listener %d max clients reached", - __func__, sock->conf.id); - goto err; - } - client->sock_id = sock->conf.id; client->thing_id = sock->conf.thing_id; client->sock = sock; - client->sock_conf = &sock->conf; - client->subscribed = 0; - client->evb = evbuffer_new(); - - evtimer_pending(&client->timeout, NULL); - log_debug("%s: started client timer", __func__); - evtimer_add(&client->timeout, &subtime); - - if (client->evb == NULL) - goto err; - - client->fd = client_fd; client->join_time = time(NULL); + if ((client->packages = calloc(1, sizeof(*client->packages))) == NULL) + goto err; + TAILQ_INIT(client->packages); + if (sock->conf.tls) { + if (tls_accept_socket(sock->tls_ctx, &client->tls_ctx, + client_fd) == -1) { + log_warnx("tls accept failed: %s", + tls_error(sock->tls_ctx)); + goto err; + } client->tls = 1; - event_del(&client->ev); - event_set(&client->ev, client->fd, EV_READ | EV_PERSIST, - socket_tls_handshake, client); - event_add(&client->ev, NULL); } - client->bev = bufferevent_new(client->fd, clientrd, clientwr, + client->bev = bufferevent_new(client->fd, client_rd, client_wr, client_err, client); - if (client->bev == NULL) goto err; + /* + * libtls completes the handshake from within tls_read/tls_write, + * so the regular bufferevent callbacks are simply replaced. + */ if (client->tls) { event_set(&client->bev->ev_read, client->fd, EV_READ, sockets_tls_readcb, client->bev); @@ -966,21 +865,22 @@ sockets_socket_conn(int fd, short event, void *arg) } bufferevent_setwatermark(client->bev, EV_READ, 0, PKT_BUFF); - bufferevent_enable(client->bev, EV_READ|EV_WRITE); + bufferevent_enable(client->bev, EV_READ | EV_WRITE); + + evtimer_set(&client->timeout, sockets_client_sub_timeout, client); + evtimer_add(&client->timeout, &subtime); + TAILQ_INSERT_TAIL(sock->clients, client, entry); log_debug("%s: client connected (%d)", __func__, client->fd); return; err: - sock->client_cnt--; -err2: - log_debug("%s: client error", __func__); - client_inflight--; - if (client_fd != -1) - close(client_fd); + log_debug("%s: client setup error", __func__); + close(client_fd); if (client != NULL) { - if (sock->conf.tls) + if (client->tls) tls_free(client->tls_ctx); + free(client->packages); free(client); } } @@ -995,12 +895,11 @@ sockets_tls_readcb(int fd, short event, void *arg) size_t len; int toread = EVBUFFER_READ; - memset(pkt, 0, sizeof(pkt)); - - ret = tls_read(client->tls_ctx, pkt, PKT_BUFF); + ret = tls_read(client->tls_ctx, pkt, sizeof(pkt)); if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) { goto retry; } else if (ret < 0) { + log_debug("%s: %s", __func__, tls_error(client->tls_ctx)); toread |= EVBUFFER_ERROR; goto err; } @@ -1051,9 +950,6 @@ sockets_tls_writecb(int fd, short event, void *arg) int towrite = EVBUFFER_WRITE; if (EVBUFFER_LENGTH(bufev->output)) { - if (client == NULL) - return; - ret = tls_write(client->tls_ctx, EVBUFFER_DATA(bufev->output), EVBUFFER_LENGTH(bufev->output)); @@ -1061,6 +957,8 @@ sockets_tls_writecb(int fd, short event, void *arg) if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) { goto retry; } else if (ret < 0) { + log_debug("%s: %s", __func__, + tls_error(client->tls_ctx)); towrite |= EVBUFFER_ERROR; goto err; } @@ -1086,4 +984,3 @@ retry: err: (*bufev->errorcb)(bufev, towrite, bufev->cbarg); } - blob - 7f4a2ee4b47f6889c937c000e0c2d3ff05432d11 blob + 042078270683874257a4f044f8c7b7220e967c12 --- thingsd/thingsd.8 +++ thingsd/thingsd.8 @@ -1,5 +1,5 @@ .\" -.\" Copyright (c) 2016-2019 Tracey Emery +.\" Copyright (c) 2016-2026 Tracey Emery .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above @@ -132,4 +132,4 @@ program refactoring and rename began on The .Nm program was written by -.An Tracey Emery Aq Mt tracey@traceyemery.net . +.An Tracey Emery Aq Mt tracey@openbsd.org . blob - a97f89935b64e35507abe857e5432c61dace37d4 blob + 0a2f08cf8ae42a3f3b9fd2484c6b17552ce2812f --- thingsd/thingsd.c +++ thingsd/thingsd.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2019-2021 Tracey Emery + * Copyright (c) 2016, 2019-2026 Tracey Emery * Copyright (c) 2015 Reyk Floeter * * Permission to use, copy, modify, and distribute this software for any @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -41,7 +42,6 @@ #include #include -#include "proc.h" #include "thingsd.h" __dead void usage(void); @@ -49,153 +49,282 @@ __dead void usage(void); int main(int, char **); int thingsd_configure(struct thingsd *); -int thingsd_control_run(void); -int thingsd_dispatch_control(int, struct privsep_proc *, struct imsg *); -int thingsd_dispatch_sockets(int, struct privsep_proc *, struct imsg *); +void thingsd_dispatch_control(int, short, void *); +void thingsd_dispatch_sockets(int, short, void *); void thingsd_sighdlr(int sig, short event, void *arg); -void thingsd_shutdown(struct thingsd *); +__dead void + thingsd_shutdown(struct thingsd *); void thingsd_parent_shutdown(struct thingsd *); void thingsd_configure_done(struct thingsd *); -void thingsd_thing_serial_open(struct thing *, int); -void thingsd_thing_setup(struct thingsd *, struct thing *, int); -void thingsd_show_info(struct privsep *, struct imsg *); -void thingsd_show_thing_info(struct privsep *, struct imsg *); +int thingsd_thing_serial_open(struct thing *); +int thingsd_thing_connect(struct thingsd *, struct thing *, int); +void thingsd_thing_disconnect(struct thing *); +void thingsd_show_info(struct imsg *); +void thingsd_show_thing_info(struct imsg *); void thingsd_thing_udp_event(int, short, void *); void thingsd_thing_read(struct bufferevent *, void *); void thingsd_thing_write(struct bufferevent *, void *); void thingsd_thing_err(struct bufferevent *, short, void *); +void thingsd_oneshot_cb(int, short, void *); void thingsd_add_reconn(struct thing *); void thingsd_do_reconn(struct thingsd *); void thingsd_write_to_socks(struct packages *); void thingsd_write_to_things(struct package *); -void thingsd_echo_pkt(struct privsep *, struct imsg *); -void thingsd_stop_pkt(struct privsep *, struct imsg *); +void thingsd_echo_pkt(struct imsg *); +void thingsd_stop_pkt(struct imsg *); struct thing thingsd_compose_thing(struct thing *, enum imsg_type); -struct dead_thing - *thingsd_new_dead_thing(struct thing *); -extern enum privsep_procid privsep_process; +/* a packet on its way to a non-persistent thing */ +struct oneshot { + struct event ev; + int fd; + int len; + char pkt[PKT_BUFF]; + char name[THINGSD_MAXNAME]; +}; struct thingsd *thingsd_env; int thing_id; +struct imsgev *parent_iev_control; +struct imsgev *parent_iev_socks; -static struct privsep_proc procs[] = { - { "control", PROC_CONTROL, thingsd_dispatch_control, control }, - { "sockets", PROC_SOCKS, thingsd_dispatch_sockets, sockets, - sockets_shutdown }, -}; +static struct event ev_sigint; +static struct event ev_sigterm; +static struct event ev_sighup; +static struct event ev_sigpipe; +static struct event ev_sigusr1; +void +imsg_event_init(struct imsgev *iev, int fd, void (*handler)(int, short, + void *)) +{ + if (imsgbuf_init(&iev->ibuf, fd) == -1) + fatal("%s: imsgbuf_init", __func__); + imsgbuf_allow_fdpass(&iev->ibuf); + iev->handler = handler; + iev->events = EV_READ; + event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); + event_add(&iev->ev, NULL); +} + +void +imsg_event_add(struct imsgev *iev) +{ + iev->events = EV_READ; + if (imsgbuf_queuelen(&iev->ibuf) > 0) + iev->events |= EV_WRITE; + + event_del(&iev->ev); + event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); + event_add(&iev->ev, NULL); +} + int -thingsd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg) +imsg_compose_event(struct imsgev *iev, uint32_t type, uint32_t peerid, + pid_t pid, int fd, void *data, size_t datalen) { - struct privsep *ps = p->p_ps; - int res = 0, cmd = 0, verbose, exists = 0; - unsigned int v = 0; + int ret; + + if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, + datalen)) == -1) + return (ret); + imsg_event_add(iev); + return (ret); +} + +int +imsg_composev_event(struct imsgev *iev, uint32_t type, uint32_t peerid, + pid_t pid, int fd, const struct iovec *iov, int iovcnt) +{ + int ret; + + if ((ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, + iovcnt)) == -1) + return (ret); + imsg_event_add(iev); + return (ret); +} + +int +imsg_forward_event(struct imsgev *iev, struct imsg *imsg) +{ + return (imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid, + imsg->hdr.pid, -1, imsg->data, IMSG_DATA_SIZE(imsg))); +} + +/* + * Copy a string payload out of an imsg, bounded by both the payload and + * the destination so a short or unterminated message can't over-read. + */ +void +imsg_get_string(struct imsg *imsg, char *buf, size_t len) +{ + size_t n = IMSG_DATA_SIZE(imsg); + + if (n >= len) + n = len - 1; + if (n > 0) + memcpy(buf, imsg->data, n); + buf[n] = '\0'; +} + +void +thingsd_dispatch_control(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + struct thing *thing; + int i, n, res = 0, verbose, exists; + unsigned int v; char thing_name[THINGSD_MAXNAME]; - struct thing *thing; + char client_name[THINGSD_MAXNAME]; - switch (imsg->hdr.type) { - case IMSG_SHOW_PACKETS_END_DATA: - IMSG_SIZE_CHECK(imsg, &v); - if (imsg->data == NULL) - break; - thingsd_stop_pkt(ps, imsg); - break; - case IMSG_SHOW_PACKETS_REQUEST: - IMSG_SIZE_CHECK(imsg, &v); - if (imsg->data == NULL) - break; - memcpy(thing_name, imsg->data, sizeof(thing_name)); - TAILQ_FOREACH(thing, thingsd_env->things, entry) { - if (strcmp(thing->conf.name, thing_name) == 0) { - thingsd_echo_pkt(ps, imsg); - exists = 1; - break; + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost control process", __func__); + thingsd_shutdown(thingsd_env); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost control process", __func__); + thingsd_shutdown(thingsd_env); } + fatal("%s: imsgbuf_write", __func__); } - if (!exists) - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_BAD_THING, - imsg->hdr.peerid, -1, &res, sizeof(res)) == -1) - return (-1); - break; - case IMSG_KILL_CLIENT: - IMSG_SIZE_CHECK(imsg, &v); - if (imsg->data == NULL) - break; - proc_forward_imsg(ps, imsg, PROC_SOCKS, -1); - break; - case IMSG_GET_INFO_CLIENTS_REQUEST: - proc_forward_imsg(ps, imsg, PROC_SOCKS, -1); - break; - case IMSG_GET_INFO_SOCKETS_REQUEST: - proc_forward_imsg(ps, imsg, PROC_SOCKS, -1); - break; - case IMSG_GET_INFO_THINGS_REQUEST: - case IMSG_GET_INFO_THINGS_REQUEST_ROOT: - thingsd_show_thing_info(ps, imsg); - break; - case IMSG_GET_INFO_THINGSD_REQUEST: - thingsd_show_info(ps, imsg); - break; - case IMSG_CTL_VERBOSE: - IMSG_SIZE_CHECK(imsg, &verbose); - memcpy(&verbose, imsg->data, sizeof(verbose)); - log_setverbose(verbose); - proc_forward_imsg(ps, imsg, PROC_SOCKS, -1); - break; - default: - return (-1); } - switch (cmd) { - case 0: - break; - default: - if (proc_compose_imsg(ps, PROC_CONTROL, -1, cmd, - imsg->hdr.peerid, -1, &res, sizeof(res)) == -1) - return (-1); - break; - } + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; - return (0); + switch (imsg.hdr.type) { + case IMSG_SHOW_PACKETS_END_DATA: + IMSG_SIZE_CHECK(&imsg, &v); + if (imsg.data == NULL) + break; + thingsd_stop_pkt(&imsg); + break; + case IMSG_SHOW_PACKETS_REQUEST: + exists = 0; + imsg_get_string(&imsg, thing_name, sizeof(thing_name)); + TAILQ_FOREACH(thing, thingsd_env->things, entry) { + if (strcmp(thing->conf.name, thing_name) == 0) { + thingsd_echo_pkt(&imsg); + exists = 1; + break; + } + } + if (!exists) + imsg_compose_event(parent_iev_control, + IMSG_BAD_THING, imsg.hdr.peerid, 0, -1, + &res, sizeof(res)); + break; + case IMSG_KILL_CLIENT: + imsg_get_string(&imsg, client_name, + sizeof(client_name)); + if (client_name[0] == '\0') + break; + /* FALLTHROUGH */ + case IMSG_GET_INFO_CLIENTS_REQUEST: + case IMSG_GET_INFO_SOCKETS_REQUEST: + for (i = 0; i < thingsd_env->prefork_socks; i++) + imsg_forward_event(&parent_iev_socks[i], &imsg); + break; + case IMSG_GET_INFO_THINGS_REQUEST: + case IMSG_GET_INFO_THINGS_REQUEST_ROOT: + thingsd_show_thing_info(&imsg); + break; + case IMSG_GET_INFO_THINGSD_REQUEST: + thingsd_show_info(&imsg); + break; + case IMSG_CTL_VERBOSE: + IMSG_SIZE_CHECK(&imsg, &verbose); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + for (i = 0; i < thingsd_env->prefork_socks; i++) + imsg_forward_event(&parent_iev_socks[i], &imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from control", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); } -int -thingsd_dispatch_sockets(int fd, struct privsep_proc *p, struct imsg *imsg) +void +thingsd_dispatch_sockets(int fd, short event, void *arg) { - struct package *package = NULL; - struct privsep *ps = p->p_ps; - struct thingsd *env = ps->ps_env; + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + struct package *package; + int n; - switch (imsg->hdr.type) { - case IMSG_DIST_CLIENT_PACKAGE: - IMSG_SIZE_CHECK(imsg, package); - package = (struct package *)imsg->data; - thingsd_write_to_things(package); - break; - case IMSG_CFG_DONE: - thingsd_configure_done(env); - break; - default: - return (-1); + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost sockets process", __func__); + thingsd_shutdown(thingsd_env); + } } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost sockets process", __func__); + thingsd_shutdown(thingsd_env); + } + fatal("%s: imsgbuf_write", __func__); + } + } - return (0); + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_DIST_CLIENT_PACKAGE: + IMSG_SIZE_CHECK(&imsg, package); + package = (struct package *)imsg.data; + if (package->len < 0 || + (size_t)package->len > sizeof(package->pkt)) + fatalx("%s: bad package length", __func__); + thingsd_write_to_things(package); + break; + case IMSG_CFG_DONE: + thingsd_configure_done(thingsd_env); + break; + default: + log_warnx("%s: unexpected imsg %d from sockets", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); } void thingsd_sighdlr(int sig, short event, void *arg) { - struct privsep *ps = arg; + struct thingsd *env = arg; - if (privsep_process != PROC_PARENT) - return; - switch (sig) { case SIGHUP: log_info("%s: ignoring SIGHUP", __func__); @@ -208,7 +337,7 @@ thingsd_sighdlr(int sig, short event, void *arg) break; case SIGTERM: case SIGINT: - thingsd_shutdown(ps->ps_env); + thingsd_shutdown(env); break; default: fatalx("unexpected signal"); @@ -227,19 +356,18 @@ int main(int argc, char **argv) { struct thingsd *env; - struct privsep *ps; struct timeval eb_timeout; - const char *errp, *title = NULL; const char *conffile = THINGSD_CONF; - int proc_instance = 0; - int ch; - int argc0 = argc; - enum privsep_procid proc_id = PROC_PARENT; + int ch, i, n; + int pipe_control[2]; + int pipe_socks[THINGSD_MAX_PREFORK][2]; + int pipe_control_socks[THINGSD_MAX_PREFORK][2]; + int fd_socks[THINGSD_MAX_PREFORK]; if ((env = calloc(1, sizeof(*env))) == NULL) fatal("%s: calloc", __func__); - while ((ch = getopt(argc, argv, "D:P:I:df:vn")) != -1) { + while ((ch = getopt(argc, argv, "D:df:vn")) != -1) { switch (ch) { case 'D': if (cmdline_symset(optarg) < 0) @@ -259,18 +387,6 @@ main(int argc, char **argv) env->thingsd_debug = 2; env->thingsd_noaction = 1; break; - case 'P': - title = optarg; - proc_id = proc_getid(procs, nitems(procs), title); - if (proc_id == PROC_MAX) - fatalx("invalid process name"); - break; - case 'I': - proc_instance = strtonum(optarg, 0, - PROC_MAX_INSTANCES, &errp); - if (errp) - fatalx("invalid process instance"); - break; default: usage(); } @@ -283,13 +399,7 @@ main(int argc, char **argv) if (argc > 0) usage(); - if ((ps = calloc(1, sizeof(*ps))) == NULL) - fatal("%s: calloc", __func__); - thingsd_env = env; - env->thingsd_ps = ps; - ps->ps_env = env; - TAILQ_INIT(&ps->ps_rcsocks); env->thingsd_conffile = conffile; if (parse_config(env->thingsd_conffile, env) == -1) @@ -304,51 +414,109 @@ main(int argc, char **argv) fatalx("need root privileges"); } - if ((ps->ps_pw = getpwnam(THINGSD_USER)) == NULL) + if (getpwnam(THINGSD_USER) == NULL) fatal("unknown user %s", THINGSD_USER); - /* Configure the control socket */ - ps->ps_csock.cs_name = THINGSD_SOCKET; + if (env->thingsd_noaction) { + fprintf(stderr, "configuration OK\n"); + exit(0); + } log_init(env->thingsd_debug, LOG_DAEMON); log_setverbose(env->thingsd_verbose); - - if (env->thingsd_noaction) - ps->ps_noaction = 1; - - ps->ps_instances[PROC_SOCKS] = env->prefork_socks; - ps->ps_instance = proc_instance; - if (title != NULL) - ps->ps_title[proc_id] = title; - - /* only the thingsd returns */ - proc_init(ps, procs, nitems(procs), env->thingsd_debug, argc0, - argv, proc_id); - log_procinit("thingsd"); + if (!env->thingsd_debug && daemon(0, 0) == -1) fatal("can't daemonize"); - if (ps->ps_noaction == 0) - log_info("%s startup", getprogname()); + log_info("%s startup", getprogname()); + /* + * All pipes are created before forking so the children inherit + * them and no descriptor passing is needed to wire the processes + * together. Each child closes the ends it does not own. + */ + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_control) == -1) + fatal("%s: socketpair", __func__); + for (i = 0; i < env->prefork_socks; i++) { + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_socks[i]) == -1) + fatal("%s: socketpair", __func__); + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_control_socks[i]) == -1) + fatal("%s: socketpair", __func__); + } + + switch (fork()) { + case -1: + fatal("%s: fork", __func__); + case 0: + close(pipe_control[0]); + for (i = 0; i < env->prefork_socks; i++) { + close(pipe_socks[i][0]); + close(pipe_socks[i][1]); + close(pipe_control_socks[i][1]); + fd_socks[i] = pipe_control_socks[i][0]; + } + control(pipe_control[1], fd_socks); + /* NOTREACHED */ + } + + for (n = 0; n < env->prefork_socks; n++) { + switch (fork()) { + case -1: + fatal("%s: fork", __func__); + case 0: + close(pipe_control[0]); + close(pipe_control[1]); + for (i = 0; i < env->prefork_socks; i++) { + close(pipe_socks[i][0]); + close(pipe_control_socks[i][0]); + if (i == n) + continue; + close(pipe_socks[i][1]); + close(pipe_control_socks[i][1]); + } + sockets(pipe_socks[n][1], pipe_control_socks[n][1]); + /* NOTREACHED */ + } + } + + close(pipe_control[1]); + for (i = 0; i < env->prefork_socks; i++) { + close(pipe_socks[i][1]); + close(pipe_control_socks[i][0]); + close(pipe_control_socks[i][1]); + } + env->thingsd_eb = event_init(); - signal_set(&ps->ps_evsigint, SIGINT, thingsd_sighdlr, ps); - signal_set(&ps->ps_evsigterm, SIGTERM, thingsd_sighdlr, ps); - signal_set(&ps->ps_evsighup, SIGHUP, thingsd_sighdlr, ps); - signal_set(&ps->ps_evsigpipe, SIGPIPE, thingsd_sighdlr, ps); - signal_set(&ps->ps_evsigusr1, SIGUSR1, thingsd_sighdlr, ps); + signal_set(&ev_sigint, SIGINT, thingsd_sighdlr, env); + signal_set(&ev_sigterm, SIGTERM, thingsd_sighdlr, env); + signal_set(&ev_sighup, SIGHUP, thingsd_sighdlr, env); + signal_set(&ev_sigpipe, SIGPIPE, thingsd_sighdlr, env); + signal_set(&ev_sigusr1, SIGUSR1, thingsd_sighdlr, env); - signal_add(&ps->ps_evsigint, NULL); - signal_add(&ps->ps_evsigterm, NULL); - signal_add(&ps->ps_evsighup, NULL); - signal_add(&ps->ps_evsigpipe, NULL); - signal_add(&ps->ps_evsigusr1, NULL); + signal_add(&ev_sigint, NULL); + signal_add(&ev_sigterm, NULL); + signal_add(&ev_sighup, NULL); + signal_add(&ev_sigpipe, NULL); + signal_add(&ev_sigusr1, NULL); - if (!env->thingsd_noaction) - proc_connect(ps); + if ((parent_iev_control = calloc(1, sizeof(*parent_iev_control))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(parent_iev_control, pipe_control[0], + thingsd_dispatch_control); + if ((parent_iev_socks = calloc(env->prefork_socks, + sizeof(*parent_iev_socks))) == NULL) + fatal("%s: calloc", __func__); + for (i = 0; i < env->prefork_socks; i++) + imsg_event_init(&parent_iev_socks[i], pipe_socks[i][0], + thingsd_dispatch_sockets); + if (thingsd_configure(env) == -1) fatalx("configuration failed"); @@ -356,8 +524,7 @@ main(int argc, char **argv) eb_timeout.tv_sec = EB_TIMEOUT; eb_timeout.tv_usec = 0; - if (unveil(THINGSD_CONF, "r") == -1) - err(1, "unveil"); + /* serial things are reopened from here on, nothing else is */ if (unveil("/dev", "rw") == -1) err(1, "unveil"); if (unveil(NULL, NULL) != 0) @@ -368,7 +535,7 @@ main(int argc, char **argv) env->run = 1; while (env->run) { - if (env->exists) + if (!TAILQ_EMPTY(env->dead_things)) thingsd_do_reconn(env); event_base_loopexit(env->thingsd_eb, &eb_timeout); event_base_dispatch(env->thingsd_eb); @@ -384,37 +551,25 @@ thingsd_configure(struct thingsd *env) { struct thing *thing; struct socket *sock; - unsigned int id; + int n; - if (env->thingsd_noaction) { - fprintf(stderr, "configuration OK\n"); - proc_kill(env->thingsd_ps); - exit(0); - } - env->socks_reload = env->prefork_socks; /* setup our things, which run privileged */ - TAILQ_FOREACH(thing, env->things, entry) { - /* setup serial things */ - thingsd_thing_serial_open(thing, 0); + TAILQ_FOREACH(thing, env->things, entry) + thingsd_thing_connect(env, thing, 0); - /* setup udp/tcp things */ - thingsd_thing_setup(env, thing, 0); - } - - TAILQ_FOREACH(sock, env->sockets, entry) { socket_tls_load(sock); if (config_setsocks(env, sock) == -1) fatalx("%s: send socket error", __func__); } - for (id = 0; id < PROC_MAX; id++) { - if (id == privsep_process) - continue; - proc_compose(env->thingsd_ps, id, IMSG_CFG_DONE, NULL, 0); - } + imsg_compose_event(parent_iev_control, IMSG_CFG_DONE, 0, 0, -1, + NULL, 0); + for (n = 0; n < env->prefork_socks; n++) + imsg_compose_event(&parent_iev_socks[n], IMSG_CFG_DONE, 0, 0, + -1, NULL, 0); return (0); } @@ -422,7 +577,8 @@ thingsd_configure(struct thingsd *env) void thingsd_configure_done(struct thingsd *env) { - unsigned int id; + int n; + if (env->socks_reload == 0) { log_warnx("%s: configuration already finished", __func__); return; @@ -430,21 +586,45 @@ thingsd_configure_done(struct thingsd *env) env->socks_reload--; if (env->socks_reload == 0) { - for (id = 0; id < PROC_MAX; id++) { - if (id == privsep_process) - continue; - proc_compose(env->thingsd_ps, id, IMSG_CTL_START, - NULL, 0); - } + imsg_compose_event(parent_iev_control, IMSG_CTL_START, 0, 0, + -1, NULL, 0); + for (n = 0; n < env->prefork_socks; n++) + imsg_compose_event(&parent_iev_socks[n], + IMSG_CTL_START, 0, 0, -1, NULL, 0); } } -void +__dead void thingsd_shutdown(struct thingsd *env) { - proc_kill(env->thingsd_ps); - control_cleanup(&env->thingsd_ps->ps_csock); + pid_t pid; + int n, status; + /* closing the pipes tells the children to exit */ + event_del(&parent_iev_control->ev); + imsgbuf_clear(&parent_iev_control->ibuf); + close(parent_iev_control->ibuf.fd); + for (n = 0; n < env->prefork_socks; n++) { + event_del(&parent_iev_socks[n].ev); + imsgbuf_clear(&parent_iev_socks[n].ibuf); + close(parent_iev_socks[n].ibuf.fd); + } + free(parent_iev_control); + free(parent_iev_socks); + + log_debug("waiting for children to terminate"); + do { + pid = wait(&status); + if (pid == -1) { + if (errno != EINTR && errno != ECHILD) + fatal("wait"); + } else if (WIFSIGNALED(status)) + log_warnx("lost child: pid %d terminated; signal %d", + pid, WTERMSIG(status)); + else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) + log_warnx("lost child: pid %d exited abnormally", pid); + } while (pid != -1 || (pid == -1 && errno == EINTR)); + thingsd_parent_shutdown(env); log_warnx("thingsd terminating"); @@ -452,20 +632,20 @@ thingsd_shutdown(struct thingsd *env) } void -thingsd_show_info(struct privsep *ps, struct imsg *imsg) +thingsd_show_info(struct imsg *imsg) { struct thingsd_thingsd_info npi; switch (imsg->hdr.type) { case IMSG_GET_INFO_THINGSD_REQUEST: npi.verbose = log_getverbose(); - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_GET_INFO_THINGSD_DATA, imsg->hdr.peerid, - -1, &npi, sizeof(npi)) == -1) + if (imsg_compose_event(parent_iev_control, + IMSG_GET_INFO_THINGSD_DATA, imsg->hdr.peerid, 0, -1, + &npi, sizeof(npi)) == -1) return; - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_GET_INFO_THINGSD_END_DATA, imsg->hdr.peerid, - -1, &npi, sizeof(npi)) == -1) + if (imsg_compose_event(parent_iev_control, + IMSG_GET_INFO_THINGSD_END_DATA, imsg->hdr.peerid, 0, -1, + &npi, sizeof(npi)) == -1) return; break; default: @@ -482,6 +662,7 @@ thingsd_parent_shutdown(struct thingsd *env) struct package *package, *tpkg; struct dead_thing *dead_thing, *tdead_thing; struct packet_client *pclt, *tpclt; + int n; log_debug("thingsd parent shutting down"); @@ -519,7 +700,8 @@ thingsd_parent_shutdown(struct thingsd *env) } if (event_initialized(&sock->ev)) event_del(&sock->ev); - close(sock->fd); + if (sock->fd != -1) + close(sock->fd); TAILQ_REMOVE(env->sockets, sock, entry); free(sock); } @@ -540,12 +722,12 @@ thingsd_parent_shutdown(struct thingsd *env) free(thing->conf.tls_ocsp_staple_file); } - if (event_initialized(&thing->udp_ev)) - event_del(&thing->udp_ev); + for (n = 0; n < thing->udp_nfd; n++) { + event_del(&thing->udp_ev[n]); + close(thing->udp_fd[n]); + } if (thing->bev != NULL) - bufferevent_disable(thing->bev, EV_READ | EV_WRITE); - if (thing->bev != NULL) bufferevent_free(thing->bev); if (thing->fd != -1) @@ -565,10 +747,10 @@ thingsd_conf_new_thing(struct thingsd *env, struct thi { struct thing *thing; - /* check if thing already exists */ + /* duplicate names are a config error */ TAILQ_FOREACH(thing, env->things, entry) { if (strcmp(name, thing->conf.name) == 0) - return (thing); + return (NULL); } if ((thing = calloc(1, sizeof(*thing))) == NULL) @@ -576,263 +758,297 @@ thingsd_conf_new_thing(struct thingsd *env, struct thi thing->conf.ipv4 = 1; thing->conf.id = id; + thing->fd = -1; TAILQ_INSERT_TAIL(env->things, thing, entry); return (thing); } -void -thingsd_thing_serial_open(struct thing *thing, int reconn) +int +thingsd_thing_serial_open(struct thing *thing) { - struct dead_thing *dead_thing; struct termios s_opts; - int baudrate = 0, stop = 0; + int baudrate = 0, csize = 0; - if (thing->conf.type == S_DEV) { + /* + * Just a reminder to set the ownership of your serial + * devices to _thingsd. Otherwise, a thing will not be + * able to successfully open(2) the file descriptor. + */ + if ((thing->fd = open(thing->conf.location, O_RDWR | + O_NONBLOCK | O_NOCTTY | O_NDELAY)) == -1) { + log_warn("failed to open %s", thing->conf.location); + return (-1); + } - /* - * Just a reminder to set the ownership of your serial - * devices to _thingsd. Otherwise, a thing will not be - * able to successfully open(2) the file descriptor. - */ - if ((thing->fd = open(thing->conf.location, O_RDWR | - O_NONBLOCK | O_NOCTTY | O_NDELAY)) == -1) { - log_warnx("failed to open %s", thing->conf.location); + if (tcgetattr(thing->fd, &s_opts) == -1) { + log_warn("%s: tcgetattr %s", __func__, thing->conf.location); + close(thing->fd); + thing->fd = -1; + return (-1); + } - if (reconn) - return; + switch (thing->conf.baud) { + case 50: + baudrate = B50; + break; + case 75: + baudrate = B75; + break; + case 110: + baudrate = B110; + break; + case 134: + baudrate = B134; + break; + case 150: + baudrate = B150; + break; + case 200: + baudrate = B200; + break; + case 300: + baudrate = B300; + break; + case 600: + baudrate = B600; + break; + case 1200: + baudrate = B1200; + break; + case 1800: + baudrate = B1800; + break; + case 2400: + baudrate = B2400; + break; + case 4800: + baudrate = B4800; + break; + case 9600: + baudrate = B9600; + break; + case 19200: + baudrate = B19200; + break; + case 38400: + baudrate = B38400; + break; + case 57600: + baudrate = B57600; + break; + case 76800: + baudrate = B76800; + break; + case 115200: + baudrate = B115200; + break; + } - dead_thing = thingsd_new_dead_thing(thing); + cfsetispeed(&s_opts, baudrate); + cfsetospeed(&s_opts, baudrate); - thingsd_env->exists = 1; - thingsd_env->dcount++; + /* enable and set local */ + s_opts.c_cflag |= (CLOCAL | CREAD); - TAILQ_INSERT_TAIL(thingsd_env->dead_things, - dead_thing, entry); + if (thing->conf.data_bits != -1) { + s_opts.c_cflag &= ~CSIZE; + switch (thing->conf.data_bits) { + case 5: + csize = CS5; + break; + case 6: + csize = CS6; + break; + case 7: + csize = CS7; + break; + case 8: + csize = CS8; + break; + } + s_opts.c_cflag |= csize; + } - return; - } else { - /* load current s_opts */ - tcgetattr(thing->fd, &s_opts); + if (strlen(thing->conf.parity) != 0) { + s_opts.c_cflag &= ~PARENB; - /* set baud */ - switch (thing->conf.baud) { - case 50: - baudrate = B50; - break; - case 75: - baudrate = B75; - break; - case 110: - baudrate = B110; - break; - case 134: - baudrate = B134; - break; - case 150: - baudrate = B150; - break; - case 200: - baudrate = B200; - break; - case 300: - baudrate = B300; - break; - case 600: - baudrate = B600; - break; - case 1200: - baudrate = B1200; - break; - case 1800: - baudrate = B1800; - break; - case 2400: - baudrate = B2400; - break; - case 4800: - baudrate = B4800; - break; - case 9600: - baudrate = B9600; - break; - case 19200: - baudrate = B19200; - break; - case 38400: - baudrate = B38400; - break; - case 57600: - baudrate = B57600; - break; - case 76800: - baudrate = B76800; - break; - case 115200: - baudrate = B115200; - break; - } + if (strcmp(thing->conf.parity, "odd") == 0) { + s_opts.c_cflag |= PARENB; + s_opts.c_cflag |= PARODD; + s_opts.c_iflag |= (INPCK | ISTRIP); + } else if (strcmp(thing->conf.parity, "even") == 0) { + s_opts.c_cflag |= PARENB; + s_opts.c_cflag &= ~PARODD; + s_opts.c_iflag |= (INPCK | ISTRIP); + } + } - cfsetispeed(&s_opts, baudrate); - cfsetospeed(&s_opts, baudrate); + if (thing->conf.stop_bits != -1) { + if (thing->conf.stop_bits == 2) + s_opts.c_cflag |= CSTOPB; + else + s_opts.c_cflag &= ~CSTOPB; + } - /* enable and set local */ - s_opts.c_cflag |= (CLOCAL | CREAD); + if (thing->conf.hw_ctl == 0) + s_opts.c_cflag &= ~CRTSCTS; + else + s_opts.c_cflag |= CRTSCTS; - /* set data bits */ - if (thing->conf.data_bits != -1) { - s_opts.c_cflag &= ~CSIZE; - switch(thing->conf.data_bits) { - case 5: - stop = CS5; - break; - case 6: - stop = CS6; - break; - case 7: - stop = CS7; - break; - case 8: - stop = CS8; - break; - } - s_opts.c_cflag |= stop; - } + if (thing->conf.sw_ctl == 0) + s_opts.c_iflag &= ~(IXON | IXOFF | IXANY); + else + s_opts.c_iflag |= (IXON | IXOFF | IXANY); - /* set parity */ - if (strlen(thing->conf.parity) != 0) { - s_opts.c_cflag &= ~PARENB; + /* raw input/output */ + s_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + s_opts.c_oflag &= ~OPOST; - /* enable parity checking */ - if (strcmp(thing->conf.parity, "odd") == 0) { - s_opts.c_cflag |= PARENB; - s_opts.c_cflag |= PARODD; - s_opts.c_iflag |= (INPCK | - ISTRIP); - } else if (strcmp(thing->conf.parity, - "even") == 0) { - s_opts.c_cflag |= PARENB; - s_opts.c_cflag &= ~PARODD; - s_opts.c_iflag |= (INPCK | - ISTRIP); - } + if (tcsetattr(thing->fd, TCSANOW, &s_opts) == -1) { + log_warn("%s: tcsetattr %s", __func__, thing->conf.location); + close(thing->fd); + thing->fd = -1; + return (-1); + } - } + return (0); +} - /* set stop bits */ - if (thing->conf.stop_bits != -1) { - if (thing->conf.stop_bits == 2) - s_opts.c_cflag |= CSTOPB; - else - s_opts.c_cflag &= ~CSTOPB; - } +/* + * Open the thing's descriptor(s) and hook them into the event loop. + * On failure the thing is queued for a retry unless this already is one. + */ +int +thingsd_thing_connect(struct thingsd *env, struct thing *thing, int reconn) +{ + struct address *a; + int fd, n; - /* set hardware control */ - if (thing->conf.hw_ctl == 0) { - s_opts.c_cflag &= ~CRTSCTS; - } else { - s_opts.c_cflag |= CRTSCTS; - } + thing->conf.env = env; - /* set software control */ - if (thing->conf.sw_ctl == 0) { - s_opts.c_iflag &= ~(IXON | IXOFF | - IXANY); - } else { - s_opts.c_iflag |= (IXON | IXOFF | - IXANY); - } - - /* set input/output as raw */ - s_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | - ISIG); - - s_opts.c_oflag &= ~OPOST; - - /* Set the new options for the port */ - tcsetattr(thing->fd, TCSANOW, &s_opts); - - if (thing->fd == '\0') { - log_warnx("serial device not opened"); - if (reconn) - return; - thingsd_add_reconn(thing); - return; - } + switch (thing->conf.type) { + case S_DEV: + if (thingsd_thing_serial_open(thing) == -1) + goto fail; + break; + case S_TCP: + if (thing->conf.persist == 0) { + /* connections are made per packet */ + thing->fd = -1; thing->exists = 1; + return (0); } - - if (reconn && thing->exists) - log_info("reconnected: %s", thing->conf.name); - } -} - -void -thingsd_thing_setup(struct thingsd *env, struct thing *thing, int reconn) -{ - evbuffercb thingrd = thingsd_thing_read; - evbuffercb thingwr = thingsd_thing_write; - - if (thing->conf.type == S_TCP) { - if (thing->conf.persist == 1) { - if ((thing->fd = sockets_open_client(thing->conf.ipaddr, - thing->conf.tcp_conn_port)) == -1) { - log_warnx("%s: ipaddr connection failed", - __func__); - if (reconn) - return; - thingsd_add_reconn(thing); - return; + if ((thing->fd = sockets_open_client(thing->conf.ipaddr, + thing->conf.tcp_conn_port)) == -1) { + log_warnx("%s: ipaddr connection failed for %s", + __func__, thing->conf.name); + goto fail; + } + thing->connecting = 1; + break; + case S_UDP: + /* udp things never go away, so this only runs at startup */ + n = 0; + TAILQ_FOREACH(a, thing->conf.udp_al, entry) { + if (n >= THINGSD_MAXIFACE) { + log_warnx("%s: too many udp addresses for %s", + __func__, thing->conf.name); + break; } - } else - /* 0 indicates non-persistent socket */ - thing->fd = 0; + if ((fd = sockets_create_socket(&a->ss, + thing->conf.udp_rcv_port, S_UDP)) == -1) + fatalx("%s: create udp socket failed for %s", + __func__, thing->conf.name); + thing->udp_fd[n] = fd; + event_set(&thing->udp_ev[n], fd, EV_READ | EV_PERSIST, + thingsd_thing_udp_event, thing); + if (event_add(&thing->udp_ev[n], NULL)) + fatalx("%s: udp ev error", __func__); + n++; + } + if (n == 0) + fatalx("%s: no udp address for %s", __func__, + thing->conf.name); + thing->udp_nfd = n; + thing->exists = 1; + log_debug("%s: running udp thing %s", __func__, + thing->conf.name); + return (0); + default: + fatalx("%s: unknown thing type %d", __func__, + thing->conf.type); } - if (thing->conf.type == S_UDP) { - if ((thing->fd = sockets_create_socket(thing->conf.udp_al, - thing->conf.udp_rcv_port, S_UDP)) == -1) - fatalx("%s: create udp socket failed", __func__); - } - log_debug("%s: configuring thing %s (%d)", __func__, thing->conf.name, thing->fd); - if (thing->conf.type == S_UDP) { - event_set(&thing->udp_ev, thing->fd, EV_READ | - EV_PERSIST, thingsd_thing_udp_event, thing); + thing->bev = bufferevent_new(thing->fd, thingsd_thing_read, + thingsd_thing_write, thingsd_thing_err, thing); + if (thing->bev == NULL) + fatalx("%s: bufferevent_new", __func__); - if (event_add(&thing->udp_ev, NULL)) - fatalx("%s: udp ev error", __func__); - } else { - thing->bev = bufferevent_new(thing->fd, - thingrd, thingwr, thingsd_thing_err, thing); + /* never buffer more than one package worth of input */ + bufferevent_setwatermark(thing->bev, EV_READ, 0, PKT_BUFF); + bufferevent_enable(thing->bev, EV_READ | EV_WRITE); - if (thing->bev == NULL) - fatalx("%s: ipaddr bev error", __func__); - - thing->evb = evbuffer_new(); - - if (thing->evb == NULL) - fatalx("ipaddr evb error"); - - bufferevent_enable(thing->bev, EV_READ | EV_WRITE); - } - - log_debug("%s: running thing %s", __func__, thing->conf.name); - - thing->conf.env = env; thing->exists = 1; - if (reconn) - log_info("reconnected: %s", thing->conf.name); + if (thing->conf.type == S_DEV) + log_info("%s: %s", reconn ? "reconnected" : "connected", + thing->conf.name); + + return (0); + +fail: + if (!reconn) + thingsd_add_reconn(thing); + return (-1); } void +thingsd_thing_disconnect(struct thing *thing) +{ + if (thing->bev != NULL) { + bufferevent_free(thing->bev); + thing->bev = NULL; + } + if (thing->fd != -1) { + close(thing->fd); + thing->fd = -1; + } + thing->connecting = 0; + thingsd_add_reconn(thing); +} + +void +thingsd_oneshot_cb(int fd, short event, void *arg) +{ + struct oneshot *os = arg; + socklen_t len = sizeof(int); + int error = 0; + + if (event & EV_TIMEOUT) + log_warnx("%s: connection to %s timed out", __func__, + os->name); + else if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) == -1 || + error != 0) { + errno = error ? error : errno; + log_warn("%s: connection to %s failed", __func__, os->name); + } else if (write(fd, os->pkt, os->len) == -1) + log_warn("%s: write to %s failed", __func__, os->name); + + close(fd); + free(os); +} + +void thingsd_write_to_things(struct package *package) { struct thing *thing = NULL; + struct oneshot *os; + struct timeval tv; TAILQ_FOREACH(thing, thingsd_env->things, entry) if (thing->conf.id == package->thing_id) @@ -841,80 +1057,62 @@ thingsd_write_to_things(struct package *package) if (thing == NULL) return; - switch (thing->conf.type) { - case S_TCP: - case S_DEV: - if (thing->conf.persist == 0) { - if ((thing->fd = sockets_open_client(thing->conf.ipaddr, - thing->conf.tcp_conn_port)) == -1) { - log_warnx("%s: temporary ipaddr connection" - " failed", __func__); - return; - } - write(thing->fd, package->pkt, package->len); - close(thing->fd); - thing->fd = -1; - } else { - if (thing->fd != -1) { - bufferevent_write(thing->bev, package->pkt, - package->len); - } - } - break; - case S_UDP: - default: - write(thing->fd, package->pkt, package->len); - break; + if (thing->conf.type == S_UDP) { + /* udp things are receive only */ + log_debug("%s: dropping packet for udp thing %s", __func__, + thing->conf.name); + return; } + + if (thing->conf.type == S_DEV || thing->conf.persist) { + if (thing->bev != NULL) + bufferevent_write(thing->bev, package->pkt, + package->len); + return; + } + + if ((os = calloc(1, sizeof(*os))) == NULL) { + log_warn("%s: calloc", __func__); + return; + } + if ((os->fd = sockets_open_client(thing->conf.ipaddr, + thing->conf.tcp_conn_port)) == -1) { + free(os); + return; + } + memcpy(os->pkt, package->pkt, package->len); + os->len = package->len; + strlcpy(os->name, thing->conf.name, sizeof(os->name)); + + /* fires once the non-blocking connect has settled */ + tv.tv_sec = ONESHOT_TIMEOUT; + tv.tv_usec = 0; + event_set(&os->ev, os->fd, EV_WRITE, thingsd_oneshot_cb, os); + if (event_add(&os->ev, &tv) == -1) { + log_warn("%s: event_add", __func__); + close(os->fd); + free(os); + } } void thingsd_write_to_socks(struct packages *packages) { - struct privsep *ps = thingsd_env->thingsd_ps; struct package *package, *tpkg, p; struct packet_client *packet_client; struct thing *thing = NULL; - unsigned int id, what; - int fd = -1, n, m; - struct iovec iov[6]; - size_t c; + int n; TAILQ_FOREACH_SAFE(package, packages, entry, tpkg) { - for (id = 0; id < PROC_MAX; id++) { - what = ps->ps_what[id]; + memcpy(&p, package, sizeof(p)); - if ((what & CONFIG_SOCKS) == 0 || id == privsep_process) - continue; - - memcpy(&p, package, sizeof(p)); - - c = 0; - iov[c].iov_base = &p; - iov[c++].iov_len = sizeof(p); - if (id == PROC_SOCKS) { - /* XXX imsg code will close the fd after 1st call */ - n = -1; - proc_range(ps, id, &n, &m); - for (n = 0; n < m; n++) { - /* send thing fd */ - if (proc_composev_imsg(ps, id, n, - IMSG_DIST_THING_PACKAGE, -1, fd, - iov, c) != 0) { - log_warn("%s: failed to compose" - " IMSG_DIST_THING_PACKAGE" - " imsg", - __func__); - return; - } - if (proc_flush_imsg(ps, id, n) == -1) { - log_warn("%s: failed to flush " - "IMSG_DIST_THING_PACKAGE " - "imsg", - __func__); - return; - } - } + for (n = 0; n < thingsd_env->prefork_socks; n++) { + if (imsg_compose_event(&parent_iev_socks[n], + IMSG_DIST_THING_PACKAGE, 0, 0, -1, &p, + sizeof(p)) == -1) { + log_warn("%s: failed to compose " + "IMSG_DIST_THING_PACKAGE imsg", __func__); + return; } } @@ -928,17 +1126,14 @@ thingsd_write_to_socks(struct packages *packages) TAILQ_FOREACH(packet_client, thingsd_env->packet_clients, entry) { - if (strlen(packet_client->name) != 0) { - if (strcmp(thing->conf.name, - packet_client->name) == 0) - if (proc_compose_imsg( - &packet_client->ps, - PROC_CONTROL, -1, - IMSG_SHOW_PACKETS_DATA, - packet_client->imsg.hdr.peerid, -1, - package->pkt, package->len) == -1) - break; - } + if (strlen(packet_client->name) == 0) + continue; + if (strcmp(thing->conf.name, packet_client->name) != 0) + continue; + if (imsg_compose_event(parent_iev_control, + IMSG_SHOW_PACKETS_DATA, packet_client->peerid, 0, + -1, package->pkt, package->len) == -1) + break; } free: TAILQ_REMOVE(packages, package, entry); @@ -974,25 +1169,47 @@ void thingsd_thing_read(struct bufferevent *bev, void *arg) { struct thing *thing = (struct thing *)arg; - struct package *package = NULL; + struct evbuffer *evb = EVBUFFER_INPUT(bev); + struct package *package; + size_t len; + while ((len = EVBUFFER_LENGTH(evb)) > 0) { + if ((package = calloc(1, sizeof(*package))) == NULL) + fatalx("%s: calloc", __func__); - if ((package = calloc(1, sizeof(*package))) == NULL) - fatalx("%s: calloc", __func__); + if (len > sizeof(package->pkt)) + len = sizeof(package->pkt); + evbuffer_remove(evb, package->pkt, len); + package->len = len; + package->thing_id = thing->conf.id; - thing->evb = EVBUFFER_INPUT(bev); - package->len = EVBUFFER_LENGTH(thing->evb); + TAILQ_INSERT_TAIL(thingsd_env->packages, package, entry); + } - evbuffer_remove(thing->evb, package->pkt, package->len); - package->thing_id = thing->conf.id; - - TAILQ_INSERT_TAIL(thingsd_env->packages, package, entry); thingsd_write_to_socks(thingsd_env->packages); } void thingsd_thing_write(struct bufferevent *bev, void *arg) { + struct thing *thing = (struct thing *)arg; + socklen_t len = sizeof(int); + int error = 0; + + if (!thing->connecting) + return; + + /* first writable event after a non-blocking connect */ + thing->connecting = 0; + if (getsockopt(thing->fd, SOL_SOCKET, SO_ERROR, &error, &len) == -1 || + error != 0) { + errno = error ? error : errno; + log_warn("thing %s: connection failed", thing->conf.name); + thingsd_thing_disconnect(thing); + return; + } + + log_info("connected: %s", thing->conf.name); } void @@ -1000,26 +1217,14 @@ thingsd_thing_err(struct bufferevent *bev, short error { struct thing *thing = (struct thing *)arg; - if ((error & EVBUFFER_ERROR) == 0 || error & EVBUFFER_TIMEOUT) { - if (thing->fd != -1) { - if (thing->bev != NULL) - bufferevent_disable(thing->bev, - EV_READ | EV_WRITE); - if (thing->bev != NULL) - bufferevent_free(thing->bev); + if (error & EVBUFFER_EOF) + log_warnx("thing %s disconnected", thing->conf.name); + else if (error & EVBUFFER_TIMEOUT) + log_warnx("thing %s timed out", thing->conf.name); + else + log_warnx("thing %s error, disconnecting", thing->conf.name); - thing->evb = NULL; - thing->bev = NULL; - - close(thing->fd); - thing->fd = -1; - - thingsd_add_reconn(thing); - - log_warnx("thing error: %s disconnected", - thing->conf.name); - } - } + thingsd_thing_disconnect(thing); } void @@ -1027,22 +1232,14 @@ thingsd_add_reconn(struct thing *thing) { struct dead_thing *dead_thing; - dead_thing = thingsd_new_dead_thing(thing); + thing->exists = 0; - thingsd_env->exists = 1; - thingsd_env->dcount++; + /* already waiting for a retry */ + TAILQ_FOREACH(dead_thing, thingsd_env->dead_things, entry) + if (strcmp(dead_thing->name, thing->conf.name) == 0) + return; - TAILQ_INSERT_TAIL(thingsd_env->dead_things, dead_thing, entry); - -} - -struct dead_thing -*thingsd_new_dead_thing(struct thing *thing) -{ - struct dead_thing *dead_thing; - - dead_thing = calloc(1, sizeof(*dead_thing)); - if (dead_thing == NULL) + if ((dead_thing = calloc(1, sizeof(*dead_thing))) == NULL) fatal("%s: calloc", __func__); log_debug("%s: adding detached thing, %s", __func__, thing->conf.name); @@ -1054,54 +1251,42 @@ struct dead_thing dead_thing->type = thing->conf.type; dead_thing->dtime = time(NULL); - thing->exists = 0; - - return dead_thing; + TAILQ_INSERT_TAIL(thingsd_env->dead_things, dead_thing, entry); } void thingsd_do_reconn(struct thingsd *env) { - struct dead_thing *dead_thing = NULL; - struct thing *thing = NULL; - int found = 0; + struct dead_thing *dead_thing, *tdead; + struct thing *thing; + time_t now = time(NULL); - TAILQ_FOREACH(thing, env->things, entry) { - TAILQ_FOREACH(dead_thing, env->dead_things, entry) - if (strcmp(thing->conf.name, dead_thing->name) == 0) { - found = 1; + TAILQ_FOREACH_SAFE(dead_thing, env->dead_things, entry, tdead) { + if ((size_t)(now - dead_thing->dtime) <= env->conn_retry) + continue; + + TAILQ_FOREACH(thing, env->things, entry) + if (strcmp(thing->conf.name, dead_thing->name) == 0) break; - } - if (found) - break; - } - if (thing == NULL || dead_thing == NULL) - return; + if (thing == NULL || thing->exists) { + TAILQ_REMOVE(env->dead_things, dead_thing, entry); + free(dead_thing); + continue; + } - if ((size_t)(time(NULL) - dead_thing->dtime) > env->conn_retry) { - dead_thing->dtime = time(NULL); - log_debug("attempting to reconnect %s", dead_thing->name); + dead_thing->dtime = now; - if (dead_thing->type == S_DEV) - thingsd_thing_serial_open(thing, 1); - - thingsd_thing_setup(env, thing, 1); + if (thingsd_thing_connect(env, thing, 1) == 0) { + TAILQ_REMOVE(env->dead_things, dead_thing, entry); + free(dead_thing); + } } - - if (thing->exists) { - TAILQ_REMOVE(env->dead_things, dead_thing, entry); - env->dcount--; - free(dead_thing); - } - - if (env->dcount == 0) - env->exists = 0; } void -thingsd_echo_pkt(struct privsep *ps, struct imsg *imsg) +thingsd_echo_pkt(struct imsg *imsg) { struct packet_client *packet_client; @@ -1111,10 +1296,10 @@ thingsd_echo_pkt(struct privsep *ps, struct imsg *imsg thingsd_env->packet_client_count++; - packet_client->ps = *ps; - packet_client->imsg = *imsg; + packet_client->peerid = imsg->hdr.peerid; - memcpy(packet_client->name, imsg->data, sizeof(packet_client->name)); + imsg_get_string(imsg, packet_client->name, + sizeof(packet_client->name)); log_debug("control packet echo request for %s", packet_client->name); @@ -1122,7 +1307,7 @@ thingsd_echo_pkt(struct privsep *ps, struct imsg *imsg } void -thingsd_stop_pkt(struct privsep *ps, struct imsg *imsg) +thingsd_stop_pkt(struct imsg *imsg) { struct packet_client *packet_client, *tpacket_client; uint32_t fd; @@ -1131,7 +1316,7 @@ thingsd_stop_pkt(struct privsep *ps, struct imsg *imsg TAILQ_FOREACH_SAFE(packet_client, thingsd_env->packet_clients, entry, tpacket_client) { - if (fd == packet_client->imsg.hdr.peerid) { + if (fd == packet_client->peerid) { TAILQ_REMOVE(thingsd_env->packet_clients, packet_client, entry); log_debug("control packet echo request stopping for %s", @@ -1145,31 +1330,28 @@ thingsd_stop_pkt(struct privsep *ps, struct imsg *imsg } void -thingsd_show_thing_info(struct privsep *ps, struct imsg *imsg) +thingsd_show_thing_info(struct imsg *imsg) { - char filter[THINGSD_MAXNAME]; - struct thing *thing, nti; + struct thing *thing, nti; + char filter[THINGSD_MAXNAME]; - memcpy(filter, imsg->data, sizeof(filter)); + memset(&nti, 0, sizeof(nti)); + imsg_get_string(imsg, filter, sizeof(filter)); TAILQ_FOREACH(thing, thingsd_env->things, entry) { if (filter[0] == '\0' || strcmp(filter, thing->conf.name) == 0) { - nti = thingsd_compose_thing(thing, - imsg->hdr.type); + nti = thingsd_compose_thing(thing, imsg->hdr.type); - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_GET_INFO_THINGS_DATA, - imsg->hdr.peerid, -1, &nti, - sizeof(nti)) == -1) + if (imsg_compose_event(parent_iev_control, + IMSG_GET_INFO_THINGS_DATA, imsg->hdr.peerid, 0, + -1, &nti, sizeof(nti)) == -1) return; } } - if (proc_compose_imsg(ps, PROC_CONTROL, -1, - IMSG_GET_INFO_THINGS_END_DATA, imsg->hdr.peerid, - -1, &nti, sizeof(nti)) == -1) - return; + imsg_compose_event(parent_iev_control, IMSG_GET_INFO_THINGS_END_DATA, + imsg->hdr.peerid, 0, -1, &nti, sizeof(nti)); } struct thing blob - e4722aa14179afb58334fa47f72516170908b851 blob + 57ccb9e6b192312cb14e5de02adaec541ced1636 --- thingsd/thingsd.conf.5 +++ thingsd/thingsd.conf.5 @@ -1,5 +1,5 @@ .\" -.\" Copyright (c) 2016-2019 Tracey Emery +.\" Copyright (c) 2016-2026 Tracey Emery .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above @@ -135,7 +135,7 @@ or .It Ic parity Ar string Set the parity for a serial thing .Ar location . -Parity options are none, odd, even, or space. +Parity options are none, odd, or even. .It Ic password Ar string Set the subscription password for a thing .Ar location @@ -327,4 +327,4 @@ file format first appeared with busybeed development o The .Xr thingsd 8 program was written by -.An Tracey Emery Aq Mt tracey@traceyemery.net . +.An Tracey Emery Aq Mt tracey@openbsd.org . blob - c63f48d8b2595640c015476949de5126e4a531fe blob + c23ff1460f49a68f37e772087bca8431ab54536d --- thingsd/thingsd.h +++ thingsd/thingsd.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2019-2021 Tracey Emery + * Copyright (c) 2016, 2019-2026 Tracey Emery * Copyright (c) 2015 Mike Larkin * * Permission to use, copy, modify, and distribute this software for any @@ -39,6 +39,7 @@ #define CLIENT_SUB_TIME_USEC 100 #define CLIENT_SUB_CHK 1 #define EB_TIMEOUT 1 +#define ONESHOT_TIMEOUT 10 #define TLS_CONFIG_MAX 511 #define TLS_CERT "/etc/ssl/thing.crt" @@ -60,10 +61,23 @@ #define PKT_BUFF 2048 +#define THINGSD_MAX_PREFORK 32 + #define MAX_IMSG_DATA_SIZE (MAX_IMSGSIZE - IMSG_HEADER_SIZE) +#define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE) +#define IMSG_SIZE_CHECK(imsg, p) do { \ + if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \ + fatalx("bad length imsg received (%s)", #p); \ +} while (0) enum imsg_type { - IMSG_GET_INFO_THINGSD_REQUEST = IMSG_PROC_MAX, + IMSG_NONE, + IMSG_CTL_OK, + IMSG_CTL_FAIL, + IMSG_CTL_VERBOSE, + IMSG_CTL_NOTIFY, + + IMSG_GET_INFO_THINGSD_REQUEST, IMSG_GET_INFO_THINGSD_DATA, IMSG_GET_INFO_THINGSD_END_DATA, @@ -100,6 +114,13 @@ enum imsg_type { IMSG_DIST_CLIENT_PACKAGE, }; +struct imsgev { + struct imsgbuf ibuf; + void (*handler)(int, short, void *); + struct event ev; + short events; +}; + /* * parse.y contains TCP and UDP * so, these have to be different @@ -144,19 +165,16 @@ struct package { }; TAILQ_HEAD(packages, package); +struct socket; + struct client { TAILQ_ENTRY(client) entry; struct packages *packages; - int id; int thing_id; int sock_id; - void *sock; - void *sock_conf; + struct socket *sock; - struct clt_time *clt; - struct evbuffer *evb; struct bufferevent *bev; - struct event ev; struct event timeout; int subscribed; @@ -173,7 +191,6 @@ struct client { TAILQ_HEAD(clientlist, client); struct socket_config { - struct addresslist *al; char iface[THINGSD_MAXTEXT]; char name[THINGSD_MAXTEXT]; char thing_name[THINGSD_MAXTEXT]; @@ -328,15 +345,15 @@ struct thing { struct thing_config conf; int exists; - /* either DEV, TCP conn, or UDP rcv fd */ + /* DEV or persistent TCP fd, -1 when not connected */ int fd; - - /* event for TCP thing fd */ + int connecting; struct bufferevent *bev; - struct evbuffer *evb; - /* event for UDP thing fd */ - struct event udp_ev; + /* one receiving socket per bound UDP address */ + int udp_fd[THINGSD_MAXIFACE]; + struct event udp_ev[THINGSD_MAXIFACE]; + int udp_nfd; }; TAILQ_HEAD(thinglist, thing); @@ -350,9 +367,7 @@ TAILQ_HEAD(deadthinglist, dead_thing); struct packet_client { TAILQ_ENTRY(packet_client) entry; - struct privsep ps; - struct imsg imsg; - int fd; + uint32_t peerid; char name[THINGSD_MAXNAME]; }; TAILQ_HEAD(packetclientlist, packet_client); @@ -362,7 +377,6 @@ struct thingsd { struct socketlist *sockets; struct packages *packages; - struct privsep *thingsd_ps; const char *thingsd_conffile; int thingsd_debug; @@ -373,15 +387,11 @@ struct thingsd { char udp_iface[THINGSD_MAXTEXT]; size_t max_clients; - size_t client_cnt; size_t conn_retry; uint16_t prefork_socks; int run; - /* disconnected things parts */ struct deadthinglist *dead_things; - int exists; - int dcount; /* things */ struct event_base *thingsd_eb; @@ -397,6 +407,13 @@ struct thingsd { extern struct thingsd *thingsd_env; extern int thing_id; +/* parent process pipes, indexed by prefork instance for sockets */ +extern struct imsgev *parent_iev_control; +extern struct imsgev *parent_iev_socks; + +/* sockets process pipe to the parent */ +extern struct imsgev *socks_iev_parent; + /* client.c */ void client_del(struct client *); void client_rd(struct bufferevent *, void *); @@ -405,30 +422,34 @@ void client_err(struct bufferevent *, short, void *); /* sockets.c */ struct socket - *sockets_new_socket(char *); -struct socket - *sockets_get_socket(struct thingsd *, int); -struct socket *sockets_get_socket_byid(struct thingsd *, int); -int sockets_create_socket(struct addresslist *, in_port_t, int); -int sockets_open_client(char *, in_port_t); +int sockets_create_socket(struct sockaddr_storage *, in_port_t, int); +int sockets_open_client(const char *, in_port_t); int sockets_privinit(struct socket *); -int sockets_client_cmp(struct client *, struct client *); +size_t sockets_thing_client_cnt(int); void sockets_parse_sockets(struct thingsd *); -void sockets_socket_rlimit(int); -void sockets_sighdlr(int, short, void *); -void sockets_shutdown(void); -void sockets(struct privsep *, struct privsep_proc *); +void sockets_socket_rlimit(void); +__dead void + sockets(int, int); /* thingsd.c */ struct thing *thingsd_conf_new_thing(struct thingsd *, struct thing *, char *, int); +void imsg_event_init(struct imsgev *, int, void (*)(int, short, void *)); +void imsg_event_add(struct imsgev *); +int imsg_compose_event(struct imsgev *, uint32_t, uint32_t, pid_t, int, + void *, size_t); +int imsg_composev_event(struct imsgev *, uint32_t, uint32_t, pid_t, int, + const struct iovec *, int); +int imsg_forward_event(struct imsgev *, struct imsg *); +void imsg_get_string(struct imsg *, char *, size_t); +/* control.c */ +__dead void + control(int, int *); + /* config.c */ int config_init(struct thingsd *); -void config_purge(struct thingsd *, unsigned int); -int config_setreset(struct thingsd *, unsigned int); -int config_getreset(struct thingsd *, struct imsg *); int config_setsocks(struct thingsd *, struct socket *); int config_getsocks(struct thingsd *, struct imsg *); int config_getcfg(struct thingsd *, struct imsg *); @@ -436,7 +457,7 @@ int config_getsocks_tls(struct thingsd *, struct imsg /* parse.y */ int parse_config(const char *, struct thingsd *); -int parse_buf(struct client *, u_char *, int); +int parse_buf(struct client *, u_char *); int cmdline_symset(char *); struct address *host_v4(const char *); struct address *host_v6(const char *); @@ -455,4 +476,25 @@ int tls_load_crl(struct socket *); int tls_load_ocsp(struct socket *); void socket_tls_load(struct socket *); int socket_tls_init(struct socket *); -void socket_tls_handshake(int, short, void *); + +/* log.c */ +void log_init(int, int); +void log_procinit(const char *); +void log_setverbose(int); +int log_getverbose(void); +void log_warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_info(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_debug(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void logit(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +void vlog(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); blob - 775cb1b60baf43498aceabb1e4fad58b706947ab blob + 40a9aa938fcb2162e992ea0698180e76667c7b61 --- thingsd/tls.c +++ thingsd/tls.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 - 2021 Tracey Emery + * Copyright (c) 2019 - 2026 Tracey Emery * Copyright (c) 2006 - 2015 Reyk Floeter * (snippets from httpd functions) * @@ -34,7 +34,6 @@ #include #include -#include "proc.h" #include "thingsd.h" int @@ -192,7 +191,8 @@ socket_tls_init(struct socket *sock) log_warnx("%s: failed to add ca cert(s)", __func__); return (-1); } - if (tls_config_set_crl_mem(sock->tls_config, + if (sock->conf.tls_crl != NULL && + tls_config_set_crl_mem(sock->tls_config, sock->conf.tls_crl, sock->conf.tls_crl_len) != 0) { log_warnx("%s: failed to add crl(s)", __func__); return (-1); @@ -203,18 +203,6 @@ socket_tls_init(struct socket *sock) tls_config_verify_client(sock->tls_config); } - log_debug("%s: adding keypair for listener %s", __func__, - sock->conf.name); - - if (tls_config_add_keypair_ocsp_mem(sock->tls_config, - sock->conf.tls_cert, sock->conf.tls_cert_len, - sock->conf.tls_key, sock->conf.tls_key_len, - sock->conf.tls_ocsp_staple, - sock->conf.tls_ocsp_staple_len) != 0) { - log_warnx("%s: failed to add tls keypair", __func__); - return (-1); - } - if (tls_configure(sock->tls_ctx, sock->tls_config) != 0) { log_warnx("%s: failed to configure tls - %s", __func__, tls_error(sock->tls_ctx)); @@ -243,43 +231,6 @@ socket_tls_init(struct socket *sock) } void -socket_tls_handshake(int fd, short event, void *arg) -{ - struct client *client = (struct client *)arg; - struct socket *sock = client->sock; - ssize_t ret = 0; - - if (client == NULL) - return; - - if (sock->tls_ctx == NULL || client->tls_ctx == NULL) - fatalx("NULL tls context"); - - ret = tls_handshake(client->tls_ctx); - - if (ret == 0) { - event_del(&client->ev); - log_debug("%s: tls handshake success", __func__); - } else if (ret == TLS_WANT_POLLIN) { - event_del(&client->ev); - event_set(&client->ev, client->fd, EV_READ|EV_PERSIST, - socket_tls_handshake, client); - if (event_add(&client->ev, NULL)) - log_debug("%s: event add client", __func__); - } else if (ret == TLS_WANT_POLLOUT) { - event_del(&client->ev); - event_set(&client->ev, client->fd, EV_WRITE|EV_PERSIST, - socket_tls_handshake, client); - if (event_add(&client->ev, NULL)) - log_debug("%s: event add client", __func__); - } else { - event_del(&client->ev); - log_debug("%s: tls handshake failed - %s", __func__, - tls_error(client->tls_ctx)); - } -} - -void socket_tls_load(struct socket *sock) { log_debug("%s: setup TLS for listener %d", __func__, sock->conf.id);