view src/auth/auth.c @ 903:fd8888f6f037 HEAD

Naming style changes, finally got tired of most of the typedefs. Also the previous enum -> macro change reverted so that we don't use the highest bit anymore, that's incompatible with old indexes so they will be rebuilt.
author Timo Sirainen <tss@iki.fi>
date Sun, 05 Jan 2003 15:09:51 +0200
parents 7f41a4b33975
children 218e68ab290d
line wrap: on
line source

/* Copyright (C) 2002 Timo Sirainen */

#include "common.h"
#include "auth.h"
#include "cookie.h"

#include <stdlib.h>

struct auth_module_list {
	struct auth_module_list *next;

	struct auth_module module;
};

enum auth_method auth_methods;
const char *const *auth_realms;

static struct auth_module_list *auth_modules;
static struct auth_reply_data failure_reply;

void auth_register_module(struct auth_module *module)
{
	struct auth_module_list *list;

	i_assert((auth_methods & module->method) == 0);

	auth_methods |= module->method;

	list = i_new(struct auth_module_list, 1);
	memcpy(&list->module, module, sizeof(struct auth_module));

	list->next = auth_modules;
	auth_modules = list;
}

void auth_unregister_module(struct auth_module *module)
{
	struct auth_module_list **pos, *list;

	if ((auth_methods & module->method) == 0)
		return; /* not registered */

        auth_methods &= ~module->method;

	for (pos = &auth_modules; *pos != NULL; pos = &(*pos)->next) {
		if ((*pos)->module.method == module->method) {
			list = *pos;
			*pos = (*pos)->next;
			i_free(list);
			break;
		}
	}
}

void auth_init_request(unsigned int login_pid,
		       struct auth_init_request_data *request,
		       AuthCallback callback, void *context)
{
	struct auth_module_list *list;

	if ((auth_methods & request->method) == 0) {
		/* unsupported method */
		i_error("BUG: imap-login requested unsupported "
			"auth method %d", request->method);
		failure_reply.id = request->id;
		callback(&failure_reply, NULL, context);
		return;
	}

	for (list = auth_modules; list != NULL; list = list->next) {
		if (list->module.method == request->method) {
			list->module.init(login_pid, request,
					  callback, context);
			return;
		}
	}

	i_unreached();
}

void auth_continue_request(unsigned int login_pid,
			   struct auth_continued_request_data *request,
			   const unsigned char *data,
			   AuthCallback callback, void *context)
{
	struct cookie_data *cookie_data;

	cookie_data = cookie_lookup(request->cookie);
	if (cookie_data == NULL) {
		/* timeouted cookie */
		failure_reply.id = request->id;
		callback(&failure_reply, NULL, context);
	} else if (cookie_data->login_pid != login_pid) {
		i_error("BUG: imap-login requested cookie it didn't own");
	} else {
		cookie_data->auth_continue(cookie_data, request,
					   data, callback, context);
	}
}

extern struct auth_module auth_plain;
extern struct auth_module auth_digest_md5;

void auth_init(void)
{
	const char *const *methods;
	const char *env;

        auth_modules = NULL;
	auth_methods = 0;

	memset(&failure_reply, 0, sizeof(failure_reply));
	failure_reply.result = AUTH_RESULT_FAILURE;

	/* register wanted methods */
	env = getenv("METHODS");
	if (env == NULL || *env == '\0')
		i_fatal("METHODS environment is unset");

	methods = t_strsplit(env, " ");
	while (*methods != NULL) {
		if (strcasecmp(*methods, "plain") == 0)
			auth_register_module(&auth_plain);
		else if (strcasecmp(*methods, "digest-md5") == 0)
			auth_register_module(&auth_digest_md5);
		else {
			i_fatal("Unknown authentication method '%s'",
				*methods);
		}
		methods++;
	}

	/* get our realm - note that we allocate from data stack so
	   this function should never be called inside I/O loop or anywhere
	   else where t_pop() is called */
	env = getenv("REALMS");
	if (env == NULL)
		env = "";
	auth_realms = t_strsplit(env, " ");
}

void auth_deinit(void)
{
	auth_unregister_module(&auth_plain);
	auth_unregister_module(&auth_digest_md5);
}