Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * easyrule.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2009-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9
 * Originally Sponsored By Anathematic @ pfSense Forums
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
$blockaliasname = 'EasyRuleBlockHosts';
26
$protocols_with_ports = array('tcp', 'udp');
27
require_once("functions.inc");
28
require_once("util.inc");
29
require_once("ipsec.inc");
30
require_once("config.inc");
31

    
32
function easyrule_find_rule_interface($int) {
33
	global $config;
34
	/* Borrowed from firewall_rules.php */
35
	$iflist = get_configured_interface_with_descr(true);
36

    
37
	if ($config['pppoe']['mode'] == "server") {
38
		$iflist['pppoe'] = "PPPoE Server";
39
	}
40

    
41
	if ($config['l2tp']['mode'] == "server") {
42
		$iflist['l2tp'] = "L2TP VPN";
43
	}
44

    
45
	/* add ipsec interfaces */
46
	if (ipsec_enabled()) {
47
		$iflist["enc0"] = "IPSEC";
48
	}
49

    
50
	if (isset($iflist[$int])) {
51
		return $int;
52
	}
53

    
54
	foreach ($iflist as $if => $ifd) {
55
		if (strtolower($int) == strtolower($ifd)) {
56
			return $if;
57
		}
58
	}
59

    
60
	if (substr($int, 0, 4) == "ovpn") {
61
		return "openvpn";
62
	}
63
	if (substr($int, 0, 5) == "ipsec") {
64
		return "ipsec";
65
	}
66
	if (substr($int, 0, 2) == "wg") {
67
		return "wireguard";
68
	}
69

    
70
	return false;
71
}
72

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

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

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

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

    
106
	init_config_arr(array('filter', 'rule'));
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, "Easy Rule");
119
	$filterent['tracker'] = (int)microtime(true);
120

    
121
	// Refer to firewall_rules_edit.php separators updating code.
122
	// Using same code, variables, and techniques here.
123
	$after = -1;	// Place rule at top and move all separators.
124
	array_splice($a_filter, $after+1, 0, array($filterent));
125

    
126
	$tmpif = $int;
127

    
128
	// Update the separators
129
	init_config_arr(array('filter', 'separator', strtolower($tmpif)));
130
	$a_separators = &$config['filter']['separator'][strtolower($tmpif)];
131
	$ridx = ifridx($tmpif, $after);	// get rule index within interface
132
	$mvnrows = +1;
133
	move_separators($a_separators, $ridx, $mvnrows);
134

    
135
	return true;
136
}
137

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

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

    
151
	return false;
152
}
153

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

    
162
	init_config_arr(array('aliases', 'alias'));
163
	$a_aliases = &$config['aliases']['alias'];
164

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

    
171
	$alias = array();
172

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

    
183
	if (isset($id) && $a_aliases[$id]) {
184

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

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

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

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

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

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

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

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

    
227
	// Sort list
228
	$a_aliases = msort($a_aliases, "name");
229

    
230
	return true;
231
}
232

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

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

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

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

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

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

    
283
	init_config_arr(array('filter', 'rule'));
284
	filter_rules_sort();
285
	$a_filter = &$config['filter']['rule'];
286

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

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

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

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

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

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

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

    
333
	$filterent['created'] = make_config_revision_entry(null, "Easy Rule");
334
	$filterent['tracker'] = (int)microtime(true);
335
	$a_filter[] = $filterent;
336

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

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

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

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

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

    
387
		init_config_arr(array('aliases', 'alias', $id));
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(sprintf(gettext("Unblocked host %s via easy rule"), $host));
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 is 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
?>
(16-16/61)