Welcome Guest, Not a member yet? Register   Sign In
[solved] Ldap (linux) user validation
#1

[eluser]mjsilva[/eluser]
Hi igniters,

I need to check againts a ldap linux server if a provided user and password are valid.

Does someone have the code for that? Have someone did something like it?


Tks in advance.
#2

[eluser]mjsilva[/eluser]
Ok, I was able to do it with Net_LDAP2 from PEAR.

Let me explain:

1) Follow this instructions to integrate PEAR: http://codeigniter.com/wiki/PEAR_integration/

2) Donwload NET_LDAP2 from here: http://pear.php.net/package/Net_LDAP2/download and copy to your brand new created directory something like this:

Code:
pear/
pear/Net
pear/Net/LDAP2.php
pear/Net/LDAP2
pear/Net/LDAP2/RootDSE.php
pear/Net/LDAP2/LDIF.php
pear/Net/LDAP2/Filter.php
pear/Net/LDAP2/Search.php
pear/Net/LDAP2/Entry.php
pear/Net/LDAP2/SimpleFileSchemaCache.php
pear/Net/LDAP2/SchemaCache.interface.php
pear/Net/LDAP2/Util.php
pear/Net/LDAP2/Schema.php
pear/PEAR.php

Next I've created 2 functions to serve my porpuses, one to validate a user and other to get user data:

To check if user is valid:
Code:
function _checkLdap($user, $password){
        if(empty($user) OR empty($password)){ return FALSE; }

        $this->load->library('pearloader');
        $net_ldap = $this->pearloader->load('Net','LDAP2');

        $config = array (
        'basedn'   => 'dc=example,dc=com',
        'host'     => 'ldap.example.com',
        'binddn'   => 'uid='.$user.',ou=people,dc=example,dc=com',
        'bindpw'   => $password
        );

        $ldap = Net_LDAP2::connect($config);

        if (Net_LDAP2::isError($ldap)) {
            return FALSE;
        }else{
            return TRUE;
        }
    }


To get user parameters:
Code:
function _getLdap($user, $password){
        if(empty($user) OR empty($password)){ return FALSE; }

        $this->load->library('pearloader');
        $net_ldap = $this->pearloader->load('Net','LDAP2');

        $config = array (
        'basedn'   => 'dc=example,dc=com',
        'host'     => 'ldap.example.com',
        'binddn'   => 'uid='.$user.',ou=people,dc=example,dc=com',
        'bindpw'   => $password
        );

        $ldap = Net_LDAP2::connect($config);

        $filter = 'uid='.$user;
        $searchbase = 'dc=example,dc=com';
        $options = array(
        'scope' => 'sub',
        'attributes' => array('uid', 'cn', 'mail')
        );

        $result = $ldap->search($searchbase, $filter, $options);

        $entries = $result->entries();

        if (count($entries) != 1){

            return FALSE;
        }else{
            foreach ($entries as $entry) {
                $return['uid'] = $entry->getValue('uid');
                $return['cn'] = $entry->getValue('cn');
                $return['mail'] = $entry->getValue('mail');
            }
            return $return;
        }
    }


Any questions or constructive ideas are welcome.

cheers




Theme © iAndrew 2016 - Forum software by © MyBB