AD on Windows Server 2012 + Windows LDAP + PHP Bind - php

I've set up Active Directory and ADLDAP on Windows server 2012. I'm trying a simple ldap_bind but continue to have a "invalid credentials" error spit back to me.
In my AD Users and Groups screen, I clearly see the domain I made along with the OU (organizational unit) and users inside of it. ASDI Edit clearly tells me the DN for that user:
CN=Bob Smith,OU=Accounting,DC=mydomain,DC=net
Further, the BaseDN is clearly told to me in ASDI Edit because it's above the OU group "accounting" -
DC=mydomain,DC=net
Now onto my script - which throws no LDAP connect errors, only on bind, with a constant invalid credentials:
$connectionLDAP = "LDAP://localhost:54126";
$basedn = 'DC=mydomain,DC=net';
$ldap = ldap_connect($connectionLDAP) or die("Could not connect to LDAP server.");
$username = $post['username'];
$password = $post['password'];
$usernameForBind = "CN=".$username.",OU=Accounting,".$basedn;
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($ldap, $usernameForBind, $password);
This spits the following warning, and of course my script ends there since there is no positive match to username and password found:
Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\....\login.php on line 41
And the below error echos produce this:
echo(ldap_error($ldap)."<br>");
echo(ldap_errno($ldap)."<br>");
Invalid credentials
49
I have tried every combination of DN, username, email address, mydomain\username without the rest of the DN info, everything I can think of....but for the life of me it won't take, and google + Stack searches unfortunately aren't helping me at the moment get past this.
Thanks for any assistance.

You are using Active directory on Windows, So please change your code to following It would work. Because AD need #domain_name as username suffix in bind function.
$connectionLDAP = "ldap://localhost";
$basedn = '#mydomain.net';
$ldap = #ldap_connect($connectionLDAP, 54126) or die("Could not connect to LDAP server.");
$username = $post['username'];
$password = $post['password'];
$usernameForBind = $username.$basedn;
#ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
#ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = #ldap_bind($ldap, $usernameForBind, $password);
I've tested such scenarios many times, It works for AD.
And also Please make sure that your AD server is running on the same port you're using in code ie. 54126.

Related

Authenticating against LDAP with ApacheDS and PHP

