* domain.c: use qsort() to sort MX records by preference. Why have a
[citadel.git] / citadel / domain.c
1 /*
2  * $Id$
3  *
4  * DNS lookup for SMTP sender
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <netinet/in.h>
13 #include <stdio.h>
14
15 #ifdef HAVE_RESOLV_H
16 #include <arpa/nameser.h>
17 #include <resolv.h>
18 #endif
19
20 #include "sysdep_decls.h"
21 #include "citadel.h"
22 #include "domain.h"
23 #include "server.h"
24 #include "tools.h"
25 #include "internet_addressing.h"
26
27
28 /*
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.
32  * 
33  * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
34  */
35 int get_hosts(char *mxbuf, char *rectype) {
36         int config_lines;
37         int i;
38         char buf[SIZ];
39         char host[SIZ], type[SIZ];
40         int total_smarthosts = 0;
41
42         if (inetcfg == NULL) return(0);
43         strcpy(mxbuf, "");
44
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, '|');
50
51                 if (!strcasecmp(type, rectype)) {
52                         strcat(mxbuf, host);
53                         strcat(mxbuf, "|");
54                         ++total_smarthosts;
55                 }
56         }
57
58         return(total_smarthosts);
59 }
60
61
62 /*
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
65  * result.
66  */
67 int mx_compare_pref(const void *mx1, const void *mx2) {
68         int pref1;
69         int pref2;
70
71         pref1 = ((const struct mx *)mx1)->pref;
72         pref2 = ((const struct mx *)mx2)->pref;
73
74         if (pref1 > pref2) {
75                 return(1);
76         }
77         else if (pref1 < pref2) {
78                 return(0);
79         }
80         else {
81                 return(rand() % 2);
82         }
83 }
84
85
86 /* 
87  * getmx()
88  *
89  * Return one or more MX's for a mail destination.
90  *
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.
94  *
95  */
96 int getmx(char *mxbuf, char *dest) {
97
98 #ifdef HAVE_RESOLV_H
99         union {
100                         u_char bytes[1024];
101                         HEADER header;
102     } answer;
103 #else
104         char buf[SIZ];
105         FILE *fp;
106 #endif
107
108         int ret;
109         unsigned char *startptr, *endptr, *ptr;
110         char expanded_buf[1024];
111         unsigned short pref, type;
112         int n = 0;
113         int qdcount;
114
115         struct mx *mxrecs = NULL;
116         int num_mxrecs = 0;
117         
118         /* If we're configured to send all mail to a smart-host, then our
119          * job here is really easy.
120          */
121         n = get_hosts(mxbuf, "smarthost");
122         if (n > 0) return(n);
123
124         /*
125          * No smart-host?  Look up the best MX for a site.
126          */
127
128 #ifndef HAVE_RESOLV_H
129
130         /*
131          * On systems with b0rken or non-standard resolver libraries, learn
132          * the MX records by calling "nslookup" from the command line.
133          *
134          * Someday.
135          *
136          */
137
138         return(0);
139
140 #else /* HAVE_RESOLV_H */
141
142         /*
143          * Make a call to the standard resolver library.
144          */
145
146         ret = res_query(
147                 dest,
148                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
149
150         if (ret < 0) {
151                 mxrecs = mallok(sizeof(struct mx));
152                 mxrecs[0].pref = 0;
153                 strcpy(mxrecs[0].host, dest);
154                 num_mxrecs = 1;
155         }
156         else {
157
158                 /* If we had to truncate, shrink the number to avoid fireworks */
159                 if (ret > sizeof(answer))
160                         ret = sizeof(answer);
161         
162                 startptr = &answer.bytes[0];            /* start and end of buffer */
163                 endptr = &answer.bytes[ret];
164                 ptr = startptr + HFIXEDSZ;      /* advance past header */
165         
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");
169                                 return(0);
170                         }
171                 }
172         
173                 while(1) {
174                         memset(expanded_buf, 0, sizeof(expanded_buf));
175                         ret = dn_expand(startptr,
176                                         endptr,
177                                         ptr,
178                                         expanded_buf,
179                                         sizeof(expanded_buf)
180                                         );
181                         if (ret < 0) break;
182                         ptr += ret;
183         
184                         GETSHORT(type, ptr);
185                         ptr += INT16SZ + INT32SZ;
186                         GETSHORT(n, ptr);
187         
188                         if (type != T_MX) {
189                                 ptr += n;
190                         }
191         
192                         else {
193                                 GETSHORT(pref, ptr);
194                                 ret = dn_expand(startptr,
195                                                 endptr,
196                                                 ptr,
197                                                 expanded_buf,
198                                                 sizeof(expanded_buf)
199                                                 );
200                                 ptr += ret;
201         
202                                 ++num_mxrecs;
203                                 if (mxrecs == NULL) {
204                                         mxrecs = mallok(sizeof(struct mx));
205                                 }
206                                 else {
207                                         mxrecs = reallok(mxrecs,
208                                             (sizeof(struct mx) * num_mxrecs) );
209                                 }
210         
211                                 mxrecs[num_mxrecs - 1].pref = pref;
212                                 strcpy(mxrecs[num_mxrecs - 1].host,
213                                        expanded_buf);
214                         }
215                 }
216         }
217 #endif /* HAVE_RESOLV_H */
218
219         /* Sort the MX records by preference */
220         if (num_mxrecs > 1) {
221                 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
222         }
223
224         strcpy(mxbuf, "");
225         for (n=0; n<num_mxrecs; ++n) {
226                 strcat(mxbuf, mxrecs[n].host);
227                 strcat(mxbuf, "|");
228         }
229         phree(mxrecs);
230         return(num_mxrecs);
231 }