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