Index: servers/slapd/result.c
===================================================================
RCS file: /repo/OpenLDAP/pkg/ldap/servers/slapd/result.c,v
retrieving revision 1.316
retrieving revision 1.317
diff -u -r1.316 -r1.317
--- servers/slapd/result.c	13 Oct 2008 08:18:15 -0000	1.316
+++ servers/slapd/result.c	16 Oct 2008 22:03:17 -0000	1.317
@@ -1273,6 +1273,7 @@
 	BerElement	*ber = (BerElement *) &berbuf;
 	int rc = 0;
 	int bytes;
+	char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
 
 	AttributeDescription *ad_ref = slap_schema.si_ad_ref;
 	AttributeDescription *ad_entry = slap_schema.si_ad_entry;
@@ -1287,7 +1288,7 @@
 
 	Debug( LDAP_DEBUG_TRACE,
 		"=> send_search_reference: dn=\"%s\"\n",
-		rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
+		edn, 0, 0 );
 
 	if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
 		ad_entry, NULL, ACL_READ, NULL ) )
@@ -1313,7 +1314,7 @@
 	if( op->o_domain_scope ) {
 		Debug( LDAP_DEBUG_ANY,
 			"send_search_reference: domainScope control in (%s)\n", 
-			rs->sr_entry->e_dn, 0, 0 );
+			edn, 0, 0 );
 		rc = 0;
 		goto rel;
 	}
@@ -1321,7 +1322,7 @@
 	if( rs->sr_ref == NULL ) {
 		Debug( LDAP_DEBUG_ANY,
 			"send_search_reference: null ref in (%s)\n", 
-			rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
+			edn, 0, 0 );
 		rc = 1;
 		goto rel;
 	}
@@ -1371,6 +1372,7 @@
 
 	rc = 0;
 	if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
+		assert( rs->sr_entry != NULL );
 		be_entry_release_rw( op, rs->sr_entry, 0 );
 		rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
 		rs->sr_entry = NULL;
@@ -1520,8 +1522,8 @@
 	LDAPControl c;
 	Operation myop;
 
-	Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
-		oid->bv_val, e->e_dn, 0 );
+	Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
+		op->o_log_prefix, oid->bv_val, e->e_dn );
 
 	rs->sr_entry = e;
 	rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
Index: servers/slapd/back-ldap/chain.c
===================================================================
RCS file: /repo/OpenLDAP/pkg/ldap/servers/slapd/back-ldap/chain.c,v
retrieving revision 1.70
retrieving revision 1.71
diff -u -r1.70 -r1.71
--- servers/slapd/back-ldap/chain.c	12 Oct 2008 18:20:46 -0000	1.70
+++ servers/slapd/back-ldap/chain.c	16 Oct 2008 22:06:12 -0000	1.71
@@ -597,6 +597,8 @@
 	struct berval	odn = op->o_req_dn,
 			ondn = op->o_req_ndn;
 	slap_response	*save_response = op->o_callback->sc_response;
+	Entry		*save_entry = rs->sr_entry;
+	slap_mask_t	save_flags = rs->sr_flags;
 
 	int		rc = LDAP_OTHER,
 			first_rc = -1;
@@ -761,7 +763,8 @@
 	op->o_req_ndn = ondn;
 	op->o_callback->sc_response = save_response;
 	rs->sr_type = REP_SEARCHREF;
-	rs->sr_entry = NULL;
+	rs->sr_entry = save_entry;
+	rs->sr_flags = save_flags;
 
 	if ( rc != LDAP_SUCCESS ) {
 		/* couldn't chase any of the referrals */
Index: tests/scripts/test032-chain
===================================================================
RCS file: /repo/OpenLDAP/pkg/ldap/tests/scripts/test032-chain,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- tests/scripts/test032-chain	7 Jan 2008 23:20:17 -0000	1.13
+++ tests/scripts/test032-chain	16 Oct 2008 22:07:15 -0000	1.14
@@ -128,7 +128,7 @@
 		exit 1
 	fi
 
-	echo "Reading the referral entry "ou=Other,$BASEDN" as anonymous on port $P..."
+	echo "Reading the referral entry \"ou=Other,$BASEDN\" as anonymous on port $P..."
 	$LDAPSEARCH -h $LOCALHOST -p $P -b "ou=Other,$BASEDN" -S "" \
 		 > $SEARCHOUT 2>&1
 
@@ -304,6 +304,31 @@
 	exit $RC
 fi
 
+# ITS#57??
+$LDAPADD -h $LOCALHOST -p $PORT1 \
+	-D "$MANAGERDN" -w secret \
+	>> $TESTOUT 2>&1 \
+	<< EOMODS
+dn: ou=Can't Contact,dc=example,dc=com
+changetype: add
+objectclass: referral
+objectclass: extensibleobject
+ou: Can't Contact
+# invalid URI to test broken connectivity handling (search only)
+ref: ${URI3}ou=Can't%20Contact,dc=example,dc=com
+EOMODS
+
+echo "Reading the referral entry \"ou=Can't Contact,$BASEDN\" as anonymous on port $PORT1..."
+$LDAPSEARCH -h $LOCALHOST -p $PORT1 -b "$BASEDN" -S "" "(cn=Can't Contact)" \
+	 > $SEARCHOUT 2>&1
+
+RC=$?
+if test $RC != 0 ; then
+	echo "ldapsearch failed ($RC)!"
+	test $KILLSERVERS != no && kill -HUP $KILLPIDS
+	exit $RC
+fi
+
 test $KILLSERVERS != no && kill -HUP $KILLPIDS
 
 echo ">>>>> Test succeeded"