Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

    
134
$authenticated = false;
135

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

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

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

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

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

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

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

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

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

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

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

    
226
?>
(27-27/51)