[svn-commits] branch oej/res_config_ldap r27392 - /team/oej/res_config_ldap/res/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Tue May 16 10:04:33 MST 2006


Author: oej
Date: Tue May 16 12:04:32 2006
New Revision: 27392

URL: http://svn.digium.com/view/asterisk?rev=27392&view=rev
Log:
Code cleanup during dev conf call - AstriDevCon Online!

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

Modified: team/oej/res_config_ldap/res/res_config_ldap.c
URL: http://svn.digium.com/view/asterisk/team/oej/res_config_ldap/res/res_config_ldap.c?rev=27392&r1=27391&r2=27392&view=diff
==============================================================================
--- team/oej/res_config_ldap/res/res_config_ldap.c (original)
+++ team/oej/res_config_ldap/res/res_config_ldap.c Tue May 16 12:04:32 2006
@@ -74,6 +74,13 @@
 
 LOCAL_USER_DECL;
 
+struct category_and_metric {
+	char *name;
+	int metric;
+	char *variable_name;
+	char *variable_value;
+};
+
 static char cli_realtime_ldap_status_usage[] =
 	"Usage: realtime ldap status\n"
 	"       Shows connection information for the LDAP RealTime driver\n";
@@ -87,8 +94,9 @@
 /*! \brief Log variable */
 static void ast_variable_log(const char *log_prefix, struct ast_variable *var)
 {
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "LOG Variable: %s\n", log_prefix);
+	if (!option_debug)
+		return;
+	ast_log(LOG_DEBUG, "LOG Variable: %s\n", log_prefix);
 	while (var) {
 		ast_log(LOG_DEBUG, "%s => %s\n", var->name, var->value);
 		var = var->next;
@@ -97,16 +105,14 @@
 		ast_log(LOG_DEBUG, "END LOG Variable: %s\n", log_prefix);
 }
 
-/*! \brief Log config */
+/*! \brief Log config (only executed if option_debug is set) */
 static void ast_config_log(const char *log_prefix, struct ast_config *config)
 {
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "LOG Config: %s\n", log_prefix);
 	char *category_name = NULL;
+
+	ast_log(LOG_DEBUG, "LOG Config: %s\n", log_prefix);
 	while ((category_name = ast_category_browse(config, category_name))) {
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "========= category: %s ==========\n",
-				category_name);
+		ast_log(LOG_DEBUG, "========= category: %s ==========\n", category_name);
 		struct ast_variable *var = ast_variable_browse(config, category_name);
 		if (var) {
 			while (var) {
@@ -115,16 +121,15 @@
 			}
 		}
 	}
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "END LOG Config: %s\n", log_prefix);
+	ast_log(LOG_DEBUG, "END LOG Config: %s\n", log_prefix);
 };
 
 /*! \brief Table configuration */
 struct ldap_table_config {
-	char *table_name;			/* table name */
-	char *additional_filter;	/* additional filter        */
-	struct ast_variable *attributes;	/* attribute names conversion */
-	struct ldap_table_config *next;	/* next table */
+	char *table_name;			/*!< table name */
+	char *additional_filter;		/*!< additional filter        */
+	struct ast_variable *attributes;	/*!< attribute names conversion */
+	struct ldap_table_config *next;		/*!< next table */
 };
 
 /*! \brief Should be locked before using it */
@@ -153,42 +158,6 @@
 	return NULL;
 };
 
-#ifdef DISABLED_CODE
-
-/*! \brief add attributes to table config - Should be locked before using it */
-static void ldap_table_config_add_attributes(struct ldap_table_config* table_config,const char* attributes_string)
-{
-	if (attributes_string && *attributes_string) {      
-		char* string=strdup(attributes_string);
-		char* string_end=string+strlen(string);
-		char* start=string;
-		while(start<string_end) {
-			char* conv_sep=NULL;
-    			char* attr_sep=strchr(start,',');
-			if (!attr_sep)
-				attr_sep=string_end;
-			else
-				*attr_sep='\0';
-			conv_sep=strchr(start,'=');
-			if (conv_sep>=attr_sep)
-				conv_sep=NULL;
-			if (!conv_sep) {
-				ast_log(LOG_WARNING, "LDAP RealTime: Missing '=' in attributes conversion list: %s\n",attributes_string);
-			} else {
-				struct ast_variable* var;
-				*conv_sep='\0';
-				var=ast_variable_new(start,conv_sep+1);
-				if (table_config->attributes)
-					var->next=table_config->attributes;
-				table_config->attributes=var;
-			}
-			start=attr_sep+1;
-		};
-		free(string);
-	};
-};
-#endif
-
 /*! \brief add attribute to table config - Should be locked before using it */
 static void ldap_table_config_add_attribute(struct ldap_table_config *table_config,
 					    const char *attribute_string)
