Project

General

Profile

Download (4.46 KB) Statistics
| Branch: | Tag: | Revision:
1 52c9f9fa Ermal
<?php
2
/*
3 ac24dc24 Renato Botelho
 * ipsec.attributes.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 880ed461 jim-p
 * Copyright (c) 2011-2020 Rubicon Communications, LLC (Netgate)
7 ac24dc24 Renato Botelho
 * All rights reserved.
8
 *
9 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
13 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
14 ac24dc24 Renato Botelho
 *
15 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
21 52c9f9fa Ermal
22
if (empty($common_name)) {
23
	$common_name = getenv("common_name");
24 b37a2e8c Phil Davis
	if (empty($common_name)) {
25 52c9f9fa Ermal
		$common_name = getenv("username");
26 b37a2e8c Phil Davis
	}
27 52c9f9fa Ermal
}
28
29
function cisco_to_cidr($addr) {
30 b37a2e8c Phil Davis
	if (!is_ipaddr($addr)) {
31 52c9f9fa Ermal
		return 0;
32 b37a2e8c Phil Davis
	}
33 52c9f9fa Ermal
	$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 b37a2e8c Phil Davis
44 52c9f9fa Ermal
	$index = explode("#", $prule);
45 b37a2e8c Phil Davis
	if (is_numeric($index[1])) {
46 52c9f9fa Ermal
		return intval($index[1]);
47 b37a2e8c Phil Davis
	} else {
48 52c9f9fa Ermal
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
49 b37a2e8c Phil Davis
	}
50 52c9f9fa Ermal
	return -1;;
51
}
52
53
function parse_cisco_acl($attribs) {
54
	global $attributes;
55 b37a2e8c Phil Davis
	if (!is_array($attribs)) {
56 52c9f9fa Ermal
		return "";
57 b37a2e8c Phil Davis
	}
58 52c9f9fa Ermal
59
	$devname = "enc0";
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 b37a2e8c Phil Davis
			} else if (strstr($rule[0], "outacl")) {
70 52c9f9fa Ermal
				$dir = "out";
71 b37a2e8c Phil Davis
			} else if (strstr($rule[0], "dns-servers")) {
72 52c9f9fa Ermal
				$attributes['dns-servers'] = explode(" ", $rule[1]);
73
				continue;
74
			} else if (strstr($rule[0], "route")) {
75 b37a2e8c Phil Davis
				if (!is_array($attributes['routes'])) {
76 52c9f9fa Ermal
					$attributes['routes'] = array();
77 b37a2e8c Phil Davis
				}
78 45758be4 Ermal
				$attributes['routes'][] = $rule[1];
79 52c9f9fa Ermal
				continue;
80 b37a2e8c Phil Davis
			}
81 52c9f9fa Ermal
			$rindex = cisco_extract_index($rule[0]);
82 b37a2e8c Phil Davis
			if ($rindex < 0) {
83 52c9f9fa Ermal
				continue;
84 b37a2e8c Phil Davis
			}
85 52c9f9fa Ermal
86
			$rule = $rule[1];
87
			$rule = explode(" ", $rule);
88
			$tmprule = "";
89
			$index = 0;
90
			$isblock = false;
91 b37a2e8c Phil Davis
			if ($rule[$index] == "permit") {
92 52c9f9fa Ermal
				$tmprule = "pass {$dir} quick on {$devname} ";
93 b37a2e8c Phil Davis
			} else if ($rule[$index] == "deny") {
94 52c9f9fa Ermal
				//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 b37a2e8c Phil Davis
				case "tcp":
105
				case "udp":
106
					$tmprule .= "proto {$rule[$index]} ";
107
					break;
108 52c9f9fa Ermal
			}
109
110
			$index++;
111
			/* Source */
