userdb-checkpassword.c revision 9d47b3d53a3ecacfe07d03e5b2339e6043e0f48e
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen/* Copyright (c) 2004-2014 Dovecot authors, see the included COPYING file */
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen#include "auth-common.h"
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen#include "userdb.h"
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen#ifdef USERDB_CHECKPASSWORD
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen#include "db-checkpassword.h"
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainenstruct checkpassword_userdb_module {
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainen struct userdb_module module;
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainen struct db_checkpassword *db;
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen};
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainenstatic void
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainenuserdb_checkpassword_callback(struct auth_request *request,
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainen enum db_checkpassword_status status,
7327394e30c1020b9a2a49c72a7e3d0f7803e680Timo Sirainen const char *const *extra_fields,
7327394e30c1020b9a2a49c72a7e3d0f7803e680Timo Sirainen userdb_callback_t *callback)
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen{
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen unsigned int i;
7327394e30c1020b9a2a49c72a7e3d0f7803e680Timo Sirainen
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen switch (status) {
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen case DB_CHECKPASSWORD_STATUS_INTERNAL_FAILURE:
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen callback(USERDB_RESULT_INTERNAL_FAILURE, request);
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen break;
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen case DB_CHECKPASSWORD_STATUS_FAILURE:
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen callback(USERDB_RESULT_USER_UNKNOWN, request);
7327394e30c1020b9a2a49c72a7e3d0f7803e680Timo Sirainen break;
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen case DB_CHECKPASSWORD_STATUS_OK:
7327394e30c1020b9a2a49c72a7e3d0f7803e680Timo Sirainen for (i = 0; extra_fields[i] != NULL; i++) {
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen if (strncmp(extra_fields[i], "userdb_", 7) != 0)
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen continue;
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen auth_request_set_field_keyvalue(request,
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen extra_fields[i], NULL);
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen }
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen callback(USERDB_RESULT_OK, request);
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen break;
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen }
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen}
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainenstatic void
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainencheckpassword_lookup(struct auth_request *request, userdb_callback_t *callback)
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen{
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen struct userdb_module *_module = request->userdb->userdb;
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainen struct checkpassword_userdb_module *module =
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainen (struct checkpassword_userdb_module *)_module;
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainen db_checkpassword_call(module->db, request, NULL,
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen userdb_checkpassword_callback, callback);
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen}
4fc52b7b25c3d3f42348903e0154840f8761f306Timo Sirainen
20b2d47ed762ca2c009aa37e360af6f223ac71bdTimo Sirainenstatic struct userdb_module *
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainencheckpassword_preinit(pool_t pool, const char *args)
{
struct checkpassword_userdb_module *module;
const char *checkpassword_path = args;
const char *checkpassword_reply_path =
PKG_LIBEXECDIR"/checkpassword-reply";
module = p_new(pool, struct checkpassword_userdb_module, 1);
module->db = db_checkpassword_init(checkpassword_path,
checkpassword_reply_path);
return &module->module;
}
static void checkpassword_deinit(struct userdb_module *_module)
{
struct checkpassword_userdb_module *module =
(struct checkpassword_userdb_module *)_module;
db_checkpassword_deinit(&module->db);
}
struct userdb_module_interface userdb_checkpassword = {
"checkpassword",
checkpassword_preinit,
NULL,
checkpassword_deinit,
checkpassword_lookup,
NULL,
NULL,
NULL
};
#else
struct userdb_module_interface userdb_checkpassword = {
.name = "checkpassword"
};
#endif