Blame


1 48c4bdc1 2019-04-04 martijn /*
2 48c4bdc1 2019-04-04 martijn * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
3 48c4bdc1 2019-04-04 martijn *
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.
7 48c4bdc1 2019-04-04 martijn *
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.
15 48c4bdc1 2019-04-04 martijn */
16 48c4bdc1 2019-04-04 martijn #include <sys/time.h>
17 48c4bdc1 2019-04-04 martijn #include <sys/socket.h>
18 48c4bdc1 2019-04-04 martijn
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>
31 48c4bdc1 2019-04-04 martijn
32 48c4bdc1 2019-04-04 martijn #include "smtp_proc.h"
33 48c4bdc1 2019-04-04 martijn
34 48c4bdc1 2019-04-04 martijn #define NITEMS(x) (sizeof(x) / sizeof(*x))
35 48c4bdc1 2019-04-04 martijn
36 48c4bdc1 2019-04-04 martijn struct smtp_callback;
37 48c4bdc1 2019-04-04 martijn struct smtp_request;
38 48c4bdc1 2019-04-04 martijn
39 48c4bdc1 2019-04-04 martijn extern struct event_base *current_base;
40 48c4bdc1 2019-04-04 martijn
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 *);
56 48c4bdc1 2019-04-04 martijn
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;
61 48c4bdc1 2019-04-04 martijn };
62 48c4bdc1 2019-04-04 martijn
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;
67 48c4bdc1 2019-04-04 martijn union {
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 *);
72 48c4bdc1 2019-04-04 martijn };
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}
81 48c4bdc1 2019-04-04 martijn };
82 48c4bdc1 2019-04-04 martijn
83 48c4bdc1 2019-04-04 martijn static int ready = 0;
84 48c4bdc1 2019-04-04 martijn
85 48c4bdc1 2019-04-04 martijn int
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 *))
88 48c4bdc1 2019-04-04 martijn {
89 48c4bdc1 2019-04-04 martijn return smtp_register("filter", "connect", "smtp-in", (void *)cb);
90 48c4bdc1 2019-04-04 martijn }
91 48c4bdc1 2019-04-04 martijn
92 48c4bdc1 2019-04-04 martijn int
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))
95 48c4bdc1 2019-04-04 martijn {
96 48c4bdc1 2019-04-04 martijn return smtp_register("filter", "data", "smtp-in", (void *)cb);
97 48c4bdc1 2019-04-04 martijn }
98 48c4bdc1 2019-04-04 martijn
99 48c4bdc1 2019-04-04 martijn int
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 *))
102 48c4bdc1 2019-04-04 martijn {
103 48c4bdc1 2019-04-04 martijn return smtp_register("filter", "data-line", "smtp-in", (void *)cb);
104 48c4bdc1 2019-04-04 martijn }
105 48c4bdc1 2019-04-04 martijn
106 48c4bdc1 2019-04-04 martijn int
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))
109 748e39c7 2019-04-06 martijn {
110 748e39c7 2019-04-06 martijn return smtp_register("filter", "commit", "smtp-in", (void *)cb);
111 748e39c7 2019-04-06 martijn }
112 748e39c7 2019-04-06 martijn
113 748e39c7 2019-04-06 martijn int
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))
116 48c4bdc1 2019-04-04 martijn {
117 48c4bdc1 2019-04-04 martijn return smtp_register("report", "link-disconnect", "smtp-in", (void *)cb);
118 48c4bdc1 2019-04-04 martijn }
119 48c4bdc1 2019-04-04 martijn
120 48c4bdc1 2019-04-04 martijn void
121 48c4bdc1 2019-04-04 martijn smtp_run(int debug)
122 48c4bdc1 2019-04-04 martijn {
123 48c4bdc1 2019-04-04 martijn struct event stdinev;
124 48c4bdc1 2019-04-04 martijn
125 48c4bdc1 2019-04-04 martijn smtp_printf("register|ready\n");
126 48c4bdc1 2019-04-04 martijn ready = 1;
127 48c4bdc1 2019-04-04 martijn
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);
131 48c4bdc1 2019-04-04 martijn
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();
135 48c4bdc1 2019-04-04 martijn }
136 48c4bdc1 2019-04-04 martijn
137 48c4bdc1 2019-04-04 martijn static ssize_t
138 48c4bdc1 2019-04-04 martijn smtp_getline(char ** restrict buf, size_t * restrict size)
139 48c4bdc1 2019-04-04 martijn {
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;
146 48c4bdc1 2019-04-04 martijn
147 48c4bdc1 2019-04-04 martijn do {
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);
159 48c4bdc1 2019-04-04 martijn }
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;
166 48c4bdc1 2019-04-04 martijn }
167 48c4bdc1 2019-04-04 martijn }
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;
173 48c4bdc1 2019-04-04 martijn }
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;
179 48c4bdc1 2019-04-04 martijn }
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);
185 48c4bdc1 2019-04-04 martijn }
186 48c4bdc1 2019-04-04 martijn
187 48c4bdc1 2019-04-04 martijn static void
188 48c4bdc1 2019-04-04 martijn smtp_newline(int fd, short event, void *arg)
189 48c4bdc1 2019-04-04 martijn {
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;
199 48c4bdc1 2019-04-04 martijn int i;
200 48c4bdc1 2019-04-04 martijn
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;
206 48c4bdc1 2019-04-04 martijn }
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;
234 48c4bdc1 2019-04-04 martijn }
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, &params, 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++;
245 48c4bdc1 2019-04-04 martijn
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)
250 48c4bdc1 2019-04-04 martijn break;
251 48c4bdc1 2019-04-04 martijn }
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);
255 48c4bdc1 2019-04-04 martijn }
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, &params, 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);
264 48c4bdc1 2019-04-04 martijn } else
265 48c4bdc1 2019-04-04 martijn smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
266 48c4bdc1 2019-04-04 martijn version, &tm, reqid, params);
267 48c4bdc1 2019-04-04 martijn }
268 48c4bdc1 2019-04-04 martijn if (linelen == 0 || errno != EAGAIN)
269 48c4bdc1 2019-04-04 martijn event_del(stdinev);
270 48c4bdc1 2019-04-04 martijn }
271 48c4bdc1 2019-04-04 martijn
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)
275 48c4bdc1 2019-04-04 martijn {
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 *);
282 48c4bdc1 2019-04-04 martijn
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';
287 48c4bdc1 2019-04-04 martijn
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;
292 48c4bdc1 2019-04-04 martijn }
293 48c4bdc1 2019-04-04 martijn
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);
300 48c4bdc1 2019-04-04 martijn
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);
304 48c4bdc1 2019-04-04 martijn }
305 48c4bdc1 2019-04-04 martijn
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)
309 48c4bdc1 2019-04-04 martijn {
310 48c4bdc1 2019-04-04 martijn void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
311 48c4bdc1 2019-04-04 martijn uint64_t);
312 48c4bdc1 2019-04-04 martijn
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);
315 48c4bdc1 2019-04-04 martijn }
316 48c4bdc1 2019-04-04 martijn
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)
320 48c4bdc1 2019-04-04 martijn {
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 *);
323 48c4bdc1 2019-04-04 martijn
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,
326 48c4bdc1 2019-04-04 martijn line);
327 48c4bdc1 2019-04-04 martijn }
328 48c4bdc1 2019-04-04 martijn
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)
332 48c4bdc1 2019-04-04 martijn {
333 48c4bdc1 2019-04-04 martijn void (*f)(char *, int, struct timespec *, char *, char *, uint64_t);
334 48c4bdc1 2019-04-04 martijn
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);
337 48c4bdc1 2019-04-04 martijn }
338 48c4bdc1 2019-04-04 martijn
339 48c4bdc1 2019-04-04 martijn void
340 48c4bdc1 2019-04-04 martijn smtp_filter_proceed(uint64_t reqid, uint64_t token)
341 48c4bdc1 2019-04-04 martijn {
342 48c4bdc1 2019-04-04 martijn smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
343 48c4bdc1 2019-04-04 martijn reqid);
344 48c4bdc1 2019-04-04 martijn }
345 48c4bdc1 2019-04-04 martijn
346 48c4bdc1 2019-04-04 martijn static void
347 48c4bdc1 2019-04-04 martijn smtp_printf(const char *fmt, ...)
348 48c4bdc1 2019-04-04 martijn {
349 48c4bdc1 2019-04-04 martijn va_list ap;
350 48c4bdc1 2019-04-04 martijn
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);
354 48c4bdc1 2019-04-04 martijn }
355 48c4bdc1 2019-04-04 martijn
356 48c4bdc1 2019-04-04 martijn static void
357 48c4bdc1 2019-04-04 martijn smtp_vprintf(const char *fmt, va_list ap)
358 48c4bdc1 2019-04-04 martijn {
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;
362 48c4bdc1 2019-04-04 martijn
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,
365 48c4bdc1 2019-04-04 martijn ap);
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");
378 48c4bdc1 2019-04-04 martijn }
379 48c4bdc1 2019-04-04 martijn va_end(cap);
380 48c4bdc1 2019-04-04 martijn buf.buflen += fmtlen;
381 48c4bdc1 2019-04-04 martijn
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);
384 48c4bdc1 2019-04-04 martijn }
385 48c4bdc1 2019-04-04 martijn
386 48c4bdc1 2019-04-04 martijn static void
387 48c4bdc1 2019-04-04 martijn smtp_write(int fd, short event, void *arg)
388 48c4bdc1 2019-04-04 martijn {
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;
393 48c4bdc1 2019-04-04 martijn
394 48c4bdc1 2019-04-04 martijn if (buf->buflen == 0)
395 48c4bdc1 2019-04-04 martijn return;
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;
399 48c4bdc1 2019-04-04 martijn }
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);
405 48c4bdc1 2019-04-04 martijn return;
406 48c4bdc1 2019-04-04 martijn }
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);
410 48c4bdc1 2019-04-04 martijn }
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);
414 48c4bdc1 2019-04-04 martijn }
415 48c4bdc1 2019-04-04 martijn
416 48c4bdc1 2019-04-04 martijn void
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, ...)
419 48c4bdc1 2019-04-04 martijn {
420 48c4bdc1 2019-04-04 martijn va_list ap;
421 48c4bdc1 2019-04-04 martijn
422 48c4bdc1 2019-04-04 martijn if (code < 200 || code > 599)
423 27541da8 2019-06-30 martijn errx(1, "Invalid reject code");
424 48c4bdc1 2019-04-04 martijn
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");
431 48c4bdc1 2019-04-04 martijn }
432 48c4bdc1 2019-04-04 martijn
433 48c4bdc1 2019-04-04 martijn void
434 48c4bdc1 2019-04-04 martijn smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
435 48c4bdc1 2019-04-04 martijn {
436 48c4bdc1 2019-04-04 martijn va_list ap;
437 48c4bdc1 2019-04-04 martijn
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");
444 48c4bdc1 2019-04-04 martijn }
445 48c4bdc1 2019-04-04 martijn
446 48c4bdc1 2019-04-04 martijn void
447 48c4bdc1 2019-04-04 martijn smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
448 48c4bdc1 2019-04-04 martijn {
449 48c4bdc1 2019-04-04 martijn va_list ap;
450 48c4bdc1 2019-04-04 martijn
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");
456 48c4bdc1 2019-04-04 martijn }
457 48c4bdc1 2019-04-04 martijn
458 48c4bdc1 2019-04-04 martijn static int
459 48c4bdc1 2019-04-04 martijn smtp_register(char *type, char *phase, char *direction, void *cb)
460 48c4bdc1 2019-04-04 martijn {
461 48c4bdc1 2019-04-04 martijn int i;
462 48c4bdc1 2019-04-04 martijn static int evinit = 0;
463 48c4bdc1 2019-04-04 martijn
464 48c4bdc1 2019-04-04 martijn if (ready)
465 27541da8 2019-06-30 martijn errx(1, "Can't register when proc is running");
466 48c4bdc1 2019-04-04 martijn
467 48c4bdc1 2019-04-04 martijn if (!evinit) {
468 48c4bdc1 2019-04-04 martijn event_init();
469 48c4bdc1 2019-04-04 martijn evinit = 1;
470 48c4bdc1 2019-04-04 martijn }
471 48c4bdc1 2019-04-04 martijn
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;
479 48c4bdc1 2019-04-04 martijn }
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,
482 48c4bdc1 2019-04-04 martijn phase);
483 48c4bdc1 2019-04-04 martijn return 0;
484 48c4bdc1 2019-04-04 martijn }
485 48c4bdc1 2019-04-04 martijn }
486 48c4bdc1 2019-04-04 martijn errno = EINVAL;
487 48c4bdc1 2019-04-04 martijn return -1;
488 48c4bdc1 2019-04-04 martijn }