Projet

Général

Profil

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

univnautes / etc / inc / openvpn.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
$devname = getenv("dev");
36
if (empty($devname))
37
	$devname = "openvpn";
38

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

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

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

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

    
104
			$index++;
105

    
106
			switch ($rule[$index]) {
107
			case "tcp":
108
			case "udp":
109
				$tmprule .= "proto {$rule[$index]} ";
110
				break;
111
				
112
			}
113

    
114
			$index++;
115
			/* Source */
116
			if (trim($rule[$index]) == "host") {
117
				$index++;
118
				$tmprule .= "from {$rule[$index]} ";
119
				$index++;
120
				if ($isblock == true)
121
					$isblock = false;
122
			} else if (trim($rule[$index]) == "any") {
123
				$tmprule .= "from any";
124
				$index++;
125
			} else {
126
				$tmprule .= "from {$rule[$index]}";
127
				$index++;
128
				$netmask = cisco_to_cidr($rule[$index]);
129
				$tmprule .= "/{$netmask} ";
130
				$index++;
131
				if ($isblock == true)
132
					$isblock = false;
133
			}
134
			/* Destination */
135
			if (trim($rule[$index]) == "host") {
136
				$index++;
137
				$tmprule .= "to {$rule[$index]} ";
138
				$index++;
139
				if ($isblock == true)
140
					$isblock = false;
141
			} else if (trim($rule[$index]) == "any") {
142
				$index++;
143
				$tmprule .= "to any";
144
			} else {
145
				$tmprule .= "to {$rule[$index]}";
146
				$index++;
147
				$netmask = cisco_to_cidr($rule[$index]);
148
				$tmprule .= "/{$netmask} ";
149
				$index++;
150
				if ($isblock == true)
151
					$isblock = false;
152
			}
153

    
154
			if ($isblock == true)
155
				continue;
156

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

    
163

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

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

    
187
?>
(36-36/68)