Enumerating LDAP query results

Enumerating LDAP query results

Post by Sten Weste » Sat, 12 Feb 2005 22:34:13


Hi

I'm have successfully implemented a set of Active Directory interfacing
routines
using ADSI before but now i need to make similar easy to use routines for
pure LDAP.

The connection is fine (in anonymous mode but i do get "Inappropriate
authentication"
even if account and password is correct; any ideas why?) but i can't seem to
find out how to convert the returned attribute values to proper string
representations.
This is the essential parts of my latest attempt:

LDAP *p_ld;
LDAPMessage *p_LDM, *p_m;
char *p_szAttrib, *p_szValue, szLabel[500], szV[1000], *p_sz;
BerElement *p_berList=NULL, *p_ber;
BERVAL *p_berval;
ULONG ulTag, ulLen;
CHAR *p_BerCookie;

// ldap_search_st() used to produce p_ld and p_LDM

if ((p_m=ldap_first_entry(p_ld, p_LDM))!=NULL)
{
do
{
if ((p_szAttrib=ldap_first_attribute(p_ld, p_m, &p_berList))==NULL)
break;
do
{
ulTag=ber_peek_tag(p_berList, &ulLen); // this gives wrong Tag value
p_berval=ldap_get_values_len(p_ld, p_m, p_szAttrib); p_szValue=NULL;
ulTag=ber_first_element(p_berList, &ulLen, &p_BerCookie);
if (ulTag==LBER_DEFAULT) continue;
do
{
switch(ulTag)
{
case 0x1U: // boolean: 0=FALSE; 0xFF=TRUE
if (*p_sz==0) p_szValue="No"; else p_szValue="Yes";
break;
case 0x2U: // integer
sprintf(szV, "Intvalue(%lu)", ulLen); p_szValue=szV;
break;
case 0x4U: // octet string
memcpy(&p_szValue, p_sz, sizeof(void *));
break;
}
// will use p_szValue here as value for p_szAttrib with indexes
depending on Entry and Element
} while ((ulTag=ber_next_element(p_berList, &ulLen,
p_BerCookie))!=LBER_DEFAULT);
} while ((p_szAttrib=ldap_next_attribute(p_ld, p_m, p_berList))!=NULL);
} while ((p_m=ldap_next_entry(p_ld, p_m))!=NULL);

The problem is that i can't be able to figure out the Type of the attribute
found by ldap_first_attribute() as there obviously isn't anything connecting
the it with ber_peek_tag() or at least i'm missing something.

I have also tried using ldap_get_values() but there doesn't seem to be
a way to find out what type of value that attribute has.
I know the query works as i the de *** shows proper values somewhere in
the buffers.

Help! :)

- Sten


- Sten
 
 
 

Enumerating LDAP query results

Post by Sten Weste » Sun, 13 Feb 2005 00:08:06

To make code reasonable (and so noone can complain about it) i reentered a
line a had temporarily commented out for debugging purposes... :)



to
value

p_sz=ldap_get_values(p_ld, p_m, p_szAttrib); // uncommented to
make switch cases reasonable

attribute
connecting