2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
35 * $DragonFly: src/libexec/dma/net.c,v 1.9 2008/09/30 17:47:21 swildner Exp $
38 #include <sys/param.h>
39 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <openssl/ssl.h>
48 #endif /* HAVE_CRYPTO */
59 extern struct config
*config
;
60 extern struct authusers authusers
;
61 static jmp_buf timeout_alarm
;
64 sig_alarm(int signo __unused
)
66 longjmp(timeout_alarm
, 1);
70 send_remote_command(int fd
, const char* fmt
, ...)
77 vsprintf(cmd
, fmt
, va
);
79 if (((config
->features
& SECURETRANS
) != 0) &&
80 ((config
->features
& NOSSL
) == 0)) {
81 len
= SSL_write(config
->ssl
, (const char*)cmd
, strlen(cmd
));
82 SSL_write(config
->ssl
, "\r\n", 2);
85 len
= write(fd
, cmd
, strlen(cmd
));
94 read_remote(int fd
, int extbufsize
, char *extbuf
)
99 int done
= 0, status
= 0, extbufpos
= 0;
101 if (signal(SIGALRM
, sig_alarm
) == SIG_ERR
) {
102 syslog(LOG_ERR
, "SIGALRM error: %m");
104 if (setjmp(timeout_alarm
) != 0) {
105 syslog(LOG_ERR
, "Timeout reached");
111 * Remote reading code from femail.c written by Henning Brauer of
112 * OpenBSD and released under a BSD style license.
114 for (len
= pos
= 0; !done
; ) {
117 (pos
> 0 && memchr(buff
+ pos
, '\n', len
- pos
) == NULL
)) {
118 memmove(buff
, buff
+ pos
, len
- pos
);
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)
127 if ((rlen
= read(fd
, buff
+ len
,
128 sizeof(buff
) - len
)) == -1)
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
139 if (extbufpos
<= (extbufsize
- 1) && rlen
&& extbufsize
> 0
141 /* do not write over the bounds of the buffer */
142 if(extbufpos
+ rlen
> (extbufsize
- 1)) {
143 rlen
= extbufsize
- extbufpos
;
145 memcpy(extbuf
+ extbufpos
, buff
+ len
- rlen
, rlen
);
148 for (; pos
< len
&& buff
[pos
] >= '0' && buff
[pos
] <= '9'; pos
++)
154 if (buff
[pos
] == ' ')
156 else if (buff
[pos
] != '-')
157 syslog(LOG_ERR
, "invalid syntax in reply from server");
160 for (; pos
< len
&& buff
[pos
- 1] != '\n'; pos
++)
171 * Handle SMTP authentication
174 smtp_login(struct qitem
*it
, int fd
, char *login
, char* password
)
180 res
= smtp_auth_md5(it
, fd
, login
, password
);
183 } else if (res
== -2) {
185 * If the return code is -2, then then the login attempt failed,
186 * do not try other login mechanisms
190 #endif /* HAVE_CRYPTO */
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
);
201 len
= base64_encode(login
, strlen(login
), &temp
);
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
);
212 len
= base64_encode(password
, strlen(password
), &temp
);
216 send_remote_command(fd
, "%s", temp
);
217 res
= read_remote(fd
, 0, NULL
);
219 syslog(LOG_ERR
, "%s: remote delivery failed:"
220 " Authentication failed: %m", it
->queueid
);
222 } else if (res
!= 2) {
223 syslog(LOG_ERR
, "%s: remote delivery failed:"
224 " AUTH password failed: %m", it
->queueid
);
228 syslog(LOG_ERR
, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ",
237 open_connection(struct qitem
*it
, const char *host
)
239 struct addrinfo hints
, *res
, *res0
;
241 const char *errmsg
= NULL
;
242 int fd
, error
= 0, port
;
244 if (config
->port
!= 0)
249 /* FIXME get MX record of host */
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
;
256 snprintf(servname
, sizeof(servname
), "%d", port
);
257 error
= getaddrinfo(host
, servname
, &hints
, &res0
);
259 syslog(LOG_ERR
, "%s: remote delivery deferred: "
260 "%s: %m", it
->queueid
, gai_strerror(error
));
264 for (res
= res0
; res
; res
= res
->ai_next
) {
265 fd
=socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
267 errmsg
= "socket failed";
270 if (connect(fd
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
271 errmsg
= "connect failed";
279 syslog(LOG_ERR
, "%s: remote delivery deferred: %s (%s:%s)",
280 it
->queueid
, errmsg
, host
, servname
);
289 deliver_remote(struct qitem
*it
, const char **errmsg
)
292 char *host
, line
[1000];
293 int fd
, error
= 0, do_auth
= 0, res
= 0;
296 host
= strrchr(it
->addr
, '@');
297 /* Should not happen */
301 /* Step over the @ */
304 /* Smarthost support? */
305 if (config
->smarthost
!= NULL
&& strlen(config
->smarthost
) > 0) {
306 syslog(LOG_INFO
, "%s: using smarthost (%s:%i)",
307 it
->queueid
, config
->smarthost
, config
->port
);
308 host
= config
->smarthost
;
311 fd
= open_connection(it
, host
);
315 /* Check first reply from remote host */
316 config
->features
|= NOSSL
;
317 res
= read_remote(fd
, 0, NULL
);
319 syslog(LOG_INFO
, "%s: Invalid initial response: %i",
323 config
->features
&= ~NOSSL
;
326 if ((config
->features
& SECURETRANS
) != 0) {
327 error
= smtp_init_crypto(it
, fd
, config
->features
);
329 syslog(LOG_INFO
, "%s: SSL initialization successful",
336 * If the user doesn't want STARTTLS, but SSL encryption, we
337 * have to enable SSL first, then send EHLO
339 if (((config
->features
& STARTTLS
) == 0) &&
340 ((config
->features
& SECURETRANS
) != 0)) {
341 send_remote_command(fd
, "EHLO %s", hostname());
342 if (read_remote(fd
, 0, NULL
) != 2) {
343 syslog(LOG_ERR
, "%s: remote delivery deferred: "
344 " EHLO failed: %m", it
->queueid
);
348 #endif /* HAVE_CRYPTO */
349 if (((config
->features
& SECURETRANS
) == 0)) {
350 send_remote_command(fd
, "EHLO %s", hostname());
351 if (read_remote(fd
, 0, NULL
) != 2) {
352 syslog(LOG_ERR
, "%s: remote delivery deferred: "
353 " EHLO failed: %m", it
->queueid
);
359 * Use SMTP authentication if the user defined an entry for the remote
362 SLIST_FOREACH(a
, &authusers
, next
) {
363 if (strcmp(a
->host
, host
) == 0) {
371 * Check if the user wants plain text login without using
374 syslog(LOG_INFO
, "%s: Use SMTP authentication",
376 error
= smtp_login(it
, fd
, a
->login
, a
->password
);
378 syslog(LOG_ERR
, "%s: remote delivery failed:"
379 " SMTP login failed: %m", it
->queueid
);
382 /* SMTP login is not available, so try without */
384 syslog(LOG_ERR
, "%s: SMTP login not available."
385 " Try without", it
->queueid
);
388 send_remote_command(fd
, "MAIL FROM:<%s>", it
->sender
);
389 if (read_remote(fd
, 0, NULL
) != 2) {
390 syslog(LOG_ERR
, "%s: remote delivery deferred:"
391 " MAIL FROM failed: %m", it
->queueid
);
395 send_remote_command(fd
, "RCPT TO:<%s>", it
->addr
);
396 if (read_remote(fd
, 0, NULL
) != 2) {
397 syslog(LOG_ERR
, "%s: remote delivery deferred:"
398 " RCPT TO failed: %m", it
->queueid
);
402 send_remote_command(fd
, "DATA");
403 if (read_remote(fd
, 0, NULL
) != 3) {
404 syslog(LOG_ERR
, "%s: remote delivery deferred:"
405 " DATA failed: %m", it
->queueid
);
409 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0) {
410 syslog(LOG_ERR
, "%s: remote delivery deferred: cannot seek: %m",
415 while (!feof(it
->queuef
)) {
416 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
418 linelen
= strlen(line
);
419 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
420 syslog(LOG_CRIT
, "%s: remote delivery failed:"
421 "corrupted queue file", it
->queueid
);
422 *errmsg
= "corrupted queue file";
427 /* Remove trailing \n's and escape leading dots */
431 * If the first character is a dot, we escape it so the line
437 if (send_remote_command(fd
, "%s", line
) != (ssize_t
)linelen
+1) {
438 syslog(LOG_ERR
, "%s: remote delivery deferred: "
439 "write error", it
->queueid
);
445 send_remote_command(fd
, ".");
446 if (read_remote(fd
, 0, NULL
) != 2) {
447 syslog(LOG_ERR
, "%s: remote delivery deferred: %m",
452 send_remote_command(fd
, "QUIT");
453 if (read_remote(fd
, 0, NULL
) != 2) {
454 syslog(LOG_ERR
, "%s: remote delivery deferred: "
455 "QUIT failed: %m", it
->queueid
);