mod_blacklist: Resource leak fixes, config checks and add help output for api interface

Don't leak the file handle after parsing the configuration
Don't leak memory if the file could not be opened

Check list name and filename attribute of configuration entry.

Add "blacklist help" and error messages to api interface.

Signed-off-by: Stefan Knoblich <stkn@openisdn.net>
This commit is contained in:
Stefan Knoblich 2011-09-21 15:50:57 +02:00
parent d00f74640f
commit 41abb3e644
2 changed files with 37 additions and 14 deletions

View File

@ -1,6 +1,10 @@
<configuration name="mod_blacklist.conf" description="Blacklist module">
<lists>
<!-- Example blacklist, the referenced file contains blacklisted items, one entry per line
<!--
Example blacklist, the referenced file contains blacklisted items, one entry per line
NOTE: make sure the file exists and is readable by FreeSWITCH.
<list name="example" filename="/usr/local/freeswitch/conf/blacklists/example.list"/>
-->
</lists>

View File

@ -86,24 +86,25 @@ void trim(char *string)
static switch_status_t load_list(const char *name, const char *filename)
{
FILE *f;
blacklist_t *bl = blacklist_create(name);
/* Create a hashtable + mutex for that list */
if ((f = fopen(filename, "r"))) {
char buf[1024] = {0};
blacklist_t *bl = blacklist_create(name); /* Create a hashtable + mutex for that list */
while (fgets(buf, 1024, f)) {
trim(buf);
switch_core_hash_insert(bl->list, buf, (void*)SWITCH_TRUE);
switch_core_hash_insert(bl->list, buf, (void *)SWITCH_TRUE);
}
switch_core_hash_insert(globals.lists, name, bl);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Loaded list [%s]\n", name);
fclose(f);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't open [%s] to load list [%s]\n", filename, name);
return SWITCH_STATUS_FALSE;
}
switch_core_hash_insert(globals.lists, name, bl);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Loaded list [%s]\n", name);
return SWITCH_STATUS_SUCCESS;
}
@ -134,9 +135,16 @@ static switch_status_t do_config(switch_bool_t reload)
const char *name = switch_xml_attr_soft(list, "name");
const char *filename = switch_xml_attr_soft(list, "filename");
if (name && filename) {
load_list(name, filename);
if (zstr(name)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "list has no name\n");
continue;
}
if (zstr(filename)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "list [%s] has no filename\n", name);
continue;
}
load_list(name, filename);
}
}
@ -150,6 +158,13 @@ static switch_status_t do_config(switch_bool_t reload)
return SWITCH_STATUS_SUCCESS;
}
#define BLACKLIST_API_SYNTAX \
"blacklist check <listname> <item>\n" \
"blacklist add <listname> <item>\n" \
"blacklist del <listname> <item>\n" \
"blacklist reload\n" \
"blacklist help\n"
SWITCH_STANDARD_API(blacklist_api_function)
{
char *data;
@ -209,7 +224,7 @@ SWITCH_STANDARD_API(blacklist_api_function)
switch_mutex_unlock(bl->list_mutex);
stream->write_function(stream, "+OK\n");
} else if (!strcasecmp(argv[0], "del")) {
blacklist_t *bl = NULL;
blacklist_t *bl = NULL;
if (argc < 2 || zstr(argv[1]) || zstr(argv[2])) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Wrong syntax");
goto done;
@ -233,6 +248,10 @@ SWITCH_STANDARD_API(blacklist_api_function)
} else if (!strcasecmp(argv[0], "reload")) {
do_config(SWITCH_TRUE);
stream->write_function(stream, "+OK\n");
} else if (!strcasecmp(argv[0], "help")) {
stream->write_function(stream, BLACKLIST_API_SYNTAX "+OK\n");
} else if (!zstr(argv[0])) {
stream->write_function(stream, "-ERR: No such command: %s (see 'blacklist help')\n", argv[0]);
}
done: