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