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-2016 Rubicon Communications, LLC (Netgate)
|
8
|
* All rights reserved.
|
9
|
*
|
10
|
* Redistribution and use in source and binary forms, with or without
|
11
|
* modification, are permitted provided that the following conditions are met:
|
12
|
*
|
13
|
* 1. Redistributions of source code must retain the above copyright notice,
|
14
|
* this list of conditions and the following disclaimer.
|
15
|
*
|
16
|
* 2. Redistributions in binary form must reproduce the above copyright
|
17
|
* notice, this list of conditions and the following disclaimer in
|
18
|
* the documentation and/or other materials provided with the
|
19
|
* distribution.
|
20
|
*
|
21
|
* 3. All advertising materials mentioning features or use of this software
|
22
|
* must display the following acknowledgment:
|
23
|
* "This product includes software developed by the pfSense Project
|
24
|
* for use in the pfSense® software distribution. (http://www.pfsense.org/).
|
25
|
*
|
26
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
27
|
* endorse or promote products derived from this software without
|
28
|
* prior written permission. For written permission, please contact
|
29
|
* coreteam@pfsense.org.
|
30
|
*
|
31
|
* 5. Products derived from this software may not be called "pfSense"
|
32
|
* nor may "pfSense" appear in their names without prior written
|
33
|
* permission of the Electric Sheep Fencing, LLC.
|
34
|
*
|
35
|
* 6. Redistributions of any form whatsoever must retain the following
|
36
|
* acknowledgment:
|
37
|
*
|
38
|
* "This product includes software developed by the pfSense Project
|
39
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
40
|
*
|
41
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
42
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
43
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
44
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
45
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
46
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
47
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
48
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
49
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
50
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
51
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
52
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
53
|
*/
|
54
|
|
55
|
|
56
|
require_once("pfsense-utils.inc");
|
57
|
require_once("easyrule.inc");
|
58
|
require_once("filter.inc");
|
59
|
require_once("shaper.inc");
|
60
|
|
61
|
$message = "";
|
62
|
$specialsrcdst = explode(" ", "any pptp pppoe l2tp openvpn");
|
63
|
$ifdisp = get_configured_interface_with_descr();
|
64
|
foreach ($ifdisp as $kif => $kdescr) {
|
65
|
$specialsrcdst[] = "{$kif}";
|
66
|
$specialsrcdst[] = "{$kif}ip";
|
67
|
}
|
68
|
|
69
|
/* Borrow this function from guiconfig.inc since we can't include it for use at the CLI
|
70
|
|
71
|
- Maybe these need to be moved to util.inc or pfsense-utils.inc?
|
72
|
|
73
|
*/
|
74
|
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
|
75
|
|
76
|
$adr = array();
|
77
|
|
78
|
if ($padr == "any") {
|
79
|
$adr['any'] = true;
|
80
|
} else if (is_specialnet($padr)) {
|
81
|
$adr['network'] = $padr;
|
82
|
} else {
|
83
|
$adr['address'] = $padr;
|
84
|
if ($pmask != 32) {
|
85
|
$adr['address'] .= "/" . $pmask;
|
86
|
}
|
87
|
}
|
88
|
|
89
|
if ($pnot) {
|
90
|
$adr['not'] = true;
|
91
|
} else {
|
92
|
unset($adr['not']);
|
93
|
}
|
94
|
|
95
|
if (($pbeginport != 0) && ($pbeginport != "any")) {
|
96
|
if ($pbeginport != $pendport) {
|
97
|
$adr['port'] = $pbeginport . "-" . $pendport;
|
98
|
} else {
|
99
|
$adr['port'] = $pbeginport;
|
100
|
}
|
101
|
}
|
102
|
|
103
|
if (is_alias($pbeginport)) {
|
104
|
$adr['port'] = $pbeginport;
|
105
|
}
|
106
|
}
|
107
|
|
108
|
/* Borrow this one from guiconfig.inc also */
|
109
|
function is_specialnet($net) {
|
110
|
global $specialsrcdst;
|
111
|
|
112
|
if (!$net) {
|
113
|
return false;
|
114
|
}
|
115
|
if (in_array($net, $specialsrcdst)) {
|
116
|
return true;
|
117
|
} else {
|
118
|
return false;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
|
123
|
if (($argc > 1) && !empty($argv[1])) {
|
124
|
|
125
|
/* Automagically derive an alternate alias name from the scripts name
|
126
|
* This allows for using alternate alias lists with just a symlink */
|
127
|
if (($alias = basename($argv[0])) != 'easyrule') {
|
128
|
$blockaliasname = ucfirst($alias).'Rules';
|
129
|
}
|
130
|
|
131
|
$message = "";
|
132
|
switch ($argv[1]) {
|
133
|
case 'block':
|
134
|
$message = easyrule_parse_block($argv[2], $argv[3]);
|
135
|
break;
|
136
|
case 'unblock':
|
137
|
$message = easyrule_parse_unblock($argv[2], $argv[3]);
|
138
|
break;
|
139
|
case 'showblock':
|
140
|
$message = easyrule_parse_getblock($argv[2]);
|
141
|
break;
|
142
|
case 'pass':
|
143
|
$message = easyrule_parse_pass($argv[2], $argv[3], $argv[4], $argv[5], $argv[6]);
|
144
|
break;
|
145
|
}
|
146
|
echo $message . "\n";
|
147
|
} else {
|
148
|
// Print usage:
|
149
|
echo "usage:\n";
|
150
|
echo " Blocking only requires an IP to block, block rules can be shown with showblock and revoked using unblock\n";
|
151
|
echo " " . basename($argv[0]) . " block <interface> <source IP>\n";
|
152
|
echo "\n";
|
153
|
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";
|
154
|
echo " " . basename($argv[0]) . " pass <interface> <protocol> <source IP> <destination ip> [destination port]\n";
|
155
|
echo "\n";
|
156
|
echo " Block example:\n";
|
157
|
echo " " . basename($argv[0]) . " block wan 1.2.3.4\n";
|
158
|
echo "\n";
|
159
|
echo " Show active blocks example:\n";
|
160
|
echo " " . basename($argv[0]) . " showblock wan\n";
|
161
|
echo "\n";
|
162
|
echo " Unblock example:\n";
|
163
|
echo " " . basename($argv[0]) . " unblock wan 1.2.3.4\n";
|
164
|
echo "\n";
|
165
|
echo " Pass example (protocol with port):\n";
|
166
|
echo " " . basename($argv[0]) . " pass wan tcp 1.2.3.4 192.168.0.4 80\n";
|
167
|
echo "\n";
|
168
|
echo " Pass example (protocol without port):\n";
|
169
|
echo " " . basename($argv[0]) . " pass wan icmp 1.2.3.4 192.168.0.4\n";
|
170
|
echo "\n";
|
171
|
}
|
172
|
?>
|