Fix more compile warnings for Windows in socket.c.
[claws.git] / src / ldaputil.c
index ed6231da5fec9472f55adbacc96a7f3c8f8126db..21e5d97d734e85dd02d74287f2ff14645d51dbcf 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2003-2006 Match Grun and the Sylpheed-Claws team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2003-2015 Match Grun and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_LDAP
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <string.h>
 #include <sys/time.h>
-#include <ldap.h>
-#include <lber.h>
+#include <errno.h>
+#include "common/utils.h"
+#include "ldaputil.h"
+#include "ldapserver.h"
+#include "ldapctrl.h"
 
 #define SYLDAP_TEST_FILTER   "(objectclass=*)"
 #define SYLDAP_SEARCHBASE_V2 "cn=config"
  * \return List of Base DN's, or NULL if could not read. List should be
  *         g_free() when done.
  */
-static GList *ldaputil_test_v3( LDAP *ld, gint tov ) {
+static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
        GList *baseDN = NULL;
        gint rc, i;
-       LDAPMessage *result, *e;
+       LDAPMessage *result = NULL, *e;
        gchar *attribs[2];
        BerElement *ber;
        gchar *attribute;
-       gchar **vals;
+       struct berval **vals;
        struct timeval timeout;
 
        /* Set timeout */
@@ -86,14 +90,14 @@ static GList *ldaputil_test_v3( LDAP *ld, gint tov ) {
                                if( strcasecmp(
                                        attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
                                {
-                                       vals = ldap_get_values( ld, e, attribute );
+                                       vals = ldap_get_values_len( ld, e, attribute );
                                        if( vals != NULL ) {
                                                for( i = 0; vals[i] != NULL; i++ ) {
                                                        baseDN = g_list_append(
-                                                               baseDN, g_strdup( vals[i] ) );
+                                                               baseDN, g_strndup( vals[i]->bv_val, vals[i]->bv_len ) );
                                                }
                                        }
-                                       ldap_value_free( vals );
+                                       ldap_value_free_len( vals );
                                }
                                ldap_memfree( attribute );
                        }
@@ -102,8 +106,13 @@ static GList *ldaputil_test_v3( LDAP *ld, gint tov ) {
                        }
                        ber = NULL;
                }
-       }
-       ldap_msgfree( result );
+       } else
+               debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
+       
+       if (errcode)
+               *errcode = rc;
+       if (result)
+               ldap_msgfree( result );
        return baseDN;
 }
 
@@ -117,11 +126,11 @@ static GList *ldaputil_test_v3( LDAP *ld, gint tov ) {
 static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
        GList *baseDN = NULL;
        gint rc, i;
-       LDAPMessage *result, *e;
+       LDAPMessage *result = NULL, *e;
        gchar *attribs[1];
        BerElement *ber;
        gchar *attribute;
-       gchar **vals;
+       struct berval **vals;
        struct timeval timeout;
 
        /* Set timeout */
@@ -152,26 +161,27 @@ static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
                                if( strcasecmp(
                                        attribute,
                                        SYLDAP_V2_TEST_ATTR ) == 0 ) {
-                                       vals = ldap_get_values( ld, e, attribute );
+                                       vals = ldap_get_values_len( ld, e, attribute );
                                        if( vals != NULL ) {
                                                for( i = 0; vals[i] != NULL; i++ ) {
-                                                       char *ch;
+                                                       char *ch, *tmp;
                                                        /*
                                                         * Strip the 'ldb:' from the
                                                         * front of the value.
                                                         */
-                                                       ch = ( char * ) strchr( vals[i], ':' );
+                                                       tmp = g_strndup( vals[i]->bv_val, vals[i]->bv_len);
+                                                       ch = ( char * ) strchr( tmp, ':' );
                                                        if( ch ) {
                                                                gchar *bn = g_strdup( ++ch );
-                                                               g_strchomp( bn );
-                                                               g_strchug( bn );
+                                                               g_strstrip( bn );
                                                                baseDN = g_list_append(
                                                                        baseDN, g_strdup( bn ) );
                                                                g_free( bn );
                                                        }
+                                                       g_free(tmp);
                                                }
                                        }
-                                       ldap_value_free( vals );
+                                       ldap_value_free_len( vals );
                                }
                                ldap_memfree( attribute );
                        }
@@ -181,10 +191,32 @@ static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
                        ber = NULL;
                }
        }
-       ldap_msgfree( result );
+       if (result)
+               ldap_msgfree( result );
        return baseDN;
 }
 
