main.c revision d11344383ad0a57379ec435bca21e0378af0b672
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "common.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "ioloop.h"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include "network.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "lib-signals.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "restrict-access.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "randgen.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "auth.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "cookie.h"
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen#include "login-connection.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "userinfo.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <stdlib.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <syslog.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvoid master_init(void);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvoid master_deinit(void);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo SirainenIOLoop ioloop;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstatic IO io_listen;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void sig_quit(int signo __attr_unused__)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen io_loop_stop(ioloop);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void auth_accept(void *context __attr_unused__, int listen_fd,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen IO io __attr_unused__)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen int fd;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen fd = net_accept(listen_fd, NULL, NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (fd != -1)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen (void)login_connection_create(fd);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainenstatic void main_init(void)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *logfile;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen lib_init_signals(sig_quit);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen logfile = getenv("IMAP_LOGFILE");
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen if (logfile == NULL) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* open the syslog immediately so chroot() won't
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen break logging */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen openlog("imap-auth", LOG_NDELAY, LOG_MAIL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_set_panic_handler(i_syslog_panic_handler);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_set_fatal_handler(i_syslog_fatal_handler);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_set_error_handler(i_syslog_error_handler);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_set_warning_handler(i_syslog_warning_handler);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen } else {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* log failures into specified log file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_set_failure_file(logfile, "imap-auth");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_set_failure_timestamp_format(getenv("IMAP_LOGSTAMP"));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* open /dev/urandom before chrooting */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen random_init();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen restrict_access_by_env();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen auth_init();
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen cookies_init();
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen login_connections_init();
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen master_init();
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen userinfo_init();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen io_listen = io_add(LOGIN_LISTEN_FD, IO_READ, auth_accept, NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void main_deinit(void)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (lib_signal_kill != 0)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen i_warning("Killed with signal %d", lib_signal_kill);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen io_remove(io_listen);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen userinfo_deinit();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen master_deinit();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen login_connections_deinit();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen cookies_deinit();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen auth_deinit();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen random_deinit();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen closelog();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint main(int argc __attr_unused__, char *argv[] __attr_unused__)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* NOTE: we start rooted, so keep the code minimal until
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen restrict_access_by_env() is called */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen lib_init();
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ioloop = io_loop_create(system_pool);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen main_init();
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen io_loop_run(ioloop);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen main_deinit();
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen io_loop_destroy(ioloop);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen lib_deinit();
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen