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
|
|
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
|
if ($pnot)
|
59
|
$adr['not'] = true;
|
60
|
else
|
61
|
unset($adr['not']);
|
62
|
|
63
|
if (($pbeginport != 0) && ($pbeginport != "any")) {
|
64
|
if ($pbeginport != $pendport)
|
65
|
$adr['port'] = $pbeginport . "-" . $pendport;
|
66
|
else
|
67
|
$adr['port'] = $pbeginport;
|
68
|
}
|
69
|
|
70
|
if(is_alias($pbeginport)) {
|
71
|
$adr['port'] = $pbeginport;
|
72
|
}
|
73
|
}
|
74
|
|
75
|
/* Borrow this one from guiconfig.inc also */
|
76
|
function is_specialnet($net) {
|
77
|
global $specialsrcdst;
|
78
|
|
79
|
if(!$net)
|
80
|
return false;
|
81
|
if (in_array($net, $specialsrcdst))
|
82
|
return true;
|
83
|
else
|
84
|
return false;
|
85
|
}
|
86
|
|
87
|
|
88
|
if (($argc > 1) && !empty($argv[1])) {
|
89
|
$message = "";
|
90
|
switch ($argv[1]) {
|
91
|
case 'block':
|
92
|
$message = easyrule_parse_block($argv[2], $argv[3]);
|
93
|
break;
|
94
|
case 'pass':
|
95
|
$message = easyrule_parse_pass($argv[2], $argv[3], $argv[4], $argv[5], $argv[6]);
|
96
|
break;
|
97
|
}
|
98
|
echo $message . "\n";
|
99
|
} else {
|
100
|
// Print usage:
|
101
|
echo "usage:\n";
|
102
|
echo " Blocking only requires an IP to block\n";
|
103
|
echo " " . basename($argv[0]) . " block <interface> <source IP>\n";
|
104
|
echo "\n";
|
105
|
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";
|
106
|
echo " " . basename($argv[0]) . " pass <interface> <protocol> <source IP> <destination ip> [destination port]\n";
|
107
|
echo "\n";
|
108
|
echo " Block example:\n";
|
109
|
echo " " . basename($argv[0]) . " block wan 1.2.3.4\n";
|
110
|
echo "\n";
|
111
|
echo " Pass example (protocol with port):\n";
|
112
|
echo " " . basename($argv[0]) . " pass wan tcp 1.2.3.4 192.168.0.4 80\n";
|
113
|
echo "\n";
|
114
|
echo " Block example (protocol without port):\n";
|
115
|
echo " " . basename($argv[0]) . " pass wan icmp 1.2.3.4 192.168.0.4\n";
|
116
|
echo "\n";
|
117
|
}
|
118
|
?>
|