Project

General

Profile

Download (10.4 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 = intval($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
	if (is_ipaddr($config['captiveportal']['radiussrcip_attribute']))
71
            $calledstationid = $config['captiveportal']['radiussrcip_attribute'];
72
        else
73
                $calledstationid = get_interface_ip($config['captiveportal']['radiussrcip_attribute']);
74
            $callingstationid = $clientmac;
75
        $callingstationid = $clientmac;
76
    }
77

    
78
    // Create our instance
79
    $racct = new Auth_RADIUS_Acct_Start;
80

    
81
    /* Different Authentication options
82
     *
83
     * Its possible todo other authentication methods but still do radius accounting
84
     *
85
     * RADIUS_AUTH_RADIUS => authenticated via Radius
86
     * RADIUS_AUTH_LOCAL  => authenticated local
87
     * RADIUS_AUTH_REMOTE => authenticated remote
88
     *
89
     */
90
    $racct->authentic = RADIUS_AUTH_RADIUS;
91

    
92
    // Construct data package
93
    $racct->username = $username;
94
    /*
95
    Add support for more then one radiusserver.
96
    At most 10 servers may be specified.
97
    When multiple servers are given, they are tried in round-robin fashion until a valid response is received
98
    */
99
    foreach ($radiusservers as $radsrv) {
100
        // Add a new server to our instance
101
        $racct->addServer($radsrv['ipaddr'], $radsrv['acctport'], $radsrv['key']);
102
    }
103

    
104
    if (PEAR::isError($racct->start())) {
105
        $retvalue['acct_val'] = 1;
106
        $retvalue['error'] = $racct->getMessage();
107

    
108
        // If we encounter an error immediately stop this function and go back
109
        $racct->close();
110
        return $retvalue;
111

    
112
        /* Old code:
113
         * $status = $racct->start();
114
         * if(PEAR::isError($status)) {
115
         *    if ($debug)
116
         *        printf("Radius start: %s<br>\n", $status->getMessage());
117
         *        exit;
118
         * }
119
         */
120
    }
121

    
122
    /*
123
     * NAS_PORT_TYPE, int => RADIUS_ETHERNET (15), RADIUS_WIRELESS_OTHER (18), RADIUS_WIRELESS_IEEE_802_11 (19)
124
     */
125

    
126
    // Default attributes
127
    $racct->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_LOGIN);
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

    
158
    return $retvalue ;
159

    
160
}
161

    
162
/*
163
RADIUS ACCOUNTING STOP/UPDATE
164
-----------------------------
165
*/
166

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

    
169
    global $config;
170

    
171
    $retvalue = array();
172
    $nas_mac = mac_format(get_interface_mac("wan"));
173
    $clientmac = mac_format($clientmac);
174
    $nas_port = intval($ruleno);
175
    $radiusvendor = $config['captiveportal']['radiusvendor'] ? $config['captiveportal']['radiusvendor'] : null;
176
    $stop_time = (empty($stop_time)) ? time() : $stop_time;
177
    $session_time = $stop_time - $start_time;
178
    $volume = getVolume($clientip);
179
    $volume['input_bytes_radius'] = remainder($volume['input_bytes']);
180
    $volume['input_gigawords'] = gigawords($volume['input_bytes']);
181
    $volume['output_bytes_radius'] = remainder($volume['output_bytes']);
182
    $volume['output_gigawords'] = gigawords($volume['output_bytes']);
183

    
184
    switch($radiusvendor) {
185

    
186
        case 'cisco':
187
        $calledstationid = $clientmac;
188
        $callingstationid = $clientip;
189
        break;
190

    
191
        default:
192
        $calledstationid = get_interface_ip("wan");
193
        $callingstationid = $clientmac;
194
    }
195

    
196
    // Create our instance, see if we should use Accounting Interim Updates or Accounting STOP messages
197
    if ($interimupdate)
198
        $racct = new Auth_RADIUS_Acct_Update;
199
    else
200
        $racct = new Auth_RADIUS_Acct_Stop;
201

    
202
    /*
203
    Add support for more then one radiusserver. 
204
    At most 10 servers may be specified. 
205
    When multiple servers are given, they are tried in round-robin fashion until a valid response is received 
206
    */
