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