我正在尝试使用 C 程序连接到 openLDAP 服务器,我找到了 openLDAP 客户端库并实现了以下程序。我尝试连接到 this LDAP 服务器以及我的本地 LDAP 服务器。 我使用此命令编译程序没有错误
gcc ldapClient.c -o ldapClient -lldap
我尝试使用此命令运行程序
./ldapClient euler password
然后它说
ldap_simple_bind_s: Protocol error
我用谷歌搜索并找到了一些答案,例如 this ,他们说这个错误是由于协议(protocol)版本丢失匹配 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
后,您需要设置协议(protocol)类型,使用:
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);
}
关于c - openLDAP c 客户端协议(protocol)错误 (ldap_simple_bind_s : Protocol error),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768726/