Project

General

Profile

Download (11.3 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']['client']['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 (!is_array($rule) || !is_array($rule['source']))
84
			continue;
85
		if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int) && ($rule['ipprotocol'] == $ipproto))
86
			return true;
87
	}
88
	return false;
89
}
90

    
91
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
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, $ipproto))
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
	$filterent['interface'] = $int;
114
	$filterent['ipprotocol'] = $ipproto;
115
	$filterent['source']['address'] = $blockaliasname . strtoupper($int);
116
	$filterent['destination']['any'] = '';
117
	$filterent['descr'] = gettext("Easy Rule: Blocked from Firewall Log View");
118
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
119

    
120
	array_splice($a_filter, 0, 0, array($filterent));
121

    
122
	return true;
123
}
124

    
125
function easyrule_block_alias_getid($int = 'wan') {
126
	global $blockaliasname, $config;
127
	if (!is_array($config['aliases']))
128
		return false;
129

    
130
	/* Hunt down an alias with the name we want, return its id */
131
	foreach ($config['aliases']['alias'] as $aliasid => $alias)
132
		if ($alias['name'] == $blockaliasname . strtoupper($int))
133
			return $aliasid;
134

    
135
	return false;
136
}
137

    
138
function easyrule_block_alias_add($host, $int = 'wan') {
139
	global $blockaliasname, $config;
140
	/* If the host isn't a valid IP address, bail */
141
	$host = trim($host, "[]");
142
	if (!is_ipaddr($host) && !is_subnet($host))
143
		return false;
144

    
145
	/* If there are no aliases, start an array */
146
	if (!is_array($config['aliases']['alias']))
147
		$config['aliases']['alias'] = array();
148

    
149
	$a_aliases = &$config['aliases']['alias'];
150

    
151
	/* Try to get the ID if the alias already exists */
152
	$id = easyrule_block_alias_getid($int);
153
	if ($id === false)
154
	  unset($id);
155

    
156
	$alias = array();
157

    
158
	if (is_subnet($host)) {
159
		list($host, $mask) = explode("/", $host);
160
	} elseif (is_specialnet($host)) {
161
		$mask = 0;
162
	} elseif (is_ipaddrv6($host)) {
163
		$mask = 128;
164
	} else {
165
		$mask = 32;
166
	}
167

    
168
	if (isset($id) && $a_aliases[$id]) {
169
		/* Make sure this IP isn't already in the list. */
170
		if (in_array($host.'/'.$mask, explode(" ", $a_aliases[$id]['address'])))
171
			return true;
172
		/* Since the alias already exists, just add to it. */
173
		$alias['name']    = $a_aliases[$id]['name'];
174
		$alias['type']    = $a_aliases[$id]['type'];
175
		$alias['descr']   = $a_aliases[$id]['descr'];
176

    
177
		$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/' . $mask;
178
		$alias['detail']  = $a_aliases[$id]['detail'] . gettext('Entry added') . ' ' . date('r') . '||';
179
	} else {
180
		/* Create a new alias with all the proper information */
181
	 	$alias['name']    = $blockaliasname . strtoupper($int);
182
	 	$alias['type']    = 'network';
183
		$alias['descr']   = gettext("Hosts blocked from Firewall Log view");
184

    
185
		$alias['address'] = $host . '/' . $mask;
186
		$alias['detail']  = gettext('Entry added') . ' ' . date('r') . '||';
187
	}
188

    
189
	/* Replace the old alias if needed, otherwise tack it on the end */
190
	if (isset($id) && $a_aliases[$id])
191
		$a_aliases[$id] = $alias;
192
	else
193
		$a_aliases[] = $alias;
194

    
195
	// Sort list
196
	$a_aliases = msort($a_aliases, "name");
197

    
198
	return true;
199
}
200

    
201
function easyrule_block_host_add($host, $int = 'wan', $ipproto = "inet") {
202
	global $retval;
203
	/* Bail if the supplied host is not a valid IP address */
204
	$host = trim($host, "[]");
205
	if (!is_ipaddr($host) && !is_subnet($host))
206
		return false;
207

    
208
	/* Flag whether or not we need to reload the filter */
209
	$dirty = false;
210

    
211
	/* Attempt to add this host to the alias */
212
	if (easyrule_block_alias_add($host, $int)) {
213
		$dirty = true;
214
	} else {
215
		/* Couldn't add the alias, or adding the host failed. */
216
		return false;
217
	}
218

    
219
	/* Attempt to add the firewall rule if it doesn't exist.
220
	 * Failing to add the rule isn't necessarily an error, it may
221
	 * have been modified by the user in some way. Adding to the
222
	 * Alias is what's important.
223
	 */
224
	if (!easyrule_block_rule_exists($int, $ipproto)) {
225
		if (easyrule_block_rule_create($int, $ipproto)) {
226
			$dirty = true;
227
		} else {
228
			return false;
229
		}
230
	}
231

    
232
	/* If needed, write the config and reload the filter */
233
	if ($dirty) {
234
		write_config();
235
		$retval = filter_configure();
236
		if (!empty($_SERVER['DOCUMENT_ROOT'])) {
237
			header("Location: firewall_aliases.php");
238
			exit;
239
		} else {
240
			return true;
241
		}
242
	} else {
243
		return false;
244
	}
245
}
246

    
247
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
248
	global $config;
