[Freeswitch-svn] [commit] r9979 - freeswitch/trunk/src/mod/xml_int/mod_xml_ldap

Freeswitch SVN jskopis at freeswitch.org
Sat Oct 11 18:54:00 EDT 2008


Author: jskopis
Date: Sat Oct 11 18:54:00 2008
New Revision: 9979

Modified:
   freeswitch/trunk/src/mod/xml_int/mod_xml_ldap/mod_xml_ldap.c

Log:
oops

Modified: freeswitch/trunk/src/mod/xml_int/mod_xml_ldap/mod_xml_ldap.c
==============================================================================
--- freeswitch/trunk/src/mod/xml_int/mod_xml_ldap/mod_xml_ldap.c	(original)
+++ freeswitch/trunk/src/mod/xml_int/mod_xml_ldap/mod_xml_ldap.c	Sat Oct 11 18:54:00 2008
@@ -1,11 +1,50 @@
+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005/2006, Anthony Minessale II <anthmct at yahoo.com>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II <anthmct at yahoo.com>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Anthony Minessale II <anthmct at yahoo.com>
+ * Bret McDanel <trixter AT 0xdecafbad.com>
+ * Justin Cassidy <xachenant at hotmail.com>
+ * John Skopis <john+fs at skopis.com>
+ * 
+ * mod_xml_ldap.c -- LDAP XML Gateway
+ *
+ */
 #include <switch.h>
 #include <stdlib.h>
 #include <string.h>
+#ifdef MSLDAP
+#include <windows.h>
+#include <winldap.h>
+#include <winber.h>
+#define LDAP_OPT_SUCCESS LDAP_SUCCESS
+#else
 #include <lber.h>
 #include <ldap.h>
-
-#define PCACHE_TTL 300
-#define NCACHE_TTL 900
+#include <sasl/sasl.h>
+#include "lutil_ldap.h"
+#endif
 
 typedef enum {
 	XML_LDAP_CONFIG = 0,
@@ -14,344 +53,464 @@
 	XML_LDAP_PHRASE
 } xml_ldap_query_type_t;
 
+SWITCH_MODULE_LOAD_FUNCTION(mod_xml_ldap_load);
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_ldap_shutdown);
+SWITCH_MODULE_DEFINITION(mod_xml_ldap, mod_xml_ldap_load, mod_xml_ldap_shutdown, NULL);
 
 typedef struct xml_binding {
 	char *bindings;
-	xml_ldap_query_type_t bt;
-	char *url;
+	char *host;
 	char *basedn;
 	char *binddn;
 	char *bindpass;
 	char *filter;
+	char **attrs;
+	lutilSASLdefaults *defaults;
 } xml_binding_t;
 
+typedef struct ldap_c {
+	LDAP *ld;
+	LDAPMessage *msg;
+	LDAPMessage *entry;
+	BerElement *berkey;
+	BerElement *berval;
+	char *key;
+	char *val;
+	char **keyvals;
+	char **valvals;
+	char *sp;
+} ldap_ctx;
 
-SWITCH_MODULE_LOAD_FUNCTION(mod_xml_ldap_load);
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_ldap_shutdown);
-SWITCH_MODULE_DEFINITION(mod_xml_ldap, mod_xml_ldap_load, mod_xml_ldap_shutdown, NULL);
+static switch_status_t xml_ldap_directory_result(void *ldap_connection, xml_binding_t *binding, switch_xml_t *xml, int *off);
+static switch_status_t xml_ldap_dialplan_result(void *ldap_connection, xml_binding_t *binding, switch_xml_t *xml, int *off);
 
 
-static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params,
-                                    void *user_data);
+#define XML_LDAP_SYNTAX "[debug_on|debug_off]"
 
