Search code examples
c++ldaplibcurl

Libcurl C++ Ldap Request


i am new to cUrl. What i need todo is a curl request to an ldap server.

So far the request works fine over the command line:
$curl ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password

After trying to get this done in my C++ CMake Project. I issued this error Code from libcurl:
libcurl: (39) LDAP local: ldap_search_ext Bad search filter

My code as snippet:

std::string readBuffer;
CURL* curl = curl_easy_init();
if (curl) {
        CURLcode res;
        char errbuf[CURL_ERROR_SIZE];
        curl_easy_setopt(curl, CURLOPT_URL, "ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password");

        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
        errbuf[0] = 0;

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);        // this line prints into console, what curl is trying to do
        curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L);   // closes the connection, if not here the connection can be used more than once

        // curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        
        res = curl_easy_perform(curl);

        std::cout << res << std::endl;

        curl_easy_cleanup(curl);

        std::cout << readBuffer << std::endl;

        if (res != CURLE_OK) {
            size_t len = strlen(errbuf);
            fprintf(stderr, "\nlibcurl: (%d) ", res);
            if (len)
                fprintf(stderr, "%s%s", errbuf,
                    ((errbuf[len - 1] != '\n') ? "\n" : ""));
            else
                fprintf(stderr, "%s\n", curl_easy_strerror(res));
        }
}

My Output:

Test curl library
*   Trying XXXXXXXXXXXXXX
* TCP_NODELAY set
* Connected to XXXXXXXXXXXXXX
* LDAP local: ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password
* LDAP local: ldap_search_ext Bad search filter
* Closing connection 0
39


libcurl: (39) LDAP local: ldap_search_ext Bad search filter

I appricate every help! Thank you for taking the time reading this.


Solution

  • You cannot just bung everything into the CURLOPT_OPT_URL field. The --user bit should be separated into a separate parameter:

    curl_easy_setopt(curl, CURLOPT_URL, "ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX)");
    curl_easy_setopt(curl, CURLOPT_USERPWD, "s_ad_XXXXXXXXX:Password");