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
|
<?php
if (preg_match("/\/includes\//", $PHP_SELF)) {
die ("You can't 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.
/***************************** Config *******************************/
// Set some global config variables about your system.
// Next three are NOT yet implemented for LDAP
$user_can_update_password = false;
$admin_can_add_user = false;
$admin_can_delete_user = false;
//------ LDAP General Server Settings ------//
//
// Name or address of the LDAP server
// For SSL/TLS use 'ldaps://localhost'
$ldap_server = 'localhost';
// Port LDAP listens on (default 389)
$ldap_port = '389';
// base DN to search for users
$ldap_base_dn = 'ou=people,dc=company,dc=com';
// The ldap attribute used to find a user (login).
// E.g., if you use cn, your login might be "Jane Smith"
// if you use uid, your login might be "jsmith"
$ldap_login_attr = 'uid';
// Account used to bind to the server and search for information.
// This user must have the correct rights to perform search.
// If left empty the search will be made in anonymous.
//
// *** We do NOT recommend storing the root LDAP account info here ***
$ldap_admin_dn = ''; // user DN
$ldap_admin_pwd = ''; // user password
//------ Admin Group Settings ------//
//
// A group name (complete DN) to find users with admin rights
$ldap_admin_group_name = 'cn=webcal_admin,ou=group,dc=company,dc=com';
// What type of group do we want (posixgroup, groupofnames, groupofuniquenames)
$ldap_admin_group_type = 'posixgroup';
// The LDAP attribute used to store member of a group
$ldap_admin_group_attr = 'memberuid';
//------ LDAP Search Settings ------//
//
// LDAP filter to find a user list.
$ldap_user_filter = '(objectclass=person)';
// Attributes to fetch from LDAP and corresponding user variables in the
// application. Do change according to your LDAP Schema
$ldap_user_attr = array(
// LDAP attribute //WebCalendar variable
'uid', //login
'sn', //lastname
'givenname', //firstname
'cn', //fullname
'mail' //email
);
/*************************** End Config *****************************/
// 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
// $dn - complete dn for the user (must be given by ref )
// return:
// TRUE if the user is found, FALSE in other case
function user_search_dn ( $login ,$dn ) {
global $error, $ds, $ldap_base_dn, $ldap_login_attr, $ldap_user_attr;
$ret = false;
if ($r = connect_and_bind()) {
$sr = @ldap_search ( $ds, $ldap_base_dn, "($ldap_login_attr=$login)", $ldap_user_attr );
if (!$sr) {
$error = 'Error searching LDAP server: ' . ldap_error();
} else {
$info = @ldap_get_entries ( $ds, $sr );
if ( $info['count'] != 1 ) {
$error = 'Invalid login';
} else {
$ret = true;
$dn = $info[0]['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_server, $ldap_port, $ldap_base_dn, $ldap_login_attr;
global $ldap_admin_dn,$ldap_admin_pwd;
$ret = false;
$ds = @ldap_connect ( $ldap_server, $ldap_port );
if ( $ds ) {
if ( user_search_dn ( $login, &$dn) ) {
$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 $error, $ds, $ldap_base_dn, $ldap_login_attr, $ldap_user_attr;
global $PUBLIC_ACCESS_FULLNAME, $NONUSER_PREFIX;
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_attr );
if (!$sr) {
$error = 'Error searching LDAP server: ' . ldap_error();
} 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 );
}
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")
function user_add_user ( $user, $password, $firstname, $lastname, $email, $admin ) {
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?
function user_update_user ( $user, $firstname, $lastname, $email, $admin ) {
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 system.
// Once this does get implemented, be sure to delete the user from
// various WebCalendar tables (see user.php user_delete_user function).
// params:
// $user - user to delete
function user_delete_user ( $user ) {
$error = 'Not yet supported';
return false;
}
// Get a list of users and return info in an array.
// returns: array of users
function user_get_users () {
global $error, $ds, $ldap_base_dn, $ldap_user_attr, $ldap_user_filter;
global $public_access, $PUBLIC_ACCESS_FULLNAME;
$Admins = get_admins();
$count = 0;
$ret = array ();
if ( $public_access == 'Y' )
$ret[$count++] = array (
'cal_login' => '__public__',
'cal_lastname' => '',
'cal_firstname' => '',
'cal_is_admin' => 'N',
'cal_email' => '',
'cal_password' => '',
'cal_fullname' => $PUBLIC_ACCESS_FULLNAME );
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();
} else {
if ( (float)substr(PHP_VERSION,0,3) >= 4.2 ) ldap_sort ( $ds, $sr, $ldap_user_attr[3]);
$info = @ldap_get_entries( $ds, $sr );
for ( $i = 0; $i < $info['count']; $i++ ) {
$ret[$count++] = array (
'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 );
}
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 $error, $ds, $ldap_user_attr, $cached_admins;
global $ldap_admin_group_name,$ldap_admin_group_attr,$ldap_admin_group_type;
if ( ! empty ( $cached_admins ) ) return $cached_admins;
$cached_admins = array ();
if ($r = connect_and_bind()) {
$search_filter = "($ldap_admin_group_attr=*)";
$sr = @ldap_search ( $ds, $ldap_admin_group_name, $search_filter, $ldap_user_attr );
if (!$sr) {
$error = 'Error searching LDAP server: ' . ldap_error();
} 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) = split (',', $dn, 2);
list ($trash,$user) = split ('=', $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_server, $ldap_port, $ldap_admin_dn, $ldap_admin_pwd;
$ret = false;
$ds = @ldap_connect ( $ldap_server, $ldap_port );
if ( $ds ) {
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;
}
?>
|