1
/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
10
#include "dsync-client.h"
14
#define DSYNC_FAIL_TIMEOUT_MSECS (1000*5)
15
#define DOVEADM_HANDSHAKE "VERSION\tdoveadm-server\t1\t0\n"
21
struct istream *input;
22
struct ostream *output;
27
dsync_callback_t *callback;
30
time_t last_connect_failure;
31
unsigned int handshaked:1;
32
unsigned int cmd_sent:1;
36
dsync_client_init(const char *path, const char *dsync_params)
38
struct dsync_client *client;
40
client = i_new(struct dsync_client, 1);
41
client->path = i_strdup(path);
43
client->dsync_params = i_strdup(dsync_params);
47
static void dsync_callback(struct dsync_client *client,
48
const char *state, enum dsync_reply reply)
50
dsync_callback_t *callback = client->callback;
51
void *context = client->context;
53
if (client->to != NULL)
54
timeout_remove(&client->to);
56
client->callback = NULL;
57
client->context = NULL;
59
/* make sure callback doesn't try to reuse this connection, since
60
we can't currently handle it */
61
i_assert(!client->cmd_sent);
62
client->cmd_sent = TRUE;
63
callback(reply, state, context);
64
client->cmd_sent = FALSE;
67
static void dsync_close(struct dsync_client *client)
69
client->cmd_sent = FALSE;
70
client->handshaked = FALSE;
71
i_free_and_null(client->state);
76
io_remove(&client->io);
77
o_stream_destroy(&client->output);
78
i_stream_destroy(&client->input);
79
if (close(client->fd) < 0)
80
i_error("close(dsync) failed: %m");
84
static void dsync_disconnect(struct dsync_client *client)
87
if (client->callback != NULL)
88
dsync_callback(client, "", DSYNC_REPLY_FAIL);
91
void dsync_client_deinit(struct dsync_client **_client)
93
struct dsync_client *client = *_client;
97
dsync_disconnect(client);
98
i_free(client->dsync_params);
103
static int dsync_input_line(struct dsync_client *client, const char *line)
107
if (!client->handshaked) {
108
if (strcmp(line, "+") != 0) {
109
i_error("%s: Unexpected handshake: %s",
113
client->handshaked = TRUE;
116
if (client->callback == NULL) {
117
i_error("%s: Unexpected input: %s", client->path, line);
120
if (client->state == NULL) {
121
client->state = i_strdup(t_strcut(line, '\t'));
124
state = t_strdup(client->state);
125
line = t_strdup(line);
129
dsync_callback(client, state, DSYNC_REPLY_OK);
130
else if (line[0] == '-') {
131
if (strcmp(line+1, "NOUSER") == 0)
132
dsync_callback(client, "", DSYNC_REPLY_NOUSER);
134
dsync_callback(client, "", DSYNC_REPLY_FAIL);
136
i_error("%s: Invalid input: %s", client->path, line);
139
/* FIXME: disconnect after each request for now.
140
doveadm server's getopt() handling seems to break otherwise.
141
also with multiple UIDs doveadm-server fails because setid() fails */
145
static void dsync_input(struct dsync_client *client)
149
while ((line = i_stream_read_next_line(client->input)) != NULL) {
150
if (dsync_input_line(client, line) < 0) {
151
dsync_disconnect(client);
155
if (client->input->eof)
156
dsync_disconnect(client);
159
static int dsync_connect(struct dsync_client *client)
161
if (client->fd != -1)
164
if (client->last_connect_failure == ioloop_time)
167
client->fd = net_connect_unix(client->path);
168
if (client->fd == -1) {
169
i_error("net_connect_unix(%s) failed: %m", client->path);
170
client->last_connect_failure = ioloop_time;
173
client->last_connect_failure = 0;
174
client->io = io_add(client->fd, IO_READ, dsync_input, client);
175
client->input = i_stream_create_fd(client->fd, (size_t)-1, FALSE);
176
client->output = o_stream_create_fd(client->fd, (size_t)-1, FALSE);
177
o_stream_set_no_error_handling(client->output, TRUE);
178
o_stream_nsend_str(client->output, DOVEADM_HANDSHAKE);
182
static void dsync_fail_timeout(struct dsync_client *client)
184
dsync_disconnect(client);
187
void dsync_client_sync(struct dsync_client *client,
188
const char *username, const char *state, bool full,
189
dsync_callback_t *callback, void *context)
195
i_assert(callback != NULL);
196
i_assert(!dsync_client_is_busy(client));
198
client->cmd_sent = TRUE;
199
client->callback = callback;
200
client->context = context;
202
if (dsync_connect(client) < 0) {
203
i_assert(client->to == NULL);
204
client->to = timeout_add(DSYNC_FAIL_TIMEOUT_MSECS,
205
dsync_fail_timeout, client);
207
/* <flags> <username> <command> [<args>] */
208
cmd = t_str_new(256);
209
str_append_c(cmd, '\t');
210
str_append_tabescaped(cmd, username);
211
str_append(cmd, "\tsync\t");
213
/* insert the parameters. we can do it simply by converting
214
spaces into tabs, it's unlikely we'll ever need anything
215
more complex here. */
216
str_append(cmd, client->dsync_params);
217
p = str_c_modifiable(cmd) + pos;
218
for (; *p != '\0'; p++) {
223
str_append(cmd, "\t-f");
224
str_append(cmd, "\t-s\t");
226
str_append(cmd, state);
227
str_append_c(cmd, '\n');
228
o_stream_nsend(client->output, str_data(cmd), str_len(cmd));
232
bool dsync_client_is_busy(struct dsync_client *client)
234
return client->cmd_sent;