2010-07-08 12:24:40 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
|
|
|
* Copyright (C) 2010, Jan Vidar Krey
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "plugin_api/handle.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/list.h"
|
|
|
|
#include "util/ipcalc.h"
|
2010-07-10 01:36:47 +00:00
|
|
|
#include "util/misc.h"
|
|
|
|
#include "util/log.h"
|
|
|
|
#include "util/config_token.h"
|
2010-07-08 12:24:40 +00:00
|
|
|
|
2010-07-29 06:41:26 +00:00
|
|
|
static void set_error_message(struct uhub_plugin_handle* plugin, const char* msg)
|
|
|
|
{
|
|
|
|
plugin->error_msg = msg;
|
|
|
|
}
|
|
|
|
|
2010-07-10 01:36:47 +00:00
|
|
|
struct acl_list
|
2010-07-08 12:24:40 +00:00
|
|
|
{
|
2010-07-10 01:36:47 +00:00
|
|
|
struct linked_list* users;
|
2010-07-08 12:24:40 +00:00
|
|
|
};
|
|
|
|
|
2010-07-10 01:36:47 +00:00
|
|
|
void insert_user(struct linked_list* users, const char* nick, const char* pass, enum auth_credentials cred)
|
2010-07-08 12:24:40 +00:00
|
|
|
{
|
2010-07-10 01:36:47 +00:00
|
|
|
struct auth_info* data = (struct auth_info*) hub_malloc_zero(sizeof(struct auth_info));
|
|
|
|
strncpy(data->nickname, nick, MAX_NICK_LEN);
|
|
|
|
strncpy(data->password, pass, MAX_PASS_LEN);
|
|
|
|
data->credentials = cred;
|
|
|
|
list_append(users, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_line(char* line, int line_count, void* ptr_data)
|
2010-07-08 12:24:40 +00:00
|
|
|
{
|
2010-07-10 01:36:47 +00:00
|
|
|
struct linked_list* users = (struct linked_list*) ptr_data;
|
2010-07-18 17:57:07 +00:00
|
|
|
struct cfg_tokens* tokens = cfg_tokenize(line);
|
2010-07-10 01:36:47 +00:00
|
|
|
enum auth_credentials cred;
|
|
|
|
|
2010-07-18 17:57:07 +00:00
|
|
|
if (cfg_token_count(tokens) == 0)
|
2010-07-10 01:36:47 +00:00
|
|
|
return 0;
|
|
|
|
|
2010-07-18 17:57:07 +00:00
|
|
|
if (cfg_token_count(tokens) < 2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
char* credential = cfg_token_get_first(tokens);
|
|
|
|
char* username = cfg_token_get_next(tokens);
|
|
|
|
char* password = cfg_token_get_next(tokens);
|
2010-07-10 01:36:47 +00:00
|
|
|
|
2010-07-12 15:00:42 +00:00
|
|
|
if (strcmp(credential, "admin")) cred = auth_cred_admin;
|
|
|
|
else if (strcmp(credential, "super")) cred = auth_cred_super;
|
|
|
|
else if (strcmp(credential, "op")) cred = auth_cred_operator;
|
|
|
|
else if (strcmp(credential, "reg")) cred = auth_cred_user;
|
2010-07-10 01:36:47 +00:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
insert_user(users, username, password, cred);
|
|
|
|
cfg_tokens_free(tokens);
|
|
|
|
return 0;
|
2010-07-08 12:24:40 +00:00
|
|
|
}
|
|
|
|
|
2010-07-10 01:36:47 +00:00
|
|
|
|
2010-07-18 17:57:07 +00:00
|
|
|
static struct acl_list* load_acl(const char* filename, struct uhub_plugin_handle* handle)
|
2010-07-08 12:24:40 +00:00
|
|
|
{
|
|
|
|
struct acl_list* list = (struct acl_list*) hub_malloc(sizeof(struct acl_list));
|
|
|
|
struct linked_list* users = list_create();
|
|
|
|
|
2010-07-29 06:41:26 +00:00
|
|
|
if (!list || !users)
|
|
|
|
{
|
|
|
|
list_destroy(users);
|
|
|
|
hub_free(list);
|
|
|
|
set_error_message(handle, "Unable to allocate memory");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!filename || !*filename)
|
2010-07-08 12:24:40 +00:00
|
|
|
{
|
|
|
|
list_destroy(users);
|
|
|
|
hub_free(list);
|
2010-07-29 06:41:26 +00:00
|
|
|
set_error_message(handle, "No configuration file given");
|
2010-07-08 12:24:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-10 01:36:47 +00:00
|
|
|
if (users)
|
|
|
|
{
|
|
|
|
if (file_read_lines(filename, users, &parse_line) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to load %s\n", filename);
|
2010-07-29 06:41:26 +00:00
|
|
|
set_error_message(handle, "Unable to load file");
|
2010-07-10 01:36:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 12:24:40 +00:00
|
|
|
list->users = users;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unload_acl(struct acl_list* list)
|
|
|
|
{
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2010-07-10 01:36:47 +00:00
|
|
|
list_clear(list->users, hub_free);
|
2010-07-08 12:24:40 +00:00
|
|
|
list_destroy(list->users);
|
|
|
|
hub_free(list);
|
|
|
|
}
|
|
|
|
|
2010-07-10 01:36:47 +00:00
|
|
|
static int get_user(const char* nickname, struct auth_info* info)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static plugin_st register_user(struct auth_info* user)
|
|
|
|
{
|
2010-07-18 17:57:07 +00:00
|
|
|
/* Read only mode - so rejected */
|
2010-07-10 01:36:47 +00:00
|
|
|
return st_deny;
|
|
|
|
}
|
|
|
|
|
|
|
|
static plugin_st update_user(struct auth_info* user)
|
|
|
|
{
|
2010-07-18 17:57:07 +00:00
|
|
|
/* Read only mode - so rejected */
|
2010-07-10 01:36:47 +00:00
|
|
|
return st_deny;
|
|
|
|
}
|
|
|
|
|
|
|
|
static plugin_st delete_user(struct auth_info* user)
|
|
|
|
{
|
2010-07-18 17:57:07 +00:00
|
|
|
/* Read only mode - so rejected */
|
2010-07-10 01:36:47 +00:00
|
|
|
return st_deny;
|
|
|
|
}
|
|
|
|
|
2010-07-08 12:24:40 +00:00
|
|
|
int plugin_register(struct uhub_plugin_handle* plugin, const char* config)
|
|
|
|
{
|
|
|
|
plugin->name = "File authentication plugin";
|
|
|
|
plugin->version = "0.1";
|
2010-07-18 17:57:07 +00:00
|
|
|
plugin->description = "Authenticated users based on a read-only text file.";
|
2010-07-08 12:24:40 +00:00
|
|
|
plugin->plugin_api_version = PLUGIN_API_VERSION;
|
|
|
|
plugin->plugin_funcs_size = sizeof(struct plugin_funcs);
|
|
|
|
memset(&plugin->funcs, 0, sizeof(struct plugin_funcs));
|
|
|
|
|
2010-07-10 01:36:47 +00:00
|
|
|
// Authentication actions.
|
|
|
|
plugin->funcs.auth_get_user = get_user;
|
|
|
|
plugin->funcs.auth_register_user = register_user;
|
|
|
|
plugin->funcs.auth_update_user = update_user;
|
|
|
|
plugin->funcs.auth_delete_user = delete_user;
|
|
|
|
|
2010-07-29 06:41:26 +00:00
|
|
|
plugin->ptr = load_acl(config, plugin);
|
|
|
|
if (plugin->ptr)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
2010-07-08 12:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int plugin_unregister(struct uhub_plugin_handle* plugin)
|
|
|
|
{
|
2010-07-18 17:57:07 +00:00
|
|
|
set_error_message(plugin, 0);
|
2010-07-08 12:24:40 +00:00
|
|
|
unload_acl(plugin->ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|