masqmail

annotate 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
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@200 175 int
meillo@10 176 dns_look_ip(gchar * domain, guint32 * ip)
meillo@0 177 {
meillo@10 178 gchar *n = domain;
meillo@0 179
meillo@10 180 while (TRUE) {
meillo@200 181 if (dns_resolve(n, T_A, FALSE) != 0) {
meillo@200 182 return -1;
meillo@200 183 }
meillo@0 184
meillo@200 185 dns_next();
meillo@200 186 if (rr_type == T_A) {
meillo@200 187 if (rr_dlen < 4) {
meillo@200 188 return -1; /* soft */
meillo@200 189 }
meillo@200 190 *ip = *(guint32 *) (resp_pos);
meillo@10 191
meillo@200 192 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
meillo@10 193
meillo@200 194 resp_pos += rr_dlen;
meillo@200 195 return 0;
meillo@200 196 } else if (rr_type == T_CNAME) {
meillo@200 197 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) {
meillo@200 198 return -1;
meillo@200 199 }
meillo@10 200
meillo@200 201 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name);
meillo@200 202
meillo@200 203 resp_pos += rr_dlen;
meillo@200 204 n = name;
meillo@200 205 } else {
meillo@10 206 return -1;
meillo@200 207 }
meillo@10 208 }
meillo@0 209 }
meillo@0 210
meillo@10 211 GList*
meillo@10 212 resolve_dns_a(GList * list, gchar * domain)
meillo@0 213 {
meillo@10 214 int ret;
meillo@0 215
meillo@10 216 DEBUG(5) debugf("DNS: resolve_dns_a entered\n");
meillo@0 217
meillo@10 218 if (dns_resolve(domain, T_A, TRUE) == 0) {
meillo@10 219 mxip_addr mxip;
meillo@10 220 while ((ret = dns_getip(&(mxip.ip))) != 2) {
meillo@10 221 if (ret == 1) {
meillo@10 222 mxip.name = g_strdup(name);
meillo@10 223 mxip.pref = 0;
meillo@10 224 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
meillo@10 225 }
meillo@10 226 }
meillo@10 227 }
meillo@10 228 return list;
meillo@0 229 }
meillo@0 230
meillo@10 231 static gint
meillo@10 232 _mx_sort_func(gconstpointer aa, gconstpointer bb)
meillo@0 233 {
meillo@10 234 const mxip_addr *a = (mxip_addr *) aa;
meillo@10 235 const mxip_addr *b = (mxip_addr *) bb;
meillo@0 236
meillo@10 237 if (a->pref == b->pref)
meillo@10 238 return a->ip - b->ip;
meillo@10 239 else
meillo@10 240 return a->pref - b->pref;
meillo@0 241 }
meillo@0 242
meillo@10 243 GList*
meillo@10 244 resolve_dns_mx(GList * list, gchar * domain)
meillo@0 245 {
meillo@10 246 GList *node;
meillo@10 247 int ret;
meillo@10 248 int cnt = 0;
meillo@0 249
meillo@10 250 DEBUG(5) debugf("DNS: resolve_dns_mx entered\n");
meillo@0 251
meillo@10 252 if (dns_resolve(domain, T_MX, TRUE) == 0) {
meillo@10 253 GList *node_next;
meillo@10 254 mxip_addr mxip;
meillo@10 255 while ((ret = dns_getmx(&(mxip.pref))) != 2) {
meillo@10 256 if (ret == 1) {
meillo@10 257 mxip.name = g_strdup(name);
meillo@10 258 mxip.ip = rand();
meillo@10 259 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
meillo@10 260 cnt++;
meillo@10 261 }
meillo@10 262 }
meillo@0 263
meillo@10 264 DEBUG(5) debugf("DNS: found %d mx records\n", cnt);
meillo@0 265
meillo@10 266 /* to randomize sequences with equal pref values,
meillo@10 267 we temporarily 'misused' the ip field and
meillo@10 268 put a random number in it as a secondary sort key.
meillo@10 269 */
meillo@10 270 list = g_list_sort(list, _mx_sort_func);
meillo@0 271
meillo@10 272 /* CNAME resolving has to be added as well. */
meillo@0 273
meillo@10 274 for (node = g_list_first(list); node != NULL; node = node_next) {
meillo@0 275
meillo@10 276 mxip_addr *p_mxip = (mxip_addr *) (node->data);
meillo@10 277 node_next = g_list_next(node);
meillo@0 278
meillo@10 279 if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) {
meillo@10 280 DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name);
meillo@10 281 list = g_list_remove_link(list, node);
meillo@10 282 g_free(node->data);
meillo@10 283 g_list_free_1(node);
meillo@10 284 }
meillo@10 285 }
meillo@10 286 }
meillo@10 287 return list;
meillo@0 288 }
meillo@0 289
meillo@0 290 #endif
meillo@0 291
meillo@0 292 /* now something completely different... */
meillo@0 293
meillo@10 294 GList*
meillo@10 295 resolve_byname(GList * list, gchar * domain)
meillo@0 296 {
meillo@10 297 struct hostent *hent;
meillo@0 298
meillo@10 299 DEBUG(5) debugf("DNS: resolve_byname entered\n");
meillo@0 300
meillo@10 301 if ((hent = gethostbyname(domain))) {
meillo@10 302 char *haddr;
meillo@10 303 int i = 0;
meillo@10 304 while ((haddr = hent->h_addr_list[i++])) {
meillo@10 305 mxip_addr mxip;
meillo@10 306 mxip.ip = *(guint32 *) (haddr);
meillo@10 307 mxip.pref = 0;
meillo@10 308 mxip.name = g_strdup(hent->h_name);
meillo@10 309 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
meillo@10 310 }
meillo@10 311 }
meillo@10 312 return list;
meillo@0 313 }