]>
git.ipfire.org Git - people/ms/dma.git/blob - net.c
2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/param.h>
40 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
47 #include <openssl/ssl.h>
48 #include <openssl/err.h>
61 char neterr
[ERRMSG_SIZE
];
69 while ((nerr
= ERR_get_error()) != 0)
72 return (ERR_error_string(oerr
, NULL
));
76 send_remote_command(int fd
, const char* fmt
, ...)
85 s
= vsnprintf(cmd
, sizeof(cmd
) - 2, fmt
, va
);
87 if (s
== sizeof(cmd
) - 2 || s
< 0) {
88 strcpy(neterr
, "Internal error: oversized command string");
92 /* We *know* there are at least two more bytes available */
96 if (((config
.features
& SECURETRANS
) != 0) &&
97 ((config
.features
& NOSSL
) == 0)) {
98 while ((s
= SSL_write(config
.ssl
, (const char*)cmd
, len
)) <= 0) {
99 s
= SSL_get_error(config
.ssl
, s
);
100 if (s
!= SSL_ERROR_WANT_READ
&&
101 s
!= SSL_ERROR_WANT_WRITE
) {
102 strncpy(neterr
, ssl_errstr(), sizeof(neterr
));
110 n
= write(fd
, cmd
+ pos
, len
- pos
);
121 read_remote(int fd
, int extbufsize
, char *extbuf
)
124 size_t pos
, len
, copysize
;
126 int done
= 0, status
= 0, status_running
= 0, extbufpos
= 0;
127 enum { parse_status
, parse_spacedash
, parse_rest
} parsestate
;
129 if (do_timeout(CON_TIMEOUT
, 1) != 0) {
130 snprintf(neterr
, sizeof(neterr
), "Timeout reached");
135 * Remote reading code from femail.c written by Henning Brauer of
136 * OpenBSD and released under a BSD style license.
140 parsestate
= parse_status
;
142 while (!(done
&& parsestate
== parse_status
)) {
145 (pos
> 0 && memchr(buff
+ pos
, '\n', len
- pos
) == NULL
)) {
146 memmove(buff
, buff
+ pos
, len
- pos
);
149 if (((config
.features
& SECURETRANS
) != 0) &&
150 (config
.features
& NOSSL
) == 0) {
151 if ((rlen
= SSL_read(config
.ssl
, buff
+ len
, sizeof(buff
) - len
)) == -1) {
152 strncpy(neterr
, ssl_errstr(), sizeof(neterr
));
156 if ((rlen
= read(fd
, buff
+ len
, sizeof(buff
) - len
)) == -1) {
157 strncpy(neterr
, strerror(errno
), sizeof(neterr
));
163 copysize
= sizeof(neterr
) - strlen(neterr
) - 1;
166 strncat(neterr
, buff
, copysize
);
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
> 0 && extbufsize
> 0 && extbuf
!= NULL
) {
175 /* do not write over the bounds of the buffer */
176 if(extbufpos
+ rlen
> (extbufsize
- 1)) {
177 rlen
= extbufsize
- extbufpos
;
179 memcpy(extbuf
+ extbufpos
, buff
+ len
- rlen
, rlen
);
186 switch (parsestate
) {
188 for (; pos
< len
; pos
++) {
189 if (isdigit(buff
[pos
])) {
190 status_running
= status_running
* 10 + (buff
[pos
] - '0');
192 status
= status_running
;
194 parsestate
= parse_spacedash
;
200 case parse_spacedash
:
208 /* XXX read capabilities */
212 strcpy(neterr
, "invalid syntax in reply from server");
217 parsestate
= parse_rest
;
222 for (; pos
< len
; pos
++) {
223 if (buff
[pos
] == '\n') {
225 parsestate
= parse_status
;
235 /* chop off trailing newlines */
236 while (neterr
[0] != 0 && strchr("\r\n", neterr
[strlen(neterr
) - 1]) != 0)
237 neterr
[strlen(neterr
) - 1] = 0;
247 * Handle SMTP authentication
250 smtp_login(int fd
, char *login
, char* password
, const struct smtp_features
* features
)
256 if (features
->auth
.cram_md5
) {
257 res
= smtp_auth_md5(fd
, login
, password
);
260 } else if (res
== -2) {
262 * If the return code is -2, then then the login attempt failed,
263 * do not try other login mechanisms
270 if (features
->auth
.login
) {
271 if ((config
.features
& INSECURE
) != 0 ||
272 (config
.features
& SECURETRANS
) != 0) {
273 /* Send AUTH command according to RFC 2554 */
274 send_remote_command(fd
, "AUTH LOGIN");
275 if (read_remote(fd
, 0, NULL
) != 3) {
276 syslog(LOG_NOTICE
, "remote delivery deferred:"
277 " AUTH login not available: %s",
282 len
= base64_encode(login
, strlen(login
), &temp
);
285 syslog(LOG_ERR
, "can not encode auth reply: %m");
289 send_remote_command(fd
, "%s", temp
);
291 res
= read_remote(fd
, 0, NULL
);
293 syslog(LOG_NOTICE
, "remote delivery %s: AUTH login failed: %s",
294 res
== 5 ? "failed" : "deferred", neterr
);
295 return (res
== 5 ? -1 : 1);
298 len
= base64_encode(password
, strlen(password
), &temp
);
302 send_remote_command(fd
, "%s", temp
);
304 res
= read_remote(fd
, 0, NULL
);
306 syslog(LOG_NOTICE
, "remote delivery %s: Authentication failed: %s",
307 res
== 5 ? "failed" : "deferred", neterr
);
308 return (res
== 5 ? -1 : 1);
311 syslog(LOG_WARNING
, "non-encrypted SMTP login is disabled in config, so skipping it. ");
320 open_connection(struct mx_hostentry
*h
)
324 syslog(LOG_INFO
, "trying remote delivery to %s [%s] pref %d",
325 h
->host
, h
->addr
, h
->pref
);
327 fd
= socket(h
->ai
.ai_family
, h
->ai
.ai_socktype
, h
->ai
.ai_protocol
);
329 syslog(LOG_INFO
, "socket for %s [%s] failed: %m",
334 if (connect(fd
, (struct sockaddr
*)&h
->sa
, h
->ai
.ai_addrlen
) < 0) {
335 syslog(LOG_INFO
, "connect to %s [%s] failed: %m",
345 close_connection(int fd
)
347 if (config
.ssl
!= NULL
) {
348 if (((config
.features
& SECURETRANS
) != 0) &&
349 ((config
.features
& NOSSL
) == 0))
350 SSL_shutdown(config
.ssl
);
351 SSL_free(config
.ssl
);
357 static void parse_auth_line(char* line
, struct smtp_auth_mechanisms
* auth
) {
358 // Skip the auth prefix
359 line
+= strlen("AUTH ");
361 char* method
= strtok(line
, " ");
363 if (strcmp(method
, "CRAM-MD5") == 0)
366 else if (strcmp(method
, "LOGIN") == 0)
369 method
= strtok(NULL
, " ");
373 int perform_server_greeting(int fd
, struct smtp_features
* features
) {
376 XXX allow HELO fallback
378 send_remote_command(fd
, "EHLO %s", hostname());
380 char buffer
[EHLO_RESPONSE_SIZE
];
381 memset(buffer
, 0, sizeof(buffer
));
383 int res
= read_remote(fd
, sizeof(buffer
) - 1, buffer
);
385 // Got an unexpected response
389 // Reset all features
390 memset(features
, 0, sizeof(*features
));
392 // Run through the buffer line by line
393 char linebuffer
[EHLO_RESPONSE_SIZE
];
397 char* line
= linebuffer
;
398 while (*p
&& *p
!= '\n') {
402 // p should never point to NULL after the loop
403 // above unless we reached the end of the buffer.
404 // In that case we will raise an error.
409 // Otherwise p points to the newline character which
413 // Terminte the string (and remove the carriage-return character)
417 // End main loop for empty lines
422 // - Must start with 250, followed by dash or space
423 // - We won't check for the correct usage of space and dash because
424 // that is already done in read_remote().
425 if ((strncmp(line
, "250-", 4) != 0) && (strncmp(line
, "250 ", 4) != 0)) {
426 syslog(LOG_ERR
, "Invalid line: %s\n", line
);
433 // Check for STARTTLS
434 if (strcmp(line
, "STARTTLS") == 0)
435 features
->starttls
= 1;
437 // Parse authentication mechanisms
438 else if (strncmp(line
, "AUTH ", 5) == 0)
439 parse_auth_line(line
, &features
->auth
);
442 syslog(LOG_DEBUG
, "Server greeting successfully completed");
445 if (features
->starttls
)
446 syslog(LOG_DEBUG
, " Server supports STARTTLS");
448 syslog(LOG_DEBUG
, " Server does not support STARTTLS");
451 if (features
->auth
.cram_md5
) {
452 syslog(LOG_DEBUG
, " Server supports CRAM-MD5 authentication");
454 if (features
->auth
.login
) {
455 syslog(LOG_DEBUG
, " Server supports LOGIN authentication");
462 deliver_to_host(struct qitem
*it
, struct mx_hostentry
*host
)
465 struct smtp_features features
;
468 int fd
, error
= 0, do_auth
= 0, res
= 0;
470 if (fseek(it
->mailf
, 0, SEEK_SET
) != 0) {
471 snprintf(errmsg
, sizeof(errmsg
), "can not seek: %s", strerror(errno
));
475 fd
= open_connection(host
);
479 #define READ_REMOTE_CHECK(c, exp) \
480 res = read_remote(fd, 0, NULL); \
482 syslog(LOG_ERR, "remote delivery to %s [%s] failed after %s: %s", \
483 host->host, host->addr, c, neterr); \
484 snprintf(errmsg, sizeof(errmsg), "%s [%s] did not like our %s:\n%s", \
485 host->host, host->addr, c, neterr); \
488 } else if (res != exp) { \
489 syslog(LOG_NOTICE, "remote delivery deferred: %s [%s] failed after %s: %s", \
490 host->host, host->addr, c, neterr); \
495 /* Check first reply from remote host */
496 if ((config
.features
& SECURETRANS
) == 0 ||
497 (config
.features
& STARTTLS
) != 0) {
498 config
.features
|= NOSSL
;
499 READ_REMOTE_CHECK("connect", 2);
501 config
.features
&= ~NOSSL
;
504 if ((config
.features
& SECURETRANS
) != 0) {
505 error
= smtp_init_crypto(fd
, config
.features
, &features
);
507 syslog(LOG_DEBUG
, "SSL initialization successful");
511 if ((config
.features
& STARTTLS
) == 0)
512 READ_REMOTE_CHECK("connect", 2);
516 if (perform_server_greeting(fd
, &features
) != 0) {
517 syslog(LOG_ERR
, "Could not perform server greeting at %s [%s]: %s",
518 host
->host
, host
->addr
, neterr
);
523 * Use SMTP authentication if the user defined an entry for the remote
526 SLIST_FOREACH(a
, &authusers
, next
) {
527 if (strcmp(a
->host
, host
->host
) == 0) {
535 * Check if the user wants plain text login without using
538 syslog(LOG_INFO
, "using SMTP authentication for user %s", a
->login
);
539 error
= smtp_login(fd
, a
->login
, a
->password
, &features
);
541 syslog(LOG_ERR
, "remote delivery failed:"
542 " SMTP login failed: %m");
543 snprintf(errmsg
, sizeof(errmsg
), "SMTP login to %s failed", host
->host
);
547 /* SMTP login is not available, so try without */
548 else if (error
> 0) {
549 syslog(LOG_WARNING
, "SMTP login not available. Trying without.");
553 /* XXX send ESMTP ENVID, RET (FULL/HDRS) and 8BITMIME */
554 send_remote_command(fd
, "MAIL FROM:<%s>", it
->sender
);
555 READ_REMOTE_CHECK("MAIL FROM", 2);
557 /* XXX send ESMTP ORCPT */
558 send_remote_command(fd
, "RCPT TO:<%s>", it
->addr
);
559 READ_REMOTE_CHECK("RCPT TO", 2);
561 send_remote_command(fd
, "DATA");
562 READ_REMOTE_CHECK("DATA", 3);
565 while (!feof(it
->mailf
)) {
566 if (fgets(line
, sizeof(line
), it
->mailf
) == NULL
)
568 linelen
= strlen(line
);
569 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
570 syslog(LOG_CRIT
, "remote delivery failed: corrupted queue file");
571 snprintf(errmsg
, sizeof(errmsg
), "corrupted queue file");
576 /* Remove trailing \n's and escape leading dots */
580 * If the first character is a dot, we escape it so the line
586 if (send_remote_command(fd
, "%s", line
) != (ssize_t
)linelen
+1) {
587 syslog(LOG_NOTICE
, "remote delivery deferred: write error");
593 send_remote_command(fd
, ".");
594 READ_REMOTE_CHECK("final DATA", 2);
596 send_remote_command(fd
, "QUIT");
597 if (read_remote(fd
, 0, NULL
) != 2)
598 syslog(LOG_INFO
, "remote delivery succeeded but QUIT failed: %s", neterr
);
601 close_connection(fd
);
606 deliver_remote(struct qitem
*it
)
608 struct mx_hostentry
*hosts
, *h
;
611 int error
= 1, smarthost
= 0;
615 /* Smarthost support? */
616 if (config
.smarthost
!= NULL
) {
617 host
= config
.smarthost
;
619 syslog(LOG_INFO
, "using smarthost (%s:%i)", host
, port
);
622 host
= strrchr(it
->addr
, '@');
623 /* Should not happen */
625 snprintf(errmsg
, sizeof(errmsg
), "Internal error: badly formed address %s",
629 /* Step over the @ */
634 error
= dns_get_mx_list(host
, port
, &hosts
, smarthost
);
636 snprintf(errmsg
, sizeof(errmsg
), "DNS lookup failure: host %s not found", host
);
637 syslog(LOG_NOTICE
, "remote delivery %s: DNS lookup failure: host %s not found",
638 error
< 0 ? "failed" : "deferred",
643 for (h
= hosts
; *h
->host
!= 0; h
++) {
644 switch (deliver_to_host(it
, h
)) {