da0594df489b9ea4375f21ec9d39a8a2db790164
[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 <limits.h>
18 #include <netdb.h>
19 #include <string.h>
20 #include "citadel.h"
21 #include "server.h"
22 #include "serv_extensions.h"
23 #include "locate_host.h"
24 #include "sysdep_decls.h"
25 #include "config.h"
26 #include "tools.h"
27 #include "domain.h"
28
29 #ifdef HAVE_RESOLV_H
30 #include <arpa/nameser.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
33 #endif
34 #include <resolv.h>
35 #endif
36
37
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
40  */
41
42 #ifndef NS_INT16SZ
43 # ifdef INT16SZ
44 #  define NS_INT16SZ INT16SZ
45 # endif
46 #endif
47
48 #ifndef NS_INT32SZ
49 # ifdef INT32SZ
50 #  define NS_INT32SZ INT32SZ
51 # endif
52 #endif
53
54 #ifndef NS_GET16
55 # ifdef GETSHORT
56 #  define NS_GET16 GETSHORT
57 # endif
58 #endif
59
60
61 /***************************************************************************/
62
63
64 void locate_host(char *tbuf, size_t n,
65                 char *abuf, size_t na,
66                 const struct in_addr *addr)
67 {
68         struct hostent *ch;
69         const char *i;
70         char *j;
71         int a1, a2, a3, a4;
72         char address_string[SIZ];
73
74
75 #ifdef HAVE_NONREENTRANT_NETDB
76         begin_critical_section(S_NETDB);
77 #endif
78
79         i = (const char *) addr;
80         a1 = ((*i++) & 0xff);
81         a2 = ((*i++) & 0xff);
82         a3 = ((*i++) & 0xff);
83         a4 = ((*i++) & 0xff);
84         sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
85
86         if (abuf != NULL) {
87                 safestrncpy(abuf, address_string, na);
88         }
89
90         if ((ch = gethostbyaddr((const char *) addr,
91            sizeof(*addr), AF_INET)) == NULL) {
92 bad_dns:
93                 safestrncpy(tbuf, address_string, n);
94                 goto end;       /* because we might need to end the critical
95                                    section */
96         }
97         /* check if the forward DNS agrees; if not, they're spoofing */
98         j = strdup(ch->h_name);
99         ch = gethostbyname(j);
100         free(j);
101         if (ch == NULL)
102                 goto bad_dns;
103
104         /* check address for consistency */
105         for (; *ch->h_addr_list; ch->h_addr_list++)
106                 if (!memcmp(*ch->h_addr_list, addr,
107                             sizeof *addr)) {
108                         safestrncpy(tbuf, ch->h_name, 63);
109                         goto end;
110                 }
111         goto bad_dns;           /* they were spoofing. report a numeric IP
112                                    address. */
113
114       end:
115
116 #ifdef HAVE_NONREENTRANT_NETDB
117         end_critical_section(S_NETDB);
118 #endif
119
120         tbuf[63] = 0;
121 }
122
123
124 /*
125  * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
126  */
127 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
128 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
129         int a, b, c;
130         char *result = NULL;
131         u_char fixedans[ PACKETSZ ];
132         u_char *answer;
133         int need_to_free_answer = 0;
134         const u_char *cp;
135         u_char *rp;
136         const u_char *cend;
137         const u_char *rend;
138         int len;
139         char *p = NULL;
140
141         /* Make our DNS query. */
142         //res_init();
143         answer = fixedans;
144         len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
145
146         /* Was there a problem? If so, the domain doesn't exist. */
147         if( len == -1 ) {
148                 if (txtbuf != NULL) {
149                         strcpy(txtbuf, "");
150                 }
151                 return(0);
152         }
153
154         if( len > PACKETSZ )
155         {
156                 answer = malloc( len );
157                 need_to_free_answer = 1;
158                 len = res_query( domain, C_IN, T_A, answer, len );
159                 if( len == -1 ) {
160                         if (txtbuf != NULL) {
161                                 snprintf(txtbuf, txtbufsize,
162                                         "Message rejected due to known spammer source IP address");
163                         }
164                         if (need_to_free_answer) free(answer);
165                         return(1);
166                 }
167         }
168
169         result = ( char * )malloc( RESULT_SIZE );
170         result[ 0 ] = '\0';
171
172
173         /* Make another DNS query for textual data; this shouldn't
174            be a performance hit, since it'll now be cached at the
175            nameserver we're using. */
176         res_init();
177         len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
178
179         /* Just in case there's no TXT record... */
180         if( len == -1 )
181         {
182                 if (txtbuf != NULL) {
183                         snprintf(txtbuf, txtbufsize,
184                                 "Message rejected due to known spammer source IP address");
185                 }
186                 if (need_to_free_answer) free(answer);
187                 free(result);
188                 return(1);
189         }
190
191         /* Skip the header and the address we queried. */
192         cp = answer + sizeof( HEADER );
193         while( *cp != '\0' )
194         {
195                 a = *cp++;
196                 while( a-- )
197                         cp++;
198         }
199
200         /* This seems to be a bit of magic data that we need to
201            skip. I wish there were good online documentation
202            for programming for libresolv, so I'd know what I'm
203            skipping here. Anyone reading this, feel free to
204            enlighten me. */
205         cp += 1 + NS_INT16SZ + NS_INT32SZ;
206
207         /* Skip the type, class and ttl. */
208         cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
209
210         /* Get the length and end of the buffer. */
211         NS_GET16( c, cp );
212         cend = cp + c;
213
214         /* Iterate over any multiple answers we might have. In
215            this context, it's unlikely, but anyway. */
216         rp = (u_char *) result;
217         rend = (u_char *) result + RESULT_SIZE - 1;
218         while( cp < cend && rp < rend )
219         {
220                 a = *cp++;
221                 if( a != 0 )
222                         for( b = a; b > 0 && cp < cend && rp < rend;
223                           b-- )
224                         {
225                                 if( *cp == '\n' || *cp == '"' ||
226                                   *cp == '\\' )
227                                 {
228                                         *rp++ = '\\';
229                                 }
230                                 *rp++ = *cp++;
231                         }
232         }
233         *rp = '\0';
234         if (txtbuf != NULL) {
235                 snprintf(txtbuf, txtbufsize, "%s", result);
236         }
237         /* Remove nonprintable characters */
238         for (p=txtbuf; *p; ++p) {
239                 if (!isprint(*p)) strcpy(p, p+1);
240         }
241         if (need_to_free_answer) free(answer);
242         free(result);
243         return(1);
244 }
245
246
247 /*
248  * Check to see if a host is on some sort of spam list (RBL)
249  * If spammer, returns nonzero and places reason in 'message_to_spammer'
250  */
251 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
252 {
253         const char *i;
254         int a1, a2, a3, a4;
255         char tbuf[256];
256         int rbl;
257         int num_rbl;
258         char rbl_domains[SIZ];
259         char txt_answer[1024];
260
261         strcpy(message_to_spammer, "ok");
262
263         i = (const char *) addr;
264         a1 = ((*i++) & 0xff);
265         a2 = ((*i++) & 0xff);
266         a3 = ((*i++) & 0xff);
267         a4 = ((*i++) & 0xff);
268
269         /* See if we have any RBL domains configured */
270         num_rbl = get_hosts(rbl_domains, "rbl");
271         if (num_rbl < 1) return(0);
272
273         /* Try all configured RBL's */
274         for (rbl=0; rbl<num_rbl; ++rbl) {
275                 snprintf(tbuf, sizeof tbuf,
276                         "%d.%d.%d.%d.",
277                         a4, a3, a2, a1);
278                 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
279
280                 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
281                         sprintf(message_to_spammer, "5.7.1 %s", txt_answer);
282                         lprintf(CTDL_INFO, "RBL: %s\n", txt_answer);
283                         return(1);
284                 }
285         }
286
287         return(0);
288 }
289                         
290
291 /*
292  * Check to see if the client host is on some sort of spam list (RBL)
293  * If spammer, returns nonzero and places reason in 'message_to_spammer'
294  */
295 int rbl_check(char *message_to_spammer) {
296         struct sockaddr_in sin;
297         int len;        /* should be socklen_t but doesn't work on Macintosh */
298
299         if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, (socklen_t *)&len)) {
300                 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
301         }
302         return(0);
303 }
304
305 /*
306  * Convert a host name to a dotted quad address. 
307  * Returns zero on success or nonzero on failure.
308  */
309 int hostname_to_dotted_quad(char *addr, char *host) {
310         struct hostent *ch;
311         const char *i;
312         int a1, a2, a3, a4;
313
314         ch = gethostbyname(host);
315         if (ch == NULL) {
316                 strcpy(addr, "0.0.0.0");
317                 return(1);
318         }
319
320         i = (const char *) ch->h_addr_list[0];
321         a1 = ((*i++) & 0xff);
322         a2 = ((*i++) & 0xff);
323         a3 = ((*i++) & 0xff);
324         a4 = ((*i++) & 0xff);
325         sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);
326         return(0);
327 }