c++ - 如何使用 LDAP 对用户进行密码验证?

标签 c++ linux ldap openldap

我正在编写一个客户端应用程序(使用 OpenLDAP 库),用户通过 LDAP 服务器对其进行身份验证。

这是无法比较用户的 userPassword 的硬编码示例程序。

#include <stdio.h>
#include <ldap.h>
#define LDAP_SERVER "ldap://192.168.1.95:389"

int main( int argc, char **argv ){
    LDAP        *ld;
    int         rc;
    char        bind_dn[100];
    LDAPMessage *result, *e;
    char *dn;
    int has_value;

    sprintf( bind_dn, "cn=%s,dc=ashwin,dc=com", "manager" );
    printf( "Connecting as %s...\n", bind_dn );

    if( ldap_initialize( &ld, LDAP_SERVER ) )
    {
        perror( "ldap_initialize" );
        return( 1 );
    }

    rc = ldap_simple_bind_s( ld, bind_dn, "ashwin" );
    if( rc != LDAP_SUCCESS )
    {
        fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc) );
        return( 1 );
    }

    printf( "Successful authentication\n" );

    rc = ldap_search_ext_s(ld, "dc=ashwin,dc=com", LDAP_SCOPE_SUBTREE, "sn=ashwin kumar", NULL, 0, NULL, NULL, NULL, 0, &result);
    if ( rc != LDAP_SUCCESS ) {
        fprintf(stderr, "ldap_search_ext_s: %s\n", ldap_err2string(rc));
    }

    for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
        if ( (dn = ldap_get_dn( ld, e )) != NULL ) {
            printf( "dn: %s\n", dn );
            has_value = ldap_compare_s( ld, dn, "userPassword", "secret" ); 
            switch ( has_value ) { 
                case LDAP_COMPARE_TRUE: 
                    printf( "Works.\n"); 
                    break; 
                case LDAP_COMPARE_FALSE: 
                    printf( "Failed.\n"); 
                    break; 
                default: 
                    ldap_perror( ld, "ldap_compare_s" ); 
                    return( 1 ); 
            } 
            ldap_memfree( dn );
        }
    }

    ldap_msgfree( result );
    ldap_unbind( ld );
    return( 0 );
}

userPassword 如果它在 LDAP 服务器中是普通的,它就可以工作。 相同的密码,如果是 MD5 加密,ldap_compare_s失败。那是因为我正在传递明文密码进行比较。

如何让这个示例程序运行?

我这样做对吗?使用 ldap_compare_s 通过 LDAP 验证用户是否正确?

P.S:这是我第一次使用 LDAP。

最佳答案

这并不是在 LDAP 上执行密码检查的正确方法,您应该尝试使用从第一次搜索中获得的 dn 和提供的密码进行绑定(bind)。

即您执行第二次绑定(bind)以验证密码。如果绑定(bind)失败则密码不正确。

类似于:

    if ( (dn = ldap_get_dn( ld, e )) != NULL ) {
        printf( "dn: %s\n", dn );
        /* rebind */
        ldap_initialize(&ld2, LDAP_SERVER);
        rc = ldap_simple_bind_s(ld2, dn, "secret");
        printf("%d\n", rc);
        if (rc != 0) {
            printf("Failed.\n");
        } else {
            printf("Works.\n");
            ldap_unbind(ld2);
        }
        ldap_memfree( dn );
    }

出于安全原因,表明用户名不正确(即搜索用户帐户失败)通常被认为是过度披露,应该避免。

关于c++ - 如何使用 LDAP 对用户进行密码验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168293/

相关文章:

c++ - 使用 aws-cpp-sdk 将 avi 文件上传到 AWS S3

c - 推送函数C语言中堆栈中的段错误

linux - vala是跨平台语言吗?

Windows LDAP 客户端 - 通过 CRL 启用撤销

c++ - 为什么我的 DwmExtendFrameIntoClientArea() 窗口没有绘制 DWM 边框?

c++ - 不在 C++ 中打印

Python3-LDAP 未返回预期结果

ruby-on-rails - Ruby on Rails 3: Devise::LdapAdapter.get_ldap_param 未定义方法错误

c++ - 未处理的异常 Microsoft C++ 异常:cv::Exception at memory location

linux - 添加了对mongodb的异步调用后,带有mongodb项目的actix Web无法构建