Filter out zero-length MX records
authorArt Cancro <ajc@citadel.org>
Wed, 10 Feb 2021 16:55:43 +0000 (11:55 -0500)
committerArt Cancro <ajc@citadel.org>
Wed, 10 Feb 2021 16:55:43 +0000 (11:55 -0500)
citadel/domain.c
citadel/modules/smtp/serv_smtpclient.c

index 25a4d7a..3c4f145 100644 (file)
@@ -155,7 +155,6 @@ int getmx(char *mxbuf, char *dest) {
                }
        
                while(1) {
                }
        
                while(1) {
-                       TRACE;
                        memset(expanded_buf, 0, sizeof(expanded_buf));
                        ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
                        if (ret < 0) break;
                        memset(expanded_buf, 0, sizeof(expanded_buf));
                        ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
                        if (ret < 0) break;
@@ -165,7 +164,6 @@ int getmx(char *mxbuf, char *dest) {
                        ptr += INT16SZ + INT32SZ;
                        GETSHORT(n, ptr);
        
                        ptr += INT16SZ + INT32SZ;
                        GETSHORT(n, ptr);
        
-                       syslog(LOG_DEBUG, "\033[35mgetmx: found record of type %d and length %d\033[0m", type, n);
                        if (type != T_MX) {
                                ptr += n;
                        }
                        if (type != T_MX) {
                                ptr += n;
                        }
@@ -175,16 +173,20 @@ int getmx(char *mxbuf, char *dest) {
                                ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
                                ptr += ret;
        
                                ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
                                ptr += ret;
        
-                               ++num_mxrecs;
-                               if (mxrecs == NULL) {
-                                       mxrecs = malloc(sizeof(struct mx));
-                               }
-                               else {
-                                       mxrecs = realloc(mxrecs, (sizeof(struct mx) * num_mxrecs) );
-                               }
+                               // If there are no MX records for the domain, resolv will give us a single one with zero length.
+                               // Make sure we only record actual MX records and not the blank.
+                               if (strlen(expanded_buf) > 0) {
+                                       ++num_mxrecs;
+                                       if (mxrecs == NULL) {
+                                               mxrecs = malloc(sizeof(struct mx));
+                                       }
+                                       else {
+                                               mxrecs = realloc(mxrecs, (sizeof(struct mx) * num_mxrecs) );
+                                       }
        
        
-                               mxrecs[num_mxrecs - 1].pref = pref;
-                               strcpy(mxrecs[num_mxrecs - 1].host, expanded_buf);
+                                       mxrecs[num_mxrecs - 1].pref = pref;
+                                       strcpy(mxrecs[num_mxrecs - 1].host, expanded_buf);
+                               }
                        }
                }
        }
                        }
                }
        }
@@ -196,7 +198,6 @@ int getmx(char *mxbuf, char *dest) {
 
        strcpy(mxbuf, "");
        for (n=0; n<num_mxrecs; ++n) {
 
        strcpy(mxbuf, "");
        for (n=0; n<num_mxrecs; ++n) {
-               syslog(LOG_DEBUG, "\033[35mgetmx: %d : <%s>\033[0m", n, mxrecs[n].host);
                strcat(mxbuf, mxrecs[n].host);
                strcat(mxbuf, "|");
        }
                strcat(mxbuf, mxrecs[n].host);
                strcat(mxbuf, "|");
        }
index e31581f..e18fe56 100644 (file)
@@ -300,7 +300,7 @@ int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from, char *res
                                 try_this_mx, CtdlGetConfigStr("c_fqdn")
                            );
                        curl_easy_setopt(curl, CURLOPT_URL, smtp_url);
                                 try_this_mx, CtdlGetConfigStr("c_fqdn")
                            );
                        curl_easy_setopt(curl, CURLOPT_URL, smtp_url);
-                       syslog(LOG_DEBUG, "smtpclient: trying mx %d of %d <%s>", i, num_mx, smtp_url);  // send the message
+                       syslog(LOG_DEBUG, "smtpclient: trying MX %d of %d <%s>", i+1, num_mx, smtp_url);        // send the message
                        res = curl_easy_perform(curl);
                        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
                        syslog(LOG_DEBUG,
                        res = curl_easy_perform(curl);
                        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
                        syslog(LOG_DEBUG,