Project

General

Profile

Download (9.56 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	easyrule.inc.php
4

    
5
	Copyright (C) 2009-2010 Jim Pingle (jpingle@gmail.com)
6
	Originally Sponsored By Anathematic @ pfSense Forums
7
	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
/*
31
	pfSense_BUILDER_BINARIES:
32
	pfSense_MODULE:	filter
33
*/
34

    
35
$blockaliasname = 'EasyRuleBlockHosts';
36
$protocols_with_ports = array('tcp', 'udp');
37
require_once("functions.inc");
38
require_once("util.inc");
39
require_once("config.inc");
40

    
41
function easyrule_find_rule_interface($int) {
42
	global $config;
43
	/* Borrowed from firewall_rules.php */
44
	$iflist = get_configured_interface_with_descr(false, true);
45

    
46
	if ($config['pptpd']['mode'] == "server")
47
		$iflist['pptp'] = "PPTP VPN";
48

    
49
	if ($config['pppoe']['mode'] == "server")
50
		$iflist['pppoe'] = "PPPoE VPN";
51

    
52
	if ($config['l2tp']['mode'] == "server")
53
                $iflist['l2tp'] = "L2TP VPN";
54

    
55
	/* add ipsec interfaces */
56
	if (isset($config['ipsec']['enable']) || isset($config['ipsec']['mobileclients']['enable'])){
57
		$iflist["enc0"] = "IPSEC";
58
	}
59

    
60
	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

    
68
	if (substr($int, 0, 4) == "ovpn")
69
		return "openvpn";
70

    
71
	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
	foreach ($config['filter']['rule'] as $rule)
83
		if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int))
84
			return true;
85
	return false;
