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>
41 void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket)
43 struct sockaddr_in6 clientaddr;
44 unsigned int addrlen = sizeof(clientaddr);
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);
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]);
61 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
63 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
64 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
67 u_char fixedans[ PACKETSZ ];
69 int need_to_free_answer = 0;
77 /* Make our DNS query. */
80 if (CtdlThreadCheckStop())
83 snprintf(txtbuf, txtbufsize, "System shutting down");
86 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
88 /* Was there a problem? If so, the domain doesn't exist. */
98 answer = malloc( len );
99 need_to_free_answer = 1;
100 len = res_query( domain, C_IN, T_A, answer, len );
102 if (txtbuf != NULL) {
103 snprintf(txtbuf, txtbufsize,
104 "Message rejected due to known spammer source IP address");
106 if (need_to_free_answer) free(answer);
110 if (CtdlThreadCheckStop())
113 snprintf(txtbuf, txtbufsize, "System shutting down");
114 if (need_to_free_answer) free(answer);
118 result = ( char * )malloc( RESULT_SIZE );
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. */
126 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
127 if (CtdlThreadCheckStop())
130 snprintf(txtbuf, txtbufsize, "System shutting down");
131 if (need_to_free_answer) free(answer);
136 /* Just in case there's no TXT record... */
139 if (txtbuf != NULL) {
140 snprintf(txtbuf, txtbufsize,
141 "Message rejected due to known spammer source IP address");
143 if (need_to_free_answer) free(answer);
148 /* Skip the header and the address we queried. */
149 cp = answer + sizeof( HEADER );
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
162 cp += 1 + NS_INT16SZ + NS_INT32SZ;
164 /* Skip the type, class and ttl. */
165 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
167 /* Get the length and end of the buffer. */
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 )
179 for( b = a; b > 0 && cp < cend && rp < rend;
182 if( *cp == '\n' || *cp == '"' ||
191 if (txtbuf != NULL) {
192 snprintf(txtbuf, txtbufsize, "%s", result);
194 /* Remove nonprintable characters */
195 for (p=txtbuf; *p; ++p) {
196 if (!isprint(*p)) strcpy(p, p+1);
198 if (need_to_free_answer) free(answer);
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'
208 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
214 char rbl_domains[SIZ];
215 char txt_answer[1024];
216 char dotted_quad[32];
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);
222 /* See if we have any RBL domains configured */
223 num_rbl = get_hosts(rbl_domains, "rbl");
224 if (num_rbl < 1) return(0);
226 /* Try all configured RBL's */
227 for (rbl=0; rbl<num_rbl; ++rbl) {
228 snprintf(tbuf, sizeof tbuf,
231 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
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);
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'
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!
252 int rbl_check(char *message_to_spammer) {
254 struct sockaddr_in peer;
255 socklen_t peer_len = 0;
257 peer_len = sizeof(peer);
258 r = getpeername(CC->client_socket, &peer, &peer_len);
260 return(rbl_check_addr(&peer.sin_addr, message_to_spammer));
263 CtdlLogPrintf(CTDL_INFO, "RBL getpeername() failed: %s\n", strerror(errno));
269 * Convert a host name to a dotted quad address.
270 * Returns zero on success or nonzero on failure.
272 int hostname_to_dotted_quad(char *addr, char *host) {
277 ch = gethostbyname(host);
279 strcpy(addr, "0.0.0.0");
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);