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