Project

General

Profile

Download (5.06 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

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

    
50
function cisco_extract_index($prule) {
51

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

    
61
function parse_cisco_acl($attribs) {
62
	global $attributes;
63
	if (!is_array($attribs)) {
64
		return "";
65
	}
66

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

    
94
			$rule = $rule[1];
95
			$rule = explode(" ", $rule);
96
			$tmprule = "";
97
			$index = 0;
98
			$isblock = false;
99
			if ($rule[$index] == "permit") {
100
				$tmprule = "pass {$dir} quick on {$devname} ";
101
			} else if ($rule[$index] == "deny") {
102
				//continue;
103
				$isblock = true;
104
				$tmprule = "block {$dir} quick on {$devname} ";
105
			} else {
106
				continue;
107
			}
108

    
109
			$index++;
110

    
111
			switch ($rule[$index]) {
112
				case "tcp":
113
				case "udp":
114
					$tmprule .= "proto {$rule[$index]} ";
115
					break;
116
			}
117

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

    
162
			if ($isblock == true) {
163
				continue;
164
			}
165

    
166
			if ($dir == "in") {
167
				$inrules[$rindex] = $tmprule;
168
			} else if ($dir == "out") {
169
				$outrules[$rindex] = $tmprule;
170
			}
171
		}
172

    
173

    
174
		$state = "";
175
		if (!empty($outrules)) {
176
			$state = "no state";
177
		}
178
		ksort($inrules, SORT_NUMERIC);
179
		foreach ($inrules as $inrule) {
180
			$finalrules .= "{$inrule} {$state}\n";
181
		}
182
		if (!empty($outrules)) {
183
			ksort($outrules, SORT_NUMERIC);
184
			foreach ($outrules as $outrule) {
185
				$finalrules .= "{$outrule} {$state}\n";
186
			}
187
		}
188
	}
189
	return $finalrules;
190
}
191

    
192
$rules = parse_cisco_acl($attributes);
193
if (!empty($rules)) {
194
	$pid = posix_getpid();
195
	@file_put_contents("/tmp/ipsec_{$pid}{$common_name}.rules", $rules);
196
	mwexec("/sbin/pfctl -a " . escapeshellarg("ipsec/{$common_name}") . " -f {$g['tmp_path']}/ipsec_{$pid}" . escapeshellarg($common_name) . ".rules");
197
	@unlink("{$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
198
}
199

    
200
?>
(27-27/67)