<commit_before><?php

/*
 * Copyright 2005-2015 Centreon
 * Centreon is developped by : <NAME> and <NAME> under
 * GPL Licence 2.0.
 *
 * 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.
 *
 * 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, see <http://www.gnu.org/licenses>.
 *
 * Linking this program statically or dynamically with other modules is making a
 * combined work based on this program. Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this program give Centreon
 * permission to link this program with independent modules to produce an executable,
 * regardless of the license terms of these independent modules, and to copy and
 * distribute the resulting executable under terms of Centreon choice, provided that
 * Centreon also meet, for each linked independent module, the terms  and conditions
 * of the license of that module. An independent module is a module which is not
 * derived from this program. If you modify this program, you may extend this
 * exception to your version of the program, but you are not obliged to do so. If you
 * do not wish to do so, delete this exception statement from your version.
 *
 * For more information : <EMAIL>
 *
 * SVN : $URL$
 * SVN : $Id$
 *
 */

require_once _CENTREON_PATH_ . 'www/class/centreonLDAP.class.php';

/**
 * Class for Ldap authentication
 */
class CentreonAuthLDAP {

    var $pearDB;
    var $ldap;
    var $CentreonLog;
    var $contactInfos;
    var $typePassword;
    var $debug;
    var $firstCheck = true;
    protected $arId;

    /**
     * Constructor
     *
     * @param CentreonDB $pearDB Connection to centreon database
     * @param CentreonLog $CentreonLog Log event
     * @param string $login The username
     * @param string $password The <PASSWORD>
     * @param string $contactInfos
     * @param int $arId | Auth Ressource ID
     * @return void
     */
    function CentreonAuthLDAP($pearDB, $CentreonLog, $login, $password, $contactInfos, $arId) {
        $this->arId = $arId;
        $this->pearDB = $pearDB;

        $this->CentreonLog = $CentreonLog;

        $this->ldap = new CentreonLDAP($pearDB, $CentreonLog, $arId);
        $this->ldap->connect();
        $this->ds = $this->ldap->getDs();

        /*
         * Set contact Informations
         */
        $this->contactInfos = $contactInfos;

        /*
         * Keep password
         */
        $this->typePassword = $password;

        $this->debug = $this->getLogFlag();
    }

    /**
     * Is loging enable ?
     *
     * @return int 1 enable 0 disable
     */
    private function getLogFlag() {
        global $pearDB;

        $res = $pearDB->query("SELECT value FROM options WHERE `key` = 'debug_ldap_import'");
        $data = $res->fetchRow();
        if (isset($data["value"])) {
            return $data["value"];
        }
        return 0;
    }

    /**
     * Check the user pass
     *
     */
    function checkPassword() {

        /*
         * Check if it's a new user
         */
        $newUser = false;
        if (!isset($this->contactInfos['contact_ldap_dn']) || $this->contactInfos['contact_ldap_dn'] == '') {
            $this->contactInfos['contact_ldap_dn'] = $this->ldap->findUserDn($this->contactInfos['contact_alias']);
            $newUser = true;
        }

        /*
         * LDAP BIND
         */
        if (!isset($this->contactInfos['contact_ldap_dn']) || trim($this->contactInfos['contact_ldap_dn']) == '') {
            return 2;
        }
        @ldap_bind($this->ds, $this->contactInfos['contact_ldap_dn'], $this->typePassword);
        if ($this->debug) {
            $this->CentreonLog->insertLog(3, "Connexion = " . $this->contactInfos['contact_ldap_dn'] . " :: " . ldap_error($this->ds));
        }

        /*
         * In some case, we fallback to local Auth
         * 0 : Bind succesfull => Default case
         * 2 : Protocol error
         * -1 : Can't contact LDAP server (php4) => Fallback
         * 51 : Server is busy => Fallback
         * 52 : Server is unavailable => Fallback
         * 81 : Can't contact LDAP server (php5) => Fallback
         */
        if (isset($this->ds) && $this->ds) {
            switch (ldap_errno($this->ds)) {
                case 0:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : OK, let's go ! ");
                    if (false == $this->updateUserDn()) {
                        return 0;
                    }
                    return 1;
                    break;
                case 2:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : Protocol Error ");
                    return 2;
                    break;
                case -1:
                case 51:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : Error, Server Busy. Try later");
                    return -1;
                    break;
                case 52:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : Error, Server unavailable. Try later");
                    return -1;
                    break;
                case 81:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : Error, Fallback to Local AUTH");
                    return 2;
                    break;
                default:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : LDAP don't like you, sorry");
                    if ($this->firstCheck && $this->updateUserDn()) {
                        $this->firstCheck = false;
                        return $this->checkPassword();
                    }
                    return 0;
                    break;
            }
        } else {
            if ($this->debug)
                $this->CentreonLog->insertLog(3, "DS empty");
            return 0; /* 2 ?? */
        }
    }

