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
|
2010-01-28 05:22:37 +01:00
|
|
|
* @author Ralf Becker <ralfbecker@outdoor-training.de>
|
2008-03-15 18:27:36 +01:00
|
|
|
* @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
|
|
|
|
*/
|
2010-01-28 05:22:37 +01:00
|
|
|
class auth_sql implements auth_backend
|
2008-03-15 18:27:36 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2010-01-28 05:22:37 +01:00
|
|
|
function __construct()
|
2008-03-15 18:27:36 +01:00
|
|
|
{
|
|
|
|
$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
|
|
|
{
|
2011-05-04 15:33:34 +02:00
|
|
|
if (!($row = $this->db->select($this->table,'account_lid,account_pwd,account_lastlogin,account_id',$where,__LINE__,__FILE__)->fetch()) ||
|
2008-06-07 10:25:28 +02:00
|
|
|
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;
|
|
|
|
}
|
2011-06-06 01:22:51 +02:00
|
|
|
if(!($match = auth::compare_password($passwd, $row['account_pwd'], $this->type, strtolower($username), $type)) ||
|
|
|
|
$type != $this->type && in_array($type, explode(',',strtolower($GLOBALS['egw_info']['server']['pwd_migration_types']))))
|
2008-03-15 18:27:36 +01:00
|
|
|
{
|
|
|
|
// 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
|
|
|
{
|
2011-06-06 01:22:51 +02:00
|
|
|
if (!$match)
|
2005-05-10 21:00:55 +02:00
|
|
|
{
|
2011-06-06 01:22:51 +02:00
|
|
|
foreach(explode(',', $GLOBALS['egw_info']['server']['pwd_migration_types']) as $type)
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
2011-06-06 01:22:51 +02:00
|
|
|
if(($match = auth::compare_password($passwd,$row['account_pwd'],$type,strtolower($username))))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2004-01-18 22:12:53 +01:00
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
}
|
2011-06-06 01:22:51 +02:00
|
|
|
if ($match)
|
|
|
|
{
|
|
|
|
$encrypted_passwd = auth::encrypt_sql($passwd);
|
|
|
|
$this->_update_passwd($encrypted_passwd,$passwd,$row['account_id'],false,true);
|
|
|
|
}
|
2004-01-16 08:44:38 +01:00
|
|
|
}
|
2011-05-04 15:33:34 +02:00
|
|
|
if (!$match) return false;
|
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
|
|
|
|
2011-03-16 12:00:16 +01:00
|
|
|
/**
|
|
|
|
* fetch the last pwd change for the user
|
|
|
|
*
|
|
|
|
* @param string $username username of account to authenticate
|
|
|
|
* @return mixed false or account_lastpwd_change
|
|
|
|
*/
|
|
|
|
function getLastPwdChange($username)
|
|
|
|
{
|
|
|
|
/* normal web form login */
|
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
if (!($row = $this->db->select($this->table,'account_lid,account_lastpwd_change',$where,__LINE__,__FILE__)->fetch()) ||
|
|
|
|
$GLOBALS['egw_info']['server']['case_sensitive_username'] && $row['account_lid'] != $username)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// if this point is reached, we found a user with that name and return the account_lastpwd_change
|
|
|
|
$rv = $row['account_lastpwd_change'];
|
|
|
|
|
|
|
|
return $rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* changes account_lastpwd_change in sql 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)
|
|
|
|
{
|
|
|
|
$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'];
|
|
|
|
$username = $GLOBALS['egw_info']['user']['account_lid'];
|
2011-05-04 15:33:34 +02:00
|
|
|
}
|
2011-03-16 12:00:16 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$username = $GLOBALS['egw']->accounts->id2name($account_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($pw = $this->db->select($this->table,'account_pwd',array(
|
|
|
|
'account_id' => $account_id,
|
|
|
|
'account_type' => 'u',
|
|
|
|
'account_status' => 'A',
|
|
|
|
),__LINE__,__FILE__)->fetchColumn()) === false)
|
|
|
|
{
|
|
|
|
return false; // account not found
|
|
|
|
}
|
|
|
|
// Check the passwd to make sure this is legal
|
|
|
|
if(!$admin && !auth::compare_password($passwd,$pw,$this->type,strtolower($username)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-26 10:51:48 +02:00
|
|
|
$lastpwdchange = (is_null($lastpwdchange) || $lastpwdchange<0 ? time():$lastpwdchange);
|
2011-03-16 12:00:16 +01:00
|
|
|
$this->db->update($this->table,array(
|
2011-09-26 10:51:48 +02:00
|
|
|
'account_lastpwd_change' => $lastpwdchange,
|
2011-03-16 12:00:16 +01:00
|
|
|
),array(
|
|
|
|
'account_id' => $account_id,
|
|
|
|
),__LINE__,__FILE__);
|
|
|
|
|
|
|
|
if(!$this->db->affected_rows()) return false;
|
2011-09-26 10:51:48 +02:00
|
|
|
if (!$admin) egw_cache::setSession('phpgwapi','auth_alpwchange_val',$lastpwdchange);
|
2011-03-16 12:00:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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'];
|
2011-03-16 12:00:16 +01:00
|
|
|
$username = $GLOBALS['egw_info']['user']['account_lid'];
|
2011-05-04 15:33:34 +02:00
|
|
|
}
|
2011-03-16 12:00:16 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$username = $GLOBALS['egw']->accounts->id2name($account_id);
|
2008-03-15 18:27:36 +01:00
|
|
|
}
|
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',
|
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).
|
2010-01-28 05:22:37 +01:00
|
|
|
return $this->_update_passwd(auth::encrypt_sql($new_passwd),$new_passwd,$account_id,$admin);
|
2008-03-15 18:27:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* changes password in sql datababse
|
|
|
|
*
|
|
|
|
* @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
|
2011-05-04 15:33:34 +02:00
|
|
|
* @param boolean $update_lastpw_change=true
|
2008-03-15 18:27:36 +01:00
|
|
|
* @return boolean true if password successful changed, false otherwise
|
|
|
|
*/
|
2011-05-04 15:33:34 +02:00
|
|
|
private function _update_passwd($encrypted_passwd,$new_passwd,$account_id,$admin=false,$update_lastpw_change=true)
|
2008-03-15 18:27:36 +01:00
|
|
|
{
|
2011-05-04 15:33:34 +02:00
|
|
|
$update = array('account_pwd' => $encrypted_passwd);
|
2011-05-11 11:39:02 +02:00
|
|
|
if ($update_lastpw_change) $update['account_lastpwd_change'] = time();
|
2011-05-04 15:33:34 +02:00
|
|
|
|
2013-06-26 11:49:57 +02:00
|
|
|
if (!$this->db->update($this->table,$update,array(
|
2008-03-15 18:27:36 +01:00
|
|
|
'account_id' => $account_id,
|
2013-06-26 11:49:57 +02:00
|
|
|
),__LINE__,__FILE__))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-07 10:25:28 +02:00
|
|
|
|
2008-03-15 18:27:36 +01:00
|
|
|
if(!$admin)
|
|
|
|
{
|
2011-09-26 10:51:48 +02:00
|
|
|
egw_cache::setSession('phpgwapi','auth_alpwchange_val',$update['account_lastpwd_change']);
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2013-06-26 11:49:57 +02:00
|
|
|
return true;
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2008-03-15 18:27:36 +01:00
|
|
|
}
|