]> Pileus Git - ~andy/fetchmail/blobdiff - checkalias.c
Minor bug fixes for socket.c
[~andy/fetchmail] / checkalias.c
index d3d1cb07b18bde7152697ae67028298089cd9b24..1e1d16339fe033fc082154c494d78f15b830e512 100644 (file)
@@ -5,21 +5,50 @@
  * For license terms, see the file COPYING in this directory.
  */
 #include "config.h"
-#ifdef HAVE_GETHOSTBYNAME
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
+#ifdef HAVE_NET_SOCKET_H
+#include <net/socket.h>
+#else
 #include <sys/socket.h>
+#endif
 #include <netinet/in.h>
+#ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
+#endif
 #include <netdb.h>
 #include "i18n.h"
 #include "mx.h"
 #include "fetchmail.h"
+#include "getaddrinfo.h"
 
 #define MX_RETRIES     3
 
+typedef unsigned char address_t[sizeof (struct in_addr)];
+
+#ifdef HAVE_RES_SEARCH
+static int getaddresses(struct addrinfo **result, const char *name)
+{
+    struct addrinfo hints;
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_socktype=SOCK_STREAM;
+    hints.ai_protocol=PF_UNSPEC;
+    hints.ai_family=AF_UNSPEC;
+    return fm_getaddrinfo(name, NULL, &hints, result);
+}
+
+/* XXX FIXME: doesn't detect if an IPv6-mapped IPv4 address
+ * matches a real IPv4 address */
+static int compareaddr(const struct addrinfo *a1, const struct addrinfo *a2)
+{
+    if (a1->ai_family != a2->ai_family) return FALSE;
+    if (a1->ai_addrlen != a2->ai_addrlen) return FALSE;
+    return (!memcmp(a1->ai_addr, a2->ai_addr, a1->ai_addrlen));
+}
+
 static int is_ip_alias(const char *name1,const char *name2)
 /*
  * Given two hostnames as arguments, returns TRUE if they
@@ -28,70 +57,46 @@ static int is_ip_alias(const char *name1,const char *name2)
  * the calling function does them.
  */
 {
-    typedef unsigned char address_t[sizeof (struct in_addr)]; 
-    typedef struct _address_e
-    {
-       struct _address_e *next;
-       address_t address;
-    } 
-    address_e;
-    address_e *host_a_addr=0, *host_b_addr=0;  /* assignments pacify -Wall */
-    address_e *dummy_addr;
-
-    int i;
-    struct hostent *hp;
-    char **p;
-    hp = gethostbyname(name1);
-    dummy_addr = (address_e *)NULL;
-
-    for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
-    {
-       struct in_addr in;
-       (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
-       xalloca(host_a_addr, address_e *, sizeof (address_e));
-       memset (host_a_addr,0, sizeof (address_e));
-       host_a_addr->next = dummy_addr;
-       (void) memcpy(&host_a_addr->address, *p, sizeof (in.s_addr));
-       dummy_addr = host_a_addr;
-    }
+    int rc = FALSE;
 
-    hp = gethostbyname(name2);
+    struct addrinfo *res1 = NULL, *res2 = NULL, *ii, *ij;
 
-    dummy_addr = (address_e *)NULL;
-    for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
-    {
-       struct in_addr in;
-       (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
-       xalloca(host_b_addr, address_e *, sizeof (address_e));
-       memset (host_b_addr,0, sizeof (address_e));
-       host_b_addr->next = dummy_addr;
-       (void) memcpy(&host_b_addr->address, *p, sizeof (in.s_addr));
-       dummy_addr = host_b_addr;
-    }
+    if (getaddresses(&res1, name1))
+       goto found;
 
-    while (host_a_addr)
-    {
-       while (host_b_addr)
-       {
-           if (!memcmp(host_b_addr->address,host_a_addr->address, sizeof (address_t)))
-               return (TRUE);
+    if (getaddresses(&res2, name2))
+       goto found;
 
-           host_b_addr = host_b_addr->next;
+    for (ii = res1 ; ii ; ii = ii -> ai_next) {
+       for (ij = res2 ; ij ; ij = ij -> ai_next) {
+           if (compareaddr(ii, ij)) {
+               rc = TRUE;
+               goto found;
+           }
        }
-       host_a_addr = host_a_addr->next;
     }
-    return (FALSE);
+
+found:
+    if (res2)
+       fm_freeaddrinfo(res2);
+    if (res1)
+       fm_freeaddrinfo(res1);
+    return rc;
 }
+#endif
 
-int is_host_alias(const char *name, struct query *ctl)
+int is_host_alias(const char *name, struct query *ctl, struct addrinfo **res)
 /* determine whether name is a DNS alias of the mailserver for this query */
 {
-    struct hostent     *he,*he_st;
+#ifdef HAVE_RES_SEARCH
     struct mxentry     *mxp, *mxrecords;
+    int                        e;
+    struct addrinfo    hints, *res_st;
+#endif
+    struct idlist      *idl;
+    size_t             namelen;
 
-    struct hostdata *lead_server = 
+    struct hostdata *lead_server =
        ctl->server.lead_server ? ctl->server.lead_server : &ctl->server;
 
     /*
@@ -99,7 +104,7 @@ int is_host_alias(const char *name, struct query *ctl)
      * many cases.
      *
      * (1) check against the `true name' deduced from the poll label
-     * and the via option (if present) at the beginning of the poll cycle.  
+     * and the via option (if present) at the beginning of the poll cycle.
      * Odds are good this will either be the mailserver's FQDN or a suffix of
      * it with the mailserver's domain's default host name omitted.
      *
@@ -115,10 +120,34 @@ int is_host_alias(const char *name, struct query *ctl)
        return(TRUE);
     else if (str_in_list(&lead_server->akalist, name, TRUE))
        return(TRUE);
-    else if (!ctl->server.dns)
-       return(FALSE);
 
+    /*
+     * Now check for a suffix match on the akalist.  The theory here is
+     * that if the user says `aka netaxs.com', we actually want to match
+     * foo.netaxs.com and bar.netaxs.com.
+     */
+    namelen = strlen(name);
+    for (idl = lead_server->akalist; idl; idl = idl->next)
+    {
+       const char      *ep;
+
+       /*
+        * Test is >= here because str_in_list() should have caught the
+        * equal-length case above.  Doing it this way guarantees that
+        * ep[-1] is a valid reference.
+        */
+       if (strlen(idl->id) >= namelen)
+           continue;
+       ep = name + (namelen - strlen(idl->id));
+       /* a suffix led by . must match */
+       if (ep[-1] == '.' && !strcasecmp(ep, idl->id))
+           return(TRUE);
+    }
+
+    if (!ctl->server.dns)
+       return(FALSE);
 #ifndef HAVE_RES_SEARCH
+    (void)res;
     return(FALSE);
 #else
     /*
@@ -130,43 +159,60 @@ int is_host_alias(const char *name, struct query *ctl)
      * delivering the current message or anything else from the
      * current server until it's back up.
      */
-    else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
+    memset(&hints, 0, sizeof hints);
+    hints.ai_family=AF_UNSPEC;
+    hints.ai_protocol=PF_UNSPEC;
+    hints.ai_socktype=SOCK_STREAM;
+    hints.ai_flags=AI_CANONNAME;
+
+    e = fm_getaddrinfo(name, NULL, &hints, res);
+    if (e == 0)
     {
-       if (strcasecmp(ctl->server.truename, he->h_name) == 0)
+       int rr = (strcasecmp(ctl->server.truename, (*res)->ai_canonname) == 0);
+       fm_freeaddrinfo(*res); *res = NULL;
+       if (rr)
            goto match;
-        else if (((he_st = gethostbyname(ctl->server.truename)) != (struct hostent *)NULL) && ctl->server.checkalias)
+        else if (ctl->server.checkalias && 0 == fm_getaddrinfo(ctl->server.truename, NULL, &hints, &res_st))
        {
+           fm_freeaddrinfo(res_st);
            if (outlevel >= O_DEBUG)
-               report(stdout, _("Checking if %s is really the same node as %s\n"),ctl->server.truename,name);
+               report(stdout, GT_("Checking if %s is really the same node as %s\n"),ctl->server.truename,name);
            if (is_ip_alias(ctl->server.truename,name) == TRUE)
            {
                if (outlevel >= O_DEBUG)
-                   report(stdout, _("Yes, their IP addresses match\n"));
+                   report(stdout, GT_("Yes, their IP addresses match\n"));
                goto match;
            }
            if (outlevel >= O_DEBUG)
-               report(stdout, _("No, their IP addresses don't match\n"));
-       }
-       else
+               report(stdout, GT_("No, their IP addresses don't match\n"));
+           return(FALSE);
+       } else {
            return(FALSE);
+       }
     }
     else
-       switch (h_errno)
+       switch (e)
        {
-       case HOST_NOT_FOUND:    /* specified host is unknown */
-       case NO_ADDRESS:        /* valid, but does not have an IP address */
-           break;
+           case EAI_NONAME:    /* specified host is unknown */
+#ifdef EAI_NODATA
+           /* EAI_NODATA was in the older RFC-2553, but that got
+            * obsoleted by RFC-3493 - there, EAI_NODATA is no longer
+            * valid, and modern operating systems no longer issue this
+            * error code. libesmtp/getaddrinfo.?, however, still does.
+            * (This was reported to Brian Stafford 2013-02-03.)
+            */
+           case EAI_NODATA:    /* specified host is unknown */
+#endif
+               break;
 
-       case NO_RECOVERY:       /* non-recoverable name server error */
-       case TRY_AGAIN:         /* temporary error on authoritative server */
-       default:
-           if (outlevel != O_SILENT)
-               report_complete(stdout, "\n");  /* terminate the progress message */
-           report(stderr,
-               _("nameserver failure while looking for `%s' during poll of %s.\n"),
-               name, ctl->server.pollname);
-           ctl->errcount++;
-           break;
+           default:
+               if (outlevel != O_SILENT)
+                   report_complete(stdout, "\n");      /* terminate the progress message */
+               report(stderr,
+                       GT_("nameserver failure while looking for '%s' during poll of %s: %s\n"),
+                       name, ctl->server.pollname, gai_strerror(e));
+               ctl->errcount++;
+               break;
        }
 
     /*
@@ -180,24 +226,23 @@ int is_host_alias(const char *name, struct query *ctl)
        switch (h_errno)
        {
        case HOST_NOT_FOUND:    /* specified host is unknown */
+#ifdef NO_ADDRESS
        case NO_ADDRESS:        /* valid, but does not have an IP address */
            return(FALSE);
-           break;
-
+#endif
        case NO_RECOVERY:       /* non-recoverable name server error */
        case TRY_AGAIN:         /* temporary error on authoritative server */
        default:
            report(stderr,
-               _("nameserver failure while looking for `%s' during poll of %s.\n"),
+               GT_("nameserver failure while looking for `%s' during poll of %s.\n"),
                name, ctl->server.pollname);
            ctl->errcount++;
            break;
        }
-    }
-    else
-    {
+    } else {
        for (mxp = mxrecords; mxp->name; mxp++)
-           if (strcasecmp(ctl->server.truename, mxp->name) == 0)
+           if (strcasecmp(ctl->server.truename, mxp->name) == 0
+                   || is_ip_alias(ctl->server.truename, mxp->name) == TRUE)
                goto match;
        return(FALSE);
     match:;
@@ -208,6 +253,5 @@ int is_host_alias(const char *name, struct query *ctl)
     return(TRUE);
 #endif /* HAVE_RES_SEARCH */
 }
-#endif /* HAVE_GETHOSTBYNAME */
 
 /* checkalias.c ends here */