Project

General

Profile

Download (5.37 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
	openvpn.auth-user.php
5

    
6
	Copyright (C) 2008 Shrew Soft Inc
7
	Copyright (C) 2010 Ermal Luçi
8
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
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 the
19
	   documentation and/or other materials provided with the distribution.
20

    
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31

    
32
*/
33
/*
34
 * OpenVPN calls this script to authenticate a user
35
 * based on a username and password. We lookup these
36
 * in our config.xml file and check the credentials.
37
 */
38

    
39
require_once("globals.inc");
40
require_once("config.inc");
41
require_once("radius.inc");
42
require_once("auth.inc");
43
require_once("interfaces.inc");
44

    
45
/**
46
 * Get the NAS-Identifier
47
 *
48
 * We will use our local hostname to make up the nas_id
49
 */
50
if (!function_exists("getNasID")) {
51
function getNasID() {
52
	global $g;
53

    
54
	$nasId = gethostname();
55
	if (empty($nasId)) {
56
		$nasId = $g['product_name'];
57
	}
58
	return $nasId;
59
}
60
}
61

    
62
/**
63
 * Get the NAS-IP-Address based on the current wan address
64
 *
65
 * Use functions in interfaces.inc to find this out
66
 *
67
 */
68
if (!function_exists("getNasIP")) {
69
function getNasIP() {
70
	$nasIp = get_interface_ip();
71
	if (!$nasIp) {
72
		$nasIp = "0.0.0.0";
73
	}
74
	return $nasIp;
75
}
76
}
77
/* setup syslog logging */
78
openlog("openvpn", LOG_ODELAY, LOG_AUTH);
79

    
80
if (isset($_GET['username'])) {
81
	$authmodes = explode(",", $_GET['authcfg']);
82
	$username = base64_decode(str_replace('%3D', '=', $_GET['username']));
83
	$password = base64_decode(str_replace('%3D', '=', $_GET['password']));
84
	$common_name = $_GET['cn'];
85
	$modeid = $_GET['modeid'];
86
	$strictusercn = $_GET['strictcn'] == "false" ? false : true;
87
} else {
88
	/* read data from environment */
89
	$username = getenv("username");
90
	$password = getenv("password");
91
	$common_name = getenv("common_name");
92
}
93

    
94
if (!$username || !$password) {
95
	syslog(LOG_ERR, "invalid user authentication environment");
96
	if (isset($_GET['username'])) {
97
		echo "FAILED";
98
		closelog();
99
		return;
100
	} else {
101
		closelog();
102
		return (-1);
103
	}
104
}
105

    
106
/* Replaced by a sed with proper variables used below(ldap parameters). */
107
//<template>
108

    
109
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
110
	putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
111
	putenv("LDAPTLS_REQCERT=never");
112
}
113

    
114
$authenticated = false;
115

    
116
if (($strictusercn === true) && (mb_strtolower($common_name) !== mb_strtolower($username))) {
117
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
118
	if (isset($_GET['username'])) {
119
		echo "FAILED";
120
		closelog();
121
		return;
122
	} else {
123
		closelog();
124
		return (1);
125
	}
126
}
127

    
128
if (!is_array($authmodes)) {
129
	syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
130
	if (isset($_GET['username'])) {
131
		echo "FAILED";
132
		closelog();
133
		return;
134
	} else {
135
		closelog();
136
		return (1);
137
	}
138
}
139

    
140
$attributes = array();
141
foreach ($authmodes as $authmode) {
142
	$authcfg = auth_get_authserver($authmode);
143
	if (!$authcfg && $authmode != "Local Database") {
144
		continue;
145
	}
146

    
147
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
148
	if ($authenticated == true) {
149
		break;
150
	}
151
}
152

    
153
if ($authenticated == false) {
154
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
155
	if (isset($_GET['username'])) {
156
		echo "FAILED";
157
		closelog();
158
		return;
159
	} else {
160
		closelog();
161
		return (-1);
162
	}
163
}
164

    
165
if (file_exists("/etc/inc/openvpn.attributes.php")) {
166
	include_once("/etc/inc/openvpn.attributes.php");
167
}
168

    
169
$content = "";
170
if (is_array($attributes['dns-servers'])) {
171
	foreach ($attributes['dns-servers'] as $dnssrv) {
172
		if (is_ipaddr($dnssrv)) {
173
			$content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
174
		}
175
	}
176
}
177
if (is_array($attributes['routes'])) {
178
	foreach ($attributes['routes'] as $route) {
179
		$content .= "push \"route {$route} vpn_gateway\"\n";
180
	}
181
}
182

    
183
if (isset($attributes['framed_ip'])) {
184
	if (isset($attributes['framed_mask'])) {
185
		$content .= "topology subnet\n";
186
		$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
187
	} else {
188
		$content .= "topology net30\n";
189
		$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) - 1));
190
	}
191
}
192

    
193
if (!empty($content)) {
194
	@file_put_contents("{$g['tmp_path']}/{$username}", $content);
195
}
196

    
197
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
198
closelog();
199

    
200
if (isset($_GET['username'])) {
201
	echo "OK";
202
} else {
203
	return (0);
204
}
205

    
206
?>
(36-36/65)