Reworked plugin APIs slightly, added full support for authentication from plugins.
This commit is contained in:
@@ -288,18 +288,29 @@ int acl_shutdown(struct acl_handle* handle)
|
||||
}
|
||||
|
||||
|
||||
struct auth_info* acl_get_access_info(struct acl_handle* handle, const char* name)
|
||||
struct auth_info* acl_get_access_info(struct hub_info* hub, const char* name)
|
||||
{
|
||||
struct auth_info* info = (struct auth_info*) list_get_first(handle->users);
|
||||
struct auth_info* info = 0;
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
info = (struct auth_info*) hub_malloc(sizeof(struct auth_info));
|
||||
if (plugin_auth_get_user(hub, name, info) != st_allow)
|
||||
{
|
||||
hub_free(info);
|
||||
return NULL;
|
||||
}
|
||||
return info;
|
||||
#else
|
||||
info = (struct auth_info*) list_get_first(hub->acl->users);
|
||||
while (info)
|
||||
{
|
||||
if (strcasecmp((char*)info->nickname, name) == 0)
|
||||
{
|
||||
return info;
|
||||
}
|
||||
info = (struct auth_info*) list_get_next(handle->users);
|
||||
info = (struct auth_info*) list_get_next(hub->acl->users);
|
||||
}
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
#define STR_LIST_CONTAINS(LIST, STR) \
|
||||
@@ -411,7 +422,7 @@ int acl_is_ip_nat_override(struct acl_handle* handle, const char* ip_address)
|
||||
* seconds since the unix epoch (modulus 1 million)
|
||||
* and the SID of the user (0-1 million).
|
||||
*/
|
||||
const char* acl_password_generate_challenge(struct acl_handle* acl, struct hub_user* user)
|
||||
const char* acl_password_generate_challenge(struct hub_info* hub, struct hub_user* user)
|
||||
{
|
||||
char buf[64];
|
||||
uint64_t tiger_res[3];
|
||||
@@ -427,7 +438,7 @@ const char* acl_password_generate_challenge(struct acl_handle* acl, struct hub_u
|
||||
}
|
||||
|
||||
|
||||
int acl_password_verify(struct acl_handle* acl, struct hub_user* user, const char* password)
|
||||
int acl_password_verify(struct hub_info* hub, struct hub_user* user, const char* password)
|
||||
{
|
||||
char buf[1024];
|
||||
struct auth_info* access;
|
||||
@@ -440,17 +451,11 @@ int acl_password_verify(struct acl_handle* acl, struct hub_user* user, const cha
|
||||
if (!password || !user || strlen(password) != MAX_CID_LEN)
|
||||
return 0;
|
||||
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
access = (struct auth_info*) hub_malloc(sizeof(struct auth_info));
|
||||
if (!plugin_auth_get_user(user->hub, user->id.nick, access))
|
||||
return 0;
|
||||
#else
|
||||
access = acl_get_access_info(acl, user->id.nick);
|
||||
#endif
|
||||
if (!access || !access->password)
|
||||
access = acl_get_access_info(hub, user->id.nick);
|
||||
if (!access)
|
||||
return 0;
|
||||
|
||||
challenge = acl_password_generate_challenge(acl, user);
|
||||
challenge = acl_password_generate_challenge(hub, user);
|
||||
|
||||
base32_decode(challenge, (unsigned char*) raw_challenge, MAX_CID_LEN);
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#define HAVE_UHUB_ACL_H
|
||||
|
||||
struct hub_config;
|
||||
struct hub_info;
|
||||
struct hub_user;
|
||||
struct ip_addr_encap;
|
||||
|
||||
@@ -38,7 +39,8 @@ struct acl_handle
|
||||
extern int acl_initialize(struct hub_config* config, struct acl_handle* handle);
|
||||
extern int acl_shutdown(struct acl_handle* handle);
|
||||
|
||||
extern struct auth_info* acl_get_access_info(struct acl_handle* handle, const char* name);
|
||||
extern struct auth_info* acl_get_access_info(struct hub_info* hub, const char* name);
|
||||
|
||||
extern int acl_is_cid_banned(struct acl_handle* handle, const char* cid);
|
||||
extern int acl_is_ip_banned(struct acl_handle* handle, const char* ip_address);
|
||||
extern int acl_is_ip_nat_override(struct acl_handle* handle, const char* ip_address);
|
||||
@@ -51,14 +53,14 @@ extern int acl_user_ban_cid(struct acl_handle* handle, const char* cid);
|
||||
extern int acl_user_unban_nick(struct acl_handle* handle, const char* nick);
|
||||
extern int acl_user_unban_cid(struct acl_handle* handle, const char* cid);
|
||||
|
||||
extern const char* acl_password_generate_challenge(struct acl_handle* acl, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Verify a password.
|
||||
*
|
||||
* @param password the hashed password (based on the nonce).
|
||||
* @return 1 if the password matches, or 0 if the password is incorrect.
|
||||
*/
|
||||
extern int acl_password_verify(struct acl_handle* acl, struct hub_user* user, const char* password);
|
||||
extern int acl_password_verify(struct hub_info* hub, struct hub_user* user, const char* password);
|
||||
extern const char* acl_password_generate_challenge(struct hub_info* hub, struct hub_user* user);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_ACL_H */
|
||||
|
||||
@@ -234,7 +234,7 @@ int hub_handle_password(struct hub_info* hub, struct hub_user* u, struct adc_mes
|
||||
|
||||
if (u->state == state_verify)
|
||||
{
|
||||
if (acl_password_verify(hub->acl, u, password))
|
||||
if (acl_password_verify(hub, u, password))
|
||||
{
|
||||
on_login_success(hub, u);
|
||||
}
|
||||
@@ -465,7 +465,7 @@ void hub_send_password_challenge(struct hub_info* hub, struct hub_user* u)
|
||||
{
|
||||
struct adc_message* igpa;
|
||||
igpa = adc_msg_construct(ADC_CMD_IGPA, 38);
|
||||
adc_msg_add_argument(igpa, acl_password_generate_challenge(hub->acl, u));
|
||||
adc_msg_add_argument(igpa, acl_password_generate_challenge(hub, u));
|
||||
user_set_state(u, state_verify);
|
||||
route_to_user(hub, u, igpa);
|
||||
adc_msg_free(igpa);
|
||||
|
||||
@@ -535,7 +535,7 @@ static int check_limits(struct hub_info* hub, struct hub_user* user, struct adc_
|
||||
static int set_credentials(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
|
||||
{
|
||||
int ret = 0;
|
||||
struct auth_info* info = acl_get_access_info(hub->acl, user->id.nick);
|
||||
struct auth_info* info = acl_get_access_info(hub, user->id.nick);
|
||||
|
||||
if (info)
|
||||
{
|
||||
|
||||
@@ -20,30 +20,57 @@
|
||||
#include "uhub.h"
|
||||
#include "plugin_api/handle.h"
|
||||
|
||||
#define PLUGIN_DEBUG(hub, name) printf("Invoke %s on %d plugins\n",name, (int) (hub->plugins ? list_size(hub->plugins->loaded) : -1));
|
||||
|
||||
|
||||
#define INVOKE(HUB, FUNCNAME, CODE) \
|
||||
PLUGIN_DEBUG(HUB, # FUNCNAME) \
|
||||
if (HUB->plugins && HUB->plugins->loaded) \
|
||||
{ \
|
||||
struct uhub_plugin_handle* plugin = (struct uhub_plugin_handle*) list_get_first(HUB->plugins->loaded); \
|
||||
struct plugin_handle* plugin = (struct plugin_handle*) list_get_first(HUB->plugins->loaded); \
|
||||
while (plugin) \
|
||||
{ \
|
||||
if (plugin->funcs.FUNCNAME) \
|
||||
CODE \
|
||||
plugin = (struct uhub_plugin_handle*) list_get_next(HUB->plugins->loaded); \
|
||||
plugin = (struct plugin_handle*) list_get_next(HUB->plugins->loaded); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define PLUGIN_INVOKE_STATUS(HUB, FUNCNAME, ARGS) \
|
||||
#define PLUGIN_INVOKE_STATUS_1(HUB, FUNCNAME, ARG1) \
|
||||
plugin_st status = st_default; \
|
||||
INVOKE(HUB, FUNCNAME, { \
|
||||
status = plugin->funcs.FUNCNAME ARGS ; \
|
||||
status = plugin->funcs.FUNCNAME(plugin, ARG1); \
|
||||
if (status != st_default) \
|
||||
break; \
|
||||
}); \
|
||||
return status
|
||||
|
||||
#define PLUGIN_INVOKE(HUB, FUNCNAME, ARGS) \
|
||||
#define PLUGIN_INVOKE_STATUS_2(HUB, FUNCNAME, ARG1, ARG2) \
|
||||
plugin_st status = st_default; \
|
||||
INVOKE(HUB, FUNCNAME, { \
|
||||
plugin->funcs.FUNCNAME ARGS ; \
|
||||
status = plugin->funcs.FUNCNAME(plugin, ARG1, ARG2); \
|
||||
if (status != st_default) \
|
||||
break; \
|
||||
}); \
|
||||
return status
|
||||
|
||||
#define PLUGIN_INVOKE_STATUS_3(HUB, FUNCNAME, ARG1, ARG2, ARG3) \
|
||||
plugin_st status = st_default; \
|
||||
INVOKE(HUB, FUNCNAME, { \
|
||||
status = plugin->funcs.FUNCNAME(plugin, ARG1, ARG2, ARG3); \
|
||||
if (status != st_default) \
|
||||
break; \
|
||||
}); \
|
||||
return status
|
||||
|
||||
#define PLUGIN_INVOKE_1(HUB, FUNCNAME, ARG1) \
|
||||
INVOKE(HUB, FUNCNAME, { \
|
||||
plugin->funcs.FUNCNAME(plugin, ARG1); \
|
||||
})
|
||||
|
||||
#define PLUGIN_INVOKE_2(HUB, FUNCNAME, ARG1, ARG2) \
|
||||
INVOKE(HUB, FUNCNAME, { \
|
||||
plugin->funcs.FUNCNAME(plugin, ARG1, ARG2); \
|
||||
})
|
||||
|
||||
static void convert_user_type(struct plugin_user* puser, struct hub_user* user)
|
||||
@@ -57,12 +84,12 @@ static void convert_user_type(struct plugin_user* puser, struct hub_user* user)
|
||||
|
||||
plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr)
|
||||
{
|
||||
PLUGIN_INVOKE_STATUS(hub, login_check_ip_early, (addr));
|
||||
PLUGIN_INVOKE_STATUS_1(hub, login_check_ip_early, addr);
|
||||
}
|
||||
|
||||
plugin_st plugin_check_ip_late(struct hub_info* hub, struct ip_addr_encap* addr)
|
||||
{
|
||||
PLUGIN_INVOKE_STATUS(hub, login_check_ip_late, (addr));
|
||||
PLUGIN_INVOKE_STATUS_1(hub, login_check_ip_late, addr);
|
||||
}
|
||||
|
||||
void plugin_log_connection_accepted(struct hub_info* hub, struct ip_addr_encap* ipaddr)
|
||||
@@ -81,42 +108,42 @@ void plugin_log_user_login_success(struct hub_info* hub, struct hub_user* who)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE(hub, on_user_login, (&user));
|
||||
PLUGIN_INVOKE_1(hub, on_user_login, &user);
|
||||
}
|
||||
|
||||
void plugin_log_user_login_error(struct hub_info* hub, struct hub_user* who, const char* reason)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE(hub, on_user_login_error, (&user, reason));
|
||||
PLUGIN_INVOKE_2(hub, on_user_login_error, &user, reason);
|
||||
}
|
||||
|
||||
void plugin_log_user_logout(struct hub_info* hub, struct hub_user* who, const char* reason)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE(hub, on_user_logout, (&user, reason));
|
||||
PLUGIN_INVOKE_2(hub, on_user_logout, &user, reason);
|
||||
}
|
||||
|
||||
void plugin_log_user_nick_change(struct hub_info* hub, struct hub_user* who, const char* new_nick)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE(hub, on_user_nick_change, (&user, new_nick));
|
||||
PLUGIN_INVOKE_2(hub, on_user_nick_change, &user, new_nick);
|
||||
}
|
||||
|
||||
void plugin_log_user_update_error(struct hub_info* hub, struct hub_user* who, const char* reason)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE(hub, on_user_update_error, (&user, reason));
|
||||
PLUGIN_INVOKE_2(hub, on_user_update_error, &user, reason);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_chat_message(struct hub_info* hub, struct hub_user* from, const char* message, int flags)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, from);
|
||||
PLUGIN_INVOKE_STATUS(hub, on_chat_msg, (&user, message));
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_chat_msg, &user, message);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_private_message(struct hub_info* hub, struct hub_user* from, struct hub_user* to, const char* message, int flags)
|
||||
@@ -125,14 +152,14 @@ plugin_st plugin_handle_private_message(struct hub_info* hub, struct hub_user* f
|
||||
struct plugin_user user2;
|
||||
convert_user_type(&user1, from);
|
||||
convert_user_type(&user2, to);
|
||||
PLUGIN_INVOKE_STATUS(hub, on_private_msg, (&user1, &user2, message));
|
||||
PLUGIN_INVOKE_STATUS_3(hub, on_private_msg, &user1, &user2, message);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* from, const char* data)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, from);
|
||||
PLUGIN_INVOKE_STATUS(hub, on_search, (&user, data));
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_search, &user, data);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to)
|
||||
@@ -141,7 +168,7 @@ plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, str
|
||||
struct plugin_user user2;
|
||||
convert_user_type(&user1, from);
|
||||
convert_user_type(&user2, to);
|
||||
PLUGIN_INVOKE_STATUS(hub, on_p2p_connect, (&user1, &user2));
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_p2p_connect, &user1, &user2);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_revconnect(struct hub_info* hub, struct hub_user* from, struct hub_user* to)
|
||||
@@ -150,25 +177,25 @@ plugin_st plugin_handle_revconnect(struct hub_info* hub, struct hub_user* from,
|
||||
struct plugin_user user2;
|
||||
convert_user_type(&user1, from);
|
||||
convert_user_type(&user2, to);
|
||||
PLUGIN_INVOKE_STATUS(hub, on_p2p_revconnect, (&user1, &user2));
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_p2p_revconnect, &user1, &user2);
|
||||
}
|
||||
|
||||
plugin_st plugin_auth_get_user(struct hub_info* hub, const char* nickname, struct auth_info* info)
|
||||
{
|
||||
PLUGIN_INVOKE_STATUS(hub, auth_get_user, (nickname, info));
|
||||
PLUGIN_INVOKE_STATUS_2(hub, auth_get_user, nickname, info);
|
||||
}
|
||||
|
||||
plugin_st plugin_auth_register_user(struct hub_info* hub, struct auth_info* info)
|
||||
{
|
||||
PLUGIN_INVOKE_STATUS(hub, auth_register_user, (info));
|
||||
PLUGIN_INVOKE_STATUS_1(hub, auth_register_user, info);
|
||||
}
|
||||
|
||||
plugin_st plugin_auth_update_user(struct hub_info* hub, struct auth_info* info)
|
||||
{
|
||||
PLUGIN_INVOKE_STATUS(hub, auth_update_user, (info));
|
||||
PLUGIN_INVOKE_STATUS_1(hub, auth_update_user, info);
|
||||
}
|
||||
|
||||
plugin_st plugin_auth_delete_user(struct hub_info* hub, struct auth_info* info)
|
||||
{
|
||||
PLUGIN_INVOKE_STATUS(hub, auth_delete_user, (info));
|
||||
PLUGIN_INVOKE_STATUS_1(hub, auth_delete_user, info);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, str
|
||||
plugin_st plugin_handle_revconnect(struct hub_info* hub, struct hub_user* from, struct hub_user* to);
|
||||
|
||||
/* Authentication related */
|
||||
int plugin_auth_get_user(struct hub_info* hub, const char* nickname, struct auth_info* info);
|
||||
plugin_st plugin_auth_get_user(struct hub_info* hub, const char* nickname, struct auth_info* info);
|
||||
plugin_st plugin_auth_register_user(struct hub_info* hub, struct auth_info* user);
|
||||
plugin_st plugin_auth_update_user(struct hub_info* hub, struct auth_info* user);
|
||||
plugin_st plugin_auth_delete_user(struct hub_info* hub, struct auth_info* user);
|
||||
|
||||
@@ -65,12 +65,12 @@ void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
|
||||
#endif
|
||||
}
|
||||
|
||||
struct uhub_plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
struct plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
{
|
||||
plugin_register_f register_f;
|
||||
plugin_unregister_f unregister_f;
|
||||
int ret;
|
||||
struct uhub_plugin_handle* handle = hub_malloc_zero(sizeof(struct uhub_plugin_handle));
|
||||
struct plugin_handle* handle = hub_malloc_zero(sizeof(struct plugin_handle));
|
||||
struct uhub_plugin* plugin = plugin_open(filename);
|
||||
|
||||
if (!plugin)
|
||||
@@ -114,7 +114,7 @@ struct uhub_plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void plugin_unload(struct uhub_plugin_handle* plugin)
|
||||
void plugin_unload(struct plugin_handle* plugin)
|
||||
{
|
||||
plugin->handle->unregister(plugin);
|
||||
plugin_close(plugin->handle);
|
||||
@@ -148,7 +148,7 @@ static int plugin_parse_line(char* line, int line_count, void* ptr_data)
|
||||
params = "";
|
||||
|
||||
LOG_TRACE("Load plugin: \"%s\", params=\"%s\"", soname, params);
|
||||
struct uhub_plugin_handle* plugin = plugin_load(soname, params);
|
||||
struct plugin_handle* plugin = plugin_load(soname, params);
|
||||
if (plugin)
|
||||
{
|
||||
list_append(handle->loaded, plugin);
|
||||
@@ -183,12 +183,12 @@ int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle)
|
||||
|
||||
void plugin_shutdown(struct uhub_plugins* handle)
|
||||
{
|
||||
struct uhub_plugin_handle* plugin = (struct uhub_plugin_handle*) list_get_first(handle->loaded);
|
||||
struct plugin_handle* plugin = (struct plugin_handle*) list_get_first(handle->loaded);
|
||||
while (plugin)
|
||||
{
|
||||
list_remove(handle->loaded, plugin);
|
||||
plugin_unload(plugin);
|
||||
plugin = (struct uhub_plugin_handle*) list_get_first(handle->loaded);
|
||||
plugin = (struct plugin_handle*) list_get_first(handle->loaded);
|
||||
}
|
||||
|
||||
list_destroy(handle->loaded);
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
struct hub_config;
|
||||
struct linked_list;
|
||||
struct uhub_plugin_handle;
|
||||
struct plugin_handle;
|
||||
|
||||
struct uhub_plugin
|
||||
{
|
||||
@@ -42,10 +42,10 @@ struct uhub_plugins
|
||||
};
|
||||
|
||||
// High level plugin loader ode
|
||||
extern struct uhub_plugin_handle* plugin_load(const char* filename, const char* config);
|
||||
extern void plugin_unload(struct uhub_plugin_handle* plugin);
|
||||
extern struct plugin_handle* plugin_load(const char* filename, const char* config);
|
||||
extern void plugin_unload(struct plugin_handle* plugin);
|
||||
|
||||
// extern void plugin_unload(struct uhub_plugin_handle*);
|
||||
// extern void plugin_unload(struct plugin_handle*);
|
||||
extern int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle);
|
||||
extern void plugin_shutdown(struct uhub_plugins* handle);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user