Project

General

Profile

Download (4.22 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -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
 * ipsec calls this script to authenticate a user
35
 * based on a username and password. We lookup these
36
 * in our config.xml file and check the credentials.
37
 */
38

    
39
require_once("globals.inc");
40
require_once("config.inc");
41
require_once("radius.inc");
42
require_once("auth.inc");
43
require_once("interfaces.inc");
44

    
45
/**
46
 * Get the NAS-Identifier
47
 *
48
 * We will use our local hostname to make up the nas_id
49
 */
50
if (!function_exists("getNasID")) {
51
function getNasID() {
52
	global $g;
53

    
54
	$nasId = gethostname();
55
	if (empty($nasId)) {
56
		$nasId = $g['product_name'];
57
	}
58
	return $nasId;
59
}
60
}
61

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

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

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

    
105
$authenticated = false;
106

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

    
119
$attributes = array();
120
foreach ($authmodes as $authmode) {
121
	$authcfg = auth_get_authserver($authmode);
122
	if (!$authcfg && $authmode != "Local Database") {
123
		continue;
124
	}
125

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

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

    
152
if (file_exists("/etc/inc/ipsec.attributes.php")) {
153
	include_once("/etc/inc/ipsec.attributes.php");
154
}
155

    
156
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
157
closelog();
158

    
159
if (isset($_GET['username'])) {
160
	echo "OK";
161
} else {
162
	exit (0);
163
}
164

    
165
?>
(27-27/65)