4 * DNS lookup for SMTP sender
11 #include <netinet/in.h>
15 #include <arpa/nameser.h>
19 #include "sysdep_decls.h"
24 #include "internet_addressing.h"
28 * get_hosts() checks the Internet configuration for various types of
29 * entries and returns them in the same format as getmx() does -- fill the
30 * buffer with a delimited list of hosts and return the number of hosts.
32 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
34 int get_hosts(char *mxbuf, char *rectype) {
38 char host[SIZ], type[SIZ];
39 int total_smarthosts = 0;
41 if (inetcfg == NULL) return(0);
44 config_lines = num_tokens(inetcfg, '\n');
45 for (i=0; i<config_lines; ++i) {
46 extract_token(buf, inetcfg, i, '\n');
47 extract_token(host, buf, 0, '|');
48 extract_token(type, buf, 1, '|');
50 if (!strcasecmp(type, rectype)) {
57 return(total_smarthosts);
62 * Compare the preference of two MX records. First check by the actual
63 * number listed in the MX record. If they're identical, randomize the
66 inline int mx_compare_pref(int pref1, int pref2) {
70 else if (pref1 < pref2) {
82 * Sort a pile of MX records (struct mx, definted in domain.h) by preference
85 void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) {
87 struct mx hold1, hold2;
89 if (num_mxrecs < 2) return;
92 for (a = num_mxrecs - 2; a >= 0; --a) {
93 for (b = 0; b <= a; ++b) {
94 if (mx_compare_pref(mxrecs[b].pref,mxrecs[b+1].pref)) {
95 memcpy(&hold1, &mxrecs[b], sizeof(struct mx));
96 memcpy(&hold2, &mxrecs[b+1], sizeof(struct mx));
97 memcpy(&mxrecs[b], &hold2, sizeof(struct mx));
98 memcpy(&mxrecs[b+1], &hold1, sizeof(struct mx));
109 * Return one or more MX's for a mail destination.
111 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
112 * vertical bar characters, and returns the number of hosts as its return
113 * value. If no MX's are found, it returns 0.
116 int getmx(char *mxbuf, char *dest) {
126 unsigned char *startptr, *endptr, *ptr;
127 char expanded_buf[1024];
130 unsigned short pref, type;
134 struct mx *mxrecs = NULL;
137 /* If we're configured to send all mail to a smart-host, then our
138 * job here is really easy.
140 n = get_hosts(mxbuf, "smarthost");
141 if (n > 0) return(n);
145 * No smart-host? Look up the best MX for a site.
148 #ifndef HAVE_RESOLV_H
151 * On systems with b0rken or non-standard resolver libraries, learn
152 * the MX records by calling "nslookup" from the command line.
154 sprintf(buf, "nslookup -query=mx %s", dest);
155 fp = popen(buf, "r");
156 if (fp == NULL) return(0);
157 while (fgets(buf, sizeof buf, fp) != NULL) {
158 buf[strlen(buf) - 1] = 0;
159 lprintf(9, "RESOLV: %s\n", buf);
162 return(0); /* FIXME */
164 #else /* HAVE_RESOLV_H */
167 * Make a call to the standard resolver library.
172 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
175 mxrecs = mallok(sizeof(struct mx));
177 strcpy(mxrecs[0].host, dest);
182 /* If we had to truncate, shrink the number to avoid fireworks */
183 if (ret > sizeof(answer))
184 ret = sizeof(answer);
186 startptr = &answer.bytes[0]; /* start and end of buffer */
187 endptr = &answer.bytes[ret];
188 ptr = startptr + HFIXEDSZ; /* advance past header */
190 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
191 if ((ret = dn_skipname(ptr, endptr)) < 0) {
192 lprintf(9, "dn_skipname error\n");
198 memset(expanded_buf, 0, sizeof(expanded_buf));
199 ret = dn_expand(startptr,
209 ptr += INT16SZ + INT32SZ;
218 ret = dn_expand(startptr,
227 if (mxrecs == NULL) {
228 mxrecs = mallok(sizeof(struct mx));
231 mxrecs = reallok(mxrecs,
232 (sizeof(struct mx) * num_mxrecs) );
235 mxrecs[num_mxrecs - 1].pref = pref;
236 strcpy(mxrecs[num_mxrecs - 1].host,
241 #endif /* HAVE_RESOLV_H */
243 sort_mxrecs(mxrecs, num_mxrecs);
246 for (n=0; n<num_mxrecs; ++n) {
247 strcat(mxbuf, mxrecs[n].host);