diff --git a/configs/res_ldap.conf.sample b/configs/res_ldap.conf.sample index 8477ec59011d7e79368f30f55e1794b25dd394d2..f64d6c58b14ef6023ca2c99ce267c275d8fd3ccb 100644 --- a/configs/res_ldap.conf.sample +++ b/configs/res_ldap.conf.sample @@ -3,7 +3,7 @@ ; ; Sample Asterisk config file for res_config_ldap -; in extconfig.conf you can use it like this: +; in extconfig.conf; you can use it like this: ; sipusers = ldap,"dc=myDomain,dc=myDomainExt",sip ; sippeers = ldap,"dc=myDomain,dc=myDomainExt",sip ; extensions = ldap,"dc=myDomain,dc=myDomainExt",extensions @@ -11,20 +11,29 @@ [_general] -;host=192.168.1.1,ldap.mydomain.com ; LDAP host(s) -;protocol=3 ; Version of the LDAP protocol to use default is 3. -;basedn=MyRootDN ; Base DN -;pass=MyPassword ; Bind password -;user=MyDN ; Bind DN +; +; Specify one of either host and port OR url. URL is preferred, as you can +; use more options. +;host=192.168.1.1 ; LDAP host +;port=389 +;url=ldap://ldap3.mydomain.com:3890 +;protocol=3 ; Version of the LDAP protocol to use; default is 3. +;basedn=MyRootDN ; Base DN +;user=MyDN ; Bind DN +;pass=MyPassword ; Bind password ; Configuration Table [config] -; addtional filter - This specifies an additional set of criteria to be used +; +; additionalFilter - This specifies an additional set of criteria to be used ; when querying the LDAP server. +; additionalFilter=(objectClass=PBXConfig) +; ; Attributes mapping (asterisk variable name = ldap attribute name) ; When Asterisk requests the variable by the name of the value on the left, ; this module will look up the attribute listed on the right. +; filename = PBXConfigFilename category = PBXConfigCategory variable_name = PBXConfigVariableName @@ -32,7 +41,9 @@ variable_value = PBXConfigVariableValue cat_metric = PBXConfigCategoryMetric commented = PBXConfigCommented +; ; Extensions Table +; [extensions] context = PBXExtensionContext exten = PBXExtensionExten @@ -41,7 +52,9 @@ app = PBXExtensionApplication appdata = PBXExtensionApplicationData additionalFilter=(objectClass=PBXExtension) +; ; Sip Users Table +; [sip] name = uid amaflags = PBXAccountAMAFlags @@ -77,7 +90,9 @@ regexten = PBXAccountRegistrationExten CanCallForward = PBXAccountCanCallForward additionalFilter=(objectClass=PBXAccountSIP) +; ; IAX Users Table +; [iax] amaflags = PBXAccountAMAFlags callerid = PBXAccountCallerID @@ -100,7 +115,9 @@ regexten = PBXAccountRegistrationExten notransfer = PBXAccountNoTransfer additionalFilter=(objectClass=PBXAccountIAX) +; ; A Test Family +; [testfamily] MyUSERID = uid additionalFilter=(objectClass=*) diff --git a/res/res_config_ldap.c b/res/res_config_ldap.c index 4cc97a764a47a40c747da8c768677cd9c3bcf9b5..79a326b20ce98c696f8413f39340aaefa508b293 100644 --- a/res/res_config_ldap.c +++ b/res/res_config_ldap.c @@ -62,11 +62,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") AST_MUTEX_DEFINE_STATIC(ldap_lock); static LDAP *ldapConn; -static char host[512]; +static char url[512]; static char user[512]; static char pass[50]; static char basedn[512]; -static int port = 389; static int version = 3; static time_t connect_time; @@ -1383,7 +1382,8 @@ int parse_config(void) { struct ast_config *config; struct ast_flags config_flags = {0}; - const char *s; + const char *s, *host; + int port; char *category_name = NULL; config = ast_config_load(RES_CONFIG_LDAP_CONF, config_flags); @@ -1405,12 +1405,20 @@ int parse_config(void) } 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"); - host[0] = '\0'; + /* URL is preferred, use host and port if not found */ + if ((s = ast_variable_retrieve(config, "_general", "url"))) { + ast_copy_string(url, s, sizeof(url)); + } else if ((host = ast_variable_retrieve(config, "_general", "host"))) { + if (!(s = ast_variable_retrieve(config, "_general", "port")) || sscanf(s, "%d", &port) != 1) { + ast_log(LOG_NOTICE, "No directory port found, using 389 as default.\n"); + port = 389; + } + + snprintf(url, sizeof(url), "ldap://%s:%d", host, port); } else { - ast_copy_string(host, "ldap://", 8 ); - ast_copy_string(host + 7, s, sizeof(host) - 7); + ast_log(LOG_ERROR, "No directory URL or host found.\n"); + ast_config_destroy(config); + return -1; } if (!(s = ast_variable_retrieve(config, "_general", "basedn"))) { @@ -1419,11 +1427,6 @@ int parse_config(void) } else ast_copy_string(basedn, s, sizeof(basedn)); - if (!(s = ast_variable_retrieve(config, "_general", "port")) || sscanf(s, "%d", &port) != 1) { - ast_log(LOG_WARNING, "No directory port found, using 389 as default.\n"); - port = 389; - } - if (!(s = ast_variable_retrieve(config, "_general", "version")) || !(s = ast_variable_retrieve(config, "_general", "protocol"))) { ast_log(LOG_NOTICE, "No explicit LDAP version found, using 3 as default.\n"); version = 3; @@ -1475,13 +1478,13 @@ static int ldap_reconnect(void) return 1; } - if (ast_strlen_zero(host)) { + if (ast_strlen_zero(url)) { ast_log(LOG_ERROR, "Not enough parameters to connect to ldap database\n"); return 0; } - if (!(ldapConn = ldap_open(host, port))) { - ast_log(LOG_ERROR, "Failed to init ldap connection to %s, port %d. Check debug for more info.\n", host, port); + if (LDAP_SUCCESS != ldap_initialize(&ldapConn, url)) { + ast_log(LOG_ERROR, "Failed to init ldap connection to '%s'. Check debug for more info.\n", url); return 0; } @@ -1490,12 +1493,12 @@ static int ldap_reconnect(void) } if (!ast_strlen_zero(user)) { - ast_debug(2, "bind to %s:%d as %s\n", host, port, user); + ast_debug(2, "bind to '%s' as user '%s'\n", url, 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 %s anonymously\n", url); bind_result = ldap_sasl_bind_s(ldapConn, NULL, LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL); } if (bind_result == LDAP_SUCCESS) { @@ -1529,8 +1532,8 @@ static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_c 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(url)) + snprintf(status, sizeof(status), "Connected to '%s', baseDN %s", url, basedn); if (!ast_strlen_zero(user)) snprintf(status2, sizeof(status2), " with username %s", user);