1 // Functions which handle hostname/address lookups and resolution
3 // Copyright (c) 1987-2024 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>
17 #include <libcitadel.h>
21 #include <arpa/nameser.h>
22 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
23 #include <arpa/nameser_compat.h>
29 #include "locate_host.h"
31 // START: some missing macros on OpenBSD 3.9
33 #define NS_CMPRSFLGS 0xc0
35 #if !defined(NS_MAXCDNAME) && defined (MAXCDNAME)
36 #define NS_MAXCDNAME MAXCDNAME
38 #if !defined(NS_INT16SZ) && defined(INT16SZ)
39 #define NS_INT16SZ INT16SZ
40 #define NS_INT32SZ INT32SZ
43 # define NS_GET16 GETSHORT
45 // 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)
50 void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket) {
51 struct sockaddr_in6 clientaddr;
52 unsigned int addrlen = sizeof(clientaddr);
57 getpeername(client_socket, (struct sockaddr *)&clientaddr, &addrlen);
58 getnameinfo((struct sockaddr *)&clientaddr, addrlen, tbuf, n, NULL, 0, 0);
59 getnameinfo((struct sockaddr *)&clientaddr, addrlen, abuf, na, NULL, 0, NI_NUMERICHOST);
61 // Convert IPv6-mapped IPv4 addresses back to traditional dotted quad.
63 // Other code here, such as the RBL check, will expect IPv4 addresses to be represented
64 // as dotted-quad, even if they come in over a hybrid IPv6/IPv4 socket.
65 if ( (strlen(abuf) > 7) && (!strncasecmp(abuf, "::ffff:", 7)) ) {
66 if (!strcmp(abuf, tbuf)) strcpy(tbuf, &tbuf[7]);
67 strcpy(abuf, &abuf[7]);
72 // RBL check written by Edward S. Marshall [http://rblcheck.sourceforge.net]
73 #define RESULT_SIZE 4096 // What is the longest result text we support?
74 int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
77 u_char fixedans[ PACKETSZ ];
79 int need_to_free_answer = 0;
86 static int res_initted = 0;
88 if (!res_initted) { // only have to do this once
93 // Make our DNS query.
95 if (server_shutting_down) {
97 snprintf(txtbuf, txtbufsize, "System shutting down");
101 len = res_query(domain, C_IN, T_A, answer, PACKETSZ);
103 // Was there a problem? If so, the domain doesn't exist.
105 if (txtbuf != NULL) {
111 if (len > PACKETSZ) {
112 answer = malloc(len);
113 need_to_free_answer = 1;
114 len = res_query(domain, C_IN, T_A, answer, len);
116 if (txtbuf != NULL) {
117 snprintf(txtbuf, txtbufsize, "Message rejected due to known spammer source IP address");
119 if (need_to_free_answer) free(answer);
123 if (server_shutting_down) {
124 if (txtbuf != NULL) {
125 snprintf(txtbuf, txtbufsize, "System shutting down");
127 if (need_to_free_answer) free(answer);
131 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.
137 len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ);
138 if (server_shutting_down) {
139 if (txtbuf != NULL) {
140 snprintf(txtbuf, txtbufsize, "System shutting down");
142 if (need_to_free_answer) free(answer);
147 // Just in case there's no TXT record...
149 if (txtbuf != NULL) {
150 snprintf(txtbuf, txtbufsize, "Message rejected due to known spammer source IP address");
152 if (need_to_free_answer) free(answer);
157 // Skip the header and the address we queried.
158 cp = answer + sizeof( HEADER );
159 while( *cp != '\0' ) {
165 // This seems to be a bit of magic data that we need to
166 // skip. I wish there were good online documentation
167 // for programming for libresolv, so I'd know what I'm
168 // skipping here. Anyone reading this, feel free to
170 cp += 1 + NS_INT16SZ + NS_INT32SZ;
172 // Skip the type, class and ttl.
173 cp += (NS_INT16SZ * 2) + NS_INT32SZ;
175 // Get the length and end of the buffer.
179 // Iterate over any multiple answers we might have. In
180 // this context, it's unlikely, but anyway.
181 rp = (u_char *) result;
182 rend = (u_char *) result + RESULT_SIZE - 1;
183 while (cp < cend && rp < rend) {
186 for (b = a; b > 0 && cp < cend && rp < rend; b--) {
187 if (*cp == '\n' || *cp == '"' || *cp == '\\') {
195 if (txtbuf != NULL) {
197 len = snprintf(txtbuf, txtbufsize, "%s", result);
199 // Remove nonprintable characters
200 for (p = txtbuf; *p != '\0'; p++) {
204 len - (p - txtbuf) - 1);
208 if (need_to_free_answer) free(answer);
214 // Check to see if the client host is on some sort of spam list (RBL)
215 // If spammer, returns nonzero and places reason in 'message_to_spammer'
216 int rbl_check(char *cs_addr, char *message_to_spammer) {
222 char rbl_domains[SIZ];
223 char txt_answer[1024];
224 struct timeval tx_start;
225 struct timeval tx_finish;
228 strcpy(message_to_spammer, "ok");
229 gettimeofday(&tx_start, NULL); // start a stopwatch for performance timing
231 if ((strchr(cs_addr, '.')) && (!strchr(cs_addr, ':'))) {
234 sscanf(cs_addr, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
235 snprintf(tbuf, sizeof tbuf, "%d.%d.%d.%d.", a4, a3, a2, a1);
236 suffix_pos = strlen(tbuf);
238 else if ((!strchr(cs_addr, '.')) && (strchr(cs_addr, ':'))) {
241 char workbuf[sizeof tbuf];
244 // tedious code to expand and reverse an IPv6 address
245 safestrncpy(tbuf, cs_addr, sizeof tbuf);
246 num_colons = haschar(tbuf, ':');
247 if ((num_colons < 2) || (num_colons > 7))
248 goto finish_rbl; // badly formed address
250 // expand the "::" shorthand
251 while (num_colons < 7) {
252 ptr = strstr(tbuf, "::");
254 goto finish_rbl; // badly formed address
257 strcpy(workbuf, ptr);
259 strcat(ptr, workbuf);
263 // expand to 32 hex characters with no colons
264 strcpy(workbuf, tbuf);
265 strcpy(tbuf, "00000000000000000000000000000000");
266 for (i=0; i<8; ++i) {
268 extract_token(tokbuf, workbuf, i, ':', sizeof tokbuf);
269 memcpy(&tbuf[ (i*4) + (4-strlen(tokbuf)) ], tokbuf, strlen(tokbuf) );
271 if (strlen(tbuf) != 32) {
275 // now reverse it and add dots
276 strcpy(workbuf, tbuf);
277 for (i=0; i<32; ++i) {
278 tbuf[i*2] = workbuf[31-i];
285 goto finish_rbl; // unknown address format
288 // See if we have any RBL domains configured
289 num_rbl = get_hosts(rbl_domains, "rbl");
295 // Try all configured RBL's
296 for (rbl=0; rbl<num_rbl; ++rbl) {
297 extract_token(&tbuf[suffix_pos], rbl_domains, rbl, '|', (sizeof tbuf - suffix_pos));
299 if (rblcheck_backend(tbuf, txt_answer, sizeof txt_answer)) {
300 strcpy(message_to_spammer, txt_answer);
301 syslog(LOG_INFO, "RBL: %s %s", cs_addr, txt_answer);
306 // How long did this transaction take?
307 gettimeofday(&tx_finish, NULL);
309 syslog(LOG_WARNING, "rbl: %s [%ld.%06ld] %s",
311 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
312 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
313 (rc)?"found":"none found"
320 // Convert a host name to a dotted quad address.
321 // Returns zero on success or nonzero on failure.
323 // FIXME this is obviously not IPv6 compatible.
324 int hostname_to_dotted_quad(char *addr, char *host) {
329 ch = gethostbyname(host);
331 strcpy(addr, "0.0.0.0");
335 i = (const char *) ch->h_addr_list[0];
336 a1 = ((*i++) & 0xff);
337 a2 = ((*i++) & 0xff);
338 a3 = ((*i++) & 0xff);
339 a4 = ((*i++) & 0xff);
340 sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);