* Rewrote part of the RBL check in a more portable way. It was completely failing...
[citadel.git] / citadel / locate_host.c
1 /*
2  * $Id$
3  *
4  * locate the originating host
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <limits.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <libcitadel.h>
23 #include "citadel.h"
24 #include "server.h"
25 #include "locate_host.h"
26 #include "sysdep_decls.h"
27 #include "config.h"
28 #include "domain.h"
29 #include "ctdl_module.h"
30
31 #ifdef HAVE_RESOLV_H
32 #include <arpa/nameser.h>
33 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
34 #include <arpa/nameser_compat.h>
35 #endif
36 #include <resolv.h>
37 #endif
38
39
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
42  */
43
44 #ifndef NS_INT16SZ
45 # ifdef INT16SZ
46 #  define NS_INT16SZ INT16SZ
47 # endif
48 #endif
49
50 #ifndef NS_INT32SZ
51 # ifdef INT32SZ
52 #  define NS_INT32SZ INT32SZ
53 # endif
54 #endif
55
56 #ifndef NS_GET16
57 # ifdef GETSHORT
58 #  define NS_GET16 GETSHORT
59 # endif
60 #endif
61
62
63 /***************************************************************************/
64
65
66 void locate_host(char *tbuf, size_t n,
67                 char *abuf, size_t na,
68                 const struct in_addr *addr)
69 {
70         struct hostent *ch;
71         const char *i;
72         char *j;
73         int a1, a2, a3, a4;
74         char address_string[SIZ];
75
76
77 #ifdef HAVE_NONREENTRANT_NETDB
78         begin_critical_section(S_NETDB);
79 #endif
80
81         i = (const char *) addr;
82         a1 = ((*i++) & 0xff);
83         a2 = ((*i++) & 0xff);
84         a3 = ((*i++) & 0xff);
85         a4 = ((*i++) & 0xff);
86         sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
87
88         if (abuf != NULL) {
89                 safestrncpy(abuf, address_string, na);
90         }
91
92         if ((ch = gethostbyaddr((const char *) addr,
93            sizeof(*addr), AF_INET)) == NULL) {
94 bad_dns:
95                 safestrncpy(tbuf, address_string, n);
96                 goto end;       /* because we might need to end the critical
97                                    section */
98         }
99         /* check if the forward DNS agrees; if not, they're spoofing */
100         j = strdup(ch->h_name);
101         ch = gethostbyname(j);
102         free(j);
103         if (ch == NULL)
104                 goto bad_dns;
105
106         /* check address for consistency */
107         for (; *ch->h_addr_list; ch->h_addr_list++)
108                 if (!memcmp(*ch->h_addr_list, addr,
109                             sizeof *addr)) {
110                         safestrncpy(tbuf, ch->h_name, 63);
111                         goto end;
112                 }
113         goto bad_dns;           /* they were spoofing. report a numeric IP
114                                    address. */
115
116       end:
117
118 #ifdef HAVE_NONREENTRANT_NETDB
119         end_critical_section(S_NETDB);
120 #endif
121
122         tbuf[63] = 0;
123 }
124
125
126 /*
127  * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
128  */
129 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
130 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
131         int a, b, c;
132         char *result = NULL;
133         u_char fixedans[ PACKETSZ ];
134         u_char *answer;
135         int need_to_free_answer = 0;
136         const u_char *cp;
137         u_char *rp;
138         const u_char *cend;
139         const u_char *rend;
140         int len;
141         char *p = NULL;
142
143         /* Make our DNS query. */
144         //res_init();
145         answer = fixedans;
146         if (CtdlThreadCheckStop())
147         {
148                 if (txtbuf != NULL)
149                         snprintf(txtbuf, txtbufsize, "System shutting down");
150                 return (1);
151         }
152         len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
153
154         /* Was there a problem? If so, the domain doesn't exist. */
155         if( len == -1 ) {
156                 if (txtbuf != NULL) {
157                         strcpy(txtbuf, "");
158                 }
159                 return(0);
160         }
161
162         if( len > PACKETSZ )
163         {
164                 answer = malloc( len );
165                 need_to_free_answer = 1;
166                 len = res_query( domain, C_IN, T_A, answer, len );
167                 if( len == -1 ) {
168                         if (txtbuf != NULL) {
169                                 snprintf(txtbuf, txtbufsize,
170                                         "Message rejected due to known spammer source IP address");
171                         }
172                         if (need_to_free_answer) free(answer);
173                         return(1);
174                 }
175         }
176         if (CtdlThreadCheckStop())
177         {
178                 if (txtbuf != NULL)
179                         snprintf(txtbuf, txtbufsize, "System shutting down");
180                 if (need_to_free_answer) free(answer);
181                 return (1);
182         }
183
184         result = ( char * )malloc( RESULT_SIZE );
185         result[ 0 ] = '\0';
186
187
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. */
191         res_init();
192         len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
193         if (CtdlThreadCheckStop())
194         {
195                 if (txtbuf != NULL)
196                         snprintf(txtbuf, txtbufsize, "System shutting down");
197                 if (need_to_free_answer) free(answer);
198                 free(result);
199                 return (1);
200         }
201
202         /* Just in case there's no TXT record... */
203         if( len == -1 )
204         {
205                 if (txtbuf != NULL) {
206                         snprintf(txtbuf, txtbufsize,
207                                 "Message rejected due to known spammer source IP address");
208                 }
209                 if (need_to_free_answer) free(answer);
210                 free(result);
211                 return(1);
212         }
213
214         /* Skip the header and the address we queried. */
215         cp = answer + sizeof( HEADER );
216         while( *cp != '\0' )
217         {
218                 a = *cp++;
219                 while( a-- )
220                         cp++;
221         }
222
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
227            enlighten me. */
228         cp += 1 + NS_INT16SZ + NS_INT32SZ;
229
230         /* Skip the type, class and ttl. */
231         cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
232
233         /* Get the length and end of the buffer. */
234         NS_GET16( c, cp );
235         cend = cp + c;
236
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 )
242         {
243                 a = *cp++;
244                 if( a != 0 )
245                         for( b = a; b > 0 && cp < cend && rp < rend;
246                           b-- )
247                         {
248                                 if( *cp == '\n' || *cp == '"' ||
249                                   *cp == '\\' )
250                                 {
251                                         *rp++ = '\\';
252                                 }
253                                 *rp++ = *cp++;
254                         }
255         }
256         *rp = '\0';
257         if (txtbuf != NULL) {
258                 snprintf(txtbuf, txtbufsize, "%s", result);
259         }
260         /* Remove nonprintable characters */
261         for (p=txtbuf; *p; ++p) {
262                 if (!isprint(*p)) strcpy(p, p+1);
263         }
264         if (need_to_free_answer) free(answer);
265         free(result);
266         return(1);
267 }
268
269
270 /*
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'
273  */
274 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
275 {
276         int a1, a2, a3, a4;
277         char tbuf[256];
278         int rbl;
279         int num_rbl;
280         char rbl_domains[SIZ];
281         char txt_answer[1024];
282         char dotted_quad[32];
283
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);
287
288         /* See if we have any RBL domains configured */
289         num_rbl = get_hosts(rbl_domains, "rbl");
290         if (num_rbl < 1) return(0);
291
292         /* Try all configured RBL's */
293         for (rbl=0; rbl<num_rbl; ++rbl) {
294                 snprintf(tbuf, sizeof tbuf,
295                         "%d.%d.%d.%d.",
296                         a4, a3, a2, a1);
297                 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
298
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);
302                         return(1);
303                 }
304         }
305
306         return(0);
307 }
308                         
309
310 /*
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'
313  *
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!
317  */
318 int rbl_check(char *message_to_spammer) {
319         int r;
320         struct sockaddr_in peer;
321         socklen_t peer_len = 0;
322
323         peer_len = sizeof(peer);
324         r = getpeername(CC->client_socket, &peer, &peer_len);
325         if (r == 0) {
326                 return(rbl_check_addr(&peer.sin_addr, message_to_spammer));
327         }
328         else {
329                 CtdlLogPrintf(CTDL_INFO, "RBL getpeername() failed: %s\n", strerror(errno));
330         }
331         return(0);
332 }
333
334 /*
335  * Convert a host name to a dotted quad address. 
336  * Returns zero on success or nonzero on failure.
337  */
338 int hostname_to_dotted_quad(char *addr, char *host) {
339         struct hostent *ch;
340         const char *i;
341         int a1, a2, a3, a4;
342
343         ch = gethostbyname(host);
344         if (ch == NULL) {
345                 strcpy(addr, "0.0.0.0");
346                 return(1);
347         }
348
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);
355         return(0);
356 }