Project

General

Profile

Download (9.8 KB) Statistics
| Branch: | Tag: | Revision:
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
RADIUS ACCOUNTING START 
43
-----------------------
44
*/
45

    
46
function RADIUS_ACCOUNTING_START($ruleno,$username,$sessionid,$radiusip,$radiusport,$radiuskey,$clientip,$clientmac) {
47

    
48
    global $config;
49

    
50
    $retvalue = array();
51
    $nas_mac = mac_format(get_interface_mac($config['interfaces']['wan']['if']));
52
    $clientmac = mac_format($clientmac);
53
    $nas_port = $ruleno - 10000;
54
    $radiusvendor = $config['captiveportal']['radiusvendor'] ? $config['captiveportal']['radiusvendor'] : null;
55

    
56
    switch($radiusvendor) {
57

    
58
        case 'cisco':
59
        $calledstationid = $clientmac;
60
        $callingstationid = $clientip;
61
        break;
62

    
63
        default:
64
        $calledstationid = $nas_mac;
65
        $callingstationid = $clientmac;
66
    }
67

    
68
    // Create our instance
69
    $racct = new Auth_RADIUS_Acct_Start;
70

    
71
    /* Different Authentication options
72
     *
73
     * Its possible todo other authentication methods but still do radius accounting
74
     *
75
     * RADIUS_AUTH_RADIUS => authenticated via Radius
76
     * RADIUS_AUTH_LOCAL  => authenticated local
77
     * RADIUS_AUTH_REMOTE => authenticated remote
78
     *
79
     */
80
    $racct->authentic = RADIUS_AUTH_RADIUS;
81

    
82
    // Construct data package
83
    $racct->username = $username;
84
    $racct->addServer($radiusip, $radiusport, $radiuskey);
85

    
86
    if (PEAR::isError($racct->start())) {
87
        $retvalue['acct_val'] = 1;
88
        $retvalue['error'] = $racct->getMessage();
89

    
90
        // If we encounter an error immediately stop this function and go back
91
        $racct->close();
92
        return $retvalue;
93

    
94
        /* Old code:
95
         * $status = $racct->start();
96
         * if(PEAR::isError($status)) {
97
         *    if ($debug)
98
         *        printf("Radius start: %s<br>\n", $status->getMessage());
99
         *        exit;
100
         * }
101
         */
102
    }
103

    
104
    /*
105
     * NAS_PORT_TYPE, int => RADIUS_ETHERNET (15), RADIUS_WIRELESS_OTHER (18), RADIUS_WIRELESS_IEEE_802_11 (19)
106
     */
107

    
108
    // Default attributes
109
    $racct->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_LOGIN);
110
    $racct->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
111
    $racct->putAttribute(RADIUS_NAS_PORT, $nas_port);
112
    $racct->putAttribute(RADIUS_ACCT_SESSION_ID, $sessionid);
113

    
114
    // Extra data to identify the client and nas
115
    $racct->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, "addr");
116
    $racct->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
117
    $racct->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
118

    
119
    // Send request
120
    $result = $racct->send();
121

    
122
    // Evaluation of the response
123
    // 5 -> Accounting-Response
124
    // See RFC2866 for this.
125
    if (PEAR::isError($result)) {
126
        $retvalue['acct_val'] = 1;
127
        $retvalue['error'] = $result->getMessage();
128

    
129
    } else if ($result === true) {
130
        $retvalue['acct_val'] = 5 ;
131

    
132
    } else {
133
        $retvalue['acct_val'] = 1 ;
134

    
135
    }
136

    
137
    // close OO RADIUS_ACCOUNTING
138
    $racct->close();
139

    
140
    return $retvalue ;
