Project

General

Profile

Download (11.4 KB) Statistics
| Branch: | Tag: | Revision:
1 c0b6fdde jim-p
<?php
2
/*
3
	easyrule.inc.php
4
5 998f77a8 jim-p
	Copyright (C) 2009-2010 Jim Pingle (jpingle@gmail.com)
6
	Originally Sponsored By Anathematic @ pfSense Forums
7 c0b6fdde jim-p
	All rights reserved.
8
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11
12
	1. Redistributions of source code must retain the above copyright notice,
13
	this list of conditions and the following disclaimer.
14
15
	2. Redistributions in binary form must reproduce the above copyright
16
	notice, this list of conditions and the following disclaimer in the
17
	documentation and/or other materials provided with the distribution.
18
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
*/
30 7ac5a4cb Scott Ullrich
/*
31 dadad8b3 jim-p
	pfSense_BUILDER_BINARIES:
32 7ac5a4cb Scott Ullrich
	pfSense_MODULE:	filter
33
*/
34 c0b6fdde jim-p
35
$blockaliasname = 'EasyRuleBlockHosts';
36 865ff9b4 jim-p
$protocols_with_ports = array('tcp', 'udp');
37
require_once("functions.inc");
38
require_once("util.inc");
39
require_once("config.inc");
40 4d828a9a Ermal Lu?i
41 c0b6fdde jim-p
function easyrule_find_rule_interface($int) {
42
	global $config;
43
	/* Borrowed from firewall_rules.php */
44 4d828a9a Ermal Lu?i
	$iflist = get_configured_interface_with_descr(false, true);
45 dadad8b3 jim-p
46 c0b6fdde jim-p
	if ($config['pptpd']['mode'] == "server")
47
		$iflist['pptp'] = "PPTP VPN";
48 dadad8b3 jim-p
49 bd40781a Seth Mos
	if ($config['pppoe']['mode'] == "server")
50 c0b6fdde jim-p
		$iflist['pppoe'] = "PPPoE VPN";
51 dadad8b3 jim-p
52 4d828a9a Ermal Lu?i
	if ($config['l2tp']['mode'] == "server")
53
                $iflist['l2tp'] = "L2TP VPN";
54
55 c0b6fdde jim-p
	/* add ipsec interfaces */
56 c6dfd289 jim-p
	if (isset($config['ipsec']['enable']) || isset($config['ipsec']['client']['enable'])){
57 c0b6fdde jim-p
		$iflist["enc0"] = "IPSEC";
58
	}
59 dadad8b3 jim-p
60 c0b6fdde jim-p
	if (isset($iflist[$int]))
61
		return $int;
62
63
	foreach ($iflist as $if => $ifd) {
64
		if (strtolower($int) == strtolower($ifd))
65
			return $if;
66
	}
67 dadad8b3 jim-p
68 066afaf1 jim-p
	if (substr($int, 0, 4) == "ovpn")
69
		return "openvpn";
70
71 c0b6fdde jim-p
	return false;
72
}
73
74 4475997e jim-p
function easyrule_block_rule_exists($int = 'wan', $ipproto = "inet") {
75 c0b6fdde jim-p
	global $blockaliasname, $config;
76
	/* No rules, we we know it doesn't exist */
77
	if (!is_array($config['filter']['rule'])) {
78
		return false;
79
	}
80
81
	/* Search through the rules for one referencing our alias */
82 28a581b8 jim-p
	foreach ($config['filter']['rule'] as $rule) {
83 f3704cb2 jim-p
		if (!is_array($rule) || !is_array($rule['source']))
84
			continue;
85 4475997e jim-p
		$checkproto = isset($rule['ipprotocol']) ? $rule['ipprotocol'] : "inet";
86
		if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int) && ($checkproto == $ipproto))
87 c0b6fdde jim-p
			return true;
88 28a581b8 jim-p
	}
89 c0b6fdde jim-p
	return false;
90
}
91
92 64eda26c jim-p
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
93 c0b6fdde jim-p
	global $blockaliasname, $config;
94
	/* If the alias doesn't exist, exit.
95
	 * Can't create an empty alias, and we don't know a host */
96
	if (easyrule_block_alias_getid($int) === false)
