* SpamAssassin connector is now configurable in <.A>ide <S>ysconfig <I>nternet.
[citadel.git] / citadel / domain.c
1 /*
2  * $Id$
3  *
4  * DNS lookup for SMTP sender
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <netinet/in.h>
12 #include <stdio.h>
13 #include <arpa/nameser.h>
14 #include <resolv.h>
15 #include "sysdep_decls.h"
16 #include "citadel.h"
17 #include "domain.h"
18 #include "server.h"
19 #include "tools.h"
20 #include "internet_addressing.h"
21
22
23 /*
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.
27  * 
28  * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
29  */
30 int get_hosts(char *mxbuf, char *rectype) {
31         int config_lines;
32         int i;
33         char buf[SIZ];
34         char host[SIZ], type[SIZ];
35         int total_smarthosts = 0;
36
37         if (inetcfg == NULL) return(0);
38         strcpy(mxbuf, "");
39
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, '|');
45
46                 if (!strcasecmp(type, rectype)) {
47                         strcat(mxbuf, host);
48                         strcat(mxbuf, "|");
49                         ++total_smarthosts;
50                 }
51         }
52
53         return(total_smarthosts);
54 }
55
56
57 /*
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
60  * result.
61  */
62 inline int mx_compare_pref(int pref1, int pref2) {
63         if (pref1 > pref2) {
64                 return(1);
65         }
66         else if (pref1 < pref2) {
67                 return(0);
68         }
69         else {
70                 return(rand() % 2);
71         }
72 }
73
74
75 /*
76  * sort_mxrecs()
77  *
78  * Sort a pile of MX records (struct mx, definted in domain.h) by preference
79  *
80  */
81 void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) {
82         int a, b;
83         struct mx hold1, hold2;
84
85         if (num_mxrecs < 2) return;
86
87         /* do the sort */
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));
95                         }
96                 }
97         }
98 }
99
100
101
102 /* 
103  * getmx()
104  *
105  * Return one or more MX's for a mail destination.
106  *
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.
110  *
111  */
112 int getmx(char *mxbuf, char *dest) {
113         union {
114                         u_char bytes[1024];
115                         HEADER header;
116     } answer;
117         int ret;
118         unsigned char *startptr, *endptr, *ptr;
119         char expanded_buf[1024];
120         unsigned short pref, type;
121         int n = 0;
122         int qdcount;
123
124         struct mx *mxrecs = NULL;
125         int num_mxrecs = 0;
126         
127         /* If we're configured to send all mail to a smart-host, then our
128          * job here is really easy.
129          */
130         n = get_hosts(mxbuf, "smarthost");
131         if (n > 0) return(n);
132
133         /*
134          * No smart-host?  Look up the best MX for a site.
135          */
136         ret = res_query(
137                 dest,
138                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
139
140         if (ret < 0) {
141                 mxrecs = mallok(sizeof(struct mx));
142                 mxrecs[0].pref = 0;
143                 strcpy(mxrecs[0].host, dest);
144                 num_mxrecs = 1;
145         }
146         else {
147
148                 /* If we had to truncate, shrink the number to avoid fireworks */
149                 if (ret > sizeof(answer))
150                         ret = sizeof(answer);
151         
152                 startptr = &answer.bytes[0];            /* start and end of buffer */
153                 endptr = &answer.bytes[ret];
154                 ptr = startptr + HFIXEDSZ;      /* advance past header */
155         
156                 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
157                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
158                                 lprintf(9, "dn_skipname error\n");
159                                 return(0);
160                         }
161                 }
162         
163                 while(1) {
164                         memset(expanded_buf, 0, sizeof(expanded_buf));
165                         ret = dn_expand(startptr,
166                                         endptr,
167                                         ptr,
168                                         expanded_buf,
169                                         sizeof(expanded_buf)
170                                         );
171                         if (ret < 0) break;
172                         ptr += ret;
173         
174                         GETSHORT(type, ptr);
175                         ptr += INT16SZ + INT32SZ;
176                         GETSHORT(n, ptr);
177         
178                         if (type != T_MX) {
179                                 ptr += n;
180                         }
181         
182                         else {
183                                 GETSHORT(pref, ptr);
184                                 ret = dn_expand(startptr,
185                                                 endptr,
186                                                 ptr,
187                                                 expanded_buf,
188                                                 sizeof(expanded_buf)
189                                                 );
190                                 ptr += ret;
191         
192                                 ++num_mxrecs;
193                                 if (mxrecs == NULL) {
194                                         mxrecs = mallok(sizeof(struct mx));
195                                 }
196                                 else {
197                                         mxrecs = reallok(mxrecs,
198                                             (sizeof(struct mx) * num_mxrecs) );
199                                 }
200         
201                                 mxrecs[num_mxrecs - 1].pref = pref;
202                                 strcpy(mxrecs[num_mxrecs - 1].host,
203                                        expanded_buf);
204                         }
205                 }
206         }
207
208         sort_mxrecs(mxrecs, num_mxrecs);
209
210         strcpy(mxbuf, "");
211         for (n=0; n<num_mxrecs; ++n) {
212                 strcat(mxbuf, mxrecs[n].host);
213                 strcat(mxbuf, "|");
214         }
215         phree(mxrecs);
216         return(num_mxrecs);
217 }