Examining Microsoft's LDAP API
by Sven B. Schreiber 


Listing One
typedef struct _LDAP_CONN
    {
    PLDAP        pl;
    DWORD        dPort;
    DWORD        dVersion;
    DWORD        dPage;
    DWORD        dTimeout;
    LDAP_TIMEVAL ltTimeout;
    TBYTE atUser [N_DN];
    }
    LDAP_CONN, *PLDAP_CONN, **PPLDAP_CONN;
#define LDAP_CONN_ sizeof (LDAP_CONN)

typedef struct _LDAP_FIND
    {
    struct _LDAP_FIND *plf;
    struct _LDAP_FIND *plfNext;
    PLDAP_CONN         plc;
    PLDAPMessage       plm;
    PLDAPMessage       plmNext;
    DWORD              dEntries;
    }
    LDAP_FIND, *PLDAP_FIND, **PPLDAP_FIND;
#define LDAP_FIND_ sizeof (LDAP_FIND)

Listing Two 
DWORD EXPORT LdapConnect (PLDAP_CONN plc,
                          PTBYTE     ptServer,
                          PTBYTE     ptAccount,
                          PTBYTE     ptPassword,
                          DWORD      dPort,
                          DWORD      dVersion,
                          DWORD      dPage,
                          DWORD      dTimeout)
    {
    DWORD dStatus = LDAP_OTHER;
    plc->pl = NULL;
    plc->dPort    = (dPort    != LDAP_PRESET_PORT    ?  dPort
                                                     : gdPort);
    plc->dVersion = (dVersion != LDAP_PRESET_VERSION ?  dVersion
                                                     : gdVersion);
    plc->dPage    = (dPage    != LDAP_PRESET_PAGE    ?  dPage
                                                     : gdPage);
    plc->dTimeout = (dTimeout != LDAP_PRESET_TIMEOUT ?  dTimeout
                                                     : gdTimeout);
    plc->ltTimeout.tv_sec  = (plc->dTimeout / 1000);
    plc->ltTimeout.tv_usec = (plc->dTimeout % 1000) * 1000;

    LdapNameFromAccount (ptAccount, plc->atUser, sizeof (plc->atUser));
    if ((plc->pl = ldap_init ((*ptServer ? ptServer : NULL), plc->dPort))
        != NULL)
        {
        LdapVersionSet (plc, plc->dVersion);
        if (((dStatus = ldap_connect (plc->pl, &plc->ltTimeout))
             != LDAP_SUCCESS)
            ||
            ((dStatus = ldap_bind_s (plc->pl, plc->atUser, ptPassword,
                   (ptPassword != NULL ? LDAP_AUTH_SIMPLE : LDAP_AUTH_NTLM)))
             != LDAP_SUCCESS))
            {
            LdapDisconnect (plc);
            }
        }
    else
        {
        dStatus = LdapError ();
        }
    return dStatus;
    }





2