-static switch_status_t do_config(void);
-static switch_status_t trysearch( switch_xml_t *pxml, int *xoff, LDAP *ld, char *basedn, char *filter);
-void rec( switch_xml_t *, int*, LDAP *ld, char *);
+SWITCH_STANDARD_API(xml_ldap_function)
+{
+	if (session) {
+		return SWITCH_STATUS_FALSE;
+	}
 
-#define XML_LDAP_SYNTAX ""
+	if (switch_strlen_zero(cmd)) {
+		goto usage;
+	}
 
-SWITCH_STANDARD_API(xml_ldap_function)
+	if (!strcasecmp(cmd, "debug_on")) {
+	} else if (!strcasecmp(cmd, "debug_off")) {
+	} else {
+		goto usage;
+	}
+
+	stream->write_function(stream, "OK\n");
+	return SWITCH_STATUS_SUCCESS;
+
+  usage:
+	stream->write_function(stream, "USAGE: %s\n", XML_LDAP_SYNTAX);
+	return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t xml_ldap_result(void *ldap_connection, xml_binding_t *binding, switch_xml_t *xml, int *off, const xml_ldap_query_type_t query_type)
+{
+	switch (query_type) {
+	case XML_LDAP_DIRECTORY:
+		return xml_ldap_directory_result(ldap_connection, binding, xml, off);
+
+	case XML_LDAP_DIALPLAN:
+		return xml_ldap_dialplan_result(ldap_connection, binding, xml, off);
+
+	default:
+		return SWITCH_STATUS_FALSE;
+	}
+}
+
+static switch_status_t xml_ldap_dialplan_result(void *ldap_connection, xml_binding_t *binding, switch_xml_t *xml, int *off)
 {
 	return SWITCH_STATUS_FALSE;
 }
 
-SWITCH_MODULE_LOAD_FUNCTION(mod_xml_ldap_load)
+static switch_status_t xml_ldap_directory_result(void *ldap_connection, xml_binding_t *binding, switch_xml_t *xml, int *off)
 {
-	switch_api_interface_t *xml_ldap_api_interface;
+	struct ldap_c *ldap = ldap_connection;
+	switch_xml_t asdf = *xml;
+	switch_xml_t param, variable, params = NULL, variables = NULL;
+	int i = 0;
+	int loff = *off;
+
+	for (ldap->entry = ldap_first_entry(ldap->ld, ldap->msg); ldap->entry != NULL; ldap->entry = ldap_next_entry(ldap->ld, ldap->entry)) {
+		ldap->key = ldap_first_attribute(ldap->ld, ldap->entry, &ldap->berkey);
+		do {
+			ldap->val = ldap_first_attribute(ldap->ld, ldap->entry, &ldap->berval);
+			do {
+				if (strstr(ldap->val, "value")) {
+					if (strstr(ldap->val, ldap->key) && strcmp(ldap->val, ldap->key)) {
+						if (!strcmp(ldap->key, "param")) {
+							params = switch_xml_add_child_d(asdf, "params", loff++);
+						} else if (!strcmp(ldap->key, "variable")) {
+							variables = switch_xml_add_child_d(asdf, "variables", loff++);
+						}
+
+						ldap->keyvals = ldap_get_values(ldap->ld, ldap->entry, ldap->key);
+						ldap->valvals = ldap_get_values(ldap->ld, ldap->entry, ldap->val);
+
+						if (ldap->keyvals && ldap->valvals) {
+							if (ldap_count_values(ldap->valvals) == ldap_count_values(ldap->keyvals)) {
+								for (i = 0; ldap->keyvals[i] != NULL && ldap->valvals[i] != NULL; i++) {
+									if (!strcmp(ldap->key, "param")) {
+										param = switch_xml_add_child_d(params, "param", loff++);
+										switch_xml_set_attr_d(param, "name", ldap->keyvals[i]);
+										switch_xml_set_attr_d(param, "value", ldap->valvals[i]);
+									} else if (!strcmp(ldap->key, "variable")) {
+										variable = switch_xml_add_child_d(variables, "variable", loff++);
+										switch_xml_set_attr_d(variable, "name", ldap->keyvals[i]);
+										switch_xml_set_attr_d(variable, "value", ldap->valvals[i]);
+									}
+								}
+
+								if (ldap->keyvals) {
+									ldap_value_free(ldap->keyvals);
+								}
+
+								if (ldap->valvals) {
+									ldap_value_free(ldap->valvals);
+								}
+							} else {
+								switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Seems the values %d and %d are not the same??\n",
+												  ldap_count_values(ldap->valvals), ldap_count_values(ldap->keyvals));
+							}
+						}
+					}
+				}
+				if (ldap->val) {
+					ldap_memfree(ldap->val);
+				}
 
-	/* connect my internal structure to the blank pointer passed to me */
-	*module_interface = switch_loadable_module_create_module_interface(pool, modname);
+				ldap->val = ldap_next_attribute(ldap->ld, ldap->entry, ldap->berval);
 
-	SWITCH_ADD_API(xml_ldap_api_interface, "xml_ldap", "XML LDAP", xml_ldap_function, XML_LDAP_SYNTAX);
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "XML LDAP module loading...\n");
+			} while (ldap->val != NULL);
 
-	if (do_config() != SWITCH_STATUS_SUCCESS) {
-		return SWITCH_STATUS_FALSE;
-	}
+			if (ldap->key) {
+				ldap_memfree(ldap->key);
+			}
 
+			if (ldap->berval) {
+				ber_free(ldap->berval, 0);
+			}
 
-	/* indicate that the module should continue to be loaded */
+			ldap->key = ldap_next_attribute(ldap->ld, ldap->entry, ldap->berkey);
+
+		} while (ldap->key != NULL);
+
+		if (ldap->berkey) {
+			ber_free(ldap->berkey, 0);
+		}
+	}
 	return SWITCH_STATUS_SUCCESS;
 }
 
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_ldap_shutdown)
+
+static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params,
+									void *user_data)
 {
-	return SWITCH_STATUS_SUCCESS;
-}
+	xml_binding_t *binding = (xml_binding_t *) user_data;
+	switch_event_header_t *hi;
 
