1

現在、ldap_bind_sを使用して、C アプリケーションで SEC_WINNT_AUTH_IDENTITY 構造体を使用してサーバーにバインドしていますが、関数は非推奨としてマークされています。このため、 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 バインディングを使用した実際のサンプル コードが見つかりません。

このコードを機能させる方法について、いくつかのガイドに感謝します。

4

2 に答える 2

1

最後に、過去 2 週間の調査とデバッグの結果、WinLDAP のldap_sasl_bind_s関数でDIGEST-MD5認証を使用する実用的なサンプル コードを作成することができました。対応するRFCこの回答、および公式のSSPI ドキュメントは、私に多くの助けを与えてくれました。

私が遭遇したいくつかの落とし穴:

  • 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;
}
于 2015-06-05T23:32:41.173 に答える
1

の最後のパラメーターをldap_sasl_bind_sANULL にすることはできません。関数がサーバーの応答を配置できる場所を指す必要があります ( struct berval*)。

...
struct berval* serverResponse = NULL;
rc = ldap_sasl_bind_sA(ld, binddn, "DIGEST-MD5", &saslcred, NULL, NULL, &serverResponse);
...
于 2014-03-12T17:12:09.577 に答える