copenldapldapconnectionldap-client

openLDAP c client protocol error (ldap_simple_bind_s: Protocol error)


I am trying to connect to openLDAP server using c program, I found openLDAP client library and I implement the following program. and I try to connect to this ldap server as well as my local ldap server. I compile the program with no error using this command

gcc ldapClient.c -o ldapClient -lldap

and I try to run the program using this command

./ldapClient euler password

then it says

ldap_simple_bind_s: Protocol error

I googled and found some answers like this ,they say this error come with protocol version miss match e.i: LDAPv2 and LDAPv3 , but I couldn't able to fine how to solve this problem

  #include <stdio.h>
#include <ldap.h>
/* LDAP Server settings */
#define LDAP_SERVER "ldap://ldap.forumsys.com:389"
int
main( int argc, char **argv )
{
LDAP        *ld;
int        rc;
char        bind_dn[100];

/* Get username and password */
if( argc != 3 )
{
perror( "invalid args, required: username password" );
return( 1 );
}
sprintf( bind_dn, "cn=%s,ou=mathematicians,dc=example,dc=com", argv[1] );
printf( "Connecting as %s...\n", bind_dn );

/* Open LDAP Connection */
if( ldap_initialize( &ld, LDAP_SERVER ) )
{
perror( "ldap_initialize" );
return( 1 );
}

/* User authentication (bind) */
rc = ldap_simple_bind_s( ld, bind_dn, argv[2] );
if( rc != LDAP_SUCCESS )
{
fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc) );
return( 1 );
}
printf( "Successful authentication\n" );
ldap_unbind( ld );
return( 0 );
}

Solution

  • After calling ldap_initialize you need to set the protocol type, using:

    int protocol_version = LDAP_VERSION3;
    rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &protocol_version);
    if (rc != LDAP_SUCCESS) {
        fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc));
        return(1);
    }