mlmmj

changeset 563b513fae21

Avoid losing mail when connection to relayhost fails
author Ben Schmidt
date Mon, 15 Nov 2010 11:12:50 +1100
parents ddae562f7cf0
children 1311f31713ba
files ChangeLog src/init_sockfd.c src/mlmmj-send.c
diffstat 3 files changed, 34 insertions(+), 13 deletions(-) [+]
line diff
     1.1 --- a/ChangeLog	Mon Nov 15 10:11:28 2010 +1100
     1.2 +++ b/ChangeLog	Mon Nov 15 11:12:50 2010 +1100
     1.3 @@ -1,3 +1,4 @@
     1.4 + o Avoid losing mail when connecting to relayhost fails
     1.5   o Improved and more consistent closing of SMTP sessions in error cases
     1.6   o Check the relayhost gives a reply before reading it to avoid a crash
     1.7   o Avoid checking addresses multiple times for notmetoo and make it work even
     2.1 --- a/src/init_sockfd.c	Mon Nov 15 10:11:28 2010 +1100
     2.2 +++ b/src/init_sockfd.c	Mon Nov 15 11:12:50 2010 +1100
     2.3 @@ -24,6 +24,7 @@
     2.4  #include <stdlib.h>
     2.5  #include <sys/types.h>
     2.6  #include <sys/socket.h>
     2.7 +#include <unistd.h>
     2.8  #include <netinet/in.h>
     2.9  #include <arpa/inet.h>
    2.10  #include <string.h>
    2.11 @@ -45,16 +46,17 @@
    2.12  	*sockfd = socket(PF_INET, SOCK_STREAM, 0);
    2.13  	if(*sockfd == -1) {
    2.14  		log_error(LOG_ARGS, "Could not get socket");
    2.15 -		exit(EXIT_FAILURE);
    2.16 +		return;
    2.17  	}
    2.18  	addr.sin_family = PF_INET;
    2.19  	addr.sin_addr.s_addr = inet_addr(relayhost);
    2.20  	addr.sin_port = htons(port);
    2.21  	len = sizeof(addr);
    2.22  	if(connect(*sockfd, (struct sockaddr *)&addr, len) == -1) {
    2.23 -		log_error(LOG_ARGS, "Could not connect to %s, "
    2.24 -				    "exiting ... ", relayhost);
    2.25 -		exit(EXIT_FAILURE);
    2.26 +		log_error(LOG_ARGS, "Could not connect to %s", relayhost);
    2.27 +		close(*sockfd);
    2.28 +		*sockfd = -1;
    2.29 +		return;
    2.30  	}
    2.31  
    2.32  	on = 1;
     3.1 --- a/src/mlmmj-send.c	Mon Nov 15 10:11:28 2010 +1100
     3.2 +++ b/src/mlmmj-send.c	Mon Nov 15 11:12:50 2010 +1100
     3.3 @@ -250,6 +250,9 @@
     3.4  	int retval = 0;
     3.5  	char *reply, *reply2, *tohdr;
     3.6  
     3.7 +	if(sockfd == -1)
     3.8 +		return EBADF;
     3.9 +
    3.10  	if(strchr(to, '@') == NULL) {
    3.11  		errno = 0;
    3.12  		log_error(LOG_ARGS, "No @ in address, ignoring %s",
    3.13 @@ -381,6 +384,9 @@
    3.14  	
    3.15  	init_sockfd(sockfd, relayhost, port);
    3.16  	
    3.17 +	if(*sockfd == -1)
    3.18 +		return EBADF;
    3.19 +
    3.20  	if((reply = checkwait_smtpreply(*sockfd, MLMMJ_CONNECT)) != NULL) {
    3.21  		log_error(LOG_ARGS, "No proper greeting to our connect"
    3.22  			  "Reply: [%s]", reply);
    3.23 @@ -433,6 +439,9 @@
    3.24  	int retval, i;
    3.25  	char *reply, *reply2;
    3.26  
    3.27 +	if(sockfd == -1)
    3.28 +		return EBADF;
    3.29 +
    3.30  	retval = write_mail_from(sockfd, from, verpextra);
    3.31  	if(retval) {
    3.32  		log_error(LOG_ARGS, "Could not write MAIL FROM\n");
    3.33 @@ -1167,16 +1176,18 @@
    3.34  		
    3.35  		if(verp) {
    3.36  			initsmtp(&sockfd, relay, smtpport);
    3.37 +			if(sockfd > -1) {
    3.38  			if(write_mail_from(sockfd, verpfrom, verp)) {
    3.39  				log_error(LOG_ARGS,
    3.40 -						"Could not write MAIL FROM\n");
    3.41 +					    "Could not write VERP MAIL FROM. "
    3.42 +					    "Not sending with VERP.");
    3.43  				verp = NULL;
    3.44  			} else {
    3.45  				reply = checkwait_smtpreply(sockfd, MLMMJ_FROM);
    3.46  				if(reply) {
    3.47  					log_error(LOG_ARGS,
    3.48  						"Mailserver did not "
    3.49 -						"accept verp mail from. "
    3.50 +						"accept VERP MAIL FROM. "
    3.51  						"Not sending with VERP.");
    3.52  					myfree(reply);
    3.53  					verp = NULL;
    3.54 @@ -1185,6 +1196,13 @@
    3.55  			/* We can't be in SMTP DATA state or anything like
    3.56  			 * that, so should be able to safely QUIT. */
    3.57  			endsmtp(&sockfd);
    3.58 +			} else {
    3.59 +			    log_error(LOG_ARGS,
    3.60 +				    "Could not connect to "
    3.61 +				    "write VERP MAIL FROM. "
    3.62 +				    "Not sending with VERP.");
    3.63 +			    verp = NULL;
    3.64 +			}
    3.65  		}
    3.66  
    3.67  		while((dp = readdir(subddir)) != NULL) {