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-2019 Rubicon Communications, LLC (Netgate)
7
 * Originally Sponsored By Anathematic @ pfSense Forums
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

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

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

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

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

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

    
48
	if (isset($iflist[$int])) {
49
		return $int;
50
	}
51

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

    
58
	if (substr($int, 0, 4) == "ovpn") {
59
		return "openvpn";
60
	}
61
	if (substr($int, 0, 5) == "ipsec") {
62
		return "ipsec";
63
	}
64

    
65
	return false;
66
}
67

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

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

    
88
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
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

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

    
101
	init_config_arr(array('filter', 'rule'));
102
	filter_rules_sort();
103
	$a_filter = &$config['filter']['rule'];
104

    
105
	/* Make up a new rule */
106
	$filterent = array();
107
	$filterent['type'] = 'block';
108
	$filterent['interface'] = $int;
109
	$filterent['ipprotocol'] = $ipproto;
110
	$filterent['source']['address'] = $blockaliasname . strtoupper($int);
111
	$filterent['destination']['any'] = '';
112
	$filterent['descr'] = gettext("Easy Rule: Blocked from Firewall Log View");
113
	/* Do not translate this, it's considered a username which cannot contain international characters */
114
	$filterent['created'] = make_config_revision_entry(null, "Easy Rule");
115
	$filterent['tracker'] = (int)microtime(true);
116

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

    
122
	$tmpif = $int;
123

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

    
131
	return true;
132
}
133

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

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

    
147
	return false;
148
}
149

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

    
158
	init_config_arr(array('aliases', 'alias'));
159
	$a_aliases = &$config['aliases']['alias'];
160

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

    
167
	$alias = array();
168

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

    
179
	if (isset($id) && $a_aliases[$id]) {
180

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

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

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

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

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

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

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

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

    
223
	// Sort list
224
	$a_aliases = msort($a_aliases, "name");
225

    
226
	return true;
227
}
228

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

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

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

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

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

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

    
279
	init_config_arr(array('filter', 'rule'));
280
	filter_rules_sort();
281
	$a_filter = &$config['filter']['rule'];
282

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

    
290
	if ($proto != "any") {
291
		$filterent['protocol'] = $proto;
292
	} else {
293
		unset($filterent['protocol']);
294
	}
295

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

    
302
	if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6")) {
303
		$filterent['protocol'] = "icmp";
304
	}
305

    
306
	if (is_subnet($srchost)) {
307
		list($srchost, $srcmask) = explode("/", $srchost);
308
	} elseif (is_specialnet($srchost)) {
309
		$srcmask = 0;
310
	} elseif (is_ipaddrv6($srchost)) {
311
		$srcmask = 128;
312
	} else {
313
		$srcmask = 32;
314
	}
315

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

    
326
	pconfig_to_address($filterent['source'], $srchost, $srcmask);
327
	pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
328

    
329
	/* Do not translate this, it's considered a username which cannot contain international characters */
330
	$filterent['created'] = make_config_revision_entry(null, "Easy Rule");
331
	$filterent['tracker'] = (int)microtime(true);
332
	$a_filter[] = $filterent;
333

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

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

    
365
function easyrule_parse_unblock($int, $host, $ipproto = "inet") {
366
	global $blockaliasname, $config;
367

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

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

    
384
		init_config_arr(array('aliases', 'alias', $id));
385
		$alias = &$config['aliases']['alias'][$id];
386

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

    
397
		// Create the expected string representation
398
		$unblock = $host.'/'.$mask;
399

    
400
		$a_address = explode(" ", $config['aliases']['alias'][$id]['address']);
401
		$a_detail = explode("||", $config['aliases']['alias'][$id]['detail']);
402

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

    
410
			// Update config
411
			write_config(sprintf(gettext("Unblocked host %s via easy rule"), $host));
412
			$retval = filter_configure();
413
			if (!empty($_SERVER['DOCUMENT_ROOT'])) {
414
				header("Location: firewall_aliases.php");
415
				exit;
416
			} else {
417
				return gettext("Host unblocked successfully");
418
			}
419
		} else {
420
			return gettext("Host is not on block list: " . $host);
421
		}
422
	}
423

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

    
426
}
427

    
428
function easyrule_parse_getblock($int = 'wan', $sep = "\n") {
429
	global $blockaliasname, $config;
430

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

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

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

    
444
}
445

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

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

    
488
?>
(16-16/59)