Project

General

Profile

Download (3.55 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * ipsec.auth-user.php
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2008 Shrew Soft Inc
8
 * Copyright (c) 2008-2013 BSD Perimeter
9
 * Copyright (c) 2013-2016 Electric Sheep Fencing
10
 * Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
11
 * All rights reserved.
12
 *
13
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

    
26
/*
27
 * ipsec calls this script to authenticate a user
28
 * based on a username and password. We lookup these
29
 * in our config.xml file and check the credentials.
30
 */
31

    
32
require_once("globals.inc");
33
require_once("config.inc");
34
require_once("auth.inc");
35
require_once("interfaces.inc");
36

    
37
/* setup syslog logging */
38
openlog("charon", LOG_ODELAY, LOG_AUTH);
39

    
40
if (isset($_GET['username'])) {
41
	$authmodes = explode(",", $_GET['authcfg']);
42
	$username = $_GET['username'];
43
	$password = $_GET['password'];
44
	$common_name = $_GET['cn'];
45
} else {
46
	/* read data from environment */
47
	$username = getenv("username");
48
	$password = getenv("password");
49
	$common_name = getenv("common_name");
50
	$authmodes = explode(",", getenv("authcfg"));
51
}
52

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

    
65
$authenticated = false;
66

    
67
if (($strictusercn === true) && ($common_name != $username)) {
68
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.");
69
	if (isset($_GET['username'])) {
70
		echo "FAILED";
71
		closelog();
72
		return;
73
	} else {
74
		closelog();
75
		exit (1);
76
	}
77
}
78

    
79
$attributes = array("nas_identifier" => "xauthIPsec");
80
if ((config_get_path('ipsec/client/group_source') == 'enabled') &&
81
    !empty(config_get_path('ipsec/client/auth_groups'))) {
82
	$ipsec_groups = explode(",", config_get_path('ipsec/client/auth_groups'));
83
} else { 
84
	$ipsec_groups = '';
85
}
86
foreach ($authmodes as $authmode) {
87
	$authcfg = auth_get_authserver($authmode);
88
	if (!$authcfg && $authmode != "Local Database") {
89
		continue;
90
	}
91

    
92
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
93
	if ($authenticated == true) {
94
		$userGroups = getUserGroups($username, $authcfg, $attributes);
95
		if ($authmode == "Local Database") {
96
			$user = getUserEntry($username);
97
			if (!is_array($user) || !userHasPrivilege($user, "user-ipsec-xauth-dialin") ||
98
			    (!empty($ipsec_groups) && (count(array_intersect($userGroups, $ipsec_groups)) == 0))) {
99
				$authenticated = false;
100
				syslog(LOG_WARNING, "user '{$username}' cannot authenticate through IPsec since the required privileges are missing.");
101
				continue;
102
			}
103
		}
104
		break;
105
	}
106
}
107

    
108
if ($authenticated == false) {
109
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.");
110
	if (isset($_GET['username'])) {
111
		echo "FAILED";
112
		closelog();
113
		return;
114
	} else {
115
		closelog();
116
		exit (-1);
117
	}
118
}
119

    
120
if (file_exists("/etc/inc/ipsec.attributes.php")) {
121
	include_once("/etc/inc/ipsec.attributes.php");
122
}
123

    
124
syslog(LOG_NOTICE, "user '{$username}' authenticated");
125
closelog();
126

    
127
if (isset($_GET['username'])) {
128
	echo "OK";
129
} else {
130
	exit (0);
131
}
132

    
133
?>
(25-25/61)