-static switch_status_t do_config(void) {
-	char *cf = "xml_ldap.conf";
-	switch_xml_t cfg, xml, bindings_tag, binding_tag, param;
-	xml_binding_t *binding = NULL;
-	int x = 0;
+	switch_xml_t xml = NULL, sub = NULL;
 
-	if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
-		return SWITCH_STATUS_TERM;
-	}
+	struct ldap_c ldap_connection;
+	struct ldap_c *ldap = &ldap_connection;
 
-	if (!(bindings_tag = switch_xml_child(cfg, "bindings"))) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing <bindings> tag!\n");
-		goto done;
-	}
+	int auth_method = LDAP_AUTH_SIMPLE;
+	int desired_version = LDAP_VERSION3;
+	xml_ldap_query_type_t query_type;
+	char *dir_exten = NULL, *dir_domain = NULL;
 
-	for (binding_tag = switch_xml_child(bindings_tag, "binding"); binding_tag; binding_tag = binding_tag->next) {
-		char *bname = (char *) switch_xml_attr_soft(binding_tag, "name");
+	char *search_filter = NULL, *search_base = NULL;
+	int off = 0, ret = 1;
 
-		if (!(binding = malloc(sizeof(*binding)))) {
-			goto done;
-		}
-		memset(binding, 0, sizeof(*binding));
+	//char *buf;
+	//buf = malloc(4096);
 
-		for (param = switch_xml_child(binding_tag, "param"); param; param = param->next) {
 
-			char *var = (char *) switch_xml_attr_soft(param, "name");
-			char *val = (char *) switch_xml_attr_soft(param, "value");
+	if (!binding) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No bindings...sorry bud returning now\n");
+		return NULL;
+	}
 
-			if (!strcasecmp(var, "filter")) {
-				binding->bindings = (char *) switch_xml_attr_soft(param, "bindings");
+	if (!strcmp(section, "configuration")) {
+		query_type = XML_LDAP_CONFIG;
+	} else if (!strcmp(section, "directory")) {
+		query_type = XML_LDAP_DIRECTORY;
+	} else if (!strcmp(section, "dialplan")) {
+		query_type = XML_LDAP_DIALPLAN;
+	} else if (!strcmp(section, "phrases")) {
+		query_type = XML_LDAP_PHRASE;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid section\n");
+		return NULL;
+	}
 
-				if (!strncmp(binding->bindings, "configuration",strlen(binding->bindings))) {
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setting type XML_LDAP_CONFIG\n");
-					binding->bt = XML_LDAP_CONFIG;
-				} else if (!strncmp(binding->bindings, "directory",strlen(binding->bindings))) {
-					binding->bt = XML_LDAP_DIRECTORY;
-				} else if (!strncmp(binding->bindings, "dialplain",strlen(binding->bindings))) {
-					binding->bt = XML_LDAP_DIALPLAN;
-				} else if (!strncmp(binding->bindings, "phrases",strlen(binding->bindings))) {
-					binding->bt = XML_LDAP_PHRASE;
-				}
+	if (params) {
+		if ((hi = params->headers)) {
+			for (; hi; hi = hi->next) {
+				switch (query_type) {
+				case XML_LDAP_CONFIG:
+					break;
 
-				if (val) {
-					binding->filter = strdup(val);
-					printf("binding filter %s to %s\n", binding->filter, binding->bindings);
+				case XML_LDAP_DIRECTORY:
+					if (!strcmp(hi->name, "user")) {
+						dir_exten = strdup(hi->value);
+					} else if (!strcmp(hi->name, "domain")) {
+						dir_domain = strdup(hi->value);
+					}
+					break;
+
+				case XML_LDAP_DIALPLAN:
+				case XML_LDAP_PHRASE:
+					break;
 				}
-			} else if (!strncasecmp(var, "basedn", strlen(val))) {
-				binding->basedn = strdup(val);
-			} else if (!strncasecmp(var, "binddn", strlen(val))) {
-				binding->binddn = strdup(val);
-			} else if (!strncasecmp(var, "bindpass", strlen(val))) {
-				binding->bindpass = strdup(val);
-			} else if (!strncasecmp(var, "url", strlen(val))) {
-				binding->url = strdup(val);
 			}
+			switch (query_type) {
+			case XML_LDAP_CONFIG:
+				break;
+
+			case XML_LDAP_DIRECTORY:
+				if (dir_exten && dir_domain) {
+					if ((xml = switch_xml_new("directory"))) {
+						switch_xml_set_attr_d(xml, "type", "freeswitch/xml");
+
+						if ((sub = switch_xml_add_child_d(xml, "section", off++))) {
+							switch_xml_set_attr_d(sub, "name", "directory");
+						}
+
+						if ((sub = switch_xml_add_child_d(sub, "domain", off++))) {
+							switch_xml_set_attr_d(sub, "name", dir_domain);
+						}
+
+						if ((sub = switch_xml_add_child_d(sub, "user", off++))) {
+							switch_xml_set_attr_d(sub, "id", dir_exten);
+						}
 
-		}
-
-		if (!binding->basedn || !binding->filter || !binding->url) {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
-							  "You must define \"basedn\", and \"filter\" in mod_xml_ldap.conf.xml\n");
-			continue;
-		}
+					}
 
+					search_filter = switch_mprintf(binding->filter, dir_exten);
+					search_base = switch_mprintf(binding->basedn, dir_domain);
 
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding [%s] XML Fetch Function [%s] (%s) [%s]\n",
-						  switch_strlen_zero(bname) ? "N/A" : bname, binding->basedn, binding->filter, binding->bindings ? binding->bindings : "all");
+					free(dir_exten);
+					dir_exten = NULL;
 
-		switch_xml_bind_search_function(xml_ldap_search, switch_xml_parse_section_string(bname), binding);
+					free(dir_domain);
+					dir_domain = NULL;
 
-		x++;
-		binding = NULL;
-	}
+				} else {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
+									  "Something bad happened during the query construction phase likely exten(%s) or domain(%s) is null\n", dir_exten,
+									  dir_domain);
+					goto cleanup;
+				}
+				break;
 
-  done:
-	switch_xml_free(xml);
+			case XML_LDAP_DIALPLAN:
+				if ((xml = switch_xml_new("document"))) {
+					switch_xml_set_attr_d(xml, "type", "freeswitch/xml");
 
-	return SWITCH_STATUS_SUCCESS;
-}
+					if ((sub = switch_xml_add_child_d(xml, "section", off++))) {
+						switch_xml_set_attr_d(sub, "name", "dialplan");
+					}
 
-static switch_status_t trysearch ( switch_xml_t *pxml, int *xoff, LDAP *ld, char *basedn, char *filter) {
-	switch_status_t ret;
-	int off = *xoff;
-	char *key = NULL;
-	char *dn = NULL;
-	char **val = NULL;
-	BerElement *ber = NULL;
-	switch_xml_t xml = *pxml;
-	LDAPMessage *msg, *entry;
-
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "trying search in base %s with filter %s\n", basedn, filter);
-
-	ldap_search_s(ld, basedn,  LDAP_SCOPE_ONE, filter, NULL, 0, &msg);
-
-	if ( ldap_count_entries(ld, msg) > 0 ) {
-		ret = SWITCH_STATUS_SUCCESS;
-		for ( 
-			entry = ldap_first_entry(ld, msg); 
-			entry != NULL; 
-			entry = ldap_next_entry(ld, entry) ) {
-
-			val = ldap_get_values(ld,entry,"fstag" );
-			xml = switch_xml_add_child_d(xml, val[0], off);
-			ldap_value_free(val);
-
-			for (
-				key = ldap_first_attribute(ld, entry, &ber);
-				key != NULL;
-				key = ldap_next_attribute(ld, entry, ber) ) {
-
-				if ( !strncasecmp(key,"fstag",strlen(key)) || !strncasecmp(key,"objectclass",strlen(key)) ) {
-					ldap_memfree(key);
-					continue;
+					sub = switch_xml_add_child_d(xml, "context", off++);
 				}
 
-				val = ldap_get_values(ld,entry,key);
-				switch_xml_set_attr_d(xml, key, val[0]);
-
-				ldap_memfree(key);
-				ldap_value_free(val);
+				break;
 
+			case XML_LDAP_PHRASE:
+				break;
 			}
-			ber_free(ber,0);
-
-			dn = ldap_get_dn(ld,entry);
-		    rec(&xml,&off,ld,dn);
-
-			*xoff=1;
+		} else {
+			goto cleanup;
 		}
-
-		ldap_msgfree(entry);
-		ldap_msgfree(msg);
-	} else {
-		ret = SWITCH_STATUS_FALSE;
 	}
 
-	switch_safe_free(filter);
-	switch_safe_free(key);
 
-	return ret;
-	
-}
 
