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.
7 // The program is distributed without any warranty, expressed or implied.
13 #include <arpa/nameser.h>
14 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
15 #include <arpa/nameser_compat.h>
18 #include <netinet/in.h>
22 #include <libcitadel.h>
23 #include "sysdep_decls.h"
26 #include "internet_addressing.h"
29 // get_hosts() checks the Internet configuration for various types of
30 // entries and returns them in the same format as getmx() does -- fill the
31 // buffer with a delimited list of hosts and return the number of hosts.
33 // This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
34 int get_hosts(char *mxbuf, char *rectype) {
38 char host[256], type[256];
39 int total_smarthosts = 0;
41 if (inetcfg == NULL) return(0);
44 config_lines = num_tokens(inetcfg, '\n');
45 for (i=0; i<config_lines; ++i) {
46 extract_token(buf, inetcfg, i, '\n', sizeof buf);
47 extract_token(host, buf, 0, '|', sizeof host);
48 extract_token(type, buf, 1, '|', sizeof type);
50 if (!strcasecmp(type, rectype)) {
57 return(total_smarthosts);
61 // Compare the preference of two MX records. First check by the actual
62 // number listed in the MX record. If they're identical, randomize the
64 int mx_compare_pref(const void *mx1, const void *mx2) {
68 pref1 = ((const struct mx *)mx1)->pref;
69 pref2 = ((const struct mx *)mx2)->pref;
74 else if (pref1 < pref2) {
85 // Return one or more MX's for a mail destination.
87 // Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
88 // vertical bar characters, and returns the number of hosts as its return
89 // value. If no MX's are found, it returns 0.
90 int getmx(char *mxbuf, char *dest) {
100 unsigned char *startptr, *endptr, *ptr;
101 char expanded_buf[1024];
102 unsigned short pref, type;
105 Array *mxrecords = NULL;
108 // If we're configured to send all mail to a smart-host, then our job here is really easy -- just return those.
109 n = get_hosts(mxbuf, "smarthost");
114 mxrecords = array_new(sizeof(struct mx));
116 // No smart-host? Look up the best MX for a site. Make a call to the resolver library.
117 ret = res_query(dest, C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer));
121 strcpy(mx.host, dest);
122 array_append(mxrecords, &mx);
125 if (ret > sizeof(answer)) { // If we had to truncate, shrink the number to avoid fireworks
126 ret = sizeof(answer);
129 startptr = &answer.bytes[0]; // start and end of buffer
130 endptr = &answer.bytes[ret];
131 ptr = startptr + HFIXEDSZ; // advance past header
133 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
134 if ((ret = dn_skipname(ptr, endptr)) < 0) {
135 syslog(LOG_DEBUG, "domain: dn_skipname error");
141 memset(expanded_buf, 0, sizeof(expanded_buf));
142 ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
147 ptr += INT16SZ + INT32SZ;
156 ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
160 strcpy(mx.host, expanded_buf);
161 array_append(mxrecords, &mx);
166 // Sort the MX records by preference
167 if (array_len(mxrecords) > 1) {
168 array_sort(mxrecords, mx_compare_pref);
171 int num_mxrecs = array_len(mxrecords);
173 for (n=0; n<num_mxrecs; ++n) {
174 strcat(mxbuf, ((struct mx *)array_get_element_at(mxrecords, n))->host);
177 array_free(mxrecords);
179 // Append any fallback smart hosts we have configured.
180 num_mxrecs += get_hosts(&mxbuf[strlen(mxbuf)], "fallbackhost");