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

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

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

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

    
69
		return $nasIp;
70
	}
71
}
72

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

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

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

    
82
    global $config, $cpzone;
83

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

    
90
    switch($radiusvendor) {
91

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

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

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

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

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

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

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

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

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

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

    
183
    return $retvalue ;
184

    
185
}
186

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

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

    
194
    global $config, $cpzone;
195

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

    
209
    switch($radiusvendor) {
210

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

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

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

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

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

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

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

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

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

    
259
    // Default attributes
260
    $racct->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_LOGIN);
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) ) , PHP_INT_MAX) ;
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, PHP_INT_MAX);
338

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

    
343

    
344
    return $bytes;
345

    
346
}
347

    
348
?>
(2-2/3)