class_ldap.inc 36.73 KiB
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 1998  Eric Kilfoil
  Copyright (C) 2003 Alejandro Escanero Blanco
  Copyright (C) 2003-2010  Cajus Pollmeier
  Copyright (C) 2011-2018  FusionDirectory
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
/*!
 * \file class_ldap.inc
 * Source code for Class LDAP
/*!
 * \brief This class contains all ldap function needed to make
 * ldap operations easy
class LDAP
  var $hascon         = FALSE;
  var $reconnect      = FALSE;
  var $tls            = FALSE;
  /* connection identifier */
  var $cid;
  var $hasres         = [];
  var $sr             = [];
  var $re             = [];
  var $basedn         = "";
  /* 0 if we are fetching the first entry, otherwise 1 */
  var $start          = [];
  /* Any error messages to be returned can be put here */
  var $error          = "";
  var $srp            = 0;
  /* Information read from slapd.oc.conf */
  var $objectClasses    = [];
  /* the dn for the bind */
  var $binddn           = "";
  /* the dn's password for the bind */
  var $bindpw           = "";
  var $hostname         = "";
  var $follow_referral  = FALSE;
  var $referrals        = [];
  /* 0, empty or negative values will disable this check */
  var $max_ldap_query_time  = 0;
  /*!
   * \brief Create a LDAP connection
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
* * \param string $binddn Bind of the DN * * \param string $bindpw Bind * * \param string $hostname The hostname * * \param boolean $follow_referral FALSE * * \param boolean $tls FALSE */ function __construct ($binddn, $bindpw, $hostname, $follow_referral = FALSE, $tls = FALSE) { global $config; $this->follow_referral = $follow_referral; $this->tls = $tls; $this->binddn = $binddn; $this->bindpw = $bindpw; $this->hostname = $hostname; /* Check if MAX_LDAP_QUERY_TIME is defined */ if (is_object($config) && ($config->get_cfg_value("ldapMaxQueryTime") != "")) { $str = $config->get_cfg_value("ldapMaxQueryTime"); $this->max_ldap_query_time = (float)($str); } $this->connect(); } /*! * \brief Get the search ressource * * \return increase srp */ function getSearchResource () { $this->sr[$this->srp] = NULL; $this->start[$this->srp] = 0; $this->hasres[$this->srp] = FALSE; return $this->srp++; } /*! * \brief Function to fix problematic characters in DN's that are used for search requests. I.e. member=.... * * \param string $dn The DN */ static function prepare4filter ($dn) { trigger_error('deprecated, use ldap_escape_f instead'); return ldap_escape_f($dn); } /*! * \brief Create a connection to LDAP server * * The string $error containts result of the connection */ function connect () { $this->hascon = FALSE; $this->reconnect = FALSE; if ($this->cid = @ldap_connect($this->hostname)) { @ldap_set_option($this->cid, LDAP_OPT_PROTOCOL_VERSION, 3); if (function_exists("ldap_set_rebind_proc") && $this->follow_referral) { @ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1); @ldap_set_rebind_proc($this->cid, [&$this, "rebind"]); } if (function_exists("ldap_start_tls") && $this->tls) { @ldap_start_tls($this->cid);
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
} $this->error = "No Error"; if (@ldap_bind($this->cid, $this->binddn, $this->bindpw)) { $this->error = "Success"; $this->hascon = TRUE; } else { if ($this->reconnect) { if ($this->error != "Success") { $this->error = "Could not rebind to " . $this->binddn; } } else { $this->error = "Could not bind to " . $this->binddn; } } } else { $this->error = "Could not connect to LDAP server"; } } /*! * \brief Rebind */ function rebind ($ldap, $referral) { $credentials = $this->get_credentials($referral); if (@ldap_bind($ldap, $credentials['ADMINDN'], $credentials['ADMINPASSWORD'])) { $this->error = "Success"; $this->hascon = TRUE; $this->reconnect = TRUE; return 0; } else { $this->error = "Could not bind to " . $credentials['ADMINDN']; return NULL; } } /*! * \brief Reconnect to LDAP server */ function reconnect () { if ($this->reconnect) { $this->unbind(); } } /*! * \brief Unbind to LDAP server */ function unbind () { @ldap_unbind($this->cid); $this->cid = NULL; } /*! * \brief Disconnect to LDAP server */ function disconnect () { if ($this->hascon) { @ldap_close($this->cid); $this->hascon = FALSE; } } /*! * \brief Change directory *
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
* \param string $dir The new directory */ function cd ($dir) { if ($dir == '..') { $this->basedn = $this->getParentDir(); } else { $this->basedn = $dir; } } /*! * \brief Accessor of the parent directory of the basedn * * \param string $basedn The basedn which we want the parent directory * * \return String, the parent directory */ function getParentDir ($basedn = '') { if ($basedn == '') { $basedn = $this->basedn; } return preg_replace("/[^,]*[,]*[ ]*(.*)/", "$1", $basedn); } /*! * \brief Search about filter * * \param integer $srp srp * * \param string $filter The filter * * \param array $attrs * * \param string $scope Scope of the search: subtree/base/one */ function search ($srp, $filter, $attrs = [], $scope = 'subtree') { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } $start = microtime(TRUE); $this->clearResult($srp); switch (strtolower($scope)) { case 'base': $this->sr[$srp] = @ldap_read($this->cid, $this->basedn, $filter, $attrs); break; case 'one': $this->sr[$srp] = @ldap_list($this->cid, $this->basedn, $filter, $attrs); break; default: case 'subtree': $this->sr[$srp] = @ldap_search($this->cid, $this->basedn, $filter, $attrs); break; } $this->error = @ldap_error($this->cid); $this->resetResult($srp); $this->hasres[$srp] = TRUE; /* Check if query took longer as specified in max_ldap_query_time */ if ($this->max_ldap_query_time) { $diff = microtime(TRUE) - $start; if ($diff > $this->max_ldap_query_time) { msg_dialog::display(_("Performance warning"), sprintf(_("LDAP performance is poor: last query took about %.2fs!"), $diff), WARNING_DIALOG); } }
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
$this->log("LDAP operation: time=".(microtime(TRUE) - $start)." operation=search('".$this->basedn."', '$filter')"); return $this->sr[$srp]; } else { $this->error = "Could not connect to LDAP server"; return ""; } } /* * \brief List * * \param integer $srp * * \param string $filter Initialized at "(objectclass=*)" * * \param string $basedn Empty string * * \param array $attrs */ function ls ($srp, $filter = "(objectclass=*)", $basedn = "", $attrs = ["*"]) { trigger_error('deprecated'); $this->cd($basedn); return $this->search($srp, $filter, $attrs, 'one'); } /* * \brief Concatenate * * \param integer $srp * * \param string $dn The DN * * \param array $attrs * * \param string $filter Initialized at "(objectclass=*)" */ function cat ($srp, $dn, $attrs = ["*"], $filter = "(objectclass=*)") { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } $this->clearResult($srp); $this->sr[$srp] = @ldap_read($this->cid, $dn, $filter, $attrs); $this->error = @ldap_error($this->cid); $this->resetResult($srp); $this->hasres[$srp] = TRUE; return $this->sr[$srp]; } else { $this->error = "Could not connect to LDAP server"; return ""; } } /*! * \brief Search object from a filter * * \param string $dn The DN * * \param string $filter The filter of the research */ function object_match_filter ($dn, $filter) { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } $res = @ldap_read($this->cid, $dn, $filter, ["objectClass"]);
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
$rv = @ldap_count_entries($this->cid, $res); return $rv; } else { $this->error = "Could not connect to LDAP server"; return FALSE; } } /*! * \brief Set a size limit * * \param $size The limit */ function set_size_limit ($size) { /* Ignore zero settings */ if ($size == 0) { @ldap_set_option($this->cid, LDAP_OPT_SIZELIMIT, 10000000); } if ($this->hascon) { @ldap_set_option($this->cid, LDAP_OPT_SIZELIMIT, $size); } else { $this->error = "Could not connect to LDAP server"; } } /*! * \brief Fetch * * \param integer $srp */ function fetch ($srp) { $att = []; if ($this->hascon) { if ($this->hasres[$srp]) { if ($this->start[$srp] == 0) { if ($this->sr[$srp]) { $this->start[$srp] = 1; $this->re[$srp] = @ldap_first_entry($this->cid, $this->sr[$srp]); } else { return []; } } else { $this->re[$srp] = @ldap_next_entry($this->cid, $this->re[$srp]); } if ($this->re[$srp]) { $att = @ldap_get_attributes($this->cid, $this->re[$srp]); $att['dn'] = trim(@ldap_get_dn($this->cid, $this->re[$srp])); } $this->error = @ldap_error($this->cid); if (!isset($att)) { $att = []; } return $att; } else { $this->error = "Perform a fetch with no search"; return ""; } } else { $this->error = "Could not connect to LDAP server"; return ""; } } /*! * \brief Reset the result * * \param integer $srp Value to be reset */
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
function resetResult ($srp) { $this->start[$srp] = 0; } /*! * \brief Clear a result * * \param integer $srp The result to clear */ function clearResult ($srp) { if ($this->hasres[$srp]) { $this->hasres[$srp] = FALSE; @ldap_free_result($this->sr[$srp]); } } /*! * \brief Accessor of the DN * * \param $srp srp */ function getDN ($srp) { if ($this->hascon) { if ($this->hasres[$srp]) { if (!$this->re[$srp]) { $this->error = "Perform a Fetch with no valid Result"; } else { $rv = @ldap_get_dn($this->cid, $this->re[$srp]); $this->error = @ldap_error($this->cid); return trim($rv); } } else { $this->error = "Perform a Fetch with no Search"; return ""; } } else { $this->error = "Could not connect to LDAP server"; return ""; } } /*! * \brief Return the numbers of entries * * \param $srp srp */ function count ($srp) { if ($this->hascon) { if ($this->hasres[$srp]) { $rv = @ldap_count_entries($this->cid, $this->sr[$srp]); $this->error = @ldap_error($this->cid); return $rv; } else { $this->error = "Perform a Fetch with no Search"; return ""; } } else { $this->error = "Could not connect to LDAP server"; return ""; } } /*! * \brief Remove
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
* * \param string $attrs Empty string * * \param string $dn Empty string */ function rm ($attrs = "", $dn = "") { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } if ($dn == '') { $dn = $this->basedn; } $r = ldap_mod_del($this->cid, $dn, $attrs); $this->error = @ldap_error($this->cid); return $r; } else { $this->error = 'Could not connect to LDAP server'; return ''; } } function mod_add ($attrs = "", $dn = "") { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } if ($dn == "") { $dn = $this->basedn; } $r = @ldap_mod_add($this->cid, $dn, $attrs); $this->error = @ldap_error($this->cid); return $r; } else { $this->error = "Could not connect to LDAP server"; return ""; } } /*! * \brief Remove directory * * \param string $deletedn The DN to be deleted */ function rmdir ($deletedn) { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } $r = @ldap_delete($this->cid, $deletedn); $this->error = @ldap_error($this->cid); return ($r ? $r : 0); } else { $this->error = "Could not connect to LDAP server"; return ""; } } /*! * \brief Move the given Ldap entry from $source to $dest * * \param String $source The source dn. * * \param String $dest The destination dn.
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
* * \return Boolean TRUE on success else FALSE. */ function rename_dn ($source, $dest) { /* Check if source and destination are the same entry */ if (strtolower($source) == strtolower($dest)) { trigger_error("Source and destination can't be the same entry."); $this->error = "Source and destination can't be the same entry."; return FALSE; } /* Check if destination entry exists */ if ($this->dn_exists($dest)) { trigger_error("Destination '$dest' already exists."); $this->error = "Destination '$dest' already exists."; return FALSE; } /* Extract the name and the parent part out ouf source dn. e.g. cn=herbert,ou=department,dc=... parent => ou=department,dc=... dest_rdn => cn=herbert */ $parent = preg_replace("/^[^,]+,/", "", $dest); $dest_rdn = preg_replace("/,.*$/", "", $dest); if ($this->hascon) { if ($this->reconnect) { $this->connect(); } /* We have to pass TRUE as deleteoldrdn in case the attribute is single-valued */ $r = ldap_rename($this->cid, $source, $dest_rdn, $parent, TRUE); $this->error = ldap_error($this->cid); /* Check if destination dn exists, if not the server may not support this operation */ $r &= is_resource($this->dn_exists($dest)); return $r; } else { $this->error = "Could not connect to LDAP server"; return FALSE; } } /*! * \brief Function rmdir_recursive * * Based on recursive_remove, adding two thing: full subtree remove, and delete own node. * * \param $srp srp * * \param string $deletedn The dn to delete * * \return TRUE on sucessfull , 0 in error, and "" when we don't get a ldap conection */ function rmdir_recursive ($srp, $deletedn) { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } $delarray = []; /* Get sorted list of dn's to delete */ $this->cd($deletedn); $this->search($srp, '(objectClass=*)', ['dn']); while ($attrs = $this->fetch($srp)) { $delarray[$attrs['dn']] = strlen($attrs['dn']);
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
} arsort($delarray); reset($delarray); /* Really Delete ALL dn's in subtree */ foreach (array_keys($delarray) as $key) { $r = @ldap_delete($this->cid, $key); if ($r === FALSE) { break; } } $this->error = @ldap_error($this->cid); return ($r ? $r : 0); } else { $this->error = "Could not connect to LDAP server"; return ""; } } function makeReadableErrors ($error, $attrs) { if ($this->success()) { return ""; } $str = ""; if (preg_match("/^objectClass: value #([0-9]*) invalid per syntax$/", $this->get_additional_error(), $m)) { if (isset($attrs['objectClass'])) { $ocs = $attrs['objectClass']; if (!is_array($ocs)) { $ocs = [$ocs]; } if (isset($ocs[$m[1]])) { $str .= " - <b>objectClass: ".$ocs[$m[1]]."</b>"; } } } if ($error == "Undefined attribute type") { $str = " - <b>attribute: ".preg_replace("/:.*$/", "", $this->get_additional_error())."</b>"; } @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $attrs, "Erroneous data"); return $str; } /*! * \brief Modify a entry of the directory LDAP * * \param array $attrs The new entry */ function modify (array $attrs) { if (count($attrs) == 0) { return 0; } if ($this->hascon) { if ($this->reconnect) { $this->connect(); } $r = @ldap_modify($this->cid, $this->basedn, $attrs); $this->error = @ldap_error($this->cid); if (!$this->success()) { $this->error .= $this->makeReadableErrors($this->error, $attrs); } return ($r ? $r : 0); } else { $this->error = "Could not connect to LDAP server"; return ""; }
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
} /*! * \brief Add entry in the LDAP directory * * \param string $attrs The entry to add */ function add ($attrs) { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } $r = @ldap_add($this->cid, $this->basedn, $attrs); $this->error = @ldap_error($this->cid); if (!$this->success()) { $this->error .= $this->makeReadableErrors($this->error, $attrs); } return ($r ? $r : 0); } else { $this->error = "Could not connect to LDAP server"; return ""; } } /* * $target is a dn, i.e. "ou=example,ou=orga,dc=base" * * Creates missing trees, in our example ou=orga,dc=base will get created if not existing, same thing for ou=example,ou=orga,dc=base * */ function create_missing_trees ($srp, $target, $ignoreReferralBases = TRUE) { $real_path = substr($target, 0, strlen($target) - strlen($this->basedn) - 1); if ($target == $this->basedn) { $l = ['dummy']; } else { $l = array_reverse(ldap_explode_dn($real_path, 0)); } unset($l['count']); $cdn = $this->basedn; /* Load schema if available... */ $classes = $this->get_objectclasses(); foreach ($l as $part) { if ($part != 'dummy') { $cdn = "$part,$cdn"; } /* Ignore referrals */ if ($ignoreReferralBases) { foreach ($this->referrals as $ref) { if ($ref['BASE'] == $cdn) { continue 2; } } } $this->cat($srp, $cdn); $attrs = $this->fetch($srp); /* Create missing entry? */ if (count($attrs)) { continue; } $type = preg_replace('/^([^=]+)=.*$/', '\\1', $cdn); $param = preg_replace('/^[^=]+=([^,]+).*$/', '\\1', $cdn); $param = preg_replace(['/\\\\,/','/\\\\"/'], [',','"'], $param);
771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
$attrs = [$type => $param]; /* Hardcoded classes */ switch ($type) { case 'ou': $attrs['objectClass'] = ['organizationalUnit']; break; case 'd': $attrs['objectClass'] = ['domain']; break; case 'dc': $attrs['objectClass'] = ['dcObject']; break; case 'o': $attrs['objectClass'] = ['organization']; break; case 'l': $attrs['objectClass'] = ['locality']; break; case 'c': $attrs['objectClass'] = ['country']; break; default: /* Fallback to autodetection of objectClass */ if (!count($classes)) { msg_dialog::display(_('Internal error'), sprintf(_('Cannot automatically create subtrees with RDN "%s": not supported'), $type), FATAL_ERROR_DIALOG); exit(); } /* Get name of first matching objectClass */ $attrs['objectClass'] = []; foreach ($classes as $class) { if (isset($class['MUST']) && in_array($type, $class['MUST'])) { /* Look for first class that is structural... */ if (isset($class['STRUCTURAL'])) { $attrs['objectClass'] = [$class['NAME']]; break; } /* Look for class that is auxiliary... */ if (empty($attrs['objectClass']) && isset($class['AUXILIARY'])) { $attrs['objectClass'] = [$class['NAME']]; } } elseif (empty($attrs['objectClass']) && isset($class['MAY']) && in_array($type, $class['MAY'])) { /* Better than nothing */ $attrs['objectClass'] = [$class['NAME']]; } } /* Bail out, if we've nothing to do... */ if (empty($attrs['objectClass'])) { msg_dialog::display(_('Internal error'), sprintf(_('Cannot automatically create subtrees with RDN "%s": no object class found!'), $type), FATAL_ERROR_DIALOG); exit(); } } $ocname = $attrs['objectClass'][0]; while (isset($classes[$ocname]['SUP']) && ($classes[$ocname]['SUP'] != 'top')) { $ocname = $classes[$ocname]['SUP']; $attrs['objectClass'][] = $ocname; } if (isset($classes[$ocname]['AUXILIARY'])) { /* AUXILIARY class, we have to add a STRUCTURAL one */ $attrs['objectClass'][] = 'organization'; } foreach ($attrs['objectClass'] as $ocname) { // Fill in MUST values - but do not overwrite existing ones. if (is_array($classes[$ocname]['MUST'])) {
841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
foreach ($classes[$ocname]['MUST'] as $attr) { if (empty($attrs[$attr])) { $attrs[$attr] = $param; } } } } $this->cd($cdn); $this->add($attrs); if (!$this->success()) { @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $cdn, 'dn'); @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $attrs, 'Content'); @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->get_error(), 'LDAP error'); msg_dialog::display(_('LDAP error'), msgPool::ldaperror($this->get_error(), $cdn, LDAP_ADD, get_class()), LDAP_ERROR); return FALSE; } } return TRUE; } /*! * \brief Get the LDAP additional error * * \return string $error containts LDAP_OPT_ERROR_STRING */ function get_additional_error () { $error = ""; @ldap_get_option($this->cid, LDAP_OPT_ERROR_STRING, $error); return $error; } /*! * \brief Success * * \return boolean TRUE if Success is found in $error, else return FALSE */ function success () { return (trim($this->error) === 'Success'); } /*! * \brief Get the error */ function get_error () { if ($this->error == 'Success') { return $this->error; } else { $adderror = $this->get_additional_error(); if ($adderror != "") { $error = $this->error." (".$this->get_additional_error().", ".sprintf(_("while operating on '%s' using LDAP server '%s'"), $this->basedn, $this->hostname).")"; } else { $error = $this->error." (".sprintf(_("while operating on LDAP server %s"), $this->hostname).")"; } return $error; } } /*! * \brief Get the errno * * Must be run right after the ldap request */ function get_errno ()
911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
{ if ($this->error == 'Success') { return 0; } else { return ldap_errno($this->cid); } } /*! * \brief Check if the search hit the size limit * * Must be run right after the search */ function hitSizeLimit () { /* LDAP_SIZELIMIT_EXCEEDED 0x04 */ return ($this->get_errno() == 0x04); } function get_credentials ($url, $referrals = NULL) { $ret = []; $url = preg_replace('!\?\?.*$!', '', $url); $server = preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url); if ($referrals === NULL) { $referrals = $this->referrals; } if (isset($referrals[$server])) { return $referrals[$server]; } else { $ret['ADMINDN'] = $this->binddn; $ret['ADMINPASSWORD'] = $this->bindpw; } return $ret; } /*! * \brief Generates an ldif for all entries matching the filter settings, scope and limit. * * \param $dn The entry to export. * * \param $filter Limit the exported object to those maching this filter. * * \param $scope 'base', 'sub' .. see manpage for 'ldapmodify' for details. * * \param $limit Limits the result. */ function generateLdif ($dn, $filter = "(objectClass=*)", $scope = 'sub', $limit = 0) { // Ensure that limit is numeric if not skip here. if (!empty($limit) && !is_numeric($limit)) { trigger_error(sprintf("Invalid parameter for limit '%s', a numeric value is required."), $limit); return NULL; } $limit = (!$limit) ? '' : ' -z '.$limit; // Check scope values $scope = trim($scope); if (!empty($scope) && !in_array($scope, ['base', 'one', 'sub', 'children'])) { trigger_error(sprintf("Invalid parameter for scope '%s', please use 'base', 'one', 'sub' or 'children'."), $scope); return NULL; } $scope = (!empty($scope)) ? ' -s '.$scope : ''; // Prepare parameters to be valid for shell execution $dn = escapeshellarg($dn);
981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
$pwd = escapeshellarg($this->bindpw); $host = escapeshellarg($this->hostname); $admin = escapeshellarg($this->binddn); $filter = escapeshellarg($filter); $cmd = 'ldapsearch'.($this->tls ? ' -ZZ' : '')." -x -LLLL -D {$admin} {$filter} {$limit} {$scope} -H {$host} -b {$dn} -w {$pwd} "; // Create list of process pipes $descriptorspec = [ 0 => ["pipe", "r"], // stdin 1 => ["pipe", "w"], // stdout 2 => ["pipe", "w"] // stderr ]; // Try to open the process $process = proc_open($cmd, $descriptorspec, $pipes); if (is_resource($process)) { // Write the password to stdin fclose($pipes[0]); // Get results from stdout and stderr $res = stream_get_contents($pipes[1]); $err = stream_get_contents($pipes[2]); fclose($pipes[1]); // Close the process and check its return value if (proc_close($process) != 0) { $this->error = $err; return NULL; } } else { $this->error = _("proc_open failed to execute ldapsearch"); return NULL; } return $res; } function dn_exists ($dn) { return @ldap_read($this->cid, $dn, "(objectClass=*)", ["objectClass"]); } /*! * \brief Function to imports ldifs * * If DeleteOldEntries is TRUE, the destination entry will be deleted first. * If JustModify is TRUE the destination entry will only be touched by the attributes specified in the ldif. * if JustMofify is FALSE the destination dn will be overwritten by the new ldif. * * \param integer $srp * * \param string $str_attr * * \param boolean $JustModify * * \param boolean $DeleteOldEntries */ function import_complete_ldif ($srp, $str_attr, $JustModify, $DeleteOldEntries) { if ($this->reconnect) { $this->connect(); } /* First we split the string into lines */ $fileLines = preg_split("/\n/", $str_attr); if (end($fileLines) != '') { $fileLines[] = ''; } /* Joining lines */
1051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
$line = NULL; $entry = []; $entries = []; $entryStart = -1; foreach ($fileLines as $lineNumber => $fileLine) { if (preg_match('/^ /', $fileLine)) { if ($line === NULL) { throw new LDIFImportException(sprintf(_('Error line %s, first line of an entry cannot start with a space'), $lineNumber)); } /* Append to current line */ $line .= substr($fileLine, 1); } else { if ($line !== NULL) { if (preg_match('/^#/', $line)) { /* Ignore comment */ } elseif (preg_match('/^version:/', $line) && empty($entry)) { /* Ignore version number */ } else { /* Line has ended */ list($key, $value) = explode(':', $line, 2); $value = trim($value); if (preg_match('/^:/', $value)) { $value = base64_decode(trim(substr($value, 1))); } if (preg_match('/^</', $value)) { throw new LDIFImportException(sprintf(_('Error line %s, references to an external file are not supported'), $lineNumber)); } if ($value === '') { throw new LDIFImportException(sprintf(_('Error line %s, attribute "%s" has no value'), $lineNumber, $key)); } if ($key == 'dn') { if (!empty($entry)) { throw new LDIFImportException(sprintf(_('Error line %s, an entry bloc can only have one dn'), $lineNumber)); } $entry['dn'] = $value; $entryStart = $lineNumber; } elseif (empty($entry)) { throw new LDIFImportException(sprintf(_('Error line %s, an entry bloc should start with the dn'), $lineNumber)); } else { if (!isset($entry[$key])) { $entry[$key] = []; } $entry[$key][] = $value; } } } /* Start new line */ $line = trim($fileLine); if ($line == '') { if (!empty($entry)) { /* Entry is finished */ $entries[$entryStart] = $entry; } /* Start a new entry */ $entry = []; $entryStart = -1; $line = NULL; } } } foreach ($entries as $startLine => $entry) { /* Delete before insert */ $usermdir = ($this->dn_exists($entry['dn']) && $DeleteOldEntries); /* Should we use Modify instead of Add */ $usemodify = ($this->dn_exists($entry['dn']) && $JustModify); /* If we can't Import, return with a file error */ if (!$this->import_single_entry($srp, $entry, $usemodify, $usermdir)) { throw new LDIFImportException(sprintf(_('Error while importing dn: "%s", please check your LDIF from line %s on!'), $entry['dn'][0], $startLine));
1121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
} } return count($entries); } /*! \brief Function to Imports a single entry * * If $delete is TRUE; The old entry will be deleted if it exists. * if $modify is TRUE; All variables that are not touched by the new ldif will be kept. * if $modify is FALSE; The new ldif overwrites the old entry, and all untouched attributes get lost. * * \param integer $srp * * \param array $data * * \param boolean $modify * * \param boolean $delete */ protected function import_single_entry ($srp, $data, $modify, $delete) { global $config; if (!$config) { trigger_error("Can't import ldif, can't read config object."); } if ($this->reconnect) { $this->connect(); } $ret = FALSE; /* If dn is an index of data, we should try to insert the data */ if (isset($data['dn'])) { /* Fix dn */ $tmp = ldap_explode_dn($data['dn'], 0); unset($tmp['count']); $dn = ''; foreach ($tmp as $tm) { $dn .= trim($tm).','; } $dn = preg_replace('/,$/', '', $dn); unset($data['dn']); /* Creating Entry */ $this->cd($dn); /* Delete existing entry */ if ($delete) { $this->rmdir_recursive($srp, $dn); } /* Create missing trees */ $this->cd($config->current['BASE']); $this->create_missing_trees($srp, preg_replace('/^[^,]+,/', '', $dn)); $this->cd($dn); if (!$modify) { $this->cat($srp, $dn); if ($this->count($srp)) { /* The destination entry exists, overwrite it with the new entry */ $attrs = $this->fetch($srp); foreach (array_keys($attrs) as $name) { if (!is_numeric($name)) { if (in_array($name, ['dn','count'])) { continue; } if (!isset($data[$name])) {
1191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
$data[$name] = []; } } } $ret = $this->modify($data); } else { /* The destination entry doesn't exists, create it */ $ret = $this->add($data); } } else { /* Keep all vars that aren't touched by this ldif */ $ret = $this->modify($data); } } if (!$this->success()) { msg_dialog::display(_('LDAP error'), msgPool::ldaperror($this->get_error(), $dn, '', get_class()), LDAP_ERROR); } return $ret; } /*! * \brief Get the object classes * * \param boolean $force_reload FALSE */ function get_objectclasses ($force_reload = FALSE) { $objectclasses = []; /* Return the cached results. */ if (class_available('session') && session::is_set('LDAP_CACHE::get_objectclasses') && !$force_reload) { $objectclasses = session::get('LDAP_CACHE::get_objectclasses'); return $objectclasses; } // Get base to look for schema $sr = @ldap_read($this->cid, '', 'objectClass=*', ['subschemaSubentry']); $attr = @ldap_get_entries($this->cid, $sr); if (!isset($attr[0]['subschemasubentry'][0])) { return []; } /* Get list of objectclasses and fill array */ $nb = $attr[0]['subschemasubentry'][0]; $objectclasses = []; $sr = ldap_read($this->cid, $nb, 'objectClass=*', ['objectclasses']); $attrs = ldap_get_entries($this->cid, $sr); if (!isset($attrs[0])) { return []; } foreach ($attrs[0]['objectclasses'] as $val) { if (preg_match('/^[0-9]+$/', $val)) { continue; } $name = "OID"; $pattern = explode(' ', $val); $ocname = preg_replace("/^.* NAME\s+\(*\s*'([^']+)'\s*\)*.*$/", '\\1', $val); $objectclasses[$ocname] = []; foreach ($pattern as $chunk) { switch ($chunk) { case '(': $value = ''; break; case ')': if ($name != '') {
1261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
$v = $this->value2container($value); if (in_array($name, ['MUST', 'MAY']) && !is_array($v)) { $v = [$v]; } $objectclasses[$ocname][$name] = $v; } $name = ''; $value = ''; break; case 'NAME': case 'DESC': case 'SUP': case 'STRUCTURAL': case 'ABSTRACT': case 'AUXILIARY': case 'MUST': case 'MAY': if ($name != '') { $v = $this->value2container($value); if (in_array($name, ['MUST','MAY']) && !is_array($v)) { $v = [$v]; } $objectclasses[$ocname][$name] = $v; } $name = $chunk; $value = ''; break; default: $value .= $chunk.' '; } } } if (class_available('session')) { session::set('LDAP_CACHE::get_objectclasses', $objectclasses); } return $objectclasses; } function value2container ($value) { /* Set emtpy values to "TRUE" only */ if (preg_match('/^\s*$/', $value)) { return TRUE; } /* Remove ' and " if needed */ $value = preg_replace('/^[\'"]/', '', $value); $value = preg_replace('/[\'"] *$/', '', $value); /* Convert to array if $ is inside... */ if (preg_match('/\$/', $value)) { $container = preg_split('/\s*\$\s*/', $value); } else { $container = chop($value); } return $container; } /*! * \brief Add a string in log file * * \param string $string */ function log ($string) {
13311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
if (session::is_set('config')) { $cfg = session::get('config'); if (isset($cfg->current['LDAPSTATS']) && preg_match('/true/i', $cfg->current['LDAPSTATS'])) { syslog(LOG_INFO, $string); } } } /* added by Guido Serra aka Zeph <zeph@purotesto.it> */ /*! * \brief Function to get cn * * \param $dn The DN */ function getCn ($dn) { $simple = explode(",", $dn); foreach ($simple as $piece) { $partial = explode("=", $piece); if ($partial[0] == "cn") { return $partial[1]; } } } function get_naming_contexts ($server, $admin = '', $password = '') { /* Build LDAP connection */ $ds = ldap_connect($server); if (!$ds) { die('Can\'t bind to LDAP. No check possible!'); } ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_bind($ds, $admin, $password); /* Get base to look for naming contexts */ $sr = @ldap_read($ds, '', 'objectClass=*', ['namingContexts']); $attr = @ldap_get_entries($ds, $sr); return $attr[0]['namingcontexts']; } } ?>