Project

General

Profile

Download (7.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
$specialsrcdst = explode(" ", "any wanip lanip lan pptp pppoe");
33

    
34
function easyrule_find_rule_interface($int) {
35
	global $config;
36
	/* Borrowed from firewall_rules.php */
37
	$iflist = array("lan" => "LAN", "wan" => "WAN");
38
	
39
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
40
		$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
41
	}
42
	
43
	if ($config['pptpd']['mode'] == "server")
44
		$iflist['pptp'] = "PPTP VPN";
45
	
46
	if ($config['pppoe']['mode'] == "server")
47
		$iflist['pppoe'] = "PPPoE VPN";
48
	
49
	/* add ipsec interfaces */
50
	if (isset($config['ipsec']['enable']) || isset($config['ipsec']['mobileclients']['enable'])){ 
51
		$iflist["enc0"] = "IPSEC";
52
	}
53
	
54
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
55
		$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
56
	}
57

    
58
	if (isset($iflist[$int]))
59
		return $int;
60

    
61
	foreach ($iflist as $if => $ifd) {
62
		if (strtolower($int) == strtolower($ifd))
63
			return $if;
64
	}
65
	
66
	return false;
67
}
68

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

    
76
	/* Search through the rules for one referencing our alias */
77
	foreach ($config['filter']['rule'] as $rule)
78
		if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int))
79
			return true;
80
	return false;
