4 * Functions which handle hostname/address lookups and resolution
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"
30 #include "ctdl_module.h"
33 #include <arpa/nameser.h>
34 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
35 #include <arpa/nameser_compat.h>
41 /* Hacks to work around nameser.h declarations missing on OpenBSD
42 * see also: http://search.cpan.org/src/MIKER/Net-DNS-ToolKit-0.30/ToolKit.h
47 # define NS_INT16SZ INT16SZ
53 # define NS_INT32SZ INT32SZ
59 # define NS_GET16 GETSHORT
64 /***************************************************************************/
67 void locate_host(char *tbuf, size_t n,
68 char *abuf, size_t na,
69 const struct in_addr *addr)
75 char address_string[SIZ];
78 #ifdef HAVE_NONREENTRANT_NETDB
79 begin_critical_section(S_NETDB);
82 i = (const char *) addr;
87 sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
90 safestrncpy(abuf, address_string, na);
93 if ((ch = gethostbyaddr((const char *) addr,
94 sizeof(*addr), AF_INET)) == NULL) {
96 safestrncpy(tbuf, address_string, n);
97 goto end; /* because we might need to end the critical
100 /* check if the forward DNS agrees; if not, they're spoofing */
101 j = strdup(ch->h_name);
102 ch = gethostbyname(j);
107 /* check address for consistency */
108 for (; *ch->h_addr_list; ch->h_addr_list++)
109 if (!memcmp(*ch->h_addr_list, addr,
111 safestrncpy(tbuf, ch->h_name, 63);
114 goto bad_dns; /* they were spoofing. report a numeric IP
119 #ifdef HAVE_NONREENTRANT_NETDB
120 end_critical_section(S_NETDB);
128 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
130 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
131 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
134 u_char fixedans[ PACKETSZ ];
136 int need_to_free_answer = 0;
144 /* Make our DNS query. */
147 if (CtdlThreadCheckStop())
150 snprintf(txtbuf, txtbufsize, "System shutting down");
153 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
155 /* Was there a problem? If so, the domain doesn't exist. */
157 if (txtbuf != NULL) {
165 answer = malloc( len );
166 need_to_free_answer = 1;
167 len = res_query( domain, C_IN, T_A, answer, len );
169 if (txtbuf != NULL) {
170 snprintf(txtbuf, txtbufsize,
171 "Message rejected due to known spammer source IP address");
173 if (need_to_free_answer) free(answer);
177 if (CtdlThreadCheckStop())
180 snprintf(txtbuf, txtbufsize, "System shutting down");
181 if (need_to_free_answer) free(answer);
185 result = ( char * )malloc( RESULT_SIZE );
189 /* Make another DNS query for textual data; this shouldn't
190 be a performance hit, since it'll now be cached at the
191 nameserver we're using. */
193 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
194 if (CtdlThreadCheckStop())
197 snprintf(txtbuf, txtbufsize, "System shutting down");
198 if (need_to_free_answer) free(answer);
203 /* Just in case there's no TXT record... */
206 if (txtbuf != NULL) {
207 snprintf(txtbuf, txtbufsize,
208 "Message rejected due to known spammer source IP address");
210 if (need_to_free_answer) free(answer);
215 /* Skip the header and the address we queried. */
216 cp = answer + sizeof( HEADER );
224 /* This seems to be a bit of magic data that we need to
225 skip. I wish there were good online documentation
226 for programming for libresolv, so I'd know what I'm
227 skipping here. Anyone reading this, feel free to
229 cp += 1 + NS_INT16SZ + NS_INT32SZ;
231 /* Skip the type, class and ttl. */
232 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
234 /* Get the length and end of the buffer. */
238 /* Iterate over any multiple answers we might have. In
239 this context, it's unlikely, but anyway. */
240 rp = (u_char *) result;
241 rend = (u_char *) result + RESULT_SIZE - 1;
242 while( cp < cend && rp < rend )
246 for( b = a; b > 0 && cp < cend && rp < rend;
249 if( *cp == '\n' || *cp == '"' ||
258 if (txtbuf != NULL) {
259 snprintf(txtbuf, txtbufsize, "%s", result);
261 /* Remove nonprintable characters */
262 for (p=txtbuf; *p; ++p) {
263 if (!isprint(*p)) strcpy(p, p+1);
265 if (need_to_free_answer) free(answer);
272 * Check to see if a host is on some sort of spam list (RBL)
273 * If spammer, returns nonzero and places reason in 'message_to_spammer'
275 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
281 char rbl_domains[SIZ];
282 char txt_answer[1024];
283 char dotted_quad[32];
285 strcpy(message_to_spammer, "ok");
286 safestrncpy(dotted_quad, inet_ntoa(*addr), sizeof dotted_quad);
287 sscanf(dotted_quad, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
289 /* See if we have any RBL domains configured */
290 num_rbl = get_hosts(rbl_domains, "rbl");
291 if (num_rbl < 1) return(0);
293 /* Try all configured RBL's */
294 for (rbl=0; rbl<num_rbl; ++rbl) {
295 snprintf(tbuf, sizeof tbuf,
298 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
300 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
301 strcpy(message_to_spammer, txt_answer);
302 CtdlLogPrintf(CTDL_INFO, "RBL: %s\n", txt_answer);
312 * Check to see if the client host is on some sort of spam list (RBL)
313 * If spammer, returns nonzero and places reason in 'message_to_spammer'
315 * PORTABILITY NOTE! I've made my best effort to rewrite this in a portable fashion.
316 * If anyone makes changes to this function, please shout-out so we can test it to
317 * make sure it didn't break on Linux!
319 int rbl_check(char *message_to_spammer) {
321 struct sockaddr_in peer;
322 socklen_t peer_len = 0;
324 peer_len = sizeof(peer);
325 r = getpeername(CC->client_socket, &peer, &peer_len);
327 return(rbl_check_addr(&peer.sin_addr, message_to_spammer));
330 CtdlLogPrintf(CTDL_INFO, "RBL getpeername() failed: %s\n", strerror(errno));
336 * Convert a host name to a dotted quad address.
337 * Returns zero on success or nonzero on failure.
339 int hostname_to_dotted_quad(char *addr, char *host) {
344 ch = gethostbyname(host);
346 strcpy(addr, "0.0.0.0");
350 i = (const char *) ch->h_addr_list[0];
351 a1 = ((*i++) & 0xff);
352 a2 = ((*i++) & 0xff);
353 a3 = ((*i++) & 0xff);
354 a4 = ((*i++) & 0xff);
355 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);