Project

General

Profile

Download (6.12 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * ipsec.attributes.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2011-2016 Rubicon Communications, LLC (Netgate)
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
function cisco_to_cidr($addr) {
62
	if (!is_ipaddr($addr)) {
63
		return 0;
64
	}
65
	$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

    
76
	$index = explode("#", $prule);
77
	if (is_numeric($index[1])) {
78
		return intval($index[1]);
79
	} else {
80
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
81
	}
82
	return -1;;
83
}
84

    
85
function parse_cisco_acl($attribs) {
86
	global $attributes;
87
	if (!is_array($attribs)) {
88
		return "";
89
	}
90

    
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
			} else if (strstr($rule[0], "outacl")) {
102
				$dir = "out";
103
			} else if (strstr($rule[0], "dns-servers")) {
104
				$attributes['dns-servers'] = explode(" ", $rule[1]);
105
				continue;
106
			} else if (strstr($rule[0], "route")) {
107
				if (!is_array($attributes['routes'])) {
108
					$attributes['routes'] = array();
109
				}
110
				$attributes['routes'][] = $rule[1];
111
				continue;
112
			}
113
			$rindex = cisco_extract_index($rule[0]);
114
			if ($rindex < 0) {
115
				continue;
116
			}
117

    
118
			$rule = $rule[1];
119
			$rule = explode(" ", $rule);
120
			$tmprule = "";
121
			$index = 0;
122
			$isblock = false;
123
			if ($rule[$index] == "permit") {
124
				$tmprule = "pass {$dir} quick on {$devname} ";
125
			} else if ($rule[$index] == "deny") {
126
				//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
				case "tcp":
137
				case "udp":
138
					$tmprule .= "proto {$rule[$index]} ";
139
					break;
140
			}
141

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

    
186
			if ($isblock == true) {
187
				continue;
188
			}
189

    
190
			if ($dir == "in") {
191
				$inrules[$rindex] = $tmprule;
192
			} else if ($dir == "out") {
193
				$outrules[$rindex] = $tmprule;
194
			}
195
		}
196

    
197

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

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

    
224
?>
(26-26/65)