Project

General

Profile

Download (6.12 KB) Statistics
| Branch: | Tag: | Revision:
1 52c9f9fa Ermal
<?php
2
/*
3 8acd654a Renato Botelho
 * ipsec.attributes.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2011-2016 Rubicon Communications, LLC (Netgate)
7 8acd654a Renato Botelho
 * 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 52c9f9fa Ermal
54
if (empty($common_name)) {
55
	$common_name = getenv("common_name");
56 b37a2e8c Phil Davis
	if (empty($common_name)) {
57 52c9f9fa Ermal
		$common_name = getenv("username");
58 b37a2e8c Phil Davis
	}
59 52c9f9fa Ermal
}
60
61
function cisco_to_cidr($addr) {
62 b37a2e8c Phil Davis
	if (!is_ipaddr($addr)) {
63 52c9f9fa Ermal
		return 0;
64 b37a2e8c Phil Davis
	}
65 52c9f9fa Ermal
	$mask = decbin(~ip2long($addr));
66
	$mask = substr($mask, -32);
67
	$k = 0;
68
	for ($i = 0; $i <= 32; $i++) {
69
		$k += intval($mask[$i]);
70
	}
71
	return $k;
72
}
73
74
function cisco_extract_index($prule) {
75 b37a2e8c Phil Davis
76 52c9f9fa Ermal
	$index = explode("#", $prule);
77 b37a2e8c Phil Davis
	if (is_numeric($index[1])) {
78 52c9f9fa Ermal
		return intval($index[1]);
79 b37a2e8c Phil Davis
	} else {
80 52c9f9fa Ermal
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
81 b37a2e8c Phil Davis
	}
82 52c9f9fa Ermal
	return -1;;
83
}
84
85
function parse_cisco_acl($attribs) {
86
	global $attributes;
87 b37a2e8c Phil Davis
	if (!is_array($attribs)) {
88 52c9f9fa Ermal
		return "";
89 b37a2e8c Phil Davis
	}
90 52c9f9fa Ermal
91
	$devname = "enc0";
92
	$finalrules = "";
93
	if (is_array($attribs['ciscoavpair'])) {
94
		$inrules = array();
95
		$outrules = array();
96
		foreach ($attribs['ciscoavpair'] as $avrules) {
97
			$rule = explode("=", $avrules);
98
			$dir = "";
99
			if (strstr($rule[0], "inacl")) {
100
				$dir = "in";
101 b37a2e8c Phil Davis
			} else if (strstr($rule[0], "outacl")) {
102 52c9f9fa Ermal
				$dir = "out";
103 b37a2e8c Phil Davis
			} else if (strstr($rule[0], "dns-servers")) {
104 52c9f9fa Ermal
				$attributes['dns-servers'] = explode(" ", $rule[1]);
105
				continue;
106
			} else if (strstr($rule[0], "route")) {
107 b37a2e8c Phil Davis
				if (!is_array($attributes['routes'])) {
108 52c9f9fa Ermal
					$attributes['routes'] = array();
109 b37a2e8c Phil Davis
				}
110 45758be4 Ermal
				$attributes['routes'][] = $rule[1];
111 52c9f9fa Ermal
				continue;
112 b37a2e8c Phil Davis
			}
113 52c9f9fa Ermal
			$rindex = cisco_extract_index($rule[0]);
114 b37a2e8c Phil Davis
			if ($rindex < 0) {
115 52c9f9fa Ermal
				continue;
116 b37a2e8c Phil Davis
			}
117 52c9f9fa Ermal
118
			$rule = $rule[1];
119
			$rule = explode(" ", $rule);
120
			$tmprule = "";
121
			$index = 0;
122
			$isblock = false;
123 b37a2e8c Phil Davis
			if ($rule[$index] == "permit") {
124 52c9f9fa Ermal
				$tmprule = "pass {$dir} quick on {$devname} ";
125 b37a2e8c Phil Davis
			} else if ($rule[$index] == "deny") {
126 52c9f9fa Ermal
				//continue;
127
				$isblock = true;
128
				$tmprule = "block {$dir} quick on {$devname} ";
129
			} else {
130
				continue;
131
			}
132
133
			$index++;
134
135
			switch ($rule[$index]) {
136 b37a2e8c Phil Davis
				case "tcp":
137
				case "udp":
138
					$tmprule .= "proto {$rule[$index]} ";
139
					break;
140 52c9f9fa Ermal
			}
141
142
			$index++;
143
			/* Source */
144
			if (trim($rule[$index]) == "host") {
145
				$index++;
146
				$tmprule .= "from {$rule[$index]} ";
147
				$index++;
148 b37a2e8c Phil Davis
				if ($isblock == true) {
149 52c9f9fa Ermal
					$isblock = false;
150 b37a2e8c Phil Davis
				}
151 52c9f9fa Ermal
			} else if (trim($rule[$index]) == "any") {
152
				$tmprule .= "from any";
153
				$index++;
154
			} else {
155 3f4bd83b Renato Botelho
				$tmprule .= "from {$rule[$index]}";
156 52c9f9fa Ermal
				$index++;
157
				$netmask = cisco_to_cidr($rule[$index]);
158
				$tmprule .= "/{$netmask} ";
159
				$index++;
160 b37a2e8c Phil Davis
				if ($isblock == true) {
161 52c9f9fa Ermal
					$isblock = false;
162 b37a2e8c Phil Davis
				}
163 52c9f9fa Ermal
			}
164
			/* Destination */
165
			if (trim($rule[$index]) == "host") {
166
				$index++;
167
				$tmprule .= "to {$rule[$index]} ";
168
				$index++;
169 b37a2e8c Phil Davis
				if ($isblock == true) {
170 52c9f9fa Ermal
					$isblock = false;
171 b37a2e8c Phil Davis
				}
172 52c9f9fa Ermal
			} else if (trim($rule[$index]) == "any") {
173
				$index++;
174
				$tmprule .= "to any";
175
			} else {
176 3f4bd83b Renato Botelho
				$tmprule .= "to {$rule[$index]}";
177 52c9f9fa Ermal
				$index++;
178
				$netmask = cisco_to_cidr($rule[$index]);
179
				$tmprule .= "/{$netmask} ";
180
				$index++;
181 b37a2e8c Phil Davis
				if ($isblock == true) {
182 52c9f9fa Ermal
					$isblock = false;
183 b37a2e8c Phil Davis
				}
184 52c9f9fa Ermal
			}
185
186 b37a2e8c Phil Davis
			if ($isblock == true) {
187 52c9f9fa Ermal
				continue;
188 b37a2e8c Phil Davis
			}
189 52c9f9fa Ermal
190 b37a2e8c Phil Davis
			if ($dir == "in") {
191 52c9f9fa Ermal
				$inrules[$rindex] = $tmprule;
192 b37a2e8c Phil Davis
			} else if ($dir == "out") {
193 52c9f9fa Ermal
				$outrules[$rindex] = $tmprule;
194 b37a2e8c Phil Davis
			}
195 52c9f9fa Ermal
		}
196
197
198
		$state = "";
199 b37a2e8c Phil Davis
		if (!empty($outrules)) {
200 52c9f9fa Ermal
			$state = "no state";
201 b37a2e8c Phil Davis
		}
202 52c9f9fa Ermal
		ksort($inrules, SORT_NUMERIC);
203 b37a2e8c Phil Davis
		foreach ($inrules as $inrule) {
204 52c9f9fa Ermal
			$finalrules .= "{$inrule} {$state}\n";
205 b37a2e8c Phil Davis
		}
206 52c9f9fa Ermal
		if (!empty($outrules)) {
207
			ksort($outrules, SORT_NUMERIC);
208 b37a2e8c Phil Davis
			foreach ($outrules as $outrule) {
209 52c9f9fa Ermal
				$finalrules .= "{$outrule} {$state}\n";
210 b37a2e8c Phil Davis
			}
211 52c9f9fa Ermal
		}
212
	}
213
	return $finalrules;
214
}
215
216
$rules = parse_cisco_acl($attributes);
217
if (!empty($rules)) {
218 eb7d43c0 Ermal
	$pid = posix_getpid();
219 10d9290f Ermal
	@file_put_contents("/tmp/ipsec_{$pid}{$common_name}.rules", $rules);
220 7b27b18b Renato Botelho
	mwexec("/sbin/pfctl -a " . escapeshellarg("ipsec/{$common_name}") . " -f {$g['tmp_path']}/ipsec_{$pid}" . escapeshellarg($common_name) . ".rules");
221 10d9290f Ermal
	@unlink("{$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
222 52c9f9fa Ermal
}
223
224
?>