summaryrefslogtreecommitdiffstats
path: root/src/doveadm/doveadm-proxy.c
blob: 04fde523aa750d58f96dec1bde45ed6aa58b114d (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "ioloop.h"
#include "str.h"
#include "strescape.h"
#include "ipc-client.h"
#include "doveadm.h"
#include "doveadm-print.h"

#include <stdio.h>
#include <unistd.h>

struct proxy_context {
	struct ipc_client *ipc;
	const char *username_field;
	const char *kick_hosts;
};

extern struct doveadm_cmd_ver2 doveadm_cmd_proxy[];

static void proxy_cmd_help(struct doveadm_cmd_context *cctx) ATTR_NORETURN;

static struct proxy_context *
cmd_proxy_init(struct doveadm_cmd_context *cctx)
{
	struct proxy_context *ctx;
	const char *socket_path;

	ctx = t_new(struct proxy_context, 1);
	if (!doveadm_cmd_param_str(cctx, "socket-path", &socket_path)) {
		socket_path = t_strconcat(doveadm_settings->base_dir,
					  "/ipc", NULL);
	}
	(void)doveadm_cmd_param_str(cctx, "passdb-field", &ctx->username_field);
	(void)doveadm_cmd_param_str(cctx, "host", &ctx->kick_hosts);
	ctx->ipc = ipc_client_init(socket_path);
	return ctx;
}

static void cmd_proxy_list_header(const char *const *args)
{
	struct {
		const char *key;
		const char *title;
	} header_map[] = {
		{ "service", "proto" },
		{ "src-ip", "src ip" },
		{ "dest-ip", "dest ip" },
		{ "dest-port", "port" },
	};
	for (unsigned int i = 0; args[i] != NULL; i++) {
		const char *arg = args[i];

		if (strcmp(arg, "username") == 0 ||
		    str_begins(arg, "user_")) {
			doveadm_print_header(arg, arg,
					     DOVEADM_PRINT_HEADER_FLAG_EXPAND);
			continue;
		}
		const char *title = arg;
		for (unsigned int j = 0; j < N_ELEMENTS(header_map); j++) {
			if (strcmp(header_map[j].key, arg) == 0) {
				title = header_map[j].title;
				break;
			}
		}
		doveadm_print_header(arg, title, 0);
	}
}

static void cmd_proxy_list_callback(enum ipc_client_cmd_state state,
				    const char *data, void *context)
{
	bool *seen_header = context;

	switch (state) {
	case IPC_CLIENT_CMD_STATE_REPLY: {
		const char *const *args = t_strsplit_tabescaped(data);

		if (!*seen_header) {
			cmd_proxy_list_header(args);
			*seen_header = TRUE;
		} else {
			for (; *args != NULL; args++)
				doveadm_print(*args);
		}
		return;
	}
	case IPC_CLIENT_CMD_STATE_OK:
		break;
	case IPC_CLIENT_CMD_STATE_ERROR:
		i_error("LIST-FULL failed: %s", data);
		break;
	}
	io_loop_stop(current_ioloop);
}

static void cmd_proxy_list(struct doveadm_cmd_context *cctx)
{
	struct proxy_context *ctx;
	bool seen_header = FALSE;

	ctx = cmd_proxy_init(cctx);

	doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);

	io_loop_set_running(current_ioloop);
	ipc_client_cmd(ctx->ipc, "proxy\t*\tLIST-FULL",
		       cmd_proxy_list_callback, &seen_header);
	if (io_loop_is_running(current_ioloop))
		io_loop_run(current_ioloop);
	ipc_client_deinit(&ctx->ipc);
}

static void cmd_proxy_kick_callback(enum ipc_client_cmd_state state,
				    const char *data, void *context ATTR_UNUSED)
{
	switch (state) {
	case IPC_CLIENT_CMD_STATE_REPLY:
		return;
	case IPC_CLIENT_CMD_STATE_OK:
		if (data[0] == '\0')
			data = "0";
		doveadm_print(data);
		break;
	case IPC_CLIENT_CMD_STATE_ERROR:
		i_error("KICK failed: %s", data);
		doveadm_exit_code = EX_TEMPFAIL;
		break;
	}
	io_loop_stop(current_ioloop);
}

static void cmd_proxy_kick(struct doveadm_cmd_context *cctx)
{
	struct proxy_context *ctx;
	const char *const *users = NULL;
	string_t *cmd;

	ctx = cmd_proxy_init(cctx);
	(void)doveadm_cmd_param_array(cctx, "user", &users);
	if (users == NULL && ctx->kick_hosts == NULL) {
		proxy_cmd_help(cctx);
		return;
	}

	doveadm_print_init(DOVEADM_PRINT_TYPE_FORMATTED);
	doveadm_print_formatted_set_format("%{count} connections kicked\n");
	doveadm_print_header_simple("count");

	cmd = t_str_new(128);
	str_append(cmd, "proxy\t*\t");
        if (ctx->kick_hosts != NULL) {
		str_append(cmd, "KICK-HOST\t");
		str_append(cmd, ctx->kick_hosts);
        }
	else if (ctx->username_field == NULL)
		str_append(cmd, "KICK");
	else {
		str_append(cmd, "KICK-ALT\t");
		str_append_tabescaped(cmd, ctx->username_field);
	}
	if (users != NULL) {
		for (unsigned int i = 0; users[i] != NULL; i++) {
			str_append_c(cmd, '\t');
			str_append_tabescaped(cmd, users[i]);
		}
	}
	ipc_client_cmd(ctx->ipc, str_c(cmd), cmd_proxy_kick_callback, NULL);
	io_loop_run(current_ioloop);
	ipc_client_deinit(&ctx->ipc);
}

struct doveadm_cmd_ver2 doveadm_cmd_proxy[] = {
{
	.name = "proxy list",
	.usage = "[-a <ipc socket path>]",
	.cmd = cmd_proxy_list,
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_PARAM('a', "socket-path", CMD_PARAM_STR, 0)
DOVEADM_CMD_PARAMS_END
},
{
	.name = "proxy kick",
	.usage = "[-a <ipc socket path>] [-f <passdb field>] [-h <host> [...] | <user> [...]]",
	.cmd = cmd_proxy_kick,
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_PARAM('a', "socket-path", CMD_PARAM_STR, 0)
DOVEADM_CMD_PARAM('f', "passdb-field", CMD_PARAM_STR, 0)
DOVEADM_CMD_PARAM('h', "host", CMD_PARAM_STR, 0)
DOVEADM_CMD_PARAM('\0', "user", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
}
};

static void proxy_cmd_help(struct doveadm_cmd_context *cctx)
{
	unsigned int i;

	for (i = 0; i < N_ELEMENTS(doveadm_cmd_proxy); i++) {
		if (doveadm_cmd_proxy[i].cmd == cctx->cmd->cmd)
			help_ver2(&doveadm_cmd_proxy[i]);
	}
	i_unreached();
}

void doveadm_register_proxy_commands(void)
{
	unsigned int i;

	for (i = 0; i < N_ELEMENTS(doveadm_cmd_proxy); i++)
		doveadm_cmd_register_ver2(&doveadm_cmd_proxy[i]);
}