4 * DNS lookup for SMTP sender
12 #include <netinet/in.h>
16 #include <arpa/nameser.h>
17 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
18 #include <arpa/nameser_compat.h>
22 #include <libcitadel.h>
23 #include "sysdep_decls.h"
27 #include "internet_addressing.h"
31 * get_hosts() checks the Internet configuration for various types of
32 * entries and returns them in the same format as getmx() does -- fill the
33 * buffer with a delimited list of hosts and return the number of hosts.
35 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
37 int get_hosts(char *mxbuf, char *rectype) {
41 char host[256], type[256];
42 int total_smarthosts = 0;
44 if (inetcfg == NULL) return(0);
47 config_lines = num_tokens(inetcfg, '\n');
48 for (i=0; i<config_lines; ++i) {
49 extract_token(buf, inetcfg, i, '\n', sizeof buf);
50 extract_token(host, buf, 0, '|', sizeof host);
51 extract_token(type, buf, 1, '|', sizeof type);
53 if (!strcasecmp(type, rectype)) {
60 return(total_smarthosts);
65 * Compare the preference of two MX records. First check by the actual
66 * number listed in the MX record. If they're identical, randomize the
69 int mx_compare_pref(const void *mx1, const void *mx2) {
73 pref1 = ((const struct mx *)mx1)->pref;
74 pref2 = ((const struct mx *)mx2)->pref;
79 else if (pref1 < pref2) {
91 * Return one or more MX's for a mail destination.
93 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
94 * vertical bar characters, and returns the number of hosts as its return
95 * value. If no MX's are found, it returns 0.
98 int getmx(char *mxbuf, char *dest) {
108 unsigned char *startptr, *endptr, *ptr;
109 char expanded_buf[1024];
110 unsigned short pref, type;
114 struct mx *mxrecs = NULL;
117 /* If we're configured to send all mail to a smart-host, then our
118 * job here is really easy.
120 n = get_hosts(mxbuf, "smarthost");
121 if (n > 0) return(n);
124 * No smart-host? Look up the best MX for a site.
125 * Make a call to the resolver library.
130 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);
144 startptr = &answer.bytes[0]; /* start and end of buffer */
145 endptr = &answer.bytes[ret];
146 ptr = startptr + HFIXEDSZ; /* advance past header */
148 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
149 if ((ret = dn_skipname(ptr, endptr)) < 0) {
150 lprintf(CTDL_DEBUG, "dn_skipname error\n");
156 memset(expanded_buf, 0, sizeof(expanded_buf));
157 ret = dn_expand(startptr,
167 ptr += INT16SZ + INT32SZ;
176 ret = dn_expand(startptr,
185 if (mxrecs == NULL) {
186 mxrecs = malloc(sizeof(struct mx));
189 mxrecs = realloc(mxrecs,
190 (sizeof(struct mx) * num_mxrecs) );
193 mxrecs[num_mxrecs - 1].pref = pref;
194 strcpy(mxrecs[num_mxrecs - 1].host,
200 /* Sort the MX records by preference */
201 if (num_mxrecs > 1) {
202 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
206 for (n=0; n<num_mxrecs; ++n) {
207 strcat(mxbuf, mxrecs[n].host);