+	if ((ldap->ld = (LDAP*)ldap_init(binding->host, LDAP_PORT)) == NULL) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to connect to ldap server.%s\n", binding->host);
+		goto cleanup;
+	}
 
+	if (ldap_set_option(ldap->ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) != LDAP_OPT_SUCCESS) {
+		goto cleanup;
+	}
 
+	ldap_set_option( ldap->ld, LDAP_OPT_X_SASL_SECPROPS, &ldap->sp );
 
-void rec( switch_xml_t *pxml, int *xoff, LDAP *ld, char *dn) {
-	int off = *xoff;
-	char *key;
-	char **val;
 
-	switch_xml_t xml = *pxml, new;
 
-	LDAPMessage *msg, *entry;
-	BerElement *ber;
+	if (binding->binddn) {
+		if (ldap_bind_s(ldap->ld, binding->binddn, binding->bindpass, auth_method) != LDAP_SUCCESS) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to bind to ldap server %s as %s\n", binding->host, binding->binddn);
+			goto cleanup;
+		}
+	} else {
+		if (ldap_sasl_interactive_bind_s( ldap->ld, NULL, binding->defaults->mech, NULL, NULL,(unsigned)LDAP_SASL_SIMPLE, lutil_sasl_interact , binding->defaults ) != LDAP_SUCCESS) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to sasl_bind to ldap server %s as %s\n", binding->host, binding->defaults->authcid);
+			goto cleanup;
+		}
+	}
 
