2 15cacd92 2019-03-29 martijn * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
4 15cacd92 2019-03-29 martijn * Permission to use, copy, modify, and distribute this software for any
5 15cacd92 2019-03-29 martijn * purpose with or without fee is hereby granted, provided that the above
6 15cacd92 2019-03-29 martijn * copyright notice and this permission notice appear in all copies.
8 15cacd92 2019-03-29 martijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 15cacd92 2019-03-29 martijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 15cacd92 2019-03-29 martijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 15cacd92 2019-03-29 martijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 15cacd92 2019-03-29 martijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 15cacd92 2019-03-29 martijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 15cacd92 2019-03-29 martijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 a248a3c1 2019-03-27 martijn #include <sys/time.h>
17 a248a3c1 2019-03-27 martijn #include <sys/socket.h>
19 a248a3c1 2019-03-27 martijn #include <arpa/inet.h>
20 a248a3c1 2019-03-27 martijn #include <errno.h>
21 eaefb262 2019-03-27 martijn #include <event.h>
22 eaefb262 2019-03-27 martijn #include <fcntl.h>
23 a248a3c1 2019-03-27 martijn #include <inttypes.h>
24 57cbfccc 2019-03-27 martijn #include <stdarg.h>
25 a248a3c1 2019-03-27 martijn #include <stdio.h>
26 a248a3c1 2019-03-27 martijn #include <stdlib.h>
27 a248a3c1 2019-03-27 martijn #include <string.h>
28 b633fa4c 2019-03-29 martijn #include <syslog.h>
29 eaefb262 2019-03-27 martijn #include <unistd.h>
31 b633fa4c 2019-03-29 martijn #include "log.h"
32 a248a3c1 2019-03-27 martijn #include "smtp_proc.h"
34 a248a3c1 2019-03-27 martijn #define NITEMS(x) (sizeof(x) / sizeof(*x))
36 a248a3c1 2019-03-27 martijn struct smtp_callback;
37 73ac3ec1 2019-03-27 martijn struct smtp_request;
39 c6714f66 2019-04-01 martijn extern struct event_base *current_base;
41 03a6f52a 2019-03-29 martijn static int smtp_register(char *, char *, char *, void *);
42 6bf2fad2 2019-03-29 martijn static ssize_t smtp_getline(char ** restrict, size_t * restrict);
43 eaefb262 2019-03-27 martijn static void smtp_newline(int, short, void *);
44 42a4c512 2019-03-28 martijn static void smtp_connect(struct smtp_callback *, int, struct timespec *,
45 42a4c512 2019-03-28 martijn uint64_t, uint64_t, char *);
46 0696aea6 2019-04-06 martijn static void smtp_noargs(struct smtp_callback *, int, struct timespec *,
47 a28aed52 2019-03-31 martijn uint64_t, uint64_t, char *);
48 02090eee 2019-03-29 martijn static void smtp_dataline(struct smtp_callback *, int, struct timespec *,
49 02090eee 2019-03-29 martijn uint64_t, uint64_t, char *);
50 47628d21 2019-03-29 martijn static void smtp_in_link_disconnect(struct smtp_callback *, int, struct timespec *,
51 47628d21 2019-03-29 martijn uint64_t, char *);
52 a211223d 2019-04-01 martijn static void smtp_printf(const char *, ...)
53 a211223d 2019-04-01 martijn __attribute__((__format__ (printf, 1, 2)));
54 a211223d 2019-04-01 martijn static void smtp_vprintf(const char *, va_list);
55 a211223d 2019-04-01 martijn static void smtp_write(int, short, void *);
57 a211223d 2019-04-01 martijn struct smtp_writebuf {
58 a211223d 2019-04-01 martijn char *buf;
59 a211223d 2019-04-01 martijn size_t bufsize;
60 a211223d 2019-04-01 martijn size_t buflen;
63 a248a3c1 2019-03-27 martijn struct smtp_callback {
64 a248a3c1 2019-03-27 martijn char *type;
65 a248a3c1 2019-03-27 martijn char *phase;
66 a248a3c1 2019-03-27 martijn char *direction;
68 47628d21 2019-03-29 martijn void (*smtp_filter)(struct smtp_callback *, int,
69 47628d21 2019-03-29 martijn struct timespec *, uint64_t, uint64_t, char *);
70 47628d21 2019-03-29 martijn void (*smtp_report)(struct smtp_callback *, int,
71 47628d21 2019-03-29 martijn struct timespec *, uint64_t, char *);
73 03a6f52a 2019-03-29 martijn void *cb;
74 a248a3c1 2019-03-27 martijn } smtp_callbacks[] = {
75 47628d21 2019-03-29 martijn {"filter", "connect", "smtp-in", .smtp_filter = smtp_connect, NULL},
76 0696aea6 2019-04-06 martijn {"filter", "data", "smtp-in", .smtp_filter = smtp_noargs, NULL},
77 02090eee 2019-03-29 martijn {"filter", "data-line", "smtp-in", .smtp_filter = smtp_dataline, NULL},
78 0696aea6 2019-04-06 martijn {"filter", "commit", "smtp-in", .smtp_filter = smtp_noargs, NULL},
79 47628d21 2019-03-29 martijn {"report", "link-disconnect", "smtp-in",
80 47628d21 2019-03-29 martijn .smtp_report = smtp_in_link_disconnect, NULL}
83 a248a3c1 2019-03-27 martijn static int ready = 0;
86 42a4c512 2019-03-28 martijn smtp_register_filter_connect(void (*cb)(char *, int, struct timespec *, char *,
87 03a6f52a 2019-03-29 martijn char *, uint64_t, uint64_t, char *, struct inx_addr *))
89 03a6f52a 2019-03-29 martijn return smtp_register("filter", "connect", "smtp-in", (void *)cb);
93 a28aed52 2019-03-31 martijn smtp_register_filter_data(void (*cb)(char *, int, struct timespec *, char *,
94 a28aed52 2019-03-31 martijn char *, uint64_t, uint64_t))
96 a28aed52 2019-03-31 martijn return smtp_register("filter", "data", "smtp-in", (void *)cb);
100 02090eee 2019-03-29 martijn smtp_register_filter_dataline(void (*cb)(char *, int, struct timespec *, char *,
101 02090eee 2019-03-29 martijn char *, uint64_t, uint64_t, char *))
103 02090eee 2019-03-29 martijn return smtp_register("filter", "data-line", "smtp-in", (void *)cb);
107 0696aea6 2019-04-06 martijn smtp_register_filter_commit(void (*cb)(char *, int, struct timespec *, char *,
108 0696aea6 2019-04-06 martijn char *, uint64_t, uint64_t))
110 0696aea6 2019-04-06 martijn return smtp_register("filter", "commit", "smtp-in", (void *)cb);
114 47628d21 2019-03-29 martijn smtp_in_register_report_disconnect(void (*cb)(char *, int, struct timespec *,
115 47628d21 2019-03-29 martijn char *, char *, uint64_t))
117 47628d21 2019-03-29 martijn return smtp_register("report", "link-disconnect", "smtp-in", (void *)cb);
121 b633fa4c 2019-03-29 martijn smtp_run(int debug)
123 eaefb262 2019-03-27 martijn struct event stdinev;
125 a211223d 2019-04-01 martijn smtp_printf("register|ready\n");
126 eaefb262 2019-03-27 martijn ready = 1;
128 b633fa4c 2019-03-29 martijn log_init(debug, LOG_MAIL);
129 eaefb262 2019-03-27 martijn event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline,
130 eaefb262 2019-03-27 martijn &stdinev);
131 eaefb262 2019-03-27 martijn event_add(&stdinev, NULL);
133 eaefb262 2019-03-27 martijn if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
134 b633fa4c 2019-03-29 martijn fatal("fcntl");
135 eaefb262 2019-03-27 martijn event_dispatch();
138 6bf2fad2 2019-03-29 martijn static ssize_t
139 6bf2fad2 2019-03-29 martijn smtp_getline(char ** restrict buf, size_t * restrict size)
141 6bf2fad2 2019-03-29 martijn static char *rbuf = NULL;
142 6bf2fad2 2019-03-29 martijn static size_t rsoff = 0, reoff = 0;
143 6bf2fad2 2019-03-29 martijn static size_t rbsize = 0;
144 6bf2fad2 2019-03-29 martijn char *sep;
145 6bf2fad2 2019-03-29 martijn size_t sepoff;
146 6bf2fad2 2019-03-29 martijn ssize_t strlen, nread;
149 6bf2fad2 2019-03-29 martijn if (rsoff != reoff) {
150 6bf2fad2 2019-03-29 martijn if ((sep = memchr(rbuf + rsoff, '\n', reoff - rsoff))
151 6bf2fad2 2019-03-29 martijn != NULL) {
152 6bf2fad2 2019-03-29 martijn sepoff = sep - rbuf;
153 6bf2fad2 2019-03-29 martijn if (*buf == NULL)
154 6bf2fad2 2019-03-29 martijn *size = 0;
155 6bf2fad2 2019-03-29 martijn if (*size < (sepoff - rsoff + 1)) {
156 6bf2fad2 2019-03-29 martijn *size = sepoff - rsoff + 1;
157 6bf2fad2 2019-03-29 martijn *buf = realloc(*buf, sepoff - rsoff + 1);
158 6bf2fad2 2019-03-29 martijn if (*buf == NULL)
159 6bf2fad2 2019-03-29 martijn fatal(NULL);
161 6bf2fad2 2019-03-29 martijn sep[0] = '\0';
162 6bf2fad2 2019-03-29 martijn strlen = strlcpy(*buf, rbuf + rsoff, *size);
163 6bf2fad2 2019-03-29 martijn if (strlen >= *size)
164 6bf2fad2 2019-03-29 martijn fatalx("copy buffer too small");
165 6bf2fad2 2019-03-29 martijn rsoff = sepoff + 1;
166 6bf2fad2 2019-03-29 martijn return strlen;
169 4d7575c0 2019-04-03 martijn /* If we can't fill at the end, move everything back. */
170 4d7575c0 2019-04-03 martijn if (rbsize - reoff < 1500 && rsoff != 0) {
171 4d7575c0 2019-04-03 martijn memmove(rbuf, rbuf + rsoff, reoff - rsoff);
172 4d7575c0 2019-04-03 martijn reoff -= rsoff;
173 4d7575c0 2019-04-03 martijn rsoff = 0;
175 4d7575c0 2019-04-03 martijn /* If we still can't fill alloc some new memory. */
176 4d7575c0 2019-04-03 martijn if (rbsize - reoff < 1500) {
177 6bf2fad2 2019-03-29 martijn if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
178 6bf2fad2 2019-03-29 martijn fatal(NULL);
179 6bf2fad2 2019-03-29 martijn rbsize += 4096;
181 6bf2fad2 2019-03-29 martijn nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff);
182 6bf2fad2 2019-03-29 martijn if (nread <= 0)
183 6bf2fad2 2019-03-29 martijn return nread;
184 6bf2fad2 2019-03-29 martijn reoff += nread;
185 6bf2fad2 2019-03-29 martijn } while (1);
188 eaefb262 2019-03-27 martijn static void
189 eaefb262 2019-03-27 martijn smtp_newline(int fd, short event, void *arg)
191 eaefb262 2019-03-27 martijn struct event *stdinev = (struct event *)arg;
192 d5a6ce31 2019-04-03 martijn static char *line = NULL, *linedup = NULL;
193 d5a6ce31 2019-04-03 martijn static size_t linesize = 0;
194 d5a6ce31 2019-04-03 martijn static size_t dupsize = 0;
195 a248a3c1 2019-03-27 martijn ssize_t linelen;
196 a248a3c1 2019-03-27 martijn char *start, *end, *type, *direction, *phase, *params;
197 a248a3c1 2019-03-27 martijn int version;
198 42a4c512 2019-03-28 martijn struct timespec tm;
199 a248a3c1 2019-03-27 martijn uint64_t reqid, token;
202 6bf2fad2 2019-03-29 martijn while ((linelen = smtp_getline(&line, &linesize)) > 0) {
203 ced7acc6 2019-03-29 martijn if (dupsize < linesize) {
204 ced7acc6 2019-03-29 martijn if ((linedup = realloc(linedup, linesize)) == NULL)
205 ced7acc6 2019-03-29 martijn fatal(NULL);
206 ced7acc6 2019-03-29 martijn dupsize = linesize;
208 ced7acc6 2019-03-29 martijn strlcpy(linedup, line, dupsize);
209 a248a3c1 2019-03-27 martijn type = line;
210 a248a3c1 2019-03-27 martijn if ((start = strchr(type, '|')) == NULL)
211 35699cf9 2019-03-29 martijn fatalx("Invalid line received: missing version: %s", linedup);
212 a248a3c1 2019-03-27 martijn start++[0] = '\0';
213 a248a3c1 2019-03-27 martijn if ((end = strchr(start, '|')) == NULL)
214 35699cf9 2019-03-29 martijn fatalx("Invalid line received: missing time: %s", linedup);
215 a248a3c1 2019-03-27 martijn end++[0] = '\0';
216 a248a3c1 2019-03-27 martijn if (strcmp(start, "1") != 0)
217 35699cf9 2019-03-29 martijn fatalx("Unsupported protocol received: %s: %s", start, linedup);
218 a248a3c1 2019-03-27 martijn version = 1;
219 a248a3c1 2019-03-27 martijn start = end;
220 a248a3c1 2019-03-27 martijn if ((direction = strchr(start, '|')) == NULL)
221 35699cf9 2019-03-29 martijn fatalx("Invalid line received: missing direction: %s", linedup);
222 a248a3c1 2019-03-27 martijn direction++[0] = '\0';
223 42a4c512 2019-03-28 martijn tm.tv_sec = (time_t) strtoull(start, &end, 10);
224 42a4c512 2019-03-28 martijn tm.tv_nsec = 0;
225 42a4c512 2019-03-28 martijn if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
226 35699cf9 2019-03-29 martijn fatalx("Invalid line received: invalid timestamp: %s", linedup);
227 42a4c512 2019-03-28 martijn if (end[0] == '.') {
228 42a4c512 2019-03-28 martijn start = end + 1;
229 42a4c512 2019-03-28 martijn tm.tv_nsec = strtol(start, &end, 10);
230 42a4c512 2019-03-28 martijn if (start[0] == '\0' || end[0] != '\0')
231 b633fa4c 2019-03-29 martijn fatalx("Invalid line received: invalid "
232 35699cf9 2019-03-29 martijn "timestamp: %s", linedup);
233 57d45cc8 2019-03-28 martijn for (i = 9 - (end - start); i > 0; i--)
234 42a4c512 2019-03-28 martijn tm.tv_nsec *= 10;
236 a248a3c1 2019-03-27 martijn if ((phase = strchr(direction, '|')) == NULL)
237 35699cf9 2019-03-29 martijn fatalx("Invalid line receieved: missing phase: %s", linedup);
238 a248a3c1 2019-03-27 martijn phase++[0] = '\0';
239 a248a3c1 2019-03-27 martijn if ((start = strchr(phase, '|')) == NULL)
240 35699cf9 2019-03-29 martijn fatalx("Invalid line received: missing reqid: %s", linedup);
241 a248a3c1 2019-03-27 martijn start++[0] = '\0';
242 47628d21 2019-03-29 martijn reqid = strtoull(start, ¶ms, 16);
243 47628d21 2019-03-29 martijn if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
244 35699cf9 2019-03-29 martijn fatalx("Invalid line received: invalid reqid: %s", linedup);
245 47628d21 2019-03-29 martijn params++;
247 a248a3c1 2019-03-27 martijn for (i = 0; i < NITEMS(smtp_callbacks); i++) {
248 a248a3c1 2019-03-27 martijn if (strcmp(type, smtp_callbacks[i].type) == 0 &&
249 a248a3c1 2019-03-27 martijn strcmp(phase, smtp_callbacks[i].phase) == 0 &&
250 a248a3c1 2019-03-27 martijn strcmp(direction, smtp_callbacks[i].direction) == 0)
253 a248a3c1 2019-03-27 martijn if (i == NITEMS(smtp_callbacks)) {
254 b633fa4c 2019-03-29 martijn fatalx("Invalid line received: received unregistered "
255 35699cf9 2019-03-29 martijn "%s: %s: %s", type, phase, linedup);
257 47628d21 2019-03-29 martijn if (strcmp(type, "filter") == 0) {
258 47628d21 2019-03-29 martijn start = params;
259 47628d21 2019-03-29 martijn token = strtoull(start, ¶ms, 16);
260 47628d21 2019-03-29 martijn if (start[0] == '|' || params[0] != '|')
261 35699cf9 2019-03-29 martijn fatalx("Invalid line received: invalid token: %s", linedup);
262 47628d21 2019-03-29 martijn params++;
263 47628d21 2019-03-29 martijn smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
264 47628d21 2019-03-29 martijn version, &tm, reqid, token, params);
266 47628d21 2019-03-29 martijn smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
267 47628d21 2019-03-29 martijn version, &tm, reqid, params);
269 6bf2fad2 2019-03-29 martijn if (linelen == 0 || errno != EAGAIN)
270 eaefb262 2019-03-27 martijn event_del(stdinev);
273 a248a3c1 2019-03-27 martijn static void
274 42a4c512 2019-03-28 martijn smtp_connect(struct smtp_callback *cb, int version, struct timespec *tm,
275 42a4c512 2019-03-28 martijn uint64_t reqid, uint64_t token, char *params)
277 03a6f52a 2019-03-29 martijn struct inx_addr addrx;
278 03a6f52a 2019-03-29 martijn char *hostname;
279 a248a3c1 2019-03-27 martijn char *address;
280 a248a3c1 2019-03-27 martijn int ret;
281 03a6f52a 2019-03-29 martijn void (*f)(char *, int, struct timespec *,char *, char *, uint64_t,
282 03a6f52a 2019-03-29 martijn uint64_t, char *, struct inx_addr *);
284 03a6f52a 2019-03-29 martijn hostname = params;
285 a248a3c1 2019-03-27 martijn if ((address = strchr(params, '|')) == NULL)
286 44775e42 2019-03-29 martijn fatalx("Invalid line received: missing address: %s", params);
287 a248a3c1 2019-03-27 martijn address++[0] = '\0';
289 03a6f52a 2019-03-29 martijn addrx.af = AF_INET;
290 a248a3c1 2019-03-27 martijn if (strncasecmp(address, "ipv6:", 5) == 0) {
291 03a6f52a 2019-03-29 martijn addrx.af = AF_INET6;
292 a248a3c1 2019-03-27 martijn address += 5;
295 03a6f52a 2019-03-29 martijn ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
296 03a6f52a 2019-03-29 martijn (void *)&(addrx.addr) : (void *)&(addrx.addr6));
297 a248a3c1 2019-03-27 martijn if (ret == 0)
298 44775e42 2019-03-29 martijn fatalx("Invalid line received: Couldn't parse address: %s", params);
299 a248a3c1 2019-03-27 martijn if (ret == -1)
300 44775e42 2019-03-29 martijn fatal("Couldn't convert address: %s", params);
302 03a6f52a 2019-03-29 martijn f = cb->cb;
303 03a6f52a 2019-03-29 martijn f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
304 03a6f52a 2019-03-29 martijn hostname, &addrx);
307 47628d21 2019-03-29 martijn static void
308 0696aea6 2019-04-06 martijn smtp_noargs(struct smtp_callback *cb, int version, struct timespec *tm,
309 a28aed52 2019-03-31 martijn uint64_t reqid, uint64_t token, char *params)
311 a28aed52 2019-03-31 martijn void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
312 a28aed52 2019-03-31 martijn uint64_t);
314 a28aed52 2019-03-31 martijn f = cb->cb;
315 a28aed52 2019-03-31 martijn f(cb->type, version, tm, cb->direction, cb->phase, reqid, token);
318 a28aed52 2019-03-31 martijn static void
319 02090eee 2019-03-29 martijn smtp_dataline(struct smtp_callback *cb, int version, struct timespec *tm,
320 02090eee 2019-03-29 martijn uint64_t reqid, uint64_t token, char *line)
322 a28aed52 2019-03-31 martijn void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
323 02090eee 2019-03-29 martijn uint64_t, char *);
325 02090eee 2019-03-29 martijn f = cb->cb;
326 02090eee 2019-03-29 martijn f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
330 02090eee 2019-03-29 martijn static void
331 47628d21 2019-03-29 martijn smtp_in_link_disconnect(struct smtp_callback *cb, int version,
332 47628d21 2019-03-29 martijn struct timespec *tm, uint64_t reqid, char *params)
334 47628d21 2019-03-29 martijn void (*f)(char *, int, struct timespec *, char *, char *, uint64_t);
336 47628d21 2019-03-29 martijn f = cb->cb;
337 47628d21 2019-03-29 martijn f(cb->type, version, tm, cb->direction, cb->phase, reqid);
341 57cbfccc 2019-03-27 martijn smtp_filter_proceed(uint64_t reqid, uint64_t token)
343 a211223d 2019-04-01 martijn smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
347 a211223d 2019-04-01 martijn static void
348 a211223d 2019-04-01 martijn smtp_printf(const char *fmt, ...)
350 a211223d 2019-04-01 martijn va_list ap;
352 a211223d 2019-04-01 martijn va_start(ap, fmt);
353 a211223d 2019-04-01 martijn smtp_vprintf(fmt, ap);
354 a211223d 2019-04-01 martijn va_end(ap);
357 a211223d 2019-04-01 martijn static void
358 a211223d 2019-04-01 martijn smtp_vprintf(const char *fmt, va_list ap)
360 a211223d 2019-04-01 martijn va_list cap;
361 a211223d 2019-04-01 martijn static struct smtp_writebuf buf = {NULL, 0, 0};
362 a211223d 2019-04-01 martijn int fmtlen;
364 a211223d 2019-04-01 martijn va_copy(cap, ap);
365 a211223d 2019-04-01 martijn fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
367 a211223d 2019-04-01 martijn if (fmtlen == -1)
368 a211223d 2019-04-01 martijn fatal("vsnprintf");
369 a211223d 2019-04-01 martijn if (fmtlen >= buf.bufsize - buf.buflen) {
370 a211223d 2019-04-01 martijn buf.bufsize = buf.buflen + fmtlen + 1;
371 a211223d 2019-04-01 martijn buf.buf = reallocarray(buf.buf, buf.bufsize,
372 a211223d 2019-04-01 martijn sizeof(*(buf.buf)));
373 a211223d 2019-04-01 martijn if (buf.buf == NULL)
374 ed005105 2019-04-15 martijn fatal(NULL);
375 a211223d 2019-04-01 martijn fmtlen = vsnprintf(buf.buf + buf.buflen,
376 a211223d 2019-04-01 martijn buf.bufsize - buf.buflen, fmt, cap);
377 a211223d 2019-04-01 martijn if (fmtlen == -1)
378 a211223d 2019-04-01 martijn fatal("vsnprintf");
380 56ffa48b 2019-04-03 martijn va_end(cap);
381 a211223d 2019-04-01 martijn buf.buflen += fmtlen;
383 a211223d 2019-04-01 martijn if (strchr(buf.buf, '\n') != NULL)
384 a211223d 2019-04-01 martijn smtp_write(STDOUT_FILENO, EV_WRITE, &buf);
387 a211223d 2019-04-01 martijn static void
388 a211223d 2019-04-01 martijn smtp_write(int fd, short event, void *arg)
390 a211223d 2019-04-01 martijn struct smtp_writebuf *buf = arg;
391 6fdaa2e1 2019-04-03 martijn static struct event stdoutev;
392 a211223d 2019-04-01 martijn static int evset = 0;
393 a211223d 2019-04-01 martijn ssize_t wlen;
395 a211223d 2019-04-01 martijn if (buf->buflen == 0)
397 a211223d 2019-04-01 martijn if (!evset) {
398 6fdaa2e1 2019-04-03 martijn event_set(&stdoutev, fd, EV_WRITE, smtp_write, buf);
399 a211223d 2019-04-01 martijn evset = 1;
401 a211223d 2019-04-01 martijn wlen = write(fd, buf->buf, buf->buflen);
402 a211223d 2019-04-01 martijn if (wlen == -1) {
403 585e2e89 2019-04-01 martijn if (errno != EAGAIN && errno != EINTR)
404 a211223d 2019-04-01 martijn fatal("Failed to write to smtpd");
405 6fdaa2e1 2019-04-03 martijn event_add(&stdoutev, NULL);
408 a211223d 2019-04-01 martijn if (wlen < buf->buflen) {
409 a211223d 2019-04-01 martijn memmove(buf->buf, buf->buf + wlen, buf->buflen - wlen);
410 6fdaa2e1 2019-04-03 martijn event_add(&stdoutev, NULL);
412 a211223d 2019-04-01 martijn buf->buflen -= wlen;
413 ed005105 2019-04-15 martijn if (buf->buflen == 0 && event_pending(&stdoutev, EV_WRITE, NULL))
414 ed005105 2019-04-15 martijn event_del(&stdoutev);
418 57cbfccc 2019-03-27 martijn smtp_filter_reject(uint64_t reqid, uint64_t token, int code,
419 57cbfccc 2019-03-27 martijn const char *reason, ...)
421 57cbfccc 2019-03-27 martijn va_list ap;
423 57cbfccc 2019-03-27 martijn if (code < 200 || code > 599)
424 b633fa4c 2019-03-29 martijn fatalx("Invalid reject code");
426 a211223d 2019-04-01 martijn smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
427 57cbfccc 2019-03-27 martijn reqid, code);
428 57cbfccc 2019-03-27 martijn va_start(ap, reason);
429 a211223d 2019-04-01 martijn smtp_vprintf(reason, ap);
430 57cbfccc 2019-03-27 martijn va_end(ap);
431 a211223d 2019-04-01 martijn smtp_printf("\n");
435 57cbfccc 2019-03-27 martijn smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
437 57cbfccc 2019-03-27 martijn va_list ap;
439 a211223d 2019-04-01 martijn smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|disconnect|421 ",
440 57cbfccc 2019-03-27 martijn token, reqid);
441 57cbfccc 2019-03-27 martijn va_start(ap, reason);
442 a211223d 2019-04-01 martijn smtp_vprintf(reason, ap);
443 57cbfccc 2019-03-27 martijn va_end(ap);
444 a211223d 2019-04-01 martijn smtp_printf("\n");
448 02090eee 2019-03-29 martijn smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
450 02090eee 2019-03-29 martijn va_list ap;
452 a211223d 2019-04-01 martijn smtp_printf("filter-dataline|%016"PRIx64"|%016"PRIx64"|", token, reqid);
453 02090eee 2019-03-29 martijn va_start(ap, line);
454 a211223d 2019-04-01 martijn smtp_vprintf(line, ap);
455 02090eee 2019-03-29 martijn va_end(ap);
456 a211223d 2019-04-01 martijn smtp_printf("\n");
459 a248a3c1 2019-03-27 martijn static int
460 03a6f52a 2019-03-29 martijn smtp_register(char *type, char *phase, char *direction, void *cb)
463 c6714f66 2019-04-01 martijn static int evinit = 0;
465 73ac3ec1 2019-03-27 martijn if (ready)
466 b633fa4c 2019-03-29 martijn fatalx("Can't register when proc is running");
468 dd179c35 2019-04-03 martijn if (!evinit) {
469 c6714f66 2019-04-01 martijn event_init();
470 dd179c35 2019-04-03 martijn evinit = 1;
473 a248a3c1 2019-03-27 martijn for (i = 0; i < NITEMS(smtp_callbacks); i++) {
474 a248a3c1 2019-03-27 martijn if (strcmp(type, smtp_callbacks[i].type) == 0 &&
475 a248a3c1 2019-03-27 martijn strcmp(phase, smtp_callbacks[i].phase) == 0 &&
476 a248a3c1 2019-03-27 martijn strcmp(direction, smtp_callbacks[i].direction) == 0) {
477 a248a3c1 2019-03-27 martijn if (smtp_callbacks[i].cb != NULL) {
478 a248a3c1 2019-03-27 martijn errno = EALREADY;
479 a248a3c1 2019-03-27 martijn return -1;
481 a248a3c1 2019-03-27 martijn smtp_callbacks[i].cb = cb;
482 a211223d 2019-04-01 martijn smtp_printf("register|%s|%s|%s\n", type, direction,
484 a248a3c1 2019-03-27 martijn return 0;
487 a248a3c1 2019-03-27 martijn errno = EINVAL;
488 a248a3c1 2019-03-27 martijn return -1;