2 * DNS lookup for SMTP sender
4 * Copyright (c) 1987-2021 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, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
19 #include <arpa/nameser.h>
20 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
21 #include <arpa/nameser_compat.h>
24 #include <netinet/in.h>
28 #include <libcitadel.h>
29 #include "sysdep_decls.h"
32 #include "internet_addressing.h"
36 * get_hosts() checks the Internet configuration for various types of
37 * entries and returns them in the same format as getmx() does -- fill the
38 * buffer with a delimited list of hosts and return the number of hosts.
40 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
42 int get_hosts(char *mxbuf, char *rectype) {
46 char host[256], type[256];
47 int total_smarthosts = 0;
49 if (inetcfg == NULL) return(0);
52 config_lines = num_tokens(inetcfg, '\n');
53 for (i=0; i<config_lines; ++i) {
54 extract_token(buf, inetcfg, i, '\n', sizeof buf);
55 extract_token(host, buf, 0, '|', sizeof host);
56 extract_token(type, buf, 1, '|', sizeof type);
58 if (!strcasecmp(type, rectype)) {
65 return(total_smarthosts);
70 * Compare the preference of two MX records. First check by the actual
71 * number listed in the MX record. If they're identical, randomize the
74 int mx_compare_pref(const void *mx1, const void *mx2) {
78 pref1 = ((const struct mx *)mx1)->pref;
79 pref2 = ((const struct mx *)mx2)->pref;
84 else if (pref1 < pref2) {
96 * Return one or more MX's for a mail destination.
98 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
99 * vertical bar characters, and returns the number of hosts as its return
100 * value. If no MX's are found, it returns 0.
103 int getmx(char *mxbuf, char *dest) {
113 unsigned char *startptr, *endptr, *ptr;
114 char expanded_buf[1024];
115 unsigned short pref, type;
118 Array *mxrecords = NULL;
121 // If we're configured to send all mail to a smart-host, then our job here is really easy -- just return those.
122 n = get_hosts(mxbuf, "smarthost");
127 mxrecords = array_new(sizeof(struct mx));
129 // No smart-host? Look up the best MX for a site. Make a call to the resolver library.
130 ret = res_query(dest, C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer));
134 strcpy(mx.host, dest);
135 array_append(mxrecords, &mx);
138 if (ret > sizeof(answer)) { // If we had to truncate, shrink the number to avoid fireworks
139 ret = sizeof(answer);
142 startptr = &answer.bytes[0]; // start and end of buffer
143 endptr = &answer.bytes[ret];
144 ptr = startptr + HFIXEDSZ; // advance past header
146 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
147 if ((ret = dn_skipname(ptr, endptr)) < 0) {
148 syslog(LOG_DEBUG, "domain: dn_skipname error");
154 memset(expanded_buf, 0, sizeof(expanded_buf));
155 ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
160 ptr += INT16SZ + INT32SZ;
169 ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
173 strcpy(mx.host, expanded_buf);
174 array_append(mxrecords, &mx);
179 // Sort the MX records by preference
180 if (array_len(mxrecords) > 1) {
181 array_sort(mxrecords, mx_compare_pref);
184 int num_mxrecs = array_len(mxrecords);
186 for (n=0; n<num_mxrecs; ++n) {
187 strcat(mxbuf, ((struct mx *)array_get_element_at(mxrecords, n))->host);
190 array_free(mxrecords);
192 // Append any fallback smart hosts we have configured.
193 num_mxrecs += get_hosts(&mxbuf[strlen(mxbuf)], "fallbackhost");