@@ -196,9 +165,11 @@
 	if (attribute_string && *attribute_string) {
 		char *string = strdup(attribute_string);
 		char *start = string;
-		ast_log(LOG_DEBUG, "LDAP RealTime: Add attribute: start: %s\n",
-				start);
 		char *p = strstr(start, "=>");
+
+		if (option_debug)
+			ast_log(LOG_DEBUG, "LDAP RealTime: Add attribute: start: %s\n", start);
+
 		if (!p) {
 			ast_log(LOG_WARNING,
 					"LDAP RealTime: Missing '=>' in attribute: %s in %s\n",
@@ -230,12 +201,13 @@
 						attribute_string, table_config->table_name);
 			} else {
 				struct ast_variable *var = ast_variable_new(start, value);
-				//ast_log(LOG_DEBUG, "LDAP RealTime: Add attribute: VAR %s => %s\n",var->name,var->value);
+				if (option_debug > 2)
+					ast_log(LOG_DEBUG, "LDAP RealTime: Add attribute: VAR %s => %s\n",var->name,var->value);
 				if (table_config->attributes)
 					var->next = table_config->attributes;
 				table_config->attributes = var;
-				ast_log(LOG_DEBUG,
-						"LDAP RealTime (%d): Added attribute in %s: %s -> %s\n",
+				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);
 			}
 		};
