]> Pileus Git - ~andy/fetchmail/blob - checkalias.c
Merge branch 'legacy_63'
[~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 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/types.h>
12
13 #include "fetchmail.h"
14
15 int is_host_alias(const char *name, struct query *ctl, struct addrinfo **res)
16 /* determine whether name is a DNS alias of the mailserver for this query */
17 {
18     struct idlist       *idl;
19     size_t              namelen;
20
21     struct hostdata *lead_server =
22         ctl->server.lead_server ? ctl->server.lead_server : &ctl->server;
23
24     /*
25      * The first two checks are optimizations that will catch a good
26      * many cases.
27      *
28      * (1) check against the `true name' deduced from the poll label
29      * and the via option (if present) at the beginning of the poll cycle.
30      * Odds are good this will either be the mailserver's FQDN or a suffix of
31      * it with the mailserver's domain's default host name omitted.
32      *
33      * (2) Then check the rest of the `also known as'
34      * cache accumulated by previous DNS checks.  This cache is primed
35      * by the aka list option.
36      *
37      * Any of these on a mail address is definitive.  Only if the
38      * name doesn't match any is it time to call the bind library.
39      * If this happens odds are good we're looking at an MX name.
40      */
41     if (strcasecmp(lead_server->truename, name) == 0)
42         return(TRUE);
43     else if (str_in_list(&lead_server->akalist, name, TRUE))
44         return(TRUE);
45
46     /*
47      * Now check for a suffix match on the akalist.  The theory here is
48      * that if the user says `aka netaxs.com', we actually want to match
49      * foo.netaxs.com and bar.netaxs.com.
50      */
51     namelen = strlen(name);
52     for (idl = lead_server->akalist; idl; idl = idl->next)
53     {
54         const char      *ep;
55
56         /*
57          * Test is >= here because str_in_list() should have caught the
58          * equal-length case above.  Doing it this way guarantees that
59          * ep[-1] is a valid reference.
60          */
61         if (strlen(idl->id) >= namelen)
62             continue;
63         ep = name + (namelen - strlen(idl->id));
64         /* a suffix led by . must match */
65         if (ep[-1] == '.' && !strcasecmp(ep, idl->id))
66             return(TRUE);
67     }
68
69     if (!ctl->server.dns)
70         return(FALSE);
71     (void)res;
72     return(FALSE);
73 }
74
75 /* checkalias.c ends here */