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