masqmail
view src/lookup.c @ 201:138e66e1a61f
removed dns_getname() which was already commented
seems as if one can use dns_getip() or dns_getmx() instead without problem
their code is almost identical to the one of dns_getname() anyway
author | meillo@marmaro.de |
---|---|
date | Fri, 16 Jul 2010 13:08:29 +0200 |
parents | 116b0269c934 |
children | dcb315792513 |
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 int
176 dns_look_ip(gchar * domain, guint32 * ip)
177 {
178 gchar *n = domain;
180 while (TRUE) {
181 if (dns_resolve(n, T_A, FALSE) != 0) {
182 return -1;
183 }
185 dns_next();
186 if (rr_type == T_A) {
187 if (rr_dlen < 4) {
188 return -1; /* soft */
189 }
190 *ip = *(guint32 *) (resp_pos);
192 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
194 resp_pos += rr_dlen;
195 return 0;
196 } else if (rr_type == T_CNAME) {
197 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) {
198 return -1;
199 }
201 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name);
203 resp_pos += rr_dlen;
204 n = name;
205 } else {
206 return -1;
207 }
208 }
209 }
211 GList*
212 resolve_dns_a(GList * list, gchar * domain)
213 {
214 int ret;
216 DEBUG(5) debugf("DNS: resolve_dns_a entered\n");
218 if (dns_resolve(domain, T_A, TRUE) == 0) {
219 mxip_addr mxip;
220 while ((ret = dns_getip(&(mxip.ip))) != 2) {
221 if (ret == 1) {
222 mxip.name = g_strdup(name);
223 mxip.pref = 0;
224 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
225 }
226 }
227 }
228 return list;
229 }
231 static gint
232 _mx_sort_func(gconstpointer aa, gconstpointer bb)
233 {
234 const mxip_addr *a = (mxip_addr *) aa;
235 const mxip_addr *b = (mxip_addr *) bb;
237 if (a->pref == b->pref)
238 return a->ip - b->ip;
239 else
240 return a->pref - b->pref;
241 }
243 GList*
244 resolve_dns_mx(GList * list, gchar * domain)
245 {
246 GList *node;
247 int ret;
248 int cnt = 0;
250 DEBUG(5) debugf("DNS: resolve_dns_mx entered\n");
252 if (dns_resolve(domain, T_MX, TRUE) == 0) {
253 GList *node_next;
254 mxip_addr mxip;
255 while ((ret = dns_getmx(&(mxip.pref))) != 2) {
256 if (ret == 1) {
257 mxip.name = g_strdup(name);
258 mxip.ip = rand();
259 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
260 cnt++;
261 }
262 }
264 DEBUG(5) debugf("DNS: found %d mx records\n", cnt);
266 /* to randomize sequences with equal pref values,
267 we temporarily 'misused' the ip field and
268 put a random number in it as a secondary sort key.
269 */
270 list = g_list_sort(list, _mx_sort_func);
272 /* CNAME resolving has to be added as well. */
274 for (node = g_list_first(list); node != NULL; node = node_next) {
276 mxip_addr *p_mxip = (mxip_addr *) (node->data);
277 node_next = g_list_next(node);
279 if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) {
280 DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name);
281 list = g_list_remove_link(list, node);
282 g_free(node->data);
283 g_list_free_1(node);
284 }
285 }
286 }
287 return list;
288 }
290 #endif
292 /* now something completely different... */
294 GList*
295 resolve_byname(GList * list, gchar * domain)
296 {
297 struct hostent *hent;
299 DEBUG(5) debugf("DNS: resolve_byname entered\n");
301 if ((hent = gethostbyname(domain))) {
302 char *haddr;
303 int i = 0;
304 while ((haddr = hent->h_addr_list[i++])) {
305 mxip_addr mxip;
306 mxip.ip = *(guint32 *) (haddr);
307 mxip.pref = 0;
308 mxip.name = g_strdup(hent->h_name);
309 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
310 }
311 }
312 return list;
313 }