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