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
|
$filterent['created'] = make_config_revision_entry(null, "Easy Rule");
|
114
|
$filterent['tracker'] = (int)microtime(true);
|
115
|
|
116
|
// Refer to firewall_rules_edit.php separators updating code.
|
117
|
// Using same code, variables, and techniques here.
|
118
|
$after = -1; // Place rule at top and move all separators.
|
119
|
array_splice($a_filter, $after+1, 0, array($filterent));
|
120
|
|
121
|
$tmpif = $int;
|
122
|
|
123
|
// Update the separators
|
124
|
init_config_arr(array('filter', 'separator', strtolower($tmpif)));
|
125
|
$a_separators = &$config['filter']['separator'][strtolower($tmpif)];
|
126
|
$ridx = ifridx($tmpif, $after); // get rule index within interface
|
127
|
$mvnrows = +1;
|
128
|
move_separators($a_separators, $ridx, $mvnrows);
|
129
|
|
130
|
return true;
|
131
|
}
|
132
|
|
133
|
function easyrule_block_alias_getid($int = 'wan') {
|
134
|
global $blockaliasname, $config;
|
135
|
if (!is_array($config['aliases'])) {
|
136
|
return false;
|
137
|
}
|
138
|
|
139
|
/* Hunt down an alias with the name we want, return its id */
|
140
|
foreach ($config['aliases']['alias'] as $aliasid => $alias) {
|
141
|
if ($alias['name'] == $blockaliasname . strtoupper($int)) {
|
142
|
return $aliasid;
|
143
|
}
|
144
|
}
|
145
|
|
146
|
return false;
|
147
|
}
|
148
|
|
149
|
function easyrule_block_alias_add($host, $int = 'wan') {
|
150
|
global $blockaliasname, $config;
|
151
|
/* If the host isn't a valid IP address, bail */
|
152
|
$host = trim($host, "[]");
|
153
|
if (!is_ipaddr($host) && !is_subnet($host)) {
|
154
|
return false;
|
155
|
}
|
156
|
|
157
|
init_config_arr(array('aliases', 'alias'));
|
158
|
$a_aliases = &$config['aliases']['alias'];
|
159
|
|
160
|
/* Try to get the ID if the alias already exists */
|
161
|
$id = easyrule_block_alias_getid($int);
|
162
|
if ($id === false) {
|
163
|
unset($id);
|
164
|
}
|
165
|
|
166
|
$alias = array();
|
167
|
|
168
|
if (is_subnet($host)) {
|
169
|
list($host, $mask) = explode("/", $host);
|
170
|
} elseif (is_specialnet($host)) {
|
171
|
$mask = 0;
|
172
|
} elseif (is_ipaddrv6($host)) {
|
173
|
$mask = 128;
|
174
|
} else {
|
175
|
$mask = 32;
|
176
|
}
|
177
|
|
178
|
if (isset($id) && $a_aliases[$id]) {
|
179
|
|
180
|
// Catch case when the list is empty
|
181
|
if (empty($a_aliases[$id]['address'])) {
|
182
|
$a_address = array();
|
183
|
$a_detail = array();
|
184
|
} else {
|
185
|
$a_address = explode(" ", $a_aliases[$id]['address']);
|
186
|
|
187
|
/* Make sure this IP isn't already in the list. */
|
188
|
if (in_array($host.'/'.$mask, $a_address)) {
|
189
|
return true;
|
190
|
}
|
191
|
$a_detail = explode("||", $a_aliases[$id]['detail']);
|
192
|
}
|
193
|
|
194
|
/* Since the alias already exists, just add to it. */
|
195
|
$alias['name'] = $a_aliases[$id]['name'];
|
196
|
$alias['type'] = $a_aliases[$id]['type'];
|
197
|
$alias['descr'] = $a_aliases[$id]['descr'];
|
198
|
|
199
|
$a_address[] = $host.'/'.$mask;
|
200
|
$a_detail[] = gettext('Entry added') . ' ' . date('r');
|
201
|
|
202
|
$alias['address'] = join(" ", $a_address);
|
203
|
$alias['detail'] = join("||", $a_detail);
|
204
|
|
205
|
} else {
|
206
|
/* Create a new alias with all the proper information */
|
207
|
$alias['name'] = $blockaliasname . strtoupper($int);
|
208
|
$alias['type'] = 'network';
|
209
|
$alias['descr'] = gettext("Hosts blocked from Firewall Log view");
|
210
|
|
211
|
$alias['address'] = $host . '/' . $mask;
|
212
|
$alias['detail'] = gettext('Entry added') . ' ' . date('r') . '||';
|
213
|
}
|
214
|
|
215
|
/* Replace the old alias if needed, otherwise tack it on the end */
|
216
|
if (isset($id) && $a_aliases[$id]) {
|
217
|
$a_aliases[$id] = $alias;
|
218
|
} else {
|
219
|
$a_aliases[] = $alias;
|
220
|
}
|
221
|
|
222
|
// Sort list
|
223
|
$a_aliases = msort($a_aliases, "name");
|
224
|
|
225
|
return true;
|
226
|
}
|
227
|
|
228
|
function easyrule_block_host_add($host, $int = 'wan', $ipproto = "inet") {
|
229
|
global $retval;
|
230
|
/* Bail if the supplied host is not a valid IP address */
|
231
|
$host = trim($host, "[]");
|
232
|
if (!is_ipaddr($host) && !is_subnet($host)) {
|
233
|
return false;
|
234
|
}
|
235
|
|
236
|
/* Flag whether or not we need to reload the filter */
|
237
|
$dirty = false;
|
238
|
|
239
|
/* Attempt to add this host to the alias */
|
240
|
if (easyrule_block_alias_add($host, $int)) {
|
241
|
$dirty = true;
|
242
|
} else {
|
243
|
/* Couldn't add the alias, or adding the host failed. */
|
244
|
return false;
|
245
|
}
|
246
|
|
247
|
/* Attempt to add the firewall rule if it doesn't exist.
|
248
|
* Failing to add the rule isn't necessarily an error, it may
|
249
|
* have been modified by the user in some way. Adding to the
|
250
|
* Alias is what's important.
|
251
|
*/
|
252
|
if (!easyrule_block_rule_exists($int, $ipproto)) {
|
253
|
if (easyrule_block_rule_create($int, $ipproto)) {
|
254
|
$dirty = true;
|
255
|
} else {
|
256
|
return false;
|
257
|
}
|
258
|
}
|
259
|
|
260
|
/* If needed, write the config and reload the filter */
|
261
|
if ($dirty) {
|
262
|
write_config(sprintf(gettext("Blocked host %s via easy rule"), $host));
|
263
|
$retval = filter_configure();
|
264
|
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
|
265
|
header("Location: firewall_aliases.php");
|
266
|
exit;
|
267
|
} else {
|
268
|
return true;
|
269
|
}
|
270
|
} else {
|
271
|
return false;
|
272
|
}
|
273
|
}
|
274
|
|
275
|
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
|
276
|
global $config;
|
277
|
|
278
|
init_config_arr(array('filter', 'rule'));
|
279
|
filter_rules_sort();
|
280
|
$a_filter = &$config['filter']['rule'];
|
281
|
|
282
|
/* Make up a new rule */
|
283
|
$filterent = array();
|
284
|
$filterent['type'] = 'pass';
|
285
|
$filterent['interface'] = $int;
|
286
|
$filterent['ipprotocol'] = $ipproto;
|
287
|
$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
|
288
|
|
289
|
if ($proto != "any") {
|
290
|
$filterent['protocol'] = $proto;
|
291
|
} else {
|
292
|
unset($filterent['protocol']);
|
293
|
}
|
294
|
|
295
|
/* Default to only allow echo requests, since that's what most people want and
|
296
|
* it should be a safe choice. */
|
297
|
if ($proto == "icmp") {
|
298
|
$filterent['icmptype'] = 'echoreq';
|
299
|
}
|
300
|
|
301
|
if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6")) {
|
302
|
$filterent['protocol'] = "icmp";
|
303
|
}
|
304
|
|
305
|
if (is_subnet($srchost)) {
|
306
|
list($srchost, $srcmask) = explode("/", $srchost);
|
307
|
} elseif (is_specialnet($srchost)) {
|
308
|
$srcmask = 0;
|
309
|
} elseif (is_ipaddrv6($srchost)) {
|
310
|
$srcmask = 128;
|
311
|
} else {
|
312
|
$srcmask = 32;
|
313
|
}
|
314
|
|
315
|
if (is_subnet($dsthost)) {
|
316
|
list($dsthost, $dstmask) = explode("/", $dsthost);
|
317
|
} elseif (is_specialnet($dsthost)) {
|
318
|
$dstmask = 0;
|
319
|
} elseif (is_ipaddrv6($dsthost)) {
|
320
|
$dstmask = 128;
|
321
|
} else {
|
322
|
$dstmask = 32;
|
323
|
}
|
324
|
|
325
|
pconfig_to_address($filterent['source'], $srchost, $srcmask);
|
326
|
pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
|
327
|
|
328
|
$filterent['created'] = make_config_revision_entry(null, "Easy Rule");
|
329
|
$filterent['tracker'] = (int)microtime(true);
|
330
|
$a_filter[] = $filterent;
|
331
|
|
332
|
write_config($filterent['descr']);
|
333
|
$retval = filter_configure();
|
334
|
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
|
335
|
header("Location: firewall_rules.php?if={$int}");
|
336
|
exit;
|
337
|
} else {
|
338
|
return true;
|
339
|
}
|
340
|
}
|
341
|
|
342
|
function easyrule_parse_block($int, $src, $ipproto = "inet") {
|
343
|
if (!empty($src) && !empty($int)) {
|
344
|
$src = trim($src, "[]");
|
345
|
if (!is_ipaddr($src) && !is_subnet($src)) {
|
346
|
return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
|
347
|
}
|
348
|
$int = easyrule_find_rule_interface($int);
|
349
|
if ($int === false) {
|
350
|
return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
|
351
|
}
|
352
|
if (easyrule_block_host_add($src, $int, $ipproto)) {
|
353
|
return gettext("Host added successfully");
|
354
|
} else {
|
355
|
return gettext("Failed to create block rule, alias, or add host.");
|
356
|
}
|
357
|
} else {
|
358
|
return gettext("Tried to block but had no host IP or interface");
|
359
|
}
|
360
|
return gettext("Unknown block error.");
|
361
|
}
|
362
|
|
363
|
function easyrule_parse_unblock($int, $host, $ipproto = "inet") {
|
364
|
global $blockaliasname, $config;
|
365
|
|
366
|
if (!empty($host) && !empty($int)) {
|
367
|
$host = trim($host, "[]");
|
368
|
if (!is_ipaddr($host) && !is_subnet($host)) {
|
369
|
return gettext("Tried to unblock invalid IP:") . ' ' . htmlspecialchars($host);
|
370
|
}
|
371
|
$real_int = easyrule_find_rule_interface($int);
|
372
|
if ($real_int === false) {
|
373
|
return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
|
374
|
}
|
375
|
|
376
|
/* Try to get the ID - will fail if there are no rules/alias on this interface */
|
377
|
$id = easyrule_block_alias_getid($real_int);
|
378
|
if ($id === false || !$config['aliases']['alias'][$id]) {
|
379
|
return gettext("No block rules set on interface:") . ' ' . htmlspecialchars($int);
|
380
|
}
|
381
|
|
382
|
init_config_arr(array('aliases', 'alias', $id));
|
383
|
$alias = &$config['aliases']['alias'][$id];
|
384
|
|
385
|
if (is_subnet($host)) {
|
386
|
list($host, $mask) = explode("/", $host);
|
387
|
} elseif (is_specialnet($host)) {
|
388
|
$mask = 0;
|
389
|
} elseif (is_ipaddrv6($host)) {
|
390
|
$mask = 128;
|
391
|
} else {
|
392
|
$mask = 32;
|
393
|
}
|
394
|
|
395
|
// Create the expected string representation
|
396
|
$unblock = $host.'/'.$mask;
|
397
|
|
398
|
$a_address = explode(" ", $config['aliases']['alias'][$id]['address']);
|
399
|
$a_detail = explode("||", $config['aliases']['alias'][$id]['detail']);
|
400
|
|
401
|
if (($key = array_search($unblock, $a_address)) !== false) {
|
402
|
unset($a_address[$key]);
|
403
|
unset($a_detail[$key]);
|
404
|
// Write back the result to the config array
|
405
|
$config['aliases']['alias'][$id]['address'] = join(" ", $a_address);
|
406
|
$config['aliases']['alias'][$id]['detail'] = join("||", $a_detail);
|
407
|
|
408
|
// Update config
|
409
|
write_config(sprintf(gettext("Unblocked host %s via easy rule"), $host));
|
410
|
$retval = filter_configure();
|
411
|
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
|
412
|
header("Location: firewall_aliases.php");
|
413
|
exit;
|
414
|
} else {
|
415
|
return gettext("Host unblocked successfully");
|
416
|
}
|
417
|
} else {
|
418
|
return gettext("Host is not on block list: " . $host);
|
419
|
}
|
420
|
}
|
421
|
|
422
|
return gettext("Tried to unblock but had no host IP or interface");
|
423
|
|
424
|
}
|
425
|
|
426
|
function easyrule_parse_getblock($int = 'wan', $sep = "\n") {
|
427
|
global $blockaliasname, $config;
|
428
|
|
429
|
$real_int = easyrule_find_rule_interface($int);
|
430
|
if ($real_int === false) {
|
431
|
return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
|
432
|
}
|
433
|
|
434
|
/* Try to get the ID - will fail if there are no rules/alias on this interface */
|
435
|
$id = easyrule_block_alias_getid($real_int);
|
436
|
|
437
|
if ($id === false || !$config['aliases']['alias'][$id] || empty($config['aliases']['alias'][$id]['address'])) {
|
438
|
return gettext("No block rules set on interface:") . ' ' . htmlspecialchars($int);
|
439
|
}
|
440
|
return join($sep, explode(" ", $config['aliases']['alias'][$id]['address']));
|
441
|
|
442
|
}
|
443
|
|
444
|
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
|
445
|
/* Check for valid int, srchost, dsthost, dstport, and proto */
|
446
|
global $protocols_with_ports;
|
447
|
$src = trim($src, "[]");
|
448
|
$dst = trim($dst, "[]");
|
449
|
|
450
|
if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
|
451
|
$int = easyrule_find_rule_interface($int);
|
452
|
if ($int === false) {
|
453
|
return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
|
454
|
}
|
455
|
if (getprotobyname($proto) == -1) {
|
456
|
return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
|
457
|
}
|
458
|
if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
|
459
|
return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
|
460
|
}
|
461
|
if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
|
462
|
return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
|
463
|
}
|
464
|
if (in_array($proto, $protocols_with_ports)) {
|
465
|
if (empty($dstport)) {
|
466
|
return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
|
467
|
}
|
468
|
if (!is_port($dstport) && ($dstport != "any")) {
|
469
|
return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
|
470
|
}
|
471
|
} else {
|
472
|
$dstport = 0;
|
473
|
}
|
474
|
/* Should have valid input... */
|
475
|
if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
|
476
|
return gettext("Successfully added pass rule!");
|
477
|
} else {
|
478
|
return gettext("Failed to add pass rule.");
|
479
|
}
|
480
|
} else {
|
481
|
return gettext("Missing parameters for pass rule.");
|
482
|
}
|
483
|
return gettext("Unknown pass error.");
|
484
|
}
|
485
|
|
486
|
?>
|