2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2004-05-05 14:06:13 +02:00
|
|
|
* eGroupWare API - Auth from SQL *
|
2001-01-13 11:18:50 +01:00
|
|
|
* This file written by Dan Kuykendall <seek3r@phpgroupware.org> *
|
|
|
|
* and Joseph Engo <jengo@phpgroupware.org> *
|
2004-01-18 22:12:53 +01:00
|
|
|
* Encryption types other than md5() added by *
|
|
|
|
* Miles Lott <milos@groupwhere.org> based on code from *
|
|
|
|
* http://www.thomas-alfeld.de/frank/ *
|
2005-05-10 21:00:55 +02:00
|
|
|
* massive code cleanup and *
|
|
|
|
* added password migration by *
|
|
|
|
* Cornelius Weiss <egw@von-und-zu-weiss.de *
|
2001-01-13 11:18:50 +01:00
|
|
|
* Authentication based on SQL table *
|
|
|
|
* Copyright (C) 2000, 2001 Dan Kuykendall *
|
2001-01-16 14:52:32 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
2001-01-13 11:18:50 +01: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 *
|
2001-01-11 10:52:33 +01:00
|
|
|
\**************************************************************************/
|
|
|
|
|
2001-01-13 11:18:50 +01:00
|
|
|
/* $Id$ */
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2004-01-18 22:12:53 +01:00
|
|
|
class auth_
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2004-01-18 22:12:53 +01:00
|
|
|
var $db = '';
|
2001-06-03 19:58:12 +02:00
|
|
|
var $previous_login = -1;
|
2003-08-28 16:31:11 +02:00
|
|
|
|
2004-01-18 22:12:53 +01:00
|
|
|
function auth_()
|
2003-08-28 16:31:11 +02:00
|
|
|
{
|
2005-05-10 21:00:55 +02:00
|
|
|
copyobj($GLOBALS['egw']->db,$this->db);
|
|
|
|
$this->type = @$GLOBALS['egw_info']['server']['sql_encryption_type']
|
|
|
|
? strtolower($GLOBALS['egw_info']['server']['sql_encryption_type'])
|
|
|
|
: 'md5';
|
2003-08-28 16:31:11 +02:00
|
|
|
}
|
2001-06-03 19:58:12 +02:00
|
|
|
|
2005-05-10 21:00:55 +02:00
|
|
|
/*!
|
|
|
|
@function authenticate
|
|
|
|
@abstract password authentication against password stored in sql datababse
|
|
|
|
@param $username username of account to authenticate
|
|
|
|
@param $passwd corresponding password
|
|
|
|
@param $passwd_type 'text' for cleartext passwords
|
|
|
|
*/
|
2001-10-02 07:38:35 +02:00
|
|
|
function authenticate($username, $passwd, $passwd_type)
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2005-05-10 21:00:55 +02:00
|
|
|
/* normal web form login */
|
2004-01-18 22:12:53 +01:00
|
|
|
if($passwd_type == 'text')
|
2001-10-02 07:38:35 +02:00
|
|
|
{
|
2005-05-10 21:00:55 +02:00
|
|
|
$this->db->query("SELECT account_lid,account_pwd,account_lastlogin FROM phpgw_accounts WHERE account_lid = '$username' AND "
|
|
|
|
. " account_type='u' AND "
|
|
|
|
. " account_status ='A'",__LINE__,__FILE__);
|
|
|
|
$this->db->next_record();
|
|
|
|
|
|
|
|
if($GLOBALS['egw_info']['server']['case_sensitive_username'] == true)
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
2005-05-10 21:00:55 +02:00
|
|
|
if($this->db->f('account_lid') != $username) return false;
|
|
|
|
}
|
|
|
|
if(!$this->db->f('account_pwd')) return false;
|
|
|
|
if(!$this->compare_password($passwd,$this->db->f('account_pwd'),$this->type,strtolower($username)))
|
|
|
|
{
|
|
|
|
// do we have to migrate an old password ?
|
2005-05-10 23:14:20 +02:00
|
|
|
if($GLOBALS['egw_info']['server']['pwd_migration_allowed'])
|
2005-05-10 21:00:55 +02:00
|
|
|
{
|
2005-05-11 20:25:17 +02:00
|
|
|
if(!isset($GLOBALS['egw_info']['server']['pwd_migration_types'])) return false;
|
|
|
|
|
|
|
|
$allowed_types = explode(',', $GLOBALS['egw_info']['server']['pwd_migration_types']);
|
|
|
|
foreach($allowed_types as $num => $type)
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
2005-05-10 21:00:55 +02:00
|
|
|
if($this->compare_password($passwd,$this->db->f('account_pwd'),$type,strtolower($username)))
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
2005-05-10 21:00:55 +02:00
|
|
|
$account_id = $GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
$encrypted_passwd = $this->encrypt_sql($passwd);
|
|
|
|
$this->_update_passwd($encrypted_passwd,$passwd,$account_id,false,__FILE__);
|
|
|
|
break;
|
2004-01-18 22:12:53 +01:00
|
|
|
}
|
2005-05-10 23:14:20 +02:00
|
|
|
return false;
|
2004-01-18 22:12:53 +01:00
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-16 08:44:38 +01:00
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
|
|
|
|
/* if this point is reached, auth was successfull */
|
|
|
|
$this->previous_login = $this->db->f('account_lastlogin');
|
|
|
|
return true;
|
2004-01-16 08:44:38 +01:00
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
/* Auth via crypted password. NOTE: mail needs cleartext password to authenticate against mailserver! */
|
|
|
|
else
|
2001-06-03 19:58:12 +02:00
|
|
|
{
|
2004-01-18 22:12:53 +01:00
|
|
|
$this->db->query("SELECT * FROM phpgw_accounts WHERE account_lid = '$username' AND "
|
|
|
|
. "account_pwd='" . $passwd . "' AND account_status ='A'",__LINE__,__FILE__);
|
|
|
|
$this->db->next_record();
|
|
|
|
|
2005-05-10 21:00:55 +02:00
|
|
|
if($GLOBALS['egw_info']['server']['case_sensitive_username'] == true)
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
|
|
|
if($this->db->f('account_lid') != $username)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($this->db->f('account_lid'))
|
|
|
|
{
|
|
|
|
$this->previous_login = $this->db->f('account_lastlogin');
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2001-06-03 19:58:12 +02:00
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2001-02-05 15:58:03 +01:00
|
|
|
|
2005-05-10 21:00:55 +02:00
|
|
|
/*!
|
|
|
|
@function change_password
|
|
|
|
@abstract changes password in sql datababse
|
|
|
|
@param $old_passwd must be cleartext
|
|
|
|
@param $new_passwd must be cleartext
|
|
|
|
@param $account_id account id of user whose passwd should be changed
|
|
|
|
*/
|
2001-04-17 19:49:13 +02:00
|
|
|
function change_password($old_passwd, $new_passwd, $account_id = '')
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
2004-01-21 23:53:02 +01:00
|
|
|
$admin = True;
|
2004-01-18 22:12:53 +01:00
|
|
|
// Don't allow password changes for other accounts when using XML-RPC
|
2005-05-10 21:00:55 +02:00
|
|
|
if(!$account_id || $GLOBALS['egw_info']['flags']['currentapp'] == 'login')
|
2001-04-17 19:49:13 +02:00
|
|
|
{
|
2004-01-21 23:53:02 +01:00
|
|
|
$admin = False;
|
2005-05-10 21:00:55 +02:00
|
|
|
$account_id = $GLOBALS['egw_info']['user']['account_id'];
|
2001-04-17 19:49:13 +02:00
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
|
2005-08-27 14:19:35 +02:00
|
|
|
$this->db->query("SELECT account_pwd FROM phpgw_accounts WHERE account_id = " . (int)$account_id
|
|
|
|
. " AND " // . " account_type='u' AND "
|
2005-05-10 21:00:55 +02:00
|
|
|
. " account_status ='A'",__LINE__,__FILE__);
|
|
|
|
$this->db->next_record();
|
|
|
|
if(!$this->db->f('account_pwd')) return false;
|
2001-04-17 19:49:13 +02:00
|
|
|
|
2005-05-10 21:00:55 +02:00
|
|
|
/* Check the old_passwd to make sure this is legal */
|
|
|
|
if(!$admin)
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
2005-08-27 14:19:35 +02:00
|
|
|
if(!$this->compare_password($old_passwd,$this->db->f('account_pwd'),$this->type,strtolower($username)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-21 23:53:02 +01:00
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
|
|
|
|
/* old password ok, or admin called the function from the admin application (no old passwd available).*/
|
|
|
|
$encrypted_passwd = $this->encrypt_sql($new_passwd);
|
|
|
|
return $this->_update_passwd($encrypted_passwd,$new_passwd,$account_id,$admin,__FILE__);
|
2004-01-21 23:53:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function _update_passwd($encrypted_passwd,$new_passwd,$account_id,$admin=False,$file='')
|
|
|
|
{
|
|
|
|
/* This should only be called from this file */
|
2005-08-27 14:19:35 +02:00
|
|
|
if($file != EGW_API_INC . SEP . 'class.auth_sql.inc.php')
|
2004-01-21 23:53:02 +01:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
$this->db->query("UPDATE phpgw_accounts SET account_pwd='" . $encrypted_passwd . "',"
|
|
|
|
. "account_lastpwd_change='" . time()
|
|
|
|
. "' WHERE account_id=" . (int)$account_id,__LINE__,__FILE__);
|
|
|
|
$this->db->next_record();
|
|
|
|
if($this->db->affected_rows())
|
|
|
|
{
|
|
|
|
if(!$admin)
|
|
|
|
{
|
2005-05-10 21:00:55 +02:00
|
|
|
$GLOBALS['egw']->session->appsession('password','phpgwapi',$new_passwd);
|
2004-01-18 22:12:53 +01:00
|
|
|
}
|
2004-01-21 23:53:02 +01:00
|
|
|
return $encrypted_passwd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2001-03-26 23:36:32 +02:00
|
|
|
function update_lastlogin($account_id, $ip)
|
|
|
|
{
|
2005-05-10 21:00:55 +02:00
|
|
|
$GLOBALS['egw']->db->query("UPDATE phpgw_accounts SET account_lastloginfrom='"
|
2001-03-26 23:36:32 +02:00
|
|
|
. "$ip', account_lastlogin='" . time()
|
2005-08-27 14:19:35 +02:00
|
|
|
. "' WHERE account_id=" . (int)$account_id,__LINE__,__FILE__);
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
?>
|