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