2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2008-03-15 18:27:36 +01:00
|
|
|
/**
|
|
|
|
* eGroupWare API - Authentication from SQL
|
2008-06-07 10:25:28 +02:00
|
|
|
*
|
2008-03-15 18:27:36 +01:00
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Dan Kuykendall <seek3r@phpgroupware.org>
|
|
|
|
* @author Joseph Engo <jengo@phpgroupware.org>
|
|
|
|
* Copyright (C) 2000, 2001 Dan Kuykendall
|
|
|
|
* @license http://opensource.org/licenses/lgpl-license.php LGPL - GNU Lesser General Public License
|
|
|
|
* @package api
|
|
|
|
* @subpackage authentication
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
/**
|
|
|
|
* eGroupWare API - Authentication based on SQL table of accounts
|
|
|
|
*
|
2008-06-07 10:25:28 +02:00
|
|
|
* Encryption types other than md5() added by Miles Lott <milos@groupwhere.org>
|
2008-03-15 18:27:36 +01:00
|
|
|
* based on code from http://www.thomas-alfeld.de/frank/
|
|
|
|
*
|
|
|
|
* Massive code cleanup and added password migration by Cornelius Weiss <egw@von-und-zu-weiss.de
|
|
|
|
*/
|
|
|
|
class auth_
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Reference to the global db object
|
|
|
|
*
|
|
|
|
* @var egw_db
|
|
|
|
*/
|
|
|
|
var $db;
|
|
|
|
var $table = 'egw_accounts';
|
|
|
|
var $previous_login = -1;
|
2003-08-28 16:31:11 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
function auth_()
|
|
|
|
{
|
|
|
|
$this->db = $GLOBALS['egw']->db;
|
2005-11-02 12:45:52 +01:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
$this->type = @$GLOBALS['egw_info']['server']['sql_encryption_type'] ?
|
|
|
|
strtolower($GLOBALS['egw_info']['server']['sql_encryption_type']) : 'md5';
|
|
|
|
}
|
2001-06-03 19:58:12 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
/**
|
|
|
|
* password authentication against password stored in sql datababse
|
|
|
|
*
|
|
|
|
* @param string $username username of account to authenticate
|
|
|
|
* @param string $passwd corresponding password
|
2008-06-07 10:25:28 +02:00
|
|
|
* @param string $passwd_type='text' 'text' for cleartext passwords (default)
|
2008-03-15 18:27:36 +01:00
|
|
|
* @return boolean true if successful authenticated, false otherwise
|
|
|
|
*/
|
|
|
|
function authenticate($username, $passwd, $passwd_type='text')
|
|
|
|
{
|
|
|
|
/* normal web form login */
|
2008-06-07 10:25:28 +02:00
|
|
|
$where = array(
|
|
|
|
'account_lid' => $username,
|
|
|
|
'account_type' => 'u',
|
|
|
|
'account_status' => 'A'
|
|
|
|
);
|
|
|
|
if (!$GLOBALS['egw_info']['server']['case_sensitive_username']) // = is case sensitiv eg. on postgres, but not on mysql!
|
|
|
|
{
|
|
|
|
$where[] = 'account_lid '.$this->db->capabilities[egw_db::CAPABILITY_CASE_INSENSITIV_LIKE].' '.$this->db->quote($username);
|
|
|
|
unset($where['account_lid']);
|
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
if($passwd_type == 'text')
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2008-06-07 10:25:28 +02:00
|
|
|
if (!($row = $this->db->select($this->table,'account_lid,account_pwd,account_lastlogin',$where,__LINE__,__FILE__)->fetch()) ||
|
|
|
|
empty($row['account_pwd']) ||
|
2008-03-15 18:27:36 +01:00
|
|
|
$GLOBALS['egw_info']['server']['case_sensitive_username'] && $row['account_lid'] != $username)
|
2001-10-02 07:38:35 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!auth::compare_password($passwd,$row['account_pwd'],$this->type,strtolower($username)))
|
|
|
|
{
|
|
|
|
$match = false;
|
|
|
|
// do we have to migrate an old password ?
|
|
|
|
if($GLOBALS['egw_info']['server']['pwd_migration_allowed'] && !empty($GLOBALS['egw_info']['server']['pwd_migration_types']))
|
2005-05-10 21:00:55 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
foreach(explode(',', $GLOBALS['egw_info']['server']['pwd_migration_types']) as $type)
|
2005-05-10 21:00:55 +02:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
if(auth::compare_password($passwd,$row['account_pwd'],$type,strtolower($username)))
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
$account_id = $GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
$encrypted_passwd = $this->encrypt_sql($passwd);
|
|
|
|
$this->_update_passwd($encrypted_passwd,$passwd,$account_id);
|
|
|
|
$match = true;
|
|
|
|
break;
|
2004-01-18 22:12:53 +01:00
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
}
|
2004-01-16 08:44:38 +01:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
if (!$match)
|
2008-06-07 10:25:28 +02:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
return false;
|
2004-01-18 22:12:53 +01:00
|
|
|
}
|
2001-06-03 19:58:12 +02:00
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
/* Auth via crypted password. NOTE: mail needs cleartext password to authenticate against mailserver! */
|
|
|
|
else
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2008-06-07 10:25:28 +02:00
|
|
|
$where['account_pwd'] = $passwd;
|
|
|
|
if (!($row = $this->db->select($this->table,'account_lid,account_lastlogin',$where,__LINE__,__FILE__)->fetch()) ||
|
2008-03-15 18:27:36 +01:00
|
|
|
$GLOBALS['egw_info']['server']['case_sensitive_username'] && $row['account_lid'] != $username)
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
return false;
|
2004-01-21 23:53:02 +01:00
|
|
|
}
|
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
// if this point is reached, auth was successfull
|
|
|
|
$this->previous_login = $row['account_lastlogin'];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2004-01-21 23:53:02 +01:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
/**
|
|
|
|
* changes password in sql datababse
|
|
|
|
*
|
|
|
|
* @param string $old_passwd must be cleartext
|
|
|
|
* @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)
|
|
|
|
{
|
|
|
|
$admin = True;
|
|
|
|
// Don't allow password changes for other accounts when using XML-RPC
|
|
|
|
if(!$account_id || $GLOBALS['egw_info']['flags']['currentapp'] == 'login')
|
|
|
|
{
|
|
|
|
$admin = False;
|
|
|
|
$account_id = $GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
}
|
2008-06-07 10:25:28 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
if (($pw = $this->db->select($this->table,'account_pwd',array(
|
|
|
|
'account_id' => $account_id,
|
|
|
|
'account_type' => 'u',
|
|
|
|
'account_status' => 'A',
|
2009-06-08 18:21:14 +02:00
|
|
|
),__LINE__,__FILE__)->fetchColumn()) === false)
|
2004-01-21 23:53:02 +01:00
|
|
|
{
|
2008-03-15 18:27:36 +01:00
|
|
|
return false; // account not found
|
|
|
|
}
|
|
|
|
// Check the old_passwd to make sure this is legal
|
|
|
|
if(!$admin && !auth::compare_password($old_passwd,$pw,$this->type,strtolower($username)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-11-02 12:45:52 +01:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
// old password ok, or admin called the function from the admin application (no old passwd available).
|
|
|
|
return $this->_update_passwd($this->encrypt_sql($new_passwd),$new_passwd,$account_id,$admin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* changes password in sql datababse
|
|
|
|
*
|
2008-06-07 10:25:28 +02:00
|
|
|
* @internal
|
2008-03-15 18:27:36 +01:00
|
|
|
* @param string $encrypted_passwd
|
|
|
|
* @param string $new_passwd cleartext
|
|
|
|
* @param int $account_id account id of user whose passwd should be changed
|
|
|
|
* @param boolean $admin=false called by admin, if not update password in the session
|
|
|
|
* @return boolean true if password successful changed, false otherwise
|
|
|
|
*/
|
|
|
|
function _update_passwd($encrypted_passwd,$new_passwd,$account_id,$admin=false)
|
|
|
|
{
|
|
|
|
$this->db->update($this->table,array(
|
|
|
|
'account_pwd' => $encrypted_passwd,
|
|
|
|
'account_lastpwd_change' => time(),
|
|
|
|
),array(
|
|
|
|
'account_id' => $account_id,
|
|
|
|
),__LINE__,__FILE__);
|
|
|
|
|
|
|
|
if(!$this->db->affected_rows()) return false;
|
2008-06-07 10:25:28 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
if(!$admin)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->session->appsession('password','phpgwapi',$new_passwd);
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
return $encrypted_passwd;
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
}
|