Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	easyrule.inc
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

    
50
	if ($config['pppoe']['mode'] == "server") {
51
		$iflist['pppoe'] = "PPPoE Server";
52
	}
53

    
54
	if ($config['l2tp']['mode'] == "server") {
55
		$iflist['l2tp'] = "L2TP VPN";
56
	}
57

    
58
	/* add ipsec interfaces */
59
	if (isset($config['ipsec']['enable']) || isset($config['ipsec']['client']['enable'])) {
60
		$iflist["enc0"] = "IPSEC";
61
	}
62

    
63
	if (isset($iflist[$int])) {
64
		return $int;
65
	}
66

    
67
	foreach ($iflist as $if => $ifd) {
68
		if (strtolower($int) == strtolower($ifd)) {
69
			return $if;
70
		}
71
	}
72

    
73
	if (substr($int, 0, 4) == "ovpn") {
74
		return "openvpn";
75
	}
76

    
77
	return false;
78
}
79

    
80
function easyrule_block_rule_exists($int = 'wan', $ipproto = "inet") {
81
	global $blockaliasname, $config;
82
	/* No rules, we we know it doesn't exist */
83
	if (!is_array($config['filter']['rule'])) {
84
		return false;
85
	}
86

    
87
	/* Search through the rules for one referencing our alias */
88
	foreach ($config['filter']['rule'] as $rule) {
89
		if (!is_array($rule) || !is_array($rule['source'])) {
90
			continue;
91
		}
92
		$checkproto = isset($rule['ipprotocol']) ? $rule['ipprotocol'] : "inet";
93
		if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int) && ($checkproto == $ipproto)) {
94
			return true;
95
		}
96
	}
97
	return false;
98
}
99

    
100
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
101
	global $blockaliasname, $config;
102
	/* If the alias doesn't exist, exit.
103
	 * Can't create an empty alias, and we don't know a host */
104
	if (easyrule_block_alias_getid($int) === false) {
105
		return false;
106
	}
107

    
108
	/* If the rule already exists, no need to do it again */
109
	if (easyrule_block_rule_exists($int, $ipproto)) {
110
		return true;
111
	}
112

    
113
	/* No rules, start a new array */
114
	if (!is_array($config['filter']['rule'])) {
115
		$config['filter']['rule'] = array();
116
	}
117

    
118
	filter_rules_sort();
119
	$a_filter = &$config['filter']['rule'];
120

    
121
	/* Make up a new rule */
122
	$filterent = array();
123
	$filterent['type'] = 'block';
124
	$filterent['interface'] = $int;
125
	$filterent['ipprotocol'] = $ipproto;
126
	$filterent['source']['address'] = $blockaliasname . strtoupper($int);
127
	$filterent['destination']['any'] = '';
128
	$filterent['descr'] = gettext("Easy Rule: Blocked from Firewall Log View");
129
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
130

    
131
	array_splice($a_filter, 0, 0, array($filterent));
132

    
133
	return true;
