Project

General

Profile

Download (4.44 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
global $username;
25

    
26
$devname = getenv("dev");
27
if (empty($devname)) {
28
	$devname = "openvpn";
29
}
30

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

    
44
function cisco_extract_index($prule) {
45

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

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

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

    
101
			$index++;
102

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

    
110
			$index++;
111
			/* Source */
112
			if (trim($rule[$index]) == "host") {
113
				$index++;
114
				$tmprule .= "from {$rule[$index]} ";
115
				$index++;
116
				if ($isblock == true) {
117
					$isblock = false;
118
				}
119
			} else if (trim($rule[$index]) == "any") {
120
				$tmprule .= "from any ";
121
				$index++;
122
			} else {
123
				$tmprule .= "from {$rule[$index]}";
124
				$index++;
125
				$netmask = cisco_to_cidr($rule[$index]);
126
				$tmprule .= "/{$netmask} ";
127
				$index++;
128
				if ($isblock == true) {
129
					$isblock = false;
130
				}
131
			}
132
			/* Destination */
133
			if (trim($rule[$index]) == "host") {
134
				$index++;
135
				$tmprule .= "to {$rule[$index]} ";
136
				$index++;
137
				if ($isblock == true) {
138
					$isblock = false;
139
				}
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

    
154
			if ($isblock == true) {
155
				continue;
156
			}
157

    
158
			if ($dir == "in") {
159
				$inrules[$rindex] = $tmprule;
160
			} else if ($dir == "out") {
161
				$outrules[$rindex] = $tmprule;
162
			}
163
		}
164

    
165

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

    
184
$rules = parse_cisco_acl($attributes);
185
if (!empty($rules)) {
186
	$pid = posix_getpid();
187
	$filename = "{$g['tmp_path']}/ovpn_{$pid}{$username}.rules";
188
	@file_put_contents($filename, $rules);
189
	mwexec("/sbin/pfctl -a " . escapeshellarg("openvpn/{$username}") . " -f " . escapeshellarg($filename));
190
	@unlink($filename);
191
}
192

    
193
?>
(31-31/60)