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 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>
30 48c4bdc1 2019-04-04 martijn
31 48c4bdc1 2019-04-04 martijn #include "log.h"
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 48c4bdc1 2019-04-04 martijn static void smtp_data(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 48c4bdc1 2019-04-04 martijn {"filter", "data", "smtp-in", .smtp_filter = smtp_data, NULL},
77 48c4bdc1 2019-04-04 martijn {"filter", "data-line", "smtp-in", .smtp_filter = smtp_dataline, NULL},
78 48c4bdc1 2019-04-04 martijn {"report", "link-disconnect", "smtp-in",
79 48c4bdc1 2019-04-04 martijn .smtp_report = smtp_in_link_disconnect, NULL}
80 48c4bdc1 2019-04-04 martijn };
81 48c4bdc1 2019-04-04 martijn
82 48c4bdc1 2019-04-04 martijn static int ready = 0;
83 48c4bdc1 2019-04-04 martijn
84 48c4bdc1 2019-04-04 martijn int
85 48c4bdc1 2019-04-04 martijn smtp_register_filter_connect(void (*cb)(char *, int, struct timespec *, char *,
86 48c4bdc1 2019-04-04 martijn char *, uint64_t, uint64_t, char *, struct inx_addr *))
87 48c4bdc1 2019-04-04 martijn {
88 48c4bdc1 2019-04-04 martijn return smtp_register("filter", "connect", "smtp-in", (void *)cb);
89 48c4bdc1 2019-04-04 martijn }
90 48c4bdc1 2019-04-04 martijn
91 48c4bdc1 2019-04-04 martijn int
92 48c4bdc1 2019-04-04 martijn smtp_register_filter_data(void (*cb)(char *, int, struct timespec *, char *,
93 48c4bdc1 2019-04-04 martijn char *, uint64_t, uint64_t))
94 48c4bdc1 2019-04-04 martijn {
95 48c4bdc1 2019-04-04 martijn return smtp_register("filter", "data", "smtp-in", (void *)cb);
96 48c4bdc1 2019-04-04 martijn }
97 48c4bdc1 2019-04-04 martijn
98 48c4bdc1 2019-04-04 martijn int
99 48c4bdc1 2019-04-04 martijn smtp_register_filter_dataline(void (*cb)(char *, int, struct timespec *, char *,
100 48c4bdc1 2019-04-04 martijn char *, uint64_t, uint64_t, char *))
101 48c4bdc1 2019-04-04 martijn {
102 48c4bdc1 2019-04-04 martijn return smtp_register("filter", "data-line", "smtp-in", (void *)cb);
103 48c4bdc1 2019-04-04 martijn }
104 48c4bdc1 2019-04-04 martijn
105 48c4bdc1 2019-04-04 martijn int
106 48c4bdc1 2019-04-04 martijn smtp_in_register_report_disconnect(void (*cb)(char *, int, struct timespec *,
107 48c4bdc1 2019-04-04 martijn char *, char *, uint64_t))
108 48c4bdc1 2019-04-04 martijn {
109 48c4bdc1 2019-04-04 martijn return smtp_register("report", "link-disconnect", "smtp-in", (void *)cb);
110 48c4bdc1 2019-04-04 martijn }
111 48c4bdc1 2019-04-04 martijn
112 48c4bdc1 2019-04-04 martijn void
113 48c4bdc1 2019-04-04 martijn smtp_run(int debug)
114 48c4bdc1 2019-04-04 martijn {
115 48c4bdc1 2019-04-04 martijn struct event stdinev;
116 48c4bdc1 2019-04-04 martijn
117 48c4bdc1 2019-04-04 martijn smtp_printf("register|ready\n");
118 48c4bdc1 2019-04-04 martijn ready = 1;
119 48c4bdc1 2019-04-04 martijn
120 48c4bdc1 2019-04-04 martijn log_init(debug, LOG_MAIL);
121 48c4bdc1 2019-04-04 martijn event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline,
122 48c4bdc1 2019-04-04 martijn &stdinev);
123 48c4bdc1 2019-04-04 martijn event_add(&stdinev, NULL);
124 48c4bdc1 2019-04-04 martijn
125 48c4bdc1 2019-04-04 martijn if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
126 48c4bdc1 2019-04-04 martijn fatal("fcntl");
127 48c4bdc1 2019-04-04 martijn event_dispatch();
128 48c4bdc1 2019-04-04 martijn }
129 48c4bdc1 2019-04-04 martijn
130 48c4bdc1 2019-04-04 martijn static ssize_t
131 48c4bdc1 2019-04-04 martijn smtp_getline(char ** restrict buf, size_t * restrict size)
132 48c4bdc1 2019-04-04 martijn {
133 48c4bdc1 2019-04-04 martijn static char *rbuf = NULL;
134 48c4bdc1 2019-04-04 martijn static size_t rsoff = 0, reoff = 0;
135 48c4bdc1 2019-04-04 martijn static size_t rbsize = 0;
136 48c4bdc1 2019-04-04 martijn char *sep;
137 48c4bdc1 2019-04-04 martijn size_t sepoff;
138 48c4bdc1 2019-04-04 martijn ssize_t strlen, nread;
139 48c4bdc1 2019-04-04 martijn
140 48c4bdc1 2019-04-04 martijn do {
141 48c4bdc1 2019-04-04 martijn if (rsoff != reoff) {
142 48c4bdc1 2019-04-04 martijn if ((sep = memchr(rbuf + rsoff, '\n', reoff - rsoff))
143 48c4bdc1 2019-04-04 martijn != NULL) {
144 48c4bdc1 2019-04-04 martijn sepoff = sep - rbuf;
145 48c4bdc1 2019-04-04 martijn if (*buf == NULL)
146 48c4bdc1 2019-04-04 martijn *size = 0;
147 48c4bdc1 2019-04-04 martijn if (*size < (sepoff - rsoff + 1)) {
148 48c4bdc1 2019-04-04 martijn *size = sepoff - rsoff + 1;
149 48c4bdc1 2019-04-04 martijn *buf = realloc(*buf, sepoff - rsoff + 1);
150 48c4bdc1 2019-04-04 martijn if (*buf == NULL)
151 48c4bdc1 2019-04-04 martijn fatal(NULL);
152 48c4bdc1 2019-04-04 martijn }
153 48c4bdc1 2019-04-04 martijn sep[0] = '\0';
154 48c4bdc1 2019-04-04 martijn strlen = strlcpy(*buf, rbuf + rsoff, *size);
155 48c4bdc1 2019-04-04 martijn if (strlen >= *size)
156 48c4bdc1 2019-04-04 martijn fatalx("copy buffer too small");
157 48c4bdc1 2019-04-04 martijn rsoff = sepoff + 1;
158 48c4bdc1 2019-04-04 martijn return strlen;
159 48c4bdc1 2019-04-04 martijn }
160 48c4bdc1 2019-04-04 martijn }
161 48c4bdc1 2019-04-04 martijn /* If we can't fill at the end, move everything back. */
162 48c4bdc1 2019-04-04 martijn if (rbsize - reoff < 1500 && rsoff != 0) {
163 48c4bdc1 2019-04-04 martijn memmove(rbuf, rbuf + rsoff, reoff - rsoff);
164 48c4bdc1 2019-04-04 martijn reoff -= rsoff;
165 48c4bdc1 2019-04-04 martijn rsoff = 0;
166 48c4bdc1 2019-04-04 martijn }
167 48c4bdc1 2019-04-04 martijn /* If we still can't fill alloc some new memory. */
168 48c4bdc1 2019-04-04 martijn if (rbsize - reoff < 1500) {
169 48c4bdc1 2019-04-04 martijn if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
170 48c4bdc1 2019-04-04 martijn fatal(NULL);
171 48c4bdc1 2019-04-04 martijn rbsize += 4096;
172 48c4bdc1 2019-04-04 martijn }
173 48c4bdc1 2019-04-04 martijn nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff);
174 48c4bdc1 2019-04-04 martijn if (nread <= 0)
175 48c4bdc1 2019-04-04 martijn return nread;
176 48c4bdc1 2019-04-04 martijn reoff += nread;
177 48c4bdc1 2019-04-04 martijn } while (1);
178 48c4bdc1 2019-04-04 martijn }
179 48c4bdc1 2019-04-04 martijn
180 48c4bdc1 2019-04-04 martijn static void
181 48c4bdc1 2019-04-04 martijn smtp_newline(int fd, short event, void *arg)
182 48c4bdc1 2019-04-04 martijn {
183 48c4bdc1 2019-04-04 martijn struct event *stdinev = (struct event *)arg;
184 48c4bdc1 2019-04-04 martijn static char *line = NULL, *linedup = NULL;
185 48c4bdc1 2019-04-04 martijn static size_t linesize = 0;
186 48c4bdc1 2019-04-04 martijn static size_t dupsize = 0;
187 48c4bdc1 2019-04-04 martijn ssize_t linelen;
188 48c4bdc1 2019-04-04 martijn char *start, *end, *type, *direction, *phase, *params;
189 48c4bdc1 2019-04-04 martijn int version;
190 48c4bdc1 2019-04-04 martijn struct timespec tm;
191 48c4bdc1 2019-04-04 martijn uint64_t reqid, token;
192 48c4bdc1 2019-04-04 martijn int i;
193 48c4bdc1 2019-04-04 martijn
194 48c4bdc1 2019-04-04 martijn while ((linelen = smtp_getline(&line, &linesize)) > 0) {
195 48c4bdc1 2019-04-04 martijn if (dupsize < linesize) {
196 48c4bdc1 2019-04-04 martijn if ((linedup = realloc(linedup, linesize)) == NULL)
197 48c4bdc1 2019-04-04 martijn fatal(NULL);
198 48c4bdc1 2019-04-04 martijn dupsize = linesize;
199 48c4bdc1 2019-04-04 martijn }
200 48c4bdc1 2019-04-04 martijn strlcpy(linedup, line, dupsize);
201 48c4bdc1 2019-04-04 martijn type = line;
202 48c4bdc1 2019-04-04 martijn if ((start = strchr(type, '|')) == NULL)
203 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: missing version: %s", linedup);
204 48c4bdc1 2019-04-04 martijn start++[0] = '\0';
205 48c4bdc1 2019-04-04 martijn if ((end = strchr(start, '|')) == NULL)
206 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: missing time: %s", linedup);
207 48c4bdc1 2019-04-04 martijn end++[0] = '\0';
208 48c4bdc1 2019-04-04 martijn if (strcmp(start, "1") != 0)
209 48c4bdc1 2019-04-04 martijn fatalx("Unsupported protocol received: %s: %s", start, linedup);
210 48c4bdc1 2019-04-04 martijn version = 1;
211 48c4bdc1 2019-04-04 martijn start = end;
212 48c4bdc1 2019-04-04 martijn if ((direction = strchr(start, '|')) == NULL)
213 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: missing direction: %s", linedup);
214 48c4bdc1 2019-04-04 martijn direction++[0] = '\0';
215 48c4bdc1 2019-04-04 martijn tm.tv_sec = (time_t) strtoull(start, &end, 10);
216 48c4bdc1 2019-04-04 martijn tm.tv_nsec = 0;
217 48c4bdc1 2019-04-04 martijn if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
218 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: invalid timestamp: %s", linedup);
219 48c4bdc1 2019-04-04 martijn if (end[0] == '.') {
220 48c4bdc1 2019-04-04 martijn start = end + 1;
221 48c4bdc1 2019-04-04 martijn tm.tv_nsec = strtol(start, &end, 10);
222 48c4bdc1 2019-04-04 martijn if (start[0] == '\0' || end[0] != '\0')
223 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: invalid "
224 48c4bdc1 2019-04-04 martijn "timestamp: %s", linedup);
225 48c4bdc1 2019-04-04 martijn for (i = 9 - (end - start); i > 0; i--)
226 48c4bdc1 2019-04-04 martijn tm.tv_nsec *= 10;
227 48c4bdc1 2019-04-04 martijn }
228 48c4bdc1 2019-04-04 martijn if ((phase = strchr(direction, '|')) == NULL)
229 48c4bdc1 2019-04-04 martijn fatalx("Invalid line receieved: missing phase: %s", linedup);
230 48c4bdc1 2019-04-04 martijn phase++[0] = '\0';
231 48c4bdc1 2019-04-04 martijn if ((start = strchr(phase, '|')) == NULL)
232 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: missing reqid: %s", linedup);
233 48c4bdc1 2019-04-04 martijn start++[0] = '\0';
234 48c4bdc1 2019-04-04 martijn reqid = strtoull(start, &params, 16);
235 48c4bdc1 2019-04-04 martijn if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
236 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: invalid reqid: %s", linedup);
237 48c4bdc1 2019-04-04 martijn params++;
238 48c4bdc1 2019-04-04 martijn
239 48c4bdc1 2019-04-04 martijn for (i = 0; i < NITEMS(smtp_callbacks); i++) {
240 48c4bdc1 2019-04-04 martijn if (strcmp(type, smtp_callbacks[i].type) == 0 &&
241 48c4bdc1 2019-04-04 martijn strcmp(phase, smtp_callbacks[i].phase) == 0 &&
242 48c4bdc1 2019-04-04 martijn strcmp(direction, smtp_callbacks[i].direction) == 0)
243 48c4bdc1 2019-04-04 martijn break;
244 48c4bdc1 2019-04-04 martijn }
245 48c4bdc1 2019-04-04 martijn if (i == NITEMS(smtp_callbacks)) {
246 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: received unregistered "
247 48c4bdc1 2019-04-04 martijn "%s: %s: %s", type, phase, linedup);
248 48c4bdc1 2019-04-04 martijn }
249 48c4bdc1 2019-04-04 martijn if (strcmp(type, "filter") == 0) {
250 48c4bdc1 2019-04-04 martijn start = params;
251 48c4bdc1 2019-04-04 martijn token = strtoull(start, &params, 16);
252 48c4bdc1 2019-04-04 martijn if (start[0] == '|' || params[0] != '|')
253 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: invalid token: %s", linedup);
254 48c4bdc1 2019-04-04 martijn params++;
255 48c4bdc1 2019-04-04 martijn smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
256 48c4bdc1 2019-04-04 martijn version, &tm, reqid, token, params);
257 48c4bdc1 2019-04-04 martijn } else
258 48c4bdc1 2019-04-04 martijn smtp_callbacks[i].smtp_report(&(smtp_callbacks[i]),
259 48c4bdc1 2019-04-04 martijn version, &tm, reqid, params);
260 48c4bdc1 2019-04-04 martijn }
261 48c4bdc1 2019-04-04 martijn if (linelen == 0 || errno != EAGAIN)
262 48c4bdc1 2019-04-04 martijn event_del(stdinev);
263 48c4bdc1 2019-04-04 martijn }
264 48c4bdc1 2019-04-04 martijn
265 48c4bdc1 2019-04-04 martijn static void
266 48c4bdc1 2019-04-04 martijn smtp_connect(struct smtp_callback *cb, int version, struct timespec *tm,
267 48c4bdc1 2019-04-04 martijn uint64_t reqid, uint64_t token, char *params)
268 48c4bdc1 2019-04-04 martijn {
269 48c4bdc1 2019-04-04 martijn struct inx_addr addrx;
270 48c4bdc1 2019-04-04 martijn char *hostname;
271 48c4bdc1 2019-04-04 martijn char *address;
272 48c4bdc1 2019-04-04 martijn int ret;
273 48c4bdc1 2019-04-04 martijn void (*f)(char *, int, struct timespec *,char *, char *, uint64_t,
274 48c4bdc1 2019-04-04 martijn uint64_t, char *, struct inx_addr *);
275 48c4bdc1 2019-04-04 martijn
276 48c4bdc1 2019-04-04 martijn hostname = params;
277 48c4bdc1 2019-04-04 martijn if ((address = strchr(params, '|')) == NULL)
278 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: missing address: %s", params);
279 48c4bdc1 2019-04-04 martijn address++[0] = '\0';
280 48c4bdc1 2019-04-04 martijn
281 48c4bdc1 2019-04-04 martijn addrx.af = AF_INET;
282 48c4bdc1 2019-04-04 martijn if (strncasecmp(address, "ipv6:", 5) == 0) {
283 48c4bdc1 2019-04-04 martijn addrx.af = AF_INET6;
284 48c4bdc1 2019-04-04 martijn address += 5;
285 48c4bdc1 2019-04-04 martijn }
286 48c4bdc1 2019-04-04 martijn
287 48c4bdc1 2019-04-04 martijn ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
288 48c4bdc1 2019-04-04 martijn (void *)&(addrx.addr) : (void *)&(addrx.addr6));
289 48c4bdc1 2019-04-04 martijn if (ret == 0)
290 48c4bdc1 2019-04-04 martijn fatalx("Invalid line received: Couldn't parse address: %s", params);
291 48c4bdc1 2019-04-04 martijn if (ret == -1)
292 48c4bdc1 2019-04-04 martijn fatal("Couldn't convert address: %s", params);
293 48c4bdc1 2019-04-04 martijn
294 48c4bdc1 2019-04-04 martijn f = cb->cb;
295 48c4bdc1 2019-04-04 martijn f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
296 48c4bdc1 2019-04-04 martijn hostname, &addrx);
297 48c4bdc1 2019-04-04 martijn }
298 48c4bdc1 2019-04-04 martijn
299 48c4bdc1 2019-04-04 martijn static void
300 48c4bdc1 2019-04-04 martijn smtp_data(struct smtp_callback *cb, int version, struct timespec *tm,
301 48c4bdc1 2019-04-04 martijn uint64_t reqid, uint64_t token, char *params)
302 48c4bdc1 2019-04-04 martijn {
303 48c4bdc1 2019-04-04 martijn void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
304 48c4bdc1 2019-04-04 martijn uint64_t);
305 48c4bdc1 2019-04-04 martijn
306 48c4bdc1 2019-04-04 martijn f = cb->cb;
307 48c4bdc1 2019-04-04 martijn f(cb->type, version, tm, cb->direction, cb->phase, reqid, token);
308 48c4bdc1 2019-04-04 martijn }
309 48c4bdc1 2019-04-04 martijn
310 48c4bdc1 2019-04-04 martijn static void
311 48c4bdc1 2019-04-04 martijn smtp_dataline(struct smtp_callback *cb, int version, struct timespec *tm,
312 48c4bdc1 2019-04-04 martijn uint64_t reqid, uint64_t token, char *line)
313 48c4bdc1 2019-04-04 martijn {
314 48c4bdc1 2019-04-04 martijn void (*f)(char *, int, struct timespec *, char *, char *, uint64_t,
315 48c4bdc1 2019-04-04 martijn uint64_t, char *);
316 48c4bdc1 2019-04-04 martijn
317 48c4bdc1 2019-04-04 martijn f = cb->cb;
318 48c4bdc1 2019-04-04 martijn f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
319 48c4bdc1 2019-04-04 martijn line);
320 48c4bdc1 2019-04-04 martijn }
321 48c4bdc1 2019-04-04 martijn
322 48c4bdc1 2019-04-04 martijn static void
323 48c4bdc1 2019-04-04 martijn smtp_in_link_disconnect(struct smtp_callback *cb, int version,
324 48c4bdc1 2019-04-04 martijn struct timespec *tm, uint64_t reqid, char *params)
325 48c4bdc1 2019-04-04 martijn {
326 48c4bdc1 2019-04-04 martijn void (*f)(char *, int, struct timespec *, char *, char *, uint64_t);
327 48c4bdc1 2019-04-04 martijn
328 48c4bdc1 2019-04-04 martijn f = cb->cb;
329 48c4bdc1 2019-04-04 martijn f(cb->type, version, tm, cb->direction, cb->phase, reqid);
330 48c4bdc1 2019-04-04 martijn }
331 48c4bdc1 2019-04-04 martijn
332 48c4bdc1 2019-04-04 martijn void
333 48c4bdc1 2019-04-04 martijn smtp_filter_proceed(uint64_t reqid, uint64_t token)
334 48c4bdc1 2019-04-04 martijn {
335 48c4bdc1 2019-04-04 martijn smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|proceed\n", token,
336 48c4bdc1 2019-04-04 martijn reqid);
337 48c4bdc1 2019-04-04 martijn }
338 48c4bdc1 2019-04-04 martijn
339 48c4bdc1 2019-04-04 martijn static void
340 48c4bdc1 2019-04-04 martijn smtp_printf(const char *fmt, ...)
341 48c4bdc1 2019-04-04 martijn {
342 48c4bdc1 2019-04-04 martijn va_list ap;
343 48c4bdc1 2019-04-04 martijn
344 48c4bdc1 2019-04-04 martijn va_start(ap, fmt);
345 48c4bdc1 2019-04-04 martijn smtp_vprintf(fmt, ap);
346 48c4bdc1 2019-04-04 martijn va_end(ap);
347 48c4bdc1 2019-04-04 martijn }
348 48c4bdc1 2019-04-04 martijn
349 48c4bdc1 2019-04-04 martijn static void
350 48c4bdc1 2019-04-04 martijn smtp_vprintf(const char *fmt, va_list ap)
351 48c4bdc1 2019-04-04 martijn {
352 48c4bdc1 2019-04-04 martijn va_list cap;
353 48c4bdc1 2019-04-04 martijn static struct smtp_writebuf buf = {NULL, 0, 0};
354 48c4bdc1 2019-04-04 martijn int fmtlen;
355 48c4bdc1 2019-04-04 martijn
356 48c4bdc1 2019-04-04 martijn va_copy(cap, ap);
357 48c4bdc1 2019-04-04 martijn fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
358 48c4bdc1 2019-04-04 martijn ap);
359 48c4bdc1 2019-04-04 martijn if (fmtlen == -1)
360 48c4bdc1 2019-04-04 martijn fatal("vsnprintf");
361 48c4bdc1 2019-04-04 martijn if (fmtlen >= buf.bufsize - buf.buflen) {
362 48c4bdc1 2019-04-04 martijn buf.bufsize = buf.buflen + fmtlen + 1;
363 48c4bdc1 2019-04-04 martijn buf.buf = reallocarray(buf.buf, buf.bufsize,
364 48c4bdc1 2019-04-04 martijn sizeof(*(buf.buf)));
365 48c4bdc1 2019-04-04 martijn if (buf.buf == NULL)
366 48c4bdc1 2019-04-04 martijn fatalx(NULL);
367 48c4bdc1 2019-04-04 martijn fmtlen = vsnprintf(buf.buf + buf.buflen,
368 48c4bdc1 2019-04-04 martijn buf.bufsize - buf.buflen, fmt, cap);
369 48c4bdc1 2019-04-04 martijn if (fmtlen == -1)
370 48c4bdc1 2019-04-04 martijn fatal("vsnprintf");
371 48c4bdc1 2019-04-04 martijn }
372 48c4bdc1 2019-04-04 martijn va_end(cap);
373 48c4bdc1 2019-04-04 martijn buf.buflen += fmtlen;
374 48c4bdc1 2019-04-04 martijn
375 48c4bdc1 2019-04-04 martijn if (strchr(buf.buf, '\n') != NULL)
376 48c4bdc1 2019-04-04 martijn smtp_write(STDOUT_FILENO, EV_WRITE, &buf);
377 48c4bdc1 2019-04-04 martijn }
378 48c4bdc1 2019-04-04 martijn
379 48c4bdc1 2019-04-04 martijn static void
380 48c4bdc1 2019-04-04 martijn smtp_write(int fd, short event, void *arg)
381 48c4bdc1 2019-04-04 martijn {
382 48c4bdc1 2019-04-04 martijn struct smtp_writebuf *buf = arg;
383 48c4bdc1 2019-04-04 martijn static struct event stdoutev;
384 48c4bdc1 2019-04-04 martijn static int evset = 0;
385 48c4bdc1 2019-04-04 martijn ssize_t wlen;
386 48c4bdc1 2019-04-04 martijn
387 48c4bdc1 2019-04-04 martijn if (buf->buflen == 0)
388 48c4bdc1 2019-04-04 martijn return;
389 48c4bdc1 2019-04-04 martijn if (event_pending(&stdoutev, EV_WRITE, NULL))
390 48c4bdc1 2019-04-04 martijn return;
391 48c4bdc1 2019-04-04 martijn if (!evset) {
392 48c4bdc1 2019-04-04 martijn event_set(&stdoutev, fd, EV_WRITE, smtp_write, buf);
393 48c4bdc1 2019-04-04 martijn evset = 1;
394 48c4bdc1 2019-04-04 martijn }
395 48c4bdc1 2019-04-04 martijn wlen = write(fd, buf->buf, buf->buflen);
396 48c4bdc1 2019-04-04 martijn if (wlen == -1) {
397 48c4bdc1 2019-04-04 martijn if (errno != EAGAIN && errno != EINTR)
398 48c4bdc1 2019-04-04 martijn fatal("Failed to write to smtpd");
399 48c4bdc1 2019-04-04 martijn event_add(&stdoutev, NULL);
400 48c4bdc1 2019-04-04 martijn return;
401 48c4bdc1 2019-04-04 martijn }
402 48c4bdc1 2019-04-04 martijn if (wlen < buf->buflen) {
403 48c4bdc1 2019-04-04 martijn memmove(buf->buf, buf->buf + wlen, buf->buflen - wlen);
404 48c4bdc1 2019-04-04 martijn event_add(&stdoutev, NULL);
405 48c4bdc1 2019-04-04 martijn }
406 48c4bdc1 2019-04-04 martijn buf->buflen -= wlen;
407 48c4bdc1 2019-04-04 martijn }
408 48c4bdc1 2019-04-04 martijn
409 48c4bdc1 2019-04-04 martijn void
410 48c4bdc1 2019-04-04 martijn smtp_filter_reject(uint64_t reqid, uint64_t token, int code,
411 48c4bdc1 2019-04-04 martijn const char *reason, ...)
412 48c4bdc1 2019-04-04 martijn {
413 48c4bdc1 2019-04-04 martijn va_list ap;
414 48c4bdc1 2019-04-04 martijn
415 48c4bdc1 2019-04-04 martijn if (code < 200 || code > 599)
416 48c4bdc1 2019-04-04 martijn fatalx("Invalid reject code");
417 48c4bdc1 2019-04-04 martijn
418 48c4bdc1 2019-04-04 martijn smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
419 48c4bdc1 2019-04-04 martijn reqid, code);
420 48c4bdc1 2019-04-04 martijn va_start(ap, reason);
421 48c4bdc1 2019-04-04 martijn smtp_vprintf(reason, ap);
422 48c4bdc1 2019-04-04 martijn va_end(ap);
423 48c4bdc1 2019-04-04 martijn smtp_printf("\n");
424 48c4bdc1 2019-04-04 martijn }
425 48c4bdc1 2019-04-04 martijn
426 48c4bdc1 2019-04-04 martijn void
427 48c4bdc1 2019-04-04 martijn smtp_filter_disconnect(uint64_t reqid, uint64_t token, const char *reason, ...)
428 48c4bdc1 2019-04-04 martijn {
429 48c4bdc1 2019-04-04 martijn va_list ap;
430 48c4bdc1 2019-04-04 martijn
431 48c4bdc1 2019-04-04 martijn smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|disconnect|421 ",
432 48c4bdc1 2019-04-04 martijn token, reqid);
433 48c4bdc1 2019-04-04 martijn va_start(ap, reason);
434 48c4bdc1 2019-04-04 martijn smtp_vprintf(reason, ap);
435 48c4bdc1 2019-04-04 martijn va_end(ap);
436 48c4bdc1 2019-04-04 martijn smtp_printf("\n");
437 48c4bdc1 2019-04-04 martijn }
438 48c4bdc1 2019-04-04 martijn
439 48c4bdc1 2019-04-04 martijn void
440 48c4bdc1 2019-04-04 martijn smtp_filter_dataline(uint64_t reqid, uint64_t token, const char *line, ...)
441 48c4bdc1 2019-04-04 martijn {
442 48c4bdc1 2019-04-04 martijn va_list ap;
443 48c4bdc1 2019-04-04 martijn
444 48c4bdc1 2019-04-04 martijn smtp_printf("filter-dataline|%016"PRIx64"|%016"PRIx64"|", token, reqid);
445 48c4bdc1 2019-04-04 martijn va_start(ap, line);
446 48c4bdc1 2019-04-04 martijn smtp_vprintf(line, ap);
447 48c4bdc1 2019-04-04 martijn va_end(ap);
448 48c4bdc1 2019-04-04 martijn smtp_printf("\n");
449 48c4bdc1 2019-04-04 martijn }
450 48c4bdc1 2019-04-04 martijn
451 48c4bdc1 2019-04-04 martijn static int
452 48c4bdc1 2019-04-04 martijn smtp_register(char *type, char *phase, char *direction, void *cb)
453 48c4bdc1 2019-04-04 martijn {
454 48c4bdc1 2019-04-04 martijn int i;
455 48c4bdc1 2019-04-04 martijn static int evinit = 0;
456 48c4bdc1 2019-04-04 martijn
457 48c4bdc1 2019-04-04 martijn if (ready)
458 48c4bdc1 2019-04-04 martijn fatalx("Can't register when proc is running");
459 48c4bdc1 2019-04-04 martijn
460 48c4bdc1 2019-04-04 martijn if (!evinit) {
461 48c4bdc1 2019-04-04 martijn event_init();
462 48c4bdc1 2019-04-04 martijn evinit = 1;
463 48c4bdc1 2019-04-04 martijn }
464 48c4bdc1 2019-04-04 martijn
465 48c4bdc1 2019-04-04 martijn for (i = 0; i < NITEMS(smtp_callbacks); i++) {
466 48c4bdc1 2019-04-04 martijn if (strcmp(type, smtp_callbacks[i].type) == 0 &&
467 48c4bdc1 2019-04-04 martijn strcmp(phase, smtp_callbacks[i].phase) == 0 &&
468 48c4bdc1 2019-04-04 martijn strcmp(direction, smtp_callbacks[i].direction) == 0) {
469 48c4bdc1 2019-04-04 martijn if (smtp_callbacks[i].cb != NULL) {
470 48c4bdc1 2019-04-04 martijn errno = EALREADY;
471 48c4bdc1 2019-04-04 martijn return -1;
472 48c4bdc1 2019-04-04 martijn }
473 48c4bdc1 2019-04-04 martijn smtp_callbacks[i].cb = cb;
474 48c4bdc1 2019-04-04 martijn smtp_printf("register|%s|%s|%s\n", type, direction,
475 48c4bdc1 2019-04-04 martijn phase);
476 48c4bdc1 2019-04-04 martijn return 0;
477 48c4bdc1 2019-04-04 martijn }
478 48c4bdc1 2019-04-04 martijn }
479 48c4bdc1 2019-04-04 martijn errno = EINVAL;
480 48c4bdc1 2019-04-04 martijn return -1;
481 48c4bdc1 2019-04-04 martijn }