207
    foreach ($radiusservers as $radsrv) {
208
        // Add a new server to our instance
209
        $racct->addServer($radsrv['ipaddr'], $radsrv['acctport'], $radsrv['key']);
210
    }
211

    
212
    // See RADIUS_ACCOUNTING_START for info
213
    $racct->authentic = RADIUS_AUTH_RADIUS;
214

    
215
    // Construct data package
216
    $racct->username = $username;
217
    // Set session_time
218
    $racct->session_time = $session_time;
219

    
220
    if (PEAR::isError($racct->start())) {
221
        $retvalue['acct_val'] = 1;
222
        $retvalue['error'] = $racct->getMessage();
223

    
224
        // If we encounter an error immediately stop this function and go back
225
        $racct->close();
226
        return $retvalue;
227
    }
228

    
229
    // The RADIUS PECL Package doesn't have this vars so we create them ourself
230
    define("RADIUS_ACCT_INPUT_GIGAWORDS", "52");
231
    define("RADIUS_ACCT_OUTPUT_GIGAWORDS", "53");
232

    
233
    // Default attributes
234
    $racct->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_LOGIN);
235
    $racct->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
236
    $racct->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
237
    $racct->putAttribute(RADIUS_ACCT_SESSION_ID, $sessionid);
238

    
239
    // Extra data to identify the client and nas
240
    $racct->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, "addr");
241
    $racct->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
242
    $racct->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
243

    
244
    // Volume stuff: Ingress
245
    $racct->putAttribute(RADIUS_ACCT_INPUT_PACKETS, $volume['input_pkts'], "integer");
246
    $racct->putAttribute(RADIUS_ACCT_INPUT_OCTETS, $volume['input_bytes_radius'], "integer");
247
    $racct->putAttribute(RADIUS_ACCT_INPUT_GIGAWORDS, $volume['input_gigawords'], "integer");
248
    // Volume stuff: Outgress
249
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_PACKETS, $volume['output_pkts'], "integer");
250
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_OCTETS, $volume['output_bytes_radius'], "integer");
251
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_GIGAWORDS, $volume['output_gigawords'], "integer");
252
	$racct->putAttribute(RADIUS_ACCT_SESSION_TIME, $session_time, "integer");
253

    
254
    if (!$interimupdate)
255
        $racct->putAttribute(RADIUS_ACCT_TERMINATE_CAUSE, $term_cause);
256

    
257
    // Send request
258
    $result = $racct->send();
259

    
260
    // Evaluation of the response
261
    // 5 -> Accounting-Response
262
    // See RFC2866 for this.
263
    if (PEAR::isError($result)) {
264
        $retvalue['acct_val'] = 1;
265
        $retvalue['error'] = $result->getMessage();
266

    
267
    } else if ($result === true) {
268
        $retvalue['acct_val'] = 5 ;
269

    
270
    } else {
271
        $retvalue['acct_val'] = 1 ;
272

    
273
    }
274

    
275
    // close OO RADIUS_ACCOUNTING
276
    $racct->close();
277

    
278
    return $retvalue;
279

    
280
}
281

    
282

    
283
/**
284
 * Radius Volume Helpers
285
 *
286
 */
287

    
288
function gigawords($bytes) {
289

    
290

    
291
    /*
292
     * RFC2866 Specifies a 32bit unsigned integer, which is a max of 4294967295
293
     * Currently there is a fault in the PECL radius_put_int function which can handle only 32bit signed integer.
294
     */
295

    
296
    // We use BCMath functions since normal integers don't work with so large numbers
297
    $gigawords = bcdiv( bcsub( $bytes, remainder($bytes) ) , PHP_INT_MAX) ;
298

    
299
    // We need to manually set this to a zero instead of NULL for put_int() safety
300
    if (is_null($gigawords)) {
301
        $gigawords = 0;
302
    }
303

    
304
    return $gigawords;
305

    
306
}
307

    
308
function remainder($bytes) {
309

    
310
    // Calculate the bytes we are going to send to the radius
311
    $bytes = bcmod($bytes, PHP_INT_MAX);
312

    
313
    if (is_null($bytes)) {
314
        $bytes = 0;
315
    }
316

    
317

    
318
    return $bytes;
319

    
320
}
321

    
322
?>
(2-2/3)