commit 605bf9f121de8fbec15a7e6f15f1060ed29ee16c831b7597f4a351afa5806a3a from: Tracey Emery date: Tue Nov 10 16:28:46 2026 UTC Initial import commit - /dev/null commit + 605bf9f121de8fbec15a7e6f15f1060ed29ee16c831b7597f4a351afa5806a3a blob - /dev/null blob + 9ea81a96d6fcaf1e2b326195c494155bf155d749c14ca2eda415802364e1b48d (mode 644) --- /dev/null +++ LICENSE.md @@ -0,0 +1,13 @@ +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 +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. blob - /dev/null blob + 0538f33c49037d622ef3ae7bcf16e842bdc5598da08a460b3da52f51fa6daa31 (mode 644) --- /dev/null +++ Makefile @@ -0,0 +1,3 @@ +SUBDIR = mqttd mqttctl + +.include blob - /dev/null blob + 86ba75eaa735b2ed45091ebaab4b6dc2d682b89a3cec821a0963a16ce11f7c38 (mode 644) --- /dev/null +++ README.md @@ -0,0 +1,77 @@ +mqttd for OpenBSD +================= + +mqttd is a privilege separated MQTT broker for OpenBSD. It speaks MQTT +3.1, 3.1.1 and 5.0 over TCP and TLS and implements QoS 0, 1 and 2, +retained messages, will messages with delay, persistent sessions with +offline queues, session and message expiry, topic aliases, subscription +identifiers, shared subscriptions, bridging to other brokers, Sparkplug +Aware behaviour and topic ACLs with password, certificate or external +authentication. + +The daemon is split into a privileged parent, a control process, a single +broker process holding all protocol state, and a configurable number of +preforked sockets processes that own the network connections and TLS. +The layout and plumbing come from the thingsd proxy daemon. + +Prerequisites +------------- + +* OpenBSD 7.7 or higher (uses the current imsg API) + +Files +----- + +* `/usr/local/sbin/mqttd` +* `/usr/local/man/man8/mqttd.8` +* `/etc/examples/mqttd.conf` +* `/usr/local/man/man5/mqttd.conf.5` +* `/etc/rc.d/mqttd` +* `/usr/local/sbin/mqttctl` +* `/usr/local/man/man8/mqttctl.8` + +Usage +----- + +Compile and install. + + make + doas make install + doas groupadd _mqttd + doas useradd -d /var/empty -L daemon -g _mqttd -s /sbin/nologin _mqttd + doas cp mqttd/examples/etc/rc.d/mqttd /etc/rc.d/ + doas cp mqttd/examples/etc/mqttd.conf /etc/examples/ + doas install -d -o _mqttd -g _mqttd -m 700 /var/db/mqttd + +Create password hashes with `encrypt -b 10`, write `/etc/mqttd.conf` +according to `man mqttd.conf`, then `rcctl enable mqttd` and +`rcctl start mqttd`. Run `mqttd -d` to stay in the foreground, and +`mqttd -n` to check the configuration. + +Inspect the broker with `mqttctl show broker`, `mqttctl show clients`, +`mqttctl show subscriptions` and `mqttctl show retained`. + +The optional external authenticator example lives in +`mqttd/examples/auth/mqttd-auth.pl`; install it somewhere like +`/usr/local/libexec/` and point `auth exec` at it. + +Listeners speak plain MQTT, MQTT over TLS, MQTT over WebSocket (with or +without TLS), accept PROXY protocol headers from a load balancer, or sit +on a unix socket. + +A `snowflake` block sinks topics straight into a Snowflake table through +the SQL REST API with key-pair authentication, with local queueing and +at-least-once delivery; see `mqttd.conf(5)`. + +Not yet implemented +------------------- + +* adding or removing bridges without a restart +* fan-out offload to the sockets processes (see ROADMAP.md) + +Author +------ + +[Tracey Emery](https://github.com/basepr1me/) + +See the [License](LICENSE.md) file for more information. blob - /dev/null blob + 5898f35bc333f166f7eae8f9aa756b0010a9c17f0d8bca199b7edb143d0d2dc7 (mode 644) --- /dev/null +++ mqttctl/Makefile @@ -0,0 +1,20 @@ +PROG= mqttctl + +SRCS+= mqttctl.c parser.c + +PREFIX= /usr/local +BINDIR?= ${PREFIX}/sbin +MANDIR?= ${PREFIX}/man/man + +LDADD= -lutil +DPADD= ${LIBUTIL} +CFLAGS+= -Wall -I${.CURDIR} +CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes +CFLAGS+= -Wmissing-declarations +CFLAGS+= -Wshadow -Wpointer-arith +CFLAGS+= -Wsign-compare -fno-common +CFLAGS+= -I${.CURDIR} -I${.CURDIR}/../mqttd + +MAN= mqttctl.8 + +.include blob - /dev/null blob + b21f7bae8819426b5ae8420b54f713f4787dc3df983c2b78d97df4102ebf1653 (mode 644) --- /dev/null +++ mqttctl/mqttctl.8 @@ -0,0 +1,102 @@ +.\" +.\" Copyright (c) 2016-2021, 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 +.\" 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. +.\" +.Dd $Mdocdate: October 31 2026 $ +.Dt MQTTCTL 8 +.Os +.Sh NAME +.Nm mqttctl +.Nd control the MQTT broker +.Sh SYNOPSIS +.Nm +.Op Fl s Ar socket +.Ar command +.Op Ar argument ... +.Sh DESCRIPTION +The +.Nm +program controls the +.Xr mqttd 8 +broker. +.Pp +The following options are available: +.Bl -tag -width Ds +.It Fl s Ar socket +Use +.Ar socket +instead of the default +.Pa /var/run/mqttd.sock +to communicate with +.Xr mqttd 8 . +.El +.Pp +The following commands are available: +.Bl -tag -width Ds +.It Cm disconnect all +Disconnect every client with reason code 0x8B, leaving bridges and +sessions in place; clients reconnect and resume. +Requires root. +.It Cm kill Ar client-id +Disconnect the client with the given identifier. +Its will message, if any, is published. +Requires root. +.It Cm log brief +Disable verbose logging. +Requires root. +.It Cm log debug +Enable debug logging. +Requires root. +.It Cm log verbose +Enable verbose logging. +Requires root. +.It Cm save +Write a persistence snapshot now instead of waiting for the next +interval. +Requires root. +.It Cm show bridges +List configured bridges with their state, uptime, traffic and queue. +.It Cm show broker +Show broker statistics: sessions, subscriptions, retained messages and +traffic counters. +.It Cm show clients Op Ar client-id +List all sessions, connected or not, or the one with the given +identifier. +.It Cm show control +Show the state of the control process. +.It Cm show listeners +List the configured listeners with their connection counts. +.It Cm show parent +Show the state of the parent process. +.It Cm show retained Op Ar topic +List retained messages, or the one for the given topic. +.It Cm show stats +The broker statistics as +.Dq key=value +lines, one per line, for monitoring scripts. +.It Cm show subscriptions Op Ar client-id +List all subscriptions, or those of the given client. +.El +.Sh FILES +.Bl -tag -width "/var/run/mqttd.sockXX" -compact +.It Pa /var/run/mqttd.sock +.Ux Ns -domain +socket used for communication with +.Xr mqttd 8 . +.El +.Sh SEE ALSO +.Xr mqttd.conf 5 , +.Xr mqttd 8 +.Sh AUTHORS +.An Tracey Emery Aq Mt tracey@openbsd.org blob - /dev/null blob + 63cb40e9fd999f3a04529bf015f343349db5b30abbf3480783887288c069d608 (mode 644) --- /dev/null +++ mqttctl/mqttctl.c @@ -0,0 +1,665 @@ +/* + * Copyright (c) 2016, 2019-2021, 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 + * 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 + +#include "mqttd.h" +#include "parser.h" + +#define MAX_LISTENERS 256 + +struct listener_sum { + uint32_t id; + char name[MQTTD_MAXNAME]; + char addr[MQTTD_MAXPEER]; + in_port_t port; + int tls; + int websocket; + int proxy; + uint32_t nconns; + int procs; +}; + +__dead void usage(void); +static int show_parent_msg(struct imsg *); +static int show_control_msg(struct imsg *); +static int show_broker_msg(struct imsg *); +static int show_stats_msg(struct imsg *); +static int show_bridges_msg(struct imsg *); +static int show_listeners_msg(struct imsg *); +static int show_clients_msg(struct imsg *); +static int show_subs_msg(struct imsg *); +static int show_retained_msg(struct imsg *); +static int kill_msg(struct imsg *); +static const char + *fmt_duration(time_t, char *, size_t); +static const char + *fmt_time(time_t); +static const char + *fmt_level(int); + +static struct imsgbuf *ibuf; +static struct listener_sum lsum[MAX_LISTENERS]; +static int nlsum; +static int header_done; + +__dead void +usage(void) +{ + extern char *__progname; + + fprintf(stderr, "usage: %s [-s socket] command [argument ...]\n", + __progname); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + struct sockaddr_un sun; + struct parse_result *res; + struct imsg imsg; + const char *sockname = MQTTD_SOCKET; + int ctl_sock, done = 0, n, verbose = 0, ch; + + while ((ch = getopt(argc, argv, "s:")) != -1) { + switch (ch) { + case 's': + sockname = optarg; + break; + default: + usage(); + } + } + argc -= optind; + argv += optind; + + if ((res = parse(argc, argv)) == NULL) + exit(1); + + if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) + err(1, "socket"); + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + if (strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path)) >= + sizeof(sun.sun_path)) + errx(1, "socket name too long"); + if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) + err(1, "connect: %s", sockname); + + if (pledge("stdio", NULL) == -1) + err(1, "pledge"); + + if ((ibuf = malloc(sizeof(*ibuf))) == NULL) + err(1, "malloc"); + if (imsgbuf_init(ibuf, ctl_sock) == -1) + err(1, "imsgbuf_init"); + + switch (res->action) { + case LOG_DEBUG: + case LOG_VERBOSE: + case LOG_BRIEF: + case KILL_CLIENT: + case DISCONNECT_ALL: + case SAVE: + if (geteuid() != 0) + errx(1, "need root privileges"); + break; + default: + break; + } + + switch (res->action) { + case KILL_CLIENT: + imsg_compose(ibuf, IMSG_CTL_KILL_CLIENT, 0, 0, -1, + &res->filter, sizeof(res->filter)); + break; + case DISCONNECT_ALL: + imsg_compose(ibuf, IMSG_CTL_DISCONNECT_ALL, 0, 0, -1, NULL, 0); + break; + case SHOW_STATS: + imsg_compose(ibuf, IMSG_CTL_SHOW_BROKER_REQUEST, 0, 0, -1, + NULL, 0); + break; + case SAVE: + imsg_compose(ibuf, IMSG_CTL_SAVE, 0, 0, -1, NULL, 0); + break; + case LOG_DEBUG: + verbose++; + /* FALLTHROUGH */ + case LOG_VERBOSE: + verbose++; + /* FALLTHROUGH */ + case LOG_BRIEF: + imsg_compose(ibuf, IMSG_CTL_VERBOSE, 0, 0, -1, &verbose, + sizeof(verbose)); + printf("logging request sent.\n"); + done = 1; + break; + case SHOW_PARENT: + imsg_compose(ibuf, IMSG_CTL_SHOW_PARENT_REQUEST, 0, 0, -1, + NULL, 0); + break; + case SHOW_CONTROL: + imsg_compose(ibuf, IMSG_CTL_SHOW_CONTROL_REQUEST, 0, 0, -1, + NULL, 0); + break; + case SHOW_BROKER: + imsg_compose(ibuf, IMSG_CTL_SHOW_BROKER_REQUEST, 0, 0, -1, + NULL, 0); + break; + case SHOW_BRIDGES: + imsg_compose(ibuf, IMSG_CTL_SHOW_BRIDGES_REQUEST, 0, 0, -1, + NULL, 0); + break; + case SHOW_LISTENERS: + imsg_compose(ibuf, IMSG_CTL_SHOW_LISTENERS_REQUEST, 0, 0, -1, + NULL, 0); + break; + case SHOW_CLIENTS: + imsg_compose(ibuf, IMSG_CTL_SHOW_CLIENTS_REQUEST, 0, 0, -1, + &res->filter, sizeof(res->filter)); + break; + case SHOW_SUBSCRIPTIONS: + imsg_compose(ibuf, IMSG_CTL_SHOW_SUBS_REQUEST, 0, 0, -1, + &res->filter, sizeof(res->filter)); + break; + case SHOW_RETAINED: + imsg_compose(ibuf, IMSG_CTL_SHOW_RETAINED_REQUEST, 0, 0, -1, + &res->filter, sizeof(res->filter)); + break; + default: + usage(); + } + + if (imsgbuf_flush(ibuf) == -1) + err(1, "write error"); + + while (!done) { + if ((n = imsgbuf_read(ibuf)) == -1) + errx(1, "imsgbuf_read error"); + if (n == 0) + errx(1, "pipe closed"); + + while (!done) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + errx(1, "imsgbuf_get error"); + if (n == 0) + break; + + if (imsg.hdr.type == IMSG_CTL_FAIL) { + int e = 0; + + if (IMSG_DATA_SIZE(&imsg) >= sizeof(e)) + memcpy(&e, imsg.data, sizeof(e)); + imsg_free(&imsg); + errx(1, "request failed: %s", e == ENOENT ? + "no such client" : e == ENOTCONN ? + "client is not connected" : e == EPERM ? + "permission denied" : strerror(e)); + } + + switch (res->action) { + case KILL_CLIENT: + case DISCONNECT_ALL: + case SAVE: + done = kill_msg(&imsg); + break; + case SHOW_STATS: + done = show_stats_msg(&imsg); + break; + case SHOW_PARENT: + done = show_parent_msg(&imsg); + break; + case SHOW_CONTROL: + done = show_control_msg(&imsg); + break; + case SHOW_BROKER: + done = show_broker_msg(&imsg); + break; + case SHOW_BRIDGES: + done = show_bridges_msg(&imsg); + break; + case SHOW_LISTENERS: + done = show_listeners_msg(&imsg); + break; + case SHOW_CLIENTS: + done = show_clients_msg(&imsg); + break; + case SHOW_SUBSCRIPTIONS: + done = show_subs_msg(&imsg); + break; + case SHOW_RETAINED: + done = show_retained_msg(&imsg); + break; + default: + break; + } + imsg_free(&imsg); + } + } + + close(ctl_sock); + free(ibuf); + + return (0); +} + +static const char * +fmt_level(int verbose) +{ + switch (verbose) { + case 0: + return ("brief"); + case 1: + return ("verbose"); + default: + return ("debug"); + } +} + +static const char * +fmt_duration(time_t secs, char *buf, size_t len) +{ + long long d, h, m, s; + + if (secs < 0) + secs = 0; + d = secs / 86400; + h = (secs % 86400) / 3600; + m = (secs % 3600) / 60; + s = secs % 60; + + if (d > 0) + snprintf(buf, len, "%lldd%02lld:%02lld:%02lld", d, h, m, s); + else + snprintf(buf, len, "%02lld:%02lld:%02lld", h, m, s); + return (buf); +} + +static const char * +fmt_time(time_t t) +{ + static char buf[64]; + struct tm *tm; + + if (t == 0) + return ("never"); + if ((tm = localtime(&t)) == NULL) + return ("unknown"); + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm); + return (buf); +} + +static int +show_parent_msg(struct imsg *imsg) +{ + struct ctl_parent_info pi; + char dbuf[64]; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_PARENT_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(pi)) + errx(1, "short parent info"); + memcpy(&pi, imsg->data, sizeof(pi)); + printf("parent process\n"); + printf(" log level: %s\n", fmt_level(pi.verbose)); + printf(" started: %s\n", fmt_time(pi.started)); + printf(" uptime: %s\n", + fmt_duration(time(NULL) - pi.started, dbuf, sizeof(dbuf))); + printf(" sockets processes: %u\n", pi.prefork); + printf(" listeners: %u\n", pi.nlisteners); + break; + case IMSG_CTL_SHOW_PARENT_END: + return (1); + default: + break; + } + return (0); +} + +static int +show_control_msg(struct imsg *imsg) +{ + struct ctl_control_info ci; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_CONTROL_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(ci)) + errx(1, "short control info"); + memcpy(&ci, imsg->data, sizeof(ci)); + printf("control process\n"); + printf(" log level: %s\n", fmt_level(ci.verbose)); + break; + case IMSG_CTL_SHOW_CONTROL_END: + return (1); + default: + break; + } + return (0); +} + +static int +show_broker_msg(struct imsg *imsg) +{ + struct ctl_broker_info bi; + char dbuf[64]; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_BROKER_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(bi)) + errx(1, "short broker info"); + memcpy(&bi, imsg->data, sizeof(bi)); + printf("broker process\n"); + printf(" log level: %s\n", fmt_level(bi.verbose)); + printf(" started: %s\n", fmt_time(bi.started)); + printf(" uptime: %s\n", + fmt_duration(time(NULL) - bi.started, dbuf, sizeof(dbuf))); + printf(" sessions: %llu (%llu connected)\n", + (unsigned long long)bi.sessions, + (unsigned long long)bi.connected); + printf(" subscriptions: %llu\n", + (unsigned long long)bi.subscriptions); + printf(" retained messages: %llu\n", + (unsigned long long)bi.retained); + printf(" publish received: %llu\n", + (unsigned long long)bi.pub_received); + printf(" publish sent: %llu\n", + (unsigned long long)bi.pub_sent); + printf(" publish dropped: %llu\n", + (unsigned long long)bi.pub_dropped); + printf(" bytes in: %llu\n", + (unsigned long long)bi.bytes_in); + printf(" bytes out: %llu\n", + (unsigned long long)bi.bytes_out); + if (bi.persist) + printf(" snapshots: %llu, last %s\n", + (unsigned long long)bi.snapshots, + fmt_time(bi.last_snapshot)); + else + printf(" persistence: off\n"); + break; + case IMSG_CTL_SHOW_BROKER_END: + return (1); + default: + break; + } + return (0); +} + +/* the same numbers as show broker, one key=value per line for scripts */ +static int +show_stats_msg(struct imsg *imsg) +{ + struct ctl_broker_info bi; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_BROKER_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(bi)) + errx(1, "short broker info"); + memcpy(&bi, imsg->data, sizeof(bi)); + printf("uptime=%lld\n", (long long)(time(NULL) - bi.started)); + printf("sessions=%llu\n", (unsigned long long)bi.sessions); + printf("connected=%llu\n", (unsigned long long)bi.connected); + printf("subscriptions=%llu\n", + (unsigned long long)bi.subscriptions); + printf("retained=%llu\n", (unsigned long long)bi.retained); + printf("publish_received=%llu\n", + (unsigned long long)bi.pub_received); + printf("publish_sent=%llu\n", (unsigned long long)bi.pub_sent); + printf("publish_dropped=%llu\n", + (unsigned long long)bi.pub_dropped); + printf("bytes_in=%llu\n", (unsigned long long)bi.bytes_in); + printf("bytes_out=%llu\n", (unsigned long long)bi.bytes_out); + printf("persistence=%d\n", bi.persist); + printf("snapshots=%llu\n", (unsigned long long)bi.snapshots); + printf("last_snapshot=%lld\n", (long long)bi.last_snapshot); + break; + case IMSG_CTL_SHOW_BROKER_END: + return (1); + default: + break; + } + return (0); +} + +static int +show_bridges_msg(struct imsg *imsg) +{ + struct ctl_bridge_info bi; + char dbuf[64]; + const char *state; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_BRIDGES_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(bi)) + errx(1, "short bridge info"); + memcpy(&bi, imsg->data, sizeof(bi)); + if (!header_done) { + printf("%-16s %-40s %-11s %-3s %-5s %-14s %8s %8s " + "%6s %8s\n", "BRIDGE", "REMOTE", "STATE", "VER", + "TLS", "UP FOR", "IN", "OUT", "QUEUED", + "ATTEMPTS"); + header_done = 1; + } + switch (bi.state) { + case 0: + state = "down"; + break; + case 1: + state = "connecting"; + break; + case 2: + state = "handshake"; + break; + default: + state = "up"; + break; + } + printf("%-16s %-40s %-11s %-3u %-5s %-14s %8llu %8llu %6u " + "%8u\n", bi.name, bi.remote, state, bi.version, + bi.tls ? "yes" : "no", bi.state == 3 ? + fmt_duration(time(NULL) - bi.since, dbuf, sizeof(dbuf)) : + "-", (unsigned long long)bi.msgs_in, + (unsigned long long)bi.msgs_out, bi.nqueued, bi.attempts); + break; + case IMSG_CTL_SHOW_BRIDGES_END: + if (!header_done) + printf("no bridges configured\n"); + return (1); + default: + break; + } + return (0); +} + +/* every sockets process reports its own counts; sum them per listener */ +static int +show_listeners_msg(struct imsg *imsg) +{ + struct ctl_listener_info li; + int i; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_LISTENERS_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(li)) + errx(1, "short listener info"); + memcpy(&li, imsg->data, sizeof(li)); + for (i = 0; i < nlsum; i++) + if (lsum[i].id == li.id) + break; + if (i == nlsum) { + if (nlsum == MAX_LISTENERS) + break; + nlsum++; + lsum[i].id = li.id; + strlcpy(lsum[i].name, li.name, sizeof(lsum[i].name)); + strlcpy(lsum[i].addr, li.addr, sizeof(lsum[i].addr)); + lsum[i].port = li.port; + lsum[i].tls = li.tls; + lsum[i].websocket = li.websocket; + lsum[i].proxy = li.proxy; + } + lsum[i].nconns += li.nconns; + lsum[i].procs++; + break; + case IMSG_CTL_SHOW_LISTENERS_END: + printf("%-4s %-16s %-40s %-14s %s\n", "ID", "NAME", "ADDRESS", + "OPTIONS", "CONNECTIONS"); + for (i = 0; i < nlsum; i++) + printf("%-4u %-16s %-40s %s%s%s%-14s %u\n", lsum[i].id, + lsum[i].name, lsum[i].addr, + lsum[i].tls ? "tls " : "", + lsum[i].websocket ? "ws " : "", + lsum[i].proxy ? "proxy " : "", + lsum[i].tls || lsum[i].websocket || lsum[i].proxy ? + "" : "-", lsum[i].nconns); + return (1); + default: + break; + } + return (0); +} + +static int +show_clients_msg(struct imsg *imsg) +{ + struct ctl_client_info ci; + char ebuf[64]; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_CLIENTS_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(ci)) + errx(1, "short client info"); + memcpy(&ci, imsg->data, sizeof(ci)); + if (!header_done) { + printf("%-32s %-16s %-40s %-3s %-9s %-6s %-10s %5s %5s " + "%6s %8s %8s\n", "CLIENT ID", "USER", "PEER", "VER", + "STATE", "KEEP", "EXPIRY", "SUBS", "INFL", "QUEUED", + "IN", "OUT"); + header_done = 1; + } + printf("%-32s %-16s %-40s %-3u %-9s %-6u %-10s %5u %5u %6u " + "%8llu %8llu\n", ci.client_id, + ci.username[0] != '\0' ? ci.username : "-", + ci.connected ? ci.peer : "-", ci.version, + ci.connected ? (ci.tls ? "tls" : "connected") : "offline", + ci.keepalive, ci.expiry == UINT32_MAX ? "never" : + fmt_duration(ci.expiry, ebuf, sizeof(ebuf)), ci.nsubs, + ci.ninflight, + ci.nqueued, (unsigned long long)ci.msgs_in, + (unsigned long long)ci.msgs_out); + break; + case IMSG_CTL_SHOW_CLIENTS_END: + if (!header_done) + printf("no matching clients\n"); + return (1); + default: + break; + } + return (0); +} + +static int +show_subs_msg(struct imsg *imsg) +{ + struct ctl_sub_info si; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_SUBS_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(si)) + errx(1, "short subscription info"); + memcpy(&si, imsg->data, sizeof(si)); + if (!header_done) { + printf("%-32s %-3s %-2s %-3s %-2s %-10s %s\n", + "CLIENT ID", "QOS", "NL", "RAP", "RH", "SUBID", + "FILTER"); + header_done = 1; + } + printf("%-32s %-3u %-2u %-3u %-2u %-10u %s\n", si.client_id, + si.qos, si.no_local, si.rap, si.rh, si.id, si.filter); + break; + case IMSG_CTL_SHOW_SUBS_END: + if (!header_done) + printf("no matching subscriptions\n"); + return (1); + default: + break; + } + return (0); +} + +static int +show_retained_msg(struct imsg *imsg) +{ + struct ctl_retained_info ri; + char abuf[64], ebuf[64]; + + switch (imsg->hdr.type) { + case IMSG_CTL_SHOW_RETAINED_DATA: + if (IMSG_DATA_SIZE(imsg) < sizeof(ri)) + errx(1, "short retained info"); + memcpy(&ri, imsg->data, sizeof(ri)); + if (!header_done) { + printf("%-3s %-10s %-14s %-10s %s\n", "QOS", "BYTES", + "AGE", "EXPIRY", "TOPIC"); + header_done = 1; + } + printf("%-3u %-10u %-14s %-10s %s\n", ri.qos, ri.payload_len, + fmt_duration(time(NULL) - ri.received, abuf, sizeof(abuf)), + ri.expiry == 0 ? "never" : + fmt_duration(ri.expiry, ebuf, sizeof(ebuf)), ri.topic); + break; + case IMSG_CTL_SHOW_RETAINED_END: + if (!header_done) + printf("no matching retained messages\n"); + return (1); + default: + break; + } + return (0); +} + +static int +kill_msg(struct imsg *imsg) +{ + switch (imsg->hdr.type) { + case IMSG_CTL_OK: + printf("ok\n"); + return (1); + default: + break; + } + return (0); +} blob - /dev/null blob + 8f12a3955529707c376cf87d0683c3a27e944056223b829b5bd85d87ad345069 (mode 644) --- /dev/null +++ mqttctl/parser.c @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2020-2021, 2026 Tracey Emery + * Copyright (c) 2006 Pierre-Yves Ritschard + * Copyright (c) 2004 Esben Norby + * Copyright (c) 2003, 2004 Henning Brauer + * + * 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 "mqttd.h" +#include "parser.h" + +enum token_type { + NOTOKEN, + ENDTOKEN, + CLIENTID, + TOPIC, + KEYWORD +}; + +struct token { + enum token_type type; + const char *keyword; + int value; + const struct token *next; +}; + +static const struct token t_main[]; +static const struct token t_disconnect[]; +static const struct token t_log[]; +static const struct token t_show[]; +static const struct token t_kill_client[]; +static const struct token t_opt_client[]; +static const struct token t_opt_topic[]; + +static const struct token t_disconnect[] = { + {KEYWORD, "all", DISCONNECT_ALL, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_main[] = { + {KEYWORD, "disconnect", NONE, t_disconnect}, + {KEYWORD, "kill", NONE, t_kill_client}, + {KEYWORD, "log", NONE, t_log}, + {KEYWORD, "save", SAVE, NULL}, + {KEYWORD, "show", SHOW, t_show}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_log[] = { + {KEYWORD, "brief", LOG_BRIEF, NULL}, + {KEYWORD, "debug", LOG_DEBUG, NULL}, + {KEYWORD, "verbose", LOG_VERBOSE, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_show[] = { + {KEYWORD, "bridges", SHOW_BRIDGES, NULL}, + {KEYWORD, "broker", SHOW_BROKER, NULL}, + {KEYWORD, "clients", SHOW_CLIENTS, t_opt_client}, + {KEYWORD, "control", SHOW_CONTROL, NULL}, + {KEYWORD, "listeners", SHOW_LISTENERS, NULL}, + {KEYWORD, "parent", SHOW_PARENT, NULL}, + {KEYWORD, "retained", SHOW_RETAINED, t_opt_topic}, + {KEYWORD, "stats", SHOW_STATS, NULL}, + {KEYWORD, "subscriptions", SHOW_SUBSCRIPTIONS, t_opt_client}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_kill_client[] = { + {CLIENTID, "", KILL_CLIENT, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_opt_client[] = { + {NOTOKEN, "", NONE, NULL}, + {CLIENTID, "", NONE, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token t_opt_topic[] = { + {NOTOKEN, "", NONE, NULL}, + {TOPIC, "", NONE, NULL}, + {ENDTOKEN, "", NONE, NULL} +}; + +static const struct token *match_token(const char *, const struct token *, + struct parse_result *); +static void show_valid_args(const struct token *); + +struct parse_result * +parse(int argc, char *argv[]) +{ + static struct parse_result res; + const struct token *table = t_main; + const struct token *match; + + memset(&res, 0, sizeof(res)); + + while (argc >= 0) { + if ((match = match_token(argv[0], table, &res)) == NULL) { + fprintf(stderr, "valid commands/args:\n"); + show_valid_args(table); + return (NULL); + } + + argc--; + argv++; + + if (match->type == NOTOKEN || match->next == NULL) + break; + + table = match->next; + } + + if (argc > 0) { + fprintf(stderr, "superfluous argument: %s\n", argv[0]); + return (NULL); + } + + return (&res); +} + +static const struct token * +match_token(const char *word, const struct token *table, + struct parse_result *res) +{ + const struct token *t = NULL; + u_int i, match = 0; + + for (i = 0; table[i].type != ENDTOKEN; i++) { + switch (table[i].type) { + case NOTOKEN: + if (word == NULL || strlen(word) == 0) { + match++; + t = &table[i]; + } + break; + case CLIENTID: + case TOPIC: + if (!match && word != NULL && strlen(word) > 0) { + if (strlcpy(res->filter.name, word, + sizeof(res->filter.name)) >= + sizeof(res->filter.name)) { + fprintf(stderr, "argument too long\n"); + return (NULL); + } + match++; + t = &table[i]; + if (t->value) + res->action = t->value; + } + break; + case KEYWORD: + if (word != NULL && strncmp(word, table[i].keyword, + strlen(word)) == 0) { + match++; + t = &table[i]; + if (t->value) + res->action = t->value; + } + break; + case ENDTOKEN: + break; + } + } + + if (match != 1) { + if (word == NULL) + fprintf(stderr, "missing argument:\n"); + else if (match > 1) + fprintf(stderr, "ambiguous argument: %s\n", word); + else if (match < 1) + fprintf(stderr, "unknown argument: %s\n", word); + return (NULL); + } + + return (t); +} + +static void +show_valid_args(const struct token *table) +{ + int i; + + for (i = 0; table[i].type != ENDTOKEN; i++) { + switch (table[i].type) { + case NOTOKEN: + fprintf(stderr, " \n"); + break; + case CLIENTID: + fprintf(stderr, " \n"); + break; + case TOPIC: + fprintf(stderr, " \n"); + break; + case KEYWORD: + fprintf(stderr, " %s\n", table[i].keyword); + break; + case ENDTOKEN: + break; + } + } +} blob - /dev/null blob + 78b3fe27872f601e29b3584ccfc3884df32b1775487df84e0979f76d87c5b610 (mode 644) --- /dev/null +++ mqttctl/parser.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020-2021, 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 + * 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 actions { + NONE, + DISCONNECT_ALL, + KILL_CLIENT, + LOG_BRIEF, + LOG_DEBUG, + LOG_VERBOSE, + SAVE, + SHOW, + SHOW_BRIDGES, + SHOW_BROKER, + SHOW_CLIENTS, + SHOW_CONTROL, + SHOW_LISTENERS, + SHOW_PARENT, + SHOW_RETAINED, + SHOW_STATS, + SHOW_SUBSCRIPTIONS, +}; + +struct parse_result { + enum actions action; + struct ctl_filter filter; +}; + +struct parse_result *parse(int, char *[]); blob - /dev/null blob + 8af8c13e9d413c115c68638a094f11e0317a8c87781fd260589f8e21903d94f8 (mode 644) --- /dev/null +++ mqttd/Makefile @@ -0,0 +1,25 @@ +PROG = mqttd +SRCS = parse.y +SRCS += mqttd.c config.c control.c log.c +SRCS += sockets.c tls.c +SRCS += broker.c session.c topic.c mqtt.c db.c storage.c auth.c +SRCS += bridge.c sparkplug.c snowflake.c jwt.c + +MAN = mqttd.8 mqttd.conf.5 + +PREFIX= /usr/local +BINDIR?= ${PREFIX}/sbin +MANDIR?= ${PREFIX}/man/man + +LDADD = -levent -ltls -lssl -lcrypto -lutil +DPADD = ${LIBEVENT} ${LIBTLS} ${LIBSSL} ${LIBCRYPTO} ${LIBUTIL} +CFLAGS += -Wall -I${.CURDIR} +CFLAGS += -Wstrict-prototypes -Wmissing-prototypes +CFLAGS += -Wmissing-declarations +CFLAGS += -Wshadow -Wpointer-arith -fno-common +CFLAGS += -Wsign-compare +YFLAGS = + +CLEANFILES+= y.tab.h + +.include blob - /dev/null blob + 8e4cea3f56302abc277ef01567f16050844b14584467cde990df15aa648213a5 (mode 644) --- /dev/null +++ mqttd/auth.c @@ -0,0 +1,635 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * The auth process keeps root so it can run pfctl and spawn the external + * authenticator, which itself runs as the daemon user. The broker asks + * it two things: is this username and password acceptable, and please + * ban this address. It holds no broker state. + * + * Authenticator line protocol, one request per line, hex keeps the + * fields free of spaces and newlines: + * + * auth + * ok | fail + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" + +struct pending { + TAILQ_ENTRY(pending) entry; + uint32_t id; + time_t started; +}; +TAILQ_HEAD(pendinglist, pending); + +static void auth_dispatch_parent(int, short, void *); +static void auth_dispatch_broker(int, short, void *); +static void auth_sighdlr(int, short, void *); +static __dead void + auth_shutdown(void); +static void auth_request(struct imsg *); +static void auth_reply(uint32_t, int); +static void auth_fail_all(void); +static void auth_timeouts(int, short, void *); +static void helper_spawn(int, short, void *); +static void helper_readcb(struct bufferevent *, void *); +static void helper_errcb(struct bufferevent *, short, void *); +static void helper_close(void); +static void hexencode(const char *, char *, size_t); +static void hexencode_bin(const uint8_t *, size_t, char *, size_t); +static int hexdecode(const char *, uint8_t *, size_t); +static void pf_ban(struct imsg *); + +static struct imsgev *auth_iev_parent; +static struct imsgev *auth_iev_broker; + +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 event ev_respawn; +static struct event ev_timeouts; + +static struct pendinglist pending = TAILQ_HEAD_INITIALIZER(pending); +static struct bufferevent *helper_bev; +static pid_t helper_pid = -1; +static int helper_fd = -1; + +__dead void +auth(int fd_parent, int fd_broker) +{ + struct timeval tv; + + log_procinit("auth"); + setproctitle("%s", "auth"); + + /* pfctl and the helper are reaped by the kernel */ + signal(SIGCHLD, SIG_IGN); + + event_init(); + + signal_set(&ev_sigint, SIGINT, auth_sighdlr, NULL); + signal_set(&ev_sigterm, SIGTERM, auth_sighdlr, NULL); + signal_set(&ev_sighup, SIGHUP, auth_sighdlr, NULL); + signal_set(&ev_sigpipe, SIGPIPE, auth_sighdlr, NULL); + signal_set(&ev_sigusr1, SIGUSR1, auth_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 ((auth_iev_parent = calloc(1, sizeof(*auth_iev_parent))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(auth_iev_parent, fd_parent, auth_dispatch_parent); + + if ((auth_iev_broker = calloc(1, sizeof(*auth_iev_broker))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(auth_iev_broker, fd_broker, auth_dispatch_broker); + + evtimer_set(&ev_respawn, helper_spawn, NULL); + evtimer_set(&ev_timeouts, auth_timeouts, NULL); + tv.tv_sec = 1; + tv.tv_usec = 0; + evtimer_add(&ev_timeouts, &tv); + + if (env->conf.auth_exec_set) + helper_spawn(0, 0, NULL); + + if (pledge("stdio proc exec id getpw", NULL) == -1) + fatal("pledge"); + + event_dispatch(); + + auth_shutdown(); +} + +static void +auth_sighdlr(int sig, short event, void *arg) +{ + switch (sig) { + case SIGINT: + case SIGTERM: + auth_shutdown(); + break; + case SIGHUP: + case SIGPIPE: + case SIGUSR1: + break; + default: + fatalx("%s: unexpected signal", __func__); + } +} + +static __dead void +auth_shutdown(void) +{ + helper_close(); + if (helper_pid > 0) + kill(helper_pid, SIGTERM); + + imsg_event_shutdown(auth_iev_parent); + free(auth_iev_parent); + imsg_event_shutdown(auth_iev_broker); + free(auth_iev_broker); + + log_info("auth exiting, pid %d", getpid()); + exit(0); +} + +static void +auth_dispatch_parent(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n, verbose; + + 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__); + auth_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) + auth_shutdown(); + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_GLOBAL: + config_recv_global(env, &imsg); + break; + case IMSG_CFG_DONE: + imsg_compose_event(iev, IMSG_CFG_DONE, 0, 0, -1, NULL, + 0); + break; + case IMSG_CTL_START: + break; + case IMSG_CTL_VERBOSE: + IMSG_SIZE_CHECK(&imsg, &verbose); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + break; + default: + log_warnx("%s: unexpected imsg %d from parent", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +auth_dispatch_broker(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost broker process", __func__); + auth_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) + auth_shutdown(); + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_AUTH_REQUEST: + auth_request(&imsg); + break; + case IMSG_PF_BAN: + pf_ban(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from broker", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +hexencode_bin(const uint8_t *s, size_t n, char *out, size_t outlen) +{ + static const char hex[] = "0123456789abcdef"; + size_t i; + + if (outlen < 2 * n + 1) + n = (outlen - 1) / 2; + for (i = 0; i < n; i++) { + out[2 * i] = hex[s[i] >> 4]; + out[2 * i + 1] = hex[s[i] & 0x0f]; + } + out[2 * n] = '\0'; +} + +static void +hexencode(const char *s, char *out, size_t outlen) +{ + hexencode_bin((const uint8_t *)s, strlen(s), out, outlen); +} + +/* returns the decoded length or -1 */ +static int +hexdecode(const char *s, uint8_t *out, size_t outlen) +{ + size_t i, n = strlen(s); + int hi, lo; + + if (n % 2 != 0 || n / 2 > outlen) + return (-1); + for (i = 0; i < n; i += 2) { + hi = s[i] >= '0' && s[i] <= '9' ? s[i] - '0' : + s[i] >= 'a' && s[i] <= 'f' ? s[i] - 'a' + 10 : + s[i] >= 'A' && s[i] <= 'F' ? s[i] - 'A' + 10 : -1; + lo = s[i + 1] >= '0' && s[i + 1] <= '9' ? s[i + 1] - '0' : + s[i + 1] >= 'a' && s[i + 1] <= 'f' ? s[i + 1] - 'a' + 10 : + s[i + 1] >= 'A' && s[i + 1] <= 'F' ? s[i + 1] - 'A' + 10 : + -1; + if (hi == -1 || lo == -1) + return (-1); + out[i / 2] = hi << 4 | lo; + } + return (n / 2); +} + +static void +auth_reply(uint32_t id, int ok) +{ + struct auth_reply r; + + memset(&r, 0, sizeof(r)); + r.id = id; + r.ok = ok; + imsg_compose_event(auth_iev_broker, IMSG_AUTH_REPLY, 0, 0, -1, &r, + sizeof(r)); +} + +static void +auth_request(struct imsg *imsg) +{ + struct auth_request req; + struct pending *p; + char user[2 * MQTTD_MAXNAME + 1]; + char pass[2 * MQTTD_MAXPASSWORD + 1]; + char cid[2 * (MQTTD_MAX_CLIENTID + 1) + 1]; + char method[2 * MQTTD_AUTH_METHOD_MAX + 1]; + char data[2 * MQTTD_AUTH_DATA_MAX + 1]; + char *line; + int len; + + IMSG_SIZE_CHECK(imsg, &req); + memcpy(&req, imsg->data, sizeof(req)); + req.user[sizeof(req.user) - 1] = '\0'; + req.password[sizeof(req.password) - 1] = '\0'; + req.client_id[sizeof(req.client_id) - 1] = '\0'; + req.peer[sizeof(req.peer) - 1] = '\0'; + req.method[sizeof(req.method) - 1] = '\0'; + if (req.datalen > sizeof(req.data)) + req.datalen = sizeof(req.data); + + if (helper_bev == NULL) { + explicit_bzero(&req, sizeof(req)); + auth_reply(req.id, 0); + return; + } + + hexencode(req.user, user, sizeof(user)); + hexencode(req.client_id, cid, sizeof(cid)); + hexencode(req.method, method, sizeof(method)); + hexencode_bin(req.data, req.datalen, data, sizeof(data)); + + switch (req.kind) { + case AUTH_REQ_PASSWORD: + hexencode(req.password, pass, sizeof(pass)); + len = asprintf(&line, "auth %u %s %s %s %s\n", req.id, user, + pass, cid, req.peer); + explicit_bzero(pass, sizeof(pass)); + break; + case AUTH_REQ_START: + len = asprintf(&line, "auth-start %u %s %s %s %s %s\n", req.id, + user, cid, req.peer, method, data); + break; + case AUTH_REQ_DATA: + len = asprintf(&line, "auth-data %u %s\n", req.id, data); + break; + default: + len = -1; + break; + } + explicit_bzero(&req.password, sizeof(req.password)); + explicit_bzero(data, sizeof(data)); + if (len == -1) { + auth_reply(req.id, 0); + return; + } + + if ((p = calloc(1, sizeof(*p))) == NULL) { + freezero(line, len); + auth_reply(req.id, 0); + return; + } + p->id = req.id; + p->started = time(NULL); + TAILQ_INSERT_TAIL(&pending, p, entry); + + bufferevent_write(helper_bev, line, len); + freezero(line, len); +} + +static void +auth_fail_all(void) +{ + struct pending *p; + + while ((p = TAILQ_FIRST(&pending)) != NULL) { + TAILQ_REMOVE(&pending, p, entry); + auth_reply(p->id, 0); + free(p); + } +} + +/* a helper that does not answer in time fails the request, not the broker */ +static void +auth_timeouts(int fd, short event, void *arg) +{ + struct pending *p, *tmp; + struct timeval tv; + time_t now = time(NULL); + + TAILQ_FOREACH_SAFE(p, &pending, entry, tmp) { + if (now - p->started < MQTTD_AUTH_TIMEOUT) + break; + log_warnx("%s: authenticator did not answer request %u", + __func__, p->id); + TAILQ_REMOVE(&pending, p, entry); + auth_reply(p->id, 0); + free(p); + } + + tv.tv_sec = 1; + tv.tv_usec = 0; + evtimer_add(&ev_timeouts, &tv); +} + +static void +helper_close(void) +{ + if (helper_bev != NULL) { + bufferevent_free(helper_bev); + helper_bev = NULL; + } + if (helper_fd != -1) { + close(helper_fd); + helper_fd = -1; + } +} + +/* the authenticator runs unprivileged with the socket as stdin and stdout */ +static void +helper_spawn(int fd, short event, void *arg) +{ + struct passwd *pw; + struct timeval tv; + int sp[2]; + + if (!env->conf.auth_exec_set) + return; + + if ((pw = getpwnam(MQTTD_USER)) == NULL) + fatal("unknown user %s", MQTTD_USER); + + if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) { + log_warn("%s: socketpair", __func__); + goto retry; + } + + switch (helper_pid = fork()) { + case -1: + log_warn("%s: fork", __func__); + close(sp[0]); + close(sp[1]); + goto retry; + case 0: + 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)) + _exit(1); + if (dup2(sp[1], STDIN_FILENO) == -1 || + dup2(sp[1], STDOUT_FILENO) == -1) + _exit(1); + closefrom(STDERR_FILENO + 1); + execl(env->auth_exec, env->auth_exec, (char *)NULL); + _exit(1); + } + close(sp[1]); + + helper_fd = sp[0]; + if (fcntl(helper_fd, F_SETFL, O_NONBLOCK) == -1) + log_warn("%s: fcntl", __func__); + helper_bev = bufferevent_new(helper_fd, helper_readcb, NULL, + helper_errcb, NULL); + if (helper_bev == NULL) { + log_warn("%s: bufferevent_new", __func__); + helper_close(); + goto retry; + } + bufferevent_enable(helper_bev, EV_READ | EV_WRITE); + log_info("authenticator %s started, pid %d", env->auth_exec, + helper_pid); + return; + +retry: + tv.tv_sec = MQTTD_AUTH_RESPAWN; + tv.tv_usec = 0; + evtimer_add(&ev_respawn, &tv); +} + +/* + * ok [data-hex] [user-hex] + * continue + * fail + */ +static void +helper_readcb(struct bufferevent *bev, void *arg) +{ + struct auth_reply r; + struct pending *p; + char *line, *end, *word, *verb, *arg1, *arg2; + const char *errstr; + uint32_t id; + int n; + + while ((line = evbuffer_readline(EVBUFFER_INPUT(bev))) != NULL) { + end = line; + if ((word = strsep(&end, " \t")) == NULL || *word == '\0') + goto next; + id = strtonum(word, 1, UINT32_MAX, &errstr); + if (errstr != NULL || end == NULL) + goto next; + while ((verb = strsep(&end, " \t")) != NULL && *verb == '\0') + ; + if (verb == NULL) + goto next; + arg1 = arg2 = NULL; + while (end != NULL && (arg1 = strsep(&end, " \t")) != NULL && + *arg1 == '\0') + ; + while (end != NULL && (arg2 = strsep(&end, " \t")) != NULL && + *arg2 == '\0') + ; + + memset(&r, 0, sizeof(r)); + r.id = id; + if (strcmp(verb, "ok") == 0) { + r.ok = 1; + if (arg1 != NULL && *arg1 != '\0') { + n = hexdecode(arg1, r.data, sizeof(r.data)); + if (n == -1) + goto bad; + r.datalen = n; + } + if (arg2 != NULL && *arg2 != '\0') { + n = hexdecode(arg2, (uint8_t *)r.user, + sizeof(r.user) - 1); + if (n == -1) + goto bad; + r.user[n] = '\0'; + } + } else if (strcmp(verb, "continue") == 0) { + if (arg1 == NULL) + goto bad; + n = hexdecode(arg1, r.data, sizeof(r.data)); + if (n == -1) + goto bad; + r.datalen = n; + r.cont = 1; + } else if (strcmp(verb, "fail") != 0) { +bad: + log_warnx("%s: authenticator said \"%s\"", __func__, + verb); + goto next; + } + + TAILQ_FOREACH(p, &pending, entry) + if (p->id == id) + break; + if (p != NULL) { + TAILQ_REMOVE(&pending, p, entry); + free(p); + imsg_compose_event(auth_iev_broker, IMSG_AUTH_REPLY, 0, + 0, -1, &r, sizeof(r)); + } +next: + explicit_bzero(&r, sizeof(r)); + free(line); + } +} + +static void +helper_errcb(struct bufferevent *bev, short what, void *arg) +{ + struct timeval tv; + + log_warnx("authenticator %s exited, restarting in %d seconds", + env->auth_exec, MQTTD_AUTH_RESPAWN); + helper_close(); + helper_pid = -1; + auth_fail_all(); + + tv.tv_sec = MQTTD_AUTH_RESPAWN; + tv.tv_usec = 0; + evtimer_add(&ev_respawn, &tv); +} + +static void +pf_ban(struct imsg *imsg) +{ + struct pf_ban ban; + + IMSG_SIZE_CHECK(imsg, &ban); + memcpy(&ban, imsg->data, sizeof(ban)); + ban.addr[sizeof(ban.addr) - 1] = '\0'; + + if (env->pf_table[0] == '\0' || ban.addr[0] == '\0' || + strpbrk(ban.addr, " \t\n") != NULL) + return; + + switch (fork()) { + case -1: + log_warn("%s: fork", __func__); + return; + case 0: + closefrom(STDERR_FILENO + 1); + execl(MQTTD_PFCTL, "pfctl", "-q", "-t", env->pf_table, "-T", + "add", ban.addr, (char *)NULL); + _exit(1); + } + + log_info("event=ban peer=%s table=%s", ban.addr, env->pf_table); +} blob - /dev/null blob + 37147be5cc9d0b8834b998b4d930f048680985cf4bcf06c133b5de8a14df983d (mode 644) --- /dev/null +++ mqttd/bridge.c @@ -0,0 +1,614 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * Bridges: the broker as an MQTT client of another broker. A sockets + * process opens the TCP/TLS connection on request; from then on the + * connection looks like any other, except that we send CONNECT and + * SUBSCRIBE and expect CONNACK, SUBACK and PINGRESP. Local messages for + * the remote flow through the bridge's own session like any delivery; + * remote messages are routed locally with the bridge session as origin + * so no-local keeps them from going straight back. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "mqttd.h" +#include "mqtt.h" +#include "broker.h" + +static struct bridge + *bridge_find(uint32_t); +static void bridge_connect_now(struct bridge *); +static void bridge_schedule(struct bridge *); +static void bridge_reconnect_cb(int, short, void *); +static void bridge_ping_cb(int, short, void *); +static void bridge_drop(struct bridge *, const char *); +static void bridge_send_connect(struct bridge *); +static void bridge_send_subscribe(struct bridge *); +static int bridge_map_in(struct bridge *, const char *, size_t, char *, + size_t); +static void bridge_publish_in(struct bconn *, uint8_t, const uint8_t *, + size_t); +static int prefix_join(char *, size_t, const char *, const char *, + size_t); + +static struct bridge * +bridge_find(uint32_t id) +{ + struct bridge *br; + + TAILQ_FOREACH(br, &broker_env->bridges, entry) + if (br->conf.id == id) + return (br); + return (NULL); +} + +static int +prefix_join(char *out, size_t outlen, const char *prefix, const char *rest, + size_t restlen) +{ + int n; + + n = snprintf(out, outlen, "%s%.*s", prefix, (int)restlen, rest); + if (n < 0 || (size_t)n >= outlen) + return (-1); + return (n); +} + +void +bridge_config_recv(struct imsg *imsg) +{ + struct bridge *br; + struct session *s; + char cid[MQTTD_MAX_CLIENTID + 1]; + + if ((br = calloc(1, sizeof(*br))) == NULL) + fatal("%s: calloc", __func__); + IMSG_SIZE_CHECK(imsg, &br->conf); + memcpy(&br->conf, imsg->data, sizeof(br->conf)); + br->conf.name[sizeof(br->conf.name) - 1] = '\0'; + br->conf.host[sizeof(br->conf.host) - 1] = '\0'; + br->conf.client_id[sizeof(br->conf.client_id) - 1] = '\0'; + br->conf.username[sizeof(br->conf.username) - 1] = '\0'; + br->conf.password[sizeof(br->conf.password) - 1] = '\0'; + br->conf.tls_ca_file = NULL; + br->conf.tls_ca = NULL; + br->conf.tls_ca_len = 0; + TAILQ_INIT(&br->topics); + br->backoff = MQTTD_BRIDGE_BACKOFF_MIN; + evtimer_set(&br->reconnect_ev, bridge_reconnect_cb, br); + evtimer_set(&br->ping_ev, bridge_ping_cb, br); + + if (bridge_find(br->conf.id) != NULL) { + log_warnx("%s: duplicate bridge %u", __func__, br->conf.id); + free(br); + return; + } + + snprintf(cid, sizeof(cid), "bridge:%s", br->conf.name); + if ((s = session_find(cid)) == NULL && (s = session_new(cid)) == NULL) + fatal("%s: session_new", __func__); + s->bridge = br; + s->version = br->conf.version; + s->expiry_interval = UINT32_MAX; + s->recv_max = 0; + s->max_queued = br->conf.max_queued; + br->session = s; + + TAILQ_INSERT_TAIL(&broker_env->bridges, br, entry); + log_debug("%s: bridge %s to %s configured", __func__, br->conf.name, + br->conf.host); +} + +void +bridge_topic_recv(struct imsg *imsg) +{ + struct bridge_topic *bt; + struct bridge *br; + struct subscription *sub; + char filter[MQTTD_MAXTEXT + MQTTD_MAXPREFIX]; + int n; + + if ((bt = calloc(1, sizeof(*bt))) == NULL) + fatal("%s: calloc", __func__); + IMSG_SIZE_CHECK(imsg, &bt->conf); + memcpy(&bt->conf, imsg->data, sizeof(bt->conf)); + bt->conf.pattern[sizeof(bt->conf.pattern) - 1] = '\0'; + bt->conf.local_prefix[sizeof(bt->conf.local_prefix) - 1] = '\0'; + bt->conf.remote_prefix[sizeof(bt->conf.remote_prefix) - 1] = '\0'; + + if ((br = bridge_find(bt->conf.bridge_id)) == NULL) { + free(bt); + return; + } + TAILQ_INSERT_TAIL(&br->topics, bt, entry); + + if ((bt->conf.dir & BRIDGE_OUT) == 0) + return; + + /* out-topics are local subscriptions held by the bridge session */ + n = prefix_join(filter, sizeof(filter), bt->conf.local_prefix, + bt->conf.pattern, strlen(bt->conf.pattern)); + if (n == -1 || !mqtt_filter_valid((const uint8_t *)filter, n)) { + log_warnx("bridge %s: invalid local filter %s", br->conf.name, + filter); + return; + } + if (topic_find_sub(&broker_env->topics, filter, n, NULL, + br->session) != NULL) + return; + if ((sub = calloc(1, sizeof(*sub))) == NULL) + fatal("%s: calloc", __func__); + if (topic_subscribe(&broker_env->topics, filter, n, sub) == NULL) { + free(sub); + return; + } + sub->session = br->session; + sub->qos = bt->conf.qos; + sub->no_local = 1; + sub->rap = 1; + TAILQ_INSERT_TAIL(&br->session->subs, sub, sess_entry); + br->session->nsubs++; +} + +void +bridge_start(void) +{ + struct bridge *br; + + TAILQ_FOREACH(br, &broker_env->bridges, entry) + if (br->state == BRIDGE_DOWN && + !evtimer_pending(&br->reconnect_ev, NULL)) + bridge_connect_now(br); +} + +static void +bridge_connect_now(struct bridge *br) +{ + br->state = BRIDGE_CONNECTING; + br->attempts++; + log_debug("bridge %s: connecting to %s (attempt %u)", br->conf.name, + br->conf.host, br->attempts); + broker_socks_send(br->conf.proc, IMSG_BRIDGE_CONNECT, &br->conf.id, + sizeof(br->conf.id)); +} + +static void +bridge_schedule(struct bridge *br) +{ + struct timeval tv; + + br->state = BRIDGE_DOWN; + evtimer_del(&br->ping_ev); + if (evtimer_pending(&br->reconnect_ev, NULL)) + return; + tv.tv_sec = br->backoff; + tv.tv_usec = 0; + evtimer_add(&br->reconnect_ev, &tv); + log_debug("bridge %s: retry in %u second(s)", br->conf.name, + br->backoff); + br->backoff *= 2; + if (br->backoff > MQTTD_BRIDGE_BACKOFF_MAX) + br->backoff = MQTTD_BRIDGE_BACKOFF_MAX; +} + +static void +bridge_reconnect_cb(int fd, short event, void *arg) +{ + struct bridge *br = arg; + + if (br->conn == NULL) + bridge_connect_now(br); +} + +void +bridge_failed(struct imsg *imsg) +{ + struct bridge *br; + uint32_t id; + + IMSG_SIZE_CHECK(imsg, &id); + memcpy(&id, imsg->data, sizeof(id)); + if ((br = bridge_find(id)) == NULL) + return; + log_info("event=bridge-fail bridge=\"%s\" remote=%s", br->conf.name, + br->conf.host); + bridge_schedule(br); +} + +/* the sockets process has a connection up; we are the client now */ +void +bridge_conn_open(struct bconn *bc, uint32_t id) +{ + struct bridge *br; + + if ((br = bridge_find(id)) == NULL || br->conn != NULL) { + log_warnx("%s: unexpected bridge connection %u", __func__, id); + broker_close(bc, RC_NONE, DETACH_WILL); + return; + } + bc->bridge = br; + bc->connected = 1; + bc->version = br->conf.version; + bc->last_rx = time(NULL); + br->conn = bc; + br->state = BRIDGE_WAIT_CONNACK; + bridge_send_connect(br); +} + +static void +bridge_send_connect(struct bridge *br) +{ + struct mqtt_buf out; + + mqtt_buf_init(&out); + if (mqtt_encode_connect(&out, br->conf.version, br->conf.client_id, + br->conf.username, br->conf.password, br->conf.keepalive) == 0) + broker_send(br->conn, &out); + mqtt_buf_free(&out); +} + +static void +bridge_send_subscribe(struct bridge *br) +{ + struct bridge_topic *bt; + struct mqtt_subopt *opts; + struct mqtt_buf out; + char (*filters)[MQTTD_MAXTEXT + MQTTD_MAXPREFIX]; + size_t n = 0, i; + int len; + + TAILQ_FOREACH(bt, &br->topics, entry) + if (bt->conf.dir & BRIDGE_IN) + n++; + if (n == 0) + return; + + if ((opts = calloc(n, sizeof(*opts))) == NULL || + (filters = calloc(n, sizeof(*filters))) == NULL) + fatal("%s: calloc", __func__); + + i = 0; + TAILQ_FOREACH(bt, &br->topics, entry) { + if ((bt->conf.dir & BRIDGE_IN) == 0) + continue; + len = prefix_join(filters[i], sizeof(filters[i]), + bt->conf.remote_prefix, bt->conf.pattern, + strlen(bt->conf.pattern)); + if (len == -1) + continue; + opts[i].filter.data = (const uint8_t *)filters[i]; + opts[i].filter.len = len; + opts[i].qos = bt->conf.qos; + opts[i].no_local = 1; + opts[i].rap = 1; + i++; + } + + if (++br->sub_pid == 0) + br->sub_pid = 1; + mqtt_buf_init(&out); + if (i > 0 && mqtt_encode_subscribe(&out, br->conf.version, + br->sub_pid, opts, i) == 0) + broker_send(br->conn, &out); + mqtt_buf_free(&out); + free(opts); + free(filters); +} + +static void +bridge_drop(struct bridge *br, const char *why) +{ + log_info("event=bridge-down bridge=\"%s\" remote=%s reason=\"%s\"", + br->conf.name, br->conf.host, why); + if (br->conn != NULL) + broker_close(br->conn, RC_NONE, DETACH_WILL); + else + bridge_schedule(br); +} + +/* bconn_free calls this for every bridge connection that goes away */ +void +bridge_conn_closed(struct bconn *bc) +{ + struct bridge *br = bc->bridge; + + bc->bridge = NULL; + if (br == NULL || br->conn != bc) + return; + br->conn = NULL; + if (br->state == BRIDGE_UP) + log_info("event=bridge-down bridge=\"%s\" remote=%s " + "reason=\"connection closed\"", br->conf.name, + br->conf.host); + bridge_schedule(br); +} + +static void +bridge_ping_cb(int fd, short event, void *arg) +{ + struct bridge *br = arg; + struct mqtt_buf out; + struct timeval tv; + + if (br->state != BRIDGE_UP || br->conn == NULL || + br->conf.keepalive == 0) + return; + + if (time(NULL) - br->conn->last_rx > (time_t)br->conf.keepalive * 3 / 2) { + bridge_drop(br, "keepalive timeout"); + return; + } + + mqtt_buf_init(&out); + if (mqtt_encode_pingreq(&out) == 0) + broker_send(br->conn, &out); + mqtt_buf_free(&out); + + tv.tv_sec = br->conf.keepalive / 2 > 0 ? br->conf.keepalive / 2 : 1; + tv.tv_usec = 0; + evtimer_add(&br->ping_ev, &tv); +} + +/* + * Returns 1 when the packet was consumed here. Anything else, the QoS + * handshakes mainly, is handled by the normal client code once the + * bridge session is attached. + */ +int +bridge_packet(struct bconn *bc, uint8_t type, uint8_t flags, + const uint8_t *body, size_t len) +{ + struct bridge *br = bc->bridge; + struct mqtt_connack ca; + struct timeval tv; + const uint8_t *codes; + size_t ncodes, i; + uint16_t pid; + int rc; + + bc->last_rx = time(NULL); + + switch (type) { + case MQTT_CONNACK: + if (br->state != BRIDGE_WAIT_CONNACK) { + bridge_drop(br, "unexpected CONNACK"); + return (1); + } + rc = mqtt_parse_connack(body, len, br->conf.version, &ca); + if (rc != MQTT_RC_SUCCESS || ca.reason != MQTT_RC_SUCCESS) { + log_warnx("bridge %s: remote refused CONNECT (0x%02x)", + br->conf.name, rc != MQTT_RC_SUCCESS ? rc : + ca.reason); + bridge_drop(br, "CONNECT refused"); + return (1); + } + if (br->conf.version == MQTT_V5) { + br->session->recv_max = MQTT_PROP_ISSET(&ca.props, + MQTT_PROP_RECEIVE_MAXIMUM) ? + ca.props.receive_maximum : 65535; + br->session->max_packet = MQTT_PROP_ISSET(&ca.props, + MQTT_PROP_MAXIMUM_PACKET_SIZE) ? + ca.props.maximum_packet_size : 0; + if (MQTT_PROP_ISSET(&ca.props, + MQTT_PROP_SERVER_KEEP_ALIVE) && + ca.props.server_keep_alive != 0) + br->conf.keepalive = ca.props.server_keep_alive; + } + session_attach(br->session, bc); + br->state = BRIDGE_UP; + br->backoff = MQTTD_BRIDGE_BACKOFF_MIN; + br->since = time(NULL); + log_info("event=bridge-up bridge=\"%s\" remote=%s version=%d", + br->conf.name, br->conf.host, br->conf.version); + bridge_send_subscribe(br); + session_resume(br->session); + /* keepalive 0 (sinks) means no pinging and no timeout */ + if (br->conf.keepalive > 0) { + tv.tv_sec = br->conf.keepalive / 2 > 0 ? + br->conf.keepalive / 2 : 1; + tv.tv_usec = 0; + evtimer_add(&br->ping_ev, &tv); + } + return (1); + case MQTT_SUBACK: + if (mqtt_parse_suback(body, len, br->conf.version, &pid, &codes, + &ncodes) != MQTT_RC_SUCCESS) { + bridge_drop(br, "malformed SUBACK"); + return (1); + } + for (i = 0; i < ncodes; i++) + if (codes[i] >= 0x80) + log_warnx("bridge %s: remote refused " + "subscription %zu (0x%02x)", br->conf.name, + i + 1, codes[i]); + return (1); + case MQTT_UNSUBACK: + case MQTT_PINGRESP: + return (1); + case MQTT_PUBLISH: + if (br->state != BRIDGE_UP) { + bridge_drop(br, "PUBLISH before CONNACK"); + return (1); + } + bridge_publish_in(bc, flags, body, len); + return (1); + case MQTT_DISCONNECT: + bridge_drop(br, "remote sent DISCONNECT"); + return (1); + case MQTT_CONNECT: + case MQTT_SUBSCRIBE: + case MQTT_UNSUBSCRIBE: + case MQTT_AUTH: + bridge_drop(br, "protocol error"); + return (1); + default: + if (br->state != BRIDGE_UP) { + bridge_drop(br, "packet before CONNACK"); + return (1); + } + return (0); + } +} + +/* remote topic -> local topic through the first matching in-rule */ +static int +bridge_map_in(struct bridge *br, const char *topic, size_t len, char *out, + size_t outlen) +{ + struct bridge_topic *bt; + char filter[MQTTD_MAXTEXT + MQTTD_MAXPREFIX]; + size_t plen; + + TAILQ_FOREACH(bt, &br->topics, entry) { + if ((bt->conf.dir & BRIDGE_IN) == 0) + continue; + if (prefix_join(filter, sizeof(filter), bt->conf.remote_prefix, + bt->conf.pattern, strlen(bt->conf.pattern)) == -1) + continue; + if (!broker_filter_match(filter, topic, len)) + continue; + plen = strlen(bt->conf.remote_prefix); + if (len < plen || memcmp(topic, bt->conf.remote_prefix, plen)) + continue; + return (prefix_join(out, outlen, bt->conf.local_prefix, + topic + plen, len - plen)); + } + return (-1); +} + +/* local topic -> remote topic through the first matching out-rule */ +int +bridge_map_out(struct bridge *br, const char *topic, size_t len, char *out, + size_t outlen) +{ + struct bridge_topic *bt; + char filter[MQTTD_MAXTEXT + MQTTD_MAXPREFIX]; + size_t plen; + + TAILQ_FOREACH(bt, &br->topics, entry) { + if ((bt->conf.dir & BRIDGE_OUT) == 0) + continue; + if (prefix_join(filter, sizeof(filter), bt->conf.local_prefix, + bt->conf.pattern, strlen(bt->conf.pattern)) == -1) + continue; + if (!broker_filter_match(filter, topic, len)) + continue; + plen = strlen(bt->conf.local_prefix); + if (len < plen || memcmp(topic, bt->conf.local_prefix, plen)) + continue; + return (prefix_join(out, outlen, bt->conf.remote_prefix, + topic + plen, len - plen)); + } + return (-1); +} + +static void +bridge_publish_in(struct bconn *bc, uint8_t flags, const uint8_t *body, + size_t len) +{ + struct bridge *br = bc->bridge; + struct session *s = br->session; + struct mqtt_publish p; + struct message *m; + char local[MQTTD_MAXTEXT + MQTTD_MAXPREFIX]; + uint8_t ack = 0; + int rc, n, r; + + rc = mqtt_parse_publish(body, len, flags, bc->version, &p); + if (rc != MQTT_RC_SUCCESS) { + bridge_drop(br, "malformed PUBLISH"); + return; + } + if (p.qos == 1) + ack = MQTT_PUBACK; + else if (p.qos == 2) + ack = MQTT_PUBREC; + + if (!mqtt_topic_valid(p.topic.data, p.topic.len) || + p.topic.data[0] == '$' || + (n = bridge_map_in(br, (const char *)p.topic.data, p.topic.len, + local, sizeof(local))) == -1 || local[0] == '$') { + /* nothing we asked for; acknowledge and forget it */ + if (ack != 0) + broker_send_ack(bc, ack, p.packet_id, MQTT_RC_SUCCESS); + return; + } + + if (p.qos == 2) { + r = session_incoming_add(s, p.packet_id); + if (r == 1) { + broker_send_ack(bc, MQTT_PUBREC, p.packet_id, + MQTT_RC_SUCCESS); + return; + } + } + + m = message_new((const uint8_t *)local, n, p.payload, p.payload_len, + p.qos, p.retain); + if (m == NULL || message_set_props(m, &p.props) == -1) { + if (m != NULL) + message_unref(m); + return; + } + m->origin = s->serial; + br->msgs_in++; + s->msgs_in++; + broker_env->stats.pub_received++; + broker_route(m); + message_unref(m); + + if (ack != 0) + broker_send_ack(bc, ack, p.packet_id, MQTT_RC_SUCCESS); +} + +void +bridge_ctl_show(struct imsg *imsg, struct imsgev *iev) +{ + struct ctl_bridge_info bi; + struct bridge *br; + + TAILQ_FOREACH(br, &broker_env->bridges, entry) { + memset(&bi, 0, sizeof(bi)); + strlcpy(bi.name, br->conf.name, sizeof(bi.name)); + snprintf(bi.remote, sizeof(bi.remote), "%s:%u", br->conf.host, + ntohs(br->conf.port)); + bi.state = br->state; + bi.tls = br->conf.tls; + bi.version = br->conf.version; + bi.since = br->since; + bi.attempts = br->attempts; + bi.msgs_in = br->msgs_in; + bi.msgs_out = br->msgs_out; + bi.nqueued = br->session->nqueued; + if (imsg_compose_event(iev, IMSG_CTL_SHOW_BRIDGES_DATA, + imsg->hdr.peerid, 0, -1, &bi, sizeof(bi)) == -1) + return; + } + imsg_compose_event(iev, IMSG_CTL_SHOW_BRIDGES_END, imsg->hdr.peerid, + 0, -1, NULL, 0); +} blob - /dev/null blob + 615e5c9904a4de5cf302e777413092e27393f9203ffbce9538d42cbddff7ac99 (mode 644) --- /dev/null +++ mqttd/broker.c @@ -0,0 +1,2736 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * The broker process is the protocol authority. The sockets processes + * hand it complete control packets and it answers with the packets to + * write back; it never touches a network socket itself. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" +#include "mqtt.h" +#include "broker.h" + +struct broker *broker_env; +struct imsgev *broker_iev_storage; + +static struct imsgev *broker_iev_parent; +static struct imsgev *broker_iev_control; +static struct imsgev *broker_iev_socks; + +/* CFG_DONE is acknowledged only after storage has replayed its snapshot */ +static int cfg_ack_pending; +static int broker_started; + +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 event ev_sweep; +static struct event ev_sys; + +/* set while any sockets pipe is backed up and reading from all is off */ +static int broker_paused; + +RB_GENERATE(bconntree, bconn, entry, bconn_cmp); +RB_GENERATE(authtree, bconn, auth_entry, bconn_auth_cmp); +RB_GENERATE(failtree, failcount, entry, failcount_cmp); +RB_GENERATE(usertree, user_entry, entry, user_entry_cmp); + +static struct imsgev *broker_iev_auth; +static struct imsgev *broker_iev_snow; +static uint32_t auth_next_id; + +/* sockets processes by index, or the snowflake process */ +static struct imsgev * +broker_proc_iev(uint32_t proc) +{ + if (proc == MQTTD_PROC_SNOW) + return (broker_iev_snow); + if (proc >= env->conf.prefork) + return (NULL); + return (&broker_iev_socks[proc]); +} + +struct route_ctx { + struct message *m; + uint32_t gen; + struct dlvqueue list; + int count; +}; + +struct retained_ctx { + struct session *s; + struct subscription *sub; +}; + +struct retained_show_ctx { + uint32_t peerid; + const char *filter; +}; + +static void broker_dispatch_parent(int, short, void *); +static void broker_dispatch_control(int, short, void *); +static void broker_dispatch_storage(int, short, void *); +static void broker_dispatch_auth(int, short, void *); +static void broker_auth_lost(void); +static void broker_auth_reply(struct imsg *); +static void broker_auth_packet(struct bconn *, const uint8_t *, size_t); +static void broker_auth_failed(struct bconn *, const char *); +static void broker_connect_finish(struct bconn *, struct mqtt_connect *, + const char *, const struct auth_reply *); +static void broker_ctl_disconnect_all(struct imsg *); +static void broker_snow_lost(void); +static struct imsgev + *broker_proc_iev(uint32_t); +static void broker_dispatch_sockets(int, short, void *); +static void broker_sighdlr(int, short, void *); +static void broker_sweep(int, short, void *); +static __dead void + broker_shutdown(void); + +static void broker_socks_event_add(struct imsgev *); +static void broker_flow_update(void); +static void broker_sys_start(void); +static void broker_sys_publish(int, short, void *); +static void broker_sys_put(const char *, const char *); + +static struct bconn + *bconn_find(uint32_t, uint32_t); +static void bconn_free(struct bconn *); +static void broker_send_keepalive(struct bconn *, uint16_t); + +static void broker_conn_open(uint32_t, struct imsg *); +static void broker_conn_packet(uint32_t, struct imsg *); +static void broker_conn_close(uint32_t, struct imsg *); +static void broker_conn_error(uint32_t, struct imsg *); + +static void broker_packet(struct bconn *, const uint8_t *, size_t); +static void broker_connect(struct bconn *, const uint8_t *, size_t); +static void broker_connack_reject(struct bconn *, uint8_t); +static void broker_publish(struct bconn *, uint8_t, const uint8_t *, + size_t); +static void broker_ack(struct bconn *, uint8_t, const uint8_t *, size_t); +static void broker_pubrel(struct bconn *, const uint8_t *, size_t); +static void broker_subscribe(struct bconn *, const uint8_t *, size_t); +static void broker_unsubscribe(struct bconn *, const uint8_t *, size_t); +static void broker_disconnect(struct bconn *, const uint8_t *, size_t); + +static void route_cb(struct subscription *, void *); +static void retained_cb(struct message *, void *); + +static struct user_entry + *user_find(const char *); +static int acl_expand(const char *, const char *, const char *, char *, + size_t); +static int acl_check(const char *, const char *, const char *, size_t, + uint8_t); + +static void broker_ctl_broker(struct imsg *); +static void broker_ctl_clients(struct imsg *); +static void broker_ctl_subs(struct imsg *); +static void broker_ctl_retained(struct imsg *); +static void broker_ctl_retained_cb(struct message *, void *); +static void broker_ctl_kill(struct imsg *); + +int +bconn_cmp(struct bconn *a, struct bconn *b) +{ + if (a->proc != b->proc) + return (a->proc < b->proc ? -1 : 1); + if (a->id != b->id) + return (a->id < b->id ? -1 : 1); + return (0); +} + +int +user_entry_cmp(struct user_entry *a, struct user_entry *b) +{ + return (strcmp(a->conf.name, b->conf.name)); +} + +__dead void +broker(int fd_parent, int fd_control, int fd_storage, int fd_auth, + int fd_snow, int *fd_socks) +{ + struct timeval tv; + int n; + + log_procinit("broker"); + proc_privdrop("broker"); + + if ((broker_env = calloc(1, sizeof(*broker_env))) == NULL) + fatal("%s: calloc", __func__); + RB_INIT(&broker_env->sessions); + RB_INIT(&broker_env->conns); + RB_INIT(&broker_env->users); + RB_INIT(&broker_env->authq); + RB_INIT(&broker_env->fails); + RB_INIT(&broker_env->usercounts); + TAILQ_INIT(&broker_env->acls); + TAILQ_INIT(&broker_env->bridges); + topic_init(&broker_env->topics); + broker_env->started = time(NULL); + + event_init(); + + signal_set(&ev_sigint, SIGINT, broker_sighdlr, NULL); + signal_set(&ev_sigterm, SIGTERM, broker_sighdlr, NULL); + signal_set(&ev_sighup, SIGHUP, broker_sighdlr, NULL); + signal_set(&ev_sigpipe, SIGPIPE, broker_sighdlr, NULL); + signal_set(&ev_sigusr1, SIGUSR1, broker_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 ((broker_iev_parent = calloc(1, sizeof(*broker_iev_parent))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(broker_iev_parent, fd_parent, broker_dispatch_parent); + + if ((broker_iev_control = calloc(1, sizeof(*broker_iev_control))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(broker_iev_control, fd_control, + broker_dispatch_control); + + if ((broker_iev_storage = calloc(1, sizeof(*broker_iev_storage))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(broker_iev_storage, fd_storage, + broker_dispatch_storage); + + if ((broker_iev_auth = calloc(1, sizeof(*broker_iev_auth))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(broker_iev_auth, fd_auth, broker_dispatch_auth); + + if ((broker_iev_snow = calloc(1, sizeof(*broker_iev_snow))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(broker_iev_snow, fd_snow, broker_dispatch_sockets); + + if ((broker_iev_socks = calloc(env->conf.prefork, + sizeof(*broker_iev_socks))) == NULL) + fatal("%s: calloc", __func__); + for (n = 0; n < env->conf.prefork; n++) + imsg_event_init(&broker_iev_socks[n], fd_socks[n], + broker_dispatch_sockets); + + evtimer_set(&ev_sweep, broker_sweep, NULL); + tv.tv_sec = MQTTD_RETAIN_SWEEP; + tv.tv_usec = 0; + evtimer_add(&ev_sweep, &tv); + + if (pledge("stdio", NULL) == -1) + fatal("pledge"); + + event_dispatch(); + + broker_shutdown(); +} + +static void +broker_sighdlr(int sig, short event, void *arg) +{ + switch (sig) { + case SIGINT: + case SIGTERM: + broker_shutdown(); + break; + case SIGHUP: + case SIGPIPE: + case SIGUSR1: + break; + default: + fatalx("%s: unexpected signal", __func__); + } +} + +static void +broker_sweep(int fd, short event, void *arg) +{ + struct failcount *fc, *tmp; + struct timeval tv; + time_t now = time(NULL); + + topic_retained_expire(&broker_env->topics, now); + + RB_FOREACH_SAFE(fc, failtree, &broker_env->fails, tmp) { + if (now - fc->first > env->conf.ban_within) { + RB_REMOVE(failtree, &broker_env->fails, fc); + free(fc); + } + } + + tv.tv_sec = MQTTD_RETAIN_SWEEP; + tv.tv_usec = 0; + evtimer_add(&ev_sweep, &tv); +} + +static __dead void +broker_shutdown(void) +{ + int n; + + /* the last snapshot must reach storage before the pipe closes */ + db_snapshot_sync(); + + imsg_event_shutdown(broker_iev_parent); + free(broker_iev_parent); + imsg_event_shutdown(broker_iev_control); + free(broker_iev_control); + if (broker_iev_storage != NULL) { + imsg_event_shutdown(broker_iev_storage); + free(broker_iev_storage); + } + if (broker_iev_auth != NULL) { + imsg_event_shutdown(broker_iev_auth); + free(broker_iev_auth); + } + if (broker_iev_snow != NULL) { + imsg_event_shutdown(broker_iev_snow); + free(broker_iev_snow); + } + for (n = 0; n < env->conf.prefork; n++) + imsg_event_shutdown(&broker_iev_socks[n]); + free(broker_iev_socks); + + log_info("broker exiting, pid %d", getpid()); + exit(0); +} + +static void +broker_dispatch_parent(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + struct user_entry *ue; + struct acl *a; + int n, verbose; + + 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__); + broker_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost parent process", __func__); + broker_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_GLOBAL: + config_recv_global(env, &imsg); + /* a reload may have switched the timers on */ + if (broker_started) { + broker_sys_start(); + db_start(); + } + break; + case IMSG_CFG_AUTH_RESET: + while ((ue = RB_MIN(usertree, &broker_env->users)) + != NULL) { + RB_REMOVE(usertree, &broker_env->users, ue); + explicit_bzero(ue, sizeof(*ue)); + free(ue); + } + while ((a = TAILQ_FIRST(&broker_env->acls)) != NULL) { + TAILQ_REMOVE(&broker_env->acls, a, entry); + free(a); + } + break; + case IMSG_CFG_USER: + if ((ue = calloc(1, sizeof(*ue))) == NULL) + fatal("%s: calloc", __func__); + IMSG_SIZE_CHECK(&imsg, &ue->conf); + memcpy(&ue->conf, imsg.data, sizeof(ue->conf)); + ue->conf.name[sizeof(ue->conf.name) - 1] = '\0'; + ue->conf.hash[sizeof(ue->conf.hash) - 1] = '\0'; + if (RB_INSERT(usertree, &broker_env->users, ue) != NULL) { + log_warnx("%s: duplicate user %s", __func__, + ue->conf.name); + free(ue); + } + break; + case IMSG_CFG_ACL: + if ((a = calloc(1, sizeof(*a))) == NULL) + fatal("%s: calloc", __func__); + IMSG_SIZE_CHECK(&imsg, &a->conf); + memcpy(&a->conf, imsg.data, sizeof(a->conf)); + a->conf.user[sizeof(a->conf.user) - 1] = '\0'; + a->conf.pattern[sizeof(a->conf.pattern) - 1] = '\0'; + TAILQ_INSERT_TAIL(&broker_env->acls, a, entry); + break; + case IMSG_CFG_DONE: + if (env->conf.persist) + cfg_ack_pending = 1; + else + imsg_compose_event(broker_iev_parent, + IMSG_CFG_DONE, 0, 0, -1, NULL, 0); + break; + case IMSG_CTL_START: + log_debug("%s: broker ready", __func__); + broker_started = 1; + broker_sys_start(); + db_start(); + bridge_start(); + break; + case IMSG_CFG_BRIDGE: + bridge_config_recv(&imsg); + break; + case IMSG_CFG_BRIDGE_TOPIC: + bridge_topic_recv(&imsg); + break; + case IMSG_CTL_SHOW_BRIDGES_REQUEST: + bridge_ctl_show(&imsg, broker_iev_control); + break; + case IMSG_CTL_SAVE: + db_snapshot(); + imsg_compose_event(broker_iev_control, IMSG_CTL_OK, + imsg.hdr.peerid, 0, -1, NULL, 0); + break; + case IMSG_CTL_VERBOSE: + IMSG_SIZE_CHECK(&imsg, &verbose); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + break; + case IMSG_CTL_SHOW_BROKER_REQUEST: + broker_ctl_broker(&imsg); + break; + case IMSG_CTL_SHOW_CLIENTS_REQUEST: + broker_ctl_clients(&imsg); + break; + case IMSG_CTL_SHOW_SUBS_REQUEST: + broker_ctl_subs(&imsg); + break; + case IMSG_CTL_SHOW_RETAINED_REQUEST: + broker_ctl_retained(&imsg); + break; + case IMSG_CTL_KILL_CLIENT: + broker_ctl_kill(&imsg); + break; + case IMSG_CTL_DISCONNECT_ALL: + broker_ctl_disconnect_all(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from parent", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +broker_dispatch_control(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + 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__); + broker_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost control process", __func__); + broker_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + /* control only listens to us; its 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); +} + +static void +broker_dispatch_storage(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + /* keep serving clients, just without snapshots */ + log_warnx("%s: lost storage process, persistence off", + __func__); + env->conf.persist = 0; + imsg_event_shutdown(iev); + free(iev); + broker_iev_storage = NULL; + if (cfg_ack_pending) { + cfg_ack_pending = 0; + imsg_compose_event(broker_iev_parent, + IMSG_CFG_DONE, 0, 0, -1, NULL, 0); + } + return; + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost storage process", __func__); + env->conf.persist = 0; + imsg_event_shutdown(iev); + free(iev); + broker_iev_storage = NULL; + return; + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_DB_RECORD: + db_restore_record(&imsg); + break; + case IMSG_DB_RESTORE_END: + db_restore_end(); + if (cfg_ack_pending) { + cfg_ack_pending = 0; + imsg_compose_event(broker_iev_parent, + IMSG_CFG_DONE, 0, 0, -1, NULL, 0); + } + break; + default: + log_warnx("%s: unexpected imsg %d from storage", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +/* without the auth process every parked CONNECT is refused */ +static void +broker_auth_lost(void) +{ + struct bconn *bc, *tmp; + + log_warnx("lost auth process, external authentication off"); + if (broker_iev_auth != NULL) { + imsg_event_shutdown(broker_iev_auth); + free(broker_iev_auth); + broker_iev_auth = NULL; + } + RB_FOREACH_SAFE(bc, authtree, &broker_env->authq, tmp) + broker_connack_reject(bc, MQTT_RC_SERVER_UNAVAILABLE); +} + +static void +broker_dispatch_auth(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + broker_auth_lost(); + return; + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + broker_auth_lost(); + return; + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_AUTH_REPLY: + broker_auth_reply(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from auth", __func__, + imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +/* the snowflake process speaks the sockets protocol; losing it is survivable */ +static void +broker_snow_lost(void) +{ + struct bconn *bc, *tmp; + + log_warnx("lost snowflake process, sinks are down"); + imsg_event_shutdown(broker_iev_snow); + free(broker_iev_snow); + broker_iev_snow = NULL; + RB_FOREACH_SAFE(bc, bconntree, &broker_env->conns, tmp) + if (bc->proc == MQTTD_PROC_SNOW) + broker_close(bc, RC_NONE, DETACH_WILL); +} + +static void +broker_dispatch_sockets(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + uint32_t proc; + int n; + + proc = iev == broker_iev_snow ? MQTTD_PROC_SNOW : + (uint32_t)(iev - broker_iev_socks); + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + if (proc == MQTTD_PROC_SNOW) { + broker_snow_lost(); + return; + } + log_warnx("%s: lost sockets process %u", __func__, + proc); + broker_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + if (proc == MQTTD_PROC_SNOW) { + broker_snow_lost(); + return; + } + log_warnx("%s: lost sockets process %u", + __func__, proc); + broker_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CONN_OPEN: + broker_conn_open(proc, &imsg); + break; + case IMSG_CONN_PACKET: + broker_conn_packet(proc, &imsg); + break; + case IMSG_CONN_CLOSE: + broker_conn_close(proc, &imsg); + break; + case IMSG_CONN_ERROR: + broker_conn_error(proc, &imsg); + break; + case IMSG_BRIDGE_FAILED: + bridge_failed(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from sockets", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + broker_flow_update(); + broker_socks_event_add(iev); +} + +/* like imsg_event_add, but reading stays off while the broker is paused */ +static void +broker_socks_event_add(struct imsgev *iev) +{ + event_del(&iev->ev); + iev->events = broker_paused ? 0 : EV_READ; + if (imsgbuf_queuelen(&iev->ibuf) > 0) + iev->events |= EV_WRITE; + if (iev->events == 0) + return; + event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); + event_add(&iev->ev, NULL); +} + +/* + * Backpressure. When any sockets pipe holds more than MQTTD_QUEUE_HIGH + * unsent messages the broker stops reading from every sockets process, + * so publishers stall instead of the broker growing without bound. + * Reading resumes once every pipe is below MQTTD_QUEUE_LOW. + */ +static void +broker_flow_update(void) +{ + size_t q; + int n, over = 0, above_low = 0; + + for (n = 0; n < env->conf.prefork; n++) { + q = imsgbuf_queuelen(&broker_iev_socks[n].ibuf); + if (q > MQTTD_QUEUE_HIGH) + over = 1; + if (q > MQTTD_QUEUE_LOW) + above_low = 1; + } + if (broker_iev_snow != NULL) { + q = imsgbuf_queuelen(&broker_iev_snow->ibuf); + if (q > MQTTD_QUEUE_HIGH) + over = 1; + if (q > MQTTD_QUEUE_LOW) + above_low = 1; + } + + if (!broker_paused && over) { + broker_paused = 1; + log_debug("%s: sockets pipe congested, pausing input", __func__); + } else if (broker_paused && !above_low) { + broker_paused = 0; + log_debug("%s: resuming input", __func__); + } else + return; + + for (n = 0; n < env->conf.prefork; n++) + broker_socks_event_add(&broker_iev_socks[n]); + if (broker_iev_snow != NULL) + broker_socks_event_add(broker_iev_snow); +} + +void +broker_socks_send(uint32_t proc, uint32_t type, const void *data, size_t len) +{ + struct imsgev *iev; + + if ((iev = broker_proc_iev(proc)) == NULL) + return; + if (imsg_compose(&iev->ibuf, type, 0, 0, -1, data, len) == -1) { + log_warn("%s: imsg_compose", __func__); + return; + } + broker_socks_event_add(iev); + broker_flow_update(); +} + +static void +broker_sys_start(void) +{ + if (env->conf.sys_interval == 0 || evtimer_pending(&ev_sys, NULL)) + return; + evtimer_set(&ev_sys, broker_sys_publish, NULL); + broker_sys_publish(0, 0, NULL); +} + +static void +broker_sys_put(const char *topic, const char *value) +{ + struct message *m; + + m = message_new((const uint8_t *)topic, strlen(topic), + (const uint8_t *)value, strlen(value), 0, 1); + if (m == NULL) + return; + broker_route(m); + message_unref(m); +} + +static void +broker_sys_publish(int fd, short event, void *arg) +{ + struct timeval tv; + char buf[32]; + +#define SYS_U64(t, v) do { \ + snprintf(buf, sizeof(buf), "%llu", (unsigned long long)(v)); \ + broker_sys_put(t, buf); \ +} while (0) + + broker_sys_put("$SYS/broker/version", "mqttd " MQTTD_VERSION); + SYS_U64("$SYS/broker/uptime", time(NULL) - broker_env->started); + SYS_U64("$SYS/broker/clients/connected", broker_env->nconnected); + SYS_U64("$SYS/broker/clients/total", broker_env->nsessions); + SYS_U64("$SYS/broker/subscriptions/count", + broker_env->topics.nsubs); + SYS_U64("$SYS/broker/retained messages/count", + broker_env->topics.nretained); + SYS_U64("$SYS/broker/messages/received", + broker_env->stats.pub_received); + SYS_U64("$SYS/broker/messages/sent", broker_env->stats.pub_sent); + SYS_U64("$SYS/broker/messages/dropped", + broker_env->stats.pub_dropped); + SYS_U64("$SYS/broker/bytes/received", broker_env->stats.bytes_in); + SYS_U64("$SYS/broker/bytes/sent", broker_env->stats.bytes_out); +#undef SYS_U64 + + /* a reload may have switched statistics off */ + if (env->conf.sys_interval == 0) + return; + tv.tv_sec = env->conf.sys_interval; + tv.tv_usec = 0; + evtimer_add(&ev_sys, &tv); +} + +static struct bconn * +bconn_find(uint32_t proc, uint32_t id) +{ + struct bconn key; + + key.proc = proc; + key.id = id; + return (RB_FIND(bconntree, &broker_env->conns, &key)); +} + +static void +bconn_free(struct bconn *bc) +{ + RB_REMOVE(bconntree, &broker_env->conns, bc); + if (bc->auth_id != 0) + RB_REMOVE(authtree, &broker_env->authq, bc); + if (bc->pending != NULL) + freezero(bc->pending, bc->pending_len); + if (bc->bridge != NULL) + bridge_conn_closed(bc); + free(bc->inbuf); + free(bc); +} + +int +bconn_auth_cmp(struct bconn *a, struct bconn *b) +{ + if (a->auth_id != b->auth_id) + return (a->auth_id < b->auth_id ? -1 : 1); + return (0); +} + +int +failcount_cmp(struct failcount *a, struct failcount *b) +{ + return (strcmp(a->addr, b->addr)); +} + +void +broker_send(struct bconn *bc, const struct mqtt_buf *b) +{ + struct conn_pkt hdr; + struct iovec iov[2]; + struct imsgev *iev; + size_t off = 0, n; + + if ((iev = broker_proc_iev(bc->proc)) == NULL) + return; + + hdr.id = bc->id; + hdr.total = b->len; + + while (off < b->len) { + n = b->len - off; + if (n > CONN_PKT_CHUNK) + n = CONN_PKT_CHUNK; + hdr.offset = off; + hdr.len = n; + iov[0].iov_base = &hdr; + iov[0].iov_len = sizeof(hdr); + iov[1].iov_base = b->data + off; + iov[1].iov_len = n; + if (imsg_composev(&iev->ibuf, IMSG_CONN_PACKET, 0, 0, -1, iov, + 2) == -1) { + log_warn("%s: imsg_composev", __func__); + return; + } + off += n; + } + broker_socks_event_add(iev); + broker_flow_update(); + broker_env->stats.bytes_out += b->len; +} + +void +broker_send_ack(struct bconn *bc, uint8_t type, uint16_t pid, uint8_t rc) +{ + struct mqtt_buf out; + + mqtt_buf_init(&out); + if (mqtt_encode_ack(&out, type, bc->version, pid, rc) == 0) + broker_send(bc, &out); + mqtt_buf_free(&out); +} + +void +broker_send_pubrel(struct session *s, uint16_t pid, uint8_t rc) +{ + if (s->conn != NULL) + broker_send_ack(s->conn, MQTT_PUBREL, pid, rc); +} + +static void +broker_send_keepalive(struct bconn *bc, uint16_t keepalive) +{ + struct conn_keepalive ka; + + ka.id = bc->id; + ka.keepalive = keepalive; + broker_socks_send(bc->proc, IMSG_CONN_KEEPALIVE, &ka, sizeof(ka)); +} + +/* + * Send a DISCONNECT when the protocol allows it, tell the sockets + * process to flush and close, and release the connection. The session, + * if any, decides whether its will fires. + */ +void +broker_close(struct bconn *bc, int rc, enum detach_how how) +{ + struct mqtt_buf out; + struct conn_close cc; + + if (rc != RC_NONE && bc->connected && bc->version == MQTT_V5) { + mqtt_buf_init(&out); + if (mqtt_encode_disconnect(&out, MQTT_V5, rc, NULL) == 0 && + out.len > 0) + broker_send(bc, &out); + mqtt_buf_free(&out); + } + + cc.id = bc->id; + cc.reason = CONN_CLOSE_ADMIN; + broker_socks_send(bc->proc, IMSG_CONN_CLOSE, &cc, sizeof(cc)); + + if (bc->session != NULL) { + log_debug("%s: closing %s (%s) rc 0x%02x", __func__, + bc->session->client_id, bc->peer, rc < 0 ? 0 : rc); + session_detach(bc->session, how); + } + bconn_free(bc); +} + +int +broker_send_publish(struct session *s, struct delivery *d, int dup) +{ + struct bconn *bc = s->conn; + struct message *m = d->msg; + struct mqtt_buf props, out; + const char *topic = m != NULL ? m->topic : NULL; + char mapped[MQTTD_MAXTEXT + MQTTD_MAXPREFIX]; + size_t topic_len = m != NULL ? m->topic_len : 0; + time_t now; + uint32_t remaining; + int i, n, ret = -1; + + if (bc == NULL || m == NULL) + return (-1); + + now = time(NULL); + if (!dup && message_expired(m, now)) + return (-1); + + /* a bridge session publishes under the remote's topic names */ + if (s->bridge != NULL) { + if ((n = bridge_map_out(s->bridge, m->topic, m->topic_len, + mapped, sizeof(mapped))) == -1) + return (-1); + topic = mapped; + topic_len = n; + } + + mqtt_buf_init(&props); + mqtt_buf_init(&out); + + if (s->version == MQTT_V5) { + if (m->props_len > 0 && + mqtt_buf_bytes(&props, m->props, m->props_len) == -1) + goto done; + if (m->expires != 0) { + remaining = m->expires > now ? + (uint32_t)(m->expires - now) : 1; + if (mqtt_prop_u32(&props, MQTT_PROP_MESSAGE_EXPIRY, + remaining) == -1) + goto done; + } + for (i = 0; i < d->nsubids; i++) { + if (mqtt_prop_varint(&props, MQTT_PROP_SUBSCRIPTION_ID, + d->subids[i]) == -1) + goto done; + } + } + + if (mqtt_encode_publish(&out, s->version, dup, d->qos, d->retain, + topic, topic_len, d->pid, &props, m->payload, + m->payload_len) == -1) + goto done; + + if (s->max_packet != 0 && out.len > s->max_packet) { + log_debug("%s: %s: %zu byte publish exceeds client maximum", + __func__, s->client_id, out.len); + goto done; + } + + broker_send(bc, &out); + s->msgs_out++; + if (s->bridge != NULL) + s->bridge->msgs_out++; + broker_env->stats.pub_sent++; + ret = 0; +done: + mqtt_buf_free(&props); + mqtt_buf_free(&out); + return (ret); +} + +static void +broker_conn_open(uint32_t proc, struct imsg *imsg) +{ + struct conn_open co; + struct bconn *bc; + + IMSG_SIZE_CHECK(imsg, &co); + memcpy(&co, imsg->data, sizeof(co)); + co.peer[sizeof(co.peer) - 1] = '\0'; + co.cert_user[sizeof(co.cert_user) - 1] = '\0'; + + if (bconn_find(proc, co.id) != NULL) { + log_warnx("%s: duplicate connection %u/%u", __func__, proc, + co.id); + return; + } + + if ((bc = calloc(1, sizeof(*bc))) == NULL) { + log_warn("%s: calloc", __func__); + return; + } + bc->proc = proc; + bc->id = co.id; + bc->listener_id = co.listener_id; + bc->tls = co.tls; + bc->opened = time(NULL); + memcpy(bc->peer, co.peer, sizeof(bc->peer)); + memcpy(bc->cert_user, co.cert_user, sizeof(bc->cert_user)); + RB_INSERT(bconntree, &broker_env->conns, bc); + + log_debug("%s: connection %u/%u %s %s", __func__, proc, co.id, + co.bridge_id != 0 ? "to" : "from", bc->peer); + + if (co.bridge_id != 0) + bridge_conn_open(bc, co.bridge_id); +} + +static void +broker_conn_packet(uint32_t proc, struct imsg *imsg) +{ + struct conn_pkt hdr; + struct bconn *bc; + uint8_t *p = imsg->data, *buf; + size_t len = IMSG_DATA_SIZE(imsg), total; + + if (len < sizeof(hdr)) { + log_warnx("%s: short packet imsg", __func__); + return; + } + memcpy(&hdr, p, sizeof(hdr)); + p += sizeof(hdr); + len -= sizeof(hdr); + + if (hdr.len != len) { + log_warnx("%s: chunk length mismatch", __func__); + return; + } + + if ((bc = bconn_find(proc, hdr.id)) == NULL) + return; + + if (hdr.total > env->conf.max_packet_size || + hdr.offset > hdr.total || hdr.len > hdr.total - hdr.offset) { + broker_close(bc, MQTT_RC_MALFORMED, DETACH_WILL); + return; + } + + if (hdr.offset == 0) { + free(bc->inbuf); + bc->inbuf = NULL; + if (hdr.total == hdr.len) { + broker_packet(bc, p, len); + return; + } + if ((bc->inbuf = malloc(hdr.total)) == NULL) { + broker_close(bc, MQTT_RC_SERVER_BUSY, DETACH_WILL); + return; + } + bc->intotal = hdr.total; + bc->inlen = 0; + } + + if (bc->inbuf == NULL || hdr.total != bc->intotal || + hdr.offset != bc->inlen) { + broker_close(bc, MQTT_RC_MALFORMED, DETACH_WILL); + return; + } + + memcpy(bc->inbuf + hdr.offset, p, len); + bc->inlen += len; + + if (bc->inlen == bc->intotal) { + /* broker_packet may close and free bc */ + buf = bc->inbuf; + total = bc->intotal; + bc->inbuf = NULL; + broker_packet(bc, buf, total); + free(buf); + } +} + +static void +broker_conn_close(uint32_t proc, struct imsg *imsg) +{ + struct conn_close cc; + struct bconn *bc; + + IMSG_SIZE_CHECK(imsg, &cc); + memcpy(&cc, imsg->data, sizeof(cc)); + + if ((bc = bconn_find(proc, cc.id)) == NULL) + return; + + if (bc->session != NULL) { + log_info("event=disconnect client=\"%s\" peer=%s reason=%s", + bc->session->client_id, bc->peer, + cc.reason == CONN_CLOSE_TIMEOUT ? "keepalive-timeout" : + cc.reason == CONN_CLOSE_OVERFLOW ? "slow-consumer" : + cc.reason == CONN_CLOSE_ERROR ? "socket-error" : + "connection-closed"); + session_detach(bc->session, DETACH_WILL); + } + bconn_free(bc); +} + +static void +broker_conn_error(uint32_t proc, struct imsg *imsg) +{ + struct conn_error ce; + struct bconn *bc; + + IMSG_SIZE_CHECK(imsg, &ce); + memcpy(&ce, imsg->data, sizeof(ce)); + + if ((bc = bconn_find(proc, ce.id)) == NULL) + return; + + broker_close(bc, ce.reason == CONN_ERR_TOOBIG ? + MQTT_RC_PACKET_TOO_LARGE : MQTT_RC_MALFORMED, DETACH_WILL); +} + +static void +broker_packet(struct bconn *bc, const uint8_t *data, size_t len) +{ + struct mqtt_fixed f; + struct mqtt_buf out; + const uint8_t *body; + size_t blen; + + broker_env->stats.bytes_in += len; + + if (mqtt_parse_fixed(data, len, &f) != 0 || + (size_t)f.hdrlen + f.remaining != len) { + broker_close(bc, MQTT_RC_MALFORMED, DETACH_WILL); + return; + } + body = data + f.hdrlen; + blen = f.remaining; + + switch (f.type) { + case MQTT_PUBLISH: + break; + case MQTT_PUBREL: + case MQTT_SUBSCRIBE: + case MQTT_UNSUBSCRIBE: + if (f.flags != 0x02) { + broker_close(bc, MQTT_RC_MALFORMED, DETACH_WILL); + return; + } + break; + default: + if (f.flags != 0) { + broker_close(bc, MQTT_RC_MALFORMED, DETACH_WILL); + return; + } + break; + } + + /* an enhanced authentication in progress may continue with AUTH */ + if (f.type == MQTT_AUTH) { + if (bc->bridge != NULL || + (!bc->connected && bc->pending == NULL)) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + broker_auth_packet(bc, body, blen); + return; + } + + if (!bc->connected && f.type != MQTT_CONNECT) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + + /* on a bridge we are the client; the handshakes below are shared */ + if (bc->bridge != NULL && + bridge_packet(bc, f.type, f.flags, body, blen)) + return; + + switch (f.type) { + case MQTT_CONNECT: + broker_connect(bc, body, blen); + break; + case MQTT_PUBLISH: + broker_publish(bc, f.flags, body, blen); + break; + case MQTT_PUBACK: + case MQTT_PUBREC: + case MQTT_PUBCOMP: + broker_ack(bc, f.type, body, blen); + break; + case MQTT_PUBREL: + broker_pubrel(bc, body, blen); + break; + case MQTT_SUBSCRIBE: + broker_subscribe(bc, body, blen); + break; + case MQTT_UNSUBSCRIBE: + broker_unsubscribe(bc, body, blen); + break; + case MQTT_PINGREQ: + if (blen != 0) { + broker_close(bc, MQTT_RC_MALFORMED, DETACH_WILL); + return; + } + mqtt_buf_init(&out); + if (mqtt_encode_pingresp(&out) == 0) + broker_send(bc, &out); + mqtt_buf_free(&out); + break; + case MQTT_DISCONNECT: + broker_disconnect(bc, body, blen); + break; + default: + /* server-bound only: CONNACK, SUBACK, UNSUBACK, PINGRESP, AUTH */ + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + break; + } +} + +static void +broker_connack_reject(struct bconn *bc, uint8_t rc) +{ + struct mqtt_buf out; + + if (bc->version == 0) + bc->version = MQTT_V311; + + log_debug("%s: rejecting %s with 0x%02x", __func__, bc->peer, rc); + + mqtt_buf_init(&out); + if (mqtt_encode_connack(&out, bc->version, 0, rc, NULL) == 0) + broker_send(bc, &out); + mqtt_buf_free(&out); + + broker_close(bc, RC_NONE, DETACH_WILL); +} + +static void +broker_connect(struct bconn *bc, const uint8_t *body, size_t len) +{ + struct mqtt_connect c; + struct user_entry *ue; + struct auth_request req; + char uname[MQTTD_MAXNAME]; + char pw[MQTTD_MAXPASSWORD]; + const char *user = NULL; + int rc; + + if (bc->connected || bc->pending != NULL) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + + rc = mqtt_parse_connect(body, len, &c); + if (rc != MQTT_RC_SUCCESS) { + bc->version = c.version >= MQTT_V5 ? MQTT_V5 : MQTT_V311; + broker_connack_reject(bc, rc); + return; + } + bc->version = c.version; + + if (MQTT_PROP_ISSET(&c.props, MQTT_PROP_AUTH_METHOD)) { + /* enhanced authentication: only the external helper does it */ + if (!env->conf.auth_exec_set || broker_iev_auth == NULL || + c.props.auth_method.len == 0 || + c.props.auth_method.len >= sizeof(bc->auth_method) || + c.props.auth_data.len > MQTTD_AUTH_DATA_MAX || + (c.has_username && + c.username.len >= sizeof(req.user))) { + broker_connack_reject(bc, MQTT_RC_BAD_AUTH_METHOD); + return; + } + memcpy(bc->auth_method, c.props.auth_method.data, + c.props.auth_method.len); + bc->auth_method[c.props.auth_method.len] = '\0'; + + memset(&req, 0, sizeof(req)); + req.kind = AUTH_REQ_START; + do { + req.id = ++auth_next_id; + } while (req.id == 0); + if (c.has_username) { + memcpy(req.user, c.username.data, c.username.len); + req.user[c.username.len] = '\0'; + } + memcpy(req.client_id, c.client_id.data, c.client_id.len); + strlcpy(req.peer, bc->peer, sizeof(req.peer)); + strlcpy(req.method, bc->auth_method, sizeof(req.method)); + req.datalen = c.props.auth_data.len; + memcpy(req.data, c.props.auth_data.data, req.datalen); + + if ((bc->pending = malloc(len)) == NULL) { + broker_connack_reject(bc, MQTT_RC_SERVER_UNAVAILABLE); + return; + } + memcpy(bc->pending, body, len); + bc->pending_len = len; + bc->auth_id = req.id; + RB_INSERT(authtree, &broker_env->authq, bc); + imsg_compose_event(broker_iev_auth, IMSG_AUTH_REQUEST, 0, 0, + -1, &req, sizeof(req)); + explicit_bzero(&req, sizeof(req)); + return; + } + + if (c.will) { + if (c.will_qos > env->conf.max_qos) { + broker_connack_reject(bc, MQTT_RC_QOS_NOT_SUPPORTED); + return; + } + if (c.will_retain && !env->conf.retain_available) { + broker_connack_reject(bc, MQTT_RC_RETAIN_NOT_SUPPORTED); + return; + } + if (!mqtt_topic_valid(c.will_topic.data, c.will_topic.len)) { + broker_connack_reject(bc, MQTT_RC_TOPIC_NAME_INVALID); + return; + } + if (MQTT_PROP_ISSET(&c.will_props, MQTT_PROP_PAYLOAD_FORMAT) && + c.will_props.payload_format == 1 && + !mqtt_utf8_valid(c.will_payload.data, + c.will_payload.len)) { + broker_connack_reject(bc, + MQTT_RC_PAYLOAD_FORMAT_INVALID); + return; + } + } + + if (c.client_id.len == 0) { + if (c.version < MQTT_V5 && !c.clean_start) { + broker_connack_reject(bc, MQTT_RC_CLIENT_ID_INVALID); + return; + } + } else if (c.client_id.len > MQTTD_MAX_CLIENTID || + !mqtt_utf8_valid(c.client_id.data, c.client_id.len)) { + broker_connack_reject(bc, MQTT_RC_CLIENT_ID_INVALID); + return; + } + + if (bc->cert_user[0] != '\0') { + /* + * A verified client certificate is the identity. A username + * in CONNECT may repeat it but may not claim someone else. + */ + if (c.has_username && + (c.username.len != strlen(bc->cert_user) || + memcmp(c.username.data, bc->cert_user, + c.username.len) != 0)) { + log_info("event=auth-fail peer=%s cert=\"%s\" " + "user=\"%.*s\" reason=certificate-mismatch", + bc->peer, bc->cert_user, (int)c.username.len, + c.username.data); + broker_connack_reject(bc, MQTT_RC_BAD_CREDENTIALS); + return; + } + user = bc->cert_user; + } else if (c.has_username) { + if (c.username.len == 0 || + c.username.len >= sizeof(uname) || + !mqtt_utf8_valid(c.username.data, c.username.len)) { + broker_connack_reject(bc, MQTT_RC_BAD_CREDENTIALS); + return; + } + memcpy(uname, c.username.data, c.username.len); + uname[c.username.len] = '\0'; + + if (!c.has_password || c.password.len >= sizeof(pw) || + memchr(c.password.data, '\0', c.password.len) != NULL) { + broker_auth_failed(bc, uname); + broker_connack_reject(bc, MQTT_RC_BAD_CREDENTIALS); + return; + } + + if ((ue = user_find(uname)) != NULL) { + memcpy(pw, c.password.data, c.password.len); + pw[c.password.len] = '\0'; + rc = crypt_checkpass(pw, ue->conf.hash); + explicit_bzero(pw, sizeof(pw)); + if (rc != 0) { + broker_auth_failed(bc, uname); + broker_connack_reject(bc, + MQTT_RC_BAD_CREDENTIALS); + return; + } + } else if (env->conf.auth_exec_set && broker_iev_auth != NULL) { + /* unknown locally: park the CONNECT and ask outside */ + memset(&req, 0, sizeof(req)); + req.kind = AUTH_REQ_PASSWORD; + do { + req.id = ++auth_next_id; + } while (req.id == 0); + strlcpy(req.user, uname, sizeof(req.user)); + memcpy(req.password, c.password.data, c.password.len); + memcpy(req.client_id, c.client_id.data, + c.client_id.len); + strlcpy(req.peer, bc->peer, sizeof(req.peer)); + + if ((bc->pending = malloc(len)) == NULL) { + explicit_bzero(&req, sizeof(req)); + broker_connack_reject(bc, + MQTT_RC_SERVER_UNAVAILABLE); + return; + } + memcpy(bc->pending, body, len); + bc->pending_len = len; + bc->auth_id = req.id; + RB_INSERT(authtree, &broker_env->authq, bc); + + imsg_compose_event(broker_iev_auth, IMSG_AUTH_REQUEST, + 0, 0, -1, &req, sizeof(req)); + explicit_bzero(&req, sizeof(req)); + return; + } else { + broker_auth_failed(bc, uname); + broker_connack_reject(bc, MQTT_RC_BAD_CREDENTIALS); + return; + } + user = uname; + } else if (!env->conf.anonymous) { + broker_connack_reject(bc, MQTT_RC_NOT_AUTHORIZED); + return; + } + + broker_connect_finish(bc, &c, user, NULL); +} + +/* + * The client's AUTH packet during an enhanced exchange (before CONNACK) + * or a re-authentication (after). Either way the data goes to the + * helper under the connection's current request id. + */ +static void +broker_auth_packet(struct bconn *bc, const uint8_t *body, size_t len) +{ + struct mqtt_auth a; + struct auth_request req; + int rc; + + if (bc->version != MQTT_V5 || bc->auth_method[0] == '\0' || + broker_iev_auth == NULL) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + rc = mqtt_parse_auth(body, len, &a); + if (rc != MQTT_RC_SUCCESS || + a.props.auth_method.len != strlen(bc->auth_method) || + memcmp(a.props.auth_method.data, bc->auth_method, + a.props.auth_method.len) != 0 || + a.props.auth_data.len > MQTTD_AUTH_DATA_MAX) { + broker_close(bc, rc != MQTT_RC_SUCCESS ? rc : + MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + + memset(&req, 0, sizeof(req)); + if (!bc->connected) { + /* answer to our continue */ + if (a.reason != 0x18 || bc->auth_id == 0) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + req.kind = AUTH_REQ_DATA; + req.id = bc->auth_id; + } else { + /* client initiated re-authentication */ + if (a.reason != 0x19 || bc->auth_id != 0) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + req.kind = AUTH_REQ_START; + do { + req.id = ++auth_next_id; + } while (req.id == 0); + bc->auth_id = req.id; + RB_INSERT(authtree, &broker_env->authq, bc); + if (bc->session != NULL && bc->session->username != NULL) + strlcpy(req.user, bc->session->username, + sizeof(req.user)); + if (bc->session != NULL) + strlcpy(req.client_id, bc->session->client_id, + sizeof(req.client_id)); + } + strlcpy(req.peer, bc->peer, sizeof(req.peer)); + strlcpy(req.method, bc->auth_method, sizeof(req.method)); + req.datalen = a.props.auth_data.len; + memcpy(req.data, a.props.auth_data.data, req.datalen); + imsg_compose_event(broker_iev_auth, IMSG_AUTH_REQUEST, 0, 0, -1, &req, + sizeof(req)); + explicit_bzero(&req, sizeof(req)); +} + +/* the external authenticator answered */ +static void +broker_auth_reply(struct imsg *imsg) +{ + struct auth_reply r; + struct bconn key, *bc; + struct mqtt_connect c; + struct mqtt_buf out; + char uname[MQTTD_MAXNAME]; + const char *user; + uint8_t *body; + size_t len; + + IMSG_SIZE_CHECK(imsg, &r); + memcpy(&r, imsg->data, sizeof(r)); + r.user[sizeof(r.user) - 1] = '\0'; + if (r.datalen > sizeof(r.data)) + r.datalen = sizeof(r.data); + + key.auth_id = r.id; + if ((bc = RB_FIND(authtree, &broker_env->authq, &key)) == NULL) + return; + + /* more round trips: the request id stays live */ + if (r.cont) { + mqtt_buf_init(&out); + if (mqtt_encode_auth(&out, 0x18, bc->auth_method, r.data, + r.datalen) == 0) + broker_send(bc, &out); + mqtt_buf_free(&out); + return; + } + + RB_REMOVE(authtree, &broker_env->authq, bc); + bc->auth_id = 0; + + if (bc->connected) { + /* re-authentication outcome */ + if (!r.ok) { + log_info("event=auth-fail user=\"%s\" peer=%s " + "reason=reauth", bc->session != NULL && + bc->session->username != NULL ? + bc->session->username : "", bc->peer); + broker_close(bc, MQTT_RC_NOT_AUTHORIZED, DETACH_WILL); + return; + } + mqtt_buf_init(&out); + if (mqtt_encode_auth(&out, MQTT_RC_SUCCESS, bc->auth_method, + r.data, r.datalen) == 0) + broker_send(bc, &out); + mqtt_buf_free(&out); + return; + } + + body = bc->pending; + len = bc->pending_len; + bc->pending = NULL; + bc->pending_len = 0; + + if (body == NULL || mqtt_parse_connect(body, len, &c) != + MQTT_RC_SUCCESS || c.username.len >= sizeof(uname)) { + if (body != NULL) + freezero(body, len); + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + memcpy(uname, c.username.data, c.username.len); + uname[c.username.len] = '\0'; + + /* the helper may name the identity; otherwise CONNECT's username */ + user = r.user[0] != '\0' ? r.user : uname[0] != '\0' ? uname : NULL; + + if (!r.ok) { + broker_auth_failed(bc, user != NULL ? user : ""); + broker_connack_reject(bc, bc->auth_method[0] != '\0' ? + MQTT_RC_NOT_AUTHORIZED : MQTT_RC_BAD_CREDENTIALS); + } else if (user == NULL && !env->conf.anonymous) + broker_connack_reject(bc, MQTT_RC_NOT_AUTHORIZED); + else + broker_connect_finish(bc, &c, user, &r); + + freezero(body, len); + explicit_bzero(&r, sizeof(r)); +} + +/* count failures per source address and hand repeat offenders to pf */ +static void +broker_auth_failed(struct bconn *bc, const char *user) +{ + struct failcount key, *fc; + struct pf_ban ban; + const char *end; + time_t now = time(NULL); + + log_info("event=auth-fail user=\"%s\" peer=%s", user, bc->peer); + + if (env->conf.ban_failures == 0 || broker_iev_auth == NULL) + return; + + /* strip the port: "a.b.c.d:port" or "[v6]:port" */ + memset(&key, 0, sizeof(key)); + if (bc->peer[0] == '[') { + if ((end = strchr(bc->peer, ']')) == NULL) + return; + if ((size_t)(end - bc->peer - 1) >= sizeof(key.addr)) + return; + memcpy(key.addr, bc->peer + 1, end - bc->peer - 1); + } else { + if ((end = strrchr(bc->peer, ':')) == NULL) + end = bc->peer + strlen(bc->peer); + if ((size_t)(end - bc->peer) >= sizeof(key.addr)) + return; + memcpy(key.addr, bc->peer, end - bc->peer); + } + + if ((fc = RB_FIND(failtree, &broker_env->fails, &key)) == NULL) { + if ((fc = calloc(1, sizeof(*fc))) == NULL) + return; + strlcpy(fc->addr, key.addr, sizeof(fc->addr)); + fc->first = now; + RB_INSERT(failtree, &broker_env->fails, fc); + } else if (now - fc->first > env->conf.ban_within) { + fc->first = now; + fc->count = 0; + } + + if (++fc->count < env->conf.ban_failures) + return; + + strlcpy(ban.addr, fc->addr, sizeof(ban.addr)); + imsg_compose_event(broker_iev_auth, IMSG_PF_BAN, 0, 0, -1, &ban, + sizeof(ban)); + log_info("event=ban peer=%s failures=%u within=%u", fc->addr, + fc->count, env->conf.ban_within); + RB_REMOVE(failtree, &broker_env->fails, fc); + free(fc); +} + +static void +broker_connect_finish(struct bconn *bc, struct mqtt_connect *cp, + const char *user, const struct auth_reply *ar) +{ + struct mqtt_connect c = *cp; + struct session *s; + struct user_entry *ue; + struct message *will; + struct mqtt_buf props, out; + char cid[MQTTD_MAX_CLIENTID + 1]; + uint32_t expiry, requested_expiry, mine; + uint16_t keepalive; + int assigned = 0, session_present = 0; + + if (c.client_id.len == 0) { + snprintf(cid, sizeof(cid), "mqttd-%08x%08x", arc4random(), + arc4random()); + assigned = 1; + } else { + memcpy(cid, c.client_id.data, c.client_id.len); + cid[c.client_id.len] = '\0'; + } + + if (c.will && !acl_check(user, cid, (const char *)c.will_topic.data, + c.will_topic.len, ACL_PUBLISH)) { + broker_connack_reject(bc, MQTT_RC_NOT_AUTHORIZED); + return; + } + + /* a reconnecting client's old connection must not count against it */ + s = session_find(cid); + mine = s != NULL && s->conn != NULL ? 1 : 0; + if (env->conf.max_clients != 0 && + broker_env->nconnected - mine >= env->conf.max_clients) { + broker_connack_reject(bc, MQTT_RC_SERVER_BUSY); + return; + } + + ue = user != NULL ? user_find(user) : NULL; + if (ue != NULL && ue->conf.max_clients != 0) { + if (!(mine && s->username != NULL && + strcmp(s->username, user) == 0)) + mine = 0; + if (usercount_get(user) - mine >= ue->conf.max_clients) { + log_info("event=quota user=\"%s\" peer=%s " + "limit=%u", user, bc->peer, ue->conf.max_clients); + broker_connack_reject(bc, MQTT_RC_QUOTA_EXCEEDED); + return; + } + } + + keepalive = c.keepalive; + if (env->conf.max_keepalive != 0 && + (keepalive == 0 || keepalive > env->conf.max_keepalive)) + keepalive = env->conf.max_keepalive; + + if (c.version == MQTT_V5) { + requested_expiry = MQTT_PROP_ISSET(&c.props, + MQTT_PROP_SESSION_EXPIRY) ? c.props.session_expiry : 0; + } else + requested_expiry = c.clean_start ? 0 : UINT32_MAX; + expiry = requested_expiry; + if (env->conf.max_session_expiry != 0 && + expiry > env->conf.max_session_expiry) + expiry = env->conf.max_session_expiry; + + will = NULL; + if (c.will) { + will = message_new(c.will_topic.data, c.will_topic.len, + c.will_payload.data, c.will_payload.len, c.will_qos, + c.will_retain); + if (will == NULL || + message_set_props(will, &c.will_props) == -1) { + if (will != NULL) + message_unref(will); + broker_connack_reject(bc, MQTT_RC_SERVER_UNAVAILABLE); + return; + } + } + + if (s != NULL) { + if (s->conn != NULL) { + log_info("event=takeover client=\"%s\" old_peer=%s " + "new_peer=%s", cid, s->conn->peer, bc->peer); + broker_close(s->conn, MQTT_RC_SESSION_TAKEN_OVER, + DETACH_TAKEOVER); + } + if (c.clean_start) + session_reset(s); + else + session_present = 1; + } else if ((s = session_new(cid)) == NULL) { + if (will != NULL) + message_unref(will); + broker_connack_reject(bc, MQTT_RC_SERVER_UNAVAILABLE); + return; + } + + s->version = c.version; + s->clean_start = c.clean_start; + s->expiry_interval = expiry; + s->keepalive = keepalive; + s->recv_max = 0; + s->max_packet = 0; + s->topic_alias_max = 0; + if (c.version == MQTT_V5) { + s->recv_max = MQTT_PROP_ISSET(&c.props, + MQTT_PROP_RECEIVE_MAXIMUM) ? c.props.receive_maximum : + 65535; + if (MQTT_PROP_ISSET(&c.props, MQTT_PROP_MAXIMUM_PACKET_SIZE)) + s->max_packet = c.props.maximum_packet_size; + if (MQTT_PROP_ISSET(&c.props, MQTT_PROP_TOPIC_ALIAS_MAXIMUM)) + s->topic_alias_max = c.props.topic_alias_maximum; + } + s->rate = ue != NULL && ue->conf.rate != 0 ? ue->conf.rate : + env->conf.max_rate; + + free(s->username); + s->username = NULL; + if (user != NULL && (s->username = strdup(user)) == NULL) + fatal("%s: strdup", __func__); + + if (s->will != NULL) + message_unref(s->will); + s->will = will; + s->will_delay = 0; + if (will != NULL) { + will->origin = s->serial; + if (MQTT_PROP_ISSET(&c.will_props, MQTT_PROP_WILL_DELAY)) + s->will_delay = c.will_props.will_delay; + } + + session_attach(s, bc); + bc->connected = 1; + + mqtt_buf_init(&props); + mqtt_buf_init(&out); + if (c.version == MQTT_V5) { + if (expiry != requested_expiry) + mqtt_prop_u32(&props, MQTT_PROP_SESSION_EXPIRY, expiry); + mqtt_prop_u16(&props, MQTT_PROP_RECEIVE_MAXIMUM, + env->conf.max_inflight); + if (env->conf.max_qos < 2) + mqtt_prop_u8(&props, MQTT_PROP_MAXIMUM_QOS, + env->conf.max_qos); + if (!env->conf.retain_available) + mqtt_prop_u8(&props, MQTT_PROP_RETAIN_AVAILABLE, 0); + mqtt_prop_u32(&props, MQTT_PROP_MAXIMUM_PACKET_SIZE, + env->conf.max_packet_size); + if (assigned) + mqtt_prop_str(&props, MQTT_PROP_ASSIGNED_CLIENT_ID, cid, + strlen(cid)); + mqtt_prop_u16(&props, MQTT_PROP_TOPIC_ALIAS_MAXIMUM, + env->conf.topic_alias_max); + if (keepalive != c.keepalive) + mqtt_prop_u16(&props, MQTT_PROP_SERVER_KEEP_ALIVE, + keepalive); + if (bc->auth_method[0] != '\0') { + mqtt_prop_str(&props, MQTT_PROP_AUTH_METHOD, + bc->auth_method, strlen(bc->auth_method)); + if (ar != NULL && ar->datalen > 0) + mqtt_prop_str(&props, MQTT_PROP_AUTH_DATA, + ar->data, ar->datalen); + } + } + if (mqtt_encode_connack(&out, c.version, session_present, + MQTT_RC_SUCCESS, &props) == 0) + broker_send(bc, &out); + mqtt_buf_free(&props); + mqtt_buf_free(&out); + + broker_send_keepalive(bc, keepalive); + + log_info("event=connect client=\"%s\" user=\"%s\" peer=%s version=%d " + "tls=%d clean=%d expiry=%u keepalive=%u session_present=%d", cid, + user != NULL ? user : "", bc->peer, c.version, bc->tls, + c.clean_start, expiry, keepalive, session_present); + + if (session_present) + session_resume(s); +} + +static void +broker_publish(struct bconn *bc, uint8_t flags, const uint8_t *body, + size_t len) +{ + struct session *s = bc->session; + struct mqtt_publish p; + struct message *m; + const char *topic; + size_t topic_len; + uint16_t alias; + uint8_t ack_type; + int rc, matched, r; + + rc = mqtt_parse_publish(body, len, flags, bc->version, &p); + if (rc != MQTT_RC_SUCCESS) { + broker_close(bc, rc, DETACH_WILL); + return; + } + + if (!session_rate_ok(s)) { + log_info("event=rate-limit client=\"%s\" user=\"%s\" peer=%s " + "rate=%u", s->client_id, + s->username != NULL ? s->username : "", bc->peer, s->rate); + broker_close(bc, MQTT_RC_MESSAGE_RATE_TOO_HIGH, DETACH_WILL); + return; + } + + if (p.qos > env->conf.max_qos) { + broker_close(bc, MQTT_RC_QOS_NOT_SUPPORTED, DETACH_WILL); + return; + } + if (p.retain && !env->conf.retain_available) { + broker_close(bc, MQTT_RC_RETAIN_NOT_SUPPORTED, DETACH_WILL); + return; + } + if (MQTT_PROP_ISSET(&p.props, MQTT_PROP_SUBSCRIPTION_ID)) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + + topic = (const char *)p.topic.data; + topic_len = p.topic.len; + + if (MQTT_PROP_ISSET(&p.props, MQTT_PROP_TOPIC_ALIAS)) { + alias = p.props.topic_alias; + if (alias == 0 || alias > s->naliases) { + broker_close(bc, MQTT_RC_TOPIC_ALIAS_INVALID, + DETACH_WILL); + return; + } + if (topic_len > 0) { + if (!mqtt_topic_valid(p.topic.data, topic_len)) { + broker_close(bc, MQTT_RC_TOPIC_NAME_INVALID, + DETACH_WILL); + return; + } + free(s->aliases[alias - 1]); + s->aliases[alias - 1] = strndup(topic, topic_len); + if (s->aliases[alias - 1] == NULL) + fatal("%s: strndup", __func__); + } else { + if (s->aliases[alias - 1] == NULL) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, + DETACH_WILL); + return; + } + topic = s->aliases[alias - 1]; + topic_len = strlen(topic); + } + } else if (!mqtt_topic_valid(p.topic.data, topic_len)) { + broker_close(bc, topic_len == 0 ? MQTT_RC_PROTOCOL_ERROR : + MQTT_RC_TOPIC_NAME_INVALID, DETACH_WILL); + return; + } + + ack_type = p.qos == 1 ? MQTT_PUBACK : MQTT_PUBREC; + + if (MQTT_PROP_ISSET(&p.props, MQTT_PROP_PAYLOAD_FORMAT) && + p.props.payload_format == 1 && + !mqtt_utf8_valid(p.payload, p.payload_len)) { + if (p.qos == 0) + broker_close(bc, MQTT_RC_PAYLOAD_FORMAT_INVALID, + DETACH_WILL); + else + broker_send_ack(bc, ack_type, p.packet_id, + MQTT_RC_PAYLOAD_FORMAT_INVALID); + return; + } + + /* topics starting with '$' belong to the broker */ + if (topic[0] == '$' || !acl_check(s->username, s->client_id, topic, + topic_len, ACL_PUBLISH)) { + log_debug("event=acl-deny action=publish client=\"%s\" " + "user=\"%s\" topic=\"%.*s\"", s->client_id, + s->username != NULL ? s->username : "", (int)topic_len, + topic); + if (p.qos > 0) + broker_send_ack(bc, ack_type, p.packet_id, + bc->version == MQTT_V5 ? MQTT_RC_NOT_AUTHORIZED : + MQTT_RC_SUCCESS); + return; + } + + if (p.qos == 2) { + /* a retransmission must be answered even at the window limit */ + if (s->nincoming >= env->conf.max_inflight && + session_incoming_find(s, p.packet_id) == 0) { + broker_close(bc, MQTT_RC_RECEIVE_MAX_EXCEEDED, + DETACH_WILL); + return; + } + r = session_incoming_add(s, p.packet_id); + if (r == -1) { + broker_close(bc, MQTT_RC_SERVER_BUSY, DETACH_WILL); + return; + } + if (r == 1) { + /* retransmission of a message already routed */ + broker_send_ack(bc, MQTT_PUBREC, p.packet_id, + MQTT_RC_SUCCESS); + return; + } + } + + m = message_new((const uint8_t *)topic, topic_len, p.payload, + p.payload_len, p.qos, p.retain); + if (m == NULL || message_set_props(m, &p.props) == -1) { + if (m != NULL) + message_unref(m); + broker_close(bc, MQTT_RC_SERVER_BUSY, DETACH_WILL); + return; + } + m->origin = s->serial; + + s->msgs_in++; + broker_env->stats.pub_received++; + + matched = broker_route(m); + message_unref(m); + + if (p.qos > 0) + broker_send_ack(bc, ack_type, p.packet_id, + matched > 0 || bc->version != MQTT_V5 ? MQTT_RC_SUCCESS : + MQTT_RC_NO_MATCHING_SUBS); +} + +static void +broker_ack(struct bconn *bc, uint8_t type, const uint8_t *body, size_t len) +{ + struct session *s = bc->session; + struct mqtt_ack a; + struct delivery *d; + int rc; + + rc = mqtt_parse_ack(body, len, bc->version, type, &a); + if (rc != MQTT_RC_SUCCESS) { + broker_close(bc, rc, DETACH_WILL); + return; + } + + d = session_inflight_find(s, a.packet_id); + + switch (type) { + case MQTT_PUBACK: + if (d != NULL && d->state == DLV_PUBLISH && d->qos == 1) { + session_inflight_remove(s, d); + delivery_free(d); + } + break; + case MQTT_PUBREC: + if (d == NULL || d->state != DLV_PUBLISH || d->qos != 2) { + if (bc->version == MQTT_V5) + broker_send_ack(bc, MQTT_PUBREL, a.packet_id, + MQTT_RC_PACKET_ID_NOT_FOUND); + return; + } + if (a.reason >= 0x80) { + session_inflight_remove(s, d); + delivery_free(d); + break; + } + /* the message is delivered, only the handshake remains */ + d->state = DLV_PUBREL; + message_unref(d->msg); + d->msg = NULL; + broker_send_ack(bc, MQTT_PUBREL, a.packet_id, MQTT_RC_SUCCESS); + return; + case MQTT_PUBCOMP: + if (d != NULL && d->state == DLV_PUBREL) { + session_inflight_remove(s, d); + delivery_free(d); + } + break; + } + + session_drain(s); +} + +static void +broker_pubrel(struct bconn *bc, const uint8_t *body, size_t len) +{ + struct session *s = bc->session; + struct mqtt_ack a; + int rc; + + rc = mqtt_parse_ack(body, len, bc->version, MQTT_PUBREL, &a); + if (rc != MQTT_RC_SUCCESS) { + broker_close(bc, rc, DETACH_WILL); + return; + } + + if (session_incoming_remove(s, a.packet_id) == 0) + broker_send_ack(bc, MQTT_PUBCOMP, a.packet_id, MQTT_RC_SUCCESS); + else + broker_send_ack(bc, MQTT_PUBCOMP, a.packet_id, + bc->version == MQTT_V5 ? MQTT_RC_PACKET_ID_NOT_FOUND : + MQTT_RC_SUCCESS); +} + +static void +broker_subscribe(struct bconn *bc, const uint8_t *body, size_t len) +{ + struct session *s = bc->session; + struct mqtt_subscribe sp; + struct mqtt_subopt o; + struct subscription *sub; + struct mqtt_buf codes, retained, out; + struct retained_ctx rctx; + struct mqtt_str group, filter; + char sharebuf[MQTTD_MAXNAME]; + const char *share, *fdata; + size_t i, flen; + uint32_t subid; + uint8_t code, qos, is_new, want_retained; + int rc, r, shared; + + rc = mqtt_parse_subscribe(body, len, bc->version, MQTT_SUBSCRIBE, &sp); + if (rc != MQTT_RC_SUCCESS) { + broker_close(bc, rc, DETACH_WILL); + return; + } + subid = MQTT_PROP_ISSET(&sp.props, MQTT_PROP_SUBSCRIPTION_ID) ? + sp.props.subscription_id : 0; + + mqtt_buf_init(&codes); + mqtt_buf_init(&retained); + mqtt_buf_init(&out); + + while ((r = mqtt_subscribe_next(&sp, &o)) == 1) { + want_retained = 0; + share = NULL; + code = MQTT_RC_SUCCESS; + + shared = mqtt_filter_shared_parse(o.filter.data, o.filter.len, + &group, &filter); + if (shared == -1 || (shared == 0 && + !mqtt_filter_valid(o.filter.data, o.filter.len))) { + broker_close(bc, MQTT_RC_TOPIC_FILTER_INVALID, + DETACH_WILL); + goto done; + } + if (shared == 1) { + /* no local makes no sense for a shared subscription */ + if (o.no_local) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, + DETACH_WILL); + goto done; + } + if (group.len >= sizeof(sharebuf)) + code = MQTT_RC_TOPIC_FILTER_INVALID; + else { + memcpy(sharebuf, group.data, group.len); + sharebuf[group.len] = '\0'; + share = sharebuf; + } + } else + filter = o.filter; + fdata = (const char *)filter.data; + flen = filter.len; + + if (code != MQTT_RC_SUCCESS) + ; + else if (!acl_check(s->username, s->client_id, fdata, flen, + ACL_SUBSCRIBE)) { + log_debug("event=acl-deny action=subscribe client=\"%s\" " + "user=\"%s\" filter=\"%.*s\"", s->client_id, + s->username != NULL ? s->username : "", (int)flen, + fdata); + code = MQTT_RC_NOT_AUTHORIZED; + } else { + qos = o.qos < env->conf.max_qos ? o.qos : + env->conf.max_qos; + sub = topic_find_sub(&broker_env->topics, fdata, flen, + share, s); + is_new = sub == NULL; + if (is_new) { + if ((sub = calloc(1, sizeof(*sub))) == NULL) + fatal("%s: calloc", __func__); + if (share != NULL && + (sub->share = strdup(share)) == NULL) + fatal("%s: strdup", __func__); + if (topic_subscribe(&broker_env->topics, fdata, + flen, sub) == NULL) { + free(sub->share); + free(sub); + broker_close(bc, MQTT_RC_SERVER_BUSY, + DETACH_WILL); + goto done; + } + sub->session = s; + TAILQ_INSERT_TAIL(&s->subs, sub, sess_entry); + s->nsubs++; + } + sub->qos = qos; + sub->no_local = o.no_local; + sub->rap = o.rap; + sub->rh = o.rh; + sub->id = subid; + code = qos; + /* retained messages are never sent to shared subs */ + if (share == NULL && env->conf.retain_available && + (o.rh == 0 || (o.rh == 1 && is_new))) + want_retained = 1; + } + + if (mqtt_buf_u8(&codes, code) == -1 || + mqtt_buf_u8(&retained, want_retained) == -1) + fatal("%s: mqtt_buf_u8", __func__); + } + if (r == -1) { + broker_close(bc, MQTT_RC_MALFORMED, DETACH_WILL); + goto done; + } + + if (mqtt_encode_suback(&out, bc->version, sp.packet_id, codes.data, + codes.len) == 0) + broker_send(bc, &out); + + /* second pass over the filters delivers retained messages */ + rc = mqtt_parse_subscribe(body, len, bc->version, MQTT_SUBSCRIBE, &sp); + for (i = 0; rc == MQTT_RC_SUCCESS && + mqtt_subscribe_next(&sp, &o) == 1 && i < retained.len; i++) { + if (!retained.data[i]) + continue; + /* only ordinary filters are flagged, so o.filter is the key */ + sub = topic_find_sub(&broker_env->topics, + (const char *)o.filter.data, o.filter.len, NULL, s); + if (sub == NULL) + continue; + rctx.s = s; + rctx.sub = sub; + topic_retained_match(&broker_env->topics, (const char *)o.filter.data, + o.filter.len, retained_cb, &rctx); + } + +done: + mqtt_buf_free(&codes); + mqtt_buf_free(&retained); + mqtt_buf_free(&out); +} + +static void +broker_unsubscribe(struct bconn *bc, const uint8_t *body, size_t len) +{ + struct session *s = bc->session; + struct mqtt_subscribe sp; + struct mqtt_str f, group, filter; + struct subscription *sub; + struct mqtt_buf codes, out; + char sharebuf[MQTTD_MAXNAME]; + const char *share; + int rc, r, shared; + + rc = mqtt_parse_subscribe(body, len, bc->version, MQTT_UNSUBSCRIBE, + &sp); + if (rc != MQTT_RC_SUCCESS) { + broker_close(bc, rc, DETACH_WILL); + return; + } + + mqtt_buf_init(&codes); + mqtt_buf_init(&out); + + while ((r = mqtt_unsubscribe_next(&sp, &f)) == 1) { + shared = mqtt_filter_shared_parse(f.data, f.len, &group, + &filter); + if (shared == -1 || + (shared == 0 && !mqtt_filter_valid(f.data, f.len))) { + broker_close(bc, MQTT_RC_TOPIC_FILTER_INVALID, + DETACH_WILL); + goto done; + } + share = NULL; + sub = NULL; + if (shared == 1 && group.len < sizeof(sharebuf)) { + memcpy(sharebuf, group.data, group.len); + sharebuf[group.len] = '\0'; + share = sharebuf; + } else if (shared == 0) + filter = f; + if (shared == 0 || share != NULL) + sub = topic_find_sub(&broker_env->topics, + (const char *)filter.data, filter.len, share, s); + if (sub != NULL) { + TAILQ_REMOVE(&s->subs, sub, sess_entry); + s->nsubs--; + topic_unsubscribe(&broker_env->topics, sub); + free(sub->share); + free(sub); + } + if (mqtt_buf_u8(&codes, sub != NULL ? MQTT_RC_SUCCESS : + MQTT_RC_NO_SUBSCRIPTION) == -1) + fatal("%s: mqtt_buf_u8", __func__); + } + if (r == -1) { + broker_close(bc, MQTT_RC_MALFORMED, DETACH_WILL); + goto done; + } + + if (mqtt_encode_unsuback(&out, bc->version, sp.packet_id, codes.data, + codes.len) == 0) + broker_send(bc, &out); +done: + mqtt_buf_free(&codes); + mqtt_buf_free(&out); +} + +static void +broker_disconnect(struct bconn *bc, const uint8_t *body, size_t len) +{ + struct session *s = bc->session; + struct mqtt_disconnect d; + uint32_t expiry; + int rc; + + rc = mqtt_parse_disconnect(body, len, bc->version, &d); + if (rc != MQTT_RC_SUCCESS) { + broker_close(bc, rc, DETACH_WILL); + return; + } + + if (MQTT_PROP_ISSET(&d.props, MQTT_PROP_SESSION_EXPIRY)) { + expiry = d.props.session_expiry; + if (s->expiry_interval == 0 && expiry != 0) { + broker_close(bc, MQTT_RC_PROTOCOL_ERROR, DETACH_WILL); + return; + } + if (env->conf.max_session_expiry != 0 && + expiry > env->conf.max_session_expiry) + expiry = env->conf.max_session_expiry; + s->expiry_interval = expiry; + } + + log_info("event=disconnect client=\"%s\" peer=%s reason=%s", + s->client_id, bc->peer, d.reason == MQTT_RC_DISCONNECT_WILL ? + "client-with-will" : "client"); + broker_close(bc, RC_NONE, d.reason == MQTT_RC_DISCONNECT_WILL ? + DETACH_WILL : DETACH_NORMAL); +} + +static void +route_cb(struct subscription *sub, void *arg) +{ + struct route_ctx *ctx = arg; + struct session *s = sub->session; + struct message *m = ctx->m; + struct delivery *d; + uint8_t qos; + + if (sub->no_local && m->origin == s->serial) + return; + + qos = m->qos < sub->qos ? m->qos : sub->qos; + + if (s->route_gen == ctx->gen && s->route_dlv != NULL) { + d = s->route_dlv; + if (qos > d->qos) + d->qos = qos; + if (sub->rap && m->retain) + d->retain = 1; + delivery_add_subid(d, sub->id); + return; + } + + if ((d = delivery_new(m, qos, sub->rap ? m->retain : 0)) == NULL) { + log_warn("%s: delivery_new", __func__); + return; + } + d->session = s; + delivery_add_subid(d, sub->id); + s->route_gen = ctx->gen; + s->route_dlv = d; + TAILQ_INSERT_TAIL(&ctx->list, d, entry); + ctx->count++; +} + +int +broker_route(struct message *m) +{ + struct route_ctx ctx; + struct delivery *d; + + if (m->retain && topic_retain(&broker_env->topics, m) == -1) + log_warn("%s: topic_retain", __func__); + + if (env->conf.sparkplug) + sparkplug_publish_hook(m); + + ctx.m = m; + ctx.gen = ++broker_env->route_gen; + ctx.count = 0; + TAILQ_INIT(&ctx.list); + + topic_match(&broker_env->topics, m->topic, m->topic_len, route_cb, &ctx); + + while ((d = TAILQ_FIRST(&ctx.list)) != NULL) { + TAILQ_REMOVE(&ctx.list, d, entry); + d->session->route_dlv = NULL; + session_deliver(d->session, d); + } + + return (ctx.count); +} + +static void +retained_cb(struct message *m, void *arg) +{ + struct retained_ctx *ctx = arg; + struct delivery *d; + uint8_t qos; + + if (message_expired(m, time(NULL))) + return; + + qos = m->qos < ctx->sub->qos ? m->qos : ctx->sub->qos; + if ((d = delivery_new(m, qos, 1)) == NULL) + return; + delivery_add_subid(d, ctx->sub->id); + session_deliver(ctx->s, d); +} + +static struct user_entry * +user_find(const char *name) +{ + struct user_entry key; + + if (strlcpy(key.conf.name, name, sizeof(key.conf.name)) >= + sizeof(key.conf.name)) + return (NULL); + return (RB_FIND(usertree, &broker_env->users, &key)); +} + +/* %u and %c in an ACL pattern stand for the username and client id */ +static int +acl_expand(const char *pat, const char *user, const char *cid, char *out, + size_t outlen) +{ + const char *rep; + size_t o = 0, rl; + + for (; *pat != '\0'; pat++) { + if (pat[0] == '%' && (pat[1] == 'u' || pat[1] == 'c')) { + rep = pat[1] == 'u' ? user : cid; + if (rep == NULL) + return (-1); + rl = strlen(rep); + if (o + rl >= outlen) + return (-1); + memcpy(out + o, rep, rl); + o += rl; + pat++; + continue; + } + if (o + 1 >= outlen) + return (-1); + out[o++] = *pat; + } + out[o] = '\0'; + return (0); +} + +/* MQTT filter match with '+' and '#', also used by ACLs and bridges */ +int +broker_filter_match(const char *pat, const char *s, size_t slen) +{ + const char *pe, *se; + size_t plen = strlen(pat), pi = 0, si = 0, pl, sl; + + for (;;) { + pe = memchr(pat + pi, '/', plen - pi); + pl = pe != NULL ? (size_t)(pe - (pat + pi)) : plen - pi; + if (pl == 1 && pat[pi] == '#') + return (1); + + se = memchr(s + si, '/', slen - si); + sl = se != NULL ? (size_t)(se - (s + si)) : slen - si; + + if (!(pl == 1 && pat[pi] == '+') && + (pl != sl || memcmp(pat + pi, s + si, pl) != 0)) + return (0); + + if (pe == NULL && se == NULL) + return (1); + if (se == NULL) + return (pe != NULL && strcmp(pe + 1, "#") == 0); + if (pe == NULL) + return (0); + + pi += pl + 1; + si += sl + 1; + } +} + +static int +acl_check(const char *user, const char *cid, const char *topic, size_t len, + uint8_t action) +{ + struct acl *a; + char pat[MQTTD_MAXTEXT * 2]; + + if (!env->conf.acl_present) + return (1); + + TAILQ_FOREACH(a, &broker_env->acls, entry) { + if ((a->conf.action & action) == 0) + continue; + if (a->conf.user[0] != '\0' && + (user == NULL || strcmp(a->conf.user, user) != 0)) + continue; + if (a->conf.client[0] != '\0' && strcmp(a->conf.client, cid) != 0) + continue; + if (acl_expand(a->conf.pattern, user, cid, pat, + sizeof(pat)) == -1) + continue; + if (broker_filter_match(pat, topic, len)) + return (a->conf.allow); + } + return (0); +} + +static void +broker_ctl_broker(struct imsg *imsg) +{ + struct ctl_broker_info bi; + + memset(&bi, 0, sizeof(bi)); + bi.verbose = log_getverbose(); + bi.started = broker_env->started; + bi.sessions = broker_env->nsessions; + bi.connected = broker_env->nconnected; + bi.subscriptions = broker_env->topics.nsubs; + bi.retained = broker_env->topics.nretained; + bi.pub_received = broker_env->stats.pub_received; + bi.pub_sent = broker_env->stats.pub_sent; + bi.pub_dropped = broker_env->stats.pub_dropped; + bi.bytes_in = broker_env->stats.bytes_in; + bi.bytes_out = broker_env->stats.bytes_out; + bi.snapshots = broker_env->stats.snapshots; + bi.last_snapshot = broker_env->stats.last_snapshot; + bi.persist = env->conf.persist; + + imsg_compose_event(broker_iev_control, IMSG_CTL_SHOW_BROKER_DATA, + imsg->hdr.peerid, 0, -1, &bi, sizeof(bi)); + imsg_compose_event(broker_iev_control, IMSG_CTL_SHOW_BROKER_END, + imsg->hdr.peerid, 0, -1, NULL, 0); +} + +static void +ctl_filter_get(struct imsg *imsg, struct ctl_filter *f) +{ + memset(f, 0, sizeof(*f)); + if (IMSG_DATA_SIZE(imsg) >= sizeof(*f)) + memcpy(f, imsg->data, sizeof(*f)); + f->name[sizeof(f->name) - 1] = '\0'; +} + +static void +broker_ctl_clients(struct imsg *imsg) +{ + struct ctl_filter f; + struct ctl_client_info ci; + struct session *s; + + ctl_filter_get(imsg, &f); + + RB_FOREACH(s, sesstree, &broker_env->sessions) { + if (f.name[0] != '\0' && strcmp(f.name, s->client_id) != 0) + continue; + + memset(&ci, 0, sizeof(ci)); + strlcpy(ci.client_id, s->client_id, sizeof(ci.client_id)); + if (s->username != NULL) + strlcpy(ci.username, s->username, sizeof(ci.username)); + if (s->conn != NULL) { + strlcpy(ci.peer, s->conn->peer, sizeof(ci.peer)); + ci.connected = 1; + ci.tls = s->conn->tls; + } + ci.version = s->version; + ci.clean_start = s->clean_start; + ci.keepalive = s->keepalive; + ci.expiry = s->expiry_interval; + ci.nsubs = s->nsubs; + ci.ninflight = s->ninflight; + ci.nqueued = s->nqueued; + ci.connected_at = s->connected_at; + ci.disconnected_at = s->disconnected_at; + ci.msgs_in = s->msgs_in; + ci.msgs_out = s->msgs_out; + ci.dropped = s->dropped; + + if (imsg_compose_event(broker_iev_control, + IMSG_CTL_SHOW_CLIENTS_DATA, imsg->hdr.peerid, 0, -1, &ci, + sizeof(ci)) == -1) + return; + } + + imsg_compose_event(broker_iev_control, IMSG_CTL_SHOW_CLIENTS_END, + imsg->hdr.peerid, 0, -1, NULL, 0); +} + +static void +broker_ctl_subs(struct imsg *imsg) +{ + struct ctl_filter f; + struct ctl_sub_info si; + struct session *s; + struct subscription *sub; + int off; + + ctl_filter_get(imsg, &f); + + RB_FOREACH(s, sesstree, &broker_env->sessions) { + if (f.name[0] != '\0' && strcmp(f.name, s->client_id) != 0) + continue; + TAILQ_FOREACH(sub, &s->subs, sess_entry) { + memset(&si, 0, sizeof(si)); + strlcpy(si.client_id, s->client_id, + sizeof(si.client_id)); + off = 0; + if (sub->share != NULL) + off = snprintf(si.filter, sizeof(si.filter), + "$share/%s/", sub->share); + if (off < 0 || (size_t)off >= sizeof(si.filter) || + topic_node_name(sub->node, si.filter + off, + sizeof(si.filter) - off) == -1) + strlcpy(si.filter, "(too long)", + sizeof(si.filter)); + si.id = sub->id; + si.qos = sub->qos; + si.no_local = sub->no_local; + si.rap = sub->rap; + si.rh = sub->rh; + if (imsg_compose_event(broker_iev_control, + IMSG_CTL_SHOW_SUBS_DATA, imsg->hdr.peerid, 0, -1, + &si, sizeof(si)) == -1) + return; + } + } + + imsg_compose_event(broker_iev_control, IMSG_CTL_SHOW_SUBS_END, + imsg->hdr.peerid, 0, -1, NULL, 0); +} + +static void +broker_ctl_retained_cb(struct message *m, void *arg) +{ + struct retained_show_ctx *ctx = arg; + struct ctl_retained_info ri; + + if (ctx->filter[0] != '\0' && strcmp(ctx->filter, m->topic) != 0) + return; + + memset(&ri, 0, sizeof(ri)); + strlcpy(ri.topic, m->topic, sizeof(ri.topic)); + ri.payload_len = m->payload_len; + ri.expiry = m->expiry_interval; + ri.qos = m->qos; + ri.received = m->received; + + imsg_compose_event(broker_iev_control, IMSG_CTL_SHOW_RETAINED_DATA, + ctx->peerid, 0, -1, &ri, sizeof(ri)); +} + +static void +broker_ctl_retained(struct imsg *imsg) +{ + struct ctl_filter f; + struct retained_show_ctx ctx; + + ctl_filter_get(imsg, &f); + ctx.peerid = imsg->hdr.peerid; + ctx.filter = f.name; + + topic_retained_foreach(&broker_env->topics, broker_ctl_retained_cb, &ctx); + + imsg_compose_event(broker_iev_control, IMSG_CTL_SHOW_RETAINED_END, + imsg->hdr.peerid, 0, -1, NULL, 0); +} + +static void +broker_ctl_kill(struct imsg *imsg) +{ + struct ctl_filter f; + struct session *s; + int ret; + + ctl_filter_get(imsg, &f); + + s = session_find(f.name); + if (s == NULL || s->conn == NULL) { + ret = s == NULL ? ENOENT : ENOTCONN; + imsg_compose_event(broker_iev_control, IMSG_CTL_FAIL, + imsg->hdr.peerid, 0, -1, &ret, sizeof(ret)); + return; + } + + log_info("event=kill client=\"%s\" peer=%s", s->client_id, + s->conn->peer); + broker_close(s->conn, MQTT_RC_ADMINISTRATIVE, DETACH_WILL); + imsg_compose_event(broker_iev_control, IMSG_CTL_OK, imsg->hdr.peerid, + 0, -1, NULL, 0); +} + +/* every client, not the bridges; wills fire as for any lost connection */ +static void +broker_ctl_disconnect_all(struct imsg *imsg) +{ + struct bconn *bc, *tmp; + uint64_t n = 0; + + RB_FOREACH_SAFE(bc, bconntree, &broker_env->conns, tmp) { + if (bc->bridge != NULL) + continue; + broker_close(bc, MQTT_RC_SERVER_SHUTDOWN, DETACH_WILL); + n++; + } + log_info("event=disconnect-all count=%llu", (unsigned long long)n); + imsg_compose_event(broker_iev_control, IMSG_CTL_OK, imsg->hdr.peerid, + 0, -1, NULL, 0); +} blob - /dev/null blob + ac35b57826a94aa77801850866dacd5d7c34d140d28c625a15c07fdecff6d0b9 (mode 644) --- /dev/null +++ mqttd/broker.h @@ -0,0 +1,365 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * State owned by the broker process: sessions, subscriptions, retained + * messages and the connections the sockets processes report to it. + */ + +#ifndef BROKER_H +#define BROKER_H + +struct session; +struct topic_node; +struct mqtt_props; +struct mqtt_buf; + +struct message { + int refcnt; + uint8_t qos; + uint8_t retain; + uint8_t has_payload_format; + uint8_t payload_format; + uint16_t topic_len; + uint32_t payload_len; + uint32_t expiry_interval; + time_t expires; + time_t received; + uint64_t origin; + char *topic; + uint8_t *payload; + /* forwardable v5 properties, already encoded */ + uint8_t *props; + size_t props_len; +}; + +struct subscription { + TAILQ_ENTRY(subscription) node_entry; + TAILQ_ENTRY(subscription) sess_entry; + struct topic_node *node; + struct session *session; + char *share; + uint32_t id; + uint8_t qos; + uint8_t no_local; + uint8_t rap; + uint8_t rh; +}; +TAILQ_HEAD(sublist, subscription); + +#define MQTTD_MAX_SUBIDS 8 + +enum delivery_state { + DLV_QUEUED, + DLV_PUBLISH, + DLV_PUBREL +}; + +struct delivery { + TAILQ_ENTRY(delivery) entry; + RB_ENTRY(delivery) tree; + struct session *session; + struct message *msg; + uint16_t pid; + uint8_t qos; + uint8_t retain; + uint8_t state; + uint8_t nsubids; + uint32_t subids[MQTTD_MAX_SUBIDS]; +}; +TAILQ_HEAD(dlvqueue, delivery); +RB_HEAD(dlvtree, delivery); + +struct incoming { + RB_ENTRY(incoming) tree; + uint16_t pid; +}; +RB_HEAD(inctree, incoming); + +struct bconn { + RB_ENTRY(bconn) entry; + uint32_t proc; + uint32_t id; + uint32_t listener_id; + uint8_t tls; + uint8_t version; + uint8_t connected; + char peer[MQTTD_MAXPEER]; + char cert_user[MQTTD_MAXNAME]; + struct session *session; + uint8_t *inbuf; + uint32_t inlen; + uint32_t intotal; + time_t opened; + time_t last_rx; + struct bridge *bridge; + /* CONNECT parked while the external authenticator decides */ + RB_ENTRY(bconn) auth_entry; + uint32_t auth_id; + uint8_t *pending; + size_t pending_len; + /* enhanced authentication method in use, empty for none */ + char auth_method[MQTTD_AUTH_METHOD_MAX]; +}; +RB_HEAD(bconntree, bconn); +RB_HEAD(authtree, bconn); + +/* authentication failures per source address, for pf banning */ +struct failcount { + RB_ENTRY(failcount) entry; + char addr[MQTTD_MAXPEER]; + uint32_t count; + time_t first; +}; +RB_HEAD(failtree, failcount); + +enum bridge_state { + BRIDGE_DOWN, + BRIDGE_CONNECTING, + BRIDGE_WAIT_CONNACK, + BRIDGE_UP +}; + +/* + * A bridge is a local session whose connection we open ourselves. Its + * out-topics are ordinary subscriptions on that session, so the queue, + * QoS handshakes and persistence all come for free. + */ +struct bridge { + TAILQ_ENTRY(bridge) entry; + struct bridge_config conf; + struct bridge_topics topics; + struct session *session; + struct bconn *conn; + struct event reconnect_ev; + struct event ping_ev; + uint32_t backoff; + uint32_t attempts; + uint16_t sub_pid; + uint8_t state; + time_t since; + uint64_t msgs_in; + uint64_t msgs_out; +}; +TAILQ_HEAD(bridgelist, bridge); + +/* connected sessions per username, for per-user limits */ +struct usercount { + RB_ENTRY(usercount) entry; + char name[MQTTD_MAXNAME]; + uint32_t count; +}; +RB_HEAD(usercounttree, usercount); + +struct session { + RB_ENTRY(session) entry; + uint64_t serial; + char *client_id; + char *username; + struct bconn *conn; + struct bridge *bridge; + uint8_t version; + uint8_t clean_start; + uint32_t expiry_interval; + uint32_t will_delay; + struct message *will; + struct event expiry_ev; + struct event will_ev; + uint16_t keepalive; + uint16_t recv_max; + uint16_t topic_alias_max; + uint32_t max_packet; + uint32_t rate; + double rate_tokens; + struct timeval rate_last; + char **aliases; + uint16_t naliases; + struct sublist subs; + uint32_t nsubs; + struct dlvtree inflight; + uint32_t ninflight; + struct dlvqueue queue; + uint32_t nqueued; + uint32_t max_queued; + struct inctree incoming; + uint32_t nincoming; + uint16_t next_pid; + uint32_t route_gen; + struct delivery *route_dlv; + time_t created; + time_t connected_at; + time_t disconnected_at; + uint64_t msgs_in; + uint64_t msgs_out; + uint64_t dropped; +}; +RB_HEAD(sesstree, session); + +struct user_entry { + RB_ENTRY(user_entry) entry; + struct user_config conf; +}; +RB_HEAD(usertree, user_entry); + +struct topic_tree { + struct topic_node *root; + uint64_t nsubs; + uint64_t nretained; +}; + +struct broker_stats { + uint64_t pub_received; + uint64_t pub_sent; + uint64_t pub_dropped; + uint64_t bytes_in; + uint64_t bytes_out; + uint64_t snapshots; + time_t last_snapshot; +}; + +struct broker { + struct sesstree sessions; + struct bconntree conns; + struct topic_tree topics; + struct usertree users; + struct acllist acls; + struct authtree authq; + struct failtree fails; + struct usercounttree usercounts; + struct bridgelist bridges; + struct broker_stats stats; + uint64_t nsessions; + uint64_t nconnected; + uint64_t next_serial; + uint32_t route_gen; + time_t started; +}; + +extern struct broker *broker_env; +extern struct imsgev *broker_iev_storage; + +enum detach_how { + DETACH_NORMAL, + DETACH_WILL, + DETACH_TAKEOVER +}; + +RB_PROTOTYPE(sesstree, session, entry, session_cmp); +RB_PROTOTYPE(bconntree, bconn, entry, bconn_cmp); +RB_PROTOTYPE(authtree, bconn, auth_entry, bconn_auth_cmp); +RB_PROTOTYPE(failtree, failcount, entry, failcount_cmp); +RB_PROTOTYPE(usercounttree, usercount, entry, usercount_cmp); +RB_PROTOTYPE(dlvtree, delivery, tree, delivery_cmp); +RB_PROTOTYPE(inctree, incoming, tree, incoming_cmp); +RB_PROTOTYPE(usertree, user_entry, entry, user_entry_cmp); + +/* session.c */ +int session_cmp(struct session *, struct session *); +int delivery_cmp(struct delivery *, struct delivery *); +int incoming_cmp(struct incoming *, struct incoming *); +struct message *message_new(const uint8_t *, uint16_t, const uint8_t *, + uint32_t, uint8_t, uint8_t); +int message_set_props(struct message *, const struct mqtt_props *); +void message_ref(struct message *); +void message_unref(struct message *); +int message_expired(const struct message *, time_t); +struct delivery *delivery_new(struct message *, uint8_t, uint8_t); +void delivery_add_subid(struct delivery *, uint32_t); +void delivery_free(struct delivery *); +struct session *session_find(const char *); +struct session *session_new(const char *); +void session_reset(struct session *); +void session_destroy(struct session *); +void session_attach(struct session *, struct bconn *); +int session_detach(struct session *, enum detach_how); +void session_deliver(struct session *, struct delivery *); +void session_drain(struct session *); +void session_resume(struct session *); +struct delivery *session_inflight_find(struct session *, uint16_t); +void session_inflight_remove(struct session *, struct delivery *); +int session_incoming_add(struct session *, uint16_t); +int session_incoming_find(struct session *, uint16_t); +int session_incoming_remove(struct session *, uint16_t); +void session_unsubscribe_all(struct session *); +void session_clear_aliases(struct session *); +int session_rate_ok(struct session *); +uint32_t session_max_queued(struct session *); +int usercount_cmp(struct usercount *, struct usercount *); +uint32_t usercount_get(const char *); +void usercount_adjust(const char *, int); + +/* broker.c */ +int bconn_cmp(struct bconn *, struct bconn *); +int bconn_auth_cmp(struct bconn *, struct bconn *); +int failcount_cmp(struct failcount *, struct failcount *); +int user_entry_cmp(struct user_entry *, struct user_entry *); +void broker_send(struct bconn *, const struct mqtt_buf *); +int broker_send_publish(struct session *, struct delivery *, int); +void broker_send_pubrel(struct session *, uint16_t, uint8_t); +void broker_send_ack(struct bconn *, uint8_t, uint16_t, uint8_t); +void broker_close(struct bconn *, int, enum detach_how); +void broker_socks_send(uint32_t, uint32_t, const void *, size_t); +int broker_filter_match(const char *, const char *, size_t); +int broker_route(struct message *); +#define RC_NONE -1 + +/* bridge.c */ +void bridge_config_recv(struct imsg *); +void bridge_topic_recv(struct imsg *); +void bridge_start(void); +void bridge_failed(struct imsg *); +void bridge_conn_open(struct bconn *, uint32_t); +int bridge_packet(struct bconn *, uint8_t, uint8_t, const uint8_t *, + size_t); +void bridge_conn_closed(struct bconn *); +int bridge_map_out(struct bridge *, const char *, size_t, char *, + size_t); +void bridge_ctl_show(struct imsg *, struct imsgev *); + +/* sparkplug.c */ +void sparkplug_publish_hook(struct message *); +void sparkplug_will_hook(struct message *); + +/* db.c */ +void db_start(void); +void db_snapshot(void); +void db_snapshot_sync(void); +void db_restore_record(struct imsg *); +void db_restore_end(void); + +/* topic.c */ +typedef void (*topic_sub_cb)(struct subscription *, void *); +typedef void (*topic_msg_cb)(struct message *, void *); +void topic_init(struct topic_tree *); +struct topic_node + *topic_subscribe(struct topic_tree *, const char *, size_t, + struct subscription *); +void topic_unsubscribe(struct topic_tree *, struct subscription *); +struct subscription + *topic_find_sub(struct topic_tree *, const char *, size_t, + const char *, struct session *); +void topic_match(struct topic_tree *, const char *, size_t, + topic_sub_cb, void *); +int topic_retain(struct topic_tree *, struct message *); +struct message *topic_retained_get(struct topic_tree *, const char *, size_t); +void topic_retained_match(struct topic_tree *, const char *, size_t, + topic_msg_cb, void *); +void topic_retained_foreach(struct topic_tree *, topic_msg_cb, + void *); +void topic_retained_expire(struct topic_tree *, time_t); +int topic_node_name(struct topic_node *, char *, size_t); + +#endif /* BROKER_H */ blob - /dev/null blob + acbfedcc388b96d19f6feb8f9e0a07e39414e6086c6beb70cc7405a8ff1e5f83 (mode 644) --- /dev/null +++ mqttd/config.c @@ -0,0 +1,413 @@ +/* + * Copyright (c) 2020-2021, 2026 Tracey Emery + * Copyright (c) 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. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" + +static int config_send_tls_blob(struct imsgev *, uint32_t, const char *, + enum tls_config_type, const char *, uint8_t *, size_t); +static int config_recv_tls_blob(const char *, uint32_t, + struct tls_config_msg *, const char *, uint8_t *, size_t, + uint8_t **, size_t *); + +void +config_init(struct mqttd *e) +{ + TAILQ_INIT(&e->listeners); + TAILQ_INIT(&e->users); + TAILQ_INIT(&e->acls); + TAILQ_INIT(&e->bridges); + e->nlisteners = 0; +} + +int +config_send_global(struct imsgev *iev, struct mqttd *e) +{ + return (imsg_compose_event(iev, IMSG_CFG_GLOBAL, 0, 0, -1, &e->conf, + sizeof(e->conf))); +} + +int +config_recv_global(struct mqttd *e, struct imsg *imsg) +{ + IMSG_SIZE_CHECK(imsg, &e->conf); + memcpy(&e->conf, imsg->data, sizeof(e->conf)); + return (0); +} + +/* + * imsg closes the passed descriptor once sent, so every receiver gets + * its own dup. The flush is synchronous because the parent pledges + * without sendfd once configuration is finished. + */ +int +config_send_listener(struct imsgev *iev, struct listener *l, int fd) +{ + int ret; + + if (imsg_compose(&iev->ibuf, IMSG_CFG_LISTENER, 0, 0, fd, &l->conf, + sizeof(l->conf)) == -1) { + log_warn("%s: imsg_compose", __func__); + return (-1); + } + do { + ret = imsgbuf_flush(&iev->ibuf); + } while (ret == -1 && errno == EAGAIN); + if (ret == -1) { + log_warn("%s: imsgbuf_flush", __func__); + return (-1); + } + return (0); +} + +struct listener * +config_recv_listener(struct mqttd *e, struct imsg *imsg) +{ + struct listener *l; + + if ((l = calloc(1, sizeof(*l))) == NULL) + fatal("%s: calloc", __func__); + + IMSG_SIZE_CHECK(imsg, &l->conf); + memcpy(&l->conf, imsg->data, sizeof(l->conf)); + l->conf.name[sizeof(l->conf.name) - 1] = '\0'; + + /* the sender's heap pointers mean nothing here */ + l->conf.tls_cert = NULL; + l->conf.tls_cert_file = NULL; + l->conf.tls_cert_len = 0; + l->conf.tls_key = NULL; + l->conf.tls_key_file = NULL; + l->conf.tls_key_len = 0; + l->conf.tls_ca = NULL; + l->conf.tls_ca_file = NULL; + l->conf.tls_ca_len = 0; + l->conf.tls_crl = NULL; + l->conf.tls_crl_file = NULL; + l->conf.tls_crl_len = 0; + l->conf.tls_ocsp_staple = NULL; + l->conf.tls_ocsp_staple_file = NULL; + l->conf.tls_ocsp_staple_len = 0; + + l->fd = imsg_get_fd(imsg); + + TAILQ_INSERT_TAIL(&e->listeners, l, entry); + e->nlisteners++; + return (l); +} + +static int +config_send_tls_blob(struct imsgev *iev, uint32_t id, const char *name, + enum tls_config_type type, const char *label, uint8_t *data, size_t len) +{ + struct tls_config_msg tls; + struct iovec iov[2]; + + if (data == NULL || len == 0) + return (0); + + log_debug("%s: sending tls %s for %s[%u]", __func__, label, name, id); + + memset(&tls, 0, sizeof(tls)); + tls.id = id; + tls.tls_type = type; + tls.tls_len = len; + tls.tls_chunk_offset = 0; + + while (len > 0) { + tls.tls_chunk_len = len; + if (tls.tls_chunk_len > MAX_IMSG_DATA_SIZE - sizeof(tls)) + tls.tls_chunk_len = MAX_IMSG_DATA_SIZE - sizeof(tls); + + iov[0].iov_base = &tls; + iov[0].iov_len = sizeof(tls); + iov[1].iov_base = data; + iov[1].iov_len = tls.tls_chunk_len; + + if (imsg_composev_event(iev, IMSG_CFG_TLS, 0, 0, -1, iov, + 2) == -1) { + log_warn("%s: imsg_composev_event", __func__); + return (-1); + } + + tls.tls_chunk_offset += tls.tls_chunk_len; + data += tls.tls_chunk_len; + len -= tls.tls_chunk_len; + } + + return (0); +} + +int +config_send_tls(struct imsgev *iev, struct listener *l) +{ + struct listener_config *c = &l->conf; + + if (c->tls == 0) + return (0); + + if (config_send_tls_blob(iev, c->id, c->name, TLS_CFG_CA, "ca", + c->tls_ca, c->tls_ca_len) == -1 || + config_send_tls_blob(iev, c->id, c->name, TLS_CFG_CERT, "cert", + c->tls_cert, c->tls_cert_len) == -1 || + config_send_tls_blob(iev, c->id, c->name, TLS_CFG_CRL, "crl", + c->tls_crl, c->tls_crl_len) == -1 || + config_send_tls_blob(iev, c->id, c->name, TLS_CFG_KEY, "key", + c->tls_key, c->tls_key_len) == -1 || + config_send_tls_blob(iev, c->id, c->name, TLS_CFG_OCSP_STAPLE, + "ocsp staple", c->tls_ocsp_staple, c->tls_ocsp_staple_len) == -1) + return (-1); + + return (0); +} + +static int +config_recv_tls_blob(const char *name, uint32_t id, struct tls_config_msg *tls, + const char *label, uint8_t *data, size_t len, uint8_t **outdata, + size_t *outlen) +{ + log_debug("%s: getting tls %s (%zu:%zu@%zu) for %s[%u]", __func__, + label, tls->tls_len, len, tls->tls_chunk_offset, name, id); + + if (tls->tls_chunk_offset == 0) { + free(*outdata); + *outdata = NULL; + *outlen = 0; + if ((*outdata = calloc(1, tls->tls_len)) == NULL) + return (-1); + *outlen = tls->tls_len; + } + + if (*outdata == NULL) { + log_warnx("%s: tls config invalid chunk sequence", __func__); + return (-1); + } + if (*outlen != tls->tls_len) { + log_warnx("%s: tls config length mismatch (%zu != %zu)", + __func__, *outlen, tls->tls_len); + return (-1); + } + if (tls->tls_chunk_offset > tls->tls_len || + len > tls->tls_len - tls->tls_chunk_offset) { + log_warnx("%s: tls config invalid chunk length", __func__); + return (-1); + } + + memcpy(*outdata + tls->tls_chunk_offset, data, len); + return (0); +} + +int +config_recv_tls(struct mqttd *e, struct imsg *imsg) +{ + struct listener *l; + struct listener_config *c; + struct tls_config_msg tls; + uint8_t *p = imsg->data; + size_t len; + + IMSG_SIZE_CHECK(imsg, &tls); + memcpy(&tls, p, sizeof(tls)); + + len = tls.tls_chunk_len; + if (IMSG_DATA_SIZE(imsg) - sizeof(tls) < len) { + log_warnx("%s: invalid message length", __func__); + return (-1); + } + p += sizeof(tls); + + if ((l = config_listener_byid(e, tls.id)) == NULL) + return (0); + c = &l->conf; + + switch (tls.tls_type) { + case TLS_CFG_CA: + return (config_recv_tls_blob(c->name, c->id, &tls, "ca", p, len, + &c->tls_ca, &c->tls_ca_len)); + case TLS_CFG_CERT: + return (config_recv_tls_blob(c->name, c->id, &tls, "cert", p, + len, &c->tls_cert, &c->tls_cert_len)); + case TLS_CFG_CRL: + return (config_recv_tls_blob(c->name, c->id, &tls, "crl", p, + len, &c->tls_crl, &c->tls_crl_len)); + case TLS_CFG_KEY: + return (config_recv_tls_blob(c->name, c->id, &tls, "key", p, + len, &c->tls_key, &c->tls_key_len)); + case TLS_CFG_OCSP_STAPLE: + return (config_recv_tls_blob(c->name, c->id, &tls, + "ocsp staple", p, len, &c->tls_ocsp_staple, + &c->tls_ocsp_staple_len)); + default: + log_warnx("%s: unknown tls config type %d", __func__, + tls.tls_type); + return (-1); + } +} + +int +config_send_bridge(struct imsgev *iev, struct bridge_def *b) +{ + struct bridge_topic *bt; + + if (imsg_compose_event(iev, IMSG_CFG_BRIDGE, 0, 0, -1, &b->conf, + sizeof(b->conf)) == -1) + return (-1); + TAILQ_FOREACH(bt, &b->topics, entry) + if (imsg_compose_event(iev, IMSG_CFG_BRIDGE_TOPIC, 0, 0, -1, + &bt->conf, sizeof(bt->conf)) == -1) + return (-1); + return (0); +} + +int +config_send_bridge_ca(struct imsgev *iev, struct bridge_def *b) +{ + return (config_send_tls_blob(iev, b->conf.id, b->conf.name, + TLS_CFG_BRIDGE_CA, "bridge ca", b->conf.tls_ca, + b->conf.tls_ca_len)); +} + +int +config_recv_bridge_ca(struct bridge_config *c, struct imsg *imsg) +{ + struct tls_config_msg tls; + uint8_t *p = imsg->data; + size_t len; + + IMSG_SIZE_CHECK(imsg, &tls); + memcpy(&tls, p, sizeof(tls)); + len = tls.tls_chunk_len; + if (IMSG_DATA_SIZE(imsg) - sizeof(tls) < len) { + log_warnx("%s: invalid message length", __func__); + return (-1); + } + p += sizeof(tls); + return (config_recv_tls_blob(c->name, c->id, &tls, "bridge ca", p, + len, &c->tls_ca, &c->tls_ca_len)); +} + +int +config_send_snow_ca(struct imsgev *iev, struct snow_config *c) +{ + return (config_send_tls_blob(iev, c->id, c->name, TLS_CFG_BRIDGE_CA, + "snowflake ca", c->tls_ca, c->tls_ca_len)); +} + +void +config_free_bridge(struct bridge_def *b) +{ + struct bridge_topic *bt; + + while ((bt = TAILQ_FIRST(&b->topics)) != NULL) { + TAILQ_REMOVE(&b->topics, bt, entry); + free(bt); + } + free(b->conf.tls_ca_file); + free(b->conf.tls_ca); + if (b->snow != NULL) { + freezero(b->snow->key, b->snow->key_len); + free(b->snow->tls_ca_file); + free(b->snow->tls_ca); + explicit_bzero(b->snow, sizeof(*b->snow)); + free(b->snow); + } + explicit_bzero(b, sizeof(*b)); + free(b); +} + +int +config_send_user(struct imsgev *iev, struct user *u) +{ + return (imsg_compose_event(iev, IMSG_CFG_USER, 0, 0, -1, &u->conf, + sizeof(u->conf))); +} + +int +config_send_acl(struct imsgev *iev, struct acl *a) +{ + return (imsg_compose_event(iev, IMSG_CFG_ACL, 0, 0, -1, &a->conf, + sizeof(a->conf))); +} + +struct listener * +config_listener_byid(struct mqttd *e, uint32_t id) +{ + struct listener *l; + + TAILQ_FOREACH(l, &e->listeners, entry) + if (l->conf.id == id) + return (l); + return (NULL); +} + +void +config_free(struct mqttd *e) +{ + struct listener *l; + struct user *u; + struct acl *a; + struct bridge_def *b; + + while ((l = TAILQ_FIRST(&e->listeners)) != NULL) { + TAILQ_REMOVE(&e->listeners, l, entry); + config_free_listener(l); + } + while ((u = TAILQ_FIRST(&e->users)) != NULL) { + TAILQ_REMOVE(&e->users, u, entry); + explicit_bzero(u, sizeof(*u)); + free(u); + } + while ((a = TAILQ_FIRST(&e->acls)) != NULL) { + TAILQ_REMOVE(&e->acls, a, entry); + free(a); + } + while ((b = TAILQ_FIRST(&e->bridges)) != NULL) { + TAILQ_REMOVE(&e->bridges, b, entry); + config_free_bridge(b); + } + free(e); +} + +void +config_free_listener(struct listener *l) +{ + free(l->conf.tls_cert); + free(l->conf.tls_cert_file); + free(l->conf.tls_key); + free(l->conf.tls_key_file); + free(l->conf.tls_ca); + free(l->conf.tls_ca_file); + free(l->conf.tls_crl); + free(l->conf.tls_crl_file); + free(l->conf.tls_ocsp_staple); + free(l->conf.tls_ocsp_staple_file); + if (l->fd != -1) + close(l->fd); + free(l); +} blob - /dev/null blob + 2fb538fa061e09d314f3733770a7cb7704b31e0bd95030fba26272e921074105 (mode 644) --- /dev/null +++ mqttd/control.c @@ -0,0 +1,656 @@ +/* + * Copyright (c) 2020-2021, 2026 Tracey Emery + * Copyright (c) 2010-2015 Reyk Floeter + * Copyright (c) 2003, 2004 Henning Brauer + * + * 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 "mqttd.h" + +#define CONTROL_BACKLOG 5 + +struct control_sock { + const char *cs_name; + struct event cs_ev; + struct event cs_evt; + int cs_fd; + int cs_restricted; +}; + +struct ctl_conn { + TAILQ_ENTRY(ctl_conn) entry; + struct imsgev iev; + struct sockpeercred peercred; + int listeners_pending; +}; +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_broker; +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; + +static struct ctl_conn + *control_connbyfd(int); +static int control_init(struct control_sock *); +static int control_listen(struct control_sock *); +static void control_cleanup(struct control_sock *); +static void control_accept(int, short, void *); +static void control_close(int, struct control_sock *); +static void control_dispatch_imsg(int, short, void *); +static void control_dispatch_parent(int, short, void *); +static void control_dispatch_broker(int, short, void *); +static void control_dispatch_sockets(int, short, void *); +static void control_relay(struct imsg *); +static void control_sighdlr(int, short, void *); +static __dead void + control_shutdown(void); + +__dead void +control(int fd_parent, int fd_broker, int *fd_socks) +{ + int n; + + log_procinit("control"); + + /* the socket is bound as root, before the chroot */ + control_cs.cs_name = MQTTD_SOCKET; + if (control_init(&control_cs) == -1) + fatalx("%s: control_init", __func__); + + proc_privdrop("control"); + + 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_parent); + + if ((control_iev_broker = calloc(1, sizeof(*control_iev_broker))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(control_iev_broker, fd_broker, + control_dispatch_broker); + + if ((control_iev_socks = calloc(env->conf.prefork, + sizeof(*control_iev_socks))) == NULL) + fatal("%s: calloc", __func__); + for (n = 0; n < env->conf.prefork; 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(); +} + +static void +control_sighdlr(int sig, short event, void *arg) +{ + switch (sig) { + case SIGINT: + case SIGTERM: + control_shutdown(); + break; + case SIGHUP: + case SIGPIPE: + case SIGUSR1: + break; + default: + fatalx("%s: unexpected signal", __func__); + } +} + +static __dead void +control_shutdown(void) +{ + struct ctl_conn *c; + int n; + + control_cleanup(&control_cs); + + while ((c = TAILQ_FIRST(&ctl_conns)) != NULL) { + TAILQ_REMOVE(&ctl_conns, c, entry); + imsg_event_shutdown(&c->iev); + free(c); + } + + imsg_event_shutdown(control_iev_parent); + free(control_iev_parent); + imsg_event_shutdown(control_iev_broker); + free(control_iev_broker); + for (n = 0; n < env->conf.prefork; n++) + imsg_event_shutdown(&control_iev_socks[n]); + free(control_iev_socks); + + log_info("control exiting, pid %d", getpid()); + exit(0); +} + +/* replies carry the requesting connection's fd as peerid */ +static void +control_relay(struct imsg *imsg) +{ + struct ctl_conn *c; + + if ((c = control_connbyfd(imsg->hdr.peerid)) == NULL) { + log_debug("%s: fd %d: not found", __func__, imsg->hdr.peerid); + return; + } + imsg_compose_event(&c->iev, imsg->hdr.type, 0, 0, -1, imsg->data, + IMSG_DATA_SIZE(imsg)); +} + +static void +control_dispatch_parent(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + 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__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CTL_SHOW_PARENT_DATA: + case IMSG_CTL_SHOW_PARENT_END: + control_relay(&imsg); + break; + case IMSG_CTL_START: + break; + default: + log_warnx("%s: unexpected imsg %d from parent", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +control_dispatch_broker(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost broker process", __func__); + control_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost broker process", __func__); + control_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CTL_OK: + case IMSG_CTL_FAIL: + case IMSG_CTL_SHOW_BROKER_DATA: + case IMSG_CTL_SHOW_BROKER_END: + case IMSG_CTL_SHOW_CLIENTS_DATA: + case IMSG_CTL_SHOW_CLIENTS_END: + case IMSG_CTL_SHOW_SUBS_DATA: + case IMSG_CTL_SHOW_SUBS_END: + case IMSG_CTL_SHOW_RETAINED_DATA: + case IMSG_CTL_SHOW_RETAINED_END: + case IMSG_CTL_SHOW_BRIDGES_DATA: + case IMSG_CTL_SHOW_BRIDGES_END: + control_relay(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from broker", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static 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(); + } + } + 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__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CTL_SHOW_LISTENERS_DATA: + control_relay(&imsg); + break; + case IMSG_CTL_SHOW_LISTENERS_END: + /* + * Every sockets process answers, so the end marker + * is passed on once the last one has replied. + */ + c = control_connbyfd(imsg.hdr.peerid); + if (c != NULL && --c->listeners_pending == 0) + control_relay(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from sockets", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static int +control_init(struct control_sock *cs) +{ + struct sockaddr_un sun; + int fd; + mode_t old_umask, mode; + + if (cs->cs_name == NULL) + return (0); + + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); + if (fd == -1) { + log_warn("%s: socket", __func__); + return (-1); + } + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + if (strlcpy(sun.sun_path, cs->cs_name, sizeof(sun.sun_path)) >= + sizeof(sun.sun_path)) { + log_warnx("%s: socket name too long", __func__); + close(fd); + return (-1); + } + + if (unlink(cs->cs_name) == -1) + if (errno != ENOENT) { + log_warn("%s: unlink %s", __func__, cs->cs_name); + close(fd); + return (-1); + } + + if (cs->cs_restricted) { + old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH); + mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + } else { + old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); + mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP; + } + + if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { + log_warn("%s: bind: %s", __func__, cs->cs_name); + close(fd); + (void)umask(old_umask); + return (-1); + } + (void)umask(old_umask); + + if (chmod(cs->cs_name, mode) == -1) { + log_warn("%s: chmod", __func__); + close(fd); + (void)unlink(cs->cs_name); + return (-1); + } + + cs->cs_fd = fd; + return (0); +} + +static int +control_listen(struct control_sock *cs) +{ + if (cs->cs_name == NULL) + return (0); + + if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) { + log_warn("%s: listen", __func__); + return (-1); + } + + event_set(&cs->cs_ev, cs->cs_fd, EV_READ, control_accept, cs); + event_add(&cs->cs_ev, NULL); + evtimer_set(&cs->cs_evt, control_accept, cs); + + return (0); +} + +static void +control_cleanup(struct control_sock *cs) +{ + if (cs->cs_name == NULL) + return; + event_del(&cs->cs_ev); + event_del(&cs->cs_evt); + close(cs->cs_fd); +} + +static void +control_accept(int listenfd, short event, void *arg) +{ + struct control_sock *cs = arg; + struct sockaddr_un sun; + struct ctl_conn *c; + socklen_t len; + int connfd; + + event_add(&cs->cs_ev, NULL); + if (event & EV_TIMEOUT) + return; + + len = sizeof(sun); + connfd = accept4(listenfd, (struct sockaddr *)&sun, &len, + SOCK_NONBLOCK); + if (connfd == -1) { + /* + * Pause accept if we are out of file descriptors, or + * libevent will haunt us here too. + */ + if (errno == ENFILE || errno == EMFILE) { + struct timeval evtpause = { 1, 0 }; + + event_del(&cs->cs_ev); + evtimer_add(&cs->cs_evt, &evtpause); + } else if (errno != EWOULDBLOCK && errno != EINTR && + errno != ECONNABORTED) + log_warn("%s: accept", __func__); + return; + } + + if ((c = calloc(1, sizeof(*c))) == NULL) { + log_warn("%s: calloc", __func__); + close(connfd); + 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__); + close(connfd); + free(c); + return; + } + + if (imsgbuf_init(&c->iev.ibuf, connfd) == -1) { + log_warn("%s: imsgbuf_init", __func__); + close(connfd); + free(c); + return; + } + c->iev.handler = control_dispatch_imsg; + c->iev.events = EV_READ; + event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, c->iev.handler, + &c->iev); + event_add(&c->iev.ev, NULL); + + TAILQ_INSERT_TAIL(&ctl_conns, c, entry); +} + +static struct ctl_conn * +control_connbyfd(int fd) +{ + struct ctl_conn *c; + + TAILQ_FOREACH(c, &ctl_conns, entry) + if (c->iev.ibuf.fd == fd) + break; + return (c); +} + +static void +control_close(int fd, struct control_sock *cs) +{ + struct ctl_conn *c; + + if ((c = control_connbyfd(fd)) == NULL) { + log_warn("%s: fd %d: not found", __func__, fd); + return; + } + + imsgbuf_clear(&c->iev.ibuf); + TAILQ_REMOVE(&ctl_conns, c, entry); + + event_del(&c->iev.ev); + close(c->iev.ibuf.fd); + + /* some file descriptors are available again */ + if (evtimer_pending(&cs->cs_evt, NULL)) { + evtimer_del(&cs->cs_evt); + event_add(&cs->cs_ev, NULL); + } + + free(c); +} + +static void +control_dispatch_imsg(int fd, short event, void *arg) +{ + struct control_sock *cs = &control_cs; + struct ctl_control_info ci; + struct ctl_conn *c; + struct imsg imsg; + int n, v, ret = 0; + + if ((c = control_connbyfd(fd)) == NULL) { + log_warn("%s: fd %d: not found", __func__, fd); + return; + } + + if (event & EV_READ) { + n = imsgbuf_read(&c->iev.ibuf); + if (n == -1 || n == 0) { + control_close(fd, cs); + return; + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(&c->iev.ibuf) == -1) { + control_close(fd, cs); + return; + } + } + + for (;;) { + n = imsgbuf_get(&c->iev.ibuf, &imsg); + if (n == -1) { + control_close(fd, cs); + return; + } + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CTL_KILL_CLIENT: + case IMSG_CTL_DISCONNECT_ALL: + case IMSG_CTL_VERBOSE: + case IMSG_CTL_SAVE: + if (c->peercred.uid != 0) { + log_warnx("denied request %d from uid %d", + imsg.hdr.type, c->peercred.uid); + ret = EPERM; + goto fail; + } + break; + default: + break; + } + + /* the connection fd is the peerid replies are routed by */ + imsg.hdr.peerid = fd; + + switch (imsg.hdr.type) { + case IMSG_CTL_VERBOSE: + if (IMSG_DATA_SIZE(&imsg) != sizeof(v)) { + ret = EINVAL; + goto fail; + } + memcpy(&v, imsg.data, sizeof(v)); + log_setverbose(v); + imsg_forward_event(control_iev_parent, &imsg); + break; + case IMSG_CTL_SHOW_CONTROL_REQUEST: + ci.verbose = log_getverbose(); + imsg_compose_event(&c->iev, IMSG_CTL_SHOW_CONTROL_DATA, + 0, 0, -1, &ci, sizeof(ci)); + imsg_compose_event(&c->iev, IMSG_CTL_SHOW_CONTROL_END, + 0, 0, -1, NULL, 0); + break; + case IMSG_CTL_SHOW_LISTENERS_REQUEST: + c->listeners_pending = env->conf.prefork; + /* FALLTHROUGH */ + case IMSG_CTL_SHOW_PARENT_REQUEST: + case IMSG_CTL_SHOW_BROKER_REQUEST: + case IMSG_CTL_SHOW_CLIENTS_REQUEST: + case IMSG_CTL_SHOW_SUBS_REQUEST: + case IMSG_CTL_SHOW_RETAINED_REQUEST: + case IMSG_CTL_SHOW_BRIDGES_REQUEST: + case IMSG_CTL_KILL_CLIENT: + case IMSG_CTL_DISCONNECT_ALL: + case IMSG_CTL_SAVE: + if (imsg_forward_event(control_iev_parent, + &imsg) == -1) { + imsg_free(&imsg); + control_close(fd, cs); + return; + } + break; + default: + log_debug("%s: error handling imsg %d", __func__, + imsg.hdr.type); + imsg_free(&imsg); + control_close(fd, cs); + return; + } + imsg_free(&imsg); + } + imsg_event_add(&c->iev); + 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(fd, cs); +} blob - /dev/null blob + 07eab184bf774ec96a5ad3c10b8a4d6999e041f23630724641b862fa71c3d197 (mode 644) --- /dev/null +++ mqttd/db.c @@ -0,0 +1,808 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * Broker side of persistence: serialise persistent sessions and + * retained messages into records for the storage process, and rebuild + * them from the records played back at startup. Restore inserts only + * what is absent, so a snapshot arriving late never clobbers live state. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" +#include "mqtt.h" +#include "broker.h" + +struct db_rd { + const uint8_t *p; + size_t left; +}; + +static struct event ev_db; +static struct mqtt_buf restore_buf; +static uint32_t restore_type; +static uint32_t restore_total; +static int restore_active; +static char skip_client[MQTTD_MAX_CLIENTID + 1]; +static uint64_t restored_sessions; +static uint64_t restored_retained; +static uint64_t restored_deliveries; + +static void db_timer(int, short, void *); +static int db_send_record(uint32_t, const struct mqtt_buf *); +static int put_u64(struct mqtt_buf *, uint64_t); +static int put_blob(struct mqtt_buf *, const void *, uint32_t); +static int put_message(struct mqtt_buf *, const struct message *); +static void db_retained_cb(struct message *, void *); +static int db_put_session(struct session *); +static int db_put_delivery(struct session *, struct delivery *); +static int rd_u8(struct db_rd *, uint8_t *); +static int rd_u16(struct db_rd *, uint16_t *); +static int rd_u32(struct db_rd *, uint32_t *); +static int rd_u64(struct db_rd *, uint64_t *); +static int rd_str(struct db_rd *, const char **, uint16_t *); +static int rd_blob(struct db_rd *, const uint8_t **, uint32_t *); +static struct message + *rd_message(struct db_rd *); +static int rd_client(struct db_rd *, char *, size_t); +static void db_restore_dispatch(uint32_t, const uint8_t *, size_t); +static void db_restore_retained(struct db_rd *); +static void db_restore_session(struct db_rd *); +static void db_restore_subscription(struct db_rd *); +static void db_restore_delivery(struct db_rd *); +static void db_restore_incoming(struct db_rd *); +static void db_publish_now(struct message *); + +void +db_start(void) +{ + struct timeval tv; + + if (!env->conf.persist || env->conf.persist_interval == 0 || + evtimer_pending(&ev_db, NULL)) + return; + evtimer_set(&ev_db, db_timer, NULL); + tv.tv_sec = env->conf.persist_interval; + tv.tv_usec = 0; + evtimer_add(&ev_db, &tv); +} + +static void +db_timer(int fd, short event, void *arg) +{ + struct timeval tv; + + db_snapshot(); + + if (!env->conf.persist || env->conf.persist_interval == 0) + return; + tv.tv_sec = env->conf.persist_interval; + tv.tv_usec = 0; + evtimer_add(&ev_db, &tv); +} + +static int +db_send_record(uint32_t type, const struct mqtt_buf *b) +{ + struct db_chunk hdr; + struct iovec iov[2]; + size_t off = 0, n; + + hdr.type = type; + hdr.total = b->len; + do { + n = b->len - off; + if (n > DB_CHUNK) + n = DB_CHUNK; + hdr.offset = off; + hdr.len = n; + iov[0].iov_base = &hdr; + iov[0].iov_len = sizeof(hdr); + iov[1].iov_base = b->data + off; + iov[1].iov_len = n; + if (imsg_composev_event(broker_iev_storage, IMSG_DB_RECORD, 0, + 0, -1, iov, n > 0 ? 2 : 1) == -1) { + log_warn("%s: imsg_composev_event", __func__); + return (-1); + } + off += n; + } while (off < b->len); + return (0); +} + +static int +put_u64(struct mqtt_buf *b, uint64_t v) +{ + if (mqtt_buf_u32(b, v >> 32) == -1) + return (-1); + return (mqtt_buf_u32(b, v & 0xffffffff)); +} + +static int +put_blob(struct mqtt_buf *b, const void *p, uint32_t len) +{ + if (mqtt_buf_u32(b, len) == -1) + return (-1); + return (mqtt_buf_bytes(b, p, len)); +} + +static int +put_message(struct mqtt_buf *b, const struct message *m) +{ + if (mqtt_buf_str(b, m->topic, m->topic_len) == -1 || + mqtt_buf_u8(b, m->qos) == -1 || + mqtt_buf_u8(b, m->retain) == -1 || + put_u64(b, m->received) == -1 || + mqtt_buf_u32(b, m->expiry_interval) == -1 || + mqtt_buf_u8(b, m->has_payload_format) == -1 || + mqtt_buf_u8(b, m->payload_format) == -1 || + put_blob(b, m->props, m->props_len) == -1 || + put_blob(b, m->payload, m->payload_len) == -1) + return (-1); + return (0); +} + +static void +db_retained_cb(struct message *m, void *arg) +{ + struct mqtt_buf b; + + /* broker statistics are regenerated, not restored */ + if (m->topic_len >= 5 && memcmp(m->topic, "$SYS/", 5) == 0) + return; + if (message_expired(m, time(NULL))) + return; + + mqtt_buf_init(&b); + if (put_message(&b, m) == 0) + db_send_record(DB_RETAINED, &b); + mqtt_buf_free(&b); +} + +static int +db_put_delivery(struct session *s, struct delivery *d) +{ + struct mqtt_buf b; + int i, ret = -1; + + mqtt_buf_init(&b); + if (mqtt_buf_str(&b, s->client_id, strlen(s->client_id)) == -1 || + mqtt_buf_u8(&b, d->state) == -1 || + mqtt_buf_u16(&b, d->pid) == -1 || + mqtt_buf_u8(&b, d->qos) == -1 || + mqtt_buf_u8(&b, d->retain) == -1 || + mqtt_buf_u8(&b, d->nsubids) == -1) + goto done; + for (i = 0; i < d->nsubids; i++) + if (mqtt_buf_u32(&b, d->subids[i]) == -1) + goto done; + if (mqtt_buf_u8(&b, d->msg != NULL) == -1) + goto done; + if (d->msg != NULL && put_message(&b, d->msg) == -1) + goto done; + ret = db_send_record(DB_DELIVERY, &b); +done: + mqtt_buf_free(&b); + return (ret); +} + +static int +db_put_session(struct session *s) +{ + struct mqtt_buf b; + struct subscription *sub; + struct delivery *d; + struct incoming *in; + size_t cidlen = strlen(s->client_id); + int ret = -1; + + mqtt_buf_init(&b); + if (mqtt_buf_str(&b, s->client_id, cidlen) == -1 || + mqtt_buf_str(&b, s->username != NULL ? s->username : "", + s->username != NULL ? strlen(s->username) : 0) == -1 || + mqtt_buf_u8(&b, s->version) == -1 || + mqtt_buf_u32(&b, s->expiry_interval) == -1 || + put_u64(&b, s->conn != NULL ? 0 : s->disconnected_at) == -1 || + mqtt_buf_u32(&b, s->will_delay) == -1 || + mqtt_buf_u16(&b, s->next_pid) == -1 || + mqtt_buf_u8(&b, s->will != NULL) == -1) + goto done; + if (s->will != NULL && put_message(&b, s->will) == -1) + goto done; + if (db_send_record(DB_SESSION, &b) == -1) + goto done; + + TAILQ_FOREACH(sub, &s->subs, sess_entry) { + mqtt_buf_reset(&b); + if (mqtt_buf_str(&b, s->client_id, cidlen) == -1 || + mqtt_buf_str(&b, sub->share != NULL ? sub->share : "", + sub->share != NULL ? strlen(sub->share) : 0) == -1) + goto done; + /* the filter is reconstructed from the trie node */ + if (mqtt_buf_reserve(&b, 2 + MQTTD_MAXTEXT) == -1) + goto done; + if (topic_node_name(sub->node, (char *)b.data + b.len + 2, + MQTTD_MAXTEXT) == -1) + continue; + { + size_t flen = strlen((char *)b.data + b.len + 2); + + b.data[b.len] = flen >> 8; + b.data[b.len + 1] = flen & 0xff; + b.len += 2 + flen; + } + if (mqtt_buf_u8(&b, sub->qos) == -1 || + mqtt_buf_u8(&b, sub->no_local) == -1 || + mqtt_buf_u8(&b, sub->rap) == -1 || + mqtt_buf_u8(&b, sub->rh) == -1 || + mqtt_buf_u32(&b, sub->id) == -1) + goto done; + if (db_send_record(DB_SUBSCRIPTION, &b) == -1) + goto done; + } + + RB_FOREACH(d, dlvtree, &s->inflight) + if (db_put_delivery(s, d) == -1) + goto done; + TAILQ_FOREACH(d, &s->queue, entry) + if (db_put_delivery(s, d) == -1) + goto done; + + RB_FOREACH(in, inctree, &s->incoming) { + mqtt_buf_reset(&b); + if (mqtt_buf_str(&b, s->client_id, cidlen) == -1 || + mqtt_buf_u16(&b, in->pid) == -1) + goto done; + if (db_send_record(DB_INCOMING, &b) == -1) + goto done; + } + ret = 0; +done: + mqtt_buf_free(&b); + return (ret); +} + +void +db_snapshot(void) +{ + struct mqtt_buf b; + struct session *s; + + if (!env->conf.persist || broker_iev_storage == NULL) + return; + + mqtt_buf_init(&b); + if (mqtt_buf_bytes(&b, MQTTD_DB_MAGIC, 8) == -1 || + put_u64(&b, time(NULL)) == -1 || + db_send_record(DB_BEGIN, &b) == -1) + goto done; + + topic_retained_foreach(&broker_env->topics, db_retained_cb, NULL); + + RB_FOREACH(s, sesstree, &broker_env->sessions) { + /* sessions that end with the connection are not kept */ + if (s->expiry_interval == 0) + continue; + if (db_put_session(s) == -1) + goto done; + } + + mqtt_buf_reset(&b); + db_send_record(DB_END, &b); + + broker_env->stats.snapshots++; + broker_env->stats.last_snapshot = time(NULL); + log_debug("%s: snapshot queued for storage", __func__); +done: + mqtt_buf_free(&b); +} + +/* at shutdown the snapshot must leave this process before it exits */ +void +db_snapshot_sync(void) +{ + struct pollfd pfd; + + if (!env->conf.persist || broker_iev_storage == NULL) + return; + + db_snapshot(); + + while (imsgbuf_queuelen(&broker_iev_storage->ibuf) > 0) { + if (imsgbuf_flush(&broker_iev_storage->ibuf) == 0) + break; + if (errno != EAGAIN) { + log_warn("%s: imsgbuf_flush", __func__); + return; + } + pfd.fd = broker_iev_storage->ibuf.fd; + pfd.events = POLLOUT; + if (poll(&pfd, 1, 10000) <= 0) { + log_warnx("%s: storage did not drain, snapshot lost", + __func__); + return; + } + } + log_info("final snapshot handed to storage"); +} + +static int +rd_u8(struct db_rd *rd, uint8_t *v) +{ + if (rd->left < 1) + return (-1); + *v = *rd->p++; + rd->left--; + return (0); +} + +static int +rd_u16(struct db_rd *rd, uint16_t *v) +{ + if (rd->left < 2) + return (-1); + *v = (uint16_t)rd->p[0] << 8 | rd->p[1]; + rd->p += 2; + rd->left -= 2; + return (0); +} + +static int +rd_u32(struct db_rd *rd, uint32_t *v) +{ + if (rd->left < 4) + return (-1); + *v = (uint32_t)rd->p[0] << 24 | (uint32_t)rd->p[1] << 16 | + (uint32_t)rd->p[2] << 8 | rd->p[3]; + rd->p += 4; + rd->left -= 4; + return (0); +} + +static int +rd_u64(struct db_rd *rd, uint64_t *v) +{ + uint32_t hi, lo; + + if (rd_u32(rd, &hi) == -1 || rd_u32(rd, &lo) == -1) + return (-1); + *v = (uint64_t)hi << 32 | lo; + return (0); +} + +static int +rd_str(struct db_rd *rd, const char **s, uint16_t *len) +{ + if (rd_u16(rd, len) == -1 || rd->left < *len) + return (-1); + *s = (const char *)rd->p; + rd->p += *len; + rd->left -= *len; + return (0); +} + +static int +rd_blob(struct db_rd *rd, const uint8_t **s, uint32_t *len) +{ + if (rd_u32(rd, len) == -1 || rd->left < *len) + return (-1); + *s = rd->p; + rd->p += *len; + rd->left -= *len; + return (0); +} + +static struct message * +rd_message(struct db_rd *rd) +{ + struct message *m; + const char *topic; + const uint8_t *props, *payload; + uint64_t received; + uint32_t expiry, props_len, payload_len; + uint16_t topic_len; + uint8_t qos, retain, has_pf, pf; + + if (rd_str(rd, &topic, &topic_len) == -1 || + rd_u8(rd, &qos) == -1 || rd_u8(rd, &retain) == -1 || + rd_u64(rd, &received) == -1 || rd_u32(rd, &expiry) == -1 || + rd_u8(rd, &has_pf) == -1 || rd_u8(rd, &pf) == -1 || + rd_blob(rd, &props, &props_len) == -1 || + rd_blob(rd, &payload, &payload_len) == -1) + return (NULL); + if (qos > 2 || !mqtt_topic_valid((const uint8_t *)topic, topic_len)) + return (NULL); + + m = message_new((const uint8_t *)topic, topic_len, payload, + payload_len, qos, retain); + if (m == NULL) + return (NULL); + m->received = received; + m->expiry_interval = expiry; + if (expiry != 0) + m->expires = (time_t)received + (time_t)expiry; + m->has_payload_format = has_pf; + m->payload_format = pf; + if (props_len > 0) { + if ((m->props = malloc(props_len)) == NULL) { + message_unref(m); + return (NULL); + } + memcpy(m->props, props, props_len); + m->props_len = props_len; + } + return (m); +} + +static int +rd_client(struct db_rd *rd, char *buf, size_t len) +{ + const char *s; + uint16_t n; + + if (rd_str(rd, &s, &n) == -1 || n == 0 || n >= len) + return (-1); + memcpy(buf, s, n); + buf[n] = '\0'; + return (0); +} + +void +db_restore_record(struct imsg *imsg) +{ + struct db_chunk hdr; + uint8_t *p = imsg->data; + size_t len = IMSG_DATA_SIZE(imsg); + + if (len < sizeof(hdr)) { + log_warnx("%s: short record imsg", __func__); + return; + } + memcpy(&hdr, p, sizeof(hdr)); + p += sizeof(hdr); + len -= sizeof(hdr); + + if (hdr.len != len || hdr.offset > hdr.total || + hdr.len > hdr.total - hdr.offset || + hdr.total > env->conf.max_packet_size + 4096) { + log_warnx("%s: bad record chunk", __func__); + restore_active = 0; + return; + } + + if (hdr.offset == 0) { + mqtt_buf_reset(&restore_buf); + restore_type = hdr.type; + restore_total = hdr.total; + restore_active = 1; + } + if (!restore_active || hdr.type != restore_type || + hdr.total != restore_total || hdr.offset != restore_buf.len) { + log_warnx("%s: record chunk sequence error", __func__); + restore_active = 0; + return; + } + if (mqtt_buf_bytes(&restore_buf, p, len) == -1) { + restore_active = 0; + return; + } + if (restore_buf.len < restore_total) + return; + + restore_active = 0; + db_restore_dispatch(restore_type, restore_buf.data, restore_buf.len); +} + +static void +db_restore_dispatch(uint32_t type, const uint8_t *data, size_t len) +{ + struct db_rd rd = { data, len }; + + switch (type) { + case DB_RETAINED: + db_restore_retained(&rd); + break; + case DB_SESSION: + db_restore_session(&rd); + break; + case DB_SUBSCRIPTION: + db_restore_subscription(&rd); + break; + case DB_DELIVERY: + db_restore_delivery(&rd); + break; + case DB_INCOMING: + db_restore_incoming(&rd); + break; + default: + log_warnx("%s: unknown record type %u", __func__, type); + break; + } +} + +static void +db_publish_now(struct message *m) +{ + m->received = time(NULL); + if (m->expiry_interval != 0) + m->expires = m->received + (time_t)m->expiry_interval; + if (env->conf.sparkplug) + sparkplug_will_hook(m); + broker_route(m); +} + +static void +db_restore_retained(struct db_rd *rd) +{ + struct message *m; + + if ((m = rd_message(rd)) == NULL) { + log_warnx("%s: bad retained record", __func__); + return; + } + if (!message_expired(m, time(NULL)) && + topic_retained_get(&broker_env->topics, m->topic, + m->topic_len) == NULL) { + m->retain = 1; + topic_retain(&broker_env->topics, m); + restored_retained++; + } + message_unref(m); +} + +static void +db_restore_session(struct db_rd *rd) +{ + struct session *s; + struct message *will = NULL; + struct timeval tv; + const char *user; + char cid[MQTTD_MAX_CLIENTID + 1]; + uint64_t disconnected_at, elapsed; + uint32_t expiry, will_delay; + uint16_t userlen, next_pid; + uint8_t version, has_will; + time_t now = time(NULL); + + if (rd_client(rd, cid, sizeof(cid)) == -1 || + rd_str(rd, &user, &userlen) == -1 || rd_u8(rd, &version) == -1 || + rd_u32(rd, &expiry) == -1 || rd_u64(rd, &disconnected_at) == -1 || + rd_u32(rd, &will_delay) == -1 || rd_u16(rd, &next_pid) == -1 || + rd_u8(rd, &has_will) == -1 || + (has_will && (will = rd_message(rd)) == NULL)) { + log_warnx("%s: bad session record", __func__); + return; + } + + if ((s = session_find(cid)) != NULL) { + if (will != NULL) + message_unref(will); + if (s->bridge != NULL) { + /* the bridge session exists from config; refill it */ + skip_client[0] = '\0'; + return; + } + /* a live client got here first; drop its stale records */ + log_debug("%s: %s already present, not restored", __func__, + cid); + strlcpy(skip_client, cid, sizeof(skip_client)); + return; + } + skip_client[0] = '\0'; + + elapsed = 0; + if (disconnected_at != 0 && (uint64_t)now > disconnected_at) + elapsed = now - disconnected_at; + + if (expiry == 0 || (expiry != UINT32_MAX && elapsed >= expiry)) { + /* the session ended while we were down; its will is due */ + if (will != NULL) { + db_publish_now(will); + message_unref(will); + } + return; + } + + if ((s = session_new(cid)) == NULL) { + if (will != NULL) + message_unref(will); + return; + } + s->version = version; + s->expiry_interval = expiry; + s->will_delay = will_delay; + s->next_pid = next_pid != 0 ? next_pid : 1; + s->disconnected_at = now - elapsed; + if (userlen > 0 && userlen < MQTTD_MAXNAME) { + if ((s->username = strndup(user, userlen)) == NULL) + fatal("%s: strndup", __func__); + } + + if (will != NULL) { + will->origin = s->serial; + if (will_delay <= elapsed) { + db_publish_now(will); + message_unref(will); + } else { + s->will = will; + tv.tv_sec = will_delay - elapsed; + tv.tv_usec = 0; + evtimer_add(&s->will_ev, &tv); + } + } + + if (expiry != UINT32_MAX) { + tv.tv_sec = expiry - elapsed; + tv.tv_usec = 0; + evtimer_add(&s->expiry_ev, &tv); + } + restored_sessions++; +} + +static void +db_restore_subscription(struct db_rd *rd) +{ + struct session *s; + struct subscription *sub; + const char *share, *filter; + char cid[MQTTD_MAX_CLIENTID + 1]; + uint32_t id; + uint16_t sharelen, flen; + uint8_t qos, nl, rap, rh; + + if (rd_client(rd, cid, sizeof(cid)) == -1 || + rd_str(rd, &share, &sharelen) == -1 || + rd_str(rd, &filter, &flen) == -1 || rd_u8(rd, &qos) == -1 || + rd_u8(rd, &nl) == -1 || rd_u8(rd, &rap) == -1 || + rd_u8(rd, &rh) == -1 || rd_u32(rd, &id) == -1) { + log_warnx("%s: bad subscription record", __func__); + return; + } + if (strcmp(cid, skip_client) == 0 || (s = session_find(cid)) == NULL) + return; + /* bridge subscriptions come from the configuration, not the file */ + if (s->bridge != NULL) + return; + if (qos > 2 || sharelen >= MQTTD_MAXNAME || + !mqtt_filter_valid((const uint8_t *)filter, flen)) + return; + if (topic_find_sub(&broker_env->topics, filter, flen, + sharelen > 0 ? share : NULL, s) != NULL) + return; + + if ((sub = calloc(1, sizeof(*sub))) == NULL) + fatal("%s: calloc", __func__); + if (sharelen > 0 && (sub->share = strndup(share, sharelen)) == NULL) + fatal("%s: strndup", __func__); + if (topic_subscribe(&broker_env->topics, filter, flen, sub) == NULL) { + free(sub->share); + free(sub); + return; + } + sub->session = s; + sub->qos = qos; + sub->no_local = nl; + sub->rap = rap; + sub->rh = rh; + sub->id = id; + TAILQ_INSERT_TAIL(&s->subs, sub, sess_entry); + s->nsubs++; +} + +static void +db_restore_delivery(struct db_rd *rd) +{ + struct session *s; + struct delivery *d; + char cid[MQTTD_MAX_CLIENTID + 1]; + uint32_t subid; + uint16_t pid; + uint8_t state, qos, retain, nsubids, has_msg, i; + + if (rd_client(rd, cid, sizeof(cid)) == -1 || rd_u8(rd, &state) == -1 || + rd_u16(rd, &pid) == -1 || rd_u8(rd, &qos) == -1 || + rd_u8(rd, &retain) == -1 || rd_u8(rd, &nsubids) == -1) { + log_warnx("%s: bad delivery record", __func__); + return; + } + if ((d = calloc(1, sizeof(*d))) == NULL) + fatal("%s: calloc", __func__); + d->state = state; + d->pid = pid; + d->qos = qos; + d->retain = retain; + for (i = 0; i < nsubids; i++) { + if (rd_u32(rd, &subid) == -1) + goto bad; + delivery_add_subid(d, subid); + } + if (rd_u8(rd, &has_msg) == -1) + goto bad; + if (has_msg && (d->msg = rd_message(rd)) == NULL) + goto bad; + + if (strcmp(cid, skip_client) == 0 || (s = session_find(cid)) == NULL || + qos == 0 || qos > 2 || state > DLV_PUBREL || + (state != DLV_PUBREL && d->msg == NULL)) { + delivery_free(d); + return; + } + d->session = s; + + if (state == DLV_QUEUED) { + if (s->nqueued >= session_max_queued(s)) { + delivery_free(d); + return; + } + TAILQ_INSERT_TAIL(&s->queue, d, entry); + s->nqueued++; + } else { + if (pid == 0 || session_inflight_find(s, pid) != NULL) { + delivery_free(d); + return; + } + RB_INSERT(dlvtree, &s->inflight, d); + s->ninflight++; + } + restored_deliveries++; + return; +bad: + log_warnx("%s: bad delivery record", __func__); + delivery_free(d); +} + +static void +db_restore_incoming(struct db_rd *rd) +{ + struct session *s; + char cid[MQTTD_MAX_CLIENTID + 1]; + uint16_t pid; + + if (rd_client(rd, cid, sizeof(cid)) == -1 || rd_u16(rd, &pid) == -1) { + log_warnx("%s: bad incoming record", __func__); + return; + } + if (strcmp(cid, skip_client) == 0 || (s = session_find(cid)) == NULL || + pid == 0) + return; + session_incoming_add(s, pid); +} + +void +db_restore_end(void) +{ + log_info("restored %llu session(s), %llu retained message(s), " + "%llu pending deliver%s", + (unsigned long long)restored_sessions, + (unsigned long long)restored_retained, + (unsigned long long)restored_deliveries, + restored_deliveries == 1 ? "y" : "ies"); + mqtt_buf_free(&restore_buf); + restore_active = 0; + skip_client[0] = '\0'; +} blob - /dev/null blob + 05cbede47d28a70f01d884901bb3efa6b73457ab4a1a7bacfa40cd085bb885d6 (mode 644) --- /dev/null +++ mqttd/examples/auth/mqttd-auth.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl +# +# Example external authenticator for mqttd(8). Runs as _mqttd with the +# broker's request pipe on stdin and stdout, one request per line: +# +# auth +# +# and answers +# +# ok +# fail +# +# This one accepts any user whose password is "letmein". Replace the +# check() body with an LDAP, RADIUS, database or JWT lookup; the broker +# fails the request after 10 seconds if no answer arrives, and restarts +# this program if it exits. + +use strict; +use warnings; + +$| = 1; + +sub check { + my ($user, $password, $client_id, $peer) = @_; + + return $password eq "letmein"; +} + +# Enhanced authentication example: the SASL PLAIN method, whose single +# client message is "authzid\0authcid\0password". Real deployments +# would implement SCRAM-SHA-256 here, answering "continue" with the +# server-first message and finishing with "ok ". +sub enhanced { + my ($method, $data) = @_; + + return (0) unless $method eq "PLAIN"; + my (undef, $authcid, $password) = split /\0/, $data, 3; + return (0) unless defined $password && $password eq "letmein"; + return (1, "", unpack("H*", $authcid)); +} + +while (my $line = ) { + chomp $line; + my @f = split / /, $line; + my $cmd = shift @f; + next unless defined $cmd; + + if ($cmd eq "auth") { + my ($id, $user, $pass, $cid, $peer) = @f; + next unless defined $peer; + my $ok = check(pack("H*", $user), pack("H*", $pass), + pack("H*", $cid), $peer); + print "$id ", ($ok ? "ok" : "fail"), "\n"; + } elsif ($cmd eq "auth-start") { + my ($id, $user, $cid, $peer, $method, $data) = @f; + next unless defined $method; + my ($ok, $reply, $who) = enhanced(pack("H*", $method), + pack("H*", $data // "")); + print "$id ", ($ok ? "ok $reply $who" : "fail"), "\n"; + } elsif ($cmd eq "auth-data") { + # PLAIN needs no second round trip + my ($id) = @f; + print "$id fail\n" if defined $id; + } +} blob - /dev/null blob + 3fd239245b477c9df343fe91b4d23f849e9cecd795ca1758d39945798e0b4028 (mode 644) --- /dev/null +++ mqttd/examples/etc/mqttd.conf @@ -0,0 +1,120 @@ +# mqttd.conf - example configuration for the mqttd MQTT broker +# +# Password hashes are generated with encrypt(1): +# encrypt -b 10 +# +# Keep this file readable by root only once it holds users. + +# Run four sockets processes; each handles its share of the connections. +prefork 4 + +# Refuse CONNECT once this many clients are online (0 = unlimited). +max clients 100000 + +# Largest control packet accepted from a client, in bytes. +max packet size 1048576 + +# Unacknowledged QoS 1 and 2 messages per client in either direction. +max inflight 32 + +# Messages held for an offline persistent session before dropping. +max queued 1000 + +# Highest QoS the broker grants; v5 clients are told via CONNACK. +max qos 2 + +# Keepalive ceiling in seconds (0 = whatever the client asks for). +max keepalive 300 + +# Longest a disconnected session is kept, in seconds (0 = unlimited). +max session expiry 86400 + +# Retained messages are enabled by default. +retain yes + +# Publish $SYS/broker/ statistics every 10 seconds (0 disables). +sys interval 10 + +# Keep persistent sessions and retained messages across restarts. +# The directory must exist and be owned by _mqttd. +persistence yes +persistence path "/var/db/mqttd" +persistence interval 60 + +# Require a username and password. +anonymous no + +# Publishes per second per client (0 = unlimited); users may override. +max rate 100 + +user "sensor" password "$2b$10$replace.with.output.of.encrypt.-b.10" { + max clients 500 + rate 10 +} +user "dashboard" password "$2b$10$replace.with.output.of.encrypt.-b.10" + +# Users not listed above are checked by this program (see mqttd.conf(5)). +#auth exec "/usr/local/libexec/mqttd-auth" + +# Five failed logins within a minute puts the address into a pf table: +# table persist +# block in quick from +#auth ban after 5 failures within 60 pf table "mqttd_ban" + +# Plain MQTT on every address, TLS on a dedicated interface. +listen on * port 1883 name "plain" + +# Browsers and cloud SDKs: MQTT over WebSocket, here behind relayd. +#listen on 127.0.0.1 websocket proxy port 8083 name "ws" + +# Local collectors with no network exposure at all. +#listen on "/var/run/mqttd-local.sock" name "local" + +listen on egress tls port 8883 name "secure" { + tls certificate "/etc/ssl/mqttd.crt" + tls key "/etc/ssl/private/mqttd.key" + # require client certificates signed by this CA + #tls client ca "/etc/ssl/mqttd-clients.crt" +} + +# Keep Sparkplug B birth certificates for late joining hosts. +sparkplug aware yes + +# Forward everything under sensors/ to a central broker and accept +# commands back; messages queue locally while it is unreachable. +#bridge "central" { +# remote "mqtt.example.com" tls +# user "site1" password "secret" +# version 5 +# topic "sensors/#" out qos 1 remote "site1/" +# topic "cmd/#" in qos 1 remote "site1/" +#} + +# Land sensor data in Snowflake; see mqttd.conf(5) for the table and key +# setup. Messages queue locally while Snowflake is unreachable. +#snowflake "warehouse" { +# account "myorg-myaccount" +# user "MQTT_INGEST" +# role "INGEST" +# warehouse "INGEST_WH" +# key "/etc/mqttd/snowflake.p8" +# table "IOT.RAW.MQTT_EVENTS" +# # insert every four hours to keep the warehouse asleep in between; +# # hold up to 500000 rows locally while waiting or during outages +# flush 14400 +# batch 1000 +# max queued 500000 +# topic "sensors/#" qos 1 +# topic "spBv1.0/#" qos 1 +#} + +# First match wins; without an acl block every topic is open. +# %u is the username and %c the client id of the connecting client. +acl { + allow user "sensor" publish "sensors/%u/#" + allow user "sensor" subscribe "cmd/%u/#" + allow user "dashboard" subscribe "sensors/#" + allow user "dashboard" publish "cmd/#" + allow client "gateway-01" all "site/1/#" + allow any subscribe "$SYS/#" +} blob - /dev/null blob + 98890b2a475ca6dbde03804b95c9b5a72bbb4424753d1eb421ab4a12a986f6cc (mode 644) --- /dev/null +++ mqttd/examples/etc/rc.d/mqttd @@ -0,0 +1,11 @@ +#!/bin/ksh + +daemon="/usr/local/sbin/mqttd" + +. /etc/rc.d/rc.subr + +rc_pre() { + ${daemon} -n ${daemon_flags} +} + +rc_cmd $1 blob - /dev/null blob + 63e8945b397b8a2fbdf0d17a213c9f088b89c7efa6b89697081a31fbdec4d054 (mode 644) --- /dev/null +++ mqttd/jwt.c @@ -0,0 +1,160 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * Snowflake key-pair JWT: RS256 over {"iss":"ACCOUNT.USER.SHA256:", + * "sub":"ACCOUNT.USER","iat":now,"exp":now+lifetime}, where fp is the + * base64 SHA-256 of the DER encoded public key. Signed in the parent, + * which is the only process that sees the private key. + */ + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "mqttd.h" + +static int b64url(const uint8_t *, size_t, char *, size_t); +static void upper(char *, const char *, size_t); + +/* base64 without padding, with the URL alphabet */ +static int +b64url(const uint8_t *in, size_t inlen, char *out, size_t outlen) +{ + char *p; + int n; + + if ((n = b64_ntop(in, inlen, out, outlen)) == -1) + return (-1); + for (p = out; *p != '\0'; p++) { + if (*p == '+') + *p = '-'; + else if (*p == '/') + *p = '_'; + } + while (n > 0 && out[n - 1] == '=') + out[--n] = '\0'; + return (n); +} + +static void +upper(char *dst, const char *src, size_t len) +{ + size_t i; + + for (i = 0; i + 1 < len && src[i] != '\0'; i++) + dst[i] = toupper((unsigned char)src[i]); + dst[i] = '\0'; +} + +int +jwt_sign(const struct snow_config *c, char *out, size_t outlen, + time_t *expires) +{ + BIO *bio = NULL; + EVP_PKEY *pkey = NULL; + EVP_MD_CTX *md = NULL; + uint8_t *der = NULL, *sig = NULL; + uint8_t digest[SHA256_DIGEST_LENGTH]; + char fp[64], account[MQTTD_SNOW_IDENT], user[MQTTD_SNOW_IDENT]; + char header[128], claims[1024], payload[1536]; + char hb64[256], pb64[2048], sb64[1024]; + size_t siglen; + time_t now = time(NULL); + int derlen, ret = -1; + + if (c->key == NULL || c->key_len == 0) + return (-1); + + if ((bio = BIO_new_mem_buf(c->key, c->key_len)) == NULL) + goto done; + if ((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)) == NULL) { + log_warnx("snowflake %s: cannot read private key %s (PKCS#8 " + "PEM, unencrypted)", c->name, c->key_file); + goto done; + } + + /* fingerprint of the public key as Snowflake computes it */ + if ((derlen = i2d_PUBKEY(pkey, &der)) <= 0) + goto done; + SHA256(der, derlen, digest); + if (b64_ntop(digest, sizeof(digest), fp, sizeof(fp)) == -1) + goto done; + + upper(account, c->account, sizeof(account)); + upper(user, c->user, sizeof(user)); + + snprintf(header, sizeof(header), "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"); + snprintf(claims, sizeof(claims), + "{\"iss\":\"%s.%s.SHA256:%s\",\"sub\":\"%s.%s\"," + "\"iat\":%lld,\"exp\":%lld}", account, user, fp, account, user, + (long long)now, (long long)(now + MQTTD_JWT_LIFETIME)); + + if (b64url((const uint8_t *)header, strlen(header), hb64, + sizeof(hb64)) == -1 || + b64url((const uint8_t *)claims, strlen(claims), pb64, + sizeof(pb64)) == -1) + goto done; + if ((size_t)snprintf(payload, sizeof(payload), "%s.%s", hb64, pb64) >= + sizeof(payload)) + goto done; + + if ((md = EVP_MD_CTX_new()) == NULL) + goto done; + if (EVP_DigestSignInit(md, NULL, EVP_sha256(), NULL, pkey) != 1) + goto done; + if (EVP_DigestSign(md, NULL, &siglen, (const uint8_t *)payload, + strlen(payload)) != 1) + goto done; + if ((sig = malloc(siglen)) == NULL) + goto done; + if (EVP_DigestSign(md, sig, &siglen, (const uint8_t *)payload, + strlen(payload)) != 1) + goto done; + if (b64url(sig, siglen, sb64, sizeof(sb64)) == -1) + goto done; + + if ((size_t)snprintf(out, outlen, "%s.%s", payload, sb64) >= outlen) + goto done; + *expires = now + MQTTD_JWT_LIFETIME; + ret = 0; +done: + free(sig); + OPENSSL_free(der); + EVP_MD_CTX_free(md); + EVP_PKEY_free(pkey); + BIO_free(bio); + return (ret); +} blob - /dev/null blob + a0ac49b18bf0a574afb7781a1aa80295a31878f46b3788fa26d6c5d989a50d90 (mode 644) --- /dev/null +++ mqttd/log.c @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2003, 2004 Henning Brauer + * + * 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 + +static int debug; +static int verbose; +const char *log_procname; + +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))); + +void +log_init(int n_debug, int facility) +{ + debug = n_debug; + verbose = n_debug; + log_procinit(getprogname()); + + if (!debug) + openlog(getprogname(), LOG_PID | LOG_NDELAY, facility); + + tzset(); +} + +void +log_procinit(const char *procname) +{ + if (procname != NULL) + log_procname = procname; +} + +void +log_setverbose(int v) +{ + verbose = v; +} + +int +log_getverbose(void) +{ + return (verbose); +} + +void +logit(int pri, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlog(pri, fmt, ap); + va_end(ap); +} + +void +vlog(int pri, const char *fmt, va_list ap) +{ + char *nfmt; + int saved_errno = errno; + + if (debug) { + /* best effort in out of mem situations */ + if (asprintf(&nfmt, "%s\n", fmt) == -1) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + } else { + vfprintf(stderr, nfmt, ap); + free(nfmt); + } + fflush(stderr); + } else + vsyslog(pri, fmt, ap); + + errno = saved_errno; +} + +void +log_warn(const char *emsg, ...) +{ + char *nfmt; + va_list ap; + int saved_errno = errno; + + /* best effort to even work in out of memory situations */ + if (emsg == NULL) + logit(LOG_CRIT, "%s", strerror(saved_errno)); + else { + va_start(ap, emsg); + + if (asprintf(&nfmt, "%s: %s", emsg, + strerror(saved_errno)) == -1) { + /* we tried it... */ + vlog(LOG_CRIT, emsg, ap); + logit(LOG_CRIT, "%s", strerror(saved_errno)); + } else { + vlog(LOG_CRIT, nfmt, ap); + free(nfmt); + } + va_end(ap); + } + + errno = saved_errno; +} + +void +log_warnx(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(LOG_CRIT, emsg, ap); + va_end(ap); +} + +void +log_info(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(LOG_INFO, emsg, ap); + va_end(ap); +} + +void +log_debug(const char *emsg, ...) +{ + va_list ap; + + if (verbose > 1) { + va_start(ap, emsg); + vlog(LOG_DEBUG, emsg, ap); + va_end(ap); + } +} + +static void +vfatalc(int code, const char *emsg, va_list ap) +{ + static char s[BUFSIZ]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(s, sizeof(s), emsg, ap); + sep = ": "; + } else { + s[0] = '\0'; + sep = ""; + } + if (code) + logit(LOG_CRIT, "%s: %s%s%s", + log_procname, s, sep, strerror(code)); + else + logit(LOG_CRIT, "%s%s%s", log_procname, sep, s); +} + +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatalc(errno, emsg, ap); + va_end(ap); + exit(1); +} + +void +fatalx(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatalc(0, emsg, ap); + va_end(ap); + exit(1); +} blob - /dev/null blob + 105205e5397f9bb320a7f1993ab1809a8b28a012d6f8fa53c0c93ef922061c47 (mode 644) --- /dev/null +++ mqttd/mqtt.c @@ -0,0 +1,1309 @@ +/* + * Copyright (c) 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 + * 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 "mqtt.h" + +/* which properties each packet from a client may carry */ +#define PROPS_CONNECT \ + (MQTT_PROP_BIT(MQTT_PROP_SESSION_EXPIRY) | \ + MQTT_PROP_BIT(MQTT_PROP_RECEIVE_MAXIMUM) | \ + MQTT_PROP_BIT(MQTT_PROP_MAXIMUM_PACKET_SIZE) | \ + MQTT_PROP_BIT(MQTT_PROP_TOPIC_ALIAS_MAXIMUM) | \ + MQTT_PROP_BIT(MQTT_PROP_REQUEST_RESPONSE_INFO) | \ + MQTT_PROP_BIT(MQTT_PROP_REQUEST_PROBLEM_INFO) | \ + MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY) | \ + MQTT_PROP_BIT(MQTT_PROP_AUTH_METHOD) | \ + MQTT_PROP_BIT(MQTT_PROP_AUTH_DATA)) +#define PROPS_WILL \ + (MQTT_PROP_BIT(MQTT_PROP_WILL_DELAY) | \ + MQTT_PROP_BIT(MQTT_PROP_PAYLOAD_FORMAT) | \ + MQTT_PROP_BIT(MQTT_PROP_MESSAGE_EXPIRY) | \ + MQTT_PROP_BIT(MQTT_PROP_CONTENT_TYPE) | \ + MQTT_PROP_BIT(MQTT_PROP_RESPONSE_TOPIC) | \ + MQTT_PROP_BIT(MQTT_PROP_CORRELATION_DATA) | \ + MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY)) +#define PROPS_PUBLISH \ + (MQTT_PROP_BIT(MQTT_PROP_PAYLOAD_FORMAT) | \ + MQTT_PROP_BIT(MQTT_PROP_MESSAGE_EXPIRY) | \ + MQTT_PROP_BIT(MQTT_PROP_TOPIC_ALIAS) | \ + MQTT_PROP_BIT(MQTT_PROP_RESPONSE_TOPIC) | \ + MQTT_PROP_BIT(MQTT_PROP_CORRELATION_DATA) | \ + MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY) | \ + MQTT_PROP_BIT(MQTT_PROP_SUBSCRIPTION_ID) | \ + MQTT_PROP_BIT(MQTT_PROP_CONTENT_TYPE)) +#define PROPS_ACK \ + (MQTT_PROP_BIT(MQTT_PROP_REASON_STRING) | \ + MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY)) +#define PROPS_SUBSCRIBE \ + (MQTT_PROP_BIT(MQTT_PROP_SUBSCRIPTION_ID) | \ + MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY)) +#define PROPS_UNSUBSCRIBE \ + (MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY)) +#define PROPS_DISCONNECT \ + (MQTT_PROP_BIT(MQTT_PROP_SESSION_EXPIRY) | \ + MQTT_PROP_BIT(MQTT_PROP_REASON_STRING) | \ + MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY) | \ + MQTT_PROP_BIT(MQTT_PROP_SERVER_REFERENCE)) +#define PROPS_AUTH \ + (MQTT_PROP_BIT(MQTT_PROP_AUTH_METHOD) | \ + MQTT_PROP_BIT(MQTT_PROP_AUTH_DATA) | \ + MQTT_PROP_BIT(MQTT_PROP_REASON_STRING) | \ + MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY)) + +struct rd { + const uint8_t *p; + size_t left; +}; + +static int rd_u8(struct rd *, uint8_t *); +static int rd_u16(struct rd *, uint16_t *); +static int rd_u32(struct rd *, uint32_t *); +static int rd_varint(struct rd *, uint32_t *); +static int rd_str(struct rd *, struct mqtt_str *); +static int parse_props(struct rd *, uint64_t, struct mqtt_props *); +static size_t varint_size(uint32_t); +static int pkt_finish(struct mqtt_buf *, uint8_t, uint8_t, + const struct mqtt_buf *); + +static int +rd_u8(struct rd *rd, uint8_t *v) +{ + if (rd->left < 1) + return (-1); + *v = rd->p[0]; + rd->p++; + rd->left--; + return (0); +} + +static int +rd_u16(struct rd *rd, uint16_t *v) +{ + if (rd->left < 2) + return (-1); + *v = (uint16_t)rd->p[0] << 8 | rd->p[1]; + rd->p += 2; + rd->left -= 2; + return (0); +} + +static int +rd_u32(struct rd *rd, uint32_t *v) +{ + if (rd->left < 4) + return (-1); + *v = (uint32_t)rd->p[0] << 24 | (uint32_t)rd->p[1] << 16 | + (uint32_t)rd->p[2] << 8 | rd->p[3]; + rd->p += 4; + rd->left -= 4; + return (0); +} + +static int +rd_varint(struct rd *rd, uint32_t *v) +{ + uint32_t mult = 1, val = 0; + size_t i; + + for (i = 0; i < 4; i++) { + if (rd->left < 1) + return (-1); + val += (rd->p[0] & 0x7f) * mult; + rd->p++; + rd->left--; + if ((rd->p[-1] & 0x80) == 0) { + *v = val; + return (0); + } + mult *= 128; + } + return (-1); +} + +static int +rd_str(struct rd *rd, struct mqtt_str *s) +{ + uint16_t len; + + if (rd_u16(rd, &len) == -1) + return (-1); + if (rd->left < len) + return (-1); + s->data = rd->p; + s->len = len; + rd->p += len; + rd->left -= len; + return (0); +} + +int +mqtt_parse_fixed(const uint8_t *buf, size_t len, struct mqtt_fixed *f) +{ + uint32_t mult = 1, val = 0; + size_t i; + + if (len < 2) + return (1); + + f->type = buf[0] >> 4; + f->flags = buf[0] & 0x0f; + if (f->type == 0) + return (-1); + + for (i = 1; ; i++) { + if (i > 4) + return (-1); + if (i >= len) + return (1); + val += (buf[i] & 0x7f) * mult; + if ((buf[i] & 0x80) == 0) + break; + mult *= 128; + } + + f->hdrlen = i + 1; + f->remaining = val; + return (0); +} + +static int +parse_props(struct rd *rd, uint64_t allowed, struct mqtt_props *props) +{ + struct rd pr; + uint32_t len, id; + int n; + + memset(props, 0, sizeof(*props)); + + if (rd_varint(rd, &len) == -1) + return (MQTT_RC_MALFORMED); + if (len > rd->left) + return (MQTT_RC_MALFORMED); + + pr.p = rd->p; + pr.left = len; + rd->p += len; + rd->left -= len; + + while (pr.left > 0) { + if (rd_varint(&pr, &id) == -1) + return (MQTT_RC_MALFORMED); + if (id == 0 || id > MQTT_PROP_MAX || + (allowed & MQTT_PROP_BIT(id)) == 0) + return (MQTT_RC_MALFORMED); + if (id != MQTT_PROP_USER_PROPERTY && + (props->present & MQTT_PROP_BIT(id))) + return (MQTT_RC_PROTOCOL_ERROR); + props->present |= MQTT_PROP_BIT(id); + + switch (id) { + case MQTT_PROP_PAYLOAD_FORMAT: + if (rd_u8(&pr, &props->payload_format) == -1) + return (MQTT_RC_MALFORMED); + if (props->payload_format > 1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_MESSAGE_EXPIRY: + if (rd_u32(&pr, &props->message_expiry) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_CONTENT_TYPE: + if (rd_str(&pr, &props->content_type) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_RESPONSE_TOPIC: + if (rd_str(&pr, &props->response_topic) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_CORRELATION_DATA: + if (rd_str(&pr, &props->correlation_data) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_SUBSCRIPTION_ID: + if (rd_varint(&pr, &props->subscription_id) == -1) + return (MQTT_RC_MALFORMED); + if (props->subscription_id == 0) + return (MQTT_RC_PROTOCOL_ERROR); + break; + case MQTT_PROP_SESSION_EXPIRY: + if (rd_u32(&pr, &props->session_expiry) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_ASSIGNED_CLIENT_ID: + if (rd_str(&pr, &props->assigned_client_id) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_SERVER_KEEP_ALIVE: + if (rd_u16(&pr, &props->server_keep_alive) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_AUTH_METHOD: + if (rd_str(&pr, &props->auth_method) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_AUTH_DATA: + if (rd_str(&pr, &props->auth_data) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_REQUEST_PROBLEM_INFO: + if (rd_u8(&pr, &props->request_problem_info) == -1) + return (MQTT_RC_MALFORMED); + if (props->request_problem_info > 1) + return (MQTT_RC_PROTOCOL_ERROR); + break; + case MQTT_PROP_WILL_DELAY: + if (rd_u32(&pr, &props->will_delay) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_REQUEST_RESPONSE_INFO: + if (rd_u8(&pr, &props->request_response_info) == -1) + return (MQTT_RC_MALFORMED); + if (props->request_response_info > 1) + return (MQTT_RC_PROTOCOL_ERROR); + break; + case MQTT_PROP_RESPONSE_INFO: + if (rd_str(&pr, &props->response_info) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_SERVER_REFERENCE: + if (rd_str(&pr, &props->server_reference) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_REASON_STRING: + if (rd_str(&pr, &props->reason_string) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_RECEIVE_MAXIMUM: + if (rd_u16(&pr, &props->receive_maximum) == -1) + return (MQTT_RC_MALFORMED); + if (props->receive_maximum == 0) + return (MQTT_RC_PROTOCOL_ERROR); + break; + case MQTT_PROP_TOPIC_ALIAS_MAXIMUM: + if (rd_u16(&pr, &props->topic_alias_maximum) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_TOPIC_ALIAS: + if (rd_u16(&pr, &props->topic_alias) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_MAXIMUM_QOS: + if (rd_u8(&pr, &props->maximum_qos) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_RETAIN_AVAILABLE: + if (rd_u8(&pr, &props->retain_available) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_USER_PROPERTY: + n = props->nuser_props; + if (n >= MQTT_MAX_USER_PROPS) + return (MQTT_RC_IMPL_SPECIFIC); + if (rd_str(&pr, &props->user_props[n][0]) == -1 || + rd_str(&pr, &props->user_props[n][1]) == -1) + return (MQTT_RC_MALFORMED); + props->nuser_props++; + break; + case MQTT_PROP_MAXIMUM_PACKET_SIZE: + if (rd_u32(&pr, &props->maximum_packet_size) == -1) + return (MQTT_RC_MALFORMED); + if (props->maximum_packet_size == 0) + return (MQTT_RC_PROTOCOL_ERROR); + break; + case MQTT_PROP_WILDCARD_SUB_AVAIL: + if (rd_u8(&pr, &props->wildcard_sub_available) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_SUB_ID_AVAIL: + if (rd_u8(&pr, &props->sub_id_available) == -1) + return (MQTT_RC_MALFORMED); + break; + case MQTT_PROP_SHARED_SUB_AVAIL: + if (rd_u8(&pr, &props->shared_sub_available) == -1) + return (MQTT_RC_MALFORMED); + break; + default: + return (MQTT_RC_MALFORMED); + } + } + + return (MQTT_RC_SUCCESS); +} + +int +mqtt_parse_connect(const uint8_t *body, size_t len, struct mqtt_connect *c) +{ + struct rd rd = { body, len }; + struct mqtt_str name; + uint8_t flags; + int rc; + + memset(c, 0, sizeof(*c)); + + if (rd_str(&rd, &name) == -1) + return (MQTT_RC_MALFORMED); + if (rd_u8(&rd, &c->version) == -1) + return (MQTT_RC_MALFORMED); + + if (name.len == 4 && memcmp(name.data, "MQTT", 4) == 0) { + if (c->version != MQTT_V311 && c->version != MQTT_V5) + return (MQTT_RC_UNSUPPORTED_VERSION); + } else if (name.len == 6 && memcmp(name.data, "MQIsdp", 6) == 0) { + if (c->version != MQTT_V31) + return (MQTT_RC_UNSUPPORTED_VERSION); + } else + return (MQTT_RC_UNSUPPORTED_VERSION); + + if (rd_u8(&rd, &flags) == -1) + return (MQTT_RC_MALFORMED); + if (flags & 0x01) + return (MQTT_RC_MALFORMED); + c->clean_start = (flags >> 1) & 1; + c->will = (flags >> 2) & 1; + c->will_qos = (flags >> 3) & 3; + c->will_retain = (flags >> 5) & 1; + c->has_password = (flags >> 6) & 1; + c->has_username = (flags >> 7) & 1; + + if (c->will_qos == 3) + return (MQTT_RC_MALFORMED); + if (c->will == 0 && (c->will_qos != 0 || c->will_retain != 0)) + return (MQTT_RC_MALFORMED); + if (c->version < MQTT_V5 && c->has_password && !c->has_username) + return (MQTT_RC_MALFORMED); + + if (rd_u16(&rd, &c->keepalive) == -1) + return (MQTT_RC_MALFORMED); + + if (c->version == MQTT_V5) { + if ((rc = parse_props(&rd, PROPS_CONNECT, &c->props)) != 0) + return (rc); + } + + if (rd_str(&rd, &c->client_id) == -1) + return (MQTT_RC_MALFORMED); + + if (c->will) { + if (c->version == MQTT_V5) { + if ((rc = parse_props(&rd, PROPS_WILL, + &c->will_props)) != 0) + return (rc); + } + if (rd_str(&rd, &c->will_topic) == -1) + return (MQTT_RC_MALFORMED); + if (rd_str(&rd, &c->will_payload) == -1) + return (MQTT_RC_MALFORMED); + } + + if (c->has_username && rd_str(&rd, &c->username) == -1) + return (MQTT_RC_MALFORMED); + if (c->has_password && rd_str(&rd, &c->password) == -1) + return (MQTT_RC_MALFORMED); + + if (rd.left != 0) + return (MQTT_RC_MALFORMED); + + return (MQTT_RC_SUCCESS); +} + +int +mqtt_parse_publish(const uint8_t *body, size_t len, uint8_t flags, + uint8_t version, struct mqtt_publish *p) +{ + struct rd rd = { body, len }; + int rc; + + memset(p, 0, sizeof(*p)); + + p->dup = (flags >> 3) & 1; + p->qos = (flags >> 1) & 3; + p->retain = flags & 1; + if (p->qos == 3) + return (MQTT_RC_MALFORMED); + if (p->qos == 0 && p->dup) + return (MQTT_RC_MALFORMED); + + if (rd_str(&rd, &p->topic) == -1) + return (MQTT_RC_MALFORMED); + + if (p->qos > 0) { + if (rd_u16(&rd, &p->packet_id) == -1) + return (MQTT_RC_MALFORMED); + if (p->packet_id == 0) + return (MQTT_RC_PROTOCOL_ERROR); + } + + if (version == MQTT_V5) { + if ((rc = parse_props(&rd, PROPS_PUBLISH, &p->props)) != 0) + return (rc); + } + + p->payload = rd.p; + p->payload_len = rd.left; + + return (MQTT_RC_SUCCESS); +} + +int +mqtt_parse_ack(const uint8_t *body, size_t len, uint8_t version, + uint8_t type, struct mqtt_ack *a) +{ + struct rd rd = { body, len }; + int rc; + + memset(a, 0, sizeof(*a)); + + if (rd_u16(&rd, &a->packet_id) == -1) + return (MQTT_RC_MALFORMED); + if (a->packet_id == 0) + return (MQTT_RC_PROTOCOL_ERROR); + + if (version == MQTT_V5 && rd.left > 0) { + if (rd_u8(&rd, &a->reason) == -1) + return (MQTT_RC_MALFORMED); + if (rd.left > 0) { + if ((rc = parse_props(&rd, PROPS_ACK, &a->props)) != 0) + return (rc); + } + } + + if (rd.left != 0) + return (MQTT_RC_MALFORMED); + + return (MQTT_RC_SUCCESS); +} + +int +mqtt_parse_subscribe(const uint8_t *body, size_t len, uint8_t version, + uint8_t type, struct mqtt_subscribe *s) +{ + struct rd rd = { body, len }; + int rc; + + memset(s, 0, sizeof(*s)); + s->version = version; + + if (rd_u16(&rd, &s->packet_id) == -1) + return (MQTT_RC_MALFORMED); + if (s->packet_id == 0) + return (MQTT_RC_PROTOCOL_ERROR); + + if (version == MQTT_V5) { + rc = parse_props(&rd, type == MQTT_SUBSCRIBE ? + PROPS_SUBSCRIBE : PROPS_UNSUBSCRIBE, &s->props); + if (rc != 0) + return (rc); + } + + if (rd.left == 0) + return (MQTT_RC_PROTOCOL_ERROR); + + s->cursor = rd.p; + s->left = rd.left; + + return (MQTT_RC_SUCCESS); +} + +int +mqtt_subscribe_next(struct mqtt_subscribe *s, struct mqtt_subopt *o) +{ + struct rd rd = { s->cursor, s->left }; + uint8_t opts; + + if (s->left == 0) + return (0); + + memset(o, 0, sizeof(*o)); + if (rd_str(&rd, &o->filter) == -1 || rd_u8(&rd, &opts) == -1) + return (-1); + + o->qos = opts & 0x03; + if (o->qos == 3) + return (-1); + if (s->version == MQTT_V5) { + o->no_local = (opts >> 2) & 1; + o->rap = (opts >> 3) & 1; + o->rh = (opts >> 4) & 3; + if (o->rh == 3 || (opts & 0xc0)) + return (-1); + } else if (opts & 0xfc) + return (-1); + + s->cursor = rd.p; + s->left = rd.left; + return (1); +} + +int +mqtt_unsubscribe_next(struct mqtt_subscribe *s, struct mqtt_str *f) +{ + struct rd rd = { s->cursor, s->left }; + + if (s->left == 0) + return (0); + if (rd_str(&rd, f) == -1) + return (-1); + + s->cursor = rd.p; + s->left = rd.left; + return (1); +} + +int +mqtt_parse_disconnect(const uint8_t *body, size_t len, uint8_t version, + struct mqtt_disconnect *d) +{ + struct rd rd = { body, len }; + int rc; + + memset(d, 0, sizeof(*d)); + + if (version == MQTT_V5 && rd.left > 0) { + if (rd_u8(&rd, &d->reason) == -1) + return (MQTT_RC_MALFORMED); + if (rd.left > 0) { + rc = parse_props(&rd, PROPS_DISCONNECT, &d->props); + if (rc != 0) + return (rc); + } + } + + if (rd.left != 0) + return (MQTT_RC_MALFORMED); + + return (MQTT_RC_SUCCESS); +} + +/* v5 only; an empty body means reason 0x00 and no properties */ +int +mqtt_parse_auth(const uint8_t *body, size_t len, struct mqtt_auth *a) +{ + struct rd rd = { body, len }; + int rc; + + memset(a, 0, sizeof(*a)); + if (rd.left == 0) + return (MQTT_RC_SUCCESS); + if (rd_u8(&rd, &a->reason) == -1) + return (MQTT_RC_MALFORMED); + if (rd.left > 0 && (rc = parse_props(&rd, PROPS_AUTH, &a->props)) != 0) + return (rc); + if (rd.left != 0) + return (MQTT_RC_MALFORMED); + if (!MQTT_PROP_ISSET(&a->props, MQTT_PROP_AUTH_METHOD)) + return (MQTT_RC_PROTOCOL_ERROR); + return (MQTT_RC_SUCCESS); +} + +int +mqtt_encode_auth(struct mqtt_buf *out, uint8_t reason, const char *method, + const uint8_t *data, size_t datalen) +{ + struct mqtt_buf body, props; + int ret = -1; + + mqtt_buf_init(&body); + mqtt_buf_init(&props); + if (mqtt_prop_str(&props, MQTT_PROP_AUTH_METHOD, method, + strlen(method)) == -1) + goto done; + if (datalen > 0 && mqtt_prop_str(&props, MQTT_PROP_AUTH_DATA, data, + datalen) == -1) + goto done; + if (mqtt_buf_u8(&body, reason) == -1 || + mqtt_buf_varint(&body, props.len) == -1 || + mqtt_buf_bytes(&body, props.data, props.len) == -1) + goto done; + ret = pkt_finish(out, MQTT_AUTH, 0, &body); +done: + mqtt_buf_free(&body); + mqtt_buf_free(&props); + return (ret); +} + +void +mqtt_buf_init(struct mqtt_buf *b) +{ + memset(b, 0, sizeof(*b)); +} + +void +mqtt_buf_free(struct mqtt_buf *b) +{ + free(b->data); + mqtt_buf_init(b); +} + +void +mqtt_buf_reset(struct mqtt_buf *b) +{ + b->len = 0; +} + +int +mqtt_buf_reserve(struct mqtt_buf *b, size_t n) +{ + uint8_t *p; + size_t cap; + + if (n > SIZE_MAX - b->len) + return (-1); + if (b->len + n <= b->cap) + return (0); + + cap = b->cap ? b->cap : 128; + while (cap < b->len + n) { + if (cap > SIZE_MAX / 2) + return (-1); + cap *= 2; + } + if ((p = realloc(b->data, cap)) == NULL) + return (-1); + b->data = p; + b->cap = cap; + return (0); +} + +int +mqtt_buf_u8(struct mqtt_buf *b, uint8_t v) +{ + if (mqtt_buf_reserve(b, 1) == -1) + return (-1); + b->data[b->len++] = v; + return (0); +} + +int +mqtt_buf_u16(struct mqtt_buf *b, uint16_t v) +{ + if (mqtt_buf_reserve(b, 2) == -1) + return (-1); + b->data[b->len++] = v >> 8; + b->data[b->len++] = v & 0xff; + return (0); +} + +int +mqtt_buf_u32(struct mqtt_buf *b, uint32_t v) +{ + if (mqtt_buf_reserve(b, 4) == -1) + return (-1); + b->data[b->len++] = v >> 24; + b->data[b->len++] = (v >> 16) & 0xff; + b->data[b->len++] = (v >> 8) & 0xff; + b->data[b->len++] = v & 0xff; + return (0); +} + +int +mqtt_buf_varint(struct mqtt_buf *b, uint32_t v) +{ + uint8_t byte; + + if (v > MQTT_MAX_REMAINING) + return (-1); + do { + byte = v % 128; + v /= 128; + if (v > 0) + byte |= 0x80; + if (mqtt_buf_u8(b, byte) == -1) + return (-1); + } while (v > 0); + return (0); +} + +int +mqtt_buf_bytes(struct mqtt_buf *b, const void *p, size_t n) +{ + if (n == 0) + return (0); + if (mqtt_buf_reserve(b, n) == -1) + return (-1); + memcpy(b->data + b->len, p, n); + b->len += n; + return (0); +} + +int +mqtt_buf_str(struct mqtt_buf *b, const void *p, uint16_t n) +{ + if (mqtt_buf_u16(b, n) == -1) + return (-1); + return (mqtt_buf_bytes(b, p, n)); +} + +int +mqtt_prop_u8(struct mqtt_buf *b, uint8_t id, uint8_t v) +{ + if (mqtt_buf_u8(b, id) == -1) + return (-1); + return (mqtt_buf_u8(b, v)); +} + +int +mqtt_prop_u16(struct mqtt_buf *b, uint8_t id, uint16_t v) +{ + if (mqtt_buf_u8(b, id) == -1) + return (-1); + return (mqtt_buf_u16(b, v)); +} + +int +mqtt_prop_u32(struct mqtt_buf *b, uint8_t id, uint32_t v) +{ + if (mqtt_buf_u8(b, id) == -1) + return (-1); + return (mqtt_buf_u32(b, v)); +} + +int +mqtt_prop_varint(struct mqtt_buf *b, uint8_t id, uint32_t v) +{ + if (mqtt_buf_u8(b, id) == -1) + return (-1); + return (mqtt_buf_varint(b, v)); +} + +int +mqtt_prop_str(struct mqtt_buf *b, uint8_t id, const void *p, uint16_t n) +{ + if (mqtt_buf_u8(b, id) == -1) + return (-1); + return (mqtt_buf_str(b, p, n)); +} + +int +mqtt_prop_pair(struct mqtt_buf *b, const void *k, uint16_t klen, + const void *v, uint16_t vlen) +{ + if (mqtt_buf_u8(b, MQTT_PROP_USER_PROPERTY) == -1) + return (-1); + if (mqtt_buf_str(b, k, klen) == -1) + return (-1); + return (mqtt_buf_str(b, v, vlen)); +} + +static size_t +varint_size(uint32_t v) +{ + if (v < 128) + return (1); + if (v < 16384) + return (2); + if (v < 2097152) + return (3); + return (4); +} + +static int +pkt_finish(struct mqtt_buf *out, uint8_t type, uint8_t flags, + const struct mqtt_buf *body) +{ + size_t blen = body != NULL ? body->len : 0; + + if (blen > MQTT_MAX_REMAINING) + return (-1); + + mqtt_buf_reset(out); + if (mqtt_buf_reserve(out, 5 + blen) == -1) + return (-1); + if (mqtt_buf_u8(out, type << 4 | (flags & 0x0f)) == -1) + return (-1); + if (mqtt_buf_varint(out, blen) == -1) + return (-1); + if (blen > 0 && mqtt_buf_bytes(out, body->data, blen) == -1) + return (-1); + return (0); +} + +int +mqtt_encode_connack(struct mqtt_buf *out, uint8_t version, + int session_present, uint8_t reason, const struct mqtt_buf *props) +{ + struct mqtt_buf body; + size_t plen = props != NULL ? props->len : 0; + int ret = -1; + + mqtt_buf_init(&body); + + if (version == MQTT_V5) { + if (mqtt_buf_u8(&body, session_present ? 1 : 0) == -1 || + mqtt_buf_u8(&body, reason) == -1 || + mqtt_buf_varint(&body, plen) == -1) + goto done; + if (plen > 0 && mqtt_buf_bytes(&body, props->data, plen) == -1) + goto done; + } else { + if (reason != MQTT_RC_SUCCESS) + session_present = 0; + if (mqtt_buf_u8(&body, session_present ? 1 : 0) == -1 || + mqtt_buf_u8(&body, mqtt_rc_to_v3(reason)) == -1) + goto done; + } + + ret = pkt_finish(out, MQTT_CONNACK, 0, &body); +done: + mqtt_buf_free(&body); + return (ret); +} + +int +mqtt_encode_publish(struct mqtt_buf *out, uint8_t version, uint8_t dup, + uint8_t qos, uint8_t retain, const void *topic, uint16_t topic_len, + uint16_t packet_id, const struct mqtt_buf *props, const void *payload, + uint32_t payload_len) +{ + size_t plen = props != NULL ? props->len : 0; + size_t remaining; + uint8_t flags; + + remaining = 2 + topic_len + payload_len; + if (qos > 0) + remaining += 2; + if (version == MQTT_V5) + remaining += varint_size(plen) + plen; + if (remaining > MQTT_MAX_REMAINING) + return (-1); + + flags = (qos > 0 && dup ? 0x08 : 0) | (qos << 1) | (retain ? 1 : 0); + + mqtt_buf_reset(out); + if (mqtt_buf_reserve(out, 5 + remaining) == -1) + return (-1); + if (mqtt_buf_u8(out, MQTT_PUBLISH << 4 | flags) == -1 || + mqtt_buf_varint(out, remaining) == -1 || + mqtt_buf_str(out, topic, topic_len) == -1) + return (-1); + if (qos > 0 && mqtt_buf_u16(out, packet_id) == -1) + return (-1); + if (version == MQTT_V5) { + if (mqtt_buf_varint(out, plen) == -1) + return (-1); + if (plen > 0 && mqtt_buf_bytes(out, props->data, plen) == -1) + return (-1); + } + return (mqtt_buf_bytes(out, payload, payload_len)); +} + +int +mqtt_encode_ack(struct mqtt_buf *out, uint8_t type, uint8_t version, + uint16_t packet_id, uint8_t reason) +{ + struct mqtt_buf body; + int ret = -1; + + mqtt_buf_init(&body); + if (mqtt_buf_u16(&body, packet_id) == -1) + goto done; + if (version == MQTT_V5 && reason != MQTT_RC_SUCCESS && + mqtt_buf_u8(&body, reason) == -1) + goto done; + + ret = pkt_finish(out, type, type == MQTT_PUBREL ? 0x02 : 0, &body); +done: + mqtt_buf_free(&body); + return (ret); +} + +int +mqtt_encode_suback(struct mqtt_buf *out, uint8_t version, + uint16_t packet_id, const uint8_t *codes, size_t n) +{ + struct mqtt_buf body; + size_t i; + int ret = -1; + + mqtt_buf_init(&body); + if (mqtt_buf_u16(&body, packet_id) == -1) + goto done; + if (version == MQTT_V5 && mqtt_buf_varint(&body, 0) == -1) + goto done; + for (i = 0; i < n; i++) { + if (version == MQTT_V5) { + if (mqtt_buf_u8(&body, codes[i]) == -1) + goto done; + } else if (mqtt_buf_u8(&body, codes[i] >= 0x80 ? 0x80 : + codes[i]) == -1) + goto done; + } + + ret = pkt_finish(out, MQTT_SUBACK, 0, &body); +done: + mqtt_buf_free(&body); + return (ret); +} + +int +mqtt_encode_unsuback(struct mqtt_buf *out, uint8_t version, + uint16_t packet_id, const uint8_t *codes, size_t n) +{ + struct mqtt_buf body; + int ret = -1; + + mqtt_buf_init(&body); + if (mqtt_buf_u16(&body, packet_id) == -1) + goto done; + if (version == MQTT_V5) { + if (mqtt_buf_varint(&body, 0) == -1 || + mqtt_buf_bytes(&body, codes, n) == -1) + goto done; + } + + ret = pkt_finish(out, MQTT_UNSUBACK, 0, &body); +done: + mqtt_buf_free(&body); + return (ret); +} + +int +mqtt_encode_pingresp(struct mqtt_buf *out) +{ + return (pkt_finish(out, MQTT_PINGRESP, 0, NULL)); +} + +int +mqtt_encode_pingreq(struct mqtt_buf *out) +{ + return (pkt_finish(out, MQTT_PINGREQ, 0, NULL)); +} + +/* only the properties a bridge cares about: everything else is optional */ +#define PROPS_CONNACK \ + (MQTT_PROP_BIT(MQTT_PROP_SESSION_EXPIRY) | \ + MQTT_PROP_BIT(MQTT_PROP_RECEIVE_MAXIMUM) | \ + MQTT_PROP_BIT(MQTT_PROP_MAXIMUM_QOS) | \ + MQTT_PROP_BIT(MQTT_PROP_RETAIN_AVAILABLE) | \ + MQTT_PROP_BIT(MQTT_PROP_MAXIMUM_PACKET_SIZE) | \ + MQTT_PROP_BIT(MQTT_PROP_ASSIGNED_CLIENT_ID) | \ + MQTT_PROP_BIT(MQTT_PROP_TOPIC_ALIAS_MAXIMUM) | \ + MQTT_PROP_BIT(MQTT_PROP_REASON_STRING) | \ + MQTT_PROP_BIT(MQTT_PROP_USER_PROPERTY) | \ + MQTT_PROP_BIT(MQTT_PROP_WILDCARD_SUB_AVAIL) | \ + MQTT_PROP_BIT(MQTT_PROP_SUB_ID_AVAIL) | \ + MQTT_PROP_BIT(MQTT_PROP_SHARED_SUB_AVAIL) | \ + MQTT_PROP_BIT(MQTT_PROP_SERVER_KEEP_ALIVE) | \ + MQTT_PROP_BIT(MQTT_PROP_RESPONSE_INFO) | \ + MQTT_PROP_BIT(MQTT_PROP_SERVER_REFERENCE) | \ + MQTT_PROP_BIT(MQTT_PROP_AUTH_METHOD) | \ + MQTT_PROP_BIT(MQTT_PROP_AUTH_DATA)) + +int +mqtt_parse_connack(const uint8_t *body, size_t len, uint8_t version, + struct mqtt_connack *ca) +{ + struct rd rd = { body, len }; + uint8_t flags; + int rc; + + memset(ca, 0, sizeof(*ca)); + if (rd_u8(&rd, &flags) == -1 || rd_u8(&rd, &ca->reason) == -1) + return (MQTT_RC_MALFORMED); + if (flags & 0xfe) + return (MQTT_RC_MALFORMED); + ca->session_present = flags & 1; + if (version == MQTT_V5) { + if ((rc = parse_props(&rd, PROPS_CONNACK, &ca->props)) != 0) + return (rc); + } + if (rd.left != 0) + return (MQTT_RC_MALFORMED); + return (MQTT_RC_SUCCESS); +} + +int +mqtt_parse_suback(const uint8_t *body, size_t len, uint8_t version, + uint16_t *pid, const uint8_t **codes, size_t *ncodes) +{ + struct rd rd = { body, len }; + struct mqtt_props props; + int rc; + + if (rd_u16(&rd, pid) == -1) + return (MQTT_RC_MALFORMED); + if (version == MQTT_V5) { + if ((rc = parse_props(&rd, PROPS_ACK, &props)) != 0) + return (rc); + } + *codes = rd.p; + *ncodes = rd.left; + return (MQTT_RC_SUCCESS); +} + +/* a clean session every time; the local bridge session holds the queue */ +int +mqtt_encode_connect(struct mqtt_buf *out, uint8_t version, + const char *client_id, const char *username, const char *password, + uint16_t keepalive) +{ + struct mqtt_buf body; + uint8_t flags = 0x02; + int ret = -1; + + if (username != NULL && username[0] != '\0') { + flags |= 0x80; + if (password != NULL && password[0] != '\0') + flags |= 0x40; + } + + mqtt_buf_init(&body); + if (version == MQTT_V31) { + if (mqtt_buf_str(&body, "MQIsdp", 6) == -1) + goto done; + } else if (mqtt_buf_str(&body, "MQTT", 4) == -1) + goto done; + if (mqtt_buf_u8(&body, version) == -1 || + mqtt_buf_u8(&body, flags) == -1 || + mqtt_buf_u16(&body, keepalive) == -1) + goto done; + if (version == MQTT_V5 && mqtt_buf_varint(&body, 0) == -1) + goto done; + if (mqtt_buf_str(&body, client_id, strlen(client_id)) == -1) + goto done; + if ((flags & 0x80) && + mqtt_buf_str(&body, username, strlen(username)) == -1) + goto done; + if ((flags & 0x40) && + mqtt_buf_str(&body, password, strlen(password)) == -1) + goto done; + + ret = pkt_finish(out, MQTT_CONNECT, 0, &body); +done: + mqtt_buf_free(&body); + return (ret); +} + +int +mqtt_encode_subscribe(struct mqtt_buf *out, uint8_t version, uint16_t pid, + const struct mqtt_subopt *opts, size_t n) +{ + struct mqtt_buf body; + size_t i; + uint8_t o; + int ret = -1; + + mqtt_buf_init(&body); + if (mqtt_buf_u16(&body, pid) == -1) + goto done; + if (version == MQTT_V5 && mqtt_buf_varint(&body, 0) == -1) + goto done; + for (i = 0; i < n; i++) { + o = opts[i].qos & 0x03; + if (version == MQTT_V5) + o |= (opts[i].no_local ? 0x04 : 0) | + (opts[i].rap ? 0x08 : 0) | + ((opts[i].rh & 0x03) << 4); + if (mqtt_buf_str(&body, opts[i].filter.data, + opts[i].filter.len) == -1 || mqtt_buf_u8(&body, o) == -1) + goto done; + } + + ret = pkt_finish(out, MQTT_SUBSCRIBE, 0x02, &body); +done: + mqtt_buf_free(&body); + return (ret); +} + +int +mqtt_encode_disconnect(struct mqtt_buf *out, uint8_t version, uint8_t reason, + const struct mqtt_buf *props) +{ + struct mqtt_buf body; + size_t plen = props != NULL ? props->len : 0; + int ret = -1; + + /* pre-v5 servers close the connection without a DISCONNECT */ + if (version != MQTT_V5) { + mqtt_buf_reset(out); + return (0); + } + + mqtt_buf_init(&body); + if (reason != MQTT_RC_SUCCESS || plen > 0) { + if (mqtt_buf_u8(&body, reason) == -1) + goto done; + if (plen > 0) { + if (mqtt_buf_varint(&body, plen) == -1 || + mqtt_buf_bytes(&body, props->data, plen) == -1) + goto done; + } + } + + ret = pkt_finish(out, MQTT_DISCONNECT, 0, &body); +done: + mqtt_buf_free(&body); + return (ret); +} + +int +mqtt_utf8_valid(const uint8_t *s, size_t len) +{ + size_t i = 0, n, j; + uint32_t cp, min; + + while (i < len) { + if (s[i] < 0x80) { + if (s[i] == 0) + return (0); + i++; + continue; + } + if ((s[i] & 0xe0) == 0xc0) { + n = 1; + cp = s[i] & 0x1f; + min = 0x80; + } else if ((s[i] & 0xf0) == 0xe0) { + n = 2; + cp = s[i] & 0x0f; + min = 0x800; + } else if ((s[i] & 0xf8) == 0xf0) { + n = 3; + cp = s[i] & 0x07; + min = 0x10000; + } else + return (0); + if (len - i - 1 < n) + return (0); + for (j = 1; j <= n; j++) { + if ((s[i + j] & 0xc0) != 0x80) + return (0); + cp = cp << 6 | (s[i + j] & 0x3f); + } + if (cp < min || cp > 0x10ffff || + (cp >= 0xd800 && cp <= 0xdfff)) + return (0); + i += n + 1; + } + return (1); +} + +int +mqtt_topic_valid(const uint8_t *s, size_t len) +{ + if (len == 0) + return (0); + if (memchr(s, '+', len) != NULL || memchr(s, '#', len) != NULL) + return (0); + return (mqtt_utf8_valid(s, len)); +} + +int +mqtt_filter_valid(const uint8_t *s, size_t len) +{ + size_t i, start = 0, lvl; + + if (len == 0) + return (0); + if (!mqtt_utf8_valid(s, len)) + return (0); + + for (i = 0; i <= len; i++) { + if (i < len && s[i] != '/') + continue; + lvl = i - start; + if (memchr(s + start, '#', lvl) != NULL) { + if (lvl != 1 || i != len) + return (0); + } + if (memchr(s + start, '+', lvl) != NULL && lvl != 1) + return (0); + start = i + 1; + } + return (1); +} + +int +mqtt_filter_is_shared(const uint8_t *s, size_t len) +{ + return (len >= 7 && memcmp(s, "$share/", 7) == 0); +} + +/* + * Split "$share//". Returns 1 and fills in both parts + * for a well formed shared filter, 0 for an ordinary filter and -1 for + * a shared filter with an empty or wildcarded group or a bad filter. + */ +int +mqtt_filter_shared_parse(const uint8_t *s, size_t len, struct mqtt_str *group, + struct mqtt_str *filter) +{ + const uint8_t *slash; + + if (!mqtt_filter_is_shared(s, len)) + return (0); + s += 7; + len -= 7; + + if ((slash = memchr(s, '/', len)) == NULL || slash == s) + return (-1); + group->data = s; + group->len = slash - s; + if (memchr(group->data, '+', group->len) != NULL || + memchr(group->data, '#', group->len) != NULL) + return (-1); + + filter->data = slash + 1; + filter->len = len - group->len - 1; + if (filter->len == 0 || !mqtt_filter_valid(filter->data, filter->len)) + return (-1); + return (1); +} + +uint8_t +mqtt_rc_to_v3(uint8_t reason) +{ + switch (reason) { + case MQTT_RC_SUCCESS: + return (MQTT_V3_ACCEPTED); + case MQTT_RC_UNSUPPORTED_VERSION: + return (MQTT_V3_BAD_VERSION); + case MQTT_RC_CLIENT_ID_INVALID: + return (MQTT_V3_ID_REJECTED); + case MQTT_RC_SERVER_UNAVAILABLE: + case MQTT_RC_SERVER_BUSY: + case MQTT_RC_SERVER_SHUTDOWN: + case MQTT_RC_PACKET_TOO_LARGE: + case MQTT_RC_QUOTA_EXCEEDED: + case MQTT_RC_CONNECTION_RATE: + return (MQTT_V3_UNAVAILABLE); + case MQTT_RC_BAD_CREDENTIALS: + case MQTT_RC_BAD_AUTH_METHOD: + return (MQTT_V3_BAD_CREDENTIALS); + default: + return (MQTT_V3_NOT_AUTHORIZED); + } +} + +const char * +mqtt_type_name(uint8_t type) +{ + static const char *names[] = { + "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", + "PUBREC", "PUBREL", "PUBCOMP", "SUBSCRIBE", "SUBACK", + "UNSUBSCRIBE", "UNSUBACK", "PINGREQ", "PINGRESP", + "DISCONNECT", "AUTH" + }; + + if (type > MQTT_AUTH) + return ("UNKNOWN"); + return (names[type]); +} blob - /dev/null blob + 944b6bc02f8e1a25486f9e84cc34a9d4154a63e6e54c80f1d0779c5b1ae2b2f3 (mode 644) --- /dev/null +++ mqttd/mqtt.h @@ -0,0 +1,335 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * MQTT 3.1, 3.1.1 and 5.0 control packet codec. Decoders point into the + * caller's packet buffer and never allocate; encoders append to a growable + * mqtt_buf. + */ + +#ifndef MQTT_H +#define MQTT_H + +#include +#include +#include + +#define MQTT_V31 3 +#define MQTT_V311 4 +#define MQTT_V5 5 + +#define MQTT_MAX_REMAINING 268435455U +#define MQTT_MAX_USER_PROPS 32 + +enum mqtt_pkt_type { + MQTT_CONNECT = 1, + MQTT_CONNACK, + MQTT_PUBLISH, + MQTT_PUBACK, + MQTT_PUBREC, + MQTT_PUBREL, + MQTT_PUBCOMP, + MQTT_SUBSCRIBE, + MQTT_SUBACK, + MQTT_UNSUBSCRIBE, + MQTT_UNSUBACK, + MQTT_PINGREQ, + MQTT_PINGRESP, + MQTT_DISCONNECT, + MQTT_AUTH +}; + +/* property identifiers */ +#define MQTT_PROP_PAYLOAD_FORMAT 0x01 +#define MQTT_PROP_MESSAGE_EXPIRY 0x02 +#define MQTT_PROP_CONTENT_TYPE 0x03 +#define MQTT_PROP_RESPONSE_TOPIC 0x08 +#define MQTT_PROP_CORRELATION_DATA 0x09 +#define MQTT_PROP_SUBSCRIPTION_ID 0x0b +#define MQTT_PROP_SESSION_EXPIRY 0x11 +#define MQTT_PROP_ASSIGNED_CLIENT_ID 0x12 +#define MQTT_PROP_SERVER_KEEP_ALIVE 0x13 +#define MQTT_PROP_AUTH_METHOD 0x15 +#define MQTT_PROP_AUTH_DATA 0x16 +#define MQTT_PROP_REQUEST_PROBLEM_INFO 0x17 +#define MQTT_PROP_WILL_DELAY 0x18 +#define MQTT_PROP_REQUEST_RESPONSE_INFO 0x19 +#define MQTT_PROP_RESPONSE_INFO 0x1a +#define MQTT_PROP_SERVER_REFERENCE 0x1c +#define MQTT_PROP_REASON_STRING 0x1f +#define MQTT_PROP_RECEIVE_MAXIMUM 0x21 +#define MQTT_PROP_TOPIC_ALIAS_MAXIMUM 0x22 +#define MQTT_PROP_TOPIC_ALIAS 0x23 +#define MQTT_PROP_MAXIMUM_QOS 0x24 +#define MQTT_PROP_RETAIN_AVAILABLE 0x25 +#define MQTT_PROP_USER_PROPERTY 0x26 +#define MQTT_PROP_MAXIMUM_PACKET_SIZE 0x27 +#define MQTT_PROP_WILDCARD_SUB_AVAIL 0x28 +#define MQTT_PROP_SUB_ID_AVAIL 0x29 +#define MQTT_PROP_SHARED_SUB_AVAIL 0x2a +#define MQTT_PROP_MAX 0x2a + +#define MQTT_PROP_BIT(id) (1ULL << (id)) + +/* reason codes */ +#define MQTT_RC_SUCCESS 0x00 +#define MQTT_RC_GRANTED_QOS1 0x01 +#define MQTT_RC_GRANTED_QOS2 0x02 +#define MQTT_RC_DISCONNECT_WILL 0x04 +#define MQTT_RC_NO_MATCHING_SUBS 0x10 +#define MQTT_RC_NO_SUBSCRIPTION 0x11 +#define MQTT_RC_UNSPECIFIED 0x80 +#define MQTT_RC_MALFORMED 0x81 +#define MQTT_RC_PROTOCOL_ERROR 0x82 +#define MQTT_RC_IMPL_SPECIFIC 0x83 +#define MQTT_RC_UNSUPPORTED_VERSION 0x84 +#define MQTT_RC_CLIENT_ID_INVALID 0x85 +#define MQTT_RC_BAD_CREDENTIALS 0x86 +#define MQTT_RC_NOT_AUTHORIZED 0x87 +#define MQTT_RC_SERVER_UNAVAILABLE 0x88 +#define MQTT_RC_SERVER_BUSY 0x89 +#define MQTT_RC_BANNED 0x8a +#define MQTT_RC_SERVER_SHUTDOWN 0x8b +#define MQTT_RC_BAD_AUTH_METHOD 0x8c +#define MQTT_RC_KEEP_ALIVE_TIMEOUT 0x8d +#define MQTT_RC_SESSION_TAKEN_OVER 0x8e +#define MQTT_RC_TOPIC_FILTER_INVALID 0x8f +#define MQTT_RC_TOPIC_NAME_INVALID 0x90 +#define MQTT_RC_PACKET_ID_IN_USE 0x91 +#define MQTT_RC_PACKET_ID_NOT_FOUND 0x92 +#define MQTT_RC_RECEIVE_MAX_EXCEEDED 0x93 +#define MQTT_RC_TOPIC_ALIAS_INVALID 0x94 +#define MQTT_RC_PACKET_TOO_LARGE 0x95 +#define MQTT_RC_MESSAGE_RATE_TOO_HIGH 0x96 +#define MQTT_RC_QUOTA_EXCEEDED 0x97 +#define MQTT_RC_ADMINISTRATIVE 0x98 +#define MQTT_RC_PAYLOAD_FORMAT_INVALID 0x99 +#define MQTT_RC_RETAIN_NOT_SUPPORTED 0x9a +#define MQTT_RC_QOS_NOT_SUPPORTED 0x9b +#define MQTT_RC_USE_ANOTHER_SERVER 0x9c +#define MQTT_RC_SERVER_MOVED 0x9d +#define MQTT_RC_SHARED_SUB_UNSUPPORTED 0x9e +#define MQTT_RC_CONNECTION_RATE 0x9f +#define MQTT_RC_MAX_CONNECT_TIME 0xa0 +#define MQTT_RC_SUB_ID_UNSUPPORTED 0xa1 +#define MQTT_RC_WILDCARD_UNSUPPORTED 0xa2 + +/* v3 CONNACK return codes */ +#define MQTT_V3_ACCEPTED 0 +#define MQTT_V3_BAD_VERSION 1 +#define MQTT_V3_ID_REJECTED 2 +#define MQTT_V3_UNAVAILABLE 3 +#define MQTT_V3_BAD_CREDENTIALS 4 +#define MQTT_V3_NOT_AUTHORIZED 5 + +struct mqtt_str { + const uint8_t *data; + uint16_t len; +}; + +struct mqtt_props { + uint64_t present; + uint8_t payload_format; + uint32_t message_expiry; + struct mqtt_str content_type; + struct mqtt_str response_topic; + struct mqtt_str correlation_data; + uint32_t subscription_id; + uint32_t session_expiry; + struct mqtt_str assigned_client_id; + uint16_t server_keep_alive; + struct mqtt_str auth_method; + struct mqtt_str auth_data; + uint8_t request_problem_info; + uint32_t will_delay; + uint8_t request_response_info; + struct mqtt_str response_info; + struct mqtt_str server_reference; + struct mqtt_str reason_string; + uint16_t receive_maximum; + uint16_t topic_alias_maximum; + uint16_t topic_alias; + uint8_t maximum_qos; + uint8_t retain_available; + uint32_t maximum_packet_size; + uint8_t wildcard_sub_available; + uint8_t sub_id_available; + uint8_t shared_sub_available; + struct mqtt_str user_props[MQTT_MAX_USER_PROPS][2]; + int nuser_props; +}; + +#define MQTT_PROP_ISSET(p, id) (((p)->present & MQTT_PROP_BIT(id)) != 0) + +struct mqtt_fixed { + uint8_t type; + uint8_t flags; + uint8_t hdrlen; + uint32_t remaining; +}; + +struct mqtt_connect { + uint8_t version; + uint8_t clean_start; + uint8_t will; + uint8_t will_qos; + uint8_t will_retain; + uint8_t has_username; + uint8_t has_password; + uint16_t keepalive; + struct mqtt_str client_id; + struct mqtt_str will_topic; + struct mqtt_str will_payload; + struct mqtt_str username; + struct mqtt_str password; + struct mqtt_props props; + struct mqtt_props will_props; +}; + +struct mqtt_publish { + uint8_t dup; + uint8_t qos; + uint8_t retain; + uint16_t packet_id; + struct mqtt_str topic; + const uint8_t *payload; + uint32_t payload_len; + struct mqtt_props props; +}; + +struct mqtt_ack { + uint16_t packet_id; + uint8_t reason; + struct mqtt_props props; +}; + +/* SUBSCRIBE and UNSUBSCRIBE share the header; entries are read by cursor */ +struct mqtt_subscribe { + uint16_t packet_id; + struct mqtt_props props; + const uint8_t *cursor; + size_t left; + uint8_t version; +}; + +struct mqtt_subopt { + struct mqtt_str filter; + uint8_t qos; + uint8_t no_local; + uint8_t rap; + uint8_t rh; +}; + +struct mqtt_disconnect { + uint8_t reason; + struct mqtt_props props; +}; + +struct mqtt_auth { + uint8_t reason; + struct mqtt_props props; +}; + +struct mqtt_buf { + uint8_t *data; + size_t len; + size_t cap; +}; + +/* decoding: 0 complete, 1 need more bytes, -1 malformed */ +int mqtt_parse_fixed(const uint8_t *, size_t, struct mqtt_fixed *); + +/* decoding: return a reason code, MQTT_RC_SUCCESS when parsed */ +int mqtt_parse_connect(const uint8_t *, size_t, struct mqtt_connect *); +int mqtt_parse_publish(const uint8_t *, size_t, uint8_t, uint8_t, + struct mqtt_publish *); +int mqtt_parse_ack(const uint8_t *, size_t, uint8_t, uint8_t, + struct mqtt_ack *); +int mqtt_parse_subscribe(const uint8_t *, size_t, uint8_t, uint8_t, + struct mqtt_subscribe *); +int mqtt_subscribe_next(struct mqtt_subscribe *, struct mqtt_subopt *); +int mqtt_unsubscribe_next(struct mqtt_subscribe *, struct mqtt_str *); +int mqtt_parse_disconnect(const uint8_t *, size_t, uint8_t, + struct mqtt_disconnect *); +int mqtt_parse_auth(const uint8_t *, size_t, struct mqtt_auth *); +int mqtt_encode_auth(struct mqtt_buf *, uint8_t, const char *, + const uint8_t *, size_t); + +/* buffers */ +void mqtt_buf_init(struct mqtt_buf *); +void mqtt_buf_free(struct mqtt_buf *); +void mqtt_buf_reset(struct mqtt_buf *); +int mqtt_buf_reserve(struct mqtt_buf *, size_t); +int mqtt_buf_u8(struct mqtt_buf *, uint8_t); +int mqtt_buf_u16(struct mqtt_buf *, uint16_t); +int mqtt_buf_u32(struct mqtt_buf *, uint32_t); +int mqtt_buf_varint(struct mqtt_buf *, uint32_t); +int mqtt_buf_bytes(struct mqtt_buf *, const void *, size_t); +int mqtt_buf_str(struct mqtt_buf *, const void *, uint16_t); + +/* property builders append id plus value */ +int mqtt_prop_u8(struct mqtt_buf *, uint8_t, uint8_t); +int mqtt_prop_u16(struct mqtt_buf *, uint8_t, uint16_t); +int mqtt_prop_u32(struct mqtt_buf *, uint8_t, uint32_t); +int mqtt_prop_varint(struct mqtt_buf *, uint8_t, uint32_t); +int mqtt_prop_str(struct mqtt_buf *, uint8_t, const void *, uint16_t); +int mqtt_prop_pair(struct mqtt_buf *, const void *, uint16_t, + const void *, uint16_t); + +/* client side, used by bridges */ +struct mqtt_connack { + uint8_t session_present; + uint8_t reason; + struct mqtt_props props; +}; +int mqtt_parse_connack(const uint8_t *, size_t, uint8_t, + struct mqtt_connack *); +int mqtt_parse_suback(const uint8_t *, size_t, uint8_t, uint16_t *, + const uint8_t **, size_t *); +int mqtt_encode_connect(struct mqtt_buf *, uint8_t, const char *, + const char *, const char *, uint16_t); +int mqtt_encode_subscribe(struct mqtt_buf *, uint8_t, uint16_t, + const struct mqtt_subopt *, size_t); +int mqtt_encode_pingreq(struct mqtt_buf *); + +/* encoders replace the contents of the output buffer */ +int mqtt_encode_connack(struct mqtt_buf *, uint8_t, int, uint8_t, + const struct mqtt_buf *); +int mqtt_encode_publish(struct mqtt_buf *, uint8_t, uint8_t, uint8_t, + uint8_t, const void *, uint16_t, uint16_t, const struct mqtt_buf *, + const void *, uint32_t); +int mqtt_encode_ack(struct mqtt_buf *, uint8_t, uint8_t, uint16_t, + uint8_t); +int mqtt_encode_suback(struct mqtt_buf *, uint8_t, uint16_t, + const uint8_t *, size_t); +int mqtt_encode_unsuback(struct mqtt_buf *, uint8_t, uint16_t, + const uint8_t *, size_t); +int mqtt_encode_pingresp(struct mqtt_buf *); +int mqtt_encode_disconnect(struct mqtt_buf *, uint8_t, uint8_t, + const struct mqtt_buf *); + +/* validation */ +int mqtt_utf8_valid(const uint8_t *, size_t); +int mqtt_topic_valid(const uint8_t *, size_t); +int mqtt_filter_valid(const uint8_t *, size_t); +int mqtt_filter_is_shared(const uint8_t *, size_t); +int mqtt_filter_shared_parse(const uint8_t *, size_t, struct mqtt_str *, + struct mqtt_str *); +uint8_t mqtt_rc_to_v3(uint8_t); +const char + *mqtt_type_name(uint8_t); + +#endif /* MQTT_H */ blob - /dev/null blob + d9a4ef5672a8d27051390245fa4e28002e8069c99c6f8285f3c8a59225f1646c (mode 644) --- /dev/null +++ mqttd/mqttd.8 @@ -0,0 +1,189 @@ +.\" +.\" Copyright (c) 2016-2021, 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 +.\" 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. +.\" +.Dd $Mdocdate: October 31 2026 $ +.Dt MQTTD 8 +.Os +.Sh NAME +.Nm mqttd +.Nd MQTT message broker +.Sh SYNOPSIS +.Nm mqttd +.Op Fl dnv +.Op Fl D Ar macro Ns = Ns Ar value +.Op Fl f Ar file +.Sh DESCRIPTION +.Nm +is a message broker speaking MQTT versions 3.1, 3.1.1 and 5.0. +Clients connect over TCP, optionally protected by TLS, publish messages +to topics and subscribe to topic filters. +.Nm +routes every published message to the matching subscribers, honouring +QoS 0, 1 and 2 delivery, retained messages, will messages, persistent +sessions with offline queues, session and message expiry, topic aliases +and subscription identifiers. +.Pp +.Nm +is split into several processes which communicate over +.Xr imsg_init 3 +pipes: +.Bl -tag -width "sockets" +.It parent +Keeps root privileges to read the configuration and certificates and to +bind the listening sockets. +Afterwards it only relays control requests and waits for its children. +.It control +Owns the +.Xr unix 4 +control socket used by +.Xr mqttctl 8 . +.It broker +The protocol authority: it holds the sessions, the subscription tree and +the retained messages, authenticates clients and decides what is written +to every connection. +It never touches a network socket. +.It storage +Writes the broker's periodic snapshots into the database directory and +replays the last one at startup. +It is chrooted into that directory and is the only process that writes +to disk. +.It auth +Keeps root to run +.Xr pfctl 8 +for banning addresses and to start the optional external authenticator, +which itself runs unprivileged. +It holds no broker state. +.It snowflake +Plays the remote end for Snowflake sinks: it batches the messages the +broker hands it and inserts them over HTTPS, acknowledging only what the +warehouse confirmed. +The parent resolves names and signs its JWTs so it needs neither DNS nor +the private key. +.It sockets +A configurable number of preforked processes which accept connections, +run TLS, split the byte stream into control packets and relay them to +the broker. +.El +.Pp +Apart from auth, all children run +.Xr chroot 2 Ns ed +as the +.Dq _mqttd +user with reduced +.Xr pledge 2 +promises. +.Pp +Connection events are logged as +.Dq key=value +lines starting with +.Dq event= , +for example +.Dq event=connect , +.Dq event=disconnect , +.Dq event=auth-fail , +.Dq event=takeover , +.Dq event=rate-limit , +.Dq event=quota +and +.Dq event=ban . +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl D Ar macro Ns = Ns Ar value +Define +.Ar macro +to be set to +.Ar value +on the command line. +Overrides the definition of +.Ar macro +in the configuration file. +.It Fl d +Do not daemonize. +If this option is specified, +.Nm +will run in the foreground and log to +.Em stderr . +.It Fl f Ar file +Specify an alternative configuration file. +.It Fl n +Configtest mode. +Only check the configuration file for validity. +.It Fl v +Produce more verbose output. +Multiple +.Fl v +options increase the verbosity. +.El +.Sh PROTOCOL NOTES +Shared subscriptions deliver each message to one member of the group, +round robin, preferring connected members. +Enhanced authentication is available only through an external +authenticator configured with +.Ic auth exec ; +without one, v5 clients asking for it are rejected with reason code 0x8C. +Bridges to other brokers and Sparkplug Aware behaviour are configured in +.Xr mqttd.conf 5 . +On +.Dv SIGHUP +the configuration is re-read: limits, users, ACLs, TLS certificates and +listeners take effect immediately without dropping connections; a +removed listener's existing clients stay connected. +Changes to +.Ic prefork , +.Ic persistence , +.Ic auth +or bridges are reported in the log and need a restart. +Session state survives a restart only with +.Ic persistence +enabled in +.Xr mqttd.conf 5 . +.Sh FILES +.Bl -tag -width "/var/run/mqttd.sockXX" -compact +.It Pa /etc/mqttd.conf +Default configuration file. +.It Pa /etc/ssl/mqttd.crt +Default TLS server certificate. +.It Pa /etc/ssl/private/mqttd.key +Default TLS server private key. +.It Pa /var/run/mqttd.sock +.Ux Ns -domain +socket used for communication with +.Xr mqttctl 8 . +.It Pa /var/db/mqttd/mqttd.db +Default persistence database. +.El +.Sh SEE ALSO +.Xr mqttd.conf 5 , +.Xr mqttctl 8 +.Sh STANDARDS +.Rs +.%A OASIS +.%T MQTT Version 5.0 +.%D March 2019 +.Re +.Pp +.Rs +.%A OASIS +.%T MQTT Version 3.1.1 +.%D October 2014 +.Re +.Sh HISTORY +.Nm +grew out of the +.Nm thingsd +proxy daemon, keeping its privilege separated process layout. +.Sh AUTHORS +.An Tracey Emery Aq Mt tracey@openbsd.org blob - /dev/null blob + 61440506ee13e193d055e45631203af3f5c224386d370f26e5b9ace73c82ac57 (mode 644) --- /dev/null +++ mqttd/mqttd.c @@ -0,0 +1,1459 @@ +/* + * Copyright (c) 2016, 2019-2021, 2026 Tracey Emery + * Copyright (c) 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. + */ + +/* + * The parent keeps root for the privileged setup: reading the + * configuration and certificates, binding the listeners and forking the + * control, broker and sockets processes. Afterwards it only relays + * control requests and waits for its children. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" + +__dead void usage(void); +int main(int, char **); + +static int parent_configure(struct mqttd *); +static void parent_configure_done(struct mqttd *); +static void parent_reload(struct mqttd *); +static int sockaddr_same(struct sockaddr_storage *, + struct sockaddr_storage *); +static void parent_dispatch_control(int, short, void *); +static void parent_dispatch_broker(int, short, void *); +static void parent_dispatch_storage(int, short, void *); +static void parent_dispatch_auth(int, short, void *); +static void parent_dispatch_snow(int, short, void *); +static void parent_snow_resolve(struct imsg *); +static void parent_snow_jwt(struct imsg *); +static void parent_dispatch_sockets(int, short, void *); +static void parent_sighdlr(int, short, void *); +static void parent_show_info(struct imsg *); +static __dead void + parent_shutdown(struct mqttd *); + +struct mqttd *env; + +static struct imsgev *parent_iev_control; +static struct imsgev *parent_iev_broker; +static struct imsgev *parent_iev_storage; +static struct imsgev *parent_iev_auth; +static struct imsgev *parent_iev_snow; +static struct imsgev *parent_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; + +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 +imsg_compose_event(struct imsgev *iev, uint32_t type, uint32_t peerid, + pid_t pid, int fd, void *data, size_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, 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))); +} + +void +imsg_event_shutdown(struct imsgev *iev) +{ + event_del(&iev->ev); + imsgbuf_clear(&iev->ibuf); + close(iev->ibuf.fd); +} + +/* chroot into the daemon user's home and become that user */ +void +proc_privdrop(const char *title) +{ + proc_privdrop_dir(title, NULL); +} + +/* chroot into dir, or the daemon user's home when NULL, and drop root */ +void +proc_privdrop_dir(const char *title, const char *dir) +{ + struct passwd *pw; + + if ((pw = getpwnam(MQTTD_USER)) == NULL) + fatal("unknown user %s", MQTTD_USER); + + if (chroot(dir != NULL ? dir : pw->pw_dir) == -1) + fatal("%s: chroot %s", __func__, dir != NULL ? dir : pw->pw_dir); + if (chdir("/") == -1) + fatal("%s: chdir(\"/\")", __func__); + + setproctitle("%s", 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__); +} + +int +sockaddr_ntop(struct sockaddr_storage *ss, char *buf, size_t len) +{ + char host[NI_MAXHOST], serv[NI_MAXSERV]; + + if (ss->ss_family == AF_UNIX) { + snprintf(buf, len, "unix:%s", + ((struct sockaddr_un *)ss)->sun_path); + return (0); + } + + if (getnameinfo((struct sockaddr *)ss, ss->ss_len, host, sizeof(host), + serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { + strlcpy(buf, "unknown", len); + return (-1); + } + if (ss->ss_family == AF_INET6) + snprintf(buf, len, "[%s]:%s", host, serv); + else + snprintf(buf, len, "%s:%s", host, serv); + return (0); +} + +static void +parent_dispatch_control(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int i, n, verbose; + + 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__); + parent_shutdown(env); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost control process", __func__); + parent_shutdown(env); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CTL_SHOW_PARENT_REQUEST: + parent_show_info(&imsg); + break; + case IMSG_CTL_SHOW_BROKER_REQUEST: + case IMSG_CTL_SHOW_CLIENTS_REQUEST: + case IMSG_CTL_SHOW_SUBS_REQUEST: + case IMSG_CTL_SHOW_RETAINED_REQUEST: + case IMSG_CTL_SHOW_BRIDGES_REQUEST: + case IMSG_CTL_KILL_CLIENT: + case IMSG_CTL_DISCONNECT_ALL: + case IMSG_CTL_SAVE: + imsg_forward_event(parent_iev_broker, &imsg); + break; + case IMSG_CTL_SHOW_LISTENERS_REQUEST: + for (i = 0; i < env->conf.prefork; i++) + imsg_forward_event(&parent_iev_socks[i], &imsg); + break; + case IMSG_CTL_VERBOSE: + IMSG_SIZE_CHECK(&imsg, &verbose); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + imsg_forward_event(parent_iev_broker, &imsg); + for (i = 0; i < env->conf.prefork; 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); +} + +static void +parent_dispatch_broker(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost broker process", __func__); + parent_shutdown(env); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost broker process", __func__); + parent_shutdown(env); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_DONE: + parent_configure_done(env); + break; + default: + log_warnx("%s: unexpected imsg %d from broker", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +parent_dispatch_storage(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost storage process", __func__); + parent_shutdown(env); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost storage process", __func__); + parent_shutdown(env); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_DONE: + parent_configure_done(env); + break; + default: + log_warnx("%s: unexpected imsg %d from storage", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +parent_dispatch_auth(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost auth process", __func__); + parent_shutdown(env); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost auth process", __func__); + parent_shutdown(env); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_DONE: + parent_configure_done(env); + break; + default: + log_warnx("%s: unexpected imsg %d from auth", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +/* the chrooted sink process can neither resolve names nor hold the key */ +static void +parent_snow_resolve(struct imsg *imsg) +{ + struct snow_resolve rq; + struct snow_resolved rs; + struct bridge_def *b; + struct addrinfo hints, *res0, *res; + char port[8]; + int error; + + IMSG_SIZE_CHECK(imsg, &rq); + memcpy(&rq, imsg->data, sizeof(rq)); + + memset(&rs, 0, sizeof(rs)); + rs.id = rq.id; + + TAILQ_FOREACH(b, &env->bridges, entry) + if (b->snow != NULL && b->snow->id == rq.id) + break; + if (b == NULL) + goto reply; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + snprintf(port, sizeof(port), "%u", ntohs(b->snow->port)); + if ((error = getaddrinfo(b->snow->host, port, &hints, &res0)) != 0) { + log_warnx("snowflake %s: %s: %s", b->snow->name, b->snow->host, + gai_strerror(error)); + goto reply; + } + for (res = res0; res != NULL && rs.n < MQTTD_SNOW_MAXADDR; + res = res->ai_next) { + if (res->ai_addrlen > sizeof(rs.addrs[0])) + continue; + memcpy(&rs.addrs[rs.n], res->ai_addr, res->ai_addrlen); + rs.n++; + } + freeaddrinfo(res0); +reply: + imsg_compose_event(parent_iev_snow, IMSG_SNOW_RESOLVED, 0, 0, -1, &rs, + sizeof(rs)); +} + +static void +parent_snow_jwt(struct imsg *imsg) +{ + struct snow_jwt_reply r; + struct bridge_def *b; + uint32_t id; + + IMSG_SIZE_CHECK(imsg, &id); + memcpy(&id, imsg->data, sizeof(id)); + + memset(&r, 0, sizeof(r)); + r.id = id; + TAILQ_FOREACH(b, &env->bridges, entry) + if (b->snow != NULL && b->snow->id == id) + break; + if (b != NULL && jwt_sign(b->snow, r.token, sizeof(r.token), + &r.expires) == 0) + r.ok = 1; + imsg_compose_event(parent_iev_snow, IMSG_SNOW_JWT_REPLY, 0, 0, -1, &r, + sizeof(r)); + explicit_bzero(&r, sizeof(r)); +} + +static void +parent_dispatch_snow(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost snowflake process", __func__); + parent_shutdown(env); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost snowflake process", + __func__); + parent_shutdown(env); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_DONE: + parent_configure_done(env); + break; + case IMSG_SNOW_RESOLVE: + parent_snow_resolve(&imsg); + break; + case IMSG_SNOW_JWT_REQUEST: + parent_snow_jwt(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from snowflake", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +parent_dispatch_sockets(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + 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__); + parent_shutdown(env); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost sockets process", __func__); + parent_shutdown(env); + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_DONE: + parent_configure_done(env); + break; + default: + log_warnx("%s: unexpected imsg %d from sockets", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +parent_sighdlr(int sig, short event, void *arg) +{ + struct mqttd *e = arg; + + switch (sig) { + case SIGHUP: + parent_reload(e); + break; + case SIGPIPE: + case SIGUSR1: + break; + case SIGTERM: + case SIGINT: + parent_shutdown(e); + break; + default: + fatalx("unexpected signal"); + } +} + +__dead void +usage(void) +{ + fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n", + getprogname()); + exit(1); +} + +int +main(int argc, char **argv) +{ + struct mqttd *e; + const char *conffile = MQTTD_CONF; + struct stat st; + int ch, i, n; + int pipe_control[2]; + int pipe_broker[2]; + int pipe_storage[2]; + int pipe_control_broker[2]; + int pipe_broker_storage[2]; + int pipe_auth[2]; + int pipe_broker_auth[2]; + int pipe_snow[2]; + int pipe_broker_snow[2]; + int pipe_socks[MQTTD_MAX_PREFORK][2]; + int pipe_control_socks[MQTTD_MAX_PREFORK][2]; + int pipe_broker_socks[MQTTD_MAX_PREFORK][2]; + int fds[MQTTD_MAX_PREFORK]; + + if ((e = calloc(1, sizeof(*e))) == NULL) + fatal("%s: calloc", __func__); + + while ((ch = getopt(argc, argv, "D:df:nv")) != -1) { + switch (ch) { + case 'D': + if (cmdline_symset(optarg) < 0) + log_warnx("could not parse macro definition %s", + optarg); + break; + case 'd': + e->debug = 2; + break; + case 'f': + conffile = optarg; + break; + case 'n': + e->debug = 2; + e->noaction = 1; + break; + case 'v': + e->verbose++; + break; + default: + usage(); + } + } + + argc -= optind; + if (argc > 0) + usage(); + + /* log to stderr until daemonized */ + log_init(e->debug ? e->debug : 1, LOG_DAEMON); + + env = e; + e->conffile = conffile; + e->started = time(NULL); + strlcpy(e->dbdir, MQTTD_DB_DIR, sizeof(e->dbdir)); + + if (parse_config(e->conffile, e) == -1) + exit(1); + + if (e->noaction) { + fprintf(stderr, "configuration OK\n"); + exit(0); + } + + if (geteuid()) + fatalx("need root privileges"); + if (getpwnam(MQTTD_USER) == NULL) + fatal("unknown user %s", MQTTD_USER); + + if (e->conf.persist) { + if (stat(e->dbdir, &st) == -1) + fatal("persistence path %s", e->dbdir); + if (!S_ISDIR(st.st_mode)) + fatalx("persistence path %s is not a directory", + e->dbdir); + if (st.st_uid != getpwnam(MQTTD_USER)->pw_uid) + log_warnx("persistence path %s is not owned by %s, " + "snapshots will fail", e->dbdir, MQTTD_USER); + } + + log_init(e->debug, LOG_DAEMON); + log_setverbose(e->verbose); + log_procinit("parent"); + + if (!e->debug && daemon(0, 0) == -1) + fatal("can't daemonize"); + + 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 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_broker) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_storage) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_control_broker) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_broker_storage) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_auth) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_broker_auth) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_snow) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_broker_snow) == -1) + fatal("%s: socketpair", __func__); + for (i = 0; i < e->conf.prefork; i++) { + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_socks[i]) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_control_socks[i]) == -1 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC, + pipe_broker_socks[i]) == -1) + fatal("%s: socketpair", __func__); + } + + /* control: parent[1], control_broker[0], control_socks[i][0] */ + switch (fork()) { + case -1: + fatal("%s: fork", __func__); + case 0: + close(pipe_control[0]); + close(pipe_broker[0]); + close(pipe_broker[1]); + close(pipe_storage[0]); + close(pipe_storage[1]); + close(pipe_auth[0]); + close(pipe_auth[1]); + close(pipe_snow[0]); + close(pipe_snow[1]); + close(pipe_control_broker[1]); + close(pipe_broker_storage[0]); + close(pipe_broker_storage[1]); + close(pipe_broker_auth[0]); + close(pipe_broker_auth[1]); + close(pipe_broker_snow[0]); + close(pipe_broker_snow[1]); + for (i = 0; i < e->conf.prefork; i++) { + close(pipe_socks[i][0]); + close(pipe_socks[i][1]); + close(pipe_control_socks[i][1]); + close(pipe_broker_socks[i][0]); + close(pipe_broker_socks[i][1]); + fds[i] = pipe_control_socks[i][0]; + } + control(pipe_control[1], pipe_control_broker[0], fds); + /* NOTREACHED */ + } + + /* snowflake: snow[1], broker_snow[1] */ + switch (fork()) { + case -1: + fatal("%s: fork", __func__); + case 0: + close(pipe_control[0]); + close(pipe_control[1]); + close(pipe_broker[0]); + close(pipe_broker[1]); + close(pipe_storage[0]); + close(pipe_storage[1]); + close(pipe_auth[0]); + close(pipe_auth[1]); + close(pipe_snow[0]); + close(pipe_control_broker[0]); + close(pipe_control_broker[1]); + close(pipe_broker_storage[0]); + close(pipe_broker_storage[1]); + close(pipe_broker_auth[0]); + close(pipe_broker_auth[1]); + close(pipe_broker_snow[0]); + for (i = 0; i < e->conf.prefork; i++) { + close(pipe_socks[i][0]); + close(pipe_socks[i][1]); + close(pipe_control_socks[i][0]); + close(pipe_control_socks[i][1]); + close(pipe_broker_socks[i][0]); + close(pipe_broker_socks[i][1]); + } + snowflake(pipe_snow[1], pipe_broker_snow[1]); + /* NOTREACHED */ + } + + /* storage: storage[1], broker_storage[1] */ + switch (fork()) { + case -1: + fatal("%s: fork", __func__); + case 0: + close(pipe_control[0]); + close(pipe_control[1]); + close(pipe_broker[0]); + close(pipe_broker[1]); + close(pipe_storage[0]); + close(pipe_auth[0]); + close(pipe_auth[1]); + close(pipe_snow[0]); + close(pipe_snow[1]); + close(pipe_control_broker[0]); + close(pipe_control_broker[1]); + close(pipe_broker_storage[0]); + close(pipe_broker_auth[0]); + close(pipe_broker_auth[1]); + close(pipe_broker_snow[0]); + close(pipe_broker_snow[1]); + for (i = 0; i < e->conf.prefork; i++) { + close(pipe_socks[i][0]); + close(pipe_socks[i][1]); + close(pipe_control_socks[i][0]); + close(pipe_control_socks[i][1]); + close(pipe_broker_socks[i][0]); + close(pipe_broker_socks[i][1]); + } + storage(pipe_storage[1], pipe_broker_storage[1]); + /* NOTREACHED */ + } + + /* auth: auth[1], broker_auth[1]; keeps root for pfctl and exec */ + switch (fork()) { + case -1: + fatal("%s: fork", __func__); + case 0: + close(pipe_control[0]); + close(pipe_control[1]); + close(pipe_broker[0]); + close(pipe_broker[1]); + close(pipe_storage[0]); + close(pipe_storage[1]); + close(pipe_auth[0]); + close(pipe_snow[0]); + close(pipe_snow[1]); + close(pipe_control_broker[0]); + close(pipe_control_broker[1]); + close(pipe_broker_storage[0]); + close(pipe_broker_storage[1]); + close(pipe_broker_auth[0]); + close(pipe_broker_snow[0]); + close(pipe_broker_snow[1]); + for (i = 0; i < e->conf.prefork; i++) { + close(pipe_socks[i][0]); + close(pipe_socks[i][1]); + close(pipe_control_socks[i][0]); + close(pipe_control_socks[i][1]); + close(pipe_broker_socks[i][0]); + close(pipe_broker_socks[i][1]); + } + auth(pipe_auth[1], pipe_broker_auth[1]); + /* NOTREACHED */ + } + + /* broker: broker[1], control_broker[1], and the [0] end of the rest */ + switch (fork()) { + case -1: + fatal("%s: fork", __func__); + case 0: + close(pipe_control[0]); + close(pipe_control[1]); + close(pipe_broker[0]); + close(pipe_storage[0]); + close(pipe_storage[1]); + close(pipe_auth[0]); + close(pipe_auth[1]); + close(pipe_snow[0]); + close(pipe_snow[1]); + close(pipe_control_broker[0]); + close(pipe_broker_storage[1]); + close(pipe_broker_auth[1]); + close(pipe_broker_snow[1]); + for (i = 0; i < e->conf.prefork; i++) { + close(pipe_socks[i][0]); + close(pipe_socks[i][1]); + close(pipe_control_socks[i][0]); + close(pipe_control_socks[i][1]); + close(pipe_broker_socks[i][1]); + fds[i] = pipe_broker_socks[i][0]; + } + broker(pipe_broker[1], pipe_control_broker[1], + pipe_broker_storage[0], pipe_broker_auth[0], + pipe_broker_snow[0], fds); + /* NOTREACHED */ + } + + /* sockets n: socks[n][1], control_socks[n][1], broker_socks[n][1] */ + for (n = 0; n < e->conf.prefork; n++) { + switch (fork()) { + case -1: + fatal("%s: fork", __func__); + case 0: + close(pipe_control[0]); + close(pipe_control[1]); + close(pipe_broker[0]); + close(pipe_broker[1]); + close(pipe_storage[0]); + close(pipe_storage[1]); + close(pipe_auth[0]); + close(pipe_auth[1]); + close(pipe_snow[0]); + close(pipe_snow[1]); + close(pipe_control_broker[0]); + close(pipe_control_broker[1]); + close(pipe_broker_storage[0]); + close(pipe_broker_storage[1]); + close(pipe_broker_auth[0]); + close(pipe_broker_auth[1]); + close(pipe_broker_snow[0]); + close(pipe_broker_snow[1]); + for (i = 0; i < e->conf.prefork; i++) { + close(pipe_socks[i][0]); + close(pipe_control_socks[i][0]); + close(pipe_broker_socks[i][0]); + if (i == n) + continue; + close(pipe_socks[i][1]); + close(pipe_control_socks[i][1]); + close(pipe_broker_socks[i][1]); + } + sockets(pipe_socks[n][1], pipe_control_socks[n][1], + pipe_broker_socks[n][1]); + /* NOTREACHED */ + } + } + + /* parent: the [0] end of every pipe to a child */ + close(pipe_control[1]); + close(pipe_broker[1]); + close(pipe_storage[1]); + close(pipe_auth[1]); + close(pipe_snow[1]); + close(pipe_control_broker[0]); + close(pipe_control_broker[1]); + close(pipe_broker_storage[0]); + close(pipe_broker_storage[1]); + close(pipe_broker_auth[0]); + close(pipe_broker_auth[1]); + close(pipe_broker_snow[0]); + close(pipe_broker_snow[1]); + for (i = 0; i < e->conf.prefork; i++) { + close(pipe_socks[i][1]); + close(pipe_control_socks[i][0]); + close(pipe_control_socks[i][1]); + close(pipe_broker_socks[i][0]); + close(pipe_broker_socks[i][1]); + } + + event_init(); + + signal_set(&ev_sigint, SIGINT, parent_sighdlr, e); + signal_set(&ev_sigterm, SIGTERM, parent_sighdlr, e); + signal_set(&ev_sighup, SIGHUP, parent_sighdlr, e); + signal_set(&ev_sigpipe, SIGPIPE, parent_sighdlr, e); + signal_set(&ev_sigusr1, SIGUSR1, parent_sighdlr, e); + 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 ((parent_iev_control = calloc(1, sizeof(*parent_iev_control))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(parent_iev_control, pipe_control[0], + parent_dispatch_control); + + if ((parent_iev_broker = calloc(1, sizeof(*parent_iev_broker))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(parent_iev_broker, pipe_broker[0], + parent_dispatch_broker); + + if ((parent_iev_storage = calloc(1, sizeof(*parent_iev_storage))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(parent_iev_storage, pipe_storage[0], + parent_dispatch_storage); + + if ((parent_iev_auth = calloc(1, sizeof(*parent_iev_auth))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(parent_iev_auth, pipe_auth[0], parent_dispatch_auth); + + if ((parent_iev_snow = calloc(1, sizeof(*parent_iev_snow))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(parent_iev_snow, pipe_snow[0], parent_dispatch_snow); + + if ((parent_iev_socks = calloc(e->conf.prefork, + sizeof(*parent_iev_socks))) == NULL) + fatal("%s: calloc", __func__); + for (i = 0; i < e->conf.prefork; i++) + imsg_event_init(&parent_iev_socks[i], pipe_socks[i][0], + parent_dispatch_sockets); + + if (parent_configure(e) == -1) + fatalx("configuration failed"); + + /* + * A reload re-parses (rpath dns route), may bind new listeners + * (inet unix cpath fattr) and hands their descriptors to the + * sockets processes (sendfd). + */ + if (pledge("stdio rpath inet dns route proc sendfd unix cpath fattr", + NULL) == -1) + fatal("pledge"); + + event_dispatch(); + + parent_shutdown(e); +} + +static int +parent_configure(struct mqttd *e) +{ + struct listener *l; + struct user *u; + struct acl *a; + struct bridge_def *b; + int n, fd; + + TAILQ_FOREACH(l, &e->listeners, entry) { + if (sockets_create_listener(l) == -1) + return (-1); + if (listener_tls_load(l) == -1) + return (-1); + + for (n = 0; n < e->conf.prefork; n++) { + if ((fd = dup(l->fd)) == -1) { + log_warn("%s: dup", __func__); + return (-1); + } + if (config_send_listener(&parent_iev_socks[n], l, + fd) == -1) + return (-1); + if (config_send_tls(&parent_iev_socks[n], l) == -1) + return (-1); + } + + /* the children hold their own copies now */ + close(l->fd); + l->fd = -1; + freezero(l->conf.tls_key, l->conf.tls_key_len); + l->conf.tls_key = NULL; + l->conf.tls_key_len = 0; + } + + if (config_send_global(parent_iev_broker, e) == -1) + return (-1); + if (config_send_global(parent_iev_storage, e) == -1) + return (-1); + if (config_send_global(parent_iev_auth, e) == -1) + return (-1); + if (config_send_global(parent_iev_snow, e) == -1) + return (-1); + for (n = 0; n < e->conf.prefork; n++) + if (config_send_global(&parent_iev_socks[n], e) == -1) + return (-1); + + TAILQ_FOREACH(u, &e->users, entry) + if (config_send_user(parent_iev_broker, u) == -1) + return (-1); + TAILQ_FOREACH(a, &e->acls, entry) + if (config_send_acl(parent_iev_broker, a) == -1) + return (-1); + + /* + * Bridges: the broker gets the whole definition, the sockets + * process that will dial out gets the address and CA bundle. + */ + TAILQ_FOREACH(b, &e->bridges, entry) { + if (b->snow != NULL) { + /* a sink: the snowflake process plays the remote */ + b->conf.proc = MQTTD_PROC_SNOW; + b->snow->key = tls_load_file(b->snow->key_file, + &b->snow->key_len, NULL); + if (b->snow->key == NULL) { + log_warn("snowflake %s: key file %s", + b->snow->name, b->snow->key_file); + return (-1); + } + b->snow->tls_ca = tls_load_file(b->snow->tls_ca_file != + NULL ? b->snow->tls_ca_file : MQTTD_BRIDGE_CA, + &b->snow->tls_ca_len, NULL); + if (b->snow->tls_ca == NULL) { + log_warn("snowflake %s: ca file", + b->snow->name); + return (-1); + } + if (config_send_bridge(parent_iev_broker, b) == -1) + return (-1); + if (imsg_compose_event(parent_iev_snow, IMSG_CFG_SNOW, + 0, 0, -1, b->snow, sizeof(*b->snow)) == -1) + return (-1); + if (config_send_snow_ca(parent_iev_snow, + b->snow) == -1) + return (-1); + continue; + } + b->conf.proc = b->conf.id % e->conf.prefork; + if (b->conf.tls) { + b->conf.tls_ca = tls_load_file(b->conf.tls_ca_file != + NULL ? b->conf.tls_ca_file : MQTTD_BRIDGE_CA, + &b->conf.tls_ca_len, NULL); + if (b->conf.tls_ca == NULL) { + log_warn("bridge %s: ca file", b->conf.name); + return (-1); + } + } + if (config_send_bridge(parent_iev_broker, b) == -1) + return (-1); + if (imsg_compose_event(&parent_iev_socks[b->conf.proc], + IMSG_CFG_BRIDGE, 0, 0, -1, &b->conf, + sizeof(b->conf)) == -1) + return (-1); + if (config_send_bridge_ca(&parent_iev_socks[b->conf.proc], + b) == -1) + return (-1); + explicit_bzero(b->conf.password, sizeof(b->conf.password)); + } + + /* + * Every unprivileged worker acknowledges before anything starts. + * The broker only acknowledges once storage has replayed the last + * snapshot into it, so no client connects to a half restored state. + */ + e->cfg_pending = 4 + e->conf.prefork; + imsg_compose_event(parent_iev_storage, IMSG_CFG_DONE, 0, 0, -1, NULL, + 0); + imsg_compose_event(parent_iev_auth, IMSG_CFG_DONE, 0, 0, -1, NULL, 0); + imsg_compose_event(parent_iev_snow, IMSG_CFG_DONE, 0, 0, -1, NULL, 0); + imsg_compose_event(parent_iev_broker, IMSG_CFG_DONE, 0, 0, -1, NULL, 0); + for (n = 0; n < e->conf.prefork; n++) + imsg_compose_event(&parent_iev_socks[n], IMSG_CFG_DONE, 0, 0, + -1, NULL, 0); + + return (0); +} + +static void +parent_configure_done(struct mqttd *e) +{ + int n; + + if (e->cfg_pending == 0) { + log_warnx("%s: configuration already finished", __func__); + return; + } + + if (--e->cfg_pending > 0) + return; + + imsg_compose_event(parent_iev_control, IMSG_CTL_START, 0, 0, -1, NULL, + 0); + imsg_compose_event(parent_iev_broker, IMSG_CTL_START, 0, 0, -1, NULL, + 0); + imsg_compose_event(parent_iev_storage, IMSG_CTL_START, 0, 0, -1, NULL, + 0); + imsg_compose_event(parent_iev_auth, IMSG_CTL_START, 0, 0, -1, NULL, 0); + imsg_compose_event(parent_iev_snow, IMSG_CTL_START, 0, 0, -1, NULL, 0); + for (n = 0; n < e->conf.prefork; n++) + imsg_compose_event(&parent_iev_socks[n], IMSG_CTL_START, 0, 0, + -1, NULL, 0); + + log_info("%s ready, %u listener(s), %u sockets process(es)", + getprogname(), e->nlisteners, e->conf.prefork); +} + +static int +sockaddr_same(struct sockaddr_storage *a, struct sockaddr_storage *b) +{ + struct sockaddr_in *a4, *b4; + struct sockaddr_in6 *a6, *b6; + + if (a->ss_family != b->ss_family) + return (0); + switch (a->ss_family) { + case AF_UNIX: + return (strcmp(((struct sockaddr_un *)a)->sun_path, + ((struct sockaddr_un *)b)->sun_path) == 0); + case AF_INET: + a4 = (struct sockaddr_in *)a; + b4 = (struct sockaddr_in *)b; + return (a4->sin_addr.s_addr == b4->sin_addr.s_addr); + case AF_INET6: + a6 = (struct sockaddr_in6 *)a; + b6 = (struct sockaddr_in6 *)b; + return (memcmp(&a6->sin6_addr, &b6->sin6_addr, + sizeof(a6->sin6_addr)) == 0 && + a6->sin6_scope_id == b6->sin6_scope_id); + default: + return (0); + } +} + +/* + * SIGHUP: re-read the configuration. Limits, users, ACLs and TLS + * material take effect at once. Listeners, prefork and persistence are + * fixed at startup because the children holding them are chrooted and + * pledged; changes there are reported and need a restart. + */ +static void +parent_reload(struct mqttd *e) +{ + struct mqttd *n; + struct listener *ol, *nl, *tl; + struct user *u; + struct acl *a; + struct bridge_def *b; + int i, fd; + + log_info("reloading configuration from %s", e->conffile); + + if ((n = calloc(1, sizeof(*n))) == NULL) { + log_warn("%s: calloc", __func__); + return; + } + n->conffile = e->conffile; + n->debug = e->debug; + n->verbose = e->verbose; + n->started = e->started; + strlcpy(n->dbdir, MQTTD_DB_DIR, sizeof(n->dbdir)); + + if (parse_config(n->conffile, n) == -1) { + log_warnx("reload failed, keeping the running configuration"); + config_free(n); + return; + } + + if (n->conf.prefork != e->conf.prefork) { + log_warnx("prefork changed, a restart is needed for that"); + n->conf.prefork = e->conf.prefork; + } + if (n->conf.persist != e->conf.persist || + strcmp(n->dbdir, e->dbdir) != 0) { + log_warnx("persistence settings changed, a restart is needed " + "for that"); + n->conf.persist = e->conf.persist; + strlcpy(n->dbdir, e->dbdir, sizeof(n->dbdir)); + } + if (strcmp(n->auth_exec, e->auth_exec) != 0 || + strcmp(n->pf_table, e->pf_table) != 0) { + log_warnx("auth exec and pf table changes need a restart"); + n->conf.auth_exec_set = e->conf.auth_exec_set; + strlcpy(n->auth_exec, e->auth_exec, sizeof(n->auth_exec)); + strlcpy(n->pf_table, e->pf_table, sizeof(n->pf_table)); + } + if (!TAILQ_EMPTY(&n->bridges) || !TAILQ_EMPTY(&e->bridges)) + log_warnx("bridge changes need a restart"); + while ((b = TAILQ_FIRST(&n->bridges)) != NULL) { + TAILQ_REMOVE(&n->bridges, b, entry); + config_free_bridge(b); + } + + /* + * Listeners: an unchanged one keeps its id and gets fresh TLS + * material, a new one is bound here and handed to every sockets + * process, a vanished one is torn down there. + */ + TAILQ_FOREACH(nl, &n->listeners, entry) { + TAILQ_FOREACH(ol, &e->listeners, entry) { + if (ol->conf.tls == nl->conf.tls && + ol->conf.websocket == nl->conf.websocket && + ol->conf.proxy == nl->conf.proxy && + ol->conf.port == nl->conf.port && + sockaddr_same(&ol->conf.ss, &nl->conf.ss)) + break; + } + if (ol != NULL) { + nl->conf.id = ol->conf.id; + memcpy(&nl->conf.ss, &ol->conf.ss, sizeof(nl->conf.ss)); + ol->fd = -2; /* mark as matched */ + if (!nl->conf.tls) + continue; + if (listener_tls_load(nl) == -1) { + log_warnx("listener %s keeps its old tls " + "material", nl->conf.name); + continue; + } + for (i = 0; i < e->conf.prefork; i++) { + if (config_send_tls(&parent_iev_socks[i], + nl) == -1) + break; + imsg_compose_event(&parent_iev_socks[i], + IMSG_CFG_TLS_DONE, 0, 0, -1, &nl->conf.id, + sizeof(nl->conf.id)); + } + } else { + if (sockets_create_listener(nl) == -1 || + listener_tls_load(nl) == -1) { + log_warnx("listener %s could not be created, " + "skipped", nl->conf.name); + nl->conf.id = 0; + continue; + } + for (i = 0; i < e->conf.prefork; i++) { + if ((fd = dup(nl->fd)) == -1 || + config_send_listener(&parent_iev_socks[i], + nl, fd) == -1 || + config_send_tls(&parent_iev_socks[i], + nl) == -1) + break; + imsg_compose_event(&parent_iev_socks[i], + IMSG_CFG_LISTENER_START, 0, 0, -1, + &nl->conf.id, sizeof(nl->conf.id)); + } + close(nl->fd); + nl->fd = -1; + log_info("listener %s added", nl->conf.name); + } + freezero(nl->conf.tls_key, nl->conf.tls_key_len); + nl->conf.tls_key = NULL; + nl->conf.tls_key_len = 0; + } + + TAILQ_FOREACH_SAFE(ol, &e->listeners, entry, tl) { + if (ol->fd != -2) { + for (i = 0; i < e->conf.prefork; i++) + imsg_compose_event(&parent_iev_socks[i], + IMSG_CFG_LISTENER_DEL, 0, 0, -1, + &ol->conf.id, sizeof(ol->conf.id)); + log_info("listener %s removed", ol->conf.name); + } + ol->fd = -1; + TAILQ_REMOVE(&e->listeners, ol, entry); + config_free_listener(ol); + } + TAILQ_FOREACH_SAFE(nl, &n->listeners, entry, tl) { + if (nl->conf.id == 0) { + TAILQ_REMOVE(&n->listeners, nl, entry); + config_free_listener(nl); + n->nlisteners--; + } + } + TAILQ_CONCAT(&e->listeners, &n->listeners, entry); + e->nlisteners = n->nlisteners; + + while ((u = TAILQ_FIRST(&e->users)) != NULL) { + TAILQ_REMOVE(&e->users, u, entry); + explicit_bzero(u, sizeof(*u)); + free(u); + } + while ((a = TAILQ_FIRST(&e->acls)) != NULL) { + TAILQ_REMOVE(&e->acls, a, entry); + free(a); + } + TAILQ_CONCAT(&e->users, &n->users, entry); + TAILQ_CONCAT(&e->acls, &n->acls, entry); + e->conf = n->conf; + free(n); + + config_send_global(parent_iev_broker, e); + config_send_global(parent_iev_storage, e); + config_send_global(parent_iev_auth, e); + config_send_global(parent_iev_snow, e); + for (i = 0; i < e->conf.prefork; i++) + config_send_global(&parent_iev_socks[i], e); + + imsg_compose_event(parent_iev_broker, IMSG_CFG_AUTH_RESET, 0, 0, -1, + NULL, 0); + TAILQ_FOREACH(u, &e->users, entry) + config_send_user(parent_iev_broker, u); + TAILQ_FOREACH(a, &e->acls, entry) + config_send_acl(parent_iev_broker, a); + + log_info("configuration reloaded"); +} + +static void +parent_show_info(struct imsg *imsg) +{ + struct ctl_parent_info pi; + + memset(&pi, 0, sizeof(pi)); + pi.verbose = log_getverbose(); + pi.prefork = env->conf.prefork; + pi.nlisteners = env->nlisteners; + pi.started = env->started; + + imsg_compose_event(parent_iev_control, IMSG_CTL_SHOW_PARENT_DATA, + imsg->hdr.peerid, 0, -1, &pi, sizeof(pi)); + imsg_compose_event(parent_iev_control, IMSG_CTL_SHOW_PARENT_END, + imsg->hdr.peerid, 0, -1, NULL, 0); +} + +static __dead void +parent_shutdown(struct mqttd *e) +{ + struct listener *l; + struct user *u; + struct acl *a; + struct bridge_def *b; + pid_t pid; + int n, status; + + /* closing the pipes tells the children to exit */ + imsg_event_shutdown(parent_iev_control); + free(parent_iev_control); + imsg_event_shutdown(parent_iev_broker); + free(parent_iev_broker); + imsg_event_shutdown(parent_iev_storage); + free(parent_iev_storage); + imsg_event_shutdown(parent_iev_auth); + free(parent_iev_auth); + imsg_event_shutdown(parent_iev_snow); + free(parent_iev_snow); + for (n = 0; n < e->conf.prefork; n++) + imsg_event_shutdown(&parent_iev_socks[n]); + 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)); + + while ((l = TAILQ_FIRST(&e->listeners)) != NULL) { + TAILQ_REMOVE(&e->listeners, l, entry); + config_free_listener(l); + } + while ((u = TAILQ_FIRST(&e->users)) != NULL) { + TAILQ_REMOVE(&e->users, u, entry); + explicit_bzero(u, sizeof(*u)); + free(u); + } + while ((a = TAILQ_FIRST(&e->acls)) != NULL) { + TAILQ_REMOVE(&e->acls, a, entry); + free(a); + } + while ((b = TAILQ_FIRST(&e->bridges)) != NULL) { + TAILQ_REMOVE(&e->bridges, b, entry); + config_free_bridge(b); + } + free(e); + + log_warnx("mqttd terminating"); + exit(0); +} blob - /dev/null blob + 39ef196a560787977e63fc84a10b25618e336828f2409a3135fe744f2b341cc1 (mode 644) --- /dev/null +++ mqttd/mqttd.conf.5 @@ -0,0 +1,605 @@ +.\" +.\" Copyright (c) 2016-2021, 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 +.\" 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. +.\" +.Dd $Mdocdate: October 31 2026 $ +.Dt MQTTD.CONF 5 +.Os +.Sh NAME +.Nm mqttd.conf +.Nd MQTT broker configuration file +.Sh DESCRIPTION +.Nm +is the configuration file for the MQTT broker, +.Xr mqttd 8 . +.Sh SECTIONS +.Nm +is divided into the following main sections: +.Bl -tag -width xxxx +.It Sy Macros +User-defined variables may be defined and used later, simplifying the +configuration file. +.It Sy Global Configuration +Global settings for +.Xr mqttd 8 . +.It Sy Listeners +Addresses and ports the broker accepts connections on. +.It Sy ACL +Topic access rules. +.El +.Pp +The current line can be extended over multiple lines using a backslash +.Pq Sq \e . +Comments can be put anywhere in the file using a hash mark +.Pq Sq # , +and extend to the end of the current line. +Care should be taken when commenting out multi-line text: +the comment is effective until the end of the entire block. +.Pp +Argument names not beginning with a letter, digit, or underscore +must be quoted. +.Pp +Additional configuration files can be included with the +.Ic include +keyword, for example: +.Bd -literal -offset indent +include "/etc/mqttd.users" +.Ed +.Sh MACROS +Macros can be defined that will later be expanded in context. +Macro names must start with a letter, digit, or underscore, +and may contain any of those characters. +Macro names may not be reserved words (for example, +.Ic listen , +.Ic user , +or +.Ic acl ) . +Macros are not expanded inside quotes. +.Pp +For example: +.Bd -literal -offset indent +ext_if="egress" +listen on $ext_if tls port 8883 +.Ed +.Sh GLOBAL CONFIGURATION +Here are the settings that can be set globally: +.Bl -tag -width Ds +.It Ic anonymous Pq Ic yes Ns | Ns Ic no +Allow clients to connect without a username and password. +The default is +.Ic no . +.It Ic auth ban after Ar count Ic failures within Ar seconds Ic pf table Ar name +Add a client's address to the +.Xr pf 4 +table +.Ar name +after +.Ar count +failed authentications from that address within +.Ar seconds . +The table must exist in +.Xr pf.conf 5 ; +.Xr mqttd 8 +only adds to it, expiring entries is left to +.Xr pfctl 8 . +.It Ic auth exec Ar path +Run +.Ar path +as an external authenticator for usernames not defined with +.Ic user . +It is started once, as the +.Dq _mqttd +user, with the request pipe on standard input and output, and restarted +if it exits. +Requests are one line each: +.Bd -literal -offset indent +auth +.Ed +.Pp +and are answered with +.Dq Ar id Ic ok Op Ar data-hex Op Ar user-hex +or +.Dq Ar id Ic fail . +Fields are hex encoded so they never contain spaces. +A request unanswered after 10 seconds fails. +.Pp +MQTT 5.0 enhanced authentication (SCRAM, Kerberos and the like) is also +delegated to the helper. +A CONNECT carrying an Authentication Method starts +.Bd -literal -offset indent +auth-start +.Ed +.Pp +to which the helper answers +.Dq Ar id Ic continue Ar data-hex +to challenge the client, whose AUTH reply arrives as +.Dq Ic auth-data Ar id data-hex , +or +.Dq Ar id Ic ok Op Ar data-hex Op Ar user-hex +to finish, with +.Ar user-hex +naming the authenticated identity when it differs from the CONNECT +username. +Client initiated re-authentication follows the same exchange. +See +.Pa /usr/local/share/examples/mqttd/mqttd-auth.pl . +.It Ic max clients Ar number +Refuse new connections once this many clients are connected. +The default of 0 means unlimited. +.It Ic max inflight Ar number +The number of unacknowledged QoS 1 and QoS 2 messages +.Xr mqttd 8 +sends to a client before queueing, and the number it accepts from a +client before disconnecting it. +This is announced to MQTT 5.0 clients as the Receive Maximum. +The default is 32. +.It Ic max keepalive Ar number +Cap the keepalive interval requested by clients to this many seconds. +MQTT 5.0 clients are told the effective value in the CONNACK. +The default of 0 accepts whatever the client asks for, including no +keepalive at all. +.It Ic max packet size Ar number +The largest control packet accepted from a client, in bytes. +Connections sending larger packets are closed. +The default is 1048576. +.It Ic max qos Ar number +The highest QoS granted to subscriptions and accepted for publishes, +0, 1 or 2. +The default is 2. +.It Ic max rate Ar number +Publishes per second a client may send, with a burst of one second's +worth; a client exceeding it is disconnected with reason code 0x96. +The default of 0 means unlimited. +A +.Ic user +block may override it. +.It Ic max queued Ar number +How many QoS 1 and QoS 2 messages are held for a persistent session +while its client is offline or its inflight window is full. +Further messages are dropped. +The default is 1000. +.It Ic max session expiry Ar number +Cap the session expiry interval requested by clients to this many +seconds. +Sessions of MQTT 3.1.1 clients with clean session 0 would otherwise +never expire. +The default of 0 applies no cap. +.It Ic max topic alias Ar number +The number of topic aliases an MQTT 5.0 client may establish. +The default is 16. +.It Ic persistence Pq Ic yes Ns | Ns Ic no +Keep sessions with a non-zero expiry interval, their subscriptions and +pending QoS 1 and 2 messages, and retained messages across restarts. +A snapshot is written periodically and at shutdown; state changed after +the last snapshot is lost on a crash. +The default is +.Ic no . +.It Ic persistence interval Ar number +Seconds between snapshots. +The default is 60. +.It Ic persistence path Ar directory +Directory holding the database. +It must exist and be owned by the +.Dq _mqttd +user; the storage process is chrooted into it. +The default is +.Pa /var/db/mqttd . +.It Ic prefork Ar number +Run the specified number of sockets processes. +Connections are spread over them by the kernel. +The default is 3. +.It Ic retain Pq Ic yes Ns | Ns Ic no +Whether retained messages are supported. +The default is +.Ic yes . +.It Ic sparkplug aware Pq Ic yes Ns | Ns Ic no +Behave as a Sparkplug Aware broker: keep a retained copy of every NBIRTH +and DBIRTH under +.Pa $sparkplug/certificates/ , +remove them on NDEATH and DDEATH, and write the time the connection was +lost into the payload of an NDEATH will as it is published. +The default is +.Ic no . +.It Ic sys interval Ar number +Publish broker statistics as retained messages under +.Pa $SYS/broker/ +every +.Ar number +seconds. +0 disables them. +The default is 10. +Clients can subscribe to +.Pa $SYS/# +but never publish below +.Pa $ . +.It Ic user Ar name Ic password Ar hash Op { ... } +Define a user allowed to connect. +.Ar hash +is a +.Xr crypt 3 +hash as produced by +.Xr encrypt 1 . +The broker checks the password of every CONNECT with +.Xr crypt_checkpass 3 . +The optional block accepts: +.Bl -tag -width Ds +.It Ic max clients Ar number +Connections this user may hold at once; a further CONNECT is refused +with reason code 0x97. +The default of 0 means unlimited. +.It Ic rate Ar number +Publishes per second for this user, overriding +.Ic max rate . +.El +.El +.Sh LISTENERS +A listener is declared with the +.Ic listen +keyword: +.Bd -ragged -offset indent +.Ic listen on Ar address +.Op Ic tls +.Op Ic websocket +.Op Ic proxy +.Op Ic port Ar number +.Op Ic name Ar string +.Op { ... } +.Ed +.Pp +.Ar address +is an IP address, a hostname, an interface or interface group name, +.Sq * +for all addresses, or an absolute path for a +.Xr unix 4 +socket. +A listener is created for every address the argument resolves to. +Without +.Ic port +the listener uses 1883, or 8883 with +.Ic tls , +8083 with +.Ic websocket +and 8084 with both. +The optional +.Ic name +labels the listener in +.Xr mqttctl 8 +output. +.Pp +.Ic websocket +accepts MQTT over WebSocket as browsers and many cloud SDKs speak it: +an HTTP upgrade offering the +.Dq mqtt +subprotocol, then binary frames. +.Ic proxy +expects a PROXY protocol version 1 or 2 header from a load balancer such +as +.Xr relayd 8 +or haproxy before anything else, and takes the client address from it; +restrict such a listener to the proxy's address with +.Xr pf 4 , +since anyone able to reach it can claim any source. +A unix socket listener is created with mode 0666 and takes none of these +flags; clients authenticate as usual. +.Pp +TLS listeners accept the following options inside the block: +.Bl -tag -width Ds +.It Ic tls certificate Ar file +Specify the certificate to use for this listener. +The +.Ar file +should contain a PEM encoded certificate. +The default is +.Pa /etc/ssl/mqttd.crt . +.It Ic tls key Ar file +Specify the private key to use for this listener. +The +.Ar file +should contain a PEM encoded private key. +The default is +.Pa /etc/ssl/private/mqttd.key . +.It Ic tls ciphers Ar string +Specify the TLS cipher string. +See the CIPHERS section of +.Xr openssl 1 +for information about SSL/TLS cipher suites and preference lists. +The default is +.Qq compat . +.It Ic tls client ca Ar cafile Oo Ic crl Ar crlfile Oc Op Ic optional +Require +.Po +or, if +.Ic optional +is specified, request but do not require +.Pc +a client certificate signed by a CA in +.Ar cafile , +checked against the optional revocation list in +.Ar crlfile . +The common name of a verified certificate becomes the client's username +for ACL purposes without a password check and without a matching +.Ic user +line; a username in the CONNECT packet must then be absent or identical. +.It Ic tls dhe Ar params +Specify the DHE parameters to use for DHE cipher suites. +Valid parameter values are none, legacy and auto. +The default is none. +.It Ic tls ecdhe Ar curves +Specify a comma separated list of elliptic curves to use for ECDHE +cipher suites. +The default is default. +.It Ic tls ocsp Ar file +Specify an OCSP response to be stapled during TLS handshakes. +.It Ic tls protocols Ar string +Specify the TLS protocols to enable. +Refer to the +.Xr tls_config_parse_protocols 3 +function for the correct syntax. +The default is +.Qq default . +.El +.Sh BRIDGES +A bridge connects +.Xr mqttd 8 +as a client to another broker and copies messages between the two. +.Bd -ragged -offset indent +.Ic bridge Ar name Brq ... +.Ed +.Pp +The block accepts: +.Bl -tag -width Ds +.It Ic remote Ar host Oo Ic tls Oc Op Ic port Ar number +The broker to connect to. +.Ar host +is resolved when the configuration is read. +The default port is 1883, or 8883 with +.Ic tls . +Required. +.It Ic client Ar id +Client identifier presented to the remote. +The default is +.Dq mqttd-bridge- Ns Ar name . +.It Ic user Ar name Ic password Ar secret +Credentials for the remote, sent in clear inside the connection. +.It Ic keepalive Ar seconds +Keepalive negotiated with the remote. +The default is 60. +.It Ic max queued Ar number +Messages held for this bridge while the remote is down, replacing the +global +.Ic max queued . +.It Ic version Ar number +MQTT version to speak to the remote: 3, 4 or 5. +The default is 4, meaning MQTT 3.1.1. +.It Ic tls ca Ar file +CA bundle used to verify the remote. +The default is +.Pa /etc/ssl/cert.pem . +.It Xo +.Ic topic Ar pattern +.Pq Ic in Ns | Ns Ic out Ns | Ns Ic both +.Op Ic qos Ar number +.Op Ic local Ar prefix +.Op Ic remote Ar prefix +.Xc +A topic filter to copy. +.Ic out +sends local publishes matching +.Ar prefix Ns Ar pattern +to the remote under the remote prefix; +.Ic in +subscribes to the remote and republishes locally under the local prefix. +At least one is required. +.El +.Pp +Messages for a remote that is down are queued in the bridge's own +session, bounded by +.Ic max queued +and included in persistence snapshots. +Reconnection backs off from 1 to 60 seconds. +Changes to bridges need a restart. +.Sh SNOWFLAKE SINKS +A +.Ic snowflake +block is a bridge whose remote is a Snowflake table. +Matching publishes are collected into batches and inserted through the +Snowflake SQL REST API with key-pair JWT authentication; a message is +acknowledged towards its publisher only once the warehouse has confirmed +the insert, and messages queue locally, bounded by +.Ic max queued +and included in persistence snapshots, while Snowflake is unreachable. +.Bd -ragged -offset indent +.Ic snowflake Ar name Brq ... +.Ed +.Pp +The block accepts: +.Bl -tag -width Ds +.It Ic account Ar identifier +The account identifier in the +.Dq orgname-accountname +form. +Required. +.It Ic user Ar name +The Snowflake user whose public key was registered with +.Ql ALTER USER ... SET RSA_PUBLIC_KEY . +Required. +.It Ic key Ar file +The matching unencrypted PKCS#8 private key. +Only the parent process reads it; it signs tokens for the sink process. +Required. +.It Ic table Ar database.schema.table +Target table. +Required. +It needs the columns +.Bd -literal -offset indent +CREATE TABLE mqtt_events ( + received_at TIMESTAMP_LTZ, + topic VARCHAR, + qos NUMBER(1), + retain BOOLEAN, + payload BINARY +); +.Ed +.Pp +Text payloads are read back with +.Ql TRY_PARSE_JSON(TO_VARCHAR(payload, 'UTF-8')) ; +Sparkplug B payloads stay protobuf. +.It Ic role Ar name +.It Ic warehouse Ar name +Role and warehouse to run the inserts with. +.It Ic host Ar name Op Ic port Ar number +Endpoint to connect to when it differs from +.Ar account Ns Pa .snowflakecomputing.com , +for example a PrivateLink URL. +.It Ic tls servername Ar name +Name to verify the server certificate against when it differs from +.Ic host . +.It Ic tls ca Ar file +CA bundle, default +.Pa /etc/ssl/cert.pem . +.It Ic tls noverify +Skip certificate verification. +For debugging PrivateLink or intercepting proxies only. +.It Ic flush Ar seconds +How long rows accumulate before they are inserted, up to 86400. +This is the cost control: every insert wakes the warehouse, so a site +that can tolerate the delay inserts every few hours instead of every +few seconds. +The default is 2. +.It Ic batch Ar rows +Rows per INSERT statement when a flush runs; a flush issues as many +statements as needed, each also kept under Snowflake's statement size +limit, and acknowledges rows statement by statement. +The default is 500. +.It Ic max queued Ar number +Rows held for this sink beyond the 65535 the broker keeps in flight, +replacing the global +.Ic max queued . +Size it for the longest +.Ic flush +plus the longest outage at the expected message rate. +.It Ic timeout Ar seconds +HTTP and statement timeout. +The default is 60. +.It Ic topic Ar pattern Oo Ic qos Ar number Oc Op Ic local Ar prefix +A topic filter to sink, QoS 0 or 1. +At least one is required. +.El +.Pp +Snowflake certificates: account endpoints live under wildcard +certificates for +.Pa *.snowflakecomputing.com +and regional variants, so the name verified must be the exact URL form +Snowflake gives for the account; multi-label legacy locators and +PrivateLink names need +.Ic host +or +.Ic tls servername +set accordingly. +Snowflake's own drivers additionally require OCSP; mqttd validates the +chain against +.Ic tls ca +and does not check revocation. +.Pp +Key setup: +.Bd -literal -offset indent +openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt \e + -out /etc/mqttd/snowflake.p8 +openssl rsa -in /etc/mqttd/snowflake.p8 -pubout -out snowflake.pub +# in Snowflake, paste the PEM body without header and footer: +ALTER USER mqtt_ingest SET RSA_PUBLIC_KEY='MIIBIjANBgkq...'; +.Ed +.Sh ACL +Topic access is controlled by an optional +.Ic acl +block. +Without one every authenticated client may publish and subscribe +anywhere. +With one, the first matching rule decides and anything unmatched is +denied. +Rules have the form: +.Bd -ragged -offset indent +.Pq Ic allow Ns | Ns Ic deny +.Pq Ic any Ns | Ns Ic user Ar name Ns | Ns Ic client Ar id +.Pq Ic publish Ns | Ns Ic subscribe Ns | Ns Ic all +.Ar pattern +.Ed +.Pp +A rule for +.Ic user +matches the authenticated username, a rule for +.Ic client +the client identifier. +Shared subscriptions are checked against the filter after the +.Pa $share/group/ +prefix. +.Pp +.Ar pattern +is a topic filter which may use the +.Sq + +and +.Sq # +wildcards. +.Sq %u +is replaced by the username and +.Sq %c +by the client identifier of the connecting client. +A subscription filter only matches a rule that covers it level by level; +a wildcard in the subscription never matches a literal level in the +pattern. +Will topics are checked as publishes when the client connects. +.Sh FILES +.Bl -tag -width "/etc/examples/mqttd.conf" -compact +.It Pa /etc/mqttd.conf +.Xr mqttd 8 +configuration file. +.It Pa /etc/examples/mqttd.conf +Example configuration file. +.El +.Sh EXAMPLES +A broker for a fleet of sensors reporting to a dashboard: +.Bd -literal -offset indent +prefork 4 +max keepalive 300 +max session expiry 86400 + +user "sensor" password "$2b$10$..." +user "dashboard" password "$2b$10$..." + +listen on * port 1883 name "plain" +listen on egress tls port 8883 name "secure" { + tls certificate "/etc/ssl/mqttd.crt" + tls key "/etc/ssl/private/mqttd.key" +} + +acl { + allow user "sensor" publish "sensors/%u/#" + allow user "sensor" subscribe "cmd/%u/#" + allow user "dashboard" all "#" +} + +bridge "central" { + remote "mqtt.example.com" tls + user "site1" password "secret" + topic "sensors/#" out qos 1 remote "site1/" + topic "cmd/#" in qos 1 remote "site1/" +} +.Ed +.Sh SEE ALSO +.Xr encrypt 1 , +.Xr crypt_checkpass 3 , +.Xr mqttctl 8 , +.Xr mqttd 8 +.Sh AUTHORS +.An Tracey Emery Aq Mt tracey@openbsd.org blob - /dev/null blob + 9a840761a13ab607a3c81b1e41d127a8e943c8968c6e451de3463bd7a1727090 (mode 644) --- /dev/null +++ mqttd/mqttd.h @@ -0,0 +1,792 @@ +/* + * Copyright (c) 2016, 2019-2021, 2026 Tracey Emery + * Copyright (c) 2015 Mike Larkin + * + * 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. + */ + +#ifndef MQTTD_H +#define MQTTD_H + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#define MQTTD_CONF "/etc/mqttd.conf" +#define MQTTD_SOCKET "/var/run/mqttd.sock" +#define MQTTD_USER "_mqttd" +#define MQTTD_DB_DIR "/var/db/mqttd" +#define MQTTD_DB_FILE "/mqttd.db" +#define MQTTD_DB_TMP "/mqttd.db.tmp" +#define MQTTD_DB_MAGIC "MQTTDB01" +#define MQTTD_DB_MAXSIZE (1024 * 1024 * 1024) +#define MQTTD_DEFAULT_PERSIST_INTERVAL 60 +#define MQTTD_AUTH_TIMEOUT 10 +#define MQTTD_AUTH_RESPAWN 2 +#define MQTTD_MAXPASSWORD 256 +#define MQTTD_AUTH_METHOD_MAX 64 +#define MQTTD_AUTH_DATA_MAX 512 +#define MQTTD_PF_TABLE_MAX 32 +#define MQTTD_PFCTL "/sbin/pfctl" +#define MQTTD_MAXHOST 256 +#define MQTTD_MAXPREFIX 128 +#define MQTTD_BRIDGE_KEEPALIVE 60 +#define MQTTD_BRIDGE_BACKOFF_MIN 1 +#define MQTTD_BRIDGE_BACKOFF_MAX 60 +#define MQTTD_BRIDGE_CA "/etc/ssl/cert.pem" +#define MQTTD_SPARKPLUG_PREFIX "spBv1.0/" +#define MQTTD_SPARKPLUG_CERTS "$sparkplug/certificates/" + +/* a bridge whose remote is the snowflake process, not a sockets process */ +#define MQTTD_PROC_SNOW UINT32_MAX +#define MQTTD_SNOW_SUFFIX ".snowflakecomputing.com" +#define MQTTD_SNOW_IDENT 256 +#define MQTTD_SNOW_MAXADDR 8 +#define MQTTD_SNOW_DEFAULT_BATCH 500 +#define MQTTD_SNOW_DEFAULT_FLUSH 2 +#define MQTTD_SNOW_DEFAULT_TIMEOUT 60 +#define MQTTD_SNOW_MAXFLUSH 86400 +#define MQTTD_SNOW_MAXSQL (900 * 1024) +#define MQTTD_SNOW_WINDOW 65535 +#define MQTTD_SNOW_MAXFAIL 3 +#define MQTTD_SNOW_MAXRESP (1024 * 1024) +#define MQTTD_SNOW_POLLS 30 +#define MQTTD_JWT_MAX 4096 +#define MQTTD_JWT_LIFETIME 3540 + +#define MQTTD_MAXTEXT 511 +#define MQTTD_MAXNAME 64 +#define MQTTD_MAXPEER 64 +#define MQTTD_MAXHASH 128 +#define MQTTD_MAXIFACE 16 +#define MQTTD_MAX_PREFORK 32 +#define MQTTD_NUMPROC 3 + +#define MQTT_PORT 1883 +#define MQTTS_PORT 8883 +#define MQTT_WS_PORT 8083 +#define MQTT_WSS_PORT 8084 +#define MQTTD_WS_MAXHDR 8192 +#define MQTTD_WS_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" +#define MQTTD_UNIX_MODE 0666 + +/* server side protocol limits */ +#define MQTTD_MAX_CLIENTID 128 +#define MQTTD_DEFAULT_MAX_PACKET (1024 * 1024) +#define MQTTD_DEFAULT_MAX_INFLIGHT 32 +#define MQTTD_DEFAULT_MAX_QUEUED 1000 +#define MQTTD_DEFAULT_TOPIC_ALIAS_MAX 16 +#define MQTTD_CONNECT_TIMEOUT 10 +#define MQTTD_RETAIN_SWEEP 60 +#define MQTTD_OUTBUF_FACTOR 4 +#define MQTTD_DEFAULT_SYS_INTERVAL 10 +#define MQTTD_VERSION "1.0" + +/* + * imsg queue depths, in messages, at which a process stops reading from + * its peers and at which it resumes. + */ +#define MQTTD_QUEUE_HIGH 2048 +#define MQTTD_QUEUE_LOW 256 + +#define TLS_CONFIG_MAX 511 +#define TLS_CERT "/etc/ssl/mqttd.crt" +#define TLS_KEY "/etc/ssl/private/mqttd.key" +#define TLS_CIPHERS "compat" +#define TLS_DHE_PARAMS "none" +#define TLS_ECDHE_CURVES "default" +#define TLSFLAG_CA 0x01 +#define TLSFLAG_CRL 0x02 +#define TLSFLAG_OPTIONAL 0x04 +#define TLSFLAG_BITS "\10\01CA\02CRL\03OPTIONAL" + +#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_NONE, + IMSG_CTL_OK, + IMSG_CTL_FAIL, + IMSG_CTL_VERBOSE, + IMSG_CTL_START, + IMSG_CFG_DONE, + + IMSG_CFG_GLOBAL, + IMSG_CFG_LISTENER, + IMSG_CFG_TLS, + IMSG_CFG_TLS_DONE, + IMSG_CFG_LISTENER_START, + IMSG_CFG_LISTENER_DEL, + IMSG_CFG_USER, + IMSG_CFG_ACL, + IMSG_CFG_AUTH_RESET, + IMSG_CTL_DISCONNECT_ALL, + + /* broker <-> auth */ + IMSG_AUTH_REQUEST, + IMSG_AUTH_REPLY, + IMSG_PF_BAN, + + /* bridges */ + IMSG_CFG_BRIDGE, + IMSG_CFG_BRIDGE_TOPIC, + IMSG_BRIDGE_CONNECT, + IMSG_BRIDGE_FAILED, + IMSG_CTL_SHOW_BRIDGES_REQUEST, + IMSG_CTL_SHOW_BRIDGES_DATA, + IMSG_CTL_SHOW_BRIDGES_END, + + /* snowflake sinks */ + IMSG_CFG_SNOW, + IMSG_SNOW_RESOLVE, + IMSG_SNOW_RESOLVED, + IMSG_SNOW_JWT_REQUEST, + IMSG_SNOW_JWT_REPLY, + + /* sockets <-> broker */ + IMSG_CONN_OPEN, + IMSG_CONN_PACKET, + IMSG_CONN_CLOSE, + IMSG_CONN_ERROR, + IMSG_CONN_KEEPALIVE, + + /* broker <-> storage */ + IMSG_DB_RECORD, + IMSG_DB_RESTORE_END, + IMSG_CTL_SAVE, + + /* control queries */ + IMSG_CTL_SHOW_PARENT_REQUEST, + IMSG_CTL_SHOW_PARENT_DATA, + IMSG_CTL_SHOW_PARENT_END, + IMSG_CTL_SHOW_CONTROL_REQUEST, + IMSG_CTL_SHOW_CONTROL_DATA, + IMSG_CTL_SHOW_CONTROL_END, + IMSG_CTL_SHOW_BROKER_REQUEST, + IMSG_CTL_SHOW_BROKER_DATA, + IMSG_CTL_SHOW_BROKER_END, + IMSG_CTL_SHOW_LISTENERS_REQUEST, + IMSG_CTL_SHOW_LISTENERS_DATA, + IMSG_CTL_SHOW_LISTENERS_END, + IMSG_CTL_SHOW_CLIENTS_REQUEST, + IMSG_CTL_SHOW_CLIENTS_DATA, + IMSG_CTL_SHOW_CLIENTS_END, + IMSG_CTL_SHOW_SUBS_REQUEST, + IMSG_CTL_SHOW_SUBS_DATA, + IMSG_CTL_SHOW_SUBS_END, + IMSG_CTL_SHOW_RETAINED_REQUEST, + IMSG_CTL_SHOW_RETAINED_DATA, + IMSG_CTL_SHOW_RETAINED_END, + IMSG_CTL_KILL_CLIENT, +}; + +struct imsgev { + struct imsgbuf ibuf; + void (*handler)(int, short, void *); + struct event ev; + short events; +}; + +/* global broker limits, fixed size so it travels in a single imsg */ +struct mqttd_global { + uint16_t prefork; + uint32_t max_clients; + uint32_t max_packet_size; + uint16_t max_inflight; + uint16_t max_keepalive; + uint32_t max_session_expiry; + uint32_t max_queued; + uint16_t topic_alias_max; + uint8_t max_qos; + uint8_t retain_available; + uint8_t anonymous; + uint8_t acl_present; + uint8_t persist; + uint8_t auth_exec_set; + uint8_t sparkplug; + uint16_t sys_interval; + uint16_t persist_interval; + uint16_t ban_failures; + uint16_t ban_within; + uint32_t max_rate; +}; + +struct address { + TAILQ_ENTRY(address) entry; + struct sockaddr_storage ss; + int ipproto; + int prefixlen; + in_port_t port; + char ifname[IFNAMSIZ]; +}; +TAILQ_HEAD(addresslist, address); + +/* + * One listener per resolved address. Pointers are only meaningful in the + * process that filled them in; the receiving side clears them and gets the + * TLS material through IMSG_CFG_TLS chunks. + */ +struct listener_config { + uint32_t id; + char name[MQTTD_MAXNAME]; + struct sockaddr_storage ss; + in_port_t port; + + int tls; + uint8_t websocket; + uint8_t proxy; + uint8_t tls_flags; + + uint8_t *tls_cert; + char *tls_cert_file; + size_t tls_cert_len; + + uint8_t *tls_key; + char *tls_key_file; + size_t tls_key_len; + + uint8_t *tls_ca; + char *tls_ca_file; + size_t tls_ca_len; + + uint8_t *tls_crl; + char *tls_crl_file; + size_t tls_crl_len; + + uint8_t *tls_ocsp_staple; + char *tls_ocsp_staple_file; + size_t tls_ocsp_staple_len; + + char tls_ciphers[TLS_CONFIG_MAX]; + char tls_dhe_params[TLS_CONFIG_MAX]; + char tls_ecdhe_curves[TLS_CONFIG_MAX]; + + uint32_t tls_protocols; +}; + +enum tls_config_type { + TLS_CFG_CA, + TLS_CFG_CERT, + TLS_CFG_CRL, + TLS_CFG_KEY, + TLS_CFG_OCSP_STAPLE, + TLS_CFG_BRIDGE_CA, +}; + +struct tls_config_msg { + uint32_t id; + enum tls_config_type tls_type; + size_t tls_len; + size_t tls_chunk_len; + size_t tls_chunk_offset; +}; + +struct listener { + TAILQ_ENTRY(listener) entry; + struct listener_config conf; + + int fd; + uint32_t nconns; + + struct tls_config *tls_config; + struct tls *tls_ctx; + + struct event ev; + struct event pause; +}; +TAILQ_HEAD(listenerlist, listener); + +struct user_config { + char name[MQTTD_MAXNAME]; + char hash[MQTTD_MAXHASH]; + uint32_t max_clients; + uint32_t rate; +}; + +/* + * broker -> auth: ask the external authenticator. PASSWORD carries a + * CONNECT username and password; START opens an enhanced (AUTH packet) + * exchange with the method and the client's first data; DATA continues + * one with the client's next AUTH payload. + */ +enum auth_kind { + AUTH_REQ_PASSWORD, + AUTH_REQ_START, + AUTH_REQ_DATA +}; + +struct auth_request { + uint32_t id; + uint8_t kind; + char user[MQTTD_MAXNAME]; + char password[MQTTD_MAXPASSWORD]; + char client_id[MQTTD_MAX_CLIENTID + 1]; + char peer[MQTTD_MAXPEER]; + char method[MQTTD_AUTH_METHOD_MAX]; + uint16_t datalen; + uint8_t data[MQTTD_AUTH_DATA_MAX]; +}; + +/* cont asks the client for more; user, when set, is the identity to use */ +struct auth_reply { + uint32_t id; + uint8_t ok; + uint8_t cont; + char user[MQTTD_MAXNAME]; + uint16_t datalen; + uint8_t data[MQTTD_AUTH_DATA_MAX]; +}; + +/* broker -> auth: add this address to the configured pf table */ +struct pf_ban { + char addr[MQTTD_MAXPEER]; +}; + +/* + * Bridges. The parent resolves the remote address and loads the CA; + * the sockets process named by proc opens the connection on request and + * the broker drives the MQTT client side of it. + */ +#define BRIDGE_IN 0x01 +#define BRIDGE_OUT 0x02 +#define BRIDGE_BOTH (BRIDGE_IN | BRIDGE_OUT) + +struct bridge_config { + uint32_t id; + uint32_t proc; + uint8_t sink; + char name[MQTTD_MAXNAME]; + char host[MQTTD_MAXHOST]; + struct sockaddr_storage ss; + in_port_t port; + uint8_t tls; + uint8_t version; + uint16_t keepalive; + uint32_t max_queued; + char client_id[MQTTD_MAX_CLIENTID + 1]; + char username[MQTTD_MAXNAME]; + char password[MQTTD_MAXPASSWORD]; + char *tls_ca_file; + uint8_t *tls_ca; + size_t tls_ca_len; +}; + +struct bridge_topic_config { + uint32_t bridge_id; + uint8_t dir; + uint8_t qos; + char pattern[MQTTD_MAXTEXT]; + char local_prefix[MQTTD_MAXPREFIX]; + char remote_prefix[MQTTD_MAXPREFIX]; +}; + +struct bridge_topic { + TAILQ_ENTRY(bridge_topic) entry; + struct bridge_topic_config conf; +}; +TAILQ_HEAD(bridge_topics, bridge_topic); + +/* + * Snowflake sink. Rows are inserted through the SQL REST API with + * key-pair JWT authentication; the parent keeps the private key and + * signs tokens for the sink process on request. + */ +struct snow_config { + uint32_t id; + char name[MQTTD_MAXNAME]; + char account[MQTTD_SNOW_IDENT]; + char user[MQTTD_SNOW_IDENT]; + char role[MQTTD_SNOW_IDENT]; + char warehouse[MQTTD_SNOW_IDENT]; + char database[MQTTD_SNOW_IDENT]; + char schema[MQTTD_SNOW_IDENT]; + char table[MQTTD_SNOW_IDENT]; + char host[MQTTD_MAXHOST]; + char servername[MQTTD_MAXHOST]; + in_port_t port; + uint8_t noverify; + uint16_t batch; + uint32_t flush; + uint16_t timeout; + /* parent only */ + char key_file[PATH_MAX]; + uint8_t *key; + size_t key_len; + char *tls_ca_file; + uint8_t *tls_ca; + size_t tls_ca_len; +}; + +struct snow_resolve { + uint32_t id; +}; + +struct snow_resolved { + uint32_t id; + int n; + struct sockaddr_storage addrs[MQTTD_SNOW_MAXADDR]; +}; + +struct snow_jwt_reply { + uint32_t id; + int ok; + time_t expires; + char token[MQTTD_JWT_MAX]; +}; + +/* parent side only */ +struct bridge_def { + TAILQ_ENTRY(bridge_def) entry; + struct bridge_config conf; + struct bridge_topics topics; + struct snow_config *snow; +}; +TAILQ_HEAD(bridge_defs, bridge_def); + +struct ctl_bridge_info { + char name[MQTTD_MAXNAME]; + char remote[MQTTD_MAXPEER]; + uint8_t state; + uint8_t tls; + uint8_t version; + time_t since; + uint32_t attempts; + uint64_t msgs_in; + uint64_t msgs_out; + uint32_t nqueued; +}; + +struct user { + TAILQ_ENTRY(user) entry; + struct user_config conf; +}; +TAILQ_HEAD(userlist, user); + +#define ACL_PUBLISH 0x01 +#define ACL_SUBSCRIBE 0x02 +#define ACL_ALL (ACL_PUBLISH | ACL_SUBSCRIBE) + +struct acl_config { + uint8_t allow; + uint8_t action; + char user[MQTTD_MAXNAME]; + char client[MQTTD_MAX_CLIENTID + 1]; + char pattern[MQTTD_MAXTEXT]; +}; + +struct acl { + TAILQ_ENTRY(acl) entry; + struct acl_config conf; +}; +TAILQ_HEAD(acllist, acl); + +/* + * sockets -> broker: a connection is up; cert_user is the verified CN, + * bridge_id is set when this is an outbound bridge connection. + */ +struct conn_open { + uint32_t id; + uint32_t listener_id; + uint32_t bridge_id; + uint8_t tls; + char peer[MQTTD_MAXPEER]; + char cert_user[MQTTD_MAXNAME]; +}; + +/* both directions: one chunk of a complete MQTT control packet */ +struct conn_pkt { + uint32_t id; + uint32_t total; + uint32_t offset; + uint32_t len; +}; +#define CONN_PKT_CHUNK (MAX_IMSG_DATA_SIZE - sizeof(struct conn_pkt)) + +enum conn_close_reason { + CONN_CLOSE_EOF, + CONN_CLOSE_ERROR, + CONN_CLOSE_TIMEOUT, + CONN_CLOSE_OVERFLOW, + CONN_CLOSE_ADMIN, + CONN_CLOSE_SHUTDOWN, +}; + +/* sockets -> broker: connection is gone; broker -> sockets: flush and close */ +struct conn_close { + uint32_t id; + uint8_t reason; +}; + +enum conn_error_reason { + CONN_ERR_MALFORMED, + CONN_ERR_TOOBIG, +}; + +/* sockets -> broker: framing failed, reading is paused until a close */ +struct conn_error { + uint32_t id; + uint8_t reason; +}; + +/* broker -> sockets: CONNECT accepted, enforce this keepalive (0 = none) */ +struct conn_keepalive { + uint32_t id; + uint16_t keepalive; +}; + +/* + * Snapshot records. The broker serialises its state into a stream of + * typed records which the storage process writes out verbatim and plays + * back at startup; storage never interprets record contents. + */ +enum db_rec_type { + DB_BEGIN, + DB_RETAINED, + DB_SESSION, + DB_SUBSCRIPTION, + DB_DELIVERY, + DB_INCOMING, + DB_END, +}; + +struct db_chunk { + uint32_t type; + uint32_t total; + uint32_t offset; + uint32_t len; +}; +#define DB_CHUNK (MAX_IMSG_DATA_SIZE - sizeof(struct db_chunk)) + +/* on-disk framing in front of every record */ +struct db_rec_hdr { + uint32_t type; + uint32_t len; +}; + +/* control plane: an optional client id or topic to narrow a listing */ +struct ctl_filter { + char name[MQTTD_MAXTEXT]; +}; + +struct ctl_parent_info { + int verbose; + uint16_t prefork; + uint32_t nlisteners; + time_t started; +}; + +struct ctl_control_info { + int verbose; +}; + +struct ctl_broker_info { + int verbose; + time_t started; + uint64_t sessions; + uint64_t connected; + uint64_t subscriptions; + uint64_t retained; + uint64_t pub_received; + uint64_t pub_sent; + uint64_t pub_dropped; + uint64_t bytes_in; + uint64_t bytes_out; + uint64_t snapshots; + time_t last_snapshot; + uint8_t persist; +}; + +struct ctl_listener_info { + uint32_t id; + char name[MQTTD_MAXNAME]; + char addr[MQTTD_MAXPEER]; + in_port_t port; + int tls; + uint8_t websocket; + uint8_t proxy; + int fd; + uint32_t nconns; +}; + +struct ctl_client_info { + char client_id[MQTTD_MAX_CLIENTID + 1]; + char username[MQTTD_MAXNAME]; + char peer[MQTTD_MAXPEER]; + uint8_t version; + uint8_t connected; + uint8_t clean_start; + uint8_t tls; + uint16_t keepalive; + uint32_t expiry; + uint32_t nsubs; + uint32_t ninflight; + uint32_t nqueued; + time_t connected_at; + time_t disconnected_at; + uint64_t msgs_in; + uint64_t msgs_out; + uint64_t dropped; +}; + +struct ctl_sub_info { + char client_id[MQTTD_MAX_CLIENTID + 1]; + char filter[MQTTD_MAXTEXT]; + uint32_t id; + uint8_t qos; + uint8_t no_local; + uint8_t rap; + uint8_t rh; +}; + +struct ctl_retained_info { + char topic[MQTTD_MAXTEXT]; + uint32_t payload_len; + uint32_t expiry; + uint8_t qos; + time_t received; +}; + +struct mqttd { + struct mqttd_global conf; + struct listenerlist listeners; + struct userlist users; + struct acllist acls; + struct bridge_defs bridges; + + const char *conffile; + char dbdir[PATH_MAX]; + char auth_exec[PATH_MAX]; + char pf_table[MQTTD_PF_TABLE_MAX]; + int debug; + int verbose; + int noaction; + + uint32_t nlisteners; + int cfg_pending; + time_t started; +}; + +extern struct mqttd *env; + +/* mqttd.c */ +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_event_shutdown(struct imsgev *); +void proc_privdrop(const char *); +void proc_privdrop_dir(const char *, const char *); +int sockaddr_ntop(struct sockaddr_storage *, char *, size_t); + +/* sockets.c */ +__dead void + sockets(int, int, int); +int sockets_create_listener(struct listener *); + +/* broker.c */ +__dead void + broker(int, int, int, int, int, int *); + +/* storage.c */ +__dead void + storage(int, int); + +/* auth.c */ +__dead void + auth(int, int); + +/* snowflake.c */ +__dead void + snowflake(int, int); + +/* jwt.c */ +int jwt_sign(const struct snow_config *, char *, size_t, time_t *); + +/* control.c */ +__dead void + control(int, int, int *); + +/* config.c */ +void config_init(struct mqttd *); +int config_send_global(struct imsgev *, struct mqttd *); +int config_recv_global(struct mqttd *, struct imsg *); +int config_send_listener(struct imsgev *, struct listener *, int); +int config_send_tls(struct imsgev *, struct listener *); +struct listener + *config_recv_listener(struct mqttd *, struct imsg *); +int config_recv_tls(struct mqttd *, struct imsg *); +int config_send_user(struct imsgev *, struct user *); +int config_send_acl(struct imsgev *, struct acl *); +int config_send_bridge(struct imsgev *, struct bridge_def *); +int config_send_bridge_ca(struct imsgev *, struct bridge_def *); +int config_recv_bridge_ca(struct bridge_config *, struct imsg *); +int config_send_snow_ca(struct imsgev *, struct snow_config *); +void config_free_bridge(struct bridge_def *); +struct listener + *config_listener_byid(struct mqttd *, uint32_t); +void config_free_listener(struct listener *); +void config_free(struct mqttd *); + +/* parse.y */ +int parse_config(const char *, struct mqttd *); +int cmdline_symset(char *); +int host(const char *, struct addresslist *, int, in_port_t, const char *, + int); + +/* tls.c */ +int tls_load_keypair(struct listener *); +int tls_load_ca(struct listener *); +int tls_load_crl(struct listener *); +int tls_load_ocsp(struct listener *); +int listener_tls_load(struct listener *); +int listener_tls_init(struct listener *); +int listener_tls_reload(struct listener *); + +/* 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))); + +#endif /* MQTTD_H */ blob - /dev/null blob + ad98b10190f877f828484e172cb50b6b271aa54be03fd2a506ec5bac2510e429 (mode 644) --- /dev/null +++ mqttd/parse.y @@ -0,0 +1,2130 @@ +/* + * Copyright (c) 2016-2021, 2026 Tracey Emery + * Copyright (c) 2020 Matthias Pressfreund + * Copyright (c) 2007 - 2015 Reyk Floeter + * Copyright (c) 2008 Gilles Chehade + * Copyright (c) 2006 Pierre-Yves Ritschard + * Copyright (c) 2004, 2005 Esben Norby + * Copyright (c) 2004 Ryan McBride + * Copyright (c) 2002, 2003, 2004 Henning Brauer + * Copyright (c) 2001 Markus Friedl. All rights reserved. + * Copyright (c) 2001 Daniel Hartmeier. All rights reserved. + * Copyright (c) 2001 Theo de Raadt. All rights reserved. + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" +#include "mqtt.h" + +TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); +static struct file { + TAILQ_ENTRY(file) entry; + FILE *stream; + char *name; + int lineno; + int errors; +} *file, *topfile; +struct file *pushfile(const char *, int); +int popfile(void); +int check_file_secrecy(int, const char *); +int yyparse(void); +int yylex(void); +int yyerror(const char *, ...) + __attribute__((__format__ (printf, 1, 2))) + __attribute__((__nonnull__ (1))); +int kw_cmp(const void *, const void *); +int lookup(char *); +int lgetc(int); +int lungetc(int); +int findeol(void); + +TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); +struct sym { + TAILQ_ENTRY(sym) entry; + int used; + int persist; + char *nam; + char *val; +}; +int symset(const char *, const char *, int); +char *symget(const char *); + +int getservice(char *); +struct address *host_v4(const char *); +struct address *host_v6(const char *); +int host_dns(const char *, struct addresslist *, int, in_port_t, + const char *, int); +int host_if(const char *, struct addresslist *, int, in_port_t, + const char *, int); +int is_if_in_group(const char *, const char *); + +static void listen_init(void); +static void listen_clear(void); +static int listen_setfile(char **, const char *); + +static struct mqttd *conf; +static struct listener_config lconf; +static struct user *cur_user; +static struct bridge_def *cur_bridge; +static struct bridge_topic *cur_topic; +static struct snow_config *cur_snow; +static uint32_t listener_id; +static uint32_t bridge_id; +static int errors; + +static int snow_set(char *, size_t, char *, const char *); + +typedef struct { + union { + long long number; + char *string; + in_port_t port; + } v; + int lineno; +} YYSTYPE; + +%} + +%token ACCOUNT ACL AFTER ALIAS ALL ALLOW ANONYMOUS ANY AUTH AWARE BAN BATCH +%token BOTH BRIDGE CA CERTIFICATE CIPHERS CLIENT CLIENTS CRL DENY DHE ECDHE +%token ERROR EXEC EXPIRY FAILURES FLUSH HOST IN INCLUDE INFLIGHT INTERVAL +%token KEEPALIVE KEY LISTEN LOCAL MAX NAME NO NOVERIFY OCSP ON OPTIONAL OUT +%token PACKET PASSWORD PATH PERSISTENCE PF PORT PREFORK PROTOCOLS PROXY +%token PUBLISH QOS QUEUED RATE REMOTE RETAIN ROLE SERVERNAME SESSION SIZE +%token SNOWFLAKE SPARKPLUG SUBSCRIBE SYS TABLE TIMEOUT TLS TOPIC USER VERSION +%token WAREHOUSE WEBSOCKET WITHIN YES + +%token STRING +%token NUMBER +%type string +%type yesno permit aclaction direction optqos bridgetls +%type port optport + +%% + +grammar : /* empty */ + | grammar include '\n' + | grammar '\n' + | grammar varset '\n' + | grammar main '\n' + | grammar listen '\n' + | grammar acl '\n' + | grammar bridge '\n' + | grammar snowflake '\n' + | grammar error '\n' { file->errors++; } + ; + +include : INCLUDE STRING { + struct file *nfile; + + if ((nfile = pushfile($2, 0)) == NULL) { + yyerror("failed to include file %s", $2); + free($2); + YYERROR; + } + free($2); + + file = nfile; + lungetc('\n'); + } + ; + +varset : STRING '=' string { + char *s = $1; + if (conf->verbose) + printf("%s = \"%s\"\n", $1, $3); + while (*s++) { + if (isspace((unsigned char)*s)) { + yyerror("macro name cannot contain " + "whitespace"); + free($1); + free($3); + YYERROR; + } + } + if (symset($1, $3, 0) == -1) + fatal("cannot store variable"); + free($1); + free($3); + } + ; + +string : string STRING { + if (asprintf(&$$, "%s %s", $1, $2) == -1) + fatal("string: asprintf"); + free($1); + free($2); + } + | STRING + ; + +yesno : YES { $$ = 1; } + | NO { $$ = 0; } + ; + +main : PREFORK NUMBER { + if ($2 <= 0 || $2 > MQTTD_MAX_PREFORK) { + yyerror("invalid number of preforked " + "sockets processes: %lld", $2); + YYERROR; + } + conf->conf.prefork = $2; + } + | MAX CLIENTS NUMBER { + if ($3 < 0 || $3 > UINT32_MAX) { + yyerror("invalid max clients: %lld", $3); + YYERROR; + } + conf->conf.max_clients = $3; + } + | MAX PACKET SIZE NUMBER { + if ($4 < 64 || $4 > (long long)MQTT_MAX_REMAINING + 5) { + yyerror("invalid max packet size: %lld", $4); + YYERROR; + } + conf->conf.max_packet_size = $4; + } + | MAX INFLIGHT NUMBER { + if ($3 < 1 || $3 > UINT16_MAX) { + yyerror("invalid max inflight: %lld", $3); + YYERROR; + } + conf->conf.max_inflight = $3; + } + | MAX QOS NUMBER { + if ($3 < 0 || $3 > 2) { + yyerror("invalid max qos: %lld", $3); + YYERROR; + } + conf->conf.max_qos = $3; + } + | MAX KEEPALIVE NUMBER { + if ($3 < 0 || $3 > UINT16_MAX) { + yyerror("invalid max keepalive: %lld", $3); + YYERROR; + } + conf->conf.max_keepalive = $3; + } + | MAX QUEUED NUMBER { + if ($3 < 0 || $3 > UINT32_MAX) { + yyerror("invalid max queued: %lld", $3); + YYERROR; + } + conf->conf.max_queued = $3; + } + | MAX SESSION EXPIRY NUMBER { + if ($4 < 0 || $4 >= UINT32_MAX) { + yyerror("invalid max session expiry: %lld", $4); + YYERROR; + } + conf->conf.max_session_expiry = $4; + } + | MAX TOPIC ALIAS NUMBER { + if ($4 < 0 || $4 > UINT16_MAX) { + yyerror("invalid max topic alias: %lld", $4); + YYERROR; + } + conf->conf.topic_alias_max = $4; + } + | SYS INTERVAL NUMBER { + if ($3 < 0 || $3 > UINT16_MAX) { + yyerror("invalid sys interval: %lld", $3); + YYERROR; + } + conf->conf.sys_interval = $3; + } + | PERSISTENCE yesno { + conf->conf.persist = $2; + } + | PERSISTENCE PATH STRING { + if ($3[0] != '/') { + yyerror("persistence path must be absolute"); + free($3); + YYERROR; + } + if (strlcpy(conf->dbdir, $3, sizeof(conf->dbdir)) >= + sizeof(conf->dbdir)) { + yyerror("persistence path too long"); + free($3); + YYERROR; + } + free($3); + } + | PERSISTENCE INTERVAL NUMBER { + if ($3 < 1 || $3 > UINT16_MAX) { + yyerror("invalid persistence interval: %lld", + $3); + YYERROR; + } + conf->conf.persist_interval = $3; + } + | MAX RATE NUMBER { + if ($3 < 0 || $3 > UINT32_MAX) { + yyerror("invalid max rate: %lld", $3); + YYERROR; + } + conf->conf.max_rate = $3; + } + | SPARKPLUG AWARE yesno { + conf->conf.sparkplug = $3; + } + | AUTH EXEC STRING { + if ($3[0] != '/') { + yyerror("auth exec path must be absolute"); + free($3); + YYERROR; + } + if (strlcpy(conf->auth_exec, $3, + sizeof(conf->auth_exec)) >= sizeof(conf->auth_exec)) { + yyerror("auth exec path too long"); + free($3); + YYERROR; + } + conf->conf.auth_exec_set = 1; + free($3); + } + | AUTH BAN AFTER NUMBER FAILURES WITHIN NUMBER PF TABLE STRING { + if ($4 < 1 || $4 > UINT16_MAX || + $7 < 1 || $7 > UINT16_MAX) { + yyerror("invalid auth ban thresholds"); + free($10); + YYERROR; + } + if (strlen($10) == 0 || + strlcpy(conf->pf_table, $10, + sizeof(conf->pf_table)) >= sizeof(conf->pf_table)) { + yyerror("invalid pf table name: %s", $10); + free($10); + YYERROR; + } + conf->conf.ban_failures = $4; + conf->conf.ban_within = $7; + free($10); + } + | RETAIN yesno { + conf->conf.retain_available = $2; + } + | ANONYMOUS yesno { + conf->conf.anonymous = $2; + } + | USER STRING PASSWORD STRING { + struct user *u; + + if (strlen($2) == 0 || + strlen($2) >= MQTTD_MAXNAME) { + yyerror("invalid user name: %s", $2); + free($2); + free($4); + YYERROR; + } + if ($4[0] != '$' || strlen($4) >= MQTTD_MAXHASH) { + yyerror("password for %s is not a crypt(3) " + "hash, see encrypt(1)", $2); + free($2); + free($4); + YYERROR; + } + TAILQ_FOREACH(u, &conf->users, entry) { + if (strcmp(u->conf.name, $2) == 0) { + yyerror("duplicate user: %s", $2); + free($2); + free($4); + YYERROR; + } + } + if ((u = calloc(1, sizeof(*u))) == NULL) + fatal("%s: calloc", __func__); + strlcpy(u->conf.name, $2, sizeof(u->conf.name)); + strlcpy(u->conf.hash, $4, sizeof(u->conf.hash)); + TAILQ_INSERT_TAIL(&conf->users, u, entry); + explicit_bzero($4, strlen($4)); + free($2); + free($4); + cur_user = u; + } useropts + ; + +useropts : /* empty */ + | '{' optnl useropts_l '}' + ; + +useropts_l : useropts_l useropt nl + | useropt optnl + ; + +useropt : MAX CLIENTS NUMBER { + if ($3 < 0 || $3 > UINT32_MAX) { + yyerror("invalid user max clients: %lld", $3); + YYERROR; + } + cur_user->conf.max_clients = $3; + } + | RATE NUMBER { + if ($2 < 0 || $2 > UINT32_MAX) { + yyerror("invalid user rate: %lld", $2); + YYERROR; + } + cur_user->conf.rate = $2; + } + ; + +listen : LISTEN { + listen_init(); + } ON STRING listenflags optport optname listenopts { + struct addresslist al; + struct address *a; + struct listener *l; + struct sockaddr_un *sun; + + if ($4[0] == '/') { + /* a unix socket: one listener, no network */ + if (lconf.tls || lconf.websocket || lconf.proxy) { + yyerror("unix listeners take no tls, " + "websocket or proxy"); + free($4); + listen_clear(); + YYERROR; + } + if ((l = calloc(1, sizeof(*l))) == NULL) + fatal("%s: calloc", __func__); + memcpy(&l->conf, &lconf, sizeof(l->conf)); + l->conf.id = ++listener_id; + l->fd = -1; + sun = (struct sockaddr_un *)&l->conf.ss; + sun->sun_family = AF_UNIX; + sun->sun_len = sizeof(*sun); + if (strlcpy(sun->sun_path, $4, + sizeof(sun->sun_path)) >= + sizeof(sun->sun_path)) { + yyerror("unix socket path too long"); + free(l); + free($4); + listen_clear(); + YYERROR; + } + if (l->conf.name[0] == '\0') + strlcpy(l->conf.name, $4, + sizeof(l->conf.name)); + TAILQ_INSERT_TAIL(&conf->listeners, l, entry); + conf->nlisteners++; + free($4); + listen_clear(); + break; + } + + TAILQ_INIT(&al); + if (strcmp($4, "*") == 0) { + if (host("0.0.0.0", &al, 1, 0, NULL, -1) <= 0 || + host("::", &al, 1, 0, NULL, -1) <= 0) { + yyerror("invalid listen address"); + free($4); + listen_clear(); + YYERROR; + } + } else if (host($4, &al, MQTTD_MAXIFACE, 0, NULL, + -1) <= 0) { + yyerror("invalid listen address: %s", $4); + free($4); + listen_clear(); + YYERROR; + } + + if (lconf.name[0] == '\0' && + strlcpy(lconf.name, $4, sizeof(lconf.name)) >= + sizeof(lconf.name)) { + yyerror("listener name too long"); + free($4); + listen_clear(); + YYERROR; + } + if ($6 != 0) + lconf.port = $6; + else if (lconf.websocket) + lconf.port = htons(lconf.tls ? MQTT_WSS_PORT : + MQTT_WS_PORT); + else + lconf.port = htons(lconf.tls ? MQTTS_PORT : + MQTT_PORT); + + while ((a = TAILQ_FIRST(&al)) != NULL) { + TAILQ_REMOVE(&al, a, entry); + if ((l = calloc(1, sizeof(*l))) == NULL) + fatal("%s: calloc", __func__); + memcpy(&l->conf, &lconf, sizeof(l->conf)); + l->conf.id = ++listener_id; + memcpy(&l->conf.ss, &a->ss, sizeof(l->conf.ss)); + l->fd = -1; + + /* every listener owns its own file names */ + l->conf.tls_cert_file = NULL; + l->conf.tls_key_file = NULL; + l->conf.tls_ca_file = NULL; + l->conf.tls_crl_file = NULL; + l->conf.tls_ocsp_staple_file = NULL; + if (lconf.tls) { + if (listen_setfile(&l->conf.tls_cert_file, + lconf.tls_cert_file) == -1 || + listen_setfile(&l->conf.tls_key_file, + lconf.tls_key_file) == -1 || + listen_setfile(&l->conf.tls_ca_file, + lconf.tls_ca_file) == -1 || + listen_setfile(&l->conf.tls_crl_file, + lconf.tls_crl_file) == -1 || + listen_setfile( + &l->conf.tls_ocsp_staple_file, + lconf.tls_ocsp_staple_file) == -1) + fatal("%s: strdup", __func__); + } + + TAILQ_INSERT_TAIL(&conf->listeners, l, entry); + conf->nlisteners++; + free(a); + } + free($4); + listen_clear(); + } + ; + +listenflags : /* empty */ + | listenflags listenflag + ; + +listenflag : WEBSOCKET { lconf.websocket = 1; } + | PROXY { lconf.proxy = 1; } + | TLS { + if (lconf.tls) + break; + lconf.tls = 1; + if (listen_setfile(&lconf.tls_cert_file, TLS_CERT) == -1 || + listen_setfile(&lconf.tls_key_file, TLS_KEY) == -1) + fatal("%s: strdup", __func__); + strlcpy(lconf.tls_ciphers, TLS_CIPHERS, + sizeof(lconf.tls_ciphers)); + strlcpy(lconf.tls_dhe_params, TLS_DHE_PARAMS, + sizeof(lconf.tls_dhe_params)); + strlcpy(lconf.tls_ecdhe_curves, TLS_ECDHE_CURVES, + sizeof(lconf.tls_ecdhe_curves)); + lconf.tls_protocols = TLS_PROTOCOLS_DEFAULT; + } + ; + +optport : /* empty */ { $$ = 0; } + | PORT port { $$ = $2; } + ; + +port : NUMBER { + if ($1 <= 0 || $1 > (int)USHRT_MAX) { + yyerror("invalid port: %lld", $1); + YYERROR; + } + $$ = htons($1); + } + | STRING { + int val; + + if ((val = getservice($1)) == -1) { + yyerror("invalid port: %s", $1); + free($1); + YYERROR; + } + free($1); + $$ = val; + } + ; + +optname : /* empty */ + | NAME STRING { + if (strlcpy(lconf.name, $2, sizeof(lconf.name)) >= + sizeof(lconf.name)) { + yyerror("listener name too long"); + free($2); + YYERROR; + } + free($2); + } + ; + +listenopts : /* empty */ + | '{' optnl listenopts_l '}' + ; + +listenopts_l : listenopts_l listenopt nl + | listenopt optnl + ; + +listenopt : TLS tlsopt + ; + +tlsopt : CERTIFICATE STRING { + if (!lconf.tls) { + yyerror("tls options require a tls listener"); + free($2); + YYERROR; + } + if (listen_setfile(&lconf.tls_cert_file, $2) == -1) + fatal("%s: strdup", __func__); + free($2); + } + | KEY STRING { + if (!lconf.tls) { + yyerror("tls options require a tls listener"); + free($2); + YYERROR; + } + if (listen_setfile(&lconf.tls_key_file, $2) == -1) + fatal("%s: strdup", __func__); + free($2); + } + | CIPHERS STRING { + if (!lconf.tls) { + yyerror("tls options require a tls listener"); + free($2); + YYERROR; + } + if (strlcpy(lconf.tls_ciphers, $2, + sizeof(lconf.tls_ciphers)) >= + sizeof(lconf.tls_ciphers)) { + yyerror("tls ciphers too long"); + free($2); + YYERROR; + } + free($2); + } + | DHE STRING { + if (!lconf.tls) { + yyerror("tls options require a tls listener"); + free($2); + YYERROR; + } + if (strlcpy(lconf.tls_dhe_params, $2, + sizeof(lconf.tls_dhe_params)) >= + sizeof(lconf.tls_dhe_params)) { + yyerror("tls dhe too long"); + free($2); + YYERROR; + } + free($2); + } + | ECDHE STRING { + if (!lconf.tls) { + yyerror("tls options require a tls listener"); + free($2); + YYERROR; + } + if (strlcpy(lconf.tls_ecdhe_curves, $2, + sizeof(lconf.tls_ecdhe_curves)) >= + sizeof(lconf.tls_ecdhe_curves)) { + yyerror("tls ecdhe too long"); + free($2); + YYERROR; + } + free($2); + } + | PROTOCOLS STRING { + if (!lconf.tls) { + yyerror("tls options require a tls listener"); + free($2); + YYERROR; + } + if (tls_config_parse_protocols(&lconf.tls_protocols, + $2) != 0) { + yyerror("invalid tls protocols: %s", $2); + free($2); + YYERROR; + } + free($2); + } + | OCSP STRING { + if (!lconf.tls) { + yyerror("tls options require a tls listener"); + free($2); + YYERROR; + } + if (listen_setfile(&lconf.tls_ocsp_staple_file, + $2) == -1) + fatal("%s: strdup", __func__); + free($2); + } + | CLIENT CA STRING tlscltopt { + if (!lconf.tls) { + yyerror("tls options require a tls listener"); + free($3); + YYERROR; + } + if (listen_setfile(&lconf.tls_ca_file, $3) == -1) + fatal("%s: strdup", __func__); + lconf.tls_flags |= TLSFLAG_CA; + free($3); + } + ; + +tlscltopt : /* empty */ + | tlscltopt CRL STRING { + if (listen_setfile(&lconf.tls_crl_file, $3) == -1) + fatal("%s: strdup", __func__); + lconf.tls_flags |= TLSFLAG_CRL; + free($3); + } + | tlscltopt OPTIONAL { + lconf.tls_flags |= TLSFLAG_OPTIONAL; + } + ; + +acl : ACL '{' optnl aclrules '}' + ; + +aclrules : aclrules aclrule nl + | aclrule optnl + ; + +aclrule : permit aclsubject aclaction STRING { + struct acl *a; + + a = TAILQ_LAST(&conf->acls, acllist); + if (!mqtt_filter_valid((const uint8_t *)$4, + strlen($4)) || strlen($4) >= MQTTD_MAXTEXT) { + yyerror("invalid acl topic pattern: %s", $4); + TAILQ_REMOVE(&conf->acls, a, entry); + free(a); + free($4); + YYERROR; + } + a->conf.allow = $1; + a->conf.action = $3; + strlcpy(a->conf.pattern, $4, sizeof(a->conf.pattern)); + free($4); + } + ; + +permit : ALLOW { $$ = 1; } + | DENY { $$ = 0; } + ; + +/* the rule is allocated here so the subject can fill in the user */ +aclsubject : ANY { + struct acl *a; + + if ((a = calloc(1, sizeof(*a))) == NULL) + fatal("%s: calloc", __func__); + TAILQ_INSERT_TAIL(&conf->acls, a, entry); + } + | USER STRING { + struct acl *a; + + if (strlen($2) == 0 || strlen($2) >= MQTTD_MAXNAME) { + yyerror("invalid acl user: %s", $2); + free($2); + YYERROR; + } + if ((a = calloc(1, sizeof(*a))) == NULL) + fatal("%s: calloc", __func__); + strlcpy(a->conf.user, $2, sizeof(a->conf.user)); + TAILQ_INSERT_TAIL(&conf->acls, a, entry); + free($2); + } + | CLIENT STRING { + struct acl *a; + + if (strlen($2) == 0 || + strlen($2) > MQTTD_MAX_CLIENTID) { + yyerror("invalid acl client id: %s", $2); + free($2); + YYERROR; + } + if ((a = calloc(1, sizeof(*a))) == NULL) + fatal("%s: calloc", __func__); + strlcpy(a->conf.client, $2, sizeof(a->conf.client)); + TAILQ_INSERT_TAIL(&conf->acls, a, entry); + free($2); + } + ; + +aclaction : PUBLISH { $$ = ACL_PUBLISH; } + | SUBSCRIBE { $$ = ACL_SUBSCRIBE; } + | ALL { $$ = ACL_ALL; } + ; + +bridge : BRIDGE STRING { + struct bridge_def *b; + + if (strlen($2) == 0 || strlen($2) >= MQTTD_MAXNAME) { + yyerror("invalid bridge name: %s", $2); + free($2); + YYERROR; + } + TAILQ_FOREACH(b, &conf->bridges, entry) { + if (strcmp(b->conf.name, $2) == 0) { + yyerror("duplicate bridge: %s", $2); + free($2); + YYERROR; + } + } + if ((b = calloc(1, sizeof(*b))) == NULL) + fatal("%s: calloc", __func__); + TAILQ_INIT(&b->topics); + strlcpy(b->conf.name, $2, sizeof(b->conf.name)); + snprintf(b->conf.client_id, sizeof(b->conf.client_id), + "mqttd-bridge-%s", $2); + b->conf.id = ++bridge_id; + b->conf.version = MQTT_V311; + b->conf.keepalive = MQTTD_BRIDGE_KEEPALIVE; + TAILQ_INSERT_TAIL(&conf->bridges, b, entry); + cur_bridge = b; + free($2); + } '{' optnl bridgeopts_l '}' { + if (cur_bridge->conf.host[0] == '\0') { + yyerror("bridge %s has no remote", + cur_bridge->conf.name); + YYERROR; + } + if (TAILQ_EMPTY(&cur_bridge->topics)) { + yyerror("bridge %s has no topics", + cur_bridge->conf.name); + YYERROR; + } + cur_bridge = NULL; + } + ; + +bridgeopts_l : bridgeopts_l bridgeopt nl + | bridgeopt optnl + ; + +bridgetls : /* empty */ { $$ = 0; } + | TLS { $$ = 1; } + ; + +bridgeopt : REMOTE STRING bridgetls optport { + struct addresslist al; + struct address *a; + + TAILQ_INIT(&al); + if (host($2, &al, 1, 0, NULL, -1) <= 0) { + yyerror("bridge remote %s does not resolve", + $2); + free($2); + YYERROR; + } + a = TAILQ_FIRST(&al); + memcpy(&cur_bridge->conf.ss, &a->ss, + sizeof(cur_bridge->conf.ss)); + while ((a = TAILQ_FIRST(&al)) != NULL) { + TAILQ_REMOVE(&al, a, entry); + free(a); + } + if (strlcpy(cur_bridge->conf.host, $2, + sizeof(cur_bridge->conf.host)) >= + sizeof(cur_bridge->conf.host)) { + yyerror("bridge remote name too long"); + free($2); + YYERROR; + } + cur_bridge->conf.tls = $3; + cur_bridge->conf.port = $4 != 0 ? $4 : + htons($3 ? MQTTS_PORT : MQTT_PORT); + free($2); + } + | CLIENT STRING { + if (strlen($2) == 0 || + strlen($2) > MQTTD_MAX_CLIENTID) { + yyerror("invalid bridge client id: %s", $2); + free($2); + YYERROR; + } + strlcpy(cur_bridge->conf.client_id, $2, + sizeof(cur_bridge->conf.client_id)); + free($2); + } + | USER STRING PASSWORD STRING { + if (strlen($2) >= MQTTD_MAXNAME || + strlen($4) >= MQTTD_MAXPASSWORD) { + yyerror("bridge credentials too long"); + free($2); + free($4); + YYERROR; + } + strlcpy(cur_bridge->conf.username, $2, + sizeof(cur_bridge->conf.username)); + strlcpy(cur_bridge->conf.password, $4, + sizeof(cur_bridge->conf.password)); + explicit_bzero($4, strlen($4)); + free($2); + free($4); + } + | KEEPALIVE NUMBER { + if ($2 < 1 || $2 > UINT16_MAX) { + yyerror("invalid bridge keepalive: %lld", $2); + YYERROR; + } + cur_bridge->conf.keepalive = $2; + } + | MAX QUEUED NUMBER { + if ($3 < 1 || $3 > UINT32_MAX) { + yyerror("invalid bridge max queued: %lld", $3); + YYERROR; + } + cur_bridge->conf.max_queued = $3; + } + | VERSION NUMBER { + if ($2 != 3 && $2 != 4 && $2 != 5) { + yyerror("bridge version must be 3, 4 or 5"); + YYERROR; + } + cur_bridge->conf.version = $2; + } + | TLS CA STRING { + free(cur_bridge->conf.tls_ca_file); + if ((cur_bridge->conf.tls_ca_file = strdup($3)) == NULL) + fatal("%s: strdup", __func__); + free($3); + } + | TOPIC STRING direction optqos { + struct bridge_topic *bt; + + if (strlen($2) >= MQTTD_MAXTEXT || + !mqtt_filter_valid((const uint8_t *)$2, + strlen($2))) { + yyerror("invalid bridge topic: %s", $2); + free($2); + YYERROR; + } + if ((bt = calloc(1, sizeof(*bt))) == NULL) + fatal("%s: calloc", __func__); + bt->conf.bridge_id = cur_bridge->conf.id; + bt->conf.dir = $3; + bt->conf.qos = $4; + strlcpy(bt->conf.pattern, $2, sizeof(bt->conf.pattern)); + TAILQ_INSERT_TAIL(&cur_bridge->topics, bt, entry); + cur_topic = bt; + free($2); + } optprefixes + ; + +direction : IN { $$ = BRIDGE_IN; } + | OUT { $$ = BRIDGE_OUT; } + | BOTH { $$ = BRIDGE_BOTH; } + ; + +optqos : /* empty */ { $$ = 0; } + | QOS NUMBER { + if ($2 < 0 || $2 > 2) { + yyerror("invalid bridge qos: %lld", $2); + YYERROR; + } + $$ = $2; + } + ; + +/* + * A Snowflake sink is a bridge whose remote is the snowflake process: + * version 5 so the sink can announce its batch size as Receive Maximum, + * no keepalive because the "remote" is local. + */ +snowflake : SNOWFLAKE STRING { + struct bridge_def *b; + + if (strlen($2) == 0 || strlen($2) >= MQTTD_MAXNAME) { + yyerror("invalid snowflake name: %s", $2); + free($2); + YYERROR; + } + TAILQ_FOREACH(b, &conf->bridges, entry) { + if (strcmp(b->conf.name, $2) == 0) { + yyerror("duplicate bridge or snowflake " + "name: %s", $2); + free($2); + YYERROR; + } + } + if ((b = calloc(1, sizeof(*b))) == NULL || + (b->snow = calloc(1, sizeof(*b->snow))) == NULL) + fatal("%s: calloc", __func__); + TAILQ_INIT(&b->topics); + strlcpy(b->conf.name, $2, sizeof(b->conf.name)); + snprintf(b->conf.client_id, sizeof(b->conf.client_id), + "mqttd-snowflake-%s", $2); + b->conf.id = ++bridge_id; + b->conf.sink = 1; + b->conf.proc = MQTTD_PROC_SNOW; + b->conf.version = MQTT_V5; + b->conf.keepalive = 0; + b->conf.tls = 1; + b->snow->id = b->conf.id; + strlcpy(b->snow->name, $2, sizeof(b->snow->name)); + b->snow->port = htons(443); + b->snow->batch = MQTTD_SNOW_DEFAULT_BATCH; + b->snow->flush = MQTTD_SNOW_DEFAULT_FLUSH; + b->snow->timeout = MQTTD_SNOW_DEFAULT_TIMEOUT; + TAILQ_INSERT_TAIL(&conf->bridges, b, entry); + cur_bridge = b; + cur_snow = b->snow; + free($2); + } '{' optnl snowopts_l '}' { + struct snow_config *s = cur_snow; + size_t i; + + if (s->account[0] == '\0' || s->user[0] == '\0' || + s->key_file[0] == '\0' || s->table[0] == '\0') { + yyerror("snowflake %s needs account, user, key " + "and table", s->name); + YYERROR; + } + if (TAILQ_EMPTY(&cur_bridge->topics)) { + yyerror("snowflake %s has no topics", s->name); + YYERROR; + } + if (s->host[0] == '\0') { + /* org-account.snowflakecomputing.com */ + for (i = 0; s->account[i] != '\0' && + i < sizeof(s->host) - 1; i++) + s->host[i] = tolower((unsigned char) + s->account[i]); + s->host[i] = '\0'; + if (strlcat(s->host, MQTTD_SNOW_SUFFIX, + sizeof(s->host)) >= sizeof(s->host)) { + yyerror("snowflake %s host too long", + s->name); + YYERROR; + } + } + strlcpy(cur_bridge->conf.host, s->host, + sizeof(cur_bridge->conf.host)); + cur_bridge->conf.port = s->port; + cur_bridge = NULL; + cur_snow = NULL; + } + ; + +snowopts_l : snowopts_l snowopt nl + | snowopt optnl + ; + +snowopt : ACCOUNT STRING { + if (snow_set(cur_snow->account, + sizeof(cur_snow->account), $2, "account") == -1) + YYERROR; + } + | HOST STRING optport { + if (snow_set(cur_snow->host, sizeof(cur_snow->host), + $2, "host") == -1) + YYERROR; + if ($3 != 0) + cur_snow->port = $3; + } + | USER STRING { + if (snow_set(cur_snow->user, sizeof(cur_snow->user), $2, + "user") == -1) + YYERROR; + } + | ROLE STRING { + if (snow_set(cur_snow->role, sizeof(cur_snow->role), $2, + "role") == -1) + YYERROR; + } + | WAREHOUSE STRING { + if (snow_set(cur_snow->warehouse, + sizeof(cur_snow->warehouse), $2, "warehouse") == -1) + YYERROR; + } + | KEY STRING { + if ($2[0] != '/') { + yyerror("snowflake key path must be absolute"); + free($2); + YYERROR; + } + if (snow_set(cur_snow->key_file, + sizeof(cur_snow->key_file), $2, "key") == -1) + YYERROR; + } + | TABLE STRING { + char *p = $2, *db, *sc, *tb; + + db = strsep(&p, "."); + sc = strsep(&p, "."); + tb = strsep(&p, "."); + if (db == NULL || sc == NULL || tb == NULL || + p != NULL || *db == '\0' || *sc == '\0' || + *tb == '\0') { + yyerror("snowflake table must be " + "DATABASE.SCHEMA.TABLE"); + free($2); + YYERROR; + } + if (strlcpy(cur_snow->database, db, + sizeof(cur_snow->database)) >= + sizeof(cur_snow->database) || + strlcpy(cur_snow->schema, sc, + sizeof(cur_snow->schema)) >= + sizeof(cur_snow->schema) || + strlcpy(cur_snow->table, tb, + sizeof(cur_snow->table)) >= + sizeof(cur_snow->table)) { + yyerror("snowflake table name too long"); + free($2); + YYERROR; + } + free($2); + } + | BATCH NUMBER { + if ($2 < 1 || $2 > UINT16_MAX) { + yyerror("invalid snowflake batch: %lld", $2); + YYERROR; + } + cur_snow->batch = $2; + } + | FLUSH NUMBER { + if ($2 < 1 || $2 > MQTTD_SNOW_MAXFLUSH) { + yyerror("invalid snowflake flush: %lld", $2); + YYERROR; + } + cur_snow->flush = $2; + } + | MAX QUEUED NUMBER { + if ($3 < 1 || $3 > UINT32_MAX) { + yyerror("invalid snowflake max queued: %lld", + $3); + YYERROR; + } + cur_bridge->conf.max_queued = $3; + } + | TIMEOUT NUMBER { + if ($2 < 1 || $2 > 3600) { + yyerror("invalid snowflake timeout: %lld", $2); + YYERROR; + } + cur_snow->timeout = $2; + } + | TLS CA STRING { + free(cur_snow->tls_ca_file); + if ((cur_snow->tls_ca_file = strdup($3)) == NULL) + fatal("%s: strdup", __func__); + free($3); + } + | TLS SERVERNAME STRING { + if (snow_set(cur_snow->servername, + sizeof(cur_snow->servername), $3, + "servername") == -1) + YYERROR; + } + | TLS NOVERIFY { + cur_snow->noverify = 1; + } + | TOPIC STRING optqos { + struct bridge_topic *bt; + + if (strlen($2) >= MQTTD_MAXTEXT || + !mqtt_filter_valid((const uint8_t *)$2, + strlen($2))) { + yyerror("invalid snowflake topic: %s", $2); + free($2); + YYERROR; + } + if ($3 > 1) { + yyerror("snowflake topics take qos 0 or 1"); + free($2); + YYERROR; + } + if ((bt = calloc(1, sizeof(*bt))) == NULL) + fatal("%s: calloc", __func__); + bt->conf.bridge_id = cur_bridge->conf.id; + bt->conf.dir = BRIDGE_OUT; + bt->conf.qos = $3; + strlcpy(bt->conf.pattern, $2, sizeof(bt->conf.pattern)); + TAILQ_INSERT_TAIL(&cur_bridge->topics, bt, entry); + cur_topic = bt; + free($2); + } optlocal + ; + +optlocal : /* empty */ + | LOCAL STRING { + if (strlcpy(cur_topic->conf.local_prefix, $2, + sizeof(cur_topic->conf.local_prefix)) >= + sizeof(cur_topic->conf.local_prefix)) { + yyerror("local prefix too long"); + free($2); + YYERROR; + } + free($2); + } + ; + +optprefixes : /* empty */ + | optprefixes LOCAL STRING { + if (strlcpy(cur_topic->conf.local_prefix, $3, + sizeof(cur_topic->conf.local_prefix)) >= + sizeof(cur_topic->conf.local_prefix)) { + yyerror("local prefix too long"); + free($3); + YYERROR; + } + free($3); + } + | optprefixes REMOTE STRING { + if (strlcpy(cur_topic->conf.remote_prefix, $3, + sizeof(cur_topic->conf.remote_prefix)) >= + sizeof(cur_topic->conf.remote_prefix)) { + yyerror("remote prefix too long"); + free($3); + YYERROR; + } + free($3); + } + ; + +optnl : '\n' optnl + | /* empty */ + ; + +nl : '\n' optnl + ; + +%% + +struct keywords { + const char *k_name; + int k_val; +}; + +int +yyerror(const char *fmt, ...) +{ + va_list ap; + char *msg; + + file->errors++; + va_start(ap, fmt); + if (vasprintf(&msg, fmt, ap) == -1) + fatalx("yyerror vasprintf"); + va_end(ap); + logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg); + free(msg); + return (0); +} + +int +kw_cmp(const void *k, const void *e) +{ + return (strcmp(k, ((const struct keywords *)e)->k_name)); +} + +int +lookup(char *s) +{ + /* This has to be sorted always. */ + static const struct keywords keywords[] = { + { "account", ACCOUNT }, + { "acl", ACL }, + { "after", AFTER }, + { "alias", ALIAS }, + { "all", ALL }, + { "allow", ALLOW }, + { "anonymous", ANONYMOUS }, + { "any", ANY }, + { "auth", AUTH }, + { "aware", AWARE }, + { "ban", BAN }, + { "batch", BATCH }, + { "both", BOTH }, + { "bridge", BRIDGE }, + { "ca", CA }, + { "certificate", CERTIFICATE }, + { "ciphers", CIPHERS }, + { "client", CLIENT }, + { "clients", CLIENTS }, + { "crl", CRL }, + { "deny", DENY }, + { "dhe", DHE }, + { "ecdhe", ECDHE }, + { "exec", EXEC }, + { "expiry", EXPIRY }, + { "failures", FAILURES }, + { "flush", FLUSH }, + { "host", HOST }, + { "in", IN }, + { "include", INCLUDE }, + { "inflight", INFLIGHT }, + { "interval", INTERVAL }, + { "keepalive", KEEPALIVE }, + { "key", KEY }, + { "listen", LISTEN }, + { "local", LOCAL }, + { "max", MAX }, + { "name", NAME }, + { "no", NO }, + { "noverify", NOVERIFY }, + { "ocsp", OCSP }, + { "on", ON }, + { "optional", OPTIONAL }, + { "out", OUT }, + { "packet", PACKET }, + { "password", PASSWORD }, + { "path", PATH }, + { "persistence", PERSISTENCE }, + { "pf", PF }, + { "port", PORT }, + { "prefork", PREFORK }, + { "protocols", PROTOCOLS }, + { "proxy", PROXY }, + { "publish", PUBLISH }, + { "qos", QOS }, + { "queued", QUEUED }, + { "rate", RATE }, + { "remote", REMOTE }, + { "retain", RETAIN }, + { "role", ROLE }, + { "servername", SERVERNAME }, + { "session", SESSION }, + { "size", SIZE }, + { "snowflake", SNOWFLAKE }, + { "sparkplug", SPARKPLUG }, + { "subscribe", SUBSCRIBE }, + { "sys", SYS }, + { "table", TABLE }, + { "timeout", TIMEOUT }, + { "tls", TLS }, + { "topic", TOPIC }, + { "user", USER }, + { "version", VERSION }, + { "warehouse", WAREHOUSE }, + { "websocket", WEBSOCKET }, + { "within", WITHIN }, + { "yes", YES } + }; + const struct keywords *p; + + p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]), + sizeof(keywords[0]), kw_cmp); + + if (p) + return (p->k_val); + else + return (STRING); +} + +#define MAXPUSHBACK 128 + +unsigned char *parsebuf; +int parseindex; +unsigned char pushback_buffer[MAXPUSHBACK]; +int pushback_index = 0; + +int +lgetc(int quotec) +{ + int c, next; + + if (parsebuf) { + /* Read character from the parsebuffer instead of input. */ + if (parseindex >= 0) { + c = parsebuf[parseindex++]; + if (c != '\0') + return (c); + parsebuf = NULL; + } else + parseindex++; + } + + if (pushback_index) + return (pushback_buffer[--pushback_index]); + + if (quotec) { + if ((c = getc(file->stream)) == EOF) { + yyerror("reached end of file while parsing " + "quoted string"); + if (file == topfile || popfile() == EOF) + return (EOF); + return (quotec); + } + return (c); + } + + while ((c = getc(file->stream)) == '\\') { + next = getc(file->stream); + if (next != '\n') { + c = next; + break; + } + yylval.lineno = file->lineno; + file->lineno++; + } + + while (c == EOF) { + if (file == topfile || popfile() == EOF) + return (EOF); + c = getc(file->stream); + } + return (c); +} + +int +lungetc(int c) +{ + if (c == EOF) + return (EOF); + if (parsebuf) { + parseindex--; + if (parseindex >= 0) + return (c); + } + if (pushback_index < MAXPUSHBACK-1) + return (pushback_buffer[pushback_index++] = c); + else + return (EOF); +} + +int +findeol(void) +{ + int c; + + parsebuf = NULL; + + /* Skip to either EOF or the first real EOL. */ + while (1) { + if (pushback_index) + c = pushback_buffer[--pushback_index]; + else + c = lgetc(0); + if (c == '\n') { + file->lineno++; + break; + } + if (c == EOF) + break; + } + return (ERROR); +} + +int +yylex(void) +{ + unsigned char buf[8096]; + unsigned char *p, *val; + int quotec, next, c; + int token; + +top: + p = buf; + while ((c = lgetc(0)) == ' ' || c == '\t') + ; /* nothing */ + + yylval.lineno = file->lineno; + if (c == '#') + while ((c = lgetc(0)) != '\n' && c != EOF) + ; /* nothing */ + if (c == '$' && parsebuf == NULL) { + while (1) { + if ((c = lgetc(0)) == EOF) + return (0); + + if (p + 1 >= buf + sizeof(buf) - 1) { + yyerror("string too long"); + return (findeol()); + } + if (isalnum(c) || c == '_') { + *p++ = c; + continue; + } + *p = '\0'; + lungetc(c); + break; + } + if ((val = symget(buf)) == NULL) { + yyerror("macro '%s' not defined", buf); + return (findeol()); + } + parsebuf = val; + parseindex = 0; + goto top; + } + + switch (c) { + case '\'': + case '"': + quotec = c; + while (1) { + if ((c = lgetc(quotec)) == EOF) + return (0); + if (c == '\n') { + file->lineno++; + continue; + } else if (c == '\\') { + if ((next = lgetc(quotec)) == EOF) + return (0); + if (next == quotec || c == ' ' || c == '\t') + c = next; + else if (next == '\n') { + file->lineno++; + continue; + } else + lungetc(next); + } else if (c == quotec) { + *p = '\0'; + break; + } else if (c == '\0') { + yyerror("syntax error"); + return (findeol()); + } + if (p + 1 >= buf + sizeof(buf) - 1) { + yyerror("string too long"); + return (findeol()); + } + *p++ = c; + } + if ((yylval.v.string = strdup(buf)) == NULL) + err(1, "yylex: strdup"); + return (STRING); + } + +#define allowed_to_end_number(x) \ + (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=') + + if (c == '-' || isdigit(c)) { + do { + *p++ = c; + if ((unsigned)(p-buf) >= sizeof(buf)) { + yyerror("string too long"); + return (findeol()); + } + } while ((c = lgetc(0)) != EOF && isdigit(c)); + lungetc(c); + if (p == buf + 1 && buf[0] == '-') + goto nodigits; + if (c == EOF || allowed_to_end_number(c)) { + const char *errstr = NULL; + + *p = '\0'; + yylval.v.number = strtonum(buf, LLONG_MIN, + LLONG_MAX, &errstr); + if (errstr) { + yyerror("\"%s\" invalid number: %s", + buf, errstr); + return (findeol()); + } + return (NUMBER); + } else { +nodigits: + while (p > buf + 1) + lungetc(*--p); + c = *--p; + if (c == '-') + return (c); + } + } + +#define allowed_in_string(x) \ + (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \ + x != '{' && x != '}' && \ + x != '!' && x != '=' && x != '#' && \ + x != ',')) + + if (isalnum(c) || c == ':' || c == '_' || c == '*') { + do { + *p++ = c; + if ((unsigned)(p-buf) >= sizeof(buf)) { + yyerror("string too long"); + return (findeol()); + } + } while ((c = lgetc(0)) != EOF && (allowed_in_string(c))); + lungetc(c); + *p = '\0'; + if ((token = lookup(buf)) == STRING) { + yylval.v.string = strdup(buf); + if (yylval.v.string == NULL) + err(1, "yylex: strdup"); + } + return (token); + } + if (c == '\n') { + yylval.lineno = file->lineno; + file->lineno++; + } + if (c == EOF) + return (0); + return (c); +} + +int +check_file_secrecy(int fd, const char *fname) +{ + struct stat st; + + if (fstat(fd, &st)) { + log_warn("cannot stat %s", fname); + return (-1); + } + if (st.st_uid != 0 && st.st_uid != getuid()) { + log_warnx("%s: owner not root or current user", fname); + return (-1); + } + if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) { + log_warnx("%s: group writable or world read/writable", fname); + return (-1); + } + return (0); +} + +struct file * +pushfile(const char *name, int secret) +{ + struct file *nfile; + + if ((nfile = calloc(1, sizeof(struct file))) == NULL) { + log_warn("calloc"); + return (NULL); + } + if ((nfile->name = strdup(name)) == NULL) { + log_warn("strdup"); + free(nfile); + return (NULL); + } + if ((nfile->stream = fopen(nfile->name, "r")) == NULL) { + log_warn("%s", nfile->name); + free(nfile->name); + free(nfile); + return (NULL); + } else if (secret && + check_file_secrecy(fileno(nfile->stream), nfile->name)) { + fclose(nfile->stream); + free(nfile->name); + free(nfile); + return (NULL); + } + nfile->lineno = 1; + TAILQ_INSERT_TAIL(&files, nfile, entry); + return (nfile); +} + +int +popfile(void) +{ + struct file *prev; + + if ((prev = TAILQ_PREV(file, files, entry)) != NULL) + prev->errors += file->errors; + + TAILQ_REMOVE(&files, file, entry); + fclose(file->stream); + free(file->name); + free(file); + file = prev; + return (file ? 0 : EOF); +} + +/* copy a snowflake string option, consuming the token's string */ +static int +snow_set(char *dst, size_t dstlen, char *val, const char *what) +{ + if (strlen(val) == 0 || strlcpy(dst, val, dstlen) >= dstlen) { + yyerror("invalid snowflake %s: %s", what, val); + free(val); + return (-1); + } + free(val); + return (0); +} + +static void +listen_init(void) +{ + memset(&lconf, 0, sizeof(lconf)); +} + +static void +listen_clear(void) +{ + free(lconf.tls_cert_file); + free(lconf.tls_key_file); + free(lconf.tls_ca_file); + free(lconf.tls_crl_file); + free(lconf.tls_ocsp_staple_file); + memset(&lconf, 0, sizeof(lconf)); +} + +static int +listen_setfile(char **dst, const char *src) +{ + free(*dst); + *dst = NULL; + if (src == NULL) + return (0); + if ((*dst = strdup(src)) == NULL) + return (-1); + return (0); +} + +int +parse_config(const char *filename, struct mqttd *xconf) +{ + struct sym *sym, *next; + + conf = xconf; + config_init(conf); + + conf->conf.prefork = MQTTD_NUMPROC; + conf->conf.max_clients = 0; + conf->conf.max_packet_size = MQTTD_DEFAULT_MAX_PACKET; + conf->conf.max_inflight = MQTTD_DEFAULT_MAX_INFLIGHT; + conf->conf.max_keepalive = 0; + conf->conf.max_session_expiry = 0; + conf->conf.max_queued = MQTTD_DEFAULT_MAX_QUEUED; + conf->conf.topic_alias_max = MQTTD_DEFAULT_TOPIC_ALIAS_MAX; + conf->conf.max_qos = 2; + conf->conf.retain_available = 1; + conf->conf.anonymous = 0; + conf->conf.acl_present = 0; + conf->conf.sys_interval = MQTTD_DEFAULT_SYS_INTERVAL; + conf->conf.persist = 0; + conf->conf.persist_interval = MQTTD_DEFAULT_PERSIST_INTERVAL; + conf->conf.sparkplug = 0; + + if ((file = pushfile(filename, 0)) == NULL) { + log_warn("failed to open %s", filename); + return (-1); + } + topfile = file; + + yyparse(); + errors = file->errors; + popfile(); + + /* Free macros and check which have not been used. */ + TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) { + if ((conf->verbose > 1) && !sym->used) + fprintf(stderr, "warning: macro '%s' not used\n", + sym->nam); + if (!sym->persist) { + free(sym->nam); + free(sym->val); + TAILQ_REMOVE(&symhead, sym, entry); + free(sym); + } + } + + if (errors) + return (-1); + + if (TAILQ_EMPTY(&conf->listeners)) { + log_warnx("no listeners defined"); + return (-1); + } + + conf->conf.acl_present = !TAILQ_EMPTY(&conf->acls); + + return (0); +} + +int +symset(const char *nam, const char *val, int persist) +{ + struct sym *sym; + + TAILQ_FOREACH(sym, &symhead, entry) { + if (strcmp(nam, sym->nam) == 0) + break; + } + + if (sym != NULL) { + if (sym->persist == 1) + return (0); + else { + free(sym->nam); + free(sym->val); + TAILQ_REMOVE(&symhead, sym, entry); + free(sym); + } + } + if ((sym = calloc(1, sizeof(*sym))) == NULL) + return (-1); + if ((sym->nam = strdup(nam)) == NULL) { + free(sym); + return (-1); + } + if ((sym->val = strdup(val)) == NULL) { + free(sym->nam); + free(sym); + return (-1); + } + sym->used = 0; + sym->persist = persist; + TAILQ_INSERT_TAIL(&symhead, sym, entry); + return (0); +} + +int +cmdline_symset(char *s) +{ + char *sym, *val; + int ret; + size_t len; + + if ((val = strrchr(s, '=')) == NULL) + return (-1); + + len = strlen(s) - strlen(val) + 1; + if ((sym = malloc(len)) == NULL) + fatal("%s: malloc", __func__); + + strlcpy(sym, s, len); + + ret = symset(sym, val + 1, 1); + free(sym); + + return (ret); +} + +char * +symget(const char *nam) +{ + struct sym *sym; + + TAILQ_FOREACH(sym, &symhead, entry) { + if (strcmp(nam, sym->nam) == 0) { + sym->used = 1; + return (sym->val); + } + } + return (NULL); +} + +int +getservice(char *n) +{ + struct servent *s; + const char *errstr; + long long llval; + + llval = strtonum(n, 0, UINT16_MAX, &errstr); + if (errstr) { + s = getservbyname(n, "tcp"); + if (s == NULL) + return (-1); + return (s->s_port); + } + + return (htons((unsigned short)llval)); +} + +struct address * +host_v4(const char *s) +{ + struct in_addr ina; + struct sockaddr_in *sain; + struct address *h; + + memset(&ina, 0, sizeof(ina)); + if (inet_pton(AF_INET, s, &ina) != 1) + return (NULL); + + if ((h = calloc(1, sizeof(*h))) == NULL) + fatal(__func__); + sain = (struct sockaddr_in *)&h->ss; + sain->sin_len = sizeof(struct sockaddr_in); + sain->sin_family = AF_INET; + sain->sin_addr.s_addr = ina.s_addr; + if (sain->sin_addr.s_addr == INADDR_ANY) + h->prefixlen = 0; /* 0.0.0.0 address */ + else + h->prefixlen = -1; /* host address */ + return (h); +} + +struct address * +host_v6(const char *s) +{ + struct addrinfo hints, *res; + struct sockaddr_in6 *sa_in6; + struct address *h = NULL; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_DGRAM; /* dummy */ + hints.ai_flags = AI_NUMERICHOST; + if (getaddrinfo(s, "0", &hints, &res) == 0) { + if ((h = calloc(1, sizeof(*h))) == NULL) + fatal(__func__); + sa_in6 = (struct sockaddr_in6 *)&h->ss; + sa_in6->sin6_len = sizeof(struct sockaddr_in6); + sa_in6->sin6_family = AF_INET6; + memcpy(&sa_in6->sin6_addr, + &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, + sizeof(sa_in6->sin6_addr)); + sa_in6->sin6_scope_id = + ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; + if (memcmp(&sa_in6->sin6_addr, &in6addr_any, + sizeof(sa_in6->sin6_addr)) == 0) + h->prefixlen = 0; /* any address */ + else + h->prefixlen = -1; /* host address */ + freeaddrinfo(res); + } + + return (h); +} + +int +host_dns(const char *s, struct addresslist *al, int max, + in_port_t port, const char *ifname, int ipproto) +{ + struct addrinfo hints, *res0, *res; + int error, cnt = 0; + struct sockaddr_in *sain; + struct sockaddr_in6 *sin6; + struct address *h; + + if ((cnt = host_if(s, al, max, port, ifname, ipproto)) != 0) + return (cnt); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; /* DUMMY */ + hints.ai_flags = AI_ADDRCONFIG; + error = getaddrinfo(s, NULL, &hints, &res0); + if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME) + return (0); + if (error) { + log_warnx("%s: could not parse \"%s\": %s", __func__, s, + gai_strerror(error)); + return (-1); + } + + for (res = res0; res && cnt < max; res = res->ai_next) { + if (res->ai_family != AF_INET && + res->ai_family != AF_INET6) + continue; + if ((h = calloc(1, sizeof(*h))) == NULL) + fatal(__func__); + + if (port) + h->port = port; + if (ifname != NULL) { + if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >= + sizeof(h->ifname)) { + log_warnx("%s: interface name truncated", + __func__); + freeaddrinfo(res0); + free(h); + return (-1); + } + } + if (ipproto != -1) + h->ipproto = ipproto; + h->ss.ss_family = res->ai_family; + h->prefixlen = -1; /* host address */ + + if (res->ai_family == AF_INET) { + sain = (struct sockaddr_in *)&h->ss; + sain->sin_len = sizeof(struct sockaddr_in); + sain->sin_addr.s_addr = ((struct sockaddr_in *) + res->ai_addr)->sin_addr.s_addr; + } else { + sin6 = (struct sockaddr_in6 *)&h->ss; + sin6->sin6_len = sizeof(struct sockaddr_in6); + memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *) + res->ai_addr)->sin6_addr, sizeof(struct in6_addr)); + } + + TAILQ_INSERT_HEAD(al, h, entry); + cnt++; + } + if (cnt == max && res) { + log_warnx("%s: %s resolves to more than %d hosts", __func__, + s, max); + } + freeaddrinfo(res0); + return (cnt); +} + +int +host_if(const char *s, struct addresslist *al, int max, + in_port_t port, const char *ifname, int ipproto) +{ + struct ifaddrs *ifap, *p; + struct sockaddr_in *sain; + struct sockaddr_in6 *sin6; + struct address *h; + int cnt = 0, af; + + if (getifaddrs(&ifap) == -1) + fatal("getifaddrs"); + + /* First search for IPv4 addresses */ + af = AF_INET; + + nextaf: + for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) { + if (p->ifa_addr == NULL || + p->ifa_addr->sa_family != af || + (strcmp(s, p->ifa_name) != 0 && + !is_if_in_group(p->ifa_name, s))) + continue; + if ((h = calloc(1, sizeof(*h))) == NULL) + fatal("calloc"); + + if (port) + h->port = port; + if (ifname != NULL) { + if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >= + sizeof(h->ifname)) { + log_warnx("%s: interface name truncated", + __func__); + free(h); + freeifaddrs(ifap); + return (-1); + } + } + if (ipproto != -1) + h->ipproto = ipproto; + h->ss.ss_family = af; + h->prefixlen = -1; /* host address */ + + if (af == AF_INET) { + sain = (struct sockaddr_in *)&h->ss; + sain->sin_len = sizeof(struct sockaddr_in); + sain->sin_addr.s_addr = ((struct sockaddr_in *) + p->ifa_addr)->sin_addr.s_addr; + } else { + sin6 = (struct sockaddr_in6 *)&h->ss; + sin6->sin6_len = sizeof(struct sockaddr_in6); + memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *) + p->ifa_addr)->sin6_addr, sizeof(struct in6_addr)); + sin6->sin6_scope_id = ((struct sockaddr_in6 *) + p->ifa_addr)->sin6_scope_id; + } + + TAILQ_INSERT_HEAD(al, h, entry); + cnt++; + } + if (af == AF_INET) { + /* Next search for IPv6 addresses */ + af = AF_INET6; + goto nextaf; + } + + if (cnt > max) { + log_warnx("%s: %s resolves to more than %d hosts", __func__, + s, max); + } + freeifaddrs(ifap); + return (cnt); +} + +int +host(const char *s, struct addresslist *al, int max, + in_port_t port, const char *ifname, int ipproto) +{ + struct address *h; + + h = host_v4(s); + + /* IPv6 address? */ + if (h == NULL) + h = host_v6(s); + + if (h != NULL) { + if (port) + h->port = port; + if (ifname != NULL) { + if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >= + sizeof(h->ifname)) { + log_warnx("%s: interface name truncated", + __func__); + free(h); + return (-1); + } + } + if (ipproto != -1) + h->ipproto = ipproto; + + TAILQ_INSERT_HEAD(al, h, entry); + return (1); + } + + return (host_dns(s, al, max, port, ifname, ipproto)); +} + +int +is_if_in_group(const char *ifname, const char *groupname) +{ + unsigned int len; + struct ifgroupreq ifgr; + struct ifg_req *ifg; + int s; + int ret = 0; + + if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) + err(1, "socket"); + + memset(&ifgr, 0, sizeof(ifgr)); + if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ) + err(1, "IFNAMSIZ"); + if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) { + if (errno == EINVAL || errno == ENOTTY) + goto end; + err(1, "SIOCGIFGROUP"); + } + + len = ifgr.ifgr_len; + ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req), + sizeof(struct ifg_req)); + if (ifgr.ifgr_groups == NULL) + err(1, "getifgroups"); + if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) + err(1, "SIOCGIFGROUP"); + + ifg = ifgr.ifgr_groups; + for (; ifg && len >= sizeof(struct ifg_req); ifg++) { + len -= sizeof(struct ifg_req); + if (strcmp(ifg->ifgrq_group, groupname) == 0) { + ret = 1; + break; + } + } + free(ifgr.ifgr_groups); + +end: + close(s); + return (ret); +} blob - /dev/null blob + 5c4c72221089cc4d54e428c9cd24ae301b90169f39263afa0dad89dab3133d37 (mode 644) --- /dev/null +++ mqttd/session.c @@ -0,0 +1,694 @@ +/* + * Copyright (c) 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 + * 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 "mqttd.h" +#include "mqtt.h" +#include "broker.h" + +RB_GENERATE(sesstree, session, entry, session_cmp); +RB_GENERATE(dlvtree, delivery, tree, delivery_cmp); +RB_GENERATE(inctree, incoming, tree, incoming_cmp); +RB_GENERATE(usercounttree, usercount, entry, usercount_cmp); + +static uint32_t session_window(struct session *); +static uint16_t session_next_pid(struct session *); +static void session_inflight_insert(struct session *, struct delivery *); +static void session_publish_will(struct session *); +static void session_expire_cb(int, short, void *); +static void session_will_cb(int, short, void *); +static void session_free_deliveries(struct session *); + +int +session_cmp(struct session *a, struct session *b) +{ + return (strcmp(a->client_id, b->client_id)); +} + +int +delivery_cmp(struct delivery *a, struct delivery *b) +{ + return ((int)a->pid - (int)b->pid); +} + +int +incoming_cmp(struct incoming *a, struct incoming *b) +{ + return ((int)a->pid - (int)b->pid); +} + +int +usercount_cmp(struct usercount *a, struct usercount *b) +{ + return (strcmp(a->name, b->name)); +} + +uint32_t +usercount_get(const char *name) +{ + struct usercount key, *uc; + + if (name == NULL || + strlcpy(key.name, name, sizeof(key.name)) >= sizeof(key.name)) + return (0); + uc = RB_FIND(usercounttree, &broker_env->usercounts, &key); + return (uc != NULL ? uc->count : 0); +} + +void +usercount_adjust(const char *name, int delta) +{ + struct usercount key, *uc; + + if (name == NULL || + strlcpy(key.name, name, sizeof(key.name)) >= sizeof(key.name)) + return; + uc = RB_FIND(usercounttree, &broker_env->usercounts, &key); + if (uc == NULL) { + if (delta <= 0) + return; + if ((uc = calloc(1, sizeof(*uc))) == NULL) + return; + strlcpy(uc->name, name, sizeof(uc->name)); + RB_INSERT(usercounttree, &broker_env->usercounts, uc); + } + if (delta < 0 && uc->count < (uint32_t)-delta) + uc->count = 0; + else + uc->count += delta; + if (uc->count == 0) { + RB_REMOVE(usercounttree, &broker_env->usercounts, uc); + free(uc); + } +} + +/* + * Token bucket: rate publishes per second with a burst of one second. + * Returns 0 when the client is over its limit. + */ +int +session_rate_ok(struct session *s) +{ + struct timeval now, delta; + double elapsed; + + if (s->rate == 0) + return (1); + + gettimeofday(&now, NULL); + timersub(&now, &s->rate_last, &delta); + elapsed = delta.tv_sec + delta.tv_usec / 1000000.0; + if (elapsed > 0) { + s->rate_tokens += elapsed * s->rate; + if (s->rate_tokens > s->rate) + s->rate_tokens = s->rate; + s->rate_last = now; + } + if (s->rate_tokens < 1.0) + return (0); + s->rate_tokens -= 1.0; + return (1); +} + +struct message * +message_new(const uint8_t *topic, uint16_t topic_len, const uint8_t *payload, + uint32_t payload_len, uint8_t qos, uint8_t retain) +{ + struct message *m; + + if ((m = calloc(1, sizeof(*m))) == NULL) + return (NULL); + if ((m->topic = malloc((size_t)topic_len + 1)) == NULL) { + free(m); + return (NULL); + } + memcpy(m->topic, topic, topic_len); + m->topic[topic_len] = '\0'; + m->topic_len = topic_len; + + if (payload_len > 0) { + if ((m->payload = malloc(payload_len)) == NULL) { + free(m->topic); + free(m); + return (NULL); + } + memcpy(m->payload, payload, payload_len); + } + m->payload_len = payload_len; + m->qos = qos; + m->retain = retain; + m->refcnt = 1; + m->received = time(NULL); + return (m); +} + +/* + * Keep the properties a server forwards unchanged. Message expiry is + * recomputed at send time and subscription identifiers belong to the + * receiving subscription, so neither is stored here. + */ +int +message_set_props(struct message *m, const struct mqtt_props *p) +{ + struct mqtt_buf b; + int i; + + if (p == NULL || p->present == 0) + return (0); + + mqtt_buf_init(&b); + + if (MQTT_PROP_ISSET(p, MQTT_PROP_PAYLOAD_FORMAT)) { + m->has_payload_format = 1; + m->payload_format = p->payload_format; + if (mqtt_prop_u8(&b, MQTT_PROP_PAYLOAD_FORMAT, + p->payload_format) == -1) + goto fail; + } + if (MQTT_PROP_ISSET(p, MQTT_PROP_MESSAGE_EXPIRY)) { + m->expiry_interval = p->message_expiry; + m->expires = m->received + (time_t)p->message_expiry; + } + if (MQTT_PROP_ISSET(p, MQTT_PROP_CONTENT_TYPE) && + mqtt_prop_str(&b, MQTT_PROP_CONTENT_TYPE, p->content_type.data, + p->content_type.len) == -1) + goto fail; + if (MQTT_PROP_ISSET(p, MQTT_PROP_RESPONSE_TOPIC) && + mqtt_prop_str(&b, MQTT_PROP_RESPONSE_TOPIC, + p->response_topic.data, p->response_topic.len) == -1) + goto fail; + if (MQTT_PROP_ISSET(p, MQTT_PROP_CORRELATION_DATA) && + mqtt_prop_str(&b, MQTT_PROP_CORRELATION_DATA, + p->correlation_data.data, p->correlation_data.len) == -1) + goto fail; + for (i = 0; i < p->nuser_props; i++) { + if (mqtt_prop_pair(&b, p->user_props[i][0].data, + p->user_props[i][0].len, p->user_props[i][1].data, + p->user_props[i][1].len) == -1) + goto fail; + } + + m->props = b.data; + m->props_len = b.len; + return (0); +fail: + mqtt_buf_free(&b); + return (-1); +} + +void +message_ref(struct message *m) +{ + m->refcnt++; +} + +void +message_unref(struct message *m) +{ + if (--m->refcnt > 0) + return; + free(m->topic); + free(m->payload); + free(m->props); + free(m); +} + +int +message_expired(const struct message *m, time_t now) +{ + return (m->expires != 0 && now >= m->expires); +} + +struct delivery * +delivery_new(struct message *m, uint8_t qos, uint8_t retain) +{ + struct delivery *d; + + if ((d = calloc(1, sizeof(*d))) == NULL) + return (NULL); + message_ref(m); + d->msg = m; + d->qos = qos; + d->retain = retain; + d->state = DLV_QUEUED; + return (d); +} + +void +delivery_add_subid(struct delivery *d, uint32_t id) +{ + int i; + + if (id == 0) + return; + for (i = 0; i < d->nsubids; i++) + if (d->subids[i] == id) + return; + if (d->nsubids < MQTTD_MAX_SUBIDS) + d->subids[d->nsubids++] = id; +} + +void +delivery_free(struct delivery *d) +{ + if (d->msg != NULL) + message_unref(d->msg); + free(d); +} + +struct session * +session_find(const char *client_id) +{ + struct session key; + + key.client_id = (char *)client_id; + return (RB_FIND(sesstree, &broker_env->sessions, &key)); +} + +struct session * +session_new(const char *client_id) +{ + struct session *s; + + if ((s = calloc(1, sizeof(*s))) == NULL) + return (NULL); + if ((s->client_id = strdup(client_id)) == NULL) { + free(s); + return (NULL); + } + s->serial = ++broker_env->next_serial; + s->next_pid = 1; + s->created = time(NULL); + TAILQ_INIT(&s->subs); + TAILQ_INIT(&s->queue); + RB_INIT(&s->inflight); + RB_INIT(&s->incoming); + evtimer_set(&s->expiry_ev, session_expire_cb, s); + evtimer_set(&s->will_ev, session_will_cb, s); + RB_INSERT(sesstree, &broker_env->sessions, s); + broker_env->nsessions++; + return (s); +} + +void +session_unsubscribe_all(struct session *s) +{ + struct subscription *sub; + + while ((sub = TAILQ_FIRST(&s->subs)) != NULL) { + TAILQ_REMOVE(&s->subs, sub, sess_entry); + topic_unsubscribe(&broker_env->topics, sub); + free(sub->share); + free(sub); + } + s->nsubs = 0; +} + +void +session_clear_aliases(struct session *s) +{ + uint16_t i; + + if (s->aliases == NULL) + return; + for (i = 0; i < s->naliases; i++) + free(s->aliases[i]); + free(s->aliases); + s->aliases = NULL; + s->naliases = 0; +} + +static void +session_free_deliveries(struct session *s) +{ + struct delivery *d, *tmp; + struct incoming *in, *itmp; + + RB_FOREACH_SAFE(d, dlvtree, &s->inflight, tmp) { + RB_REMOVE(dlvtree, &s->inflight, d); + delivery_free(d); + } + s->ninflight = 0; + + while ((d = TAILQ_FIRST(&s->queue)) != NULL) { + TAILQ_REMOVE(&s->queue, d, entry); + delivery_free(d); + } + s->nqueued = 0; + + RB_FOREACH_SAFE(in, inctree, &s->incoming, itmp) { + RB_REMOVE(inctree, &s->incoming, in); + free(in); + } + s->nincoming = 0; +} + +/* clean start: forget everything the session accumulated */ +void +session_reset(struct session *s) +{ + evtimer_del(&s->expiry_ev); + evtimer_del(&s->will_ev); + session_unsubscribe_all(s); + session_free_deliveries(s); + session_clear_aliases(s); + if (s->will != NULL) { + message_unref(s->will); + s->will = NULL; + } + s->will_delay = 0; + s->next_pid = 1; + s->serial = ++broker_env->next_serial; +} + +void +session_destroy(struct session *s) +{ + if (s->conn != NULL) + fatalx("%s: session %s still connected", __func__, + s->client_id); + session_reset(s); + RB_REMOVE(sesstree, &broker_env->sessions, s); + broker_env->nsessions--; + free(s->client_id); + free(s->username); + free(s); +} + +void +session_attach(struct session *s, struct bconn *bc) +{ + evtimer_del(&s->expiry_ev); + evtimer_del(&s->will_ev); + s->conn = bc; + bc->session = s; + s->connected_at = time(NULL); + broker_env->nconnected++; + usercount_adjust(s->username, 1); + s->rate_tokens = s->rate; + gettimeofday(&s->rate_last, NULL); + + if (env->conf.topic_alias_max > 0) { + s->aliases = calloc(env->conf.topic_alias_max, + sizeof(*s->aliases)); + if (s->aliases != NULL) + s->naliases = env->conf.topic_alias_max; + } +} + +/* + * The connection is gone. Returns 1 when the session was destroyed and + * must not be touched again. + */ +int +session_detach(struct session *s, enum detach_how how) +{ + struct timeval tv; + uint32_t delay; + + if (s->conn != NULL) { + s->conn->session = NULL; + s->conn = NULL; + broker_env->nconnected--; + usercount_adjust(s->username, -1); + } + s->disconnected_at = time(NULL); + session_clear_aliases(s); + + if (how == DETACH_NORMAL && s->will != NULL) { + message_unref(s->will); + s->will = NULL; + } + + if (s->will != NULL) { + if (how == DETACH_TAKEOVER && s->will_delay > 0) { + /* the new connection to this session cancels it */ + } else if (s->will_delay == 0 || s->expiry_interval == 0) { + session_publish_will(s); + } else { + delay = s->will_delay < s->expiry_interval ? + s->will_delay : s->expiry_interval; + tv.tv_sec = delay; + tv.tv_usec = 0; + evtimer_add(&s->will_ev, &tv); + } + } + + if (how == DETACH_TAKEOVER) + return (0); + + if (s->expiry_interval == 0) { + log_debug("%s: session %s ended", __func__, s->client_id); + session_destroy(s); + return (1); + } + if (s->expiry_interval != UINT32_MAX) { + tv.tv_sec = s->expiry_interval; + tv.tv_usec = 0; + evtimer_add(&s->expiry_ev, &tv); + } + return (0); +} + +static void +session_publish_will(struct session *s) +{ + struct message *m = s->will; + + if (m == NULL) + return; + s->will = NULL; + + /* the will's message expiry counts from publication, not CONNECT */ + m->received = time(NULL); + if (m->expiry_interval != 0) + m->expires = m->received + (time_t)m->expiry_interval; + if (env->conf.sparkplug) + sparkplug_will_hook(m); + + log_debug("%s: publishing will for %s to %s", __func__, s->client_id, + m->topic); + broker_route(m); + message_unref(m); +} + +static void +session_expire_cb(int fd, short event, void *arg) +{ + struct session *s = arg; + + log_debug("%s: session %s expired", __func__, s->client_id); + session_publish_will(s); + session_destroy(s); +} + +static void +session_will_cb(int fd, short event, void *arg) +{ + struct session *s = arg; + + session_publish_will(s); +} + +/* + * Clients get the smaller of their receive maximum and our limit. A + * bridge or sink negotiated its window with the remote and may hold far + * more, which is how a sink accumulates hours of rows before an insert. + */ +static uint32_t +session_window(struct session *s) +{ + uint32_t win = env->conf.max_inflight; + + if (s->bridge != NULL && s->recv_max != 0) + win = s->recv_max; + else if (s->recv_max != 0 && s->recv_max < win) + win = s->recv_max; + if (win == 0) + win = 1; + return (win); +} + +uint32_t +session_max_queued(struct session *s) +{ + return (s->max_queued != 0 ? s->max_queued : env->conf.max_queued); +} + +static uint16_t +session_next_pid(struct session *s) +{ + uint16_t pid; + int i; + + for (i = 0; i < 65535; i++) { + pid = s->next_pid++; + if (s->next_pid == 0) + s->next_pid = 1; + if (pid != 0 && session_inflight_find(s, pid) == NULL) + return (pid); + } + return (0); +} + +struct delivery * +session_inflight_find(struct session *s, uint16_t pid) +{ + struct delivery key; + + key.pid = pid; + return (RB_FIND(dlvtree, &s->inflight, &key)); +} + +static void +session_inflight_insert(struct session *s, struct delivery *d) +{ + d->pid = session_next_pid(s); + d->state = DLV_PUBLISH; + RB_INSERT(dlvtree, &s->inflight, d); + s->ninflight++; +} + +void +session_inflight_remove(struct session *s, struct delivery *d) +{ + RB_REMOVE(dlvtree, &s->inflight, d); + s->ninflight--; +} + +int +session_incoming_add(struct session *s, uint16_t pid) +{ + struct incoming key, *in; + + key.pid = pid; + if (RB_FIND(inctree, &s->incoming, &key) != NULL) + return (1); + if ((in = calloc(1, sizeof(*in))) == NULL) + return (-1); + in->pid = pid; + RB_INSERT(inctree, &s->incoming, in); + s->nincoming++; + return (0); +} + +int +session_incoming_find(struct session *s, uint16_t pid) +{ + struct incoming key; + + key.pid = pid; + return (RB_FIND(inctree, &s->incoming, &key) != NULL); +} + +int +session_incoming_remove(struct session *s, uint16_t pid) +{ + struct incoming key, *in; + + key.pid = pid; + if ((in = RB_FIND(inctree, &s->incoming, &key)) == NULL) + return (-1); + RB_REMOVE(inctree, &s->incoming, in); + s->nincoming--; + free(in); + return (0); +} + +void +session_deliver(struct session *s, struct delivery *d) +{ + d->session = s; + + if (d->qos == 0) { + if (s->conn != NULL) + broker_send_publish(s, d, 0); + delivery_free(d); + return; + } + + if (s->conn != NULL && s->ninflight < session_window(s)) { + session_inflight_insert(s, d); + if (d->pid == 0 || broker_send_publish(s, d, 0) == -1) { + session_inflight_remove(s, d); + delivery_free(d); + s->dropped++; + broker_env->stats.pub_dropped++; + } + return; + } + + if (s->nqueued >= session_max_queued(s)) { + s->dropped++; + broker_env->stats.pub_dropped++; + delivery_free(d); + return; + } + d->state = DLV_QUEUED; + TAILQ_INSERT_TAIL(&s->queue, d, entry); + s->nqueued++; +} + +void +session_drain(struct session *s) +{ + struct delivery *d; + + while (s->conn != NULL && s->ninflight < session_window(s) && + (d = TAILQ_FIRST(&s->queue)) != NULL) { + TAILQ_REMOVE(&s->queue, d, entry); + s->nqueued--; + if (message_expired(d->msg, time(NULL))) { + delivery_free(d); + continue; + } + session_inflight_insert(s, d); + if (d->pid == 0 || broker_send_publish(s, d, 0) == -1) { + session_inflight_remove(s, d); + delivery_free(d); + s->dropped++; + broker_env->stats.pub_dropped++; + } + } +} + +/* a session resumed: replay unacknowledged QoS 1 and 2 traffic */ +void +session_resume(struct session *s) +{ + struct delivery *d, *tmp; + + RB_FOREACH_SAFE(d, dlvtree, &s->inflight, tmp) { + if (d->state == DLV_PUBLISH) { + if (broker_send_publish(s, d, 1) == -1) { + session_inflight_remove(s, d); + delivery_free(d); + s->dropped++; + } + } else + broker_send_pubrel(s, d->pid, MQTT_RC_SUCCESS); + } + session_drain(s); +} blob - /dev/null blob + c32f39c3180ab89146c41d52a0ceda11ae00b804a172efac57658c912c8a1277 (mode 644) --- /dev/null +++ mqttd/snowflake.c @@ -0,0 +1,1222 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * The snowflake process looks like a sockets process to the broker and + * like a remote broker to a bridge: each sink is a virtual connection + * that accepts CONNECT, answers CONNACK with Receive Maximum set to the + * batch size, collects the PUBLISHes it is sent into rows, inserts them + * through the Snowflake SQL REST API and acknowledges only what the + * warehouse confirmed. Repeated failures close the virtual connection + * so the broker's bridge logic queues, backs off and replays. + * + * HTTPS here is synchronous: the broker never has more than one batch + * outstanding per sink, so a slow warehouse stalls only this process. + */ + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" +#include "mqtt.h" + +struct row { + TAILQ_ENTRY(row) entry; + uint64_t received_ms; + uint16_t pid; + uint8_t qos; + uint8_t retain; + char *topic; + uint8_t *payload; + uint32_t payload_len; +}; +TAILQ_HEAD(rows, row); + +struct sink { + TAILQ_ENTRY(sink) entry; + struct snow_config conf; + struct tls_config *tls_config; + struct sockaddr_storage addrs[MQTTD_SNOW_MAXADDR]; + int naddrs; + int resolving; + char token[MQTTD_JWT_MAX]; + time_t token_expires; + int token_pending; + /* the virtual connection the broker's bridge sees */ + uint32_t conn_id; + int open; + uint8_t version; + uint8_t *inbuf; + uint32_t inlen; + uint32_t intotal; + struct rows rows; + uint32_t nrows; + struct event flush_ev; + int failures; + uint64_t inserted; +}; +TAILQ_HEAD(sinklist, sink); + +static void snow_dispatch_parent(int, short, void *); +static void snow_dispatch_broker(int, short, void *); +static void snow_sighdlr(int, short, void *); +static __dead void + snow_shutdown(void); +static struct sink + *sink_find(uint32_t); +static struct sink + *sink_byconn(uint32_t); +static void snow_config_recv(struct imsg *); +static void snow_ca_recv(struct imsg *); +static void snow_resolved(struct imsg *); +static void snow_jwt_reply(struct imsg *); +static void snow_want_resolve(struct sink *); +static void snow_want_token(struct sink *); +static void snow_bridge_connect(struct imsg *); +static void snow_conn_packet(struct imsg *); +static void snow_conn_close(struct imsg *); +static void snow_packet(struct sink *, const uint8_t *, size_t); +static void snow_send(struct sink *, const struct mqtt_buf *); +static void snow_virtual_close(struct sink *, const char *); +static void snow_flush_cb(int, short, void *); +static void snow_flush(struct sink *); +static void snow_rows_free(struct sink *); +static int snow_insert(struct sink *, uint32_t *, int *); +static int snow_https(struct sink *, const char *, const char *, + const char *, int *, char **, size_t *); +static int tls_io(struct tls *, int, int); +static void sql_quote(struct mqtt_buf *, const char *, size_t); +static void json_quote(struct mqtt_buf *, const char *, size_t); +static int json_string(const char *, const char *, char *, size_t); + +static struct imsgev *snow_iev_parent; +static struct imsgev *snow_iev_broker; +static struct sinklist sinks = TAILQ_HEAD_INITIALIZER(sinks); +static uint32_t snow_next_conn; + +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; + +__dead void +snowflake(int fd_parent, int fd_broker) +{ + log_procinit("snowflake"); + proc_privdrop("snowflake"); + + event_init(); + + signal_set(&ev_sigint, SIGINT, snow_sighdlr, NULL); + signal_set(&ev_sigterm, SIGTERM, snow_sighdlr, NULL); + signal_set(&ev_sighup, SIGHUP, snow_sighdlr, NULL); + signal_set(&ev_sigpipe, SIGPIPE, snow_sighdlr, NULL); + signal_set(&ev_sigusr1, SIGUSR1, snow_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 ((snow_iev_parent = calloc(1, sizeof(*snow_iev_parent))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(snow_iev_parent, fd_parent, snow_dispatch_parent); + + if ((snow_iev_broker = calloc(1, sizeof(*snow_iev_broker))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(snow_iev_broker, fd_broker, snow_dispatch_broker); + + if (pledge("stdio inet", NULL) == -1) + fatal("pledge"); + + event_dispatch(); + + snow_shutdown(); +} + +static void +snow_sighdlr(int sig, short event, void *arg) +{ + switch (sig) { + case SIGINT: + case SIGTERM: + snow_shutdown(); + break; + case SIGHUP: + case SIGPIPE: + case SIGUSR1: + break; + default: + fatalx("%s: unexpected signal", __func__); + } +} + +static __dead void +snow_shutdown(void) +{ + struct sink *s; + + while ((s = TAILQ_FIRST(&sinks)) != NULL) { + TAILQ_REMOVE(&sinks, s, entry); + snow_rows_free(s); + if (s->tls_config != NULL) + tls_config_free(s->tls_config); + free(s->conf.tls_ca); + free(s->inbuf); + explicit_bzero(s, sizeof(*s)); + free(s); + } + + imsg_event_shutdown(snow_iev_parent); + free(snow_iev_parent); + imsg_event_shutdown(snow_iev_broker); + free(snow_iev_broker); + + log_info("snowflake exiting, pid %d", getpid()); + exit(0); +} + +static struct sink * +sink_find(uint32_t id) +{ + struct sink *s; + + TAILQ_FOREACH(s, &sinks, entry) + if (s->conf.id == id) + return (s); + return (NULL); +} + +static struct sink * +sink_byconn(uint32_t conn_id) +{ + struct sink *s; + + TAILQ_FOREACH(s, &sinks, entry) + if (s->open && s->conn_id == conn_id) + return (s); + return (NULL); +} + +static void +snow_dispatch_parent(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n, verbose; + + 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__); + snow_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) + snow_shutdown(); + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_GLOBAL: + config_recv_global(env, &imsg); + break; + case IMSG_CFG_SNOW: + snow_config_recv(&imsg); + break; + case IMSG_CFG_TLS: + snow_ca_recv(&imsg); + break; + case IMSG_CFG_DONE: + imsg_compose_event(iev, IMSG_CFG_DONE, 0, 0, -1, NULL, + 0); + break; + case IMSG_CTL_START: + break; + case IMSG_CTL_VERBOSE: + IMSG_SIZE_CHECK(&imsg, &verbose); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + break; + case IMSG_SNOW_RESOLVED: + snow_resolved(&imsg); + break; + case IMSG_SNOW_JWT_REPLY: + snow_jwt_reply(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from parent", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +snow_dispatch_broker(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost broker process", __func__); + snow_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) + snow_shutdown(); + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_BRIDGE_CONNECT: + snow_bridge_connect(&imsg); + break; + case IMSG_CONN_PACKET: + snow_conn_packet(&imsg); + break; + case IMSG_CONN_CLOSE: + snow_conn_close(&imsg); + break; + case IMSG_CONN_KEEPALIVE: + break; + default: + log_warnx("%s: unexpected imsg %d from broker", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +snow_config_recv(struct imsg *imsg) +{ + struct sink *s; + + if ((s = calloc(1, sizeof(*s))) == NULL) + fatal("%s: calloc", __func__); + IMSG_SIZE_CHECK(imsg, &s->conf); + memcpy(&s->conf, imsg->data, sizeof(s->conf)); + s->conf.name[sizeof(s->conf.name) - 1] = '\0'; + s->conf.host[sizeof(s->conf.host) - 1] = '\0'; + s->conf.servername[sizeof(s->conf.servername) - 1] = '\0'; + s->conf.key = NULL; + s->conf.key_len = 0; + s->conf.tls_ca_file = NULL; + s->conf.tls_ca = NULL; + s->conf.tls_ca_len = 0; + TAILQ_INIT(&s->rows); + evtimer_set(&s->flush_ev, snow_flush_cb, s); + TAILQ_INSERT_TAIL(&sinks, s, entry); + log_debug("%s: sink %s to %s configured", __func__, s->conf.name, + s->conf.host); +} + +static void +snow_ca_recv(struct imsg *imsg) +{ + struct tls_config_msg tls; + struct sink *s; + struct bridge_config shim; + + IMSG_SIZE_CHECK(imsg, &tls); + memcpy(&tls, imsg->data, sizeof(tls)); + if ((s = sink_find(tls.id)) == NULL) + return; + + /* reuse the bridge CA receiver by lending it our fields */ + memset(&shim, 0, sizeof(shim)); + shim.id = s->conf.id; + strlcpy(shim.name, s->conf.name, sizeof(shim.name)); + shim.tls_ca = s->conf.tls_ca; + shim.tls_ca_len = s->conf.tls_ca_len; + if (config_recv_bridge_ca(&shim, imsg) == -1) + log_warnx("snowflake %s: bad ca material", s->conf.name); + s->conf.tls_ca = shim.tls_ca; + s->conf.tls_ca_len = shim.tls_ca_len; +} + +static void +snow_want_resolve(struct sink *s) +{ + struct snow_resolve r; + + if (s->resolving) + return; + s->resolving = 1; + r.id = s->conf.id; + imsg_compose_event(snow_iev_parent, IMSG_SNOW_RESOLVE, 0, 0, -1, &r, + sizeof(r)); +} + +static void +snow_resolved(struct imsg *imsg) +{ + struct snow_resolved r; + struct sink *s; + + IMSG_SIZE_CHECK(imsg, &r); + memcpy(&r, imsg->data, sizeof(r)); + if ((s = sink_find(r.id)) == NULL) + return; + s->resolving = 0; + if (r.n <= 0 || r.n > MQTTD_SNOW_MAXADDR) { + log_warnx("snowflake %s: %s does not resolve", s->conf.name, + s->conf.host); + s->naddrs = 0; + return; + } + memcpy(s->addrs, r.addrs, sizeof(s->addrs)); + s->naddrs = r.n; +} + +static void +snow_want_token(struct sink *s) +{ + uint32_t id = s->conf.id; + + if (s->token_pending) + return; + s->token_pending = 1; + imsg_compose_event(snow_iev_parent, IMSG_SNOW_JWT_REQUEST, 0, 0, -1, + &id, sizeof(id)); +} + +static void +snow_jwt_reply(struct imsg *imsg) +{ + struct snow_jwt_reply r; + struct sink *s; + + IMSG_SIZE_CHECK(imsg, &r); + memcpy(&r, imsg->data, sizeof(r)); + r.token[sizeof(r.token) - 1] = '\0'; + if ((s = sink_find(r.id)) == NULL) + goto done; + s->token_pending = 0; + if (!r.ok) { + log_warnx("snowflake %s: parent could not sign a token", + s->conf.name); + s->token[0] = '\0'; + s->token_expires = 0; + goto done; + } + strlcpy(s->token, r.token, sizeof(s->token)); + s->token_expires = r.expires; +done: + explicit_bzero(&r, sizeof(r)); +} + +/* the broker's bridge wants its remote; we are it */ +static void +snow_bridge_connect(struct imsg *imsg) +{ + struct conn_open co; + struct sink *s; + uint32_t id; + + IMSG_SIZE_CHECK(imsg, &id); + memcpy(&id, imsg->data, sizeof(id)); + if ((s = sink_find(id)) == NULL) { + log_warnx("%s: unknown sink %u", __func__, id); + imsg_compose_event(snow_iev_broker, IMSG_BRIDGE_FAILED, 0, 0, + -1, &id, sizeof(id)); + return; + } + + do { + s->conn_id = ++snow_next_conn; + } while (s->conn_id == 0 || sink_byconn(s->conn_id) != NULL); + s->open = 1; + s->failures = 0; + free(s->inbuf); + s->inbuf = NULL; + + snow_want_resolve(s); + snow_want_token(s); + + memset(&co, 0, sizeof(co)); + co.id = s->conn_id; + co.bridge_id = s->conf.id; + co.tls = 1; + snprintf(co.peer, sizeof(co.peer), "snowflake:%s", s->conf.host); + imsg_compose_event(snow_iev_broker, IMSG_CONN_OPEN, 0, 0, -1, &co, + sizeof(co)); +} + +static void +snow_conn_close(struct imsg *imsg) +{ + struct conn_close cc; + struct sink *s; + + IMSG_SIZE_CHECK(imsg, &cc); + memcpy(&cc, imsg->data, sizeof(cc)); + if ((s = sink_byconn(cc.id)) == NULL) + return; + s->open = 0; + evtimer_del(&s->flush_ev); + snow_rows_free(s); +} + +static void +snow_conn_packet(struct imsg *imsg) +{ + struct conn_pkt hdr; + struct sink *s; + uint8_t *p = imsg->data, *buf; + size_t len = IMSG_DATA_SIZE(imsg), total; + + if (len < sizeof(hdr)) + return; + memcpy(&hdr, p, sizeof(hdr)); + p += sizeof(hdr); + len -= sizeof(hdr); + if (hdr.len != len || (s = sink_byconn(hdr.id)) == NULL) + return; + if (hdr.total > env->conf.max_packet_size || hdr.offset > hdr.total || + hdr.len > hdr.total - hdr.offset) + return; + + if (hdr.offset == 0) { + free(s->inbuf); + s->inbuf = NULL; + if (hdr.total == hdr.len) { + snow_packet(s, p, len); + return; + } + if ((s->inbuf = malloc(hdr.total)) == NULL) + return; + s->intotal = hdr.total; + s->inlen = 0; + } + if (s->inbuf == NULL || hdr.total != s->intotal || + hdr.offset != s->inlen) + return; + memcpy(s->inbuf + hdr.offset, p, len); + s->inlen += len; + if (s->inlen == s->intotal) { + buf = s->inbuf; + total = s->intotal; + s->inbuf = NULL; + snow_packet(s, buf, total); + free(buf); + } +} + +static void +snow_send(struct sink *s, const struct mqtt_buf *b) +{ + struct conn_pkt hdr; + struct iovec iov[2]; + size_t off = 0, n; + + hdr.id = s->conn_id; + hdr.total = b->len; + while (off < b->len) { + n = b->len - off; + if (n > CONN_PKT_CHUNK) + n = CONN_PKT_CHUNK; + hdr.offset = off; + hdr.len = n; + iov[0].iov_base = &hdr; + iov[0].iov_len = sizeof(hdr); + iov[1].iov_base = b->data + off; + iov[1].iov_len = n; + if (imsg_composev_event(snow_iev_broker, IMSG_CONN_PACKET, 0, + 0, -1, iov, 2) == -1) + return; + off += n; + } +} + +/* drop the virtual connection; the broker queues and comes back later */ +static void +snow_virtual_close(struct sink *s, const char *why) +{ + struct conn_close cc; + + log_info("event=snowflake-down sink=\"%s\" reason=\"%s\"", + s->conf.name, why); + cc.id = s->conn_id; + cc.reason = CONN_CLOSE_ERROR; + imsg_compose_event(snow_iev_broker, IMSG_CONN_CLOSE, 0, 0, -1, &cc, + sizeof(cc)); + s->open = 0; + evtimer_del(&s->flush_ev); + snow_rows_free(s); + s->naddrs = 0; +} + +static void +snow_packet(struct sink *s, const uint8_t *data, size_t len) +{ + struct mqtt_fixed f; + struct mqtt_connect c; + struct mqtt_publish p; + struct mqtt_buf props, out; + struct row *r; + struct timeval tv, now; + const uint8_t *body; + + if (mqtt_parse_fixed(data, len, &f) != 0 || + (size_t)f.hdrlen + f.remaining != len) + return; + body = data + f.hdrlen; + + mqtt_buf_init(&props); + mqtt_buf_init(&out); + + switch (f.type) { + case MQTT_CONNECT: + if (mqtt_parse_connect(body, f.remaining, &c) != 0) { + snow_virtual_close(s, "bad CONNECT"); + break; + } + s->version = c.version; + /* let the broker hand us as much as it will ever hold */ + if (s->version == MQTT_V5) + mqtt_prop_u16(&props, MQTT_PROP_RECEIVE_MAXIMUM, + MQTTD_SNOW_WINDOW); + if (mqtt_encode_connack(&out, s->version, 0, MQTT_RC_SUCCESS, + &props) == 0) + snow_send(s, &out); + log_info("event=snowflake-up sink=\"%s\" host=%s table=%s.%s.%s", + s->conf.name, s->conf.host, s->conf.database, + s->conf.schema, s->conf.table); + break; + case MQTT_PUBLISH: + if (mqtt_parse_publish(body, f.remaining, f.flags, s->version, + &p) != 0) { + snow_virtual_close(s, "bad PUBLISH"); + break; + } + if ((r = calloc(1, sizeof(*r))) == NULL || + (r->topic = strndup((const char *)p.topic.data, + p.topic.len)) == NULL || + (p.payload_len > 0 && + (r->payload = malloc(p.payload_len)) == NULL)) { + if (r != NULL) { + free(r->topic); + free(r); + } + snow_virtual_close(s, "out of memory"); + break; + } + if (p.payload_len > 0) + memcpy(r->payload, p.payload, p.payload_len); + r->payload_len = p.payload_len; + r->pid = p.packet_id; + r->qos = p.qos; + r->retain = p.retain; + gettimeofday(&now, NULL); + r->received_ms = (uint64_t)now.tv_sec * 1000 + + now.tv_usec / 1000; + TAILQ_INSERT_TAIL(&s->rows, r, entry); + s->nrows++; + + /* + * The flush interval is the cost knob: one insert every + * few hours wakes the warehouse far less often than one + * every few seconds. Rows wait here until then. + */ + if (!evtimer_pending(&s->flush_ev, NULL)) { + tv.tv_sec = s->conf.flush; + tv.tv_usec = 0; + evtimer_add(&s->flush_ev, &tv); + } + break; + case MQTT_PINGREQ: + if (mqtt_encode_pingresp(&out) == 0) + snow_send(s, &out); + break; + case MQTT_PUBREL: + /* QoS 2 is refused at configuration time; be tolerant */ + break; + case MQTT_DISCONNECT: + s->open = 0; + evtimer_del(&s->flush_ev); + snow_rows_free(s); + break; + default: + break; + } + + mqtt_buf_free(&props); + mqtt_buf_free(&out); +} + +static void +snow_rows_free(struct sink *s) +{ + struct row *r; + + while ((r = TAILQ_FIRST(&s->rows)) != NULL) { + TAILQ_REMOVE(&s->rows, r, entry); + free(r->topic); + free(r->payload); + free(r); + } + s->nrows = 0; +} + +static void +snow_flush_cb(int fd, short event, void *arg) +{ + snow_flush(arg); +} + +static void +snow_flush(struct sink *s) +{ + struct mqtt_buf out; + struct row *r; + struct timeval tv; + int status = 0; + + if (!s->open || s->nrows == 0) + return; + + /* wait for the parent's answers, but not forever */ + if (s->naddrs == 0 || s->token[0] == '\0' || + s->token_expires - 60 < time(NULL)) { + if (s->naddrs == 0) + snow_want_resolve(s); + if (s->token[0] == '\0' || s->token_expires - 60 < time(NULL)) + snow_want_token(s); + if (++s->failures >= MQTTD_SNOW_MAXFAIL * 10) { + snow_virtual_close(s, "no address or token"); + return; + } + tv.tv_sec = 1; + tv.tv_usec = 0; + evtimer_add(&s->flush_ev, &tv); + return; + } + + /* + * Everything accumulated goes out now, as many statements as the + * row and statement size limits require. Each statement's rows + * are acknowledged as soon as it is committed, so a failure part + * way keeps only what was not yet inserted. + */ + mqtt_buf_init(&out); + while (s->open && s->nrows > 0) { + uint32_t sent = 0, done = 0; + + if (snow_insert(s, &sent, &status) == -1) { + s->failures++; + log_warnx("snowflake %s: insert failed with %u row(s) " + "left (http %d, failure %d)", s->conf.name, + s->nrows, status, s->failures); + if (status == 401 || status == 403) { + s->token[0] = '\0'; + s->token_expires = 0; + } + if (s->failures >= MQTTD_SNOW_MAXFAIL) { + snow_virtual_close(s, + "repeated insert failures"); + break; + } + tv.tv_sec = s->failures * 2; + tv.tv_usec = 0; + evtimer_add(&s->flush_ev, &tv); + break; + } + + while (done < sent && (r = TAILQ_FIRST(&s->rows)) != NULL) { + if (r->qos != 0 && mqtt_encode_ack(&out, MQTT_PUBACK, + s->version, r->pid, MQTT_RC_SUCCESS) == 0) + snow_send(s, &out); + TAILQ_REMOVE(&s->rows, r, entry); + s->nrows--; + free(r->topic); + free(r->payload); + free(r); + done++; + } + s->inserted += sent; + s->failures = 0; + log_debug("event=snowflake-flush sink=\"%s\" rows=%u " + "left=%u total=%llu", s->conf.name, sent, s->nrows, + (unsigned long long)s->inserted); + } + mqtt_buf_free(&out); +} + +static void +sql_quote(struct mqtt_buf *b, const char *s, size_t len) +{ + size_t i; + + mqtt_buf_u8(b, '\''); + for (i = 0; i < len; i++) { + if (s[i] == '\'') + mqtt_buf_u8(b, '\''); + if (s[i] == '\\') + mqtt_buf_u8(b, '\\'); + mqtt_buf_u8(b, s[i]); + } + mqtt_buf_u8(b, '\''); +} + +static void +json_quote(struct mqtt_buf *b, const char *s, size_t len) +{ + static const char hex[] = "0123456789abcdef"; + size_t i; + unsigned char c; + + mqtt_buf_u8(b, '"'); + for (i = 0; i < len; i++) { + c = s[i]; + switch (c) { + case '"': + case '\\': + mqtt_buf_u8(b, '\\'); + mqtt_buf_u8(b, c); + break; + case '\n': + mqtt_buf_bytes(b, "\\n", 2); + break; + case '\r': + mqtt_buf_bytes(b, "\\r", 2); + break; + case '\t': + mqtt_buf_bytes(b, "\\t", 2); + break; + default: + if (c < 0x20) { + mqtt_buf_bytes(b, "\\u00", 4); + mqtt_buf_u8(b, hex[c >> 4]); + mqtt_buf_u8(b, hex[c & 0x0f]); + } else + mqtt_buf_u8(b, c); + break; + } + } + mqtt_buf_u8(b, '"'); +} + +/* pull the string value of key out of a flat JSON object, crudely */ +static int +json_string(const char *json, const char *key, char *out, size_t outlen) +{ + const char *p, *e; + char pat[64]; + size_t n; + + snprintf(pat, sizeof(pat), "\"%s\"", key); + if ((p = strstr(json, pat)) == NULL) + return (-1); + p += strlen(pat); + while (*p == ' ' || *p == ':') + p++; + if (*p != '"') + return (-1); + p++; + if ((e = strchr(p, '"')) == NULL) + return (-1); + n = e - p; + if (n >= outlen) + return (-1); + memcpy(out, p, n); + out[n] = '\0'; + return (0); +} + +/* + * INSERT INTO db.schema.table (received_at, topic, qos, retain, payload) + * SELECT TO_TIMESTAMP_LTZ(column1, 3), column2, column3, column4, + * TO_BINARY(column5, 'HEX') FROM VALUES (...), (...) + * + * Takes rows from the head of the queue up to the batch size or the + * statement size limit and reports how many it covered. + */ +static int +snow_insert(struct sink *s, uint32_t *nsent, int *status) +{ + static const char hex[] = "0123456789abcdef"; + struct mqtt_buf sql, body, path; + struct row *r; + char num[32], handle[128], *resp = NULL; + size_t resplen, i, mark; + int first = 1, ret = -1, polls; + + *nsent = 0; + mqtt_buf_init(&sql); + mqtt_buf_init(&body); + mqtt_buf_init(&path); + + mqtt_buf_bytes(&sql, "INSERT INTO ", 12); + mqtt_buf_bytes(&sql, s->conf.database, strlen(s->conf.database)); + mqtt_buf_u8(&sql, '.'); + mqtt_buf_bytes(&sql, s->conf.schema, strlen(s->conf.schema)); + mqtt_buf_u8(&sql, '.'); + mqtt_buf_bytes(&sql, s->conf.table, strlen(s->conf.table)); + mqtt_buf_bytes(&sql, " (received_at, topic, qos, retain, payload) " + "SELECT TO_TIMESTAMP_LTZ(column1, 3), column2, column3, column4, " + "TO_BINARY(column5, 'HEX') FROM VALUES ", 123); + + TAILQ_FOREACH(r, &s->rows, entry) { + if (*nsent >= s->conf.batch) + break; + mark = sql.len; + if (!first) + mqtt_buf_u8(&sql, ','); + snprintf(num, sizeof(num), "(%llu,", + (unsigned long long)r->received_ms); + mqtt_buf_bytes(&sql, num, strlen(num)); + sql_quote(&sql, r->topic, strlen(r->topic)); + snprintf(num, sizeof(num), ",%u,%s,'", r->qos, + r->retain ? "true" : "false"); + mqtt_buf_bytes(&sql, num, strlen(num)); + for (i = 0; i < r->payload_len; i++) { + mqtt_buf_u8(&sql, hex[r->payload[i] >> 4]); + mqtt_buf_u8(&sql, hex[r->payload[i] & 0x0f]); + } + mqtt_buf_bytes(&sql, "')", 2); + /* Snowflake refuses statements over a megabyte */ + if (sql.len > MQTTD_SNOW_MAXSQL && !first) { + sql.len = mark; + break; + } + first = 0; + (*nsent)++; + } + if (*nsent == 0) + goto done; + + mqtt_buf_bytes(&body, "{\"statement\":", 13); + json_quote(&body, (const char *)sql.data, sql.len); + snprintf(num, sizeof(num), ",\"timeout\":%u", s->conf.timeout); + mqtt_buf_bytes(&body, num, strlen(num)); + if (s->conf.warehouse[0] != '\0') { + mqtt_buf_bytes(&body, ",\"warehouse\":", 13); + json_quote(&body, s->conf.warehouse, strlen(s->conf.warehouse)); + } + if (s->conf.role[0] != '\0') { + mqtt_buf_bytes(&body, ",\"role\":", 8); + json_quote(&body, s->conf.role, strlen(s->conf.role)); + } + mqtt_buf_bytes(&body, ",\"database\":", 12); + json_quote(&body, s->conf.database, strlen(s->conf.database)); + mqtt_buf_bytes(&body, ",\"schema\":", 10); + json_quote(&body, s->conf.schema, strlen(s->conf.schema)); + mqtt_buf_u8(&body, '}'); + mqtt_buf_u8(&body, '\0'); + body.len--; + + if (snow_https(s, "POST", "/api/v2/statements", (const char *)body.data, + status, &resp, &resplen) == -1) + goto done; + + /* 202 means still running: poll the statement handle */ + for (polls = 0; *status == 202 && polls < MQTTD_SNOW_POLLS; polls++) { + if (resp == NULL || + json_string(resp, "statementHandle", handle, + sizeof(handle)) == -1) + break; + free(resp); + resp = NULL; + sleep(1); + mqtt_buf_reset(&path); + mqtt_buf_bytes(&path, "/api/v2/statements/", 19); + mqtt_buf_bytes(&path, handle, strlen(handle)); + mqtt_buf_u8(&path, '\0'); + if (snow_https(s, "GET", (const char *)path.data, NULL, status, + &resp, &resplen) == -1) + goto done; + } + + if (*status == 200) + ret = 0; + else if (resp != NULL) + log_warnx("snowflake %s: %.*s", s->conf.name, + (int)(resplen > 300 ? 300 : resplen), resp); +done: + free(resp); + mqtt_buf_free(&sql); + mqtt_buf_free(&body); + mqtt_buf_free(&path); + return (ret); +} + +/* drive a libtls call to completion on a blocking socket with a timeout */ +static int +tls_io(struct tls *ctx, int fd, int want) +{ + struct pollfd pfd; + + pfd.fd = fd; + pfd.events = want == TLS_WANT_POLLIN ? POLLIN : POLLOUT; + return (poll(&pfd, 1, MQTTD_SNOW_DEFAULT_TIMEOUT * 1000) > 0 ? 0 : -1); +} + +/* + * One HTTPS request per call, Connection: close. Returns -1 on a + * transport failure, 0 with the status code and body otherwise. + */ +static int +snow_https(struct sink *s, const char *method, const char *path, + const char *body, int *status, char **resp, size_t *resplen) +{ + struct sockaddr_storage ss; + struct tls *ctx = NULL; + struct mqtt_buf req, in; + struct timeval tv; + const char *servername; + char *hdr_end, *p, *e; + size_t off, clen = 0, hlen, chunk; + ssize_t n; + int fd = -1, ret = -1, chunked = 0, i; + + *resp = NULL; + *resplen = 0; + *status = 0; + mqtt_buf_init(&req); + mqtt_buf_init(&in); + + if (s->tls_config == NULL) { + if ((s->tls_config = tls_config_new()) == NULL) + goto done; + if (s->conf.noverify) { + tls_config_insecure_noverifycert(s->tls_config); + tls_config_insecure_noverifyname(s->tls_config); + } else if (s->conf.tls_ca != NULL && + tls_config_set_ca_mem(s->tls_config, s->conf.tls_ca, + s->conf.tls_ca_len) == -1) { + log_warnx("snowflake %s: %s", s->conf.name, + tls_config_error(s->tls_config)); + goto done; + } + } + + /* try the resolved addresses in turn */ + for (i = 0; i < s->naddrs && fd == -1; i++) { + memcpy(&ss, &s->addrs[i], sizeof(ss)); + if (ss.ss_family == AF_INET) + ((struct sockaddr_in *)&ss)->sin_port = s->conf.port; + else if (ss.ss_family == AF_INET6) + ((struct sockaddr_in6 *)&ss)->sin6_port = s->conf.port; + if ((fd = socket(ss.ss_family, SOCK_STREAM, 0)) == -1) + continue; + tv.tv_sec = s->conf.timeout; + tv.tv_usec = 0; + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + if (connect(fd, (struct sockaddr *)&ss, ss.ss_len) == -1) { + log_warn("snowflake %s: connect", s->conf.name); + close(fd); + fd = -1; + } + } + if (fd == -1) + goto done; + + servername = s->conf.servername[0] != '\0' ? s->conf.servername : + s->conf.host; + if ((ctx = tls_client()) == NULL || + tls_configure(ctx, s->tls_config) == -1 || + tls_connect_socket(ctx, fd, servername) == -1) { + log_warnx("snowflake %s: tls: %s", s->conf.name, + ctx != NULL ? tls_error(ctx) : "no context"); + goto done; + } + for (;;) { + n = tls_handshake(ctx); + if (n == 0) + break; + if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) { + if (tls_io(ctx, fd, n) == -1) + goto done; + continue; + } + log_warnx("snowflake %s: tls handshake with %s: %s", + s->conf.name, servername, tls_error(ctx)); + goto done; + } + + mqtt_buf_bytes(&req, method, strlen(method)); + mqtt_buf_u8(&req, ' '); + mqtt_buf_bytes(&req, path, strlen(path)); + mqtt_buf_bytes(&req, " HTTP/1.1\r\nHost: ", 17); + mqtt_buf_bytes(&req, s->conf.host, strlen(s->conf.host)); + mqtt_buf_bytes(&req, "\r\nAuthorization: Bearer ", 24); + mqtt_buf_bytes(&req, s->token, strlen(s->token)); + mqtt_buf_bytes(&req, "\r\nX-Snowflake-Authorization-Token-Type: " + "KEYPAIR_JWT\r\nAccept: application/json\r\n" + "User-Agent: mqttd/" MQTTD_VERSION "\r\nConnection: close\r\n", + strlen("\r\nX-Snowflake-Authorization-Token-Type: KEYPAIR_JWT\r\n" + "Accept: application/json\r\nUser-Agent: mqttd/" MQTTD_VERSION + "\r\nConnection: close\r\n")); + if (body != NULL) { + char cl[64]; + + snprintf(cl, sizeof(cl), "Content-Type: application/json\r\n" + "Content-Length: %zu\r\n", strlen(body)); + mqtt_buf_bytes(&req, cl, strlen(cl)); + } + mqtt_buf_bytes(&req, "\r\n", 2); + if (body != NULL) + mqtt_buf_bytes(&req, body, strlen(body)); + + for (off = 0; off < req.len; ) { + n = tls_write(ctx, req.data + off, req.len - off); + if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) { + if (tls_io(ctx, fd, n) == -1) + goto done; + continue; + } + if (n < 0) { + log_warnx("snowflake %s: tls_write: %s", s->conf.name, + tls_error(ctx)); + goto done; + } + off += n; + } + + for (;;) { + if (mqtt_buf_reserve(&in, 16384) == -1) + goto done; + n = tls_read(ctx, in.data + in.len, in.cap - in.len - 1); + if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) { + if (tls_io(ctx, fd, n) == -1) + goto done; + continue; + } + if (n < 0) { + log_warnx("snowflake %s: tls_read: %s", s->conf.name, + tls_error(ctx)); + goto done; + } + if (n == 0) + break; + in.len += n; + if (in.len > MQTTD_SNOW_MAXRESP) + goto done; + } + in.data[in.len] = '\0'; + + if (strncmp((char *)in.data, "HTTP/1.", 7) != 0 || + (hdr_end = strstr((char *)in.data, "\r\n\r\n")) == NULL) + goto done; + *status = atoi((char *)in.data + 9); + hlen = hdr_end + 4 - (char *)in.data; + + for (p = (char *)in.data; p < hdr_end; p = e + 2) { + if ((e = strstr(p, "\r\n")) == NULL) + break; + if (strncasecmp(p, "Transfer-Encoding:", 18) == 0 && + strcasestr(p, "chunked") != NULL && strcasestr(p, + "chunked") < e) + chunked = 1; + else if (strncasecmp(p, "Content-Length:", 15) == 0) + clen = strtoul(p + 15, NULL, 10); + } + + if (chunked) { + /* decode chunks in place into the response buffer */ + if ((*resp = malloc(in.len - hlen + 1)) == NULL) + goto done; + p = (char *)in.data + hlen; + for (;;) { + chunk = strtoul(p, &e, 16); + if (e == p || (e = strstr(e, "\r\n")) == NULL) + break; + e += 2; + if (chunk == 0) + break; + if ((size_t)((char *)in.data + in.len - e) < chunk) + break; + memcpy(*resp + *resplen, e, chunk); + *resplen += chunk; + p = e + chunk + 2; + } + (*resp)[*resplen] = '\0'; + } else { + *resplen = in.len - hlen; + if (clen > 0 && clen < *resplen) + *resplen = clen; + if ((*resp = malloc(*resplen + 1)) == NULL) + goto done; + memcpy(*resp, in.data + hlen, *resplen); + (*resp)[*resplen] = '\0'; + } + ret = 0; +done: + if (ctx != NULL) { + tls_close(ctx); + tls_free(ctx); + } + if (fd != -1) + close(fd); + mqtt_buf_free(&req); + mqtt_buf_free(&in); + if (ret == -1) { + free(*resp); + *resp = NULL; + *resplen = 0; + } + return (ret); +} blob - /dev/null blob + a7c5423a0b6ac849c7d3913446cb437090553318e95caabd5eaabd85e1b3a023 (mode 644) --- /dev/null +++ mqttd/sockets.c @@ -0,0 +1,1845 @@ +/* + * Copyright (c) 2016, 2019-2021, 2026 Tracey Emery + * Copyright (c) 2015 Mike Larkin + * Copyright (c) 2007 - 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. + */ + +/* + * The sockets processes own the network connections. They accept, run + * TLS, cut the byte stream into MQTT control packets and relay those to + * the broker; everything the broker sends back is written out verbatim. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" +#include "mqtt.h" + +#define FD_RESERVE 8 +#define TLS_READ_SIZE 16384 +#define PROXY_V1_MAX 107 +#define PROXY_V2_MIN 16 +#define PROXY_PEEK (PROXY_V2_MIN + 216) + +enum ws_state { + WS_NONE, + WS_HANDSHAKE, + WS_OPEN +}; + +enum ws_opcode { + WS_OP_CONT = 0x0, + WS_OP_TEXT = 0x1, + WS_OP_BINARY = 0x2, + WS_OP_CLOSE = 0x8, + WS_OP_PING = 0x9, + WS_OP_PONG = 0xa +}; + +/* outbound bridge target, configured by the parent */ +struct sbridge { + TAILQ_ENTRY(sbridge) entry; + struct bridge_config conf; + struct tls_config *tls_config; +}; +TAILQ_HEAD(sbridgelist, sbridge); + +struct conn { + RB_ENTRY(conn) entry; + TAILQ_ENTRY(conn) tentry; + uint32_t id; + int fd; + struct listener *l; + struct sbridge *bridge; + struct bufferevent *bev; + struct tls *tls_ctx; + struct event ev; + struct event timer; + uint16_t keepalive; + int opened; + int closing; + int paused; + int throttled; + /* websocket: decoded MQTT bytes in, one frame per packet out */ + uint8_t ws; + struct evbuffer *wsin; + struct mqtt_buf wsout; + char peer[MQTTD_MAXPEER]; +}; +RB_HEAD(conntree, conn); +TAILQ_HEAD(connq, conn); + +static int conn_cmp(struct conn *, struct conn *); +RB_PROTOTYPE_STATIC(conntree, conn, entry, conn_cmp); +RB_GENERATE_STATIC(conntree, conn, entry, conn_cmp); + +/* libevent internal, used to resume reading below the high watermark */ +void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, + void *); + +static void sockets_dispatch_parent(int, short, void *); +static void sockets_dispatch_control(int, short, void *); +static void sockets_dispatch_broker(int, short, void *); +static void sockets_sighdlr(int, short, void *); +static __dead void + sockets_shutdown(void); +static void sockets_launch(void); +static int sockets_listener_launch(struct listener *); +static void sockets_listener_remove(struct listener *); +static void sockets_rlimit(void); +static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *); +static void sockets_accept(int, short, void *); +static void sockets_accept_paused(int, short, void *); +static void sockets_show_listeners(struct imsg *); + +static struct conn + *conn_find(uint32_t); +static void conn_start(struct conn *); +static void conn_free(struct conn *, int); +static void conn_error(struct conn *, enum conn_error_reason); +static void conn_readcb(struct bufferevent *, void *); +static void conn_writecb(struct bufferevent *, void *); +static void conn_errcb(struct bufferevent *, short, void *); +static void conn_timeout(int, short, void *); +static void conn_set_timer(struct conn *, time_t); +static int conn_send_packet(struct conn *, const uint8_t *, uint32_t); +static void conn_write_chunk(struct conn *, const struct conn_pkt *, + const uint8_t *); +static void conn_begin(struct conn *); +static void conn_open_send(struct conn *); +static void conn_proxy_read(int, short, void *); +static int proxy_parse_v1(struct conn *, const uint8_t *, size_t); +static int proxy_parse_v2(struct conn *, const uint8_t *, size_t); +static int ws_handshake(struct conn *); +static int ws_decode(struct conn *); +static void ws_send_frame(struct conn *, uint8_t, const uint8_t *, + size_t); +static int ws_header_find(const char *, const char *, const char **, + size_t *); +static int ws_token_in(const char *, size_t, const char *); +static void conn_tls_handshake(int, short, void *); +static void conn_tls_readcb(int, short, void *); +static void conn_tls_writecb(int, short, void *); +static void conn_cert_user(struct tls *, char *, size_t); +static void conn_throttle(struct conn *); +static void sockets_unthrottle(void); +static struct sbridge + *sbridge_find(uint32_t); +static void sockets_bridge_config(struct imsg *); +static void sockets_bridge_ca(struct imsg *); +static void sockets_bridge_connect(struct imsg *); +static void sockets_bridge_connected(int, short, void *); +static void sockets_bridge_failed(struct conn *); + +static struct imsgev *socks_iev_parent; +static struct imsgev *socks_iev_control; +static struct imsgev *socks_iev_broker; + +static struct conntree conns = RB_INITIALIZER(&conns); +/* connections we stopped reading from while the broker pipe is full */ +static struct connq throttled_conns = TAILQ_HEAD_INITIALIZER(throttled_conns); +static struct sbridgelist sbridges = TAILQ_HEAD_INITIALIZER(sbridges); +static uint32_t conn_next_id; + +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 int +conn_cmp(struct conn *a, struct conn *b) +{ + if (a->id != b->id) + return (a->id < b->id ? -1 : 1); + return (0); +} + +__dead void +sockets(int fd_parent, int fd_control, int fd_broker) +{ + log_procinit("sockets"); + proc_privdrop("sockets"); + + /* start from empty lists, the listeners arrive from the parent */ + config_init(env); + + sockets_rlimit(); + + event_init(); + + 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_parent); + + 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 ((socks_iev_broker = calloc(1, sizeof(*socks_iev_broker))) == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(socks_iev_broker, fd_broker, sockets_dispatch_broker); + + if (pledge("stdio inet recvfd", NULL) == -1) + fatal("pledge"); + + event_dispatch(); + + sockets_shutdown(); +} + +static void +sockets_sighdlr(int sig, short event, void *arg) +{ + switch (sig) { + case SIGINT: + case SIGTERM: + sockets_shutdown(); + break; + case SIGHUP: + case SIGPIPE: + case SIGUSR1: + break; + default: + fatalx("%s: unexpected signal", __func__); + } +} + +static __dead void +sockets_shutdown(void) +{ + struct listener *l, *tl; + struct conn *c, *tc; + struct sbridge *sb; + + RB_FOREACH_SAFE(c, conntree, &conns, tc) + conn_free(c, -1); + + while ((sb = TAILQ_FIRST(&sbridges)) != NULL) { + TAILQ_REMOVE(&sbridges, sb, entry); + if (sb->tls_config != NULL) + tls_config_free(sb->tls_config); + free(sb->conf.tls_ca); + free(sb); + } + + TAILQ_FOREACH_SAFE(l, &env->listeners, entry, tl) { + if (l->conf.tls) { + tls_config_free(l->tls_config); + tls_free(l->tls_ctx); + } + if (event_initialized(&l->ev)) + event_del(&l->ev); + if (evtimer_initialized(&l->pause)) + evtimer_del(&l->pause); + TAILQ_REMOVE(&env->listeners, l, entry); + config_free_listener(l); + } + + imsg_event_shutdown(socks_iev_parent); + free(socks_iev_parent); + imsg_event_shutdown(socks_iev_control); + free(socks_iev_control); + imsg_event_shutdown(socks_iev_broker); + free(socks_iev_broker); + + log_info("sockets exiting, pid %d", getpid()); + exit(0); +} + +static void +sockets_rlimit(void) +{ + struct rlimit rl; + + if (getrlimit(RLIMIT_NOFILE, &rl) == -1) + fatal("%s: getrlimit", __func__); + log_debug("%s: max open files %llu", __func__, + (unsigned long long)rl.rlim_max); + + /* take everything the login class allows, every client costs an fd */ + rl.rlim_cur = rl.rlim_max; + if (setrlimit(RLIMIT_NOFILE, &rl) == -1) + fatal("%s: setrlimit", __func__); +} + +int +sockets_create_listener(struct listener *l) +{ + struct sockaddr_storage *ss = &l->conf.ss; + struct sockaddr_un *sun; + int fd, on = 1; + + if (ss->ss_family == AF_UNIX) { + sun = (struct sockaddr_un *)ss; + if (unlink(sun->sun_path) == -1 && errno != ENOENT) { + log_warn("%s: unlink %s", __func__, sun->sun_path); + return (-1); + } + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); + if (fd == -1) { + log_warn("%s: socket", __func__); + return (-1); + } + if (bind(fd, (struct sockaddr *)sun, sizeof(*sun)) == -1) { + log_warn("%s: bind %s", __func__, sun->sun_path); + goto fail; + } + /* authentication still applies; let local users reach it */ + if (chmod(sun->sun_path, MQTTD_UNIX_MODE) == -1) { + log_warn("%s: chmod %s", __func__, sun->sun_path); + goto fail; + } + if (listen(fd, SOMAXCONN) == -1) { + log_warn("%s: listen", __func__); + goto fail; + } + l->fd = fd; + return (0); + } + + switch (ss->ss_family) { + case AF_INET: + ((struct sockaddr_in *)ss)->sin_port = l->conf.port; + ((struct sockaddr_in *)ss)->sin_len = sizeof(struct sockaddr_in); + break; + case AF_INET6: + ((struct sockaddr_in6 *)ss)->sin6_port = l->conf.port; + ((struct sockaddr_in6 *)ss)->sin6_len = + sizeof(struct sockaddr_in6); + break; + default: + log_warnx("%s: unsupported address family", __func__); + return (-1); + } + + if ((fd = socket(ss->ss_family, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) { + log_warn("%s: socket", __func__); + return (-1); + } + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { + log_warn("%s: setsockopt SO_REUSEADDR", __func__); + goto fail; + } + if (ss->ss_family == AF_INET6 && + setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) { + log_warn("%s: setsockopt IPV6_V6ONLY", __func__); + goto fail; + } + if (bind(fd, (struct sockaddr *)ss, ss->ss_len) == -1) { + log_warn("%s: bind listener %s port %d", __func__, l->conf.name, + ntohs(l->conf.port)); + goto fail; + } + if (listen(fd, SOMAXCONN) == -1) { + log_warn("%s: listen", __func__); + goto fail; + } + + l->fd = fd; + return (0); +fail: + close(fd); + return (-1); +} + +static int +sockets_listener_launch(struct listener *l) +{ + log_debug("%s: configuring listener %s[%u] (fd %d)", __func__, + l->conf.name, l->conf.id, l->fd); + + if (listener_tls_init(l) == -1) { + log_warnx("%s: tls setup failed for listener %s", __func__, + l->conf.name); + return (-1); + } + + event_set(&l->ev, l->fd, EV_READ | EV_PERSIST, sockets_accept, l); + if (event_add(&l->ev, NULL) == -1) + fatal("%s: event_add", __func__); + evtimer_set(&l->pause, sockets_accept_paused, l); + return (0); +} + +/* a reload removed this listener; its connections live on without it */ +static void +sockets_listener_remove(struct listener *l) +{ + struct conn *c; + + RB_FOREACH(c, conntree, &conns) + if (c->l == l) + c->l = NULL; + + if (event_initialized(&l->ev)) + event_del(&l->ev); + if (evtimer_initialized(&l->pause)) + evtimer_del(&l->pause); + if (l->conf.tls) { + if (l->tls_config != NULL) + tls_config_free(l->tls_config); + if (l->tls_ctx != NULL) + tls_free(l->tls_ctx); + } + TAILQ_REMOVE(&env->listeners, l, entry); + env->nlisteners--; + config_free_listener(l); +} + +/* listeners that arrive after startup need recvfd, so the pledge stays */ +static void +sockets_launch(void) +{ + struct listener *l; + + TAILQ_FOREACH(l, &env->listeners, entry) + if (sockets_listener_launch(l) == -1) + fatalx("listener %s failed", l->conf.name); +} + +static void +sockets_dispatch_parent(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + struct listener *l; + struct tls_config_msg tlsmsg; + uint32_t lid; + int n, verbose; + + 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(); + } + } + 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__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_GLOBAL: + config_recv_global(env, &imsg); + break; + case IMSG_CFG_LISTENER: + config_recv_listener(env, &imsg); + break; + case IMSG_CFG_TLS: + IMSG_SIZE_CHECK(&imsg, &tlsmsg); + memcpy(&tlsmsg, imsg.data, sizeof(tlsmsg)); + if (tlsmsg.tls_type == TLS_CFG_BRIDGE_CA) { + sockets_bridge_ca(&imsg); + break; + } + if (config_recv_tls(env, &imsg) == -1) + fatalx("%s: bad tls configuration", __func__); + break; + case IMSG_CFG_BRIDGE: + sockets_bridge_config(&imsg); + break; + case IMSG_CFG_LISTENER_START: + IMSG_SIZE_CHECK(&imsg, &lid); + memcpy(&lid, imsg.data, sizeof(lid)); + if ((l = config_listener_byid(env, lid)) == NULL) + break; + if (sockets_listener_launch(l) == -1) + sockets_listener_remove(l); + else + log_info("listener %s added", l->conf.name); + break; + case IMSG_CFG_LISTENER_DEL: + IMSG_SIZE_CHECK(&imsg, &lid); + memcpy(&lid, imsg.data, sizeof(lid)); + if ((l = config_listener_byid(env, lid)) == NULL) + break; + log_info("listener %s removed", l->conf.name); + sockets_listener_remove(l); + break; + case IMSG_CFG_TLS_DONE: + IMSG_SIZE_CHECK(&imsg, &lid); + memcpy(&lid, imsg.data, sizeof(lid)); + if ((l = config_listener_byid(env, lid)) == NULL || + !l->conf.tls) + break; + if (listener_tls_reload(l) == 0) + log_info("listener %s: tls material reloaded", + l->conf.name); + else + log_warnx("listener %s: tls reload failed, " + "keeping the old keys", l->conf.name); + break; + case IMSG_CFG_DONE: + imsg_compose_event(socks_iev_parent, IMSG_CFG_DONE, 0, + 0, -1, NULL, 0); + break; + case IMSG_CTL_START: + sockets_launch(); + break; + case IMSG_CTL_VERBOSE: + IMSG_SIZE_CHECK(&imsg, &verbose); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + break; + case IMSG_CTL_SHOW_LISTENERS_REQUEST: + sockets_show_listeners(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from parent", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +sockets_dispatch_control(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + 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(); + } + } + 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__); + } + } + + /* control only listens to us; its 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); +} + +static void +sockets_dispatch_broker(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + struct conn *c; + struct conn_pkt hdr; + struct conn_close cc; + struct conn_keepalive ka; + size_t len; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_warnx("%s: lost broker process", __func__); + sockets_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + log_warnx("%s: lost broker process", __func__); + sockets_shutdown(); + } + fatal("%s: imsgbuf_write", __func__); + } + if (imsgbuf_queuelen(ibuf) <= MQTTD_QUEUE_LOW) + sockets_unthrottle(); + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CONN_PACKET: + len = IMSG_DATA_SIZE(&imsg); + if (len < sizeof(hdr)) { + log_warnx("%s: short packet imsg", __func__); + break; + } + memcpy(&hdr, imsg.data, sizeof(hdr)); + if (hdr.len != len - sizeof(hdr)) { + log_warnx("%s: chunk length mismatch", + __func__); + break; + } + if ((c = conn_find(hdr.id)) == NULL || c->closing) + break; + conn_write_chunk(c, &hdr, + (uint8_t *)imsg.data + sizeof(hdr)); + break; + case IMSG_CONN_CLOSE: + IMSG_SIZE_CHECK(&imsg, &cc); + memcpy(&cc, imsg.data, sizeof(cc)); + if ((c = conn_find(cc.id)) == NULL) + break; + /* let queued output drain before closing */ + c->closing = 1; + bufferevent_disable(c->bev, EV_READ); + if (EVBUFFER_LENGTH(EVBUFFER_OUTPUT(c->bev)) == 0) + conn_free(c, -1); + else + conn_set_timer(c, MQTTD_CONNECT_TIMEOUT); + break; + case IMSG_CONN_KEEPALIVE: + IMSG_SIZE_CHECK(&imsg, &ka); + memcpy(&ka, imsg.data, sizeof(ka)); + if ((c = conn_find(ka.id)) == NULL) + break; + c->keepalive = ka.keepalive; + if (ka.keepalive == 0) + evtimer_del(&c->timer); + else + conn_set_timer(c, (time_t)ka.keepalive * 3 / 2); + break; + case IMSG_BRIDGE_CONNECT: + sockets_bridge_connect(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from broker", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +sockets_show_listeners(struct imsg *imsg) +{ + struct ctl_listener_info li; + struct listener *l; + + TAILQ_FOREACH(l, &env->listeners, entry) { + memset(&li, 0, sizeof(li)); + li.id = l->conf.id; + strlcpy(li.name, l->conf.name, sizeof(li.name)); + sockaddr_ntop(&l->conf.ss, li.addr, sizeof(li.addr)); + li.port = l->conf.port; + li.tls = l->conf.tls; + li.websocket = l->conf.websocket; + li.proxy = l->conf.proxy; + li.fd = l->fd; + li.nconns = l->nconns; + if (imsg_compose_event(socks_iev_control, + IMSG_CTL_SHOW_LISTENERS_DATA, imsg->hdr.peerid, 0, -1, + &li, sizeof(li)) == -1) + return; + } + + imsg_compose_event(socks_iev_control, IMSG_CTL_SHOW_LISTENERS_END, + imsg->hdr.peerid, 0, -1, NULL, 0); +} + +/* keep a few descriptors free for the imsg pipes and libevent itself */ +static int +sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen) +{ + if (getdtablecount() + FD_RESERVE >= getdtablesize()) { + log_debug("%s: descriptor table nearly full", __func__); + errno = EMFILE; + return (-1); + } + + return (accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC)); +} + +static void +sockets_accept_paused(int fd, short event, void *arg) +{ + struct listener *l = arg; + + event_add(&l->ev, NULL); +} + +static void +sockets_accept(int fd, short event, void *arg) +{ + struct listener *l = arg; + struct conn *c; + struct sockaddr_storage ss; + struct timeval backoff = { 1, 0 }; + socklen_t len = sizeof(ss); + int cfd; + + cfd = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len); + if (cfd == -1) { + switch (errno) { + case EINTR: + case EWOULDBLOCK: + case ECONNABORTED: + return; + case EMFILE: + case ENFILE: + /* out of descriptors, give the clients a moment */ + event_del(&l->ev); + evtimer_add(&l->pause, &backoff); + return; + default: + log_warn("%s: accept", __func__); + return; + } + } + + if ((c = calloc(1, sizeof(*c))) == NULL) { + log_warn("%s: calloc", __func__); + close(cfd); + return; + } + + do { + c->id = ++conn_next_id; + } while (c->id == 0 || conn_find(c->id) != NULL); + c->fd = cfd; + c->l = l; + if (ss.ss_family == AF_UNIX) + strlcpy(c->peer, "unix", sizeof(c->peer)); + else + sockaddr_ntop(&ss, c->peer, sizeof(c->peer)); + evtimer_set(&c->timer, conn_timeout, c); + RB_INSERT(conntree, &conns, c); + l->nconns++; + + log_debug("%s: connection %u from %s on listener %s", __func__, c->id, + c->peer, l->conf.name); + + /* the client has this long to finish PROXY, TLS, WS and CONNECT */ + conn_set_timer(c, MQTTD_CONNECT_TIMEOUT); + + if (l->conf.proxy) { + event_set(&c->ev, cfd, EV_READ, conn_proxy_read, c); + event_add(&c->ev, NULL); + return; + } + + conn_begin(c); +} + +/* PROXY header consumed if any; now TLS, then the MQTT or WS stream */ +static void +conn_begin(struct conn *c) +{ + struct listener *l = c->l; + + if (l->conf.tls) { + if (tls_accept_socket(l->tls_ctx, &c->tls_ctx, c->fd) == -1) { + log_warnx("%s: tls accept failed: %s", __func__, + tls_error(l->tls_ctx)); + conn_free(c, -1); + return; + } + event_set(&c->ev, c->fd, EV_READ, conn_tls_handshake, c); + event_add(&c->ev, NULL); + return; + } + + conn_start(c); +} + +/* + * The proxy sends its header in one write before any client byte, so + * peek, work out the exact length, then consume exactly that much and + * leave the stream untouched for TLS or MQTT. + */ +static void +conn_proxy_read(int fd, short event, void *arg) +{ + struct conn *c = arg; + uint8_t buf[PROXY_PEEK]; + ssize_t n; + int used; + + n = recv(fd, buf, sizeof(buf), MSG_PEEK); + if (n == -1) { + if (errno == EAGAIN || errno == EINTR) { + event_add(&c->ev, NULL); + return; + } + conn_free(c, -1); + return; + } + if (n == 0) { + conn_free(c, -1); + return; + } + + if (n >= PROXY_V2_MIN && + memcmp(buf, "\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a", + 12) == 0) + used = proxy_parse_v2(c, buf, n); + else if (n >= 6 && memcmp(buf, "PROXY ", 6) == 0) + used = proxy_parse_v1(c, buf, n); + else { + log_warnx("%s: no PROXY header from %s", __func__, c->peer); + conn_free(c, -1); + return; + } + + if (used == 0) { + /* incomplete header, wait for the rest */ + if (n >= (ssize_t)sizeof(buf)) { + conn_free(c, -1); + return; + } + event_add(&c->ev, NULL); + return; + } + if (used == -1) { + log_warnx("%s: bad PROXY header from %s", __func__, c->peer); + conn_free(c, -1); + return; + } + + /* the peeked bytes are still there; this read cannot block */ + if (recv(fd, buf, used, MSG_WAITALL) != used) { + conn_free(c, -1); + return; + } + conn_begin(c); +} + +/* returns bytes consumed, 0 for more needed, -1 for malformed */ +static int +proxy_parse_v1(struct conn *c, const uint8_t *buf, size_t n) +{ + char line[PROXY_V1_MAX + 1], *p, *end, *fam, *src, *sport; + const char *eol; + size_t len; + + eol = memmem(buf, n < PROXY_V1_MAX ? n : PROXY_V1_MAX, "\r\n", 2); + if (eol == NULL) + return (n >= PROXY_V1_MAX ? -1 : 0); + len = eol - (const char *)buf; + memcpy(line, buf, len); + line[len] = '\0'; + + /* PROXY TCP4 src dst sport dport, or PROXY UNKNOWN ... */ + p = line + 6; + end = p; + fam = strsep(&end, " "); + if (fam == NULL) + return (-1); + if (strcmp(fam, "UNKNOWN") == 0) + return (len + 2); + if (strcmp(fam, "TCP4") != 0 && strcmp(fam, "TCP6") != 0) + return (-1); + src = strsep(&end, " "); + (void)strsep(&end, " "); + sport = strsep(&end, " "); + if (src == NULL || sport == NULL || end == NULL) + return (-1); + if (strchr(src, ':') != NULL) + snprintf(c->peer, sizeof(c->peer), "[%s]:%s", src, sport); + else + snprintf(c->peer, sizeof(c->peer), "%s:%s", src, sport); + return (len + 2); +} + +static int +proxy_parse_v2(struct conn *c, const uint8_t *buf, size_t n) +{ + char host[INET6_ADDRSTRLEN]; + size_t len, total; + uint8_t cmd, fam; + uint16_t port; + + if ((buf[12] >> 4) != 2) + return (-1); + cmd = buf[12] & 0x0f; + fam = buf[13]; + len = (size_t)buf[14] << 8 | buf[15]; + total = PROXY_V2_MIN + len; + if (total > PROXY_PEEK) + return (-1); + if (n < total) + return (0); + + /* LOCAL (health checks) keeps the proxy's own address */ + if (cmd != 0x01) + return (total); + + switch (fam) { + case 0x11: + if (len < 12) + return (-1); + inet_ntop(AF_INET, buf + 16, host, sizeof(host)); + port = (uint16_t)buf[24] << 8 | buf[25]; + snprintf(c->peer, sizeof(c->peer), "%s:%u", host, port); + break; + case 0x21: + if (len < 36) + return (-1); + inet_ntop(AF_INET6, buf + 16, host, sizeof(host)); + port = (uint16_t)buf[48] << 8 | buf[49]; + snprintf(c->peer, sizeof(c->peer), "[%s]:%u", host, port); + break; + default: + break; + } + return (total); +} + +static struct conn * +conn_find(uint32_t id) +{ + struct conn key; + + key.id = id; + return (RB_FIND(conntree, &conns, &key)); +} + +static void +conn_set_timer(struct conn *c, time_t secs) +{ + struct timeval tv; + + tv.tv_sec = secs; + tv.tv_usec = 0; + evtimer_del(&c->timer); + evtimer_add(&c->timer, &tv); +} + +static void +conn_tls_handshake(int fd, short event, void *arg) +{ + struct conn *c = arg; + ssize_t ret; + + ret = tls_handshake(c->tls_ctx); + if (ret == 0) { + event_del(&c->ev); + conn_start(c); + return; + } + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) { + event_del(&c->ev); + event_set(&c->ev, c->fd, ret == TLS_WANT_POLLIN ? EV_READ : + EV_WRITE, conn_tls_handshake, c); + event_add(&c->ev, NULL); + return; + } + + log_warnx("%s: tls handshake with %s failed: %s", __func__, c->peer, + tls_error(c->tls_ctx)); + conn_free(c, -1); +} + +static void +conn_start(struct conn *c) +{ + c->bev = bufferevent_new(c->fd, conn_readcb, conn_writecb, conn_errcb, + c); + if (c->bev == NULL) { + log_warn("%s: bufferevent_new", __func__); + conn_free(c, -1); + return; + } + + if (c->tls_ctx != NULL) { + event_set(&c->bev->ev_read, c->fd, EV_READ, conn_tls_readcb, + c->bev); + event_set(&c->bev->ev_write, c->fd, EV_WRITE, conn_tls_writecb, + c->bev); + } + + /* a single control packet may legitimately be this large */ + bufferevent_setwatermark(c->bev, EV_READ, 0, + env->conf.max_packet_size); + bufferevent_enable(c->bev, EV_READ | EV_WRITE); + + /* the broker hears about a websocket client once HTTP is done */ + if (c->l != NULL && c->l->conf.websocket) { + c->ws = WS_HANDSHAKE; + return; + } + + conn_open_send(c); +} + +static void +conn_open_send(struct conn *c) +{ + struct conn_open co; + + memset(&co, 0, sizeof(co)); + co.id = c->id; + co.listener_id = c->l != NULL ? c->l->conf.id : 0; + co.bridge_id = c->bridge != NULL ? c->bridge->conf.id : 0; + co.tls = c->tls_ctx != NULL; + strlcpy(co.peer, c->peer, sizeof(co.peer)); + if (c->tls_ctx != NULL && c->bridge == NULL) + conn_cert_user(c->tls_ctx, co.cert_user, sizeof(co.cert_user)); + imsg_compose_event(socks_iev_broker, IMSG_CONN_OPEN, 0, 0, -1, &co, + sizeof(co)); + c->opened = 1; + + /* the broker runs the keepalive for bridges itself */ + if (c->bridge != NULL) + evtimer_del(&c->timer); +} + +/* reason -1 means the broker is not told, it already knows or never knew */ +static void +conn_free(struct conn *c, int reason) +{ + struct conn_close cc; + + if (reason >= 0 && c->opened) { + cc.id = c->id; + cc.reason = reason; + imsg_compose_event(socks_iev_broker, IMSG_CONN_CLOSE, 0, 0, -1, + &cc, sizeof(cc)); + } + /* a bridge that never came up is reported so the broker retries */ + if (c->bridge != NULL && !c->opened) + sockets_bridge_failed(c); + + log_debug("%s: closing connection %u %s %s", __func__, c->id, + c->bridge != NULL ? "to" : "from", c->peer); + + evtimer_del(&c->timer); + if (event_initialized(&c->ev)) + event_del(&c->ev); + if (c->bev != NULL) + bufferevent_free(c->bev); + if (c->wsin != NULL) + evbuffer_free(c->wsin); + mqtt_buf_free(&c->wsout); + if (c->tls_ctx != NULL) { + tls_close(c->tls_ctx); + tls_free(c->tls_ctx); + } + close(c->fd); + RB_REMOVE(conntree, &conns, c); + if (c->throttled) + TAILQ_REMOVE(&throttled_conns, c, tentry); + if (c->l != NULL) + c->l->nconns--; + free(c); +} + +static struct sbridge * +sbridge_find(uint32_t id) +{ + struct sbridge *sb; + + TAILQ_FOREACH(sb, &sbridges, entry) + if (sb->conf.id == id) + return (sb); + return (NULL); +} + +static void +sockets_bridge_config(struct imsg *imsg) +{ + struct sbridge *sb; + + if ((sb = calloc(1, sizeof(*sb))) == NULL) + fatal("%s: calloc", __func__); + IMSG_SIZE_CHECK(imsg, &sb->conf); + memcpy(&sb->conf, imsg->data, sizeof(sb->conf)); + sb->conf.name[sizeof(sb->conf.name) - 1] = '\0'; + sb->conf.host[sizeof(sb->conf.host) - 1] = '\0'; + sb->conf.tls_ca_file = NULL; + sb->conf.tls_ca = NULL; + sb->conf.tls_ca_len = 0; + explicit_bzero(sb->conf.password, sizeof(sb->conf.password)); + TAILQ_INSERT_TAIL(&sbridges, sb, entry); +} + +static void +sockets_bridge_ca(struct imsg *imsg) +{ + struct tls_config_msg tls; + struct sbridge *sb; + + IMSG_SIZE_CHECK(imsg, &tls); + memcpy(&tls, imsg->data, sizeof(tls)); + if ((sb = sbridge_find(tls.id)) == NULL) + return; + if (config_recv_bridge_ca(&sb->conf, imsg) == -1) + log_warnx("bridge %s: bad ca material", sb->conf.name); +} + +static void +sockets_bridge_failed(struct conn *c) +{ + uint32_t id = c->bridge->conf.id; + + imsg_compose_event(socks_iev_broker, IMSG_BRIDGE_FAILED, 0, 0, -1, + &id, sizeof(id)); +} + +/* the broker wants this bridge up: start a non-blocking connect */ +static void +sockets_bridge_connect(struct imsg *imsg) +{ + struct sbridge *sb; + struct conn *c; + struct sockaddr_storage ss; + uint32_t id; + int fd; + + IMSG_SIZE_CHECK(imsg, &id); + memcpy(&id, imsg->data, sizeof(id)); + if ((sb = sbridge_find(id)) == NULL) { + log_warnx("%s: unknown bridge %u", __func__, id); + return; + } + + if (sb->conf.tls && sb->tls_config == NULL) { + if ((sb->tls_config = tls_config_new()) == NULL) + fatal("%s: tls_config_new", __func__); + if (sb->conf.tls_ca != NULL && + tls_config_set_ca_mem(sb->tls_config, sb->conf.tls_ca, + sb->conf.tls_ca_len) == -1) + log_warnx("bridge %s: %s", sb->conf.name, + tls_config_error(sb->tls_config)); + } + + memcpy(&ss, &sb->conf.ss, sizeof(ss)); + switch (ss.ss_family) { + case AF_INET: + ((struct sockaddr_in *)&ss)->sin_port = sb->conf.port; + break; + case AF_INET6: + ((struct sockaddr_in6 *)&ss)->sin6_port = sb->conf.port; + break; + } + + fd = socket(ss.ss_family, SOCK_STREAM | SOCK_NONBLOCK, 0); + if (fd == -1) { + log_warn("%s: socket", __func__); + imsg_compose_event(socks_iev_broker, IMSG_BRIDGE_FAILED, 0, 0, + -1, &id, sizeof(id)); + return; + } + + if ((c = calloc(1, sizeof(*c))) == NULL) { + log_warn("%s: calloc", __func__); + close(fd); + imsg_compose_event(socks_iev_broker, IMSG_BRIDGE_FAILED, 0, 0, + -1, &id, sizeof(id)); + return; + } + do { + c->id = ++conn_next_id; + } while (c->id == 0 || conn_find(c->id) != NULL); + c->fd = fd; + c->bridge = sb; + sockaddr_ntop(&ss, c->peer, sizeof(c->peer)); + evtimer_set(&c->timer, conn_timeout, c); + RB_INSERT(conntree, &conns, c); + conn_set_timer(c, MQTTD_CONNECT_TIMEOUT); + + if (connect(fd, (struct sockaddr *)&ss, ss.ss_len) == -1 && + errno != EINPROGRESS) { + log_warn("bridge %s: connect to %s", sb->conf.name, c->peer); + conn_free(c, -1); + return; + } + + event_set(&c->ev, fd, EV_WRITE, sockets_bridge_connected, c); + event_add(&c->ev, NULL); +} + +static void +sockets_bridge_connected(int fd, short event, void *arg) +{ + struct conn *c = arg; + struct sbridge *sb = c->bridge; + socklen_t len = sizeof(int); + int err = 0; + + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len) == -1) + err = errno; + if (err != 0) { + log_warnx("bridge %s: connect to %s: %s", sb->conf.name, + c->peer, strerror(err)); + conn_free(c, -1); + return; + } + + if (!sb->conf.tls) { + conn_start(c); + return; + } + + if ((c->tls_ctx = tls_client()) == NULL || + tls_configure(c->tls_ctx, sb->tls_config) == -1 || + tls_connect_socket(c->tls_ctx, fd, sb->conf.host) == -1) { + log_warnx("bridge %s: tls setup: %s", sb->conf.name, + c->tls_ctx != NULL ? tls_error(c->tls_ctx) : "no context"); + conn_free(c, -1); + return; + } + event_set(&c->ev, fd, EV_READ | EV_WRITE, conn_tls_handshake, c); + event_add(&c->ev, NULL); +} + +/* the CN of a verified client certificate, empty when there is none */ +static void +conn_cert_user(struct tls *ctx, char *buf, size_t len) +{ + const char *subj, *cn, *end; + size_t n; + + buf[0] = '\0'; + if (!tls_peer_cert_provided(ctx) || + (subj = tls_peer_cert_subject(ctx)) == NULL) + return; + if ((cn = strstr(subj, "/CN=")) == NULL) + return; + cn += 4; + end = strchr(cn, '/'); + n = end != NULL ? (size_t)(end - cn) : strlen(cn); + if (n == 0 || n >= len) + return; + memcpy(buf, cn, n); + buf[n] = '\0'; +} + +/* the broker pipe is full: stop reading from this client for now */ +static void +conn_throttle(struct conn *c) +{ + if (c->throttled) + return; + c->throttled = 1; + bufferevent_disable(c->bev, EV_READ); + TAILQ_INSERT_TAIL(&throttled_conns, c, tentry); +} + +static void +sockets_unthrottle(void) +{ + struct connq q; + struct conn *c; + + if (TAILQ_EMPTY(&throttled_conns)) + return; + + /* take the whole list, a resumed client may throttle again */ + TAILQ_INIT(&q); + TAILQ_CONCAT(&q, &throttled_conns, tentry); + + while ((c = TAILQ_FIRST(&q)) != NULL) { + TAILQ_REMOVE(&q, c, tentry); + c->throttled = 0; + if (c->closing || c->paused) + continue; + bufferevent_enable(c->bev, EV_READ); + /* input may already be buffered, and conn_readcb may free c */ + conn_readcb(c->bev, c); + } +} + +/* the stream is unusable; stop reading and let the broker say goodbye */ +static void +conn_error(struct conn *c, enum conn_error_reason reason) +{ + struct conn_error ce; + + if (c->paused) + return; + c->paused = 1; + bufferevent_disable(c->bev, EV_READ); + + ce.id = c->id; + ce.reason = reason; + imsg_compose_event(socks_iev_broker, IMSG_CONN_ERROR, 0, 0, -1, &ce, + sizeof(ce)); +} + +static int +conn_send_packet(struct conn *c, const uint8_t *data, uint32_t len) +{ + struct conn_pkt hdr; + struct iovec iov[2]; + uint32_t off = 0, n; + + hdr.id = c->id; + hdr.total = len; + + while (off < len) { + n = len - off; + if (n > CONN_PKT_CHUNK) + n = CONN_PKT_CHUNK; + hdr.offset = off; + hdr.len = n; + iov[0].iov_base = &hdr; + iov[0].iov_len = sizeof(hdr); + iov[1].iov_base = (void *)(data + off); + iov[1].iov_len = n; + if (imsg_composev_event(socks_iev_broker, IMSG_CONN_PACKET, 0, + 0, -1, iov, 2) == -1) { + log_warn("%s: imsg_composev_event", __func__); + return (-1); + } + off += n; + } + return (0); +} + +static void +conn_readcb(struct bufferevent *bev, void *arg) +{ + struct conn *c = arg; + struct evbuffer *in = EVBUFFER_INPUT(bev); + struct mqtt_fixed f; + size_t len, total; + int r; + + if (c->paused || c->closing) + return; + + if (c->ws == WS_HANDSHAKE) { + if ((r = ws_handshake(c)) <= 0) + return; + } + if (c->ws == WS_OPEN) { + if (ws_decode(c) == -1) + return; + in = c->wsin; + } + + for (;;) { + len = EVBUFFER_LENGTH(in); + if (len < 2) + break; + + r = mqtt_parse_fixed(EVBUFFER_DATA(in), len, &f); + if (r == -1) { + conn_error(c, CONN_ERR_MALFORMED); + return; + } + if (r == 1) + break; + + total = (size_t)f.hdrlen + f.remaining; + if (total > env->conf.max_packet_size) { + conn_error(c, CONN_ERR_TOOBIG); + return; + } + if (len < total) + break; + + if (conn_send_packet(c, EVBUFFER_DATA(in), total) == -1) { + conn_free(c, CONN_CLOSE_ERROR); + return; + } + evbuffer_drain(in, total); + + if (c->keepalive != 0) + conn_set_timer(c, (time_t)c->keepalive * 3 / 2); + + if (imsgbuf_queuelen(&socks_iev_broker->ibuf) > + MQTTD_QUEUE_HIGH) { + conn_throttle(c); + break; + } + } +} + +static void +conn_writecb(struct bufferevent *bev, void *arg) +{ + struct conn *c = arg; + + if (c->closing && EVBUFFER_LENGTH(EVBUFFER_OUTPUT(bev)) == 0) + conn_free(c, -1); +} + +static void +conn_errcb(struct bufferevent *bev, short what, void *arg) +{ + struct conn *c = arg; + + if (c->closing) { + conn_free(c, -1); + return; + } + if (what & EVBUFFER_EOF) + conn_free(c, CONN_CLOSE_EOF); + else + conn_free(c, CONN_CLOSE_ERROR); +} + +static void +conn_timeout(int fd, short event, void *arg) +{ + struct conn *c = arg; + + if (c->closing) { + conn_free(c, -1); + return; + } + log_debug("%s: connection %u from %s timed out", __func__, c->id, + c->peer); + conn_free(c, CONN_CLOSE_TIMEOUT); +} + +/* + * broker -> client: one chunk. Plain connections take it as is; a + * websocket collects the packet and sends it as one binary frame. + */ +static void +conn_write_chunk(struct conn *c, const struct conn_pkt *hdr, + const uint8_t *data) +{ + size_t hiwat; + + if (c->ws == WS_OPEN) { + if (hdr->offset == 0) + mqtt_buf_reset(&c->wsout); + if (mqtt_buf_bytes(&c->wsout, data, hdr->len) == -1) { + conn_free(c, CONN_CLOSE_ERROR); + return; + } + if (hdr->offset + hdr->len < hdr->total) + return; + ws_send_frame(c, WS_OP_BINARY, c->wsout.data, c->wsout.len); + mqtt_buf_reset(&c->wsout); + } else + bufferevent_write(c->bev, data, hdr->len); + + hiwat = (size_t)env->conf.max_packet_size * MQTTD_OUTBUF_FACTOR; + if (EVBUFFER_LENGTH(EVBUFFER_OUTPUT(c->bev)) > hiwat) { + log_debug("%s: connection %u from %s is not draining", + __func__, c->id, c->peer); + conn_free(c, CONN_CLOSE_OVERFLOW); + } +} + +/* case-insensitive header lookup in a NUL terminated request */ +static int +ws_header_find(const char *req, const char *name, const char **val, + size_t *vlen) +{ + const char *p = req, *eol; + size_t nlen = strlen(name); + + while ((p = strchr(p, '\n')) != NULL) { + p++; + if (strncasecmp(p, name, nlen) != 0 || p[nlen] != ':') + continue; + p += nlen + 1; + while (*p == ' ' || *p == '\t') + p++; + if ((eol = strpbrk(p, "\r\n")) == NULL) + eol = p + strlen(p); + *val = p; + *vlen = eol - p; + while (*vlen > 0 && ((*val)[*vlen - 1] == ' ' || + (*val)[*vlen - 1] == '\t')) + (*vlen)--; + return (1); + } + return (0); +} + +/* is token one of the comma separated values, case-insensitively */ +static int +ws_token_in(const char *val, size_t vlen, const char *token) +{ + const char *p = val, *end = val + vlen, *comma; + size_t tlen = strlen(token), len; + + while (p < end) { + while (p < end && (*p == ' ' || *p == ',')) + p++; + comma = memchr(p, ',', end - p); + len = (comma != NULL ? comma : end) - p; + while (len > 0 && p[len - 1] == ' ') + len--; + if (len == tlen && strncasecmp(p, token, tlen) == 0) + return (1); + p += len; + } + return (0); +} + +/* + * RFC 6455 opening handshake for the "mqtt" subprotocol. Returns 1 when + * the connection is now a websocket, 0 when more bytes are needed and + * -1 when the connection was closed. + */ +static int +ws_handshake(struct conn *c) +{ + struct evbuffer *in = EVBUFFER_INPUT(c->bev); + SHA1_CTX sha; + uint8_t digest[SHA1_DIGEST_LENGTH]; + char req[MQTTD_WS_MAXHDR + 1], accept[64], resp[512]; + const char *key, *val, *bad = NULL; + u_char *end; + size_t len, klen, vlen; + int n; + + end = evbuffer_find(in, (u_char *)"\r\n\r\n", 4); + if (end == NULL) { + if (EVBUFFER_LENGTH(in) > MQTTD_WS_MAXHDR) { + conn_free(c, -1); + return (-1); + } + return (0); + } + len = end + 4 - EVBUFFER_DATA(in); + memcpy(req, EVBUFFER_DATA(in), len); + req[len] = '\0'; + evbuffer_drain(in, len); + + if (strncmp(req, "GET ", 4) != 0) + bad = "not a GET"; + else if (!ws_header_find(req, "Upgrade", &val, &vlen) || + !ws_token_in(val, vlen, "websocket")) + bad = "no websocket upgrade"; + else if (!ws_header_find(req, "Connection", &val, &vlen) || + !ws_token_in(val, vlen, "Upgrade")) + bad = "no connection upgrade"; + else if (!ws_header_find(req, "Sec-WebSocket-Version", &val, &vlen) || + vlen != 2 || strncmp(val, "13", 2) != 0) + bad = "unsupported websocket version"; + else if (!ws_header_find(req, "Sec-WebSocket-Protocol", &val, &vlen) || + !ws_token_in(val, vlen, "mqtt")) + bad = "mqtt subprotocol not offered"; + else if (!ws_header_find(req, "Sec-WebSocket-Key", &key, &klen) || + klen == 0 || klen > 64) + bad = "no key"; + + if (bad != NULL) { + log_debug("%s: %s: %s", __func__, c->peer, bad); + n = snprintf(resp, sizeof(resp), "HTTP/1.1 400 Bad Request\r\n" + "Connection: close\r\nContent-Length: 0\r\n\r\n"); + bufferevent_write(c->bev, resp, n); + c->closing = 1; + bufferevent_disable(c->bev, EV_READ); + conn_set_timer(c, MQTTD_CONNECT_TIMEOUT); + return (-1); + } + + SHA1Init(&sha); + SHA1Update(&sha, (const uint8_t *)key, klen); + SHA1Update(&sha, (const uint8_t *)MQTTD_WS_GUID, + sizeof(MQTTD_WS_GUID) - 1); + SHA1Final(digest, &sha); + if (b64_ntop(digest, sizeof(digest), accept, sizeof(accept)) == -1) { + conn_free(c, -1); + return (-1); + } + + n = snprintf(resp, sizeof(resp), "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\nConnection: Upgrade\r\n" + "Sec-WebSocket-Accept: %s\r\nSec-WebSocket-Protocol: mqtt\r\n" + "\r\n", accept); + bufferevent_write(c->bev, resp, n); + + if ((c->wsin = evbuffer_new()) == NULL) { + conn_free(c, -1); + return (-1); + } + c->ws = WS_OPEN; + conn_open_send(c); + return (1); +} + +static void +ws_send_frame(struct conn *c, uint8_t opcode, const uint8_t *data, size_t len) +{ + uint8_t hdr[10]; + size_t hlen = 2; + + hdr[0] = 0x80 | opcode; + if (len < 126) + hdr[1] = len; + else if (len < 65536) { + hdr[1] = 126; + hdr[2] = len >> 8; + hdr[3] = len & 0xff; + hlen = 4; + } else { + hdr[1] = 127; + hdr[2] = 0; + hdr[3] = 0; + hdr[4] = 0; + hdr[5] = 0; + hdr[6] = (len >> 24) & 0xff; + hdr[7] = (len >> 16) & 0xff; + hdr[8] = (len >> 8) & 0xff; + hdr[9] = len & 0xff; + hlen = 10; + } + bufferevent_write(c->bev, hdr, hlen); + if (len > 0) + bufferevent_write(c->bev, data, len); +} + +/* + * Unwrap every complete frame in the socket buffer into the MQTT byte + * stream. Clients must mask; control frames are answered here. + * Returns -1 when the connection was closed. + */ +static int +ws_decode(struct conn *c) +{ + struct evbuffer *in = EVBUFFER_INPUT(c->bev); + uint8_t *d, mask[4], opcode; + size_t len, hlen, plen, i; + + for (;;) { + len = EVBUFFER_LENGTH(in); + if (len < 2) + return (0); + d = EVBUFFER_DATA(in); + + if (d[0] & 0x70) { + log_debug("%s: %s: reserved bits set", __func__, + c->peer); + goto bad; + } + opcode = d[0] & 0x0f; + if ((d[1] & 0x80) == 0) { + log_debug("%s: %s: unmasked client frame", __func__, + c->peer); + goto bad; + } + plen = d[1] & 0x7f; + hlen = 2; + if (plen == 126) { + if (len < 4) + return (0); + plen = (size_t)d[2] << 8 | d[3]; + hlen = 4; + } else if (plen == 127) { + if (len < 10) + return (0); + if (d[2] | d[3] | d[4] | d[5]) { + goto bad; + } + plen = (size_t)d[6] << 24 | (size_t)d[7] << 16 | + (size_t)d[8] << 8 | d[9]; + hlen = 10; + } + if (plen > (size_t)env->conf.max_packet_size + 16) { + log_debug("%s: %s: frame too large", __func__, + c->peer); + goto bad; + } + if (len < hlen + 4 + plen) + return (0); + memcpy(mask, d + hlen, 4); + hlen += 4; + for (i = 0; i < plen; i++) + d[hlen + i] ^= mask[i % 4]; + + switch (opcode) { + case WS_OP_CONT: + case WS_OP_TEXT: + case WS_OP_BINARY: + if (evbuffer_add(c->wsin, d + hlen, plen) == -1) { + conn_free(c, CONN_CLOSE_ERROR); + return (-1); + } + break; + case WS_OP_PING: + ws_send_frame(c, WS_OP_PONG, d + hlen, plen); + break; + case WS_OP_PONG: + break; + case WS_OP_CLOSE: + ws_send_frame(c, WS_OP_CLOSE, d + hlen, + plen >= 2 ? 2 : 0); + conn_free(c, CONN_CLOSE_EOF); + return (-1); + default: + log_debug("%s: %s: bad opcode %u", __func__, c->peer, + opcode); + goto bad; + } + evbuffer_drain(in, hlen + plen); + } +bad: + ws_send_frame(c, WS_OP_CLOSE, (const uint8_t *)"\x03\xea", 2); + conn_free(c, CONN_CLOSE_ERROR); + return (-1); +} + +static void +conn_tls_readcb(int fd, short event, void *arg) +{ + struct bufferevent *bufev = arg; + struct conn *c = bufev->cbarg; + uint8_t buf[TLS_READ_SIZE]; + ssize_t ret; + size_t len; + short what = EVBUFFER_READ; + + ret = tls_read(c->tls_ctx, buf, sizeof(buf)); + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) + goto retry; + if (ret < 0) { + what |= EVBUFFER_ERROR; + goto err; + } + if (ret == 0) { + what |= EVBUFFER_EOF; + goto err; + } + + if (evbuffer_add(bufev->input, buf, ret) == -1) { + what |= EVBUFFER_ERROR; + goto err; + } + + event_add(&bufev->ev_read, NULL); + + len = EVBUFFER_LENGTH(bufev->input); + if (bufev->wm_read.low != 0 && len < bufev->wm_read.low) + return; + + /* + * Over the high watermark: stop reading until the input buffer + * drains, but still hand what we have to the read callback, as + * libevent's own bufferevent_readcb does. thingsd returned here + * and stalled the connection. + */ + if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) { + event_del(&bufev->ev_read); + evbuffer_setcb(bufev->input, bufferevent_read_pressure_cb, + bufev); + } + + if (bufev->readcb != NULL) + (*bufev->readcb)(bufev, bufev->cbarg); + return; + +retry: + event_add(&bufev->ev_read, NULL); + return; +err: + (*bufev->errorcb)(bufev, what, bufev->cbarg); +} + +static void +conn_tls_writecb(int fd, short event, void *arg) +{ + struct bufferevent *bufev = arg; + struct conn *c = bufev->cbarg; + ssize_t ret; + short what = EVBUFFER_WRITE; + + if (EVBUFFER_LENGTH(bufev->output) > 0) { + ret = tls_write(c->tls_ctx, EVBUFFER_DATA(bufev->output), + EVBUFFER_LENGTH(bufev->output)); + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) + goto retry; + if (ret < 0) { + what |= EVBUFFER_ERROR; + goto err; + } + evbuffer_drain(bufev->output, ret); + } + + if (EVBUFFER_LENGTH(bufev->output) != 0) + event_add(&bufev->ev_write, NULL); + + if (bufev->writecb != NULL && + EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low) + (*bufev->writecb)(bufev, bufev->cbarg); + return; + +retry: + event_add(&bufev->ev_write, NULL); + return; +err: + (*bufev->errorcb)(bufev, what, bufev->cbarg); +} blob - /dev/null blob + a962f79d409314ad194fd3dc298e4df25f9832e99d5d6ea6627fc36cf84395fb (mode 644) --- /dev/null +++ mqttd/sparkplug.c @@ -0,0 +1,219 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * Sparkplug Aware behaviour (Sparkplug 3.0, section 10): keep a retained + * copy of every NBIRTH and DBIRTH under $sparkplug/certificates/, drop + * them again on NDEATH and DDEATH, and stamp the broker's time into an + * NDEATH will as it is published. Payloads are protobuf; adding a field + * is appending its tag and value, and for a scalar the last one wins. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "mqttd.h" +#include "mqtt.h" +#include "broker.h" + +#define SP_LEVELS 5 + +struct clear_ctx { + TAILQ_HEAD(, clear_item) list; +}; + +struct clear_item { + TAILQ_ENTRY(clear_item) entry; + char *topic; + size_t len; +}; + +static int sp_split(const char *, size_t, const char **, size_t *); +static void sp_retain_copy(struct message *); +static void sp_clear_filter(const char *); +static void sp_clear_cb(struct message *, void *); + +/* spBv1.0///[/] into up to five levels */ +static int +sp_split(const char *topic, size_t len, const char **lv, size_t *ll) +{ + size_t i, start = 0; + int n = 0; + + for (i = 0; i <= len && n < SP_LEVELS; i++) { + if (i < len && topic[i] != '/') + continue; + lv[n] = topic + start; + ll[n] = i - start; + n++; + start = i + 1; + } + return (n); +} + +static void +sp_retain_copy(struct message *m) +{ + struct message *c; + char topic[MQTTD_MAXTEXT + sizeof(MQTTD_SPARKPLUG_CERTS)]; + int n; + + n = snprintf(topic, sizeof(topic), "%s%.*s", MQTTD_SPARKPLUG_CERTS, + (int)m->topic_len, m->topic); + if (n < 0 || (size_t)n >= sizeof(topic)) + return; + + c = message_new((const uint8_t *)topic, n, m->payload, m->payload_len, + m->qos, 1); + if (c == NULL) + return; + if (m->props_len > 0 && (c->props = malloc(m->props_len)) != NULL) { + memcpy(c->props, m->props, m->props_len); + c->props_len = m->props_len; + } + topic_retain(&broker_env->topics, c); + message_unref(c); +} + +static void +sp_clear_cb(struct message *m, void *arg) +{ + struct clear_ctx *ctx = arg; + struct clear_item *ci; + + if ((ci = calloc(1, sizeof(*ci))) == NULL) + return; + if ((ci->topic = strndup(m->topic, m->topic_len)) == NULL) { + free(ci); + return; + } + ci->len = m->topic_len; + TAILQ_INSERT_TAIL(&ctx->list, ci, entry); +} + +/* retained entries cannot be removed while the trie is being walked */ +static void +sp_clear_filter(const char *filter) +{ + struct clear_ctx ctx; + struct clear_item *ci; + struct message *e; + + TAILQ_INIT(&ctx.list); + topic_retained_match(&broker_env->topics, filter, strlen(filter), + sp_clear_cb, &ctx); + + while ((ci = TAILQ_FIRST(&ctx.list)) != NULL) { + TAILQ_REMOVE(&ctx.list, ci, entry); + e = message_new((const uint8_t *)ci->topic, ci->len, NULL, 0, 0, + 1); + if (e != NULL) { + topic_retain(&broker_env->topics, e); + message_unref(e); + } + free(ci->topic); + free(ci); + } +} + +void +sparkplug_publish_hook(struct message *m) +{ + const char *lv[SP_LEVELS]; + size_t ll[SP_LEVELS]; + char filter[MQTTD_MAXTEXT + sizeof(MQTTD_SPARKPLUG_CERTS)]; + int n; + + if (m->topic_len < sizeof(MQTTD_SPARKPLUG_PREFIX) - 1 || + memcmp(m->topic, MQTTD_SPARKPLUG_PREFIX, + sizeof(MQTTD_SPARKPLUG_PREFIX) - 1) != 0) + return; + + n = sp_split(m->topic, m->topic_len, lv, ll); + if (n < 4) + return; + + if ((ll[2] == 6 && memcmp(lv[2], "NBIRTH", 6) == 0 && n == 4) || + (ll[2] == 6 && memcmp(lv[2], "DBIRTH", 6) == 0 && n == 5)) { + sp_retain_copy(m); + return; + } + + if (ll[2] == 6 && memcmp(lv[2], "NDEATH", 6) == 0 && n == 4) { + snprintf(filter, sizeof(filter), "%sspBv1.0/%.*s/NBIRTH/%.*s", + MQTTD_SPARKPLUG_CERTS, (int)ll[1], lv[1], (int)ll[3], lv[3]); + sp_clear_filter(filter); + snprintf(filter, sizeof(filter), "%sspBv1.0/%.*s/DBIRTH/%.*s/+", + MQTTD_SPARKPLUG_CERTS, (int)ll[1], lv[1], (int)ll[3], lv[3]); + sp_clear_filter(filter); + return; + } + + if (ll[2] == 6 && memcmp(lv[2], "DDEATH", 6) == 0 && n == 5) { + snprintf(filter, sizeof(filter), + "%sspBv1.0/%.*s/DBIRTH/%.*s/%.*s", MQTTD_SPARKPLUG_CERTS, + (int)ll[1], lv[1], (int)ll[3], lv[3], (int)ll[4], lv[4]); + sp_clear_filter(filter); + } +} + +/* an NDEATH will gets the broker's idea of when the node went away */ +void +sparkplug_will_hook(struct message *m) +{ + const char *lv[SP_LEVELS]; + size_t ll[SP_LEVELS]; + struct timeval tv; + uint64_t ms; + uint8_t *p, field[11]; + size_t flen = 0; + int n; + + if (m->topic_len < sizeof(MQTTD_SPARKPLUG_PREFIX) - 1 || + memcmp(m->topic, MQTTD_SPARKPLUG_PREFIX, + sizeof(MQTTD_SPARKPLUG_PREFIX) - 1) != 0) + return; + n = sp_split(m->topic, m->topic_len, lv, ll); + if (n != 4 || ll[2] != 6 || memcmp(lv[2], "NDEATH", 6) != 0) + return; + + gettimeofday(&tv, NULL); + ms = (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; + + /* field 1, varint: tag 0x08 then base-128 little endian */ + field[flen++] = 0x08; + do { + field[flen] = ms & 0x7f; + ms >>= 7; + if (ms != 0) + field[flen] |= 0x80; + flen++; + } while (ms != 0); + + if ((p = realloc(m->payload, m->payload_len + flen)) == NULL) + return; + memcpy(p + m->payload_len, field, flen); + m->payload = p; + m->payload_len += flen; +} blob - /dev/null blob + 2b04d12c640b869693613394191dfec47dd063e78e00725e883bfebbcb674692 (mode 644) --- /dev/null +++ mqttd/storage.c @@ -0,0 +1,503 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * The storage process is the only one that touches the database + * directory. It is chrooted into it, writes the record stream the + * broker sends into a temporary file and renames it into place on the + * end marker, and replays the last complete file at startup. It never + * looks inside a record. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mqttd.h" + +static void storage_dispatch_parent(int, short, void *); +static void storage_dispatch_broker(int, short, void *); +static void storage_sighdlr(int, short, void *); +static __dead void + storage_shutdown(void); +static void storage_record(struct imsg *); +static void storage_abort(void); +static void storage_load(void); +static int storage_send(uint32_t, const uint8_t *, uint32_t); +static int write_all(int, const void *, size_t); + +static struct imsgev *storage_iev_parent; +static struct imsgev *storage_iev_broker; + +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 int tmp_fd = -1; +static uint32_t cur_type; +static uint32_t cur_total; +static uint32_t cur_offset; +static int cur_active; + +__dead void +storage(int fd_parent, int fd_broker) +{ + log_procinit("storage"); + + /* + * With persistence off there is nothing to do but idle; chroot to + * the empty home like the other children. + */ + proc_privdrop_dir("storage", env->conf.persist ? env->dbdir : NULL); + + event_init(); + + signal_set(&ev_sigint, SIGINT, storage_sighdlr, NULL); + signal_set(&ev_sigterm, SIGTERM, storage_sighdlr, NULL); + signal_set(&ev_sighup, SIGHUP, storage_sighdlr, NULL); + signal_set(&ev_sigpipe, SIGPIPE, storage_sighdlr, NULL); + signal_set(&ev_sigusr1, SIGUSR1, storage_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 ((storage_iev_parent = calloc(1, sizeof(*storage_iev_parent))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(storage_iev_parent, fd_parent, + storage_dispatch_parent); + + if ((storage_iev_broker = calloc(1, sizeof(*storage_iev_broker))) + == NULL) + fatal("%s: calloc", __func__); + imsg_event_init(storage_iev_broker, fd_broker, + storage_dispatch_broker); + + if (pledge("stdio rpath wpath cpath", NULL) == -1) + fatal("pledge"); + + event_dispatch(); + + storage_shutdown(); +} + +static void +storage_sighdlr(int sig, short event, void *arg) +{ + switch (sig) { + case SIGINT: + case SIGTERM: + /* the broker's final snapshot is still on its way */ + break; + case SIGHUP: + case SIGPIPE: + case SIGUSR1: + break; + default: + fatalx("%s: unexpected signal", __func__); + } +} + +static __dead void +storage_shutdown(void) +{ + storage_abort(); + + if (storage_iev_parent != NULL) { + imsg_event_shutdown(storage_iev_parent); + free(storage_iev_parent); + } + imsg_event_shutdown(storage_iev_broker); + free(storage_iev_broker); + + log_info("storage exiting, pid %d", getpid()); + exit(0); +} + +static void +storage_dispatch_parent(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n, verbose; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + /* + * The parent is going away. Keep running until the + * broker has sent its last snapshot and closed too. + */ + log_debug("%s: parent gone, waiting for the broker", + __func__); + imsg_event_shutdown(iev); + free(iev); + storage_iev_parent = NULL; + return; + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) { + imsg_event_shutdown(iev); + free(iev); + storage_iev_parent = NULL; + return; + } + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_CFG_GLOBAL: + config_recv_global(env, &imsg); + break; + case IMSG_CFG_DONE: + if (env->conf.persist) + storage_load(); + imsg_compose_event(iev, IMSG_CFG_DONE, 0, 0, -1, NULL, + 0); + break; + case IMSG_CTL_START: + break; + case IMSG_CTL_VERBOSE: + IMSG_SIZE_CHECK(&imsg, &verbose); + memcpy(&verbose, imsg.data, sizeof(verbose)); + log_setverbose(verbose); + break; + default: + log_warnx("%s: unexpected imsg %d from parent", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static void +storage_dispatch_broker(int fd, short event, void *arg) +{ + struct imsgev *iev = arg; + struct imsgbuf *ibuf = &iev->ibuf; + struct imsg imsg; + int n; + + if (event & EV_READ) { + if ((n = imsgbuf_read(ibuf)) == -1) + fatal("%s: imsgbuf_read", __func__); + if (n == 0) { + log_debug("%s: broker gone", __func__); + storage_shutdown(); + } + } + if (event & EV_WRITE) { + if (imsgbuf_write(ibuf) == -1) { + if (errno == EPIPE) + storage_shutdown(); + fatal("%s: imsgbuf_write", __func__); + } + } + + for (;;) { + if ((n = imsgbuf_get(ibuf, &imsg)) == -1) + fatal("%s: imsgbuf_get", __func__); + if (n == 0) + break; + + switch (imsg.hdr.type) { + case IMSG_DB_RECORD: + storage_record(&imsg); + break; + default: + log_warnx("%s: unexpected imsg %d from broker", + __func__, imsg.hdr.type); + break; + } + imsg_free(&imsg); + } + imsg_event_add(iev); +} + +static int +write_all(int fd, const void *buf, size_t len) +{ + const uint8_t *p = buf; + ssize_t n; + + while (len > 0) { + if ((n = write(fd, p, len)) == -1) { + if (errno == EINTR) + continue; + return (-1); + } + p += n; + len -= n; + } + return (0); +} + +/* a snapshot in progress is worthless; drop it and keep the last good file */ +static void +storage_abort(void) +{ + if (tmp_fd == -1) + return; + close(tmp_fd); + tmp_fd = -1; + unlink(MQTTD_DB_TMP); + cur_active = 0; +} + +static void +storage_record(struct imsg *imsg) +{ + struct db_chunk hdr; + struct db_rec_hdr rh; + uint8_t *p = imsg->data; + size_t len = IMSG_DATA_SIZE(imsg); + + if (len < sizeof(hdr)) { + log_warnx("%s: short record imsg", __func__); + return; + } + memcpy(&hdr, p, sizeof(hdr)); + p += sizeof(hdr); + len -= sizeof(hdr); + + if (hdr.len != len || hdr.offset > hdr.total || + hdr.len > hdr.total - hdr.offset) { + log_warnx("%s: bad chunk, aborting snapshot", __func__); + storage_abort(); + return; + } + + if (hdr.offset == 0) { + if (hdr.type == DB_BEGIN) { + storage_abort(); + tmp_fd = open(MQTTD_DB_TMP, + O_WRONLY | O_CREAT | O_TRUNC, 0600); + if (tmp_fd == -1) { + log_warn("%s: open %s", __func__, + MQTTD_DB_TMP); + return; + } + } + if (tmp_fd == -1) + return; + if (cur_active) { + log_warnx("%s: record started mid record, aborting", + __func__); + storage_abort(); + return; + } + cur_type = hdr.type; + cur_total = hdr.total; + cur_offset = 0; + cur_active = 1; + + rh.type = hdr.type; + rh.len = hdr.total; + if (write_all(tmp_fd, &rh, sizeof(rh)) == -1) { + log_warn("%s: write", __func__); + storage_abort(); + return; + } + } + + if (tmp_fd == -1) + return; + if (!cur_active || hdr.type != cur_type || hdr.total != cur_total || + hdr.offset != cur_offset) { + log_warnx("%s: chunk sequence error, aborting snapshot", + __func__); + storage_abort(); + return; + } + + if (len > 0 && write_all(tmp_fd, p, len) == -1) { + log_warn("%s: write", __func__); + storage_abort(); + return; + } + cur_offset += len; + if (cur_offset < cur_total) + return; + cur_active = 0; + + if (cur_type != DB_END) + return; + + if (fsync(tmp_fd) == -1 || close(tmp_fd) == -1) { + log_warn("%s: fsync", __func__); + close(tmp_fd); + tmp_fd = -1; + unlink(MQTTD_DB_TMP); + return; + } + tmp_fd = -1; + if (rename(MQTTD_DB_TMP, MQTTD_DB_FILE) == -1) { + log_warn("%s: rename", __func__); + unlink(MQTTD_DB_TMP); + return; + } + log_debug("%s: snapshot written", __func__); +} + +static int +storage_send(uint32_t type, const uint8_t *data, uint32_t len) +{ + struct db_chunk hdr; + struct iovec iov[2]; + uint32_t off = 0, n; + + hdr.type = type; + hdr.total = len; + do { + n = len - off; + if (n > DB_CHUNK) + n = DB_CHUNK; + hdr.offset = off; + hdr.len = n; + iov[0].iov_base = &hdr; + iov[0].iov_len = sizeof(hdr); + iov[1].iov_base = (void *)(data + off); + iov[1].iov_len = n; + if (imsg_composev_event(storage_iev_broker, IMSG_DB_RECORD, 0, + 0, -1, iov, n > 0 ? 2 : 1) == -1) + return (-1); + off += n; + } while (off < len); + return (0); +} + +/* replay the last complete snapshot into the broker */ +static void +storage_load(void) +{ + struct db_rec_hdr rh; + struct stat st; + uint8_t *buf = NULL, *p; + size_t size, off = 0, got; + ssize_t n; + uint64_t records = 0; + int fd; + + if ((fd = open(MQTTD_DB_FILE, O_RDONLY)) == -1) { + if (errno != ENOENT) + log_warn("%s: open %s", __func__, MQTTD_DB_FILE); + else + log_info("no database to restore"); + goto done; + } + if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) { + log_warn("%s: fstat", __func__); + goto done; + } + if (st.st_size > MQTTD_DB_MAXSIZE) { + log_warnx("%s: database too large (%lld bytes)", __func__, + (long long)st.st_size); + goto done; + } + size = st.st_size; + if (size < sizeof(rh)) { + log_warnx("%s: database truncated", __func__); + goto done; + } + if ((buf = malloc(size)) == NULL) { + log_warn("%s: malloc", __func__); + goto done; + } + for (got = 0; got < size; got += n) { + if ((n = read(fd, buf + got, size - got)) == -1) { + if (errno == EINTR) { + n = 0; + continue; + } + log_warn("%s: read", __func__); + goto done; + } + if (n == 0) + break; + } + size = got; + + while (off + sizeof(rh) <= size) { + memcpy(&rh, buf + off, sizeof(rh)); + off += sizeof(rh); + if (rh.len > size - off) { + log_warnx("%s: truncated record, restore stops", + __func__); + break; + } + p = buf + off; + off += rh.len; + + if (records == 0) { + if (rh.type != DB_BEGIN || rh.len < 8 || + memcmp(p, MQTTD_DB_MAGIC, 8) != 0) { + log_warnx("%s: not an mqttd database", + __func__); + break; + } + records++; + continue; + } + records++; + if (rh.type == DB_END) + break; + if (storage_send(rh.type, p, rh.len) == -1) { + log_warn("%s: storage_send", __func__); + break; + } + } + log_info("restored %llu record(s) from %s", + (unsigned long long)(records > 0 ? records - 1 : 0), + MQTTD_DB_FILE); + +done: + free(buf); + if (fd != -1) + close(fd); + imsg_compose_event(storage_iev_broker, IMSG_DB_RESTORE_END, 0, 0, -1, + NULL, 0); +} blob - /dev/null blob + 85a85f04ec1c824acc0d0fbd07fdb515543e247fd231659b9984353c38fe8092 (mode 644) --- /dev/null +++ mqttd/tls.c @@ -0,0 +1,284 @@ +/* + * Copyright (c) 2020-2021, 2026 Tracey Emery + * Copyright (c) 2014-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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "mqttd.h" + +int +tls_load_keypair(struct listener *l) +{ + if (l->conf.tls == 0) + return (0); + + if ((l->conf.tls_cert = tls_load_file(l->conf.tls_cert_file, + &l->conf.tls_cert_len, NULL)) == NULL) + return (-1); + log_debug("%s: using certificate %s", __func__, + l->conf.tls_cert_file); + + /* XXX allow to specify password for encrypted key */ + if ((l->conf.tls_key = tls_load_file(l->conf.tls_key_file, + &l->conf.tls_key_len, NULL)) == NULL) + return (-1); + log_debug("%s: using private key %s", __func__, l->conf.tls_key_file); + + return (0); +} + +int +tls_load_ca(struct listener *l) +{ + if ((l->conf.tls_flags & TLSFLAG_CA) == 0 || + l->conf.tls_ca_file == NULL) + return (0); + + if ((l->conf.tls_ca = tls_load_file(l->conf.tls_ca_file, + &l->conf.tls_ca_len, NULL)) == NULL) + return (-1); + log_debug("%s: using ca cert(s) from %s", __func__, + l->conf.tls_ca_file); + + return (0); +} + +int +tls_load_ocsp(struct listener *l) +{ + if (l->conf.tls == 0 || l->conf.tls_ocsp_staple_file == NULL) + return (0); + + if ((l->conf.tls_ocsp_staple = tls_load_file( + l->conf.tls_ocsp_staple_file, &l->conf.tls_ocsp_staple_len, + NULL)) == NULL) { + log_warnx("%s: failed to load ocsp staple from %s", __func__, + l->conf.tls_ocsp_staple_file); + return (-1); + } + if (l->conf.tls_ocsp_staple_len == 0) { + log_warnx("%s: ignoring 0 length ocsp staple from %s", + __func__, l->conf.tls_ocsp_staple_file); + return (0); + } + log_debug("%s: using ocsp staple from %s", __func__, + l->conf.tls_ocsp_staple_file); + + return (0); +} + +int +tls_load_crl(struct listener *l) +{ + if ((l->conf.tls_flags & TLSFLAG_CA) == 0 || + l->conf.tls_crl_file == NULL) + return (0); + + if ((l->conf.tls_crl = tls_load_file(l->conf.tls_crl_file, + &l->conf.tls_crl_len, NULL)) == NULL) + return (-1); + log_debug("%s: using crl(s) from %s", __func__, l->conf.tls_crl_file); + + return (0); +} + +int +listener_tls_load(struct listener *l) +{ + if (l->conf.tls == 0) + return (0); + + log_debug("%s: loading tls material for listener %s[%u]", __func__, + l->conf.name, l->conf.id); + + if (tls_load_keypair(l) == -1) { + log_warnx("failed to load public/private keys for listener %s", + l->conf.name); + return (-1); + } + if (tls_load_ca(l) == -1) { + log_warnx("failed to load ca cert(s) for listener %s", + l->conf.name); + return (-1); + } + if (tls_load_crl(l) == -1) { + log_warnx("failed to load crl(s) for listener %s", l->conf.name); + return (-1); + } + if (tls_load_ocsp(l) == -1) { + log_warnx("failed to load ocsp staple for listener %s", + l->conf.name); + return (-1); + } + return (0); +} + +/* + * Build a fresh server context from newly received material and swap it + * in. Connections already accepted keep their own references to the + * old context, so freeing it is safe. On failure the old keys stay. + */ +int +listener_tls_reload(struct listener *l) +{ + struct tls_config *oconf = l->tls_config; + struct tls *octx = l->tls_ctx; + + l->tls_config = NULL; + l->tls_ctx = NULL; + + if (listener_tls_init(l) == -1) { + if (l->tls_config != NULL) + tls_config_free(l->tls_config); + if (l->tls_ctx != NULL) + tls_free(l->tls_ctx); + l->tls_config = oconf; + l->tls_ctx = octx; + + freezero(l->conf.tls_cert, l->conf.tls_cert_len); + freezero(l->conf.tls_key, l->conf.tls_key_len); + free(l->conf.tls_ca); + free(l->conf.tls_crl); + l->conf.tls_cert = NULL; + l->conf.tls_key = NULL; + l->conf.tls_ca = NULL; + l->conf.tls_crl = NULL; + l->conf.tls_cert_len = 0; + l->conf.tls_key_len = 0; + l->conf.tls_ca_len = 0; + l->conf.tls_crl_len = 0; + return (-1); + } + + if (oconf != NULL) + tls_config_free(oconf); + if (octx != NULL) + tls_free(octx); + return (0); +} + +int +listener_tls_init(struct listener *l) +{ + if (l->conf.tls == 0) + return (0); + + if (l->conf.tls_cert == NULL || l->conf.tls_key == NULL) { + log_warnx("%s: listener %s has no certificate", __func__, + l->conf.name); + return (-1); + } + + log_debug("%s: initializing tls for listener %s[%u]", __func__, + l->conf.name, l->conf.id); + + if (tls_init() != 0) { + log_warnx("%s: failed to initialise tls", __func__); + return (-1); + } + if ((l->tls_config = tls_config_new()) == NULL) { + log_warnx("%s: failed to get tls config", __func__); + return (-1); + } + if ((l->tls_ctx = tls_server()) == NULL) { + log_warnx("%s: failed to get tls server", __func__); + return (-1); + } + + if (tls_config_set_protocols(l->tls_config, + l->conf.tls_protocols) != 0) { + log_warnx("%s: failed to set tls protocols: %s", __func__, + tls_config_error(l->tls_config)); + return (-1); + } + if (tls_config_set_ciphers(l->tls_config, l->conf.tls_ciphers) != 0) { + log_warnx("%s: failed to set tls ciphers: %s", __func__, + tls_config_error(l->tls_config)); + return (-1); + } + if (tls_config_set_dheparams(l->tls_config, + l->conf.tls_dhe_params) != 0) { + log_warnx("%s: failed to set tls dhe params: %s", __func__, + tls_config_error(l->tls_config)); + return (-1); + } + if (tls_config_set_ecdhecurves(l->tls_config, + l->conf.tls_ecdhe_curves) != 0) { + log_warnx("%s: failed to set tls ecdhe curves: %s", __func__, + tls_config_error(l->tls_config)); + return (-1); + } + + if (tls_config_set_keypair_ocsp_mem(l->tls_config, l->conf.tls_cert, + l->conf.tls_cert_len, l->conf.tls_key, l->conf.tls_key_len, + l->conf.tls_ocsp_staple, l->conf.tls_ocsp_staple_len) != 0) { + log_warnx("%s: failed to set tls certificate/key: %s", + __func__, tls_config_error(l->tls_config)); + return (-1); + } + + if (l->conf.tls_ca != NULL) { + if (tls_config_set_ca_mem(l->tls_config, l->conf.tls_ca, + l->conf.tls_ca_len) != 0) { + log_warnx("%s: failed to add ca cert(s)", __func__); + return (-1); + } + if (l->conf.tls_crl != NULL && + tls_config_set_crl_mem(l->tls_config, l->conf.tls_crl, + l->conf.tls_crl_len) != 0) { + log_warnx("%s: failed to add crl(s)", __func__); + return (-1); + } + if (l->conf.tls_flags & TLSFLAG_OPTIONAL) + tls_config_verify_client_optional(l->tls_config); + else + tls_config_verify_client(l->tls_config); + } + + if (tls_configure(l->tls_ctx, l->tls_config) != 0) { + log_warnx("%s: failed to configure tls: %s", __func__, + tls_error(l->tls_ctx)); + return (-1); + } + + /* the key material has been copied into the context */ + tls_config_clear_keys(l->tls_config); + + freezero(l->conf.tls_cert, l->conf.tls_cert_len); + freezero(l->conf.tls_key, l->conf.tls_key_len); + free(l->conf.tls_ca); + free(l->conf.tls_crl); + + l->conf.tls_cert = NULL; + l->conf.tls_key = NULL; + l->conf.tls_ca = NULL; + l->conf.tls_crl = NULL; + l->conf.tls_cert_len = 0; + l->conf.tls_key_len = 0; + l->conf.tls_ca_len = 0; + l->conf.tls_crl_len = 0; + + return (0); +} blob - /dev/null blob + 1e6f45dca20e0af25345b5b8725d882622313c3ca2dc0c82c4d43acdab51ed54 (mode 644) --- /dev/null +++ mqttd/topic.c @@ -0,0 +1,552 @@ +/* + * Copyright (c) 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 + * 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. + */ + +/* + * Subscription trie. Every topic level is a node; wildcard levels are + * ordinary children named "+" and "#". Retained messages hang off the + * node of their exact topic. Shared subscriptions ($share//...) + * live on the node of the underlying filter, grouped by name, and a + * matching message goes to one member of each group. + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include "mqttd.h" +#include "broker.h" + +struct topic_node; +RB_HEAD(topic_children, topic_node); + +struct share_group { + TAILQ_ENTRY(share_group) entry; + struct sublist subs; + struct subscription *next; + char *name; + size_t nsubs; +}; +TAILQ_HEAD(sharelist, share_group); + +struct topic_node { + RB_ENTRY(topic_node) entry; + struct topic_node *parent; + struct topic_children children; + struct sublist subs; + struct sharelist groups; + struct message *retained; + char *level; + size_t levellen; +}; + +static int topic_node_cmp(struct topic_node *, struct topic_node *); + +RB_PROTOTYPE_STATIC(topic_children, topic_node, entry, topic_node_cmp); +RB_GENERATE_STATIC(topic_children, topic_node, entry, topic_node_cmp); + +static struct topic_node + *node_child(struct topic_node *, const char *, size_t); +static struct topic_node + *node_child_create(struct topic_node *, const char *, size_t); +static struct topic_node + *node_walk(struct topic_tree *, const char *, size_t, int); +static int node_empty(struct topic_node *); +static void node_free(struct topic_node *); +static void node_prune(struct topic_tree *, struct topic_node *); +static void node_deliver(struct topic_node *, topic_sub_cb, void *); +static struct share_group + *group_find(struct topic_node *, const char *); +static struct share_group + *group_create(struct topic_node *, const char *); +static void match_level(struct topic_node *, const char *, size_t, int, + topic_sub_cb, void *); +static void retained_walk(struct topic_node *, int, topic_msg_cb, void *); +static void retained_level(struct topic_node *, const char *, size_t, int, + topic_msg_cb, void *); +static void expire_walk(struct topic_tree *, struct topic_node *, time_t); +static int name_walk(struct topic_node *, char *, size_t, size_t *); + +static int +topic_node_cmp(struct topic_node *a, struct topic_node *b) +{ + size_t n = a->levellen < b->levellen ? a->levellen : b->levellen; + int r; + + if (n > 0 && (r = memcmp(a->level, b->level, n)) != 0) + return (r); + if (a->levellen < b->levellen) + return (-1); + if (a->levellen > b->levellen) + return (1); + return (0); +} + +void +topic_init(struct topic_tree *t) +{ + memset(t, 0, sizeof(*t)); + if ((t->root = calloc(1, sizeof(*t->root))) == NULL) + fatal("%s: calloc", __func__); + t->root->level = ""; + RB_INIT(&t->root->children); + TAILQ_INIT(&t->root->subs); + TAILQ_INIT(&t->root->groups); +} + +static struct topic_node * +node_child(struct topic_node *n, const char *lvl, size_t len) +{ + struct topic_node key; + + key.level = (char *)lvl; + key.levellen = len; + return (RB_FIND(topic_children, &n->children, &key)); +} + +static struct topic_node * +node_child_create(struct topic_node *n, const char *lvl, size_t len) +{ + struct topic_node *c; + + if ((c = node_child(n, lvl, len)) != NULL) + return (c); + + if ((c = calloc(1, sizeof(*c))) == NULL) + return (NULL); + if ((c->level = malloc(len + 1)) == NULL) { + free(c); + return (NULL); + } + memcpy(c->level, lvl, len); + c->level[len] = '\0'; + c->levellen = len; + c->parent = n; + RB_INIT(&c->children); + TAILQ_INIT(&c->subs); + TAILQ_INIT(&c->groups); + RB_INSERT(topic_children, &n->children, c); + return (c); +} + +static struct topic_node * +node_walk(struct topic_tree *t, const char *path, size_t len, int create) +{ + struct topic_node *n = t->root, *c; + size_t i, start = 0; + + for (i = 0; i <= len; i++) { + if (i < len && path[i] != '/') + continue; + if (create) + c = node_child_create(n, path + start, i - start); + else + c = node_child(n, path + start, i - start); + if (c == NULL) + return (NULL); + n = c; + start = i + 1; + } + return (n); +} + +static int +node_empty(struct topic_node *n) +{ + return (TAILQ_EMPTY(&n->subs) && TAILQ_EMPTY(&n->groups) && + n->retained == NULL && RB_EMPTY(&n->children)); +} + +static void +node_free(struct topic_node *n) +{ + free(n->level); + free(n); +} + +static void +node_prune(struct topic_tree *t, struct topic_node *n) +{ + struct topic_node *p; + + while (n != t->root && node_empty(n)) { + p = n->parent; + RB_REMOVE(topic_children, &p->children, n); + node_free(n); + n = p; + } +} + +static struct share_group * +group_find(struct topic_node *n, const char *name) +{ + struct share_group *g; + + TAILQ_FOREACH(g, &n->groups, entry) + if (strcmp(g->name, name) == 0) + return (g); + return (NULL); +} + +static struct share_group * +group_create(struct topic_node *n, const char *name) +{ + struct share_group *g; + + if ((g = group_find(n, name)) != NULL) + return (g); + if ((g = calloc(1, sizeof(*g))) == NULL) + return (NULL); + if ((g->name = strdup(name)) == NULL) { + free(g); + return (NULL); + } + TAILQ_INIT(&g->subs); + TAILQ_INSERT_TAIL(&n->groups, g, entry); + return (g); +} + +struct topic_node * +topic_subscribe(struct topic_tree *t, const char *filter, size_t len, + struct subscription *sub) +{ + struct topic_node *n; + struct share_group *g; + + if ((n = node_walk(t, filter, len, 1)) == NULL) + return (NULL); + + if (sub->share != NULL) { + if ((g = group_create(n, sub->share)) == NULL) { + node_prune(t, n); + return (NULL); + } + TAILQ_INSERT_TAIL(&g->subs, sub, node_entry); + g->nsubs++; + if (g->next == NULL) + g->next = sub; + } else + TAILQ_INSERT_TAIL(&n->subs, sub, node_entry); + + sub->node = n; + t->nsubs++; + return (n); +} + +void +topic_unsubscribe(struct topic_tree *t, struct subscription *sub) +{ + struct topic_node *n = sub->node; + struct share_group *g; + + if (n == NULL) + return; + + if (sub->share != NULL) { + if ((g = group_find(n, sub->share)) == NULL) + fatalx("%s: share group %s missing", __func__, + sub->share); + if (g->next == sub) + g->next = TAILQ_NEXT(sub, node_entry); + TAILQ_REMOVE(&g->subs, sub, node_entry); + g->nsubs--; + if (g->next == NULL) + g->next = TAILQ_FIRST(&g->subs); + if (TAILQ_EMPTY(&g->subs)) { + TAILQ_REMOVE(&n->groups, g, entry); + free(g->name); + free(g); + } + } else + TAILQ_REMOVE(&n->subs, sub, node_entry); + + sub->node = NULL; + t->nsubs--; + node_prune(t, n); +} + +struct subscription * +topic_find_sub(struct topic_tree *t, const char *filter, size_t len, + const char *share, struct session *s) +{ + struct topic_node *n; + struct share_group *g; + struct sublist *list; + struct subscription *sub; + + if ((n = node_walk(t, filter, len, 0)) == NULL) + return (NULL); + if (share != NULL) { + if ((g = group_find(n, share)) == NULL) + return (NULL); + list = &g->subs; + } else + list = &n->subs; + + TAILQ_FOREACH(sub, list, node_entry) + if (sub->session == s) + return (sub); + return (NULL); +} + +/* + * Ordinary subscriptions all get the message. Each share group gets it + * once, round robin, skipping members that are offline while any member + * is connected so queueing only happens when the whole group is away. + */ +static void +node_deliver(struct topic_node *n, topic_sub_cb cb, void *arg) +{ + struct subscription *sub, *tmp, *start, *pick; + struct share_group *g; + + TAILQ_FOREACH_SAFE(sub, &n->subs, node_entry, tmp) + cb(sub, arg); + + TAILQ_FOREACH(g, &n->groups, entry) { + start = g->next != NULL ? g->next : TAILQ_FIRST(&g->subs); + if (start == NULL) + continue; + pick = NULL; + sub = start; + do { + if (sub->session->conn != NULL) { + pick = sub; + break; + } + sub = TAILQ_NEXT(sub, node_entry); + if (sub == NULL) + sub = TAILQ_FIRST(&g->subs); + } while (sub != start); + if (pick == NULL) + pick = start; + g->next = TAILQ_NEXT(pick, node_entry); + if (g->next == NULL) + g->next = TAILQ_FIRST(&g->subs); + cb(pick, arg); + } +} + +/* + * rest is the unconsumed part of the topic, NULL once every level has + * been matched. Wildcards at the first level never match topics that + * start with '$'. + */ +static void +match_level(struct topic_node *n, const char *rest, size_t restlen, + int first, topic_sub_cb cb, void *arg) +{ + struct topic_node *c; + const char *slash, *next; + size_t lvl, nextlen; + int dollar; + + dollar = first && restlen > 0 && rest[0] == '$'; + + if (!dollar && (c = node_child(n, "#", 1)) != NULL) + node_deliver(c, cb, arg); + + if (rest == NULL) { + node_deliver(n, cb, arg); + return; + } + + slash = memchr(rest, '/', restlen); + lvl = slash != NULL ? (size_t)(slash - rest) : restlen; + next = slash != NULL ? slash + 1 : NULL; + nextlen = slash != NULL ? restlen - lvl - 1 : 0; + + if ((c = node_child(n, rest, lvl)) != NULL) + match_level(c, next, nextlen, 0, cb, arg); + + if (!dollar && (c = node_child(n, "+", 1)) != NULL) + match_level(c, next, nextlen, 0, cb, arg); +} + +void +topic_match(struct topic_tree *t, const char *topic, size_t len, + topic_sub_cb cb, void *arg) +{ + match_level(t->root, topic, len, 1, cb, arg); +} + +int +topic_retain(struct topic_tree *t, struct message *m) +{ + struct topic_node *n; + + if (m->payload_len == 0) { + n = node_walk(t, m->topic, m->topic_len, 0); + if (n != NULL && n->retained != NULL) { + message_unref(n->retained); + n->retained = NULL; + t->nretained--; + node_prune(t, n); + } + return (0); + } + + if ((n = node_walk(t, m->topic, m->topic_len, 1)) == NULL) + return (-1); + if (n->retained != NULL) + message_unref(n->retained); + else + t->nretained++; + message_ref(m); + n->retained = m; + return (0); +} + +struct message * +topic_retained_get(struct topic_tree *t, const char *topic, size_t len) +{ + struct topic_node *n; + + if ((n = node_walk(t, topic, len, 0)) == NULL) + return (NULL); + return (n->retained); +} + +static void +retained_walk(struct topic_node *n, int skip_dollar, topic_msg_cb cb, + void *arg) +{ + struct topic_node *c; + + if (n->retained != NULL) + cb(n->retained, arg); + RB_FOREACH(c, topic_children, &n->children) { + if (skip_dollar && c->levellen > 0 && c->level[0] == '$') + continue; + retained_walk(c, 0, cb, arg); + } +} + +static void +retained_level(struct topic_node *n, const char *rest, size_t restlen, + int first, topic_msg_cb cb, void *arg) +{ + struct topic_node *c; + const char *slash, *next; + size_t lvl, nextlen; + + if (rest == NULL) { + if (n->retained != NULL) + cb(n->retained, arg); + return; + } + + slash = memchr(rest, '/', restlen); + lvl = slash != NULL ? (size_t)(slash - rest) : restlen; + next = slash != NULL ? slash + 1 : NULL; + nextlen = slash != NULL ? restlen - lvl - 1 : 0; + + if (lvl == 1 && rest[0] == '#') { + if (n->retained != NULL) + cb(n->retained, arg); + RB_FOREACH(c, topic_children, &n->children) { + if (first && c->levellen > 0 && c->level[0] == '$') + continue; + retained_walk(c, 0, cb, arg); + } + return; + } + + if (lvl == 1 && rest[0] == '+') { + RB_FOREACH(c, topic_children, &n->children) { + if (first && c->levellen > 0 && c->level[0] == '$') + continue; + retained_level(c, next, nextlen, 0, cb, arg); + } + return; + } + + if ((c = node_child(n, rest, lvl)) != NULL) + retained_level(c, next, nextlen, 0, cb, arg); +} + +void +topic_retained_match(struct topic_tree *t, const char *filter, size_t len, + topic_msg_cb cb, void *arg) +{ + retained_level(t->root, filter, len, 1, cb, arg); +} + +void +topic_retained_foreach(struct topic_tree *t, topic_msg_cb cb, void *arg) +{ + retained_walk(t->root, 0, cb, arg); +} + +static void +expire_walk(struct topic_tree *t, struct topic_node *n, time_t now) +{ + struct topic_node *c, *tmp; + + RB_FOREACH_SAFE(c, topic_children, &n->children, tmp) { + expire_walk(t, c, now); + if (node_empty(c)) { + RB_REMOVE(topic_children, &n->children, c); + node_free(c); + } + } + if (n->retained != NULL && message_expired(n->retained, now)) { + message_unref(n->retained); + n->retained = NULL; + t->nretained--; + } +} + +void +topic_retained_expire(struct topic_tree *t, time_t now) +{ + expire_walk(t, t->root, now); +} + +static int +name_walk(struct topic_node *n, char *buf, size_t size, size_t *off) +{ + if (n->parent == NULL) + return (0); + if (name_walk(n->parent, buf, size, off) == -1) + return (-1); + if (n->parent->parent != NULL) { + if (*off + 1 >= size) + return (-1); + buf[(*off)++] = '/'; + } + if (*off + n->levellen >= size) + return (-1); + memcpy(buf + *off, n->level, n->levellen); + *off += n->levellen; + buf[*off] = '\0'; + return (0); +} + +int +topic_node_name(struct topic_node *n, char *buf, size_t size) +{ + size_t off = 0; + + if (size == 0) + return (-1); + buf[0] = '\0'; + return (name_walk(n, buf, size, &off)); +}