* Fixed an error in the template that was causing the 'c_creataide' field to load...
[citadel.git] / citadel / locate_host.c
blobed566a42bc08d0479991db7a60d08d6fb04b776a
1 /*
2 * $Id$
4 * locate the originating host
6 */
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 <libcitadel.h>
21 #include "citadel.h"
22 #include "server.h"
23 #include "locate_host.h"
24 #include "sysdep_decls.h"
25 #include "config.h"
26 #include "domain.h"
27 #include "ctdl_module.h"
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
38 /* Hacks to work around nameser.h declarations missing on OpenBSD
39 * see also: http://search.cpan.org/src/MIKER/Net-DNS-ToolKit-0.30/ToolKit.h
42 #ifndef NS_INT16SZ
43 # ifdef INT16SZ
44 # define NS_INT16SZ INT16SZ
45 # endif
46 #endif
48 #ifndef NS_INT32SZ
49 # ifdef INT32SZ
50 # define NS_INT32SZ INT32SZ
51 # endif
52 #endif
54 #ifndef NS_GET16
55 # ifdef GETSHORT
56 # define NS_GET16 GETSHORT
57 # endif
58 #endif
61 /***************************************************************************/
64 void locate_host(char *tbuf, size_t n,
65 char *abuf, size_t na,
66 const struct in_addr *addr)
68 struct hostent *ch;
69 const char *i;
70 char *j;
71 int a1, a2, a3, a4;
72 char address_string[SIZ];
75 #ifdef HAVE_NONREENTRANT_NETDB
76 begin_critical_section(S_NETDB);
77 #endif
79 i = (const char *) addr;
80 a1 = ((*i++) & 0xff);
81 a2 = ((*i++) & 0xff);
82 a3 = ((*i++) & 0xff);
83 a4 = ((*i++) & 0xff);
84 sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
86 if (abuf != NULL) {
87 safestrncpy(abuf, address_string, na);
90 if ((ch = gethostbyaddr((const char *) addr,
91 sizeof(*addr), AF_INET)) == NULL) {
92 bad_dns:
93 safestrncpy(tbuf, address_string, n);
94 goto end; /* because we might need to end the critical
95 section */
97 /* check if the forward DNS agrees; if not, they're spoofing */
98 j = strdup(ch->h_name);
99 ch = gethostbyname(j);
100 free(j);
101 if (ch == NULL)
102 goto bad_dns;
104 /* check address for consistency */
105 for (; *ch->h_addr_list; ch->h_addr_list++)
106 if (!memcmp(*ch->h_addr_list, addr,
107 sizeof *addr)) {
108 safestrncpy(tbuf, ch->h_name, 63);
109 goto end;
111 goto bad_dns; /* they were spoofing. report a numeric IP
112 address. */
114 end:
116 #ifdef HAVE_NONREENTRANT_NETDB
117 end_critical_section(S_NETDB);
118 #endif
120 tbuf[63] = 0;
125 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
127 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
128 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
129 int a, b, c;
130 char *result = NULL;
131 u_char fixedans[ PACKETSZ ];
132 u_char *answer;
133 int need_to_free_answer = 0;
134 const u_char *cp;
135 u_char *rp;
136 const u_char *cend;
137 const u_char *rend;
138 int len;
139 char *p = NULL;
141 /* Make our DNS query. */
142 //res_init();
143 answer = fixedans;
144 if (CtdlThreadCheckStop())
146 if (txtbuf != NULL)
147 snprintf(txtbuf, txtbufsize, "System shutting down");
148 return (1);
150 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
152 /* Was there a problem? If so, the domain doesn't exist. */
153 if( len == -1 ) {
154 if (txtbuf != NULL) {
155 strcpy(txtbuf, "");
157 return(0);
160 if( len > PACKETSZ )
162 answer = malloc( len );
163 need_to_free_answer = 1;
164 len = res_query( domain, C_IN, T_A, answer, len );
165 if( len == -1 ) {
166 if (txtbuf != NULL) {
167 snprintf(txtbuf, txtbufsize,
168 "Message rejected due to known spammer source IP address");
170 if (need_to_free_answer) free(answer);
171 return(1);
174 if (CtdlThreadCheckStop())
176 if (txtbuf != NULL)
177 snprintf(txtbuf, txtbufsize, "System shutting down");
178 if (need_to_free_answer) free(answer);
179 return (1);
182 result = ( char * )malloc( RESULT_SIZE );
183 result[ 0 ] = '\0';
186 /* Make another DNS query for textual data; this shouldn't
187 be a performance hit, since it'll now be cached at the
188 nameserver we're using. */
189 res_init();
190 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
191 if (CtdlThreadCheckStop())
193 if (txtbuf != NULL)
194 snprintf(txtbuf, txtbufsize, "System shutting down");
195 if (need_to_free_answer) free(answer);
196 free(result);
197 return (1);
200 /* Just in case there's no TXT record... */
201 if( len == -1 )
203 if (txtbuf != NULL) {
204 snprintf(txtbuf, txtbufsize,
205 "Message rejected due to known spammer source IP address");
207 if (need_to_free_answer) free(answer);
208 free(result);
209 return(1);
212 /* Skip the header and the address we queried. */
213 cp = answer + sizeof( HEADER );
214 while( *cp != '\0' )
216 a = *cp++;
217 while( a-- )
218 cp++;
221 /* This seems to be a bit of magic data that we need to
222 skip. I wish there were good online documentation
223 for programming for libresolv, so I'd know what I'm
224 skipping here. Anyone reading this, feel free to
225 enlighten me. */
226 cp += 1 + NS_INT16SZ + NS_INT32SZ;
228 /* Skip the type, class and ttl. */
229 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
231 /* Get the length and end of the buffer. */
232 NS_GET16( c, cp );
233 cend = cp + c;
235 /* Iterate over any multiple answers we might have. In
236 this context, it's unlikely, but anyway. */
237 rp = (u_char *) result;
238 rend = (u_char *) result + RESULT_SIZE - 1;
239 while( cp < cend && rp < rend )
241 a = *cp++;
242 if( a != 0 )
243 for( b = a; b > 0 && cp < cend && rp < rend;
244 b-- )
246 if( *cp == '\n' || *cp == '"' ||
247 *cp == '\\' )
249 *rp++ = '\\';
251 *rp++ = *cp++;
254 *rp = '\0';
255 if (txtbuf != NULL) {
256 snprintf(txtbuf, txtbufsize, "%s", result);
258 /* Remove nonprintable characters */
259 for (p=txtbuf; *p; ++p) {
260 if (!isprint(*p)) strcpy(p, p+1);
262 if (need_to_free_answer) free(answer);
263 free(result);
264 return(1);
269 * Check to see if a host is on some sort of spam list (RBL)
270 * If spammer, returns nonzero and places reason in 'message_to_spammer'
272 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
274 const char *i;
275 int a1, a2, a3, a4;
276 char tbuf[256];
277 int rbl;
278 int num_rbl;
279 char rbl_domains[SIZ];
280 char txt_answer[1024];
282 strcpy(message_to_spammer, "ok");
284 i = (const char *) addr;
285 a1 = ((*i++) & 0xff);
286 a2 = ((*i++) & 0xff);
287 a3 = ((*i++) & 0xff);
288 a4 = ((*i++) & 0xff);
290 /* See if we have any RBL domains configured */
291 num_rbl = get_hosts(rbl_domains, "rbl");
292 if (num_rbl < 1) return(0);
294 /* Try all configured RBL's */
295 for (rbl=0; rbl<num_rbl; ++rbl) {
296 snprintf(tbuf, sizeof tbuf,
297 "%d.%d.%d.%d.",
298 a4, a3, a2, a1);
299 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
301 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
302 sprintf(message_to_spammer, "5.7.1 %s", txt_answer);
303 CtdlLogPrintf(CTDL_INFO, "RBL: %s\n", txt_answer);
304 return(1);
308 return(0);
313 * Check to see if the client host is on some sort of spam list (RBL)
314 * If spammer, returns nonzero and places reason in 'message_to_spammer'
316 int rbl_check(char *message_to_spammer) {
317 struct sockaddr_in sin;
318 int len; /* should be socklen_t but doesn't work on Macintosh */
319 struct timeval tv1, tv2;
320 suseconds_t total_time = 0;
322 gettimeofday(&tv1, NULL);
323 len = 0;
324 memset (&sin, 0, sizeof (struct sockaddr_in));
325 if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, (socklen_t *)&len)) {
326 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
329 gettimeofday(&tv2, NULL);
330 total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
331 CtdlLogPrintf(CTDL_DEBUG, "RBL check completed in %ld.%ld seconds\n",
332 (total_time / 1000000),
333 (total_time % 1000000)
335 return(0);
339 * Convert a host name to a dotted quad address.
340 * Returns zero on success or nonzero on failure.
342 int hostname_to_dotted_quad(char *addr, char *host) {
343 struct hostent *ch;
344 const char *i;
345 int a1, a2, a3, a4;
347 ch = gethostbyname(host);
348 if (ch == NULL) {
349 strcpy(addr, "0.0.0.0");
350 return(1);
353 i = (const char *) ch->h_addr_list[0];
354 a1 = ((*i++) & 0xff);
355 a2 = ((*i++) & 0xff);
356 a3 = ((*i++) & 0xff);
357 a4 = ((*i++) & 0xff);
358 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);
359 return(0);