1 // DNS lookup for SMTP sender
3 // Copyright (c) 1987-2022 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.
12 #include <arpa/nameser.h>
13 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
14 #include <arpa/nameser_compat.h>
17 #include <netinet/in.h>
21 #include <libcitadel.h>
22 #include "sysdep_decls.h"
23 #include "citadel_defs.h"
25 #include "internet_addressing.h"
28 // get_hosts() checks the Internet configuration for various types of
29 // entries and returns them in the same format as getmx() does -- fill the
30 // buffer with a delimited list of hosts and return the number of hosts.
32 // This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
33 int get_hosts(char *mxbuf, char *rectype) {
37 char host[256], type[256];
38 int total_smarthosts = 0;
40 if (inetcfg == NULL) return(0);
43 config_lines = num_tokens(inetcfg, '\n');
44 for (i=0; i<config_lines; ++i) {
45 extract_token(buf, inetcfg, i, '\n', sizeof buf);
46 extract_token(host, buf, 0, '|', sizeof host);
47 extract_token(type, buf, 1, '|', sizeof type);
49 if (!strcasecmp(type, rectype)) {
56 return(total_smarthosts);
60 // Compare the preference of two MX records. First check by the actual
61 // number listed in the MX record. If they're identical, randomize the
63 int mx_compare_pref(const void *mx1, const void *mx2) {
67 pref1 = ((const struct mx *)mx1)->pref;
68 pref2 = ((const struct mx *)mx2)->pref;
73 else if (pref1 < pref2) {
84 // Return one or more MX's for a mail destination.
86 // Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
87 // vertical bar characters, and returns the number of hosts as its return
88 // value. If no MX's are found, it returns 0.
89 int getmx(char *mxbuf, char *dest) {
99 unsigned char *startptr, *endptr, *ptr;
100 char expanded_buf[1024];
101 unsigned short pref, type;
104 Array *mxrecords = NULL;
107 // If we're configured to send all mail to a smart-host, then our job here is really easy -- just return those.
108 n = get_hosts(mxbuf, "smarthost");
113 mxrecords = array_new(sizeof(struct mx));
115 // No smart-host? Look up the best MX for a site. Make a call to the resolver library.
116 ret = res_query(dest, C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer));
120 strcpy(mx.host, dest);
121 array_append(mxrecords, &mx);
124 if (ret > sizeof(answer)) { // If we had to truncate, shrink the number to avoid fireworks
125 ret = sizeof(answer);
128 startptr = &answer.bytes[0]; // start and end of buffer
129 endptr = &answer.bytes[ret];
130 ptr = startptr + HFIXEDSZ; // advance past header
132 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
133 if ((ret = dn_skipname(ptr, endptr)) < 0) {
134 syslog(LOG_DEBUG, "domain: dn_skipname error");
140 memset(expanded_buf, 0, sizeof(expanded_buf));
141 ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
146 ptr += INT16SZ + INT32SZ;
155 ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
159 strcpy(mx.host, expanded_buf);
160 array_append(mxrecords, &mx);
165 // Sort the MX records by preference
166 if (array_len(mxrecords) > 1) {
167 array_sort(mxrecords, mx_compare_pref);
170 int num_mxrecs = array_len(mxrecords);
172 for (n=0; n<num_mxrecs; ++n) {
173 strcat(mxbuf, ((struct mx *)array_get_element_at(mxrecords, n))->host);
176 array_free(mxrecords);
178 // Append any fallback smart hosts we have configured.
179 num_mxrecs += get_hosts(&mxbuf[strlen(mxbuf)], "fallbackhost");