changeset 781: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 wrap: on
line diff
--- a/ChangeLog	Mon Nov 15 10:11:28 2010 +1100
+++ b/ChangeLog	Mon Nov 15 11:12:50 2010 +1100
@@ -1,3 +1,4 @@
+ o Avoid losing mail when connecting to relayhost fails
  o Improved and more consistent closing of SMTP sessions in error cases
  o Check the relayhost gives a reply before reading it to avoid a crash
  o Avoid checking addresses multiple times for notmetoo and make it work even
--- a/src/init_sockfd.c	Mon Nov 15 10:11:28 2010 +1100
+++ b/src/init_sockfd.c	Mon Nov 15 11:12:50 2010 +1100
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <unistd.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
@@ -45,16 +46,17 @@
 	*sockfd = socket(PF_INET, SOCK_STREAM, 0);
 	if(*sockfd == -1) {
 		log_error(LOG_ARGS, "Could not get socket");
-		exit(EXIT_FAILURE);
+		return;
 	}
 	addr.sin_family = PF_INET;
 	addr.sin_addr.s_addr = inet_addr(relayhost);
 	addr.sin_port = htons(port);
 	len = sizeof(addr);
 	if(connect(*sockfd, (struct sockaddr *)&addr, len) == -1) {
-		log_error(LOG_ARGS, "Could not connect to %s, "
-				    "exiting ... ", relayhost);
-		exit(EXIT_FAILURE);
+		log_error(LOG_ARGS, "Could not connect to %s", relayhost);
+		close(*sockfd);
+		*sockfd = -1;
+		return;
 	}
 
 	on = 1;
--- a/src/mlmmj-send.c	Mon Nov 15 10:11:28 2010 +1100
+++ b/src/mlmmj-send.c	Mon Nov 15 11:12:50 2010 +1100
@@ -250,6 +250,9 @@
 	int retval = 0;
 	char *reply, *reply2, *tohdr;
 
+	if(sockfd == -1)
+		return EBADF;
+
 	if(strchr(to, '@') == NULL) {
 		errno = 0;
 		log_error(LOG_ARGS, "No @ in address, ignoring %s",
@@ -381,6 +384,9 @@
 	
 	init_sockfd(sockfd, relayhost, port);
 	
+	if(*sockfd == -1)
+		return EBADF;
+
 	if((reply = checkwait_smtpreply(*sockfd, MLMMJ_CONNECT)) != NULL) {
 		log_error(LOG_ARGS, "No proper greeting to our connect"
 			  "Reply: [%s]", reply);
@@ -433,6 +439,9 @@
 	int retval, i;
 	char *reply, *reply2;
 
+	if(sockfd == -1)
+		return EBADF;
+
 	retval = write_mail_from(sockfd, from, verpextra);
 	if(retval) {
 		log_error(LOG_ARGS, "Could not write MAIL FROM\n");
@@ -1167,16 +1176,18 @@
 		
 		if(verp) {
 			initsmtp(&sockfd, relay, smtpport);
+			if(sockfd > -1) {
 			if(write_mail_from(sockfd, verpfrom, verp)) {
 				log_error(LOG_ARGS,
-						"Could not write MAIL FROM\n");
+					    "Could not write VERP MAIL FROM. "
+					    "Not sending with VERP.");
 				verp = NULL;
 			} else {
 				reply = checkwait_smtpreply(sockfd, MLMMJ_FROM);
 				if(reply) {
 					log_error(LOG_ARGS,
 						"Mailserver did not "
-						"accept verp mail from. "
+						"accept VERP MAIL FROM. "
 						"Not sending with VERP.");
 					myfree(reply);
 					verp = NULL;
@@ -1185,6 +1196,13 @@
 			/* We can't be in SMTP DATA state or anything like
 			 * that, so should be able to safely QUIT. */
 			endsmtp(&sockfd);
+			} else {
+			    log_error(LOG_ARGS,
+				    "Could not connect to "
+				    "write VERP MAIL FROM. "
+				    "Not sending with VERP.");
+			    verp = NULL;
+			}
 		}
 
 		while((dp = readdir(subddir)) != NULL) {