2 * Functions which handle hostname/address lookups and resolution
4 * Copyright (c) 1987-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
30 #include <libcitadel.h>
33 #include "locate_host.h"
34 #include "sysdep_decls.h"
38 #include "ctdl_module.h"
41 #include <arpa/nameser.h>
42 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
43 #include <arpa/nameser_compat.h>
48 /** START:some missing macros on OpenBSD 3.9 */
50 #define NS_CMPRSFLGS 0xc0
52 #if !defined(NS_MAXCDNAME) && defined (MAXCDNAME)
53 #define NS_MAXCDNAME MAXCDNAME
55 #if !defined(NS_INT16SZ) && defined(INT16SZ)
56 #define NS_INT16SZ INT16SZ
57 #define NS_INT32SZ INT32SZ
60 # define NS_GET16 GETSHORT
63 /** END:some missing macros on OpenBSD 3.9 */
66 * Given an open client socket, return the host name and IP address at the other end.
67 * (IPv4 and IPv6 compatible)
69 void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket)
71 struct sockaddr_in6 clientaddr;
72 unsigned int addrlen = sizeof(clientaddr);
77 getpeername(client_socket, (struct sockaddr *)&clientaddr, &addrlen);
78 getnameinfo((struct sockaddr *)&clientaddr, addrlen, tbuf, n, NULL, 0, 0);
79 getnameinfo((struct sockaddr *)&clientaddr, addrlen, abuf, na, NULL, 0, NI_NUMERICHOST);
81 /* Convert IPv6-mapped IPv4 addresses back to traditional dotted quad.
83 * Other code here, such as the RBL check, will expect IPv4 addresses to be represented
84 * as dotted-quad, even if they come in over a hybrid IPv6/IPv4 socket.
86 if ( (strlen(abuf) > 7) && (!strncasecmp(abuf, "::ffff:", 7)) ) {
87 if (!strcmp(abuf, tbuf)) strcpy(tbuf, &tbuf[7]);
88 strcpy(abuf, &abuf[7]);
94 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
96 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
97 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
100 u_char fixedans[ PACKETSZ ];
102 int need_to_free_answer = 0;
109 static int res_initted = 0;
111 if (!res_initted) { /* only have to do this once */
116 /* Make our DNS query. */
118 if (server_shutting_down)
120 if (txtbuf != NULL) {
121 snprintf(txtbuf, txtbufsize, "System shutting down");
125 len = res_query(domain, C_IN, T_A, answer, PACKETSZ);
127 /* Was there a problem? If so, the domain doesn't exist. */
129 if (txtbuf != NULL) {
137 answer = malloc(len);
138 need_to_free_answer = 1;
139 len = res_query(domain, C_IN, T_A, answer, len);
141 if (txtbuf != NULL) {
142 snprintf(txtbuf, txtbufsize,
143 "Message rejected due to known spammer source IP address");
145 if (need_to_free_answer) free(answer);
149 if (server_shutting_down)
152 snprintf(txtbuf, txtbufsize, "System shutting down");
153 if (need_to_free_answer) free(answer);
157 result = (char *) malloc(RESULT_SIZE);
161 /* Make another DNS query for textual data; this shouldn't
162 * be a performance hit, since it'll now be cached at the
163 * nameserver we're using.
165 len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ);
166 if (server_shutting_down)
168 if (txtbuf != NULL) {
169 snprintf(txtbuf, txtbufsize, "System shutting down");
171 if (need_to_free_answer) free(answer);
176 /* Just in case there's no TXT record... */
179 if (txtbuf != NULL) {
180 snprintf(txtbuf, txtbufsize,
181 "Message rejected due to known spammer source IP address");
183 if (need_to_free_answer) free(answer);
188 /* Skip the header and the address we queried. */
189 cp = answer + sizeof( HEADER );
197 /* This seems to be a bit of magic data that we need to
198 * skip. I wish there were good online documentation
199 * for programming for libresolv, so I'd know what I'm
200 * skipping here. Anyone reading this, feel free to
203 cp += 1 + NS_INT16SZ + NS_INT32SZ;
205 /* Skip the type, class and ttl. */
206 cp += (NS_INT16SZ * 2) + NS_INT32SZ;
208 /* Get the length and end of the buffer. */
212 /* Iterate over any multiple answers we might have. In
213 * this context, it's unlikely, but anyway.
215 rp = (u_char *) result;
216 rend = (u_char *) result + RESULT_SIZE - 1;
217 while (cp < cend && rp < rend)
221 for (b = a; b > 0 && cp < cend && rp < rend; b--)
223 if (*cp == '\n' || *cp == '"' || *cp == '\\')
231 if (txtbuf != NULL) {
233 len = snprintf(txtbuf, txtbufsize, "%s", result);
235 /* Remove nonprintable characters */
236 for (p = txtbuf; *p != '\0'; p++) {
240 len - (p - txtbuf) - 1);
244 if (need_to_free_answer) free(answer);
251 * Check to see if the client host is on some sort of spam list (RBL)
252 * If spammer, returns nonzero and places reason in 'message_to_spammer'
254 int rbl_check(char *message_to_spammer)
260 char rbl_domains[SIZ];
261 char txt_answer[1024];
263 strcpy(message_to_spammer, "ok");
265 if ((strchr(CC->cs_addr, '.')) && (!strchr(CC->cs_addr, ':'))) {
268 sscanf(CC->cs_addr, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
269 snprintf(tbuf, sizeof tbuf, "%d.%d.%d.%d.", a4, a3, a2, a1);
270 suffix_pos = strlen(tbuf);
272 else if ((!strchr(CC->cs_addr, '.')) && (strchr(CC->cs_addr, ':'))) {
275 char workbuf[sizeof tbuf];
278 /* tedious code to expand and reverse an IPv6 address */
279 safestrncpy(tbuf, CC->cs_addr, sizeof tbuf);
280 num_colons = haschar(tbuf, ':');
281 if ((num_colons < 2) || (num_colons > 7)) return(0); /* badly formed address */
283 /* expand the "::" shorthand */
284 while (num_colons < 7) {
285 ptr = strstr(tbuf, "::");
286 if (!ptr) return(0); /* badly formed address */
288 strcpy(workbuf, ptr);
290 strcat(ptr, workbuf);
294 /* expand to 32 hex characters with no colons */
295 strcpy(workbuf, tbuf);
296 strcpy(tbuf, "00000000000000000000000000000000");
297 for (i=0; i<8; ++i) {
299 extract_token(tokbuf, workbuf, i, ':', sizeof tokbuf);
301 memcpy(&tbuf[ (i*4) + (4-strlen(tokbuf)) ], tokbuf, strlen(tokbuf) );
303 if (strlen(tbuf) != 32) return(0);
305 /* now reverse it and add dots */
306 strcpy(workbuf, tbuf);
307 for (i=0; i<32; ++i) {
308 tbuf[i*2] = workbuf[31-i];
315 return(0); /* unknown address format */
318 /* See if we have any RBL domains configured */
319 num_rbl = get_hosts(rbl_domains, "rbl");
320 if (num_rbl < 1) return(0);
322 /* Try all configured RBL's */
323 for (rbl=0; rbl<num_rbl; ++rbl) {
324 extract_token(&tbuf[suffix_pos], rbl_domains, rbl, '|', (sizeof tbuf - suffix_pos));
326 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
327 strcpy(message_to_spammer, txt_answer);
328 syslog(LOG_INFO, "RBL: %s\n", txt_answer);
338 * Convert a host name to a dotted quad address.
339 * Returns zero on success or nonzero on failure.
341 * FIXME this is obviously not IPv6 compatible.
343 int hostname_to_dotted_quad(char *addr, char *host) {
348 ch = gethostbyname(host);
350 strcpy(addr, "0.0.0.0");
354 i = (const char *) ch->h_addr_list[0];
355 a1 = ((*i++) & 0xff);
356 a2 = ((*i++) & 0xff);
357 a3 = ((*i++) & 0xff);
358 a4 = ((*i++) & 0xff);
359 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);