2003-06-01 10:02:44 +00:00
|
|
|
<?php
|
2003-04-23 15:47:00 +00:00
|
|
|
/*
|
|
|
|
$Id$
|
|
|
|
|
2009-10-27 18:47:12 +00:00
|
|
|
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
|
2006-03-03 17:30:35 +00:00
|
|
|
Copyright (C) 2003 - 2006 Tilo Lutz
|
2014-01-12 19:52:14 +00:00
|
|
|
2009 - 2014 Roland Gruber
|
2003-04-23 15:47:00 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2004-09-16 17:25:42 +00:00
|
|
|
/**
|
|
|
|
* This provides several helper function for the account modules.
|
|
|
|
*
|
|
|
|
* @author Tilo Lutz
|
|
|
|
* @author Roland Gruber
|
|
|
|
*
|
2005-07-21 10:33:02 +00:00
|
|
|
* @package lib
|
2004-09-16 17:25:42 +00:00
|
|
|
*/
|
2003-12-29 14:07:06 +00:00
|
|
|
|
2004-12-07 20:30:29 +00:00
|
|
|
|
2005-08-12 13:18:05 +00:00
|
|
|
/**
|
|
|
|
* This function will return all values from $array without values of $values.
|
|
|
|
*
|
|
|
|
* @param array $values list of values which should be removed
|
|
|
|
* @param array $array list of original values
|
|
|
|
* @return array list of remaining values
|
2003-10-19 17:04:49 +00:00
|
|
|
*/
|
|
|
|
function array_delete($values, $array) {
|
|
|
|
// Loop for every entry and check if it should be removed
|
|
|
|
if (is_array($array)) {
|
2003-12-27 11:21:00 +00:00
|
|
|
$return = array();
|
2003-10-19 17:04:49 +00:00
|
|
|
foreach ($array as $array_value)
|
|
|
|
if (!@in_array($array_value, $values))
|
|
|
|
$return[] = $array_value;
|
|
|
|
return $return;
|
|
|
|
}
|
2005-08-12 13:18:05 +00:00
|
|
|
else return array();
|
|
|
|
}
|
2003-09-11 16:55:57 +00:00
|
|
|
|
|
|
|
|
2006-07-17 17:45:14 +00:00
|
|
|
/**
|
|
|
|
* Checks if a string exists in an array, ignoring case.
|
2012-07-15 12:05:47 +00:00
|
|
|
*
|
|
|
|
* @param String $needle search string
|
|
|
|
* @param array $haystack array
|
2006-07-17 17:45:14 +00:00
|
|
|
*/
|
2009-11-25 23:07:28 +00:00
|
|
|
function in_array_ignore_case($needle, $haystack) {
|
2006-07-17 17:45:14 +00:00
|
|
|
if( ! is_array( $haystack ) )
|
|
|
|
return false;
|
|
|
|
if( ! is_string( $needle ) )
|
|
|
|
return false;
|
|
|
|
foreach( $haystack as $element )
|
|
|
|
if( is_string( $element ) && 0 == strcasecmp( $needle, $element ) )
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-08-12 13:18:05 +00:00
|
|
|
/**
|
|
|
|
* This function will return the days from 1.1.1970 until now.
|
|
|
|
*
|
|
|
|
* @return number of days
|
|
|
|
*/
|
2003-10-19 17:04:49 +00:00
|
|
|
function getdays() {
|
2003-04-23 15:47:00 +00:00
|
|
|
$days = time() / 86400;
|
|
|
|
settype($days, 'integer');
|
|
|
|
return $days;
|
|
|
|
}
|
|
|
|
|
2005-08-12 13:18:05 +00:00
|
|
|
/**
|
|
|
|
* Takes a list of Samba flags and creates the corresponding flag string.
|
|
|
|
*
|
|
|
|
* @param array $input is an array of Samba flags (e.g. X or D)
|
|
|
|
* @return string Samba flag string
|
2003-10-19 17:04:49 +00:00
|
|
|
*/
|
2003-11-17 16:02:23 +00:00
|
|
|
function smbflag($input) {
|
2003-10-19 17:04:49 +00:00
|
|
|
// Start character
|
2003-04-23 15:47:00 +00:00
|
|
|
$flag = "[";
|
2003-10-19 17:04:49 +00:00
|
|
|
// Add Options
|
2003-11-17 16:02:23 +00:00
|
|
|
if ($input['W']) $flag .= "W"; else $flag .= "U";
|
|
|
|
if ($input['D']) $flag .= "D";
|
|
|
|
if ($input['X']) $flag .= "X";
|
|
|
|
if ($input['N']) $flag .= "N";
|
|
|
|
if ($input['S']) $flag .= "S";
|
|
|
|
if ($input['H']) $flag .= "H";
|
2003-10-19 17:04:49 +00:00
|
|
|
// Expand string to fixed length
|
2003-05-18 09:45:56 +00:00
|
|
|
$flag = str_pad($flag, 12);
|
2003-10-19 17:04:49 +00:00
|
|
|
// End character
|
2003-04-23 15:47:00 +00:00
|
|
|
$flag = $flag. "]";
|
|
|
|
return $flag;
|
2005-08-12 13:18:05 +00:00
|
|
|
}
|
2003-04-23 15:47:00 +00:00
|
|
|
|
2004-06-22 13:05:19 +00:00
|
|
|
/**
|
|
|
|
* Generates the LM hash of a password.
|
|
|
|
*
|
|
|
|
* @param string password original password
|
|
|
|
* @return string password hash
|
|
|
|
*/
|
|
|
|
function lmPassword($password) {
|
2007-11-15 11:02:57 +00:00
|
|
|
// Needed to calculate Samba passwords
|
|
|
|
include_once("createntlm.inc");
|
|
|
|
// get hash
|
2004-12-07 20:30:29 +00:00
|
|
|
$hash = new smbHash();
|
|
|
|
return $hash->lmhash($password);
|
2004-06-22 13:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the NT hash of a password.
|
|
|
|
*
|
|
|
|
* @param string password original password
|
|
|
|
* @return string password hash
|
|
|
|
*/
|
|
|
|
function ntPassword($password) {
|
2007-11-15 11:02:57 +00:00
|
|
|
// Needed to calculate Samba passwords
|
|
|
|
include_once("createntlm.inc");
|
|
|
|
// get hash
|
2004-12-07 20:30:29 +00:00
|
|
|
$hash = new smbHash();
|
|
|
|
return $hash->nthash($password);
|
2004-06-22 13:05:19 +00:00
|
|
|
}
|
|
|
|
|
2004-09-27 15:45:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-08-03 18:49:19 +00:00
|
|
|
/**
|
2012-08-26 17:54:31 +00:00
|
|
|
* Returns the hash value of a plain text password.
|
|
|
|
* @see getSupportedHashTypes()
|
2004-08-03 18:49:19 +00:00
|
|
|
*
|
|
|
|
* @param string $password the password string
|
|
|
|
* @param boolean $enabled marks the hash as enabled/disabled (e.g. by prefixing "!")
|
2012-08-26 17:54:31 +00:00
|
|
|
* @param string $hashType password hash type (CRYPT, CRYPT-SHA512, SHA, SSHA, MD5, SMD5, PLAIN)
|
2004-08-03 18:49:19 +00:00
|
|
|
* @return string the password hash
|
|
|
|
*/
|
|
|
|
function pwd_hash($password, $enabled = true, $hashType = 'SSHA') {
|
|
|
|
// check for empty password
|
|
|
|
if (! $password || ($password == "")) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
$hash = "";
|
|
|
|
switch ($hashType) {
|
|
|
|
case 'CRYPT':
|
|
|
|
$hash = "{CRYPT}" . crypt($password);
|
|
|
|
break;
|
2012-08-26 17:54:31 +00:00
|
|
|
case 'CRYPT-SHA512':
|
|
|
|
$hash = "{CRYPT}" . crypt($password, '$6$' . generateSalt(16));
|
|
|
|
break;
|
2004-08-03 18:49:19 +00:00
|
|
|
case 'MD5':
|
2012-01-11 18:54:35 +00:00
|
|
|
$hash = "{MD5}" . base64_encode(convertHex2bin(md5($password)));
|
2004-08-03 18:49:19 +00:00
|
|
|
break;
|
|
|
|
case 'SMD5':
|
2012-08-26 17:54:31 +00:00
|
|
|
$salt = generateSalt(4);
|
2012-01-11 18:54:35 +00:00
|
|
|
$hash = "{SMD5}" . base64_encode(convertHex2bin(md5($password . $salt)) . $salt);
|
2004-08-03 18:49:19 +00:00
|
|
|
break;
|
|
|
|
case 'SHA':
|
2012-01-11 18:54:35 +00:00
|
|
|
$hash = "{SHA}" . base64_encode(convertHex2bin(sha1($password)));
|
2004-08-03 18:49:19 +00:00
|
|
|
break;
|
|
|
|
case 'PLAIN':
|
|
|
|
$hash = $password;
|
|
|
|
break;
|
2008-02-25 20:54:11 +00:00
|
|
|
case 'SSHA':
|
|
|
|
default: // use SSHA if the setting is invalid
|
2012-08-26 17:54:31 +00:00
|
|
|
$salt = generateSalt(4);
|
2012-01-11 18:54:35 +00:00
|
|
|
$hash = "{SSHA}" . base64_encode(convertHex2bin(sha1($password . $salt)) . $salt);
|
2008-02-25 20:54:11 +00:00
|
|
|
break;
|
2004-08-03 18:49:19 +00:00
|
|
|
}
|
|
|
|
// enable/disable password
|
|
|
|
if (! $enabled) return pwd_disable($hash);
|
|
|
|
else return $hash;
|
|
|
|
}
|
|
|
|
|
2012-08-26 17:54:31 +00:00
|
|
|
/**
|
|
|
|
* Returns the list of supported hash types (e.g. SSHA).
|
|
|
|
*
|
|
|
|
* @return array hash types
|
|
|
|
*/
|
|
|
|
function getSupportedHashTypes() {
|
|
|
|
if (version_compare(phpversion(), '5.3.2') < 0) {
|
|
|
|
// CRYPT-SHA512 requires PHP 5.3.2 or higher
|
|
|
|
return array('CRYPT', 'SHA', 'SSHA', 'MD5', 'SMD5', 'PLAIN');
|
|
|
|
}
|
|
|
|
return array('CRYPT', 'CRYPT-SHA512', 'SHA', 'SSHA', 'MD5', 'SMD5', 'PLAIN');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates a password salt of the given legth.
|
|
|
|
*
|
|
|
|
* @param int $len salt length
|
|
|
|
* @return String the salt string
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function generateSalt($len) {
|
|
|
|
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./';
|
|
|
|
$salt = '';
|
|
|
|
for ($i = 0; $i < $len; $i++) {
|
2013-07-21 11:34:31 +00:00
|
|
|
$pos= getRandomNumber() % strlen($chars);
|
2012-08-26 17:54:31 +00:00
|
|
|
$salt .= $chars{$pos};
|
|
|
|
}
|
|
|
|
return $salt;
|
|
|
|
}
|
|
|
|
|
2004-09-16 17:25:42 +00:00
|
|
|
/**
|
|
|
|
* Marks an password hash as enabled and returns the new hash string
|
|
|
|
*
|
|
|
|
* @param string $hash hash value to enable
|
|
|
|
* @return string enabled password hash
|
|
|
|
*/
|
|
|
|
function pwd_enable($hash) {
|
|
|
|
// check if password is disabled (old wrong LAM method)
|
|
|
|
if ((substr($hash, 0, 2) == "!{") || ((substr($hash, 0, 2) == "*{"))) {
|
|
|
|
return substr($hash, 1, strlen($hash));
|
|
|
|
}
|
|
|
|
// check for "!" or "*" at beginning of password hash
|
|
|
|
else {
|
|
|
|
if (substr($hash, 0, 1) == "{") {
|
|
|
|
$pos = strpos($hash, "}");
|
|
|
|
if ((substr($hash, $pos + 1, 1) == "!") || (substr($hash, $pos + 1, 1) == "*")) {
|
|
|
|
// enable hash
|
|
|
|
return substr($hash, 0, $pos + 1) . substr($hash, $pos + 2, strlen($hash));
|
|
|
|
}
|
|
|
|
else return $hash; // not disabled
|
|
|
|
}
|
|
|
|
else return $hash; // password is plain text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks an password hash as disabled and returns the new hash string
|
|
|
|
*
|
|
|
|
* @param string $hash hash value to disable
|
|
|
|
* @return string disabled hash value
|
|
|
|
*/
|
|
|
|
function pwd_disable($hash) {
|
|
|
|
// check if password is disabled (old wrong LAM method)
|
|
|
|
if ((substr($hash, 0, 2) == "!{") || ((substr($hash, 0, 2) == "*{"))) {
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
// check for "!" or "*" at beginning of password hash
|
|
|
|
else {
|
|
|
|
if (substr($hash, 0, 1) == "{") {
|
|
|
|
$pos = strpos($hash, "}");
|
|
|
|
if ((substr($hash, $pos + 1, 1) == "!") || (substr($hash, $pos + 1, 1) == "*")) {
|
|
|
|
// hash already disabled
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
else return substr($hash, 0, $pos + 1) . "!" . substr($hash, $pos + 1, strlen($hash)); // not disabled
|
|
|
|
}
|
|
|
|
else return $hash; // password is plain text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-01 12:17:31 +00:00
|
|
|
/**
|
|
|
|
* Checks if a Unix password can be locked.
|
|
|
|
* This checks if the password is not plain text but e.g. contains {SSHA}.
|
|
|
|
*
|
|
|
|
* @param String $password password value
|
|
|
|
* @return boolean can be locked
|
|
|
|
*/
|
|
|
|
function pwd_is_lockable($password) {
|
|
|
|
if (($password == null) || (strlen($password) < 5)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ((substr($password, 0, 1) == "{") || (substr($password, 1, 1) == "{")) && (strpos($password, "}") > 3);
|
|
|
|
}
|
|
|
|
|
2004-09-16 17:25:42 +00:00
|
|
|
/**
|
|
|
|
* Checks if a password hash is enabled/disabled
|
|
|
|
*
|
|
|
|
* @param string $hash password hash to check
|
|
|
|
* @return boolean true if the password is marked as enabled
|
|
|
|
*/
|
|
|
|
function pwd_is_enabled($hash) {
|
|
|
|
// disabled passwords have a "!" or "*" at the beginning (old wrong LAM method)
|
|
|
|
if ((substr($hash, 0, 2) == "!{") || ((substr($hash, 0, 2) == "*{"))) return false;
|
|
|
|
if (substr($hash, 0, 1) == "{") {
|
|
|
|
$pos = strrpos($hash, "}");
|
|
|
|
// check if hash starts with "!" or "*"
|
|
|
|
if ((substr($hash, $pos + 1, 1) == "!") || (substr($hash, $pos + 1, 1) == "*")) return false;
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
}
|
2004-08-03 18:49:19 +00:00
|
|
|
|
2007-11-05 18:16:29 +00:00
|
|
|
/**
|
|
|
|
* Generates a random password with 12 digits.
|
|
|
|
*
|
|
|
|
* @return String password
|
|
|
|
*/
|
|
|
|
function generateRandomPassword() {
|
|
|
|
$list = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_';
|
|
|
|
$password = '';
|
2011-05-10 18:22:48 +00:00
|
|
|
$length = $_SESSION['cfgMain']->passwordMinLength;
|
|
|
|
if ($length < 12) {
|
|
|
|
$length = 12;
|
|
|
|
}
|
|
|
|
$isOk = false;
|
|
|
|
for ($x = 0; $x < 10000; $x++) {
|
|
|
|
$password = '';
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
2013-07-21 11:34:31 +00:00
|
|
|
$rand = getRandomNumber() % 65;
|
2011-05-10 18:22:48 +00:00
|
|
|
$password .= $list[$rand];
|
|
|
|
}
|
2014-04-17 19:26:08 +00:00
|
|
|
if (checkPasswordStrength($password, null, null) === true) {
|
2011-05-10 18:22:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-05 18:16:29 +00:00
|
|
|
}
|
|
|
|
return $password;
|
|
|
|
}
|
|
|
|
|
2013-07-26 19:04:56 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given password mathes the crypto hash.
|
|
|
|
*
|
|
|
|
* @param String type hash type (must be one of getSupportedHashTypes())
|
|
|
|
* @param unknown_type $hash password hash value
|
|
|
|
* @param unknown_type $password plain text password to check
|
|
|
|
* @see getSupportedHashTypes()
|
|
|
|
*/
|
|
|
|
function checkPasswordHash($type, $hash, $password) {
|
|
|
|
switch ($type) {
|
|
|
|
case 'SSHA':
|
|
|
|
$bin = base64_decode($hash);
|
|
|
|
$salt = substr($bin, 20);
|
|
|
|
$pwdHash = base64_encode(convertHex2bin(sha1($password . $salt)) . $salt);
|
|
|
|
return (strcmp($hash, $pwdHash) == 0);
|
|
|
|
break;
|
|
|
|
case 'SHA':
|
|
|
|
return (strcmp($hash, base64_encode(convertHex2bin(sha1($password)))) == 0);
|
|
|
|
break;
|
|
|
|
case 'SMD5':
|
|
|
|
$bin = base64_decode($hash);
|
|
|
|
$salt = substr($bin, 16);
|
|
|
|
$pwdHash = base64_encode(convertHex2bin(md5($password . $salt)) . $salt);
|
|
|
|
return (strcmp($hash, $pwdHash) == 0);
|
|
|
|
break;
|
|
|
|
case 'MD5':
|
|
|
|
return (strcmp($hash, base64_encode(convertHex2bin(md5($password)))) == 0);
|
|
|
|
break;
|
|
|
|
case 'CRYPT':
|
|
|
|
$parts = explode('$', $hash);
|
|
|
|
if (sizeof($parts) == 4) {
|
|
|
|
$version = $parts[1];
|
|
|
|
$salt = $parts[2];
|
|
|
|
$pwdHash = crypt($password, '$' . $version . '$' . $salt);
|
|
|
|
return (strcmp($hash, $pwdHash) == 0);
|
|
|
|
}
|
|
|
|
elseif (sizeof($parts) == 5) {
|
|
|
|
$version = $parts[1];
|
|
|
|
$rounds = $parts[2];
|
|
|
|
$salt = $parts[3];
|
|
|
|
$pwdHash = crypt($password, '$' . $version . '$' . $rounds . '$' . $salt);
|
|
|
|
return (strcmp($hash, $pwdHash) == 0);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-01-01 16:30:05 +00:00
|
|
|
/**
|
|
|
|
* Returns an array with all Samba 3 domain entries under the given suffix
|
|
|
|
*
|
2012-07-15 12:05:47 +00:00
|
|
|
* @param handle LDAP handle (if null then $_SESSION['ldap']->server() is used)
|
|
|
|
* @param String $suffix LDAP suffix to search (if null then $_SESSION['config']->get_Suffix('smbDomain') is used)
|
2006-01-01 16:30:05 +00:00
|
|
|
* @return array list of samba3domain objects
|
|
|
|
*/
|
2008-12-09 18:52:39 +00:00
|
|
|
function search_domains($server = null, $suffix = null) {
|
|
|
|
if ($suffix == null) {
|
|
|
|
$suffix = $_SESSION['config']->get_Suffix('smbDomain');
|
|
|
|
}
|
2006-01-01 16:30:05 +00:00
|
|
|
$ret = array();
|
|
|
|
$attr = array("DN", "sambaDomainName", "sambaSID", "sambaNextRid", "sambaNextGroupRid",
|
2008-12-09 18:52:39 +00:00
|
|
|
"sambaNextUserRid", "sambaAlgorithmicRidBase", 'sambaMinPwdAge', 'sambaMaxPwdAge');
|
|
|
|
if ($server == null) {
|
|
|
|
$server = $_SESSION['ldap']->server();
|
|
|
|
}
|
2010-02-06 11:52:48 +00:00
|
|
|
$units = searchLDAPByAttribute(null, null, 'sambaDomain', $attr, array('smbDomain'));
|
|
|
|
// extract attributes
|
|
|
|
for ($i = 0; $i < sizeof($units); $i++) {
|
|
|
|
$ret[$i] = new samba3domain();
|
|
|
|
$ret[$i]->dn = $units[$i]['dn'];
|
|
|
|
$ret[$i]->name = $units[$i]['sambadomainname'][0];
|
|
|
|
$ret[$i]->SID = $units[$i]['sambasid'][0];
|
|
|
|
if (isset($units[$i]['sambanextrid'][0])) $ret[$i]->nextRID = $units[$i]['sambanextrid'][0];
|
|
|
|
if (isset($units[$i]['sambanextgrouprid'][0])) $ret[$i]->nextGroupRID = $units[$i]['sambanextgrouprid'][0];
|
|
|
|
if (isset($units[$i]['sambanextuserrid'][0])) $ret[$i]->nextUserRID = $units[$i]['sambanextuserrid'][0];
|
|
|
|
if (isset($units[$i]['sambaalgorithmicridbase'][0])) $ret[$i]->RIDbase = $units[$i]['sambaalgorithmicridbase'][0];
|
|
|
|
if (isset($units[$i]['sambaminpwdage'][0])) $ret[$i]->minPwdAge = $units[$i]['sambaminpwdage'][0];
|
|
|
|
if (isset($units[$i]['sambamaxpwdage'][0])) $ret[$i]->maxPwdAge = $units[$i]['sambamaxpwdage'][0];
|
2004-09-19 08:35:01 +00:00
|
|
|
}
|
2006-01-01 16:30:05 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
2004-09-19 08:35:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a Samba 3 domain entry
|
|
|
|
*
|
|
|
|
* @package modules
|
|
|
|
*/
|
|
|
|
class samba3domain {
|
|
|
|
|
|
|
|
/** DN */
|
2007-10-13 17:28:37 +00:00
|
|
|
public $dn;
|
2004-09-19 08:35:01 +00:00
|
|
|
|
|
|
|
/** Domain name */
|
2007-10-13 17:28:37 +00:00
|
|
|
public $name;
|
2004-09-19 08:35:01 +00:00
|
|
|
|
|
|
|
/** Domain SID */
|
2007-10-13 17:28:37 +00:00
|
|
|
public $SID;
|
2004-09-19 08:35:01 +00:00
|
|
|
|
|
|
|
/** Next RID */
|
2007-10-13 17:28:37 +00:00
|
|
|
public $nextRID;
|
2004-09-19 08:35:01 +00:00
|
|
|
|
|
|
|
/** Next user RID */
|
2007-10-13 17:28:37 +00:00
|
|
|
public $nextUserRID;
|
2004-09-19 08:35:01 +00:00
|
|
|
|
|
|
|
/** Next group RID */
|
2007-10-13 17:28:37 +00:00
|
|
|
public $nextGroupRID;
|
2004-09-19 08:35:01 +00:00
|
|
|
|
|
|
|
/** RID base to calculate RIDs, default 1000 */
|
2007-10-13 17:28:37 +00:00
|
|
|
public $RIDbase = 1000;
|
2008-12-09 18:52:39 +00:00
|
|
|
|
|
|
|
/** seconds after the password can be changed */
|
|
|
|
public $minPwdAge;
|
|
|
|
|
|
|
|
/** seconds after the password must be changed */
|
|
|
|
public $maxPwdAge;
|
2004-09-19 08:35:01 +00:00
|
|
|
}
|
|
|
|
|
2005-08-12 13:18:05 +00:00
|
|
|
/**
|
|
|
|
* Checks if a given value matches the selected regular expression.
|
|
|
|
*
|
|
|
|
* @param string $argument value to check
|
|
|
|
* @param string $regexp pattern name
|
|
|
|
* @return boolean true if matches, otherwise false
|
|
|
|
*/
|
2004-09-24 16:32:46 +00:00
|
|
|
function get_preg($argument, $regexp) {
|
2013-11-29 19:47:29 +00:00
|
|
|
/* Bug in php preg_match doesn't work correct with utf8 */
|
2014-02-02 12:36:12 +00:00
|
|
|
$language = $_SESSION['language'];
|
|
|
|
$language2 = explode ('.', $language);
|
2004-10-11 12:20:27 +00:00
|
|
|
setlocale(LC_ALL, $language2[0]);
|
2013-11-29 19:47:29 +00:00
|
|
|
// workaround for buggy PHP with Turkish
|
2014-02-02 12:36:12 +00:00
|
|
|
if (($language == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
|
2013-11-29 19:47:29 +00:00
|
|
|
setlocale(LC_CTYPE, 'en_GB');
|
|
|
|
}
|
2004-09-23 11:30:20 +00:00
|
|
|
// First we check "positive" cases
|
2004-09-24 16:32:46 +00:00
|
|
|
$pregexpr = '';
|
2004-09-23 11:30:20 +00:00
|
|
|
switch ($regexp) {
|
2005-08-12 13:18:05 +00:00
|
|
|
case 'password':
|
2013-01-13 10:40:47 +00:00
|
|
|
$pregexpr = '/^([[:alnum:]\\^\\ \\|\\#\\*\\,\\.\\;\\:\\_\\+\\!\\%\\&\\/\\?\\{\\(\\)\\}\\[\\]\\$§°@=-])*$/u';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
2009-11-22 11:37:31 +00:00
|
|
|
case 'groupname': // all letters, numbers, space and ._- are allowed characters
|
|
|
|
case 'username':
|
2009-02-01 16:18:05 +00:00
|
|
|
case 'hostname':
|
2014-07-03 20:01:44 +00:00
|
|
|
$pregexpr = '/^([[:alnum:]%#@\\.\\ \\_\\$-])+$/u';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
2012-02-19 14:50:57 +00:00
|
|
|
case 'krbUserName':
|
2013-11-08 18:14:06 +00:00
|
|
|
$pregexpr = '/^([[:alnum:]#@\\/\\.\\ \\_\\$-])+$/u';
|
2012-02-19 14:50:57 +00:00
|
|
|
break;
|
2010-04-03 13:31:13 +00:00
|
|
|
case 'hostObject':
|
2010-04-30 21:08:23 +00:00
|
|
|
$pregexpr = '/^[!]?([[:alnum:]@\\.\\ \\_\\$\\*-])+$/u';
|
|
|
|
break;
|
2004-09-27 16:51:17 +00:00
|
|
|
case 'usernameList': // comma separated list of user names
|
|
|
|
case 'groupnameList': // comma separated list of group names
|
2014-07-03 20:01:44 +00:00
|
|
|
$pregexpr = '/^([[:alnum:]%#@\\.\\ \\_-])+(,([[:alnum:]%#@\\.\\ \\_-])+)*$/u';
|
2004-09-27 16:51:17 +00:00
|
|
|
break;
|
2005-12-10 09:42:55 +00:00
|
|
|
case 'realname': // Allow all but \, <, >, =, $, ?
|
2005-12-10 09:47:09 +00:00
|
|
|
case 'cn':
|
2006-02-23 08:37:37 +00:00
|
|
|
$pregexpr = '/^[^\\\<>=\\$\\?]+(\\$)?$/';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
2006-06-29 11:24:55 +00:00
|
|
|
case "telephone": // Allow letters, numbers, space, brackets, /-+.
|
|
|
|
$pregexpr = '/^(\\+)*([0-9a-zA-Z\\.\\ \\(\\)\\/-])*$/';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
|
|
|
case "email":
|
2013-10-29 18:33:02 +00:00
|
|
|
$pregexpr = '/^([0-9a-zA-Z\'!~#+*%\\$\\/\\._-])+[@]([0-9a-zA-Z-])+([.]([0-9a-zA-Z-])+)*$/';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
2011-05-17 16:02:31 +00:00
|
|
|
case "emailWithName":
|
2013-10-29 18:33:02 +00:00
|
|
|
$pregexpr = '/^([[:alnum:] \'!~#+*%\\$\\(\\)_-])+ <([0-9a-zA-Z\'!~#+*%\\$\\/\\._-])+[@]([0-9a-zA-Z-])+([.]([0-9a-zA-Z-])+)*>$/u';
|
2011-05-17 16:02:31 +00:00
|
|
|
break;
|
2005-09-20 14:40:11 +00:00
|
|
|
case "mailLocalAddress":
|
2010-09-04 13:51:25 +00:00
|
|
|
$pregexpr = '/^([0-9a-zA-Z+\\/\\._-])+([@]([0-9a-zA-Z-])+([.]([0-9a-zA-Z-])+)*)?$/';
|
2005-09-20 14:40:11 +00:00
|
|
|
break;
|
2013-08-29 16:44:58 +00:00
|
|
|
case 'kolabEmailPrefix':
|
|
|
|
$pregexpr = '/^([-])?([0-9a-zA-Z+\\/\\._-])*([@]([0-9a-zA-Z\\.-])*)?$/';
|
|
|
|
break;
|
2011-05-10 18:22:48 +00:00
|
|
|
case "postalAddress": // Allow all but \, <, >, =, ?
|
|
|
|
$pregexpr = '/^[^\\\<>=\\?]*$/';
|
|
|
|
break;
|
|
|
|
case "postalCode": // Allow all but \, <, >, =, ?
|
2005-12-10 09:42:55 +00:00
|
|
|
case "street":
|
|
|
|
case "title":
|
|
|
|
case "employeeType":
|
2007-06-11 18:17:30 +00:00
|
|
|
case "businessCategory":
|
2005-12-10 09:42:55 +00:00
|
|
|
$pregexpr = '/^[^\\\<>=\\$\\?]*$/';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
2004-09-26 13:48:52 +00:00
|
|
|
case "homeDirectory": // Homapath, /path/......
|
2010-09-11 11:42:05 +00:00
|
|
|
case "filePath":
|
2008-01-29 18:22:36 +00:00
|
|
|
$pregexpr = '/^([\/]([[:alnum:]@\\$\\.\\ \\_-])+)+(\/)?$/u';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
|
|
|
case "digit": // Normal number
|
|
|
|
$pregexpr = '/^[[:digit:]]*$/';
|
|
|
|
break;
|
2013-08-20 18:56:52 +00:00
|
|
|
case "float": // float value
|
|
|
|
$pregexpr = '/^[[:digit:]]+(\\.[[:digit:]]+)?$/';
|
|
|
|
break;
|
2004-09-24 16:32:46 +00:00
|
|
|
case "UNC": // UNC Path, e.g. \\server\share\folder\...
|
2005-07-11 19:01:06 +00:00
|
|
|
$pregexpr = '/^((([\\\][\\\])|(%))([a-zA-Z0-9%\\.-])+)([\\\]([[:alnum:]%\\.\\$\\ \\_-])+)+$/u';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
|
|
|
case "logonscript": // path to login-script. normal unix file
|
2009-03-04 17:31:31 +00:00
|
|
|
$pregexpr = '/^(([\/\\\])*([[:alnum:]%\\.\\ \\$\\_-])+([\/\\\]([[:alnum:]%\\.\\ \\$\\_-])+)*((\\.bat)|(\\.cmd)|(\\.exe)|(\\.vbs)))*$/u';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
|
|
|
case "workstations": // comma separated list with windows-hosts
|
|
|
|
$pregexpr = '/^(([a-zA-Z0-9\\.\\_-])+(,[a-zA-Z0-9\\.\\_-])*)*$/';
|
|
|
|
break;
|
|
|
|
case "domainname": // Windows Domainname
|
2006-01-01 16:30:05 +00:00
|
|
|
$pregexpr = '/^([A-Za-z0-9\\.\\_-])+$/';
|
2004-09-26 13:48:52 +00:00
|
|
|
break;
|
|
|
|
case "unixhost": // Unix hosts
|
2006-05-05 10:28:23 +00:00
|
|
|
$pregexpr = '/^([a-z0-9,\\.\\*_-])*$/';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
|
|
|
case 'digit2': // Same as digit but also -1
|
2004-10-10 11:21:54 +00:00
|
|
|
$pregexpr = '/^(([-][1])|([[:digit:]]*))$/';
|
2004-09-24 16:32:46 +00:00
|
|
|
break;
|
2004-10-16 11:30:08 +00:00
|
|
|
case 'gecos':
|
2010-10-08 19:00:35 +00:00
|
|
|
$pregexpr = '/^[[:alnum:] \\._-]+([,][[:alnum:] \\._-]+)*$/u';
|
2004-10-16 11:30:08 +00:00
|
|
|
break;
|
2004-11-01 11:50:42 +00:00
|
|
|
case 'macAddress':
|
|
|
|
$pregexpr = '/^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/';
|
|
|
|
break;
|
2012-11-25 11:01:08 +00:00
|
|
|
case 'date': // 31-12-2012
|
2006-02-12 11:03:20 +00:00
|
|
|
$pregexpr = '/^((0?[1-9])|([1-2][0-9])|30|31)\\-((0?[1-9])|(1[0-2]))\\-[1-3][0-9][0-9][0-9]$/';
|
2004-11-08 19:25:50 +00:00
|
|
|
break;
|
2007-07-02 18:23:13 +00:00
|
|
|
case 'date2':
|
|
|
|
$pregexpr = '/^((0[1-9])|([1-2][0-9])|30|31)\\.((0[1-9])|(1[0-2]))\\.[1-3][0-9][0-9][0-9]$/';
|
|
|
|
break;
|
2004-11-28 19:44:46 +00:00
|
|
|
case 'sambaLogonHours':
|
|
|
|
$pregexpr = '/^[0-9a-fA-F]{42}$/';
|
|
|
|
break;
|
2004-12-29 08:49:09 +00:00
|
|
|
case 'DNSname':
|
|
|
|
$pregexpr = '/^[0-9a-zA-Z_-]+(\\.[0-9a-zA-Z_-]+)*$/';
|
|
|
|
break;
|
2004-12-31 13:50:30 +00:00
|
|
|
case 'nis_alias':
|
2007-03-05 16:42:58 +00:00
|
|
|
$pregexpr = '/^([[:alnum:]@\\.\\ \\_-])+$/u';
|
2005-01-04 20:14:48 +00:00
|
|
|
break;
|
2011-04-19 17:47:41 +00:00
|
|
|
case 'nis_recipient':
|
|
|
|
$pregexpr = '/^([[:alnum:]+@\\.\\ \\_-])+$/u';
|
|
|
|
break;
|
2005-04-07 13:10:15 +00:00
|
|
|
case 'country': // Allow all letters and space
|
|
|
|
$pregexpr = '/^[[:alpha:]]([[:alpha:] ])+$/u';
|
|
|
|
break;
|
2005-07-02 12:03:49 +00:00
|
|
|
case 'dn': // LDAP DN
|
|
|
|
$pregexpr = '/^([^=,]+=[^=,]+)(,([^=,]+=[^=,]+))*$/';
|
|
|
|
break;
|
2006-01-01 16:30:05 +00:00
|
|
|
case 'domainSID': // Samba domain SID
|
|
|
|
$pregexpr = "/^S\\-[0-9]\\-[0-9]\\-[0-9]{2,2}\\-[0-9]+\\-[0-9]+\\-[0-9]+$/";
|
2010-04-30 21:08:23 +00:00
|
|
|
break;
|
2010-01-04 17:51:56 +00:00
|
|
|
case 'ip': // IP address
|
|
|
|
$pregexpr = '/^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}$/';
|
|
|
|
break;
|
2013-10-05 14:54:56 +00:00
|
|
|
case 'ip6': // IPv6 address (only basic check)
|
|
|
|
$pregexpr = '/^[0-9a-f:]+$/i';
|
|
|
|
break;
|
2010-05-07 19:10:46 +00:00
|
|
|
case 'ascii': // ASCII
|
|
|
|
$pregexpr = '/^[' . chr(1) . '-' . chr(128) . ']*$/';
|
|
|
|
break;
|
2012-11-25 11:01:08 +00:00
|
|
|
case 'objectClass':
|
|
|
|
$pregexpr = '/^[[:alnum:]_]+$/';
|
|
|
|
break;
|
2004-09-23 11:30:20 +00:00
|
|
|
}
|
2004-09-24 16:32:46 +00:00
|
|
|
if ($pregexpr!='')
|
2004-10-11 12:20:27 +00:00
|
|
|
if (preg_match($pregexpr, $argument)) {
|
2013-11-29 19:47:29 +00:00
|
|
|
/* Bug in php preg_match doesn't work correct with utf8 */
|
2014-02-02 12:36:12 +00:00
|
|
|
setlocale(LC_ALL, $language);
|
2013-11-29 19:47:29 +00:00
|
|
|
// workaround for buggy PHP with Turkish
|
2014-02-02 12:36:12 +00:00
|
|
|
if (($language == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
|
2013-11-29 19:47:29 +00:00
|
|
|
setlocale(LC_CTYPE, 'en_GB');
|
|
|
|
}
|
2004-10-11 12:20:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-09-23 11:30:20 +00:00
|
|
|
// Now we check "negative" cases, characters which are not allowed
|
2004-09-24 16:32:46 +00:00
|
|
|
$pregexpr = '';
|
2004-09-23 11:30:20 +00:00
|
|
|
switch ($regexp) {
|
2004-09-26 13:48:52 +00:00
|
|
|
case "!lower":
|
|
|
|
$pregexpr = '/[[:lower:]]/';
|
|
|
|
break;
|
|
|
|
case "!upper":
|
|
|
|
$pregexpr = '/[[:upper:]]/';
|
|
|
|
break;
|
|
|
|
case "!digit":
|
|
|
|
$pregexpr = '/[[:digit:]]/';
|
|
|
|
break;
|
2004-09-23 11:30:20 +00:00
|
|
|
}
|
2004-09-24 16:32:46 +00:00
|
|
|
if ($pregexpr!='')
|
2004-10-11 12:20:27 +00:00
|
|
|
if (!preg_match($pregexpr, $argument)) {
|
2013-11-29 19:47:29 +00:00
|
|
|
/* Bug in php preg_match doesn't work correct with utf8 */
|
2014-02-02 12:36:12 +00:00
|
|
|
setlocale(LC_ALL, $language);
|
2013-11-29 19:47:29 +00:00
|
|
|
// workaround for buggy PHP with Turkish
|
2014-02-02 12:36:12 +00:00
|
|
|
if (($language == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
|
2013-11-29 19:47:29 +00:00
|
|
|
setlocale(LC_CTYPE, 'en_GB');
|
|
|
|
}
|
2004-10-11 12:20:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-11-29 19:47:29 +00:00
|
|
|
/* Bug in php preg_match doesn't work correct with utf8 */
|
2014-02-02 12:36:12 +00:00
|
|
|
setlocale(LC_ALL, $language);
|
2013-11-29 19:47:29 +00:00
|
|
|
// workaround for buggy PHP with Turkish
|
2014-02-02 12:36:12 +00:00
|
|
|
if (($language == 'tr_TR.utf8') && (version_compare(phpversion(), '5.5') < 0)) {
|
2013-11-29 19:47:29 +00:00
|
|
|
setlocale(LC_CTYPE, 'en_GB');
|
|
|
|
}
|
2004-09-23 11:30:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-04 17:21:29 +00:00
|
|
|
/**
|
|
|
|
* Escapes any special characters in an LDAP DN.
|
|
|
|
*
|
|
|
|
* @param String $dn DN
|
|
|
|
* @return String escaped DN
|
|
|
|
*/
|
|
|
|
function escapeDN($dn) {
|
2009-11-02 19:33:34 +00:00
|
|
|
$dn = preg_replace('/[ ]*,[ ]*/', ',', $dn);
|
2008-09-04 17:21:29 +00:00
|
|
|
return str_replace(
|
|
|
|
array(')', '(', ' ', '*'),
|
|
|
|
array('\\29', '\\28', '\\20', '\\2a'),
|
|
|
|
$dn
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-11-25 23:07:28 +00:00
|
|
|
/**
|
|
|
|
* This will search the given LDAP suffix for all entries which have the given attribute.
|
|
|
|
*
|
|
|
|
* @param String $name attribute name (may be null)
|
|
|
|
* @param String $value attribute value
|
|
|
|
* @param String $objectClass object class (may be null)
|
|
|
|
* @param array $attributes list of attributes to return
|
2010-02-06 11:52:48 +00:00
|
|
|
* @param array $scopes account types
|
2009-11-25 23:07:28 +00:00
|
|
|
* @return array list of found entries
|
|
|
|
*/
|
|
|
|
function searchLDAPByAttribute($name, $value, $objectClass, $attributes, $scopes) {
|
|
|
|
$return = array();
|
|
|
|
// build filter
|
|
|
|
$filter = '';
|
|
|
|
$filterParts = array();
|
|
|
|
if ($name != null) {
|
|
|
|
$filterParts[] = '(' . $name . '=' . $value . ')';
|
|
|
|
}
|
|
|
|
if ($objectClass != null) {
|
|
|
|
$filterParts[] = '(objectClass=' . $objectClass . ')';
|
|
|
|
}
|
|
|
|
if (sizeof($filterParts) == 1) {
|
|
|
|
$filter = $filterParts[0];
|
|
|
|
}
|
|
|
|
elseif (sizeof($filterParts) > 1) {
|
|
|
|
$filter = '(& ' . implode(' ', $filterParts) . ')';
|
|
|
|
}
|
2014-01-18 09:55:56 +00:00
|
|
|
$activeTypes = $_SESSION['config']->get_ActiveTypes();
|
2009-11-25 23:07:28 +00:00
|
|
|
for ($s = 0; $s < sizeof($scopes); $s++) {
|
2014-01-18 09:55:56 +00:00
|
|
|
if (!in_array($scopes[$s], $activeTypes)) {
|
|
|
|
continue; // skip non-active account types
|
|
|
|
}
|
2009-11-25 23:07:28 +00:00
|
|
|
// search LDAP
|
2014-11-29 17:40:39 +00:00
|
|
|
$entries = searchLDAPPaged($_SESSION['ldap']->server(), escapeDN($_SESSION['config']->get_Suffix($scopes[$s])),
|
|
|
|
$filter, $attributes, 0, $_SESSION['config']->get_searchLimit());
|
2013-10-29 18:33:02 +00:00
|
|
|
if (ldap_errno($_SESSION['ldap']->server()) == 4) {
|
|
|
|
logNewMessage(LOG_WARNING, 'LDAP size limit exeeded. Please increase the limit on your server.');
|
|
|
|
}
|
2014-11-29 17:40:39 +00:00
|
|
|
$return = array_merge($return, $entries);
|
2010-02-06 11:52:48 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will search the given LDAP suffix for all entries which match the given filter.
|
|
|
|
*
|
|
|
|
* @param String $filter
|
|
|
|
* @param array $attributes list of attributes to return
|
|
|
|
* @param array $scopes account types
|
2013-01-09 20:10:33 +00:00
|
|
|
* @param boolean $attrsOnly get only attributes but no values (default: false)
|
2010-02-07 14:04:29 +00:00
|
|
|
* @return array list of found entries
|
2010-02-06 11:52:48 +00:00
|
|
|
*/
|
2013-01-09 20:10:33 +00:00
|
|
|
function searchLDAPByFilter($filter, $attributes, $scopes, $attrsOnly = false) {
|
2010-02-06 11:52:48 +00:00
|
|
|
$return = array();
|
2013-01-09 20:10:33 +00:00
|
|
|
$readAttributesOnly = 0;
|
|
|
|
if ($attrsOnly) {
|
|
|
|
$readAttributesOnly = 1;
|
|
|
|
}
|
2010-02-06 11:52:48 +00:00
|
|
|
for ($s = 0; $s < sizeof($scopes); $s++) {
|
|
|
|
// search LDAP
|
2014-11-29 17:40:39 +00:00
|
|
|
$entries = searchLDAPPaged($_SESSION['ldap']->server(), escapeDN($_SESSION['config']->get_Suffix($scopes[$s])),
|
|
|
|
$filter, $attributes, $readAttributesOnly, $_SESSION['config']->get_searchLimit());
|
2013-10-29 18:33:02 +00:00
|
|
|
if (ldap_errno($_SESSION['ldap']->server()) == 4) {
|
|
|
|
logNewMessage(LOG_WARNING, 'LDAP size limit exeeded. Please increase the limit on your server.');
|
|
|
|
}
|
2014-11-29 17:40:39 +00:00
|
|
|
$return = array_merge($return, $entries);
|
2009-11-25 23:07:28 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2010-02-07 14:04:29 +00:00
|
|
|
/**
|
|
|
|
* Runs an LDAP search.
|
|
|
|
*
|
|
|
|
* @param String $suffix LDAP suffix
|
|
|
|
* @param String $filter filter
|
|
|
|
* @param array $attributes list of attributes to return
|
|
|
|
* @return array list of found entries
|
|
|
|
*/
|
|
|
|
function searchLDAP($suffix, $filter, $attributes) {
|
2014-11-29 17:40:39 +00:00
|
|
|
$return = searchLDAPPaged($_SESSION['ldap']->server(), escapeDN($suffix), $filter, $attributes,
|
|
|
|
0, $_SESSION['config']->get_searchLimit());
|
2013-10-29 18:33:02 +00:00
|
|
|
if (ldap_errno($_SESSION['ldap']->server()) == 4) {
|
|
|
|
logNewMessage(LOG_WARNING, 'LDAP size limit exeeded. Please increase the limit on your server.');
|
|
|
|
}
|
2014-11-29 17:40:39 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs an LDAP search and uses paging if configured.
|
|
|
|
*
|
|
|
|
* @param handle $server LDAP connection handle
|
|
|
|
* @param String $dn DN
|
|
|
|
* @param String $filter filter
|
|
|
|
* @param array $attributes attribute list
|
|
|
|
* @param boolean $attrsOnly return only attribute names
|
|
|
|
* @param int $limit size limit
|
|
|
|
*/
|
|
|
|
function searchLDAPPaged($server, $dn, $filter, $attributes, $attrsOnly, $limit) {
|
|
|
|
if (empty($_SESSION['config']) || ($_SESSION['config']->getPagedResults() !== 'true')) {
|
|
|
|
$sr = @ldap_search($server, $dn, $filter, $attributes, $attrsOnly, $limit, 0, LDAP_DEREF_NEVER);
|
|
|
|
if ($sr) {
|
|
|
|
$entries = ldap_get_entries($server, $sr);
|
|
|
|
if (!$entries) {
|
|
|
|
return array();
|
|
|
|
}
|
2011-05-21 10:58:22 +00:00
|
|
|
cleanLDAPResult($entries);
|
|
|
|
return $entries;
|
2010-02-07 14:04:29 +00:00
|
|
|
}
|
2014-11-29 17:40:39 +00:00
|
|
|
else {
|
|
|
|
return array();
|
|
|
|
}
|
2010-02-07 14:04:29 +00:00
|
|
|
}
|
2014-11-29 17:40:39 +00:00
|
|
|
$pageSize = 999;
|
|
|
|
$cookie = '';
|
|
|
|
$return = array();
|
|
|
|
do {
|
|
|
|
@ldap_control_paged_result($server, $pageSize, true, $cookie);
|
|
|
|
$sr = @ldap_search($server, $dn, $filter, $attributes, $attrsOnly, $limit, 0, LDAP_DEREF_NEVER);
|
|
|
|
if (!$sr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$entries = ldap_get_entries($server, $sr);
|
|
|
|
if (!$entries) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$return = array_merge($return, $entries);
|
|
|
|
@ldap_control_paged_result_response($server, $sr, $cookie);
|
|
|
|
} while($cookie !== null && $cookie != '');
|
|
|
|
cleanLDAPResult($return);
|
2010-02-07 14:04:29 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2011-04-25 17:56:06 +00:00
|
|
|
/**
|
|
|
|
* Returns the given DN.
|
|
|
|
*
|
|
|
|
* @param String $dn DN
|
|
|
|
* @param array $attributes list of attributes to fetch
|
2012-11-30 19:19:56 +00:00
|
|
|
* @param handle $handle LDAP handle (optional for admin interface pages)
|
2011-04-25 17:56:06 +00:00
|
|
|
* @return array attributes or null if not found
|
|
|
|
*/
|
2012-11-30 19:19:56 +00:00
|
|
|
function ldapGetDN($dn, $attributes = array('dn'), $handle = null) {
|
|
|
|
if ($handle == null) {
|
|
|
|
$handle = $_SESSION['ldap']->server();
|
|
|
|
}
|
2011-04-25 17:56:06 +00:00
|
|
|
$return = null;
|
2012-11-30 19:19:56 +00:00
|
|
|
$sr = @ldap_read($handle, escapeDN($dn), 'objectClass=*', $attributes);
|
2011-04-25 17:56:06 +00:00
|
|
|
if ($sr) {
|
|
|
|
$entries = ldap_get_entries($_SESSION['ldap']->server(), $sr);
|
|
|
|
if ($entries) {
|
2011-05-21 10:58:22 +00:00
|
|
|
cleanLDAPResult($entries);
|
|
|
|
$return = $entries[0];
|
2011-04-25 17:56:06 +00:00
|
|
|
}
|
|
|
|
@ldap_free_result($sr);
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2010-02-07 14:04:29 +00:00
|
|
|
/**
|
2010-02-14 18:01:20 +00:00
|
|
|
* Returns the parameters for a StatusMessage of the last LDAP search.
|
2010-02-07 14:04:29 +00:00
|
|
|
*
|
2010-02-14 18:01:20 +00:00
|
|
|
* @return array parameters for StatusMessage or null if all was ok
|
2010-02-07 14:04:29 +00:00
|
|
|
*/
|
2010-02-14 18:01:20 +00:00
|
|
|
function getLastLDAPError() {
|
|
|
|
$errorNumber = ldap_errno($_SESSION["ldap"]->server());
|
|
|
|
switch ($errorNumber) {
|
|
|
|
// all ok
|
|
|
|
case 0:
|
|
|
|
return null;
|
|
|
|
break;
|
|
|
|
// size limit exceeded
|
|
|
|
case 4:
|
|
|
|
$error = array("WARN", _("LDAP sizelimit exceeded, not all entries are shown."));
|
|
|
|
if ($_SESSION['config']->get_searchLimit() == 0) {
|
|
|
|
// server limit exceeded
|
|
|
|
$error[] = _("See the manual for instructions to solve this problem.");
|
|
|
|
}
|
|
|
|
return $error;
|
|
|
|
break;
|
|
|
|
// other errors
|
|
|
|
default:
|
|
|
|
return array("ERROR", _("LDAP search failed! Please check your preferences."), ldap_error($_SESSION["ldap"]->server()));
|
|
|
|
break;
|
|
|
|
}
|
2010-02-07 14:04:29 +00:00
|
|
|
}
|
2010-02-06 11:52:48 +00:00
|
|
|
|
2009-11-25 23:07:28 +00:00
|
|
|
/**
|
|
|
|
* Cleans the result of an LDAP search.
|
|
|
|
* This will remove all 'count' entries and also all numeric array keys.
|
|
|
|
*
|
|
|
|
* @param array $entries LDAP entries in format $entries[entry number][attribute name][attribute values]
|
|
|
|
*/
|
2011-05-21 10:58:22 +00:00
|
|
|
function cleanLDAPResult(&$entries) {
|
2009-11-25 23:07:28 +00:00
|
|
|
if (isset($entries['count'])) {
|
|
|
|
unset($entries['count']);
|
|
|
|
}
|
|
|
|
// iterate over all results
|
2011-05-21 10:58:22 +00:00
|
|
|
$count = sizeof($entries);
|
|
|
|
for ($e = 0; $e < $count; $e++) {
|
2009-11-25 23:07:28 +00:00
|
|
|
// remove 'count' entries and numerical entries
|
|
|
|
for ($i = 0; $i < $entries[$e]['count']; $i++) {
|
|
|
|
if (isset($entries[$e][$i])) {
|
|
|
|
unset($entries[$e][$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($entries[$e]['count']);
|
|
|
|
$attrNames = array_keys($entries[$e]);
|
2011-05-21 10:58:22 +00:00
|
|
|
$attrCount = sizeof($attrNames);
|
|
|
|
for ($i = 0; $i < $attrCount; $i++) {
|
2009-11-25 23:07:28 +00:00
|
|
|
if (is_array($entries[$e][$attrNames[$i]])) {
|
|
|
|
unset($entries[$e][$attrNames[$i]]['count']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-28 18:49:59 +00:00
|
|
|
/**
|
|
|
|
* Transforms a DN into a more user friendly format.
|
|
|
|
* E.g. "dc=company,dc=de" is transformed to "company > de".
|
|
|
|
*
|
|
|
|
* @param String $dn DN
|
|
|
|
* @return String transformed DN
|
|
|
|
*/
|
|
|
|
function getAbstractDN($dn) {
|
|
|
|
if ($dn == '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$parts = explode(',', $dn);
|
|
|
|
for ($i = 0; $i < sizeof($parts); $i++) {
|
|
|
|
$subparts = explode('=', $parts[$i]);
|
|
|
|
if (sizeof($subparts) == 2) {
|
|
|
|
$parts[$i] = $subparts[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return implode(' > ', $parts);
|
|
|
|
}
|
|
|
|
|
2011-04-25 17:56:06 +00:00
|
|
|
/**
|
|
|
|
* Helper function to sort DNs.
|
|
|
|
*
|
|
|
|
* @param string $a first argument to compare
|
|
|
|
* @param string $b second argument to compare
|
|
|
|
* @return integer 0 if equal, 1 if $a is greater, -1 if $b is greater
|
|
|
|
*/
|
2013-10-27 15:54:36 +00:00
|
|
|
function compareDN(&$a, &$b) {
|
2011-04-25 17:56:06 +00:00
|
|
|
// split DNs
|
2013-10-27 15:54:36 +00:00
|
|
|
$array_a = explode(",", strtolower($a));
|
|
|
|
$array_b = explode(",", strtolower($b));
|
2011-04-25 17:56:06 +00:00
|
|
|
$len_a = sizeof($array_a);
|
|
|
|
$len_b = sizeof($array_b);
|
|
|
|
// check how many parts to compare
|
|
|
|
$len = min($len_a, $len_b);
|
|
|
|
// compare from last part on
|
|
|
|
for ($i = 0; $i < $len; $i++) {
|
|
|
|
// get parts to compare
|
2013-10-27 15:54:36 +00:00
|
|
|
$part_a = $array_a[$len_a - $i - 1];
|
|
|
|
$part_a = explode('=', $part_a);
|
|
|
|
$part_a = $part_a[1];
|
|
|
|
$part_b = $array_b[$len_b - $i - 1];
|
|
|
|
$part_b = explode('=', $part_b);
|
|
|
|
$part_b = $part_b[1];
|
2011-04-25 17:56:06 +00:00
|
|
|
// compare parts
|
|
|
|
if ($part_a == $part_b) { // part is identical
|
|
|
|
if ($i == ($len - 1)) {
|
|
|
|
if ($len_a > $len_b) return 1;
|
|
|
|
elseif ($len_a < $len_b) return -1;
|
|
|
|
else return 0; // DNs are identical
|
|
|
|
}
|
|
|
|
}
|
2013-03-12 20:05:41 +00:00
|
|
|
else {
|
|
|
|
return strnatcasecmp($part_a, $part_b);
|
|
|
|
}
|
2011-04-25 17:56:06 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats an LDAP time string (e.g. from createTimestamp).
|
|
|
|
*
|
2012-07-15 12:05:47 +00:00
|
|
|
* @param String $time LDAP time value
|
2011-04-25 17:56:06 +00:00
|
|
|
* @return String formated time
|
|
|
|
*/
|
|
|
|
function formatLDAPTimestamp($time) {
|
|
|
|
return substr($time, 6, 2) . '.' . substr($time, 4, 2) . '.' . substr($time, 0, 4) .
|
|
|
|
' ' . substr($time, 8, 2) . ':' . substr($time, 10, 2) . ':' . substr($time, 12, 2) . ' GMT';
|
|
|
|
}
|
|
|
|
|
2011-07-23 15:01:20 +00:00
|
|
|
/**
|
|
|
|
* Simple function to obfuscate strings.
|
|
|
|
*
|
|
|
|
* @param String $text text to obfuscate
|
|
|
|
*/
|
|
|
|
function obfuscateText($text) {
|
|
|
|
if (($text == null) || ($text == '')) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
return str_rot13(base64_encode('LAM_OBFUSCATE:'.$text));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple function to deobfuscate strings.
|
|
|
|
*
|
|
|
|
* @param String $text text to deobfuscate
|
|
|
|
*/
|
|
|
|
function deobfuscateText($text) {
|
|
|
|
if (($text == null) || ($text == '')) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
return str_replace('LAM_OBFUSCATE:', '', base64_decode(str_rot13($text)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given text is obfuscated.
|
|
|
|
*
|
|
|
|
* @param String $text text to check
|
|
|
|
* @return boolean obfuscated or not
|
|
|
|
*/
|
|
|
|
function isObfuscatedText($text) {
|
|
|
|
if (($text == null) || ($text == '')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$deob = base64_decode(str_rot13($text));
|
|
|
|
if (strpos($deob, 'LAM_OBFUSCATE:') === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-03 18:23:08 +00:00
|
|
|
/**
|
|
|
|
* Extracts the RDN attribute name from a given DN.
|
|
|
|
*
|
|
|
|
* @param String $dn DN
|
|
|
|
* @return String RDN attribute name
|
|
|
|
*/
|
|
|
|
function extractRDNAttribute($dn) {
|
|
|
|
if ($dn == null) return null;
|
|
|
|
$parts = explode("=", substr($dn, 0, strpos($dn, ',')));
|
|
|
|
return $parts[0];
|
|
|
|
}
|
|
|
|
|
2012-11-25 17:04:01 +00:00
|
|
|
/**
|
|
|
|
* Extracts the RDN attribute value from a given DN.
|
|
|
|
*
|
|
|
|
* @param String $dn DN
|
|
|
|
* @return String RDN attribute value
|
|
|
|
*/
|
|
|
|
function extractRDNValue($dn) {
|
|
|
|
if ($dn == null) return null;
|
|
|
|
$parts = explode("=", substr($dn, 0, strpos($dn, ',')));
|
|
|
|
return $parts[1];
|
|
|
|
}
|
|
|
|
|
2011-12-03 18:23:08 +00:00
|
|
|
/**
|
|
|
|
* Extracts the DN suffix from a given DN.
|
|
|
|
* E.g. ou=people,dc=test,dc=com will result in dc=test,dc=com.
|
|
|
|
*
|
|
|
|
* @param String $dn DN
|
|
|
|
* @return String DN suffix
|
|
|
|
*/
|
|
|
|
function extractDNSuffix($dn) {
|
|
|
|
if ($dn == null) return null;
|
|
|
|
return substr($dn, strpos($dn, ',')+1);
|
|
|
|
}
|
|
|
|
|
2012-05-30 19:00:56 +00:00
|
|
|
/**
|
|
|
|
* Sends the password mail.
|
|
|
|
*
|
|
|
|
* @param String $pwd new password
|
|
|
|
* @param array $user LDAP attributes of user
|
2013-03-24 19:18:15 +00:00
|
|
|
* @param String $recipient recipient address (optional, $user['mail'][0] used by default)
|
2012-05-30 19:00:56 +00:00
|
|
|
* @return array list of arrays that can be used to create status messages
|
|
|
|
*/
|
2013-03-24 19:18:15 +00:00
|
|
|
function sendPasswordMail($pwd, $user, $recipient = null) {
|
2013-03-16 14:33:15 +00:00
|
|
|
$user = array_change_key_case($user, CASE_LOWER);
|
2012-05-30 19:00:56 +00:00
|
|
|
// read mail data
|
|
|
|
$mailTo = $user['mail'][0];
|
2013-03-24 19:18:15 +00:00
|
|
|
if (!empty($recipient)) {
|
|
|
|
$mailTo = $recipient;
|
|
|
|
}
|
2013-12-29 18:10:26 +00:00
|
|
|
if (empty($mailTo)) {
|
|
|
|
logNewMessage(LOG_ERR, 'Unable to send password mail, no TO address set.');
|
|
|
|
return array(
|
|
|
|
array('ERROR', _('Unable to send mail!'))
|
|
|
|
);
|
|
|
|
}
|
2012-05-30 19:00:56 +00:00
|
|
|
$mailFrom = $_SESSION['config']->getLamProMailFrom();
|
|
|
|
$mailReplyTo = $_SESSION['config']->getLamProMailReplyTo();
|
|
|
|
$mailSubject = $_SESSION['config']->getLamProMailSubject();
|
|
|
|
$mailText = $_SESSION['config']->getLamProMailText();
|
|
|
|
$mailIsHTML = $_SESSION['config']->getLamProMailIsHTML();
|
|
|
|
$subject = $mailSubject;
|
|
|
|
$body = $mailText;
|
|
|
|
$body = str_replace('@@newPassword@@', $pwd, $body);
|
|
|
|
$results = array();
|
|
|
|
$found = preg_match('/\@\@[^\@]+\@\@/', $body, $results);
|
|
|
|
while ($found == 1) {
|
|
|
|
$attr = str_replace('@', '', $results[0]);
|
|
|
|
$value = '';
|
|
|
|
if (isset($user[strtolower($attr)][0])) {
|
2013-04-28 10:20:54 +00:00
|
|
|
if (is_array($user[strtolower($attr)])) {
|
|
|
|
$value = $user[strtolower($attr)][0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$value = $user[strtolower($attr)];
|
|
|
|
}
|
2012-05-30 19:00:56 +00:00
|
|
|
}
|
|
|
|
$body = str_replace('@@' . $attr . '@@', $value, $body);
|
|
|
|
$found = preg_match('/\@\@[^\@]+\@\@/', $body, $results);
|
|
|
|
}
|
2013-07-26 19:52:18 +00:00
|
|
|
$headerLines = createEMailHeaders($mailFrom, ($mailIsHTML == 'true'), $mailReplyTo);
|
2013-10-16 16:48:59 +00:00
|
|
|
$success = sendEMail($mailTo, $subject, $body, $headerLines);
|
2012-05-30 19:00:56 +00:00
|
|
|
if ($success) {
|
|
|
|
logNewMessage(LOG_DEBUG, 'Sent password mail to ' . $mailTo);
|
|
|
|
return array(
|
|
|
|
array('INFO', sprintf(_('Mail successfully sent to %s.'), htmlspecialchars($mailTo)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logNewMessage(LOG_ERR, 'Unable to send password mail to ' . htmlspecialchars($mailTo));
|
|
|
|
return array(
|
|
|
|
array('ERROR', _('Unable to send mail!'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 19:52:18 +00:00
|
|
|
/**
|
|
|
|
* Generates the email header text for the given parameters.
|
|
|
|
*
|
|
|
|
* @param String $from FROM address
|
|
|
|
* @param boolean $isHTML mail is formatted as HTML or plain text
|
|
|
|
* @param String $replyTo reply-to address (optional)
|
|
|
|
*/
|
|
|
|
function createEMailHeaders($from, $isHTML, $replyTo = null) {
|
|
|
|
$headerLines = "X-Mailer: LDAP Account Manager\r\n";
|
|
|
|
if (!empty($from)) {
|
|
|
|
if (preg_match('/^(.*)<(.*)>$/', $from, $matchesFrom)) {
|
|
|
|
$from = base64EncodeForEMail($matchesFrom[1]) . ' <' . $matchesFrom[2] . '>';
|
|
|
|
}
|
|
|
|
$headerLines .= 'From: ' . $from . "\r\n";
|
|
|
|
}
|
|
|
|
if (!empty($replyTo)) {
|
|
|
|
if (preg_match('/^(.*)<(.*)>$/', $replyTo, $matchesReplyTo)) {
|
|
|
|
$replyTo = base64EncodeForEMail($matchesReplyTo[1]) . ' <' . $matchesReplyTo[2] . '>';
|
|
|
|
}
|
|
|
|
$headerLines .= 'Reply-To: ' . $replyTo . "\r\n";
|
|
|
|
}
|
|
|
|
$headerLines .= "MIME-Version: 1.0\r\n";
|
|
|
|
if ($isHTML) {
|
|
|
|
$headerLines .= "Content-type: text/html; charset=UTF-8\r\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$headerLines .= "Content-type: text/plain; charset=UTF-8\r\n";
|
|
|
|
}
|
|
|
|
return $headerLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a base64 encoded string of the given values in a fomat that is used in emails.
|
|
|
|
*
|
|
|
|
* @param String $value value to encode
|
|
|
|
* @return String base64 encoded value
|
|
|
|
*/
|
|
|
|
function base64EncodeForEMail($value) {
|
|
|
|
return '=?UTF-8?B?' . base64_encode($value) . '?=';
|
|
|
|
}
|
|
|
|
|
2013-10-16 16:48:59 +00:00
|
|
|
/**
|
|
|
|
* Sends out an email.
|
|
|
|
*
|
|
|
|
* @param String $to TO address
|
|
|
|
* @param String $subject email subject
|
|
|
|
* @param String $text mail body (with \r\n EOL)
|
|
|
|
* @param String $headers header lines (with \r\n EOL)
|
|
|
|
*/
|
|
|
|
function sendEMail($to, $subject, $text, $headers) {
|
|
|
|
if (!empty($_SESSION['cfgMain']->mailEOL) && ($_SESSION['cfgMain']->mailEOL === 'unix')) {
|
|
|
|
$text = str_replace("\r\n", "\n", $text);
|
|
|
|
$headers = str_replace("\r\n", "\n", $headers);
|
|
|
|
}
|
|
|
|
logNewMessage(LOG_WARNING, $text);
|
|
|
|
return mail($to, base64EncodeForEMail($subject), $text, $headers);
|
|
|
|
}
|
|
|
|
|
2013-05-09 19:10:35 +00:00
|
|
|
/**
|
|
|
|
* Caches module objects.
|
|
|
|
* This improves performance if the same module does not need to be created multiple times (calling get_metaData() each time).
|
|
|
|
*
|
|
|
|
* @author Roland Gruber
|
|
|
|
*/
|
|
|
|
class moduleCache {
|
|
|
|
|
|
|
|
/** module cache ("name:scope" => module) */
|
|
|
|
private static $cache = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new/cached module with the given name and scope.
|
|
|
|
*
|
|
|
|
* @param String $name module name
|
|
|
|
* @param String $scope module scope (e.g. user)
|
|
|
|
*/
|
|
|
|
public static function getModule($name, $scope) {
|
|
|
|
if (isset(moduleCache::$cache[$name . ':' . $scope])) {
|
|
|
|
return moduleCache::$cache[$name . ':' . $scope];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
moduleCache::$cache[$name . ':' . $scope] = new $name($scope);
|
|
|
|
return moduleCache::$cache[$name . ':' . $scope];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-21 11:34:31 +00:00
|
|
|
/**
|
|
|
|
* Returns a random number.
|
|
|
|
*
|
|
|
|
* @return int random number
|
|
|
|
*/
|
|
|
|
function getRandomNumber() {
|
2013-07-21 11:47:49 +00:00
|
|
|
if (function_exists('openssl_random_pseudo_bytes')) {
|
2013-11-24 10:04:46 +00:00
|
|
|
return abs(hexdec(bin2hex(openssl_random_pseudo_bytes(5))));
|
2013-07-21 11:47:49 +00:00
|
|
|
}
|
2013-11-24 10:04:46 +00:00
|
|
|
return abs(mt_rand());
|
2013-07-21 11:34:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-10 12:43:01 +00:00
|
|
|
/**
|
|
|
|
* Connects to the LDAP server and extracts the certificates.
|
|
|
|
*
|
|
|
|
* @param String $server server name
|
|
|
|
* @param String $port server port
|
|
|
|
* @return mixed false on error and certificate if extracted successfully
|
|
|
|
*/
|
|
|
|
function getLDAPSSLCertificate($server, $port) {
|
|
|
|
$stream = @stream_context_create(array("ssl" => array("capture_peer_cert_chain" => true)));
|
|
|
|
if (!$stream) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$client = @stream_socket_client('ssl://' . $server . ':' . $port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $stream);
|
|
|
|
if (!$client) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$context = stream_context_get_params($client);
|
|
|
|
if (!isset($context['options']['ssl']['peer_certificate_chain'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$finalPEM = '';
|
|
|
|
for ($i = 0; $i < sizeof($context['options']['ssl']['peer_certificate_chain']); $i++) {
|
|
|
|
$cert = $context['options']['ssl']['peer_certificate_chain'][$i];
|
|
|
|
$pemData = null;
|
|
|
|
$pemResult = @openssl_x509_export($cert, $pemData);
|
|
|
|
if ($pemResult) {
|
|
|
|
$finalPEM .= $pemData;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $finalPEM;
|
|
|
|
}
|
|
|
|
|
2013-09-15 14:57:55 +00:00
|
|
|
/**
|
|
|
|
* Returns the extended LDAP error message if any.
|
|
|
|
*
|
|
|
|
* @param handle $server LDAP server handle
|
|
|
|
* @return String error message
|
|
|
|
*/
|
|
|
|
function getExtendedLDAPErrorMessage($server) {
|
|
|
|
$ldapMsg = null;
|
|
|
|
ldap_get_option($server, LDAP_OPT_ERROR_STRING, $ldapMsg);
|
|
|
|
if (empty($ldapMsg)) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-16 17:37:17 +00:00
|
|
|
return $ldapMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the default error message to display on the web page.
|
|
|
|
* HTML special characters are already escaped.
|
|
|
|
*
|
|
|
|
* @param handle $server LDAP server handle
|
|
|
|
* @return String error message
|
|
|
|
*/
|
|
|
|
function getDefaultLDAPErrorString($server) {
|
2014-02-27 20:50:57 +00:00
|
|
|
$extError = htmlspecialchars(getExtendedLDAPErrorMessage($server));
|
|
|
|
// Active Directory message translations
|
|
|
|
if ((strpos($extError, 'DSID-031A120C') !== false) && (strpos($extError, '5003') !== false)) {
|
|
|
|
$extError = _('Your password does not meet the password strength qualifications. Please retry with another one.') . '<br><br>' . $extError;
|
|
|
|
}
|
2013-10-16 17:37:17 +00:00
|
|
|
$message = _('LDAP error, server says:') . ' ' . ldap_error($server);
|
|
|
|
if (!empty($extError)) {
|
|
|
|
$message .= ' - ' . $extError;
|
|
|
|
}
|
2014-02-27 20:50:57 +00:00
|
|
|
return $message;
|
2013-09-15 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
2013-10-15 18:51:36 +00:00
|
|
|
/**
|
|
|
|
* Returns the URL under which the page was loaded.
|
|
|
|
* This includes any GET parameters set.
|
|
|
|
*
|
|
|
|
* @return String URL
|
|
|
|
*/
|
|
|
|
function getCallingURL() {
|
|
|
|
$url = null;
|
2014-01-12 19:52:14 +00:00
|
|
|
if (!empty($_SERVER['HTTP_HOST']) && !empty($_SERVER['REQUEST_URI'])) {
|
2013-10-15 18:51:36 +00:00
|
|
|
$proto = 'http://';
|
|
|
|
if (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) {
|
|
|
|
$proto = 'https://';
|
|
|
|
}
|
|
|
|
$url = $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
|
|
|
}
|
2014-01-12 19:52:14 +00:00
|
|
|
elseif (!empty($_SERVER['HTTP_REFERER'])) {
|
|
|
|
$url = $_SERVER['HTTP_REFERER'];
|
|
|
|
}
|
2013-10-15 18:51:36 +00:00
|
|
|
logNewMessage(LOG_DEBUG, 'Calling URL detected as ' . $url);
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2003-10-21 13:40:13 +00:00
|
|
|
?>
|