Mcsky
Mcsky

Reputation: 1445

ldapsearch on dn working, but not with php

I'm working with a service provider, who handles the hosting of the virtual server and the configuration of the LDAP server.

My job is to create a PHP application that use the LDAP to create a user at login (to keep things simple).

After many shares with the service provider, I finally achieve to contact the LDAP server with the following:

ldapsearch -x -LLL -h vmdc2.local -D email@local -w myPassword -b"CN=xxx,OU=APPLICATIF,OU=GROUPES,OU=UTILISATEUR,DC=enterprise,DC=local"

It comes from the ldap-utils Linux packet. This query returns good results.


So I created a simple script, trying to use the previous DN to list the users in CLI, for proof of work in PHP.

<?php

// create connection to LDAP server
$ldapconn = ldap_connect("ldap://vmdc2.local")
    or die("Impossible to connect to the LDAP server.");

$ldapbind = ldap_bind($ldapconn, 'email@local', 'myPassword');

// check binding
if ($ldapbind) {
    echo "Successfully connected to LDAP !" . PHP_EOL;

    $dn = 'CN=xxx,OU=APPLICATIF,OU=GROUPES,OU=UTILISATEUR,DC=enterprise,DC=local';
    $sr = ldap_list($ldapconn, $dn, 'cn=*');
    if (false === $sr) {
        die('Impossible to use the dn: ' . $dn . PHP_EOL);
    }

    $info = ldap_get_entries($ldapconn, $sr);

    if ($info['count'] === 0) die('No entries :(');
    for ($i=0; $i < $info["count"]; $i++) {
        echo $info[$i]["cn"][0] . PHP_EOL;
    }
} else {
    $var = '';
    ldap_get_option($ldapconn, LDAP_OPT_ERROR_STRING, $var);

    echo "Connection to LDAP failed..." . PHP_EOL . $var . PHP_EOL;
}

The script output No entries :(

My questions

It's my first time against LDAP, so I'm lost :/ Your help is really welcome!

Upvotes: 3

Views: 280

Answers (0)

Related Questions