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

problem with ldap_get_values



In the following program, ldap_count_entries returns 1
for a query and ldap_first_attribute returns a proper
string with the attribute I specified in my *attrs[2]
variable. Problem is, when I then use ldap_get_values
with the same LDAP * and LDAPMessage *, along with
the found attribute, I get **val == NULL. Even more
odd is that the following query from the commandline
returns properly:

# ldapsearch -x -S "" -b "o=internet" "uid=foobar" "maildrop"
version: 2
#
# filter: uid=roland*
# requesting: maildrop
#
# roland,people,pagebleu,dc=sitepak,dc=net,internet
dn: uid=foobar,o=internet
maildrop: foo
# search result
search: 2
result: 0 Success
# numResponses: 2
# numEntries: 1

Please have a look at the code below and let me know
if there's anything wrong with it or if ldap_get_values
could be the problem.

get_maildrop(const char *identifier)
{
    static char retbuf[81];
    char filter[88],
         *attrs[2],
         **val,
         *str,
         *p;
 
    LDAP *ld;
    LDAPMessage *result,
                *entry;
    BerElement *berptr;
    int i;
 
    if (strlen(identifier) >= sizeof(retbuf))
      return NULL;
 
    ld = ldap_open("localhost", 389);
    if (ld == NULL)
      return NULL;
 
    if (ldap_simple_bind_s(ld, "", "") != LDAP_SUCCESS)
      return NULL;
 
    sprintf(filter, "(uid=%s)", identifier);
    attrs[0] = "maildrop";
    attrs[1] = NULL;
    if (ldap_search_s(ld, "o=internet", LDAP_SCOPE_SUBTREE, \
                      filter, attrs, 1, &result) != LDAP_SUCCESS) {
      ldap_unbind(ld);
      return NULL;
    }
 
    i = ldap_count_entries (ld, result);
    if (i != 1) {
      ldap_unbind(ld);
      return NULL;
    }
 
    printf("count: %d\n", i);
    entry = ldap_first_entry (ld, result);
    if (entry == NULL) {
      fprintf(stderr, "entry is null\n");
      ldap_msgfree(result);
      ldap_unbind(ld);
      return NULL;
    }
 
    str = ldap_first_attribute (ld, entry, &berptr);
    if (str == NULL) {
      fprintf(stderr, "str is null\n");
      ldap_msgfree(result);
      ldap_unbind(ld);
      return NULL;
    }
 
    printf("str: %s\n", str);
    val = ldap_get_values (ld, entry, *attrs);
    if (val == NULL) {
      fprintf(stderr, "val is null\n");
      ldap_msgfree(result);
      ldap_unbind(ld);
      return NULL;
    }
 
    strcpy (retbuf, *val);
 
    return retbuf;
}