Project

General

Profile

Download (4.52 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * openvpn.attributes.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2011-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22

    
23
if (empty($common_name)) {
24
	$common_name = getenv("common_name");
25
	if (empty($common_name)) {
26
		$common_name = getenv("username");
27
	}
28
}
29

    
30
$devname = getenv("dev");
31
if (empty($devname)) {
32
	$devname = "openvpn";
33
}
34

    
35
function cisco_to_cidr($addr) {
36
	if (!is_ipaddr($addr)) {
37
		return 0;
38
	}
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
	}
56
	return -1;;
57
}
58

    
59
function parse_cisco_acl($attribs) {
60
	global $devname, $attributes;
61
	if (!is_array($attribs)) {
62
		return "";
63
	}
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
				}
82
				$attributes['routes'][] = $rule[1];
83
				continue;
84
			}
85
			$rindex = cisco_extract_index($rule[0]);
86
			if ($rindex < 0) {
87
				continue;
88
			}
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
			$index++;
115
			/* Source */
116
			if (trim($rule[$index]) == "host") {
117
				$index++;
118
				$tmprule .= "from {$rule[$index]} ";
119
				$index++;
120
				if ($isblock == true) {
121
					$isblock = false;
122
				}
123
			} else if (trim($rule[$index]) == "any") {
124
				$tmprule .= "from any ";
125
				$index++;
126
			} else {
127
				$tmprule .= "from {$rule[$index]}";
128
				$index++;
129
				$netmask = cisco_to_cidr($rule[$index]);
130
				$tmprule .= "/{$netmask} ";
131
				$index++;
132
				if ($isblock == true) {
133
					$isblock = false;
134
				}
135
			}
136
			/* Destination */
137
			if (trim($rule[$index]) == "host") {
138
				$index++;
139
				$tmprule .= "to {$rule[$index]} ";
140
				$index++;
141
				if ($isblock == true) {
142
					$isblock = false;
143
				}
144
			} else if (trim($rule[$index]) == "any") {
145
				$index++;
146
				$tmprule .= "to any";
147
			} else {
148
				$tmprule .= "to {$rule[$index]}";
149
				$index++;
150
				$netmask = cisco_to_cidr($rule[$index]);
151
				$tmprule .= "/{$netmask} ";
152
				$index++;
153
				if ($isblock == true) {
154
					$isblock = false;
155
				}
156
			}
157

    
158
			if ($isblock == true) {
159
				continue;
160
			}
161

    
162
			if ($dir == "in") {
163
				$inrules[$rindex] = $tmprule;
164
			} else if ($dir == "out") {
165
				$outrules[$rindex] = $tmprule;
166
			}
167
		}
168

    
169

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

    
188
$rules = parse_cisco_acl($attributes);
189
if (!empty($rules)) {
190
	$pid = posix_getpid();
191
	@file_put_contents("/tmp/ovpn_{$pid}{$common_name}.rules", $rules);
192
	mwexec("/sbin/pfctl -a " . escapeshellarg("openvpn/{$common_name}") . " -f {$g['tmp_path']}/ovpn_{$pid}" . escapeshellarg($common_name) . ".rules");
193
	@unlink("{$g['tmp_path']}/ovpn_{$pid}{$common_name}.rules");
194
}
195

    
196
?>
(26-26/51)