forked from craigk5n/webcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-ldap.php
More file actions
490 lines (450 loc) · 15.5 KB
/
Copy pathuser-ldap.php
File metadata and controls
490 lines (450 loc) · 15.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
defined ( '_ISVALID' ) or die ( 'You cannot access this file directly!' );
/*
* LDAP user functions.
* This file is intended to be used instead of the standard user.php file.
* I have not tested this yet (I do not have an LDAP server running yet),
* so please provide feedback.
*
* This file contains all the functions for getting information about users.
* So, if you want to use an authentication scheme other than the webcal_user
* table, you can just create a new version of each function found below.
*
* Note: this application assumes that usernames (logins) are unique.
*
* Note #2: If you are using HTTP-based authentication, then you still need
* these functions and you will still need to add users to webcal_user.
*/
include_once 'auth-settings.php';
// Next two features are NOT yet implemented for LDAP
$user_can_update_password = false;
$admin_can_add_user = false;
// Convert group name to lower case to prevent problems
$ldap_admin_group_attr = strtolower($ldap_admin_group_attr);
$ldap_admin_group_type = strtolower($ldap_admin_group_type);
// Function to search the dn of a given user the error message will
// be placed in $error.
// params:
// $login - user login
// return:
// $dn - complete dn for the user
// TRUE if the user is found, FALSE in other case
function user_search_dn ( $login ) {
global $ds, $error, $ldap_base_dn, $ldap_login_attr,
$ldap_user_attr, $ldap_user_filter;
$ret = false;
if ($r = connect_and_bind ()) {
$sr = @ldap_search ( $ds, $ldap_base_dn,
"(&($ldap_login_attr=$login)$ldap_user_filter )", $ldap_user_attr );
if (!$sr) {
$error = 'Error searching LDAP server: ' . ldap_error( $ds );
} else {
$info = @ldap_get_entries ( $ds, $sr );
if ( $info['count'] != 1 ) {
$error = 'Invalid login';
} else {
$dn = $info[0]['dn'];
$ret = $dn;
}
@ldap_free_result ( $sr );
}
@ldap_close ( $ds );
}
return $ret;
}
// Check to see if a given login/password is valid. If invalid,
// the error message will be placed in $error.
// params:
// $login - user login
// $password - user password
// returns: true or false
function user_valid_login ( $login, $password ) {
global $error, $ldap_admin_dn, $ldap_admin_pwd,
$ldap_base_dn, $ldap_login_attr, $ldap_port, $ldap_server,
$ldap_start_tls, $ldap_version, $set_ldap_version;
if ( ! function_exists ( "ldap_connect" ) ) {
die_miserable_death ( "Your installation of PHP does not support LDAP" );
}
$ret = false;
$ds = @ldap_connect ( $ldap_server, $ldap_port );
if ( $ds ) {
if ($set_ldap_version || $ldap_start_tls)
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $ldap_version);
if ($ldap_start_tls) {
if (!ldap_start_tls($ds)) {
$error = 'Could not start TLS for LDAP connection';
return $ret;
}
}
if ( ($dn = user_search_dn ( $login )) ) {
$r = @ldap_bind ( $ds, $dn, $password );
if (!$r) {
$error = 'Invalid login';
//$error .= ': incorrect password'; // uncomment for debugging
} else {
$ret = true;
}
} else {
$error = 'Invalid login';
//$error .= ': no such user'; // uncomment for debugging
}
@ldap_close ( $ds );
} else {
$error = 'Error connecting to LDAP server';
}
return $ret;
}
// TODO: implement this function properly for LDAP.
// Check to see if a given login/crypted password is valid. If invalid,
// the error message will be placed in $error.
// params:
// $login - user login
// $crypt_password - crypted user password
// returns: true or false
function user_valid_crypt ( $login, $crypt_password ) {
return true;
}
// Load info about a user (first name, last name, admin) and set globally.
// params:
// $user - user login
// $prefix - variable prefix to use
function user_load_variables ( $login, $prefix ) {
global $cached_user_var, $ds, $error, $ldap_base_dn, $ldap_login_attr,
$ldap_user_attr, $ldap_user_filter, $NONUSER_PREFIX, $PUBLIC_ACCESS_FULLNAME;
if ( ! empty ( $cached_user_var[$login][$prefix] ) )
return $cached_user_var[$login][$prefix];
$cached_user_var = [];
if ($NONUSER_PREFIX && substr ($login, 0, strlen ($NONUSER_PREFIX) ) == $NONUSER_PREFIX ) {
nonuser_load_variables ( $login, $prefix );
return true;
}
if ( $login == '__public__' ) {
$GLOBALS[$prefix . 'login'] = $login;
$GLOBALS[$prefix . 'firstname'] = '';
$GLOBALS[$prefix . 'lastname'] = '';
$GLOBALS[$prefix . 'is_admin'] = 'N';
$GLOBALS[$prefix . 'email'] = '';
$GLOBALS[$prefix . 'fullname'] = $PUBLIC_ACCESS_FULLNAME;
$GLOBALS[$prefix . 'password'] = '';
return true;
}
$ret = false;
if ($r = connect_and_bind ()) {
$sr = @ldap_search ( $ds, $ldap_base_dn,
"(&($ldap_login_attr=$login)$ldap_user_filter )", $ldap_user_attr );
if (!$sr) {
$error = 'Error searching LDAP server: ' . ldap_error( $ds );
} else {
$info = @ldap_get_entries ( $ds, $sr );
if ( $info['count'] != 1 ) {
$error = 'Invalid login';
} else {
$GLOBALS[$prefix . 'login'] = $login;
$GLOBALS[$prefix . 'firstname'] = $info[0][$ldap_user_attr[2]][0];
$GLOBALS[$prefix . 'lastname'] = $info[0][$ldap_user_attr[1]][0];
$GLOBALS[$prefix . 'email'] = $info[0][$ldap_user_attr[4]][0];
$GLOBALS[$prefix . 'fullname'] = $info[0][$ldap_user_attr[3]][0];
$GLOBALS[$prefix . 'is_admin'] = user_is_admin ($login,get_admins ());
$ret = true;
}
@ldap_free_result ( $sr );
}
@ldap_close ( $ds );
}
//save these results
$cached_user_var[$login][$prefix] = $ret;
return $ret;
}
// Add a new user.
// params:
// $user - user login
// $password - user password
// $firstname - first name
// $lastname - last name
// $email - email address
// $admin - is admin? ("Y" or "N")
// $enabled - account enabled? ("Y" or "N")
function user_add_user ( $user, $password, $firstname, $lastname, $email, $admin, $enabled = 'Y' ) {
global $error;
$error = 'Not yet supported.';
return false;
}
// Update a user
// params:
// $user - user login
// $firstname - first name
// $lastname - last name
// $email - email address
// $admin - is admin?
// $enabled - account enabled? ("Y" or "N")
function user_update_user ( $user, $firstname, $lastname, $email, $admin, $enabled = 'Y' ) {
global $error;
$error = 'Not yet supported.';
return false;
}
// Update user password
// params:
// $user - user login
// $password - last name
function user_update_user_password ( $user, $password ) {
global $error;
$error = 'Not yet supported';
return false;
}
/**
* Delete a user from the webcalendar tables. (NOT from LDAP)
*
* This will also delete any of the user's events in the system that have
* no other participants. Any layers that point to this user
* will be deleted. Any views that include this user will be updated.
*
* @param string $user User to delete
*/
function user_delete_user ( $user ) {
// Get event ids for all events this user is a participant
$events = get_users_event_ids ( $user );
// Now count number of participants in each event...
// If just 1, then save id to be deleted
$delete_em = [];
for ( $i = 0; $i < count ( $events ); $i++ ) {
$res = dbi_execute ( 'SELECT COUNT( * )
FROM webcal_entry_user
WHERE cal_id = ?', [$events[$i]] );
if ( $res ) {
if ( $row = dbi_fetch_row ( $res ) ) {
if ( $row[0] == 1 )
$delete_em[] = $events[$i];
}
dbi_free_result ( $res );
}
}
// Now delete events that were just for this user
for ( $i = 0; $i < count ( $delete_em ); $i++ ) {
dbi_execute ( 'DELETE FROM webcal_entry_repeats WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_entry_repeats_not WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_entry_log WHERE cal_entry_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_import_data WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_site_extras WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_entry_ext_user WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_reminders WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_blob WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_entry WHERE cal_id = ?',
[$delete_em[$i]] );
}
// Delete user participation from events
dbi_execute ( 'DELETE FROM webcal_entry_user WHERE cal_login = ?',
[$user] );
// Delete preferences
dbi_execute ( 'DELETE FROM webcal_user_pref WHERE cal_login = ?',
[$user] );
// Delete from groups
dbi_execute ( 'DELETE FROM webcal_group_user WHERE cal_login = ?',
[$user] );
// Delete bosses & assistants
dbi_execute ( 'DELETE FROM webcal_asst WHERE cal_boss = ?',
[$user] );
dbi_execute ( 'DELETE FROM webcal_asst WHERE cal_assistant = ?',
[$user] );
// Delete user's views
$delete_em = [];
$res = dbi_execute ( 'SELECT cal_view_id FROM webcal_view WHERE cal_owner = ?',
[$user] );
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$delete_em[] = $row[0];
}
dbi_free_result ( $res );
}
for ( $i = 0; $i < count ( $delete_em ); $i++ ) {
dbi_execute ( 'DELETE FROM webcal_view_user WHERE cal_view_id = ?',
[$delete_em[$i]] );
}
dbi_execute ( 'DELETE FROM webcal_view WHERE cal_owner = ?',
[$user] );
//Delete them from any other user's views
dbi_execute ( 'DELETE FROM webcal_view_user WHERE cal_login = ?',
[$user] );
// Delete layers
dbi_execute ( 'DELETE FROM webcal_user_layers WHERE cal_login = ?',
[$user] );
// Delete any layers other users may have that point to this user.
dbi_execute ( 'DELETE FROM webcal_user_layers WHERE cal_layeruser = ?',
[$user] );
// Delete user
dbi_execute ( 'DELETE FROM webcal_user WHERE cal_login = ?',
[$user] );
// Delete function access
dbi_execute ( 'DELETE FROM webcal_access_function WHERE cal_login = ?',
[$user] );
// Delete user access
dbi_execute ( 'DELETE FROM webcal_access_user WHERE cal_login = ?',
[$user] );
dbi_execute ( 'DELETE FROM webcal_access_user WHERE cal_other_user = ?',
[$user] );
// Delete user's categories
dbi_execute ( 'DELETE FROM webcal_categories WHERE cat_owner = ?',
[$user] );
dbi_execute ( 'DELETE FROM webcal_entry_categories WHERE cat_owner = ?',
[$user] );
// Delete user's reports
$delete_em = [];
$res = dbi_execute ( 'SELECT cal_report_id FROM webcal_report WHERE cal_login = ?',
[$user] );
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$delete_em[] = $row[0];
}
dbi_free_result ( $res );
}
for ( $i = 0; $i < count ( $delete_em ); $i++ ) {
dbi_execute ( 'DELETE FROM webcal_report_template WHERE cal_report_id = ?',
[$delete_em[$i]] );
}
dbi_execute ( 'DELETE FROM webcal_report WHERE cal_login = ?',
[$user] );
//not sure about this one???
dbi_execute ( 'DELETE FROM webcal_report WHERE cal_user = ?',
[$user] );
// Delete user templates
dbi_execute ( 'DELETE FROM webcal_user_template WHERE cal_login = ?',
[$user] );
}
// Get a list of users and return info in an array.
// returns: array of users
function user_get_users ( $publicOnly=false ) {
global $ds, $error, $ldap_base_dn, $ldap_user_attr,
$ldap_user_filter, $PUBLIC_ACCESS_FULLNAM, $PUBLIC_ACCESS;
$Admins = get_admins ();
$count = 0;
$ret = [];
if ( $PUBLIC_ACCESS == 'Y' )
$ret[$count++] = [
'cal_login' => '__public__',
'cal_lastname' => '',
'cal_firstname' => '',
'cal_is_admin' => 'N',
'cal_email' => '',
'cal_password' => '',
'cal_fullname' => $PUBLIC_ACCESS_FULLNAME];
if ( $publicOnly ) return $ret;
if ($r = connect_and_bind ()) {
$sr = @ldap_search ( $ds, $ldap_base_dn, $ldap_user_filter, $ldap_user_attr );
if (!$sr) {
$error = 'Error searching LDAP server: ' . ldap_error( $ds );
} else {
// ldap_sort() was removed in PHP 8.0; results are sorted by usort() below.
$info = @ldap_get_entries( $ds, $sr );
for ( $i = 0; $i < $info['count']; $i++ ) {
$ret[$count++] = [
'cal_login' => $info[$i][$ldap_user_attr[0]][0],
'cal_lastname' => $info[$i][$ldap_user_attr[1]][0],
'cal_firstname' => $info[$i][$ldap_user_attr[2]][0],
'cal_email' => $info[$i][$ldap_user_attr[4]][0],
'cal_is_admin' => user_is_admin ($info[$i][$ldap_user_attr[0]][0],$Admins),
'cal_fullname' => $info[$i][$ldap_user_attr[3]][0]];
}
@ldap_free_result($sr);
}
@ldap_close ( $ds );
}
usort ( $ret, 'sort_users');
return $ret;
}
// Test if a user is an admin, that is: if the user is a member of a special
// group in the LDAP Server
// params:
// $values - the login name
// returns: Y if user is admin, N if not
function user_is_admin ($values,$Admins) {
if ( ! $Admins ) {
return 'N';
} else if (in_array ($values, $Admins)) {
return 'Y';
} else {
return 'N';
}
}
// Searches $ldap_admin_group_name and returns an array of the group members.
// Do this search only once per request.
// returns: array of admins
function get_admins () {
global $cached_admins, $ds, $error, $ldap_admin_group_attr,
$ldap_admin_group_name, $ldap_admin_group_type;
if ( ! empty ( $cached_admins ) ) return $cached_admins;
$cached_admins = [];
if ($r = connect_and_bind ()) {
$search_filter = "($ldap_admin_group_attr=*)";
$sr = @ldap_search ( $ds, $ldap_admin_group_name, $search_filter, [$ldap_admin_group_attr] );
if (!$sr) {
$error = 'Error searching LDAP server: ' . ldap_error( $ds );
} else {
$admins = ldap_get_entries( $ds, $sr );
for( $x = 0; $x < $admins[0][$ldap_admin_group_attr]['count']; $x ++ ) {
if ($ldap_admin_group_type != 'posixgroup') {
$cached_admins[] = stripdn($admins[0][$ldap_admin_group_attr][$x]);
} else {
$cached_admins[] = $admins[0][$ldap_admin_group_attr][$x];
}
}
@ldap_free_result($sr);
}
@ldap_close ( $ds );
}
return $cached_admins;
}
// Strip everything but the username (uid) from a dn.
// params:
// $dn - the dn you want to strip the uid from.
// returns: string - userid
//
// ex: stripdn(uid=jeffh,ou=people,dc=example,dc=com) returns jeffh
function stripdn( $dn ) {
list( $uid, $trash ) = explode( ',', $dn, 2 );
list( $trash, $user ) = explode( '=', $uid );
return $user;
}
// Connects and binds to the LDAP server
// Tries to connect as $ldap_admin_dn if we set it.
// returns: bind result or false
function connect_and_bind () {
global $ds, $error, $ldap_admin_dn, $ldap_admin_pwd, $ldap_port,
$ldap_server, $ldap_start_tls, $ldap_version, $set_ldap_version;
if ( ! function_exists ( 'ldap_connect' ) ) {
die_miserable_death ( 'Your installation of PHP does not support LDAP' );
}
$ret = false;
$ds = @ldap_connect ( $ldap_server, $ldap_port );
if ( $ds ) {
if ($set_ldap_version || $ldap_start_tls)
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $ldap_version);
if ($ldap_start_tls) {
if (!ldap_start_tls($ds)) {
$error = 'Could not start TLS for LDAP connection';
return $ret;
}
}
if ( $ldap_admin_dn != '') {
$r = @ldap_bind ( $ds, $ldap_admin_dn, $ldap_admin_pwd );
} else {
$r = @ldap_bind ( $ds );
}
if (!$r) {
$error = 'Invalid Admin login for LDAP Server';
} else {
$ret = $r;
}
} else {
$error = 'Error connecting to LDAP server';
$ret = false;
}
return $ret;
}
?>