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