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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "net.h"
#include "istream.h"
#include "ostream.h"
#include "istream-zlib.h"
#include "ostream-zlib.h"
#include "module-dir.h"
#include "master-service.h"
#include "compression.h"
#include "doveadm-dump.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
static bool test_dump_imapzlib(const char *path)
{
const char *p;
char buf[4096];
int fd, ret;
bool match = FALSE;
p = strrchr(path, '.');
if (p == NULL || (strcmp(p, ".in") != 0 && strcmp(p, ".out") != 0))
return FALSE;
fd = open(path, O_RDONLY);
if (fd == -1)
return FALSE;
ret = read(fd, buf, sizeof(buf)-1);
if (ret > 0) {
buf[ret] = '\0';
(void)str_lcase(buf);
match = strstr(buf, " ok begin compression.") != NULL ||
strstr(buf, " compress deflate") != NULL;
}
i_close_fd(&fd);
return match;
}
#ifdef HAVE_ZLIB
static void
cmd_dump_imapzlib(const char *path, const char *const *args ATTR_UNUSED)
{
struct istream *input, *input2;
const unsigned char *data;
size_t size;
const char *line;
int fd;
fd = open(path, O_RDONLY);
if (fd < 0)
i_fatal("open(%s) failed: %m", path);
input = i_stream_create_fd_autoclose(&fd, 1024*32);
while ((line = i_stream_read_next_line(input)) != NULL) {
/* skip tag */
printf("%s\r\n", line);
while (*line != ' ' && *line != '\0') line++;
if (*line == '\0')
continue;
line++;
if (str_begins(line, "OK Begin compression") ||
strcasecmp(line, "COMPRESS DEFLATE") == 0)
break;
}
input2 = i_stream_create_deflate(input);
i_stream_unref(&input);
while (i_stream_read_more(input2, &data, &size) != -1) {
if (fwrite(data, 1, size, stdout) != size)
break;
i_stream_skip(input2, size);
}
if (input2->stream_errno != 0)
i_error("read(%s) failed: %s", path, i_stream_get_error(input));
i_stream_unref(&input2);
fflush(stdout);
}
struct client {
int fd;
struct io *io_client, *io_server;
struct istream *input, *stdin_input;
struct ostream *output;
const struct compression_handler *handler;
char *algorithm;
bool compressed;
bool compress_waiting;
};
static bool
client_input_get_compress_algorithm(struct client *client, const char *line)
{
/* skip tag */
while (*line != ' ' && *line != '\0')
line++;
if (strncasecmp(line, " COMPRESS ", 10) != 0)
return FALSE;
if (compression_lookup_handler(t_str_lcase(line+10),
&client->handler) <= 0)
i_fatal("Unsupported compression mechanism: %s", line+10);
return TRUE;
}
static bool client_input_uncompressed(struct client *client)
{
const char *line;
if (client->compress_waiting) {
/* just read all the pipelined input for now */
(void)i_stream_read(client->stdin_input);
return TRUE;
}
while ((line = i_stream_read_next_line(client->stdin_input)) != NULL) {
o_stream_nsend_str(client->output, line);
o_stream_nsend(client->output, "\n", 1);
if (client_input_get_compress_algorithm(client, line))
return TRUE;
}
return FALSE;
}
static void client_input(struct client *client)
{
const unsigned char *data;
size_t size;
if (!client->compressed &&
client_input_uncompressed(client)) {
/* stop until server has sent reply to COMPRESS command. */
client->compress_waiting = TRUE;
return;
}
if (client->compressed) {
if (i_stream_read_more(client->stdin_input, &data, &size) > 0) {
o_stream_nsend(client->output, data, size);
i_stream_skip(client->stdin_input, size);
}
if (o_stream_flush(client->output) < 0) {
i_fatal("write() failed: %s",
o_stream_get_error(client->output));
}
}
if (client->stdin_input->eof) {
if (client->stdin_input->stream_errno != 0) {
i_fatal("read(stdin) failed: %s",
i_stream_get_error(client->stdin_input));
}
master_service_stop(master_service);
}
}
static bool server_input_is_compress_reply(const char *line)
{
/* skip tag */
while (*line != ' ' && *line != '\0')
line++;
return str_begins(line, " OK Begin compression");
}
static bool server_input_uncompressed(struct client *client)
{
const char *line;
while ((line = i_stream_read_next_line(client->input)) != NULL) {
if (write(STDOUT_FILENO, line, strlen(line)) < 0)
i_fatal("write(stdout) failed: %m");
if (write(STDOUT_FILENO, "\n", 1) < 0)
i_fatal("write(stdout) failed: %m");
if (server_input_is_compress_reply(line))
return TRUE;
}
return FALSE;
}
static void server_input(struct client *client)
{
const unsigned char *data;
size_t size;
if (i_stream_read(client->input) == -1) {
if (client->input->stream_errno != 0) {
i_fatal("read(server) failed: %s",
i_stream_get_error(client->input));
}
i_info("Server disconnected");
master_service_stop(master_service);
return;
}
if (!client->compressed && server_input_uncompressed(client)) {
/* start compression */
struct istream *input;
struct ostream *output;
i_info("<Compression started>");
input = client->handler->create_istream(client->input);
output = client->handler->create_ostream(client->output, 6);
i_stream_unref(&client->input);
o_stream_unref(&client->output);
client->input = input;
client->output = output;
client->compressed = TRUE;
client->compress_waiting = FALSE;
i_stream_set_input_pending(client->stdin_input, TRUE);
}
data = i_stream_get_data(client->input, &size);
if (write(STDOUT_FILENO, data, size) < 0)
i_fatal("write(stdout) failed: %m");
i_stream_skip(client->input, size);
}
static void cmd_zlibconnect(struct doveadm_cmd_context *cctx)
{
struct client client;
const char *host;
struct ip_addr *ips;
unsigned int ips_count;
int64_t port_int64;
in_port_t port = 143;
int fd, ret;
if (!doveadm_cmd_param_str(cctx, "host", &host))
help_ver2(&doveadm_cmd_zlibconnect);
if (doveadm_cmd_param_int64(cctx, "port", &port_int64)) {
if (port_int64 == 0 || port_int64 > 65535)
i_fatal("Invalid port: %"PRId64, port_int64);
port = (in_port_t)port_int64;
}
ret = net_gethostbyname(host, &ips, &ips_count);
if (ret != 0) {
i_fatal("Host %s lookup failed: %s", host,
net_gethosterror(ret));
}
if ((fd = net_connect_ip(&ips[0], port, NULL)) == -1)
i_fatal("connect(%s, %u) failed: %m", host, port);
i_info("Connected to %s port %u.", net_ip2addr(&ips[0]), port);
i_zero(&client);
client.fd = fd;
fd_set_nonblock(STDIN_FILENO, TRUE);
client.stdin_input = i_stream_create_fd(STDIN_FILENO, SIZE_MAX);
client.input = i_stream_create_fd(fd, SIZE_MAX);
client.output = o_stream_create_fd(fd, 0);
o_stream_set_no_error_handling(client.output, TRUE);
client.io_client = io_add_istream(client.stdin_input, client_input, &client);
client.io_server = io_add_istream(client.input, server_input, &client);
master_service_run(master_service, NULL);
io_remove(&client.io_client);
io_remove(&client.io_server);
i_stream_unref(&client.stdin_input);
i_stream_unref(&client.input);
o_stream_unref(&client.output);
if (close(fd) < 0)
i_fatal("close() failed: %m");
}
#else
static void
cmd_dump_imapzlib(const char *path ATTR_UNUSED,
const char *const *args ATTR_UNUSED)
{
i_fatal("Dovecot compiled without zlib support");
}
static void cmd_zlibconnect(struct doveadm_cmd_context *cctx ATTR_UNUSED)
{
i_fatal("Dovecot compiled without zlib support");
}
#endif
struct doveadm_cmd_dump doveadm_cmd_dump_zlib = {
"imapzlib",
test_dump_imapzlib,
cmd_dump_imapzlib
};
struct doveadm_cmd_ver2 doveadm_cmd_zlibconnect = {
.name = "zlibconnect",
.cmd = cmd_zlibconnect,
.usage = "<host> [<port>]",
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_PARAM('\0', "host", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAM('\0', "port", CMD_PARAM_INT64, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
};
|