mirror of
https://github.com/falk-werner/webfuse
synced 2025-06-13 12:54:15 +00:00
enabled username authentication in daemon example
This commit is contained in:
parent
511d819241
commit
b6935d753e
@ -7,12 +7,15 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include <wsfs_adapter.h>
|
||||
|
||||
|
||||
struct args
|
||||
{
|
||||
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,6 +80,7 @@ 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}
|
||||
};
|
||||
@ -57,7 +91,7 @@ static int parse_arguments(int argc, char * argv[], struct args * args)
|
||||
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)
|
||||
{
|
||||
@ -87,6 +121,14 @@ static int parse_arguments(int argc, char * argv[], struct args * args)
|
||||
case 'p':
|
||||
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");
|
||||
result = EXIT_FAILURE;
|
||||
@ -124,6 +166,7 @@ int main(int argc, char * argv[])
|
||||
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);
|
||||
@ -148,6 +191,7 @@ int main(int argc, char * argv[])
|
||||
show_help();
|
||||
}
|
||||
|
||||
free(args.passwd_path);
|
||||
wsfs_server_config_dispose(args.config);
|
||||
return result;
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ extern WSFSA_API void wsfs_server_config_set_port(
|
||||
struct wsfs_server_config * config,
|
||||
int port);
|
||||
|
||||
extern WSFSA_API void wsfs_server_add_authenticator(
|
||||
extern WSFSA_API void wsfs_server_config_add_authenticator(
|
||||
struct wsfs_server_config * config,
|
||||
char const * type,
|
||||
wsfs_authenticate_fn * authenticate,
|
||||
|
@ -115,7 +115,7 @@ void wsfs_server_config_set_port(
|
||||
config->port = port;
|
||||
}
|
||||
|
||||
void wsfs_server_add_authenticator(
|
||||
void wsfs_server_config_add_authenticator(
|
||||
struct wsfs_server_config * config,
|
||||
char const * type,
|
||||
wsfs_authenticate_fn * authenticate,
|
||||
|
Loading…
Reference in New Issue
Block a user