Project

General

Profile

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