Project

General

Profile

Download (6.08 KB) Statistics
| Branch: | Tag: | Revision:
1 5b4ee05e Ermal
<?php
2
/*
3 ce77a9c4 Phil Davis
	openvpn.attributes.php
4 09221bc3 Renato Botelho
5
	part of pfSense (https://www.pfsense.org)
6
	Copyright (c) 2011-2016 Electric Sheep Fencing, LLC.
7 ce77a9c4 Phil Davis
	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 09221bc3 Renato Botelho
	   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 5b4ee05e Ermal
*/
53
54
if (empty($common_name)) {
55
	$common_name = getenv("common_name");
56 b37a2e8c Phil Davis
	if (empty($common_name)) {
57 5b4ee05e Ermal
		$common_name = getenv("username");
58 b37a2e8c Phil Davis
	}
59 5b4ee05e Ermal
}
60
61
$devname = getenv("dev");
62 b37a2e8c Phil Davis
if (empty($devname)) {
63 5b4ee05e Ermal
	$devname = "openvpn";
64 b37a2e8c Phil Davis
}
65 5b4ee05e Ermal
66
function cisco_to_cidr($addr) {
67 b37a2e8c Phil Davis
	if (!is_ipaddr($addr)) {
68 5b4ee05e Ermal
		return 0;
69 b37a2e8c Phil Davis
	}
70 5b4ee05e Ermal
	$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 b37a2e8c Phil Davis
81 5b4ee05e Ermal
	$index = explode("#", $prule);
82 b37a2e8c Phil Davis
	if (is_numeric($index[1])) {
83 5b4ee05e Ermal
		return intval($index[1]);
84 b37a2e8c Phil Davis
	} else {
85 5b4ee05e Ermal
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
86 b37a2e8c Phil Davis
	}
87 5b4ee05e Ermal
	return -1;;
88
}
89
90
function parse_cisco_acl($attribs) {
91
	global $devname, $attributes;
92 b37a2e8c Phil Davis
	if (!is_array($attribs)) {
93 5b4ee05e Ermal
		return "";
94 b37a2e8c Phil Davis
	}
95 5b4ee05e Ermal
	$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 b37a2e8c Phil Davis
			} else if (strstr($rule[0], "outacl")) {
105 5b4ee05e Ermal
				$dir = "out";
106 b37a2e8c Phil Davis
			} else if (strstr($rule[0], "dns-servers")) {
107 5b4ee05e Ermal
				$attributes['dns-servers'] = explode(" ", $rule[1]);
108
				continue;
109
			} else if (strstr($rule[0], "route")) {
110 b37a2e8c Phil Davis
				if (!is_array($attributes['routes'])) {
111 5b4ee05e Ermal
					$attributes['routes'] = array();
112 b37a2e8c Phil Davis
				}
113 9f293b1c jim-p
				$attributes['routes'][] = $rule[1];
114 5b4ee05e Ermal
				continue;
115 b37a2e8c Phil Davis
			}
116 5b4ee05e Ermal
			$rindex = cisco_extract_index($rule[0]);
117 b37a2e8c Phil Davis
			if ($rindex < 0) {
118 5b4ee05e Ermal
				continue;
119 b37a2e8c Phil Davis
			}
120 5b4ee05e Ermal
121
			$rule = $rule[1];
122
			$rule = explode(" ", $rule);
123
			$tmprule = "";
124
			$index = 0;
125
			$isblock = false;
126 b37a2e8c Phil Davis
			if ($rule[$index] == "permit") {
127 5b4ee05e Ermal
				$tmprule = "pass {$dir} quick on {$devname} ";
128 b37a2e8c Phil Davis
			} else if ($rule[$index] == "deny") {
129 5b4ee05e Ermal
				//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 b37a2e8c Phil Davis
				case "tcp":
140
				case "udp":
141
					$tmprule .= "proto {$rule[$index]} ";
142
					break;
143 5b4ee05e Ermal
			}
144
145
			$index++;
146
			/* Source */
