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