81
}
82

    
83
function easyrule_block_rule_create($int = 'wan') {
84
	global $blockaliasname, $config;
85
	/* If the alias doesn't exist, exit.
86
	 * Can't create an empty alias, and we don't know a host */
87
	if (easyrule_block_alias_getid($int) === false)
88
		return false;
89

    
90
	/* If the rule already exists, no need to do it again */
91
	if (easyrule_block_rule_exists($int))
92
		return true;
93

    
94
	/* No rules, start a new array */
95
	if (!is_array($config['filter']['rule'])) {
96
		$config['filter']['rule'] = array();
97
	}
98

    
99
	filter_rules_sort();
100
	$a_filter = &$config['filter']['rule'];
101

    
102
	/* Make up a new rule */
103
	$filterent = array();
104
	$filterent['type'] = 'block';
105
	$filterent['interface'] = $int; 
106
	$filterent['source']['address'] = $blockaliasname . strtoupper($int);
107
	$filterent['destination']['any'] = '';
108
	$filterent['descr'] = "Easy Rule: Blocked from Firewall Log View";
109

    
110
	$a_filter[] = $filterent;
111

    
112
	return true;
113
}
114

    
115
function easyrule_block_alias_getid($int = 'wan') {
116
	global $blockaliasname, $config;
117
	if (!is_array($config['aliases']))
118
		return false;
119

    
120
	/* Hunt down an alias with the name we want, return its id */
121
	foreach ($config['aliases']['alias'] as $aliasid => $alias)
122
		if ($alias['name'] == $blockaliasname . strtoupper($int))
123
			return $aliasid;
124

    
125
	return false;
126
}
127

    
128
function easyrule_block_alias_add($host, $int = 'wan') {
129
	global $blockaliasname, $config;
130
	/* If the host isn't a valid IP address, bail */
131
	if (!is_ipaddr($host))
132
		return false;
133

    
134
	/* If there are no aliases, start an array */
135
	if (!is_array($config['aliases']['alias']))
136
		$config['aliases']['alias'] = array();
137

    
138
	aliases_sort();
139
	$a_aliases = &$config['aliases']['alias'];
140

    
141
	/* Try to get the ID if the alias already exists */
142
	$id = easyrule_block_alias_getid($int);
143
	if ($id === false)
144
	  unset($id);
145

    
146
	$alias = array();
147

    
148
	if (isset($id) && $a_aliases[$id]) {
149
		/* Make sure this IP isn't already in the list. */
150
		if (in_array($host.'/32', explode(" ", $a_aliases[$id]['address'])))
151
			return true;
152
		/* Since the alias already exists, just add to it. */
153
		$alias['name']    = $a_aliases[$id]['name'];
154
		$alias['type']    = $a_aliases[$id]['type'];
155
		$alias['descr']   = $a_aliases[$id]['descr'];
156

    
157
		$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/32';
158
	 	$alias['detail']  = $a_aliases[$id]['detail'] . 'Entry added ' . date('r') . '||';
159
	} else {
160
		/* Create a new alias with all the proper information */
161
	 	$alias['name']    = $blockaliasname . strtoupper($int);
162
	 	$alias['type']    = 'network';
163
	 	$alias['descr']   = mb_convert_encoding("Hosts blocked from Firewall Log view","HTML-ENTITIES","auto");
164

    
165
		$alias['address'] = $host . '/32';
166
	 	$alias['detail']  = 'Entry added ' . date('r') . '||';
167
	}
168

    
169
	/* Replace the old alias if needed, otherwise tack it on the end */
170
	if (isset($id) && $a_aliases[$id])
171
		$a_aliases[$id] = $alias;
172
	else
173
		$a_aliases[] = $alias;
174

    
175
	return true;
176
}
177

    
178
function easyrule_block_host_add($host, $int = 'wan') {
179
	global $retval;
180
	/* Bail if the supplied host is not a valid IP address */
181
	if (!is_ipaddr($host))
182
		return false;
183

    
184
	/* Flag whether or not we need to reload the filter */
185
	$dirty = false;
186

    
187
	/* Attempt to add this host to the alias */
188
	if (easyrule_block_alias_add($host, $int)) {
189
		$dirty = true;
190
	} else {
191
		/* Couldn't add the alias, or adding the host failed. */
192
		return false;
193
	}
194

    
195
	/* Attempt to add the firewall rule if it doesn't exist.
196
	 * Failing to add the rule isn't necessarily an error, it may
197
	 * have been modified by the user in some way. Adding to the
198
	 * Alias is what's important.
199
	 */
200
	if (!easyrule_block_rule_exists($int)) {
201
		if (easyrule_block_rule_create($int)) {
202
			$dirty = true;
203
		} else {
204
			return false;
205
		}
206
	}
207

    
208
	/* If needed, write the config and reload the filter */
209
	if ($dirty) {
210
		write_config();
211
		$retval = filter_configure();
212
		header("Location: firewall_aliases.php");
213
		exit;
214
	} else {
215
		return false;
216
	}
217
}
218

    
219
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport) {
220
	global $config;
221

    
222
	/* No rules, start a new array */
223
	if (!is_array($config['filter']['rule'])) {
224
		$config['filter']['rule'] = array();
225
	}
226

    
227
	filter_rules_sort();
228
	$a_filter = &$config['filter']['rule'];
229

    
230
	/* Make up a new rule */
231
	$filterent = array();
232
	$filterent['type'] = 'pass';
233
	$filterent['interface'] = $int;
234
	$filterent['descr'] = "Easy Rule: Passed from Firewall Log View";
235

    
236
	if ($proto != "any")
237
		$filterent['protocol'] = $proto;
238
	else
239
		unset($filterent['protocol']);
240

    
241
	/* Default to only allow echo requests, since that's what most people want and
242
	 *  it should be a safe choice. */
243
	if ($proto == "icmp")
244
		$filterent['icmptype'] = 'echoreq';
245

    
246
	pconfig_to_address($filterent['source'], $srchost, 32);
247
	pconfig_to_address($filterent['destination'], $dsthost, 32, '', $dstport, $dstport);
248

    
249
	$a_filter[] = $filterent;
250

    
251
	write_config();
252
	$retval = filter_configure();
253
	header("Location: firewall_rules.php?if={$int}");
254
	exit;
255
}
256
?>
(36-36/217)