c - openLDAP c 客户端协议(protocol)错误 (ldap_simple_bind_s : Protocol error)

标签 c openldap ldapconnection ldap-client

我正在尝试使用 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/

相关文章:

ldap - LDAP 中的 memberOf 与 groupMembership (Liferay)

C: switch 语句错误: "this is the first entry overlapping that value"

c - 将 uint8_t 传递给atomic_clr

c - strtok 用于 GPS 解析,无法识别字符串

windows-7 - Windows 7 OpenLDAP Curl DLL 依赖 hell

authentication - 外部用户使用 LDAP 的优点和缺点

c - 如何断言两种类型在c中是相等的?

ubuntu - slapd -- 推送来自消费者的更改

ldapconnection - phabricator LDAP 连接失败