* Added support for any standard RBL
[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 inline int mx_compare_pref(int pref1, int pref2) {
68         if (pref1 > pref2) {
69                 return(1);
70         }
71         else if (pref1 < pref2) {
72                 return(0);
73         }
74         else {
75                 return(rand() % 2);
76         }
77 }
78
79
80 /*
81  * sort_mxrecs()
82  *
83  * Sort a pile of MX records (struct mx, definted in domain.h) by preference
84  *
85  */
86 void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) {
87         int a, b;
88         struct mx hold1, hold2;
89
90         if (num_mxrecs < 2) return;
91
92         /* do the sort */
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));
100                         }
101                 }
102         }
103 }
104
105
106
107 /* 
108  * getmx()
109  *
110  * Return one or more MX's for a mail destination.
111  *
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.
115  *
116  */
117 int getmx(char *mxbuf, char *dest) {
118
119 #ifdef HAVE_RESOLV_H
120         union {
121                         u_char bytes[1024];
122                         HEADER header;
123     } answer;
124 #else
125         char buf[SIZ];
126         FILE *fp;
127 #endif
128
129         int ret;
130         unsigned char *startptr, *endptr, *ptr;
131         char expanded_buf[1024];
132         unsigned short pref, type;
133         int n = 0;
134         int qdcount;
135
136         struct mx *mxrecs = NULL;
137         int num_mxrecs = 0;
138         
139         /* If we're configured to send all mail to a smart-host, then our
140          * job here is really easy.
141          */
142         n = get_hosts(mxbuf, "smarthost");
143         if (n > 0) return(n);
144
145         /*
146          * No smart-host?  Look up the best MX for a site.
147          */
148
149 #ifndef HAVE_RESOLV_H
150
151         /*
152          * On systems with b0rken or non-standard resolver libraries, learn
153          * the MX records by calling "nslookup" from the command line.
154          *
155          * Someday.
156          *
157          */
158
159         return(0);
160
161 #else /* HAVE_RESOLV_H */
162
163         /*
164          * Make a call to the standard resolver library.
165          */
166
167         ret = res_query(
168                 dest,
169                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
170
171         if (ret < 0) {
172                 mxrecs = mallok(sizeof(struct mx));
173                 mxrecs[0].pref = 0;
174                 strcpy(mxrecs[0].host, dest);
175                 num_mxrecs = 1;
176         }
177         else {
178
179                 /* If we had to truncate, shrink the number to avoid fireworks */
180                 if (ret > sizeof(answer))
181                         ret = sizeof(answer);
182         
183                 startptr = &answer.bytes[0];            /* start and end of buffer */
184                 endptr = &answer.bytes[ret];
185                 ptr = startptr + HFIXEDSZ;      /* advance past header */
186         
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");
190                                 return(0);
191                         }
192                 }
193         
194                 while(1) {
195                         memset(expanded_buf, 0, sizeof(expanded_buf));
196                         ret = dn_expand(startptr,
197                                         endptr,
198                                         ptr,
199                                         expanded_buf,
200                                         sizeof(expanded_buf)
201                                         );
202                         if (ret < 0) break;
203                         ptr += ret;
204         
205                         GETSHORT(type, ptr);
206                         ptr += INT16SZ + INT32SZ;
207                         GETSHORT(n, ptr);
208         
209                         if (type != T_MX) {
210                                 ptr += n;
211                         }
212         
213                         else {
214                                 GETSHORT(pref, ptr);
215                                 ret = dn_expand(startptr,
216                                                 endptr,
217                                                 ptr,
218                                                 expanded_buf,
219                                                 sizeof(expanded_buf)
220                                                 );
221                                 ptr += ret;
222         
223                                 ++num_mxrecs;
224                                 if (mxrecs == NULL) {
225                                         mxrecs = mallok(sizeof(struct mx));
226                                 }
227                                 else {
228                                         mxrecs = reallok(mxrecs,
229                                             (sizeof(struct mx) * num_mxrecs) );
230                                 }
231         
232                                 mxrecs[num_mxrecs - 1].pref = pref;
233                                 strcpy(mxrecs[num_mxrecs - 1].host,
234                                        expanded_buf);
235                         }
236                 }
237         }
238 #endif /* HAVE_RESOLV_H */
239
240         sort_mxrecs(mxrecs, num_mxrecs);
241
242         strcpy(mxbuf, "");
243         for (n=0; n<num_mxrecs; ++n) {
244                 strcat(mxbuf, mxrecs[n].host);
245                 strcat(mxbuf, "|");
246         }
247         phree(mxrecs);
248         return(num_mxrecs);
249 }