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 <err.h>
21 #include <errno.h>
22 #include <event.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <unistd.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 event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline,
129 &stdinev);
130 event_add(&stdinev, NULL);
132 if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
133 err(1, "fcntl");
134 event_dispatch();
137 static ssize_t
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;
143 char *sep;
144 size_t sepoff;
145 ssize_t strlen, nread;
147 do {
148 if (rsoff != reoff) {
149 if ((sep = memchr(rbuf + rsoff, '\n', reoff - rsoff))
150 != NULL) {
151 sepoff = sep - rbuf;
152 if (*buf == NULL)
153 *size = 0;
154 if (*size < (sepoff - rsoff + 1)) {
155 *size = sepoff - rsoff + 1;
156 *buf = realloc(*buf, sepoff - rsoff + 1);
157 if (*buf == NULL)
158 err(1, NULL);
160 sep[0] = '\0';
161 strlen = strlcpy(*buf, rbuf + rsoff, *size);
162 if (strlen >= *size)
163 errx(1, "copy buffer too small");
164 rsoff = sepoff + 1;
165 return strlen;
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);
171 reoff -= rsoff;
172 rsoff = 0;
174 /* If we still can't fill alloc some new memory. */
175 if (rbsize - reoff < 1500) {
176 if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
177 err(1, NULL);
178 rbsize += 4096;
180 nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff);
181 if (nread <= 0)
182 return nread;
183 reoff += nread;
184 } while (1);
187 static void
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;
194 ssize_t linelen;
195 char *start, *end, *type, *direction, *phase, *params;
196 int version;
197 struct timespec tm;
198 uint64_t reqid, token;
199 int i;
201 while ((linelen = smtp_getline(&line, &linesize)) > 0) {
202 if (dupsize < linesize) {
203 if ((linedup = realloc(linedup, linesize)) == NULL)
204 err(1, NULL);
205 dupsize = linesize;
207 strlcpy(linedup, line, dupsize);
208 type = line;
209 if ((start = strchr(type, '|')) == NULL)
210 errx(1, "Invalid line received: missing version: %s", linedup);
211 start++[0] = '\0';
212 if ((end = strchr(start, '|')) == NULL)
213 errx(1, "Invalid line received: missing time: %s", linedup);
214 end++[0] = '\0';
215 if (strcmp(start, "1") != 0)
216 errx(1, "Unsupported protocol received: %s: %s", start, linedup);
217 version = 1;
218 start = end;
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);
223 tm.tv_nsec = 0;
224 if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
225 errx(1, "Invalid line received: invalid timestamp: %s", linedup);
226 if (end[0] == '.') {
227 start = end + 1;
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--)
233 tm.tv_nsec *= 10;
235 if ((phase = strchr(direction, '|')) == NULL)
236 errx(1, "Invalid line receieved: missing phase: %s", linedup);
237 phase++[0] = '\0';
238 if ((start = strchr(phase, '|')) == NULL)
239 errx(1, "Invalid line received: missing reqid: %s", linedup);
240 start++[0] = '\0';
241 reqid = strtoull(start, &params, 16);
242 if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
243 errx(1, "Invalid line received: invalid reqid: %s", linedup);
244 params++;
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)
250 break;
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) {
257 start = params;
258 token = strtoull(start, &params, 16);
259 if (start[0] == '|' || params[0] != '|')
260 errx(1, "Invalid line received: invalid token: %s", linedup);
261 params++;
262 smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
263 version, &tm, reqid, token, params);
264 } else
265 smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
266 version, &tm, reqid, params);
268 if (linelen == 0 || errno != EAGAIN)
269 event_del(stdinev);
272 static void
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;
277 char *hostname;
278 char *address;
279 int ret;
280 void (*f)(char *, int, struct timespec *,char *, char *, uint64_t,
281 uint64_t, char *, struct inx_addr *);
283 hostname = params;
284 if ((address = strchr(params, '|')) == NULL)
285 errx(1, "Invalid line received: missing address: %s", params);
286 address++[0] = '\0';
288 addrx.af = AF_INET;
289 if (strncasecmp(address, "ipv6:", 5) == 0) {
290 addrx.af = AF_INET6;
291 address += 5;
294 ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
295 (void *)&(addrx.addr) : (void *)&(addrx.addr6));
296 if (ret == 0)
297 errx(1, "Invalid line received: Couldn't parse address: %s", params);
298 if (ret == -1)
299 err(1, "Couldn't convert address: %s", params);
301 f = cb->cb;
302 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
303 hostname, &addrx);
306 static void
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,
311 uint64_t);
313 f = cb->cb;
314 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token);
317 static void
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,
322 uint64_t, char *);
324 f = cb->cb;
325 f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
326 line);
329 static void
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);
335 f = cb->cb;
336 f(cb->type, version, tm, cb->direction, cb->phase, reqid);
339 void
340 smtp_filter_proceed(uint64_t reqid, uint64_t token)
342 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
343 reqid);
346 static void
347 smtp_printf(const char *fmt, ...)
349 va_list ap;
351 va_start(ap, fmt);
352 smtp_vprintf(fmt, ap);
353 va_end(ap);
356 static void
357 smtp_vprintf(const char *fmt, va_list ap)
359 va_list cap;
360 static struct smtp_writebuf buf = {NULL, 0, 0};
361 int fmtlen;
363 va_copy(cap, ap);
364 fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
365 ap);
366 if (fmtlen == -1)
367 err(1, "vsnprintf");
368 if (fmtlen >= buf.bufsize - buf.buflen) {
369 buf.bufsize = buf.buflen + fmtlen + 1;
370 buf.buf = reallocarray(buf.buf, buf.bufsize,
371 sizeof(*(buf.buf)));
372 if (buf.buf == NULL)
373 err(1, NULL);
374 fmtlen = vsnprintf(buf.buf + buf.buflen,
375 buf.bufsize - buf.buflen, fmt, cap);
376 if (fmtlen == -1)
377 err(1, "vsnprintf");
379 va_end(cap);
380 buf.buflen += fmtlen;
382 if (strchr(buf.buf, '\n') != NULL)
383 smtp_write(STDOUT_FILENO, EV_WRITE, &buf);
386 static void
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;
392 ssize_t wlen;
394 if (buf->buflen == 0)
395 return;
396 if (!evset) {
397 event_set(&stdoutev, fd, EV_WRITE, smtp_write, buf);
398 evset = 1;
400 wlen = write(fd, buf->buf, buf->buflen);
401 if (wlen == -1) {
402 if (errno != EAGAIN && errno != EINTR)
403 err(1, "Failed to write to smtpd");
404 event_add(&stdoutev, NULL);
405 return;
407 if (wlen < buf->buflen) {
408 memmove(buf->buf, buf->buf + wlen, buf->buflen - wlen);
409 event_add(&stdoutev, NULL);
411 buf->buflen -= wlen;
412 if (buf->buflen == 0 && event_pending(&stdoutev, EV_WRITE, NULL))
413 event_del(&stdoutev);
416 void
417 smtp_filter_reject(uint64_t reqid, uint64_t token, int code,
418 const char *reason, ...)
420 va_list ap;
422 if (code < 200 || code > 599)
423 errx(1, "Invalid reject code");
425 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
426 reqid, code);
427 va_start(ap, reason);
428 smtp_vprintf(reason, ap);
429 va_end(ap);
430 smtp_printf("\n");
433 void
434 smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
436 va_list ap;
438 smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|disconnect|421 ",
439 token, reqid);
440 va_start(ap, reason);
441 smtp_vprintf(reason, ap);
442 va_end(ap);
443 smtp_printf("\n");
446 void
447 smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
449 va_list ap;
451 smtp_printf("filter-dataline|%016"PRIx64"|%016"PRIx64"|", token, reqid);
452 va_start(ap, line);
453 smtp_vprintf(line, ap);
454 va_end(ap);
455 smtp_printf("\n");
458 static int
459 smtp_register(char *type, char *phase, char *direction, void *cb)
461 int i;
462 static int evinit = 0;
464 if (ready)
465 errx(1, "Can't register when proc is running");
467 if (!evinit) {
468 event_init();
469 evinit = 1;
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) {
477 errno = EALREADY;
478 return -1;
480 smtp_callbacks[i].cb = cb;
481 smtp_printf("register|%s|%s|%s\n", type, direction,
482 phase);
483 return 0;
486 errno = EINVAL;
487 return -1;