Transmit the proper host name (set from c_fqdn) in EHLO command when we talk to a...
[citadel.git] / citadel / modules / smtp / serv_smtpclient.c
index 62dfbb30b53c96b0f3ce1d3a712232f98085e306..dd2c7c9e9b0678f9a1a2b9148d33889e6a0b7def 100644 (file)
@@ -198,6 +198,7 @@ int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from)
        char node[1024];
        char name[1024];
        char try_this_mx[256];
+       char smtp_url[512];
        int i;
 
        syslog(LOG_DEBUG, "smtpclient: smtp_attempt_delivery(%ld, %s)", msgid, recp);
@@ -238,19 +239,22 @@ int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from)
                        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);                                           // Time out after 20 seconds
                        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
                        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
-
-                       strcpy(try_this_mx, "smtp://");
-                       extract_token(&try_this_mx[7], mxes, i, '|', (sizeof try_this_mx - 7));
-                       if (
-                               (!strncasecmp(try_this_mx, HKEY("smtp://smtp://")))                             // This can happen if the administrator
-                               || (!strncasecmp(try_this_mx, HKEY("smtp://smtps://")))                         // puts a full smtp[s] URI as the smart-host
-                       ) {
-                               strcpy(try_this_mx, &try_this_mx[7]);
-                       }
-
-                       curl_easy_setopt(curl, CURLOPT_URL, try_this_mx);
-
-                       syslog(LOG_DEBUG, "smtpclient: trying %s", try_this_mx);                        // send the message
+                       // curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_buffer);
+
+                       // Construct an SMTP URL in the form of:
+                       //      smtp[s]://target_host/source_host
+                       // This looks weird but libcurl uses that last part to set our name for EHLO or HELO.
+                       // We check for "smtp://" and "smtps://" because the admin may have put those prefixes in a smart-host entry.
+                       // If there is no prefix we add "smtp://"
+                       extract_token(try_this_mx, mxes, i, '|', (sizeof try_this_mx - 7));
+                       snprintf(smtp_url, sizeof smtp_url,
+                               "%s%s/%s",
+                               (((!strncasecmp(try_this_mx, HKEY("smtp://"))) || (!strncasecmp(try_this_mx, HKEY("smtps://")))) ? "" : "smtp://"),
+                               try_this_mx,
+                               CtdlGetConfigStr("c_fqdn")
+                       );
+                       curl_easy_setopt(curl, CURLOPT_URL, smtp_url);
+                       syslog(LOG_DEBUG, "smtpclient: trying %s", smtp_url);                           // send the message
                        res = curl_easy_perform(curl);
                        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
                        syslog(LOG_DEBUG, "smtpclient: libcurl returned %d (%s) , SMTP response %ld",
@@ -261,8 +265,10 @@ int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from)
                                response_code = 421;
                        }
 
-               curl_slist_free_all(recipients);
-               curl_easy_cleanup(curl);
+                       curl_slist_free_all(recipients);
+                       recipients = NULL;                                                              // this gets reused; avoid double-free
+                       curl_easy_cleanup(curl);
+                       curl = NULL;                                                                    // this gets reused; avoid double-free
                }
        }
 
@@ -286,11 +292,9 @@ void smtp_process_one_msg(long qmsgnum)
        long deletes[2];
        int delete_this_queue = 0;
 
-       syslog(LOG_DEBUG, "smtpclient: processing queue entry %ld", qmsgnum);
-
        msg = CtdlFetchMessage(qmsgnum, 1, 1);
        if (msg == NULL) {
-               syslog(LOG_WARNING, "smtpclient: queue message %ld does not exist", qmsgnum);
+               syslog(LOG_WARNING, "smtpclient: %ld does not exist", qmsgnum);
                return;
        }
 
@@ -326,7 +330,7 @@ void smtp_process_one_msg(long qmsgnum)
                if (!strncasecmp(cfgline, HKEY("envelope_from|")))      envelope_from = strdup(&cfgline[14]);
        }
 
-       int should_try_now = 1;
+       int should_try_now = 0;
        if (attempted < submitted) {                            // If no attempts have been made yet, try now
                should_try_now = 1;
        }
@@ -342,7 +346,7 @@ void smtp_process_one_msg(long qmsgnum)
        }
 
        if (should_try_now) {
-               syslog(LOG_DEBUG, "smtpclient: attempting delivery now");
+               syslog(LOG_DEBUG, "smtpclient: %ld attempting delivery now", qmsgnum);
                StrBuf *NewInstr = NewStrBuf();
                StrBufAppendPrintf(NewInstr, "Content-type: "SPOOLMIME"\n\n");
                StrBufAppendPrintf(NewInstr, "msgid|%ld\n", msgid);
@@ -412,7 +416,7 @@ void smtp_process_one_msg(long qmsgnum)
                }
        
                if (delete_this_queue) {
-                       syslog(LOG_DEBUG, "smtpclient: deleting this queue entry");
+                       syslog(LOG_DEBUG, "smtpclient: %ld deleting", qmsgnum);
                        deletes[0] = qmsgnum;
                        deletes[1] = msgid;
                        CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, deletes, 2, "");
@@ -420,7 +424,7 @@ void smtp_process_one_msg(long qmsgnum)
                }
                else {
                        // replace the old queue entry with the new one
-                       syslog(LOG_DEBUG, "smtpclient: rewriting this queue entry");
+                       syslog(LOG_DEBUG, "smtpclient: %ld rewriting", qmsgnum);
                        msg = convert_internet_message_buf(&NewInstr);                  // This function will free NewInstr for us
                        CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, 0);
                        CM_Free(msg);
@@ -428,7 +432,7 @@ void smtp_process_one_msg(long qmsgnum)
                }
        }
        else {
-               syslog(LOG_DEBUG, "smtpclient: retry time not reached");
+               syslog(LOG_DEBUG, "smtpclient: %ld retry time not reached", qmsgnum);
        }
 
        if (bounceto != NULL)           free(bounceto);