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 we can't fill at the end, move everything back. */
162 if (rbsize - reoff < 1500 && rsoff != 0) {
163 memmove(rbuf, rbuf + rsoff, reoff - rsoff);
167 /* If we still can't fill alloc some new memory. */
168 if (rbsize - reoff < 1500) {
169 if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
173 nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff);
181 smtp_newline(int fd, short event, void *arg)
183 struct event *stdinev = (struct event *)arg;
184 static char *line = NULL, *linedup = NULL;
185 static size_t linesize = 0;
186 static size_t dupsize = 0;
188 char *start, *end, *type, *direction, *phase, *params;
191 uint64_t reqid, token;
194 while ((linelen = smtp_getline(&line, &linesize)) > 0) {
195 if (dupsize < linesize) {
196 if ((linedup = realloc(linedup, linesize)) == NULL)
200 strlcpy(linedup, line, dupsize);
202 if ((start = strchr(type, '|')) == NULL)
203 fatalx("Invalid line received: missing version: %s", linedup);
205 if ((end = strchr(start, '|')) == NULL)
206 fatalx("Invalid line received: missing time: %s", linedup);
208 if (strcmp(start, "1") != 0)
209 fatalx("Unsupported protocol received: %s: %s", start, linedup);
212 if ((direction = strchr(start, '|')) == NULL)
213 fatalx("Invalid line received: missing direction: %s", linedup);
214 direction++[0] = '\0';
215 tm.tv_sec = (time_t) strtoull(start, &end, 10);
217 if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
218 fatalx("Invalid line received: invalid timestamp: %s", linedup);
221 tm.tv_nsec = strtol(start, &end, 10);
222 if (start[0] == '\0' || end[0] != '\0')
223 fatalx("Invalid line received: invalid "
224 "timestamp: %s", linedup);
225 for (i = 9 - (end - start); i > 0; i--)
228 if ((phase = strchr(direction, '|')) == NULL)
229 fatalx("Invalid line receieved: missing phase: %s", linedup);
231 if ((start = strchr(phase, '|')) == NULL)
232 fatalx("Invalid line received: missing reqid: %s", linedup);
234 reqid = strtoull(start, ¶ms, 16);
235 if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
236 fatalx("Invalid line received: invalid reqid: %s", linedup);
239 for (i = 0; i < NITEMS(smtp_callbacks); i++) {
240 if (strcmp(type, smtp_callbacks[i].type) == 0 &&
241 strcmp(phase, smtp_callbacks[i].phase) == 0 &&
242 strcmp(direction, smtp_callbacks[i].direction) == 0)
245 if (i == NITEMS(smtp_callbacks)) {
246 fatalx("Invalid line received: received unregistered "
247 "%s: %s: %s", type, phase, linedup);
249 if (strcmp(type, "filter") == 0) {
251 token = strtoull(start, ¶ms, 16);
252 if (start[0] == '|' || params[0] != '|')
253 fatalx("Invalid line received: invalid token: %s", linedup);
255 smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
256 version, &tm, reqid, token, params);
258 smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
259 version, &tm, reqid, params);
261 if (linelen == 0 || errno != EAGAIN)
266 smtp_connect(struct smtp_callback *cb, int version, struct timespec *tm,
267 uint64_t reqid, uint64_t token, char *params)
269 struct inx_addr addrx;
273 void (*f)(char *, int, struct timespec *,char *, char *, uint64_t,
274 uint64_t, char *, struct inx_addr *);
277 if ((address = strchr(params, '|')) == NULL)
278 fatalx("Invalid line received: missing address: %s", params);
282 if (strncasecmp(address, "ipv6:", 5) == 0) {
287 ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
288 (void *)&(addrx.addr) : (void *)&(addrx.addr6));
290 fatalx("Invalid line received: Couldn't parse address: %s", params);
292 fatal("Couldn't convert address: %s", params);
295 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
300 smtp_data(struct smtp_callback *cb, int version, struct timespec *tm,
301 uint64_t reqid, uint64_t token, char *params)
303 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
307 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token);
311 smtp_dataline(struct smtp_callback *cb, int version, struct timespec *tm,
312 uint64_t reqid, uint64_t token, char *line)
314 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
318 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
323 smtp_in_link_disconnect(struct smtp_callback *cb, int version,
324 struct timespec *tm, uint64_t reqid, char *params)
326 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t);
329 f(cb->type, version, tm, cb->direction, cb->phase, reqid);
333 smtp_filter_proceed(uint64_t reqid, uint64_t token)
335 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
340 smtp_printf(const char *fmt, ...)
345 smtp_vprintf(fmt, ap);
350 smtp_vprintf(const char *fmt, va_list ap)
353 static struct smtp_writebuf buf = {NULL, 0, 0};
357 fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
361 if (fmtlen >= buf.bufsize - buf.buflen) {
362 buf.bufsize = buf.buflen + fmtlen + 1;
363 buf.buf = reallocarray(buf.buf, buf.bufsize,
367 fmtlen = vsnprintf(buf.buf + buf.buflen,
368 buf.bufsize - buf.buflen, fmt, cap);
373 buf.buflen += fmtlen;
375 if (strchr(buf.buf, '\n') != NULL)
376 smtp_write(STDOUT_FILENO, EV_WRITE, &buf);
380 smtp_write(int fd, short event, void *arg)
382 struct smtp_writebuf *buf = arg;
383 static struct event stdoutev;
384 static int evset = 0;
387 if (buf->buflen == 0)
389 if (event_pending(&stdoutev, EV_WRITE, NULL))
392 event_set(&stdoutev, fd, EV_WRITE, smtp_write, buf);
395 wlen = write(fd, buf->buf, buf->buflen);
397 if (errno != EAGAIN && errno != EINTR)
398 fatal("Failed to write to smtpd");
399 event_add(&stdoutev, NULL);
402 if (wlen < buf->buflen) {
403 memmove(buf->buf, buf->buf + wlen, buf->buflen - wlen);
404 event_add(&stdoutev, NULL);
410 smtp_filter_reject(uint64_t reqid, uint64_t token, int code,
411 const char *reason, ...)
415 if (code < 200 || code > 599)
416 fatalx("Invalid reject code");
418 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
420 va_start(ap, reason);
421 smtp_vprintf(reason, ap);
427 smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
431 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|disconnect|421 ",
433 va_start(ap, reason);
434 smtp_vprintf(reason, ap);
440 smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
444 smtp_printf("filter-dataline|%016"PRIx64"|%016"PRIx64"|", token, reqid);
446 smtp_vprintf(line, ap);
452 smtp_register(char *type, char *phase, char *direction, void *cb)
455 static int evinit = 0;
458 fatalx("Can't register when proc is running");
465 for (i = 0; i < NITEMS(smtp_callbacks); i++) {
466 if (strcmp(type, smtp_callbacks[i].type) == 0 &&
467 strcmp(phase, smtp_callbacks[i].phase) == 0 &&
468 strcmp(direction, smtp_callbacks[i].direction) == 0) {
469 if (smtp_callbacks[i].cb != NULL) {
473 smtp_callbacks[i].cb = cb;
474 smtp_printf("register|%s|%s|%s\n", type, direction,