Project

General

Profile

Download (14.2 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['pppoe']['mode'] == "server") {
47
		$iflist['pppoe'] = "PPPoE Server";
48
	}
49

    
50
	if ($config['l2tp']['mode'] == "server") {
51
		$iflist['l2tp'] = "L2TP VPN";
52
	}
53

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

    
59
	if (isset($iflist[$int])) {
60
		return $int;
61
	}
62

    
63
	foreach ($iflist as $if => $ifd) {
64
		if (strtolower($int) == strtolower($ifd)) {
65
			return $if;
66
		}
67
	}
68

    
69
	if (substr($int, 0, 4) == "ovpn") {
70
		return "openvpn";
71
	}
72

    
73
	return false;
74
}
75

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

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

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

    
104
	/* If the rule already exists, no need to do it again */
105
	if (easyrule_block_rule_exists($int, $ipproto)) {
106
		return true;
107
	}
108

    
109
	/* No rules, start a new array */
110
	if (!is_array($config['filter']['rule'])) {
111
		$config['filter']['rule'] = array();
112
	}
113

    
114
	filter_rules_sort();
115
	$a_filter = &$config['filter']['rule'];
116

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

    
127
	array_splice($a_filter, 0, 0, array($filterent));
128

    
129
	return true;
130
}
131

    
132
function easyrule_block_alias_getid($int = 'wan') {
133
	global $blockaliasname, $config;
134
	if (!is_array($config['aliases'])) {
135
		return false;
136
	}
137

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

    
145
	return false;
146
}
147

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

    
156
	/* If there are no aliases, start an array */
157
	if (!is_array($config['aliases']['alias'])) {
158
		$config['aliases']['alias'] = array();
159
	}
160

    
161
	$a_aliases = &$config['aliases']['alias'];
162

    
163
	/* Try to get the ID if the alias already exists */
164
	$id = easyrule_block_alias_getid($int);
165
	if ($id === false) {
166
	  unset($id);
167
	}
168

    
169
	$alias = array();
170

    
171
	if (is_subnet($host)) {
172
		list($host, $mask) = explode("/", $host);
173
	} elseif (is_specialnet($host)) {
174
		$mask = 0;
175
	} elseif (is_ipaddrv6($host)) {
176
		$mask = 128;
177
	} else {
178
		$mask = 32;
179
	}
180

    
181
	if (isset($id) && $a_aliases[$id]) {
182

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

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

    
197
		/* Since the alias already exists, just add to it. */
198
		$alias['name']    = $a_aliases[$id]['name'];
199
		$alias['type']    = $a_aliases[$id]['type'];
200
		$alias['descr']   = $a_aliases[$id]['descr'];
201

    
202
		$a_address[] = $host.'/'.$mask;
203
		$a_detail[] = gettext('Entry added') . ' ' . date('r');
204

    
205
		$alias['address'] = join(" ", $a_address);
206
		$alias['detail']  = join("||", $a_detail);
207

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

    
214
		$alias['address'] = $host . '/' . $mask;
215
		$alias['detail']  = gettext('Entry added') . ' ' . date('r') . '||';
216
	}
217

    
218
	/* Replace the old alias if needed, otherwise tack it on the end */
219
	if (isset($id) && $a_aliases[$id]) {
220
		$a_aliases[$id] = $alias;
221
	} else {
222
		$a_aliases[] = $alias;
223
	}
224

    
225
	// Sort list
226
	$a_aliases = msort($a_aliases, "name");
227

    
228
	return true;
229
}
230

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

    
239
	/* Flag whether or not we need to reload the filter */
240
	$dirty = false;
241

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

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

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

    
278
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
279
	global $config;
280

    
281
	/* No rules, start a new array */
282
	if (!is_array($config['filter']['rule'])) {
283
		$config['filter']['rule'] = array();
284
	}
285

    
286
	filter_rules_sort();
287
	$a_filter = &$config['filter']['rule'];
288

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

    
296
	if ($proto != "any") {
297
		$filterent['protocol'] = $proto;
298
	} else {
299
		unset($filterent['protocol']);
300
	}
301

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

    
308
	if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6")) {
309
		$filterent['protocol'] = "icmp";
310
	}
311

    
312
	if (is_subnet($srchost)) {
313
		list($srchost, $srcmask) = explode("/", $srchost);
314
	} elseif (is_specialnet($srchost)) {
315
		$srcmask = 0;
316
	} elseif (is_ipaddrv6($srchost)) {
317
		$srcmask = 128;
318
	} else {
319
		$srcmask = 32;
320
	}
321

    
322
	if (is_subnet($dsthost)) {
323
		list($dsthost, $dstmask) = explode("/", $dsthost);
324
	} elseif (is_specialnet($dsthost)) {
325
		$dstmask = 0;
326
	} elseif (is_ipaddrv6($dsthost)) {
327
		$dstmask = 128;
328
	} else {
329
		$dstmask = 32;
330
	}
331

    
332
	pconfig_to_address($filterent['source'], $srchost, $srcmask);
333
	pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
334

    
335
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
336
	$a_filter[] = $filterent;
337

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

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

    
369
function easyrule_parse_unblock($int, $host, $ipproto = "inet") {
370
	global $blockaliasname, $config;
371

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

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

    
388
		$alias = &$config['aliases']['alias'][$id];
389

    
390
		if (is_subnet($host)) {
391
			list($host, $mask) = explode("/", $host);
392
		} elseif (is_specialnet($host)) {
393
			$mask = 0;
394
		} elseif (is_ipaddrv6($host)) {
395
			$mask = 128;
396
		} else {
397
			$mask = 32;
398
		}
399

    
400
		// Create the expected string representation
401
		$unblock = $host.'/'.$mask;
402

    
403
		$a_address = explode(" ", $config['aliases']['alias'][$id]['address']);
404
		$a_detail = explode("||", $config['aliases']['alias'][$id]['detail']);
405

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

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

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

    
429
}
430

    
431
function easyrule_parse_getblock($int = 'wan', $sep = "\n") {
432
	global $blockaliasname, $config;
433

    
434
	$real_int = easyrule_find_rule_interface($int);
435
	if ($real_int === false) {
436
		return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
437
	}
438

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

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

    
447
}
448

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

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

    
491
?>
(18-18/67)