Project

General

Profile

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

    
7
	Copyright (C) 2008 Shrew Soft Inc
8
	Copyright (C) 2010 Ermal Luçi
9
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
10
	All rights reserved.
11

    
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17

    
18
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21

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

    
33
*/
34
/*
35
	pfSense_BUILDER_BINARIES:
36
	pfSense_MODULE:	openvpn
37
*/
38
/*
39
 * OpenVPN calls this script to authenticate a user
40
 * based on a username and password. We lookup these
41
 * in our config.xml file and check the credentials.
42
 */
43

    
44
require_once("globals.inc");
45
require_once("config.inc");
46
require_once("radius.inc");
47
require_once("auth.inc");
48
require_once("interfaces.inc");
49

    
50
/**
51
 * Get the NAS-Identifier
52
 *
53
 * We will use our local hostname to make up the nas_id
54
 */
55
if (!function_exists("getNasID")) {
56
function getNasID()
57
{
58
	global $g;
59

    
60
	$nasId = gethostname();
61
	if (empty($nasId)) {
62
		$nasId = $g['product_name'];
63
	}
64
	return $nasId;
65
}
66
}
67

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

    
87
if (isset($_GET['username'])) {
88
	$authmodes = explode(",", $_GET['authcfg']);
89
	$username = base64_decode(str_replace('%3D', '=', $_GET['username']));
90
	$password = base64_decode(str_replace('%3D', '=', $_GET['password']));
91
	$common_name = $_GET['cn'];
92
	$modeid = $_GET['modeid'];
93
	$strictusercn = $_GET['strictcn'] == "false" ? false : true;
94
} else {
95
	/* read data from environment */
96
	$username = getenv("username");
97
	$password = getenv("password");
98
	$common_name = getenv("common_name");
99
}
100

    
101
if (!$username || !$password) {
102
	syslog(LOG_ERR, "invalid user authentication environment");
103
	if (isset($_GET['username'])) {
104
		echo "FAILED";
105
		closelog();
106
		return;
107
	} else {
108
		closelog();
109
		return (-1);
110
	}
111
}
112

    
113
/* Replaced by a sed with proper variables used below(ldap parameters). */
114
//<template>
115

    
116
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
117
	putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
118
	putenv("LDAPTLS_REQCERT=never");
119
}
120

    
121
$authenticated = false;
122

    
123
if (($strictusercn === true) && ($common_name != $username)) {
124
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
125
	if (isset($_GET['username'])) {
126
		echo "FAILED";
127
		closelog();
128
		return;
129
	} else {
130
		closelog();
131
		return (1);
132
	}
133
}
134

    
135
if (!is_array($authmodes)) {
136
	syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
137
	if (isset($_GET['username'])) {
138
		echo "FAILED";
139
		closelog();
140
		return;
141
	} else {
142
		closelog();
143
		return (1);
144
	}
145
}
146

    
147
$attributes = array();
148
foreach ($authmodes as $authmode) {
149
	$authcfg = auth_get_authserver($authmode);
150
	if (!$authcfg && $authmode != "local") {
151
		continue;
152
	}
153

    
154
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
155
	if ($authenticated == true) {
156
		break;
157
	}
158
}
159

    
160
if ($authenticated == false) {
161
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
162
	if (isset($_GET['username'])) {
163
		echo "FAILED";
164
		closelog();
165
		return;
166
	} else {
167
		closelog();
168
		return (-1);
169
	}
170
}
171

    
172
if (file_exists("/etc/inc/openvpn.attributes.php")) {
173
	include_once("/etc/inc/openvpn.attributes.php");
174
}
175

    
176
$content = "";
177
if (is_array($attributes['dns-servers'])) {
178
	foreach ($attributes['dns-servers'] as $dnssrv) {
179
		if (is_ipaddr($dnssrv)) {
180
			$content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
181
		}
182
	}
183
}
184
if (is_array($attributes['routes'])) {
185
	foreach ($attributes['routes'] as $route) {
186
		$content .= "push \"route {$route} vpn_gateway\"\n";
187
	}
188
}
189

    
190
if (isset($attributes['framed_ip'])) {
191
/* XXX: only use when TAP windows driver >= 8.2.x */
192
/*	if (isset($attributes['framed_mask'])) {
193
		$content .= "topology subnet\n";
194
		$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
195
	} else {
196
*/
197
	$content .= "topology net30\n";
198
	$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) + 1));
199
//	}
200
}
201

    
202
if (!empty($content)) {
203
	@file_put_contents("{$g['tmp_path']}/{$username}", $content);
204
}
205

    
206
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
207
closelog();
208

    
209
if (isset($_GET['username'])) {
210
	echo "OK";
211
} else {
212
	return (0);
213
}
214

    
215
?>
(37-37/68)