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