Project

General

Profile

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