Project

General

Profile

Download (4.46 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -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

    
64
	if ($pnot) {
65
		$adr['not'] = true;
66
	} else {
67
		unset($adr['not']);
68
	}
69

    
70
	if (($pbeginport != 0) && ($pbeginport != "any")) {
71
		if ($pbeginport != $pendport) {
72
			$adr['port'] = $pbeginport . "-" . $pendport;
73
		} else {
74
			$adr['port'] = $pbeginport;
75
		}
76
	}
77

    
78
	if (is_alias($pbeginport)) {
79
		$adr['port'] = $pbeginport;
80
	}
81
}
82

    
83
/* Borrow this one from guiconfig.inc also */
84
function is_specialnet($net) {
85
	global $specialsrcdst;
86

    
87
	if (!$net) {
88
		return false;
89
	}
90
	if (in_array($net, $specialsrcdst)) {
91
		return true;
92
	} else {
93
		return false;
94
	}
95
}
96

    
97

    
98
if (($argc > 1) && !empty($argv[1])) {
99

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

    
106
	$message = "";
107
	switch ($argv[1]) {
108
		case 'block':
109
			$message = easyrule_parse_block($argv[2], $argv[3]);
110
			break;
111
		case 'unblock':
112
			$message = easyrule_parse_unblock($argv[2], $argv[3]);
113
			break;
114
		case 'showblock':
115
			$message = easyrule_parse_getblock($argv[2]);
116
			break;
117
		case 'pass':
118
			$message = easyrule_parse_pass($argv[2], $argv[3], $argv[4], $argv[5], $argv[6]);
119
			break;
120
	}
121
	echo $message . "\n";
122
} else {
123
	// Print usage:
124
	echo "usage:\n";
125
	echo " Blocking only requires an IP to block, block rules can be shown with showblock and revoked using unblock\n";
126
	echo "     " . basename($argv[0]) . " block <interface> <source IP>\n";
127
	echo "\n";
128
	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";
129
	echo "     " . basename($argv[0]) . " pass <interface> <protocol> <source IP> <destination ip> [destination port]\n";
130
	echo "\n";
131
	echo " Block example:\n";
132
	echo "     " . basename($argv[0]) . " block wan 1.2.3.4\n";
133
	echo "\n";
134
	echo " Show active blocks example:\n";
135
	echo "     " . basename($argv[0]) . " showblock wan\n";
136
	echo "\n";
137
	echo " Unblock example:\n";
138
	echo "     " . basename($argv[0]) . " unblock wan 1.2.3.4\n";
139
	echo "\n";
140
	echo " Pass example (protocol with port):\n";
141
	echo "     " . basename($argv[0]) . " pass wan tcp 1.2.3.4 192.168.0.4 80\n";
142
	echo "\n";
143
	echo " Pass example (protocol without port):\n";
144
	echo "     " . basename($argv[0]) . " pass wan icmp 1.2.3.4 192.168.0.4\n";
145
	echo "\n";
146
}
147
?>
(5-5/11)