4 * Functions which handle hostname/address lookups and resolution
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
22 #include <libcitadel.h>
25 #include "locate_host.h"
26 #include "sysdep_decls.h"
30 #include "ctdl_module.h"
33 #include <arpa/nameser.h>
34 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
35 #include <arpa/nameser_compat.h>
42 * Given an open client socket, return the host name and IP address at the other end.
43 * (IPv4 and IPv6 compatible)
45 void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket)
47 struct sockaddr_in6 clientaddr;
48 unsigned int addrlen = sizeof(clientaddr);
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);
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]);
65 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
67 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
68 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
71 u_char fixedans[ PACKETSZ ];
73 int need_to_free_answer = 0;
81 /* Make our DNS query. */
84 if (CtdlThreadCheckStop())
87 snprintf(txtbuf, txtbufsize, "System shutting down");
90 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
92 /* Was there a problem? If so, the domain doesn't exist. */
102 answer = malloc( len );
103 need_to_free_answer = 1;
104 len = res_query( domain, C_IN, T_A, answer, len );
106 if (txtbuf != NULL) {
107 snprintf(txtbuf, txtbufsize,
108 "Message rejected due to known spammer source IP address");
110 if (need_to_free_answer) free(answer);
114 if (CtdlThreadCheckStop())
117 snprintf(txtbuf, txtbufsize, "System shutting down");
118 if (need_to_free_answer) free(answer);
122 result = ( char * )malloc( RESULT_SIZE );
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. */
130 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
131 if (CtdlThreadCheckStop())
134 snprintf(txtbuf, txtbufsize, "System shutting down");
135 if (need_to_free_answer) free(answer);
140 /* Just in case there's no TXT record... */
143 if (txtbuf != NULL) {
144 snprintf(txtbuf, txtbufsize,
145 "Message rejected due to known spammer source IP address");
147 if (need_to_free_answer) free(answer);
152 /* Skip the header and the address we queried. */
153 cp = answer + sizeof( HEADER );
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
166 cp += 1 + NS_INT16SZ + NS_INT32SZ;
168 /* Skip the type, class and ttl. */
169 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
171 /* Get the length and end of the buffer. */
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 )
183 for( b = a; b > 0 && cp < cend && rp < rend;
186 if( *cp == '\n' || *cp == '"' ||
195 if (txtbuf != NULL) {
196 snprintf(txtbuf, txtbufsize, "%s", result);
198 /* Remove nonprintable characters */
199 for (p=txtbuf; *p; ++p) {
200 if (!isprint(*p)) strcpy(p, p+1);
202 if (need_to_free_answer) free(answer);
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'
212 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
218 char rbl_domains[SIZ];
219 char txt_answer[1024];
220 char dotted_quad[32];
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);
226 /* See if we have any RBL domains configured */
227 num_rbl = get_hosts(rbl_domains, "rbl");
228 if (num_rbl < 1) return(0);
230 /* Try all configured RBL's */
231 for (rbl=0; rbl<num_rbl; ++rbl) {
232 snprintf(tbuf, sizeof tbuf,
235 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
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);
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'
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!
256 * FIXME: support IPv6 RBL as specified in http://tools.ietf.org/html/draft-irtf-asrg-dnsbl-08
258 int rbl_check(char *message_to_spammer) {
260 struct sockaddr_in peer;
261 socklen_t peer_len = 0;
263 peer_len = sizeof(peer);
264 r = getpeername(CC->client_socket, &peer, &peer_len);
266 return(rbl_check_addr(&peer.sin_addr, message_to_spammer));
269 CtdlLogPrintf(CTDL_INFO, "RBL getpeername() failed: %s\n", strerror(errno));
275 * Convert a host name to a dotted quad address.
276 * Returns zero on success or nonzero on failure.
278 int hostname_to_dotted_quad(char *addr, char *host) {
283 ch = gethostbyname(host);
285 strcpy(addr, "0.0.0.0");
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);