claws-mail/src/ldaputil.c
Colin Leroy 6edf2f5fe8 2012-07-07 [colin] 3.8.1cvs7
* claws-features.h.in
	* configure.ac
	* Makefile.am
	* src/account.c
	* src/action.c
	* src/addr_compl.c
	* src/addrcustomattr.c
	* src/addrduplicates.c
	* src/addressadd.c
	* src/addressbook.c
	* src/addressbook_foldersel.c
	* src/addrgather.c
	* src/addrindex.c
	* src/alertpanel.c
	* src/autofaces.c
	* src/browseldap.c
	* src/codeconv.c
	* src/codeconv.h
	* src/compose.c
	* src/crash.c
	* src/customheader.c
	* src/displayheader.c
	* src/editaddress.c
	* src/editaddress_other_attributes_ldap.c
	* src/editbook.c
	* src/editgroup.c
	* src/editjpilot.c
	* src/editldap.c
	* src/editldap_basedn.c
	* src/edittags.c
	* src/editvcard.c
	* src/exphtmldlg.c
	* src/expldifdlg.c
	* src/export.c
	* src/exporthtml.c
	* src/exportldif.c
	* src/folder.c
	* src/folder_item_prefs.c
	* src/grouplistdialog.c
	* src/headerview.c
	* src/image_viewer.c
	* src/imap.c
	* src/imap_gtk.c
	* src/imap_gtk.h
	* src/import.c
	* src/importldif.c
	* src/importmutt.c
	* src/importpine.c
	* src/inc.c
	* src/inc.h
	* src/jpilot.c
	* src/jpilot.h
	* src/ldapctrl.c
	* src/ldaplocate.c
	* src/ldapquery.c
	* src/ldapserver.c
	* src/ldapupdate.c
	* src/ldaputil.c
	* src/main.c
	* src/manual.c
	* src/matcher.c
	* src/mbox.c
	* src/message_search.c
	* src/messageview.h
	* src/mh.c
	* src/mh_gtk.c
	* src/mimeview.c
	* src/msgcache.c
	* src/msgcache.h
	* src/news.c
	* src/news_gtk.c
	* src/news_gtk.h
	* src/noticeview.c
	* src/partial_download.c
	* src/partial_download.h
	* src/pop.c
	* src/pop.h
	* src/prefs_account.c
	* src/prefs_account.h
	* src/prefs_actions.c
	* src/prefs_common.c
	* src/prefs_common.h
	* src/prefs_compose_writing.c
	* src/prefs_customheader.c
	* src/prefs_display_header.c
	* src/prefs_ext_prog.c
	* src/prefs_filtering.c
	* src/prefs_filtering_action.c
	* src/prefs_folder_column.c
	* src/prefs_folder_item.c
	* src/prefs_fonts.c
	* src/prefs_gtk.c
	* src/prefs_image_viewer.c
	* src/prefs_logging.c
	* src/prefs_matcher.c
	* src/prefs_message.c
	* src/prefs_msg_colors.c
	* src/prefs_other.c
	* src/prefs_quote.c
	* src/prefs_receive.c
	* src/prefs_send.c
	* src/prefs_spelling.c
	* src/prefs_summaries.c
	* src/prefs_summary_column.c
	* src/prefs_summary_open.c
	* src/prefs_toolbar.c
	* src/prefs_wrapping.c
	* src/printing.c
	* src/printing.h
	* src/procheader.c
	* src/procmime.c
	* src/procmime.h
	* src/procmsg.h
	* src/quote_fmt.c
	* src/recv.c
	* src/send_message.c
	* src/setup.c
	* src/ssl_manager.c
	* src/statusbar.c
	* src/summary_search.c
	* src/textview.c
	* src/textview.h
	* src/toolbar.c
	* src/undo.c
	* src/unmime.c
	* src/uri_opener.c
	* src/wizard.c
	* src/common/claws.c
	* src/common/defs.h
	* src/common/hooks.c
	* src/common/log.c
	* src/common/log.h
	* src/common/plugin.c
	* src/common/prefs.c
	* src/common/session.c
	* src/common/session.h
	* src/common/smtp.c
	* src/common/smtp.h
	* src/common/socket.c
	* src/common/socket.h
	* src/common/ssl.c
	* src/common/ssl.h
	* src/common/ssl_certificate.c
	* src/common/ssl_certificate.h
	* src/common/string_match.c
	* src/common/tags.c
	* src/common/timing.h
	* src/common/utils.c
	* src/common/utils.h
	* src/etpan/etpan-thread-manager.c
	* src/etpan/imap-thread.c
	* src/etpan/nntp-thread.c
	* src/gtk/about.c
	* src/gtk/combobox.c
	* src/gtk/description_window.c
	* src/gtk/filesel.c
	* src/gtk/gtkaspell.c
	* src/gtk/gtkaspell.h
	* src/gtk/gtkshruler.c
	* src/gtk/gtkunit.c
	* src/gtk/gtkutils.c
	* src/gtk/gtkutils.h
	* src/gtk/gtkvscrollbutton.c
	* src/gtk/icon_legend.c
	* src/gtk/inputdialog.c
	* src/gtk/logwindow.c
	* src/gtk/menu.c
	* src/gtk/pluginwindow.c
	* src/gtk/pluginwindow.h
	* src/gtk/prefswindow.c
	* src/gtk/progressdialog.c
	* src/gtk/quicksearch.c
	* src/gtk/spell_entry.c
	* src/gtk/spell_entry.h
	* src/gtk/sslcertwindow.c
	* src/gtk/sslcertwindow.h
	* src/plugins/bogofilter/bogofilter.c
	* src/plugins/bogofilter/bogofilter_gtk.c
	* src/plugins/dillo_viewer/dillo_prefs.c
	* src/plugins/dillo_viewer/dillo_viewer.c
	* src/plugins/pgpcore/pgp_viewer.c
	* src/plugins/pgpcore/plugin.c
	* src/plugins/pgpcore/sgpgme.c
	* src/plugins/pgpinline/pgpinline.c
	* src/plugins/pgpinline/plugin.c
	* src/plugins/pgpmime/pgpmime.c
	* src/plugins/pgpmime/plugin.c
	* src/plugins/smime/plugin.c
	* src/plugins/smime/smime.c
	* src/plugins/spamassassin/libspamc.c
	* src/plugins/spamassassin/spamassassin.c
	* src/plugins/spamassassin/spamassassin_gtk.c
	* src/plugins/trayicon/trayicon.c
	* src/plugins/trayicon/trayicon_prefs.c
		Specify Claws compile-time features in a specific header