112
			if (trim($rule[$index]) == "host") {
113
				$index++;
114
				$tmprule .= "from {$rule[$index]} ";
115
				$index++;
116 b37a2e8c Phil Davis
				if ($isblock == true) {
117 52c9f9fa Ermal
					$isblock = false;
118 b37a2e8c Phil Davis
				}
119 52c9f9fa Ermal
			} else if (trim($rule[$index]) == "any") {
120 1a6857d0 Aurélien BONANNI
				$tmprule .= "from any ";
121 52c9f9fa Ermal
				$index++;
122
			} else {
123 1a6857d0 Aurélien BONANNI
				$tmprule .= "from {$rule[$index]} ";
124 52c9f9fa Ermal
				$index++;
125
				$netmask = cisco_to_cidr($rule[$index]);
126
				$tmprule .= "/{$netmask} ";
127
				$index++;
128 b37a2e8c Phil Davis
				if ($isblock == true) {
129 52c9f9fa Ermal
					$isblock = false;
130 b37a2e8c Phil Davis
				}
131 52c9f9fa Ermal
			}
132
			/* Destination */
133
			if (trim($rule[$index]) == "host") {
134
				$index++;
135
				$tmprule .= "to {$rule[$index]} ";
136
				$index++;
137 b37a2e8c Phil Davis
				if ($isblock == true) {
138 52c9f9fa Ermal
					$isblock = false;
139 b37a2e8c Phil Davis
				}
140 52c9f9fa Ermal
			} else if (trim($rule[$index]) == "any") {
141
				$index++;
142
				$tmprule .= "to any";
143
			} else {
144 3f4bd83b Renato Botelho
				$tmprule .= "to {$rule[$index]}";
145 52c9f9fa Ermal
				$index++;
146
				$netmask = cisco_to_cidr($rule[$index]);
147
				$tmprule .= "/{$netmask} ";
148
				$index++;
149 b37a2e8c Phil Davis
				if ($isblock == true) {
150 52c9f9fa Ermal
					$isblock = false;
151 b37a2e8c Phil Davis
				}
152 52c9f9fa Ermal
			}
153
154 b37a2e8c Phil Davis
			if ($isblock == true) {
155 52c9f9fa Ermal
				continue;
156 b37a2e8c Phil Davis
			}
157 52c9f9fa Ermal
158 b37a2e8c Phil Davis
			if ($dir == "in") {
159 52c9f9fa Ermal
				$inrules[$rindex] = $tmprule;
160 b37a2e8c Phil Davis
			} else if ($dir == "out") {
161 52c9f9fa Ermal
				$outrules[$rindex] = $tmprule;
162 b37a2e8c Phil Davis
			}
163 52c9f9fa Ermal
		}
164
165
166
		$state = "";
167 b37a2e8c Phil Davis
		if (!empty($outrules)) {
168 52c9f9fa Ermal
			$state = "no state";
169 b37a2e8c Phil Davis
		}
170 52c9f9fa Ermal
		ksort($inrules, SORT_NUMERIC);
171 b37a2e8c Phil Davis
		foreach ($inrules as $inrule) {
172 52c9f9fa Ermal
			$finalrules .= "{$inrule} {$state}\n";
173 b37a2e8c Phil Davis
		}
174 52c9f9fa Ermal
		if (!empty($outrules)) {
175
			ksort($outrules, SORT_NUMERIC);
176 b37a2e8c Phil Davis
			foreach ($outrules as $outrule) {
177 52c9f9fa Ermal
				$finalrules .= "{$outrule} {$state}\n";
178 b37a2e8c Phil Davis
			}
179 52c9f9fa Ermal
		}
180
	}
181
	return $finalrules;
182
}
183
184
$rules = parse_cisco_acl($attributes);
185
if (!empty($rules)) {
186 eb7d43c0 Ermal
	$pid = posix_getpid();
187 10d9290f Ermal
	@file_put_contents("/tmp/ipsec_{$pid}{$common_name}.rules", $rules);
188 7b27b18b Renato Botelho
	mwexec("/sbin/pfctl -a " . escapeshellarg("ipsec/{$common_name}") . " -f {$g['tmp_path']}/ipsec_{$pid}" . escapeshellarg($common_name) . ".rules");
189 10d9290f Ermal
	@unlink("{$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
190 52c9f9fa Ermal
}
191
192
?>