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> #include <ldap.h>
#endif #endif
#define XML_LDAP_CONFIG 0 typedef enum {
#define XML_LDAP_DIRECTORY 1 XML_LDAP_CONFIG = 0,
#define XML_LDAP_DIALPLAN 2 XML_LDAP_DIRECTORY,
#define XML_LDAP_PHRASE 3 XML_LDAP_DIALPLAN,
XML_LDAP_PHRASE
} xml_ldap_query_type_t;
SWITCH_MODULE_LOAD_FUNCTION(mod_xml_ldap_load); SWITCH_MODULE_LOAD_FUNCTION(mod_xml_ldap_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_ldap_shutdown); SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_ldap_shutdown);
SWITCH_MODULE_DEFINITION(mod_xml_ldap, mod_xml_ldap_load, mod_xml_ldap_shutdown, NULL); SWITCH_MODULE_DEFINITION(mod_xml_ldap, mod_xml_ldap_load, mod_xml_ldap_shutdown, NULL);
struct xml_binding { struct xml_binding {
char *bindings; char *bindings;
char *host; char *host;
@ -78,11 +79,12 @@ struct ldap_c {
}; };
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);
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);
#define XML_LDAP_SYNTAX "[debug_on|debug_off]" #define XML_LDAP_SYNTAX "[debug_on|debug_off]"
SWITCH_STANDARD_API(xml_ldap_function) SWITCH_STANDARD_API(xml_ldap_function)
{ {
if (session) { if (session) {
@ -94,8 +96,10 @@ SWITCH_STANDARD_API(xml_ldap_function)
} }
if (!strcasecmp(cmd, "debug_on")) { if (!strcasecmp(cmd, "debug_on")) {
} else if (!strcasecmp(cmd, "debug_off")) { }
} else { else if (!strcasecmp(cmd, "debug_off")) {
}
else {
goto usage; goto usage;
} }
@ -107,71 +111,102 @@ usage:
return SWITCH_STATUS_SUCCESS; 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; switch (query_type) {
if (qt == 1 ) ret = xml_ldap_directory_result(ldap_connection, binding, xml , off); case XML_LDAP_DIRECTORY:
else if (qt == 2 ) ret = xml_ldap_dialplan_result(ldap_connection, binding, xml , off); return xml_ldap_directory_result(ldap_connection, binding, xml, off);
return ret;
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; struct ldap_c *ldap = ldap_connection;
switch_xml_t asdf = *xml; switch_xml_t asdf = *xml;
switch_xml_t param, variable, params, variables; switch_xml_t param, variable, params, variables;
int i =0; int i = 0;
int loff = *off; 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)) { 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 ); ldap->key = ldap_first_attribute(ldap->ld, ldap->entry, &ldap->berkey);
do { do {
ldap->val = ldap_first_attribute( ldap->ld, ldap->entry, &ldap->berval ); ldap->val = ldap_first_attribute(ldap->ld, ldap->entry, &ldap->berval);
do { do {
if(strstr(ldap->val,"value") ) { if (strstr(ldap->val, "value")) {
if(strstr(ldap->val,ldap->key) && strcmp(ldap->val,ldap->key) ) { if (strstr(ldap->val, ldap->key) && strcmp(ldap->val, ldap->key) ) {
if(!strcmp(ldap->key,"param")) { if (!strcmp(ldap->key, "param")) {
params = switch_xml_add_child_d(asdf,"params",loff++); params = switch_xml_add_child_d(asdf, "params", loff++);
}else if (!strcmp(ldap->key,"variable")) { }
variables = switch_xml_add_child_d(asdf,"variables",loff++); 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);
if ( ldap_count_values(ldap->valvals) == ldap_count_values(ldap->keyvals) ) { ldap->valvals = ldap_get_values(ldap->ld, ldap->entry, ldap->val);
for ( i = 0 ; ldap->keyvals[i] != NULL && ldap->valvals[i] != NULL ; i++ ) {
if(!strcmp(ldap->key,"param")) { if (ldap->keyvals && ldap->valvals) {
param = switch_xml_add_child_d(params,"param",loff++); if (ldap_count_values(ldap->valvals) == ldap_count_values(ldap->keyvals)) {
switch_xml_set_attr_d(param,"name",ldap->keyvals[i]); for (i = 0; ldap->keyvals[i] != NULL && ldap->valvals[i] != NULL; i++) {
switch_xml_set_attr_d(param,"value",ldap->valvals[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")) { else if (!strcmp(ldap->key, "variable")) {
variable = switch_xml_add_child_d(variables,"variable",loff++); 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, "name", ldap->keyvals[i]);
switch_xml_set_attr_d(variable,"value",ldap->valvals[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); if (ldap->keyvals) {
ldap_value_free(ldap->keyvals);
}
if (ldap->valvals) {
ldap_value_free(ldap->valvals);
}
} }
else { 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 )); 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); if (ldap->val) {
ldap->val = ldap_next_attribute(ldap->ld, ldap->entry,ldap->berval); ldap_memfree(ldap->val);
}
ldap->val = ldap_next_attribute(ldap->ld, ldap->entry, ldap->berval);
} while (ldap->val != NULL); } 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 ); ldap->key = ldap_next_attribute(ldap->ld, ldap->entry, ldap->berkey );
} while ( ldap->key != NULL );
if (ldap->berkey) ber_free(ldap->berkey,0); } while (ldap->key != NULL);
if (ldap->berkey) {
ber_free(ldap->berkey, 0);
}
} }
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
@ -179,7 +214,7 @@ static switch_status_t xml_ldap_directory_result( void *ldap_connection, xml_bin
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; xml_binding_t *binding = (xml_binding_t *)user_data;
switch_event_header_t *hi; switch_event_header_t *hi;
switch_xml_t xml, sub; switch_xml_t xml, sub;
@ -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 auth_method = LDAP_AUTH_SIMPLE;
int desired_version = LDAP_VERSION3; 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 *dir_exten, *dir_domain;
char *filter = "(objectClass=*)"; char *filter = "(objectClass=*)";
@ -202,32 +237,36 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
return NULL; return NULL;
} }
if(!strcmp(section,"configuration")) { if (!strcmp(section,"configuration")) {
query_type = 0; query_type = XML_LDAP_CONFIG;
} }
else if (!strcmp(section,"directory")) { else if (!strcmp(section,"directory")) {
query_type = 1; query_type = XML_LDAP_DIRECTORY;
} }
else if (!strcmp(section,"dialplan")) { else if (!strcmp(section,"dialplan")) {
query_type = 2; query_type = XML_LDAP_DIALPLAN;
} }
else if (!strcmp(section,"phrases")) { else if (!strcmp(section,"phrases")) {
query_type = 3; query_type = XML_LDAP_PHRASE;
} }
if (params) { if (params) {
if ((hi = params->headers)) { if ((hi = params->headers)) {
for (; hi; hi = hi->next) { for (; hi; hi = hi->next) {
switch(query_type) { switch(query_type) {
case XML_LDAP_CONFIG: case XML_LDAP_CONFIG:
break; break;
case XML_LDAP_DIRECTORY: case XML_LDAP_DIRECTORY:
if (!strcmp(hi->name,"user")) dir_exten = strdup(hi->value); if (!strcmp(hi->name,"user")) {
else if (!strcmp(hi->name,"domain")) dir_domain = strdup(hi->value); dir_exten = strdup(hi->value);
}
else if (!strcmp(hi->name,"domain")) {
dir_domain = strdup(hi->value);
}
break; break;
case XML_LDAP_DIALPLAN: case XML_LDAP_DIALPLAN:
break;
case XML_LDAP_PHRASE: case XML_LDAP_PHRASE:
break; break;
} }
@ -235,36 +274,45 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
switch(query_type) { switch(query_type) {
case XML_LDAP_CONFIG: case XML_LDAP_CONFIG:
break; 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); 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); free(dir_exten);
dir_exten = NULL; dir_exten = NULL;
free(dir_domain); free(dir_domain);
dir_domain = NULL; dir_domain = NULL;
} else { } 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); 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; goto cleanup;
} }
break; break;
case XML_LDAP_DIALPLAN: case XML_LDAP_DIALPLAN:
xml = switch_xml_new("document"); xml = switch_xml_new("document");
switch_xml_set_attr_d(xml,"type","freeswitch/xml"); 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, "section", off++);
sub = switch_xml_add_child_d(xml,"context",off++); switch_xml_set_attr_d(sub, "name", "dialplan");
search_base = switch_mprintf(binding->queryfmt,dir_exten,dir_domain,binding->ldap_base);
sub = switch_xml_add_child_d(xml, "context", off++);
search_base = switch_mprintf(binding->queryfmt, dir_exten, dir_domain, binding->ldap_base);
break; break;
case XML_LDAP_PHRASE: case XML_LDAP_PHRASE:
break; break;
} }
@ -285,7 +333,7 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
} }
if (ldap_bind_s(ldap->ld, binding->binddn, binding->bindpass, auth_method) != LDAP_SUCCESS) { 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); 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; goto cleanup;
} }
@ -297,13 +345,18 @@ static switch_xml_t xml_ldap_search(const char *section, const char *tag_name, c
goto cleanup; goto cleanup;
} }
if ( xml_ldap_result( &ldap_connection, binding, &sub, &off, query_type) != SWITCH_STATUS_SUCCESS ){ if (xml_ldap_result(&ldap_connection, binding, &sub, &off, query_type) != SWITCH_STATUS_SUCCESS) {
goto cleanup; goto cleanup;
} }
cleanup: cleanup:
if (ldap->msg) ldap_msgfree(ldap->msg); if (ldap->msg) {
if (ldap->ld) ldap_unbind_s(ldap->ld); ldap_msgfree(ldap->msg);
}
if (ldap->ld) {
ldap_unbind_s(ldap->ld);
}
switch_safe_free(search_base); switch_safe_free(search_base);
@ -336,42 +389,46 @@ static switch_status_t do_config(void)
} }
memset(binding, 0, sizeof(*binding)); memset(binding, 0, sizeof(*binding));
for (param = switch_xml_child(binding_tag, "param"); param; param = param->next) { for (param = switch_xml_child(binding_tag, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name"); char *var = (char *) switch_xml_attr_soft(param, "name");
char *val = (char *) switch_xml_attr_soft(param, "value"); char *val = (char *) switch_xml_attr_soft(param, "value");
if (!strcasecmp(var, "queryfmt")) { if (!strcasecmp(var, "queryfmt")) {
binding->bindings = (char *) switch_xml_attr_soft(param, "bindings"); binding->bindings = (char *) switch_xml_attr_soft(param, "bindings");
if(val) binding->queryfmt = strdup(val); if (val) {
} else if (!strcasecmp(var, "base")) { binding->queryfmt = strdup(val);
}
}
else if (!strcasecmp(var, "base")) {
binding->ldap_base = strdup(val); binding->ldap_base = strdup(val);
} else if (!strcasecmp(var, "binddn")) { }
else if (!strcasecmp(var, "binddn")) {
binding->binddn = strdup(val); binding->binddn = strdup(val);
} else if (!strcasecmp(var, "bindpass")) { }
else if (!strcasecmp(var, "bindpass")) {
binding->bindpass = strdup(val); binding->bindpass = strdup(val);
} else if (!strcasecmp(var, "host")) { }
else if (!strcasecmp(var, "host")) {
binding->host = strdup(val); binding->host = strdup(val);
} }
} }
if (!binding->ldap_base || !binding->binddn || !binding->bindpass ) { 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\n"); 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; continue;
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding [%s] XML Fetch Function [%s] [%s]\n", 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); switch_xml_bind_search_function(xml_ldap_search, switch_xml_parse_section_string(bname), binding);
x++; x++;
binding = NULL; binding = NULL;
} }
done: done:
switch_xml_free(xml); switch_xml_free(xml);
return SWITCH_STATUS_SUCCESS; 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); *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_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 */ /* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;