Project

General

Profile

Download (7.33 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
	pfSense_BUILDER_BINARIES:	
32
	pfSense_MODULE:	filter
33
*/
34

    
35
$blockaliasname = 'EasyRuleBlockHosts';
36

    
37
function easyrule_find_rule_interface($int) {
38
	global $config;
39
	/* Borrowed from firewall_rules.php */
40
	$iflist = get_configured_interface_with_descr(false, true);
41
	
42
	if ($config['pptpd']['mode'] == "server")
43
		$iflist['pptp'] = "PPTP VPN";
44
	
45
	if ($config['pppoe']['mode'] == "server")
46
		$iflist['pppoe'] = "PPPoE VPN";
47
	
48
	if ($config['l2tp']['mode'] == "server")
49
                $iflist['l2tp'] = "L2TP VPN";
50

    
51
	/* add ipsec interfaces */
52
	if (isset($config['ipsec']['enable']) || isset($config['ipsec']['mobileclients']['enable'])){ 
53
		$iflist["enc0"] = "IPSEC";
54
	}
55
	
56
	if (isset($iflist[$int]))
57
		return $int;
58

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

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

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

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

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

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

    
97
	filter_rules_sort();
98
	$a_filter = &$config['filter']['rule'];
99

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

    
108
	$a_filter[] = $filterent;
109

    
110
	return true;
111
}
112

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

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

    
123
	return false;
124
}
125

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

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

    
136
	aliases_sort();
137
	$a_aliases = &$config['aliases']['alias'];
138

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

    
144
	$alias = array();
145

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

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

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

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

    
173
	return true;
174
}
175

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

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

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

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

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

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

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

    
225
	filter_rules_sort();
226
	$a_filter = &$config['filter']['rule'];
227

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

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

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

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

    
247
	$a_filter[] = $filterent;
248

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