2 * checkalias.c -- check to see if two hostnames or IP addresses are equivalent
4 * Copyright 1997 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
8 #ifdef HAVE_GETHOSTBYNAME
12 #include <sys/types.h>
13 #ifdef HAVE_NET_SOCKET_H
14 #include <net/socket.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #ifdef HAVE_ARPA_INET_H
20 #include <arpa/inet.h>
25 #include "fetchmail.h"
29 typedef unsigned char address_t[sizeof (struct in_addr)];
31 typedef struct _address_e
33 struct _address_e *next;
37 static void free_addrlst(address_e *lst)
47 /* XXX FIXME: This junk isn't IPv6 aware */
48 static int is_ip_alias(const char *name1,const char *name2)
50 * Given two hostnames as arguments, returns TRUE if they
51 * have at least one IP address in common.
52 * No check is done on errors returned by gethostbyname,
53 * the calling function does them.
56 address_e *host_a_addr=0, *host_b_addr=0;
57 address_e *dummy_addr, *ii, *ij;
63 hp = gethostbyname((char*)name1);
65 dummy_addr = (address_e *)NULL;
68 for (p = hp->h_addr_list; *p != 0; p++) {
71 host_a_addr = xmalloc(sizeof (address_e));
72 memset (host_a_addr,0, sizeof (address_e));
73 host_a_addr->next = dummy_addr;
74 (void) memcpy(&host_a_addr->address, *p, sizeof (in.s_addr));
75 dummy_addr = host_a_addr;
78 hp = gethostbyname((char*)name2);
80 dummy_addr = (address_e *)NULL;
83 for (p = hp->h_addr_list; *p != 0; p++) {
86 host_b_addr = xmalloc(sizeof (address_e));
87 memset (host_b_addr,0, sizeof (address_e));
88 host_b_addr->next = dummy_addr;
89 (void) memcpy(&host_b_addr->address, *p, sizeof (in.s_addr));
90 dummy_addr = host_b_addr;
93 for (ii = host_a_addr ; ii ; ii = ii -> next) {
94 for (ij = host_b_addr ; ij ; ij = ij -> next) {
95 if (!memcmp(ii->address, ij->address, sizeof (address_t))) {
103 free_addrlst(host_b_addr);
104 free_addrlst(host_a_addr);
108 int is_host_alias(const char *name, struct query *ctl)
109 /* determine whether name is a DNS alias of the mailserver for this query */
111 struct hostent *he,*he_st;
112 struct mxentry *mxp, *mxrecords;
116 struct hostdata *lead_server =
117 ctl->server.lead_server ? ctl->server.lead_server : &ctl->server;
120 * The first two checks are optimizations that will catch a good
123 * (1) check against the `true name' deduced from the poll label
124 * and the via option (if present) at the beginning of the poll cycle.
125 * Odds are good this will either be the mailserver's FQDN or a suffix of
126 * it with the mailserver's domain's default host name omitted.
128 * (2) Then check the rest of the `also known as'
129 * cache accumulated by previous DNS checks. This cache is primed
130 * by the aka list option.
132 * Any of these on a mail address is definitive. Only if the
133 * name doesn't match any is it time to call the bind library.
134 * If this happens odds are good we're looking at an MX name.
136 if (strcasecmp(lead_server->truename, name) == 0)
138 else if (str_in_list(&lead_server->akalist, name, TRUE))
142 * Now check for a suffix match on the akalist. The theory here is
143 * that if the user says `aka netaxs.com', we actually want to match
144 * foo.netaxs.com and bar.netaxs.com.
146 namelen = strlen(name);
147 for (idl = lead_server->akalist; idl; idl = idl->next)
152 * Test is >= here because str_in_list() should have caught the
153 * equal-length case above. Doing it this way guarantees that
154 * ep[-1] is a valid reference.
156 if (strlen(idl->id) >= namelen)
158 ep = (char *)name + (namelen - strlen(idl->id));
159 /* a suffix led by . must match */
160 if (ep[-1] == '.' && !strcasecmp(ep, idl->id))
164 if (!ctl->server.dns)
166 #ifndef HAVE_RES_SEARCH
170 * The only code that calls the BIND library is here and in the
171 * start-of-run probe with gethostbyname(3) under ETRN/Kerberos.
173 * We know DNS service was up at the beginning of the run.
174 * If it's down, our nameserver has crashed. We don't want to try
175 * delivering the current message or anything else from the
176 * current server until it's back up.
178 if ((he = gethostbyname((char*)name)) != (struct hostent *)NULL)
180 if (strcasecmp(ctl->server.truename, he->h_name) == 0)
182 else if (((he_st = gethostbyname(ctl->server.truename)) != (struct hostent *)NULL) && ctl->server.checkalias)
184 if (outlevel >= O_DEBUG)
185 report(stdout, GT_("Checking if %s is really the same node as %s\n"),ctl->server.truename,name);
186 if (is_ip_alias(ctl->server.truename,name) == TRUE)
188 if (outlevel >= O_DEBUG)
189 report(stdout, GT_("Yes, their IP addresses match\n"));
192 if (outlevel >= O_DEBUG)
193 report(stdout, GT_("No, their IP addresses don't match\n"));
202 case HOST_NOT_FOUND: /* specified host is unknown */
204 case NO_ADDRESS: /* valid, but does not have an IP address */
207 case NO_RECOVERY: /* non-recoverable name server error */
208 case TRY_AGAIN: /* temporary error on authoritative server */
210 if (outlevel != O_SILENT)
211 report_complete(stdout, "\n"); /* terminate the progress message */
213 GT_("nameserver failure while looking for `%s' during poll of %s.\n"),
214 name, ctl->server.pollname);
220 * We're only here if DNS was OK but the gethostbyname() failed
221 * with a HOST_NOT_FOUND or NO_ADDRESS error.
222 * Search for a name match on MX records pointing to the server.
225 if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
229 case HOST_NOT_FOUND: /* specified host is unknown */
231 case NO_ADDRESS: /* valid, but does not have an IP address */
234 case NO_RECOVERY: /* non-recoverable name server error */
235 case TRY_AGAIN: /* temporary error on authoritative server */
238 GT_("nameserver failure while looking for `%s' during poll of %s.\n"),
239 name, ctl->server.pollname);
246 for (mxp = mxrecords; mxp->name; mxp++)
247 if (strcasecmp(ctl->server.truename, mxp->name) == 0
248 || is_ip_alias(ctl->server.truename, mxp->name) == TRUE)
254 /* add this name to relevant server's `also known as' list */
255 save_str(&lead_server->akalist, name, 0);
257 #endif /* HAVE_RES_SEARCH */
259 #endif /* HAVE_GETHOSTBYNAME */
261 /* checkalias.c ends here */