2 * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/socket.h>
19 #include <arpa/inet.h>
32 #include "smtp_proc.h"
34 #define NITEMS(x) (sizeof(x) / sizeof(*x))
39 extern struct event_base *current_base;
41 static int smtp_register(char *, char *, char *, void *);
42 static ssize_t smtp_getline(char ** restrict, size_t * restrict);
43 static void smtp_newline(int, short, void *);
44 static void smtp_connect(struct smtp_callback *, int, struct timespec *,
45 uint64_t, uint64_t, char *);
46 static void smtp_noargs(struct smtp_callback *, int, struct timespec *,
47 uint64_t, uint64_t, char *);
48 static void smtp_dataline(struct smtp_callback *, int, struct timespec *,
49 uint64_t, uint64_t, char *);
50 static void smtp_in_link_disconnect(struct smtp_callback *, int, struct timespec *,
52 static void smtp_printf(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)));
54 static void smtp_vprintf(const char *, va_list);
55 static void smtp_write(int, short, void *);
57 struct smtp_writebuf {
63 struct smtp_callback {
68 void (*smtp_filter)(struct smtp_callback *, int,
69 struct timespec *, uint64_t, uint64_t, char *);
70 void (*smtp_report)(struct smtp_callback *, int,
71 struct timespec *, uint64_t, char *);
74 } smtp_callbacks[] = {
75 {"filter", "connect", "smtp-in", .smtp_filter = smtp_connect, NULL},
76 {"filter", "data", "smtp-in", .smtp_filter = smtp_noargs, NULL},
77 {"filter", "data-line", "smtp-in", .smtp_filter = smtp_dataline, NULL},
78 {"filter", "commit", "smtp-in", .smtp_filter = smtp_noargs, NULL},
79 {"report", "link-disconnect", "smtp-in",
80 .smtp_report = smtp_in_link_disconnect, NULL}
86 smtp_register_filter_connect(void (*cb)(char *, int, struct timespec *, char *,
87 char *, uint64_t, uint64_t, char *, struct inx_addr *))
89 return smtp_register("filter", "connect", "smtp-in", (void *)cb);
93 smtp_register_filter_data(void (*cb)(char *, int, struct timespec *, char *,
94 char *, uint64_t, uint64_t))
96 return smtp_register("filter", "data", "smtp-in", (void *)cb);
100 smtp_register_filter_dataline(void (*cb)(char *, int, struct timespec *, char *,
101 char *, uint64_t, uint64_t, char *))
103 return smtp_register("filter", "data-line", "smtp-in", (void *)cb);
107 smtp_register_filter_commit(void (*cb)(char *, int, struct timespec *, char *,
108 char *, uint64_t, uint64_t))
110 return smtp_register("filter", "commit", "smtp-in", (void *)cb);
114 smtp_in_register_report_disconnect(void (*cb)(char *, int, struct timespec *,
115 char *, char *, uint64_t))
117 return smtp_register("report", "link-disconnect", "smtp-in", (void *)cb);
123 struct event stdinev;
125 smtp_printf("register|ready\n");
128 event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline,
130 event_add(&stdinev, NULL);
132 if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
138 smtp_getline(char ** restrict buf, size_t * restrict size)
140 static char *rbuf = NULL;
141 static size_t rsoff = 0, reoff = 0;
142 static size_t rbsize = 0;
145 ssize_t strlen, nread;
148 if (rsoff != reoff) {
149 if ((sep = memchr(rbuf + rsoff, '\n', reoff - rsoff))
154 if (*size < (sepoff - rsoff + 1)) {
155 *size = sepoff - rsoff + 1;
156 *buf = realloc(*buf, sepoff - rsoff + 1);
161 strlen = strlcpy(*buf, rbuf + rsoff, *size);
163 errx(1, "copy buffer too small");
168 /* If we can't fill at the end, move everything back. */
169 if (rbsize - reoff < 1500 && rsoff != 0) {
170 memmove(rbuf, rbuf + rsoff, reoff - rsoff);
174 /* If we still can't fill alloc some new memory. */
175 if (rbsize - reoff < 1500) {
176 if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
180 nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff);
188 smtp_newline(int fd, short event, void *arg)
190 struct event *stdinev = (struct event *)arg;
191 static char *line = NULL, *linedup = NULL;
192 static size_t linesize = 0;
193 static size_t dupsize = 0;
195 char *start, *end, *type, *direction, *phase, *params;
198 uint64_t reqid, token;
201 while ((linelen = smtp_getline(&line, &linesize)) > 0) {
202 if (dupsize < linesize) {
203 if ((linedup = realloc(linedup, linesize)) == NULL)
207 strlcpy(linedup, line, dupsize);
209 if ((start = strchr(type, '|')) == NULL)
210 errx(1, "Invalid line received: missing version: %s", linedup);
212 if ((end = strchr(start, '|')) == NULL)
213 errx(1, "Invalid line received: missing time: %s", linedup);
215 if (strcmp(start, "1") != 0)
216 errx(1, "Unsupported protocol received: %s: %s", start, linedup);
219 if ((direction = strchr(start, '|')) == NULL)
220 errx(1, "Invalid line received: missing direction: %s", linedup);
221 direction++[0] = '\0';
222 tm.tv_sec = (time_t) strtoull(start, &end, 10);
224 if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
225 errx(1, "Invalid line received: invalid timestamp: %s", linedup);
228 tm.tv_nsec = strtol(start, &end, 10);
229 if (start[0] == '\0' || end[0] != '\0')
230 errx(1, "Invalid line received: invalid "
231 "timestamp: %s", linedup);
232 for (i = 9 - (end - start); i > 0; i--)
235 if ((phase = strchr(direction, '|')) == NULL)
236 errx(1, "Invalid line receieved: missing phase: %s", linedup);
238 if ((start = strchr(phase, '|')) == NULL)
239 errx(1, "Invalid line received: missing reqid: %s", linedup);
241 reqid = strtoull(start, ¶ms, 16);
242 if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
243 errx(1, "Invalid line received: invalid reqid: %s", linedup);
246 for (i = 0; i < NITEMS(smtp_callbacks); i++) {
247 if (strcmp(type, smtp_callbacks[i].type) == 0 &&
248 strcmp(phase, smtp_callbacks[i].phase) == 0 &&
249 strcmp(direction, smtp_callbacks[i].direction) == 0)
252 if (i == NITEMS(smtp_callbacks)) {
253 errx(1, "Invalid line received: received unregistered "
254 "%s: %s: %s", type, phase, linedup);
256 if (strcmp(type, "filter") == 0) {
258 token = strtoull(start, ¶ms, 16);
259 if (start[0] == '|' || params[0] != '|')
260 errx(1, "Invalid line received: invalid token: %s", linedup);
262 smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
263 version, &tm, reqid, token, params);
265 smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
266 version, &tm, reqid, params);
268 if (linelen == 0 || errno != EAGAIN)
273 smtp_connect(struct smtp_callback *cb, int version, struct timespec *tm,
274 uint64_t reqid, uint64_t token, char *params)
276 struct inx_addr addrx;
280 void (*f)(char *, int, struct timespec *,char *, char *, uint64_t,
281 uint64_t, char *, struct inx_addr *);
284 if ((address = strchr(params, '|')) == NULL)
285 errx(1, "Invalid line received: missing address: %s", params);
289 if (strncasecmp(address, "ipv6:", 5) == 0) {
294 ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
295 (void *)&(addrx.addr) : (void *)&(addrx.addr6));
297 errx(1, "Invalid line received: Couldn't parse address: %s", params);
299 err(1, "Couldn't convert address: %s", params);
302 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
307 smtp_noargs(struct smtp_callback *cb, int version, struct timespec *tm,
308 uint64_t reqid, uint64_t token, char *params)
310 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
314 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token);
318 smtp_dataline(struct smtp_callback *cb, int version, struct timespec *tm,
319 uint64_t reqid, uint64_t token, char *line)
321 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
325 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
330 smtp_in_link_disconnect(struct smtp_callback *cb, int version,
331 struct timespec *tm, uint64_t reqid, char *params)
333 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t);
336 f(cb->type, version, tm, cb->direction, cb->phase, reqid);
340 smtp_filter_proceed(uint64_t reqid, uint64_t token)
342 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
347 smtp_printf(const char *fmt, ...)
352 smtp_vprintf(fmt, ap);
357 smtp_vprintf(const char *fmt, va_list ap)
360 static struct smtp_writebuf buf = {NULL, 0, 0};
364 fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
368 if (fmtlen >= buf.bufsize - buf.buflen) {
369 buf.bufsize = buf.buflen + fmtlen + 1;
370 buf.buf = reallocarray(buf.buf, buf.bufsize,
374 fmtlen = vsnprintf(buf.buf + buf.buflen,
375 buf.bufsize - buf.buflen, fmt, cap);
380 buf.buflen += fmtlen;
382 if (strchr(buf.buf, '\n') != NULL)
383 smtp_write(STDOUT_FILENO, EV_WRITE, &buf);
387 smtp_write(int fd, short event, void *arg)
389 struct smtp_writebuf *buf = arg;
390 static struct event stdoutev;
391 static int evset = 0;
394 if (buf->buflen == 0)
397 event_set(&stdoutev, fd, EV_WRITE, smtp_write, buf);
400 wlen = write(fd, buf->buf, buf->buflen);
402 if (errno != EAGAIN && errno != EINTR)
403 err(1, "Failed to write to smtpd");
404 event_add(&stdoutev, NULL);
407 if (wlen < buf->buflen) {
408 memmove(buf->buf, buf->buf + wlen, buf->buflen - wlen);
409 event_add(&stdoutev, NULL);
412 if (buf->buflen == 0 && event_pending(&stdoutev, EV_WRITE, NULL))
413 event_del(&stdoutev);
417 smtp_filter_reject(uint64_t reqid, uint64_t token, int code,
418 const char *reason, ...)
422 if (code < 200 || code > 599)
423 errx(1, "Invalid reject code");
425 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
427 va_start(ap, reason);
428 smtp_vprintf(reason, ap);
434 smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
438 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|disconnect|421 ",
440 va_start(ap, reason);
441 smtp_vprintf(reason, ap);
447 smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
451 smtp_printf("filter-dataline|%016"PRIx64"|%016"PRIx64"|", token, reqid);
453 smtp_vprintf(line, ap);
459 smtp_register(char *type, char *phase, char *direction, void *cb)
462 static int evinit = 0;
465 errx(1, "Can't register when proc is running");
472 for (i = 0; i < NITEMS(smtp_callbacks); i++) {
473 if (strcmp(type, smtp_callbacks[i].type) == 0 &&
474 strcmp(phase, smtp_callbacks[i].phase) == 0 &&
475 strcmp(direction, smtp_callbacks[i].direction) == 0) {
476 if (smtp_callbacks[i].cb != NULL) {
480 smtp_callbacks[i].cb = cb;
481 smtp_printf("register|%s|%s|%s\n", type, direction,