-	ldap_search_s(ld, dn, LDAP_SCOPE_ONE, NULL, NULL, 0, &msg);
-	switch_safe_free(dn);
+	if (ldap_search_s(ldap->ld, search_base, LDAP_SCOPE_SUBTREE, search_filter, NULL, 0, &ldap->msg) != LDAP_SUCCESS) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Query failed: -b \"%s\" \"%s\"\n", search_base, search_filter);
+		goto cleanup;
+	}
 
-	if ( ldap_count_entries(ld, msg) > 0 ) {
+	if (ldap_count_entries(ldap->ld, ldap->msg) <= 0) {
+		goto cleanup;
+	}
 
-		for ( 
-			entry = ldap_first_entry(ld, msg);
-			entry != NULL;
-			entry = ldap_next_entry(ld, entry) ) {
+	if (sub && xml_ldap_result(&ldap_connection, binding, &sub, &off, query_type) != SWITCH_STATUS_SUCCESS) {
+		goto cleanup;
+	}
 
-			val = ldap_get_values(ld,entry,"fstag" );
-			new  = switch_xml_add_child_d(xml, val[0], off);
-			ldap_value_free(val);
+	ret = 0;
 
-			for (
-				key = ldap_first_attribute(ld, entry,&ber);
-				key != NULL;
-				key = ldap_next_attribute(ld,entry,ber) ) {
+  cleanup:
+	if (ldap->msg) {
+		ldap_msgfree(ldap->msg);
+	}
 
-				if ( !strncasecmp("fstag",key,5) || !strncasecmp("objectclass",key,10) ) {
-					ldap_memfree(key);
-					continue;
-				}
+	if (ldap->ld) {
+		ldap_unbind_s(ldap->ld);
+	}
 
-                val = ldap_get_values(ld,entry,key);
-                switch_xml_set_attr_d(new, key, val[0]);
-				ldap_memfree(key);
-				ldap_value_free(val);
-			}
-			ber_free(ber,0);
-			rec( &new, xoff , ld, ldap_get_dn(ld,entry) );
-		}
+	switch_safe_free(search_filter);
+	switch_safe_free(search_base);
 
-		ldap_msgfree(entry);
+	//switch_xml_toxml_buf(xml,buf,0,0,1);
+	//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Providing:\n%s\n", buf);
 
+	if (ret) {
+		switch_xml_free(xml);
+		return NULL;
 	}
-	ldap_msgfree(msg);
+
+	return xml;
 }
 
 
