Project

General

Profile

Download (7.41 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
	if (substr($int, 0, 4) == "ovpn")
65
		return "openvpn";
66

    
67
	return false;
68
}
69

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

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

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

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

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

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

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

    
111
	$a_filter[] = $filterent;
112

    
113
	return true;
114
}
115

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

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

    
126
	return false;
127
}
128

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

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

    
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
	// Sort list
176
	$a_aliases = msort($a_aliases, "name");
177

    
178
	return true;
179
}
180

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

    
187
	/* Flag whether or not we need to reload the filter */
188
	$dirty = false;
189

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

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

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

    
222
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport) {
223
	global $config;
224

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

    
230
	filter_rules_sort();
231
	$a_filter = &$config['filter']['rule'];
232

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

    
239
	if ($proto != "any")
240
		$filterent['protocol'] = $proto;
241
	else
242
		unset($filterent['protocol']);
243

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

    
249
	pconfig_to_address($filterent['source'], $srchost, 32);
250
	pconfig_to_address($filterent['destination'], $dsthost, 32, '', $dstport, $dstport);
251

    
252
	$a_filter[] = $filterent;
253

    
254
	write_config();
255
	$retval = filter_configure();
256
	header("Location: firewall_rules.php?if={$int}");
257
	exit;
258
}
259
?>
(14-14/52)