Merge "rtp_engine/res_rtp_asterisk: Fix RTP struct reentrancy crashes."
[asterisk/asterisk.git] / res / res_config_ldap.c
index b44adf4..8f24a8d 100644 (file)
@@ -53,8 +53,6 @@
 #include <stdio.h>
 #include <ldap.h>
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/channel.h"
 #include "asterisk/logger.h"
 #include "asterisk/config.h"
@@ -124,7 +122,7 @@ static struct ldap_table_config *table_config_new(const char *table_name)
 
        if (table_name) {
                if (!(p->table_name = ast_strdup(table_name))) {
-                       free(p);
+                       ast_free(p);
                        return NULL;
                }
        }
@@ -192,7 +190,7 @@ static int semicolon_count_var(struct ast_variable *var)
                return 0;
        }
 
-       ast_debug(2, "LINE(%d) semicolon_count_var: %s\n", __LINE__, var_value->value);
+       ast_debug(2, "semicolon_count_var: %s\n", var_value->value);
 
        return semicolon_count_str(var_value->value);
 }
@@ -238,7 +236,7 @@ static void table_configs_free(void)
                if (c->attributes) {
                        ast_variables_destroy(c->attributes);
                }
-               free(c);
+               ast_free(c);
        }
 
        base_table_config = NULL;
