1
|
<?php
|
2
|
/*
|
3
|
* ipsec.attributes.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2011-2013 BSD Perimeter
|
7
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
8
|
* Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
|
9
|
* All rights reserved.
|
10
|
*
|
11
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
12
|
* you may not use this file except in compliance with the License.
|
13
|
* You may obtain a copy of the License at
|
14
|
*
|
15
|
* http://www.apache.org/licenses/LICENSE-2.0
|
16
|
*
|
17
|
* Unless required by applicable law or agreed to in writing, software
|
18
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
19
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
20
|
* See the License for the specific language governing permissions and
|
21
|
* limitations under the License.
|
22
|
*/
|
23
|
|
24
|
if (empty($common_name)) {
|
25
|
$common_name = getenv("common_name");
|
26
|
if (empty($common_name)) {
|
27
|
$common_name = getenv("username");
|
28
|
}
|
29
|
}
|
30
|
|
31
|
function cisco_to_cidr($addr) {
|
32
|
if (!is_ipaddr($addr)) {
|
33
|
return 0;
|
34
|
}
|
35
|
$mask = decbin(~ip2long($addr));
|
36
|
$mask = substr($mask, -32);
|
37
|
$k = 0;
|
38
|
for ($i = 0; $i <= 32; $i++) {
|
39
|
$k += intval($mask[$i]);
|
40
|
}
|
41
|
return $k;
|
42
|
}
|
43
|
|
44
|
function cisco_extract_index($prule) {
|
45
|
|
46
|
$index = explode("#", $prule);
|
47
|
if (is_numeric($index[1])) {
|
48
|
return intval($index[1]);
|
49
|
} else {
|
50
|
syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
|
51
|
}
|
52
|
return -1;;
|
53
|
}
|
54
|
|
55
|
function parse_cisco_acl($attribs) {
|
56
|
global $attributes;
|
57
|
if (!is_array($attribs)) {
|
58
|
return "";
|
59
|
}
|
60
|
|
61
|
$devname = "enc0";
|
62
|
$finalrules = "";
|
63
|
if (is_array($attribs['ciscoavpair'])) {
|
64
|
$inrules = array();
|
65
|
$outrules = array();
|
66
|
foreach ($attribs['ciscoavpair'] as $avrules) {
|
67
|
$rule = explode("=", $avrules);
|
68
|
$dir = "";
|
69
|
if (strstr($rule[0], "inacl")) {
|
70
|
$dir = "in";
|
71
|
} else if (strstr($rule[0], "outacl")) {
|
72
|
$dir = "out";
|
73
|
} else if (strstr($rule[0], "dns-servers")) {
|
74
|
$attributes['dns-servers'] = explode(" ", $rule[1]);
|
75
|
continue;
|
76
|
} else if (strstr($rule[0], "route")) {
|
77
|
if (!is_array($attributes['routes'])) {
|
78
|
$attributes['routes'] = array();
|
79
|
}
|
80
|
$attributes['routes'][] = $rule[1];
|
81
|
continue;
|
82
|
}
|
83
|
$rindex = cisco_extract_index($rule[0]);
|
84
|
if ($rindex < 0) {
|
85
|
continue;
|
86
|
}
|
87
|
|
88
|
$rule = $rule[1];
|
89
|
$rule = explode(" ", $rule);
|
90
|
$tmprule = "";
|
91
|
$index = 0;
|
92
|
$isblock = false;
|
93
|
if ($rule[$index] == "permit") {
|
94
|
$tmprule = "pass {$dir} quick on {$devname} ";
|
95
|
} else if ($rule[$index] == "deny") {
|
96
|
//continue;
|
97
|
$isblock = true;
|
98
|
$tmprule = "block {$dir} quick on {$devname} ";
|
99
|
} else {
|
100
|
continue;
|
101
|
}
|
102
|
|
103
|
$index++;
|
104
|
|
105
|
switch ($rule[$index]) {
|
106
|
case "tcp":
|
107
|
case "udp":
|
108
|
$tmprule .= "proto {$rule[$index]} ";
|
109
|
break;
|
110
|
}
|
111
|
|
112
|
$index++;
|
113
|
/* Source */
|
114
|
if (trim($rule[$index]) == "host") {
|
115
|
$index++;
|
116
|
$tmprule .= "from {$rule[$index]} ";
|
117
|
$index++;
|
118
|
if ($isblock == true) {
|
119
|
$isblock = false;
|
120
|
}
|
121
|
} else if (trim($rule[$index]) == "any") {
|
122
|
$tmprule .= "from any ";
|
123
|
$index++;
|
124
|
} else {
|
125
|
$tmprule .= "from {$rule[$index]} ";
|
126
|
$index++;
|
127
|
$netmask = cisco_to_cidr($rule[$index]);
|
128
|
$tmprule .= "/{$netmask} ";
|
129
|
$index++;
|
130
|
if ($isblock == true) {
|
131
|
$isblock = false;
|
132
|
}
|
133
|
}
|
134
|
/* Destination */
|
135
|
if (trim($rule[$index]) == "host") {
|
136
|
$index++;
|
137
|
$tmprule .= "to {$rule[$index]} ";
|
138
|
$index++;
|
139
|
if ($isblock == true) {
|
140
|
$isblock = false;
|
141
|
}
|
142
|
} else if (trim($rule[$index]) == "any") {
|
143
|
$index++;
|
144
|
$tmprule .= "to any";
|
145
|
} else {
|
146
|
$tmprule .= "to {$rule[$index]}";
|
147
|
$index++;
|
148
|
$netmask = cisco_to_cidr($rule[$index]);
|
149
|
$tmprule .= "/{$netmask} ";
|
150
|
$index++;
|
151
|
if ($isblock == true) {
|
152
|
$isblock = false;
|
153
|
}
|
154
|
}
|
155
|
|
156
|
if ($isblock == true) {
|
157
|
continue;
|
158
|
}
|
159
|
|
160
|
if ($dir == "in") {
|
161
|
$inrules[$rindex] = $tmprule;
|
162
|
} else if ($dir == "out") {
|
163
|
$outrules[$rindex] = $tmprule;
|
164
|
}
|
165
|
}
|
166
|
|
167
|
|
168
|
$state = "";
|
169
|
if (!empty($outrules)) {
|
170
|
$state = "no state";
|
171
|
}
|
172
|
ksort($inrules, SORT_NUMERIC);
|
173
|
foreach ($inrules as $inrule) {
|
174
|
$finalrules .= "{$inrule} {$state}\n";
|
175
|
}
|
176
|
if (!empty($outrules)) {
|
177
|
ksort($outrules, SORT_NUMERIC);
|
178
|
foreach ($outrules as $outrule) {
|
179
|
$finalrules .= "{$outrule} {$state}\n";
|
180
|
}
|
181
|
}
|
182
|
}
|
183
|
return $finalrules;
|
184
|
}
|
185
|
|
186
|
$rules = parse_cisco_acl($attributes);
|
187
|
if (!empty($rules)) {
|
188
|
$pid = posix_getpid();
|
189
|
@file_put_contents("/tmp/ipsec_{$pid}{$common_name}.rules", $rules);
|
190
|
mwexec("/sbin/pfctl -a " . escapeshellarg("ipsec/{$common_name}") . " -f {$g['tmp_path']}/ipsec_{$pid}" . escapeshellarg($common_name) . ".rules");
|
191
|
@unlink("{$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
|
192
|
}
|
193
|
|
194
|
?>
|