dma: always send EHLO after tls setup
[dragonfly.git] / libexec / dma / net.c
CommitLineData
f67beddd
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 *
bc7baf1d 35 * $DragonFly: src/libexec/dma/net.c,v 1.9 2008/09/30 17:47:21 swildner Exp $
f67beddd
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
46#ifdef HAVE_CRYPTO
47#include <openssl/ssl.h>
48#endif /* HAVE_CRYPTO */
49
bc7baf1d 50#include <err.h>
f67beddd
MS
51#include <netdb.h>
52#include <setjmp.h>
53#include <signal.h>
54#include <syslog.h>
55#include <unistd.h>
56
57#include "dma.h"
58
59extern struct config *config;
60extern struct authusers authusers;
61static jmp_buf timeout_alarm;
62
63static void
dba19026 64sig_alarm(int signo __unused)
f67beddd
MS
65{
66 longjmp(timeout_alarm, 1);
67}
68
69ssize_t
70send_remote_command(int fd, const char* fmt, ...)
71{
72 va_list va;
73 char cmd[4096];
74 ssize_t len = 0;
75
76 va_start(va, fmt);
77 vsprintf(cmd, fmt, va);
78
79 if (((config->features & SECURETRANS) != 0) &&
6ef9fe01 80 ((config->features & NOSSL) == 0)) {
f67beddd
MS
81 len = SSL_write(config->ssl, (const char*)cmd, strlen(cmd));
82 SSL_write(config->ssl, "\r\n", 2);
83 }
84 else {
85 len = write(fd, cmd, strlen(cmd));
86 write(fd, "\r\n", 2);
87 }
88 va_end(va);
89
90 return (len+2);
91}
92
6ef9fe01 93int
7b68d8ae 94read_remote(int fd, int extbufsize, char *extbuf)
f67beddd 95{
6ef9fe01
MS
96 ssize_t rlen = 0;
97 size_t pos, len;
98 char buff[BUF_SIZE];
7b68d8ae 99 int done = 0, status = 0, extbufpos = 0;
f67beddd
MS
100
101 if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
102 syslog(LOG_ERR, "SIGALRM error: %m");
103 }
104 if (setjmp(timeout_alarm) != 0) {
105 syslog(LOG_ERR, "Timeout reached");
106 return (1);
107 }
108 alarm(CON_TIMEOUT);
109
110 /*
6ef9fe01
MS
111 * Remote reading code from femail.c written by Henning Brauer of
112 * OpenBSD and released under a BSD style license.
f67beddd 113 */
6ef9fe01 114 for (len = pos = 0; !done; ) {
7b68d8ae 115 rlen = 0;
6ef9fe01
MS
116 if (pos == 0 ||
117 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
118 memmove(buff, buff + pos, len - pos);
119 len -= pos;
120 pos = 0;
121 if (((config->features & SECURETRANS) != 0) &&
122 (config->features & NOSSL) == 0) {
123 if ((rlen = SSL_read(config->ssl, buff + len,
124 sizeof(buff) - len)) == -1)
125 err(1, "read");
126 } else {
127 if ((rlen = read(fd, buff + len,
128 sizeof(buff) - len)) == -1)
129 err(1, "read");
130 }
131 len += rlen;
132 }
7b68d8ae
MS
133 /*
134 * If there is an external buffer with a size bigger than zero
135 * and as long as there is space in the external buffer and
136 * there are new characters read from the mailserver
137 * copy them to the external buffer
138 */
139 if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
140 && extbuf != NULL) {
141 /* do not write over the bounds of the buffer */
142 if(extbufpos + rlen > (extbufsize - 1)) {
143 rlen = extbufsize - extbufpos;
144 }
145 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
146 extbufpos += rlen;
147 }
6ef9fe01
MS
148 for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
149 ; /* Do nothing */
f67beddd 150
6ef9fe01
MS
151 if (pos == len)
152 return (0);
f67beddd 153
6ef9fe01
MS
154 if (buff[pos] == ' ')
155 done = 1;
156 else if (buff[pos] != '-')
157 syslog(LOG_ERR, "invalid syntax in reply from server");
f67beddd 158
6ef9fe01
MS
159 /* skip up to \n */
160 for (; pos < len && buff[pos - 1] != '\n'; pos++)
161 ; /* Do nothing */
f67beddd 162
f67beddd 163 }
6ef9fe01
MS
164 alarm(0);
165
166 status = atoi(buff);
167 return (status/100);
f67beddd
MS
168}
169
170/*
171 * Handle SMTP authentication
f67beddd
MS
172 */
173static int
174smtp_login(struct qitem *it, int fd, char *login, char* password)
175{
f67beddd 176 char *temp;
6ef9fe01 177 int len, res = 0;
f67beddd 178
7b68d8ae
MS
179#ifdef HAVE_CRYPTO
180 res = smtp_auth_md5(it, fd, login, password);
181 if (res == 0) {
182 return (0);
183 } else if (res == -2) {
184 /*
185 * If the return code is -2, then then the login attempt failed,
186 * do not try other login mechanisms
187 */
188 return (-1);
f67beddd 189 }
7b68d8ae 190#endif /* HAVE_CRYPTO */
f67beddd 191
7b68d8ae
MS
192 if ((config->features & INSECURE) != 0) {
193 /* Send AUTH command according to RFC 2554 */
194 send_remote_command(fd, "AUTH LOGIN");
195 if (read_remote(fd, 0, NULL) != 3) {
196 syslog(LOG_ERR, "%s: remote delivery deferred:"
197 " AUTH login not available: %m", it->queueid);
198 return (1);
199 }
f67beddd 200
7b68d8ae
MS
201 len = base64_encode(login, strlen(login), &temp);
202 if (len <= 0)
203 return (-1);
f67beddd 204
7b68d8ae
MS
205 send_remote_command(fd, "%s", temp);
206 if (read_remote(fd, 0, NULL) != 3) {
207 syslog(LOG_ERR, "%s: remote delivery deferred:"
208 " AUTH login failed: %m", it->queueid);
209 return (-1);
210 }
f67beddd 211
7b68d8ae
MS
212 len = base64_encode(password, strlen(password), &temp);
213 if (len <= 0)
214 return (-1);
215
216 send_remote_command(fd, "%s", temp);
217 res = read_remote(fd, 0, NULL);
218 if (res == 5) {
219 syslog(LOG_ERR, "%s: remote delivery failed:"
220 " Authentication failed: %m", it->queueid);
221 return (-1);
222 } else if (res != 2) {
223 syslog(LOG_ERR, "%s: remote delivery failed:"
224 " AUTH password failed: %m", it->queueid);
225 return (-1);
226 }
227 } else {
228 syslog(LOG_ERR, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ",
229 it->queueid);
230 return (1);
f67beddd
MS
231 }
232
233 return (0);
234}
235
236static int
237open_connection(struct qitem *it, const char *host)
238{
f67beddd
MS
239 struct addrinfo hints, *res, *res0;
240 char servname[128];
241 const char *errmsg = NULL;
f67beddd
MS
242 int fd, error = 0, port;
243
dba19026 244 if (config->port != 0)
f67beddd
MS
245 port = config->port;
246 else
247 port = SMTP_PORT;
248
7b68d8ae 249 /* FIXME get MX record of host */
f67beddd
MS
250 /* Shamelessly taken from getaddrinfo(3) */
251 memset(&hints, 0, sizeof(hints));
252 hints.ai_family = PF_UNSPEC;
253 hints.ai_socktype = SOCK_STREAM;
254 hints.ai_protocol = IPPROTO_TCP;
255
256 snprintf(servname, sizeof(servname), "%d", port);
257 error = getaddrinfo(host, servname, &hints, &res0);
258 if (error) {
259 syslog(LOG_ERR, "%s: remote delivery deferred: "
260 "%s: %m", it->queueid, gai_strerror(error));
261 return (-1);
262 }
263 fd = -1;
264 for (res = res0; res; res = res->ai_next) {
265 fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
266 if (fd < 0) {
267 errmsg = "socket failed";
268 continue;
269 }
270 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
271 errmsg = "connect failed";
272 close(fd);
273 fd = -1;
274 continue;
275 }
276 break;
277 }
278 if (fd < 0) {
279 syslog(LOG_ERR, "%s: remote delivery deferred: %s (%s:%s)",
280 it->queueid, errmsg, host, servname);
281 freeaddrinfo(res0);
282 return (-1);
283 }
284 freeaddrinfo(res0);
f67beddd
MS
285 return (fd);
286}
287
288int
4a23bd3d 289deliver_remote(struct qitem *it, const char **errmsg)
f67beddd
MS
290{
291 struct authuser *a;
6ef9fe01
MS
292 char *host, line[1000];
293 int fd, error = 0, do_auth = 0, res = 0;
f67beddd
MS
294 size_t linelen;
295
296 host = strrchr(it->addr, '@');
297 /* Should not happen */
298 if (host == NULL)
299 return(-1);
300 else
301 /* Step over the @ */
302 host++;
303
304 /* Smarthost support? */
305 if (config->smarthost != NULL && strlen(config->smarthost) > 0) {
4a23bd3d
MS
306 syslog(LOG_INFO, "%s: using smarthost (%s:%i)",
307 it->queueid, config->smarthost, config->port);
f67beddd
MS
308 host = config->smarthost;
309 }
310
311 fd = open_connection(it, host);
312 if (fd < 0)
4a23bd3d 313 return (1);
f67beddd 314
6ef9fe01
MS
315 /* Check first reply from remote host */
316 config->features |= NOSSL;
7b68d8ae 317 res = read_remote(fd, 0, NULL);
6ef9fe01
MS
318 if (res != 2) {
319 syslog(LOG_INFO, "%s: Invalid initial response: %i",
320 it->queueid, res);
321 return(1);
322 }
323 config->features &= ~NOSSL;
324
f67beddd
MS
325#ifdef HAVE_CRYPTO
326 if ((config->features & SECURETRANS) != 0) {
327 error = smtp_init_crypto(it, fd, config->features);
328 if (error >= 0)
0ca0cd25 329 syslog(LOG_INFO, "%s: SSL initialization successful",
f67beddd
MS
330 it->queueid);
331 else
332 goto out;
333 }
f67beddd 334#endif /* HAVE_CRYPTO */
7bf034e1
SS
335
336 send_remote_command(fd, "EHLO %s", hostname());
337 if (read_remote(fd, 0, NULL) != 2) {
338 syslog(LOG_ERR, "%s: remote delivery deferred: "
339 " EHLO failed: %m", it->queueid);
340 return (-1);
f67beddd
MS
341 }
342
343 /*
344 * Use SMTP authentication if the user defined an entry for the remote
345 * or smarthost
346 */
347 SLIST_FOREACH(a, &authusers, next) {
348 if (strcmp(a->host, host) == 0) {
349 do_auth = 1;
350 break;
351 }
352 }
353
354 if (do_auth == 1) {
b558d098
MS
355 /*
356 * Check if the user wants plain text login without using
357 * encryption.
358 */
7b68d8ae 359 syslog(LOG_INFO, "%s: Use SMTP authentication",
f67beddd 360 it->queueid);
7b68d8ae
MS
361 error = smtp_login(it, fd, a->login, a->password);
362 if (error < 0) {
363 syslog(LOG_ERR, "%s: remote delivery failed:"
b558d098 364 " SMTP login failed: %m", it->queueid);
4a23bd3d 365 return (-1);
b558d098 366 }
7b68d8ae
MS
367 /* SMTP login is not available, so try without */
368 else if (error > 0)
369 syslog(LOG_ERR, "%s: SMTP login not available."
370 " Try without", it->queueid);
f67beddd
MS
371 }
372
373 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
7b68d8ae 374 if (read_remote(fd, 0, NULL) != 2) {
f67beddd
MS
375 syslog(LOG_ERR, "%s: remote delivery deferred:"
376 " MAIL FROM failed: %m", it->queueid);
4a23bd3d 377 return (1);
f67beddd
MS
378 }
379
4a23bd3d
MS
380 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
381 if (read_remote(fd, 0, NULL) != 2) {
382 syslog(LOG_ERR, "%s: remote delivery deferred:"
383 " RCPT TO failed: %m", it->queueid);
384 return (1);
385 }
f67beddd
MS
386
387 send_remote_command(fd, "DATA");
7b68d8ae 388 if (read_remote(fd, 0, NULL) != 3) {
f67beddd
MS
389 syslog(LOG_ERR, "%s: remote delivery deferred:"
390 " DATA failed: %m", it->queueid);
391 return (1);
392 }
393
394 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
395 syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %m",
396 it->queueid);
397 return (1);
398 }
399
400 while (!feof(it->queuef)) {
401 if (fgets(line, sizeof(line), it->queuef) == NULL)
402 break;
403 linelen = strlen(line);
404 if (linelen == 0 || line[linelen - 1] != '\n') {
405 syslog(LOG_CRIT, "%s: remote delivery failed:"
406 "corrupted queue file", it->queueid);
407 *errmsg = "corrupted queue file";
408 error = -1;
409 goto out;
410 }
411
412 /* Remove trailing \n's and escape leading dots */
413 trim_line(line);
414
415 /*
416 * If the first character is a dot, we escape it so the line
417 * length increases
418 */
419 if (line[0] == '.')
420 linelen++;
421
422 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
423 syslog(LOG_ERR, "%s: remote delivery deferred: "
424 "write error", it->queueid);
425 error = 1;
426 goto out;
427 }
428 }
429
430 send_remote_command(fd, ".");
7b68d8ae 431 if (read_remote(fd, 0, NULL) != 2) {
f67beddd
MS
432 syslog(LOG_ERR, "%s: remote delivery deferred: %m",
433 it->queueid);
434 return (1);
435 }
436
437 send_remote_command(fd, "QUIT");
7b68d8ae 438 if (read_remote(fd, 0, NULL) != 2) {
f67beddd
MS
439 syslog(LOG_ERR, "%s: remote delivery deferred: "
440 "QUIT failed: %m", it->queueid);
441 return (1);
442 }
443out:
444
445 close(fd);
446 return (error);
447}
448