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/wsfs/adapter/impl/server.c

154 lines
4.0 KiB
C
Raw Normal View History

#include "wsfs/adapter/server.h"
2019-01-27 02:45:03 +00:00
#include <stdlib.h>
#include <stdbool.h>
#include <libwebsockets.h>
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
2019-03-23 21:53:14 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "wsfs/adapter/impl/server_config.h"
#include "wsfs/adapter/impl/server_protocol.h"
2019-01-27 02:45:03 +00:00
2019-02-10 14:12:14 +00:00
#define WSFS_DISABLE_LWS_LOG 0
#define WSFS_SERVER_PROTOCOL_COUNT 3
#define WSFS_SERVER_TIMEOUT (1 * 1000)
2019-01-27 02:45:03 +00:00
struct wsfs_server
{
struct wsfs_server_config config;
struct wsfs_server_protocol protocol;
struct lws_protocols ws_protocols[WSFS_SERVER_PROTOCOL_COUNT];
struct lws_context * context;
volatile bool shutdown_requested;
2019-02-09 02:08:02 +00:00
struct lws_http_mount mount;
struct lws_context_creation_info info;
2019-01-27 02:45:03 +00:00
};
static bool wsfs_impl_server_tls_enabled(
2019-01-27 02:45:03 +00:00
struct wsfs_server * server)
{
return ((server->config.key_path != NULL) && (server->config.cert_path != NULL));
}
static struct lws_context * wsfs_impl_server_context_create(
struct wsfs_server * server)
2019-01-27 02:45:03 +00:00
{
2019-02-10 14:12:14 +00:00
lws_set_log_level(WSFS_DISABLE_LWS_LOG, NULL);
memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WSFS_SERVER_PROTOCOL_COUNT);
server->ws_protocols[0].name = "http";
server->ws_protocols[0].callback = lws_callback_http_dummy;
server->ws_protocols[1].name = "fs";
wsfs_impl_server_protocol_init_lws(&server->protocol, &server->ws_protocols[1]);
2019-01-27 02:45:03 +00:00
2019-02-09 02:08:02 +00:00
memset(&server->mount, 0, sizeof(struct lws_http_mount));
server->mount.mount_next = NULL,
server->mount.mountpoint = "/",
server->mount.origin = server->config.document_root,
server->mount.def = "index.html",
server->mount.origin_protocol = LWSMPRO_FILE,
server->mount.mountpoint_len = 1,
memset(&server->info, 0, sizeof(struct lws_context_creation_info));
server->info.port = server->config.port;
server->info.mounts = &server->mount;
server->info.protocols = server->ws_protocols;
server->info.vhost_name = server->config.vhost_name;
server->info.ws_ping_pong_interval = 10;
server->info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
2019-01-27 02:45:03 +00:00
if (NULL == server->config.document_root)
{
// disable http
2019-02-09 02:08:02 +00:00
server->info.protocols = &server->ws_protocols[1];
server->info.mounts = NULL;
2019-01-27 02:45:03 +00:00
}
if (wsfs_impl_server_tls_enabled(server))
2019-01-27 02:45:03 +00:00
{
2019-02-09 02:08:02 +00:00
server->info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
server->info.ssl_cert_filepath = server->config.cert_path;
server->info.ssl_private_key_filepath = server->config.key_path;
2019-01-27 02:45:03 +00:00
}
2019-02-09 02:08:02 +00:00
struct lws_context * const context = lws_create_context(&server->info);
return context;
2019-01-27 02:45:03 +00:00
}
static bool wsfs_impl_server_check_mountpoint(
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
2019-03-23 21:53:14 +00:00
struct wsfs_server_config * config)
2019-01-27 02:45:03 +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
2019-03-23 21:53:14 +00:00
bool result = false;
if (NULL != config->mount_point)
{
struct stat info;
int const rc = stat(config->mount_point, &info);
result = ((0 == rc) && (S_ISDIR(info.st_mode)));
if (!result)
2019-02-09 02:08:02 +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
2019-03-23 21:53:14 +00:00
result = (0 == mkdir(config->mount_point, 0755));
2019-02-09 02:08:02 +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
2019-03-23 21:53:14 +00:00
}
return result;
}
struct wsfs_server * wsfs_impl_server_create(
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
2019-03-23 21:53:14 +00:00
struct wsfs_server_config * config)
{
struct wsfs_server * server = NULL;
if (wsfs_impl_server_check_mountpoint(config))
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
2019-03-23 21:53:14 +00:00
{
server = malloc(sizeof(struct wsfs_server));
if (NULL != server)
2019-02-09 02:08:02 +00:00
{
if (wsfs_impl_server_protocol_init(&server->protocol, config->mount_point))
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
2019-03-23 21:53:14 +00:00
{
server->shutdown_requested = false;
wsfs_impl_server_config_clone(config, &server->config);
wsfs_impl_authenticators_move(&server->config.authenticators, &server->protocol.authenticators);
server->context = wsfs_impl_server_context_create(server);
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
2019-03-23 21:53:14 +00:00
}
else
{
free(server);
server = NULL;
}
}
}
return server;
2019-01-27 02:45:03 +00:00
}
void wsfs_impl_server_dispose(
struct wsfs_server * server)
2019-01-27 02:45:03 +00:00
{
lws_context_destroy(server->context);
wsfs_impl_server_protocol_cleanup(&server->protocol);
wsfs_impl_server_config_cleanup(&server->config);
free(server);
2019-01-27 02:45:03 +00:00
}
void wsfs_impl_server_run(
struct wsfs_server * server)
2019-01-27 02:45:03 +00:00
{
int n = 0;
while ((0 <= n) && (!server->shutdown_requested))
{
n = lws_service(server->context, WSFS_SERVER_TIMEOUT);
}
2019-01-27 02:45:03 +00:00
}
void wsfs_impl_server_shutdown(
struct wsfs_server * server)
2019-01-27 02:45:03 +00:00
{
server->shutdown_requested = true;
2019-01-27 02:45:03 +00:00
}