https://bugzilla.samba.org/show_bug.cgi?id=4372
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=21263

getpeername() returning -1 is not a reliable indication if a TCP
connection is dead. Might be my code, this rings a very distant bell...
This patch fixes a problem that Samba retries to use the dead LDAP
connection 10 times. [OST-Tech:352]

Index: source/lib/smbldap.c
===================================================================
--- source/lib/smbldap.c	(Revision 21260)
+++ source/lib/smbldap.c	(Arbeitskopie)
@@ -1230,12 +1230,23 @@
 				       sizelimit, res);
 		if (rc != LDAP_SUCCESS) {
 			char *ld_error = NULL;
+			int ld_errno;
+
 			ldap_get_option(ldap_state->ldap_struct,
+					LDAP_OPT_ERROR_NUMBER, &ld_errno);
+
+			ldap_get_option(ldap_state->ldap_struct,
 					LDAP_OPT_ERROR_STRING, &ld_error);
-			DEBUG(10,("Failed search for base: %s, error: %s "
-				  "(%s)\n", base, ldap_err2string(rc),
-				  ld_error ? ld_error : "unknown"));
+			DEBUG(10, ("Failed search for base: %s, error: %d (%s) "
+				   "(%s)\n", base, ld_errno,
+				   ldap_err2string(rc),
+				   ld_error ? ld_error : "unknown"));
 			SAFE_FREE(ld_error);
+
+			if (ld_errno == LDAP_SERVER_DOWN) {
+				ldap_unbind(ldap_state->ldap_struct);
+				ldap_state->ldap_struct = NULL;
+			}
 		}
 	}
 
@@ -1370,12 +1381,23 @@
 		rc = ldap_modify_s(ldap_state->ldap_struct, utf8_dn, attrs);
 		if (rc != LDAP_SUCCESS) {
 			char *ld_error = NULL;
+			int ld_errno;
+
 			ldap_get_option(ldap_state->ldap_struct,
+					LDAP_OPT_ERROR_NUMBER, &ld_errno);
+
+			ldap_get_option(ldap_state->ldap_struct,
 					LDAP_OPT_ERROR_STRING, &ld_error);
-			DEBUG(10,("Failed to modify dn: %s, error: %s "
-				  "(%s)\n", dn, ldap_err2string(rc),
-				  ld_error ? ld_error : "unknown"));
+			DEBUG(10, ("Failed to modify dn: %s, error: %d (%s) "
+				   "(%s)\n", dn, ld_errno,
+				   ldap_err2string(rc),
+				   ld_error ? ld_error : "unknown"));
 			SAFE_FREE(ld_error);
+
+			if (ld_errno == LDAP_SERVER_DOWN) {
+				ldap_unbind(ldap_state->ldap_struct);
+				ldap_state->ldap_struct = NULL;
+			}
 		}
 	}
 		
@@ -1402,12 +1424,23 @@
 		rc = ldap_add_s(ldap_state->ldap_struct, utf8_dn, attrs);
 		if (rc != LDAP_SUCCESS) {
 			char *ld_error = NULL;
+			int ld_errno;
+
 			ldap_get_option(ldap_state->ldap_struct,
+					LDAP_OPT_ERROR_NUMBER, &ld_errno);
+
+			ldap_get_option(ldap_state->ldap_struct,
 					LDAP_OPT_ERROR_STRING, &ld_error);
-			DEBUG(10,("Failed to add dn: %s, error: %s "
-				  "(%s)\n", dn, ldap_err2string(rc),
-				  ld_error ? ld_error : "unknown"));
+			DEBUG(10, ("Failed to add dn: %s, error: %d (%s) "
+				   "(%s)\n", dn, ld_errno,
+				   ldap_err2string(rc),
+				   ld_error ? ld_error : "unknown"));
 			SAFE_FREE(ld_error);
+
+			if (ld_errno == LDAP_SERVER_DOWN) {
+				ldap_unbind(ldap_state->ldap_struct);
+				ldap_state->ldap_struct = NULL;
+			}
 		}
 	}
 	
@@ -1434,12 +1467,23 @@
 		rc = ldap_delete_s(ldap_state->ldap_struct, utf8_dn);
 		if (rc != LDAP_SUCCESS) {
 			char *ld_error = NULL;
+			int ld_errno;
+
 			ldap_get_option(ldap_state->ldap_struct,
+					LDAP_OPT_ERROR_NUMBER, &ld_errno);
+
+			ldap_get_option(ldap_state->ldap_struct,
 					LDAP_OPT_ERROR_STRING, &ld_error);
-			DEBUG(10,("Failed to delete dn: %s, error: %s "
-				  "(%s)\n", dn, ldap_err2string(rc),
-				  ld_error ? ld_error : "unknown"));
+			DEBUG(10, ("Failed to delete dn: %s, error: %d (%s) "
+				   "(%s)\n", dn, ld_errno,
+				   ldap_err2string(rc),
+				   ld_error ? ld_error : "unknown"));
 			SAFE_FREE(ld_error);
+
+			if (ld_errno == LDAP_SERVER_DOWN) {
+				ldap_unbind(ldap_state->ldap_struct);
+				ldap_state->ldap_struct = NULL;
+			}
 		}
 	}
 	
@@ -1465,12 +1509,23 @@
 					       clientctrls, retoidp, retdatap);
 		if (rc != LDAP_SUCCESS) {
 			char *ld_error = NULL;
+			int ld_errno;
+
 			ldap_get_option(ldap_state->ldap_struct,
+					LDAP_OPT_ERROR_NUMBER, &ld_errno);
+
+			ldap_get_option(ldap_state->ldap_struct,
 					LDAP_OPT_ERROR_STRING, &ld_error);
-			DEBUG(10,("Extended operation failed with error: %s "
-				  "(%s)\n", ldap_err2string(rc),
-				  ld_error ? ld_error : "unknown"));
+			DEBUG(10, ("Extended operation failed with error: "
+				   "%d (%s) (%s)\n", ld_errno,
+				   ldap_err2string(rc),
+				   ld_error ? ld_error : "unknown"));
 			SAFE_FREE(ld_error);
+
+			if (ld_errno == LDAP_SERVER_DOWN) {
+				ldap_unbind(ldap_state->ldap_struct);
+				ldap_state->ldap_struct = NULL;
+			}
 		}
 	}
 		
