Project

General

Profile

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

    
45
define('GIGAWORDS_RIGHT_OPERAND', '4294967296'); // 2^32
46

    
47
/**
48
 * Get the NAS-IP-Address based on the current wan address
49
 *
50
 * Use functions in interfaces.inc to find this out
51
 *
52
 */
53
if (!function_exists('getNasIP')) {
54
	function getNasIP()
55
	{
56
		global $config, $cpzone;
57

    
58
		if (empty($config['captiveportal'][$cpzone]['radiussrcip_attribute'])) {
59
				$nasIp = get_interface_ip();
60
		} else {
61
			if (is_ipaddr($config['captiveportal'][$cpzone]['radiussrcip_attribute']))
62
				$nasIp = $config['captiveportal'][$cpzone]['radiussrcip_attribute'];
63
			else
64
				$nasIp = get_interface_ip($config['captiveportal'][$cpzone]['radiussrcip_attribute']);
65
		}
66

    
67
		if(!is_ipaddr($nasIp))
68
			$nasIp = "0.0.0.0";
69

    
70
		return $nasIp;
71
	}
72
}
73

    
74
/*
75
RADIUS ACCOUNTING START 
76
-----------------------
77
*/
78

    
79
PEAR::loadExtension('bcmath');
80

    
81
function RADIUS_ACCOUNTING_START($ruleno, $username, $sessionid, $radiusservers, $clientip, $clientmac) {
82

    
83
    global $config, $cpzone;
84

    
85
    $retvalue = array();
86
    $nas_mac = mac_format(get_interface_mac("wan"));
87
    $clientmac = mac_format($clientmac);
88
    $nas_port = intval($ruleno);
89
    $radiusvendor = $config['captiveportal'][$cpzone]['radiusvendor'] ? $config['captiveportal'][$cpzone]['radiusvendor'] : null;
90

    
91
    switch($radiusvendor) {
92

    
93
        case 'cisco':
94
        $calledstationid = $clientmac;
95
        $callingstationid = $clientip;
96
        break;
97

    
98
        default:
99
	$calledstationid = getNasIP();
100
        $callingstationid = $clientmac;
101
	break;
102
    }
103

    
104
    // Create our instance
105
    $racct = new Auth_RADIUS_Acct_Start;
106

    
107
    /* Different Authentication options
108
     *
109
     * Its possible todo other authentication methods but still do radius accounting
110
     *
111
     * RADIUS_AUTH_RADIUS => authenticated via Radius
112
     * RADIUS_AUTH_LOCAL  => authenticated local
113
     * RADIUS_AUTH_REMOTE => authenticated remote
114
     *
115
     */
116
    $racct->authentic = RADIUS_AUTH_RADIUS;
117

    
118
    // Construct data package
119
    $racct->username = $username;
120
    /*
121
    Add support for more then one radiusserver.
122
    At most 10 servers may be specified.
123
    When multiple servers are given, they are tried in round-robin fashion until a valid response is received
124
    */
125
    foreach ($radiusservers as $radsrv) {
126
        // Add a new server to our instance
127
        $racct->addServer($radsrv['ipaddr'], $radsrv['acctport'], $radsrv['key']);
128
    }
129

    
130
    if (PEAR::isError($racct->start())) {
131
        $retvalue['acct_val'] = 1;
132
        $retvalue['error'] = $racct->getMessage();
133

    
134
        // If we encounter an error immediately stop this function and go back
135
        $racct->close();
136
        return $retvalue;
137

    
138
        /* Old code:
139
         * $status = $racct->start();
140
         * if(PEAR::isError($status)) {
141
         *    if ($debug)
142
         *        printf("Radius start: %s<br>\n", $status->getMessage());
143
         *        exit;
144
         * }
145
         */
146
    }
147

    
148
    /*
149
     * NAS_PORT_TYPE, int => RADIUS_ETHERNET (15), RADIUS_WIRELESS_OTHER (18), RADIUS_WIRELESS_IEEE_802_11 (19)
150
     */
151

    
152
    // Default attributes
153
    $racct->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
154
    $racct->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
155
    $racct->putAttribute(RADIUS_ACCT_SESSION_ID, $sessionid);
156

    
157
    // Extra data to identify the client and nas
158
    $racct->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, "addr");
159
    $racct->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
160
    $racct->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
161

    
162
    // Send request
163
    $result = $racct->send();
164

    
165
    // Evaluation of the response
166
    // 5 -> Accounting-Response
167
    // See RFC2866 for this.
168
    if (PEAR::isError($result)) {
169
        $retvalue['acct_val'] = 1;
170
        $retvalue['error'] = $result->getMessage();
171

    
172
    } else if ($result === true) {
173
        $retvalue['acct_val'] = 5 ;
174

    
175
    } else {
176
        $retvalue['acct_val'] = 1 ;
177

    
178
    }
179

    
180
    // close OO RADIUS_ACCOUNTING
181
    $racct->close();
182
    unset($racct);
183

    
184
    return $retvalue ;
185

    
186
}
187

    
188
/*
189
RADIUS ACCOUNTING STOP/UPDATE
190
-----------------------------
191
*/
192

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

    
195
    global $config, $cpzone;
196

    
197
    $retvalue = array();
198
    $nas_mac = mac_format(get_interface_mac("wan"));
199
    $clientmac = mac_format($clientmac);
200
    $nas_port = intval($ruleno);
201
    $radiusvendor = $config['captiveportal'][$cpzone]['radiusvendor'] ? $config['captiveportal'][$cpzone]['radiusvendor'] : null;
202
    $stop_time = (empty($stop_time)) ? time() : $stop_time;