-static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params,
-									void *user_data) {
+static switch_status_t do_config(void)
+{
+	char *cf = "xml_ldap.conf";
+	switch_xml_t cfg, xml, bindings_tag, binding_tag, param;
+	xml_binding_t *binding = NULL;
+	int x = 0;
 
-	xml_binding_t *binding = (xml_binding_t *) user_data;
-	switch_event_header_t *hi;
+	if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
+		return SWITCH_STATUS_TERM;
+	}
 
-	int desired_version = LDAP_VERSION3;
-	int auth_method = LDAP_AUTH_SIMPLE;
+	if (!(bindings_tag = switch_xml_child(cfg, "bindings"))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing <bindings> tag!\n");
+		goto done;
+	}
 
-	char *basedn = NULL, *filter = NULL;
-	char *dir_domain = NULL, *dir_exten = NULL;
-	
-	char *buf;        
-	buf = malloc(4096);
+	for (binding_tag = switch_xml_child(bindings_tag, "binding"); binding_tag; binding_tag = binding_tag->next) {
+		char *bname = (char *) switch_xml_attr_soft(binding_tag, "name");
 
-	LDAP *ld;
-	switch_xml_t xml = NULL;
+		if (!(binding = malloc(sizeof(*binding)))) {
+			goto done;
+		}
+		memset(binding, 0, sizeof(*binding));
 
-	int xoff = 0;
+		if (!(binding->defaults = malloc(sizeof(lutilSASLdefaults)))) {
+			goto done;
+		}
+		memset(binding->defaults, 0, sizeof(lutilSASLdefaults));
 
-	if (params) {
-		if ((hi = params->headers)) {
-			for (; hi; hi = hi->next) {
-				switch (binding->bt) {
-				case XML_LDAP_CONFIG:
-					break;
+		for (param = switch_xml_child(binding_tag, "param"); param; param = param->next) {
 
-				case XML_LDAP_DIRECTORY:
-					if (!strncmp(hi->name, "user", strlen(hi->name))) {
-						dir_exten = strdup(hi->value);
-					} else if (!strncmp(hi->name, "domain", strlen(hi->name))) {
-						dir_domain = strdup(hi->value);
-					}
-					break;
+			char *var = (char *) switch_xml_attr_soft(param, "name");
+			char *val = (char *) switch_xml_attr_soft(param, "value");
 
-				case XML_LDAP_DIALPLAN:
-				case XML_LDAP_PHRASE:
-					break;
+			if (!strcasecmp(var, "filter")) {
+				binding->bindings = (char *) switch_xml_attr_soft(param, "bindings");
+				if (val) {
+					binding->filter = strdup(val);
 				}
+			} else if (!strcasecmp(var, "basedn")) {
+				binding->basedn = strdup(val);
+			} else if (!strcasecmp(var, "binddn")) {
+				binding->binddn = strdup(val);
+			} else if (!strcasecmp(var, "bindpass")) {
+				binding->bindpass = strdup(val);
+			} else if (!strcasecmp(var, "host")) {
+				binding->host = strdup(val);
+			} else if (!strcasecmp(var, "mech")) {
+				binding->defaults->mech = strdup(val);
+			} else if (!strcasecmp(var, "realm")) {
+				binding->defaults->realm = strdup(val);
+			} else if (!strcasecmp(var, "authcid")) {
+				binding->defaults->authcid = strdup(val);
+			} else if (!strcasecmp(var, "authzid")) {
+				binding->defaults->authzid = strdup(val);
 			}
+
+		}
+
+		if (!binding->basedn || !binding->filter) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
+							  "You must define \"basedn\", and \"filter\" in mod_xml_ldap.conf.xml\n");
+			continue;
 		}
-	}
-	switch (binding->bt) {
-		case XML_LDAP_CONFIG:
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "humm %s", binding->filter);
-			filter = switch_mprintf(binding->filter,key_name,key_value);
-			basedn = switch_mprintf(binding->basedn,tag_name);
-			break;
-
-		case XML_LDAP_DIRECTORY:
-			if(!dir_exten) {
-				filter = switch_mprintf(binding->filter,"objectclass","*","(!(objectclass=fsuser))");
-				basedn = switch_mprintf(binding->basedn,dir_domain);
-			} else {
-				filter = switch_mprintf(binding->filter,key_name,key_value,"object_class=*");
-				basedn = switch_mprintf(binding->basedn,dir_domain);
-			}
-			break;
 
-		case XML_LDAP_DIALPLAN:
-			break;
 
-		case XML_LDAP_PHRASE:
-			break;
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding [%s] XML Fetch Function [%s] (%s) [%s]\n",
+						  switch_strlen_zero(bname) ? "N/A" : bname, binding->basedn, binding->filter, binding->bindings ? binding->bindings : "all");
+
+		switch_xml_bind_search_function(xml_ldap_search, switch_xml_parse_section_string(bname), binding);
+
+		x++;
+		binding = NULL;
 	}
 
+  done:
+	switch_xml_free(xml);
 
-	ldap_initialize(&ld,binding->url);
-	ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version);
-	ldap_bind_s(ld, binding->binddn, binding->bindpass, auth_method);
+	return SWITCH_STATUS_SUCCESS;
+}
 
