1
|
<?php
|
2
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */
|
3
|
/*
|
4
|
$Id$
|
5
|
|
6
|
Copyright (c) 2006, Jonathan De Graeve <jonathan.de.graeve@imelda.be>
|
7
|
All rights reserved.
|
8
|
|
9
|
Redistribution and use in source and binary forms, with or without
|
10
|
modification, are permitted provided that the following conditions
|
11
|
are met:
|
12
|
|
13
|
1. Redistributions of source code must retain the above copyright
|
14
|
notice, this list of conditions and the following disclaimer.
|
15
|
2. Redistributions in binary form must reproduce the above copyright
|
16
|
notice, this list of conditions and the following disclaimer in the
|
17
|
documentation and/or other materials provided with the distribution.
|
18
|
3. The names of the authors may not be used to endorse or promote products
|
19
|
derived from this software without specific prior written permission.
|
20
|
|
21
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
22
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
23
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
24
|
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
25
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
26
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
28
|
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
29
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
30
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
|
32
|
This code cannot simply be copied and put under the GNU Public License or
|
33
|
any other GPL-like (LGPL, GPL2) License.
|
34
|
|
35
|
This code is made possible thx to samples made by Michael Bretterklieber <michael@bretterklieber.com>
|
36
|
author of the PHP PECL Radius package
|
37
|
|
38
|
*/
|
39
|
|
40
|
/*
|
41
|
pfSense_MODULE: captiveportal
|
42
|
*/
|
43
|
|
44
|
/**
|
45
|
* Get the NAS-IP-Address based on the current wan address
|
46
|
*
|
47
|
* Use functions in interfaces.inc to find this out
|
48
|
*
|
49
|
*/
|
50
|
if (!function_exists('getNasIP')) {
|
51
|
function getNasIP()
|
52
|
{
|
53
|
global $config, $cpzone;
|
54
|
|
55
|
if (empty($config['captiveportal'][$cpzone]['radiussrcip_attribute'])) {
|
56
|
$nasIp = get_interface_ip();
|
57
|
} else {
|
58
|
if (is_ipaddr($config['captiveportal'][$cpzone]['radiussrcip_attribute']))
|
59
|
$nasIp = $config['captiveportal'][$cpzone]['radiussrcip_attribute'];
|
60
|
else
|
61
|
$nasIp = get_interface_ip($config['captiveportal'][$cpzone]['radiussrcip_attribute']);
|
62
|
}
|
63
|
|
64
|
if(!is_ipaddr($nasIp))
|
65
|
$nasIp = "0.0.0.0";
|
66
|
|
67
|
return $nasIp;
|
68
|
}
|
69
|
}
|
70
|
|
71
|
/*
|
72
|
RADIUS AUTHENTICATION
|
73
|
---------------------
|
74
|
*/
|
75
|
|
76
|
require_once("CHAP.inc");
|
77
|
|
78
|
function RADIUS_AUTHENTICATION($username,$password,$radiusservers,$clientip,$clientmac,$ruleno) {
|
79
|
|
80
|
global $config, $cpzone;
|
81
|
|
82
|
$retvalue = array();
|
83
|
$clientmac = mac_format($clientmac);
|
84
|
$nas_port = $ruleno;
|
85
|
$radiusvendor = $config['captiveportal'][$cpzone]['radiusvendor'] ? $config['captiveportal'][$cpzone]['radiusvendor'] : null;
|
86
|
$radius_protocol = $config['captiveportal'][$cpzone]['radius_protocol'];
|
87
|
// Do we even need to set it to NULL?
|
88
|
$retvalue['error'] = $retvalue['reply_message'] = $retvalue['url_redirection'] = $retvalue['session_timeout'] = null;
|
89
|
$retvalue['idle_timeout'] = $retvalue['session_terminate_time'] = $retvalue['interim_interval'] = null;
|
90
|
|
91
|
switch($radiusvendor) {
|
92
|
|
93
|
case 'cisco':
|
94
|
$calledstationid = $clientmac;
|
95
|
$callingstationid = $clientip;
|
96
|
break;
|
97
|
default:
|
98
|
$calledstationid = getNasIP();
|
99
|
$callingstationid = $clientmac;
|
100
|
break;
|
101
|
}
|
102
|
|
103
|
// Create our instance
|
104
|
$classname = 'Auth_RADIUS_' . $radius_protocol;
|
105
|
$rauth = new $classname($username, $password);
|
106
|
|
107
|
/*
|
108
|
* Add support for more then one radiusserver.
|
109
|
* At most 10 servers may be specified.
|
110
|
* When multiple servers are given, they are tried in round-robin fashion until a valid response is received
|
111
|
*/
|
112
|
foreach ($radiusservers as $radsrv) {
|
113
|
// Add a new server to our instance
|
114
|
$rauth->addServer($radsrv['ipaddr'], $radsrv['port'], $radsrv['key']);
|
115
|
|
116
|
}
|
117
|
|
118
|
// Construct data package
|
119
|
$rauth->username = $username;
|
120
|
switch ($radius_protocol) {
|
121
|
case 'CHAP_MD5':
|
122
|
case 'MSCHAPv1':
|
123
|
$classname = $radius_protocol == 'MSCHAPv1' ? 'Crypt_CHAP_MSv1' : 'Crypt_CHAP_MD5';
|
124
|
$crpt = new $classname;
|
125
|
$crpt->username = $username;
|
126
|
$crpt->password = $password;
|
127
|
$rauth->challenge = $crpt->challenge;
|
128
|
$rauth->chapid = $crpt->chapid;
|
129
|
$rauth->response = $crpt->challengeResponse();
|
130
|
$rauth->flags = 1;
|
131
|
// If you must use deprecated and weak LAN-Manager-Responses use this:
|
132
|
//$rauth->lmResponse = $crpt->lmChallengeResponse();
|
133
|
//$rauth->flags = 0;
|
134
|
break;
|
135
|
|
136
|
case 'MSCHAPv2':
|
137
|
// Construct data package
|
138
|
$crpt = new Crypt_CHAP_MSv2;
|
139
|
$crpt->username = $username;
|
140
|
$crpt->password = $password;
|
141
|
$rauth->challenge = $crpt->authChallenge;
|
142
|
$rauth->peerChallenge = $crpt->peerChallenge;
|
143
|
$rauth->chapid = $crpt->chapid;
|
144
|
$rauth->response = $crpt->challengeResponse();
|
145
|
break;
|
146
|
|
147
|
default:
|
148
|
$rauth->password = $password;
|
149
|
break;
|
150
|
}
|
151
|
|
152
|
if (PEAR::isError($rauth->start())) {
|
153
|
$retvalue['auth_val'] = 1;
|
154
|
$retvalue['error'] = $rauth->getError();
|
155
|
|
156
|
// If we encounter an error immediately stop this function and go back
|
157
|
$rauth->close();
|
158
|
return $retvalue;
|
159
|
}
|
160
|
|
161
|
// Default attributes
|
162
|
$rauth->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_LOGIN);
|
163
|
$rauth->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
|
164
|
$rauth->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
|
165
|
|
166
|
// Extra data to identify the client and nas
|
167
|
$rauth->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, addr);
|
168
|
$rauth->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
|
169
|
$rauth->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
|
170
|
|
171
|
// Send request
|
172
|
$result = $rauth->send();
|
173
|
|
174
|
// Evaluation of the response
|
175
|
// 1 -> Access-Request => We will use this value as an error indicator since we can't get a 1 back from the radius
|
176
|
// 2 -> Access-Accept
|
177
|
// 3 -> Access-Reject
|
178
|
// See RFC2865 for this.
|
179
|
if (PEAR::isError($result)) {
|
180
|
$retvalue['auth_val'] = 1;
|
181
|
$retvalue['error'] = $result->getMessage();
|
182
|
|
183
|
} else if ($result === true) {
|
184
|
$retvalue['auth_val'] = 2;
|
185
|
|
186
|
} else {
|
187
|
$retvalue['auth_val'] = 3;
|
188
|
|
189
|
}
|
190
|
|
191
|
// Get attributes, even if auth failed.
|
192
|
// We will push the results in the retvalue array
|
193
|
if (!$rauth->getAttributes()) {
|
194
|
$retvalue['error'] = $rauth->getError();
|
195
|
|
196
|
} else {
|
197
|
$retvalue = array_merge($retvalue,$rauth->listAttributes());
|
198
|
|
199
|
// We convert the session_terminate_time to unixtimestamp if its set before returning the whole array to our caller
|
200
|
if (!empty($retvalue['session_terminate_time'])) {
|
201
|
$stt = &$retvalue['session_terminate_time'];
|
202
|
$stt = strtotime(preg_replace("/\+(\d+):(\d+)$/", " +\${1}\${2}", preg_replace("/(\d+)T(\d+)/", "\${1} \${2}",$stt)));
|
203
|
}
|
204
|
}
|
205
|
|
206
|
// close OO RADIUS_AUTHENTICATION
|
207
|
$rauth->close();
|
208
|
unset($rauth);
|
209
|
|
210
|
return $retvalue;
|
211
|
|
212
|
}
|
213
|
|
214
|
?>
|