* oops
[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_smarthosts() checks the Internet configuration for "smarthost"
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 int get_smarthosts(char *mxbuf) {
29         int config_lines;
30         int i;
31         char buf[SIZ];
32         char host[SIZ], type[SIZ];
33         int total_smarthosts = 0;
34
35         if (inetcfg == NULL) return(0);
36         strcpy(mxbuf, "");
37
38         config_lines = num_tokens(inetcfg, '\n');
39         for (i=0; i<config_lines; ++i) {
40                 extract_token(buf, inetcfg, i, '\n');
41                 extract_token(host, buf, 0, '|');
42                 extract_token(type, buf, 1, '|');
43
44                 if (!strcasecmp(type, "smarthost")) {
45                         strcat(mxbuf, host);
46                         strcat(mxbuf, "|");
47                         ++total_smarthosts;
48                 }
49         }
50
51         return(total_smarthosts);
52 }
53
54
55 /*
56  * Compare the preference of two MX records.  First check by the actual
57  * number listed in the MX record.  If they're identical, randomize the
58  * result.
59  */
60 inline int mx_compare_pref(int pref1, int pref2) {
61         if (pref1 > pref2) {
62                 return(1);
63         }
64         else if (pref1 < pref2) {
65                 return(0);
66         }
67         else {
68                 return(rand() % 2);
69         }
70 }
71
72
73 /*
74  * sort_mxrecs()
75  *
76  * Sort a pile of MX records (struct mx, definted in domain.h) by preference
77  *
78  */
79 void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) {
80         int a, b;
81         struct mx hold1, hold2;
82
83         if (num_mxrecs < 2) return;
84
85         /* do the sort */
86         for (a = num_mxrecs - 2; a >= 0; --a) {
87                 for (b = 0; b <= a; ++b) {
88                         if (mx_compare_pref(mxrecs[b].pref,mxrecs[b+1].pref)) {
89                                 memcpy(&hold1, &mxrecs[b], sizeof(struct mx));
90                                 memcpy(&hold2, &mxrecs[b+1], sizeof(struct mx));
91                                 memcpy(&mxrecs[b], &hold2, sizeof(struct mx));
92                                 memcpy(&mxrecs[b+1], &hold1, sizeof(struct mx));
93                         }
94                 }
95         }
96 }
97
98
99
100 /* 
101  * getmx()
102  *
103  * Return one or more MX's for a mail destination.
104  *
105  * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
106  * vertical bar characters, and returns the number of hosts as its return
107  * value.  If no MX's are found, it returns 0.
108  *
109  */
110 int getmx(char *mxbuf, char *dest) {
111         union {
112                         u_char bytes[1024];
113                         HEADER header;
114     } answer;
115         int ret;
116         unsigned char *startptr, *endptr, *ptr;
117         char expanded_buf[1024];
118         unsigned short pref, type;
119         int n = 0;
120         int qdcount;
121
122         struct mx *mxrecs = NULL;
123         int num_mxrecs = 0;
124         
125         /* If we're configured to send all mail to a smart-host, then our
126          * job here is really easy.
127          */
128         n = get_smarthosts(mxbuf);
129         if (n > 0) return(n);
130
131         /*
132          * No smart-host?  Look up the best MX for a site.
133          */
134         ret = res_query(
135                 dest,
136                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
137
138         if (ret < 0) {
139                 mxrecs = mallok(sizeof(struct mx));
140                 mxrecs[0].pref = 0;
141                 strcpy(mxrecs[0].host, dest);
142                 num_mxrecs = 1;
143         }
144         else {
145
146                 /* If we had to truncate, shrink the number to avoid fireworks */
147                 if (ret > sizeof(answer))
148                         ret = sizeof(answer);
149         
150                 startptr = &answer.bytes[0];            /* start and end of buffer */
151                 endptr = &answer.bytes[ret];
152                 ptr = startptr + HFIXEDSZ;      /* advance past header */
153         
154                 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
155                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
156                                 lprintf(9, "dn_skipname error\n");
157                                 return(0);
158                         }
159                 }
160         
161                 while(1) {
162                         memset(expanded_buf, 0, sizeof(expanded_buf));
163                         ret = dn_expand(startptr,
164                                         endptr,
165                                         ptr,
166                                         expanded_buf,
167                                         sizeof(expanded_buf)
168                                         );
169                         if (ret < 0) break;
170                         ptr += ret;
171         
172                         GETSHORT(type, ptr);
173                         ptr += INT16SZ + INT32SZ;
174                         GETSHORT(n, ptr);
175         
176                         if (type != T_MX) {
177                                 ptr += n;
178                         }
179         
180                         else {
181                                 GETSHORT(pref, ptr);
182                                 ret = dn_expand(startptr,
183                                                 endptr,
184                                                 ptr,
185                                                 expanded_buf,
186                                                 sizeof(expanded_buf)
187                                                 );
188                                 ptr += ret;
189         
190                                 ++num_mxrecs;
191                                 if (mxrecs == NULL) {
192                                         mxrecs = mallok(sizeof(struct mx));
193                                 }
194                                 else {
195                                         mxrecs = reallok(mxrecs,
196                                             (sizeof(struct mx) * num_mxrecs) );
197                                 }
198         
199                                 mxrecs[num_mxrecs - 1].pref = pref;
200                                 strcpy(mxrecs[num_mxrecs - 1].host,
201                                        expanded_buf);
202                         }
203                 }
204         }
205
206         sort_mxrecs(mxrecs, num_mxrecs);
207
208         strcpy(mxbuf, "");
209         for (n=0; n<num_mxrecs; ++n) {
210                 strcat(mxbuf, mxrecs[n].host);
211                 strcat(mxbuf, "|");
212         }
213         phree(mxrecs);
214         return(num_mxrecs);
215 }