1 // Functions which handle hostname/address lookups and resolution
3 // Copyright (c) 1987-2019 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <libcitadel.h>
20 #include <arpa/nameser.h>
21 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
22 #include <arpa/nameser_compat.h>
28 #include "locate_host.h"
30 /* START: some missing macros on OpenBSD 3.9 */
32 #define NS_CMPRSFLGS 0xc0
34 #if !defined(NS_MAXCDNAME) && defined (MAXCDNAME)
35 #define NS_MAXCDNAME MAXCDNAME
37 #if !defined(NS_INT16SZ) && defined(INT16SZ)
38 #define NS_INT16SZ INT16SZ
39 #define NS_INT32SZ INT32SZ
42 # define NS_GET16 GETSHORT
44 /* END: some missing macros on OpenBSD 3.9 */
48 * Given an open client socket, return the host name and IP address at the other end.
49 * (IPv4 and IPv6 compatible)
51 void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket)
53 struct sockaddr_in6 clientaddr;
54 unsigned int addrlen = sizeof(clientaddr);
59 getpeername(client_socket, (struct sockaddr *)&clientaddr, &addrlen);
60 getnameinfo((struct sockaddr *)&clientaddr, addrlen, tbuf, n, NULL, 0, 0);
61 getnameinfo((struct sockaddr *)&clientaddr, addrlen, abuf, na, NULL, 0, NI_NUMERICHOST);
63 /* Convert IPv6-mapped IPv4 addresses back to traditional dotted quad.
65 * Other code here, such as the RBL check, will expect IPv4 addresses to be represented
66 * as dotted-quad, even if they come in over a hybrid IPv6/IPv4 socket.
68 if ( (strlen(abuf) > 7) && (!strncasecmp(abuf, "::ffff:", 7)) ) {
69 if (!strcmp(abuf, tbuf)) strcpy(tbuf, &tbuf[7]);
70 strcpy(abuf, &abuf[7]);
76 * RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
78 #define RESULT_SIZE 4096 /* What is the longest result text we support? */
79 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
82 u_char fixedans[ PACKETSZ ];
84 int need_to_free_answer = 0;
91 static int res_initted = 0;
93 if (!res_initted) { /* only have to do this once */
98 /* Make our DNS query. */
100 if (server_shutting_down) {
101 if (txtbuf != NULL) {
102 snprintf(txtbuf, txtbufsize, "System shutting down");
106 len = res_query(domain, C_IN, T_A, answer, PACKETSZ);
108 /* Was there a problem? If so, the domain doesn't exist. */
110 if (txtbuf != NULL) {
116 if (len > PACKETSZ) {
117 answer = malloc(len);
118 need_to_free_answer = 1;
119 len = res_query(domain, C_IN, T_A, answer, len);
121 if (txtbuf != NULL) {
122 snprintf(txtbuf, txtbufsize, "Message rejected due to known spammer source IP address");
124 if (need_to_free_answer) free(answer);
128 if (server_shutting_down) {
129 if (txtbuf != NULL) {
130 snprintf(txtbuf, txtbufsize, "System shutting down");
132 if (need_to_free_answer) free(answer);
136 result = (char *) malloc(RESULT_SIZE);
139 /* Make another DNS query for textual data; this shouldn't
140 * be a performance hit, since it'll now be cached at the
141 * nameserver we're using.
143 len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ);
144 if (server_shutting_down) {
145 if (txtbuf != NULL) {
146 snprintf(txtbuf, txtbufsize, "System shutting down");
148 if (need_to_free_answer) free(answer);
153 /* Just in case there's no TXT record... */
155 if (txtbuf != NULL) {
156 snprintf(txtbuf, txtbufsize, "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 );
165 while( *cp != '\0' ) {
171 /* This seems to be a bit of magic data that we need to
172 * skip. I wish there were good online documentation
173 * for programming for libresolv, so I'd know what I'm
174 * skipping here. Anyone reading this, feel free to
177 cp += 1 + NS_INT16SZ + NS_INT32SZ;
179 /* Skip the type, class and ttl. */
180 cp += (NS_INT16SZ * 2) + NS_INT32SZ;
182 /* Get the length and end of the buffer. */
186 /* Iterate over any multiple answers we might have. In
187 * this context, it's unlikely, but anyway.
189 rp = (u_char *) result;
190 rend = (u_char *) result + RESULT_SIZE - 1;
191 while (cp < cend && rp < rend) {
194 for (b = a; b > 0 && cp < cend && rp < rend; b--) {
195 if (*cp == '\n' || *cp == '"' || *cp == '\\') {
203 if (txtbuf != NULL) {
205 len = snprintf(txtbuf, txtbufsize, "%s", result);
207 /* Remove nonprintable characters */
208 for (p = txtbuf; *p != '\0'; p++) {
212 len - (p - txtbuf) - 1);
216 if (need_to_free_answer) free(answer);
223 * Check to see if the client host is on some sort of spam list (RBL)
224 * If spammer, returns nonzero and places reason in 'message_to_spammer'
226 int rbl_check(char *cs_addr, char *message_to_spammer)
233 char rbl_domains[SIZ];
234 char txt_answer[1024];
235 struct timeval tx_start;
236 struct timeval tx_finish;
239 strcpy(message_to_spammer, "ok");
240 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
242 if ((strchr(cs_addr, '.')) && (!strchr(cs_addr, ':'))) {
245 sscanf(cs_addr, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
246 snprintf(tbuf, sizeof tbuf, "%d.%d.%d.%d.", a4, a3, a2, a1);
247 suffix_pos = strlen(tbuf);
249 else if ((!strchr(cs_addr, '.')) && (strchr(cs_addr, ':'))) {
252 char workbuf[sizeof tbuf];
255 /* tedious code to expand and reverse an IPv6 address */
256 safestrncpy(tbuf, cs_addr, sizeof tbuf);
257 num_colons = haschar(tbuf, ':');
258 if ((num_colons < 2) || (num_colons > 7))
259 goto finish_rbl; /* badly formed address */
261 /* expand the "::" shorthand */
262 while (num_colons < 7) {
263 ptr = strstr(tbuf, "::");
265 goto finish_rbl; /* badly formed address */
268 strcpy(workbuf, ptr);
270 strcat(ptr, workbuf);
274 /* expand to 32 hex characters with no colons */
275 strcpy(workbuf, tbuf);
276 strcpy(tbuf, "00000000000000000000000000000000");
277 for (i=0; i<8; ++i) {
279 extract_token(tokbuf, workbuf, i, ':', sizeof tokbuf);
280 memcpy(&tbuf[ (i*4) + (4-strlen(tokbuf)) ], tokbuf, strlen(tokbuf) );
282 if (strlen(tbuf) != 32) {
286 /* now reverse it and add dots */
287 strcpy(workbuf, tbuf);
288 for (i=0; i<32; ++i) {
289 tbuf[i*2] = workbuf[31-i];
296 goto finish_rbl; /* unknown address format */
299 /* See if we have any RBL domains configured */
300 num_rbl = get_hosts(rbl_domains, "rbl");
306 /* Try all configured RBL's */
307 for (rbl=0; rbl<num_rbl; ++rbl) {
308 extract_token(&tbuf[suffix_pos], rbl_domains, rbl, '|', (sizeof tbuf - suffix_pos));
310 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
311 strcpy(message_to_spammer, txt_answer);
312 syslog(LOG_INFO, "RBL: %s %s", cs_addr, txt_answer);
317 /* How long did this transaction take? */
318 gettimeofday(&tx_finish, NULL);
320 syslog(LOG_WARNING, "rbl: %s [%ld.%06ld] %s",
322 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
323 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
324 (rc)?"found":"none found"
332 * Convert a host name to a dotted quad address.
333 * Returns zero on success or nonzero on failure.
335 * FIXME this is obviously not IPv6 compatible.
337 int hostname_to_dotted_quad(char *addr, char *host) {
342 ch = gethostbyname(host);
344 strcpy(addr, "0.0.0.0");
348 i = (const char *) ch->h_addr_list[0];
349 a1 = ((*i++) & 0xff);
350 a2 = ((*i++) & 0xff);
351 a3 = ((*i++) & 0xff);
352 a4 = ((*i++) & 0xff);
353 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);