1 |
52c9f9fa
|
Ermal
|
<?php
|
2 |
|
|
/*
|
3 |
ce77a9c4
|
Phil Davis
|
ipsec.attributes.php
|
4 |
b37a2e8c
|
Phil Davis
|
Copyright (C) 2011-2012 Ermal Luçi
|
5 |
ce77a9c4
|
Phil Davis
|
Copyright (C) 2013-2015 Electric Sheep Fencing, LP
|
6 |
|
|
All rights reserved.
|
7 |
|
|
|
8 |
|
|
Redistribution and use in source and binary forms, with or without
|
9 |
|
|
modification, are permitted provided that the following conditions are met:
|
10 |
|
|
|
11 |
|
|
1. Redistributions of source code must retain the above copyright notice,
|
12 |
|
|
this list of conditions and the following disclaimer.
|
13 |
|
|
|
14 |
|
|
2. Redistributions in binary form must reproduce the above copyright
|
15 |
|
|
notice, this list of conditions and the following disclaimer in the
|
16 |
|
|
documentation and/or other materials provided with the distribution.
|
17 |
|
|
|
18 |
|
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19 |
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20 |
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
21 |
|
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
22 |
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
23 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
25 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
26 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
|
|
POSSIBILITY OF SUCH DAMAGE.
|
28 |
52c9f9fa
|
Ermal
|
*/
|
29 |
|
|
|
30 |
|
|
if (empty($common_name)) {
|
31 |
|
|
$common_name = getenv("common_name");
|
32 |
b37a2e8c
|
Phil Davis
|
if (empty($common_name)) {
|
33 |
52c9f9fa
|
Ermal
|
$common_name = getenv("username");
|
34 |
b37a2e8c
|
Phil Davis
|
}
|
35 |
52c9f9fa
|
Ermal
|
}
|
36 |
|
|
|
37 |
|
|
function cisco_to_cidr($addr) {
|
38 |
b37a2e8c
|
Phil Davis
|
if (!is_ipaddr($addr)) {
|
39 |
52c9f9fa
|
Ermal
|
return 0;
|
40 |
b37a2e8c
|
Phil Davis
|
}
|
41 |
52c9f9fa
|
Ermal
|
$mask = decbin(~ip2long($addr));
|
42 |
|
|
$mask = substr($mask, -32);
|
43 |
|
|
$k = 0;
|
44 |
|
|
for ($i = 0; $i <= 32; $i++) {
|
45 |
|
|
$k += intval($mask[$i]);
|
46 |
|
|
}
|
47 |
|
|
return $k;
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
function cisco_extract_index($prule) {
|
51 |
b37a2e8c
|
Phil Davis
|
|
52 |
52c9f9fa
|
Ermal
|
$index = explode("#", $prule);
|
53 |
b37a2e8c
|
Phil Davis
|
if (is_numeric($index[1])) {
|
54 |
52c9f9fa
|
Ermal
|
return intval($index[1]);
|
55 |
b37a2e8c
|
Phil Davis
|
} else {
|
56 |
52c9f9fa
|
Ermal
|
syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
|
57 |
b37a2e8c
|
Phil Davis
|
}
|
58 |
52c9f9fa
|
Ermal
|
return -1;;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
function parse_cisco_acl($attribs) {
|
62 |
|
|
global $attributes;
|
63 |
b37a2e8c
|
Phil Davis
|
if (!is_array($attribs)) {
|
64 |
52c9f9fa
|
Ermal
|
return "";
|
65 |
b37a2e8c
|
Phil Davis
|
}
|
66 |
52c9f9fa
|
Ermal
|
|
67 |
|
|
$devname = "enc0";
|
68 |
|
|
$finalrules = "";
|
69 |
|
|
if (is_array($attribs['ciscoavpair'])) {
|
70 |
|
|
$inrules = array();
|
71 |
|
|
$outrules = array();
|
72 |
|
|
foreach ($attribs['ciscoavpair'] as $avrules) {
|
73 |
|
|
$rule = explode("=", $avrules);
|
74 |
|
|
$dir = "";
|
75 |
|
|
if (strstr($rule[0], "inacl")) {
|
76 |
|
|
$dir = "in";
|
77 |
b37a2e8c
|
Phil Davis
|
} else if (strstr($rule[0], "outacl")) {
|
78 |
52c9f9fa
|
Ermal
|
$dir = "out";
|
79 |
b37a2e8c
|
Phil Davis
|
} else if (strstr($rule[0], "dns-servers")) {
|
80 |
52c9f9fa
|
Ermal
|
$attributes['dns-servers'] = explode(" ", $rule[1]);
|
81 |
|
|
continue;
|
82 |
|
|
} else if (strstr($rule[0], "route")) {
|
83 |
b37a2e8c
|
Phil Davis
|
if (!is_array($attributes['routes'])) {
|
84 |
52c9f9fa
|
Ermal
|
$attributes['routes'] = array();
|
85 |
b37a2e8c
|
Phil Davis
|
}
|
86 |
45758be4
|
Ermal
|
$attributes['routes'][] = $rule[1];
|
87 |
52c9f9fa
|
Ermal
|
continue;
|
88 |
b37a2e8c
|
Phil Davis
|
}
|
89 |
52c9f9fa
|
Ermal
|
$rindex = cisco_extract_index($rule[0]);
|
90 |
b37a2e8c
|
Phil Davis
|
if ($rindex < 0) {
|
91 |
52c9f9fa
|
Ermal
|
continue;
|
92 |
b37a2e8c
|
Phil Davis
|
}
|
93 |
52c9f9fa
|
Ermal
|
|
94 |
|
|
$rule = $rule[1];
|
95 |
|
|
$rule = explode(" ", $rule);
|
96 |
|
|
$tmprule = "";
|
97 |
|
|
$index = 0;
|
98 |
|
|
$isblock = false;
|
99 |
b37a2e8c
|
Phil Davis
|
if ($rule[$index] == "permit") {
|
100 |
52c9f9fa
|
Ermal
|
$tmprule = "pass {$dir} quick on {$devname} ";
|
101 |
b37a2e8c
|
Phil Davis
|
} else if ($rule[$index] == "deny") {
|
102 |
52c9f9fa
|
Ermal
|
//continue;
|
103 |
|
|
$isblock = true;
|
104 |
|
|
$tmprule = "block {$dir} quick on {$devname} ";
|
105 |
|
|
} else {
|
106 |
|
|
continue;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
$index++;
|
110 |
|
|
|
111 |
|
|
switch ($rule[$index]) {
|
112 |
b37a2e8c
|
Phil Davis
|
case "tcp":
|
113 |
|
|
case "udp":
|
114 |
|
|
$tmprule .= "proto {$rule[$index]} ";
|
115 |
|
|
break;
|
116 |
52c9f9fa
|
Ermal
|
}
|
117 |
|
|
|
118 |
|
|
$index++;
|
119 |
|
|
/* Source */
|
120 |
|
|
if (trim($rule[$index]) == "host") {
|
121 |
|
|
$index++;
|
122 |
|
|
$tmprule .= "from {$rule[$index]} ";
|
123 |
|
|
$index++;
|
124 |
b37a2e8c
|
Phil Davis
|
if ($isblock == true) {
|
125 |
52c9f9fa
|
Ermal
|
$isblock = false;
|
126 |
b37a2e8c
|
Phil Davis
|
}
|
127 |
52c9f9fa
|
Ermal
|
} else if (trim($rule[$index]) == "any") {
|
128 |
|
|
$tmprule .= "from any";
|
129 |
|
|
$index++;
|
130 |
|
|
} else {
|
131 |
3f4bd83b
|
Renato Botelho
|
$tmprule .= "from {$rule[$index]}";
|
132 |
52c9f9fa
|
Ermal
|
$index++;
|
133 |
|
|
$netmask = cisco_to_cidr($rule[$index]);
|
134 |
|
|
$tmprule .= "/{$netmask} ";
|
135 |
|
|
$index++;
|
136 |
b37a2e8c
|
Phil Davis
|
if ($isblock == true) {
|
137 |
52c9f9fa
|
Ermal
|
$isblock = false;
|
138 |
b37a2e8c
|
Phil Davis
|
}
|
139 |
52c9f9fa
|
Ermal
|
}
|
140 |
|
|
/* Destination */
|
141 |
|
|
if (trim($rule[$index]) == "host") {
|
142 |
|
|
$index++;
|
143 |
|
|
$tmprule .= "to {$rule[$index]} ";
|
144 |
|
|
$index++;
|
145 |
b37a2e8c
|
Phil Davis
|
if ($isblock == true) {
|
146 |
52c9f9fa
|
Ermal
|
$isblock = false;
|
147 |
b37a2e8c
|
Phil Davis
|
}
|
148 |
52c9f9fa
|
Ermal
|
} else if (trim($rule[$index]) == "any") {
|
149 |
|
|
$index++;
|
150 |
|
|
$tmprule .= "to any";
|
151 |
|
|
} else {
|
152 |
3f4bd83b
|
Renato Botelho
|
$tmprule .= "to {$rule[$index]}";
|
153 |
52c9f9fa
|
Ermal
|
$index++;
|
154 |
|
|
$netmask = cisco_to_cidr($rule[$index]);
|
155 |
|
|
$tmprule .= "/{$netmask} ";
|
156 |
|
|
$index++;
|
157 |
b37a2e8c
|
Phil Davis
|
if ($isblock == true) {
|
158 |
52c9f9fa
|
Ermal
|
$isblock = false;
|
159 |
b37a2e8c
|
Phil Davis
|
}
|
160 |
52c9f9fa
|
Ermal
|
}
|
161 |
|
|
|
162 |
b37a2e8c
|
Phil Davis
|
if ($isblock == true) {
|
163 |
52c9f9fa
|
Ermal
|
continue;
|
164 |
b37a2e8c
|
Phil Davis
|
}
|
165 |
52c9f9fa
|
Ermal
|
|
166 |
b37a2e8c
|
Phil Davis
|
if ($dir == "in") {
|
167 |
52c9f9fa
|
Ermal
|
$inrules[$rindex] = $tmprule;
|
168 |
b37a2e8c
|
Phil Davis
|
} else if ($dir == "out") {
|
169 |
52c9f9fa
|
Ermal
|
$outrules[$rindex] = $tmprule;
|
170 |
b37a2e8c
|
Phil Davis
|
}
|
171 |
52c9f9fa
|
Ermal
|
}
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
$state = "";
|
175 |
b37a2e8c
|
Phil Davis
|
if (!empty($outrules)) {
|
176 |
52c9f9fa
|
Ermal
|
$state = "no state";
|
177 |
b37a2e8c
|
Phil Davis
|
}
|
178 |
52c9f9fa
|
Ermal
|
ksort($inrules, SORT_NUMERIC);
|
179 |
b37a2e8c
|
Phil Davis
|
foreach ($inrules as $inrule) {
|
180 |
52c9f9fa
|
Ermal
|
$finalrules .= "{$inrule} {$state}\n";
|
181 |
b37a2e8c
|
Phil Davis
|
}
|
182 |
52c9f9fa
|
Ermal
|
if (!empty($outrules)) {
|
183 |
|
|
ksort($outrules, SORT_NUMERIC);
|
184 |
b37a2e8c
|
Phil Davis
|
foreach ($outrules as $outrule) {
|
185 |
52c9f9fa
|
Ermal
|
$finalrules .= "{$outrule} {$state}\n";
|
186 |
b37a2e8c
|
Phil Davis
|
}
|
187 |
52c9f9fa
|
Ermal
|
}
|
188 |
|
|
}
|
189 |
|
|
return $finalrules;
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
$rules = parse_cisco_acl($attributes);
|
193 |
|
|
if (!empty($rules)) {
|
194 |
eb7d43c0
|
Ermal
|
$pid = posix_getpid();
|
195 |
10d9290f
|
Ermal
|
@file_put_contents("/tmp/ipsec_{$pid}{$common_name}.rules", $rules);
|
196 |
7b27b18b
|
Renato Botelho
|
mwexec("/sbin/pfctl -a " . escapeshellarg("ipsec/{$common_name}") . " -f {$g['tmp_path']}/ipsec_{$pid}" . escapeshellarg($common_name) . ".rules");
|
197 |
10d9290f
|
Ermal
|
@unlink("{$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
|
198 |
52c9f9fa
|
Ermal
|
}
|
199 |
|
|
|
200 |
|
|
?>
|