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-2022 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
|
global $specialsrcdst;
|
33
|
$specialsrcdst = explode(" ", "any pppoe l2tp");
|
34
|
|
35
|
function easyrule_find_rule_interface($int) {
|
36
|
global $config;
|
37
|
/* Borrowed from firewall_rules.php */
|
38
|
$iflist = get_configured_interface_with_descr(true);
|
39
|
|
40
|
// add group interfaces
|
41
|
if (isset($config['ifgroups']['ifgroupentry']) && is_array($config['ifgroups']['ifgroupentry'])) {
|
42
|
foreach ($config['ifgroups']['ifgroupentry'] as $ifgen) {
|
43
|
$iflist[$ifgen['ifname']] = $ifgen['ifname'];
|
44
|
}
|
45
|
}
|
46
|
|
47
|
if (is_pppoe_server_enabled()) {
|
48
|
$iflist['pppoe'] = gettext("PPPoE Server");
|
49
|
}
|
50
|
|
51
|
if ($config['l2tp']['mode'] == "server") {
|
52
|
$iflist['l2tp'] = gettext("L2TP VPN");
|
53
|
}
|
54
|
|
55
|
/* add ipsec interfaces */
|
56
|
if (ipsec_enabled()) {
|
57
|
$iflist["enc0"] = gettext("IPsec");
|
58
|
}
|
59
|
|
60
|
if ($config['openvpn']["openvpn-server"] || $config['openvpn']["openvpn-client"]) {
|
61
|
$iflist["openvpn"] = gettext("OpenVPN");
|
62
|
}
|
63
|
|
64
|
if (isset($iflist[$int])) {
|
65
|
return $int;
|
66
|
}
|
67
|
|
68
|
foreach ($iflist as $if => $ifd) {
|
69
|
if (strtolower($int) == strtolower($ifd)) {
|
70
|
return $if;
|
71
|
}
|
72
|
}
|
73
|
|
74
|
if (substr($int, 0, 4) == "ovpn") {
|
75
|
return "openvpn";
|
76
|
}
|
77
|
if (substr($int, 0, 5) == "ipsec") {
|
78
|
return "ipsec";
|
79
|
}
|
80
|
|
81
|
return false;
|
82
|
}
|
83
|
|
84
|
function easyrule_block_rule_exists($int = 'wan', $ipproto = "inet") {
|
85
|
global $blockaliasname, $config;
|
86
|
/* No rules, we we know it doesn't exist */
|
87
|
if (!is_array($config['filter']['rule'])) {
|
88
|
return false;
|
89
|
}
|
90
|
|
91
|
/* Search through the rules for one referencing our alias */
|
92
|
foreach ($config['filter']['rule'] as $rule) {
|
93
|
if (!is_array($rule) || !is_array($rule['source'])) {
|
94
|
continue;
|
95
|
}
|
96
|
$checkproto = isset($rule['ipprotocol']) ? $rule['ipprotocol'] : "inet";
|
97
|
if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int) && ($checkproto == $ipproto)) {
|
98
|
return true;
|
99
|
}
|
100
|
}
|
101
|
return false;
|
102
|
}
|
103
|
|
104
|
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
|
105
|
global $blockaliasname, $config;
|
106
|
/* If the alias doesn't exist, exit.
|
107
|
* Can't create an empty alias, and we don't know a host */
|
108
|
if (easyrule_block_alias_getid($int) === false) {
|
109
|
return false;
|
110
|
}
|
111
|
|
112
|
/* If the rule already exists, no need to do it again */
|
113
|
if (easyrule_block_rule_exists($int, $ipproto)) {
|
114
|
return true;
|
115
|
}
|
116
|
|
117
|
init_config_arr(array('filter', 'rule'));
|
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, "Easy Rule");
|
130
|
$filterent['tracker'] = (int)microtime(true);
|
131
|
|
132
|
// Refer to firewall_rules_edit.php separators updating code.
|
133
|
// Using same code, variables, and techniques here.
|
134
|
$after = -1; // Place rule at top and move all separators.
|
135
|
array_splice($a_filter, $after+1, 0, array($filterent));
|
136
|
|
137
|
$tmpif = $int;
|
138
|
|
139
|
// Update the separators
|
140
|
init_config_arr(array('filter', 'separator', strtolower($tmpif)));
|
141
|
$a_separators = &$config['filter']['separator'][strtolower($tmpif)];
|
142
|
$ridx = ifridx($tmpif, $after); // get rule index within interface
|
143
|
$mvnrows = +1;
|
144
|
move_separators($a_separators, $ridx, $mvnrows);
|
145
|
|
146
|
return true;
|
147
|
}
|
148
|
|
149
|
function easyrule_block_alias_getid($int = 'wan') {
|
150
|
global $blockaliasname, $config;
|
151
|
if (!is_array($config['aliases'])) {
|
152
|
return false;
|
153
|
}
|
154
|
|
155
|
/* Hunt down an alias with the name we want, return its id */
|
156
|
foreach ($config['aliases']['alias'] as $aliasid => $alias) {
|
157
|
if ($alias['name'] == $blockaliasname . strtoupper($int)) {
|
158
|
return $aliasid;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
return false;
|
163
|
}
|
164
|
|
165
|
function easyrule_block_alias_add($host, $int = 'wan') {
|
166
|
global $blockaliasname, $config;
|
167
|
/* If the host isn't a valid IP address, bail */
|
168
|
$host = trim($host, "[]");
|
169
|
if (!is_ipaddr($host) && !is_subnet($host)) {
|
170
|
return false;
|
171
|
}
|
172
|
|
173
|
init_config_arr(array('aliases', 'alias'));
|
174
|
$a_aliases = &$config['aliases']['alias'];
|
175
|
|
176
|
/* Try to get the ID if the alias already exists */
|
177
|
$id = easyrule_block_alias_getid($int);
|
178
|
if ($id === false) {
|
179
|
unset($id);
|
180
|
}
|
181
|
|
182
|
$alias = array();
|
183
|
|
184
|
if (is_subnet($host)) {
|
185
|
list($host, $mask) = explode("/", $host);
|
186
|
} elseif (is_specialnet($host)) {
|
187
|
$mask = 0;
|
188
|
} elseif (is_ipaddrv6($host)) {
|
189
|
$mask = 128;
|
190
|
} else {
|
191
|
$mask = 32;
|
192
|
}
|
193
|
|
194
|
if (isset($id) && $a_aliases[$id]) {
|
195
|
|
196
|
// Catch case when the list is empty
|
197
|
if (empty($a_aliases[$id]['address'])) {
|
198
|
$a_address = array();
|
199
|
$a_detail = array();
|
200
|
} else {
|
201
|
$a_address = explode(" ", $a_aliases[$id]['address']);
|
202
|
|
203
|
/* Make sure this IP isn't already in the list. */
|
204
|
if (in_array($host.'/'.$mask, $a_address)) {
|
205
|
return true;
|
206
|
}
|
207
|
$a_detail = explode("||", $a_aliases[$id]['detail']);
|
208
|
}
|
209
|
|
210
|
/* Since the alias already exists, just add to it. */
|
211
|
$alias['name'] = $a_aliases[$id]['name'];
|
212
|
$alias['type'] = $a_aliases[$id]['type'];
|
213
|
$alias['descr'] = $a_aliases[$id]['descr'];
|
214
|
|
215
|
$a_address[] = $host.'/'.$mask;
|
216
|
$a_detail[] = gettext('Entry added') . ' ' . date('r');
|
217
|
|
218
|
$alias['address'] = join(" ", $a_address);
|
219
|
$alias['detail'] = join("||", $a_detail);
|
220
|
|
221
|
} else {
|
222
|
/* Create a new alias with all the proper information */
|
223
|
$alias['name'] = $blockaliasname . strtoupper($int);
|
224
|
$alias['type'] = 'network';
|
225
|
$alias['descr'] = gettext("Hosts blocked from Firewall Log view");
|
226
|
|
227
|
$alias['address'] = $host . '/' . $mask;
|
228
|
$alias['detail'] = gettext('Entry added') . ' ' . date('r') . '||';
|
229
|
}
|
230
|
|
231
|
/* Replace the old alias if needed, otherwise tack it on the end */
|
232
|
if (isset($id) && $a_aliases[$id]) {
|
233
|
$a_aliases[$id] = $alias;
|
234
|
} else {
|
235
|
$a_aliases[] = $alias;
|
236
|
}
|
237
|
|
238
|
// Sort list
|
239
|
$a_aliases = msort($a_aliases, "name");
|
240
|
|
241
|
return true;
|
242
|
}
|
243
|
|
244
|
function easyrule_block_host_add($host, $int = 'wan') {
|
245
|
global $retval;
|
246
|
/* Bail if the supplied host is not a valid IP address */
|
247
|
$host = trim($host, "[]");
|
248
|
if (!is_ipaddr($host) && !is_subnet($host)) {
|
249
|
return false;
|
250
|
}
|
251
|
|
252
|
if (is_v6($host)) {
|
253
|
$ipproto = 'inet6';
|
254
|
} else {
|
255
|
$ipproto = 'inet';
|
256
|
}
|
257
|
|
258
|
/* Flag whether or not we need to reload the filter */
|
259
|
$dirty = false;
|
260
|
|
261
|
/* Attempt to add this host to the alias */
|
262
|
if (easyrule_block_alias_add($host, $int)) {
|
263
|
$dirty = true;
|
264
|
} else {
|
265
|
/* Couldn't add the alias, or adding the host failed. */
|
266
|
return false;
|
267
|
}
|
268
|
|
269
|
/* Attempt to add the firewall rule if it doesn't exist.
|
270
|
* Failing to add the rule isn't necessarily an error, it may
|
271
|
* have been modified by the user in some way. Adding to the
|
272
|
* Alias is what's important.
|
273
|
*/
|
274
|
if (!easyrule_block_rule_exists($int, $ipproto)) {
|
275
|
if (easyrule_block_rule_create($int, $ipproto)) {
|
276
|
$dirty = true;
|
277
|
} else {
|
278
|
return false;
|
279
|
}
|
280
|
}
|
281
|
|
282
|
/* If needed, write the config and reload the filter */
|
283
|
if ($dirty) {
|
284
|
write_config(sprintf(gettext("Blocked host %s via easy rule"), $host));
|
285
|
$retval = filter_configure();
|
286
|
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
|
287
|
header("Location: firewall_aliases.php");
|
288
|
exit;
|
289
|
} else {
|
290
|
return true;
|
291
|
}
|
292
|
} else {
|
293
|
return false;
|
294
|
}
|
295
|
}
|
296
|
|
297
|
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
|
298
|
global $config;
|
299
|
|
300
|
init_config_arr(array('filter', 'rule'));
|
301
|
filter_rules_sort();
|
302
|
$a_filter = &$config['filter']['rule'];
|
303
|
|
304
|
/* Make up a new rule */
|
305
|
$filterent = array();
|
306
|
$filterent['type'] = 'pass';
|
307
|
$filterent['interface'] = $int;
|
308
|
$filterent['ipprotocol'] = $ipproto;
|
309
|
$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
|
310
|
|
311
|
if ($proto != "any") {
|
312
|
$filterent['protocol'] = $proto;
|
313
|
} else {
|
314
|
unset($filterent['protocol']);
|
315
|
}
|
316
|
|
317
|
/* Default to only allow echo requests, since that's what most people want and
|
318
|
* it should be a safe choice. */
|
319
|
if ($proto == "icmp") {
|
320
|
$filterent['icmptype'] = 'echoreq';
|
321
|
}
|
322
|
|
323
|
if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6")) {
|
324
|
$filterent['protocol'] = "icmp";
|
325
|
}
|
326
|
|
327
|
if (is_subnet($srchost)) {
|
328
|
list($srchost, $srcmask) = explode("/", $srchost);
|
329
|
} elseif (is_specialnet($srchost)) {
|
330
|
$srcmask = 0;
|
331
|
} elseif (is_ipaddrv6($srchost)) {
|
332
|
$srcmask = 128;
|
333
|
} else {
|
334
|
$srcmask = 32;
|
335
|
}
|
336
|
|
337
|
if (is_subnet($dsthost)) {
|
338
|
list($dsthost, $dstmask) = explode("/", $dsthost);
|
339
|
} elseif (is_specialnet($dsthost)) {
|
340
|
$dstmask = 0;
|
341
|
} elseif (is_ipaddrv6($dsthost)) {
|
342
|
$dstmask = 128;
|
343
|
} else {
|
344
|
$dstmask = 32;
|
345
|
}
|
346
|
|
347
|
pconfig_to_address($filterent['source'], $srchost, $srcmask);
|
348
|
pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
|
349
|
|
350
|
$filterent['created'] = make_config_revision_entry(null, "Easy Rule");
|
351
|
$filterent['tracker'] = (int)microtime(true);
|
352
|
$a_filter[] = $filterent;
|
353
|
|
354
|
write_config($filterent['descr']);
|
355
|
$retval = filter_configure();
|
356
|
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
|
357
|
header("Location: firewall_rules.php?if={$int}");
|
358
|
exit;
|
359
|
} else {
|
360
|
return true;
|
361
|
}
|
362
|
}
|
363
|
|
364
|
function easyrule_parse_block($int, $src) {
|
365
|
if (!empty($src) && !empty($int)) {
|
366
|
$src = trim($src, "[]");
|
367
|
if (!is_ipaddr($src) && !is_subnet($src)) {
|
368
|
return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
|
369
|
}
|
370
|
$int = easyrule_find_rule_interface($int);
|
371
|
if ($int === false) {
|
372
|
return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
|
373
|
}
|
374
|
if (easyrule_block_host_add($src, $int)) {
|
375
|
return gettext("Host added successfully");
|
376
|
} else {
|
377
|
return gettext("Failed to create block rule, alias, or add host.");
|
378
|
}
|
379
|
} else {
|
380
|
return gettext("Tried to block but had no host IP or interface");
|
381
|
}
|
382
|
return gettext("Unknown block error.");
|
383
|
}
|
384
|
|
385
|
function easyrule_parse_unblock($int, $host) {
|
386
|
global $blockaliasname, $config;
|
387
|
|
388
|
if (!empty($host) && !empty($int)) {
|
389
|
$host = trim($host, "[]");
|
390
|
if (!is_ipaddr($host) && !is_subnet($host)) {
|
391
|
return gettext("Tried to unblock invalid IP:") . ' ' . htmlspecialchars($host);
|
392
|
}
|
393
|
$real_int = easyrule_find_rule_interface($int);
|
394
|
if ($real_int === false) {
|
395
|
return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
|
396
|
}
|
397
|
|
398
|
/* Try to get the ID - will fail if there are no rules/alias on this interface */
|
399
|
$id = easyrule_block_alias_getid($real_int);
|
400
|
if ($id === false || !$config['aliases']['alias'][$id]) {
|
401
|
return gettext("No block rules set on interface:") . ' ' . htmlspecialchars($int);
|
402
|
}
|
403
|
|
404
|
init_config_arr(array('aliases', 'alias', $id));
|
405
|
$alias = &$config['aliases']['alias'][$id];
|
406
|
|
407
|
if (is_subnet($host)) {
|
408
|
list($host, $mask) = explode("/", $host);
|
409
|
} elseif (is_specialnet($host)) {
|
410
|
$mask = 0;
|
411
|
} elseif (is_ipaddrv6($host)) {
|
412
|
$mask = 128;
|
413
|
} else {
|
414
|
$mask = 32;
|
415
|
}
|
416
|
|
417
|
// Create the expected string representation
|
418
|
$unblock = $host.'/'.$mask;
|
419
|
|
420
|
$a_address = explode(" ", $config['aliases']['alias'][$id]['address']);
|
421
|
$a_detail = explode("||", $config['aliases']['alias'][$id]['detail']);
|
422
|
|
423
|
if (($key = array_search($unblock, $a_address)) !== false) {
|
424
|
unset($a_address[$key]);
|
425
|
unset($a_detail[$key]);
|
426
|
// Write back the result to the config array
|
427
|
$config['aliases']['alias'][$id]['address'] = join(" ", $a_address);
|
428
|
$config['aliases']['alias'][$id]['detail'] = join("||", $a_detail);
|
429
|
|
430
|
// Update config
|
431
|
write_config(sprintf(gettext("Unblocked host %s via easy rule"), $host));
|
432
|
$retval = filter_configure();
|
433
|
if (!empty($_SERVER['DOCUMENT_ROOT'])) {
|
434
|
header("Location: firewall_aliases.php");
|
435
|
exit;
|
436
|
} else {
|
437
|
return gettext("Host unblocked successfully");
|
438
|
}
|
439
|
} else {
|
440
|
return gettext("Host is not on block list: " . $host);
|
441
|
}
|
442
|
}
|
443
|
|
444
|
return gettext("Tried to unblock but had no host IP or interface");
|
445
|
|
446
|
}
|
447
|
|
448
|
function easyrule_parse_getblock($int = 'wan', $sep = "\n") {
|
449
|
global $blockaliasname, $config;
|
450
|
|
451
|
$real_int = easyrule_find_rule_interface($int);
|
452
|
if ($real_int === false) {
|
453
|
return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
|
454
|
}
|
455
|
|
456
|
/* Try to get the ID - will fail if there are no rules/alias on this interface */
|
457
|
$id = easyrule_block_alias_getid($real_int);
|
458
|
|
459
|
if ($id === false || !$config['aliases']['alias'][$id] || empty($config['aliases']['alias'][$id]['address'])) {
|
460
|
return gettext("No block rules set on interface:") . ' ' . htmlspecialchars($int);
|
461
|
}
|
462
|
return join($sep, explode(" ", $config['aliases']['alias'][$id]['address']));
|
463
|
|
464
|
}
|
465
|
|
466
|
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
|
467
|
/* Check for valid int, srchost, dsthost, dstport, and proto */
|
468
|
global $protocols_with_ports;
|
469
|
$src = trim($src, "[]");
|
470
|
$dst = trim($dst, "[]");
|
471
|
|
472
|
if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
|
473
|
$int = easyrule_find_rule_interface($int);
|
474
|
if ($int === false) {
|
475
|
return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
|
476
|
}
|
477
|
if (getprotobyname($proto) == -1) {
|
478
|
return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
|
479
|
}
|
480
|
if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
|
481
|
return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
|
482
|
}
|
483
|
if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
|
484
|
return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
|
485
|
}
|
486
|
if ((is_v6($src) && is_v4($dst)) || (is_v4($src) && is_v6($dst))) {
|
487
|
return gettext("The source IP address family has to match the family of the destination IP address.");
|
488
|
}
|
489
|
if (is_v6($src)) {
|
490
|
$ipproto = 'inet6';
|
491
|
} else {
|
492
|
$ipproto = 'inet';
|
493
|
}
|
494
|
if (in_array($proto, $protocols_with_ports)) {
|
495
|
if (empty($dstport)) {
|
496
|
return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
|
497
|
}
|
498
|
if (!is_port($dstport) && ($dstport != "any")) {
|
499
|
return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
|
500
|
}
|
501
|
} else {
|
502
|
$dstport = 0;
|
503
|
}
|
504
|
/* Should have valid input... */
|
505
|
if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
|
506
|
return gettext("Successfully added pass rule!");
|
507
|
} else {
|
508
|
return gettext("Failed to add pass rule.");
|
509
|
}
|
510
|
} else {
|
511
|
return gettext("Missing parameters for pass rule.");
|
512
|
}
|
513
|
return gettext("Unknown pass error.");
|
514
|
}
|
515
|
|
516
|
?>
|