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