Project

General

Profile

Download (4.52 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * openvpn.auth-user.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2008 Shrew Soft Inc
7
 * Copyright (c) 2008-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
/*
26
 * OpenVPN calls this script to authenticate a user
27
 * based on a username and password. We lookup these
28
 * in our config.xml file and check the credentials.
29
 */
30

    
31
require_once("globals.inc");
32
require_once("config.inc");
33
require_once("auth.inc");
34
require_once("interfaces.inc");
35

    
36
/* setup syslog logging */
37
openlog("openvpn", LOG_ODELAY, LOG_AUTH);
38

    
39
global $common_name, $username;
40

    
41
if (isset($_GET['username'])) {
42
	$authmodes = explode(",", base64_decode($_GET['authcfg']));
43
	/* Any string retrieved through $_GET is automatically urlDecoded */
44
	$username = base64_decode($_GET['username']);
45
	$password = base64_decode($_GET['password']);
46
	$common_name = $_GET['cn'];
47
	$modeid = $_GET['modeid'];
48
	$strictusercn = $_GET['strictcn'] == "false" ? false : true;
49
} else {
50
	/* read data from environment */
51
	$username = getenv("username");
52
	$password = getenv("password");
53
	$common_name = getenv("common_name");
54
}
55

    
56
if (!$username || !$password) {
57
	syslog(LOG_ERR, "invalid user authentication environment");
58
	if (isset($_GET['username'])) {
59
		echo "FAILED";
60
		closelog();
61
		return;
62
	} else {
63
		closelog();
64
		return (-1);
65
	}
66
}
67

    
68
/* Replaced by a sed with proper variables used below(ldap parameters). */
69
//<template>
70

    
71
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
72
	putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
73
	putenv("LDAPTLS_REQCERT=never");
74
}
75

    
76
$authenticated = false;
77

    
78
if (($strictusercn === true) && (mb_strtolower($common_name) !== mb_strtolower($username))) {
79
	syslog(LOG_WARNING, "Username does not match certificate common name (\"{$username}\" != \"{$common_name}\"), access denied.");
80
	if (isset($_GET['username'])) {
81
		echo "FAILED";
82
		closelog();
83
		return;
84
	} else {
85
		closelog();
86
		return (1);
87
	}
88
}
89

    
90
if (!is_array($authmodes)) {
91
	syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
92
	if (isset($_GET['username'])) {
93
		echo "FAILED";
94
		closelog();
95
		return;
96
	} else {
97
		closelog();
98
		return (1);
99
	}
100
}
101

    
102

    
103
$attributes = array("nas_identifier" => "openVPN",
104
	"nas_port_type" => RADIUS_VIRTUAL,
105
	"nas_port" => $_GET['nas_port'],
106
	"calling_station_id" => get_interface_ip() . ":" . $_GET['nas_port']);
107
	
108
foreach ($authmodes as $authmode) {
109
	$authcfg = auth_get_authserver($authmode);
110
	if (!$authcfg && $authmode != "Local Database") {
111
		continue;
112
	}
113

    
114
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
115
	if ($authenticated == true) {
116
		break;
117
	}
118
}
119

    
120
if ($authenticated == false) {
121
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.");
122
	if (isset($_GET['username'])) {
123
		echo "FAILED";
124
		closelog();
125
		return;
126
	} else {
127
		closelog();
128
		return (-1);
129
	}
130
}
131

    
132
if (file_exists("/etc/inc/openvpn.attributes.php")) {
133
	include_once("/etc/inc/openvpn.attributes.php");
134
}
135

    
136
$content = "";
137
if (is_array($attributes['dns-servers'])) {
138
	foreach ($attributes['dns-servers'] as $dnssrv) {
139
		if (is_ipaddr($dnssrv)) {
140
			$content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
141
		}
142
	}
143
}
144
if (is_array($attributes['routes'])) {
145
	foreach ($attributes['routes'] as $route) {
146
		$content .= "push \"route {$route} vpn_gateway\"\n";
147
	}
148
}
149

    
150
if (isset($attributes['framed_ip'])) {
151
	if (isset($attributes['framed_mask'])) {
152
		$content .= "topology subnet\n";
153
		$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
154
	} else {
155
		$content .= "topology net30\n";
156
		$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) - 1));
157
	}
158
}
159

    
160
if (!empty($content)) {
161
	@file_put_contents("{$g['tmp_path']}/{$username}", $content);
162
}
163

    
164
syslog(LOG_NOTICE, "user '{$username}' authenticated");
165
closelog();
166

    
167
if (isset($_GET['username'])) {
168
	echo "OK";
169
} else {
170
	return (0);
171
}
172

    
173
?>
(32-32/59)