-
Notifications
You must be signed in to change notification settings - Fork 4
/
radius.www.authenticate.php
147 lines (136 loc) · 5.05 KB
/
radius.www.authenticate.php
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
<?php
/*********************************************************************
*
* Pure PHP radius class, WWW Authentication file to be required
*
* This file must be required before displaying any protected page.
* This file should be prepended automatically using the
* PHP auto_prepend_file directive in a .htaccess file.
*
*
* LICENCE
*
* Copyright (c) 2008, SysCo systèmes de communication sa
* SysCo (tm) is a trademark of SysCo systèmes de communication sa
* (http://www.sysco.ch/)
* All rights reserved.
*
* This file is part of the Pure PHP radius class
*
* Pure PHP radius class is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Pure PHP radius class is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Pure PHP radius class.
* If not, see <http://www.gnu.org/licenses/>.
*
*
* @author: SysCo/al
* @since CreationDate: 2008-01-06
* @copyright (c) 2008 by SysCo systèmes de communication sa
* @version $LastChangedRevision: 1.0 $
* @version $LastChangedDate: 2008-01-07 $
* @version $LastChangedBy: SysCo/al $
* @link $HeadURL: radius.www.authenticate.php $
* @link http://developer.sysco.ch/php/
* @link developer@sysco.ch
* Language: PHP 4.0.7 or higher
*
*
* Usage
*
* require_once("radius.www.authenticate.php");
* echo "User <strong>".$_SERVER['PHP_AUTH_USER']."</strong> authenticated.";
*
*
* External file needed
*
* radius.class.php
*
*
* External file created
*
* none.
*
*
* Change Log
*
* 2008-01-07 1.0 SysCo/al Initial release
*
*********************************************************************/
require_once('radius.class.php');
function authenticate_and_cache($ip_radius_server, $shared_secret, $username, $password, $timeout = 900)
{
$result = FALSE;
$cache_unique_id = (isset($_SESSION["authentication_unique_id"])?$_SESSION["authentication_unique_id"]:'');
if ('' != $cache_unique_id)
{
$cache_timestamp = $_SESSION[$cache_unique_id."_authentication_timestamp"];
$cache_remote_addr = $_SESSION[$cache_unique_id."_authentication_remote_addr"];
$cache_username = $_SESSION[$cache_unique_id."_authentication_username"];
}
if ((0 == $cache_timestamp) ||
(time() > ($cache_timestamp + $timeout)) ||
($cache_remote_addr != $_SERVER['REMOTE_ADDR']) ||
($cache_username != $username))
{
$radius = new Radius($ip_radius_server, $shared_secret);
$radius->SetDebugMode($php_debug_mode);
$result = $radius->AccessRequest($username, $password);
if (TRUE == $result)
{
if ('' == $cache_unique_id)
{
$cache_unique_id = md5(uniqid(rand(), true));
}
$_SESSION["authentication_unique_id"] = $cache_unique_id;
$_SESSION[$cache_unique_id."_authentication_timestamp"] = time();
$_SESSION[$cache_unique_id."_authentication_remote_addr"] = $_SERVER['REMOTE_ADDR'];
$_SESSION[$cache_unique_id."_authentication_username"] = $username;
}
else
{
$_SESSION["authentication_unique_id"] = '';
}
}
else
{
$_SESSION[$cache_unique_id."_authentication_timestamp"] = time();
$result = TRUE;
}
return $result;
}
// Start session if not already done
if ('' == session_id())
{
session_start();
}
$php_auth_user = $_SERVER['PHP_AUTH_USER'];
$php_auth_pw = $_SERVER['PHP_AUTH_PW'];
$php_auth_realm = (isset($custom_auth_realm)?$custom_auth_realm:$_SERVER["SERVER_NAME"]);
$php_auth_timeout = (isset($custom_auth_timeout)?$custom_auth_timeout:(15*60));
$php_ip_radius_server = (isset($custom_ip_radius_server)?$custom_ip_radius_server:'');
$php_shared_secret = (isset($custom_shared_secret)?$custom_shared_secret:'');
$php_debug_mode = (isset($custom_debug_mode)?(TRUE === $custom_debug_mode):FALSE);
if (('' == $php_auth_user) || (!authenticate_and_cache($php_ip_radius_server, $php_shared_secret, $php_auth_user, $php_auth_pw, $php_auth_timeout)))
{
header("HTTP/1.0 401 Unauthorized");
header("WWW-Authenticate: Basic realm=\"".$php_auth_realm."\"");
echo "<html>";
echo "<head><title>401 Unauthorized access</title></head>";
echo "<body>";
echo "<h1>401 Unauthorized access</h1>";
echo "<br />";
echo "You must login using your username and your password.";
echo "</body>";
echo "</html>";
exit;
}
?>