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>
56 * Given an open client socket, return the host name and IP address at the other end.
57 * (IPv4 and IPv6 compatible)
59 void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket)
61 struct sockaddr_in6 clientaddr;
62 unsigned int addrlen = sizeof(clientaddr);
67 getpeername(client_socket, (struct sockaddr *)&clientaddr, &addrlen);
68 getnameinfo((struct sockaddr *)&clientaddr, addrlen, tbuf, n, NULL, 0, 0);
69 getnameinfo((struct sockaddr *)&clientaddr, addrlen, abuf, na, NULL, 0, NI_NUMERICHOST);
71 /* Convert IPv6-mapped IPv4 addresses back to traditional dotted quad.
73 * Other code here, such as the RBL check, will expect IPv4 addresses to be represented
74 * as dotted-quad, even if they come in over a hybrid IPv6/IPv4 socket.
76 if ( (strlen(abuf) > 7) && (!strncasecmp(abuf, "::ffff:", 7)) ) {
77 if (!strcmp(abuf, tbuf)) strcpy(tbuf, &tbuf[7]);
78 strcpy(abuf, &abuf[7]);
84 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
86 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
87 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
90 u_char fixedans[ PACKETSZ ];
92 int need_to_free_answer = 0;
99 static int res_initted = 0;
101 if (!res_initted) { /* only have to do this once */
106 /* Make our DNS query. */
108 if (server_shutting_down)
110 if (txtbuf != NULL) {
111 snprintf(txtbuf, txtbufsize, "System shutting down");
115 len = res_query(domain, C_IN, T_A, answer, PACKETSZ);
117 /* Was there a problem? If so, the domain doesn't exist. */
119 if (txtbuf != NULL) {
127 answer = malloc(len);
128 need_to_free_answer = 1;
129 len = res_query(domain, C_IN, T_A, answer, len);
131 if (txtbuf != NULL) {
132 snprintf(txtbuf, txtbufsize,
133 "Message rejected due to known spammer source IP address");
135 if (need_to_free_answer) free(answer);
139 if (server_shutting_down)
142 snprintf(txtbuf, txtbufsize, "System shutting down");
143 if (need_to_free_answer) free(answer);
147 result = (char *) malloc(RESULT_SIZE);
151 /* Make another DNS query for textual data; this shouldn't
152 * be a performance hit, since it'll now be cached at the
153 * nameserver we're using.
155 len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ);
156 if (server_shutting_down)
158 if (txtbuf != NULL) {
159 snprintf(txtbuf, txtbufsize, "System shutting down");
161 if (need_to_free_answer) free(answer);
166 /* Just in case there's no TXT record... */
169 if (txtbuf != NULL) {
170 snprintf(txtbuf, txtbufsize,
171 "Message rejected due to known spammer source IP address");
173 if (need_to_free_answer) free(answer);
178 /* Skip the header and the address we queried. */
179 cp = answer + sizeof( HEADER );
187 /* This seems to be a bit of magic data that we need to
188 * skip. I wish there were good online documentation
189 * for programming for libresolv, so I'd know what I'm
190 * skipping here. Anyone reading this, feel free to
193 cp += 1 + NS_INT16SZ + NS_INT32SZ;
195 /* Skip the type, class and ttl. */
196 cp += (NS_INT16SZ * 2) + NS_INT32SZ;
198 /* Get the length and end of the buffer. */
202 /* Iterate over any multiple answers we might have. In
203 * this context, it's unlikely, but anyway.
205 rp = (u_char *) result;
206 rend = (u_char *) result + RESULT_SIZE - 1;
207 while (cp < cend && rp < rend)
211 for (b = a; b > 0 && cp < cend && rp < rend; b--)
213 if (*cp == '\n' || *cp == '"' || *cp == '\\')
221 if (txtbuf != NULL) {
222 snprintf(txtbuf, txtbufsize, "%s", result);
224 /* Remove nonprintable characters */
225 for (p=txtbuf; *p; ++p) {
226 if (!isprint(*p)) strcpy(p, p+1);
228 if (need_to_free_answer) free(answer);
235 * Check to see if the client host is on some sort of spam list (RBL)
236 * If spammer, returns nonzero and places reason in 'message_to_spammer'
238 int rbl_check(char *message_to_spammer)
244 char rbl_domains[SIZ];
245 char txt_answer[1024];
248 strcpy(message_to_spammer, "ok");
250 if ((strchr(CC->cs_addr, '.')) && (!strchr(CC->cs_addr, ':'))) {
254 sscanf(CC->cs_addr, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
255 snprintf(tbuf, sizeof tbuf, "%d.%d.%d.%d.", a4, a3, a2, a1);
256 suffix_pos = strlen(tbuf);
258 else if ((!strchr(CC->cs_addr, '.')) && (strchr(CC->cs_addr, ':'))) {
261 char workbuf[sizeof tbuf];
266 /* tedious code to expand and reverse an IPv6 address */
267 safestrncpy(tbuf, CC->cs_addr, sizeof tbuf);
268 num_colons = haschar(tbuf, ':');
269 if ((num_colons < 2) || (num_colons > 7)) return(0); /* badly formed address */
271 /* expand the "::" shorthand */
272 while (num_colons < 7) {
273 ptr = strstr(tbuf, "::");
274 if (!ptr) return(0); /* badly formed address */
276 strcpy(workbuf, ptr);
278 strcat(ptr, workbuf);
282 /* expand to 32 hex characters with no colons */
283 strcpy(workbuf, tbuf);
284 strcpy(tbuf, "00000000000000000000000000000000");
285 for (i=0; i<8; ++i) {
287 extract_token(tokbuf, workbuf, i, ':', sizeof tokbuf);
289 memcpy(&tbuf[ (i*4) + (4-strlen(tokbuf)) ], tokbuf, strlen(tokbuf) );
291 if (strlen(tbuf) != 32) return(0);
293 /* now reverse it and add dots */
294 strcpy(workbuf, tbuf);
295 for (i=0; i<32; ++i) {
296 tbuf[i*2] = workbuf[31-i];
303 return(0); /* unknown address format */
306 /* See if we have any RBL domains configured */
307 num_rbl = get_hosts(rbl_domains, "rbl");
308 if (num_rbl < 1) return(0);
310 /* Try all configured RBL's */
311 for (rbl=0; rbl<num_rbl; ++rbl) {
312 extract_token(&tbuf[suffix_pos], rbl_domains, rbl, '|', (sizeof tbuf - suffix_pos));
314 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
315 strcpy(message_to_spammer, txt_answer);
316 syslog(LOG_INFO, "RBL: %s\n", txt_answer);
326 * Convert a host name to a dotted quad address.
327 * Returns zero on success or nonzero on failure.
329 * FIXME this is obviously not IPv6 compatible.
331 int hostname_to_dotted_quad(char *addr, char *host) {
336 ch = gethostbyname(host);
338 strcpy(addr, "0.0.0.0");
342 i = (const char *) ch->h_addr_list[0];
343 a1 = ((*i++) & 0xff);
344 a2 = ((*i++) & 0xff);
345 a3 = ((*i++) & 0xff);
346 a4 = ((*i++) & 0xff);
347 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);