]> Pileus Git - ~andy/fetchmail/blob - checkalias.c
Minor bug fixes for socket.c
[~andy/fetchmail] / checkalias.c
1 /*
2  * checkalias.c -- check to see if two hostnames or IP addresses are equivalent
3  *
4  * Copyright 1997 by Eric S. Raymond
5  * For license terms, see the file COPYING in this directory.
6  */
7 #include "config.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #ifdef HAVE_NET_SOCKET_H
13 #include <net/socket.h>
14 #else
15 #include <sys/socket.h>
16 #endif
17 #include <netinet/in.h>
18 #ifdef HAVE_ARPA_INET_H
19 #include <arpa/inet.h>
20 #endif
21 #include <netdb.h>
22 #include "i18n.h"
23 #include "mx.h"
24 #include "fetchmail.h"
25 #include "getaddrinfo.h"
26
27 #define MX_RETRIES      3
28
29 typedef unsigned char address_t[sizeof (struct in_addr)];
30
31 #ifdef HAVE_RES_SEARCH
32 static int getaddresses(struct addrinfo **result, const char *name)
33 {
34     struct addrinfo hints;
35
36     memset(&hints, 0, sizeof(hints));
37     hints.ai_socktype=SOCK_STREAM;
38     hints.ai_protocol=PF_UNSPEC;
39     hints.ai_family=AF_UNSPEC;
40     return fm_getaddrinfo(name, NULL, &hints, result);
41 }
42
43 /* XXX FIXME: doesn't detect if an IPv6-mapped IPv4 address
44  * matches a real IPv4 address */
45 static int compareaddr(const struct addrinfo *a1, const struct addrinfo *a2)
46 {
47     if (a1->ai_family != a2->ai_family) return FALSE;
48     if (a1->ai_addrlen != a2->ai_addrlen) return FALSE;
49     return (!memcmp(a1->ai_addr, a2->ai_addr, a1->ai_addrlen));
50 }
51
52 static int is_ip_alias(const char *name1,const char *name2)
53 /*
54  * Given two hostnames as arguments, returns TRUE if they
55  * have at least one IP address in common.
56  * No check is done on errors returned by gethostbyname,
57  * the calling function does them.
58  */
59 {
60     int rc = FALSE;
61
62     struct addrinfo *res1 = NULL, *res2 = NULL, *ii, *ij;
63
64     if (getaddresses(&res1, name1))
65         goto found;
66
67     if (getaddresses(&res2, name2))
68         goto found;
69
70     for (ii = res1 ; ii ; ii = ii -> ai_next) {
71         for (ij = res2 ; ij ; ij = ij -> ai_next) {
72             if (compareaddr(ii, ij)) {
73                 rc = TRUE;
74                 goto found;
75             }
76         }
77     }
78
79 found:
80     if (res2)
81         fm_freeaddrinfo(res2);
82     if (res1)
83         fm_freeaddrinfo(res1);
84     return rc;
85 }
86 #endif
87
88 int is_host_alias(const char *name, struct query *ctl, struct addrinfo **res)
89 /* determine whether name is a DNS alias of the mailserver for this query */
90 {
91 #ifdef HAVE_RES_SEARCH
92     struct mxentry      *mxp, *mxrecords;
93     int                 e;
94     struct addrinfo     hints, *res_st;
95 #endif
96     struct idlist       *idl;
97     size_t              namelen;
98
99     struct hostdata *lead_server =
100         ctl->server.lead_server ? ctl->server.lead_server : &ctl->server;
101
102     /*
103      * The first two checks are optimizations that will catch a good
104      * many cases.
105      *
106      * (1) check against the `true name' deduced from the poll label
107      * and the via option (if present) at the beginning of the poll cycle.
108      * Odds are good this will either be the mailserver's FQDN or a suffix of
109      * it with the mailserver's domain's default host name omitted.
110      *
111      * (2) Then check the rest of the `also known as'
112      * cache accumulated by previous DNS checks.  This cache is primed
113      * by the aka list option.
114      *
115      * Any of these on a mail address is definitive.  Only if the
116      * name doesn't match any is it time to call the bind library.
117      * If this happens odds are good we're looking at an MX name.
118      */
119     if (strcasecmp(lead_server->truename, name) == 0)
120         return(TRUE);
121     else if (str_in_list(&lead_server->akalist, name, TRUE))
122         return(TRUE);
123
124     /*
125      * Now check for a suffix match on the akalist.  The theory here is
126      * that if the user says `aka netaxs.com', we actually want to match
127      * foo.netaxs.com and bar.netaxs.com.
128      */
129     namelen = strlen(name);
130     for (idl = lead_server->akalist; idl; idl = idl->next)
131     {
132         const char      *ep;
133
134         /*
135          * Test is >= here because str_in_list() should have caught the
136          * equal-length case above.  Doing it this way guarantees that
137          * ep[-1] is a valid reference.
138          */
139         if (strlen(idl->id) >= namelen)
140             continue;
141         ep = name + (namelen - strlen(idl->id));
142         /* a suffix led by . must match */
143         if (ep[-1] == '.' && !strcasecmp(ep, idl->id))
144             return(TRUE);
145     }
146
147     if (!ctl->server.dns)
148         return(FALSE);
149 #ifndef HAVE_RES_SEARCH
150     (void)res;
151     return(FALSE);
152 #else
153     /*
154      * The only code that calls the BIND library is here and in the
155      * start-of-run probe with gethostbyname(3) under ETRN/Kerberos.
156      *
157      * We know DNS service was up at the beginning of the run.
158      * If it's down, our nameserver has crashed.  We don't want to try
159      * delivering the current message or anything else from the
160      * current server until it's back up.
161      */
162     memset(&hints, 0, sizeof hints);
163     hints.ai_family=AF_UNSPEC;
164     hints.ai_protocol=PF_UNSPEC;
165     hints.ai_socktype=SOCK_STREAM;
166     hints.ai_flags=AI_CANONNAME;
167
168     e = fm_getaddrinfo(name, NULL, &hints, res);
169     if (e == 0)
170     {
171         int rr = (strcasecmp(ctl->server.truename, (*res)->ai_canonname) == 0);
172         fm_freeaddrinfo(*res); *res = NULL;
173         if (rr)
174             goto match;
175         else if (ctl->server.checkalias && 0 == fm_getaddrinfo(ctl->server.truename, NULL, &hints, &res_st))
176         {
177             fm_freeaddrinfo(res_st);
178             if (outlevel >= O_DEBUG)
179                 report(stdout, GT_("Checking if %s is really the same node as %s\n"),ctl->server.truename,name);
180             if (is_ip_alias(ctl->server.truename,name) == TRUE)
181             {
182                 if (outlevel >= O_DEBUG)
183                     report(stdout, GT_("Yes, their IP addresses match\n"));
184                 goto match;
185             }
186             if (outlevel >= O_DEBUG)
187                 report(stdout, GT_("No, their IP addresses don't match\n"));
188             return(FALSE);
189         } else {
190             return(FALSE);
191         }
192     }
193     else
194         switch (e)
195         {
196             case EAI_NONAME:    /* specified host is unknown */
197 #ifdef EAI_NODATA
198             /* EAI_NODATA was in the older RFC-2553, but that got
199              * obsoleted by RFC-3493 - there, EAI_NODATA is no longer
200              * valid, and modern operating systems no longer issue this
201              * error code. libesmtp/getaddrinfo.?, however, still does.
202              * (This was reported to Brian Stafford 2013-02-03.)
203              */
204             case EAI_NODATA:    /* specified host is unknown */
205 #endif
206                 break;
207
208             default:
209                 if (outlevel != O_SILENT)
210                     report_complete(stdout, "\n");      /* terminate the progress message */
211                 report(stderr,
212                         GT_("nameserver failure while looking for '%s' during poll of %s: %s\n"),
213                         name, ctl->server.pollname, gai_strerror(e));
214                 ctl->errcount++;
215                 break;
216         }
217
218     /*
219      * We're only here if DNS was OK but the gethostbyname() failed
220      * with a HOST_NOT_FOUND or NO_ADDRESS error.
221      * Search for a name match on MX records pointing to the server.
222      */
223     h_errno = 0;
224     if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
225     {
226         switch (h_errno)
227         {
228         case HOST_NOT_FOUND:    /* specified host is unknown */
229 #ifdef NO_ADDRESS
230         case NO_ADDRESS:        /* valid, but does not have an IP address */
231             return(FALSE);
232 #endif
233         case NO_RECOVERY:       /* non-recoverable name server error */
234         case TRY_AGAIN:         /* temporary error on authoritative server */
235         default:
236             report(stderr,
237                 GT_("nameserver failure while looking for `%s' during poll of %s.\n"),
238                 name, ctl->server.pollname);
239             ctl->errcount++;
240             break;
241         }
242     } else {
243         for (mxp = mxrecords; mxp->name; mxp++)
244             if (strcasecmp(ctl->server.truename, mxp->name) == 0
245                     || is_ip_alias(ctl->server.truename, mxp->name) == TRUE)
246                 goto match;
247         return(FALSE);
248     match:;
249     }
250
251     /* add this name to relevant server's `also known as' list */
252     save_str(&lead_server->akalist, name, 0);
253     return(TRUE);
254 #endif /* HAVE_RES_SEARCH */
255 }
256
257 /* checkalias.c ends here */