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