我正在尝试使用c程序连接到openLDAP服务器,我找到了openLDAP客户端库并实现了以下程序。我尝试连接到this ldap服务器以及我的本地ldap服务器。 我使用此命令编译程序时没有错误
gcc ldapClient.c -o ldapClient -lldap
我尝试使用此命令运行程序
./ldapClient euler password
然后它说
ldap_simple_bind_s:协议错误
我用Google搜索并找到了一些答案,例如this,他们说这个错误来自协议版本未匹配ei: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 );
}
答案 0 :(得分:2)
调用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);
}