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