86
}
87

    
88
function easyrule_block_rule_create($int = 'wan') {
89
	global $blockaliasname, $config;
90
	/* If the alias doesn't exist, exit.
91
	 * Can't create an empty alias, and we don't know a host */
92
	if (easyrule_block_alias_getid($int) === false)
93
		return false;
94

    
95
	/* If the rule already exists, no need to do it again */
96
	if (easyrule_block_rule_exists($int))
97
		return true;
98

    
99
	/* No rules, start a new array */
100
	if (!is_array($config['filter']['rule'])) {
101
		$config['filter']['rule'] = array();
102
	}
103

    
104
	filter_rules_sort();
105
	$a_filter = &$config['filter']['rule'];
106

    
107
	/* Make up a new rule */
108
	$filterent = array();
109
	$filterent['type'] = 'block';
110
	$filterent['interface'] = $int;
111
	$filterent['source']['address'] = $blockaliasname . strtoupper($int);
112
	$filterent['destination']['any'] = '';
113
	$filterent['descr'] = "Easy Rule: Blocked from Firewall Log View";
114

    
115
	$a_filter[] = $filterent;
116

    
117
	return true;
118
}
119

    
120
function easyrule_block_alias_getid($int = 'wan') {
121
	global $blockaliasname, $config;
122
	if (!is_array($config['aliases']))
123
		return false;
124

    
125
	/* Hunt down an alias with the name we want, return its id */
126
	foreach ($config['aliases']['alias'] as $aliasid => $alias)
127
		if ($alias['name'] == $blockaliasname . strtoupper($int))
128
			return $aliasid;
129

    
130
	return false;
131
}
132

    
133
function easyrule_block_alias_add($host, $int = 'wan') {
134
	global $blockaliasname, $config;
135
	/* If the host isn't a valid IP address, bail */
136
	if (!is_ipaddr($host))
137
		return false;
138

    
139
	/* If there are no aliases, start an array */
140
	if (!is_array($config['aliases']['alias']))
141
		$config['aliases']['alias'] = array();
142

    
143
	$a_aliases = &$config['aliases']['alias'];
144

    
145
	/* Try to get the ID if the alias already exists */
146
	$id = easyrule_block_alias_getid($int);
147
	if ($id === false)
148
	  unset($id);
149

    
150
	$alias = array();
151

    
152
	if (isset($id) && $a_aliases[$id]) {
153
		/* Make sure this IP isn't already in the list. */
154
		if (in_array($host.'/32', explode(" ", $a_aliases[$id]['address'])))
155
			return true;
156
		/* Since the alias already exists, just add to it. */
157
		$alias['name']    = $a_aliases[$id]['name'];
158
		$alias['type']    = $a_aliases[$id]['type'];
159
		$alias['descr']   = $a_aliases[$id]['descr'];
160

    
161
		$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/32';
162
	 	$alias['detail']  = $a_aliases[$id]['detail'] . 'Entry added ' . date('r') . '||';
163
	} else {
164
		/* Create a new alias with all the proper information */
165
	 	$alias['name']    = $blockaliasname . strtoupper($int);
166
	 	$alias['type']    = 'network';
167
	 	$alias['descr']   = mb_convert_encoding("Hosts blocked from Firewall Log view","HTML-ENTITIES","auto");
168

    
169
		$alias['address'] = $host . '/32';
170
	 	$alias['detail']  = 'Entry added ' . date('r') . '||';
171
	}
172

    
173
	/* Replace the old alias if needed, otherwise tack it on the end */
174
	if (isset($id) && $a_aliases[$id])
175
		$a_aliases[$id] = $alias;
176
	else
177
		$a_aliases[] = $alias;
178

    
179
	// Sort list
180
	$a_aliases = msort($a_aliases, "name");
181

    
182
	return true;
183
}
184

    
185
function easyrule_block_host_add($host, $int = 'wan') {
186
	global $retval;
187
	/* Bail if the supplied host is not a valid IP address */
188
	if (!is_ipaddr($host))
189
		return false;
190

    
191
	/* Flag whether or not we need to reload the filter */
192
	$dirty = false;
193

    
194
	/* Attempt to add this host to the alias */
195
	if (easyrule_block_alias_add($host, $int)) {
196
		$dirty = true;
197
	} else {
198
		/* Couldn't add the alias, or adding the host failed. */
199
		return false;
200
	}
201

    
202
	/* Attempt to add the firewall rule if it doesn't exist.
203
	 * Failing to add the rule isn't necessarily an error, it may
204
	 * have been modified by the user in some way. Adding to the
205
	 * Alias is what's important.
206
	 */
207
	if (!easyrule_block_rule_exists($int)) {
208
		if (easyrule_block_rule_create($int)) {
209
			$dirty = true;
210
		} else {
211
			return false;
212
		}
213
	}
214

    
215
	/* If needed, write the config and reload the filter */
216
	if ($dirty) {
217
		write_config();
218
		$retval = filter_configure();
219
		if (!empty($_SERVER['DOCUMENT_ROOT'])) {
220
			header("Location: firewall_aliases.php");
221
			exit;
222
		} else {
223
			return true;
224
		}
225
	} else {
226
		return false;
227
	}
228
}
229

    
230
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport) {
231
	global $config;
232

    
233
	/* No rules, start a new array */
234
	if (!is_array($config['filter']['rule'])) {
235
		$config['filter']['rule'] = array();
236
	}
237

    
238
	filter_rules_sort();
239
	$a_filter = &$config['filter']['rule'];
240

    
241
	/* Make up a new rule */
242
	$filterent = array();
243
	$filterent['type'] = 'pass';
244
	$filterent['interface'] = $int;
245
	$filterent['descr'] = "Easy Rule: Passed from Firewall Log View";
246

    
247
	if ($proto != "any")
248
		$filterent['protocol'] = $proto;
249
	else
250
		unset($filterent['protocol']);
251

    
252
	/* Default to only allow echo requests, since that's what most people want and
253
	 *  it should be a safe choice. */
254
	if ($proto == "icmp")
255
		$filterent['icmptype'] = 'echoreq';
256

    
257
	pconfig_to_address($filterent['source'], $srchost, 32);
258
	pconfig_to_address($filterent['destination'], $dsthost, 32, '', $dstport, $dstport);
259

    
260
	$a_filter[] = $filterent;
261

    
262
	write_config($filterent['descr']);
263
	$retval = filter_configure();
264
	if (!empty($_SERVER['DOCUMENT_ROOT'])) {
265
		header("Location: firewall_rules.php?if={$int}");
266
		exit;
267
	} else {
268
		return true;
269
	}
270
}
271

    
272
function easyrule_parse_block($int, $src) {
273
	if (!empty($src) && !empty($int)) {
274
		if (!is_ipaddr($src)) {
275
			return "Tried to block invalid IP: " . htmlspecialchars($src);
276
		}
277
		$int = easyrule_find_rule_interface($int);
278
		if ($int === false) {
279
			return "Invalid interface for block rule: " . htmlspecialchars($int);
280
		}
281
		if (easyrule_block_host_add($src, $int)) {
282
			return "Host added successfully";
283
		} else {
284
			return "Failed to create block rule, alias, or add host.";
285
		}
286
	} else {
287
		return "Tried to block but had no host IP or interface";
288
	}
289
	return "Unknown block error.";
290
}
291
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0) {
292
	/* Check for valid int, srchost, dsthost, dstport, and proto */
293
	global $protocols_with_ports;
294

    
295
	if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
296
		$int = easyrule_find_rule_interface($int);
297
		if ($int === false) {
298
			return "Invalid interface for pass rule: " . htmlspecialchars($int);
299
		}
300
		if (getprotobyname($proto) == -1) {
301
			return "Invalid protocol for pass rule: " . htmlspecialchars($proto);
302
		}
303
		if (!is_ipaddr($src)) {
304
			return "Tried to pass invalid source IP: " . htmlspecialchars($src);
305
		}
306
		if (!is_ipaddr($dst)) {
307
			return "Tried to pass invalid destination IP: " . htmlspecialchars($dst);
308
		}
309
		if (in_array($proto, $protocols_with_ports)) {
310
			if (empty($dstport)) {
311
				return "Missing destination port: " . htmlspecialchars($dstport);
312
			}
313
			if (!is_port($dstport)) {
314
				return "Tried to pass invalid destination port: " . htmlspecialchars($dstport);
315
			}
316
		} else {
317
			$dstport = 0;
318
		}
319
		/* Should have valid input... */
320
		if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport)) {
321
			return "Successfully added pass rule!";
322
		} else {
323
			return "Failed to add pass rule.";
324
		}
325
	} else {
326
		return "Missing parameters for pass rule.";
327
	}
328
	return "Unknown pass error.";
329
}
330
?>
(14-14/50)