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

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue May 8 08:52:25 MST 2007


Author: russell
Date: Tue May  8 10:52:25 2007
New Revision: 63405

URL: http://svn.digium.com/view/asterisk?view=rev&rev=63405
Log:
more 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=63405&r1=63404&r2=63405
==============================================================================
--- team/group/res_config_ldap/res/res_config_ldap.c (original)
+++ team/group/res_config_ldap/res/res_config_ldap.c Tue May  8 10:52:25 2007
@@ -105,10 +105,17 @@
 /*! \brief Create a new table_config */
 static struct ldap_table_config *table_config_new(const char *table_name)
 {
-	struct ldap_table_config *p = ast_calloc(1, sizeof(*p));
-
-	if (table_name)
-		p->table_name = strdup(table_name);
+	struct ldap_table_config *p;
+	
+	if (!(p = ast_calloc(1, sizeof(*p))))
+		return NULL;
+
+	if (table_name) {
+		if (!(p->table_name = ast_strdup(table_name))) {
+			free(p);
+			return NULL;
+		}
+	}
 
 	return p;
 }
@@ -176,56 +183,57 @@
 static void ldap_table_config_add_escape(struct ldap_table_config *table_config,
 	const char *escape_string)
 {
-	if (escape_string && *escape_string) {
-		char *string = strdup(escape_string);
-		char *start = string;
-		char *p = strstr(start, "=>");
-		if (option_debug)
-			ast_log(LOG_DEBUG, "LDAP RealTime: Add escape: start: %s\n", start);
-		if (!p) {
-			ast_log(LOG_WARNING,
-					"LDAP RealTime: Missing '=>' in escape: %s in %s\n",
-					escape_string, table_config->table_name);
-		} else {
-			char *value = p + 2;	//skip =>      
-			// trim !
-			while (isspace(*start))
-				start++;
-			p--;
-			while (p >= start && isspace(*p)) {
-				*p = '\0';
-				p--;
-			}
-			while (isspace(*value))
-				value++;
-			p = value + strlen(value) - 1;
-			while (p >= value && isspace(*p)) {
-				*p = '\0';
-				p--;
-			}
-			if (*start == '\0') {
-				ast_log(LOG_WARNING,
-						"LDAP RealTime: Empty variable name in escape: %s in %s\n",
-						escape_string, table_config->table_name);
-			} else if (*value == '\0') {
-				ast_log(LOG_WARNING,
-						"LDAP RealTime: Empty ldap escape name in escape: %s in %s\n",
-						escape_string, table_config->table_name);
-			} else {
-				struct ast_variable *var = ast_variable_new(start, value);
-				if(option_debug)
-					ast_log(LOG_DEBUG, "LDAP RealTime: Add escape: VAR %s => %s\n",var->name,var->value);
-				if (table_config->escapes)
-					var->next = table_config->escapes;
-				table_config->escapes = var;
-				if(option_debug)
-					ast_log(LOG_DEBUG,
-							"LDAP RealTime (%d): Added escape in %s: %s -> %s\n",
-							__LINE__, table_config->table_name, start, value);
-			}
-		}
-		free(string);
-	}
+	char *string;
+	char *start;
+	char *p;
+	char *value;
+	struct ast_variable *var;
+
+	if (ast_strlen_zero(escape_string))
+		return;
+
+	start = string = ast_strdupa(escape_string);
+
+	if (!(p = strstr(start, "=>"))) {
+		ast_log(LOG_WARNING, "LDAP RealTime: Missing '=>' in escape: %s in %s\n",
+			escape_string, table_config->table_name);
+		return;
+	}
+
+	value = p + 2;	/* skip => */
+
+	/* trim ! */
+
+	start = ast_skip_blanks(start);
+	p--;
+	while (p >= start && isspace(*p)) {
+		*p = '\0';
+		p--;
+	}
+
+	value = ast_skip_blanks(value);
+	p = value + strlen(value) - 1;
+	while (p >= value && isspace(*p)) {
+		*p = '\0';
+		p--;
+	}
+
+	if (ast_strlen_zero(start)) {
+		ast_log(LOG_WARNING, "LDAP RealTime: Empty variable name in escape: %s in %s\n",
+			escape_string, table_config->table_name);
+		return;
+	}
+
+	if (ast_strlen_zero(value)) {
+		ast_log(LOG_WARNING, "LDAP RealTime: Empty ldap escape name in escape: %s in %s\n",
+			escape_string, table_config->table_name);
+		return;
+	}
+	
+	var = ast_variable_new(start, value);
+	if (table_config->escapes)
+		var->next = table_config->escapes;
+	table_config->escapes = var;
 }
 
 /*! \brief add attribute to table config - Should be locked before using it */
