Index: res/res_config_ldap.c =================================================================== --- res/res_config_ldap.c (revision 32675) +++ res/res_config_ldap.c (working copy) @@ -131,11 +131,11 @@ char *p = strstr(start, "=>"); if (option_debug) - ast_log(LOG_DEBUG, "LDAP RealTime: Add attribute: start: %s\n", start); + ast_log(LOG_DEBUG, "Add attribute: start: %s\n", start); if (!p) { ast_log(LOG_WARNING, - "LDAP RealTime: Missing '=>' in attribute: %s in %s\n", + "Missing '=>' in attribute: %s in %s\n", attribute_string, table_config->table_name); } else { char *value = p + 2; //skip => @@ -156,22 +156,22 @@ } if (*start == '\0') { ast_log(LOG_WARNING, - "LDAP RealTime: Empty variable name in attribute: %s in %s\n", + "Empty variable name in attribute: %s in %s\n", attribute_string, table_config->table_name); } else if (*value == '\0') { ast_log(LOG_WARNING, - "LDAP RealTime: Empty ldap attribute name in attribute: %s in %s\n", + "Empty ldap attribute name in attribute: %s in %s\n", attribute_string, table_config->table_name); } else { struct ast_variable *var = ast_variable_new(start, value); if (option_debug > 2) - ast_log(LOG_DEBUG, "LDAP RealTime: Add attribute: VAR %s => %s\n",var->name,var->value); + ast_log(LOG_DEBUG, "Add attribute: VAR %s => %s\n",var->name,var->value); if (table_config->attributes) var->next = table_config->attributes; table_config->attributes = var; if (option_debug > 2) - ast_log(LOG_DEBUG, "LDAP RealTime (%d): Added attribute in %s: %s -> %s\n", - __LINE__, table_config->table_name, start, value); + ast_log(LOG_DEBUG, "Added attribute in %s: %s -> %s\n", + table_config->table_name, start, value); } } free(string); @@ -283,17 +283,14 @@ while (*v) { char *value = *v; if (option_debug > 1) - ast_log(LOG_DEBUG, - "LDAP RealTime (%d): attribute_name: %s value: %s\n", - __LINE__, attribute_name, value); + ast_log(LOG_DEBUG, "attribute_name: %s value: %s\n", attribute_name, value); if (is_realmed_password_attribute) { if (strncasecmp(value, "{md5}", 5) == 0) value += 5; else value = NULL; if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime (%d): md5: %s\n", - __LINE__, value); + ast_log(LOG_DEBUG, "md5: %s\n", value); } if (value) { if (prev) { @@ -326,7 +323,7 @@ const char *dn) { if (!table_config) { - ast_log(LOG_ERROR, "LDAP RealTime: No table config\n"); + ast_log(LOG_ERROR, "No table config\n"); return NULL; } else { struct ast_variable *var = NULL; @@ -335,15 +332,14 @@ int tries = 0; if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime (%d): ldap_loadentry dn=%s\n", - __LINE__, dn); + ast_log(LOG_DEBUG, "ldap_loadentry dn=%s\n", dn); do { result = ldap_search_s(ldapConn, dn, LDAP_SCOPE_BASE, "(objectclass=*)", NULL, 0, &ldap_result); if (result < 0 && is_ldap_connect_error(result)) { ast_log(LOG_WARNING, - "LDAP RealTime: Failed to query database. Try %d/3\n", + "Failed to query database. Try %d/3\n", tries + 1); tries++; if (tries < 3) { @@ -360,10 +356,10 @@ if (result < 0) { ast_log(LOG_WARNING, - "LDAP RealTime: Failed to query database. Check debug for more info.\n"); + "Failed to query database. Check debug for more info.\n"); if (option_debug > 1) { - ast_log(LOG_DEBUG, "LDAP RealTime: dn=%s\n", dn); - ast_log(LOG_DEBUG, "LDAP RealTime: Query Failed because: %s\n", + ast_log(LOG_DEBUG, "dn=%s\n", dn); + ast_log(LOG_DEBUG, "Query Failed because: %s\n", ldap_err2string(result)); } ast_mutex_unlock(&ldap_lock); @@ -373,15 +369,14 @@ if ((num_entry = ldap_count_entries(ldapConn, ldap_result)) > 0) { LDAPMessage *ldap_entry = NULL; if (option_debug) - ast_log(LOG_DEBUG, "LDAP RealTime (%d): num_entry: %d\n", - __LINE__, num_entry); + ast_log(LOG_DEBUG, "num_entry: %d\n", num_entry); ldap_entry = ldap_first_entry(ldapConn, ldap_result); var = realtime_ldap_entry_to_var(table_config, ldap_entry); if (num_entry > 1) - ast_log(LOG_WARNING, "LDAP RealTime: More than one entry for dn=%s. Take only 1st one\n", dn); + ast_log(LOG_WARNING, "More than one entry for dn=%s. Take only 1st one\n", dn); } else { - ast_log(LOG_WARNING, "LDAP RealTime: Could not find any entry dn=%s.\n", dn); + ast_log(LOG_WARNING, "Could not find any entry dn=%s.\n", dn); } } ldap_msgfree(ldap_result); @@ -401,7 +396,7 @@ pbx_substitute_variables_helper(channel, string, ret_string, MAXRESULT - 1); } if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime: substituted: string: '%s' => '%s' \n", + ast_log(LOG_DEBUG, "substituted: string: '%s' => '%s' \n", string, ret_string); return ret_string; } @@ -430,8 +425,7 @@ } } if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime (%d): basedn: '%s' => '%s' \n", - __LINE__, basedn, cbasedn); + ast_log(LOG_DEBUG, "basedn: '%s' => '%s' \n", basedn, cbasedn); return cbasedn; } @@ -497,7 +491,7 @@ char *new_value = NULL; char *wsPos = strstr(name, " LIKE"); if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime: name='%s' value='%s'\n", name, value); + ast_log(LOG_DEBUG, "name='%s' value='%s'\n", name, value); if (wsPos) { new_name = strndup(name, wsPos - name); name = new_name; @@ -535,10 +529,9 @@ if (option_debug > 1) ast_log(LOG_DEBUG, - "LDAP RealTime (%d): realtime_ldap_base: basedn: %s table_name: %s\n", - __LINE__, basedn, table_name); + "realtime_ldap_base: basedn: %s table_name: %s\n", basedn, table_name); if (!table_name) { - ast_log(LOG_WARNING, "LDAP RealTime: No table_name specified.\n"); + ast_log(LOG_WARNING, "No table_name specified.\n"); return NULL; } @@ -547,7 +540,7 @@ newval = va_arg(ap, const char *); if (!newparam || !newval) { ast_log(LOG_WARNING, - "LDAP RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n"); + "Realtime retrieval requires at least 1 parameter and 1 value to search on.\n"); } else { ast_mutex_lock(&ldap_lock); @@ -558,7 +551,7 @@ table_config = table_config_for_table_name(table_name); if (!table_config) { ast_log(LOG_WARNING, - "LDAP RealTime: No table named '%s'.\n", + "No table named '%s'.\n", table_name); } else { char *clean_basedn = cleaned_basedn(NULL, basedn); @@ -594,7 +587,7 @@ append_string_to_filter(&filter, &filter_size, ")"); if (option_debug) - ast_log(LOG_DEBUG, "LDAP RealTime (%d): filter: %s\n", __LINE__, filter); + ast_log(LOG_DEBUG, "filter: %s\n", filter); do { @@ -602,7 +595,7 @@ LDAP_SCOPE_SUBTREE, filter, NULL, 0, &ldap_result); if (result < 0 && is_ldap_connect_error(result)) { - ast_log(LOG_WARNING, "LDAP RealTime: Failed to query database. Try %d/3\n", + ast_log(LOG_WARNING, "Failed to query database. Try %d/3\n", tries + 1); tries++; if (tries < 3) { @@ -619,11 +612,11 @@ if (result < 0) { ast_log(LOG_WARNING, - "LDAP RealTime: Failed to query database. Check debug for more info.\n"); - ast_log(LOG_WARNING, "LDAP RealTime: Query: %s\n", + "Failed to query database. Check debug for more info.\n"); + ast_log(LOG_WARNING, "Query: %s\n", filter); ast_log(LOG_WARNING, - "LDAP RealTime: Query Failed because: %s\n", + "Query Failed because: %s\n", ldap_err2string(result)); } else { int num_entry = 0; @@ -634,7 +627,7 @@ *entries_count_ptr = num_entry; vars = ast_calloc(1, sizeof(struct ast_variable *) *(num_entry + 1)); if (option_debug) - ast_log(LOG_DEBUG, "LDAP RealTime (%d): num_entry: %d\n", __LINE__, num_entry); + ast_log(LOG_DEBUG, "num_entry: %d\n", num_entry); ldap_entry = ldap_first_entry(ldapConn, ldap_result); @@ -643,7 +636,7 @@ ldap_entry = ldap_next_entry(ldapConn, ldap_entry); } } else { - ast_log(LOG_WARNING, "LDAP RealTime: Could not find any entry matching %s in base dn %s.\n", + ast_log(LOG_WARNING, "Could not find any entry matching %s in base dn %s.\n", filter, clean_basedn); } @@ -801,11 +794,9 @@ const char *file, struct ast_config *cfg) { - ast_log(LOG_DEBUG, - "LDAP RealTime (%d): config_ldap: basedn: %s table_name: %s\n", - __LINE__, basedn, table_name); + ast_log(LOG_DEBUG, "config_ldap: basedn: %s table_name: %s\n", basedn, table_name); if (!file || !strcmp(file, RES_CONFIG_LDAP_CONF)) { - ast_log(LOG_WARNING, "LDAP RealTime: Cannot configure myself.\n"); + ast_log(LOG_WARNING, "Cannot configure myself.\n"); } else { unsigned int vars_count = 0; struct ast_variable **vars = @@ -834,35 +825,35 @@ struct ast_variable *var_val = variable_named(*p, "variable_value"); if (option_debug) { - ast_log(LOG_DEBUG, "LDAP RealTime: category: %s\n", + ast_log(LOG_DEBUG, "category: %s\n", category->value); - ast_log(LOG_DEBUG, "LDAP RealTime: var_name: %s\n", + ast_log(LOG_DEBUG, "var_name: %s\n", var_name->value); - ast_log(LOG_DEBUG, "LDAP RealTime: var_val: %s\n", + ast_log(LOG_DEBUG, "var_val: %s\n", var_val->value); - ast_log(LOG_DEBUG, "LDAP RealTime: cat_metric: %s\n", + ast_log(LOG_DEBUG, "cat_metric: %s\n", cat_metric->value); } if (!category) { struct ast_variable *dn = variable_named(*p, "dn"); ast_log(LOG_ERROR, - "LDAP RealTime: No category name in entry '%s' for file '%s'.\n", + "No category name in entry '%s' for file '%s'.\n", (dn ? dn->value : "?"), file); } else if (!cat_metric) { struct ast_variable *dn = variable_named(*p, "dn"); ast_log(LOG_ERROR, - "LDAP RealTime: No category metric in entry '%s'(category: %s) for file '%s'.\n", + "No category metric in entry '%s'(category: %s) for file '%s'.\n", (dn ? dn->value : "?"), category->value, file); } else if (!var_name) { struct ast_variable *dn = variable_named(*p, "dn"); ast_log(LOG_ERROR, - "LDAP RealTime: No variable name in entry '%s' (category: %s metric: %s) for file '%s'.\n", + "No variable name in entry '%s' (category: %s metric: %s) for file '%s'.\n", (dn ? dn->value : "?"), category->value, cat_metric->value, file); } else if (!var_val) { struct ast_variable *dn = variable_named(*p, "dn"); ast_log(LOG_ERROR, - "LDAP RealTime: No variable value in entry '%s' (category: %s metric: %s variable: %s) for file '%s'.\n", + "No variable value in entry '%s' (category: %s metric: %s variable: %s) for file '%s'.\n", (dn ? dn->value : "?"), category->value, cat_metric->value, var_name->value, file); } else { @@ -903,7 +894,7 @@ free(vars); free(categories); } else { - ast_log(LOG_WARNING, "LDAP RealTime: Could not find config '%s' in database.\n", file); + ast_log(LOG_WARNING, "Could not find config '%s' in database.\n", file); } } return cfg; @@ -920,12 +911,16 @@ static int load_module(void *mod) { - parse_config(); + + if (parse_config() < 0) { + ast_log(LOG_NOTICE, "Cannot load LDAP RealTime driver.\n"); + return 0; + } ast_mutex_lock(&ldap_lock); if (!ldap_reconnect()) - ast_log(LOG_WARNING, "LDAP RealTime: Couldn't establish connection. Check debug.\n"); + ast_log(LOG_WARNING, "Couldn't establish connection. Check debug.\n"); ast_config_engine_register(&ldap_engine); if (option_verbose) @@ -970,10 +965,14 @@ ldap_unbind_s(ldapConn); ldapConn = NULL; } - parse_config(); + + if (parse_config() < 0) { + ast_log(LOG_NOTICE, "Cannot reload LDAP RealTime driver.\n"); + return 0; + } if (!ldap_reconnect()) - ast_log(LOG_WARNING, "LDAP RealTime: Couldn't establish connection. Check debug.\n"); + ast_log(LOG_WARNING, "Couldn't establish connection. Check debug.\n"); ast_verbose(VERBOSE_PREFIX_2 "LDAP RealTime reloaded.\n"); @@ -990,70 +989,74 @@ config = ast_config_load(RES_CONFIG_LDAP_CONF); - if (config) { - if (!(s = ast_variable_retrieve(config, "_general", "dbuser"))) { - ast_log(LOG_WARNING, "LDAP RealTime: No database user found, anonymous binding as default.\n"); - dbuser[0] = '\0'; - } else - strncpy(dbuser, s, sizeof(dbuser) - 1); + if (!config) { + ast_log(LOG_WARNING, "Cannot load configuration %s\n", RES_CONFIG_LDAP_CONF); + return -1; + } - if (!(s = ast_variable_retrieve(config, "_general", "dbpass"))) { - ast_log(LOG_WARNING, "LDAP RealTime: No database password found, using 'asterisk' as default.\n"); - strncpy(dbpass, "asterisk", sizeof(dbpass) - 1); - } else - strncpy(dbpass, s, sizeof(dbpass) - 1); - - if (!(s = ast_variable_retrieve(config, "_general", "dbhost"))) { - ast_log(LOG_ERROR, "LDAP RealTime: No directory host found.\n"); - dbhost[0] = '\0'; - } else - strncpy(dbhost, s, sizeof(dbhost) - 1); - - if (!(s = ast_variable_retrieve(config, "_general", "dbbasedn"))) { - ast_log(LOG_ERROR, "LDAP RealTime: No LDAP base dn found, using 'asterisk' as default.\n"); - dbbasedn[0] = '\0'; - } else - strncpy(dbbasedn, s, sizeof(dbbasedn) - 1); - - if (!(s = ast_variable_retrieve(config, "_general", "dbport"))) { - ast_log(LOG_WARNING, "LDAP RealTime: No directory port found, using 389 as default.\n"); - dbport = 389; - } else - dbport = atoi(s); - - table_configs_free(); - - char *category_name = NULL; - while ((category_name = ast_category_browse(config, category_name))) { - int is_general = (strcasecmp(category_name, "_general") == 0); - struct ast_variable *var = ast_variable_browse(config, category_name); - - if (option_debug) - ast_log(LOG_DEBUG, "LDAP RealTime: found: category_name=%s\n", category_name); - if (var) { - struct ldap_table_config *table_config = - table_config_for_table_name(category_name); - if (!table_config) { - table_config = table_config_new(category_name); - if (table_configs) - table_config->next = table_configs; - table_configs = table_config; - if (is_general) - base_table_config = table_config; - } - while (var) { - if (option_debug) - ast_log(LOG_DEBUG, "LDAP RealTime: found: category_name=%s var->name=%s var->value=%s\n", - category_name, var->name, var->value); - if (strcasecmp(var->name, "attribute") == 0) - ldap_table_config_add_attribute(table_config, var->value); - else if (strcasecmp(var->name, "additionalFilter") == 0) - table_config->additional_filter = strdup(var->value); - var = var->next; - } + if (!(s = ast_variable_retrieve(config, "_general", "dbuser"))) { + ast_log(LOG_WARNING, "No database user found, anonymous binding as default.\n"); + dbuser[0] = '\0'; + } else + ast_copy_string(dbuser, s, sizeof(dbuser)); + + if (!(s = ast_variable_retrieve(config, "_general", "dbpass"))) { + ast_log(LOG_WARNING, "No database password found, using 'asterisk' as default.\n"); + ast_copy_string(dbpass, "asterisk", sizeof(dbpass) - 1); + } else + ast_copy_string(dbpass, s, sizeof(dbpass)); + + if (!(s = ast_variable_retrieve(config, "_general", "dbhost"))) { + ast_log(LOG_ERROR, "No directory host found.\n"); + dbhost[0] = '\0'; + } else + ast_copy_string(dbhost, s, sizeof(dbhost)); + + if (!(s = ast_variable_retrieve(config, "_general", "dbbasedn"))) { + ast_log(LOG_ERROR, "No LDAP base dn found, using 'asterisk' as default.\n"); + dbbasedn[0] = '\0'; + } else + ast_copy_string(dbbasedn, s, sizeof(dbbasedn)); + + if (!(s = ast_variable_retrieve(config, "_general", "dbport"))) { + ast_log(LOG_WARNING, "No directory port found, using 389 as default.\n"); + dbport = 389; + } else + dbport = atoi(s); + + table_configs_free(); + + char *category_name = NULL; + while ((category_name = ast_category_browse(config, category_name))) { + int is_general = (strcasecmp(category_name, "_general") == 0); + struct ast_variable *var = ast_variable_browse(config, category_name); + + if (option_debug) + ast_log(LOG_DEBUG, "found: category_name=%s\n", category_name); + if (var) { + struct ldap_table_config *table_config = + table_config_for_table_name(category_name); + if (!table_config) { + table_config = table_config_new(category_name); + if (table_configs) + table_config->next = table_configs; + table_configs = table_config; + if (is_general) + base_table_config = table_config; } + while (var) { + if (option_debug) + ast_log(LOG_DEBUG, "found: category_name=%s var->name=%s var->value=%s\n", + category_name, var->name, var->value); + if (strcasecmp(var->name, "attribute") == 0) + ldap_table_config_add_attribute(table_config, var->value); + else if (strcasecmp(var->name, "additionalFilter") == 0) + table_config->additional_filter = strdup(var->value); + var = var->next; + } } } + ast_config_destroy(config); if (option_debug > 3) { ast_log(LOG_DEBUG, "LDAP RealTime Host: %s\n", dbhost); @@ -1078,42 +1081,43 @@ static int ldap_reconnect(void) { /* mutex lock should have been locked before calling this function. */ + int bind_result = 0; - if (!ldapConn) { - if (!dbhost) { - ast_log(LOG_ERROR, "LDAP RealTime: Not enough parameters to connect to ldap database\n"); - } else { - if (!(ldapConn = ldap_init(dbhost, dbport))) { - ast_log(LOG_ERROR, "LDAP RealTime: Failed to init ldap connection to %s. Check debug for more info.\n", dbhost); - return 0; - } else { - int bind_result = 0; - if (dbuser && *dbuser) { - if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime: bind to %s as %s\n", dbhost, dbuser); - bind_result = ldap_simple_bind_s(ldapConn, dbuser, dbpass); - } else { - if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime: bind anonymously %s anonymously\n", dbhost); - bind_result = ldap_simple_bind_s(ldapConn, NULL, NULL); - } - if (bind_result == LDAP_SUCCESS) { - if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime: Successfully connected to database.\n"); - connect_time = time(NULL); - return 1; - } else { - ast_log(LOG_WARNING, "LDAP RealTime: bind failed: %s\n", ldap_err2string(bind_result)); - ldap_unbind(ldapConn); - ldapConn = NULL; - return 0; - } - } - } + if (ldapConn) { + if (option_debug > 1) + ast_log(LOG_DEBUG, "Everything seems fine.\n"); + return 1; + } + + if (!dbhost) { + ast_log(LOG_ERROR, "Not enough parameters to connect to ldap database\n"); + return 0; + } + + if (!(ldapConn = ldap_init(dbhost, dbport))) { + ast_log(LOG_ERROR, "Failed to init ldap connection to %s. Check debug for more info.\n", dbhost); + return 0; + } + + if (dbuser && *dbuser) { + if (option_debug > 1) + ast_log(LOG_DEBUG, "bind to %s as %s\n", dbhost, dbuser); + bind_result = ldap_simple_bind_s(ldapConn, dbuser, dbpass); } else { if (option_debug > 1) - ast_log(LOG_DEBUG, "LDAP RealTime: Everything seems fine.\n"); + ast_log(LOG_DEBUG, "bind anonymously %s anonymously\n", dbhost); + bind_result = ldap_simple_bind_s(ldapConn, NULL, NULL); + } + if (bind_result == LDAP_SUCCESS) { + if (option_debug > 1) + ast_log(LOG_DEBUG, "Successfully connected to database.\n"); + connect_time = time(NULL); return 1; + } else { + ast_log(LOG_WARNING, "bind failed: %s\n", ldap_err2string(bind_result)); + ldap_unbind(ldapConn); + ldapConn = NULL; + return 0; } }