Project

General

Profile

Download (4.42 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-2022 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, $dev, $untrusted_port;
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
	$dev = $_GET['dev'];
50
	$untrusted_port = $_GET['untrusted_port'];
51
} else {
52
	/* read data from environment */
53
	$username = getenv("username");
54
	$password = getenv("password");
55
	$common_name = getenv("common_name");
56
	$dev = getenv("dev");
57
	$untrusted_port = getenv("untrusted_port");
58
}
59

    
60
if (!$username) {
61
	syslog(LOG_ERR, "invalid user authentication environment");
62
	if (isset($_GET['username'])) {
63
		echo "FAILED";
64
		closelog();
65
		return;
66
	} else {
67
		closelog();
68
		return (-1);
69
	}
70
}
71

    
72
/* Replaced by a sed with proper variables used below(ldap parameters). */
73
//<template>
74

    
75
$authenticated = false;
76

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

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

    
101

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

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

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

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

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

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

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

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

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

    
170
?>
(33-33/62)