3
votes

I am trying to connect to a remote LDAP server from a local Ubuntu VM Box on my Windows machine. The PHP code is:

$ldap = ldap_connect("ldaps://11.22.33.44",636);
ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
$username = "domain\usr";
$password = "blah";
$ds = ldap_bind($ldap, $username, $password );
if( $ds ){
    echo "logged in!";
}
else{
    echo "failed to log in!";
    exit;
}

When running this I get the 'logged in!' message, so I'm assuming that the connection is working. However, when I run this PHP code afterwards:

$sr = ldap_search($ds, "OU=User Accounts,DC=Domain1,DC=foobar,DC=Local", "(|(sn=*))");

I get this error:

Warning: ldap_search(): supplied argument is not a valid ldap link resource in /usr/share/nginx/www/ldap_test.php on line 37

(which refers to the line that contians the ldap_search command. This doesn't make sense if the connection is successful and a link resource is created - any ideas?

1
I would move the port in the ldap_connect into the URI as the second parameter is not used when an LDAP-URI is passers as first parameter. So calling ldap_connect("ldaps://11.22.33.44:636"); would be the better way. - heiglandreas

1 Answers

6
votes

ldap_bind will return true or false. You need to pass the result of ldap_connect to ldap_search instead - which is $ldap in your example.

$sr = ldap_search($ldap, "OU=User Accounts,DC=Domain1,DC=foobar,DC=Local", "(|(sn=*))");