Index: .travis.yml
===================================================================
--- .travis.yml	(revision a6005da78ae1563ae4577179d1e54fff74cfe21a)
+++ .travis.yml	(revision 50bb49039b264ac599ad1ddee2be86cfc7cb68ce)
@@ -3,5 +3,5 @@
 
 script:
- - ./configure --pam=1
+ - ./configure --pam=1 --ldap=1
  - make check
  - BITLBEE_SKYPE=plugin dpkg-buildpackage -uc -us -d
@@ -30,4 +30,5 @@
     - check
     - libpam0g-dev
+    - libldap2-dev
   coverity_scan:
     project:
@@ -35,5 +36,5 @@
       description: "An IRC to other chat networks gateway"
     notification_email: dx@dxzone.com.ar
-    build_command_prepend: ./configure --otr=1 --debug=1 --pam=1
+    build_command_prepend: ./configure --otr=1 --debug=1 --pam=1 --ldap=1
     build_command: make
     branch_pattern: coverity_scan
Index: auth.c
===================================================================
--- auth.c	(revision a6005da78ae1563ae4577179d1e54fff74cfe21a)
+++ auth.c	(revision 50bb49039b264ac599ad1ddee2be86cfc7cb68ce)
@@ -4,4 +4,7 @@
 #ifdef WITH_PAM
 extern auth_backend_t auth_pam;
+#endif
+#ifdef WITH_LDAP
+extern auth_backend_t auth_ldap;
 #endif
 
@@ -13,4 +16,10 @@
 	gl = g_list_append(gl, &auth_pam);
 	if (backend && !strcmp(backend, "pam")) {
+		ok = 1;
+	}
+#endif
+#ifdef WITH_LDAP
+	gl = g_list_append(gl, &auth_ldap);
+	if (backend && !strcmp(backend, "ldap")) {
 		ok = 1;
 	}
Index: auth_ldap.c
===================================================================
--- auth_ldap.c	(revision 50bb49039b264ac599ad1ddee2be86cfc7cb68ce)
+++ auth_ldap.c	(revision 50bb49039b264ac599ad1ddee2be86cfc7cb68ce)
@@ -0,0 +1,77 @@
+#define BITLBEE_CORE
+#define LDAP_DEPRECATED 1
+#include "bitlbee.h"
+#include <ldap.h>
+
+static storage_status_t ldap_check_pass(const char *nick, const char *password)
+{
+	LDAP *ldap;
+	LDAPMessage *msg, *entry;
+	char *dn = NULL;
+	char *filter;
+	char *attrs[1] = { NULL };
+	int ret, count;
+
+	if((ret = ldap_initialize(&ldap, NULL)) != LDAP_SUCCESS) {
+		log_message(LOGLVL_WARNING, "ldap_initialize failed: %s", ldap_err2string(ret));
+		return STORAGE_OTHER_ERROR;
+	}
+
+	/* First we do an anonymous bind to map uid=$nick to a DN*/
+	if((ret = ldap_simple_bind_s(ldap, NULL, NULL)) != LDAP_SUCCESS) {
+		ldap_unbind_s(ldap);
+		log_message(LOGLVL_WARNING, "Anonymous bind failed: %s", ldap_err2string(ret));
+		return STORAGE_OTHER_ERROR;
+	}
+
+
+	/* We search and process the result */
+	filter = g_strdup_printf("(uid=%s)", nick);
+	ret = ldap_search_ext_s(ldap, NULL, LDAP_SCOPE_SUBTREE, filter, attrs, 0, NULL, NULL, NULL, 1, &msg);
+	g_free(filter);
+
+	if(ret != LDAP_SUCCESS) {
+		ldap_unbind_s(ldap);
+		log_message(LOGLVL_WARNING, "uid search failed: %s", ldap_err2string(ret));
+		return STORAGE_OTHER_ERROR;
+	}
+
+	count = ldap_count_entries(ldap, msg);
+	if (count == -1) {
+		ldap_get_option(ldap, LDAP_OPT_ERROR_NUMBER, &ret);
+		ldap_msgfree(msg);
+		ldap_unbind_s(ldap);
+		log_message(LOGLVL_WARNING, "uid search failed: %s", ldap_err2string(ret));
+		return STORAGE_OTHER_ERROR;
+	}
+
+	if (!count) {
+		ldap_msgfree(msg);
+		ldap_unbind_s(ldap);
+		return STORAGE_NO_SUCH_USER;
+	}
+
+	entry = ldap_first_entry(ldap, msg);
+	dn = ldap_get_dn(ldap, entry);
+	ldap_msgfree(msg);
+
+	/* And now we bind as the user to authenticate */
+	ret = ldap_simple_bind_s(ldap, dn, password);
+	g_free(dn);
+	ldap_unbind_s(ldap);
+
+	switch (ret) {
+		case LDAP_SUCCESS:
+			return STORAGE_OK;
+		case LDAP_INVALID_CREDENTIALS:
+			return STORAGE_INVALID_PASSWORD;
+		default:
+			log_message(LOGLVL_WARNING, "Authenticated bind failed: %s", ldap_err2string(ret));
+			return STORAGE_OTHER_ERROR;
+	}
+}
+
+auth_backend_t auth_ldap = {
+	.name = "ldap",
+	.check_pass = ldap_check_pass,
+};
Index: bitlbee.conf
===================================================================
--- bitlbee.conf	(revision a6005da78ae1563ae4577179d1e54fff74cfe21a)
+++ bitlbee.conf	(revision 50bb49039b264ac599ad1ddee2be86cfc7cb68ce)
@@ -66,4 +66,5 @@
 ## - storage (internal storage)
 ## - pam (Linux PAM authentication)
