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-2018 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
|
global $common_name, $username;
|
99
|
|
100
|
if (isset($_GET['username'])) {
|
101
|
$authmodes = explode(",", base64_decode($_GET['authcfg']));
|
102
|
/* Any string retrieved through $_GET is automatically urlDecoded */
|
103
|
$username = base64_decode($_GET['username']);
|
104
|
$password = base64_decode($_GET['password']);
|
105
|
$common_name = $_GET['cn'];
|
106
|
$modeid = $_GET['modeid'];
|
107
|
$strictusercn = $_GET['strictcn'] == "false" ? false : true;
|
108
|
} else {
|
109
|
/* read data from environment */
|
110
|
$username = getenv("username");
|
111
|
$password = getenv("password");
|
112
|
$common_name = getenv("common_name");
|
113
|
}
|
114
|
|
115
|
if (!$username || !$password) {
|
116
|
syslog(LOG_ERR, "invalid user authentication environment");
|
117
|
if (isset($_GET['username'])) {
|
118
|
echo "FAILED";
|
119
|
closelog();
|
120
|
return;
|
121
|
} else {
|
122
|
closelog();
|
123
|
return (-1);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
/* Replaced by a sed with proper variables used below(ldap parameters). */
|
128
|
//<template>
|
129
|
|
130
|
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
|
131
|
putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
|
132
|
putenv("LDAPTLS_REQCERT=never");
|
133
|
}
|
134
|
|
135
|
$authenticated = false;
|
136
|
|
137
|
if (($strictusercn === true) && (mb_strtolower($common_name) !== mb_strtolower($username))) {
|
138
|
syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
|
139
|
if (isset($_GET['username'])) {
|
140
|
echo "FAILED";
|
141
|
closelog();
|
142
|
return;
|
143
|
} else {
|
144
|
closelog();
|
145
|
return (1);
|
146
|
}
|
147
|
}
|
148
|
|
149
|
if (!is_array($authmodes)) {
|
150
|
syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
|
151
|
if (isset($_GET['username'])) {
|
152
|
echo "FAILED";
|
153
|
closelog();
|
154
|
return;
|
155
|
} else {
|
156
|
closelog();
|
157
|
return (1);
|
158
|
}
|
159
|
}
|
160
|
|
161
|
$attributes = array();
|
162
|
foreach ($authmodes as $authmode) {
|
163
|
$authcfg = auth_get_authserver($authmode);
|
164
|
if (!$authcfg && $authmode != "Local Database") {
|
165
|
continue;
|
166
|
}
|
167
|
|
168
|
$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
|
169
|
if ($authenticated == true) {
|
170
|
break;
|
171
|
}
|
172
|
}
|
173
|
|
174
|
if ($authenticated == false) {
|
175
|
syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
|
176
|
if (isset($_GET['username'])) {
|
177
|
echo "FAILED";
|
178
|
closelog();
|
179
|
return;
|
180
|
} else {
|
181
|
closelog();
|
182
|
return (-1);
|
183
|
}
|
184
|
}
|
185
|
|
186
|
if (file_exists("/etc/inc/openvpn.attributes.php")) {
|
187
|
include_once("/etc/inc/openvpn.attributes.php");
|
188
|
}
|
189
|
|
190
|
$content = "";
|
191
|
if (is_array($attributes['dns-servers'])) {
|
192
|
foreach ($attributes['dns-servers'] as $dnssrv) {
|
193
|
if (is_ipaddr($dnssrv)) {
|
194
|
$content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
|
195
|
}
|
196
|
}
|
197
|
}
|
198
|
if (is_array($attributes['routes'])) {
|
199
|
foreach ($attributes['routes'] as $route) {
|
200
|
$content .= "push \"route {$route} vpn_gateway\"\n";
|
201
|
}
|
202
|
}
|
203
|
|
204
|
if (isset($attributes['framed_ip'])) {
|
205
|
if (isset($attributes['framed_mask'])) {
|
206
|
$content .= "topology subnet\n";
|
207
|
$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
|
208
|
} else {
|
209
|
$content .= "topology net30\n";
|
210
|
$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) - 1));
|
211
|
}
|
212
|
}
|
213
|
|
214
|
if (!empty($content)) {
|
215
|
@file_put_contents("{$g['tmp_path']}/{$username}", $content);
|
216
|
}
|
217
|
|
218
|
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
|
219
|
closelog();
|
220
|
|
221
|
if (isset($_GET['username'])) {
|
222
|
echo "OK";
|
223
|
} else {
|
224
|
return (0);
|
225
|
}
|
226
|
|
227
|
?>
|