2012-07-07 07:09:39 +00:00

335 lines
8 KiB
C

/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
* Copyright (C) 2003-2012 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* 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, see <http://www.gnu.org/licenses/>.
*
*/
/*
* Some utility functions to access LDAP servers.
*/
#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 <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"
#define SYLDAP_SEARCHBASE_V3 ""
#define SYLDAP_V2_TEST_ATTR "database"
#define SYLDAP_V3_TEST_ATTR "namingcontexts"
/**
* Attempt to discover the base DN for a server using LDAP version 3.
* \param ld LDAP handle for a connected server.
* \param tov Timeout value (seconds), or 0 for none, default 30 secs.
* \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, gint *errcode ) {
GList *baseDN = NULL;
gint rc, i;
LDAPMessage *result = NULL, *e;
gchar *attribs[2];
BerElement *ber;
gchar *attribute;
struct berval **vals;
struct timeval timeout;
/* Set timeout */
timeout.tv_usec = 0L;
if( tov > 0 ) {
timeout.tv_sec = tov;
}
else {
timeout.tv_sec = 30L;
}
/* Test for LDAP version 3 */
attribs[0] = SYLDAP_V3_TEST_ATTR;
attribs[1] = NULL;
rc = ldap_search_ext_s(
ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
attribs, 0, NULL, NULL, &timeout, 0, &result );
if( rc == LDAP_SUCCESS ) {
/* Process entries */
for( e = ldap_first_entry( ld, result );
e != NULL;
e = ldap_next_entry( ld, e ) )
{
/* Process attributes */
for( attribute = ldap_first_attribute( ld, e, &ber );
attribute != NULL;
attribute = ldap_next_attribute( ld, e, ber ) )
{
if( strcasecmp(
attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
{
vals = ldap_get_values_len( ld, e, attribute );
if( vals != NULL ) {
for( i = 0; vals[i] != NULL; i++ ) {
baseDN = g_list_append(
baseDN, g_strndup( vals[i]->bv_val, vals[i]->bv_len ) );
}
}
ldap_value_free_len( vals );
}
ldap_memfree( attribute );
}
if( ber != NULL ) {
ber_free( ber, 0 );
}
ber = NULL;
}
} else
debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
if (errcode)
*errcode = rc;
if (result)
ldap_msgfree( result );
return baseDN;
}
/**
* Attempt to discover the base DN for a server using LDAP version 2.
* \param ld LDAP handle for a connected server.
* \param tov Timeout value (seconds), or 0 for none, default 30 secs.
* \return List of Base DN's, or NULL if could not read. List should be
* g_free() when done.
*/
static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
GList *baseDN = NULL;
gint rc, i;
LDAPMessage *result = NULL, *e;
gchar *attribs[1];
BerElement *ber;
gchar *attribute;
struct berval **vals;
struct timeval timeout;
/* Set timeout */
timeout.tv_usec = 0L;
if( tov > 0 ) {
timeout.tv_sec = tov;
}
else {
timeout.tv_sec = 30L;
}
attribs[0] = NULL;
rc = ldap_search_ext_s(
ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
attribs, 0, NULL, NULL, &timeout, 0, &result );
if( rc == LDAP_SUCCESS ) {
/* Process entries */
for( e = ldap_first_entry( ld, result );
e != NULL;
e = ldap_next_entry( ld, e ) )
{
/* Process attributes */
for( attribute = ldap_first_attribute( ld, e, &ber );
attribute != NULL;
attribute = ldap_next_attribute( ld, e, ber ) )
{
if( strcasecmp(
attribute,
SYLDAP_V2_TEST_ATTR ) == 0 ) {
vals = ldap_get_values_len( ld, e, attribute );
if( vals != NULL ) {
for( i = 0; vals[i] != NULL; i++ ) {
char *ch, *tmp;
/*
* Strip the 'ldb:' from the
* front of the value.
*/
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 );
baseDN = g_list_append(
baseDN, g_strdup( bn ) );
g_free( bn );
}
g_free(tmp);
}
}
ldap_value_free_len( vals );
}
ldap_memfree( attribute );
}
if( ber != NULL ) {
ber_free( ber, 0 );
}
ber = NULL;
}
}
if (result)
ldap_msgfree( result );
return baseDN;
}
int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
{
struct berval cred;
if ( passwd != NULL ) {
cred.bv_val = (char *) passwd;
cred.bv_len = strlen( passwd );
} else {
cred.bv_val = "";
cred.bv_len = 0;
}
debug_print("binding: DN->%s\n", dn?dn:"null");
#ifdef G_OS_UNIX
return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
NULL, NULL, NULL );
#else
return ldap_simple_bind_s(ld, dn, passwd);
#endif
}
/**
* Attempt to discover the base DN for the server.
* \param host Host name.
* \param port Port number.
* \param bindDN Bind DN (optional).
* \param bindPW Bind PW (optional).
* \param tov Timeout value (seconds), or 0 for none, default 30 secs.
* \return List of Base DN's, or NULL if could not read. This list should be
* g_free() when done.
*/
GList *ldaputil_read_basedn(
const gchar *host, const gint port, const gchar *bindDN,
const gchar *bindPW, const gint tov, int ssl, int tls )
{
GList *baseDN = NULL;
LDAP *ld = NULL;
LdapControl *ctl = ldapctl_create();
gint rc;
if( host == NULL )
return NULL;
if( port < 1 )
return NULL;
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);
ldapctl_set_bind_password(ctl, bindPW, FALSE, FALSE);
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");
#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");
}
if (ld)
ldapsvr_disconnect(ld);
ldapctl_free(ctl);
return baseDN;
}
/**
* Attempt to connect to the server.
* Enter:
* \param host Host name.
* \param port Port number.
* \return <i>TRUE</i> if connected successfully.
*/
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;
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 ) {
ldapsvr_disconnect(ld);
debug_print("ld != NULL\n");
retVal = TRUE;
}
ldapctl_free(ctl);
return retVal;
}
/**
* Test whether LDAP libraries installed.
* Return: TRUE if library available.
*/
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));
ldap_memfree(ld_error);
return error;
}
#endif /* USE_LDAP */
/*
* End of Source.
*/