Project

General

Profile

Download (5.12 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	openvpn.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
$devname = getenv("dev");
38
if (empty($devname)) {
39
	$devname = "openvpn";
40
}
41

    
42
function cisco_to_cidr($addr) {
43
	if (!is_ipaddr($addr)) {
44
		return 0;
45
	}
46
	$mask = decbin(~ip2long($addr));
47
	$mask = substr($mask, -32);
48
	$k = 0;
49
	for ($i = 0; $i <= 32; $i++) {
50
		$k += intval($mask[$i]);
51
	}
52
	return $k;
53
}
54

    
55
function cisco_extract_index($prule) {
56

    
57
	$index = explode("#", $prule);
58
	if (is_numeric($index[1])) {
59
		return intval($index[1]);
60
	} else {
61
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
62
	}
63
	return -1;;
64
}
65

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

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

    
112
			$index++;
113

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

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

    
165
			if ($isblock == true) {
166
				continue;
167
			}
168

    
169
			if ($dir == "in") {
170
				$inrules[$rindex] = $tmprule;
171
			} else if ($dir == "out") {
172
				$outrules[$rindex] = $tmprule;
173
			}
174
		}
175

    
176

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

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

    
203
?>
(36-36/67)