Ticket #547: ldap_v2.patch

File ldap_v2.patch, 19.9 kB (added by laurentj, 4 months ago)

Patch version 2

  • lib/jelix-scripts/commands/createapp.cmd.php

    old new  
    77* @contributor Gildas Givaja (bug #83) 
    88* @contributor Christophe Thiriot 
    99* @contributor Bastien Jaillot 
    10 * @copyright   2005-2007 Jouanneau laurent, 2006 Loic Mathaud, 2007 Gildas Givaja, 2007 Christophe Thiriot, 2008 Bastien Jaillot 
     10* @copyright   2005-2008 Jouanneau laurent, 2006 Loic Mathaud, 2007 Gildas Givaja, 2007 Christophe Thiriot, 2008 Bastien Jaillot 
    1111* @link        http://www.jelix.org 
    1212* @licence     GNU General Public Licence see LICENCE file or http://www.gnu.org/licenses/gpl.html 
    1313*/ 
     
    9090       $this->createFile(JELIX_APP_PATH.'project.xml','project.xml.tpl',$param); 
    9191       $this->createFile(JELIX_APP_CONFIG_PATH.'defaultconfig.ini.php','var/config/defaultconfig.ini.php.tpl',$param); 
    9292       $this->createFile(JELIX_APP_CONFIG_PATH.'dbprofils.ini.php','var/config/dbprofils.ini.php.tpl',$param); 
     93       $this->createFile(JELIX_APP_CONFIG_PATH.'ldapprofils.ini.php','var/config/ldapprofils.ini.php.tpl',$param); 
    9394       $this->createFile(JELIX_APP_CONFIG_PATH.'index/config.ini.php','var/config/index/config.ini.php.tpl',$param); 
    9495 
    9596       $this->createFile(JELIX_APP_PATH.'responses/myHtmlResponse.class.php','myHtmlResponse.class.php.tpl',$param); 
     
    139140    } 
    140141} 
    141142 
    142 ?> 
  • lib/jelix-scripts/templates/var/config/ldapprofils.ini.php.tpl

    old new  
     1;<?php die(''); ?> 
     2;for security reasons, don't remove or modify the first line 
     3 
     4; name of the default profil to use for any connection 
     5default = ldap 
     6 
     7; each section correspond to a connection 
     8; the name of the section is the name of the connection, to use as an argument 
     9; for jLdap methods 
     10 
     11[ldap] 
     12server = "127.0.0.1" 
     13port = "389" 
     14base_dn = "dc=cnw,dc=local" 
     15mail_base = "dc=Mails,dc=Services,dc=cnw,dc=local" 
  • lib/jelix/plugins/acl/ldap/ldap.acl.php

    old new  
     1<?php 
     2/** 
     3* @package     jelix 
     4* @subpackage  acl_driver 
     5* @author      Lissyx 
     6* @contributor  Laurent Jouanneau 
     7* @copyright   2008 Lissyx, 2008  Laurent Jouanneau 
     8* @link        http://www.jelix.org 
     9* @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
     10*/ 
     11 
     12/** 
     13 * driver for jAcl based on a LDAP Server 
     14 * @package jelix 
     15 * @subpackage acl_driver 
     16 * @experimental 
     17 * @since 1.1 
     18 * @see jAcl 
     19 */ 
     20class ldapAclDriver implements jIAclDriver { 
     21 
     22    private $params = array(); 
     23 
     24    private $ldap = null; 
     25 
     26    /** 
     27     *  
     28     */ 
     29    function __construct ($params) { 
     30        $this->params = array_merge($params; 
     31        $this->ldap = jLdap::getConnection(); 
     32    } 
     33 
     34 
     35    protected static $aclres = array(); 
     36 
     37    protected static $acl = array(); 
     38 
     39    /** 
     40     * return the value of the right on the given subject (and on the optional resource) 
     41     * @param string $subject the key of the subject 
     42     * @param string $resource the id of a resource 
     43     * @return array list of values corresponding to the right 
     44     */ 
     45    public function getRight($subject, $resource=null){ 
     46 
     47        if(!jAuth::isConnected()) // not authificated = no rights 
     48            return array(); 
     49 
     50        $session = jAuth::getUserSession(); 
     51 
     52        if ( false !== ($attrs = $this->ldap->searchFirst($session->login, "(" . $this->params["ldapAttributeRight"] . "=*)"))) { 
     53 
     54            $values= array(); 
     55 
     56            foreach ($attrs[$this->params["ldapAttributeRight"]] as $obj) { 
     57                switch($subject) { 
     58                    case "domaines": 
     59                    case "accounts": 
     60                        switch($obj) { 
     61                            case $this->params["ldapRightUser"]: 
     62                                $values[] = "LIST"; 
     63                                $values[] = "READ"; 
     64                            break; 
     65 
     66                            case $this->params["ldapRightAdmin"]: 
     67                                $values[] = "LIST"; 
     68                                $values[] = "UPDATE"; 
     69                                $values[] = "CREATE"; 
     70                                $values[] = "READ"; 
     71                                $values[] = "DELETE"; 
     72                            break; 
     73                        } 
     74                        break; 
     75                } 
     76            } 
     77        } 
     78 
     79        return $values; 
     80    } 
     81 
     82    /** 
     83     * clear right cache 
     84     * @since 1.0b2 
     85     */ 
     86    public function clearCache(){ 
     87        self::$acl = array(); 
     88        self::$aclres = array(); 
     89    } 
     90 
     91} 
     92 
     93 
  • lib/jelix/plugins/auth/ldap/ldap.auth.php

    old new  
     1<?php 
     2/** 
     3* @package    jelix 
     4* @subpackage auth_driver 
     5* @author     Lissyx 
     6* @contributor Laurent Jouanneau 
     7* @copyright  2008 Laurent Jouanneau, 2008 Lissyx 
     8* @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
     9*/ 
     10 
     11/** 
     12 * object which represent a user 
     13 * 
     14 * @package    jelix 
     15 * @subpackage auth_driver 
     16 * @since 1.1 
     17 */ 
     18class jAuthUserLDAP extends jAuthUser { 
     19} 
     20 
     21/** 
     22 * authentification driver, which communicate with a LDAP server 
     23 * @package    jelix 
     24 * @subpackage auth_driver 
     25 * @see jAuth 
     26 * @since 1.1 
     27 * @experimental 
     28 */ 
     29class ldapAuthDriver implements jIAuthDriver { 
     30 
     31    protected $_params; 
     32 
     33    protected $ldap; 
     34 
     35    function __construct($params){ 
     36        $this->_params = $params; 
     37        $this->ldap =  jLdap::getConnection(); 
     38    } 
     39 
     40    /** 
     41     * No write ! 
     42     */ 
     43    public function saveNewUser($user){ 
     44        throw new Exception("Not implemented"); 
     45        return true; 
     46    } 
     47 
     48    /** 
     49     * No write ! 
     50     */ 
     51    public function removeUser($login){ 
     52        throw new Exception("Not implemented"); 
     53        return true; 
     54    } 
     55 
     56    /** 
     57     * No write ! 
     58     */ 
     59    public function updateUser($user){ 
     60        throw new Exception("Not implemented"); 
     61        return true; 
     62    } 
     63 
     64    public function getUser($login){ 
     65        throw new Exception("Not implemented"); 
     66        return null; 
     67    } 
     68 
     69    /** 
     70     * No write ! 
     71     */ 
     72    public function createUserObject($login,$password){ 
     73        throw new Exception("Not implemented"); 
     74        return $user; 
     75    } 
     76 
     77    public function getUserList($pattern){ 
     78        throw new Exception("Not implemented"); 
     79    } 
     80 
     81    /** 
     82     * No write ! 
     83     */ 
     84    public function changePassword($login, $newpassword){ 
     85        throw new Exception("Not implemented"); 
     86 
     87    } 
     88 
     89    public function verifyPassword($login, $password) { 
     90        $this->ldap->bindServer($login, $password,  $this->params["mail_search"], $this->_params["mail_filter"], "mail"); 
     91 
     92        if ($res["res"]) { 
     93            $user = new jAuthUserLDAP(); 
     94            $user->login = $res["DN"]; 
     95            $user->password = $res["password"]; 
     96            return $user; 
     97        } else 
     98            return false; 
     99    } 
     100} 
  • lib/jelix/core/defaultconfig.ini.php

    old new  
    1616modulesPath = lib:jelix-modules/,app:modules/ 
    1717 
    1818dbProfils = dbprofils.ini.php 
     19ldapProfils = ldapprofils.ini.php 
    1920 
    2021theme = default 
    2122use_error_handler = on 
     
    210211driver = db 
    211212enableAcl2DbEventListener = off 
    212213 
     214ldapAttributeRight =  
     215ldapRightUser =  
     216ldapRightAdmin =  
    213217 
    214218[sessions] 
    215219; to disable sessions, set the following parameter to 0 
  • lib/jelix/init.php

    old new  
    159159    if(file_exists($f)){ 
    160160        require_once($f); 
    161161    }else{ 
    162         throw new Exception("Jelix fatal error : Unknow class $class"); 
     162        throw new Exception("Jelix fatal error : Unknow class $class ($f)"); 
    163163    } 
    164164#endif 
    165165} 
  • lib/jelix/core-modules/jelix/locales/fr_FR/ldap.UTF-8.properties

    old new  
     1error.profil.unknow=(400)Le profil de connexion "%s" pour jLdap est inconnu 
     2error.database.unknow=(401)La base %s est inconnue 
     3error.connection=(402)Impossible de se connecter sur %s (mauvais host, login ou mot de passe ?) 
     4error.query.bad=(403)Erreur dans la requête (%s) 
     5error.feature.unsupported=(404)JLdap : le driver %s ne prend pas en charge la fonctionnalité "%s" 
     6error.nofunction=(405)jLdap : l'extension %s n'est pas installée dans php pour le driver jLdap configuré 
     7error.profil.type.unknow=(406)Le type de profil de connexion "%s" pour jLdap est inconnu 
     8error.default.profil.unknow=(407)Pas de profil de connexion par défaut pour jLdap 
     9error.driver.notfound=(408)Le driver "%s" pour jLdap est introuvable 
     10error.connection.closed=(409) La connection sur la base de donnée n'est plus valide : timeout, deconnection... (profil %s) 
  • lib/jelix/utils/jLdap.class.php

    old new  
     1<?php 
     2 
     3/** 
     4* @package    jelix 
     5* @subpackage utils 
     6* @author     Lissyx 
     7* @contributor Laurent Jouanneau 
     8* @copyright  2008 Lissyx, 2008 Laurent Jouanneau 
     9* @link       http://www.jelix.org 
     10* @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
     11*/ 
     12 
     13/** 
     14 * Ldap class to connect to a LDAP 
     15 * @experimental 
     16 * @since 1.1 
     17 */ 
     18class jLdap { 
     19 
     20    /** 
     21    * Connect to LDAP server if not connected 
     22    * @param string $name LDAP profile 
     23    * @return resource LDAP connection resource. 
     24    */ 
     25    public static function getConnection( $name = null ) { 
     26        static $cnxPool = array(); 
     27 
     28        $profil = self::getProfil($name); 
     29        if (!isset ($cnxPool[$name])){ 
     30            $cnxPool[$name] = self::_ldapConnect ($profil); 
     31        } 
     32 
     33        return $cnxPool[$name]; 
     34    } 
     35 
     36    /** 
     37    * Computes SSHA. 
     38    * @param string $pass The password to cipher. 
     39    * @return string The SSHA, including "{SSHA}". 
     40    */ 
     41    /*public static function SSHA($pass) { 
     42        mt_srand((double)microtime()*1000000); 
     43                $salt = pack("CCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand()); 
     44                $hash = "{SSHA}" . base64_encode(pack("H*", sha1($pass . $salt)) . $salt); 
     45        return $hash; 
     46    }*/ 
     47 
     48    /** 
     49    * load properties of a connector profil 
     50    * 
     51    * a profil is a section in the dbprofils.ini.php file 
     52    * 
     53    * with getProfil('myprofil') (or getProfil('myprofil', false)), you get the profil which 
     54    * has the name "myprofil". this name should correspond to a section name in the ini file 
     55    * 
     56    * with getProfil('myprofiltype',true), it will search a parameter named 'myprofiltype' in the ini file. 
     57    * This parameter should contains a profil name, and the corresponding profil will be loaded. 
     58    * 
     59    * with getProfil(), it will load the default profil, (so the profil of "default" type) 
     60    * 
     61    * @param string   $name  profil name or profil type to load. if empty, use the default one 
     62    * @param boolean  $nameIsProfilType  says if the name is a profil name or a profil type. this parameter exists since 1.0b2 
     63    * @return array  properties 
     64    */ 
     65    public static function getProfil ($name='', $nameIsProfilType=false) { 
     66        static $profils = null; 
     67        global $gJConfig; 
     68        if($profils === null){ 
     69            $profils = parse_ini_file(JELIX_APP_CONFIG_PATH.$gJConfig->ldapProfils , true); 
     70        } 
     71 
     72        if ($name == '') { 
     73            if(isset($profils['default'])) 
     74                $name=$profils['default']; 
     75            else 
     76                throw new jException('jelix~ldap.error.default.profil.unknow'); 
     77 
     78        } elseif ($nameIsProfilType) { 
     79            if (isset($profils[$name]) && is_string($profils[$name])) { 
     80                $name = $profils[$name]; 
     81            } else { 
     82                throw new jException('jelix~ldap.error.profil.type.unknow',$name); 
     83            } 
     84        } 
     85 
     86        if(isset($profils[$name]) && is_array($profils[$name])){ 
     87            $profils[$name]['name'] = $name; 
     88            return $profils[$name]; 
     89        } else { 
     90            throw new jException('jelix~ldap.error.profil.unknow',$name); 
     91        } 
     92    } 
     93 
     94    private static function _ldapConnect($profil) { 
     95        $cnx = ldap_connect($profil["server"], $profil["port"]); 
     96        if (!$cnx) { 
     97            throw new Exception('jelix~ldap.error.connect'); 
     98        } else { 
     99            if (ldap_set_option($cnx, LDAP_OPT_PROTOCOL_VERSION, 3) === false) { 
     100                throw new Exception('jelix~ldap.error.set_option'); 
     101            } 
     102        } 
     103 
     104        return new jLdapConnection($cnx, $profil); 
     105    } 
     106 
     107} 
     108 
     109/** 
     110 * Ldap class to query a LDAP. This object is given by jLdap. 
     111 * 
     112 * @experimental 
     113 * @since 1.1 
     114 */ 
     115class jLdapConnection { 
     116 
     117    /** 
     118     * ldap ressource connection 
     119     */ 
     120    protected $conn = null 
     121 
     122    protected $profile; 
     123 
     124    function __construct( $conn, $profile) { 
     125        $this->conn = $conn; 
     126        $this->profile = $profile; 
     127    } 
     128 
     129    /** 
     130    * Get LDAP parameter stored in the profile of the connection 
     131    * @param string $paramName parameter name 
     132    * @return string the value 
     133    */ 
     134    public function getParam($paramName) { 
     135        if (isset($this->profile[$paramName])) 
     136            return $this->profile[$paramName]; 
     137        else 
     138            return null; 
     139    } 
     140 
     141    /** 
     142    * Bind to server. Handle either login as a DN or email address. 
     143    * If an email is passed, resolves to a DN and use it after. 
     144    * 
     145    * @param string $login Login as a DN or email address. 
     146    * @param string $password The password to bind with ... 
     147    * @param string $mailSearch a string definings how to search for email address in LDAP directory. 
     148    *               Scope is subtree. You can use #USER# and #DOMAIN# for them to be replaced with 
     149    *               the username and domainame of the email address. Base DN is added to the string. 
     150    * @param string $mailFilter Which filter to use (e.g. "(objectClass=CourierMailAccount)") 
     151    * @param string $mailAttrib Which attributes defines the email address ... 
     152    * @return array Informations array("res" => true/false, "DN" => ..., "password" => ... ) 
     153    */ 
     154    public function bindServer($login, $password, $mailSearch, $mailFilter, $mailAttrib) { 
     155        $filtre = new jFilter(); 
     156        if ($filtre->isEmail($login)) { 
     157            $mail = explode("@", $login); 
     158            // Search an associated mail account to have the DN, to bind 
     159            $search = str_replace(array("#USER#", "#DOMAIN#"), array($mail[0], $mail[1]), $mailSearch) . "," . $this->getParam('mail_base'); 
     160            if (($result = ldap_search($this->conn, $search, $mailFilter, array($mailAttrib) )) !== false) { 
     161                $data = ldap_get_entries($this->conn, $result); 
     162                $DN = $data[0]["dn"]; 
     163            } else { 
     164                throw new Exception("LDAP search failed !"); 
     165            } 
     166        } else if ($filtre->isDN($login)) { 
     167            // we already have a DN...  
     168            $DN = $login; 
     169        } else { 
     170            throw new Exception("No valid account."); 
     171        } 
     172 
     173        $res = false; 
     174 
     175        if (ldap_bind($this->conn, $DN, $password) === false) { 
     176            throw new Exception("Cannot bind LDAP server."); 
     177        } else { 
     178            // Bind OK 
     179            $res = true; 
     180        } 
     181 
     182        return array("res" => $res, "DN" => $DN, "password" => $password); 
     183    } 
     184 
     185    /** 
     186    * Add values to the LDAP directory. 
     187    * @param string $target The DN to play with ... 
     188    * @param array $valeurs Associative array of attrib => values to put in. 
     189    */ 
     190    public function add($target, $valeurs) { 
     191        return ldap_add($this->conn, $target, $valeurs); 
     192    } 
     193 
     194    /** 
     195    * Modify values of the LDAP directory. 
     196    * @param string $target The DN to play with ... 
     197    * @param array $valeurs Associative array of attrib => values to put in. 
     198    */ 
     199    public function modify($target, $valeurs) { 
     200        return ldap_modify($this->conn, $target, $valeurs); 
     201    } 
     202 
     203    /** 
     204    * List in the LDAP directory. 
     205    * Uses the ldap_list() function ... 
     206    * @param string $target The DN to list from 
     207    * @param string $filter The filter to use 
     208    * @param array $attrib Attributes to get back. 
     209    * @return array Array of values found. 
     210    */ 
     211    public function getAll($target, $filter, $attrib) { 
     212        $list = ldap_list($this->conn, $target, $filter, $attrib); 
     213        if(!$list) 
     214            return $list; 
     215        return ldap_get_entries($this->conn, $list ); 
     216    } 
     217 
     218    /** 
     219    * Search in the LDAP directory. 
     220    * Uses the ldap_search() function ... 
     221    * @param string $target The DN to list from 
     222    * @param string $filter The filter to use 
     223    * @param array $attrib Attributes to get back. 
     224    * @return array Array of values found. 
     225    */ 
     226    public function searchAll( $target, $filter, $attrib) { 
     227        $list = ldap_search($this->conn, $target, $filter, $attrib); 
     228        if(!$list) 
     229            return $list; 
     230        return ldap_get_entries($this->conn, $list); 
     231    } 
     232 
     233    /** 
     234    * Search in the LDAP directory. and return only the first entry 
     235    * Uses the ldap_search() function ... 
     236    * @param string $target The DN to list from 
     237    * @param string $filter The filter to use 
     238    * @param array $attrib Attributes to get back. 
     239    * @return array found value 
     240    */ 
     241    public function searchFirst( $target, $filter, $attrib) { 
     242        $list = ldap_search($this->conn, $target, $filter, $attrib); 
     243        if(!$list) 
     244            return $list; 
     245        $first = ldap_first_entry($this->conn, $list); 
     246        if(!$first) 
     247            return $first; 
     248        return ldap_get_attributes($this->conn, $first); 
     249    } 
     250 
     251 
     252    /** 
     253    * Delete from the LDAP directory. 
     254    * Uses the ldap_delete() function ... 
     255    * @param string $target The DN to list from 
     256    * @return boolean Does it worked ?  
     257    */ 
     258    public function delete($target) { 
     259        return ldap_delete($this->conn, $target); 
     260    } 
     261 
     262    /** 
     263    * Makes a BIND ! 
     264    * @return boolean true or false, wether the bind was successfull or not. 
     265    */ 
     266    public function reBind()  { 
     267        $auth = jAuth::getUserSession(); 
     268        $res = $this->bindServer($auth->login, $auth->password, null, null, null); 
     269        return $res["res"]; 
     270    } 
     271 
     272} 
     273 
     274 
     275 
     276 
     277 
     278 
  • lib/jelix/utils/jFilter.class.php

    old new  
    7979        return in_array($val, array('true','false','1','0','TRUE', 'FALSE','on','off')); 
    8080    } 
    8181 
     82    /** 
     83     * check if the given value looks like a valid Distinguished Name 
     84     * @param string $val the value 
     85     * @return boolean true if it looks like a vali DN 
     86     */ 
     87    static public function isDN($val) { 
     88        $reg = "/^[a-zA-Z]+=[\.\w\d\s]+(,[a-zA-Z]+=[\.\w\d\s]+)*$/"; 
     89        // echo "Testing $val against $reg.\n<br />"; 
     90        return filter_var($val, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => $reg))); 
     91    } 
    8292 
    8393    /** 
    8494     * check if the given value is a float 
Download in other formats: Original Format