-	xml = switch_xml_new("document");
-	switch_xml_set_attr_d(xml, "type", "freeswitch/xml");
 
+SWITCH_MODULE_LOAD_FUNCTION(mod_xml_ldap_load)
+{
+	switch_api_interface_t *xml_ldap_api_interface;
 
-	
-	trysearch(&xml,&xoff,ld, basedn, filter);
+	/* connect my internal structure to the blank pointer passed to me */
+	*module_interface = switch_loadable_module_create_module_interface(pool, modname);
 
-	ldap_unbind_s(ld);
+	SWITCH_ADD_API(xml_ldap_api_interface, "xml_ldap", "XML LDAP", xml_ldap_function, XML_LDAP_SYNTAX);
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "XML LDAP module loading...\n");
 
+	if (do_config() != SWITCH_STATUS_SUCCESS) {
+		return SWITCH_STATUS_FALSE;
+	}
 
+	/* indicate that the module should continue to be loaded */
+	return SWITCH_STATUS_SUCCESS;
+}
 
-    switch_xml_toxml_buf(xml,buf,0,0,1);
-    printf("providing:\n%s\n", buf);
-	switch_safe_free(buf);
-	
-	return xml;
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_ldap_shutdown)
+{
+	return SWITCH_STATUS_SUCCESS;
 }
 
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */



More information about the Freeswitch-svn mailing list