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.
59 * Other code here, such as the RBL check, will expect IPv4 addresses to be represented
60 * as dotted-quad, even if they come in over a hybrid IPv6/IPv4 socket.
62 if ( (strlen(abuf) > 7) && (!strncasecmp(abuf, "::ffff:", 7)) ) {
63 strcpy(abuf, &abuf[7]);
69 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
71 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
72 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
75 u_char fixedans[ PACKETSZ ];
77 int need_to_free_answer = 0;
84 static int res_initted = 0;
86 if (!res_initted) { /* only have to do this once */
91 /* Make our DNS query. */
93 if (CtdlThreadCheckStop())
96 snprintf(txtbuf, txtbufsize, "System shutting down");
100 len = res_query(domain, C_IN, T_A, answer, PACKETSZ);
102 /* Was there a problem? If so, the domain doesn't exist. */
104 if (txtbuf != NULL) {
112 answer = malloc(len);
113 need_to_free_answer = 1;
114 len = res_query(domain, C_IN, T_A, answer, len);
116 if (txtbuf != NULL) {
117 snprintf(txtbuf, txtbufsize,
118 "Message rejected due to known spammer source IP address");
120 if (need_to_free_answer) free(answer);
124 if (CtdlThreadCheckStop())
127 snprintf(txtbuf, txtbufsize, "System shutting down");
128 if (need_to_free_answer) free(answer);
132 result = (char *) malloc(RESULT_SIZE);
136 /* Make another DNS query for textual data; this shouldn't
137 * be a performance hit, since it'll now be cached at the
138 * nameserver we're using.
140 len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ);
141 if (CtdlThreadCheckStop())
143 if (txtbuf != NULL) {
144 snprintf(txtbuf, txtbufsize, "System shutting down");
146 if (need_to_free_answer) free(answer);
151 /* Just in case there's no TXT record... */
154 if (txtbuf != NULL) {
155 snprintf(txtbuf, txtbufsize,
156 "Message rejected due to known spammer source IP address");
158 if (need_to_free_answer) free(answer);
163 /* Skip the header and the address we queried. */
164 cp = answer + sizeof( HEADER );
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
178 cp += 1 + NS_INT16SZ + NS_INT32SZ;
180 /* Skip the type, class and ttl. */
181 cp += (NS_INT16SZ * 2) + NS_INT32SZ;
183 /* Get the length and end of the buffer. */
187 /* Iterate over any multiple answers we might have. In
188 * this context, it's unlikely, but anyway.
190 rp = (u_char *) result;
191 rend = (u_char *) result + RESULT_SIZE - 1;
192 while (cp < cend && rp < rend)
196 for (b = a; b > 0 && cp < cend && rp < rend; b--)
198 if (*cp == '\n' || *cp == '"' || *cp == '\\')
206 if (txtbuf != NULL) {
207 snprintf(txtbuf, txtbufsize, "%s", result);
209 /* Remove nonprintable characters */
210 for (p=txtbuf; *p; ++p) {
211 if (!isprint(*p)) strcpy(p, p+1);
213 if (need_to_free_answer) free(answer);
220 * Check to see if the client host is on some sort of spam list (RBL)
221 * If spammer, returns nonzero and places reason in 'message_to_spammer'
223 * FIXME: support IPv6 RBL as specified in http://tools.ietf.org/html/draft-irtf-asrg-dnsbl-08
225 int rbl_check(char *message_to_spammer)
231 char rbl_domains[SIZ];
232 char txt_answer[1024];
234 strcpy(message_to_spammer, "ok");
235 sscanf(CC->cs_addr, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
237 /* See if we have any RBL domains configured */
238 num_rbl = get_hosts(rbl_domains, "rbl");
239 if (num_rbl < 1) return(0);
241 /* Try all configured RBL's */
242 for (rbl=0; rbl<num_rbl; ++rbl) {
243 snprintf(tbuf, sizeof tbuf, "%d.%d.%d.%d.", a4, a3, a2, a1);
244 extract_token(&tbuf[strlen(tbuf)], rbl_domains, rbl, '|', (sizeof tbuf - strlen(tbuf)));
246 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
247 strcpy(message_to_spammer, txt_answer);
248 CtdlLogPrintf(CTDL_INFO, "RBL: %s\n", txt_answer);
258 * Convert a host name to a dotted quad address.
259 * Returns zero on success or nonzero on failure.
261 int hostname_to_dotted_quad(char *addr, char *host) {
266 ch = gethostbyname(host);
268 strcpy(addr, "0.0.0.0");
272 i = (const char *) ch->h_addr_list[0];
273 a1 = ((*i++) & 0xff);
274 a2 = ((*i++) & 0xff);
275 a3 = ((*i++) & 0xff);
276 a4 = ((*i++) & 0xff);
277 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);