warning fixes and cleanups for 64-bit machines
[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         union {
97                         u_char bytes[1024];
98                         HEADER header;
99     } answer;
100         int ret;
101         unsigned char *startptr, *endptr, *ptr;
102         char expanded_buf[1024];
103         unsigned short pref, type;
104         int n = 0;
105         int qdcount;
106
107         struct mx *mxrecs = NULL;
108         int num_mxrecs = 0;
109         
110         /* If we're configured to send all mail to a smart-host, then our
111          * job here is really easy.
112          */
113         n = get_smarthosts(mxbuf);
114         if (n > 0) return(n);
115
116         /*
117          * No smart-host?  Look up the best MX for a site.
118          */
119         ret = res_query(
120                 dest,
121                 C_IN, T_MX, (unsigned char *)answer.bytes, sizeof(answer)  );
122
123         if (ret < 0) {
124                 mxrecs = mallok(sizeof(struct mx));
125                 mxrecs[0].pref = 0;
126                 strcpy(mxrecs[0].host, dest);
127                 num_mxrecs = 1;
128         }
129         else {
130
131                 /* If we had to truncate, shrink the number to avoid fireworks */
132                 if (ret > sizeof(answer))
133                         ret = sizeof(answer);
134         
135                 startptr = &answer.bytes[0];            /* start and end of buffer */
136                 endptr = &answer.bytes[ret];
137                 ptr = startptr + HFIXEDSZ;      /* advance past header */
138         
139                 for (qdcount = ntohs(answer.header.qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
140                         if ((ret = dn_skipname(ptr, endptr)) < 0) {
141                                 lprintf(9, "dn_skipname error\n");
142                                 return(0);
143                         }
144                 }
145         
146                 while(1) {
147                         memset(expanded_buf, 0, sizeof(expanded_buf));
148                         ret = dn_expand(startptr,
149                                         endptr,
150                                         ptr,
151                                         expanded_buf,
152                                         sizeof(expanded_buf)
153                                         );
154                         if (ret < 0) break;
155                         ptr += ret;
156         
157                         GETSHORT(type, ptr);
158                         ptr += INT16SZ + INT32SZ;
159                         GETSHORT(n, ptr);
160         
161                         if (type != T_MX) {
162                                 ptr += n;
163                         }
164         
165                         else {
166                                 GETSHORT(pref, ptr);
167                                 ret = dn_expand(startptr,
168                                                 endptr,
169                                                 ptr,
170                                                 expanded_buf,
171                                                 sizeof(expanded_buf)
172                                                 );
173                                 ptr += ret;
174         
175                                 ++num_mxrecs;
176                                 if (mxrecs == NULL) {
177                                         mxrecs = mallok(sizeof(struct mx));
178                                 }
179                                 else {
180                                         mxrecs = reallok(mxrecs,
181                                             (sizeof(struct mx) * num_mxrecs) );
182                                 }
183         
184                                 mxrecs[num_mxrecs - 1].pref = pref;
185                                 strcpy(mxrecs[num_mxrecs - 1].host,
186                                        expanded_buf);
187                         }
188                 }
189         }
190
191         sort_mxrecs(mxrecs, num_mxrecs);
192
193         strcpy(mxbuf, "");
194         for (n=0; n<num_mxrecs; ++n) {
195                 strcat(mxbuf, mxrecs[n].host);
196                 strcat(mxbuf, "|");
197         }
198         phree(mxrecs);
199         return(num_mxrecs);
200 }