d7bd402de868f2352e71b8ba91331c43c868da91
[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 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
18 #include <arpa/nameser_compat.h>
19 #endif
20 #include <resolv.h>
21 #endif
22
23 #include "sysdep_decls.h"
24 #include "citadel.h"
25 #include "domain.h"
26 #include "server.h"
27 #include "tools.h"
28 #include "internet_addressing.h"
29
30
31 /*
32  * get_hosts() checks the Internet configuration for various types of
33  * entries and returns them in the same format as getmx() does -- fill the
34  * buffer with a delimited list of hosts and return the number of hosts.
35  * 
36  * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
37  */
38 int get_hosts(char *mxbuf, char *rectype) {
39         int config_lines;
40         int i;
41         char buf[256];
42         char host[256], type[256];
43         int total_smarthosts = 0;
44
45         if (inetcfg == NULL) return(0);
46         strcpy(mxbuf, "");
47
48         config_lines = num_tokens(inetcfg, '\n');
49         for (i=0; i<config_lines; ++i) {
50                 extract_token(buf, inetcfg, i, '\n', sizeof buf);
51                 extract_token(host, buf, 0, '|', sizeof host);
52                 extract_token(type, buf, 1, '|', sizeof type);
53
54                 if (!strcasecmp(type, rectype)) {
55                         strcat(mxbuf, host);
56                         strcat(mxbuf, "|");
57                         ++total_smarthosts;
58                 }
59         }
60
61         return(total_smarthosts);
62 }
63
64
65 /*
66  * Compare the preference of two MX records.  First check by the actual
67  * number listed in the MX record.  If they're identical, randomize the
68  * result.
69  */
70 int mx_compare_pref(const void *mx1, const void *mx2) {
71         int pref1;
72         int pref2;
73
74         pref1 = ((const struct mx *)mx1)->pref;
75         pref2 = ((const struct mx *)mx2)->pref;
76
77         if (pref1 > pref2) {
78                 return(1);
79         }
80         else if (pref1 < pref2) {
81                 return(0);
82         }
83         else {
84                 return(rand() % 2);
85         }
86 }
87
88
89 /* 
90  * getmx()
91  *
92  * Return one or more MX's for a mail destination.
93  *
94  * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
95  * vertical bar characters, and returns the number of hosts as its return
96  * value.  If no MX's are found, it returns 0.
97  *
98  */
99 int getmx(char *mxbuf, char *dest) {
100
101 #ifdef HAVE_RESOLV_H
102         union {
103                         u_char bytes[1024];
104                         HEADER header;
105     } answer;
106 #else
107         char buf[SIZ];
108         FILE *fp;
109 #endif
110
111         int ret;
112         unsigned char *startptr, *endptr, *ptr;
113         char expanded_buf[1024];
114         unsigned short pref, type;
115         int n = 0;
116         int qdcount;
117
118         struct mx *mxrecs = NULL;
119         int num_mxrecs = 0;
120         
121         /* If we're configured to send all mail to a smart-host, then our
122          * job here is really easy.
123          */
124         n = get_hosts(mxbuf, "smarthost");
125         if (n > 0) return(n);
126
127         /*
128          * No smart-host?  Look up the best MX for a site.
129          */
130
131 #ifndef HAVE_RESOLV_H
132
133         /*
134          * On systems with b0rken or non-standard resolver libraries, learn
135          * the MX records by calling "nslookup" from the command line.
136          *
137          * Someday.
138          *
139          */
140
141         return(0);
142
143 #else /* HAVE_RESOLV_H */
144
145         /*
146          * Make a call to the standard resolver library.
147          */
148
149         ret = res_query(
150                 dest,
151                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
152
153         if (ret < 0) {
154                 mxrecs = malloc(sizeof(struct mx));
155                 mxrecs[0].pref = 0;
156                 strcpy(mxrecs[0].host, dest);
157                 num_mxrecs = 1;
158         }
159         else {
160
161                 /* If we had to truncate, shrink the number to avoid fireworks */
162                 if (ret > sizeof(answer))
163                         ret = sizeof(answer);
164         
165                 startptr = &answer.bytes[0];            /* start and end of buffer */
166                 endptr = &answer.bytes[ret];
167                 ptr = startptr + HFIXEDSZ;      /* advance past header */
168         
169                 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
170                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
171                                 lprintf(CTDL_DEBUG, "dn_skipname error\n");
172                                 return(0);
173                         }
174                 }
175         
176                 while(1) {
177                         memset(expanded_buf, 0, sizeof(expanded_buf));
178                         ret = dn_expand(startptr,
179                                         endptr,
180                                         ptr,
181                                         expanded_buf,
182                                         sizeof(expanded_buf)
183                                         );
184                         if (ret < 0) break;
185                         ptr += ret;
186         
187                         GETSHORT(type, ptr);
188                         ptr += INT16SZ + INT32SZ;
189                         GETSHORT(n, ptr);
190         
191                         if (type != T_MX) {
192                                 ptr += n;
193                         }
194         
195                         else {
196                                 GETSHORT(pref, ptr);
197                                 ret = dn_expand(startptr,
198                                                 endptr,
199                                                 ptr,
200                                                 expanded_buf,
201                                                 sizeof(expanded_buf)
202                                                 );
203                                 ptr += ret;
204         
205                                 ++num_mxrecs;
206                                 if (mxrecs == NULL) {
207                                         mxrecs = malloc(sizeof(struct mx));
208                                 }
209                                 else {
210                                         mxrecs = realloc(mxrecs,
211                                             (sizeof(struct mx) * num_mxrecs) );
212                                 }
213         
214                                 mxrecs[num_mxrecs - 1].pref = pref;
215                                 strcpy(mxrecs[num_mxrecs - 1].host,
216                                        expanded_buf);
217                         }
218                 }
219         }
220 #endif /* HAVE_RESOLV_H */
221
222         /* Sort the MX records by preference */
223         if (num_mxrecs > 1) {
224                 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
225         }
226
227         strcpy(mxbuf, "");
228         for (n=0; n<num_mxrecs; ++n) {
229                 strcat(mxbuf, mxrecs[n].host);
230                 strcat(mxbuf, "|");
231         }
232         free(mxrecs);
233         return(num_mxrecs);
234 }