Projet

Général

Profil

Télécharger (5,07 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / ipsec.attributes.php @ 340ce958

1
<?php
2
/*
3
        Copyright (C) 2011-2012         Ermal Luçi
4
        Copyright (C) 2013-2014 Electric Sheep Fencing, LP
5
        All rights reserved.
6

    
7
        Redistribution and use in source and binary forms, with or without
8
        modification, are permitted provided that the following conditions are met:
9

    
10
        1. Redistributions of source code must retain the above copyright notice,
11
           this list of conditions and the following disclaimer.
12

    
13
        2. Redistributions in binary form must reproduce the above copyright
14
           notice, this list of conditions and the following disclaimer in the
15
           documentation and/or other materials provided with the distribution.
16

    
17
        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
        POSSIBILITY OF SUCH DAMAGE.
27
*/
28

    
29
if (empty($common_name)) {
30
	$common_name = getenv("common_name");
31
	if (empty($common_name))
32
		$common_name = getenv("username");
33
}
34

    
35
function cisco_to_cidr($addr) {
36
	if (!is_ipaddr($addr))
37
		return 0;
38
	$mask = decbin(~ip2long($addr));
39
	$mask = substr($mask, -32);
40
	$k = 0;
41
	for ($i = 0; $i <= 32; $i++) {
42
		$k += intval($mask[$i]);
43
	}
44
	return $k;
45
}
46

    
47
function cisco_extract_index($prule) {
48
	
49
	$index = explode("#", $prule);
50
	if (is_numeric($index[1]))
51
		return intval($index[1]);
52
	else
53
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
54
	return -1;;
55
}
56

    
57
function parse_cisco_acl($attribs) {
58
	global $attributes;
59
	if (!is_array($attribs))
60
		return "";
61

    
62
	$devname = "enc0";
63
	$finalrules = "";
64
	if (is_array($attribs['ciscoavpair'])) {
65
		$inrules = array();
66
		$outrules = array();
67
		foreach ($attribs['ciscoavpair'] as $avrules) {
68
			$rule = explode("=", $avrules);
69
			$dir = "";
70
			if (strstr($rule[0], "inacl")) {
71
				$dir = "in";
72
			} else if (strstr($rule[0], "outacl"))
73
				$dir = "out";
74
			else if (strstr($rule[0], "dns-servers")) {
75
				$attributes['dns-servers'] = explode(" ", $rule[1]);
76
				continue;
77
			} else if (strstr($rule[0], "route")) {
78
				if (!is_array($attributes['routes']))
79
					$attributes['routes'] = array();
80
				$attributes['routes'][] = $rule[1];
81
				continue;
82
			}	
83
			$rindex = cisco_extract_index($rule[0]);
84
			if ($rindex < 0)
85
				continue;
86

    
87
			$rule = $rule[1];
88
			$rule = explode(" ", $rule);
89
			$tmprule = "";
90
			$index = 0;
91
			$isblock = false;
92
			if ($rule[$index] == "permit")
93
				$tmprule = "pass {$dir} quick on {$devname} ";
94
			else if ($rule[$index] == "deny") {
95
				//continue;
96
				$isblock = true;
97
				$tmprule = "block {$dir} quick on {$devname} ";
98
			} else {
99
				continue;
100
			}
101

    
102
			$index++;
103

    
104
			switch ($rule[$index]) {
105
			case "tcp":
106
			case "udp":
107
				$tmprule .= "proto {$rule[$index]} ";
108
				break;
109
				
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
			} else if (trim($rule[$index]) == "any") {
121
				$tmprule .= "from any";
122
				$index++;
123
			} else {
124
				$tmprule .= "from {$rule[$index]}";
125
				$index++;
126
				$netmask = cisco_to_cidr($rule[$index]);
127
				$tmprule .= "/{$netmask} ";
128
				$index++;
129
				if ($isblock == true)
130
					$isblock = false;
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
			} else if (trim($rule[$index]) == "any") {
140
				$index++;
141
				$tmprule .= "to any";
142
			} else {
143
				$tmprule .= "to {$rule[$index]}";
144
				$index++;
145
				$netmask = cisco_to_cidr($rule[$index]);
146
				$tmprule .= "/{$netmask} ";
147
				$index++;
148
				if ($isblock == true)
149
					$isblock = false;
150
			}
151

    
152
			if ($isblock == true)
153
				continue;
154

    
155
			if ($dir == "in")
156
				$inrules[$rindex] = $tmprule;
157
			else if ($dir == "out")
158
				$outrules[$rindex] = $tmprule;
159
		}
160

    
161

    
162
		$state = "";
163
		if (!empty($outrules))
164
			$state = "no state";
165
		ksort($inrules, SORT_NUMERIC);
166
		foreach ($inrules as $inrule)
167
			$finalrules .= "{$inrule} {$state}\n";
168
		if (!empty($outrules)) {
169
			ksort($outrules, SORT_NUMERIC);
170
			foreach ($outrules as $outrule)
171
				$finalrules .= "{$outrule} {$state}\n";
172
		}
173
	}
174
	return $finalrules;
175
}
176

    
177
$rules = parse_cisco_acl($attributes);
178
if (!empty($rules)) {
179
	$pid = posix_getpid();
180
	@file_put_contents("/tmp/ipsec_{$pid}{$common_name}.rules", $rules);
181
	mwexec("/sbin/pfctl -a " . escapeshellarg("ipsec/{$common_name}") . " -f {$g['tmp_path']}/ipsec_{$pid}" . escapeshellarg($common_name) . ".rules");
182
	@unlink("{$g['tmp_path']}/ipsec_{$pid}{$common_name}.rules");
183
}
184

    
185
?>
(27-27/68)