mech.c revision 94a78eb438622fa53abef1e1726714dacad4b61c
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "common.h"
345648b341f228bd7f0b89f8aa3ecb9c470d817eTimo Sirainen#include "ioloop.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "buffer.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "hash.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "mech.h"
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen#include "auth-client-connection.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include <stdlib.h>
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenstruct mech_module_list {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct mech_module_list *next;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct mech_module module;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen};
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenenum auth_mech auth_mechanisms;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenconst char *const *auth_realms;
20caa6854f0ba83719248a94464a7a24bb7dbd20Timo Sirainenconst char *default_realm;
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainenconst char *anonymous_username;
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainenchar username_chars[256];
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenstatic int set_use_cyrus_sasl;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenstatic struct mech_module_list *mech_modules;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainenstatic struct auth_client_request_reply failure_reply;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenvoid mech_register_module(struct mech_module *module)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct mech_module_list *list;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_assert((auth_mechanisms & module->mech) == 0);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen auth_mechanisms |= module->mech;
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
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenvoid mech_unregister_module(struct mech_module *module)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct mech_module_list **pos, *list;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if ((auth_mechanisms & module->mech) == 0)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen return; /* not registered */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen auth_mechanisms &= ~module->mech;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen for (pos = &mech_modules; *pos != NULL; pos = &(*pos)->next) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if ((*pos)->module.mech == module->mech) {
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
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainenvoid mech_request_new(struct auth_client_connection *conn,
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen struct auth_client_request_new *request,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_callback_t *callback)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct mech_module_list *list;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct auth_request *auth_request;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if ((auth_mechanisms & request->mech) == 0) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen /* unsupported mechanism */
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen i_error("BUG: Auth client %u requested unsupported "
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen "auth mechanism %d", conn->pid, request->mech);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen failure_reply.id = request->id;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen callback(&failure_reply, NULL, conn);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen return;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#ifdef USE_CYRUS_SASL2
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (set_use_cyrus_sasl) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen auth_request = mech_cyrus_sasl_new(conn, request, callback);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen } else
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#endif
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen auth_request = NULL;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen for (list = mech_modules; list != NULL; list = list->next) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (list->module.mech == request->mech) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen auth_request =
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen list->module.auth_new(conn, request->id,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen callback);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen break;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (auth_request != NULL) {
345648b341f228bd7f0b89f8aa3ecb9c470d817eTimo Sirainen auth_request->created = ioloop_time;
f53b8258e5f68ab3d431b1c97520efa0200d134bTimo Sirainen auth_request->conn = conn;
f53b8258e5f68ab3d431b1c97520efa0200d134bTimo Sirainen auth_request->id = request->id;
7d6389e4053c2dac1fb37180b5756b00785983dcTimo Sirainen auth_request->protocol = request->protocol;
f53b8258e5f68ab3d431b1c97520efa0200d134bTimo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen hash_insert(conn->auth_requests, POINTER_CAST(request->id),
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen auth_request);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainenvoid mech_request_continue(struct auth_client_connection *conn,
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen struct auth_client_request_continue *request,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen const unsigned char *data,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_callback_t *callback)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct auth_request *auth_request;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen auth_request = hash_lookup(conn->auth_requests,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen POINTER_CAST(request->id));
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (auth_request == NULL) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen /* timeouted */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen failure_reply.id = request->id;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen callback(&failure_reply, NULL, conn);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen } else {
345648b341f228bd7f0b89f8aa3ecb9c470d817eTimo Sirainen if (!auth_request->auth_continue(auth_request,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen request, data, callback))
bbef8d37812f877525ca57e7ed206094e1efe288Timo Sirainen mech_request_free(auth_request, request->id);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
bbef8d37812f877525ca57e7ed206094e1efe288Timo Sirainenvoid mech_request_free(struct auth_request *auth_request, unsigned int id)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
bbef8d37812f877525ca57e7ed206094e1efe288Timo Sirainen if (auth_request->conn != NULL) {
bbef8d37812f877525ca57e7ed206094e1efe288Timo Sirainen hash_remove(auth_request->conn->auth_requests,
bbef8d37812f877525ca57e7ed206094e1efe288Timo Sirainen POINTER_CAST(id));
bbef8d37812f877525ca57e7ed206094e1efe288Timo Sirainen }
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen auth_request_unref(auth_request);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainenvoid mech_init_auth_client_reply(struct auth_client_request_reply *reply)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen memset(reply, 0, sizeof(*reply));
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen reply->username_idx = (size_t)-1;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen reply->reply_idx = (size_t)-1;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainenvoid *mech_auth_success(struct auth_client_request_reply *reply,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct auth_request *auth_request,
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen const void *data, size_t data_size)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen buffer_t *buf;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
cd466fe7b84b0223735a6469c7f7bc225f65996dTimo Sirainen buf = buffer_create_dynamic(pool_datastack_create(), 256, (size_t)-1);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen reply->username_idx = 0;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen buffer_append(buf, auth_request->user, strlen(auth_request->user)+1);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (data_size == 0)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen reply->reply_idx = (size_t)-1;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen else {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen reply->reply_idx = buffer_get_used_size(buf);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen buffer_append(buf, data, data_size);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen reply->result = AUTH_CLIENT_RESULT_SUCCESS;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen reply->data_size = buffer_get_used_size(buf);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen return buffer_get_modifyable_data(buf, NULL);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
ffa179f76a2b6f4ef97bd560f65fa9e3e35361b4Timo Sirainenvoid mech_auth_finish(struct auth_request *auth_request,
ffa179f76a2b6f4ef97bd560f65fa9e3e35361b4Timo Sirainen const void *data, size_t data_size, int success)
22535a9e685e29214082878e37a267157044618eTimo Sirainen{
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen struct auth_client_request_reply reply;
22535a9e685e29214082878e37a267157044618eTimo Sirainen void *reply_data;
22535a9e685e29214082878e37a267157044618eTimo Sirainen
22535a9e685e29214082878e37a267157044618eTimo Sirainen memset(&reply, 0, sizeof(reply));
22535a9e685e29214082878e37a267157044618eTimo Sirainen reply.id = auth_request->id;
22535a9e685e29214082878e37a267157044618eTimo Sirainen
22535a9e685e29214082878e37a267157044618eTimo Sirainen if (success) {
ffa179f76a2b6f4ef97bd560f65fa9e3e35361b4Timo Sirainen reply_data = mech_auth_success(&reply, auth_request,
ffa179f76a2b6f4ef97bd560f65fa9e3e35361b4Timo Sirainen data, data_size);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen reply.result = AUTH_CLIENT_RESULT_SUCCESS;
22535a9e685e29214082878e37a267157044618eTimo Sirainen } else {
22535a9e685e29214082878e37a267157044618eTimo Sirainen reply_data = NULL;
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen reply.result = AUTH_CLIENT_RESULT_FAILURE;
22535a9e685e29214082878e37a267157044618eTimo Sirainen }
22535a9e685e29214082878e37a267157044618eTimo Sirainen
22535a9e685e29214082878e37a267157044618eTimo Sirainen auth_request->callback(&reply, reply_data, auth_request->conn);
22535a9e685e29214082878e37a267157044618eTimo Sirainen
bbef8d37812f877525ca57e7ed206094e1efe288Timo Sirainen if (!success)
bbef8d37812f877525ca57e7ed206094e1efe288Timo Sirainen mech_request_free(auth_request, auth_request->id);
22535a9e685e29214082878e37a267157044618eTimo Sirainen}
22535a9e685e29214082878e37a267157044618eTimo Sirainen
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainenint mech_is_valid_username(const char *username)
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen{
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen const unsigned char *p;
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen for (p = (const unsigned char *)username; *p != '\0'; p++) {
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen if (username_chars[*p & 0xff] == 0)
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen return FALSE;
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen }
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen return TRUE;
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen}
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainenvoid auth_request_ref(struct auth_request *request)
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen{
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen request->refcount++;
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen}
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainenint auth_request_unref(struct auth_request *request)
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen{
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen if (--request->refcount > 0)
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen return TRUE;
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen request->auth_free(request);
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen return FALSE;
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen}
68d76bc6de2d923d03955e49d563d6e4629b86bfTimo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenextern struct mech_module mech_plain;
94a78eb438622fa53abef1e1726714dacad4b61cTimo Sirainenextern struct mech_module mech_cram_md5;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenextern struct mech_module mech_digest_md5;
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainenextern struct mech_module mech_anonymous;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenvoid mech_init(void)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen const char *const *mechanisms;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen const char *env;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_modules = NULL;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen auth_mechanisms = 0;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen memset(&failure_reply, 0, sizeof(failure_reply));
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen failure_reply.result = AUTH_CLIENT_RESULT_FAILURE;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen anonymous_username = getenv("ANONYMOUS_USERNAME");
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen if (anonymous_username != NULL && *anonymous_username == '\0')
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen anonymous_username = NULL;
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen /* register wanted mechanisms */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen env = getenv("MECHANISMS");
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (env == NULL || *env == '\0')
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_fatal("MECHANISMS environment is unset");
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
50fd2adea4c945b85e5a81d5e55b885b93405c0cTimo Sirainen mechanisms = t_strsplit_spaces(env, " ");
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen while (*mechanisms != NULL) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (strcasecmp(*mechanisms, "PLAIN") == 0)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_register_module(&mech_plain);
94a78eb438622fa53abef1e1726714dacad4b61cTimo Sirainen else if (strcasecmp(*mechanisms, "CRAM-MD5") == 0)
94a78eb438622fa53abef1e1726714dacad4b61cTimo Sirainen mech_register_module(&mech_cram_md5);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen else if (strcasecmp(*mechanisms, "DIGEST-MD5") == 0)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_register_module(&mech_digest_md5);
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen else if (strcasecmp(*mechanisms, "ANONYMOUS") == 0) {
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen if (anonymous_username == NULL) {
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen i_fatal("ANONYMOUS listed in mechanisms, "
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen "but anonymous_username not given");
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen }
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen mech_register_module(&mech_anonymous);
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen } else {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_fatal("Unknown authentication mechanism '%s'",
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen *mechanisms);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mechanisms++;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (auth_mechanisms == 0)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_fatal("No authentication mechanisms configured");
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen /* get our realm - note that we allocate from data stack so
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen this function should never be called inside I/O loop or anywhere
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen else where t_pop() is called */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen env = getenv("REALMS");
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (env == NULL)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen env = "";
50fd2adea4c945b85e5a81d5e55b885b93405c0cTimo Sirainen auth_realms = t_strsplit_spaces(env, " ");
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
20caa6854f0ba83719248a94464a7a24bb7dbd20Timo Sirainen default_realm = getenv("DEFAULT_REALM");
20caa6854f0ba83719248a94464a7a24bb7dbd20Timo Sirainen if (default_realm != NULL && *default_realm == '\0')
20caa6854f0ba83719248a94464a7a24bb7dbd20Timo Sirainen default_realm = NULL;
20caa6854f0ba83719248a94464a7a24bb7dbd20Timo Sirainen
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen env = getenv("USERNAME_CHARS");
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen if (env == NULL || *env == '\0') {
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen /* all chars are allowed */
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen memset(username_chars, 0xff, sizeof(username_chars));
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen } else {
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen memset(username_chars, 0, sizeof(username_chars));
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen for (; *env != '\0'; env++)
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen username_chars[((unsigned char)*env) & 0xff] = 0xff;
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen }
1c38a95332f1945c9806d7d83175a0d948f51291Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen set_use_cyrus_sasl = getenv("USE_CYRUS_SASL") != NULL;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#ifdef USE_CYRUS_SASL2
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (set_use_cyrus_sasl)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_cyrus_sasl_init_lib();
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#endif
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenvoid mech_deinit(void)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_unregister_module(&mech_plain);
94a78eb438622fa53abef1e1726714dacad4b61cTimo Sirainen mech_unregister_module(&mech_cram_md5);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen mech_unregister_module(&mech_digest_md5);
b7c2065b3f10f9ae27787a9db5aaefbfc70d4502Timo Sirainen mech_unregister_module(&mech_anonymous);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}