From c167ad37364b62cff1dbe53bbf8273593523da61 Mon Sep 17 00:00:00 2001 From: Frederic Marchal Date: Fri, 2 Oct 2015 19:30:12 +0200 Subject: [PATCH] Try to reconnect to the LDAP server If a query fails, we try to connect again to the server just in case it timed out between two requests. If it fails again, sarg abort the reporting. --- usertab.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/usertab.c b/usertab.c index 454b871..8b02a1e 100644 --- a/usertab.c +++ b/usertab.c @@ -185,12 +185,17 @@ static void get_usertab_name(const char *user,char *name,int namelen) } #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)); @@ -227,6 +232,16 @@ static void init_ldap_usertab(void) { 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 @@ -242,7 +257,6 @@ static void init_ldap_usertab(void) { #endif /* Initializing cache */ - init_cache(); } @@ -357,11 +371,23 @@ static void get_ldap_name(const char *userlogin,char *mappedname,int namelen) /* 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))) { -- 2.47.3