openLDAP c 客户端协议错误(ldap_simple_bind_s:协议错误)

openLDAP c client protocol error (ldap_simple_bind_s: Protocol error)

我正在尝试使用 c 程序连接到 openLDAP 服务器,我找到了 openLDAP 客户端库并实现了以下程序。我尝试连接到 this ldap 服务器以及我的本地 ldap 服务器。 我使用这个命令

编译程序没有错误
gcc ldapClient.c -o ldapClient -lldap

我尝试 运行 使用此命令的程序

./ldapClient euler password

然后它说

ldap_simple_bind_s: Protocol error

我用谷歌搜索并找到了一些答案,例如 this ,他们说这个错误伴随着协议版本未匹配 e.i: LDAPv2 和 LDAPv3 ,但我不知道如何解决这个问题问题

  #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 );
}

调用ldap_initialize后需要设置协议类型,使用:

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);
}