# HG changeset patch # User Ben Schmidt # Date 1289779970 -39600 # Node ID 563b513fae210c03ce78c0244051ce3a26c6de1f # Parent ddae562f7cf0359e357ea83a10ef3bb20b16de49 Avoid losing mail when connection to relayhost fails diff -r ddae562f7cf0 -r 563b513fae21 ChangeLog --- 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 diff -r ddae562f7cf0 -r 563b513fae21 src/init_sockfd.c --- 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 #include #include +#include #include #include #include @@ -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; diff -r ddae562f7cf0 -r 563b513fae21 src/mlmmj-send.c --- 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) {