4 * locate the originating host
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
21 #include "serv_extensions.h"
22 #include "locate_host.h"
23 #include "sysdep_decls.h"
28 void locate_host(char *tbuf, size_t n,
29 char *abuf, size_t na,
30 const struct in_addr *addr)
36 char address_string[SIZ];
38 lprintf(CTDL_DEBUG, "locate_host() called\n");
40 #ifdef HAVE_NONREENTRANT_NETDB
41 begin_critical_section(S_NETDB);
44 i = (const char *) addr;
49 sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
52 safestrncpy(abuf, address_string, na);
55 if ((ch = gethostbyaddr((const char *) addr,
56 sizeof(*addr), AF_INET)) == NULL) {
58 safestrncpy(tbuf, address_string, n);
59 goto end; /* because we might need to end the critical
62 /* check if the forward DNS agrees; if not, they're spoofing */
63 j = strdup(ch->h_name);
64 ch = gethostbyname(j);
69 /* check address for consistency */
70 for (; *ch->h_addr_list; ch->h_addr_list++)
71 if (!memcmp(*ch->h_addr_list, addr,
73 safestrncpy(tbuf, ch->h_name, 63);
76 goto bad_dns; /* they were spoofing. report a numeric IP
81 #ifdef HAVE_NONREENTRANT_NETDB
82 end_critical_section(S_NETDB);
86 lprintf(CTDL_DEBUG, "locate_host() exiting\n");
91 * Check to see if a host is on some sort of spam list (RBL)
92 * If spammer, returns nonzero and places reason in 'message_to_spammer'
94 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
101 char rbl_domains[SIZ];
103 strcpy(message_to_spammer, "ok");
105 i = (const char *) addr;
106 a1 = ((*i++) & 0xff);
107 a2 = ((*i++) & 0xff);
108 a3 = ((*i++) & 0xff);
109 a4 = ((*i++) & 0xff);
111 /* See if we have any RBL domains configured */
112 num_rbl = get_hosts(rbl_domains, "rbl");
113 if (num_rbl < 1) return(0);
115 /* Try all configured RBL's */
116 for (rbl=0; rbl<num_rbl; ++rbl) {
117 snprintf(tbuf, sizeof tbuf,
120 extract(&tbuf[strlen(tbuf)], rbl_domains, rbl);
122 if (gethostbyname(tbuf) != NULL) {
123 strcpy(message_to_spammer,
124 "5.7.1 Message rejected due to "
125 "known spammer source IP address"
136 * Check to see if the client host is on some sort of spam list (RBL)
137 * If spammer, returns nonzero and places reason in 'message_to_spammer'
139 int rbl_check(char *message_to_spammer) {
140 struct sockaddr_in sin;
141 int len; /* should be socklen_t but doesn't work on Macintosh */
143 if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, &len)) {
144 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
150 * Convert a host name to a dotted quad address.
151 * Returns zero on success or nonzero on failure.
153 int hostname_to_dotted_quad(char *addr, char *host) {
158 ch = gethostbyname(host);
160 strcpy(addr, "0.0.0.0");
164 i = (const char *) ch->h_addr_list[0];
165 a1 = ((*i++) & 0xff);
166 a2 = ((*i++) & 0xff);
167 a3 = ((*i++) & 0xff);
168 a4 = ((*i++) & 0xff);
169 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);