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@0: * 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: meillo@0: #undef DEBUG meillo@0: #define DEBUG(x) if(x > 0) meillo@0: meillo@0: #define debugf g_print meillo@0: #endif meillo@0: meillo@0: #ifdef ENABLE_RESOLVER meillo@0: meillo@0: static union { meillo@0: HEADER hdr; meillo@0: 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@0: static meillo@0: unsigned short getshort(unsigned char *c) meillo@0: { meillo@0: unsigned short u; meillo@0: u = c[0]; meillo@0: return (u << 8) + c[1]; meillo@0: } meillo@0: meillo@0: static meillo@0: int dns_resolve(char *domain, int type, gboolean do_search) meillo@0: { meillo@0: int n; meillo@0: int i; meillo@0: meillo@0: int resp_len; meillo@0: /*errno = 0;*/ meillo@0: meillo@0: /* meillo@0: if (!stralloc_copy(&glue,domain)) return DNS_MEM; meillo@0: if (!stralloc_0(&glue)) return DNS_MEM; meillo@0: */ meillo@0: meillo@0: // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); meillo@0: DEBUG(5) debugf("DNS: before res_search()\n"); meillo@0: if(do_search) meillo@0: resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response)); meillo@0: else meillo@0: resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); meillo@0: DEBUG(5) debugf("DBG: after res_search()\n"); meillo@0: meillo@0: if (resp_len <= 0){ meillo@0: /* meillo@0: if (errno == ECONNREFUSED) return DNS_SOFT; meillo@0: if (h_errno == TRY_AGAIN) return DNS_SOFT; meillo@0: return DNS_HARD; meillo@0: */ meillo@0: return -1; meillo@0: } meillo@0: if (resp_len >= sizeof(response)) meillo@0: resp_len = sizeof(response); meillo@0: meillo@0: resp_end = response.buf + resp_len; meillo@0: resp_pos = response.buf + sizeof(HEADER); meillo@0: n = ntohs(response.hdr.qdcount); meillo@0: meillo@0: while (n-- > 0){ meillo@0: i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME); meillo@0: if (i < 0) meillo@0: return -1; meillo@0: DEBUG(5) debugf("DBG: resolve name = %s\n", name); meillo@0: resp_pos += i; meillo@0: i = resp_end - resp_pos; meillo@0: if (i < QFIXEDSZ) meillo@0: return -1; meillo@0: resp_pos += QFIXEDSZ; meillo@0: } meillo@0: num_answers = ntohs(response.hdr.ancount); meillo@0: meillo@0: return 0; meillo@0: } meillo@0: meillo@0: static int dns_next() meillo@0: { meillo@0: int i; meillo@0: meillo@0: if (num_answers <= 0) return 2; meillo@0: num_answers--; meillo@0: meillo@0: if (resp_pos == resp_end) meillo@0: return -1 /* soft */; meillo@0: meillo@0: i = dn_expand(response.buf, resp_end, resp_pos, name, 256); meillo@0: if (i < 0) meillo@0: return -1; /* soft */ meillo@0: resp_pos += i; meillo@0: meillo@0: i = resp_end - resp_pos; meillo@0: if (i < 4 + 3 * 2) meillo@0: return -1; /* soft */ meillo@0: meillo@0: rr_type = getshort(resp_pos); meillo@0: rr_dlen = getshort(resp_pos + 8); meillo@0: resp_pos += 10; meillo@0: meillo@0: return 0; meillo@0: } meillo@0: meillo@0: static meillo@0: int dns_getip(guint32 *ip) meillo@0: { meillo@0: int ret; meillo@0: meillo@0: if((ret = dns_next())) return ret; meillo@0: meillo@0: if (rr_type == T_A){ meillo@0: if (rr_dlen < 4) meillo@0: return -1; /* soft */ meillo@0: *ip = *(guint32 *)(resp_pos); meillo@0: DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr*)ip)); 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: static meillo@0: int dns_getmx(int *pref) meillo@0: { meillo@0: int ret; meillo@0: meillo@0: if((ret = dns_next())) return ret; meillo@0: meillo@0: if (rr_type == T_MX){ meillo@0: if (rr_dlen < 3) meillo@0: return -1; /* soft */ meillo@0: meillo@0: *pref = (resp_pos[0] << 8) + resp_pos[1]; meillo@0: if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0) meillo@0: return -1; meillo@0: 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@0: static meillo@0: int 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@0: 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@0: static meillo@0: int dns_look_ip(gchar *domain, guint32 *ip) meillo@0: { meillo@0: gchar *n = domain; meillo@0: meillo@0: while(TRUE){ meillo@0: if(dns_resolve(n, T_A, FALSE) == 0){ meillo@0: dns_next(); meillo@0: if(rr_type == T_A){ meillo@0: if (rr_dlen < 4) meillo@0: return -1; /* soft */ meillo@0: *ip = *(guint32 *)(resp_pos); meillo@0: meillo@0: DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", meillo@0: inet_ntoa(*(struct in_addr*)ip)); meillo@0: meillo@0: resp_pos += rr_dlen; meillo@0: return 0; meillo@0: }else if(rr_type == T_CNAME){ meillo@0: if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) meillo@0: return -1; meillo@0: meillo@0: DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name); meillo@0: meillo@0: resp_pos += rr_dlen; meillo@0: n = name; meillo@0: }else meillo@0: return -1; meillo@0: }else meillo@0: return -1; meillo@0: } meillo@0: } meillo@0: meillo@0: GList *resolve_dns_a(GList *list, gchar *domain) meillo@0: { meillo@0: int ret; meillo@0: meillo@0: DEBUG(5) debugf("DNS: resolve_dns_a entered\n"); meillo@0: meillo@0: if(dns_resolve(domain, T_A, TRUE) == 0){ meillo@0: mxip_addr mxip; meillo@0: while((ret = dns_getip(&(mxip.ip))) != 2){ meillo@0: if(ret == 1){ meillo@0: mxip.name = g_strdup(name); meillo@0: mxip.pref = 0; meillo@0: list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); meillo@0: } meillo@0: } meillo@0: } meillo@0: return list; meillo@0: } meillo@0: meillo@0: static meillo@0: gint _mx_sort_func(gconstpointer aa, gconstpointer bb) meillo@0: { meillo@0: const mxip_addr *a = (mxip_addr *)aa; meillo@0: const mxip_addr *b = (mxip_addr *)bb; meillo@0: meillo@0: if(a->pref == b->pref) meillo@0: return a->ip - b->ip; meillo@0: else meillo@0: return a->pref - b->pref; meillo@0: } meillo@0: meillo@0: GList *resolve_dns_mx(GList *list, gchar *domain) meillo@0: { meillo@0: GList *node; meillo@0: int ret; meillo@0: int cnt = 0; meillo@0: meillo@0: DEBUG(5) debugf("DNS: resolve_dns_mx entered\n"); meillo@0: meillo@0: if(dns_resolve(domain, T_MX, TRUE) == 0){ meillo@0: GList *node_next; meillo@0: mxip_addr mxip; meillo@0: while((ret = dns_getmx(&(mxip.pref))) != 2){ meillo@0: if(ret == 1){ meillo@0: mxip.name = g_strdup(name); meillo@0: mxip.ip = rand(); meillo@0: list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); meillo@0: cnt++; meillo@0: } meillo@0: } meillo@0: meillo@0: DEBUG(5) debugf("DNS: found %d mx records\n", cnt); meillo@0: meillo@0: /* to randomize sequences with equal pref values, meillo@0: we temporarily 'misused' the ip field and meillo@0: put a random number in it as a secondary sort key. meillo@0: */ meillo@0: list = g_list_sort(list, _mx_sort_func); meillo@0: meillo@0: /* CNAME resolving has to be added as well. */ meillo@0: meillo@0: for(node = g_list_first(list); meillo@0: node != NULL; meillo@0: node = node_next){ meillo@0: meillo@0: mxip_addr *p_mxip = (mxip_addr *)(node->data); meillo@0: node_next = g_list_next(node); meillo@0: meillo@0: if(dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0){ meillo@0: DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name); meillo@0: list = g_list_remove_link(list, node); meillo@0: g_free(node->data); meillo@0: g_list_free_1(node); meillo@0: } meillo@0: } meillo@0: } meillo@0: return list; meillo@0: } meillo@0: meillo@0: #endif meillo@0: meillo@0: /* now something completely different... */ meillo@0: meillo@0: GList *resolve_byname(GList *list, gchar *domain) meillo@0: { meillo@0: struct hostent *hent; meillo@0: meillo@0: DEBUG(5) debugf("DNS: resolve_byname entered\n"); meillo@0: meillo@0: if((hent = gethostbyname(domain))){ meillo@0: char *haddr; meillo@0: int i = 0; meillo@0: while((haddr = hent->h_addr_list[i++])){ meillo@0: mxip_addr mxip; meillo@0: mxip.ip = *(guint32 *)(haddr); meillo@0: mxip.pref = 0; meillo@0: mxip.name = g_strdup(hent->h_name); meillo@0: list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); meillo@0: } meillo@0: } meillo@0: return list; meillo@0: } meillo@0: meillo@0: #ifdef RESOLV_TEST meillo@0: int main(int argc, char *argv[]) meillo@0: { meillo@0: GList *addr_list = NULL, *node; meillo@0: meillo@0: g_print("starting res_init()\n"); meillo@0: meillo@0: g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry); meillo@0: meillo@0: if(res_init() == 0){ meillo@0: meillo@0: addr_list = resolve_dns_a(NULL, argv[1]); meillo@0: g_print("A:\n"); meillo@0: meillo@0: foreach(addr_list, node){ meillo@0: mxip_addr *p_mxip = (mxip_addr *)(node->data); meillo@0: meillo@0: printf("name = %s\n IP = %s\n", meillo@0: p_mxip->name, meillo@0: inet_ntoa(*(struct in_addr *)&(p_mxip->ip))); meillo@0: } meillo@0: addr_list = resolve_dns_mx(NULL, argv[1]); meillo@0: g_print("MX:\n"); meillo@0: meillo@0: foreach(addr_list, node){ meillo@0: mxip_addr *p_mxip = (mxip_addr *)(node->data); meillo@0: meillo@0: printf("name = %s\n IP = %s pref = %d\n", meillo@0: p_mxip->name, meillo@0: inet_ntoa(*(struct in_addr *)&(p_mxip->ip)), meillo@0: p_mxip->pref); meillo@0: } meillo@0: { meillo@0: guint32 ip; meillo@0: dns_look_ip(argv[1], &ip); meillo@0: printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *)(&ip)))); meillo@0: } meillo@0: }else meillo@0: printf("res_init() failed.\n"); meillo@0: meillo@0: } meillo@0: #endif