main.c revision 304eae229e1e3cc02044f94a8f3a057c6345030e
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen/* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "auth-common.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "array.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "ioloop.h"
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen#include "network.h"
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen#include "lib-signals.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "restrict-access.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "child-wait.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "sql-api.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "module-dir.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "randgen.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "process-title.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "settings-parser.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "master-service.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "master-service-settings.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "master-interface.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "password-scheme.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "passdb-cache.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "mech.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "auth.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "auth-penalty.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "auth-request-handler.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "auth-worker-server.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "auth-worker-client.h"
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen#include "auth-master-connection.h"
220e21750948941dc6e33b8f11b552fa21d7f81eTimo Sirainen#include "auth-client-connection.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include <unistd.h>
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#define AUTH_PENALTY_ANVIL_PATH "anvil-auth-penalty"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenenum auth_socket_type {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen AUTH_SOCKET_UNKNOWN = 0,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen AUTH_SOCKET_CLIENT,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen AUTH_SOCKET_LOGIN_CLIENT,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen AUTH_SOCKET_MASTER,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen AUTH_SOCKET_USERDB
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen};
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenbool worker = FALSE, shutdown_request = FALSE;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainentime_t process_start_time;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstruct auth_penalty *auth_penalty;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic pool_t auth_set_pool;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic struct module *modules = NULL;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic struct mechanisms_register *mech_reg;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic ARRAY_DEFINE(listen_fd_types, enum auth_socket_type);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid auth_refresh_proctitle(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (!global_auth_settings->verbose_proctitle)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen process_title_set(t_strdup_printf(
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "[%u wait, %u passdb, %u userdb]",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_request_state_count[AUTH_REQUEST_STATE_NEW] +
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_request_state_count[AUTH_REQUEST_STATE_MECH_CONTINUE] +
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_request_state_count[AUTH_REQUEST_STATE_FINISHED],
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_request_state_count[AUTH_REQUEST_STATE_PASSDB],
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_request_state_count[AUTH_REQUEST_STATE_USERDB]));
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic const char *const *read_global_settings(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct master_service_settings_output set_output;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const char **services;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen unsigned int i, count;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_set_pool = pool_alloconly_create("auth settings", 8192);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen global_auth_settings =
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_settings_read(NULL, auth_set_pool, &set_output);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* strdup() the service names, because they're allocated from
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen set parser pool, and we'll later clear it. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen count = str_array_length(set_output.specific_services);
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen services = p_new(auth_set_pool, const char *, count + 1);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (i = 0; i < count; i++) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen services[i] = p_strdup(auth_set_pool,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen set_output.specific_services[i]);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return services;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void main_preinit(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct module_dir_load_settings mod_set;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const char *const *services;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* Open /dev/urandom before chrooting */
740dfc753f1f087f0c41082e4c551053129e875dTimo Sirainen random_init();
740dfc753f1f087f0c41082e4c551053129e875dTimo Sirainen
740dfc753f1f087f0c41082e4c551053129e875dTimo Sirainen /* Load built-in SQL drivers (if any) */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sql_drivers_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sql_drivers_register_all();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* Initialize databases so their configuration files can be readable
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen only by root. Also load all modules here. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen passdbs_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen userdbs_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen services = read_global_settings();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen memset(&mod_set, 0, sizeof(mod_set));
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen mod_set.version = master_service_get_version_string(master_service);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen mod_set.require_init_funcs = TRUE;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen mod_set.debug = global_auth_settings->debug;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen modules = module_dir_load(AUTH_MODULE_DIR, NULL, &mod_set);
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen module_dir_init(modules);
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen auth_penalty = auth_penalty_init(AUTH_PENALTY_ANVIL_PATH);
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen mech_init(global_auth_settings);
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen mech_reg = mech_register_init(global_auth_settings);
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen auths_preinit(global_auth_settings, auth_set_pool,
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen mech_reg, services);
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen /* Password lookups etc. may require roots, allow it. */
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen restrict_access_by_env(NULL, FALSE);
7e459d4ba3add84f31a72992efc33bd53f2c655dTimo Sirainen restrict_access_allow_coredumps(TRUE);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void main_init(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_array_init(&listen_fd_types, 8);
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen process_start_time = ioloop_time;
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* If auth caches aren't used, just ignore these signals */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen lib_signals_ignore(SIGHUP, TRUE);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen lib_signals_ignore(SIGUSR2, TRUE);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen child_wait_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen password_schemes_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_worker_server_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auths_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_request_handler_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_master_connections_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_client_connections_init();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (worker) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* workers have only a single connection from the master
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth process */
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen master_service_set_client_limit(master_service, 1);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen } else {
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen /* caching is handled only by the main auth process */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen passdb_cache_init(global_auth_settings);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_refresh_proctitle();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void main_deinit(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (auth_worker_client != NULL)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_worker_client_destroy(&auth_worker_client);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen else
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_request_handler_flush_failures(TRUE);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_client_connections_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_master_connections_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_worker_server_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth_request_handler_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* there may still be some async auth requests left, but above
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen functions should have marked all of them as destroyed. pass/userdb
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen deinits should abort the pending requests, which still triggers
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen callbacks, which should avoid crashes by checking the destroyed
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen state. */
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen auths_deinit();
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen passdb_cache_deinit();
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen mech_register_deinit(&mech_reg);
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen mech_deinit(global_auth_settings);
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen auth_penalty_deinit(&auth_penalty);
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen /* allow modules to unregister their dbs/drivers/etc. before freeing
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen the whole data structures containing them. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen module_dir_unload(&modules);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen userdbs_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen passdbs_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen password_schemes_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sql_drivers_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen random_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen array_free(&listen_fd_types);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen pool_unref(&auth_set_pool);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void worker_connected(struct master_service_connection *conn)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (auth_worker_client != NULL) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_error("Auth workers can handle only a single client");
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen master_service_client_connection_accept(conn);
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen (void)auth_worker_client_create(auth_find_service(NULL), conn->fd);
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen enum auth_socket_type *type;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const char *path, *name, *suffix;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct auth *auth;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen type = array_idx_modifiable(&listen_fd_types, conn->listen_fd);
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen if (*type == AUTH_SOCKET_UNKNOWN) {
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen /* figure out if this is a server or network socket by
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen checking the socket path name. */
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen if (net_getunixname(conn->listen_fd, &path) < 0)
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen i_fatal("getsockname(%d) failed: %m", conn->listen_fd);
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen name = strrchr(path, '/');
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen if (name == NULL)
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen name = path;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen else
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen name++;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen suffix = strrchr(name, '-');
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen if (suffix == NULL)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen suffix = name;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen else
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen suffix++;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen if (strcmp(suffix, "login") == 0)
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen *type = AUTH_SOCKET_LOGIN_CLIENT;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen else if (strcmp(suffix, "master") == 0)
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen *type = AUTH_SOCKET_MASTER;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen else if (strcmp(suffix, "userdb") == 0)
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen *type = AUTH_SOCKET_USERDB;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen else
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *type = AUTH_SOCKET_CLIENT;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen auth = auth_find_service(NULL);
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen switch (*type) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen case AUTH_SOCKET_MASTER:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen (void)auth_master_connection_create(auth, conn->fd, FALSE);
f97cacf16251b42f530c6a28686cc8c9aa7df3a2Timo Sirainen break;
f97cacf16251b42f530c6a28686cc8c9aa7df3a2Timo Sirainen case AUTH_SOCKET_USERDB:
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen (void)auth_master_connection_create(auth, conn->fd, TRUE);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen break;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen case AUTH_SOCKET_LOGIN_CLIENT:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen (void)auth_client_connection_create(auth, conn->fd, TRUE);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen break;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen case AUTH_SOCKET_CLIENT:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen (void)auth_client_connection_create(auth, conn->fd, FALSE);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen break;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen default:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_unreached();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen master_service_client_connection_accept(conn);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void auth_die(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* do nothing. auth clients should disconnect soon. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenint main(int argc, char *argv[])
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen int c;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen master_service = master_service_init("auth", 0, &argc, &argv, "w");
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen master_service_init_log(master_service, "auth: ");
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen while ((c = master_getopt(master_service)) > 0) {
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen switch (c) {
25d9db0c00e8041165540e0829d7eab7548cbaa5Timo Sirainen case 'w':
25d9db0c00e8041165540e0829d7eab7548cbaa5Timo Sirainen worker = TRUE;
25d9db0c00e8041165540e0829d7eab7548cbaa5Timo Sirainen break;
25d9db0c00e8041165540e0829d7eab7548cbaa5Timo Sirainen default:
7d7917f3b54749f667d8c98e881a965ae84f033eTimo Sirainen return FATAL_DEFAULT;
7d7917f3b54749f667d8c98e881a965ae84f033eTimo Sirainen }
7d7917f3b54749f667d8c98e881a965ae84f033eTimo Sirainen }
7d7917f3b54749f667d8c98e881a965ae84f033eTimo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen main_preinit();
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen master_service_init_finish(master_service);
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen master_service_set_die_callback(master_service, auth_die);
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen main_init();
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen master_service_run(master_service, worker ? worker_connected :
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen client_connected);
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen main_deinit();
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen master_service_deinit(&master_service);
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen return 0;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen}
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen