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 inline int mx_compare_pref(int pref1, int pref2) {
71 else if (pref1 < pref2) {
83 * Sort a pile of MX records (struct mx, definted in domain.h) by preference
86 void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) {
88 struct mx hold1, hold2;
90 if (num_mxrecs < 2) return;
93 for (a = num_mxrecs - 2; a >= 0; --a) {
94 for (b = 0; b <= a; ++b) {
95 if (mx_compare_pref(mxrecs[b].pref,mxrecs[b+1].pref)) {
96 memcpy(&hold1, &mxrecs[b], sizeof(struct mx));
97 memcpy(&hold2, &mxrecs[b+1], sizeof(struct mx));
98 memcpy(&mxrecs[b], &hold2, sizeof(struct mx));
99 memcpy(&mxrecs[b+1], &hold1, sizeof(struct mx));
110 * Return one or more MX's for a mail destination.
112 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
113 * vertical bar characters, and returns the number of hosts as its return
114 * value. If no MX's are found, it returns 0.
117 int getmx(char *mxbuf, char *dest) {
130 unsigned char *startptr, *endptr, *ptr;
131 char expanded_buf[1024];
132 unsigned short pref, type;
136 struct mx *mxrecs = NULL;
139 /* If we're configured to send all mail to a smart-host, then our
140 * job here is really easy.
142 n = get_hosts(mxbuf, "smarthost");
143 if (n > 0) return(n);
146 * No smart-host? Look up the best MX for a site.
149 #ifndef HAVE_RESOLV_H
152 * On systems with b0rken or non-standard resolver libraries, learn
153 * the MX records by calling "nslookup" from the command line.
161 #else /* HAVE_RESOLV_H */
164 * Make a call to the standard resolver library.
169 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
172 mxrecs = mallok(sizeof(struct mx));
174 strcpy(mxrecs[0].host, dest);
179 /* If we had to truncate, shrink the number to avoid fireworks */
180 if (ret > sizeof(answer))
181 ret = sizeof(answer);
183 startptr = &answer.bytes[0]; /* start and end of buffer */
184 endptr = &answer.bytes[ret];
185 ptr = startptr + HFIXEDSZ; /* advance past header */
187 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
188 if ((ret = dn_skipname(ptr, endptr)) < 0) {
189 lprintf(9, "dn_skipname error\n");
195 memset(expanded_buf, 0, sizeof(expanded_buf));
196 ret = dn_expand(startptr,
206 ptr += INT16SZ + INT32SZ;
215 ret = dn_expand(startptr,
224 if (mxrecs == NULL) {
225 mxrecs = mallok(sizeof(struct mx));
228 mxrecs = reallok(mxrecs,
229 (sizeof(struct mx) * num_mxrecs) );
232 mxrecs[num_mxrecs - 1].pref = pref;
233 strcpy(mxrecs[num_mxrecs - 1].host,
238 #endif /* HAVE_RESOLV_H */
240 sort_mxrecs(mxrecs, num_mxrecs);
243 for (n=0; n<num_mxrecs; ++n) {
244 strcat(mxbuf, mxrecs[n].host);