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 Electric Sheep Fencing, LLC
|
9
|
* All rights reserved.
|
10
|
*
|
11
|
* Redistribution and use in source and binary forms, with or without
|
12
|
* modification, are permitted provided that the following conditions are met:
|
13
|
*
|
14
|
* 1. Redistributions of source code must retain the above copyright notice,
|
15
|
* this list of conditions and the following disclaimer.
|
16
|
*
|
17
|
* 2. Redistributions in binary form must reproduce the above copyright
|
18
|
* notice, this list of conditions and the following disclaimer in
|
19
|
* the documentation and/or other materials provided with the
|
20
|
* distribution.
|
21
|
*
|
22
|
* 3. All advertising materials mentioning features or use of this software
|
23
|
* must display the following acknowledgment:
|
24
|
* "This product includes software developed by the pfSense Project
|
25
|
* for use in the pfSense® software distribution. (http://www.pfsense.org/).
|
26
|
*
|
27
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
28
|
* endorse or promote products derived from this software without
|
29
|
* prior written permission. For written permission, please contact
|
30
|
* coreteam@pfsense.org.
|
31
|
*
|
32
|
* 5. Products derived from this software may not be called "pfSense"
|
33
|
* nor may "pfSense" appear in their names without prior written
|
34
|
* permission of the Electric Sheep Fencing, LLC.
|
35
|
*
|
36
|
* 6. Redistributions of any form whatsoever must retain the following
|
37
|
* acknowledgment:
|
38
|
*
|
39
|
* "This product includes software developed by the pfSense Project
|
40
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
41
|
*
|
42
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
43
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
44
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
45
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
46
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
47
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
48
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
49
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
50
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
51
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
52
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
53
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
54
|
*/
|
55
|
|
56
|
/*
|
57
|
* OpenVPN calls this script to authenticate a user
|
58
|
* based on a username and password. We lookup these
|
59
|
* in our config.xml file and check the credentials.
|
60
|
*/
|
61
|
|
62
|
require_once("globals.inc");
|
63
|
require_once("config.inc");
|
64
|
require_once("radius.inc");
|
65
|
require_once("auth.inc");
|
66
|
require_once("interfaces.inc");
|
67
|
|
68
|
/**
|
69
|
* Get the NAS-Identifier
|
70
|
*
|
71
|
* We will use our local hostname to make up the nas_id
|
72
|
*/
|
73
|
if (!function_exists("getNasID")) {
|
74
|
function getNasID() {
|
75
|
global $g;
|
76
|
|
77
|
$nasId = gethostname();
|
78
|
if (empty($nasId)) {
|
79
|
$nasId = $g['product_name'];
|
80
|
}
|
81
|
return $nasId;
|
82
|
}
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Get the NAS-IP-Address based on the current wan address
|
87
|
*
|
88
|
* Use functions in interfaces.inc to find this out
|
89
|
*
|
90
|
*/
|
91
|
if (!function_exists("getNasIP")) {
|
92
|
function getNasIP() {
|
93
|
$nasIp = get_interface_ip();
|
94
|
if (!$nasIp) {
|
95
|
$nasIp = "0.0.0.0";
|
96
|
}
|
97
|
return $nasIp;
|
98
|
}
|
99
|
}
|
100
|
/* setup syslog logging */
|
101
|
openlog("openvpn", LOG_ODELAY, LOG_AUTH);
|
102
|
|
103
|
if (isset($_GET['username'])) {
|
104
|
$authmodes = explode(",", $_GET['authcfg']);
|
105
|
/* Any string retrieved through $_GET is automatically urlDecoded */
|
106
|
$username = base64_decode($_GET['username']);
|
107
|
$password = base64_decode($_GET['password']);
|
108
|
$common_name = $_GET['cn'];
|
109
|
$modeid = $_GET['modeid'];
|
110
|
$strictusercn = $_GET['strictcn'] == "false" ? false : true;
|
111
|
} else {
|
112
|
/* read data from environment */
|
113
|
$username = getenv("username");
|
114
|
$password = getenv("password");
|
115
|
$common_name = getenv("common_name");
|
116
|
}
|
117
|
|
118
|
if (!$username || !$password) {
|
119
|
syslog(LOG_ERR, "invalid user authentication environment");
|
120
|
if (isset($_GET['username'])) {
|
121
|
echo "FAILED";
|
122
|
closelog();
|
123
|
return;
|
124
|
} else {
|
125
|
closelog();
|
126
|
return (-1);
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/* Replaced by a sed with proper variables used below(ldap parameters). */
|
131
|
//<template>
|
132
|
|
133
|
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
|
134
|
putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
|
135
|
putenv("LDAPTLS_REQCERT=never");
|
136
|
}
|
137
|
|
138
|
$authenticated = false;
|
139
|
|
140
|
if (($strictusercn === true) && (mb_strtolower($common_name) !== mb_strtolower($username))) {
|
141
|
syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
|
142
|
if (isset($_GET['username'])) {
|
143
|
echo "FAILED";
|
144
|
closelog();
|
145
|
return;
|
146
|
} else {
|
147
|
closelog();
|
148
|
return (1);
|
149
|
}
|
150
|
}
|
151
|
|
152
|
if (!is_array($authmodes)) {
|
153
|
syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
|
154
|
if (isset($_GET['username'])) {
|
155
|
echo "FAILED";
|
156
|
closelog();
|
157
|
return;
|
158
|
} else {
|
159
|
closelog();
|
160
|
return (1);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
$attributes = array();
|
165
|
foreach ($authmodes as $authmode) {
|
166
|
$authcfg = auth_get_authserver($authmode);
|
167
|
if (!$authcfg && $authmode != "Local Database") {
|
168
|
continue;
|
169
|
}
|
170
|
|
171
|
$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
|
172
|
if ($authenticated == true) {
|
173
|
break;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
if ($authenticated == false) {
|
178
|
syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
|
179
|
if (isset($_GET['username'])) {
|
180
|
echo "FAILED";
|
181
|
closelog();
|
182
|
return;
|
183
|
} else {
|
184
|
closelog();
|
185
|
return (-1);
|
186
|
}
|
187
|
}
|
188
|
|
189
|
if (file_exists("/etc/inc/openvpn.attributes.php")) {
|
190
|
include_once("/etc/inc/openvpn.attributes.php");
|
191
|
}
|
192
|
|
193
|
$content = "";
|
194
|
if (is_array($attributes['dns-servers'])) {
|
195
|
foreach ($attributes['dns-servers'] as $dnssrv) {
|
196
|
if (is_ipaddr($dnssrv)) {
|
197
|
$content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
if (is_array($attributes['routes'])) {
|
202
|
foreach ($attributes['routes'] as $route) {
|
203
|
$content .= "push \"route {$route} vpn_gateway\"\n";
|
204
|
}
|
205
|
}
|
206
|
|
207
|
if (isset($attributes['framed_ip'])) {
|
208
|
if (isset($attributes['framed_mask'])) {
|
209
|
$content .= "topology subnet\n";
|
210
|
$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
|
211
|
} else {
|
212
|
$content .= "topology net30\n";
|
213
|
$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) - 1));
|
214
|
}
|
215
|
}
|
216
|
|
217
|
if (!empty($content)) {
|
218
|
@file_put_contents("{$g['tmp_path']}/{$username}", $content);
|
219
|
}
|
220
|
|
221
|
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
|
222
|
closelog();
|
223
|
|
224
|
if (isset($_GET['username'])) {
|
225
|
echo "OK";
|
226
|
} else {
|
227
|
return (0);
|
228
|
}
|
229
|
|
230
|
?>
|