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.3 2008/02/04 08:58:54 matthias 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
& TLSINIT
) == 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
));
93 read_remote_command(int fd
, char *buff
)
97 if (signal(SIGALRM
, sig_alarm
) == SIG_ERR
) {
98 syslog(LOG_ERR
, "SIGALRM error: %m");
100 if (setjmp(timeout_alarm
) != 0) {
101 syslog(LOG_ERR
, "Timeout reached");
107 * According to RFC 821 a reply can consists of multiple lines, so
108 * so read until the 4th char of the reply code is != '-'
110 if (((config
->features
& SECURETRANS
) != 0) &&
111 ((config
->features
& TLSINIT
) == 0))
113 len
= SSL_read(config
->ssl
, buff
, BUF_SIZE
);
114 } while (len
> 3 && buff
[3] == '-');
117 len
= read(fd
, buff
, BUF_SIZE
);
118 } while (len
> 3 && buff
[3] == '-');
126 check_for_smtp_error(int fd
, char *buff
)
128 if (read_remote_command(fd
, buff
) < 0)
131 /* We received a 5XX reply thus an error happend */
132 if (strncmp(buff
, "5", 1) == 0) {
133 syslog(LOG_ERR
, "SMTP error : %s", buff
);
140 * Handle SMTP authentication
142 * XXX TODO: give me AUTH CRAM-MD5
145 smtp_login(struct qitem
*it
, int fd
, char *login
, char* password
)
151 /* Send AUTH command according to RFC 2554 */
152 send_remote_command(fd
, "AUTH LOGIN");
153 if (check_for_smtp_error(fd
, buf
) < 0) {
154 syslog(LOG_ERR
, "%s: remote delivery deferred:"
155 " AUTH login not available: %m", it
->queueid
);
159 len
= base64_encode(login
, strlen(login
), &temp
);
163 send_remote_command(fd
, "%s", temp
);
164 if (check_for_smtp_error(fd
, buf
) < 0) {
165 syslog(LOG_ERR
, "%s: remote delivery deferred:"
166 " AUTH login failed: %m", it
->queueid
);
170 len
= base64_encode(password
, strlen(password
), &temp
);
174 send_remote_command(fd
, "%s", temp
);
175 if (check_for_smtp_error(fd
, buf
) < 0) {
176 syslog(LOG_ERR
, "%s: remote delivery deferred:"
177 " AUTH password failed: %m", it
->queueid
);
185 open_connection(struct qitem
*it
, const char *host
)
187 struct addrinfo hints
, *res
, *res0
;
189 const char *errmsg
= NULL
;
190 int fd
, error
= 0, port
;
192 if (config
->port
!= 0)
197 /* Shamelessly taken from getaddrinfo(3) */
198 memset(&hints
, 0, sizeof(hints
));
199 hints
.ai_family
= PF_UNSPEC
;
200 hints
.ai_socktype
= SOCK_STREAM
;
201 hints
.ai_protocol
= IPPROTO_TCP
;
203 snprintf(servname
, sizeof(servname
), "%d", port
);
204 error
= getaddrinfo(host
, servname
, &hints
, &res0
);
206 syslog(LOG_ERR
, "%s: remote delivery deferred: "
207 "%s: %m", it
->queueid
, gai_strerror(error
));
211 for (res
= res0
; res
; res
= res
->ai_next
) {
212 fd
=socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
214 errmsg
= "socket failed";
217 if (connect(fd
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
218 errmsg
= "connect failed";
226 syslog(LOG_ERR
, "%s: remote delivery deferred: %s (%s:%s)",
227 it
->queueid
, errmsg
, host
, servname
);
236 deliver_remote(struct qitem
*it
, const char **errmsg
)
239 char *host
, buf
[2048], line
[1000];
240 int fd
, error
= 0, do_auth
= 0;
243 host
= strrchr(it
->addr
, '@');
244 /* Should not happen */
248 /* Step over the @ */
251 /* Smarthost support? */
252 if (config
->smarthost
!= NULL
&& strlen(config
->smarthost
) > 0) {
253 syslog(LOG_INFO
, "%s: using smarthost (%s)",
254 it
->queueid
, config
->smarthost
);
255 host
= config
->smarthost
;
258 fd
= open_connection(it
, host
);
263 if ((config
->features
& SECURETRANS
) != 0) {
264 error
= smtp_init_crypto(it
, fd
, config
->features
);
266 syslog(LOG_INFO
, "%s: SSL initialization sucessful",
273 * If the user doesn't want STARTTLS, but SSL encryption, we
274 * have to enable SSL first, then send EHLO
276 if (((config
->features
& STARTTLS
) == 0) &&
277 ((config
->features
& SECURETRANS
) != 0)) {
278 send_remote_command(fd
, "EHLO %s", hostname());
279 if (check_for_smtp_error(fd
, buf
) < 0) {
280 syslog(LOG_ERR
, "%s: remote delivery deferred: "
281 " EHLO failed: %m", it
->queueid
);
285 #endif /* HAVE_CRYPTO */
286 if (((config
->features
& SECURETRANS
) == 0)) {
287 send_remote_command(fd
, "EHLO %s", hostname());
288 if (check_for_smtp_error(fd
, buf
) < 0) {
289 syslog(LOG_ERR
, "%s: remote delivery deferred: "
290 " EHLO failed: %m", it
->queueid
);
296 * Use SMTP authentication if the user defined an entry for the remote
299 SLIST_FOREACH(a
, &authusers
, next
) {
300 if (strcmp(a
->host
, host
) == 0) {
307 syslog(LOG_INFO
, "%s: Use SMTP authentication", it
->queueid
);
308 error
= smtp_login(it
, fd
, a
->login
, a
->password
);
310 syslog(LOG_ERR
, "%s: remote delivery failed:"
311 " SMTP login failed: %m", it
->queueid
);
314 /* SMTP login is not available, so try without */
316 syslog(LOG_ERR
, "%s: SMTP login not available. Try without",
320 send_remote_command(fd
, "MAIL FROM:<%s>", it
->sender
);
321 if (check_for_smtp_error(fd
, buf
) < 0) {
322 syslog(LOG_ERR
, "%s: remote delivery deferred:"
323 " MAIL FROM failed: %m", it
->queueid
);
328 * Iterate over all recepients and open only one connection
330 send_remote_command(fd
, "RCPT TO:<%s>", it
->addr
);
331 if (check_for_smtp_error(fd
, buf
) < 0) {
332 syslog(LOG_ERR
, "%s: remote delivery deferred:"
333 " RCPT TO failed: %m", it
->queueid
);
337 send_remote_command(fd
, "DATA");
338 if (check_for_smtp_error(fd
, buf
) < 0) {
339 syslog(LOG_ERR
, "%s: remote delivery deferred:"
340 " DATA failed: %m", it
->queueid
);
344 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0) {
345 syslog(LOG_ERR
, "%s: remote delivery deferred: cannot seek: %m",
350 while (!feof(it
->queuef
)) {
351 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
353 linelen
= strlen(line
);
354 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
355 syslog(LOG_CRIT
, "%s: remote delivery failed:"
356 "corrupted queue file", it
->queueid
);
357 *errmsg
= "corrupted queue file";
362 /* Remove trailing \n's and escape leading dots */
366 * If the first character is a dot, we escape it so the line
372 if (send_remote_command(fd
, "%s", line
) != (ssize_t
)linelen
+1) {
373 syslog(LOG_ERR
, "%s: remote delivery deferred: "
374 "write error", it
->queueid
);
380 send_remote_command(fd
, ".");
381 if (check_for_smtp_error(fd
, buf
) < 0) {
382 syslog(LOG_ERR
, "%s: remote delivery deferred: %m",
387 send_remote_command(fd
, "QUIT");
388 if (check_for_smtp_error(fd
, buf
) < 0) {
389 syslog(LOG_ERR
, "%s: remote delivery deferred: "
390 "QUIT failed: %m", it
->queueid
);