]>
Commit | Line | Data |
---|---|---|
86e4d161 MS |
1 | /* |
2 | * Copyright (c) 2008 The DragonFly Project. All rights reserved. | |
3 | * | |
4 | * This code is derived from software contributed to The DragonFly Project | |
5 | * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg, | |
6 | * Germany. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * 3. Neither the name of The DragonFly Project nor the names of its | |
19 | * contributors may be used to endorse or promote products derived | |
20 | * from this software without specific, prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | * SUCH DAMAGE. | |
34 | * | |
c507d897 | 35 | * $DragonFly: src/libexec/dma/net.c,v 1.9 2008/09/30 17:47:21 swildner Exp $ |
86e4d161 MS |
36 | */ |
37 | ||
38 | #include <sys/param.h> | |
39 | #include <sys/queue.h> | |
40 | #include <sys/stat.h> | |
41 | #include <sys/types.h> | |
42 | #include <sys/socket.h> | |
43 | #include <netinet/in.h> | |
44 | #include <arpa/inet.h> | |
45 | ||
86e4d161 | 46 | #include <openssl/ssl.h> |
86e4d161 | 47 | |
c507d897 | 48 | #include <err.h> |
7a73c463 | 49 | #include <errno.h> |
86e4d161 MS |
50 | #include <netdb.h> |
51 | #include <setjmp.h> | |
52 | #include <signal.h> | |
53 | #include <syslog.h> | |
54 | #include <unistd.h> | |
55 | ||
56 | #include "dma.h" | |
57 | ||
58 | extern struct config *config; | |
59 | extern struct authusers authusers; | |
60 | static jmp_buf timeout_alarm; | |
7a73c463 | 61 | char neterr[BUF_SIZE]; |
86e4d161 MS |
62 | |
63 | static void | |
65bec70e | 64 | sig_alarm(int signo __unused) |
86e4d161 MS |
65 | { |
66 | longjmp(timeout_alarm, 1); | |
67 | } | |
68 | ||
69 | ssize_t | |
70 | send_remote_command(int fd, const char* fmt, ...) | |
71 | { | |
72 | va_list va; | |
73 | char cmd[4096]; | |
5b84c25b SS |
74 | size_t len, pos; |
75 | int s; | |
76 | ssize_t n; | |
86e4d161 MS |
77 | |
78 | va_start(va, fmt); | |
5b84c25b SS |
79 | s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va); |
80 | va_end(va); | |
81 | if (s == sizeof(cmd) - 2 || s < 0) | |
82 | errx(1, "Internal error: oversized command string"); | |
83 | /* We *know* there are at least two more bytes available */ | |
84 | strcat(cmd, "\r\n"); | |
85 | len = strlen(cmd); | |
86e4d161 MS |
86 | |
87 | if (((config->features & SECURETRANS) != 0) && | |
50c8e68a | 88 | ((config->features & NOSSL) == 0)) { |
5b84c25b SS |
89 | while ((s = SSL_write(config->ssl, (const char*)cmd, len)) <= 0) { |
90 | s = SSL_get_error(config->ssl, s); | |
91 | if (s != SSL_ERROR_WANT_READ && | |
92 | s != SSL_ERROR_WANT_WRITE) | |
93 | return (-1); | |
94 | } | |
86e4d161 MS |
95 | } |
96 | else { | |
5b84c25b SS |
97 | pos = 0; |
98 | while (pos < len) { | |
99 | n = write(fd, cmd + pos, len - pos); | |
100 | if (n < 0) | |
101 | return (-1); | |
102 | pos += n; | |
103 | } | |
86e4d161 | 104 | } |
86e4d161 | 105 | |
5b84c25b | 106 | return (len); |
86e4d161 MS |
107 | } |
108 | ||
50c8e68a | 109 | int |
bf28fcc6 | 110 | read_remote(int fd, int extbufsize, char *extbuf) |
86e4d161 | 111 | { |
50c8e68a MS |
112 | ssize_t rlen = 0; |
113 | size_t pos, len; | |
114 | char buff[BUF_SIZE]; | |
bf28fcc6 | 115 | int done = 0, status = 0, extbufpos = 0; |
86e4d161 MS |
116 | |
117 | if (signal(SIGALRM, sig_alarm) == SIG_ERR) { | |
7a73c463 SS |
118 | snprintf(neterr, sizeof(neterr), "SIGALRM error: %s", |
119 | strerror(errno)); | |
120 | return (1); | |
86e4d161 MS |
121 | } |
122 | if (setjmp(timeout_alarm) != 0) { | |
7a73c463 | 123 | snprintf(neterr, sizeof(neterr), "Timeout reached"); |
86e4d161 MS |
124 | return (1); |
125 | } | |
126 | alarm(CON_TIMEOUT); | |
127 | ||
128 | /* | |
50c8e68a MS |
129 | * Remote reading code from femail.c written by Henning Brauer of |
130 | * OpenBSD and released under a BSD style license. | |
86e4d161 | 131 | */ |
50c8e68a | 132 | for (len = pos = 0; !done; ) { |
bf28fcc6 | 133 | rlen = 0; |
50c8e68a MS |
134 | if (pos == 0 || |
135 | (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) { | |
136 | memmove(buff, buff + pos, len - pos); | |
137 | len -= pos; | |
138 | pos = 0; | |
139 | if (((config->features & SECURETRANS) != 0) && | |
140 | (config->features & NOSSL) == 0) { | |
141 | if ((rlen = SSL_read(config->ssl, buff + len, | |
142 | sizeof(buff) - len)) == -1) | |
143 | err(1, "read"); | |
144 | } else { | |
145 | if ((rlen = read(fd, buff + len, | |
146 | sizeof(buff) - len)) == -1) | |
147 | err(1, "read"); | |
148 | } | |
149 | len += rlen; | |
150 | } | |
bf28fcc6 MS |
151 | /* |
152 | * If there is an external buffer with a size bigger than zero | |
153 | * and as long as there is space in the external buffer and | |
154 | * there are new characters read from the mailserver | |
155 | * copy them to the external buffer | |
156 | */ | |
157 | if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0 | |
158 | && extbuf != NULL) { | |
159 | /* do not write over the bounds of the buffer */ | |
160 | if(extbufpos + rlen > (extbufsize - 1)) { | |
161 | rlen = extbufsize - extbufpos; | |
162 | } | |
163 | memcpy(extbuf + extbufpos, buff + len - rlen, rlen); | |
164 | extbufpos += rlen; | |
165 | } | |
50c8e68a MS |
166 | for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++) |
167 | ; /* Do nothing */ | |
86e4d161 | 168 | |
50c8e68a MS |
169 | if (pos == len) |
170 | return (0); | |
86e4d161 | 171 | |
50c8e68a MS |
172 | if (buff[pos] == ' ') |
173 | done = 1; | |
174 | else if (buff[pos] != '-') | |
175 | syslog(LOG_ERR, "invalid syntax in reply from server"); | |
86e4d161 | 176 | |
50c8e68a MS |
177 | /* skip up to \n */ |
178 | for (; pos < len && buff[pos - 1] != '\n'; pos++) | |
179 | ; /* Do nothing */ | |
86e4d161 | 180 | |
86e4d161 | 181 | } |
50c8e68a MS |
182 | alarm(0); |
183 | ||
7a73c463 SS |
184 | buff[len] = '\0'; |
185 | while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n')) | |
186 | buff[--len] = '\0'; | |
187 | snprintf(neterr, sizeof(neterr), "%s", buff); | |
50c8e68a MS |
188 | status = atoi(buff); |
189 | return (status/100); | |
86e4d161 MS |
190 | } |
191 | ||
192 | /* | |
193 | * Handle SMTP authentication | |
86e4d161 MS |
194 | */ |
195 | static int | |
196 | smtp_login(struct qitem *it, int fd, char *login, char* password) | |
197 | { | |
86e4d161 | 198 | char *temp; |
50c8e68a | 199 | int len, res = 0; |
86e4d161 | 200 | |
bf28fcc6 MS |
201 | res = smtp_auth_md5(it, fd, login, password); |
202 | if (res == 0) { | |
203 | return (0); | |
204 | } else if (res == -2) { | |
205 | /* | |
206 | * If the return code is -2, then then the login attempt failed, | |
207 | * do not try other login mechanisms | |
208 | */ | |
209 | return (-1); | |
86e4d161 MS |
210 | } |
211 | ||
994b9c06 SS |
212 | if ((config->features & INSECURE) != 0 || |
213 | (config->features & SECURETRANS) != 0) { | |
bf28fcc6 MS |
214 | /* Send AUTH command according to RFC 2554 */ |
215 | send_remote_command(fd, "AUTH LOGIN"); | |
216 | if (read_remote(fd, 0, NULL) != 3) { | |
791d1a91 | 217 | syslog(LOG_NOTICE, "%s: remote delivery deferred:" |
7a73c463 SS |
218 | " AUTH login not available: %s", |
219 | it->queueid, neterr); | |
bf28fcc6 MS |
220 | return (1); |
221 | } | |
86e4d161 | 222 | |
bf28fcc6 MS |
223 | len = base64_encode(login, strlen(login), &temp); |
224 | if (len <= 0) | |
225 | return (-1); | |
86e4d161 | 226 | |
bf28fcc6 MS |
227 | send_remote_command(fd, "%s", temp); |
228 | if (read_remote(fd, 0, NULL) != 3) { | |
791d1a91 | 229 | syslog(LOG_NOTICE, "%s: remote delivery deferred:" |
7a73c463 SS |
230 | " AUTH login failed: %s", it->queueid, |
231 | neterr); | |
bf28fcc6 MS |
232 | return (-1); |
233 | } | |
86e4d161 | 234 | |
bf28fcc6 MS |
235 | len = base64_encode(password, strlen(password), &temp); |
236 | if (len <= 0) | |
237 | return (-1); | |
238 | ||
239 | send_remote_command(fd, "%s", temp); | |
240 | res = read_remote(fd, 0, NULL); | |
241 | if (res == 5) { | |
791d1a91 | 242 | syslog(LOG_NOTICE, "%s: remote delivery failed:" |
7a73c463 SS |
243 | " Authentication failed: %s", |
244 | it->queueid, neterr); | |
bf28fcc6 MS |
245 | return (-1); |
246 | } else if (res != 2) { | |
791d1a91 | 247 | syslog(LOG_NOTICE, "%s: remote delivery failed:" |
7a73c463 SS |
248 | " AUTH password failed: %s", |
249 | it->queueid, neterr); | |
bf28fcc6 MS |
250 | return (-1); |
251 | } | |
252 | } else { | |
791d1a91 | 253 | syslog(LOG_WARNING, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ", |
bf28fcc6 MS |
254 | it->queueid); |
255 | return (1); | |
86e4d161 MS |
256 | } |
257 | ||
258 | return (0); | |
259 | } | |
260 | ||
261 | static int | |
262 | open_connection(struct qitem *it, const char *host) | |
263 | { | |
86e4d161 MS |
264 | struct addrinfo hints, *res, *res0; |
265 | char servname[128]; | |
266 | const char *errmsg = NULL; | |
86e4d161 MS |
267 | int fd, error = 0, port; |
268 | ||
65bec70e | 269 | if (config->port != 0) |
86e4d161 MS |
270 | port = config->port; |
271 | else | |
272 | port = SMTP_PORT; | |
273 | ||
bf28fcc6 | 274 | /* FIXME get MX record of host */ |
86e4d161 MS |
275 | /* Shamelessly taken from getaddrinfo(3) */ |
276 | memset(&hints, 0, sizeof(hints)); | |
277 | hints.ai_family = PF_UNSPEC; | |
278 | hints.ai_socktype = SOCK_STREAM; | |
279 | hints.ai_protocol = IPPROTO_TCP; | |
280 | ||
281 | snprintf(servname, sizeof(servname), "%d", port); | |
282 | error = getaddrinfo(host, servname, &hints, &res0); | |
283 | if (error) { | |
791d1a91 | 284 | syslog(LOG_NOTICE, "%s: remote delivery deferred: " |
86e4d161 MS |
285 | "%s: %m", it->queueid, gai_strerror(error)); |
286 | return (-1); | |
287 | } | |
288 | fd = -1; | |
289 | for (res = res0; res; res = res->ai_next) { | |
290 | fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
291 | if (fd < 0) { | |
292 | errmsg = "socket failed"; | |
293 | continue; | |
294 | } | |
295 | if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { | |
296 | errmsg = "connect failed"; | |
297 | close(fd); | |
298 | fd = -1; | |
299 | continue; | |
300 | } | |
301 | break; | |
302 | } | |
303 | if (fd < 0) { | |
791d1a91 | 304 | syslog(LOG_NOTICE, "%s: remote delivery deferred: %s (%s:%s)", |
86e4d161 MS |
305 | it->queueid, errmsg, host, servname); |
306 | freeaddrinfo(res0); | |
307 | return (-1); | |
308 | } | |
309 | freeaddrinfo(res0); | |
86e4d161 MS |
310 | return (fd); |
311 | } | |
312 | ||
8c365457 SS |
313 | static void |
314 | close_connection(int fd) | |
315 | { | |
316 | if (((config->features & SECURETRANS) != 0) && | |
317 | ((config->features & NOSSL) == 0)) | |
318 | SSL_shutdown(config->ssl); | |
319 | ||
320 | if (config->ssl != NULL) | |
321 | SSL_free(config->ssl); | |
322 | ||
323 | close(fd); | |
324 | } | |
325 | ||
86e4d161 | 326 | int |
09da7b5e | 327 | deliver_remote(struct qitem *it, const char **errmsg) |
86e4d161 MS |
328 | { |
329 | struct authuser *a; | |
50c8e68a MS |
330 | char *host, line[1000]; |
331 | int fd, error = 0, do_auth = 0, res = 0; | |
86e4d161 | 332 | size_t linelen; |
09da7b5e SS |
333 | /* asprintf can't take const */ |
334 | void *errmsgc = __DECONST(char **, errmsg); | |
86e4d161 MS |
335 | |
336 | host = strrchr(it->addr, '@'); | |
337 | /* Should not happen */ | |
a43346d5 | 338 | if (host == NULL) { |
09da7b5e | 339 | asprintf(errmsgc, "Internal error: badly formed address %s", |
a43346d5 | 340 | it->addr); |
86e4d161 | 341 | return(-1); |
a43346d5 | 342 | } else { |
86e4d161 MS |
343 | /* Step over the @ */ |
344 | host++; | |
a43346d5 | 345 | } |
86e4d161 MS |
346 | |
347 | /* Smarthost support? */ | |
348 | if (config->smarthost != NULL && strlen(config->smarthost) > 0) { | |
e0a95d28 MS |
349 | syslog(LOG_INFO, "%s: using smarthost (%s:%i)", |
350 | it->queueid, config->smarthost, config->port); | |
86e4d161 MS |
351 | host = config->smarthost; |
352 | } | |
353 | ||
354 | fd = open_connection(it, host); | |
355 | if (fd < 0) | |
e0a95d28 | 356 | return (1); |
86e4d161 | 357 | |
50c8e68a MS |
358 | /* Check first reply from remote host */ |
359 | config->features |= NOSSL; | |
bf28fcc6 | 360 | res = read_remote(fd, 0, NULL); |
50c8e68a | 361 | if (res != 2) { |
791d1a91 | 362 | syslog(LOG_WARNING, "%s: Invalid initial response: %i", |
50c8e68a MS |
363 | it->queueid, res); |
364 | return(1); | |
365 | } | |
366 | config->features &= ~NOSSL; | |
367 | ||
86e4d161 MS |
368 | if ((config->features & SECURETRANS) != 0) { |
369 | error = smtp_init_crypto(it, fd, config->features); | |
370 | if (error >= 0) | |
40234162 | 371 | syslog(LOG_INFO, "%s: SSL initialization successful", |
86e4d161 MS |
372 | it->queueid); |
373 | else | |
374 | goto out; | |
375 | } | |
f3105067 SS |
376 | |
377 | send_remote_command(fd, "EHLO %s", hostname()); | |
378 | if (read_remote(fd, 0, NULL) != 2) { | |
791d1a91 | 379 | syslog(LOG_WARNING, "%s: remote delivery deferred: " |
f3105067 SS |
380 | " EHLO failed: %s", it->queueid, neterr); |
381 | asprintf(errmsgc, "%s did not like our EHLO:\n%s", | |
382 | host, neterr); | |
383 | return (-1); | |
86e4d161 MS |
384 | } |
385 | ||
386 | /* | |
387 | * Use SMTP authentication if the user defined an entry for the remote | |
388 | * or smarthost | |
389 | */ | |
390 | SLIST_FOREACH(a, &authusers, next) { | |
391 | if (strcmp(a->host, host) == 0) { | |
392 | do_auth = 1; | |
393 | break; | |
394 | } | |
395 | } | |
396 | ||
397 | if (do_auth == 1) { | |
dfce7456 MS |
398 | /* |
399 | * Check if the user wants plain text login without using | |
400 | * encryption. | |
401 | */ | |
bf28fcc6 | 402 | syslog(LOG_INFO, "%s: Use SMTP authentication", |
86e4d161 | 403 | it->queueid); |
bf28fcc6 MS |
404 | error = smtp_login(it, fd, a->login, a->password); |
405 | if (error < 0) { | |
406 | syslog(LOG_ERR, "%s: remote delivery failed:" | |
dfce7456 | 407 | " SMTP login failed: %m", it->queueid); |
09da7b5e | 408 | asprintf(errmsgc, "SMTP login to %s failed", host); |
e0a95d28 | 409 | return (-1); |
dfce7456 | 410 | } |
bf28fcc6 MS |
411 | /* SMTP login is not available, so try without */ |
412 | else if (error > 0) | |
791d1a91 | 413 | syslog(LOG_WARNING, "%s: SMTP login not available." |
bf28fcc6 | 414 | " Try without", it->queueid); |
86e4d161 MS |
415 | } |
416 | ||
c68aff7a SS |
417 | #define READ_REMOTE_CHECK(c, exp) \ |
418 | res = read_remote(fd, 0, NULL); \ | |
419 | if (res == 5) { \ | |
420 | syslog(LOG_ERR, "%s: remote delivery failed: " \ | |
421 | c " failed: %s", it->queueid, neterr); \ | |
09da7b5e | 422 | asprintf(errmsgc, "%s did not like our " c ":\n%s", \ |
a43346d5 | 423 | host, neterr); \ |
c68aff7a SS |
424 | return (-1); \ |
425 | } else if (res != exp) { \ | |
791d1a91 | 426 | syslog(LOG_NOTICE, "%s: remote delivery deferred: " \ |
c68aff7a SS |
427 | c " failed: %s", it->queueid, neterr); \ |
428 | return (1); \ | |
86e4d161 MS |
429 | } |
430 | ||
c68aff7a SS |
431 | send_remote_command(fd, "MAIL FROM:<%s>", it->sender); |
432 | READ_REMOTE_CHECK("MAIL FROM", 2); | |
433 | ||
e0a95d28 | 434 | send_remote_command(fd, "RCPT TO:<%s>", it->addr); |
c68aff7a | 435 | READ_REMOTE_CHECK("RCPT TO", 2); |
86e4d161 MS |
436 | |
437 | send_remote_command(fd, "DATA"); | |
c68aff7a | 438 | READ_REMOTE_CHECK("DATA", 3); |
86e4d161 MS |
439 | |
440 | if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) { | |
7a73c463 SS |
441 | syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %s", |
442 | it->queueid, neterr); | |
86e4d161 MS |
443 | return (1); |
444 | } | |
445 | ||
3a7b3ec9 | 446 | error = 0; |
86e4d161 MS |
447 | while (!feof(it->queuef)) { |
448 | if (fgets(line, sizeof(line), it->queuef) == NULL) | |
449 | break; | |
450 | linelen = strlen(line); | |
451 | if (linelen == 0 || line[linelen - 1] != '\n') { | |
452 | syslog(LOG_CRIT, "%s: remote delivery failed:" | |
453 | "corrupted queue file", it->queueid); | |
09da7b5e | 454 | *errmsg = "corrupted queue file"; |
86e4d161 MS |
455 | error = -1; |
456 | goto out; | |
457 | } | |
458 | ||
459 | /* Remove trailing \n's and escape leading dots */ | |
460 | trim_line(line); | |
461 | ||
462 | /* | |
463 | * If the first character is a dot, we escape it so the line | |
464 | * length increases | |
465 | */ | |
466 | if (line[0] == '.') | |
467 | linelen++; | |
468 | ||
469 | if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) { | |
791d1a91 | 470 | syslog(LOG_NOTICE, "%s: remote delivery deferred: " |
86e4d161 MS |
471 | "write error", it->queueid); |
472 | error = 1; | |
473 | goto out; | |
474 | } | |
475 | } | |
476 | ||
477 | send_remote_command(fd, "."); | |
c68aff7a | 478 | READ_REMOTE_CHECK("final DATA", 2); |
86e4d161 MS |
479 | |
480 | send_remote_command(fd, "QUIT"); | |
83f3cc40 | 481 | if (read_remote(fd, 0, NULL) != 2) |
791d1a91 | 482 | syslog(LOG_INFO, "%s: remote delivery succeeded but " |
7a73c463 | 483 | "QUIT failed: %s", it->queueid, neterr); |
86e4d161 MS |
484 | out: |
485 | ||
8c365457 | 486 | close_connection(fd); |
86e4d161 MS |
487 | return (error); |
488 | } | |
489 |