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