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_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 *,
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_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}
80 };
82 static int ready = 0;
84 int
85 smtp_register_filter_connect(void (*cb)(char *, int, struct timespec *, char *,
86 char *, uint64_t, uint64_t, char *, struct inx_addr *))
87 {
88 return smtp_register("filter", "connect", "smtp-in", (void *)cb);
89 }
91 int
92 smtp_register_filter_data(void (*cb)(char *, int, struct timespec *, char *,
93 char *, uint64_t, uint64_t))
94 {
95 return smtp_register("filter", "data", "smtp-in", (void *)cb);
96 }
98 int
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);
105 int
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);
112 void
113 smtp_run(int debug)
115 struct event stdinev;
117 smtp_printf("register|ready\n");
118 ready = 1;
120 log_init(debug, LOG_MAIL);
121 event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline,
122 &stdinev);
123 event_add(&stdinev, NULL);
125 if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
126 fatal("fcntl");
127 event_dispatch();
130 static ssize_t
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;
136 char *sep;
137 size_t sepoff;
138 ssize_t strlen, nread;
140 do {
141 if (rsoff != reoff) {
142 if ((sep = memchr(rbuf + rsoff, '\n', reoff - rsoff))
143 != NULL) {
144 sepoff = sep - rbuf;
145 if (*buf == NULL)
146 *size = 0;
147 if (*size < (sepoff - rsoff + 1)) {
148 *size = sepoff - rsoff + 1;
149 *buf = realloc(*buf, sepoff - rsoff + 1);
150 if (*buf == NULL)
151 fatal(NULL);
153 sep[0] = '\0';
154 strlen = strlcpy(*buf, rbuf + rsoff, *size);
155 if (strlen >= *size)
156 fatalx("copy buffer too small");
157 rsoff = sepoff + 1;
158 return strlen;
161 if (reoff - rsoff < 1500) {
162 if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
163 fatal(NULL);
164 rbsize += 4096;
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);
170 if (nread <= 0)
171 return nread;
172 reoff += nread;
173 } while (1);
176 static void
177 smtp_newline(int fd, short event, void *arg)
179 struct event *stdinev = (struct event *)arg;
180 char *line = NULL, *linedup = NULL;
181 size_t linesize = 0;
182 size_t dupsize = 0;
183 ssize_t linelen;
184 char *start, *end, *type, *direction, *phase, *params;
185 int version;
186 struct timespec tm;
187 uint64_t reqid, token;
188 int i;
190 while ((linelen = smtp_getline(&line, &linesize)) > 0) {
191 if (dupsize < linesize) {
192 if ((linedup = realloc(linedup, linesize)) == NULL)
193 fatal(NULL);
194 dupsize = linesize;
196 strlcpy(linedup, line, dupsize);
197 type = line;
198 if ((start = strchr(type, '|')) == NULL)
199 fatalx("Invalid line received: missing version: %s", linedup);
200 start++[0] = '\0';
201 if ((end = strchr(start, '|')) == NULL)
202 fatalx("Invalid line received: missing time: %s", linedup);
203 end++[0] = '\0';
204 if (strcmp(start, "1") != 0)
205 fatalx("Unsupported protocol received: %s: %s", start, linedup);
206 version = 1;
207 start = end;
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);
212 tm.tv_nsec = 0;
213 if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
214 fatalx("Invalid line received: invalid timestamp: %s", linedup);
215 if (end[0] == '.') {
216 start = end + 1;
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--)
222 tm.tv_nsec *= 10;
224 if ((phase = strchr(direction, '|')) == NULL)
225 fatalx("Invalid line receieved: missing phase: %s", linedup);
226 phase++[0] = '\0';
227 if ((start = strchr(phase, '|')) == NULL)
228 fatalx("Invalid line received: missing reqid: %s", linedup);
229 start++[0] = '\0';
230 reqid = strtoull(start, &params, 16);
231 if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
232 fatalx("Invalid line received: invalid reqid: %s", linedup);
233 params++;
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)
239 break;
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) {
246 start = params;
247 token = strtoull(start, &params, 16);
248 if (start[0] == '|' || params[0] != '|')
249 fatalx("Invalid line received: invalid token: %s", linedup);
250 params++;
251 smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
252 version, &tm, reqid, token, params);
253 } else
254 smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
255 version, &tm, reqid, params);
257 if (linelen == 0 || errno != EAGAIN)
258 event_del(stdinev);
261 static void
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;
266 char *hostname;
267 char *address;
268 int ret;
269 void (*f)(char *, int, struct timespec *,char *, char *, uint64_t,
270 uint64_t, char *, struct inx_addr *);
272 hostname = params;
273 if ((address = strchr(params, '|')) == NULL)
274 fatalx("Invalid line received: missing address: %s", params);
275 address++[0] = '\0';
277 addrx.af = AF_INET;
278 if (strncasecmp(address, "ipv6:", 5) == 0) {
279 addrx.af = AF_INET6;
280 address += 5;
283 ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
284 (void *)&(addrx.addr) : (void *)&(addrx.addr6));
285 if (ret == 0)
286 fatalx("Invalid line received: Couldn't parse address: %s", params);
287 if (ret == -1)
288 fatal("Couldn't convert address: %s", params);
290 f = cb->cb;
291 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
292 hostname, &addrx);
295 static void
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,
300 uint64_t);
302 f = cb->cb;
303 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token);
306 static void
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,
311 uint64_t, char *);
313 f = cb->cb;
314 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
315 line);
318 static void
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);
324 f = cb->cb;
325 f(cb->type, version, tm, cb->direction, cb->phase, reqid);
328 void
329 smtp_filter_proceed(uint64_t reqid, uint64_t token)
331 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
332 reqid);
335 static void
336 smtp_printf(const char *fmt, ...)
338 va_list ap;
340 va_start(ap, fmt);
341 smtp_vprintf(fmt, ap);
342 va_end(ap);
345 static void
346 smtp_vprintf(const char *fmt, va_list ap)
348 va_list cap;
349 static struct smtp_writebuf buf = {NULL, 0, 0};
350 int fmtlen;
352 va_copy(cap, ap);
353 fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
354 ap);
355 if (fmtlen == -1)
356 fatal("vsnprintf");
357 if (fmtlen >= buf.bufsize - buf.buflen) {
358 buf.bufsize = buf.buflen + fmtlen + 1;
359 buf.buf = reallocarray(buf.buf, buf.bufsize,
360 sizeof(*(buf.buf)));
361 if (buf.buf == NULL)
362 fatalx(NULL);
363 fmtlen = vsnprintf(buf.buf + buf.buflen,
364 buf.bufsize - buf.buflen, fmt, cap);
365 if (fmtlen == -1)
366 fatal("vsnprintf");
368 buf.buflen += fmtlen;
370 if (strchr(buf.buf, '\n') != NULL)
371 smtp_write(STDOUT_FILENO, EV_WRITE, &buf);
374 static void
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;
380 ssize_t wlen;
382 if (buf->buflen == 0)
383 return;
384 if (event_pending(&stdoutev, EV_WRITE, NULL))
385 return;
386 if (!evset) {
387 event_set(&stdoutev, fd, EV_WRITE, smtp_write, buf);
388 evset = 1;
390 wlen = write(fd, buf->buf, buf->buflen);
391 if (wlen == -1) {
392 if (errno != EAGAIN && errno != EINTR)
393 fatal("Failed to write to smtpd");
394 event_add(&stdoutev, NULL);
395 return;
397 if (wlen < buf->buflen) {
398 memmove(buf->buf, buf->buf + wlen, buf->buflen - wlen);
399 event_add(&stdoutev, NULL);
401 buf->buflen -= wlen;
404 void
405 smtp_filter_reject(uint64_t reqid, uint64_t token, int code,
406 const char *reason, ...)
408 va_list ap;
410 if (code < 200 || code > 599)
411 fatalx("Invalid reject code");
413 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
414 reqid, code);
415 va_start(ap, reason);
416 smtp_vprintf(reason, ap);
417 va_end(ap);
418 smtp_printf("\n");
421 void
422 smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
424 va_list ap;
426 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|disconnect|421 ",
427 token, reqid);
428 va_start(ap, reason);
429 smtp_vprintf(reason, ap);
430 va_end(ap);
431 smtp_printf("\n");
434 void
435 smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
437 va_list ap;
439 smtp_printf("filter-dataline|%016"PRIx64"|%016"PRIx64"|", token, reqid);
440 va_start(ap, line);
441 smtp_vprintf(line, ap);
442 va_end(ap);
443 smtp_printf("\n");
446 static int
447 smtp_register(char *type, char *phase, char *direction, void *cb)
449 int i;
450 static int evinit = 0;
452 if (ready)
453 fatalx("Can't register when proc is running");
455 if (!evinit) {
456 event_init();
457 evinit = 1;
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) {
465 errno = EALREADY;
466 return -1;
468 smtp_callbacks[i].cb = cb;
469 smtp_printf("register|%s|%s|%s\n", type, direction,
470 phase);
471 return 0;
474 errno = EINVAL;
475 return -1;