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"
17
/* normally there shouldn't be any need for locking, since replicator doesn't
18
start dsync in parallel for the same user. we'll do locking just in case
20
#define DSYNC_LOCK_TIMEOUT_SECS 30
26
struct istream *input;
27
struct ostream *output;
31
dsync_callback_t *callback;
34
time_t last_connect_failure;
35
unsigned int handshaked:1;
36
unsigned int cmd_sent:1;
39
struct dsync_client *dsync_client_init(const char *path)
41
struct dsync_client *client;
43
client = i_new(struct dsync_client, 1);
44
client->path = i_strdup(path);
49
static void dsync_callback(struct dsync_client *client,
50
const char *state, enum dsync_reply reply)
52
dsync_callback_t *callback = client->callback;
53
void *context = client->context;
55
if (client->to != NULL)
56
timeout_remove(&client->to);
58
client->callback = NULL;
59
client->context = NULL;
61
/* make sure callback doesn't try to reuse this connection, since
62
we can't currently handle it */
63
i_assert(!client->cmd_sent);
64
client->cmd_sent = TRUE;
65
callback(reply, state, context);
66
client->cmd_sent = FALSE;
69
static void dsync_close(struct dsync_client *client)
74
io_remove(&client->io);
75
o_stream_destroy(&client->output);
76
i_stream_destroy(&client->input);
77
if (close(client->fd) < 0)
78
i_error("close(dsync) failed: %m");
80
i_free_and_null(client->state);
81
client->cmd_sent = FALSE;
82
client->handshaked = FALSE;
85
static void dsync_disconnect(struct dsync_client *client)
88
if (client->callback != NULL)
89
dsync_callback(client, "", DSYNC_REPLY_FAIL);
92
void dsync_client_deinit(struct dsync_client **_client)
94
struct dsync_client *client = *_client;
98
dsync_disconnect(client);
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)
193
i_assert(callback != NULL);
194
i_assert(!dsync_client_is_busy(client));
196
client->cmd_sent = TRUE;
197
client->callback = callback;
198
client->context = context;
200
if (dsync_connect(client) < 0) {
201
i_assert(client->to == NULL);
202
client->to = timeout_add(DSYNC_FAIL_TIMEOUT_MSECS,
203
dsync_fail_timeout, client);
205
/* <flags> <username> <command> [<args>] */
206
cmd = t_str_new(256);
207
str_append_c(cmd, '\t');
208
str_append_tabescaped(cmd, username);
209
str_printfa(cmd, "\tsync\t-d\t-N\t-l\t%u", DSYNC_LOCK_TIMEOUT_SECS);
211
str_append(cmd, "\t-f");
212
str_append(cmd, "\t-U\t-s\t");
214
str_append(cmd, state);
215
str_append_c(cmd, '\n');
216
o_stream_nsend(client->output, str_data(cmd), str_len(cmd));
220
bool dsync_client_is_busy(struct dsync_client *client)
222
return client->cmd_sent;