aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/dns.bi
blob: 46433a00ce804b8add12e143be07590544f1bc7e (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2006-2011, 2015-2017 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/>. */

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "srvcfg.h"
#include "global.h"

static size_t max_ptr = MAX_DNS_PTR;
static size_t max_a = MAX_DNS_A;
static size_t max_mx = MAX_DNS_MX;
static struct mu_cfg_param dns_cfg_param[] = {
	{ "max-dns-reply-a", mu_c_size, &max_a, 0, NULL,
	  N_("Maximum number of A records in a DNS reply.") },
	{ "max-dns-reply-ptr", mu_c_size, &max_a, 0, NULL,
	  N_("Maximum number of PTR records in a DNS reply.") },
	{ "max-dns-reply-mx", mu_c_size, &max_mx, 0, NULL,
	  N_("Maximum number of MX records in a DNS reply.") },
	{ NULL }
};


MF_DEFUN(primitive_hostname, STRING, STRING string)
{
	char *hbuf;
	mf_status stat;

	stat = resolve_ipstr(string, &hbuf);
	MF_ASSERT(stat == mf_success,
		  mf_status_to_exception(stat),
		  _("cannot resolve IP %s"),
		  string);
	
	pushs(env, hbuf);
	free(hbuf);
}
END

MF_DEFUN(primitive_resolve, STRING, STRING string, OPTIONAL, STRING domain)
{
	char *ipstr;
	mf_status stat;
	
	if (MF_OPTVAL(domain,"")[0]) {
		stat = resolve_ipstr_domain(string, domain, &ipstr);
		MF_ASSERT(stat == mf_success,
			  mf_status_to_exception(stat),
			  _("cannot resolve %s.%s"), string, domain);
	} else {
		stat = resolve_hostname(string, &ipstr);
		MF_ASSERT(stat == mf_success,
			  mf_status_to_exception(stat),
			  _("cannot resolve %s"), string);
	}
	pushs(env, ipstr);
	free(ipstr);
}
END

static int
ipaddr_cmp(const void *a, const void *b)
{
	GACOPYZ_UINT32_T ipa = ntohl(*(GACOPYZ_UINT32_T*)a);
	GACOPYZ_UINT32_T ipb = ntohl(*(GACOPYZ_UINT32_T*)b);
	if (ipa < ipb)
		return -1;
	if (ipa > ipb)
		return 1;
	return 0;
}

MF_DEFUN(dns_getaddr, STRING, STRING string)
{
	GACOPYZ_UINT32_T *ipbuf;
	size_t i, ipcount;
	unsigned long ttl;
	dns_status dnstat;

	ipbuf = mu_calloc(max_a, sizeof(ipbuf[0]));
	dnstat = a_lookup(string, ipbuf, max_a, &ipcount,
			  &ttl, NULL, 0);
	switch (dnstat) {
	case dns_success: {
		MF_OBSTACK_BEGIN();
		qsort(ipbuf, ipcount, sizeof ipbuf[0], ipaddr_cmp);
		for (i = 0; i < ipcount; i++) {
			struct in_addr addr;
			char *q;
			
			addr.s_addr = ipbuf[i];
			q = inet_ntoa(addr);
			if (i > 0)
				MF_OBSTACK_1GROW(' ');
 			MF_OBSTACK_GROW(q);
		}
		free(ipbuf);
		MF_OBSTACK_1GROW(0);
		MF_RETURN_OBSTACK();
	}
	case dns_not_found:
		free(ipbuf);
		MF_RETURN("");
	default:
		free(ipbuf);
		MF_THROW(mf_status_to_exception(dns_to_mf_status(dnstat)),
			 _("failed to get A record for %s"), string);
	}
}
END

static int
hostname_cmp(const void *a, const void *b)
{
	return strcmp(*(const char**) a, *(const char**) b);
}

MF_DEFUN(dns_getname, STRING, STRING ipstr)
{
	dns_status dnstat;
	struct in_addr addr;
	unsigned long ttl;
	char **names;

	MF_ASSERT(inet_aton(ipstr, &addr),
		  mfe_invip,
		  _("invalid IP: %s"), ipstr);

	names = mu_calloc(max_ptr, sizeof(names[0]));
	dnstat = ptr_lookup(addr, names, max_ptr, &ttl, NULL, 0);
	switch (dnstat) {
	case dns_success: {
		size_t i;
		size_t ncount;

		for (ncount = 0; ncount < max_ptr && names[ncount];
		     ncount++);
		
		qsort(names, ncount, sizeof names[0], hostname_cmp);

		MF_OBSTACK_BEGIN();
		for (i = 0; i < ncount; i++) {
			if (i > 0)
				MF_OBSTACK_1GROW(' ');
			MF_OBSTACK_GROW(names[i]);
		}
		MF_OBSTACK_1GROW(0);

		for (; i < ncount; i++) 
			free(names[i]);
		free(names);
		MF_RETURN_OBSTACK();
	}
	case dns_not_found:
		free(names);
		MF_RETURN("");
	default:
		free(names);
		MF_THROW(mf_status_to_exception(dns_to_mf_status(dnstat)),
			 _("failed to get PTR record for %s"), ipstr);
	}
}
END

MF_DEFUN(primitive_hasmx, NUMBER, STRING string)
{
	struct mxbuf mxbuf;
	mf_status mxstat;

	mxbuf.mx_flags = 0;
	mxstat = getmx(string, &mxbuf);
	
	MF_ASSERT(mxstat == mf_success || mxstat == mf_not_found,
		  mf_status_to_exception(mxstat),
		  _("cannot get MX records for %s"),
		  string);
	mxbuf_free(&mxbuf);
	if (mxstat == mf_success) {
		MF_RETURN(1);
	}
	MF_RETURN(0);
}
END

MF_DEFUN(getmx, STRING, STRING domain, OPTIONAL, NUMBER no_resolve)
{
	mf_status mxstat;

	if (MF_OPTVAL(no_resolve)) {
		GACOPYZ_UINT32_T *ipbuf;
		size_t ipcount;

		ipbuf = mu_calloc(max_mx, sizeof(ipbuf[0]));
		mxstat = getmxip(domain, ipbuf, max_mx, &ipcount);
		if (!mf_resolved(mxstat)) {
			free(ipbuf);
			MF_THROW(mf_status_to_exception(mxstat),
				 _("cannot get MX records for %s"), domain);
		}
		if (mxstat == mf_not_found) {
			free(ipbuf);
			MF_RETURN("");
		} else {
			int i;
			
			MF_OBSTACK_BEGIN();
			for (i = 0; i < ipcount; i++) {
				struct in_addr s;
				s.s_addr = htonl(ipbuf[i]);
				
				if (i > 0)
					MF_OBSTACK_1GROW(' ');
				MF_OBSTACK_GROW(inet_ntoa(s));
			}
			free(ipbuf);
			MF_OBSTACK_1GROW(0);
			MF_RETURN_OBSTACK();
		}
	} else {
		struct mxbuf mxbuf;

		mxbuf.mx_max = max_mx;
		mxbuf.mx_flags = MXF_MAX;
		mxstat = getmx(domain, &mxbuf);
		if (!mf_resolved(mxstat)) {
			mxbuf_free(&mxbuf);
			MF_THROW(mf_status_to_exception(mxstat),
				 _("cannot get MX records for %s"), domain);
		}
		if (mxstat == mf_not_found) {
			mxbuf_free(&mxbuf);
			MF_RETURN("");
		} else {
			int i;
		
			MF_OBSTACK_BEGIN();
			for (i = 0; i < mxbuf.mx_cnt; i++) {
				if (i > 0)
					MF_OBSTACK_1GROW(' ');
				MF_OBSTACK_GROW(mxbuf.mx_buf[i]);
			}
			MF_OBSTACK_1GROW(0);
			mxbuf_free(&mxbuf);
			MF_RETURN_OBSTACK();
		}
	}
}
END

static int
resolve_host(const char *string, unsigned long *ip)
{
	int rc;
	struct in_addr addr;
	char *ipstr;

	if (inet_aton(string, &addr)) {
		*ip = addr.s_addr;
		return 0;
	}
	
	if (resolve_hostname(string, &ipstr) != mf_success)
		return 1;

	rc = inet_aton(ipstr, &addr);
	free(ipstr);
	if (rc == 0) {
		mu_error(_("INTERNAL ERROR at %s:%d: resolve_hostname returned "
			   "invalid IP address: %s"),
			 __FILE__, __LINE__, ipstr);
		return 1;
	}
	*ip = addr.s_addr;
	return 0;
}

MF_DEFUN(primitive_ismx, NUMBER, STRING domain, STRING ipstr)
{
	GACOPYZ_UINT32_T *ipbuf;
	size_t ipcount;
	mf_status mxstat;
	unsigned long ip;
	int rc = 0;
	int i;
	
	MF_ASSERT(resolve_host(ipstr, &ip) == 0, mfe_noresolve, 
                  _("cannot resolve host name %s"), ipstr);
	ip = ntohl(ip);

	ipbuf = mu_calloc(max_mx, sizeof(ipbuf[0]));
	mxstat = getmxip(domain, ipbuf, max_mx, &ipcount);

	if (mxstat != mf_success) {
		free(ipbuf);
		MF_THROW(mf_status_to_exception(mxstat),
			 _("cannot get MXs for %s"), domain);
	}

	for (i = 0; i < ipcount; i++) {
		if (ipbuf[i] == ip) {
			rc = 1;
			break;
		}
	}
		
	free(ipbuf);
	MF_RETURN(rc);
}
END

MF_DEFUN(relayed, NUMBER, STRING s)
{
	MF_RETURN(relayed_domain_p(s));
}
END

MF_INIT([<
	 mf_add_runtime_params(dns_cfg_param);
	 >])

Return to:

Send suggestions and report system problems to the System administrator.