2 * DNS lookup for SMTP sender
4 * Copyright (c) 1987-2011 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 as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
36 #include <libcitadel.h>
37 #include "sysdep_decls.h"
41 #include "internet_addressing.h"
45 * get_hosts() checks the Internet configuration for various types of
46 * entries and returns them in the same format as getmx() does -- fill the
47 * buffer with a delimited list of hosts and return the number of hosts.
49 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
51 int get_hosts(char *mxbuf, char *rectype) {
55 char host[256], type[256];
56 int total_smarthosts = 0;
58 if (inetcfg == NULL) return(0);
61 config_lines = num_tokens(inetcfg, '\n');
62 for (i=0; i<config_lines; ++i) {
63 extract_token(buf, inetcfg, i, '\n', sizeof buf);
64 extract_token(host, buf, 0, '|', sizeof host);
65 extract_token(type, buf, 1, '|', sizeof type);
67 if (!strcasecmp(type, rectype)) {
74 return(total_smarthosts);
79 * Compare the preference of two MX records. First check by the actual
80 * number listed in the MX record. If they're identical, randomize the
83 int mx_compare_pref(const void *mx1, const void *mx2) {
87 pref1 = ((const struct mx *)mx1)->pref;
88 pref2 = ((const struct mx *)mx2)->pref;
93 else if (pref1 < pref2) {
105 * Return one or more MX's for a mail destination.
107 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
108 * vertical bar characters, and returns the number of hosts as its return
109 * value. If no MX's are found, it returns 0.
112 int getmx(char *mxbuf, char *dest) {
122 unsigned char *startptr, *endptr, *ptr;
123 char expanded_buf[1024];
124 unsigned short pref, type;
128 struct mx *mxrecs = NULL;
131 /* If we're configured to send all mail to a smart-host, then our
132 * job here is really easy.
134 n = get_hosts(mxbuf, "smarthost");
135 if (n > 0) return(n);
138 * No smart-host? Look up the best MX for a site.
139 * Make a call to the resolver library.
144 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
147 mxrecs = malloc(sizeof(struct mx));
149 strcpy(mxrecs[0].host, dest);
154 /* If we had to truncate, shrink the number to avoid fireworks */
155 if (ret > sizeof(answer))
156 ret = sizeof(answer);
158 startptr = &answer.bytes[0]; /* start and end of buffer */
159 endptr = &answer.bytes[ret];
160 ptr = startptr + HFIXEDSZ; /* advance past header */
162 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
163 if ((ret = dn_skipname(ptr, endptr)) < 0) {
164 syslog(LOG_DEBUG, "dn_skipname error\n");
170 memset(expanded_buf, 0, sizeof(expanded_buf));
171 ret = dn_expand(startptr,
181 ptr += INT16SZ + INT32SZ;
190 ret = dn_expand(startptr,
199 if (mxrecs == NULL) {
200 mxrecs = malloc(sizeof(struct mx));
203 mxrecs = realloc(mxrecs,
204 (sizeof(struct mx) * num_mxrecs) );
207 mxrecs[num_mxrecs - 1].pref = pref;
208 strcpy(mxrecs[num_mxrecs - 1].host,
214 /* Sort the MX records by preference */
215 if (num_mxrecs > 1) {
216 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
220 for (n=0; n<num_mxrecs; ++n) {
221 strcat(mxbuf, mxrecs[n].host);
226 /* Append any fallback smart hosts we have configured.
228 num_mxrecs += get_hosts(&mxbuf[strlen(mxbuf)], "fallbackhost");