~ubuntu-branches/ubuntu/utopic/dovecot/utopic-proposed

« back to all changes in this revision

Viewing changes to src/replication/replicator/dsync-client.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-08 09:35:49 UTC
  • mfrom: (4.1.35 sid)
  • Revision ID: package-import@ubuntu.com-20140108093549-i72o93pux8p0dlaf
Tags: 1:2.2.9-1ubuntu1
* Merge from Debian unstable, remaining changes:
  + Add mail-stack-delivery package:
    - Update d/rules
    - d/control: convert existing dovecot-postfix package to a dummy
      package and add new mail-stack-delivery package.
    - Update maintainer scripts.
    - Rename d/dovecot-postfix.* to debian/mail-stack-delivery.*
    - d/mail-stack-delivery.preinst: Move previously installed backups and
      config files to a new package namespace.
    - d/mail-stack-delivery.prerm: Added to handle downgrades.
  + Use Snakeoil SSL certificates by default:
    - d/control: Depend on ssl-cert.
    - d/dovecot-core.postinst: Relax grep for SSL_* a bit.
  + Add autopkgtest to debian/tests/*.
  + Add ufw integration:
    - d/dovecot-core.ufw.profile: new ufw profile.
    - d/rules: install profile in dovecot-core.
    - d/control: dovecot-core - suggest ufw.
  + d/dovecot-core.dirs: Added usr/share/doc/dovecot-core
  + Add apport hook:
    - d/rules, d/source_dovecot.py
  + Add upstart job:
    - d/rules, d/dovecot-core.dovecot.upstart, d/control,
      d/dovecot-core.dirs, dovecot-imapd.{postrm, postinst, prerm},
      d/dovecot-pop3d.{postinst, postrm, prerm}.
      d/mail-stack-deliver.postinst: Convert init script to upstart.
  + Use the autotools-dev dh addon to update config.guess/config.sub for
    arm64.
* Dropped changes, included in Debian:
  - Update Dovecot name to reflect distribution in login greeting.
  - Update Drac plugin for >= 2.0.0 support.
* d/control: Drop dovecot-postfix package as its no longer required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
 
2
 
 
3
#include "lib.h"
 
4
#include "ioloop.h"
 
5
#include "net.h"
 
6
#include "istream.h"
 
7
#include "ostream.h"
 
8
#include "str.h"
 
9
#include "strescape.h"
 
10
#include "dsync-client.h"
 
11
 
 
12
#include <unistd.h>
 
13
 
 
14
#define DSYNC_FAIL_TIMEOUT_MSECS (1000*5)
 
15
#define DOVEADM_HANDSHAKE "VERSION\tdoveadm-server\t1\t0\n"
 
16
 
 
17
struct dsync_client {
 
18
        char *path;
 
19
        int fd;
 
20
        struct io *io;
 
21
        struct istream *input;
 
22
        struct ostream *output;
 
23
        struct timeout *to;
 
24
 
 
25
        char *dsync_params;
 
26
        char *state;
 
27
        dsync_callback_t *callback;
 
28
        void *context;
 
29
 
 
30
        time_t last_connect_failure;
 
31
        unsigned int handshaked:1;
 
32
        unsigned int cmd_sent:1;
 
33
};
 
34
 
 
35
struct dsync_client *
 
36
dsync_client_init(const char *path, const char *dsync_params)
 
37
{
 
38
        struct dsync_client *client;
 
39
 
 
40
        client = i_new(struct dsync_client, 1);
 
41
        client->path = i_strdup(path);
 
42
        client->fd = -1;
 
43
        client->dsync_params = i_strdup(dsync_params);
 
44
        return client;
 
45
}
 
46
 
 
47
static void dsync_callback(struct dsync_client *client,
 
48
                           const char *state, enum dsync_reply reply)
 
49
{
 
50
        dsync_callback_t *callback = client->callback;
 
51
        void *context = client->context;
 
52
 
 
53
        if (client->to != NULL)
 
54
                timeout_remove(&client->to);
 
55
 
 
56
        client->callback = NULL;
 
57
        client->context = NULL;
 
58
 
 
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;
 
65
}
 
66
 
 
67
static void dsync_close(struct dsync_client *client)
 
68
{
 
69
        client->cmd_sent = FALSE;
 
70
        client->handshaked = FALSE;
 
71
        i_free_and_null(client->state);
 
72
 
 
73
        if (client->fd == -1)
 
74
                return;
 
75
 
 
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");
 
81
        client->fd = -1;
 
82
}
 
83
 
 
84
static void dsync_disconnect(struct dsync_client *client)
 
85
{
 
86
        dsync_close(client);
 
87
        if (client->callback != NULL)
 
88
                dsync_callback(client, "", DSYNC_REPLY_FAIL);
 
89
}
 
90
 
 
91
void dsync_client_deinit(struct dsync_client **_client)
 
92
{
 
93
        struct dsync_client *client = *_client;
 
94
 
 
95
        *_client = NULL;
 
96
 
 
97
        dsync_disconnect(client);
 
98
        i_free(client->dsync_params);
 
99
        i_free(client->path);
 
100
        i_free(client);
 
101
}
 
102
 
 
103
static int dsync_input_line(struct dsync_client *client, const char *line)
 
104
{
 
105
        const char *state;
 
106
 
 
107
        if (!client->handshaked) {
 
108
                if (strcmp(line, "+") != 0) {
 
109
                        i_error("%s: Unexpected handshake: %s",
 
110
                                client->path, line);
 
111
                        return -1;
 
112
                }
 
113
                client->handshaked = TRUE;
 
114
                return 0;
 
115
        }
 
116
        if (client->callback == NULL) {
 
117
                i_error("%s: Unexpected input: %s", client->path, line);
 
118
                return -1;
 
119
        }
 
120
        if (client->state == NULL) {
 
121
                client->state = i_strdup(t_strcut(line, '\t'));
 
122
                return 0;
 
123
        }
 
124
        state = t_strdup(client->state);
 
125
        line = t_strdup(line);
 
126
        dsync_close(client);
 
127
 
 
128
        if (line[0] == '+')
 
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);
 
133
                else
 
134
                        dsync_callback(client, "", DSYNC_REPLY_FAIL);
 
135
        } else {
 
136
                i_error("%s: Invalid input: %s", client->path, line);
 
137
                return -1;
 
138
        }
 
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 */
 
