]>
git.ipfire.org Git - people/ms/dma.git/blob - net.c
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
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>
46 #include <openssl/ssl.h>
47 #include <openssl/err.h>
60 char neterr
[BUF_SIZE
];
68 while ((nerr
= ERR_get_error()) != 0)
71 return (ERR_error_string(oerr
, NULL
));
75 send_remote_command(int fd
, const char* fmt
, ...)
84 s
= vsnprintf(cmd
, sizeof(cmd
) - 2, fmt
, va
);
86 if (s
== sizeof(cmd
) - 2 || s
< 0) {
87 strcpy(neterr
, "Internal error: oversized command string");
91 /* We *know* there are at least two more bytes available */
95 if (((config
.features
& SECURETRANS
) != 0) &&
96 ((config
.features
& NOSSL
) == 0)) {
97 while ((s
= SSL_write(config
.ssl
, (const char*)cmd
, len
)) <= 0) {
98 s
= SSL_get_error(config
.ssl
, s
);
99 if (s
!= SSL_ERROR_WANT_READ
&&
100 s
!= SSL_ERROR_WANT_WRITE
) {
101 strncpy(neterr
, ssl_errstr(), sizeof(neterr
));
109 n
= write(fd
, cmd
+ pos
, len
- pos
);
120 read_remote(int fd
, int extbufsize
, char *extbuf
)
123 size_t pos
, len
, copysize
;
125 int done
= 0, status
= 0, status_running
= 0, extbufpos
= 0;
126 enum { parse_status
, parse_spacedash
, parse_rest
} parsestate
;
128 if (do_timeout(CON_TIMEOUT
, 1) != 0) {
129 snprintf(neterr
, sizeof(neterr
), "Timeout reached");
134 * Remote reading code from femail.c written by Henning Brauer of
135 * OpenBSD and released under a BSD style license.
139 parsestate
= parse_status
;
141 while (!(done
&& parsestate
== parse_status
)) {
144 (pos
> 0 && memchr(buff
+ pos
, '\n', len
- pos
) == NULL
)) {
145 memmove(buff
, buff
+ pos
, len
- pos
);
148 if (((config
.features
& SECURETRANS
) != 0) &&
149 (config
.features
& NOSSL
) == 0) {
150 if ((rlen
= SSL_read(config
.ssl
, buff
+ len
, sizeof(buff
) - len
)) == -1) {
151 strncpy(neterr
, ssl_errstr(), sizeof(neterr
));
155 if ((rlen
= read(fd
, buff
+ len
, sizeof(buff
) - len
)) == -1) {
156 strncpy(neterr
, strerror(errno
), sizeof(neterr
));
162 copysize
= sizeof(neterr
) - strlen(neterr
) - 1;
165 strncat(neterr
, buff
, copysize
);
168 * If there is an external buffer with a size bigger than zero
169 * and as long as there is space in the external buffer and
170 * there are new characters read from the mailserver
171 * copy them to the external buffer
173 if (extbufpos
<= (extbufsize
- 1) && rlen
> 0 && extbufsize
> 0 && extbuf
!= NULL
) {
174 /* do not write over the bounds of the buffer */
175 if(extbufpos
+ rlen
> (extbufsize
- 1)) {
176 rlen
= extbufsize
- extbufpos
;
178 memcpy(extbuf
+ extbufpos
, buff
+ len
- rlen
, rlen
);
185 switch (parsestate
) {
187 for (; pos
< len
; pos
++) {
188 if (isdigit(buff
[pos
])) {
189 status_running
= status_running
* 10 + (buff
[pos
] - '0');
191 status
= status_running
;
193 parsestate
= parse_spacedash
;
199 case parse_spacedash
:
207 /* XXX read capabilities */
211 strcpy(neterr
, "invalid syntax in reply from server");
216 parsestate
= parse_rest
;
221 for (; pos
< len
; pos
++) {
222 if (buff
[pos
] == '\n') {
224 parsestate
= parse_status
;
234 /* chop off trailing newlines */
235 while (neterr
[0] != 0 && strchr("\r\n", neterr
[strlen(neterr
) - 1]) != 0)
236 neterr
[strlen(neterr
) - 1] = 0;
246 * Handle SMTP authentication
249 smtp_login(int fd
, char *login
, char* password
)
254 res
= smtp_auth_md5(fd
, login
, password
);
257 } else if (res
== -2) {
259 * If the return code is -2, then then the login attempt failed,
260 * do not try other login mechanisms
265 if ((config
.features
& INSECURE
) != 0 ||
266 (config
.features
& SECURETRANS
) != 0) {
267 /* Send AUTH command according to RFC 2554 */
268 send_remote_command(fd
, "AUTH LOGIN");
269 if (read_remote(fd
, 0, NULL
) != 3) {
270 syslog(LOG_NOTICE
, "remote delivery deferred:"
271 " AUTH login not available: %s",
276 len
= base64_encode(login
, strlen(login
), &temp
);
279 syslog(LOG_ERR
, "can not encode auth reply: %m");
283 send_remote_command(fd
, "%s", temp
);
285 res
= read_remote(fd
, 0, NULL
);
287 syslog(LOG_NOTICE
, "remote delivery %s: AUTH login failed: %s",
288 res
== 5 ? "failed" : "deferred", neterr
);
289 return (res
== 5 ? -1 : 1);
292 len
= base64_encode(password
, strlen(password
), &temp
);
296 send_remote_command(fd
, "%s", temp
);
298 res
= read_remote(fd
, 0, NULL
);
300 syslog(LOG_NOTICE
, "remote delivery %s: Authentication failed: %s",
301 res
== 5 ? "failed" : "deferred", neterr
);
302 return (res
== 5 ? -1 : 1);
305 syslog(LOG_WARNING
, "non-encrypted SMTP login is disabled in config, so skipping it. ");
313 open_connection(struct mx_hostentry
*h
)
317 syslog(LOG_INFO
, "trying remote delivery to %s [%s] pref %d",
318 h
->host
, h
->addr
, h
->pref
);
320 fd
= socket(h
->ai
.ai_family
, h
->ai
.ai_socktype
, h
->ai
.ai_protocol
);
322 syslog(LOG_INFO
, "socket for %s [%s] failed: %m",
327 if (connect(fd
, (struct sockaddr
*)&h
->sa
, h
->ai
.ai_addrlen
) < 0) {
328 syslog(LOG_INFO
, "connect to %s [%s] failed: %m",
338 close_connection(int fd
)
340 if (config
.ssl
!= NULL
) {
341 if (((config
.features
& SECURETRANS
) != 0) &&
342 ((config
.features
& NOSSL
) == 0))
343 SSL_shutdown(config
.ssl
);
344 SSL_free(config
.ssl
);
351 deliver_to_host(struct qitem
*it
, struct mx_hostentry
*host
, void *errmsgc
)
356 int fd
, error
= 0, do_auth
= 0, res
= 0;
358 if (fseek(it
->mailf
, 0, SEEK_SET
) != 0) {
359 asprintf(errmsgc
, "can not seek: %s", strerror(errno
));
363 fd
= open_connection(host
);
367 #define READ_REMOTE_CHECK(c, exp) \
368 res = read_remote(fd, 0, NULL); \
370 syslog(LOG_ERR, "remote delivery to %s [%s] failed after %s: %s", \
371 host->host, host->addr, c, neterr); \
372 asprintf(errmsgc, "%s [%s] did not like our %s:\n%s", \
373 host->host, host->addr, c, neterr); \
375 } else if (res != exp) { \
376 syslog(LOG_NOTICE, "remote delivery deferred: %s [%s] failed after %s: %s", \
377 host->host, host->addr, c, neterr); \
381 /* Check first reply from remote host */
382 config
.features
|= NOSSL
;
383 READ_REMOTE_CHECK("connect", 2);
385 config
.features
&= ~NOSSL
;
387 if ((config
.features
& SECURETRANS
) != 0) {
388 error
= smtp_init_crypto(fd
, config
.features
);
390 syslog(LOG_DEBUG
, "SSL initialization successful");
395 /* XXX allow HELO fallback */
396 /* XXX record ESMTP keywords */
397 send_remote_command(fd
, "EHLO %s", hostname());
398 READ_REMOTE_CHECK("EHLO", 2);
401 * Use SMTP authentication if the user defined an entry for the remote
404 SLIST_FOREACH(a
, &authusers
, next
) {
405 if (strcmp(a
->host
, host
->host
) == 0) {
413 * Check if the user wants plain text login without using
416 syslog(LOG_INFO
, "using SMTP authentication for user %s", a
->login
);
417 error
= smtp_login(fd
, a
->login
, a
->password
);
419 syslog(LOG_ERR
, "remote delivery failed:"
420 " SMTP login failed: %m");
421 asprintf(errmsgc
, "SMTP login to %s failed", host
->host
);
424 /* SMTP login is not available, so try without */
425 else if (error
> 0) {
426 syslog(LOG_WARNING
, "SMTP login not available. Trying without.");
430 /* XXX send ESMTP ENVID, RET (FULL/HDRS) and 8BITMIME */
431 send_remote_command(fd
, "MAIL FROM:<%s>", it
->sender
);
432 READ_REMOTE_CHECK("MAIL FROM", 2);
434 /* XXX send ESMTP ORCPT */
435 send_remote_command(fd
, "RCPT TO:<%s>", it
->addr
);
436 READ_REMOTE_CHECK("RCPT TO", 2);
438 send_remote_command(fd
, "DATA");
439 READ_REMOTE_CHECK("DATA", 3);
442 while (!feof(it
->mailf
)) {
443 if (fgets(line
, sizeof(line
), it
->mailf
) == NULL
)
445 linelen
= strlen(line
);
446 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
447 syslog(LOG_CRIT
, "remote delivery failed: corrupted queue file");
448 *(const char **)errmsgc
= "corrupted queue file";
453 /* Remove trailing \n's and escape leading dots */
457 * If the first character is a dot, we escape it so the line
463 if (send_remote_command(fd
, "%s", line
) != (ssize_t
)linelen
+1) {
464 syslog(LOG_NOTICE
, "remote delivery deferred: write error");
470 send_remote_command(fd
, ".");
471 READ_REMOTE_CHECK("final DATA", 2);
473 send_remote_command(fd
, "QUIT");
474 if (read_remote(fd
, 0, NULL
) != 2)
475 syslog(LOG_INFO
, "remote delivery succeeded but QUIT failed: %s", neterr
);
478 close_connection(fd
);
483 deliver_remote(struct qitem
*it
, const char **errmsg
)
485 /* asprintf can't take const */
486 void *errmsgc
= __DECONST(char **, errmsg
);
487 struct mx_hostentry
*hosts
, *h
;
490 int error
= 1, smarthost
= 0;
492 host
= strrchr(it
->addr
, '@');
493 /* Should not happen */
495 asprintf(errmsgc
, "Internal error: badly formed address %s",
499 /* Step over the @ */
505 /* Smarthost support? */
506 if (config
.smarthost
!= NULL
) {
507 host
= config
.smarthost
;
509 syslog(LOG_INFO
, "using smarthost (%s:%i)", host
, port
);
513 error
= dns_get_mx_list(host
, port
, &hosts
, smarthost
);
515 syslog(LOG_NOTICE
, "remote delivery %s: DNS failure (%s)",
516 error
< 0 ? "failed" : "deferred",
521 for (h
= hosts
; *h
->host
!= 0; h
++) {
522 switch (deliver_to_host(it
, h
, errmsgc
)) {