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