EV: fix possible nullpointer access in last commit.
[citadel.git] / citadel / modules / smtp / serv_smtpeventclient.c
index c7de74fac3e9da7fffd65b10251b42c36a9411af..dd4e3e2662b08a05a10784a4e36ee4a26dda378f 100644 (file)
 #include "smtpqueue.h"
 #include "smtp_clienthandlers.h"
 
+ConstStr SMTPStates[] = {
+       {HKEY("looking up mx - record")},
+       {HKEY("evaluating what to do next")},
+       {HKEY("looking up a - record")},
+       {HKEY("looking up aaaa - record")},
+       {HKEY("connecting remote")},
+       {HKEY("smtp conversation ongoing")},
+       {HKEY("smtp sending maildata")},
+       {HKEY("smtp sending done")},
+       {HKEY("smtp successfully finished")},
+       {HKEY("failed one attempt")},
+       {HKEY("failed temporarily")},
+       {HKEY("failed permanently")}
+};
+
+void SetSMTPState(AsyncIO *IO, smtpstate State)
+{
+       CitContext* CCC = IO->CitContext;
+       if (CCC != NULL)
+               memcpy(CCC->cs_clientname, SMTPStates[State].Key, SMTPStates[State].len + 1);
+}
+
 int SMTPClientDebugEnabled = 0;
 void DeleteSmtpOutMsg(void *v)
 {
@@ -133,13 +155,26 @@ eNextState FinalizeMessageSend_DB(AsyncIO *IO)
 {
        const char *Status;
        SmtpOutMsg *Msg = IO->Data;
-       
-       if (Msg->MyQEntry->Status == 2) 
+       StrBuf *StatusMessage;
+
+       if (Msg->MyQEntry->AllStatusMessages != NULL)
+               StatusMessage = Msg->MyQEntry->AllStatusMessages;
+       else
+               StatusMessage = Msg->MyQEntry->StatusMessage;
+
+
+       if (Msg->MyQEntry->Status == 2) {
+               SetSMTPState(IO, eSTMPfinished);
                Status = "Delivery successful.";
-       else if (Msg->MyQEntry->Status == 5) 
+       }
+       else if (Msg->MyQEntry->Status == 5) {
+               SetSMTPState(IO, eSMTPFailTotal);
                Status = "Delivery failed permanently; giving up.";
-       else
+       }
+       else {
+               SetSMTPState(IO, eSMTPFailTemporary);
                Status = "Delivery failed temporarily; will retry later.";
+       }
                        
        EVS_syslog(LOG_INFO,
                   "%s Time[%fs] Recipient <%s> @ <%s> (%s) Status message: %s\n",
@@ -148,14 +183,15 @@ eNextState FinalizeMessageSend_DB(AsyncIO *IO)
                   Msg->user,
                   Msg->node,
                   Msg->name,
-                  ChrPtr(Msg->MyQEntry->StatusMessage));
+                  ChrPtr(StatusMessage));
 
 
        Msg->IDestructQueItem = DecreaseQReference(Msg->MyQItem);
 
-       Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem);
+       Msg->nRemain = CountActiveQueueEntries(Msg->MyQItem, 0);
 
        if (Msg->MyQEntry->Active && 
+           !Msg->MyQEntry->StillActive &&
            CheckQEntryIsBounce(Msg->MyQEntry))
        {
                /* are we casue for a bounce mail? */
@@ -176,7 +212,7 @@ eNextState FinalizeMessageSend_DB(AsyncIO *IO)
        Msg->MyQItem->QueMsgID = -1;
 
        if (Msg->IDestructQueItem)
-               smtpq_do_bounce(Msg->MyQItem, Msg->msgtext);
+               smtpq_do_bounce(Msg->MyQItem, StatusMessage, Msg->msgtext, Msg->pCurrRelay);
 
        if (Msg->nRemain > 0)
        {
@@ -225,6 +261,7 @@ eNextState FailOneAttempt(AsyncIO *IO)
 {
        SmtpOutMsg *Msg = IO->Data;
 
+       SetSMTPState(IO, eSTMPfailOne);
        if (Msg->MyQEntry->Status == 2)
                return eAbort;
 
@@ -233,10 +270,25 @@ eNextState FailOneAttempt(AsyncIO *IO)
         * - connection timeout
         * - dns lookup failed
         */
-       StopClientWatchers(IO);
+       StopClientWatchers(IO, 1);
+
+       Msg->MyQEntry->nAttempt ++;
+       if (Msg->MyQEntry->AllStatusMessages == NULL)
+               Msg->MyQEntry->AllStatusMessages = NewStrBuf();
+
+       StrBufAppendPrintf(Msg->MyQEntry->AllStatusMessages, "%ld) ", Msg->MyQEntry->nAttempt);
+       StrBufAppendBuf(Msg->MyQEntry->AllStatusMessages, Msg->MyQEntry->StatusMessage, 0);
+       StrBufAppendBufPlain(Msg->MyQEntry->AllStatusMessages, HKEY("; "), 0);
 
        if (Msg->pCurrRelay != NULL)
                Msg->pCurrRelay = Msg->pCurrRelay->Next;
+       if ((Msg->pCurrRelay != NULL) &&
+           !Msg->pCurrRelay->IsRelay &&
+           Msg->MyQItem->HaveRelay)
+       {
+               EVS_syslog(LOG_DEBUG, "%s Aborting; last relay failed.\n", __FUNCTION__);
+               return eAbort;
+       }
 
        if (Msg->pCurrRelay == NULL) {
                EVS_syslog(LOG_DEBUG, "%s Aborting\n", __FUNCTION__);
@@ -302,6 +354,7 @@ void SetConnectStatus(AsyncIO *IO)
 eNextState mx_connect_ip(AsyncIO *IO)
 {
        SmtpOutMsg *Msg = IO->Data;
+       SetSMTPState(IO, eSTMPconnecting);
 
        EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
@@ -368,6 +421,7 @@ eNextState get_one_mx_host_ip_done(AsyncIO *IO)
                return mx_connect_ip(IO);
        }
        else {
+               SetSMTPState(IO, eSTMPfailOne);
                if (Msg->HostLookup.VParsedDNSReply != NULL) {
                        Msg->HostLookup.DNSReplyFree(Msg->HostLookup.VParsedDNSReply);
                        Msg->HostLookup.VParsedDNSReply = NULL;
@@ -385,6 +439,7 @@ eNextState get_one_mx_host_ip(AsyncIO *IO)
         * - the direct hostname if there was no mx record
         * - one of the mx'es
         */
+       SetSMTPState(IO, (Msg->pCurrRelay->IPv6)?eSTMPalookup:eSTMPaaaalookup);
 
        EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
@@ -501,6 +556,8 @@ eNextState resolve_mx_records(AsyncIO *IO)
 {
        SmtpOutMsg * Msg = IO->Data;
 
+       SetSMTPState(IO, eSTMPmxlookup);
+
        EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        /* start resolving MX records here. */
        if (!QueueQuery(ns_t_mx,
@@ -579,8 +636,10 @@ void smtp_try_one_queue_entry(OneQueItem *MyQItem,
        if (KeepMsgText) Msg->msgtext = MsgText;
        else             Msg->msgtext = NewStrBufDup(MsgText);
 
-       if (smtp_resolve_recipients(Msg)) {
-
+       if (smtp_resolve_recipients(Msg) &&
+           (!MyQItem->HaveRelay ||
+            (MyQItem->URL != NULL)))
+       {
                safestrncpy(
                        ((CitContext *)Msg->IO.CitContext)->cs_host,
                        Msg->node,
@@ -591,15 +650,19 @@ void smtp_try_one_queue_entry(OneQueItem *MyQItem,
                             Msg->MyQItem->MessageID,
                             ChrPtr(Msg->MyQEntry->Recipient),
                             ((CitContext*)Msg->IO.CitContext)->cs_pid);
-               if (Msg->pCurrRelay == NULL)
+               if (Msg->pCurrRelay == NULL) {
+                       SetSMTPState(&Msg->IO, eSTMPmxlookup);
                        QueueEventContext(&Msg->IO,
                                          resolve_mx_records);
+               }
                else { /* oh... via relay host */
                        if (Msg->pCurrRelay->IsIP) {
+                               SetSMTPState(&Msg->IO, eSTMPconnecting);
                                QueueEventContext(&Msg->IO,
                                                  mx_connect_ip);
                        }
                        else {
+                               SetSMTPState(&Msg->IO, eSTMPalookup);
                                /* uneducated admin has chosen to
                                   add DNS to the equation... */
                                QueueEventContext(&Msg->IO,
@@ -608,14 +671,16 @@ void smtp_try_one_queue_entry(OneQueItem *MyQItem,
                }
        }
        else {
+               SetSMTPState(&Msg->IO, eSMTPFailTotal);
                /* No recipients? well fail then. */
                if (Msg->MyQEntry != NULL) {
                        Msg->MyQEntry->Status = 5;
-                       StrBufPlain(Msg->MyQEntry->StatusMessage,
-                                   HKEY("Invalid Recipient!"));
+                       if (StrLength(Msg->MyQEntry->StatusMessage) == 0)
+                               StrBufPlain(Msg->MyQEntry->StatusMessage,
+                                           HKEY("Invalid Recipient!"));
                }
                FinalizeMessageSend_DB(&Msg->IO);
-               DeleteSmtpOutMsg(&Msg->IO);
+               DeleteSmtpOutMsg(Msg);
        }
 }
 
@@ -717,7 +782,9 @@ eNextState SMTP_C_Timeout(AsyncIO *IO)
 
        Msg->MyQEntry->Status = 4;
        EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
-       StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
+       StrBufPrintf(IO->ErrMsg, "Timeout: %s while talking to %s",
+                    ReadErrors[Msg->State].Key,
+                    Msg->mx_host);
        if (Msg->State > eRCPT)
                return eAbort;
        else
@@ -729,7 +796,10 @@ eNextState SMTP_C_ConnFail(AsyncIO *IO)
 
        Msg->MyQEntry->Status = 4;
        EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
-       StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[Msg->State]));
+       StrBufPrintf(IO->ErrMsg, "Connection failure: %s while talking to %s",
+                    ReadErrors[Msg->State].Key,
+                    Msg->mx_host);
+
        return FailOneAttempt(IO);
 }
 eNextState SMTP_C_DNSFail(AsyncIO *IO)
@@ -744,6 +814,28 @@ eNextState SMTP_C_Shutdown(AsyncIO *IO)
        EVS_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        SmtpOutMsg *Msg = IO->Data;
 
+       switch (IO->NextState) {
+       case eSendDNSQuery:
+       case eReadDNSReply:
+
+               /* todo: abort c-ares */
+       case eConnect:
+       case eSendReply:
+       case eSendMore:
+       case eSendFile:
+       case eReadMessage:
+       case eReadMore:
+       case eReadPayload:
+       case eReadFile:
+               StopClientWatchers(IO, 1);
+               break;
+       case eDBQuery:
+
+               break;
+       case eTerminateConnection:
+       case eAbort:
+               break;
+       }
        Msg->MyQEntry->Status = 3;
        StrBufPlain(Msg->MyQEntry->StatusMessage,
                    HKEY("server shutdown during message submit."));