    /**
     * Search and update the user dn
     *
     * @return bool If the DN is modified
     */
    function updateUserDn() {
        if ($this->ldap->rebind()) {
            $userDn = $this->ldap->findUserDn(html_entity_decode($this->contactInfos['contact_alias'], ENT_QUOTES, 'UTF-8'));

            if (false === $userDn) {
                $this->CentreonLog->insertLog(3, "LDAP AUTH : No DN for user " . html_entity_decode($this->contactInfos['contact_alias'], ENT_QUOTES, 'UTF-8'));
                return false;
            }

            /*
             * Get ldap user informations
             */
            $userInfos = $this->ldap->getEntry($userDn);<commit_msg>Begin fix the ldap problem who try to mush to connect to ldap server when the password is wrong<commit_after><?php

/*
 * Copyright 2005-2015 Centreon
 * Centreon is developped by : <NAME> and <NAME> under
 * GPL Licence 2.0.
 *
 * 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.
 *
 * 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, see <http://www.gnu.org/licenses>.
 *
 * Linking this program statically or dynamically with other modules is making a
 * combined work based on this program. Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this program give Centreon
 * permission to link this program with independent modules to produce an executable,
 * regardless of the license terms of these independent modules, and to copy and
 * distribute the resulting executable under terms of Centreon choice, provided that
 * Centreon also meet, for each linked independent module, the terms  and conditions
 * of the license of that module. An independent module is a module which is not
 * derived from this program. If you modify this program, you may extend this
 * exception to your version of the program, but you are not obliged to do so. If you
 * do not wish to do so, delete this exception statement from your version.
 *
 * For more information : <EMAIL>
 *
 * SVN : $URL$
 * SVN : $Id$
 *
 */

require_once _CENTREON_PATH_ . 'www/class/centreonLDAP.class.php';

/**
 * Class for Ldap authentication
 */
class CentreonAuthLDAP {

    var $pearDB;
    var $ldap;
    var $CentreonLog;
    var $contactInfos;
    var $typePassword;
    var $debug;
    var $firstCheck = true;
    protected $arId;

    /**
     * Constructor
     *
     * @param CentreonDB $pearDB Connection to centreon database
     * @param CentreonLog $CentreonLog Log event
     * @param string $login The username
     * @param string $password The user password
     * @param string $contactInfos
     * @param int $arId | Auth Ressource ID
     * @return void
     */
    function CentreonAuthLDAP($pearDB, $CentreonLog, $login, $password, $contactInfos, $arId) {
        $this->arId = $arId;
        $this->pearDB = $pearDB;

        $this->CentreonLog = $CentreonLog;

        $this->ldap = new CentreonLDAP($pearDB, $CentreonLog, $arId);
        $this->ldap->connect();
        $this->ds = $this->ldap->getDs();

        /*
         * Set contact Informations
         */
        $this->contactInfos = $contactInfos;

        /*
         * Keep password
         */
        $this->typePassword = $password;

        $this->debug = $this->getLogFlag();
    }

    /**
     * Is loging enable ?
     *
     * @return int 1 enable 0 disable
     */
    private function getLogFlag() {
        global $pearDB;

        $res = $pearDB->query("SELECT value FROM options WHERE `key` = 'debug_ldap_import'");
        $data = $res->fetchRow();
        if (isset($data["value"])) {
            return $data["value"];
        }
        return 0;
    }

    /**
     * Check the user pass
     *
     */
    function checkPassword() {

        /*
         * Check if it's a new user
         */
        $newUser = false;
        if (!isset($this->contactInfos['contact_ldap_dn']) || $this->contactInfos['contact_ldap_dn'] == '') {
            $this->contactInfos['contact_ldap_dn'] = $this->ldap->findUserDn($this->contactInfos['contact_alias']);
            $newUser = true;
        /* Validate if user exists in this resource */
        } else if (isset($this->contactInfos['contact_ldap_dn'])
            && $this->contactInfos['contact_ldap_dn'] != ''
            && $this->ldap->findUserDn($this->contactInfos['contact_alias']) !== $this->contactInfos['contact_ldap_dn']) {
            return 2;
        }

        /*
         * LDAP BIND
         */
        if (!isset($this->contactInfos['contact_ldap_dn']) || trim($this->contactInfos['contact_ldap_dn']) == '') {
            return 2;
        }
        @ldap_bind($this->ds, $this->contactInfos['contact_ldap_dn'], $this->typePassword);
        if ($this->debug) {
            $this->CentreonLog->insertLog(3, "Connexion = " . $this->contactInfos['contact_ldap_dn'] . " :: " . ldap_error($this->ds));
        }

        /*
         * In some case, we fallback to local Auth
         * 0 : Bind succesfull => Default case
         * 2 : Protocol error
         * -1 : Can't contact LDAP server (php4) => Fallback
         * 51 : Server is busy => Fallback
         * 52 : Server is unavailable => Fallback
         * 81 : Can't contact LDAP server (php5) => Fallback
         */
        if (isset($this->ds) && $this->ds) {
            switch (ldap_errno($this->ds)) {
                case 0:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : OK, let's go ! ");
                    if (false == $this->updateUserDn()) {
                        return 0;
                    }
                    return 1;
                    break;
                case 2:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : Protocol Error ");
                    return 2;
                    break;
                case -1:
                case 51:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : Error, Server Busy. Try later");
                    return -1;
                    break;
                case 52:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : Error, Server unavailable. Try later");
                    return -1;
                    break;
                case 81:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : Error, Fallback to Local AUTH");
                    return 2;
                    break;
                default:
                    if ($this->debug)
                        $this->CentreonLog->insertLog(3, "LDAP AUTH : LDAP don't like you, sorry");
                    /*if ($this->firstCheck && $this->updateUserDn()) {
                        $this->firstCheck = false;
                        return $this->checkPassword();
                    }*/
                    return 0;
                    break;
            }
        } else {
            if ($this->debug)
                $this->CentreonLog->insertLog(3, "DS empty");
            return 0; /* 2 ?? */
        }
    }

    /**
     * Search and update the user dn
     *
     * @return bool If the DN is modified
     */
    function updateUserDn() {
        if ($this->ldap->rebind()) {
            $userDn = $this->ldap->findUserDn(html_entity_decode($this->contactInfos['contact_alias'], ENT_QUOTES, 'UTF-8'));

            if (false === $userDn) {
                $this->CentreonLog->insertLog(3, "LDAP AUTH : No DN for user " . html_entity_decode($this->contactInfos['contact_alias'], ENT_QUOTES, 'UTF-8'));
                return false;
            }

            /*
             * Get ldap user informations
             */
            $userInfos = $this->ldap->getEntry($userDn);
{'fraction_non_alphanumeric': 0.11508522345993827, 'fraction_numerical': 0.007448664608777345, 'mean_word_length': 1.8246777862016679, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 168, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3425530', 'n_tokens_mistral': 4292, 'n_tokens_neox': 4047, 'n_words': 1271}