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-2025 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 = array_filter(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 = array_filter(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
|
$user = $user['item'];
|
98
|
if (!is_array($user) || !userHasPrivilege($user, "user-ipsec-xauth-dialin") ||
|
99
|
(!empty($ipsec_groups) && (count(array_intersect($userGroups, $ipsec_groups)) == 0))) {
|
100
|
$authenticated = false;
|
101
|
syslog(LOG_WARNING, "user '{$username}' cannot authenticate through IPsec since the required privileges are missing.");
|
102
|
continue;
|
103
|
}
|
104
|
}
|
105
|
break;
|
106
|
}
|
107
|
}
|
108
|
|
109
|
if ($authenticated == false) {
|
110
|
syslog(LOG_WARNING, "user '{$username}' could not authenticate.");
|
111
|
if (isset($_GET['username'])) {
|
112
|
echo "FAILED";
|
113
|
closelog();
|
114
|
return;
|
115
|
} else {
|
116
|
closelog();
|
117
|
exit (-1);
|
118
|
}
|
119
|
}
|
120
|
|
121
|
if (file_exists("/etc/inc/ipsec.attributes.php")) {
|
122
|
include_once("/etc/inc/ipsec.attributes.php");
|
123
|
}
|
124
|
|
125
|
syslog(LOG_NOTICE, "user '{$username}' authenticated");
|
126
|
closelog();
|
127
|
|
128
|
if (isset($_GET['username'])) {
|
129
|
echo "OK";
|
130
|
} else {
|
131
|
exit (0);
|
132
|
}
|
133
|
|
134
|
?>
|