4 * locate the originating host
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.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 /* Hacks to work around nameser.h declarations missing on OpenBSD
38 * see also: http://search.cpan.org/src/MIKER/Net-DNS-ToolKit-0.30/ToolKit.h
43 # define NS_INT16SZ INT16SZ
49 # define NS_INT32SZ INT32SZ
55 # define NS_GET16 GETSHORT
60 /***************************************************************************/
63 void locate_host(char *tbuf, size_t n,
64 char *abuf, size_t na,
65 const struct in_addr *addr)
71 char address_string[SIZ];
74 #ifdef HAVE_NONREENTRANT_NETDB
75 begin_critical_section(S_NETDB);
78 i = (const char *) addr;
83 sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
86 safestrncpy(abuf, address_string, na);
89 if ((ch = gethostbyaddr((const char *) addr,
90 sizeof(*addr), AF_INET)) == NULL) {
92 safestrncpy(tbuf, address_string, n);
93 goto end; /* because we might need to end the critical
96 /* check if the forward DNS agrees; if not, they're spoofing */
97 j = strdup(ch->h_name);
98 ch = gethostbyname(j);
103 /* check address for consistency */
104 for (; *ch->h_addr_list; ch->h_addr_list++)
105 if (!memcmp(*ch->h_addr_list, addr,
107 safestrncpy(tbuf, ch->h_name, 63);
110 goto bad_dns; /* they were spoofing. report a numeric IP
115 #ifdef HAVE_NONREENTRANT_NETDB
116 end_critical_section(S_NETDB);
124 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
126 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
127 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
130 u_char fixedans[ PACKETSZ ];
132 int need_to_free_answer = 0;
140 /* Make our DNS query. */
143 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
145 /* Was there a problem? If so, the domain doesn't exist. */
147 if (txtbuf != NULL) {
155 answer = malloc( len );
156 need_to_free_answer = 1;
157 len = res_query( domain, C_IN, T_A, answer, len );
159 if (txtbuf != NULL) {
160 snprintf(txtbuf, txtbufsize,
161 "Message rejected due to known spammer source IP address");
163 if (need_to_free_answer) free(answer);
168 result = ( char * )malloc( RESULT_SIZE );
172 /* Make another DNS query for textual data; this shouldn't
173 be a performance hit, since it'll now be cached at the
174 nameserver we're using. */
176 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
178 /* Just in case there's no TXT record... */
181 if (txtbuf != NULL) {
182 snprintf(txtbuf, txtbufsize,
183 "Message rejected due to known spammer source IP address");
185 if (need_to_free_answer) free(answer);
190 /* Skip the header and the address we queried. */
191 cp = answer + sizeof( HEADER );
199 /* This seems to be a bit of magic data that we need to
200 skip. I wish there were good online documentation
201 for programming for libresolv, so I'd know what I'm
202 skipping here. Anyone reading this, feel free to
204 cp += 1 + NS_INT16SZ + NS_INT32SZ;
206 /* Skip the type, class and ttl. */
207 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
209 /* Get the length and end of the buffer. */
213 /* Iterate over any multiple answers we might have. In
214 this context, it's unlikely, but anyway. */
215 rp = (u_char *) result;
216 rend = (u_char *) result + RESULT_SIZE - 1;
217 while( cp < cend && rp < rend )
221 for( b = a; b > 0 && cp < cend && rp < rend;
224 if( *cp == '\n' || *cp == '"' ||
233 if (txtbuf != NULL) {
234 snprintf(txtbuf, txtbufsize, "%s", result);
236 /* Remove nonprintable characters */
237 for (p=txtbuf; *p; ++p) {
238 if (!isprint(*p)) strcpy(p, p+1);
240 if (need_to_free_answer) free(answer);
247 * Check to see if a host is on some sort of spam list (RBL)
248 * If spammer, returns nonzero and places reason in 'message_to_spammer'
250 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
257 char rbl_domains[SIZ];
258 char txt_answer[1024];
260 strcpy(message_to_spammer, "ok");
262 i = (const char *) addr;
263 a1 = ((*i++) & 0xff);
264 a2 = ((*i++) & 0xff);
265 a3 = ((*i++) & 0xff);
266 a4 = ((*i++) & 0xff);
268 /* See if we have any RBL domains configured */
269 num_rbl = get_hosts(rbl_domains, "rbl");
270 if (num_rbl < 1) return(0);
272 /* Try all configured RBL's */
273 for (rbl=0; rbl<num_rbl; ++rbl) {
274 snprintf(tbuf, sizeof tbuf,
277 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
279 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
280 sprintf(message_to_spammer, "5.7.1 %s", txt_answer);
281 lprintf(CTDL_INFO, "RBL: %s\n", txt_answer);
291 * Check to see if the client host is on some sort of spam list (RBL)
292 * If spammer, returns nonzero and places reason in 'message_to_spammer'
294 int rbl_check(char *message_to_spammer) {
295 struct sockaddr_in sin;
296 int len; /* should be socklen_t but doesn't work on Macintosh */
299 memset (&sin, 0, sizeof (struct sockaddr_in));
300 if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, (socklen_t *)&len)) {
301 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
307 * Convert a host name to a dotted quad address.
308 * Returns zero on success or nonzero on failure.
310 int hostname_to_dotted_quad(char *addr, char *host) {
315 ch = gethostbyname(host);
317 strcpy(addr, "0.0.0.0");
321 i = (const char *) ch->h_addr_list[0];
322 a1 = ((*i++) & 0xff);
323 a2 = ((*i++) & 0xff);
324 a3 = ((*i++) & 0xff);
325 a4 = ((*i++) & 0xff);
326 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);