[Date Prev][Date Next]
[Chronological]
[Thread]
[Top]
parsing output from ldap_search_ext_s C API
- To: openldap-technical@openldap.org
- Subject: parsing output from ldap_search_ext_s C API
- From: sim123 <Sim3159@gmail.com>
- Date: Thu, 21 Apr 2011 10:36:36 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:date:x-google-sender-auth :message-id:subject:from:to:content-type; bh=HkRZRbvxwqxWqNtvqc5JIJUSnJs/5yPHvbgoSwqMUXc=; b=q0B1aHTg7TQjkk6EeEXGZN/oBtV6LaeIsb5Vf75qQ0Bo4I+vXiIaUiIsxGdQEeTTh3 K7axbEHvhJ7RxOhd5EW2KBy9NdaNBA/H/Xg98dIJQRiSjp+7AuHTlJL8AWud/KxKoYha BkkdFncRmoNr6+9QkCdZ57XEkq5e0g6QaRVaE=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:date:x-google-sender-auth:message-id:subject :from:to:content-type; b=XSzGVzD8ZbdV8EED4MhJb6IrEYKdLPUqigtMubkIzjkckIuAdcQYInUD3mBqqqmlj1 7aoHJJ79RcGO7Ov2acsKEFr2Aso6Rz5bfn3eIXEJ/QYyeQHKJKRLpLFkRPhb6e4BDQLj kDwnasLV+YleuaDlAh2Oarx3LFiV0Ka7JF1RQ=
Hi All,
I am using ldap_search_ext_s C API and having strange results, not sure if I am missing something or its an API bug:
My Directory Tree looks like this:
| -- dc=example,dc=com
| ------ ou=users,dc=example,dc=com
| ---------- uid=1,ou=users,dc=example,dc=com
| --------------- cn=john
| ------ ou=departments,dc=example,dc=com
| ---------- uid=11,ou=departments,dc=example,dc=com
| --------------- cn=hr
| --------------- member=uid=1
| ---------- uid=12,ou=departments,dc=example,dc=com
| --------------- cn=sales
| --------------- member=uid=1
| ---------- uid=13,ou=departments,dc=example,dc=com
| --------------- cn=marketing
| --------------- member=uid=1
First I get all the departments for "john" by using memberof in sarch attribute. for this query my search filter contains only one criteria ("uid=1")
Then I construct another search filter for getting names of all the departments john belongs to : (|(uid=11)(uid=12)(uid=13))
I pass this filter to ldap_search_ext_s, where base is "ou=departments,dc=example,dc=com", scope is one level down, and I want "cn" in the attribute
LDAPMessage* output;
int retCode = ldap_search_ext_s(ld,base.c_str(),
scope,filter.c_str(),attrs,false,NULL,NULL,NULL,0,&output);
if(retCode == LDAP_SUCCESS){
//log success
//send result to a static method for parsing
LDAPUtil::parseResult(ld,output,result);
ldap_msgfree(output);
}else{
//log error & throw exception
}
LDAPUtil parse result implementation
void LDAPUtil::parseResult(LDAP* ld, LDAPMessage* ldapResponse, LDAPSearchResult* parsedResult){
int numEntries = ldap_count_entries(ld,ldapResponse);
cout << "number of entries" << numEntries << endl; // I get 3 here
if(numEntries > 0){
//parse result
LDAPMessage * entry;
BerElement * ber;
char * attr;
BerVarray* vals ;
map<string,LDAPAttribute*> attributeValueMap ;
int count = 0;
//If I don't use count < numEntries this loop becomes an infinite loop, this loop runs 3 times, however the dn value output is:
// run 1 :: dn: uid=11,ou=departments,dc=examples,dc=com
//run 2 :: dn: uid=12,ou=departments,dc=examples,dc=com
// run 3 :: dn: uid=12,ou=departments,dc=examples,dc=com
for ( entry = ldap_first_entry(ld,ldapResponse);entry != NULL && count < numEntries;
entry = ldap_next_entry(ld,ldapResponse)){
count++;
//create LDAP Attributes
LDAPAttribute* attribute = new LDAPAttribute();
//set DN
attribute->setDn(ldap_get_dn(ld,entry));
cout << "dn is " << ldap_get_dn(ld,entry) <<endl;
for(attr = ldap_first_attribute(ld,entry,&ber);
attr != NULL; attr=ldap_next_attribute(ld,entry,ber))
{
string temp = attr;
cout << "attribute :: " << attr << endl;
vals = ldap_get_values_len(ld,entry,attr);
if((ldap_count_values_len(vals))> 0 ){
LDAPUtil::processAttribute(attribute,temp,vals);
}
ldap_value_free_len(vals);
}
attributeValueMap.insert(pair<string,LDAPAttribute*>(attribute->getDn(),attribute));
}
parsedResult->setAttributeValueMap(attributeValueMap);
}
}
Basically above code is working only if I have one entry returned in the output. I would really appreciate if someone can help me with this. As I have hard time beliving its an API bug since I am just doing basic operation.
Thanks,
- Simon