Project

General

Profile

Download (5.42 KB) Statistics
| Branch: | Tag: | Revision:
1 cb7d18d5 Renato Botelho
#!/usr/local/bin/php-cgi -f
2 3c11bd3c Matthew Grooms
<?php
3
/*
4 ce77a9c4 Phil Davis
	openvpn.auth-user.php
5 3c11bd3c Matthew Grooms
6 ce77a9c4 Phil Davis
	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 3c11bd3c Matthew Grooms
11 ce77a9c4 Phil Davis
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 3c11bd3c Matthew Grooms
14 ce77a9c4 Phil Davis
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 3c11bd3c Matthew Grooms
17 ce77a9c4 Phil Davis
	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 3c11bd3c Matthew Grooms
21 ce77a9c4 Phil Davis
	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 1f5309a3 Matthew Grooms
32 3c11bd3c Matthew Grooms
*/
33 523855b0 Scott Ullrich
/*
34 b37a2e8c Phil Davis
	pfSense_BUILDER_BINARIES:
35 523855b0 Scott Ullrich
	pfSense_MODULE:	openvpn
36
*/
37 3c11bd3c Matthew Grooms
/*
38
 * OpenVPN calls this script to authenticate a user
39
 * based on a username and password. We lookup these
40
 * in our config.xml file and check the credentials.
41
 */
42
43 befad728 Ermal
require_once("globals.inc");
44 3c11bd3c Matthew Grooms
require_once("config.inc");
45 cc686d98 Ermal Lu?i
require_once("radius.inc");
46 a13ce628 Ermal Lu?i
require_once("auth.inc");
47 cc686d98 Ermal Lu?i
require_once("interfaces.inc");
48 3c11bd3c Matthew Grooms
49 cc686d98 Ermal Lu?i
/**
50
 * Get the NAS-Identifier
51
 *
52
 * We will use our local hostname to make up the nas_id
53
 */
54
if (!function_exists("getNasID")) {
55 086cf944 Phil Davis
function getNasID() {
56 b37a2e8c Phil Davis
	global $g;
57 cc686d98 Ermal Lu?i
58 b37a2e8c Phil Davis
	$nasId = gethostname();
59
	if (empty($nasId)) {
60
		$nasId = $g['product_name'];
61
	}
62
	return $nasId;
63 cc686d98 Ermal Lu?i
}
64
}
65
66
/**
67
 * Get the NAS-IP-Address based on the current wan address
68
 *
69
 * Use functions in interfaces.inc to find this out
70
 *
71
 */
72
if (!function_exists("getNasIP")) {
73 086cf944 Phil Davis
function getNasIP() {
74 b37a2e8c Phil Davis
	$nasIp = get_interface_ip();
75
	if (!$nasIp) {
76
		$nasIp = "0.0.0.0";
77
	}
78
	return $nasIp;
79 cc686d98 Ermal Lu?i
}
80
}
81 3c11bd3c Matthew Grooms
/* setup syslog logging */
82
openlog("openvpn", LOG_ODELAY, LOG_AUTH);
83
84 8fa0a534 Ermal LUÇI
if (isset($_GET['username'])) {
85 5e28dad4 Ermal
	$authmodes = explode(",", $_GET['authcfg']);
86 fdf6fcb3 Ermal LUÇI
	$username = base64_decode(str_replace('%3D', '=', $_GET['username']));
87 e821f30e Ermal LUÇI
	$password = base64_decode(str_replace('%3D', '=', $_GET['password']));
88 5e28dad4 Ermal
	$common_name = $_GET['cn'];
89
	$modeid = $_GET['modeid'];
90
	$strictusercn = $_GET['strictcn'] == "false" ? false : true;
91
} else {
92
	/* read data from environment */
93
	$username = getenv("username");
94
	$password = getenv("password");
95
	$common_name = getenv("common_name");
96
}
97 3c11bd3c Matthew Grooms
98
if (!$username || !$password) {
99 1f5309a3 Matthew Grooms
	syslog(LOG_ERR, "invalid user authentication environment");
100 8fa0a534 Ermal LUÇI
	if (isset($_GET['username'])) {
101 5e28dad4 Ermal
		echo "FAILED";
102 b2af12ad Ermal
		closelog();
103 5e28dad4 Ermal
		return;
104 b2af12ad Ermal
	} else {
105
		closelog();
106 52550ca5 Ermal LUÇI
		return (-1);
107 b2af12ad Ermal
	}
108 3c11bd3c Matthew Grooms
}
109
110 b37a2e8c Phil Davis
/* Replaced by a sed with proper variables used below(ldap parameters). */
111 c61e4626 Ermal Lu?i
//<template>
112
113 1492e02c Ermal
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
114
	putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
