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