Project

General

Profile

Download (4.35 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-2020 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
$authenticated = false;
72

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

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

    
97

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

    
109
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
110
	if ($authenticated == true) {
111
		break;
112
	}
113
}
114

    
115
if ($authenticated == false) {
116
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.");
117
	if (isset($_GET['username'])) {
118
		echo "FAILED";
119
		closelog();
120
		return;
121
	} else {
122
		closelog();
123
		return (-1);
124
	}
125
}
126

    
127
if (file_exists("/etc/inc/openvpn.attributes.php")) {
128
	include_once("/etc/inc/openvpn.attributes.php");
129
}
130

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

    
145
if (isset($attributes['framed_ip'])) {
146
	if (isset($attributes['framed_mask'])) {
147
		$content .= "topology subnet\n";
148
		$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
149
	} else {
150
		$content .= "topology net30\n";
151
		$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) - 1));
152
	}
153
}
154

    
155
if (!empty($content)) {
156
	@file_put_contents("{$g['tmp_path']}/{$username}", $content);
157
}
158

    
159
syslog(LOG_NOTICE, "user '{$username}' authenticated");
160
closelog();
161

    
162
if (isset($_GET['username'])) {
163
	echo "OK";
164
} else {
165
	return (0);
166
}
167

    
168
?>
(32-32/60)