4 * locate the originating host
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
22 #include "serv_extensions.h"
23 #include "locate_host.h"
24 #include "sysdep_decls.h"
30 #include <arpa/nameser.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
38 void locate_host(char *tbuf, size_t n,
39 char *abuf, size_t na,
40 const struct in_addr *addr)
46 char address_string[SIZ];
49 #ifdef HAVE_NONREENTRANT_NETDB
50 begin_critical_section(S_NETDB);
53 i = (const char *) addr;
58 sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
61 safestrncpy(abuf, address_string, na);
64 if ((ch = gethostbyaddr((const char *) addr,
65 sizeof(*addr), AF_INET)) == NULL) {
67 safestrncpy(tbuf, address_string, n);
68 goto end; /* because we might need to end the critical
71 /* check if the forward DNS agrees; if not, they're spoofing */
72 j = strdup(ch->h_name);
73 ch = gethostbyname(j);
78 /* check address for consistency */
79 for (; *ch->h_addr_list; ch->h_addr_list++)
80 if (!memcmp(*ch->h_addr_list, addr,
82 safestrncpy(tbuf, ch->h_name, 63);
85 goto bad_dns; /* they were spoofing. report a numeric IP
90 #ifdef HAVE_NONREENTRANT_NETDB
91 end_critical_section(S_NETDB);
99 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
101 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
102 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
105 u_char fixedans[ PACKETSZ ];
107 int need_to_free_answer = 0;
115 /* Make our DNS query. */
118 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
120 /* Was there a problem? If so, the domain doesn't exist. */
122 if (txtbuf != NULL) {
130 answer = malloc( len );
131 need_to_free_answer = 1;
132 len = res_query( domain, C_IN, T_A, answer, len );
134 if (txtbuf != NULL) {
135 snprintf(txtbuf, txtbufsize,
136 "Message rejected due to known spammer source IP address");
138 if (need_to_free_answer) free(answer);
143 result = ( char * )malloc( RESULT_SIZE );
147 /* Make another DNS query for textual data; this shouldn't
148 be a performance hit, since it'll now be cached at the
149 nameserver we're using. */
151 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
153 /* Just in case there's no TXT record... */
156 if (txtbuf != NULL) {
157 snprintf(txtbuf, txtbufsize,
158 "Message rejected due to known spammer source IP address");
160 if (need_to_free_answer) free(answer);
165 /* Skip the header and the address we queried. */
166 cp = answer + sizeof( HEADER );
174 /* This seems to be a bit of magic data that we need to
175 skip. I wish there were good online documentation
176 for programming for libresolv, so I'd know what I'm
177 skipping here. Anyone reading this, feel free to
179 cp += 1 + NS_INT16SZ + NS_INT32SZ;
181 /* Skip the type, class and ttl. */
182 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
184 /* Get the length and end of the buffer. */
188 /* Iterate over any multiple answers we might have. In
189 this context, it's unlikely, but anyway. */
190 rp = (u_char *) result;
191 rend = (u_char *) result + RESULT_SIZE - 1;
192 while( cp < cend && rp < rend )
196 for( b = a; b > 0 && cp < cend && rp < rend;
199 if( *cp == '\n' || *cp == '"' ||
208 if (txtbuf != NULL) {
209 snprintf(txtbuf, txtbufsize, "%s", result);
211 /* Remove nonprintable characters */
212 for (p=txtbuf; *p; ++p) {
213 if (!isprint(*p)) strcpy(p, p+1);
215 if (need_to_free_answer) free(answer);
222 * Check to see if a host is on some sort of spam list (RBL)
223 * If spammer, returns nonzero and places reason in 'message_to_spammer'
225 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
232 char rbl_domains[SIZ];
233 char txt_answer[1024];
235 strcpy(message_to_spammer, "ok");
237 i = (const char *) addr;
238 a1 = ((*i++) & 0xff);
239 a2 = ((*i++) & 0xff);
240 a3 = ((*i++) & 0xff);
241 a4 = ((*i++) & 0xff);
243 /* See if we have any RBL domains configured */
244 num_rbl = get_hosts(rbl_domains, "rbl");
245 if (num_rbl < 1) return(0);
247 /* Try all configured RBL's */
248 for (rbl=0; rbl<num_rbl; ++rbl) {
249 snprintf(tbuf, sizeof tbuf,
252 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
254 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
255 sprintf(message_to_spammer, "5.7.1 %s", txt_answer);
256 lprintf(CTDL_INFO, "RBL: %s\n", txt_answer);
266 * Check to see if the client host is on some sort of spam list (RBL)
267 * If spammer, returns nonzero and places reason in 'message_to_spammer'
269 int rbl_check(char *message_to_spammer) {
270 struct sockaddr_in sin;
271 int len; /* should be socklen_t but doesn't work on Macintosh */
273 if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, (socklen_t *)&len)) {
274 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
280 * Convert a host name to a dotted quad address.
281 * Returns zero on success or nonzero on failure.
283 int hostname_to_dotted_quad(char *addr, char *host) {
288 ch = gethostbyname(host);
290 strcpy(addr, "0.0.0.0");
294 i = (const char *) ch->h_addr_list[0];
295 a1 = ((*i++) & 0xff);
296 a2 = ((*i++) & 0xff);
297 a3 = ((*i++) & 0xff);
298 a4 = ((*i++) & 0xff);
299 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);