2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2005-11-02 12:45:52 +01:00
|
|
|
/**************************************************************************\
|
|
|
|
* eGroupWare API - Auth from SQL *
|
|
|
|
* This file written by Dan Kuykendall <seek3r@phpgroupware.org> *
|
|
|
|
* and Joseph Engo <jengo@phpgroupware.org> *
|
|
|
|
* Encryption types other than md5() added by *
|
|
|
|
* Miles Lott <milos@groupwhere.org> 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 *
|
|
|
|
* Authentication based on SQL table *
|
|
|
|
* Copyright (C) 2000, 2001 Dan Kuykendall *
|
|
|
|
* ------------------------------------------------------------------------ *
|
|
|
|
* 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$ */
|
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-11-02 12:45:52 +01:00
|
|
|
$this->db = clone($GLOBALS['egw']->db);
|
|
|
|
$this->db->set_app('phpgwapi');
|
|
|
|
$this->table = 'egw_accounts';
|
|
|
|
|
|
|
|
$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-11-02 12:45:52 +01:00
|
|
|
/**
|
|
|
|
* password authentication against password stored in sql datababse
|
|
|
|
*
|
|
|
|
* @param string $username username of account to authenticate
|
|
|
|
* @param string $passwd corresponding password
|
|
|
|
* @param string $passwd_type='text' 'text' for cleartext passwords (default)
|
|
|
|
* @return boolean true if successful authenticated, false otherwise
|
|
|
|
*/
|
|
|
|
function authenticate($username, $passwd, $passwd_type='text')
|
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-11-02 12:45:52 +01:00
|
|
|
$this->db->select($this->table,'account_lid,account_pwd,account_lastlogin',array(
|
|
|
|
'account_lid' => $username,
|
|
|
|
'account_type' => 'u',
|
|
|
|
'account_status' => 'A'
|
|
|
|
),__LINE__,__FILE__);
|
2005-05-10 21:00:55 +02:00
|
|
|
|
2005-11-02 12:45:52 +01:00
|
|
|
if(!$this->db->next_record() || !$this->db->f('account_pwd') ||
|
|
|
|
$GLOBALS['egw_info']['server']['case_sensitive_username'] && $this->db->f('account_lid') != $username)
|
2004-01-18 22:12:53 +01:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
return false;
|
2005-05-10 21:00:55 +02:00
|
|
|
}
|
|
|
|
if(!$this->compare_password($passwd,$this->db->f('account_pwd'),$this->type,strtolower($username)))
|
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$match = false;
|
2005-05-10 21:00:55 +02:00
|
|
|
// do we have to migrate an old password ?
|
2005-11-22 23:32:21 +01:00
|
|
|
if($GLOBALS['egw_info']['server']['pwd_migration_allowed'] && !empty($GLOBALS['egw_info']['server']['pwd_migration_types']))
|
2005-05-10 21:00:55 +02:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
foreach(explode(',', $GLOBALS['egw_info']['server']['pwd_migration_types']) as $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);
|
2005-11-02 12:45:52 +01:00
|
|
|
$this->_update_passwd($encrypted_passwd,$passwd,$account_id);
|
|
|
|
$match = true;
|
2005-05-10 21:00:55 +02:00
|
|
|
break;
|
2004-01-18 22:12:53 +01:00
|
|
|
}
|
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
}
|
2005-11-02 12:45:52 +01:00
|
|
|
if (!$match) return false;
|
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
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$this->db->select($this->table,'account_lid,account_lastlogin',array(
|
|
|
|
'account_lid' => $username,
|
|
|
|
'account_type' => 'u',
|
|
|
|
'account_status' => 'A',
|
|
|
|
'account_pwd' => $passwd,
|
|
|
|
),__LINE__,__FILE__);
|
|
|
|
|
|
|
|
if(!$this->db->next_record() ||
|
|
|
|
$GLOBALS['egw_info']['server']['case_sensitive_username'] && $this->db->f('account_lid') != $username)
|
2004-01-18 22:12:53 +01: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
|
|
|
}
|
2005-11-02 12:45:52 +01:00
|
|
|
// if this point is reached, auth was successfull
|
|
|
|
$this->previous_login = $this->db->f('account_lastlogin');
|
|
|
|
|
|
|
|
return true;
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
2001-02-05 15:58:03 +01:00
|
|
|
|
2005-11-02 12:45:52 +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)
|
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-11-02 12:45:52 +01:00
|
|
|
$this->db->select($this->table,'account_pwd',array(
|
|
|
|
'account_id' => $account_id,
|
|
|
|
'account_type' => 'u',
|
|
|
|
'account_status' => 'A',
|
|
|
|
),__LINE__,__FILE__);
|
|
|
|
|
|
|
|
if(!$this->db->next_record()) return false; // account not found
|
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 */
|
2005-11-02 12:45:52 +01:00
|
|
|
if(!$admin && !$this->compare_password($old_passwd,$this->db->f('account_pwd'),$this->type,strtolower($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
|
|
|
}
|
2005-05-10 21:00:55 +02:00
|
|
|
|
|
|
|
/* old password ok, or admin called the function from the admin application (no old passwd available).*/
|
2005-11-02 12:45:52 +01:00
|
|
|
return $this->_update_passwd($this->encrypt_sql($new_passwd),$new_passwd,$account_id,$admin);
|
2004-01-21 23:53:02 +01:00
|
|
|
}
|
|
|
|
|
2005-11-02 12:45:52 +01:00
|
|
|
/**
|
|
|
|
* changes password in sql datababse
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @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)
|
2004-01-21 23:53:02 +01:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$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;
|
|
|
|
|
|
|
|
if(!$admin)
|
2004-01-21 23:53:02 +01:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$GLOBALS['egw']->session->appsession('password','phpgwapi',$new_passwd);
|
2004-01-21 23:53:02 +01:00
|
|
|
}
|
2005-11-02 12:45:52 +01:00
|
|
|
return $encrypted_passwd;
|
2001-03-26 23:36:32 +02:00
|
|
|
}
|
|
|
|
}
|