Project

General

Profile

Download (11 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
function easyrule_block_rule_exists($int = 'wan') {
75
	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 64eda26c jim-p
		if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int) && ($rule['ipprotocol'] == $ipproto))
86 c0b6fdde jim-p
			return true;
87 28a581b8 jim-p
	}
88 c0b6fdde jim-p
	return false;
89
}
90
91 64eda26c jim-p
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
92 c0b6fdde jim-p
	global $blockaliasname, $config;
93
	/* If the alias doesn't exist, exit.
94
	 * Can't create an empty alias, and we don't know a host */
95
	if (easyrule_block_alias_getid($int) === false)
96
		return false;
97
98
	/* If the rule already exists, no need to do it again */
99 64eda26c jim-p
	if (easyrule_block_rule_exists($int, $ipproto))
100 c0b6fdde jim-p
		return true;
101
102
	/* No rules, start a new array */
103
	if (!is_array($config['filter']['rule'])) {
104
		$config['filter']['rule'] = array();
105
	}
106
107
	filter_rules_sort();
108
	$a_filter = &$config['filter']['rule'];
109
110
	/* Make up a new rule */
111
	$filterent = array();
112
	$filterent['type'] = 'block';
113 dadad8b3 jim-p
	$filterent['interface'] = $int;
114 64eda26c jim-p
	$filterent['ipprotocol'] = $ipproto;
115 c0b6fdde jim-p
	$filterent['source']['address'] = $blockaliasname . strtoupper($int);
116
	$filterent['destination']['any'] = '';
117 5bd033a0 Renato Botelho
	$filterent['descr'] = gettext("Easy Rule: Blocked from Firewall Log View");
118 c0b6fdde jim-p
119 a0140246 jim-p
	array_splice($a_filter, 0, 0, array($filterent));
120 c0b6fdde jim-p
121
	return true;
