[Date Prev][Date Next] [Chronological] [Thread] [Top]

little problem searching in ldap with own client



Hello,

	I'm developing a small client to search in OpenLDAP server. All
seems to work, but my program only retrieves the first and the second entry
that match the filter, and the the second entry is repeated the number of
total entries that match the filter. I mean, if I find 5 entries in the
directory, my program prints the first, the second, and then 3 more times
the second entry...

	This is the source code:

>--
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ldap.h>
#include <lber.h>
#include <sys/time.h>

int main (void) {
    	char *host="localhost", *port="389", *basedn="dc=domain,dc=com";
	LDAP *ldapconn = NULL;
	LDAPMessage *res;
	LDAPMessage *primero;
	BerElement *ber;
	char *binddn = "cn=admin,dc=domain,dc=com", *bindpw = "xxxxxxx";
	char *atributos[]={"cn","mail","telephonenumber",NULL};
	int msg, rows,a;
	char *nombre, **vals;
	
	ldapconn = ldap_init(host, atoi(port));
	ldap_simple_bind(ldapconn,binddn,bindpw);
	msg=ldap_search_s(ldapconn,basedn,LDAP_SCOPE_SUBTREE,"(&(cn=*)(mail=*)(telephonenumber=*))",atributos,0,&res);
	rows=ldap_count_messages(ldapconn,res);
	primero = ldap_first_entry(ldapconn,res);
	for(a=0;a<rows;a++) {
		nombre = ldap_first_attribute(ldapconn, primero, &ber);
		vals = ldap_get_values(ldapconn, primero, nombre);
		printf("[%d] %s : %s\n",a,(char *)strdup(nombre),(char *)strdup(vals[0]));
		ldap_value_free(vals);
		ldap_memfree(nombre);
		
		nombre = ldap_next_attribute(ldapconn, primero, ber);
		vals = ldap_get_values(ldapconn, primero, nombre);
		printf("[%d] %s : %s\n",a,(char *)strdup(nombre),(char *)strdup(vals[0]));
		ldap_value_free(vals);
		ldap_memfree(nombre);

		nombre = ldap_next_attribute(ldapconn, primero, ber);
		vals = ldap_get_values(ldapconn, primero, nombre);
		printf("[%d] %s : %s\n",a,(char *)strdup(nombre),(char *)strdup(vals[0]));
		ldap_value_free(vals);
		ldap_memfree(nombre);
		primero = ldap_next_entry(ldapconn,res);
	}
	if (ber != NULL) {
	    ber_free(ber, 0);
	}
	ldap_msgfree(res);
	ldap_unbind(ldapconn);
	return(0);
}
>--

	Thanks in advance :)

...Braincracking. Neophyte. 1998
--- Debian + Mutt + Postfix
 * Origin: Web Page: http://pbrufal.kleenux.org (Fido 2:346/7.68)