[asterisk-commits] russell: branch group/res_config_ldap r63981 - /team/group/res_config_ldap/res/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri May 11 13:10:51 MST 2007


Author: russell
Date: Fri May 11 15:10:50 2007
New Revision: 63981

URL: http://svn.digium.com/view/asterisk?view=rev&rev=63981
Log:
code cleanup ...

Modified:
    team/group/res_config_ldap/res/res_config_ldap.c

Modified: team/group/res_config_ldap/res/res_config_ldap.c
URL: http://svn.digium.com/view/asterisk/team/group/res_config_ldap/res/res_config_ldap.c?view=diff&rev=63981&r1=63980&r2=63981
==============================================================================
--- team/group/res_config_ldap/res/res_config_ldap.c (original)
+++ team/group/res_config_ldap/res/res_config_ldap.c Fri May 11 15:10:50 2007
@@ -2,8 +2,10 @@
  * Asterisk -- A telephony toolkit for Linux.
  *
  * Copyright (C) 2005, Oxymium sarl
- * 
  * Manuel Guesdon <mguesdon at oxymium.net> - LDAP RealTime Driver Author/Adaptor
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ * Russell Bryant <russell at digium.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -24,6 +26,7 @@
  * \author Mark Spencer <markster at digium.com>
  * \author Manuel Guesdon
  * \author Carl-Einar Thorner <cthorner at voicerd.com>
+ * \author Russell Bryant <russell at digium.com>
  *
  * \arg http://www.openldap.org
  */
