4 * DNS lookup for SMTP sender
11 #include <netinet/in.h>
13 #include <arpa/nameser.h>
15 #include "sysdep_decls.h"
20 #include "internet_addressing.h"
24 * get_hosts() checks the Internet configuration for various types of
25 * entries and returns them in the same format as getmx() does -- fill the
26 * buffer with a delimited list of hosts and return the number of hosts.
28 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
30 int get_hosts(char *mxbuf, char *rectype) {
34 char host[SIZ], type[SIZ];
35 int total_smarthosts = 0;
37 if (inetcfg == NULL) return(0);
40 config_lines = num_tokens(inetcfg, '\n');
41 for (i=0; i<config_lines; ++i) {
42 extract_token(buf, inetcfg, i, '\n');
43 extract_token(host, buf, 0, '|');
44 extract_token(type, buf, 1, '|');
46 if (!strcasecmp(type, rectype)) {
53 return(total_smarthosts);
58 * Compare the preference of two MX records. First check by the actual
59 * number listed in the MX record. If they're identical, randomize the
62 inline int mx_compare_pref(int pref1, int pref2) {
66 else if (pref1 < pref2) {
78 * Sort a pile of MX records (struct mx, definted in domain.h) by preference
81 void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) {
83 struct mx hold1, hold2;
85 if (num_mxrecs < 2) return;
88 for (a = num_mxrecs - 2; a >= 0; --a) {
89 for (b = 0; b <= a; ++b) {
90 if (mx_compare_pref(mxrecs[b].pref,mxrecs[b+1].pref)) {
91 memcpy(&hold1, &mxrecs[b], sizeof(struct mx));
92 memcpy(&hold2, &mxrecs[b+1], sizeof(struct mx));
93 memcpy(&mxrecs[b], &hold2, sizeof(struct mx));
94 memcpy(&mxrecs[b+1], &hold1, sizeof(struct mx));
105 * Return one or more MX's for a mail destination.
107 * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
108 * vertical bar characters, and returns the number of hosts as its return
109 * value. If no MX's are found, it returns 0.
112 int getmx(char *mxbuf, char *dest) {
118 unsigned char *startptr, *endptr, *ptr;
119 char expanded_buf[1024];
122 unsigned short pref, type;
126 struct mx *mxrecs = NULL;
129 /* If we're configured to send all mail to a smart-host, then our
130 * job here is really easy.
132 n = get_hosts(mxbuf, "smarthost");
133 if (n > 0) return(n);
137 * No smart-host? Look up the best MX for a site.
143 * On systems with b0rken or non-standard resolver libraries, learn
144 * the MX records by calling "nslookup" from the command line.
146 sprintf(buf, "nslookup -query=mx %s", dest);
147 fp = popen(buf, "r");
148 if (fp == NULL) return(0);
149 while (fgets(buf, sizeof buf, fp) != NULL) {
150 buf[strlen(buf) - 1] = 0;
151 lprintf(9, "RESOLV: %s\n", buf);
154 return(0); /* FIXME */
156 #else /* __CYGWIN__ */
159 * Make a call to the standard resolver library.
164 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
167 mxrecs = mallok(sizeof(struct mx));
169 strcpy(mxrecs[0].host, dest);
174 /* If we had to truncate, shrink the number to avoid fireworks */
175 if (ret > sizeof(answer))
176 ret = sizeof(answer);
178 startptr = &answer.bytes[0]; /* start and end of buffer */
179 endptr = &answer.bytes[ret];
180 ptr = startptr + HFIXEDSZ; /* advance past header */
182 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
183 if ((ret = dn_skipname(ptr, endptr)) < 0) {
184 lprintf(9, "dn_skipname error\n");
190 memset(expanded_buf, 0, sizeof(expanded_buf));
191 ret = dn_expand(startptr,
201 ptr += INT16SZ + INT32SZ;
210 ret = dn_expand(startptr,
219 if (mxrecs == NULL) {
220 mxrecs = mallok(sizeof(struct mx));
223 mxrecs = reallok(mxrecs,
224 (sizeof(struct mx) * num_mxrecs) );
227 mxrecs[num_mxrecs - 1].pref = pref;
228 strcpy(mxrecs[num_mxrecs - 1].host,
233 #endif /* __CYGWIN__ */
235 sort_mxrecs(mxrecs, num_mxrecs);
238 for (n=0; n<num_mxrecs; ++n) {
239 strcat(mxbuf, mxrecs[n].host);