* Fixes
[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
12 #define SMART_HOST      "gatekeeper.wdcs.com"
13
14 /*
15  * sort_mxrecs()
16  *
17  * Sort a pile of MX records (struct mx, definted in domain.h) by preference
18  *
19  */
20 void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) {
21         int a, b;
22         struct mx hold1, hold2;
23
24         if (num_mxrecs < 2) return;
25
26         /* do the sort */
27         for (a = num_mxrecs - 2; a >= 0; --a) {
28                 for (b = 0; b <= a; ++b) {
29                         if (mxrecs[b].pref > mxrecs[b+1].pref) {
30
31                                 memcpy(&hold1, &mxrecs[b], sizeof(struct mx));
32                                 memcpy(&hold2, &mxrecs[b+1], sizeof(struct mx));
33                                 memcpy(&mxrecs[b], &hold2, sizeof(struct mx));
34                                 memcpy(&mxrecs[b+1], &hold1, sizeof(struct mx));
35                         }
36                 }
37         }
38 }
39
40
41
42 /* 
43  * getmx()
44  *
45  * Return one or more MX's for a mail destination.
46  *
47  * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
48  * vertical bar characters, and returns the number of hosts as its return
49  * value.  If no MX's are found, it returns 0.
50  *
51  */
52 int getmx(char *mxbuf, char *dest) {
53         char answer[1024];
54         int ret;
55         unsigned char *startptr, *endptr, *ptr;
56         char expanded_buf[1024];
57         unsigned short pref, type;
58         int n;
59         HEADER *hp;
60         int qdcount;
61
62         struct mx *mxrecs = NULL;
63         int num_mxrecs = 0;
64
65         /* If we're configured to send all mail to a smart-host, then our
66          * job here is really easy.
67          */
68         if (0) {        /* FIX */
69                 strcpy(mxbuf, SMART_HOST);
70                 return(1);
71         }
72
73         /* No smart-host?  Look up the best MX for a site.
74          */
75         ret = res_query(
76                 dest,
77                 C_IN, T_MX, (unsigned char *)answer, sizeof(answer)  );
78
79         if (ret < 0) {
80                 lprintf(5, "No MX found\n");
81                 return(0);
82         }
83
84         /* If we had to truncate, shrink the number to avoid fireworks */
85         if (ret > sizeof(answer))
86                 ret = sizeof(answer);
87
88         hp = (HEADER *)&answer[0];
89         startptr = &answer[0];          /* start and end of buffer */
90         endptr = &answer[ret];
91         ptr = startptr + HFIXEDSZ;      /* advance past header */
92
93         for (qdcount = ntohs(hp->qdcount); qdcount--; ptr += ret + QFIXEDSZ) {
94                 if ((ret = dn_skipname(ptr, endptr)) < 0) {
95                         lprintf(9, "dn_skipname error\n");
96                         return(0);
97                 }
98         }
99
100         while(1) {
101                 memset(expanded_buf, 0, sizeof(expanded_buf));
102                 ret = dn_expand(startptr,
103                                 endptr,
104                                 ptr,
105                                 expanded_buf,
106                                 sizeof(expanded_buf)
107                                 );
108                 if (ret < 0) break;
109                 ptr += ret;
110
111                 GETSHORT(type, ptr);
112                 ptr += INT16SZ + INT32SZ;
113                 GETSHORT(n, ptr);
114
115                 if (type != T_MX) {
116                         ptr += n;
117                 }
118
119                 else {
120                         GETSHORT(pref, ptr);
121                         ret = dn_expand(startptr,
122                                         endptr,
123                                         ptr,
124                                         expanded_buf,
125                                         sizeof(expanded_buf)
126                                         );
127                         ptr += ret;
128
129                         ++num_mxrecs;
130                         if (mxrecs == NULL) {
131                                 mxrecs = mallok(sizeof(struct mx));
132                         }
133                         else {
134                                 mxrecs = reallok(mxrecs,
135                                         (sizeof(struct mx) * num_mxrecs) );
136                         }
137
138                         mxrecs[num_mxrecs - 1].pref = pref;
139                         strcpy(mxrecs[num_mxrecs - 1].host, expanded_buf);
140                 }
141         }
142
143         sort_mxrecs(mxrecs, num_mxrecs);
144
145         strcpy(mxbuf, "");
146         for (n=0; n<num_mxrecs; ++n) {
147                 strcat(mxbuf, mxrecs[n].host);
148                 strcat(mxbuf, "|");
149         }
150         phree(mxrecs);
151         return(num_mxrecs);
152 }