Project

General

Profile

Download (4.26 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2
<?php
3
/*
4
	ipsec.auth-user.php
5

    
6
	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

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	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

    
21
	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

    
32
*/
33
/*
34
	pfSense_BUILDER_BINARIES:
35
	pfSense_MODULE:	openvpn
36
*/
37
/*
38
 * ipsec calls this script to authenticate a user
39
 * 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
	global $g;
57

    
58
	$nasId = gethostname();
59
	if (empty($nasId)) {
60
		$nasId = $g['product_name'];
61
	}
62
	return $nasId;
63
}
64
}
65

    
66
/**
67
 * Get the NAS-IP-Address based on the current wan address
68
 *
69
 * Use functions in interfaces.inc to find this out
70
 *
71
 */
72
if (!function_exists("getNasIP")) {
73
function getNasIP() {
74
	$nasIp = get_interface_ip();
75
	if (!$nasIp) {
76
		$nasIp = "0.0.0.0";
77
	}
78
	return $nasIp;
79
}
80
}
81
/* setup syslog logging */
82
openlog("charon", LOG_ODELAY, LOG_AUTH);
83

    
84
if (isset($_GET['username'])) {
85
	$authmodes = explode(",", $_GET['authcfg']);
86
	$username = $_GET['username'];
87
	$password = $_GET['password'];
88
	$common_name = $_GET['cn'];
89
} else {
90
	/* read data from environment */
91
	$username = getenv("username");
92
	$password = getenv("password");
93
	$common_name = getenv("common_name");
94
	$authmodes = explode(",", getenv("authcfg"));
95
}
96

    
97
if (!$username || !$password) {
98
	syslog(LOG_ERR, "invalid user authentication environment");
99
	if (isset($_GET['username'])) {
100
		echo "FAILED";
101
		closelog();
102
		return;
103
	} else {
104
		closelog();
105
		exit (-1);
106
	}
107
}
108

    
109
$authenticated = false;
110

    
111
if (($strictusercn === true) && ($common_name != $username)) {
112
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
113
	if (isset($_GET['username'])) {
114
		echo "FAILED";
115
		closelog();
116
		return;
117
	} else {
118
		closelog();
119
		exit (1);
120
	}
121
}
122

    
123
$attributes = array();
124
foreach ($authmodes as $authmode) {
125
	$authcfg = auth_get_authserver($authmode);
126
	if (!$authcfg && $authmode != "local") {
127
		continue;
128
	}
129

    
130
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
131
	if ($authenticated == true) {
132
		if (stristr($authmode, "local")) {
133
			$user = getUserEntry($username);
134
			if (!is_array($user) || !userHasPrivilege($user, "user-ipsec-xauth-dialin")) {
135
				$authenticated = false;
136
				syslog(LOG_WARNING, "user '{$username}' cannot authenticate through IPsec since the required privileges are missing.\n");
137
				continue;
138
			}
139
		}
140
		break;
141
	}
142
}
143

    
144
if ($authenticated == false) {
145
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
146
	if (isset($_GET['username'])) {
147
		echo "FAILED";
148
		closelog();
149
		return;
150
	} else {
151
		closelog();
152
		exit (-1);
153
	}
154
}
155

    
156
if (file_exists("/etc/inc/ipsec.attributes.php")) {
157
	include_once("/etc/inc/ipsec.attributes.php");
158
}
159

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

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

    
169
?>
(28-28/68)