]> Pileus Git - ~andy/fetchmail/blob - checkalias.c
Internationalization support via GNU gettext().
[~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  * i18n by Arnaldo Carvalho de Melo <acme@conectiva.com.br> 7-Nov-1998
8  */
9 #include "config.h"
10 #ifdef HAVE_GETHOSTBYNAME
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <netdb.h>
19 #include "i18n.h"
20 #include "mx.h"
21 #include "fetchmail.h"
22
23 #define MX_RETRIES      3
24
25 static int is_ip_alias(const char *name1,const char *name2)
26 /*
27  * Given two hostnames as arguments, returns TRUE if they
28  * have at least one IP address in common.
29  * No check is done on errors returned by gethostbyname,
30  * the calling function does them.
31  */
32 {
33     typedef unsigned char address_t[sizeof (struct in_addr)]; 
34     typedef struct _address_e
35     {
36         struct _address_e *next;
37         address_t address;
38     } 
39     address_e;
40     address_e *host_a_addr=0, *host_b_addr=0;   /* assignments pacify -Wall */
41     address_e *dummy_addr;
42
43     int i;
44     struct hostent *hp;
45     char **p;
46  
47     hp = gethostbyname(name1);
48  
49     dummy_addr = (address_e *)NULL;
50
51     for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
52     {
53         struct in_addr in;
54         (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
55         xalloca(host_a_addr, address_e *, sizeof (address_e));
56         memset (host_a_addr,0, sizeof (address_e));
57         host_a_addr->next = dummy_addr;
58         (void) memcpy(&host_a_addr->address, *p, sizeof (in.s_addr));
59         dummy_addr = host_a_addr;
60     }
61
62     hp = gethostbyname(name2);
63
64     dummy_addr = (address_e *)NULL;
65     for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
66     {
67         struct in_addr in;
68         (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
69         xalloca(host_b_addr, address_e *, sizeof (address_e));
70         memset (host_b_addr,0, sizeof (address_e));
71         host_b_addr->next = dummy_addr;
72         (void) memcpy(&host_b_addr->address, *p, sizeof (in.s_addr));
73         dummy_addr = host_b_addr;
74     }
75
76     while (host_a_addr)
77     {
78         while (host_b_addr)
79         {
80             if (!memcmp(host_b_addr->address,host_a_addr->address, sizeof (address_t)))
81                 return (TRUE);
82
83             host_b_addr = host_b_addr->next;
84         }
85         host_a_addr = host_a_addr->next;
86     }
87     return (FALSE);
88 }
89
90 int is_host_alias(const char *name, struct query *ctl)
91 /* determine whether name is a DNS alias of the mailserver for this query */
92 {
93     struct hostent      *he,*he_st;
94     struct mxentry      *mxp, *mxrecords;
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 #ifndef HAVE_RES_SEARCH
124     return(FALSE);
125 #else
126     /*
127      * The only code that calls the BIND library is here and in the
128      * start-of-run probe with gethostbyname(3) under ETRN/Kerberos.
129      *
130      * We know DNS service was up at the beginning of the run.
131      * If it's down, our nameserver has crashed.  We don't want to try
132      * delivering the current message or anything else from the
133      * current server until it's back up.
134      */
135     else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
136     {
137         if (strcasecmp(ctl->server.truename, he->h_name) == 0)
138             goto match;
139         else if (((he_st = gethostbyname(ctl->server.truename)) != (struct hostent *)NULL) && ctl->server.checkalias)
140         {
141             if (outlevel >= O_DEBUG)
142                 error(0, 0, _("Checking if %s is really the same node as %s"),ctl->server.truename,name);
143             if (is_ip_alias(ctl->server.truename,name) == TRUE)
144             {
145                 if (outlevel >= O_DEBUG)
146                     error(0, 0, _("Yes, their IP addresses match"));
147                 goto match;
148             }
149             if (outlevel >= O_DEBUG)
150                 error(0, 0, _("No, their IP addresses don't match"));
151         }
152         else
153             return(FALSE);
154     }
155     else
156         switch (h_errno)
157         {
158         case HOST_NOT_FOUND:    /* specified host is unknown */
159         case NO_ADDRESS:        /* valid, but does not have an IP address */
160             break;
161
162         case NO_RECOVERY:       /* non-recoverable name server error */
163         case TRY_AGAIN:         /* temporary error on authoritative server */
164         default:
165             if (outlevel != O_SILENT)
166                 putchar('\n');  /* terminate the progress message */
167             error(0, 0,
168                 _("nameserver failure while looking for `%s' during poll of %s."),
169                 name, ctl->server.pollname);
170             ctl->errcount++;
171             break;
172         }
173
174     /*
175      * We're only here if DNS was OK but the gethostbyname() failed
176      * with a HOST_NOT_FOUND or NO_ADDRESS error.
177      * Search for a name match on MX records pointing to the server.
178      */
179     h_errno = 0;
180     if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
181     {
182         switch (h_errno)
183         {
184         case HOST_NOT_FOUND:    /* specified host is unknown */
185         case NO_ADDRESS:        /* valid, but does not have an IP address */
186             return(FALSE);
187             break;
188
189         case NO_RECOVERY:       /* non-recoverable name server error */
190         case TRY_AGAIN:         /* temporary error on authoritative server */
191         default:
192             error(0, -1,
193                 _("nameserver failure while looking for `%s' during poll of %s."),
194                 name, ctl->server.pollname);
195             ctl->errcount++;
196             break;
197         }
198     }
199     else
200     {
201         for (mxp = mxrecords; mxp->name; mxp++)
202             if (strcasecmp(ctl->server.truename, mxp->name) == 0)
203                 goto match;
204         return(FALSE);
205     match:;
206     }
207
208     /* add this name to relevant server's `also known as' list */
209     save_str(&lead_server->akalist, name, 0);
210     return(TRUE);
211 #endif /* HAVE_RES_SEARCH */
212 }
213 #endif /* HAVE_GETHOSTBYNAME */
214
215 /* checkalias.c ends here */