@@ -1584,6 +1592,7 @@
 {
 	struct ast_config *config;
 	const char *s;
+	char *category_name = NULL;
 
 	config = ast_config_load(RES_CONFIG_LDAP_CONF);
 
@@ -1629,16 +1638,12 @@
 	}
 	
 	table_configs_free();
-	
-	char *category_name = NULL;
+
 	while ((category_name = ast_category_browse(config, category_name))) {
 		int is_general = (strcasecmp(category_name, "_general") == 0);
 		int is_config = (strcasecmp(category_name, "config") == 0); /*!< using the [config] context for Static RealTime */
-		
 		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);
@@ -1650,36 +1655,25 @@
 				if (is_config)
 					static_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) {
+			for (; var; var = var->next) {
+				if (!strcasecmp(var->name, "attribute"))
 					ldap_table_config_add_attribute(table_config, var->value);
-				} else if (strcasecmp(var->name, "escape") == 0) {
-					ldap_table_config_add_escape(table_config,var->value);
-				} else if (strcasecmp(var->name, "additionalFilter") == 0) {
+				else if (!strcasecmp(var->name, "escape"))
+					ldap_table_config_add_escape(table_config, var->value);
+				else if (!strcasecmp(var->name, "additionalFilter"))
 					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", host);
-		ast_log(LOG_DEBUG, "LDAP RealTime Port: %i\n", port);
-		ast_log(LOG_DEBUG, "LDAP RealTime User: %s\n", user);
-		ast_log(LOG_DEBUG, "LDAP RealTime Password: %s\n", pass);
-		ast_log(LOG_DEBUG, "LDAP RealTime BaseDN: %s\n", basedn);
-	}
+	
 	return 1;
 }
 
+/*! \note ldap_lock should have been locked before calling this function. */
 static int ldap_reconnect(void)
 {
-	/* mutex lock should have been locked before calling this function. */
 	int bind_result = 0;
 	struct berval cred;
 
@@ -1694,16 +1688,14 @@
 		return 0;
 	}
 
-	//if (!(ldapConn = ldap_init(host, port))) {
 	if (LDAP_SUCCESS != ldap_initialize(&ldapConn, host)) {
 		ast_log(LOG_ERROR, "Failed to init ldap connection to %s. Check debug for more info.\n", host);
 		return 0;
 	} 
 
-	if (user && *user) {
+	if (!ast_strlen_zero(user)) {
 		if (option_debug > 1)
 			ast_log(LOG_DEBUG, "bind to %s as %s\n", host, user);
-		//bind_result = ldap_simple_bind_s(ldapConn, user, pass);
 		cred.bv_val = (char *) pass;
 		cred.bv_len = strlen(pass);
 		bind_result = ldap_sasl_bind_s(ldapConn, user, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
@@ -1711,7 +1703,6 @@
 		if (option_debug > 1)
 			ast_log(LOG_DEBUG, "bind anonymously %s anonymously\n", host);
 		bind_result = ldap_sasl_bind_s(ldapConn, NULL, LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
-		//bind_result = ldap_simple_bind_s(ldapConn, NULL, NULL);
 	}
 	if (bind_result == LDAP_SUCCESS) {
 		if (option_debug > 1)
@@ -1720,7 +1711,7 @@
 		return 1;
 	} else {
 		ast_log(LOG_WARNING, "bind failed: %s\n", ldap_err2string(bind_result));
-		ldap_unbind_ext_s(ldapConn,NULL,NULL);
+		ldap_unbind_ext_s(ldapConn, NULL, NULL);
 		ldapConn = NULL;
 		return 0;
 	}



More information about the asterisk-commits mailing list