--- asterisk-1.6.0-beta5.orig/res/res_config_ldap.c	2008-02-15 19:29:08.000000000 +0200
+++ asterisk-1.6.0-beta5/res/res_config_ldap.c	2008-03-17 15:14:03.440700469 +0200
@@ -62,11 +62,11 @@
 AST_MUTEX_DEFINE_STATIC(ldap_lock);
 
 static LDAP *ldapConn;
-static char host[512];
+static char ldapUrl[512];
 static char user[512];
 static char pass[50];
 static char basedn[512];
-static int port = 389;
+static int protocol = LDAP_VERSION3;
 static time_t connect_time;
 
 static int parse_config(void);
@@ -1380,7 +1382,12 @@
 	struct ast_config *config;
 	struct ast_flags config_flags = {0};
 	const char *s;
+	char url[512];
+	char host[512];
+	int port;
 	char *category_name = NULL;
+	LDAPURLDesc urlDesc;
+	char *urlPtr;
 
 	config = ast_config_load(RES_CONFIG_LDAP_CONF, config_flags);
 
@@ -1401,12 +1408,39 @@
 	} else
 		ast_copy_string(pass, s, sizeof(pass));
 
-	if (!(s = ast_variable_retrieve(config, "_general", "host"))) {
-		ast_log(LOG_ERROR, "No directory host found.\n");
+	if ((s = ast_variable_retrieve(config, "_general", "url"))) {
+		ast_copy_string(url, s, strlen(s) + 1);
 		host[0] = '\0';
 	} else {
-		ast_copy_string(host, "ldap://", 8 );
-		ast_copy_string(host + 7, s, sizeof(host));
+		if ((s = ast_variable_retrieve(config, "_general", "host"))) {
+			ast_copy_string(host, s, strlen(s) + 1);
+		}
+		if (!(s = ast_variable_retrieve(config, "_general", "port"))) {
+			ast_log(LOG_WARNING, "No directory port found, using 389 as default.\n");
+			port = 389;
+		} else { 
+			port = atoi(s);
+		}
+		url[0] = '\0';
+	}
+	if (ast_strlen_zero(url) && !ast_strlen_zero(host)) {
+		memset( &urlDesc, 0, sizeof(urlDesc));
+		urlDesc.lud_scheme = "ldap";
+		urlDesc.lud_host = host;
+		urlDesc.lud_port = port;
+		urlDesc.lud_scope = LDAP_SCOPE_DEFAULT;
+		urlPtr = ldap_url_desc2str(&urlDesc);
+		ast_copy_string(url, urlPtr, strlen(urlPtr) + 1);
+		free(urlPtr);
+	}
+	if (ast_strlen_zero(url)) {
+		ast_log(LOG_ERROR, "No directory url or host found.\n");
+		ldapUrl[0] = '\0';
+	} else if (!ldap_is_ldap_url(url)) {
+		ast_log(LOG_ERROR, "LDAP URL %s is invalid.\n", url);
+		ldapUrl[0] = '\0';
+	} else {
+		ast_copy_string(ldapUrl, url, strlen(url) + 1);
 	}
 
 	if (!(s = ast_variable_retrieve(config, "_general", "basedn"))) {
@@ -1415,14 +1449,8 @@
 	} else 
 		ast_copy_string(basedn, s, sizeof(basedn));
 
-	if (!(s = ast_variable_retrieve(config, "_general", "port"))) {
-		ast_log(LOG_WARNING, "No directory port found, using 389 as default.\n");
-		port = 389;
-		ast_copy_string(host + strlen(host), ":389", sizeof(host));
-	} else { 
-		ast_copy_string(host + 1, ":", sizeof(s));
-		ast_copy_string(host + strlen(host), s, sizeof(s));
-		port = atoi(s);
+	if ((s = ast_variable_retrieve(config, "_general", "protocol"))) {
+		protocol = atoi(s);
 	}
 
 	table_configs_free();
@@ -1468,23 +1496,28 @@
 		return 1;
 	}
 
-	if (ast_strlen_zero(host)) {
+	if (ast_strlen_zero(ldapUrl)) {
 		ast_log(LOG_ERROR, "Not enough parameters to connect to ldap database\n");
 		return 0;
 	}
 
-	if (LDAP_SUCCESS != ldap_initialize(&ldapConn, host)) {
-		ast_log(LOG_ERROR, "Failed to init ldap connection to %s. Check debug for more info.\n", host);
+	if (LDAP_SUCCESS != ldap_initialize(&ldapConn, ldapUrl)) {
+		ast_log(LOG_ERROR, "Failed to init ldap connection to %s. Check debug for more info.\n", ldapUrl);
 		return 0;
 	} 
 
+	if (LDAP_SUCCESS != ldap_set_option(ldapConn, LDAP_OPT_PROTOCOL_VERSION, &protocol)) {
+		ast_log(LOG_ERROR, "Failed to set protocol version for ldap connection to %s. Check debug for more info.\n", ldapUrl);
+		return 0;
+	}
+
 	if (!ast_strlen_zero(user)) {
-		ast_debug(2, "bind to %s as %s\n", host, user);
+		ast_debug(2, "bind to %s as %s\n", ldapUrl, user);
 		cred.bv_val = (char *) pass;
 		cred.bv_len = strlen(pass);
 		bind_result = ldap_sasl_bind_s(ldapConn, user, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
 	} else {
-		ast_debug(2, "bind anonymously %s anonymously\n", host);
+		ast_debug(2, "bind anonymously %s anonymously\n", ldapUrl);
 		bind_result = ldap_sasl_bind_s(ldapConn, NULL, LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
 	}
 	if (bind_result == LDAP_SUCCESS) {
@@ -1518,8 +1551,8 @@
 	if (!ldapConn)
 		return CLI_FAILURE;
 
-	if (!ast_strlen_zero(host)) 
-		snprintf(status, sizeof(status), "Connected to %s, port %d baseDN %s", host, port, basedn);
+	if (!ast_strlen_zero(ldapUrl)) 
+		snprintf(status, sizeof(status), "Connected to %s, baseDN %s", ldapUrl, basedn);
 
 	if (!ast_strlen_zero(user))
 		snprintf(status2, sizeof(status2), " with username %s", user);
