Project

General

Profile

Download (5.01 KB) Statistics
| Branch: | Tag: | Revision:
1 5b4ee05e Ermal
<?php
2
/*
3 6317d31d Phil Davis
	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 5b4ee05e Ermal
*/
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
$devname = getenv("dev");
37
if (empty($devname))
38
	$devname = "openvpn";
39
40
function cisco_to_cidr($addr) {
41
	if (!is_ipaddr($addr))
42
		return 0;
43
	$mask = decbin(~ip2long($addr));
44
	$mask = substr($mask, -32);
45
	$k = 0;
46
	for ($i = 0; $i <= 32; $i++) {
47
		$k += intval($mask[$i]);
48
	}
49
	return $k;
50
}
51
52
function cisco_extract_index($prule) {
53
	
54
	$index = explode("#", $prule);
55
	if (is_numeric($index[1]))
56
		return intval($index[1]);
57
	else
58
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
59
	return -1;;
60
}
61
62
function parse_cisco_acl($attribs) {
63
	global $devname, $attributes;
64
	if (!is_array($attribs))
65
		return "";
66
	$finalrules = "";
67
	if (is_array($attribs['ciscoavpair'])) {
68
		$inrules = array();
69
		$outrules = array();
70
		foreach ($attribs['ciscoavpair'] as $avrules) {
71
			$rule = explode("=", $avrules);
72
			$dir = "";
73
			if (strstr($rule[0], "inacl")) {
74
				$dir = "in";
75
			} else if (strstr($rule[0], "outacl"))
76
				$dir = "out";
77
			else if (strstr($rule[0], "dns-servers")) {
78
				$attributes['dns-servers'] = explode(" ", $rule[1]);
79
				continue;
80
			} else if (strstr($rule[0], "route")) {
81
				if (!is_array($attributes['routes']))
82
					$attributes['routes'] = array();
83 9f293b1c jim-p
				$attributes['routes'][] = $rule[1];
84 5b4ee05e Ermal
				continue;
85
			}	
86
			$rindex = cisco_extract_index($rule[0]);
87
			if ($rindex < 0)
88
				continue;
89
90
			$rule = $rule[1];
91
			$rule = explode(" ", $rule);
92
			$tmprule = "";
93
			$index = 0;
94
			$isblock = false;
95
			if ($rule[$index] == "permit")
96
				$tmprule = "pass {$dir} quick on {$devname} ";
97
			else if ($rule[$index] == "deny") {
98
				//continue;
99
				$isblock = true;
100
				$tmprule = "block {$dir} quick on {$devname} ";
101
			} else {
102
				continue;
103
			}
104
105
			$index++;
106
107
			switch ($rule[$index]) {
108
			case "tcp":
109
			case "udp":
110
				$tmprule .= "proto {$rule[$index]} ";
111
				break;
112
				
113
			}
114
115
			$index++;
116
			/* Source */
117
			if (trim($rule[$index]) == "host") {
118
				$index++;
119
				$tmprule .= "from {$rule[$index]} ";
120
				$index++;
121
				if ($isblock == true)
122
					$isblock = false;
123
			} else if (trim($rule[$index]) == "any") {
124
				$tmprule .= "from any";
125
				$index++;
126
			} else {
127 8340d956 Renato Botelho
				$tmprule .= "from {$rule[$index]}";
128 5b4ee05e Ermal
				$index++;
129
				$netmask = cisco_to_cidr($rule[$index]);
130
				$tmprule .= "/{$netmask} ";
131
				$index++;
132
				if ($isblock == true)
133
					$isblock = false;
134
			}
135
			/* Destination */
136
			if (trim($rule[$index]) == "host") {
137
				$index++;
138
				$tmprule .= "to {$rule[$index]} ";
139
				$index++;
140
				if ($isblock == true)
141
					$isblock = false;
142
			} else if (trim($rule[$index]) == "any") {
143
				$index++;
144
				$tmprule .= "to any";
145
			} else {
146 8340d956 Renato Botelho
				$tmprule .= "to {$rule[$index]}";
147 5b4ee05e Ermal
				$index++;
148
				$netmask = cisco_to_cidr($rule[$index]);
149
				$tmprule .= "/{$netmask} ";
150
				$index++;
151
				if ($isblock == true)
152
					$isblock = false;
153
			}
154
155
			if ($isblock == true)
156
				continue;
157
158
			if ($dir == "in")
159
				$inrules[$rindex] = $tmprule;
160
			else if ($dir == "out")
161
				$outrules[$rindex] = $tmprule;
162
		}
163
164
165
		$state = "";
166
		if (!empty($outrules))
167
			$state = "no state";
168
		ksort($inrules, SORT_NUMERIC);
169
		foreach ($inrules as $inrule)
170
			$finalrules .= "{$inrule} {$state}\n";
171
		if (!empty($outrules)) {
172
			ksort($outrules, SORT_NUMERIC);
173
			foreach ($outrules as $outrule)
174
				$finalrules .= "{$outrule} {$state}\n";
175
		}
176
	}
177
	return $finalrules;
178
}
179
180
$rules = parse_cisco_acl($attributes);
181
if (!empty($rules)) {
182 7b95ffdd Ermal
	$pid = posix_getpid();
183
	@file_put_contents("/tmp/ovpn_{$pid}{$common_name}.rules", $rules);
184 7b27b18b Renato Botelho
	mwexec("/sbin/pfctl -a " . escapeshellarg("openvpn/{$common_name}") . " -f {$g['tmp_path']}/ovpn_{$pid}" . escapeshellarg($common_name) . ".rules");
185 7b95ffdd Ermal
	@unlink("{$g['tmp_path']}/ovpn_{$pid}{$common_name}.rules");
186 5b4ee05e Ermal
}
187
188
?>