2020-03-16 20:50:31 +00:00
|
|
|
#include "webfused/auth/file_authenticator.h"
|
2020-03-17 12:17:33 +00:00
|
|
|
#include "webfused/auth/settings.h"
|
|
|
|
#include "webfused/auth/authenticator.h"
|
|
|
|
|
|
|
|
#include "webfuse/adapter/credentials.h"
|
|
|
|
#include "userdb/userdb.h"
|
2020-03-17 21:53:38 +00:00
|
|
|
#include "webfused/log/log.h"
|
2020-03-17 12:17:33 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct wfd_file_authenticator
|
|
|
|
{
|
|
|
|
char * filename;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
wfd_file_authenticator_dispose(
|
|
|
|
void * data)
|
|
|
|
{
|
|
|
|
struct wfd_file_authenticator * authenticator = data;
|
|
|
|
|
|
|
|
free(authenticator->filename);
|
|
|
|
free(authenticator);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
wfd_file_authenticator_authenticate(
|
|
|
|
struct wf_credentials * credentials,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
struct wfd_file_authenticator * authenticator = user_data;
|
|
|
|
|
|
|
|
char const * username = wf_credentials_get(credentials, "username");
|
|
|
|
char const * password = wf_credentials_get(credentials, "password");
|
|
|
|
if ((NULL != username) && (NULL != password))
|
|
|
|
{
|
|
|
|
struct userdb * db = userdb_create("");
|
|
|
|
result = userdb_load(db, authenticator->filename);
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
result = userdb_check(db, username, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
userdb_dispose(db);
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:53:38 +00:00
|
|
|
WFD_INFO("authenticate user \'%s\': %s",
|
|
|
|
(NULL != username) ? username : "<unknown>",
|
|
|
|
result ? "success" : "failure");
|
|
|
|
|
2020-03-17 12:17:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-17 15:49:17 +00:00
|
|
|
static char const *
|
|
|
|
wfd_file_authenticator_get_type(
|
|
|
|
void * data)
|
|
|
|
{
|
|
|
|
(void) data;
|
|
|
|
return "username";
|
|
|
|
}
|
|
|
|
|
2020-03-17 12:17:33 +00:00
|
|
|
static struct wfd_authenticator_vtable
|
|
|
|
wfd_file_authenticator_vtable =
|
|
|
|
{
|
|
|
|
.dispose = &wfd_file_authenticator_dispose,
|
2020-03-17 15:49:17 +00:00
|
|
|
.authenticate = &wfd_file_authenticator_authenticate,
|
|
|
|
.get_type = &wfd_file_authenticator_get_type
|
2020-03-17 12:17:33 +00:00
|
|
|
};
|
2020-03-16 20:50:31 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
wfd_file_authenticator_create(
|
|
|
|
struct wfd_auth_settings * settings,
|
|
|
|
struct wfd_authenticator * authenticator)
|
|
|
|
{
|
2020-03-17 12:17:33 +00:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
char const * filename = wfd_auth_settings_get(settings, "file");
|
|
|
|
if (NULL != filename)
|
|
|
|
{
|
|
|
|
struct wfd_file_authenticator * data = malloc(sizeof(struct wfd_file_authenticator));
|
|
|
|
data->filename = strdup(filename);
|
|
|
|
|
|
|
|
authenticator->vtable = &wfd_file_authenticator_vtable;
|
|
|
|
authenticator->data = data;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2020-03-16 20:50:31 +00:00
|
|
|
}
|