bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "lib.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "restrict-access.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "master-service.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "master-service-settings.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "aggregator-settings.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "notify-connection.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen#include "replicator-connection.h"
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenstruct replicator_connection *replicator;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen{
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen master_service_client_connection_accept(conn);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen notify_connection_create(conn->fd, conn->fifo);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen}
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenstatic void main_preinit(void)
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen{
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen struct ip_addr *ips;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen unsigned int ips_count;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen const struct aggregator_settings *set;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen void **sets;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen int ret;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen sets = master_service_settings_get_others(master_service);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen set = sets[0];
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen if (set->replicator_port != 0) {
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen ret = net_gethostbyname(set->replicator_host, &ips, &ips_count);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen if (ret != 0) {
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen i_fatal("replicator_host: gethostbyname(%s) failed: %s",
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen set->replicator_host, net_gethosterror(ret));
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen }
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen replicator = replicator_connection_create_inet(ips, ips_count,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen set->replicator_port,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen notify_connection_sync_callback);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen } else {
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen replicator = replicator_connection_create_unix(set->replicator_host,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen notify_connection_sync_callback);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen }
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen}
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainenint main(int argc, char *argv[])
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen{
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen const struct setting_parser_info *set_roots[] = {
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen &aggregator_setting_parser_info,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen NULL
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen };
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen const char *error;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen master_service = master_service_init("aggregator", 0, &argc, &argv, "");
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen if (master_getopt(master_service) > 0)
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen return FATAL_DEFAULT;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen if (master_service_settings_read_simple(master_service, set_roots,
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen &error) < 0)
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen i_fatal("Error reading configuration: %s", error);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen master_service_init_log(master_service, "aggregator: ");
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen main_preinit();
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
816d20be0cf95fc4eb1a8aa716639e73b8ba525eMartti Rannanjärvi restrict_access_by_env(RESTRICT_ACCESS_FLAG_ALLOW_ROOT, NULL);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen restrict_access_allow_coredumps(TRUE);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen master_service_init_finish(master_service);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen master_service_run(master_service, client_connected);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen notify_connections_destroy_all();
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen replicator_connection_destroy(&replicator);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen master_service_deinit(&master_service);
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen return 0;
447e086422f1ab7cc16833583ed70a4af7a84bc5Timo Sirainen}