142
        return -1;
 
143
}
 
144
 
 
145
static void dsync_input(struct dsync_client *client)
 
146
{
 
147
        const char *line;
 
148
 
 
149
        while ((line = i_stream_read_next_line(client->input)) != NULL) {
 
150
                if (dsync_input_line(client, line) < 0) {
 
151
                        dsync_disconnect(client);
 
152
                        return;
 
153
                }
 
154
        }
 
155
        if (client->input->eof)
 
156
                dsync_disconnect(client);
 
157
}
 
158
 
 
159
static int dsync_connect(struct dsync_client *client)
 
160
{
 
161
        if (client->fd != -1)
 
162
                return 0;
 
163
 
 
164
        if (client->last_connect_failure == ioloop_time)
 
165
                return -1;
 
166
 
 
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;
 
171
                return -1;
 
172
        }
 
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);
 
179
        return 0;
 
180
}
 
181
 
 
182
static void dsync_fail_timeout(struct dsync_client *client)
 
183
{
 
184
        dsync_disconnect(client);
 
185
}
 
186
 
 
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)
 
190
{
 
191
        string_t *cmd;
 
192
        unsigned int pos;
 
193
        char *p;
 
194
 
 
195
        i_assert(callback != NULL);
 
196
        i_assert(!dsync_client_is_busy(client));
 
197
 
 
198
        client->cmd_sent = TRUE;
 
199
        client->callback = callback;
 
200
        client->context = context;
 
201
 
 
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);
 
206
        } else {
 
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");
 
212
                pos = str_len(cmd);
 
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++) {
 
219
                        if (*p == ' ')
 
220
                                *p = '\t';
 
221
                }
 
222
                if (full)
 
223
                        str_append(cmd, "\t-f");
 
224
                str_append(cmd, "\t-s\t");
 
225
                if (state != NULL)
 
226
                        str_append(cmd, state);
 
227
                str_append_c(cmd, '\n');
 
228
                o_stream_nsend(client->output, str_data(cmd), str_len(cmd));
 
229
        }
 
230
}
 
231
 
 
232
bool dsync_client_is_busy(struct dsync_client *client)
 
233
{
 
234
        return client->cmd_sent;
 
235
}