2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2008-03-15 18:27:36 +01:00
|
|
|
/**
|
|
|
|
* eGroupWare API - LDAP Authentication
|
2009-04-09 08:41:41 +02:00
|
|
|
*
|
2008-03-15 18:27:36 +01:00
|
|
|
* @link http://www.egroupware.org
|
2010-01-28 05:22:37 +01:00
|
|
|
* @author Ralf Becker <ralfbecker@outdoor-training.de>
|
2008-03-15 18:27:36 +01:00
|
|
|
* @author Lars Kneschke <lkneschke@linux-at-work.de>
|
2009-04-09 08:41:41 +02:00
|
|
|
* @author Joseph Engo <jengo@phpgroupware.org>
|
2008-03-15 18:27:36 +01:00
|
|
|
* Copyright (C) 2000, 2001 Joseph Engo
|
|
|
|
* Copyright (C) 2002, 2003 Lars Kneschke
|
2010-01-28 05:22:37 +01:00
|
|
|
* @license http://opensource.org/licenses/lgpl-license.php LGPL - GNU Lesser General Public License
|
2008-03-15 18:27:36 +01:00
|
|
|
* @package api
|
|
|
|
* @subpackage authentication
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
2001-04-10 09:58:19 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
/**
|
|
|
|
* Authentication agains a LDAP Server
|
|
|
|
*/
|
2010-01-28 05:22:37 +01:00
|
|
|
class auth_ldap implements auth_backend
|
2008-03-15 18:27:36 +01:00
|
|
|
{
|
|
|
|
var $previous_login = -1;
|
2010-11-16 09:54:09 +01:00
|
|
|
/**
|
|
|
|
* Switch this on to get messages in Apache error_log, why authtication fails
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $debug = false;
|
2005-08-27 14:19:35 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
/**
|
|
|
|
* authentication against LDAP
|
|
|
|
*
|
|
|
|
* @param string $username username of account to authenticate
|
|
|
|
* @param string $passwd corresponding password
|
|
|
|
* @return boolean true if successful authenticated, false otherwise
|
|
|
|
*/
|
2010-01-28 05:22:37 +01:00
|
|
|
function authenticate($username, $passwd, $passwd_type='text')
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
// allow non-ascii in username & password
|
2010-01-28 05:22:37 +01:00
|
|
|
$username = translation::convert($username,translation::charset(),'utf-8');
|
|
|
|
$passwd = translation::convert($passwd,translation::charset(),'utf-8');
|
2001-06-03 19:58:12 +02:00
|
|
|
|
2010-01-28 05:22:37 +01:00
|
|
|
if(!$ldap = common::ldapConnect())
|
2001-04-10 09:58:19 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
$GLOBALS['egw']->log->message('F-Abort, Failed connecting to LDAP server for authenication, execution stopped');
|
|
|
|
$GLOBALS['egw']->log->commit();
|
|
|
|
return False;
|
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
/* Login with the LDAP Admin. User to find the User DN. */
|
|
|
|
if(!@ldap_bind($ldap, $GLOBALS['egw_info']['server']['ldap_root_dn'], $GLOBALS['egw_info']['server']['ldap_root_pw']))
|
|
|
|
{
|
2010-11-16 09:54:09 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."('$username',\$password) can NOT bind with ldap_root_dn to search!");
|
2008-03-15 18:27:36 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
/* find the dn for this uid, the uid is not always in the dn */
|
2011-11-15 13:43:59 +01:00
|
|
|
$attributes = array('uid','dn','givenName','sn','mail','uidNumber','shadowExpire','homeDirectory');
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
$filter = $GLOBALS['egw_info']['server']['ldap_search_filter'] ? $GLOBALS['egw_info']['server']['ldap_search_filter'] : '(uid=%user)';
|
|
|
|
$filter = str_replace(array('%user','%domain'),array(ldap::quote($username),$GLOBALS['egw_info']['user']['domain']),$filter);
|
2004-08-21 14:18:07 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
if ($GLOBALS['egw_info']['server']['account_repository'] == 'ldap')
|
|
|
|
{
|
|
|
|
$filter = "(&$filter(objectclass=posixaccount))";
|
|
|
|
}
|
|
|
|
$sri = ldap_search($ldap, $GLOBALS['egw_info']['server']['ldap_context'], $filter, $attributes);
|
|
|
|
$allValues = ldap_get_entries($ldap, $sri);
|
2005-08-27 14:19:35 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
if ($allValues['count'] > 0)
|
|
|
|
{
|
|
|
|
if ($GLOBALS['egw_info']['server']['case_sensitive_username'] == true &&
|
|
|
|
$allValues[0]['uid'][0] != $username)
|
2003-09-24 14:21:38 +02:00
|
|
|
{
|
2010-11-16 09:54:09 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."('$username',\$password) wrong case in username!");
|
2008-03-15 18:27:36 +01:00
|
|
|
return false;
|
2003-09-24 14:21:38 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
if ($GLOBALS['egw_info']['server']['account_repository'] == 'ldap' &&
|
|
|
|
isset($allValues[0]['shadowexpire']) && $allValues[0]['shadowexpire'][0]*24*3600 < time())
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2010-11-16 09:54:09 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."('$username',\$password) account is expired!");
|
2008-03-15 18:27:36 +01:00
|
|
|
return false; // account is expired
|
|
|
|
}
|
|
|
|
$userDN = $allValues[0]['dn'];
|
2007-06-20 08:49:02 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
// try to bind as the user with user suplied password
|
|
|
|
// only if a non-empty password given, in case anonymous search is enabled
|
2011-05-04 18:35:40 +02:00
|
|
|
if (!empty($passwd) && ($ret = @ldap_bind($ldap, $userDN, $passwd)))
|
2008-03-15 18:27:36 +01:00
|
|
|
{
|
|
|
|
if ($GLOBALS['egw_info']['server']['account_repository'] != 'ldap')
|
2001-06-26 23:29:39 +02:00
|
|
|
{
|
2011-11-15 13:43:59 +01:00
|
|
|
// store homedirectory for egw_session->read_repositories
|
|
|
|
$GLOBALS['auto_create_acct'] = array();
|
|
|
|
if (isset($allValues[0]['homedirectory']))
|
|
|
|
{
|
2011-11-15 20:16:09 +01:00
|
|
|
$GLOBALS['auto_create_acct']['homedirectory'] = $allValues[0]['homedirectory'][0];
|
2011-11-15 13:43:59 +01:00
|
|
|
}
|
2011-05-04 18:35:40 +02:00
|
|
|
if (!($id = $GLOBALS['egw']->accounts->name2id($username,'account_lid','u')))
|
2003-09-24 14:21:38 +02:00
|
|
|
{
|
2011-05-04 18:35:40 +02:00
|
|
|
// account does NOT exist, check if we should create it
|
|
|
|
if ($GLOBALS['egw_info']['server']['auto_create_acct'])
|
2004-05-22 13:00:18 +02:00
|
|
|
{
|
2011-05-04 18:35:40 +02:00
|
|
|
// create a global array with all availible info about that account
|
|
|
|
foreach(array(
|
|
|
|
'givenname' => 'firstname',
|
|
|
|
'sn' => 'lastname',
|
|
|
|
'uidnumber' => 'account_id',
|
|
|
|
'mail' => 'email',
|
|
|
|
) as $ldap_name => $acct_name)
|
|
|
|
{
|
|
|
|
$GLOBALS['auto_create_acct'][$acct_name] =
|
|
|
|
translation::convert($allValues[0][$ldap_name][0],'utf-8');
|
|
|
|
}
|
|
|
|
$ret = true;
|
2004-05-22 13:00:18 +02:00
|
|
|
}
|
2011-05-04 18:35:40 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$ret = false;
|
|
|
|
if ($this->debug) error_log(__METHOD__."('$username',\$password) bind as user failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// account exists, check if it is acctive
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ret = $GLOBALS['egw']->accounts->id2name($id,'account_status') == 'A';
|
|
|
|
|
|
|
|
if ($this->debug && !$ret) error_log(__METHOD__."('$username',\$password) account NOT active!");
|
2003-09-24 14:21:38 +02:00
|
|
|
}
|
2001-06-26 23:29:39 +02:00
|
|
|
}
|
2011-05-04 18:35:40 +02:00
|
|
|
// account-repository is ldap --> check if passwd hash migration is enabled
|
|
|
|
elseif ($GLOBALS['egw_info']['server']['pwd_migration_allowed'] &&
|
|
|
|
!empty($GLOBALS['egw_info']['server']['pwd_migration_types']))
|
|
|
|
{
|
|
|
|
// try to query password from ldap server (might fail because of ACL) and check if we need to migrate the hash
|
|
|
|
if (($sri = ldap_search($ldap, $userDN,"(objectclass=*)", array('userPassword'))) &&
|
|
|
|
($values = ldap_get_entries($ldap, $sri)) && isset($values[0]['userpassword'][0]) &&
|
2011-06-06 01:22:51 +02:00
|
|
|
($type = preg_match('/^{(.+)}/',$values[0]['userpassword'][0],$matches) ? strtolower($matches[1]) : 'plain') &&
|
|
|
|
// for crypt use auth::crypt_compare to detect correct sub-type, strlen("{crypt}")=7
|
|
|
|
($type != 'crypt' || auth::crypt_compare($passwd, substr($values[0]['userpassword'][0], 7), $type)) &&
|
|
|
|
in_array($type, explode(',',strtolower($GLOBALS['egw_info']['server']['pwd_migration_types']))))
|
2011-05-04 18:35:40 +02:00
|
|
|
{
|
|
|
|
$this->change_password($passwd, $passwd, $allValues[0]['uidnumber'][0], false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
2001-06-26 23:29:39 +02:00
|
|
|
}
|
2001-02-12 22:13:09 +01:00
|
|
|
}
|
2010-11-16 09:54:09 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."('$username','$password') dn not found or password wrong!");
|
2008-03-15 18:27:36 +01:00
|
|
|
// dn not found or password wrong
|
|
|
|
return False;
|
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2011-03-16 12:00:16 +01:00
|
|
|
/**
|
|
|
|
* fetch the last pwd change for the user
|
|
|
|
*
|
|
|
|
* @param string $username username of account to authenticate
|
2011-05-04 18:35:40 +02:00
|
|
|
* @return mixed false or shadowlastchange*24*3600
|
2011-03-16 12:00:16 +01:00
|
|
|
*/
|
|
|
|
function getLastPwdChange($username)
|
|
|
|
{
|
|
|
|
// allow non-ascii in username & password
|
|
|
|
$username = translation::convert($username,translation::charset(),'utf-8');
|
|
|
|
|
|
|
|
if(!$ldap = common::ldapConnect())
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->log->message('F-Abort, Failed connecting to LDAP server for authenication, execution stopped');
|
|
|
|
$GLOBALS['egw']->log->commit();
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Login with the LDAP Admin. User to find the User DN. */
|
|
|
|
if(!@ldap_bind($ldap, $GLOBALS['egw_info']['server']['ldap_root_dn'], $GLOBALS['egw_info']['server']['ldap_root_pw']))
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."('$username') can NOT bind with ldap_root_dn to search!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* find the dn for this uid, the uid is not always in the dn */
|
|
|
|
$attributes = array('uid','dn','shadowexpire','shadowlastchange');
|
|
|
|
|
|
|
|
$filter = $GLOBALS['egw_info']['server']['ldap_search_filter'] ? $GLOBALS['egw_info']['server']['ldap_search_filter'] : '(uid=%user)';
|
|
|
|
$filter = str_replace(array('%user','%domain'),array(ldap::quote($username),$GLOBALS['egw_info']['user']['domain']),$filter);
|
|
|
|
|
|
|
|
if ($GLOBALS['egw_info']['server']['account_repository'] == 'ldap')
|
|
|
|
{
|
|
|
|
$filter = "(&$filter(objectclass=posixaccount))";
|
|
|
|
}
|
|
|
|
$sri = ldap_search($ldap, $GLOBALS['egw_info']['server']['ldap_context'], $filter, $attributes);
|
|
|
|
$allValues = ldap_get_entries($ldap, $sri);
|
|
|
|
|
|
|
|
if ($allValues['count'] > 0)
|
|
|
|
{
|
2011-11-15 11:09:44 +01:00
|
|
|
if (!isset($allValues[0]['shadowlastchange']))
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."('$username') no shadowlastchange attribute!");
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-16 12:00:16 +01:00
|
|
|
if ($GLOBALS['egw_info']['server']['case_sensitive_username'] == true &&
|
|
|
|
$allValues[0]['uid'][0] != $username)
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."('$username') wrong case in username!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($GLOBALS['egw_info']['server']['account_repository'] == 'ldap' &&
|
|
|
|
isset($allValues[0]['shadowexpire']) && $allValues[0]['shadowexpire'][0]*24*3600 < time())
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."('$username',\$password) account is expired!");
|
|
|
|
return false; // account is expired
|
|
|
|
}
|
|
|
|
return $allValues[0]['shadowlastchange'][0]*24*3600;
|
|
|
|
}
|
|
|
|
if ($this->debug) error_log(__METHOD__."('$username') dn not found or password wrong!");
|
|
|
|
// dn not found or password wrong
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* changes account_lastpwd_change in ldap datababse
|
|
|
|
*
|
|
|
|
* @param int $account_id account id of user whose passwd should be changed
|
|
|
|
* @param string $passwd must be cleartext, usually not used, but may be used to authenticate as user to do the change -> ldap
|
|
|
|
* @param int $lastpwdchange must be a unixtimestamp
|
|
|
|
* @return boolean true if account_lastpwd_change successful changed, false otherwise
|
|
|
|
*/
|
|
|
|
function setLastPwdChange($account_id=0, $passwd=NULL, $lastpwdchange=NULL)
|
|
|
|
{
|
2011-05-19 12:32:46 +02:00
|
|
|
$admin = True;
|
|
|
|
// Don't allow password changes for other accounts when using XML-RPC
|
|
|
|
if(!$account_id || $GLOBALS['egw_info']['flags']['currentapp'] == 'login')
|
2011-03-16 12:00:16 +01:00
|
|
|
{
|
2011-05-19 12:32:46 +02:00
|
|
|
$admin = False;
|
2011-03-16 12:00:16 +01:00
|
|
|
$username = $GLOBALS['egw_info']['user']['account_lid'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$username = translation::convert($GLOBALS['egw']->accounts->id2name($account_id),
|
|
|
|
translation::charset(),'utf-8');
|
|
|
|
}
|
|
|
|
//echo "<p>auth_ldap::change_password('$old_passwd','$new_passwd',$account_id) username='$username'</p>\n";
|
|
|
|
|
|
|
|
$filter = $GLOBALS['egw_info']['server']['ldap_search_filter'] ? $GLOBALS['egw_info']['server']['ldap_search_filter'] : '(uid=%user)';
|
|
|
|
$filter = str_replace(array('%user','%domain'),array($username,$GLOBALS['egw_info']['user']['domain']),$filter);
|
|
|
|
|
|
|
|
$ds = common::ldapConnect();
|
|
|
|
$sri = ldap_search($ds, $GLOBALS['egw_info']['server']['ldap_context'], $filter);
|
|
|
|
$allValues = ldap_get_entries($ds, $sri);
|
|
|
|
|
2011-03-16 13:44:42 +01:00
|
|
|
$entry['shadowlastchange'] = (is_null($lastpwdchange) || $lastpwdchange<0 ? round((time()-date('Z')) / (24*3600)):$lastpwdchange);
|
2011-03-16 12:00:16 +01:00
|
|
|
|
|
|
|
$dn = $allValues[0]['dn'];
|
|
|
|
|
2011-05-19 12:32:46 +02:00
|
|
|
if(!$admin && $passwd) // if old password given (not called by admin) --> bind as that user to change the pw
|
2011-03-16 12:00:16 +01:00
|
|
|
{
|
|
|
|
$ds = common::ldapConnect('',$dn,$passwd);
|
|
|
|
}
|
|
|
|
if (!@ldap_modify($ds, $dn, $entry))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-23 13:10:05 +02:00
|
|
|
// using time() is sufficient to represent the current time, we do not need the timestamp written to the storage
|
|
|
|
if (!$admin) egw_cache::setSession('phpgwapi','auth_alpwchange_val',(is_null($lastpwdchange) || $lastpwdchange<0 ? time():$lastpwdchange));
|
2011-03-16 12:00:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
/**
|
|
|
|
* changes password in LDAP
|
|
|
|
*
|
2009-04-09 08:41:41 +02:00
|
|
|
* If $old_passwd is given, the password change is done binded as user and NOT with the
|
2008-03-15 18:27:36 +01:00
|
|
|
* "root" dn given in the configurations.
|
2009-04-09 08:41:41 +02:00
|
|
|
*
|
2008-03-15 18:27:36 +01:00
|
|
|
* @param string $old_passwd must be cleartext or empty to not to be checked
|
|
|
|
* @param string $new_passwd must be cleartext
|
|
|
|
* @param int $account_id account id of user whose passwd should be changed
|
2011-05-04 18:35:40 +02:00
|
|
|
* @param boolean $update_lastchange=true
|
2008-03-15 18:27:36 +01:00
|
|
|
* @return boolean true if password successful changed, false otherwise
|
|
|
|
*/
|
2011-05-04 18:35:40 +02:00
|
|
|
function change_password($old_passwd, $new_passwd, $account_id=0, $update_lastchange=true)
|
2008-03-15 18:27:36 +01:00
|
|
|
{
|
|
|
|
if (!$account_id)
|
2001-04-10 09:58:19 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
$username = $GLOBALS['egw_info']['user']['account_lid'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-28 05:22:37 +01:00
|
|
|
$username = translation::convert($GLOBALS['egw']->accounts->id2name($account_id),
|
|
|
|
translation::charset(),'utf-8');
|
2008-03-15 18:27:36 +01:00
|
|
|
}
|
2011-05-04 18:35:40 +02:00
|
|
|
if ($this->debug) error_log(__METHOD__."('$old_passwd','$new_passwd',$account_id, $update_lastchange) username='$username'");
|
2005-11-13 07:09:10 +01:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
$filter = $GLOBALS['egw_info']['server']['ldap_search_filter'] ? $GLOBALS['egw_info']['server']['ldap_search_filter'] : '(uid=%user)';
|
|
|
|
$filter = str_replace(array('%user','%domain'),array($username,$GLOBALS['egw_info']['user']['domain']),$filter);
|
2004-08-21 14:18:07 +02:00
|
|
|
|
2012-07-31 14:28:05 +02:00
|
|
|
$ds = $ds_admin = common::ldapConnect();
|
2008-03-15 18:27:36 +01:00
|
|
|
$sri = ldap_search($ds, $GLOBALS['egw_info']['server']['ldap_context'], $filter);
|
|
|
|
$allValues = ldap_get_entries($ds, $sri);
|
2009-04-09 08:41:41 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
$entry['userpassword'] = auth::encrypt_password($new_passwd);
|
2011-09-23 11:56:12 +02:00
|
|
|
if ($update_lastchange)
|
|
|
|
{
|
|
|
|
$entry['shadowlastchange'] = round((time()-date('Z')) / (24*3600));
|
|
|
|
}
|
2009-04-09 08:41:41 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
$dn = $allValues[0]['dn'];
|
2005-08-27 14:19:35 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
if($old_passwd) // if old password given (not called by admin) --> bind as that user to change the pw
|
|
|
|
{
|
2012-07-31 14:28:05 +02:00
|
|
|
$user_ds = new ldap(true); // true throw exceptions in case of error
|
|
|
|
try {
|
|
|
|
$ds = $user_ds->ldapConnect('',$dn,$old_passwd);
|
|
|
|
}
|
|
|
|
catch (egw_exception_no_permission $e) {
|
|
|
|
return false; // wrong old user password
|
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
}
|
2012-07-31 14:28:05 +02:00
|
|
|
// try changing password bind as user or as admin, to cater for all sorts of ldap configuration
|
|
|
|
// where either only user is allowed to change his password, or only admin user is allowed to
|
|
|
|
if (!@ldap_modify($ds, $dn, $entry) && (!$old_passwd || !@ldap_modify($ds_admin, $dn, $entry)))
|
2008-03-15 18:27:36 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if($old_passwd) // if old password given (not called by admin) update the password in the session
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->session->appsession('password','phpgwapi',$new_passwd);
|
2011-09-23 13:10:05 +02:00
|
|
|
// using time() is sufficient to represent the current time, we do not need the timestamp written to the storage
|
|
|
|
egw_cache::setSession('phpgwapi','auth_alpwchange_val',time());
|
2001-06-26 23:29:39 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
return $entry['userpassword'];
|
2001-02-12 22:13:09 +01:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
}
|