2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2001-04-10 09:58:19 +02:00
|
|
|
/**************************************************************************\
|
|
|
|
* phpGroupWare API - Auth from LDAP *
|
|
|
|
* This file written by Lars Kneschke <kneschke@phpgroupware.org> *
|
|
|
|
* and Joseph Engo <jengo@phpgroupware.org> *
|
|
|
|
* Authentication based on LDAP Server *
|
|
|
|
* Copyright (C) 2000, 2001 Joseph Engo *
|
|
|
|
* -------------------------------------------------------------------------*
|
|
|
|
* This library is part of the phpGroupWare API *
|
|
|
|
* http://www.phpgroupware.org/api *
|
|
|
|
* ------------------------------------------------------------------------ *
|
|
|
|
* 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
|
|
|
|
2001-03-26 23:36:32 +02:00
|
|
|
class auth
|
|
|
|
{
|
2001-06-03 19:58:12 +02:00
|
|
|
var $previous_login = -1;
|
|
|
|
|
2001-04-10 09:58:19 +02:00
|
|
|
function authenticate($username, $passwd)
|
|
|
|
{
|
2001-06-26 23:29:39 +02:00
|
|
|
/*
|
|
|
|
error_reporting MUST be set to zero, otherwise you'll get nasty LDAP errors with a bad login/pass...
|
|
|
|
these are just "warnings" and can be ignored.....
|
|
|
|
*/
|
2001-03-26 23:36:32 +02:00
|
|
|
error_reporting(0);
|
|
|
|
|
2002-11-24 02:45:28 +01:00
|
|
|
if(!$ldap = @ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']))
|
2001-08-05 11:54:44 +02:00
|
|
|
{
|
2001-08-30 21:39:13 +02:00
|
|
|
$GLOBALS['phpgw']->log->message('F-Abort, Failed connecting to LDAP server for authenication, execution stopped');
|
|
|
|
$GLOBALS['phpgw']->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
|
|
|
|
2002-11-24 02:45:28 +01:00
|
|
|
/* Login with the LDAP Admin. User to find the User DN. */
|
|
|
|
if(!@ldap_bind($ldap, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']))
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2001-06-26 23:29:39 +02:00
|
|
|
/* find the dn for this uid, the uid is not always in the dn */
|
2002-11-24 02:45:28 +01:00
|
|
|
$attributes = array( "uid", "dn" );
|
|
|
|
$sri = ldap_search($ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], "(uid=$username)", $attributes);
|
2001-03-26 23:36:32 +02:00
|
|
|
$allValues = ldap_get_entries($ldap, $sri);
|
2001-04-10 09:58:19 +02:00
|
|
|
if ($allValues['count'] > 0)
|
2001-03-26 23:36:32 +02:00
|
|
|
{
|
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
|
|
|
/*
|
|
|
|
generate a bogus password to pass if the user doesn't give us one
|
|
|
|
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
|
|
|
{
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2001-06-26 23:29:39 +02:00
|
|
|
/* Turn error reporting back to normal */
|
|
|
|
error_reporting(7);
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2001-06-26 23:29:39 +02:00
|
|
|
/* dn not found or password wrong */
|
|
|
|
return False;
|
2001-02-12 22:13:09 +01:00
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2001-06-26 23:29:39 +02:00
|
|
|
function change_password($old_passwd, $new_passwd, $_account_id='')
|
2001-04-10 09:58:19 +02:00
|
|
|
{
|
2001-06-26 23:29:39 +02:00
|
|
|
if ('' == $_account_id)
|
2001-04-10 09:58:19 +02:00
|
|
|
{
|
2001-08-30 21:39:13 +02:00
|
|
|
$_account_id = $GLOBALS['phpgw_info']['user']['account_id'];
|
2001-04-10 09:58:19 +02:00
|
|
|
}
|
|
|
|
|
2001-08-30 21:39:13 +02:00
|
|
|
$ds = $GLOBALS['phpgw']->common->ldapConnect();
|
|
|
|
$sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], "uidnumber=$_account_id");
|
2001-04-10 09:58:19 +02:00
|
|
|
$allValues = ldap_get_entries($ds, $sri);
|
|
|
|
|
|
|
|
|
2001-08-30 21:39:13 +02:00
|
|
|
$entry['userpassword'] = $GLOBALS['phpgw']->common->encrypt_password($new_passwd);
|
2001-06-26 23:29:39 +02:00
|
|
|
$dn = $allValues[0]['dn'];
|
2001-04-10 09:58:19 +02:00
|
|
|
|
|
|
|
if (!@ldap_modify($ds, $dn, $entry))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2001-08-30 21:39:13 +02:00
|
|
|
$GLOBALS['phpgw']->session->appsession('password','phpgwapi',$new_passwd);
|
2001-04-10 09:58:19 +02:00
|
|
|
|
|
|
|
return $encrypted_passwd;
|
|
|
|
}
|
2001-03-26 23:36:32 +02:00
|
|
|
|
2001-06-26 23:29:39 +02:00
|
|
|
/* This data needs to be updated in LDAP, not SQL (jengo) */
|
2001-09-03 05:56:12 +02:00
|
|
|
function old_update_lastlogin($account_id, $ip)
|
2001-02-12 22:13:09 +01:00
|
|
|
{
|
2001-08-30 21:39:13 +02:00
|
|
|
$GLOBALS['phpgw']->db->query("SELECT account_lastlogin FROM phpgw_accounts WHERE account_id='$account_id'",__LINE__,__FILE__);
|
|
|
|
$GLOBALS['phpgw']->db->next_record();
|
|
|
|
$this->previous_login = $GLOBALS['phpgw']->db->f('account_lastlogin');
|
2001-06-03 19:58:12 +02:00
|
|
|
|
2001-04-10 09:58:19 +02:00
|
|
|
$now = time();
|
2001-06-03 19:58:12 +02:00
|
|
|
|
2001-08-30 21:39:13 +02:00
|
|
|
$GLOBALS['phpgw']->db->query("UPDATE phpgw_accounts SET account_lastloginfrom='"
|
2001-04-10 09:58:19 +02:00
|
|
|
. "$ip', account_lastlogin='" . $now
|
2001-06-26 23:29:39 +02:00
|
|
|
. "' WHERE account_id='$account_id'",__LINE__,__FILE__);
|
2001-02-12 22:13:09 +01:00
|
|
|
}
|
2001-06-26 23:29:39 +02:00
|
|
|
|
2001-09-03 05:56:12 +02:00
|
|
|
function update_lastlogin($_account_id, $ip)
|
2001-06-26 23:29:39 +02:00
|
|
|
{
|
|
|
|
$entry['phpgwaccountlastlogin'] = time();
|
|
|
|
$entry['phpgwaccountlastloginfrom'] = $ip;
|
|
|
|
|
2001-08-30 21:39:13 +02:00
|
|
|
$ds = $GLOBALS['phpgw']->common->ldapConnect();
|
|
|
|
$sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], 'uidnumber=' . $_account_id);
|
2001-06-26 23:29:39 +02:00
|
|
|
$allValues = ldap_get_entries($ds, $sri);
|
|
|
|
|
|
|
|
$dn = $allValues[0]['dn'];
|
|
|
|
$this->previous_login = $allValues[0]['phpgwaccountlastlogin'][0];
|
|
|
|
|
|
|
|
@ldap_modify($ds, $dn, $entry);
|
|
|
|
}
|
2001-02-12 22:13:09 +01:00
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
?>
|