1 |
52c9f9fa
|
Ermal
|
<?php
|
2 |
|
|
/*
|
3 |
|
|
Copyright (C) 2011-2012 Ermal Luçi
|
4 |
|
|
All rights reserved.
|
5 |
|
|
|
6 |
|
|
Redistribution and use in source and binary forms, with or without
|
7 |
|
|
modification, are permitted provided that the following conditions are met:
|
8 |
|
|
|
9 |
|
|
1. Redistributions of source code must retain the above copyright notice,
|
10 |
|
|
this list of conditions and the following disclaimer.
|
11 |
|
|
|
12 |
|
|
2. Redistributions in binary form must reproduce the above copyright
|
13 |
|
|
notice, this list of conditions and the following disclaimer in the
|
14 |
|
|
documentation and/or other materials provided with the distribution.
|
15 |
|
|
|
16 |
|
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
17 |
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
18 |
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
19 |
|
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
20 |
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
21 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
24 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
25 |
|
|
POSSIBILITY OF SUCH DAMAGE.
|
26 |
|
|
*/
|
27 |
|
|
|
28 |
|
|
if (empty($common_name)) {
|
29 |
|
|
$common_name = getenv("common_name");
|
30 |
|
|
if (empty($common_name))
|
31 |
|
|
$common_name = getenv("username");
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
function cisco_to_cidr($addr) {
|
35 |
|
|
if (!is_ipaddr($addr))
|
36 |
|
|
return 0;
|
37 |
|
|
$mask = decbin(~ip2long($addr));
|
38 |
|
|
$mask = substr($mask, -32);
|
39 |
|
|
$k = 0;
|
40 |
|
|
for ($i = 0; $i <= 32; $i++) {
|
41 |
|
|
$k += intval($mask[$i]);
|
42 |
|
|
}
|
43 |
|
|
return $k;
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
function cisco_extract_index($prule) {
|
47 |
|
|
|
48 |
|
|
$index = explode("#", $prule);
|
49 |
|
|
if (is_numeric($index[1]))
|
50 |
|
|
return intval($index[1]);
|
51 |
|
|
else
|
52 |
|
|
syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
|
53 |
|
|
return -1;;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
function parse_cisco_acl($attribs) {
|
57 |
|
|
global $attributes;
|
58 |
|
|
if (!is_array($attribs))
|
59 |
|
|
return "";
|
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 |
c4844c2c
|
Ermal
|
$attributes['routes'][] = $rule[1];
|
80 |
52c9f9fa
|
Ermal
|
continue;
|
81 |
|
|
}
|
82 |
|
|
$rindex = cisco_extract_index($rule[0]);
|
83 |
|
|
if ($rindex < 0)
|
84 |
|
|
continue;
|
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 |
|
|
|
111 |
|
|
$index++;
|
112 |
|
|
/* Source */
|
113 |
|
|
if (trim($rule[$index]) == "host") {
|
114 |
|
|
$index++;
|
115 |
|
|
$tmprule .= "from {$rule[$index]} ";
|
116 |
|
|
$index++;
|
117 |
|
|
if ($isblock == true)
|
118 |
|
|
$isblock = false;
|
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 |
|
|
/* Destination */
|
132 |
|
|
if (trim($rule[$index]) == "host") {
|
133 |
|
|
$index++;
|
134 |
|
|
$tmprule .= "to {$rule[$index]} ";
|
135 |
|
|
$index++;
|
136 |
|
|
if ($isblock == true)
|
137 |
|
|
$isblock = false;
|
138 |
|
|
} else if (trim($rule[$index]) == "any") {
|
139 |
|
|
$index++;
|
140 |
|
|
$tmprule .= "to any";
|
141 |
|
|
} else {
|
142 |
|
|
$tmprule .= "to $rule[$index]";
|
143 |
|
|
$index++;
|
144 |
|
|
$netmask = cisco_to_cidr($rule[$index]);
|
145 |
|
|
$tmprule .= "/{$netmask} ";
|
146 |
|
|
$index++;
|
147 |
|
|
if ($isblock == true)
|
148 |
|
|
$isblock = false;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
if ($isblock == true)
|
152 |
|
|
continue;
|
153 |
|
|
|
154 |
|
|
if ($dir == "in")
|
155 |
|
|
$inrules[$rindex] = $tmprule;
|
156 |
|
|
else if ($dir == "out")
|
157 |
|
|
$outrules[$rindex] = $tmprule;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
$state = "";
|
162 |
|
|
if (!empty($outrules))
|
163 |
|
|
$state = "no state";
|
164 |
|
|
ksort($inrules, SORT_NUMERIC);
|
165 |
|
|
foreach ($inrules as $inrule)
|
166 |
|
|
$finalrules .= "{$inrule} {$state}\n";
|
167 |
|
|
if (!empty($outrules)) {
|
168 |
|
|
ksort($outrules, SORT_NUMERIC);
|
169 |
|
|
foreach ($outrules as $outrule)
|
170 |
|
|
$finalrules .= "{$outrule} {$state}\n";
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
return $finalrules;
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
$rules = parse_cisco_acl($attributes);
|
177 |
|
|
if (!empty($rules)) {
|
178 |
56fbff2e
|
Ermal
|
$pid = posix_getpid();
|
179 |
599d564e
|
Ermal
|
@file_put_contents("/tmp/ipsec_{$pid}{$common_name}.rules", $rules);
|
180 |
|
|
mwexec("/sbin/pfctl -a \"ipsec/{$common_name}\" -f {$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
|
181 |
|
|
@unlink("{$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
|
182 |
52c9f9fa
|
Ermal
|
}
|
183 |
|
|
|
184 |
|
|
?>
|