2 * DNS lookup for SMTP sender
4 * Copyright (c) 1987-2017 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>
25 #include <libcitadel.h>
26 #include "sysdep_decls.h"
29 #include "internet_addressing.h"
33 * get_hosts() checks the Internet configuration for various types of
34 * entries and returns them in the same format as getmx() does -- fill the
35 * buffer with a delimited list of hosts and return the number of hosts.
37 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
39 int get_hosts(char *mxbuf, char *rectype) {
43 char host[256], type[256];
44 int total_smarthosts = 0;
46 if (inetcfg == NULL) return(0);
49 config_lines = num_tokens(inetcfg, '\n');
50 for (i=0; i<config_lines; ++i) {
51 extract_token(buf, inetcfg, i, '\n', sizeof buf);
52 extract_token(host, buf, 0, '|', sizeof host);
53 extract_token(type, buf, 1, '|', sizeof type);
55 if (!strcasecmp(type, rectype)) {
62 return(total_smarthosts);
67 * Compare the preference of two MX records. First check by the actual
68 * number listed in the MX record. If they're identical, randomize the
71 int mx_compare_pref(const void *mx1, const void *mx2) {
75 pref1 = ((const struct mx *)mx1)->pref;
76 pref2 = ((const struct mx *)mx2)->pref;
81 else if (pref1 < pref2) {
93 * Return one or more MX's for a mail destination.
95 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
96 * vertical bar characters, and returns the number of hosts as its return
97 * value. If no MX's are found, it returns 0.
100 int getmx(char *mxbuf, char *dest) {
110 unsigned char *startptr, *endptr, *ptr;
111 char expanded_buf[1024];
112 unsigned short pref, type;
116 struct mx *mxrecs = NULL;
119 /* If we're configured to send all mail to a smart-host, then our
120 * job here is really easy.
122 n = get_hosts(mxbuf, "smarthost");
123 if (n > 0) return(n);
126 * No smart-host? Look up the best MX for a site.
127 * Make a call to the resolver library.
130 ret = res_query(dest, C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer));
133 mxrecs = malloc(sizeof(struct mx));
135 strcpy(mxrecs[0].host, dest);
140 /* If we had to truncate, shrink the number to avoid fireworks */
141 if (ret > sizeof(answer)) {
142 ret = sizeof(answer);
145 startptr = &answer.bytes[0]; // start and end of buffer
146 endptr = &answer.bytes[ret];
147 ptr = startptr + HFIXEDSZ; // advance past header
149 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
150 if ((ret = dn_skipname(ptr, endptr)) < 0) {
151 syslog(LOG_DEBUG, "domain: dn_skipname error");
157 memset(expanded_buf, 0, sizeof(expanded_buf));
158 ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
163 ptr += INT16SZ + INT32SZ;
172 ret = dn_expand(startptr, endptr, ptr, expanded_buf, sizeof(expanded_buf));
176 if (mxrecs == NULL) {
177 mxrecs = malloc(sizeof(struct mx));
180 mxrecs = realloc(mxrecs, (sizeof(struct mx) * num_mxrecs) );
183 mxrecs[num_mxrecs - 1].pref = pref;
184 strcpy(mxrecs[num_mxrecs - 1].host, expanded_buf);
189 /* Sort the MX records by preference */
190 if (num_mxrecs > 1) {
191 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
195 for (n=0; n<num_mxrecs; ++n) {
196 strcat(mxbuf, mxrecs[n].host);
202 * Append any fallback smart hosts we have configured.
204 num_mxrecs += get_hosts(&mxbuf[strlen(mxbuf)], "fallbackhost");