]> Pileus Git - ~andy/fetchmail/blob - checkalias.c
317cd2d1e99f989674db961228ca2c8e7bff4a78
[~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 <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 "mx.h"
17 #include "fetchmail.h"
18
19 #define MX_RETRIES      3
20
21 static int is_ip_alias(const char *name1,const char *name2)
22 /*
23  * Given two hostnames as arguments, returns TRUE if they
24  * have at least one IP address in common.
25  * It is meant to be called by the is_host_alias() function in driver.c
26  * No check is done on errors returned by gethostbyname,
27  * the calling function does them.
28  */
29 {
30     typedef unsigned char address_t[sizeof (struct in_addr)]; 
31     typedef struct _address_e
32     {
33         struct _address_e *next;
34         address_t address;
35     } 
36     address_e;
37     address_e *host_a_addr, *host_b_addr,*dummy_addr;
38
39     int i;
40     struct hostent *hp;
41     char **p;
42  
43     hp = gethostbyname(name1);
44  
45     dummy_addr = (address_e *)NULL;
46
47     for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
48     {
49         struct in_addr in;
50         (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
51         host_a_addr = (address_e *)xmalloc(sizeof( address_e));
52         memset (host_a_addr,0, sizeof (address_e));
53         host_a_addr->next = dummy_addr;
54         (void) memcpy(&host_a_addr->address, *p, sizeof (in.s_addr));
55         dummy_addr = host_a_addr;
56     }
57
58     hp = gethostbyname(name2);
59
60     dummy_addr = (address_e *)NULL;
61     for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
62     {
63         struct in_addr in;
64         (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
65         host_b_addr = (address_e *)xmalloc(sizeof( address_e));
66         memset (host_b_addr,0, sizeof (address_e));
67         host_b_addr->next = dummy_addr;
68         (void) memcpy(&host_b_addr->address, *p, sizeof (in.s_addr));
69         dummy_addr = host_b_addr;
70     }
71
72     while (host_a_addr)
73     {
74         while (host_b_addr)
75         {
76             if (!memcmp(host_b_addr->address,host_a_addr->address, sizeof (address_t)))
77                 return (TRUE);
78
79             host_b_addr = host_b_addr->next;
80         }
81         host_a_addr = host_a_addr->next;
82     }
83     return (FALSE);
84 }
85
86 int is_host_alias(const char *name, struct query *ctl)
87 /* determine whether name is a DNS alias of the hostname */
88 {
89     struct hostent      *he,*he_st;
90     struct mxentry      *mxp, *mxrecords;
91
92     struct hostdata *lead_server = 
93         ctl->server.lead_server ? ctl->server.lead_server : &ctl->server;
94
95     /*
96      * The first two checks are optimizations that will catch a good
97      * many cases.
98      *
99      * (1) check against the `true name' deduced from the poll label
100      * and the via option (if present) at the beginning of the poll cycle.  
101      * Odds are good this will either be the mailserver's FQDN or a suffix of
102      * it with the mailserver's domain's default host name omitted.
103      *
104      * (2) Then check the rest of the `also known as'
105      * cache accumulated by previous DNS checks.  This cache is primed
106      * by the aka list option.
107      *
108      * Any of these on a mail address is definitive.  Only if the
109      * name doesn't match any is it time to call the bind library.
110      * If this happens odds are good we're looking at an MX name.
111      */
112     if (strcasecmp(lead_server->truename, name) == 0)
113         return(TRUE);
114     else if (str_in_list(&lead_server->akalist, name, TRUE))
115         return(TRUE);
116     else if (!ctl->server.dns)
117         return(FALSE);
118
119 #ifndef HAVE_RES_SEARCH
120     return(FALSE);
121 #else
122     /*
123      * The only code that calls the BIND library is here and in the
124      * start-of-query probe with gethostbyname(3).
125      *
126      * We know DNS service was up at the beginning of this poll cycle.
127      * If it's down, our nameserver has crashed.  We don't want to try
128      * delivering the current message or anything else from this
129      * mailbox until it's back up.
130      */
131     else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
132     {
133         if (strcasecmp(ctl->server.truename, he->h_name) == 0)
134             goto match;
135         else if (((he_st = gethostbyname(ctl->server.truename)) != (struct hostent *)NULL) && ctl->server.checkalias)
136         {
137             if (outlevel == O_VERBOSE)
138                 error(0, 0, "Checking if %s is really the same node as %s",ctl->server.truename,name);
139             if (is_ip_alias(ctl->server.truename,name) == TRUE)
140             {
141                 if (outlevel == O_VERBOSE)
142                     error(0, 0, "Yes, their IP addresses match");
143                 goto match;
144             }
145             if (outlevel == O_VERBOSE)
146                 error(0, 0, "No, their IP addresses don't match");
147         }
148         else
149             return(FALSE);
150     }
151     else
152         switch (h_errno)
153         {
154         case HOST_NOT_FOUND:    /* specified host is unknown */
155         case NO_ADDRESS:        /* valid, but does not have an IP address */
156             break;
157
158         case NO_RECOVERY:       /* non-recoverable name server error */
159         case TRY_AGAIN:         /* temporary error on authoritative server */
160         default:
161             if (outlevel != O_SILENT)
162                 putchar('\n');  /* terminate the progress message */
163             error(0, 0,
164                 "nameserver failure while looking for `%s' during poll of %s.",
165                 name, ctl->server.pollname);
166             ctl->errcount++;
167             break;
168         }
169
170     /*
171      * We're only here if DNS was OK but the gethostbyname() failed
172      * with a HOST_NOT_FOUND or NO_ADDRESS error.
173      * Search for a name match on MX records pointing to the server.
174      */
175     h_errno = 0;
176     if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
177     {
178         switch (h_errno)
179         {
180         case HOST_NOT_FOUND:    /* specified host is unknown */
181         case NO_ADDRESS:        /* valid, but does not have an IP address */
182             return(FALSE);
183             break;
184
185         case NO_RECOVERY:       /* non-recoverable name server error */
186         case TRY_AGAIN:         /* temporary error on authoritative server */
187         default:
188             error(0, -1,
189                 "nameserver failure while looking for `%s' during poll of %s.",
190                 name, ctl->server.pollname);
191             ctl->errcount++;
192             break;
193         }
194     }
195     else
196     {
197         for (mxp = mxrecords; mxp->name; mxp++)
198             if (strcasecmp(ctl->server.truename, mxp->name) == 0)
199                 goto match;
200         return(FALSE);
201     match:;
202     }
203
204     /* add this name to relevant server's `also known as' list */
205     save_str(&lead_server->akalist, name, 0);
206     return(TRUE);
207 #endif /* HAVE_RES_SEARCH */
208 }
209 #endif /* HAVE_GETHOSTBYNAME */
210
211 /* checkalias.c ends here */