1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

Feature/authentication (#14)

* makes wsfs_server_config opaque

* feature: try to create mount point, if not present

* fixes server start failure due to existing mountpoint

* added basic authentication infrastructure

* makes wsfs_server_config opaque

* feature: try to create mount point, if not present

* fixes server start failure due to existing mountpoint

* added basic authentication infrastructure

* added unit tests for credentials

* added unit tests for authenticators

* propagates authenticators to server protocol

* enabled username authentication in daemon example

* adds example to compute password hash

* adds infrastructure to execute commands

* added userdb to encapsulate authentication stuff

* adds session and session_manager

* fixes warning about unused param

* moves some logic from server_protocol to session

* makes wsfs_server_config opaque

* feature: try to create mount point, if not present

* fixes server start failure due to existing mountpoint

* added basic authentication infrastructure

* makes wsfs_server_config opaque

* added unit tests for credentials

* added unit tests for authenticators

* propagates authenticators to server protocol

* enabled username authentication in daemon example

* adds example to compute password hash

* adds infrastructure to execute commands

* added userdb to encapsulate authentication stuff

* adds session and session_manager

* fixes warning about unused param

* moves some logic from server_protocol to session

* updates libcrypto to version 1.1.0
This commit is contained in:
Falk Werner
2019-03-23 22:53:14 +01:00
committed by GitHub
parent de9095a978
commit 48185776b6
32 changed files with 1969 additions and 112 deletions

View File

@@ -7,12 +7,15 @@
#include <unistd.h>
#include <getopt.h>
#include <jansson.h>
#include <wsfs_adapter.h>
struct args
{
struct wsfs_server_config config;
struct wsfs_server_config * config;
char * passwd_path;
bool show_help;
};
@@ -25,7 +28,7 @@ static void show_help(void)
"Websocket file system daemon\n"
"\n"
"Usage: wsfsd [m <mount_point>] [-d <document_root] [-n <vhost_name>] [-p <port>]\n"
" [-c <server_cert_path> -k] [<server_key_path>]\n"
" [-c <server_cert_path>] [-k <server_key_path>] [-P <passwd_path>]\n"
"\n"
"Options:\n"
"\t-m, --mount_point Path of mount point (required)\n"
@@ -34,9 +37,39 @@ static void show_help(void)
"\t-k, --server_key_path Path of servers private key (default: not set, TLS disabled)\n"
"\t-n, --vhost_name Name of virtual host (default: \"localhost\")\n"
"\t-p, --port Number of servers port (default: 8080)\n"
"\t-P, --passwd_path Path to password file (default: not set, authentication disabled)\n"
"\n");
}
static bool authenticate(struct wsfs_credentials * creds, void * user_data)
{
bool result = false;
struct args * args = user_data;
char const * username = wsfs_credentials_get(creds, "username");
char const * password = wsfs_credentials_get(creds, "password");
if ((NULL != username) && (NULL != password))
{
json_t * passwd = json_load_file(args->passwd_path, 0, NULL);
if (NULL != passwd)
{
json_t * user = json_object_get(passwd, username);
if (json_is_object(user))
{
json_t * password_holder = json_object_get(user, "password");
if (json_is_string(password_holder))
{
result = (0 == strcmp(password, json_string_value(password_holder)));
}
}
json_decref(passwd);
}
}
return result;
}
static int parse_arguments(int argc, char * argv[], struct args * args)
{
static struct option const options[] =
@@ -47,16 +80,18 @@ static int parse_arguments(int argc, char * argv[], struct args * args)
{"server_key_path", required_argument, NULL, 'k'},
{"vhost_name", required_argument, NULL, 'n'},
{"port", required_argument, NULL, 'p'},
{"passwd_path", required_argument, NULL, 'P'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
bool result = EXIT_SUCCESS;
bool finished = false;
bool has_mountpoint = false;
while ((!finished) && (EXIT_SUCCESS == result))
{
int option_index = 0;
int const c = getopt_long(argc, argv, "m:d:c:k:n:p:h", options, &option_index);
int const c = getopt_long(argc, argv, "m:d:c:k:n:p:P:h", options, &option_index);
switch (c)
{
@@ -68,27 +103,31 @@ static int parse_arguments(int argc, char * argv[], struct args * args)
finished = true;
break;
case 'm':
free(args->config.mount_point);
args->config.mount_point = strdup(optarg);
wsfs_server_config_set_mountpoint(args->config, optarg);
has_mountpoint = true;
break;
case 'd':
free(args->config.document_root);
args->config.document_root = strdup(optarg);
wsfs_server_config_set_documentroot(args->config, optarg);
break;
case 'c':
free(args->config.cert_path);
args->config.cert_path = strdup(optarg);
wsfs_server_config_set_certpath(args->config, optarg);
break;
case 'k':
free(args->config.key_path);
args->config.key_path = strdup(optarg);
wsfs_server_config_set_keypath(args->config, optarg);
break;
case 'n':
free(args->config.vhost_name);
args->config.vhost_name = strdup(optarg);
wsfs_server_config_set_vhostname(args->config, optarg);
break;
case 'p':
args->config.port = atoi(optarg);
wsfs_server_config_set_port(args->config, atoi(optarg));
break;
case 'P':
free(args->passwd_path);
args->passwd_path = strdup(optarg);
wsfs_server_config_add_authenticator(args->config,
"username",
&authenticate,
args);
break;
default:
fprintf(stderr, "error: unknown argument\n");
@@ -99,7 +138,7 @@ static int parse_arguments(int argc, char * argv[], struct args * args)
if ((EXIT_SUCCESS == result) && (!args->show_help))
{
if (NULL == args->config.mount_point)
if (!has_mountpoint)
{
fprintf(stderr, "error: missing mount point\n");
result = EXIT_FAILURE;
@@ -123,26 +162,19 @@ static void on_interrupt(int signal_id)
int main(int argc, char * argv[])
{
struct args args =
{
.config =
{
.mount_point = NULL,
.document_root = NULL,
.cert_path = NULL,
.key_path = NULL,
.vhost_name = strdup("localhost"),
.port = 8080,
},
.show_help = 0
};
struct args args;
args.config = wsfs_server_config_create();
wsfs_server_config_set_vhostname(args.config, "localhost");
wsfs_server_config_set_port(args.config, 8080);
args.passwd_path = NULL;
args.show_help = false;
int result = parse_arguments(argc, argv, &args);
if (!args.show_help)
{
signal(SIGINT, on_interrupt);
server = wsfs_server_create(&args.config);
server = wsfs_server_create(args.config);
if (NULL != server)
{
wsfs_server_run(server);
@@ -159,7 +191,8 @@ int main(int argc, char * argv[])
show_help();
}
wsfs_server_config_cleanup(&args.config);
free(args.passwd_path);
wsfs_server_config_dispose(args.config);
return result;
}