97
		return false;
98
99
	/* If the rule already exists, no need to do it again */
100 64eda26c jim-p
	if (easyrule_block_rule_exists($int, $ipproto))
101 c0b6fdde jim-p
		return true;
102
103
	/* No rules, start a new array */
104
	if (!is_array($config['filter']['rule'])) {
105
		$config['filter']['rule'] = array();
106
	}
107
108
	filter_rules_sort();
109
	$a_filter = &$config['filter']['rule'];
110
111
	/* Make up a new rule */
112
	$filterent = array();
113
	$filterent['type'] = 'block';
114 dadad8b3 jim-p
	$filterent['interface'] = $int;
115 64eda26c jim-p
	$filterent['ipprotocol'] = $ipproto;
116 c0b6fdde jim-p
	$filterent['source']['address'] = $blockaliasname . strtoupper($int);
117
	$filterent['destination']['any'] = '';
118 5bd033a0 Renato Botelho
	$filterent['descr'] = gettext("Easy Rule: Blocked from Firewall Log View");
119 ba1d9714 jim-p
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
120 c0b6fdde jim-p
121 a0140246 jim-p
	array_splice($a_filter, 0, 0, array($filterent));
122 c0b6fdde jim-p
123
	return true;
124
}
125
126
function easyrule_block_alias_getid($int = 'wan') {
127
	global $blockaliasname, $config;
128
	if (!is_array($config['aliases']))
129
		return false;
130
131
	/* Hunt down an alias with the name we want, return its id */
132
	foreach ($config['aliases']['alias'] as $aliasid => $alias)
133
		if ($alias['name'] == $blockaliasname . strtoupper($int))
134
			return $aliasid;
135
136
	return false;
137
}
138
139
function easyrule_block_alias_add($host, $int = 'wan') {
140
	global $blockaliasname, $config;
141
	/* If the host isn't a valid IP address, bail */
142 b4147482 jim-p
	$host = trim($host, "[]");
143 0c305760 jim-p
	if (!is_ipaddr($host) && !is_subnet($host))
144 c0b6fdde jim-p
		return false;
145
146
	/* If there are no aliases, start an array */
147
	if (!is_array($config['aliases']['alias']))
148
		$config['aliases']['alias'] = array();
149
150
	$a_aliases = &$config['aliases']['alias'];
151
152
	/* Try to get the ID if the alias already exists */
153
	$id = easyrule_block_alias_getid($int);
154
	if ($id === false)
155
	  unset($id);
156
157
	$alias = array();
158
159 0c305760 jim-p
	if (is_subnet($host)) {
160
		list($host, $mask) = explode("/", $host);
161
	} elseif (is_specialnet($host)) {
162
		$mask = 0;
163 b4147482 jim-p
	} elseif (is_ipaddrv6($host)) {
164
		$mask = 128;
165 0c305760 jim-p
	} else {
166
		$mask = 32;
167
	}
168
169 c0b6fdde jim-p
	if (isset($id) && $a_aliases[$id]) {
170
		/* Make sure this IP isn't already in the list. */
171 0c305760 jim-p
		if (in_array($host.'/'.$mask, explode(" ", $a_aliases[$id]['address'])))
172 c0b6fdde jim-p
			return true;
173
		/* Since the alias already exists, just add to it. */
174
		$alias['name']    = $a_aliases[$id]['name'];
175
		$alias['type']    = $a_aliases[$id]['type'];
176
		$alias['descr']   = $a_aliases[$id]['descr'];
177
178 0c305760 jim-p
		$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/' . $mask;
179 5bd033a0 Renato Botelho
		$alias['detail']  = $a_aliases[$id]['detail'] . gettext('Entry added') . ' ' . date('r') . '||';
180 c0b6fdde jim-p
	} else {
181
		/* Create a new alias with all the proper information */
182
	 	$alias['name']    = $blockaliasname . strtoupper($int);
183
	 	$alias['type']    = 'network';
184 9d3d8d00 Vinicius Coque
		$alias['descr']   = gettext("Hosts blocked from Firewall Log view");
185 c0b6fdde jim-p
186 0c305760 jim-p
		$alias['address'] = $host . '/' . $mask;
187 5bd033a0 Renato Botelho
		$alias['detail']  = gettext('Entry added') . ' ' . date('r') . '||';
188 c0b6fdde jim-p
	}
189
190
	/* Replace the old alias if needed, otherwise tack it on the end */
191
	if (isset($id) && $a_aliases[$id])
192
		$a_aliases[$id] = $alias;
193
	else
194
		$a_aliases[] = $alias;
195 9bb8d542 Ermal Lu?i
196
	// Sort list
197
	$a_aliases = msort($a_aliases, "name");
198 c0b6fdde jim-p
199
	return true;
200
}
201
202 64eda26c jim-p
function easyrule_block_host_add($host, $int = 'wan', $ipproto = "inet") {
203 c0b6fdde jim-p
	global $retval;
204
	/* Bail if the supplied host is not a valid IP address */
205 b4147482 jim-p
	$host = trim($host, "[]");
206 0c305760 jim-p
	if (!is_ipaddr($host) && !is_subnet($host))
207 c0b6fdde jim-p
		return false;
208
209
	/* Flag whether or not we need to reload the filter */
210
	$dirty = false;
211
212
	/* Attempt to add this host to the alias */
213
	if (easyrule_block_alias_add($host, $int)) {
214
		$dirty = true;
215
	} else {
216
		/* Couldn't add the alias, or adding the host failed. */
217
		return false;
218
	}
219
220
	/* Attempt to add the firewall rule if it doesn't exist.
221
	 * Failing to add the rule isn't necessarily an error, it may
222
	 * have been modified by the user in some way. Adding to the
223
	 * Alias is what's important.
224
	 */
225 64eda26c jim-p
	if (!easyrule_block_rule_exists($int, $ipproto)) {
226
		if (easyrule_block_rule_create($int, $ipproto)) {
227 c0b6fdde jim-p
			$dirty = true;
228
		} else {
229
			return false;
230
		}
231
	}
232
233
	/* If needed, write the config and reload the filter */
234
	if ($dirty) {
235
		write_config();
236
		$retval = filter_configure();
237 865ff9b4 jim-p
		if (!empty($_SERVER['DOCUMENT_ROOT'])) {
238
			header("Location: firewall_aliases.php");
239
			exit;
240
		} else {
241
			return true;
242
		}
243 c0b6fdde jim-p
	} else {
244
		return false;
245
	}
246
}
247
248 bd40781a Seth Mos
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
249 c0b6fdde jim-p
	global $config;
