aboutsummaryrefslogtreecommitdiff
path: root/src/bi_dns.m4
blob: 96f9d36e1587bb03e7b810c9a506ca25861772f8 (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
/* This file is part of mailfrom filter.             -*- c -*-
   Copyright (C) 2006 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 2, 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, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

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

MF_DEFUN(hostname, STRING, STRING string)
{
	char *hbuf;
	if (resolve_ipstr(string, &hbuf) != mf_success) 
		pushs(env, string);
	else {
		pushs(env, hbuf);
		free(hbuf);
	}
}
END

MF_DEFUN(resolve, STRING, STRING string)
{
	char *ipstr;
	if (resolve_hostname(string, &ipstr) != mf_success)
		pushs(env, "0");
	else {
		pushs(env, ipstr);
		free(ipstr);
	}
}
END

MF_DEFUN(hasmx, NUMBER, STRING string)
{
	mxbuf_t mxbuf;
	mf_status mxstat;

	mxstat = getmx(string, mxbuf);
	
	if (mxstat != mf_success) {
		if (env_catch(env, mxstat) == 0)
			return;
	} else
		freemx(mxbuf);

	MF_RETURN(mxstat == mf_success);
}
END

MF_DEFUN(getmx, STRING, STRING domain, OPTIONAL, NUMBER resolve)
{
	mxbuf_t mxbuf;
	mf_status mxstat;
	int i;

	if (resolve)
		mxstat = getmxip(domain, mxbuf);
	else
		mxstat = getmx(domain, mxbuf);
	MF_ASSERT(mxstat == mf_success || mxstat == mf_not_found,
		  mxstat,
		  "Cannot get MX records for %s", domain);
	if (mxstat == mf_not_found)
		MF_RETURN_STRING("");
	else {
		int i;
		size_t ns;
		char *p;
		
		MF_BEGIN_TEMP_SPACE(s, size);
		
		for (i = 0, ns = 0, p = s; i < MAXMXCOUNT && mxbuf[i]; i++) {
			size_t len = strlen(mxbuf[i]);
			if (ns + len + 1 > size)
				break;
			if (i > 0)
				*p++ = ' ';
			memcpy(p, mxbuf[i], len);
			p += len;
			ns += len + 1;
		}
		*p = 0;
		freemx(mxbuf);
		MF_RETURN_TEMP_SPACE(ns);
	}
}
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:%lu: resolve_hostname returned "
			 "invalid IP address: %s",
			 __FILE__, __FILE__, ipstr);
		return 1;
	}
	*ip = addr.s_addr;
	return 0;
}

MF_DEFUN(ismx, NUMBER, STRING domain, STRING ipstr)
{
	mxbuf_t mxbuf;
	mf_status mxstat;
	unsigned long ip;
	int rc = 0;
	
	MF_ASSERT(resolve_host(ipstr, &ip) == 0, mf_resolve, 
                 "cannot resolve host name %s", ipstr);
	
	mxstat = getmx(domain, mxbuf);
	
	if (mxstat != mf_success) {
		if (env_catch(env, mxstat) == 0)
			return;
	} else {
		int i;

		for (i = 0; i < MAXMXCOUNT && mxbuf[i]; i++) {
			unsigned long n;
			
			if (!resolve_host(mxbuf[i], &n) && n == ip) {
				rc = 1;
				break;
			}
		}
		freemx(mxbuf);
	}
	MF_RETURN(rc);
}
END

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

MF_DEFUN(listens, NUMBER, STRING s)
{
	MF_RETURN(listens_on (s, 25) == mf_success);
}
END

MF_DEFUN(match_cidr, NUMBER, STRING ipstr, STRING cidrstr)
{
	char *p;
	struct in_addr addr, cidr;
	char netbuf[16], *netstr;
	unsigned long netmask;
	int result = 0;
	const struct locus *locus = env_get_locus(env);
	
	do {
		MF_ASSERT(inet_aton(ipstr, &addr),
		         mf_invip,
			 "invalid IP address (%s)",
			 ipstr);
		p = strchr(cidrstr, '/');
		if (!p) {
			netstr = cidrstr;
			netmask = 0xfffffffful;
		} else {
			size_t len = p - cidrstr;
			unsigned long n;
			
			MF_ASSERT(len < sizeof netbuf,
				 mf_invcidr,
				 "invalid CIDR (%s)", cidrstr);
			memcpy(netbuf, cidrstr, len);
			netbuf[len] = 0;
			netstr = netbuf;
			n = strtoul(p + 1, &p, 10);
			MF_ASSERT(*p == 0 && n <= 32, mf_invcidr,
			         "invalid CIDR (netmask of %s)", cidrstr);
			n = 32 - n;
			if (n == 32)
				netmask = 0;
			else
				netmask = (0xfffffffful >> n) << n;
		}
		MF_ASSERT(inet_aton(netstr, &cidr), 
			 mf_invcidr,
			 "invalid CIDR (network part of %s)", cidrstr);

		addr.s_addr = ntohl(addr.s_addr);
		cidr.s_addr = ntohl(cidr.s_addr);
		debug5(100, "%s:%lu: addr=%lx net=%lx mask=%lx" ,
		       locus->file, locus->line,
		       addr.s_addr, cidr.s_addr, netmask);
		result = (addr.s_addr & netmask) == cidr.s_addr;
	} while (0);
	MF_RETURN(result);
}
END

MF_DEFUN(domainpart, STRING, STRING str)
{
	char *p = strchr(str, '@');
	MF_RETURN(p ? p+1 : str);
}
END

MF_DEFUN(localpart, STRING, STRING str)
{
	char *p = strchr(str, '@');

	if (p) {
		size_t size = p - str;
		char *string_space = MF_ALLOC_HEAP(size + 1);
		memcpy(string_space, str, size);
		string_space[size] = 0;
		MF_RETURN(string_space);
	} else
		MF_RETURN_STRING(str);
}
END

MF_INIT

Return to:

Send suggestions and report system problems to the System administrator.