--- asterisk_ldap-orig/res/res_config_ldap.c 2006-08-10 16:02:42.000000000 +0100 +++ asterisk_ldap/res/res_config_ldap.c 2006-08-10 16:03:32.000000000 +0100 @@ -21,8 +21,9 @@ * * \brief ldap plugin for portable configuration engine (ARA) * - * \author Mark Spencer * \author Manuel Guesdon + * \author Mark Spencer + * \author Gavin Henry * * \arg http://www.openldap.org */ @@ -57,11 +58,12 @@ AST_MUTEX_DEFINE_STATIC(ldap_lock); #define RES_CONFIG_LDAP_CONF "res_ldap.conf" static LDAP *ldapConn = NULL; -static char dbhost[512] = ""; -static char dbuser[512] = ""; -static char dbpass[50] = ""; -static char dbbasedn[512] = ""; -static int dbport = 389; +static char host[512] = ""; +static char dn[512] = ""; +static char pass[50] = ""; +static char basedn[512] = ""; +static int port = 389; +static int sizelimit = 100; static time_t connect_time = 0; static int parse_config(void); @@ -276,7 +278,7 @@ int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0; char **values = NULL; - values = ldap_get_values(ldapConn, ldap_entry, ldap_attribute_name); + values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name); if (values) { char **v = values; @@ -303,7 +305,7 @@ } v++; } - ldap_value_free(values); + ldap_value_free_len(values); } ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber); } @@ -335,17 +337,18 @@ 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); + result = ldap_search_ext_s(ldapConn, dn, LDAP_SCOPE_BASE, + "(objectclass=*)", NULL, 0, NULL, NULL, + NULL, sizelimit, &ldap_result); if (result < 0 && is_ldap_connect_error(result)) { ast_log(LOG_WARNING, - "Failed to query database. Try %d/3\n", + "Failed to query directory server. Try %d/3\n", tries + 1); tries++; if (tries < 3) { usleep(500000L * tries); if (ldapConn) { - ldap_unbind_s(ldapConn); + ldap_unbind_ext_s(ldapConn, NULL, NULL); ldapConn = NULL; } if (!ldap_reconnect()) @@ -356,7 +359,7 @@ if (result < 0) { ast_log(LOG_WARNING, - "Failed to query database. Check debug for more info.\n"); + "Failed to query directory server. Check debug for more info.\n"); if (option_debug > 1) { ast_log(LOG_DEBUG, "dn=%s\n", dn); ast_log(LOG_DEBUG, "Query Failed because: %s\n", @@ -591,17 +594,17 @@ do { - result = ldap_search_s(ldapConn, clean_basedn, - LDAP_SCOPE_SUBTREE, filter, NULL, 0, - &ldap_result); + result = ldap_search_ext_s(ldapConn, clean_basedn, + LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, + NULL, NULL, sizelimit, &ldap_result); if (result < 0 && is_ldap_connect_error(result)) { - ast_log(LOG_WARNING, "Failed to query database. Try %d/3\n", + ast_log(LOG_WARNING, "Failed to query directory server. Try %d/3\n", tries + 1); tries++; if (tries < 3) { usleep(500000L * tries); if (ldapConn) { - ldap_unbind_s(ldapConn); + ldap_unbind_ext_s(ldapConn, NULL, NULL); ldapConn = NULL; } if (!ldap_reconnect()) @@ -612,7 +615,7 @@ if (result < 0) { ast_log(LOG_WARNING, - "Failed to query database. Check debug for more info.\n"); + "Failed to query directory server. Check debug for more info.\n"); ast_log(LOG_WARNING, "Query: %s\n", filter); ast_log(LOG_WARNING, @@ -895,7 +898,7 @@ free(vars); free(categories); } else { - ast_log(LOG_WARNING, "Could not find config '%s' in database.\n", file); + ast_log(LOG_WARNING, "Could not find config '%s' in directory server.\n", file); } } return cfg; @@ -941,7 +944,7 @@ table_configs_free(); if (ldapConn) { - ldap_unbind_s(ldapConn); + ldap_unbind_ext_s(ldapConn, NULL, NULL); ldapConn = NULL; } ast_cli_unregister(&cli_realtime_ldap_status); @@ -963,7 +966,7 @@ ast_mutex_lock(&ldap_lock); if (ldapConn) { - ldap_unbind_s(ldapConn); + ldap_unbind_ext_s(ldapConn, NULL, NULL); ldapConn = NULL; } @@ -995,35 +998,35 @@ return -1; } - if (!(s = ast_variable_retrieve(config, "_general", "dbuser"))) { - ast_log(LOG_WARNING, "No database user found, anonymous binding as default.\n"); - dbuser[0] = '\0'; + if (!(s = ast_variable_retrieve(config, "_general", "dn"))) { + ast_log(LOG_WARNING, "No directory server dn found, anonymous binding as default.\n"); + dn[0] = '\0'; } else - ast_copy_string(dbuser, s, sizeof(dbuser)); + ast_copy_string(dn, s, sizeof(dn)); - 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); + if (!(s = ast_variable_retrieve(config, "_general", "pass"))) { + ast_log(LOG_WARNING, "No directory server password found, using 'asterisk' as default.\n"); + ast_copy_string(pass, "asterisk", sizeof(pass) - 1); } else - ast_copy_string(dbpass, s, sizeof(dbpass)); + ast_copy_string(pass, s, sizeof(pass)); - if (!(s = ast_variable_retrieve(config, "_general", "dbhost"))) { + if (!(s = ast_variable_retrieve(config, "_general", "host"))) { ast_log(LOG_ERROR, "No directory host found.\n"); - dbhost[0] = '\0'; + host[0] = '\0'; } else - ast_copy_string(dbhost, s, sizeof(dbhost)); + ast_copy_string(host, s, sizeof(host)); - if (!(s = ast_variable_retrieve(config, "_general", "dbbasedn"))) { + if (!(s = ast_variable_retrieve(config, "_general", "basedn"))) { ast_log(LOG_ERROR, "No LDAP base dn found, using 'asterisk' as default.\n"); - dbbasedn[0] = '\0'; + basedn[0] = '\0'; } else - ast_copy_string(dbbasedn, s, sizeof(dbbasedn)); + ast_copy_string(basedn, s, sizeof(basedn)); - if (!(s = ast_variable_retrieve(config, "_general", "dbport"))) { + if (!(s = ast_variable_retrieve(config, "_general", "port"))) { ast_log(LOG_WARNING, "No directory port found, using 389 as default.\n"); - dbport = 389; + port = 389; } else - dbport = atoi(s); + port = atoi(s); table_configs_free(); @@ -1060,11 +1063,11 @@ ast_config_destroy(config); if (option_debug > 3) { - ast_log(LOG_DEBUG, "LDAP RealTime Host: %s\n", dbhost); - ast_log(LOG_DEBUG, "LDAP RealTime Port: %i\n", dbport); - ast_log(LOG_DEBUG, "LDAP RealTime User: %s\n", dbuser); - ast_log(LOG_DEBUG, "LDAP RealTime Password: %s\n", dbpass); - ast_log(LOG_DEBUG, "LDAP RealTime BaseDN: %s\n", dbbasedn); + ast_log(LOG_DEBUG, "LDAP RealTime Host: %s\n", host); + ast_log(LOG_DEBUG, "LDAP RealTime Port: %i\n", port); + ast_log(LOG_DEBUG, "LDAP RealTime User: %s\n", dn); + ast_log(LOG_DEBUG, "LDAP RealTime Password: %s\n", pass); + ast_log(LOG_DEBUG, "LDAP RealTime BaseDN: %s\n", basedn); } return 1; } @@ -1090,33 +1093,35 @@ return 1; } - if (!dbhost) { - ast_log(LOG_ERROR, "Not enough parameters to connect to ldap database\n"); + if (!host) { + ast_log(LOG_ERROR, "Not enough parameters to connect to ldap directory server\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); + if (!(ldapConn = ldap_initialize(host, port))) { + ast_log(LOG_ERROR, "Failed to init ldap connection to %s. Check debug for more info.\n", host); return 0; } - if (dbuser && *dbuser) { + if (dn && *dn) { 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); + ast_log(LOG_DEBUG, "bind to %s as %s\n", host, dn); + bind_result = ldap_sasl_bind_s(ldapConn, dn, NULL, pass, NULL, NULL, + NULL); } else { if (option_debug > 1) - ast_log(LOG_DEBUG, "bind anonymously %s anonymously\n", dbhost); - bind_result = ldap_simple_bind_s(ldapConn, NULL, NULL); + ast_log(LOG_DEBUG, "bind anonymously %s anonymously\n", host); + bind_result = ldap_sasl_bind_s(ldapConn, NULL, NULL, NULL, NULL, NULL, + NULL); } if (bind_result == LDAP_SUCCESS) { if (option_debug > 1) - ast_log(LOG_DEBUG, "Successfully connected to database.\n"); + ast_log(LOG_DEBUG, "Successfully connected to directory server.\n"); connect_time = time(NULL); return 1; } else { ast_log(LOG_WARNING, "bind failed: %s\n", ldap_err2string(bind_result)); - ldap_unbind(ldapConn); + ldap_unbind_ext_s(ldapConn, NULL, NULL); ldapConn = NULL; return 0; } @@ -1129,11 +1134,11 @@ if (!ldapConn) return RESULT_FAILURE; - if (dbhost) - snprintf(status, 255, "Connected to %s, port %d baseDN %s", dbhost, dbport, dbbasedn); + if (host) + snprintf(status, 255, "Connected to %s, port %d baseDN %s", host, port, basedn); - if (dbuser && *dbuser) - snprintf(status2, 99, " with username %s", dbuser); + if (dn && *dn) + snprintf(status2, 99, " with dn %s", dn); if (ctime > 31536000) { ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",