masqmail

view src/lookup.c @ 200:116b0269c934

reworked resolvtest; let it build; refactored in lookup.c the resolvtest helper and code test tool was available in the code but did not get built I moved it to resolvtest.c and included it into the build process the define RESOLV_TEST was removed
author meillo@marmaro.de
date Fri, 16 Jul 2010 12:53:07 +0200
parents f671821d8222
children 138e66e1a61f
line source
1 /* MasqMail Copyright (C) Oliver Kurth,
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
21 #include <resolv.h>
23 #include "masqmail.h"
26 #ifdef ENABLE_RESOLVER
28 static union {
29 HEADER hdr;
30 unsigned char buf[PACKETSZ];
31 } response;
32 static unsigned char *resp_end;
33 static unsigned char *resp_pos;
35 static int num_answers;
36 static char name[MAX_DNSNAME];
38 unsigned short rr_type;
39 unsigned short rr_dlen;
41 static unsigned short
42 getshort(unsigned char *c)
43 {
44 unsigned short u;
45 u = c[0];
46 return (u << 8) + c[1];
47 }
49 static int
50 dns_resolve(char *domain, int type, gboolean do_search)
51 {
52 int n;
53 int i;
55 int resp_len;
56 /*errno = 0; */
58 /*
59 if (!stralloc_copy(&glue,domain)) return DNS_MEM;
60 if (!stralloc_0(&glue)) return DNS_MEM;
61 */
63 // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response));
64 DEBUG(5) debugf("DNS: before res_search()\n");
65 if (do_search)
66 resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response));
67 else
68 resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response));
69 DEBUG(5) debugf("DBG: after res_search()\n");
71 if (resp_len <= 0) {
72 /*
73 if (errno == ECONNREFUSED) return DNS_SOFT;
74 if (h_errno == TRY_AGAIN) return DNS_SOFT;
75 return DNS_HARD;
76 */
77 return -1;
78 }
79 if (resp_len >= sizeof(response))
80 resp_len = sizeof(response);
82 resp_end = response.buf + resp_len;
83 resp_pos = response.buf + sizeof(HEADER);
84 n = ntohs(response.hdr.qdcount);
86 while (n-- > 0) {
87 i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME);
88 if (i < 0)
89 return -1;
90 DEBUG(5) debugf("DBG: resolve name = %s\n", name);
91 resp_pos += i;
92 i = resp_end - resp_pos;
93 if (i < QFIXEDSZ)
94 return -1;
95 resp_pos += QFIXEDSZ;
96 }
97 num_answers = ntohs(response.hdr.ancount);
99 return 0;
100 }
102 static int
103 dns_next()
104 {
105 int i;
107 if (num_answers <= 0)
108 return 2;
109 num_answers--;
111 if (resp_pos == resp_end)
112 return -1; /* soft */
114 i = dn_expand(response.buf, resp_end, resp_pos, name, 256);
115 if (i < 0)
116 return -1; /* soft */
117 resp_pos += i;
119 i = resp_end - resp_pos;
120 if (i < 4 + 3 * 2)
121 return -1; /* soft */
123 rr_type = getshort(resp_pos);
124 rr_dlen = getshort(resp_pos + 8);
125 resp_pos += 10;
127 return 0;
128 }
130 static int
131 dns_getip(guint32 * ip)
132 {
133 int ret;
135 if ((ret = dns_next()))
136 return ret;
138 if (rr_type == T_A) {
139 if (rr_dlen < 4)
140 return -1; /* soft */
141 *ip = *(guint32 *) (resp_pos);
142 DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
143 resp_pos += rr_dlen;
145 return 1;
146 }
147 resp_pos += rr_dlen;
148 return 0;
149 }
151 static int
152 dns_getmx(int *pref)
153 {
154 int ret;
156 if ((ret = dns_next()))
157 return ret;
159 if (rr_type == T_MX) {
160 if (rr_dlen < 3)
161 return -1; /* soft */
163 *pref = (resp_pos[0] << 8) + resp_pos[1];
164 if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0)
165 return -1;
167 resp_pos += rr_dlen;
169 return 1;
170 }
171 resp_pos += rr_dlen;
172 return 0;
173 }
175 /*
176 static int
177 dns_getname(int type)
178 {
179 int ret;
181 if((ret = dns_next())) return ret;
183 if (rr_type == type){
184 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0)
185 return -1;
187 resp_pos += rr_dlen;
189 return 1;
190 }
191 resp_pos += rr_dlen;
192 return 0;
193 }
194 */
196 int
197 dns_look_ip(gchar * domain, guint32 * ip)
198 {
199 gchar *n = domain;
201 while (TRUE) {
202 if (dns_resolve(n, T_A, FALSE) != 0) {
203 return -1;
204 }
206 dns_next();
207 if (rr_type == T_A) {
208 if (rr_dlen < 4) {
209 return -1; /* soft */
210 }
211 *ip = *(guint32 *) (resp_pos);
213 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
215 resp_pos += rr_dlen;
216 return 0;
217 } else if (rr_type == T_CNAME) {
218 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) {
219 return -1;
220 }
222 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name);
224 resp_pos += rr_dlen;
225 n = name;
226 } else {
227 return -1;
228 }
229 }
230 }
232 GList*
233 resolve_dns_a(GList * list, gchar * domain)
234 {
235 int ret;
237 DEBUG(5) debugf("DNS: resolve_dns_a entered\n");
239 if (dns_resolve(domain, T_A, TRUE) == 0) {
240 mxip_addr mxip;
241 while ((ret = dns_getip(&(mxip.ip))) != 2) {
242 if (ret == 1) {
243 mxip.name = g_strdup(name);
244 mxip.pref = 0;
245 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
246 }
247 }
248 }
249 return list;
250 }
252 static gint
253 _mx_sort_func(gconstpointer aa, gconstpointer bb)
254 {
255 const mxip_addr *a = (mxip_addr *) aa;
256 const mxip_addr *b = (mxip_addr *) bb;
258 if (a->pref == b->pref)
259 return a->ip - b->ip;
260 else
261 return a->pref - b->pref;
262 }
264 GList*
265 resolve_dns_mx(GList * list, gchar * domain)
266 {
267 GList *node;
268 int ret;
269 int cnt = 0;
271 DEBUG(5) debugf("DNS: resolve_dns_mx entered\n");
273 if (dns_resolve(domain, T_MX, TRUE) == 0) {
274 GList *node_next;
275 mxip_addr mxip;
276 while ((ret = dns_getmx(&(mxip.pref))) != 2) {
277 if (ret == 1) {
278 mxip.name = g_strdup(name);
279 mxip.ip = rand();
280 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
281 cnt++;
282 }
283 }
285 DEBUG(5) debugf("DNS: found %d mx records\n", cnt);
287 /* to randomize sequences with equal pref values,
288 we temporarily 'misused' the ip field and
289 put a random number in it as a secondary sort key.
290 */
291 list = g_list_sort(list, _mx_sort_func);
293 /* CNAME resolving has to be added as well. */
295 for (node = g_list_first(list); node != NULL; node = node_next) {
297 mxip_addr *p_mxip = (mxip_addr *) (node->data);
298 node_next = g_list_next(node);
300 if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) {
301 DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name);
302 list = g_list_remove_link(list, node);
303 g_free(node->data);
304 g_list_free_1(node);
305 }
306 }
307 }
308 return list;
309 }
311 #endif
313 /* now something completely different... */
315 GList*
316 resolve_byname(GList * list, gchar * domain)
317 {
318 struct hostent *hent;
320 DEBUG(5) debugf("DNS: resolve_byname entered\n");
322 if ((hent = gethostbyname(domain))) {
323 char *haddr;
324 int i = 0;
325 while ((haddr = hent->h_addr_list[i++])) {
326 mxip_addr mxip;
327 mxip.ip = *(guint32 *) (haddr);
328 mxip.pref = 0;
329 mxip.name = g_strdup(hent->h_name);
330 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
331 }
332 }
333 return list;
334 }