2 * Functions which handle hostname/address lookups and resolution
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
19 #include <libcitadel.h>
22 #include "locate_host.h"
23 #include "sysdep_decls.h"
27 #include "ctdl_module.h"
30 #include <arpa/nameser.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
39 * Given an open client socket, return the host name and IP address at the other end.
40 * (IPv4 and IPv6 compatible)
42 void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket)
44 struct sockaddr_in6 clientaddr;
45 unsigned int addrlen = sizeof(clientaddr);
50 getpeername(client_socket, (struct sockaddr *)&clientaddr, &addrlen);
51 getnameinfo((struct sockaddr *)&clientaddr, addrlen, tbuf, n, NULL, 0, 0);
52 getnameinfo((struct sockaddr *)&clientaddr, addrlen, abuf, na, NULL, 0, NI_NUMERICHOST);
54 /* Convert IPv6-mapped IPv4 addresses back to traditional dotted quad.
56 * Other code here, such as the RBL check, will expect IPv4 addresses to be represented
57 * as dotted-quad, even if they come in over a hybrid IPv6/IPv4 socket.
59 if ( (strlen(abuf) > 7) && (!strncasecmp(abuf, "::ffff:", 7)) ) {
60 if (!strcmp(abuf, tbuf)) strcpy(tbuf, &tbuf[7]);
61 strcpy(abuf, &abuf[7]);
67 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
69 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
70 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
73 u_char fixedans[ PACKETSZ ];
75 int need_to_free_answer = 0;
82 static int res_initted = 0;
84 if (!res_initted) { /* only have to do this once */
89 /* Make our DNS query. */
91 if (CtdlThreadCheckStop())
94 snprintf(txtbuf, txtbufsize, "System shutting down");
98 len = res_query(domain, C_IN, T_A, answer, PACKETSZ);
100 /* Was there a problem? If so, the domain doesn't exist. */
102 if (txtbuf != NULL) {
110 answer = malloc(len);
111 need_to_free_answer = 1;
112 len = res_query(domain, C_IN, T_A, answer, len);
114 if (txtbuf != NULL) {
115 snprintf(txtbuf, txtbufsize,
116 "Message rejected due to known spammer source IP address");
118 if (need_to_free_answer) free(answer);
122 if (CtdlThreadCheckStop())
125 snprintf(txtbuf, txtbufsize, "System shutting down");
126 if (need_to_free_answer) free(answer);
130 result = (char *) malloc(RESULT_SIZE);
134 /* Make another DNS query for textual data; this shouldn't
135 * be a performance hit, since it'll now be cached at the
136 * nameserver we're using.
138 len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ);
139 if (CtdlThreadCheckStop())
141 if (txtbuf != NULL) {
142 snprintf(txtbuf, txtbufsize, "System shutting down");
144 if (need_to_free_answer) free(answer);
149 /* Just in case there's no TXT record... */
152 if (txtbuf != NULL) {
153 snprintf(txtbuf, txtbufsize,
154 "Message rejected due to known spammer source IP address");
156 if (need_to_free_answer) free(answer);
161 /* Skip the header and the address we queried. */
162 cp = answer + sizeof( HEADER );
170 /* This seems to be a bit of magic data that we need to
171 * skip. I wish there were good online documentation
172 * for programming for libresolv, so I'd know what I'm
173 * skipping here. Anyone reading this, feel free to
176 cp += 1 + NS_INT16SZ + NS_INT32SZ;
178 /* Skip the type, class and ttl. */
179 cp += (NS_INT16SZ * 2) + NS_INT32SZ;
181 /* Get the length and end of the buffer. */
185 /* Iterate over any multiple answers we might have. In
186 * this context, it's unlikely, but anyway.
188 rp = (u_char *) result;
189 rend = (u_char *) result + RESULT_SIZE - 1;
190 while (cp < cend && rp < rend)
194 for (b = a; b > 0 && cp < cend && rp < rend; b--)
196 if (*cp == '\n' || *cp == '"' || *cp == '\\')
204 if (txtbuf != NULL) {
205 snprintf(txtbuf, txtbufsize, "%s", result);
207 /* Remove nonprintable characters */
208 for (p=txtbuf; *p; ++p) {
209 if (!isprint(*p)) strcpy(p, p+1);
211 if (need_to_free_answer) free(answer);
218 * Check to see if the client host is on some sort of spam list (RBL)
219 * If spammer, returns nonzero and places reason in 'message_to_spammer'
221 int rbl_check(char *message_to_spammer)
227 char rbl_domains[SIZ];
228 char txt_answer[1024];
231 strcpy(message_to_spammer, "ok");
233 if ((strchr(CC->cs_addr, '.')) && (!strchr(CC->cs_addr, ':'))) {
237 sscanf(CC->cs_addr, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
238 snprintf(tbuf, sizeof tbuf, "%d.%d.%d.%d.", a4, a3, a2, a1);
239 suffix_pos = strlen(tbuf);
241 else if ((!strchr(CC->cs_addr, '.')) && (strchr(CC->cs_addr, ':'))) {
244 char workbuf[sizeof tbuf];
249 /* tedious code to expand and reverse an IPv6 address */
250 safestrncpy(tbuf, CC->cs_addr, sizeof tbuf);
251 num_colons = haschar(tbuf, ':');
252 if ((num_colons < 2) || (num_colons > 7)) return(0); /* badly formed address */
254 /* expand the "::" shorthand */
255 while (num_colons < 7) {
256 ptr = strstr(tbuf, "::");
257 if (!ptr) return(0); /* badly formed address */
259 strcpy(workbuf, ptr);
261 strcat(ptr, workbuf);
265 /* expand to 32 hex characters with no colons */
266 strcpy(workbuf, tbuf);
267 strcpy(tbuf, "00000000000000000000000000000000");
268 for (i=0; i<8; ++i) {
270 extract_token(tokbuf, workbuf, i, ':', sizeof tokbuf);
272 memcpy(&tbuf[ (i*4) + (4-strlen(tokbuf)) ], tokbuf, strlen(tokbuf) );
274 if (strlen(tbuf) != 32) return(0);
276 /* now reverse it and add dots */
277 strcpy(workbuf, tbuf);
278 for (i=0; i<32; ++i) {
279 tbuf[i*2] = workbuf[31-i];
286 return(0); /* unknown address format */
289 /* See if we have any RBL domains configured */
290 num_rbl = get_hosts(rbl_domains, "rbl");
291 if (num_rbl < 1) return(0);
293 /* Try all configured RBL's */
294 for (rbl=0; rbl<num_rbl; ++rbl) {
295 extract_token(&tbuf[suffix_pos], rbl_domains, rbl, '|', (sizeof tbuf - suffix_pos));
297 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
298 strcpy(message_to_spammer, txt_answer);
299 CtdlLogPrintf(CTDL_INFO, "RBL: %s\n", txt_answer);
309 * Convert a host name to a dotted quad address.
310 * Returns zero on success or nonzero on failure.
312 * FIXME this is obviously not IPv6 compatible.
314 int hostname_to_dotted_quad(char *addr, char *host) {
319 ch = gethostbyname(host);
321 strcpy(addr, "0.0.0.0");
325 i = (const char *) ch->h_addr_list[0];
326 a1 = ((*i++) & 0xff);
327 a2 = ((*i++) & 0xff);
328 a3 = ((*i++) & 0xff);
329 a4 = ((*i++) & 0xff);
330 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);