1
|
#!/usr/local/bin/php -q
|
2
|
<?php
|
3
|
/*
|
4
|
easyrule CLI Program
|
5
|
|
6
|
Copyright (C) 2010 Jim Pingle (jpingle@gmail.com)
|
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
|
require_once("pfsense-utils.inc");
|
32
|
require_once("easyrule.inc");
|
33
|
require_once("filter.inc");
|
34
|
require_once("shaper.inc");
|
35
|
|
36
|
$message = "";
|
37
|
$specialsrcdst = explode(" ", "any pptp pppoe l2tp openvpn");
|
38
|
$ifdisp = get_configured_interface_with_descr();
|
39
|
foreach ($ifdisp as $kif => $kdescr) {
|
40
|
$specialsrcdst[] = "{$kif}";
|
41
|
$specialsrcdst[] = "{$kif}ip";
|
42
|
}
|
43
|
|
44
|
/* Borrow this function from guiconfig.inc since we can't include it for use at the CLI
|
45
|
|
46
|
- Maybe these need to be moved to util.inc or pfsense-utils.inc?
|
47
|
|
48
|
*/
|
49
|
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
|
50
|
|
51
|
$adr = array();
|
52
|
|
53
|
if ($padr == "any")
|
54
|
$adr['any'] = true;
|
55
|
else if (is_specialnet($padr))
|
56
|
$adr['network'] = $padr;
|
57
|
else {
|
58
|
$adr['address'] = $padr;
|
59
|
if ($pmask != 32)
|
60
|
$adr['address'] .= "/" . $pmask;
|
61
|
}
|
62
|
|
63
|
if ($pnot)
|
64
|
$adr['not'] = true;
|
65
|
else
|
66
|
unset($adr['not']);
|
67
|
|
68
|
if (($pbeginport != 0) && ($pbeginport != "any")) {
|
69
|
if ($pbeginport != $pendport)
|
70
|
$adr['port'] = $pbeginport . "-" . $pendport;
|
71
|
else
|
72
|
$adr['port'] = $pbeginport;
|
73
|
}
|
74
|
|
75
|
if(is_alias($pbeginport)) {
|
76
|
$adr['port'] = $pbeginport;
|
77
|
}
|
78
|
}
|
79
|
|
80
|
/* Borrow this one from guiconfig.inc also */
|
81
|
function is_specialnet($net) {
|
82
|
global $specialsrcdst;
|
83
|
|
84
|
if(!$net)
|
85
|
return false;
|
86
|
if (in_array($net, $specialsrcdst))
|
87
|
return true;
|
88
|
else
|
89
|
return false;
|
90
|
}
|
91
|
|
92
|
|
93
|
if (($argc > 1) && !empty($argv[1])) {
|
94
|
$message = "";
|
95
|
switch ($argv[1]) {
|
96
|
case 'block':
|
97
|
$message = easyrule_parse_block($argv[2], $argv[3]);
|
98
|
break;
|
99
|
case 'pass':
|
100
|
$message = easyrule_parse_pass($argv[2], $argv[3], $argv[4], $argv[5], $argv[6]);
|
101
|
break;
|
102
|
}
|
103
|
echo $message . "\n";
|
104
|
} else {
|
105
|
// Print usage:
|
106
|
echo "usage:\n";
|
107
|
echo " Blocking only requires an IP to block\n";
|
108
|
echo " " . basename($argv[0]) . " block <interface> <source IP>\n";
|
109
|
echo "\n";
|
110
|
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";
|
111
|
echo " " . basename($argv[0]) . " pass <interface> <protocol> <source IP> <destination ip> [destination port]\n";
|
112
|
echo "\n";
|
113
|
echo " Block example:\n";
|
114
|
echo " " . basename($argv[0]) . " block wan 1.2.3.4\n";
|
115
|
echo "\n";
|
116
|
echo " Pass example (protocol with port):\n";
|
117
|
echo " " . basename($argv[0]) . " pass wan tcp 1.2.3.4 192.168.0.4 80\n";
|
118
|
echo "\n";
|
119
|
echo " Pass example (protocol without port):\n";
|
120
|
echo " " . basename($argv[0]) . " pass wan icmp 1.2.3.4 192.168.0.4\n";
|
121
|
echo "\n";
|
122
|
}
|
123
|
?>
|