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