- port to Cygwin (DLL support, etc.)
[citadel.git] / citadel / domain.c
1 /*
2  * $Id$
3  *
4  * DNS lookup for SMTP sender
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <netinet/in.h>
12 #include <stdio.h>
13 #include <arpa/nameser.h>
14 #include <resolv.h>
15 #include "sysdep_decls.h"
16 #include "citadel.h"
17 #include "domain.h"
18 #include "server.h"
19 #include "tools.h"
20 #include "internet_addressing.h"
21
22
23 /*
24  * get_smarthosts() checks the Internet configuration for "smarthost"
25  * entries and returns them in the same format as getmx() does -- fill the
26  * buffer with a delimited list of hosts and return the number of hosts.
27  */
28 int get_smarthosts(char *mxbuf) {
29         int config_lines;
30         int i;
31         char buf[SIZ];
32         char host[SIZ], type[SIZ];
33         int total_smarthosts = 0;
34
35         if (inetcfg == NULL) return(0);
36         strcpy(mxbuf, "");
37
38         config_lines = num_tokens(inetcfg, '\n');
39         for (i=0; i<config_lines; ++i) {
40                 extract_token(buf, inetcfg, i, '\n');
41                 extract_token(host, buf, 0, '|');
42                 extract_token(type, buf, 1, '|');
43
44                 if (!strcasecmp(type, "smarthost")) {
45                         strcat(mxbuf, host);
46                         strcat(mxbuf, "|");
47                         ++total_smarthosts;
48                 }
49         }
50
51         return(total_smarthosts);
52 }
53
54
55
56
57 /*
58  * sort_mxrecs()
59  *
60  * Sort a pile of MX records (struct mx, definted in domain.h) by preference
61  *
62  */
63 void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) {
64         int a, b;
65         struct mx hold1, hold2;
66
67         if (num_mxrecs < 2) return;
68
69         /* do the sort */
70         for (a = num_mxrecs - 2; a >= 0; --a) {
71                 for (b = 0; b <= a; ++b) {
72                         if (mxrecs[b].pref > mxrecs[b+1].pref) {
73
74                                 memcpy(&hold1, &mxrecs[b], sizeof(struct mx));
75                                 memcpy(&hold2, &mxrecs[b+1], sizeof(struct mx));
76                                 memcpy(&mxrecs[b], &hold2, sizeof(struct mx));
77                                 memcpy(&mxrecs[b+1], &hold1, sizeof(struct mx));
78                         }
79                 }
80         }
81 }
82
83
84
85 /* 
86  * getmx()
87  *
88  * Return one or more MX's for a mail destination.
89  *
90  * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
91  * vertical bar characters, and returns the number of hosts as its return
92  * value.  If no MX's are found, it returns 0.
93  *
94  */
95 int getmx(char *mxbuf, char *dest) {
96         char answer[1024];
97         int ret;
98         unsigned char *startptr, *endptr, *ptr;
99         char expanded_buf[1024];
100         unsigned short pref, type;
101         int n = 0;
102         HEADER *hp;
103         int qdcount;
104
105         struct mx *mxrecs = NULL;
106         int num_mxrecs = 0;
107         
108         /* If we're configured to send all mail to a smart-host, then our
109          * job here is really easy.
110          */
111         n = get_smarthosts(mxbuf);
112         if (n > 0) return(n);
113
114         /*
115          * No smart-host?  Look up the best MX for a site.
116          */
117         ret = res_query(
118                 dest,
119                 C_IN, T_MX, (unsigned char *)answer, sizeof(answer)  );
120
121         if (ret < 0) {
122                 mxrecs = mallok(sizeof(struct mx));
123                 mxrecs[0].pref = 0;
124                 strcpy(mxrecs[0].host, dest);
125                 num_mxrecs = 1;
126         }
127         else {
128
129                 /* If we had to truncate, shrink the number to avoid fireworks */
130                 if (ret > sizeof(answer))
131                         ret = sizeof(answer);
132         
133                 hp = (HEADER *)&answer[0];
134                 startptr = &answer[0];          /* start and end of buffer */
135                 endptr = &answer[ret];
136                 ptr = startptr + HFIXEDSZ;      /* advance past header */
137         
138                 for (qdcount = ntohs(hp->qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
139                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
140                                 lprintf(9, "dn_skipname error\n");
141                                 return(0);
142                         }
143                 }
144         
145                 while(1) {
146                         memset(expanded_buf, 0, sizeof(expanded_buf));
147                         ret = dn_expand(startptr,
148                                         endptr,
149                                         ptr,
150                                         expanded_buf,
151                                         sizeof(expanded_buf)
152                                         );
153                         if (ret < 0) break;
154                         ptr += ret;
155         
156                         GETSHORT(type, ptr);
157                         ptr += INT16SZ + INT32SZ;
158                         GETSHORT(n, ptr);
159         
160                         if (type != T_MX) {
161                                 ptr += n;
162                         }
163         
164                         else {
165                                 GETSHORT(pref, ptr);
166                                 ret = dn_expand(startptr,
167                                                 endptr,
168                                                 ptr,
169                                                 expanded_buf,
170                                                 sizeof(expanded_buf)
171                                                 );
172                                 ptr += ret;
173         
174                                 ++num_mxrecs;
175                                 if (mxrecs == NULL) {
176                                         mxrecs = mallok(sizeof(struct mx));
177                                 }
178                                 else {
179                                         mxrecs = reallok(mxrecs,
180                                             (sizeof(struct mx) * num_mxrecs) );
181                                 }
182         
183                                 mxrecs[num_mxrecs - 1].pref = pref;
184                                 strcpy(mxrecs[num_mxrecs - 1].host,
185                                        expanded_buf);
186                         }
187                 }
188         }
189
190         sort_mxrecs(mxrecs, num_mxrecs);
191
192         strcpy(mxbuf, "");
193         for (n=0; n<num_mxrecs; ++n) {
194                 strcat(mxbuf, mxrecs[n].host);
195                 strcat(mxbuf, "|");
196         }
197         phree(mxrecs);
198         return(num_mxrecs);
199 }