]>
git.ipfire.org Git - people/ms/dma.git/blob - net.c
38d86146546fbc87c2c13b3f07d26761afa020d9
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
36 #include <sys/param.h>
37 #include <sys/queue.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
44 #include <openssl/ssl.h>
45 #include <openssl/err.h>
57 static jmp_buf timeout_alarm
;
58 char neterr
[BUF_SIZE
];
66 while ((nerr
= ERR_get_error()) != 0)
69 return (ERR_error_string(oerr
, NULL
));
73 sig_alarm(int signo __unused
)
75 longjmp(timeout_alarm
, 1);
79 send_remote_command(int fd
, const char* fmt
, ...)
88 s
= vsnprintf(cmd
, sizeof(cmd
) - 2, fmt
, va
);
90 if (s
== sizeof(cmd
) - 2 || s
< 0) {
91 strcpy(neterr
, "Internal error: oversized command string");
95 /* We *know* there are at least two more bytes available */
99 if (((config
->features
& SECURETRANS
) != 0) &&
100 ((config
->features
& NOSSL
) == 0)) {
101 while ((s
= SSL_write(config
->ssl
, (const char*)cmd
, len
)) <= 0) {
102 s
= SSL_get_error(config
->ssl
, s
);
103 if (s
!= SSL_ERROR_WANT_READ
&&
104 s
!= SSL_ERROR_WANT_WRITE
) {
105 strncpy(neterr
, ssl_errstr(), sizeof(neterr
));
113 n
= write(fd
, cmd
+ pos
, len
- pos
);
124 read_remote(int fd
, int extbufsize
, char *extbuf
)
129 int done
= 0, status
= 0, extbufpos
= 0;
131 if (signal(SIGALRM
, sig_alarm
) == SIG_ERR
) {
132 snprintf(neterr
, sizeof(neterr
), "SIGALRM error: %s",
136 if (setjmp(timeout_alarm
) != 0) {
137 snprintf(neterr
, sizeof(neterr
), "Timeout reached");
143 * Remote reading code from femail.c written by Henning Brauer of
144 * OpenBSD and released under a BSD style license.
146 for (len
= pos
= 0; !done
; ) {
149 (pos
> 0 && memchr(buff
+ pos
, '\n', len
- pos
) == NULL
)) {
150 memmove(buff
, buff
+ pos
, len
- pos
);
153 if (((config
->features
& SECURETRANS
) != 0) &&
154 (config
->features
& NOSSL
) == 0) {
155 if ((rlen
= SSL_read(config
->ssl
, buff
+ len
,
156 sizeof(buff
) - len
)) == -1) {
157 strncpy(neterr
, ssl_errstr(), sizeof(neterr
));
161 if ((rlen
= read(fd
, buff
+ len
, sizeof(buff
) - len
)) == -1) {
162 strncpy(neterr
, strerror(errno
), sizeof(neterr
));
169 * If there is an external buffer with a size bigger than zero
170 * and as long as there is space in the external buffer and
171 * there are new characters read from the mailserver
172 * copy them to the external buffer
174 if (extbufpos
<= (extbufsize
- 1) && rlen
&& extbufsize
> 0
176 /* do not write over the bounds of the buffer */
177 if(extbufpos
+ rlen
> (extbufsize
- 1)) {
178 rlen
= extbufsize
- extbufpos
;
180 memcpy(extbuf
+ extbufpos
, buff
+ len
- rlen
, rlen
);
183 for (; pos
< len
&& buff
[pos
] >= '0' && buff
[pos
] <= '9'; pos
++)
189 if (buff
[pos
] == ' ') {
191 } else if (buff
[pos
] != '-') {
192 strcpy(neterr
, "invalid syntax in reply from server");
197 for (; pos
< len
&& buff
[pos
- 1] != '\n'; pos
++)
204 while (len
> 0 && (buff
[len
- 1] == '\r' || buff
[len
- 1] == '\n'))
206 snprintf(neterr
, sizeof(neterr
), "%s", buff
);
212 * Handle SMTP authentication
215 smtp_login(int fd
, char *login
, char* password
)
220 res
= smtp_auth_md5(fd
, login
, password
);
223 } else if (res
== -2) {
225 * If the return code is -2, then then the login attempt failed,
226 * do not try other login mechanisms
231 if ((config
->features
& INSECURE
) != 0 ||
232 (config
->features
& SECURETRANS
) != 0) {
233 /* Send AUTH command according to RFC 2554 */
234 send_remote_command(fd
, "AUTH LOGIN");
235 if (read_remote(fd
, 0, NULL
) != 3) {
236 syslog(LOG_NOTICE
, "remote delivery deferred:"
237 " AUTH login not available: %s",
242 len
= base64_encode(login
, strlen(login
), &temp
);
245 syslog(LOG_ERR
, "can not encode auth reply: %m");
249 send_remote_command(fd
, "%s", temp
);
251 res
= read_remote(fd
, 0, NULL
);
253 syslog(LOG_NOTICE
, "remote delivery %s: AUTH login failed: %s",
254 res
== 5 ? "failed" : "deferred", neterr
);
255 return (res
== 5 ? -1 : 1);
258 len
= base64_encode(password
, strlen(password
), &temp
);
262 send_remote_command(fd
, "%s", temp
);
264 res
= read_remote(fd
, 0, NULL
);
266 syslog(LOG_NOTICE
, "remote delivery %s: Authentication failed: %s",
267 res
== 5 ? "failed" : "deferred", neterr
);
268 return (res
== 5 ? -1 : 1);
271 syslog(LOG_WARNING
, "non-encrypted SMTP login is disabled in config, so skipping it. ");
279 open_connection(const char *host
)
281 struct addrinfo hints
, *res
, *res0
;
283 const char *errmsg
= NULL
;
284 int fd
, error
= 0, port
;
286 if (config
->port
!= 0)
291 /* XXX FIXME get MX record of host */
292 /* Shamelessly taken from getaddrinfo(3) */
293 memset(&hints
, 0, sizeof(hints
));
294 hints
.ai_family
= PF_UNSPEC
;
295 hints
.ai_socktype
= SOCK_STREAM
;
296 hints
.ai_protocol
= IPPROTO_TCP
;
298 snprintf(servname
, sizeof(servname
), "%d", port
);
299 error
= getaddrinfo(host
, servname
, &hints
, &res0
);
301 syslog(LOG_NOTICE
, "remote delivery deferred: %s", gai_strerror(error
));
305 for (res
= res0
; res
; res
= res
->ai_next
) {
306 fd
=socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
308 errmsg
= "socket failed";
311 if (connect(fd
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
312 errmsg
= "connect failed";
320 syslog(LOG_NOTICE
, "remote delivery deferred: %s (%s:%s)",
321 errmsg
, host
, servname
);
330 close_connection(int fd
)
332 if (((config
->features
& SECURETRANS
) != 0) &&
333 ((config
->features
& NOSSL
) == 0))
334 SSL_shutdown(config
->ssl
);
336 if (config
->ssl
!= NULL
)
337 SSL_free(config
->ssl
);
343 deliver_remote(struct qitem
*it
, const char **errmsg
)
346 char *host
, line
[1000];
347 int fd
, error
= 0, do_auth
= 0, res
= 0;
349 /* asprintf can't take const */
350 void *errmsgc
= __DECONST(char **, errmsg
);
352 if (fseek(it
->mailf
, 0, SEEK_SET
) != 0) {
353 asprintf(errmsgc
, "can not seek: %s", strerror(errno
));
357 host
= strrchr(it
->addr
, '@');
358 /* Should not happen */
360 asprintf(errmsgc
, "Internal error: badly formed address %s",
364 /* Step over the @ */
368 /* Smarthost support? */
369 if (config
->smarthost
!= NULL
&& strlen(config
->smarthost
) > 0) {
370 syslog(LOG_INFO
, "using smarthost (%s:%i)",
371 config
->smarthost
, config
->port
);
372 host
= config
->smarthost
;
375 fd
= open_connection(host
);
379 /* Check first reply from remote host */
380 config
->features
|= NOSSL
;
381 res
= read_remote(fd
, 0, NULL
);
383 syslog(LOG_WARNING
, "Invalid initial response: %i", res
);
386 config
->features
&= ~NOSSL
;
388 if ((config
->features
& SECURETRANS
) != 0) {
389 error
= smtp_init_crypto(fd
, config
->features
);
391 syslog(LOG_DEBUG
, "SSL initialization successful");
396 /* XXX allow HELO fallback */
397 /* XXX record ESMTP keywords */
398 send_remote_command(fd
, "EHLO %s", hostname());
399 if (read_remote(fd
, 0, NULL
) != 2) {
400 syslog(LOG_WARNING
, "remote delivery deferred: EHLO failed: %s", neterr
);
401 asprintf(errmsgc
, "%s did not like our EHLO:\n%s",
407 * Use SMTP authentication if the user defined an entry for the remote
410 SLIST_FOREACH(a
, &authusers
, next
) {
411 if (strcmp(a
->host
, host
) == 0) {
419 * Check if the user wants plain text login without using
422 syslog(LOG_INFO
, "using SMTP authentication");
423 error
= smtp_login(fd
, a
->login
, a
->password
);
425 syslog(LOG_ERR
, "remote delivery failed:"
426 " SMTP login failed: %m");
427 asprintf(errmsgc
, "SMTP login to %s failed", host
);
430 /* SMTP login is not available, so try without */
431 else if (error
> 0) {
432 syslog(LOG_WARNING
, "SMTP login not available. Trying without.");
436 #define READ_REMOTE_CHECK(c, exp) \
437 res = read_remote(fd, 0, NULL); \
439 syslog(LOG_ERR, "remote delivery failed: " \
440 c " failed: %s", neterr); \
441 asprintf(errmsgc, "%s did not like our " c ":\n%s", \
444 } else if (res != exp) { \
445 syslog(LOG_NOTICE, "remote delivery deferred: " \
446 c " failed: %s", neterr); \
450 /* XXX send ESMTP ENVID, RET (FULL/HDRS) and 8BITMIME */
451 send_remote_command(fd
, "MAIL FROM:<%s>", it
->sender
);
452 READ_REMOTE_CHECK("MAIL FROM", 2);
454 /* XXX send ESMTP ORCPT */
455 send_remote_command(fd
, "RCPT TO:<%s>", it
->addr
);
456 READ_REMOTE_CHECK("RCPT TO", 2);
458 send_remote_command(fd
, "DATA");
459 READ_REMOTE_CHECK("DATA", 3);
462 while (!feof(it
->mailf
)) {
463 if (fgets(line
, sizeof(line
), it
->mailf
) == NULL
)
465 linelen
= strlen(line
);
466 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
467 syslog(LOG_CRIT
, "remote delivery failed: corrupted queue file");
468 *errmsg
= "corrupted queue file";
473 /* Remove trailing \n's and escape leading dots */
477 * If the first character is a dot, we escape it so the line
483 if (send_remote_command(fd
, "%s", line
) != (ssize_t
)linelen
+1) {
484 syslog(LOG_NOTICE
, "remote delivery deferred: write error");
490 send_remote_command(fd
, ".");
491 READ_REMOTE_CHECK("final DATA", 2);
493 send_remote_command(fd
, "QUIT");
494 if (read_remote(fd
, 0, NULL
) != 2)
495 syslog(LOG_INFO
, "remote delivery succeeded but QUIT failed: %s", neterr
);
498 close_connection(fd
);