Project

General

Profile

« Previous | Next » 

Revision 1a69c4d1

Added by Ermal LUÇI about 13 years ago

Wrong branch

Revert "Import OpenVPN cisco style radius attributes applying policy to logged in users. Feature #2100"

This reverts commit 477cc2bc24b4b0a36b2bc765c1bb4d79a2eacaed.

View differences:

etc/inc/auth.inc
1099 1099
	return true;
1100 1100
}
1101 1101

  
1102
function radius_backed($username, $passwd, $authcfg, &$attributes = array()) {
1102
function radius_backed($username, $passwd, $authcfg){
1103 1103
	global $debug, $config;
1104 1104
	$ret = false;
1105 1105

  
......
1135 1135
		if ($debug)
1136 1136
			printf("Radius send failed: %s<br>\n", $retvalue['error']);
1137 1137
	} else if ($result === true) {
1138
		if ($rauth->getAttributes())
1139
			$attributes = $rauth->listAttributes();
1140 1138
		$retvalue['auth_val'] = 2;
1141 1139
		if ($debug)
1142 1140
			printf(gettext("Radius Auth succeeded")."<br>\n");
......
1233 1231
	return $member_groups;
1234 1232
}
1235 1233

  
1236
function authenticate_user($username, $password, $authcfg = NULL, &$attributes = array()) {
1234
function authenticate_user($username, $password, $authcfg = NULL) {
1237 1235

  
1238 1236
	if (!$authcfg) {
1239 1237
		return local_backed($username, $password);
......
1246 1244
                        $authenticated = true;
1247 1245
                break;
1248 1246
        case 'radius':
1249
                if (radius_backed($username, $password, $authcfg, $attributes))
1247
                if (radius_backed($username, $password, $authcfg))
1250 1248
                        $authenticated = true;
1251 1249
                break;
1252 1250
        default:
etc/inc/filter.inc
2131 2131

  
2132 2132
	/* relayd */
2133 2133
	$ipfrules .= "anchor \"relayd/*\"\n";
2134
	/* OpenVPN user rules from radius */
2135
	$ipfrules .= "anchor \"openvpn/*\"\n";
2136 2134
	# BEGIN OF firewall rules
2137 2135
	/* default block logging? */
2138 2136
	if(!isset($config['syslog']['nologdefaultblock']))
etc/inc/openvpn.attributes.php
1
<?php
2
/*
3
        filter.inc
4
        Copyright (C) 2011-2012         Ermal Luçi
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'][] = $route[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
	@file_put_contents("/tmp/{$common_name}.rules", $rules);
182
	mwexec("/sbin/pfctl -a \"openvpn/{$common_name}\" -f {$g['tmp_path']}/{$common_name}.rules");
183
	@unlink("{$g['tmp_path']}/{$common_name}.rules");
184
}
185

  
186
?>
etc/inc/openvpn.auth-user.php
96 96
/* Replaced by a sed with propper variables used below(ldap parameters). */
97 97
//<template>
98 98

  
99
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
100
	putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
101
	putenv("LDAPTLS_REQCERT=never");
102
}
103

  
104 99
$authenticated = false;
105 100

  
106 101
if (($strictusercn === true) && ($common_name != $username)) {
......
108 103
	exit(1);
109 104
}
110 105

  
111
$attributes = array();
112 106
foreach ($authmodes as $authmode) {
113 107
	$authcfg = auth_get_authserver($authmode);
114 108
	if (!$authcfg && $authmode != "local")
115 109
		continue;
116 110

  
117
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
111
	$authenticated = authenticate_user($username, $password, $authcfg);
118 112
	if ($authenticated == true)
119 113
		break;
120 114
}
......
124 118
	exit(-1);
125 119
}
126 120

  
127
if (file_exists("/etc/inc/openvpn.attributes.php"))
128
	include_once("/etc/inc/openvpn.attributes.php");
129

  
130
$content = "";
131
if (is_array($attributes['dns-servers'])) {
132
	foreach ($attributes['dns-servers'] as $dnssrv) {
133
		if (is_ipaddr($dnssrv))
134
			$content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
135
	}
136
}
137
if (is_array($attributes['routes'])) {
138
	foreach ($attributes['routes'] as $route)
139
			$content .= "push \"route {$route} vpn_gateway\"\n";
140
}
141

  
142
if (isset($attributes['framed_ip'])) {
143
/* XXX: only use when TAP windows driver >= 8.2.x */
144
/*      if (isset($attributes['framed_mask'])) {
145
                $content .= "topology subnet\n";
146
                $content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
147
        } else {
148
*/
149
                $content .= "topology net30\n";
150
                $content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) + 1));
151
//      }
152
}
153

  
154
if (!empty($content))
155
	@file_put_contents("{$g['tmp_path']}/{$username}", $content);
156

  
157 121
syslog(LOG_WARNING, "user {$username} authenticated\n");
158 122

  
159 123
exit(0);
etc/inc/openvpn.inc
364 364
	$conf .= "cipher {$cipher}\n";
365 365
	$conf .= "up /usr/local/sbin/ovpn-linkup\n";
366 366
	$conf .= "down /usr/local/sbin/ovpn-linkdown\n";
367
	if (file_exists("/usr/local/sbin/openvpn.attributes.sh")) {
368
		$conf .= "client-connect /usr/local/sbin/openvpn.attributes.sh\n";
369
		$conf .= "client-disconnect /usr/local/sbin/openvpn.attributes.sh\n";
370
	}
371 367

  
372 368
	if (!empty($iface_ip)) {
373 369
		$conf .= "local {$iface_ip}\n";	
usr/local/sbin/openvpn.attributes.sh
1
#!/bin/sh
2

  
3
echo $script_type > /tmp/script
4
if [ "$script_type" = "client-connect" ]; then
5
	if [ -f /tmp/$common_name ]; then
6
		/bin/cat /tmp/$common_name > $1
7
		/bin/rm /tmp/$common_name
8
	fi
9
elif [ "$script_type" = "client-disconnect" ]; then
10
	command="/sbin/pfctl -a 'openvpn/$common_name' -F rules"
11
        eval $command
12
	/sbin/pfctl -k $ifconfig_pool_remote_ip
13
	/sbin/pfctl -K $ifconfig_pool_remote_ip
14
fi
15

  
16
exit 0

Also available in: Unified diff