简体   繁体   中英

Authehticate to LDAP server from C/C++

I try ./ldapsearch -h 192.168.1.1 -p 389 -D uid=myname,ou=People,dc=mycompany,dc=ru -w mypassword -b dc=mycompany,dc=ru -xL myname

And I write this c++ code:

#define LDAP_DEPRECATED 1
#include "ldap.h"


SERVER_API void TestSherst()
{
    LDAP *ld;

    if ((ld = ldap_init("192.168.1.1", LDAP_PORT)) == NULL)
    {
        perror("ldap_init failed");
        exit(EXIT_FAILURE);
    }

    int desired_v = LDAP_VERSION3;
    if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &desired_v) != LDAP_OPT_SUCCESS)
    {
        ldap_perror(ld, "ldap_set_option failed!");
        exit(EXIT_FAILURE);
    }

    if (ldap_bind_s(ld, "uid=myname,ou=People,dc=mycompany,dc=ru", "mypassword", LDAP_AUTH_SIMPLE) != LDAP_SUCCESS)
    {
        ldap_perror(ld, "ldap_bind");
        exit(EXIT_FAILURE);
    }

    std::cout << "Connected" << std::endl;
}

I need a good advice, or, even better, a good guide.

How can I make a search request from code after that? Can somebody give me ref to good documentation?

I can give you an example,

suppose you want to search for a person whose uid is bob then you should create a search filter

char *search_filter = "uid=bob";

and, assumes that your search base is dc=mycompany,dc=ru .

LDAPMessage *res;

ldap_search_ext_s(ld, "dc=mycompany,dc=ru", LDAP_SCOPE_CHILDREN, search_filter, NULL, 0,NULL, NULL, NULL, 0, &res);

Above API call will retrieve the search result in res . later you can use the res to obtain attributes and values of search result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM