summaryrefslogtreecommitdiffstats
path: root/src/doveadm/main.c
blob: 9635b8891d4aa876dc99bba27ad83a6329b0f305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "restrict-access.h"
#include "process-title.h"
#include "master-service.h"
#include "settings-parser.h"
#include "dict.h"
#include "doveadm.h"
#include "client-connection.h"
#include "client-connection-private.h"
#include "doveadm-settings.h"
#include "doveadm-dump.h"
#include "doveadm-mail.h"
#include "doveadm-print-private.h"
#include "doveadm-server.h"
#include "ostream.h"

const struct doveadm_print_vfuncs *doveadm_print_vfuncs_all[] = {
	&doveadm_print_server_vfuncs,
	&doveadm_print_json_vfuncs,
	NULL
};

struct client_connection *doveadm_client;
int doveadm_exit_code = 0;

static void doveadm_die(void)
{
	/* do nothing. doveadm connections should be over soon. */
}

static void client_connected(struct master_service_connection *conn)
{
	if (doveadm_client != NULL) {
		i_error("doveadm server can handle only a single client");
		return;
	}

	master_service_client_connection_accept(conn);
	if (strcmp(conn->name, "http") == 0) {
		doveadm_client = client_connection_http_create(conn->fd, conn->ssl);
	} else {
		doveadm_client = client_connection_tcp_create(conn->fd, conn->listen_fd,
							  conn->ssl);
	}
}

void help_ver2(const struct doveadm_cmd_ver2 *cmd)
{
	i_fatal("Client sent invalid command. Usage: %s %s",
		cmd->name, cmd->usage);
}

static void main_preinit(void)
{
	restrict_access_by_env(RESTRICT_ACCESS_FLAG_ALLOW_ROOT, NULL);
	restrict_access_allow_coredumps(TRUE);
}

static void main_init(void)
{
	doveadm_server = TRUE;

	doveadm_settings_init();
	doveadm_cmds_init();
	doveadm_register_auth_server_commands();
	doveadm_dump_init();
	doveadm_mail_init();
	dict_drivers_register_builtin();
	doveadm_load_modules();
	/* read settings only after loading doveadm plugins, which
	   may modify what settings are read */
	doveadm_read_settings();
	/* Load mail_plugins */
	doveadm_mail_init_finish();
	/* kludgy: Load the rest of the doveadm plugins after
	   mail_plugins have been loaded. */
	doveadm_load_modules();

	doveadm_server_init();
	if (doveadm_verbose_proctitle)
		process_title_set("[idling]");
}

static void main_deinit(void)
{
	doveadm_server_deinit();
	doveadm_mail_deinit();
	doveadm_dump_deinit();
	doveadm_unload_modules();
	dict_drivers_unregister_builtin();
	doveadm_print_deinit();
	doveadm_cmds_deinit();
	doveadm_settings_deinit();
}

int main(int argc, char *argv[])
{
	enum master_service_flags service_flags =
		MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN |
		MASTER_SERVICE_FLAG_HAVE_STARTTLS;
	int c;

	master_service = master_service_init("doveadm", service_flags,
					     &argc, &argv, "D");
	while ((c = master_getopt(master_service)) > 0) {
		switch (c) {
		case 'D':
			doveadm_debug = TRUE;
			doveadm_verbose = TRUE;
			break;
		default:
			return FATAL_DEFAULT;
		}
	}

	master_service_init_log(master_service);
	main_preinit();
	master_service_set_die_callback(master_service, doveadm_die);

	main_init();
	master_service_init_finish(master_service);
	master_service_run(master_service, client_connected);

	main_deinit();
	master_service_deinit(&master_service);
        return 0;
}