1 |
c0b6fdde
|
jim-p
|
<?php
|
2 |
|
|
/*
|
3 |
|
|
easyrule.inc.php
|
4 |
|
|
|
5 |
|
|
Copyright (C) 2009 Jim Pingle (jpingle@gmail.com)
|
6 |
|
|
Sponsored By Anathematic @ pfSense Forums
|
7 |
|
|
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 |
|
|
|
31 |
|
|
$blockaliasname = 'EasyRuleBlockHosts';
|
32 |
4d828a9a
|
Ermal Lu?i
|
|
33 |
c0b6fdde
|
jim-p
|
|
34 |
|
|
function easyrule_find_rule_interface($int) {
|
35 |
|
|
global $config;
|
36 |
|
|
/* Borrowed from firewall_rules.php */
|
37 |
4d828a9a
|
Ermal Lu?i
|
$iflist = get_configured_interface_with_descr(false, true);
|
38 |
c0b6fdde
|
jim-p
|
|
39 |
|
|
if ($config['pptpd']['mode'] == "server")
|
40 |
|
|
$iflist['pptp'] = "PPTP VPN";
|
41 |
|
|
|
42 |
|
|
if ($config['pppoe']['mode'] == "server")
|
43 |
|
|
$iflist['pppoe'] = "PPPoE VPN";
|
44 |
|
|
|
45 |
4d828a9a
|
Ermal Lu?i
|
if ($config['l2tp']['mode'] == "server")
|
46 |
|
|
$iflist['l2tp'] = "L2TP VPN";
|
47 |
|
|
|
48 |
c0b6fdde
|
jim-p
|
/* add ipsec interfaces */
|
49 |
|
|
if (isset($config['ipsec']['enable']) || isset($config['ipsec']['mobileclients']['enable'])){
|
50 |
|
|
$iflist["enc0"] = "IPSEC";
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
if (isset($iflist[$int]))
|
54 |
|
|
return $int;
|
55 |
|
|
|
56 |
|
|
foreach ($iflist as $if => $ifd) {
|
57 |
|
|
if (strtolower($int) == strtolower($ifd))
|
58 |
|
|
return $if;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
return false;
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
function easyrule_block_rule_exists($int = 'wan') {
|
65 |
|
|
global $blockaliasname, $config;
|
66 |
|
|
/* No rules, we we know it doesn't exist */
|
67 |
|
|
if (!is_array($config['filter']['rule'])) {
|
68 |
|
|
return false;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
/* Search through the rules for one referencing our alias */
|
72 |
|
|
foreach ($config['filter']['rule'] as $rule)
|
73 |
|
|
if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int))
|
74 |
|
|
return true;
|
75 |
|
|
return false;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
function easyrule_block_rule_create($int = 'wan') {
|
79 |
|
|
global $blockaliasname, $config;
|
80 |
|
|
/* If the alias doesn't exist, exit.
|
81 |
|
|
* Can't create an empty alias, and we don't know a host */
|
82 |
|
|
if (easyrule_block_alias_getid($int) === false)
|
83 |
|
|
return false;
|
84 |
|
|
|
85 |
|
|
/* If the rule already exists, no need to do it again */
|
86 |
|
|
if (easyrule_block_rule_exists($int))
|
87 |
|
|
return true;
|
88 |
|
|
|
89 |
|
|
/* No rules, start a new array */
|
90 |
|
|
if (!is_array($config['filter']['rule'])) {
|
91 |
|
|
$config['filter']['rule'] = array();
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
filter_rules_sort();
|
95 |
|
|
$a_filter = &$config['filter']['rule'];
|
96 |
|
|
|
97 |
|
|
/* Make up a new rule */
|
98 |
|
|
$filterent = array();
|
99 |
|
|
$filterent['type'] = 'block';
|
100 |
|
|
$filterent['interface'] = $int;
|
101 |
|
|
$filterent['source']['address'] = $blockaliasname . strtoupper($int);
|
102 |
|
|
$filterent['destination']['any'] = '';
|
103 |
|
|
$filterent['descr'] = "Easy Rule: Blocked from Firewall Log View";
|
104 |
|
|
|
105 |
|
|
$a_filter[] = $filterent;
|
106 |
|
|
|
107 |
|
|
return true;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
function easyrule_block_alias_getid($int = 'wan') {
|
111 |
|
|
global $blockaliasname, $config;
|
112 |
|
|
if (!is_array($config['aliases']))
|
113 |
|
|
return false;
|
114 |
|
|
|
115 |
|
|
/* Hunt down an alias with the name we want, return its id */
|
116 |
|
|
foreach ($config['aliases']['alias'] as $aliasid => $alias)
|
117 |
|
|
if ($alias['name'] == $blockaliasname . strtoupper($int))
|
118 |
|
|
return $aliasid;
|
119 |
|
|
|
120 |
|
|
return false;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
function easyrule_block_alias_add($host, $int = 'wan') {
|
124 |
|
|
global $blockaliasname, $config;
|
125 |
|
|
/* If the host isn't a valid IP address, bail */
|
126 |
|
|
if (!is_ipaddr($host))
|
127 |
|
|
return false;
|
128 |
|
|
|
129 |
|
|
/* If there are no aliases, start an array */
|
130 |
|
|
if (!is_array($config['aliases']['alias']))
|
131 |
|
|
$config['aliases']['alias'] = array();
|
132 |
|
|
|
133 |
|
|
aliases_sort();
|
134 |
|
|
$a_aliases = &$config['aliases']['alias'];
|
135 |
|
|
|
136 |
|
|
/* Try to get the ID if the alias already exists */
|
137 |
|
|
$id = easyrule_block_alias_getid($int);
|
138 |
|
|
if ($id === false)
|
139 |
|
|
unset($id);
|
140 |
|
|
|
141 |
|
|
$alias = array();
|
142 |
|
|
|
143 |
|
|
if (isset($id) && $a_aliases[$id]) {
|
144 |
|
|
/* Make sure this IP isn't already in the list. */
|
145 |
|
|
if (in_array($host.'/32', explode(" ", $a_aliases[$id]['address'])))
|
146 |
|
|
return true;
|
147 |
|
|
/* Since the alias already exists, just add to it. */
|
148 |
|
|
$alias['name'] = $a_aliases[$id]['name'];
|
149 |
|
|
$alias['type'] = $a_aliases[$id]['type'];
|
150 |
|
|
$alias['descr'] = $a_aliases[$id]['descr'];
|
151 |
|
|
|
152 |
|
|
$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/32';
|
153 |
|
|
$alias['detail'] = $a_aliases[$id]['detail'] . 'Entry added ' . date('r') . '||';
|
154 |
|
|
} else {
|
155 |
|
|
/* Create a new alias with all the proper information */
|
156 |
|
|
$alias['name'] = $blockaliasname . strtoupper($int);
|
157 |
|
|
$alias['type'] = 'network';
|
158 |
|
|
$alias['descr'] = mb_convert_encoding("Hosts blocked from Firewall Log view","HTML-ENTITIES","auto");
|
159 |
|
|
|
160 |
|
|
$alias['address'] = $host . '/32';
|
161 |
|
|
$alias['detail'] = 'Entry added ' . date('r') . '||';
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
/* Replace the old alias if needed, otherwise tack it on the end */
|
165 |
|
|
if (isset($id) && $a_aliases[$id])
|
166 |
|
|
$a_aliases[$id] = $alias;
|
167 |
|
|
else
|
168 |
|
|
$a_aliases[] = $alias;
|
169 |
|
|
|
170 |
|
|
return true;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
function easyrule_block_host_add($host, $int = 'wan') {
|
174 |
|
|
global $retval;
|
175 |
|
|
/* Bail if the supplied host is not a valid IP address */
|
176 |
|
|
if (!is_ipaddr($host))
|
177 |
|
|
return false;
|
178 |
|
|
|
179 |
|
|
/* Flag whether or not we need to reload the filter */
|
180 |
|
|
$dirty = false;
|
181 |
|
|
|
182 |
|
|
/* Attempt to add this host to the alias */
|
183 |
|
|
if (easyrule_block_alias_add($host, $int)) {
|
184 |
|
|
$dirty = true;
|
185 |
|
|
} else {
|
186 |
|
|
/* Couldn't add the alias, or adding the host failed. */
|
187 |
|
|
return false;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
/* Attempt to add the firewall rule if it doesn't exist.
|
191 |
|
|
* Failing to add the rule isn't necessarily an error, it may
|
192 |
|
|
* have been modified by the user in some way. Adding to the
|
193 |
|
|
* Alias is what's important.
|
194 |
|
|
*/
|
195 |
|
|
if (!easyrule_block_rule_exists($int)) {
|
196 |
|
|
if (easyrule_block_rule_create($int)) {
|
197 |
|
|
$dirty = true;
|
198 |
|
|
} else {
|
199 |
|
|
return false;
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
/* If needed, write the config and reload the filter */
|
204 |
|
|
if ($dirty) {
|
205 |
|
|
write_config();
|
206 |
|
|
$retval = filter_configure();
|
207 |
|
|
header("Location: firewall_aliases.php");
|
208 |
|
|
exit;
|
209 |
|
|
} else {
|
210 |
|
|
return false;
|
211 |
|
|
}
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport) {
|
215 |
|
|
global $config;
|
216 |
|
|
|
217 |
|
|
/* No rules, start a new array */
|
218 |
|
|
if (!is_array($config['filter']['rule'])) {
|
219 |
|
|
$config['filter']['rule'] = array();
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
filter_rules_sort();
|
223 |
|
|
$a_filter = &$config['filter']['rule'];
|
224 |
|
|
|
225 |
|
|
/* Make up a new rule */
|
226 |
|
|
$filterent = array();
|
227 |
|
|
$filterent['type'] = 'pass';
|
228 |
|
|
$filterent['interface'] = $int;
|
229 |
|
|
$filterent['descr'] = "Easy Rule: Passed from Firewall Log View";
|
230 |
|
|
|
231 |
|
|
if ($proto != "any")
|
232 |
|
|
$filterent['protocol'] = $proto;
|
233 |
|
|
else
|
234 |
|
|
unset($filterent['protocol']);
|
235 |
|
|
|
236 |
|
|
/* Default to only allow echo requests, since that's what most people want and
|
237 |
|
|
* it should be a safe choice. */
|
238 |
|
|
if ($proto == "icmp")
|
239 |
|
|
$filterent['icmptype'] = 'echoreq';
|
240 |
|
|
|
241 |
|
|
pconfig_to_address($filterent['source'], $srchost, 32);
|
242 |
|
|
pconfig_to_address($filterent['destination'], $dsthost, 32, '', $dstport, $dstport);
|
243 |
|
|
|
244 |
|
|
$a_filter[] = $filterent;
|
245 |
|
|
|
246 |
|
|
write_config();
|
247 |
|
|
$retval = filter_configure();
|
248 |
|
|
header("Location: firewall_rules.php?if={$int}");
|
249 |
|
|
exit;
|
250 |
|
|
}
|
251 |
ab0dbe3c
|
Ermal Lu?i
|
?>
|