1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
<?php
/**
* The ldap class attempts to return user information stored in an ldap
* directory service.
*
* Copyright 2001-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL-2). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl.
*
* @author Eric Jon Rostetter <eric.rostetter@physics.utexas.edu>
* @author Jan Schneider <jan@horde.org>
* @package Horde
*/
class Horde_Block_Account_Ldap extends Horde_Block_Account_Base
{
/**
* Pointer to the LDAP connection.
*
* @var Horde_Ldap
*/
protected $_ldap;
/**
* User information hash.
*
* @var array
*/
protected $_information;
/**
* Constructor.
*
* @param array $params A hash containing connection parameters.
*/
public function __construct($params = array())
{
$this->_ldap = $params['ldap'];
unset($params['ldap']);
parent::__construct($params);
}
/**
* Returns the win32 AD epoch number of days the password may be unchanged.
*
* @return integer|boolean Number of days or false if no limit.
*/
protected function _getMaxPasswd()
{
$dn = Horde_Ldap_Util::explodeDN($this->_params['basedn']);
$domaindn = array();
foreach ($dn as $rdn) {
$attribute = Horde_Ldap_Util::splitAttributeString($rdn);
if ($attribute[0] == 'DC') {
$domaindn[] = $rdn;
}
}
$dn = Horde_Ldap_Util::canonicalDN($domaindn);
$search = $this->_ldap->search($domaindn, 'objectClass=*');
$entry = $search->shiftEntry();
try {
return $entry->getValue('maxPwdAge', 'single');
} catch (Horde_Ldap_Exception $e) {
return false;
}
}
/**
* Code from 'brudinie at yahoo dot co dot uk' at http://nl3.php.net/ldap/
*
* @param integer $dateLargeInt The win32 active directory epoch time.
*
* @return integer A unix timestamp.
*/
protected function _convertWinTimeToUnix($dateLargeInt)
{
// Seconds since jan 1st 1601.
$secsAfterADEpoch = $dateLargeInt / (10000000);
// Unix epoch - AD epoch * number of tropical days * seconds in a day.
$ADToUnixConvertor = ((1970 - 1601) * 365.242190) * 86400;
return intval($secsAfterADEpoch - $ADToUnixConvertor);
}
/**
* Returns the user account from the LDAP source.
*
* @return Horde_Ldap_Entry An entry with complete account details.
*
* @throws Horde_Exception if user not found.
* @throws Horde_Ldap_Exception on LDAP errors.
*/
protected function _getAccount()
{
if (!isset($this->_information)) {
$search = $this->_ldap->search($this->_params['basedn'],
$this->_params['attr'] . '=' . $this->_params['user']);
if (!$search->count()) {
throw new Horde_Exception(_("User account not found"));
}
$this->_information = $search->shiftEntry();
}
return $this->_information;
}
/**
* Returns the user's full name.
*
* @return string The user's full name.
*
* @throws Horde_Exception if user not found.
* @throws Horde_Ldap_Exception on LDAP errors.
*/
public function getFullname()
{
$information = $this->_getAccount();
try {
return $information->getValue('cn', 'single');
} catch (Horde_Ldap_Exception $e) {
return '';
}
}
/**
* Returns the user's home (login) directory.
*
* @return string The user's directory.
*
* @throws Horde_Exception if user not found.
* @throws Horde_Ldap_Exception on LDAP errors.
*/
public function getHome()
{
$information = $this->_getAccount();
try {
return $information->getValue('homedirectory', 'single');
} catch (Horde_Ldap_Exception $e) {
return '';
}
}
/**
* Returns the user's default shell.
*
* @return string The user's shell.
*
* @throws Horde_Exception if user not found.
* @throws Horde_Ldap_Exception on LDAP errors.
*/
public function getShell()
{
$information = $this->_getAccount();
try {
return $information->getValue('useraccountcontrol', 'single');
} catch (Horde_Ldap_Exception $e) {
}
try {
return $information->getValue('loginshell', 'single');
} catch (Horde_Ldap_Exception $e) {
return '';
}
}
/**
* Returns the date of the user's last password change.
*
* @return string Date string.
*
* @throws Horde_Exception if user not found.
* @throws Horde_Ldap_Exception on LDAP errors.
*/
public function getPasswordChange()
{
$information = $this->_getAccount();
try {
return strftime('%x', $information->getValue('shadowlastchange', 'single') * 86400);
} catch (Horde_Ldap_Exception $e) {
}
try {
return strftime('%x', $this->_convertWinTimeToUnix($information->getValue('pwdlastset', 'single')));
} catch (Horde_Ldap_Exception $e) {
return '';
}
}
/**
* Returns the status of the current password.
*
* @return string A string with a warning message if the password is about
* to expire.
*
* @throws Horde_Exception if user not found.
* @throws Horde_Ldap_Exception on LDAP errors.
*/
public function checkPasswordStatus()
{
$information = $this->_getAccount();
// Active Directory.
try {
$accountControl = $information->getValue('useraccountcontrol', 'single');
$pwdlastset = $information->getValue('pwdlastset', 'single');
$accountControl = $information->getValue('useraccountcontrol', 'single');
if (($accountControl & 65536) != 0) {
// ADS_UF_DONT_EXPIRE_PASSWD
return '';
}
if (($accountControl & 524288) != 0) {
// ADS_UF_PASSWORD_EXPIRED
return _("Your password has expired");
}
$maxdays = $this->_getMaxPasswd();
if ($maxdays === false) {
return '';
}
$today = time();
$lastset = $pwdlastset - $maxdays;
$toexpire = floor(($this->_convertWinTimeToUnix($lastset) - $today) / 86400);
if ($toexpire < 0) {
return _("Your password has expired");
}
if ($toexpire < 14) {
// Two weeks.
return sprintf(_("%d days until your password expires."), $toexpire);
}
} catch (Horde_Ldap_Exception $e) {
}
// OpenLDAP.
try {
$shadowmax = $information->getValue('shadowmax', 'single');
$shadowlastchange = $information->getValue('shadowlastchange', 'single');
$shadowwarning = $information->getValue('shadowwarning', 'single');
$today = floor(time() / 86400);
$warnday = $shadowlastchange + $shadowmax - $shadowwarning;
$toexpire = $shadowlastchange + $shadowmax - $today;
if ($today >= $warnday) {
return sprintf(_("%d days until your password expires."), $toexpire);
}
} catch (Horde_Ldap_Exception $e) {
}
return '';
}
}
|