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);
56 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
58 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
59 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
62 u_char fixedans[ PACKETSZ ];
64 int need_to_free_answer = 0;
72 /* Make our DNS query. */
75 if (CtdlThreadCheckStop())
78 snprintf(txtbuf, txtbufsize, "System shutting down");
81 len = res_query( domain, C_IN, T_A, answer, PACKETSZ );
83 /* Was there a problem? If so, the domain doesn't exist. */
93 answer = malloc( len );
94 need_to_free_answer = 1;
95 len = res_query( domain, C_IN, T_A, answer, len );
98 snprintf(txtbuf, txtbufsize,
99 "Message rejected due to known spammer source IP address");
101 if (need_to_free_answer) free(answer);
105 if (CtdlThreadCheckStop())
108 snprintf(txtbuf, txtbufsize, "System shutting down");
109 if (need_to_free_answer) free(answer);
113 result = ( char * )malloc( RESULT_SIZE );
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. */
121 len = res_query( domain, C_IN, T_TXT, answer, PACKETSZ );
122 if (CtdlThreadCheckStop())
125 snprintf(txtbuf, txtbufsize, "System shutting down");
126 if (need_to_free_answer) free(answer);
131 /* Just in case there's no TXT record... */
134 if (txtbuf != NULL) {
135 snprintf(txtbuf, txtbufsize,
136 "Message rejected due to known spammer source IP address");
138 if (need_to_free_answer) free(answer);
143 /* Skip the header and the address we queried. */
144 cp = answer + sizeof( HEADER );
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
157 cp += 1 + NS_INT16SZ + NS_INT32SZ;
159 /* Skip the type, class and ttl. */
160 cp += ( NS_INT16SZ * 2 ) + NS_INT32SZ;
162 /* Get the length and end of the buffer. */
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 )
174 for( b = a; b > 0 && cp < cend && rp < rend;
177 if( *cp == '\n' || *cp == '"' ||
186 if (txtbuf != NULL) {
187 snprintf(txtbuf, txtbufsize, "%s", result);
189 /* Remove nonprintable characters */
190 for (p=txtbuf; *p; ++p) {
191 if (!isprint(*p)) strcpy(p, p+1);
193 if (need_to_free_answer) free(answer);
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'
203 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
209 char rbl_domains[SIZ];
210 char txt_answer[1024];
211 char dotted_quad[32];
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);
217 /* See if we have any RBL domains configured */
218 num_rbl = get_hosts(rbl_domains, "rbl");
219 if (num_rbl < 1) return(0);
221 /* Try all configured RBL's */
222 for (rbl=0; rbl<num_rbl; ++rbl) {
223 snprintf(tbuf, sizeof tbuf,
226 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
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);
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'
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!
247 int rbl_check(char *message_to_spammer) {
249 struct sockaddr_in peer;
250 socklen_t peer_len = 0;
252 peer_len = sizeof(peer);
253 r = getpeername(CC->client_socket, &peer, &peer_len);
255 return(rbl_check_addr(&peer.sin_addr, message_to_spammer));
258 CtdlLogPrintf(CTDL_INFO, "RBL getpeername() failed: %s\n", strerror(errno));
264 * Convert a host name to a dotted quad address.
265 * Returns zero on success or nonzero on failure.
267 int hostname_to_dotted_quad(char *addr, char *host) {
272 ch = gethostbyname(host);
274 strcpy(addr, "0.0.0.0");
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);