Project

General

Profile

Download (6.08 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 Electric Sheep Fencing, LLC.
7
	All rights reserved.
8

    
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11

    
12
	1. Redistributions of source code must retain the above copyright notice,
13
	   this list of conditions and the following disclaimer.
14

    
15
	2. Redistributions in binary form must reproduce the above copyright
16
	   notice, this list of conditions and the following disclaimer in
17
	   the documentation and/or other materials provided with the
18
	   distribution.
19

    
20
	3. All advertising materials mentioning features or use of this software
21
	   must display the following acknowledgment:
22
	   "This product includes software developed by the pfSense Project
23
	   for use in the pfSense® software distribution. (http://www.pfsense.org/).
24

    
25
	4. The names "pfSense" and "pfSense Project" must not be used to
26
	   endorse or promote products derived from this software without
27
	   prior written permission. For written permission, please contact
28
	   coreteam@pfsense.org.
29

    
30
	5. Products derived from this software may not be called "pfSense"
31
	   nor may "pfSense" appear in their names without prior written
32
	   permission of the Electric Sheep Fencing, LLC.
33

    
34
	6. Redistributions of any form whatsoever must retain the following
35
	   acknowledgment:
36

    
37
	"This product includes software developed by the pfSense Project
38
	for use in the pfSense software distribution (http://www.pfsense.org/).
39

    
40
	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
	OF THE POSSIBILITY OF SUCH DAMAGE.
52
*/
53

    
54
if (empty($common_name)) {
55
	$common_name = getenv("common_name");
56
	if (empty($common_name)) {
57
		$common_name = getenv("username");
58
	}
59
}
60

    
61
$devname = getenv("dev");
62
if (empty($devname)) {
63
	$devname = "openvpn";
64
}
65

    
66
function cisco_to_cidr($addr) {
67
	if (!is_ipaddr($addr)) {
68
		return 0;
69
	}
70
	$mask = decbin(~ip2long($addr));
71
	$mask = substr($mask, -32);
72
	$k = 0;
73
	for ($i = 0; $i <= 32; $i++) {
74
		$k += intval($mask[$i]);
75
	}
76
	return $k;
77
}
78

    
79
function cisco_extract_index($prule) {
80

    
81
	$index = explode("#", $prule);
82
	if (is_numeric($index[1])) {
83
		return intval($index[1]);
84
	} else {
85
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
86
	}
87
	return -1;;
88
}
89

    
90
function parse_cisco_acl($attribs) {
91
	global $devname, $attributes;
92
	if (!is_array($attribs)) {
93
		return "";
94
	}
95
	$finalrules = "";
96
	if (is_array($attribs['ciscoavpair'])) {
97
		$inrules = array();
98
		$outrules = array();
99
		foreach ($attribs['ciscoavpair'] as $avrules) {
100
			$rule = explode("=", $avrules);
101
			$dir = "";
102
			if (strstr($rule[0], "inacl")) {
103
				$dir = "in";
104
			} else if (strstr($rule[0], "outacl")) {
105
				$dir = "out";
106
			} else if (strstr($rule[0], "dns-servers")) {
107
				$attributes['dns-servers'] = explode(" ", $rule[1]);
108
				continue;
109
			} else if (strstr($rule[0], "route")) {
110
				if (!is_array($attributes['routes'])) {
111
					$attributes['routes'] = array();
112
				}
113
				$attributes['routes'][] = $rule[1];
114
				continue;
115
			}
116
			$rindex = cisco_extract_index($rule[0]);
117
			if ($rindex < 0) {
118
				continue;
119
			}
120

    
121
			$rule = $rule[1];
122
			$rule = explode(" ", $rule);
123
			$tmprule = "";
124
			$index = 0;
125
			$isblock = false;
126
			if ($rule[$index] == "permit") {
127
				$tmprule = "pass {$dir} quick on {$devname} ";
128
			} else if ($rule[$index] == "deny") {
129
				//continue;
130
				$isblock = true;
131
				$tmprule = "block {$dir} quick on {$devname} ";
132
			} else {
133
				continue;
134
			}
135

    
136
			$index++;
137

    
138
			switch ($rule[$index]) {
139
				case "tcp":
140
				case "udp":
141
					$tmprule .= "proto {$rule[$index]} ";
142
					break;
143
			}
144

    
145
			$index++;
146
			/* Source */
147
			if (trim($rule[$index]) == "host") {
148
				$index++;
149
				$tmprule .= "from {$rule[$index]} ";
150
				$index++;
151
				if ($isblock == true) {
152
					$isblock = false;
153
				}
154
			} else if (trim($rule[$index]) == "any") {
155
				$tmprule .= "from any ";
156
				$index++;
157
			} else {
158
				$tmprule .= "from {$rule[$index]}";
159
				$index++;
160
				$netmask = cisco_to_cidr($rule[$index]);
161
				$tmprule .= "/{$netmask} ";
162
				$index++;
163
				if ($isblock == true) {
164
					$isblock = false;
165
				}
166
			}
167
			/* Destination */
168
			if (trim($rule[$index]) == "host") {
169
				$index++;
170
				$tmprule .= "to {$rule[$index]} ";
171
				$index++;
172
				if ($isblock == true) {
173
					$isblock = false;
174
				}
175
			} else if (trim($rule[$index]) == "any") {
176
				$index++;
177
				$tmprule .= "to any";
178
			} else {
179
				$tmprule .= "to {$rule[$index]}";
180
				$index++;
181
				$netmask = cisco_to_cidr($rule[$index]);
182
				$tmprule .= "/{$netmask} ";
183
				$index++;
184
				if ($isblock == true) {
185
					$isblock = false;
186
				}
187
			}
188

    
189
			if ($isblock == true) {
190
				continue;
191
			}
192

    
193
			if ($dir == "in") {
194
				$inrules[$rindex] = $tmprule;
195
			} else if ($dir == "out") {
196
				$outrules[$rindex] = $tmprule;
197
			}
198
		}
199

    
200

    
201
		$state = "";
202
		if (!empty($outrules)) {
203
			$state = "no state";
204
		}
205
		ksort($inrules, SORT_NUMERIC);
206
		foreach ($inrules as $inrule) {
207
			$finalrules .= "{$inrule} {$state}\n";
208
		}
209
		if (!empty($outrules)) {
210
			ksort($outrules, SORT_NUMERIC);
211
			foreach ($outrules as $outrule) {
212
				$finalrules .= "{$outrule} {$state}\n";
213
			}
214
		}
215
	}
216
	return $finalrules;
217
}
218

    
219
$rules = parse_cisco_acl($attributes);
220
if (!empty($rules)) {
221
	$pid = posix_getpid();
222
	@file_put_contents("/tmp/ovpn_{$pid}{$common_name}.rules", $rules);
223
	mwexec("/sbin/pfctl -a " . escapeshellarg("openvpn/{$common_name}") . " -f {$g['tmp_path']}/ovpn_{$pid}" . escapeshellarg($common_name) . ".rules");
224
	@unlink("{$g['tmp_path']}/ovpn_{$pid}{$common_name}.rules");
225
}
226

    
227
?>
(35-35/65)