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
1 changed files with 282 additions and 223 deletions

View File

@ -44,16 +44,17 @@
#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;
@ -83,6 +84,7 @@ static switch_status_t xml_ldap_dialplan_result( void *ldap_connection, xml_bind
#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,12 +111,18 @@ 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)
@ -137,11 +147,15 @@ static switch_status_t xml_ldap_directory_result( void *ldap_connection, xml_bin
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")) {
}
else if (!strcmp(ldap->key, "variable")) {
variables = switch_xml_add_child_d(asdf, "variables", loff++);
}
if( (ldap->keyvals = ldap_get_values( ldap->ld, ldap->entry, ldap->key )) && (ldap->valvals = ldap_get_values( ldap->ld, ldap->entry, ldap->val )) ) {
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")) {
@ -155,8 +169,14 @@ static switch_status_t xml_ldap_directory_result( void *ldap_connection, xml_bin
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);
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));
@ -164,14 +184,29 @@ static switch_status_t xml_ldap_directory_result( void *ldap_connection, xml_bin
}
}
}
if (ldap->val) ldap_memfree(ldap->val);
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);
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);
if (ldap->berkey) {
ber_free(ldap->berkey, 0);
}
}
return SWITCH_STATUS_SUCCESS;
}
@ -189,7 +224,7 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
int auth_method = LDAP_AUTH_SIMPLE;
int desired_version = LDAP_VERSION3;
int query_type; // see defines on top XML_LDAP_foo
xml_ldap_query_type_t query_type;
char *dir_exten, *dir_domain;
char *filter = "(objectClass=*)";
@ -203,31 +238,35 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
}
if (!strcmp(section,"configuration")) {
query_type = 0;
query_type = XML_LDAP_CONFIG;
}
else if (!strcmp(section,"directory")) {
query_type = 1;
query_type = XML_LDAP_DIRECTORY;
}
else if (!strcmp(section,"dialplan")) {
query_type = 2;
query_type = XML_LDAP_DIALPLAN;
}
else if (!strcmp(section,"phrases")) {
query_type = 3;
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);
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;
}
@ -235,14 +274,18 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
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);
@ -250,6 +293,7 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
free(dir_exten);
dir_exten = NULL;
free(dir_domain);
dir_domain = NULL;
} else {
@ -257,14 +301,18 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
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;
}
@ -302,8 +350,13 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
}
cleanup:
if (ldap->msg) ldap_msgfree(ldap->msg);
if (ldap->ld) ldap_unbind_s(ldap->ld);
if (ldap->msg) {
ldap_msgfree(ldap->msg);
}
if (ldap->ld) {
ldap_unbind_s(ldap->ld);
}
switch_safe_free(search_base);
@ -336,37 +389,41 @@ static switch_status_t do_config(void)
}
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")) {
if (val) {
binding->queryfmt = strdup(val);
}
}
else if (!strcasecmp(var, "base")) {
binding->ldap_base = strdup(val);
} else if (!strcasecmp(var, "binddn")) {
}
else if (!strcasecmp(var, "binddn")) {
binding->binddn = strdup(val);
} else if (!strcasecmp(var, "bindpass")) {
}
else if (!strcasecmp(var, "bindpass")) {
binding->bindpass = strdup(val);
} else if (!strcasecmp(var, "host")) {
}
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_xml_bind_search_function(xml_ldap_search, switch_xml_parse_section_string(bname), binding);
x++;
binding = NULL;
}
@ -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;