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>
23 #include "sysdep_decls.h"
28 #include "internet_addressing.h"
32 * get_hosts() checks the Internet configuration for various types of
33 * entries and returns them in the same format as getmx() does -- fill the
34 * buffer with a delimited list of hosts and return the number of hosts.
36 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
38 int get_hosts(char *mxbuf, char *rectype) {
42 char host[SIZ], type[SIZ];
43 int total_smarthosts = 0;
45 if (inetcfg == NULL) return(0);
48 config_lines = num_tokens(inetcfg, '\n');
49 for (i=0; i<config_lines; ++i) {
50 extract_token(buf, inetcfg, i, '\n');
51 extract_token(host, buf, 0, '|');
52 extract_token(type, buf, 1, '|');
54 if (!strcasecmp(type, rectype)) {
61 return(total_smarthosts);
66 * Compare the preference of two MX records. First check by the actual
67 * number listed in the MX record. If they're identical, randomize the
70 int mx_compare_pref(const void *mx1, const void *mx2) {
74 pref1 = ((const struct mx *)mx1)->pref;
75 pref2 = ((const struct mx *)mx2)->pref;
80 else if (pref1 < pref2) {
92 * Return one or more MX's for a mail destination.
94 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
95 * vertical bar characters, and returns the number of hosts as its return
96 * value. If no MX's are found, it returns 0.
99 int getmx(char *mxbuf, char *dest) {
112 unsigned char *startptr, *endptr, *ptr;
113 char expanded_buf[1024];
114 unsigned short pref, type;
118 struct mx *mxrecs = NULL;
121 /* If we're configured to send all mail to a smart-host, then our
122 * job here is really easy.
124 n = get_hosts(mxbuf, "smarthost");
125 if (n > 0) return(n);
128 * No smart-host? Look up the best MX for a site.
131 #ifndef HAVE_RESOLV_H
134 * On systems with b0rken or non-standard resolver libraries, learn
135 * the MX records by calling "nslookup" from the command line.
143 #else /* HAVE_RESOLV_H */
146 * Make a call to the standard resolver library.
151 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
154 mxrecs = mallok(sizeof(struct mx));
156 strcpy(mxrecs[0].host, dest);
161 /* If we had to truncate, shrink the number to avoid fireworks */
162 if (ret > sizeof(answer))
163 ret = sizeof(answer);
165 startptr = &answer.bytes[0]; /* start and end of buffer */
166 endptr = &answer.bytes[ret];
167 ptr = startptr + HFIXEDSZ; /* advance past header */
169 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
170 if ((ret = dn_skipname(ptr, endptr)) < 0) {
171 lprintf(9, "dn_skipname error\n");
177 memset(expanded_buf, 0, sizeof(expanded_buf));
178 ret = dn_expand(startptr,
188 ptr += INT16SZ + INT32SZ;
197 ret = dn_expand(startptr,
206 if (mxrecs == NULL) {
207 mxrecs = mallok(sizeof(struct mx));
210 mxrecs = reallok(mxrecs,
211 (sizeof(struct mx) * num_mxrecs) );
214 mxrecs[num_mxrecs - 1].pref = pref;
215 strcpy(mxrecs[num_mxrecs - 1].host,
220 #endif /* HAVE_RESOLV_H */
222 /* Sort the MX records by preference */
223 if (num_mxrecs > 1) {
224 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
228 for (n=0; n<num_mxrecs; ++n) {
229 strcat(mxbuf, mxrecs[n].host);