passdb-static.c revision 0a58543e241f335f0048d11de84c46b820cf1fd1
039cd2c4871a00e51af909222a34695d9cec3000vboxsync/* Copyright (c) 2010-2016 Dovecot authors, see the included COPYING file */
7defbe0b8606d5e66c4bb527e2dc5c5883ab3802vboxsync
039cd2c4871a00e51af909222a34695d9cec3000vboxsync#include "auth-common.h"
039cd2c4871a00e51af909222a34695d9cec3000vboxsync#include "passdb.h"
039cd2c4871a00e51af909222a34695d9cec3000vboxsync#include "passdb-template.h"
f1301dd8b6870b5a25c7dbdd46e0a0671bb62031vboxsync#include "password-scheme.h"
039cd2c4871a00e51af909222a34695d9cec3000vboxsync
039cd2c4871a00e51af909222a34695d9cec3000vboxsyncstruct static_passdb_module {
039cd2c4871a00e51af909222a34695d9cec3000vboxsync struct passdb_module module;
039cd2c4871a00e51af909222a34695d9cec3000vboxsync struct passdb_template *tmpl;
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync const char *static_password_tmpl;
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync};
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncstatic enum passdb_result
039cd2c4871a00e51af909222a34695d9cec3000vboxsyncstatic_save_fields(struct auth_request *request, const char **password_r, const char **scheme_r)
039cd2c4871a00e51af909222a34695d9cec3000vboxsync{
7defbe0b8606d5e66c4bb527e2dc5c5883ab3802vboxsync struct static_passdb_module *module =
7defbe0b8606d5e66c4bb527e2dc5c5883ab3802vboxsync (struct static_passdb_module *)request->passdb->passdb;
039cd2c4871a00e51af909222a34695d9cec3000vboxsync
039cd2c4871a00e51af909222a34695d9cec3000vboxsync auth_request_log_debug(request, AUTH_SUBSYS_DB, "lookup");
039cd2c4871a00e51af909222a34695d9cec3000vboxsync passdb_template_export(module->tmpl, request);
039cd2c4871a00e51af909222a34695d9cec3000vboxsync
039cd2c4871a00e51af909222a34695d9cec3000vboxsync if (module->static_password_tmpl != NULL) {
039cd2c4871a00e51af909222a34695d9cec3000vboxsync *password_r = t_auth_request_var_expand(
c9a2df6a889ca4118cc41c73ef67f4e559731dcavboxsync module->static_password_tmpl, request, NULL);
039cd2c4871a00e51af909222a34695d9cec3000vboxsync } else if (auth_fields_exists(request->extra_fields, "nopassword")) {
039cd2c4871a00e51af909222a34695d9cec3000vboxsync *password_r = "";
039cd2c4871a00e51af909222a34695d9cec3000vboxsync } else {
0b2fe03525bf326aeae736bb44c572debe8ba180vboxsync auth_request_log_info(request, AUTH_SUBSYS_DB,
0b2fe03525bf326aeae736bb44c572debe8ba180vboxsync "No password returned (and no nopassword)");
8657e832440d81752b123c03be7d68fd4ec58cefvboxsync *password_r = NULL;
f1301dd8b6870b5a25c7dbdd46e0a0671bb62031vboxsync *scheme_r = NULL;
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync return PASSDB_RESULT_PASSWORD_MISMATCH;
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync }
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync
039cd2c4871a00e51af909222a34695d9cec3000vboxsync *scheme_r = password_get_scheme(password_r);
db6431d7b73ab0741bc1f6d0dd7624e848b89a78vboxsync
039cd2c4871a00e51af909222a34695d9cec3000vboxsync if (*scheme_r == NULL)
039cd2c4871a00e51af909222a34695d9cec3000vboxsync *scheme_r = STATIC_PASS_SCHEME;
039cd2c4871a00e51af909222a34695d9cec3000vboxsync
f1301dd8b6870b5a25c7dbdd46e0a0671bb62031vboxsync auth_request_set_field(request, "password",
f1301dd8b6870b5a25c7dbdd46e0a0671bb62031vboxsync *password_r, *scheme_r);
039cd2c4871a00e51af909222a34695d9cec3000vboxsync
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync return PASSDB_RESULT_OK;
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync}
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync
039cd2c4871a00e51af909222a34695d9cec3000vboxsyncstatic void
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsyncstatic_verify_plain(struct auth_request *request, const char *password,
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync verify_plain_callback_t *callback)
039cd2c4871a00e51af909222a34695d9cec3000vboxsync{
c9a2df6a889ca4118cc41c73ef67f4e559731dcavboxsync enum passdb_result result;
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync const char *static_password;
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync const char *static_scheme;
039cd2c4871a00e51af909222a34695d9cec3000vboxsync
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync int ret;
ba31bc205e96548d3557ae82087dc020a52b6a0avboxsync
db6431d7b73ab0741bc1f6d0dd7624e848b89a78vboxsync result = static_save_fields(request, &static_password, &static_scheme);
7defbe0b8606d5e66c4bb527e2dc5c5883ab3802vboxsync if (result != PASSDB_RESULT_OK) {
7defbe0b8606d5e66c4bb527e2dc5c5883ab3802vboxsync callback(result, request);
return;
}
ret = auth_request_password_verify(request, password, static_password,
static_scheme, AUTH_SUBSYS_DB);
if (ret <= 0) {
callback(PASSDB_RESULT_PASSWORD_MISMATCH, request);
return;
}
callback(PASSDB_RESULT_OK, request);
}
static void
static_lookup_credentials(struct auth_request *request,
lookup_credentials_callback_t *callback)
{
enum passdb_result result;
const char *static_password;
const char *static_scheme;
result = static_save_fields(request, &static_password, &static_scheme);
passdb_handle_credentials(result, static_password,
static_scheme, callback, request);
}
static struct passdb_module *
static_preinit(pool_t pool, const char *args)
{
struct static_passdb_module *module;
const char *value;
module = p_new(pool, struct static_passdb_module, 1);
module->tmpl = passdb_template_build(pool, args);
if (passdb_template_remove(module->tmpl, "password", &value))
module->static_password_tmpl = value;
return &module->module;
}
struct passdb_module_interface passdb_static = {
"static",
static_preinit,
NULL,
NULL,
static_verify_plain,
static_lookup_credentials,
NULL
};