Project

General

Profile

Download (5.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * openvpn.auth-user.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2008 Shrew Soft Inc
7
 * Copyright (c) 2008-2016 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
/*
24
 * OpenVPN calls this script to authenticate a user
25
 * based on a username and password. We lookup these
26
 * in our config.xml file and check the credentials.
27
 */
28

    
29
require_once("globals.inc");
30
require_once("config.inc");
31
require_once("radius.inc");
32
require_once("auth.inc");
33
require_once("interfaces.inc");
34

    
35
/**
36
 * Get the NAS-Identifier
37
 *
38
 * We will return "openVPN" so that connections can be distinguished by the Radius
39
 */
40
if (!function_exists("getNasID")) {
41
function getNasID() {
42
	return "openVPN";
43
}
44
}
45

    
46
/**
47
 * Get the NAS-IP-Address based on the current wan address
48
 *
49
 * Use functions in interfaces.inc to find this out
50
 *
51
 */
52
if (!function_exists("getNasIP")) {
53
function getNasIP() {
54
	$nasIp = get_interface_ip();
55
	if (!$nasIp) {
56
		$nasIp = "0.0.0.0";
57
	}
58
	return $nasIp;
59
}
60
}
61

    
62
/**
63
 * Set the NAS-Port-Type
64
 *
65
 * Should be "Virtual" since that denotes VPN connections
66
 */
67
if (!function_exists("getNasPortType")) {
68
function getNasPortType() {
69
	return RADIUS_VIRTUAL;
70
}
71
}
72

    
73
/**
74
 * Set the NAS-Port
75
 *
76
 * We will return the port the client connected to
77
 */
78
if (!function_exists("getNasPort")) {
79
function getNasPort() {
80
	return $_GET['nas_port'];
81
}
82
}
83

    
84
/**
85
 * Set the Called-Station-ID
86
 *
87
 * We will return the IP and port the client connected to
88
 */
89
if (!function_exists("getCalledStationId")) {
90
function getCalledStationId() {
91
	return get_interface_ip() . ":" . getNasPort();
92
}
93
}
94

    
95
/* setup syslog logging */
96
openlog("openvpn", LOG_ODELAY, LOG_AUTH);
97

    
98
if (isset($_GET['username'])) {
99
	$authmodes = explode(",", base64_decode($_GET['authcfg']));
100
	/* Any string retrieved through $_GET is automatically urlDecoded */
101
	$username = base64_decode($_GET['username']);
102
	$password = base64_decode($_GET['password']);
103
	$common_name = $_GET['cn'];
104
	$modeid = $_GET['modeid'];
105
	$strictusercn = $_GET['strictcn'] == "false" ? false : true;
106
} else {
107
	/* read data from environment */
108
	$username = getenv("username");
109
	$password = getenv("password");
110
	$common_name = getenv("common_name");
111
}
112

    
113
if (!$username || !$password) {
114
	syslog(LOG_ERR, "invalid user authentication environment");
115
	if (isset($_GET['username'])) {
116
		echo "FAILED";
117
		closelog();
118
		return;
119
	} else {
120
		closelog();
121
		return (-1);
122
	}
123
}
124

    
125
/* Replaced by a sed with proper variables used below(ldap parameters). */
126
//<template>
127

    
128
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
129
	putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
130
	putenv("LDAPTLS_REQCERT=never");
131
}
132

    
133
$authenticated = false;
134

    
135
if (($strictusercn === true) && (mb_strtolower($common_name) !== mb_strtolower($username))) {
136
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
137
	if (isset($_GET['username'])) {
138
		echo "FAILED";
139
		closelog();
140
		return;
141
	} else {
142
		closelog();
143
		return (1);
144
	}
145
}
146

    
147
if (!is_array($authmodes)) {
148
	syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
149
	if (isset($_GET['username'])) {
150
		echo "FAILED";
151
		closelog();
152
		return;
153
	} else {
154
		closelog();
155
		return (1);
156
	}
157
}
158

    
159
$attributes = array();
160
foreach ($authmodes as $authmode) {
161
	$authcfg = auth_get_authserver($authmode);
162
	if (!$authcfg && $authmode != "Local Database") {
163
		continue;
164
	}
165

    
166
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
167
	if ($authenticated == true) {
168
		break;
169
	}
170
}
171

    
172
if ($authenticated == false) {
173
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
174
	if (isset($_GET['username'])) {
175
		echo "FAILED";
176
		closelog();
177
		return;
178
	} else {
179
		closelog();
180
		return (-1);
181
	}
182
}
183

    
184
if (file_exists("/etc/inc/openvpn.attributes.php")) {
185
	include_once("/etc/inc/openvpn.attributes.php");
186
}
187

    
188
$content = "";
189
if (is_array($attributes['dns-servers'])) {
190
	foreach ($attributes['dns-servers'] as $dnssrv) {
191
		if (is_ipaddr($dnssrv)) {
192
			$content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
193
		}
194
	}
195
}
196
if (is_array($attributes['routes'])) {
197
	foreach ($attributes['routes'] as $route) {
198
		$content .= "push \"route {$route} vpn_gateway\"\n";
199
	}
200
}
201

    
202
if (isset($attributes['framed_ip'])) {
203
	if (isset($attributes['framed_mask'])) {
204
		$content .= "topology subnet\n";
205
		$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
206
	} else {
207
		$content .= "topology net30\n";
208
		$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) - 1));
209
	}
210
}
211

    
212
if (!empty($content)) {
213
	@file_put_contents("{$g['tmp_path']}/{$username}", $content);
214
}
215

    
216
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
217
closelog();
218

    
219
if (isset($_GET['username'])) {
220
	echo "OK";
221
} else {
222
	return (0);
223
}
224

    
225
?>
(27-27/51)