3793
|
1 /* Copyright (C) 2005 Timo Sirainen */
|
|
2
|
|
3 #include "lib.h"
|
|
4 #include "str.h"
|
|
5 #include "network.h"
|
|
6 #include "istream.h"
|
|
7 #include "ostream.h"
|
|
8 #include "dict-private.h"
|
|
9 #include "dict-client.h"
|
|
10
|
|
11 #include <unistd.h>
|
|
12 #include <fcntl.h>
|
|
13
|
|
14 struct client_dict {
|
|
15 struct dict dict;
|
|
16
|
|
17 pool_t pool;
|
|
18 int fd;
|
|
19 const char *uri;
|
|
20 const char *username;
|
|
21 const char *path;
|
|
22
|
|
23 struct istream *input;
|
|
24 struct ostream *output;
|
|
25
|
|
26 unsigned int connect_counter;
|
|
27 unsigned int transaction_id_counter;
|
|
28
|
|
29 unsigned int in_iteration:1;
|
|
30 unsigned int handshaked:1;
|
|
31 };
|
|
32
|
|
33 struct client_dict_iterate_context {
|
|
34 struct dict_iterate_context ctx;
|
|
35
|
|
36 pool_t pool;
|
|
37 int failed;
|
|
38 };
|
|
39
|
|
40 struct client_dict_transaction_context {
|
|
41 struct dict_transaction_context ctx;
|
|
42
|
|
43 unsigned int id;
|
|
44 unsigned int connect_counter;
|
|
45
|
|
46 int failed;
|
|
47 };
|
|
48
|
|
49 static int client_dict_connect(struct client_dict *dict);
|
|
50 static void client_dict_disconnect(struct client_dict *dict);
|
|
51
|
|
52 const char *dict_client_escape(const char *src)
|
|
53 {
|
|
54 const char *p;
|
|
55 string_t *dest;
|
|
56
|
|
57 /* first do a quick lookup to see if there's anything to escape.
|
|
58 probably not. */
|
|
59 for (p = src; *p != '\0'; p++) {
|
|
60 if (*p == '\t' || *p == '\n' || *p == '\001')
|
|
61 break;
|
|
62 }
|
|
63
|
|
64 if (*p == '\0')
|
|
65 return src;
|
|
66
|
|
67 dest = t_str_new(256);
|
|
68 str_append_n(dest, src, p - src);
|
|
69
|
|
70 for (; *p != '\0'; *p++) {
|
|
71 switch (*p) {
|
|
72 case '\t':
|
|
73 str_append_c(dest, '\001');
|
|
74 str_append_c(dest, 't');
|
|
75 break;
|
|
76 case '\n':
|
|
77 str_append_c(dest, '\001');
|
|
78 str_append_c(dest, 'n');
|
|
79 break;
|
|
80 case '\001':
|
|
81 str_append_c(dest, '\001');
|
|
82 str_append_c(dest, '1');
|
|
83 break;
|
|
84 default:
|
|
85 str_append_c(dest, *p);
|
|
86 break;
|
|
87 }
|
|
88 }
|
|
89 return str_c(dest);
|
|
90 }
|
|
91
|
|
92 const char *dict_client_unescape(const char *src)
|
|
93 {
|
|
94 const char *p;
|
|
95 string_t *dest;
|
|
96
|
|
97 /* first do a quick lookup to see if there's anything to unescape.
|
|
98 probably not. */
|
|
99 for (p = src; *p != '\0'; p++) {
|
|
100 if (*p == '\001')
|
|
101 break;
|
|
102 }
|
|
103
|
|
104 if (*p == '\0')
|
|
105 return src;
|
|
106
|
|
107 dest = t_str_new(256);
|
|
108 str_append_n(dest, src, p - src);
|
|
109 for (; *p != '\0'; p++) {
|
|
110 if (*p != '\001')
|
|
111 str_append_c(dest, *p);
|
|
112 else if (p[1] != '\0') {
|
|
113 p++;
|
|
114 switch (*p) {
|
|
115 case '1':
|
|
116 str_append_c(dest, '\001');
|
|
117 break;
|
|
118 case 't':
|
|
119 str_append_c(dest, '\t');
|
|
120 break;
|
|
121 case 'n':
|
|
122 str_append_c(dest, '\n');
|
|
123 break;
|
|
124 }
|
|
125 }
|
|
126 }
|
|
127 return str_c(dest);
|
|
128 }
|
|
129
|
|
130 static int client_dict_send_query(struct client_dict *dict, const char *query)
|
|
131 {
|
|
132 if (o_stream_send_str(dict->output, query) < 0 ||
|
|
133 o_stream_flush(dict->output) < 0) {
|
|
134 /* Send failed */
|
|
135 if (!dict->handshaked) {
|
|
136 /* we're trying to send hello, don't try to reconnect */
|
|
137 return -1;
|
|
138 }
|
|
139
|
|
140 /* Reconnect and try again. */
|
|
141 client_dict_disconnect(dict);
|
|
142 if (client_dict_connect(dict) < 0)
|
|
143 return -1;
|
|
144
|
|
145 if (o_stream_send_str(dict->output, query) < 0 ||
|
|
146 o_stream_flush(dict->output) < 0) {
|
|
147 i_error("write(%s) failed: %m", dict->path);
|
|
148 return -1;
|
|
149 }
|
|
150 }
|
|
151 return 0;
|
|
152 }
|
|
153
|
|
154 static char *client_dict_read_line(struct client_dict *dict)
|
|
155 {
|
|
156 char *line;
|
|
157 int ret;
|
|
158
|
|
159 line = NULL;
|
|
160 while ((ret = i_stream_read(dict->input)) > 0) {
|
|
161 line = i_stream_next_line(dict->input);
|
|
162 if (line != NULL)
|
|
163 return line;
|
|
164 }
|
|
165
|
|
166 i_error("read(%s) failed: %m", dict->path);
|
|
167 return NULL;
|
|
168 }
|
|
169
|
|
170 static int client_dict_connect(struct client_dict *dict)
|
|
171 {
|
|
172 const char *query;
|
|
173
|
|
174 i_assert(dict->fd == -1);
|
|
175
|
|
176 dict->fd = net_connect_unix(dict->path);
|
|
177 if (dict->fd == -1) {
|
|
178 i_error("net_connect_unix(%s) failed: %m", dict->path);
|
|
179 return -1;
|
|
180 }
|
|
181
|
|
182 /* Dictionary lookups are blocking */
|
|
183 net_set_nonblock(dict->fd, FALSE);
|
|
184
|
|
185 dict->input = i_stream_create_file(dict->fd, default_pool,
|
|
186 (size_t)-1, FALSE);
|
|
187 dict->output = o_stream_create_file(dict->fd, default_pool,
|
|
188 4096, FALSE);
|
|
189 dict->transaction_id_counter = 0;
|
|
190
|
|
191 t_push();
|
3852
|
192 query = t_strdup_printf("%c%u\t%u\t%s\t%s\n", DICT_PROTOCOL_CMD_HELLO,
|
3793
|
193 DICT_CLIENT_PROTOCOL_MAJOR_VERSION,
|
|
194 DICT_CLIENT_PROTOCOL_MINOR_VERSION,
|
|
195 dict->username, dict->uri);
|
|
196 if (client_dict_send_query(dict, query) < 0) {
|
|
197 client_dict_disconnect(dict);
|
|
198 return -1;
|
|
199 }
|
|
200 t_pop();
|
|
201
|
|
202 dict->handshaked = TRUE;
|
|
203 return 0;
|
|
204 }
|
|
205
|
|
206 static void client_dict_disconnect(struct client_dict *dict)
|
|
207 {
|
|
208 dict->connect_counter++;
|
|
209 dict->handshaked = FALSE;
|
|
210
|
|
211 if (dict->input != NULL) {
|
|
212 i_stream_unref(dict->input);
|
|
213 dict->input = NULL;
|
|
214 }
|
|
215 if (dict->output != NULL) {
|
|
216 o_stream_unref(dict->output);
|
|
217 dict->output = NULL;
|
|
218 }
|
|
219
|
|
220 if (dict->fd != -1) {
|
|
221 if (close(dict->fd) < 0)
|
|
222 i_error("close(%s) failed: %m", dict->path);
|
|
223 dict->fd = -1;
|
|
224 }
|
|
225 }
|
|
226
|
|
227 static struct dict *client_dict_init(struct dict *dict_class, const char *uri)
|
|
228 {
|
|
229 struct client_dict *dict;
|
|
230 const char *path, *dest_uri;
|
|
231 pool_t pool;
|
|
232
|
|
233 /* uri = <username> [" " <path>] ":" <uri> */
|
|
234 dest_uri = strchr(uri, ':');
|
|
235 if (dest_uri == NULL) {
|
|
236 i_error("dict-client: Invalid URI: %s", uri);
|
|
237 return NULL;
|
|
238 }
|
|
239
|
|
240 pool = pool_alloconly_create("client dict", 1024);
|
|
241 dict = p_new(pool, struct client_dict, 1);
|
|
242 dict->pool = pool;
|
|
243 dict->dict = *dict_class;
|
|
244
|
|
245 dict->fd = -1;
|
|
246
|
|
247 path = strchr(uri, ' ');
|
|
248 if (path != NULL && path < dest_uri) {
|
|
249 /* path given */
|
|
250 dict->path = p_strdup_until(pool, path + 1, dest_uri);
|
|
251 dict->username = p_strdup_until(pool, uri, path);
|
|
252 } else {
|
|
253 dict->path = DEFAULT_DICT_SERVER_SOCKET_PATH;
|
|
254 dict->username = p_strdup_until(pool, uri, dest_uri);
|
|
255 }
|
|
256 dict->uri = p_strdup(pool, dest_uri + 1);
|
|
257
|
|
258 (void)client_dict_connect(dict);
|
|
259 return &dict->dict;
|
|
260 }
|
|
261
|
|
262 static void client_dict_deinit(struct dict *_dict)
|
|
263 {
|
|
264 struct client_dict *dict = (struct client_dict *)_dict;
|
|
265
|
|
266 client_dict_disconnect(dict);
|
|
267 pool_unref(dict->pool);
|
|
268 }
|
|
269
|
|
270 static int client_dict_lookup(struct dict *_dict, pool_t pool,
|
|
271 const char *key, const char **value_r)
|
|
272 {
|
|
273 struct client_dict *dict = (struct client_dict *)_dict;
|
|
274 const char *line;
|
|
275
|
|
276 if (dict->fd == -1)
|
|
277 return -1;
|
|
278
|
|
279 t_push();
|
|
280 line = t_strdup_printf("%c%s\n", DICT_PROTOCOL_CMD_LOOKUP,
|
|
281 dict_client_escape(key));
|
|
282 if (client_dict_send_query(dict, line) < 0) {
|
|
283 t_pop();
|
|
284 return -1;
|
|
285 }
|
|
286 t_pop();
|
|
287
|
|
288 /* read reply */
|
|
289 line = client_dict_read_line(dict);
|
|
290 if (line == NULL)
|
|
291 return -1;
|
|
292
|
|
293 if (*line == DICT_PROTOCOL_REPLY_OK) {
|
|
294 *value_r = p_strdup(pool, dict_client_unescape(line + 1));
|
|
295 return 1;
|
|
296 } else {
|
|
297 *value_r = NULL;
|
|
298 return *line == DICT_PROTOCOL_REPLY_NOTFOUND ? 0 : -1;
|
|
299 }
|
|
300 }
|
|
301
|
|
302 static struct dict_iterate_context *
|
|
303 client_dict_iterate_init(struct dict *_dict, const char *path, int recurse)
|
|
304 {
|
|
305 struct client_dict *dict = (struct client_dict *)_dict;
|
|
306 struct client_dict_iterate_context *ctx;
|
|
307 const char *query;
|
|
308
|
|
309 if (dict->in_iteration)
|
|
310 i_panic("dict-client: Only one iteration supported");
|
|
311 dict->in_iteration = TRUE;
|
|
312
|
|
313 ctx = i_new(struct client_dict_iterate_context, 1);
|
|
314 ctx->ctx.dict = _dict;
|
|
315 ctx->pool = pool_alloconly_create("client dict iteration", 512);
|
|
316
|
|
317 t_push();
|
|
318 query = t_strdup_printf("%c%d\t%s\n", DICT_PROTOCOL_CMD_ITERATE,
|
|
319 recurse, dict_client_escape(path));
|
|
320 if (client_dict_send_query(dict, query) < 0)
|
|
321 ctx->failed = TRUE;
|
|
322 t_pop();
|
|
323
|
|
324 return &ctx->ctx;
|
|
325 }
|
|
326
|
|
327 static int client_dict_iterate(struct dict_iterate_context *_ctx,
|
|
328 const char **key_r, const char **value_r)
|
|
329 {
|
|
330 struct client_dict_iterate_context *ctx =
|
|
331 (struct client_dict_iterate_context *)_ctx;
|
|
332 struct client_dict *dict = (struct client_dict *)_ctx->dict;
|
|
333 char *line, *value;
|
|
334
|
|
335 if (ctx->failed)
|
|
336 return -1;
|
|
337
|
|
338 /* read next reply */
|
|
339 line = client_dict_read_line(dict);
|
|
340 if (line == NULL)
|
|
341 return -1;
|
|
342
|
|
343 if (*line == '\0') {
|
|
344 /* end of iteration */
|
|
345 return 0;
|
|
346 }
|
|
347
|
|
348 /* line contains key \t value */
|
|
349 p_clear(ctx->pool);
|
|
350
|
|
351 value = strchr(line, '\t');
|
|
352 if (value == NULL) {
|
|
353 /* broken protocol */
|
|
354 i_error("dict client (%s) sent broken reply", dict->path);
|
|
355 return -1;
|
|
356 }
|
|
357 *value++ = '\0';
|
|
358
|
|
359 *key_r = p_strdup(ctx->pool, dict_client_unescape(line));
|
|
360 *value_r = p_strdup(ctx->pool, dict_client_unescape(value));
|
|
361 return 1;
|
|
362 }
|
|
363
|
|
364 static void client_dict_iterate_deinit(struct dict_iterate_context *_ctx)
|
|
365 {
|
|
366 struct client_dict *dict = (struct client_dict *)_ctx->dict;
|
|
367 struct client_dict_iterate_context *ctx =
|
|
368 (struct client_dict_iterate_context *)_ctx;
|
|
369
|
|
370 pool_unref(ctx->pool);
|
|
371 i_free(ctx);
|
|
372 dict->in_iteration = TRUE;
|
|
373 }
|
|
374
|
|
375 static struct dict_transaction_context *
|
|
376 client_dict_transaction_init(struct dict *_dict)
|
|
377 {
|
|
378 struct client_dict *dict = (struct client_dict *)_dict;
|
|
379 struct client_dict_transaction_context *ctx;
|
|
380
|
|
381 ctx = i_new(struct client_dict_transaction_context, 1);
|
|
382 ctx->ctx.dict = _dict;
|
|
383 ctx->id = ++dict->transaction_id_counter;
|
|
384 ctx->connect_counter = dict->connect_counter;
|
|
385 return &ctx->ctx;
|
|
386 }
|
|
387
|
|
388 static int client_dict_transaction_commit(struct dict_transaction_context *_ctx)
|
|
389 {
|
|
390 struct client_dict_transaction_context *ctx =
|
|
391 (struct client_dict_transaction_context *)_ctx;
|
|
392 struct client_dict *dict = (struct client_dict *)_ctx->dict;
|
|
393 const char *query, *line;
|
|
394 int ret = ctx->failed ? -1 : 0;
|
|
395
|
|
396 if (ctx->connect_counter != dict->connect_counter)
|
|
397 ret = -1;
|
|
398 else {
|
|
399 t_push();
|
|
400 query = t_strdup_printf("%c%u", !ctx->failed ?
|
|
401 DICT_PROTOCOL_CMD_COMMIT :
|
|
402 DICT_PROTOCOL_CMD_ROLLBACK,
|
|
403 ctx->id);
|
|
404 if (client_dict_send_query(dict, query) < 0)
|
|
405 ret = -1;
|
|
406 else if (ret == 0) {
|
|
407 /* read reply */
|
|
408 line = client_dict_read_line(dict);
|
|
409 if (line == NULL)
|
|
410 return -1;
|
|
411
|
|
412 if (*line != DICT_PROTOCOL_REPLY_OK)
|
|
413 ret = -1;
|
|
414 }
|
|
415
|
|
416 t_pop();
|
|
417 }
|
|
418 i_free(ctx);
|
|
419
|
|
420 return ret;
|
|
421 }
|
|
422
|
|
423 static void
|
|
424 client_dict_transaction_rollback(struct dict_transaction_context *_ctx)
|
|
425 {
|
|
426 struct client_dict_transaction_context *ctx =
|
|
427 (struct client_dict_transaction_context *)_ctx;
|
|
428 struct client_dict *dict = (struct client_dict *)_ctx->dict;
|
|
429 const char *query;
|
|
430
|
|
431 if (ctx->connect_counter == dict->connect_counter) {
|
|
432 t_push();
|
|
433 query = t_strdup_printf("%c%u", DICT_PROTOCOL_CMD_ROLLBACK,
|
|
434 ctx->id);
|
|
435 (void)client_dict_send_query(dict, query);
|
|
436 t_pop();
|
|
437 }
|
|
438 i_free(ctx);
|
|
439 }
|
|
440
|
|
441 static void client_dict_set(struct dict_transaction_context *_ctx,
|
|
442 const char *key, const char *value)
|
|
443 {
|
|
444 struct client_dict_transaction_context *ctx =
|
|
445 (struct client_dict_transaction_context *)_ctx;
|
|
446 struct client_dict *dict = (struct client_dict *)_ctx->dict;
|
|
447 const char *query;
|
|
448
|
|
449 if (ctx->connect_counter != dict->connect_counter)
|
|
450 return;
|
|
451
|
|
452 t_push();
|
|
453 query = t_strdup_printf("%c%u\t%s\t%s", DICT_PROTOCOL_CMD_SET, ctx->id,
|
|
454 dict_client_escape(key),
|
|
455 dict_client_escape(value));
|
|
456 if (client_dict_send_query(dict, query) < 0)
|
|
457 ctx->failed = TRUE;
|
|
458 t_pop();
|
|
459 }
|
|
460
|
|
461 static void client_dict_atomic_inc(struct dict_transaction_context *_ctx,
|
|
462 const char *key, long long diff)
|
|
463 {
|
|
464 struct client_dict_transaction_context *ctx =
|
|
465 (struct client_dict_transaction_context *)_ctx;
|
|
466 struct client_dict *dict = (struct client_dict *)_ctx->dict;
|
|
467 const char *query;
|
|
468
|
|
469 if (ctx->connect_counter != dict->connect_counter)
|
|
470 return;
|
|
471
|
|
472 t_push();
|
|
473 query = t_strdup_printf("%c%u\t%s\t%lld", DICT_PROTOCOL_CMD_ATOMIC_INC,
|
|
474 ctx->id, dict_client_escape(key), diff);
|
|
475 if (client_dict_send_query(dict, query) < 0)
|
|
476 ctx->failed = TRUE;
|
|
477 t_pop();
|
|
478 }
|
|
479
|
|
480 static struct dict client_dict = {
|
|
481 MEMBER(name) "proxy",
|
|
482
|
|
483 {
|
|
484 client_dict_init,
|
|
485 client_dict_deinit,
|
|
486 client_dict_lookup,
|
|
487 client_dict_iterate_init,
|
|
488 client_dict_iterate,
|
|
489 client_dict_iterate_deinit,
|
|
490 client_dict_transaction_init,
|
|
491 client_dict_transaction_commit,
|
|
492 client_dict_transaction_rollback,
|
|
493 client_dict_set,
|
|
494 client_dict_atomic_inc
|
|
495 }
|
|
496 };
|
|
497
|
|
498 void dict_client_register(void)
|
|
499 {
|
|
500 dict_class_register(&client_dict);
|
|
501 }
|
|
502
|
|
503 void dict_client_unregister(void)
|
|
504 {
|
|
505 dict_class_unregister(&client_dict);
|
|
506 }
|