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

Sorting stuff from OpenLdap (i hope my first post isnt OT)

Hi world,

i am writing an application which deals with LDAP-data. My problem is to get
a sorted list of entries. I need the dn and the cn values and get them
sorted by the cn. If i try like discribed above i get a segfault in
ldap_sort_strcasecmp(). Did i made a mistake in my code, or is the truely a
bug ?

I am using the openldap version 2.0.11 from the SuSE Linux eMail Server.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ldap.h>

int main(void) {
        LDAP        *ld;
        LDAPMessage *searchResult, *entry;
        int rc=0,anzEntries,i=0;
        char **result=NULL;
        char *attribute=NULL;
        BerElement *ber;
        char *attrs[]={"dn","cn",NULL};
//      char *attrs[]={NULL};

        ld = ldap_init( "mailintern.sawag.com", 389 );
        rc = ldap_simple_bind_s( ld, "uid=kus,dc=dev,dc=sawag,dc=com",
"system" );
        rc = ldap_search_s( ld, "dc=dev,dc=sawag,dc=com",
LDAP_SCOPE_SUBTREE, "(objectclass=posixAccount)", attrs, 0, &searchResult );

        rc=ldap_sort_entries( ld, &searchResult, "cn", (int
(*)())ldap_sort_strcasecmp );

        anzEntries = ldap_count_entries(ld,searchResult);
        result = (char **)malloc((anzEntries+1)*sizeof(char *));
        memset(result,0,(anzEntries+1)*sizeof(char *));

        for (   entry   =   ldap_first_entry( ld, searchResult),i=0;
                entry   !=  NULL;
                entry   =   ldap_next_entry( ld, entry ),i++ )
                attribute = ldap_first_attribute( ld, entry, &ber );
                result=ldap_get_values( ld, entry, attribute );
printf("Result: %s: %s\n",ldap_get_dn( ld, entry), result[0]);

        ldap_msgfree( searchResult );
        ldap_unbind_s( ld );

        return 0;