* Changed the comments at the beginning of each file to a consistent format
[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[256];
31         char host[256], type[256];
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                 lprintf(5, "No MX found\n");
122                 return(0);
123         }
124
125         /* If we had to truncate, shrink the number to avoid fireworks */
126         if (ret > sizeof(answer))
127                 ret = sizeof(answer);
128
129         hp = (HEADER *)&answer[0];
130         startptr = &answer[0];          /* start and end of buffer */
131         endptr = &answer[ret];
132         ptr = startptr + HFIXEDSZ;      /* advance past header */
133
134         for (qdcount = ntohs(hp->qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
135                 if ((ret = dn_skipname(ptr, endptr)) < 0) {
136                         lprintf(9, "dn_skipname error\n");
137                         return(0);
138                 }
139         }
140
141         while(1) {
142                 memset(expanded_buf, 0, sizeof(expanded_buf));
143                 ret = dn_expand(startptr,
144                                 endptr,
145                                 ptr,
146                                 expanded_buf,
147                                 sizeof(expanded_buf)
148                                 );
149                 if (ret < 0) break;
150                 ptr += ret;
151
152                 GETSHORT(type, ptr);
153                 ptr += INT16SZ + INT32SZ;
154                 GETSHORT(n, ptr);
155
156                 if (type != T_MX) {
157                         ptr += n;
158                 }
159
160                 else {
161                         GETSHORT(pref, ptr);
162                         ret = dn_expand(startptr,
163                                         endptr,
164                                         ptr,
165                                         expanded_buf,
166                                         sizeof(expanded_buf)
167                                         );
168                         ptr += ret;
169
170                         ++num_mxrecs;
171                         if (mxrecs == NULL) {
172                                 mxrecs = mallok(sizeof(struct mx));
173                         }
174                         else {
175                                 mxrecs = reallok(mxrecs,
176                                         (sizeof(struct mx) * num_mxrecs) );
177                         }
178
179                         mxrecs[num_mxrecs - 1].pref = pref;
180                         strcpy(mxrecs[num_mxrecs - 1].host, expanded_buf);
181                 }
182         }
183
184         sort_mxrecs(mxrecs, num_mxrecs);
185
186         strcpy(mxbuf, "");
187         for (n=0; n<num_mxrecs; ++n) {
188                 strcat(mxbuf, mxrecs[n].host);
189                 strcat(mxbuf, "|");
190         }
191         phree(mxrecs);
192         return(num_mxrecs);
193 }