masqmail

annotate 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
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
meillo@0 26 #ifdef ENABLE_RESOLVER
meillo@0 27
meillo@0 28 static union {
meillo@10 29 HEADER hdr;
meillo@10 30 unsigned char buf[PACKETSZ];
meillo@0 31 } response;
meillo@0 32 static unsigned char *resp_end;
meillo@0 33 static unsigned char *resp_pos;
meillo@0 34
meillo@0 35 static int num_answers;
meillo@0 36 static char name[MAX_DNSNAME];
meillo@0 37
meillo@0 38 unsigned short rr_type;
meillo@0 39 unsigned short rr_dlen;
meillo@0 40
meillo@10 41 static unsigned short
meillo@10 42 getshort(unsigned char *c)
meillo@0 43 {
meillo@10 44 unsigned short u;
meillo@10 45 u = c[0];
meillo@10 46 return (u << 8) + c[1];
meillo@0 47 }
meillo@0 48
meillo@10 49 static int
meillo@10 50 dns_resolve(char *domain, int type, gboolean do_search)
meillo@0 51 {
meillo@10 52 int n;
meillo@10 53 int i;
meillo@0 54
meillo@10 55 int resp_len;
meillo@10 56 /*errno = 0; */
meillo@0 57
meillo@10 58 /*
meillo@10 59 if (!stralloc_copy(&glue,domain)) return DNS_MEM;
meillo@10 60 if (!stralloc_0(&glue)) return DNS_MEM;
meillo@10 61 */
meillo@0 62
meillo@10 63 // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response));
meillo@10 64 DEBUG(5) debugf("DNS: before res_search()\n");
meillo@10 65 if (do_search)
meillo@10 66 resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response));
meillo@10 67 else
meillo@10 68 resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response));
meillo@10 69 DEBUG(5) debugf("DBG: after res_search()\n");
meillo@0 70
meillo@10 71 if (resp_len <= 0) {
meillo@10 72 /*
meillo@10 73 if (errno == ECONNREFUSED) return DNS_SOFT;
meillo@10 74 if (h_errno == TRY_AGAIN) return DNS_SOFT;
meillo@10 75 return DNS_HARD;
meillo@10 76 */
meillo@10 77 return -1;
meillo@10 78 }
meillo@10 79 if (resp_len >= sizeof(response))
meillo@10 80 resp_len = sizeof(response);
meillo@0 81
meillo@10 82 resp_end = response.buf + resp_len;
meillo@10 83 resp_pos = response.buf + sizeof(HEADER);
meillo@10 84 n = ntohs(response.hdr.qdcount);
meillo@0 85
meillo@10 86 while (n-- > 0) {
meillo@10 87 i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME);
meillo@10 88 if (i < 0)
meillo@10 89 return -1;
meillo@10 90 DEBUG(5) debugf("DBG: resolve name = %s\n", name);
meillo@10 91 resp_pos += i;
meillo@10 92 i = resp_end - resp_pos;
meillo@10 93 if (i < QFIXEDSZ)
meillo@10 94 return -1;
meillo@10 95 resp_pos += QFIXEDSZ;
meillo@10 96 }
meillo@10 97 num_answers = ntohs(response.hdr.ancount);
meillo@0 98
meillo@10 99 return 0;
meillo@0 100 }
meillo@0 101
meillo@10 102 static int
meillo@10 103 dns_next()
meillo@0 104 {
meillo@10 105 int i;
meillo@0 106
meillo@10 107 if (num_answers <= 0)
meillo@10 108 return 2;
meillo@10 109 num_answers--;
meillo@0 110
meillo@10 111 if (resp_pos == resp_end)
meillo@10 112 return -1; /* soft */
meillo@0 113
meillo@10 114 i = dn_expand(response.buf, resp_end, resp_pos, name, 256);
meillo@10 115 if (i < 0)
meillo@10 116 return -1; /* soft */
meillo@10 117 resp_pos += i;
meillo@0 118
meillo@10 119 i = resp_end - resp_pos;
meillo@10 120 if (i < 4 + 3 * 2)
meillo@10 121 return -1; /* soft */
meillo@0 122
meillo@10 123 rr_type = getshort(resp_pos);
meillo@10 124 rr_dlen = getshort(resp_pos + 8);
meillo@10 125 resp_pos += 10;
meillo@10 126
meillo@10 127 return 0;
meillo@0 128 }
meillo@0 129
meillo@10 130 static int
meillo@10 131 dns_getip(guint32 * ip)
meillo@0 132 {
meillo@10 133 int ret;
meillo@0 134
meillo@10 135 if ((ret = dns_next()))
meillo@10 136 return ret;
meillo@0 137
meillo@10 138 if (rr_type == T_A) {
meillo@10 139 if (rr_dlen < 4)
meillo@10 140 return -1; /* soft */
meillo@10 141 *ip = *(guint32 *) (resp_pos);
meillo@10 142 DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
meillo@10 143 resp_pos += rr_dlen;
meillo@0 144
meillo@10 145 return 1;
meillo@10 146 }
meillo@10 147 resp_pos += rr_dlen;
meillo@10 148 return 0;
meillo@0 149 }
meillo@0 150
meillo@10 151 static int
meillo@10 152 dns_getmx(int *pref)
meillo@0 153 {
meillo@10 154 int ret;
meillo@0 155
meillo@10 156 if ((ret = dns_next()))
meillo@10 157 return ret;
meillo@0 158
meillo@10 159 if (rr_type == T_MX) {
meillo@10 160 if (rr_dlen < 3)
meillo@10 161 return -1; /* soft */
meillo@0 162
meillo@10 163 *pref = (resp_pos[0] << 8) + resp_pos[1];
meillo@10 164 if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0)
meillo@10 165 return -1;
meillo@0 166
meillo@10 167 resp_pos += rr_dlen;
meillo@10 168
meillo@10 169 return 1;
meillo@10 170 }
meillo@10 171 resp_pos += rr_dlen;
meillo@10 172 return 0;
meillo@0 173 }
meillo@0 174
meillo@0 175 /*
meillo@10 176 static int
meillo@10 177 dns_getname(int type)
meillo@0 178 {
meillo@0 179 int ret;
meillo@0 180
meillo@0 181 if((ret = dns_next())) return ret;
meillo@0 182
meillo@0 183 if (rr_type == type){
meillo@0 184 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0)
meillo@0 185 return -1;
meillo@10 186
meillo@0 187 resp_pos += rr_dlen;
meillo@0 188
meillo@0 189 return 1;
meillo@0 190 }
meillo@0 191 resp_pos += rr_dlen;
meillo@0 192 return 0;
meillo@0 193 }
meillo@0 194 */
meillo@0 195
meillo@200 196 int
meillo@10 197 dns_look_ip(gchar * domain, guint32 * ip)
meillo@0 198 {
meillo@10 199 gchar *n = domain;
meillo@0 200
meillo@10 201 while (TRUE) {
meillo@200 202 if (dns_resolve(n, T_A, FALSE) != 0) {
meillo@200 203 return -1;
meillo@200 204 }
meillo@0 205
meillo@200 206 dns_next();
meillo@200 207 if (rr_type == T_A) {
meillo@200 208 if (rr_dlen < 4) {
meillo@200 209 return -1; /* soft */
meillo@200 210 }
meillo@200 211 *ip = *(guint32 *) (resp_pos);
meillo@10 212
meillo@200 213 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
meillo@10 214
meillo@200 215 resp_pos += rr_dlen;
meillo@200 216 return 0;
meillo@200 217 } else if (rr_type == T_CNAME) {
meillo@200 218 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) {
meillo@200 219 return -1;
meillo@200 220 }
meillo@10 221
meillo@200 222 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name);
meillo@200 223
meillo@200 224 resp_pos += rr_dlen;
meillo@200 225 n = name;
meillo@200 226 } else {
meillo@10 227 return -1;
meillo@200 228 }
meillo@10 229 }
meillo@0 230 }
meillo@0 231
meillo@10 232 GList*
meillo@10 233 resolve_dns_a(GList * list, gchar * domain)
meillo@0 234 {
meillo@10 235 int ret;
meillo@0 236
meillo@10 237 DEBUG(5) debugf("DNS: resolve_dns_a entered\n");
meillo@0 238
meillo@10 239 if (dns_resolve(domain, T_A, TRUE) == 0) {
meillo@10 240 mxip_addr mxip;
meillo@10 241 while ((ret = dns_getip(&(mxip.ip))) != 2) {
meillo@10 242 if (ret == 1) {
meillo@10 243 mxip.name = g_strdup(name);
meillo@10 244 mxip.pref = 0;
meillo@10 245 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
meillo@10 246 }
meillo@10 247 }
meillo@10 248 }
meillo@10 249 return list;
meillo@0 250 }
meillo@0 251
meillo@10 252 static gint
meillo@10 253 _mx_sort_func(gconstpointer aa, gconstpointer bb)
meillo@0 254 {
meillo@10 255 const mxip_addr *a = (mxip_addr *) aa;
meillo@10 256 const mxip_addr *b = (mxip_addr *) bb;
meillo@0 257
meillo@10 258 if (a->pref == b->pref)
meillo@10 259 return a->ip - b->ip;
meillo@10 260 else
meillo@10 261 return a->pref - b->pref;
meillo@0 262 }
meillo@0 263
meillo@10 264 GList*
meillo@10 265 resolve_dns_mx(GList * list, gchar * domain)
meillo@0 266 {
meillo@10 267 GList *node;
meillo@10 268 int ret;
meillo@10 269 int cnt = 0;
meillo@0 270
meillo@10 271 DEBUG(5) debugf("DNS: resolve_dns_mx entered\n");
meillo@0 272
meillo@10 273 if (dns_resolve(domain, T_MX, TRUE) == 0) {
meillo@10 274 GList *node_next;
meillo@10 275 mxip_addr mxip;
meillo@10 276 while ((ret = dns_getmx(&(mxip.pref))) != 2) {
meillo@10 277 if (ret == 1) {
meillo@10 278 mxip.name = g_strdup(name);
meillo@10 279 mxip.ip = rand();
meillo@10 280 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
meillo@10 281 cnt++;
meillo@10 282 }
meillo@10 283 }
meillo@0 284
meillo@10 285 DEBUG(5) debugf("DNS: found %d mx records\n", cnt);
meillo@0 286
meillo@10 287 /* to randomize sequences with equal pref values,
meillo@10 288 we temporarily 'misused' the ip field and
meillo@10 289 put a random number in it as a secondary sort key.
meillo@10 290 */
meillo@10 291 list = g_list_sort(list, _mx_sort_func);
meillo@0 292
meillo@10 293 /* CNAME resolving has to be added as well. */
meillo@0 294
meillo@10 295 for (node = g_list_first(list); node != NULL; node = node_next) {
meillo@0 296
meillo@10 297 mxip_addr *p_mxip = (mxip_addr *) (node->data);
meillo@10 298 node_next = g_list_next(node);
meillo@0 299
meillo@10 300 if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) {
meillo@10 301 DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name);
meillo@10 302 list = g_list_remove_link(list, node);
meillo@10 303 g_free(node->data);
meillo@10 304 g_list_free_1(node);
meillo@10 305 }
meillo@10 306 }
meillo@10 307 }
meillo@10 308 return list;
meillo@0 309 }
meillo@0 310
meillo@0 311 #endif
meillo@0 312
meillo@0 313 /* now something completely different... */
meillo@0 314
meillo@10 315 GList*
meillo@10 316 resolve_byname(GList * list, gchar * domain)
meillo@0 317 {
meillo@10 318 struct hostent *hent;
meillo@0 319
meillo@10 320 DEBUG(5) debugf("DNS: resolve_byname entered\n");
meillo@0 321
meillo@10 322 if ((hent = gethostbyname(domain))) {
meillo@10 323 char *haddr;
meillo@10 324 int i = 0;
meillo@10 325 while ((haddr = hent->h_addr_list[i++])) {
meillo@10 326 mxip_addr mxip;
meillo@10 327 mxip.ip = *(guint32 *) (haddr);
meillo@10 328 mxip.pref = 0;
meillo@10 329 mxip.name = g_strdup(hent->h_name);
meillo@10 330 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
meillo@10 331 }
meillo@10 332 }
meillo@10 333 return list;
meillo@0 334 }