+## - ldap (LDAP server configured in the openldap settings)
 #
 # AuthBackend = storage
Index: conf.c
===================================================================
--- conf.c	(revision a6005da78ae1563ae4577179d1e54fff74cfe21a)
+++ conf.c	(revision 50bb49039b264ac599ad1ddee2be86cfc7cb68ce)
@@ -245,5 +245,6 @@
 				if (g_strcasecmp(ini->value, "storage") == 0) {
 					conf->auth_backend = NULL;
-				} else if (g_strcasecmp(ini->value, "pam") == 0) {
+				} else if (g_strcasecmp(ini->value, "pam") == 0 ||
+				         g_strcasecmp(ini->value, "ldap") == 0) {
 					g_free(conf->auth_backend);
 					conf->auth_backend = g_strdup(ini->value);
Index: configure
===================================================================
--- configure	(revision a6005da78ae1563ae4577179d1e54fff74cfe21a)
+++ configure	(revision 50bb49039b264ac599ad1ddee2be86cfc7cb68ce)
@@ -53,4 +53,5 @@
 
 pam=0
+ldap=0
 
 pie=1
@@ -137,4 +138,5 @@
 
 --pam=0/1	Disable/enable PAM authentication	$pam
+--ldap=0/1	Disable/enable LDAP authentication	$ldap
 
 --doc=0/1	Disable/enable help.txt generation	$doc
@@ -645,4 +647,15 @@
 	authobjs=$authobjs'auth_pam.o '
 	authlibs=$authlibs'-lpam '
+fi
+if [ "$ldap" = 0 ]; then
+	echo '#undef WITH_LDAP' >> config.h
+else
+	if ! echo '#include <ldap.h>' | $CC -E - >/dev/null 2>/dev/null; then
+		echo 'Cannot find libldap development libraries, aborting. (Install libldap2-dev?)'
+		exit 1
+	fi
+	echo '#define WITH_LDAP' >> config.h
+	authobjs=$authobjs'auth_ldap.o '
+	authlibs=$authlibs'-lldap '
 fi
 echo AUTH_OBJS=$authobjs >> Makefile.settings
Index: tests/Makefile
===================================================================
--- tests/Makefile	(revision a6005da78ae1563ae4577179d1e54fff74cfe21a)
+++ tests/Makefile	(revision 50bb49039b264ac599ad1ddee2be86cfc7cb68ce)
@@ -15,5 +15,5 @@
 distclean: clean
 
-main_objs = bitlbee.o conf.o dcc.o help.o ipc.o irc.o irc_cap.o irc_channel.o irc_commands.o irc_im.o irc_send.o irc_user.o irc_util.o irc_commands.o log.o nick.o query.o root_commands.o set.o storage.o storage_xml.o auth.o auth_pam.o
+main_objs = bitlbee.o conf.o dcc.o help.o ipc.o irc.o irc_cap.o irc_channel.o irc_commands.o irc_im.o irc_send.o irc_user.o irc_util.o irc_commands.o log.o nick.o query.o root_commands.o set.o storage.o storage_xml.o auth.o auth_pam.o auth_ldap.o
 
 test_objs = check.o check_util.o check_nick.o check_md5.o check_arc.o check_irc.o check_help.o check_user.o check_set.o check_jabber_sasl.o check_jabber_util.o
