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

ldap_get_values returning NULL

I'm running OpenLDAP 2.0.10 and trying to get the value from an attribute
using the ldap_get_values C function. I have condenced the relevant part
of my code reproducing the problem at the bottom of this message. It has
been compiled with the following option:
gcc ldap_err.c -g -I/usr/local/openldap/include -L/usr/local/openldap/lib
-llber -lldap -lresolv

This is the output from my program:

ldap_get_values returned NULL
filter: (&(uid=intmktg@cam.org)(objectClass=spUser))
error: Success

This means it found the entry (see code checking if count entries == 1),
and yet the error message is "Success"? This is the output from slapd
when running on the command-line with -d 256:

daemon: conn=856 fd=24 connection from IP=
(IP= accepted.
conn=856 op=0 BIND dn="" method=128
ber_flush: 14 bytes to sd 24
conn=856 op=0 RESULT tag=97 err=0 text=
conn=856 op=1 SRCH base="o=internet" scope=2
ber_flush: 96 bytes to sd 24
ber_flush: 14 bytes to sd 24
conn=856 op=1 SEARCH RESULT tag=101 err=0 text=
conn=856 op=2 UNBIND
conn=-1 fd=24 closed

The two occurences of ber_flush indicate the entry is found in the LDAP
database, but ldap_get_values still returns NULL. Also note that the entry
I'm looking for does actually contain the attribute which should be
returned. So here's the code, the problem is probably very simple:

/* ldap_err.c -- LDAP problem using ldap_get_values */

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

#include <resolv.h>
#include <lber.h>
#include <ldap.h>

const char *server_host      = "localhost";
const int   server_port      = 389;
const char *search_base      = "o=internet";
const char *query_filter     = "(&(uid=%s)(objectClass=spUser))";
const char *result_attribute = "mailFolderMap";
const char *identifier       = "intmktg@cam.org";

fatal (const char *msg)
	fprintf (stderr, "ERROR: %s\n", msg);
	exit (1);

int main()
	LDAP *ld;
	LDAPMessage *result,
	BerElement *ber;

	static char retbuf[81];
	char *ret, *p, *filter, *attrs[2];
	char **ary;

	/* Open the LDAP connection. */
	if ((ld = ldap_open(server_host,
			server_port)) == NULL) {
		printf("LDAP open failed");
		goto end_4;

	if (ldap_simple_bind_s(ld,"","") != LDAP_SUCCESS) {
		printf("LDAP bind failed");
		goto end_4;

	/* set query filter */
	ret = NULL;
	filter = malloc(strlen(query_filter) + strlen(identifier) + 1);
	if (!filter) fatal("malloc failed");
	sprintf(filter, query_filter, identifier);

	/* Retrieve result attribute */
	attrs[0] = (char *)result_attribute;
	attrs[1] = NULL;

	/* Perform the search... */
	if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, \
				filter, attrs, 1, &result) != LDAP_SUCCESS) {
		printf("LDAP search failed: %s", filter);
		goto end_2;

	/* Expected to have only one entry */
	if (ldap_count_entries(ld, result) != 1) {
		printf("number of entries != 1\n");
		goto end_1;

	/* Get the first entry */
	entry = ldap_first_entry(ld, result);
	if (entry == NULL) {
		printf("could not fetch first entry\n");
		goto end_1;

	p = ldap_first_attribute(ld, entry, &ber);
	if (!p) {
		printf("could not fetch first attribute\n");
		goto end_1;

	ary = ldap_get_values(ld, entry, p);
	if (ary == NULL) {
		int err;
		err = ldap_result2error(ld, entry, 0);
		printf("ldap_get_values returned NULL\n");
		printf("filter: %s\n", filter);
		printf("error: %s\n", ldap_err2string(err));
		goto end_1;

	/* success, show everyone */
	printf("got: %s\n", ary[0]);

 end_0: ldap_value_free(ary);
 end_1: ldap_msgfree(result);
 end_2: free(filter);
 end_3: ldap_unbind(ld);
 end_4: return 0;