@@ -247,6 +219,7 @@
 static void table_configs_free(void)
 {
 	struct ldap_table_config *c = table_configs;
+
 	while (c) {
 		struct ldap_table_config *next = c->next;
 		if (c->table_name)
@@ -291,9 +264,11 @@
 						    const char *attribute_name)
 {
 	int i = 0;
+
 	for (i = 0; i < 2; i++) {
 		if (table_config) {
 			struct ast_variable *attribute = table_config->attributes;
+
 			while (attribute) {
 				if (strcasecmp(attribute_name, attribute->value) == 0)
 					return attribute->name;
@@ -335,8 +310,7 @@
 	while (ldap_attribute_name) {
 		const char *attribute_name =
 			convert_attribute_name_from_ldap(table_config,ldap_attribute_name);
-		int is_realmed_password_attribute =
-			strcasecmp(attribute_name, "md5secret") == 0;
+		int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
 		char **values = NULL;
 
 		values = ldap_get_values(ldapConn, ldap_entry, ldap_attribute_name);
@@ -348,7 +322,7 @@
 				if (option_debug > 1)
 					ast_log(LOG_DEBUG,
 						"LDAP RealTime (%d): attribute_name: %s value: %s\n",
-						__LINE__, attribute_name, value);
+							__LINE__, attribute_name, value);
 				if (is_realmed_password_attribute) {
 					if (strncasecmp(value, "{md5}", 5) == 0)
 						value += 5;
@@ -361,9 +335,8 @@
 				if (value) {
 					if (prev) {
 						prev->next = ast_variable_new(attribute_name, value);
-						if (prev->next) {
+						if (prev->next) 
 							prev = prev->next;
-						}
 					} else {
 						prev = var = ast_variable_new(attribute_name, value);
 					}
@@ -390,7 +363,7 @@
 					   const char *dn)
 {
 	if (!table_config) {
-		ast_log(LOG_ERROR, "LDAP RealTime: Not table config\n");
+		ast_log(LOG_ERROR, "LDAP RealTime: No table config\n");
 		return NULL;
 	} else {
 		struct ast_variable *var = NULL;
@@ -398,16 +371,17 @@
 		LDAPMessage *ldap_result = NULL;
 		int tries = 0;
 
-		ast_log(LOG_DEBUG, "LDAP RealTime (%d): ldap_loadentry dn=%s\n",
+		if (option_debug > 1)
+			ast_log(LOG_DEBUG, "LDAP RealTime (%d): ldap_loadentry dn=%s\n",
 				__LINE__, dn);
 
 		do {
 			result = ldap_search_s(ldapConn, dn, LDAP_SCOPE_BASE,
-								   "(objectclass=*)", NULL, 0, &ldap_result);
+					   "(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",
-						tries + 1);
+					"LDAP RealTime: Failed to query database. Try %d/3\n",
+					tries + 1);
 				tries++;
 				if (tries < 3) {
 					usleep(500000L * tries);
@@ -424,29 +398,27 @@
 		if (result < 0) {
 			ast_log(LOG_WARNING,
 					"LDAP RealTime: Failed to query database. Check debug for more info.\n");
-			ast_log(LOG_DEBUG, "LDAP RealTime: dn=%s\n", dn);
-			ast_log(LOG_DEBUG, "LDAP RealTime: Query Failed because: %s\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",
 					ldap_err2string(result));
+			}
 			ast_mutex_unlock(&ldap_lock);
 			return NULL;
 		} else {
 			int num_entry = 0;
 			if ((num_entry = ldap_count_entries(ldapConn, ldap_result)) > 0) {
 				LDAPMessage *ldap_entry = NULL;
-				ast_log(LOG_DEBUG, "LDAP RealTime (%d): num_entry: %d\n",
+				if (option_debug)
+					ast_log(LOG_DEBUG, "LDAP RealTime (%d): num_entry: %d\n",
 						__LINE__, 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);
-				}
+				if (num_entry > 1)
+					ast_log(LOG_WARNING, "LDAP RealTime: 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, "LDAP RealTime: Could not find any entry dn=%s.\n", dn);
 			}
 		}
 		ldap_msgfree(ldap_result);
@@ -461,16 +433,13 @@
 {
 #define MAXRESULT	2048
 	char *ret_string = NULL;
+
 	if (!ast_strlen_zero(string)) {
-		ret_string = malloc(MAXRESULT);
-		if (!ret_string) {
-			ast_log(LOG_WARNING, "Out of memory\n");
-		} else {
-			memset(ret_string, 0, MAXRESULT);
-			pbx_substitute_variables_helper(channel, string, ret_string, MAXRESULT - 1);
-		}
-	}
-	ast_log(LOG_DEBUG, "LDAP RealTime: substituted: string: '%s' => '%s' \n",
+		ret_string = ast_calloc(1, MAXRESULT);
+		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",
 			string, ret_string);
 	return ret_string;
 }
@@ -498,18 +467,19 @@
 			p++;
 		};
 	}
-	ast_log(LOG_DEBUG, "LDAP RealTime (%d): basedn: '%s' => '%s' \n",
+	if (option_debug > 1)
+		ast_log(LOG_DEBUG, "LDAP RealTime (%d): basedn: '%s' => '%s' \n",
 			__LINE__, basedn, cbasedn);
 	return cbasedn;
 }
 
 /*! \brief Append a string to a filter string. The filter string can grow */
-static void append_string_to_filter(char **filter_ptr, int *filter_size_ptr,
-				    const char *filter)
+static void append_string_to_filter(char **filter_ptr, int *filter_size_ptr, const char *filter)
 {
 	int current_len = 0;
 	int needed_len = 0;
 	char *r_filter = NULL;
+
 	if (strchr(filter, '$')) {
 		r_filter = substituted(NULL, filter);
 		filter = r_filter;
@@ -564,7 +534,8 @@
 	char *new_name = NULL;
 	char *new_value = NULL;
 	char *wsPos = strstr(name, " LIKE");
-	//ast_log(LOG_DEBUG, "LDAP RealTime: name='%s' value='%s'\n",name,value);
+	if (option_debug > 1)
+		ast_log(LOG_DEBUG, "LDAP RealTime: name='%s' value='%s'\n", name, value);
 	if (wsPos) {
 		new_name = strndup(name, wsPos - name);
 		name = new_name;
@@ -593,227 +564,198 @@
  table_name is the table_name (used dor attribute convertion and additional filter)
  ap contains null terminated list of pairs name/value
 */
-static struct ast_variable **realtime_ldap_base(unsigned int
-						*entries_count_ptr,
-						const char *basedn,
-						const char *table_name,
-						va_list ap)
-{
-	ast_log(LOG_DEBUG,
+static struct ast_variable **realtime_ldap_base(unsigned int *entries_count_ptr,
+	const char *basedn, const char *table_name, va_list ap)
+{
+	struct ast_variable **vars = NULL;
+	const char *newparam = NULL;
+	const char *newval = NULL;
+
+	if (option_debug > 1)
+		ast_log(LOG_DEBUG,
 			"LDAP RealTime (%d): realtime_ldap_base: basedn: %s table_name: %s\n",
 			__LINE__, basedn, table_name);
-	struct ast_variable **vars = NULL;
 	if (!table_name) {
 		ast_log(LOG_WARNING, "LDAP RealTime: No table_name specified.\n");
+		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) {
+		ast_log(LOG_WARNING,
+				"LDAP RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
 	} else {
-		const char *newparam = NULL;
-		const char *newval = 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) {
-			ast_log(LOG_WARNING,
-					"LDAP RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
-		} else {
-			ast_mutex_lock(&ldap_lock);
-
-			/* We now have our complete statement; Lets connect to the server and execute it.  */
-			if (!ldap_reconnect()) {
+		ast_mutex_lock(&ldap_lock);
+
+		/* We now have our complete statement; Lets connect to the server and execute it.  */
+		if (ldap_reconnect()) {
+			struct ldap_table_config *table_config = NULL;
+
+			table_config = table_config_for_table_name(table_name);
+			if (!table_config) {
+				ast_log(LOG_WARNING,
+						"LDAP RealTime: No table named '%s'.\n",
+						table_name);
 			} else {
-				struct ldap_table_config *table_config = NULL;
-
-				table_config = table_config_for_table_name(table_name);
-				if (!table_config) {
-					ast_log(LOG_WARNING,
-							"LDAP RealTime: No table named '%s'.\n",
-							table_name);
-				} else {
-					char *clean_basedn = cleaned_basedn(NULL, basedn);
-					char *filter = NULL;
-					int filter_size = 0;
-					int tries = 0;
-
-					int result = 0;
-					LDAPMessage *ldap_result = NULL;
-
-					append_string_to_filter(&filter, &filter_size, "(&");
-
-					if (table_config && table_config->additional_filter) {
+				char *clean_basedn = cleaned_basedn(NULL, basedn);
+				char *filter = NULL;
+				int filter_size = 0;
+				int tries = 0;
+
+				int result = 0;
+				LDAPMessage *ldap_result = NULL;
+
+				append_string_to_filter(&filter, &filter_size, "(&");
+
+				if (table_config && table_config->additional_filter) {
+					append_string_to_filter(&filter, &filter_size,
+						table_config->additional_filter);
+				};
+				if (table_config != base_table_config && base_table_config
+					&& base_table_config->additional_filter) {
 						append_string_to_filter(&filter, &filter_size,
-									table_config->additional_filter);
-					};
-					if (table_config != base_table_config && base_table_config
-						&& base_table_config->additional_filter) {
-						append_string_to_filter(&filter, &filter_size,
-									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 */
-
+							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, &filter_size,
+				       table_config, newparam, newval);
+				while ((newparam = va_arg(ap, const char *))) {
+					newval = va_arg(ap, const char *);
 					append_var_and_value_to_filter(&filter, &filter_size,
-								       table_config, newparam, newval);
-					while ((newparam = va_arg(ap, const char *))) {
-						newval = va_arg(ap, const char *);
-						append_var_and_value_to_filter(&filter, &filter_size,
-									       table_config, newparam,
-									       newval);
-					}
-					append_string_to_filter(&filter, &filter_size, ")");
-
-					ast_log(LOG_DEBUG, "LDAP RealTime (%d): filter: %s\n",
-							__LINE__, filter);
-
-
-					do {
-						result =
-							ldap_search_s(ldapConn, clean_basedn,
-										  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",
-									tries + 1);
-							tries++;
-							if (tries < 3) {
-								usleep(500000L * tries);
-								if (ldapConn) {
-									ldap_unbind_s(ldapConn);
-									ldapConn = NULL;
-								};
-								if (!ldap_reconnect())
-									break;
-							}
-						}
-					} while (result < 0 && tries < 3 &&
-							 is_ldap_connect_error(result));
-
-					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",
-								filter);
-						ast_log(LOG_WARNING,
-								"LDAP RealTime: Query Failed because: %s\n",
-								ldap_err2string(result));
-					} else {
-						int num_entry = 0;
-						if ((num_entry =
-							 ldap_count_entries(ldapConn, ldap_result)) > 0) {
-							LDAPMessage *ldap_entry = NULL;
-							int entry_index = 0;
-							if (entries_count_ptr)
-								*entries_count_ptr = num_entry;
-							vars =
-								malloc(sizeof(struct ast_variable *) *
-									   (num_entry + 1));
-							memset(vars, 0,
-								   sizeof(struct ast_variable *) *
-								   (num_entry + 1));
-							ast_log(LOG_DEBUG,
-									"LDAP RealTime (%d): num_entry: %d\n",
-									__LINE__, num_entry);
-
-							ldap_entry =
-								ldap_first_entry(ldapConn, ldap_result);
-
-							for (entry_index = 0; ldap_entry; entry_index++) {
-								vars[entry_index] =
-									realtime_ldap_entry_to_var(table_config,ldap_entry);
-								ast_variable_log("LDAP RealTime: Found Entry",
-												 vars[entry_index]);
-								ldap_entry =
-									ldap_next_entry(ldapConn, ldap_entry);
-								ast_log(LOG_DEBUG,
-										"LDAP RealTime (%d): next entry: %p\n",
-										__LINE__, ldap_entry);
+					       table_config, newparam, newval);
+				}
+				append_string_to_filter(&filter, &filter_size, ")");
+
+				if (option_debug)
+					ast_log(LOG_DEBUG, "LDAP RealTime (%d): filter: %s\n", __LINE__, filter);
+
+
+				do {
+					result = ldap_search_s(ldapConn, clean_basedn,
+							  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",
+							tries + 1);
+						tries++;
+						if (tries < 3) {
+							usleep(500000L * tries);
+							if (ldapConn) {
+								ldap_unbind_s(ldapConn);
+								ldapConn = NULL;
 							};
-						} else {
-							ast_log(LOG_WARNING,
-									"LDAP RealTime: Could not find any entry matching %s in base dn %s.\n",
-									filter, clean_basedn);
-						}
-
-						ldap_msgfree(ldap_result);
-
-						if (vars) {
-							struct ast_variable **p = vars;
-							while (*p) {
-								struct ast_variable *append_var = NULL;
-								struct ast_variable *tmp = *p;
-								while (tmp) {
-									if (strcasecmp(tmp->name, "accountBaseDN")
-										== 0) {
-										struct ast_variable *base_var =
-											ldap_loadentry(table_config,
-														   tmp->value);
-										ast_log(LOG_DEBUG,
-												"LDAP RealTime (%d): Loaded base: %s\n",
-												__LINE__, tmp->value);
-										ast_variable_log("Base Variables",
-														 base_var);
-										while (base_var) {
-											struct ast_variable *next =
-												base_var->next;
-											struct ast_variable *test_var =
-												*p;
-											int base_var_found = 0;
-											while (test_var) {
-												if (strcasecmp
-													(test_var->name,
-													 base_var->name) == 0) {
-													base_var_found = 1;
-													break;
-												} else
-													test_var = test_var->next;
-											};
-											if (base_var_found) {
-												base_var->next = NULL;
-												ast_variables_destroy
-													(base_var);
-												base_var = next;
-											} else {
-												if (append_var)
-													base_var->next =
-														append_var;
-												else
-													base_var->next = NULL;
-												append_var = base_var;
-												base_var = next;
-											};
-										}
-									}
-									if (!tmp->next && append_var) {
-										tmp->next = append_var;
-										tmp = NULL;
-									} else
-										tmp = tmp->next;
-								};
-								p++;
-							}
+							if (!ldap_reconnect())
+								break;
 						}
 					}
-					if (filter)
-						free(filter);
-					if (clean_basedn)
-						free(clean_basedn);
+				} while (result < 0 && tries < 3 && is_ldap_connect_error(result));
+
+				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",
+							filter);
+					ast_log(LOG_WARNING,
+							"LDAP RealTime: Query Failed because: %s\n",
+								ldap_err2string(result));
+				} else {
+					int num_entry = 0;
+					if ((num_entry = ldap_count_entries(ldapConn, ldap_result)) > 0) {
+						LDAPMessage *ldap_entry = NULL;
+						int entry_index = 0;
+						if (entries_count_ptr)
+							*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);
+
+						ldap_entry = ldap_first_entry(ldapConn, ldap_result);
+
+						for (entry_index = 0; ldap_entry; entry_index++) {
+							vars[entry_index] = realtime_ldap_entry_to_var(table_config,ldap_entry);
+							ast_variable_log("LDAP RealTime: Found Entry", vars[entry_index]);
+							ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
+							if (option_debug)
+								ast_log(LOG_DEBUG, "LDAP RealTime (%d): next entry: %p\n", __LINE__, ldap_entry);
+						};
+					} else {
+						ast_log(LOG_WARNING, "LDAP RealTime: Could not find any entry matching %s in base dn %s.\n",
+							filter, clean_basedn);
+					}
+
+					ldap_msgfree(ldap_result);
+
+					if (vars) {
+						struct ast_variable **p = vars;
+						while (*p) {
+							struct ast_variable *append_var = NULL;
+							struct ast_variable *tmp = *p;
+							while (tmp) {
+								if (strcasecmp(tmp->name, "accountBaseDN") == 0) {
+									struct ast_variable *base_var = ldap_loadentry(table_config, tmp->value);
+									if (option_debug)
+										ast_log(LOG_DEBUG, "LDAP RealTime (%d): Loaded base: %s\n",
+												__LINE__, tmp->value);
+									ast_variable_log("Base Variables", base_var);
+									while (base_var) {
+										struct ast_variable *next = base_var->next;
+										struct ast_variable *test_var = *p;
+										int base_var_found = 0;
+
+										while (test_var) {
+											if (strcasecmp(test_var->name, base_var->name) == 0) {
+												base_var_found = 1;
+												break;
+											} else
+												test_var = test_var->next;
+										};
+										if (base_var_found) {
+											base_var->next = NULL;
+											ast_variables_destroy (base_var);
+											base_var = next;
+										} else {
+											if (append_var)
+												base_var->next = append_var;
+											else
+												base_var->next = NULL;
+											append_var = base_var;
+											base_var = next;
+										};
+									}
+								}
+								if (!tmp->next && append_var) {
+									tmp->next = append_var;
+									tmp = NULL;
+								} else
+									tmp = tmp->next;
+							};
+							p++;
+						}
+					}
 				}
-			}
-			ast_mutex_unlock(&ldap_lock);
+				if (filter)
+					free(filter);
+				if (clean_basedn)
+					free(clean_basedn);
+			}
 		}
+		ast_mutex_unlock(&ldap_lock);
 	}
 	return vars;
 }
 
 /*! \brief same as realtime_ldap_base_ 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, ...)
+static struct ast_variable **realtime_ldap_base_(unsigned int *entries_count_ptr,
+	const char *basedn, const char *table_name, ...)
 {
 	struct ast_variable **vars = NULL;
 	va_list ap;
+
 	va_start(ap, table_name);
 	vars = realtime_ldap_base(entries_count_ptr, basedn, table_name, ap);
 	va_end(ap);
@@ -824,12 +766,12 @@
 static struct ast_variable *realtime_ldap(const char *basedn,
 					  const char *table_name, va_list ap)
 {
-	ast_log(LOG_DEBUG,
-			"LDAP RealTime (%d): realtime_ldap: basedn: %s table_name: %s\n",
+	if (option_debug)
+		ast_log(LOG_DEBUG, "LDAP RealTime (%d): realtime_ldap: basedn: %s table_name: %s\n",
 			__LINE__, basedn, table_name);
-	struct ast_variable **vars =
-		realtime_ldap_base(NULL, basedn, table_name, ap);
+	struct ast_variable **vars = realtime_ldap_base(NULL, basedn, table_name, ap);
 	struct ast_variable *var = NULL;
+
 	if (vars) {
 		struct ast_variable *last_var = NULL;
 		struct ast_variable **p = vars;
@@ -852,21 +794,22 @@
 
 /*! \brief See Asterisk doc */
 static struct ast_config *realtime_multi_ldap(const char *basedn,
-					      const char *table_name,
-					      va_list ap)
-{
-	ast_log(LOG_DEBUG,
-			"LDAP RealTime (%d): realtime_multi_ldap: basedn: %s table_name: %s\n",
+      const char *table_name, va_list ap)
+{
+	if (option_debug)
+		ast_log(LOG_DEBUG, "LDAP RealTime (%d): realtime_multi_ldap: basedn: %s table_name: %s\n",
 			__LINE__, basedn, table_name);
 	struct ast_variable **vars =
 		realtime_ldap_base(NULL, basedn, table_name, ap);
 	struct ast_config *cfg = NULL;
+
 	if (vars) {
 		cfg = ast_config_new();
 		if (!cfg) {
 			ast_log(LOG_WARNING, "Out of memory!\n");
 		} else {
 			struct ast_variable **p = vars;
+
 			while (*p) {
 				struct ast_category *cat = NULL;
 				cat = ast_category_new("");
@@ -891,12 +834,7 @@
 	return cfg;
 
 }
-struct category_and_metric {
-	char *name;
-	int metric;
-	char *variable_name;
-	char *variable_value;
-};
+
 static int compare_categories(const void *a, const void *b)
 {
 	struct category_and_metric *as = (struct category_and_metric *) a;
@@ -947,14 +885,16 @@
 					variable_named(*p, "variable_name");
 				struct ast_variable *var_val =
 					variable_named(*p, "variable_value");
-				ast_log(LOG_DEBUG, "LDAP RealTime: category: %s\n",
+				if (option_debug) {
+					ast_log(LOG_DEBUG, "LDAP RealTime: category: %s\n",
 						category->value);
-				ast_log(LOG_DEBUG, "LDAP RealTime: var_name: %s\n",
+					ast_log(LOG_DEBUG, "LDAP RealTime: var_name: %s\n",
 						var_name->value);
-				ast_log(LOG_DEBUG, "LDAP RealTime: var_val: %s\n",
+					ast_log(LOG_DEBUG, "LDAP RealTime: var_val: %s\n",
 						var_val->value);
-				ast_log(LOG_DEBUG, "LDAP RealTime: cat_metric: %s\n",
+					ast_log(LOG_DEBUG, "LDAP RealTime: cat_metric: %s\n",
 						cat_metric->value);
+				}
 				if (!category) {
 					struct ast_variable *dn = variable_named(*p, "dn");
 					ast_log(LOG_ERROR,
@@ -991,14 +931,12 @@
 
 			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)) {
+					if (!ast_config_internal_load(categories[i].variable_value, cfg)) {
 						break;
 						return NULL;
 					}
 				} else {
-					if (!last_category ||
-						strcmp(last_category, categories[i].name) != 0 ||
+					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) {
@@ -1009,21 +947,19 @@
 						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);
+					new_v = ast_variable_new(categories[i].variable_name,
+							 categories[i].variable_value);
 					ast_variable_append(cur_cat, new_v);
 				}
 			}
 			free(vars);
 			free(categories);
 		} else {
-			ast_log(LOG_WARNING,
-					"LDAP RealTime: Could not find config '%s' in database.\n",
-					file);
+			ast_log(LOG_WARNING, "LDAP RealTime: Could not find config '%s' in database.\n", file);
 		}
 	}
-	ast_config_log("LDAP RealTime: Configuration", cfg);
+	if (option_debug)
+		ast_config_log("LDAP RealTime: Configuration", cfg);
 	return cfg;
 
 }
@@ -1042,15 +978,12 @@
 
 	ast_mutex_lock(&ldap_lock);
 
-	if (!ldap_reconnect()) {
-		ast_log(LOG_WARNING,
-				"LDAP RealTime: Couldn't establish connection. Check debug.\n");
-	}
+	if (!ldap_reconnect()) 
+		ast_log(LOG_WARNING, "LDAP RealTime: Couldn't establish connection. Check debug.\n");
 
 	ast_config_engine_register(&ldap_engine);
-	if (option_verbose) {
+	if (option_verbose) 
 		ast_verbose("LDAP RealTime driver loaded.\n");
-	}
 	ast_cli_register(&cli_realtime_ldap_status);
 
 	ast_mutex_unlock(&ldap_lock);
@@ -1071,9 +1004,8 @@
 	};
 	ast_cli_unregister(&cli_realtime_ldap_status);
 	ast_config_engine_deregister(&ldap_engine);
-	if (option_verbose) {
+	if (option_verbose)
 		ast_verbose("LDAP RealTime unloaded.\n");
-	}
 
 	STANDARD_HANGUP_LOCALUSERS;
 
@@ -1094,10 +1026,8 @@
 	};
 	parse_config();
 
-	if (!ldap_reconnect()) {
-		ast_log(LOG_WARNING,
-				"LDAP RealTime: Couldn't establish connection. Check debug.\n");
-	}
+	if (!ldap_reconnect()) 
+		ast_log(LOG_WARNING, "LDAP RealTime: Couldn't establish connection. Check debug.\n");
 
 	ast_verbose(VERBOSE_PREFIX_2 "LDAP RealTime reloaded.\n");
 
@@ -1116,53 +1046,44 @@
 
 	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");
+			ast_log(LOG_WARNING, "LDAP RealTime: No database user found, anonymous binding as default.\n");
 			dbuser[0] = '\0';
-		} else {
+		} else 
 			strncpy(dbuser, s, sizeof(dbuser) - 1);
-		}
 
 		if (!(s = ast_variable_retrieve(config, "_general", "dbpass"))) {
-			ast_log(LOG_WARNING,
-					"LDAP RealTime: No database password found, using 'asterisk' as default.\n");
+			ast_log(LOG_WARNING, "LDAP RealTime: No database password found, using 'asterisk' as default.\n");
 			strncpy(dbpass, "asterisk", sizeof(dbpass) - 1);
-		} else {
+		} 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 {
+		} 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");
+			ast_log(LOG_ERROR, "LDAP RealTime: No LDAP base dn found, using 'asterisk' as default.\n");
 			dbbasedn[0] = '\0';
-		} else {
+		} 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");
+			ast_log(LOG_WARNING, "LDAP RealTime: No directory port found, using 389 as default.\n");
 			dbport = 389;
-		} else {
+		} 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);
-			ast_log(LOG_DEBUG, "LDAP RealTime: found: category_name=%s\n",
-					category_name);
-			struct ast_variable *var =
-				ast_variable_browse(config, category_name);
+			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);
@@ -1175,15 +1096,13 @@
 						base_table_config = table_config;
 				};
 				while (var) {
-					ast_log(LOG_DEBUG,
-							"LDAP RealTime: found: category_name=%s var->name=%s var->value=%s\n",
+					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) {
+					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;
 				};
 			}
@@ -1215,47 +1134,39 @@
 	/* mutex lock should have been locked before calling this function. */
 
 	if (!ldapConn) {
-		if (dbhost) {
-			ldapConn = ldap_init(dbhost, dbport);
-			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;
-				//ldap_set_option(ldapConn, LDAP_OPT_TIMELIMIT, &timeout);
-				//ldap_set_option(ldapConn, LDAP_OPT_PROTOCOL_VERSION, &version);
 				if (dbuser && *dbuser) {
-					ast_log(LOG_DEBUG, "LDAP RealTime: bind to %s as %s\n",
-							dbhost, dbuser);
-					bind_result =
-						ldap_simple_bind_s(ldapConn, dbuser, dbpass);
+					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 {
-					ast_log(LOG_DEBUG,
-							"LDAP RealTime: bind anonymously %s anonymously\n",
-							dbhost);
+					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) {
-					ast_log(LOG_DEBUG,
-							"LDAP RealTime: Successfully connected to database.\n");
+					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));
+					ast_log(LOG_WARNING, "LDAP RealTime: bind failed: %s\n", ldap_err2string(bind_result));
 					ldap_unbind(ldapConn);
 					ldapConn = NULL;
 					return 0;
 				}
-			} else {
-				ast_log(LOG_ERROR,
-						"LDAP RealTime: Failed to init ldap connection to %s. Check debug for more info.\n",
-						dbhost);
-				return 0;
-			}
-		} else {
-			ast_log(LOG_ERROR,
-					"LDAP RealTime: Not enough parameters to connect to ldap database\n");
+			}
 		}
 	} else {
-		ast_log(LOG_DEBUG, "LDAP RealTime: Everything seems fine.\n");
+		if (option_debug > 1)
+			ast_log(LOG_DEBUG, "LDAP RealTime: Everything seems fine.\n");
 		return 1;
 	}
 }
@@ -1265,42 +1176,35 @@
 	char status[256], status2[100] = "";
 	int ctime = time(NULL) - connect_time;
 
-	if (ldapConn) {
-		if (dbhost) {
-			snprintf(status, 255, "Connected to %s, port %d baseDN %s",
-					 dbhost, dbport, dbbasedn);
-		}
-
-		if (dbuser && *dbuser) {
-			snprintf(status2, 99, " with username %s", dbuser);
-		}
-
-		if (ctime > 31536000) {
-			ast_cli(fd,
-					"%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",
-					status, status2, ctime / 31536000,
-					(ctime % 31536000) / 86400, (ctime % 86400) / 3600,
-					(ctime % 3600) / 60, ctime % 60);
-		} else if (ctime > 86400) {
-			ast_cli(fd,
-					"%s%s for %d days, %d hours, %d minutes, %d seconds.\n",
-					status, status2, ctime / 86400, (ctime % 86400) / 3600,
-					(ctime % 3600) / 60, ctime % 60);
-		} else if (ctime > 3600) {
-			ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n",
-					status, status2, ctime / 3600, (ctime % 3600) / 60,
-					ctime % 60);
-		} else if (ctime > 60) {
-			ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2,
+	if (!ldapConn)
+		return RESULT_FAILURE;
+	if (dbhost) 
+		snprintf(status, 255, "Connected to %s, port %d baseDN %s", dbhost, dbport, dbbasedn);
+
+	if (dbuser && *dbuser)
+		snprintf(status2, 99, " with username %s", dbuser);
+
+	if (ctime > 31536000) {
+		ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",
+				status, status2, ctime / 31536000,
+				(ctime % 31536000) / 86400, (ctime % 86400) / 3600,
+				(ctime % 3600) / 60, ctime % 60);
+	} else if (ctime > 86400) {
+		ast_cli(fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n",
+				status, status2, ctime / 86400, (ctime % 86400) / 3600,
+				(ctime % 3600) / 60, ctime % 60);
+	} else if (ctime > 3600) {
+		ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n",
+				status, status2, ctime / 3600, (ctime % 3600) / 60,
+				ctime % 60);
+	} else if (ctime > 60) {
+		ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2,
 					ctime / 60, ctime % 60);
-		} else {
-			ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
-		}
-
-		return RESULT_SUCCESS;
 	} else {
-		return RESULT_FAILURE;
-	}
+		ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
+	}
+
+	return RESULT_SUCCESS;
 }
 
 STD_MOD(MOD_1, reload, NULL, NULL);



More information about the svn-commits mailing list