Blob


1 /*
2 * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
3 *
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.
7 *
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.
15 */
16 #include <sys/time.h>
17 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <event.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <syslog.h>
29 #include <unistd.h>
31 #include "log.h"
32 #include "smtp_proc.h"
34 #define NITEMS(x) (sizeof(x) / sizeof(*x))
36 struct smtp_callback;
37 struct smtp_request;
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 *,
51 uint64_t, char *);
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 {
58 char *buf;
59 size_t bufsize;
60 size_t buflen;
61 };
63 struct smtp_callback {
64 char *type;
65 char *phase;
66 char *direction;
67 union {
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 *);
72 };
73 void *cb;
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}
81 };
83 static int ready = 0;
85 int
86 smtp_register_filter_connect(void (*cb)(char *, int, struct timespec *, char *,
87 char *, uint64_t, uint64_t, char *, struct inx_addr *))
88 {
89 return smtp_register("filter", "connect", "smtp-in", (void *)cb);
90 }
92 int
93 smtp_register_filter_data(void (*cb)(char *, int, struct timespec *, char *,
94 char *, uint64_t, uint64_t))
95 {
96 return smtp_register("filter", "data", "smtp-in", (void *)cb);
97 }
99 int
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);
106 int
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);
113 int
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);
120 void
121 smtp_run(int debug)
123 struct event stdinev;
125 smtp_printf("register|ready\n");
126 ready = 1;
128 log_init(debug, LOG_MAIL);
129 event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline,
130 &stdinev);
131 event_add(&stdinev, NULL);
133 if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
134 fatal("fcntl");
135 event_dispatch();
138 static ssize_t
139 smtp_getline(char ** restrict buf, size_t * restrict size)
141 static char *rbuf = NULL;
142 static size_t rsoff = 0, reoff = 0;
143 static size_t rbsize = 0;
144 char *sep;
145 size_t sepoff;
146 ssize_t strlen, nread;
148 do {
149 if (rsoff != reoff) {
150 if ((sep = memchr(rbuf + rsoff, '\n', reoff - rsoff))
151 != NULL) {
152 sepoff = sep - rbuf;
153 if (*buf == NULL)
154 *size = 0;
155 if (*size < (sepoff - rsoff + 1)) {
156 *size = sepoff - rsoff + 1;
157 *buf = realloc(*buf, sepoff - rsoff + 1);
158 if (*buf == NULL)
159 fatal(NULL);
161 sep[0] = '\0';
162 strlen = strlcpy(*buf, rbuf + rsoff, *size);
163 if (strlen >= *size)
164 fatalx("copy buffer too small");
165 rsoff = sepoff + 1;
166 return strlen;
169 /* If we can't fill at the end, move everything back. */
170 if (rbsize - reoff < 1500 && rsoff != 0) {
171 memmove(rbuf, rbuf + rsoff, reoff - rsoff);
172 reoff -= rsoff;
173 rsoff = 0;
175 /* If we still can't fill alloc some new memory. */
176 if (rbsize - reoff < 1500) {
177 if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
178 fatal(NULL);
179 rbsize += 4096;
181 nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff);
182 if (nread <= 0)
183 return nread;
184 reoff += nread;
185 } while (1);
188 static void
189 smtp_newline(int fd, short event, void *arg)
191 struct event *stdinev = (struct event *)arg;
192 static char *line = NULL, *linedup = NULL;
193 static size_t linesize = 0;
194 static size_t dupsize = 0;
195 ssize_t linelen;
196 char *start, *end, *type, *direction, *phase, *params;
197 int version;
198 struct timespec tm;
199 uint64_t reqid, token;
200 int i;
202 while ((linelen = smtp_getline(&line, &linesize)) > 0) {
203 if (dupsize < linesize) {
204 if ((linedup = realloc(linedup, linesize)) == NULL)
205 fatal(NULL);
206 dupsize = linesize;
208 strlcpy(linedup, line, dupsize);
209 type = line;
210 if ((start = strchr(type, '|')) == NULL)
211 fatalx("Invalid line received: missing version: %s", linedup);
212 start++[0] = '\0';
213 if ((end = strchr(start, '|')) == NULL)
214 fatalx("Invalid line received: missing time: %s", linedup);
215 end++[0] = '\0';
216 if (strcmp(start, "1") != 0)
217 fatalx("Unsupported protocol received: %s: %s", start, linedup);
218 version = 1;
219 start = end;
220 if ((direction = strchr(start, '|')) == NULL)
221 fatalx("Invalid line received: missing direction: %s", linedup);
222 direction++[0] = '\0';
223 tm.tv_sec = (time_t) strtoull(start, &end, 10);
224 tm.tv_nsec = 0;
225 if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
226 fatalx("Invalid line received: invalid timestamp: %s", linedup);
227 if (end[0] == '.') {
228 start = end + 1;
229 tm.tv_nsec = strtol(start, &end, 10);
230 if (start[0] == '\0' || end[0] != '\0')
231 fatalx("Invalid line received: invalid "
232 "timestamp: %s", linedup);
233 for (i = 9 - (end - start); i > 0; i--)
234 tm.tv_nsec *= 10;
236 if ((phase = strchr(direction, '|')) == NULL)
237 fatalx("Invalid line receieved: missing phase: %s", linedup);
238 phase++[0] = '\0';
239 if ((start = strchr(phase, '|')) == NULL)
240 fatalx("Invalid line received: missing reqid: %s", linedup);
241 start++[0] = '\0';
242 reqid = strtoull(start, &params, 16);
243 if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
244 fatalx("Invalid line received: invalid reqid: %s", linedup);
245 params++;
247 for (i = 0; i < NITEMS(smtp_callbacks); i++) {
248 if (strcmp(type, smtp_callbacks[i].type) == 0 &&
249 strcmp(phase, smtp_callbacks[i].phase) == 0 &&
250 strcmp(direction, smtp_callbacks[i].direction) == 0)
251 break;
253 if (i == NITEMS(smtp_callbacks)) {
254 fatalx("Invalid line received: received unregistered "
255 "%s: %s: %s", type, phase, linedup);
257 if (strcmp(type, "filter") == 0) {
258 start = params;
259 token = strtoull(start, &params, 16);
260 if (start[0] == '|' || params[0] != '|')
261 fatalx("Invalid line received: invalid token: %s", linedup);
262 params++;
263 smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
264 version, &tm, reqid, token, params);
265 } else
266 smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
267 version, &tm, reqid, params);
269 if (linelen == 0 || errno != EAGAIN)
270 event_del(stdinev);
273 static void
274 smtp_connect(struct smtp_callback *cb, int version, struct timespec *tm,
275 uint64_t reqid, uint64_t token, char *params)
277 struct inx_addr addrx;
278 char *hostname;
279 char *address;
280 int ret;
281 void (*f)(char *, int, struct timespec *,char *, char *, uint64_t,
282 uint64_t, char *, struct inx_addr *);
284 hostname = params;
285 if ((address = strchr(params, '|')) == NULL)
286 fatalx("Invalid line received: missing address: %s", params);
287 address++[0] = '\0';
289 addrx.af = AF_INET;
290 if (strncasecmp(address, "ipv6:", 5) == 0) {
291 addrx.af = AF_INET6;
292 address += 5;
295 ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
296 (void *)&(addrx.addr) : (void *)&(addrx.addr6));
297 if (ret == 0)
298 fatalx("Invalid line received: Couldn't parse address: %s", params);
299 if (ret == -1)
300 fatal("Couldn't convert address: %s", params);
302 f = cb->cb;
303 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
304 hostname, &addrx);
307 static void
308 smtp_noargs(struct smtp_callback *cb, int version, struct timespec *tm,
309 uint64_t reqid, uint64_t token, char *params)
311 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
312 uint64_t);
314 f = cb->cb;
315 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token);
318 static void
319 smtp_dataline(struct smtp_callback *cb, int version, struct timespec *tm,
320 uint64_t reqid, uint64_t token, char *line)
322 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
323 uint64_t, char *);
325 f = cb->cb;
326 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
327 line);
330 static void
331 smtp_in_link_disconnect(struct smtp_callback *cb, int version,
332 struct timespec *tm, uint64_t reqid, char *params)
334 void (*f)(char *, int, struct timespec *, char *, char *, uint64_t);
336 f = cb->cb;
337 f(cb->type, version, tm, cb->direction, cb->phase, reqid);
340 void
341 smtp_filter_proceed(uint64_t reqid, uint64_t token)
343 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
344 reqid);
347 static void
348 smtp_printf(const char *fmt, ...)
350 va_list ap;
352 va_start(ap, fmt);
353 smtp_vprintf(fmt, ap);
354 va_end(ap);
357 static void
358 smtp_vprintf(const char *fmt, va_list ap)
360 va_list cap;
361 static struct smtp_writebuf buf = {NULL, 0, 0};
362 int fmtlen;
364 va_copy(cap, ap);
365 fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
366 ap);
367 if (fmtlen == -1)
368 fatal("vsnprintf");
369 if (fmtlen >= buf.bufsize - buf.buflen) {
370 buf.bufsize = buf.buflen + fmtlen + 1;
371 buf.buf = reallocarray(buf.buf, buf.bufsize,
372 sizeof(*(buf.buf)));
373 if (buf.buf == NULL)
374 fatal(NULL);
375 fmtlen = vsnprintf(buf.buf + buf.buflen,
376 buf.bufsize - buf.buflen, fmt, cap);
377 if (fmtlen == -1)
378 fatal("vsnprintf");
380 va_end(cap);
381 buf.buflen += fmtlen;
383 if (strchr(buf.buf, '\n') != NULL)
384 smtp_write(STDOUT_FILENO, EV_WRITE, &buf);
387 static void
388 smtp_write(int fd, short event, void *arg)
390 struct smtp_writebuf *buf = arg;
391 static struct event stdoutev;
392 static int evset = 0;
393 ssize_t wlen;
395 if (buf->buflen == 0)
396 return;
397 if (!evset) {
398 event_set(&stdoutev, fd, EV_WRITE, smtp_write, buf);
399 evset = 1;
401 wlen = write(fd, buf->buf, buf->buflen);
402 if (wlen == -1) {
403 if (errno != EAGAIN && errno != EINTR)
404 fatal("Failed to write to smtpd");
405 event_add(&stdoutev, NULL);
406 return;
408 if (wlen < buf->buflen) {
409 memmove(buf->buf, buf->buf + wlen, buf->buflen - wlen);
410 event_add(&stdoutev, NULL);
412 buf->buflen -= wlen;
413 if (buf->buflen == 0 && event_pending(&stdoutev, EV_WRITE, NULL))
414 event_del(&stdoutev);
417 void
418 smtp_filter_reject(uint64_t reqid, uint64_t token, int code,
419 const char *reason, ...)
421 va_list ap;
423 if (code < 200 || code > 599)
424 fatalx("Invalid reject code");
426 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
427 reqid, code);
428 va_start(ap, reason);
429 smtp_vprintf(reason, ap);
430 va_end(ap);
431 smtp_printf("\n");
434 void
435 smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
437 va_list ap;
439 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|disconnect|421 ",
440 token, reqid);
441 va_start(ap, reason);
442 smtp_vprintf(reason, ap);
443 va_end(ap);
444 smtp_printf("\n");
447 void
448 smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
450 va_list ap;
452 smtp_printf("filter-dataline|%016"PRIx64"|%016"PRIx64"|", token, reqid);
453 va_start(ap, line);
454 smtp_vprintf(line, ap);
455 va_end(ap);
456 smtp_printf("\n");
459 static int
460 smtp_register(char *type, char *phase, char *direction, void *cb)
462 int i;
463 static int evinit = 0;
465 if (ready)
466 fatalx("Can't register when proc is running");
468 if (!evinit) {
469 event_init();
470 evinit = 1;
473 for (i = 0; i < NITEMS(smtp_callbacks); i++) {
474 if (strcmp(type, smtp_callbacks[i].type) == 0 &&
475 strcmp(phase, smtp_callbacks[i].phase) == 0 &&
476 strcmp(direction, smtp_callbacks[i].direction) == 0) {
477 if (smtp_callbacks[i].cb != NULL) {
478 errno = EALREADY;
479 return -1;
481 smtp_callbacks[i].cb = cb;
482 smtp_printf("register|%s|%s|%s\n", type, direction,
483 phase);
484 return 0;
487 errno = EINVAL;
488 return -1;