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