-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLDAP.php
More file actions
324 lines (286 loc) · 9.5 KB
/
LDAP.php
File metadata and controls
324 lines (286 loc) · 9.5 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* The LDAP class handles all communication with LDAP, formatting
* responses and handling business logic (e.g. a user can have only
* one pass at a time, a pass can belong to only one user).
*/
class LDAP
{
/**
* LDAP connection
* @var resource
*/
private $ldap;
/**
* Configuration object
* @var array
*/
private $config;
/**
* Response code constants
*/
const ERROR_USER_NOT_FOUND = 'user_not_found';
const ERROR_DOUBLE_PASS = 'user_already_has_a_pass';
const ERROR_PASS_EXISTS = 'pass_exists';
/**
* Setup LDAP class, does not connect or login
* @param array $config LDAP-configuration
*/
public function __construct($config)
{
$this->config = $config;
}
/**
* Connect to LDAP using the configuration provided
* @return boolean true if connected
*/
public function connect()
{
$this->ldap = @ldap_connect($this->config['host']);
return ($this->ldap != false);
}
/**
* Login to LDAP using the configuration provided
* @return boolean true if the login is successful
*/
public function login()
{
return @ldap_bind($this->ldap, $this->config['username'], $this->config['password']);
}
/**
* Decommission the ldap-connection
*/
public function __destruct()
{
@ldap_close($this->ldap);
}
/**
* Find all users which have passes in LDAP
* @return array an user information, containing uid, name, pass and access
*/
public function getAllUsers()
{
// Find all cards
$search = ldap_search($this->ldap, $this->config['base_dn'], '(&(objectClass=device)(cn=ovchipkaart))', ['dn']);
$cards = ldap_get_entries($this->ldap, $search);
// Strip off initial 'count' entry
array_shift($cards);
return array_map(function($card){
// Construct DN of the owner of the card
$owner_dn = str_replace('cn=ovchipkaart,', '', $card['dn']);
// Get the owner details
$owner_ldap = ldap_read($this->ldap, $owner_dn, '(objectclass=inetOrgPerson)', ['uid', 'objectclass', 'cn']);
$owner = ldap_get_entries($this->ldap, $owner_ldap);
// Construct result
return [
'uid' => $owner[0]['uid'][0],
'name' => $owner[0]['cn'][0],
'pass' => true,
'access' => in_array('gosaIntranetAccount', $owner[0]['objectclass'])
];
}, $cards);
}
/**
* Find the pass and access data of a specific user
* @param string $uid the user id to retrieve
* @return array a users information, containing uid, name, pass and access
*/
public function getUser($uid)
{
$user = $this->findUser($uid);
if (!$user) {
return null;
}
$pass = $this->findPass($uid);
if (!$pass) {
return null;
}
return [
'uid' => $user['uid'][0],
'name' => $user['cn'][0],
'pass' => true,
'access' => in_array('gosaIntranetAccount', $user['objectclass'])
];
}
/**
* Add the access flag to a user
* @param string $uid the user ID to update
* @return boolean
*/
public function grantAccess($uid)
{
// Find the user
$user = $this->findUser($uid);
if (!$user) {
return false;
}
// Determine if we need to update
if (in_array('gosaIntranetAccount', $user['objectclass'])) {
return;
}
// Add flag to user
$patch = ['objectclass' => ['gosaIntranetAccount']];
ldap_mod_add($this->ldap, $user['dn'], $patch);
return true;
}
/**
* Remove the access flag of a user
* @param string $uid user ID to update
* @return boolean
*/
public function denyAccess($uid)
{
// Find the user
$user = $this->findUser($uid);
if (!$user) {
return false;
}
// Determine if we need to update
if (! in_array('gosaIntranetAccount', $user['objectclass'])) {
return;
}
// Remove flag from user
$patch = ['objectclass' => ['gosaIntranetAccount']];
ldap_mod_del($this->ldap, $user['dn'], $patch);
return true;
}
/**
* Add a new pass to a user
* @param string $uid the userID to add to
* @param string $passNumber the full pass number
* @return boolean true if succesful, or a error constant otherwise
*/
public function addPass($uid, $passNumber)
{
// User must exist
$user = $this->findUser($uid);
if (!$user) {
return self::ERROR_USER_NOT_FOUND;
}
// User must not already have a pass
$existing_pass = $this->findPass($uid);
if ($existing_pass) {
return self::ERROR_DOUBLE_PASS;
}
// Pass may not exist in the system for another user
if ($this->passExists($passNumber)){
return self::ERROR_PASS_EXISTS;
}
// Build new entry
$dn = 'cn=ovchipkaart,' . $user['dn'];
$entry = [
'objectClass' => 'device',
'cn' => 'ovchipkaart',
'serialNumber' => $passNumber
];
ldap_add($this->ldap, $dn, $entry);
return true;
}
/**
* Delete the pass of a user
* @param string $uid the user ID to remove from
* @return boolean
*/
public function removePass($uid)
{
$pass = $this->findPass($uid);
if (!$pass) {
return false;
}
ldap_delete($this->ldap, $pass['dn']);
return true;
}
/**
* Return basic information on a pass attempt to open the door
* @param string $passNumber the full card ID
* @return array containing three members:
* access (boolean) true if the door should open, false otherwise
* username (string) if known, the username of owner of the pass, null otherwise
* reason (string) if known, the reason for acceptance or denial, null otherwise
*/
public function infoOnPassAttempt($passNumber)
{
$info = ['access' => false, 'username' => null, 'reason' => null];
// Find the user
$user = $this->findUserByPass($passNumber);
if ($user) {
// Log username when known
$info['username'] = $user['uid'][0];
}
else {
// Deny access if we can't find the pass or its owner
$info['access'] = false;
$info['reason'] = 'pass_unknown';
return $info;
}
// Grant access if the user has the right flag set
if (in_array('gosaIntranetAccount', $user['objectclass'])) {
$info['access'] = true;
return $info;
}
else {
$info['access'] = false;
$info['reason'] = 'user_not_authorised';
return $info;
}
}
/**
* Find a LDAP user by its uid
* @param string $uid the user id to find
* @return array details of the user, or null if it doesn't exist
*/
private function findUser($uid)
{
$search = ldap_search($this->ldap, $this->config['base_dn'], "(&(objectClass=inetOrgPerson)(uid={$uid}))", ['gosaIntranetAccount']);
if (ldap_count_entries($this->ldap, $search) !== 1) {
return null;
}
return ldap_get_entries($this->ldap, $search)[0];
}
/**
* Find the pass based on a user id
* @param strin $uid the user ID
* @return array details of the pass, or null if it doesn't exist
*/
private function findPass($uid)
{
$user = $this->findUser($uid);
if ($user === null) {
return null;
}
$pass = ldap_search($this->ldap, $user['dn'], "(&(objectClass=device)(cn=ovchipkaart))");
if (ldap_count_entries($this->ldap, $pass) !== 1) {
return null;
}
return ldap_get_entries($this->ldap, $pass)[0];
}
/**
* Find a user by its pass
* @param string $passNumber full pass number
* @return array details of the user
*/
private function findUserByPass($passNumber)
{
// Find the card
$search = ldap_search($this->ldap, $this->config['base_dn'], "(&(objectClass=device)(cn=ovchipkaart)(serialNumber=$passNumber))");
if (ldap_count_entries($this->ldap, $search) !== 1) {
return null;
}
$card = ldap_get_entries($this->ldap, $search)[0];
// Construct DN of the owner of the card
$owner_dn = str_replace('cn=ovchipkaart,', '', $card['dn']);
$owner_ldap = ldap_read($this->ldap, $owner_dn, '(objectclass=inetOrgPerson)', ['uid', 'objectclass', 'cn']);
$owner = ldap_get_entries($this->ldap, $owner_ldap);
return $owner[0];
}
/**
* Determine whether a passNumber has been registered in LDAP
* @param string $passNumber full pass number
* @return boolean true if the pass exists
*/
private function passExists($passNumber)
{
$search = ldap_search($this->ldap, $this->config['base_dn'], "(&(objectClass=device)(cn=ovchipkaart)(serialNumber=$passNumber))");
return (ldap_count_entries($this->ldap, $search) > 0);
}
}