2019-01-27 02:45:03 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
2019-01-31 20:29:42 +00:00
|
|
|
#include <stdbool.h>
|
2019-02-05 23:58:51 +00:00
|
|
|
#include <signal.h>
|
2019-01-31 20:29:42 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
#include <webfuse_adapter.h>
|
2019-04-01 20:15:12 +00:00
|
|
|
#include <userdb.h>
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-01-31 20:29:42 +00:00
|
|
|
struct args
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_server_config * config;
|
2019-03-23 21:53:14 +00:00
|
|
|
char * passwd_path;
|
2019-01-31 20:43:49 +00:00
|
|
|
bool show_help;
|
2019-01-27 02:45:03 +00:00
|
|
|
};
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
static struct wf_server * server;
|
2019-02-05 23:58:51 +00:00
|
|
|
|
2019-01-27 02:45:03 +00:00
|
|
|
static void show_help(void)
|
|
|
|
{
|
|
|
|
printf(
|
2019-03-26 22:04:53 +00:00
|
|
|
"webfused, Copyright (c) 2019, webfuse authors <https://github.com/falk-werner/webfuse>\n"
|
2019-01-31 20:29:42 +00:00
|
|
|
"Websocket file system daemon\n"
|
2019-01-27 02:45:03 +00:00
|
|
|
"\n"
|
2019-03-26 22:04:53 +00:00
|
|
|
"Usage: webfused [m <mount_point>] [-d <document_root] [-n <vhost_name>] [-p <port>]\n"
|
2019-03-23 21:53:14 +00:00
|
|
|
" [-c <server_cert_path>] [-k <server_key_path>] [-P <passwd_path>]\n"
|
2019-01-31 20:29:42 +00:00
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
"\t-m, --mount_point Path of mount point (required)\n"
|
|
|
|
"\t-d, --document_root Path of www directory (default: not set, www disabled)\n"
|
|
|
|
"\t-c, --server_cert_path Path of servers own certificate (default: not set, TLS disabled)\n"
|
|
|
|
"\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"
|
2019-03-23 21:53:14 +00:00
|
|
|
"\t-P, --passwd_path Path to password file (default: not set, authentication disabled)\n"
|
2019-01-27 02:45:03 +00:00
|
|
|
"\n");
|
|
|
|
}
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
static bool authenticate(struct wf_credentials * creds, void * user_data)
|
2019-03-23 21:53:14 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
struct args * args = user_data;
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
char const * username = wf_credentials_get(creds, "username");
|
|
|
|
char const * password = wf_credentials_get(creds, "password");
|
2019-03-23 21:53:14 +00:00
|
|
|
if ((NULL != username) && (NULL != password))
|
|
|
|
{
|
2019-04-01 20:15:12 +00:00
|
|
|
struct userdb * db = userdb_create("<pepper>");
|
|
|
|
result = userdb_load(db, args->passwd_path);
|
|
|
|
if (result)
|
2019-03-23 21:53:14 +00:00
|
|
|
{
|
2019-04-01 20:15:12 +00:00
|
|
|
result = userdb_check(db, username, password);
|
|
|
|
userdb_dispose(db);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
return result;
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 20:29:42 +00:00
|
|
|
static int parse_arguments(int argc, char * argv[], struct args * args)
|
|
|
|
{
|
|
|
|
static struct option const options[] =
|
|
|
|
{
|
|
|
|
{"mount_point", required_argument, NULL, 'm'},
|
|
|
|
{"document_root", required_argument, NULL, 'd'},
|
|
|
|
{"server_cert_path", required_argument, NULL, 'c'},
|
|
|
|
{"server_key_path", required_argument, NULL, 'k'},
|
|
|
|
{"vhost_name", required_argument, NULL, 'n'},
|
|
|
|
{"port", required_argument, NULL, 'p'},
|
2019-03-23 21:53:14 +00:00
|
|
|
{"passwd_path", required_argument, NULL, 'P'},
|
2019-01-31 20:29:42 +00:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool result = EXIT_SUCCESS;
|
|
|
|
bool finished = false;
|
2019-03-23 21:53:14 +00:00
|
|
|
bool has_mountpoint = false;
|
2019-01-31 20:29:42 +00:00
|
|
|
while ((!finished) && (EXIT_SUCCESS == result))
|
|
|
|
{
|
|
|
|
int option_index = 0;
|
2019-03-23 21:53:14 +00:00
|
|
|
int const c = getopt_long(argc, argv, "m:d:c:k:n:p:P:h", options, &option_index);
|
2019-01-31 20:29:42 +00:00
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
finished = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
args->show_help = true;
|
|
|
|
finished = true;
|
|
|
|
break;
|
|
|
|
case 'm':
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_config_set_mountpoint(args->config, optarg);
|
2019-03-23 21:53:14 +00:00
|
|
|
has_mountpoint = true;
|
2019-01-31 20:29:42 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_config_set_documentroot(args->config, optarg);
|
2019-01-31 20:29:42 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_config_set_certpath(args->config, optarg);
|
2019-01-31 20:29:42 +00:00
|
|
|
break;
|
|
|
|
case 'k':
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_config_set_keypath(args->config, optarg);
|
2019-01-31 20:29:42 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_config_set_vhostname(args->config, optarg);
|
2019-01-31 20:29:42 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_config_set_port(args->config, atoi(optarg));
|
2019-03-23 21:53:14 +00:00
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
free(args->passwd_path);
|
|
|
|
args->passwd_path = strdup(optarg);
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_config_add_authenticator(args->config,
|
2019-03-23 21:53:14 +00:00
|
|
|
"username",
|
|
|
|
&authenticate,
|
|
|
|
args);
|
2019-01-31 20:29:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "error: unknown argument\n");
|
|
|
|
result = EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 20:43:49 +00:00
|
|
|
if ((EXIT_SUCCESS == result) && (!args->show_help))
|
2019-01-31 20:29:42 +00:00
|
|
|
{
|
2019-03-23 21:53:14 +00:00
|
|
|
if (!has_mountpoint)
|
2019-01-31 20:43:49 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "error: missing mount point\n");
|
|
|
|
result = EXIT_FAILURE;
|
|
|
|
}
|
2019-01-31 20:29:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 20:43:49 +00:00
|
|
|
if (EXIT_SUCCESS != result)
|
2019-01-31 20:29:42 +00:00
|
|
|
{
|
2019-01-31 20:43:49 +00:00
|
|
|
args->show_help = true;
|
2019-01-31 20:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-05 23:58:51 +00:00
|
|
|
static void on_interrupt(int signal_id)
|
|
|
|
{
|
|
|
|
(void) signal_id;
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_shutdown(server);
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 02:45:03 +00:00
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
2019-03-23 21:53:14 +00:00
|
|
|
struct args args;
|
2019-03-26 22:04:53 +00:00
|
|
|
args.config = wf_server_config_create();
|
|
|
|
wf_server_config_set_vhostname(args.config, "localhost");
|
|
|
|
wf_server_config_set_port(args.config, 8080);
|
2019-03-23 21:53:14 +00:00
|
|
|
args.passwd_path = NULL;
|
|
|
|
args.show_help = false;
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2019-01-31 20:29:42 +00:00
|
|
|
int result = parse_arguments(argc, argv, &args);
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2019-01-31 20:29:42 +00:00
|
|
|
if (!args.show_help)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-02-05 23:58:51 +00:00
|
|
|
signal(SIGINT, on_interrupt);
|
2019-03-26 22:04:53 +00:00
|
|
|
server = wf_server_create(args.config);
|
2019-01-27 02:45:03 +00:00
|
|
|
if (NULL != server)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_run(server);
|
|
|
|
wf_server_dispose(server);
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "fatal: unable start server\n");
|
|
|
|
result = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-31 20:29:42 +00:00
|
|
|
show_help();
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 21:53:14 +00:00
|
|
|
free(args.passwd_path);
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_server_config_dispose(args.config);
|
2019-01-27 02:45:03 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|