如何在WinLDAP中使用ldap_sasl_bind?

时间:2014-02-01 17:12:27

标签: c windows winapi ldap sasl

我目前使用ldap_bind_s使用SEC_WINNT_AUTH_IDENTITY结构绑定到我的C应用程序中的服务器,但该函数被标记为已弃用。出于这个原因,我想将其更改为ldap_sasl_bind_s函数。

int main(void) { 
    LDAP *ld;
    int rc = 0;
    char *binddn = "cn=admin,dc=local";
    const int version = LDAP_VERSION3;
    SEC_WINNT_AUTH_IDENTITY wincreds;
    struct berval saslcred;

    wincreds.User = "admin";
    wincreds.UserLength = 5;
    wincreds.Password = "secret";
    wincreds.PasswordLength = 6;
    wincreds.Domain = NULL;
    wincreds.DomainLength = 0;
    wincreds.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;

    ld = ldap_initA("localhost", LDAP_PORT);
    ldap_set_optionA(ld, LDAP_OPT_PROTOCOL_VERSION, &version);

    rc = ldap_bind_sA(ld, binddn, (PCHAR)&wincreds, LDAP_AUTH_DIGEST);
    printf("0x%x\n", rc); // It's OK (0x0) 
    ldap_unbind(ld);

    saslcred.bv_val = "secret";
    saslcred.bv_len = 6;

    rc = ldap_sasl_bind_sA(ld, binddn, "DIGEST-MD5", &saslcred, NULL, NULL, NULL);
    printf("0x%x\n", rc); // Returns with 0x59
    ldap_unbind(ld)

    return 0;
}

ldap_sasl_bind_s返回LDAP_PARAM_ERROR代码。显然,上面的函数参数是错误的,但是我找不到带有winldap和SASL绑定的工作示例代码。

我将感谢一些指南,如何使这段代码正常工作。

2 个答案:

答案 0 :(得分:1)

ldap_sasl_bind_sA的最后一个参数不能为NULL。它必须指向函数可以放置服务器响应的位置(struct berval*)。

...
struct berval* serverResponse = NULL;
rc = ldap_sasl_bind_sA(ld, binddn, "DIGEST-MD5", &saslcred, NULL, NULL, &serverResponse);
...

答案 1 :(得分:1)

最后,在过去两周的一些研究和调试之后,我成功编写了一个使用 DIGEST-MD5 身份验证的工作示例代码与WinLDAP的 ldap_sasl_bind_s 功能。相应的RFCthis answer和官方SSPI documentation给了我很多帮助。

我遇到的一些问题:

  • 无论文档中有关于ldap_connect函数的内容如何:如果您想使用ldap_sasl_bind_s函数,那么首先调用它不仅仅是“良好的编程习惯”有必要。没有它, ldap_sasl_bind_s 将返回 LDAP_SERVER_DOWN (0x51)错误代码。
  • 有效的 pszTargetName (digest-uri)参数对InitializeSecurityContext函数至关重要,可避免无效的令牌错误。

我希望它能帮助其他人花更少的时间来弄清楚如何在WinLDAP中使用SASL绑定机制。

#include <stdio.h>
#include <windows.h>
#include <winldap.h>

#define SECURITY_WIN32 1

#include <security.h>
#include <sspi.h>

int _tmain(int argc, _TCHAR* argv[]) {
    LDAP *ld;
    int rc = 0;
    const int version = LDAP_VERSION3;
    SEC_WINNT_AUTH_IDENTITY wincreds;
    struct berval *servresp = NULL;
    SECURITY_STATUS res;
    CredHandle credhandle;
    CtxtHandle newhandle;
    SecBufferDesc OutBuffDesc;
    SecBuffer OutSecBuff;
    SecBufferDesc InBuffDesc;
    SecBuffer InSecBuff;
    unsigned long contextattr;

    ZeroMemory(&wincreds, sizeof(wincreds));

    // Set credential information
    wincreds.User = (unsigned short *)L"root";
    wincreds.UserLength = 4;
    wincreds.Password = (unsigned short *)L"p@ssword";
    wincreds.PasswordLength = 8;
    wincreds.Domain = NULL;
    wincreds.DomainLength = 0;
    wincreds.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;

    res = AcquireCredentialsHandle(NULL, L"WDigest", SECPKG_CRED_OUTBOUND,
        NULL, &wincreds, NULL, NULL, &credhandle, NULL);

    // Buffer for the output token.
    OutBuffDesc.ulVersion = 0;
    OutBuffDesc.cBuffers = 1;
    OutBuffDesc.pBuffers = &OutSecBuff;

    OutSecBuff.BufferType = SECBUFFER_TOKEN;
    OutSecBuff.pvBuffer = NULL;

    ld = ldap_init(L"localhost", LDAP_PORT);
    rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void*)&version);
    rc = ldap_connect(ld, NULL); // Need to connect before SASL bind!

    do {
        if (servresp != NULL) {
            InBuffDesc.ulVersion = 0;
            InBuffDesc.cBuffers = 1;
            InBuffDesc.pBuffers = &InSecBuff;

            /* The digest-challenge will be passed as an input buffer to
            InitializeSecurityContext function */
            InSecBuff.cbBuffer = servresp->bv_len;
            InSecBuff.BufferType = SECBUFFER_TOKEN;
            InSecBuff.pvBuffer = servresp->bv_val;

            /* The OutBuffDesc will contain the digest-response. */
            res = InitializeSecurityContext(&credhandle, &newhandle, L"ldap/localhost", ISC_REQ_MUTUAL_AUTH | ISC_REQ_ALLOCATE_MEMORY,
                0, 0, &InBuffDesc, 0, &newhandle, &OutBuffDesc, &contextattr, NULL);
        }
        else {
            res = InitializeSecurityContext(&credhandle, NULL, L"ldap/localhost", ISC_REQ_MUTUAL_AUTH, 0, 0, NULL, 0, &newhandle, &OutBuffDesc, &contextattr, NULL);
        }

        switch (res) {
            case SEC_I_COMPLETE_NEEDED:
            case SEC_I_COMPLETE_AND_CONTINUE:
            case SEC_E_OK:
            case SEC_I_CONTINUE_NEEDED:
                break;
            case SEC_E_INVALID_HANDLE:
                return -2;
            case SEC_E_INVALID_TOKEN:
                return -1;
            default:
                break;
        }

        struct berval cred;
        cred.bv_len = OutSecBuff.cbBuffer;
        /* The digest-response will be passed to the server
        as credential after the second (loop)run. */
        cred.bv_val = (char *)OutSecBuff.pvBuffer;

        // The servresp will contain the digest-challange after the first call.
        rc = ldap_sasl_bind_s(ld, L"", L"DIGEST-MD5", &cred, NULL, NULL, &servresp);
        ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &res)
    } while (res == LDAP_SASL_BIND_IN_PROGRESS);

    if (rc != LDAP_SUCCESS) {
        printf("Bind failed with 0x%x\n", rc);
    } else {
        printf("Bind succeeded\n");
    }

    return 0;
}