rev |
line source |
meillo@0
|
1 /* MasqMail Copyright (C) Oliver Kurth,
|
meillo@0
|
2 *
|
meillo@0
|
3 * This program is free software; you can redistribute it and/or modify
|
meillo@0
|
4 * it under the terms of the GNU General Public License as published by
|
meillo@0
|
5 * the Free Software Foundation; either version 2 of the License, or
|
meillo@0
|
6 * (at your option) any later version.
|
meillo@10
|
7 *
|
meillo@0
|
8 * This program is distributed in the hope that it will be useful,
|
meillo@0
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
meillo@0
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
meillo@0
|
11 * GNU General Public License for more details.
|
meillo@0
|
12 *
|
meillo@0
|
13 * You should have received a copy of the GNU General Public License
|
meillo@0
|
14 * along with this program; if not, write to the Free Software
|
meillo@0
|
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
meillo@0
|
16 */
|
meillo@0
|
17
|
meillo@0
|
18 #include <sys/types.h>
|
meillo@0
|
19 #include <netinet/in.h>
|
meillo@0
|
20 #include <arpa/nameser.h>
|
meillo@0
|
21 #include <resolv.h>
|
meillo@0
|
22
|
meillo@0
|
23 #include "masqmail.h"
|
meillo@0
|
24
|
meillo@0
|
25 #ifdef RESOLV_TEST
|
meillo@0
|
26
|
meillo@0
|
27 #undef DEBUG
|
meillo@0
|
28 #define DEBUG(x) if(x > 0)
|
meillo@0
|
29
|
meillo@0
|
30 #define debugf g_print
|
meillo@0
|
31 #endif
|
meillo@0
|
32
|
meillo@0
|
33 #ifdef ENABLE_RESOLVER
|
meillo@0
|
34
|
meillo@0
|
35 static union {
|
meillo@10
|
36 HEADER hdr;
|
meillo@10
|
37 unsigned char buf[PACKETSZ];
|
meillo@0
|
38 } response;
|
meillo@0
|
39 static unsigned char *resp_end;
|
meillo@0
|
40 static unsigned char *resp_pos;
|
meillo@0
|
41
|
meillo@0
|
42 static int num_answers;
|
meillo@0
|
43 static char name[MAX_DNSNAME];
|
meillo@0
|
44
|
meillo@0
|
45 unsigned short rr_type;
|
meillo@0
|
46 unsigned short rr_dlen;
|
meillo@0
|
47
|
meillo@10
|
48 static unsigned short
|
meillo@10
|
49 getshort(unsigned char *c)
|
meillo@0
|
50 {
|
meillo@10
|
51 unsigned short u;
|
meillo@10
|
52 u = c[0];
|
meillo@10
|
53 return (u << 8) + c[1];
|
meillo@0
|
54 }
|
meillo@0
|
55
|
meillo@10
|
56 static int
|
meillo@10
|
57 dns_resolve(char *domain, int type, gboolean do_search)
|
meillo@0
|
58 {
|
meillo@10
|
59 int n;
|
meillo@10
|
60 int i;
|
meillo@0
|
61
|
meillo@10
|
62 int resp_len;
|
meillo@10
|
63 /*errno = 0; */
|
meillo@0
|
64
|
meillo@10
|
65 /*
|
meillo@10
|
66 if (!stralloc_copy(&glue,domain)) return DNS_MEM;
|
meillo@10
|
67 if (!stralloc_0(&glue)) return DNS_MEM;
|
meillo@10
|
68 */
|
meillo@0
|
69
|
meillo@10
|
70 // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response));
|
meillo@10
|
71 DEBUG(5) debugf("DNS: before res_search()\n");
|
meillo@10
|
72 if (do_search)
|
meillo@10
|
73 resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response));
|
meillo@10
|
74 else
|
meillo@10
|
75 resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response));
|
meillo@10
|
76 DEBUG(5) debugf("DBG: after res_search()\n");
|
meillo@0
|
77
|
meillo@10
|
78 if (resp_len <= 0) {
|
meillo@10
|
79 /*
|
meillo@10
|
80 if (errno == ECONNREFUSED) return DNS_SOFT;
|
meillo@10
|
81 if (h_errno == TRY_AGAIN) return DNS_SOFT;
|
meillo@10
|
82 return DNS_HARD;
|
meillo@10
|
83 */
|
meillo@10
|
84 return -1;
|
meillo@10
|
85 }
|
meillo@10
|
86 if (resp_len >= sizeof(response))
|
meillo@10
|
87 resp_len = sizeof(response);
|
meillo@0
|
88
|
meillo@10
|
89 resp_end = response.buf + resp_len;
|
meillo@10
|
90 resp_pos = response.buf + sizeof(HEADER);
|
meillo@10
|
91 n = ntohs(response.hdr.qdcount);
|
meillo@0
|
92
|
meillo@10
|
93 while (n-- > 0) {
|
meillo@10
|
94 i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME);
|
meillo@10
|
95 if (i < 0)
|
meillo@10
|
96 return -1;
|
meillo@10
|
97 DEBUG(5) debugf("DBG: resolve name = %s\n", name);
|
meillo@10
|
98 resp_pos += i;
|
meillo@10
|
99 i = resp_end - resp_pos;
|
meillo@10
|
100 if (i < QFIXEDSZ)
|
meillo@10
|
101 return -1;
|
meillo@10
|
102 resp_pos += QFIXEDSZ;
|
meillo@10
|
103 }
|
meillo@10
|
104 num_answers = ntohs(response.hdr.ancount);
|
meillo@0
|
105
|
meillo@10
|
106 return 0;
|
meillo@0
|
107 }
|
meillo@0
|
108
|
meillo@10
|
109 static int
|
meillo@10
|
110 dns_next()
|
meillo@0
|
111 {
|
meillo@10
|
112 int i;
|
meillo@0
|
113
|
meillo@10
|
114 if (num_answers <= 0)
|
meillo@10
|
115 return 2;
|
meillo@10
|
116 num_answers--;
|
meillo@0
|
117
|
meillo@10
|
118 if (resp_pos == resp_end)
|
meillo@10
|
119 return -1; /* soft */
|
meillo@0
|
120
|
meillo@10
|
121 i = dn_expand(response.buf, resp_end, resp_pos, name, 256);
|
meillo@10
|
122 if (i < 0)
|
meillo@10
|
123 return -1; /* soft */
|
meillo@10
|
124 resp_pos += i;
|
meillo@0
|
125
|
meillo@10
|
126 i = resp_end - resp_pos;
|
meillo@10
|
127 if (i < 4 + 3 * 2)
|
meillo@10
|
128 return -1; /* soft */
|
meillo@0
|
129
|
meillo@10
|
130 rr_type = getshort(resp_pos);
|
meillo@10
|
131 rr_dlen = getshort(resp_pos + 8);
|
meillo@10
|
132 resp_pos += 10;
|
meillo@10
|
133
|
meillo@10
|
134 return 0;
|
meillo@0
|
135 }
|
meillo@0
|
136
|
meillo@10
|
137 static int
|
meillo@10
|
138 dns_getip(guint32 * ip)
|
meillo@0
|
139 {
|
meillo@10
|
140 int ret;
|
meillo@0
|
141
|
meillo@10
|
142 if ((ret = dns_next()))
|
meillo@10
|
143 return ret;
|
meillo@0
|
144
|
meillo@10
|
145 if (rr_type == T_A) {
|
meillo@10
|
146 if (rr_dlen < 4)
|
meillo@10
|
147 return -1; /* soft */
|
meillo@10
|
148 *ip = *(guint32 *) (resp_pos);
|
meillo@10
|
149 DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
|
meillo@10
|
150 resp_pos += rr_dlen;
|
meillo@0
|
151
|
meillo@10
|
152 return 1;
|
meillo@10
|
153 }
|
meillo@10
|
154 resp_pos += rr_dlen;
|
meillo@10
|
155 return 0;
|
meillo@0
|
156 }
|
meillo@0
|
157
|
meillo@10
|
158 static int
|
meillo@10
|
159 dns_getmx(int *pref)
|
meillo@0
|
160 {
|
meillo@10
|
161 int ret;
|
meillo@0
|
162
|
meillo@10
|
163 if ((ret = dns_next()))
|
meillo@10
|
164 return ret;
|
meillo@0
|
165
|
meillo@10
|
166 if (rr_type == T_MX) {
|
meillo@10
|
167 if (rr_dlen < 3)
|
meillo@10
|
168 return -1; /* soft */
|
meillo@0
|
169
|
meillo@10
|
170 *pref = (resp_pos[0] << 8) + resp_pos[1];
|
meillo@10
|
171 if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0)
|
meillo@10
|
172 return -1;
|
meillo@0
|
173
|
meillo@10
|
174 resp_pos += rr_dlen;
|
meillo@10
|
175
|
meillo@10
|
176 return 1;
|
meillo@10
|
177 }
|
meillo@10
|
178 resp_pos += rr_dlen;
|
meillo@10
|
179 return 0;
|
meillo@0
|
180 }
|
meillo@0
|
181
|
meillo@0
|
182 /*
|
meillo@10
|
183 static int
|
meillo@10
|
184 dns_getname(int type)
|
meillo@0
|
185 {
|
meillo@0
|
186 int ret;
|
meillo@0
|
187
|
meillo@0
|
188 if((ret = dns_next())) return ret;
|
meillo@0
|
189
|
meillo@0
|
190 if (rr_type == type){
|
meillo@0
|
191 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0)
|
meillo@0
|
192 return -1;
|
meillo@10
|
193
|
meillo@0
|
194 resp_pos += rr_dlen;
|
meillo@0
|
195
|
meillo@0
|
196 return 1;
|
meillo@0
|
197 }
|
meillo@0
|
198 resp_pos += rr_dlen;
|
meillo@0
|
199 return 0;
|
meillo@0
|
200 }
|
meillo@0
|
201 */
|
meillo@0
|
202
|
meillo@10
|
203 static int
|
meillo@10
|
204 dns_look_ip(gchar * domain, guint32 * ip)
|
meillo@0
|
205 {
|
meillo@10
|
206 gchar *n = domain;
|
meillo@0
|
207
|
meillo@10
|
208 while (TRUE) {
|
meillo@10
|
209 if (dns_resolve(n, T_A, FALSE) == 0) {
|
meillo@10
|
210 dns_next();
|
meillo@10
|
211 if (rr_type == T_A) {
|
meillo@10
|
212 if (rr_dlen < 4)
|
meillo@10
|
213 return -1; /* soft */
|
meillo@10
|
214 *ip = *(guint32 *) (resp_pos);
|
meillo@0
|
215
|
meillo@10
|
216 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
|
meillo@10
|
217
|
meillo@10
|
218 resp_pos += rr_dlen;
|
meillo@10
|
219 return 0;
|
meillo@10
|
220 } else if (rr_type == T_CNAME) {
|
meillo@10
|
221 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0)
|
meillo@10
|
222 return -1;
|
meillo@10
|
223
|
meillo@10
|
224 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name);
|
meillo@10
|
225
|
meillo@10
|
226 resp_pos += rr_dlen;
|
meillo@10
|
227 n = name;
|
meillo@10
|
228 } else
|
meillo@10
|
229 return -1;
|
meillo@10
|
230 } else
|
meillo@10
|
231 return -1;
|
meillo@10
|
232 }
|
meillo@0
|
233 }
|
meillo@0
|
234
|
meillo@10
|
235 GList*
|
meillo@10
|
236 resolve_dns_a(GList * list, gchar * domain)
|
meillo@0
|
237 {
|
meillo@10
|
238 int ret;
|
meillo@0
|
239
|
meillo@10
|
240 DEBUG(5) debugf("DNS: resolve_dns_a entered\n");
|
meillo@0
|
241
|
meillo@10
|
242 if (dns_resolve(domain, T_A, TRUE) == 0) {
|
meillo@10
|
243 mxip_addr mxip;
|
meillo@10
|
244 while ((ret = dns_getip(&(mxip.ip))) != 2) {
|
meillo@10
|
245 if (ret == 1) {
|
meillo@10
|
246 mxip.name = g_strdup(name);
|
meillo@10
|
247 mxip.pref = 0;
|
meillo@10
|
248 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
|
meillo@10
|
249 }
|
meillo@10
|
250 }
|
meillo@10
|
251 }
|
meillo@10
|
252 return list;
|
meillo@0
|
253 }
|
meillo@0
|
254
|
meillo@10
|
255 static gint
|
meillo@10
|
256 _mx_sort_func(gconstpointer aa, gconstpointer bb)
|
meillo@0
|
257 {
|
meillo@10
|
258 const mxip_addr *a = (mxip_addr *) aa;
|
meillo@10
|
259 const mxip_addr *b = (mxip_addr *) bb;
|
meillo@0
|
260
|
meillo@10
|
261 if (a->pref == b->pref)
|
meillo@10
|
262 return a->ip - b->ip;
|
meillo@10
|
263 else
|
meillo@10
|
264 return a->pref - b->pref;
|
meillo@0
|
265 }
|
meillo@0
|
266
|
meillo@10
|
267 GList*
|
meillo@10
|
268 resolve_dns_mx(GList * list, gchar * domain)
|
meillo@0
|
269 {
|
meillo@10
|
270 GList *node;
|
meillo@10
|
271 int ret;
|
meillo@10
|
272 int cnt = 0;
|
meillo@0
|
273
|
meillo@10
|
274 DEBUG(5) debugf("DNS: resolve_dns_mx entered\n");
|
meillo@0
|
275
|
meillo@10
|
276 if (dns_resolve(domain, T_MX, TRUE) == 0) {
|
meillo@10
|
277 GList *node_next;
|
meillo@10
|
278 mxip_addr mxip;
|
meillo@10
|
279 while ((ret = dns_getmx(&(mxip.pref))) != 2) {
|
meillo@10
|
280 if (ret == 1) {
|
meillo@10
|
281 mxip.name = g_strdup(name);
|
meillo@10
|
282 mxip.ip = rand();
|
meillo@10
|
283 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
|
meillo@10
|
284 cnt++;
|
meillo@10
|
285 }
|
meillo@10
|
286 }
|
meillo@0
|
287
|
meillo@10
|
288 DEBUG(5) debugf("DNS: found %d mx records\n", cnt);
|
meillo@0
|
289
|
meillo@10
|
290 /* to randomize sequences with equal pref values,
|
meillo@10
|
291 we temporarily 'misused' the ip field and
|
meillo@10
|
292 put a random number in it as a secondary sort key.
|
meillo@10
|
293 */
|
meillo@10
|
294 list = g_list_sort(list, _mx_sort_func);
|
meillo@0
|
295
|
meillo@10
|
296 /* CNAME resolving has to be added as well. */
|
meillo@0
|
297
|
meillo@10
|
298 for (node = g_list_first(list); node != NULL; node = node_next) {
|
meillo@0
|
299
|
meillo@10
|
300 mxip_addr *p_mxip = (mxip_addr *) (node->data);
|
meillo@10
|
301 node_next = g_list_next(node);
|
meillo@0
|
302
|
meillo@10
|
303 if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) {
|
meillo@10
|
304 DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name);
|
meillo@10
|
305 list = g_list_remove_link(list, node);
|
meillo@10
|
306 g_free(node->data);
|
meillo@10
|
307 g_list_free_1(node);
|
meillo@10
|
308 }
|
meillo@10
|
309 }
|
meillo@10
|
310 }
|
meillo@10
|
311 return list;
|
meillo@0
|
312 }
|
meillo@0
|
313
|
meillo@0
|
314 #endif
|
meillo@0
|
315
|
meillo@0
|
316 /* now something completely different... */
|
meillo@0
|
317
|
meillo@10
|
318 GList*
|
meillo@10
|
319 resolve_byname(GList * list, gchar * domain)
|
meillo@0
|
320 {
|
meillo@10
|
321 struct hostent *hent;
|
meillo@0
|
322
|
meillo@10
|
323 DEBUG(5) debugf("DNS: resolve_byname entered\n");
|
meillo@0
|
324
|
meillo@10
|
325 if ((hent = gethostbyname(domain))) {
|
meillo@10
|
326 char *haddr;
|
meillo@10
|
327 int i = 0;
|
meillo@10
|
328 while ((haddr = hent->h_addr_list[i++])) {
|
meillo@10
|
329 mxip_addr mxip;
|
meillo@10
|
330 mxip.ip = *(guint32 *) (haddr);
|
meillo@10
|
331 mxip.pref = 0;
|
meillo@10
|
332 mxip.name = g_strdup(hent->h_name);
|
meillo@10
|
333 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
|
meillo@10
|
334 }
|
meillo@10
|
335 }
|
meillo@10
|
336 return list;
|
meillo@0
|
337 }
|
meillo@0
|
338
|
meillo@0
|
339 #ifdef RESOLV_TEST
|
meillo@10
|
340 int
|
meillo@10
|
341 main(int argc, char *argv[])
|
meillo@0
|
342 {
|
meillo@10
|
343 GList *addr_list = NULL, *node;
|
meillo@0
|
344
|
meillo@10
|
345 g_print("starting res_init()\n");
|
meillo@0
|
346
|
meillo@10
|
347 g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry);
|
meillo@0
|
348
|
meillo@10
|
349 if (res_init() == 0) {
|
meillo@0
|
350
|
meillo@10
|
351 addr_list = resolve_dns_a(NULL, argv[1]);
|
meillo@10
|
352 g_print("A:\n");
|
meillo@0
|
353
|
meillo@10
|
354 foreach(addr_list, node) {
|
meillo@10
|
355 mxip_addr *p_mxip = (mxip_addr *) (node->data);
|
meillo@10
|
356
|
meillo@10
|
357 printf("name = %s\n IP = %s\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip)));
|
meillo@10
|
358 }
|
meillo@10
|
359 addr_list = resolve_dns_mx(NULL, argv[1]);
|
meillo@10
|
360 g_print("MX:\n");
|
meillo@10
|
361
|
meillo@10
|
362 foreach(addr_list, node) {
|
meillo@10
|
363 mxip_addr *p_mxip = (mxip_addr *) (node->data);
|
meillo@10
|
364
|
meillo@10
|
365 printf("name = %s\n IP = %s pref = %d\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip)), p_mxip->pref);
|
meillo@10
|
366 }
|
meillo@10
|
367 {
|
meillo@10
|
368 guint32 ip;
|
meillo@10
|
369 dns_look_ip(argv[1], &ip);
|
meillo@10
|
370 printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *) (&ip))));
|
meillo@10
|
371 }
|
meillo@10
|
372 } else
|
meillo@10
|
373 printf("res_init() failed.\n");
|
meillo@10
|
374
|
meillo@0
|
375 }
|
meillo@0
|
376 #endif
|