115
	putenv("LDAPTLS_REQCERT=never");
116
}
117
118 c61e4626 Ermal Lu?i
$authenticated = false;
119 8901958c jim-p
120 5319cf40 Talle
if (($strictusercn === true) && (mb_strtolower($common_name) !== mb_strtolower($username))) {
121 8901958c jim-p
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
122 8fa0a534 Ermal LUÇI
	if (isset($_GET['username'])) {
123 5e28dad4 Ermal
		echo "FAILED";
124 b2af12ad Ermal
		closelog();
125 5e28dad4 Ermal
		return;
126 b2af12ad Ermal
	} else {
127
		closelog();
128 52550ca5 Ermal LUÇI
		return (1);
129 b2af12ad Ermal
	}
130 5e28dad4 Ermal
}
131
132
if (!is_array($authmodes)) {
133
	syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
134 8fa0a534 Ermal LUÇI
	if (isset($_GET['username'])) {
135 5e28dad4 Ermal
		echo "FAILED";
136 b2af12ad Ermal
		closelog();
137 5e28dad4 Ermal
		return;
138 b2af12ad Ermal
	} else {
139
		closelog();
140 52550ca5 Ermal LUÇI
		return (1);
141 b2af12ad Ermal
	}
142 8901958c jim-p
}
143
144 1492e02c Ermal
$attributes = array();
145 c61e4626 Ermal Lu?i
foreach ($authmodes as $authmode) {
146
	$authcfg = auth_get_authserver($authmode);
147 821a4351 Renato Botelho
	if (!$authcfg && $authmode != "Local Database") {
148 c61e4626 Ermal Lu?i
		continue;
149 b37a2e8c Phil Davis
	}
150 c61e4626 Ermal Lu?i
151 1492e02c Ermal
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
152 b37a2e8c Phil Davis
	if ($authenticated == true) {
153 006a162f Ermal Lu?i
		break;
154 b37a2e8c Phil Davis
	}
155 c61e4626 Ermal Lu?i
}
156
157
if ($authenticated == false) {
158 3260b82f Ermal
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
159 8fa0a534 Ermal LUÇI
	if (isset($_GET['username'])) {
160 5e28dad4 Ermal
		echo "FAILED";
161 b2af12ad Ermal
		closelog();
162 5e28dad4 Ermal
		return;
163 b2af12ad Ermal
	} else {
164
		closelog();
165 52550ca5 Ermal LUÇI
		return (-1);
166 b2af12ad Ermal
	}
167 3c11bd3c Matthew Grooms
}
168
169 b37a2e8c Phil Davis
if (file_exists("/etc/inc/openvpn.attributes.php")) {
170
	include_once("/etc/inc/openvpn.attributes.php");
171
}
172
173 1492e02c Ermal
$content = "";
174
if (is_array($attributes['dns-servers'])) {
175 b37a2e8c Phil Davis
	foreach ($attributes['dns-servers'] as $dnssrv) {
176
		if (is_ipaddr($dnssrv)) {
177
			$content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
178
		}
179
	}
180 1492e02c Ermal
}
181
if (is_array($attributes['routes'])) {
182 b37a2e8c Phil Davis
	foreach ($attributes['routes'] as $route) {
183 5e28dad4 Ermal
		$content .= "push \"route {$route} vpn_gateway\"\n";
184 b37a2e8c Phil Davis
	}
185 1492e02c Ermal
}
186
187
if (isset($attributes['framed_ip'])) {
188 ee8f9940 doktornotor
	if (isset($attributes['framed_mask'])) {
189 b37a2e8c Phil Davis
		$content .= "topology subnet\n";
190
		$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
191
	} else {
192 ee8f9940 doktornotor
		$content .= "topology net30\n";
193 356ec787 doktornotor
		$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) - 1));
194 ee8f9940 doktornotor
	}
195 b37a2e8c Phil Davis
}
196
197
if (!empty($content)) {
198
	@file_put_contents("{$g['tmp_path']}/{$username}", $content);
199 1492e02c Ermal
}
200
201 3260b82f Ermal
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
202 b2af12ad Ermal
closelog();
203 a13ce628 Ermal Lu?i
204 b37a2e8c Phil Davis
if (isset($_GET['username'])) {
205 5e28dad4 Ermal
	echo "OK";
206 b37a2e8c Phil Davis
} else {
207 52550ca5 Ermal LUÇI
	return (0);
208 b37a2e8c Phil Davis
}
209 3c11bd3c Matthew Grooms
210 fe2031ab Ermal
?>