summaryrefslogtreecommitdiffstats
path: root/usr/local/captiveportal/radius_accounting.inc
blob: 1aac92d26da37da654914544528a946fcff676ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/*
	radius_accounting.inc
	part of m0n0wall (http://m0n0.ch/wall)
	
	Copyright (C) 2004 Dinesh Nair <dinesh@alphaque.com>
	All rights reserved.
	
	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
	
	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.
	
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.

	// This version of radius_accounting.inc has been modified by
	// Rob Parker <rob.parker@keycom.co.uk>. Changes made include:
	// * now sends Framed-IP-Address (client IP)
	// * now sends Called-Station-ID (NAS IP)
	// * now sends Calling-Station-ID (client IP)
*/


function RADIUS_ACCOUNTING_START($username,$sessionid,$radiusip,$radiusport,$radiuskey,$clientip) {
	$sharedsecret=$radiuskey ;
	# $debug = 1 ;

	exec("/bin/hostname", $nasHostname) ;
	if(!$nasHostname[0])
		$nasHostname[0] = "m0n0wall" ;

	$fd = @fsockopen("udp://$radiusip",$radiusport,$errno,$errstr,3) ;
	if(!$fd) 
		return 1 ; /* error return */
	
	/* set 5 second timeout on socket i/o */
	stream_set_timeout($fd, 5) ;

	$nas_ip_address=get_current_wan_address();

	if(!isset($clientip)) {
		//if there's no client ip, we'll need to use the NAS ip
		$clientip=get_current_wan_address();
	}
	$ip_exp=explode(".",$clientip);

	if ($debug)
	    echo "<br>radius-port: $radiusport<br>radius-host: $radiusip<br>username: $username<hr>\n";

	$thisidentifier=rand()%256;

	$length=4+				// header
		16+				// auth code
		6+				// service type
		2+strlen($username)+		// username
		2+strlen($nasHostname[0])+			// nasIdentifier
		6+				// nasPort
		6+				// nasPortType
		6+				// Acct Status Type
		6+				// Acct RADIUS Authenticated
		2+strlen($sessionid)+	// Acct SessionID
		6;				// Framed-IP-Address

	//          v   v   v     v   v   v     v     v     v     1   v
	// Line #   1   2   3     4   5   6     7     8     9     0   E
	$data=pack("CCCCNNNNCCCCCCCCa*CCa*CCCCCCCCCCCCCCCCCCCCCCCCCCa*CCCCCC",
	    4,$thisidentifier,$length/256,$length%256,		// header
	    0,0,0,0,						// authcode
	    6,6,0,0,0,1,					// service type
	    1,2+strlen($username),$username,			// username
	    32,2+strlen($nasHostname[0]),$nasHostname[0],	// nasIdentifier
	    5,6,0,0,0,0,						// nasPort
	    61,6,0,0,0,15,						// nasPortType = Ethernet
		40,6,0,0,0,1,						// Acct Status Type = Start
		45,6,0,0,0,1,						// Acct RADIUS Authenticated
		44,2+strlen($sessionid),$sessionid,	// Acct Session ID
		8,6,$ip_exp[0],$ip_exp[1],$ip_exp[2],$ip_exp[3]	//Framed-IP-Address
	    );

	/* Generate Accounting Request Authenticator */
	$RA = md5($data.$radiuskey) ;

	//          v   v v     v   v   v     v     v     v     1   v
	// Line #   1   2 3     4   5   6     7     8     9     0   E
	$data=pack("CCCCH*CCCCCCCCa*CCa*CCCCCCCCCCCCCCCCCCCCCCCCCCa*CCCCCC",
	    4,$thisidentifier,$length/256,$length%256,		// header
	    $RA,						// authcode
	    6,6,0,0,0,1,					// service type
	    1,2+strlen($username),$username,			// username
	    32,2+strlen($nasHostname[0]),$nasHostname[0],	// nasIdentifier
	    5,6,0,0,0,0,						// nasPort
	    61,6,0,0,0,15,						// nasPortType = Ethernet
		40,6,0,0,0,1,						// Acct Status Type = Start
		45,6,0,0,0,1,						// Acct RADIUS Authenticated
		44,2+strlen($sessionid),$sessionid,	// Acct Session ID
		8,6,$ip_exp[0],$ip_exp[1],$ip_exp[2],$ip_exp[3]	//Framed-IP-Address
	    );

	if($debug) {
		echo "username is $username with len " . strlen($username) ."\n" ;
		echo "nasHostname is {$nasHostname[0]} with len " . strlen($nasHostname[0]) ."\n" ;
	}	

	$ret = fwrite($fd,$data) ;
	if( !$ret || ($ret != $length) ) 
		return 1; /* error return */

	if ($debug)
	    echo "<br>writing $length bytes<hr>\n";

	$readdata = fgets($fd,2) ; /* read 1 byte */
	$status = socket_get_status($fd) ;
	fclose($fd) ;

	if($status['timed_out'])
		$retvalue = 1 ;
	else
		$retvalue = ord($readdata) ;

	syslog(LOG_INFO,"Sent Accounting-Request Start packet to RADIUS for $username");

	return $retvalue ;
	// 5 -> Accounting-Response
	// See RFC2866 for this.
}