134
}
135

    
136
function easyrule_block_alias_getid($int = 'wan') {
137
	global $blockaliasname, $config;
138
	if (!is_array($config['aliases'])) {
139
		return false;
140
	}
141

    
142
	/* Hunt down an alias with the name we want, return its id */
143
	foreach ($config['aliases']['alias'] as $aliasid => $alias) {
144
		if ($alias['name'] == $blockaliasname . strtoupper($int)) {
145
			return $aliasid;
146
		}
147
	}
148

    
149
	return false;
150
}
151

    
152
function easyrule_block_alias_add($host, $int = 'wan') {
153
	global $blockaliasname, $config;
154
	/* If the host isn't a valid IP address, bail */
155
	$host = trim($host, "[]");
156
	if (!is_ipaddr($host) && !is_subnet($host)) {
157
		return false;
158
	}
159

    
160
	/* If there are no aliases, start an array */
161
	if (!is_array($config['aliases']['alias'])) {
162
		$config['aliases']['alias'] = array();
163
	}
164

    
165
	$a_aliases = &$config['aliases']['alias'];
166

    
167
	/* Try to get the ID if the alias already exists */
168
	$id = easyrule_block_alias_getid($int);
169
	if ($id === false) {
170
	  unset($id);
171
	}
172

    
173
	$alias = array();
174

    
175
	if (is_subnet($host)) {
176
		list($host, $mask) = explode("/", $host);
177
	} elseif (is_specialnet($host)) {
178
		$mask = 0;
179
	} elseif (is_ipaddrv6($host)) {
180
		$mask = 128;
181
	} else {
182
		$mask = 32;
183
	}
184

    
185
	if (isset($id) && $a_aliases[$id]) {
186

    
187
		// Catch case when the list is empty
188
		if (empty($a_aliases[$id]['address'])) {
189
			$a_address = array();
190
			$a_detail = array();
191
		} else {
192
			$a_address = explode(" ", $a_aliases[$id]['address']);
193

    
194
			/* Make sure this IP isn't already in the list. */
195
			if (in_array($host.'/'.$mask, $a_address)) {
196
				return true;
197
			}
198
			$a_detail = explode("||", $a_aliases[$id]['detail']);
199
		}
200

    
201
		/* Since the alias already exists, just add to it. */
202
		$alias['name']    = $a_aliases[$id]['name'];
203
		$alias['type']    = $a_aliases[$id]['type'];
204
		$alias['descr']   = $a_aliases[$id]['descr'];
205

    
206
		$a_address[] = $host.'/'.$mask;
207
		$a_detail[] = gettext('Entry added') . ' ' . date('r');
208

    
209
		$alias['address'] = join(" ", $a_address);
210
		$alias['detail']  = join("||", $a_detail);
211

    
212
	} else {
213
		/* Create a new alias with all the proper information */
214
		$alias['name']    = $blockaliasname . strtoupper($int);
215
		$alias['type']    = 'network';
216
		$alias['descr']   = gettext("Hosts blocked from Firewall Log view");
217

    
218
		$alias['address'] = $host . '/' . $mask;
219
		$alias['detail']  = gettext('Entry added') . ' ' . date('r') . '||';
220
	}
221

    
222
	/* Replace the old alias if needed, otherwise tack it on the end */
223
	if (isset($id) && $a_aliases[$id]) {
224
		$a_aliases[$id] = $alias;
225
	} else {
226
		$a_aliases[] = $alias;
227
	}
228

    
229
	// Sort list
230
	$a_aliases = msort($a_aliases, "name");
231

    
232
	return true;
233
}
234

    
235
function easyrule_block_host_add($host, $int = 'wan', $ipproto = "inet") {
236
	global $retval;
237
	/* Bail if the supplied host is not a valid IP address */
238
	$host = trim($host, "[]");
239
	if (!is_ipaddr($host) && !is_subnet($host)) {
240
		return false;
241
	}
242

    
243
	/* Flag whether or not we need to reload the filter */
244
	$dirty = false;
245

    
246
	/* Attempt to add this host to the alias */
247
	if (easyrule_block_alias_add($host, $int)) {
248
		$dirty = true;
249
	} else {
250
		/* Couldn't add the alias, or adding the host failed. */
251
		return false;
252
	}
253

    
254
	/* Attempt to add the firewall rule if it doesn't exist.
255
	 * Failing to add the rule isn't necessarily an error, it may
256
	 * have been modified by the user in some way. Adding to the
257
	 * Alias is what's important.
258
	 */
259
	if (!easyrule_block_rule_exists($int, $ipproto)) {
260
		if (easyrule_block_rule_create($int, $ipproto)) {
261
			$dirty = true;
262
		} else {
263
			return false;
264
		}
265
	}
266

    
267
	/* If needed, write the config and reload the filter */
268
	if ($dirty) {
269
		write_config();
270
		$retval = filter_configure();
271
		if (!empty($_SERVER['DOCUMENT_ROOT'])) {
272
			header("Location: firewall_aliases.php");
273
			exit;
274
		} else {
275
			return true;
276
		}
277
	} else {
278
		return false;
279
	}
280
}
281

    
282
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
283
	global $config;
284

    
285
	/* No rules, start a new array */
286
	if (!is_array($config['filter']['rule'])) {
287
		$config['filter']['rule'] = array();
288
	}
289

    
290
	filter_rules_sort();
291
	$a_filter = &$config['filter']['rule'];
292

    
293
	/* Make up a new rule */
294
	$filterent = array();
295
	$filterent['type'] = 'pass';
296
	$filterent['interface'] = $int;
297
	$filterent['ipprotocol'] = $ipproto;
298
	$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
299

    
300
	if ($proto != "any") {
301
		$filterent['protocol'] = $proto;
302
	} else {
303
		unset($filterent['protocol']);
304
	}
305

    
306
	/* Default to only allow echo requests, since that's what most people want and
307
	 *  it should be a safe choice. */
308
	if ($proto == "icmp") {
309
		$filterent['icmptype'] = 'echoreq';
310
	}
311

    
312
	if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6")) {
313
		$filterent['protocol'] = "icmp";
314
	}
315

    
316
	if (is_subnet($srchost)) {
317
		list($srchost, $srcmask) = explode("/", $srchost);
318
	} elseif (is_specialnet($srchost)) {
319
		$srcmask = 0;
320
	} elseif (is_ipaddrv6($srchost)) {
321
		$srcmask = 128;
322
	} else {
323
		$srcmask = 32;
324
	}
325

    
326
	if (is_subnet($dsthost)) {
327
		list($dsthost, $dstmask) = explode("/", $dsthost);
328
	} elseif (is_specialnet($dsthost)) {
329
		$dstmask = 0;
330
	} elseif (is_ipaddrv6($dsthost)) {
331
		$dstmask = 128;
332
	} else {
333
		$dstmask = 32;
334
	}
335

    
336
	pconfig_to_address($filterent['source'], $srchost, $srcmask);
337
	pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
338

    
339
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
340
	$a_filter[] = $filterent;
341

    
342
	write_config($filterent['descr']);
343
	$retval = filter_configure();
344
	if (!empty($_SERVER['DOCUMENT_ROOT'])) {
345
		header("Location: firewall_rules.php?if={$int}");
346
		exit;
347
	} else {
348
		return true;
349
	}
