}
#ifdef HAVE_LDAP_H
-static void init_ldap_usertab(void) {
+/*!
+ * \brief Connect to the LDAP server
+ */
+static void connect_ldap(void)
+{
char *ldapuri;
LDAPURLDesc url;
int rc;
- ldap_handle = NULL;
+ if (ldap_handle)
+ ldap_unbind(ldap_handle);
/* Setting LDAP connection and initializing cache */
memset(&url,0,sizeof(url));
debuga(__FILE__,__LINE__,_("Cannot bind to LDAP server: %s\n"), ldap_err2string(rc));
exit(EXIT_FAILURE);
}
+}
+
+/*!
+Initialize the communication with the LDAP server whose name is in
+::LDAPHost and connect to port ::LDAPPort.
+*/
+static void init_ldap_usertab(void)
+{
+ ldap_handle = NULL;
+ connect_ldap();
#ifdef USE_ICONV
// prepare for the string conversion
#endif
/* Initializing cache */
-
init_cache();
}
/* Search record(s) in LDAP base */
attrs[0]=LDAPTargetAttr;
attrs[1]=NULL;
- rc= ldap_search_ext_s(ldap_handle, LDAPBaseSearch, LDAP_SCOPE_SUBTREE, filtersearch, attrs, 0, NULL, NULL, NULL, -1, &result);
+ rc=ldap_search_ext_s(ldap_handle, LDAPBaseSearch, LDAP_SCOPE_SUBTREE, filtersearch, attrs, 0, NULL, NULL, NULL, -1, &result);
if (rc != LDAP_SUCCESS) {
- debuga(__FILE__,__LINE__,_("LDAP search failed: %s\nlooking for \"%s\" at or below \"%s\"\n"), ldap_err2string(rc),filtersearch,LDAPBaseSearch);
- safe_strcpy(mappedname,userlogin,namelen);
- return;
+ /*
+ * We know the connection was successfully established once. If it fails now,
+ * it may be because the server timed out between two requests or because
+ * there is an error in the request.
+ *
+ * Just in case the failure is due to a timeout, we try to connect and send
+ * the query again.
+ */
+ connect_ldap();
+ rc=ldap_search_ext_s(ldap_handle, LDAPBaseSearch, LDAP_SCOPE_SUBTREE, filtersearch, attrs, 0, NULL, NULL, NULL, -1, &result);
+ if (rc != LDAP_SUCCESS) {
+ debuga(__FILE__,__LINE__,_("LDAP search failed: %s\nlooking for \"%s\" at or below \"%s\"\n"), ldap_err2string(rc),filtersearch,LDAPBaseSearch);
+ safe_strcpy(mappedname,userlogin,namelen);
+ return;
+ }
}
if (!(e = ldap_first_entry(ldap_handle, result))) {