Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	easyrule.inc
4

    
5
	Copyright (C) 2009-2010 Jim Pingle (jpingle@gmail.com)
6
	Originally 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
$protocols_with_ports = array('tcp', 'udp');
37
require_once("functions.inc");
38
require_once("util.inc");
39
require_once("config.inc");
40

    
41
function easyrule_find_rule_interface($int) {
42
	global $config;
43
	/* Borrowed from firewall_rules.php */
44
	$iflist = get_configured_interface_with_descr(false, true);
45

    
46
	if ($config['pptpd']['mode'] == "server") {
47
		$iflist['pptp'] = "PPTP VPN";
48
	}
49

    
50
	if ($config['pppoe']['mode'] == "server") {
51
		$iflist['pppoe'] = "PPPoE Server";
52
	}
53

    
54
	if ($config['l2tp']['mode'] == "server") {
55
		$iflist['l2tp'] = "L2TP VPN";
56
	}
57

    
58
	/* add ipsec interfaces */
59
	if (isset($config['ipsec']['enable']) || isset($config['ipsec']['client']['enable'])){
60
		$iflist["enc0"] = "IPSEC";
61
	}
62

    
63
	if (isset($iflist[$int])) {
64
		return $int;
65
	}
66

    
67
	foreach ($iflist as $if => $ifd) {
68
		if (strtolower($int) == strtolower($ifd)) {
69
			return $if;
70
		}
71
	}
72

    
73
	if (substr($int, 0, 4) == "ovpn") {
74
		return "openvpn";
75
	}
76

    
77
	return false;
78
}
79

    
80
function easyrule_block_rule_exists($int = 'wan', $ipproto = "inet") {
81
	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
	foreach ($config['filter']['rule'] as $rule) {
89
		if (!is_array($rule) || !is_array($rule['source'])) {
90
			continue;
91
		}
92
		$checkproto = isset($rule['ipprotocol']) ? $rule['ipprotocol'] : "inet";
93
		if ($rule['source']['address'] == $blockaliasname . strtoupper($int) && ($rule['interface'] == $int) && ($checkproto == $ipproto)) {
94
			return true;
95
		}
96
	}
97
	return false;
98
}
99

    
100
function easyrule_block_rule_create($int = 'wan', $ipproto = "inet") {
101
	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
	if (easyrule_block_alias_getid($int) === false) {
105
		return false;
106
	}
107

    
108
	/* If the rule already exists, no need to do it again */
109
	if (easyrule_block_rule_exists($int, $ipproto)) {
110
		return true;
111
	}
112

    
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
	$filterent['interface'] = $int;
125
	$filterent['ipprotocol'] = $ipproto;
126
	$filterent['source']['address'] = $blockaliasname . strtoupper($int);
127
	$filterent['destination']['any'] = '';
128
	$filterent['descr'] = gettext("Easy Rule: Blocked from Firewall Log View");
129
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
130

    
131
	array_splice($a_filter, 0, 0, array($filterent));
132

    
133
	return true;
134
}
135

    
136
function easyrule_block_alias_getid($int = 'wan') {
137
	global $blockaliasname, $config;
138
	if (!is_array($config['aliases'])) {
139
		return false;
140
	}
141

    
142
	/* Hunt down an alias with the name we want, return its id */
143
	foreach ($config['aliases']['alias'] as $aliasid => $alias) {
144
		if ($alias['name'] == $blockaliasname . strtoupper($int)) {
145
			return $aliasid;
146
		}
147
	}
148

    
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
	$host = trim($host, "[]");
156
	if (!is_ipaddr($host) && !is_subnet($host)) {
157
		return false;
158
	}
159

    
160
	/* If there are no aliases, start an array */
161
	if (!is_array($config['aliases']['alias'])) {
162
		$config['aliases']['alias'] = array();
163
	}
164

    
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
	if ($id === false) {
170
	  unset($id);
171
	}
172

    
173
	$alias = array();
174

    
175
	if (is_subnet($host)) {
176
		list($host, $mask) = explode("/", $host);
177
	} elseif (is_specialnet($host)) {
178
		$mask = 0;
179
	} elseif (is_ipaddrv6($host)) {
180
		$mask = 128;
181
	} else {
182
		$mask = 32;
183
	}
184

    
185
	if (isset($id) && $a_aliases[$id]) {
186
		/* Make sure this IP isn't already in the list. */
187
		if (in_array($host.'/'.$mask, explode(" ", $a_aliases[$id]['address']))) {
188
			return true;
189
		}
190
		/* Since the alias already exists, just add to it. */
191
		$alias['name']    = $a_aliases[$id]['name'];
192
		$alias['type']    = $a_aliases[$id]['type'];
193
		$alias['descr']   = $a_aliases[$id]['descr'];
194

    
195
		$alias['address'] = $a_aliases[$id]['address'] . ' ' . $host . '/' . $mask;
196
		$alias['detail']  = $a_aliases[$id]['detail'] . gettext('Entry added') . ' ' . date('r') . '||';
197
	} else {
198
		/* Create a new alias with all the proper information */
199
		$alias['name']    = $blockaliasname . strtoupper($int);
200
		$alias['type']    = 'network';
201
		$alias['descr']   = gettext("Hosts blocked from Firewall Log view");
202

    
203
		$alias['address'] = $host . '/' . $mask;
204
		$alias['detail']  = gettext('Entry added') . ' ' . date('r') . '||';
205
	}
206

    
207
	/* Replace the old alias if needed, otherwise tack it on the end */
208
	if (isset($id) && $a_aliases[$id]) {
209
		$a_aliases[$id] = $alias;
210
	} else {
211
		$a_aliases[] = $alias;
212
	}
213

    
214
	// Sort list
215
	$a_aliases = msort($a_aliases, "name");
216

    
217
	return true;
218
}
219

    
220
function easyrule_block_host_add($host, $int = 'wan', $ipproto = "inet") {
221
	global $retval;
222
	/* Bail if the supplied host is not a valid IP address */
223
	$host = trim($host, "[]");
224
	if (!is_ipaddr($host) && !is_subnet($host)) {
225
		return false;
226
	}
227

    
228
	/* Flag whether or not we need to reload the filter */
229
	$dirty = false;
230

    
231
	/* Attempt to add this host to the alias */
232
	if (easyrule_block_alias_add($host, $int)) {
233
		$dirty = true;
234
	} else {
235
		/* Couldn't add the alias, or adding the host failed. */
236
		return false;
237
	}
238

    
239
	/* Attempt to add the firewall rule if it doesn't exist.
240
	 * Failing to add the rule isn't necessarily an error, it may
241
	 * have been modified by the user in some way. Adding to the
242
	 * Alias is what's important.
243
	 */
244
	if (!easyrule_block_rule_exists($int, $ipproto)) {
245
		if (easyrule_block_rule_create($int, $ipproto)) {
246
			$dirty = true;
247
		} else {
248
			return false;
249
		}
250
	}
251

    
252
	/* If needed, write the config and reload the filter */
253
	if ($dirty) {
254
		write_config();
255
		$retval = filter_configure();
256
		if (!empty($_SERVER['DOCUMENT_ROOT'])) {
257
			header("Location: firewall_aliases.php");
258
			exit;
259
		} else {
260
			return true;
261
		}
262
	} else {
263
		return false;
264
	}
265
}
266

    
267
function easyrule_pass_rule_add($int, $proto, $srchost, $dsthost, $dstport, $ipproto) {
268
	global $config;
269

    
270
	/* No rules, start a new array */
271
	if (!is_array($config['filter']['rule'])) {
272
		$config['filter']['rule'] = array();
273
	}
274

    
275
	filter_rules_sort();
276
	$a_filter = &$config['filter']['rule'];
277

    
278
	/* Make up a new rule */
279
	$filterent = array();
280
	$filterent['type'] = 'pass';
281
	$filterent['interface'] = $int;
282
	$filterent['ipprotocol'] = $ipproto;
283
	$filterent['descr'] = gettext("Easy Rule: Passed from Firewall Log View");
284

    
285
	if ($proto != "any") {
286
		$filterent['protocol'] = $proto;
287
	} else {
288
		unset($filterent['protocol']);
289
	}
290

    
291
	/* Default to only allow echo requests, since that's what most people want and
292
	 *  it should be a safe choice. */
293
	if ($proto == "icmp") {
294
		$filterent['icmptype'] = 'echoreq';
295
	}
296

    
297
	if ((strtolower($proto) == "icmp6") || (strtolower($proto) == "icmpv6")) {
298
		$filterent['protocol'] = "icmp";
299
	}
300

    
301
	if (is_subnet($srchost)) {
302
		list($srchost, $srcmask) = explode("/", $srchost);
303
	} elseif (is_specialnet($srchost)) {
304
		$srcmask = 0;
305
	} elseif (is_ipaddrv6($srchost)) {
306
		$srcmask = 128;
307
	} else {
308
		$srcmask = 32;
309
	}
310

    
311
	if (is_subnet($dsthost)) {
312
		list($dsthost, $dstmask) = explode("/", $dsthost);
313
	} elseif (is_specialnet($dsthost)) {
314
		$dstmask = 0;
315
	} elseif (is_ipaddrv6($dsthost)) {
316
		$dstmask = 128;
317
	} else {
318
		$dstmask = 32;
319
	}
320

    
321
	pconfig_to_address($filterent['source'], $srchost, $srcmask);
322
	pconfig_to_address($filterent['destination'], $dsthost, $dstmask, '', $dstport, $dstport);
323

    
324
	$filterent['created'] = make_config_revision_entry(null, gettext("Easy Rule"));
325
	$a_filter[] = $filterent;
326

    
327
	write_config($filterent['descr']);
328
	$retval = filter_configure();
329
	if (!empty($_SERVER['DOCUMENT_ROOT'])) {
330
		header("Location: firewall_rules.php?if={$int}");
331
		exit;
332
	} else {
333
		return true;
334
	}
335
}
336

    
337
function easyrule_parse_block($int, $src, $ipproto = "inet") {
338
	if (!empty($src) && !empty($int)) {
339
		$src = trim($src, "[]");
340
		if (!is_ipaddr($src) && !is_subnet($src)) {
341
			return gettext("Tried to block invalid IP:") . ' ' . htmlspecialchars($src);
342
		}
343
		$int = easyrule_find_rule_interface($int);
344
		if ($int === false) {
345
			return gettext("Invalid interface for block rule:") . ' ' . htmlspecialchars($int);
346
		}
347
		if (easyrule_block_host_add($src, $int, $ipproto)) {
348
			return gettext("Host added successfully");
349
		} else {
350
			return gettext("Failed to create block rule, alias, or add host.");
351
		}
352
	} else {
353
		return gettext("Tried to block but had no host IP or interface");
354
	}
355
	return gettext("Unknown block error.");
356
}
357
function easyrule_parse_pass($int, $proto, $src, $dst, $dstport = 0, $ipproto = "inet") {
358
	/* Check for valid int, srchost, dsthost, dstport, and proto */
359
	global $protocols_with_ports;
360
	$src = trim($src, "[]");
361
	$dst = trim($dst, "[]");
362

    
363
	if (!empty($int) && !empty($proto) && !empty($src) && !empty($dst)) {
364
		$int = easyrule_find_rule_interface($int);
365
		if ($int === false) {
366
			return gettext("Invalid interface for pass rule:") . ' ' . htmlspecialchars($int);
367
		}
368
		if (getprotobyname($proto) == -1) {
369
			return gettext("Invalid protocol for pass rule:") . ' ' . htmlspecialchars($proto);
370
		}
371
		if (!is_ipaddr($src) && !is_subnet($src) && !is_ipaddroralias($src) && !is_specialnet($src)) {
372
			return gettext("Tried to pass invalid source IP:") . ' ' . htmlspecialchars($src);
373
		}
374
		if (!is_ipaddr($dst) && !is_subnet($dst) && !is_ipaddroralias($dst) && !is_specialnet($dst)) {
375
			return gettext("Tried to pass invalid destination IP:") . ' ' . htmlspecialchars($dst);
376
		}
377
		if (in_array($proto, $protocols_with_ports)) {
378
			if (empty($dstport)) {
379
				return gettext("Missing destination port:") . ' ' . htmlspecialchars($dstport);
380
			}
381
			if (!is_port($dstport) && ($dstport != "any")) {
382
				return gettext("Tried to pass invalid destination port:") . ' ' . htmlspecialchars($dstport);
383
			}
384
		} else {
385
			$dstport = 0;
386
		}
387
		/* Should have valid input... */
388
		if (easyrule_pass_rule_add($int, $proto, $src, $dst, $dstport, $ipproto)) {
389
			return gettext("Successfully added pass rule!");
390
		} else {
391
			return gettext("Failed to add pass rule.");
392
		}
393
	} else {
394
		return gettext("Missing parameters for pass rule.");
395
	}
396
	return gettext("Unknown pass error.");
397
}
398

    
399
?>
(17-17/67)