4 * locate the originating host
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
20 #include <libcitadel.h>
23 #include "locate_host.h"
24 #include "sysdep_decls.h"
27 #include "ctdl_module.h"
30 #include <arpa/nameser.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
38 /* Hacks to work around nameser.h declarations missing on OpenBSD
39 * see also: http://search.cpan.org/src/MIKER/Net-DNS-ToolKit-0.30/ToolKit.h
44 # define NS_INT16SZ INT16SZ
50 # define NS_INT32SZ INT32SZ
56 # define NS_GET16 GETSHORT
61 /***************************************************************************/
64 void locate_host(char *tbuf
, size_t n
,
65 char *abuf
, size_t na
,
66 const struct in_addr
*addr
)
72 char address_string
[SIZ
];
75 #ifdef HAVE_NONREENTRANT_NETDB
76 begin_critical_section(S_NETDB
);
79 i
= (const char *) addr
;
84 sprintf(address_string
, "%d.%d.%d.%d", a1
, a2
, a3
, a4
);
87 safestrncpy(abuf
, address_string
, na
);
90 if ((ch
= gethostbyaddr((const char *) addr
,
91 sizeof(*addr
), AF_INET
)) == NULL
) {
93 safestrncpy(tbuf
, address_string
, n
);
94 goto end
; /* because we might need to end the critical
97 /* check if the forward DNS agrees; if not, they're spoofing */
98 j
= strdup(ch
->h_name
);
99 ch
= gethostbyname(j
);
104 /* check address for consistency */
105 for (; *ch
->h_addr_list
; ch
->h_addr_list
++)
106 if (!memcmp(*ch
->h_addr_list
, addr
,
108 safestrncpy(tbuf
, ch
->h_name
, 63);
111 goto bad_dns
; /* they were spoofing. report a numeric IP
116 #ifdef HAVE_NONREENTRANT_NETDB
117 end_critical_section(S_NETDB
);
125 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
127 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
128 int rblcheck_backend(char *domain
, char *txtbuf
, int txtbufsize
) {
131 u_char fixedans
[ PACKETSZ
];
133 int need_to_free_answer
= 0;
141 /* Make our DNS query. */
144 if (CtdlThreadCheckStop())
147 snprintf(txtbuf
, txtbufsize
, "System shutting down");
150 len
= res_query( domain
, C_IN
, T_A
, answer
, PACKETSZ
);
152 /* Was there a problem? If so, the domain doesn't exist. */
154 if (txtbuf
!= NULL
) {
162 answer
= malloc( len
);
163 need_to_free_answer
= 1;
164 len
= res_query( domain
, C_IN
, T_A
, answer
, len
);
166 if (txtbuf
!= NULL
) {
167 snprintf(txtbuf
, txtbufsize
,
168 "Message rejected due to known spammer source IP address");
170 if (need_to_free_answer
) free(answer
);
174 if (CtdlThreadCheckStop())
177 snprintf(txtbuf
, txtbufsize
, "System shutting down");
178 if (need_to_free_answer
) free(answer
);
182 result
= ( char * )malloc( RESULT_SIZE
);
186 /* Make another DNS query for textual data; this shouldn't
187 be a performance hit, since it'll now be cached at the
188 nameserver we're using. */
190 len
= res_query( domain
, C_IN
, T_TXT
, answer
, PACKETSZ
);
191 if (CtdlThreadCheckStop())
194 snprintf(txtbuf
, txtbufsize
, "System shutting down");
195 if (need_to_free_answer
) free(answer
);
200 /* Just in case there's no TXT record... */
203 if (txtbuf
!= NULL
) {
204 snprintf(txtbuf
, txtbufsize
,
205 "Message rejected due to known spammer source IP address");
207 if (need_to_free_answer
) free(answer
);
212 /* Skip the header and the address we queried. */
213 cp
= answer
+ sizeof( HEADER
);
221 /* This seems to be a bit of magic data that we need to
222 skip. I wish there were good online documentation
223 for programming for libresolv, so I'd know what I'm
224 skipping here. Anyone reading this, feel free to
226 cp
+= 1 + NS_INT16SZ
+ NS_INT32SZ
;
228 /* Skip the type, class and ttl. */
229 cp
+= ( NS_INT16SZ
* 2 ) + NS_INT32SZ
;
231 /* Get the length and end of the buffer. */
235 /* Iterate over any multiple answers we might have. In
236 this context, it's unlikely, but anyway. */
237 rp
= (u_char
*) result
;
238 rend
= (u_char
*) result
+ RESULT_SIZE
- 1;
239 while( cp
< cend
&& rp
< rend
)
243 for( b
= a
; b
> 0 && cp
< cend
&& rp
< rend
;
246 if( *cp
== '\n' || *cp
== '"' ||
255 if (txtbuf
!= NULL
) {
256 snprintf(txtbuf
, txtbufsize
, "%s", result
);
258 /* Remove nonprintable characters */
259 for (p
=txtbuf
; *p
; ++p
) {
260 if (!isprint(*p
)) strcpy(p
, p
+1);
262 if (need_to_free_answer
) free(answer
);
269 * Check to see if a host is on some sort of spam list (RBL)
270 * If spammer, returns nonzero and places reason in 'message_to_spammer'
272 int rbl_check_addr(struct in_addr
*addr
, char *message_to_spammer
)
279 char rbl_domains
[SIZ
];
280 char txt_answer
[1024];
282 strcpy(message_to_spammer
, "ok");
284 i
= (const char *) addr
;
285 a1
= ((*i
++) & 0xff);
286 a2
= ((*i
++) & 0xff);
287 a3
= ((*i
++) & 0xff);
288 a4
= ((*i
++) & 0xff);
290 /* See if we have any RBL domains configured */
291 num_rbl
= get_hosts(rbl_domains
, "rbl");
292 if (num_rbl
< 1) return(0);
294 /* Try all configured RBL's */
295 for (rbl
=0; rbl
<num_rbl
; ++rbl
) {
296 snprintf(tbuf
, sizeof tbuf
,
299 extract_token(&tbuf
[strlen(tbuf
)], rbl_domains
, rbl
, '|', (sizeof tbuf
- strlen(tbuf
)));
301 if (rblcheck_backend(tbuf
, txt_answer
, sizeof txt_answer
)) {
302 sprintf(message_to_spammer
, "5.7.1 %s", txt_answer
);
303 CtdlLogPrintf(CTDL_INFO
, "RBL: %s\n", txt_answer
);
313 * Check to see if the client host is on some sort of spam list (RBL)
314 * If spammer, returns nonzero and places reason in 'message_to_spammer'
316 int rbl_check(char *message_to_spammer
) {
317 struct sockaddr_in sin
;
318 int len
; /* should be socklen_t but doesn't work on Macintosh */
319 struct timeval tv1
, tv2
;
320 suseconds_t total_time
= 0;
322 gettimeofday(&tv1
, NULL
);
324 memset (&sin
, 0, sizeof (struct sockaddr_in
));
325 if (!getpeername(CC
->client_socket
, (struct sockaddr
*) &sin
, (socklen_t
*)&len
)) {
326 return(rbl_check_addr(&sin
.sin_addr
, message_to_spammer
));
329 gettimeofday(&tv2
, NULL
);
330 total_time
= (tv2
.tv_usec
+ (tv2
.tv_sec
* 1000000)) - (tv1
.tv_usec
+ (tv1
.tv_sec
* 1000000));
331 CtdlLogPrintf(CTDL_DEBUG
, "RBL check completed in %ld.%ld seconds\n",
332 (total_time
/ 1000000),
333 (total_time
% 1000000)
339 * Convert a host name to a dotted quad address.
340 * Returns zero on success or nonzero on failure.
342 int hostname_to_dotted_quad(char *addr
, char *host
) {
347 ch
= gethostbyname(host
);
349 strcpy(addr
, "0.0.0.0");
353 i
= (const char *) ch
->h_addr_list
[0];
354 a1
= ((*i
++) & 0xff);
355 a2
= ((*i
++) & 0xff);
356 a3
= ((*i
++) & 0xff);
357 a4
= ((*i
++) & 0xff);
358 sprintf(addr
, "%d.%d.%d.%d", a1
, a2
, a3
, a4
);