Project

General

Profile

Download (5.45 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -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
	global $g;
58

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

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

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

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

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

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

    
119
$authenticated = false;
120

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

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

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

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

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

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

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

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

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

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

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

    
213
?>
(37-37/68)