249

    
250
	/* No rules, start a new array */
251
	if (!is_array($config['filter']['rule'])) {
252
		$config['filter']['rule'] = array();
253
	}
254

    
255
	filter_rules_sort();
256
	$a_filter = &$config['filter']['rule'];
257

    
258
	/* Make up a new rule */
259
	$filterent = array();
260
	$filterent['type'] = 'pass';
261
	$filterent['interface'] = $int;
262
	$filterent['ipprotocol'] = $ipproto;
263
	$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
264

    
265
	if ($proto != "any")
266
		$filterent['protocol'] = $proto;
267
	else
268
		unset($filterent['protocol']);
269

    
270
	/* Default to only allow echo requests, since that's what most people want and
271
	 *  it should be a safe choice. */
272
	if ($proto == "icmp")
273
		$filterent['icmptype'] = 'echoreq';
274

    
275
	if (strtolower($proto) == "icmp6")
276
		$filterent['protocol'] = "icmp";
277

    
278
	if (is_subnet($srchost)) {
279
		list($srchost, $srcmask) = explode("/", $srchost);
280
	} elseif (is_specialnet($srchost)) {
281
		$srcmask = 0;
282
	} elseif (is_ipaddrv6($srchost)) {
283
		$srcmask = 128;
284
	} else {
285
		$srcmask = 32;
286
	}
287

    
288
	if (is_subnet($dsthost)) {
289
		list($dsthost, $dstmask) = explode("/", $dsthost);
290
	} elseif (is_specialnet($dsthost)) {
291
		$dstmask = 0;
292
	} elseif (is_ipaddrv6($dsthost)) {
293
		$dstmask = 128;
294
	} else {
295
		$dstmask = 32;
296
	}
297

    
298
	pconfig_to_address($filterent['source'], $srchost, $srcmask);
299
	pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
300

    
301
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
302
	$a_filter[] = $filterent;
303

    
304
	write_config($filterent['descr']);
305
	$retval = filter_configure();
306
	if (!empty($_SERVER['DOCUMENT_ROOT'])) {
307
		header("Location: firewall_rules.php?if={$int}");
308
		exit;
309
	} else {
310
		return true;
311
	}
312
}
313

    
314
function easyrule_parse_block($int, $src, $ipproto = "inet") {
315
	if (!empty($src) && !empty($int)) {
316
		$src = trim($src, "[]");
317
		if (!is_ipaddr($src) && !is_subnet($src)) {
318
			return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
319
		}
320
		$int = easyrule_find_rule_interface($int);
321
		if ($int === false) {
322
			return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
323
		}
324
		if (easyrule_block_host_add($src, $int, $ipproto)) {
325
			return gettext("Host added successfully");
326
		} else {
327
			return gettext("Failed to create block rule, alias, or add host.");
328
		}
329
	} else {
330
		return gettext("Tried to block but had no host IP or interface");
331
	}
332
	return gettext("Unknown block error.");
333
}
334
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
335
	/* Check for valid int, srchost, dsthost, dstport, and proto */
336
	global $protocols_with_ports;
337
	$src = trim($src, "[]");
338
	$dst = trim($dst, "[]");
339

    
340
	if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
341
		$int = easyrule_find_rule_interface($int);
342
		if ($int === false) {
343
			return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
344
		}
345
		if (getprotobyname($proto) == -1) {
346
			return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
347
		}
348
		if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
349
			return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
350
		}
351
		if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
352
			return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
353
		}
354
		if (in_array($proto, $protocols_with_ports)) {
355
			if (empty($dstport)) {
356
				return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
357
			}
358
			if (!is_port($dstport) && ($dstport != "any")) {
359
				return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
360
			}
361
		} else {
362
			$dstport = 0;
363
		}
364
		/* Should have valid input... */
365
		if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
366
			return gettext("Successfully added pass rule!");
367
		} else {
368
			return gettext("Failed to add pass rule.");
369
		}
370
	} else {
371
		return gettext("Missing parameters for pass rule.");
372
	}
373
	return gettext("Unknown pass error.");
374
}
375

    
376
?>
(18-18/66)