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
|
<?php
/*
* $Id: ActiveDirectory.php 501 2013-07-11 17:44:37Z imooreyahoo@gmail.com $
* Experimental!
*/
class phpvbAuthActiveDirectory implements phpvbAuth {
var $capabilities = array(
'canChangePassword' => false,
'canLogout' => true
);
var $config = array(
'host' => '127.0.0.1',
'admin_group' => null,
'adminUser' => null,
'user_group' => null,
'container' => 'CN=Users',
'domain' => 'internal.local',
'filter' => '(&(objectclass=User)(objectCategory=Person))'
);
/**
* Constructor
* @param array $userConfig - user configuration for this module
*/
function phpvbAuthActiveDirectory($userConfig = null) {
// Merge user config
if($userConfig) {
$this->config = array_merge($this->config,$userConfig);
}
}
/**
* Test log in and set $_SESSION vars
* @param string $username
* @param string $password
* @see phpvbAuth::login()
*/
function login($username, $password)
{
global $_SESSION;
/*
* Check for LDAP functionality and provide some direction
*/
if(!function_exists('ldap_connect')) {
$ex = 'LDAP support is not enabled in your PHP configuration.';
if(strtolower(substr(PHP_OS, 0, 3)) == 'win') {
ob_start();
phpinfo(INFO_GENERAL);
$phpinfo = ob_get_contents();
ob_end_clean();
preg_match('/Loaded Configuration File <\/td><td.*?>(.*?)\s*</', $phpinfo, $phpinfo);
$ex .= ' You probably just need to uncomment the line ;extension=php_ldap.dll in php.ini'.
(count($phpinfo) > 1 ? ' (' .trim($phpinfo[1]).')' : '') . ' by removing the ";" and restart your web server.';
} else if(strtolower(substr(PHP_OS, 0, 5)) == 'Linux') {
$ex .= ' You probably need to install the php5-ldap (or similar depending on your distribution) package and restart your web server.';
}
throw new Exception($ex);
}
$_SESSION['valid'] = false;
// Connect to server
if(!($auth = ldap_connect($this->config['host']))) {
throw new Exception('Active Directory error ('.ldap_errno($auth).') ' . ldap_error($auth));
}
// Set relevant LDAP options
ldap_set_option($auth,LDAP_OPT_PROTOCOL_VERSION, 3);
// Main login /bind
if(!($bind = @ldap_bind($auth, $username . "@" .$this->config['domain'], $password))) {
if(ldap_errno($auth) == 49) return false;
throw new Exception('Active Directory error ('.ldap_errno($auth).') ' . ldap_error($auth));
}
// Get user information from AD
////////////////////////////////////
// Set filter and sanitize username before sending it to AD
$filter = "(sAMAccountName=" .
str_replace(array(',','=','+','<','>',';','\\','"','#','(',')','*',chr(0)), '', $username) . ")";
if($this->config['filter'] && false) {
$filter = '(&'. $this->config['filter'] .' ('. $filter .'))';
}
$result = @ldap_search($auth,
$this->config['container'] . ',DC=' . join(',DC=', explode('.', $this->config['domain'])),
$filter, array("memberof","useraccountcontrol"));
if(!result) throw new Exception ("Unable to search Active Directory server: " . ldap_error($auth));
@list($entries) = @ldap_get_entries($auth, $result);
@ldap_unbind($auth);
if(!$entries) {
throw new Exception("Permission denied");
}
// Check for disabled user
if((intval($entries['useraccountcontrol'][0]) & 2)) {
throw new Exception('This account is disabled in Active Directory.');
}
// check for valid admin group
if($this->config['admin_group']) {
foreach($entries['memberof'] as $group) {
list($group) = explode(',', $group);
if(strtolower($group) == strtolower('cn='.$this->config['admin_group'])) {
$_SESSION['admin'] = $_SESSION['valid'] = true;
break;
}
}
}
// Admin user explicitly set?
if(!$_SESSION['admin'] && $this->config['adminUser']) {
$_SESSION['admin'] = (strtolower($this->config['adminUser']) == strtolower($username));
// Admin is ok
$_SESSION['valid'] = ($_SESSION['admin'] || $_SESSION['valid']);
}
// check for valid user group
if($this->config['user_group'] && !$_SESSION['valid']) {
foreach($entries['memberof'] as $group) {
list($group) = explode(',', $group);
if(strtolower($group) == strtolower('cn='.$this->config['user_group'])) {
$_SESSION['valid'] = true;
break;
}
}
} else {
$_SESSION['valid'] = true;
}
if(!$_SESSION['valid'])
throw new Exception("Permission denied");
// Admin user explicitly set?
if(!$_SESSION['admin'] && $this->config['adminUser']) {
$_SESSION['admin'] = (strtolower($this->config['adminUser']) == strtolower($username));
}
// No admin information specified makes everyone an admin
if(!$this->config['adminUser'] && !$this->config['admin_group'])
$_SESSION['admin'] = true;
// user has permission. establish session variables
$_SESSION['user'] = $username;
$_SESSION['authCheckHeartbeat'] = time();
return true;
}
function heartbeat($vbox)
{
global $_SESSION;
$_SESSION['valid'] = true;
$_SESSION['authCheckHeartbeat'] = time();
}
function changePassword($old, $new)
{
}
function logout(&$response)
{
global $_SESSION;
if(function_exists('session_destroy')) session_destroy();
else unset($_SESSION['valid']);
$response['data']['result'] = 1;
}
function listUsers()
{
}
function updateUser($vboxRequest, $skipExistCheck)
{
}
function deleteUser($user)
{
}
}
|