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