Project

General

Profile

Download (4.35 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-2020 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
global $username;
23

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

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

    
42
function cisco_extract_index($prule) {
43

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

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

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

    
99
			$index++;
100

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

    
108
			$index++;
109
			/* Source */
110
			if (trim($rule[$index]) == "host") {
111
				$index++;
112
				$tmprule .= "from {$rule[$index]} ";
113
				$index++;
114
				if ($isblock == true) {
115
					$isblock = false;
116
				}
117
			} else if (trim($rule[$index]) == "any") {
118
				$tmprule .= "from any ";
119
				$index++;
120
			} else {
121
				$tmprule .= "from {$rule[$index]}";
122
				$index++;
123
				$netmask = cisco_to_cidr($rule[$index]);
124
				$tmprule .= "/{$netmask} ";
125
				$index++;
126
				if ($isblock == true) {
127
					$isblock = false;
128
				}
129
			}
130
			/* Destination */
131
			if (trim($rule[$index]) == "host") {
132
				$index++;
133
				$tmprule .= "to {$rule[$index]} ";
134
				$index++;
135
				if ($isblock == true) {
136
					$isblock = false;
137
				}
138
			} else if (trim($rule[$index]) == "any") {
139
				$index++;
140
				$tmprule .= "to any";
141
			} else {
142
				$tmprule .= "to {$rule[$index]}";
143
				$index++;
144
				$netmask = cisco_to_cidr($rule[$index]);
145
				$tmprule .= "/{$netmask} ";
146
				$index++;
147
				if ($isblock == true) {
148
					$isblock = false;
149
				}
150
			}
151

    
152
			if ($isblock == true) {
153
				continue;
154
			}
155

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

    
163

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

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

    
191
?>
(32-32/60)