250
251
	/* No rules, start a new array */
252
	if (!is_array($config['filter']['rule'])) {
253
		$config['filter']['rule'] = array();
254
	}
255
256
	filter_rules_sort();
257
	$a_filter = &$config['filter']['rule'];
258
259
	/* Make up a new rule */
260
	$filterent = array();
261
	$filterent['type'] = 'pass';
262
	$filterent['interface'] = $int;
263 bd40781a Seth Mos
	$filterent['ipprotocol'] = $ipproto;
264 5bd033a0 Renato Botelho
	$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
265 c0b6fdde jim-p
266
	if ($proto != "any")
267
		$filterent['protocol'] = $proto;
268
	else
269
		unset($filterent['protocol']);
270
271
	/* Default to only allow echo requests, since that's what most people want and
272
	 *  it should be a safe choice. */
273
	if ($proto == "icmp")
274
		$filterent['icmptype'] = 'echoreq';
275
276 4410f699 jim-p
	if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6"))
277 daffbc34 jim-p
		$filterent['protocol'] = "icmp";
278
279 0c305760 jim-p
	if (is_subnet($srchost)) {
280
		list($srchost, $srcmask) = explode("/", $srchost);
281
	} elseif (is_specialnet($srchost)) {
282
		$srcmask = 0;
283 aea83400 Thomas Rieschl
	} elseif (is_ipaddrv6($srchost)) {
284
		$srcmask = 128;
285 0c305760 jim-p
	} else {
286
		$srcmask = 32;
287
	}
288
289
	if (is_subnet($dsthost)) {
290
		list($dsthost, $dstmask) = explode("/", $dsthost);
291
	} elseif (is_specialnet($dsthost)) {
292
		$dstmask = 0;
293 aea83400 Thomas Rieschl
	} elseif (is_ipaddrv6($dsthost)) {
294
		$dstmask = 128;
295 0c305760 jim-p
	} else {
296
		$dstmask = 32;
297
	}
