Project

General

Profile

Download (4.95 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	ipsec.attributes.php
4
	Copyright (C) 2011-2012         Ermal Luçi
5
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6
	All rights reserved.
7

    
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10

    
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13

    
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17

    
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28
*/
29

    
30
if (empty($common_name)) {
31
	$common_name = getenv("common_name");
32
	if (empty($common_name))
33
		$common_name = getenv("username");
34
}
35

    
36
function cisco_to_cidr($addr) {
37
	if (!is_ipaddr($addr))
38
		return 0;
39
	$mask = decbin(~ip2long($addr));
40
	$mask = substr($mask, -32);
41
	$k = 0;
42
	for ($i = 0; $i <= 32; $i++) {
43
		$k += intval($mask[$i]);
44
	}
45
	return $k;
46
}
47

    
48
function cisco_extract_index($prule) {
49
	
50
	$index = explode("#", $prule);
51
	if (is_numeric($index[1]))
52
		return intval($index[1]);
53
	else
54
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
55
	return -1;;
56
}
57

    
58
function parse_cisco_acl($attribs) {
59
	global $attributes;
60
	if (!is_array($attribs))
61
		return "";
62

    
63
	$devname = "enc0";
64
	$finalrules = "";
65
	if (is_array($attribs['ciscoavpair'])) {
66
		$inrules = array();
67
		$outrules = array();
68
		foreach ($attribs['ciscoavpair'] as $avrules) {
69
			$rule = explode("=", $avrules);
70
			$dir = "";
71
			if (strstr($rule[0], "inacl")) {
72
				$dir = "in";
73
			} else if (strstr($rule[0], "outacl"))
74
				$dir = "out";
75
			else if (strstr($rule[0], "dns-servers")) {
76
				$attributes['dns-servers'] = explode(" ", $rule[1]);
77
				continue;
78
			} else if (strstr($rule[0], "route")) {
79
				if (!is_array($attributes['routes']))
80
					$attributes['routes'] = array();
81
				$attributes['routes'][] = $rule[1];
82
				continue;
83
			}	
84
			$rindex = cisco_extract_index($rule[0]);
85
			if ($rindex < 0)
86
				continue;
87

    
88
			$rule = $rule[1];
89
			$rule = explode(" ", $rule);
90
			$tmprule = "";
91
			$index = 0;
92
			$isblock = false;
93
			if ($rule[$index] == "permit")
94
				$tmprule = "pass {$dir} quick on {$devname} ";
95
			else if ($rule[$index] == "deny") {
96
				//continue;
97
				$isblock = true;
98
				$tmprule = "block {$dir} quick on {$devname} ";
99
			} else {
100
				continue;
101
			}
102

    
103
			$index++;
104

    
105
			switch ($rule[$index]) {
106
			case "tcp":
107
			case "udp":
108
				$tmprule .= "proto {$rule[$index]} ";
109
				break;
110
				
111
			}
112

    
113
			$index++;
114
			/* Source */
115
			if (trim($rule[$index]) == "host") {
116
				$index++;
117
				$tmprule .= "from {$rule[$index]} ";
118
				$index++;
119
				if ($isblock == true)
120
					$isblock = false;
121
			} else if (trim($rule[$index]) == "any") {
122
				$tmprule .= "from any";
123
				$index++;
124
			} else {
125
				$tmprule .= "from {$rule[$index]}";
126
				$index++;
127
				$netmask = cisco_to_cidr($rule[$index]);
128
				$tmprule .= "/{$netmask} ";
129
				$index++;
130
				if ($isblock == true)
131
					$isblock = false;
132
			}
133
			/* Destination */
134
			if (trim($rule[$index]) == "host") {
135
				$index++;
136
				$tmprule .= "to {$rule[$index]} ";
137
				$index++;
138
				if ($isblock == true)
139
					$isblock = false;
140
			} else if (trim($rule[$index]) == "any") {
141
				$index++;
142
				$tmprule .= "to any";
143
			} else {
144
				$tmprule .= "to {$rule[$index]}";
145
				$index++;
146
				$netmask = cisco_to_cidr($rule[$index]);
147
				$tmprule .= "/{$netmask} ";
148
				$index++;
149
				if ($isblock == true)
150
					$isblock = false;
151
			}
152

    
153
			if ($isblock == true)
154
				continue;
155

    
156
			if ($dir == "in")
157
				$inrules[$rindex] = $tmprule;
158
			else if ($dir == "out")
159
				$outrules[$rindex] = $tmprule;
160
		}
161

    
162

    
163
		$state = "";
164
		if (!empty($outrules))
165
			$state = "no state";
166
		ksort($inrules, SORT_NUMERIC);
167
		foreach ($inrules as $inrule)
168
			$finalrules .= "{$inrule} {$state}\n";
169
		if (!empty($outrules)) {
170
			ksort($outrules, SORT_NUMERIC);
171
			foreach ($outrules as $outrule)
172
				$finalrules .= "{$outrule} {$state}\n";
173
		}
174
	}
175
	return $finalrules;
176
}
177

    
178
$rules = parse_cisco_acl($attributes);
179
if (!empty($rules)) {
180
	$pid = posix_getpid();
181
	@file_put_contents("/tmp/ipsec_{$pid}{$common_name}.rules", $rules);
182
	mwexec("/sbin/pfctl -a " . escapeshellarg("ipsec/{$common_name}") . " -f {$g['tmp_path']}/ipsec_{$pid}" . escapeshellarg($common_name) . ".rules");
183
	@unlink("{$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
184
}
185

    
186
?>
(26-26/67)