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.6 2008/04/20 13:44:24 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 */
58 extern struct config
*config
;
59 extern struct authusers authusers
;
60 static jmp_buf timeout_alarm
;
63 sig_alarm(int signo __unused
)
65 longjmp(timeout_alarm
, 1);
69 send_remote_command(int fd
, const char* fmt
, ...)
76 vsprintf(cmd
, fmt
, va
);
78 if (((config
->features
& SECURETRANS
) != 0) &&
79 ((config
->features
& NOSSL
) == 0)) {
80 len
= SSL_write(config
->ssl
, (const char*)cmd
, strlen(cmd
));
81 SSL_write(config
->ssl
, "\r\n", 2);
84 len
= write(fd
, cmd
, strlen(cmd
));
98 int done
= 0, status
= 0;
100 if (signal(SIGALRM
, sig_alarm
) == SIG_ERR
) {
101 syslog(LOG_ERR
, "SIGALRM error: %m");
103 if (setjmp(timeout_alarm
) != 0) {
104 syslog(LOG_ERR
, "Timeout reached");
110 * Remote reading code from femail.c written by Henning Brauer of
111 * OpenBSD and released under a BSD style license.
113 for (len
= pos
= 0; !done
; ) {
115 (pos
> 0 && memchr(buff
+ pos
, '\n', len
- pos
) == NULL
)) {
116 memmove(buff
, buff
+ pos
, len
- pos
);
119 if (((config
->features
& SECURETRANS
) != 0) &&
120 (config
->features
& NOSSL
) == 0) {
121 if ((rlen
= SSL_read(config
->ssl
, buff
+ len
,
122 sizeof(buff
) - len
)) == -1)
125 if ((rlen
= read(fd
, buff
+ len
,
126 sizeof(buff
) - len
)) == -1)
131 for (; pos
< len
&& buff
[pos
] >= '0' && buff
[pos
] <= '9'; pos
++)
137 if (buff
[pos
] == ' ')
139 else if (buff
[pos
] != '-')
140 syslog(LOG_ERR
, "invalid syntax in reply from server");
143 for (; pos
< len
&& buff
[pos
- 1] != '\n'; pos
++)
154 * Handle SMTP authentication
156 * XXX TODO: give me AUTH CRAM-MD5
159 smtp_login(struct qitem
*it
, int fd
, char *login
, char* password
)
164 /* Send AUTH command according to RFC 2554 */
165 send_remote_command(fd
, "AUTH LOGIN");
166 if (read_remote(fd
) != 3) {
167 syslog(LOG_ERR
, "%s: remote delivery deferred:"
168 " AUTH login not available: %m", it
->queueid
);
172 len
= base64_encode(login
, strlen(login
), &temp
);
176 send_remote_command(fd
, "%s", temp
);
177 if (read_remote(fd
) != 3) {
178 syslog(LOG_ERR
, "%s: remote delivery deferred:"
179 " AUTH login failed: %m", it
->queueid
);
183 len
= base64_encode(password
, strlen(password
), &temp
);
187 send_remote_command(fd
, "%s", temp
);
188 res
= read_remote(fd
);
190 syslog(LOG_ERR
, "%s: remote delivery failed:"
191 " Authentication failed: %m", it
->queueid
);
193 } else if (res
!= 2) {
194 syslog(LOG_ERR
, "%s: remote delivery failed:"
195 " AUTH password failed: %m", it
->queueid
);
203 open_connection(struct qitem
*it
, const char *host
)
205 struct addrinfo hints
, *res
, *res0
;
207 const char *errmsg
= NULL
;
208 int fd
, error
= 0, port
;
210 if (config
->port
!= 0)
215 /* Shamelessly taken from getaddrinfo(3) */
216 memset(&hints
, 0, sizeof(hints
));
217 hints
.ai_family
= PF_UNSPEC
;
218 hints
.ai_socktype
= SOCK_STREAM
;
219 hints
.ai_protocol
= IPPROTO_TCP
;
221 snprintf(servname
, sizeof(servname
), "%d", port
);
222 error
= getaddrinfo(host
, servname
, &hints
, &res0
);
224 syslog(LOG_ERR
, "%s: remote delivery deferred: "
225 "%s: %m", it
->queueid
, gai_strerror(error
));
229 for (res
= res0
; res
; res
= res
->ai_next
) {
230 fd
=socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
232 errmsg
= "socket failed";
235 if (connect(fd
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
236 errmsg
= "connect failed";
244 syslog(LOG_ERR
, "%s: remote delivery deferred: %s (%s:%s)",
245 it
->queueid
, errmsg
, host
, servname
);
254 deliver_remote(struct qitem
*it
, const char **errmsg
)
257 char *host
, line
[1000];
258 int fd
, error
= 0, do_auth
= 0, res
= 0;
261 host
= strrchr(it
->addr
, '@');
262 /* Should not happen */
266 /* Step over the @ */
269 /* Smarthost support? */
270 if (config
->smarthost
!= NULL
&& strlen(config
->smarthost
) > 0) {
271 syslog(LOG_INFO
, "%s: using smarthost (%s)",
272 it
->queueid
, config
->smarthost
);
273 host
= config
->smarthost
;
276 fd
= open_connection(it
, host
);
280 /* Check first reply from remote host */
281 config
->features
|= NOSSL
;
282 res
= read_remote(fd
);
284 syslog(LOG_INFO
, "%s: Invalid initial response: %i",
288 config
->features
&= ~NOSSL
;
291 if ((config
->features
& SECURETRANS
) != 0) {
292 error
= smtp_init_crypto(it
, fd
, config
->features
);
294 syslog(LOG_INFO
, "%s: SSL initialization successful",
301 * If the user doesn't want STARTTLS, but SSL encryption, we
302 * have to enable SSL first, then send EHLO
304 if (((config
->features
& STARTTLS
) == 0) &&
305 ((config
->features
& SECURETRANS
) != 0)) {
306 send_remote_command(fd
, "EHLO %s", hostname());
307 if (read_remote(fd
) != 2) {
308 syslog(LOG_ERR
, "%s: remote delivery deferred: "
309 " EHLO failed: %m", it
->queueid
);
313 #endif /* HAVE_CRYPTO */
314 if (((config
->features
& SECURETRANS
) == 0)) {
315 send_remote_command(fd
, "EHLO %s", hostname());
316 if (read_remote(fd
) != 2) {
317 syslog(LOG_ERR
, "%s: remote delivery deferred: "
318 " EHLO failed: %m", it
->queueid
);
324 * Use SMTP authentication if the user defined an entry for the remote
327 SLIST_FOREACH(a
, &authusers
, next
) {
328 if (strcmp(a
->host
, host
) == 0) {
336 * Check if the user wants plain text login without using
339 if ((config
->features
& INSECURE
) != 0) {
340 syslog(LOG_INFO
, "%s: Use SMTP authentication",
342 error
= smtp_login(it
, fd
, a
->login
, a
->password
);
344 syslog(LOG_ERR
, "%s: remote delivery failed:"
345 " SMTP login failed: %m", it
->queueid
);
348 /* SMTP login is not available, so try without */
350 syslog(LOG_ERR
, "%s: SMTP login not available."
351 " Try without", it
->queueid
);
353 syslog(LOG_ERR
, "%s: Skip SMTP login. ",
358 send_remote_command(fd
, "MAIL FROM:<%s>", it
->sender
);
359 if (read_remote(fd
) != 2) {
360 syslog(LOG_ERR
, "%s: remote delivery deferred:"
361 " MAIL FROM failed: %m", it
->queueid
);
366 * Iterate over all recepients and open only one connection
368 send_remote_command(fd
, "RCPT TO:<%s>", it
->addr
);
369 if (read_remote(fd
) != 2) {
370 syslog(LOG_ERR
, "%s: remote delivery deferred:"
371 " RCPT TO failed: %m", it
->queueid
);
375 send_remote_command(fd
, "DATA");
376 if (read_remote(fd
) != 3) {
377 syslog(LOG_ERR
, "%s: remote delivery deferred:"
378 " DATA failed: %m", it
->queueid
);
382 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0) {
383 syslog(LOG_ERR
, "%s: remote delivery deferred: cannot seek: %m",
388 while (!feof(it
->queuef
)) {
389 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
391 linelen
= strlen(line
);
392 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
393 syslog(LOG_CRIT
, "%s: remote delivery failed:"
394 "corrupted queue file", it
->queueid
);
395 *errmsg
= "corrupted queue file";
400 /* Remove trailing \n's and escape leading dots */
404 * If the first character is a dot, we escape it so the line
410 if (send_remote_command(fd
, "%s", line
) != (ssize_t
)linelen
+1) {
411 syslog(LOG_ERR
, "%s: remote delivery deferred: "
412 "write error", it
->queueid
);
418 send_remote_command(fd
, ".");
419 if (read_remote(fd
) != 2) {
420 syslog(LOG_ERR
, "%s: remote delivery deferred: %m",
425 send_remote_command(fd
, "QUIT");
426 if (read_remote(fd
) != 2) {
427 syslog(LOG_ERR
, "%s: remote delivery deferred: "
428 "QUIT failed: %m", it
->queueid
);