Did away with lprintf all together now its called CtdlLogPrintf()
[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 #endif
106
107         int ret;
108         unsigned char *startptr, *endptr, *ptr;
109         char expanded_buf[1024];
110         unsigned short pref, type;
111         int n = 0;
112         int qdcount;
113
114         struct mx *mxrecs = NULL;
115         int num_mxrecs = 0;
116         
117         /* If we're configured to send all mail to a smart-host, then our
118          * job here is really easy.
119          */
120         n = get_hosts(mxbuf, "smarthost");
121         if (n > 0) return(n);
122
123         /*
124          * No smart-host?  Look up the best MX for a site.
125          * Make a call to the resolver library.
126          */
127
128         ret = res_query(
129                 dest,
130                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
131
132         if (ret < 0) {
133                 mxrecs = malloc(sizeof(struct mx));
134                 mxrecs[0].pref = 0;
135                 strcpy(mxrecs[0].host, dest);
136                 num_mxrecs = 1;
137         }
138         else {
139
140                 /* If we had to truncate, shrink the number to avoid fireworks */
141                 if (ret > sizeof(answer))
142                         ret = sizeof(answer);
143         
144                 startptr = &answer.bytes[0];            /* start and end of buffer */
145                 endptr = &answer.bytes[ret];
146                 ptr = startptr + HFIXEDSZ;      /* advance past header */
147         
148                 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
149                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
150                                 CtdlLogPrintf(CTDL_DEBUG, "dn_skipname error\n");
151                                 return(0);
152                         }
153                 }
154         
155                 while(1) {
156                         memset(expanded_buf, 0, sizeof(expanded_buf));
157                         ret = dn_expand(startptr,
158                                         endptr,
159                                         ptr,
160                                         expanded_buf,
161                                         sizeof(expanded_buf)
162                                         );
163                         if (ret < 0) break;
164                         ptr += ret;
165         
166                         GETSHORT(type, ptr);
167                         ptr += INT16SZ + INT32SZ;
168                         GETSHORT(n, ptr);
169         
170                         if (type != T_MX) {
171                                 ptr += n;
172                         }
173         
174                         else {
175                                 GETSHORT(pref, ptr);
176                                 ret = dn_expand(startptr,
177                                                 endptr,
178                                                 ptr,
179                                                 expanded_buf,
180                                                 sizeof(expanded_buf)
181                                                 );
182                                 ptr += ret;
183         
184                                 ++num_mxrecs;
185                                 if (mxrecs == NULL) {
186                                         mxrecs = malloc(sizeof(struct mx));
187                                 }
188                                 else {
189                                         mxrecs = realloc(mxrecs,
190                                             (sizeof(struct mx) * num_mxrecs) );
191                                 }
192         
193                                 mxrecs[num_mxrecs - 1].pref = pref;
194                                 strcpy(mxrecs[num_mxrecs - 1].host,
195                                        expanded_buf);
196                         }
197                 }
198         }
199
200         /* Sort the MX records by preference */
201         if (num_mxrecs > 1) {
202                 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
203         }
204
205         strcpy(mxbuf, "");
206         for (n=0; n<num_mxrecs; ++n) {
207                 strcat(mxbuf, mxrecs[n].host);
208                 strcat(mxbuf, "|");
209         }
210         free(mxrecs);
211         return(num_mxrecs);
212 }