I'm wondering how to authenticate against ApacheDS in PHP. I keep getting a "Invalid Credentials" when I try to log on as a user in a group. I can log in as "uid=admin,ou=system" just fine, but if I try "uid=,ou=consumers,ou=system", it returns "Invalid Credentials".
It is important to note that this is not the full DN of the entry. It's more like "uid=...+gn=...+...,ou=consumers,ou=system". I can search and find this value just fine when bound to the administrator and the API account.
How do I bind to a user just to authenticate and retrieve information on them (like the rest of their attributes and the children of their entry? Here's what I'm doing and failing.
$dn = ldap_connect($serveraddress,10389);
$bn = ldap_bind("uid=".$user.",ou=consumers,ou=system");
var_dump($bn);
var_dump(ldap_error($dn);
Thank you for any help you can provide.
Edit: So I've gotten farther. Why is this a protocol error?
$ds=ldap_connect("192.168.1.126",10389); // must be a valid LDAP server!
if ($ds) ldap_bind($ds,"uid=apiaccess,ou=system",...);
else die("!Can't connect to server");
$userid = md5($user);
$results = ldap_get_entries($ds,ldap_search($ds,"ou=consumers,ou=system","(uid=".$userid.")"));
$result = $results[0]["dn"];
echo $result;
if ($ds) ldap_bind($ds,$result,$pass);
else die("!Can't connect to server");
var_dump(ldap_error($ds));
You need to tell PHP to use LDAPv3.
Before you call ldap_bind, add the following call:
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

I am unable to bind to an LDAP server due to "Invalid Credentials", though the credentials are valid

I'm setting up a php page to connect to an LDAP server but for some reason it will not let me connect. At first I thought that my credentials had not been set up correctly, but after entering them into Softerra LDAP browser I was able to connect there.
<?php
$url = "ldaps://ldap.XXX.XXXX.edu:PORT/o=XXXX.edu";
$ldap_user = "uid=XXXXXXXX,ou=Campus Accounts,o=XXXX.edu";
$ldap_pass = "XXXXXXXX";
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
$conn = ldap_connect($url) or die ("Could not connect to server");
if ($conn)
{
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3) ;
$bind = ldap_bind($conn, $ldap_user, $ldap_pass);
}
?>
But all that I get back is the following message.
Warning: ldap_bind(): Unable to bind to server: Invalid credentials
Is there something extra I need to do to the user data to get it to be accepted?
If you are connecting to Active Directory (which is implied by the o=XXX.edu style notation (though if so, incorrect)) and by the comment suggestions of trying to bind as xxxx.edu\xxxx then the root most nodes in Active Directory are always dc= not o= and therefore a more correct bind DN or base DN would most likely finish as:
dc=xxx,dc=edu

LDAP Authenticating user in PHP

I'm building an authentication script from PHP to LDAP. My problem is that I don't really know how to check for the user if the user isn't my admin.
I don't really understand ldap_bind - here I can only login as my admin user, but then I can search for other users in my ou, but I don't know how to check their password.
What I have so far:
function login($up, $pw){
$ldap = ldap_connect("dejan.local") or die("Could not connect to LDAP server.");
if ($ldap){
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
//if I try $up and $pw here, I get an error
if ($bind = ldap_bind($ldap, "Admin", "Somepassword")){
$sr = ldap_search($ldap, "ou=testunit,DC=dejan,DC=local", "samaccountname=$up");
$info = ldap_get_entries($ldap, $sr);
//so here I've gotten information from the user $up
//but I would like to check if his password matches and then get his information
}
}
}
I've looked at some sort of auth scripts from others and they check the information through ldap_bind, but I can only connect with my admin user.
I believe the only change you need to make is:
if ($bind = ldap_bind($ldap, "$up#dejan.local", $pw)){
Which will make the request local to the specific domain. With Active Directory (which is somewhat different, blame Kerberos), you have to provide a context for login.

PHP ldap_bind() authentication - error Unable to bind to server: Invalid credentials?

I'm trying to authenticate a user with LDAP using PHP. I have the DN for the user which I have checked to be correct. I also have a password. This is the correct password for the user when they authenticate with SamAccountName.
I am hoping this is the password to use when authenticating with the DN. There isn't a Distinguished Name specific password for LDAP is there? The following is my code to authenticate using PHP's ldap_bind() function. Am I doing this correct?
$ldaphost="ldap://somehost.com:3268";
$dn = "cn=LastName\, FirstName Dept/Country/ext,OU=Accounts,OU=Location,ou=Division,";
$basedn="dc=abc,dc=enterprise";
if (!($connect = ldap_connect($ldaphost))) {
die ("Could not connect to LDAP server");
}
$ldapbind = ldap_bind($connect, "$dn" . "$basedn", $password);
if ($ldapbind) {
echo "LDAP bind successful...";
} else {
echo "LDAP bind failed...";
}
The result I get from the above code is :
Warning: ldap_bind() [function.ldap-bind]: Unable to bind to server:
Invalid credentials LDAP bind failed...
From the line where ldap_bind() call is made:
$ldapbind = ldap_bind($connect, "$dn" . "$basedn", $password);
Invalid credentials makes me believe there is something wrong potentially with the DN or password. I have triple checked the DN and there is not an error there as far as I can see.
Any ideas?
I guess you are connecting to a Microsoft Domain, you can try the domain syntax for the credentials then. For User015 in DOMAIN - DOMAIN\user015
When ever dealing with ldap I always find jxplorer useful

Authenticating user using LDAP from PHP

My project is to make a module enrollment system for our university. So I contacted the IT people in my university for details to authenticate the students into the system. We are developing the system using the existing university login. They gave me some LDAP information, I don't know the usage of that.
I'm using PHP,Mysql on an Apacha server.
How can I authenticate a user logging into my system, given his userid and password with the LDAP information.
Given below is the LDAP information(i have changed the domain name etc.)
LDAP information for blueroom.ac.uk domain
LDAP Host : ad.blueroom.ac.uk
LDAP port no: 389
BASE DN : ou=bluebird, dc=bluebird, dc=ac, dc=my
LDAP account to bind : cn = kikdap, ou=servacc, dc=bluebird,dc=ac,dc=uk
LDAP account password : ********
Attribute : sAMAccountName
The general procedure would be (relevant ext/ldap php commands in brackets):
connect to LDAP server using the "LDAP Host" and "LDAP port no" (ldap_connect()) and set the correct connection options (ldap_set_option()), especially LDAP_OPT_PROTOCOL_VERSION and LDAP_OPT_REFERRALS
bind to LDAP server using the "LDAP account to bind" and "LDAP account password" (ldap_bind()) - if you're authenticating against an Active Directory server you can directly use the username and password from the login page and skip all the following steps.
search the tree for a matching user entry/object by specifing the "BASE DN" and the appropriate LDAP filter - most likely something like (&(objectClass=user)(sAMAccountName=%s)) where %s should be replaced by the username to be authenticated (ldap_search())
check if the number of returned entries is 1 (if <> 1 then something has gone wrong, e.g. no user found or multiple users found)
retrive the distinguished name (DN) of this single entry (ldap_get_dn())
use the DN found in the last step to try to bind to the LDAP server with the password given at the authentication page (ldap_bind())
if the bind succeeds then everything is OK, if not, most likely the password is wrong
It's really not as hard as it sounds at first. Generally I'd propose to use some sort of standard library for authenticating against a LDAP server such as the Net_LDAP2 PEAR package or Zend_Ldap out of the Zend Framework. I have no experience with actually using Net_LDAP2 (although I know the code quite well) but Zend_Ldap works very well against Active Directory servers or ADAMS servers (which is obviously what you're working with).
This will do the trick using Zend_Ldap:
$options = array(
'host' => 'ad.blueroom.ac.uk',
'useStartTls' => true,
'accountDomainName' => 'blueroom.ac.uk',
'accountCanonicalForm' => 4,
'baseDn' => 'ou=bluebird,dc=bluebird,dc=ac,dc=my',
);
$ldap = new Zend_Ldap($options);
try {
$ldap->bind('user', 'password');
} catch (Zend_Ldap_Exception $e) {
// something failed - inspect $e
}
// bind successful
$acctname = $ldap->getCanonicalAccountName('user', Zend_Ldap::ACCTNAME_FORM_DN);
You might try http://code.activestate.com/recipes/101525/ while referring to http://us3.php.net/ldap and other results from a Google search for [php ldap authentication].
#Stephen provided good points. Here is my plain PHP code to authenticate using AD:
first you need to know this parameters: server host, user domain (you need also base dn if you want query AD).
use the following code:
$ldap = ldap_connect($host); // e.g. 165.5.54.6 or an URL
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); // Recommended for AD
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($ldap, $username.'#'.$userDomain, $passwrod);
if($bind){
// successful authentication.
}
you could use http://pear.php.net/package/Net_LDAP2/docs
it's nice and works.
Example of connection taken by the doc:
// Inclusion of the Net_LDAP2 package:
require_once 'Net/LDAP.php';
// The configuration array:
$config = array (
'binddn' => 'cn=admin,ou=users,dc=example,dc=org',
'bindpw' => 'password',
'basedn' => 'dc=example,dc=org',
'host' => 'ldap.example.org'
);
// Connecting using the configuration:
$ldap = Net_LDAP2::connect($config);
// Testing for connection error
if (PEAR::isError($ldap)) {
die('Could not connect to LDAP-server: '.$ldap->getMessage());
}

Categories