]> Pileus Git - ~andy/fetchmail/blob - checkalias.c
28d7861f59456191c7b3fd24303a33fcc3208555
[~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 #ifdef HAVE_GETHOSTBYNAME
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <netdb.h>
17 #include "i18n.h"
18 #include "mx.h"
19 #include "fetchmail.h"
20
21 #define MX_RETRIES      3
22
23 static int is_ip_alias(const char *name1,const char *name2)
24 /*
25  * Given two hostnames as arguments, returns TRUE if they
26  * have at least one IP address in common.
27  * No check is done on errors returned by gethostbyname,
28  * the calling function does them.
29  */
30 {
31     typedef unsigned char address_t[sizeof (struct in_addr)]; 
32     typedef struct _address_e
33     {
34         struct _address_e *next;
35         address_t address;
36     } 
37     address_e;
38     address_e *host_a_addr=0, *host_b_addr=0;   /* assignments pacify -Wall */
39     address_e *dummy_addr;
40
41     int i;
42     struct hostent *hp;
43     char **p;
44  
45     hp = gethostbyname(name1);
46  
47     dummy_addr = (address_e *)NULL;
48
49     for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
50     {
51         struct in_addr in;
52         (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
53         xalloca(host_a_addr, address_e *, sizeof (address_e));
54         memset (host_a_addr,0, sizeof (address_e));
55         host_a_addr->next = dummy_addr;
56         (void) memcpy(&host_a_addr->address, *p, sizeof (in.s_addr));
57         dummy_addr = host_a_addr;
58     }
59
60     hp = gethostbyname(name2);
61
62     dummy_addr = (address_e *)NULL;
63     for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
64     {
65         struct in_addr in;
66         (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
67         xalloca(host_b_addr, address_e *, sizeof (address_e));
68         memset (host_b_addr,0, sizeof (address_e));
69         host_b_addr->next = dummy_addr;
70         (void) memcpy(&host_b_addr->address, *p, sizeof (in.s_addr));
71         dummy_addr = host_b_addr;
72     }
73
74     while (host_a_addr)
75     {
76         while (host_b_addr)
77         {
78             if (!memcmp(host_b_addr->address,host_a_addr->address, sizeof (address_t)))
79                 return (TRUE);
80
81             host_b_addr = host_b_addr->next;
82         }
83         host_a_addr = host_a_addr->next;
84     }
85     return (FALSE);
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 hostent      *he,*he_st;
92     struct mxentry      *mxp, *mxrecords;
93     struct idlist       *idl;
94     int                 namelen;
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     else if (!ctl->server.dns)
121         return(FALSE);
122
123     /*
124      * Now check for a suffix match on the akalist.  The theory here is
125      * that if the user says `aka netaxs.com', we actually want to match
126      * foo.netaxs.com and bar.netaxs.com.
127      */
128     namelen = strlen(name);
129     for (idl = lead_server->akalist; idl; idl = idl->next)
130     {
131         char    *ep;
132
133         /*
134          * Test is >= here because str_in_list() should have caught the
135          * equal-length case above.  Doing it this way guarantees that
136          * ep[-1] is a valid reference.
137          */
138         if (strlen(idl->id) >= namelen)
139             continue;
140         ep = (char *)name + (namelen - strlen(idl->id));
141         /* a suffix led by . must match */
142         if (ep[-1] == '.' && !strcmp(ep, idl->id))
143             return(TRUE);
144     }
145
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     if ((he = gethostbyname(name)) != (struct hostent *)NULL)
159     {
160         if (strcasecmp(ctl->server.truename, he->h_name) == 0)
161             goto match;
162         else if (((he_st = gethostbyname(ctl->server.truename)) != (struct hostent *)NULL) && ctl->server.checkalias)
163         {
164             if (outlevel >= O_DEBUG)
165                 report(stdout, _("Checking if %s is really the same node as %s\n"),ctl->server.truename,name);
166             if (is_ip_alias(ctl->server.truename,name) == TRUE)
167             {
168                 if (outlevel >= O_DEBUG)
169                     report(stdout, _("Yes, their IP addresses match\n"));
170                 goto match;
171             }
172             if (outlevel >= O_DEBUG)
173                 report(stdout, _("No, their IP addresses don't match\n"));
174             return(FALSE);
175         }
176         else
177             return(FALSE);
178     }
179     else
180         switch (h_errno)
181         {
182         case HOST_NOT_FOUND:    /* specified host is unknown */
183         case NO_ADDRESS:        /* valid, but does not have an IP address */
184             break;
185
186         case NO_RECOVERY:       /* non-recoverable name server error */
187         case TRY_AGAIN:         /* temporary error on authoritative server */
188         default:
189             if (outlevel != O_SILENT)
190                 report_complete(stdout, "\n");  /* terminate the progress message */
191             report(stderr,
192                 _("nameserver failure while looking for `%s' during poll of %s.\n"),
193                 name, ctl->server.pollname);
194             ctl->errcount++;
195             break;
196         }
197
198     /*
199      * We're only here if DNS was OK but the gethostbyname() failed
200      * with a HOST_NOT_FOUND or NO_ADDRESS error.
201      * Search for a name match on MX records pointing to the server.
202      */
203     h_errno = 0;
204     if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
205     {
206         switch (h_errno)
207         {
208         case HOST_NOT_FOUND:    /* specified host is unknown */
209         case NO_ADDRESS:        /* valid, but does not have an IP address */
210             return(FALSE);
211             break;
212
213         case NO_RECOVERY:       /* non-recoverable name server error */
214         case TRY_AGAIN:         /* temporary error on authoritative server */
215         default:
216             report(stderr,
217                 _("nameserver failure while looking for `%s' during poll of %s.\n"),
218                 name, ctl->server.pollname);
219             ctl->errcount++;
220             break;
221         }
222     }
223     else
224     {
225         for (mxp = mxrecords; mxp->name; mxp++)
226             if (strcasecmp(ctl->server.truename, mxp->name) == 0)
227                 goto match;
228         return(FALSE);
229     match:;
230     }
231
232     /* add this name to relevant server's `also known as' list */
233     save_str(&lead_server->akalist, name, 0);
234     return(TRUE);
235 #endif /* HAVE_RES_SEARCH */
236 }
237 #endif /* HAVE_GETHOSTBYNAME */
238
239 /* checkalias.c ends here */