]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
Avoid losing mail when connection to relayhost fails
authorBen Schmidt <none@none>
Mon, 15 Nov 2010 00:12:50 +0000 (11:12 +1100)
committerBen Schmidt <none@none>
Mon, 15 Nov 2010 00:12:50 +0000 (11:12 +1100)
ChangeLog
src/init_sockfd.c
src/mlmmj-send.c

index f1891f2cd312fb6f6c330cb11b38c53299478416..8f76c404adb99f4acfdc175a90acf089388a71e3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
index 9a2f00451d9369095564828dd93cc238acc77a5f..b96bec1137cee292bc66230bf80833100dfba9e2 100644 (file)
@@ -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 @@ void init_sockfd(int *sockfd, const char *relayhost, unsigned short port)
        *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;
index 0907908831b48bc0a9adbac13517ec9816aeb3f2..1e52ab980b16348fc039b2708cb27de71f25f669 100644 (file)
@@ -250,6 +250,9 @@ int send_mail(int sockfd, const char *from, const char *to,
        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",
@@ -378,9 +381,12 @@ int initsmtp(int *sockfd, const char *relayhost, unsigned short port)
        int retval = 0;
        char *reply = NULL;
        char *myhostname = hostnamestr();
-       
+
        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 send_mail_verp(int sockfd, struct strlist *addrs, char *mailmap,
        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,24 +1176,33 @@ int main(int argc, char **argv)
                
                if(verp) {
                        initsmtp(&sockfd, relay, smtpport);
-                       if(write_mail_from(sockfd, verpfrom, verp)) {
+                       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 {
+                           } 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;
                                }
+                           }
+                           /* 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;
                        }
-                       /* We can't be in SMTP DATA state or anything like
-                        * that, so should be able to safely QUIT. */
-                       endsmtp(&sockfd);
                }
 
                while((dp = readdir(subddir)) != NULL) {