1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/lib/webfuse/adapter/impl/server_protocol.c

272 lines
7.6 KiB
C
Raw Normal View History

2019-03-26 22:04:53 +00:00
#include "webfuse/adapter/impl/server_protocol.h"
#include <stdlib.h>
feat(webfuse): add multiclient support (#23) * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * removes language settings, which contains alternating values * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * adds container_of * added dlist * allows multiple clients to connect * removes directory when session is closed * adds dependecy to uuid-dev * allow clients to register filesystems * updates documentation * moves mountpoint handling into filesystem: mountpoints are removed during session cleanup * adds filesystem name/id to request parameters * fixes security issue: add_filesystem did not check name * removes default link, if it is broken * recreates symlink "default", if filesystem is gone * updates documentation * fixes memory leak * makes authentication work .. again * updates provider to support changed protocol * removes execute right of hello.txt * fixes style issues * fixes javascript style issues * fixes flase positive from Flawfinder * fixes some javascript style issues * removes use of PATH_MAX * removes use of GNU extensions in container_of implementation * ignores findings of flawfinder * replaces dlist by slist * removes duplicate implementation of slist (message_queue)
2019-04-17 20:51:16 +00:00
#include <ctype.h>
2019-03-26 22:04:53 +00:00
#include <libwebsockets.h>
#include "webfuse/core/message.h"
#include "webfuse/core/util.h"
#include "webfuse/core/protocol_names.h"
2019-03-26 22:04:53 +00:00
#include "webfuse/adapter/impl/credentials.h"
#include "jsonrpc/request.h"
2020-02-16 20:03:17 +00:00
#include "webfuse/adapter/impl/uuid_mountpoint_factory.h"
2020-02-29 01:32:03 +00:00
#include "webfuse/core/status_intern.h"
2019-03-26 22:04:53 +00:00
#include "wf/timer/manager.h"
#include "wf/timer/timer.h"
2019-03-26 22:04:53 +00:00
static int wf_impl_server_protocol_callback(
struct lws * wsi,
enum lws_callback_reasons reason,
void * WF_UNUSED_PARAM(user),
void * in,
size_t len)
{
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
if (NULL == ws_protocol)
{
return 0;
}
2019-03-26 22:04:53 +00:00
struct wf_server_protocol * protocol = ws_protocol->user;
wf_timer_manager_check(protocol->timer_manager);
2019-03-26 22:04:53 +00:00
struct wf_impl_session * session = wf_impl_session_manager_get(&protocol->session_manager, wsi);
switch (reason)
{
case LWS_CALLBACK_PROTOCOL_INIT:
protocol->is_operational = true;
break;
2019-03-26 22:04:53 +00:00
case LWS_CALLBACK_ESTABLISHED:
session = wf_impl_session_manager_add(
&protocol->session_manager,
wsi,
&protocol->authenticators,
2020-02-16 20:03:17 +00:00
&protocol->mountpoint_factory,
protocol->timer_manager,
2020-02-29 01:32:03 +00:00
protocol->server);
2019-03-26 22:04:53 +00:00
if (NULL != session)
{
wf_impl_session_authenticate(session, NULL);
}
break;
case LWS_CALLBACK_CLOSED:
wf_impl_session_manager_remove(&protocol->session_manager, wsi);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
if (NULL != session)
{
wf_impl_session_onwritable(session);
}
break;
case LWS_CALLBACK_RECEIVE:
if (NULL != session)
{
wf_impl_session_receive(session, in, len);
}
break;
case LWS_CALLBACK_RAW_RX_FILE:
feat(webfuse): add multiclient support (#23) * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * removes language settings, which contains alternating values * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * adds container_of * added dlist * allows multiple clients to connect * removes directory when session is closed * adds dependecy to uuid-dev * allow clients to register filesystems * updates documentation * moves mountpoint handling into filesystem: mountpoints are removed during session cleanup * adds filesystem name/id to request parameters * fixes security issue: add_filesystem did not check name * removes default link, if it is broken * recreates symlink "default", if filesystem is gone * updates documentation * fixes memory leak * makes authentication work .. again * updates provider to support changed protocol * removes execute right of hello.txt * fixes style issues * fixes javascript style issues * fixes flase positive from Flawfinder * fixes some javascript style issues * removes use of PATH_MAX * removes use of GNU extensions in container_of implementation * ignores findings of flawfinder * replaces dlist by slist * removes duplicate implementation of slist (message_queue)
2019-04-17 20:51:16 +00:00
if (NULL != session)
{
wf_impl_session_process_filesystem_request(session, wsi);
}
2019-03-26 22:04:53 +00:00
break;
default:
break;
}
return 0;
}
struct wf_server_protocol * wf_impl_server_protocol_create(
char * mount_point)
{
struct wf_server_protocol * protocol = malloc(sizeof(struct wf_server_protocol));
if (NULL != protocol)
{
2020-02-16 20:03:17 +00:00
struct wf_impl_mountpoint_factory mountpoint_factory;
wf_impl_uuid_mountpoint_factory_init(&mountpoint_factory, mount_point);
wf_impl_server_protocol_init(protocol, &mountpoint_factory);
}
return protocol;
}
struct wf_server_protocol * wf_impl_server_protocol_create2(
wf_create_mountpoint_fn * create_mountpoint,
void * create_mountpoint_context)
{
struct wf_server_protocol * protocol = malloc(sizeof(struct wf_server_protocol));
if (NULL != protocol)
{
struct wf_impl_mountpoint_factory mountpoint_factory;
wf_impl_mountpoint_factory_init(&mountpoint_factory,
create_mountpoint, create_mountpoint_context, NULL);
wf_impl_server_protocol_init(protocol, &mountpoint_factory);
2019-03-26 22:04:53 +00:00
}
return protocol;
2020-02-16 20:03:17 +00:00
2019-03-26 22:04:53 +00:00
}
2020-02-16 20:03:17 +00:00
2019-03-26 22:04:53 +00:00
void wf_impl_server_protocol_dispose(
struct wf_server_protocol * protocol)
{
wf_impl_server_protocol_cleanup(protocol);
free(protocol);
}
void wf_impl_server_protocol_init_lws(
struct wf_server_protocol * protocol,
struct lws_protocols * lws_protocol)
{
lws_protocol->name = WF_PROTOCOL_NAME_ADAPTER_SERVER;
2019-03-26 22:04:53 +00:00
lws_protocol->callback = &wf_impl_server_protocol_callback;
lws_protocol->per_session_data_size = 0;
lws_protocol->user = protocol;
}
static void wf_impl_server_protocol_authenticate(
struct jsonrpc_request * request,
char const * WF_UNUSED_PARAM(method_name),
json_t * params,
void * WF_UNUSED_PARAM(user_data))
{
bool result = false;
json_t * type_holder = json_array_get(params, 0);
json_t * creds_holder = json_array_get(params, 1);
if (json_is_string(type_holder) && json_is_object(creds_holder))
{
char const * type = json_string_value(type_holder);
struct wf_credentials creds;
wf_impl_credentials_init(&creds, type, creds_holder);
struct wf_impl_session * session = jsonrpc_request_get_userdata(request);
result = wf_impl_session_authenticate(session, &creds);
wf_impl_credentials_cleanup(&creds);
}
if (result)
{
json_t * result = json_object();
jsonrpc_respond(request, result);
}
else
{
2020-02-29 01:32:03 +00:00
jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED, wf_status_tostring(WF_BAD_ACCESS_DENIED));
}
}
feat(webfuse): add multiclient support (#23) * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * removes language settings, which contains alternating values * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * adds container_of * added dlist * allows multiple clients to connect * removes directory when session is closed * adds dependecy to uuid-dev * allow clients to register filesystems * updates documentation * moves mountpoint handling into filesystem: mountpoints are removed during session cleanup * adds filesystem name/id to request parameters * fixes security issue: add_filesystem did not check name * removes default link, if it is broken * recreates symlink "default", if filesystem is gone * updates documentation * fixes memory leak * makes authentication work .. again * updates provider to support changed protocol * removes execute right of hello.txt * fixes style issues * fixes javascript style issues * fixes flase positive from Flawfinder * fixes some javascript style issues * removes use of PATH_MAX * removes use of GNU extensions in container_of implementation * ignores findings of flawfinder * replaces dlist by slist * removes duplicate implementation of slist (message_queue)
2019-04-17 20:51:16 +00:00
static bool wf_impl_server_protocol_check_name(char const * value)
{
while ('\0' != *value)
{
char const c = * value;
if (!isalpha(c) && !isdigit(c) && ('_' != c))
{
return false;
}
value++;
}
return true;
}
static void wf_impl_server_protocol_add_filesystem(
struct jsonrpc_request * request,
feat(webfuse): add multiclient support (#23) * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * removes language settings, which contains alternating values * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * adds container_of * added dlist * allows multiple clients to connect * removes directory when session is closed * adds dependecy to uuid-dev * allow clients to register filesystems * updates documentation * moves mountpoint handling into filesystem: mountpoints are removed during session cleanup * adds filesystem name/id to request parameters * fixes security issue: add_filesystem did not check name * removes default link, if it is broken * recreates symlink "default", if filesystem is gone * updates documentation * fixes memory leak * makes authentication work .. again * updates provider to support changed protocol * removes execute right of hello.txt * fixes style issues * fixes javascript style issues * fixes flase positive from Flawfinder * fixes some javascript style issues * removes use of PATH_MAX * removes use of GNU extensions in container_of implementation * ignores findings of flawfinder * replaces dlist by slist * removes duplicate implementation of slist (message_queue)
2019-04-17 20:51:16 +00:00
char const * WF_UNUSED_PARAM(method_name),
json_t * params,
void * WF_UNUSED_PARAM(user_data))
{
struct wf_impl_session * session = jsonrpc_request_get_userdata(request);
feat(webfuse): add multiclient support (#23) * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * removes language settings, which contains alternating values * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * adds container_of * added dlist * allows multiple clients to connect * removes directory when session is closed * adds dependecy to uuid-dev * allow clients to register filesystems * updates documentation * moves mountpoint handling into filesystem: mountpoints are removed during session cleanup * adds filesystem name/id to request parameters * fixes security issue: add_filesystem did not check name * removes default link, if it is broken * recreates symlink "default", if filesystem is gone * updates documentation * fixes memory leak * makes authentication work .. again * updates provider to support changed protocol * removes execute right of hello.txt * fixes style issues * fixes javascript style issues * fixes flase positive from Flawfinder * fixes some javascript style issues * removes use of PATH_MAX * removes use of GNU extensions in container_of implementation * ignores findings of flawfinder * replaces dlist by slist * removes duplicate implementation of slist (message_queue)
2019-04-17 20:51:16 +00:00
wf_status status = (session->is_authenticated) ? WF_GOOD : WF_BAD_ACCESS_DENIED;
char const * name = NULL;
if (WF_GOOD == status)
{
json_t * name_holder = json_array_get(params, 0);
if (json_is_string(name_holder))
{
name = json_string_value(name_holder);
if (wf_impl_server_protocol_check_name(name))
{
bool const success = wf_impl_session_add_filesystem(session, name);
if (!success)
{
status = WF_BAD;
}
}
else
{
status = WF_BAD_FORMAT;
}
}
else
{
status = WF_BAD_FORMAT;
}
}
if (WF_GOOD == status)
{
json_t * result = json_object();
json_object_set_new(result, "id", json_string(name));
jsonrpc_respond(request, result);
feat(webfuse): add multiclient support (#23) * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * removes language settings, which contains alternating values * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * adds container_of * added dlist * allows multiple clients to connect * removes directory when session is closed * adds dependecy to uuid-dev * allow clients to register filesystems * updates documentation * moves mountpoint handling into filesystem: mountpoints are removed during session cleanup * adds filesystem name/id to request parameters * fixes security issue: add_filesystem did not check name * removes default link, if it is broken * recreates symlink "default", if filesystem is gone * updates documentation * fixes memory leak * makes authentication work .. again * updates provider to support changed protocol * removes execute right of hello.txt * fixes style issues * fixes javascript style issues * fixes flase positive from Flawfinder * fixes some javascript style issues * removes use of PATH_MAX * removes use of GNU extensions in container_of implementation * ignores findings of flawfinder * replaces dlist by slist * removes duplicate implementation of slist (message_queue)
2019-04-17 20:51:16 +00:00
}
else
{
2020-02-29 01:32:03 +00:00
jsonrpc_respond_error(request, status, wf_status_tostring(status));
feat(webfuse): add multiclient support (#23) * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * removes language settings, which contains alternating values * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * adds container_of * added dlist * allows multiple clients to connect * removes directory when session is closed * adds dependecy to uuid-dev * allow clients to register filesystems * updates documentation * moves mountpoint handling into filesystem: mountpoints are removed during session cleanup * adds filesystem name/id to request parameters * fixes security issue: add_filesystem did not check name * removes default link, if it is broken * recreates symlink "default", if filesystem is gone * updates documentation * fixes memory leak * makes authentication work .. again * updates provider to support changed protocol * removes execute right of hello.txt * fixes style issues * fixes javascript style issues * fixes flase positive from Flawfinder * fixes some javascript style issues * removes use of PATH_MAX * removes use of GNU extensions in container_of implementation * ignores findings of flawfinder * replaces dlist by slist * removes duplicate implementation of slist (message_queue)
2019-04-17 20:51:16 +00:00
}
}
void wf_impl_server_protocol_init(
2019-03-26 22:04:53 +00:00
struct wf_server_protocol * protocol,
2020-02-16 20:03:17 +00:00
struct wf_impl_mountpoint_factory * mountpoint_factory)
2019-03-26 22:04:53 +00:00
{
protocol->is_operational = false;
feat(webfuse): add multiclient support (#23) * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * adds missing toolchain headers to project * renames container macros * adds gdbserver * removes language settings, which contains alternating values * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * fixes verbosity option when set through command line * adds support for build type and allows to run gdb in container * renames container macros * adds gdbserver * adds wrapper script to launch gdbserver * fix docker command in wrapper script * fixes run in dind setup * replaces docker's init through dump-init * moves filesystem to session * adds container_of * added dlist * allows multiple clients to connect * removes directory when session is closed * adds dependecy to uuid-dev * allow clients to register filesystems * updates documentation * moves mountpoint handling into filesystem: mountpoints are removed during session cleanup * adds filesystem name/id to request parameters * fixes security issue: add_filesystem did not check name * removes default link, if it is broken * recreates symlink "default", if filesystem is gone * updates documentation * fixes memory leak * makes authentication work .. again * updates provider to support changed protocol * removes execute right of hello.txt * fixes style issues * fixes javascript style issues * fixes flase positive from Flawfinder * fixes some javascript style issues * removes use of PATH_MAX * removes use of GNU extensions in container_of implementation * ignores findings of flawfinder * replaces dlist by slist * removes duplicate implementation of slist (message_queue)
2019-04-17 20:51:16 +00:00
2020-02-16 20:03:17 +00:00
wf_impl_mountpoint_factory_move(mountpoint_factory, &protocol->mountpoint_factory);
protocol->timer_manager = wf_timer_manager_create();
2019-03-26 22:04:53 +00:00
wf_impl_session_manager_init(&protocol->session_manager);
wf_impl_authenticators_init(&protocol->authenticators);
2020-02-29 01:32:03 +00:00
protocol->server = jsonrpc_server_create();
jsonrpc_server_add(protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol);
jsonrpc_server_add(protocol->server, "add_filesystem", &wf_impl_server_protocol_add_filesystem, protocol);
2019-03-26 22:04:53 +00:00
}
void wf_impl_server_protocol_cleanup(
struct wf_server_protocol * protocol)
{
protocol->is_operational = false;
2020-02-29 01:32:03 +00:00
jsonrpc_server_dispose(protocol->server);
wf_timer_manager_dispose(protocol->timer_manager);
2019-03-26 22:04:53 +00:00
wf_impl_authenticators_cleanup(&protocol->authenticators);
wf_impl_session_manager_cleanup(&protocol->session_manager);
2020-02-16 20:03:17 +00:00
wf_impl_mountpoint_factory_cleanup(&protocol->mountpoint_factory);
2019-03-26 22:04:53 +00:00
}
void wf_impl_server_protocol_add_authenticator(
struct wf_server_protocol * protocol,
char const * type,
wf_authenticate_fn * authenticate,
void * user_data)
{
wf_impl_authenticators_add(&protocol->authenticators, type, authenticate, user_data);
}