203
    $session_time = $stop_time - $start_time;
204
    $volume = getVolume($clientip);
205
    $volume['input_bytes_radius'] = remainder($volume['input_bytes']);
206
    $volume['input_gigawords'] = gigawords($volume['input_bytes']);
207
    $volume['output_bytes_radius'] = remainder($volume['output_bytes']);
208
    $volume['output_gigawords'] = gigawords($volume['output_bytes']);
209

    
210
    switch($radiusvendor) {
211

    
212
        case 'cisco':
213
        $calledstationid = $clientmac;
214
        $callingstationid = $clientip;
215
        break;
216

    
217
        default:
218
	$calledstationid = getNasIP();
219
        $callingstationid = $clientmac;
220
	break;
221
    }
222

    
223
    // Create our instance, see if we should use Accounting Interim Updates or Accounting STOP messages
224
    if ($interimupdate)
225
        $racct = new Auth_RADIUS_Acct_Update;
226
    else
227
        $racct = new Auth_RADIUS_Acct_Stop;
228

    
229
    /*
230
    Add support for more then one radiusserver. 
231
    At most 10 servers may be specified. 
232
    When multiple servers are given, they are tried in round-robin fashion until a valid response is received 
233
    */
234
    foreach ($radiusservers as $radsrv) {
235
        // Add a new server to our instance
236
        $racct->addServer($radsrv['ipaddr'], $radsrv['acctport'], $radsrv['key']);
237
    }
238

    
239
    // See RADIUS_ACCOUNTING_START for info
240
    $racct->authentic = RADIUS_AUTH_RADIUS;
241

    
242
    // Construct data package
243
    $racct->username = $username;
244
    // Set session_time
245
    $racct->session_time = $session_time;
246

    
247
    if (PEAR::isError($racct->start())) {
248
        $retvalue['acct_val'] = 1;
249
        $retvalue['error'] = $racct->getMessage();
250

    
251
        // If we encounter an error immediately stop this function and go back
252
        $racct->close();
253
        return $retvalue;
254
    }
255

    
256
    // The RADIUS PECL Package doesn't have this vars so we create them ourself
257
    define("RADIUS_ACCT_INPUT_GIGAWORDS", "52");
258
    define("RADIUS_ACCT_OUTPUT_GIGAWORDS", "53");
259

    
260
    // Default attributes
261
    $racct->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
262
    $racct->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
263
    $racct->putAttribute(RADIUS_ACCT_SESSION_ID, $sessionid);
264

    
265
    // Extra data to identify the client and nas
266
    $racct->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, "addr");
267
    $racct->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
268
    $racct->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
269

    
270
    // Volume stuff: Ingress
271
    $racct->putAttribute(RADIUS_ACCT_INPUT_PACKETS, $volume['input_pkts'], "integer");
272
    $racct->putAttribute(RADIUS_ACCT_INPUT_OCTETS, $volume['input_bytes_radius'], "integer");
273
    $racct->putAttribute(RADIUS_ACCT_INPUT_GIGAWORDS, $volume['input_gigawords'], "integer");
274
    // Volume stuff: Outgress
275
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_PACKETS, $volume['output_pkts'], "integer");
276
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_OCTETS, $volume['output_bytes_radius'], "integer");
277
    $racct->putAttribute(RADIUS_ACCT_OUTPUT_GIGAWORDS, $volume['output_gigawords'], "integer");
278
	$racct->putAttribute(RADIUS_ACCT_SESSION_TIME, $session_time, "integer");
279

    
280
    if (!$interimupdate)
281
        $racct->putAttribute(RADIUS_ACCT_TERMINATE_CAUSE, $term_cause);
282

    
283
    // Send request
284
    $result = $racct->send();
285

    
286
    // Evaluation of the response
287
    // 5 -> Accounting-Response
288
    // See RFC2866 for this.
289
    if (PEAR::isError($result)) {
290
        $retvalue['acct_val'] = 1;
291
        $retvalue['error'] = $result->getMessage();
292

    
293
    } else if ($result === true) {
294
        $retvalue['acct_val'] = 5 ;
295

    
296
    } else {
297
        $retvalue['acct_val'] = 1 ;
298

    
299
    }
300

    
301
    // close OO RADIUS_ACCOUNTING
302
    $racct->close();
303

    
304
    return $retvalue;
305

    
306
}
307

    
308

    
309
/**
310
 * Radius Volume Helpers
311
 *
312
 */
313

    
314
function gigawords($bytes) {
315

    
316

    
317
    /*
318
     * RFC2866 Specifies a 32bit unsigned integer, which is a max of 4294967295
319
     * Currently there is a fault in the PECL radius_put_int function which can handle only 32bit signed integer.
320
     */
321

    
322
    // We use BCMath functions since normal integers don't work with so large numbers
323
    $gigawords = bcdiv( bcsub( $bytes, remainder($bytes) ) , GIGAWORDS_RIGHT_OPERAND) ;
324

    
325
    // We need to manually set this to a zero instead of NULL for put_int() safety
326
    if (is_null($gigawords)) {
327
        $gigawords = 0;
328
    }
329

    
330
    return $gigawords;
331

    
332
}
333

    
334
function remainder($bytes) {
335

    
336
    // Calculate the bytes we are going to send to the radius
337
    $bytes = bcmod($bytes, GIGAWORDS_RIGHT_OPERAND);
338

    
339
    if (is_null($bytes)) {
340
        $bytes = 0;
341
    }
342

    
343

    
344
    return $bytes;
345

    
346
}
347

    
348
?>
(2-2/3)