4 * DNS lookup for SMTP sender
7 * Copyright (c) 1987-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
34 #include <arpa/nameser_compat.h>
38 #include <libcitadel.h>
39 #include "sysdep_decls.h"
43 #include "internet_addressing.h"
47 * get_hosts() checks the Internet configuration for various types of
48 * entries and returns them in the same format as getmx() does -- fill the
49 * buffer with a delimited list of hosts and return the number of hosts.
51 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
53 int get_hosts(char *mxbuf, char *rectype) {
57 char host[256], type[256];
58 int total_smarthosts = 0;
60 if (inetcfg == NULL) return(0);
63 config_lines = num_tokens(inetcfg, '\n');
64 for (i=0; i<config_lines; ++i) {
65 extract_token(buf, inetcfg, i, '\n', sizeof buf);
66 extract_token(host, buf, 0, '|', sizeof host);
67 extract_token(type, buf, 1, '|', sizeof type);
69 if (!strcasecmp(type, rectype)) {
76 return(total_smarthosts);
81 * Compare the preference of two MX records. First check by the actual
82 * number listed in the MX record. If they're identical, randomize the
85 int mx_compare_pref(const void *mx1, const void *mx2) {
89 pref1 = ((const struct mx *)mx1)->pref;
90 pref2 = ((const struct mx *)mx2)->pref;
95 else if (pref1 < pref2) {
107 * Return one or more MX's for a mail destination.
109 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
110 * vertical bar characters, and returns the number of hosts as its return
111 * value. If no MX's are found, it returns 0.
114 int getmx(char *mxbuf, char *dest) {
124 unsigned char *startptr, *endptr, *ptr;
125 char expanded_buf[1024];
126 unsigned short pref, type;
130 struct mx *mxrecs = NULL;
133 /* If we're configured to send all mail to a smart-host, then our
134 * job here is really easy.
136 n = get_hosts(mxbuf, "smarthost");
137 if (n > 0) return(n);
140 * No smart-host? Look up the best MX for a site.
141 * Make a call to the resolver library.
146 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
149 mxrecs = malloc(sizeof(struct mx));
151 strcpy(mxrecs[0].host, dest);
156 /* If we had to truncate, shrink the number to avoid fireworks */
157 if (ret > sizeof(answer))
158 ret = sizeof(answer);
160 startptr = &answer.bytes[0]; /* start and end of buffer */
161 endptr = &answer.bytes[ret];
162 ptr = startptr + HFIXEDSZ; /* advance past header */
164 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
165 if ((ret = dn_skipname(ptr, endptr)) < 0) {
166 CtdlLogPrintf(CTDL_DEBUG, "dn_skipname error\n");
172 memset(expanded_buf, 0, sizeof(expanded_buf));
173 ret = dn_expand(startptr,
183 ptr += INT16SZ + INT32SZ;
192 ret = dn_expand(startptr,
201 if (mxrecs == NULL) {
202 mxrecs = malloc(sizeof(struct mx));
205 mxrecs = realloc(mxrecs,
206 (sizeof(struct mx) * num_mxrecs) );
209 mxrecs[num_mxrecs - 1].pref = pref;
210 strcpy(mxrecs[num_mxrecs - 1].host,
216 /* Sort the MX records by preference */
217 if (num_mxrecs > 1) {
218 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
222 for (n=0; n<num_mxrecs; ++n) {
223 strcat(mxbuf, mxrecs[n].host);
228 /* Append any fallback smart hosts we have configured.
230 num_mxrecs += get_hosts(&mxbuf[strlen(mxbuf)], "fallbackhost");
231 CtdlLogPrintf(CTDL_DEBUG, "\033[33m%d: %s\033[0m\n", num_mxrecs, mxbuf);