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);
97 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
99 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
102 u_char fixedans[ PACKETSZ ];
104 int need_to_free_answer = 0;
111 /* Make our DNS query. */
114 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
116 /* Was there a problem? If so, the domain doesn't exist. */
118 if (txtbuf != NULL) {
126 answer = malloc( len );
127 need_to_free_answer = 1;
128 len = res_query( domain, C_IN, T_A, answer, len );
130 if (txtbuf != NULL) {
131 snprintf(txtbuf, txtbufsize,
132 "Message rejected due to known spammer source IP address");
134 if (need_to_free_answer) free(answer);
139 result = ( char * )malloc( RESULT_SIZE );
143 /* Make another DNS query for textual data; this shouldn't
144 be a performance hit, since it'll now be cached at the
145 nameserver we're using. */
147 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
149 /* Just in case there's no TXT record... */
152 if (txtbuf != NULL) {
153 snprintf(txtbuf, txtbufsize,
154 "Message rejected due to known spammer source IP address");
156 if (need_to_free_answer) free(answer);
161 /* Skip the header and the address we queried. */
162 cp = answer + sizeof( HEADER );
170 /* This seems to be a bit of magic data that we need to
171 skip. I wish there were good online documentation
172 for programming for libresolv, so I'd know what I'm
173 skipping here. Anyone reading this, feel free to
175 cp += 1 + NS_INT16SZ + NS_INT32SZ;
177 /* Skip the type, class and ttl. */
178 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
180 /* Get the length and end of the buffer. */
184 /* Iterate over any multiple answers we might have. In
185 this context, it's unlikely, but anyway. */
187 rend = result + RESULT_SIZE - 1;
188 while( cp < cend && rp < rend )
192 for( b = a; b > 0 && cp < cend && rp < rend;
195 if( *cp == '\n' || *cp == '"' ||
204 if (txtbuf != NULL) {
205 snprintf(txtbuf, txtbufsize, "%s", result);
207 if (need_to_free_answer) free(answer);
214 * Check to see if a host is on some sort of spam list (RBL)
215 * If spammer, returns nonzero and places reason in 'message_to_spammer'
217 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
224 char rbl_domains[SIZ];
225 char txt_answer[1024];
227 strcpy(message_to_spammer, "ok");
229 i = (const char *) addr;
230 a1 = ((*i++) & 0xff);
231 a2 = ((*i++) & 0xff);
232 a3 = ((*i++) & 0xff);
233 a4 = ((*i++) & 0xff);
235 /* See if we have any RBL domains configured */
236 num_rbl = get_hosts(rbl_domains, "rbl");
237 if (num_rbl < 1) return(0);
239 /* Try all configured RBL's */
240 for (rbl=0; rbl<num_rbl; ++rbl) {
241 snprintf(tbuf, sizeof tbuf,
244 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
246 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
247 sprintf(message_to_spammer, "5.7.1 %s", txt_answer);
248 lprintf(CTDL_INFO, "RBL: %s\n", txt_answer);
258 * Check to see if the client host is on some sort of spam list (RBL)
259 * If spammer, returns nonzero and places reason in 'message_to_spammer'
261 int rbl_check(char *message_to_spammer) {
262 struct sockaddr_in sin;
263 int len; /* should be socklen_t but doesn't work on Macintosh */
265 if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, (socklen_t *)&len)) {
266 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
272 * Convert a host name to a dotted quad address.
273 * Returns zero on success or nonzero on failure.
275 int hostname_to_dotted_quad(char *addr, char *host) {
280 ch = gethostbyname(host);
282 strcpy(addr, "0.0.0.0");
286 i = (const char *) ch->h_addr_list[0];
287 a1 = ((*i++) & 0xff);
288 a2 = ((*i++) & 0xff);
289 a3 = ((*i++) & 0xff);
290 a4 = ((*i++) & 0xff);
291 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);