4 * DNS lookup for SMTP sender
12 #include <netinet/in.h>
16 #include <arpa/nameser.h>
20 #include "sysdep_decls.h"
25 #include "internet_addressing.h"
29 * get_hosts() checks the Internet configuration for various types of
30 * entries and returns them in the same format as getmx() does -- fill the
31 * buffer with a delimited list of hosts and return the number of hosts.
33 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
35 int get_hosts(char *mxbuf, char *rectype) {
39 char host[SIZ], type[SIZ];
40 int total_smarthosts = 0;
42 if (inetcfg == NULL) return(0);
45 config_lines = num_tokens(inetcfg, '\n');
46 for (i=0; i<config_lines; ++i) {
47 extract_token(buf, inetcfg, i, '\n');
48 extract_token(host, buf, 0, '|');
49 extract_token(type, buf, 1, '|');
51 if (!strcasecmp(type, rectype)) {
58 return(total_smarthosts);
63 * Compare the preference of two MX records. First check by the actual
64 * number listed in the MX record. If they're identical, randomize the
67 int mx_compare_pref(const void *mx1, const void *mx2) {
71 pref1 = ((const struct mx *)mx1)->pref;
72 pref2 = ((const struct mx *)mx2)->pref;
77 else if (pref1 < pref2) {
89 * Return one or more MX's for a mail destination.
91 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
92 * vertical bar characters, and returns the number of hosts as its return
93 * value. If no MX's are found, it returns 0.
96 int getmx(char *mxbuf, char *dest) {
109 unsigned char *startptr, *endptr, *ptr;
110 char expanded_buf[1024];
111 unsigned short pref, type;
115 struct mx *mxrecs = NULL;
118 /* If we're configured to send all mail to a smart-host, then our
119 * job here is really easy.
121 n = get_hosts(mxbuf, "smarthost");
122 if (n > 0) return(n);
125 * No smart-host? Look up the best MX for a site.
128 #ifndef HAVE_RESOLV_H
131 * On systems with b0rken or non-standard resolver libraries, learn
132 * the MX records by calling "nslookup" from the command line.
140 #else /* HAVE_RESOLV_H */
143 * Make a call to the standard resolver library.
148 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
151 mxrecs = mallok(sizeof(struct mx));
153 strcpy(mxrecs[0].host, dest);
158 /* If we had to truncate, shrink the number to avoid fireworks */
159 if (ret > sizeof(answer))
160 ret = sizeof(answer);
162 startptr = &answer.bytes[0]; /* start and end of buffer */
163 endptr = &answer.bytes[ret];
164 ptr = startptr + HFIXEDSZ; /* advance past header */
166 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
167 if ((ret = dn_skipname(ptr, endptr)) < 0) {
168 lprintf(9, "dn_skipname error\n");
174 memset(expanded_buf, 0, sizeof(expanded_buf));
175 ret = dn_expand(startptr,
185 ptr += INT16SZ + INT32SZ;
194 ret = dn_expand(startptr,
203 if (mxrecs == NULL) {
204 mxrecs = mallok(sizeof(struct mx));
207 mxrecs = reallok(mxrecs,
208 (sizeof(struct mx) * num_mxrecs) );
211 mxrecs[num_mxrecs - 1].pref = pref;
212 strcpy(mxrecs[num_mxrecs - 1].host,
217 #endif /* HAVE_RESOLV_H */
219 /* Sort the MX records by preference */
220 if (num_mxrecs > 1) {
221 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
225 for (n=0; n<num_mxrecs; ++n) {
226 strcat(mxbuf, mxrecs[n].host);