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