147
			if (trim($rule[$index]) == "host") {
148
				$index++;
149
				$tmprule .= "from {$rule[$index]} ";
150
				$index++;
151 b37a2e8c Phil Davis
				if ($isblock == true) {
152 5b4ee05e Ermal
					$isblock = false;
153 b37a2e8c Phil Davis
				}
154 5b4ee05e Ermal
			} else if (trim($rule[$index]) == "any") {
155 b0ccc67b Phil Davis
				$tmprule .= "from any ";
156 5b4ee05e Ermal
				$index++;
157
			} else {
158 8340d956 Renato Botelho
				$tmprule .= "from {$rule[$index]}";
159 5b4ee05e Ermal
				$index++;
160
				$netmask = cisco_to_cidr($rule[$index]);
161
				$tmprule .= "/{$netmask} ";
162
				$index++;
163 b37a2e8c Phil Davis
				if ($isblock == true) {
164 5b4ee05e Ermal
					$isblock = false;
165 b37a2e8c Phil Davis
				}
166 5b4ee05e Ermal
			}
167
			/* Destination */
168
			if (trim($rule[$index]) == "host") {
169
				$index++;
170 b0ccc67b Phil Davis
				$tmprule .= "to {$rule[$index]} ";
171 5b4ee05e Ermal
				$index++;
172 b37a2e8c Phil Davis
				if ($isblock == true) {
173 5b4ee05e Ermal
					$isblock = false;
174 b37a2e8c Phil Davis
				}
175 5b4ee05e Ermal
			} else if (trim($rule[$index]) == "any") {
176
				$index++;
177 b0ccc67b Phil Davis
				$tmprule .= "to any";
178 5b4ee05e Ermal
			} else {
179 b0ccc67b Phil Davis
				$tmprule .= "to {$rule[$index]}";
180 5b4ee05e Ermal
				$index++;
181
				$netmask = cisco_to_cidr($rule[$index]);
182
				$tmprule .= "/{$netmask} ";
183
				$index++;
184 b37a2e8c Phil Davis
				if ($isblock == true) {
185 5b4ee05e Ermal
					$isblock = false;
186 b37a2e8c Phil Davis
				}
187 5b4ee05e Ermal
			}
188
189 b37a2e8c Phil Davis
			if ($isblock == true) {
190 5b4ee05e Ermal
				continue;
191 b37a2e8c Phil Davis
			}
192 5b4ee05e Ermal
193 b37a2e8c Phil Davis
			if ($dir == "in") {
194 5b4ee05e Ermal
				$inrules[$rindex] = $tmprule;
195 b37a2e8c Phil Davis
			} else if ($dir == "out") {
196 5b4ee05e Ermal
				$outrules[$rindex] = $tmprule;
197 b37a2e8c Phil Davis
			}
198 5b4ee05e Ermal
		}
199
200
201
		$state = "";
202 b37a2e8c Phil Davis
		if (!empty($outrules)) {
203 5b4ee05e Ermal
			$state = "no state";
204 b37a2e8c Phil Davis
		}
205 5b4ee05e Ermal
		ksort($inrules, SORT_NUMERIC);
206 b37a2e8c Phil Davis
		foreach ($inrules as $inrule) {
207 5b4ee05e Ermal
			$finalrules .= "{$inrule} {$state}\n";
208 b37a2e8c Phil Davis
		}
209 5b4ee05e Ermal
		if (!empty($outrules)) {
210
			ksort($outrules, SORT_NUMERIC);
211 b37a2e8c Phil Davis
			foreach ($outrules as $outrule) {
212 5b4ee05e Ermal
				$finalrules .= "{$outrule} {$state}\n";
213 b37a2e8c Phil Davis
			}
214 5b4ee05e Ermal
		}
215
	}
216
	return $finalrules;
217
}
218
219
$rules = parse_cisco_acl($attributes);
220
if (!empty($rules)) {
221 7b95ffdd Ermal
	$pid = posix_getpid();
222
	@file_put_contents("/tmp/ovpn_{$pid}{$common_name}.rules", $rules);
223 7b27b18b Renato Botelho
	mwexec("/sbin/pfctl -a " . escapeshellarg("openvpn/{$common_name}") . " -f {$g['tmp_path']}/ovpn_{$pid}" . escapeshellarg($common_name) . ".rules");
224 7b95ffdd Ermal
	@unlink("{$g['tmp_path']}/ovpn_{$pid}{$common_name}.rules");
225 5b4ee05e Ermal
}
226
227
?>