+int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
+{
+       debug_print("binding: DN->%s\n", dn?dn:"null");
+#ifdef G_OS_UNIX
+       struct berval cred;
+
+       if ( passwd != NULL ) {
+               cred.bv_val = (char *) passwd;
+               cred.bv_len = strlen( passwd );
+       } else {
+               cred.bv_val = "";
+               cred.bv_len = 0;
+       }
+
+       return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
+               NULL, NULL, NULL );
+#else
+       return ldap_simple_bind_s(ld, (PCHAR)dn, (PCHAR)passwd);
+#endif
+}
+
 /**
  * Attempt to discover the base DN for the server.
  * \param  host   Host name.
@@ -197,37 +229,48 @@ static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
  */
 GList *ldaputil_read_basedn(
                const gchar *host, const gint port, const gchar *bindDN,
-               const gchar *bindPW, const gint tov )
+               const gchar *bindPW, const gint tov, int ssl, int tls )
 {
        GList *baseDN = NULL;
-       LDAP *ld;
+       LDAP *ld = NULL;
+       LdapControl *ctl = ldapctl_create();
        gint rc;
 
-       if( host == NULL ) return baseDN;
-       if( port < 1 ) return baseDN;
+       if( host == NULL ) 
+               return NULL;
+       if( port < 1 ) 
+               return NULL;
 
-       /* Connect to server. */
-       if( ( ld = ldap_init( host, port ) ) == NULL ) {
-               return baseDN;
-       }
+       ldapctl_set_tls(ctl, tls);
+       ldapctl_set_ssl(ctl, ssl);
+       ldapctl_set_port(ctl, port);
+       ldapctl_set_host(ctl, host);
+       ldapctl_set_timeout(ctl, tov);
+       ldapctl_set_bind_dn(ctl, bindDN);
 
-       /* Bind to the server, if required */
-       if( bindDN ) {
-               if( *bindDN != '\0' ) {
-                       rc = ldap_simple_bind_s( ld, bindDN, bindPW );
-                       if( rc != LDAP_SUCCESS ) {
-                               ldap_unbind( ld );
-                               return baseDN;
-                       }
-               }
+       ld = ldapsvr_connect(ctl);
+       if (ld == NULL) {
+               ldapctl_free(ctl);
+               return NULL;
        }
+       baseDN = ldaputil_test_v3( ld, tov, &rc );
+       if (baseDN)
+               debug_print("Using LDAP v3\n");
 
-       /* Test for LDAP version 3 */
-       baseDN = ldaputil_test_v3( ld, tov );
-       if( baseDN == NULL ) {
+#ifdef G_OS_UNIX
+       if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
+#else
+       if( baseDN == NULL) {
+#endif
                baseDN = ldaputil_test_v2( ld, tov );
+               if (baseDN)
+                       debug_print("Using LDAP v2\n");
        }
-       ldap_unbind( ld );
+       if (ld)
+               ldapsvr_disconnect(ld);
+
+       ldapctl_free(ctl);
+       
        return baseDN;
 }
 
@@ -238,17 +281,25 @@ GList *ldaputil_read_basedn(
  * \param  port Port number.
  * \return <i>TRUE</i> if connected successfully.
  */
-gboolean ldaputil_test_connect( const gchar *host, const gint port ) {
+gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
        gboolean retVal = FALSE;
+       LdapControl *ctl = ldapctl_create();
        LDAP *ld;
 
-       if( host == NULL ) return retVal;
-       if( port < 1 ) return retVal;
-       ld = ldap_open( host, port );
+       ldapctl_set_tls(ctl, tls);
+       ldapctl_set_ssl(ctl, ssl);
+       ldapctl_set_port(ctl, port);
+       ldapctl_set_host(ctl, host);
+       ldapctl_set_timeout(ctl, secs);
+
+       ld = ldapsvr_connect(ctl);
        if( ld != NULL ) {
-               ldap_unbind( ld );
+               ldapsvr_disconnect(ld);
+               debug_print("ld != NULL\n");
                retVal = TRUE;
        }
+       ldapctl_free(ctl);
+
        return retVal;
 }
 
@@ -260,6 +311,27 @@ gboolean ldaputil_test_ldap_lib( void ) {
        return TRUE;
 }
 
+const gchar *ldaputil_get_error(LDAP *ld)
+{
+       gchar *ld_error = NULL;
+       static gchar error[512];
+
+       ldap_get_option( ld, LDAP_OPT_ERROR_STRING, &ld_error);
+       if (ld_error != NULL)
+               strncpy2(error, ld_error, sizeof(error));
+       else
+               strncpy2(error, _("Unknown error"), sizeof(error));
+#ifndef G_OS_WIN32
+       /* From https://msdn.microsoft.com/en-us/library/aa366594%28v=vs.85%29.aspx
+        * "LDAP_OPT_ERROR_STRING returns a pointer to an internal static
+        * string table, and ldap_memfree should not be called when using
+        * this session option."
+        */
+       ldap_memfree(ld_error);
+#endif
+
+       return error;
+}
 #endif /* USE_LDAP */
 
 /*