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-2018 Rubicon Communications, LLC (Netgate)
|
9
|
* All rights reserved.
|
10
|
*
|
11
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
12
|
* you may not use this file except in compliance with the License.
|
13
|
* You may obtain a copy of the License at
|
14
|
*
|
15
|
* http://www.apache.org/licenses/LICENSE-2.0
|
16
|
*
|
17
|
* Unless required by applicable law or agreed to in writing, software
|
18
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
19
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
20
|
* See the License for the specific language governing permissions and
|
21
|
* limitations under the License.
|
22
|
*/
|
23
|
|
24
|
/*
|
25
|
* ipsec calls this script to authenticate a user
|
26
|
* based on a username and password. We lookup these
|
27
|
* in our config.xml file and check the credentials.
|
28
|
*/
|
29
|
|
30
|
require_once("globals.inc");
|
31
|
require_once("config.inc");
|
32
|
require_once("auth.inc");
|
33
|
require_once("interfaces.inc");
|
34
|
|
35
|
/**
|
36
|
* Get the NAS-Identifier
|
37
|
*
|
38
|
* We will use our local hostname to make up the nas_id
|
39
|
*/
|
40
|
if (!function_exists("getNasID")) {
|
41
|
function getNasID() {
|
42
|
global $g;
|
43
|
|
44
|
$nasId = gethostname();
|
45
|
if (empty($nasId)) {
|
46
|
$nasId = $g['product_name'];
|
47
|
}
|
48
|
return $nasId;
|
49
|
}
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Get the NAS-IP-Address based on the current wan address
|
54
|
*
|
55
|
* Use functions in interfaces.inc to find this out
|
56
|
*
|
57
|
*/
|
58
|
if (!function_exists("getNasIP")) {
|
59
|
function getNasIP() {
|
60
|
$nasIp = get_interface_ip();
|
61
|
if (!$nasIp) {
|
62
|
$nasIp = "0.0.0.0";
|
63
|
}
|
64
|
return $nasIp;
|
65
|
}
|
66
|
}
|
67
|
/* setup syslog logging */
|
68
|
openlog("charon", LOG_ODELAY, LOG_AUTH);
|
69
|
|
70
|
if (isset($_GET['username'])) {
|
71
|
$authmodes = explode(",", $_GET['authcfg']);
|
72
|
$username = $_GET['username'];
|
73
|
$password = $_GET['password'];
|
74
|
$common_name = $_GET['cn'];
|
75
|
} else {
|
76
|
/* read data from environment */
|
77
|
$username = getenv("username");
|
78
|
$password = getenv("password");
|
79
|
$common_name = getenv("common_name");
|
80
|
$authmodes = explode(",", getenv("authcfg"));
|
81
|
}
|
82
|
|
83
|
if (!$username || !$password) {
|
84
|
syslog(LOG_ERR, "invalid user authentication environment");
|
85
|
if (isset($_GET['username'])) {
|
86
|
echo "FAILED";
|
87
|
closelog();
|
88
|
return;
|
89
|
} else {
|
90
|
closelog();
|
91
|
exit (-1);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
$authenticated = false;
|
96
|
|
97
|
if (($strictusercn === true) && ($common_name != $username)) {
|
98
|
syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
|
99
|
if (isset($_GET['username'])) {
|
100
|
echo "FAILED";
|
101
|
closelog();
|
102
|
return;
|
103
|
} else {
|
104
|
closelog();
|
105
|
exit (1);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
$attributes = array();
|
110
|
foreach ($authmodes as $authmode) {
|
111
|
$authcfg = auth_get_authserver($authmode);
|
112
|
if (!$authcfg && $authmode != "Local Database") {
|
113
|
continue;
|
114
|
}
|
115
|
|
116
|
$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
|
117
|
if ($authenticated == true) {
|
118
|
if ($authmode == "Local Database") {
|
119
|
$user = getUserEntry($username);
|
120
|
if (!is_array($user) || !userHasPrivilege($user, "user-ipsec-xauth-dialin")) {
|
121
|
$authenticated = false;
|
122
|
syslog(LOG_WARNING, "user '{$username}' cannot authenticate through IPsec since the required privileges are missing.\n");
|
123
|
continue;
|
124
|
}
|
125
|
}
|
126
|
break;
|
127
|
}
|
128
|
}
|
129
|
|
130
|
if ($authenticated == false) {
|
131
|
syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
|
132
|
if (isset($_GET['username'])) {
|
133
|
echo "FAILED";
|
134
|
closelog();
|
135
|
return;
|
136
|
} else {
|
137
|
closelog();
|
138
|
exit (-1);
|
139
|
}
|
140
|
}
|
141
|
|
142
|
if (file_exists("/etc/inc/ipsec.attributes.php")) {
|
143
|
include_once("/etc/inc/ipsec.attributes.php");
|
144
|
}
|
145
|
|
146
|
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
|
147
|
closelog();
|
148
|
|
149
|
if (isset($_GET['username'])) {
|
150
|
echo "OK";
|
151
|
} else {
|
152
|
exit (0);
|
153
|
}
|
154
|
|
155
|
?>
|