2 * DNS lookup for SMTP sender
4 * Copyright (c) 1987-2009 by the citadel.org team
6 * This program is free 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>
29 #include <arpa/nameser.h>
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 #include <arpa/nameser_compat.h>
35 #include <libcitadel.h>
36 #include "sysdep_decls.h"
40 #include "internet_addressing.h"
44 * get_hosts() checks the Internet configuration for various types of
45 * entries and returns them in the same format as getmx() does -- fill the
46 * buffer with a delimited list of hosts and return the number of hosts.
48 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
50 int get_hosts(char *mxbuf, char *rectype) {
54 char host[256], type[256];
55 int total_smarthosts = 0;
57 if (inetcfg == NULL) return(0);
60 config_lines = num_tokens(inetcfg, '\n');
61 for (i=0; i<config_lines; ++i) {
62 extract_token(buf, inetcfg, i, '\n', sizeof buf);
63 extract_token(host, buf, 0, '|', sizeof host);
64 extract_token(type, buf, 1, '|', sizeof type);
66 if (!strcasecmp(type, rectype)) {
73 return(total_smarthosts);
78 * Compare the preference of two MX records. First check by the actual
79 * number listed in the MX record. If they're identical, randomize the
82 int mx_compare_pref(const void *mx1, const void *mx2) {
86 pref1 = ((const struct mx *)mx1)->pref;
87 pref2 = ((const struct mx *)mx2)->pref;
92 else if (pref1 < pref2) {
104 * Return one or more MX's for a mail destination.
106 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
107 * vertical bar characters, and returns the number of hosts as its return
108 * value. If no MX's are found, it returns 0.
111 int getmx(char *mxbuf, char *dest) {
121 unsigned char *startptr, *endptr, *ptr;
122 char expanded_buf[1024];
123 unsigned short pref, type;
127 struct mx *mxrecs = NULL;
130 /* If we're configured to send all mail to a smart-host, then our
131 * job here is really easy.
133 n = get_hosts(mxbuf, "smarthost");
134 if (n > 0) return(n);
137 * No smart-host? Look up the best MX for a site.
138 * Make a call to the resolver library.
143 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
146 mxrecs = malloc(sizeof(struct mx));
148 strcpy(mxrecs[0].host, dest);
153 /* If we had to truncate, shrink the number to avoid fireworks */
154 if (ret > sizeof(answer))
155 ret = sizeof(answer);
157 startptr = &answer.bytes[0]; /* start and end of buffer */
158 endptr = &answer.bytes[ret];
159 ptr = startptr + HFIXEDSZ; /* advance past header */
161 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
162 if ((ret = dn_skipname(ptr, endptr)) < 0) {
163 CtdlLogPrintf(CTDL_DEBUG, "dn_skipname error\n");
169 memset(expanded_buf, 0, sizeof(expanded_buf));
170 ret = dn_expand(startptr,
180 ptr += INT16SZ + INT32SZ;
189 ret = dn_expand(startptr,
198 if (mxrecs == NULL) {
199 mxrecs = malloc(sizeof(struct mx));
202 mxrecs = realloc(mxrecs,
203 (sizeof(struct mx) * num_mxrecs) );
206 mxrecs[num_mxrecs - 1].pref = pref;
207 strcpy(mxrecs[num_mxrecs - 1].host,
213 /* Sort the MX records by preference */
214 if (num_mxrecs > 1) {
215 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
219 for (n=0; n<num_mxrecs; ++n) {
220 strcat(mxbuf, mxrecs[n].host);
225 /* Append any fallback smart hosts we have configured.
227 num_mxrecs += get_hosts(&mxbuf[strlen(mxbuf)], "fallbackhost");