main.c revision 0fe87c6c4972ab53eb4827814950d039fbcd5935
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void sig_quit(int signo __attr_unused__)
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenstatic void open_logfile(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_set_failure_syslog("dovecot-auth", LOG_NDELAY, LOG_MAIL);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* log to file or stderr */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_set_failure_file(getenv("LOGFILE"), "dovecot-auth");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_set_failure_timestamp_format(getenv("LOGSTAMP"));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int create_unix_listener(const char *env)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int mask;
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen mode = getenv(t_strdup_printf("%s_MODE", env));
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen for (i = 0; i < 5; i++) {
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen i_fatal("net_listen_unix(%s) failed: %m", path);
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen /* see if it really exists */
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen if (net_connect_unix(path) != -1 || errno != ECONNREFUSED)
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen /* delete and try again */
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen user = getenv(t_strdup_printf("%s_USER", env));
6ae329de09afb7214c906d762320847e05469d53Timo Sirainen group = getenv(t_strdup_printf("%s_GROUP", env));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void add_extra_listeners(void)
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen unsigned int i;
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen for (i = 1;; i++) {
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen client_path = getenv(t_strdup_printf("AUTH_%u", i));
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen master_path = getenv(t_strdup_printf("AUTH_%u_MASTER", i));
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen if (client_path == NULL && master_path == NULL) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen master = auth_master_connection_create(-1, getpid());
817d027593510c3ba70ad542ce0011f5f6916d1eTimo Sirainen auth_master_connection_add_listener(master, master_fd,
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen auth_master_connection_add_listener(master, client_fd,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen buffer_append(masters_buf, &master, sizeof(master));
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainenstatic void drop_privileges(void)
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen verbose_debug = getenv("VERBOSE_DEBUG") != NULL;
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* Open /dev/urandom before chrooting */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* Initialize databases so their configuration files can be readable
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen only by root. Also load all modules here. */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen masters_buf = buffer_create_dynamic(default_pool, 64);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* Password lookups etc. may require roots, allow it. */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen struct auth_master_connection *master, **master_p;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const char *env;
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen unsigned int pid;
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* starting standalone */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen i_fatal("dovecot-auth is usually started through "
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen "dovecot master process. If you wish to run "
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen "it standalone, you'll need to set AUTH_* "
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen "environment variables (AUTH_1 isn't set).");
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen master = auth_master_connection_create(MASTER_SOCKET_FD, pid);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen auth_master_connection_add_listener(master, LOGIN_LISTEN_FD,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen buffer_append(masters_buf, &master, sizeof(master));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* everything initialized, notify masters that all is well */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen master_p = buffer_get_modifyable_data(masters_buf, &size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < size; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen auth_master_connection_send_handshake(master_p[i]);
3f91e60401495a4046c73992fabaa5e77200a451Timo Sirainenstatic void main_deinit(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen i_warning("Killed with signal %d", lib_signal_kill);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen master = buffer_get_modifyable_data(masters_buf, &size);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen for (i = 0; i < size; i++)
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen /* NOTE: we start rooted, so keep the code minimal until
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen restrict_access_by_env() is called */
37ee89f3cba22cd975912a882f0d3097fa5031e1Timo Sirainen main_init(argc > 1 && strcmp(argv[1], "-F") == 0);