122
}
123
124
function easyrule_block_alias_getid($int = 'wan') {
125
	global $blockaliasname, $config;
126
	if (!is_array($config['aliases']))
127
		return false;
128
129
	/* Hunt down an alias with the name we want, return its id */
130
	foreach ($config['aliases']['alias'] as $aliasid => $alias)
131
		if ($alias['name'] == $blockaliasname . strtoupper($int))
132
			return $aliasid;
133
134
	return false;
135
}
136
137
function easyrule_block_alias_add($host, $int = 'wan') {
138
	global $blockaliasname, $config;
139
	/* If the host isn't a valid IP address, bail */
140 b4147482 jim-p
	$host = trim($host, "[]");
141 0c305760 jim-p
	if (!is_ipaddr($host) && !is_subnet($host))
142 c0b6fdde jim-p
		return false;
143
144
	/* If there are no aliases, start an array */
145
	if (!is_array($config['aliases']['alias']))
146
		$config['aliases']['alias'] = array();
147
148
	$a_aliases = &$config['aliases']['alias'];
149
150
	/* Try to get the ID if the alias already exists */
151
	$id = easyrule_block_alias_getid($int);
152
	if ($id === false)
153
	  unset($id);
154
155
	$alias = array();
156
157 0c305760 jim-p
	if (is_subnet($host)) {
158
		list($host, $mask) = explode("/", $host);
159
	} elseif (is_specialnet($host)) {
160
		$mask = 0;
161 b4147482 jim-p
	} elseif (is_ipaddrv6($host)) {
162
		$mask = 128;
163 0c305760 jim-p
	} else {
164
		$mask = 32;
165
	}
166
167 c0b6fdde jim-p
	if (isset($id) && $a_aliases[$id]) {
168
		/* Make sure this IP isn't already in the list. */
169 0c305760 jim-p
		if (in_array($host.'/'.$mask, explode(" ", $a_aliases[$id]['address'])))
170 c0b6fdde jim-p
			return true;
171
		/* Since the alias already exists, just add to it. */
172
		$alias['name']    = $a_aliases[$id]['name'];
173
		$alias['type']    = $a_aliases[$id]['type'];
174
		$alias['descr']   = $a_aliases[$id]['descr'];
175
176 0c305760 jim-p
		$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/' . $mask;
177 5bd033a0 Renato Botelho
		$alias['detail']  = $a_aliases[$id]['detail'] . gettext('Entry added') . ' ' . date('r') . '||';
178 c0b6fdde jim-p
	} else {
179
		/* Create a new alias with all the proper information */
180
	 	$alias['name']    = $blockaliasname . strtoupper($int);
181
	 	$alias['type']    = 'network';
182 9d3d8d00 Vinicius Coque
		$alias['descr']   = gettext("Hosts blocked from Firewall Log view");
183 c0b6fdde jim-p
184 0c305760 jim-p
		$alias['address'] = $host . '/' . $mask;
185 5bd033a0 Renato Botelho
		$alias['detail']  = gettext('Entry added') . ' ' . date('r') . '||';
186 c0b6fdde jim-p
	}
187
188
	/* Replace the old alias if needed, otherwise tack it on the end */
189
	if (isset($id) && $a_aliases[$id])
190
		$a_aliases[$id] = $alias;
191
	else
192
		$a_aliases[] = $alias;
193 9bb8d542 Ermal Lu?i
194
	// Sort list
195
	$a_aliases = msort($a_aliases, "name");
196 c0b6fdde jim-p
197
	return true;
198
}
199
200 64eda26c jim-p
function easyrule_block_host_add($host, $int = 'wan', $ipproto = "inet") {
201 c0b6fdde jim-p
	global $retval;
202
	/* Bail if the supplied host is not a valid IP address */
203 b4147482 jim-p
	$host = trim($host, "[]");
204 0c305760 jim-p
	if (!is_ipaddr($host) && !is_subnet($host))
205 c0b6fdde jim-p
		return false;
206
207
	/* Flag whether or not we need to reload the filter */
208
	$dirty = false;
209
210
	/* Attempt to add this host to the alias */
211
	if (easyrule_block_alias_add($host, $int)) {
212
		$dirty = true;
213
	} else {
214
		/* Couldn't add the alias, or adding the host failed. */
215
		return false;
216
	}
217
218
	/* Attempt to add the firewall rule if it doesn't exist.
219
	 * Failing to add the rule isn't necessarily an error, it may
220
	 * have been modified by the user in some way. Adding to the
221
	 * Alias is what's important.
222
	 */
223 64eda26c jim-p
	if (!easyrule_block_rule_exists($int, $ipproto)) {
224
		if (easyrule_block_rule_create($int, $ipproto)) {
225 c0b6fdde jim-p
			$dirty = true;
226
		} else {
227
			return false;
228
		}
229
	}
230
231
	/* If needed, write the config and reload the filter */
232
	if ($dirty) {
233
		write_config();
234
		$retval = filter_configure();
235 865ff9b4 jim-p
		if (!empty($_SERVER['DOCUMENT_ROOT'])) {
236
			header("Location: firewall_aliases.php");
237
			exit;
238
		} else {
239
			return true;
240
		}
241 c0b6fdde jim-p
	} else {
242
		return false;
243
	}
244
}
245
246 bd40781a Seth Mos
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
247 c0b6fdde jim-p
	global $config;
248
249
	/* No rules, start a new array */
250
	if (!is_array($config['filter']['rule'])) {
251
		$config['filter']['rule'] = array();
252
	}
253
254
	filter_rules_sort();
255
	$a_filter = &$config['filter']['rule'];
256
257
	/* Make up a new rule */
258
	$filterent = array();
259
	$filterent['type'] = 'pass';
260
	$filterent['interface'] = $int;
261 bd40781a Seth Mos
	$filterent['ipprotocol'] = $ipproto;
262 5bd033a0 Renato Botelho
	$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
263 c0b6fdde jim-p
264
	if ($proto != "any")
265
		$filterent['protocol'] = $proto;
266
	else
267
		unset($filterent['protocol']);
268
269
	/* Default to only allow echo requests, since that's what most people want and
270
	 *  it should be a safe choice. */
271
	if ($proto == "icmp")
272
		$filterent['icmptype'] = 'echoreq';
273
274 0c305760 jim-p
	if (is_subnet($srchost)) {
275
		list($srchost, $srcmask) = explode("/", $srchost);
276
	} elseif (is_specialnet($srchost)) {
277
		$srcmask = 0;
278
	} else {
279
		$srcmask = 32;
280
	}
281
282
	if (is_subnet($dsthost)) {
283
		list($dsthost, $dstmask) = explode("/", $dsthost);
284
	} elseif (is_specialnet($dsthost)) {
285
		$dstmask = 0;
286
	} else {
287
		$dstmask = 32;
288
	}
289
290
	pconfig_to_address($filterent['source'], $srchost, $srcmask);
291
	pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
292 c0b6fdde jim-p
293
	$a_filter[] = $filterent;
294
295 998f77a8 jim-p
	write_config($filterent['descr']);
296 c0b6fdde jim-p
	$retval = filter_configure();
297 865ff9b4 jim-p
	if (!empty($_SERVER['DOCUMENT_ROOT'])) {
298
		header("Location: firewall_rules.php?if={$int}");
299
		exit;
300
	} else {
301
		return true;
302
	}
303
}
304
305 64eda26c jim-p
function easyrule_parse_block($int, $src, $ipproto = "inet") {
306 865ff9b4 jim-p
	if (!empty($src) && !empty($int)) {
307 b4147482 jim-p
		$src = trim($src, "[]");
308 0c305760 jim-p
		if (!is_ipaddr($src) && !is_subnet($src)) {
309 5bd033a0 Renato Botelho
			return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
310 865ff9b4 jim-p
		}
311
		$int = easyrule_find_rule_interface($int);
312
		if ($int === false) {
313 5bd033a0 Renato Botelho
			return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
314 865ff9b4 jim-p
		}
315 64eda26c jim-p
		if (easyrule_block_host_add($src, $int, $ipproto)) {
316 5bd033a0 Renato Botelho
			return gettext("Host added successfully");
317 865ff9b4 jim-p
		} else {
318 5bd033a0 Renato Botelho
			return gettext("Failed to create block rule, alias, or add host.");
319 865ff9b4 jim-p
		}
320
	} else {
321 5bd033a0 Renato Botelho
		return gettext("Tried to block but had no host IP or interface");
322 865ff9b4 jim-p
	}
323 5bd033a0 Renato Botelho
	return gettext("Unknown block error.");
324 865ff9b4 jim-p
}
325 64eda26c jim-p
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
326 865ff9b4 jim-p
	/* Check for valid int, srchost, dsthost, dstport, and proto */
327
	global $protocols_with_ports;
328 b4147482 jim-p
	$src = trim($src, "[]");
329
	$dst = trim($dst, "[]");
330 865ff9b4 jim-p
331
	if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
332
		$int = easyrule_find_rule_interface($int);
333
		if ($int === false) {
334 5bd033a0 Renato Botelho
			return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
335 865ff9b4 jim-p
		}
336
		if (getprotobyname($proto) == -1) {
337 5bd033a0 Renato Botelho
			return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
338 865ff9b4 jim-p
		}
339 0c305760 jim-p
		if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
340 5bd033a0 Renato Botelho
			return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
341 865ff9b4 jim-p
		}
342 0c305760 jim-p
		if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
343 5bd033a0 Renato Botelho
			return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
344 865ff9b4 jim-p
		}
345
		if (in_array($proto, $protocols_with_ports)) {
346
			if (empty($dstport)) {
347 5bd033a0 Renato Botelho
				return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
348 865ff9b4 jim-p
			}
349 0c305760 jim-p
			if (!is_port($dstport) && ($dstport != "any")) {
350 5bd033a0 Renato Botelho
				return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
351 865ff9b4 jim-p
			}
352
		} else {
353
			$dstport = 0;
354
		}
355
		/* Should have valid input... */
356 bd40781a Seth Mos
		if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
357 5bd033a0 Renato Botelho
			return gettext("Successfully added pass rule!");
358 865ff9b4 jim-p
		} else {
359 5bd033a0 Renato Botelho
			return gettext("Failed to add pass rule.");
360 865ff9b4 jim-p
		}
361
	} else {
362 5bd033a0 Renato Botelho
		return gettext("Missing parameters for pass rule.");
363 865ff9b4 jim-p
	}
364 5bd033a0 Renato Botelho
	return gettext("Unknown pass error.");
365 c0b6fdde jim-p
}
366 9734b054 Scott Ullrich
367 bd40781a Seth Mos
?>