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, 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 <netinet/in.h>
24 #include <arpa/nameser.h>
25 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
26 #include <arpa/nameser_compat.h>
30 #include <libcitadel.h>
31 #include "sysdep_decls.h"
35 #include "internet_addressing.h"
39 * get_hosts() checks the Internet configuration for various types of
40 * entries and returns them in the same format as getmx() does -- fill the
41 * buffer with a delimited list of hosts and return the number of hosts.
43 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
45 int get_hosts(char *mxbuf, char *rectype) {
49 char host[256], type[256];
50 int total_smarthosts = 0;
52 if (inetcfg == NULL) return(0);
55 config_lines = num_tokens(inetcfg, '\n');
56 for (i=0; i<config_lines; ++i) {
57 extract_token(buf, inetcfg, i, '\n', sizeof buf);
58 extract_token(host, buf, 0, '|', sizeof host);
59 extract_token(type, buf, 1, '|', sizeof type);
61 if (!strcasecmp(type, rectype)) {
68 return(total_smarthosts);
73 * Compare the preference of two MX records. First check by the actual
74 * number listed in the MX record. If they're identical, randomize the
77 int mx_compare_pref(const void *mx1, const void *mx2) {
81 pref1 = ((const struct mx *)mx1)->pref;
82 pref2 = ((const struct mx *)mx2)->pref;
87 else if (pref1 < pref2) {
99 * Return one or more MX's for a mail destination.
101 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
102 * vertical bar characters, and returns the number of hosts as its return
103 * value. If no MX's are found, it returns 0.
106 int getmx(char *mxbuf, char *dest) {
116 unsigned char *startptr, *endptr, *ptr;
117 char expanded_buf[1024];
118 unsigned short pref, type;
122 struct mx *mxrecs = NULL;
125 /* If we're configured to send all mail to a smart-host, then our
126 * job here is really easy.
128 n = get_hosts(mxbuf, "smarthost");
129 if (n > 0) return(n);
132 * No smart-host? Look up the best MX for a site.
133 * Make a call to the resolver library.
138 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
141 mxrecs = malloc(sizeof(struct mx));
143 strcpy(mxrecs[0].host, dest);
148 /* If we had to truncate, shrink the number to avoid fireworks */
149 if (ret > sizeof(answer))
150 ret = sizeof(answer);
152 startptr = &answer.bytes[0]; /* start and end of buffer */
153 endptr = &answer.bytes[ret];
154 ptr = startptr + HFIXEDSZ; /* advance past header */
156 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
157 if ((ret = dn_skipname(ptr, endptr)) < 0) {
158 syslog(LOG_DEBUG, "dn_skipname error\n");
164 memset(expanded_buf, 0, sizeof(expanded_buf));
165 ret = dn_expand(startptr,
175 ptr += INT16SZ + INT32SZ;
184 ret = dn_expand(startptr,
193 if (mxrecs == NULL) {
194 mxrecs = malloc(sizeof(struct mx));
197 mxrecs = realloc(mxrecs,
198 (sizeof(struct mx) * num_mxrecs) );
201 mxrecs[num_mxrecs - 1].pref = pref;
202 strcpy(mxrecs[num_mxrecs - 1].host,
208 /* Sort the MX records by preference */
209 if (num_mxrecs > 1) {
210 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
214 for (n=0; n<num_mxrecs; ++n) {
215 strcat(mxbuf, mxrecs[n].host);
220 /* Append any fallback smart hosts we have configured.
222 num_mxrecs += get_hosts(&mxbuf[strlen(mxbuf)], "fallbackhost");