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