Project

General

Profile

Download (4.23 KB) Statistics
| Branch: | Tag: | Revision:
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
/* Another one we need from guiconfig.inc but can't include... */
88
function filter_rules_sort() {
89
	global $config;
90

    
91
	/* mark each rule with the sequence number (to retain the order while sorting) */
92
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
93
		$config['filter']['rule'][$i]['seq'] = $i;
94

    
95
	function filtercmp($a, $b) {
96
		if ($a['interface'] == $b['interface'])
97
			return $a['seq'] - $b['seq'];
98
		else
99
			return -strcmp($a['interface'], $b['interface']);
100
	}
101

    
102
	usort($config['filter']['rule'], "filtercmp");
103

    
104
	/* strip the sequence numbers again */
105
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
106
		unset($config['filter']['rule'][$i]['seq']);
107
}
108

    
109

    
110
if (($argc > 1) && !empty($argv[1])) {
111
	$message = "";
112
	switch ($argv[1]) {
113
		case 'block':
114
			$message = easyrule_parse_block($argv[2], $argv[3]);
115
			break;
116
		case 'pass':
117
			$message = easyrule_parse_pass($argv[2], $argv[3], $argv[4], $argv[5], $argv[6]);
118
			break;
119
	}
120
	echo $message . "\n";
121
} else {
122
	// Print usage:
123
	echo "usage:\n";
124
	echo " Blocking only requires an IP to block\n";
125
	echo "     " . basename($argv[0]) . " block <interface> <source IP>\n";
126
	echo "\n";
127
	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";
128
	echo "     " . basename($argv[0]) . " pass <interface> <protocol> <source IP> <destination ip> [destination port]\n";
129
	echo "\n";
130
	echo " Block example:\n";
131
	echo "     " . basename($argv[0]) . " block wan 1.2.3.4\n";
132
	echo "\n";
133
	echo " Pass example (protocol with port):\n";
134
	echo "     " . basename($argv[0]) . " pass wan tcp 1.2.3.4 192.168.0.4 80\n";
135
	echo "\n";
136
	echo " Block example (protocol without port):\n";
137
	echo "     " . basename($argv[0]) . " pass wan icmp 1.2.3.4 192.168.0.4\n";
138
	echo "\n";
139
}
140
?>
(3-3/9)