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