* Removed some temporary diagnostics that had been left in serv_sieve.c
[citadel.git] / citadel / domain.c
blobac9d24c2a1e85fb537e6dd9a1d847161d8716345
1 /*
2 * $Id$
4 * DNS lookup for SMTP sender
6 */
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>
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"
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.
35 * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
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;
44 if (inetcfg == NULL) return(0);
45 strcpy(mxbuf, "");
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);
53 if (!strcasecmp(type, rectype)) {
54 strcat(mxbuf, host);
55 strcat(mxbuf, "|");
56 ++total_smarthosts;
60 return(total_smarthosts);
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.
69 int mx_compare_pref(const void *mx1, const void *mx2) {
70 int pref1;
71 int pref2;
73 pref1 = ((const struct mx *)mx1)->pref;
74 pref2 = ((const struct mx *)mx2)->pref;
76 if (pref1 > pref2) {
77 return(1);
79 else if (pref1 < pref2) {
80 return(0);
82 else {
83 return(rand() % 2);
88 /*
89 * getmx()
91 * Return one or more MX's for a mail destination.
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.
98 int getmx(char *mxbuf, char *dest) {
100 #ifdef HAVE_RESOLV_H
101 union {
102 u_char bytes[1024];
103 HEADER header;
104 } answer;
105 #endif
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;
114 struct mx *mxrecs = NULL;
115 int num_mxrecs = 0;
117 /* If we're configured to send all mail to a smart-host, then our
118 * job here is really easy.
120 n = get_hosts(mxbuf, "smarthost");
121 if (n > 0) return(n);
124 * No smart-host? Look up the best MX for a site.
125 * Make a call to the resolver library.
128 ret = res_query(
129 dest,
130 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer) );
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;
138 else {
140 /* If we had to truncate, shrink the number to avoid fireworks */
141 if (ret > sizeof(answer))
142 ret = sizeof(answer);
144 startptr = &answer.bytes[0]; /* start and end of buffer */
145 endptr = &answer.bytes[ret];
146 ptr = startptr + HFIXEDSZ; /* advance past header */
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);
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)
163 if (ret < 0) break;
164 ptr += ret;
166 GETSHORT(type, ptr);
167 ptr += INT16SZ + INT32SZ;
168 GETSHORT(n, ptr);
170 if (type != T_MX) {
171 ptr += n;
174 else {
175 GETSHORT(pref, ptr);
176 ret = dn_expand(startptr,
177 endptr,
178 ptr,
179 expanded_buf,
180 sizeof(expanded_buf)
182 ptr += ret;
184 ++num_mxrecs;
185 if (mxrecs == NULL) {
186 mxrecs = malloc(sizeof(struct mx));
188 else {
189 mxrecs = realloc(mxrecs,
190 (sizeof(struct mx) * num_mxrecs) );
193 mxrecs[num_mxrecs - 1].pref = pref;
194 strcpy(mxrecs[num_mxrecs - 1].host,
195 expanded_buf);
200 /* Sort the MX records by preference */
201 if (num_mxrecs > 1) {
202 qsort(mxrecs, num_mxrecs, sizeof(struct mx), mx_compare_pref);
205 strcpy(mxbuf, "");
206 for (n=0; n<num_mxrecs; ++n) {
207 strcat(mxbuf, mxrecs[n].host);
208 strcat(mxbuf, "|");
210 free(mxrecs);
211 return(num_mxrecs);