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