* whobbs.c: line up the columns better
[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         /*
147          * No smart-host?  Look up the best MX for a site.
148          */
149
150 #ifndef HAVE_RESOLV_H
151
152         /*
153          * On systems with b0rken or non-standard resolver libraries, learn
154          * the MX records by calling "nslookup" from the command line.
155          */
156         sprintf(buf, "nslookup -query=mx %s", dest);
157         fp = popen(buf, "r");
158         if (fp == NULL) return(0);
159         while (fgets(buf, sizeof buf, fp) != NULL) {
160                 buf[strlen(buf) - 1] = 0;
161                 lprintf(9, "RESOLV: %s\n", buf);
162         }
163         pclose(fp);
164         return(0);      /* FIXME */
165
166 #else /* HAVE_RESOLV_H */
167
168         /*
169          * Make a call to the standard resolver library.
170          */
171
172         ret = res_query(
173                 dest,
174                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
175
176         if (ret < 0) {
177                 mxrecs = mallok(sizeof(struct mx));
178                 mxrecs[0].pref = 0;
179                 strcpy(mxrecs[0].host, dest);
180                 num_mxrecs = 1;
181         }
182         else {
183
184                 /* If we had to truncate, shrink the number to avoid fireworks */
185                 if (ret > sizeof(answer))
186                         ret = sizeof(answer);
187         
188                 startptr = &answer.bytes[0];            /* start and end of buffer */
189                 endptr = &answer.bytes[ret];
190                 ptr = startptr + HFIXEDSZ;      /* advance past header */
191         
192                 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
193                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
194                                 lprintf(9, "dn_skipname error\n");
195                                 return(0);
196                         }
197                 }
198         
199                 while(1) {
200                         memset(expanded_buf, 0, sizeof(expanded_buf));
201                         ret = dn_expand(startptr,
202                                         endptr,
203                                         ptr,
204                                         expanded_buf,
205                                         sizeof(expanded_buf)
206                                         );
207                         if (ret < 0) break;
208                         ptr += ret;
209         
210                         GETSHORT(type, ptr);
211                         ptr += INT16SZ + INT32SZ;
212                         GETSHORT(n, ptr);
213         
214                         if (type != T_MX) {
215                                 ptr += n;
216                         }
217         
218                         else {
219                                 GETSHORT(pref, ptr);
220                                 ret = dn_expand(startptr,
221                                                 endptr,
222                                                 ptr,
223                                                 expanded_buf,
224                                                 sizeof(expanded_buf)
225                                                 );
226                                 ptr += ret;
227         
228                                 ++num_mxrecs;
229                                 if (mxrecs == NULL) {
230                                         mxrecs = mallok(sizeof(struct mx));
231                                 }
232                                 else {
233                                         mxrecs = reallok(mxrecs,
234                                             (sizeof(struct mx) * num_mxrecs) );
235                                 }
236         
237                                 mxrecs[num_mxrecs - 1].pref = pref;
238                                 strcpy(mxrecs[num_mxrecs - 1].host,
239                                        expanded_buf);
240                         }
241                 }
242         }
243 #endif /* HAVE_RESOLV_H */
244
245         sort_mxrecs(mxrecs, num_mxrecs);
246
247         strcpy(mxbuf, "");
248         for (n=0; n<num_mxrecs; ++n) {
249                 strcat(mxbuf, mxrecs[n].host);
250                 strcat(mxbuf, "|");
251         }
252         phree(mxrecs);
253         return(num_mxrecs);
254 }