Project

General

Profile

Download (4.43 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-2018 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

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

    
29
require_once("globals.inc");
30
require_once("config.inc");
31
require_once("auth.inc");
32
require_once("interfaces.inc");
33

    
34
/* setup syslog logging */
35
openlog("openvpn", LOG_ODELAY, LOG_AUTH);
36

    
37
global $common_name, $username;
38

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

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

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

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

    
74
$authenticated = false;
75

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

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

    
100

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

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

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

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

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

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

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

    
162
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
163
closelog();
164

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

    
171
?>
(33-33/60)