@@ -332,7 +330,7 @@ static struct ast_variable *realtime_ldap_entry_to_var(struct ldap_table_config
                        for (v = values; *v; v++) {
                                value = *v;
                                valptr = value->bv_val;
-                               ast_debug(2, "LINE(%d) attribute_name: %s LDAP value: %s\n", __LINE__, attribute_name, valptr);
+                               ast_debug(2, "attribute_name: %s LDAP value: %s\n", attribute_name, valptr);
                                if (is_realmed_password_attribute) {
                                        if (!strncasecmp(valptr, "{md5}", 5)) {
                                                valptr += 5;
@@ -424,7 +422,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
         * value in \a variable_value; otherwise, we keep \a vars static and increase the length of the linked list of variables in the array element.
         * This memory must be freed outside of this function.
         */
-       vars = ast_calloc(sizeof(struct ast_variable *), tot_count + 1);
+       vars = ast_calloc(tot_count + 1, sizeof(struct ast_variable *));
 
        ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
 
@@ -469,7 +467,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                delim_value = ast_strdup(valptr);
 
                                                                if ((delim_tot_count = semicolon_count_str(delim_value)) > 0) {
-                                                                       ast_debug(4, "LINE(%d) is delimited %d times: %s\n", __LINE__, delim_tot_count, delim_value);
+                                                                       ast_debug(4, "is delimited %d times: %s\n", delim_tot_count, delim_value);
                                                                        is_delimited = 1;
                                                                }
                                                        }
@@ -479,11 +477,11 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                /* for non-Static RealTime, first */
 
                                                                for (i = pos; !ast_strlen_zero(valptr + i); i++) {
-                                                                       ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
+                                                                       ast_debug(4, "DELIM pos: %d i: %d\n", pos, i);
                                                                        if (delim_value[i] == ';') {
                                                                                delim_value[i] = '\0';
 
-                                                                               ast_debug(2, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
+                                                                               ast_debug(2, "DELIM - attribute_name: %s value: %s pos: %d\n", attribute_name, &delim_value[pos], pos);
 
                                                                                if (prev) {
                                                                                        prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
@@ -501,9 +499,9 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                        }
                                                                }
                                                                if (ast_strlen_zero(valptr + i)) {
-                                                                       ast_debug(4, "LINE(%d) DELIM pos: %d i: %d delim_count: %d\n", __LINE__, pos, i, delim_count);
+                                                                       ast_debug(4, "DELIM pos: %d i: %d delim_count: %d\n", pos, i, delim_count);
                                                                        /* Last delimited value */
-                                                                       ast_debug(4, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
+                                                                       ast_debug(4, "DELIM - attribute_name: %s value: %s pos: %d\n", attribute_name, &delim_value[pos], pos);
                                                                        if (prev) {
                                                                                prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
                                                                                if (prev->next) {
@@ -516,17 +514,17 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                        is_delimited = 0;
                                                                        pos = 0;
                                                                }
-                                                               free(delim_value);
+                                                               ast_free(delim_value);
                                                                delim_value = NULL;
 
-                                                               ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
+                                                               ast_debug(4, "DELIM pos: %d i: %d\n", pos, i);
                                                        } else {
                                                                /* not delimited */
                                                                if (delim_value) {
-                                                                       free(delim_value);
+                                                                       ast_free(delim_value);
                                                                        delim_value = NULL;
                                                                }
-                                                               ast_debug(2, "LINE(%d) attribute_name: %s value: %s\n", __LINE__, attribute_name, valptr);
+                                                               ast_debug(2, "attribute_name: %s value: %s\n", attribute_name, valptr);
 
                                                                if (prev) {
                                                                        prev->next = ast_variable_new(attribute_name, valptr, table_config->table_name);
@@ -550,7 +548,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                        const struct ast_variable *tmpdebug = variable_named(var, "variable_name");
                                        const struct ast_variable *tmpdebug2 = variable_named(var, "variable_value");
                                        if (tmpdebug && tmpdebug2) {
-                                               ast_debug(3, "LINE(%d) Added to vars - %s = %s\n", __LINE__, tmpdebug->value, tmpdebug2->value);
+                                               ast_debug(3, "Added to vars - %s = %s\n", tmpdebug->value, tmpdebug2->value);
                                        }
                                }
                                vars[entry_index++] = var;
@@ -561,7 +559,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                } while (delim_count <= delim_tot_count && static_table_config == table_config);
 
                if (static_table_config != table_config) {
-                       ast_debug(3, "LINE(%d) Added to vars - non static\n", __LINE__);
+                       ast_debug(3, "Added to vars - non static\n");
 
                        vars[entry_index++] = var;
                        prev = NULL;
@@ -756,20 +754,60 @@ static void append_var_and_value_to_filter(struct ast_str **filter,
        ast_str_append(filter, 0, "(%s=%s)", name, value);
 }
 
+/*!
+ * \internal
+ * \brief Create an LDAP filter using search fields
+ *
+ * \param config the \c ldap_table_config for this search
+ * \param fields the \c ast_variable criteria to include
+ *
+ * \returns an \c ast_str pointer on success, NULL otherwise.
+ */
+static struct ast_str *create_lookup_filter(struct ldap_table_config *config, const struct ast_variable *fields)
+{
+       struct ast_str *filter;
+       const struct ast_variable *field;
+
+       filter = ast_str_create(80);
+       if (!filter) {
+               return NULL;
+       }
+
+       /*
+        * Create the filter with the table additional filter and the
+        * parameter/value pairs we were given
+        */
+       ast_str_append(&filter, 0, "(&");
+       if (config && config->additional_filter) {
+               ast_str_append(&filter, 0, "%s", config->additional_filter);
+       }
+       if (config != base_table_config
+               && base_table_config
+               && base_table_config->additional_filter) {
+               ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
+       }
+       /* Append the lookup fields */
+       for (field = fields; field; field = field->next) {
+               append_var_and_value_to_filter(&filter, config, field->name, field->value);
+       }
+       ast_str_append(&filter, 0, ")");
+
+       return filter;
+}
+
 /*! \brief LDAP base function 
  * \return a null terminated array of ast_variable (one per entry) or NULL if no entry is found or if an error occured
  * caller should free the returned array and ast_variables
  * \param entries_count_ptr is a pointer to found entries count (can be NULL)
  * \param basedn is the base DN
  * \param table_name is the table_name (used dor attribute convertion and additional filter)
- * \param ap contains null terminated list of pairs name/value
+ * \param fields contains list of pairs name/value
 */
 static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_ptr,
-       const char *basedn, const char *table_name, va_list ap)
+       const char *basedn, const char *table_name, const struct ast_variable *fields)
 {
        struct ast_variable **vars = NULL;
-       const char *newparam = NULL;
-       const char *newval = NULL;
+       const struct ast_variable *field = fields;
        struct ldap_table_config *table_config = NULL;
        char *clean_basedn = cleaned_basedn(NULL, basedn);
        struct ast_str *filter = NULL;
@@ -783,20 +821,9 @@ static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_p
                return NULL;
        } 
 
-       if (!(filter = ast_str_create(80))) {
-               ast_log(LOG_ERROR, "Can't initialize data structures.n");
-               ast_free(clean_basedn);
-               return NULL;
-       }
-
-       /* Get the first parameter and first value in our list of passed paramater/value pairs  */
-       newparam = va_arg(ap, const char *);
-       newval = va_arg(ap, const char *);
-
-       if (!newparam || !newval) {
+       if (!field) {
                ast_log(LOG_ERROR, "Realtime retrieval requires at least 1 parameter"
                        " and 1 value to search on.\n");
-               ast_free(filter);
                ast_free(clean_basedn);
                return NULL;
        }
@@ -806,7 +833,6 @@ static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_p
        /* We now have our complete statement; Lets connect to the server and execute it.  */
        if (!ldap_reconnect()) {
                ast_mutex_unlock(&ldap_lock);
-               ast_free(filter);
                ast_free(clean_basedn);
                return NULL;
        }
@@ -815,31 +841,16 @@ static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_p
        if (!table_config) {
                ast_log(LOG_WARNING, "No table named '%s'.\n", table_name);
                ast_mutex_unlock(&ldap_lock);
-               ast_free(filter);
                ast_free(clean_basedn);
                return NULL;
        }
 
-       ast_str_append(&filter, 0, "(&");
-
-       if (table_config && table_config->additional_filter) {
-               ast_str_append(&filter, 0, "%s", table_config->additional_filter);
-       }
-       if (table_config != base_table_config && base_table_config && 
-               base_table_config->additional_filter) {
-               ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
-       }
-
-       /* Create the first part of the query using the first parameter/value pairs we just extracted.
-        * If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat
-        */
-
-       append_var_and_value_to_filter(&filter, table_config, newparam, newval);
-       while ((newparam = va_arg(ap, const char *))) {
-               newval = va_arg(ap, const char *);
-               append_var_and_value_to_filter(&filter, table_config, newparam, newval);
+       filter = create_lookup_filter(table_config, fields);
+       if (!filter) {
+               ast_mutex_unlock(&ldap_lock);
+               ast_free(clean_basedn);
+               return NULL;
        }
-       ast_str_append(&filter, 0, ")");
 
        do {
                /* freeing ldap_result further down */
@@ -934,31 +945,50 @@ static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_p
                }
        }
 
-       if (filter) {
-               ast_free(filter);
-       }
-
-       if (clean_basedn) {
-               ast_free(clean_basedn);
-       }
+       ast_free(filter);
+       ast_free(clean_basedn);
 
        ast_mutex_unlock(&ldap_lock);
 
        return vars;
 }
 
+static struct ast_variable *realtime_arguments_to_fields(va_list ap)
+{
+       struct ast_variable *fields = NULL;
+       const char *newparam, *newval;
+
+       while ((newparam = va_arg(ap, const char *))) {
+               struct ast_variable *field;
+
+               newval = va_arg(ap, const char *);
+               if (!(field = ast_variable_new(newparam, newval, ""))) {
+                       ast_variables_destroy(fields);
+                       return NULL;
+               }
+
+               field->next = fields;
+               fields = field;
+       }
+
+       return fields;
+}
+
 /*! \brief same as realtime_ldap_base_ap but take variable arguments count list
  */
 static struct ast_variable **realtime_ldap_base(unsigned int *entries_count_ptr,
        const char *basedn, const char *table_name, ...)
 {
+       RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);
        struct ast_variable **vars = NULL;
        va_list ap;
 
        va_start(ap, table_name);
-       vars = realtime_ldap_base_ap(entries_count_ptr, basedn, table_name, ap);
+       fields = realtime_arguments_to_fields(ap);
        va_end(ap);
 
+       vars = realtime_ldap_base_ap(entries_count_ptr, basedn, table_name, fields);
+
        return vars;
 }
 
@@ -967,9 +997,9 @@ static struct ast_variable **realtime_ldap_base(unsigned int *entries_count_ptr,
  * For Realtime Dynamic(i.e., switch, queues, and directory)
  */
 static struct ast_variable *realtime_ldap(const char *basedn,
-                                         const char *table_name, va_list ap)
+                                         const char *table_name, const struct ast_variable *fields)
 {
-       struct ast_variable **vars = realtime_ldap_base_ap(NULL, basedn, table_name, ap);
+       struct ast_variable **vars = realtime_ldap_base_ap(NULL, basedn, table_name, fields);
        struct ast_variable *var = NULL;
 
        if (vars) {
@@ -989,7 +1019,7 @@ static struct ast_variable *realtime_ldap(const char *basedn,
                        }
                        p++;
                }
-               free(vars);
+               ast_free(vars);
        }
        return var;
 }
@@ -1002,22 +1032,19 @@ static struct ast_variable *realtime_ldap(const char *basedn,
  * I think this function returns Realtime dynamic objects
  */
 static struct ast_config *realtime_multi_ldap(const char *basedn,
-      const char *table_name, va_list ap)
+      const char *table_name, const struct ast_variable *fields)
 {
        char *op;
        const char *initfield = NULL;
-       const char *newparam, *newval;
        struct ast_variable **vars =
-               realtime_ldap_base_ap(NULL, basedn, table_name, ap);
+               realtime_ldap_base_ap(NULL, basedn, table_name, fields);
        struct ast_config *cfg = NULL;
 
-       newparam = va_arg(ap, const char *);
-       newval = va_arg(ap, const char *);
-       if (!newparam || !newval) {
+       if (!fields) {
            ast_log(LOG_WARNING, "realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
            return NULL;
        }
-       initfield = ast_strdupa(newparam);
+       initfield = ast_strdupa(fields->name);
        if ((op = strchr(initfield, ' '))) {
                *op = '\0';
        }
@@ -1030,10 +1057,8 @@ static struct ast_config *realtime_multi_ldap(const char *basedn,
                        struct ast_variable **p = vars;
 
                        while (*p) {
-                               struct ast_category *cat = NULL;
-                               cat = ast_category_new("", table_name, -1);
+                               struct ast_category *cat = ast_category_new_anonymous();
                                if (!cat) {
-                                       ast_log(LOG_ERROR, "Unable to create a new category!\n");
                                        break;
                                } else {
                                        struct ast_variable *var = *p;
@@ -1051,7 +1076,7 @@ static struct ast_config *realtime_multi_ldap(const char *basedn,
                                p++;
                        }
                }
-               free(vars);
+               ast_free(vars);
        }
        return cfg;
 
@@ -1123,7 +1148,7 @@ static struct ast_config *config_ldap(const char *basedn, const char *table_name
         * first, and since the data could easily exceed stack size, this is
         * allocated from the heap.
         */
-       if (!(categories = ast_calloc(sizeof(*categories), vars_count))) {
+       if (!(categories = ast_calloc(vars_count, sizeof(*categories)))) {
                return NULL;
        }
 
@@ -1182,7 +1207,7 @@ static struct ast_config *config_ldap(const char *basedn, const char *table_name
                if (!last_category || strcmp(last_category, categories[i].name) ||
                        last_category_metric != categories[i].metric) {
 
-                       cur_cat = ast_category_new(categories[i].name, table_name, -1);
+                       cur_cat = ast_category_new_dynamic(categories[i].name);
                        if (!cur_cat) {
                                break;
                        }
@@ -1204,218 +1229,285 @@ static struct ast_config *config_ldap(const char *basedn, const char *table_name
        return cfg;
 }
 
-/* \brief Function to update a set of values in ldap static mode
+/*!
+ * \internal
+ * \brief Create an LDAP modification structure (LDAPMod)
+ *
+ * \param attribute the name of the LDAP attribute to modify
+ * \param new_value the new value of the LDAP attribute
+ *
+ * \returns an LDAPMod * if successful, NULL otherwise.
  */
-static int update_ldap(const char *basedn, const char *table_name, const char *attribute,
-       const char *lookup, va_list ap)
+static LDAPMod *ldap_mod_create(const char *attribute, const char *new_value)
 {
-       int error = 0;
-       LDAPMessage *ldap_entry = NULL;
-       LDAPMod **ldap_mods;
-       const char *newparam = NULL;
-       const char *newval = NULL;
-       char *dn;
-       int num_entries = 0;
-       int i = 0;
-       int mods_size = 0;
-       int mod_exists = 0;
-       struct ldap_table_config *table_config = NULL;
-       char *clean_basedn = NULL;
-       struct ast_str *filter = NULL;
-       int tries = 0;
-       int result = 0;
-       LDAPMessage *ldap_result_msg = NULL;
+       LDAPMod *mod;
+       char *type;
 
-       if (!table_name) {
-               ast_log(LOG_ERROR, "No table_name specified.\n");
-               return -1;
-       } 
+       mod = ldap_memcalloc(1, sizeof(LDAPMod));
+       type = ldap_strdup(attribute);
 
-       if (!(filter = ast_str_create(80))) {
-               return -1;
+       if (!(mod && type)) {
+               ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
+               ldap_memfree(type);
+               ldap_memfree(mod);
+               return NULL;
        }
 
-       if (!attribute || !lookup) {
-               ast_log(LOG_WARNING, "LINE(%d): search parameters are empty.\n", __LINE__);
-               return -1;
-       }
-       ast_mutex_lock(&ldap_lock);
+       mod->mod_type = type;
 
-       /* We now have our complete statement; Lets connect to the server and execute it.  */
-       if (!ldap_reconnect()) {
-               ast_mutex_unlock(&ldap_lock);
-               return -1;
-       }
+       if (strlen(new_value)) {
+               char **values, *value;
+               values = ldap_memcalloc(2, sizeof(char *));
+               value = ldap_strdup(new_value);
 
-       table_config = table_config_for_table_name(table_name);
-       if (!table_config) {
-               ast_log(LOG_ERROR, "No table named '%s'.\n", table_name);
-               ast_mutex_unlock(&ldap_lock);
-               return -1;
+               if (!(values && value)) {
+                       ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
+                       ldap_memfree(value);
+                       ldap_memfree(values);
+                       ldap_memfree(type);
+                       ldap_memfree(mod);
+                       return NULL;
+               }
+
+               mod->mod_op = LDAP_MOD_REPLACE;
+               mod->mod_values = values;
+               mod->mod_values[0] = value;
+       } else {
+               mod->mod_op = LDAP_MOD_DELETE;
        }
 
-       clean_basedn = cleaned_basedn(NULL, basedn);
+       return mod;
+}
 
-       /* Create the filter with the table additional filter and the parameter/value pairs we were given */
-       ast_str_append(&filter, 0, "(&");
-       if (table_config && table_config->additional_filter) {
-               ast_str_append(&filter, 0, "%s", table_config->additional_filter);
-       }
-       if (table_config != base_table_config && base_table_config && base_table_config->additional_filter) {
-               ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
+/*!
+ * \internal
+ * \brief Append a value to an existing LDAP modification structure
+ *
+ * \param src the LDAPMod to update
+ * \param new_value the new value to append to the LDAPMod
+ *
+ * \returns the \c src original passed in if successful, NULL otherwise.
+ */
+static LDAPMod *ldap_mod_append(LDAPMod *src, const char *new_value)
+{
+       char *new_buffer;
+
+       if (src->mod_op != LDAP_MOD_REPLACE) {
+               return src;
        }
-       append_var_and_value_to_filter(&filter, table_config, attribute, lookup);
-       ast_str_append(&filter, 0, ")");
 
-       /* Create the modification array with the parameter/value pairs we were given, 
-        * if there are several parameters with the same name, we collect them into 
-        * one parameter/value pair and delimit them with a semicolon */
-       newparam = va_arg(ap, const char *);
-       newparam = convert_attribute_name_to_ldap(table_config, newparam);
-       newval = va_arg(ap, const char *);
-       if (!newparam || !newval) {
-               ast_log(LOG_WARNING, "LINE(%d): need at least one parameter to modify.\n", __LINE__);
-               return -1;
+       new_buffer = ldap_memrealloc(
+                       src->mod_values[0],
+                       strlen(src->mod_values[0]) + strlen(new_value) + sizeof(";"));
+
+       if (!new_buffer) {
+               ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
+               return NULL;
        }
 
-       mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
-       ldap_mods = ldap_memcalloc(sizeof(LDAPMod *), mods_size);
-       ldap_mods[0] = ldap_memcalloc(1, sizeof(LDAPMod));
+       strcat(new_buffer, ";");
+       strcat(new_buffer, new_value);
+
+       src->mod_values[0] = new_buffer;
 
-       ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
-       ldap_mods[0]->mod_type = ldap_strdup(newparam);
+       return src;
+}
 
-       ldap_mods[0]->mod_values = ast_calloc(sizeof(char *), 2);
-       ldap_mods[0]->mod_values[0] = ldap_strdup(newval);
+/*!
+ * \internal
+ * \brief Duplicates an LDAP modification structure
+ *
+ * \param src the LDAPMod to duplicate
+ *
+ * \returns a deep copy of \c src if successful, NULL otherwise.
+ */
+static LDAPMod *ldap_mod_duplicate(const LDAPMod *src)
+{
+       LDAPMod *mod;
+       char *type, **values = NULL;
 
-       while ((newparam = va_arg(ap, const char *))) {
-               newparam = convert_attribute_name_to_ldap(table_config, newparam);
-               newval = va_arg(ap, const char *);
-               mod_exists = 0;
-
-               for (i = 0; i < mods_size - 1; i++) {
-                       if (ldap_mods[i]&& !strcmp(ldap_mods[i]->mod_type, newparam)) {
-                               /* We have the parameter allready, adding the value as a semicolon delimited value */
-                               ldap_mods[i]->mod_values[0] = ldap_memrealloc(ldap_mods[i]->mod_values[0], sizeof(char) * (strlen(ldap_mods[i]->mod_values[0]) + strlen(newval) + 2));
-                               strcat(ldap_mods[i]->mod_values[0], ";");
-                               strcat(ldap_mods[i]->mod_values[0], newval);
-                               mod_exists = 1; 
-                               break;
-                       }
-               }
+       mod = ldap_memcalloc(1, sizeof(LDAPMod));
+       type = ldap_strdup(src->mod_type);
 
-               /* create new mod */
-               if (!mod_exists) {
-                       mods_size++;
-                       ldap_mods = ldap_memrealloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
-                       ldap_mods[mods_size - 1] = NULL;
-                       
-                       ldap_mods[mods_size - 2] = ldap_memcalloc(1, sizeof(LDAPMod));
+       if (!(mod && type)) {
+               ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
+               ldap_memfree(type);
+               ldap_memfree(mod);
+               return NULL;
+       }
 
-                       ldap_mods[mods_size - 2]->mod_type = ldap_memcalloc(sizeof(char), strlen(newparam) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
+       if (src->mod_op == LDAP_MOD_REPLACE) {
+               char *value;
 
-                       if (strlen(newval) == 0) {
-                               ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_DELETE;
-                       } else {
-                               ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
+               values = ldap_memcalloc(2, sizeof(char *));
+               value = ldap_strdup(src->mod_values[0]);
 
-                               ldap_mods[mods_size - 2]->mod_values = ldap_memcalloc(sizeof(char *), 2);
-                               ldap_mods[mods_size - 2]->mod_values[0] = ldap_memcalloc(sizeof(char), strlen(newval) + 1);
-                               strcpy(ldap_mods[mods_size - 2]->mod_values[0], newval);
-                       }
+               if (!(values && value)) {
+                       ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
+                       ldap_memfree(value);
+                       ldap_memfree(values);
+                       ldap_memfree(type);
+                       ldap_memfree(mod);
+                       return NULL;
                }
+
+               values[0] = value;
        }
-       /* freeing ldap_mods further down */
 
-       do {
-               /* freeing ldap_result further down */
-               result = ldap_search_ext_s(ldapConn, clean_basedn,
-                                 LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
-                                 &ldap_result_msg);
-               if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
-                       ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
-                       tries++;
-                       if (tries < 3) {
-                               usleep(500000L * tries);
-                               if (ldapConn) {
-                                       ldap_unbind_ext_s(ldapConn, NULL, NULL);
-                                       ldapConn = NULL;
-                               }
-                               if (!ldap_reconnect())
-                                       break;
-                       }
+       mod->mod_op = src->mod_op;
+       mod->mod_type = type;
+       mod->mod_values = values;
+       return mod;
+}
+
+/*!
+ * \internal
+ * \brief Search for an existing LDAP modification structure
+ *
+ * \param modifications a NULL terminated array of LDAP modification structures
+ * \param lookup the attribute name to search for
+ *
+ * \returns an LDAPMod * if successful, NULL otherwise.
+ */
+static LDAPMod *ldap_mod_find(LDAPMod **modifications, const char *lookup)
+{
+       size_t i;
+       for (i = 0; modifications[i]; i++) {
+               if (modifications[i]->mod_op == LDAP_MOD_REPLACE &&
+                       !strcasecmp(modifications[i]->mod_type, lookup)) {
+                       return modifications[i];
                }
-       } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
+       }
+       return NULL;
+}
 
-       if (result != LDAP_SUCCESS) {
-               ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
-               ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
+/*!
+ * \internal
+ * \brief Determine if an LDAP entry has the specified attribute
+ *
+ * \param entry the LDAP entry to examine
+ * \param lookup the attribute name to search for
+ *
+ * \returns 1 if the attribute was found, 0 otherwise.
+ */
+static int ldap_entry_has_attribute(LDAPMessage *entry, const char *lookup)
+{
+       BerElement *ber = NULL;
+       char *attribute;
 
-               ast_mutex_unlock(&ldap_lock);
-               free(filter);
-               free(clean_basedn);
-               ldap_msgfree(ldap_result_msg);
-               ldap_mods_free(ldap_mods, 0);
-               return -1;
+       attribute = ldap_first_attribute(ldapConn, entry, &ber);
+       while (attribute) {
+               if (!strcasecmp(attribute, lookup)) {
+                       ldap_memfree(attribute);
+                       ber_free(ber, 0);
+                       return 1;
+               }
+               ldap_memfree(attribute);
+               attribute = ldap_next_attribute(ldapConn, entry, ber);
        }
-       /* Ready to update */
-       if ((num_entries = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
-               ast_debug(3, "LINE(%d) Modifying %s=%s hits: %d\n", __LINE__, attribute, lookup, num_entries);
-               for (i = 0; option_debug > 2 && i < mods_size - 1; i++) {
-                       if (ldap_mods[i]->mod_op != LDAP_MOD_DELETE) {
-                               ast_debug(3, "LINE(%d) %s=%s \n", __LINE__, ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
-                       } else {
-                               ast_debug(3, "LINE(%d) deleting %s \n", __LINE__, ldap_mods[i]->mod_type);
-                       }
+       ber_free(ber, 0);
+       return 0;
+}
+
+/*!
+ * \internal
+ * \brief Remove LDAP_MOD_DELETE modifications that will not succeed
+ *
+ * \details
+ * A LDAP_MOD_DELETE operation will fail if the LDAP entry does not already have
+ * the corresponding attribute. Because we may be updating multiple LDAP entries
+ * in a single call to update_ldap(), we may need our own copy of the
+ * modifications array for each one.
+ *
+ * \note
+ * This function dynamically allocates memory. If it returns a non-NULL pointer,
+ * it is up to the caller to free it with ldap_mods_free()
+ *
+ * \returns an LDAPMod * if modifications needed to be removed, NULL otherwise.
+ */
+static LDAPMod **massage_mods_for_entry(LDAPMessage *entry, LDAPMod **mods)
+{
+       size_t k, i, remove_count;
+       LDAPMod **copies;
+
+       for (i = remove_count = 0; mods[i]; i++) {
+               if (mods[i]->mod_op == LDAP_MOD_DELETE
+                       && !ldap_entry_has_attribute(entry, mods[i]->mod_type)) {
+                       remove_count++;
                }
-               ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
+       }
+
+       if (!remove_count) {
+               return NULL;
+       }
 
-               for (i = 0; ldap_entry; i++) { 
-                       dn = ldap_get_dn(ldapConn, ldap_entry);
-                       if ((error = ldap_modify_ext_s(ldapConn, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS)  {
-                               ast_log(LOG_ERROR, "Couldn't modify '%s'='%s', dn:%s because %s\n",
-                                               attribute, lookup, dn, ldap_err2string(error));
+       copies = ldap_memcalloc(i - remove_count + 1, sizeof(LDAPMod *));
+       if (!copies) {
+               ast_log(LOG_ERROR, "Memory allocation failure massaging LDAP modification\n");
+               return NULL;
+       }
+
+       for (i = k = 0; mods[i]; i++) {
+               if (mods[i]->mod_op != LDAP_MOD_DELETE
+                       || ldap_entry_has_attribute(entry, mods[i]->mod_type)) {
+                       copies[k] = ldap_mod_duplicate(mods[i]);
+                       if (!copies[k]) {
+                               ast_log(LOG_ERROR, "Memory allocation failure massaging LDAP modification\n");
+                               ldap_mods_free(copies, 1);
+                               return NULL;
                        }
-                       ldap_memfree(dn);
-                       ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
+                       k++;
+               } else {
+                       ast_debug(3, "Skipping %s deletion because it doesn't exist\n",
+                                       mods[i]->mod_type);
                }
        }
 
-       ast_mutex_unlock(&ldap_lock);
-       ast_free(filter);
-       ast_free(clean_basedn);
-       ldap_msgfree(ldap_result_msg);
-       ldap_mods_free(ldap_mods, 0);
-       return num_entries;
+       return copies;
 }
 
-static int update2_ldap(const char *basedn, const char *table_name, va_list ap)
+/*!
+ * \internal
+ * \brief Count the number of variables in an ast_variables list
+ *
+ * \param vars the list of variables to count
+ *
+ * \returns the number of variables in the specified list
+ */
+static size_t variables_count(const struct ast_variable *vars)
 {
-       int error = 0;
-       LDAPMessage *ldap_entry = NULL;
-       LDAPMod **ldap_mods;
-       const char *newparam = NULL;
-       const char *newval = NULL;
-       char *dn;
-       int num_entries = 0;
-       int i = 0;
-       int mods_size = 0;
-       int mod_exists = 0;
+       const struct ast_variable *var;
+       size_t count = 0;
+       for (var = vars; var; var = var->next) {
+               count++;
+       }
+       return count;
+}
+
+static int update2_ldap(const char *basedn, const char *table_name, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
+{
+       const struct ast_variable *field;
        struct ldap_table_config *table_config = NULL;
        char *clean_basedn = NULL;
        struct ast_str *filter = NULL;
+       int search_result = 0;
+       int res = -1;
        int tries = 0;
-       int result = 0;
+       size_t update_count, update_index, entry_count;
+
+       LDAPMessage *ldap_entry = NULL;
+       LDAPMod **modifications;
        LDAPMessage *ldap_result_msg = NULL;
 
        if (!table_name) {
                ast_log(LOG_ERROR, "No table_name specified.\n");
-               return -1;
-       } 
+               return res;
+       }
 
-       if (!(filter = ast_str_create(80))) {
-               return -1;
+       update_count = variables_count(update_fields);
+       if (!update_count) {
+               ast_log(LOG_WARNING, "Need at least one parameter to modify.\n");
+               return res;
        }
 
        ast_mutex_lock(&ldap_lock);
@@ -1423,103 +1515,40 @@ static int update2_ldap(const char *basedn, const char *table_name, va_list ap)
        /* We now have our complete statement; Lets connect to the server and execute it.  */
        if (!ldap_reconnect()) {
                ast_mutex_unlock(&ldap_lock);
-               ast_free(filter);
-               return -1;
+               return res;
        }
 
        table_config = table_config_for_table_name(table_name);
        if (!table_config) {
                ast_log(LOG_ERROR, "No table named '%s'.\n", table_name);
                ast_mutex_unlock(&ldap_lock);
-               ast_free(filter);
-               return -1;
+               return res;
        }
 
        clean_basedn = cleaned_basedn(NULL, basedn);
 
-       /* Create the filter with the table additional filter and the parameter/value pairs we were given */
-       ast_str_append(&filter, 0, "(&");
-       if (table_config && table_config->additional_filter) {
-               ast_str_append(&filter, 0, "%s", table_config->additional_filter);
-       }
-       if (table_config != base_table_config && base_table_config
-               && base_table_config->additional_filter) {
-               ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
-       }
-
-       /* Get multiple lookup keyfields and values */
-       while ((newparam = va_arg(ap, const char *))) {
-               newval = va_arg(ap, const char *);
-               append_var_and_value_to_filter(&filter, table_config, newparam, newval);
-       }
-       ast_str_append(&filter, 0, ")");
-
-       /* Create the modification array with the parameter/value pairs we were given, 
-        * if there are several parameters with the same name, we collect them into 
-        * one parameter/value pair and delimit them with a semicolon */
-       newparam = va_arg(ap, const char *);
-       newparam = convert_attribute_name_to_ldap(table_config, newparam);
-       newval = va_arg(ap, const char *);
-       if (!newparam || !newval) {
-               ast_log(LOG_WARNING, "LINE(%d): need at least one parameter to modify.\n", __LINE__);
-               ast_free(filter);
+       filter = create_lookup_filter(table_config, lookup_fields);
+       if (!filter) {
+               ast_mutex_unlock(&ldap_lock);
                ast_free(clean_basedn);
-               return -1;
+               return res;
        }
 
-       mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
-       ldap_mods = ast_calloc(sizeof(LDAPMod *), mods_size);
-       ldap_mods[0] = ast_calloc(1, sizeof(LDAPMod));
-
-       ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
-       ldap_mods[0]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
-       strcpy(ldap_mods[0]->mod_type, newparam);
-
-       ldap_mods[0]->mod_values = ast_calloc(sizeof(char), 2);
-       ldap_mods[0]->mod_values[0] = ast_calloc(sizeof(char), strlen(newval) + 1);
-       strcpy(ldap_mods[0]->mod_values[0], newval);
-
-       while ((newparam = va_arg(ap, const char *))) {
-               newparam = convert_attribute_name_to_ldap(table_config, newparam);
-               newval = va_arg(ap, const char *);
-               mod_exists = 0;
-
-               for (i = 0; i < mods_size - 1; i++) {
-                       if (ldap_mods[i]&& !strcmp(ldap_mods[i]->mod_type, newparam)) {
-                               /* We have the parameter allready, adding the value as a semicolon delimited value */
-                               ldap_mods[i]->mod_values[0] = ast_realloc(ldap_mods[i]->mod_values[0], sizeof(char) * (strlen(ldap_mods[i]->mod_values[0]) + strlen(newval) + 2));
-                               strcat(ldap_mods[i]->mod_values[0], ";");
-                               strcat(ldap_mods[i]->mod_values[0], newval);
-                               mod_exists = 1; 
-                               break;
-                       }
-               }
-
-               /* create new mod */
-               if (!mod_exists) {
-                       mods_size++;
-                       ldap_mods = ast_realloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
-                       ldap_mods[mods_size - 1] = NULL;
-                       ldap_mods[mods_size - 2] = ast_calloc(1, sizeof(LDAPMod));
-
-                       ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
-
-                       ldap_mods[mods_size - 2]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
-
-                       ldap_mods[mods_size - 2]->mod_values = ast_calloc(sizeof(char *), 2);
-                       ldap_mods[mods_size - 2]->mod_values[0] = ast_calloc(sizeof(char), strlen(newval) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_values[0], newval);
-               }
-       }
-       /* freeing ldap_mods further down */
+       /*
+        * Find LDAP records that match our lookup filter. If there are none, then
+        * we don't go through the hassle of building our modifications list.
+        */
 
        do {
-               /* freeing ldap_result further down */
-               result = ldap_search_ext_s(ldapConn, clean_basedn,
-                                 LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
-                                 &ldap_result_msg);
-               if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
+               search_result = ldap_search_ext_s(
+                               ldapConn,
+                               clean_basedn,
+                               LDAP_SCOPE_SUBTREE,
+                               ast_str_buffer(filter),
+                               NULL, 0, NULL, NULL, NULL,
+                               LDAP_NO_LIMIT,
+                               &ldap_result_msg);
+               if (search_result != LDAP_SUCCESS && is_ldap_connect_error(search_result)) {
                        ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
                        tries++;
                        if (tries < 3) {
@@ -1533,47 +1562,128 @@ static int update2_ldap(const char *basedn, const char *table_name, va_list ap)
                                }
                        }
                }
-       } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
+       } while (search_result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(search_result));
 
-       if (result != LDAP_SUCCESS) {
-               ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
+       if (search_result != LDAP_SUCCESS) {
+               ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(search_result));
                ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
+               goto early_bailout;
+       }
 
-               ast_mutex_unlock(&ldap_lock);
-               ast_free(filter);
-               ast_free(clean_basedn);
-               ldap_msgfree(ldap_result_msg);
-               ldap_mods_free(ldap_mods, 0);
-               return -1;
+       entry_count = ldap_count_entries(ldapConn, ldap_result_msg);
+       if (!entry_count) {
+               /* Nothing found, nothing to update */
+               res = 0;
+               goto early_bailout;
        }
-       /* Ready to update */
-       if ((num_entries = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
-               for (i = 0; option_debug > 2 && i < mods_size - 1; i++) {
-                       ast_debug(3, "LINE(%d) %s=%s \n", __LINE__, ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
-               }
 
-               ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
+       /* We need to NULL terminate, so we allocate one more than we need */
+       modifications = ldap_memcalloc(update_count + 1, sizeof(LDAPMod *));
+       if (!modifications) {
+               ast_log(LOG_ERROR, "Memory allocation failure\n");
+               goto early_bailout;
+       }
 
-               for (i = 0; ldap_entry; i++) {
-                       dn = ldap_get_dn(ldapConn, ldap_entry);
-                       if ((error = ldap_modify_ext_s(ldapConn, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS)  {
-                               ast_log(LOG_ERROR, "Couldn't modify dn:%s because %s", dn, ldap_err2string(error));
+       /*
+        * Create the modification array with the parameter/value pairs we were given,
+        * if there are several parameters with the same name, we collect them into
+        * one parameter/value pair and delimit them with a semicolon
+        */
+       for (field = update_fields, update_index = 0; field; field = field->next) {
+               LDAPMod *mod;
+
+               const char *ldap_attribute_name = convert_attribute_name_to_ldap(
+                               table_config,
+                               field->name);
+
+               /* See if we already have it */
+               mod = ldap_mod_find(modifications, ldap_attribute_name);
+               if (mod) {
+                       mod = ldap_mod_append(mod, field->value);
+                       if (!mod) {
+                               goto late_bailout;
+                       }
+               } else {
+                       mod = ldap_mod_create(ldap_attribute_name, field->value);
+                       if (!mod) {
+                               goto late_bailout;
                        }
-                       ldap_memfree(dn);
-                       ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
+                       modifications[update_index++] = mod;
                }
        }
 
-       ast_mutex_unlock(&ldap_lock);
-       if (filter) {
-               ast_free(filter);
+       /* Ready to update */
+       ast_debug(3, "Modifying %zu matched entries\n", entry_count);
+       if (option_debug > 2) {
+               size_t i;
+               for (i = 0; modifications[i]; i++) {
+                       if (modifications[i]->mod_op != LDAP_MOD_DELETE) {
+                               ast_debug(3, "%s => %s\n", modifications[i]->mod_type,
+                                               modifications[i]->mod_values[0]);
+                       } else {
+                               ast_debug(3, "deleting %s\n", modifications[i]->mod_type);
+                       }
+               }
        }
-       if (clean_basedn) {
-               ast_free(clean_basedn);
+
+       for (ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
+               ldap_entry;
+               ldap_entry = ldap_next_entry(ldapConn, ldap_entry)) {
+               int error;
+               LDAPMod **massaged, **working;
+
+               char *dn = ldap_get_dn(ldapConn, ldap_entry);
+               if (!dn) {
+                       ast_log(LOG_ERROR, "Memory allocation failure\n");
+                       goto late_bailout;
+               }
+
+               working = modifications;
+
+               massaged = massage_mods_for_entry(ldap_entry, modifications);
+               if (massaged) {
+                       /* Did we massage everything out of the list? */
+                       if (!massaged[0]) {
+                               ast_debug(3, "Nothing left to modify - skipping\n");
+                               ldap_mods_free(massaged, 1);
+                               ldap_memfree(dn);
+                               continue;
+                       }
+                       working = massaged;
+               }
+
+               if ((error = ldap_modify_ext_s(ldapConn, dn, working, NULL, NULL)) != LDAP_SUCCESS)  {
+                       ast_log(LOG_ERROR, "Couldn't modify dn:%s because %s", dn, ldap_err2string(error));
+               }
+
+               if (massaged) {
+                       ldap_mods_free(massaged, 1);
+               }
+
+               ldap_memfree(dn);
        }
+
+       res = entry_count;
+
+late_bailout:
+       ldap_mods_free(modifications, 1);
+
+early_bailout:
        ldap_msgfree(ldap_result_msg);
-       ldap_mods_free(ldap_mods, 0);
-       return num_entries;
+       ast_free(filter);
+       ast_free(clean_basedn);
+       ast_mutex_unlock(&ldap_lock);
+
+       return res;
+}
+
+static int update_ldap(const char *basedn, const char *table_name, const char *attribute, const char *lookup, const struct ast_variable *fields)
+{
+       int res;
+       struct ast_variable *lookup_fields = ast_variable_new(attribute, lookup, "");
+       res = update2_ldap(basedn, table_name, lookup_fields, fields);
+       ast_variables_destroy(lookup_fields);
+       return res;
 }
 
 static struct ast_config_engine ldap_engine = {
@@ -1674,6 +1784,21 @@ static int reload(void)
        return 0;
 }
 
+static int config_can_be_inherited(const char *key)
+{
+       int i;
+       static const char * const config[] = {
+               "basedn", "host", "pass", "port", "protocol", "url", "user", "version", NULL
+       };
+
+       for (i = 0; config[i]; i++) {
+               if (!strcasecmp(key, config[i])) {
+                       return 0;
+               }
+       }
+       return 1;
+}
+
 /*! \brief parse the configuration file
  */
 static int parse_config(void)
@@ -1764,7 +1889,9 @@ static int parse_config(void)
                                if (!strcasecmp(var->name, "additionalFilter")) {
                                        table_config->additional_filter = ast_strdup(var->value);
                                } else {
-                                       ldap_table_config_add_attribute(table_config, var->name, var->value);
+                                       if (!is_general || config_can_be_inherited(var->name)) {
+                                               ldap_table_config_add_attribute(table_config, var->name, var->value);
+                                       }
                                }
                        }
                }
@@ -1828,7 +1955,9 @@ static int ldap_reconnect(void)
  */
 static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       char status[256], credentials[100] = "";
+       char status[256];
+       char credentials[100] = "";
+       char buf[362]; /* 256+100+" for "+NULL */
        int ctimesec = time(NULL) - connect_time;
 
        switch (cmd) {
@@ -1851,25 +1980,8 @@ static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_c
        if (!ast_strlen_zero(user))
                snprintf(credentials, sizeof(credentials), " with username %s", user);
 
-       if (ctimesec > 31536000) {
-               ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",
-                               status, credentials, ctimesec / 31536000,
-                               (ctimesec % 31536000) / 86400, (ctimesec % 86400) / 3600,
-                               (ctimesec % 3600) / 60, ctimesec % 60);
-       } else if (ctimesec > 86400) {
-               ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n",
-                               status, credentials, ctimesec / 86400, (ctimesec % 86400) / 3600,
-                               (ctimesec % 3600) / 60, ctimesec % 60);
-       } else if (ctimesec > 3600) {
-               ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n",
-                               status, credentials, ctimesec / 3600, (ctimesec % 3600) / 60,
-                               ctimesec % 60);
-       } else if (ctimesec > 60) {
-               ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, credentials,
-                                       ctimesec / 60, ctimesec % 60);
-       } else {
-               ast_cli(a->fd, "%s%s for %d seconds.\n", status, credentials, ctimesec);
-       }
+       snprintf(buf, sizeof(buf), "%s%s for ", status, credentials);
+       ast_cli_print_timestr_fromseconds(a->fd, ctimesec, buf);
 
        return CLI_SUCCESS;
 }
@@ -1878,6 +1990,7 @@ static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_c
  *
  */
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "LDAP realtime interface",
+       .support_level = AST_MODULE_SUPPORT_EXTENDED,
        .load = load_module,
        .unload = unload_module,
        .reload = reload,