Added a chat only hub plugin that disables searching and connection setup.

This also deprecates the built-in chat_only configuration option.
If you need this functionality, then load the mod_chat_only plugin (if it is loaded then only operators
are able to search, connect, etc).
This commit is contained in:
Jan Vidar Krey 2012-01-09 19:54:03 +01:00
parent f44a420970
commit eee2636582
12 changed files with 226 additions and 31 deletions

View File

@ -229,6 +229,9 @@ plugin_auth_sqlite_LIBS := -lsqlite3
plugin_chat_history_SOURCE := src/plugins/mod_chat_history.c plugin_chat_history_SOURCE := src/plugins/mod_chat_history.c
plugin_chat_history_TARGET := mod_chat_history.so plugin_chat_history_TARGET := mod_chat_history.so
plugin_chat_only_SOURCE := src/plugins/mod_chat_only.c
plugin_chat_only_TARGET := mod_chat_only.so
# Source to objects # Source to objects
libuhub_OBJECTS := $(libuhub_SOURCES:.c=.o) libuhub_OBJECTS := $(libuhub_SOURCES:.c=.o)
libutils_OBJECTS := $(libutils_SOURCES:.c=.o) libutils_OBJECTS := $(libutils_SOURCES:.c=.o)
@ -240,7 +243,7 @@ uhub-passwd_OBJECTS := $(uhub-passwd_SOURCES:.c=.o)
adcrush_OBJECTS := $(adcrush_SOURCES:.c=.o) adcrush_OBJECTS := $(adcrush_SOURCES:.c=.o)
admin_OBJECTS := $(admin_SOURCES:.c=.o) admin_OBJECTS := $(admin_SOURCES:.c=.o)
all_plugins := $(plugin_example_TARGET) $(plugin_logging_TARGET) $(plugin_auth_TARGET) $(plugin_auth_sqlite_TARGET) $(plugin_welcome_TARGET) $(plugin_chat_history_TARGET) all_plugins := $(plugin_example_TARGET) $(plugin_logging_TARGET) $(plugin_auth_TARGET) $(plugin_auth_sqlite_TARGET) $(plugin_welcome_TARGET) $(plugin_chat_history_TARGET) $(plugin_chat_only_TARGET)
all_OBJECTS := $(libuhub_OBJECTS) $(uhub_OBJECTS) $(libutils_OBJECTS) $(adcrush_OBJECTS) $(autotest_OBJECTS) $(admin_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS) all_OBJECTS := $(libuhub_OBJECTS) $(uhub_OBJECTS) $(libutils_OBJECTS) $(adcrush_OBJECTS) $(autotest_OBJECTS) $(admin_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS)
all_OBJECTS += $(all_plugins) all_OBJECTS += $(all_plugins)
@ -280,6 +283,9 @@ $(plugin_logging_TARGET): $(plugin_logging_SOURCES) $(libutils_OBJECTS) $(libadc
$(plugin_chat_history_TARGET): $(plugin_chat_history_SOURCE) $(libutils_OBJECTS) $(plugin_chat_history_TARGET): $(plugin_chat_history_SOURCE) $(libutils_OBJECTS)
$(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS) $(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS)
$(plugin_chat_only_TARGET): $(plugin_chat_only_SOURCE) $(libutils_OBJECTS)
$(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS)
$(plugin_welcome_TARGET): $(plugin_welcome_SOURCES) $(libutils_OBJECTS) $(plugin_welcome_TARGET): $(plugin_welcome_SOURCES) $(libutils_OBJECTS)
$(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS) $(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS)

View File

@ -64,6 +64,11 @@ typedef uint32_t fourcc_t;
#define ADC_CMD_FSCH FOURCC('F','S','C','H') #define ADC_CMD_FSCH FOURCC('F','S','C','H')
#define ADC_CMD_DRES FOURCC('D','R','E','S') #define ADC_CMD_DRES FOURCC('D','R','E','S')
/* invalid search results (spam) */
#define ADC_CMD_BRES FOURCC('B','R','E','S')
#define ADC_CMD_ERES FOURCC('E','R','E','S')
#define ADC_CMD_FRES FOURCC('F','R','E','S')
/* connection setup */ /* connection setup */
#define ADC_CMD_DCTM FOURCC('D','C','T','M') #define ADC_CMD_DCTM FOURCC('D','C','T','M')
#define ADC_CMD_DRCM FOURCC('D','R','C','M') #define ADC_CMD_DRCM FOURCC('D','R','C','M')

View File

@ -126,14 +126,6 @@
<since>0.3.1</since> <since>0.3.1</since>
</option> </option>
<option name="chat_only" type="boolean" default="0">
<short>Allow chat only operation on hub</short>
<description><![CDATA[
If this is enabled the hub will refuse to relay messages for search and connection setup. This effectively makes the hub viable for chat only.
]]></description>
<since>0.1.1</since>
</option>
<option name="chat_is_privileged" type="boolean" default="0"> <option name="chat_is_privileged" type="boolean" default="0">
<short>Allow chat for operators and above only</short> <short>Allow chat for operators and above only</short>
<description><![CDATA[ <description><![CDATA[

View File

@ -13,7 +13,6 @@ void config_defaults(struct hub_config* config)
config->registered_users_only = 0; config->registered_users_only = 0;
config->register_self = 0; config->register_self = 0;
config->obsolete_clients = 0; config->obsolete_clients = 0;
config->chat_only = 0;
config->chat_is_privileged = 0; config->chat_is_privileged = 0;
config->hub_name = hub_strdup("uhub"); config->hub_name = hub_strdup("uhub");
config->hub_description = hub_strdup("no description"); config->hub_description = hub_strdup("no description");
@ -203,16 +202,6 @@ static int apply_config(struct hub_config* config, char* key, char* data, int li
return 0; return 0;
} }
if (!strcmp(key, "chat_only"))
{
if (!apply_boolean(key, data, &config->chat_only))
{
LOG_ERROR("Configuration parse error on line %d", line_count);
return -1;
}
return 0;
}
if (!strcmp(key, "chat_is_privileged")) if (!strcmp(key, "chat_is_privileged"))
{ {
if (!apply_boolean(key, data, &config->chat_is_privileged)) if (!apply_boolean(key, data, &config->chat_is_privileged))
@ -1052,9 +1041,6 @@ void dump_config(struct hub_config* config, int ignore_defaults)
if (!ignore_defaults || config->obsolete_clients != 0) if (!ignore_defaults || config->obsolete_clients != 0)
fprintf(stdout, "obsolete_clients = %s\n", config->obsolete_clients ? "yes" : "no"); fprintf(stdout, "obsolete_clients = %s\n", config->obsolete_clients ? "yes" : "no");
if (!ignore_defaults || config->chat_only != 0)
fprintf(stdout, "chat_only = %s\n", config->chat_only ? "yes" : "no");
if (!ignore_defaults || config->chat_is_privileged != 0) if (!ignore_defaults || config->chat_is_privileged != 0)
fprintf(stdout, "chat_is_privileged = %s\n", config->chat_is_privileged ? "yes" : "no"); fprintf(stdout, "chat_is_privileged = %s\n", config->chat_is_privileged ? "yes" : "no");

View File

@ -13,7 +13,6 @@ struct hub_config
int registered_users_only; /*<<< Allow registered users only (default: 0) */ int registered_users_only; /*<<< Allow registered users only (default: 0) */
int register_self; /*<<< Allow users to register themselves on the hub. (default: 0) */ int register_self; /*<<< Allow users to register themselves on the hub. (default: 0) */
int obsolete_clients; /*<<< Support obsolete clients using a ADC protocol prior to 1.0 (default: 0) */ int obsolete_clients; /*<<< Support obsolete clients using a ADC protocol prior to 1.0 (default: 0) */
int chat_only; /*<<< Allow chat only operation on hub (default: 0) */
int chat_is_privileged; /*<<< Allow chat for operators and above only (default: 0) */ int chat_is_privileged; /*<<< Allow chat for operators and above only (default: 0) */
char* hub_name; /*<<< Name of hub (default: uhub) */ char* hub_name; /*<<< Name of hub (default: uhub) */
char* hub_description; /*<<< Short hub description, topic or subject. (default: no description) */ char* hub_description; /*<<< Short hub description, topic or subject. (default: no description) */

View File

@ -21,10 +21,6 @@
struct hub_info* g_hub = 0; struct hub_info* g_hub = 0;
#define CHECK_CHAT_ONLY \
if (hub->config->chat_only && u->credentials < auth_cred_operator) \
break
/* FIXME: Flood control should be done in a plugin! */ /* FIXME: Flood control should be done in a plugin! */
#define CHECK_FLOOD(TYPE, WARN) \ #define CHECK_FLOOD(TYPE, WARN) \
if (flood_control_check(&u->flood_ ## TYPE , hub->config->flood_ctl_ ## TYPE, hub->config->flood_ctl_interval, net_get_time())) \ if (flood_control_check(&u->flood_ ## TYPE , hub->config->flood_ctl_ ## TYPE, hub->config->flood_ctl_interval, net_get_time())) \
@ -81,6 +77,7 @@ int hub_handle_message(struct hub_info* hub, struct hub_user* u, const char* lin
case ADC_CMD_EINF: case ADC_CMD_EINF:
case ADC_CMD_FINF: case ADC_CMD_FINF:
/* these must never be allowed for security reasons, so we ignore them. */ /* these must never be allowed for security reasons, so we ignore them. */
CHECK_FLOOD(extras, 1);
break; break;
case ADC_CMD_EMSG: case ADC_CMD_EMSG:
@ -96,20 +93,35 @@ int hub_handle_message(struct hub_info* hub, struct hub_user* u, const char* lin
case ADC_CMD_ESCH: case ADC_CMD_ESCH:
case ADC_CMD_FSCH: case ADC_CMD_FSCH:
cmd->priority = -1; cmd->priority = -1;
CHECK_CHAT_ONLY; if (plugin_handle_search(hub, u, cmd->cache) == st_deny)
break;
CHECK_FLOOD(search, 1); CHECK_FLOOD(search, 1);
ROUTE_MSG; ROUTE_MSG;
case ADC_CMD_FRES: // spam
case ADC_CMD_BRES: // spam
case ADC_CMD_ERES: // pointless.
CHECK_FLOOD(extras, 1);
break;
case ADC_CMD_DRES: case ADC_CMD_DRES:
cmd->priority = -1; cmd->priority = -1;
CHECK_CHAT_ONLY; if (plugin_handle_search_result(hub, u, uman_get_user_by_sid(hub, cmd->target), cmd->cache) == st_deny)
break;
/* CHECK_FLOOD(search, 0); */ /* CHECK_FLOOD(search, 0); */
ROUTE_MSG; ROUTE_MSG;
case ADC_CMD_DRCM: case ADC_CMD_DRCM:
cmd->priority = -1;
if (plugin_handle_revconnect(hub, u, uman_get_user_by_sid(hub, cmd->target)) == st_deny)
break;
CHECK_FLOOD(connect, 1);
ROUTE_MSG;
case ADC_CMD_DCTM: case ADC_CMD_DCTM:
cmd->priority = -1; cmd->priority = -1;
CHECK_CHAT_ONLY; if (plugin_handle_connect(hub, u, uman_get_user_by_sid(hub, cmd->target)) == st_deny)
break;
CHECK_FLOOD(connect, 1); CHECK_FLOOD(connect, 1);
ROUTE_MSG; ROUTE_MSG;

View File

@ -92,6 +92,20 @@ static int cbfunc_send_message(struct plugin_handle* plugin, struct plugin_user*
return 1; return 1;
} }
static int cbfunc_send_status(struct plugin_handle* plugin, struct plugin_user* user, int code, const char* message)
{
// struct plugin_callback_data* data = get_callback_data(plugin);
char code_str[4];
char* buffer = adc_msg_escape(message);
struct adc_message* command = adc_msg_construct(ADC_CMD_ISTA, strlen(buffer) + 10);
snprintf(code_str, sizeof(code_str), "%03d", code);
adc_msg_add_argument(command, code_str);
adc_msg_add_argument(command, buffer);
route_to_user(plugin_get_hub(plugin), convert_user_type(user), command);
adc_msg_free(command);
hub_free(buffer);
return 1;
}
static int cbfunc_user_disconnect(struct plugin_handle* plugin, struct plugin_user* user) static int cbfunc_user_disconnect(struct plugin_handle* plugin, struct plugin_user* user)
{ {
@ -136,6 +150,7 @@ static int cbfunc_command_del(struct plugin_handle* plugin, struct plugin_comman
void plugin_register_callback_functions(struct plugin_handle* handle) void plugin_register_callback_functions(struct plugin_handle* handle)
{ {
handle->hub.send_message = cbfunc_send_message; handle->hub.send_message = cbfunc_send_message;
handle->hub.send_status_message = cbfunc_send_status;
handle->hub.user_disconnect = cbfunc_user_disconnect; handle->hub.user_disconnect = cbfunc_user_disconnect;
handle->hub.command_add = cbfunc_command_add; handle->hub.command_add = cbfunc_command_add;
handle->hub.command_del = cbfunc_command_del; handle->hub.command_del = cbfunc_command_del;

View File

@ -155,6 +155,13 @@ plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* from, cons
PLUGIN_INVOKE_STATUS_2(hub, on_search, user, data); PLUGIN_INVOKE_STATUS_2(hub, on_search, user, data);
} }
plugin_st plugin_handle_search_result(struct hub_info* hub, struct hub_user* from, struct hub_user* to, const char* data)
{
struct plugin_user* user1 = convert_user_type(from);
struct plugin_user* user2 = convert_user_type(to);
PLUGIN_INVOKE_STATUS_3(hub, on_search_result, user1, user2, data);
}
plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to) plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to)
{ {
struct plugin_user* user1 = convert_user_type(from); struct plugin_user* user1 = convert_user_type(from);

View File

@ -50,6 +50,7 @@ plugin_st plugin_handle_private_message(struct hub_info* hub, struct hub_user* f
/* Handle searches */ /* Handle searches */
plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* user, const char* data); plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* user, const char* data);
plugin_st plugin_handle_search_result(struct hub_info* hub, struct hub_user* from, struct hub_user* to, const char* data);
/* Handle p2p connections */ /* Handle p2p connections */
plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to); plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to);

View File

@ -32,6 +32,7 @@
typedef plugin_st (*on_chat_msg_t)(struct plugin_handle*, struct plugin_user* from, const char* message); typedef plugin_st (*on_chat_msg_t)(struct plugin_handle*, struct plugin_user* from, const char* message);
typedef plugin_st (*on_private_msg_t)(struct plugin_handle*, struct plugin_user* from, struct plugin_user* to, const char* message); typedef plugin_st (*on_private_msg_t)(struct plugin_handle*, struct plugin_user* from, struct plugin_user* to, const char* message);
typedef plugin_st (*on_search_t)(struct plugin_handle*, struct plugin_user* from, const char* data); typedef plugin_st (*on_search_t)(struct plugin_handle*, struct plugin_user* from, const char* data);
typedef plugin_st (*on_search_result_t)(struct plugin_handle*, struct plugin_user* from, struct plugin_user* to, const char* data);
typedef plugin_st (*on_p2p_connect_t)(struct plugin_handle*, struct plugin_user* from, struct plugin_user* to); typedef plugin_st (*on_p2p_connect_t)(struct plugin_handle*, struct plugin_user* from, struct plugin_user* to);
typedef plugin_st (*on_p2p_revconnect_t)(struct plugin_handle*, struct plugin_user* from, struct plugin_user* to); typedef plugin_st (*on_p2p_revconnect_t)(struct plugin_handle*, struct plugin_user* from, struct plugin_user* to);
@ -89,6 +90,7 @@ struct plugin_funcs
on_chat_msg_t on_chat_msg; /* A public chat message is about to be sent (can be intercepted) */ on_chat_msg_t on_chat_msg; /* A public chat message is about to be sent (can be intercepted) */
on_private_msg_t on_private_msg; /* A public chat message is about to be sent (can be intercepted) */ on_private_msg_t on_private_msg; /* A public chat message is about to be sent (can be intercepted) */
on_search_t on_search; /* A search is about to be sent (can be intercepted) */ on_search_t on_search; /* A search is about to be sent (can be intercepted) */
on_search_result_t on_search_result; /* A search result is about to be sent (can be intercepted) */
on_p2p_connect_t on_p2p_connect; /* A user is about to connect to another user (can be intercepted) */ on_p2p_connect_t on_p2p_connect; /* A user is about to connect to another user (can be intercepted) */
on_p2p_revconnect_t on_p2p_revconnect; /* A user is about to connect to another user (can be intercepted) */ on_p2p_revconnect_t on_p2p_revconnect; /* A user is about to connect to another user (can be intercepted) */
@ -106,6 +108,7 @@ struct plugin_funcs
struct plugin_command_handle; struct plugin_command_handle;
typedef int (*hfunc_send_message)(struct plugin_handle*, struct plugin_user* user, const char* message); typedef int (*hfunc_send_message)(struct plugin_handle*, struct plugin_user* user, const char* message);
typedef int (*hfunc_send_status)(struct plugin_handle*, struct plugin_user* to, int code, const char* message);
typedef int (*hfunc_user_disconnect)(struct plugin_handle*, struct plugin_user* user); typedef int (*hfunc_user_disconnect)(struct plugin_handle*, struct plugin_user* user);
typedef int (*hfunc_command_add)(struct plugin_handle*, struct plugin_command_handle*); typedef int (*hfunc_command_add)(struct plugin_handle*, struct plugin_command_handle*);
typedef int (*hfunc_command_del)(struct plugin_handle*, struct plugin_command_handle*); typedef int (*hfunc_command_del)(struct plugin_handle*, struct plugin_command_handle*);
@ -117,6 +120,7 @@ typedef int (*hfunc_command_del)(struct plugin_handle*, struct plugin_command_ha
struct plugin_hub_funcs struct plugin_hub_funcs
{ {
hfunc_send_message send_message; hfunc_send_message send_message;
hfunc_send_status send_status_message;
hfunc_user_disconnect user_disconnect; hfunc_user_disconnect user_disconnect;
hfunc_command_add command_add; hfunc_command_add command_add;
hfunc_command_del command_del; hfunc_command_del command_del;

View File

@ -26,4 +26,9 @@
*/ */
extern int plugin_send_message(struct plugin_handle*, struct plugin_user* to, const char* message); extern int plugin_send_message(struct plugin_handle*, struct plugin_user* to, const char* message);
/**
* Send a status message to a user.
*/
extern int plugin_send_status(struct plugin_handle* struct plugin_user* to, int code, const char* message);
#endif /* HAVE_UHUB_PLUGIN_API_H */ #endif /* HAVE_UHUB_PLUGIN_API_H */

163
src/plugins/mod_chat_only.c Normal file
View File

@ -0,0 +1,163 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2012, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "plugin_api/handle.h"
#include "util/memory.h"
enum Warnings
{
WARN_SEARCH = 0x01, ///<<< "Warn about searching."
WARN_CONNECT = 0x02, ///<<< "Warn about connecting to a user"
WARN_EXTRA = 0x08, ///<<< "Warn about unknown protocol data."
};
struct user_info
{
sid_t sid; // The SID of the user
int warnings; // The number of denies (used to track wether or not a warning should be sent). @see enum Warnings.
};
struct chat_only_data
{
size_t num_users; // number of users tracked.
size_t max_users; // max users (hard limit max 1M users due to limitations in the SID (20 bits)).
struct user_info* users; // array of max_users
int operator_override; // operators are allowed to override these limitations.
};
static struct chat_only_data* co_initialize()
{
struct chat_only_data* data = (struct chat_only_data*) hub_malloc(sizeof(struct chat_only_data));
data->num_users = 0;
data->max_users = 512;
data->users = hub_malloc_zero(sizeof(struct user_info) * data->max_users);
return data;
}
static void co_shutdown(struct chat_only_data* data)
{
if (data)
{
hub_free(data->users);
hub_free(data);
}
}
static struct user_info* get_user_info(struct chat_only_data* data, sid_t sid)
{
struct user_info* u;
// resize buffer if needed.
if (sid > data->max_users) // FIXME: >= ?
{
u = hub_malloc_zero(sizeof(struct user_info) * (sid + 1));
memcpy(u, data->users, data->max_users);
hub_free(data->users);
data->users = u;
data->max_users = sid;
u = NULL;
}
u = &data->users[sid];
// reset counters if the user was not previously known.
if (!u->sid)
{
u->sid = sid;
u->warnings = 0;
data->num_users++;
}
return u;
}
static plugin_st on_search_result(struct plugin_handle* plugin, struct plugin_user* from, struct plugin_user* to, const char* search_data)
{
return st_deny;
}
static plugin_st on_search(struct plugin_handle* plugin, struct plugin_user* user, const char* search_data)
{
struct chat_only_data* data = (struct chat_only_data*) plugin->ptr;
struct user_info* info = get_user_info(data, user->sid);
if (user->credentials >= auth_cred_operator && data->operator_override)
return st_allow;
if (!(info->warnings & WARN_SEARCH))
{
plugin->hub.send_status_message(plugin, user, 000, "Searching is disabled. This is a chat only hub.");
info->warnings |= WARN_SEARCH;
}
return st_deny;
}
static plugin_st on_p2p_connect(struct plugin_handle* plugin, struct plugin_user* from, struct plugin_user* to)
{
struct chat_only_data* data = (struct chat_only_data*) plugin->ptr;
struct user_info* info = get_user_info(data, from->sid);
if (from->credentials >= auth_cred_operator && data->operator_override)
return st_allow;
if (!(info->warnings & WARN_CONNECT))
{
plugin->hub.send_status_message(plugin, from, 000, "Connection setup denied. This is a chat only hub.");
info->warnings |= WARN_CONNECT;
}
return st_deny;
}
static void on_user_login(struct plugin_handle* plugin, struct plugin_user* user)
{
struct chat_only_data* data = (struct chat_only_data*) plugin->ptr;
/*struct user_info* info = */
get_user_info(data, user->sid);
}
static void on_user_logout(struct plugin_handle* plugin, struct plugin_user* user, const char* reason)
{
struct chat_only_data* data = (struct chat_only_data*) plugin->ptr;
struct user_info* info = get_user_info(data, user->sid);
if (info->sid)
data->num_users--;
info->warnings = 0;
info->sid = 0;
}
int plugin_register(struct plugin_handle* plugin, const char* config)
{
PLUGIN_INITIALIZE(plugin, "Chat only hub", "1.0", "Disables connection setup, search and results.");
plugin->ptr = co_initialize();
plugin->funcs.on_search = on_search;
plugin->funcs.on_search_result = on_search_result;
plugin->funcs.on_p2p_connect = on_p2p_connect;
plugin->funcs.on_p2p_revconnect = on_p2p_connect;
plugin->funcs.on_user_login = on_user_login;
plugin->funcs.on_user_logout = on_user_logout;
return 0;
}
int plugin_unregister(struct plugin_handle* plugin)
{
co_shutdown((struct chat_only_data*) plugin->ptr);
return 0;
}