Projet

Général

Profil

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

univnautes / etc / inc / openvpn.attributes.php @ 34bb5eb0

1
<?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
$devname = getenv("dev");
35
if (empty($devname))
36
	$devname = "openvpn";
37

    
38
function cisco_to_cidr($addr) {
39
	if (!is_ipaddr($addr))
40
		return 0;
41
	$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
	
52
	$index = explode("#", $prule);
53
	if (is_numeric($index[1]))
54
		return intval($index[1]);
55
	else
56
		syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
57
	return -1;;
58
}
59

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

    
113
			$index++;
114
			/* Source */
115
			if (trim($rule[$index]) == "host") {
116
				$index++;
117
				$tmprule .= "from {$rule[$index]} ";
118
				$index++;
119
				if ($isblock == true)
120
					$isblock = false;
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
			/* Destination */
134
			if (trim($rule[$index]) == "host") {
135
				$index++;
136
				$tmprule .= "to {$rule[$index]} ";
137
				$index++;
138
				if ($isblock == true)
139
					$isblock = false;
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
			if ($isblock == true)
154
				continue;
155

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

    
162

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

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

    
186
?>
(35-35/67)