]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Try to reconnect to the LDAP server
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Fri, 2 Oct 2015 17:30:12 +0000 (19:30 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Fri, 2 Oct 2015 17:30:12 +0000 (19:30 +0200)
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

index 454b8710b34819b06b23ec41ee406a6685d07fec..8b02a1e61dfff786bf6e328209ab673a5b0c9089 100644 (file)
--- 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))) {