Added patch from stkn__ to cleanup code. Thanks stkn__

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7509 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
John Skopis 2008-02-04 03:50:45 +00:00
parent 814b6ba0a1
commit 5c20731484

View File

@ -1,4 +1,4 @@
/*
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
@ -22,12 +22,12 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
*
* Anthony Minessale II <anthmct@yahoo.com>
* Bret McDanel <trixter AT 0xdecafbad.com>
* Justin Cassidy <xachenant@hotmail.com>
* John Skopis <john+fs@skopis.com>
*
*
* mod_xml_ldap.c -- LDAP XML Gateway
*
*/
@ -44,45 +44,47 @@
#include <ldap.h>
#endif
#define XML_LDAP_CONFIG 0
#define XML_LDAP_DIRECTORY 1
#define XML_LDAP_DIALPLAN 2
#define XML_LDAP_PHRASE 3
typedef enum {
XML_LDAP_CONFIG = 0,
XML_LDAP_DIRECTORY,
XML_LDAP_DIALPLAN,
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);
struct xml_binding {
char *bindings;
char *host;
char *bindings;
char *host;
char *ldap_base;
char *binddn;
char *bindpass;
char *queryfmt;
char *binddn;
char *bindpass;
char *queryfmt;
};
typedef struct xml_binding xml_binding_t;
struct ldap_c {
LDAP *ld;
LDAPMessage *msg;
LDAPMessage *entry;
BerElement *berkey;
BerElement *berval;
char *key;
char *val;
char **keyvals;
char **valvals;
LDAP *ld;
LDAPMessage *msg;
LDAPMessage *entry;
BerElement *berkey;
BerElement *berval;
char *key;
char *val;
char **keyvals;
char **valvals;
};
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_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);
#define XML_LDAP_SYNTAX "[debug_on|debug_off]"
SWITCH_STANDARD_API(xml_ldap_function)
{
if (session) {
@ -94,8 +96,10 @@ SWITCH_STANDARD_API(xml_ldap_function)
}
if (!strcasecmp(cmd, "debug_on")) {
} else if (!strcasecmp(cmd, "debug_off")) {
} else {
}
else if (!strcasecmp(cmd, "debug_off")) {
}
else {
goto usage;
}
@ -107,207 +111,256 @@ usage:
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t xml_ldap_result( void *ldap_connection, xml_binding_t *binding, switch_xml_t *xml, int *off, int qt)
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_status_t ret;
if (qt == 1 ) ret = xml_ldap_directory_result(ldap_connection, binding, xml , off);
else if (qt == 2 ) ret = xml_ldap_dialplan_result(ldap_connection, binding, xml , off);
return ret;
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)
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;
return SWITCH_STATUS_FALSE;
}
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_directory_result(void *ldap_connection, xml_binding_t *binding, switch_xml_t *xml, int *off)
{
struct ldap_c *ldap = ldap_connection;
switch_xml_t asdf = *xml;
switch_xml_t param, variable, params, variables;
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++);
}
switch_xml_t asdf = *xml;
switch_xml_t param, variable, params, variables;
int i = 0;
int loff = *off;
if( (ldap->keyvals = ldap_get_values( ldap->ld, ldap->entry, ldap->key )) && (ldap->valvals = ldap_get_values( ldap->ld, ldap->entry, ldap->val )) ) {
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);
ldap->val = ldap_next_attribute(ldap->ld, ldap->entry,ldap->berval);
} while (ldap->val != NULL);
if (ldap->key) ldap_memfree(ldap->key);
if (ldap->berval) ber_free(ldap->berval,0);
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;
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);
}
ldap->val = ldap_next_attribute(ldap->ld, ldap->entry, ldap->berval);
} while (ldap->val != NULL);
if (ldap->key) {
ldap_memfree(ldap->key);
}
if (ldap->berval) {
ber_free(ldap->berval, 0);
}
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;
}
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_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)
{
xml_binding_t *binding = (xml_binding_t *) user_data;
switch_event_header_t *hi;
switch_xml_t xml, sub;
struct ldap_c ldap_connection;
struct ldap_c *ldap = &ldap_connection;
int auth_method = LDAP_AUTH_SIMPLE;
int desired_version = LDAP_VERSION3;
int query_type; // see defines on top XML_LDAP_foo
char *dir_exten, *dir_domain;
char *filter = "(objectClass=*)";
xml_binding_t *binding = (xml_binding_t *)user_data;
switch_event_header_t *hi;
char *search_base = NULL;
int off = 0;
switch_xml_t xml, sub;
struct ldap_c ldap_connection;
struct ldap_c *ldap = &ldap_connection;
int auth_method = LDAP_AUTH_SIMPLE;
int desired_version = LDAP_VERSION3;
xml_ldap_query_type_t query_type;
char *dir_exten, *dir_domain;
char *filter = "(objectClass=*)";
char *search_base = NULL;
int off = 0;
if (!binding) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "no bindings...sorry bud returning now\n");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "no bindings...sorry bud returning now\n");
return NULL;
}
if(!strcmp(section,"configuration")) {
query_type = 0;
}
else if (!strcmp(section,"directory")) {
query_type = 1;
}
else if (!strcmp(section,"dialplan")) {
query_type = 2;
}
else if (!strcmp(section,"phrases")) {
query_type = 3;
}
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;
}
if (params) {
if ((hi = params->headers)) {
for (; hi; hi = hi->next) {
switch(query_type) {
case XML_LDAP_CONFIG:
break;
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;
}
}
switch(query_type) {
case XML_LDAP_CONFIG:
break;
case XML_LDAP_DIRECTORY:
if (dir_exten && dir_domain) {
xml = switch_xml_new("directory");
switch_xml_set_attr_d(xml, "type", "freeswitch/xml");
sub = switch_xml_add_child_d(xml, "section", off++);
switch_xml_set_attr_d(sub, "name", "directory");
sub = switch_xml_add_child_d(sub, "domain", off++);
switch_xml_set_attr_d(sub, "name", dir_domain);
sub = switch_xml_add_child_d(sub, "user", off++);
switch_xml_set_attr_d(sub, "id", dir_exten);
search_base = switch_mprintf(binding->queryfmt, dir_exten, dir_domain, binding->ldap_base);
free(dir_exten);
dir_exten = NULL;
free(dir_domain);
dir_domain = 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;
case XML_LDAP_DIALPLAN:
xml = switch_xml_new("document");
switch_xml_set_attr_d(xml, "type", "freeswitch/xml");
sub = switch_xml_add_child_d(xml, "section", off++);
switch_xml_set_attr_d(sub, "name", "dialplan");
sub = switch_xml_add_child_d(xml, "context", off++);
search_base = switch_mprintf(binding->queryfmt, dir_exten, dir_domain, binding->ldap_base);
break;
case XML_LDAP_PHRASE:
break;
}
}
else {
goto cleanup;
}
}
if (params) {
if ((hi = params->headers)) {
for (; hi; hi = hi->next) {
switch(query_type) {
case XML_LDAP_CONFIG:
break;
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:
break;
case XML_LDAP_PHRASE:
break;
}
}
switch(query_type) {
case XML_LDAP_CONFIG:
break;
case XML_LDAP_DIRECTORY:
if ( dir_exten && dir_domain ) {
xml = switch_xml_new("directory");
switch_xml_set_attr_d(xml,"type","freeswitch/xml");
sub = switch_xml_add_child_d(xml,"section",off++);
switch_xml_set_attr_d(sub,"name","directory");
sub = switch_xml_add_child_d(sub,"domain",off++);
switch_xml_set_attr_d(sub,"name",dir_domain);
sub = switch_xml_add_child_d(sub,"user",off++);
switch_xml_set_attr_d(sub,"id",dir_exten);
if ((ldap->ld = 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;
}
search_base = switch_mprintf(binding->queryfmt,dir_exten,dir_domain,binding->ldap_base);
free(dir_exten);
dir_exten = NULL;
free(dir_domain);
dir_domain = 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;
case XML_LDAP_DIALPLAN:
xml = switch_xml_new("document");
switch_xml_set_attr_d(xml,"type","freeswitch/xml");
sub = switch_xml_add_child_d(xml,"section",off++);
switch_xml_set_attr_d(sub,"name","dialplan");
sub = switch_xml_add_child_d(xml,"context",off++);
search_base = switch_mprintf(binding->queryfmt,dir_exten,dir_domain,binding->ldap_base);
break;
case XML_LDAP_PHRASE:
break;
}
}
else {
goto cleanup;
}
}
if (ldap_set_option(ldap->ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) != LDAP_OPT_SUCCESS) {
goto cleanup;
}
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;
}
if ((ldap->ld = 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_search_s(ldap->ld, search_base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &ldap->msg) != LDAP_SUCCESS) {
goto cleanup;
}
if (ldap_set_option(ldap->ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) != LDAP_OPT_SUCCESS) {
goto cleanup;
}
if (ldap_count_entries(ldap->ld, ldap->msg) <= 0) {
goto cleanup;
}
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;
}
if (xml_ldap_result(&ldap_connection, binding, &sub, &off, query_type) != SWITCH_STATUS_SUCCESS) {
goto cleanup;
}
if (ldap_search_s(ldap->ld, search_base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &ldap->msg) != LDAP_SUCCESS) {
goto cleanup;
}
cleanup:
if (ldap->msg) {
ldap_msgfree(ldap->msg);
}
if (ldap_count_entries(ldap->ld, ldap->msg) <= 0) {
goto cleanup;
}
if (ldap->ld) {
ldap_unbind_s(ldap->ld);
}
if ( xml_ldap_result( &ldap_connection, binding, &sub, &off, query_type) != SWITCH_STATUS_SUCCESS ){
goto cleanup;
}
switch_safe_free(search_base);
cleanup:
if (ldap->msg) ldap_msgfree(ldap->msg);
if (ldap->ld) ldap_unbind_s(ldap->ld);
switch_safe_free(search_base);
return xml;
return xml;
}
@ -331,47 +384,51 @@ static switch_status_t do_config(void)
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");
if (!(binding = malloc(sizeof(*binding)))) {
goto done;
}
memset(binding, 0, sizeof(*binding));
if (!(binding = malloc(sizeof(*binding)))) {
goto done;
}
memset(binding, 0, sizeof(*binding));
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 (!strcasecmp(var, "queryfmt")) {
binding->bindings = (char *) switch_xml_attr_soft(param, "bindings");
if(val) binding->queryfmt = strdup(val);
} else if (!strcasecmp(var, "base")) {
binding->ldap_base = strdup(val);
} else if (!strcasecmp(var, "binddn")) {
if (!strcasecmp(var, "queryfmt")) {
binding->bindings = (char *) switch_xml_attr_soft(param, "bindings");
if (val) {
binding->queryfmt = strdup(val);
}
}
else if (!strcasecmp(var, "base")) {
binding->ldap_base = 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, "bindpass")) {
binding->bindpass = strdup(val);
}
else if (!strcasecmp(var, "host")) {
binding->host = strdup(val);
}
}
if (!binding->ldap_base || !binding->binddn || !binding->bindpass ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "You must define base binddn bindpass\n");
if (!binding->ldap_base || !binding->binddn || !binding->bindpass || !binding->queryfmt) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "You must define \"base\", \"binddn\", \"bindpass\", and \"queryfmt\" in mod_xml_ldap.conf.xml\n");
continue;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding [%s] XML Fetch Function [%s] [%s]\n",
switch_strlen_zero(bname) ? "N/A" : bname, binding->ldap_base, binding->bindings ? binding->bindings : "all");
switch_strlen_zero(bname) ? "N/A" : bname, binding->ldap_base, binding->bindings ? binding->bindings : "all");
switch_xml_bind_search_function(xml_ldap_search, switch_xml_parse_section_string(bname), binding);
x++;
binding = NULL;
}
done:
done:
switch_xml_free(xml);
return SWITCH_STATUS_SUCCESS;
@ -386,9 +443,11 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_xml_ldap_load)
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
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_ERROR, "XML LDAP module loading...\n");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "XML LDAP module loading...\n");
if (! (do_config() == SWITCH_STATUS_SUCCESS) ) return SWITCH_STATUS_FALSE;
if (do_config() != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;