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
|
|
|
|
* @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
|
|
|
|
* @license http://opensource.org/licenses/lgpl-license.php LGPL - GNU Lesser General Public License
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
class auth_
|
|
|
|
{
|
|
|
|
var $previous_login = -1;
|
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
|
|
|
|
*/
|
|
|
|
function authenticate($username, $passwd)
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
// allow non-ascii in username & password
|
|
|
|
$username = $GLOBALS['egw']->translation->convert($username,$GLOBALS['egw']->translation->charset(),'utf-8');
|
|
|
|
$passwd = $GLOBALS['egw']->translation->convert($passwd,$GLOBALS['egw']->translation->charset(),'utf-8');
|
2001-06-03 19:58:12 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
if(!$ldap = $GLOBALS['egw']->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']))
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
/* find the dn for this uid, the uid is not always in the dn */
|
|
|
|
$attributes = array('uid','dn','givenName','sn','mail','uidNumber','shadowExpire');
|
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
|
|
|
{
|
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
|
|
|
{
|
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
|
|
|
|
if (!empty($passwd) && @ldap_bind($ldap, $userDN, $passwd))
|
|
|
|
{
|
|
|
|
if ($GLOBALS['egw_info']['server']['account_repository'] != 'ldap')
|
2001-06-26 23:29:39 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
if ($GLOBALS['egw_info']['server']['auto_create_acct'])
|
2003-09-24 14:21:38 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
// create a global array with all availible info about that account
|
|
|
|
$GLOBALS['auto_create_acct'] = array();
|
|
|
|
foreach(array(
|
|
|
|
'givenname' => 'firstname',
|
|
|
|
'sn' => 'lastname',
|
|
|
|
'uidnumber' => 'account_id',
|
|
|
|
'mail' => 'email',
|
|
|
|
) as $ldap_name => $acct_name)
|
2004-05-22 13:00:18 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
$GLOBALS['auto_create_acct'][$acct_name] =
|
|
|
|
$GLOBALS['egw']->translation->convert($allValues[0][$ldap_name][0],'utf-8');
|
2004-05-22 13:00:18 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
return True;
|
2003-09-24 14:21:38 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
return ($id = $GLOBALS['egw']->accounts->name2id($username,'account_lid','u')) &&
|
|
|
|
$GLOBALS['egw']->accounts->id2name($id,'account_status') == 'A';
|
2001-06-26 23:29:39 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
return True;
|
2001-06-26 23:29:39 +02:00
|
|
|
}
|
2001-02-12 22:13:09 +01:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
// dn not found or password wrong
|
|
|
|
return False;
|
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
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
|
|
|
|
* @return boolean true if password successful changed, false otherwise
|
|
|
|
*/
|
|
|
|
function change_password($old_passwd, $new_passwd, $account_id=0)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
$username = $GLOBALS['egw']->translation->convert($GLOBALS['egw']->accounts->id2name($account_id),
|
|
|
|
$GLOBALS['egw']->translation->charset(),'utf-8');
|
|
|
|
}
|
|
|
|
//echo "<p>auth_ldap::change_password('$old_password','$new_passwd',$account_id) username='$username'</p>\n";
|
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
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
$ds = $GLOBALS['egw']->common->ldapConnect();
|
|
|
|
$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);
|
2009-04-09 10:11:24 +02:00
|
|
|
$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
|
|
|
|
{
|
|
|
|
$ds = $GLOBALS['egw']->common->ldapConnect('',$dn,$old_passwd);
|
|
|
|
}
|
|
|
|
if (!@ldap_modify($ds, $dn, $entry))
|
|
|
|
{
|
|
|
|
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);
|
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
|
|
|
}
|