mech.c revision a64adf62fa33f2463a86f990217b0c9078531a40
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "common.h"
345648b341f228bd7f0b89f8aa3ecb9c470d817eTimo Sirainen#include "ioloop.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "mech.h"
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen#include "str.h"
5a2cb3d097a2d9a9e930af997e7bf3400a8d840dTimo Sirainen#include "passdb.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include <stdlib.h>
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainenstatic struct mech_module_list *mech_modules;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
16f46efe0e090fe6975acf012a61a160f4787985Andrey Paninvoid mech_register_module(const struct mech_module *module)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct mech_module_list *list;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen list = i_new(struct mech_module_list, 1);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen list->module = *module;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen list->next = mech_modules;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_modules = list;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
16f46efe0e090fe6975acf012a61a160f4787985Andrey Paninvoid mech_unregister_module(const struct mech_module *module)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct mech_module_list **pos, *list;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen for (pos = &mech_modules; *pos != NULL; pos = &(*pos)->next) {
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen if (strcmp((*pos)->module.mech_name, module->mech_name) == 0) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen list = *pos;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen *pos = (*pos)->next;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_free(list);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen break;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
7242e1ce7803b83bc82e239ef111b47c1c72dd4bAndrey Paninconst struct mech_module *mech_module_find(const char *name)
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen{
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen struct mech_module_list *list;
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen for (list = mech_modules; list != NULL; list = list->next) {
c57776c06ec99ba9b0dafdbf9475ea72ea8ca134Timo Sirainen if (strcasecmp(list->module.mech_name, name) == 0)
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen return &list->module;
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen }
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen return NULL;
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen}
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainenvoid mech_generic_auth_initial(struct auth_request *request,
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen const unsigned char *data, size_t data_size)
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen{
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen if (data_size == 0) {
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen request->callback(request, AUTH_CLIENT_RESULT_CONTINUE,
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen NULL, 0);
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen } else {
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen request->mech->auth_continue(request, data, data_size);
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen }
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen}
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainenvoid mech_generic_auth_free(struct auth_request *request)
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen{
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen pool_unref(&request->pool);
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen}
e70d5895795732b8247ab9abb045b438e954bc46Timo Sirainen
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_plain;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_login;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_apop;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_cram_md5;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_digest_md5;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_ntlm;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_otp;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_skey;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_rpa;
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_anonymous;
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen#ifdef HAVE_GSSAPI
1ddec6312bc6882aeb17d4d46d19cbca1723b68bTimo Sirainenextern const struct mech_module mech_gssapi;
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen#endif
704fbadd78375da18dcaf2c5d93ac8cfe2c61358Timo Sirainen#ifdef HAVE_GSSAPI_SPNEGO
704fbadd78375da18dcaf2c5d93ac8cfe2c61358Timo Sirainenextern const struct mech_module mech_gssapi_spnego;
704fbadd78375da18dcaf2c5d93ac8cfe2c61358Timo Sirainen#endif
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainenextern const struct mech_module mech_winbind_ntlm;
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainenextern const struct mech_module mech_winbind_spnego;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenvoid mech_init(const struct auth_settings *set)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen mech_register_module(&mech_plain);
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen mech_register_module(&mech_login);
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen mech_register_module(&mech_apop);
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen mech_register_module(&mech_cram_md5);
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen mech_register_module(&mech_digest_md5);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (set->use_winbind) {
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainen mech_register_module(&mech_winbind_ntlm);
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen mech_register_module(&mech_winbind_spnego);
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen } else {
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainen mech_register_module(&mech_ntlm);
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen#if defined(HAVE_GSSAPI_SPNEGO) && defined(BUILTIN_GSSAPI)
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen mech_register_module(&mech_gssapi_spnego);
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen#endif
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen }
e9e2d23e1ea5a149a7d8828d2a45b9f2313c3785Timo Sirainen mech_register_module(&mech_otp);
e9e2d23e1ea5a149a7d8828d2a45b9f2313c3785Timo Sirainen mech_register_module(&mech_skey);
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen mech_register_module(&mech_rpa);
97c339398f1aba6f315b55a9b6ee6b020e33bea4Timo Sirainen mech_register_module(&mech_anonymous);
f97cf1c086715db87094bc3d0a4fefdd80bd869cTimo Sirainen#ifdef BUILTIN_GSSAPI
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen mech_register_module(&mech_gssapi);
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen#endif
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenvoid mech_deinit(const struct auth_settings *set)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_unregister_module(&mech_plain);
33c6d5807b449463e9b81db5ec99fe027cc1b984Timo Sirainen mech_unregister_module(&mech_login);
8eea67470c1bd8562a62e7445d930bb2079b1a43Timo Sirainen mech_unregister_module(&mech_apop);
94a78eb438622fa53abef1e1726714dacad4b61cTimo Sirainen mech_unregister_module(&mech_cram_md5);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_unregister_module(&mech_digest_md5);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (set->use_winbind) {
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainen mech_unregister_module(&mech_winbind_ntlm);
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen mech_unregister_module(&mech_winbind_spnego);
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen } else {
2c38504860da8a8de915f8e0f5f39d7e7bd00cf8Timo Sirainen mech_unregister_module(&mech_ntlm);
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen#if defined(HAVE_GSSAPI_SPNEGO) && defined(BUILTIN_GSSAPI)
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen mech_unregister_module(&mech_gssapi_spnego);
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen#endif
6e873f135368bcfdd1de4458dded791d0c4d00cdTimo Sirainen }
e9e2d23e1ea5a149a7d8828d2a45b9f2313c3785Timo Sirainen mech_unregister_module(&mech_otp);
e9e2d23e1ea5a149a7d8828d2a45b9f2313c3785Timo Sirainen mech_unregister_module(&mech_skey);
a4ac325c2802693c6b761e5a8fda961e5d7490eaTimo Sirainen mech_unregister_module(&mech_rpa);
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen mech_unregister_module(&mech_anonymous);
f97cf1c086715db87094bc3d0a4fefdd80bd869cTimo Sirainen#ifdef BUILTIN_GSSAPI
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen mech_unregister_module(&mech_gssapi);
0469ed17dafcc56589ce00960a23f4f06817dfb5Timo Sirainen#endif
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}