141

    
142
}
143

    
144
/*
145
RADIUS ACCOUNTING STOP/UPDATE
146
-----------------------------
147
*/
148

    
149
function RADIUS_ACCOUNTING_STOP($ruleno,$username,$sessionid,$start_time,$radiusip,$radiusport,$radiuskey,$clientip,$clientmac, $term_cause = 1, $interimupdate=false,$stop_time = null) {
150

    
151
    global $config;
152

    
153
    $retvalue = array();
154
    $nas_mac = mac_format(get_interface_mac($config['interfaces']['wan']['if']));
155
    $clientmac = mac_format($clientmac);
156
    $nas_port = $ruleno - 10000;
157
    $radiusvendor = $config['captiveportal']['radiusvendor'] ? $config['captiveportal']['radiusvendor'] : null;
158
    $stop_time = (empty($stop_time)) ? time() : $stop_time;
159
    $session_time = $stop_time - $start_time;
160
    $volume = getVolume($ruleno);
161
    $volume['input_bytes_radius'] = remainder($volume['input_bytes']);
162
    $volume['input_gigawords'] = gigawords($volume['input_bytes']);
163
    $volume['output_bytes_radius'] = remainder($volume['output_bytes']);
164
    $volume['output_gigawords'] = gigawords($volume['output_bytes']);
165

    
166
    switch($radiusvendor) {
167

    
168
        case 'cisco':
169
        $calledstationid = $clientmac;
170
        $callingstationid = $clientip;
171
        break;
172

    
173
        default:
174
        $calledstationid = $nas_mac;
175
        $callingstationid = $clientmac;
176
    }
177

    
178
    // Create our instance, see if we should use Accounting Interim Updates or Accounting STOP messages
179
    if ($interimupdate)
180
        $racct = new Auth_RADIUS_Acct_Update;
181
    else
182
        $racct = new Auth_RADIUS_Acct_Stop;
183

    
184
    /*
185
     * Currently disabled
186
    Add support for more then one radiusserver. 
187
    At most 10 servers may be specified. 
188
    When multiple servers are given, they are tried in round-robin fashion until a valid response is received 
189

    
190
    foreach ($radiusservers as $radsrv) {
191

    
192
        // Add a new server to our instance
193
        $racct->addServer($radsrv['ipaddr'], $radsrv['port'], $radsrv['key']);
194

    
195
    }
196
    */
197

    
198
    // See RADIUS_ACCOUNTING_START for info
199
    $racct->authentic = RADIUS_AUTH_RADIUS;
200

    
201
    // Construct data package
202
    $racct->username = $username;
203
    $racct->addServer($radiusip, $radiusport, $radiuskey);
204
    // Set session_time
205
    $racct->session_time = $session_time;
206

    
207
    if (PEAR::isError($racct->start())) {
208
        $retvalue['acct_val'] = 1;
209
        $retvalue['error'] = $racct->getMessage();
210

    
211
        // If we encounter an error immediately stop this function and go back
212
        $racct->close();
213
        return $retvalue;
214
    }
215

    
216
    // The RADIUS PECL Package doesn't have this vars so we create them ourself
217
    define("RADIUS_ACCT_INPUT_GIGAWORDS", "52");
218
    define("RADIUS_ACCT_OUTPUT_GIGAWORDS", "53");
219

    
220
    // Default attributes
221
    $racct->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_LOGIN);
222
    $racct->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
223
    $racct->putAttribute(RADIUS_NAS_PORT, $nas_port);
224
    $racct->putAttribute(RADIUS_ACCT_SESSION_ID, $sessionid);
225

    
226
    // Extra data to identify the client and nas
227
    $racct->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, "addr");
228
    $racct->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
229
    $racct->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
230

    
231
    // Volume stuff: Ingress
232
    $racct->putAttribute(RADIUS_ACCT_INPUT_PACKETS, $volume['input_pkts'], "integer");
233
    $racct->putAttribute(RADIUS_ACCT_INPUT_OCTETS, $volume['input_bytes_radius'], "integer");
234
    $racct->putAttribute(RADIUS_ACCT_INPUT_GIGAWORDS, $volume['input_gigawords'], "integer");
235
    // Volume stuff: Outgress
236
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_PACKETS, $volume['output_pkts'], "integer");
237
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_OCTETS, $volume['output_bytes_radius'], "integer");
238
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_GIGAWORDS, $volume['output_gigawords'], "integer");
239

    
240
    if (!$interimupdate)
241
        $racct->putAttribute(RADIUS_ACCT_TERMINATE_CAUSE, $term_cause);
242

    
243
    // Send request
244
    $result = $racct->send();
245

    
246
    // Evaluation of the response
247
    // 5 -> Accounting-Response
248
    // See RFC2866 for this.
249
    if (PEAR::isError($result)) {
250
        $retvalue['acct_val'] = 1;
251
        $retvalue['error'] = $result->getMessage();
252

    
253
    } else if ($result === true) {
254
        $retvalue['acct_val'] = 5 ;
255

    
256
    } else {
257
        $retvalue['acct_val'] = 1 ;
258

    
259
    }
260

    
261
    // close OO RADIUS_ACCOUNTING
262
    $racct->close();
263

    
264
    return $retvalue;
265

    
266
}
267

    
268

    
269
/**
270
 * Radius Volume Helpers
271
 *
272
 */
273

    
274
function gigawords($bytes) {
275

    
276

    
277
    /*
278
     * RFC2866 Specifies a 32bit unsigned integer, which is a max of 4294967295
279
     * Currently there is a fault in the PECL radius_put_int function which can handle only 32bit signed integer.
280
     */
281

    
282
    // We use BCMath functions since normal integers don't work with so large numbers
283
    $gigawords = bcdiv( bcsub( $bytes, remainder($bytes) ) , 4294967295) ;
284

    
285
    // We need to manually set this to a zero instead of NULL for put_int() safety
286
    if (is_null($gigawords)) {
287
        $gigawords = 0;
288
    }
289

    
290
    return $gigawords;
291

    
292
}
293

    
294
function remainder($bytes) {
295

    
296
    // Calculate the bytes we are going to send to the radius
297
    $bytes = bcmod($bytes, 4294967295);
298

    
299
    if (is_null($bytes)) {
300
        $bytes = 0;
301
    }
302

    
303

    
304
    return $bytes;
305

    
306
}
307

    
308
?>
(2-2/3)