]> Pileus Git - ~andy/fetchmail/blob - checkalias.c
Global variable cleanup, to fix daemon mode reinitialization problems.
[~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;
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
61     struct addrinfo *res1 = NULL, *res2 = NULL, *ii, *ij;
62
63     if (getaddresses(&res1, name1))
64         goto found;
65
66     if (getaddresses(&res2, name2))
67         goto found;
68
69     for (ii = res1 ; ii ; ii = ii -> ai_next) {
70         for (ij = res2 ; ij ; ij = ij -> ai_next) {
71             if (compareaddr(ii, ij)) {
72                 rc = TRUE;
73                 goto found;
74             }
75         }
76     }
77
78 found:
79     if (res2)
80         freeaddrinfo(res2);
81     if (res1)
82         freeaddrinfo(res1);
83     return rc;
84 }
85
86 int is_host_alias(const char *name, struct query *ctl)
87 /* determine whether name is a DNS alias of the mailserver for this query */
88 {
89     struct mxentry      *mxp, *mxrecords;
90     struct idlist       *idl;
91     int                 namelen, e;
92     struct addrinfo     hints, *res, *res_st;
93
94     struct hostdata *lead_server =
95         ctl->server.lead_server ? ctl->server.lead_server : &ctl->server;
96
97     /*
98      * The first two checks are optimizations that will catch a good
99      * many cases.
100      *
101      * (1) check against the `true name' deduced from the poll label
102      * and the via option (if present) at the beginning of the poll cycle.
103      * Odds are good this will either be the mailserver's FQDN or a suffix of
104      * it with the mailserver's domain's default host name omitted.
105      *
106      * (2) Then check the rest of the `also known as'
107      * cache accumulated by previous DNS checks.  This cache is primed
108      * by the aka list option.
109      *
110      * Any of these on a mail address is definitive.  Only if the
111      * name doesn't match any is it time to call the bind library.
112      * If this happens odds are good we're looking at an MX name.
113      */
114     if (strcasecmp(lead_server->truename, name) == 0)
115         return(TRUE);
116     else if (str_in_list(&lead_server->akalist, name, TRUE))
117         return(TRUE);
118
119     /*
120      * Now check for a suffix match on the akalist.  The theory here is
121      * that if the user says `aka netaxs.com', we actually want to match
122      * foo.netaxs.com and bar.netaxs.com.
123      */
124     namelen = strlen(name);
125     for (idl = lead_server->akalist; idl; idl = idl->next)
126     {
127         char    *ep;
128
129         /*
130          * Test is >= here because str_in_list() should have caught the
131          * equal-length case above.  Doing it this way guarantees that
132          * ep[-1] is a valid reference.
133          */
134         if (strlen(idl->id) >= namelen)
135             continue;
136         ep = (char *)name + (namelen - strlen(idl->id));
137         /* a suffix led by . must match */
138         if (ep[-1] == '.' && !strcasecmp(ep, idl->id))
139             return(TRUE);
140     }
141
142     if (!ctl->server.dns)
143         return(FALSE);
144 #ifndef HAVE_RES_SEARCH
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
161     e = getaddrinfo(name, NULL, &hints, &res);
162     if (e == 0)
163     {
164         int rr = (strcasecmp(ctl->server.truename, res->ai_canonname) == 0);
165         freeaddrinfo(res);
166         if (rr)
167             goto match;
168         else if (ctl->server.checkalias && 0 == getaddrinfo(ctl->server.truename, NULL, &hints, &res_st))
169         {
170             freeaddrinfo(res_st);
171             if (outlevel >= O_DEBUG)
172                 report(stdout, GT_("Checking if %s is really the same node as %s\n"),ctl->server.truename,name);
173             if (is_ip_alias(ctl->server.truename,name) == TRUE)
174             {
175                 if (outlevel >= O_DEBUG)
176                     report(stdout, GT_("Yes, their IP addresses match\n"));
177                 goto match;
178             }
179             if (outlevel >= O_DEBUG)
180                 report(stdout, GT_("No, their IP addresses don't match\n"));
181             return(FALSE);
182         } else {
183             return(FALSE);
184         }
185     }
186     else
187         switch (e)
188         {
189             case EAI_NONAME:    /* specified host is unknown */
190                 break;
191
192             default:
193                 if (outlevel != O_SILENT)
194                     report_complete(stdout, "\n");      /* terminate the progress message */
195                 report(stderr,
196                         GT_("nameserver failure while looking for '%s' during poll of %s: %s\n"),
197                         name, ctl->server.pollname, gai_strerror(e));
198                 ctl->errcount++;
199                 break;
200         }
201
202     /*
203      * We're only here if DNS was OK but the gethostbyname() failed
204      * with a HOST_NOT_FOUND or NO_ADDRESS error.
205      * Search for a name match on MX records pointing to the server.
206      */
207     h_errno = 0;
208     if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
209     {
210         switch (h_errno)
211         {
212         case HOST_NOT_FOUND:    /* specified host is unknown */
213 #ifdef NO_ADDRESS
214         case NO_ADDRESS:        /* valid, but does not have an IP address */
215             return(FALSE);
216 #endif
217         case NO_RECOVERY:       /* non-recoverable name server error */
218         case TRY_AGAIN:         /* temporary error on authoritative server */
219         default:
220             report(stderr,
221                 GT_("nameserver failure while looking for `%s' during poll of %s.\n"),
222                 name, ctl->server.pollname);
223             ctl->errcount++;
224             break;
225         }
226     } else {
227         for (mxp = mxrecords; mxp->name; mxp++)
228             if (strcasecmp(ctl->server.truename, mxp->name) == 0
229                     || is_ip_alias(ctl->server.truename, mxp->name) == TRUE)
230                 goto match;
231         return(FALSE);
232     match:;
233     }
234
235     /* add this name to relevant server's `also known as' list */
236     save_str(&lead_server->akalist, name, 0);
237     return(TRUE);
238 #endif /* HAVE_RES_SEARCH */
239 }
240
241 /* checkalias.c ends here */