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
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
16 #if defined(HAVE_ALLOCA_H)
24 #include "fetchmail.h"
28 static int is_ip_alias(const char *name1,const char *name2)
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.
36 typedef unsigned char address_t[sizeof (struct in_addr)];
37 typedef struct _address_e
39 struct _address_e *next;
43 address_e *host_a_addr, *host_b_addr,*dummy_addr;
49 hp = gethostbyname(name1);
51 dummy_addr = (address_e *)NULL;
53 for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
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;
64 hp = gethostbyname(name2);
66 dummy_addr = (address_e *)NULL;
67 for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
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;
82 if (!memcmp(host_b_addr->address,host_a_addr->address, sizeof (address_t)))
85 host_b_addr = host_b_addr->next;
87 host_a_addr = host_a_addr->next;
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 */
95 struct hostent *he,*he_st;
96 struct mxentry *mxp, *mxrecords;
98 struct hostdata *lead_server =
99 ctl->server.lead_server ? ctl->server.lead_server : &ctl->server;
102 * The first two checks are optimizations that will catch a good
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.
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.
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.
118 if (strcasecmp(lead_server->truename, name) == 0)
120 else if (str_in_list(&lead_server->akalist, name, TRUE))
122 else if (!ctl->server.dns)
125 #ifndef HAVE_RES_SEARCH
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.
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.
137 else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
139 if (strcasecmp(ctl->server.truename, he->h_name) == 0)
141 else if (((he_st = gethostbyname(ctl->server.truename)) != (struct hostent *)NULL) && ctl->server.checkalias)
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)
147 if (outlevel == O_VERBOSE)
148 error(0, 0, "Yes, their IP addresses match");
151 if (outlevel == O_VERBOSE)
152 error(0, 0, "No, their IP addresses don't match");
160 case HOST_NOT_FOUND: /* specified host is unknown */
161 case NO_ADDRESS: /* valid, but does not have an IP address */
164 case NO_RECOVERY: /* non-recoverable name server error */
165 case TRY_AGAIN: /* temporary error on authoritative server */
167 if (outlevel != O_SILENT)
168 putchar('\n'); /* terminate the progress message */
170 "nameserver failure while looking for `%s' during poll of %s.",
171 name, ctl->server.pollname);
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.
182 if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
186 case HOST_NOT_FOUND: /* specified host is unknown */
187 case NO_ADDRESS: /* valid, but does not have an IP address */
191 case NO_RECOVERY: /* non-recoverable name server error */
192 case TRY_AGAIN: /* temporary error on authoritative server */
195 "nameserver failure while looking for `%s' during poll of %s.",
196 name, ctl->server.pollname);
203 for (mxp = mxrecords; mxp->name; mxp++)
204 if (strcasecmp(ctl->server.truename, mxp->name) == 0)
210 /* add this name to relevant server's `also known as' list */
211 save_str(&lead_server->akalist, name, 0);
213 #endif /* HAVE_RES_SEARCH */
215 #endif /* HAVE_GETHOSTBYNAME */
217 /* checkalias.c ends here */