main.c revision 2f340bd8f8c4dfadad9c7577a0de8c0c5a7cb213
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmody/* Copyright (C) 2002 Timo Sirainen */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenstatic struct auth_worker_client *worker_client;
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Boschstatic void sig_die(int signo, void *context __attr_unused__)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* warn about being killed because of some signal, except SIGINT (^C)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen which is too common at least while testing :) */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenstatic void open_logfile(void)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen const char *env;
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen i_set_failure_syslog("dovecot-auth", LOG_NDELAY,
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* log to file or stderr */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen i_set_failure_file(getenv("LOGFILE"), "dovecot-auth");
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen i_set_failure_timestamp_format(getenv("LOGSTAMP"));
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenstatic int create_unix_listener(const char *env, int backlog)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen unsigned int mask;
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen mode = getenv(t_strdup_printf("%s_MODE", env));
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen for (i = 0; i < 5; i++) {
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen i_fatal("net_listen_unix(%s) failed: %m", path);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* see if it really exists */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if (net_connect_unix(path) != -1 || errno != ECONNREFUSED)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* delete and try again */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen user = getenv(t_strdup_printf("%s_USER", env));
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen group = getenv(t_strdup_printf("%s_GROUP", env));
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenstatic void add_extra_listeners(void)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen unsigned int i;
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen for (i = 1;; i++) {
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen client_path = getenv(t_strdup_printf("AUTH_%u", i));
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen master_path = getenv(t_strdup_printf("AUTH_%u_MASTER", i));
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if (client_path == NULL && master_path == NULL) {
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenstatic void drop_privileges(void)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* Open /dev/urandom before chrooting */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* Initialize databases so their configuration files can be readable
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen only by root. Also load all modules here. */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* Password lookups etc. may require roots, allow it. */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen lib_signals_set_handler(SIGINT, TRUE, sig_die, NULL);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen lib_signals_set_handler(SIGTERM, TRUE, sig_die, NULL);
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch lib_signals_set_handler(SIGPIPE, FALSE, NULL, NULL);
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch lib_signals_set_handler(SIGALRM, FALSE, NULL, NULL);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* If auth caches aren't used, just ignore these signals */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen lib_signals_set_handler(SIGHUP, FALSE, NULL, NULL);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen lib_signals_set_handler(SIGUSR2, FALSE, NULL, NULL);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen auth_worker_client_create(auth, WORKER_SERVER_FD);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen standalone = getenv("DOVECOT_MASTER") == NULL;
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* starting standalone */
8855b8b57050fe3b6dc3f19283488512fae98648Timo Sirainen i_fatal("dovecot-auth is usually started through "
8855b8b57050fe3b6dc3f19283488512fae98648Timo Sirainen "dovecot master process. If you wish to run "
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen "it standalone, you'll need to set AUTH_* "
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen "environment variables (AUTH_1 isn't set).");
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch (void)auth_master_connection_create(listener, MASTER_SOCKET_FD);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen auth_master_listener_add(listener, CLIENT_LISTEN_FD,
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* everything initialized, notify masters that all is well */
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Boschstatic void main_deinit(void)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Boschint main(int argc __attr_unused__, char *argv[])
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch fd_debug_verify_leaks(WORKER_SERVER_FD + 1, 1024);
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch /* NOTE: we start rooted, so keep the code minimal until
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch restrict_access_by_env() is called */