* This commit is a figment of your imagination.
[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         char buf[SIZ];
121         FILE *fp;
122         unsigned short pref, type;
123         int n = 0;
124         int qdcount;
125
126         struct mx *mxrecs = NULL;
127         int num_mxrecs = 0;
128         
129         /* If we're configured to send all mail to a smart-host, then our
130          * job here is really easy.
131          */
132         n = get_hosts(mxbuf, "smarthost");
133         if (n > 0) return(n);
134
135
136         /*
137          * No smart-host?  Look up the best MX for a site.
138          */
139
140 #ifdef __CYGWIN__
141
142         /*
143          * On systems with b0rken or non-standard resolver libraries, learn
144          * the MX records by calling "nslookup" from the command line.
145          */
146         sprintf(buf, "nslookup -query=mx %s", dest);
147         fp = popen(buf, "r");
148         if (fp == NULL) return(0);
149         while (fgets(buf, sizeof buf, fp) != NULL) {
150                 buf[strlen(buf) - 1] = 0;
151                 lprintf(9, "RESOLV: %s\n", buf);
152         }
153         pclose(fp);
154         return(0);      /* FIXME */
155
156 #else /* __CYGWIN__ */
157
158         /*
159          * Make a call to the standard resolver library.
160          */
161
162         ret = res_query(
163                 dest,
164                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
165
166         if (ret < 0) {
167                 mxrecs = mallok(sizeof(struct mx));
168                 mxrecs[0].pref = 0;
169                 strcpy(mxrecs[0].host, dest);
170                 num_mxrecs = 1;
171         }
172         else {
173
174                 /* If we had to truncate, shrink the number to avoid fireworks */
175                 if (ret > sizeof(answer))
176                         ret = sizeof(answer);
177         
178                 startptr = &answer.bytes[0];            /* start and end of buffer */
179                 endptr = &answer.bytes[ret];
180                 ptr = startptr + HFIXEDSZ;      /* advance past header */
181         
182                 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
183                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
184                                 lprintf(9, "dn_skipname error\n");
185                                 return(0);
186                         }
187                 }
188         
189                 while(1) {
190                         memset(expanded_buf, 0, sizeof(expanded_buf));
191                         ret = dn_expand(startptr,
192                                         endptr,
193                                         ptr,
194                                         expanded_buf,
195                                         sizeof(expanded_buf)
196                                         );
197                         if (ret < 0) break;
198                         ptr += ret;
199         
200                         GETSHORT(type, ptr);
201                         ptr += INT16SZ + INT32SZ;
202                         GETSHORT(n, ptr);
203         
204                         if (type != T_MX) {
205                                 ptr += n;
206                         }
207         
208                         else {
209                                 GETSHORT(pref, ptr);
210                                 ret = dn_expand(startptr,
211                                                 endptr,
212                                                 ptr,
213                                                 expanded_buf,
214                                                 sizeof(expanded_buf)
215                                                 );
216                                 ptr += ret;
217         
218                                 ++num_mxrecs;
219                                 if (mxrecs == NULL) {
220                                         mxrecs = mallok(sizeof(struct mx));
221                                 }
222                                 else {
223                                         mxrecs = reallok(mxrecs,
224                                             (sizeof(struct mx) * num_mxrecs) );
225                                 }
226         
227                                 mxrecs[num_mxrecs - 1].pref = pref;
228                                 strcpy(mxrecs[num_mxrecs - 1].host,
229                                        expanded_buf);
230                         }
231                 }
232         }
233 #endif /* __CYGWIN__ */
234
235         sort_mxrecs(mxrecs, num_mxrecs);
236
237         strcpy(mxbuf, "");
238         for (n=0; n<num_mxrecs; ++n) {
239                 strcat(mxbuf, mxrecs[n].host);
240                 strcat(mxbuf, "|");
241         }
242         phree(mxrecs);
243         return(num_mxrecs);
244 }