4 * locate the originating host
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
22 #include <libcitadel.h>
25 #include "locate_host.h"
26 #include "sysdep_decls.h"
29 #include "ctdl_module.h"
32 #include <arpa/nameser.h>
33 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
34 #include <arpa/nameser_compat.h>
40 /* Hacks to work around nameser.h declarations missing on OpenBSD
41 * see also: http://search.cpan.org/src/MIKER/Net-DNS-ToolKit-0.30/ToolKit.h
46 # define NS_INT16SZ INT16SZ
52 # define NS_INT32SZ INT32SZ
58 # define NS_GET16 GETSHORT
63 /***************************************************************************/
66 void locate_host(char *tbuf, size_t n,
67 char *abuf, size_t na,
68 const struct in_addr *addr)
74 char address_string[SIZ];
77 #ifdef HAVE_NONREENTRANT_NETDB
78 begin_critical_section(S_NETDB);
81 i = (const char *) addr;
86 sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
89 safestrncpy(abuf, address_string, na);
92 if ((ch = gethostbyaddr((const char *) addr,
93 sizeof(*addr), AF_INET)) == NULL) {
95 safestrncpy(tbuf, address_string, n);
96 goto end; /* because we might need to end the critical
99 /* check if the forward DNS agrees; if not, they're spoofing */
100 j = strdup(ch->h_name);
101 ch = gethostbyname(j);
106 /* check address for consistency */
107 for (; *ch->h_addr_list; ch->h_addr_list++)
108 if (!memcmp(*ch->h_addr_list, addr,
110 safestrncpy(tbuf, ch->h_name, 63);
113 goto bad_dns; /* they were spoofing. report a numeric IP
118 #ifdef HAVE_NONREENTRANT_NETDB
119 end_critical_section(S_NETDB);
127 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
129 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
130 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
133 u_char fixedans[ PACKETSZ ];
135 int need_to_free_answer = 0;
143 /* Make our DNS query. */
146 if (CtdlThreadCheckStop())
149 snprintf(txtbuf, txtbufsize, "System shutting down");
152 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
154 /* Was there a problem? If so, the domain doesn't exist. */
156 if (txtbuf != NULL) {
164 answer = malloc( len );
165 need_to_free_answer = 1;
166 len = res_query( domain, C_IN, T_A, answer, len );
168 if (txtbuf != NULL) {
169 snprintf(txtbuf, txtbufsize,
170 "Message rejected due to known spammer source IP address");
172 if (need_to_free_answer) free(answer);
176 if (CtdlThreadCheckStop())
179 snprintf(txtbuf, txtbufsize, "System shutting down");
180 if (need_to_free_answer) free(answer);
184 result = ( char * )malloc( RESULT_SIZE );
188 /* Make another DNS query for textual data; this shouldn't
189 be a performance hit, since it'll now be cached at the
190 nameserver we're using. */
192 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
193 if (CtdlThreadCheckStop())
196 snprintf(txtbuf, txtbufsize, "System shutting down");
197 if (need_to_free_answer) free(answer);
202 /* Just in case there's no TXT record... */
205 if (txtbuf != NULL) {
206 snprintf(txtbuf, txtbufsize,
207 "Message rejected due to known spammer source IP address");
209 if (need_to_free_answer) free(answer);
214 /* Skip the header and the address we queried. */
215 cp = answer + sizeof( HEADER );
223 /* This seems to be a bit of magic data that we need to
224 skip. I wish there were good online documentation
225 for programming for libresolv, so I'd know what I'm
226 skipping here. Anyone reading this, feel free to
228 cp += 1 + NS_INT16SZ + NS_INT32SZ;
230 /* Skip the type, class and ttl. */
231 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
233 /* Get the length and end of the buffer. */
237 /* Iterate over any multiple answers we might have. In
238 this context, it's unlikely, but anyway. */
239 rp = (u_char *) result;
240 rend = (u_char *) result + RESULT_SIZE - 1;
241 while( cp < cend && rp < rend )
245 for( b = a; b > 0 && cp < cend && rp < rend;
248 if( *cp == '\n' || *cp == '"' ||
257 if (txtbuf != NULL) {
258 snprintf(txtbuf, txtbufsize, "%s", result);
260 /* Remove nonprintable characters */
261 for (p=txtbuf; *p; ++p) {
262 if (!isprint(*p)) strcpy(p, p+1);
264 if (need_to_free_answer) free(answer);
271 * Check to see if a host is on some sort of spam list (RBL)
272 * If spammer, returns nonzero and places reason in 'message_to_spammer'
274 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
280 char rbl_domains[SIZ];
281 char txt_answer[1024];
282 char dotted_quad[32];
284 strcpy(message_to_spammer, "ok");
285 safestrncpy(dotted_quad, inet_ntoa(*addr), sizeof dotted_quad);
286 sscanf(dotted_quad, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
288 /* See if we have any RBL domains configured */
289 num_rbl = get_hosts(rbl_domains, "rbl");
290 if (num_rbl < 1) return(0);
292 /* Try all configured RBL's */
293 for (rbl=0; rbl<num_rbl; ++rbl) {
294 snprintf(tbuf, sizeof tbuf,
297 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
299 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
300 sprintf(message_to_spammer, "5.7.1 %s", txt_answer);
301 CtdlLogPrintf(CTDL_INFO, "RBL: %s\n", txt_answer);
311 * Check to see if the client host is on some sort of spam list (RBL)
312 * If spammer, returns nonzero and places reason in 'message_to_spammer'
314 * PORTABILITY NOTE! I've made my best effort to rewrite this in a portable fashion.
315 * If anyone makes changes to this function, please shout-out so we can test it to
316 * make sure it didn't break on Linux!
318 int rbl_check(char *message_to_spammer) {
320 struct sockaddr_in peer;
321 socklen_t peer_len = 0;
323 peer_len = sizeof(peer);
324 r = getpeername(CC->client_socket, &peer, &peer_len);
326 return(rbl_check_addr(&peer.sin_addr, message_to_spammer));
329 CtdlLogPrintf(CTDL_INFO, "RBL getpeername() failed: %s\n", strerror(errno));
335 * Convert a host name to a dotted quad address.
336 * Returns zero on success or nonzero on failure.
338 int hostname_to_dotted_quad(char *addr, char *host) {
343 ch = gethostbyname(host);
345 strcpy(addr, "0.0.0.0");
349 i = (const char *) ch->h_addr_list[0];
350 a1 = ((*i++) & 0xff);
351 a2 = ((*i++) & 0xff);
352 a3 = ((*i++) & 0xff);
353 a4 = ((*i++) & 0xff);
354 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);