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_data(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_data, NULL},
77 {"filter", "data-line", "smtp-in", .smtp_filter = smtp_dataline, NULL},
78 {"report", "link-disconnect", "smtp-in",
79 .smtp_report = smtp_in_link_disconnect, NULL}
85 smtp_register_filter_connect(void (*cb)(char *, int, struct timespec *, char *,
86 char *, uint64_t, uint64_t, char *, struct inx_addr *))
88 return smtp_register("filter", "connect", "smtp-in", (void *)cb);
92 smtp_register_filter_data(void (*cb)(char *, int, struct timespec *, char *,
93 char *, uint64_t, uint64_t))
95 return smtp_register("filter", "data", "smtp-in", (void *)cb);
99 smtp_register_filter_dataline(void (*cb)(char *, int, struct timespec *, char *,
100 char *, uint64_t, uint64_t, char *))
102 return smtp_register("filter", "data-line", "smtp-in", (void *)cb);
106 smtp_in_register_report_disconnect(void (*cb)(char *, int, struct timespec *,
107 char *, char *, uint64_t))
109 return smtp_register("report", "link-disconnect", "smtp-in", (void *)cb);
115 struct event stdinev;
117 smtp_printf("register|ready\n");
120 log_init(debug, LOG_MAIL);
121 event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline,
123 event_add(&stdinev, NULL);
125 if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
131 smtp_getline(char ** restrict buf, size_t * restrict size)
133 static char *rbuf = NULL;
134 static size_t rsoff = 0, reoff = 0;
135 static size_t rbsize = 0;
138 ssize_t strlen, nread;
141 if (rsoff != reoff) {
142 if ((sep = memchr(rbuf + rsoff, '\n', reoff - rsoff))
147 if (*size < (sepoff - rsoff + 1)) {
148 *size = sepoff - rsoff + 1;
149 *buf = realloc(*buf, sepoff - rsoff + 1);
154 strlen = strlcpy(*buf, rbuf + rsoff, *size);
156 fatalx("copy buffer too small");
161 if (reoff - rsoff < 1500) {
162 if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
166 // If we can't fill at the end, move everything back.
167 if (rbsize - reoff < 1500)
168 memmove(rbuf + rsoff, rbuf, reoff - rsoff);
169 nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff);
177 smtp_newline(int fd, short event, void *arg)
179 struct event *stdinev = (struct event *)arg;
180 char *line = NULL, *linedup = NULL;
184 char *start, *end, *type, *direction, *phase, *params;
187 uint64_t reqid, token;
190 while ((linelen = smtp_getline(&line, &linesize)) > 0) {
191 if (dupsize < linesize) {
192 if ((linedup = realloc(linedup, linesize)) == NULL)
196 strlcpy(linedup, line, dupsize);
198 if ((start = strchr(type, '|')) == NULL)
199 fatalx("Invalid line received: missing version: %s", linedup);
201 if ((end = strchr(start, '|')) == NULL)
202 fatalx("Invalid line received: missing time: %s", linedup);
204 if (strcmp(start, "1") != 0)
205 fatalx("Unsupported protocol received: %s: %s", start, linedup);
208 if ((direction = strchr(start, '|')) == NULL)
209 fatalx("Invalid line received: missing direction: %s", linedup);
210 direction++[0] = '\0';
211 tm.tv_sec = (time_t) strtoull(start, &end, 10);
213 if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
214 fatalx("Invalid line received: invalid timestamp: %s", linedup);
217 tm.tv_nsec = strtol(start, &end, 10);
218 if (start[0] == '\0' || end[0] != '\0')
219 fatalx("Invalid line received: invalid "
220 "timestamp: %s", linedup);
221 for (i = 9 - (end - start); i > 0; i--)
224 if ((phase = strchr(direction, '|')) == NULL)
225 fatalx("Invalid line receieved: missing phase: %s", linedup);
227 if ((start = strchr(phase, '|')) == NULL)
228 fatalx("Invalid line received: missing reqid: %s", linedup);
230 reqid = strtoull(start, ¶ms, 16);
231 if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
232 fatalx("Invalid line received: invalid reqid: %s", linedup);
235 for (i = 0; i < NITEMS(smtp_callbacks); i++) {
236 if (strcmp(type, smtp_callbacks[i].type) == 0 &&
237 strcmp(phase, smtp_callbacks[i].phase) == 0 &&
238 strcmp(direction, smtp_callbacks[i].direction) == 0)
241 if (i == NITEMS(smtp_callbacks)) {
242 fatalx("Invalid line received: received unregistered "
243 "%s: %s: %s", type, phase, linedup);
245 if (strcmp(type, "filter") == 0) {
247 token = strtoull(start, ¶ms, 16);
248 if (start[0] == '|' || params[0] != '|')
249 fatalx("Invalid line received: invalid token: %s", linedup);
251 smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
252 version, &tm, reqid, token, params);
254 smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
255 version, &tm, reqid, params);
257 if (linelen == 0 || errno != EAGAIN)
262 smtp_connect(struct smtp_callback *cb, int version, struct timespec *tm,
263 uint64_t reqid, uint64_t token, char *params)
265 struct inx_addr addrx;
269 void (*f)(char *, int, struct timespec *,char *, char *, uint64_t,
270 uint64_t, char *, struct inx_addr *);
273 if ((address = strchr(params, '|')) == NULL)
274 fatalx("Invalid line received: missing address: %s", params);
278 if (strncasecmp(address, "ipv6:", 5) == 0) {
283 ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
284 (void *)&(addrx.addr) : (void *)&(addrx.addr6));
286 fatalx("Invalid line received: Couldn't parse address: %s", params);
288 fatal("Couldn't convert address: %s", params);
291 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
296 smtp_data(struct smtp_callback *cb, int version, struct timespec *tm,
297 uint64_t reqid, uint64_t token, char *params)
299 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
303 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token);
307 smtp_dataline(struct smtp_callback *cb, int version, struct timespec *tm,
308 uint64_t reqid, uint64_t token, char *line)
310 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
314 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
319 smtp_in_link_disconnect(struct smtp_callback *cb, int version,
320 struct timespec *tm, uint64_t reqid, char *params)
322 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t);
325 f(cb->type, version, tm, cb->direction, cb->phase, reqid);
329 smtp_filter_proceed(uint64_t reqid, uint64_t token)
331 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
336 smtp_printf(const char *fmt, ...)
341 smtp_vprintf(fmt, ap);
346 smtp_vprintf(const char *fmt, va_list ap)
349 static struct smtp_writebuf buf = {NULL, 0, 0};
353 fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
357 if (fmtlen >= buf.bufsize - buf.buflen) {
358 buf.bufsize = buf.buflen + fmtlen + 1;
359 buf.buf = reallocarray(buf.buf, buf.bufsize,
363 fmtlen = vsnprintf(buf.buf + buf.buflen,
364 buf.bufsize - buf.buflen, fmt, cap);
368 buf.buflen += fmtlen;
370 if (strchr(buf.buf, '\n') != NULL)
371 smtp_write(STDOUT_FILENO, EV_WRITE, &buf);
375 smtp_write(int fd, short event, void *arg)
377 struct smtp_writebuf *buf = arg;
378 static struct event stdoutev;
379 static int evset = 0;
382 if (buf->buflen == 0)
384 if (event_pending(&stdoutev, EV_WRITE, NULL))
387 event_set(&stdoutev, fd, EV_WRITE, smtp_write, buf);
390 wlen = write(fd, buf->buf, buf->buflen);
392 if (errno != EAGAIN && errno != EINTR)
393 fatal("Failed to write to smtpd");
394 event_add(&stdoutev, NULL);
397 if (wlen < buf->buflen) {
398 memmove(buf->buf, buf->buf + wlen, buf->buflen - wlen);
399 event_add(&stdoutev, NULL);
405 smtp_filter_reject(uint64_t reqid, uint64_t token, int code,
406 const char *reason, ...)
410 if (code < 200 || code > 599)
411 fatalx("Invalid reject code");
413 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
415 va_start(ap, reason);
416 smtp_vprintf(reason, ap);
422 smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
426 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|disconnect|421 ",
428 va_start(ap, reason);
429 smtp_vprintf(reason, ap);
435 smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
439 smtp_printf("filter-dataline|%016"PRIx64"|%016"PRIx64"|", token, reqid);
441 smtp_vprintf(line, ap);
447 smtp_register(char *type, char *phase, char *direction, void *cb)
450 static int evinit = 0;
453 fatalx("Can't register when proc is running");
460 for (i = 0; i < NITEMS(smtp_callbacks); i++) {
461 if (strcmp(type, smtp_callbacks[i].type) == 0 &&
462 strcmp(phase, smtp_callbacks[i].phase) == 0 &&
463 strcmp(direction, smtp_callbacks[i].direction) == 0) {
464 if (smtp_callbacks[i].cb != NULL) {
468 smtp_callbacks[i].cb = cb;
469 smtp_printf("register|%s|%s|%s\n", type, direction,