aboutsummaryrefslogtreecommitdiff
path: root/src/dns.c
blob: 3bebe6a238ec88f5ea87c0046532e8748b9ee923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* This file is part of mailfromd.
   Copyright (C) 2005, 2006, 2007 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#define MF_SOURCE_NAME  MF_SOURCE_DNS

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/types.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <resolv.h>

#include "mailfromd.h"

mf_status
dns_to_mf_status(dns_status stat)
{
	return (mf_status) stat;
}

mf_status
getmx(char *host, mxbuf_t mxbuf)
{
	mf_status rc = dns_cache_get(T_MX, host, mxbuf, MAXMXCOUNT);
	if (!mf_resolved(rc)) {
		dns_status status;
		unsigned long ttl;
		size_t mxcnt;
		
		status = dns_get_mx_records(host, 1, mxbuf, &mxcnt, &ttl);
		if (status == dns_success) 
			dns_cache_put(T_MX, host, ttl, mxbuf, mxcnt);
		else
			dns_cache_put(T_MX, host, 0, NULL, 0);
		rc = dns_to_mf_status(status);
	}
	return rc;
}

static int
comp_ipbuf(const void *a, const void *b)
{
	const GACOPYZ_UINT32_T *in_a = a;
	const GACOPYZ_UINT32_T *in_b = b;

	if (*in_a < *in_b)
		return -1;
	else if (*in_a > *in_b)
		return 1;
	return 0;
}

mf_status
getmxip(char *host, mxbuf_t mxbuf)
{
	mf_status mxstat;
	GACOPYZ_UINT32_T ipbuf[MAXMXCOUNT];
	size_t ipcount;
	size_t i;

	mxstat = getmx(host, mxbuf);
	if (mxstat != mf_success)
		return mxstat;

	ipcount = 0;
	for (i = 0; i < MAXMXCOUNT && mxbuf[i]; i++) {
		size_t ipn;
		mf_status stat = a_lookup(mxbuf[i],
					  ipbuf + ipcount,
					  NELEMS(ipbuf) - ipcount,
					  &ipn,
					  NULL, NULL, 0);
		if (stat == mf_success) {
			size_t n;
			for (n = 0; ipcount < NELEMS(ipbuf) && n < ipn;
			     n++, ipcount++) 
				ipbuf[ipcount] = ntohl(ipbuf[ipcount]);
			if (ipcount == NELEMS(ipbuf))
				break;
		}
	}
	
	dns_freemx(mxbuf);

	qsort(ipbuf, ipcount, sizeof ipbuf[0], comp_ipbuf);
	
	for (i = 0; i < ipcount; i++) {
		struct in_addr s;
		s.s_addr = htonl(ipbuf[i]);
		mxbuf[i] = xstrdup(inet_ntoa(s));
	}
	
	if (i < MAXMXCOUNT)
		mxbuf[i] = NULL;
	return mf_success;
}

mf_status
resolve_ipstr_domain(const char *ipstr, const char *domain, char **phbuf)
{
	mf_status status;
	static unsigned char *answer;
	char *hbuf;
	
	if (!answer)
		answer = xmalloc(MAXPACKET);
	debug1(80,"Getting canonical name for %s", ipstr);

	status = dns_cache_get(T_PTR, ipstr, &hbuf, 1);
	if (status == mf_success) {
		debug2(80, "%s resolved to %s (cached value)", ipstr, hbuf);
		*phbuf = hbuf;
	} else if (status == mf_not_found) {
		debug1(80, "%s not resolved", ipstr);
	} else {
		unsigned long ttl;
		char buffer[256];
		dns_status dstat;
		
		dstat = dns_resolve_ipstr(ipstr, domain, answer, MAXPACKET,
					  buffer, sizeof buffer, &ttl);

		switch (dstat) {
		case dns_success:
			debug2(80, "%s resolved to %s", ipstr, buffer);
			*phbuf = xstrdup(buffer);
			dns_cache_put(T_PTR, ipstr, ttl, phbuf, 1);
			break;

		case dns_not_found:
			dns_cache_put(T_PTR, ipstr, 0, NULL, 0);
		default:
			debug1(80, "%s not resolved", ipstr);
		}
		status = dns_to_mf_status(dstat);
	}
	return status;
}

mf_status
resolve_ipstr(const char *ipstr, char **phbuf)	
{
	return resolve_ipstr_domain(ipstr, NULL, phbuf);
}

mf_status
resolve_hostname(const char *host, char **pipbuf)	
{
	mf_status status;
	static unsigned char *answer;
	char buffer[256];
	char *ipbuf;
	
	if (!answer)
		answer = malloc(MAXPACKET);
	debug1(80,"Getting IP address for %s", host);
	status = dns_cache_get(T_A, host, &ipbuf, 1);
	if (status == mf_success) {
		debug2(80, "%s resolved to %s (cached value)", host, ipbuf);
		*pipbuf = ipbuf;
	} else if (status == mf_not_found) {
		debug1(80, "%s not resolved (cached value)", host);
	} else {
		unsigned long ttl;
		char *tmphost = xstrdup(host);
		dns_status dstat;
		
		dstat = dns_resolve_hostname(tmphost, answer, MAXPACKET,
					     buffer, sizeof buffer,
					     &ttl);
		free(tmphost);
		switch (dstat) {
		case dns_success:
			ipbuf = xstrdup(buffer);
			debug2(80, "%s resolved to %s", host, ipbuf);
			*pipbuf = ipbuf;
			dns_cache_put(T_A, host, ttl, &ipbuf, 1);
			break;
			
		case dns_not_found:
			dns_cache_put(T_A, host, 0, NULL, 0);
		default:
			debug1(80, "%s not resolved", host);
		}
		status = dns_to_mf_status(dstat);
	}
	return status;
}

Return to:

Send suggestions and report system problems to the System administrator.