2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2001-04-10 09:58:19 +02:00
|
|
|
/**************************************************************************\
|
2004-05-05 14:06:13 +02:00
|
|
|
* eGroupWare API - Auth from LDAP *
|
2003-09-21 21:02:12 +02:00
|
|
|
* This file written by Lars Kneschke <lkneschke@linux-at-work.de> *
|
2001-04-10 09:58:19 +02:00
|
|
|
* and Joseph Engo <jengo@phpgroupware.org> *
|
|
|
|
* Authentication based on LDAP Server *
|
|
|
|
* Copyright (C) 2000, 2001 Joseph Engo *
|
2003-09-21 21:02:12 +02:00
|
|
|
* Copyright (C) 2002, 2003 Lars Kneschke *
|
2005-08-27 14:19:35 +02:00
|
|
|
* ------------------------------------------------------------------------ *
|
2004-05-05 14:06:13 +02:00
|
|
|
* This library is part of the eGroupWare API *
|
2005-08-27 14:19:35 +02:00
|
|
|
* http://www.egroupware.org/api *
|
2001-04-10 09:58:19 +02:00
|
|
|
* ------------------------------------------------------------------------ *
|
|
|
|
* This library is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, *
|
|
|
|
* or any later version. *
|
|
|
|
* This library is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU Lesser General Public License for more details. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this library; if not, write to the Free Software Foundation, *
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
2005-08-27 14:19:35 +02:00
|
|
|
|
2004-01-18 22:12:53 +01:00
|
|
|
class auth_
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2001-06-03 19:58:12 +02:00
|
|
|
var $previous_login = -1;
|
|
|
|
|
2005-11-02 12:45:52 +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
|
|
|
|
*/
|
2001-04-10 09:58:19 +02:00
|
|
|
function authenticate($username, $passwd)
|
|
|
|
{
|
2003-10-02 23:01:37 +02:00
|
|
|
if (ereg('[()|&=*,<>!~]',$username))
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2005-08-27 14:19:35 +02:00
|
|
|
if(!$ldap = @ldap_connect($GLOBALS['egw_info']['server']['ldap_host']))
|
2001-08-05 11:54:44 +02:00
|
|
|
{
|
2005-08-27 14:19:35 +02:00
|
|
|
$GLOBALS['egw']->log->message('F-Abort, Failed connecting to LDAP server for authenication, execution stopped');
|
|
|
|
$GLOBALS['egw']->log->commit();
|
2002-11-24 02:45:28 +01:00
|
|
|
return False;
|
2001-08-05 11:54:44 +02:00
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2005-08-27 14:19:35 +02:00
|
|
|
if($GLOBALS['egw_info']['server']['ldap_version3'])
|
2003-09-14 16:35:36 +02:00
|
|
|
{
|
|
|
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
|
|
}
|
|
|
|
|
2002-11-24 02:45:28 +01:00
|
|
|
/* Login with the LDAP Admin. User to find the User DN. */
|
2005-08-27 14:19:35 +02:00
|
|
|
if(!@ldap_bind($ldap, $GLOBALS['egw_info']['server']['ldap_root_dn'], $GLOBALS['egw_info']['server']['ldap_root_pw']))
|
2002-11-24 02:45:28 +01:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2001-06-26 23:29:39 +02:00
|
|
|
/* find the dn for this uid, the uid is not always in the dn */
|
2004-05-22 13:00:18 +02:00
|
|
|
$attributes = array('uid','dn','givenName','sn','mail','uidNumber','gidNumber');
|
2004-08-21 14:18:07 +02:00
|
|
|
|
2005-08-27 14:19:35 +02: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);
|
|
|
|
|
|
|
|
if ($GLOBALS['egw_info']['server']['account_repository'] == 'ldap')
|
2003-09-24 14:21:38 +02:00
|
|
|
{
|
2004-08-21 14:18:07 +02:00
|
|
|
$filter = "(&$filter(phpgwaccountstatus=A))";
|
2003-09-24 14:21:38 +02:00
|
|
|
}
|
2004-05-22 13:00:18 +02:00
|
|
|
|
2005-08-27 14:19:35 +02:00
|
|
|
$sri = ldap_search($ldap, $GLOBALS['egw_info']['server']['ldap_context'], $filter, $attributes);
|
2001-03-26 23:36:32 +02:00
|
|
|
$allValues = ldap_get_entries($ldap, $sri);
|
2004-05-22 13:00:18 +02:00
|
|
|
|
2001-04-10 09:58:19 +02:00
|
|
|
if ($allValues['count'] > 0)
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2005-08-27 14:19:35 +02:00
|
|
|
if($GLOBALS['egw_info']['server']['case_sensitive_username'] == true)
|
2004-01-16 08:44:38 +01:00
|
|
|
{
|
|
|
|
if($allValues[0]['uid'][0] != $username)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2001-06-26 23:29:39 +02:00
|
|
|
/* we only care about the first dn */
|
2001-03-26 23:36:32 +02:00
|
|
|
$userDN = $allValues[0]['dn'];
|
2001-06-26 23:29:39 +02:00
|
|
|
/*
|
2005-08-27 14:19:35 +02:00
|
|
|
generate a bogus password to pass if the user doesn't give us one
|
2001-06-26 23:29:39 +02:00
|
|
|
this gets around systems that are anonymous search enabled
|
|
|
|
*/
|
|
|
|
if (empty($passwd))
|
|
|
|
{
|
|
|
|
$passwd = crypt(microtime());
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2001-06-26 23:29:39 +02:00
|
|
|
/* try to bind as the user with user suplied password */
|
2002-11-24 02:45:28 +01:00
|
|
|
if (@ldap_bind($ldap, $userDN, $passwd))
|
2001-06-26 23:29:39 +02:00
|
|
|
{
|
2005-08-27 14:19:35 +02:00
|
|
|
if ($GLOBALS['egw_info']['server']['account_repository'] != 'ldap')
|
2003-09-24 14:21:38 +02:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$account =& CreateObject('phpgwapi.accounts',$username,'u');
|
2005-08-27 14:19:35 +02:00
|
|
|
if (!$account->account_id && $GLOBALS['egw_info']['server']['auto_create_acct'])
|
2004-05-22 13:00:18 +02:00
|
|
|
{
|
|
|
|
// create a global array with all availible info about that account
|
|
|
|
$GLOBALS['auto_create_acct'] = array();
|
|
|
|
foreach(array(
|
|
|
|
'givenname' => 'firstname',
|
|
|
|
'sn' => 'lastname',
|
|
|
|
'uidnumber' => 'id',
|
|
|
|
'mail' => 'email',
|
|
|
|
'gidnumber' => 'primary_group',
|
|
|
|
) as $ldap_name => $acct_name)
|
|
|
|
{
|
|
|
|
$GLOBALS['auto_create_acct'][$acct_name] =
|
2005-08-27 14:19:35 +02:00
|
|
|
$GLOBALS['egw']->translation->convert($allValues[0][$ldap_name][0],'utf-8');
|
2004-05-22 13:00:18 +02:00
|
|
|
}
|
|
|
|
return True;
|
|
|
|
}
|
2003-09-24 14:21:38 +02:00
|
|
|
$data = $account->read_repository();
|
|
|
|
return $data['status'] == 'A';
|
|
|
|
}
|
2001-06-26 23:29:39 +02:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* dn not found or password wrong */
|
|
|
|
return False;
|
2001-02-12 22:13:09 +01:00
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2005-11-02 12:45:52 +01:00
|
|
|
/**
|
|
|
|
* changes password in LDAP
|
|
|
|
*
|
|
|
|
* @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)
|
2001-04-10 09:58:19 +02:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
if (!$_account_id)
|
2001-04-10 09:58:19 +02:00
|
|
|
{
|
2005-08-27 14:19:35 +02:00
|
|
|
$username = $GLOBALS['egw_info']['user']['account_lid'];
|
2001-04-10 09:58:19 +02:00
|
|
|
}
|
2004-08-21 14:18:07 +02:00
|
|
|
else
|
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$username = $GLOBALS['egw']->accounts->id2name($account_id);
|
2004-08-21 14:18:07 +02:00
|
|
|
}
|
2005-08-27 14:19:35 +02: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
|
|
|
|
2005-08-27 14:19:35 +02:00
|
|
|
$ds = $GLOBALS['egw']->common->ldapConnect();
|
|
|
|
$sri = ldap_search($ds, $GLOBALS['egw_info']['server']['ldap_context'], $filter);
|
2001-04-10 09:58:19 +02:00
|
|
|
$allValues = ldap_get_entries($ds, $sri);
|
2005-08-27 14:19:35 +02:00
|
|
|
|
2004-01-18 22:12:53 +01:00
|
|
|
$entry['userpassword'] = $this->encrypt_password($new_passwd);
|
2001-06-26 23:29:39 +02:00
|
|
|
$dn = $allValues[0]['dn'];
|
2005-08-27 14:19:35 +02:00
|
|
|
|
2005-11-02 12:45:52 +01:00
|
|
|
if ($old_passwd && $GLOBALS['egw']->auth->encrypt_password($old_passwd) != $allValues[0]['userpassword'] || !@ldap_modify($ds, $dn, $entry))
|
2001-04-10 09:58:19 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-11-02 12:45:52 +01:00
|
|
|
if($old_passwd) // if old password given (not called by admin) update the password in the session
|
2004-08-21 14:18:07 +02:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$GLOBALS['egw']->session->appsession('password','phpgwapi',$new_passwd);
|
2004-08-21 14:18:07 +02:00
|
|
|
}
|
2005-11-02 12:45:52 +01:00
|
|
|
return $entry['userpassword'];
|
2001-06-26 23:29:39 +02:00
|
|
|
}
|
2001-02-12 22:13:09 +01:00
|
|
|
}
|