Project

General

Profile

Download (4.26 KB) Statistics
| Branch: | Tag: | Revision:
1 52c9f9fa Ermal
#!/usr/local/bin/php -f
2
<?php
3
/*
4 ce77a9c4 Phil Davis
	ipsec.auth-user.php
5 52c9f9fa Ermal
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 52c9f9fa Ermal
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 52c9f9fa Ermal
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 52c9f9fa Ermal
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 52c9f9fa Ermal
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 52c9f9fa Ermal
32
*/
33
/*
34 b37a2e8c Phil Davis
	pfSense_BUILDER_BINARIES:
35 52c9f9fa Ermal
	pfSense_MODULE:	openvpn
36
*/
37
/*
38 4881e5a9 Ermal
 * ipsec calls this script to authenticate a user
39 52c9f9fa Ermal
 * based on a username and password. We lookup these
40
 * in our config.xml file and check the credentials.
41
 */
42
43
require_once("globals.inc");
44
require_once("config.inc");
45
require_once("radius.inc");
46
require_once("auth.inc");
47
require_once("interfaces.inc");
48
49
/**
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
function getNasID()
56
{
57 b37a2e8c Phil Davis
	global $g;
58 52c9f9fa Ermal
59 b37a2e8c Phil Davis
	$nasId = gethostname();
60
	if (empty($nasId)) {
61
		$nasId = $g['product_name'];
62
	}
63
	return $nasId;
64 52c9f9fa Ermal
}
65
}
66
67
/**
68
 * Get the NAS-IP-Address based on the current wan address
69
 *
70
 * Use functions in interfaces.inc to find this out
71
 *
72
 */
73
if (!function_exists("getNasIP")) {
74
function getNasIP()
75
{
76 b37a2e8c Phil Davis
	$nasIp = get_interface_ip();
77
	if (!$nasIp) {
78
		$nasIp = "0.0.0.0";
79
	}
80
	return $nasIp;
81 52c9f9fa Ermal
}
82
}
83
/* setup syslog logging */
84 9eb4257f Ermal
openlog("charon", LOG_ODELAY, LOG_AUTH);
85 52c9f9fa Ermal
86 9e74f980 Ermal
if (isset($_GET['username'])) {
87 85d0e959 Ermal
	$authmodes = explode(",", $_GET['authcfg']);
88
	$username = $_GET['username'];
89
	$password = $_GET['password'];
90
	$common_name = $_GET['cn'];
91
} else {
92
	/* read data from environment */
93
	$username = getenv("username");
94
	$password = getenv("password");
95
	$common_name = getenv("common_name");
96 2a3e3057 Ermal
	$authmodes = explode(",", getenv("authcfg"));
97 85d0e959 Ermal
}
98 52c9f9fa Ermal
99
if (!$username || !$password) {
100
	syslog(LOG_ERR, "invalid user authentication environment");
101 fe06990e Ermal
	if (isset($_GET['username'])) {
102 85d0e959 Ermal
		echo "FAILED";
103
		closelog();
104
		return;
105
	} else {
106
		closelog();
107 9a92e2ef Renato Botelho
		exit (-1);
108 85d0e959 Ermal
	}
109 52c9f9fa Ermal
}
110
111
$authenticated = false;
112
113
if (($strictusercn === true) && ($common_name != $username)) {
114
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
115 eadda967 Ermal
	if (isset($_GET['username'])) {
116 85d0e959 Ermal
		echo "FAILED";
117
		closelog();
118
		return;
119
	} else {
120
		closelog();
121 9a92e2ef Renato Botelho
		exit (1);
122 85d0e959 Ermal
	}
123 52c9f9fa Ermal
}
124
125
$attributes = array();
126
foreach ($authmodes as $authmode) {
127
	$authcfg = auth_get_authserver($authmode);
128 b37a2e8c Phil Davis
	if (!$authcfg && $authmode != "local") {
129 52c9f9fa Ermal
		continue;
130 b37a2e8c Phil Davis
	}
131 52c9f9fa Ermal
132
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
133 936fc874 Ermal
	if ($authenticated == true) {
134 a9157b6b Ermal
		if (stristr($authmode, "local")) {
135 b37a2e8c Phil Davis
			$user = getUserEntry($username);
136 a9157b6b Ermal
			if (!is_array($user) || !userHasPrivilege($user, "user-ipsec-xauth-dialin")) {
137
				$authenticated = false;
138 3c4fc30b Chris Buechler
				syslog(LOG_WARNING, "user '{$username}' cannot authenticate through IPsec since the required privileges are missing.\n");
139 a9157b6b Ermal
				continue;
140
			}
141 936fc874 Ermal
		}
142 52c9f9fa Ermal
		break;
143 936fc874 Ermal
	}
144 52c9f9fa Ermal
}
145
146
if ($authenticated == false) {
147 3260b82f Ermal
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
148 eadda967 Ermal
	if (isset($_GET['username'])) {
149 85d0e959 Ermal
		echo "FAILED";
150
		closelog();
151
		return;
152
	} else {
153
		closelog();
154 9a92e2ef Renato Botelho
		exit (-1);
155 85d0e959 Ermal
	}
156 52c9f9fa Ermal
}
157
158 b37a2e8c Phil Davis
if (file_exists("/etc/inc/ipsec.attributes.php")) {
159
	include_once("/etc/inc/ipsec.attributes.php");
160
}
161
162 3260b82f Ermal
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
163 85d0e959 Ermal
closelog();
164 52c9f9fa Ermal
165 b37a2e8c Phil Davis
if (isset($_GET['username'])) {
166 85d0e959 Ermal
	echo "OK";
167 b37a2e8c Phil Davis
} else {
168 9a92e2ef Renato Botelho
	exit (0);
169 b37a2e8c Phil Davis
}
170 52c9f9fa Ermal
171
?>