1
|
<?php
|
2
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */
|
3
|
/*
|
4
|
|
5
|
$Id$
|
6
|
|
7
|
Copyright (c) 2006, Jonathan De Graeve <jonathan.de.graeve@imelda.be>
|
8
|
All rights reserved.
|
9
|
|
10
|
Redistribution and use in source and binary forms, with or without
|
11
|
modification, are permitted provided that the following conditions
|
12
|
are met:
|
13
|
|
14
|
1. Redistributions of source code must retain the above copyright
|
15
|
notice, this list of conditions and the following disclaimer.
|
16
|
2. Redistributions in binary form must reproduce the above copyright
|
17
|
notice, this list of conditions and the following disclaimer in the
|
18
|
documentation and/or other materials provided with the distribution.
|
19
|
3. The names of the authors may not be used to endorse or promote products
|
20
|
derived from this software without specific prior written permission.
|
21
|
|
22
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
23
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
24
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
25
|
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
26
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
27
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
28
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
29
|
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
30
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
31
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
|
33
|
This code cannot simply be copied and put under the GNU Public License or
|
34
|
any other GPL-like (LGPL, GPL2) License.
|
35
|
|
36
|
This code is made possible thx to samples made by Michael Bretterklieber <michael@bretterklieber.com>
|
37
|
author of the PHP PECL Radius package
|
38
|
|
39
|
*/
|
40
|
|
41
|
/*
|
42
|
pfSense_MODULE: captiveportal
|
43
|
*/
|
44
|
|
45
|
define('GIGAWORDS_RIGHT_OPERAND', '4294967296'); // 2^32
|
46
|
|
47
|
/*
|
48
|
RADIUS ACCOUNTING START
|
49
|
-----------------------
|
50
|
*/
|
51
|
|
52
|
PEAR::loadExtension('bcmath');
|
53
|
|
54
|
function RADIUS_ACCOUNTING_START($ruleno, $username, $sessionid, $radiusservers, $clientip, $clientmac) {
|
55
|
|
56
|
global $config, $cpzone;
|
57
|
|
58
|
$retvalue = array();
|
59
|
$nas_mac = mac_format(get_interface_mac("wan"));
|
60
|
$clientmac = mac_format($clientmac);
|
61
|
$nas_port = intval($ruleno);
|
62
|
$radiusvendor = $config['captiveportal'][$cpzone]['radiusvendor'] ? $config['captiveportal'][$cpzone]['radiusvendor'] : null;
|
63
|
|
64
|
switch($radiusvendor) {
|
65
|
|
66
|
case 'cisco':
|
67
|
$calledstationid = $clientmac;
|
68
|
$callingstationid = $clientip;
|
69
|
break;
|
70
|
|
71
|
default:
|
72
|
if (!function_exists('getNasIP'))
|
73
|
require_once("captiveportal.inc");
|
74
|
$calledstationid = getNasIP();
|
75
|
$callingstationid = $clientmac;
|
76
|
break;
|
77
|
}
|
78
|
|
79
|
// Create our instance
|
80
|
$racct = new Auth_RADIUS_Acct_Start;
|
81
|
|
82
|
/* Different Authentication options
|
83
|
*
|
84
|
* Its possible todo other authentication methods but still do radius accounting
|
85
|
*
|
86
|
* RADIUS_AUTH_RADIUS => authenticated via Radius
|
87
|
* RADIUS_AUTH_LOCAL => authenticated local
|
88
|
* RADIUS_AUTH_REMOTE => authenticated remote
|
89
|
*
|
90
|
*/
|
91
|
$racct->authentic = RADIUS_AUTH_RADIUS;
|
92
|
|
93
|
// Construct data package
|
94
|
$racct->username = $username;
|
95
|
/*
|
96
|
Add support for more then one radiusserver.
|
97
|
At most 10 servers may be specified.
|
98
|
When multiple servers are given, they are tried in round-robin fashion until a valid response is received
|
99
|
*/
|
100
|
foreach ($radiusservers as $radsrv) {
|
101
|
// Add a new server to our instance
|
102
|
$racct->addServer($radsrv['ipaddr'], $radsrv['acctport'], $radsrv['key']);
|
103
|
}
|
104
|
|
105
|
if (PEAR::isError($racct->start())) {
|
106
|
$retvalue['acct_val'] = 1;
|
107
|
$retvalue['error'] = $racct->getMessage();
|
108
|
|
109
|
// If we encounter an error immediately stop this function and go back
|
110
|
$racct->close();
|
111
|
return $retvalue;
|
112
|
|
113
|
/* Old code:
|
114
|
* $status = $racct->start();
|
115
|
* if(PEAR::isError($status)) {
|
116
|
* if ($debug)
|
117
|
* printf("Radius start: %s<br />\n", $status->getMessage());
|
118
|
* exit;
|
119
|
* }
|
120
|
*/
|
121
|
}
|
122
|
|
123
|
/*
|
124
|
* NAS_PORT_TYPE, int => RADIUS_ETHERNET (15), RADIUS_WIRELESS_OTHER (18), RADIUS_WIRELESS_IEEE_802_11 (19)
|
125
|
*/
|
126
|
|
127
|
// Default attributes
|
128
|
$racct->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
|
129
|
$racct->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
|
130
|
$racct->putAttribute(RADIUS_ACCT_SESSION_ID, $sessionid);
|
131
|
|
132
|
// Extra data to identify the client and nas
|
133
|
$racct->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, "addr");
|
134
|
$racct->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
|
135
|
$racct->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
|
136
|
|
137
|
// Send request
|
138
|
$result = $racct->send();
|
139
|
|
140
|
// Evaluation of the response
|
141
|
// 5 -> Accounting-Response
|
142
|
// See RFC2866 for this.
|
143
|
if (PEAR::isError($result)) {
|
144
|
$retvalue['acct_val'] = 1;
|
145
|
$retvalue['error'] = $result->getMessage();
|
146
|
|
147
|
} else if ($result === true) {
|
148
|
$retvalue['acct_val'] = 5 ;
|
149
|
|
150
|
} else {
|
151
|
$retvalue['acct_val'] = 1 ;
|
152
|
|
153
|
}
|
154
|
|
155
|
// close OO RADIUS_ACCOUNTING
|
156
|
$racct->close();
|
157
|
unset($racct);
|
158
|
|
159
|
return $retvalue ;
|
160
|
|
161
|
}
|
162
|
|
163
|
/*
|
164
|
RADIUS ACCOUNTING STOP/UPDATE
|
165
|
-----------------------------
|
166
|
*/
|
167
|
|
168
|
function RADIUS_ACCOUNTING_STOP($ruleno,$username,$sessionid,$start_time,$radiusservers,$clientip,$clientmac, $term_cause = 1, $interimupdate=false,$stop_time = null) {
|
169
|
|
170
|
global $config, $cpzone;
|
171
|
|
172
|
$retvalue = array();
|
173
|
$nas_mac = mac_format(get_interface_mac("wan"));
|
174
|
$clientmac = mac_format($clientmac);
|
175
|
$nas_port = intval($ruleno);
|
176
|
$radiusvendor = $config['captiveportal'][$cpzone]['radiusvendor'] ? $config['captiveportal'][$cpzone]['radiusvendor'] : null;
|
177
|
$stop_time = (empty($stop_time)) ? time() : $stop_time;
|
178
|
$session_time = $stop_time - $start_time;
|
179
|
$volume = getVolume($clientip, $clientmac);
|
180
|
$volume['input_bytes_radius'] = remainder($volume['input_bytes']);
|
181
|
$volume['input_gigawords'] = gigawords($volume['input_bytes']);
|
182
|
$volume['output_bytes_radius'] = remainder($volume['output_bytes']);
|
183
|
$volume['output_gigawords'] = gigawords($volume['output_bytes']);
|
184
|
|
185
|
switch($radiusvendor) {
|
186
|
|
187
|
case 'cisco':
|
188
|
$calledstationid = $clientmac;
|
189
|
$callingstationid = $clientip;
|
190
|
break;
|
191
|
|
192
|
default:
|
193
|
$calledstationid = getNasIP();
|
194
|
$callingstationid = $clientmac;
|
195
|
break;
|
196
|
}
|
197
|
|
198
|
// Create our instance, see if we should use Accounting Interim Updates or Accounting STOP messages
|
199
|
if ($interimupdate)
|
200
|
$racct = new Auth_RADIUS_Acct_Update;
|
201
|
else
|
202
|
$racct = new Auth_RADIUS_Acct_Stop;
|
203
|
|
204
|
/*
|
205
|
Add support for more then one radiusserver.
|
206
|
At most 10 servers may be specified.
|
207
|
When multiple servers are given, they are tried in round-robin fashion until a valid response is received
|
208
|
*/
|
209
|
foreach ($radiusservers as $radsrv) {
|
210
|
// Add a new server to our instance
|
211
|
$racct->addServer($radsrv['ipaddr'], $radsrv['acctport'], $radsrv['key']);
|
212
|
}
|
213
|
|
214
|
// See RADIUS_ACCOUNTING_START for info
|
215
|
$racct->authentic = RADIUS_AUTH_RADIUS;
|
216
|
|
217
|
// Construct data package
|
218
|
$racct->username = $username;
|
219
|
// Set session_time
|
220
|
$racct->session_time = $session_time;
|
221
|
|
222
|
if (PEAR::isError($racct->start())) {
|
223
|
$retvalue['acct_val'] = 1;
|
224
|
$retvalue['error'] = $racct->getMessage();
|
225
|
|
226
|
// If we encounter an error immediately stop this function and go back
|
227
|
$racct->close();
|
228
|
return $retvalue;
|
229
|
}
|
230
|
|
231
|
// The RADIUS PECL Package doesn't have this vars so we create them ourself
|
232
|
define("RADIUS_ACCT_INPUT_GIGAWORDS", "52");
|
233
|
define("RADIUS_ACCT_OUTPUT_GIGAWORDS", "53");
|
234
|
|
235
|
// Default attributes
|
236
|
$racct->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
|
237
|
$racct->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
|
238
|
$racct->putAttribute(RADIUS_ACCT_SESSION_ID, $sessionid);
|
239
|
|
240
|
// Extra data to identify the client and nas
|
241
|
$racct->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, "addr");
|
242
|
$racct->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
|
243
|
$racct->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
|
244
|
|
245
|
// Volume stuff: Ingress
|
246
|
$racct->putAttribute(RADIUS_ACCT_INPUT_PACKETS, $volume['input_pkts'], "integer");
|
247
|
$racct->putAttribute(RADIUS_ACCT_INPUT_OCTETS, $volume['input_bytes_radius'], "integer");
|
248
|
$racct->putAttribute(RADIUS_ACCT_INPUT_GIGAWORDS, $volume['input_gigawords'], "integer");
|
249
|
// Volume stuff: Outgress
|
250
|
$racct->putAttribute(RADIUS_ACCT_OUTPUT_PACKETS, $volume['output_pkts'], "integer");
|
251
|
$racct->putAttribute(RADIUS_ACCT_OUTPUT_OCTETS, $volume['output_bytes_radius'], "integer");
|
252
|
$racct->putAttribute(RADIUS_ACCT_OUTPUT_GIGAWORDS, $volume['output_gigawords'], "integer");
|
253
|
$racct->putAttribute(RADIUS_ACCT_SESSION_TIME, $session_time, "integer");
|
254
|
|
255
|
if (!$interimupdate)
|
256
|
$racct->putAttribute(RADIUS_ACCT_TERMINATE_CAUSE, $term_cause);
|
257
|
|
258
|
// Send request
|
259
|
$result = $racct->send();
|
260
|
|
261
|
// Evaluation of the response
|
262
|
// 5 -> Accounting-Response
|
263
|
// See RFC2866 for this.
|
264
|
if (PEAR::isError($result)) {
|
265
|
$retvalue['acct_val'] = 1;
|
266
|
$retvalue['error'] = $result->getMessage();
|
267
|
|
268
|
} else if ($result === true) {
|
269
|
$retvalue['acct_val'] = 5 ;
|
270
|
|
271
|
} else {
|
272
|
$retvalue['acct_val'] = 1 ;
|
273
|
|
274
|
}
|
275
|
|
276
|
// close OO RADIUS_ACCOUNTING
|
277
|
$racct->close();
|
278
|
|
279
|
return $retvalue;
|
280
|
|
281
|
}
|
282
|
|
283
|
|
284
|
/**
|
285
|
* Radius Volume Helpers
|
286
|
*
|
287
|
*/
|
288
|
|
289
|
function gigawords($bytes) {
|
290
|
|
291
|
|
292
|
/*
|
293
|
* RFC2866 Specifies a 32bit unsigned integer, which is a max of 4294967295
|
294
|
* Currently there is a fault in the PECL radius_put_int function which can handle only 32bit signed integer.
|
295
|
*/
|
296
|
|
297
|
// We use BCMath functions since normal integers don't work with so large numbers
|
298
|
$gigawords = bcdiv( bcsub( $bytes, remainder($bytes) ) , GIGAWORDS_RIGHT_OPERAND) ;
|
299
|
|
300
|
// We need to manually set this to a zero instead of NULL for put_int() safety
|
301
|
if (is_null($gigawords)) {
|
302
|
$gigawords = 0;
|
303
|
}
|
304
|
|
305
|
return $gigawords;
|
306
|
|
307
|
}
|
308
|
|
309
|
function remainder($bytes) {
|
310
|
|
311
|
// Calculate the bytes we are going to send to the radius
|
312
|
$bytes = bcmod($bytes, GIGAWORDS_RIGHT_OPERAND);
|
313
|
|
314
|
if (is_null($bytes)) {
|
315
|
$bytes = 0;
|
316
|
}
|
317
|
|
318
|
|
319
|
return $bytes;
|
320
|
|
321
|
}
|
322
|
|
323
|
?>
|