Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 856e58a6 Scott Ullrich
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3 5b237745 Scott Ullrich
/*
4 856e58a6 Scott Ullrich
5
    $Id$
6
7
    Copyright (c) 2006, Jonathan De Graeve <jonathan.de.graeve@imelda.be>
8
    All rights reserved.
9
10 ef345a70 Scott Ullrich
    Redistribution and use in source and binary forms, with or without 
11
    modification, are permitted provided that the following conditions 
12 856e58a6 Scott Ullrich
    are met:
13
14 ef345a70 Scott Ullrich
    1. Redistributions of source code must retain the above copyright 
15 856e58a6 Scott Ullrich
       notice, this list of conditions and the following disclaimer.
16 ef345a70 Scott Ullrich
    2. Redistributions in binary form must reproduce the above copyright 
17
       notice, this list of conditions and the following disclaimer in the 
18 856e58a6 Scott Ullrich
       documentation and/or other materials provided with the distribution.
19 ef345a70 Scott Ullrich
    3. The names of the authors may not be used to endorse or promote products 
20 856e58a6 Scott Ullrich
       derived from this software without specific prior written permission.
21
22 ef345a70 Scott Ullrich
    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 856e58a6 Scott Ullrich
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 ef345a70 Scott Ullrich
    This code cannot simply be copied and put under the GNU Public License or 
34 856e58a6 Scott Ullrich
    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 31964b64 Scott Ullrich
*/
40 5b237745 Scott Ullrich
41 f5024891 Scott Ullrich
/*
42
	pfSense_MODULE:	captiveportal
43
*/
44
45 cff5204a Ermal
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 856e58a6 Scott Ullrich
/*
74 ef345a70 Scott Ullrich
RADIUS ACCOUNTING START 
75 856e58a6 Scott Ullrich
-----------------------
76
*/
77
78 45a99750 Ermal
PEAR::loadExtension('bcmath');
79
80 40b48c6c Ermal Lu?i
function RADIUS_ACCOUNTING_START($ruleno, $username, $sessionid, $radiusservers, $clientip, $clientmac) {
81 856e58a6 Scott Ullrich
82 b4792bf8 Ermal
    global $config, $cpzone;
83 856e58a6 Scott Ullrich
84
    $retvalue = array();
85 805b9ab6 Ermal
    $nas_mac = mac_format(get_interface_mac("wan"));
86
    $clientmac = mac_format($clientmac);
87 e6bd2312 Ermal
    $nas_port = intval($ruleno);
88 b4792bf8 Ermal
    $radiusvendor = $config['captiveportal'][$cpzone]['radiusvendor'] ? $config['captiveportal'][$cpzone]['radiusvendor'] : null;
89 856e58a6 Scott Ullrich
90
    switch($radiusvendor) {
91
92
        case 'cisco':
93
        $calledstationid = $clientmac;
94
        $callingstationid = $clientip;
95
        break;
96
97
        default:
98 cff5204a Ermal
	$calledstationid = getNasIP();
99 856e58a6 Scott Ullrich
        $callingstationid = $clientmac;
100 cff5204a Ermal
	break;
101 856e58a6 Scott Ullrich
    }
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 40b48c6c Ermal Lu?i
    /*
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 856e58a6 Scott Ullrich
129
    if (PEAR::isError($racct->start())) {
130
        $retvalue['acct_val'] = 1;
131
        $retvalue['error'] = $racct->getMessage();
132 5918fbaf Scott Ullrich
133 856e58a6 Scott Ullrich
        // 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 d0d70b03 Ermal
    $racct->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
155 856e58a6 Scott Ullrich
    $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 5918fbaf Scott Ullrich
172 856e58a6 Scott Ullrich
    } else if ($result === true) {
173
        $retvalue['acct_val'] = 5 ;
174 5918fbaf Scott Ullrich
175 856e58a6 Scott Ullrich
    } else {
176
        $retvalue['acct_val'] = 1 ;
177 5918fbaf Scott Ullrich
178 856e58a6 Scott Ullrich
    }
179
180
    // close OO RADIUS_ACCOUNTING
181
    $racct->close();
182
183
    return $retvalue ;
184
185 5b237745 Scott Ullrich
}
186
187 856e58a6 Scott Ullrich
/*
188
RADIUS ACCOUNTING STOP/UPDATE
189
-----------------------------
190
*/
191
192 40b48c6c Ermal Lu?i
function RADIUS_ACCOUNTING_STOP($ruleno,$username,$sessionid,$start_time,$radiusservers,$clientip,$clientmac, $term_cause = 1, $interimupdate=false,$stop_time = null) {
193 856e58a6 Scott Ullrich
194 b4792bf8 Ermal
    global $config, $cpzone;
195 856e58a6 Scott Ullrich
196
    $retvalue = array();
197 805b9ab6 Ermal
    $nas_mac = mac_format(get_interface_mac("wan"));
198
    $clientmac = mac_format($clientmac);
199 e6bd2312 Ermal
    $nas_port = intval($ruleno);
200 b4792bf8 Ermal
    $radiusvendor = $config['captiveportal'][$cpzone]['radiusvendor'] ? $config['captiveportal'][$cpzone]['radiusvendor'] : null;
201 856e58a6 Scott Ullrich
    $stop_time = (empty($stop_time)) ? time() : $stop_time;
202
    $session_time = $stop_time - $start_time;
203 f9f71ad3 Ermal Lu?i
    $volume = getVolume($clientip);
204 856e58a6 Scott Ullrich
    $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 cff5204a Ermal
	$calledstationid = getNasIP();
218 856e58a6 Scott Ullrich
        $callingstationid = $clientmac;
219 cff5204a Ermal
	break;
220 856e58a6 Scott Ullrich
    }
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 ef345a70 Scott Ullrich
    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 40b48c6c Ermal Lu?i
    */
233 856e58a6 Scott Ullrich
    foreach ($radiusservers as $radsrv) {
234
        // Add a new server to our instance
235 40b48c6c Ermal Lu?i
        $racct->addServer($radsrv['ipaddr'], $radsrv['acctport'], $radsrv['key']);
236 856e58a6 Scott Ullrich
    }
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 5918fbaf Scott Ullrich
250 856e58a6 Scott Ullrich
        // 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 e6bd2312 Ermal
    $racct->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
263 856e58a6 Scott Ullrich
    $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 d3665a71 Scott Ullrich
	$racct->putAttribute(RADIUS_ACCT_SESSION_TIME, $session_time, "integer");
279 856e58a6 Scott Ullrich
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 637345f3 Scott Ullrich
293 856e58a6 Scott Ullrich
    } else if ($result === true) {
294
        $retvalue['acct_val'] = 5 ;
295 637345f3 Scott Ullrich
296 856e58a6 Scott Ullrich
    } else {
297
        $retvalue['acct_val'] = 1 ;
298 637345f3 Scott Ullrich
299 856e58a6 Scott Ullrich
    }
300
301
    // close OO RADIUS_ACCOUNTING
302
    $racct->close();
303
304
    return $retvalue;
305
306 5b237745 Scott Ullrich
}
307 31964b64 Scott Ullrich
308 856e58a6 Scott Ullrich
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 ac655e1b Ermal
    $gigawords = bcdiv( bcsub( $bytes, remainder($bytes) ) , PHP_INT_MAX) ;
324 856e58a6 Scott Ullrich
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 ac655e1b Ermal
    $bytes = bcmod($bytes, PHP_INT_MAX);
338 856e58a6 Scott Ullrich
339
    if (is_null($bytes)) {
340
        $bytes = 0;
341
    }
342
343
344
    return $bytes;
345
346 31964b64 Scott Ullrich
}
347
348 d0d70b03 Ermal
?>