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"
29 #include <arpa/nameser.h>
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 #include <arpa/nameser_compat.h>
37 void locate_host(char *tbuf, size_t n,
38 char *abuf, size_t na,
39 const struct in_addr *addr)
45 char address_string[SIZ];
48 #ifdef HAVE_NONREENTRANT_NETDB
49 begin_critical_section(S_NETDB);
52 i = (const char *) addr;
57 sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
60 safestrncpy(abuf, address_string, na);
63 if ((ch = gethostbyaddr((const char *) addr,
64 sizeof(*addr), AF_INET)) == NULL) {
66 safestrncpy(tbuf, address_string, n);
67 goto end; /* because we might need to end the critical
70 /* check if the forward DNS agrees; if not, they're spoofing */
71 j = strdup(ch->h_name);
72 ch = gethostbyname(j);
77 /* check address for consistency */
78 for (; *ch->h_addr_list; ch->h_addr_list++)
79 if (!memcmp(*ch->h_addr_list, addr,
81 safestrncpy(tbuf, ch->h_name, 63);
84 goto bad_dns; /* they were spoofing. report a numeric IP
89 #ifdef HAVE_NONREENTRANT_NETDB
90 end_critical_section(S_NETDB);
98 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
100 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
101 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
104 u_char fixedans[ PACKETSZ ];
106 int need_to_free_answer = 0;
113 /* Make our DNS query. */
116 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
118 /* Was there a problem? If so, the domain doesn't exist. */
120 if (txtbuf != NULL) {
128 answer = malloc( len );
129 need_to_free_answer = 1;
130 len = res_query( domain, C_IN, T_A, answer, len );
132 if (txtbuf != NULL) {
133 snprintf(txtbuf, txtbufsize,
134 "Message rejected due to known spammer source IP address");
136 if (need_to_free_answer) free(answer);
141 result = ( char * )malloc( RESULT_SIZE );
145 /* Make another DNS query for textual data; this shouldn't
146 be a performance hit, since it'll now be cached at the
147 nameserver we're using. */
149 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
151 /* Just in case there's no TXT record... */
154 if (txtbuf != NULL) {
155 snprintf(txtbuf, txtbufsize,
156 "Message rejected due to known spammer source IP address");
158 if (need_to_free_answer) free(answer);
163 /* Skip the header and the address we queried. */
164 cp = answer + sizeof( HEADER );
172 /* This seems to be a bit of magic data that we need to
173 skip. I wish there were good online documentation
174 for programming for libresolv, so I'd know what I'm
175 skipping here. Anyone reading this, feel free to
177 cp += 1 + NS_INT16SZ + NS_INT32SZ;
179 /* Skip the type, class and ttl. */
180 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
182 /* Get the length and end of the buffer. */
186 /* Iterate over any multiple answers we might have. In
187 this context, it's unlikely, but anyway. */
189 rend = result + RESULT_SIZE - 1;
190 while( cp < cend && rp < rend )
194 for( b = a; b > 0 && cp < cend && rp < rend;
197 if( *cp == '\n' || *cp == '"' ||
206 if (txtbuf != NULL) {
207 snprintf(txtbuf, txtbufsize, "%s", result);
209 if (need_to_free_answer) free(answer);
216 * Check to see if a host is on some sort of spam list (RBL)
217 * If spammer, returns nonzero and places reason in 'message_to_spammer'
219 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
226 char rbl_domains[SIZ];
227 char txt_answer[1024];
229 strcpy(message_to_spammer, "ok");
231 i = (const char *) addr;
232 a1 = ((*i++) & 0xff);
233 a2 = ((*i++) & 0xff);
234 a3 = ((*i++) & 0xff);
235 a4 = ((*i++) & 0xff);
237 /* See if we have any RBL domains configured */
238 num_rbl = get_hosts(rbl_domains, "rbl");
239 if (num_rbl < 1) return(0);
241 /* Try all configured RBL's */
242 for (rbl=0; rbl<num_rbl; ++rbl) {
243 snprintf(tbuf, sizeof tbuf,
246 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
248 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
249 sprintf(message_to_spammer, "5.7.1 %s", txt_answer);
250 lprintf(CTDL_INFO, "RBL: %s\n", txt_answer);
260 * Check to see if the client host is on some sort of spam list (RBL)
261 * If spammer, returns nonzero and places reason in 'message_to_spammer'
263 int rbl_check(char *message_to_spammer) {
264 struct sockaddr_in sin;
265 int len; /* should be socklen_t but doesn't work on Macintosh */
267 if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, (socklen_t *)&len)) {
268 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
274 * Convert a host name to a dotted quad address.
275 * Returns zero on success or nonzero on failure.
277 int hostname_to_dotted_quad(char *addr, char *host) {
282 ch = gethostbyname(host);
284 strcpy(addr, "0.0.0.0");
288 i = (const char *) ch->h_addr_list[0];
289 a1 = ((*i++) & 0xff);
290 a2 = ((*i++) & 0xff);
291 a3 = ((*i++) & 0xff);
292 a4 = ((*i++) & 0xff);
293 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);