* Began (but did not finish) applying GPL3+ declarations to each source file. This...
[citadel.git] / citadel / domain.c
1 /*
2  * $Id$
3  *
4  * DNS lookup for SMTP sender
5  *
6  *
7  * Copyright (c) 1987-2009 by the citadel.org team
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <netinet/in.h>
29 #include <stdio.h>
30
31 #ifdef HAVE_RESOLV_H
32 #include <arpa/nameser.h>
33 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
34 #include <arpa/nameser_compat.h>
35 #endif
36 #include <resolv.h>
37 #endif
38 #include <libcitadel.h>
39 #include "sysdep_decls.h"
40 #include "citadel.h"
41 #include "domain.h"
42 #include "server.h"
43 #include "internet_addressing.h"
44
45
46 /*
47  * get_hosts() checks the Internet configuration for various types of
48  * entries and returns them in the same format as getmx() does -- fill the
49  * buffer with a delimited list of hosts and return the number of hosts.
50  * 
51  * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
52  */
53 int get_hosts(char *mxbuf, char *rectype) {
54         int config_lines;
55         int i;
56         char buf[256];
57         char host[256], type[256];
58         int total_smarthosts = 0;
59
60         if (inetcfg == NULL) return(0);
61         strcpy(mxbuf, "");
62
63         config_lines = num_tokens(inetcfg, '\n');
64         for (i=0; i<config_lines; ++i) {
65                 extract_token(buf, inetcfg, i, '\n', sizeof buf);
66                 extract_token(host, buf, 0, '|', sizeof host);
67                 extract_token(type, buf, 1, '|', sizeof type);
68
69                 if (!strcasecmp(type, rectype)) {
70                         strcat(mxbuf, host);
71                         strcat(mxbuf, "|");
72                         ++total_smarthosts;
73                 }
74         }
75
76         return(total_smarthosts);
77 }
78
79
80 /*
81  * Compare the preference of two MX records.  First check by the actual
82  * number listed in the MX record.  If they're identical, randomize the
83  * result.
84  */
85 int mx_compare_pref(const void *mx1, const void *mx2) {
86         int pref1;
87         int pref2;
88
89         pref1 = ((const struct mx *)mx1)->pref;
90         pref2 = ((const struct mx *)mx2)->pref;
91
92         if (pref1 > pref2) {
93                 return(1);
94         }
95         else if (pref1 < pref2) {
96                 return(0);
97         }
98         else {
99                 return(rand() % 2);
100         }
101 }
102
103
104 /* 
105  * getmx()
106  *
107  * Return one or more MX's for a mail destination.
108  *
109  * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
110  * vertical bar characters, and returns the number of hosts as its return
111  * value.  If no MX's are found, it returns 0.
112  *
113  */
114 int getmx(char *mxbuf, char *dest) {
115
116 #ifdef HAVE_RESOLV_H
117         union {
118                         u_char bytes[1024];
119                         HEADER header;
120     } answer;
121 #endif
122
123         int ret;
124         unsigned char *startptr, *endptr, *ptr;
125         char expanded_buf[1024];
126         unsigned short pref, type;
127         int n = 0;
128         int qdcount;
129
130         struct mx *mxrecs = NULL;
131         int num_mxrecs = 0;
132         
133         /* If we're configured to send all mail to a smart-host, then our
134          * job here is really easy.
135          */
136         n = get_hosts(mxbuf, "smarthost");
137         if (n > 0) return(n);
138
139         /*
140          * No smart-host?  Look up the best MX for a site.
141          * Make a call to the resolver library.
142          */
143
144         ret = res_query(
145                 dest,
146                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
147
148         if (ret < 0) {
149                 mxrecs = malloc(sizeof(struct mx));
150                 mxrecs[0].pref = 0;
151                 strcpy(mxrecs[0].host, dest);
152                 num_mxrecs = 1;
153         }
154         else {
155
156                 /* If we had to truncate, shrink the number to avoid fireworks */
157                 if (ret > sizeof(answer))
158                         ret = sizeof(answer);
159         
160                 startptr = &answer.bytes[0];            /* start and end of buffer */
161                 endptr = &answer.bytes[ret];
162                 ptr = startptr + HFIXEDSZ;      /* advance past header */
163         
164                 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
165                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
166                                 CtdlLogPrintf(CTDL_DEBUG, "dn_skipname error\n");
167                                 return(0);
168                         }
169                 }
170         
171                 while(1) {
172                         memset(expanded_buf, 0, sizeof(expanded_buf));
173                         ret = dn_expand(startptr,
174                                         endptr,
175                                         ptr,
176                                         expanded_buf,
177                                         sizeof(expanded_buf)
178                                         );
179                         if (ret < 0) break;
180                         ptr += ret;
181         
182                         GETSHORT(type, ptr);
183                         ptr += INT16SZ + INT32SZ;
184                         GETSHORT(n, ptr);
185         
186                         if (type != T_MX) {
187                                 ptr += n;
188                         }
189         
190                         else {
191                                 GETSHORT(pref, ptr);
192                                 ret = dn_expand(startptr,
193                                                 endptr,
194                                                 ptr,
195                                                 expanded_buf,
196                                                 sizeof(expanded_buf)
197                                                 );
198                                 ptr += ret;
199         
200                                 ++num_mxrecs;
201                                 if (mxrecs == NULL) {
202                                         mxrecs = malloc(sizeof(struct mx));
203                                 }
204                                 else {
205                                         mxrecs = realloc(mxrecs,
206                                             (sizeof(struct mx) * num_mxrecs) );
207                                 }
208         
209                                 mxrecs[num_mxrecs - 1].pref = pref;
210                                 strcpy(mxrecs[num_mxrecs - 1].host,
211                                        expanded_buf);
212                         }
213                 }
214         }
215
216         /* Sort the MX records by preference */
217         if (num_mxrecs > 1) {
218                 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
219         }
220
221         strcpy(mxbuf, "");
222         for (n=0; n<num_mxrecs; ++n) {
223                 strcat(mxbuf, mxrecs[n].host);
224                 strcat(mxbuf, "|");
225         }
226         free(mxrecs);
227         return(num_mxrecs);
228 }