Project

General

Profile

Download (3.98 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -q
2
<?php
3
/*
4
 * easyrule
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2010-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25

    
26
require_once("pfsense-utils.inc");
27
require_once("easyrule.inc");
28
require_once("filter.inc");
29
require_once("shaper.inc");
30

    
31
$message = "";
32
$specialsrcdst = explode(" ", "any pptp pppoe l2tp openvpn");
33
$ifdisp = get_configured_interface_with_descr();
34
foreach ($ifdisp as $kif => $kdescr) {
35
	$specialsrcdst[] = "{$kif}";
36
	$specialsrcdst[] = "{$kif}ip";
37
}
38

    
39
/* Borrow this function from guiconfig.inc since we can't include it for use at the CLI
40

    
41
 - Maybe these need to be moved to util.inc or pfsense-utils.inc?
42

    
43
*/
44
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
45

    
46
	$adr = array();
47

    
48
	if ($padr == "any") {
49
		$adr['any'] = true;
50
	} else if (is_specialnet($padr)) {
51
		$adr['network'] = $padr;
52
	} else {
53
		$adr['address'] = $padr;
54
		if ($pmask != 32) {
55
			$adr['address'] .= "/" . $pmask;
56
		}
57
	}
58

    
59
	if ($pnot) {
60
		$adr['not'] = true;
61
	} else {
62
		unset($adr['not']);
63
	}
64

    
65
	if (($pbeginport != 0) && ($pbeginport != "any")) {
66
		if ($pbeginport != $pendport) {
67
			$adr['port'] = $pbeginport . "-" . $pendport;
68
		} else {
69
			$adr['port'] = $pbeginport;
70
		}
71
	}
72

    
73
	if (is_alias($pbeginport)) {
74
		$adr['port'] = $pbeginport;
75
	}
76
}
77

    
78
/* Borrow this one from guiconfig.inc also */
79
function is_specialnet($net) {
80
	global $specialsrcdst;
81

    
82
	if (!$net) {
83
		return false;
84
	}
85
	if (in_array($net, $specialsrcdst)) {
86
		return true;
87
	} else {
88
		return false;
89
	}
90
}
91

    
92

    
93
if (($argc > 1) && !empty($argv[1])) {
94

    
95
	/* Automagically derive an alternate alias name from the scripts name
96
	 * This allows for using alternate alias lists with just a symlink */
97
	if (($alias = basename($argv[0])) != 'easyrule') {
98
		$blockaliasname = ucfirst($alias).'Rules';
99
	}
100

    
101
	$message = "";
102
	switch ($argv[1]) {
103
		case 'block':
104
			$message = easyrule_parse_block($argv[2], $argv[3]);
105
			break;
106
		case 'unblock':
107
			$message = easyrule_parse_unblock($argv[2], $argv[3]);
108
			break;
109
		case 'showblock':
110
			$message = easyrule_parse_getblock($argv[2]);
111
			break;
112
		case 'pass':
113
			$message = easyrule_parse_pass($argv[2], $argv[3], $argv[4], $argv[5], $argv[6]);
114
			break;
115
	}
116
	echo $message . "\n";
117
} else {
118
	// Print usage:
119
	echo "usage:\n";
120
	echo " Blocking only requires an IP to block, block rules can be shown with showblock and revoked using unblock\n";
121
	echo "     " . basename($argv[0]) . " block <interface> <source IP>\n";
122
	echo "\n";
123
	echo " Passing requires more detail, as it must be as specific as possible. The destination port is optional if you're using a protocol without a port (e.g. ICMP, OSPF, etc).\n";
124
	echo "     " . basename($argv[0]) . " pass <interface> <protocol> <source IP> <destination ip> [destination port]\n";
125
	echo "\n";
126
	echo " Block example:\n";
127
	echo "     " . basename($argv[0]) . " block wan 1.2.3.4\n";
128
	echo "\n";
129
	echo " Show active blocks example:\n";
130
	echo "     " . basename($argv[0]) . " showblock wan\n";
131
	echo "\n";
132
	echo " Unblock example:\n";
133
	echo "     " . basename($argv[0]) . " unblock wan 1.2.3.4\n";
134
	echo "\n";
135
	echo " Pass example (protocol with port):\n";
136
	echo "     " . basename($argv[0]) . " pass wan tcp 1.2.3.4 192.168.0.4 80\n";
137
	echo "\n";
138
	echo " Pass example (protocol without port):\n";
139
	echo "     " . basename($argv[0]) . " pass wan icmp 1.2.3.4 192.168.0.4\n";
140
	echo "\n";
141
}
142
?>
(5-5/15)