bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "lib.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "restrict-access.h"
5e327e031d1591f8bff17b67eba7139afbd36cddTimo Sirainen#include "process-title.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "master-service.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "master-service-settings.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "settings-parser.h"
a2fdfd2efdbb2d912aad23900a466cf74114920bTimo Sirainen#include "dict.h"
d29c67900d3d104e24ed213ed4a10d204decdf18Aki Tuomi#include "doveadm.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "client-connection.h"
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi#include "client-connection-private.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "doveadm-settings.h"
c6be98b5270900746f35ebe28bd636019976e29eTimo Sirainen#include "doveadm-dump.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "doveadm-mail.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "doveadm-print-private.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "doveadm-server.h"
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi#include "ostream.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenconst struct doveadm_print_vfuncs *doveadm_print_vfuncs_all[] = {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen &doveadm_print_server_vfuncs,
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi &doveadm_print_json_vfuncs,
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen NULL
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen};
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstruct client_connection *doveadm_client;
5e327e031d1591f8bff17b67eba7139afbd36cddTimo Sirainenbool doveadm_verbose_proctitle;
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainenint doveadm_exit_code = 0;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
f36c4185474823594a78b3f252e79d8923522c36Timo Sirainenstatic pool_t doveadm_settings_pool;
f36c4185474823594a78b3f252e79d8923522c36Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void doveadm_die(void)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen /* do nothing. doveadm connections should be over soon. */
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen if (doveadm_client != NULL) {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen i_error("doveadm server can handle only a single client");
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen return;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen }
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen master_service_client_connection_accept(conn);
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi if (strcmp(conn->name, "http") == 0) {
f300f927771a39549ce6cb7607129508e9041b4aStephan Bosch doveadm_client = client_connection_http_create(conn->fd, conn->ssl);
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi } else {
e60c3e17c656c53da60f0ac51aa15e9ef2742d77Stephan Bosch doveadm_client = client_connection_tcp_create(conn->fd, conn->listen_fd,
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi conn->ssl);
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi }
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
45ead232666a47819e89dc71dec57767340d0b62Timo Sirainenvoid help(const struct doveadm_cmd *cmd)
45ead232666a47819e89dc71dec57767340d0b62Timo Sirainen{
45ead232666a47819e89dc71dec57767340d0b62Timo Sirainen i_fatal("Client sent invalid command. Usage: %s %s",
45ead232666a47819e89dc71dec57767340d0b62Timo Sirainen cmd->name, cmd->short_usage);
45ead232666a47819e89dc71dec57767340d0b62Timo Sirainen}
45ead232666a47819e89dc71dec57767340d0b62Timo Sirainen
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomivoid help_ver2(const struct doveadm_cmd_ver2 *cmd)
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi{
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi i_fatal("Client sent invalid command. Usage: %s %s",
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi cmd->name, cmd->usage);
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi}
1e11a94ec50fc9b57eb2c859771c6a326ccaf86fAki Tuomi
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void main_preinit(void)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
816d20be0cf95fc4eb1a8aa716639e73b8ba525eMartti Rannanjärvi restrict_access_by_env(RESTRICT_ACCESS_FLAG_ALLOW_ROOT, NULL);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen restrict_access_allow_coredumps(TRUE);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void main_init(void)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_server = TRUE;
f36c4185474823594a78b3f252e79d8923522c36Timo Sirainen doveadm_settings_pool = pool_alloconly_create("doveadm settings", 1024);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_settings = master_service_settings_get_others(master_service)[0];
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_settings = settings_dup(&doveadm_setting_parser_info,
f36c4185474823594a78b3f252e79d8923522c36Timo Sirainen doveadm_settings, doveadm_settings_pool);
5e327e031d1591f8bff17b67eba7139afbd36cddTimo Sirainen doveadm_verbose_proctitle =
5e327e031d1591f8bff17b67eba7139afbd36cddTimo Sirainen master_service_settings_get(master_service)->verbose_proctitle;
5e327e031d1591f8bff17b67eba7139afbd36cddTimo Sirainen if (doveadm_verbose_proctitle)
5e327e031d1591f8bff17b67eba7139afbd36cddTimo Sirainen process_title_set("[idling]");
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
45ead232666a47819e89dc71dec57767340d0b62Timo Sirainen doveadm_cmds_init();
a13b1245bee0b6524b4aeb3c8fd9e34af648b746Aki Tuomi doveadm_register_auth_server_commands();
c6be98b5270900746f35ebe28bd636019976e29eTimo Sirainen doveadm_dump_init();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_mail_init();
80a225c0b1f4bf322a562cc7c21d5891fb6895eeStephan Bosch doveadm_server_init();
a2fdfd2efdbb2d912aad23900a466cf74114920bTimo Sirainen dict_drivers_register_builtin();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_load_modules();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void main_deinit(void)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
80a225c0b1f4bf322a562cc7c21d5891fb6895eeStephan Bosch doveadm_server_deinit();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_mail_deinit();
c6be98b5270900746f35ebe28bd636019976e29eTimo Sirainen doveadm_dump_deinit();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_unload_modules();
a2fdfd2efdbb2d912aad23900a466cf74114920bTimo Sirainen dict_drivers_unregister_builtin();
237a6211c7fc4d6dbb58dd0467da6dba1b8f21f6Timo Sirainen doveadm_print_deinit();
45ead232666a47819e89dc71dec57767340d0b62Timo Sirainen doveadm_cmds_deinit();
f36c4185474823594a78b3f252e79d8923522c36Timo Sirainen pool_unref(&doveadm_settings_pool);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenint main(int argc, char *argv[])
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen const struct setting_parser_info *set_roots[] = {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen &doveadm_setting_parser_info,
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen NULL
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen };
2d1892aaeb63b9774237b6e60d6bb04bf6f8259cTimo Sirainen enum master_service_flags service_flags =
838d6a4751c3fbe17c3ec45c0e109629c4156815Timo Sirainen MASTER_SERVICE_FLAG_SEND_STATS |
2d1892aaeb63b9774237b6e60d6bb04bf6f8259cTimo Sirainen MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN;
c1fc5a97a15332f1253ee13a9cab65a7b4b6cd5fTimo Sirainen struct master_service_settings_input input;
c1fc5a97a15332f1253ee13a9cab65a7b4b6cd5fTimo Sirainen struct master_service_settings_output output;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen const char *error;
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen int c;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
2d1892aaeb63b9774237b6e60d6bb04bf6f8259cTimo Sirainen master_service = master_service_init("doveadm", service_flags,
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen &argc, &argv, "D");
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen while ((c = master_getopt(master_service)) > 0) {
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen switch (c) {
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen case 'D':
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen doveadm_debug = TRUE;
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen doveadm_verbose = TRUE;
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen break;
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen default:
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen return FATAL_DEFAULT;
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen }
107659c01b2359b0ee426bde020c8d4e29ede30dTimo Sirainen }
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&input);
c1fc5a97a15332f1253ee13a9cab65a7b4b6cd5fTimo Sirainen input.roots = set_roots;
c1fc5a97a15332f1253ee13a9cab65a7b4b6cd5fTimo Sirainen input.module = "doveadm";
c1fc5a97a15332f1253ee13a9cab65a7b4b6cd5fTimo Sirainen input.service = "doveadm";
c1fc5a97a15332f1253ee13a9cab65a7b4b6cd5fTimo Sirainen
c1fc5a97a15332f1253ee13a9cab65a7b4b6cd5fTimo Sirainen if (master_service_settings_read(master_service, &input, &output,
c1fc5a97a15332f1253ee13a9cab65a7b4b6cd5fTimo Sirainen &error) < 0)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen i_fatal("Error reading configuration: %s", error);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen master_service_init_log(master_service, "doveadm: ");
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen main_preinit();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen master_service_set_die_callback(master_service, doveadm_die);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen main_init();
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen master_service_init_finish(master_service);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen master_service_run(master_service, client_connected);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen main_deinit();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen master_service_deinit(&master_service);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen return 0;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}