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