350
}
351

    
352
function easyrule_parse_block($int, $src, $ipproto = "inet") {
353
	if (!empty($src) && !empty($int)) {
354
		$src = trim($src, "[]");
355
		if (!is_ipaddr($src) && !is_subnet($src)) {
356
			return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
357
		}
358
		$int = easyrule_find_rule_interface($int);
359
		if ($int === false) {
360
			return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
361
		}
362
		if (easyrule_block_host_add($src, $int, $ipproto)) {
363
			return gettext("Host added successfully");
364
		} else {
365
			return gettext("Failed to create block rule, alias, or add host.");
366
		}
367
	} else {
368
		return gettext("Tried to block but had no host IP or interface");
369
	}
370
	return gettext("Unknown block error.");
371
}
372

    
373
function easyrule_parse_unblock($int, $host, $ipproto = "inet") {
374
	global $blockaliasname, $config;
375

    
376
	if (!empty($host) && !empty($int)) {
377
		$host = trim($host, "[]");
378
		if (!is_ipaddr($host) && !is_subnet($host)) {
379
			return gettext("Tried to unblock invalid IP:") . ' ' . htmlspecialchars($host);
380
		}
381
		$real_int = easyrule_find_rule_interface($int);
382
		if ($real_int === false) {
383
			return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
384
		}
385

    
386
		/* Try to get the ID - will fail if there are no rules/alias on this interface */
387
		$id = easyrule_block_alias_getid($real_int);
388
		if ($id === false || !$config['aliases']['alias'][$id]) {
389
			return gettext("No block rules set on interface:") . ' ' . htmlspecialchars($int);
390
		}
391

    
392
		$alias = &$config['aliases']['alias'][$id];
393

    
394
		if (is_subnet($host)) {
395
			list($host, $mask) = explode("/", $host);
396
		} elseif (is_specialnet($host)) {
397
			$mask = 0;
398
		} elseif (is_ipaddrv6($host)) {
399
			$mask = 128;
400
		} else {
401
			$mask = 32;
402
		}
403

    
404
		// Create the expected string representation
405
		$unblock = $host.'/'.$mask;
406

    
407
		$a_address = explode(" ", $config['aliases']['alias'][$id]['address']);
408
		$a_detail = explode("||", $config['aliases']['alias'][$id]['detail']);
409

    
410
		if (($key = array_search($unblock, $a_address)) !== false) {
411
			unset($a_address[$key]);
412
			unset($a_detail[$key]);
413
			// Write back the result to the config array
414
			$config['aliases']['alias'][$id]['address'] = join(" ", $a_address);
415
			$config['aliases']['alias'][$id]['detail'] = join("||", $a_detail);
416

    
417
			// Update config
418
			write_config();
419
			$retval = filter_configure();
420
			if (!empty($_SERVER['DOCUMENT_ROOT'])) {
421
				header("Location: firewall_aliases.php");
422
				exit;
423
			} else {
424
				return gettext("Host unblocked successfully");
425
			}
426
		} else {
427
			return gettext("Host ist not on block list: " . $host);
428
		}
429
	}
430

    
431
	return gettext("Tried to unblock but had no host IP or interface");
432

    
433
}
434

    
435
function easyrule_parse_getblock($int = 'wan', $sep = "\n") {
436
	global $blockaliasname, $config;
437

    
438
	$real_int = easyrule_find_rule_interface($int);
439
	if ($real_int === false) {
440
		return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
441
	}
442

    
443
	/* Try to get the ID - will fail if there are no rules/alias on this interface */
444
	$id = easyrule_block_alias_getid($real_int);
445

    
446
	if ($id === false || !$config['aliases']['alias'][$id] || empty($config['aliases']['alias'][$id]['address'])) {
447
		return gettext("No block rules set on interface:") . ' ' . htmlspecialchars($int);
448
	}
449
	return join($sep, explode(" ", $config['aliases']['alias'][$id]['address']));
450

    
451
}
452

    
453
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
454
	/* Check for valid int, srchost, dsthost, dstport, and proto */
455
	global $protocols_with_ports;
456
	$src = trim($src, "[]");
457
	$dst = trim($dst, "[]");
458

    
459
	if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
460
		$int = easyrule_find_rule_interface($int);
461
		if ($int === false) {
462
			return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
463
		}
464
		if (getprotobyname($proto) == -1) {
465
			return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
466
		}
467
		if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
468
			return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
469
		}
470
		if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
471
			return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
472
		}
473
		if (in_array($proto, $protocols_with_ports)) {
474
			if (empty($dstport)) {
475
				return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
476
			}
477
			if (!is_port($dstport) && ($dstport != "any")) {
478
				return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
479
			}
480
		} else {
481
			$dstport = 0;
482
		}
483
		/* Should have valid input... */
484
		if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
485
			return gettext("Successfully added pass rule!");
486
		} else {
487
			return gettext("Failed to add pass rule.");
488
		}
489
	} else {
490
		return gettext("Missing parameters for pass rule.");
491
	}
492
	return gettext("Unknown pass error.");
493
}
494

    
495
?>
(18-18/68)