function RADIUS_ACCOUNTING_STOP($ruleno,$username,$sessionid,$start_time,$radiusip,$radiusport,$radiuskey,$clientip) {
	$sharedsecret=$radiuskey ;
	# $debug = 1 ;

	exec("/bin/hostname", $nasHostname) ;
	if(!$nasHostname[0])
		$nasHostname[0] = "quewall" ;

	$input_pkts = $input_bytes = $output_pkts = $output_bytes = 0 ;

	exec("/sbin/ipfw show {$ruleno}", $ipfw) ;	
	preg_match("/(\d+)\s+(\d+)\s+(\d+)\s+skipto/", $ipfw[0], $matches) ;
	$output_pkts = $matches[2] ;
	$output_bytes = $matches[3] ;

	unset($matches) ;
	preg_match("/(\d+)\s+(\d+)\s+(\d+)\s+skipto/", $ipfw[1], $matches) ;
	$input_pkts = $matches[2] ;
	$input_bytes = $matches[3] ;

	$fd = @fsockopen("udp://$radiusip",$radiusport,$errno,$errstr,3) ;
	if(!$fd) 
		return 1 ; /* error return */
	
	/* set 5 second timeout on socket i/o */
	stream_set_timeout($fd, 5) ;

	$nas_ip_address=get_current_wan_address();

	if(!isset($clientip)) {
		//if there's no client ip, we'll need to use the NAS ip
		$clientip=get_current_wan_address();
	}
	$ip_exp=explode(".",$clientip);

	if ($debug)
	    echo "<br>radius-port: $radiusport<br>radius-host: $radiusip<br>username: $username<hr>\n";

	$thisidentifier=rand()%256;

	$length=4+				// header
		16+				// auth code
		6+				// service type
		2+strlen($username)+		// username
		2+strlen($nasHostname[0])+			// nasIdentifier
		6+				// nasPort
		6+				// nasPortType
		6+				// Acct Status Type
		6+				// Acct RADIUS Authenticated
		2+strlen($sessionid)+	// Acct SessionID
		6+				// Acct terminate
		6+				// Session time
		6+				// input bytes
		6+				// input packets
		6+				// output bytes
		6+				// output packets
		2+strlen($nas_ip_address)+		//Called-Station-ID
		2+strlen($clientip);	//Calling-Station-ID

	//          v   v   v     v   v   v     v     v     v     1   1  1  1  1  1  1  v
	// Line #   1   2   3     4   5   6     7     8     9     0   1  2  3  4  5  6  E
	$data=pack("CCCCNNNNCCCCCCCCa*CCa*CCCCCCCCCCCCCCCCCCCCCCCCCCa*CCNCCNCCNCCNCCNCCNCCa*CCa*",
	    4,$thisidentifier,$length/256,$length%256,		// header
	    0,0,0,0,						// authcode
	    6,6,0,0,0,1,					// service type
	    1,2+strlen($username),$username,			// username
	    32,2+strlen($nasHostname[0]),$nasHostname[0],	// nasIdentifier
	    5,6,0,0,0,0,						// nasPort
	    61,6,0,0,0,15,						// nasPortType = Ethernet
		40,6,0,0,0,2,						// Acct Status Type = Stop
		45,6,0,0,0,1,						// Acct RADIUS Authenticated
		44,2+strlen($sessionid),$sessionid,	// Acct Session ID
		49,6,1,		// Acct Terminate = User Request
		46,6,time() - $start_time,			// Session Time
		42,6,$input_bytes,	// Input Octets
		47,6,$input_pkts,	// Input Packets
		43,6,$output_bytes, // Output Octets
		48,6,$output_pkts,	// Output Packets
		30,2+strlen($nas_ip_address),$nas_ip_address,	//Called-Station-ID
		31,2+strlen($clientip),$clientip				//Calling-Station-ID
	    );

	/* Generate Accounting Request Authenticator */
	$RA = md5($data.$radiuskey) ;

	//          v   v v     v   v   v     v     v     v     1   1  1  1  1  1  1  v
	// Line #   1   2 3     4   5   6     7     8     9     0   1  2  3  4  5  6  E
	$data=pack("CCCCH*CCCCCCCCa*CCa*CCCCCCCCCCCCCCCCCCCCCCCCCCa*CCNCCNCCNCCNCCNCCNCCa*CCa*",
	    4,$thisidentifier,$length/256,$length%256,		// header
	    $RA,						// authcode
	    6,6,0,0,0,1,					// service type
	    1,2+strlen($username),$username,			// username
	    32,2+strlen($nasHostname[0]),$nasHostname[0],	// nasIdentifier
	    5,6,0,0,0,0,						// nasPort
	    61,6,0,0,0,15,						// nasPortType = Ethernet
		40,6,0,0,0,2,						// Acct Status Type = Stop
		45,6,0,0,0,1,						// Acct RADIUS Authenticated
		44,2+strlen($sessionid),$sessionid,	// Acct Session ID
		49,6,1,		// Acct Terminate = User Request
		46,6,time() - $start_time,			// Session Time
		42,6,$input_bytes,	// Input Octets
		47,6,$input_pkts,	// Input Packets
		43,6,$output_bytes, // Output Octets
		48,6,$output_pkts,	// Output Packets
		30,2+strlen($nas_ip_address),$nas_ip_address,	//Called-Station-ID
		31,2+strlen($clientip),$clientip				//Calling-Station-ID
	    );

	if($debug) {
		echo "username is $username with len " . strlen($username) ."\n" ;
		echo "nasHostname is {$nasHostname[0]} with len " . strlen($nasHostname[0]) ."\n" ;
	}	

	$ret = fwrite($fd,$data) ;
	if( !$ret || ($ret != $length) ) 
		return 1; /* error return */

	if ($debug)
	    echo "<br>writing $length bytes<hr>\n";

	$readdata = fgets($fd,2) ; /* read 1 byte */
	$status = socket_get_status($fd) ;
	fclose($fd) ;

	if($status['timed_out'])
		$retvalue = 1 ;
	else
		$retvalue = ord($readdata) ;

	syslog(LOG_INFO,"Sent Accounting-Request Stop packet to RADIUS for $username");

	return $retvalue ;
	// 5 -> Accounting-Response
	// See RFC2866 for this.
}
?>
OpenPOWER on IntegriCloud