meillo@0: /* MasqMail Copyright (C) Oliver Kurth, meillo@0: * meillo@0: * This program is free software; you can redistribute it and/or modify meillo@0: * it under the terms of the GNU General Public License as published by meillo@0: * the Free Software Foundation; either version 2 of the License, or meillo@0: * (at your option) any later version. meillo@10: * meillo@0: * This program is distributed in the hope that it will be useful, meillo@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: * GNU General Public License for more details. meillo@0: * meillo@0: * You should have received a copy of the GNU General Public License meillo@0: * along with this program; if not, write to the Free Software meillo@0: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. meillo@0: */ meillo@0: meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: meillo@0: #include "masqmail.h" meillo@0: meillo@0: #ifdef RESOLV_TEST meillo@0: #undef DEBUG meillo@0: #define DEBUG(x) if(x > 0) meillo@0: #define debugf g_print meillo@0: #endif meillo@0: meillo@0: #ifdef ENABLE_RESOLVER meillo@0: meillo@0: static union { meillo@10: HEADER hdr; meillo@10: unsigned char buf[PACKETSZ]; meillo@0: } response; meillo@0: static unsigned char *resp_end; meillo@0: static unsigned char *resp_pos; meillo@0: meillo@0: static int num_answers; meillo@0: static char name[MAX_DNSNAME]; meillo@0: meillo@0: unsigned short rr_type; meillo@0: unsigned short rr_dlen; meillo@0: meillo@10: static unsigned short meillo@10: getshort(unsigned char *c) meillo@0: { meillo@10: unsigned short u; meillo@10: u = c[0]; meillo@10: return (u << 8) + c[1]; meillo@0: } meillo@0: meillo@10: static int meillo@10: dns_resolve(char *domain, int type, gboolean do_search) meillo@0: { meillo@10: int n; meillo@10: int i; meillo@0: meillo@10: int resp_len; meillo@10: /*errno = 0; */ meillo@0: meillo@10: /* meillo@10: if (!stralloc_copy(&glue,domain)) return DNS_MEM; meillo@10: if (!stralloc_0(&glue)) return DNS_MEM; meillo@10: */ meillo@0: meillo@10: // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); meillo@10: DEBUG(5) debugf("DNS: before res_search()\n"); meillo@10: if (do_search) meillo@10: resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response)); meillo@10: else meillo@10: resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); meillo@10: DEBUG(5) debugf("DBG: after res_search()\n"); meillo@0: meillo@10: if (resp_len <= 0) { meillo@10: /* meillo@10: if (errno == ECONNREFUSED) return DNS_SOFT; meillo@10: if (h_errno == TRY_AGAIN) return DNS_SOFT; meillo@10: return DNS_HARD; meillo@10: */ meillo@10: return -1; meillo@10: } meillo@10: if (resp_len >= sizeof(response)) meillo@10: resp_len = sizeof(response); meillo@0: meillo@10: resp_end = response.buf + resp_len; meillo@10: resp_pos = response.buf + sizeof(HEADER); meillo@10: n = ntohs(response.hdr.qdcount); meillo@0: meillo@10: while (n-- > 0) { meillo@10: i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME); meillo@10: if (i < 0) meillo@10: return -1; meillo@10: DEBUG(5) debugf("DBG: resolve name = %s\n", name); meillo@10: resp_pos += i; meillo@10: i = resp_end - resp_pos; meillo@10: if (i < QFIXEDSZ) meillo@10: return -1; meillo@10: resp_pos += QFIXEDSZ; meillo@10: } meillo@10: num_answers = ntohs(response.hdr.ancount); meillo@0: meillo@10: return 0; meillo@0: } meillo@0: meillo@10: static int meillo@10: dns_next() meillo@0: { meillo@10: int i; meillo@0: meillo@10: if (num_answers <= 0) meillo@10: return 2; meillo@10: num_answers--; meillo@0: meillo@10: if (resp_pos == resp_end) meillo@10: return -1; /* soft */ meillo@0: meillo@10: i = dn_expand(response.buf, resp_end, resp_pos, name, 256); meillo@10: if (i < 0) meillo@10: return -1; /* soft */ meillo@10: resp_pos += i; meillo@0: meillo@10: i = resp_end - resp_pos; meillo@10: if (i < 4 + 3 * 2) meillo@10: return -1; /* soft */ meillo@0: meillo@10: rr_type = getshort(resp_pos); meillo@10: rr_dlen = getshort(resp_pos + 8); meillo@10: resp_pos += 10; meillo@10: meillo@10: return 0; meillo@0: } meillo@0: meillo@10: static int meillo@10: dns_getip(guint32 * ip) meillo@0: { meillo@10: int ret; meillo@0: meillo@10: if ((ret = dns_next())) meillo@10: return ret; meillo@0: meillo@10: if (rr_type == T_A) { meillo@10: if (rr_dlen < 4) meillo@10: return -1; /* soft */ meillo@10: *ip = *(guint32 *) (resp_pos); meillo@10: DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip)); meillo@10: resp_pos += rr_dlen; meillo@0: meillo@10: return 1; meillo@10: } meillo@10: resp_pos += rr_dlen; meillo@10: return 0; meillo@0: } meillo@0: meillo@10: static int meillo@10: dns_getmx(int *pref) meillo@0: { meillo@10: int ret; meillo@0: meillo@10: if ((ret = dns_next())) meillo@10: return ret; meillo@0: meillo@10: if (rr_type == T_MX) { meillo@10: if (rr_dlen < 3) meillo@10: return -1; /* soft */ meillo@0: meillo@10: *pref = (resp_pos[0] << 8) + resp_pos[1]; meillo@10: if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0) meillo@10: return -1; meillo@0: meillo@10: resp_pos += rr_dlen; meillo@10: meillo@10: return 1; meillo@10: } meillo@10: resp_pos += rr_dlen; meillo@10: return 0; meillo@0: } meillo@0: meillo@0: /* meillo@10: static int meillo@10: dns_getname(int type) meillo@0: { meillo@0: int ret; meillo@0: meillo@0: if((ret = dns_next())) return ret; meillo@0: meillo@0: if (rr_type == type){ meillo@0: if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) meillo@0: return -1; meillo@10: meillo@0: resp_pos += rr_dlen; meillo@0: meillo@0: return 1; meillo@0: } meillo@0: resp_pos += rr_dlen; meillo@0: return 0; meillo@0: } meillo@0: */ meillo@0: meillo@10: static int meillo@10: dns_look_ip(gchar * domain, guint32 * ip) meillo@0: { meillo@10: gchar *n = domain; meillo@0: meillo@10: while (TRUE) { meillo@10: if (dns_resolve(n, T_A, FALSE) == 0) { meillo@10: dns_next(); meillo@10: if (rr_type == T_A) { meillo@10: if (rr_dlen < 4) meillo@10: return -1; /* soft */ meillo@10: *ip = *(guint32 *) (resp_pos); meillo@0: meillo@10: DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip)); meillo@10: meillo@10: resp_pos += rr_dlen; meillo@10: return 0; meillo@10: } else if (rr_type == T_CNAME) { meillo@10: if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) meillo@10: return -1; meillo@10: meillo@10: DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name); meillo@10: meillo@10: resp_pos += rr_dlen; meillo@10: n = name; meillo@10: } else meillo@10: return -1; meillo@10: } else meillo@10: return -1; meillo@10: } meillo@0: } meillo@0: meillo@10: GList* meillo@10: resolve_dns_a(GList * list, gchar * domain) meillo@0: { meillo@10: int ret; meillo@0: meillo@10: DEBUG(5) debugf("DNS: resolve_dns_a entered\n"); meillo@0: meillo@10: if (dns_resolve(domain, T_A, TRUE) == 0) { meillo@10: mxip_addr mxip; meillo@10: while ((ret = dns_getip(&(mxip.ip))) != 2) { meillo@10: if (ret == 1) { meillo@10: mxip.name = g_strdup(name); meillo@10: mxip.pref = 0; meillo@10: list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); meillo@10: } meillo@10: } meillo@10: } meillo@10: return list; meillo@0: } meillo@0: meillo@10: static gint meillo@10: _mx_sort_func(gconstpointer aa, gconstpointer bb) meillo@0: { meillo@10: const mxip_addr *a = (mxip_addr *) aa; meillo@10: const mxip_addr *b = (mxip_addr *) bb; meillo@0: meillo@10: if (a->pref == b->pref) meillo@10: return a->ip - b->ip; meillo@10: else meillo@10: return a->pref - b->pref; meillo@0: } meillo@0: meillo@10: GList* meillo@10: resolve_dns_mx(GList * list, gchar * domain) meillo@0: { meillo@10: GList *node; meillo@10: int ret; meillo@10: int cnt = 0; meillo@0: meillo@10: DEBUG(5) debugf("DNS: resolve_dns_mx entered\n"); meillo@0: meillo@10: if (dns_resolve(domain, T_MX, TRUE) == 0) { meillo@10: GList *node_next; meillo@10: mxip_addr mxip; meillo@10: while ((ret = dns_getmx(&(mxip.pref))) != 2) { meillo@10: if (ret == 1) { meillo@10: mxip.name = g_strdup(name); meillo@10: mxip.ip = rand(); meillo@10: list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); meillo@10: cnt++; meillo@10: } meillo@10: } meillo@0: meillo@10: DEBUG(5) debugf("DNS: found %d mx records\n", cnt); meillo@0: meillo@10: /* to randomize sequences with equal pref values, meillo@10: we temporarily 'misused' the ip field and meillo@10: put a random number in it as a secondary sort key. meillo@10: */ meillo@10: list = g_list_sort(list, _mx_sort_func); meillo@0: meillo@10: /* CNAME resolving has to be added as well. */ meillo@0: meillo@10: for (node = g_list_first(list); node != NULL; node = node_next) { meillo@0: meillo@10: mxip_addr *p_mxip = (mxip_addr *) (node->data); meillo@10: node_next = g_list_next(node); meillo@0: meillo@10: if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) { meillo@10: DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name); meillo@10: list = g_list_remove_link(list, node); meillo@10: g_free(node->data); meillo@10: g_list_free_1(node); meillo@10: } meillo@10: } meillo@10: } meillo@10: return list; meillo@0: } meillo@0: meillo@0: #endif meillo@0: meillo@0: /* now something completely different... */ meillo@0: meillo@10: GList* meillo@10: resolve_byname(GList * list, gchar * domain) meillo@0: { meillo@10: struct hostent *hent; meillo@0: meillo@10: DEBUG(5) debugf("DNS: resolve_byname entered\n"); meillo@0: meillo@10: if ((hent = gethostbyname(domain))) { meillo@10: char *haddr; meillo@10: int i = 0; meillo@10: while ((haddr = hent->h_addr_list[i++])) { meillo@10: mxip_addr mxip; meillo@10: mxip.ip = *(guint32 *) (haddr); meillo@10: mxip.pref = 0; meillo@10: mxip.name = g_strdup(hent->h_name); meillo@10: list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); meillo@10: } meillo@10: } meillo@10: return list; meillo@0: } meillo@0: meillo@0: #ifdef RESOLV_TEST meillo@10: int meillo@10: main(int argc, char *argv[]) meillo@0: { meillo@10: GList *addr_list = NULL, *node; meillo@0: meillo@10: g_print("starting res_init()\n"); meillo@10: g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry); meillo@10: if (res_init() == 0) { meillo@10: addr_list = resolve_dns_a(NULL, argv[1]); meillo@10: g_print("A:\n"); meillo@10: foreach(addr_list, node) { meillo@10: mxip_addr *p_mxip = (mxip_addr *) (node->data); meillo@10: printf("name = %s\n IP = %s\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip))); meillo@10: } meillo@10: addr_list = resolve_dns_mx(NULL, argv[1]); meillo@10: g_print("MX:\n"); meillo@10: foreach(addr_list, node) { meillo@10: mxip_addr *p_mxip = (mxip_addr *) (node->data); meillo@15: printf("name = %s\n IP = %s pref = %d\n", p_mxip->name, meillo@15: inet_ntoa(*(struct in_addr *) &(p_mxip->ip)), p_mxip->pref); meillo@10: } meillo@10: { meillo@10: guint32 ip; meillo@10: dns_look_ip(argv[1], &ip); meillo@10: printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *) (&ip)))); meillo@10: } meillo@10: } else meillo@10: printf("res_init() failed.\n"); meillo@0: } meillo@0: #endif