--- acinclude.m4.orig	Thu Dec 21 09:36:07 2006
+++ acinclude.m4	Thu Dec 21 12:59:14 2006
@@ -181,7 +181,7 @@
 ])
 
 dnl DBMAIL_SIEVE_CONF
-dnl check for ldap or sql authentication
+dnl check for sieve sorting
 AC_DEFUN([DBMAIL_SIEVE_CONF], [dnl
 WARN=0
 
@@ -400,7 +400,8 @@
         AC_MSG_ERROR([Could not find LDAP library.])
     else
         AC_MSG_RESULT($LDAPLIB)
-        AC_DEFINE([AUTHLDAP], 1, [Define if LDAP sorting will be used.])
+        AC_DEFINE([AUTHLDAP], 1, [Define if LDAP will be used.])
+        AC_SEARCH_LIBS(ldap_initialize, ldap, AC_DEFINE([HAVE_LDAP_INITIALIZE], 1, [ldap_initialize() can be used instead of ldap_init()]))
         AC_SUBST(LDAPLIB)
         AC_SUBST(LDAPINC)
         AUTHALIB="modules/.libs/libauth_ldap.a"
--- dbmail.conf.orig	Mon Nov 20 10:18:12 2006
+++ dbmail.conf	Thu Dec 21 16:36:12 2006
@@ -219,6 +219,13 @@
 BASE_DN		= ou=People,dc=mydomain,dc=com
 
 # 
+# If your LDAP library supports ldap_initialize(), then you can use the
+# alternative LDAP server DSN like following.
+#
+# URI		= ldap://127.0.0.1:389
+# URI		= ldapi://%2fvar%2frun%2fopenldap%2fldapi/
+
+# 
 # Leave blank for anonymous bind.
 # example: cn=admin,dc=mydomain,dc=com     
 #
--- modules/authldap.c.orig	Fri Nov 10 14:10:08 2006
+++ modules/authldap.c	Thu Dec 21 16:52:50 2006
@@ -46,7 +46,7 @@
 char _ldap_query[AUTH_QUERY_SIZE];
 
 typedef struct _ldap_cfg {
-	field_t bind_dn, bind_pw, base_dn, port, version, scope, hostname;
+	field_t bind_dn, bind_pw, base_dn, port, uri, version, scope, hostname;
 	field_t user_objectclass, forw_objectclass;
 	field_t cn_string;
 	field_t field_uid, field_cid, min_cid, max_cid, field_nid, min_nid, max_nid;
@@ -76,6 +76,7 @@
 	GETCONFIGVALUE("BIND_PW",		"LDAP", _ldap_cfg.bind_pw);
 	GETCONFIGVALUE("BASE_DN",		"LDAP", _ldap_cfg.base_dn);
 	GETCONFIGVALUE("PORT",			"LDAP", _ldap_cfg.port);
+	GETCONFIGVALUE("URI",			"LDAP", _ldap_cfg.uri);
 	GETCONFIGVALUE("VERSION",		"LDAP", _ldap_cfg.version);
 	GETCONFIGVALUE("HOSTNAME",		"LDAP", _ldap_cfg.hostname);
 	GETCONFIGVALUE("USER_OBJECTCLASS",	"LDAP", _ldap_cfg.user_objectclass);
@@ -153,25 +154,58 @@
 int auth_connect(void)
 {
 	int version;
+#ifdef HAVE_LDAP_INITIALIZE
+	int ret;
+	char *uri;
+#endif
 
 	if (_ldap_conn != NULL) 
 		return 0;
 	
 	__auth_get_config();
-	
-	TRACE(TRACE_DEBUG, "connecting to ldap server on [%s] : [%d] version [%d]",
-		_ldap_cfg.hostname, _ldap_cfg.port_int, _ldap_cfg.version_int);
-	
-	_ldap_conn = ldap_init(
-			_ldap_cfg.hostname, 
-			_ldap_cfg.port_int);
 
 	switch (_ldap_cfg.version_int) {
 	case 2:
 		version = LDAP_VERSION2;
+		TRACE(TRACE_DEBUG, 
+			"connecting to ldap server on [%s] : [%d] version [%d]",
+			_ldap_cfg.hostname, _ldap_cfg.port_int, _ldap_cfg.version_int);
+		_ldap_conn = ldap_init(_ldap_cfg.hostname, _ldap_cfg.port_int);
 		break;
 	case 3:
 		version = LDAP_VERSION3;
+		if (strlen(_ldap_cfg.uri)) {
+#ifdef HAVE_LDAP_INITIALIZE
+			TRACE(TRACE_DEBUG, 
+				"connecting to ldap server on [%s] version [%d]", 
+				_ldap_cfg.uri, _ldap_cfg.version_int);
+			if ((ret = ldap_initialize(&_ldap_conn, _ldap_cfg.uri) 
+				== LDAP_SUCCESS)) 
+				break;
+			else 
+				TRACE(TRACE_WARNING, "ldap_initialize() returned %d", ret);
+#else
+			TRACE(TRACE_WARNING, "LDAP library doesn't support ldap_initialize(). You cannot use the URI directive to specify the DSN.");
+#endif
+		}
+
+#ifdef HAVE_LDAP_INITIALIZE
+		if (asprintf(&uri, "ldap://%s:%d", _ldap_cfg.hostname, 
+			_ldap_cfg.port_int) == -1) 
+			TRACE(TRACE_FATAL, "asprintf() returned -1");
+
+		TRACE(TRACE_DEBUG, 
+			"connecting to ldap server on [%s] version [%d]", 
+			uri, _ldap_cfg.version_int);
+		if ((ret = ldap_initialize(&_ldap_conn, uri)) != LDAP_SUCCESS) 
+			TRACE(TRACE_FATAL, "ldap_initialize() returned %d", ret);
+		free(uri);
+#else
+		TRACE(TRACE_DEBUG, 
+			"connecting to ldap server on [%s] : [%d] version [%d]",
+			_ldap_cfg.hostname, _ldap_cfg.port_int, _ldap_cfg.version_int);
+		_ldap_conn = ldap_init(_ldap_cfg.hostname, _ldap_cfg.port_int);
+#endif
 		break;
 	default:
 		TRACE(TRACE_ERROR, "Unsupported LDAP version requested [%d]. Defaulting to LDAP version 3.", _ldap_cfg.version_int);