@@ -240,6 +243,8 @@
 static void ldap_table_config_add_attribute(struct ldap_table_config *table_config,
 	const char *attribute_name, const char *attribute_value)
 {
+	struct ast_variable *var;
+
 	if (ast_strlen_zero(attribute_name) || ast_strlen_zero(attribute_value))
  		return;
 
@@ -352,7 +357,7 @@
 				if (option_debug > 1)
 					ast_log(LOG_DEBUG, "LINE(%d) attribute_name: %s LDAP value: %s\n", __LINE__, attribute_name, value->bv_val);
 				if (is_realmed_password_attribute) {
-					if (strncasecmp(value->bv_val, "{md5}", 5) == 0)
+					if (!strncasecmp(value->bv_val, "{md5}", 5))
 						value->bv_val += 5;
 					else
 						value->bv_val = NULL;
@@ -1118,21 +1123,22 @@
 {
 	struct category_and_metric *as = (struct category_and_metric *) a;
 	struct category_and_metric *bs = (struct category_and_metric *) b;
-	if (as->metric < bs->metric){
+
+	if (as->metric < bs->metric)
 		return -1;
-	} else if (as->metric > bs->metric){
+	else if (as->metric > bs->metric)
 		return 1;
-	} else if ( as->metric == bs->metric && strcmp(as->name, bs->name) < 0){
+	else if (as->metric == bs->metric && strcmp(as->name, bs->name) < 0)
 		return strcmp(as->name, bs->name);
-	} else if ( as->metric == bs->metric && strcmp(as->name, bs->name) > 0){
+	else if (as->metric == bs->metric && strcmp(as->name, bs->name) > 0)
 		return strcmp(as->name, bs->name);
-	} 
+	 
 	/* if the metric and the category name is the same, we check the variable metric */
-	if(as->var_metric < bs->var_metric){
+	if (as->var_metric < bs->var_metric)
 		return -1;
-	} else if(as->var_metric > bs->var_metric){
+	else if(as->var_metric > bs->var_metric)
 		return 1;
-	}
+	
 	return 0;
 }
 
@@ -1143,128 +1149,109 @@
 *	load the configuration stuff for the .conf files
 *	called on a reload
 */
-static struct ast_config *config_ldap(const char *basedn,
-				      const char *table_name,
-				      const char *file,
-				      struct ast_config *cfg,
-				      int withcomments)
-{
-	if(option_debug)
-		ast_log(LOG_DEBUG, "config_ldap: basedn: %s table_name: %s\n",	basedn, table_name);
-
-	if (!file || !strcmp(file, RES_CONFIG_LDAP_CONF)) {
+static struct ast_config *config_ldap(const char *basedn, const char *table_name,
+	const char *file, struct ast_config *cfg, int withcomments)
+{
+	unsigned int vars_count = 0;
+	struct ast_variable **vars;
+	int i = 0;
+	struct ast_variable *new_v = NULL;
+	struct ast_category *cur_cat = NULL;
+	char *last_category = NULL;
+	int last_category_metric = 0;
+	struct category_and_metric *categories;
+	struct ast_variable **p;;
+
+
+	if (!file || !strcasecmp(file, RES_CONFIG_LDAP_CONF)) {
 		ast_log(LOG_WARNING, "Cannot configure myself.\n");
-	} else {
-		unsigned int vars_count = 0;
-		struct ast_variable **vars =
-			realtime_ldap_base_(&vars_count, basedn, table_name, "filename",
-								file, "commented", "FALSE", NULL);
-
-
-ast_log(LOG_DEBUG, "LINE(%d)\n",__LINE__);
-		if (vars) {
-ast_log(LOG_DEBUG, "LINE(%d)\n",__LINE__);
-			int i = 0;
-			struct ast_variable *new_v = NULL;
-			struct ast_category *cur_cat = NULL;
-			char *last_category = NULL;
-			int last_category_metric = 0;
-
-			/* sort on metric and category */
-			struct category_and_metric *categories =
-				ast_calloc(1,sizeof(struct category_and_metric) * vars_count);
-			struct ast_variable **p = vars;
-			vars_count = 0;
-			while (*p) {
-				struct ast_variable *category =
-					variable_named(*p, "category");
-				struct ast_variable *cat_metric =
-					variable_named(*p, "cat_metric");
-				struct ast_variable *var_name =
-					variable_named(*p, "variable_name");
-				struct ast_variable *var_val =
-					variable_named(*p, "variable_value");
-				struct ast_variable *var_metric =
-					variable_named(*p, "var_metric");
-				if (option_debug) {
-					ast_log(LOG_DEBUG, "category: %s\n",
-						category->value);
-					ast_log(LOG_DEBUG, "var_name: %s\n",
-						var_name->value);
-					ast_log(LOG_DEBUG, "var_val: %s\n",
-						var_val->value);
-					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,
-							"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,
-							"No category metric in entry '%s'(category: %s) for file '%s'.\n",
-							(dn ? dn->value : "?"), category->value, file);
-				} else if (!var_metric) {
-					struct ast_variable *dn = variable_named(*p, "dn");
-					ast_log(LOG_ERROR,
-							"No variable 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,
-							"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,
-							"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 {
-					categories[vars_count].name = category->value;
-					categories[vars_count].metric = atoi(cat_metric->value);
-					categories[vars_count].variable_name = var_name->value;
-					categories[vars_count].variable_value = var_val->value;
-					categories[vars_count].var_metric = atoi(var_metric->value);
-					vars_count++;
-				}
-				p++;
-			}
-			qsort(categories, vars_count, sizeof(struct category_and_metric),
-				  compare_categories);
-
-			for (i = 0; i < vars_count; i++) {
-				if (strcmp(categories[i].variable_name, "#include") == 0) {
-					if (!ast_config_internal_load(categories[i].variable_value, cfg, 0)) {
-						break;
-						return NULL;
-					}
-				} else {
-					if (!last_category || strcmp(last_category, categories[i].name) != 0 ||
-						last_category_metric != categories[i].metric) {
-						cur_cat = ast_category_new(categories[i].name);
-						if (!cur_cat) {
-							ast_log(LOG_WARNING, "Out of memory!\n");
-							break;
-						}
-						last_category = categories[i].name;
-						last_category_metric = categories[i].metric;
-						ast_category_append(cfg, cur_cat);
-					}
-					new_v = ast_variable_new(categories[i].variable_name,
-							 categories[i].variable_value);
-					ast_variable_append(cur_cat, new_v);
-				}
-			}
-			free(vars);
-			free(categories);
+		return NULL;
+	}
+
+	vars = realtime_ldap_base_(&vars_count, basedn, table_name, "filename",
+				file, "commented", "FALSE", NULL);
+
+	if (!vars) {
+		ast_log(LOG_WARNING, "Could not find config '%s' in database.\n", file);
+		return NULL;
+	}
+
+	if (!(categories = ast_calloc(1, sizeof(*categories) * vars_count)))
+		return NULL;
+
+	for (vars_count = 0, p = vars; *p; p++) {
+		struct ast_variable *category = variable_named(*p, "category");
+		struct ast_variable *cat_metric = variable_named(*p, "cat_metric");
+		struct ast_variable *var_name = variable_named(*p, "variable_name");
+		struct ast_variable *var_val = variable_named(*p, "variable_value");
+		struct ast_variable *var_metric = variable_named(*p, "var_metric");
+		struct ast_variable *dn = variable_named(*p, "dn");
+			
+		if (option_debug) {
+			ast_log(LOG_DEBUG, "category: %s\n", category->value);
+			ast_log(LOG_DEBUG, "var_name: %s\n", var_name->value);
+			ast_log(LOG_DEBUG, "var_val: %s\n", var_val->value);
+			ast_log(LOG_DEBUG, "cat_metric: %s\n", cat_metric->value);
+		}
+
+		if (!category) {
+			ast_log(LOG_ERROR,
+					"No category name in entry '%s'  for file '%s'.\n",
+					(dn ? dn->value : "?"), file);
+		} else if (!cat_metric) {
+			ast_log(LOG_ERROR,
+					"No category metric in entry '%s'(category: %s) for file '%s'.\n",
+					(dn ? dn->value : "?"), category->value, file);
+		} else if (!var_metric) {
+			ast_log(LOG_ERROR,
+					"No variable metric in entry '%s'(category: %s) for file '%s'.\n",
+					(dn ? dn->value : "?"), category->value, file);
+		} else if (!var_name) {
+			ast_log(LOG_ERROR,
+					"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) {
+			ast_log(LOG_ERROR,
+					"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 {
-			ast_log(LOG_WARNING, "Could not find config '%s' in database.\n", file);
-		}
-	}
+			categories[vars_count].name = category->value;
+			categories[vars_count].metric = atoi(cat_metric->value);
+			categories[vars_count].variable_name = var_name->value;
+			categories[vars_count].variable_value = var_val->value;
+			categories[vars_count].var_metric = atoi(var_metric->value);
+			vars_count++;
+		}
+	}
+
+	qsort(categories, vars_count, sizeof(*categories), compare_categories);
+
+	for (i = 0; i < vars_count; i++) {
+		if (!strcmp(categories[i].variable_name, "#include")) {
+			if (!ast_config_internal_load(categories[i].variable_value, cfg, 0)) {
+				break;
+				return NULL;
+			}
+		} else {
+			if (!last_category || strcmp(last_category, categories[i].name) ||
+				last_category_metric != categories[i].metric) {
+				cur_cat = ast_category_new(categories[i].name);
+				if (!cur_cat)
+					break;
+				last_category = categories[i].name;
+				last_category_metric = categories[i].metric;
+				ast_category_append(cfg, cur_cat);
+			}
+			new_v = ast_variable_new(categories[i].variable_name, categories[i].variable_value);
+			ast_variable_append(cur_cat, new_v);
+		}
+	}
+
+	free(vars);
+	free(categories);
+
 	return cfg;
 
 }
@@ -1293,9 +1280,6 @@
 	int result = 0;
 	LDAPMessage *ldap_result = NULL;
 
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG,
-			"realtime_ldap_base: basedn: %s table_name: %s\n", basedn, table_name);
 	if (!table_name) {
 		ast_log(LOG_WARNING, "No table_name specified.\n");
 		return -1;



More information about the asterisk-commits mailing list