Project

General

Profile

Download (10.1 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
	pfSense_MODULE:	captiveportal
43
*/
44

    
45
/*
46
RADIUS ACCOUNTING START 
47
-----------------------
48
*/
49

    
50
PEAR::loadExtension('bcmath');
51

    
52
function RADIUS_ACCOUNTING_START($ruleno, $username, $sessionid, $radiusservers, $clientip, $clientmac) {
53

    
54
    global $config;
55

    
56
    $retvalue = array();
57
    $nas_mac = mac_format(get_interface_mac("wan"));
58
    $clientmac = mac_format($clientmac);
59
    $nas_port = $ruleno;
60
    $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
        $calledstationid = get_interface_ip("wan");
71
        $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
    /*
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

    
100
    if (PEAR::isError($racct->start())) {
101
        $retvalue['acct_val'] = 1;
102
        $retvalue['error'] = $racct->getMessage();
103

    
104
        // 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

    
143
    } else if ($result === true) {
144
        $retvalue['acct_val'] = 5 ;
145

    
146
    } else {
147
        $retvalue['acct_val'] = 1 ;
148

    
149
    }
150

    
151
    // close OO RADIUS_ACCOUNTING
152
    $racct->close();
153

    
154
    return $retvalue ;
155

    
156
}
157

    
158
/*
159
RADIUS ACCOUNTING STOP/UPDATE
160
-----------------------------
161
*/
162

    
163
function RADIUS_ACCOUNTING_STOP($ruleno,$username,$sessionid,$start_time,$radiusservers,$clientip,$clientmac, $term_cause = 1, $interimupdate=false,$stop_time = null) {
164

    
165
    global $config;
166

    
167
    $retvalue = array();
168
    $nas_mac = mac_format(get_interface_mac("wan"));
169
    $clientmac = mac_format($clientmac);
170
    $nas_port = $ruleno;
171
    $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
    $volume = getVolume($clientip);
175
    $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
        $calledstationid = get_interface_ip("wan");
189
        $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
    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
    */
203
    foreach ($radiusservers as $radsrv) {
204
        // Add a new server to our instance
205
        $racct->addServer($radsrv['ipaddr'], $radsrv['acctport'], $radsrv['key']);
206
    }
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

    
220
        // 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
	$racct->putAttribute(RADIUS_ACCT_SESSION_TIME, $session_time, "integer");
249

    
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

    
263
    } else if ($result === true) {
264
        $retvalue['acct_val'] = 5 ;
265

    
266
    } else {
267
        $retvalue['acct_val'] = 1 ;
268

    
269
    }
270

    
271
    // close OO RADIUS_ACCOUNTING
272
    $racct->close();
273

    
274
    return $retvalue;
275

    
276
}
277

    
278

    
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
}
317

    
318
?>
(2-2/3)