Project

General

Profile

Download (4.08 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
    //
3
    // $Id$
4
    //
5
    // radius authentication v1.0 by Edwin Groothuis (edwin@mavetju.org)
6
    //
7
    // If you didn't get this file via http://www.mavetju.org, please
8
    // check for the availability of newer versions.
9
    //
10
    // See LICENSE for distribution issues. If this file isn't in
11
    // the distribution, please inform me about it.
12
    //
13
    // If you want to use this script, fill in the configuration in
14
    // radius_authentication.conf and call the function
15
    // RADIUS_AUTHENTICATION() with the username and password
16
    // provided by the user. If it returns a 2, the authentication
17
    // was successfull!
18

    
19
    // If you want to use this, make sure that you have raw sockets
20
    // enabled during compile-time: "./configure --enable-sockets".
21

    
22
	// This version has been modified by Dinesh Nair <dinesh@alphaque.com>
23
	// for use in the m0n0wall distribution http://m0n0.ch/wall/
24
	//
25
	// Changes include moving from raw sockets to fsockopen
26
	// and the removal of dependency on external conf file
27
	// An existing bug which resulted in a malformed RADIUS packet
28
	// was also fixed and patches submitted to Edwin. This bug would
29
	// have caused authentication to fail on every access.
30

    
31
function RADIUS_AUTHENTICATION($username,$password,$radiusip,$radiusport,$radiuskey) {
32
	$sharedsecret=$radiuskey ;
33
	global $debug, $errno, $nasHostname, $errstr;
34
	# $debug = 1 ;
35

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

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

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

    
50
	$RA=pack("CCCCCCCCCCCCCCCC",				// auth code
51
	    1+rand()%255, 1+rand()%255, 1+rand()%255, 1+rand()%255,
52
	    1+rand()%255, 1+rand()%255, 1+rand()%255, 1+rand()%255,
53
	    1+rand()%255, 1+rand()%255, 1+rand()%255, 1+rand()%255,
54
	    1+rand()%255, 1+rand()%255, 1+rand()%255, 1+rand()%255);
55

    
56
	$encryptedpassword=Encrypt($password,$sharedsecret,$RA);
57

    
58
	$length=4+				// header
59
		16+				// auth code
60
		6+				// service type
61
		2+strlen($username)+		// username
62
		2+strlen($encryptedpassword)+	// userpassword
63
		2+strlen($nasHostname[0])+			// nasIdentifier
64
		6+				// nasPort
65
		6;				// nasPortType
66

    
67
	$thisidentifier=rand()%256;
68
	//          v   v v     v   v   v   v     v     v
69
	// Line #   1   2 3     4   5   6   7     8     E
70
	$data=pack("CCCCa*CCCCCCCCa*CCa*CCa*CCCCCCCCCCCC",
71
	    1,$thisidentifier,$length/256,$length%256,		// header
72
	    $RA,						// authcode
73
	    6,6,0,0,0,1,					// service type
74
	    1,2+strlen($username),$username,			// username
75
	    2,2+strlen($encryptedpassword),$encryptedpassword,	// userpassword
76
	    32,2+strlen($nasHostname[0]),$nasHostname[0],	// nasIdentifier
77
	    5,6,0,0,0,0,						// nasPort
78
	    61,6,0,0,0,15						// nasPortType = Ethernet
79
	    );
80

    
81
	if($debug) {
82
		echo "username is $username with len " . strlen($username) ."\n" ;
83
		echo "encryptedpassword is $encryptedpassword with len " . strlen($encryptedpassword) ."\n" ;
84
		echo "nasHostname is {$nasHostname[0]} with len " . strlen($nasHostname[0]) ."\n" ;
85
	}	
86

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

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

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

    
98
	if($status['timed_out'])
99
		$retvalue = 1 ;
100
	else
101
		$retvalue = ord($readdata) ;
102

    
103
	return $retvalue ;
104
	// 2 -> Access-Accept
105
	// 3 -> Access-Reject
106
	// See RFC2865 for this.
107
}
108

    
109
function Encrypt($password,$key,$RA) {
110
	global $debug;
111

    
112
	$keyRA=$key.$RA;
113

    
114
	if ($debug)
115
	    echo "<br>key: $key<br>password: $password<hr>\n";
116

    
117
	$md5checksum=md5($keyRA);
118
	$output="";
119

    
120
	for ($i=0;$i<=15;$i++) {
121
	    if (2*$i>strlen($md5checksum)) $m=0; else $m=hexdec(substr($md5checksum,2*$i,2));
122
	    if ($i>strlen($keyRA)) $k=0; else $k=ord(substr($keyRA,$i,1));
123
	    if ($i>strlen($password)) $p=0; else $p=ord(substr($password,$i,1));
124
	    $c=$m^$p;
125
	    $output.=chr($c);
126
	}
127
	return $output;
128
}
129
?>
(3-3/3)