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 as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
36 #include <libcitadel.h>
39 #include "locate_host.h"
40 #include "sysdep_decls.h"
44 #include "ctdl_module.h"
47 #include <arpa/nameser.h>
48 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
49 #include <arpa/nameser_compat.h>
54 /** START:some missing macros on OpenBSD 3.9 */
56 #define NS_CMPRSFLGS 0xc0
58 #if !defined(NS_MAXCDNAME) && defined (MAXCDNAME)
59 #define NS_MAXCDNAME MAXCDNAME
61 #if !defined(NS_INT16SZ) && defined(INT16SZ)
62 #define NS_INT16SZ INT16SZ
63 #define NS_INT32SZ INT32SZ
65 /** END:some missing macros on OpenBSD 3.9 */
68 * Given an open client socket, return the host name and IP address at the other end.
69 * (IPv4 and IPv6 compatible)
71 void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket)
73 struct sockaddr_in6 clientaddr;
74 unsigned int addrlen = sizeof(clientaddr);
79 getpeername(client_socket, (struct sockaddr *)&clientaddr, &addrlen);
80 getnameinfo((struct sockaddr *)&clientaddr, addrlen, tbuf, n, NULL, 0, 0);
81 getnameinfo((struct sockaddr *)&clientaddr, addrlen, abuf, na, NULL, 0, NI_NUMERICHOST);
83 /* Convert IPv6-mapped IPv4 addresses back to traditional dotted quad.
85 * Other code here, such as the RBL check, will expect IPv4 addresses to be represented
86 * as dotted-quad, even if they come in over a hybrid IPv6/IPv4 socket.
88 if ( (strlen(abuf) > 7) && (!strncasecmp(abuf, "::ffff:", 7)) ) {
89 if (!strcmp(abuf, tbuf)) strcpy(tbuf, &tbuf[7]);
90 strcpy(abuf, &abuf[7]);
96 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
98 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
99 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
102 u_char fixedans[ PACKETSZ ];
104 int need_to_free_answer = 0;
111 static int res_initted = 0;
113 if (!res_initted) { /* only have to do this once */
118 /* Make our DNS query. */
120 if (server_shutting_down)
122 if (txtbuf != NULL) {
123 snprintf(txtbuf, txtbufsize, "System shutting down");
127 len = res_query(domain, C_IN, T_A, answer, PACKETSZ);
129 /* Was there a problem? If so, the domain doesn't exist. */
131 if (txtbuf != NULL) {
139 answer = malloc(len);
140 need_to_free_answer = 1;
141 len = res_query(domain, C_IN, T_A, answer, len);
143 if (txtbuf != NULL) {
144 snprintf(txtbuf, txtbufsize,
145 "Message rejected due to known spammer source IP address");
147 if (need_to_free_answer) free(answer);
151 if (server_shutting_down)
154 snprintf(txtbuf, txtbufsize, "System shutting down");
155 if (need_to_free_answer) free(answer);
159 result = (char *) malloc(RESULT_SIZE);
163 /* Make another DNS query for textual data; this shouldn't
164 * be a performance hit, since it'll now be cached at the
165 * nameserver we're using.
167 len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ);
168 if (server_shutting_down)
170 if (txtbuf != NULL) {
171 snprintf(txtbuf, txtbufsize, "System shutting down");
173 if (need_to_free_answer) free(answer);
178 /* Just in case there's no TXT record... */
181 if (txtbuf != NULL) {
182 snprintf(txtbuf, txtbufsize,
183 "Message rejected due to known spammer source IP address");
185 if (need_to_free_answer) free(answer);
190 /* Skip the header and the address we queried. */
191 cp = answer + sizeof( HEADER );
199 /* This seems to be a bit of magic data that we need to
200 * skip. I wish there were good online documentation
201 * for programming for libresolv, so I'd know what I'm
202 * skipping here. Anyone reading this, feel free to
205 cp += 1 + NS_INT16SZ + NS_INT32SZ;
207 /* Skip the type, class and ttl. */
208 cp += (NS_INT16SZ * 2) + NS_INT32SZ;
210 /* Get the length and end of the buffer. */
214 /* Iterate over any multiple answers we might have. In
215 * this context, it's unlikely, but anyway.
217 rp = (u_char *) result;
218 rend = (u_char *) result + RESULT_SIZE - 1;
219 while (cp < cend && rp < rend)
223 for (b = a; b > 0 && cp < cend && rp < rend; b--)
225 if (*cp == '\n' || *cp == '"' || *cp == '\\')
233 if (txtbuf != NULL) {
234 snprintf(txtbuf, txtbufsize, "%s", result);
236 /* Remove nonprintable characters */
237 for (p=txtbuf; *p; ++p) {
238 if (!isprint(*p)) strcpy(p, p+1);
240 if (need_to_free_answer) free(answer);
247 * Check to see if the client host is on some sort of spam list (RBL)
248 * If spammer, returns nonzero and places reason in 'message_to_spammer'
250 int rbl_check(char *message_to_spammer)
256 char rbl_domains[SIZ];
257 char txt_answer[1024];
259 strcpy(message_to_spammer, "ok");
261 if ((strchr(CC->cs_addr, '.')) && (!strchr(CC->cs_addr, ':'))) {
264 sscanf(CC->cs_addr, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
265 snprintf(tbuf, sizeof tbuf, "%d.%d.%d.%d.", a4, a3, a2, a1);
266 suffix_pos = strlen(tbuf);
268 else if ((!strchr(CC->cs_addr, '.')) && (strchr(CC->cs_addr, ':'))) {
271 char workbuf[sizeof tbuf];
274 /* tedious code to expand and reverse an IPv6 address */
275 safestrncpy(tbuf, CC->cs_addr, sizeof tbuf);
276 num_colons = haschar(tbuf, ':');
277 if ((num_colons < 2) || (num_colons > 7)) return(0); /* badly formed address */
279 /* expand the "::" shorthand */
280 while (num_colons < 7) {
281 ptr = strstr(tbuf, "::");
282 if (!ptr) return(0); /* badly formed address */
284 strcpy(workbuf, ptr);
286 strcat(ptr, workbuf);
290 /* expand to 32 hex characters with no colons */
291 strcpy(workbuf, tbuf);
292 strcpy(tbuf, "00000000000000000000000000000000");
293 for (i=0; i<8; ++i) {
295 extract_token(tokbuf, workbuf, i, ':', sizeof tokbuf);
297 memcpy(&tbuf[ (i*4) + (4-strlen(tokbuf)) ], tokbuf, strlen(tokbuf) );
299 if (strlen(tbuf) != 32) return(0);
301 /* now reverse it and add dots */
302 strcpy(workbuf, tbuf);
303 for (i=0; i<32; ++i) {
304 tbuf[i*2] = workbuf[31-i];
311 return(0); /* unknown address format */
314 /* See if we have any RBL domains configured */
315 num_rbl = get_hosts(rbl_domains, "rbl");
316 if (num_rbl < 1) return(0);
318 /* Try all configured RBL's */
319 for (rbl=0; rbl<num_rbl; ++rbl) {
320 extract_token(&tbuf[suffix_pos], rbl_domains, rbl, '|', (sizeof tbuf - suffix_pos));
322 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
323 strcpy(message_to_spammer, txt_answer);
324 syslog(LOG_INFO, "RBL: %s\n", txt_answer);
334 * Convert a host name to a dotted quad address.
335 * Returns zero on success or nonzero on failure.
337 * FIXME this is obviously not IPv6 compatible.
339 int hostname_to_dotted_quad(char *addr, char *host) {
344 ch = gethostbyname(host);
346 strcpy(addr, "0.0.0.0");
350 i = (const char *) ch->h_addr_list[0];
351 a1 = ((*i++) & 0xff);
352 a2 = ((*i++) & 0xff);
353 a3 = ((*i++) & 0xff);
354 a4 = ((*i++) & 0xff);
355 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);