298
299
	pconfig_to_address($filterent['source'], $srchost, $srcmask);
300
	pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
301 c0b6fdde jim-p
302 ba1d9714 jim-p
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
303 c0b6fdde jim-p
	$a_filter[] = $filterent;
304
305 998f77a8 jim-p
	write_config($filterent['descr']);
306 c0b6fdde jim-p
	$retval = filter_configure();
307 865ff9b4 jim-p
	if (!empty($_SERVER['DOCUMENT_ROOT'])) {
308
		header("Location: firewall_rules.php?if={$int}");
309
		exit;
310
	} else {
311
		return true;
312
	}
313
}
314
315 64eda26c jim-p
function easyrule_parse_block($int, $src, $ipproto = "inet") {
316 865ff9b4 jim-p
	if (!empty($src) && !empty($int)) {
317 b4147482 jim-p
		$src = trim($src, "[]");
318 0c305760 jim-p
		if (!is_ipaddr($src) && !is_subnet($src)) {
319 5bd033a0 Renato Botelho
			return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
320 865ff9b4 jim-p
		}
321
		$int = easyrule_find_rule_interface($int);
322
		if ($int === false) {
323 5bd033a0 Renato Botelho
			return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
324 865ff9b4 jim-p
		}
325 64eda26c jim-p
		if (easyrule_block_host_add($src, $int, $ipproto)) {
326 5bd033a0 Renato Botelho
			return gettext("Host added successfully");
327 865ff9b4 jim-p
		} else {
328 5bd033a0 Renato Botelho
			return gettext("Failed to create block rule, alias, or add host.");
329 865ff9b4 jim-p
		}
330
	} else {
331 5bd033a0 Renato Botelho
		return gettext("Tried to block but had no host IP or interface");
332 865ff9b4 jim-p
	}
333 5bd033a0 Renato Botelho
	return gettext("Unknown block error.");
334 865ff9b4 jim-p
}
335 64eda26c jim-p
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
336 865ff9b4 jim-p
	/* Check for valid int, srchost, dsthost, dstport, and proto */
337
	global $protocols_with_ports;
338 b4147482 jim-p
	$src = trim($src, "[]");
339
	$dst = trim($dst, "[]");
340 865ff9b4 jim-p
341
	if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
342
		$int = easyrule_find_rule_interface($int);
343
		if ($int === false) {
344 5bd033a0 Renato Botelho
			return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
345 865ff9b4 jim-p
		}
346
		if (getprotobyname($proto) == -1) {
347 5bd033a0 Renato Botelho
			return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
348 865ff9b4 jim-p
		}
349 0c305760 jim-p
		if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
350 5bd033a0 Renato Botelho
			return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
351 865ff9b4 jim-p
		}
352 0c305760 jim-p
		if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
353 5bd033a0 Renato Botelho
			return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
354 865ff9b4 jim-p
		}
355
		if (in_array($proto, $protocols_with_ports)) {
356
			if (empty($dstport)) {
357 5bd033a0 Renato Botelho
				return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
358 865ff9b4 jim-p
			}
359 0c305760 jim-p
			if (!is_port($dstport) && ($dstport != "any")) {
360 5bd033a0 Renato Botelho
				return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
361 865ff9b4 jim-p
			}
362
		} else {
363
			$dstport = 0;
364
		}
365
		/* Should have valid input... */
366 bd40781a Seth Mos
		if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
367 5bd033a0 Renato Botelho
			return gettext("Successfully added pass rule!");
368 865ff9b4 jim-p
		} else {
369 5bd033a0 Renato Botelho
			return gettext("Failed to add pass rule.");
370 865ff9b4 jim-p
		}
371
	} else {
372 5bd033a0 Renato Botelho
		return gettext("Missing parameters for pass rule.");
373 865ff9b4 jim-p
	}
374 5bd033a0 Renato Botelho
	return gettext("Unknown pass error.");
375 c0b6fdde jim-p
}
376 9734b054 Scott Ullrich
377 bd40781a Seth Mos
?>